]> err.no Git - linux-2.6/blob - drivers/acpi/dispatcher/dsmethod.c
c9d9a6c45ae3f89066dade9fb356f1c01ceed44f
[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                 status = AE_NO_MEMORY;
143                 goto cleanup;
144         }
145
146         status = acpi_ds_init_aml_walk (walk_state, op, node,
147                           obj_desc->method.aml_start,
148                           obj_desc->method.aml_length, NULL, 1);
149         if (ACPI_FAILURE (status)) {
150                 acpi_ds_delete_walk_state (walk_state);
151                 goto cleanup;
152         }
153
154         /*
155          * Parse the method, first pass
156          *
157          * The first pass load is where newly declared named objects are added into
158          * the namespace.  Actual evaluation of the named objects (what would be
159          * called a "second pass") happens during the actual execution of the
160          * method so that operands to the named objects can take on dynamic
161          * run-time values.
162          */
163         status = acpi_ps_parse_aml (walk_state);
164         if (ACPI_FAILURE (status)) {
165                 goto cleanup;
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 cleanup:
173         acpi_ps_delete_parse_tree (op);
174         return_ACPI_STATUS (status);
175 }
176
177
178 /*******************************************************************************
179  *
180  * FUNCTION:    acpi_ds_begin_method_execution
181  *
182  * PARAMETERS:  method_node         - Node of the method
183  *              obj_desc            - The method object
184  *              calling_method_node - Caller of this method (if non-null)
185  *
186  * RETURN:      Status
187  *
188  * DESCRIPTION: Prepare a method for execution.  Parses the method if necessary,
189  *              increments the thread count, and waits at the method semaphore
190  *              for clearance to execute.
191  *
192  ******************************************************************************/
193
194 acpi_status
195 acpi_ds_begin_method_execution (
196         struct acpi_namespace_node      *method_node,
197         union acpi_operand_object       *obj_desc,
198         struct acpi_namespace_node      *calling_method_node)
199 {
200         acpi_status                     status = AE_OK;
201
202
203         ACPI_FUNCTION_TRACE_PTR ("ds_begin_method_execution", method_node);
204
205
206         if (!method_node) {
207                 return_ACPI_STATUS (AE_NULL_ENTRY);
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) {
224                         if (obj_desc->method.thread_count >= obj_desc->method.concurrency) {
225                                 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
226                         }
227                 }
228
229                 /*
230                  * Get a unit from the method semaphore. This releases the
231                  * interpreter if we block
232                  */
233                 status = acpi_ex_system_wait_semaphore (obj_desc->method.semaphore,
234                                  ACPI_WAIT_FOREVER);
235         }
236
237         /*
238          * Increment the method parse tree thread count since it has been
239          * reentered one more time (even if it is the same thread)
240          */
241         obj_desc->method.thread_count++;
242         return_ACPI_STATUS (status);
243 }
244
245
246 /*******************************************************************************
247  *
248  * FUNCTION:    acpi_ds_call_control_method
249  *
250  * PARAMETERS:  Thread              - Info for this thread
251  *              this_walk_state     - Current walk state
252  *              Op                  - Current Op to be walked
253  *
254  * RETURN:      Status
255  *
256  * DESCRIPTION: Transfer execution to a called control method
257  *
258  ******************************************************************************/
259
260 acpi_status
261 acpi_ds_call_control_method (
262         struct acpi_thread_state        *thread,
263         struct acpi_walk_state          *this_walk_state,
264         union acpi_parse_object         *op)
265 {
266         acpi_status                     status;
267         struct acpi_namespace_node      *method_node;
268         struct acpi_walk_state          *next_walk_state;
269         union acpi_operand_object       *obj_desc;
270         struct acpi_parameter_info      info;
271         u32                             i;
272
273
274         ACPI_FUNCTION_TRACE_PTR ("ds_call_control_method", this_walk_state);
275
276         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Execute method %p, currentstate=%p\n",
277                 this_walk_state->prev_op, this_walk_state));
278
279         /*
280          * Get the namespace entry for the control method we are about to call
281          */
282         method_node = this_walk_state->method_call_node;
283         if (!method_node) {
284                 return_ACPI_STATUS (AE_NULL_ENTRY);
285         }
286
287         obj_desc = acpi_ns_get_attached_object (method_node);
288         if (!obj_desc) {
289                 return_ACPI_STATUS (AE_NULL_OBJECT);
290         }
291
292         obj_desc->method.owning_id = acpi_ut_allocate_owner_id (ACPI_OWNER_TYPE_METHOD);
293
294         /* Init for new method, wait on concurrency semaphore */
295
296         status = acpi_ds_begin_method_execution (method_node, obj_desc,
297                           this_walk_state->method_node);
298         if (ACPI_FAILURE (status)) {
299                 return_ACPI_STATUS (status);
300         }
301
302         if (!(obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY)) {
303                 /* 1) Parse: Create a new walk state for the preempting walk */
304
305                 next_walk_state = acpi_ds_create_walk_state (obj_desc->method.owning_id,
306                                   op, obj_desc, NULL);
307                 if (!next_walk_state) {
308                         return_ACPI_STATUS (AE_NO_MEMORY);
309                 }
310
311                 /* Create and init a Root Node */
312
313                 op = acpi_ps_create_scope_op ();
314                 if (!op) {
315                         status = AE_NO_MEMORY;
316                         goto cleanup;
317                 }
318
319                 status = acpi_ds_init_aml_walk (next_walk_state, op, method_node,
320                                   obj_desc->method.aml_start, obj_desc->method.aml_length,
321                                   NULL, 1);
322                 if (ACPI_FAILURE (status)) {
323                         acpi_ds_delete_walk_state (next_walk_state);
324                         goto cleanup;
325                 }
326
327                 /* Begin AML parse */
328
329                 status = acpi_ps_parse_aml (next_walk_state);
330                 acpi_ps_delete_parse_tree (op);
331         }
332
333         /* 2) Execute: Create a new state for the preempting walk */
334
335         next_walk_state = acpi_ds_create_walk_state (obj_desc->method.owning_id,
336                           NULL, obj_desc, thread);
337         if (!next_walk_state) {
338                 status = AE_NO_MEMORY;
339                 goto cleanup;
340         }
341         /*
342          * The resolved arguments were put on the previous walk state's operand
343          * stack.  Operands on the previous walk state stack always
344          * start at index 0.
345          * Null terminate the list of arguments
346          */
347         this_walk_state->operands [this_walk_state->num_operands] = NULL;
348
349         info.parameters = &this_walk_state->operands[0];
350         info.parameter_type = ACPI_PARAM_ARGS;
351
352         status = acpi_ds_init_aml_walk (next_walk_state, NULL, method_node,
353                           obj_desc->method.aml_start, obj_desc->method.aml_length,
354                           &info, 3);
355         if (ACPI_FAILURE (status)) {
356                 goto cleanup;
357         }
358
359         /*
360          * Delete the operands on the previous walkstate operand stack
361          * (they were copied to new objects)
362          */
363         for (i = 0; i < obj_desc->method.param_count; i++) {
364                 acpi_ut_remove_reference (this_walk_state->operands [i]);
365                 this_walk_state->operands [i] = NULL;
366         }
367
368         /* Clear the operand stack */
369
370         this_walk_state->num_operands = 0;
371
372         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
373                 "Starting nested execution, newstate=%p\n", next_walk_state));
374
375         if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
376                 status = obj_desc->method.implementation (next_walk_state);
377                 return_ACPI_STATUS (status);
378         }
379
380         return_ACPI_STATUS (AE_OK);
381
382
383         /* On error, we must delete the new walk state */
384
385 cleanup:
386         if (next_walk_state && (next_walk_state->method_desc)) {
387                 /* Decrement the thread count on the method parse tree */
388
389            next_walk_state->method_desc->method.thread_count--;
390         }
391         (void) acpi_ds_terminate_control_method (next_walk_state);
392         acpi_ds_delete_walk_state (next_walk_state);
393         return_ACPI_STATUS (status);
394 }
395
396
397 /*******************************************************************************
398  *
399  * FUNCTION:    acpi_ds_restart_control_method
400  *
401  * PARAMETERS:  walk_state          - State for preempted method (caller)
402  *              return_desc         - Return value from the called method
403  *
404  * RETURN:      Status
405  *
406  * DESCRIPTION: Restart a method that was preempted by another (nested) method
407  *              invocation.  Handle the return value (if any) from the callee.
408  *
409  ******************************************************************************/
410
411 acpi_status
412 acpi_ds_restart_control_method (
413         struct acpi_walk_state          *walk_state,
414         union acpi_operand_object       *return_desc)
415 {
416         acpi_status                     status;
417
418
419         ACPI_FUNCTION_TRACE_PTR ("ds_restart_control_method", walk_state);
420
421
422         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
423                 "****Restart [%4.4s] Op %p return_value_from_callee %p\n",
424                 (char *) &walk_state->method_node->name, walk_state->method_call_op,
425                 return_desc));
426
427         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
428                 "    return_from_this_method_used?=%X res_stack %p Walk %p\n",
429                 walk_state->return_used,
430                 walk_state->results, walk_state));
431
432         /* Did the called method return a value? */
433
434         if (return_desc) {
435                 /* Are we actually going to use the return value? */
436
437                 if (walk_state->return_used) {
438                         /* Save the return value from the previous method */
439
440                         status = acpi_ds_result_push (return_desc, walk_state);
441                         if (ACPI_FAILURE (status)) {
442                                 acpi_ut_remove_reference (return_desc);
443                                 return_ACPI_STATUS (status);
444                         }
445
446                         /*
447                          * Save as THIS method's return value in case it is returned
448                          * immediately to yet another method
449                          */
450                         walk_state->return_desc = return_desc;
451                 }
452
453                 /*
454                  * The following code is the
455                  * optional support for a so-called "implicit return". Some AML code
456                  * assumes that the last value of the method is "implicitly" returned
457                  * to the caller. Just save the last result as the return value.
458                  * NOTE: this is optional because the ASL language does not actually
459                  * support this behavior.
460                  */
461                 else if (!acpi_ds_do_implicit_return (return_desc, walk_state, FALSE)) {
462                         /*
463                          * Delete the return value if it will not be used by the
464                          * calling method
465                          */
466                         acpi_ut_remove_reference (return_desc);
467                 }
468         }
469
470         return_ACPI_STATUS (AE_OK);
471 }
472
473
474 /*******************************************************************************
475  *
476  * FUNCTION:    acpi_ds_terminate_control_method
477  *
478  * PARAMETERS:  walk_state          - State of the method
479  *
480  * RETURN:      Status
481  *
482  * DESCRIPTION: Terminate a control method.  Delete everything that the method
483  *              created, delete all locals and arguments, and delete the parse
484  *              tree if requested.
485  *
486  ******************************************************************************/
487
488 acpi_status
489 acpi_ds_terminate_control_method (
490         struct acpi_walk_state          *walk_state)
491 {
492         union acpi_operand_object       *obj_desc;
493         struct acpi_namespace_node      *method_node;
494         acpi_status                     status;
495
496
497         ACPI_FUNCTION_TRACE_PTR ("ds_terminate_control_method", walk_state);
498
499
500         if (!walk_state) {
501                 return (AE_BAD_PARAMETER);
502         }
503
504         /* The current method object was saved in the walk state */
505
506         obj_desc = walk_state->method_desc;
507         if (!obj_desc) {
508                 return_ACPI_STATUS (AE_OK);
509         }
510
511         /* Delete all arguments and locals */
512
513         acpi_ds_method_data_delete_all (walk_state);
514
515         /*
516          * Lock the parser while we terminate this method.
517          * If this is the last thread executing the method,
518          * we have additional cleanup to perform
519          */
520         status = acpi_ut_acquire_mutex (ACPI_MTX_PARSER);
521         if (ACPI_FAILURE (status)) {
522                 return_ACPI_STATUS (status);
523         }
524
525         /* Signal completion of the execution of this method if necessary */
526
527         if (walk_state->method_desc->method.semaphore) {
528                 status = acpi_os_signal_semaphore (
529                                   walk_state->method_desc->method.semaphore, 1);
530                 if (ACPI_FAILURE (status)) {
531                         ACPI_REPORT_ERROR (("Could not signal method semaphore\n"));
532                         status = AE_OK;
533
534                         /* Ignore error and continue cleanup */
535                 }
536         }
537
538         if (walk_state->method_desc->method.thread_count) {
539                 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
540                         "*** Not deleting method namespace, there are still %d threads\n",
541                         walk_state->method_desc->method.thread_count));
542         }
543
544         if (!walk_state->method_desc->method.thread_count) {
545                 /*
546                  * Support to dynamically change a method from not_serialized to
547                  * Serialized if it appears that the method is written foolishly and
548                  * does not support multiple thread execution.  The best example of this
549                  * is if such a method creates namespace objects and blocks.  A second
550                  * thread will fail with an AE_ALREADY_EXISTS exception
551                  *
552                  * This code is here because we must wait until the last thread exits
553                  * before creating the synchronization semaphore.
554                  */
555                 if ((walk_state->method_desc->method.concurrency == 1) &&
556                         (!walk_state->method_desc->method.semaphore)) {
557                         status = acpi_os_create_semaphore (1,
558                                          1,
559                                          &walk_state->method_desc->method.semaphore);
560                 }
561
562                 /*
563                  * There are no more threads executing this method.  Perform
564                  * additional cleanup.
565                  *
566                  * The method Node is stored in the walk state
567                  */
568                 method_node = walk_state->method_node;
569
570                 /*
571                  * Delete any namespace entries created immediately underneath
572                  * the method
573                  */
574                 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
575                 if (ACPI_FAILURE (status)) {
576                         return_ACPI_STATUS (status);
577                 }
578
579                 if (method_node->child) {
580                         acpi_ns_delete_namespace_subtree (method_node);
581                 }
582
583                 /*
584                  * Delete any namespace entries created anywhere else within
585                  * the namespace
586                  */
587                 acpi_ns_delete_namespace_by_owner (walk_state->method_desc->method.owning_id);
588                 status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
589                 if (ACPI_FAILURE (status)) {
590                         return_ACPI_STATUS (status);
591                 }
592         }
593
594         status = acpi_ut_release_mutex (ACPI_MTX_PARSER);
595         return_ACPI_STATUS (status);
596 }
597
598