2 /******************************************************************************
4 * Module Name: exutils - interpreter/scanner utilities
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2005, R. Byron Moore
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
46 * DEFINE_AML_GLOBALS is tested in amlcode.h
47 * to determine whether certain global names should be "defined" or only
48 * "declared" in the current compilation. This enhances maintainability
49 * by enabling a single header file to embody all knowledge of the names
52 * Exactly one module of any executable should #define DEFINE_GLOBALS
53 * before #including the header files which use this convention. The
54 * names in question will be defined and initialized in that module,
55 * and declared as extern in all other modules which #include those
59 #define DEFINE_AML_GLOBALS
61 #include <acpi/acpi.h>
62 #include <acpi/acinterp.h>
63 #include <acpi/amlcode.h>
64 #include <acpi/acevents.h>
66 #define _COMPONENT ACPI_EXECUTER
67 ACPI_MODULE_NAME("exutils")
69 /* Local prototypes */
70 static u32 acpi_ex_digits_needed(acpi_integer value, u32 base);
72 #ifndef ACPI_NO_METHOD_EXECUTION
73 /*******************************************************************************
75 * FUNCTION: acpi_ex_enter_interpreter
81 * DESCRIPTION: Enter the interpreter execution region. Failure to enter
82 * the interpreter region is a fatal system error
84 ******************************************************************************/
86 acpi_status acpi_ex_enter_interpreter(void)
90 ACPI_FUNCTION_TRACE("ex_enter_interpreter");
92 status = acpi_ut_acquire_mutex(ACPI_MTX_EXECUTE);
93 if (ACPI_FAILURE(status)) {
94 ACPI_REPORT_ERROR(("Could not acquire interpreter mutex\n"));
97 return_ACPI_STATUS(status);
100 /*******************************************************************************
102 * FUNCTION: acpi_ex_exit_interpreter
108 * DESCRIPTION: Exit the interpreter execution region
110 * Cases where the interpreter is unlocked:
111 * 1) Completion of the execution of a control method
112 * 2) Method blocked on a Sleep() AML opcode
113 * 3) Method blocked on an Acquire() AML opcode
114 * 4) Method blocked on a Wait() AML opcode
115 * 5) Method blocked to acquire the global lock
116 * 6) Method blocked to execute a serialized control method that is
118 * 7) About to invoke a user-installed opregion handler
120 ******************************************************************************/
122 void acpi_ex_exit_interpreter(void)
126 ACPI_FUNCTION_TRACE("ex_exit_interpreter");
128 status = acpi_ut_release_mutex(ACPI_MTX_EXECUTE);
129 if (ACPI_FAILURE(status)) {
130 ACPI_REPORT_ERROR(("Could not release interpreter mutex\n"));
136 /*******************************************************************************
138 * FUNCTION: acpi_ex_truncate_for32bit_table
140 * PARAMETERS: obj_desc - Object to be truncated
144 * DESCRIPTION: Truncate a number to 32-bits if the currently executing method
145 * belongs to a 32-bit ACPI table.
147 ******************************************************************************/
149 void acpi_ex_truncate_for32bit_table(union acpi_operand_object *obj_desc)
152 ACPI_FUNCTION_ENTRY();
155 * Object must be a valid number and we must be executing
159 (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER)) {
163 if (acpi_gbl_integer_byte_width == 4) {
165 * We are running a method that exists in a 32-bit ACPI table.
166 * Truncate the value to 32 bits by zeroing out the upper 32-bit field
168 obj_desc->integer.value &= (acpi_integer) ACPI_UINT32_MAX;
172 /*******************************************************************************
174 * FUNCTION: acpi_ex_acquire_global_lock
176 * PARAMETERS: field_flags - Flags with Lock rule:
177 * always_lock or never_lock
179 * RETURN: TRUE/FALSE indicating whether the lock was actually acquired
181 * DESCRIPTION: Obtain the global lock and keep track of this fact via two
182 * methods. A global variable keeps the state of the lock, and
183 * the state is returned to the caller.
185 ******************************************************************************/
187 u8 acpi_ex_acquire_global_lock(u32 field_flags)
192 ACPI_FUNCTION_TRACE("ex_acquire_global_lock");
194 /* Only attempt lock if the always_lock bit is set */
196 if (field_flags & AML_FIELD_LOCK_RULE_MASK) {
197 /* We should attempt to get the lock, wait forever */
199 status = acpi_ev_acquire_global_lock(ACPI_WAIT_FOREVER);
200 if (ACPI_SUCCESS(status)) {
203 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
204 "Could not acquire Global Lock, %s\n",
205 acpi_format_exception(status)));
209 return_VALUE(locked);
212 /*******************************************************************************
214 * FUNCTION: acpi_ex_release_global_lock
216 * PARAMETERS: locked_by_me - Return value from corresponding call to
217 * acquire_global_lock.
221 * DESCRIPTION: Release the global lock if it is locked.
223 ******************************************************************************/
225 void acpi_ex_release_global_lock(u8 locked_by_me)
229 ACPI_FUNCTION_TRACE("ex_release_global_lock");
231 /* Only attempt unlock if the caller locked it */
234 /* OK, now release the lock */
236 status = acpi_ev_release_global_lock();
237 if (ACPI_FAILURE(status)) {
238 /* Report the error, but there isn't much else we can do */
240 ACPI_REPORT_ERROR(("Could not release ACPI Global Lock, %s\n", acpi_format_exception(status)));
247 /*******************************************************************************
249 * FUNCTION: acpi_ex_digits_needed
251 * PARAMETERS: Value - Value to be represented
252 * Base - Base of representation
254 * RETURN: The number of digits.
256 * DESCRIPTION: Calculate the number of digits needed to represent the Value
257 * in the given Base (Radix)
259 ******************************************************************************/
261 static u32 acpi_ex_digits_needed(acpi_integer value, u32 base)
264 acpi_integer current_value;
266 ACPI_FUNCTION_TRACE("ex_digits_needed");
268 /* acpi_integer is unsigned, so we don't worry about a '-' prefix */
274 current_value = value;
277 /* Count the digits in the requested base */
279 while (current_value) {
280 (void)acpi_ut_short_divide(current_value, base, ¤t_value,
285 return_VALUE(num_digits);
288 /*******************************************************************************
290 * FUNCTION: acpi_ex_eisa_id_to_string
292 * PARAMETERS: numeric_id - EISA ID to be converted
293 * out_string - Where to put the converted string (8 bytes)
297 * DESCRIPTION: Convert a numeric EISA ID to string representation
299 ******************************************************************************/
301 void acpi_ex_eisa_id_to_string(u32 numeric_id, char *out_string)
305 ACPI_FUNCTION_ENTRY();
307 /* Swap ID to big-endian to get contiguous bits */
309 eisa_id = acpi_ut_dword_byte_swap(numeric_id);
311 out_string[0] = (char)('@' + (((unsigned long)eisa_id >> 26) & 0x1f));
312 out_string[1] = (char)('@' + ((eisa_id >> 21) & 0x1f));
313 out_string[2] = (char)('@' + ((eisa_id >> 16) & 0x1f));
314 out_string[3] = acpi_ut_hex_to_ascii_char((acpi_integer) eisa_id, 12);
315 out_string[4] = acpi_ut_hex_to_ascii_char((acpi_integer) eisa_id, 8);
316 out_string[5] = acpi_ut_hex_to_ascii_char((acpi_integer) eisa_id, 4);
317 out_string[6] = acpi_ut_hex_to_ascii_char((acpi_integer) eisa_id, 0);
321 /*******************************************************************************
323 * FUNCTION: acpi_ex_unsigned_integer_to_string
325 * PARAMETERS: Value - Value to be converted
326 * out_string - Where to put the converted string (8 bytes)
328 * RETURN: None, string
330 * DESCRIPTION: Convert a number to string representation. Assumes string
331 * buffer is large enough to hold the string.
333 ******************************************************************************/
335 void acpi_ex_unsigned_integer_to_string(acpi_integer value, char *out_string)
341 ACPI_FUNCTION_ENTRY();
343 digits_needed = acpi_ex_digits_needed(value, 10);
344 out_string[digits_needed] = 0;
346 for (count = digits_needed; count > 0; count--) {
347 (void)acpi_ut_short_divide(value, 10, &value, &remainder);
348 out_string[count - 1] = (char)('0' + remainder);