1 /******************************************************************************
3 * Module Name: utobject - ACPI object create/delete/size/cache routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utobject")
52 /* Local prototypes */
55 AcpiUtGetSimpleObjectSize (
56 ACPI_OPERAND_OBJECT
*Obj
,
57 ACPI_SIZE
*ObjLength
);
60 AcpiUtGetPackageObjectSize (
61 ACPI_OPERAND_OBJECT
*Obj
,
62 ACPI_SIZE
*ObjLength
);
65 AcpiUtGetElementLength (
67 ACPI_OPERAND_OBJECT
*SourceObject
,
68 ACPI_GENERIC_STATE
*State
,
72 /*******************************************************************************
74 * FUNCTION: AcpiUtCreateInternalObjectDbg
76 * PARAMETERS: ModuleName - Source file name of caller
77 * LineNumber - Line number of caller
78 * ComponentId - Component type of caller
79 * Type - ACPI Type of the new object
81 * RETURN: A new internal object, null on failure
83 * DESCRIPTION: Create and initialize a new internal object.
85 * NOTE: We always allocate the worst-case object descriptor because
86 * these objects are cached, and we want them to be
87 * one-size-satisifies-any-request. This in itself may not be
88 * the most memory efficient, but the efficiency of the object
89 * cache should more than make up for this!
91 ******************************************************************************/
94 AcpiUtCreateInternalObjectDbg (
95 const char *ModuleName
,
98 ACPI_OBJECT_TYPE Type
)
100 ACPI_OPERAND_OBJECT
*Object
;
101 ACPI_OPERAND_OBJECT
*SecondObject
;
104 ACPI_FUNCTION_TRACE_STR (UtCreateInternalObjectDbg
,
105 AcpiUtGetTypeName (Type
));
108 /* Allocate the raw object descriptor */
110 Object
= AcpiUtAllocateObjectDescDbg (
111 ModuleName
, LineNumber
, ComponentId
);
119 case ACPI_TYPE_REGION
:
120 case ACPI_TYPE_BUFFER_FIELD
:
121 case ACPI_TYPE_LOCAL_BANK_FIELD
:
123 /* These types require a secondary object */
125 SecondObject
= AcpiUtAllocateObjectDescDbg (
126 ModuleName
, LineNumber
, ComponentId
);
129 AcpiUtDeleteObjectDesc (Object
);
133 SecondObject
->Common
.Type
= ACPI_TYPE_LOCAL_EXTRA
;
134 SecondObject
->Common
.ReferenceCount
= 1;
136 /* Link the second object to the first */
138 Object
->Common
.NextObject
= SecondObject
;
143 /* All others have no secondary object */
147 /* Save the object type in the object descriptor */
149 Object
->Common
.Type
= (UINT8
) Type
;
151 /* Init the reference count */
153 Object
->Common
.ReferenceCount
= 1;
155 /* Any per-type initialization should go here */
161 /*******************************************************************************
163 * FUNCTION: AcpiUtCreatePackageObject
165 * PARAMETERS: Count - Number of package elements
167 * RETURN: Pointer to a new Package object, null on failure
169 * DESCRIPTION: Create a fully initialized package object
171 ******************************************************************************/
173 ACPI_OPERAND_OBJECT
*
174 AcpiUtCreatePackageObject (
177 ACPI_OPERAND_OBJECT
*PackageDesc
;
178 ACPI_OPERAND_OBJECT
**PackageElements
;
181 ACPI_FUNCTION_TRACE_U32 (UtCreatePackageObject
, Count
);
184 /* Create a new Package object */
186 PackageDesc
= AcpiUtCreateInternalObject (ACPI_TYPE_PACKAGE
);
193 * Create the element array. Count+1 allows the array to be null
196 PackageElements
= ACPI_ALLOCATE_ZEROED (
197 ((ACPI_SIZE
) Count
+ 1) * sizeof (void *));
198 if (!PackageElements
)
200 ACPI_FREE (PackageDesc
);
204 PackageDesc
->Package
.Count
= Count
;
205 PackageDesc
->Package
.Elements
= PackageElements
;
206 return_PTR (PackageDesc
);
210 /*******************************************************************************
212 * FUNCTION: AcpiUtCreateIntegerObject
214 * PARAMETERS: InitialValue - Initial value for the integer
216 * RETURN: Pointer to a new Integer object, null on failure
218 * DESCRIPTION: Create an initialized integer object
220 ******************************************************************************/
222 ACPI_OPERAND_OBJECT
*
223 AcpiUtCreateIntegerObject (
226 ACPI_OPERAND_OBJECT
*IntegerDesc
;
229 ACPI_FUNCTION_TRACE (UtCreateIntegerObject
);
232 /* Create and initialize a new integer object */
234 IntegerDesc
= AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER
);
240 IntegerDesc
->Integer
.Value
= InitialValue
;
241 return_PTR (IntegerDesc
);
245 /*******************************************************************************
247 * FUNCTION: AcpiUtCreateBufferObject
249 * PARAMETERS: BufferSize - Size of buffer to be created
251 * RETURN: Pointer to a new Buffer object, null on failure
253 * DESCRIPTION: Create a fully initialized buffer object
255 ******************************************************************************/
257 ACPI_OPERAND_OBJECT
*
258 AcpiUtCreateBufferObject (
259 ACPI_SIZE BufferSize
)
261 ACPI_OPERAND_OBJECT
*BufferDesc
;
262 UINT8
*Buffer
= NULL
;
265 ACPI_FUNCTION_TRACE_U32 (UtCreateBufferObject
, BufferSize
);
268 /* Create a new Buffer object */
270 BufferDesc
= AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER
);
276 /* Create an actual buffer only if size > 0 */
280 /* Allocate the actual buffer */
282 Buffer
= ACPI_ALLOCATE_ZEROED (BufferSize
);
285 ACPI_ERROR ((AE_INFO
, "Could not allocate size %u",
286 (UINT32
) BufferSize
));
288 AcpiUtRemoveReference (BufferDesc
);
293 /* Complete buffer object initialization */
295 BufferDesc
->Buffer
.Flags
|= AOPOBJ_DATA_VALID
;
296 BufferDesc
->Buffer
.Pointer
= Buffer
;
297 BufferDesc
->Buffer
.Length
= (UINT32
) BufferSize
;
299 /* Return the new buffer descriptor */
301 return_PTR (BufferDesc
);
305 /*******************************************************************************
307 * FUNCTION: AcpiUtCreateStringObject
309 * PARAMETERS: StringSize - Size of string to be created. Does not
310 * include NULL terminator, this is added
313 * RETURN: Pointer to a new String object
315 * DESCRIPTION: Create a fully initialized string object
317 ******************************************************************************/
319 ACPI_OPERAND_OBJECT
*
320 AcpiUtCreateStringObject (
321 ACPI_SIZE StringSize
)
323 ACPI_OPERAND_OBJECT
*StringDesc
;
327 ACPI_FUNCTION_TRACE_U32 (UtCreateStringObject
, StringSize
);
330 /* Create a new String object */
332 StringDesc
= AcpiUtCreateInternalObject (ACPI_TYPE_STRING
);
339 * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
340 * NOTE: Zero-length strings are NULL terminated
342 String
= ACPI_ALLOCATE_ZEROED (StringSize
+ 1);
345 ACPI_ERROR ((AE_INFO
, "Could not allocate size %u",
346 (UINT32
) StringSize
));
348 AcpiUtRemoveReference (StringDesc
);
352 /* Complete string object initialization */
354 StringDesc
->String
.Pointer
= String
;
355 StringDesc
->String
.Length
= (UINT32
) StringSize
;
357 /* Return the new string descriptor */
359 return_PTR (StringDesc
);
363 /*******************************************************************************
365 * FUNCTION: AcpiUtValidInternalObject
367 * PARAMETERS: Object - Object to be validated
369 * RETURN: TRUE if object is valid, FALSE otherwise
371 * DESCRIPTION: Validate a pointer to be of type ACPI_OPERAND_OBJECT
373 ******************************************************************************/
376 AcpiUtValidInternalObject (
380 ACPI_FUNCTION_NAME (UtValidInternalObject
);
383 /* Check for a null pointer */
387 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "**** Null Object Ptr\n"));
391 /* Check the descriptor type field */
393 switch (ACPI_GET_DESCRIPTOR_TYPE (Object
))
395 case ACPI_DESC_TYPE_OPERAND
:
397 /* The object appears to be a valid ACPI_OPERAND_OBJECT */
403 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
,
404 "%p is not an ACPI operand obj [%s]\n",
405 Object
, AcpiUtGetDescriptorName (Object
)));
413 /*******************************************************************************
415 * FUNCTION: AcpiUtAllocateObjectDescDbg
417 * PARAMETERS: ModuleName - Caller's module name (for error output)
418 * LineNumber - Caller's line number (for error output)
419 * ComponentId - Caller's component ID (for error output)
421 * RETURN: Pointer to newly allocated object descriptor. Null on error
423 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
426 ******************************************************************************/
429 AcpiUtAllocateObjectDescDbg (
430 const char *ModuleName
,
434 ACPI_OPERAND_OBJECT
*Object
;
437 ACPI_FUNCTION_TRACE (UtAllocateObjectDescDbg
);
440 Object
= AcpiOsAcquireObject (AcpiGbl_OperandCache
);
443 ACPI_ERROR ((ModuleName
, LineNumber
,
444 "Could not allocate an object descriptor"));
449 /* Mark the descriptor type */
451 ACPI_SET_DESCRIPTOR_TYPE (Object
, ACPI_DESC_TYPE_OPERAND
);
453 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "%p Size %X\n",
454 Object
, (UINT32
) sizeof (ACPI_OPERAND_OBJECT
)));
460 /*******************************************************************************
462 * FUNCTION: AcpiUtDeleteObjectDesc
464 * PARAMETERS: Object - An Acpi internal object to be deleted
468 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
470 ******************************************************************************/
473 AcpiUtDeleteObjectDesc (
474 ACPI_OPERAND_OBJECT
*Object
)
476 ACPI_FUNCTION_TRACE_PTR (UtDeleteObjectDesc
, Object
);
479 /* Object must be of type ACPI_OPERAND_OBJECT */
481 if (ACPI_GET_DESCRIPTOR_TYPE (Object
) != ACPI_DESC_TYPE_OPERAND
)
483 ACPI_ERROR ((AE_INFO
,
484 "%p is not an ACPI Operand object [%s]", Object
,
485 AcpiUtGetDescriptorName (Object
)));
489 (void) AcpiOsReleaseObject (AcpiGbl_OperandCache
, Object
);
494 /*******************************************************************************
496 * FUNCTION: AcpiUtGetSimpleObjectSize
498 * PARAMETERS: InternalObject - An ACPI operand object
499 * ObjLength - Where the length is returned
503 * DESCRIPTION: This function is called to determine the space required to
504 * contain a simple object for return to an external user.
506 * The length includes the object structure plus any additional
509 ******************************************************************************/
512 AcpiUtGetSimpleObjectSize (
513 ACPI_OPERAND_OBJECT
*InternalObject
,
514 ACPI_SIZE
*ObjLength
)
518 ACPI_STATUS Status
= AE_OK
;
521 ACPI_FUNCTION_TRACE_PTR (UtGetSimpleObjectSize
, InternalObject
);
524 /* Start with the length of the (external) Acpi object */
526 Length
= sizeof (ACPI_OBJECT
);
528 /* A NULL object is allowed, can be a legal uninitialized package element */
533 * Object is NULL, just return the length of ACPI_OBJECT
534 * (A NULL ACPI_OBJECT is an object of all zeroes.)
536 *ObjLength
= ACPI_ROUND_UP_TO_NATIVE_WORD (Length
);
537 return_ACPI_STATUS (AE_OK
);
540 /* A Namespace Node should never appear here */
542 if (ACPI_GET_DESCRIPTOR_TYPE (InternalObject
) == ACPI_DESC_TYPE_NAMED
)
544 /* A namespace node should never get here */
546 return_ACPI_STATUS (AE_AML_INTERNAL
);
550 * The final length depends on the object type
551 * Strings and Buffers are packed right up against the parent object and
552 * must be accessed bytewise or there may be alignment problems on
555 switch (InternalObject
->Common
.Type
)
557 case ACPI_TYPE_STRING
:
559 Length
+= (ACPI_SIZE
) InternalObject
->String
.Length
+ 1;
562 case ACPI_TYPE_BUFFER
:
564 Length
+= (ACPI_SIZE
) InternalObject
->Buffer
.Length
;
567 case ACPI_TYPE_INTEGER
:
568 case ACPI_TYPE_PROCESSOR
:
569 case ACPI_TYPE_POWER
:
571 /* No extra data for these types */
575 case ACPI_TYPE_LOCAL_REFERENCE
:
577 switch (InternalObject
->Reference
.Class
)
579 case ACPI_REFCLASS_NAME
:
581 * Get the actual length of the full pathname to this object.
582 * The reference will be converted to the pathname to the object
584 Size
= AcpiNsGetPathnameLength (InternalObject
->Reference
.Node
);
587 return_ACPI_STATUS (AE_BAD_PARAMETER
);
590 Length
+= ACPI_ROUND_UP_TO_NATIVE_WORD (Size
);
595 * No other reference opcodes are supported.
596 * Notably, Locals and Args are not supported, but this may be
597 * required eventually.
599 ACPI_ERROR ((AE_INFO
, "Cannot convert to external object - "
600 "unsupported Reference Class [%s] 0x%X in object %p",
601 AcpiUtGetReferenceName (InternalObject
),
602 InternalObject
->Reference
.Class
, InternalObject
));
610 ACPI_ERROR ((AE_INFO
, "Cannot convert to external object - "
611 "unsupported type [%s] 0x%X in object %p",
612 AcpiUtGetObjectTypeName (InternalObject
),
613 InternalObject
->Common
.Type
, InternalObject
));
619 * Account for the space required by the object rounded up to the next
620 * multiple of the machine word size. This keeps each object aligned
621 * on a machine word boundary. (preventing alignment faults on some
624 *ObjLength
= ACPI_ROUND_UP_TO_NATIVE_WORD (Length
);
625 return_ACPI_STATUS (Status
);
629 /*******************************************************************************
631 * FUNCTION: AcpiUtGetElementLength
633 * PARAMETERS: ACPI_PKG_CALLBACK
637 * DESCRIPTION: Get the length of one package element.
639 ******************************************************************************/
642 AcpiUtGetElementLength (
644 ACPI_OPERAND_OBJECT
*SourceObject
,
645 ACPI_GENERIC_STATE
*State
,
648 ACPI_STATUS Status
= AE_OK
;
649 ACPI_PKG_INFO
*Info
= (ACPI_PKG_INFO
*) Context
;
650 ACPI_SIZE ObjectSpace
;
655 case ACPI_COPY_TYPE_SIMPLE
:
657 * Simple object - just get the size (Null object/entry is handled
658 * here also) and sum it into the running package length
660 Status
= AcpiUtGetSimpleObjectSize (SourceObject
, &ObjectSpace
);
661 if (ACPI_FAILURE (Status
))
666 Info
->Length
+= ObjectSpace
;
669 case ACPI_COPY_TYPE_PACKAGE
:
671 /* Package object - nothing much to do here, let the walk handle it */
674 State
->Pkg
.ThisTargetObj
= NULL
;
679 /* No other types allowed */
681 return (AE_BAD_PARAMETER
);
688 /*******************************************************************************
690 * FUNCTION: AcpiUtGetPackageObjectSize
692 * PARAMETERS: InternalObject - An ACPI internal object
693 * ObjLength - Where the length is returned
697 * DESCRIPTION: This function is called to determine the space required to
698 * contain a package object for return to an external user.
700 * This is moderately complex since a package contains other
701 * objects including packages.
703 ******************************************************************************/
706 AcpiUtGetPackageObjectSize (
707 ACPI_OPERAND_OBJECT
*InternalObject
,
708 ACPI_SIZE
*ObjLength
)
714 ACPI_FUNCTION_TRACE_PTR (UtGetPackageObjectSize
, InternalObject
);
718 Info
.ObjectSpace
= 0;
719 Info
.NumPackages
= 1;
721 Status
= AcpiUtWalkPackageTree (
722 InternalObject
, NULL
, AcpiUtGetElementLength
, &Info
);
723 if (ACPI_FAILURE (Status
))
725 return_ACPI_STATUS (Status
);
729 * We have handled all of the objects in all levels of the package.
730 * just add the length of the package objects themselves.
731 * Round up to the next machine word.
733 Info
.Length
+= ACPI_ROUND_UP_TO_NATIVE_WORD (
734 sizeof (ACPI_OBJECT
)) * (ACPI_SIZE
) Info
.NumPackages
;
736 /* Return the total package length */
738 *ObjLength
= Info
.Length
;
739 return_ACPI_STATUS (Status
);
743 /*******************************************************************************
745 * FUNCTION: AcpiUtGetObjectSize
747 * PARAMETERS: InternalObject - An ACPI internal object
748 * ObjLength - Where the length will be returned
752 * DESCRIPTION: This function is called to determine the space required to
753 * contain an object for return to an API user.
755 ******************************************************************************/
758 AcpiUtGetObjectSize (
759 ACPI_OPERAND_OBJECT
*InternalObject
,
760 ACPI_SIZE
*ObjLength
)
765 ACPI_FUNCTION_ENTRY ();
768 if ((ACPI_GET_DESCRIPTOR_TYPE (InternalObject
) ==
769 ACPI_DESC_TYPE_OPERAND
) &&
770 (InternalObject
->Common
.Type
== ACPI_TYPE_PACKAGE
))
772 Status
= AcpiUtGetPackageObjectSize (InternalObject
, ObjLength
);
776 Status
= AcpiUtGetSimpleObjectSize (InternalObject
, ObjLength
);