1 /*******************************************************************************
3 * Module Name: rscalc - Calculate stream and list lengths
6 ******************************************************************************/
9 * Copyright (C) 2000, 2001 R. Byron Moore
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #define _COMPONENT ACPI_RESOURCES
33 MODULE_NAME ("rscalc")
36 /*******************************************************************************
38 * FUNCTION: Acpi_rs_calculate_byte_stream_length
40 * PARAMETERS: Linked_list - Pointer to the resource linked list
41 * Size_needed - u32 pointer of the size buffer needed
42 * to properly return the parsed data
46 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
47 * the size buffer needed to hold the linked list that conveys
50 ******************************************************************************/
53 acpi_rs_calculate_byte_stream_length (
54 acpi_resource *linked_list,
57 u32 byte_stream_size_needed = 0;
59 acpi_resource_ext_irq *ex_irq = NULL;
63 FUNCTION_TRACE ("Rs_calculate_byte_stream_length");
68 * Init the variable that will hold the size to add to the total.
72 switch (linked_list->id) {
76 * For an IRQ Resource, Byte 3, although optional, will
77 * always be created - it holds IRQ information.
85 * For this resource the size is static
90 case ACPI_RSTYPE_START_DPF:
92 * Start Dependent Functions Resource
93 * For a Start_dependent_functions Resource, Byte 1,
94 * although optional, will always be created.
99 case ACPI_RSTYPE_END_DPF:
101 * End Dependent Functions Resource
102 * For this resource the size is static
110 * For this resource the size is static
115 case ACPI_RSTYPE_FIXED_IO:
117 * Fixed IO Port Resource
118 * For this resource the size is static
123 case ACPI_RSTYPE_VENDOR:
125 * Vendor Defined Resource
126 * For a Vendor Specific resource, if the Length is
127 * between 1 and 7 it will be created as a Small
128 * Resource data type, otherwise it is a Large
129 * Resource data type.
131 if (linked_list->data.vendor_specific.length > 7) {
137 segment_size += linked_list->data.vendor_specific.length;
140 case ACPI_RSTYPE_END_TAG:
143 * For this resource the size is static
149 case ACPI_RSTYPE_MEM24:
151 * 24-Bit Memory Resource
152 * For this resource the size is static
157 case ACPI_RSTYPE_MEM32:
159 * 32-Bit Memory Range Resource
160 * For this resource the size is static
165 case ACPI_RSTYPE_FIXED_MEM32:
167 * 32-Bit Fixed Memory Resource
168 * For this resource the size is static
173 case ACPI_RSTYPE_ADDRESS16:
175 * 16-Bit Address Resource
176 * The base size of this byte stream is 16. If a
177 * Resource Source string is not NULL, add 1 for
178 * the Index + the length of the null terminated
179 * string Resource Source + 1 for the null.
183 if (NULL != linked_list->data.address16.resource_source.string_ptr) {
185 linked_list->data.address16.resource_source.string_length);
189 case ACPI_RSTYPE_ADDRESS32:
191 * 32-Bit Address Resource
192 * The base size of this byte stream is 26. If a Resource
193 * Source string is not NULL, add 1 for the Index + the
194 * length of the null terminated string Resource Source +
199 if (NULL != linked_list->data.address32.resource_source.string_ptr) {
201 linked_list->data.address32.resource_source.string_length);
205 case ACPI_RSTYPE_ADDRESS64:
207 * 64-Bit Address Resource
208 * The base size of this byte stream is 46. If a Resource
209 * Source string is not NULL, add 1 for the Index + the
210 * length of the null terminated string Resource Source +
215 if (NULL != linked_list->data.address64.resource_source.string_ptr) {
217 linked_list->data.address64.resource_source.string_length);
221 case ACPI_RSTYPE_EXT_IRQ:
223 * Extended IRQ Resource
224 * The base size of this byte stream is 9. This is for an
225 * Interrupt table length of 1. For each additional
227 * If a Resource Source string is not NULL, add 1 for the
228 * Index + the length of the null terminated string
229 * Resource Source + 1 for the null.
232 ((linked_list->data.extended_irq.number_of_interrupts - 1) * 4);
234 if (NULL != ex_irq->resource_source.string_ptr) {
236 linked_list->data.extended_irq.resource_source.string_length);
242 * If we get here, everything is out of sync,
243 * so exit with an error
245 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
248 } /* switch (Linked_list->Id) */
253 byte_stream_size_needed += segment_size;
256 * Point to the next object
258 linked_list = POINTER_ADD (acpi_resource,
259 linked_list, linked_list->length);
263 * This is the data the caller needs
265 *size_needed = byte_stream_size_needed;
266 return_ACPI_STATUS (AE_OK);
270 /*******************************************************************************
272 * FUNCTION: Acpi_rs_calculate_list_length
274 * PARAMETERS: Byte_stream_buffer - Pointer to the resource byte stream
275 * Byte_stream_buffer_length - Size of Byte_stream_buffer
276 * Size_needed - u32 pointer of the size buffer
277 * needed to properly return the
282 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
283 * the size buffer needed to hold the linked list that conveys
286 ******************************************************************************/
289 acpi_rs_calculate_list_length (
290 u8 *byte_stream_buffer,
291 u32 byte_stream_buffer_length,
295 u32 bytes_parsed = 0;
296 u8 number_of_interrupts = 0;
297 u8 number_of_channels = 0;
308 FUNCTION_TRACE ("Rs_calculate_list_length");
311 while (bytes_parsed < byte_stream_buffer_length) {
313 * The next byte in the stream is the resource type
315 resource_type = acpi_rs_get_resource_type (*byte_stream_buffer);
317 switch (resource_type) {
318 case RESOURCE_DESC_MEMORY_24:
320 * 24-Bit Memory Resource
324 structure_size = SIZEOF_RESOURCE (acpi_resource_mem24);
328 case RESOURCE_DESC_LARGE_VENDOR:
330 * Vendor Defined Resource
332 buffer = byte_stream_buffer;
335 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
336 bytes_consumed = temp16 + 3;
339 * Ensure a 32-bit boundary for the structure
341 temp16 = (u16) ROUND_UP_TO_32_bITS (temp16);
343 structure_size = SIZEOF_RESOURCE (acpi_resource_vendor) +
344 (temp16 * sizeof (u8));
348 case RESOURCE_DESC_MEMORY_32:
350 * 32-Bit Memory Range Resource
355 structure_size = SIZEOF_RESOURCE (acpi_resource_mem32);
359 case RESOURCE_DESC_FIXED_MEMORY_32:
361 * 32-Bit Fixed Memory Resource
365 structure_size = SIZEOF_RESOURCE (acpi_resource_fixed_mem32);
369 case RESOURCE_DESC_QWORD_ADDRESS_SPACE:
371 * 64-Bit Address Resource
373 buffer = byte_stream_buffer;
376 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
378 bytes_consumed = temp16 + 3;
381 * Resource Source Index and Resource Source are
382 * optional elements. Check the length of the
383 * Bytestream. If it is greater than 43, that
384 * means that an Index exists and is followed by
385 * a null termininated string. Therefore, set
386 * the temp variable to the length minus the minimum
387 * byte stream length plus the byte for the Index to
388 * determine the size of the NULL terminiated string.
391 temp8 = (u8) (temp16 - 44);
398 * Ensure a 64-bit boundary for the structure
400 temp8 = (u8) ROUND_UP_TO_64_bITS (temp8);
402 structure_size = SIZEOF_RESOURCE (acpi_resource_address64) +
403 (temp8 * sizeof (u8));
407 case RESOURCE_DESC_DWORD_ADDRESS_SPACE:
409 * 32-Bit Address Resource
411 buffer = byte_stream_buffer;
414 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
416 bytes_consumed = temp16 + 3;
419 * Resource Source Index and Resource Source are
420 * optional elements. Check the length of the
421 * Bytestream. If it is greater than 23, that
422 * means that an Index exists and is followed by
423 * a null termininated string. Therefore, set
424 * the temp variable to the length minus the minimum
425 * byte stream length plus the byte for the Index to
426 * determine the size of the NULL terminiated string.
429 temp8 = (u8) (temp16 - 24);
436 * Ensure a 32-bit boundary for the structure
438 temp8 = (u8) ROUND_UP_TO_32_bITS (temp8);
440 structure_size = SIZEOF_RESOURCE (acpi_resource_address32) +
441 (temp8 * sizeof (u8));
445 case RESOURCE_DESC_WORD_ADDRESS_SPACE:
447 * 16-Bit Address Resource
449 buffer = byte_stream_buffer;
452 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
454 bytes_consumed = temp16 + 3;
457 * Resource Source Index and Resource Source are
458 * optional elements. Check the length of the
459 * Bytestream. If it is greater than 13, that
460 * means that an Index exists and is followed by
461 * a null termininated string. Therefore, set
462 * the temp variable to the length minus the minimum
463 * byte stream length plus the byte for the Index to
464 * determine the size of the NULL terminiated string.
467 temp8 = (u8) (temp16 - 14);
474 * Ensure a 32-bit boundary for the structure
476 temp8 = (u8) ROUND_UP_TO_32_bITS (temp8);
478 structure_size = SIZEOF_RESOURCE (acpi_resource_address16) +
479 (temp8 * sizeof (u8));
483 case RESOURCE_DESC_EXTENDED_XRUPT:
487 buffer = byte_stream_buffer;
490 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
492 bytes_consumed = temp16 + 3;
495 * Point past the length field and the
496 * Interrupt vector flags to save off the
497 * Interrupt table length to the Temp8 variable.
503 * To compensate for multiple interrupt numbers, add 4 bytes for
504 * each additional interrupts greater than 1
506 additional_bytes = (u8) ((temp8 - 1) * 4);
509 * Resource Source Index and Resource Source are
510 * optional elements. Check the length of the
511 * Bytestream. If it is greater than 9, that
512 * means that an Index exists and is followed by
513 * a null termininated string. Therefore, set
514 * the temp variable to the length minus the minimum
515 * byte stream length plus the byte for the Index to
516 * determine the size of the NULL terminiated string.
518 if (9 + additional_bytes < temp16) {
519 temp8 = (u8) (temp16 - (9 + additional_bytes));
527 * Ensure a 32-bit boundary for the structure
529 temp8 = (u8) ROUND_UP_TO_32_bITS (temp8);
531 structure_size = SIZEOF_RESOURCE (acpi_resource_ext_irq) +
532 (additional_bytes * sizeof (u8)) +
533 (temp8 * sizeof (u8));
537 case RESOURCE_DESC_IRQ_FORMAT:
540 * Determine if it there are two or three trailing bytes
542 buffer = byte_stream_buffer;
554 * Point past the descriptor
559 * Look at the number of bits set
561 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
563 for (index = 0; index < 16; index++) {
565 ++number_of_interrupts;
571 structure_size = SIZEOF_RESOURCE (acpi_resource_io) +
572 (number_of_interrupts * sizeof (u32));
576 case RESOURCE_DESC_DMA_FORMAT:
580 buffer = byte_stream_buffer;
584 * Point past the descriptor
589 * Look at the number of bits set
593 for(index = 0; index < 8; index++) {
595 ++number_of_channels;
601 structure_size = SIZEOF_RESOURCE (acpi_resource_dma) +
602 (number_of_channels * sizeof (u32));
606 case RESOURCE_DESC_START_DEPENDENT:
608 * Start Dependent Functions Resource
609 * Determine if it there are two or three trailing bytes
611 buffer = byte_stream_buffer;
621 structure_size = SIZEOF_RESOURCE (acpi_resource_start_dpf);
625 case RESOURCE_DESC_END_DEPENDENT:
627 * End Dependent Functions Resource
630 structure_size = ACPI_RESOURCE_LENGTH;
634 case RESOURCE_DESC_IO_PORT:
639 structure_size = SIZEOF_RESOURCE (acpi_resource_io);
643 case RESOURCE_DESC_FIXED_IO_PORT:
645 * Fixed IO Port Resource
648 structure_size = SIZEOF_RESOURCE (acpi_resource_fixed_io);
652 case RESOURCE_DESC_SMALL_VENDOR:
654 * Vendor Specific Resource
656 buffer = byte_stream_buffer;
659 temp8 = (u8) (temp8 & 0x7);
660 bytes_consumed = temp8 + 1;
663 * Ensure a 32-bit boundary for the structure
665 temp8 = (u8) ROUND_UP_TO_32_bITS (temp8);
666 structure_size = SIZEOF_RESOURCE (acpi_resource_vendor) +
667 (temp8 * sizeof (u8));
671 case RESOURCE_DESC_END_TAG:
676 structure_size = ACPI_RESOURCE_LENGTH;
677 byte_stream_buffer_length = bytes_parsed;
683 * If we get here, everything is out of sync,
684 * so exit with an error
686 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
692 * Update the return value and counter
694 buffer_size += structure_size;
695 bytes_parsed += bytes_consumed;
698 * Set the byte stream to point to the next resource
700 byte_stream_buffer += bytes_consumed;
705 * This is the data the caller needs
707 *size_needed = buffer_size;
708 return_ACPI_STATUS (AE_OK);
712 /*******************************************************************************
714 * FUNCTION: Acpi_rs_calculate_pci_routing_table_length
716 * PARAMETERS: Package_object - Pointer to the package object
717 * Buffer_size_needed - u32 pointer of the size buffer
718 * needed to properly return the
723 * DESCRIPTION: Given a package representing a PCI routing table, this
724 * calculates the size of the corresponding linked list of
727 ******************************************************************************/
730 acpi_rs_calculate_pci_routing_table_length (
731 acpi_operand_object *package_object,
732 u32 *buffer_size_needed)
734 u32 number_of_elements;
735 u32 temp_size_needed = 0;
736 acpi_operand_object **top_object_list;
738 acpi_operand_object *package_element;
739 acpi_operand_object **sub_object_list;
744 FUNCTION_TRACE ("Rs_calculate_pci_routing_table_length");
747 number_of_elements = package_object->package.count;
750 * Calculate the size of the return buffer.
751 * The base size is the number of elements * the sizes of the
752 * structures. Additional space for the strings is added below.
753 * The minus one is to subtract the size of the u8 Source[1]
754 * member because it is added below.
756 * But each PRT_ENTRY structure has a pointer to a string and
757 * the size of that string must be found.
759 top_object_list = package_object->package.elements;
761 for (index = 0; index < number_of_elements; index++) {
763 * Dereference the sub-package
765 package_element = *top_object_list;
768 * The Sub_object_list will now point to an array of the
769 * four IRQ elements: Address, Pin, Source and Source_index
771 sub_object_list = package_element->package.elements;
774 * Scan the Irq_table_elements for the Source Name String
778 for (table_index = 0; table_index < 4 && !name_found; table_index++) {
779 if ((ACPI_TYPE_STRING == (*sub_object_list)->common.type) ||
780 ((INTERNAL_TYPE_REFERENCE == (*sub_object_list)->common.type) &&
781 ((*sub_object_list)->reference.opcode == AML_INT_NAMEPATH_OP))) {
787 * Look at the next element
793 temp_size_needed += (sizeof (pci_routing_table) - 4);
796 * Was a String type found?
798 if (TRUE == name_found) {
799 if (ACPI_TYPE_STRING == (*sub_object_list)->common.type) {
801 * The length String.Length field includes the
804 temp_size_needed += (*sub_object_list)->string.length;
808 temp_size_needed += acpi_ns_get_pathname_length (
809 (*sub_object_list)->reference.node);
815 * If no name was found, then this is a NULL, which is
816 * translated as a u32 zero.
818 temp_size_needed += sizeof (u32);
821 /* Round up the size since each element must be aligned */
823 temp_size_needed = ROUND_UP_TO_64_bITS (temp_size_needed);
826 * Point to the next acpi_operand_object
833 * Adding an extra element to the end of the list, essentially a NULL terminator
835 *buffer_size_needed = temp_size_needed + sizeof (pci_routing_table);
836 return_ACPI_STATUS (AE_OK);