rslist.c revision 231844
1/*******************************************************************************
2 *
3 * Module Name: rslist - Linked list utilities
4 *
5 ******************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2012, 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
69ACPI_STATUS
70AcpiRsConvertAmlToResources (
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 (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
173ACPI_STATUS
174AcpiRsConvertResourcesToAml (
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        /* Perform the conversion */
203
204        if (Resource->Type == ACPI_RESOURCE_TYPE_SERIAL_BUS)
205        {
206            if (Resource->Data.CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE)
207            {
208                ConversionTable = NULL;
209            }
210            else
211            {
212                /* This is an I2C, SPI, or UART SerialBus descriptor */
213
214                ConversionTable = AcpiGbl_ConvertResourceSerialBusDispatch[
215                    Resource->Data.CommonSerialBus.Type];
216            }
217        }
218        else
219        {
220            ConversionTable = AcpiGbl_SetResourceDispatch[Resource->Type];
221        }
222
223        if (!ConversionTable)
224        {
225            ACPI_ERROR ((AE_INFO,
226                "Invalid/unsupported resource descriptor: Type 0x%2.2X",
227                Resource->Type));
228            return (AE_AML_INVALID_RESOURCE_TYPE);
229        }
230
231        Status = AcpiRsConvertResourceToAml (Resource,
232                ACPI_CAST_PTR (AML_RESOURCE, Aml),
233                ConversionTable);
234        if (ACPI_FAILURE (Status))
235        {
236            ACPI_EXCEPTION ((AE_INFO, Status,
237                "Could not convert resource (type 0x%X) to AML",
238                Resource->Type));
239            return_ACPI_STATUS (Status);
240        }
241
242        /* Perform final sanity check on the new AML resource descriptor */
243
244        Status = AcpiUtValidateResource (
245                    ACPI_CAST_PTR (AML_RESOURCE, Aml), NULL);
246        if (ACPI_FAILURE (Status))
247        {
248            return_ACPI_STATUS (Status);
249        }
250
251        /* Check for end-of-list, normal exit */
252
253        if (Resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
254        {
255            /* An End Tag indicates the end of the input Resource Template */
256
257            return_ACPI_STATUS (AE_OK);
258        }
259
260        /*
261         * Extract the total length of the new descriptor and set the
262         * Aml to point to the next (output) resource descriptor
263         */
264        Aml += AcpiUtGetDescriptorLength (Aml);
265
266        /* Point to the next input resource descriptor */
267
268        Resource = ACPI_NEXT_RESOURCE (Resource);
269    }
270
271    /* Completed buffer, but did not find an EndTag resource descriptor */
272
273    return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
274}
275
276