1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acinterp.h>
47 #include <contrib/dev/acpica/include/acnamesp.h>
48 #include <contrib/dev/acpica/include/acevents.h>
51 #define _COMPONENT ACPI_UTILITIES
52 ACPI_MODULE_NAME ("utdelete")
54 /* Local prototypes */
57 AcpiUtDeleteInternalObj (
58 ACPI_OPERAND_OBJECT *Object);
61 AcpiUtUpdateRefCount (
62 ACPI_OPERAND_OBJECT *Object,
66 /*******************************************************************************
68 * FUNCTION: AcpiUtDeleteInternalObj
70 * PARAMETERS: Object - Object to be deleted
74 * DESCRIPTION: Low level object deletion, after reference counts have been
75 * updated (All reference counts, including sub-objects!)
77 ******************************************************************************/
80 AcpiUtDeleteInternalObj (
81 ACPI_OPERAND_OBJECT *Object)
83 void *ObjPointer = NULL;
84 ACPI_OPERAND_OBJECT *HandlerDesc;
85 ACPI_OPERAND_OBJECT *SecondDesc;
86 ACPI_OPERAND_OBJECT *NextDesc;
87 ACPI_OPERAND_OBJECT *StartDesc;
88 ACPI_OPERAND_OBJECT **LastObjPtr;
91 ACPI_FUNCTION_TRACE_PTR (UtDeleteInternalObj, Object);
100 * Must delete or free any pointers within the object that are not
101 * actual ACPI objects (for example, a raw buffer pointer).
103 switch (Object->Common.Type)
105 case ACPI_TYPE_STRING:
107 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
108 Object, Object->String.Pointer));
110 /* Free the actual string buffer */
112 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
114 /* But only if it is NOT a pointer into an ACPI table */
116 ObjPointer = Object->String.Pointer;
120 case ACPI_TYPE_BUFFER:
122 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
123 Object, Object->Buffer.Pointer));
125 /* Free the actual buffer */
127 if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
129 /* But only if it is NOT a pointer into an ACPI table */
131 ObjPointer = Object->Buffer.Pointer;
135 case ACPI_TYPE_PACKAGE:
137 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
138 Object->Package.Count));
141 * Elements of the package are not handled here, they are deleted
145 /* Free the (variable length) element pointer array */
147 ObjPointer = Object->Package.Elements;
151 * These objects have a possible list of notify handlers.
152 * Device object also may have a GPE block.
154 case ACPI_TYPE_DEVICE:
156 if (Object->Device.GpeBlock)
158 (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
161 /*lint -fallthrough */
163 case ACPI_TYPE_PROCESSOR:
164 case ACPI_TYPE_THERMAL:
166 /* Walk the address handler list for this object */
168 HandlerDesc = Object->CommonNotify.Handler;
171 NextDesc = HandlerDesc->AddressSpace.Next;
172 AcpiUtRemoveReference (HandlerDesc);
173 HandlerDesc = NextDesc;
177 case ACPI_TYPE_MUTEX:
179 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
180 "***** Mutex %p, OS Mutex %p\n",
181 Object, Object->Mutex.OsMutex));
183 if (Object == AcpiGbl_GlobalLockMutex)
185 /* Global Lock has extra semaphore */
187 (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
188 AcpiGbl_GlobalLockSemaphore = NULL;
190 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
191 AcpiGbl_GlobalLockMutex = NULL;
195 AcpiExUnlinkMutex (Object);
196 AcpiOsDeleteMutex (Object->Mutex.OsMutex);
200 case ACPI_TYPE_EVENT:
202 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
203 "***** Event %p, OS Semaphore %p\n",
204 Object, Object->Event.OsSemaphore));
206 (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
207 Object->Event.OsSemaphore = NULL;
210 case ACPI_TYPE_METHOD:
212 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
213 "***** Method %p\n", Object));
215 /* Delete the method mutex if it exists */
217 if (Object->Method.Mutex)
219 AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
220 AcpiUtDeleteObjectDesc (Object->Method.Mutex);
221 Object->Method.Mutex = NULL;
223 if (Object->Method.Node)
225 Object->Method.Node = NULL;
229 case ACPI_TYPE_REGION:
231 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
232 "***** Region %p\n", Object));
235 * Update AddressRange list. However, only permanent regions
236 * are installed in this list. (Not created within a method)
238 if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
240 AcpiUtRemoveAddressRange (Object->Region.SpaceId,
241 Object->Region.Node);
244 SecondDesc = AcpiNsGetSecondaryObject (Object);
248 * Free the RegionContext if and only if the handler is one of the
249 * default handlers -- and therefore, we created the context object
250 * locally, it was not created by an external caller.
252 HandlerDesc = Object->Region.Handler;
255 NextDesc = HandlerDesc->AddressSpace.RegionList;
256 StartDesc = NextDesc;
257 LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
259 /* Remove the region object from the handler list */
263 if (NextDesc == Object)
265 *LastObjPtr = NextDesc->Region.Next;
269 /* Walk the linked list of handlers */
271 LastObjPtr = &NextDesc->Region.Next;
272 NextDesc = NextDesc->Region.Next;
274 /* Prevent infinite loop if list is corrupted */
276 if (NextDesc == StartDesc)
278 ACPI_ERROR ((AE_INFO,
279 "Circular region list in address handler object %p",
285 if (HandlerDesc->AddressSpace.HandlerFlags &
286 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
288 /* Deactivate region and free region context */
290 if (HandlerDesc->AddressSpace.Setup)
292 (void) HandlerDesc->AddressSpace.Setup (Object,
293 ACPI_REGION_DEACTIVATE,
294 HandlerDesc->AddressSpace.Context,
295 &SecondDesc->Extra.RegionContext);
299 AcpiUtRemoveReference (HandlerDesc);
302 /* Now we can free the Extra object */
304 AcpiUtDeleteObjectDesc (SecondDesc);
308 case ACPI_TYPE_BUFFER_FIELD:
310 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
311 "***** Buffer Field %p\n", Object));
313 SecondDesc = AcpiNsGetSecondaryObject (Object);
316 AcpiUtDeleteObjectDesc (SecondDesc);
320 case ACPI_TYPE_LOCAL_BANK_FIELD:
322 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
323 "***** Bank Field %p\n", Object));
325 SecondDesc = AcpiNsGetSecondaryObject (Object);
328 AcpiUtDeleteObjectDesc (SecondDesc);
337 /* Free any allocated memory (pointer within the object) found above */
341 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
343 ACPI_FREE (ObjPointer);
346 /* Now the object can be safely deleted */
348 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
349 Object, AcpiUtGetObjectTypeName (Object)));
351 AcpiUtDeleteObjectDesc (Object);
356 /*******************************************************************************
358 * FUNCTION: AcpiUtDeleteInternalObjectList
360 * PARAMETERS: ObjList - Pointer to the list to be deleted
364 * DESCRIPTION: This function deletes an internal object list, including both
365 * simple objects and package objects
367 ******************************************************************************/
370 AcpiUtDeleteInternalObjectList (
371 ACPI_OPERAND_OBJECT **ObjList)
373 ACPI_OPERAND_OBJECT **InternalObj;
376 ACPI_FUNCTION_ENTRY ();
379 /* Walk the null-terminated internal list */
381 for (InternalObj = ObjList; *InternalObj; InternalObj++)
383 AcpiUtRemoveReference (*InternalObj);
386 /* Free the combined parameter pointer list and object array */
393 /*******************************************************************************
395 * FUNCTION: AcpiUtUpdateRefCount
397 * PARAMETERS: Object - Object whose ref count is to be updated
398 * Action - What to do (REF_INCREMENT or REF_DECREMENT)
400 * RETURN: None. Sets new reference count within the object
402 * DESCRIPTION: Modify the reference count for an internal acpi object
404 ******************************************************************************/
407 AcpiUtUpdateRefCount (
408 ACPI_OPERAND_OBJECT *Object,
411 UINT16 OriginalCount;
413 ACPI_CPU_FLAGS LockFlags;
416 ACPI_FUNCTION_NAME (UtUpdateRefCount);
425 * Always get the reference count lock. Note: Interpreter and/or
426 * Namespace is not always locked when this function is called.
428 LockFlags = AcpiOsAcquireLock (AcpiGbl_ReferenceCountLock);
429 OriginalCount = Object->Common.ReferenceCount;
431 /* Perform the reference count action (increment, decrement) */
437 NewCount = OriginalCount + 1;
438 Object->Common.ReferenceCount = NewCount;
439 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
441 /* The current reference count should never be zero here */
445 ACPI_WARNING ((AE_INFO,
446 "Obj %p, Reference Count was zero before increment\n",
450 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
451 "Obj %p Type %.2X Refs %.2X [Incremented]\n",
452 Object, Object->Common.Type, NewCount));
457 /* The current reference count must be non-zero */
461 NewCount = OriginalCount - 1;
462 Object->Common.ReferenceCount = NewCount;
465 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
469 ACPI_WARNING ((AE_INFO,
470 "Obj %p, Reference Count is already zero, cannot decrement\n",
474 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
475 "Obj %p Type %.2X Refs %.2X [Decremented]\n",
476 Object, Object->Common.Type, NewCount));
478 /* Actually delete the object on a reference count of zero */
482 AcpiUtDeleteInternalObj (Object);
488 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
489 ACPI_ERROR ((AE_INFO, "Unknown Reference Count action (0x%X)",
495 * Sanity check the reference count, for debug purposes only.
496 * (A deleted object will have a huge reference count)
498 if (NewCount > ACPI_MAX_REFERENCE_COUNT)
500 ACPI_WARNING ((AE_INFO,
501 "Large Reference Count (0x%X) in object %p, Type=0x%.2X",
502 NewCount, Object, Object->Common.Type));
507 /*******************************************************************************
509 * FUNCTION: AcpiUtUpdateObjectReference
511 * PARAMETERS: Object - Increment ref count for this object
512 * and all sub-objects
513 * Action - Either REF_INCREMENT or REF_DECREMENT
517 * DESCRIPTION: Increment the object reference count
519 * Object references are incremented when:
520 * 1) An object is attached to a Node (namespace object)
521 * 2) An object is copied (all subobjects must be incremented)
523 * Object references are decremented when:
524 * 1) An object is detached from an Node
526 ******************************************************************************/
529 AcpiUtUpdateObjectReference (
530 ACPI_OPERAND_OBJECT *Object,
533 ACPI_STATUS Status = AE_OK;
534 ACPI_GENERIC_STATE *StateList = NULL;
535 ACPI_OPERAND_OBJECT *NextObject = NULL;
536 ACPI_OPERAND_OBJECT *PrevObject;
537 ACPI_GENERIC_STATE *State;
541 ACPI_FUNCTION_NAME (UtUpdateObjectReference);
546 /* Make sure that this isn't a namespace handle */
548 if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
550 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
551 "Object %p is NS handle\n", Object));
556 * All sub-objects must have their reference count incremented also.
557 * Different object types have different subobjects.
559 switch (Object->Common.Type)
561 case ACPI_TYPE_DEVICE:
562 case ACPI_TYPE_PROCESSOR:
563 case ACPI_TYPE_POWER:
564 case ACPI_TYPE_THERMAL:
566 * Update the notify objects for these types (if present)
567 * Two lists, system and device notify handlers.
569 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
571 PrevObject = Object->CommonNotify.NotifyList[i];
574 NextObject = PrevObject->Notify.Next[i];
575 AcpiUtUpdateRefCount (PrevObject, Action);
576 PrevObject = NextObject;
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 * Null package elements are legal and can be simply
592 NextObject = Object->Package.Elements[i];
598 switch (NextObject->Common.Type)
600 case ACPI_TYPE_INTEGER:
601 case ACPI_TYPE_STRING:
602 case ACPI_TYPE_BUFFER:
604 * For these very simple sub-objects, we can just
605 * update the reference count here and continue.
606 * Greatly increases performance of this operation.
608 AcpiUtUpdateRefCount (NextObject, Action);
613 * For complex sub-objects, push them onto the stack
614 * for later processing (this eliminates recursion.)
616 Status = AcpiUtCreateUpdateStateAndPush (
617 NextObject, Action, &StateList);
618 if (ACPI_FAILURE (Status))
628 case ACPI_TYPE_BUFFER_FIELD:
630 NextObject = Object->BufferField.BufferObj;
633 case ACPI_TYPE_LOCAL_REGION_FIELD:
635 NextObject = Object->Field.RegionObj;
638 case ACPI_TYPE_LOCAL_BANK_FIELD:
640 NextObject = Object->BankField.BankObj;
641 Status = AcpiUtCreateUpdateStateAndPush (
642 Object->BankField.RegionObj, Action, &StateList);
643 if (ACPI_FAILURE (Status))
649 case ACPI_TYPE_LOCAL_INDEX_FIELD:
651 NextObject = Object->IndexField.IndexObj;
652 Status = AcpiUtCreateUpdateStateAndPush (
653 Object->IndexField.DataObj, Action, &StateList);
654 if (ACPI_FAILURE (Status))
660 case ACPI_TYPE_LOCAL_REFERENCE:
662 * The target of an Index (a package, string, or buffer) or a named
663 * reference must track changes to the ref count of the index or
666 if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
667 (Object->Reference.Class== ACPI_REFCLASS_NAME))
669 NextObject = Object->Reference.Object;
673 case ACPI_TYPE_REGION:
676 break; /* No subobjects for all other types */
680 * Now we can update the count in the main object. This can only
681 * happen after we update the sub-objects in case this causes the
682 * main object to be deleted.
684 AcpiUtUpdateRefCount (Object, Action);
687 /* Move on to the next object to be updated */
696 State = AcpiUtPopGenericState (&StateList);
697 Object = State->Update.Object;
698 AcpiUtDeleteGenericState (State);
707 ACPI_EXCEPTION ((AE_INFO, Status,
708 "Could not update object reference count"));
710 /* Free any stacked Update State objects */
714 State = AcpiUtPopGenericState (&StateList);
715 AcpiUtDeleteGenericState (State);
722 /*******************************************************************************
724 * FUNCTION: AcpiUtAddReference
726 * PARAMETERS: Object - Object whose reference count is to be
731 * DESCRIPTION: Add one reference to an ACPI object
733 ******************************************************************************/
737 ACPI_OPERAND_OBJECT *Object)
740 ACPI_FUNCTION_NAME (UtAddReference);
743 /* Ensure that we have a valid object */
745 if (!AcpiUtValidInternalObject (Object))
750 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
751 "Obj %p Current Refs=%X [To Be Incremented]\n",
752 Object, Object->Common.ReferenceCount));
754 /* Increment the reference count */
756 (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
761 /*******************************************************************************
763 * FUNCTION: AcpiUtRemoveReference
765 * PARAMETERS: Object - Object whose ref count will be decremented
769 * DESCRIPTION: Decrement the reference count of an ACPI internal object
771 ******************************************************************************/
774 AcpiUtRemoveReference (
775 ACPI_OPERAND_OBJECT *Object)
778 ACPI_FUNCTION_NAME (UtRemoveReference);
782 * Allow a NULL pointer to be passed in, just ignore it. This saves
783 * each caller from having to check. Also, ignore NS nodes.
786 (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
792 /* Ensure that we have a valid object */
794 if (!AcpiUtValidInternalObject (Object))
799 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
800 "Obj %p Current Refs=%X [To Be Decremented]\n",
801 Object, Object->Common.ReferenceCount));
804 * Decrement the reference count, and only actually delete the object
805 * if the reference count becomes 0. (Must also decrement the ref count
806 * of all subobjects!)
808 (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);