Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / executer / exmisc.c
blobb542dcd58c074219224899193de30530ca0da829
2 /******************************************************************************
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
6 *****************************************************************************/
8 /*
9 * Copyright (C) 2000 - 2005, R. Byron Moore
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
48 #include <acpi/amlcode.h>
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("exmisc")
55 /*******************************************************************************
57 * FUNCTION: acpi_ex_get_object_reference
59 * PARAMETERS: obj_desc - Create a reference to this object
60 * return_desc - Where to store the reference
61 * walk_state - Current state
63 * RETURN: Status
65 * DESCRIPTION: Obtain and return a "reference" to the target object
66 * Common code for the ref_of_op and the cond_ref_of_op.
68 ******************************************************************************/
70 acpi_status
71 acpi_ex_get_object_reference (
72 union acpi_operand_object *obj_desc,
73 union acpi_operand_object **return_desc,
74 struct acpi_walk_state *walk_state)
76 union acpi_operand_object *reference_obj;
77 union acpi_operand_object *referenced_obj;
80 ACPI_FUNCTION_TRACE_PTR ("ex_get_object_reference", obj_desc);
83 *return_desc = NULL;
85 switch (ACPI_GET_DESCRIPTOR_TYPE (obj_desc)) {
86 case ACPI_DESC_TYPE_OPERAND:
88 if (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_LOCAL_REFERENCE) {
89 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
93 * Must be a reference to a Local or Arg
95 switch (obj_desc->reference.opcode) {
96 case AML_LOCAL_OP:
97 case AML_ARG_OP:
98 case AML_DEBUG_OP:
100 /* The referenced object is the pseudo-node for the local/arg */
102 referenced_obj = obj_desc->reference.object;
103 break;
105 default:
107 ACPI_REPORT_ERROR (("Unknown Reference opcode in get_reference %X\n",
108 obj_desc->reference.opcode));
109 return_ACPI_STATUS (AE_AML_INTERNAL);
111 break;
114 case ACPI_DESC_TYPE_NAMED:
117 * A named reference that has already been resolved to a Node
119 referenced_obj = obj_desc;
120 break;
123 default:
125 ACPI_REPORT_ERROR (("Invalid descriptor type in get_reference: %X\n",
126 ACPI_GET_DESCRIPTOR_TYPE (obj_desc)));
127 return_ACPI_STATUS (AE_TYPE);
131 /* Create a new reference object */
133 reference_obj = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_REFERENCE);
134 if (!reference_obj) {
135 return_ACPI_STATUS (AE_NO_MEMORY);
138 reference_obj->reference.opcode = AML_REF_OF_OP;
139 reference_obj->reference.object = referenced_obj;
140 *return_desc = reference_obj;
142 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p Type [%s], returning Reference %p\n",
143 obj_desc, acpi_ut_get_object_type_name (obj_desc), *return_desc));
145 return_ACPI_STATUS (AE_OK);
149 /*******************************************************************************
151 * FUNCTION: acpi_ex_concat_template
153 * PARAMETERS: Operand0 - First source object
154 * Operand1 - Second source object
155 * actual_return_desc - Where to place the return object
156 * walk_state - Current walk state
158 * RETURN: Status
160 * DESCRIPTION: Concatenate two resource templates
162 ******************************************************************************/
164 acpi_status
165 acpi_ex_concat_template (
166 union acpi_operand_object *operand0,
167 union acpi_operand_object *operand1,
168 union acpi_operand_object **actual_return_desc,
169 struct acpi_walk_state *walk_state)
171 union acpi_operand_object *return_desc;
172 u8 *new_buf;
173 u8 *end_tag1;
174 u8 *end_tag2;
175 acpi_size length1;
176 acpi_size length2;
179 ACPI_FUNCTION_TRACE ("ex_concat_template");
182 /* Find the end_tags in each resource template */
184 end_tag1 = acpi_ut_get_resource_end_tag (operand0);
185 end_tag2 = acpi_ut_get_resource_end_tag (operand1);
186 if (!end_tag1 || !end_tag2) {
187 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
190 /* Compute the length of each part */
192 length1 = ACPI_PTR_DIFF (end_tag1, operand0->buffer.pointer);
193 length2 = ACPI_PTR_DIFF (end_tag2, operand1->buffer.pointer) +
194 2; /* Size of END_TAG */
196 /* Create a new buffer object for the result */
198 return_desc = acpi_ut_create_buffer_object (length1 + length2);
199 if (!return_desc) {
200 return_ACPI_STATUS (AE_NO_MEMORY);
203 /* Copy the templates to the new descriptor */
205 new_buf = return_desc->buffer.pointer;
206 ACPI_MEMCPY (new_buf, operand0->buffer.pointer, length1);
207 ACPI_MEMCPY (new_buf + length1, operand1->buffer.pointer, length2);
209 /* Compute the new checksum */
211 new_buf[return_desc->buffer.length - 1] =
212 acpi_ut_generate_checksum (return_desc->buffer.pointer,
213 (return_desc->buffer.length - 1));
215 /* Return the completed template descriptor */
217 *actual_return_desc = return_desc;
218 return_ACPI_STATUS (AE_OK);
222 /*******************************************************************************
224 * FUNCTION: acpi_ex_do_concatenate
226 * PARAMETERS: Operand0 - First source object
227 * Operand1 - Second source object
228 * actual_return_desc - Where to place the return object
229 * walk_state - Current walk state
231 * RETURN: Status
233 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
235 ******************************************************************************/
237 acpi_status
238 acpi_ex_do_concatenate (
239 union acpi_operand_object *operand0,
240 union acpi_operand_object *operand1,
241 union acpi_operand_object **actual_return_desc,
242 struct acpi_walk_state *walk_state)
244 union acpi_operand_object *local_operand1 = operand1;
245 union acpi_operand_object *return_desc;
246 char *new_buf;
247 acpi_status status;
248 acpi_size new_length;
251 ACPI_FUNCTION_TRACE ("ex_do_concatenate");
255 * Convert the second operand if necessary. The first operand
256 * determines the type of the second operand, (See the Data Types
257 * section of the ACPI specification.) Both object types are
258 * guaranteed to be either Integer/String/Buffer by the operand
259 * resolution mechanism.
261 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
262 case ACPI_TYPE_INTEGER:
263 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
264 break;
266 case ACPI_TYPE_STRING:
267 status = acpi_ex_convert_to_string (operand1, &local_operand1,
268 ACPI_IMPLICIT_CONVERT_HEX);
269 break;
271 case ACPI_TYPE_BUFFER:
272 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
273 break;
275 default:
276 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
277 ACPI_GET_OBJECT_TYPE (operand0)));
278 status = AE_AML_INTERNAL;
281 if (ACPI_FAILURE (status)) {
282 goto cleanup;
286 * Both operands are now known to be the same object type
287 * (Both are Integer, String, or Buffer), and we can now perform the
288 * concatenation.
292 * There are three cases to handle:
294 * 1) Two Integers concatenated to produce a new Buffer
295 * 2) Two Strings concatenated to produce a new String
296 * 3) Two Buffers concatenated to produce a new Buffer
298 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
299 case ACPI_TYPE_INTEGER:
301 /* Result of two Integers is a Buffer */
302 /* Need enough buffer space for two integers */
304 return_desc = acpi_ut_create_buffer_object (
305 ACPI_MUL_2 (acpi_gbl_integer_byte_width));
306 if (!return_desc) {
307 status = AE_NO_MEMORY;
308 goto cleanup;
311 new_buf = (char *) return_desc->buffer.pointer;
313 /* Copy the first integer, LSB first */
315 ACPI_MEMCPY (new_buf,
316 &operand0->integer.value,
317 acpi_gbl_integer_byte_width);
319 /* Copy the second integer (LSB first) after the first */
321 ACPI_MEMCPY (new_buf + acpi_gbl_integer_byte_width,
322 &local_operand1->integer.value,
323 acpi_gbl_integer_byte_width);
324 break;
326 case ACPI_TYPE_STRING:
328 /* Result of two Strings is a String */
330 new_length = (acpi_size) operand0->string.length +
331 (acpi_size) local_operand1->string.length;
332 if (new_length > ACPI_MAX_STRING_CONVERSION) {
333 status = AE_AML_STRING_LIMIT;
334 goto cleanup;
337 return_desc = acpi_ut_create_string_object (new_length);
338 if (!return_desc) {
339 status = AE_NO_MEMORY;
340 goto cleanup;
343 new_buf = return_desc->string.pointer;
345 /* Concatenate the strings */
347 ACPI_STRCPY (new_buf,
348 operand0->string.pointer);
349 ACPI_STRCPY (new_buf + operand0->string.length,
350 local_operand1->string.pointer);
351 break;
353 case ACPI_TYPE_BUFFER:
355 /* Result of two Buffers is a Buffer */
357 return_desc = acpi_ut_create_buffer_object (
358 (acpi_size) operand0->buffer.length +
359 (acpi_size) local_operand1->buffer.length);
360 if (!return_desc) {
361 status = AE_NO_MEMORY;
362 goto cleanup;
365 new_buf = (char *) return_desc->buffer.pointer;
367 /* Concatenate the buffers */
369 ACPI_MEMCPY (new_buf,
370 operand0->buffer.pointer,
371 operand0->buffer.length);
372 ACPI_MEMCPY (new_buf + operand0->buffer.length,
373 local_operand1->buffer.pointer,
374 local_operand1->buffer.length);
375 break;
377 default:
379 /* Invalid object type, should not happen here */
381 ACPI_REPORT_ERROR (("Concatenate - Invalid object type: %X\n",
382 ACPI_GET_OBJECT_TYPE (operand0)));
383 status =AE_AML_INTERNAL;
384 goto cleanup;
387 *actual_return_desc = return_desc;
389 cleanup:
390 if (local_operand1 != operand1) {
391 acpi_ut_remove_reference (local_operand1);
393 return_ACPI_STATUS (status);
397 /*******************************************************************************
399 * FUNCTION: acpi_ex_do_math_op
401 * PARAMETERS: Opcode - AML opcode
402 * Integer0 - Integer operand #0
403 * Integer1 - Integer operand #1
405 * RETURN: Integer result of the operation
407 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
408 * math functions here is to prevent a lot of pointer dereferencing
409 * to obtain the operands.
411 ******************************************************************************/
413 acpi_integer
414 acpi_ex_do_math_op (
415 u16 opcode,
416 acpi_integer integer0,
417 acpi_integer integer1)
420 ACPI_FUNCTION_ENTRY ();
423 switch (opcode) {
424 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
426 return (integer0 + integer1);
429 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
431 return (integer0 & integer1);
434 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
436 return (~(integer0 & integer1));
439 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
441 return (integer0 | integer1);
444 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
446 return (~(integer0 | integer1));
449 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
451 return (integer0 ^ integer1);
454 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
456 return (integer0 * integer1);
459 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
461 return (integer0 << integer1);
464 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
466 return (integer0 >> integer1);
469 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
471 return (integer0 - integer1);
473 default:
475 return (0);
480 /*******************************************************************************
482 * FUNCTION: acpi_ex_do_logical_numeric_op
484 * PARAMETERS: Opcode - AML opcode
485 * Integer0 - Integer operand #0
486 * Integer1 - Integer operand #1
487 * logical_result - TRUE/FALSE result of the operation
489 * RETURN: Status
491 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
492 * operators (LAnd and LOr), both operands must be integers.
494 * Note: cleanest machine code seems to be produced by the code
495 * below, rather than using statements of the form:
496 * Result = (Integer0 && Integer1);
498 ******************************************************************************/
500 acpi_status
501 acpi_ex_do_logical_numeric_op (
502 u16 opcode,
503 acpi_integer integer0,
504 acpi_integer integer1,
505 u8 *logical_result)
507 acpi_status status = AE_OK;
508 u8 local_result = FALSE;
511 ACPI_FUNCTION_TRACE ("ex_do_logical_numeric_op");
514 switch (opcode) {
515 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
517 if (integer0 && integer1) {
518 local_result = TRUE;
520 break;
522 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
524 if (integer0 || integer1) {
525 local_result = TRUE;
527 break;
529 default:
530 status = AE_AML_INTERNAL;
531 break;
534 /* Return the logical result and status */
536 *logical_result = local_result;
537 return_ACPI_STATUS (status);
541 /*******************************************************************************
543 * FUNCTION: acpi_ex_do_logical_op
545 * PARAMETERS: Opcode - AML opcode
546 * Operand0 - operand #0
547 * Operand1 - operand #1
548 * logical_result - TRUE/FALSE result of the operation
550 * RETURN: Status
552 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
553 * functions here is to prevent a lot of pointer dereferencing
554 * to obtain the operands and to simplify the generation of the
555 * logical value. For the Numeric operators (LAnd and LOr), both
556 * operands must be integers. For the other logical operators,
557 * operands can be any combination of Integer/String/Buffer. The
558 * first operand determines the type to which the second operand
559 * will be converted.
561 * Note: cleanest machine code seems to be produced by the code
562 * below, rather than using statements of the form:
563 * Result = (Operand0 == Operand1);
565 ******************************************************************************/
567 acpi_status
568 acpi_ex_do_logical_op (
569 u16 opcode,
570 union acpi_operand_object *operand0,
571 union acpi_operand_object *operand1,
572 u8 *logical_result)
574 union acpi_operand_object *local_operand1 = operand1;
575 acpi_integer integer0;
576 acpi_integer integer1;
577 u32 length0;
578 u32 length1;
579 acpi_status status = AE_OK;
580 u8 local_result = FALSE;
581 int compare;
584 ACPI_FUNCTION_TRACE ("ex_do_logical_op");
588 * Convert the second operand if necessary. The first operand
589 * determines the type of the second operand, (See the Data Types
590 * section of the ACPI 3.0+ specification.) Both object types are
591 * guaranteed to be either Integer/String/Buffer by the operand
592 * resolution mechanism.
594 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
595 case ACPI_TYPE_INTEGER:
596 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
597 break;
599 case ACPI_TYPE_STRING:
600 status = acpi_ex_convert_to_string (operand1, &local_operand1,
601 ACPI_IMPLICIT_CONVERT_HEX);
602 break;
604 case ACPI_TYPE_BUFFER:
605 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
606 break;
608 default:
609 status = AE_AML_INTERNAL;
610 break;
613 if (ACPI_FAILURE (status)) {
614 goto cleanup;
618 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
620 if (ACPI_GET_OBJECT_TYPE (operand0) == ACPI_TYPE_INTEGER) {
622 * 1) Both operands are of type integer
623 * Note: local_operand1 may have changed above
625 integer0 = operand0->integer.value;
626 integer1 = local_operand1->integer.value;
628 switch (opcode) {
629 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
631 if (integer0 == integer1) {
632 local_result = TRUE;
634 break;
636 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
638 if (integer0 > integer1) {
639 local_result = TRUE;
641 break;
643 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
645 if (integer0 < integer1) {
646 local_result = TRUE;
648 break;
650 default:
651 status = AE_AML_INTERNAL;
652 break;
655 else {
657 * 2) Both operands are Strings or both are Buffers
658 * Note: Code below takes advantage of common Buffer/String
659 * object fields. local_operand1 may have changed above. Use
660 * memcmp to handle nulls in buffers.
662 length0 = operand0->buffer.length;
663 length1 = local_operand1->buffer.length;
665 /* Lexicographic compare: compare the data bytes */
667 compare = ACPI_MEMCMP ((const char * ) operand0->buffer.pointer,
668 (const char * ) local_operand1->buffer.pointer,
669 (length0 > length1) ? length1 : length0);
671 switch (opcode) {
672 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
674 /* Length and all bytes must be equal */
676 if ((length0 == length1) &&
677 (compare == 0)) {
678 /* Length and all bytes match ==> TRUE */
680 local_result = TRUE;
682 break;
684 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
686 if (compare > 0) {
687 local_result = TRUE;
688 goto cleanup; /* TRUE */
690 if (compare < 0) {
691 goto cleanup; /* FALSE */
694 /* Bytes match (to shortest length), compare lengths */
696 if (length0 > length1) {
697 local_result = TRUE;
699 break;
701 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
703 if (compare > 0) {
704 goto cleanup; /* FALSE */
706 if (compare < 0) {
707 local_result = TRUE;
708 goto cleanup; /* TRUE */
711 /* Bytes match (to shortest length), compare lengths */
713 if (length0 < length1) {
714 local_result = TRUE;
716 break;
718 default:
719 status = AE_AML_INTERNAL;
720 break;
724 cleanup:
726 /* New object was created if implicit conversion performed - delete */
728 if (local_operand1 != operand1) {
729 acpi_ut_remove_reference (local_operand1);
732 /* Return the logical result and status */
734 *logical_result = local_result;
735 return_ACPI_STATUS (status);