1 /*******************************************************************************
3 * Module Name: rscalc - Calculate stream and list lengths
5 ******************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
119 #include "accommon.h"
121 #include "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
&= (UINT16
) (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;
260 * Round the length up to a multiple of the native word in order to
261 * guarantee that the entire resource descriptor is native word aligned
263 return ((UINT32
) ACPI_ROUND_UP_TO_NATIVE_WORD (StringLength
));
267 /*******************************************************************************
269 * FUNCTION: AcpiRsGetAmlLength
271 * PARAMETERS: Resource - Pointer to the resource linked list
272 * SizeNeeded - Where the required size is returned
276 * DESCRIPTION: Takes a linked list of internal resource descriptors and
277 * calculates the size buffer needed to hold the corresponding
278 * external resource byte stream.
280 ******************************************************************************/
284 ACPI_RESOURCE
*Resource
,
285 ACPI_SIZE
*SizeNeeded
)
287 ACPI_SIZE AmlSizeNeeded
= 0;
288 ACPI_RS_LENGTH TotalSize
;
291 ACPI_FUNCTION_TRACE (RsGetAmlLength
);
294 /* Traverse entire list of internal resource descriptors */
298 /* Validate the descriptor type */
300 if (Resource
->Type
> ACPI_RESOURCE_TYPE_MAX
)
302 return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE
);
305 /* Get the base size of the (external stream) resource descriptor */
307 TotalSize
= AcpiGbl_AmlResourceSizes
[Resource
->Type
];
310 * Augment the base size for descriptors with optional and/or
311 * variable-length fields
313 switch (Resource
->Type
)
315 case ACPI_RESOURCE_TYPE_IRQ
:
317 /* Length can be 3 or 2 */
319 if (Resource
->Data
.Irq
.DescriptorLength
== 2)
326 case ACPI_RESOURCE_TYPE_START_DEPENDENT
:
328 /* Length can be 1 or 0 */
330 if (Resource
->Data
.Irq
.DescriptorLength
== 0)
337 case ACPI_RESOURCE_TYPE_VENDOR
:
339 * Vendor Defined Resource:
340 * For a Vendor Specific resource, if the Length is between 1 and 7
341 * it will be created as a Small Resource data type, otherwise it
342 * is a Large Resource data type.
344 if (Resource
->Data
.Vendor
.ByteLength
> 7)
346 /* Base size of a Large resource descriptor */
348 TotalSize
= sizeof (AML_RESOURCE_LARGE_HEADER
);
351 /* Add the size of the vendor-specific data */
353 TotalSize
= (ACPI_RS_LENGTH
)
354 (TotalSize
+ Resource
->Data
.Vendor
.ByteLength
);
358 case ACPI_RESOURCE_TYPE_END_TAG
:
361 * We are done -- return the accumulated total size.
363 *SizeNeeded
= AmlSizeNeeded
+ TotalSize
;
367 return_ACPI_STATUS (AE_OK
);
370 case ACPI_RESOURCE_TYPE_ADDRESS16
:
372 * 16-Bit Address Resource:
373 * Add the size of the optional ResourceSource info
375 TotalSize
= (ACPI_RS_LENGTH
)
376 (TotalSize
+ AcpiRsStructOptionLength (
377 &Resource
->Data
.Address16
.ResourceSource
));
381 case ACPI_RESOURCE_TYPE_ADDRESS32
:
383 * 32-Bit Address Resource:
384 * Add the size of the optional ResourceSource info
386 TotalSize
= (ACPI_RS_LENGTH
)
387 (TotalSize
+ AcpiRsStructOptionLength (
388 &Resource
->Data
.Address32
.ResourceSource
));
392 case ACPI_RESOURCE_TYPE_ADDRESS64
:
394 * 64-Bit Address Resource:
395 * Add the size of the optional ResourceSource info
397 TotalSize
= (ACPI_RS_LENGTH
)
398 (TotalSize
+ AcpiRsStructOptionLength (
399 &Resource
->Data
.Address64
.ResourceSource
));
403 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ
:
405 * Extended IRQ Resource:
406 * Add the size of each additional optional interrupt beyond the
407 * required 1 (4 bytes for each UINT32 interrupt number)
409 TotalSize
= (ACPI_RS_LENGTH
)
411 ((Resource
->Data
.ExtendedIrq
.InterruptCount
- 1) * 4) +
413 /* Add the size of the optional ResourceSource info */
415 AcpiRsStructOptionLength (
416 &Resource
->Data
.ExtendedIrq
.ResourceSource
));
424 /* Update the total */
426 AmlSizeNeeded
+= TotalSize
;
428 /* Point to the next object */
430 Resource
= ACPI_ADD_PTR (ACPI_RESOURCE
, Resource
, Resource
->Length
);
433 /* Did not find an EndTag resource descriptor */
435 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG
);
439 /*******************************************************************************
441 * FUNCTION: AcpiRsGetListLength
443 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
444 * AmlBufferLength - Size of AmlBuffer
445 * SizeNeeded - Where the size needed is returned
449 * DESCRIPTION: Takes an external resource byte stream and calculates the size
450 * buffer needed to hold the corresponding internal resource
451 * descriptor linked list.
453 ******************************************************************************/
456 AcpiRsGetListLength (
458 UINT32 AmlBufferLength
,
459 ACPI_SIZE
*SizeNeeded
)
466 UINT16 ResourceLength
;
467 UINT32 ExtraStructBytes
;
469 UINT8 MinimumAmlResourceLength
;
472 ACPI_FUNCTION_TRACE (RsGetListLength
);
476 EndAml
= AmlBuffer
+ AmlBufferLength
;
478 /* Walk the list of AML resource descriptors */
480 while (AmlBuffer
< EndAml
)
482 /* Validate the Resource Type and Resource Length */
484 Status
= AcpiUtValidateResource (AmlBuffer
, &ResourceIndex
);
485 if (ACPI_FAILURE (Status
))
487 return_ACPI_STATUS (Status
);
490 /* Get the resource length and base (minimum) AML size */
492 ResourceLength
= AcpiUtGetResourceLength (AmlBuffer
);
493 MinimumAmlResourceLength
= AcpiGbl_ResourceAmlSizes
[ResourceIndex
];
496 * Augment the size for descriptors with optional
497 * and/or variable length fields
499 ExtraStructBytes
= 0;
500 Buffer
= AmlBuffer
+ AcpiUtGetResourceHeaderLength (AmlBuffer
);
502 switch (AcpiUtGetResourceType (AmlBuffer
))
504 case ACPI_RESOURCE_NAME_IRQ
:
507 * Get the number of bits set in the 16-bit IRQ mask
509 ACPI_MOVE_16_TO_16 (&Temp16
, Buffer
);
510 ExtraStructBytes
= AcpiRsCountSetBits (Temp16
);
514 case ACPI_RESOURCE_NAME_DMA
:
517 * Get the number of bits set in the 8-bit DMA mask
519 ExtraStructBytes
= AcpiRsCountSetBits (*Buffer
);
523 case ACPI_RESOURCE_NAME_VENDOR_SMALL
:
524 case ACPI_RESOURCE_NAME_VENDOR_LARGE
:
527 * Get the number of vendor data bytes
529 ExtraStructBytes
= ResourceLength
;
533 case ACPI_RESOURCE_NAME_END_TAG
:
536 * This is the normal exit, add size of EndTag
538 *SizeNeeded
+= ACPI_RS_SIZE_MIN
;
539 return_ACPI_STATUS (AE_OK
);
542 case ACPI_RESOURCE_NAME_ADDRESS32
:
543 case ACPI_RESOURCE_NAME_ADDRESS16
:
544 case ACPI_RESOURCE_NAME_ADDRESS64
:
547 * Add the size of the optional ResourceSource
549 ExtraStructBytes
= AcpiRsStreamOptionLength (
550 ResourceLength
, MinimumAmlResourceLength
);
554 case ACPI_RESOURCE_NAME_EXTENDED_IRQ
:
556 * Extended IRQ Resource:
557 * Using the InterruptTableLength, add 4 bytes for each additional
558 * interrupt. Note: at least one interrupt is required and is
559 * included in the minimum descriptor size (reason for the -1)
561 ExtraStructBytes
= (Buffer
[1] - 1) * sizeof (UINT32
);
563 /* Add the size of the optional ResourceSource */
565 ExtraStructBytes
+= AcpiRsStreamOptionLength (
566 ResourceLength
- ExtraStructBytes
, MinimumAmlResourceLength
);
575 * Update the required buffer size for the internal descriptor structs
577 * Important: Round the size up for the appropriate alignment. This
578 * is a requirement on IA64.
580 BufferSize
= AcpiGbl_ResourceStructSizes
[ResourceIndex
] +
582 BufferSize
= (UINT32
) ACPI_ROUND_UP_TO_NATIVE_WORD (BufferSize
);
584 *SizeNeeded
+= BufferSize
;
586 ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES
,
587 "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
588 AcpiUtGetResourceType (AmlBuffer
),
589 AcpiUtGetDescriptorLength (AmlBuffer
), BufferSize
));
592 * Point to the next resource within the AML stream using the length
593 * contained in the resource descriptor header
595 AmlBuffer
+= AcpiUtGetDescriptorLength (AmlBuffer
);
598 /* Did not find an EndTag resource descriptor */
600 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG
);
604 /*******************************************************************************
606 * FUNCTION: AcpiRsGetPciRoutingTableLength
608 * PARAMETERS: PackageObject - Pointer to the package object
609 * BufferSizeNeeded - UINT32 pointer of the size buffer
610 * needed to properly return the
615 * DESCRIPTION: Given a package representing a PCI routing table, this
616 * calculates the size of the corresponding linked list of
619 ******************************************************************************/
622 AcpiRsGetPciRoutingTableLength (
623 ACPI_OPERAND_OBJECT
*PackageObject
,
624 ACPI_SIZE
*BufferSizeNeeded
)
626 UINT32 NumberOfElements
;
627 ACPI_SIZE TempSizeNeeded
= 0;
628 ACPI_OPERAND_OBJECT
**TopObjectList
;
630 ACPI_OPERAND_OBJECT
*PackageElement
;
631 ACPI_OPERAND_OBJECT
**SubObjectList
;
636 ACPI_FUNCTION_TRACE (RsGetPciRoutingTableLength
);
639 NumberOfElements
= PackageObject
->Package
.Count
;
642 * Calculate the size of the return buffer.
643 * The base size is the number of elements * the sizes of the
644 * structures. Additional space for the strings is added below.
645 * The minus one is to subtract the size of the UINT8 Source[1]
646 * member because it is added below.
648 * But each PRT_ENTRY structure has a pointer to a string and
649 * the size of that string must be found.
651 TopObjectList
= PackageObject
->Package
.Elements
;
653 for (Index
= 0; Index
< NumberOfElements
; Index
++)
655 /* Dereference the sub-package */
657 PackageElement
= *TopObjectList
;
659 /* We must have a valid Package object */
661 if (!PackageElement
||
662 (PackageElement
->Common
.Type
!= ACPI_TYPE_PACKAGE
))
664 return_ACPI_STATUS (AE_AML_OPERAND_TYPE
);
668 * The SubObjectList will now point to an array of the
669 * four IRQ elements: Address, Pin, Source and SourceIndex
671 SubObjectList
= PackageElement
->Package
.Elements
;
673 /* Scan the IrqTableElements for the Source Name String */
677 for (TableIndex
= 0; TableIndex
< 4 && !NameFound
; TableIndex
++)
679 if (*SubObjectList
&& /* Null object allowed */
681 ((ACPI_TYPE_STRING
==
682 (*SubObjectList
)->Common
.Type
) ||
684 ((ACPI_TYPE_LOCAL_REFERENCE
==
685 (*SubObjectList
)->Common
.Type
) &&
687 ((*SubObjectList
)->Reference
.Class
==
688 ACPI_REFCLASS_NAME
))))
694 /* Look at the next element */
700 TempSizeNeeded
+= (sizeof (ACPI_PCI_ROUTING_TABLE
) - 4);
702 /* Was a String type found? */
706 if ((*SubObjectList
)->Common
.Type
== 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_64BIT (TempSizeNeeded
);
734 /* Point to the next ACPI_OPERAND_OBJECT */
740 * Add 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
);