1 /******************************************************************************
3 * Module Name: utobject - ACPI object create/delete/size/cache routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 (ModuleName, LineNumber, ComponentId);
118 case ACPI_TYPE_REGION:
119 case ACPI_TYPE_BUFFER_FIELD:
120 case ACPI_TYPE_LOCAL_BANK_FIELD:
122 /* These types require a secondary object */
124 SecondObject = AcpiUtAllocateObjectDescDbg (ModuleName,
125 LineNumber, ComponentId);
128 AcpiUtDeleteObjectDesc (Object);
132 SecondObject->Common.Type = ACPI_TYPE_LOCAL_EXTRA;
133 SecondObject->Common.ReferenceCount = 1;
135 /* Link the second object to the first */
137 Object->Common.NextObject = SecondObject;
142 /* All others have no secondary object */
146 /* Save the object type in the object descriptor */
148 Object->Common.Type = (UINT8) Type;
150 /* Init the reference count */
152 Object->Common.ReferenceCount = 1;
154 /* Any per-type initialization should go here */
160 /*******************************************************************************
162 * FUNCTION: AcpiUtCreatePackageObject
164 * PARAMETERS: Count - Number of package elements
166 * RETURN: Pointer to a new Package object, null on failure
168 * DESCRIPTION: Create a fully initialized package object
170 ******************************************************************************/
172 ACPI_OPERAND_OBJECT *
173 AcpiUtCreatePackageObject (
176 ACPI_OPERAND_OBJECT *PackageDesc;
177 ACPI_OPERAND_OBJECT **PackageElements;
180 ACPI_FUNCTION_TRACE_U32 (UtCreatePackageObject, Count);
183 /* Create a new Package object */
185 PackageDesc = AcpiUtCreateInternalObject (ACPI_TYPE_PACKAGE);
192 * Create the element array. Count+1 allows the array to be null
195 PackageElements = ACPI_ALLOCATE_ZEROED (
196 ((ACPI_SIZE) Count + 1) * sizeof (void *));
197 if (!PackageElements)
199 ACPI_FREE (PackageDesc);
203 PackageDesc->Package.Count = Count;
204 PackageDesc->Package.Elements = PackageElements;
205 return_PTR (PackageDesc);
209 /*******************************************************************************
211 * FUNCTION: AcpiUtCreateIntegerObject
213 * PARAMETERS: InitialValue - Initial value for the integer
215 * RETURN: Pointer to a new Integer object, null on failure
217 * DESCRIPTION: Create an initialized integer object
219 ******************************************************************************/
221 ACPI_OPERAND_OBJECT *
222 AcpiUtCreateIntegerObject (
225 ACPI_OPERAND_OBJECT *IntegerDesc;
228 ACPI_FUNCTION_TRACE (UtCreateIntegerObject);
231 /* Create and initialize a new integer object */
233 IntegerDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
239 IntegerDesc->Integer.Value = InitialValue;
240 return_PTR (IntegerDesc);
244 /*******************************************************************************
246 * FUNCTION: AcpiUtCreateBufferObject
248 * PARAMETERS: BufferSize - Size of buffer to be created
250 * RETURN: Pointer to a new Buffer object, null on failure
252 * DESCRIPTION: Create a fully initialized buffer object
254 ******************************************************************************/
256 ACPI_OPERAND_OBJECT *
257 AcpiUtCreateBufferObject (
258 ACPI_SIZE BufferSize)
260 ACPI_OPERAND_OBJECT *BufferDesc;
261 UINT8 *Buffer = NULL;
264 ACPI_FUNCTION_TRACE_U32 (UtCreateBufferObject, BufferSize);
267 /* Create a new Buffer object */
269 BufferDesc = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
275 /* Create an actual buffer only if size > 0 */
279 /* Allocate the actual buffer */
281 Buffer = ACPI_ALLOCATE_ZEROED (BufferSize);
284 ACPI_ERROR ((AE_INFO, "Could not allocate size %u",
285 (UINT32) BufferSize));
286 AcpiUtRemoveReference (BufferDesc);
291 /* Complete buffer object initialization */
293 BufferDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
294 BufferDesc->Buffer.Pointer = Buffer;
295 BufferDesc->Buffer.Length = (UINT32) BufferSize;
297 /* Return the new buffer descriptor */
299 return_PTR (BufferDesc);
303 /*******************************************************************************
305 * FUNCTION: AcpiUtCreateStringObject
307 * PARAMETERS: StringSize - Size of string to be created. Does not
308 * include NULL terminator, this is added
311 * RETURN: Pointer to a new String object
313 * DESCRIPTION: Create a fully initialized string object
315 ******************************************************************************/
317 ACPI_OPERAND_OBJECT *
318 AcpiUtCreateStringObject (
319 ACPI_SIZE StringSize)
321 ACPI_OPERAND_OBJECT *StringDesc;
325 ACPI_FUNCTION_TRACE_U32 (UtCreateStringObject, StringSize);
328 /* Create a new String object */
330 StringDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
337 * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
338 * NOTE: Zero-length strings are NULL terminated
340 String = ACPI_ALLOCATE_ZEROED (StringSize + 1);
343 ACPI_ERROR ((AE_INFO, "Could not allocate size %u",
344 (UINT32) StringSize));
345 AcpiUtRemoveReference (StringDesc);
349 /* Complete string object initialization */
351 StringDesc->String.Pointer = String;
352 StringDesc->String.Length = (UINT32) StringSize;
354 /* Return the new string descriptor */
356 return_PTR (StringDesc);
360 /*******************************************************************************
362 * FUNCTION: AcpiUtValidInternalObject
364 * PARAMETERS: Object - Object to be validated
366 * RETURN: TRUE if object is valid, FALSE otherwise
368 * DESCRIPTION: Validate a pointer to be of type ACPI_OPERAND_OBJECT
370 ******************************************************************************/
373 AcpiUtValidInternalObject (
377 ACPI_FUNCTION_NAME (UtValidInternalObject);
380 /* Check for a null pointer */
384 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "**** Null Object Ptr\n"));
388 /* Check the descriptor type field */
390 switch (ACPI_GET_DESCRIPTOR_TYPE (Object))
392 case ACPI_DESC_TYPE_OPERAND:
394 /* The object appears to be a valid ACPI_OPERAND_OBJECT */
400 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
401 "%p is not an ACPI operand obj [%s]\n",
402 Object, AcpiUtGetDescriptorName (Object)));
410 /*******************************************************************************
412 * FUNCTION: AcpiUtAllocateObjectDescDbg
414 * PARAMETERS: ModuleName - Caller's module name (for error output)
415 * LineNumber - Caller's line number (for error output)
416 * ComponentId - Caller's component ID (for error output)
418 * RETURN: Pointer to newly allocated object descriptor. Null on error
420 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
423 ******************************************************************************/
426 AcpiUtAllocateObjectDescDbg (
427 const char *ModuleName,
431 ACPI_OPERAND_OBJECT *Object;
434 ACPI_FUNCTION_TRACE (UtAllocateObjectDescDbg);
437 Object = AcpiOsAcquireObject (AcpiGbl_OperandCache);
440 ACPI_ERROR ((ModuleName, LineNumber,
441 "Could not allocate an object descriptor"));
446 /* Mark the descriptor type */
448 ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_OPERAND);
450 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
451 Object, (UINT32) sizeof (ACPI_OPERAND_OBJECT)));
457 /*******************************************************************************
459 * FUNCTION: AcpiUtDeleteObjectDesc
461 * PARAMETERS: Object - An Acpi internal object to be deleted
465 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
467 ******************************************************************************/
470 AcpiUtDeleteObjectDesc (
471 ACPI_OPERAND_OBJECT *Object)
473 ACPI_FUNCTION_TRACE_PTR (UtDeleteObjectDesc, Object);
476 /* Object must be of type ACPI_OPERAND_OBJECT */
478 if (ACPI_GET_DESCRIPTOR_TYPE (Object) != ACPI_DESC_TYPE_OPERAND)
480 ACPI_ERROR ((AE_INFO,
481 "%p is not an ACPI Operand object [%s]", Object,
482 AcpiUtGetDescriptorName (Object)));
486 (void) AcpiOsReleaseObject (AcpiGbl_OperandCache, Object);
491 /*******************************************************************************
493 * FUNCTION: AcpiUtGetSimpleObjectSize
495 * PARAMETERS: InternalObject - An ACPI operand object
496 * ObjLength - Where the length is returned
500 * DESCRIPTION: This function is called to determine the space required to
501 * contain a simple object for return to an external user.
503 * The length includes the object structure plus any additional
506 ******************************************************************************/
509 AcpiUtGetSimpleObjectSize (
510 ACPI_OPERAND_OBJECT *InternalObject,
511 ACPI_SIZE *ObjLength)
515 ACPI_STATUS Status = AE_OK;
518 ACPI_FUNCTION_TRACE_PTR (UtGetSimpleObjectSize, InternalObject);
521 /* Start with the length of the (external) Acpi object */
523 Length = sizeof (ACPI_OBJECT);
525 /* A NULL object is allowed, can be a legal uninitialized package element */
530 * Object is NULL, just return the length of ACPI_OBJECT
531 * (A NULL ACPI_OBJECT is an object of all zeroes.)
533 *ObjLength = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
534 return_ACPI_STATUS (AE_OK);
537 /* A Namespace Node should never appear here */
539 if (ACPI_GET_DESCRIPTOR_TYPE (InternalObject) == ACPI_DESC_TYPE_NAMED)
541 /* A namespace node should never get here */
543 return_ACPI_STATUS (AE_AML_INTERNAL);
547 * The final length depends on the object type
548 * Strings and Buffers are packed right up against the parent object and
549 * must be accessed bytewise or there may be alignment problems on
552 switch (InternalObject->Common.Type)
554 case ACPI_TYPE_STRING:
556 Length += (ACPI_SIZE) InternalObject->String.Length + 1;
559 case ACPI_TYPE_BUFFER:
561 Length += (ACPI_SIZE) InternalObject->Buffer.Length;
564 case ACPI_TYPE_INTEGER:
565 case ACPI_TYPE_PROCESSOR:
566 case ACPI_TYPE_POWER:
568 /* No extra data for these types */
572 case ACPI_TYPE_LOCAL_REFERENCE:
574 switch (InternalObject->Reference.Class)
576 case ACPI_REFCLASS_NAME:
578 * Get the actual length of the full pathname to this object.
579 * The reference will be converted to the pathname to the object
581 Size = AcpiNsGetPathnameLength (InternalObject->Reference.Node);
584 return_ACPI_STATUS (AE_BAD_PARAMETER);
587 Length += ACPI_ROUND_UP_TO_NATIVE_WORD (Size);
592 * No other reference opcodes are supported.
593 * Notably, Locals and Args are not supported, but this may be
594 * required eventually.
596 ACPI_ERROR ((AE_INFO, "Cannot convert to external object - "
597 "unsupported Reference Class [%s] 0x%X in object %p",
598 AcpiUtGetReferenceName (InternalObject),
599 InternalObject->Reference.Class, InternalObject));
607 ACPI_ERROR ((AE_INFO, "Cannot convert to external object - "
608 "unsupported type [%s] 0x%X in object %p",
609 AcpiUtGetObjectTypeName (InternalObject),
610 InternalObject->Common.Type, InternalObject));
616 * Account for the space required by the object rounded up to the next
617 * multiple of the machine word size. This keeps each object aligned
618 * on a machine word boundary. (preventing alignment faults on some
621 *ObjLength = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
622 return_ACPI_STATUS (Status);
626 /*******************************************************************************
628 * FUNCTION: AcpiUtGetElementLength
630 * PARAMETERS: ACPI_PKG_CALLBACK
634 * DESCRIPTION: Get the length of one package element.
636 ******************************************************************************/
639 AcpiUtGetElementLength (
641 ACPI_OPERAND_OBJECT *SourceObject,
642 ACPI_GENERIC_STATE *State,
645 ACPI_STATUS Status = AE_OK;
646 ACPI_PKG_INFO *Info = (ACPI_PKG_INFO *) Context;
647 ACPI_SIZE ObjectSpace;
652 case ACPI_COPY_TYPE_SIMPLE:
654 * Simple object - just get the size (Null object/entry is handled
655 * here also) and sum it into the running package length
657 Status = AcpiUtGetSimpleObjectSize (SourceObject, &ObjectSpace);
658 if (ACPI_FAILURE (Status))
663 Info->Length += ObjectSpace;
666 case ACPI_COPY_TYPE_PACKAGE:
668 /* Package object - nothing much to do here, let the walk handle it */
671 State->Pkg.ThisTargetObj = NULL;
676 /* No other types allowed */
678 return (AE_BAD_PARAMETER);
685 /*******************************************************************************
687 * FUNCTION: AcpiUtGetPackageObjectSize
689 * PARAMETERS: InternalObject - An ACPI internal object
690 * ObjLength - Where the length is returned
694 * DESCRIPTION: This function is called to determine the space required to
695 * contain a package object for return to an external user.
697 * This is moderately complex since a package contains other
698 * objects including packages.
700 ******************************************************************************/
703 AcpiUtGetPackageObjectSize (
704 ACPI_OPERAND_OBJECT *InternalObject,
705 ACPI_SIZE *ObjLength)
711 ACPI_FUNCTION_TRACE_PTR (UtGetPackageObjectSize, InternalObject);
715 Info.ObjectSpace = 0;
716 Info.NumPackages = 1;
718 Status = AcpiUtWalkPackageTree (InternalObject, NULL,
719 AcpiUtGetElementLength, &Info);
720 if (ACPI_FAILURE (Status))
722 return_ACPI_STATUS (Status);
726 * We have handled all of the objects in all levels of the package.
727 * just add the length of the package objects themselves.
728 * Round up to the next machine word.
730 Info.Length += ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (ACPI_OBJECT)) *
731 (ACPI_SIZE) Info.NumPackages;
733 /* Return the total package length */
735 *ObjLength = Info.Length;
736 return_ACPI_STATUS (Status);
740 /*******************************************************************************
742 * FUNCTION: AcpiUtGetObjectSize
744 * PARAMETERS: InternalObject - An ACPI internal object
745 * ObjLength - Where the length will be returned
749 * DESCRIPTION: This function is called to determine the space required to
750 * contain an object for return to an API user.
752 ******************************************************************************/
755 AcpiUtGetObjectSize (
756 ACPI_OPERAND_OBJECT *InternalObject,
757 ACPI_SIZE *ObjLength)
762 ACPI_FUNCTION_ENTRY ();
765 if ((ACPI_GET_DESCRIPTOR_TYPE (InternalObject) == ACPI_DESC_TYPE_OPERAND) &&
766 (InternalObject->Common.Type == ACPI_TYPE_PACKAGE))
768 Status = AcpiUtGetPackageObjectSize (InternalObject, ObjLength);
772 Status = AcpiUtGetSimpleObjectSize (InternalObject, ObjLength);