blob: cd051c97bf5b6cc4219f1fc909e05bee6583905a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: rscalc - Calculate stream and list lengths
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#include <acpi/amlcode.h>
Robert Moorebda663d2005-09-16 16:51:15 -040047#include <acpi/amlresrc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <acpi/acnamesp.h>
49
50#define _COMPONENT ACPI_RESOURCES
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("rscalc")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Robert Moorebda663d2005-09-16 16:51:15 -040053/*
54 * Base sizes for external resource descriptors, indexed by internal type.
55 * Includes size of the descriptor header (1 byte for small descriptors,
56 * 3 bytes for large descriptors)
57 */
58static u8 acpi_gbl_stream_sizes[] = {
59 4, /* ACPI_RSTYPE_IRQ (Byte 3 is optional, but always created) */
60 3, /* ACPI_RSTYPE_DMA */
61 2, /* ACPI_RSTYPE_START_DPF (Byte 1 is optional, but always created) */
62 1, /* ACPI_RSTYPE_END_DPF */
63 8, /* ACPI_RSTYPE_IO */
64 4, /* ACPI_RSTYPE_FIXED_IO */
65 1, /* ACPI_RSTYPE_VENDOR */
66 2, /* ACPI_RSTYPE_END_TAG */
67 12, /* ACPI_RSTYPE_MEM24 */
68 20, /* ACPI_RSTYPE_MEM32 */
69 12, /* ACPI_RSTYPE_FIXED_MEM32 */
70 16, /* ACPI_RSTYPE_ADDRESS16 */
71 26, /* ACPI_RSTYPE_ADDRESS32 */
72 46, /* ACPI_RSTYPE_ADDRESS64 */
73 9, /* ACPI_RSTYPE_EXT_IRQ */
74 15 /* ACPI_RSTYPE_GENERIC_REG */
75};
76
77/*
78 * Base sizes of resource descriptors, both the actual AML stream length and
79 * size of the internal struct representation.
80 */
81typedef struct acpi_resource_sizes {
82 u8 minimum_stream_size;
83 u8 minimum_struct_size;
84
85} ACPI_RESOURCE_SIZES;
86
87static ACPI_RESOURCE_SIZES acpi_gbl_sm_resource_sizes[] = {
88 0, 0, /* 0x00, Reserved */
89 0, 0, /* 0x01, Reserved */
90 0, 0, /* 0x02, Reserved */
91 0, 0, /* 0x03, Reserved */
92 3, ACPI_SIZEOF_RESOURCE(struct acpi_resource_irq), /* ACPI_RDESC_TYPE_IRQ_FORMAT */
93 3, ACPI_SIZEOF_RESOURCE(struct acpi_resource_dma), /* ACPI_RDESC_TYPE_DMA_FORMAT */
94 1, ACPI_SIZEOF_RESOURCE(struct acpi_resource_start_dpf), /* ACPI_RDESC_TYPE_START_DEPENDENT */
95 1, ACPI_RESOURCE_LENGTH, /* ACPI_RDESC_TYPE_END_DEPENDENT */
96 8, ACPI_SIZEOF_RESOURCE(struct acpi_resource_io), /* ACPI_RDESC_TYPE_IO_PORT */
97 4, ACPI_SIZEOF_RESOURCE(struct acpi_resource_fixed_io), /* ACPI_RDESC_TYPE_FIXED_IO_PORT */
98 0, 0, /* 0x0A, Reserved */
99 0, 0, /* 0x0B, Reserved */
100 0, 0, /* 0x0C, Reserved */
101 0, 0, /* 0x0D, Reserved */
102 1, ACPI_SIZEOF_RESOURCE(struct acpi_resource_vendor), /* ACPI_RDESC_TYPE_SMALL_VENDOR */
103 2, ACPI_RESOURCE_LENGTH, /* ACPI_RDESC_TYPE_END_TAG */
104};
105
106static ACPI_RESOURCE_SIZES acpi_gbl_lg_resource_sizes[] = {
107 0, 0, /* 0x00, Reserved */
108 12, ACPI_SIZEOF_RESOURCE(struct acpi_resource_mem24), /* ACPI_RDESC_TYPE_MEMORY_24 */
109 15, ACPI_SIZEOF_RESOURCE(struct acpi_resource_generic_reg), /* ACPI_RDESC_TYPE_GENERIC_REGISTER */
110 0, 0, /* 0x03, Reserved */
111 3, ACPI_SIZEOF_RESOURCE(struct acpi_resource_vendor), /* ACPI_RDESC_TYPE_LARGE_VENDOR */
112 20, ACPI_SIZEOF_RESOURCE(struct acpi_resource_mem32), /* ACPI_RDESC_TYPE_MEMORY_32 */
113 12, ACPI_SIZEOF_RESOURCE(struct acpi_resource_fixed_mem32), /* ACPI_RDESC_TYPE_FIXED_MEMORY_32 */
114 26, ACPI_SIZEOF_RESOURCE(struct acpi_resource_address32), /* ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE */
115 16, ACPI_SIZEOF_RESOURCE(struct acpi_resource_address16), /* ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE */
116 9, ACPI_SIZEOF_RESOURCE(struct acpi_resource_ext_irq), /* ACPI_RDESC_TYPE_EXTENDED_XRUPT */
117 46, ACPI_SIZEOF_RESOURCE(struct acpi_resource_address64), /* ACPI_RDESC_TYPE_QWORD_ADDRESS_SPACE */
118 56, ACPI_SIZEOF_RESOURCE(struct acpi_resource_address64), /* ACPI_RDESC_TYPE_EXTENDED_ADDRESS_SPACE */
119};
120
121/* Local prototypes */
122
123static u8 acpi_rs_count_set_bits(u16 bit_field);
124
125static ACPI_RESOURCE_SIZES *acpi_rs_get_resource_sizes(u8 resource_type);
126
127static u16 acpi_rs_get_resource_length(u8 * resource);
128
129static acpi_size
130acpi_rs_struct_option_length(struct acpi_resource_source *resource_source);
131
132static u32
133acpi_rs_stream_option_length(u32 resource_length, u32 minimum_total_length);
134
135/*******************************************************************************
136 *
137 * FUNCTION: acpi_rs_count_set_bits
138 *
139 * PARAMETERS: bit_field - Field in which to count bits
140 *
141 * RETURN: Number of bits set within the field
142 *
143 * DESCRIPTION: Count the number of bits set in a resource field. Used for
144 * (Short descriptor) interrupt and DMA lists.
145 *
146 ******************************************************************************/
147
148static u8 acpi_rs_count_set_bits(u16 bit_field)
149{
150 u8 bits_set;
151
152 ACPI_FUNCTION_ENTRY();
153
154 for (bits_set = 0; bit_field; bits_set++) {
155 /* Zero the least significant bit that is set */
156
157 bit_field &= (bit_field - 1);
158 }
159
160 return (bits_set);
161}
162
163/*******************************************************************************
164 *
165 * FUNCTION: acpi_rs_get_resource_sizes
166 *
167 * PARAMETERS: resource_type - Byte 0 of a resource descriptor
168 *
169 * RETURN: Pointer to the resource conversion handler
170 *
171 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
172 * a resource descriptor.
173 *
174 ******************************************************************************/
175
176static ACPI_RESOURCE_SIZES *acpi_rs_get_resource_sizes(u8 resource_type)
177{
178 ACPI_RESOURCE_SIZES *size_info;
179
180 ACPI_FUNCTION_ENTRY();
181
182 /* Determine if this is a small or large resource */
183
184 if (resource_type & ACPI_RDESC_TYPE_LARGE) {
185 /* Large Resource Type -- bits 6:0 contain the name */
186
187 if (resource_type > ACPI_RDESC_LARGE_MAX) {
188 return (NULL);
189 }
190
191 size_info = &acpi_gbl_lg_resource_sizes[(resource_type &
192 ACPI_RDESC_LARGE_MASK)];
193 } else {
194 /* Small Resource Type -- bits 6:3 contain the name */
195
196 size_info = &acpi_gbl_sm_resource_sizes[((resource_type &
197 ACPI_RDESC_SMALL_MASK)
198 >> 3)];
199 }
200
201 /* Zero entry indicates an invalid resource type */
202
203 if (!size_info->minimum_stream_size) {
204 return (NULL);
205 }
206
207 return (size_info);
208}
209
210/*******************************************************************************
211 *
212 * FUNCTION: acpi_rs_get_resource_length
213 *
214 * PARAMETERS: Resource - Pointer to the resource descriptor
215 *
216 * RETURN: Byte length of the (AML byte stream) descriptor. By definition,
217 * this does not include the size of the descriptor header and the
218 * length field itself.
219 *
220 * DESCRIPTION: Extract the length of a resource descriptor.
221 *
222 ******************************************************************************/
223
224static u16 acpi_rs_get_resource_length(u8 * resource)
225{
226 u16 resource_length;
227
228 ACPI_FUNCTION_ENTRY();
229
230 /* Determine if this is a small or large resource */
231
232 if (*resource & ACPI_RDESC_TYPE_LARGE) {
233 /* Large Resource type -- length is in bytes 1-2 */
234
235 ACPI_MOVE_16_TO_16(&resource_length, (resource + 1));
236
237 } else {
238 /* Small Resource Type -- bits 2:0 of byte 0 contain the length */
239
240 resource_length =
241 (u16) (*resource & ACPI_RDESC_SMALL_LENGTH_MASK);
242 }
243
244 return (resource_length);
245}
246
247/*******************************************************************************
248 *
249 * FUNCTION: acpi_rs_struct_option_length
250 *
251 * PARAMETERS: resource_source - Pointer to optional descriptor field
252 *
253 * RETURN: Status
254 *
255 * DESCRIPTION: Common code to handle optional resource_source_index and
256 * resource_source fields in some Large descriptors. Used during
257 * list-to-stream conversion
258 *
259 ******************************************************************************/
260
261static acpi_size
262acpi_rs_struct_option_length(struct acpi_resource_source *resource_source)
263{
264 ACPI_FUNCTION_ENTRY();
265
266 /*
267 * If the resource_source string is valid, return the size of the string
268 * (string_length includes the NULL terminator) plus the size of the
269 * resource_source_index (1).
270 */
271 if (resource_source->string_ptr) {
272 return ((acpi_size) resource_source->string_length + 1);
273 }
274
275 return (0);
276}
277
278/*******************************************************************************
279 *
280 * FUNCTION: acpi_rs_stream_option_length
281 *
282 * PARAMETERS: resource_length - Length from the resource header
283 * minimum_total_length - Minimum length of this resource, before
284 * any optional fields. Includes header size
285 *
286 * RETURN: Length of optional string (0 if no string present)
287 *
288 * DESCRIPTION: Common code to handle optional resource_source_index and
289 * resource_source fields in some Large descriptors. Used during
290 * stream-to-list conversion
291 *
292 ******************************************************************************/
293
294static u32
295acpi_rs_stream_option_length(u32 resource_length, u32 minimum_total_length)
296{
297 u32 string_length = 0;
298 u32 minimum_resource_length;
299
300 ACPI_FUNCTION_ENTRY();
301
302 /*
303 * The resource_source_index and resource_source are optional elements of some
304 * Large-type resource descriptors.
305 */
306
307 /* Compute minimum size of the data part of the resource descriptor */
308
309 minimum_resource_length =
310 minimum_total_length - sizeof(struct asl_large_header);
311
312 /*
313 * If the length of the actual resource descriptor is greater than the ACPI
314 * spec-defined minimum length, it means that a resource_source_index exists
315 * and is followed by a (required) null terminated string. The string length
316 * (including the null terminator) is the resource length minus the minimum
317 * length, minus one byte for the resource_source_index itself.
318 */
319 if (resource_length > minimum_resource_length) {
320 /* Compute the length of the optional string */
321
322 string_length = resource_length - minimum_resource_length - 1;
323 }
324
325 /* Round up length to 32 bits for internal structure alignment */
326
327 return (ACPI_ROUND_UP_to_32_bITS(string_length));
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*******************************************************************************
331 *
332 * FUNCTION: acpi_rs_get_byte_stream_length
333 *
Robert Moorebda663d2005-09-16 16:51:15 -0400334 * PARAMETERS: Resource - Pointer to the resource linked list
335 * size_needed - Where the required size is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
337 * RETURN: Status
338 *
Robert Moorebda663d2005-09-16 16:51:15 -0400339 * DESCRIPTION: Takes a linked list of internal resource descriptors and
340 * calculates the size buffer needed to hold the corresponding
341 * external resource byte stream.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 ******************************************************************************/
Robert Moorebda663d2005-09-16 16:51:15 -0400344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345acpi_status
Robert Moorebda663d2005-09-16 16:51:15 -0400346acpi_rs_get_byte_stream_length(struct acpi_resource * resource,
Len Brown4be44fc2005-08-05 00:44:28 -0400347 acpi_size * size_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Len Brown4be44fc2005-08-05 00:44:28 -0400349 acpi_size byte_stream_size_needed = 0;
350 acpi_size segment_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Len Brown4be44fc2005-08-05 00:44:28 -0400352 ACPI_FUNCTION_TRACE("rs_get_byte_stream_length");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Robert Moorebda663d2005-09-16 16:51:15 -0400354 /* Traverse entire list of internal resource descriptors */
Robert Moore44f6c012005-04-18 22:49:35 -0400355
Robert Moorebda663d2005-09-16 16:51:15 -0400356 while (resource) {
357 /* Validate the descriptor type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Robert Moorebda663d2005-09-16 16:51:15 -0400359 if (resource->type > ACPI_RSTYPE_MAX) {
360 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Robert Moorebda663d2005-09-16 16:51:15 -0400363 /* Get the base size of the (external stream) resource descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Robert Moorebda663d2005-09-16 16:51:15 -0400365 segment_size = acpi_gbl_stream_sizes[resource->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Robert Moorebda663d2005-09-16 16:51:15 -0400367 /*
368 * Augment the base size for descriptors with optional and/or
369 * variable-length fields
370 */
371 switch (resource->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 case ACPI_RSTYPE_VENDOR:
373 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400374 * Vendor Defined Resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * For a Vendor Specific resource, if the Length is between 1 and 7
376 * it will be created as a Small Resource data type, otherwise it
377 * is a Large Resource data type.
378 */
Robert Moorebda663d2005-09-16 16:51:15 -0400379 if (resource->data.vendor_specific.length > 7) {
380 /* Base size of a Large resource descriptor */
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 segment_size = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Robert Moorebda663d2005-09-16 16:51:15 -0400384
385 /* Add the size of the vendor-specific data */
386
387 segment_size += resource->data.vendor_specific.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 break;
389
390 case ACPI_RSTYPE_END_TAG:
391 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400392 * End Tag:
393 * We are done -- return the accumulated total size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Robert Moorebda663d2005-09-16 16:51:15 -0400395 *size_needed = byte_stream_size_needed + segment_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Robert Moorebda663d2005-09-16 16:51:15 -0400397 /* Normal exit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Robert Moorebda663d2005-09-16 16:51:15 -0400399 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 case ACPI_RSTYPE_ADDRESS16:
402 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400403 * 16-Bit Address Resource:
404 * Add the size of the optional resource_source info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Robert Moorebda663d2005-09-16 16:51:15 -0400406 segment_size +=
407 acpi_rs_struct_option_length(&resource->data.
408 address16.
409 resource_source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
411
412 case ACPI_RSTYPE_ADDRESS32:
413 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400414 * 32-Bit Address Resource:
415 * Add the size of the optional resource_source info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
Robert Moorebda663d2005-09-16 16:51:15 -0400417 segment_size +=
418 acpi_rs_struct_option_length(&resource->data.
419 address32.
420 resource_source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422
423 case ACPI_RSTYPE_ADDRESS64:
424 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400425 * 64-Bit Address Resource:
426 * Add the size of the optional resource_source info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Robert Moorebda663d2005-09-16 16:51:15 -0400428 segment_size +=
429 acpi_rs_struct_option_length(&resource->data.
430 address64.
431 resource_source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 break;
433
434 case ACPI_RSTYPE_EXT_IRQ:
435 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400436 * Extended IRQ Resource:
437 * Add the size of each additional optional interrupt beyond the
438 * required 1 (4 bytes for each u32 interrupt number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
Robert Moorebda663d2005-09-16 16:51:15 -0400440 segment_size += (((acpi_size)
441 resource->data.extended_irq.
442 number_of_interrupts - 1) * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Robert Moorebda663d2005-09-16 16:51:15 -0400444 /* Add the size of the optional resource_source info */
445
446 segment_size +=
447 acpi_rs_struct_option_length(&resource->data.
448 extended_irq.
449 resource_source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451
452 default:
Robert Moorebda663d2005-09-16 16:51:15 -0400453 break;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Robert Moore44f6c012005-04-18 22:49:35 -0400456 /* Update the total */
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 byte_stream_size_needed += segment_size;
459
Robert Moore44f6c012005-04-18 22:49:35 -0400460 /* Point to the next object */
461
Robert Moorebda663d2005-09-16 16:51:15 -0400462 resource = ACPI_PTR_ADD(struct acpi_resource,
463 resource, resource->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Robert Moorebda663d2005-09-16 16:51:15 -0400466 /* Did not find an END_TAG descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400467
Robert Moorebda663d2005-09-16 16:51:15 -0400468 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*******************************************************************************
472 *
473 * FUNCTION: acpi_rs_get_list_length
474 *
475 * PARAMETERS: byte_stream_buffer - Pointer to the resource byte stream
476 * byte_stream_buffer_length - Size of byte_stream_buffer
Robert Moorebda663d2005-09-16 16:51:15 -0400477 * size_needed - Where the size needed is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 *
479 * RETURN: Status
480 *
Robert Moorebda663d2005-09-16 16:51:15 -0400481 * DESCRIPTION: Takes an external resource byte stream and calculates the size
482 * buffer needed to hold the corresponding internal resource
483 * descriptor linked list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *
485 ******************************************************************************/
486
487acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400488acpi_rs_get_list_length(u8 * byte_stream_buffer,
489 u32 byte_stream_buffer_length, acpi_size * size_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Robert Moorebda663d2005-09-16 16:51:15 -0400491 u8 *buffer;
492 ACPI_RESOURCE_SIZES *resource_info;
Len Brown4be44fc2005-08-05 00:44:28 -0400493 u32 buffer_size = 0;
494 u32 bytes_parsed = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400495 u8 resource_type;
Len Brown4be44fc2005-08-05 00:44:28 -0400496 u16 temp16;
Robert Moorebda663d2005-09-16 16:51:15 -0400497 u16 resource_length;
498 u16 header_length;
499 u32 extra_struct_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Len Brown4be44fc2005-08-05 00:44:28 -0400501 ACPI_FUNCTION_TRACE("rs_get_list_length");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 while (bytes_parsed < byte_stream_buffer_length) {
Robert Moorebda663d2005-09-16 16:51:15 -0400504 /* The next byte in the stream is the resource descriptor type */
Robert Moore44f6c012005-04-18 22:49:35 -0400505
Len Brown4be44fc2005-08-05 00:44:28 -0400506 resource_type = acpi_rs_get_resource_type(*byte_stream_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Robert Moorebda663d2005-09-16 16:51:15 -0400508 /* Get the base stream size and structure sizes for the descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Robert Moorebda663d2005-09-16 16:51:15 -0400510 resource_info = acpi_rs_get_resource_sizes(resource_type);
511 if (!resource_info) {
Len Brown4be44fc2005-08-05 00:44:28 -0400512 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Robert Moorebda663d2005-09-16 16:51:15 -0400515 /* Get the Length field from the input resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400516
Robert Moorebda663d2005-09-16 16:51:15 -0400517 resource_length =
518 acpi_rs_get_resource_length(byte_stream_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Robert Moorebda663d2005-09-16 16:51:15 -0400520 /* Augment the size for descriptors with optional fields */
Robert Moore44f6c012005-04-18 22:49:35 -0400521
Robert Moorebda663d2005-09-16 16:51:15 -0400522 extra_struct_bytes = 0;
523
524 if (!(resource_type & ACPI_RDESC_TYPE_LARGE)) {
525 /*
526 * Small resource descriptors
527 */
528 header_length = 1;
529 buffer = byte_stream_buffer + header_length;
530
531 switch (resource_type) {
532 case ACPI_RDESC_TYPE_IRQ_FORMAT:
533 /*
534 * IRQ Resource:
535 * Get the number of bits set in the IRQ word
536 */
537 ACPI_MOVE_16_TO_16(&temp16, buffer);
538
539 extra_struct_bytes =
540 (acpi_rs_count_set_bits(temp16) *
541 sizeof(u32));
542 break;
543
544 case ACPI_RDESC_TYPE_DMA_FORMAT:
545 /*
546 * DMA Resource:
547 * Get the number of bits set in the DMA channels byte
548 */
549 extra_struct_bytes =
550 (acpi_rs_count_set_bits((u16) * buffer) *
551 sizeof(u32));
552 break;
553
554 case ACPI_RDESC_TYPE_SMALL_VENDOR:
555 /*
556 * Vendor Specific Resource:
557 * Ensure a 32-bit boundary for the structure
558 */
559 extra_struct_bytes =
560 ACPI_ROUND_UP_to_32_bITS(resource_length);
561 break;
562
563 case ACPI_RDESC_TYPE_END_TAG:
564 /*
565 * End Tag:
566 * Terminate the loop now
567 */
568 byte_stream_buffer_length = bytes_parsed;
569 break;
570
571 default:
572 break;
573 }
574 } else {
575 /*
576 * Large resource descriptors
577 */
578 header_length = sizeof(struct asl_large_header);
579 buffer = byte_stream_buffer + header_length;
580
581 switch (resource_type) {
582 case ACPI_RDESC_TYPE_LARGE_VENDOR:
583 /*
584 * Vendor Defined Resource:
585 * Add vendor data and ensure a 32-bit boundary for the structure
586 */
587 extra_struct_bytes =
588 ACPI_ROUND_UP_to_32_bITS(resource_length);
589 break;
590
591 case ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE:
592 case ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE:
593 /*
594 * 32-Bit or 16-bit Address Resource:
595 * Add the size of any optional data (resource_source)
596 */
597 extra_struct_bytes =
598 acpi_rs_stream_option_length
599 (resource_length,
600 resource_info->minimum_stream_size);
601 break;
602
603 case ACPI_RDESC_TYPE_EXTENDED_XRUPT:
604 /*
605 * Extended IRQ:
606 * Point past the interrupt_vector_flags to get the
607 * interrupt_table_length.
608 */
609 buffer++;
610
611 /*
612 * Add 4 bytes for each additional interrupt. Note: at least one
613 * interrupt is required and is included in the minimum
614 * descriptor size
615 */
616 extra_struct_bytes =
617 ((*buffer - 1) * sizeof(u32));
618
619 /* Add the size of any optional data (resource_source) */
620
621 extra_struct_bytes +=
622 acpi_rs_stream_option_length(resource_length
623 -
624 extra_struct_bytes,
625 resource_info->
626 minimum_stream_size);
627 break;
628
629 case ACPI_RDESC_TYPE_QWORD_ADDRESS_SPACE:
630 /*
631 * 64-Bit Address Resource:
632 * Add the size of any optional data (resource_source)
633 * Ensure a 64-bit boundary for the structure
634 */
635 extra_struct_bytes =
636 ACPI_ROUND_UP_to_64_bITS
637 (acpi_rs_stream_option_length
638 (resource_length,
639 resource_info->minimum_stream_size));
640 break;
641
642 default:
643 break;
644 }
645 }
646
647 /* Update the required buffer size for the internal descriptor structs */
648
649 temp16 =
650 (u16) (resource_info->minimum_struct_size +
651 extra_struct_bytes);
652 buffer_size += (u32) ACPI_ALIGN_RESOURCE_SIZE(temp16);
653
654 /*
655 * Update byte count and point to the next resource within the stream
656 * using the size of the header plus the length contained in the header
657 */
658 temp16 = (u16) (header_length + resource_length);
659 bytes_parsed += temp16;
660 byte_stream_buffer += temp16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Robert Moore44f6c012005-04-18 22:49:35 -0400663 /* This is the data the caller needs */
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 *size_needed = buffer_size;
Len Brown4be44fc2005-08-05 00:44:28 -0400666 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/*******************************************************************************
670 *
671 * FUNCTION: acpi_rs_get_pci_routing_table_length
672 *
673 * PARAMETERS: package_object - Pointer to the package object
674 * buffer_size_needed - u32 pointer of the size buffer
675 * needed to properly return the
676 * parsed data
677 *
678 * RETURN: Status
679 *
680 * DESCRIPTION: Given a package representing a PCI routing table, this
681 * calculates the size of the corresponding linked list of
682 * descriptions.
683 *
684 ******************************************************************************/
685
686acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400687acpi_rs_get_pci_routing_table_length(union acpi_operand_object *package_object,
688 acpi_size * buffer_size_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Len Brown4be44fc2005-08-05 00:44:28 -0400690 u32 number_of_elements;
691 acpi_size temp_size_needed = 0;
692 union acpi_operand_object **top_object_list;
693 u32 index;
694 union acpi_operand_object *package_element;
695 union acpi_operand_object **sub_object_list;
696 u8 name_found;
697 u32 table_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Len Brown4be44fc2005-08-05 00:44:28 -0400699 ACPI_FUNCTION_TRACE("rs_get_pci_routing_table_length");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 number_of_elements = package_object->package.count;
702
703 /*
704 * Calculate the size of the return buffer.
705 * The base size is the number of elements * the sizes of the
706 * structures. Additional space for the strings is added below.
707 * The minus one is to subtract the size of the u8 Source[1]
708 * member because it is added below.
709 *
710 * But each PRT_ENTRY structure has a pointer to a string and
711 * the size of that string must be found.
712 */
713 top_object_list = package_object->package.elements;
714
715 for (index = 0; index < number_of_elements; index++) {
Robert Moore44f6c012005-04-18 22:49:35 -0400716 /* Dereference the sub-package */
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 package_element = *top_object_list;
719
720 /*
721 * The sub_object_list will now point to an array of the
722 * four IRQ elements: Address, Pin, Source and source_index
723 */
724 sub_object_list = package_element->package.elements;
725
Robert Moore44f6c012005-04-18 22:49:35 -0400726 /* Scan the irq_table_elements for the Source Name String */
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 name_found = FALSE;
729
Len Brown4be44fc2005-08-05 00:44:28 -0400730 for (table_index = 0; table_index < 4 && !name_found;
731 table_index++) {
Robert Moore44f6c012005-04-18 22:49:35 -0400732 if ((ACPI_TYPE_STRING ==
Len Brown4be44fc2005-08-05 00:44:28 -0400733 ACPI_GET_OBJECT_TYPE(*sub_object_list))
734 ||
735 ((ACPI_TYPE_LOCAL_REFERENCE ==
736 ACPI_GET_OBJECT_TYPE(*sub_object_list))
737 && ((*sub_object_list)->reference.opcode ==
738 AML_INT_NAMEPATH_OP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 name_found = TRUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400740 } else {
Robert Moore44f6c012005-04-18 22:49:35 -0400741 /* Look at the next element */
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 sub_object_list++;
744 }
745 }
746
Len Brown4be44fc2005-08-05 00:44:28 -0400747 temp_size_needed += (sizeof(struct acpi_pci_routing_table) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Robert Moore44f6c012005-04-18 22:49:35 -0400749 /* Was a String type found? */
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (name_found) {
Len Brown4be44fc2005-08-05 00:44:28 -0400752 if (ACPI_GET_OBJECT_TYPE(*sub_object_list) ==
753 ACPI_TYPE_STRING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /*
755 * The length String.Length field does not include the
756 * terminating NULL, add 1
757 */
Robert Moore44f6c012005-04-18 22:49:35 -0400758 temp_size_needed += ((acpi_size)
Len Brown4be44fc2005-08-05 00:44:28 -0400759 (*sub_object_list)->string.
760 length + 1);
761 } else {
762 temp_size_needed += acpi_ns_get_pathname_length((*sub_object_list)->reference.node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Len Brown4be44fc2005-08-05 00:44:28 -0400764 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * If no name was found, then this is a NULL, which is
767 * translated as a u32 zero.
768 */
Len Brown4be44fc2005-08-05 00:44:28 -0400769 temp_size_needed += sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
772 /* Round up the size since each element must be aligned */
773
Len Brown4be44fc2005-08-05 00:44:28 -0400774 temp_size_needed = ACPI_ROUND_UP_to_64_bITS(temp_size_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Robert Moore44f6c012005-04-18 22:49:35 -0400776 /* Point to the next union acpi_operand_object */
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 top_object_list++;
779 }
780
781 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400782 * Adding an extra element to the end of the list, essentially a
783 * NULL terminator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Len Brown4be44fc2005-08-05 00:44:28 -0400785 *buffer_size_needed =
786 temp_size_needed + sizeof(struct acpi_pci_routing_table);
787 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}