Path: blob/main/sys/contrib/dev/acpica/components/resources/rslist.c
48406 views
/*******************************************************************************1*2* Module Name: rslist - Linked list utilities3*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>154155#define _COMPONENT ACPI_RESOURCES156ACPI_MODULE_NAME ("rslist")157158159/*******************************************************************************160*161* FUNCTION: AcpiRsConvertAmlToResources162*163* PARAMETERS: ACPI_WALK_AML_CALLBACK164* ResourcePtr - Pointer to the buffer that will165* contain the output structures166*167* RETURN: Status168*169* DESCRIPTION: Convert an AML resource to an internal representation of the170* resource that is aligned and easier to access.171*172******************************************************************************/173174ACPI_STATUS175AcpiRsConvertAmlToResources (176UINT8 *Aml,177UINT32 Length,178UINT32 Offset,179UINT8 ResourceIndex,180void **Context)181{182ACPI_RESOURCE **ResourcePtr = ACPI_CAST_INDIRECT_PTR (183ACPI_RESOURCE, Context);184ACPI_RESOURCE *Resource;185AML_RESOURCE *AmlResource;186ACPI_RSCONVERT_INFO *ConversionTable;187ACPI_STATUS Status;188189190ACPI_FUNCTION_TRACE (RsConvertAmlToResources);191192193/*194* Check that the input buffer and all subsequent pointers into it195* are aligned on a native word boundary. Most important on IA64196*/197Resource = *ResourcePtr;198if (ACPI_IS_MISALIGNED (Resource))199{200ACPI_WARNING ((AE_INFO,201"Misaligned resource pointer %p", Resource));202}203204/* Get the appropriate conversion info table */205206AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);207208if (AcpiUtGetResourceType (Aml) ==209ACPI_RESOURCE_NAME_SERIAL_BUS)210{211if (AmlResource->CommonSerialBus.Type >212AML_RESOURCE_MAX_SERIALBUSTYPE)213{214ConversionTable = NULL;215}216else217{218/* This is an I2C, SPI, UART, or CSI2 SerialBus descriptor */219220ConversionTable = AcpiGbl_ConvertResourceSerialBusDispatch [221AmlResource->CommonSerialBus.Type];222}223}224else225{226ConversionTable = AcpiGbl_GetResourceDispatch[ResourceIndex];227}228229if (!ConversionTable)230{231ACPI_ERROR ((AE_INFO,232"Invalid/unsupported resource descriptor: Type 0x%2.2X",233ResourceIndex));234return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);235}236237/* Convert the AML byte stream resource to a local resource struct */238239Status = AcpiRsConvertAmlToResource (240Resource, AmlResource, ConversionTable);241if (ACPI_FAILURE (Status))242{243ACPI_EXCEPTION ((AE_INFO, Status,244"Could not convert AML resource (Type 0x%X)", *Aml));245return_ACPI_STATUS (Status);246}247248if (!Resource->Length)249{250ACPI_EXCEPTION ((AE_INFO, Status,251"Zero-length resource returned from RsConvertAmlToResource"));252}253254ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,255"Type %.2X, AmlLength %.2X InternalLength %.2X\n",256AcpiUtGetResourceType (Aml), Length,257Resource->Length));258259/* Point to the next structure in the output buffer */260261*ResourcePtr = ACPI_NEXT_RESOURCE (Resource);262return_ACPI_STATUS (AE_OK);263}264265266/*******************************************************************************267*268* FUNCTION: AcpiRsConvertResourcesToAml269*270* PARAMETERS: Resource - Pointer to the resource linked list271* AmlSizeNeeded - Calculated size of the byte stream272* needed from calling AcpiRsGetAmlLength()273* The size of the OutputBuffer is274* guaranteed to be >= AmlSizeNeeded275* OutputBuffer - Pointer to the buffer that will276* contain the byte stream277*278* RETURN: Status279*280* DESCRIPTION: Takes the resource linked list and parses it, creating a281* byte stream of resources in the caller's output buffer282*283******************************************************************************/284285ACPI_STATUS286AcpiRsConvertResourcesToAml (287ACPI_RESOURCE *Resource,288ACPI_SIZE AmlSizeNeeded,289UINT8 *OutputBuffer)290{291UINT8 *Aml = OutputBuffer;292UINT8 *EndAml = OutputBuffer + AmlSizeNeeded;293ACPI_RSCONVERT_INFO *ConversionTable;294ACPI_STATUS Status;295296297ACPI_FUNCTION_TRACE (RsConvertResourcesToAml);298299300/* Walk the resource descriptor list, convert each descriptor */301302while (Aml < EndAml)303{304/* Validate the (internal) Resource Type */305306if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)307{308ACPI_ERROR ((AE_INFO,309"Invalid descriptor type (0x%X) in resource list",310Resource->Type));311return_ACPI_STATUS (AE_BAD_DATA);312}313314/* Sanity check the length. It must not be zero, or we loop forever */315316if (!Resource->Length)317{318ACPI_ERROR ((AE_INFO,319"Invalid zero length descriptor in resource list\n"));320return_ACPI_STATUS (AE_AML_BAD_RESOURCE_LENGTH);321}322323/* Perform the conversion */324325if (Resource->Type == ACPI_RESOURCE_TYPE_SERIAL_BUS)326{327if (Resource->Data.CommonSerialBus.Type >328AML_RESOURCE_MAX_SERIALBUSTYPE)329{330ConversionTable = NULL;331}332else333{334/* This is an I2C, SPI, UART or CSI2 SerialBus descriptor */335336ConversionTable = AcpiGbl_ConvertResourceSerialBusDispatch[337Resource->Data.CommonSerialBus.Type];338}339}340else341{342ConversionTable = AcpiGbl_SetResourceDispatch[Resource->Type];343}344345if (!ConversionTable)346{347ACPI_ERROR ((AE_INFO,348"Invalid/unsupported resource descriptor: Type 0x%2.2X",349Resource->Type));350return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);351}352353Status = AcpiRsConvertResourceToAml (Resource,354ACPI_CAST_PTR (AML_RESOURCE, Aml), ConversionTable);355if (ACPI_FAILURE (Status))356{357ACPI_EXCEPTION ((AE_INFO, Status,358"Could not convert resource (type 0x%X) to AML",359Resource->Type));360return_ACPI_STATUS (Status);361}362363/* Perform final sanity check on the new AML resource descriptor */364365Status = AcpiUtValidateResource (366NULL, ACPI_CAST_PTR (AML_RESOURCE, Aml), NULL);367if (ACPI_FAILURE (Status))368{369return_ACPI_STATUS (Status);370}371372/* Check for end-of-list, normal exit */373374if (Resource->Type == ACPI_RESOURCE_TYPE_END_TAG)375{376/* An End Tag indicates the end of the input Resource Template */377378return_ACPI_STATUS (AE_OK);379}380381/*382* Extract the total length of the new descriptor and set the383* Aml to point to the next (output) resource descriptor384*/385Aml += AcpiUtGetDescriptorLength (Aml);386387/* Point to the next input resource descriptor */388389Resource = ACPI_NEXT_RESOURCE (Resource);390}391392/* Completed buffer, but did not find an EndTag resource descriptor */393394return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);395}396397398