blob: ee17ef3315f8aae6b5ccb27d2616c7c8b9ccb202 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: rslist - Linked list utilities
4 *
5 ******************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
45#include <acpi/acresrc.h>
46
47#define _COMPONENT ACPI_RESOURCES
Len Brown4be44fc2005-08-05 00:44:28 -040048ACPI_MODULE_NAME("rslist")
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Robert Moorebda663d2005-09-16 16:51:15 -040050/* Local prototypes */
Bob Moore08978312005-10-21 00:00:00 -040051static struct acpi_rsconvert_info *acpi_rs_get_conversion_info(u8
52 resource_type);
Robert Moorebda663d2005-09-16 16:51:15 -040053
Bob Moore50eca3e2005-09-30 19:03:00 -040054static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml);
Robert Moorebda663d2005-09-16 16:51:15 -040055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*******************************************************************************
57 *
Bob Moore50eca3e2005-09-30 19:03:00 -040058 * FUNCTION: acpi_rs_validate_resource_length
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *
Bob Moore50eca3e2005-09-30 19:03:00 -040060 * PARAMETERS: Aml - Pointer to the AML resource descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
Bob Moore50eca3e2005-09-30 19:03:00 -040062 * RETURN: Status - AE_OK if the resource length appears valid
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
Bob Moore50eca3e2005-09-30 19:03:00 -040064 * DESCRIPTION: Validate the resource_length. Fixed-length descriptors must
65 * have the exact length; variable-length descriptors must be
66 * at least as long as the minimum. Certain Small descriptors
67 * can vary in size by at most one byte.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 *
69 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Bob Moore50eca3e2005-09-30 19:03:00 -040071static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml)
Robert Moorebda663d2005-09-16 16:51:15 -040072{
Bob Moore50eca3e2005-09-30 19:03:00 -040073 struct acpi_resource_info *resource_info;
74 u16 minimum_aml_resource_length;
75 u16 resource_length;
76
Len Brown4be44fc2005-08-05 00:44:28 -040077 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Bob Moore50eca3e2005-09-30 19:03:00 -040079 /* Get the size and type info about this resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -040080
Bob Moore50eca3e2005-09-30 19:03:00 -040081 resource_info =
82 acpi_rs_get_resource_info(aml->small_header.descriptor_type);
83 if (!resource_info) {
84 return (AE_AML_INVALID_RESOURCE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Bob Moore50eca3e2005-09-30 19:03:00 -040086
Bob Moore08978312005-10-21 00:00:00 -040087 resource_length = acpi_ut_get_resource_length(aml);
Bob Moore50eca3e2005-09-30 19:03:00 -040088 minimum_aml_resource_length =
89 resource_info->minimum_aml_resource_length;
90
91 /* Validate based upon the type of resource, fixed length or variable */
92
93 if (resource_info->length_type == ACPI_FIXED_LENGTH) {
94 /* Fixed length resource, length must match exactly */
95
96 if (resource_length != minimum_aml_resource_length) {
97 return (AE_AML_BAD_RESOURCE_LENGTH);
98 }
99 } else if (resource_info->length_type == ACPI_VARIABLE_LENGTH) {
100 /* Variable length resource, must be at least the minimum */
101
102 if (resource_length < minimum_aml_resource_length) {
103 return (AE_AML_BAD_RESOURCE_LENGTH);
104 }
105 } else {
106 /* Small variable length resource, allowed to be (Min) or (Min-1) */
107
108 if ((resource_length > minimum_aml_resource_length) ||
109 (resource_length < (minimum_aml_resource_length - 1))) {
110 return (AE_AML_BAD_RESOURCE_LENGTH);
111 }
112 }
113
114 return (AE_OK);
Robert Moorebda663d2005-09-16 16:51:15 -0400115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Robert Moorebda663d2005-09-16 16:51:15 -0400117/*******************************************************************************
118 *
Bob Moore08978312005-10-21 00:00:00 -0400119 * FUNCTION: acpi_rs_get_conversion_info
Robert Moorebda663d2005-09-16 16:51:15 -0400120 *
121 * PARAMETERS: resource_type - Byte 0 of a resource descriptor
122 *
Bob Moore08978312005-10-21 00:00:00 -0400123 * RETURN: Pointer to the resource conversion info table
Robert Moorebda663d2005-09-16 16:51:15 -0400124 *
Bob Moore08978312005-10-21 00:00:00 -0400125 * DESCRIPTION: Get the conversion table associated with this resource type
Robert Moorebda663d2005-09-16 16:51:15 -0400126 *
127 ******************************************************************************/
128
Bob Moore08978312005-10-21 00:00:00 -0400129static struct acpi_rsconvert_info *acpi_rs_get_conversion_info(u8 resource_type)
Robert Moorebda663d2005-09-16 16:51:15 -0400130{
131 ACPI_FUNCTION_ENTRY();
132
133 /* Determine if this is a small or large resource */
134
Bob Moore50eca3e2005-09-30 19:03:00 -0400135 if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
Robert Moorebda663d2005-09-16 16:51:15 -0400136 /* Large Resource Type -- bits 6:0 contain the name */
137
Bob Moore50eca3e2005-09-30 19:03:00 -0400138 if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
Robert Moorebda663d2005-09-16 16:51:15 -0400139 return (NULL);
140 }
141
Bob Moore50eca3e2005-09-30 19:03:00 -0400142 return (acpi_gbl_lg_get_resource_dispatch[(resource_type &
143 ACPI_RESOURCE_NAME_LARGE_MASK)]);
Robert Moorebda663d2005-09-16 16:51:15 -0400144 } else {
145 /* Small Resource Type -- bits 6:3 contain the name */
146
Bob Moore50eca3e2005-09-30 19:03:00 -0400147 return (acpi_gbl_sm_get_resource_dispatch[((resource_type &
148 ACPI_RESOURCE_NAME_SMALL_MASK)
149 >> 3)]);
Robert Moorebda663d2005-09-16 16:51:15 -0400150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*******************************************************************************
154 *
Bob Moore50eca3e2005-09-30 19:03:00 -0400155 * FUNCTION: acpi_rs_convert_aml_to_resources
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *
Bob Moore50eca3e2005-09-30 19:03:00 -0400157 * PARAMETERS: aml_buffer - Pointer to the resource byte stream
158 * aml_buffer_length - Length of aml_buffer
159 * output_buffer - Pointer to the buffer that will
160 * contain the output structures
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 *
162 * RETURN: Status
163 *
164 * DESCRIPTION: Takes the resource byte stream and parses it, creating a
165 * linked list of resources in the caller's output buffer
166 *
167 ******************************************************************************/
168
169acpi_status
Bob Moore50eca3e2005-09-30 19:03:00 -0400170acpi_rs_convert_aml_to_resources(u8 * aml_buffer,
171 u32 aml_buffer_length, u8 * output_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Robert Moorebda663d2005-09-16 16:51:15 -0400173 u8 *buffer = output_buffer;
Len Brown4be44fc2005-08-05 00:44:28 -0400174 acpi_status status;
175 acpi_size bytes_parsed = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400176 struct acpi_resource *resource;
Bob Moore08978312005-10-21 00:00:00 -0400177 acpi_rsdesc_size descriptor_length;
178 struct acpi_rsconvert_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Bob Moore50eca3e2005-09-30 19:03:00 -0400180 ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Robert Moorebda663d2005-09-16 16:51:15 -0400182 /* Loop until end-of-buffer or an end_tag is found */
Robert Moore44f6c012005-04-18 22:49:35 -0400183
Bob Moore50eca3e2005-09-30 19:03:00 -0400184 while (bytes_parsed < aml_buffer_length) {
Bob Moore08978312005-10-21 00:00:00 -0400185 /* Get the conversion table associated with this Descriptor Type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Bob Moore08978312005-10-21 00:00:00 -0400187 info = acpi_rs_get_conversion_info(*aml_buffer);
188 if (!info) {
189 /* No table indicates an invalid resource type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Bob Moore50eca3e2005-09-30 19:03:00 -0400191 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
Bob Moore08978312005-10-21 00:00:00 -0400194 descriptor_length = acpi_ut_get_descriptor_length(aml_buffer);
Bob Moore50eca3e2005-09-30 19:03:00 -0400195
196 /*
197 * Perform limited validation of the resource length, based upon
198 * what we know about the resource type
199 */
200 status =
201 acpi_rs_validate_resource_length(ACPI_CAST_PTR
202 (union aml_resource,
203 aml_buffer));
Len Brown4be44fc2005-08-05 00:44:28 -0400204 if (ACPI_FAILURE(status)) {
205 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
Bob Moore08978312005-10-21 00:00:00 -0400208 /* Convert the AML byte stream resource to a local resource struct */
Bob Moore50eca3e2005-09-30 19:03:00 -0400209
Bob Moore08978312005-10-21 00:00:00 -0400210 status =
211 acpi_rs_convert_aml_to_resource(ACPI_CAST_PTR
212 (struct acpi_resource,
213 buffer),
214 ACPI_CAST_PTR(union
215 aml_resource,
216 aml_buffer),
217 info);
Bob Moore50eca3e2005-09-30 19:03:00 -0400218 if (ACPI_FAILURE(status)) {
219 ACPI_REPORT_ERROR(("Could not convert AML resource (type %X) to resource, %s\n", *aml_buffer, acpi_format_exception(status)));
220 return_ACPI_STATUS(status);
221 }
222
Robert Moorebda663d2005-09-16 16:51:15 -0400223 /* Set the aligned length of the new resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400224
Len Brown4be44fc2005-08-05 00:44:28 -0400225 resource = ACPI_CAST_PTR(struct acpi_resource, buffer);
226 resource->length =
227 (u32) ACPI_ALIGN_RESOURCE_SIZE(resource->length);
Robert Moorebda663d2005-09-16 16:51:15 -0400228
229 /* Normal exit on completion of an end_tag resource descriptor */
230
Bob Moore08978312005-10-21 00:00:00 -0400231 if (acpi_ut_get_resource_type(aml_buffer) ==
Bob Moore50eca3e2005-09-30 19:03:00 -0400232 ACPI_RESOURCE_NAME_END_TAG) {
Robert Moorebda663d2005-09-16 16:51:15 -0400233 return_ACPI_STATUS(AE_OK);
234 }
235
236 /* Update counter and point to the next input resource */
237
Bob Moore50eca3e2005-09-30 19:03:00 -0400238 bytes_parsed += descriptor_length;
239 aml_buffer += descriptor_length;
Robert Moorebda663d2005-09-16 16:51:15 -0400240
241 /* Point to the next structure in the output buffer */
242
Bob Moore50eca3e2005-09-30 19:03:00 -0400243 buffer += resource->length;
Robert Moore44f6c012005-04-18 22:49:35 -0400244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Robert Moorebda663d2005-09-16 16:51:15 -0400246 /* Completed buffer, but did not find an end_tag resource descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Robert Moorebda663d2005-09-16 16:51:15 -0400248 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*******************************************************************************
252 *
Bob Moore50eca3e2005-09-30 19:03:00 -0400253 * FUNCTION: acpi_rs_convert_resources_to_aml
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *
Bob Moore50eca3e2005-09-30 19:03:00 -0400255 * PARAMETERS: Resource - Pointer to the resource linked list
256 * aml_size_needed - Calculated size of the byte stream
257 * needed from calling acpi_rs_get_aml_length()
258 * The size of the output_buffer is
259 * guaranteed to be >= aml_size_needed
260 * output_buffer - Pointer to the buffer that will
261 * contain the byte stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
263 * RETURN: Status
264 *
265 * DESCRIPTION: Takes the resource linked list and parses it, creating a
266 * byte stream of resources in the caller's output buffer
267 *
268 ******************************************************************************/
269
270acpi_status
Bob Moore50eca3e2005-09-30 19:03:00 -0400271acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
272 acpi_size aml_size_needed, u8 * output_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Bob Moore50eca3e2005-09-30 19:03:00 -0400274 u8 *aml_buffer = output_buffer;
Bob Moore08978312005-10-21 00:00:00 -0400275 u8 *end_aml_buffer = output_buffer + aml_size_needed;
Robert Moorebda663d2005-09-16 16:51:15 -0400276 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Bob Moore50eca3e2005-09-30 19:03:00 -0400278 ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Bob Moore08978312005-10-21 00:00:00 -0400280 /* Walk the resource descriptor list, convert each descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Bob Moore08978312005-10-21 00:00:00 -0400282 while (aml_buffer < end_aml_buffer) {
283 /* Validate the Resource Type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Bob Moore50eca3e2005-09-30 19:03:00 -0400285 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
Len Brown4be44fc2005-08-05 00:44:28 -0400286 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
287 "Invalid descriptor type (%X) in resource list\n",
Robert Moorebda663d2005-09-16 16:51:15 -0400288 resource->type));
289 return_ACPI_STATUS(AE_BAD_DATA);
Robert Moore44f6c012005-04-18 22:49:35 -0400290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Bob Moore08978312005-10-21 00:00:00 -0400292 /* Perform the conversion */
Robert Moorebda663d2005-09-16 16:51:15 -0400293
Bob Moore08978312005-10-21 00:00:00 -0400294 status = acpi_rs_convert_resource_to_aml(resource,
295 ACPI_CAST_PTR(union
296 aml_resource,
297 aml_buffer),
298 acpi_gbl_set_resource_dispatch
299 [resource->type]);
Bob Moore50eca3e2005-09-30 19:03:00 -0400300 if (ACPI_FAILURE(status)) {
301 ACPI_REPORT_ERROR(("Could not convert resource (type %X) to AML, %s\n", resource->type, acpi_format_exception(status)));
302 return_ACPI_STATUS(status);
303 }
304
305 /* Perform final sanity check on the new AML resource descriptor */
306
307 status =
308 acpi_rs_validate_resource_length(ACPI_CAST_PTR
309 (union aml_resource,
310 aml_buffer));
Len Brown4be44fc2005-08-05 00:44:28 -0400311 if (ACPI_FAILURE(status)) {
312 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
Bob Moore50eca3e2005-09-30 19:03:00 -0400315 /* Check for end-of-list, normal exit */
Robert Moorebda663d2005-09-16 16:51:15 -0400316
Bob Moore50eca3e2005-09-30 19:03:00 -0400317 if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
318 /* An End Tag indicates the end of the input Resource Template */
Robert Moorebda663d2005-09-16 16:51:15 -0400319
320 return_ACPI_STATUS(AE_OK);
321 }
322
Bob Moore08978312005-10-21 00:00:00 -0400323 /*
324 * Extract the total length of the new descriptor and set the
325 * aml_buffer to point to the next (output) resource descriptor
326 */
327 aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Bob Moore50eca3e2005-09-30 19:03:00 -0400329 /* Point to the next input resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400330
Bob Moore50eca3e2005-09-30 19:03:00 -0400331 resource =
332 ACPI_PTR_ADD(struct acpi_resource, resource,
333 resource->length);
Bob Moore08978312005-10-21 00:00:00 -0400334
335 /* Check for end-of-list, normal exit */
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Bob Moore08978312005-10-21 00:00:00 -0400338
339 /* Completed buffer, but did not find an end_tag resource descriptor */
340
341 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}