xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/utilities/utobject.c (revision aa3083e086e5a929c061c72983e09d916c548a38)
1 /******************************************************************************
2  *
3  * Module Name: utobject - ACPI object create/delete/size/cache routines
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2016, 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 #include "acpi.h"
117 #include "accommon.h"
118 #include "acnamesp.h"
119 
120 
121 #define _COMPONENT          ACPI_UTILITIES
122         ACPI_MODULE_NAME    ("utobject")
123 
124 /* Local prototypes */
125 
126 static ACPI_STATUS
127 AcpiUtGetSimpleObjectSize (
128     ACPI_OPERAND_OBJECT     *Obj,
129     ACPI_SIZE               *ObjLength);
130 
131 static ACPI_STATUS
132 AcpiUtGetPackageObjectSize (
133     ACPI_OPERAND_OBJECT     *Obj,
134     ACPI_SIZE               *ObjLength);
135 
136 static ACPI_STATUS
137 AcpiUtGetElementLength (
138     UINT8                   ObjectType,
139     ACPI_OPERAND_OBJECT     *SourceObject,
140     ACPI_GENERIC_STATE      *State,
141     void                    *Context);
142 
143 
144 /*******************************************************************************
145  *
146  * FUNCTION:    AcpiUtCreateInternalObjectDbg
147  *
148  * PARAMETERS:  ModuleName          - Source file name of caller
149  *              LineNumber          - Line number of caller
150  *              ComponentId         - Component type of caller
151  *              Type                - ACPI Type of the new object
152  *
153  * RETURN:      A new internal object, null on failure
154  *
155  * DESCRIPTION: Create and initialize a new internal object.
156  *
157  * NOTE:        We always allocate the worst-case object descriptor because
158  *              these objects are cached, and we want them to be
159  *              one-size-satisifies-any-request. This in itself may not be
160  *              the most memory efficient, but the efficiency of the object
161  *              cache should more than make up for this!
162  *
163  ******************************************************************************/
164 
165 ACPI_OPERAND_OBJECT  *
166 AcpiUtCreateInternalObjectDbg (
167     const char              *ModuleName,
168     UINT32                  LineNumber,
169     UINT32                  ComponentId,
170     ACPI_OBJECT_TYPE        Type)
171 {
172     ACPI_OPERAND_OBJECT     *Object;
173     ACPI_OPERAND_OBJECT     *SecondObject;
174 
175 
176     ACPI_FUNCTION_TRACE_STR (UtCreateInternalObjectDbg,
177         AcpiUtGetTypeName (Type));
178 
179 
180     /* Allocate the raw object descriptor */
181 
182     Object = AcpiUtAllocateObjectDescDbg (
183         ModuleName, LineNumber, ComponentId);
184     if (!Object)
185     {
186         return_PTR (NULL);
187     }
188 
189     switch (Type)
190     {
191     case ACPI_TYPE_REGION:
192     case ACPI_TYPE_BUFFER_FIELD:
193     case ACPI_TYPE_LOCAL_BANK_FIELD:
194 
195         /* These types require a secondary object */
196 
197         SecondObject = AcpiUtAllocateObjectDescDbg (
198             ModuleName, LineNumber, ComponentId);
199         if (!SecondObject)
200         {
201             AcpiUtDeleteObjectDesc (Object);
202             return_PTR (NULL);
203         }
204 
205         SecondObject->Common.Type = ACPI_TYPE_LOCAL_EXTRA;
206         SecondObject->Common.ReferenceCount = 1;
207 
208         /* Link the second object to the first */
209 
210         Object->Common.NextObject = SecondObject;
211         break;
212 
213     default:
214 
215         /* All others have no secondary object */
216         break;
217     }
218 
219     /* Save the object type in the object descriptor */
220 
221     Object->Common.Type = (UINT8) Type;
222 
223     /* Init the reference count */
224 
225     Object->Common.ReferenceCount = 1;
226 
227     /* Any per-type initialization should go here */
228 
229     return_PTR (Object);
230 }
231 
232 
233 /*******************************************************************************
234  *
235  * FUNCTION:    AcpiUtCreatePackageObject
236  *
237  * PARAMETERS:  Count               - Number of package elements
238  *
239  * RETURN:      Pointer to a new Package object, null on failure
240  *
241  * DESCRIPTION: Create a fully initialized package object
242  *
243  ******************************************************************************/
244 
245 ACPI_OPERAND_OBJECT *
246 AcpiUtCreatePackageObject (
247     UINT32                  Count)
248 {
249     ACPI_OPERAND_OBJECT     *PackageDesc;
250     ACPI_OPERAND_OBJECT     **PackageElements;
251 
252 
253     ACPI_FUNCTION_TRACE_U32 (UtCreatePackageObject, Count);
254 
255 
256     /* Create a new Package object */
257 
258     PackageDesc = AcpiUtCreateInternalObject (ACPI_TYPE_PACKAGE);
259     if (!PackageDesc)
260     {
261         return_PTR (NULL);
262     }
263 
264     /*
265      * Create the element array. Count+1 allows the array to be null
266      * terminated.
267      */
268     PackageElements = ACPI_ALLOCATE_ZEROED (
269         ((ACPI_SIZE) Count + 1) * sizeof (void *));
270     if (!PackageElements)
271     {
272         ACPI_FREE (PackageDesc);
273         return_PTR (NULL);
274     }
275 
276     PackageDesc->Package.Count = Count;
277     PackageDesc->Package.Elements = PackageElements;
278     return_PTR (PackageDesc);
279 }
280 
281 
282 /*******************************************************************************
283  *
284  * FUNCTION:    AcpiUtCreateIntegerObject
285  *
286  * PARAMETERS:  InitialValue        - Initial value for the integer
287  *
288  * RETURN:      Pointer to a new Integer object, null on failure
289  *
290  * DESCRIPTION: Create an initialized integer object
291  *
292  ******************************************************************************/
293 
294 ACPI_OPERAND_OBJECT *
295 AcpiUtCreateIntegerObject (
296     UINT64                  InitialValue)
297 {
298     ACPI_OPERAND_OBJECT     *IntegerDesc;
299 
300 
301     ACPI_FUNCTION_TRACE (UtCreateIntegerObject);
302 
303 
304     /* Create and initialize a new integer object */
305 
306     IntegerDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
307     if (!IntegerDesc)
308     {
309         return_PTR (NULL);
310     }
311 
312     IntegerDesc->Integer.Value = InitialValue;
313     return_PTR (IntegerDesc);
314 }
315 
316 
317 /*******************************************************************************
318  *
319  * FUNCTION:    AcpiUtCreateBufferObject
320  *
321  * PARAMETERS:  BufferSize             - Size of buffer to be created
322  *
323  * RETURN:      Pointer to a new Buffer object, null on failure
324  *
325  * DESCRIPTION: Create a fully initialized buffer object
326  *
327  ******************************************************************************/
328 
329 ACPI_OPERAND_OBJECT *
330 AcpiUtCreateBufferObject (
331     ACPI_SIZE               BufferSize)
332 {
333     ACPI_OPERAND_OBJECT     *BufferDesc;
334     UINT8                   *Buffer = NULL;
335 
336 
337     ACPI_FUNCTION_TRACE_U32 (UtCreateBufferObject, BufferSize);
338 
339 
340     /* Create a new Buffer object */
341 
342     BufferDesc = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
343     if (!BufferDesc)
344     {
345         return_PTR (NULL);
346     }
347 
348     /* Create an actual buffer only if size > 0 */
349 
350     if (BufferSize > 0)
351     {
352         /* Allocate the actual buffer */
353 
354         Buffer = ACPI_ALLOCATE_ZEROED (BufferSize);
355         if (!Buffer)
356         {
357             ACPI_ERROR ((AE_INFO, "Could not allocate size %u",
358                 (UINT32) BufferSize));
359 
360             AcpiUtRemoveReference (BufferDesc);
361             return_PTR (NULL);
362         }
363     }
364 
365     /* Complete buffer object initialization */
366 
367     BufferDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
368     BufferDesc->Buffer.Pointer = Buffer;
369     BufferDesc->Buffer.Length = (UINT32) BufferSize;
370 
371     /* Return the new buffer descriptor */
372 
373     return_PTR (BufferDesc);
374 }
375 
376 
377 /*******************************************************************************
378  *
379  * FUNCTION:    AcpiUtCreateStringObject
380  *
381  * PARAMETERS:  StringSize          - Size of string to be created. Does not
382  *                                    include NULL terminator, this is added
383  *                                    automatically.
384  *
385  * RETURN:      Pointer to a new String object
386  *
387  * DESCRIPTION: Create a fully initialized string object
388  *
389  ******************************************************************************/
390 
391 ACPI_OPERAND_OBJECT *
392 AcpiUtCreateStringObject (
393     ACPI_SIZE               StringSize)
394 {
395     ACPI_OPERAND_OBJECT     *StringDesc;
396     char                    *String;
397 
398 
399     ACPI_FUNCTION_TRACE_U32 (UtCreateStringObject, StringSize);
400 
401 
402     /* Create a new String object */
403 
404     StringDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
405     if (!StringDesc)
406     {
407         return_PTR (NULL);
408     }
409 
410     /*
411      * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
412      * NOTE: Zero-length strings are NULL terminated
413      */
414     String = ACPI_ALLOCATE_ZEROED (StringSize + 1);
415     if (!String)
416     {
417         ACPI_ERROR ((AE_INFO, "Could not allocate size %u",
418             (UINT32) StringSize));
419 
420         AcpiUtRemoveReference (StringDesc);
421         return_PTR (NULL);
422     }
423 
424     /* Complete string object initialization */
425 
426     StringDesc->String.Pointer = String;
427     StringDesc->String.Length = (UINT32) StringSize;
428 
429     /* Return the new string descriptor */
430 
431     return_PTR (StringDesc);
432 }
433 
434 
435 /*******************************************************************************
436  *
437  * FUNCTION:    AcpiUtValidInternalObject
438  *
439  * PARAMETERS:  Object              - Object to be validated
440  *
441  * RETURN:      TRUE if object is valid, FALSE otherwise
442  *
443  * DESCRIPTION: Validate a pointer to be of type ACPI_OPERAND_OBJECT
444  *
445  ******************************************************************************/
446 
447 BOOLEAN
448 AcpiUtValidInternalObject (
449     void                    *Object)
450 {
451 
452     ACPI_FUNCTION_NAME (UtValidInternalObject);
453 
454 
455     /* Check for a null pointer */
456 
457     if (!Object)
458     {
459         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "**** Null Object Ptr\n"));
460         return (FALSE);
461     }
462 
463     /* Check the descriptor type field */
464 
465     switch (ACPI_GET_DESCRIPTOR_TYPE (Object))
466     {
467     case ACPI_DESC_TYPE_OPERAND:
468 
469         /* The object appears to be a valid ACPI_OPERAND_OBJECT */
470 
471         return (TRUE);
472 
473     default:
474 
475         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
476             "%p is not an ACPI operand obj [%s]\n",
477             Object, AcpiUtGetDescriptorName (Object)));
478         break;
479     }
480 
481     return (FALSE);
482 }
483 
484 
485 /*******************************************************************************
486  *
487  * FUNCTION:    AcpiUtAllocateObjectDescDbg
488  *
489  * PARAMETERS:  ModuleName          - Caller's module name (for error output)
490  *              LineNumber          - Caller's line number (for error output)
491  *              ComponentId         - Caller's component ID (for error output)
492  *
493  * RETURN:      Pointer to newly allocated object descriptor. Null on error
494  *
495  * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
496  *              error conditions.
497  *
498  ******************************************************************************/
499 
500 void *
501 AcpiUtAllocateObjectDescDbg (
502     const char              *ModuleName,
503     UINT32                  LineNumber,
504     UINT32                  ComponentId)
505 {
506     ACPI_OPERAND_OBJECT     *Object;
507 
508 
509     ACPI_FUNCTION_TRACE (UtAllocateObjectDescDbg);
510 
511 
512     Object = AcpiOsAcquireObject (AcpiGbl_OperandCache);
513     if (!Object)
514     {
515         ACPI_ERROR ((ModuleName, LineNumber,
516             "Could not allocate an object descriptor"));
517 
518         return_PTR (NULL);
519     }
520 
521     /* Mark the descriptor type */
522 
523     ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_OPERAND);
524 
525     ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
526         Object, (UINT32) sizeof (ACPI_OPERAND_OBJECT)));
527 
528     return_PTR (Object);
529 }
530 
531 
532 /*******************************************************************************
533  *
534  * FUNCTION:    AcpiUtDeleteObjectDesc
535  *
536  * PARAMETERS:  Object          - An Acpi internal object to be deleted
537  *
538  * RETURN:      None.
539  *
540  * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
541  *
542  ******************************************************************************/
543 
544 void
545 AcpiUtDeleteObjectDesc (
546     ACPI_OPERAND_OBJECT     *Object)
547 {
548     ACPI_FUNCTION_TRACE_PTR (UtDeleteObjectDesc, Object);
549 
550 
551     /* Object must be of type ACPI_OPERAND_OBJECT */
552 
553     if (ACPI_GET_DESCRIPTOR_TYPE (Object) != ACPI_DESC_TYPE_OPERAND)
554     {
555         ACPI_ERROR ((AE_INFO,
556             "%p is not an ACPI Operand object [%s]", Object,
557             AcpiUtGetDescriptorName (Object)));
558         return_VOID;
559     }
560 
561     (void) AcpiOsReleaseObject (AcpiGbl_OperandCache, Object);
562     return_VOID;
563 }
564 
565 
566 /*******************************************************************************
567  *
568  * FUNCTION:    AcpiUtGetSimpleObjectSize
569  *
570  * PARAMETERS:  InternalObject     - An ACPI operand object
571  *              ObjLength          - Where the length is returned
572  *
573  * RETURN:      Status
574  *
575  * DESCRIPTION: This function is called to determine the space required to
576  *              contain a simple object for return to an external user.
577  *
578  *              The length includes the object structure plus any additional
579  *              needed space.
580  *
581  ******************************************************************************/
582 
583 static ACPI_STATUS
584 AcpiUtGetSimpleObjectSize (
585     ACPI_OPERAND_OBJECT     *InternalObject,
586     ACPI_SIZE               *ObjLength)
587 {
588     ACPI_SIZE               Length;
589     ACPI_SIZE               Size;
590     ACPI_STATUS             Status = AE_OK;
591 
592 
593     ACPI_FUNCTION_TRACE_PTR (UtGetSimpleObjectSize, InternalObject);
594 
595 
596     /* Start with the length of the (external) Acpi object */
597 
598     Length = sizeof (ACPI_OBJECT);
599 
600     /* A NULL object is allowed, can be a legal uninitialized package element */
601 
602     if (!InternalObject)
603     {
604         /*
605          * Object is NULL, just return the length of ACPI_OBJECT
606          * (A NULL ACPI_OBJECT is an object of all zeroes.)
607          */
608         *ObjLength = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
609         return_ACPI_STATUS (AE_OK);
610     }
611 
612     /* A Namespace Node should never appear here */
613 
614     if (ACPI_GET_DESCRIPTOR_TYPE (InternalObject) == ACPI_DESC_TYPE_NAMED)
615     {
616         /* A namespace node should never get here */
617 
618         return_ACPI_STATUS (AE_AML_INTERNAL);
619     }
620 
621     /*
622      * The final length depends on the object type
623      * Strings and Buffers are packed right up against the parent object and
624      * must be accessed bytewise or there may be alignment problems on
625      * certain processors
626      */
627     switch (InternalObject->Common.Type)
628     {
629     case ACPI_TYPE_STRING:
630 
631         Length += (ACPI_SIZE) InternalObject->String.Length + 1;
632         break;
633 
634     case ACPI_TYPE_BUFFER:
635 
636         Length += (ACPI_SIZE) InternalObject->Buffer.Length;
637         break;
638 
639     case ACPI_TYPE_INTEGER:
640     case ACPI_TYPE_PROCESSOR:
641     case ACPI_TYPE_POWER:
642 
643         /* No extra data for these types */
644 
645         break;
646 
647     case ACPI_TYPE_LOCAL_REFERENCE:
648 
649         switch (InternalObject->Reference.Class)
650         {
651         case ACPI_REFCLASS_NAME:
652             /*
653              * Get the actual length of the full pathname to this object.
654              * The reference will be converted to the pathname to the object
655              */
656             Size = AcpiNsGetPathnameLength (InternalObject->Reference.Node);
657             if (!Size)
658             {
659                 return_ACPI_STATUS (AE_BAD_PARAMETER);
660             }
661 
662             Length += ACPI_ROUND_UP_TO_NATIVE_WORD (Size);
663             break;
664 
665         default:
666             /*
667              * No other reference opcodes are supported.
668              * Notably, Locals and Args are not supported, but this may be
669              * required eventually.
670              */
671             ACPI_ERROR ((AE_INFO, "Cannot convert to external object - "
672                 "unsupported Reference Class [%s] 0x%X in object %p",
673                 AcpiUtGetReferenceName (InternalObject),
674                 InternalObject->Reference.Class, InternalObject));
675             Status = AE_TYPE;
676             break;
677         }
678         break;
679 
680     default:
681 
682         ACPI_ERROR ((AE_INFO, "Cannot convert to external object - "
683             "unsupported type [%s] 0x%X in object %p",
684             AcpiUtGetObjectTypeName (InternalObject),
685             InternalObject->Common.Type, InternalObject));
686         Status = AE_TYPE;
687         break;
688     }
689 
690     /*
691      * Account for the space required by the object rounded up to the next
692      * multiple of the machine word size. This keeps each object aligned
693      * on a machine word boundary. (preventing alignment faults on some
694      * machines.)
695      */
696     *ObjLength = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
697     return_ACPI_STATUS (Status);
698 }
699 
700 
701 /*******************************************************************************
702  *
703  * FUNCTION:    AcpiUtGetElementLength
704  *
705  * PARAMETERS:  ACPI_PKG_CALLBACK
706  *
707  * RETURN:      Status
708  *
709  * DESCRIPTION: Get the length of one package element.
710  *
711  ******************************************************************************/
712 
713 static ACPI_STATUS
714 AcpiUtGetElementLength (
715     UINT8                   ObjectType,
716     ACPI_OPERAND_OBJECT     *SourceObject,
717     ACPI_GENERIC_STATE      *State,
718     void                    *Context)
719 {
720     ACPI_STATUS             Status = AE_OK;
721     ACPI_PKG_INFO           *Info = (ACPI_PKG_INFO *) Context;
722     ACPI_SIZE               ObjectSpace;
723 
724 
725     switch (ObjectType)
726     {
727     case ACPI_COPY_TYPE_SIMPLE:
728         /*
729          * Simple object - just get the size (Null object/entry is handled
730          * here also) and sum it into the running package length
731          */
732         Status = AcpiUtGetSimpleObjectSize (SourceObject, &ObjectSpace);
733         if (ACPI_FAILURE (Status))
734         {
735             return (Status);
736         }
737 
738         Info->Length += ObjectSpace;
739         break;
740 
741     case ACPI_COPY_TYPE_PACKAGE:
742 
743         /* Package object - nothing much to do here, let the walk handle it */
744 
745         Info->NumPackages++;
746         State->Pkg.ThisTargetObj = NULL;
747         break;
748 
749     default:
750 
751         /* No other types allowed */
752 
753         return (AE_BAD_PARAMETER);
754     }
755 
756     return (Status);
757 }
758 
759 
760 /*******************************************************************************
761  *
762  * FUNCTION:    AcpiUtGetPackageObjectSize
763  *
764  * PARAMETERS:  InternalObject      - An ACPI internal object
765  *              ObjLength           - Where the length is returned
766  *
767  * RETURN:      Status
768  *
769  * DESCRIPTION: This function is called to determine the space required to
770  *              contain a package object for return to an external user.
771  *
772  *              This is moderately complex since a package contains other
773  *              objects including packages.
774  *
775  ******************************************************************************/
776 
777 static ACPI_STATUS
778 AcpiUtGetPackageObjectSize (
779     ACPI_OPERAND_OBJECT     *InternalObject,
780     ACPI_SIZE               *ObjLength)
781 {
782     ACPI_STATUS             Status;
783     ACPI_PKG_INFO           Info;
784 
785 
786     ACPI_FUNCTION_TRACE_PTR (UtGetPackageObjectSize, InternalObject);
787 
788 
789     Info.Length = 0;
790     Info.ObjectSpace = 0;
791     Info.NumPackages = 1;
792 
793     Status = AcpiUtWalkPackageTree (
794         InternalObject, NULL, AcpiUtGetElementLength, &Info);
795     if (ACPI_FAILURE (Status))
796     {
797         return_ACPI_STATUS (Status);
798     }
799 
800     /*
801      * We have handled all of the objects in all levels of the package.
802      * just add the length of the package objects themselves.
803      * Round up to the next machine word.
804      */
805     Info.Length += ACPI_ROUND_UP_TO_NATIVE_WORD (
806         sizeof (ACPI_OBJECT)) * (ACPI_SIZE) Info.NumPackages;
807 
808     /* Return the total package length */
809 
810     *ObjLength = Info.Length;
811     return_ACPI_STATUS (Status);
812 }
813 
814 
815 /*******************************************************************************
816  *
817  * FUNCTION:    AcpiUtGetObjectSize
818  *
819  * PARAMETERS:  InternalObject      - An ACPI internal object
820  *              ObjLength           - Where the length will be returned
821  *
822  * RETURN:      Status
823  *
824  * DESCRIPTION: This function is called to determine the space required to
825  *              contain an object for return to an API user.
826  *
827  ******************************************************************************/
828 
829 ACPI_STATUS
830 AcpiUtGetObjectSize (
831     ACPI_OPERAND_OBJECT     *InternalObject,
832     ACPI_SIZE               *ObjLength)
833 {
834     ACPI_STATUS             Status;
835 
836 
837     ACPI_FUNCTION_ENTRY ();
838 
839 
840     if ((ACPI_GET_DESCRIPTOR_TYPE (InternalObject) ==
841             ACPI_DESC_TYPE_OPERAND) &&
842         (InternalObject->Common.Type == ACPI_TYPE_PACKAGE))
843     {
844         Status = AcpiUtGetPackageObjectSize (InternalObject, ObjLength);
845     }
846     else
847     {
848         Status = AcpiUtGetSimpleObjectSize (InternalObject, ObjLength);
849     }
850 
851     return (Status);
852 }
853