blob: 95db4be0877baeb2acd0a664ecdf46cc38ffeb68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/******************************************************************************
3 *
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5 *
6 *****************************************************************************/
7
8/*
Bob Moorea8357b02010-01-22 19:07:36 +08009 * Copyright (C) 2000 - 2010, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * All rights reserved.
11 *
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.
26 *
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.
30 *
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.
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "acinterp.h"
48#include "amlcode.h"
49#include "amlresrc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define _COMPONENT ACPI_EXECUTER
Len Brown4be44fc2005-08-05 00:44:28 -040052ACPI_MODULE_NAME("exmisc")
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*******************************************************************************
55 *
56 * FUNCTION: acpi_ex_get_object_reference
57 *
58 * PARAMETERS: obj_desc - Create a reference to this object
59 * return_desc - Where to store the reference
60 * walk_state - Current state
61 *
62 * RETURN: Status
63 *
64 * DESCRIPTION: Obtain and return a "reference" to the target object
65 * Common code for the ref_of_op and the cond_ref_of_op.
66 *
67 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070068acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040069acpi_ex_get_object_reference(union acpi_operand_object *obj_desc,
70 union acpi_operand_object **return_desc,
71 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Len Brown4be44fc2005-08-05 00:44:28 -040073 union acpi_operand_object *reference_obj;
74 union acpi_operand_object *referenced_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Bob Mooreb229cf92006-04-21 17:15:00 -040076 ACPI_FUNCTION_TRACE_PTR(ex_get_object_reference, obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 *return_desc = NULL;
79
Len Brown4be44fc2005-08-05 00:44:28 -040080 switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 case ACPI_DESC_TYPE_OPERAND:
82
Bob Moore3371c192009-02-18 14:44:03 +080083 if (obj_desc->common.type != ACPI_TYPE_LOCAL_REFERENCE) {
Len Brown4be44fc2005-08-05 00:44:28 -040084 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87 /*
88 * Must be a reference to a Local or Arg
89 */
Bob Moore1044f1f2008-09-27 11:08:41 +080090 switch (obj_desc->reference.class) {
91 case ACPI_REFCLASS_LOCAL:
92 case ACPI_REFCLASS_ARG:
93 case ACPI_REFCLASS_DEBUG:
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /* The referenced object is the pseudo-node for the local/arg */
96
97 referenced_obj = obj_desc->reference.object;
98 break;
99
100 default:
101
Bob Mooref6a22b02010-03-05 17:56:40 +0800102 ACPI_ERROR((AE_INFO, "Unknown Reference Class 0x%2.2X",
Bob Moore1044f1f2008-09-27 11:08:41 +0800103 obj_desc->reference.class));
Len Brown4be44fc2005-08-05 00:44:28 -0400104 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106 break;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 case ACPI_DESC_TYPE_NAMED:
109
110 /*
111 * A named reference that has already been resolved to a Node
112 */
113 referenced_obj = obj_desc;
114 break;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 default:
117
Bob Mooref6a22b02010-03-05 17:56:40 +0800118 ACPI_ERROR((AE_INFO, "Invalid descriptor type 0x%X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500119 ACPI_GET_DESCRIPTOR_TYPE(obj_desc)));
Len Brown4be44fc2005-08-05 00:44:28 -0400120 return_ACPI_STATUS(AE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* Create a new reference object */
124
Len Brown4be44fc2005-08-05 00:44:28 -0400125 reference_obj =
126 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (!reference_obj) {
Len Brown4be44fc2005-08-05 00:44:28 -0400128 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
Bob Moore1044f1f2008-09-27 11:08:41 +0800131 reference_obj->reference.class = ACPI_REFCLASS_REFOF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 reference_obj->reference.object = referenced_obj;
133 *return_desc = reference_obj;
134
Len Brown4be44fc2005-08-05 00:44:28 -0400135 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
136 "Object %p Type [%s], returning Reference %p\n",
137 obj_desc, acpi_ut_get_object_type_name(obj_desc),
138 *return_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Len Brown4be44fc2005-08-05 00:44:28 -0400140 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*******************************************************************************
144 *
145 * FUNCTION: acpi_ex_concat_template
146 *
147 * PARAMETERS: Operand0 - First source object
148 * Operand1 - Second source object
149 * actual_return_desc - Where to place the return object
150 * walk_state - Current walk state
151 *
152 * RETURN: Status
153 *
154 * DESCRIPTION: Concatenate two resource templates
155 *
156 ******************************************************************************/
157
158acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400159acpi_ex_concat_template(union acpi_operand_object *operand0,
160 union acpi_operand_object *operand1,
161 union acpi_operand_object **actual_return_desc,
162 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Bob Moore96db2552005-11-02 00:00:00 -0500164 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400165 union acpi_operand_object *return_desc;
166 u8 *new_buf;
Bob Moore96db2552005-11-02 00:00:00 -0500167 u8 *end_tag;
168 acpi_size length0;
Len Brown4be44fc2005-08-05 00:44:28 -0400169 acpi_size length1;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500170 acpi_size new_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Bob Mooreb229cf92006-04-21 17:15:00 -0400172 ACPI_FUNCTION_TRACE(ex_concat_template);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Bob Moore96db2552005-11-02 00:00:00 -0500174 /*
175 * Find the end_tag descriptor in each resource template.
Bob Mooreb8e4d892006-01-27 16:43:00 -0500176 * Note1: returned pointers point TO the end_tag, not past it.
177 * Note2: zero-length buffers are allowed; treated like one end_tag
Bob Moore96db2552005-11-02 00:00:00 -0500178 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500179
180 /* Get the length of the first resource template */
181
Bob Moore96db2552005-11-02 00:00:00 -0500182 status = acpi_ut_get_resource_end_tag(operand0, &end_tag);
183 if (ACPI_FAILURE(status)) {
184 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
Bob Moore96db2552005-11-02 00:00:00 -0500187 length0 = ACPI_PTR_DIFF(end_tag, operand0->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Bob Mooreb8e4d892006-01-27 16:43:00 -0500189 /* Get the length of the second resource template */
190
Bob Moore96db2552005-11-02 00:00:00 -0500191 status = acpi_ut_get_resource_end_tag(operand1, &end_tag);
192 if (ACPI_FAILURE(status)) {
193 return_ACPI_STATUS(status);
194 }
195
Bob Mooreb8e4d892006-01-27 16:43:00 -0500196 length1 = ACPI_PTR_DIFF(end_tag, operand1->buffer.pointer);
Bob Moore96db2552005-11-02 00:00:00 -0500197
Bob Mooreb8e4d892006-01-27 16:43:00 -0500198 /* Combine both lengths, minimum size will be 2 for end_tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Bob Mooreb8e4d892006-01-27 16:43:00 -0500200 new_length = length0 + length1 + sizeof(struct aml_resource_end_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Bob Mooreb8e4d892006-01-27 16:43:00 -0500202 /* Create a new buffer object for the result (with one end_tag) */
203
204 return_desc = acpi_ut_create_buffer_object(new_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!return_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400206 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Bob Moore96db2552005-11-02 00:00:00 -0500209 /*
210 * Copy the templates to the new buffer, 0 first, then 1 follows. One
211 * end_tag descriptor is copied from Operand1.
212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 new_buf = return_desc->buffer.pointer;
Bob Moore96db2552005-11-02 00:00:00 -0500214 ACPI_MEMCPY(new_buf, operand0->buffer.pointer, length0);
215 ACPI_MEMCPY(new_buf + length0, operand1->buffer.pointer, length1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Bob Mooreb8e4d892006-01-27 16:43:00 -0500217 /* Insert end_tag and set the checksum to zero, means "ignore checksum" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Bob Mooreb8e4d892006-01-27 16:43:00 -0500219 new_buf[new_length - 1] = 0;
220 new_buf[new_length - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Bob Moore96db2552005-11-02 00:00:00 -0500222 /* Return the completed resource template */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 *actual_return_desc = return_desc;
Len Brown4be44fc2005-08-05 00:44:28 -0400225 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*******************************************************************************
229 *
230 * FUNCTION: acpi_ex_do_concatenate
231 *
232 * PARAMETERS: Operand0 - First source object
233 * Operand1 - Second source object
234 * actual_return_desc - Where to place the return object
235 * walk_state - Current walk state
236 *
237 * RETURN: Status
238 *
239 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
240 *
241 ******************************************************************************/
242
243acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400244acpi_ex_do_concatenate(union acpi_operand_object *operand0,
245 union acpi_operand_object *operand1,
246 union acpi_operand_object **actual_return_desc,
247 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Len Brown4be44fc2005-08-05 00:44:28 -0400249 union acpi_operand_object *local_operand1 = operand1;
250 union acpi_operand_object *return_desc;
251 char *new_buf;
252 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Bob Mooreb229cf92006-04-21 17:15:00 -0400254 ACPI_FUNCTION_TRACE(ex_do_concatenate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /*
257 * Convert the second operand if necessary. The first operand
258 * determines the type of the second operand, (See the Data Types
259 * section of the ACPI specification.) Both object types are
260 * guaranteed to be either Integer/String/Buffer by the operand
261 * resolution mechanism.
262 */
Bob Moore3371c192009-02-18 14:44:03 +0800263 switch (operand0->common.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 case ACPI_TYPE_INTEGER:
Len Brown4be44fc2005-08-05 00:44:28 -0400265 status =
266 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
268
269 case ACPI_TYPE_STRING:
Len Brown4be44fc2005-08-05 00:44:28 -0400270 status = acpi_ex_convert_to_string(operand1, &local_operand1,
271 ACPI_IMPLICIT_CONVERT_HEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
273
274 case ACPI_TYPE_BUFFER:
Len Brown4be44fc2005-08-05 00:44:28 -0400275 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277
278 default:
Bob Mooref6a22b02010-03-05 17:56:40 +0800279 ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X",
Bob Moore3371c192009-02-18 14:44:03 +0800280 operand0->common.type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 status = AE_AML_INTERNAL;
282 }
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto cleanup;
286 }
287
288 /*
289 * Both operands are now known to be the same object type
290 * (Both are Integer, String, or Buffer), and we can now perform the
291 * concatenation.
292 */
293
294 /*
295 * There are three cases to handle:
296 *
297 * 1) Two Integers concatenated to produce a new Buffer
298 * 2) Two Strings concatenated to produce a new String
299 * 3) Two Buffers concatenated to produce a new Buffer
300 */
Bob Moore3371c192009-02-18 14:44:03 +0800301 switch (operand0->common.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 case ACPI_TYPE_INTEGER:
303
304 /* Result of two Integers is a Buffer */
305 /* Need enough buffer space for two integers */
306
Len Brown4be44fc2005-08-05 00:44:28 -0400307 return_desc = acpi_ut_create_buffer_object((acpi_size)
308 ACPI_MUL_2
309 (acpi_gbl_integer_byte_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!return_desc) {
311 status = AE_NO_MEMORY;
312 goto cleanup;
313 }
314
Len Brown4be44fc2005-08-05 00:44:28 -0400315 new_buf = (char *)return_desc->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Copy the first integer, LSB first */
318
Bob Moorec51a4de2005-11-17 13:07:00 -0500319 ACPI_MEMCPY(new_buf, &operand0->integer.value,
Len Brown4be44fc2005-08-05 00:44:28 -0400320 acpi_gbl_integer_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 /* Copy the second integer (LSB first) after the first */
323
Len Brown4be44fc2005-08-05 00:44:28 -0400324 ACPI_MEMCPY(new_buf + acpi_gbl_integer_byte_width,
325 &local_operand1->integer.value,
326 acpi_gbl_integer_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328
329 case ACPI_TYPE_STRING:
330
331 /* Result of two Strings is a String */
332
Bob Moore67a119f2008-06-10 13:42:13 +0800333 return_desc = acpi_ut_create_string_object(((acpi_size)
334 operand0->string.
Bob Moorec51a4de2005-11-17 13:07:00 -0500335 length +
336 local_operand1->
337 string.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!return_desc) {
339 status = AE_NO_MEMORY;
340 goto cleanup;
341 }
342
343 new_buf = return_desc->string.pointer;
344
345 /* Concatenate the strings */
346
Len Brown4be44fc2005-08-05 00:44:28 -0400347 ACPI_STRCPY(new_buf, operand0->string.pointer);
348 ACPI_STRCPY(new_buf + operand0->string.length,
349 local_operand1->string.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351
352 case ACPI_TYPE_BUFFER:
353
354 /* Result of two Buffers is a Buffer */
355
Bob Moore67a119f2008-06-10 13:42:13 +0800356 return_desc = acpi_ut_create_buffer_object(((acpi_size)
357 operand0->buffer.
Bob Moorec51a4de2005-11-17 13:07:00 -0500358 length +
359 local_operand1->
360 buffer.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!return_desc) {
362 status = AE_NO_MEMORY;
363 goto cleanup;
364 }
365
Len Brown4be44fc2005-08-05 00:44:28 -0400366 new_buf = (char *)return_desc->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* Concatenate the buffers */
369
Bob Moorec51a4de2005-11-17 13:07:00 -0500370 ACPI_MEMCPY(new_buf, operand0->buffer.pointer,
371 operand0->buffer.length);
Len Brown4be44fc2005-08-05 00:44:28 -0400372 ACPI_MEMCPY(new_buf + operand0->buffer.length,
373 local_operand1->buffer.pointer,
374 local_operand1->buffer.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 break;
376
377 default:
378
379 /* Invalid object type, should not happen here */
380
Bob Mooref6a22b02010-03-05 17:56:40 +0800381 ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X",
Bob Moore3371c192009-02-18 14:44:03 +0800382 operand0->common.type));
Len Brown4be44fc2005-08-05 00:44:28 -0400383 status = AE_AML_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto cleanup;
385 }
386
387 *actual_return_desc = return_desc;
388
Len Brown4be44fc2005-08-05 00:44:28 -0400389 cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (local_operand1 != operand1) {
Len Brown4be44fc2005-08-05 00:44:28 -0400391 acpi_ut_remove_reference(local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Len Brown4be44fc2005-08-05 00:44:28 -0400393 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*******************************************************************************
397 *
398 * FUNCTION: acpi_ex_do_math_op
399 *
400 * PARAMETERS: Opcode - AML opcode
401 * Integer0 - Integer operand #0
402 * Integer1 - Integer operand #1
403 *
404 * RETURN: Integer result of the operation
405 *
406 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
407 * math functions here is to prevent a lot of pointer dereferencing
408 * to obtain the operands.
409 *
410 ******************************************************************************/
411
Bob Moore5df7e6c2010-01-21 10:06:32 +0800412u64 acpi_ex_do_math_op(u16 opcode, u64 integer0, u64 integer1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414
Len Brown4be44fc2005-08-05 00:44:28 -0400415 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400418 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 return (integer0 + integer1);
421
Len Brown4be44fc2005-08-05 00:44:28 -0400422 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 return (integer0 & integer1);
425
Len Brown4be44fc2005-08-05 00:44:28 -0400426 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 return (~(integer0 & integer1));
429
Len Brown4be44fc2005-08-05 00:44:28 -0400430 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return (integer0 | integer1);
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return (~(integer0 | integer1));
437
Len Brown4be44fc2005-08-05 00:44:28 -0400438 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 return (integer0 ^ integer1);
441
Len Brown4be44fc2005-08-05 00:44:28 -0400442 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return (integer0 * integer1);
445
Len Brown4be44fc2005-08-05 00:44:28 -0400446 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Bob Moore41195322006-05-26 16:36:00 -0400448 /*
449 * We need to check if the shiftcount is larger than the integer bit
450 * width since the behavior of this is not well-defined in the C language.
451 */
452 if (integer1 >= acpi_gbl_integer_bit_width) {
453 return (0);
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return (integer0 << integer1);
456
Len Brown4be44fc2005-08-05 00:44:28 -0400457 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Bob Moore41195322006-05-26 16:36:00 -0400459 /*
460 * We need to check if the shiftcount is larger than the integer bit
461 * width since the behavior of this is not well-defined in the C language.
462 */
463 if (integer1 >= acpi_gbl_integer_bit_width) {
464 return (0);
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return (integer0 >> integer1);
467
Len Brown4be44fc2005-08-05 00:44:28 -0400468 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 return (integer0 - integer1);
471
472 default:
473
474 return (0);
475 }
476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/*******************************************************************************
479 *
480 * FUNCTION: acpi_ex_do_logical_numeric_op
481 *
482 * PARAMETERS: Opcode - AML opcode
483 * Integer0 - Integer operand #0
484 * Integer1 - Integer operand #1
485 * logical_result - TRUE/FALSE result of the operation
486 *
487 * RETURN: Status
488 *
489 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
490 * operators (LAnd and LOr), both operands must be integers.
491 *
492 * Note: cleanest machine code seems to be produced by the code
493 * below, rather than using statements of the form:
494 * Result = (Integer0 && Integer1);
495 *
496 ******************************************************************************/
497
498acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400499acpi_ex_do_logical_numeric_op(u16 opcode,
Bob Moore5df7e6c2010-01-21 10:06:32 +0800500 u64 integer0, u64 integer1, u8 *logical_result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Len Brown4be44fc2005-08-05 00:44:28 -0400502 acpi_status status = AE_OK;
503 u8 local_result = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Bob Mooreb229cf92006-04-21 17:15:00 -0400505 ACPI_FUNCTION_TRACE(ex_do_logical_numeric_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400508 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (integer0 && integer1) {
511 local_result = TRUE;
512 }
513 break;
514
Len Brown4be44fc2005-08-05 00:44:28 -0400515 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (integer0 || integer1) {
518 local_result = TRUE;
519 }
520 break;
521
522 default:
523 status = AE_AML_INTERNAL;
524 break;
525 }
526
527 /* Return the logical result and status */
528
529 *logical_result = local_result;
Len Brown4be44fc2005-08-05 00:44:28 -0400530 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/*******************************************************************************
534 *
535 * FUNCTION: acpi_ex_do_logical_op
536 *
537 * PARAMETERS: Opcode - AML opcode
538 * Operand0 - operand #0
539 * Operand1 - operand #1
540 * logical_result - TRUE/FALSE result of the operation
541 *
542 * RETURN: Status
543 *
544 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
545 * functions here is to prevent a lot of pointer dereferencing
546 * to obtain the operands and to simplify the generation of the
547 * logical value. For the Numeric operators (LAnd and LOr), both
548 * operands must be integers. For the other logical operators,
549 * operands can be any combination of Integer/String/Buffer. The
550 * first operand determines the type to which the second operand
551 * will be converted.
552 *
553 * Note: cleanest machine code seems to be produced by the code
554 * below, rather than using statements of the form:
555 * Result = (Operand0 == Operand1);
556 *
557 ******************************************************************************/
558
559acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400560acpi_ex_do_logical_op(u16 opcode,
561 union acpi_operand_object *operand0,
562 union acpi_operand_object *operand1, u8 * logical_result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Len Brown4be44fc2005-08-05 00:44:28 -0400564 union acpi_operand_object *local_operand1 = operand1;
Bob Moore5df7e6c2010-01-21 10:06:32 +0800565 u64 integer0;
566 u64 integer1;
Len Brown4be44fc2005-08-05 00:44:28 -0400567 u32 length0;
568 u32 length1;
569 acpi_status status = AE_OK;
570 u8 local_result = FALSE;
571 int compare;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Bob Mooreb229cf92006-04-21 17:15:00 -0400573 ACPI_FUNCTION_TRACE(ex_do_logical_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /*
576 * Convert the second operand if necessary. The first operand
577 * determines the type of the second operand, (See the Data Types
578 * section of the ACPI 3.0+ specification.) Both object types are
579 * guaranteed to be either Integer/String/Buffer by the operand
580 * resolution mechanism.
581 */
Bob Moore3371c192009-02-18 14:44:03 +0800582 switch (operand0->common.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 case ACPI_TYPE_INTEGER:
Len Brown4be44fc2005-08-05 00:44:28 -0400584 status =
585 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
587
588 case ACPI_TYPE_STRING:
Len Brown4be44fc2005-08-05 00:44:28 -0400589 status = acpi_ex_convert_to_string(operand1, &local_operand1,
590 ACPI_IMPLICIT_CONVERT_HEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592
593 case ACPI_TYPE_BUFFER:
Len Brown4be44fc2005-08-05 00:44:28 -0400594 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596
597 default:
598 status = AE_AML_INTERNAL;
599 break;
600 }
601
Len Brown4be44fc2005-08-05 00:44:28 -0400602 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 goto cleanup;
604 }
605
606 /*
607 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
608 */
Bob Moore3371c192009-02-18 14:44:03 +0800609 if (operand0->common.type == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
611 * 1) Both operands are of type integer
612 * Note: local_operand1 may have changed above
613 */
614 integer0 = operand0->integer.value;
615 integer1 = local_operand1->integer.value;
616
617 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400618 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (integer0 == integer1) {
621 local_result = TRUE;
622 }
623 break;
624
Len Brown4be44fc2005-08-05 00:44:28 -0400625 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (integer0 > integer1) {
628 local_result = TRUE;
629 }
630 break;
631
Len Brown4be44fc2005-08-05 00:44:28 -0400632 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (integer0 < integer1) {
635 local_result = TRUE;
636 }
637 break;
638
639 default:
640 status = AE_AML_INTERNAL;
641 break;
642 }
Len Brown4be44fc2005-08-05 00:44:28 -0400643 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
645 * 2) Both operands are Strings or both are Buffers
646 * Note: Code below takes advantage of common Buffer/String
647 * object fields. local_operand1 may have changed above. Use
648 * memcmp to handle nulls in buffers.
649 */
650 length0 = operand0->buffer.length;
651 length1 = local_operand1->buffer.length;
652
653 /* Lexicographic compare: compare the data bytes */
654
Bob Moore08978312005-10-21 00:00:00 -0400655 compare = ACPI_MEMCMP(operand0->buffer.pointer,
656 local_operand1->buffer.pointer,
Len Brown4be44fc2005-08-05 00:44:28 -0400657 (length0 > length1) ? length1 : length0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400660 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /* Length and all bytes must be equal */
663
Len Brown4be44fc2005-08-05 00:44:28 -0400664 if ((length0 == length1) && (compare == 0)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* Length and all bytes match ==> TRUE */
667
668 local_result = TRUE;
669 }
670 break;
671
Len Brown4be44fc2005-08-05 00:44:28 -0400672 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (compare > 0) {
675 local_result = TRUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400676 goto cleanup; /* TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 if (compare < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400679 goto cleanup; /* FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 /* Bytes match (to shortest length), compare lengths */
683
684 if (length0 > length1) {
685 local_result = TRUE;
686 }
687 break;
688
Len Brown4be44fc2005-08-05 00:44:28 -0400689 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (compare > 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400692 goto cleanup; /* FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 if (compare < 0) {
695 local_result = TRUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400696 goto cleanup; /* TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 /* Bytes match (to shortest length), compare lengths */
700
701 if (length0 < length1) {
702 local_result = TRUE;
703 }
704 break;
705
706 default:
707 status = AE_AML_INTERNAL;
708 break;
709 }
710 }
711
Len Brown4be44fc2005-08-05 00:44:28 -0400712 cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* New object was created if implicit conversion performed - delete */
715
716 if (local_operand1 != operand1) {
Len Brown4be44fc2005-08-05 00:44:28 -0400717 acpi_ut_remove_reference(local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 /* Return the logical result and status */
721
722 *logical_result = local_result;
Len Brown4be44fc2005-08-05 00:44:28 -0400723 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}