2 /******************************************************************************
4 * Module Name: exutils - interpreter/scanner utilities
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
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.
48 * DEFINE_AML_GLOBALS is tested in amlcode.h
49 * to determine whether certain global names should be "defined" or only
50 * "declared" in the current compilation. This enhances maintainability
51 * by enabling a single header file to embody all knowledge of the names
54 * Exactly one module of any executable should #define DEFINE_GLOBALS
55 * before #including the header files which use this convention. The
56 * names in question will be defined and initialized in that module,
57 * and declared as extern in all other modules which #include those
61 #define DEFINE_AML_GLOBALS
63 #include <contrib/dev/acpica/include/acpi.h>
64 #include <contrib/dev/acpica/include/accommon.h>
65 #include <contrib/dev/acpica/include/acinterp.h>
66 #include <contrib/dev/acpica/include/amlcode.h>
68 #define _COMPONENT ACPI_EXECUTER
69 ACPI_MODULE_NAME ("exutils")
71 /* Local prototypes */
79 #ifndef ACPI_NO_METHOD_EXECUTION
80 /*******************************************************************************
82 * FUNCTION: AcpiExEnterInterpreter
88 * DESCRIPTION: Enter the interpreter execution region. Failure to enter
89 * the interpreter region is a fatal system error. Used in
90 * conjunction with ExitInterpreter.
92 ******************************************************************************/
95 AcpiExEnterInterpreter (
101 ACPI_FUNCTION_TRACE (ExEnterInterpreter);
104 Status = AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER);
105 if (ACPI_FAILURE (Status))
107 ACPI_ERROR ((AE_INFO, "Could not acquire AML Interpreter mutex"));
114 /*******************************************************************************
116 * FUNCTION: AcpiExReacquireInterpreter
122 * DESCRIPTION: Reacquire the interpreter execution region from within the
123 * interpreter code. Failure to enter the interpreter region is a
124 * fatal system error. Used in conjuction with
125 * RelinquishInterpreter
127 ******************************************************************************/
130 AcpiExReacquireInterpreter (
133 ACPI_FUNCTION_TRACE (ExReacquireInterpreter);
137 * If the global serialized flag is set, do not release the interpreter,
138 * since it was not actually released by AcpiExRelinquishInterpreter.
139 * This forces the interpreter to be single threaded.
141 if (!AcpiGbl_AllMethodsSerialized)
143 AcpiExEnterInterpreter ();
150 /*******************************************************************************
152 * FUNCTION: AcpiExExitInterpreter
158 * DESCRIPTION: Exit the interpreter execution region. This is the top level
159 * routine used to exit the interpreter when all processing has
162 ******************************************************************************/
165 AcpiExExitInterpreter (
171 ACPI_FUNCTION_TRACE (ExExitInterpreter);
174 Status = AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER);
175 if (ACPI_FAILURE (Status))
177 ACPI_ERROR ((AE_INFO, "Could not release AML Interpreter mutex"));
184 /*******************************************************************************
186 * FUNCTION: AcpiExRelinquishInterpreter
192 * DESCRIPTION: Exit the interpreter execution region, from within the
193 * interpreter - before attempting an operation that will possibly
194 * block the running thread.
196 * Cases where the interpreter is unlocked internally
197 * 1) Method to be blocked on a Sleep() AML opcode
198 * 2) Method to be blocked on an Acquire() AML opcode
199 * 3) Method to be blocked on a Wait() AML opcode
200 * 4) Method to be blocked to acquire the global lock
201 * 5) Method to be blocked waiting to execute a serialized control method
202 * that is currently executing
203 * 6) About to invoke a user-installed opregion handler
205 ******************************************************************************/
208 AcpiExRelinquishInterpreter (
211 ACPI_FUNCTION_TRACE (ExRelinquishInterpreter);
215 * If the global serialized flag is set, do not release the interpreter.
216 * This forces the interpreter to be single threaded.
218 if (!AcpiGbl_AllMethodsSerialized)
220 AcpiExExitInterpreter ();
227 /*******************************************************************************
229 * FUNCTION: AcpiExTruncateFor32bitTable
231 * PARAMETERS: ObjDesc - Object to be truncated
235 * DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is
236 * 32-bit, as determined by the revision of the DSDT.
238 ******************************************************************************/
241 AcpiExTruncateFor32bitTable (
242 ACPI_OPERAND_OBJECT *ObjDesc)
245 ACPI_FUNCTION_ENTRY ();
249 * Object must be a valid number and we must be executing
250 * a control method. NS node could be there for AML_INT_NAMEPATH_OP.
253 (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) != ACPI_DESC_TYPE_OPERAND) ||
254 (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
259 if (AcpiGbl_IntegerByteWidth == 4)
262 * We are running a method that exists in a 32-bit ACPI table.
263 * Truncate the value to 32 bits by zeroing out the upper 32-bit field
265 ObjDesc->Integer.Value &= (UINT64) ACPI_UINT32_MAX;
270 /*******************************************************************************
272 * FUNCTION: AcpiExAcquireGlobalLock
274 * PARAMETERS: FieldFlags - Flags with Lock rule:
275 * AlwaysLock or NeverLock
279 * DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field
280 * flags specifiy that it is to be obtained before field access.
282 ******************************************************************************/
285 AcpiExAcquireGlobalLock (
291 ACPI_FUNCTION_TRACE (ExAcquireGlobalLock);
294 /* Only use the lock if the AlwaysLock bit is set */
296 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))
301 /* Attempt to get the global lock, wait forever */
303 Status = AcpiExAcquireMutexObject (ACPI_WAIT_FOREVER,
304 AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());
306 if (ACPI_FAILURE (Status))
308 ACPI_EXCEPTION ((AE_INFO, Status,
309 "Could not acquire Global Lock"));
316 /*******************************************************************************
318 * FUNCTION: AcpiExReleaseGlobalLock
320 * PARAMETERS: FieldFlags - Flags with Lock rule:
321 * AlwaysLock or NeverLock
325 * DESCRIPTION: Release the ACPI hardware Global Lock
327 ******************************************************************************/
330 AcpiExReleaseGlobalLock (
336 ACPI_FUNCTION_TRACE (ExReleaseGlobalLock);
339 /* Only use the lock if the AlwaysLock bit is set */
341 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))
346 /* Release the global lock */
348 Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);
349 if (ACPI_FAILURE (Status))
351 /* Report the error, but there isn't much else we can do */
353 ACPI_EXCEPTION ((AE_INFO, Status,
354 "Could not release Global Lock"));
361 /*******************************************************************************
363 * FUNCTION: AcpiExDigitsNeeded
365 * PARAMETERS: Value - Value to be represented
366 * Base - Base of representation
368 * RETURN: The number of digits.
370 * DESCRIPTION: Calculate the number of digits needed to represent the Value
371 * in the given Base (Radix)
373 ******************************************************************************/
384 ACPI_FUNCTION_TRACE (ExDigitsNeeded);
387 /* UINT64 is unsigned, so we don't worry about a '-' prefix */
394 CurrentValue = Value;
397 /* Count the digits in the requested base */
401 (void) AcpiUtShortDivide (CurrentValue, Base, &CurrentValue, NULL);
405 return_UINT32 (NumDigits);
409 /*******************************************************************************
411 * FUNCTION: AcpiExEisaIdToString
413 * PARAMETERS: CompressedId - EISAID to be converted
414 * OutString - Where to put the converted string (8 bytes)
418 * DESCRIPTION: Convert a numeric EISAID to string representation. Return
419 * buffer must be large enough to hold the string. The string
420 * returned is always exactly of length ACPI_EISAID_STRING_SIZE
421 * (includes null terminator). The EISAID is always 32 bits.
423 ******************************************************************************/
426 AcpiExEisaIdToString (
433 ACPI_FUNCTION_ENTRY ();
436 /* The EISAID should be a 32-bit integer */
438 if (CompressedId > ACPI_UINT32_MAX)
440 ACPI_WARNING ((AE_INFO,
441 "Expected EISAID is larger than 32 bits: 0x%8.8X%8.8X, truncating",
442 ACPI_FORMAT_UINT64 (CompressedId)));
445 /* Swap ID to big-endian to get contiguous bits */
447 SwappedId = AcpiUtDwordByteSwap ((UINT32) CompressedId);
449 /* First 3 bytes are uppercase letters. Next 4 bytes are hexadecimal */
451 OutString[0] = (char) (0x40 + (((unsigned long) SwappedId >> 26) & 0x1F));
452 OutString[1] = (char) (0x40 + ((SwappedId >> 21) & 0x1F));
453 OutString[2] = (char) (0x40 + ((SwappedId >> 16) & 0x1F));
454 OutString[3] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 12);
455 OutString[4] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 8);
456 OutString[5] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 4);
457 OutString[6] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 0);
462 /*******************************************************************************
464 * FUNCTION: AcpiExIntegerToString
466 * PARAMETERS: OutString - Where to put the converted string. At least
467 * 21 bytes are needed to hold the largest
468 * possible 64-bit integer.
469 * Value - Value to be converted
471 * RETURN: None, string
473 * DESCRIPTION: Convert a 64-bit integer to decimal string representation.
474 * Assumes string buffer is large enough to hold the string. The
475 * largest string is (ACPI_MAX64_DECIMAL_DIGITS + 1).
477 ******************************************************************************/
480 AcpiExIntegerToString (
489 ACPI_FUNCTION_ENTRY ();
492 DigitsNeeded = AcpiExDigitsNeeded (Value, 10);
493 OutString[DigitsNeeded] = 0;
495 for (Count = DigitsNeeded; Count > 0; Count--)
497 (void) AcpiUtShortDivide (Value, 10, &Value, &Remainder);
498 OutString[Count-1] = (char) ('0' + Remainder);\