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