Import acpica-unix-20061109 from Intel.
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / utilities / utobject.c
blob6d7a058e5a153ef747e73b806d12adfcbe37d653
1 /******************************************************************************
3 * Module Name: utobject - ACPI object create/delete/size/cache routines
4 * $Revision: 1.103 $
6 *****************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13 * All rights reserved.
15 * 2. License
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
41 * 3. Conditions
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
117 #define __UTOBJECT_C__
119 #include "acpi.h"
120 #include "acnamesp.h"
121 #include "amlcode.h"
124 #define _COMPONENT ACPI_UTILITIES
125 ACPI_MODULE_NAME ("utobject")
127 /* Local prototypes */
129 static ACPI_STATUS
130 AcpiUtGetSimpleObjectSize (
131 ACPI_OPERAND_OBJECT *Obj,
132 ACPI_SIZE *ObjLength);
134 static ACPI_STATUS
135 AcpiUtGetPackageObjectSize (
136 ACPI_OPERAND_OBJECT *Obj,
137 ACPI_SIZE *ObjLength);
139 static ACPI_STATUS
140 AcpiUtGetElementLength (
141 UINT8 ObjectType,
142 ACPI_OPERAND_OBJECT *SourceObject,
143 ACPI_GENERIC_STATE *State,
144 void *Context);
147 /*******************************************************************************
149 * FUNCTION: AcpiUtCreateInternalObjectDbg
151 * PARAMETERS: ModuleName - Source file name of caller
152 * LineNumber - Line number of caller
153 * ComponentId - Component type of caller
154 * Type - ACPI Type of the new object
156 * RETURN: A new internal object, null on failure
158 * DESCRIPTION: Create and initialize a new internal object.
160 * NOTE: We always allocate the worst-case object descriptor because
161 * these objects are cached, and we want them to be
162 * one-size-satisifies-any-request. This in itself may not be
163 * the most memory efficient, but the efficiency of the object
164 * cache should more than make up for this!
166 ******************************************************************************/
168 ACPI_OPERAND_OBJECT *
169 AcpiUtCreateInternalObjectDbg (
170 char *ModuleName,
171 UINT32 LineNumber,
172 UINT32 ComponentId,
173 ACPI_OBJECT_TYPE Type)
175 ACPI_OPERAND_OBJECT *Object;
176 ACPI_OPERAND_OBJECT *SecondObject;
179 ACPI_FUNCTION_TRACE_STR (UtCreateInternalObjectDbg,
180 AcpiUtGetTypeName (Type));
183 /* Allocate the raw object descriptor */
185 Object = AcpiUtAllocateObjectDescDbg (ModuleName, LineNumber, ComponentId);
186 if (!Object)
188 return_PTR (NULL);
191 switch (Type)
193 case ACPI_TYPE_REGION:
194 case ACPI_TYPE_BUFFER_FIELD:
196 /* These types require a secondary object */
198 SecondObject = AcpiUtAllocateObjectDescDbg (ModuleName,
199 LineNumber, ComponentId);
200 if (!SecondObject)
202 AcpiUtDeleteObjectDesc (Object);
203 return_PTR (NULL);
206 SecondObject->Common.Type = ACPI_TYPE_LOCAL_EXTRA;
207 SecondObject->Common.ReferenceCount = 1;
209 /* Link the second object to the first */
211 Object->Common.NextObject = SecondObject;
212 break;
214 default:
215 /* All others have no secondary object */
216 break;
219 /* Save the object type in the object descriptor */
221 Object->Common.Type = (UINT8) Type;
223 /* Init the reference count */
225 Object->Common.ReferenceCount = 1;
227 /* Any per-type initialization should go here */
229 return_PTR (Object);
233 /*******************************************************************************
235 * FUNCTION: AcpiUtCreateBufferObject
237 * PARAMETERS: BufferSize - Size of buffer to be created
239 * RETURN: Pointer to a new Buffer object, null on failure
241 * DESCRIPTION: Create a fully initialized buffer object
243 ******************************************************************************/
245 ACPI_OPERAND_OBJECT *
246 AcpiUtCreateBufferObject (
247 ACPI_SIZE BufferSize)
249 ACPI_OPERAND_OBJECT *BufferDesc;
250 UINT8 *Buffer = NULL;
253 ACPI_FUNCTION_TRACE_U32 (UtCreateBufferObject, BufferSize);
256 /* Create a new Buffer object */
258 BufferDesc = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
259 if (!BufferDesc)
261 return_PTR (NULL);
264 /* Create an actual buffer only if size > 0 */
266 if (BufferSize > 0)
268 /* Allocate the actual buffer */
270 Buffer = ACPI_ALLOCATE_ZEROED (BufferSize);
271 if (!Buffer)
273 ACPI_ERROR ((AE_INFO, "Could not allocate size %X",
274 (UINT32) BufferSize));
275 AcpiUtRemoveReference (BufferDesc);
276 return_PTR (NULL);
280 /* Complete buffer object initialization */
282 BufferDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
283 BufferDesc->Buffer.Pointer = Buffer;
284 BufferDesc->Buffer.Length = (UINT32) BufferSize;
286 /* Return the new buffer descriptor */
288 return_PTR (BufferDesc);
292 /*******************************************************************************
294 * FUNCTION: AcpiUtCreateStringObject
296 * PARAMETERS: StringSize - Size of string to be created. Does not
297 * include NULL terminator, this is added
298 * automatically.
300 * RETURN: Pointer to a new String object
302 * DESCRIPTION: Create a fully initialized string object
304 ******************************************************************************/
306 ACPI_OPERAND_OBJECT *
307 AcpiUtCreateStringObject (
308 ACPI_SIZE StringSize)
310 ACPI_OPERAND_OBJECT *StringDesc;
311 char *String;
314 ACPI_FUNCTION_TRACE_U32 (UtCreateStringObject, StringSize);
317 /* Create a new String object */
319 StringDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
320 if (!StringDesc)
322 return_PTR (NULL);
326 * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
327 * NOTE: Zero-length strings are NULL terminated
329 String = ACPI_ALLOCATE_ZEROED (StringSize + 1);
330 if (!String)
332 ACPI_ERROR ((AE_INFO, "Could not allocate size %X",
333 (UINT32) StringSize));
334 AcpiUtRemoveReference (StringDesc);
335 return_PTR (NULL);
338 /* Complete string object initialization */
340 StringDesc->String.Pointer = String;
341 StringDesc->String.Length = (UINT32) StringSize;
343 /* Return the new string descriptor */
345 return_PTR (StringDesc);
349 /*******************************************************************************
351 * FUNCTION: AcpiUtValidInternalObject
353 * PARAMETERS: Object - Object to be validated
355 * RETURN: TRUE if object is valid, FALSE otherwise
357 * DESCRIPTION: Validate a pointer to be an ACPI_OPERAND_OBJECT
359 ******************************************************************************/
361 BOOLEAN
362 AcpiUtValidInternalObject (
363 void *Object)
366 ACPI_FUNCTION_NAME (UtValidInternalObject);
369 /* Check for a null pointer */
371 if (!Object)
373 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "**** Null Object Ptr\n"));
374 return (FALSE);
377 /* Check the descriptor type field */
379 switch (ACPI_GET_DESCRIPTOR_TYPE (Object))
381 case ACPI_DESC_TYPE_OPERAND:
383 /* The object appears to be a valid ACPI_OPERAND_OBJECT */
385 return (TRUE);
387 default:
388 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
389 "%p is not not an ACPI operand obj [%s]\n",
390 Object, AcpiUtGetDescriptorName (Object)));
391 break;
394 return (FALSE);
398 /*******************************************************************************
400 * FUNCTION: AcpiUtAllocateObjectDescDbg
402 * PARAMETERS: ModuleName - Caller's module name (for error output)
403 * LineNumber - Caller's line number (for error output)
404 * ComponentId - Caller's component ID (for error output)
406 * RETURN: Pointer to newly allocated object descriptor. Null on error
408 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
409 * error conditions.
411 ******************************************************************************/
413 void *
414 AcpiUtAllocateObjectDescDbg (
415 char *ModuleName,
416 UINT32 LineNumber,
417 UINT32 ComponentId)
419 ACPI_OPERAND_OBJECT *Object;
422 ACPI_FUNCTION_TRACE (UtAllocateObjectDescDbg);
425 Object = AcpiOsAcquireObject (AcpiGbl_OperandCache);
426 if (!Object)
428 ACPI_ERROR ((ModuleName, LineNumber,
429 "Could not allocate an object descriptor"));
431 return_PTR (NULL);
434 /* Mark the descriptor type */
436 ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_OPERAND);
438 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
439 Object, (UINT32) sizeof (ACPI_OPERAND_OBJECT)));
441 return_PTR (Object);
445 /*******************************************************************************
447 * FUNCTION: AcpiUtDeleteObjectDesc
449 * PARAMETERS: Object - An Acpi internal object to be deleted
451 * RETURN: None.
453 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
455 ******************************************************************************/
457 void
458 AcpiUtDeleteObjectDesc (
459 ACPI_OPERAND_OBJECT *Object)
461 ACPI_FUNCTION_TRACE_PTR (UtDeleteObjectDesc, Object);
464 /* Object must be an ACPI_OPERAND_OBJECT */
466 if (ACPI_GET_DESCRIPTOR_TYPE (Object) != ACPI_DESC_TYPE_OPERAND)
468 ACPI_ERROR ((AE_INFO,
469 "%p is not an ACPI Operand object [%s]", Object,
470 AcpiUtGetDescriptorName (Object)));
471 return_VOID;
474 (void) AcpiOsReleaseObject (AcpiGbl_OperandCache, Object);
475 return_VOID;
479 /*******************************************************************************
481 * FUNCTION: AcpiUtGetSimpleObjectSize
483 * PARAMETERS: InternalObject - An ACPI operand object
484 * ObjLength - Where the length is returned
486 * RETURN: Status
488 * DESCRIPTION: This function is called to determine the space required to
489 * contain a simple object for return to an external user.
491 * The length includes the object structure plus any additional
492 * needed space.
494 ******************************************************************************/
496 static ACPI_STATUS
497 AcpiUtGetSimpleObjectSize (
498 ACPI_OPERAND_OBJECT *InternalObject,
499 ACPI_SIZE *ObjLength)
501 ACPI_SIZE Length;
502 ACPI_STATUS Status = AE_OK;
505 ACPI_FUNCTION_TRACE_PTR (UtGetSimpleObjectSize, InternalObject);
509 * Handle a null object (Could be a uninitialized package
510 * element -- which is legal)
512 if (!InternalObject)
514 *ObjLength = 0;
515 return_ACPI_STATUS (AE_OK);
518 /* Start with the length of the Acpi object */
520 Length = sizeof (ACPI_OBJECT);
522 if (ACPI_GET_DESCRIPTOR_TYPE (InternalObject) == ACPI_DESC_TYPE_NAMED)
524 /* Object is a named object (reference), just return the length */
526 *ObjLength = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
527 return_ACPI_STATUS (Status);
531 * The final length depends on the object type
532 * Strings and Buffers are packed right up against the parent object and
533 * must be accessed bytewise or there may be alignment problems on
534 * certain processors
536 switch (ACPI_GET_OBJECT_TYPE (InternalObject))
538 case ACPI_TYPE_STRING:
540 Length += (ACPI_SIZE) InternalObject->String.Length + 1;
541 break;
544 case ACPI_TYPE_BUFFER:
546 Length += (ACPI_SIZE) InternalObject->Buffer.Length;
547 break;
550 case ACPI_TYPE_INTEGER:
551 case ACPI_TYPE_PROCESSOR:
552 case ACPI_TYPE_POWER:
555 * No extra data for these types
557 break;
560 case ACPI_TYPE_LOCAL_REFERENCE:
562 switch (InternalObject->Reference.Opcode)
564 case AML_INT_NAMEPATH_OP:
567 * Get the actual length of the full pathname to this object.
568 * The reference will be converted to the pathname to the object
570 Length += ACPI_ROUND_UP_TO_NATIVE_WORD (
571 AcpiNsGetPathnameLength (InternalObject->Reference.Node));
572 break;
574 default:
577 * No other reference opcodes are supported.
578 * Notably, Locals and Args are not supported, but this may be
579 * required eventually.
581 ACPI_ERROR ((AE_INFO,
582 "Unsupported Reference opcode=%X in object %p",
583 InternalObject->Reference.Opcode, InternalObject));
584 Status = AE_TYPE;
585 break;
587 break;
590 default:
592 ACPI_ERROR ((AE_INFO, "Unsupported type=%X in object %p",
593 ACPI_GET_OBJECT_TYPE (InternalObject), InternalObject));
594 Status = AE_TYPE;
595 break;
599 * Account for the space required by the object rounded up to the next
600 * multiple of the machine word size. This keeps each object aligned
601 * on a machine word boundary. (preventing alignment faults on some
602 * machines.)
604 *ObjLength = ACPI_ROUND_UP_TO_NATIVE_WORD (Length);
605 return_ACPI_STATUS (Status);
609 /*******************************************************************************
611 * FUNCTION: AcpiUtGetElementLength
613 * PARAMETERS: ACPI_PKG_CALLBACK
615 * RETURN: Status
617 * DESCRIPTION: Get the length of one package element.
619 ******************************************************************************/
621 static ACPI_STATUS
622 AcpiUtGetElementLength (
623 UINT8 ObjectType,
624 ACPI_OPERAND_OBJECT *SourceObject,
625 ACPI_GENERIC_STATE *State,
626 void *Context)
628 ACPI_STATUS Status = AE_OK;
629 ACPI_PKG_INFO *Info = (ACPI_PKG_INFO *) Context;
630 ACPI_SIZE ObjectSpace;
633 switch (ObjectType)
635 case ACPI_COPY_TYPE_SIMPLE:
638 * Simple object - just get the size (Null object/entry is handled
639 * here also) and sum it into the running package length
641 Status = AcpiUtGetSimpleObjectSize (SourceObject, &ObjectSpace);
642 if (ACPI_FAILURE (Status))
644 return (Status);
647 Info->Length += ObjectSpace;
648 break;
651 case ACPI_COPY_TYPE_PACKAGE:
653 /* Package object - nothing much to do here, let the walk handle it */
655 Info->NumPackages++;
656 State->Pkg.ThisTargetObj = NULL;
657 break;
660 default:
662 /* No other types allowed */
664 return (AE_BAD_PARAMETER);
667 return (Status);
671 /*******************************************************************************
673 * FUNCTION: AcpiUtGetPackageObjectSize
675 * PARAMETERS: InternalObject - An ACPI internal object
676 * ObjLength - Where the length is returned
678 * RETURN: Status
680 * DESCRIPTION: This function is called to determine the space required to
681 * contain a package object for return to an external user.
683 * This is moderately complex since a package contains other
684 * objects including packages.
686 ******************************************************************************/
688 static ACPI_STATUS
689 AcpiUtGetPackageObjectSize (
690 ACPI_OPERAND_OBJECT *InternalObject,
691 ACPI_SIZE *ObjLength)
693 ACPI_STATUS Status;
694 ACPI_PKG_INFO Info;
697 ACPI_FUNCTION_TRACE_PTR (UtGetPackageObjectSize, InternalObject);
700 Info.Length = 0;
701 Info.ObjectSpace = 0;
702 Info.NumPackages = 1;
704 Status = AcpiUtWalkPackageTree (InternalObject, NULL,
705 AcpiUtGetElementLength, &Info);
706 if (ACPI_FAILURE (Status))
708 return_ACPI_STATUS (Status);
712 * We have handled all of the objects in all levels of the package.
713 * just add the length of the package objects themselves.
714 * Round up to the next machine word.
716 Info.Length += ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (ACPI_OBJECT)) *
717 (ACPI_SIZE) Info.NumPackages;
719 /* Return the total package length */
721 *ObjLength = Info.Length;
722 return_ACPI_STATUS (Status);
726 /*******************************************************************************
728 * FUNCTION: AcpiUtGetObjectSize
730 * PARAMETERS: InternalObject - An ACPI internal object
731 * ObjLength - Where the length will be returned
733 * RETURN: Status
735 * DESCRIPTION: This function is called to determine the space required to
736 * contain an object for return to an API user.
738 ******************************************************************************/
740 ACPI_STATUS
741 AcpiUtGetObjectSize (
742 ACPI_OPERAND_OBJECT *InternalObject,
743 ACPI_SIZE *ObjLength)
745 ACPI_STATUS Status;
748 ACPI_FUNCTION_ENTRY ();
751 if ((ACPI_GET_DESCRIPTOR_TYPE (InternalObject) == ACPI_DESC_TYPE_OPERAND) &&
752 (ACPI_GET_OBJECT_TYPE (InternalObject) == ACPI_TYPE_PACKAGE))
754 Status = AcpiUtGetPackageObjectSize (InternalObject, ObjLength);
756 else
758 Status = AcpiUtGetSimpleObjectSize (InternalObject, ObjLength);
761 return (Status);