xref: /freebsd/sys/contrib/dev/acpica/components/resources/rscalc.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /*******************************************************************************
2  *
3  * Module Name: rscalc - Calculate stream and list lengths
4  *
5  ******************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
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
19  * property rights.
20  *
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
27  *
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;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
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.
53  *
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
64  * make.
65  *
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
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
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.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
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
88  * PARTICULAR PURPOSE.
89  *
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
97  * LIMITED REMEDY.
98  *
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.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acresrc.h>
155 #include <contrib/dev/acpica/include/acnamesp.h>
156 
157 
158 #define _COMPONENT          ACPI_RESOURCES
159         ACPI_MODULE_NAME    ("rscalc")
160 
161 
162 /* Local prototypes */
163 
164 static UINT8
165 AcpiRsCountSetBits (
166     UINT16                  BitField);
167 
168 static ACPI_RS_LENGTH
169 AcpiRsStructOptionLength (
170     ACPI_RESOURCE_SOURCE    *ResourceSource);
171 
172 static UINT32
173 AcpiRsStreamOptionLength (
174     UINT32                  ResourceLength,
175     UINT32                  MinimumTotalLength);
176 
177 
178 /*******************************************************************************
179  *
180  * FUNCTION:    AcpiRsCountSetBits
181  *
182  * PARAMETERS:  BitField        - Field in which to count bits
183  *
184  * RETURN:      Number of bits set within the field
185  *
186  * DESCRIPTION: Count the number of bits set in a resource field. Used for
187  *              (Short descriptor) interrupt and DMA lists.
188  *
189  ******************************************************************************/
190 
191 static UINT8
192 AcpiRsCountSetBits (
193     UINT16                  BitField)
194 {
195     UINT8                   BitsSet;
196 
197 
198     ACPI_FUNCTION_ENTRY ();
199 
200 
201     for (BitsSet = 0; BitField; BitsSet++)
202     {
203         /* Zero the least significant bit that is set */
204 
205         BitField &= (UINT16) (BitField - 1);
206     }
207 
208     return (BitsSet);
209 }
210 
211 
212 /*******************************************************************************
213  *
214  * FUNCTION:    AcpiRsStructOptionLength
215  *
216  * PARAMETERS:  ResourceSource      - Pointer to optional descriptor field
217  *
218  * RETURN:      Status
219  *
220  * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
221  *              ResourceSource fields in some Large descriptors. Used during
222  *              list-to-stream conversion
223  *
224  ******************************************************************************/
225 
226 static ACPI_RS_LENGTH
227 AcpiRsStructOptionLength (
228     ACPI_RESOURCE_SOURCE    *ResourceSource)
229 {
230     ACPI_FUNCTION_ENTRY ();
231 
232 
233     /*
234      * If the ResourceSource string is valid, return the size of the string
235      * (StringLength includes the NULL terminator) plus the size of the
236      * ResourceSourceIndex (1).
237      */
238     if (ResourceSource->StringPtr)
239     {
240         return ((ACPI_RS_LENGTH) (ResourceSource->StringLength + 1));
241     }
242 
243     return (0);
244 }
245 
246 
247 /*******************************************************************************
248  *
249  * FUNCTION:    AcpiRsStreamOptionLength
250  *
251  * PARAMETERS:  ResourceLength      - Length from the resource header
252  *              MinimumTotalLength  - Minimum length of this resource, before
253  *                                    any optional fields. Includes header size
254  *
255  * RETURN:      Length of optional string (0 if no string present)
256  *
257  * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
258  *              ResourceSource fields in some Large descriptors. Used during
259  *              stream-to-list conversion
260  *
261  ******************************************************************************/
262 
263 static UINT32
264 AcpiRsStreamOptionLength (
265     UINT32                  ResourceLength,
266     UINT32                  MinimumAmlResourceLength)
267 {
268     UINT32                  StringLength = 0;
269 
270 
271     ACPI_FUNCTION_ENTRY ();
272 
273 
274     /*
275      * The ResourceSourceIndex and ResourceSource are optional elements of
276      * some Large-type resource descriptors.
277      */
278 
279     /*
280      * If the length of the actual resource descriptor is greater than the
281      * ACPI spec-defined minimum length, it means that a ResourceSourceIndex
282      * exists and is followed by a (required) null terminated string. The
283      * string length (including the null terminator) is the resource length
284      * minus the minimum length, minus one byte for the ResourceSourceIndex
285      * itself.
286      */
287     if (ResourceLength > MinimumAmlResourceLength)
288     {
289         /* Compute the length of the optional string */
290 
291         StringLength = ResourceLength - MinimumAmlResourceLength - 1;
292     }
293 
294     /*
295      * Round the length up to a multiple of the native word in order to
296      * guarantee that the entire resource descriptor is native word aligned
297      */
298     return ((UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (StringLength));
299 }
300 
301 
302 /*******************************************************************************
303  *
304  * FUNCTION:    AcpiRsGetAmlLength
305  *
306  * PARAMETERS:  Resource            - Pointer to the resource linked list
307  *              ResourceListSize    - Size of the resource linked list
308  *              SizeNeeded          - Where the required size is returned
309  *
310  * RETURN:      Status
311  *
312  * DESCRIPTION: Takes a linked list of internal resource descriptors and
313  *              calculates the size buffer needed to hold the corresponding
314  *              external resource byte stream.
315  *
316  ******************************************************************************/
317 
318 ACPI_STATUS
319 AcpiRsGetAmlLength (
320     ACPI_RESOURCE           *Resource,
321     ACPI_SIZE               ResourceListSize,
322     ACPI_SIZE               *SizeNeeded)
323 {
324     ACPI_SIZE               AmlSizeNeeded = 0;
325     ACPI_RESOURCE           *ResourceEnd;
326     ACPI_RS_LENGTH          TotalSize;
327 
328 
329     ACPI_FUNCTION_TRACE (RsGetAmlLength);
330 
331 
332     /* Traverse entire list of internal resource descriptors */
333 
334     ResourceEnd = ACPI_ADD_PTR (ACPI_RESOURCE, Resource, ResourceListSize);
335     while (Resource < ResourceEnd)
336     {
337         /* Validate the descriptor type */
338 
339         if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
340         {
341             return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
342         }
343 
344         /* Sanity check the length. It must not be zero, or we loop forever */
345 
346         if (!Resource->Length)
347         {
348             return_ACPI_STATUS (AE_AML_BAD_RESOURCE_LENGTH);
349         }
350 
351         /* Get the base size of the (external stream) resource descriptor */
352 
353         TotalSize = AcpiGbl_AmlResourceSizes [Resource->Type];
354 
355         /*
356          * Augment the base size for descriptors with optional and/or
357          * variable-length fields
358          */
359         switch (Resource->Type)
360         {
361         case ACPI_RESOURCE_TYPE_IRQ:
362 
363             /* Length can be 3 or 2 */
364 
365             if (Resource->Data.Irq.DescriptorLength == 2)
366             {
367                 TotalSize--;
368             }
369             break;
370 
371 
372         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
373 
374             /* Length can be 1 or 0 */
375 
376             if (Resource->Data.Irq.DescriptorLength == 0)
377             {
378                 TotalSize--;
379             }
380             break;
381 
382 
383         case ACPI_RESOURCE_TYPE_VENDOR:
384             /*
385              * Vendor Defined Resource:
386              * For a Vendor Specific resource, if the Length is between 1 and 7
387              * it will be created as a Small Resource data type, otherwise it
388              * is a Large Resource data type.
389              */
390             if (Resource->Data.Vendor.ByteLength > 7)
391             {
392                 /* Base size of a Large resource descriptor */
393 
394                 TotalSize = sizeof (AML_RESOURCE_LARGE_HEADER);
395             }
396 
397             /* Add the size of the vendor-specific data */
398 
399             TotalSize = (ACPI_RS_LENGTH)
400                 (TotalSize + Resource->Data.Vendor.ByteLength);
401             break;
402 
403 
404         case ACPI_RESOURCE_TYPE_END_TAG:
405             /*
406              * End Tag:
407              * We are done -- return the accumulated total size.
408              */
409             *SizeNeeded = AmlSizeNeeded + TotalSize;
410 
411             /* Normal exit */
412 
413             return_ACPI_STATUS (AE_OK);
414 
415 
416         case ACPI_RESOURCE_TYPE_ADDRESS16:
417             /*
418              * 16-Bit Address Resource:
419              * Add the size of the optional ResourceSource info
420              */
421             TotalSize = (ACPI_RS_LENGTH) (TotalSize +
422                 AcpiRsStructOptionLength (
423                     &Resource->Data.Address16.ResourceSource));
424             break;
425 
426 
427         case ACPI_RESOURCE_TYPE_ADDRESS32:
428             /*
429              * 32-Bit Address Resource:
430              * Add the size of the optional ResourceSource info
431              */
432             TotalSize = (ACPI_RS_LENGTH) (TotalSize +
433                 AcpiRsStructOptionLength (
434                     &Resource->Data.Address32.ResourceSource));
435             break;
436 
437 
438         case ACPI_RESOURCE_TYPE_ADDRESS64:
439             /*
440              * 64-Bit Address Resource:
441              * Add the size of the optional ResourceSource info
442              */
443             TotalSize = (ACPI_RS_LENGTH) (TotalSize +
444                 AcpiRsStructOptionLength (
445                     &Resource->Data.Address64.ResourceSource));
446             break;
447 
448 
449         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
450             /*
451              * Extended IRQ Resource:
452              * Add the size of each additional optional interrupt beyond the
453              * required 1 (4 bytes for each UINT32 interrupt number)
454              */
455             TotalSize = (ACPI_RS_LENGTH) (TotalSize +
456                 ((Resource->Data.ExtendedIrq.InterruptCount - 1) * 4) +
457 
458                 /* Add the size of the optional ResourceSource info */
459 
460                 AcpiRsStructOptionLength (
461                     &Resource->Data.ExtendedIrq.ResourceSource));
462             break;
463 
464 
465         case ACPI_RESOURCE_TYPE_GPIO:
466 
467             TotalSize = (ACPI_RS_LENGTH) (TotalSize +
468                 (Resource->Data.Gpio.PinTableLength * 2) +
469                 Resource->Data.Gpio.ResourceSource.StringLength +
470                 Resource->Data.Gpio.VendorLength);
471 
472             break;
473 
474 
475         case ACPI_RESOURCE_TYPE_SERIAL_BUS:
476 
477             TotalSize = AcpiGbl_AmlResourceSerialBusSizes [
478                 Resource->Data.CommonSerialBus.Type];
479 
480             TotalSize = (ACPI_RS_LENGTH) (TotalSize +
481                 Resource->Data.I2cSerialBus.ResourceSource.StringLength +
482                 Resource->Data.I2cSerialBus.VendorLength);
483 
484             break;
485 
486         default:
487 
488             break;
489         }
490 
491         /* Update the total */
492 
493         AmlSizeNeeded += TotalSize;
494 
495         /* Point to the next object */
496 
497         Resource = ACPI_ADD_PTR (ACPI_RESOURCE, Resource, Resource->Length);
498     }
499 
500     /* Did not find an EndTag resource descriptor */
501 
502     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
503 }
504 
505 
506 /*******************************************************************************
507  *
508  * FUNCTION:    AcpiRsGetListLength
509  *
510  * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
511  *              AmlBufferLength     - Size of AmlBuffer
512  *              SizeNeeded          - Where the size needed is returned
513  *
514  * RETURN:      Status
515  *
516  * DESCRIPTION: Takes an external resource byte stream and calculates the size
517  *              buffer needed to hold the corresponding internal resource
518  *              descriptor linked list.
519  *
520  ******************************************************************************/
521 
522 ACPI_STATUS
523 AcpiRsGetListLength (
524     UINT8                   *AmlBuffer,
525     UINT32                  AmlBufferLength,
526     ACPI_SIZE               *SizeNeeded)
527 {
528     ACPI_STATUS             Status;
529     UINT8                   *EndAml;
530     UINT8                   *Buffer;
531     UINT32                  BufferSize;
532     UINT16                  Temp16;
533     UINT16                  ResourceLength;
534     UINT32                  ExtraStructBytes;
535     UINT8                   ResourceIndex;
536     UINT8                   MinimumAmlResourceLength;
537     AML_RESOURCE            *AmlResource;
538 
539 
540     ACPI_FUNCTION_TRACE (RsGetListLength);
541 
542 
543     *SizeNeeded = ACPI_RS_SIZE_MIN;         /* Minimum size is one EndTag */
544     EndAml = AmlBuffer + AmlBufferLength;
545 
546     /* Walk the list of AML resource descriptors */
547 
548     while (AmlBuffer < EndAml)
549     {
550         /* Validate the Resource Type and Resource Length */
551 
552         Status = AcpiUtValidateResource (NULL, AmlBuffer, &ResourceIndex);
553         if (ACPI_FAILURE (Status))
554         {
555             /*
556              * Exit on failure. Cannot continue because the descriptor length
557              * may be bogus also.
558              */
559             return_ACPI_STATUS (Status);
560         }
561 
562         AmlResource = (void *) AmlBuffer;
563 
564         /* Get the resource length and base (minimum) AML size */
565 
566         ResourceLength = AcpiUtGetResourceLength (AmlBuffer);
567         MinimumAmlResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
568 
569         /*
570          * Augment the size for descriptors with optional
571          * and/or variable length fields
572          */
573         ExtraStructBytes = 0;
574         Buffer = AmlBuffer + AcpiUtGetResourceHeaderLength (AmlBuffer);
575 
576         switch (AcpiUtGetResourceType (AmlBuffer))
577         {
578         case ACPI_RESOURCE_NAME_IRQ:
579             /*
580              * IRQ Resource:
581              * Get the number of bits set in the 16-bit IRQ mask
582              */
583             ACPI_MOVE_16_TO_16 (&Temp16, Buffer);
584             ExtraStructBytes = AcpiRsCountSetBits (Temp16);
585             break;
586 
587 
588         case ACPI_RESOURCE_NAME_DMA:
589             /*
590              * DMA Resource:
591              * Get the number of bits set in the 8-bit DMA mask
592              */
593             ExtraStructBytes = AcpiRsCountSetBits (*Buffer);
594             break;
595 
596 
597         case ACPI_RESOURCE_NAME_VENDOR_SMALL:
598         case ACPI_RESOURCE_NAME_VENDOR_LARGE:
599             /*
600              * Vendor Resource:
601              * Get the number of vendor data bytes
602              */
603             ExtraStructBytes = ResourceLength;
604 
605             /*
606              * There is already one byte included in the minimum
607              * descriptor size. If there are extra struct bytes,
608              * subtract one from the count.
609              */
610             if (ExtraStructBytes)
611             {
612                 ExtraStructBytes--;
613             }
614             break;
615 
616 
617         case ACPI_RESOURCE_NAME_END_TAG:
618             /*
619              * End Tag: This is the normal exit
620              */
621             return_ACPI_STATUS (AE_OK);
622 
623 
624         case ACPI_RESOURCE_NAME_ADDRESS32:
625         case ACPI_RESOURCE_NAME_ADDRESS16:
626         case ACPI_RESOURCE_NAME_ADDRESS64:
627             /*
628              * Address Resource:
629              * Add the size of the optional ResourceSource
630              */
631             ExtraStructBytes = AcpiRsStreamOptionLength (
632                 ResourceLength, MinimumAmlResourceLength);
633             break;
634 
635 
636         case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
637             /*
638              * Extended IRQ Resource:
639              * Using the InterruptTableLength, add 4 bytes for each additional
640              * interrupt. Note: at least one interrupt is required and is
641              * included in the minimum descriptor size (reason for the -1)
642              */
643             ExtraStructBytes = (Buffer[1] - 1) * sizeof (UINT32);
644 
645             /* Add the size of the optional ResourceSource */
646 
647             ExtraStructBytes += AcpiRsStreamOptionLength (
648                 ResourceLength - ExtraStructBytes, MinimumAmlResourceLength);
649             break;
650 
651         case ACPI_RESOURCE_NAME_GPIO:
652 
653             /* Vendor data is optional */
654 
655             if (AmlResource->Gpio.VendorLength)
656             {
657                 ExtraStructBytes +=
658                     AmlResource->Gpio.VendorOffset -
659                     AmlResource->Gpio.PinTableOffset +
660                     AmlResource->Gpio.VendorLength;
661             }
662             else
663             {
664                 ExtraStructBytes +=
665                     AmlResource->LargeHeader.ResourceLength +
666                     sizeof (AML_RESOURCE_LARGE_HEADER) -
667                     AmlResource->Gpio.PinTableOffset;
668             }
669             break;
670 
671         case ACPI_RESOURCE_NAME_SERIAL_BUS:
672 
673             MinimumAmlResourceLength = AcpiGbl_ResourceAmlSerialBusSizes[
674                 AmlResource->CommonSerialBus.Type];
675             ExtraStructBytes +=
676                 AmlResource->CommonSerialBus.ResourceLength -
677                 MinimumAmlResourceLength;
678             break;
679 
680         default:
681 
682             break;
683         }
684 
685         /*
686          * Update the required buffer size for the internal descriptor structs
687          *
688          * Important: Round the size up for the appropriate alignment. This
689          * is a requirement on IA64.
690          */
691         if (AcpiUtGetResourceType (AmlBuffer) ==
692             ACPI_RESOURCE_NAME_SERIAL_BUS)
693         {
694             BufferSize = AcpiGbl_ResourceStructSerialBusSizes[
695                 AmlResource->CommonSerialBus.Type] + ExtraStructBytes;
696         }
697         else
698         {
699             BufferSize = AcpiGbl_ResourceStructSizes[ResourceIndex] +
700                 ExtraStructBytes;
701         }
702 
703         BufferSize = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (BufferSize);
704         *SizeNeeded += BufferSize;
705 
706         ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,
707             "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
708             AcpiUtGetResourceType (AmlBuffer),
709             AcpiUtGetDescriptorLength (AmlBuffer), BufferSize));
710 
711         /*
712          * Point to the next resource within the AML stream using the length
713          * contained in the resource descriptor header
714          */
715         AmlBuffer += AcpiUtGetDescriptorLength (AmlBuffer);
716     }
717 
718     /* Did not find an EndTag resource descriptor */
719 
720     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
721 }
722 
723 
724 /*******************************************************************************
725  *
726  * FUNCTION:    AcpiRsGetPciRoutingTableLength
727  *
728  * PARAMETERS:  PackageObject           - Pointer to the package object
729  *              BufferSizeNeeded        - UINT32 pointer of the size buffer
730  *                                        needed to properly return the
731  *                                        parsed data
732  *
733  * RETURN:      Status
734  *
735  * DESCRIPTION: Given a package representing a PCI routing table, this
736  *              calculates the size of the corresponding linked list of
737  *              descriptions.
738  *
739  ******************************************************************************/
740 
741 ACPI_STATUS
742 AcpiRsGetPciRoutingTableLength (
743     ACPI_OPERAND_OBJECT     *PackageObject,
744     ACPI_SIZE               *BufferSizeNeeded)
745 {
746     UINT32                  NumberOfElements;
747     ACPI_SIZE               TempSizeNeeded = 0;
748     ACPI_OPERAND_OBJECT     **TopObjectList;
749     UINT32                  Index;
750     ACPI_OPERAND_OBJECT     *PackageElement;
751     ACPI_OPERAND_OBJECT     **SubObjectList;
752     BOOLEAN                 NameFound;
753     UINT32                  TableIndex;
754 
755 
756     ACPI_FUNCTION_TRACE (RsGetPciRoutingTableLength);
757 
758 
759     NumberOfElements = PackageObject->Package.Count;
760 
761     /*
762      * Calculate the size of the return buffer.
763      * The base size is the number of elements * the sizes of the
764      * structures. Additional space for the strings is added below.
765      * The minus one is to subtract the size of the UINT8 Source[1]
766      * member because it is added below.
767      *
768      * But each PRT_ENTRY structure has a pointer to a string and
769      * the size of that string must be found.
770      */
771     TopObjectList = PackageObject->Package.Elements;
772 
773     for (Index = 0; Index < NumberOfElements; Index++)
774     {
775         /* Dereference the subpackage */
776 
777         PackageElement = *TopObjectList;
778 
779         /* We must have a valid Package object */
780 
781         if (!PackageElement ||
782             (PackageElement->Common.Type != ACPI_TYPE_PACKAGE))
783         {
784             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
785         }
786 
787         /*
788          * The SubObjectList will now point to an array of the
789          * four IRQ elements: Address, Pin, Source and SourceIndex
790          */
791         SubObjectList = PackageElement->Package.Elements;
792 
793         /* Scan the IrqTableElements for the Source Name String */
794 
795         NameFound = FALSE;
796 
797         for (TableIndex = 0;
798              TableIndex < PackageElement->Package.Count && !NameFound;
799              TableIndex++)
800         {
801             if (*SubObjectList && /* Null object allowed */
802 
803                 ((ACPI_TYPE_STRING ==
804                     (*SubObjectList)->Common.Type) ||
805 
806                 ((ACPI_TYPE_LOCAL_REFERENCE ==
807                     (*SubObjectList)->Common.Type) &&
808 
809                     ((*SubObjectList)->Reference.Class ==
810                         ACPI_REFCLASS_NAME))))
811             {
812                 NameFound = TRUE;
813             }
814             else
815             {
816                 /* Look at the next element */
817 
818                 SubObjectList++;
819             }
820         }
821 
822         TempSizeNeeded += (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
823 
824         /* Was a String type found? */
825 
826         if (NameFound)
827         {
828             if ((*SubObjectList)->Common.Type == ACPI_TYPE_STRING)
829             {
830                 /*
831                  * The length String.Length field does not include the
832                  * terminating NULL, add 1
833                  */
834                 TempSizeNeeded += ((ACPI_SIZE)
835                     (*SubObjectList)->String.Length + 1);
836             }
837             else
838             {
839                 TempSizeNeeded += AcpiNsGetPathnameLength (
840                     (*SubObjectList)->Reference.Node);
841             }
842         }
843         else
844         {
845             /*
846              * If no name was found, then this is a NULL, which is
847              * translated as a UINT32 zero.
848              */
849             TempSizeNeeded += sizeof (UINT32);
850         }
851 
852         /* Round up the size since each element must be aligned */
853 
854         TempSizeNeeded = ACPI_ROUND_UP_TO_64BIT (TempSizeNeeded);
855 
856         /* Point to the next ACPI_OPERAND_OBJECT */
857 
858         TopObjectList++;
859     }
860 
861     /*
862      * Add an extra element to the end of the list, essentially a
863      * NULL terminator
864      */
865     *BufferSizeNeeded = TempSizeNeeded + sizeof (ACPI_PCI_ROUTING_TABLE);
866     return_ACPI_STATUS (AE_OK);
867 }
868