195b482a8SLen Brown /****************************************************************************** 295b482a8SLen Brown * 395b482a8SLen Brown * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes 495b482a8SLen Brown * 595b482a8SLen Brown *****************************************************************************/ 695b482a8SLen Brown 795b482a8SLen Brown /* 825f044e6SBob Moore * Copyright (C) 2000 - 2013, Intel Corp. 995b482a8SLen Brown * All rights reserved. 1095b482a8SLen Brown * 1195b482a8SLen Brown * Redistribution and use in source and binary forms, with or without 1295b482a8SLen Brown * modification, are permitted provided that the following conditions 1395b482a8SLen Brown * are met: 1495b482a8SLen Brown * 1. Redistributions of source code must retain the above copyright 1595b482a8SLen Brown * notice, this list of conditions, and the following disclaimer, 1695b482a8SLen Brown * without modification. 1795b482a8SLen Brown * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1895b482a8SLen Brown * substantially similar to the "NO WARRANTY" disclaimer below 1995b482a8SLen Brown * ("Disclaimer") and any redistribution must be conditioned upon 2095b482a8SLen Brown * including a substantially similar Disclaimer requirement for further 2195b482a8SLen Brown * binary redistribution. 2295b482a8SLen Brown * 3. Neither the names of the above-listed copyright holders nor the names 2395b482a8SLen Brown * of any contributors may be used to endorse or promote products derived 2495b482a8SLen Brown * from this software without specific prior written permission. 2595b482a8SLen Brown * 2695b482a8SLen Brown * Alternatively, this software may be distributed under the terms of the 2795b482a8SLen Brown * GNU General Public License ("GPL") version 2 as published by the Free 2895b482a8SLen Brown * Software Foundation. 2995b482a8SLen Brown * 3095b482a8SLen Brown * NO WARRANTY 3195b482a8SLen Brown * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 3295b482a8SLen Brown * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3395b482a8SLen Brown * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 3495b482a8SLen Brown * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3595b482a8SLen Brown * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3695b482a8SLen Brown * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3795b482a8SLen Brown * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3895b482a8SLen Brown * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 3995b482a8SLen Brown * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 4095b482a8SLen Brown * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4195b482a8SLen Brown * POSSIBILITY OF SUCH DAMAGES. 4295b482a8SLen Brown */ 4395b482a8SLen Brown 4495b482a8SLen Brown #include <acpi/acpi.h> 45e2f7a777SLen Brown #include "accommon.h" 46e2f7a777SLen Brown #include "acinterp.h" 47e2f7a777SLen Brown #include "amlcode.h" 48e2f7a777SLen Brown #include "amlresrc.h" 4995b482a8SLen Brown 5095b482a8SLen Brown #define _COMPONENT ACPI_EXECUTER 5195b482a8SLen Brown ACPI_MODULE_NAME("exmisc") 5295b482a8SLen Brown 5395b482a8SLen Brown /******************************************************************************* 5495b482a8SLen Brown * 5595b482a8SLen Brown * FUNCTION: acpi_ex_get_object_reference 5695b482a8SLen Brown * 5795b482a8SLen Brown * PARAMETERS: obj_desc - Create a reference to this object 5895b482a8SLen Brown * return_desc - Where to store the reference 5995b482a8SLen Brown * walk_state - Current state 6095b482a8SLen Brown * 6195b482a8SLen Brown * RETURN: Status 6295b482a8SLen Brown * 6395b482a8SLen Brown * DESCRIPTION: Obtain and return a "reference" to the target object 6495b482a8SLen Brown * Common code for the ref_of_op and the cond_ref_of_op. 6595b482a8SLen Brown * 6695b482a8SLen Brown ******************************************************************************/ 6795b482a8SLen Brown acpi_status 6895b482a8SLen Brown acpi_ex_get_object_reference(union acpi_operand_object *obj_desc, 6995b482a8SLen Brown union acpi_operand_object **return_desc, 7095b482a8SLen Brown struct acpi_walk_state *walk_state) 7195b482a8SLen Brown { 7295b482a8SLen Brown union acpi_operand_object *reference_obj; 7395b482a8SLen Brown union acpi_operand_object *referenced_obj; 7495b482a8SLen Brown 7595b482a8SLen Brown ACPI_FUNCTION_TRACE_PTR(ex_get_object_reference, obj_desc); 7695b482a8SLen Brown 7795b482a8SLen Brown *return_desc = NULL; 7895b482a8SLen Brown 7995b482a8SLen Brown switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) { 8095b482a8SLen Brown case ACPI_DESC_TYPE_OPERAND: 8195b482a8SLen Brown 823371c19cSBob Moore if (obj_desc->common.type != ACPI_TYPE_LOCAL_REFERENCE) { 8395b482a8SLen Brown return_ACPI_STATUS(AE_AML_OPERAND_TYPE); 8495b482a8SLen Brown } 8595b482a8SLen Brown 8695b482a8SLen Brown /* 8795b482a8SLen Brown * Must be a reference to a Local or Arg 8895b482a8SLen Brown */ 8995b482a8SLen Brown switch (obj_desc->reference.class) { 9095b482a8SLen Brown case ACPI_REFCLASS_LOCAL: 9195b482a8SLen Brown case ACPI_REFCLASS_ARG: 9295b482a8SLen Brown case ACPI_REFCLASS_DEBUG: 9395b482a8SLen Brown 9495b482a8SLen Brown /* The referenced object is the pseudo-node for the local/arg */ 9595b482a8SLen Brown 9695b482a8SLen Brown referenced_obj = obj_desc->reference.object; 9795b482a8SLen Brown break; 9895b482a8SLen Brown 9995b482a8SLen Brown default: 10095b482a8SLen Brown 101f6a22b0bSBob Moore ACPI_ERROR((AE_INFO, "Unknown Reference Class 0x%2.2X", 10295b482a8SLen Brown obj_desc->reference.class)); 10395b482a8SLen Brown return_ACPI_STATUS(AE_AML_INTERNAL); 10495b482a8SLen Brown } 10595b482a8SLen Brown break; 10695b482a8SLen Brown 10795b482a8SLen Brown case ACPI_DESC_TYPE_NAMED: 10895b482a8SLen Brown /* 10995b482a8SLen Brown * A named reference that has already been resolved to a Node 11095b482a8SLen Brown */ 11195b482a8SLen Brown referenced_obj = obj_desc; 11295b482a8SLen Brown break; 11395b482a8SLen Brown 11495b482a8SLen Brown default: 11595b482a8SLen Brown 116f6a22b0bSBob Moore ACPI_ERROR((AE_INFO, "Invalid descriptor type 0x%X", 11795b482a8SLen Brown ACPI_GET_DESCRIPTOR_TYPE(obj_desc))); 11895b482a8SLen Brown return_ACPI_STATUS(AE_TYPE); 11995b482a8SLen Brown } 12095b482a8SLen Brown 12195b482a8SLen Brown /* Create a new reference object */ 12295b482a8SLen Brown 12395b482a8SLen Brown reference_obj = 12495b482a8SLen Brown acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE); 12595b482a8SLen Brown if (!reference_obj) { 12695b482a8SLen Brown return_ACPI_STATUS(AE_NO_MEMORY); 12795b482a8SLen Brown } 12895b482a8SLen Brown 12995b482a8SLen Brown reference_obj->reference.class = ACPI_REFCLASS_REFOF; 13095b482a8SLen Brown reference_obj->reference.object = referenced_obj; 13195b482a8SLen Brown *return_desc = reference_obj; 13295b482a8SLen Brown 13395b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_EXEC, 13495b482a8SLen Brown "Object %p Type [%s], returning Reference %p\n", 13595b482a8SLen Brown obj_desc, acpi_ut_get_object_type_name(obj_desc), 13695b482a8SLen Brown *return_desc)); 13795b482a8SLen Brown 13895b482a8SLen Brown return_ACPI_STATUS(AE_OK); 13995b482a8SLen Brown } 14095b482a8SLen Brown 14195b482a8SLen Brown /******************************************************************************* 14295b482a8SLen Brown * 14395b482a8SLen Brown * FUNCTION: acpi_ex_concat_template 14495b482a8SLen Brown * 145ba494beeSBob Moore * PARAMETERS: operand0 - First source object 146ba494beeSBob Moore * operand1 - Second source object 14795b482a8SLen Brown * actual_return_desc - Where to place the return object 14895b482a8SLen Brown * walk_state - Current walk state 14995b482a8SLen Brown * 15095b482a8SLen Brown * RETURN: Status 15195b482a8SLen Brown * 15295b482a8SLen Brown * DESCRIPTION: Concatenate two resource templates 15395b482a8SLen Brown * 15495b482a8SLen Brown ******************************************************************************/ 15595b482a8SLen Brown 15695b482a8SLen Brown acpi_status 15795b482a8SLen Brown acpi_ex_concat_template(union acpi_operand_object *operand0, 15895b482a8SLen Brown union acpi_operand_object *operand1, 15995b482a8SLen Brown union acpi_operand_object **actual_return_desc, 16095b482a8SLen Brown struct acpi_walk_state *walk_state) 16195b482a8SLen Brown { 16295b482a8SLen Brown acpi_status status; 16395b482a8SLen Brown union acpi_operand_object *return_desc; 16495b482a8SLen Brown u8 *new_buf; 16595b482a8SLen Brown u8 *end_tag; 16695b482a8SLen Brown acpi_size length0; 16795b482a8SLen Brown acpi_size length1; 16895b482a8SLen Brown acpi_size new_length; 16995b482a8SLen Brown 17095b482a8SLen Brown ACPI_FUNCTION_TRACE(ex_concat_template); 17195b482a8SLen Brown 17295b482a8SLen Brown /* 17395b482a8SLen Brown * Find the end_tag descriptor in each resource template. 17495b482a8SLen Brown * Note1: returned pointers point TO the end_tag, not past it. 17595b482a8SLen Brown * Note2: zero-length buffers are allowed; treated like one end_tag 17695b482a8SLen Brown */ 17795b482a8SLen Brown 17895b482a8SLen Brown /* Get the length of the first resource template */ 17995b482a8SLen Brown 18095b482a8SLen Brown status = acpi_ut_get_resource_end_tag(operand0, &end_tag); 18195b482a8SLen Brown if (ACPI_FAILURE(status)) { 18295b482a8SLen Brown return_ACPI_STATUS(status); 18395b482a8SLen Brown } 18495b482a8SLen Brown 18595b482a8SLen Brown length0 = ACPI_PTR_DIFF(end_tag, operand0->buffer.pointer); 18695b482a8SLen Brown 18795b482a8SLen Brown /* Get the length of the second resource template */ 18895b482a8SLen Brown 18995b482a8SLen Brown status = acpi_ut_get_resource_end_tag(operand1, &end_tag); 19095b482a8SLen Brown if (ACPI_FAILURE(status)) { 19195b482a8SLen Brown return_ACPI_STATUS(status); 19295b482a8SLen Brown } 19395b482a8SLen Brown 19495b482a8SLen Brown length1 = ACPI_PTR_DIFF(end_tag, operand1->buffer.pointer); 19595b482a8SLen Brown 19695b482a8SLen Brown /* Combine both lengths, minimum size will be 2 for end_tag */ 19795b482a8SLen Brown 19895b482a8SLen Brown new_length = length0 + length1 + sizeof(struct aml_resource_end_tag); 19995b482a8SLen Brown 20095b482a8SLen Brown /* Create a new buffer object for the result (with one end_tag) */ 20195b482a8SLen Brown 20295b482a8SLen Brown return_desc = acpi_ut_create_buffer_object(new_length); 20395b482a8SLen Brown if (!return_desc) { 20495b482a8SLen Brown return_ACPI_STATUS(AE_NO_MEMORY); 20595b482a8SLen Brown } 20695b482a8SLen Brown 20795b482a8SLen Brown /* 20895b482a8SLen Brown * Copy the templates to the new buffer, 0 first, then 1 follows. One 20995b482a8SLen Brown * end_tag descriptor is copied from Operand1. 21095b482a8SLen Brown */ 21195b482a8SLen Brown new_buf = return_desc->buffer.pointer; 21295b482a8SLen Brown ACPI_MEMCPY(new_buf, operand0->buffer.pointer, length0); 21395b482a8SLen Brown ACPI_MEMCPY(new_buf + length0, operand1->buffer.pointer, length1); 21495b482a8SLen Brown 21595b482a8SLen Brown /* Insert end_tag and set the checksum to zero, means "ignore checksum" */ 21695b482a8SLen Brown 21795b482a8SLen Brown new_buf[new_length - 1] = 0; 21895b482a8SLen Brown new_buf[new_length - 2] = ACPI_RESOURCE_NAME_END_TAG | 1; 21995b482a8SLen Brown 22095b482a8SLen Brown /* Return the completed resource template */ 22195b482a8SLen Brown 22295b482a8SLen Brown *actual_return_desc = return_desc; 22395b482a8SLen Brown return_ACPI_STATUS(AE_OK); 22495b482a8SLen Brown } 22595b482a8SLen Brown 22695b482a8SLen Brown /******************************************************************************* 22795b482a8SLen Brown * 22895b482a8SLen Brown * FUNCTION: acpi_ex_do_concatenate 22995b482a8SLen Brown * 230ba494beeSBob Moore * PARAMETERS: operand0 - First source object 231ba494beeSBob Moore * operand1 - Second source object 23295b482a8SLen Brown * actual_return_desc - Where to place the return object 23395b482a8SLen Brown * walk_state - Current walk state 23495b482a8SLen Brown * 23595b482a8SLen Brown * RETURN: Status 23695b482a8SLen Brown * 23795b482a8SLen Brown * DESCRIPTION: Concatenate two objects OF THE SAME TYPE. 23895b482a8SLen Brown * 23995b482a8SLen Brown ******************************************************************************/ 24095b482a8SLen Brown 24195b482a8SLen Brown acpi_status 24295b482a8SLen Brown acpi_ex_do_concatenate(union acpi_operand_object *operand0, 24395b482a8SLen Brown union acpi_operand_object *operand1, 24495b482a8SLen Brown union acpi_operand_object **actual_return_desc, 24595b482a8SLen Brown struct acpi_walk_state *walk_state) 24695b482a8SLen Brown { 24795b482a8SLen Brown union acpi_operand_object *local_operand1 = operand1; 24895b482a8SLen Brown union acpi_operand_object *return_desc; 24995b482a8SLen Brown char *new_buf; 25095b482a8SLen Brown acpi_status status; 25195b482a8SLen Brown 25295b482a8SLen Brown ACPI_FUNCTION_TRACE(ex_do_concatenate); 25395b482a8SLen Brown 25495b482a8SLen Brown /* 25595b482a8SLen Brown * Convert the second operand if necessary. The first operand 25695b482a8SLen Brown * determines the type of the second operand, (See the Data Types 25795b482a8SLen Brown * section of the ACPI specification.) Both object types are 25895b482a8SLen Brown * guaranteed to be either Integer/String/Buffer by the operand 25995b482a8SLen Brown * resolution mechanism. 26095b482a8SLen Brown */ 2613371c19cSBob Moore switch (operand0->common.type) { 26295b482a8SLen Brown case ACPI_TYPE_INTEGER: 263*1d1ea1b7SChao Guan 26495b482a8SLen Brown status = 26595b482a8SLen Brown acpi_ex_convert_to_integer(operand1, &local_operand1, 16); 26695b482a8SLen Brown break; 26795b482a8SLen Brown 26895b482a8SLen Brown case ACPI_TYPE_STRING: 269*1d1ea1b7SChao Guan 27095b482a8SLen Brown status = acpi_ex_convert_to_string(operand1, &local_operand1, 27195b482a8SLen Brown ACPI_IMPLICIT_CONVERT_HEX); 27295b482a8SLen Brown break; 27395b482a8SLen Brown 27495b482a8SLen Brown case ACPI_TYPE_BUFFER: 275*1d1ea1b7SChao Guan 27695b482a8SLen Brown status = acpi_ex_convert_to_buffer(operand1, &local_operand1); 27795b482a8SLen Brown break; 27895b482a8SLen Brown 27995b482a8SLen Brown default: 280*1d1ea1b7SChao Guan 281f6a22b0bSBob Moore ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X", 2823371c19cSBob Moore operand0->common.type)); 28395b482a8SLen Brown status = AE_AML_INTERNAL; 28495b482a8SLen Brown } 28595b482a8SLen Brown 28695b482a8SLen Brown if (ACPI_FAILURE(status)) { 28795b482a8SLen Brown goto cleanup; 28895b482a8SLen Brown } 28995b482a8SLen Brown 29095b482a8SLen Brown /* 29195b482a8SLen Brown * Both operands are now known to be the same object type 29295b482a8SLen Brown * (Both are Integer, String, or Buffer), and we can now perform the 29395b482a8SLen Brown * concatenation. 29495b482a8SLen Brown */ 29595b482a8SLen Brown 29695b482a8SLen Brown /* 29795b482a8SLen Brown * There are three cases to handle: 29895b482a8SLen Brown * 29995b482a8SLen Brown * 1) Two Integers concatenated to produce a new Buffer 30095b482a8SLen Brown * 2) Two Strings concatenated to produce a new String 30195b482a8SLen Brown * 3) Two Buffers concatenated to produce a new Buffer 30295b482a8SLen Brown */ 3033371c19cSBob Moore switch (operand0->common.type) { 30495b482a8SLen Brown case ACPI_TYPE_INTEGER: 30595b482a8SLen Brown 30695b482a8SLen Brown /* Result of two Integers is a Buffer */ 30795b482a8SLen Brown /* Need enough buffer space for two integers */ 30895b482a8SLen Brown 30995b482a8SLen Brown return_desc = acpi_ut_create_buffer_object((acpi_size) 31095b482a8SLen Brown ACPI_MUL_2 31195b482a8SLen Brown (acpi_gbl_integer_byte_width)); 31295b482a8SLen Brown if (!return_desc) { 31395b482a8SLen Brown status = AE_NO_MEMORY; 31495b482a8SLen Brown goto cleanup; 31595b482a8SLen Brown } 31695b482a8SLen Brown 31795b482a8SLen Brown new_buf = (char *)return_desc->buffer.pointer; 31895b482a8SLen Brown 31995b482a8SLen Brown /* Copy the first integer, LSB first */ 32095b482a8SLen Brown 32195b482a8SLen Brown ACPI_MEMCPY(new_buf, &operand0->integer.value, 32295b482a8SLen Brown acpi_gbl_integer_byte_width); 32395b482a8SLen Brown 32495b482a8SLen Brown /* Copy the second integer (LSB first) after the first */ 32595b482a8SLen Brown 32695b482a8SLen Brown ACPI_MEMCPY(new_buf + acpi_gbl_integer_byte_width, 32795b482a8SLen Brown &local_operand1->integer.value, 32895b482a8SLen Brown acpi_gbl_integer_byte_width); 32995b482a8SLen Brown break; 33095b482a8SLen Brown 33195b482a8SLen Brown case ACPI_TYPE_STRING: 33295b482a8SLen Brown 33395b482a8SLen Brown /* Result of two Strings is a String */ 33495b482a8SLen Brown 33595b482a8SLen Brown return_desc = acpi_ut_create_string_object(((acpi_size) 33695b482a8SLen Brown operand0->string. 33795b482a8SLen Brown length + 33895b482a8SLen Brown local_operand1-> 33995b482a8SLen Brown string.length)); 34095b482a8SLen Brown if (!return_desc) { 34195b482a8SLen Brown status = AE_NO_MEMORY; 34295b482a8SLen Brown goto cleanup; 34395b482a8SLen Brown } 34495b482a8SLen Brown 34595b482a8SLen Brown new_buf = return_desc->string.pointer; 34695b482a8SLen Brown 34795b482a8SLen Brown /* Concatenate the strings */ 34895b482a8SLen Brown 34995b482a8SLen Brown ACPI_STRCPY(new_buf, operand0->string.pointer); 35095b482a8SLen Brown ACPI_STRCPY(new_buf + operand0->string.length, 35195b482a8SLen Brown local_operand1->string.pointer); 35295b482a8SLen Brown break; 35395b482a8SLen Brown 35495b482a8SLen Brown case ACPI_TYPE_BUFFER: 35595b482a8SLen Brown 35695b482a8SLen Brown /* Result of two Buffers is a Buffer */ 35795b482a8SLen Brown 35895b482a8SLen Brown return_desc = acpi_ut_create_buffer_object(((acpi_size) 35995b482a8SLen Brown operand0->buffer. 36095b482a8SLen Brown length + 36195b482a8SLen Brown local_operand1-> 36295b482a8SLen Brown buffer.length)); 36395b482a8SLen Brown if (!return_desc) { 36495b482a8SLen Brown status = AE_NO_MEMORY; 36595b482a8SLen Brown goto cleanup; 36695b482a8SLen Brown } 36795b482a8SLen Brown 36895b482a8SLen Brown new_buf = (char *)return_desc->buffer.pointer; 36995b482a8SLen Brown 37095b482a8SLen Brown /* Concatenate the buffers */ 37195b482a8SLen Brown 37295b482a8SLen Brown ACPI_MEMCPY(new_buf, operand0->buffer.pointer, 37395b482a8SLen Brown operand0->buffer.length); 37495b482a8SLen Brown ACPI_MEMCPY(new_buf + operand0->buffer.length, 37595b482a8SLen Brown local_operand1->buffer.pointer, 37695b482a8SLen Brown local_operand1->buffer.length); 37795b482a8SLen Brown break; 37895b482a8SLen Brown 37995b482a8SLen Brown default: 38095b482a8SLen Brown 38195b482a8SLen Brown /* Invalid object type, should not happen here */ 38295b482a8SLen Brown 383f6a22b0bSBob Moore ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X", 3843371c19cSBob Moore operand0->common.type)); 38595b482a8SLen Brown status = AE_AML_INTERNAL; 38695b482a8SLen Brown goto cleanup; 38795b482a8SLen Brown } 38895b482a8SLen Brown 38995b482a8SLen Brown *actual_return_desc = return_desc; 39095b482a8SLen Brown 39195b482a8SLen Brown cleanup: 39295b482a8SLen Brown if (local_operand1 != operand1) { 39395b482a8SLen Brown acpi_ut_remove_reference(local_operand1); 39495b482a8SLen Brown } 39595b482a8SLen Brown return_ACPI_STATUS(status); 39695b482a8SLen Brown } 39795b482a8SLen Brown 39895b482a8SLen Brown /******************************************************************************* 39995b482a8SLen Brown * 40095b482a8SLen Brown * FUNCTION: acpi_ex_do_math_op 40195b482a8SLen Brown * 402ba494beeSBob Moore * PARAMETERS: opcode - AML opcode 403ba494beeSBob Moore * integer0 - Integer operand #0 404ba494beeSBob Moore * integer1 - Integer operand #1 40595b482a8SLen Brown * 40695b482a8SLen Brown * RETURN: Integer result of the operation 40795b482a8SLen Brown * 40895b482a8SLen Brown * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the 40995b482a8SLen Brown * math functions here is to prevent a lot of pointer dereferencing 41095b482a8SLen Brown * to obtain the operands. 41195b482a8SLen Brown * 41295b482a8SLen Brown ******************************************************************************/ 41395b482a8SLen Brown 4145df7e6cbSBob Moore u64 acpi_ex_do_math_op(u16 opcode, u64 integer0, u64 integer1) 41595b482a8SLen Brown { 41695b482a8SLen Brown 41795b482a8SLen Brown ACPI_FUNCTION_ENTRY(); 41895b482a8SLen Brown 41995b482a8SLen Brown switch (opcode) { 42095b482a8SLen Brown case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */ 42195b482a8SLen Brown 42295b482a8SLen Brown return (integer0 + integer1); 42395b482a8SLen Brown 42495b482a8SLen Brown case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */ 42595b482a8SLen Brown 42695b482a8SLen Brown return (integer0 & integer1); 42795b482a8SLen Brown 42895b482a8SLen Brown case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */ 42995b482a8SLen Brown 43095b482a8SLen Brown return (~(integer0 & integer1)); 43195b482a8SLen Brown 43295b482a8SLen Brown case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */ 43395b482a8SLen Brown 43495b482a8SLen Brown return (integer0 | integer1); 43595b482a8SLen Brown 43695b482a8SLen Brown case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */ 43795b482a8SLen Brown 43895b482a8SLen Brown return (~(integer0 | integer1)); 43995b482a8SLen Brown 44095b482a8SLen Brown case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */ 44195b482a8SLen Brown 44295b482a8SLen Brown return (integer0 ^ integer1); 44395b482a8SLen Brown 44495b482a8SLen Brown case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */ 44595b482a8SLen Brown 44695b482a8SLen Brown return (integer0 * integer1); 44795b482a8SLen Brown 44895b482a8SLen Brown case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */ 44995b482a8SLen Brown 45095b482a8SLen Brown /* 45195b482a8SLen Brown * We need to check if the shiftcount is larger than the integer bit 45295b482a8SLen Brown * width since the behavior of this is not well-defined in the C language. 45395b482a8SLen Brown */ 45495b482a8SLen Brown if (integer1 >= acpi_gbl_integer_bit_width) { 45595b482a8SLen Brown return (0); 45695b482a8SLen Brown } 45795b482a8SLen Brown return (integer0 << integer1); 45895b482a8SLen Brown 45995b482a8SLen Brown case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */ 46095b482a8SLen Brown 46195b482a8SLen Brown /* 46295b482a8SLen Brown * We need to check if the shiftcount is larger than the integer bit 46395b482a8SLen Brown * width since the behavior of this is not well-defined in the C language. 46495b482a8SLen Brown */ 46595b482a8SLen Brown if (integer1 >= acpi_gbl_integer_bit_width) { 46695b482a8SLen Brown return (0); 46795b482a8SLen Brown } 46895b482a8SLen Brown return (integer0 >> integer1); 46995b482a8SLen Brown 47095b482a8SLen Brown case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */ 47195b482a8SLen Brown 47295b482a8SLen Brown return (integer0 - integer1); 47395b482a8SLen Brown 47495b482a8SLen Brown default: 47595b482a8SLen Brown 47695b482a8SLen Brown return (0); 47795b482a8SLen Brown } 47895b482a8SLen Brown } 47995b482a8SLen Brown 48095b482a8SLen Brown /******************************************************************************* 48195b482a8SLen Brown * 48295b482a8SLen Brown * FUNCTION: acpi_ex_do_logical_numeric_op 48395b482a8SLen Brown * 484ba494beeSBob Moore * PARAMETERS: opcode - AML opcode 485ba494beeSBob Moore * integer0 - Integer operand #0 486ba494beeSBob Moore * integer1 - Integer operand #1 48795b482a8SLen Brown * logical_result - TRUE/FALSE result of the operation 48895b482a8SLen Brown * 48995b482a8SLen Brown * RETURN: Status 49095b482a8SLen Brown * 49195b482a8SLen Brown * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric 49295b482a8SLen Brown * operators (LAnd and LOr), both operands must be integers. 49395b482a8SLen Brown * 49495b482a8SLen Brown * Note: cleanest machine code seems to be produced by the code 49595b482a8SLen Brown * below, rather than using statements of the form: 49695b482a8SLen Brown * Result = (Integer0 && Integer1); 49795b482a8SLen Brown * 49895b482a8SLen Brown ******************************************************************************/ 49995b482a8SLen Brown 50095b482a8SLen Brown acpi_status 50195b482a8SLen Brown acpi_ex_do_logical_numeric_op(u16 opcode, 5025df7e6cbSBob Moore u64 integer0, u64 integer1, u8 *logical_result) 50395b482a8SLen Brown { 50495b482a8SLen Brown acpi_status status = AE_OK; 50595b482a8SLen Brown u8 local_result = FALSE; 50695b482a8SLen Brown 50795b482a8SLen Brown ACPI_FUNCTION_TRACE(ex_do_logical_numeric_op); 50895b482a8SLen Brown 50995b482a8SLen Brown switch (opcode) { 51095b482a8SLen Brown case AML_LAND_OP: /* LAnd (Integer0, Integer1) */ 51195b482a8SLen Brown 51295b482a8SLen Brown if (integer0 && integer1) { 51395b482a8SLen Brown local_result = TRUE; 51495b482a8SLen Brown } 51595b482a8SLen Brown break; 51695b482a8SLen Brown 51795b482a8SLen Brown case AML_LOR_OP: /* LOr (Integer0, Integer1) */ 51895b482a8SLen Brown 51995b482a8SLen Brown if (integer0 || integer1) { 52095b482a8SLen Brown local_result = TRUE; 52195b482a8SLen Brown } 52295b482a8SLen Brown break; 52395b482a8SLen Brown 52495b482a8SLen Brown default: 525*1d1ea1b7SChao Guan 52695b482a8SLen Brown status = AE_AML_INTERNAL; 52795b482a8SLen Brown break; 52895b482a8SLen Brown } 52995b482a8SLen Brown 53095b482a8SLen Brown /* Return the logical result and status */ 53195b482a8SLen Brown 53295b482a8SLen Brown *logical_result = local_result; 53395b482a8SLen Brown return_ACPI_STATUS(status); 53495b482a8SLen Brown } 53595b482a8SLen Brown 53695b482a8SLen Brown /******************************************************************************* 53795b482a8SLen Brown * 53895b482a8SLen Brown * FUNCTION: acpi_ex_do_logical_op 53995b482a8SLen Brown * 540ba494beeSBob Moore * PARAMETERS: opcode - AML opcode 541ba494beeSBob Moore * operand0 - operand #0 542ba494beeSBob Moore * operand1 - operand #1 54395b482a8SLen Brown * logical_result - TRUE/FALSE result of the operation 54495b482a8SLen Brown * 54595b482a8SLen Brown * RETURN: Status 54695b482a8SLen Brown * 54795b482a8SLen Brown * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the 54895b482a8SLen Brown * functions here is to prevent a lot of pointer dereferencing 54995b482a8SLen Brown * to obtain the operands and to simplify the generation of the 55095b482a8SLen Brown * logical value. For the Numeric operators (LAnd and LOr), both 55195b482a8SLen Brown * operands must be integers. For the other logical operators, 55295b482a8SLen Brown * operands can be any combination of Integer/String/Buffer. The 55395b482a8SLen Brown * first operand determines the type to which the second operand 55495b482a8SLen Brown * will be converted. 55595b482a8SLen Brown * 55695b482a8SLen Brown * Note: cleanest machine code seems to be produced by the code 55795b482a8SLen Brown * below, rather than using statements of the form: 55895b482a8SLen Brown * Result = (Operand0 == Operand1); 55995b482a8SLen Brown * 56095b482a8SLen Brown ******************************************************************************/ 56195b482a8SLen Brown 56295b482a8SLen Brown acpi_status 56395b482a8SLen Brown acpi_ex_do_logical_op(u16 opcode, 56495b482a8SLen Brown union acpi_operand_object *operand0, 56595b482a8SLen Brown union acpi_operand_object *operand1, u8 * logical_result) 56695b482a8SLen Brown { 56795b482a8SLen Brown union acpi_operand_object *local_operand1 = operand1; 5685df7e6cbSBob Moore u64 integer0; 5695df7e6cbSBob Moore u64 integer1; 57095b482a8SLen Brown u32 length0; 57195b482a8SLen Brown u32 length1; 57295b482a8SLen Brown acpi_status status = AE_OK; 57395b482a8SLen Brown u8 local_result = FALSE; 57495b482a8SLen Brown int compare; 57595b482a8SLen Brown 57695b482a8SLen Brown ACPI_FUNCTION_TRACE(ex_do_logical_op); 57795b482a8SLen Brown 57895b482a8SLen Brown /* 57995b482a8SLen Brown * Convert the second operand if necessary. The first operand 58095b482a8SLen Brown * determines the type of the second operand, (See the Data Types 58195b482a8SLen Brown * section of the ACPI 3.0+ specification.) Both object types are 58295b482a8SLen Brown * guaranteed to be either Integer/String/Buffer by the operand 58395b482a8SLen Brown * resolution mechanism. 58495b482a8SLen Brown */ 5853371c19cSBob Moore switch (operand0->common.type) { 58695b482a8SLen Brown case ACPI_TYPE_INTEGER: 587*1d1ea1b7SChao Guan 58895b482a8SLen Brown status = 58995b482a8SLen Brown acpi_ex_convert_to_integer(operand1, &local_operand1, 16); 59095b482a8SLen Brown break; 59195b482a8SLen Brown 59295b482a8SLen Brown case ACPI_TYPE_STRING: 593*1d1ea1b7SChao Guan 59495b482a8SLen Brown status = acpi_ex_convert_to_string(operand1, &local_operand1, 59595b482a8SLen Brown ACPI_IMPLICIT_CONVERT_HEX); 59695b482a8SLen Brown break; 59795b482a8SLen Brown 59895b482a8SLen Brown case ACPI_TYPE_BUFFER: 599*1d1ea1b7SChao Guan 60095b482a8SLen Brown status = acpi_ex_convert_to_buffer(operand1, &local_operand1); 60195b482a8SLen Brown break; 60295b482a8SLen Brown 60395b482a8SLen Brown default: 604*1d1ea1b7SChao Guan 60595b482a8SLen Brown status = AE_AML_INTERNAL; 60695b482a8SLen Brown break; 60795b482a8SLen Brown } 60895b482a8SLen Brown 60995b482a8SLen Brown if (ACPI_FAILURE(status)) { 61095b482a8SLen Brown goto cleanup; 61195b482a8SLen Brown } 61295b482a8SLen Brown 61395b482a8SLen Brown /* 61495b482a8SLen Brown * Two cases: 1) Both Integers, 2) Both Strings or Buffers 61595b482a8SLen Brown */ 6163371c19cSBob Moore if (operand0->common.type == ACPI_TYPE_INTEGER) { 61795b482a8SLen Brown /* 61895b482a8SLen Brown * 1) Both operands are of type integer 61995b482a8SLen Brown * Note: local_operand1 may have changed above 62095b482a8SLen Brown */ 62195b482a8SLen Brown integer0 = operand0->integer.value; 62295b482a8SLen Brown integer1 = local_operand1->integer.value; 62395b482a8SLen Brown 62495b482a8SLen Brown switch (opcode) { 62595b482a8SLen Brown case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */ 62695b482a8SLen Brown 62795b482a8SLen Brown if (integer0 == integer1) { 62895b482a8SLen Brown local_result = TRUE; 62995b482a8SLen Brown } 63095b482a8SLen Brown break; 63195b482a8SLen Brown 63295b482a8SLen Brown case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */ 63395b482a8SLen Brown 63495b482a8SLen Brown if (integer0 > integer1) { 63595b482a8SLen Brown local_result = TRUE; 63695b482a8SLen Brown } 63795b482a8SLen Brown break; 63895b482a8SLen Brown 63995b482a8SLen Brown case AML_LLESS_OP: /* LLess (Operand0, Operand1) */ 64095b482a8SLen Brown 64195b482a8SLen Brown if (integer0 < integer1) { 64295b482a8SLen Brown local_result = TRUE; 64395b482a8SLen Brown } 64495b482a8SLen Brown break; 64595b482a8SLen Brown 64695b482a8SLen Brown default: 647*1d1ea1b7SChao Guan 64895b482a8SLen Brown status = AE_AML_INTERNAL; 64995b482a8SLen Brown break; 65095b482a8SLen Brown } 65195b482a8SLen Brown } else { 65295b482a8SLen Brown /* 65395b482a8SLen Brown * 2) Both operands are Strings or both are Buffers 65495b482a8SLen Brown * Note: Code below takes advantage of common Buffer/String 65595b482a8SLen Brown * object fields. local_operand1 may have changed above. Use 65695b482a8SLen Brown * memcmp to handle nulls in buffers. 65795b482a8SLen Brown */ 65895b482a8SLen Brown length0 = operand0->buffer.length; 65995b482a8SLen Brown length1 = local_operand1->buffer.length; 66095b482a8SLen Brown 66195b482a8SLen Brown /* Lexicographic compare: compare the data bytes */ 66295b482a8SLen Brown 66395b482a8SLen Brown compare = ACPI_MEMCMP(operand0->buffer.pointer, 66495b482a8SLen Brown local_operand1->buffer.pointer, 66595b482a8SLen Brown (length0 > length1) ? length1 : length0); 66695b482a8SLen Brown 66795b482a8SLen Brown switch (opcode) { 66895b482a8SLen Brown case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */ 66995b482a8SLen Brown 67095b482a8SLen Brown /* Length and all bytes must be equal */ 67195b482a8SLen Brown 67295b482a8SLen Brown if ((length0 == length1) && (compare == 0)) { 67395b482a8SLen Brown 67495b482a8SLen Brown /* Length and all bytes match ==> TRUE */ 67595b482a8SLen Brown 67695b482a8SLen Brown local_result = TRUE; 67795b482a8SLen Brown } 67895b482a8SLen Brown break; 67995b482a8SLen Brown 68095b482a8SLen Brown case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */ 68195b482a8SLen Brown 68295b482a8SLen Brown if (compare > 0) { 68395b482a8SLen Brown local_result = TRUE; 68495b482a8SLen Brown goto cleanup; /* TRUE */ 68595b482a8SLen Brown } 68695b482a8SLen Brown if (compare < 0) { 68795b482a8SLen Brown goto cleanup; /* FALSE */ 68895b482a8SLen Brown } 68995b482a8SLen Brown 69095b482a8SLen Brown /* Bytes match (to shortest length), compare lengths */ 69195b482a8SLen Brown 69295b482a8SLen Brown if (length0 > length1) { 69395b482a8SLen Brown local_result = TRUE; 69495b482a8SLen Brown } 69595b482a8SLen Brown break; 69695b482a8SLen Brown 69795b482a8SLen Brown case AML_LLESS_OP: /* LLess (Operand0, Operand1) */ 69895b482a8SLen Brown 69995b482a8SLen Brown if (compare > 0) { 70095b482a8SLen Brown goto cleanup; /* FALSE */ 70195b482a8SLen Brown } 70295b482a8SLen Brown if (compare < 0) { 70395b482a8SLen Brown local_result = TRUE; 70495b482a8SLen Brown goto cleanup; /* TRUE */ 70595b482a8SLen Brown } 70695b482a8SLen Brown 70795b482a8SLen Brown /* Bytes match (to shortest length), compare lengths */ 70895b482a8SLen Brown 70995b482a8SLen Brown if (length0 < length1) { 71095b482a8SLen Brown local_result = TRUE; 71195b482a8SLen Brown } 71295b482a8SLen Brown break; 71395b482a8SLen Brown 71495b482a8SLen Brown default: 715*1d1ea1b7SChao Guan 71695b482a8SLen Brown status = AE_AML_INTERNAL; 71795b482a8SLen Brown break; 71895b482a8SLen Brown } 71995b482a8SLen Brown } 72095b482a8SLen Brown 72195b482a8SLen Brown cleanup: 72295b482a8SLen Brown 72395b482a8SLen Brown /* New object was created if implicit conversion performed - delete */ 72495b482a8SLen Brown 72595b482a8SLen Brown if (local_operand1 != operand1) { 72695b482a8SLen Brown acpi_ut_remove_reference(local_operand1); 72795b482a8SLen Brown } 72895b482a8SLen Brown 72995b482a8SLen Brown /* Return the logical result and status */ 73095b482a8SLen Brown 73195b482a8SLen Brown *logical_result = local_result; 73295b482a8SLen Brown return_ACPI_STATUS(status); 73395b482a8SLen Brown } 734