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