blob: f72d42e0927b5b8841fdd3e14d1c14f4b7df72ff [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 Moore50eca3e2005-09-30 19:03:00 -040051static ACPI_GET_RESOURCE_HANDLER acpi_rs_get_resource_handler(u8 resource_type);
Robert Moorebda663d2005-09-16 16:51:15 -040052
Bob Moore50eca3e2005-09-30 19:03:00 -040053static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml);
Robert Moorebda663d2005-09-16 16:51:15 -040054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*******************************************************************************
56 *
Bob Moore50eca3e2005-09-30 19:03:00 -040057 * FUNCTION: acpi_rs_validate_resource_length
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 *
Bob Moore50eca3e2005-09-30 19:03:00 -040059 * PARAMETERS: Aml - Pointer to the AML resource descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
Bob Moore50eca3e2005-09-30 19:03:00 -040061 * RETURN: Status - AE_OK if the resource length appears valid
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
Bob Moore50eca3e2005-09-30 19:03:00 -040063 * DESCRIPTION: Validate the resource_length. Fixed-length descriptors must
64 * have the exact length; variable-length descriptors must be
65 * at least as long as the minimum. Certain Small descriptors
66 * can vary in size by at most one byte.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *
68 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Bob Moore50eca3e2005-09-30 19:03:00 -040070static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml)
Robert Moorebda663d2005-09-16 16:51:15 -040071{
Bob Moore50eca3e2005-09-30 19:03:00 -040072 struct acpi_resource_info *resource_info;
73 u16 minimum_aml_resource_length;
74 u16 resource_length;
75
Len Brown4be44fc2005-08-05 00:44:28 -040076 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Bob Moore50eca3e2005-09-30 19:03:00 -040078 /* Get the size and type info about this resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -040079
Bob Moore50eca3e2005-09-30 19:03:00 -040080 resource_info =
81 acpi_rs_get_resource_info(aml->small_header.descriptor_type);
82 if (!resource_info) {
83 return (AE_AML_INVALID_RESOURCE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Bob Moore50eca3e2005-09-30 19:03:00 -040085
86 resource_length = acpi_rs_get_resource_length(aml);
87 minimum_aml_resource_length =
88 resource_info->minimum_aml_resource_length;
89
90 /* Validate based upon the type of resource, fixed length or variable */
91
92 if (resource_info->length_type == ACPI_FIXED_LENGTH) {
93 /* Fixed length resource, length must match exactly */
94
95 if (resource_length != minimum_aml_resource_length) {
96 return (AE_AML_BAD_RESOURCE_LENGTH);
97 }
98 } else if (resource_info->length_type == ACPI_VARIABLE_LENGTH) {
99 /* Variable length resource, must be at least the minimum */
100
101 if (resource_length < minimum_aml_resource_length) {
102 return (AE_AML_BAD_RESOURCE_LENGTH);
103 }
104 } else {
105 /* Small variable length resource, allowed to be (Min) or (Min-1) */
106
107 if ((resource_length > minimum_aml_resource_length) ||
108 (resource_length < (minimum_aml_resource_length - 1))) {
109 return (AE_AML_BAD_RESOURCE_LENGTH);
110 }
111 }
112
113 return (AE_OK);
Robert Moorebda663d2005-09-16 16:51:15 -0400114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Robert Moorebda663d2005-09-16 16:51:15 -0400116/*******************************************************************************
117 *
118 * FUNCTION: acpi_rs_get_resource_handler
119 *
120 * PARAMETERS: resource_type - Byte 0 of a resource descriptor
121 *
122 * RETURN: Pointer to the resource conversion handler
123 *
124 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
125 * a resource descriptor.
126 *
127 ******************************************************************************/
128
Bob Moore50eca3e2005-09-30 19:03:00 -0400129static ACPI_GET_RESOURCE_HANDLER acpi_rs_get_resource_handler(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 Moore50eca3e2005-09-30 19:03:00 -0400177 u16 resource_length;
178 u32 descriptor_length;
179 ACPI_GET_RESOURCE_HANDLER handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Bob Moore50eca3e2005-09-30 19:03:00 -0400181 ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Robert Moorebda663d2005-09-16 16:51:15 -0400183 /* Loop until end-of-buffer or an end_tag is found */
Robert Moore44f6c012005-04-18 22:49:35 -0400184
Bob Moore50eca3e2005-09-30 19:03:00 -0400185 while (bytes_parsed < aml_buffer_length) {
Robert Moorebda663d2005-09-16 16:51:15 -0400186 /* Get the handler associated with this Descriptor Type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Bob Moore50eca3e2005-09-30 19:03:00 -0400188 handler = acpi_rs_get_resource_handler(*aml_buffer);
189 if (!handler) {
190 /* No handler indicates invalid resource type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Bob Moore50eca3e2005-09-30 19:03:00 -0400192 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
Bob Moore50eca3e2005-09-30 19:03:00 -0400195 resource_length =
196 acpi_rs_get_resource_length(ACPI_CAST_PTR
197 (union aml_resource,
198 aml_buffer));
199
200 descriptor_length =
201 acpi_rs_get_descriptor_length(ACPI_CAST_PTR
202 (union aml_resource,
203 aml_buffer));
204
205 /*
206 * Perform limited validation of the resource length, based upon
207 * what we know about the resource type
208 */
209 status =
210 acpi_rs_validate_resource_length(ACPI_CAST_PTR
211 (union aml_resource,
212 aml_buffer));
Len Brown4be44fc2005-08-05 00:44:28 -0400213 if (ACPI_FAILURE(status)) {
214 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Bob Moore50eca3e2005-09-30 19:03:00 -0400217 /* Convert a byte stream resource to local resource struct */
218
219 status = handler(ACPI_CAST_PTR(union aml_resource, aml_buffer),
220 resource_length,
221 ACPI_CAST_PTR(struct acpi_resource, buffer));
222 if (ACPI_FAILURE(status)) {
223 ACPI_REPORT_ERROR(("Could not convert AML resource (type %X) to resource, %s\n", *aml_buffer, acpi_format_exception(status)));
224 return_ACPI_STATUS(status);
225 }
226
Robert Moorebda663d2005-09-16 16:51:15 -0400227 /* Set the aligned length of the new resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400228
Len Brown4be44fc2005-08-05 00:44:28 -0400229 resource = ACPI_CAST_PTR(struct acpi_resource, buffer);
230 resource->length =
231 (u32) ACPI_ALIGN_RESOURCE_SIZE(resource->length);
Robert Moorebda663d2005-09-16 16:51:15 -0400232
233 /* Normal exit on completion of an end_tag resource descriptor */
234
Bob Moore50eca3e2005-09-30 19:03:00 -0400235 if (acpi_rs_get_resource_type(*aml_buffer) ==
236 ACPI_RESOURCE_NAME_END_TAG) {
Robert Moorebda663d2005-09-16 16:51:15 -0400237 return_ACPI_STATUS(AE_OK);
238 }
239
240 /* Update counter and point to the next input resource */
241
Bob Moore50eca3e2005-09-30 19:03:00 -0400242 bytes_parsed += descriptor_length;
243 aml_buffer += descriptor_length;
Robert Moorebda663d2005-09-16 16:51:15 -0400244
245 /* Point to the next structure in the output buffer */
246
Bob Moore50eca3e2005-09-30 19:03:00 -0400247 buffer += resource->length;
Robert Moore44f6c012005-04-18 22:49:35 -0400248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Robert Moorebda663d2005-09-16 16:51:15 -0400250 /* Completed buffer, but did not find an end_tag resource descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Robert Moorebda663d2005-09-16 16:51:15 -0400252 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*******************************************************************************
256 *
Bob Moore50eca3e2005-09-30 19:03:00 -0400257 * FUNCTION: acpi_rs_convert_resources_to_aml
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Bob Moore50eca3e2005-09-30 19:03:00 -0400259 * PARAMETERS: Resource - Pointer to the resource linked list
260 * aml_size_needed - Calculated size of the byte stream
261 * needed from calling acpi_rs_get_aml_length()
262 * The size of the output_buffer is
263 * guaranteed to be >= aml_size_needed
264 * output_buffer - Pointer to the buffer that will
265 * contain the byte stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
267 * RETURN: Status
268 *
269 * DESCRIPTION: Takes the resource linked list and parses it, creating a
270 * byte stream of resources in the caller's output buffer
271 *
272 ******************************************************************************/
273
274acpi_status
Bob Moore50eca3e2005-09-30 19:03:00 -0400275acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
276 acpi_size aml_size_needed, u8 * output_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Bob Moore50eca3e2005-09-30 19:03:00 -0400278 u8 *aml_buffer = output_buffer;
Robert Moorebda663d2005-09-16 16:51:15 -0400279 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Bob Moore50eca3e2005-09-30 19:03:00 -0400281 ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Robert Moorebda663d2005-09-16 16:51:15 -0400283 /* Convert each resource descriptor in the list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Robert Moorebda663d2005-09-16 16:51:15 -0400285 while (1) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400286 /* Validate Resource Descriptor Type before dispatch */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Bob Moore50eca3e2005-09-30 19:03:00 -0400288 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
Len Brown4be44fc2005-08-05 00:44:28 -0400289 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
290 "Invalid descriptor type (%X) in resource list\n",
Robert Moorebda663d2005-09-16 16:51:15 -0400291 resource->type));
292 return_ACPI_STATUS(AE_BAD_DATA);
Robert Moore44f6c012005-04-18 22:49:35 -0400293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Bob Moore50eca3e2005-09-30 19:03:00 -0400295 /* Perform the conversion per resource type */
Robert Moorebda663d2005-09-16 16:51:15 -0400296
Bob Moore50eca3e2005-09-30 19:03:00 -0400297 status =
298 acpi_gbl_set_resource_dispatch[resource->type] (resource,
299 ACPI_CAST_PTR
300 (union
301 aml_resource,
302 aml_buffer));
303 if (ACPI_FAILURE(status)) {
304 ACPI_REPORT_ERROR(("Could not convert resource (type %X) to AML, %s\n", resource->type, acpi_format_exception(status)));
305 return_ACPI_STATUS(status);
306 }
307
308 /* Perform final sanity check on the new AML resource descriptor */
309
310 status =
311 acpi_rs_validate_resource_length(ACPI_CAST_PTR
312 (union aml_resource,
313 aml_buffer));
Len Brown4be44fc2005-08-05 00:44:28 -0400314 if (ACPI_FAILURE(status)) {
315 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
Bob Moore50eca3e2005-09-30 19:03:00 -0400318 /* Check for end-of-list, normal exit */
Robert Moorebda663d2005-09-16 16:51:15 -0400319
Bob Moore50eca3e2005-09-30 19:03:00 -0400320 if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
321 /* An End Tag indicates the end of the input Resource Template */
Robert Moorebda663d2005-09-16 16:51:15 -0400322
323 return_ACPI_STATUS(AE_OK);
324 }
325
Bob Moore50eca3e2005-09-30 19:03:00 -0400326 /* Extract the total length of the new descriptor */
327 /* Set the aml_buffer to point to the next (output) resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400328
Bob Moore50eca3e2005-09-30 19:03:00 -0400329 aml_buffer +=
330 acpi_rs_get_descriptor_length(ACPI_CAST_PTR
331 (union aml_resource,
332 aml_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Bob Moore50eca3e2005-09-30 19:03:00 -0400334 /* Point to the next input resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400335
Bob Moore50eca3e2005-09-30 19:03:00 -0400336 resource =
337 ACPI_PTR_ADD(struct acpi_resource, resource,
338 resource->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}