blob: 5c219758c226ce283117733af39a4479b752e77f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: utdelete - object deletion and reference count utilities
4 *
5 ******************************************************************************/
6
7/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
45#include <acpi/acinterp.h>
46#include <acpi/acnamesp.h>
47#include <acpi/acevents.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define _COMPONENT ACPI_UTILITIES
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("utdelete")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Robert Moore44f6c012005-04-18 22:49:35 -040052/* Local prototypes */
Len Brown4be44fc2005-08-05 00:44:28 -040053static void acpi_ut_delete_internal_obj(union acpi_operand_object *object);
Robert Moore44f6c012005-04-18 22:49:35 -040054
55static void
Len Brown4be44fc2005-08-05 00:44:28 -040056acpi_ut_update_ref_count(union acpi_operand_object *object, u32 action);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*******************************************************************************
59 *
60 * FUNCTION: acpi_ut_delete_internal_obj
61 *
Robert Moore44f6c012005-04-18 22:49:35 -040062 * PARAMETERS: Object - Object to be deleted
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 * RETURN: None
65 *
66 * DESCRIPTION: Low level object deletion, after reference counts have been
67 * updated (All reference counts, including sub-objects!)
68 *
69 ******************************************************************************/
70
Len Brown4be44fc2005-08-05 00:44:28 -040071static void acpi_ut_delete_internal_obj(union acpi_operand_object *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Len Brown4be44fc2005-08-05 00:44:28 -040073 void *obj_pointer = NULL;
74 union acpi_operand_object *handler_desc;
75 union acpi_operand_object *second_desc;
76 union acpi_operand_object *next_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Bob Mooreb229cf92006-04-21 17:15:00 -040078 ACPI_FUNCTION_TRACE_PTR(ut_delete_internal_obj, object);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 if (!object) {
81 return_VOID;
82 }
83
84 /*
85 * Must delete or free any pointers within the object that are not
86 * actual ACPI objects (for example, a raw buffer pointer).
87 */
Len Brown4be44fc2005-08-05 00:44:28 -040088 switch (ACPI_GET_OBJECT_TYPE(object)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 case ACPI_TYPE_STRING:
90
Len Brown4be44fc2005-08-05 00:44:28 -040091 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
92 "**** String %p, ptr %p\n", object,
93 object->string.pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /* Free the actual string buffer */
96
97 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
Bob Moore52fc0b02006-10-02 00:00:00 -040098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /* But only if it is NOT a pointer into an ACPI table */
100
101 obj_pointer = object->string.pointer;
102 }
103 break;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 case ACPI_TYPE_BUFFER:
106
Len Brown4be44fc2005-08-05 00:44:28 -0400107 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
108 "**** Buffer %p, ptr %p\n", object,
109 object->buffer.pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* Free the actual buffer */
112
113 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /* But only if it is NOT a pointer into an ACPI table */
116
117 obj_pointer = object->buffer.pointer;
118 }
119 break;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 case ACPI_TYPE_PACKAGE:
122
Len Brown4be44fc2005-08-05 00:44:28 -0400123 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
124 " **** Package of count %X\n",
125 object->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 /*
128 * Elements of the package are not handled here, they are deleted
129 * separately
130 */
131
132 /* Free the (variable length) element pointer array */
133
134 obj_pointer = object->package.elements;
135 break;
136
Bob Moore2843ae72008-07-04 10:41:41 +0800137 /*
138 * These objects have a possible list of notify handlers.
139 * Device object also may have a GPE block.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 case ACPI_TYPE_DEVICE:
142
143 if (object->device.gpe_block) {
Len Brown4be44fc2005-08-05 00:44:28 -0400144 (void)acpi_ev_delete_gpe_block(object->device.
145 gpe_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
Bob Moore2843ae72008-07-04 10:41:41 +0800148 /*lint -fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Bob Moore2843ae72008-07-04 10:41:41 +0800150 case ACPI_TYPE_PROCESSOR:
151 case ACPI_TYPE_THERMAL:
152
153 /* Walk the notify handler list for this object */
154
155 handler_desc = object->common_notify.handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 while (handler_desc) {
157 next_desc = handler_desc->address_space.next;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 acpi_ut_remove_reference(handler_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 handler_desc = next_desc;
160 }
161 break;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 case ACPI_TYPE_MUTEX:
164
Len Brown4be44fc2005-08-05 00:44:28 -0400165 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
Bob Moore967440e32006-06-23 17:04:00 -0400166 "***** Mutex %p, OS Mutex %p\n",
167 object, object->mutex.os_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Bob Mooreba886cd2008-04-10 19:06:37 +0400169 if (object == acpi_gbl_global_lock_mutex) {
Bob Moorec81da662007-02-02 19:48:18 +0300170
171 /* Global Lock has extra semaphore */
Bob Moore967440e32006-06-23 17:04:00 -0400172
173 (void)
174 acpi_os_delete_semaphore
175 (acpi_gbl_global_lock_semaphore);
176 acpi_gbl_global_lock_semaphore = NULL;
Bob Moorec81da662007-02-02 19:48:18 +0300177
178 acpi_os_delete_mutex(object->mutex.os_mutex);
179 acpi_gbl_global_lock_mutex = NULL;
180 } else {
Len Brown262a7a22007-05-09 23:01:59 -0400181 acpi_ex_unlink_mutex(object);
Bob Moorec81da662007-02-02 19:48:18 +0300182 acpi_os_delete_mutex(object->mutex.os_mutex);
Bob Moore967440e32006-06-23 17:04:00 -0400183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 case ACPI_TYPE_EVENT:
187
Len Brown4be44fc2005-08-05 00:44:28 -0400188 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
Bob Moore967440e32006-06-23 17:04:00 -0400189 "***** Event %p, OS Semaphore %p\n",
190 object, object->event.os_semaphore));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Bob Moore967440e32006-06-23 17:04:00 -0400192 (void)acpi_os_delete_semaphore(object->event.os_semaphore);
193 object->event.os_semaphore = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 break;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 case ACPI_TYPE_METHOD:
197
Len Brown4be44fc2005-08-05 00:44:28 -0400198 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
199 "***** Method %p\n", object));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Bob Moore967440e32006-06-23 17:04:00 -0400201 /* Delete the method mutex if it exists */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Bob Moore967440e32006-06-23 17:04:00 -0400203 if (object->method.mutex) {
204 acpi_os_delete_mutex(object->method.mutex->mutex.
205 os_mutex);
206 acpi_ut_delete_object_desc(object->method.mutex);
207 object->method.mutex = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 break;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case ACPI_TYPE_REGION:
212
Len Brown4be44fc2005-08-05 00:44:28 -0400213 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
214 "***** Region %p\n", object));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Len Brown4be44fc2005-08-05 00:44:28 -0400216 second_desc = acpi_ns_get_secondary_object(object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (second_desc) {
218 /*
219 * Free the region_context if and only if the handler is one of the
220 * default handlers -- and therefore, we created the context object
221 * locally, it was not created by an external caller.
222 */
223 handler_desc = object->region.handler;
224 if (handler_desc) {
Bob Moore616861242006-03-17 16:44:00 -0500225 if (handler_desc->address_space.handler_flags &
Len Brown4be44fc2005-08-05 00:44:28 -0400226 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
Bob Moore793c2382006-03-31 00:00:00 -0500227
228 /* Deactivate region and free region context */
229
230 if (handler_desc->address_space.setup) {
231 (void)handler_desc->
232 address_space.setup(object,
233 ACPI_REGION_DEACTIVATE,
234 handler_desc->
235 address_space.
236 context,
237 &second_desc->
238 extra.
239 region_context);
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 acpi_ut_remove_reference(handler_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 /* Now we can free the Extra object */
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248 acpi_ut_delete_object_desc(second_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250 break;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 case ACPI_TYPE_BUFFER_FIELD:
253
Len Brown4be44fc2005-08-05 00:44:28 -0400254 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
255 "***** Buffer Field %p\n", object));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 second_desc = acpi_ns_get_secondary_object(object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (second_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400259 acpi_ut_delete_object_desc(second_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 break;
262
Lin Mingef805d92008-04-10 19:06:41 +0400263 case ACPI_TYPE_LOCAL_BANK_FIELD:
264
265 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
266 "***** Bank Field %p\n", object));
267
268 second_desc = acpi_ns_get_secondary_object(object);
269 if (second_desc) {
270 acpi_ut_delete_object_desc(second_desc);
271 }
272 break;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 default:
275 break;
276 }
277
278 /* Free any allocated memory (pointer within the object) found above */
279
280 if (obj_pointer) {
Len Brown4be44fc2005-08-05 00:44:28 -0400281 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
282 "Deleting Object Subptr %p\n", obj_pointer));
Bob Moore83135242006-10-03 00:00:00 -0400283 ACPI_FREE(obj_pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 /* Now the object can be safely deleted */
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
289 object, acpi_ut_get_object_type_name(object)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Len Brown4be44fc2005-08-05 00:44:28 -0400291 acpi_ut_delete_object_desc(object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return_VOID;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*******************************************************************************
296 *
297 * FUNCTION: acpi_ut_delete_internal_object_list
298 *
Robert Moore44f6c012005-04-18 22:49:35 -0400299 * PARAMETERS: obj_list - Pointer to the list to be deleted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 *
301 * RETURN: None
302 *
303 * DESCRIPTION: This function deletes an internal object list, including both
304 * simple objects and package objects
305 *
306 ******************************************************************************/
307
Len Brown4be44fc2005-08-05 00:44:28 -0400308void acpi_ut_delete_internal_object_list(union acpi_operand_object **obj_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Len Brown4be44fc2005-08-05 00:44:28 -0400310 union acpi_operand_object **internal_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Bob Mooreb229cf92006-04-21 17:15:00 -0400312 ACPI_FUNCTION_TRACE(ut_delete_internal_object_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Walk the null-terminated internal list */
315
316 for (internal_obj = obj_list; *internal_obj; internal_obj++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400317 acpi_ut_remove_reference(*internal_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
320 /* Free the combined parameter pointer list and object array */
321
Bob Moore83135242006-10-03 00:00:00 -0400322 ACPI_FREE(obj_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return_VOID;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*******************************************************************************
327 *
328 * FUNCTION: acpi_ut_update_ref_count
329 *
Robert Moore44f6c012005-04-18 22:49:35 -0400330 * PARAMETERS: Object - Object whose ref count is to be updated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * Action - What to do
332 *
333 * RETURN: New ref count
334 *
335 * DESCRIPTION: Modify the ref count and return it.
336 *
337 ******************************************************************************/
338
339static void
Len Brown4be44fc2005-08-05 00:44:28 -0400340acpi_ut_update_ref_count(union acpi_operand_object *object, u32 action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Len Brown4be44fc2005-08-05 00:44:28 -0400342 u16 count;
343 u16 new_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Bob Mooreb229cf92006-04-21 17:15:00 -0400345 ACPI_FUNCTION_NAME(ut_update_ref_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (!object) {
348 return;
349 }
350
351 count = object->common.reference_count;
352 new_count = count;
353
354 /*
Bob Moore41195322006-05-26 16:36:00 -0400355 * Perform the reference count action (increment, decrement, force delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
357 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 case REF_INCREMENT:
359
360 new_count++;
361 object->common.reference_count = new_count;
362
Len Brown4be44fc2005-08-05 00:44:28 -0400363 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
364 "Obj %p Refs=%X, [Incremented]\n",
365 object, new_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 break;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 case REF_DECREMENT:
369
370 if (count < 1) {
Len Brown4be44fc2005-08-05 00:44:28 -0400371 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
372 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
373 object, new_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 new_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400376 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 new_count--;
378
Len Brown4be44fc2005-08-05 00:44:28 -0400379 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
380 "Obj %p Refs=%X, [Decremented]\n",
381 object, new_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Len Brown4be44fc2005-08-05 00:44:28 -0400384 if (ACPI_GET_OBJECT_TYPE(object) == ACPI_TYPE_METHOD) {
385 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
386 "Method Obj %p Refs=%X, [Decremented]\n",
387 object, new_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 object->common.reference_count = new_count;
391 if (new_count == 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400392 acpi_ut_delete_internal_obj(object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 case REF_FORCE_DELETE:
397
Len Brown4be44fc2005-08-05 00:44:28 -0400398 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
399 "Obj %p Refs=%X, Force delete! (Set to 0)\n",
400 object, count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 new_count = 0;
403 object->common.reference_count = new_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400404 acpi_ut_delete_internal_obj(object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 default:
408
Bob Mooreb8e4d892006-01-27 16:43:00 -0500409 ACPI_ERROR((AE_INFO, "Unknown action (%X)", action));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
411 }
412
413 /*
414 * Sanity check the reference count, for debug purposes only.
415 * (A deleted object will have a huge reference count)
416 */
417 if (count > ACPI_MAX_REFERENCE_COUNT) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500418 ACPI_WARNING((AE_INFO,
Bob Moore41195322006-05-26 16:36:00 -0400419 "Large Reference Count (%X) in object %p", count,
420 object));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/*******************************************************************************
425 *
426 * FUNCTION: acpi_ut_update_object_reference
427 *
Robert Moore44f6c012005-04-18 22:49:35 -0400428 * PARAMETERS: Object - Increment ref count for this object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * and all sub-objects
430 * Action - Either REF_INCREMENT or REF_DECREMENT or
431 * REF_FORCE_DELETE
432 *
433 * RETURN: Status
434 *
435 * DESCRIPTION: Increment the object reference count
436 *
437 * Object references are incremented when:
438 * 1) An object is attached to a Node (namespace object)
439 * 2) An object is copied (all subobjects must be incremented)
440 *
441 * Object references are decremented when:
442 * 1) An object is detached from an Node
443 *
444 ******************************************************************************/
445
446acpi_status
Bob Moore41195322006-05-26 16:36:00 -0400447acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Len Brown4be44fc2005-08-05 00:44:28 -0400449 acpi_status status = AE_OK;
450 union acpi_generic_state *state_list = NULL;
451 union acpi_operand_object *next_object = NULL;
452 union acpi_generic_state *state;
Bob Moore67a119f2008-06-10 13:42:13 +0800453 u32 i;
David Shaohua Li4c3ffbd2005-07-14 00:00:00 -0400454
Bob Mooreb229cf92006-04-21 17:15:00 -0400455 ACPI_FUNCTION_TRACE_PTR(ut_update_object_reference, object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Robert Mooref9f46012005-07-08 00:00:00 -0400457 while (object) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400458
Robert Mooref9f46012005-07-08 00:00:00 -0400459 /* Make sure that this isn't a namespace handle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Len Brown4be44fc2005-08-05 00:44:28 -0400461 if (ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_NAMED) {
462 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
463 "Object %p is NS handle\n", object));
464 return_ACPI_STATUS(AE_OK);
Robert Mooref9f46012005-07-08 00:00:00 -0400465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /*
468 * All sub-objects must have their reference count incremented also.
469 * Different object types have different subobjects.
470 */
Len Brown4be44fc2005-08-05 00:44:28 -0400471 switch (ACPI_GET_OBJECT_TYPE(object)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 case ACPI_TYPE_DEVICE:
Bob Mooref6dd9222006-07-07 20:44:38 -0400473 case ACPI_TYPE_PROCESSOR:
474 case ACPI_TYPE_POWER:
475 case ACPI_TYPE_THERMAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Bob Mooref6dd9222006-07-07 20:44:38 -0400477 /* Update the notify objects for these types (if present) */
478
479 acpi_ut_update_ref_count(object->common_notify.
480 system_notify, action);
481 acpi_ut_update_ref_count(object->common_notify.
482 device_notify, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 case ACPI_TYPE_PACKAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
Robert Mooref9f46012005-07-08 00:00:00 -0400487 * We must update all the sub-objects of the package,
488 * each of whom may have their own sub-objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 */
490 for (i = 0; i < object->package.count; i++) {
491 /*
492 * Push each element onto the stack for later processing.
493 * Note: There can be null elements within the package,
494 * these are simply ignored
495 */
Len Brown4be44fc2005-08-05 00:44:28 -0400496 status =
497 acpi_ut_create_update_state_and_push
498 (object->package.elements[i], action,
499 &state_list);
500 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto error_exit;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 break;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 case ACPI_TYPE_BUFFER_FIELD:
507
Robert Mooref9f46012005-07-08 00:00:00 -0400508 next_object = object->buffer_field.buffer_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 case ACPI_TYPE_LOCAL_REGION_FIELD:
512
Robert Mooref9f46012005-07-08 00:00:00 -0400513 next_object = object->field.region_obj;
514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 case ACPI_TYPE_LOCAL_BANK_FIELD:
517
Robert Mooref9f46012005-07-08 00:00:00 -0400518 next_object = object->bank_field.bank_obj;
Len Brown4be44fc2005-08-05 00:44:28 -0400519 status =
520 acpi_ut_create_update_state_and_push(object->
521 bank_field.
522 region_obj,
523 action,
524 &state_list);
525 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto error_exit;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 case ACPI_TYPE_LOCAL_INDEX_FIELD:
531
Robert Mooref9f46012005-07-08 00:00:00 -0400532 next_object = object->index_field.index_obj;
Len Brown4be44fc2005-08-05 00:44:28 -0400533 status =
534 acpi_ut_create_update_state_and_push(object->
535 index_field.
536 data_obj,
537 action,
538 &state_list);
539 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto error_exit;
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 case ACPI_TYPE_LOCAL_REFERENCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /*
Bob Moore9e41d932008-04-10 19:06:39 +0400546 * The target of an Index (a package, string, or buffer) or a named
547 * reference must track changes to the ref count of the index or
548 * target object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Bob Moore1044f1f2008-09-27 11:08:41 +0800550 if ((object->reference.class == ACPI_REFCLASS_INDEX) ||
551 (object->reference.class == ACPI_REFCLASS_NAME)) {
Robert Mooref9f46012005-07-08 00:00:00 -0400552 next_object = object->reference.object;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 break;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 case ACPI_TYPE_REGION:
557 default:
Bob Moore41195322006-05-26 16:36:00 -0400558 break; /* No subobjects for all other types */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
561 /*
Bob Moore41195322006-05-26 16:36:00 -0400562 * Now we can update the count in the main object. This can only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * happen after we update the sub-objects in case this causes the
564 * main object to be deleted.
565 */
Len Brown4be44fc2005-08-05 00:44:28 -0400566 acpi_ut_update_ref_count(object, action);
Robert Mooref9f46012005-07-08 00:00:00 -0400567 object = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Move on to the next object to be updated */
570
Robert Mooref9f46012005-07-08 00:00:00 -0400571 if (next_object) {
572 object = next_object;
573 next_object = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400574 } else if (state_list) {
575 state = acpi_ut_pop_generic_state(&state_list);
Robert Mooref9f46012005-07-08 00:00:00 -0400576 object = state->update.object;
Len Brown4be44fc2005-08-05 00:44:28 -0400577 acpi_ut_delete_generic_state(state);
Robert Mooref9f46012005-07-08 00:00:00 -0400578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
Len Brown4be44fc2005-08-05 00:44:28 -0400581 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Len Brown4be44fc2005-08-05 00:44:28 -0400583 error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Bob Mooreb8e4d892006-01-27 16:43:00 -0500585 ACPI_EXCEPTION((AE_INFO, status,
586 "Could not update object reference count"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Len Brown4be44fc2005-08-05 00:44:28 -0400588 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*******************************************************************************
592 *
593 * FUNCTION: acpi_ut_add_reference
594 *
Robert Moore44f6c012005-04-18 22:49:35 -0400595 * PARAMETERS: Object - Object whose reference count is to be
596 * incremented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
598 * RETURN: None
599 *
600 * DESCRIPTION: Add one reference to an ACPI object
601 *
602 ******************************************************************************/
603
Len Brown4be44fc2005-08-05 00:44:28 -0400604void acpi_ut_add_reference(union acpi_operand_object *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606
Bob Mooreb229cf92006-04-21 17:15:00 -0400607 ACPI_FUNCTION_TRACE_PTR(ut_add_reference, object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* Ensure that we have a valid object */
610
Len Brown4be44fc2005-08-05 00:44:28 -0400611 if (!acpi_ut_valid_internal_object(object)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return_VOID;
613 }
614
Len Brown4be44fc2005-08-05 00:44:28 -0400615 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
616 "Obj %p Current Refs=%X [To Be Incremented]\n",
617 object, object->common.reference_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* Increment the reference count */
620
Len Brown4be44fc2005-08-05 00:44:28 -0400621 (void)acpi_ut_update_object_reference(object, REF_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return_VOID;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*******************************************************************************
626 *
627 * FUNCTION: acpi_ut_remove_reference
628 *
Robert Moore44f6c012005-04-18 22:49:35 -0400629 * PARAMETERS: Object - Object whose ref count will be decremented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
631 * RETURN: None
632 *
633 * DESCRIPTION: Decrement the reference count of an ACPI internal object
634 *
635 ******************************************************************************/
636
Len Brown4be44fc2005-08-05 00:44:28 -0400637void acpi_ut_remove_reference(union acpi_operand_object *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639
Bob Mooreb229cf92006-04-21 17:15:00 -0400640 ACPI_FUNCTION_TRACE_PTR(ut_remove_reference, object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /*
Bob Moore41195322006-05-26 16:36:00 -0400643 * Allow a NULL pointer to be passed in, just ignore it. This saves
644 * each caller from having to check. Also, ignore NS nodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 *
646 */
647 if (!object ||
Len Brown4be44fc2005-08-05 00:44:28 -0400648 (ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_NAMED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return_VOID;
650 }
651
652 /* Ensure that we have a valid object */
653
Len Brown4be44fc2005-08-05 00:44:28 -0400654 if (!acpi_ut_valid_internal_object(object)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return_VOID;
656 }
657
Len Brown4be44fc2005-08-05 00:44:28 -0400658 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
659 "Obj %p Current Refs=%X [To Be Decremented]\n",
660 object, object->common.reference_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /*
663 * Decrement the reference count, and only actually delete the object
Bob Moore41195322006-05-26 16:36:00 -0400664 * if the reference count becomes 0. (Must also decrement the ref count
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * of all subobjects!)
666 */
Len Brown4be44fc2005-08-05 00:44:28 -0400667 (void)acpi_ut_update_object_reference(object, REF_DECREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return_VOID;
669}