Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / acpi / utilities / utdelete.c
blob9a52ad52a23aeacc9d5d099ce978f81fd446cfd7
1 /*******************************************************************************
3 * Module Name: utdelete - object deletion and reference count utilities
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * All rights reserved.
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.
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.
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.
45 #include <acpi/acpi.h>
46 #include <acpi/acinterp.h>
47 #include <acpi/acnamesp.h>
48 #include <acpi/acevents.h>
49 #include <acpi/amlcode.h>
51 #define _COMPONENT ACPI_UTILITIES
52 ACPI_MODULE_NAME ("utdelete")
55 /*******************************************************************************
57 * FUNCTION: acpi_ut_delete_internal_obj
59 * PARAMETERS: *Object - Pointer to the list to be deleted
61 * RETURN: None
63 * DESCRIPTION: Low level object deletion, after reference counts have been
64 * updated (All reference counts, including sub-objects!)
66 ******************************************************************************/
68 void
69 acpi_ut_delete_internal_obj (
70 union acpi_operand_object *object)
72 void *obj_pointer = NULL;
73 union acpi_operand_object *handler_desc;
74 union acpi_operand_object *second_desc;
75 union acpi_operand_object *next_desc;
78 ACPI_FUNCTION_TRACE_PTR ("ut_delete_internal_obj", object);
81 if (!object) {
82 return_VOID;
86 * Must delete or free any pointers within the object that are not
87 * actual ACPI objects (for example, a raw buffer pointer).
89 switch (ACPI_GET_OBJECT_TYPE (object)) {
90 case ACPI_TYPE_STRING:
92 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
93 object, object->string.pointer));
95 /* Free the actual string buffer */
97 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
98 /* But only if it is NOT a pointer into an ACPI table */
100 obj_pointer = object->string.pointer;
102 break;
105 case ACPI_TYPE_BUFFER:
107 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
108 object, object->buffer.pointer));
110 /* Free the actual buffer */
112 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
113 /* But only if it is NOT a pointer into an ACPI table */
115 obj_pointer = object->buffer.pointer;
117 break;
120 case ACPI_TYPE_PACKAGE:
122 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
123 object->package.count));
126 * Elements of the package are not handled here, they are deleted
127 * separately
130 /* Free the (variable length) element pointer array */
132 obj_pointer = object->package.elements;
133 break;
136 case ACPI_TYPE_DEVICE:
138 if (object->device.gpe_block) {
139 (void) acpi_ev_delete_gpe_block (object->device.gpe_block);
142 /* Walk the handler list for this device */
144 handler_desc = object->device.handler;
145 while (handler_desc) {
146 next_desc = handler_desc->address_space.next;
147 acpi_ut_remove_reference (handler_desc);
148 handler_desc = next_desc;
150 break;
153 case ACPI_TYPE_MUTEX:
155 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Mutex %p, Semaphore %p\n",
156 object, object->mutex.semaphore));
158 acpi_ex_unlink_mutex (object);
159 (void) acpi_os_delete_semaphore (object->mutex.semaphore);
160 break;
163 case ACPI_TYPE_EVENT:
165 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Event %p, Semaphore %p\n",
166 object, object->event.semaphore));
168 (void) acpi_os_delete_semaphore (object->event.semaphore);
169 object->event.semaphore = NULL;
170 break;
173 case ACPI_TYPE_METHOD:
175 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Method %p\n", object));
177 /* Delete the method semaphore if it exists */
179 if (object->method.semaphore) {
180 (void) acpi_os_delete_semaphore (object->method.semaphore);
181 object->method.semaphore = NULL;
183 break;
186 case ACPI_TYPE_REGION:
188 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Region %p\n", object));
190 second_desc = acpi_ns_get_secondary_object (object);
191 if (second_desc) {
193 * Free the region_context if and only if the handler is one of the
194 * default handlers -- and therefore, we created the context object
195 * locally, it was not created by an external caller.
197 handler_desc = object->region.handler;
198 if (handler_desc) {
199 if (handler_desc->address_space.hflags & ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
200 obj_pointer = second_desc->extra.region_context;
203 acpi_ut_remove_reference (handler_desc);
206 /* Now we can free the Extra object */
208 acpi_ut_delete_object_desc (second_desc);
210 break;
213 case ACPI_TYPE_BUFFER_FIELD:
215 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Buffer Field %p\n", object));
217 second_desc = acpi_ns_get_secondary_object (object);
218 if (second_desc) {
219 acpi_ut_delete_object_desc (second_desc);
221 break;
224 default:
225 break;
228 /* Free any allocated memory (pointer within the object) found above */
230 if (obj_pointer) {
231 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
232 obj_pointer));
233 ACPI_MEM_FREE (obj_pointer);
236 /* Now the object can be safely deleted */
238 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
239 object, acpi_ut_get_object_type_name (object)));
241 acpi_ut_delete_object_desc (object);
242 return_VOID;
246 /*******************************************************************************
248 * FUNCTION: acpi_ut_delete_internal_object_list
250 * PARAMETERS: *obj_list - Pointer to the list to be deleted
252 * RETURN: None
254 * DESCRIPTION: This function deletes an internal object list, including both
255 * simple objects and package objects
257 ******************************************************************************/
259 void
260 acpi_ut_delete_internal_object_list (
261 union acpi_operand_object **obj_list)
263 union acpi_operand_object **internal_obj;
266 ACPI_FUNCTION_TRACE ("ut_delete_internal_object_list");
269 /* Walk the null-terminated internal list */
271 for (internal_obj = obj_list; *internal_obj; internal_obj++) {
272 acpi_ut_remove_reference (*internal_obj);
275 /* Free the combined parameter pointer list and object array */
277 ACPI_MEM_FREE (obj_list);
278 return_VOID;
282 /*******************************************************************************
284 * FUNCTION: acpi_ut_update_ref_count
286 * PARAMETERS: *Object - Object whose ref count is to be updated
287 * Action - What to do
289 * RETURN: New ref count
291 * DESCRIPTION: Modify the ref count and return it.
293 ******************************************************************************/
295 static void
296 acpi_ut_update_ref_count (
297 union acpi_operand_object *object,
298 u32 action)
300 u16 count;
301 u16 new_count;
304 ACPI_FUNCTION_NAME ("ut_update_ref_count");
307 if (!object) {
308 return;
311 count = object->common.reference_count;
312 new_count = count;
315 * Perform the reference count action (increment, decrement, or force delete)
317 switch (action) {
319 case REF_INCREMENT:
321 new_count++;
322 object->common.reference_count = new_count;
324 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, [Incremented]\n",
325 object, new_count));
326 break;
329 case REF_DECREMENT:
331 if (count < 1) {
332 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
333 object, new_count));
335 new_count = 0;
337 else {
338 new_count--;
340 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, [Decremented]\n",
341 object, new_count));
344 if (ACPI_GET_OBJECT_TYPE (object) == ACPI_TYPE_METHOD) {
345 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Method Obj %p Refs=%X, [Decremented]\n",
346 object, new_count));
349 object->common.reference_count = new_count;
350 if (new_count == 0) {
351 acpi_ut_delete_internal_obj (object);
354 break;
357 case REF_FORCE_DELETE:
359 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, Force delete! (Set to 0)\n",
360 object, count));
362 new_count = 0;
363 object->common.reference_count = new_count;
364 acpi_ut_delete_internal_obj (object);
365 break;
368 default:
370 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown action (%X)\n", action));
371 break;
375 * Sanity check the reference count, for debug purposes only.
376 * (A deleted object will have a huge reference count)
378 if (count > ACPI_MAX_REFERENCE_COUNT) {
380 ACPI_DEBUG_PRINT ((ACPI_DB_WARN,
381 "**** Warning **** Large Reference Count (%X) in object %p\n\n",
382 count, object));
385 return;
389 /*******************************************************************************
391 * FUNCTION: acpi_ut_update_object_reference
393 * PARAMETERS: *Object - Increment ref count for this object
394 * and all sub-objects
395 * Action - Either REF_INCREMENT or REF_DECREMENT or
396 * REF_FORCE_DELETE
398 * RETURN: Status
400 * DESCRIPTION: Increment the object reference count
402 * Object references are incremented when:
403 * 1) An object is attached to a Node (namespace object)
404 * 2) An object is copied (all subobjects must be incremented)
406 * Object references are decremented when:
407 * 1) An object is detached from an Node
409 ******************************************************************************/
411 acpi_status
412 acpi_ut_update_object_reference (
413 union acpi_operand_object *object,
414 u16 action)
416 acpi_status status;
417 u32 i;
418 union acpi_generic_state *state_list = NULL;
419 union acpi_generic_state *state;
420 union acpi_operand_object *tmp;
422 ACPI_FUNCTION_TRACE_PTR ("ut_update_object_reference", object);
425 /* Ignore a null object ptr */
427 if (!object) {
428 return_ACPI_STATUS (AE_OK);
431 /* Make sure that this isn't a namespace handle */
433 if (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) {
434 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Object %p is NS handle\n", object));
435 return_ACPI_STATUS (AE_OK);
438 state = acpi_ut_create_update_state (object, action);
440 while (state) {
441 object = state->update.object;
442 action = state->update.value;
443 acpi_ut_delete_generic_state (state);
446 * All sub-objects must have their reference count incremented also.
447 * Different object types have different subobjects.
449 switch (ACPI_GET_OBJECT_TYPE (object)) {
450 case ACPI_TYPE_DEVICE:
452 tmp = object->device.system_notify;
453 if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
454 object->device.system_notify = NULL;
455 acpi_ut_update_ref_count (tmp, action);
457 tmp = object->device.device_notify;
458 if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
459 object->device.device_notify = NULL;
460 acpi_ut_update_ref_count (tmp, action);
462 break;
465 case ACPI_TYPE_PACKAGE:
468 * We must update all the sub-objects of the package
469 * (Each of whom may have their own sub-objects, etc.
471 for (i = 0; i < object->package.count; i++) {
473 * Push each element onto the stack for later processing.
474 * Note: There can be null elements within the package,
475 * these are simply ignored
477 status = acpi_ut_create_update_state_and_push (
478 object->package.elements[i], action, &state_list);
479 if (ACPI_FAILURE (status)) {
480 goto error_exit;
483 tmp = object->package.elements[i];
484 if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
485 object->package.elements[i] = NULL;
487 break;
490 case ACPI_TYPE_BUFFER_FIELD:
492 status = acpi_ut_create_update_state_and_push (
493 object->buffer_field.buffer_obj, action, &state_list);
494 if (ACPI_FAILURE (status)) {
495 goto error_exit;
498 tmp = object->buffer_field.buffer_obj;
499 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
500 object->buffer_field.buffer_obj = NULL;
501 break;
504 case ACPI_TYPE_LOCAL_REGION_FIELD:
506 status = acpi_ut_create_update_state_and_push (
507 object->field.region_obj, action, &state_list);
508 if (ACPI_FAILURE (status)) {
509 goto error_exit;
512 tmp = object->field.region_obj;
513 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
514 object->field.region_obj = NULL;
515 break;
518 case ACPI_TYPE_LOCAL_BANK_FIELD:
520 status = acpi_ut_create_update_state_and_push (
521 object->bank_field.bank_obj, action, &state_list);
522 if (ACPI_FAILURE (status)) {
523 goto error_exit;
526 tmp = object->bank_field.bank_obj;
527 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
528 object->bank_field.bank_obj = NULL;
530 status = acpi_ut_create_update_state_and_push (
531 object->bank_field.region_obj, action, &state_list);
532 if (ACPI_FAILURE (status)) {
533 goto error_exit;
536 tmp = object->bank_field.region_obj;
537 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
538 object->bank_field.region_obj = NULL;
539 break;
542 case ACPI_TYPE_LOCAL_INDEX_FIELD:
544 status = acpi_ut_create_update_state_and_push (
545 object->index_field.index_obj, action, &state_list);
546 if (ACPI_FAILURE (status)) {
547 goto error_exit;
550 tmp = object->index_field.index_obj;
551 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
552 object->index_field.index_obj = NULL;
554 status = acpi_ut_create_update_state_and_push (
555 object->index_field.data_obj, action, &state_list);
556 if (ACPI_FAILURE (status)) {
557 goto error_exit;
560 tmp = object->index_field.data_obj;
561 if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
562 object->index_field.data_obj = NULL;
563 break;
566 case ACPI_TYPE_LOCAL_REFERENCE:
569 * The target of an Index (a package, string, or buffer) must track
570 * changes to the ref count of the index.
572 if (object->reference.opcode == AML_INDEX_OP) {
573 status = acpi_ut_create_update_state_and_push (
574 object->reference.object, action, &state_list);
575 if (ACPI_FAILURE (status)) {
576 goto error_exit;
579 break;
582 case ACPI_TYPE_REGION:
583 default:
585 /* No subobjects */
586 break;
590 * Now we can update the count in the main object. This can only
591 * happen after we update the sub-objects in case this causes the
592 * main object to be deleted.
594 acpi_ut_update_ref_count (object, action);
596 /* Move on to the next object to be updated */
598 state = acpi_ut_pop_generic_state (&state_list);
601 return_ACPI_STATUS (AE_OK);
604 error_exit:
606 ACPI_REPORT_ERROR (("Could not update object reference count, %s\n",
607 acpi_format_exception (status)));
609 return_ACPI_STATUS (status);
613 /*******************************************************************************
615 * FUNCTION: acpi_ut_add_reference
617 * PARAMETERS: *Object - Object whose reference count is to be
618 * incremented
620 * RETURN: None
622 * DESCRIPTION: Add one reference to an ACPI object
624 ******************************************************************************/
626 void
627 acpi_ut_add_reference (
628 union acpi_operand_object *object)
631 ACPI_FUNCTION_TRACE_PTR ("ut_add_reference", object);
634 /* Ensure that we have a valid object */
636 if (!acpi_ut_valid_internal_object (object)) {
637 return_VOID;
640 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
641 "Obj %p Current Refs=%X [To Be Incremented]\n",
642 object, object->common.reference_count));
644 /* Increment the reference count */
646 (void) acpi_ut_update_object_reference (object, REF_INCREMENT);
647 return_VOID;
651 /*******************************************************************************
653 * FUNCTION: acpi_ut_remove_reference
655 * PARAMETERS: *Object - Object whose ref count will be decremented
657 * RETURN: None
659 * DESCRIPTION: Decrement the reference count of an ACPI internal object
661 ******************************************************************************/
663 void
664 acpi_ut_remove_reference (
665 union acpi_operand_object *object)
668 ACPI_FUNCTION_TRACE_PTR ("ut_remove_reference", object);
672 * Allow a NULL pointer to be passed in, just ignore it. This saves
673 * each caller from having to check. Also, ignore NS nodes.
676 if (!object ||
677 (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED)) {
678 return_VOID;
681 /* Ensure that we have a valid object */
683 if (!acpi_ut_valid_internal_object (object)) {
684 return_VOID;
687 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
688 "Obj %p Current Refs=%X [To Be Decremented]\n",
689 object, object->common.reference_count));
692 * Decrement the reference count, and only actually delete the object
693 * if the reference count becomes 0. (Must also decrement the ref count
694 * of all subobjects!)
696 (void) acpi_ut_update_object_reference (object, REF_DECREMENT);
697 return_VOID;