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;
225 case ACPI_TYPE_REGION:
227 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
228 "***** Region %p\n", Object));
231 * Update AddressRange list. However, only permanent regions
232 * are installed in this list. (Not created within a method)
234 if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
236 AcpiUtRemoveAddressRange (Object->Region.SpaceId,
237 Object->Region.Node);
240 SecondDesc = AcpiNsGetSecondaryObject (Object);
244 * Free the RegionContext if and only if the handler is one of the
245 * default handlers -- and therefore, we created the context object
246 * locally, it was not created by an external caller.
248 HandlerDesc = Object->Region.Handler;
251 NextDesc = HandlerDesc->AddressSpace.RegionList;
252 StartDesc = NextDesc;
253 LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
255 /* Remove the region object from the handler list */
259 if (NextDesc == Object)
261 *LastObjPtr = NextDesc->Region.Next;
265 /* Walk the linked list of handlers */
267 LastObjPtr = &NextDesc->Region.Next;
268 NextDesc = NextDesc->Region.Next;
270 /* Prevent infinite loop if list is corrupted */
272 if (NextDesc == StartDesc)
274 ACPI_ERROR ((AE_INFO,
275 "Circular region list in address handler object %p",
281 if (HandlerDesc->AddressSpace.HandlerFlags &
282 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
284 /* Deactivate region and free region context */
286 if (HandlerDesc->AddressSpace.Setup)
288 (void) HandlerDesc->AddressSpace.Setup (Object,
289 ACPI_REGION_DEACTIVATE,
290 HandlerDesc->AddressSpace.Context,
291 &SecondDesc->Extra.RegionContext);
295 AcpiUtRemoveReference (HandlerDesc);
298 /* Now we can free the Extra object */
300 AcpiUtDeleteObjectDesc (SecondDesc);
304 case ACPI_TYPE_BUFFER_FIELD:
306 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
307 "***** Buffer Field %p\n", Object));
309 SecondDesc = AcpiNsGetSecondaryObject (Object);
312 AcpiUtDeleteObjectDesc (SecondDesc);
316 case ACPI_TYPE_LOCAL_BANK_FIELD:
318 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
319 "***** Bank Field %p\n", Object));
321 SecondDesc = AcpiNsGetSecondaryObject (Object);
324 AcpiUtDeleteObjectDesc (SecondDesc);
333 /* Free any allocated memory (pointer within the object) found above */
337 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
339 ACPI_FREE (ObjPointer);
342 /* Now the object can be safely deleted */
344 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
345 Object, AcpiUtGetObjectTypeName (Object)));
347 AcpiUtDeleteObjectDesc (Object);
352 /*******************************************************************************
354 * FUNCTION: AcpiUtDeleteInternalObjectList
356 * PARAMETERS: ObjList - Pointer to the list to be deleted
360 * DESCRIPTION: This function deletes an internal object list, including both
361 * simple objects and package objects
363 ******************************************************************************/
366 AcpiUtDeleteInternalObjectList (
367 ACPI_OPERAND_OBJECT **ObjList)
369 ACPI_OPERAND_OBJECT **InternalObj;
372 ACPI_FUNCTION_ENTRY ();
375 /* Walk the null-terminated internal list */
377 for (InternalObj = ObjList; *InternalObj; InternalObj++)
379 AcpiUtRemoveReference (*InternalObj);
382 /* Free the combined parameter pointer list and object array */
389 /*******************************************************************************
391 * FUNCTION: AcpiUtUpdateRefCount
393 * PARAMETERS: Object - Object whose ref count is to be updated
394 * Action - What to do (REF_INCREMENT or REF_DECREMENT)
396 * RETURN: None. Sets new reference count within the object
398 * DESCRIPTION: Modify the reference count for an internal acpi object
400 ******************************************************************************/
403 AcpiUtUpdateRefCount (
404 ACPI_OPERAND_OBJECT *Object,
407 UINT16 OriginalCount;
409 ACPI_CPU_FLAGS LockFlags;
412 ACPI_FUNCTION_NAME (UtUpdateRefCount);
421 * Always get the reference count lock. Note: Interpreter and/or
422 * Namespace is not always locked when this function is called.
424 LockFlags = AcpiOsAcquireLock (AcpiGbl_ReferenceCountLock);
425 OriginalCount = Object->Common.ReferenceCount;
427 /* Perform the reference count action (increment, decrement) */
433 NewCount = OriginalCount + 1;
434 Object->Common.ReferenceCount = NewCount;
435 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
437 /* The current reference count should never be zero here */
441 ACPI_WARNING ((AE_INFO,
442 "Obj %p, Reference Count was zero before increment\n",
446 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
447 "Obj %p Type %.2X Refs %.2X [Incremented]\n",
448 Object, Object->Common.Type, NewCount));
453 /* The current reference count must be non-zero */
457 NewCount = OriginalCount - 1;
458 Object->Common.ReferenceCount = NewCount;
461 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
465 ACPI_WARNING ((AE_INFO,
466 "Obj %p, Reference Count is already zero, cannot decrement\n",
470 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
471 "Obj %p Type %.2X Refs %.2X [Decremented]\n",
472 Object, Object->Common.Type, NewCount));
474 /* Actually delete the object on a reference count of zero */
478 AcpiUtDeleteInternalObj (Object);
484 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
485 ACPI_ERROR ((AE_INFO, "Unknown Reference Count action (0x%X)",
491 * Sanity check the reference count, for debug purposes only.
492 * (A deleted object will have a huge reference count)
494 if (NewCount > ACPI_MAX_REFERENCE_COUNT)
496 ACPI_WARNING ((AE_INFO,
497 "Large Reference Count (0x%X) in object %p, Type=0x%.2X",
498 NewCount, Object, Object->Common.Type));
503 /*******************************************************************************
505 * FUNCTION: AcpiUtUpdateObjectReference
507 * PARAMETERS: Object - Increment ref count for this object
508 * and all sub-objects
509 * Action - Either REF_INCREMENT or REF_DECREMENT
513 * DESCRIPTION: Increment the object reference count
515 * Object references are incremented when:
516 * 1) An object is attached to a Node (namespace object)
517 * 2) An object is copied (all subobjects must be incremented)
519 * Object references are decremented when:
520 * 1) An object is detached from an Node
522 ******************************************************************************/
525 AcpiUtUpdateObjectReference (
526 ACPI_OPERAND_OBJECT *Object,
529 ACPI_STATUS Status = AE_OK;
530 ACPI_GENERIC_STATE *StateList = NULL;
531 ACPI_OPERAND_OBJECT *NextObject = NULL;
532 ACPI_OPERAND_OBJECT *PrevObject;
533 ACPI_GENERIC_STATE *State;
537 ACPI_FUNCTION_NAME (UtUpdateObjectReference);
542 /* Make sure that this isn't a namespace handle */
544 if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
546 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
547 "Object %p is NS handle\n", Object));
552 * All sub-objects must have their reference count incremented also.
553 * Different object types have different subobjects.
555 switch (Object->Common.Type)
557 case ACPI_TYPE_DEVICE:
558 case ACPI_TYPE_PROCESSOR:
559 case ACPI_TYPE_POWER:
560 case ACPI_TYPE_THERMAL:
562 * Update the notify objects for these types (if present)
563 * Two lists, system and device notify handlers.
565 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
567 PrevObject = Object->CommonNotify.NotifyList[i];
570 NextObject = PrevObject->Notify.Next[i];
571 AcpiUtUpdateRefCount (PrevObject, Action);
572 PrevObject = NextObject;
577 case ACPI_TYPE_PACKAGE:
579 * We must update all the sub-objects of the package,
580 * each of whom may have their own sub-objects.
582 for (i = 0; i < Object->Package.Count; i++)
585 * Null package elements are legal and can be simply
588 NextObject = Object->Package.Elements[i];
594 switch (NextObject->Common.Type)
596 case ACPI_TYPE_INTEGER:
597 case ACPI_TYPE_STRING:
598 case ACPI_TYPE_BUFFER:
600 * For these very simple sub-objects, we can just
601 * update the reference count here and continue.
602 * Greatly increases performance of this operation.
604 AcpiUtUpdateRefCount (NextObject, Action);
609 * For complex sub-objects, push them onto the stack
610 * for later processing (this eliminates recursion.)
612 Status = AcpiUtCreateUpdateStateAndPush (
613 NextObject, Action, &StateList);
614 if (ACPI_FAILURE (Status))
624 case ACPI_TYPE_BUFFER_FIELD:
626 NextObject = Object->BufferField.BufferObj;
629 case ACPI_TYPE_LOCAL_REGION_FIELD:
631 NextObject = Object->Field.RegionObj;
634 case ACPI_TYPE_LOCAL_BANK_FIELD:
636 NextObject = Object->BankField.BankObj;
637 Status = AcpiUtCreateUpdateStateAndPush (
638 Object->BankField.RegionObj, Action, &StateList);
639 if (ACPI_FAILURE (Status))
645 case ACPI_TYPE_LOCAL_INDEX_FIELD:
647 NextObject = Object->IndexField.IndexObj;
648 Status = AcpiUtCreateUpdateStateAndPush (
649 Object->IndexField.DataObj, Action, &StateList);
650 if (ACPI_FAILURE (Status))
656 case ACPI_TYPE_LOCAL_REFERENCE:
658 * The target of an Index (a package, string, or buffer) or a named
659 * reference must track changes to the ref count of the index or
662 if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
663 (Object->Reference.Class== ACPI_REFCLASS_NAME))
665 NextObject = Object->Reference.Object;
669 case ACPI_TYPE_REGION:
672 break; /* No subobjects for all other types */
676 * Now we can update the count in the main object. This can only
677 * happen after we update the sub-objects in case this causes the
678 * main object to be deleted.
680 AcpiUtUpdateRefCount (Object, Action);
683 /* Move on to the next object to be updated */
692 State = AcpiUtPopGenericState (&StateList);
693 Object = State->Update.Object;
694 AcpiUtDeleteGenericState (State);
703 ACPI_EXCEPTION ((AE_INFO, Status,
704 "Could not update object reference count"));
706 /* Free any stacked Update State objects */
710 State = AcpiUtPopGenericState (&StateList);
711 AcpiUtDeleteGenericState (State);
718 /*******************************************************************************
720 * FUNCTION: AcpiUtAddReference
722 * PARAMETERS: Object - Object whose reference count is to be
727 * DESCRIPTION: Add one reference to an ACPI object
729 ******************************************************************************/
733 ACPI_OPERAND_OBJECT *Object)
736 ACPI_FUNCTION_NAME (UtAddReference);
739 /* Ensure that we have a valid object */
741 if (!AcpiUtValidInternalObject (Object))
746 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
747 "Obj %p Current Refs=%X [To Be Incremented]\n",
748 Object, Object->Common.ReferenceCount));
750 /* Increment the reference count */
752 (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
757 /*******************************************************************************
759 * FUNCTION: AcpiUtRemoveReference
761 * PARAMETERS: Object - Object whose ref count will be decremented
765 * DESCRIPTION: Decrement the reference count of an ACPI internal object
767 ******************************************************************************/
770 AcpiUtRemoveReference (
771 ACPI_OPERAND_OBJECT *Object)
774 ACPI_FUNCTION_NAME (UtRemoveReference);
778 * Allow a NULL pointer to be passed in, just ignore it. This saves
779 * each caller from having to check. Also, ignore NS nodes.
782 (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
788 /* Ensure that we have a valid object */
790 if (!AcpiUtValidInternalObject (Object))
795 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
796 "Obj %p Current Refs=%X [To Be Decremented]\n",
797 Object, Object->Common.ReferenceCount));
800 * Decrement the reference count, and only actually delete the object
801 * if the reference count becomes 0. (Must also decrement the ref count
802 * of all subobjects!)
804 (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);