i386/identcpu.c: Add VIA Nano support
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / utilities / utdelete.c
blobc85ea669be3798eacfcb10c5abba6c125bc23cb9
1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
4 * $Revision: 1.121 $
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 __UTDELETE_C__
119 #include "acpi.h"
120 #include "acinterp.h"
121 #include "acnamesp.h"
122 #include "acevents.h"
123 #include "amlcode.h"
125 #define _COMPONENT ACPI_UTILITIES
126 ACPI_MODULE_NAME ("utdelete")
128 /* Local prototypes */
130 static void
131 AcpiUtDeleteInternalObj (
132 ACPI_OPERAND_OBJECT *Object);
134 static void
135 AcpiUtUpdateRefCount (
136 ACPI_OPERAND_OBJECT *Object,
137 UINT32 Action);
140 /*******************************************************************************
142 * FUNCTION: AcpiUtDeleteInternalObj
144 * PARAMETERS: Object - Object to be deleted
146 * RETURN: None
148 * DESCRIPTION: Low level object deletion, after reference counts have been
149 * updated (All reference counts, including sub-objects!)
151 ******************************************************************************/
153 static void
154 AcpiUtDeleteInternalObj (
155 ACPI_OPERAND_OBJECT *Object)
157 void *ObjPointer = NULL;
158 ACPI_OPERAND_OBJECT *HandlerDesc;
159 ACPI_OPERAND_OBJECT *SecondDesc;
160 ACPI_OPERAND_OBJECT *NextDesc;
163 ACPI_FUNCTION_TRACE_PTR (UtDeleteInternalObj, Object);
166 if (!Object)
168 return_VOID;
172 * Must delete or free any pointers within the object that are not
173 * actual ACPI objects (for example, a raw buffer pointer).
175 switch (ACPI_GET_OBJECT_TYPE (Object))
177 case ACPI_TYPE_STRING:
179 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
180 Object, Object->String.Pointer));
182 /* Free the actual string buffer */
184 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
186 /* But only if it is NOT a pointer into an ACPI table */
188 ObjPointer = Object->String.Pointer;
190 break;
193 case ACPI_TYPE_BUFFER:
195 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
196 Object, Object->Buffer.Pointer));
198 /* Free the actual buffer */
200 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
202 /* But only if it is NOT a pointer into an ACPI table */
204 ObjPointer = Object->Buffer.Pointer;
206 break;
209 case ACPI_TYPE_PACKAGE:
211 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
212 Object->Package.Count));
215 * Elements of the package are not handled here, they are deleted
216 * separately
219 /* Free the (variable length) element pointer array */
221 ObjPointer = Object->Package.Elements;
222 break;
225 case ACPI_TYPE_DEVICE:
227 if (Object->Device.GpeBlock)
229 (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
232 /* Walk the handler list for this device */
234 HandlerDesc = Object->Device.Handler;
235 while (HandlerDesc)
237 NextDesc = HandlerDesc->AddressSpace.Next;
238 AcpiUtRemoveReference (HandlerDesc);
239 HandlerDesc = NextDesc;
241 break;
244 case ACPI_TYPE_MUTEX:
246 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
247 "***** Mutex %p, OS Mutex %p\n",
248 Object, Object->Mutex.OsMutex));
250 if (Object->Mutex.OsMutex == AcpiGbl_GlobalLockMutex)
252 /* Global Lock has extra semaphore */
254 (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
255 AcpiGbl_GlobalLockSemaphore = NULL;
257 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
258 AcpiGbl_GlobalLockMutex = NULL;
260 else
262 AcpiExUnlinkMutex (Object);
263 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
265 break;
268 case ACPI_TYPE_EVENT:
270 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
271 "***** Event %p, OS Semaphore %p\n",
272 Object, Object->Event.OsSemaphore));
274 (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
275 Object->Event.OsSemaphore = NULL;
276 break;
279 case ACPI_TYPE_METHOD:
281 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
282 "***** Method %p\n", Object));
284 /* Delete the method mutex if it exists */
286 if (Object->Method.Mutex)
288 AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
289 AcpiUtDeleteObjectDesc (Object->Method.Mutex);
290 Object->Method.Mutex = NULL;
292 break;
295 case ACPI_TYPE_REGION:
297 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
298 "***** Region %p\n", Object));
300 SecondDesc = AcpiNsGetSecondaryObject (Object);
301 if (SecondDesc)
304 * Free the RegionContext if and only if the handler is one of the
305 * default handlers -- and therefore, we created the context object
306 * locally, it was not created by an external caller.
308 HandlerDesc = Object->Region.Handler;
309 if (HandlerDesc)
311 if (HandlerDesc->AddressSpace.HandlerFlags &
312 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
314 /* Deactivate region and free region context */
316 if (HandlerDesc->AddressSpace.Setup)
318 (void) HandlerDesc->AddressSpace.Setup (Object,
319 ACPI_REGION_DEACTIVATE,
320 HandlerDesc->AddressSpace.Context,
321 &SecondDesc->Extra.RegionContext);
325 AcpiUtRemoveReference (HandlerDesc);
328 /* Now we can free the Extra object */
330 AcpiUtDeleteObjectDesc (SecondDesc);
332 break;
335 case ACPI_TYPE_BUFFER_FIELD:
337 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
338 "***** Buffer Field %p\n", Object));
340 SecondDesc = AcpiNsGetSecondaryObject (Object);
341 if (SecondDesc)
343 AcpiUtDeleteObjectDesc (SecondDesc);
345 break;
348 default:
349 break;
352 /* Free any allocated memory (pointer within the object) found above */
354 if (ObjPointer)
356 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
357 ObjPointer));
358 ACPI_FREE (ObjPointer);
361 /* Now the object can be safely deleted */
363 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
364 Object, AcpiUtGetObjectTypeName (Object)));
366 AcpiUtDeleteObjectDesc (Object);
367 return_VOID;
371 /*******************************************************************************
373 * FUNCTION: AcpiUtDeleteInternalObjectList
375 * PARAMETERS: ObjList - Pointer to the list to be deleted
377 * RETURN: None
379 * DESCRIPTION: This function deletes an internal object list, including both
380 * simple objects and package objects
382 ******************************************************************************/
384 void
385 AcpiUtDeleteInternalObjectList (
386 ACPI_OPERAND_OBJECT **ObjList)
388 ACPI_OPERAND_OBJECT **InternalObj;
391 ACPI_FUNCTION_TRACE (UtDeleteInternalObjectList);
394 /* Walk the null-terminated internal list */
396 for (InternalObj = ObjList; *InternalObj; InternalObj++)
398 AcpiUtRemoveReference (*InternalObj);
401 /* Free the combined parameter pointer list and object array */
403 ACPI_FREE (ObjList);
404 return_VOID;
408 /*******************************************************************************
410 * FUNCTION: AcpiUtUpdateRefCount
412 * PARAMETERS: Object - Object whose ref count is to be updated
413 * Action - What to do
415 * RETURN: New ref count
417 * DESCRIPTION: Modify the ref count and return it.
419 ******************************************************************************/
421 static void
422 AcpiUtUpdateRefCount (
423 ACPI_OPERAND_OBJECT *Object,
424 UINT32 Action)
426 UINT16 Count;
427 UINT16 NewCount;
430 ACPI_FUNCTION_NAME (UtUpdateRefCount);
433 if (!Object)
435 return;
438 Count = Object->Common.ReferenceCount;
439 NewCount = Count;
442 * Perform the reference count action (increment, decrement, force delete)
444 switch (Action)
446 case REF_INCREMENT:
448 NewCount++;
449 Object->Common.ReferenceCount = NewCount;
451 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
452 "Obj %p Refs=%X, [Incremented]\n",
453 Object, NewCount));
454 break;
456 case REF_DECREMENT:
458 if (Count < 1)
460 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
461 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
462 Object, NewCount));
464 NewCount = 0;
466 else
468 NewCount--;
470 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
471 "Obj %p Refs=%X, [Decremented]\n",
472 Object, NewCount));
475 if (ACPI_GET_OBJECT_TYPE (Object) == ACPI_TYPE_METHOD)
477 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
478 "Method Obj %p Refs=%X, [Decremented]\n", Object, NewCount));
481 Object->Common.ReferenceCount = NewCount;
482 if (NewCount == 0)
484 AcpiUtDeleteInternalObj (Object);
486 break;
488 case REF_FORCE_DELETE:
490 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
491 "Obj %p Refs=%X, Force delete! (Set to 0)\n", Object, Count));
493 NewCount = 0;
494 Object->Common.ReferenceCount = NewCount;
495 AcpiUtDeleteInternalObj (Object);
496 break;
498 default:
500 ACPI_ERROR ((AE_INFO, "Unknown action (%X)", Action));
501 break;
505 * Sanity check the reference count, for debug purposes only.
506 * (A deleted object will have a huge reference count)
508 if (Count > ACPI_MAX_REFERENCE_COUNT)
510 ACPI_WARNING ((AE_INFO,
511 "Large Reference Count (%X) in object %p", Count, Object));
516 /*******************************************************************************
518 * FUNCTION: AcpiUtUpdateObjectReference
520 * PARAMETERS: Object - Increment ref count for this object
521 * and all sub-objects
522 * Action - Either REF_INCREMENT or REF_DECREMENT or
523 * REF_FORCE_DELETE
525 * RETURN: Status
527 * DESCRIPTION: Increment the object reference count
529 * Object references are incremented when:
530 * 1) An object is attached to a Node (namespace object)
531 * 2) An object is copied (all subobjects must be incremented)
533 * Object references are decremented when:
534 * 1) An object is detached from an Node
536 ******************************************************************************/
538 ACPI_STATUS
539 AcpiUtUpdateObjectReference (
540 ACPI_OPERAND_OBJECT *Object,
541 UINT16 Action)
543 ACPI_STATUS Status = AE_OK;
544 ACPI_GENERIC_STATE *StateList = NULL;
545 ACPI_OPERAND_OBJECT *NextObject = NULL;
546 ACPI_GENERIC_STATE *State;
547 ACPI_NATIVE_UINT i;
550 ACPI_FUNCTION_TRACE_PTR (UtUpdateObjectReference, Object);
553 while (Object)
555 /* Make sure that this isn't a namespace handle */
557 if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
559 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
560 "Object %p is NS handle\n", Object));
561 return_ACPI_STATUS (AE_OK);
565 * All sub-objects must have their reference count incremented also.
566 * Different object types have different subobjects.
568 switch (ACPI_GET_OBJECT_TYPE (Object))
570 case ACPI_TYPE_DEVICE:
571 case ACPI_TYPE_PROCESSOR:
572 case ACPI_TYPE_POWER:
573 case ACPI_TYPE_THERMAL:
575 /* Update the notify objects for these types (if present) */
577 AcpiUtUpdateRefCount (Object->CommonNotify.SystemNotify, Action);
578 AcpiUtUpdateRefCount (Object->CommonNotify.DeviceNotify, Action);
579 break;
581 case ACPI_TYPE_PACKAGE:
583 * We must update all the sub-objects of the package,
584 * each of whom may have their own sub-objects.
586 for (i = 0; i < Object->Package.Count; i++)
589 * Push each element onto the stack for later processing.
590 * Note: There can be null elements within the package,
591 * these are simply ignored
593 Status = AcpiUtCreateUpdateStateAndPush (
594 Object->Package.Elements[i], Action, &StateList);
595 if (ACPI_FAILURE (Status))
597 goto ErrorExit;
600 break;
602 case ACPI_TYPE_BUFFER_FIELD:
604 NextObject = Object->BufferField.BufferObj;
605 break;
607 case ACPI_TYPE_LOCAL_REGION_FIELD:
609 NextObject = Object->Field.RegionObj;
610 break;
612 case ACPI_TYPE_LOCAL_BANK_FIELD:
614 NextObject = Object->BankField.BankObj;
615 Status = AcpiUtCreateUpdateStateAndPush (
616 Object->BankField.RegionObj, Action, &StateList);
617 if (ACPI_FAILURE (Status))
619 goto ErrorExit;
621 break;
623 case ACPI_TYPE_LOCAL_INDEX_FIELD:
625 NextObject = Object->IndexField.IndexObj;
626 Status = AcpiUtCreateUpdateStateAndPush (
627 Object->IndexField.DataObj, Action, &StateList);
628 if (ACPI_FAILURE (Status))
630 goto ErrorExit;
632 break;
634 case ACPI_TYPE_LOCAL_REFERENCE:
636 * The target of an Index (a package, string, or buffer) must track
637 * changes to the ref count of the index.
639 if (Object->Reference.Opcode == AML_INDEX_OP)
641 NextObject = Object->Reference.Object;
643 break;
645 case ACPI_TYPE_REGION:
646 default:
647 break; /* No subobjects for all other types */
651 * Now we can update the count in the main object. This can only
652 * happen after we update the sub-objects in case this causes the
653 * main object to be deleted.
655 AcpiUtUpdateRefCount (Object, Action);
656 Object = NULL;
658 /* Move on to the next object to be updated */
660 if (NextObject)
662 Object = NextObject;
663 NextObject = NULL;
665 else if (StateList)
667 State = AcpiUtPopGenericState (&StateList);
668 Object = State->Update.Object;
669 AcpiUtDeleteGenericState (State);
673 return_ACPI_STATUS (AE_OK);
675 ErrorExit:
677 ACPI_EXCEPTION ((AE_INFO, Status,
678 "Could not update object reference count"));
680 return_ACPI_STATUS (Status);
684 /*******************************************************************************
686 * FUNCTION: AcpiUtAddReference
688 * PARAMETERS: Object - Object whose reference count is to be
689 * incremented
691 * RETURN: None
693 * DESCRIPTION: Add one reference to an ACPI object
695 ******************************************************************************/
697 void
698 AcpiUtAddReference (
699 ACPI_OPERAND_OBJECT *Object)
702 ACPI_FUNCTION_TRACE_PTR (UtAddReference, Object);
705 /* Ensure that we have a valid object */
707 if (!AcpiUtValidInternalObject (Object))
709 return_VOID;
712 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
713 "Obj %p Current Refs=%X [To Be Incremented]\n",
714 Object, Object->Common.ReferenceCount));
716 /* Increment the reference count */
718 (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
719 return_VOID;
723 /*******************************************************************************
725 * FUNCTION: AcpiUtRemoveReference
727 * PARAMETERS: Object - Object whose ref count will be decremented
729 * RETURN: None
731 * DESCRIPTION: Decrement the reference count of an ACPI internal object
733 ******************************************************************************/
735 void
736 AcpiUtRemoveReference (
737 ACPI_OPERAND_OBJECT *Object)
740 ACPI_FUNCTION_TRACE_PTR (UtRemoveReference, Object);
744 * Allow a NULL pointer to be passed in, just ignore it. This saves
745 * each caller from having to check. Also, ignore NS nodes.
748 if (!Object ||
749 (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
752 return_VOID;
755 /* Ensure that we have a valid object */
757 if (!AcpiUtValidInternalObject (Object))
759 return_VOID;
762 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
763 "Obj %p Current Refs=%X [To Be Decremented]\n",
764 Object, Object->Common.ReferenceCount));
767 * Decrement the reference count, and only actually delete the object
768 * if the reference count becomes 0. (Must also decrement the ref count
769 * of all subobjects!)
771 (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);
772 return_VOID;