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