Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / resources / rscalc.c
blob8a5f0a52371d49b13f9f55936e30c6536b134df7
1 /*******************************************************************************
3 * Module Name: rscalc - Calculate stream and list lengths
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * All rights reserved.
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.
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.
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.
45 #include <acpi/acpi.h>
46 #include <acpi/acresrc.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acnamesp.h>
50 #define _COMPONENT ACPI_RESOURCES
51 ACPI_MODULE_NAME ("rscalc")
54 /*******************************************************************************
56 * FUNCTION: acpi_rs_get_byte_stream_length
58 * PARAMETERS: linked_list - Pointer to the resource linked list
59 * size_needed - u32 pointer of the size buffer needed
60 * to properly return the parsed data
62 * RETURN: Status
64 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
65 * the size buffer needed to hold the linked list that conveys
66 * the resource data.
68 ******************************************************************************/
70 acpi_status
71 acpi_rs_get_byte_stream_length (
72 struct acpi_resource *linked_list,
73 acpi_size *size_needed)
75 acpi_size byte_stream_size_needed = 0;
76 acpi_size segment_size;
77 u8 done = FALSE;
80 ACPI_FUNCTION_TRACE ("rs_get_byte_stream_length");
83 while (!done) {
85 * Init the variable that will hold the size to add to the total.
87 segment_size = 0;
89 switch (linked_list->id) {
90 case ACPI_RSTYPE_IRQ:
92 * IRQ Resource
93 * For an IRQ Resource, Byte 3, although optional, will always be
94 * created - it holds IRQ information.
96 segment_size = 4;
97 break;
99 case ACPI_RSTYPE_DMA:
101 * DMA Resource
102 * For this resource the size is static
104 segment_size = 3;
105 break;
107 case ACPI_RSTYPE_START_DPF:
109 * Start Dependent Functions Resource
110 * For a start_dependent_functions Resource, Byte 1, although
111 * optional, will always be created.
113 segment_size = 2;
114 break;
116 case ACPI_RSTYPE_END_DPF:
118 * End Dependent Functions Resource
119 * For this resource the size is static
121 segment_size = 1;
122 break;
124 case ACPI_RSTYPE_IO:
126 * IO Port Resource
127 * For this resource the size is static
129 segment_size = 8;
130 break;
132 case ACPI_RSTYPE_FIXED_IO:
134 * Fixed IO Port Resource
135 * For this resource the size is static
137 segment_size = 4;
138 break;
140 case ACPI_RSTYPE_VENDOR:
142 * Vendor Defined Resource
143 * For a Vendor Specific resource, if the Length is between 1 and 7
144 * it will be created as a Small Resource data type, otherwise it
145 * is a Large Resource data type.
147 if (linked_list->data.vendor_specific.length > 7) {
148 segment_size = 3;
150 else {
151 segment_size = 1;
153 segment_size += linked_list->data.vendor_specific.length;
154 break;
156 case ACPI_RSTYPE_END_TAG:
158 * End Tag
159 * For this resource the size is static
161 segment_size = 2;
162 done = TRUE;
163 break;
165 case ACPI_RSTYPE_MEM24:
167 * 24-Bit Memory Resource
168 * For this resource the size is static
170 segment_size = 12;
171 break;
173 case ACPI_RSTYPE_MEM32:
175 * 32-Bit Memory Range Resource
176 * For this resource the size is static
178 segment_size = 20;
179 break;
181 case ACPI_RSTYPE_FIXED_MEM32:
183 * 32-Bit Fixed Memory Resource
184 * For this resource the size is static
186 segment_size = 12;
187 break;
189 case ACPI_RSTYPE_ADDRESS16:
191 * 16-Bit Address Resource
192 * The base size of this byte stream is 16. If a Resource Source
193 * string is not NULL, add 1 for the Index + the length of the null
194 * terminated string Resource Source + 1 for the null.
196 segment_size = 16;
198 if (linked_list->data.address16.resource_source.string_ptr) {
199 segment_size += linked_list->data.address16.resource_source.string_length;
200 segment_size++;
202 break;
204 case ACPI_RSTYPE_ADDRESS32:
206 * 32-Bit Address Resource
207 * The base size of this byte stream is 26. If a Resource
208 * Source string is not NULL, add 1 for the Index + the
209 * length of the null terminated string Resource Source +
210 * 1 for the null.
212 segment_size = 26;
214 if (linked_list->data.address32.resource_source.string_ptr) {
215 segment_size += linked_list->data.address32.resource_source.string_length;
216 segment_size++;
218 break;
220 case ACPI_RSTYPE_ADDRESS64:
222 * 64-Bit Address Resource
223 * The base size of this byte stream is 46. If a resource_source
224 * string is not NULL, add 1 for the Index + the length of the null
225 * terminated string Resource Source + 1 for the null.
227 segment_size = 46;
229 if (linked_list->data.address64.resource_source.string_ptr) {
230 segment_size += linked_list->data.address64.resource_source.string_length;
231 segment_size++;
233 break;
235 case ACPI_RSTYPE_EXT_IRQ:
237 * Extended IRQ Resource
238 * The base size of this byte stream is 9. This is for an Interrupt
239 * table length of 1. For each additional interrupt, add 4.
240 * If a Resource Source string is not NULL, add 1 for the
241 * Index + the length of the null terminated string
242 * Resource Source + 1 for the null.
244 segment_size = 9 +
245 (((acpi_size) linked_list->data.extended_irq.number_of_interrupts - 1) * 4);
247 if (linked_list->data.extended_irq.resource_source.string_ptr) {
248 segment_size += linked_list->data.extended_irq.resource_source.string_length;
249 segment_size++;
251 break;
253 default:
255 * If we get here, everything is out of sync, exit with error
257 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
259 } /* switch (linked_list->Id) */
262 * Update the total
264 byte_stream_size_needed += segment_size;
267 * Point to the next object
269 linked_list = ACPI_PTR_ADD (struct acpi_resource,
270 linked_list, linked_list->length);
274 * This is the data the caller needs
276 *size_needed = byte_stream_size_needed;
277 return_ACPI_STATUS (AE_OK);
281 /*******************************************************************************
283 * FUNCTION: acpi_rs_get_list_length
285 * PARAMETERS: byte_stream_buffer - Pointer to the resource byte stream
286 * byte_stream_buffer_length - Size of byte_stream_buffer
287 * size_needed - u32 pointer of the size buffer
288 * needed to properly return the
289 * parsed data
291 * RETURN: Status
293 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
294 * the size buffer needed to hold the linked list that conveys
295 * the resource data.
297 ******************************************************************************/
299 acpi_status
300 acpi_rs_get_list_length (
301 u8 *byte_stream_buffer,
302 u32 byte_stream_buffer_length,
303 acpi_size *size_needed)
305 u32 buffer_size = 0;
306 u32 bytes_parsed = 0;
307 u8 number_of_interrupts = 0;
308 u8 number_of_channels = 0;
309 u8 resource_type;
310 u32 structure_size;
311 u32 bytes_consumed;
312 u8 *buffer;
313 u8 temp8;
314 u16 temp16;
315 u8 index;
316 u8 additional_bytes;
319 ACPI_FUNCTION_TRACE ("rs_get_list_length");
322 while (bytes_parsed < byte_stream_buffer_length) {
324 * The next byte in the stream is the resource type
326 resource_type = acpi_rs_get_resource_type (*byte_stream_buffer);
328 switch (resource_type) {
329 case ACPI_RDESC_TYPE_MEMORY_24:
331 * 24-Bit Memory Resource
333 bytes_consumed = 12;
335 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_mem24);
336 break;
339 case ACPI_RDESC_TYPE_LARGE_VENDOR:
341 * Vendor Defined Resource
343 buffer = byte_stream_buffer;
344 ++buffer;
346 ACPI_MOVE_16_TO_16 (&temp16, buffer);
347 bytes_consumed = temp16 + 3;
350 * Ensure a 32-bit boundary for the structure
352 temp16 = (u16) ACPI_ROUND_UP_to_32_bITS (temp16);
354 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_vendor) +
355 (temp16 * sizeof (u8));
356 break;
359 case ACPI_RDESC_TYPE_MEMORY_32:
361 * 32-Bit Memory Range Resource
363 bytes_consumed = 20;
365 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_mem32);
366 break;
369 case ACPI_RDESC_TYPE_FIXED_MEMORY_32:
371 * 32-Bit Fixed Memory Resource
373 bytes_consumed = 12;
375 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_fixed_mem32);
376 break;
379 case ACPI_RDESC_TYPE_EXTENDED_ADDRESS_SPACE:
381 * 64-Bit Address Resource
383 buffer = byte_stream_buffer;
385 ++buffer;
386 ACPI_MOVE_16_TO_16 (&temp16, buffer);
388 bytes_consumed = temp16 + 3;
389 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_address64);
390 break;
393 case ACPI_RDESC_TYPE_QWORD_ADDRESS_SPACE:
395 * 64-Bit Address Resource
397 buffer = byte_stream_buffer;
399 ++buffer;
400 ACPI_MOVE_16_TO_16 (&temp16, buffer);
402 bytes_consumed = temp16 + 3;
405 * Resource Source Index and Resource Source are optional elements.
406 * Check the length of the Bytestream. If it is greater than 43,
407 * that means that an Index exists and is followed by a null
408 * terminated string. Therefore, set the temp variable to the
409 * length minus the minimum byte stream length plus the byte for
410 * the Index to determine the size of the NULL terminated string.
412 if (43 < temp16) {
413 temp8 = (u8) (temp16 - 44);
415 else {
416 temp8 = 0;
420 * Ensure a 64-bit boundary for the structure
422 temp8 = (u8) ACPI_ROUND_UP_to_64_bITS (temp8);
424 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_address64) +
425 (temp8 * sizeof (u8));
426 break;
429 case ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE:
431 * 32-Bit Address Resource
433 buffer = byte_stream_buffer;
435 ++buffer;
436 ACPI_MOVE_16_TO_16 (&temp16, buffer);
438 bytes_consumed = temp16 + 3;
441 * Resource Source Index and Resource Source are optional elements.
442 * Check the length of the Bytestream. If it is greater than 23,
443 * that means that an Index exists and is followed by a null
444 * terminated string. Therefore, set the temp variable to the
445 * length minus the minimum byte stream length plus the byte for
446 * the Index to determine the size of the NULL terminated string.
448 if (23 < temp16) {
449 temp8 = (u8) (temp16 - 24);
451 else {
452 temp8 = 0;
456 * Ensure a 32-bit boundary for the structure
458 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS (temp8);
460 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_address32) +
461 (temp8 * sizeof (u8));
462 break;
465 case ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE:
467 * 16-Bit Address Resource
469 buffer = byte_stream_buffer;
471 ++buffer;
472 ACPI_MOVE_16_TO_16 (&temp16, buffer);
474 bytes_consumed = temp16 + 3;
477 * Resource Source Index and Resource Source are optional elements.
478 * Check the length of the Bytestream. If it is greater than 13,
479 * that means that an Index exists and is followed by a null
480 * terminated string. Therefore, set the temp variable to the
481 * length minus the minimum byte stream length plus the byte for
482 * the Index to determine the size of the NULL terminated string.
484 if (13 < temp16) {
485 temp8 = (u8) (temp16 - 14);
487 else {
488 temp8 = 0;
492 * Ensure a 32-bit boundary for the structure
494 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS (temp8);
496 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_address16) +
497 (temp8 * sizeof (u8));
498 break;
501 case ACPI_RDESC_TYPE_EXTENDED_XRUPT:
503 * Extended IRQ
505 buffer = byte_stream_buffer;
507 ++buffer;
508 ACPI_MOVE_16_TO_16 (&temp16, buffer);
510 bytes_consumed = temp16 + 3;
513 * Point past the length field and the Interrupt vector flags to
514 * save off the Interrupt table length to the Temp8 variable.
516 buffer += 3;
517 temp8 = *buffer;
520 * To compensate for multiple interrupt numbers, add 4 bytes for
521 * each additional interrupts greater than 1
523 additional_bytes = (u8) ((temp8 - 1) * 4);
526 * Resource Source Index and Resource Source are optional elements.
527 * Check the length of the Bytestream. If it is greater than 9,
528 * that means that an Index exists and is followed by a null
529 * terminated string. Therefore, set the temp variable to the
530 * length minus the minimum byte stream length plus the byte for
531 * the Index to determine the size of the NULL terminated string.
533 if (9 + additional_bytes < temp16) {
534 temp8 = (u8) (temp16 - (9 + additional_bytes));
536 else {
537 temp8 = 0;
541 * Ensure a 32-bit boundary for the structure
543 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS (temp8);
545 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_ext_irq) +
546 (additional_bytes * sizeof (u8)) +
547 (temp8 * sizeof (u8));
548 break;
551 case ACPI_RDESC_TYPE_IRQ_FORMAT:
553 * IRQ Resource.
554 * Determine if it there are two or three trailing bytes
556 buffer = byte_stream_buffer;
557 temp8 = *buffer;
559 if(temp8 & 0x01) {
560 bytes_consumed = 4;
562 else {
563 bytes_consumed = 3;
566 /* Point past the descriptor */
568 ++buffer;
571 * Look at the number of bits set
573 ACPI_MOVE_16_TO_16 (&temp16, buffer);
575 for (index = 0; index < 16; index++) {
576 if (temp16 & 0x1) {
577 ++number_of_interrupts;
580 temp16 >>= 1;
583 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_io) +
584 (number_of_interrupts * sizeof (u32));
585 break;
588 case ACPI_RDESC_TYPE_DMA_FORMAT:
590 * DMA Resource
592 buffer = byte_stream_buffer;
593 bytes_consumed = 3;
595 /* Point past the descriptor */
597 ++buffer;
600 * Look at the number of bits set
602 temp8 = *buffer;
604 for(index = 0; index < 8; index++) {
605 if(temp8 & 0x1) {
606 ++number_of_channels;
609 temp8 >>= 1;
612 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_dma) +
613 (number_of_channels * sizeof (u32));
614 break;
617 case ACPI_RDESC_TYPE_START_DEPENDENT:
619 * Start Dependent Functions Resource
620 * Determine if it there are two or three trailing bytes
622 buffer = byte_stream_buffer;
623 temp8 = *buffer;
625 if(temp8 & 0x01) {
626 bytes_consumed = 2;
628 else {
629 bytes_consumed = 1;
632 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_start_dpf);
633 break;
636 case ACPI_RDESC_TYPE_END_DEPENDENT:
638 * End Dependent Functions Resource
640 bytes_consumed = 1;
641 structure_size = ACPI_RESOURCE_LENGTH;
642 break;
645 case ACPI_RDESC_TYPE_IO_PORT:
647 * IO Port Resource
649 bytes_consumed = 8;
650 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_io);
651 break;
654 case ACPI_RDESC_TYPE_FIXED_IO_PORT:
656 * Fixed IO Port Resource
658 bytes_consumed = 4;
659 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_fixed_io);
660 break;
663 case ACPI_RDESC_TYPE_SMALL_VENDOR:
665 * Vendor Specific Resource
667 buffer = byte_stream_buffer;
669 temp8 = *buffer;
670 temp8 = (u8) (temp8 & 0x7);
671 bytes_consumed = temp8 + 1;
674 * Ensure a 32-bit boundary for the structure
676 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS (temp8);
677 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_vendor) +
678 (temp8 * sizeof (u8));
679 break;
682 case ACPI_RDESC_TYPE_END_TAG:
684 * End Tag
686 bytes_consumed = 2;
687 structure_size = ACPI_RESOURCE_LENGTH;
688 byte_stream_buffer_length = bytes_parsed;
689 break;
692 default:
694 * If we get here, everything is out of sync,
695 * exit with an error
697 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
701 * Update the return value and counter
703 buffer_size += (u32) ACPI_ALIGN_RESOURCE_SIZE (structure_size);
704 bytes_parsed += bytes_consumed;
707 * Set the byte stream to point to the next resource
709 byte_stream_buffer += bytes_consumed;
713 * This is the data the caller needs
715 *size_needed = buffer_size;
716 return_ACPI_STATUS (AE_OK);
720 /*******************************************************************************
722 * FUNCTION: acpi_rs_get_pci_routing_table_length
724 * PARAMETERS: package_object - Pointer to the package object
725 * buffer_size_needed - u32 pointer of the size buffer
726 * needed to properly return the
727 * parsed data
729 * RETURN: Status
731 * DESCRIPTION: Given a package representing a PCI routing table, this
732 * calculates the size of the corresponding linked list of
733 * descriptions.
735 ******************************************************************************/
737 acpi_status
738 acpi_rs_get_pci_routing_table_length (
739 union acpi_operand_object *package_object,
740 acpi_size *buffer_size_needed)
742 u32 number_of_elements;
743 acpi_size temp_size_needed = 0;
744 union acpi_operand_object **top_object_list;
745 u32 index;
746 union acpi_operand_object *package_element;
747 union acpi_operand_object **sub_object_list;
748 u8 name_found;
749 u32 table_index;
752 ACPI_FUNCTION_TRACE ("rs_get_pci_routing_table_length");
755 number_of_elements = package_object->package.count;
758 * Calculate the size of the return buffer.
759 * The base size is the number of elements * the sizes of the
760 * structures. Additional space for the strings is added below.
761 * The minus one is to subtract the size of the u8 Source[1]
762 * member because it is added below.
764 * But each PRT_ENTRY structure has a pointer to a string and
765 * the size of that string must be found.
767 top_object_list = package_object->package.elements;
769 for (index = 0; index < number_of_elements; index++) {
771 * Dereference the sub-package
773 package_element = *top_object_list;
776 * The sub_object_list will now point to an array of the
777 * four IRQ elements: Address, Pin, Source and source_index
779 sub_object_list = package_element->package.elements;
782 * Scan the irq_table_elements for the Source Name String
784 name_found = FALSE;
786 for (table_index = 0; table_index < 4 && !name_found; table_index++) {
787 if ((ACPI_TYPE_STRING == ACPI_GET_OBJECT_TYPE (*sub_object_list)) ||
788 ((ACPI_TYPE_LOCAL_REFERENCE == ACPI_GET_OBJECT_TYPE (*sub_object_list)) &&
789 ((*sub_object_list)->reference.opcode == AML_INT_NAMEPATH_OP))) {
790 name_found = TRUE;
792 else {
794 * Look at the next element
796 sub_object_list++;
800 temp_size_needed += (sizeof (struct acpi_pci_routing_table) - 4);
803 * Was a String type found?
805 if (name_found) {
806 if (ACPI_GET_OBJECT_TYPE (*sub_object_list) == ACPI_TYPE_STRING) {
808 * The length String.Length field does not include the
809 * terminating NULL, add 1
811 temp_size_needed += ((acpi_size) (*sub_object_list)->string.length + 1);
813 else {
814 temp_size_needed += acpi_ns_get_pathname_length (
815 (*sub_object_list)->reference.node);
818 else {
820 * If no name was found, then this is a NULL, which is
821 * translated as a u32 zero.
823 temp_size_needed += sizeof (u32);
826 /* Round up the size since each element must be aligned */
828 temp_size_needed = ACPI_ROUND_UP_to_64_bITS (temp_size_needed);
831 * Point to the next union acpi_operand_object
833 top_object_list++;
837 * Adding an extra element to the end of the list, essentially a NULL terminator
839 *buffer_size_needed = temp_size_needed + sizeof (struct acpi_pci_routing_table);
840 return_ACPI_STATUS (AE_OK);