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