vserver 2.0 rc7
[linux-2.6.git] / 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
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_ut_delete_internal_obj
58  *
59  * PARAMETERS:  *Object        - Pointer to the list to be deleted
60  *
61  * RETURN:      None
62  *
63  * DESCRIPTION: Low level object deletion, after reference counts have been
64  *              updated (All reference counts, including sub-objects!)
65  *
66  ******************************************************************************/
67
68 void
69 acpi_ut_delete_internal_obj (
70         union acpi_operand_object       *object)
71 {
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;
76
77
78         ACPI_FUNCTION_TRACE_PTR ("ut_delete_internal_obj", object);
79
80
81         if (!object) {
82                 return_VOID;
83         }
84
85         /*
86          * Must delete or free any pointers within the object that are not
87          * actual ACPI objects (for example, a raw buffer pointer).
88          */
89         switch (ACPI_GET_OBJECT_TYPE (object)) {
90         case ACPI_TYPE_STRING:
91
92                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
93                         object, object->string.pointer));
94
95                 /* Free the actual string buffer */
96
97                 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
98                         /* But only if it is NOT a pointer into an ACPI table */
99
100                         obj_pointer = object->string.pointer;
101                 }
102                 break;
103
104
105         case ACPI_TYPE_BUFFER:
106
107                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
108                         object, object->buffer.pointer));
109
110                 /* Free the actual buffer */
111
112                 if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
113                         /* But only if it is NOT a pointer into an ACPI table */
114
115                         obj_pointer = object->buffer.pointer;
116                 }
117                 break;
118
119
120         case ACPI_TYPE_PACKAGE:
121
122                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
123                         object->package.count));
124
125                 /*
126                  * Elements of the package are not handled here, they are deleted
127                  * separately
128                  */
129
130                 /* Free the (variable length) element pointer array */
131
132                 obj_pointer = object->package.elements;
133                 break;
134
135
136         case ACPI_TYPE_DEVICE:
137
138                 if (object->device.gpe_block) {
139                         (void) acpi_ev_delete_gpe_block (object->device.gpe_block);
140                 }
141
142                 /* Walk the handler list for this device */
143
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;
149                 }
150                 break;
151
152
153         case ACPI_TYPE_MUTEX:
154
155                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Mutex %p, Semaphore %p\n",
156                         object, object->mutex.semaphore));
157
158                 acpi_ex_unlink_mutex (object);
159                 (void) acpi_os_delete_semaphore (object->mutex.semaphore);
160                 break;
161
162
163         case ACPI_TYPE_EVENT:
164
165                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Event %p, Semaphore %p\n",
166                         object, object->event.semaphore));
167
168                 (void) acpi_os_delete_semaphore (object->event.semaphore);
169                 object->event.semaphore = NULL;
170                 break;
171
172
173         case ACPI_TYPE_METHOD:
174
175                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Method %p\n", object));
176
177                 /* Delete the method semaphore if it exists */
178
179                 if (object->method.semaphore) {
180                         (void) acpi_os_delete_semaphore (object->method.semaphore);
181                         object->method.semaphore = NULL;
182                 }
183                 break;
184
185
186         case ACPI_TYPE_REGION:
187
188                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Region %p\n", object));
189
190                 second_desc = acpi_ns_get_secondary_object (object);
191                 if (second_desc) {
192                         /*
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.
196                          */
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;
201                                 }
202
203                                 acpi_ut_remove_reference (handler_desc);
204                         }
205
206                         /* Now we can free the Extra object */
207
208                         acpi_ut_delete_object_desc (second_desc);
209                 }
210                 break;
211
212
213         case ACPI_TYPE_BUFFER_FIELD:
214
215                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "***** Buffer Field %p\n", object));
216
217                 second_desc = acpi_ns_get_secondary_object (object);
218                 if (second_desc) {
219                         acpi_ut_delete_object_desc (second_desc);
220                 }
221                 break;
222
223
224         default:
225                 break;
226         }
227
228         /* Free any allocated memory (pointer within the object) found above */
229
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);
234         }
235
236         /* Now the object can be safely deleted */
237
238         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
239                         object, acpi_ut_get_object_type_name (object)));
240
241         acpi_ut_delete_object_desc (object);
242         return_VOID;
243 }
244
245
246 /*******************************************************************************
247  *
248  * FUNCTION:    acpi_ut_delete_internal_object_list
249  *
250  * PARAMETERS:  *obj_list       - Pointer to the list to be deleted
251  *
252  * RETURN:      None
253  *
254  * DESCRIPTION: This function deletes an internal object list, including both
255  *              simple objects and package objects
256  *
257  ******************************************************************************/
258
259 void
260 acpi_ut_delete_internal_object_list (
261         union acpi_operand_object       **obj_list)
262 {
263         union acpi_operand_object       **internal_obj;
264
265
266         ACPI_FUNCTION_TRACE ("ut_delete_internal_object_list");
267
268
269         /* Walk the null-terminated internal list */
270
271         for (internal_obj = obj_list; *internal_obj; internal_obj++) {
272                 acpi_ut_remove_reference (*internal_obj);
273         }
274
275         /* Free the combined parameter pointer list and object array */
276
277         ACPI_MEM_FREE (obj_list);
278         return_VOID;
279 }
280
281
282 /*******************************************************************************
283  *
284  * FUNCTION:    acpi_ut_update_ref_count
285  *
286  * PARAMETERS:  *Object         - Object whose ref count is to be updated
287  *              Action          - What to do
288  *
289  * RETURN:      New ref count
290  *
291  * DESCRIPTION: Modify the ref count and return it.
292  *
293  ******************************************************************************/
294
295 static void
296 acpi_ut_update_ref_count (
297         union acpi_operand_object       *object,
298         u32                             action)
299 {
300         u16                             count;
301         u16                             new_count;
302
303
304         ACPI_FUNCTION_NAME ("ut_update_ref_count");
305
306
307         if (!object) {
308                 return;
309         }
310
311         count = object->common.reference_count;
312         new_count = count;
313
314         /*
315          * Perform the reference count action (increment, decrement, or force delete)
316          */
317         switch (action) {
318
319         case REF_INCREMENT:
320
321                 new_count++;
322                 object->common.reference_count = new_count;
323
324                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, [Incremented]\n",
325                         object, new_count));
326                 break;
327
328
329         case REF_DECREMENT:
330
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));
334
335                         new_count = 0;
336                 }
337                 else {
338                         new_count--;
339
340                         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, [Decremented]\n",
341                                 object, new_count));
342                 }
343
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));
347                 }
348
349                 object->common.reference_count = new_count;
350                 if (new_count == 0) {
351                         acpi_ut_delete_internal_obj (object);
352                 }
353
354                 break;
355
356
357         case REF_FORCE_DELETE:
358
359                 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Obj %p Refs=%X, Force delete! (Set to 0)\n",
360                         object, count));
361
362                 new_count = 0;
363                 object->common.reference_count = new_count;
364                 acpi_ut_delete_internal_obj (object);
365                 break;
366
367
368         default:
369
370                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown action (%X)\n", action));
371                 break;
372         }
373
374         /*
375          * Sanity check the reference count, for debug purposes only.
376          * (A deleted object will have a huge reference count)
377          */
378         if (count > ACPI_MAX_REFERENCE_COUNT) {
379
380                 ACPI_DEBUG_PRINT ((ACPI_DB_WARN,
381                         "**** Warning **** Large Reference Count (%X) in object %p\n\n",
382                         count, object));
383         }
384
385         return;
386 }
387
388
389 /*******************************************************************************
390  *
391  * FUNCTION:    acpi_ut_update_object_reference
392  *
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
397  *
398  * RETURN:      Status
399  *
400  * DESCRIPTION: Increment the object reference count
401  *
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)
405  *
406  * Object references are decremented when:
407  * 1) An object is detached from an Node
408  *
409  ******************************************************************************/
410
411 acpi_status
412 acpi_ut_update_object_reference (
413         union acpi_operand_object       *object,
414         u16                             action)
415 {
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;
421
422         ACPI_FUNCTION_TRACE_PTR ("ut_update_object_reference", object);
423
424
425         /* Ignore a null object ptr */
426
427         if (!object) {
428                 return_ACPI_STATUS (AE_OK);
429         }
430
431         /* Make sure that this isn't a namespace handle */
432
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);
436         }
437
438         state = acpi_ut_create_update_state (object, action);
439
440         while (state) {
441                 object = state->update.object;
442                 action = state->update.value;
443                 acpi_ut_delete_generic_state (state);
444
445                 /*
446                  * All sub-objects must have their reference count incremented also.
447                  * Different object types have different subobjects.
448                  */
449                 switch (ACPI_GET_OBJECT_TYPE (object)) {
450                 case ACPI_TYPE_DEVICE:
451
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);
456
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);
461
462                         break;
463
464
465                 case ACPI_TYPE_PACKAGE:
466
467                         /*
468                          * We must update all the sub-objects of the package
469                          * (Each of whom may have their own sub-objects, etc.
470                          */
471                         for (i = 0; i < object->package.count; i++) {
472                                 /*
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
476                                  */
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;
481                                 }
482
483                                 tmp = object->package.elements[i];
484                                 if (tmp && (tmp->common.reference_count <= 1)  && action == REF_DECREMENT)
485                                         object->package.elements[i] = NULL;
486                         }
487                         break;
488
489
490                 case ACPI_TYPE_BUFFER_FIELD:
491
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;
496                         }
497
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;
502
503
504                 case ACPI_TYPE_LOCAL_REGION_FIELD:
505
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;
510                         }
511
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;
516
517
518                 case ACPI_TYPE_LOCAL_BANK_FIELD:
519
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;
524                         }
525
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;
529
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;
534                         }
535
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;
540
541
542                 case ACPI_TYPE_LOCAL_INDEX_FIELD:
543
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;
548                         }
549
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;
553
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;
558                         }
559
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;
564
565
566                 case ACPI_TYPE_LOCAL_REFERENCE:
567
568                         /*
569                          * The target of an Index (a package, string, or buffer) must track
570                          * changes to the ref count of the index.
571                          */
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;
577                                 }
578                         }
579                         break;
580
581
582                 case ACPI_TYPE_REGION:
583                 default:
584
585                         /* No subobjects */
586                         break;
587                 }
588
589                 /*
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.
593                  */
594                 acpi_ut_update_ref_count (object, action);
595
596                 /* Move on to the next object to be updated */
597
598                 state = acpi_ut_pop_generic_state (&state_list);
599         }
600
601         return_ACPI_STATUS (AE_OK);
602
603
604 error_exit:
605
606         ACPI_REPORT_ERROR (("Could not update object reference count, %s\n",
607                 acpi_format_exception (status)));
608
609         return_ACPI_STATUS (status);
610 }
611
612
613 /*******************************************************************************
614  *
615  * FUNCTION:    acpi_ut_add_reference
616  *
617  * PARAMETERS:  *Object        - Object whose reference count is to be
618  *                                  incremented
619  *
620  * RETURN:      None
621  *
622  * DESCRIPTION: Add one reference to an ACPI object
623  *
624  ******************************************************************************/
625
626 void
627 acpi_ut_add_reference (
628         union acpi_operand_object       *object)
629 {
630
631         ACPI_FUNCTION_TRACE_PTR ("ut_add_reference", object);
632
633
634         /* Ensure that we have a valid object */
635
636         if (!acpi_ut_valid_internal_object (object)) {
637                 return_VOID;
638         }
639
640         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
641                 "Obj %p Current Refs=%X [To Be Incremented]\n",
642                 object, object->common.reference_count));
643
644         /* Increment the reference count */
645
646         (void) acpi_ut_update_object_reference (object, REF_INCREMENT);
647         return_VOID;
648 }
649
650
651 /*******************************************************************************
652  *
653  * FUNCTION:    acpi_ut_remove_reference
654  *
655  * PARAMETERS:  *Object        - Object whose ref count will be decremented
656  *
657  * RETURN:      None
658  *
659  * DESCRIPTION: Decrement the reference count of an ACPI internal object
660  *
661  ******************************************************************************/
662
663 void
664 acpi_ut_remove_reference (
665         union acpi_operand_object       *object)
666 {
667
668         ACPI_FUNCTION_TRACE_PTR ("ut_remove_reference", object);
669
670
671         /*
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.
674          *
675          */
676         if (!object ||
677                 (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED)) {
678                 return_VOID;
679         }
680
681         /* Ensure that we have a valid object */
682
683         if (!acpi_ut_valid_internal_object (object)) {
684                 return_VOID;
685         }
686
687         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
688                 "Obj %p Current Refs=%X [To Be Decremented]\n",
689                 object, object->common.reference_count));
690
691         /*
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!)
695          */
696         (void) acpi_ut_update_object_reference (object, REF_DECREMENT);
697         return_VOID;
698 }
699
700