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.
44 #include <acpi/acpi.h>
45 #include <acpi/acresrc.h>
46 #include <acpi/amlcode.h>
47 #include <acpi/acnamesp.h>
49 #define _COMPONENT ACPI_RESOURCES
50 ACPI_MODULE_NAME("rscalc")
52 /*******************************************************************************
54 * FUNCTION: acpi_rs_get_byte_stream_length
56 * PARAMETERS: linked_list - Pointer to the resource linked list
57 * size_needed - u32 pointer of the size buffer needed
58 * to properly return the parsed data
62 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
63 * the size buffer needed to hold the linked list that conveys
66 ******************************************************************************/
68 acpi_rs_get_byte_stream_length(struct acpi_resource
*linked_list
,
69 acpi_size
* size_needed
)
71 acpi_size byte_stream_size_needed
= 0;
72 acpi_size segment_size
;
75 ACPI_FUNCTION_TRACE("rs_get_byte_stream_length");
78 /* Init the variable that will hold the size to add to the total. */
82 switch (linked_list
->id
) {
86 * For an IRQ Resource, Byte 3, although optional, will always be
87 * created - it holds IRQ information.
95 * For this resource the size is static
100 case ACPI_RSTYPE_START_DPF
:
102 * Start Dependent Functions Resource
103 * For a start_dependent_functions Resource, Byte 1, although
104 * optional, will always be created.
109 case ACPI_RSTYPE_END_DPF
:
111 * End Dependent Functions Resource
112 * For this resource the size is static
120 * For this resource the size is static
125 case ACPI_RSTYPE_FIXED_IO
:
127 * Fixed IO Port Resource
128 * For this resource the size is static
133 case ACPI_RSTYPE_VENDOR
:
135 * Vendor Defined Resource
136 * For a Vendor Specific resource, if the Length is between 1 and 7
137 * it will be created as a Small Resource data type, otherwise it
138 * is a Large Resource data type.
140 if (linked_list
->data
.vendor_specific
.length
> 7) {
146 linked_list
->data
.vendor_specific
.length
;
149 case ACPI_RSTYPE_END_TAG
:
152 * For this resource the size is static
158 case ACPI_RSTYPE_MEM24
:
160 * 24-Bit Memory Resource
161 * For this resource the size is static
166 case ACPI_RSTYPE_MEM32
:
168 * 32-Bit Memory Range Resource
169 * For this resource the size is static
174 case ACPI_RSTYPE_FIXED_MEM32
:
176 * 32-Bit Fixed Memory Resource
177 * For this resource the size is static
182 case ACPI_RSTYPE_ADDRESS16
:
184 * 16-Bit Address Resource
185 * The base size of this byte stream is 16. If a Resource Source
186 * string is not NULL, add 1 for the Index + the length of the null
187 * terminated string Resource Source + 1 for the null.
191 if (linked_list
->data
.address16
.resource_source
.
194 linked_list
->data
.address16
.resource_source
.
200 case ACPI_RSTYPE_ADDRESS32
:
202 * 32-Bit Address Resource
203 * The base size of this byte stream is 26. If a Resource
204 * Source string is not NULL, add 1 for the Index + the
205 * length of the null terminated string Resource Source +
210 if (linked_list
->data
.address32
.resource_source
.
213 linked_list
->data
.address32
.resource_source
.
219 case ACPI_RSTYPE_ADDRESS64
:
221 * 64-Bit Address Resource
222 * The base size of this byte stream is 46. If a resource_source
223 * string is not NULL, add 1 for the Index + the length of the null
224 * terminated string Resource Source + 1 for the null.
228 if (linked_list
->data
.address64
.resource_source
.
231 linked_list
->data
.address64
.resource_source
.
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
.
248 number_of_interrupts
- 1) * 4);
250 if (linked_list
->data
.extended_irq
.resource_source
.
253 linked_list
->data
.extended_irq
.
254 resource_source
.string_length
;
261 /* If we get here, everything is out of sync, exit with error */
263 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE
);
265 } /* switch (linked_list->Id) */
267 /* Update the total */
269 byte_stream_size_needed
+= segment_size
;
271 /* Point to the next object */
273 linked_list
= ACPI_PTR_ADD(struct acpi_resource
,
274 linked_list
, linked_list
->length
);
277 /* This is the data the caller needs */
279 *size_needed
= byte_stream_size_needed
;
280 return_ACPI_STATUS(AE_OK
);
283 /*******************************************************************************
285 * FUNCTION: acpi_rs_get_list_length
287 * PARAMETERS: byte_stream_buffer - Pointer to the resource byte stream
288 * byte_stream_buffer_length - Size of byte_stream_buffer
289 * size_needed - u32 pointer of the size buffer
290 * needed to properly return the
295 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
296 * the size buffer needed to hold the linked list that conveys
299 ******************************************************************************/
302 acpi_rs_get_list_length(u8
* byte_stream_buffer
,
303 u32 byte_stream_buffer_length
, acpi_size
* size_needed
)
306 u32 bytes_parsed
= 0;
307 u8 number_of_interrupts
= 0;
308 u8 number_of_channels
= 0;
318 ACPI_FUNCTION_TRACE("rs_get_list_length");
320 while (bytes_parsed
< byte_stream_buffer_length
) {
321 /* The next byte in the stream is the resource type */
323 resource_type
= acpi_rs_get_resource_type(*byte_stream_buffer
);
325 switch (resource_type
) {
326 case ACPI_RDESC_TYPE_MEMORY_24
:
328 * 24-Bit Memory Resource
333 ACPI_SIZEOF_RESOURCE(struct acpi_resource_mem24
);
336 case ACPI_RDESC_TYPE_LARGE_VENDOR
:
338 * Vendor Defined Resource
340 buffer
= byte_stream_buffer
;
343 ACPI_MOVE_16_TO_16(&temp16
, buffer
);
344 bytes_consumed
= temp16
+ 3;
346 /* Ensure a 32-bit boundary for the structure */
348 temp16
= (u16
) ACPI_ROUND_UP_to_32_bITS(temp16
);
351 ACPI_SIZEOF_RESOURCE(struct acpi_resource_vendor
) +
352 (temp16
* sizeof(u8
));
355 case ACPI_RDESC_TYPE_MEMORY_32
:
357 * 32-Bit Memory Range Resource
362 ACPI_SIZEOF_RESOURCE(struct acpi_resource_mem32
);
365 case ACPI_RDESC_TYPE_FIXED_MEMORY_32
:
367 * 32-Bit Fixed Memory Resource
372 ACPI_SIZEOF_RESOURCE(struct
373 acpi_resource_fixed_mem32
);
376 case ACPI_RDESC_TYPE_EXTENDED_ADDRESS_SPACE
:
378 * 64-Bit Address Resource
380 buffer
= byte_stream_buffer
;
383 ACPI_MOVE_16_TO_16(&temp16
, buffer
);
385 bytes_consumed
= temp16
+ 3;
387 ACPI_SIZEOF_RESOURCE(struct
388 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);
416 /* Ensure a 64-bit boundary for the structure */
418 temp8
= (u8
) ACPI_ROUND_UP_to_64_bITS(temp8
);
421 ACPI_SIZEOF_RESOURCE(struct acpi_resource_address64
)
422 + (temp8
* sizeof(u8
));
425 case ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE
:
427 * 32-Bit Address Resource
429 buffer
= byte_stream_buffer
;
432 ACPI_MOVE_16_TO_16(&temp16
, buffer
);
434 bytes_consumed
= temp16
+ 3;
437 * Resource Source Index and Resource Source are optional elements.
438 * Check the length of the Bytestream. If it is greater than 23,
439 * that means that an Index exists and is followed by a null
440 * terminated string. Therefore, set the temp variable to the
441 * length minus the minimum byte stream length plus the byte for
442 * the Index to determine the size of the NULL terminated string.
445 temp8
= (u8
) (temp16
- 24);
450 /* Ensure a 32-bit boundary for the structure */
452 temp8
= (u8
) ACPI_ROUND_UP_to_32_bITS(temp8
);
455 ACPI_SIZEOF_RESOURCE(struct acpi_resource_address32
)
456 + (temp8
* sizeof(u8
));
459 case ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE
:
461 * 16-Bit Address Resource
463 buffer
= byte_stream_buffer
;
466 ACPI_MOVE_16_TO_16(&temp16
, buffer
);
468 bytes_consumed
= temp16
+ 3;
471 * Resource Source Index and Resource Source are optional elements.
472 * Check the length of the Bytestream. If it is greater than 13,
473 * that means that an Index exists and is followed by a null
474 * terminated string. Therefore, set the temp variable to the
475 * length minus the minimum byte stream length plus the byte for
476 * the Index to determine the size of the NULL terminated string.
479 temp8
= (u8
) (temp16
- 14);
484 /* Ensure a 32-bit boundary for the structure */
486 temp8
= (u8
) ACPI_ROUND_UP_to_32_bITS(temp8
);
489 ACPI_SIZEOF_RESOURCE(struct acpi_resource_address16
)
490 + (temp8
* sizeof(u8
));
493 case ACPI_RDESC_TYPE_EXTENDED_XRUPT
:
497 buffer
= byte_stream_buffer
;
500 ACPI_MOVE_16_TO_16(&temp16
, buffer
);
502 bytes_consumed
= temp16
+ 3;
505 * Point past the length field and the Interrupt vector flags to
506 * save off the Interrupt table length to the Temp8 variable.
512 * To compensate for multiple interrupt numbers, add 4 bytes for
513 * each additional interrupts greater than 1
515 additional_bytes
= (u8
) ((temp8
- 1) * 4);
518 * Resource Source Index and Resource Source are optional elements.
519 * Check the length of the Bytestream. If it is greater than 9,
520 * that means that an Index exists and is followed by a null
521 * terminated string. Therefore, set the temp variable to the
522 * length minus the minimum byte stream length plus the byte for
523 * the Index to determine the size of the NULL terminated string.
525 if (9 + additional_bytes
< temp16
) {
526 temp8
= (u8
) (temp16
- (9 + additional_bytes
));
531 /* Ensure a 32-bit boundary for the structure */
533 temp8
= (u8
) ACPI_ROUND_UP_to_32_bITS(temp8
);
536 ACPI_SIZEOF_RESOURCE(struct acpi_resource_ext_irq
) +
537 (additional_bytes
* sizeof(u8
)) +
538 (temp8
* sizeof(u8
));
541 case ACPI_RDESC_TYPE_IRQ_FORMAT
:
544 * Determine if it there are two or three trailing bytes
546 buffer
= byte_stream_buffer
;
555 /* Point past the descriptor */
559 /* Look at the number of bits set */
561 ACPI_MOVE_16_TO_16(&temp16
, buffer
);
563 for (index
= 0; index
< 16; index
++) {
565 ++number_of_interrupts
;
572 ACPI_SIZEOF_RESOURCE(struct acpi_resource_io
) +
573 (number_of_interrupts
* sizeof(u32
));
576 case ACPI_RDESC_TYPE_DMA_FORMAT
:
580 buffer
= byte_stream_buffer
;
583 /* Point past the descriptor */
587 /* Look at the number of bits set */
591 for (index
= 0; index
< 8; index
++) {
593 ++number_of_channels
;
600 ACPI_SIZEOF_RESOURCE(struct acpi_resource_dma
) +
601 (number_of_channels
* sizeof(u32
));
604 case ACPI_RDESC_TYPE_START_DEPENDENT
:
606 * Start Dependent Functions Resource
607 * Determine if it there are two or three trailing bytes
609 buffer
= byte_stream_buffer
;
619 ACPI_SIZEOF_RESOURCE(struct
620 acpi_resource_start_dpf
);
623 case ACPI_RDESC_TYPE_END_DEPENDENT
:
625 * End Dependent Functions Resource
628 structure_size
= ACPI_RESOURCE_LENGTH
;
631 case ACPI_RDESC_TYPE_IO_PORT
:
637 ACPI_SIZEOF_RESOURCE(struct acpi_resource_io
);
640 case ACPI_RDESC_TYPE_FIXED_IO_PORT
:
642 * Fixed IO Port Resource
646 ACPI_SIZEOF_RESOURCE(struct acpi_resource_fixed_io
);
649 case ACPI_RDESC_TYPE_SMALL_VENDOR
:
651 * Vendor Specific Resource
653 buffer
= byte_stream_buffer
;
656 temp8
= (u8
) (temp8
& 0x7);
657 bytes_consumed
= temp8
+ 1;
659 /* Ensure a 32-bit boundary for the structure */
661 temp8
= (u8
) ACPI_ROUND_UP_to_32_bITS(temp8
);
663 ACPI_SIZEOF_RESOURCE(struct acpi_resource_vendor
) +
664 (temp8
* sizeof(u8
));
667 case ACPI_RDESC_TYPE_END_TAG
:
672 structure_size
= ACPI_RESOURCE_LENGTH
;
673 byte_stream_buffer_length
= bytes_parsed
;
678 * If we get here, everything is out of sync,
681 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE
);
684 /* Update the return value and counter */
686 buffer_size
+= (u32
) ACPI_ALIGN_RESOURCE_SIZE(structure_size
);
687 bytes_parsed
+= bytes_consumed
;
689 /* Set the byte stream to point to the next resource */
691 byte_stream_buffer
+= bytes_consumed
;
694 /* This is the data the caller needs */
696 *size_needed
= buffer_size
;
697 return_ACPI_STATUS(AE_OK
);
700 /*******************************************************************************
702 * FUNCTION: acpi_rs_get_pci_routing_table_length
704 * PARAMETERS: package_object - Pointer to the package object
705 * buffer_size_needed - u32 pointer of the size buffer
706 * needed to properly return the
711 * DESCRIPTION: Given a package representing a PCI routing table, this
712 * calculates the size of the corresponding linked list of
715 ******************************************************************************/
718 acpi_rs_get_pci_routing_table_length(union acpi_operand_object
*package_object
,
719 acpi_size
* buffer_size_needed
)
721 u32 number_of_elements
;
722 acpi_size temp_size_needed
= 0;
723 union acpi_operand_object
**top_object_list
;
725 union acpi_operand_object
*package_element
;
726 union acpi_operand_object
**sub_object_list
;
730 ACPI_FUNCTION_TRACE("rs_get_pci_routing_table_length");
732 number_of_elements
= package_object
->package
.count
;
735 * Calculate the size of the return buffer.
736 * The base size is the number of elements * the sizes of the
737 * structures. Additional space for the strings is added below.
738 * The minus one is to subtract the size of the u8 Source[1]
739 * member because it is added below.
741 * But each PRT_ENTRY structure has a pointer to a string and
742 * the size of that string must be found.
744 top_object_list
= package_object
->package
.elements
;
746 for (index
= 0; index
< number_of_elements
; index
++) {
747 /* Dereference the sub-package */
749 package_element
= *top_object_list
;
752 * The sub_object_list will now point to an array of the
753 * four IRQ elements: Address, Pin, Source and source_index
755 sub_object_list
= package_element
->package
.elements
;
757 /* Scan the irq_table_elements for the Source Name String */
761 for (table_index
= 0; table_index
< 4 && !name_found
;
763 if ((ACPI_TYPE_STRING
==
764 ACPI_GET_OBJECT_TYPE(*sub_object_list
))
766 ((ACPI_TYPE_LOCAL_REFERENCE
==
767 ACPI_GET_OBJECT_TYPE(*sub_object_list
))
768 && ((*sub_object_list
)->reference
.opcode
==
769 AML_INT_NAMEPATH_OP
))) {
772 /* Look at the next element */
778 temp_size_needed
+= (sizeof(struct acpi_pci_routing_table
) - 4);
780 /* Was a String type found? */
783 if (ACPI_GET_OBJECT_TYPE(*sub_object_list
) ==
786 * The length String.Length field does not include the
787 * terminating NULL, add 1
789 temp_size_needed
+= ((acpi_size
)
790 (*sub_object_list
)->string
.
793 temp_size_needed
+= acpi_ns_get_pathname_length((*sub_object_list
)->reference
.node
);
797 * If no name was found, then this is a NULL, which is
798 * translated as a u32 zero.
800 temp_size_needed
+= sizeof(u32
);
803 /* Round up the size since each element must be aligned */
805 temp_size_needed
= ACPI_ROUND_UP_to_64_bITS(temp_size_needed
);
807 /* Point to the next union acpi_operand_object */
813 * Adding an extra element to the end of the list, essentially a
816 *buffer_size_needed
=
817 temp_size_needed
+ sizeof(struct acpi_pci_routing_table
);
818 return_ACPI_STATUS(AE_OK
);