]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/utilities/utdelete.c
[ACPI] revert R40 workaround
[mirror_ubuntu-zesty-kernel.git] / drivers / acpi / utilities / utdelete.c
CommitLineData
1da177e4
LT
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
44f6c012
RM
54/* Local prototypes */
55
56static void
57acpi_ut_delete_internal_obj (
58 union acpi_operand_object *object);
59
60static void
61acpi_ut_update_ref_count (
62 union acpi_operand_object *object,
63 u32 action);
64
1da177e4
LT
65
66/*******************************************************************************
67 *
68 * FUNCTION: acpi_ut_delete_internal_obj
69 *
44f6c012 70 * PARAMETERS: Object - Object to be deleted
1da177e4
LT
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
44f6c012 79static void
1da177e4
LT
80acpi_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
44f6c012
RM
166 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
167 "***** Mutex %p, Semaphore %p\n",
1da177e4
LT
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
44f6c012
RM
177 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
178 "***** Event %p, Semaphore %p\n",
1da177e4
LT
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
44f6c012
RM
188 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
189 "***** Method %p\n", object));
1da177e4
LT
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
44f6c012
RM
202 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
203 "***** Region %p\n", object));
1da177e4
LT
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
44f6c012
RM
230 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
231 "***** Buffer Field %p\n", object));
1da177e4
LT
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 *
44f6c012 266 * PARAMETERS: obj_list - Pointer to the list to be deleted
1da177e4
LT
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
275void
276acpi_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 *
44f6c012 302 * PARAMETERS: Object - Object whose ref count is to be updated
1da177e4
LT
303 * Action - What to do
304 *
305 * RETURN: New ref count
306 *
307 * DESCRIPTION: Modify the ref count and return it.
308 *
309 ******************************************************************************/
310
311static void
312acpi_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 /*
44f6c012
RM
331 * Perform the reference count action
332 * (increment, decrement, or force delete)
1da177e4
LT
333 */
334 switch (action) {
335
336 case REF_INCREMENT:
337
338 new_count++;
339 object->common.reference_count = new_count;
340
44f6c012
RM
341 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
342 "Obj %p Refs=%X, [Incremented]\n",
1da177e4
LT
343 object, new_count));
344 break;
345
346
347 case REF_DECREMENT:
348
349 if (count < 1) {
44f6c012
RM
350 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
351 "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
1da177e4
LT
352 object, new_count));
353
354 new_count = 0;
355 }
356 else {
357 new_count--;
358
44f6c012
RM
359 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
360 "Obj %p Refs=%X, [Decremented]\n",
1da177e4
LT
361 object, new_count));
362 }
363
364 if (ACPI_GET_OBJECT_TYPE (object) == ACPI_TYPE_METHOD) {
44f6c012
RM
365 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
366 "Method Obj %p Refs=%X, [Decremented]\n",
1da177e4
LT
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
44f6c012
RM
380 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
381 "Obj %p Refs=%X, Force delete! (Set to 0)\n",
1da177e4
LT
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 *
44f6c012 415 * PARAMETERS: Object - Increment ref count for this object
1da177e4
LT
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
433acpi_status
434acpi_ut_update_object_reference (
435 union acpi_operand_object *object,
436 u16 action)
437{
438 acpi_status status;
439 u32 i;
440 union acpi_generic_state *state_list = NULL;
441 union acpi_generic_state *state;
4c3ffbd7 442
1da177e4
LT
443
444 ACPI_FUNCTION_TRACE_PTR ("ut_update_object_reference", object);
445
446
447 /* Ignore a null object ptr */
448
449 if (!object) {
450 return_ACPI_STATUS (AE_OK);
451 }
452
453 /* Make sure that this isn't a namespace handle */
454
455 if (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) {
44f6c012
RM
456 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
457 "Object %p is NS handle\n", object));
1da177e4
LT
458 return_ACPI_STATUS (AE_OK);
459 }
460
461 state = acpi_ut_create_update_state (object, action);
462
463 while (state) {
464 object = state->update.object;
465 action = state->update.value;
466 acpi_ut_delete_generic_state (state);
467
468 /*
469 * All sub-objects must have their reference count incremented also.
470 * Different object types have different subobjects.
471 */
472 switch (ACPI_GET_OBJECT_TYPE (object)) {
473 case ACPI_TYPE_DEVICE:
474
4c3ffbd7
DSL
475 acpi_ut_update_ref_count (object->device.system_notify, action);
476 acpi_ut_update_ref_count (object->device.device_notify, action);
1da177e4
LT
477 break;
478
479
480 case ACPI_TYPE_PACKAGE:
481
482 /*
483 * We must update all the sub-objects of the package
484 * (Each of whom may have their own sub-objects, etc.
485 */
486 for (i = 0; i < object->package.count; i++) {
487 /*
488 * Push each element onto the stack for later processing.
489 * Note: There can be null elements within the package,
490 * these are simply ignored
491 */
492 status = acpi_ut_create_update_state_and_push (
493 object->package.elements[i], action, &state_list);
494 if (ACPI_FAILURE (status)) {
495 goto error_exit;
496 }
1da177e4
LT
497 }
498 break;
499
500
501 case ACPI_TYPE_BUFFER_FIELD:
502
503 status = acpi_ut_create_update_state_and_push (
504 object->buffer_field.buffer_obj, action, &state_list);
505 if (ACPI_FAILURE (status)) {
506 goto error_exit;
507 }
1da177e4
LT
508 break;
509
510
511 case ACPI_TYPE_LOCAL_REGION_FIELD:
512
513 status = acpi_ut_create_update_state_and_push (
514 object->field.region_obj, action, &state_list);
515 if (ACPI_FAILURE (status)) {
516 goto error_exit;
517 }
1da177e4
LT
518 break;
519
520
521 case ACPI_TYPE_LOCAL_BANK_FIELD:
522
523 status = acpi_ut_create_update_state_and_push (
524 object->bank_field.bank_obj, action, &state_list);
525 if (ACPI_FAILURE (status)) {
526 goto error_exit;
527 }
528
1da177e4
LT
529 status = acpi_ut_create_update_state_and_push (
530 object->bank_field.region_obj, action, &state_list);
531 if (ACPI_FAILURE (status)) {
532 goto error_exit;
533 }
1da177e4
LT
534 break;
535
536
537 case ACPI_TYPE_LOCAL_INDEX_FIELD:
538
539 status = acpi_ut_create_update_state_and_push (
540 object->index_field.index_obj, action, &state_list);
541 if (ACPI_FAILURE (status)) {
542 goto error_exit;
543 }
544
1da177e4
LT
545 status = acpi_ut_create_update_state_and_push (
546 object->index_field.data_obj, action, &state_list);
547 if (ACPI_FAILURE (status)) {
548 goto error_exit;
549 }
1da177e4
LT
550 break;
551
552
553 case ACPI_TYPE_LOCAL_REFERENCE:
554
555 /*
556 * The target of an Index (a package, string, or buffer) must track
557 * changes to the ref count of the index.
558 */
559 if (object->reference.opcode == AML_INDEX_OP) {
560 status = acpi_ut_create_update_state_and_push (
561 object->reference.object, action, &state_list);
562 if (ACPI_FAILURE (status)) {
563 goto error_exit;
564 }
565 }
566 break;
567
568
569 case ACPI_TYPE_REGION:
570 default:
571
572 /* No subobjects */
573 break;
574 }
575
576 /*
577 * Now we can update the count in the main object. This can only
578 * happen after we update the sub-objects in case this causes the
579 * main object to be deleted.
580 */
581 acpi_ut_update_ref_count (object, action);
582
583 /* Move on to the next object to be updated */
584
585 state = acpi_ut_pop_generic_state (&state_list);
586 }
587
588 return_ACPI_STATUS (AE_OK);
589
590
591error_exit:
592
593 ACPI_REPORT_ERROR (("Could not update object reference count, %s\n",
594 acpi_format_exception (status)));
595
596 return_ACPI_STATUS (status);
597}
598
599
600/*******************************************************************************
601 *
602 * FUNCTION: acpi_ut_add_reference
603 *
44f6c012
RM
604 * PARAMETERS: Object - Object whose reference count is to be
605 * incremented
1da177e4
LT
606 *
607 * RETURN: None
608 *
609 * DESCRIPTION: Add one reference to an ACPI object
610 *
611 ******************************************************************************/
612
613void
614acpi_ut_add_reference (
615 union acpi_operand_object *object)
616{
617
618 ACPI_FUNCTION_TRACE_PTR ("ut_add_reference", object);
619
620
621 /* Ensure that we have a valid object */
622
623 if (!acpi_ut_valid_internal_object (object)) {
624 return_VOID;
625 }
626
627 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
628 "Obj %p Current Refs=%X [To Be Incremented]\n",
629 object, object->common.reference_count));
630
631 /* Increment the reference count */
632
633 (void) acpi_ut_update_object_reference (object, REF_INCREMENT);
634 return_VOID;
635}
636
637
638/*******************************************************************************
639 *
640 * FUNCTION: acpi_ut_remove_reference
641 *
44f6c012 642 * PARAMETERS: Object - Object whose ref count will be decremented
1da177e4
LT
643 *
644 * RETURN: None
645 *
646 * DESCRIPTION: Decrement the reference count of an ACPI internal object
647 *
648 ******************************************************************************/
649
650void
651acpi_ut_remove_reference (
652 union acpi_operand_object *object)
653{
654
655 ACPI_FUNCTION_TRACE_PTR ("ut_remove_reference", object);
656
657
658 /*
659 * Allow a NULL pointer to be passed in, just ignore it. This saves
660 * each caller from having to check. Also, ignore NS nodes.
661 *
662 */
663 if (!object ||
664 (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED)) {
665 return_VOID;
666 }
667
668 /* Ensure that we have a valid object */
669
670 if (!acpi_ut_valid_internal_object (object)) {
671 return_VOID;
672 }
673
674 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
675 "Obj %p Current Refs=%X [To Be Decremented]\n",
676 object, object->common.reference_count));
677
678 /*
679 * Decrement the reference count, and only actually delete the object
680 * if the reference count becomes 0. (Must also decrement the ref count
681 * of all subobjects!)
682 */
683 (void) acpi_ut_update_object_reference (object, REF_DECREMENT);
684 return_VOID;
685}
686
687