1 /*******************************************************************************
3 * Module Name: rscalc - Calculate stream and list lengths
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
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.
50 #define _COMPONENT ACPI_RESOURCES
51 ACPI_MODULE_NAME ("rscalc")
54 /* Local prototypes */
61 AcpiRsStructOptionLength (
62 ACPI_RESOURCE_SOURCE *ResourceSource);
65 AcpiRsStreamOptionLength (
66 UINT32 ResourceLength,
67 UINT32 MinimumTotalLength);
70 /*******************************************************************************
72 * FUNCTION: AcpiRsCountSetBits
74 * PARAMETERS: BitField - Field in which to count bits
76 * RETURN: Number of bits set within the field
78 * DESCRIPTION: Count the number of bits set in a resource field. Used for
79 * (Short descriptor) interrupt and DMA lists.
81 ******************************************************************************/
90 ACPI_FUNCTION_ENTRY ();
93 for (BitsSet = 0; BitField; BitsSet++)
95 /* Zero the least significant bit that is set */
97 BitField &= (UINT16) (BitField - 1);
104 /*******************************************************************************
106 * FUNCTION: AcpiRsStructOptionLength
108 * PARAMETERS: ResourceSource - Pointer to optional descriptor field
112 * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
113 * ResourceSource fields in some Large descriptors. Used during
114 * list-to-stream conversion
116 ******************************************************************************/
118 static ACPI_RS_LENGTH
119 AcpiRsStructOptionLength (
120 ACPI_RESOURCE_SOURCE *ResourceSource)
122 ACPI_FUNCTION_ENTRY ();
126 * If the ResourceSource string is valid, return the size of the string
127 * (StringLength includes the NULL terminator) plus the size of the
128 * ResourceSourceIndex (1).
130 if (ResourceSource->StringPtr)
132 return ((ACPI_RS_LENGTH) (ResourceSource->StringLength + 1));
139 /*******************************************************************************
141 * FUNCTION: AcpiRsStreamOptionLength
143 * PARAMETERS: ResourceLength - Length from the resource header
144 * MinimumTotalLength - Minimum length of this resource, before
145 * any optional fields. Includes header size
147 * RETURN: Length of optional string (0 if no string present)
149 * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
150 * ResourceSource fields in some Large descriptors. Used during
151 * stream-to-list conversion
153 ******************************************************************************/
156 AcpiRsStreamOptionLength (
157 UINT32 ResourceLength,
158 UINT32 MinimumAmlResourceLength)
160 UINT32 StringLength = 0;
163 ACPI_FUNCTION_ENTRY ();
167 * The ResourceSourceIndex and ResourceSource are optional elements of some
168 * Large-type resource descriptors.
172 * If the length of the actual resource descriptor is greater than the ACPI
173 * spec-defined minimum length, it means that a ResourceSourceIndex exists
174 * and is followed by a (required) null terminated string. The string length
175 * (including the null terminator) is the resource length minus the minimum
176 * length, minus one byte for the ResourceSourceIndex itself.
178 if (ResourceLength > MinimumAmlResourceLength)
180 /* Compute the length of the optional string */
182 StringLength = ResourceLength - MinimumAmlResourceLength - 1;
186 * Round the length up to a multiple of the native word in order to
187 * guarantee that the entire resource descriptor is native word aligned
189 return ((UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (StringLength));
193 /*******************************************************************************
195 * FUNCTION: AcpiRsGetAmlLength
197 * PARAMETERS: Resource - Pointer to the resource linked list
198 * ResourceListSize - Size of the resource linked list
199 * SizeNeeded - Where the required size is returned
203 * DESCRIPTION: Takes a linked list of internal resource descriptors and
204 * calculates the size buffer needed to hold the corresponding
205 * external resource byte stream.
207 ******************************************************************************/
211 ACPI_RESOURCE *Resource,
212 ACPI_SIZE ResourceListSize,
213 ACPI_SIZE *SizeNeeded)
215 ACPI_SIZE AmlSizeNeeded = 0;
216 ACPI_RESOURCE *ResourceEnd;
217 ACPI_RS_LENGTH TotalSize;
220 ACPI_FUNCTION_TRACE (RsGetAmlLength);
223 /* Traverse entire list of internal resource descriptors */
225 ResourceEnd = ACPI_ADD_PTR (ACPI_RESOURCE, Resource, ResourceListSize);
226 while (Resource < ResourceEnd)
228 /* Validate the descriptor type */
230 if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
232 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
235 /* Sanity check the length. It must not be zero, or we loop forever */
237 if (!Resource->Length)
239 return_ACPI_STATUS (AE_AML_BAD_RESOURCE_LENGTH);
242 /* Get the base size of the (external stream) resource descriptor */
244 TotalSize = AcpiGbl_AmlResourceSizes [Resource->Type];
247 * Augment the base size for descriptors with optional and/or
248 * variable-length fields
250 switch (Resource->Type)
252 case ACPI_RESOURCE_TYPE_IRQ:
254 /* Length can be 3 or 2 */
256 if (Resource->Data.Irq.DescriptorLength == 2)
263 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
265 /* Length can be 1 or 0 */
267 if (Resource->Data.Irq.DescriptorLength == 0)
274 case ACPI_RESOURCE_TYPE_VENDOR:
276 * Vendor Defined Resource:
277 * For a Vendor Specific resource, if the Length is between 1 and 7
278 * it will be created as a Small Resource data type, otherwise it
279 * is a Large Resource data type.
281 if (Resource->Data.Vendor.ByteLength > 7)
283 /* Base size of a Large resource descriptor */
285 TotalSize = sizeof (AML_RESOURCE_LARGE_HEADER);
288 /* Add the size of the vendor-specific data */
290 TotalSize = (ACPI_RS_LENGTH)
291 (TotalSize + Resource->Data.Vendor.ByteLength);
295 case ACPI_RESOURCE_TYPE_END_TAG:
298 * We are done -- return the accumulated total size.
300 *SizeNeeded = AmlSizeNeeded + TotalSize;
304 return_ACPI_STATUS (AE_OK);
307 case ACPI_RESOURCE_TYPE_ADDRESS16:
309 * 16-Bit Address Resource:
310 * Add the size of the optional ResourceSource info
312 TotalSize = (ACPI_RS_LENGTH)
313 (TotalSize + AcpiRsStructOptionLength (
314 &Resource->Data.Address16.ResourceSource));
318 case ACPI_RESOURCE_TYPE_ADDRESS32:
320 * 32-Bit Address Resource:
321 * Add the size of the optional ResourceSource info
323 TotalSize = (ACPI_RS_LENGTH)
324 (TotalSize + AcpiRsStructOptionLength (
325 &Resource->Data.Address32.ResourceSource));
329 case ACPI_RESOURCE_TYPE_ADDRESS64:
331 * 64-Bit Address Resource:
332 * Add the size of the optional ResourceSource info
334 TotalSize = (ACPI_RS_LENGTH)
335 (TotalSize + AcpiRsStructOptionLength (
336 &Resource->Data.Address64.ResourceSource));
340 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
342 * Extended IRQ Resource:
343 * Add the size of each additional optional interrupt beyond the
344 * required 1 (4 bytes for each UINT32 interrupt number)
346 TotalSize = (ACPI_RS_LENGTH)
348 ((Resource->Data.ExtendedIrq.InterruptCount - 1) * 4) +
350 /* Add the size of the optional ResourceSource info */
352 AcpiRsStructOptionLength (
353 &Resource->Data.ExtendedIrq.ResourceSource));
357 case ACPI_RESOURCE_TYPE_GPIO:
359 TotalSize = (ACPI_RS_LENGTH) (TotalSize + (Resource->Data.Gpio.PinTableLength * 2) +
360 Resource->Data.Gpio.ResourceSource.StringLength +
361 Resource->Data.Gpio.VendorLength);
366 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
368 TotalSize = AcpiGbl_AmlResourceSerialBusSizes [Resource->Data.CommonSerialBus.Type];
370 TotalSize = (ACPI_RS_LENGTH) (TotalSize +
371 Resource->Data.I2cSerialBus.ResourceSource.StringLength +
372 Resource->Data.I2cSerialBus.VendorLength);
381 /* Update the total */
383 AmlSizeNeeded += TotalSize;
385 /* Point to the next object */
387 Resource = ACPI_ADD_PTR (ACPI_RESOURCE, Resource, Resource->Length);
390 /* Did not find an EndTag resource descriptor */
392 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
396 /*******************************************************************************
398 * FUNCTION: AcpiRsGetListLength
400 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
401 * AmlBufferLength - Size of AmlBuffer
402 * SizeNeeded - Where the size needed is returned
406 * DESCRIPTION: Takes an external resource byte stream and calculates the size
407 * buffer needed to hold the corresponding internal resource
408 * descriptor linked list.
410 ******************************************************************************/
413 AcpiRsGetListLength (
415 UINT32 AmlBufferLength,
416 ACPI_SIZE *SizeNeeded)
423 UINT16 ResourceLength;
424 UINT32 ExtraStructBytes;
426 UINT8 MinimumAmlResourceLength;
427 AML_RESOURCE *AmlResource;
430 ACPI_FUNCTION_TRACE (RsGetListLength);
433 *SizeNeeded = ACPI_RS_SIZE_MIN; /* Minimum size is one EndTag */
434 EndAml = AmlBuffer + AmlBufferLength;
436 /* Walk the list of AML resource descriptors */
438 while (AmlBuffer < EndAml)
440 /* Validate the Resource Type and Resource Length */
442 Status = AcpiUtValidateResource (NULL, AmlBuffer, &ResourceIndex);
443 if (ACPI_FAILURE (Status))
446 * Exit on failure. Cannot continue because the descriptor length
449 return_ACPI_STATUS (Status);
452 AmlResource = (void *) AmlBuffer;
454 /* Get the resource length and base (minimum) AML size */
456 ResourceLength = AcpiUtGetResourceLength (AmlBuffer);
457 MinimumAmlResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
460 * Augment the size for descriptors with optional
461 * and/or variable length fields
463 ExtraStructBytes = 0;
464 Buffer = AmlBuffer + AcpiUtGetResourceHeaderLength (AmlBuffer);
466 switch (AcpiUtGetResourceType (AmlBuffer))
468 case ACPI_RESOURCE_NAME_IRQ:
471 * Get the number of bits set in the 16-bit IRQ mask
473 ACPI_MOVE_16_TO_16 (&Temp16, Buffer);
474 ExtraStructBytes = AcpiRsCountSetBits (Temp16);
478 case ACPI_RESOURCE_NAME_DMA:
481 * Get the number of bits set in the 8-bit DMA mask
483 ExtraStructBytes = AcpiRsCountSetBits (*Buffer);
487 case ACPI_RESOURCE_NAME_VENDOR_SMALL:
488 case ACPI_RESOURCE_NAME_VENDOR_LARGE:
491 * Get the number of vendor data bytes
493 ExtraStructBytes = ResourceLength;
496 * There is already one byte included in the minimum
497 * descriptor size. If there are extra struct bytes,
498 * subtract one from the count.
500 if (ExtraStructBytes)
507 case ACPI_RESOURCE_NAME_END_TAG:
509 * End Tag: This is the normal exit
511 return_ACPI_STATUS (AE_OK);
514 case ACPI_RESOURCE_NAME_ADDRESS32:
515 case ACPI_RESOURCE_NAME_ADDRESS16:
516 case ACPI_RESOURCE_NAME_ADDRESS64:
519 * Add the size of the optional ResourceSource
521 ExtraStructBytes = AcpiRsStreamOptionLength (
522 ResourceLength, MinimumAmlResourceLength);
526 case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
528 * Extended IRQ Resource:
529 * Using the InterruptTableLength, add 4 bytes for each additional
530 * interrupt. Note: at least one interrupt is required and is
531 * included in the minimum descriptor size (reason for the -1)
533 ExtraStructBytes = (Buffer[1] - 1) * sizeof (UINT32);
535 /* Add the size of the optional ResourceSource */
537 ExtraStructBytes += AcpiRsStreamOptionLength (
538 ResourceLength - ExtraStructBytes, MinimumAmlResourceLength);
541 case ACPI_RESOURCE_NAME_GPIO:
543 /* Vendor data is optional */
545 if (AmlResource->Gpio.VendorLength)
547 ExtraStructBytes += AmlResource->Gpio.VendorOffset -
548 AmlResource->Gpio.PinTableOffset + AmlResource->Gpio.VendorLength;
552 ExtraStructBytes += AmlResource->LargeHeader.ResourceLength +
553 sizeof (AML_RESOURCE_LARGE_HEADER) -
554 AmlResource->Gpio.PinTableOffset;
558 case ACPI_RESOURCE_NAME_SERIAL_BUS:
560 MinimumAmlResourceLength = AcpiGbl_ResourceAmlSerialBusSizes[
561 AmlResource->CommonSerialBus.Type];
562 ExtraStructBytes += AmlResource->CommonSerialBus.ResourceLength -
563 MinimumAmlResourceLength;
572 * Update the required buffer size for the internal descriptor structs
574 * Important: Round the size up for the appropriate alignment. This
575 * is a requirement on IA64.
577 if (AcpiUtGetResourceType (AmlBuffer) == ACPI_RESOURCE_NAME_SERIAL_BUS)
579 BufferSize = AcpiGbl_ResourceStructSerialBusSizes[
580 AmlResource->CommonSerialBus.Type] + ExtraStructBytes;
584 BufferSize = AcpiGbl_ResourceStructSizes[ResourceIndex] +
587 BufferSize = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (BufferSize);
589 *SizeNeeded += BufferSize;
591 ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,
592 "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
593 AcpiUtGetResourceType (AmlBuffer),
594 AcpiUtGetDescriptorLength (AmlBuffer), BufferSize));
597 * Point to the next resource within the AML stream using the length
598 * contained in the resource descriptor header
600 AmlBuffer += AcpiUtGetDescriptorLength (AmlBuffer);
603 /* Did not find an EndTag resource descriptor */
605 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
609 /*******************************************************************************
611 * FUNCTION: AcpiRsGetPciRoutingTableLength
613 * PARAMETERS: PackageObject - Pointer to the package object
614 * BufferSizeNeeded - UINT32 pointer of the size buffer
615 * needed to properly return the
620 * DESCRIPTION: Given a package representing a PCI routing table, this
621 * calculates the size of the corresponding linked list of
624 ******************************************************************************/
627 AcpiRsGetPciRoutingTableLength (
628 ACPI_OPERAND_OBJECT *PackageObject,
629 ACPI_SIZE *BufferSizeNeeded)
631 UINT32 NumberOfElements;
632 ACPI_SIZE TempSizeNeeded = 0;
633 ACPI_OPERAND_OBJECT **TopObjectList;
635 ACPI_OPERAND_OBJECT *PackageElement;
636 ACPI_OPERAND_OBJECT **SubObjectList;
641 ACPI_FUNCTION_TRACE (RsGetPciRoutingTableLength);
644 NumberOfElements = PackageObject->Package.Count;
647 * Calculate the size of the return buffer.
648 * The base size is the number of elements * the sizes of the
649 * structures. Additional space for the strings is added below.
650 * The minus one is to subtract the size of the UINT8 Source[1]
651 * member because it is added below.
653 * But each PRT_ENTRY structure has a pointer to a string and
654 * the size of that string must be found.
656 TopObjectList = PackageObject->Package.Elements;
658 for (Index = 0; Index < NumberOfElements; Index++)
660 /* Dereference the subpackage */
662 PackageElement = *TopObjectList;
664 /* We must have a valid Package object */
666 if (!PackageElement ||
667 (PackageElement->Common.Type != ACPI_TYPE_PACKAGE))
669 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
673 * The SubObjectList will now point to an array of the
674 * four IRQ elements: Address, Pin, Source and SourceIndex
676 SubObjectList = PackageElement->Package.Elements;
678 /* Scan the IrqTableElements for the Source Name String */
683 TableIndex < PackageElement->Package.Count && !NameFound;
686 if (*SubObjectList && /* Null object allowed */
688 ((ACPI_TYPE_STRING ==
689 (*SubObjectList)->Common.Type) ||
691 ((ACPI_TYPE_LOCAL_REFERENCE ==
692 (*SubObjectList)->Common.Type) &&
694 ((*SubObjectList)->Reference.Class ==
695 ACPI_REFCLASS_NAME))))
701 /* Look at the next element */
707 TempSizeNeeded += (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
709 /* Was a String type found? */
713 if ((*SubObjectList)->Common.Type == ACPI_TYPE_STRING)
716 * The length String.Length field does not include the
717 * terminating NULL, add 1
719 TempSizeNeeded += ((ACPI_SIZE)
720 (*SubObjectList)->String.Length + 1);
724 TempSizeNeeded += AcpiNsGetPathnameLength (
725 (*SubObjectList)->Reference.Node);
731 * If no name was found, then this is a NULL, which is
732 * translated as a UINT32 zero.
734 TempSizeNeeded += sizeof (UINT32);
737 /* Round up the size since each element must be aligned */
739 TempSizeNeeded = ACPI_ROUND_UP_TO_64BIT (TempSizeNeeded);
741 /* Point to the next ACPI_OPERAND_OBJECT */
747 * Add an extra element to the end of the list, essentially a
750 *BufferSizeNeeded = TempSizeNeeded + sizeof (ACPI_PCI_ROUTING_TABLE);
751 return_ACPI_STATUS (AE_OK);