1c2c66affSColin Finck /*******************************************************************************
2c2c66affSColin Finck  *
3c2c66affSColin Finck  * Module Name: rslist - Linked list utilities
4c2c66affSColin Finck  *
5c2c66affSColin Finck  ******************************************************************************/
6c2c66affSColin Finck 
7c2c66affSColin Finck /*
8*03b24380SThomas Faber  * Copyright (C) 2000 - 2022, Intel Corp.
9c2c66affSColin Finck  * All rights reserved.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * Redistribution and use in source and binary forms, with or without
12c2c66affSColin Finck  * modification, are permitted provided that the following conditions
13c2c66affSColin Finck  * are met:
14c2c66affSColin Finck  * 1. Redistributions of source code must retain the above copyright
15c2c66affSColin Finck  *    notice, this list of conditions, and the following disclaimer,
16c2c66affSColin Finck  *    without modification.
17c2c66affSColin Finck  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18c2c66affSColin Finck  *    substantially similar to the "NO WARRANTY" disclaimer below
19c2c66affSColin Finck  *    ("Disclaimer") and any redistribution must be conditioned upon
20c2c66affSColin Finck  *    including a substantially similar Disclaimer requirement for further
21c2c66affSColin Finck  *    binary redistribution.
22c2c66affSColin Finck  * 3. Neither the names of the above-listed copyright holders nor the names
23c2c66affSColin Finck  *    of any contributors may be used to endorse or promote products derived
24c2c66affSColin Finck  *    from this software without specific prior written permission.
25c2c66affSColin Finck  *
26c2c66affSColin Finck  * Alternatively, this software may be distributed under the terms of the
27c2c66affSColin Finck  * GNU General Public License ("GPL") version 2 as published by the Free
28c2c66affSColin Finck  * Software Foundation.
29c2c66affSColin Finck  *
30c2c66affSColin Finck  * NO WARRANTY
31c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32c2c66affSColin Finck  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
336eb8cc49SThomas Faber  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34c2c66affSColin Finck  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35c2c66affSColin Finck  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36c2c66affSColin Finck  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37c2c66affSColin Finck  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38c2c66affSColin Finck  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39c2c66affSColin Finck  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40c2c66affSColin Finck  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41c2c66affSColin Finck  * POSSIBILITY OF SUCH DAMAGES.
42c2c66affSColin Finck  */
43c2c66affSColin Finck 
44c2c66affSColin Finck #include "acpi.h"
45c2c66affSColin Finck #include "accommon.h"
46c2c66affSColin Finck #include "acresrc.h"
47c2c66affSColin Finck 
48c2c66affSColin Finck #define _COMPONENT          ACPI_RESOURCES
49c2c66affSColin Finck         ACPI_MODULE_NAME    ("rslist")
50c2c66affSColin Finck 
51c2c66affSColin Finck 
52c2c66affSColin Finck /*******************************************************************************
53c2c66affSColin Finck  *
54c2c66affSColin Finck  * FUNCTION:    AcpiRsConvertAmlToResources
55c2c66affSColin Finck  *
56c2c66affSColin Finck  * PARAMETERS:  ACPI_WALK_AML_CALLBACK
57c2c66affSColin Finck  *              ResourcePtr             - Pointer to the buffer that will
58c2c66affSColin Finck  *                                        contain the output structures
59c2c66affSColin Finck  *
60c2c66affSColin Finck  * RETURN:      Status
61c2c66affSColin Finck  *
62c2c66affSColin Finck  * DESCRIPTION: Convert an AML resource to an internal representation of the
63c2c66affSColin Finck  *              resource that is aligned and easier to access.
64c2c66affSColin Finck  *
65c2c66affSColin Finck  ******************************************************************************/
66c2c66affSColin Finck 
67c2c66affSColin Finck ACPI_STATUS
AcpiRsConvertAmlToResources(UINT8 * Aml,UINT32 Length,UINT32 Offset,UINT8 ResourceIndex,void ** Context)68c2c66affSColin Finck AcpiRsConvertAmlToResources (
69c2c66affSColin Finck     UINT8                   *Aml,
70c2c66affSColin Finck     UINT32                  Length,
71c2c66affSColin Finck     UINT32                  Offset,
72c2c66affSColin Finck     UINT8                   ResourceIndex,
73c2c66affSColin Finck     void                    **Context)
74c2c66affSColin Finck {
75c2c66affSColin Finck     ACPI_RESOURCE           **ResourcePtr = ACPI_CAST_INDIRECT_PTR (
76c2c66affSColin Finck                                 ACPI_RESOURCE, Context);
77c2c66affSColin Finck     ACPI_RESOURCE           *Resource;
78c2c66affSColin Finck     AML_RESOURCE            *AmlResource;
79c2c66affSColin Finck     ACPI_RSCONVERT_INFO     *ConversionTable;
80c2c66affSColin Finck     ACPI_STATUS             Status;
81c2c66affSColin Finck 
82c2c66affSColin Finck 
83c2c66affSColin Finck     ACPI_FUNCTION_TRACE (RsConvertAmlToResources);
84c2c66affSColin Finck 
85c2c66affSColin Finck 
86c2c66affSColin Finck     /*
87c2c66affSColin Finck      * Check that the input buffer and all subsequent pointers into it
88c2c66affSColin Finck      * are aligned on a native word boundary. Most important on IA64
89c2c66affSColin Finck      */
90c2c66affSColin Finck     Resource = *ResourcePtr;
91c2c66affSColin Finck     if (ACPI_IS_MISALIGNED (Resource))
92c2c66affSColin Finck     {
93c2c66affSColin Finck         ACPI_WARNING ((AE_INFO,
94c2c66affSColin Finck             "Misaligned resource pointer %p", Resource));
95c2c66affSColin Finck     }
96c2c66affSColin Finck 
97c2c66affSColin Finck     /* Get the appropriate conversion info table */
98c2c66affSColin Finck 
99c2c66affSColin Finck     AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
100c2c66affSColin Finck 
101c2c66affSColin Finck     if (AcpiUtGetResourceType (Aml) ==
102c2c66affSColin Finck         ACPI_RESOURCE_NAME_SERIAL_BUS)
103c2c66affSColin Finck     {
104c2c66affSColin Finck         if (AmlResource->CommonSerialBus.Type >
105c2c66affSColin Finck             AML_RESOURCE_MAX_SERIALBUSTYPE)
106c2c66affSColin Finck         {
107c2c66affSColin Finck             ConversionTable = NULL;
108c2c66affSColin Finck         }
109c2c66affSColin Finck         else
110c2c66affSColin Finck         {
1116eb8cc49SThomas Faber             /* This is an I2C, SPI, UART, or CSI2 SerialBus descriptor */
112c2c66affSColin Finck 
113c2c66affSColin Finck             ConversionTable = AcpiGbl_ConvertResourceSerialBusDispatch [
114c2c66affSColin Finck                 AmlResource->CommonSerialBus.Type];
115c2c66affSColin Finck         }
116c2c66affSColin Finck     }
117c2c66affSColin Finck     else
118c2c66affSColin Finck     {
119c2c66affSColin Finck         ConversionTable = AcpiGbl_GetResourceDispatch[ResourceIndex];
120c2c66affSColin Finck     }
121c2c66affSColin Finck 
122c2c66affSColin Finck     if (!ConversionTable)
123c2c66affSColin Finck     {
124c2c66affSColin Finck         ACPI_ERROR ((AE_INFO,
125c2c66affSColin Finck             "Invalid/unsupported resource descriptor: Type 0x%2.2X",
126c2c66affSColin Finck             ResourceIndex));
127c2c66affSColin Finck         return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
128c2c66affSColin Finck     }
129c2c66affSColin Finck 
130c2c66affSColin Finck      /* Convert the AML byte stream resource to a local resource struct */
131c2c66affSColin Finck 
132c2c66affSColin Finck     Status = AcpiRsConvertAmlToResource (
133c2c66affSColin Finck         Resource, AmlResource, ConversionTable);
134c2c66affSColin Finck     if (ACPI_FAILURE (Status))
135c2c66affSColin Finck     {
136c2c66affSColin Finck         ACPI_EXCEPTION ((AE_INFO, Status,
137c2c66affSColin Finck             "Could not convert AML resource (Type 0x%X)", *Aml));
138c2c66affSColin Finck         return_ACPI_STATUS (Status);
139c2c66affSColin Finck     }
140c2c66affSColin Finck 
1416eb8cc49SThomas Faber     if (!Resource->Length)
1426eb8cc49SThomas Faber     {
1436eb8cc49SThomas Faber         ACPI_EXCEPTION ((AE_INFO, Status,
1446eb8cc49SThomas Faber             "Zero-length resource returned from RsConvertAmlToResource"));
1456eb8cc49SThomas Faber     }
1466eb8cc49SThomas Faber 
147c2c66affSColin Finck     ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,
148c2c66affSColin Finck         "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
149c2c66affSColin Finck         AcpiUtGetResourceType (Aml), Length,
150c2c66affSColin Finck         Resource->Length));
151c2c66affSColin Finck 
152c2c66affSColin Finck     /* Point to the next structure in the output buffer */
153c2c66affSColin Finck 
154c2c66affSColin Finck     *ResourcePtr = ACPI_NEXT_RESOURCE (Resource);
155c2c66affSColin Finck     return_ACPI_STATUS (AE_OK);
156c2c66affSColin Finck }
157c2c66affSColin Finck 
158c2c66affSColin Finck 
159c2c66affSColin Finck /*******************************************************************************
160c2c66affSColin Finck  *
161c2c66affSColin Finck  * FUNCTION:    AcpiRsConvertResourcesToAml
162c2c66affSColin Finck  *
163c2c66affSColin Finck  * PARAMETERS:  Resource            - Pointer to the resource linked list
164c2c66affSColin Finck  *              AmlSizeNeeded       - Calculated size of the byte stream
165c2c66affSColin Finck  *                                    needed from calling AcpiRsGetAmlLength()
166c2c66affSColin Finck  *                                    The size of the OutputBuffer is
167c2c66affSColin Finck  *                                    guaranteed to be >= AmlSizeNeeded
168c2c66affSColin Finck  *              OutputBuffer        - Pointer to the buffer that will
169c2c66affSColin Finck  *                                    contain the byte stream
170c2c66affSColin Finck  *
171c2c66affSColin Finck  * RETURN:      Status
172c2c66affSColin Finck  *
173c2c66affSColin Finck  * DESCRIPTION: Takes the resource linked list and parses it, creating a
174c2c66affSColin Finck  *              byte stream of resources in the caller's output buffer
175c2c66affSColin Finck  *
176c2c66affSColin Finck  ******************************************************************************/
177c2c66affSColin Finck 
178c2c66affSColin Finck ACPI_STATUS
AcpiRsConvertResourcesToAml(ACPI_RESOURCE * Resource,ACPI_SIZE AmlSizeNeeded,UINT8 * OutputBuffer)179c2c66affSColin Finck AcpiRsConvertResourcesToAml (
180c2c66affSColin Finck     ACPI_RESOURCE           *Resource,
181c2c66affSColin Finck     ACPI_SIZE               AmlSizeNeeded,
182c2c66affSColin Finck     UINT8                   *OutputBuffer)
183c2c66affSColin Finck {
184c2c66affSColin Finck     UINT8                   *Aml = OutputBuffer;
185c2c66affSColin Finck     UINT8                   *EndAml = OutputBuffer + AmlSizeNeeded;
186c2c66affSColin Finck     ACPI_RSCONVERT_INFO     *ConversionTable;
187c2c66affSColin Finck     ACPI_STATUS             Status;
188c2c66affSColin Finck 
189c2c66affSColin Finck 
190c2c66affSColin Finck     ACPI_FUNCTION_TRACE (RsConvertResourcesToAml);
191c2c66affSColin Finck 
192c2c66affSColin Finck 
193c2c66affSColin Finck     /* Walk the resource descriptor list, convert each descriptor */
194c2c66affSColin Finck 
195c2c66affSColin Finck     while (Aml < EndAml)
196c2c66affSColin Finck     {
197c2c66affSColin Finck         /* Validate the (internal) Resource Type */
198c2c66affSColin Finck 
199c2c66affSColin Finck         if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
200c2c66affSColin Finck         {
201c2c66affSColin Finck             ACPI_ERROR ((AE_INFO,
202c2c66affSColin Finck                 "Invalid descriptor type (0x%X) in resource list",
203c2c66affSColin Finck                 Resource->Type));
204c2c66affSColin Finck             return_ACPI_STATUS (AE_BAD_DATA);
205c2c66affSColin Finck         }
206c2c66affSColin Finck 
207c2c66affSColin Finck         /* Sanity check the length. It must not be zero, or we loop forever */
208c2c66affSColin Finck 
209c2c66affSColin Finck         if (!Resource->Length)
210c2c66affSColin Finck         {
211c2c66affSColin Finck             ACPI_ERROR ((AE_INFO,
212c2c66affSColin Finck                 "Invalid zero length descriptor in resource list\n"));
213c2c66affSColin Finck             return_ACPI_STATUS (AE_AML_BAD_RESOURCE_LENGTH);
214c2c66affSColin Finck         }
215c2c66affSColin Finck 
216c2c66affSColin Finck         /* Perform the conversion */
217c2c66affSColin Finck 
218c2c66affSColin Finck         if (Resource->Type == ACPI_RESOURCE_TYPE_SERIAL_BUS)
219c2c66affSColin Finck         {
220c2c66affSColin Finck             if (Resource->Data.CommonSerialBus.Type >
221c2c66affSColin Finck                 AML_RESOURCE_MAX_SERIALBUSTYPE)
222c2c66affSColin Finck             {
223c2c66affSColin Finck                 ConversionTable = NULL;
224c2c66affSColin Finck             }
225c2c66affSColin Finck             else
226c2c66affSColin Finck             {
2276eb8cc49SThomas Faber                 /* This is an I2C, SPI, UART or CSI2 SerialBus descriptor */
228c2c66affSColin Finck 
229c2c66affSColin Finck                 ConversionTable = AcpiGbl_ConvertResourceSerialBusDispatch[
230c2c66affSColin Finck                     Resource->Data.CommonSerialBus.Type];
231c2c66affSColin Finck             }
232c2c66affSColin Finck         }
233c2c66affSColin Finck         else
234c2c66affSColin Finck         {
235c2c66affSColin Finck             ConversionTable = AcpiGbl_SetResourceDispatch[Resource->Type];
236c2c66affSColin Finck         }
237c2c66affSColin Finck 
238c2c66affSColin Finck         if (!ConversionTable)
239c2c66affSColin Finck         {
240c2c66affSColin Finck             ACPI_ERROR ((AE_INFO,
241c2c66affSColin Finck                 "Invalid/unsupported resource descriptor: Type 0x%2.2X",
242c2c66affSColin Finck                 Resource->Type));
243c2c66affSColin Finck             return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
244c2c66affSColin Finck         }
245c2c66affSColin Finck 
246c2c66affSColin Finck         Status = AcpiRsConvertResourceToAml (Resource,
247c2c66affSColin Finck             ACPI_CAST_PTR (AML_RESOURCE, Aml), ConversionTable);
248c2c66affSColin Finck         if (ACPI_FAILURE (Status))
249c2c66affSColin Finck         {
250c2c66affSColin Finck             ACPI_EXCEPTION ((AE_INFO, Status,
251c2c66affSColin Finck                 "Could not convert resource (type 0x%X) to AML",
252c2c66affSColin Finck                 Resource->Type));
253c2c66affSColin Finck             return_ACPI_STATUS (Status);
254c2c66affSColin Finck         }
255c2c66affSColin Finck 
256c2c66affSColin Finck         /* Perform final sanity check on the new AML resource descriptor */
257c2c66affSColin Finck 
258c2c66affSColin Finck         Status = AcpiUtValidateResource (
259c2c66affSColin Finck             NULL, ACPI_CAST_PTR (AML_RESOURCE, Aml), NULL);
260c2c66affSColin Finck         if (ACPI_FAILURE (Status))
261c2c66affSColin Finck         {
262c2c66affSColin Finck             return_ACPI_STATUS (Status);
263c2c66affSColin Finck         }
264c2c66affSColin Finck 
265c2c66affSColin Finck         /* Check for end-of-list, normal exit */
266c2c66affSColin Finck 
267c2c66affSColin Finck         if (Resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
268c2c66affSColin Finck         {
269c2c66affSColin Finck             /* An End Tag indicates the end of the input Resource Template */
270c2c66affSColin Finck 
271c2c66affSColin Finck             return_ACPI_STATUS (AE_OK);
272c2c66affSColin Finck         }
273c2c66affSColin Finck 
274c2c66affSColin Finck         /*
275c2c66affSColin Finck          * Extract the total length of the new descriptor and set the
276c2c66affSColin Finck          * Aml to point to the next (output) resource descriptor
277c2c66affSColin Finck          */
278c2c66affSColin Finck         Aml += AcpiUtGetDescriptorLength (Aml);
279c2c66affSColin Finck 
280c2c66affSColin Finck         /* Point to the next input resource descriptor */
281c2c66affSColin Finck 
282c2c66affSColin Finck         Resource = ACPI_NEXT_RESOURCE (Resource);
283c2c66affSColin Finck     }
284c2c66affSColin Finck 
285c2c66affSColin Finck     /* Completed buffer, but did not find an EndTag resource descriptor */
286c2c66affSColin Finck 
287c2c66affSColin Finck     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
288c2c66affSColin Finck }
289