- Make ACPI use PCH. Cuts down compile time to 9 seconds on gcc.
[reactos.git] / reactos / drivers / bus / acpi / dispatcher / dswexec.c
1 /******************************************************************************
2 *
3 * Module Name: dswexec - Dispatcher method execution callbacks;
4 * dispatch to interpreter.
5 * $Revision: 1.1 $
6 *
7 *****************************************************************************/
8
9 /*
10 * Copyright (C) 2000, 2001 R. Byron Moore
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27
28 #include <acpi.h>
29
30 #define _COMPONENT ACPI_DISPATCHER
31 MODULE_NAME ("dswexec")
32
33
34 /*****************************************************************************
35 *
36 * FUNCTION: Acpi_ds_get_predicate_value
37 *
38 * PARAMETERS: Walk_state - Current state of the parse tree walk
39 *
40 * RETURN: Status
41 *
42 * DESCRIPTION: Get the result of a predicate evaluation
43 *
44 ****************************************************************************/
45
46 ACPI_STATUS
47 acpi_ds_get_predicate_value (
48 ACPI_WALK_STATE *walk_state,
49 ACPI_PARSE_OBJECT *op,
50 u32 has_result_obj)
51 {
52 ACPI_STATUS status = AE_OK;
53 ACPI_OPERAND_OBJECT *obj_desc;
54
55
56 walk_state->control_state->common.state = 0;
57
58 if (has_result_obj) {
59 status = acpi_ds_result_pop (&obj_desc, walk_state);
60 if (ACPI_FAILURE (status)) {
61 return (status);
62 }
63 }
64
65 else {
66 status = acpi_ds_create_operand (walk_state, op, 0);
67 if (ACPI_FAILURE (status)) {
68 return (status);
69 }
70
71 status = acpi_aml_resolve_to_value (&walk_state->operands [0], walk_state);
72 if (ACPI_FAILURE (status)) {
73 return (status);
74 }
75
76 obj_desc = walk_state->operands [0];
77 }
78
79 if (!obj_desc) {
80 return (AE_AML_NO_OPERAND);
81 }
82
83
84 /*
85 * Result of predicate evaluation currently must
86 * be a number
87 */
88
89 if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
90 status = AE_AML_OPERAND_TYPE;
91 goto cleanup;
92 }
93
94
95 /* Truncate the predicate to 32-bits if necessary */
96
97 acpi_aml_truncate_for32bit_table (obj_desc, walk_state);
98
99 /*
100 * Save the result of the predicate evaluation on
101 * the control stack
102 */
103
104 if (obj_desc->integer.value) {
105 walk_state->control_state->common.value = TRUE;
106 }
107
108 else {
109 /*
110 * Predicate is FALSE, we will just toss the
111 * rest of the package
112 */
113
114 walk_state->control_state->common.value = FALSE;
115 status = AE_CTRL_FALSE;
116 }
117
118
119 cleanup:
120
121 /* Break to debugger to display result */
122
123 DEBUGGER_EXEC (acpi_db_display_result_object (obj_desc, walk_state));
124
125 /*
126 * Delete the predicate result object (we know that
127 * we don't need it anymore)
128 */
129
130 acpi_cm_remove_reference (obj_desc);
131
132 walk_state->control_state->common.state = CONTROL_NORMAL;
133
134 return (status);
135 }
136
137
138 /*****************************************************************************
139 *
140 * FUNCTION: Acpi_ds_exec_begin_op
141 *
142 * PARAMETERS: Walk_state - Current state of the parse tree walk
143 * Op - Op that has been just been reached in the
144 * walk; Arguments have not been evaluated yet.
145 *
146 * RETURN: Status
147 *
148 * DESCRIPTION: Descending callback used during the execution of control
149 * methods. This is where most operators and operands are
150 * dispatched to the interpreter.
151 *
152 ****************************************************************************/
153
154 ACPI_STATUS
155 acpi_ds_exec_begin_op (
156 u16 opcode,
157 ACPI_PARSE_OBJECT *op,
158 ACPI_WALK_STATE *walk_state,
159 ACPI_PARSE_OBJECT **out_op)
160 {
161 ACPI_OPCODE_INFO *op_info;
162 ACPI_STATUS status = AE_OK;
163
164
165 if (!op) {
166 status = acpi_ds_load2_begin_op (opcode, NULL, walk_state, out_op);
167 if (ACPI_FAILURE (status)) {
168 return (status);
169 }
170
171 op = *out_op;
172 }
173
174 if (op == walk_state->origin) {
175 if (out_op) {
176 *out_op = op;
177 }
178
179 return (AE_OK);
180 }
181
182 /*
183 * If the previous opcode was a conditional, this opcode
184 * must be the beginning of the associated predicate.
185 * Save this knowledge in the current scope descriptor
186 */
187
188 if ((walk_state->control_state) &&
189 (walk_state->control_state->common.state ==
190 CONTROL_CONDITIONAL_EXECUTING)) {
191 walk_state->control_state->common.state = CONTROL_PREDICATE_EXECUTING;
192
193 /* Save start of predicate */
194
195 walk_state->control_state->control.predicate_op = op;
196 }
197
198
199 op_info = acpi_ps_get_opcode_info (op->opcode);
200
201 /* We want to send namepaths to the load code */
202
203 if (op->opcode == AML_NAMEPATH_OP) {
204 op_info->flags = OPTYPE_NAMED_OBJECT;
205 }
206
207
208 /*
209 * Handle the opcode based upon the opcode type
210 */
211
212 switch (ACPI_GET_OP_CLASS (op_info)) {
213 case OPTYPE_CONTROL:
214
215 status = acpi_ds_result_stack_push (walk_state);
216 if (ACPI_FAILURE (status)) {
217 return (status);
218 }
219
220 status = acpi_ds_exec_begin_control_op (walk_state, op);
221 break;
222
223
224 case OPTYPE_NAMED_OBJECT:
225
226 if (walk_state->walk_type == WALK_METHOD) {
227 /*
228 * Found a named object declaration during method
229 * execution; we must enter this object into the
230 * namespace. The created object is temporary and
231 * will be deleted upon completion of the execution
232 * of this method.
233 */
234
235 status = acpi_ds_load2_begin_op (op->opcode, op, walk_state, NULL);
236 }
237
238
239 if (op->opcode == AML_REGION_OP) {
240 status = acpi_ds_result_stack_push (walk_state);
241 }
242
243 break;
244
245
246 /* most operators with arguments */
247
248 case OPTYPE_MONADIC1:
249 case OPTYPE_DYADIC1:
250 case OPTYPE_MONADIC2:
251 case OPTYPE_MONADIC2_r:
252 case OPTYPE_DYADIC2:
253 case OPTYPE_DYADIC2_r:
254 case OPTYPE_DYADIC2_s:
255 case OPTYPE_RECONFIGURATION:
256 case OPTYPE_INDEX:
257 case OPTYPE_MATCH:
258 case OPTYPE_FATAL:
259 case OPTYPE_CREATE_FIELD:
260
261 /* Start a new result/operand state */
262
263 status = acpi_ds_result_stack_push (walk_state);
264 break;
265
266
267 default:
268 break;
269 }
270
271 /* Nothing to do here during method execution */
272
273 return (status);
274 }
275
276
277 /*****************************************************************************
278 *
279 * FUNCTION: Acpi_ds_exec_end_op
280 *
281 * PARAMETERS: Walk_state - Current state of the parse tree walk
282 * Op - Op that has been just been completed in the
283 * walk; Arguments have now been evaluated.
284 *
285 * RETURN: Status
286 *
287 * DESCRIPTION: Ascending callback used during the execution of control
288 * methods. The only thing we really need to do here is to
289 * notice the beginning of IF, ELSE, and WHILE blocks.
290 *
291 ****************************************************************************/
292
293 ACPI_STATUS
294 acpi_ds_exec_end_op (
295 ACPI_WALK_STATE *walk_state,
296 ACPI_PARSE_OBJECT *op)
297 {
298 ACPI_STATUS status = AE_OK;
299 u16 opcode;
300 u8 optype;
301 ACPI_PARSE_OBJECT *next_op;
302 ACPI_NAMESPACE_NODE *node;
303 ACPI_PARSE_OBJECT *first_arg;
304 ACPI_OPERAND_OBJECT *result_obj = NULL;
305 ACPI_OPCODE_INFO *op_info;
306 u32 operand_index;
307
308
309 opcode = (u16) op->opcode;
310
311
312 op_info = acpi_ps_get_opcode_info (op->opcode);
313 if (ACPI_GET_OP_TYPE (op_info) != ACPI_OP_TYPE_OPCODE) {
314 return (AE_NOT_IMPLEMENTED);
315 }
316
317 optype = (u8) ACPI_GET_OP_CLASS (op_info);
318 first_arg = op->value.arg;
319
320 /* Init the walk state */
321
322 walk_state->num_operands = 0;
323 walk_state->return_desc = NULL;
324 walk_state->op_info = op_info;
325 walk_state->opcode = opcode;
326
327
328 /* Call debugger for single step support (DEBUG build only) */
329
330 DEBUGGER_EXEC (status = acpi_db_single_step (walk_state, op, optype));
331 DEBUGGER_EXEC (if (ACPI_FAILURE (status)) {return (status);});
332
333
334 /* Decode the opcode */
335
336 switch (optype) {
337 case OPTYPE_UNDEFINED:
338
339 return (AE_NOT_IMPLEMENTED);
340 break;
341
342
343 case OPTYPE_BOGUS:
344 break;
345
346 case OPTYPE_CONSTANT: /* argument type only */
347 case OPTYPE_LITERAL: /* argument type only */
348 case OPTYPE_DATA_TERM: /* argument type only */
349 case OPTYPE_LOCAL_VARIABLE: /* argument type only */
350 case OPTYPE_METHOD_ARGUMENT: /* argument type only */
351 break;
352
353
354 /* most operators with arguments */
355
356 case OPTYPE_MONADIC1:
357 case OPTYPE_DYADIC1:
358 case OPTYPE_MONADIC2:
359 case OPTYPE_MONADIC2_r:
360 case OPTYPE_DYADIC2:
361 case OPTYPE_DYADIC2_r:
362 case OPTYPE_DYADIC2_s:
363 case OPTYPE_RECONFIGURATION:
364 case OPTYPE_INDEX:
365 case OPTYPE_MATCH:
366 case OPTYPE_FATAL:
367
368
369 /* Build resolved operand stack */
370
371 status = acpi_ds_create_operands (walk_state, first_arg);
372 if (ACPI_FAILURE (status)) {
373 goto cleanup;
374 }
375
376 operand_index = walk_state->num_operands - 1;
377
378
379 /* Done with this result state (Now that operand stack is built) */
380
381 status = acpi_ds_result_stack_pop (walk_state);
382 if (ACPI_FAILURE (status)) {
383 goto cleanup;
384 }
385
386 switch (optype) {
387 case OPTYPE_MONADIC1:
388
389 /* 1 Operand, 0 External_result, 0 Internal_result */
390
391 status = acpi_aml_exec_monadic1 (opcode, walk_state);
392 break;
393
394
395 case OPTYPE_MONADIC2:
396
397 /* 1 Operand, 0 External_result, 1 Internal_result */
398
399 status = acpi_aml_exec_monadic2 (opcode, walk_state, &result_obj);
400 break;
401
402
403 case OPTYPE_MONADIC2_r:
404
405 /* 1 Operand, 1 External_result, 1 Internal_result */
406
407 status = acpi_aml_exec_monadic2_r (opcode, walk_state, &result_obj);
408 break;
409
410
411 case OPTYPE_DYADIC1:
412
413 /* 2 Operands, 0 External_result, 0 Internal_result */
414
415 status = acpi_aml_exec_dyadic1 (opcode, walk_state);
416 break;
417
418
419 case OPTYPE_DYADIC2:
420
421 /* 2 Operands, 0 External_result, 1 Internal_result */
422
423 status = acpi_aml_exec_dyadic2 (opcode, walk_state, &result_obj);
424 break;
425
426
427 case OPTYPE_DYADIC2_r:
428
429 /* 2 Operands, 1 or 2 External_results, 1 Internal_result */
430
431 status = acpi_aml_exec_dyadic2_r (opcode, walk_state, &result_obj);
432 break;
433
434
435 case OPTYPE_DYADIC2_s: /* Synchronization Operator */
436
437 /* 2 Operands, 0 External_result, 1 Internal_result */
438
439 status = acpi_aml_exec_dyadic2_s (opcode, walk_state, &result_obj);
440 break;
441
442
443 case OPTYPE_INDEX: /* Type 2 opcode with 3 operands */
444
445 /* 3 Operands, 1 External_result, 1 Internal_result */
446
447 status = acpi_aml_exec_index (walk_state, &result_obj);
448 break;
449
450
451 case OPTYPE_MATCH: /* Type 2 opcode with 6 operands */
452
453 /* 6 Operands, 0 External_result, 1 Internal_result */
454
455 status = acpi_aml_exec_match (walk_state, &result_obj);
456 break;
457
458
459 case OPTYPE_RECONFIGURATION:
460
461 /* 1 or 2 operands, 0 Internal Result */
462
463 status = acpi_aml_exec_reconfiguration (opcode, walk_state);
464 break;
465
466
467 case OPTYPE_FATAL:
468
469 /* 3 Operands, 0 External_result, 0 Internal_result */
470
471 status = acpi_aml_exec_fatal (walk_state);
472 break;
473 }
474
475 /*
476 * If a result object was returned from above, push it on the
477 * current result stack
478 */
479 if (ACPI_SUCCESS (status) &&
480 result_obj) {
481 status = acpi_ds_result_push (result_obj, walk_state);
482 }
483
484 break;
485
486
487 case OPTYPE_CONTROL: /* Type 1 opcode, IF/ELSE/WHILE/NOOP */
488
489 /* 1 Operand, 0 External_result, 0 Internal_result */
490
491 status = acpi_ds_exec_end_control_op (walk_state, op);
492
493 acpi_ds_result_stack_pop (walk_state);
494 break;
495
496
497 case OPTYPE_METHOD_CALL:
498
499 /*
500 * (AML_METHODCALL) Op->Value->Arg->Node contains
501 * the method Node pointer
502 */
503 /* Next_op points to the op that holds the method name */
504
505 next_op = first_arg;
506 node = next_op->node;
507
508 /* Next_op points to first argument op */
509
510 next_op = next_op->next;
511
512 /*
513 * Get the method's arguments and put them on the operand stack
514 */
515 status = acpi_ds_create_operands (walk_state, next_op);
516 if (ACPI_FAILURE (status)) {
517 break;
518 }
519
520 /*
521 * Since the operands will be passed to another
522 * control method, we must resolve all local
523 * references here (Local variables, arguments
524 * to *this* method, etc.)
525 */
526
527 status = acpi_ds_resolve_operands (walk_state);
528 if (ACPI_FAILURE (status)) {
529 break;
530 }
531
532 /*
533 * Tell the walk loop to preempt this running method and
534 * execute the new method
535 */
536 status = AE_CTRL_TRANSFER;
537
538 /*
539 * Return now; we don't want to disturb anything,
540 * especially the operand count!
541 */
542 return (status);
543 break;
544
545
546 case OPTYPE_CREATE_FIELD:
547
548 status = acpi_ds_load2_end_op (walk_state, op);
549 if (ACPI_FAILURE (status)) {
550 break;
551 }
552
553 status = acpi_ds_eval_field_unit_operands (walk_state, op);
554 break;
555
556
557 case OPTYPE_NAMED_OBJECT:
558
559 status = acpi_ds_load2_end_op (walk_state, op);
560 if (ACPI_FAILURE (status)) {
561 break;
562 }
563
564 switch (op->opcode) {
565 case AML_REGION_OP:
566
567 status = acpi_ds_eval_region_operands (walk_state, op);
568 if (ACPI_FAILURE (status)) {
569 break;
570 }
571
572 status = acpi_ds_result_stack_pop (walk_state);
573 break;
574
575
576 case AML_METHOD_OP:
577 break;
578
579
580 case AML_ALIAS_OP:
581
582 /* Alias creation was already handled by call
583 to psxload above */
584 break;
585
586
587 default:
588 /* Nothing needs to be done */
589
590 status = AE_OK;
591 break;
592 }
593
594 break;
595
596 default:
597
598 status = AE_NOT_IMPLEMENTED;
599 break;
600 }
601
602
603 /*
604 * ACPI 2.0 support for 64-bit integers:
605 * Truncate numeric result value if we are executing from a 32-bit ACPI table
606 */
607 acpi_aml_truncate_for32bit_table (result_obj, walk_state);
608
609 /*
610 * Check if we just completed the evaluation of a
611 * conditional predicate
612 */
613
614 if ((walk_state->control_state) &&
615 (walk_state->control_state->common.state ==
616 CONTROL_PREDICATE_EXECUTING) &&
617 (walk_state->control_state->control.predicate_op == op)) {
618 status = acpi_ds_get_predicate_value (walk_state, op, (u32) result_obj);
619 result_obj = NULL;
620 }
621
622
623 cleanup:
624 if (result_obj) {
625 /* Break to debugger to display result */
626
627 DEBUGGER_EXEC (acpi_db_display_result_object (result_obj, walk_state));
628
629 /*
630 * Delete the result op if and only if:
631 * Parent will not use the result -- such as any
632 * non-nested type2 op in a method (parent will be method)
633 */
634 acpi_ds_delete_result_if_not_used (op, result_obj, walk_state);
635 }
636
637 /* Always clear the object stack */
638
639 /* TBD: [Investigate] Clear stack of return value,
640 but don't delete it */
641 walk_state->num_operands = 0;
642
643 return (status);
644 }
645
646