blob: 48c18d29222a4c1891a8b3e55478d4304a92d2bb [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 Moore4a90c7e2006-01-13 16:22:00 -05009 * Copyright (C) 2000 - 2006, R. Byron Moore
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>
46#include <acpi/acinterp.h>
47#include <acpi/amlcode.h>
Bob Moore96db2552005-11-02 00:00:00 -050048#include <acpi/amlresrc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define _COMPONENT ACPI_EXECUTER
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("exmisc")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*******************************************************************************
54 *
55 * FUNCTION: acpi_ex_get_object_reference
56 *
57 * PARAMETERS: obj_desc - Create a reference to this object
58 * return_desc - Where to store the reference
59 * walk_state - Current state
60 *
61 * RETURN: Status
62 *
63 * DESCRIPTION: Obtain and return a "reference" to the target object
64 * Common code for the ref_of_op and the cond_ref_of_op.
65 *
66 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070067acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040068acpi_ex_get_object_reference(union acpi_operand_object *obj_desc,
69 union acpi_operand_object **return_desc,
70 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Len Brown4be44fc2005-08-05 00:44:28 -040072 union acpi_operand_object *reference_obj;
73 union acpi_operand_object *referenced_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Len Brown4be44fc2005-08-05 00:44:28 -040075 ACPI_FUNCTION_TRACE_PTR("ex_get_object_reference", obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 *return_desc = NULL;
78
Len Brown4be44fc2005-08-05 00:44:28 -040079 switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 case ACPI_DESC_TYPE_OPERAND:
81
Len Brown4be44fc2005-08-05 00:44:28 -040082 if (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_LOCAL_REFERENCE) {
83 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
86 /*
87 * Must be a reference to a Local or Arg
88 */
89 switch (obj_desc->reference.opcode) {
90 case AML_LOCAL_OP:
91 case AML_ARG_OP:
92 case AML_DEBUG_OP:
93
94 /* The referenced object is the pseudo-node for the local/arg */
95
96 referenced_obj = obj_desc->reference.object;
97 break;
98
99 default:
100
Bob Mooreb8e4d892006-01-27 16:43:00 -0500101 ACPI_ERROR((AE_INFO, "Unknown Reference opcode %X",
102 obj_desc->reference.opcode));
Len Brown4be44fc2005-08-05 00:44:28 -0400103 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 break;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 case ACPI_DESC_TYPE_NAMED:
108
109 /*
110 * A named reference that has already been resolved to a Node
111 */
112 referenced_obj = obj_desc;
113 break;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 default:
116
Bob Mooreb8e4d892006-01-27 16:43:00 -0500117 ACPI_ERROR((AE_INFO, "Invalid descriptor type %X",
118 ACPI_GET_DESCRIPTOR_TYPE(obj_desc)));
Len Brown4be44fc2005-08-05 00:44:28 -0400119 return_ACPI_STATUS(AE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* Create a new reference object */
123
Len Brown4be44fc2005-08-05 00:44:28 -0400124 reference_obj =
125 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (!reference_obj) {
Len Brown4be44fc2005-08-05 00:44:28 -0400127 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 reference_obj->reference.opcode = AML_REF_OF_OP;
131 reference_obj->reference.object = referenced_obj;
132 *return_desc = reference_obj;
133
Len Brown4be44fc2005-08-05 00:44:28 -0400134 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
135 "Object %p Type [%s], returning Reference %p\n",
136 obj_desc, acpi_ut_get_object_type_name(obj_desc),
137 *return_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Len Brown4be44fc2005-08-05 00:44:28 -0400139 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*******************************************************************************
143 *
144 * FUNCTION: acpi_ex_concat_template
145 *
146 * PARAMETERS: Operand0 - First source object
147 * Operand1 - Second source object
148 * actual_return_desc - Where to place the return object
149 * walk_state - Current walk state
150 *
151 * RETURN: Status
152 *
153 * DESCRIPTION: Concatenate two resource templates
154 *
155 ******************************************************************************/
156
157acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400158acpi_ex_concat_template(union acpi_operand_object *operand0,
159 union acpi_operand_object *operand1,
160 union acpi_operand_object **actual_return_desc,
161 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Bob Moore96db2552005-11-02 00:00:00 -0500163 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400164 union acpi_operand_object *return_desc;
165 u8 *new_buf;
Bob Moore96db2552005-11-02 00:00:00 -0500166 u8 *end_tag;
167 acpi_size length0;
Len Brown4be44fc2005-08-05 00:44:28 -0400168 acpi_size length1;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500169 acpi_size new_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Len Brown4be44fc2005-08-05 00:44:28 -0400171 ACPI_FUNCTION_TRACE("ex_concat_template");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Bob Moore96db2552005-11-02 00:00:00 -0500173 /*
174 * Find the end_tag descriptor in each resource template.
Bob Mooreb8e4d892006-01-27 16:43:00 -0500175 * Note1: returned pointers point TO the end_tag, not past it.
176 * Note2: zero-length buffers are allowed; treated like one end_tag
Bob Moore96db2552005-11-02 00:00:00 -0500177 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500178
179 /* Get the length of the first resource template */
180
Bob Moore96db2552005-11-02 00:00:00 -0500181 status = acpi_ut_get_resource_end_tag(operand0, &end_tag);
182 if (ACPI_FAILURE(status)) {
183 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185
Bob Moore96db2552005-11-02 00:00:00 -0500186 length0 = ACPI_PTR_DIFF(end_tag, operand0->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Bob Mooreb8e4d892006-01-27 16:43:00 -0500188 /* Get the length of the second resource template */
189
Bob Moore96db2552005-11-02 00:00:00 -0500190 status = acpi_ut_get_resource_end_tag(operand1, &end_tag);
191 if (ACPI_FAILURE(status)) {
192 return_ACPI_STATUS(status);
193 }
194
Bob Mooreb8e4d892006-01-27 16:43:00 -0500195 length1 = ACPI_PTR_DIFF(end_tag, operand1->buffer.pointer);
Bob Moore96db2552005-11-02 00:00:00 -0500196
Bob Mooreb8e4d892006-01-27 16:43:00 -0500197 /* Combine both lengths, minimum size will be 2 for end_tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Bob Mooreb8e4d892006-01-27 16:43:00 -0500199 new_length = length0 + length1 + sizeof(struct aml_resource_end_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Bob Mooreb8e4d892006-01-27 16:43:00 -0500201 /* Create a new buffer object for the result (with one end_tag) */
202
203 return_desc = acpi_ut_create_buffer_object(new_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (!return_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400205 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
Bob Moore96db2552005-11-02 00:00:00 -0500208 /*
209 * Copy the templates to the new buffer, 0 first, then 1 follows. One
210 * end_tag descriptor is copied from Operand1.
211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 new_buf = return_desc->buffer.pointer;
Bob Moore96db2552005-11-02 00:00:00 -0500213 ACPI_MEMCPY(new_buf, operand0->buffer.pointer, length0);
214 ACPI_MEMCPY(new_buf + length0, operand1->buffer.pointer, length1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Bob Mooreb8e4d892006-01-27 16:43:00 -0500216 /* Insert end_tag and set the checksum to zero, means "ignore checksum" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Bob Mooreb8e4d892006-01-27 16:43:00 -0500218 new_buf[new_length - 1] = 0;
219 new_buf[new_length - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Bob Moore96db2552005-11-02 00:00:00 -0500221 /* Return the completed resource template */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 *actual_return_desc = return_desc;
Len Brown4be44fc2005-08-05 00:44:28 -0400224 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*******************************************************************************
228 *
229 * FUNCTION: acpi_ex_do_concatenate
230 *
231 * PARAMETERS: Operand0 - First source object
232 * Operand1 - Second source object
233 * actual_return_desc - Where to place the return object
234 * walk_state - Current walk state
235 *
236 * RETURN: Status
237 *
238 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
239 *
240 ******************************************************************************/
241
242acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400243acpi_ex_do_concatenate(union acpi_operand_object *operand0,
244 union acpi_operand_object *operand1,
245 union acpi_operand_object **actual_return_desc,
246 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Len Brown4be44fc2005-08-05 00:44:28 -0400248 union acpi_operand_object *local_operand1 = operand1;
249 union acpi_operand_object *return_desc;
250 char *new_buf;
251 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Len Brown4be44fc2005-08-05 00:44:28 -0400253 ACPI_FUNCTION_TRACE("ex_do_concatenate");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /*
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.
261 */
Len Brown4be44fc2005-08-05 00:44:28 -0400262 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 case ACPI_TYPE_INTEGER:
Len Brown4be44fc2005-08-05 00:44:28 -0400264 status =
265 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267
268 case ACPI_TYPE_STRING:
Len Brown4be44fc2005-08-05 00:44:28 -0400269 status = acpi_ex_convert_to_string(operand1, &local_operand1,
270 ACPI_IMPLICIT_CONVERT_HEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 break;
272
273 case ACPI_TYPE_BUFFER:
Len Brown4be44fc2005-08-05 00:44:28 -0400274 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276
277 default:
Bob Mooreb8e4d892006-01-27 16:43:00 -0500278 ACPI_ERROR((AE_INFO, "Invalid object type: %X",
279 ACPI_GET_OBJECT_TYPE(operand0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 status = AE_AML_INTERNAL;
281 }
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 goto cleanup;
285 }
286
287 /*
288 * Both operands are now known to be the same object type
289 * (Both are Integer, String, or Buffer), and we can now perform the
290 * concatenation.
291 */
292
293 /*
294 * There are three cases to handle:
295 *
296 * 1) Two Integers concatenated to produce a new Buffer
297 * 2) Two Strings concatenated to produce a new String
298 * 3) Two Buffers concatenated to produce a new Buffer
299 */
Len Brown4be44fc2005-08-05 00:44:28 -0400300 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 case ACPI_TYPE_INTEGER:
302
303 /* Result of two Integers is a Buffer */
304 /* Need enough buffer space for two integers */
305
Len Brown4be44fc2005-08-05 00:44:28 -0400306 return_desc = acpi_ut_create_buffer_object((acpi_size)
307 ACPI_MUL_2
308 (acpi_gbl_integer_byte_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!return_desc) {
310 status = AE_NO_MEMORY;
311 goto cleanup;
312 }
313
Len Brown4be44fc2005-08-05 00:44:28 -0400314 new_buf = (char *)return_desc->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Copy the first integer, LSB first */
317
Bob Moorec51a4de2005-11-17 13:07:00 -0500318 ACPI_MEMCPY(new_buf, &operand0->integer.value,
Len Brown4be44fc2005-08-05 00:44:28 -0400319 acpi_gbl_integer_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* Copy the second integer (LSB first) after the first */
322
Len Brown4be44fc2005-08-05 00:44:28 -0400323 ACPI_MEMCPY(new_buf + acpi_gbl_integer_byte_width,
324 &local_operand1->integer.value,
325 acpi_gbl_integer_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327
328 case ACPI_TYPE_STRING:
329
330 /* Result of two Strings is a String */
331
Bob Moorec51a4de2005-11-17 13:07:00 -0500332 return_desc = acpi_ut_create_string_object((acpi_size)
333 (operand0->string.
334 length +
335 local_operand1->
336 string.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!return_desc) {
338 status = AE_NO_MEMORY;
339 goto cleanup;
340 }
341
342 new_buf = return_desc->string.pointer;
343
344 /* Concatenate the strings */
345
Len Brown4be44fc2005-08-05 00:44:28 -0400346 ACPI_STRCPY(new_buf, operand0->string.pointer);
347 ACPI_STRCPY(new_buf + operand0->string.length,
348 local_operand1->string.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350
351 case ACPI_TYPE_BUFFER:
352
353 /* Result of two Buffers is a Buffer */
354
Len Brown4be44fc2005-08-05 00:44:28 -0400355 return_desc = acpi_ut_create_buffer_object((acpi_size)
Bob Moorec51a4de2005-11-17 13:07:00 -0500356 (operand0->buffer.
357 length +
358 local_operand1->
359 buffer.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!return_desc) {
361 status = AE_NO_MEMORY;
362 goto cleanup;
363 }
364
Len Brown4be44fc2005-08-05 00:44:28 -0400365 new_buf = (char *)return_desc->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* Concatenate the buffers */
368
Bob Moorec51a4de2005-11-17 13:07:00 -0500369 ACPI_MEMCPY(new_buf, operand0->buffer.pointer,
370 operand0->buffer.length);
Len Brown4be44fc2005-08-05 00:44:28 -0400371 ACPI_MEMCPY(new_buf + operand0->buffer.length,
372 local_operand1->buffer.pointer,
373 local_operand1->buffer.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 break;
375
376 default:
377
378 /* Invalid object type, should not happen here */
379
Bob Mooreb8e4d892006-01-27 16:43:00 -0500380 ACPI_ERROR((AE_INFO, "Invalid object type: %X",
381 ACPI_GET_OBJECT_TYPE(operand0)));
Len Brown4be44fc2005-08-05 00:44:28 -0400382 status = AE_AML_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto cleanup;
384 }
385
386 *actual_return_desc = return_desc;
387
Len Brown4be44fc2005-08-05 00:44:28 -0400388 cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (local_operand1 != operand1) {
Len Brown4be44fc2005-08-05 00:44:28 -0400390 acpi_ut_remove_reference(local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Len Brown4be44fc2005-08-05 00:44:28 -0400392 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/*******************************************************************************
396 *
397 * FUNCTION: acpi_ex_do_math_op
398 *
399 * PARAMETERS: Opcode - AML opcode
400 * Integer0 - Integer operand #0
401 * Integer1 - Integer operand #1
402 *
403 * RETURN: Integer result of the operation
404 *
405 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
406 * math functions here is to prevent a lot of pointer dereferencing
407 * to obtain the operands.
408 *
409 ******************************************************************************/
410
411acpi_integer
Len Brown4be44fc2005-08-05 00:44:28 -0400412acpi_ex_do_math_op(u16 opcode, acpi_integer integer0, acpi_integer 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
448 return (integer0 << integer1);
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return (integer0 >> integer1);
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 return (integer0 - integer1);
457
458 default:
459
460 return (0);
461 }
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*******************************************************************************
465 *
466 * FUNCTION: acpi_ex_do_logical_numeric_op
467 *
468 * PARAMETERS: Opcode - AML opcode
469 * Integer0 - Integer operand #0
470 * Integer1 - Integer operand #1
471 * logical_result - TRUE/FALSE result of the operation
472 *
473 * RETURN: Status
474 *
475 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
476 * operators (LAnd and LOr), both operands must be integers.
477 *
478 * Note: cleanest machine code seems to be produced by the code
479 * below, rather than using statements of the form:
480 * Result = (Integer0 && Integer1);
481 *
482 ******************************************************************************/
483
484acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400485acpi_ex_do_logical_numeric_op(u16 opcode,
486 acpi_integer integer0,
487 acpi_integer integer1, u8 * logical_result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Len Brown4be44fc2005-08-05 00:44:28 -0400489 acpi_status status = AE_OK;
490 u8 local_result = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Len Brown4be44fc2005-08-05 00:44:28 -0400492 ACPI_FUNCTION_TRACE("ex_do_logical_numeric_op");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400495 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (integer0 && integer1) {
498 local_result = TRUE;
499 }
500 break;
501
Len Brown4be44fc2005-08-05 00:44:28 -0400502 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (integer0 || integer1) {
505 local_result = TRUE;
506 }
507 break;
508
509 default:
510 status = AE_AML_INTERNAL;
511 break;
512 }
513
514 /* Return the logical result and status */
515
516 *logical_result = local_result;
Len Brown4be44fc2005-08-05 00:44:28 -0400517 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*******************************************************************************
521 *
522 * FUNCTION: acpi_ex_do_logical_op
523 *
524 * PARAMETERS: Opcode - AML opcode
525 * Operand0 - operand #0
526 * Operand1 - operand #1
527 * logical_result - TRUE/FALSE result of the operation
528 *
529 * RETURN: Status
530 *
531 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
532 * functions here is to prevent a lot of pointer dereferencing
533 * to obtain the operands and to simplify the generation of the
534 * logical value. For the Numeric operators (LAnd and LOr), both
535 * operands must be integers. For the other logical operators,
536 * operands can be any combination of Integer/String/Buffer. The
537 * first operand determines the type to which the second operand
538 * will be converted.
539 *
540 * Note: cleanest machine code seems to be produced by the code
541 * below, rather than using statements of the form:
542 * Result = (Operand0 == Operand1);
543 *
544 ******************************************************************************/
545
546acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400547acpi_ex_do_logical_op(u16 opcode,
548 union acpi_operand_object *operand0,
549 union acpi_operand_object *operand1, u8 * logical_result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Len Brown4be44fc2005-08-05 00:44:28 -0400551 union acpi_operand_object *local_operand1 = operand1;
552 acpi_integer integer0;
553 acpi_integer integer1;
554 u32 length0;
555 u32 length1;
556 acpi_status status = AE_OK;
557 u8 local_result = FALSE;
558 int compare;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Len Brown4be44fc2005-08-05 00:44:28 -0400560 ACPI_FUNCTION_TRACE("ex_do_logical_op");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /*
563 * Convert the second operand if necessary. The first operand
564 * determines the type of the second operand, (See the Data Types
565 * section of the ACPI 3.0+ specification.) Both object types are
566 * guaranteed to be either Integer/String/Buffer by the operand
567 * resolution mechanism.
568 */
Len Brown4be44fc2005-08-05 00:44:28 -0400569 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 case ACPI_TYPE_INTEGER:
Len Brown4be44fc2005-08-05 00:44:28 -0400571 status =
572 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574
575 case ACPI_TYPE_STRING:
Len Brown4be44fc2005-08-05 00:44:28 -0400576 status = acpi_ex_convert_to_string(operand1, &local_operand1,
577 ACPI_IMPLICIT_CONVERT_HEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579
580 case ACPI_TYPE_BUFFER:
Len Brown4be44fc2005-08-05 00:44:28 -0400581 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583
584 default:
585 status = AE_AML_INTERNAL;
586 break;
587 }
588
Len Brown4be44fc2005-08-05 00:44:28 -0400589 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto cleanup;
591 }
592
593 /*
594 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
595 */
Len Brown4be44fc2005-08-05 00:44:28 -0400596 if (ACPI_GET_OBJECT_TYPE(operand0) == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
598 * 1) Both operands are of type integer
599 * Note: local_operand1 may have changed above
600 */
601 integer0 = operand0->integer.value;
602 integer1 = local_operand1->integer.value;
603
604 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400605 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (integer0 == integer1) {
608 local_result = TRUE;
609 }
610 break;
611
Len Brown4be44fc2005-08-05 00:44:28 -0400612 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (integer0 > integer1) {
615 local_result = TRUE;
616 }
617 break;
618
Len Brown4be44fc2005-08-05 00:44:28 -0400619 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (integer0 < integer1) {
622 local_result = TRUE;
623 }
624 break;
625
626 default:
627 status = AE_AML_INTERNAL;
628 break;
629 }
Len Brown4be44fc2005-08-05 00:44:28 -0400630 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * 2) Both operands are Strings or both are Buffers
633 * Note: Code below takes advantage of common Buffer/String
634 * object fields. local_operand1 may have changed above. Use
635 * memcmp to handle nulls in buffers.
636 */
637 length0 = operand0->buffer.length;
638 length1 = local_operand1->buffer.length;
639
640 /* Lexicographic compare: compare the data bytes */
641
Bob Moore08978312005-10-21 00:00:00 -0400642 compare = ACPI_MEMCMP(operand0->buffer.pointer,
643 local_operand1->buffer.pointer,
Len Brown4be44fc2005-08-05 00:44:28 -0400644 (length0 > length1) ? length1 : length0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400647 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /* Length and all bytes must be equal */
650
Len Brown4be44fc2005-08-05 00:44:28 -0400651 if ((length0 == length1) && (compare == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Length and all bytes match ==> TRUE */
653
654 local_result = TRUE;
655 }
656 break;
657
Len Brown4be44fc2005-08-05 00:44:28 -0400658 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (compare > 0) {
661 local_result = TRUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400662 goto cleanup; /* TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 if (compare < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400665 goto cleanup; /* FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
668 /* Bytes match (to shortest length), compare lengths */
669
670 if (length0 > length1) {
671 local_result = TRUE;
672 }
673 break;
674
Len Brown4be44fc2005-08-05 00:44:28 -0400675 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (compare > 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400678 goto cleanup; /* FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 if (compare < 0) {
681 local_result = TRUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400682 goto cleanup; /* TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 /* Bytes match (to shortest length), compare lengths */
686
687 if (length0 < length1) {
688 local_result = TRUE;
689 }
690 break;
691
692 default:
693 status = AE_AML_INTERNAL;
694 break;
695 }
696 }
697
Len Brown4be44fc2005-08-05 00:44:28 -0400698 cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* New object was created if implicit conversion performed - delete */
701
702 if (local_operand1 != operand1) {
Len Brown4be44fc2005-08-05 00:44:28 -0400703 acpi_ut_remove_reference(local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 /* Return the logical result and status */
707
708 *logical_result = local_result;
Len Brown4be44fc2005-08-05 00:44:28 -0400709 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}