vserver 1.9.3
[linux-2.6.git] / drivers / acpi / executer / exoparg2.c
1 /******************************************************************************
2  *
3  * Module Name: exoparg2 - AML execution - opcodes with 2 arguments
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2004, 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/acinterp.h>
48 #include <acpi/acevents.h>
49 #include <acpi/amlcode.h>
50
51
52 #define _COMPONENT          ACPI_EXECUTER
53          ACPI_MODULE_NAME    ("exoparg2")
54
55
56 /*!
57  * Naming convention for AML interpreter execution routines.
58  *
59  * The routines that begin execution of AML opcodes are named with a common
60  * convention based upon the number of arguments, the number of target operands,
61  * and whether or not a value is returned:
62  *
63  *      AcpiExOpcode_xA_yT_zR
64  *
65  * Where:
66  *
67  * xA - ARGUMENTS:    The number of arguments (input operands) that are
68  *                    required for this opcode type (1 through 6 args).
69  * yT - TARGETS:      The number of targets (output operands) that are required
70  *                    for this opcode type (0, 1, or 2 targets).
71  * zR - RETURN VALUE: Indicates whether this opcode type returns a value
72  *                    as the function return (0 or 1).
73  *
74  * The AcpiExOpcode* functions are called via the Dispatcher component with
75  * fully resolved operands.
76 !*/
77
78
79 /*******************************************************************************
80  *
81  * FUNCTION:    acpi_ex_opcode_2A_0T_0R
82  *
83  * PARAMETERS:  walk_state          - Current walk state
84  *
85  * RETURN:      Status
86  *
87  * DESCRIPTION: Execute opcode with two arguments, no target, and no return
88  *              value.
89  *
90  * ALLOCATION:  Deletes both operands
91  *
92  ******************************************************************************/
93
94 acpi_status
95 acpi_ex_opcode_2A_0T_0R (
96         struct acpi_walk_state          *walk_state)
97 {
98         union acpi_operand_object       **operand = &walk_state->operands[0];
99         struct acpi_namespace_node      *node;
100         u32                             value;
101         acpi_status                     status = AE_OK;
102
103
104         ACPI_FUNCTION_TRACE_STR ("ex_opcode_2A_0T_0R",
105                         acpi_ps_get_opcode_name (walk_state->opcode));
106
107
108         /* Examine the opcode */
109
110         switch (walk_state->opcode) {
111         case AML_NOTIFY_OP:         /* Notify (notify_object, notify_value) */
112
113                 /* The first operand is a namespace node */
114
115                 node = (struct acpi_namespace_node *) operand[0];
116
117                 /* Second value is the notify value */
118
119                 value = (u32) operand[1]->integer.value;
120
121                 /* Notifies allowed on this object? */
122
123                 if (!acpi_ev_is_notify_object (node)) {
124                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
125                                         "Unexpected notify object type [%s]\n",
126                                         acpi_ut_get_type_name (node->type)));
127
128                         status = AE_AML_OPERAND_TYPE;
129                         break;
130                 }
131
132 #ifdef ACPI_GPE_NOTIFY_CHECK
133                 /*
134                  * GPE method wake/notify check.  Here, we want to ensure that we
135                  * don't receive any "device_wake" Notifies from a GPE _Lxx or _Exx
136                  * GPE method during system runtime.  If we do, the GPE is marked
137                  * as "wake-only" and disabled.
138                  *
139                  * 1) Is the Notify() value == device_wake?
140                  * 2) Is this a GPE deferred method?  (An _Lxx or _Exx method)
141                  * 3) Did the original GPE happen at system runtime?
142                  *    (versus during wake)
143                  *
144                  * If all three cases are true, this is a wake-only GPE that should
145                  * be disabled at runtime.
146                  */
147                 if (value == 2)     /* device_wake */ {
148                         status = acpi_ev_check_for_wake_only_gpe (walk_state->gpe_event_info);
149                         if (ACPI_FAILURE (status)) {
150                                 /* AE_WAKE_ONLY_GPE only error, means ignore this notify */
151
152                                 return_ACPI_STATUS (AE_OK)
153                         }
154                 }
155 #endif
156
157                 /*
158                  * Dispatch the notify to the appropriate handler
159                  * NOTE: the request is queued for execution after this method
160                  * completes.  The notify handlers are NOT invoked synchronously
161                  * from this thread -- because handlers may in turn run other
162                  * control methods.
163                  */
164                 status = acpi_ev_queue_notify_request (node, value);
165                 break;
166
167
168         default:
169
170                 ACPI_REPORT_ERROR (("acpi_ex_opcode_2A_0T_0R: Unknown opcode %X\n",
171                                 walk_state->opcode));
172                 status = AE_AML_BAD_OPCODE;
173         }
174
175         return_ACPI_STATUS (status);
176 }
177
178
179 /*******************************************************************************
180  *
181  * FUNCTION:    acpi_ex_opcode_2A_2T_1R
182  *
183  * PARAMETERS:  walk_state          - Current walk state
184  *
185  * RETURN:      Status
186  *
187  * DESCRIPTION: Execute a dyadic operator (2 operands) with 2 output targets
188  *              and one implicit return value.
189  *
190  ******************************************************************************/
191
192 acpi_status
193 acpi_ex_opcode_2A_2T_1R (
194         struct acpi_walk_state          *walk_state)
195 {
196         union acpi_operand_object       **operand = &walk_state->operands[0];
197         union acpi_operand_object       *return_desc1 = NULL;
198         union acpi_operand_object       *return_desc2 = NULL;
199         acpi_status                     status;
200
201
202         ACPI_FUNCTION_TRACE_STR ("ex_opcode_2A_2T_1R", acpi_ps_get_opcode_name (walk_state->opcode));
203
204
205         /*
206          * Execute the opcode
207          */
208         switch (walk_state->opcode) {
209         case AML_DIVIDE_OP:             /* Divide (Dividend, Divisor, remainder_result quotient_result) */
210
211                 return_desc1 = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
212                 if (!return_desc1) {
213                         status = AE_NO_MEMORY;
214                         goto cleanup;
215                 }
216
217                 return_desc2 = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
218                 if (!return_desc2) {
219                         status = AE_NO_MEMORY;
220                         goto cleanup;
221                 }
222
223                 /* Quotient to return_desc1, remainder to return_desc2 */
224
225                 status = acpi_ut_divide (&operand[0]->integer.value, &operand[1]->integer.value,
226                                    &return_desc1->integer.value, &return_desc2->integer.value);
227                 if (ACPI_FAILURE (status)) {
228                         goto cleanup;
229                 }
230                 break;
231
232
233         default:
234
235                 ACPI_REPORT_ERROR (("acpi_ex_opcode_2A_2T_1R: Unknown opcode %X\n",
236                                 walk_state->opcode));
237                 status = AE_AML_BAD_OPCODE;
238                 goto cleanup;
239         }
240
241
242         /* Store the results to the target reference operands */
243
244         status = acpi_ex_store (return_desc2, operand[2], walk_state);
245         if (ACPI_FAILURE (status)) {
246                 goto cleanup;
247         }
248
249         status = acpi_ex_store (return_desc1, operand[3], walk_state);
250         if (ACPI_FAILURE (status)) {
251                 goto cleanup;
252         }
253
254         /* Return the remainder */
255
256         walk_state->result_obj = return_desc1;
257
258
259 cleanup:
260         /*
261          * Since the remainder is not returned indirectly, remove a reference to
262          * it. Only the quotient is returned indirectly.
263          */
264         acpi_ut_remove_reference (return_desc2);
265
266         if (ACPI_FAILURE (status)) {
267                 /* Delete the return object */
268
269                 acpi_ut_remove_reference (return_desc1);
270         }
271
272         return_ACPI_STATUS (status);
273 }
274
275
276 /*******************************************************************************
277  *
278  * FUNCTION:    acpi_ex_opcode_2A_1T_1R
279  *
280  * PARAMETERS:  walk_state          - Current walk state
281  *
282  * RETURN:      Status
283  *
284  * DESCRIPTION: Execute opcode with two arguments, one target, and a return
285  *              value.
286  *
287  ******************************************************************************/
288
289 acpi_status
290 acpi_ex_opcode_2A_1T_1R (
291         struct acpi_walk_state          *walk_state)
292 {
293         union acpi_operand_object       **operand = &walk_state->operands[0];
294         union acpi_operand_object       *return_desc = NULL;
295         union acpi_operand_object       *temp_desc = NULL;
296         u32                             index;
297         acpi_status                     status = AE_OK;
298         acpi_size                       length;
299
300
301         ACPI_FUNCTION_TRACE_STR ("ex_opcode_2A_1T_1R", acpi_ps_get_opcode_name (walk_state->opcode));
302
303
304         /*
305          * Execute the opcode
306          */
307         if (walk_state->op_info->flags & AML_MATH) {
308                 /* All simple math opcodes (add, etc.) */
309
310                 return_desc = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
311                 if (!return_desc) {
312                         status = AE_NO_MEMORY;
313                         goto cleanup;
314                 }
315
316                 return_desc->integer.value = acpi_ex_do_math_op (walk_state->opcode,
317                                   operand[0]->integer.value,
318                                   operand[1]->integer.value);
319                 goto store_result_to_target;
320         }
321
322
323         switch (walk_state->opcode) {
324         case AML_MOD_OP:                /* Mod (Dividend, Divisor, remainder_result (ACPI 2.0) */
325
326                 return_desc = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
327                 if (!return_desc) {
328                         status = AE_NO_MEMORY;
329                         goto cleanup;
330                 }
331
332                 /* return_desc will contain the remainder */
333
334                 status = acpi_ut_divide (&operand[0]->integer.value, &operand[1]->integer.value,
335                                   NULL, &return_desc->integer.value);
336                 break;
337
338
339         case AML_CONCAT_OP:             /* Concatenate (Data1, Data2, Result) */
340
341                 /*
342                  * Convert the second operand if necessary.  The first operand
343                  * determines the type of the second operand, (See the Data Types
344                  * section of the ACPI specification.)  Both object types are
345                  * guaranteed to be either Integer/String/Buffer by the operand
346                  * resolution mechanism above.
347                  */
348                 switch (ACPI_GET_OBJECT_TYPE (operand[0])) {
349                 case ACPI_TYPE_INTEGER:
350                         status = acpi_ex_convert_to_integer (operand[1], &temp_desc, walk_state);
351                         break;
352
353                 case ACPI_TYPE_STRING:
354                         status = acpi_ex_convert_to_string (operand[1], &temp_desc, 16, ACPI_UINT32_MAX, walk_state);
355                         break;
356
357                 case ACPI_TYPE_BUFFER:
358                         status = acpi_ex_convert_to_buffer (operand[1], &temp_desc, walk_state);
359                         break;
360
361                 default:
362                         ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
363                                         ACPI_GET_OBJECT_TYPE (operand[0])));
364                         status = AE_AML_INTERNAL;
365                 }
366
367                 if (ACPI_FAILURE (status)) {
368                         goto cleanup;
369                 }
370
371                 /*
372                  * Both operands are now known to be the same object type
373                  * (Both are Integer, String, or Buffer), and we can now perform the
374                  * concatenation.
375                  */
376                 status = acpi_ex_do_concatenate (operand[0], temp_desc, &return_desc, walk_state);
377                 if (temp_desc != operand[1]) {
378                         acpi_ut_remove_reference (temp_desc);
379                 }
380                 break;
381
382
383         case AML_TO_STRING_OP:          /* to_string (Buffer, Length, Result) (ACPI 2.0) */
384
385                 /*
386                  * Input object is guaranteed to be a buffer at this point (it may have
387                  * been converted.)  Copy the raw buffer data to a new object of type String.
388                  */
389
390                 /* Get the length of the new string */
391
392                 length = 0;
393                 if (operand[1]->integer.value == 0) {
394                         /* Handle optional length value */
395
396                         operand[1]->integer.value = ACPI_INTEGER_MAX;
397                 }
398
399                 while ((length < operand[0]->buffer.length) &&
400                            (length < operand[1]->integer.value) &&
401                            (operand[0]->buffer.pointer[length])) {
402                         length++;
403                 }
404
405                 if (length > ACPI_MAX_STRING_CONVERSION) {
406                         status = AE_AML_STRING_LIMIT;
407                         goto cleanup;
408                 }
409
410                 /* Create the internal return object */
411
412                 return_desc = acpi_ut_create_internal_object (ACPI_TYPE_STRING);
413                 if (!return_desc) {
414                         status = AE_NO_MEMORY;
415                         goto cleanup;
416                 }
417
418                 /* Allocate a new string buffer (Length + 1 for null terminator) */
419
420                 return_desc->string.pointer = ACPI_MEM_CALLOCATE (length + 1);
421                 if (!return_desc->string.pointer) {
422                         status = AE_NO_MEMORY;
423                         goto cleanup;
424                 }
425
426                 /* Copy the raw buffer data with no transform */
427
428                 ACPI_MEMCPY (return_desc->string.pointer, operand[0]->buffer.pointer, length);
429
430                 /* Set the string length */
431
432                 return_desc->string.length = (u32) length;
433                 break;
434
435
436         case AML_CONCAT_RES_OP:         /* concatenate_res_template (Buffer, Buffer, Result) (ACPI 2.0) */
437
438                 status = acpi_ex_concat_template (operand[0], operand[1], &return_desc, walk_state);
439                 break;
440
441
442         case AML_INDEX_OP:              /* Index (Source Index Result) */
443
444                 /* Create the internal return object */
445
446                 return_desc = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_REFERENCE);
447                 if (!return_desc) {
448                         status = AE_NO_MEMORY;
449                         goto cleanup;
450                 }
451
452                 index = (u32) operand[1]->integer.value;
453
454                 /*
455                  * At this point, the Source operand is either a Package or a Buffer
456                  */
457                 if (ACPI_GET_OBJECT_TYPE (operand[0]) == ACPI_TYPE_PACKAGE) {
458                         /* Object to be indexed is a Package */
459
460                         if (index >= operand[0]->package.count) {
461                                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Index value (%X) beyond package end (%X)\n",
462                                         index, operand[0]->package.count));
463                                 status = AE_AML_PACKAGE_LIMIT;
464                                 goto cleanup;
465                         }
466
467                         return_desc->reference.target_type = ACPI_TYPE_PACKAGE;
468                         return_desc->reference.object    = operand[0];
469                         return_desc->reference.where     = &operand[0]->package.elements [index];
470                 }
471                 else {
472                         /* Object to be indexed is a Buffer */
473
474                         if (index >= operand[0]->buffer.length) {
475                                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Index value (%X) beyond end of buffer (%X)\n",
476                                         index, operand[0]->buffer.length));
477                                 status = AE_AML_BUFFER_LIMIT;
478                                 goto cleanup;
479                         }
480
481                         return_desc->reference.target_type = ACPI_TYPE_BUFFER_FIELD;
482                         return_desc->reference.object    = operand[0];
483                 }
484
485                 /* Complete the Index reference object */
486
487                 return_desc->reference.opcode    = AML_INDEX_OP;
488                 return_desc->reference.offset    = index;
489
490                 /* Store the reference to the Target */
491
492                 status = acpi_ex_store (return_desc, operand[2], walk_state);
493
494                 /* Return the reference */
495
496                 walk_state->result_obj = return_desc;
497                 goto cleanup;
498
499
500         default:
501
502                 ACPI_REPORT_ERROR (("acpi_ex_opcode_2A_1T_1R: Unknown opcode %X\n",
503                                 walk_state->opcode));
504                 status = AE_AML_BAD_OPCODE;
505                 break;
506         }
507
508
509 store_result_to_target:
510
511         if (ACPI_SUCCESS (status)) {
512                 /*
513                  * Store the result of the operation (which is now in return_desc) into
514                  * the Target descriptor.
515                  */
516                 status = acpi_ex_store (return_desc, operand[2], walk_state);
517                 if (ACPI_FAILURE (status)) {
518                         goto cleanup;
519                 }
520
521                 if (!walk_state->result_obj) {
522                         walk_state->result_obj = return_desc;
523                 }
524         }
525
526
527 cleanup:
528
529         /* Delete return object on error */
530
531         if (ACPI_FAILURE (status)) {
532                 acpi_ut_remove_reference (return_desc);
533         }
534
535         return_ACPI_STATUS (status);
536 }
537
538
539 /*******************************************************************************
540  *
541  * FUNCTION:    acpi_ex_opcode_2A_0T_1R
542  *
543  * PARAMETERS:  walk_state          - Current walk state
544  *
545  * RETURN:      Status
546  *
547  * DESCRIPTION: Execute opcode with 2 arguments, no target, and a return value
548  *
549  ******************************************************************************/
550
551 acpi_status
552 acpi_ex_opcode_2A_0T_1R (
553         struct acpi_walk_state          *walk_state)
554 {
555         union acpi_operand_object       **operand = &walk_state->operands[0];
556         union acpi_operand_object       *return_desc = NULL;
557         acpi_status                     status = AE_OK;
558         u8                              logical_result = FALSE;
559
560
561         ACPI_FUNCTION_TRACE_STR ("ex_opcode_2A_0T_1R", acpi_ps_get_opcode_name (walk_state->opcode));
562
563
564         /* Create the internal return object */
565
566         return_desc = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
567         if (!return_desc) {
568                 status = AE_NO_MEMORY;
569                 goto cleanup;
570         }
571
572         /*
573          * Execute the Opcode
574          */
575         if (walk_state->op_info->flags & AML_LOGICAL) /* logical_op (Operand0, Operand1) */ {
576                 /* Both operands must be of the same type */
577
578                 if (ACPI_GET_OBJECT_TYPE (operand[0]) !=
579                         ACPI_GET_OBJECT_TYPE (operand[1])) {
580                         status = AE_AML_OPERAND_TYPE;
581                         goto cleanup;
582                 }
583
584                 logical_result = acpi_ex_do_logical_op (walk_state->opcode,
585                                  operand[0],
586                                  operand[1]);
587                 goto store_logical_result;
588         }
589
590
591         switch (walk_state->opcode) {
592         case AML_ACQUIRE_OP:            /* Acquire (mutex_object, Timeout) */
593
594                 status = acpi_ex_acquire_mutex (operand[1], operand[0], walk_state);
595                 if (status == AE_TIME) {
596                         logical_result = TRUE;      /* TRUE = Acquire timed out */
597                         status = AE_OK;
598                 }
599                 break;
600
601
602         case AML_WAIT_OP:               /* Wait (event_object, Timeout) */
603
604                 status = acpi_ex_system_wait_event (operand[1], operand[0]);
605                 if (status == AE_TIME) {
606                         logical_result = TRUE;      /* TRUE, Wait timed out */
607                         status = AE_OK;
608                 }
609                 break;
610
611
612         default:
613
614                 ACPI_REPORT_ERROR (("acpi_ex_opcode_2A_0T_1R: Unknown opcode %X\n",
615                         walk_state->opcode));
616                 status = AE_AML_BAD_OPCODE;
617                 goto cleanup;
618         }
619
620
621 store_logical_result:
622         /*
623          * Set return value to according to logical_result. logical TRUE (all ones)
624          * Default is FALSE (zero)
625          */
626         if (logical_result) {
627                 return_desc->integer.value = ACPI_INTEGER_MAX;
628         }
629
630         walk_state->result_obj = return_desc;
631
632
633 cleanup:
634
635         /* Delete return object on error */
636
637         if (ACPI_FAILURE (status)) {
638                 acpi_ut_remove_reference (return_desc);
639         }
640
641         return_ACPI_STATUS (status);
642 }
643
644