1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2019, 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.
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
);
307 if (Object
->Field
.InternalPccBuffer
)
309 ACPI_FREE(Object
->Field
.InternalPccBuffer
);
314 case ACPI_TYPE_BUFFER_FIELD
:
316 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
317 "***** Buffer Field %p\n", Object
));
319 SecondDesc
= AcpiNsGetSecondaryObject (Object
);
322 AcpiUtDeleteObjectDesc (SecondDesc
);
326 case ACPI_TYPE_LOCAL_BANK_FIELD
:
328 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
329 "***** Bank Field %p\n", Object
));
331 SecondDesc
= AcpiNsGetSecondaryObject (Object
);
334 AcpiUtDeleteObjectDesc (SecondDesc
);
343 /* Free any allocated memory (pointer within the object) found above */
347 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Deleting Object Subptr %p\n",
349 ACPI_FREE (ObjPointer
);
352 /* Now the object can be safely deleted */
354 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS
, "%s: Deleting Object %p [%s]\n",
355 ACPI_GET_FUNCTION_NAME
, Object
, AcpiUtGetObjectTypeName (Object
)));
357 AcpiUtDeleteObjectDesc (Object
);
362 /*******************************************************************************
364 * FUNCTION: AcpiUtDeleteInternalObjectList
366 * PARAMETERS: ObjList - Pointer to the list to be deleted
370 * DESCRIPTION: This function deletes an internal object list, including both
371 * simple objects and package objects
373 ******************************************************************************/
376 AcpiUtDeleteInternalObjectList (
377 ACPI_OPERAND_OBJECT
**ObjList
)
379 ACPI_OPERAND_OBJECT
**InternalObj
;
382 ACPI_FUNCTION_ENTRY ();
385 /* Walk the null-terminated internal list */
387 for (InternalObj
= ObjList
; *InternalObj
; InternalObj
++)
389 AcpiUtRemoveReference (*InternalObj
);
392 /* Free the combined parameter pointer list and object array */
399 /*******************************************************************************
401 * FUNCTION: AcpiUtUpdateRefCount
403 * PARAMETERS: Object - Object whose ref count is to be updated
404 * Action - What to do (REF_INCREMENT or REF_DECREMENT)
406 * RETURN: None. Sets new reference count within the object
408 * DESCRIPTION: Modify the reference count for an internal acpi object
410 ******************************************************************************/
413 AcpiUtUpdateRefCount (
414 ACPI_OPERAND_OBJECT
*Object
,
417 UINT16 OriginalCount
;
419 ACPI_CPU_FLAGS LockFlags
;
423 ACPI_FUNCTION_NAME (UtUpdateRefCount
);
432 * Always get the reference count lock. Note: Interpreter and/or
433 * Namespace is not always locked when this function is called.
435 LockFlags
= AcpiOsAcquireLock (AcpiGbl_ReferenceCountLock
);
436 OriginalCount
= Object
->Common
.ReferenceCount
;
438 /* Perform the reference count action (increment, decrement) */
444 NewCount
= OriginalCount
+ 1;
445 Object
->Common
.ReferenceCount
= NewCount
;
446 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock
, LockFlags
);
448 /* The current reference count should never be zero here */
452 ACPI_WARNING ((AE_INFO
,
453 "Obj %p, Reference Count was zero before increment\n",
457 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
458 "Obj %p Type %.2X [%s] Refs %.2X [Incremented]\n",
459 Object
, Object
->Common
.Type
,
460 AcpiUtGetObjectTypeName (Object
), NewCount
));
461 Message
= "Incremement";
466 /* The current reference count must be non-zero */
470 NewCount
= OriginalCount
- 1;
471 Object
->Common
.ReferenceCount
= NewCount
;
474 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock
, LockFlags
);
478 ACPI_WARNING ((AE_INFO
,
479 "Obj %p, Reference Count is already zero, cannot decrement\n",
483 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS
,
484 "%s: Obj %p Type %.2X Refs %.2X [Decremented]\n",
485 ACPI_GET_FUNCTION_NAME
, Object
, Object
->Common
.Type
, NewCount
));
487 /* Actually delete the object on a reference count of zero */
491 AcpiUtDeleteInternalObj (Object
);
493 Message
= "Decrement";
498 AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock
, LockFlags
);
499 ACPI_ERROR ((AE_INFO
, "Unknown Reference Count action (0x%X)",
505 * Sanity check the reference count, for debug purposes only.
506 * (A deleted object will have a huge reference count)
508 if (NewCount
> ACPI_MAX_REFERENCE_COUNT
)
510 ACPI_WARNING ((AE_INFO
,
511 "Large Reference Count (0x%X) in object %p, Type=0x%.2X Operation=%s",
512 NewCount
, Object
, Object
->Common
.Type
, Message
));
517 /*******************************************************************************
519 * FUNCTION: AcpiUtUpdateObjectReference
521 * PARAMETERS: Object - Increment ref count for this object
522 * and all sub-objects
523 * Action - Either REF_INCREMENT or REF_DECREMENT
527 * DESCRIPTION: Increment the object reference count
529 * Object references are incremented when:
530 * 1) An object is attached to a Node (namespace object)
531 * 2) An object is copied (all subobjects must be incremented)
533 * Object references are decremented when:
534 * 1) An object is detached from an Node
536 ******************************************************************************/
539 AcpiUtUpdateObjectReference (
540 ACPI_OPERAND_OBJECT
*Object
,
543 ACPI_STATUS Status
= AE_OK
;
544 ACPI_GENERIC_STATE
*StateList
= NULL
;
545 ACPI_OPERAND_OBJECT
*NextObject
= NULL
;
546 ACPI_OPERAND_OBJECT
*PrevObject
;
547 ACPI_GENERIC_STATE
*State
;
551 ACPI_FUNCTION_NAME (UtUpdateObjectReference
);
556 /* Make sure that this isn't a namespace handle */
558 if (ACPI_GET_DESCRIPTOR_TYPE (Object
) == ACPI_DESC_TYPE_NAMED
)
560 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
561 "Object %p is NS handle\n", Object
));
566 * All sub-objects must have their reference count incremented
567 * also. Different object types have different subobjects.
569 switch (Object
->Common
.Type
)
571 case ACPI_TYPE_DEVICE
:
572 case ACPI_TYPE_PROCESSOR
:
573 case ACPI_TYPE_POWER
:
574 case ACPI_TYPE_THERMAL
:
576 * Update the notify objects for these types (if present)
577 * Two lists, system and device notify handlers.
579 for (i
= 0; i
< ACPI_NUM_NOTIFY_TYPES
; i
++)
581 PrevObject
= Object
->CommonNotify
.NotifyList
[i
];
584 NextObject
= PrevObject
->Notify
.Next
[i
];
585 AcpiUtUpdateRefCount (PrevObject
, Action
);
586 PrevObject
= NextObject
;
591 case ACPI_TYPE_PACKAGE
:
593 * We must update all the sub-objects of the package,
594 * each of whom may have their own sub-objects.
596 for (i
= 0; i
< Object
->Package
.Count
; i
++)
599 * Null package elements are legal and can be simply
602 NextObject
= Object
->Package
.Elements
[i
];
608 switch (NextObject
->Common
.Type
)
610 case ACPI_TYPE_INTEGER
:
611 case ACPI_TYPE_STRING
:
612 case ACPI_TYPE_BUFFER
:
614 * For these very simple sub-objects, we can just
615 * update the reference count here and continue.
616 * Greatly increases performance of this operation.
618 AcpiUtUpdateRefCount (NextObject
, Action
);
623 * For complex sub-objects, push them onto the stack
624 * for later processing (this eliminates recursion.)
626 Status
= AcpiUtCreateUpdateStateAndPush (
627 NextObject
, Action
, &StateList
);
628 if (ACPI_FAILURE (Status
))
638 case ACPI_TYPE_BUFFER_FIELD
:
640 NextObject
= Object
->BufferField
.BufferObj
;
643 case ACPI_TYPE_LOCAL_REGION_FIELD
:
645 NextObject
= Object
->Field
.RegionObj
;
648 case ACPI_TYPE_LOCAL_BANK_FIELD
:
650 NextObject
= Object
->BankField
.BankObj
;
651 Status
= AcpiUtCreateUpdateStateAndPush (
652 Object
->BankField
.RegionObj
, Action
, &StateList
);
653 if (ACPI_FAILURE (Status
))
659 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
661 NextObject
= Object
->IndexField
.IndexObj
;
662 Status
= AcpiUtCreateUpdateStateAndPush (
663 Object
->IndexField
.DataObj
, Action
, &StateList
);
664 if (ACPI_FAILURE (Status
))
670 case ACPI_TYPE_LOCAL_REFERENCE
:
672 * The target of an Index (a package, string, or buffer) or a named
673 * reference must track changes to the ref count of the index or
676 if ((Object
->Reference
.Class
== ACPI_REFCLASS_INDEX
) ||
677 (Object
->Reference
.Class
== ACPI_REFCLASS_NAME
))
679 NextObject
= Object
->Reference
.Object
;
683 case ACPI_TYPE_REGION
:
686 break; /* No subobjects for all other types */
690 * Now we can update the count in the main object. This can only
691 * happen after we update the sub-objects in case this causes the
692 * main object to be deleted.
694 AcpiUtUpdateRefCount (Object
, Action
);
697 /* Move on to the next object to be updated */
706 State
= AcpiUtPopGenericState (&StateList
);
707 Object
= State
->Update
.Object
;
708 AcpiUtDeleteGenericState (State
);
717 ACPI_EXCEPTION ((AE_INFO
, Status
,
718 "Could not update object reference count"));
720 /* Free any stacked Update State objects */
724 State
= AcpiUtPopGenericState (&StateList
);
725 AcpiUtDeleteGenericState (State
);
732 /*******************************************************************************
734 * FUNCTION: AcpiUtAddReference
736 * PARAMETERS: Object - Object whose reference count is to be
741 * DESCRIPTION: Add one reference to an ACPI object
743 ******************************************************************************/
747 ACPI_OPERAND_OBJECT
*Object
)
750 ACPI_FUNCTION_NAME (UtAddReference
);
753 /* Ensure that we have a valid object */
755 if (!AcpiUtValidInternalObject (Object
))
760 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
,
761 "Obj %p Current Refs=%X [To Be Incremented]\n",
762 Object
, Object
->Common
.ReferenceCount
));
764 /* Increment the reference count */
766 (void) AcpiUtUpdateObjectReference (Object
, REF_INCREMENT
);
771 /*******************************************************************************
773 * FUNCTION: AcpiUtRemoveReference
775 * PARAMETERS: Object - Object whose ref count will be decremented
779 * DESCRIPTION: Decrement the reference count of an ACPI internal object
781 ******************************************************************************/
784 AcpiUtRemoveReference (
785 ACPI_OPERAND_OBJECT
*Object
)
788 ACPI_FUNCTION_NAME (UtRemoveReference
);
792 * Allow a NULL pointer to be passed in, just ignore it. This saves
793 * each caller from having to check. Also, ignore NS nodes.
796 (ACPI_GET_DESCRIPTOR_TYPE (Object
) == ACPI_DESC_TYPE_NAMED
))
802 /* Ensure that we have a valid object */
804 if (!AcpiUtValidInternalObject (Object
))
809 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS
,
810 "%s: Obj %p Current Refs=%X [To Be Decremented]\n",
811 ACPI_GET_FUNCTION_NAME
, Object
, Object
->Common
.ReferenceCount
));
814 * Decrement the reference count, and only actually delete the object
815 * if the reference count becomes 0. (Must also decrement the ref count
816 * of all subobjects!)
818 (void) AcpiUtUpdateObjectReference (Object
, REF_DECREMENT
);