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