xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/resources/rscalc.c (revision 986e4abce4efeccd9418eb8cdc7a710487f093b9)
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 - 2013, 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 #define __RSCALC_C__
117 
118 #include "acpi.h"
119 #include "accommon.h"
120 #include "acresrc.h"
121 #include "acnamesp.h"
122 
123 
124 #define _COMPONENT          ACPI_RESOURCES
125         ACPI_MODULE_NAME    ("rscalc")
126 
127 
128 /* Local prototypes */
129 
130 static UINT8
131 AcpiRsCountSetBits (
132     UINT16                  BitField);
133 
134 static ACPI_RS_LENGTH
135 AcpiRsStructOptionLength (
136     ACPI_RESOURCE_SOURCE    *ResourceSource);
137 
138 static UINT32
139 AcpiRsStreamOptionLength (
140     UINT32                  ResourceLength,
141     UINT32                  MinimumTotalLength);
142 
143 
144 /*******************************************************************************
145  *
146  * FUNCTION:    AcpiRsCountSetBits
147  *
148  * PARAMETERS:  BitField        - Field in which to count bits
149  *
150  * RETURN:      Number of bits set within the field
151  *
152  * DESCRIPTION: Count the number of bits set in a resource field. Used for
153  *              (Short descriptor) interrupt and DMA lists.
154  *
155  ******************************************************************************/
156 
157 static UINT8
158 AcpiRsCountSetBits (
159     UINT16                  BitField)
160 {
161     UINT8                   BitsSet;
162 
163 
164     ACPI_FUNCTION_ENTRY ();
165 
166 
167     for (BitsSet = 0; BitField; BitsSet++)
168     {
169         /* Zero the least significant bit that is set */
170 
171         BitField &= (UINT16) (BitField - 1);
172     }
173 
174     return (BitsSet);
175 }
176 
177 
178 /*******************************************************************************
179  *
180  * FUNCTION:    AcpiRsStructOptionLength
181  *
182  * PARAMETERS:  ResourceSource      - Pointer to optional descriptor field
183  *
184  * RETURN:      Status
185  *
186  * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
187  *              ResourceSource fields in some Large descriptors. Used during
188  *              list-to-stream conversion
189  *
190  ******************************************************************************/
191 
192 static ACPI_RS_LENGTH
193 AcpiRsStructOptionLength (
194     ACPI_RESOURCE_SOURCE    *ResourceSource)
195 {
196     ACPI_FUNCTION_ENTRY ();
197 
198 
199     /*
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).
203      */
204     if (ResourceSource->StringPtr)
205     {
206         return ((ACPI_RS_LENGTH) (ResourceSource->StringLength + 1));
207     }
208 
209     return (0);
210 }
211 
212 
213 /*******************************************************************************
214  *
215  * FUNCTION:    AcpiRsStreamOptionLength
216  *
217  * PARAMETERS:  ResourceLength      - Length from the resource header
218  *              MinimumTotalLength  - Minimum length of this resource, before
219  *                                    any optional fields. Includes header size
220  *
221  * RETURN:      Length of optional string (0 if no string present)
222  *
223  * DESCRIPTION: Common code to handle optional ResourceSourceIndex and
224  *              ResourceSource fields in some Large descriptors. Used during
225  *              stream-to-list conversion
226  *
227  ******************************************************************************/
228 
229 static UINT32
230 AcpiRsStreamOptionLength (
231     UINT32                  ResourceLength,
232     UINT32                  MinimumAmlResourceLength)
233 {
234     UINT32                  StringLength = 0;
235 
236 
237     ACPI_FUNCTION_ENTRY ();
238 
239 
240     /*
241      * The ResourceSourceIndex and ResourceSource are optional elements of some
242      * Large-type resource descriptors.
243      */
244 
245     /*
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.
251      */
252     if (ResourceLength > MinimumAmlResourceLength)
253     {
254         /* Compute the length of the optional string */
255 
256         StringLength = ResourceLength - MinimumAmlResourceLength - 1;
257     }
258 
259     /*
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
262      */
263     return ((UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (StringLength));
264 }
265 
266 
267 /*******************************************************************************
268  *
269  * FUNCTION:    AcpiRsGetAmlLength
270  *
271  * PARAMETERS:  Resource            - Pointer to the resource linked list
272  *              SizeNeeded          - Where the required size is returned
273  *
274  * RETURN:      Status
275  *
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.
279  *
280  ******************************************************************************/
281 
282 ACPI_STATUS
283 AcpiRsGetAmlLength (
284     ACPI_RESOURCE           *Resource,
285     ACPI_SIZE               *SizeNeeded)
286 {
287     ACPI_SIZE               AmlSizeNeeded = 0;
288     ACPI_RS_LENGTH          TotalSize;
289 
290 
291     ACPI_FUNCTION_TRACE (RsGetAmlLength);
292 
293 
294     /* Traverse entire list of internal resource descriptors */
295 
296     while (Resource)
297     {
298         /* Validate the descriptor type */
299 
300         if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
301         {
302             return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
303         }
304 
305         /* Sanity check the length. It must not be zero, or we loop forever */
306 
307         if (!Resource->Length)
308         {
309             return_ACPI_STATUS (AE_AML_BAD_RESOURCE_LENGTH);
310         }
311 
312         /* Get the base size of the (external stream) resource descriptor */
313 
314         TotalSize = AcpiGbl_AmlResourceSizes [Resource->Type];
315 
316         /*
317          * Augment the base size for descriptors with optional and/or
318          * variable-length fields
319          */
320         switch (Resource->Type)
321         {
322         case ACPI_RESOURCE_TYPE_IRQ:
323 
324             /* Length can be 3 or 2 */
325 
326             if (Resource->Data.Irq.DescriptorLength == 2)
327             {
328                 TotalSize--;
329             }
330             break;
331 
332 
333         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
334 
335             /* Length can be 1 or 0 */
336 
337             if (Resource->Data.Irq.DescriptorLength == 0)
338             {
339                 TotalSize--;
340             }
341             break;
342 
343 
344         case ACPI_RESOURCE_TYPE_VENDOR:
345             /*
346              * Vendor Defined Resource:
347              * For a Vendor Specific resource, if the Length is between 1 and 7
348              * it will be created as a Small Resource data type, otherwise it
349              * is a Large Resource data type.
350              */
351             if (Resource->Data.Vendor.ByteLength > 7)
352             {
353                 /* Base size of a Large resource descriptor */
354 
355                 TotalSize = sizeof (AML_RESOURCE_LARGE_HEADER);
356             }
357 
358             /* Add the size of the vendor-specific data */
359 
360             TotalSize = (ACPI_RS_LENGTH)
361                 (TotalSize + Resource->Data.Vendor.ByteLength);
362             break;
363 
364 
365         case ACPI_RESOURCE_TYPE_END_TAG:
366             /*
367              * End Tag:
368              * We are done -- return the accumulated total size.
369              */
370             *SizeNeeded = AmlSizeNeeded + TotalSize;
371 
372             /* Normal exit */
373 
374             return_ACPI_STATUS (AE_OK);
375 
376 
377         case ACPI_RESOURCE_TYPE_ADDRESS16:
378             /*
379              * 16-Bit Address Resource:
380              * Add the size of the optional ResourceSource info
381              */
382             TotalSize = (ACPI_RS_LENGTH)
383                 (TotalSize + AcpiRsStructOptionLength (
384                                 &Resource->Data.Address16.ResourceSource));
385             break;
386 
387 
388         case ACPI_RESOURCE_TYPE_ADDRESS32:
389             /*
390              * 32-Bit Address Resource:
391              * Add the size of the optional ResourceSource info
392              */
393             TotalSize = (ACPI_RS_LENGTH)
394                 (TotalSize + AcpiRsStructOptionLength (
395                                 &Resource->Data.Address32.ResourceSource));
396             break;
397 
398 
399         case ACPI_RESOURCE_TYPE_ADDRESS64:
400             /*
401              * 64-Bit Address Resource:
402              * Add the size of the optional ResourceSource info
403              */
404             TotalSize = (ACPI_RS_LENGTH)
405                 (TotalSize + AcpiRsStructOptionLength (
406                                 &Resource->Data.Address64.ResourceSource));
407             break;
408 
409 
410         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
411             /*
412              * Extended IRQ Resource:
413              * Add the size of each additional optional interrupt beyond the
414              * required 1 (4 bytes for each UINT32 interrupt number)
415              */
416             TotalSize = (ACPI_RS_LENGTH)
417                 (TotalSize +
418                 ((Resource->Data.ExtendedIrq.InterruptCount - 1) * 4) +
419 
420                 /* Add the size of the optional ResourceSource info */
421 
422                 AcpiRsStructOptionLength (
423                     &Resource->Data.ExtendedIrq.ResourceSource));
424             break;
425 
426 
427         case ACPI_RESOURCE_TYPE_GPIO:
428 
429             TotalSize = (ACPI_RS_LENGTH) (TotalSize + (Resource->Data.Gpio.PinTableLength * 2) +
430                 Resource->Data.Gpio.ResourceSource.StringLength +
431                 Resource->Data.Gpio.VendorLength);
432 
433             break;
434 
435 
436         case ACPI_RESOURCE_TYPE_SERIAL_BUS:
437 
438             TotalSize = AcpiGbl_AmlResourceSerialBusSizes [Resource->Data.CommonSerialBus.Type];
439 
440             TotalSize = (ACPI_RS_LENGTH) (TotalSize +
441                 Resource->Data.I2cSerialBus.ResourceSource.StringLength +
442                 Resource->Data.I2cSerialBus.VendorLength);
443 
444             break;
445 
446         default:
447 
448             break;
449         }
450 
451         /* Update the total */
452 
453         AmlSizeNeeded += TotalSize;
454 
455         /* Point to the next object */
456 
457         Resource = ACPI_ADD_PTR (ACPI_RESOURCE, Resource, Resource->Length);
458     }
459 
460     /* Did not find an EndTag resource descriptor */
461 
462     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
463 }
464 
465 
466 /*******************************************************************************
467  *
468  * FUNCTION:    AcpiRsGetListLength
469  *
470  * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
471  *              AmlBufferLength     - Size of AmlBuffer
472  *              SizeNeeded          - Where the size needed is returned
473  *
474  * RETURN:      Status
475  *
476  * DESCRIPTION: Takes an external resource byte stream and calculates the size
477  *              buffer needed to hold the corresponding internal resource
478  *              descriptor linked list.
479  *
480  ******************************************************************************/
481 
482 ACPI_STATUS
483 AcpiRsGetListLength (
484     UINT8                   *AmlBuffer,
485     UINT32                  AmlBufferLength,
486     ACPI_SIZE               *SizeNeeded)
487 {
488     ACPI_STATUS             Status;
489     UINT8                   *EndAml;
490     UINT8                   *Buffer;
491     UINT32                  BufferSize;
492     UINT16                  Temp16;
493     UINT16                  ResourceLength;
494     UINT32                  ExtraStructBytes;
495     UINT8                   ResourceIndex;
496     UINT8                   MinimumAmlResourceLength;
497     AML_RESOURCE            *AmlResource;
498 
499 
500     ACPI_FUNCTION_TRACE (RsGetListLength);
501 
502 
503     *SizeNeeded = ACPI_RS_SIZE_MIN;         /* Minimum size is one EndTag */
504     EndAml = AmlBuffer + AmlBufferLength;
505 
506     /* Walk the list of AML resource descriptors */
507 
508     while (AmlBuffer < EndAml)
509     {
510         /* Validate the Resource Type and Resource Length */
511 
512         Status = AcpiUtValidateResource (NULL, AmlBuffer, &ResourceIndex);
513         if (ACPI_FAILURE (Status))
514         {
515             /*
516              * Exit on failure. Cannot continue because the descriptor length
517              * may be bogus also.
518              */
519             return_ACPI_STATUS (Status);
520         }
521 
522         AmlResource = (void *) AmlBuffer;
523 
524         /* Get the resource length and base (minimum) AML size */
525 
526         ResourceLength = AcpiUtGetResourceLength (AmlBuffer);
527         MinimumAmlResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
528 
529         /*
530          * Augment the size for descriptors with optional
531          * and/or variable length fields
532          */
533         ExtraStructBytes = 0;
534         Buffer = AmlBuffer + AcpiUtGetResourceHeaderLength (AmlBuffer);
535 
536         switch (AcpiUtGetResourceType (AmlBuffer))
537         {
538         case ACPI_RESOURCE_NAME_IRQ:
539             /*
540              * IRQ Resource:
541              * Get the number of bits set in the 16-bit IRQ mask
542              */
543             ACPI_MOVE_16_TO_16 (&Temp16, Buffer);
544             ExtraStructBytes = AcpiRsCountSetBits (Temp16);
545             break;
546 
547 
548         case ACPI_RESOURCE_NAME_DMA:
549             /*
550              * DMA Resource:
551              * Get the number of bits set in the 8-bit DMA mask
552              */
553             ExtraStructBytes = AcpiRsCountSetBits (*Buffer);
554             break;
555 
556 
557         case ACPI_RESOURCE_NAME_VENDOR_SMALL:
558         case ACPI_RESOURCE_NAME_VENDOR_LARGE:
559             /*
560              * Vendor Resource:
561              * Get the number of vendor data bytes
562              */
563             ExtraStructBytes = ResourceLength;
564 
565             /*
566              * There is already one byte included in the minimum
567              * descriptor size. If there are extra struct bytes,
568              * subtract one from the count.
569              */
570             if (ExtraStructBytes)
571             {
572                 ExtraStructBytes--;
573             }
574             break;
575 
576 
577         case ACPI_RESOURCE_NAME_END_TAG:
578             /*
579              * End Tag: This is the normal exit
580              */
581             return_ACPI_STATUS (AE_OK);
582 
583 
584         case ACPI_RESOURCE_NAME_ADDRESS32:
585         case ACPI_RESOURCE_NAME_ADDRESS16:
586         case ACPI_RESOURCE_NAME_ADDRESS64:
587             /*
588              * Address Resource:
589              * Add the size of the optional ResourceSource
590              */
591             ExtraStructBytes = AcpiRsStreamOptionLength (
592                 ResourceLength, MinimumAmlResourceLength);
593             break;
594 
595 
596         case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
597             /*
598              * Extended IRQ Resource:
599              * Using the InterruptTableLength, add 4 bytes for each additional
600              * interrupt. Note: at least one interrupt is required and is
601              * included in the minimum descriptor size (reason for the -1)
602              */
603             ExtraStructBytes = (Buffer[1] - 1) * sizeof (UINT32);
604 
605             /* Add the size of the optional ResourceSource */
606 
607             ExtraStructBytes += AcpiRsStreamOptionLength (
608                 ResourceLength - ExtraStructBytes, MinimumAmlResourceLength);
609             break;
610 
611         case ACPI_RESOURCE_NAME_GPIO:
612 
613             /* Vendor data is optional */
614 
615             if (AmlResource->Gpio.VendorLength)
616             {
617                 ExtraStructBytes += AmlResource->Gpio.VendorOffset -
618                     AmlResource->Gpio.PinTableOffset + AmlResource->Gpio.VendorLength;
619             }
620             else
621             {
622                 ExtraStructBytes += AmlResource->LargeHeader.ResourceLength +
623                     sizeof (AML_RESOURCE_LARGE_HEADER) -
624                     AmlResource->Gpio.PinTableOffset;
625             }
626             break;
627 
628         case ACPI_RESOURCE_NAME_SERIAL_BUS:
629 
630             MinimumAmlResourceLength = AcpiGbl_ResourceAmlSerialBusSizes[
631                 AmlResource->CommonSerialBus.Type];
632             ExtraStructBytes += AmlResource->CommonSerialBus.ResourceLength -
633                 MinimumAmlResourceLength;
634             break;
635 
636         default:
637 
638             break;
639         }
640 
641         /*
642          * Update the required buffer size for the internal descriptor structs
643          *
644          * Important: Round the size up for the appropriate alignment. This
645          * is a requirement on IA64.
646          */
647         if (AcpiUtGetResourceType (AmlBuffer) == ACPI_RESOURCE_NAME_SERIAL_BUS)
648         {
649             BufferSize = AcpiGbl_ResourceStructSerialBusSizes[
650                 AmlResource->CommonSerialBus.Type] + ExtraStructBytes;
651         }
652         else
653         {
654             BufferSize = AcpiGbl_ResourceStructSizes[ResourceIndex] +
655                         ExtraStructBytes;
656         }
657         BufferSize = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (BufferSize);
658 
659         *SizeNeeded += BufferSize;
660 
661         ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,
662             "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
663             AcpiUtGetResourceType (AmlBuffer),
664             AcpiUtGetDescriptorLength (AmlBuffer), BufferSize));
665 
666         /*
667          * Point to the next resource within the AML stream using the length
668          * contained in the resource descriptor header
669          */
670         AmlBuffer += AcpiUtGetDescriptorLength (AmlBuffer);
671     }
672 
673     /* Did not find an EndTag resource descriptor */
674 
675     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
676 }
677 
678 
679 /*******************************************************************************
680  *
681  * FUNCTION:    AcpiRsGetPciRoutingTableLength
682  *
683  * PARAMETERS:  PackageObject           - Pointer to the package object
684  *              BufferSizeNeeded        - UINT32 pointer of the size buffer
685  *                                        needed to properly return the
686  *                                        parsed data
687  *
688  * RETURN:      Status
689  *
690  * DESCRIPTION: Given a package representing a PCI routing table, this
691  *              calculates the size of the corresponding linked list of
692  *              descriptions.
693  *
694  ******************************************************************************/
695 
696 ACPI_STATUS
697 AcpiRsGetPciRoutingTableLength (
698     ACPI_OPERAND_OBJECT     *PackageObject,
699     ACPI_SIZE               *BufferSizeNeeded)
700 {
701     UINT32                  NumberOfElements;
702     ACPI_SIZE               TempSizeNeeded = 0;
703     ACPI_OPERAND_OBJECT     **TopObjectList;
704     UINT32                  Index;
705     ACPI_OPERAND_OBJECT     *PackageElement;
706     ACPI_OPERAND_OBJECT     **SubObjectList;
707     BOOLEAN                 NameFound;
708     UINT32                  TableIndex;
709 
710 
711     ACPI_FUNCTION_TRACE (RsGetPciRoutingTableLength);
712 
713 
714     NumberOfElements = PackageObject->Package.Count;
715 
716     /*
717      * Calculate the size of the return buffer.
718      * The base size is the number of elements * the sizes of the
719      * structures. Additional space for the strings is added below.
720      * The minus one is to subtract the size of the UINT8 Source[1]
721      * member because it is added below.
722      *
723      * But each PRT_ENTRY structure has a pointer to a string and
724      * the size of that string must be found.
725      */
726     TopObjectList = PackageObject->Package.Elements;
727 
728     for (Index = 0; Index < NumberOfElements; Index++)
729     {
730         /* Dereference the sub-package */
731 
732         PackageElement = *TopObjectList;
733 
734         /* We must have a valid Package object */
735 
736         if (!PackageElement ||
737             (PackageElement->Common.Type != ACPI_TYPE_PACKAGE))
738         {
739             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
740         }
741 
742         /*
743          * The SubObjectList will now point to an array of the
744          * four IRQ elements: Address, Pin, Source and SourceIndex
745          */
746         SubObjectList = PackageElement->Package.Elements;
747 
748         /* Scan the IrqTableElements for the Source Name String */
749 
750         NameFound = FALSE;
751 
752         for (TableIndex = 0;
753              TableIndex < PackageElement->Package.Count && !NameFound;
754              TableIndex++)
755         {
756             if (*SubObjectList && /* Null object allowed */
757 
758                 ((ACPI_TYPE_STRING ==
759                     (*SubObjectList)->Common.Type) ||
760 
761                 ((ACPI_TYPE_LOCAL_REFERENCE ==
762                     (*SubObjectList)->Common.Type) &&
763 
764                     ((*SubObjectList)->Reference.Class ==
765                         ACPI_REFCLASS_NAME))))
766             {
767                 NameFound = TRUE;
768             }
769             else
770             {
771                 /* Look at the next element */
772 
773                 SubObjectList++;
774             }
775         }
776 
777         TempSizeNeeded += (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
778 
779         /* Was a String type found? */
780 
781         if (NameFound)
782         {
783             if ((*SubObjectList)->Common.Type == ACPI_TYPE_STRING)
784             {
785                 /*
786                  * The length String.Length field does not include the
787                  * terminating NULL, add 1
788                  */
789                 TempSizeNeeded += ((ACPI_SIZE)
790                     (*SubObjectList)->String.Length + 1);
791             }
792             else
793             {
794                 TempSizeNeeded += AcpiNsGetPathnameLength (
795                                     (*SubObjectList)->Reference.Node);
796             }
797         }
798         else
799         {
800             /*
801              * If no name was found, then this is a NULL, which is
802              * translated as a UINT32 zero.
803              */
804             TempSizeNeeded += sizeof (UINT32);
805         }
806 
807         /* Round up the size since each element must be aligned */
808 
809         TempSizeNeeded = ACPI_ROUND_UP_TO_64BIT (TempSizeNeeded);
810 
811         /* Point to the next ACPI_OPERAND_OBJECT */
812 
813         TopObjectList++;
814     }
815 
816     /*
817      * Add an extra element to the end of the list, essentially a
818      * NULL terminator
819      */
820     *BufferSizeNeeded = TempSizeNeeded + sizeof (ACPI_PCI_ROUTING_TABLE);
821     return_ACPI_STATUS (AE_OK);
822 }
823