ACPICA 20050617-0624 from Bob Moore <robert.moore@intel.com>
[firewire-audio.git] / drivers / acpi / executer / exmisc.c
blob237ef28c813215f78922dc3960a56b76f9f647e6
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,
143 "Object %p Type [%s], returning Reference %p\n",
144 obj_desc, acpi_ut_get_object_type_name (obj_desc), *return_desc));
146 return_ACPI_STATUS (AE_OK);
150 /*******************************************************************************
152 * FUNCTION: acpi_ex_concat_template
154 * PARAMETERS: Operand0 - First source object
155 * Operand1 - Second source object
156 * actual_return_desc - Where to place the return object
157 * walk_state - Current walk state
159 * RETURN: Status
161 * DESCRIPTION: Concatenate two resource templates
163 ******************************************************************************/
165 acpi_status
166 acpi_ex_concat_template (
167 union acpi_operand_object *operand0,
168 union acpi_operand_object *operand1,
169 union acpi_operand_object **actual_return_desc,
170 struct acpi_walk_state *walk_state)
172 union acpi_operand_object *return_desc;
173 u8 *new_buf;
174 u8 *end_tag1;
175 u8 *end_tag2;
176 acpi_size length1;
177 acpi_size length2;
180 ACPI_FUNCTION_TRACE ("ex_concat_template");
183 /* Find the end_tags in each resource template */
185 end_tag1 = acpi_ut_get_resource_end_tag (operand0);
186 end_tag2 = acpi_ut_get_resource_end_tag (operand1);
187 if (!end_tag1 || !end_tag2) {
188 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
191 /* Compute the length of each part */
193 length1 = ACPI_PTR_DIFF (end_tag1, operand0->buffer.pointer);
194 length2 = ACPI_PTR_DIFF (end_tag2, operand1->buffer.pointer) +
195 2; /* Size of END_TAG */
197 /* Create a new buffer object for the result */
199 return_desc = acpi_ut_create_buffer_object (length1 + length2);
200 if (!return_desc) {
201 return_ACPI_STATUS (AE_NO_MEMORY);
204 /* Copy the templates to the new descriptor */
206 new_buf = return_desc->buffer.pointer;
207 ACPI_MEMCPY (new_buf, operand0->buffer.pointer, length1);
208 ACPI_MEMCPY (new_buf + length1, operand1->buffer.pointer, length2);
210 /* Compute the new checksum */
212 new_buf[return_desc->buffer.length - 1] =
213 acpi_ut_generate_checksum (return_desc->buffer.pointer,
214 (return_desc->buffer.length - 1));
216 /* Return the completed template descriptor */
218 *actual_return_desc = return_desc;
219 return_ACPI_STATUS (AE_OK);
223 /*******************************************************************************
225 * FUNCTION: acpi_ex_do_concatenate
227 * PARAMETERS: Operand0 - First source object
228 * Operand1 - Second source object
229 * actual_return_desc - Where to place the return object
230 * walk_state - Current walk state
232 * RETURN: Status
234 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
236 ******************************************************************************/
238 acpi_status
239 acpi_ex_do_concatenate (
240 union acpi_operand_object *operand0,
241 union acpi_operand_object *operand1,
242 union acpi_operand_object **actual_return_desc,
243 struct acpi_walk_state *walk_state)
245 union acpi_operand_object *local_operand1 = operand1;
246 union acpi_operand_object *return_desc;
247 char *new_buf;
248 acpi_status status;
249 acpi_size new_length;
252 ACPI_FUNCTION_TRACE ("ex_do_concatenate");
256 * Convert the second operand if necessary. The first operand
257 * determines the type of the second operand, (See the Data Types
258 * section of the ACPI specification.) Both object types are
259 * guaranteed to be either Integer/String/Buffer by the operand
260 * resolution mechanism.
262 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
263 case ACPI_TYPE_INTEGER:
264 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
265 break;
267 case ACPI_TYPE_STRING:
268 status = acpi_ex_convert_to_string (operand1, &local_operand1,
269 ACPI_IMPLICIT_CONVERT_HEX);
270 break;
272 case ACPI_TYPE_BUFFER:
273 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
274 break;
276 default:
277 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
278 ACPI_GET_OBJECT_TYPE (operand0)));
279 status = AE_AML_INTERNAL;
282 if (ACPI_FAILURE (status)) {
283 goto cleanup;
287 * Both operands are now known to be the same object type
288 * (Both are Integer, String, or Buffer), and we can now perform the
289 * concatenation.
293 * There are three cases to handle:
295 * 1) Two Integers concatenated to produce a new Buffer
296 * 2) Two Strings concatenated to produce a new String
297 * 3) Two Buffers concatenated to produce a new Buffer
299 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
300 case ACPI_TYPE_INTEGER:
302 /* Result of two Integers is a Buffer */
303 /* Need enough buffer space for two integers */
305 return_desc = acpi_ut_create_buffer_object ((acpi_size)
306 ACPI_MUL_2 (acpi_gbl_integer_byte_width));
307 if (!return_desc) {
308 status = AE_NO_MEMORY;
309 goto cleanup;
312 new_buf = (char *) return_desc->buffer.pointer;
314 /* Copy the first integer, LSB first */
316 ACPI_MEMCPY (new_buf,
317 &operand0->integer.value,
318 acpi_gbl_integer_byte_width);
320 /* Copy the second integer (LSB first) after the first */
322 ACPI_MEMCPY (new_buf + acpi_gbl_integer_byte_width,
323 &local_operand1->integer.value,
324 acpi_gbl_integer_byte_width);
325 break;
327 case ACPI_TYPE_STRING:
329 /* Result of two Strings is a String */
331 new_length = (acpi_size) operand0->string.length +
332 (acpi_size) local_operand1->string.length;
333 if (new_length > ACPI_MAX_STRING_CONVERSION) {
334 status = AE_AML_STRING_LIMIT;
335 goto cleanup;
338 return_desc = acpi_ut_create_string_object (new_length);
339 if (!return_desc) {
340 status = AE_NO_MEMORY;
341 goto cleanup;
344 new_buf = return_desc->string.pointer;
346 /* Concatenate the strings */
348 ACPI_STRCPY (new_buf,
349 operand0->string.pointer);
350 ACPI_STRCPY (new_buf + operand0->string.length,
351 local_operand1->string.pointer);
352 break;
354 case ACPI_TYPE_BUFFER:
356 /* Result of two Buffers is a Buffer */
358 return_desc = acpi_ut_create_buffer_object (
359 (acpi_size) operand0->buffer.length +
360 (acpi_size) local_operand1->buffer.length);
361 if (!return_desc) {
362 status = AE_NO_MEMORY;
363 goto cleanup;
366 new_buf = (char *) return_desc->buffer.pointer;
368 /* Concatenate the buffers */
370 ACPI_MEMCPY (new_buf,
371 operand0->buffer.pointer,
372 operand0->buffer.length);
373 ACPI_MEMCPY (new_buf + operand0->buffer.length,
374 local_operand1->buffer.pointer,
375 local_operand1->buffer.length);
376 break;
378 default:
380 /* Invalid object type, should not happen here */
382 ACPI_REPORT_ERROR (("Concatenate - Invalid object type: %X\n",
383 ACPI_GET_OBJECT_TYPE (operand0)));
384 status =AE_AML_INTERNAL;
385 goto cleanup;
388 *actual_return_desc = return_desc;
390 cleanup:
391 if (local_operand1 != operand1) {
392 acpi_ut_remove_reference (local_operand1);
394 return_ACPI_STATUS (status);
398 /*******************************************************************************
400 * FUNCTION: acpi_ex_do_math_op
402 * PARAMETERS: Opcode - AML opcode
403 * Integer0 - Integer operand #0
404 * Integer1 - Integer operand #1
406 * RETURN: Integer result of the operation
408 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
409 * math functions here is to prevent a lot of pointer dereferencing
410 * to obtain the operands.
412 ******************************************************************************/
414 acpi_integer
415 acpi_ex_do_math_op (
416 u16 opcode,
417 acpi_integer integer0,
418 acpi_integer integer1)
421 ACPI_FUNCTION_ENTRY ();
424 switch (opcode) {
425 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
427 return (integer0 + integer1);
430 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
432 return (integer0 & integer1);
435 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
437 return (~(integer0 & integer1));
440 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
442 return (integer0 | integer1);
445 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
447 return (~(integer0 | integer1));
450 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
452 return (integer0 ^ integer1);
455 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
457 return (integer0 * integer1);
460 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result)*/
462 return (integer0 << integer1);
465 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
467 return (integer0 >> integer1);
470 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
472 return (integer0 - integer1);
474 default:
476 return (0);
481 /*******************************************************************************
483 * FUNCTION: acpi_ex_do_logical_numeric_op
485 * PARAMETERS: Opcode - AML opcode
486 * Integer0 - Integer operand #0
487 * Integer1 - Integer operand #1
488 * logical_result - TRUE/FALSE result of the operation
490 * RETURN: Status
492 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
493 * operators (LAnd and LOr), both operands must be integers.
495 * Note: cleanest machine code seems to be produced by the code
496 * below, rather than using statements of the form:
497 * Result = (Integer0 && Integer1);
499 ******************************************************************************/
501 acpi_status
502 acpi_ex_do_logical_numeric_op (
503 u16 opcode,
504 acpi_integer integer0,
505 acpi_integer integer1,
506 u8 *logical_result)
508 acpi_status status = AE_OK;
509 u8 local_result = FALSE;
512 ACPI_FUNCTION_TRACE ("ex_do_logical_numeric_op");
515 switch (opcode) {
516 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
518 if (integer0 && integer1) {
519 local_result = TRUE;
521 break;
523 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
525 if (integer0 || integer1) {
526 local_result = TRUE;
528 break;
530 default:
531 status = AE_AML_INTERNAL;
532 break;
535 /* Return the logical result and status */
537 *logical_result = local_result;
538 return_ACPI_STATUS (status);
542 /*******************************************************************************
544 * FUNCTION: acpi_ex_do_logical_op
546 * PARAMETERS: Opcode - AML opcode
547 * Operand0 - operand #0
548 * Operand1 - operand #1
549 * logical_result - TRUE/FALSE result of the operation
551 * RETURN: Status
553 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
554 * functions here is to prevent a lot of pointer dereferencing
555 * to obtain the operands and to simplify the generation of the
556 * logical value. For the Numeric operators (LAnd and LOr), both
557 * operands must be integers. For the other logical operators,
558 * operands can be any combination of Integer/String/Buffer. The
559 * first operand determines the type to which the second operand
560 * will be converted.
562 * Note: cleanest machine code seems to be produced by the code
563 * below, rather than using statements of the form:
564 * Result = (Operand0 == Operand1);
566 ******************************************************************************/
568 acpi_status
569 acpi_ex_do_logical_op (
570 u16 opcode,
571 union acpi_operand_object *operand0,
572 union acpi_operand_object *operand1,
573 u8 *logical_result)
575 union acpi_operand_object *local_operand1 = operand1;
576 acpi_integer integer0;
577 acpi_integer integer1;
578 u32 length0;
579 u32 length1;
580 acpi_status status = AE_OK;
581 u8 local_result = FALSE;
582 int compare;
585 ACPI_FUNCTION_TRACE ("ex_do_logical_op");
589 * Convert the second operand if necessary. The first operand
590 * determines the type of the second operand, (See the Data Types
591 * section of the ACPI 3.0+ specification.) Both object types are
592 * guaranteed to be either Integer/String/Buffer by the operand
593 * resolution mechanism.
595 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
596 case ACPI_TYPE_INTEGER:
597 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
598 break;
600 case ACPI_TYPE_STRING:
601 status = acpi_ex_convert_to_string (operand1, &local_operand1,
602 ACPI_IMPLICIT_CONVERT_HEX);
603 break;
605 case ACPI_TYPE_BUFFER:
606 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
607 break;
609 default:
610 status = AE_AML_INTERNAL;
611 break;
614 if (ACPI_FAILURE (status)) {
615 goto cleanup;
619 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
621 if (ACPI_GET_OBJECT_TYPE (operand0) == ACPI_TYPE_INTEGER) {
623 * 1) Both operands are of type integer
624 * Note: local_operand1 may have changed above
626 integer0 = operand0->integer.value;
627 integer1 = local_operand1->integer.value;
629 switch (opcode) {
630 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
632 if (integer0 == integer1) {
633 local_result = TRUE;
635 break;
637 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
639 if (integer0 > integer1) {
640 local_result = TRUE;
642 break;
644 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
646 if (integer0 < integer1) {
647 local_result = TRUE;
649 break;
651 default:
652 status = AE_AML_INTERNAL;
653 break;
656 else {
658 * 2) Both operands are Strings or both are Buffers
659 * Note: Code below takes advantage of common Buffer/String
660 * object fields. local_operand1 may have changed above. Use
661 * memcmp to handle nulls in buffers.
663 length0 = operand0->buffer.length;
664 length1 = local_operand1->buffer.length;
666 /* Lexicographic compare: compare the data bytes */
668 compare = ACPI_MEMCMP ((const char * ) operand0->buffer.pointer,
669 (const char * ) local_operand1->buffer.pointer,
670 (length0 > length1) ? length1 : length0);
672 switch (opcode) {
673 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
675 /* Length and all bytes must be equal */
677 if ((length0 == length1) &&
678 (compare == 0)) {
679 /* Length and all bytes match ==> TRUE */
681 local_result = TRUE;
683 break;
685 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
687 if (compare > 0) {
688 local_result = TRUE;
689 goto cleanup; /* TRUE */
691 if (compare < 0) {
692 goto cleanup; /* FALSE */
695 /* Bytes match (to shortest length), compare lengths */
697 if (length0 > length1) {
698 local_result = TRUE;
700 break;
702 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
704 if (compare > 0) {
705 goto cleanup; /* FALSE */
707 if (compare < 0) {
708 local_result = TRUE;
709 goto cleanup; /* TRUE */
712 /* Bytes match (to shortest length), compare lengths */
714 if (length0 < length1) {
715 local_result = TRUE;
717 break;
719 default:
720 status = AE_AML_INTERNAL;
721 break;
725 cleanup:
727 /* New object was created if implicit conversion performed - delete */
729 if (local_operand1 != operand1) {
730 acpi_ut_remove_reference (local_operand1);
733 /* Return the logical result and status */
735 *logical_result = local_result;
736 return_ACPI_STATUS (status);