1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2011, 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.
44 #define __UTDELETE_C__
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acinterp.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/acevents.h>
53 #define _COMPONENT ACPI_UTILITIES
54 ACPI_MODULE_NAME ("utdelete")
56 /* Local prototypes */
59 AcpiUtDeleteInternalObj (
60 ACPI_OPERAND_OBJECT *Object);
63 AcpiUtUpdateRefCount (
64 ACPI_OPERAND_OBJECT *Object,
68 /*******************************************************************************
70 * FUNCTION: AcpiUtDeleteInternalObj
72 * PARAMETERS: Object - Object to be deleted
76 * DESCRIPTION: Low level object deletion, after reference counts have been
77 * updated (All reference counts, including sub-objects!)
79 ******************************************************************************/
82 AcpiUtDeleteInternalObj (
83 ACPI_OPERAND_OBJECT *Object)
85 void *ObjPointer = NULL;
86 ACPI_OPERAND_OBJECT *HandlerDesc;
87 ACPI_OPERAND_OBJECT *SecondDesc;
88 ACPI_OPERAND_OBJECT *NextDesc;
89 ACPI_OPERAND_OBJECT **LastObjPtr;
92 ACPI_FUNCTION_TRACE_PTR (UtDeleteInternalObj, Object);
101 * Must delete or free any pointers within the object that are not
102 * actual ACPI objects (for example, a raw buffer pointer).
104 switch (Object->Common.Type)
106 case ACPI_TYPE_STRING:
108 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
109 Object, Object->String.Pointer));
111 /* Free the actual string buffer */
113 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
115 /* But only if it is NOT a pointer into an ACPI table */
117 ObjPointer = Object->String.Pointer;
122 case ACPI_TYPE_BUFFER:
124 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
125 Object, Object->Buffer.Pointer));
127 /* Free the actual buffer */
129 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
131 /* But only if it is NOT a pointer into an ACPI table */
133 ObjPointer = Object->Buffer.Pointer;
138 case ACPI_TYPE_PACKAGE:
140 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
141 Object->Package.Count));
144 * Elements of the package are not handled here, they are deleted
148 /* Free the (variable length) element pointer array */
150 ObjPointer = Object->Package.Elements;
155 * These objects have a possible list of notify handlers.
156 * Device object also may have a GPE block.
158 case ACPI_TYPE_DEVICE:
160 if (Object->Device.GpeBlock)
162 (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
165 /*lint -fallthrough */
167 case ACPI_TYPE_PROCESSOR:
168 case ACPI_TYPE_THERMAL:
170 /* Walk the notify handler list for this object */
172 HandlerDesc = Object->CommonNotify.Handler;
175 NextDesc = HandlerDesc->AddressSpace.Next;
176 AcpiUtRemoveReference (HandlerDesc);
177 HandlerDesc = NextDesc;
182 case ACPI_TYPE_MUTEX:
184 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
185 "***** Mutex %p, OS Mutex %p\n",
186 Object, Object->Mutex.OsMutex));
188 if (Object == AcpiGbl_GlobalLockMutex)
190 /* Global Lock has extra semaphore */
192 (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
193 AcpiGbl_GlobalLockSemaphore = NULL;
195 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
196 AcpiGbl_GlobalLockMutex = NULL;
200 AcpiExUnlinkMutex (Object);
201 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
206 case ACPI_TYPE_EVENT:
208 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
209 "***** Event %p, OS Semaphore %p\n",
210 Object, Object->Event.OsSemaphore));
212 (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
213 Object->Event.OsSemaphore = NULL;
217 case ACPI_TYPE_METHOD:
219 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
220 "***** Method %p\n", Object));
222 /* Delete the method mutex if it exists */
224 if (Object->Method.Mutex)
226 AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
227 AcpiUtDeleteObjectDesc (Object->Method.Mutex);
228 Object->Method.Mutex = NULL;
233 case ACPI_TYPE_REGION:
235 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
236 "***** Region %p\n", Object));
238 SecondDesc = AcpiNsGetSecondaryObject (Object);
242 * Free the RegionContext if and only if the handler is one of the
243 * default handlers -- and therefore, we created the context object
244 * locally, it was not created by an external caller.
246 HandlerDesc = Object->Region.Handler;
249 NextDesc = HandlerDesc->AddressSpace.RegionList;
250 LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
252 /* Remove the region object from the handler's list */
256 if (NextDesc == Object)
258 *LastObjPtr = NextDesc->Region.Next;
262 /* Walk the linked list of handler */
264 LastObjPtr = &NextDesc->Region.Next;
265 NextDesc = NextDesc->Region.Next;
268 if (HandlerDesc->AddressSpace.HandlerFlags &
269 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
271 /* Deactivate region and free region context */
273 if (HandlerDesc->AddressSpace.Setup)
275 (void) HandlerDesc->AddressSpace.Setup (Object,
276 ACPI_REGION_DEACTIVATE,
277 HandlerDesc->AddressSpace.Context,
278 &SecondDesc->Extra.RegionContext);
282 AcpiUtRemoveReference (HandlerDesc);
285 /* Now we can free the Extra object */
287 AcpiUtDeleteObjectDesc (SecondDesc);
292 case ACPI_TYPE_BUFFER_FIELD:
294 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
295 "***** Buffer Field %p\n", Object));
297 SecondDesc = AcpiNsGetSecondaryObject (Object);
300 AcpiUtDeleteObjectDesc (SecondDesc);
305 case ACPI_TYPE_LOCAL_BANK_FIELD:
307 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
308 "***** Bank Field %p\n", Object));
310 SecondDesc = AcpiNsGetSecondaryObject (Object);
313 AcpiUtDeleteObjectDesc (SecondDesc);
322 /* Free any allocated memory (pointer within the object) found above */
326 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
328 ACPI_FREE (ObjPointer);
331 /* Now the object can be safely deleted */
333 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
334 Object, AcpiUtGetObjectTypeName (Object)));
336 AcpiUtDeleteObjectDesc (Object);
341 /*******************************************************************************
343 * FUNCTION: AcpiUtDeleteInternalObjectList
345 * PARAMETERS: ObjList - Pointer to the list to be deleted
349 * DESCRIPTION: This function deletes an internal object list, including both
350 * simple objects and package objects
352 ******************************************************************************/
355 AcpiUtDeleteInternalObjectList (
356 ACPI_OPERAND_OBJECT **ObjList)
358 ACPI_OPERAND_OBJECT **InternalObj;
361 ACPI_FUNCTION_TRACE (UtDeleteInternalObjectList);
364 /* Walk the null-terminated internal list */
366 for (InternalObj = ObjList; *InternalObj; InternalObj++)
368 AcpiUtRemoveReference (*InternalObj);
371 /* Free the combined parameter pointer list and object array */
378 /*******************************************************************************
380 * FUNCTION: AcpiUtUpdateRefCount
382 * PARAMETERS: Object - Object whose ref count is to be updated
383 * Action - What to do
385 * RETURN: New ref count
387 * DESCRIPTION: Modify the ref count and return it.
389 ******************************************************************************/
392 AcpiUtUpdateRefCount (
393 ACPI_OPERAND_OBJECT *Object,
400 ACPI_FUNCTION_NAME (UtUpdateRefCount);
408 Count = Object->Common.ReferenceCount;
412 * Perform the reference count action (increment, decrement, force delete)
419 Object->Common.ReferenceCount = NewCount;
421 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
422 "Obj %p Refs=%X, [Incremented]\n",
430 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
431 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
440 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
441 "Obj %p Refs=%X, [Decremented]\n",
445 if (Object->Common.Type == ACPI_TYPE_METHOD)
447 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
448 "Method Obj %p Refs=%X, [Decremented]\n", Object, NewCount));
451 Object->Common.ReferenceCount = NewCount;
454 AcpiUtDeleteInternalObj (Object);
458 case REF_FORCE_DELETE:
460 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
461 "Obj %p Refs=%X, Force delete! (Set to 0)\n", Object, Count));
464 Object->Common.ReferenceCount = NewCount;
465 AcpiUtDeleteInternalObj (Object);
470 ACPI_ERROR ((AE_INFO, "Unknown action (0x%X)", Action));
475 * Sanity check the reference count, for debug purposes only.
476 * (A deleted object will have a huge reference count)
478 if (Count > ACPI_MAX_REFERENCE_COUNT)
480 ACPI_WARNING ((AE_INFO,
481 "Large Reference Count (0x%X) in object %p", Count, Object));
486 /*******************************************************************************
488 * FUNCTION: AcpiUtUpdateObjectReference
490 * PARAMETERS: Object - Increment ref count for this object
491 * and all sub-objects
492 * Action - Either REF_INCREMENT or REF_DECREMENT or
497 * DESCRIPTION: Increment the object reference count
499 * Object references are incremented when:
500 * 1) An object is attached to a Node (namespace object)
501 * 2) An object is copied (all subobjects must be incremented)
503 * Object references are decremented when:
504 * 1) An object is detached from an Node
506 ******************************************************************************/
509 AcpiUtUpdateObjectReference (
510 ACPI_OPERAND_OBJECT *Object,
513 ACPI_STATUS Status = AE_OK;
514 ACPI_GENERIC_STATE *StateList = NULL;
515 ACPI_OPERAND_OBJECT *NextObject = NULL;
516 ACPI_GENERIC_STATE *State;
520 ACPI_FUNCTION_TRACE_PTR (UtUpdateObjectReference, Object);
525 /* Make sure that this isn't a namespace handle */
527 if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
529 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
530 "Object %p is NS handle\n", Object));
531 return_ACPI_STATUS (AE_OK);
535 * All sub-objects must have their reference count incremented also.
536 * Different object types have different subobjects.
538 switch (Object->Common.Type)
540 case ACPI_TYPE_DEVICE:
541 case ACPI_TYPE_PROCESSOR:
542 case ACPI_TYPE_POWER:
543 case ACPI_TYPE_THERMAL:
545 /* Update the notify objects for these types (if present) */
547 AcpiUtUpdateRefCount (Object->CommonNotify.SystemNotify, Action);
548 AcpiUtUpdateRefCount (Object->CommonNotify.DeviceNotify, Action);
551 case ACPI_TYPE_PACKAGE:
553 * We must update all the sub-objects of the package,
554 * each of whom may have their own sub-objects.
556 for (i = 0; i < Object->Package.Count; i++)
559 * Push each element onto the stack for later processing.
560 * Note: There can be null elements within the package,
561 * these are simply ignored
563 Status = AcpiUtCreateUpdateStateAndPush (
564 Object->Package.Elements[i], Action, &StateList);
565 if (ACPI_FAILURE (Status))
572 case ACPI_TYPE_BUFFER_FIELD:
574 NextObject = Object->BufferField.BufferObj;
577 case ACPI_TYPE_LOCAL_REGION_FIELD:
579 NextObject = Object->Field.RegionObj;
582 case ACPI_TYPE_LOCAL_BANK_FIELD:
584 NextObject = Object->BankField.BankObj;
585 Status = AcpiUtCreateUpdateStateAndPush (
586 Object->BankField.RegionObj, Action, &StateList);
587 if (ACPI_FAILURE (Status))
593 case ACPI_TYPE_LOCAL_INDEX_FIELD:
595 NextObject = Object->IndexField.IndexObj;
596 Status = AcpiUtCreateUpdateStateAndPush (
597 Object->IndexField.DataObj, Action, &StateList);
598 if (ACPI_FAILURE (Status))
604 case ACPI_TYPE_LOCAL_REFERENCE:
606 * The target of an Index (a package, string, or buffer) or a named
607 * reference must track changes to the ref count of the index or
610 if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
611 (Object->Reference.Class== ACPI_REFCLASS_NAME))
613 NextObject = Object->Reference.Object;
617 case ACPI_TYPE_REGION:
619 break; /* No subobjects for all other types */
623 * Now we can update the count in the main object. This can only
624 * happen after we update the sub-objects in case this causes the
625 * main object to be deleted.
627 AcpiUtUpdateRefCount (Object, Action);
630 /* Move on to the next object to be updated */
639 State = AcpiUtPopGenericState (&StateList);
640 Object = State->Update.Object;
641 AcpiUtDeleteGenericState (State);
645 return_ACPI_STATUS (AE_OK);
650 ACPI_EXCEPTION ((AE_INFO, Status,
651 "Could not update object reference count"));
653 /* Free any stacked Update State objects */
657 State = AcpiUtPopGenericState (&StateList);
658 AcpiUtDeleteGenericState (State);
661 return_ACPI_STATUS (Status);
665 /*******************************************************************************
667 * FUNCTION: AcpiUtAddReference
669 * PARAMETERS: Object - Object whose reference count is to be
674 * DESCRIPTION: Add one reference to an ACPI object
676 ******************************************************************************/
680 ACPI_OPERAND_OBJECT *Object)
683 ACPI_FUNCTION_TRACE_PTR (UtAddReference, Object);
686 /* Ensure that we have a valid object */
688 if (!AcpiUtValidInternalObject (Object))
693 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
694 "Obj %p Current Refs=%X [To Be Incremented]\n",
695 Object, Object->Common.ReferenceCount));
697 /* Increment the reference count */
699 (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
704 /*******************************************************************************
706 * FUNCTION: AcpiUtRemoveReference
708 * PARAMETERS: Object - Object whose ref count will be decremented
712 * DESCRIPTION: Decrement the reference count of an ACPI internal object
714 ******************************************************************************/
717 AcpiUtRemoveReference (
718 ACPI_OPERAND_OBJECT *Object)
721 ACPI_FUNCTION_TRACE_PTR (UtRemoveReference, Object);
725 * Allow a NULL pointer to be passed in, just ignore it. This saves
726 * each caller from having to check. Also, ignore NS nodes.
730 (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
736 /* Ensure that we have a valid object */
738 if (!AcpiUtValidInternalObject (Object))
743 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
744 "Obj %p Current Refs=%X [To Be Decremented]\n",
745 Object, Object->Common.ReferenceCount));
748 * Decrement the reference count, and only actually delete the object
749 * if the reference count becomes 0. (Must also decrement the ref count
750 * of all subobjects!)
752 (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);