1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2013, 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;
121 case ACPI_TYPE_BUFFER:
123 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
124 Object, Object->Buffer.Pointer));
126 /* Free the actual buffer */
128 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
130 /* But only if it is NOT a pointer into an ACPI table */
132 ObjPointer = Object->Buffer.Pointer;
136 case ACPI_TYPE_PACKAGE:
138 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
139 Object->Package.Count));
142 * Elements of the package are not handled here, they are deleted
146 /* Free the (variable length) element pointer array */
148 ObjPointer = Object->Package.Elements;
152 * These objects have a possible list of notify handlers.
153 * Device object also may have a GPE block.
155 case ACPI_TYPE_DEVICE:
157 if (Object->Device.GpeBlock)
159 (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
162 /*lint -fallthrough */
164 case ACPI_TYPE_PROCESSOR:
165 case ACPI_TYPE_THERMAL:
167 /* Walk the address handler list for this object */
169 HandlerDesc = Object->CommonNotify.Handler;
172 NextDesc = HandlerDesc->AddressSpace.Next;
173 AcpiUtRemoveReference (HandlerDesc);
174 HandlerDesc = NextDesc;
178 case ACPI_TYPE_MUTEX:
180 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
181 "***** Mutex %p, OS Mutex %p\n",
182 Object, Object->Mutex.OsMutex));
184 if (Object == AcpiGbl_GlobalLockMutex)
186 /* Global Lock has extra semaphore */
188 (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
189 AcpiGbl_GlobalLockSemaphore = NULL;
191 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
192 AcpiGbl_GlobalLockMutex = NULL;
196 AcpiExUnlinkMutex (Object);
197 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
201 case ACPI_TYPE_EVENT:
203 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
204 "***** Event %p, OS Semaphore %p\n",
205 Object, Object->Event.OsSemaphore));
207 (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
208 Object->Event.OsSemaphore = NULL;
211 case ACPI_TYPE_METHOD:
213 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
214 "***** Method %p\n", Object));
216 /* Delete the method mutex if it exists */
218 if (Object->Method.Mutex)
220 AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
221 AcpiUtDeleteObjectDesc (Object->Method.Mutex);
222 Object->Method.Mutex = NULL;
226 case ACPI_TYPE_REGION:
228 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
229 "***** Region %p\n", Object));
232 * Update AddressRange list. However, only permanent regions
233 * are installed in this list. (Not created within a method)
235 if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
237 AcpiUtRemoveAddressRange (Object->Region.SpaceId,
238 Object->Region.Node);
241 SecondDesc = AcpiNsGetSecondaryObject (Object);
245 * Free the RegionContext if and only if the handler is one of the
246 * default handlers -- and therefore, we created the context object
247 * locally, it was not created by an external caller.
249 HandlerDesc = Object->Region.Handler;
252 NextDesc = HandlerDesc->AddressSpace.RegionList;
253 LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
255 /* Remove the region object from the handler's list */
259 if (NextDesc == Object)
261 *LastObjPtr = NextDesc->Region.Next;
265 /* Walk the linked list of handler */
267 LastObjPtr = &NextDesc->Region.Next;
268 NextDesc = NextDesc->Region.Next;
271 if (HandlerDesc->AddressSpace.HandlerFlags &
272 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
274 /* Deactivate region and free region context */
276 if (HandlerDesc->AddressSpace.Setup)
278 (void) HandlerDesc->AddressSpace.Setup (Object,
279 ACPI_REGION_DEACTIVATE,
280 HandlerDesc->AddressSpace.Context,
281 &SecondDesc->Extra.RegionContext);
285 AcpiUtRemoveReference (HandlerDesc);
288 /* Now we can free the Extra object */
290 AcpiUtDeleteObjectDesc (SecondDesc);
294 case ACPI_TYPE_BUFFER_FIELD:
296 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
297 "***** Buffer Field %p\n", Object));
299 SecondDesc = AcpiNsGetSecondaryObject (Object);
302 AcpiUtDeleteObjectDesc (SecondDesc);
306 case ACPI_TYPE_LOCAL_BANK_FIELD:
308 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
309 "***** Bank Field %p\n", Object));
311 SecondDesc = AcpiNsGetSecondaryObject (Object);
314 AcpiUtDeleteObjectDesc (SecondDesc);
323 /* Free any allocated memory (pointer within the object) found above */
327 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
329 ACPI_FREE (ObjPointer);
332 /* Now the object can be safely deleted */
334 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
335 Object, AcpiUtGetObjectTypeName (Object)));
337 AcpiUtDeleteObjectDesc (Object);
342 /*******************************************************************************
344 * FUNCTION: AcpiUtDeleteInternalObjectList
346 * PARAMETERS: ObjList - Pointer to the list to be deleted
350 * DESCRIPTION: This function deletes an internal object list, including both
351 * simple objects and package objects
353 ******************************************************************************/
356 AcpiUtDeleteInternalObjectList (
357 ACPI_OPERAND_OBJECT **ObjList)
359 ACPI_OPERAND_OBJECT **InternalObj;
362 ACPI_FUNCTION_ENTRY ();
365 /* Walk the null-terminated internal list */
367 for (InternalObj = ObjList; *InternalObj; InternalObj++)
369 AcpiUtRemoveReference (*InternalObj);
372 /* Free the combined parameter pointer list and object array */
379 /*******************************************************************************
381 * FUNCTION: AcpiUtUpdateRefCount
383 * PARAMETERS: Object - Object whose ref count is to be updated
384 * Action - What to do (REF_INCREMENT or REF_DECREMENT)
386 * RETURN: None. Sets new reference count within the object
388 * DESCRIPTION: Modify the reference count for an internal acpi object
390 ******************************************************************************/
393 AcpiUtUpdateRefCount (
394 ACPI_OPERAND_OBJECT *Object,
397 UINT16 OriginalCount;
399 ACPI_CPU_FLAGS LockFlags;
402 ACPI_FUNCTION_NAME (UtUpdateRefCount);
411 * Always get the reference count lock. Note: Interpreter and/or
412 * Namespace is not always locked when this function is called.
414 LockFlags = AcpiOsAcquireLock (AcpiGbl_ReferenceCountLock);
415 OriginalCount = Object->Common.ReferenceCount;
417 /* Perform the reference count action (increment, decrement) */
423 NewCount = OriginalCount + 1;
424 Object->Common.ReferenceCount = NewCount;
425 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
427 /* The current reference count should never be zero here */
431 ACPI_WARNING ((AE_INFO,
432 "Obj %p, Reference Count was zero before increment\n",
436 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
437 "Obj %p Type %.2X Refs %.2X [Incremented]\n",
438 Object, Object->Common.Type, NewCount));
443 /* The current reference count must be non-zero */
447 NewCount = OriginalCount - 1;
448 Object->Common.ReferenceCount = NewCount;
451 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
455 ACPI_WARNING ((AE_INFO,
456 "Obj %p, Reference Count is already zero, cannot decrement\n",
460 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
461 "Obj %p Type %.2X Refs %.2X [Decremented]\n",
462 Object, Object->Common.Type, NewCount));
464 /* Actually delete the object on a reference count of zero */
468 AcpiUtDeleteInternalObj (Object);
474 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
475 ACPI_ERROR ((AE_INFO, "Unknown Reference Count action (0x%X)",
481 * Sanity check the reference count, for debug purposes only.
482 * (A deleted object will have a huge reference count)
484 if (NewCount > ACPI_MAX_REFERENCE_COUNT)
486 ACPI_WARNING ((AE_INFO,
487 "Large Reference Count (0x%X) in object %p, Type=0x%.2X",
488 NewCount, Object, Object->Common.Type));
493 /*******************************************************************************
495 * FUNCTION: AcpiUtUpdateObjectReference
497 * PARAMETERS: Object - Increment ref count for this object
498 * and all sub-objects
499 * Action - Either REF_INCREMENT or REF_DECREMENT
503 * DESCRIPTION: Increment the object reference count
505 * Object references are incremented when:
506 * 1) An object is attached to a Node (namespace object)
507 * 2) An object is copied (all subobjects must be incremented)
509 * Object references are decremented when:
510 * 1) An object is detached from an Node
512 ******************************************************************************/
515 AcpiUtUpdateObjectReference (
516 ACPI_OPERAND_OBJECT *Object,
519 ACPI_STATUS Status = AE_OK;
520 ACPI_GENERIC_STATE *StateList = NULL;
521 ACPI_OPERAND_OBJECT *NextObject = NULL;
522 ACPI_OPERAND_OBJECT *PrevObject;
523 ACPI_GENERIC_STATE *State;
527 ACPI_FUNCTION_NAME (UtUpdateObjectReference);
532 /* Make sure that this isn't a namespace handle */
534 if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
536 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
537 "Object %p is NS handle\n", Object));
542 * All sub-objects must have their reference count incremented also.
543 * Different object types have different subobjects.
545 switch (Object->Common.Type)
547 case ACPI_TYPE_DEVICE:
548 case ACPI_TYPE_PROCESSOR:
549 case ACPI_TYPE_POWER:
550 case ACPI_TYPE_THERMAL:
552 * Update the notify objects for these types (if present)
553 * Two lists, system and device notify handlers.
555 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
557 PrevObject = Object->CommonNotify.NotifyList[i];
560 NextObject = PrevObject->Notify.Next[i];
561 AcpiUtUpdateRefCount (PrevObject, Action);
562 PrevObject = NextObject;
567 case ACPI_TYPE_PACKAGE:
569 * We must update all the sub-objects of the package,
570 * each of whom may have their own sub-objects.
572 for (i = 0; i < Object->Package.Count; i++)
575 * Null package elements are legal and can be simply
578 NextObject = Object->Package.Elements[i];
584 switch (NextObject->Common.Type)
586 case ACPI_TYPE_INTEGER:
587 case ACPI_TYPE_STRING:
588 case ACPI_TYPE_BUFFER:
590 * For these very simple sub-objects, we can just
591 * update the reference count here and continue.
592 * Greatly increases performance of this operation.
594 AcpiUtUpdateRefCount (NextObject, Action);
599 * For complex sub-objects, push them onto the stack
600 * for later processing (this eliminates recursion.)
602 Status = AcpiUtCreateUpdateStateAndPush (
603 NextObject, Action, &StateList);
604 if (ACPI_FAILURE (Status))
614 case ACPI_TYPE_BUFFER_FIELD:
616 NextObject = Object->BufferField.BufferObj;
619 case ACPI_TYPE_LOCAL_REGION_FIELD:
621 NextObject = Object->Field.RegionObj;
624 case ACPI_TYPE_LOCAL_BANK_FIELD:
626 NextObject = Object->BankField.BankObj;
627 Status = AcpiUtCreateUpdateStateAndPush (
628 Object->BankField.RegionObj, Action, &StateList);
629 if (ACPI_FAILURE (Status))
635 case ACPI_TYPE_LOCAL_INDEX_FIELD:
637 NextObject = Object->IndexField.IndexObj;
638 Status = AcpiUtCreateUpdateStateAndPush (
639 Object->IndexField.DataObj, Action, &StateList);
640 if (ACPI_FAILURE (Status))
646 case ACPI_TYPE_LOCAL_REFERENCE:
648 * The target of an Index (a package, string, or buffer) or a named
649 * reference must track changes to the ref count of the index or
652 if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
653 (Object->Reference.Class== ACPI_REFCLASS_NAME))
655 NextObject = Object->Reference.Object;
659 case ACPI_TYPE_REGION:
662 break; /* No subobjects for all other types */
666 * Now we can update the count in the main object. This can only
667 * happen after we update the sub-objects in case this causes the
668 * main object to be deleted.
670 AcpiUtUpdateRefCount (Object, Action);
673 /* Move on to the next object to be updated */
682 State = AcpiUtPopGenericState (&StateList);
683 Object = State->Update.Object;
684 AcpiUtDeleteGenericState (State);
693 ACPI_EXCEPTION ((AE_INFO, Status,
694 "Could not update object reference count"));
696 /* Free any stacked Update State objects */
700 State = AcpiUtPopGenericState (&StateList);
701 AcpiUtDeleteGenericState (State);
708 /*******************************************************************************
710 * FUNCTION: AcpiUtAddReference
712 * PARAMETERS: Object - Object whose reference count is to be
717 * DESCRIPTION: Add one reference to an ACPI object
719 ******************************************************************************/
723 ACPI_OPERAND_OBJECT *Object)
726 ACPI_FUNCTION_NAME (UtAddReference);
729 /* Ensure that we have a valid object */
731 if (!AcpiUtValidInternalObject (Object))
736 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
737 "Obj %p Current Refs=%X [To Be Incremented]\n",
738 Object, Object->Common.ReferenceCount));
740 /* Increment the reference count */
742 (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
747 /*******************************************************************************
749 * FUNCTION: AcpiUtRemoveReference
751 * PARAMETERS: Object - Object whose ref count will be decremented
755 * DESCRIPTION: Decrement the reference count of an ACPI internal object
757 ******************************************************************************/
760 AcpiUtRemoveReference (
761 ACPI_OPERAND_OBJECT *Object)
764 ACPI_FUNCTION_NAME (UtRemoveReference);
768 * Allow a NULL pointer to be passed in, just ignore it. This saves
769 * each caller from having to check. Also, ignore NS nodes.
772 (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
778 /* Ensure that we have a valid object */
780 if (!AcpiUtValidInternalObject (Object))
785 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
786 "Obj %p Current Refs=%X [To Be Decremented]\n",
787 Object, Object->Common.ReferenceCount));
790 * Decrement the reference count, and only actually delete the object
791 * if the reference count becomes 0. (Must also decrement the ref count
792 * of all subobjects!)
794 (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);