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 ******************************************************************************/
93 #pragma GCC diagnostic push
94 #pragma GCC diagnostic ignored "-Wcast-qual"
97 AcpiUtCreateInternalObjectDbg (
98 const char *ModuleName,
101 ACPI_OBJECT_TYPE Type)
103 ACPI_OPERAND_OBJECT *Object;
104 ACPI_OPERAND_OBJECT *SecondObject;
107 ACPI_FUNCTION_TRACE_STR (UtCreateInternalObjectDbg,
108 AcpiUtGetTypeName (Type));
111 /* Allocate the raw object descriptor */
113 Object = AcpiUtAllocateObjectDescDbg (
114 ModuleName, LineNumber, ComponentId);
122 case ACPI_TYPE_REGION:
123 case ACPI_TYPE_BUFFER_FIELD:
124 case ACPI_TYPE_LOCAL_BANK_FIELD:
126 /* These types require a secondary object */
128 SecondObject = AcpiUtAllocateObjectDescDbg (
129 ModuleName, LineNumber, ComponentId);
132 AcpiUtDeleteObjectDesc (Object);
136 SecondObject->Common.Type = ACPI_TYPE_LOCAL_EXTRA;
137 SecondObject->Common.ReferenceCount = 1;
139 /* Link the second object to the first */
141 Object->Common.NextObject = SecondObject;
146 /* All others have no secondary object */
150 /* Save the object type in the object descriptor */
152 Object->Common.Type = (UINT8) Type;
154 /* Init the reference count */
156 Object->Common.ReferenceCount = 1;
158 /* Any per-type initialization should go here */
163 #pragma GCC diagnostic pop
166 /*******************************************************************************
168 * FUNCTION: AcpiUtCreatePackageObject
170 * PARAMETERS: Count - Number of package elements
172 * RETURN: Pointer to a new Package object, null on failure
174 * DESCRIPTION: Create a fully initialized package object
176 ******************************************************************************/
178 ACPI_OPERAND_OBJECT *
179 AcpiUtCreatePackageObject (
182 ACPI_OPERAND_OBJECT *PackageDesc;
183 ACPI_OPERAND_OBJECT **PackageElements;
186 ACPI_FUNCTION_TRACE_U32 (UtCreatePackageObject, Count);
189 /* Create a new Package object */
191 PackageDesc = AcpiUtCreateInternalObject (ACPI_TYPE_PACKAGE);
198 * Create the element array. Count+1 allows the array to be null
201 PackageElements = ACPI_ALLOCATE_ZEROED (
202 ((ACPI_SIZE) Count + 1) * sizeof (void *));
203 if (!PackageElements)
205 ACPI_FREE (PackageDesc);
209 PackageDesc->Package.Count = Count;
210 PackageDesc->Package.Elements = PackageElements;
211 return_PTR (PackageDesc);
215 /*******************************************************************************
217 * FUNCTION: AcpiUtCreateIntegerObject
219 * PARAMETERS: InitialValue - Initial value for the integer
221 * RETURN: Pointer to a new Integer object, null on failure
223 * DESCRIPTION: Create an initialized integer object
225 ******************************************************************************/
227 ACPI_OPERAND_OBJECT *
228 AcpiUtCreateIntegerObject (
231 ACPI_OPERAND_OBJECT *IntegerDesc;
234 ACPI_FUNCTION_TRACE (UtCreateIntegerObject);
237 /* Create and initialize a new integer object */
239 IntegerDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
245 IntegerDesc->Integer.Value = InitialValue;
246 return_PTR (IntegerDesc);
250 /*******************************************************************************
252 * FUNCTION: AcpiUtCreateBufferObject
254 * PARAMETERS: BufferSize - Size of buffer to be created
256 * RETURN: Pointer to a new Buffer object, null on failure
258 * DESCRIPTION: Create a fully initialized buffer object
260 ******************************************************************************/
262 ACPI_OPERAND_OBJECT *
263 AcpiUtCreateBufferObject (
264 ACPI_SIZE BufferSize)
266 ACPI_OPERAND_OBJECT *BufferDesc;
267 UINT8 *Buffer = NULL;
270 ACPI_FUNCTION_TRACE_U32 (UtCreateBufferObject, BufferSize);
273 /* Create a new Buffer object */
275 BufferDesc = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
281 /* Create an actual buffer only if size > 0 */
285 /* Allocate the actual buffer */
287 Buffer = ACPI_ALLOCATE_ZEROED (BufferSize);
290 ACPI_ERROR ((AE_INFO, "Could not allocate size %u",
291 (UINT32) BufferSize));
293 AcpiUtRemoveReference (BufferDesc);
298 /* Complete buffer object initialization */
300 BufferDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
301 BufferDesc->Buffer.Pointer = Buffer;
302 BufferDesc->Buffer.Length = (UINT32) BufferSize;
304 /* Return the new buffer descriptor */
306 return_PTR (BufferDesc);
310 /*******************************************************************************
312 * FUNCTION: AcpiUtCreateStringObject
314 * PARAMETERS: StringSize - Size of string to be created. Does not
315 * include NULL terminator, this is added
318 * RETURN: Pointer to a new String object
320 * DESCRIPTION: Create a fully initialized string object
322 ******************************************************************************/
324 ACPI_OPERAND_OBJECT *
325 AcpiUtCreateStringObject (
326 ACPI_SIZE StringSize)
328 ACPI_OPERAND_OBJECT *StringDesc;
332 ACPI_FUNCTION_TRACE_U32 (UtCreateStringObject, StringSize);
335 /* Create a new String object */
337 StringDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
344 * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
345 * NOTE: Zero-length strings are NULL terminated
347 String = ACPI_ALLOCATE_ZEROED (StringSize + 1);
350 ACPI_ERROR ((AE_INFO, "Could not allocate size %u",
351 (UINT32) StringSize));
353 AcpiUtRemoveReference (StringDesc);
357 /* Complete string object initialization */
359 StringDesc->String.Pointer = String;
360 StringDesc->String.Length = (UINT32) StringSize;
362 /* Return the new string descriptor */
364 return_PTR (StringDesc);
368 /*******************************************************************************
370 * FUNCTION: AcpiUtValidInternalObject
372 * PARAMETERS: Object - Object to be validated
374 * RETURN: TRUE if object is valid, FALSE otherwise
376 * DESCRIPTION: Validate a pointer to be of type ACPI_OPERAND_OBJECT
378 ******************************************************************************/
381 AcpiUtValidInternalObject (
385 ACPI_FUNCTION_NAME (UtValidInternalObject);
388 /* Check for a null pointer */
392 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "**** Null Object Ptr\n"));
396 /* Check the descriptor type field */
398 switch (ACPI_GET_DESCRIPTOR_TYPE (Object))
400 case ACPI_DESC_TYPE_OPERAND:
402 /* The object appears to be a valid ACPI_OPERAND_OBJECT */
408 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
409 "%p is not an ACPI operand obj [%s]\n",
410 Object, AcpiUtGetDescriptorName (Object)));
418 /*******************************************************************************
420 * FUNCTION: AcpiUtAllocateObjectDescDbg
422 * PARAMETERS: ModuleName - Caller's module name (for error output)
423 * LineNumber - Caller's line number (for error output)
424 * ComponentId - Caller's component ID (for error output)
426 * RETURN: Pointer to newly allocated object descriptor. Null on error
428 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
431 ******************************************************************************/
434 AcpiUtAllocateObjectDescDbg (
435 const char *ModuleName,
439 ACPI_OPERAND_OBJECT *Object;
442 ACPI_FUNCTION_TRACE (UtAllocateObjectDescDbg);
445 Object = AcpiOsAcquireObject (AcpiGbl_OperandCache);
448 ACPI_ERROR ((ModuleName, LineNumber,
449 "Could not allocate an object descriptor"));
454 /* Mark the descriptor type */
456 ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_OPERAND);
458 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
459 Object, (UINT32) sizeof (ACPI_OPERAND_OBJECT)));
465 /*******************************************************************************
467 * FUNCTION: AcpiUtDeleteObjectDesc
469 * PARAMETERS: Object - An Acpi internal object to be deleted
473 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
475 ******************************************************************************/
478 AcpiUtDeleteObjectDesc (
479 ACPI_OPERAND_OBJECT *Object)
481 ACPI_FUNCTION_TRACE_PTR (UtDeleteObjectDesc, Object);
484 /* Object must be of type ACPI_OPERAND_OBJECT */
486 if (ACPI_GET_DESCRIPTOR_TYPE (Object) != ACPI_DESC_TYPE_OPERAND)
488 ACPI_ERROR ((AE_INFO,
489 "%p is not an ACPI Operand object [%s]", Object,
490 AcpiUtGetDescriptorName (Object)));
494 (void) AcpiOsReleaseObject (AcpiGbl_OperandCache, Object);
499 /*******************************************************************************
501 * FUNCTION: AcpiUtGetSimpleObjectSize
503 * PARAMETERS: InternalObject - An ACPI operand object
504 * ObjLength - Where the length is returned
508 * DESCRIPTION: This function is called to determine the space required to
509 * contain a simple object for return to an external user.
511 * The length includes the object structure plus any additional
514 ******************************************************************************/
517 AcpiUtGetSimpleObjectSize (
518 ACPI_OPERAND_OBJECT *InternalObject,
519 ACPI_SIZE *ObjLength)
523 ACPI_STATUS Status = AE_OK;
526 ACPI_FUNCTION_TRACE_PTR (UtGetSimpleObjectSize, InternalObject);
529 /* Start with the length of the (external) Acpi object */
531 Length = sizeof (ACPI_OBJECT);
533 /* A NULL object is allowed, can be a legal uninitialized package element */
538 * Object is NULL, just return the length of ACPI_OBJECT
539 * (A NULL ACPI_OBJECT is an object of all zeroes.)
541 *ObjLength = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
542 return_ACPI_STATUS (AE_OK);
545 /* A Namespace Node should never appear here */
547 if (ACPI_GET_DESCRIPTOR_TYPE (InternalObject) == ACPI_DESC_TYPE_NAMED)
549 /* A namespace node should never get here */
551 return_ACPI_STATUS (AE_AML_INTERNAL);
555 * The final length depends on the object type
556 * Strings and Buffers are packed right up against the parent object and
557 * must be accessed bytewise or there may be alignment problems on
560 switch (InternalObject->Common.Type)
562 case ACPI_TYPE_STRING:
564 Length += (ACPI_SIZE) InternalObject->String.Length + 1;
567 case ACPI_TYPE_BUFFER:
569 Length += (ACPI_SIZE) InternalObject->Buffer.Length;
572 case ACPI_TYPE_INTEGER:
573 case ACPI_TYPE_PROCESSOR:
574 case ACPI_TYPE_POWER:
576 /* No extra data for these types */
580 case ACPI_TYPE_LOCAL_REFERENCE:
582 switch (InternalObject->Reference.Class)
584 case ACPI_REFCLASS_NAME:
586 * Get the actual length of the full pathname to this object.
587 * The reference will be converted to the pathname to the object
589 Size = AcpiNsGetPathnameLength (InternalObject->Reference.Node);
592 return_ACPI_STATUS (AE_BAD_PARAMETER);
595 Length += ACPI_ROUND_UP_TO_NATIVE_WORD (Size);
600 * No other reference opcodes are supported.
601 * Notably, Locals and Args are not supported, but this may be
602 * required eventually.
604 ACPI_ERROR ((AE_INFO, "Cannot convert to external object - "
605 "unsupported Reference Class [%s] 0x%X in object %p",
606 AcpiUtGetReferenceName (InternalObject),
607 InternalObject->Reference.Class, InternalObject));
615 ACPI_ERROR ((AE_INFO, "Cannot convert to external object - "
616 "unsupported type [%s] 0x%X in object %p",
617 AcpiUtGetObjectTypeName (InternalObject),
618 InternalObject->Common.Type, InternalObject));
624 * Account for the space required by the object rounded up to the next
625 * multiple of the machine word size. This keeps each object aligned
626 * on a machine word boundary. (preventing alignment faults on some
629 *ObjLength = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
630 return_ACPI_STATUS (Status);
634 /*******************************************************************************
636 * FUNCTION: AcpiUtGetElementLength
638 * PARAMETERS: ACPI_PKG_CALLBACK
642 * DESCRIPTION: Get the length of one package element.
644 ******************************************************************************/
647 AcpiUtGetElementLength (
649 ACPI_OPERAND_OBJECT *SourceObject,
650 ACPI_GENERIC_STATE *State,
653 ACPI_STATUS Status = AE_OK;
654 ACPI_PKG_INFO *Info = (ACPI_PKG_INFO *) Context;
655 ACPI_SIZE ObjectSpace;
660 case ACPI_COPY_TYPE_SIMPLE:
662 * Simple object - just get the size (Null object/entry is handled
663 * here also) and sum it into the running package length
665 Status = AcpiUtGetSimpleObjectSize (SourceObject, &ObjectSpace);
666 if (ACPI_FAILURE (Status))
671 Info->Length += ObjectSpace;
674 case ACPI_COPY_TYPE_PACKAGE:
676 /* Package object - nothing much to do here, let the walk handle it */
679 State->Pkg.ThisTargetObj = NULL;
684 /* No other types allowed */
686 return (AE_BAD_PARAMETER);
693 /*******************************************************************************
695 * FUNCTION: AcpiUtGetPackageObjectSize
697 * PARAMETERS: InternalObject - An ACPI internal object
698 * ObjLength - Where the length is returned
702 * DESCRIPTION: This function is called to determine the space required to
703 * contain a package object for return to an external user.
705 * This is moderately complex since a package contains other
706 * objects including packages.
708 ******************************************************************************/
711 AcpiUtGetPackageObjectSize (
712 ACPI_OPERAND_OBJECT *InternalObject,
713 ACPI_SIZE *ObjLength)
719 ACPI_FUNCTION_TRACE_PTR (UtGetPackageObjectSize, InternalObject);
723 Info.ObjectSpace = 0;
724 Info.NumPackages = 1;
726 Status = AcpiUtWalkPackageTree (
727 InternalObject, NULL, AcpiUtGetElementLength, &Info);
728 if (ACPI_FAILURE (Status))
730 return_ACPI_STATUS (Status);
734 * We have handled all of the objects in all levels of the package.
735 * just add the length of the package objects themselves.
736 * Round up to the next machine word.
738 Info.Length += ACPI_ROUND_UP_TO_NATIVE_WORD (
739 sizeof (ACPI_OBJECT)) * (ACPI_SIZE) Info.NumPackages;
741 /* Return the total package length */
743 *ObjLength = Info.Length;
744 return_ACPI_STATUS (Status);
748 /*******************************************************************************
750 * FUNCTION: AcpiUtGetObjectSize
752 * PARAMETERS: InternalObject - An ACPI internal object
753 * ObjLength - Where the length will be returned
757 * DESCRIPTION: This function is called to determine the space required to
758 * contain an object for return to an API user.
760 ******************************************************************************/
763 AcpiUtGetObjectSize (
764 ACPI_OPERAND_OBJECT *InternalObject,
765 ACPI_SIZE *ObjLength)
770 ACPI_FUNCTION_ENTRY ();
773 if ((ACPI_GET_DESCRIPTOR_TYPE (InternalObject) ==
774 ACPI_DESC_TYPE_OPERAND) &&
775 (InternalObject->Common.Type == ACPI_TYPE_PACKAGE))
777 Status = AcpiUtGetPackageObjectSize (InternalObject, ObjLength);
781 Status = AcpiUtGetSimpleObjectSize (InternalObject, ObjLength);