1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2017, 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;
224 if (Object->Method.Node)
226 Object->Method.Node = NULL;
230 case ACPI_TYPE_REGION:
232 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
233 "***** Region %p\n", Object));
236 * Update AddressRange list. However, only permanent regions
237 * are installed in this list. (Not created within a method)
239 if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
241 AcpiUtRemoveAddressRange (Object->Region.SpaceId,
242 Object->Region.Node);
245 SecondDesc = AcpiNsGetSecondaryObject (Object);
249 * Free the RegionContext if and only if the handler is one of the
250 * default handlers -- and therefore, we created the context object
251 * locally, it was not created by an external caller.
253 HandlerDesc = Object->Region.Handler;
256 NextDesc = HandlerDesc->AddressSpace.RegionList;
257 StartDesc = NextDesc;
258 LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
260 /* Remove the region object from the handler list */
264 if (NextDesc == Object)
266 *LastObjPtr = NextDesc->Region.Next;
270 /* Walk the linked list of handlers */
272 LastObjPtr = &NextDesc->Region.Next;
273 NextDesc = NextDesc->Region.Next;
275 /* Prevent infinite loop if list is corrupted */
277 if (NextDesc == StartDesc)
279 ACPI_ERROR ((AE_INFO,
280 "Circular region list in address handler object %p",
286 if (HandlerDesc->AddressSpace.HandlerFlags &
287 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
289 /* Deactivate region and free region context */
291 if (HandlerDesc->AddressSpace.Setup)
293 (void) HandlerDesc->AddressSpace.Setup (Object,
294 ACPI_REGION_DEACTIVATE,
295 HandlerDesc->AddressSpace.Context,
296 &SecondDesc->Extra.RegionContext);
300 AcpiUtRemoveReference (HandlerDesc);
303 /* Now we can free the Extra object */
305 AcpiUtDeleteObjectDesc (SecondDesc);
309 case ACPI_TYPE_BUFFER_FIELD:
311 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
312 "***** Buffer Field %p\n", Object));
314 SecondDesc = AcpiNsGetSecondaryObject (Object);
317 AcpiUtDeleteObjectDesc (SecondDesc);
321 case ACPI_TYPE_LOCAL_BANK_FIELD:
323 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
324 "***** Bank Field %p\n", Object));
326 SecondDesc = AcpiNsGetSecondaryObject (Object);
329 AcpiUtDeleteObjectDesc (SecondDesc);
338 /* Free any allocated memory (pointer within the object) found above */
342 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
344 ACPI_FREE (ObjPointer);
347 /* Now the object can be safely deleted */
349 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
350 Object, AcpiUtGetObjectTypeName (Object)));
352 AcpiUtDeleteObjectDesc (Object);
357 /*******************************************************************************
359 * FUNCTION: AcpiUtDeleteInternalObjectList
361 * PARAMETERS: ObjList - Pointer to the list to be deleted
365 * DESCRIPTION: This function deletes an internal object list, including both
366 * simple objects and package objects
368 ******************************************************************************/
371 AcpiUtDeleteInternalObjectList (
372 ACPI_OPERAND_OBJECT **ObjList)
374 ACPI_OPERAND_OBJECT **InternalObj;
377 ACPI_FUNCTION_ENTRY ();
380 /* Walk the null-terminated internal list */
382 for (InternalObj = ObjList; *InternalObj; InternalObj++)
384 AcpiUtRemoveReference (*InternalObj);
387 /* Free the combined parameter pointer list and object array */
394 /*******************************************************************************
396 * FUNCTION: AcpiUtUpdateRefCount
398 * PARAMETERS: Object - Object whose ref count is to be updated
399 * Action - What to do (REF_INCREMENT or REF_DECREMENT)
401 * RETURN: None. Sets new reference count within the object
403 * DESCRIPTION: Modify the reference count for an internal acpi object
405 ******************************************************************************/
408 AcpiUtUpdateRefCount (
409 ACPI_OPERAND_OBJECT *Object,
412 UINT16 OriginalCount;
414 ACPI_CPU_FLAGS LockFlags;
417 ACPI_FUNCTION_NAME (UtUpdateRefCount);
426 * Always get the reference count lock. Note: Interpreter and/or
427 * Namespace is not always locked when this function is called.
429 LockFlags = AcpiOsAcquireLock (AcpiGbl_ReferenceCountLock);
430 OriginalCount = Object->Common.ReferenceCount;
432 /* Perform the reference count action (increment, decrement) */
438 NewCount = OriginalCount + 1;
439 Object->Common.ReferenceCount = NewCount;
440 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
442 /* The current reference count should never be zero here */
446 ACPI_WARNING ((AE_INFO,
447 "Obj %p, Reference Count was zero before increment\n",
451 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
452 "Obj %p Type %.2X [%s] Refs %.2X [Incremented]\n",
453 Object, Object->Common.Type,
454 AcpiUtGetObjectTypeName (Object), NewCount));
459 /* The current reference count must be non-zero */
463 NewCount = OriginalCount - 1;
464 Object->Common.ReferenceCount = NewCount;
467 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
471 ACPI_WARNING ((AE_INFO,
472 "Obj %p, Reference Count is already zero, cannot decrement\n",
476 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
477 "Obj %p Type %.2X Refs %.2X [Decremented]\n",
478 Object, Object->Common.Type, NewCount));
480 /* Actually delete the object on a reference count of zero */
484 AcpiUtDeleteInternalObj (Object);
490 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
491 ACPI_ERROR ((AE_INFO, "Unknown Reference Count action (0x%X)",
497 * Sanity check the reference count, for debug purposes only.
498 * (A deleted object will have a huge reference count)
500 if (NewCount > ACPI_MAX_REFERENCE_COUNT)
502 ACPI_WARNING ((AE_INFO,
503 "Large Reference Count (0x%X) in object %p, Type=0x%.2X",
504 NewCount, Object, Object->Common.Type));
509 /*******************************************************************************
511 * FUNCTION: AcpiUtUpdateObjectReference
513 * PARAMETERS: Object - Increment ref count for this object
514 * and all sub-objects
515 * Action - Either REF_INCREMENT or REF_DECREMENT
519 * DESCRIPTION: Increment the object reference count
521 * Object references are incremented when:
522 * 1) An object is attached to a Node (namespace object)
523 * 2) An object is copied (all subobjects must be incremented)
525 * Object references are decremented when:
526 * 1) An object is detached from an Node
528 ******************************************************************************/
531 AcpiUtUpdateObjectReference (
532 ACPI_OPERAND_OBJECT *Object,
535 ACPI_STATUS Status = AE_OK;
536 ACPI_GENERIC_STATE *StateList = NULL;
537 ACPI_OPERAND_OBJECT *NextObject = NULL;
538 ACPI_OPERAND_OBJECT *PrevObject;
539 ACPI_GENERIC_STATE *State;
543 ACPI_FUNCTION_NAME (UtUpdateObjectReference);
548 /* Make sure that this isn't a namespace handle */
550 if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
552 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
553 "Object %p is NS handle\n", Object));
558 * All sub-objects must have their reference count incremented
559 * also. Different object types have different subobjects.
561 switch (Object->Common.Type)
563 case ACPI_TYPE_DEVICE:
564 case ACPI_TYPE_PROCESSOR:
565 case ACPI_TYPE_POWER:
566 case ACPI_TYPE_THERMAL:
568 * Update the notify objects for these types (if present)
569 * Two lists, system and device notify handlers.
571 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
573 PrevObject = Object->CommonNotify.NotifyList[i];
576 NextObject = PrevObject->Notify.Next[i];
577 AcpiUtUpdateRefCount (PrevObject, Action);
578 PrevObject = NextObject;
583 case ACPI_TYPE_PACKAGE:
585 * We must update all the sub-objects of the package,
586 * each of whom may have their own sub-objects.
588 for (i = 0; i < Object->Package.Count; i++)
591 * Null package elements are legal and can be simply
594 NextObject = Object->Package.Elements[i];
600 switch (NextObject->Common.Type)
602 case ACPI_TYPE_INTEGER:
603 case ACPI_TYPE_STRING:
604 case ACPI_TYPE_BUFFER:
606 * For these very simple sub-objects, we can just
607 * update the reference count here and continue.
608 * Greatly increases performance of this operation.
610 AcpiUtUpdateRefCount (NextObject, Action);
615 * For complex sub-objects, push them onto the stack
616 * for later processing (this eliminates recursion.)
618 Status = AcpiUtCreateUpdateStateAndPush (
619 NextObject, Action, &StateList);
620 if (ACPI_FAILURE (Status))
630 case ACPI_TYPE_BUFFER_FIELD:
632 NextObject = Object->BufferField.BufferObj;
635 case ACPI_TYPE_LOCAL_REGION_FIELD:
637 NextObject = Object->Field.RegionObj;
640 case ACPI_TYPE_LOCAL_BANK_FIELD:
642 NextObject = Object->BankField.BankObj;
643 Status = AcpiUtCreateUpdateStateAndPush (
644 Object->BankField.RegionObj, Action, &StateList);
645 if (ACPI_FAILURE (Status))
651 case ACPI_TYPE_LOCAL_INDEX_FIELD:
653 NextObject = Object->IndexField.IndexObj;
654 Status = AcpiUtCreateUpdateStateAndPush (
655 Object->IndexField.DataObj, Action, &StateList);
656 if (ACPI_FAILURE (Status))
662 case ACPI_TYPE_LOCAL_REFERENCE:
664 * The target of an Index (a package, string, or buffer) or a named
665 * reference must track changes to the ref count of the index or
668 if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
669 (Object->Reference.Class== ACPI_REFCLASS_NAME))
671 NextObject = Object->Reference.Object;
675 case ACPI_TYPE_REGION:
678 break; /* No subobjects for all other types */
682 * Now we can update the count in the main object. This can only
683 * happen after we update the sub-objects in case this causes the
684 * main object to be deleted.
686 AcpiUtUpdateRefCount (Object, Action);
689 /* Move on to the next object to be updated */
698 State = AcpiUtPopGenericState (&StateList);
699 Object = State->Update.Object;
700 AcpiUtDeleteGenericState (State);
709 ACPI_EXCEPTION ((AE_INFO, Status,
710 "Could not update object reference count"));
712 /* Free any stacked Update State objects */
716 State = AcpiUtPopGenericState (&StateList);
717 AcpiUtDeleteGenericState (State);
724 /*******************************************************************************
726 * FUNCTION: AcpiUtAddReference
728 * PARAMETERS: Object - Object whose reference count is to be
733 * DESCRIPTION: Add one reference to an ACPI object
735 ******************************************************************************/
739 ACPI_OPERAND_OBJECT *Object)
742 ACPI_FUNCTION_NAME (UtAddReference);
745 /* Ensure that we have a valid object */
747 if (!AcpiUtValidInternalObject (Object))
752 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
753 "Obj %p Current Refs=%X [To Be Incremented]\n",
754 Object, Object->Common.ReferenceCount));
756 /* Increment the reference count */
758 (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
763 /*******************************************************************************
765 * FUNCTION: AcpiUtRemoveReference
767 * PARAMETERS: Object - Object whose ref count will be decremented
771 * DESCRIPTION: Decrement the reference count of an ACPI internal object
773 ******************************************************************************/
776 AcpiUtRemoveReference (
777 ACPI_OPERAND_OBJECT *Object)
780 ACPI_FUNCTION_NAME (UtRemoveReference);
784 * Allow a NULL pointer to be passed in, just ignore it. This saves
785 * each caller from having to check. Also, ignore NS nodes.
788 (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
794 /* Ensure that we have a valid object */
796 if (!AcpiUtValidInternalObject (Object))
801 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
802 "Obj %p Current Refs=%X [To Be Decremented]\n",
803 Object, Object->Common.ReferenceCount));
806 * Decrement the reference count, and only actually delete the object
807 * if the reference count becomes 0. (Must also decrement the ref count
808 * of all subobjects!)
810 (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);