xref: /freebsd/sys/contrib/dev/acpica/components/resources/rslist.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*******************************************************************************
2  *
3  * Module Name: rslist - Linked list utilities
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, 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 #define __RSLIST_C__
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acresrc.h>
49 
50 #define _COMPONENT          ACPI_RESOURCES
51         ACPI_MODULE_NAME    ("rslist")
52 
53 
54 /*******************************************************************************
55  *
56  * FUNCTION:    AcpiRsConvertAmlToResources
57  *
58  * PARAMETERS:  ACPI_WALK_AML_CALLBACK
59  *              ResourcePtr             - Pointer to the buffer that will
60  *                                        contain the output structures
61  *
62  * RETURN:      Status
63  *
64  * DESCRIPTION: Convert an AML resource to an internal representation of the
65  *              resource that is aligned and easier to access.
66  *
67  ******************************************************************************/
68 
69 ACPI_STATUS
70 AcpiRsConvertAmlToResources (
71     UINT8                   *Aml,
72     UINT32                  Length,
73     UINT32                  Offset,
74     UINT8                   ResourceIndex,
75     void                    **Context)
76 {
77     ACPI_RESOURCE           **ResourcePtr = ACPI_CAST_INDIRECT_PTR (
78                                 ACPI_RESOURCE, Context);
79     ACPI_RESOURCE           *Resource;
80     AML_RESOURCE            *AmlResource;
81     ACPI_RSCONVERT_INFO     *ConversionTable;
82     ACPI_STATUS             Status;
83 
84 
85     ACPI_FUNCTION_TRACE (RsConvertAmlToResources);
86 
87 
88     /*
89      * Check that the input buffer and all subsequent pointers into it
90      * are aligned on a native word boundary. Most important on IA64
91      */
92     Resource = *ResourcePtr;
93     if (ACPI_IS_MISALIGNED (Resource))
94     {
95         ACPI_WARNING ((AE_INFO,
96             "Misaligned resource pointer %p", Resource));
97     }
98 
99     /* Get the appropriate conversion info table */
100 
101     AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
102     if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_SERIAL_BUS)
103     {
104         if (AmlResource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE)
105         {
106             ConversionTable = NULL;
107         }
108         else
109         {
110             /* This is an I2C, SPI, or UART SerialBus descriptor */
111 
112             ConversionTable =
113                 AcpiGbl_ConvertResourceSerialBusDispatch[
114                     AmlResource->CommonSerialBus.Type];
115         }
116     }
117     else
118     {
119         ConversionTable =
120             AcpiGbl_GetResourceDispatch[ResourceIndex];
121     }
122 
123     if (!ConversionTable)
124     {
125         ACPI_ERROR ((AE_INFO,
126             "Invalid/unsupported resource descriptor: Type 0x%2.2X",
127             ResourceIndex));
128         return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
129     }
130 
131      /* Convert the AML byte stream resource to a local resource struct */
132 
133     Status = AcpiRsConvertAmlToResource (
134         Resource, AmlResource, ConversionTable);
135     if (ACPI_FAILURE (Status))
136     {
137         ACPI_EXCEPTION ((AE_INFO, Status,
138             "Could not convert AML resource (Type 0x%X)", *Aml));
139         return_ACPI_STATUS (Status);
140     }
141 
142     ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,
143         "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
144         AcpiUtGetResourceType (Aml), Length,
145         Resource->Length));
146 
147     /* Point to the next structure in the output buffer */
148 
149     *ResourcePtr = ACPI_NEXT_RESOURCE (Resource);
150     return_ACPI_STATUS (AE_OK);
151 }
152 
153 
154 /*******************************************************************************
155  *
156  * FUNCTION:    AcpiRsConvertResourcesToAml
157  *
158  * PARAMETERS:  Resource            - Pointer to the resource linked list
159  *              AmlSizeNeeded       - Calculated size of the byte stream
160  *                                    needed from calling AcpiRsGetAmlLength()
161  *                                    The size of the OutputBuffer is
162  *                                    guaranteed to be >= AmlSizeNeeded
163  *              OutputBuffer        - Pointer to the buffer that will
164  *                                    contain the byte stream
165  *
166  * RETURN:      Status
167  *
168  * DESCRIPTION: Takes the resource linked list and parses it, creating a
169  *              byte stream of resources in the caller's output buffer
170  *
171  ******************************************************************************/
172 
173 ACPI_STATUS
174 AcpiRsConvertResourcesToAml (
175     ACPI_RESOURCE           *Resource,
176     ACPI_SIZE               AmlSizeNeeded,
177     UINT8                   *OutputBuffer)
178 {
179     UINT8                   *Aml = OutputBuffer;
180     UINT8                   *EndAml = OutputBuffer + AmlSizeNeeded;
181     ACPI_RSCONVERT_INFO     *ConversionTable;
182     ACPI_STATUS             Status;
183 
184 
185     ACPI_FUNCTION_TRACE (RsConvertResourcesToAml);
186 
187 
188     /* Walk the resource descriptor list, convert each descriptor */
189 
190     while (Aml < EndAml)
191     {
192         /* Validate the (internal) Resource Type */
193 
194         if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
195         {
196             ACPI_ERROR ((AE_INFO,
197                 "Invalid descriptor type (0x%X) in resource list",
198                 Resource->Type));
199             return_ACPI_STATUS (AE_BAD_DATA);
200         }
201 
202         /* Sanity check the length. It must not be zero, or we loop forever */
203 
204         if (!Resource->Length)
205         {
206             ACPI_ERROR ((AE_INFO,
207                 "Invalid zero length descriptor in resource list\n"));
208             return_ACPI_STATUS (AE_AML_BAD_RESOURCE_LENGTH);
209         }
210 
211         /* Perform the conversion */
212 
213         if (Resource->Type == ACPI_RESOURCE_TYPE_SERIAL_BUS)
214         {
215             if (Resource->Data.CommonSerialBus.Type > 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),
242                 ConversionTable);
243         if (ACPI_FAILURE (Status))
244         {
245             ACPI_EXCEPTION ((AE_INFO, Status,
246                 "Could not convert resource (type 0x%X) to AML",
247                 Resource->Type));
248             return_ACPI_STATUS (Status);
249         }
250 
251         /* Perform final sanity check on the new AML resource descriptor */
252 
253         Status = AcpiUtValidateResource (NULL,
254                     ACPI_CAST_PTR (AML_RESOURCE, Aml), NULL);
255         if (ACPI_FAILURE (Status))
256         {
257             return_ACPI_STATUS (Status);
258         }
259 
260         /* Check for end-of-list, normal exit */
261 
262         if (Resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
263         {
264             /* An End Tag indicates the end of the input Resource Template */
265 
266             return_ACPI_STATUS (AE_OK);
267         }
268 
269         /*
270          * Extract the total length of the new descriptor and set the
271          * Aml to point to the next (output) resource descriptor
272          */
273         Aml += AcpiUtGetDescriptorLength (Aml);
274 
275         /* Point to the next input resource descriptor */
276 
277         Resource = ACPI_NEXT_RESOURCE (Resource);
278     }
279 
280     /* Completed buffer, but did not find an EndTag resource descriptor */
281 
282     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
283 }
284