1 /*******************************************************************************
3 * Module Name: rscalc - Calculate stream and list lengths
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2011, 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.
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acresrc.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
52 #define _COMPONENT ACPI_RESOURCES
53 ACPI_MODULE_NAME ("rscalc")
56 /* Local prototypes */
63 AcpiRsStructOptionLength (
64 ACPI_RESOURCE_SOURCE *ResourceSource);
67 AcpiRsStreamOptionLength (
68 UINT32 ResourceLength,
69 UINT32 MinimumTotalLength);
72 /*******************************************************************************
74 * FUNCTION: AcpiRsCountSetBits
76 * PARAMETERS: BitField - Field in which to count bits
78 * RETURN: Number of bits set within the field
80 * DESCRIPTION: Count the number of bits set in a resource field. Used for
81 * (Short descriptor) interrupt and DMA lists.
83 ******************************************************************************/
92 ACPI_FUNCTION_ENTRY ();
95 for (BitsSet = 0; BitField; BitsSet++)
97 /* Zero the least significant bit that is set */
99 BitField &= (UINT16) (BitField - 1);
106 /*******************************************************************************
108 * FUNCTION: AcpiRsStructOptionLength
110 * PARAMETERS: ResourceSource - Pointer to optional descriptor field
114 * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
115 * ResourceSource fields in some Large descriptors. Used during
116 * list-to-stream conversion
118 ******************************************************************************/
120 static ACPI_RS_LENGTH
121 AcpiRsStructOptionLength (
122 ACPI_RESOURCE_SOURCE *ResourceSource)
124 ACPI_FUNCTION_ENTRY ();
128 * If the ResourceSource string is valid, return the size of the string
129 * (StringLength includes the NULL terminator) plus the size of the
130 * ResourceSourceIndex (1).
132 if (ResourceSource->StringPtr)
134 return ((ACPI_RS_LENGTH) (ResourceSource->StringLength + 1));
141 /*******************************************************************************
143 * FUNCTION: AcpiRsStreamOptionLength
145 * PARAMETERS: ResourceLength - Length from the resource header
146 * MinimumTotalLength - Minimum length of this resource, before
147 * any optional fields. Includes header size
149 * RETURN: Length of optional string (0 if no string present)
151 * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
152 * ResourceSource fields in some Large descriptors. Used during
153 * stream-to-list conversion
155 ******************************************************************************/
158 AcpiRsStreamOptionLength (
159 UINT32 ResourceLength,
160 UINT32 MinimumAmlResourceLength)
162 UINT32 StringLength = 0;
165 ACPI_FUNCTION_ENTRY ();
169 * The ResourceSourceIndex and ResourceSource are optional elements of some
170 * Large-type resource descriptors.
174 * If the length of the actual resource descriptor is greater than the ACPI
175 * spec-defined minimum length, it means that a ResourceSourceIndex exists
176 * and is followed by a (required) null terminated string. The string length
177 * (including the null terminator) is the resource length minus the minimum
178 * length, minus one byte for the ResourceSourceIndex itself.
180 if (ResourceLength > MinimumAmlResourceLength)
182 /* Compute the length of the optional string */
184 StringLength = ResourceLength - MinimumAmlResourceLength - 1;
188 * Round the length up to a multiple of the native word in order to
189 * guarantee that the entire resource descriptor is native word aligned
191 return ((UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (StringLength));
195 /*******************************************************************************
197 * FUNCTION: AcpiRsGetAmlLength
199 * PARAMETERS: Resource - Pointer to the resource linked list
200 * SizeNeeded - Where the required size is returned
204 * DESCRIPTION: Takes a linked list of internal resource descriptors and
205 * calculates the size buffer needed to hold the corresponding
206 * external resource byte stream.
208 ******************************************************************************/
212 ACPI_RESOURCE *Resource,
213 ACPI_SIZE *SizeNeeded)
215 ACPI_SIZE AmlSizeNeeded = 0;
216 ACPI_RS_LENGTH TotalSize;
219 ACPI_FUNCTION_TRACE (RsGetAmlLength);
222 /* Traverse entire list of internal resource descriptors */
226 /* Validate the descriptor type */
228 if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
230 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
233 /* Get the base size of the (external stream) resource descriptor */
235 TotalSize = AcpiGbl_AmlResourceSizes [Resource->Type];
238 * Augment the base size for descriptors with optional and/or
239 * variable-length fields
241 switch (Resource->Type)
243 case ACPI_RESOURCE_TYPE_IRQ:
245 /* Length can be 3 or 2 */
247 if (Resource->Data.Irq.DescriptorLength == 2)
254 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
256 /* Length can be 1 or 0 */
258 if (Resource->Data.Irq.DescriptorLength == 0)
265 case ACPI_RESOURCE_TYPE_VENDOR:
267 * Vendor Defined Resource:
268 * For a Vendor Specific resource, if the Length is between 1 and 7
269 * it will be created as a Small Resource data type, otherwise it
270 * is a Large Resource data type.
272 if (Resource->Data.Vendor.ByteLength > 7)
274 /* Base size of a Large resource descriptor */
276 TotalSize = sizeof (AML_RESOURCE_LARGE_HEADER);
279 /* Add the size of the vendor-specific data */
281 TotalSize = (ACPI_RS_LENGTH)
282 (TotalSize + Resource->Data.Vendor.ByteLength);
286 case ACPI_RESOURCE_TYPE_END_TAG:
289 * We are done -- return the accumulated total size.
291 *SizeNeeded = AmlSizeNeeded + TotalSize;
295 return_ACPI_STATUS (AE_OK);
298 case ACPI_RESOURCE_TYPE_ADDRESS16:
300 * 16-Bit Address Resource:
301 * Add the size of the optional ResourceSource info
303 TotalSize = (ACPI_RS_LENGTH)
304 (TotalSize + AcpiRsStructOptionLength (
305 &Resource->Data.Address16.ResourceSource));
309 case ACPI_RESOURCE_TYPE_ADDRESS32:
311 * 32-Bit Address Resource:
312 * Add the size of the optional ResourceSource info
314 TotalSize = (ACPI_RS_LENGTH)
315 (TotalSize + AcpiRsStructOptionLength (
316 &Resource->Data.Address32.ResourceSource));
320 case ACPI_RESOURCE_TYPE_ADDRESS64:
322 * 64-Bit Address Resource:
323 * Add the size of the optional ResourceSource info
325 TotalSize = (ACPI_RS_LENGTH)
326 (TotalSize + AcpiRsStructOptionLength (
327 &Resource->Data.Address64.ResourceSource));
331 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
333 * Extended IRQ Resource:
334 * Add the size of each additional optional interrupt beyond the
335 * required 1 (4 bytes for each UINT32 interrupt number)
337 TotalSize = (ACPI_RS_LENGTH)
339 ((Resource->Data.ExtendedIrq.InterruptCount - 1) * 4) +
341 /* Add the size of the optional ResourceSource info */
343 AcpiRsStructOptionLength (
344 &Resource->Data.ExtendedIrq.ResourceSource));
352 /* Update the total */
354 AmlSizeNeeded += TotalSize;
356 /* Point to the next object */
358 Resource = ACPI_ADD_PTR (ACPI_RESOURCE, Resource, Resource->Length);
361 /* Did not find an EndTag resource descriptor */
363 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
367 /*******************************************************************************
369 * FUNCTION: AcpiRsGetListLength
371 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
372 * AmlBufferLength - Size of AmlBuffer
373 * SizeNeeded - Where the size needed is returned
377 * DESCRIPTION: Takes an external resource byte stream and calculates the size
378 * buffer needed to hold the corresponding internal resource
379 * descriptor linked list.
381 ******************************************************************************/
384 AcpiRsGetListLength (
386 UINT32 AmlBufferLength,
387 ACPI_SIZE *SizeNeeded)
394 UINT16 ResourceLength;
395 UINT32 ExtraStructBytes;
397 UINT8 MinimumAmlResourceLength;
400 ACPI_FUNCTION_TRACE (RsGetListLength);
404 EndAml = AmlBuffer + AmlBufferLength;
406 /* Walk the list of AML resource descriptors */
408 while (AmlBuffer < EndAml)
410 /* Validate the Resource Type and Resource Length */
412 Status = AcpiUtValidateResource (AmlBuffer, &ResourceIndex);
413 if (ACPI_FAILURE (Status))
415 return_ACPI_STATUS (Status);
418 /* Get the resource length and base (minimum) AML size */
420 ResourceLength = AcpiUtGetResourceLength (AmlBuffer);
421 MinimumAmlResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
424 * Augment the size for descriptors with optional
425 * and/or variable length fields
427 ExtraStructBytes = 0;
428 Buffer = AmlBuffer + AcpiUtGetResourceHeaderLength (AmlBuffer);
430 switch (AcpiUtGetResourceType (AmlBuffer))
432 case ACPI_RESOURCE_NAME_IRQ:
435 * Get the number of bits set in the 16-bit IRQ mask
437 ACPI_MOVE_16_TO_16 (&Temp16, Buffer);
438 ExtraStructBytes = AcpiRsCountSetBits (Temp16);
442 case ACPI_RESOURCE_NAME_DMA:
445 * Get the number of bits set in the 8-bit DMA mask
447 ExtraStructBytes = AcpiRsCountSetBits (*Buffer);
451 case ACPI_RESOURCE_NAME_VENDOR_SMALL:
452 case ACPI_RESOURCE_NAME_VENDOR_LARGE:
455 * Get the number of vendor data bytes
457 ExtraStructBytes = ResourceLength;
461 case ACPI_RESOURCE_NAME_END_TAG:
464 * This is the normal exit, add size of EndTag
466 *SizeNeeded += ACPI_RS_SIZE_MIN;
467 return_ACPI_STATUS (AE_OK);
470 case ACPI_RESOURCE_NAME_ADDRESS32:
471 case ACPI_RESOURCE_NAME_ADDRESS16:
472 case ACPI_RESOURCE_NAME_ADDRESS64:
475 * Add the size of the optional ResourceSource
477 ExtraStructBytes = AcpiRsStreamOptionLength (
478 ResourceLength, MinimumAmlResourceLength);
482 case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
484 * Extended IRQ Resource:
485 * Using the InterruptTableLength, add 4 bytes for each additional
486 * interrupt. Note: at least one interrupt is required and is
487 * included in the minimum descriptor size (reason for the -1)
489 ExtraStructBytes = (Buffer[1] - 1) * sizeof (UINT32);
491 /* Add the size of the optional ResourceSource */
493 ExtraStructBytes += AcpiRsStreamOptionLength (
494 ResourceLength - ExtraStructBytes, MinimumAmlResourceLength);
503 * Update the required buffer size for the internal descriptor structs
505 * Important: Round the size up for the appropriate alignment. This
506 * is a requirement on IA64.
508 BufferSize = AcpiGbl_ResourceStructSizes[ResourceIndex] +
510 BufferSize = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (BufferSize);
512 *SizeNeeded += BufferSize;
514 ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,
515 "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
516 AcpiUtGetResourceType (AmlBuffer),
517 AcpiUtGetDescriptorLength (AmlBuffer), BufferSize));
520 * Point to the next resource within the AML stream using the length
521 * contained in the resource descriptor header
523 AmlBuffer += AcpiUtGetDescriptorLength (AmlBuffer);
526 /* Did not find an EndTag resource descriptor */
528 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
532 /*******************************************************************************
534 * FUNCTION: AcpiRsGetPciRoutingTableLength
536 * PARAMETERS: PackageObject - Pointer to the package object
537 * BufferSizeNeeded - UINT32 pointer of the size buffer
538 * needed to properly return the
543 * DESCRIPTION: Given a package representing a PCI routing table, this
544 * calculates the size of the corresponding linked list of
547 ******************************************************************************/
550 AcpiRsGetPciRoutingTableLength (
551 ACPI_OPERAND_OBJECT *PackageObject,
552 ACPI_SIZE *BufferSizeNeeded)
554 UINT32 NumberOfElements;
555 ACPI_SIZE TempSizeNeeded = 0;
556 ACPI_OPERAND_OBJECT **TopObjectList;
558 ACPI_OPERAND_OBJECT *PackageElement;
559 ACPI_OPERAND_OBJECT **SubObjectList;
564 ACPI_FUNCTION_TRACE (RsGetPciRoutingTableLength);
567 NumberOfElements = PackageObject->Package.Count;
570 * Calculate the size of the return buffer.
571 * The base size is the number of elements * the sizes of the
572 * structures. Additional space for the strings is added below.
573 * The minus one is to subtract the size of the UINT8 Source[1]
574 * member because it is added below.
576 * But each PRT_ENTRY structure has a pointer to a string and
577 * the size of that string must be found.
579 TopObjectList = PackageObject->Package.Elements;
581 for (Index = 0; Index < NumberOfElements; Index++)
583 /* Dereference the sub-package */
585 PackageElement = *TopObjectList;
587 /* We must have a valid Package object */
589 if (!PackageElement ||
590 (PackageElement->Common.Type != ACPI_TYPE_PACKAGE))
592 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
596 * The SubObjectList will now point to an array of the
597 * four IRQ elements: Address, Pin, Source and SourceIndex
599 SubObjectList = PackageElement->Package.Elements;
601 /* Scan the IrqTableElements for the Source Name String */
605 for (TableIndex = 0; TableIndex < 4 && !NameFound; TableIndex++)
607 if (*SubObjectList && /* Null object allowed */
609 ((ACPI_TYPE_STRING ==
610 (*SubObjectList)->Common.Type) ||
612 ((ACPI_TYPE_LOCAL_REFERENCE ==
613 (*SubObjectList)->Common.Type) &&
615 ((*SubObjectList)->Reference.Class ==
616 ACPI_REFCLASS_NAME))))
622 /* Look at the next element */
628 TempSizeNeeded += (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
630 /* Was a String type found? */
634 if ((*SubObjectList)->Common.Type == ACPI_TYPE_STRING)
637 * The length String.Length field does not include the
638 * terminating NULL, add 1
640 TempSizeNeeded += ((ACPI_SIZE)
641 (*SubObjectList)->String.Length + 1);
645 TempSizeNeeded += AcpiNsGetPathnameLength (
646 (*SubObjectList)->Reference.Node);
652 * If no name was found, then this is a NULL, which is
653 * translated as a UINT32 zero.
655 TempSizeNeeded += sizeof (UINT32);
658 /* Round up the size since each element must be aligned */
660 TempSizeNeeded = ACPI_ROUND_UP_TO_64BIT (TempSizeNeeded);
662 /* Point to the next ACPI_OPERAND_OBJECT */
668 * Add an extra element to the end of the list, essentially a
671 *BufferSizeNeeded = TempSizeNeeded + sizeof (ACPI_PCI_ROUTING_TABLE);
672 return_ACPI_STATUS (AE_OK);