ACPICA 20050708 from Bob Moore <robert.moore@intel.com>
[safe/jmp/linux-2.6] / drivers / acpi / utilities / utdelete.c
1 /*******************************************************************************
2  *
3  * Module Name: utdelete - object deletion and reference count utilities
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
9  * 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
44
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>
50
51 #define _COMPONENT          ACPI_UTILITIES
52          ACPI_MODULE_NAME    ("utdelete")
53
54 /* Local prototypes */
55
56 static void
57 acpi_ut_delete_internal_obj (
58         union acpi_operand_object       *object);
59
60 static void
61 acpi_ut_update_ref_count (
62         union acpi_operand_object       *object,
63         u32                             action);
64
65
66 /*******************************************************************************
67  *
68  * FUNCTION:    acpi_ut_delete_internal_obj
69  *
70  * PARAMETERS:  Object         - Object to be deleted
71  *
72  * RETURN:      None
73  *
74  * DESCRIPTION: Low level object deletion, after reference counts have been
75  *              updated (All reference counts, including sub-objects!)
76  *
77  ******************************************************************************/
78
79 static void
80 acpi_ut_delete_internal_obj (
81         union acpi_operand_object       *object)
82 {
83         void                            *obj_pointer = NULL;
84         union acpi_operand_object       *handler_desc;
85         union acpi_operand_object       *second_desc;
86         union acpi_operand_object       *next_desc;
87
88
89         ACPI_FUNCTION_TRACE_PTR ("ut_delete_internal_obj", object);
90
91
92         if (!object) {
93                 return_VOID;
94         }
95
96         /*
97          * Must delete or free any pointers within the object that are not
98          * actual ACPI objects (for example, a raw buffer pointer).
99          */
100         switch (ACPI_GET_OBJECT_TYPE (object)) {
101         case ACPI_TYPE_STRING:
102
103                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
104                         object, object->string.pointer));
105
106                 /* Free the actual string buffer */
107
108                 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
109                         /* But only if it is NOT a pointer into an ACPI table */
110
111                         obj_pointer = object->string.pointer;
112                 }
113                 break;
114
115
116         case ACPI_TYPE_BUFFER:
117
118                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
119                         object, object->buffer.pointer));
120
121                 /* Free the actual buffer */
122
123                 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
124                         /* But only if it is NOT a pointer into an ACPI table */
125
126                         obj_pointer = object->buffer.pointer;
127                 }
128                 break;
129
130
131         case ACPI_TYPE_PACKAGE:
132
133                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
134                         object->package.count));
135
136                 /*
137                  * Elements of the package are not handled here, they are deleted
138                  * separately
139                  */
140
141                 /* Free the (variable length) element pointer array */
142
143                 obj_pointer = object->package.elements;
144                 break;
145
146
147         case ACPI_TYPE_DEVICE:
148
149                 if (object->device.gpe_block) {
150                         (void) acpi_ev_delete_gpe_block (object->device.gpe_block);
151                 }
152
153                 /* Walk the handler list for this device */
154
155                 handler_desc = object->device.handler;
156                 while (handler_desc) {
157                         next_desc = handler_desc->address_space.next;
158                         acpi_ut_remove_reference (handler_desc);
159                         handler_desc = next_desc;
160                 }
161                 break;
162
163
164         case ACPI_TYPE_MUTEX:
165
166                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
167                         "***** Mutex %p, Semaphore %p\n",
168                         object, object->mutex.semaphore));
169
170                 acpi_ex_unlink_mutex (object);
171                 (void) acpi_os_delete_semaphore (object->mutex.semaphore);
172                 break;
173
174
175         case ACPI_TYPE_EVENT:
176
177                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
178                         "***** Event %p, Semaphore %p\n",
179                         object, object->event.semaphore));
180
181                 (void) acpi_os_delete_semaphore (object->event.semaphore);
182                 object->event.semaphore = NULL;
183                 break;
184
185
186         case ACPI_TYPE_METHOD:
187
188                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
189                         "***** Method %p\n", object));
190
191                 /* Delete the method semaphore if it exists */
192
193                 if (object->method.semaphore) {
194                         (void) acpi_os_delete_semaphore (object->method.semaphore);
195                         object->method.semaphore = NULL;
196                 }
197                 break;
198
199
200         case ACPI_TYPE_REGION:
201
202                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
203                         "***** Region %p\n", object));
204
205                 second_desc = acpi_ns_get_secondary_object (object);
206                 if (second_desc) {
207                         /*
208                          * Free the region_context if and only if the handler is one of the
209                          * default handlers -- and therefore, we created the context object
210                          * locally, it was not created by an external caller.
211                          */
212                         handler_desc = object->region.handler;
213                         if (handler_desc) {
214                                 if (handler_desc->address_space.hflags & ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
215                                         obj_pointer = second_desc->extra.region_context;
216                                 }
217
218                                 acpi_ut_remove_reference (handler_desc);
219                         }
220
221                         /* Now we can free the Extra object */
222
223                         acpi_ut_delete_object_desc (second_desc);
224                 }
225                 break;
226
227
228         case ACPI_TYPE_BUFFER_FIELD:
229
230                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
231                         "***** Buffer Field %p\n", object));
232
233                 second_desc = acpi_ns_get_secondary_object (object);
234                 if (second_desc) {
235                         acpi_ut_delete_object_desc (second_desc);
236                 }
237                 break;
238
239
240         default:
241                 break;
242         }
243
244         /* Free any allocated memory (pointer within the object) found above */
245
246         if (obj_pointer) {
247                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
248                                 obj_pointer));
249                 ACPI_MEM_FREE (obj_pointer);
250         }
251
252         /* Now the object can be safely deleted */
253
254         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
255                         object, acpi_ut_get_object_type_name (object)));
256
257         acpi_ut_delete_object_desc (object);
258         return_VOID;
259 }
260
261
262 /*******************************************************************************
263  *
264  * FUNCTION:    acpi_ut_delete_internal_object_list
265  *
266  * PARAMETERS:  obj_list        - Pointer to the list to be deleted
267  *
268  * RETURN:      None
269  *
270  * DESCRIPTION: This function deletes an internal object list, including both
271  *              simple objects and package objects
272  *
273  ******************************************************************************/
274
275 void
276 acpi_ut_delete_internal_object_list (
277         union acpi_operand_object       **obj_list)
278 {
279         union acpi_operand_object       **internal_obj;
280
281
282         ACPI_FUNCTION_TRACE ("ut_delete_internal_object_list");
283
284
285         /* Walk the null-terminated internal list */
286
287         for (internal_obj = obj_list; *internal_obj; internal_obj++) {
288                 acpi_ut_remove_reference (*internal_obj);
289         }
290
291         /* Free the combined parameter pointer list and object array */
292
293         ACPI_MEM_FREE (obj_list);
294         return_VOID;
295 }
296
297
298 /*******************************************************************************
299  *
300  * FUNCTION:    acpi_ut_update_ref_count
301  *
302  * PARAMETERS:  Object          - Object whose ref count is to be updated
303  *              Action          - What to do
304  *
305  * RETURN:      New ref count
306  *
307  * DESCRIPTION: Modify the ref count and return it.
308  *
309  ******************************************************************************/
310
311 static void
312 acpi_ut_update_ref_count (
313         union acpi_operand_object       *object,
314         u32                             action)
315 {
316         u16                             count;
317         u16                             new_count;
318
319
320         ACPI_FUNCTION_NAME ("ut_update_ref_count");
321
322
323         if (!object) {
324                 return;
325         }
326
327         count = object->common.reference_count;
328         new_count = count;
329
330         /*
331          * Perform the reference count action
332          * (increment, decrement, or force delete)
333          */
334         switch (action) {
335
336         case REF_INCREMENT:
337
338                 new_count++;
339                 object->common.reference_count = new_count;
340
341                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
342                         "Obj %p Refs=%X, [Incremented]\n",
343                         object, new_count));
344                 break;
345
346
347         case REF_DECREMENT:
348
349                 if (count < 1) {
350                         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
351                                 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
352                                 object, new_count));
353
354                         new_count = 0;
355                 }
356                 else {
357                         new_count--;
358
359                         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
360                                 "Obj %p Refs=%X, [Decremented]\n",
361                                 object, new_count));
362                 }
363
364                 if (ACPI_GET_OBJECT_TYPE (object) == ACPI_TYPE_METHOD) {
365                         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
366                                 "Method Obj %p Refs=%X, [Decremented]\n",
367                                 object, new_count));
368                 }
369
370                 object->common.reference_count = new_count;
371                 if (new_count == 0) {
372                         acpi_ut_delete_internal_obj (object);
373                 }
374
375                 break;
376
377
378         case REF_FORCE_DELETE:
379
380                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
381                         "Obj %p Refs=%X, Force delete! (Set to 0)\n",
382                         object, count));
383
384                 new_count = 0;
385                 object->common.reference_count = new_count;
386                 acpi_ut_delete_internal_obj (object);
387                 break;
388
389
390         default:
391
392                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown action (%X)\n", action));
393                 break;
394         }
395
396         /*
397          * Sanity check the reference count, for debug purposes only.
398          * (A deleted object will have a huge reference count)
399          */
400         if (count > ACPI_MAX_REFERENCE_COUNT) {
401
402                 ACPI_DEBUG_PRINT ((ACPI_DB_WARN,
403                         "**** Warning **** Large Reference Count (%X) in object %p\n\n",
404                         count, object));
405         }
406
407         return;
408 }
409
410
411 /*******************************************************************************
412  *
413  * FUNCTION:    acpi_ut_update_object_reference
414  *
415  * PARAMETERS:  Object              - Increment ref count for this object
416  *                                    and all sub-objects
417  *              Action              - Either REF_INCREMENT or REF_DECREMENT or
418  *                                    REF_FORCE_DELETE
419  *
420  * RETURN:      Status
421  *
422  * DESCRIPTION: Increment the object reference count
423  *
424  * Object references are incremented when:
425  * 1) An object is attached to a Node (namespace object)
426  * 2) An object is copied (all subobjects must be incremented)
427  *
428  * Object references are decremented when:
429  * 1) An object is detached from an Node
430  *
431  ******************************************************************************/
432
433 acpi_status
434 acpi_ut_update_object_reference (
435         union acpi_operand_object       *object,
436         u16                             action)
437 {
438         acpi_status                     status = AE_OK;
439         union acpi_generic_state        *state_list = NULL;
440         union acpi_operand_object       *next_object = NULL;
441         union acpi_generic_state        *state;
442         acpi_native_uint                i;
443
444
445         ACPI_FUNCTION_TRACE_PTR ("ut_update_object_reference", object);
446
447
448         while (object) {
449                 /* Make sure that this isn't a namespace handle */
450
451                 if (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) {
452                         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
453                                 "Object %p is NS handle\n", object));
454                         return_ACPI_STATUS (AE_OK);
455                 }
456
457                 /*
458                  * All sub-objects must have their reference count incremented also.
459                  * Different object types have different subobjects.
460                  */
461                 switch (ACPI_GET_OBJECT_TYPE (object)) {
462                 case ACPI_TYPE_DEVICE:
463
464                         acpi_ut_update_ref_count (object->device.system_notify, action);
465                         acpi_ut_update_ref_count (object->device.device_notify, action);
466                         break;
467
468                 case ACPI_TYPE_PACKAGE:
469                         /*
470                          * We must update all the sub-objects of the package,
471                          * each of whom may have their own sub-objects.
472                          */
473                         for (i = 0; i < object->package.count; i++) {
474                                 /*
475                                  * Push each element onto the stack for later processing.
476                                  * Note: There can be null elements within the package,
477                                  * these are simply ignored
478                                  */
479                                 status = acpi_ut_create_update_state_and_push (
480                                                  object->package.elements[i], action, &state_list);
481                                 if (ACPI_FAILURE (status)) {
482                                         goto error_exit;
483                                 }
484                         }
485                         break;
486
487                 case ACPI_TYPE_BUFFER_FIELD:
488
489                         next_object = object->buffer_field.buffer_obj;
490                         break;
491
492                 case ACPI_TYPE_LOCAL_REGION_FIELD:
493
494                         next_object = object->field.region_obj;
495                         break;
496
497                 case ACPI_TYPE_LOCAL_BANK_FIELD:
498
499                         next_object = object->bank_field.bank_obj;
500                         status = acpi_ut_create_update_state_and_push (
501                                          object->bank_field.region_obj, action, &state_list);
502                         if (ACPI_FAILURE (status)) {
503                                 goto error_exit;
504                         }
505                         break;
506
507                 case ACPI_TYPE_LOCAL_INDEX_FIELD:
508
509                         next_object = object->index_field.index_obj;
510                         status = acpi_ut_create_update_state_and_push (
511                                          object->index_field.data_obj, action, &state_list);
512                         if (ACPI_FAILURE (status)) {
513                                 goto error_exit;
514                         }
515                         break;
516
517                 case ACPI_TYPE_LOCAL_REFERENCE:
518                         /*
519                          * The target of an Index (a package, string, or buffer) must track
520                          * changes to the ref count of the index.
521                          */
522                         if (object->reference.opcode == AML_INDEX_OP) {
523                                 next_object = object->reference.object;
524                         }
525                         break;
526
527                 case ACPI_TYPE_REGION:
528                 default:
529                         break;/* No subobjects */
530                 }
531
532                 /*
533                  * Now we can update the count in the main object.  This can only
534                  * happen after we update the sub-objects in case this causes the
535                  * main object to be deleted.
536                  */
537                 acpi_ut_update_ref_count (object, action);
538                 object = NULL;
539
540                 /* Move on to the next object to be updated */
541
542                 if (next_object) {
543                         object = next_object;
544                         next_object = NULL;
545                 }
546                 else if (state_list) {
547                         state = acpi_ut_pop_generic_state (&state_list);
548                         object = state->update.object;
549                         acpi_ut_delete_generic_state (state);
550                 }
551         }
552
553         return_ACPI_STATUS (AE_OK);
554
555 error_exit:
556
557         ACPI_REPORT_ERROR (("Could not update object reference count, %s\n",
558                 acpi_format_exception (status)));
559
560         return_ACPI_STATUS (status);
561 }
562
563
564 /*******************************************************************************
565  *
566  * FUNCTION:    acpi_ut_add_reference
567  *
568  * PARAMETERS:  Object          - Object whose reference count is to be
569  *                                incremented
570  *
571  * RETURN:      None
572  *
573  * DESCRIPTION: Add one reference to an ACPI object
574  *
575  ******************************************************************************/
576
577 void
578 acpi_ut_add_reference (
579         union acpi_operand_object       *object)
580 {
581
582         ACPI_FUNCTION_TRACE_PTR ("ut_add_reference", object);
583
584
585         /* Ensure that we have a valid object */
586
587         if (!acpi_ut_valid_internal_object (object)) {
588                 return_VOID;
589         }
590
591         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
592                 "Obj %p Current Refs=%X [To Be Incremented]\n",
593                 object, object->common.reference_count));
594
595         /* Increment the reference count */
596
597         (void) acpi_ut_update_object_reference (object, REF_INCREMENT);
598         return_VOID;
599 }
600
601
602 /*******************************************************************************
603  *
604  * FUNCTION:    acpi_ut_remove_reference
605  *
606  * PARAMETERS:  Object         - Object whose ref count will be decremented
607  *
608  * RETURN:      None
609  *
610  * DESCRIPTION: Decrement the reference count of an ACPI internal object
611  *
612  ******************************************************************************/
613
614 void
615 acpi_ut_remove_reference (
616         union acpi_operand_object       *object)
617 {
618
619         ACPI_FUNCTION_TRACE_PTR ("ut_remove_reference", object);
620
621
622         /*
623          * Allow a NULL pointer to be passed in, just ignore it.  This saves
624          * each caller from having to check.  Also, ignore NS nodes.
625          *
626          */
627         if (!object ||
628                 (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED)) {
629                 return_VOID;
630         }
631
632         /* Ensure that we have a valid object */
633
634         if (!acpi_ut_valid_internal_object (object)) {
635                 return_VOID;
636         }
637
638         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
639                 "Obj %p Current Refs=%X [To Be Decremented]\n",
640                 object, object->common.reference_count));
641
642         /*
643          * Decrement the reference count, and only actually delete the object
644          * if the reference count becomes 0.  (Must also decrement the ref count
645          * of all subobjects!)
646          */
647         (void) acpi_ut_update_object_reference (object, REF_DECREMENT);
648         return_VOID;
649 }
650
651