[ACPI] Error: Invalid owner_id: 00
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / dispatcher / dsmethod.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
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
1da177e4
LT
44#include <acpi/acpi.h>
45#include <acpi/acparser.h>
46#include <acpi/amlcode.h>
47#include <acpi/acdispat.h>
48#include <acpi/acinterp.h>
49#include <acpi/acnamesp.h>
50
1da177e4 51#define _COMPONENT ACPI_DISPATCHER
4be44fcd 52ACPI_MODULE_NAME("dsmethod")
1da177e4
LT
53
54/*******************************************************************************
55 *
56 * FUNCTION: acpi_ds_parse_method
57 *
0c9938cc 58 * PARAMETERS: Node - Method node
1da177e4
LT
59 *
60 * RETURN: Status
61 *
0c9938cc 62 * DESCRIPTION: Parse the AML that is associated with the method.
1da177e4
LT
63 *
64 * MUTEX: Assumes parser is locked
65 *
66 ******************************************************************************/
4be44fcd 67acpi_status acpi_ds_parse_method(struct acpi_namespace_node *node)
1da177e4 68{
4be44fcd
LB
69 acpi_status status;
70 union acpi_operand_object *obj_desc;
71 union acpi_parse_object *op;
72 struct acpi_walk_state *walk_state;
1da177e4 73
4be44fcd 74 ACPI_FUNCTION_TRACE_PTR("ds_parse_method", node);
1da177e4
LT
75
76 /* Parameter Validation */
77
0c9938cc 78 if (!node) {
4be44fcd 79 return_ACPI_STATUS(AE_NULL_ENTRY);
1da177e4
LT
80 }
81
4be44fcd
LB
82 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
83 "**** Parsing [%4.4s] **** named_obj=%p\n",
84 acpi_ut_get_node_name(node), node));
1da177e4
LT
85
86 /* Extract the method object from the method Node */
87
4be44fcd 88 obj_desc = acpi_ns_get_attached_object(node);
1da177e4 89 if (!obj_desc) {
4be44fcd 90 return_ACPI_STATUS(AE_NULL_OBJECT);
1da177e4
LT
91 }
92
93 /* Create a mutex for the method if there is a concurrency limit */
94
95 if ((obj_desc->method.concurrency != ACPI_INFINITE_CONCURRENCY) &&
4be44fcd
LB
96 (!obj_desc->method.semaphore)) {
97 status = acpi_os_create_semaphore(obj_desc->method.concurrency,
98 obj_desc->method.concurrency,
99 &obj_desc->method.semaphore);
100 if (ACPI_FAILURE(status)) {
101 return_ACPI_STATUS(status);
1da177e4
LT
102 }
103 }
104
105 /*
106 * Allocate a new parser op to be the root of the parsed
107 * method tree
108 */
4be44fcd 109 op = acpi_ps_alloc_op(AML_METHOD_OP);
1da177e4 110 if (!op) {
4be44fcd 111 return_ACPI_STATUS(AE_NO_MEMORY);
1da177e4
LT
112 }
113
114 /* Init new op with the method name and pointer back to the Node */
115
4be44fcd 116 acpi_ps_set_name(op, node->name.integer);
1da177e4
LT
117 op->common.node = node;
118
119 /*
120 * Get a new owner_id for objects created by this method. Namespace
121 * objects (such as Operation Regions) can be created during the
122 * first pass parse.
123 */
4be44fcd
LB
124 status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
125 if (ACPI_FAILURE(status)) {
f9f4601f
RM
126 goto cleanup;
127 }
1da177e4
LT
128
129 /* Create and initialize a new walk state */
130
4be44fcd
LB
131 walk_state =
132 acpi_ds_create_walk_state(obj_desc->method.owner_id, NULL, NULL,
133 NULL);
1da177e4 134 if (!walk_state) {
88ac00f5 135 status = AE_NO_MEMORY;
f9f4601f 136 goto cleanup2;
1da177e4
LT
137 }
138
4be44fcd
LB
139 status = acpi_ds_init_aml_walk(walk_state, op, node,
140 obj_desc->method.aml_start,
141 obj_desc->method.aml_length, NULL, 1);
142 if (ACPI_FAILURE(status)) {
143 acpi_ds_delete_walk_state(walk_state);
f9f4601f 144 goto cleanup2;
1da177e4
LT
145 }
146
147 /*
148 * Parse the method, first pass
149 *
44f6c012
RM
150 * The first pass load is where newly declared named objects are added into
151 * the namespace. Actual evaluation of the named objects (what would be
152 * called a "second pass") happens during the actual execution of the
153 * method so that operands to the named objects can take on dynamic
154 * run-time values.
1da177e4 155 */
4be44fcd
LB
156 status = acpi_ps_parse_aml(walk_state);
157 if (ACPI_FAILURE(status)) {
f9f4601f 158 goto cleanup2;
1da177e4
LT
159 }
160
4be44fcd
LB
161 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
162 "**** [%4.4s] Parsed **** named_obj=%p Op=%p\n",
163 acpi_ut_get_node_name(node), node, op));
0c9938cc
RM
164
165 /*
166 * Delete the parse tree. We simply re-parse the method for every
167 * execution since there isn't much overhead (compared to keeping lots
168 * of parse trees around)
169 */
4be44fcd
LB
170 acpi_ns_delete_namespace_subtree(node);
171 acpi_ns_delete_namespace_by_owner(obj_desc->method.owner_id);
1da177e4 172
4be44fcd
LB
173 cleanup2:
174 acpi_ut_release_owner_id(&obj_desc->method.owner_id);
f9f4601f 175
4be44fcd
LB
176 cleanup:
177 acpi_ps_delete_parse_tree(op);
178 return_ACPI_STATUS(status);
1da177e4
LT
179}
180
1da177e4
LT
181/*******************************************************************************
182 *
183 * FUNCTION: acpi_ds_begin_method_execution
184 *
185 * PARAMETERS: method_node - Node of the method
186 * obj_desc - The method object
187 * calling_method_node - Caller of this method (if non-null)
188 *
189 * RETURN: Status
190 *
191 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
192 * increments the thread count, and waits at the method semaphore
193 * for clearance to execute.
194 *
195 ******************************************************************************/
196
197acpi_status
4be44fcd
LB
198acpi_ds_begin_method_execution(struct acpi_namespace_node *method_node,
199 union acpi_operand_object *obj_desc,
200 struct acpi_namespace_node *calling_method_node)
1da177e4 201{
4be44fcd 202 acpi_status status = AE_OK;
1da177e4 203
4be44fcd 204 ACPI_FUNCTION_TRACE_PTR("ds_begin_method_execution", method_node);
1da177e4
LT
205
206 if (!method_node) {
4be44fcd 207 return_ACPI_STATUS(AE_NULL_ENTRY);
1da177e4
LT
208 }
209
210 /*
211 * If there is a concurrency limit on this method, we need to
212 * obtain a unit from the method semaphore.
213 */
214 if (obj_desc->method.semaphore) {
215 /*
216 * Allow recursive method calls, up to the reentrancy/concurrency
217 * limit imposed by the SERIALIZED rule and the sync_level method
218 * parameter.
219 *
220 * The point of this code is to avoid permanently blocking a
221 * thread that is making recursive method calls.
222 */
223 if (method_node == calling_method_node) {
4be44fcd
LB
224 if (obj_desc->method.thread_count >=
225 obj_desc->method.concurrency) {
226 return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
1da177e4
LT
227 }
228 }
229
230 /*
231 * Get a unit from the method semaphore. This releases the
232 * interpreter if we block
233 */
4be44fcd
LB
234 status =
235 acpi_ex_system_wait_semaphore(obj_desc->method.semaphore,
236 ACPI_WAIT_FOREVER);
1da177e4 237 }
8813dfbf
AS
238 /*
239 * allocate owner id for this method
240 */
241 if (!obj_desc->method.thread_count) {
242 status = acpi_ut_allocate_owner_id (&obj_desc->method.owner_id);
243 if (ACPI_FAILURE (status)) {
244 return_ACPI_STATUS (status);
245 }
246 }
247
1da177e4
LT
248
249 /*
250 * Increment the method parse tree thread count since it has been
251 * reentered one more time (even if it is the same thread)
252 */
253 obj_desc->method.thread_count++;
4be44fcd 254 return_ACPI_STATUS(status);
1da177e4
LT
255}
256
1da177e4
LT
257/*******************************************************************************
258 *
259 * FUNCTION: acpi_ds_call_control_method
260 *
261 * PARAMETERS: Thread - Info for this thread
262 * this_walk_state - Current walk state
263 * Op - Current Op to be walked
264 *
265 * RETURN: Status
266 *
267 * DESCRIPTION: Transfer execution to a called control method
268 *
269 ******************************************************************************/
270
271acpi_status
4be44fcd
LB
272acpi_ds_call_control_method(struct acpi_thread_state *thread,
273 struct acpi_walk_state *this_walk_state,
274 union acpi_parse_object *op)
1da177e4 275{
4be44fcd
LB
276 acpi_status status;
277 struct acpi_namespace_node *method_node;
278 struct acpi_walk_state *next_walk_state = NULL;
279 union acpi_operand_object *obj_desc;
280 struct acpi_parameter_info info;
281 u32 i;
1da177e4 282
4be44fcd 283 ACPI_FUNCTION_TRACE_PTR("ds_call_control_method", this_walk_state);
1da177e4 284
4be44fcd
LB
285 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
286 "Execute method %p, currentstate=%p\n",
287 this_walk_state->prev_op, this_walk_state));
1da177e4
LT
288
289 /*
290 * Get the namespace entry for the control method we are about to call
291 */
292 method_node = this_walk_state->method_call_node;
293 if (!method_node) {
4be44fcd 294 return_ACPI_STATUS(AE_NULL_ENTRY);
1da177e4
LT
295 }
296
4be44fcd 297 obj_desc = acpi_ns_get_attached_object(method_node);
1da177e4 298 if (!obj_desc) {
4be44fcd 299 return_ACPI_STATUS(AE_NULL_OBJECT);
1da177e4
LT
300 }
301
1da177e4
LT
302 /* Init for new method, wait on concurrency semaphore */
303
4be44fcd
LB
304 status = acpi_ds_begin_method_execution(method_node, obj_desc,
305 this_walk_state->method_node);
306 if (ACPI_FAILURE(status)) {
f9f4601f 307 goto cleanup;
1da177e4
LT
308 }
309
310 if (!(obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY)) {
311 /* 1) Parse: Create a new walk state for the preempting walk */
312
4be44fcd
LB
313 next_walk_state =
314 acpi_ds_create_walk_state(obj_desc->method.owner_id, op,
315 obj_desc, NULL);
1da177e4 316 if (!next_walk_state) {
4be44fcd 317 return_ACPI_STATUS(AE_NO_MEMORY);
1da177e4
LT
318 }
319
320 /* Create and init a Root Node */
321
4be44fcd 322 op = acpi_ps_create_scope_op();
1da177e4
LT
323 if (!op) {
324 status = AE_NO_MEMORY;
325 goto cleanup;
326 }
327
4be44fcd
LB
328 status = acpi_ds_init_aml_walk(next_walk_state, op, method_node,
329 obj_desc->method.aml_start,
330 obj_desc->method.aml_length,
331 NULL, 1);
332 if (ACPI_FAILURE(status)) {
333 acpi_ds_delete_walk_state(next_walk_state);
1da177e4
LT
334 goto cleanup;
335 }
336
337 /* Begin AML parse */
338
4be44fcd
LB
339 status = acpi_ps_parse_aml(next_walk_state);
340 acpi_ps_delete_parse_tree(op);
1da177e4
LT
341 }
342
343 /* 2) Execute: Create a new state for the preempting walk */
344
4be44fcd
LB
345 next_walk_state = acpi_ds_create_walk_state(obj_desc->method.owner_id,
346 NULL, obj_desc, thread);
1da177e4
LT
347 if (!next_walk_state) {
348 status = AE_NO_MEMORY;
349 goto cleanup;
350 }
351 /*
352 * The resolved arguments were put on the previous walk state's operand
353 * stack. Operands on the previous walk state stack always
354 * start at index 0.
355 * Null terminate the list of arguments
356 */
4be44fcd 357 this_walk_state->operands[this_walk_state->num_operands] = NULL;
1da177e4
LT
358
359 info.parameters = &this_walk_state->operands[0];
360 info.parameter_type = ACPI_PARAM_ARGS;
361
4be44fcd
LB
362 status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node,
363 obj_desc->method.aml_start,
364 obj_desc->method.aml_length, &info, 3);
365 if (ACPI_FAILURE(status)) {
1da177e4
LT
366 goto cleanup;
367 }
368
369 /*
370 * Delete the operands on the previous walkstate operand stack
371 * (they were copied to new objects)
372 */
373 for (i = 0; i < obj_desc->method.param_count; i++) {
4be44fcd
LB
374 acpi_ut_remove_reference(this_walk_state->operands[i]);
375 this_walk_state->operands[i] = NULL;
1da177e4
LT
376 }
377
378 /* Clear the operand stack */
379
380 this_walk_state->num_operands = 0;
381
4be44fcd
LB
382 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
383 "Starting nested execution, newstate=%p\n",
384 next_walk_state));
1da177e4
LT
385
386 if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
4be44fcd 387 status = obj_desc->method.implementation(next_walk_state);
1da177e4 388 }
8813dfbf 389 goto end;
1da177e4 390
8813dfbf
AS
391cleanup:
392 /* Decrement the thread count on the method parse tree */
1da177e4 393 if (next_walk_state && (next_walk_state->method_desc)) {
4be44fcd 394 next_walk_state->method_desc->method.thread_count--;
1da177e4 395 }
8813dfbf
AS
396 /* On error, we must delete the new walk state */
397 acpi_ds_terminate_control_method (next_walk_state);
398 acpi_ds_delete_walk_state (next_walk_state);
399end:
4be44fcd 400 return_ACPI_STATUS(status);
1da177e4
LT
401}
402
1da177e4
LT
403/*******************************************************************************
404 *
405 * FUNCTION: acpi_ds_restart_control_method
406 *
407 * PARAMETERS: walk_state - State for preempted method (caller)
408 * return_desc - Return value from the called method
409 *
410 * RETURN: Status
411 *
412 * DESCRIPTION: Restart a method that was preempted by another (nested) method
413 * invocation. Handle the return value (if any) from the callee.
414 *
415 ******************************************************************************/
416
417acpi_status
4be44fcd
LB
418acpi_ds_restart_control_method(struct acpi_walk_state *walk_state,
419 union acpi_operand_object *return_desc)
1da177e4 420{
4be44fcd 421 acpi_status status;
1da177e4 422
4be44fcd 423 ACPI_FUNCTION_TRACE_PTR("ds_restart_control_method", walk_state);
1da177e4 424
4be44fcd
LB
425 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
426 "****Restart [%4.4s] Op %p return_value_from_callee %p\n",
427 (char *)&walk_state->method_node->name,
428 walk_state->method_call_op, return_desc));
1da177e4 429
4be44fcd
LB
430 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
431 " return_from_this_method_used?=%X res_stack %p Walk %p\n",
432 walk_state->return_used,
433 walk_state->results, walk_state));
1da177e4
LT
434
435 /* Did the called method return a value? */
436
437 if (return_desc) {
438 /* Are we actually going to use the return value? */
439
440 if (walk_state->return_used) {
441 /* Save the return value from the previous method */
442
4be44fcd
LB
443 status = acpi_ds_result_push(return_desc, walk_state);
444 if (ACPI_FAILURE(status)) {
445 acpi_ut_remove_reference(return_desc);
446 return_ACPI_STATUS(status);
1da177e4
LT
447 }
448
449 /*
450 * Save as THIS method's return value in case it is returned
451 * immediately to yet another method
452 */
453 walk_state->return_desc = return_desc;
454 }
455
456 /*
457 * The following code is the
458 * optional support for a so-called "implicit return". Some AML code
459 * assumes that the last value of the method is "implicitly" returned
460 * to the caller. Just save the last result as the return value.
461 * NOTE: this is optional because the ASL language does not actually
462 * support this behavior.
463 */
4be44fcd
LB
464 else if (!acpi_ds_do_implicit_return
465 (return_desc, walk_state, FALSE)) {
1da177e4
LT
466 /*
467 * Delete the return value if it will not be used by the
468 * calling method
469 */
4be44fcd 470 acpi_ut_remove_reference(return_desc);
1da177e4
LT
471 }
472 }
473
4be44fcd 474 return_ACPI_STATUS(AE_OK);
1da177e4
LT
475}
476
1da177e4
LT
477/*******************************************************************************
478 *
479 * FUNCTION: acpi_ds_terminate_control_method
480 *
481 * PARAMETERS: walk_state - State of the method
482 *
8813dfbf 483 * RETURN: None
1da177e4
LT
484 *
485 * DESCRIPTION: Terminate a control method. Delete everything that the method
486 * created, delete all locals and arguments, and delete the parse
487 * tree if requested.
488 *
489 ******************************************************************************/
490
8813dfbf 491void acpi_ds_terminate_control_method(struct acpi_walk_state *walk_state)
1da177e4 492{
4be44fcd
LB
493 union acpi_operand_object *obj_desc;
494 struct acpi_namespace_node *method_node;
495 acpi_status status;
1da177e4 496
4be44fcd 497 ACPI_FUNCTION_TRACE_PTR("ds_terminate_control_method", walk_state);
1da177e4
LT
498
499 if (!walk_state) {
8813dfbf 500 return_VOID;
1da177e4
LT
501 }
502
503 /* The current method object was saved in the walk state */
504
505 obj_desc = walk_state->method_desc;
506 if (!obj_desc) {
8813dfbf 507 return_VOID;
1da177e4
LT
508 }
509
510 /* Delete all arguments and locals */
511
4be44fcd 512 acpi_ds_method_data_delete_all(walk_state);
1da177e4
LT
513
514 /*
515 * Lock the parser while we terminate this method.
516 * If this is the last thread executing the method,
517 * we have additional cleanup to perform
518 */
4be44fcd
LB
519 status = acpi_ut_acquire_mutex(ACPI_MTX_PARSER);
520 if (ACPI_FAILURE(status)) {
8813dfbf 521 return_VOID;
1da177e4
LT
522 }
523
524 /* Signal completion of the execution of this method if necessary */
525
526 if (walk_state->method_desc->method.semaphore) {
4be44fcd
LB
527 status =
528 acpi_os_signal_semaphore(walk_state->method_desc->method.
529 semaphore, 1);
530 if (ACPI_FAILURE(status)) {
531 ACPI_REPORT_ERROR(("Could not signal method semaphore\n"));
1da177e4
LT
532 status = AE_OK;
533
534 /* Ignore error and continue cleanup */
535 }
536 }
537
538 if (walk_state->method_desc->method.thread_count) {
4be44fcd
LB
539 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
540 "*** Not deleting method namespace, there are still %d threads\n",
541 walk_state->method_desc->method.
542 thread_count));
1da177e4
LT
543 }
544
545 if (!walk_state->method_desc->method.thread_count) {
546 /*
547 * Support to dynamically change a method from not_serialized to
548 * Serialized if it appears that the method is written foolishly and
549 * does not support multiple thread execution. The best example of this
550 * is if such a method creates namespace objects and blocks. A second
551 * thread will fail with an AE_ALREADY_EXISTS exception
552 *
553 * This code is here because we must wait until the last thread exits
554 * before creating the synchronization semaphore.
555 */
556 if ((walk_state->method_desc->method.concurrency == 1) &&
4be44fcd
LB
557 (!walk_state->method_desc->method.semaphore)) {
558 status = acpi_os_create_semaphore(1, 1,
559 &walk_state->
560 method_desc->method.
561 semaphore);
1da177e4
LT
562 }
563
564 /*
565 * There are no more threads executing this method. Perform
566 * additional cleanup.
567 *
568 * The method Node is stored in the walk state
569 */
570 method_node = walk_state->method_node;
571
572 /*
573 * Delete any namespace entries created immediately underneath
574 * the method
575 */
4be44fcd
LB
576 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
577 if (ACPI_FAILURE(status)) {
8813dfbf 578 goto cleanup;
1da177e4
LT
579 }
580
581 if (method_node->child) {
4be44fcd 582 acpi_ns_delete_namespace_subtree(method_node);
1da177e4
LT
583 }
584
585 /*
586 * Delete any namespace entries created anywhere else within
587 * the namespace
588 */
4be44fcd
LB
589 acpi_ns_delete_namespace_by_owner(walk_state->method_desc->
590 method.owner_id);
591 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
592 acpi_ut_release_owner_id(&walk_state->method_desc->method.
593 owner_id);
594
595 if (ACPI_FAILURE(status)) {
8813dfbf 596 goto cleanup;
1da177e4
LT
597 }
598 }
8813dfbf
AS
599cleanup:
600 acpi_ut_release_mutex (ACPI_MTX_PARSER);
1da177e4 601}