1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, 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__
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 *StartDesc;
90 ACPI_OPERAND_OBJECT **LastObjPtr;
93 ACPI_FUNCTION_TRACE_PTR (UtDeleteInternalObj, Object);
102 * Must delete or free any pointers within the object that are not
103 * actual ACPI objects (for example, a raw buffer pointer).
105 switch (Object->Common.Type)
107 case ACPI_TYPE_STRING:
109 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
110 Object, Object->String.Pointer));
112 /* Free the actual string buffer */
114 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
116 /* But only if it is NOT a pointer into an ACPI table */
118 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;
137 case ACPI_TYPE_PACKAGE:
139 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
140 Object->Package.Count));
143 * Elements of the package are not handled here, they are deleted
147 /* Free the (variable length) element pointer array */
149 ObjPointer = Object->Package.Elements;
153 * These objects have a possible list of notify handlers.
154 * Device object also may have a GPE block.
156 case ACPI_TYPE_DEVICE:
158 if (Object->Device.GpeBlock)
160 (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
163 /*lint -fallthrough */
165 case ACPI_TYPE_PROCESSOR:
166 case ACPI_TYPE_THERMAL:
168 /* Walk the address handler list for this object */
170 HandlerDesc = Object->CommonNotify.Handler;
173 NextDesc = HandlerDesc->AddressSpace.Next;
174 AcpiUtRemoveReference (HandlerDesc);
175 HandlerDesc = NextDesc;
179 case ACPI_TYPE_MUTEX:
181 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
182 "***** Mutex %p, OS Mutex %p\n",
183 Object, Object->Mutex.OsMutex));
185 if (Object == AcpiGbl_GlobalLockMutex)
187 /* Global Lock has extra semaphore */
189 (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
190 AcpiGbl_GlobalLockSemaphore = NULL;
192 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
193 AcpiGbl_GlobalLockMutex = NULL;
197 AcpiExUnlinkMutex (Object);
198 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
202 case ACPI_TYPE_EVENT:
204 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
205 "***** Event %p, OS Semaphore %p\n",
206 Object, Object->Event.OsSemaphore));
208 (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
209 Object->Event.OsSemaphore = NULL;
212 case ACPI_TYPE_METHOD:
214 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
215 "***** Method %p\n", Object));
217 /* Delete the method mutex if it exists */
219 if (Object->Method.Mutex)
221 AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
222 AcpiUtDeleteObjectDesc (Object->Method.Mutex);
223 Object->Method.Mutex = NULL;
227 case ACPI_TYPE_REGION:
229 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
230 "***** Region %p\n", Object));
233 * Update AddressRange list. However, only permanent regions
234 * are installed in this list. (Not created within a method)
236 if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
238 AcpiUtRemoveAddressRange (Object->Region.SpaceId,
239 Object->Region.Node);
242 SecondDesc = AcpiNsGetSecondaryObject (Object);
246 * Free the RegionContext if and only if the handler is one of the
247 * default handlers -- and therefore, we created the context object
248 * locally, it was not created by an external caller.
250 HandlerDesc = Object->Region.Handler;
253 NextDesc = HandlerDesc->AddressSpace.RegionList;
254 StartDesc = NextDesc;
255 LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
257 /* Remove the region object from the handler list */
261 if (NextDesc == Object)
263 *LastObjPtr = NextDesc->Region.Next;
267 /* Walk the linked list of handlers */
269 LastObjPtr = &NextDesc->Region.Next;
270 NextDesc = NextDesc->Region.Next;
272 /* Prevent infinite loop if list is corrupted */
274 if (NextDesc == StartDesc)
276 ACPI_ERROR ((AE_INFO,
277 "Circular region list in address handler object %p",
283 if (HandlerDesc->AddressSpace.HandlerFlags &
284 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
286 /* Deactivate region and free region context */
288 if (HandlerDesc->AddressSpace.Setup)
290 (void) HandlerDesc->AddressSpace.Setup (Object,
291 ACPI_REGION_DEACTIVATE,
292 HandlerDesc->AddressSpace.Context,
293 &SecondDesc->Extra.RegionContext);
297 AcpiUtRemoveReference (HandlerDesc);
300 /* Now we can free the Extra object */
302 AcpiUtDeleteObjectDesc (SecondDesc);
306 case ACPI_TYPE_BUFFER_FIELD:
308 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
309 "***** Buffer Field %p\n", Object));
311 SecondDesc = AcpiNsGetSecondaryObject (Object);
314 AcpiUtDeleteObjectDesc (SecondDesc);
318 case ACPI_TYPE_LOCAL_BANK_FIELD:
320 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
321 "***** Bank Field %p\n", Object));
323 SecondDesc = AcpiNsGetSecondaryObject (Object);
326 AcpiUtDeleteObjectDesc (SecondDesc);
335 /* Free any allocated memory (pointer within the object) found above */
339 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
341 ACPI_FREE (ObjPointer);
344 /* Now the object can be safely deleted */
346 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
347 Object, AcpiUtGetObjectTypeName (Object)));
349 AcpiUtDeleteObjectDesc (Object);
354 /*******************************************************************************
356 * FUNCTION: AcpiUtDeleteInternalObjectList
358 * PARAMETERS: ObjList - Pointer to the list to be deleted
362 * DESCRIPTION: This function deletes an internal object list, including both
363 * simple objects and package objects
365 ******************************************************************************/
368 AcpiUtDeleteInternalObjectList (
369 ACPI_OPERAND_OBJECT **ObjList)
371 ACPI_OPERAND_OBJECT **InternalObj;
374 ACPI_FUNCTION_ENTRY ();
377 /* Walk the null-terminated internal list */
379 for (InternalObj = ObjList; *InternalObj; InternalObj++)
381 AcpiUtRemoveReference (*InternalObj);
384 /* Free the combined parameter pointer list and object array */
391 /*******************************************************************************
393 * FUNCTION: AcpiUtUpdateRefCount
395 * PARAMETERS: Object - Object whose ref count is to be updated
396 * Action - What to do (REF_INCREMENT or REF_DECREMENT)
398 * RETURN: None. Sets new reference count within the object
400 * DESCRIPTION: Modify the reference count for an internal acpi object
402 ******************************************************************************/
405 AcpiUtUpdateRefCount (
406 ACPI_OPERAND_OBJECT *Object,
409 UINT16 OriginalCount;
411 ACPI_CPU_FLAGS LockFlags;
414 ACPI_FUNCTION_NAME (UtUpdateRefCount);
423 * Always get the reference count lock. Note: Interpreter and/or
424 * Namespace is not always locked when this function is called.
426 LockFlags = AcpiOsAcquireLock (AcpiGbl_ReferenceCountLock);
427 OriginalCount = Object->Common.ReferenceCount;
429 /* Perform the reference count action (increment, decrement) */
435 NewCount = OriginalCount + 1;
436 Object->Common.ReferenceCount = NewCount;
437 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
439 /* The current reference count should never be zero here */
443 ACPI_WARNING ((AE_INFO,
444 "Obj %p, Reference Count was zero before increment\n",
448 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
449 "Obj %p Type %.2X Refs %.2X [Incremented]\n",
450 Object, Object->Common.Type, NewCount));
455 /* The current reference count must be non-zero */
459 NewCount = OriginalCount - 1;
460 Object->Common.ReferenceCount = NewCount;
463 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
467 ACPI_WARNING ((AE_INFO,
468 "Obj %p, Reference Count is already zero, cannot decrement\n",
472 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
473 "Obj %p Type %.2X Refs %.2X [Decremented]\n",
474 Object, Object->Common.Type, NewCount));
476 /* Actually delete the object on a reference count of zero */
480 AcpiUtDeleteInternalObj (Object);
486 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
487 ACPI_ERROR ((AE_INFO, "Unknown Reference Count action (0x%X)",
493 * Sanity check the reference count, for debug purposes only.
494 * (A deleted object will have a huge reference count)
496 if (NewCount > ACPI_MAX_REFERENCE_COUNT)
498 ACPI_WARNING ((AE_INFO,
499 "Large Reference Count (0x%X) in object %p, Type=0x%.2X",
500 NewCount, Object, Object->Common.Type));
505 /*******************************************************************************
507 * FUNCTION: AcpiUtUpdateObjectReference
509 * PARAMETERS: Object - Increment ref count for this object
510 * and all sub-objects
511 * Action - Either REF_INCREMENT or REF_DECREMENT
515 * DESCRIPTION: Increment the object reference count
517 * Object references are incremented when:
518 * 1) An object is attached to a Node (namespace object)
519 * 2) An object is copied (all subobjects must be incremented)
521 * Object references are decremented when:
522 * 1) An object is detached from an Node
524 ******************************************************************************/
527 AcpiUtUpdateObjectReference (
528 ACPI_OPERAND_OBJECT *Object,
531 ACPI_STATUS Status = AE_OK;
532 ACPI_GENERIC_STATE *StateList = NULL;
533 ACPI_OPERAND_OBJECT *NextObject = NULL;
534 ACPI_OPERAND_OBJECT *PrevObject;
535 ACPI_GENERIC_STATE *State;
539 ACPI_FUNCTION_NAME (UtUpdateObjectReference);
544 /* Make sure that this isn't a namespace handle */
546 if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
548 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
549 "Object %p is NS handle\n", Object));
554 * All sub-objects must have their reference count incremented also.
555 * Different object types have different subobjects.
557 switch (Object->Common.Type)
559 case ACPI_TYPE_DEVICE:
560 case ACPI_TYPE_PROCESSOR:
561 case ACPI_TYPE_POWER:
562 case ACPI_TYPE_THERMAL:
564 * Update the notify objects for these types (if present)
565 * Two lists, system and device notify handlers.
567 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
569 PrevObject = Object->CommonNotify.NotifyList[i];
572 NextObject = PrevObject->Notify.Next[i];
573 AcpiUtUpdateRefCount (PrevObject, Action);
574 PrevObject = NextObject;
579 case ACPI_TYPE_PACKAGE:
581 * We must update all the sub-objects of the package,
582 * each of whom may have their own sub-objects.
584 for (i = 0; i < Object->Package.Count; i++)
587 * Null package elements are legal and can be simply
590 NextObject = Object->Package.Elements[i];
596 switch (NextObject->Common.Type)
598 case ACPI_TYPE_INTEGER:
599 case ACPI_TYPE_STRING:
600 case ACPI_TYPE_BUFFER:
602 * For these very simple sub-objects, we can just
603 * update the reference count here and continue.
604 * Greatly increases performance of this operation.
606 AcpiUtUpdateRefCount (NextObject, Action);
611 * For complex sub-objects, push them onto the stack
612 * for later processing (this eliminates recursion.)
614 Status = AcpiUtCreateUpdateStateAndPush (
615 NextObject, Action, &StateList);
616 if (ACPI_FAILURE (Status))
626 case ACPI_TYPE_BUFFER_FIELD:
628 NextObject = Object->BufferField.BufferObj;
631 case ACPI_TYPE_LOCAL_REGION_FIELD:
633 NextObject = Object->Field.RegionObj;
636 case ACPI_TYPE_LOCAL_BANK_FIELD:
638 NextObject = Object->BankField.BankObj;
639 Status = AcpiUtCreateUpdateStateAndPush (
640 Object->BankField.RegionObj, Action, &StateList);
641 if (ACPI_FAILURE (Status))
647 case ACPI_TYPE_LOCAL_INDEX_FIELD:
649 NextObject = Object->IndexField.IndexObj;
650 Status = AcpiUtCreateUpdateStateAndPush (
651 Object->IndexField.DataObj, Action, &StateList);
652 if (ACPI_FAILURE (Status))
658 case ACPI_TYPE_LOCAL_REFERENCE:
660 * The target of an Index (a package, string, or buffer) or a named
661 * reference must track changes to the ref count of the index or
664 if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
665 (Object->Reference.Class== ACPI_REFCLASS_NAME))
667 NextObject = Object->Reference.Object;
671 case ACPI_TYPE_REGION:
674 break; /* No subobjects for all other types */
678 * Now we can update the count in the main object. This can only
679 * happen after we update the sub-objects in case this causes the
680 * main object to be deleted.
682 AcpiUtUpdateRefCount (Object, Action);
685 /* Move on to the next object to be updated */
694 State = AcpiUtPopGenericState (&StateList);
695 Object = State->Update.Object;
696 AcpiUtDeleteGenericState (State);
705 ACPI_EXCEPTION ((AE_INFO, Status,
706 "Could not update object reference count"));
708 /* Free any stacked Update State objects */
712 State = AcpiUtPopGenericState (&StateList);
713 AcpiUtDeleteGenericState (State);
720 /*******************************************************************************
722 * FUNCTION: AcpiUtAddReference
724 * PARAMETERS: Object - Object whose reference count is to be
729 * DESCRIPTION: Add one reference to an ACPI object
731 ******************************************************************************/
735 ACPI_OPERAND_OBJECT *Object)
738 ACPI_FUNCTION_NAME (UtAddReference);
741 /* Ensure that we have a valid object */
743 if (!AcpiUtValidInternalObject (Object))
748 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
749 "Obj %p Current Refs=%X [To Be Incremented]\n",
750 Object, Object->Common.ReferenceCount));
752 /* Increment the reference count */
754 (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
759 /*******************************************************************************
761 * FUNCTION: AcpiUtRemoveReference
763 * PARAMETERS: Object - Object whose ref count will be decremented
767 * DESCRIPTION: Decrement the reference count of an ACPI internal object
769 ******************************************************************************/
772 AcpiUtRemoveReference (
773 ACPI_OPERAND_OBJECT *Object)
776 ACPI_FUNCTION_NAME (UtRemoveReference);
780 * Allow a NULL pointer to be passed in, just ignore it. This saves
781 * each caller from having to check. Also, ignore NS nodes.
784 (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
790 /* Ensure that we have a valid object */
792 if (!AcpiUtValidInternalObject (Object))
797 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
798 "Obj %p Current Refs=%X [To Be Decremented]\n",
799 Object, Object->Common.ReferenceCount));
802 * Decrement the reference count, and only actually delete the object
803 * if the reference count becomes 0. (Must also decrement the ref count
804 * of all subobjects!)
806 (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);