1 /*******************************************************************************
3 * Module Name: rscalc - Calculate stream and list lengths
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
64 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
65 * the size buffer needed to hold the linked list that conveys
68 ******************************************************************************/
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;
80 ACPI_FUNCTION_TRACE ("rs_get_byte_stream_length");
84 /* Init the variable that will hold the size to add to the total. */
88 switch (linked_list->id) {
92 * For an IRQ Resource, Byte 3, although optional, will always be
93 * created - it holds IRQ information.
101 * For this resource the size is static
106 case ACPI_RSTYPE_START_DPF:
108 * Start Dependent Functions Resource
109 * For a start_dependent_functions Resource, Byte 1, although
110 * optional, will always be created.
115 case ACPI_RSTYPE_END_DPF:
117 * End Dependent Functions Resource
118 * For this resource the size is static
126 * For this resource the size is static
131 case ACPI_RSTYPE_FIXED_IO:
133 * Fixed IO Port Resource
134 * For this resource the size is static
139 case ACPI_RSTYPE_VENDOR:
141 * Vendor Defined Resource
142 * For a Vendor Specific resource, if the Length is between 1 and 7
143 * it will be created as a Small Resource data type, otherwise it
144 * is a Large Resource data type.
146 if (linked_list->data.vendor_specific.length > 7) {
152 segment_size += linked_list->data.vendor_specific.length;
155 case ACPI_RSTYPE_END_TAG:
158 * For this resource the size is static
164 case ACPI_RSTYPE_MEM24:
166 * 24-Bit Memory Resource
167 * For this resource the size is static
172 case ACPI_RSTYPE_MEM32:
174 * 32-Bit Memory Range Resource
175 * For this resource the size is static
180 case ACPI_RSTYPE_FIXED_MEM32:
182 * 32-Bit Fixed Memory Resource
183 * For this resource the size is static
188 case ACPI_RSTYPE_ADDRESS16:
190 * 16-Bit Address Resource
191 * The base size of this byte stream is 16. If a Resource Source
192 * string is not NULL, add 1 for the Index + the length of the null
193 * terminated string Resource Source + 1 for the null.
197 if (linked_list->data.address16.resource_source.string_ptr) {
199 linked_list->data.address16.resource_source.string_length;
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 +
214 if (linked_list->data.address32.resource_source.string_ptr) {
216 linked_list->data.address32.resource_source.string_length;
221 case ACPI_RSTYPE_ADDRESS64:
223 * 64-Bit Address Resource
224 * The base size of this byte stream is 46. If a resource_source
225 * string is not NULL, add 1 for the Index + the length of the null
226 * terminated string Resource Source + 1 for the null.
230 if (linked_list->data.address64.resource_source.string_ptr) {
232 linked_list->data.address64.resource_source.string_length;
237 case ACPI_RSTYPE_EXT_IRQ:
239 * Extended IRQ Resource
240 * The base size of this byte stream is 9. This is for an Interrupt
241 * table length of 1. For each additional interrupt, add 4.
242 * If a Resource Source string is not NULL, add 1 for the
243 * Index + the length of the null terminated string
244 * Resource Source + 1 for the null.
246 segment_size = 9 + (((acpi_size)
247 linked_list->data.extended_irq.number_of_interrupts - 1) * 4);
249 if (linked_list->data.extended_irq.resource_source.string_ptr) {
251 linked_list->data.extended_irq.resource_source.string_length;
258 /* If we get here, everything is out of sync, exit with error */
260 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
262 } /* switch (linked_list->Id) */
264 /* Update the total */
266 byte_stream_size_needed += segment_size;
268 /* Point to the next object */
270 linked_list = ACPI_PTR_ADD (struct acpi_resource,
271 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
293 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
294 * the size buffer needed to hold the linked list that conveys
297 ******************************************************************************/
300 acpi_rs_get_list_length (
301 u8 *byte_stream_buffer,
302 u32 byte_stream_buffer_length,
303 acpi_size *size_needed)
306 u32 bytes_parsed = 0;
307 u8 number_of_interrupts = 0;
308 u8 number_of_channels = 0;
319 ACPI_FUNCTION_TRACE ("rs_get_list_length");
322 while (bytes_parsed < byte_stream_buffer_length) {
323 /* The next byte in the stream is the resource type */
325 resource_type = acpi_rs_get_resource_type (*byte_stream_buffer);
327 switch (resource_type) {
328 case ACPI_RDESC_TYPE_MEMORY_24:
330 * 24-Bit Memory Resource
334 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_mem24);
338 case ACPI_RDESC_TYPE_LARGE_VENDOR:
340 * Vendor Defined Resource
342 buffer = byte_stream_buffer;
345 ACPI_MOVE_16_TO_16 (&temp16, buffer);
346 bytes_consumed = temp16 + 3;
348 /* Ensure a 32-bit boundary for the structure */
350 temp16 = (u16) ACPI_ROUND_UP_to_32_bITS (temp16);
352 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_vendor) +
353 (temp16 * sizeof (u8));
357 case ACPI_RDESC_TYPE_MEMORY_32:
359 * 32-Bit Memory Range Resource
363 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_mem32);
367 case ACPI_RDESC_TYPE_FIXED_MEMORY_32:
369 * 32-Bit Fixed Memory Resource
373 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_fixed_mem32);
377 case ACPI_RDESC_TYPE_EXTENDED_ADDRESS_SPACE:
379 * 64-Bit Address Resource
381 buffer = byte_stream_buffer;
384 ACPI_MOVE_16_TO_16 (&temp16, buffer);
386 bytes_consumed = temp16 + 3;
387 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_address64);
391 case ACPI_RDESC_TYPE_QWORD_ADDRESS_SPACE:
393 * 64-Bit Address Resource
395 buffer = byte_stream_buffer;
398 ACPI_MOVE_16_TO_16 (&temp16, buffer);
400 bytes_consumed = temp16 + 3;
403 * Resource Source Index and Resource Source are optional elements.
404 * Check the length of the Bytestream. If it is greater than 43,
405 * that means that an Index exists and is followed by a null
406 * terminated string. Therefore, set the temp variable to the
407 * length minus the minimum byte stream length plus the byte for
408 * the Index to determine the size of the NULL terminated string.
411 temp8 = (u8) (temp16 - 44);
417 /* Ensure a 64-bit boundary for the structure */
419 temp8 = (u8) ACPI_ROUND_UP_to_64_bITS (temp8);
421 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_address64) +
422 (temp8 * sizeof (u8));
426 case ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE:
428 * 32-Bit Address Resource
430 buffer = byte_stream_buffer;
433 ACPI_MOVE_16_TO_16 (&temp16, buffer);
435 bytes_consumed = temp16 + 3;
438 * Resource Source Index and Resource Source are optional elements.
439 * Check the length of the Bytestream. If it is greater than 23,
440 * that means that an Index exists and is followed by a null
441 * terminated string. Therefore, set the temp variable to the
442 * length minus the minimum byte stream length plus the byte for
443 * the Index to determine the size of the NULL terminated string.
446 temp8 = (u8) (temp16 - 24);
452 /* Ensure a 32-bit boundary for the structure */
454 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS (temp8);
456 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_address32) +
457 (temp8 * sizeof (u8));
461 case ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE:
463 * 16-Bit Address Resource
465 buffer = byte_stream_buffer;
468 ACPI_MOVE_16_TO_16 (&temp16, buffer);
470 bytes_consumed = temp16 + 3;
473 * Resource Source Index and Resource Source are optional elements.
474 * Check the length of the Bytestream. If it is greater than 13,
475 * that means that an Index exists and is followed by a null
476 * terminated string. Therefore, set the temp variable to the
477 * length minus the minimum byte stream length plus the byte for
478 * the Index to determine the size of the NULL terminated string.
481 temp8 = (u8) (temp16 - 14);
487 /* Ensure a 32-bit boundary for the structure */
489 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS (temp8);
491 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_address16) +
492 (temp8 * sizeof (u8));
496 case ACPI_RDESC_TYPE_EXTENDED_XRUPT:
500 buffer = byte_stream_buffer;
503 ACPI_MOVE_16_TO_16 (&temp16, buffer);
505 bytes_consumed = temp16 + 3;
508 * Point past the length field and the Interrupt vector flags to
509 * save off the Interrupt table length to the Temp8 variable.
515 * To compensate for multiple interrupt numbers, add 4 bytes for
516 * each additional interrupts greater than 1
518 additional_bytes = (u8) ((temp8 - 1) * 4);
521 * Resource Source Index and Resource Source are optional elements.
522 * Check the length of the Bytestream. If it is greater than 9,
523 * that means that an Index exists and is followed by a null
524 * terminated string. Therefore, set the temp variable to the
525 * length minus the minimum byte stream length plus the byte for
526 * the Index to determine the size of the NULL terminated string.
528 if (9 + additional_bytes < temp16) {
529 temp8 = (u8) (temp16 - (9 + additional_bytes));
535 /* Ensure a 32-bit boundary for the structure */
537 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS (temp8);
539 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_ext_irq) +
540 (additional_bytes * sizeof (u8)) +
541 (temp8 * sizeof (u8));
545 case ACPI_RDESC_TYPE_IRQ_FORMAT:
548 * Determine if it there are two or three trailing bytes
550 buffer = byte_stream_buffer;
560 /* Point past the descriptor */
564 /* Look at the number of bits set */
566 ACPI_MOVE_16_TO_16 (&temp16, buffer);
568 for (index = 0; index < 16; index++) {
570 ++number_of_interrupts;
576 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_io) +
577 (number_of_interrupts * sizeof (u32));
581 case ACPI_RDESC_TYPE_DMA_FORMAT:
585 buffer = byte_stream_buffer;
588 /* Point past the descriptor */
592 /* Look at the number of bits set */
596 for(index = 0; index < 8; index++) {
598 ++number_of_channels;
604 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_dma) +
605 (number_of_channels * sizeof (u32));
609 case ACPI_RDESC_TYPE_START_DEPENDENT:
611 * Start Dependent Functions Resource
612 * Determine if it there are two or three trailing bytes
614 buffer = byte_stream_buffer;
624 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_start_dpf);
628 case ACPI_RDESC_TYPE_END_DEPENDENT:
630 * End Dependent Functions Resource
633 structure_size = ACPI_RESOURCE_LENGTH;
637 case ACPI_RDESC_TYPE_IO_PORT:
642 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_io);
646 case ACPI_RDESC_TYPE_FIXED_IO_PORT:
648 * Fixed IO Port Resource
651 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_fixed_io);
655 case ACPI_RDESC_TYPE_SMALL_VENDOR:
657 * Vendor Specific Resource
659 buffer = byte_stream_buffer;
662 temp8 = (u8) (temp8 & 0x7);
663 bytes_consumed = temp8 + 1;
665 /* Ensure a 32-bit boundary for the structure */
667 temp8 = (u8) ACPI_ROUND_UP_to_32_bITS (temp8);
668 structure_size = ACPI_SIZEOF_RESOURCE (struct acpi_resource_vendor) +
669 (temp8 * sizeof (u8));
673 case ACPI_RDESC_TYPE_END_TAG:
678 structure_size = ACPI_RESOURCE_LENGTH;
679 byte_stream_buffer_length = bytes_parsed;
685 * If we get here, everything is out of sync,
688 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
691 /* Update the return value and counter */
693 buffer_size += (u32) ACPI_ALIGN_RESOURCE_SIZE (structure_size);
694 bytes_parsed += bytes_consumed;
696 /* Set the byte stream to point to the next resource */
698 byte_stream_buffer += bytes_consumed;
701 /* This is the data the caller needs */
703 *size_needed = buffer_size;
704 return_ACPI_STATUS (AE_OK);
708 /*******************************************************************************
710 * FUNCTION: acpi_rs_get_pci_routing_table_length
712 * PARAMETERS: package_object - Pointer to the package object
713 * buffer_size_needed - u32 pointer of the size buffer
714 * needed to properly return the
719 * DESCRIPTION: Given a package representing a PCI routing table, this
720 * calculates the size of the corresponding linked list of
723 ******************************************************************************/
726 acpi_rs_get_pci_routing_table_length (
727 union acpi_operand_object *package_object,
728 acpi_size *buffer_size_needed)
730 u32 number_of_elements;
731 acpi_size temp_size_needed = 0;
732 union acpi_operand_object **top_object_list;
734 union acpi_operand_object *package_element;
735 union acpi_operand_object **sub_object_list;
740 ACPI_FUNCTION_TRACE ("rs_get_pci_routing_table_length");
743 number_of_elements = package_object->package.count;
746 * Calculate the size of the return buffer.
747 * The base size is the number of elements * the sizes of the
748 * structures. Additional space for the strings is added below.
749 * The minus one is to subtract the size of the u8 Source[1]
750 * member because it is added below.
752 * But each PRT_ENTRY structure has a pointer to a string and
753 * the size of that string must be found.
755 top_object_list = package_object->package.elements;
757 for (index = 0; index < number_of_elements; index++) {
758 /* Dereference the sub-package */
760 package_element = *top_object_list;
763 * The sub_object_list will now point to an array of the
764 * four IRQ elements: Address, Pin, Source and source_index
766 sub_object_list = package_element->package.elements;
768 /* Scan the irq_table_elements for the Source Name String */
772 for (table_index = 0; table_index < 4 && !name_found; table_index++) {
773 if ((ACPI_TYPE_STRING ==
774 ACPI_GET_OBJECT_TYPE (*sub_object_list)) ||
776 ((ACPI_TYPE_LOCAL_REFERENCE ==
777 ACPI_GET_OBJECT_TYPE (*sub_object_list)) &&
779 ((*sub_object_list)->reference.opcode ==
780 AML_INT_NAMEPATH_OP))) {
784 /* Look at the next element */
790 temp_size_needed += (sizeof (struct acpi_pci_routing_table) - 4);
792 /* Was a String type found? */
795 if (ACPI_GET_OBJECT_TYPE (*sub_object_list) == ACPI_TYPE_STRING) {
797 * The length String.Length field does not include the
798 * terminating NULL, add 1
800 temp_size_needed += ((acpi_size)
801 (*sub_object_list)->string.length + 1);
804 temp_size_needed += acpi_ns_get_pathname_length (
805 (*sub_object_list)->reference.node);
810 * If no name was found, then this is a NULL, which is
811 * translated as a u32 zero.
813 temp_size_needed += sizeof (u32);
816 /* Round up the size since each element must be aligned */
818 temp_size_needed = ACPI_ROUND_UP_to_64_bITS (temp_size_needed);
820 /* Point to the next union acpi_operand_object */
826 * Adding an extra element to the end of the list, essentially a
829 *buffer_size_needed = temp_size_needed + sizeof (struct acpi_pci_routing_table);
830 return_ACPI_STATUS (AE_OK);