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/amlresrc.h>
48 #include <acpi/acnamesp.h>
50 #define _COMPONENT ACPI_RESOURCES
51 ACPI_MODULE_NAME("rscalc")
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)
58 static 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 */
78 * Base sizes of resource descriptors, both the actual AML stream length and
79 * size of the internal struct representation.
81 typedef struct acpi_resource_sizes {
82 u8 minimum_stream_size;
83 u8 minimum_struct_size;
85 } ACPI_RESOURCE_SIZES;
87 static 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 */
106 static 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 */
121 /* Local prototypes */
123 static u8 acpi_rs_count_set_bits(u16 bit_field);
125 static ACPI_RESOURCE_SIZES *acpi_rs_get_resource_sizes(u8 resource_type);
127 static u16 acpi_rs_get_resource_length(u8 * resource);
130 acpi_rs_struct_option_length(struct acpi_resource_source *resource_source);
133 acpi_rs_stream_option_length(u32 resource_length, u32 minimum_total_length);
135 /*******************************************************************************
137 * FUNCTION: acpi_rs_count_set_bits
139 * PARAMETERS: bit_field - Field in which to count bits
141 * RETURN: Number of bits set within the field
143 * DESCRIPTION: Count the number of bits set in a resource field. Used for
144 * (Short descriptor) interrupt and DMA lists.
146 ******************************************************************************/
148 static u8 acpi_rs_count_set_bits(u16 bit_field)
152 ACPI_FUNCTION_ENTRY();
154 for (bits_set = 0; bit_field; bits_set++) {
155 /* Zero the least significant bit that is set */
157 bit_field &= (bit_field - 1);
163 /*******************************************************************************
165 * FUNCTION: acpi_rs_get_resource_sizes
167 * PARAMETERS: resource_type - Byte 0 of a resource descriptor
169 * RETURN: Pointer to the resource conversion handler
171 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
172 * a resource descriptor.
174 ******************************************************************************/
176 static ACPI_RESOURCE_SIZES *acpi_rs_get_resource_sizes(u8 resource_type)
178 ACPI_RESOURCE_SIZES *size_info;
180 ACPI_FUNCTION_ENTRY();
182 /* Determine if this is a small or large resource */
184 if (resource_type & ACPI_RDESC_TYPE_LARGE) {
185 /* Large Resource Type -- bits 6:0 contain the name */
187 if (resource_type > ACPI_RDESC_LARGE_MAX) {
191 size_info = &acpi_gbl_lg_resource_sizes[(resource_type &
192 ACPI_RDESC_LARGE_MASK)];
194 /* Small Resource Type -- bits 6:3 contain the name */
196 size_info = &acpi_gbl_sm_resource_sizes[((resource_type &
197 ACPI_RDESC_SMALL_MASK)
201 /* Zero entry indicates an invalid resource type */
203 if (!size_info->minimum_stream_size) {
210 /*******************************************************************************
212 * FUNCTION: acpi_rs_get_resource_length
214 * PARAMETERS: Resource - Pointer to the resource descriptor
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.
220 * DESCRIPTION: Extract the length of a resource descriptor.
222 ******************************************************************************/
224 static u16 acpi_rs_get_resource_length(u8 * resource)
228 ACPI_FUNCTION_ENTRY();
230 /* Determine if this is a small or large resource */
232 if (*resource & ACPI_RDESC_TYPE_LARGE) {
233 /* Large Resource type -- length is in bytes 1-2 */
235 ACPI_MOVE_16_TO_16(&resource_length, (resource + 1));
238 /* Small Resource Type -- bits 2:0 of byte 0 contain the length */
241 (u16) (*resource & ACPI_RDESC_SMALL_LENGTH_MASK);
244 return (resource_length);
247 /*******************************************************************************
249 * FUNCTION: acpi_rs_struct_option_length
251 * PARAMETERS: resource_source - Pointer to optional descriptor field
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
259 ******************************************************************************/
262 acpi_rs_struct_option_length(struct acpi_resource_source *resource_source)
264 ACPI_FUNCTION_ENTRY();
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).
271 if (resource_source->string_ptr) {
272 return ((acpi_size) resource_source->string_length + 1);
278 /*******************************************************************************
280 * FUNCTION: acpi_rs_stream_option_length
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
286 * RETURN: Length of optional string (0 if no string present)
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
292 ******************************************************************************/
295 acpi_rs_stream_option_length(u32 resource_length, u32 minimum_total_length)
297 u32 string_length = 0;
298 u32 minimum_resource_length;
300 ACPI_FUNCTION_ENTRY();
303 * The resource_source_index and resource_source are optional elements of some
304 * Large-type resource descriptors.
307 /* Compute minimum size of the data part of the resource descriptor */
309 minimum_resource_length =
310 minimum_total_length - sizeof(struct asl_large_header);
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.
319 if (resource_length > minimum_resource_length) {
320 /* Compute the length of the optional string */
322 string_length = resource_length - minimum_resource_length - 1;
325 /* Round up length to 32 bits for internal structure alignment */
327 return (ACPI_ROUND_UP_to_32_bITS(string_length));
330 /*******************************************************************************
332 * FUNCTION: acpi_rs_get_byte_stream_length
334 * PARAMETERS: Resource - Pointer to the resource linked list
335 * size_needed - Where the required size is returned
339 * 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.
343 ******************************************************************************/
346 acpi_rs_get_byte_stream_length(struct acpi_resource * resource,
347 acpi_size * size_needed)
349 acpi_size byte_stream_size_needed = 0;
350 acpi_size segment_size;
352 ACPI_FUNCTION_TRACE("rs_get_byte_stream_length");
354 /* Traverse entire list of internal resource descriptors */
357 /* Validate the descriptor type */
359 if (resource->type > ACPI_RSTYPE_MAX) {
360 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
363 /* Get the base size of the (external stream) resource descriptor */
365 segment_size = acpi_gbl_stream_sizes[resource->type];
368 * Augment the base size for descriptors with optional and/or
369 * variable-length fields
371 switch (resource->type) {
372 case ACPI_RSTYPE_VENDOR:
374 * Vendor Defined Resource:
375 * 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.
379 if (resource->data.vendor_specific.length > 7) {
380 /* Base size of a Large resource descriptor */
385 /* Add the size of the vendor-specific data */
387 segment_size += resource->data.vendor_specific.length;
390 case ACPI_RSTYPE_END_TAG:
393 * We are done -- return the accumulated total size.
395 *size_needed = byte_stream_size_needed + segment_size;
399 return_ACPI_STATUS(AE_OK);
401 case ACPI_RSTYPE_ADDRESS16:
403 * 16-Bit Address Resource:
404 * Add the size of the optional resource_source info
407 acpi_rs_struct_option_length(&resource->data.
412 case ACPI_RSTYPE_ADDRESS32:
414 * 32-Bit Address Resource:
415 * Add the size of the optional resource_source info
418 acpi_rs_struct_option_length(&resource->data.
423 case ACPI_RSTYPE_ADDRESS64:
425 * 64-Bit Address Resource:
426 * Add the size of the optional resource_source info
429 acpi_rs_struct_option_length(&resource->data.
434 case ACPI_RSTYPE_EXT_IRQ:
436 * Extended IRQ Resource:
437 * Add the size of each additional optional interrupt beyond the
438 * required 1 (4 bytes for each u32 interrupt number)
440 segment_size += (((acpi_size)
441 resource->data.extended_irq.
442 number_of_interrupts - 1) * 4);
444 /* Add the size of the optional resource_source info */
447 acpi_rs_struct_option_length(&resource->data.
456 /* Update the total */
458 byte_stream_size_needed += segment_size;
460 /* Point to the next object */
462 resource = ACPI_PTR_ADD(struct acpi_resource,
463 resource, resource->length);
466 /* Did not find an END_TAG descriptor */
468 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
471 /*******************************************************************************
473 * FUNCTION: acpi_rs_get_list_length
475 * PARAMETERS: byte_stream_buffer - Pointer to the resource byte stream
476 * byte_stream_buffer_length - Size of byte_stream_buffer
477 * size_needed - Where the size needed is returned
481 * DESCRIPTION: Takes an external resource byte stream and calculates the size
482 * buffer needed to hold the corresponding internal resource
483 * descriptor linked list.
485 ******************************************************************************/
488 acpi_rs_get_list_length(u8 * byte_stream_buffer,
489 u32 byte_stream_buffer_length, acpi_size * size_needed)
492 ACPI_RESOURCE_SIZES *resource_info;
494 u32 bytes_parsed = 0;
499 u32 extra_struct_bytes;
501 ACPI_FUNCTION_TRACE("rs_get_list_length");
503 while (bytes_parsed < byte_stream_buffer_length) {
504 /* The next byte in the stream is the resource descriptor type */
506 resource_type = acpi_rs_get_resource_type(*byte_stream_buffer);
508 /* Get the base stream size and structure sizes for the descriptor */
510 resource_info = acpi_rs_get_resource_sizes(resource_type);
511 if (!resource_info) {
512 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
515 /* Get the Length field from the input resource descriptor */
518 acpi_rs_get_resource_length(byte_stream_buffer);
520 /* Augment the size for descriptors with optional fields */
522 extra_struct_bytes = 0;
524 if (!(resource_type & ACPI_RDESC_TYPE_LARGE)) {
526 * Small resource descriptors
529 buffer = byte_stream_buffer + header_length;
531 switch (resource_type) {
532 case ACPI_RDESC_TYPE_IRQ_FORMAT:
535 * Get the number of bits set in the IRQ word
537 ACPI_MOVE_16_TO_16(&temp16, buffer);
540 (acpi_rs_count_set_bits(temp16) *
544 case ACPI_RDESC_TYPE_DMA_FORMAT:
547 * Get the number of bits set in the DMA channels byte
550 (acpi_rs_count_set_bits((u16) * buffer) *
554 case ACPI_RDESC_TYPE_SMALL_VENDOR:
556 * Vendor Specific Resource:
557 * Ensure a 32-bit boundary for the structure
560 ACPI_ROUND_UP_to_32_bITS(resource_length);
563 case ACPI_RDESC_TYPE_END_TAG:
566 * Terminate the loop now
568 byte_stream_buffer_length = bytes_parsed;
576 * Large resource descriptors
578 header_length = sizeof(struct asl_large_header);
579 buffer = byte_stream_buffer + header_length;
581 switch (resource_type) {
582 case ACPI_RDESC_TYPE_LARGE_VENDOR:
584 * Vendor Defined Resource:
585 * Add vendor data and ensure a 32-bit boundary for the structure
588 ACPI_ROUND_UP_to_32_bITS(resource_length);
591 case ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE:
592 case ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE:
594 * 32-Bit or 16-bit Address Resource:
595 * Add the size of any optional data (resource_source)
598 acpi_rs_stream_option_length
600 resource_info->minimum_stream_size);
603 case ACPI_RDESC_TYPE_EXTENDED_XRUPT:
606 * Point past the interrupt_vector_flags to get the
607 * interrupt_table_length.
612 * Add 4 bytes for each additional interrupt. Note: at least one
613 * interrupt is required and is included in the minimum
617 ((*buffer - 1) * sizeof(u32));
619 /* Add the size of any optional data (resource_source) */
621 extra_struct_bytes +=
622 acpi_rs_stream_option_length(resource_length
626 minimum_stream_size);
629 case ACPI_RDESC_TYPE_QWORD_ADDRESS_SPACE:
631 * 64-Bit Address Resource:
632 * Add the size of any optional data (resource_source)
633 * Ensure a 64-bit boundary for the structure
636 ACPI_ROUND_UP_to_64_bITS
637 (acpi_rs_stream_option_length
639 resource_info->minimum_stream_size));
647 /* Update the required buffer size for the internal descriptor structs */
650 (u16) (resource_info->minimum_struct_size +
652 buffer_size += (u32) ACPI_ALIGN_RESOURCE_SIZE(temp16);
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
658 temp16 = (u16) (header_length + resource_length);
659 bytes_parsed += temp16;
660 byte_stream_buffer += temp16;
663 /* This is the data the caller needs */
665 *size_needed = buffer_size;
666 return_ACPI_STATUS(AE_OK);
669 /*******************************************************************************
671 * FUNCTION: acpi_rs_get_pci_routing_table_length
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
680 * DESCRIPTION: Given a package representing a PCI routing table, this
681 * calculates the size of the corresponding linked list of
684 ******************************************************************************/
687 acpi_rs_get_pci_routing_table_length(union acpi_operand_object *package_object,
688 acpi_size * buffer_size_needed)
690 u32 number_of_elements;
691 acpi_size temp_size_needed = 0;
692 union acpi_operand_object **top_object_list;
694 union acpi_operand_object *package_element;
695 union acpi_operand_object **sub_object_list;
699 ACPI_FUNCTION_TRACE("rs_get_pci_routing_table_length");
701 number_of_elements = package_object->package.count;
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.
710 * But each PRT_ENTRY structure has a pointer to a string and
711 * the size of that string must be found.
713 top_object_list = package_object->package.elements;
715 for (index = 0; index < number_of_elements; index++) {
716 /* Dereference the sub-package */
718 package_element = *top_object_list;
721 * The sub_object_list will now point to an array of the
722 * four IRQ elements: Address, Pin, Source and source_index
724 sub_object_list = package_element->package.elements;
726 /* Scan the irq_table_elements for the Source Name String */
730 for (table_index = 0; table_index < 4 && !name_found;
732 if ((ACPI_TYPE_STRING ==
733 ACPI_GET_OBJECT_TYPE(*sub_object_list))
735 ((ACPI_TYPE_LOCAL_REFERENCE ==
736 ACPI_GET_OBJECT_TYPE(*sub_object_list))
737 && ((*sub_object_list)->reference.opcode ==
738 AML_INT_NAMEPATH_OP))) {
741 /* Look at the next element */
747 temp_size_needed += (sizeof(struct acpi_pci_routing_table) - 4);
749 /* Was a String type found? */
752 if (ACPI_GET_OBJECT_TYPE(*sub_object_list) ==
755 * The length String.Length field does not include the
756 * terminating NULL, add 1
758 temp_size_needed += ((acpi_size)
759 (*sub_object_list)->string.
762 temp_size_needed += acpi_ns_get_pathname_length((*sub_object_list)->reference.node);
766 * If no name was found, then this is a NULL, which is
767 * translated as a u32 zero.
769 temp_size_needed += sizeof(u32);
772 /* Round up the size since each element must be aligned */
774 temp_size_needed = ACPI_ROUND_UP_to_64_bITS(temp_size_needed);
776 /* Point to the next union acpi_operand_object */
782 * Adding an extra element to the end of the list, essentially a
785 *buffer_size_needed =
786 temp_size_needed + sizeof(struct acpi_pci_routing_table);
787 return_ACPI_STATUS(AE_OK);