1c2c66affSColin Finck /*******************************************************************************
2c2c66affSColin Finck *
3c2c66affSColin Finck * Module Name: utdelete - object deletion and reference count utilities
4c2c66affSColin Finck *
5c2c66affSColin Finck ******************************************************************************/
6c2c66affSColin Finck
7c2c66affSColin Finck /*
8*03b24380SThomas Faber * Copyright (C) 2000 - 2022, Intel Corp.
9c2c66affSColin Finck * All rights reserved.
10c2c66affSColin Finck *
11c2c66affSColin Finck * Redistribution and use in source and binary forms, with or without
12c2c66affSColin Finck * modification, are permitted provided that the following conditions
13c2c66affSColin Finck * are met:
14c2c66affSColin Finck * 1. Redistributions of source code must retain the above copyright
15c2c66affSColin Finck * notice, this list of conditions, and the following disclaimer,
16c2c66affSColin Finck * without modification.
17c2c66affSColin Finck * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18c2c66affSColin Finck * substantially similar to the "NO WARRANTY" disclaimer below
19c2c66affSColin Finck * ("Disclaimer") and any redistribution must be conditioned upon
20c2c66affSColin Finck * including a substantially similar Disclaimer requirement for further
21c2c66affSColin Finck * binary redistribution.
22c2c66affSColin Finck * 3. Neither the names of the above-listed copyright holders nor the names
23c2c66affSColin Finck * of any contributors may be used to endorse or promote products derived
24c2c66affSColin Finck * from this software without specific prior written permission.
25c2c66affSColin Finck *
26c2c66affSColin Finck * Alternatively, this software may be distributed under the terms of the
27c2c66affSColin Finck * GNU General Public License ("GPL") version 2 as published by the Free
28c2c66affSColin Finck * Software Foundation.
29c2c66affSColin Finck *
30c2c66affSColin Finck * NO WARRANTY
31c2c66affSColin Finck * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32c2c66affSColin Finck * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
336eb8cc49SThomas Faber * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34c2c66affSColin Finck * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35c2c66affSColin Finck * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36c2c66affSColin Finck * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37c2c66affSColin Finck * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38c2c66affSColin Finck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39c2c66affSColin Finck * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40c2c66affSColin Finck * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41c2c66affSColin Finck * POSSIBILITY OF SUCH DAMAGES.
42c2c66affSColin Finck */
43c2c66affSColin Finck
44c2c66affSColin Finck #include "acpi.h"
45c2c66affSColin Finck #include "accommon.h"
46c2c66affSColin Finck #include "acinterp.h"
47c2c66affSColin Finck #include "acnamesp.h"
48c2c66affSColin Finck #include "acevents.h"
49c2c66affSColin Finck
50c2c66affSColin Finck
51c2c66affSColin Finck #define _COMPONENT ACPI_UTILITIES
52c2c66affSColin Finck ACPI_MODULE_NAME ("utdelete")
53c2c66affSColin Finck
54c2c66affSColin Finck /* Local prototypes */
55c2c66affSColin Finck
56c2c66affSColin Finck static void
57c2c66affSColin Finck AcpiUtDeleteInternalObj (
58c2c66affSColin Finck ACPI_OPERAND_OBJECT *Object);
59c2c66affSColin Finck
60c2c66affSColin Finck static void
61c2c66affSColin Finck AcpiUtUpdateRefCount (
62c2c66affSColin Finck ACPI_OPERAND_OBJECT *Object,
63c2c66affSColin Finck UINT32 Action);
64c2c66affSColin Finck
65c2c66affSColin Finck
66c2c66affSColin Finck /*******************************************************************************
67c2c66affSColin Finck *
68c2c66affSColin Finck * FUNCTION: AcpiUtDeleteInternalObj
69c2c66affSColin Finck *
70c2c66affSColin Finck * PARAMETERS: Object - Object to be deleted
71c2c66affSColin Finck *
72c2c66affSColin Finck * RETURN: None
73c2c66affSColin Finck *
74c2c66affSColin Finck * DESCRIPTION: Low level object deletion, after reference counts have been
75c2c66affSColin Finck * updated (All reference counts, including sub-objects!)
76c2c66affSColin Finck *
77c2c66affSColin Finck ******************************************************************************/
78c2c66affSColin Finck
79c2c66affSColin Finck static void
AcpiUtDeleteInternalObj(ACPI_OPERAND_OBJECT * Object)80c2c66affSColin Finck AcpiUtDeleteInternalObj (
81c2c66affSColin Finck ACPI_OPERAND_OBJECT *Object)
82c2c66affSColin Finck {
83c2c66affSColin Finck void *ObjPointer = NULL;
84c2c66affSColin Finck ACPI_OPERAND_OBJECT *HandlerDesc;
85c2c66affSColin Finck ACPI_OPERAND_OBJECT *SecondDesc;
86c2c66affSColin Finck ACPI_OPERAND_OBJECT *NextDesc;
87c2c66affSColin Finck ACPI_OPERAND_OBJECT *StartDesc;
88c2c66affSColin Finck ACPI_OPERAND_OBJECT **LastObjPtr;
89c2c66affSColin Finck
90c2c66affSColin Finck
91c2c66affSColin Finck ACPI_FUNCTION_TRACE_PTR (UtDeleteInternalObj, Object);
92c2c66affSColin Finck
93c2c66affSColin Finck
94c2c66affSColin Finck if (!Object)
95c2c66affSColin Finck {
96c2c66affSColin Finck return_VOID;
97c2c66affSColin Finck }
98c2c66affSColin Finck
99c2c66affSColin Finck /*
100c2c66affSColin Finck * Must delete or free any pointers within the object that are not
101c2c66affSColin Finck * actual ACPI objects (for example, a raw buffer pointer).
102c2c66affSColin Finck */
103c2c66affSColin Finck switch (Object->Common.Type)
104c2c66affSColin Finck {
105c2c66affSColin Finck case ACPI_TYPE_STRING:
106c2c66affSColin Finck
107c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
108c2c66affSColin Finck Object, Object->String.Pointer));
109c2c66affSColin Finck
110c2c66affSColin Finck /* Free the actual string buffer */
111c2c66affSColin Finck
112c2c66affSColin Finck if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
113c2c66affSColin Finck {
114c2c66affSColin Finck /* But only if it is NOT a pointer into an ACPI table */
115c2c66affSColin Finck
116c2c66affSColin Finck ObjPointer = Object->String.Pointer;
117c2c66affSColin Finck }
118c2c66affSColin Finck break;
119c2c66affSColin Finck
120c2c66affSColin Finck case ACPI_TYPE_BUFFER:
121c2c66affSColin Finck
122c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
123c2c66affSColin Finck Object, Object->Buffer.Pointer));
124c2c66affSColin Finck
125c2c66affSColin Finck /* Free the actual buffer */
126c2c66affSColin Finck
127c2c66affSColin Finck if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
128c2c66affSColin Finck {
129c2c66affSColin Finck /* But only if it is NOT a pointer into an ACPI table */
130c2c66affSColin Finck
131c2c66affSColin Finck ObjPointer = Object->Buffer.Pointer;
132c2c66affSColin Finck }
133c2c66affSColin Finck break;
134c2c66affSColin Finck
135c2c66affSColin Finck case ACPI_TYPE_PACKAGE:
136c2c66affSColin Finck
137c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
138c2c66affSColin Finck Object->Package.Count));
139c2c66affSColin Finck
140c2c66affSColin Finck /*
141c2c66affSColin Finck * Elements of the package are not handled here, they are deleted
142c2c66affSColin Finck * separately
143c2c66affSColin Finck */
144c2c66affSColin Finck
145c2c66affSColin Finck /* Free the (variable length) element pointer array */
146c2c66affSColin Finck
147c2c66affSColin Finck ObjPointer = Object->Package.Elements;
148c2c66affSColin Finck break;
149c2c66affSColin Finck
150c2c66affSColin Finck /*
151c2c66affSColin Finck * These objects have a possible list of notify handlers.
152c2c66affSColin Finck * Device object also may have a GPE block.
153c2c66affSColin Finck */
154c2c66affSColin Finck case ACPI_TYPE_DEVICE:
155c2c66affSColin Finck
156c2c66affSColin Finck if (Object->Device.GpeBlock)
157c2c66affSColin Finck {
158c2c66affSColin Finck (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
159c2c66affSColin Finck }
160c2c66affSColin Finck
1619587fe1cSThomas Faber ACPI_FALLTHROUGH;
162c2c66affSColin Finck
163c2c66affSColin Finck case ACPI_TYPE_PROCESSOR:
164c2c66affSColin Finck case ACPI_TYPE_THERMAL:
165c2c66affSColin Finck
166c2c66affSColin Finck /* Walk the address handler list for this object */
167c2c66affSColin Finck
168c2c66affSColin Finck HandlerDesc = Object->CommonNotify.Handler;
169c2c66affSColin Finck while (HandlerDesc)
170c2c66affSColin Finck {
171c2c66affSColin Finck NextDesc = HandlerDesc->AddressSpace.Next;
172c2c66affSColin Finck AcpiUtRemoveReference (HandlerDesc);
173c2c66affSColin Finck HandlerDesc = NextDesc;
174c2c66affSColin Finck }
175c2c66affSColin Finck break;
176c2c66affSColin Finck
177c2c66affSColin Finck case ACPI_TYPE_MUTEX:
178c2c66affSColin Finck
179c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
180c2c66affSColin Finck "***** Mutex %p, OS Mutex %p\n",
181c2c66affSColin Finck Object, Object->Mutex.OsMutex));
182c2c66affSColin Finck
183c2c66affSColin Finck if (Object == AcpiGbl_GlobalLockMutex)
184c2c66affSColin Finck {
185c2c66affSColin Finck /* Global Lock has extra semaphore */
186c2c66affSColin Finck
187c2c66affSColin Finck (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
188c2c66affSColin Finck AcpiGbl_GlobalLockSemaphore = NULL;
189c2c66affSColin Finck
190c2c66affSColin Finck AcpiOsDeleteMutex (Object->Mutex.OsMutex);
191c2c66affSColin Finck AcpiGbl_GlobalLockMutex = NULL;
192c2c66affSColin Finck }
193c2c66affSColin Finck else
194c2c66affSColin Finck {
195c2c66affSColin Finck AcpiExUnlinkMutex (Object);
196c2c66affSColin Finck AcpiOsDeleteMutex (Object->Mutex.OsMutex);
197c2c66affSColin Finck }
198c2c66affSColin Finck break;
199c2c66affSColin Finck
200c2c66affSColin Finck case ACPI_TYPE_EVENT:
201c2c66affSColin Finck
202c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
203c2c66affSColin Finck "***** Event %p, OS Semaphore %p\n",
204c2c66affSColin Finck Object, Object->Event.OsSemaphore));
205c2c66affSColin Finck
206c2c66affSColin Finck (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
207c2c66affSColin Finck Object->Event.OsSemaphore = NULL;
208c2c66affSColin Finck break;
209c2c66affSColin Finck
210c2c66affSColin Finck case ACPI_TYPE_METHOD:
211c2c66affSColin Finck
212c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
213c2c66affSColin Finck "***** Method %p\n", Object));
214c2c66affSColin Finck
215c2c66affSColin Finck /* Delete the method mutex if it exists */
216c2c66affSColin Finck
217c2c66affSColin Finck if (Object->Method.Mutex)
218c2c66affSColin Finck {
219c2c66affSColin Finck AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
220c2c66affSColin Finck AcpiUtDeleteObjectDesc (Object->Method.Mutex);
221c2c66affSColin Finck Object->Method.Mutex = NULL;
222c2c66affSColin Finck }
223c2c66affSColin Finck
224c2c66affSColin Finck if (Object->Method.Node)
225c2c66affSColin Finck {
226c2c66affSColin Finck Object->Method.Node = NULL;
227c2c66affSColin Finck }
228c2c66affSColin Finck break;
229c2c66affSColin Finck
230c2c66affSColin Finck case ACPI_TYPE_REGION:
231c2c66affSColin Finck
232c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
233c2c66affSColin Finck "***** Region %p\n", Object));
234c2c66affSColin Finck
235c2c66affSColin Finck /*
236c2c66affSColin Finck * Update AddressRange list. However, only permanent regions
237c2c66affSColin Finck * are installed in this list. (Not created within a method)
238c2c66affSColin Finck */
239c2c66affSColin Finck if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
240c2c66affSColin Finck {
241c2c66affSColin Finck AcpiUtRemoveAddressRange (Object->Region.SpaceId,
242c2c66affSColin Finck Object->Region.Node);
243c2c66affSColin Finck }
244c2c66affSColin Finck
245c2c66affSColin Finck SecondDesc = AcpiNsGetSecondaryObject (Object);
246c2c66affSColin Finck if (SecondDesc)
247c2c66affSColin Finck {
248c2c66affSColin Finck /*
249c2c66affSColin Finck * Free the RegionContext if and only if the handler is one of the
250c2c66affSColin Finck * default handlers -- and therefore, we created the context object
251c2c66affSColin Finck * locally, it was not created by an external caller.
252c2c66affSColin Finck */
253c2c66affSColin Finck HandlerDesc = Object->Region.Handler;
254c2c66affSColin Finck if (HandlerDesc)
255c2c66affSColin Finck {
256c2c66affSColin Finck NextDesc = HandlerDesc->AddressSpace.RegionList;
257c2c66affSColin Finck StartDesc = NextDesc;
258c2c66affSColin Finck LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
259c2c66affSColin Finck
260c2c66affSColin Finck /* Remove the region object from the handler list */
261c2c66affSColin Finck
262c2c66affSColin Finck while (NextDesc)
263c2c66affSColin Finck {
264c2c66affSColin Finck if (NextDesc == Object)
265c2c66affSColin Finck {
266c2c66affSColin Finck *LastObjPtr = NextDesc->Region.Next;
267c2c66affSColin Finck break;
268c2c66affSColin Finck }
269c2c66affSColin Finck
270c2c66affSColin Finck /* Walk the linked list of handlers */
271c2c66affSColin Finck
272c2c66affSColin Finck LastObjPtr = &NextDesc->Region.Next;
273c2c66affSColin Finck NextDesc = NextDesc->Region.Next;
274c2c66affSColin Finck
275c2c66affSColin Finck /* Prevent infinite loop if list is corrupted */
276c2c66affSColin Finck
277c2c66affSColin Finck if (NextDesc == StartDesc)
278c2c66affSColin Finck {
279c2c66affSColin Finck ACPI_ERROR ((AE_INFO,
280c2c66affSColin Finck "Circular region list in address handler object %p",
281c2c66affSColin Finck HandlerDesc));
282c2c66affSColin Finck return_VOID;
283c2c66affSColin Finck }
284c2c66affSColin Finck }
285c2c66affSColin Finck
286c2c66affSColin Finck if (HandlerDesc->AddressSpace.HandlerFlags &
287c2c66affSColin Finck ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
288c2c66affSColin Finck {
289c2c66affSColin Finck /* Deactivate region and free region context */
290c2c66affSColin Finck
291c2c66affSColin Finck if (HandlerDesc->AddressSpace.Setup)
292c2c66affSColin Finck {
293c2c66affSColin Finck (void) HandlerDesc->AddressSpace.Setup (Object,
294c2c66affSColin Finck ACPI_REGION_DEACTIVATE,
295c2c66affSColin Finck HandlerDesc->AddressSpace.Context,
296c2c66affSColin Finck &SecondDesc->Extra.RegionContext);
297c2c66affSColin Finck }
298c2c66affSColin Finck }
299c2c66affSColin Finck
300c2c66affSColin Finck AcpiUtRemoveReference (HandlerDesc);
301c2c66affSColin Finck }
302c2c66affSColin Finck
303c2c66affSColin Finck /* Now we can free the Extra object */
304c2c66affSColin Finck
305c2c66affSColin Finck AcpiUtDeleteObjectDesc (SecondDesc);
306c2c66affSColin Finck }
3079d53c935SThomas Faber if (Object->Field.InternalPccBuffer)
3089d53c935SThomas Faber {
3099d53c935SThomas Faber ACPI_FREE(Object->Field.InternalPccBuffer);
3109d53c935SThomas Faber }
3119d53c935SThomas Faber
312c2c66affSColin Finck break;
313c2c66affSColin Finck
314c2c66affSColin Finck case ACPI_TYPE_BUFFER_FIELD:
315c2c66affSColin Finck
316c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
317c2c66affSColin Finck "***** Buffer Field %p\n", Object));
318c2c66affSColin Finck
319c2c66affSColin Finck SecondDesc = AcpiNsGetSecondaryObject (Object);
320c2c66affSColin Finck if (SecondDesc)
321c2c66affSColin Finck {
322c2c66affSColin Finck AcpiUtDeleteObjectDesc (SecondDesc);
323c2c66affSColin Finck }
324c2c66affSColin Finck break;
325c2c66affSColin Finck
326c2c66affSColin Finck case ACPI_TYPE_LOCAL_BANK_FIELD:
327c2c66affSColin Finck
328c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
329c2c66affSColin Finck "***** Bank Field %p\n", Object));
330c2c66affSColin Finck
331c2c66affSColin Finck SecondDesc = AcpiNsGetSecondaryObject (Object);
332c2c66affSColin Finck if (SecondDesc)
333c2c66affSColin Finck {
334c2c66affSColin Finck AcpiUtDeleteObjectDesc (SecondDesc);
335c2c66affSColin Finck }
336c2c66affSColin Finck break;
337c2c66affSColin Finck
3383c284560SThomas Faber case ACPI_TYPE_LOCAL_ADDRESS_HANDLER:
3393c284560SThomas Faber
3403c284560SThomas Faber ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
3413c284560SThomas Faber "***** Address handler %p\n", Object));
3423c284560SThomas Faber
3433c284560SThomas Faber AcpiOsDeleteMutex (Object->AddressSpace.ContextMutex);
3443c284560SThomas Faber break;
3453c284560SThomas Faber
346c2c66affSColin Finck default:
347c2c66affSColin Finck
348c2c66affSColin Finck break;
349c2c66affSColin Finck }
350c2c66affSColin Finck
351c2c66affSColin Finck /* Free any allocated memory (pointer within the object) found above */
352c2c66affSColin Finck
353c2c66affSColin Finck if (ObjPointer)
354c2c66affSColin Finck {
355c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
356c2c66affSColin Finck ObjPointer));
357c2c66affSColin Finck ACPI_FREE (ObjPointer);
358c2c66affSColin Finck }
359c2c66affSColin Finck
360c2c66affSColin Finck /* Now the object can be safely deleted */
361c2c66affSColin Finck
3626847cc3aSThomas Faber ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS, "%s: Deleting Object %p [%s]\n",
3636847cc3aSThomas Faber ACPI_GET_FUNCTION_NAME, Object, AcpiUtGetObjectTypeName (Object)));
364c2c66affSColin Finck
365c2c66affSColin Finck AcpiUtDeleteObjectDesc (Object);
366c2c66affSColin Finck return_VOID;
367c2c66affSColin Finck }
368c2c66affSColin Finck
369c2c66affSColin Finck
370c2c66affSColin Finck /*******************************************************************************
371c2c66affSColin Finck *
372c2c66affSColin Finck * FUNCTION: AcpiUtDeleteInternalObjectList
373c2c66affSColin Finck *
374c2c66affSColin Finck * PARAMETERS: ObjList - Pointer to the list to be deleted
375c2c66affSColin Finck *
376c2c66affSColin Finck * RETURN: None
377c2c66affSColin Finck *
378c2c66affSColin Finck * DESCRIPTION: This function deletes an internal object list, including both
379c2c66affSColin Finck * simple objects and package objects
380c2c66affSColin Finck *
381c2c66affSColin Finck ******************************************************************************/
382c2c66affSColin Finck
383c2c66affSColin Finck void
AcpiUtDeleteInternalObjectList(ACPI_OPERAND_OBJECT ** ObjList)384c2c66affSColin Finck AcpiUtDeleteInternalObjectList (
385c2c66affSColin Finck ACPI_OPERAND_OBJECT **ObjList)
386c2c66affSColin Finck {
387c2c66affSColin Finck ACPI_OPERAND_OBJECT **InternalObj;
388c2c66affSColin Finck
389c2c66affSColin Finck
390c2c66affSColin Finck ACPI_FUNCTION_ENTRY ();
391c2c66affSColin Finck
392c2c66affSColin Finck
393c2c66affSColin Finck /* Walk the null-terminated internal list */
394c2c66affSColin Finck
395c2c66affSColin Finck for (InternalObj = ObjList; *InternalObj; InternalObj++)
396c2c66affSColin Finck {
397c2c66affSColin Finck AcpiUtRemoveReference (*InternalObj);
398c2c66affSColin Finck }
399c2c66affSColin Finck
400c2c66affSColin Finck /* Free the combined parameter pointer list and object array */
401c2c66affSColin Finck
402c2c66affSColin Finck ACPI_FREE (ObjList);
403c2c66affSColin Finck return;
404c2c66affSColin Finck }
405c2c66affSColin Finck
406c2c66affSColin Finck
407c2c66affSColin Finck /*******************************************************************************
408c2c66affSColin Finck *
409c2c66affSColin Finck * FUNCTION: AcpiUtUpdateRefCount
410c2c66affSColin Finck *
411c2c66affSColin Finck * PARAMETERS: Object - Object whose ref count is to be updated
412c2c66affSColin Finck * Action - What to do (REF_INCREMENT or REF_DECREMENT)
413c2c66affSColin Finck *
414c2c66affSColin Finck * RETURN: None. Sets new reference count within the object
415c2c66affSColin Finck *
416c2c66affSColin Finck * DESCRIPTION: Modify the reference count for an internal acpi object
417c2c66affSColin Finck *
418c2c66affSColin Finck ******************************************************************************/
419c2c66affSColin Finck
420c2c66affSColin Finck static void
AcpiUtUpdateRefCount(ACPI_OPERAND_OBJECT * Object,UINT32 Action)421c2c66affSColin Finck AcpiUtUpdateRefCount (
422c2c66affSColin Finck ACPI_OPERAND_OBJECT *Object,
423c2c66affSColin Finck UINT32 Action)
424c2c66affSColin Finck {
425c2c66affSColin Finck UINT16 OriginalCount;
426c2c66affSColin Finck UINT16 NewCount = 0;
427c2c66affSColin Finck ACPI_CPU_FLAGS LockFlags;
428b062d28bSThomas Faber char *Message;
429c2c66affSColin Finck
430c2c66affSColin Finck
431c2c66affSColin Finck ACPI_FUNCTION_NAME (UtUpdateRefCount);
432c2c66affSColin Finck
433c2c66affSColin Finck
434c2c66affSColin Finck if (!Object)
435c2c66affSColin Finck {
436c2c66affSColin Finck return;
437c2c66affSColin Finck }
438c2c66affSColin Finck
439c2c66affSColin Finck /*
440c2c66affSColin Finck * Always get the reference count lock. Note: Interpreter and/or
441c2c66affSColin Finck * Namespace is not always locked when this function is called.
442c2c66affSColin Finck */
443c2c66affSColin Finck LockFlags = AcpiOsAcquireLock (AcpiGbl_ReferenceCountLock);
444c2c66affSColin Finck OriginalCount = Object->Common.ReferenceCount;
445c2c66affSColin Finck
446c2c66affSColin Finck /* Perform the reference count action (increment, decrement) */
447c2c66affSColin Finck
448c2c66affSColin Finck switch (Action)
449c2c66affSColin Finck {
450c2c66affSColin Finck case REF_INCREMENT:
451c2c66affSColin Finck
452c2c66affSColin Finck NewCount = OriginalCount + 1;
453c2c66affSColin Finck Object->Common.ReferenceCount = NewCount;
454c2c66affSColin Finck AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
455c2c66affSColin Finck
456c2c66affSColin Finck /* The current reference count should never be zero here */
457c2c66affSColin Finck
458c2c66affSColin Finck if (!OriginalCount)
459c2c66affSColin Finck {
460c2c66affSColin Finck ACPI_WARNING ((AE_INFO,
461c2c66affSColin Finck "Obj %p, Reference Count was zero before increment\n",
462c2c66affSColin Finck Object));
463c2c66affSColin Finck }
464c2c66affSColin Finck
465c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
466c2c66affSColin Finck "Obj %p Type %.2X [%s] Refs %.2X [Incremented]\n",
467c2c66affSColin Finck Object, Object->Common.Type,
468c2c66affSColin Finck AcpiUtGetObjectTypeName (Object), NewCount));
469b062d28bSThomas Faber Message = "Incremement";
470c2c66affSColin Finck break;
471c2c66affSColin Finck
472c2c66affSColin Finck case REF_DECREMENT:
473c2c66affSColin Finck
474c2c66affSColin Finck /* The current reference count must be non-zero */
475c2c66affSColin Finck
476c2c66affSColin Finck if (OriginalCount)
477c2c66affSColin Finck {
478c2c66affSColin Finck NewCount = OriginalCount - 1;
479c2c66affSColin Finck Object->Common.ReferenceCount = NewCount;
480c2c66affSColin Finck }
481c2c66affSColin Finck
482c2c66affSColin Finck AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
483c2c66affSColin Finck
484c2c66affSColin Finck if (!OriginalCount)
485c2c66affSColin Finck {
486c2c66affSColin Finck ACPI_WARNING ((AE_INFO,
487c2c66affSColin Finck "Obj %p, Reference Count is already zero, cannot decrement\n",
488c2c66affSColin Finck Object));
4898129fee1SThomas Faber return;
490c2c66affSColin Finck }
491c2c66affSColin Finck
4926847cc3aSThomas Faber ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS,
4936847cc3aSThomas Faber "%s: Obj %p Type %.2X Refs %.2X [Decremented]\n",
4946847cc3aSThomas Faber ACPI_GET_FUNCTION_NAME, Object, Object->Common.Type, NewCount));
495c2c66affSColin Finck
496c2c66affSColin Finck /* Actually delete the object on a reference count of zero */
497c2c66affSColin Finck
498c2c66affSColin Finck if (NewCount == 0)
499c2c66affSColin Finck {
500c2c66affSColin Finck AcpiUtDeleteInternalObj (Object);
501c2c66affSColin Finck }
502b062d28bSThomas Faber Message = "Decrement";
503c2c66affSColin Finck break;
504c2c66affSColin Finck
505c2c66affSColin Finck default:
506c2c66affSColin Finck
507c2c66affSColin Finck AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
508c2c66affSColin Finck ACPI_ERROR ((AE_INFO, "Unknown Reference Count action (0x%X)",
509c2c66affSColin Finck Action));
510c2c66affSColin Finck return;
511c2c66affSColin Finck }
512c2c66affSColin Finck
513c2c66affSColin Finck /*
514c2c66affSColin Finck * Sanity check the reference count, for debug purposes only.
515c2c66affSColin Finck * (A deleted object will have a huge reference count)
516c2c66affSColin Finck */
517c2c66affSColin Finck if (NewCount > ACPI_MAX_REFERENCE_COUNT)
518c2c66affSColin Finck {
519c2c66affSColin Finck ACPI_WARNING ((AE_INFO,
520b062d28bSThomas Faber "Large Reference Count (0x%X) in object %p, Type=0x%.2X Operation=%s",
521b062d28bSThomas Faber NewCount, Object, Object->Common.Type, Message));
522c2c66affSColin Finck }
523c2c66affSColin Finck }
524c2c66affSColin Finck
525c2c66affSColin Finck
526c2c66affSColin Finck /*******************************************************************************
527c2c66affSColin Finck *
528c2c66affSColin Finck * FUNCTION: AcpiUtUpdateObjectReference
529c2c66affSColin Finck *
530040546d8SThomas Faber * PARAMETERS: Object - Increment or decrement the ref count for
531040546d8SThomas Faber * this object and all sub-objects
532c2c66affSColin Finck * Action - Either REF_INCREMENT or REF_DECREMENT
533c2c66affSColin Finck *
534c2c66affSColin Finck * RETURN: Status
535c2c66affSColin Finck *
536040546d8SThomas Faber * DESCRIPTION: Increment or decrement the object reference count
537c2c66affSColin Finck *
538c2c66affSColin Finck * Object references are incremented when:
539c2c66affSColin Finck * 1) An object is attached to a Node (namespace object)
540c2c66affSColin Finck * 2) An object is copied (all subobjects must be incremented)
541c2c66affSColin Finck *
542c2c66affSColin Finck * Object references are decremented when:
543c2c66affSColin Finck * 1) An object is detached from an Node
544c2c66affSColin Finck *
545c2c66affSColin Finck ******************************************************************************/
546c2c66affSColin Finck
547c2c66affSColin Finck ACPI_STATUS
AcpiUtUpdateObjectReference(ACPI_OPERAND_OBJECT * Object,UINT16 Action)548c2c66affSColin Finck AcpiUtUpdateObjectReference (
549c2c66affSColin Finck ACPI_OPERAND_OBJECT *Object,
550c2c66affSColin Finck UINT16 Action)
551c2c66affSColin Finck {
552c2c66affSColin Finck ACPI_STATUS Status = AE_OK;
553c2c66affSColin Finck ACPI_GENERIC_STATE *StateList = NULL;
554c2c66affSColin Finck ACPI_OPERAND_OBJECT *NextObject = NULL;
555c2c66affSColin Finck ACPI_OPERAND_OBJECT *PrevObject;
556c2c66affSColin Finck ACPI_GENERIC_STATE *State;
557c2c66affSColin Finck UINT32 i;
558c2c66affSColin Finck
559c2c66affSColin Finck
560c2c66affSColin Finck ACPI_FUNCTION_NAME (UtUpdateObjectReference);
561c2c66affSColin Finck
562c2c66affSColin Finck
563c2c66affSColin Finck while (Object)
564c2c66affSColin Finck {
565c2c66affSColin Finck /* Make sure that this isn't a namespace handle */
566c2c66affSColin Finck
567c2c66affSColin Finck if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
568c2c66affSColin Finck {
569c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
570c2c66affSColin Finck "Object %p is NS handle\n", Object));
571c2c66affSColin Finck return (AE_OK);
572c2c66affSColin Finck }
573c2c66affSColin Finck
574c2c66affSColin Finck /*
575040546d8SThomas Faber * All sub-objects must have their reference count updated
576c2c66affSColin Finck * also. Different object types have different subobjects.
577c2c66affSColin Finck */
578c2c66affSColin Finck switch (Object->Common.Type)
579c2c66affSColin Finck {
580c2c66affSColin Finck case ACPI_TYPE_DEVICE:
581c2c66affSColin Finck case ACPI_TYPE_PROCESSOR:
582c2c66affSColin Finck case ACPI_TYPE_POWER:
583c2c66affSColin Finck case ACPI_TYPE_THERMAL:
584c2c66affSColin Finck /*
585c2c66affSColin Finck * Update the notify objects for these types (if present)
586c2c66affSColin Finck * Two lists, system and device notify handlers.
587c2c66affSColin Finck */
588c2c66affSColin Finck for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
589c2c66affSColin Finck {
590c2c66affSColin Finck PrevObject = Object->CommonNotify.NotifyList[i];
591c2c66affSColin Finck while (PrevObject)
592c2c66affSColin Finck {
593c2c66affSColin Finck NextObject = PrevObject->Notify.Next[i];
594c2c66affSColin Finck AcpiUtUpdateRefCount (PrevObject, Action);
595c2c66affSColin Finck PrevObject = NextObject;
596c2c66affSColin Finck }
597c2c66affSColin Finck }
598c2c66affSColin Finck break;
599c2c66affSColin Finck
600c2c66affSColin Finck case ACPI_TYPE_PACKAGE:
601c2c66affSColin Finck /*
602c2c66affSColin Finck * We must update all the sub-objects of the package,
603c2c66affSColin Finck * each of whom may have their own sub-objects.
604c2c66affSColin Finck */
605c2c66affSColin Finck for (i = 0; i < Object->Package.Count; i++)
606c2c66affSColin Finck {
607c2c66affSColin Finck /*
608c2c66affSColin Finck * Null package elements are legal and can be simply
609c2c66affSColin Finck * ignored.
610c2c66affSColin Finck */
611c2c66affSColin Finck NextObject = Object->Package.Elements[i];
612c2c66affSColin Finck if (!NextObject)
613c2c66affSColin Finck {
614c2c66affSColin Finck continue;
615c2c66affSColin Finck }
616c2c66affSColin Finck
617c2c66affSColin Finck switch (NextObject->Common.Type)
618c2c66affSColin Finck {
619c2c66affSColin Finck case ACPI_TYPE_INTEGER:
620c2c66affSColin Finck case ACPI_TYPE_STRING:
621c2c66affSColin Finck case ACPI_TYPE_BUFFER:
622c2c66affSColin Finck /*
623c2c66affSColin Finck * For these very simple sub-objects, we can just
624c2c66affSColin Finck * update the reference count here and continue.
625c2c66affSColin Finck * Greatly increases performance of this operation.
626c2c66affSColin Finck */
627c2c66affSColin Finck AcpiUtUpdateRefCount (NextObject, Action);
628c2c66affSColin Finck break;
629c2c66affSColin Finck
630c2c66affSColin Finck default:
631c2c66affSColin Finck /*
632c2c66affSColin Finck * For complex sub-objects, push them onto the stack
633c2c66affSColin Finck * for later processing (this eliminates recursion.)
634c2c66affSColin Finck */
635c2c66affSColin Finck Status = AcpiUtCreateUpdateStateAndPush (
636c2c66affSColin Finck NextObject, Action, &StateList);
637c2c66affSColin Finck if (ACPI_FAILURE (Status))
638c2c66affSColin Finck {
639c2c66affSColin Finck goto ErrorExit;
640c2c66affSColin Finck }
641c2c66affSColin Finck break;
642c2c66affSColin Finck }
643c2c66affSColin Finck }
644040546d8SThomas Faber
645c2c66affSColin Finck NextObject = NULL;
646c2c66affSColin Finck break;
647c2c66affSColin Finck
648c2c66affSColin Finck case ACPI_TYPE_BUFFER_FIELD:
649c2c66affSColin Finck
650c2c66affSColin Finck NextObject = Object->BufferField.BufferObj;
651c2c66affSColin Finck break;
652c2c66affSColin Finck
653c2c66affSColin Finck case ACPI_TYPE_LOCAL_BANK_FIELD:
654c2c66affSColin Finck
655c2c66affSColin Finck NextObject = Object->BankField.BankObj;
656c2c66affSColin Finck Status = AcpiUtCreateUpdateStateAndPush (
657c2c66affSColin Finck Object->BankField.RegionObj, Action, &StateList);
658c2c66affSColin Finck if (ACPI_FAILURE (Status))
659c2c66affSColin Finck {
660c2c66affSColin Finck goto ErrorExit;
661c2c66affSColin Finck }
662c2c66affSColin Finck break;
663c2c66affSColin Finck
664c2c66affSColin Finck case ACPI_TYPE_LOCAL_INDEX_FIELD:
665c2c66affSColin Finck
666c2c66affSColin Finck NextObject = Object->IndexField.IndexObj;
667c2c66affSColin Finck Status = AcpiUtCreateUpdateStateAndPush (
668c2c66affSColin Finck Object->IndexField.DataObj, Action, &StateList);
669c2c66affSColin Finck if (ACPI_FAILURE (Status))
670c2c66affSColin Finck {
671c2c66affSColin Finck goto ErrorExit;
672c2c66affSColin Finck }
673c2c66affSColin Finck break;
674c2c66affSColin Finck
675c2c66affSColin Finck case ACPI_TYPE_LOCAL_REFERENCE:
676c2c66affSColin Finck /*
677c2c66affSColin Finck * The target of an Index (a package, string, or buffer) or a named
678c2c66affSColin Finck * reference must track changes to the ref count of the index or
679c2c66affSColin Finck * target object.
680c2c66affSColin Finck */
681c2c66affSColin Finck if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
682c2c66affSColin Finck (Object->Reference.Class== ACPI_REFCLASS_NAME))
683c2c66affSColin Finck {
684c2c66affSColin Finck NextObject = Object->Reference.Object;
685c2c66affSColin Finck }
686c2c66affSColin Finck break;
687c2c66affSColin Finck
6888605f660SThomas Faber case ACPI_TYPE_LOCAL_REGION_FIELD:
689c2c66affSColin Finck case ACPI_TYPE_REGION:
690c2c66affSColin Finck default:
691c2c66affSColin Finck
692c2c66affSColin Finck break; /* No subobjects for all other types */
693c2c66affSColin Finck }
694c2c66affSColin Finck
695c2c66affSColin Finck /*
696c2c66affSColin Finck * Now we can update the count in the main object. This can only
697c2c66affSColin Finck * happen after we update the sub-objects in case this causes the
698c2c66affSColin Finck * main object to be deleted.
699c2c66affSColin Finck */
700c2c66affSColin Finck AcpiUtUpdateRefCount (Object, Action);
701c2c66affSColin Finck Object = NULL;
702c2c66affSColin Finck
703c2c66affSColin Finck /* Move on to the next object to be updated */
704c2c66affSColin Finck
705c2c66affSColin Finck if (NextObject)
706c2c66affSColin Finck {
707c2c66affSColin Finck Object = NextObject;
708c2c66affSColin Finck NextObject = NULL;
709c2c66affSColin Finck }
710c2c66affSColin Finck else if (StateList)
711c2c66affSColin Finck {
712c2c66affSColin Finck State = AcpiUtPopGenericState (&StateList);
713c2c66affSColin Finck Object = State->Update.Object;
714c2c66affSColin Finck AcpiUtDeleteGenericState (State);
715c2c66affSColin Finck }
716c2c66affSColin Finck }
717c2c66affSColin Finck
718c2c66affSColin Finck return (AE_OK);
719c2c66affSColin Finck
720c2c66affSColin Finck
721c2c66affSColin Finck ErrorExit:
722c2c66affSColin Finck
723c2c66affSColin Finck ACPI_EXCEPTION ((AE_INFO, Status,
724c2c66affSColin Finck "Could not update object reference count"));
725c2c66affSColin Finck
726c2c66affSColin Finck /* Free any stacked Update State objects */
727c2c66affSColin Finck
728c2c66affSColin Finck while (StateList)
729c2c66affSColin Finck {
730c2c66affSColin Finck State = AcpiUtPopGenericState (&StateList);
731c2c66affSColin Finck AcpiUtDeleteGenericState (State);
732c2c66affSColin Finck }
733c2c66affSColin Finck
734c2c66affSColin Finck return (Status);
735c2c66affSColin Finck }
736c2c66affSColin Finck
737c2c66affSColin Finck
738c2c66affSColin Finck /*******************************************************************************
739c2c66affSColin Finck *
740c2c66affSColin Finck * FUNCTION: AcpiUtAddReference
741c2c66affSColin Finck *
742c2c66affSColin Finck * PARAMETERS: Object - Object whose reference count is to be
743c2c66affSColin Finck * incremented
744c2c66affSColin Finck *
745c2c66affSColin Finck * RETURN: None
746c2c66affSColin Finck *
747c2c66affSColin Finck * DESCRIPTION: Add one reference to an ACPI object
748c2c66affSColin Finck *
749c2c66affSColin Finck ******************************************************************************/
750c2c66affSColin Finck
751c2c66affSColin Finck void
AcpiUtAddReference(ACPI_OPERAND_OBJECT * Object)752c2c66affSColin Finck AcpiUtAddReference (
753c2c66affSColin Finck ACPI_OPERAND_OBJECT *Object)
754c2c66affSColin Finck {
755c2c66affSColin Finck
756c2c66affSColin Finck ACPI_FUNCTION_NAME (UtAddReference);
757c2c66affSColin Finck
758c2c66affSColin Finck
759c2c66affSColin Finck /* Ensure that we have a valid object */
760c2c66affSColin Finck
761c2c66affSColin Finck if (!AcpiUtValidInternalObject (Object))
762c2c66affSColin Finck {
763c2c66affSColin Finck return;
764c2c66affSColin Finck }
765c2c66affSColin Finck
766c2c66affSColin Finck ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
767c2c66affSColin Finck "Obj %p Current Refs=%X [To Be Incremented]\n",
768c2c66affSColin Finck Object, Object->Common.ReferenceCount));
769c2c66affSColin Finck
770c2c66affSColin Finck /* Increment the reference count */
771c2c66affSColin Finck
772c2c66affSColin Finck (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
773c2c66affSColin Finck return;
774c2c66affSColin Finck }
775c2c66affSColin Finck
776c2c66affSColin Finck
777c2c66affSColin Finck /*******************************************************************************
778c2c66affSColin Finck *
779c2c66affSColin Finck * FUNCTION: AcpiUtRemoveReference
780c2c66affSColin Finck *
781c2c66affSColin Finck * PARAMETERS: Object - Object whose ref count will be decremented
782c2c66affSColin Finck *
783c2c66affSColin Finck * RETURN: None
784c2c66affSColin Finck *
785c2c66affSColin Finck * DESCRIPTION: Decrement the reference count of an ACPI internal object
786c2c66affSColin Finck *
787c2c66affSColin Finck ******************************************************************************/
788c2c66affSColin Finck
789c2c66affSColin Finck void
AcpiUtRemoveReference(ACPI_OPERAND_OBJECT * Object)790c2c66affSColin Finck AcpiUtRemoveReference (
791c2c66affSColin Finck ACPI_OPERAND_OBJECT *Object)
792c2c66affSColin Finck {
793c2c66affSColin Finck
794c2c66affSColin Finck ACPI_FUNCTION_NAME (UtRemoveReference);
795c2c66affSColin Finck
796c2c66affSColin Finck
797c2c66affSColin Finck /*
798c2c66affSColin Finck * Allow a NULL pointer to be passed in, just ignore it. This saves
799c2c66affSColin Finck * each caller from having to check. Also, ignore NS nodes.
800c2c66affSColin Finck */
801c2c66affSColin Finck if (!Object ||
802c2c66affSColin Finck (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
803c2c66affSColin Finck
804c2c66affSColin Finck {
805c2c66affSColin Finck return;
806c2c66affSColin Finck }
807c2c66affSColin Finck
808c2c66affSColin Finck /* Ensure that we have a valid object */
809c2c66affSColin Finck
810c2c66affSColin Finck if (!AcpiUtValidInternalObject (Object))
811c2c66affSColin Finck {
812c2c66affSColin Finck return;
813c2c66affSColin Finck }
814c2c66affSColin Finck
8156847cc3aSThomas Faber ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS,
8166847cc3aSThomas Faber "%s: Obj %p Current Refs=%X [To Be Decremented]\n",
8176847cc3aSThomas Faber ACPI_GET_FUNCTION_NAME, Object, Object->Common.ReferenceCount));
818c2c66affSColin Finck
819c2c66affSColin Finck /*
820c2c66affSColin Finck * Decrement the reference count, and only actually delete the object
821c2c66affSColin Finck * if the reference count becomes 0. (Must also decrement the ref count
822c2c66affSColin Finck * of all subobjects!)
823c2c66affSColin Finck */
824c2c66affSColin Finck (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);
825c2c66affSColin Finck return;
826c2c66affSColin Finck }
827