1 /*******************************************************************************
3 * Module Name: rscalc - Calculate stream and list lengths
6 ******************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
119 #include <contrib/dev/acpica/acpi.h>
120 #include <contrib/dev/acpica/acresrc.h>
121 #include <contrib/dev/acpica/amlcode.h>
122 #include <contrib/dev/acpica/acnamesp.h>
124 #define _COMPONENT ACPI_RESOURCES
125 ACPI_MODULE_NAME ("rscalc")
128 /* Local prototypes */
134 static ACPI_RS_LENGTH
135 AcpiRsStructOptionLength (
136 ACPI_RESOURCE_SOURCE *ResourceSource);
139 AcpiRsStreamOptionLength (
140 UINT32 ResourceLength,
141 UINT32 MinimumTotalLength);
144 /*******************************************************************************
146 * FUNCTION: AcpiRsCountSetBits
148 * PARAMETERS: BitField - Field in which to count bits
150 * RETURN: Number of bits set within the field
152 * DESCRIPTION: Count the number of bits set in a resource field. Used for
153 * (Short descriptor) interrupt and DMA lists.
155 ******************************************************************************/
164 ACPI_FUNCTION_ENTRY ();
167 for (BitsSet = 0; BitField; BitsSet++)
169 /* Zero the least significant bit that is set */
171 BitField &= (BitField - 1);
178 /*******************************************************************************
180 * FUNCTION: AcpiRsStructOptionLength
182 * PARAMETERS: ResourceSource - Pointer to optional descriptor field
186 * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
187 * ResourceSource fields in some Large descriptors. Used during
188 * list-to-stream conversion
190 ******************************************************************************/
192 static ACPI_RS_LENGTH
193 AcpiRsStructOptionLength (
194 ACPI_RESOURCE_SOURCE *ResourceSource)
196 ACPI_FUNCTION_ENTRY ();
200 * If the ResourceSource string is valid, return the size of the string
201 * (StringLength includes the NULL terminator) plus the size of the
202 * ResourceSourceIndex (1).
204 if (ResourceSource->StringPtr)
206 return ((ACPI_RS_LENGTH) (ResourceSource->StringLength + 1));
213 /*******************************************************************************
215 * FUNCTION: AcpiRsStreamOptionLength
217 * PARAMETERS: ResourceLength - Length from the resource header
218 * MinimumTotalLength - Minimum length of this resource, before
219 * any optional fields. Includes header size
221 * RETURN: Length of optional string (0 if no string present)
223 * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
224 * ResourceSource fields in some Large descriptors. Used during
225 * stream-to-list conversion
227 ******************************************************************************/
230 AcpiRsStreamOptionLength (
231 UINT32 ResourceLength,
232 UINT32 MinimumAmlResourceLength)
234 UINT32 StringLength = 0;
237 ACPI_FUNCTION_ENTRY ();
241 * The ResourceSourceIndex and ResourceSource are optional elements of some
242 * Large-type resource descriptors.
246 * If the length of the actual resource descriptor is greater than the ACPI
247 * spec-defined minimum length, it means that a ResourceSourceIndex exists
248 * and is followed by a (required) null terminated string. The string length
249 * (including the null terminator) is the resource length minus the minimum
250 * length, minus one byte for the ResourceSourceIndex itself.
252 if (ResourceLength > MinimumAmlResourceLength)
254 /* Compute the length of the optional string */
256 StringLength = ResourceLength - MinimumAmlResourceLength - 1;
259 /* Round up length to 32 bits for internal structure alignment */
261 return (ACPI_ROUND_UP_TO_32BITS (StringLength));
265 /*******************************************************************************
267 * FUNCTION: AcpiRsGetAmlLength
269 * PARAMETERS: Resource - Pointer to the resource linked list
270 * SizeNeeded - Where the required size is returned
274 * DESCRIPTION: Takes a linked list of internal resource descriptors and
275 * calculates the size buffer needed to hold the corresponding
276 * external resource byte stream.
278 ******************************************************************************/
282 ACPI_RESOURCE *Resource,
283 ACPI_SIZE *SizeNeeded)
285 ACPI_SIZE AmlSizeNeeded = 0;
286 ACPI_RS_LENGTH TotalSize;
289 ACPI_FUNCTION_TRACE ("RsGetAmlLength");
292 /* Traverse entire list of internal resource descriptors */
296 /* Validate the descriptor type */
298 if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
300 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
303 /* Get the base size of the (external stream) resource descriptor */
305 TotalSize = AcpiGbl_AmlResourceSizes [Resource->Type];
308 * Augment the base size for descriptors with optional and/or
309 * variable-length fields
311 switch (Resource->Type)
313 case ACPI_RESOURCE_TYPE_VENDOR:
315 * Vendor Defined Resource:
316 * For a Vendor Specific resource, if the Length is between 1 and 7
317 * it will be created as a Small Resource data type, otherwise it
318 * is a Large Resource data type.
320 if (Resource->Data.Vendor.ByteLength > 7)
322 /* Base size of a Large resource descriptor */
324 TotalSize = sizeof (AML_RESOURCE_LARGE_HEADER);
327 /* Add the size of the vendor-specific data */
329 TotalSize = (ACPI_RS_LENGTH)
330 (TotalSize + Resource->Data.Vendor.ByteLength);
334 case ACPI_RESOURCE_TYPE_END_TAG:
337 * We are done -- return the accumulated total size.
339 *SizeNeeded = AmlSizeNeeded + TotalSize;
343 return_ACPI_STATUS (AE_OK);
346 case ACPI_RESOURCE_TYPE_ADDRESS16:
348 * 16-Bit Address Resource:
349 * Add the size of the optional ResourceSource info
351 TotalSize = (ACPI_RS_LENGTH)
352 (TotalSize + AcpiRsStructOptionLength (
353 &Resource->Data.Address16.ResourceSource));
357 case ACPI_RESOURCE_TYPE_ADDRESS32:
359 * 32-Bit Address Resource:
360 * Add the size of the optional ResourceSource info
362 TotalSize = (ACPI_RS_LENGTH)
363 (TotalSize + AcpiRsStructOptionLength (
364 &Resource->Data.Address32.ResourceSource));
368 case ACPI_RESOURCE_TYPE_ADDRESS64:
370 * 64-Bit Address Resource:
371 * Add the size of the optional ResourceSource info
373 TotalSize = (ACPI_RS_LENGTH)
374 (TotalSize + AcpiRsStructOptionLength (
375 &Resource->Data.Address64.ResourceSource));
379 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
381 * Extended IRQ Resource:
382 * Add the size of each additional optional interrupt beyond the
383 * required 1 (4 bytes for each UINT32 interrupt number)
385 TotalSize = (ACPI_RS_LENGTH)
387 ((Resource->Data.ExtendedIrq.InterruptCount - 1) * 4) +
389 /* Add the size of the optional ResourceSource info */
391 AcpiRsStructOptionLength (
392 &Resource->Data.ExtendedIrq.ResourceSource));
400 /* Update the total */
402 AmlSizeNeeded += TotalSize;
404 /* Point to the next object */
406 Resource = ACPI_PTR_ADD (ACPI_RESOURCE,
407 Resource, Resource->Length);
410 /* Did not find an END_TAG descriptor */
412 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
416 /*******************************************************************************
418 * FUNCTION: AcpiRsGetListLength
420 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
421 * AmlBufferLength - Size of AmlBuffer
422 * SizeNeeded - Where the size needed is returned
426 * DESCRIPTION: Takes an external resource byte stream and calculates the size
427 * buffer needed to hold the corresponding internal resource
428 * descriptor linked list.
430 ******************************************************************************/
433 AcpiRsGetListLength (
435 UINT32 AmlBufferLength,
436 ACPI_SIZE *SizeNeeded)
439 ACPI_RESOURCE_INFO *ResourceInfo;
440 UINT32 BufferSize = 0;
441 UINT32 BytesParsed = 0;
444 UINT16 ResourceLength;
446 UINT32 ExtraStructBytes;
449 ACPI_FUNCTION_TRACE ("RsGetListLength");
452 while (BytesParsed < AmlBufferLength)
454 /* The next byte in the stream is the resource descriptor type */
456 ResourceType = AcpiUtGetResourceType (AmlBuffer);
458 /* Get the base stream size and structure sizes for the descriptor */
460 ResourceInfo = AcpiRsGetResourceInfo (ResourceType);
463 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
466 /* Get the Length field from the input resource descriptor */
468 ResourceLength = AcpiUtGetResourceLength (AmlBuffer);
470 /* Augment the size for descriptors with optional fields */
472 ExtraStructBytes = 0;
474 if (!(ResourceType & ACPI_RESOURCE_NAME_LARGE))
477 * Small resource descriptors
479 HeaderLength = sizeof (AML_RESOURCE_SMALL_HEADER);
480 Buffer = AmlBuffer + HeaderLength;
482 switch (ResourceType)
484 case ACPI_RESOURCE_NAME_IRQ:
487 * Get the number of bits set in the IRQ word
489 ACPI_MOVE_16_TO_16 (&Temp16, Buffer);
490 ExtraStructBytes = AcpiRsCountSetBits (Temp16);
494 case ACPI_RESOURCE_NAME_DMA:
497 * Get the number of bits set in the DMA channels byte
499 ExtraStructBytes = AcpiRsCountSetBits (*Buffer);
503 case ACPI_RESOURCE_NAME_VENDOR_SMALL:
505 * Vendor Specific Resource:
506 * Ensure a 32-bit boundary for the structure
508 ExtraStructBytes = ACPI_ROUND_UP_TO_32BITS (ResourceLength);
512 case ACPI_RESOURCE_NAME_END_TAG:
514 * End Tag: This is the normal exit, add size of EndTag
516 *SizeNeeded = BufferSize + ACPI_RS_SIZE_MIN;
517 return_ACPI_STATUS (AE_OK);
526 * Large resource descriptors
528 HeaderLength = sizeof (AML_RESOURCE_LARGE_HEADER);
529 Buffer = AmlBuffer + HeaderLength;
531 switch (ResourceType)
533 case ACPI_RESOURCE_NAME_VENDOR_LARGE:
535 * Vendor Defined Resource:
536 * Add vendor data and ensure a 32-bit boundary for the structure
538 ExtraStructBytes = ACPI_ROUND_UP_TO_32BITS (ResourceLength);
542 case ACPI_RESOURCE_NAME_ADDRESS32:
543 case ACPI_RESOURCE_NAME_ADDRESS16:
545 * 32-Bit or 16-bit Address Resource:
546 * Add the size of any optional data (ResourceSource)
548 ExtraStructBytes = AcpiRsStreamOptionLength (ResourceLength,
549 ResourceInfo->MinimumAmlResourceLength);
553 case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
556 * Point past the InterruptVectorFlags to get the
557 * InterruptTableLength.
562 * Add 4 bytes for each additional interrupt. Note: at least one
563 * interrupt is required and is included in the minimum
566 ExtraStructBytes = ((*Buffer - 1) * sizeof (UINT32));
568 /* Add the size of any optional data (ResourceSource) */
570 ExtraStructBytes += AcpiRsStreamOptionLength (
571 ResourceLength - ExtraStructBytes,
572 ResourceInfo->MinimumAmlResourceLength);
576 case ACPI_RESOURCE_NAME_ADDRESS64:
578 * 64-Bit Address Resource:
579 * Add the size of any optional data (ResourceSource)
580 * Ensure a 64-bit boundary for the structure
582 ExtraStructBytes = ACPI_ROUND_UP_TO_64BITS (
583 AcpiRsStreamOptionLength (ResourceLength,
584 ResourceInfo->MinimumAmlResourceLength));
593 /* Update the required buffer size for the internal descriptor structs */
595 Temp16 = (UINT16) (ResourceInfo->MinimumInternalStructLength + ExtraStructBytes);
596 BufferSize += (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (Temp16);
599 * Update byte count and point to the next resource within the stream
600 * using the size of the header plus the length contained in the header
602 Temp16 = (UINT16) (HeaderLength + ResourceLength);
603 BytesParsed += Temp16;
607 /* This is the data the caller needs */
609 *SizeNeeded = BufferSize;
610 return_ACPI_STATUS (AE_OK);
614 /*******************************************************************************
616 * FUNCTION: AcpiRsGetPciRoutingTableLength
618 * PARAMETERS: PackageObject - Pointer to the package object
619 * BufferSizeNeeded - UINT32 pointer of the size buffer
620 * needed to properly return the
625 * DESCRIPTION: Given a package representing a PCI routing table, this
626 * calculates the size of the corresponding linked list of
629 ******************************************************************************/
632 AcpiRsGetPciRoutingTableLength (
633 ACPI_OPERAND_OBJECT *PackageObject,
634 ACPI_SIZE *BufferSizeNeeded)
636 UINT32 NumberOfElements;
637 ACPI_SIZE TempSizeNeeded = 0;
638 ACPI_OPERAND_OBJECT **TopObjectList;
640 ACPI_OPERAND_OBJECT *PackageElement;
641 ACPI_OPERAND_OBJECT **SubObjectList;
646 ACPI_FUNCTION_TRACE ("RsGetPciRoutingTableLength");
649 NumberOfElements = PackageObject->Package.Count;
652 * Calculate the size of the return buffer.
653 * The base size is the number of elements * the sizes of the
654 * structures. Additional space for the strings is added below.
655 * The minus one is to subtract the size of the UINT8 Source[1]
656 * member because it is added below.
658 * But each PRT_ENTRY structure has a pointer to a string and
659 * the size of that string must be found.
661 TopObjectList = PackageObject->Package.Elements;
663 for (Index = 0; Index < NumberOfElements; Index++)
665 /* Dereference the sub-package */
667 PackageElement = *TopObjectList;
670 * The SubObjectList will now point to an array of the
671 * four IRQ elements: Address, Pin, Source and SourceIndex
673 SubObjectList = PackageElement->Package.Elements;
675 /* Scan the IrqTableElements for the Source Name String */
679 for (TableIndex = 0; TableIndex < 4 && !NameFound; TableIndex++)
681 if ((ACPI_TYPE_STRING ==
682 ACPI_GET_OBJECT_TYPE (*SubObjectList)) ||
684 ((ACPI_TYPE_LOCAL_REFERENCE ==
685 ACPI_GET_OBJECT_TYPE (*SubObjectList)) &&
687 ((*SubObjectList)->Reference.Opcode ==
688 AML_INT_NAMEPATH_OP)))
694 /* Look at the next element */
700 TempSizeNeeded += (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
702 /* Was a String type found? */
706 if (ACPI_GET_OBJECT_TYPE (*SubObjectList) == ACPI_TYPE_STRING)
709 * The length String.Length field does not include the
710 * terminating NULL, add 1
712 TempSizeNeeded += ((ACPI_SIZE)
713 (*SubObjectList)->String.Length + 1);
717 TempSizeNeeded += AcpiNsGetPathnameLength (
718 (*SubObjectList)->Reference.Node);
724 * If no name was found, then this is a NULL, which is
725 * translated as a UINT32 zero.
727 TempSizeNeeded += sizeof (UINT32);
730 /* Round up the size since each element must be aligned */
732 TempSizeNeeded = ACPI_ROUND_UP_TO_64BITS (TempSizeNeeded);
734 /* Point to the next ACPI_OPERAND_OBJECT */
740 * Adding an extra element to the end of the list, essentially a
743 *BufferSizeNeeded = TempSizeNeeded + sizeof (ACPI_PCI_ROUTING_TABLE);
744 return_ACPI_STATUS (AE_OK);