1214571Sdim/*******************************************************************************
2214571Sdim *
3214571Sdim * Module Name: rslist - Linked list utilities
4214571Sdim *
5214571Sdim ******************************************************************************/
6214571Sdim
7214571Sdim/*
8214571Sdim * Copyright (C) 2000 - 2011, Intel Corp.
9214571Sdim * All rights reserved.
10214571Sdim *
11214571Sdim * Redistribution and use in source and binary forms, with or without
12214571Sdim * modification, are permitted provided that the following conditions
13214571Sdim * are met:
14214571Sdim * 1. Redistributions of source code must retain the above copyright
15214571Sdim *    notice, this list of conditions, and the following disclaimer,
16214571Sdim *    without modification.
17214571Sdim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18214571Sdim *    substantially similar to the "NO WARRANTY" disclaimer below
19214571Sdim *    ("Disclaimer") and any redistribution must be conditioned upon
20214571Sdim *    including a substantially similar Disclaimer requirement for further
21214571Sdim *    binary redistribution.
22214571Sdim * 3. Neither the names of the above-listed copyright holders nor the names
23214571Sdim *    of any contributors may be used to endorse or promote products derived
24214571Sdim *    from this software without specific prior written permission.
25214571Sdim *
26214571Sdim * Alternatively, this software may be distributed under the terms of the
27214571Sdim * GNU General Public License ("GPL") version 2 as published by the Free
28214571Sdim * Software Foundation.
29214571Sdim *
30214571Sdim * NO WARRANTY
31214571Sdim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32214571Sdim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33214571Sdim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34214571Sdim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35214571Sdim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36214571Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37214571Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38214571Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39214571Sdim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40214571Sdim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41214571Sdim * POSSIBILITY OF SUCH DAMAGES.
42214571Sdim */
43214571Sdim
44214571Sdim#define __RSLIST_C__
45214571Sdim
46214571Sdim#include <contrib/dev/acpica/include/acpi.h>
47214571Sdim#include <contrib/dev/acpica/include/accommon.h>
48214571Sdim#include <contrib/dev/acpica/include/acresrc.h>
49214571Sdim
50214571Sdim#define _COMPONENT          ACPI_RESOURCES
51214571Sdim        ACPI_MODULE_NAME    ("rslist")
52214571Sdim
53214571Sdim
54214571Sdim/*******************************************************************************
55214571Sdim *
56214571Sdim * FUNCTION:    AcpiRsConvertAmlToResources
57214571Sdim *
58214571Sdim * PARAMETERS:  ACPI_WALK_AML_CALLBACK
59214571Sdim *              ResourcePtr             - Pointer to the buffer that will
60214571Sdim *                                        contain the output structures
61214571Sdim *
62214571Sdim * RETURN:      Status
63214571Sdim *
64214571Sdim * DESCRIPTION: Convert an AML resource to an internal representation of the
65214571Sdim *              resource that is aligned and easier to access.
66214571Sdim *
67214571Sdim ******************************************************************************/
68214571Sdim
69214571SdimACPI_STATUS
70214571SdimAcpiRsConvertAmlToResources (
71214571Sdim    UINT8                   *Aml,
72214571Sdim    UINT32                  Length,
73214571Sdim    UINT32                  Offset,
74214571Sdim    UINT8                   ResourceIndex,
75214571Sdim    void                    *Context)
76214571Sdim{
77214571Sdim    ACPI_RESOURCE           **ResourcePtr = ACPI_CAST_INDIRECT_PTR (
78214571Sdim                                ACPI_RESOURCE, Context);
79214571Sdim    ACPI_RESOURCE           *Resource;
80214571Sdim    ACPI_STATUS             Status;
81214571Sdim
82214571Sdim
83214571Sdim    ACPI_FUNCTION_TRACE (RsConvertAmlToResources);
84214571Sdim
85214571Sdim
86214571Sdim    /*
87214571Sdim     * Check that the input buffer and all subsequent pointers into it
88214571Sdim     * are aligned on a native word boundary. Most important on IA64
89214571Sdim     */
90214571Sdim    Resource = *ResourcePtr;
91214571Sdim    if (ACPI_IS_MISALIGNED (Resource))
92214571Sdim    {
93214571Sdim        ACPI_WARNING ((AE_INFO,
94214571Sdim            "Misaligned resource pointer %p", Resource));
95214571Sdim    }
96214571Sdim
97214571Sdim    /* Convert the AML byte stream resource to a local resource struct */
98214571Sdim
99214571Sdim    Status = AcpiRsConvertAmlToResource (
100214571Sdim                Resource, ACPI_CAST_PTR (AML_RESOURCE, Aml),
101214571Sdim                AcpiGbl_GetResourceDispatch[ResourceIndex]);
102214571Sdim    if (ACPI_FAILURE (Status))
103214571Sdim    {
104214571Sdim        ACPI_EXCEPTION ((AE_INFO, Status,
105214571Sdim            "Could not convert AML resource (Type 0x%X)", *Aml));
106214571Sdim        return_ACPI_STATUS (Status);
107214571Sdim    }
108214571Sdim
109214571Sdim    ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,
110214571Sdim        "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
111214571Sdim        AcpiUtGetResourceType (Aml), Length,
112214571Sdim        Resource->Length));
113214571Sdim
114214571Sdim    /* Point to the next structure in the output buffer */
115214571Sdim
116214571Sdim    *ResourcePtr = ACPI_ADD_PTR (void, Resource, Resource->Length);
117214571Sdim    return_ACPI_STATUS (AE_OK);
118214571Sdim}
119214571Sdim
120214571Sdim
121214571Sdim/*******************************************************************************
122214571Sdim *
123214571Sdim * FUNCTION:    AcpiRsConvertResourcesToAml
124214571Sdim *
125214571Sdim * PARAMETERS:  Resource            - Pointer to the resource linked list
126214571Sdim *              AmlSizeNeeded       - Calculated size of the byte stream
127 *                                    needed from calling AcpiRsGetAmlLength()
128 *                                    The size of the OutputBuffer is
129 *                                    guaranteed to be >= AmlSizeNeeded
130 *              OutputBuffer        - Pointer to the buffer that will
131 *                                    contain the byte stream
132 *
133 * RETURN:      Status
134 *
135 * DESCRIPTION: Takes the resource linked list and parses it, creating a
136 *              byte stream of resources in the caller's output buffer
137 *
138 ******************************************************************************/
139
140ACPI_STATUS
141AcpiRsConvertResourcesToAml (
142    ACPI_RESOURCE           *Resource,
143    ACPI_SIZE               AmlSizeNeeded,
144    UINT8                   *OutputBuffer)
145{
146    UINT8                   *Aml = OutputBuffer;
147    UINT8                   *EndAml = OutputBuffer + AmlSizeNeeded;
148    ACPI_STATUS             Status;
149
150
151    ACPI_FUNCTION_TRACE (RsConvertResourcesToAml);
152
153
154    /* Walk the resource descriptor list, convert each descriptor */
155
156    while (Aml < EndAml)
157    {
158        /* Validate the (internal) Resource Type */
159
160        if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
161        {
162            ACPI_ERROR ((AE_INFO,
163                "Invalid descriptor type (0x%X) in resource list",
164                Resource->Type));
165            return_ACPI_STATUS (AE_BAD_DATA);
166        }
167
168        /* Perform the conversion */
169
170        Status = AcpiRsConvertResourceToAml (Resource,
171                    ACPI_CAST_PTR (AML_RESOURCE, Aml),
172                    AcpiGbl_SetResourceDispatch[Resource->Type]);
173        if (ACPI_FAILURE (Status))
174        {
175            ACPI_EXCEPTION ((AE_INFO, Status,
176                "Could not convert resource (type 0x%X) to AML",
177                Resource->Type));
178            return_ACPI_STATUS (Status);
179        }
180
181        /* Perform final sanity check on the new AML resource descriptor */
182
183        Status = AcpiUtValidateResource (
184                    ACPI_CAST_PTR (AML_RESOURCE, Aml), NULL);
185        if (ACPI_FAILURE (Status))
186        {
187            return_ACPI_STATUS (Status);
188        }
189
190        /* Check for end-of-list, normal exit */
191
192        if (Resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
193        {
194            /* An End Tag indicates the end of the input Resource Template */
195
196            return_ACPI_STATUS (AE_OK);
197        }
198
199        /*
200         * Extract the total length of the new descriptor and set the
201         * Aml to point to the next (output) resource descriptor
202         */
203        Aml += AcpiUtGetDescriptorLength (Aml);
204
205        /* Point to the next input resource descriptor */
206
207        Resource = ACPI_ADD_PTR (ACPI_RESOURCE, Resource, Resource->Length);
208    }
209
210    /* Completed buffer, but did not find an EndTag resource descriptor */
211
212    return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
213}
214
215