1/******************************************************************************
2 *
3 * Module Name: exutils - interpreter/scanner utilities
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2014, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions, and the following disclaimer,
16 *    without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 *    substantially similar to the "NO WARRANTY" disclaimer below
19 *    ("Disclaimer") and any redistribution must be conditioned upon
20 *    including a substantially similar Disclaimer requirement for further
21 *    binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 *    of any contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44/*
45 * DEFINE_AML_GLOBALS is tested in amlcode.h
46 * to determine whether certain global names should be "defined" or only
47 * "declared" in the current compilation. This enhances maintainability
48 * by enabling a single header file to embody all knowledge of the names
49 * in question.
50 *
51 * Exactly one module of any executable should #define DEFINE_GLOBALS
52 * before #including the header files which use this convention. The
53 * names in question will be defined and initialized in that module,
54 * and declared as extern in all other modules which #include those
55 * header files.
56 */
57
58#define DEFINE_AML_GLOBALS
59
60#include <acpi/acpi.h>
61#include "accommon.h"
62#include "acinterp.h"
63#include "amlcode.h"
64
65#define _COMPONENT          ACPI_EXECUTER
66ACPI_MODULE_NAME("exutils")
67
68/* Local prototypes */
69static u32 acpi_ex_digits_needed(u64 value, u32 base);
70
71#ifndef ACPI_NO_METHOD_EXECUTION
72/*******************************************************************************
73 *
74 * FUNCTION:    acpi_ex_enter_interpreter
75 *
76 * PARAMETERS:  None
77 *
78 * RETURN:      None
79 *
80 * DESCRIPTION: Enter the interpreter execution region. Failure to enter
81 *              the interpreter region is a fatal system error. Used in
82 *              conjunction with exit_interpreter.
83 *
84 ******************************************************************************/
85
86void acpi_ex_enter_interpreter(void)
87{
88	acpi_status status;
89
90	ACPI_FUNCTION_TRACE(ex_enter_interpreter);
91
92	status = acpi_ut_acquire_mutex(ACPI_MTX_INTERPRETER);
93	if (ACPI_FAILURE(status)) {
94		ACPI_ERROR((AE_INFO,
95			    "Could not acquire AML Interpreter mutex"));
96	}
97
98	return_VOID;
99}
100
101/*******************************************************************************
102 *
103 * FUNCTION:    acpi_ex_exit_interpreter
104 *
105 * PARAMETERS:  None
106 *
107 * RETURN:      None
108 *
109 * DESCRIPTION: Exit the interpreter execution region. This is the top level
110 *              routine used to exit the interpreter when all processing has
111 *              been completed, or when the method blocks.
112 *
113 * Cases where the interpreter is unlocked internally:
114 *      1) Method will be blocked on a Sleep() AML opcode
115 *      2) Method will be blocked on an Acquire() AML opcode
116 *      3) Method will be blocked on a Wait() AML opcode
117 *      4) Method will be blocked to acquire the global lock
118 *      5) Method will be blocked waiting to execute a serialized control
119 *          method that is currently executing
120 *      6) About to invoke a user-installed opregion handler
121 *
122 ******************************************************************************/
123
124void acpi_ex_exit_interpreter(void)
125{
126	acpi_status status;
127
128	ACPI_FUNCTION_TRACE(ex_exit_interpreter);
129
130	status = acpi_ut_release_mutex(ACPI_MTX_INTERPRETER);
131	if (ACPI_FAILURE(status)) {
132		ACPI_ERROR((AE_INFO,
133			    "Could not release AML Interpreter mutex"));
134	}
135
136	return_VOID;
137}
138
139/*******************************************************************************
140 *
141 * FUNCTION:    acpi_ex_truncate_for32bit_table
142 *
143 * PARAMETERS:  obj_desc        - Object to be truncated
144 *
145 * RETURN:      TRUE if a truncation was performed, FALSE otherwise.
146 *
147 * DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is
148 *              32-bit, as determined by the revision of the DSDT.
149 *
150 ******************************************************************************/
151
152u8 acpi_ex_truncate_for32bit_table(union acpi_operand_object *obj_desc)
153{
154
155	ACPI_FUNCTION_ENTRY();
156
157	/*
158	 * Object must be a valid number and we must be executing
159	 * a control method. Object could be NS node for AML_INT_NAMEPATH_OP.
160	 */
161	if ((!obj_desc) ||
162	    (ACPI_GET_DESCRIPTOR_TYPE(obj_desc) != ACPI_DESC_TYPE_OPERAND) ||
163	    (obj_desc->common.type != ACPI_TYPE_INTEGER)) {
164		return (FALSE);
165	}
166
167	if ((acpi_gbl_integer_byte_width == 4) &&
168	    (obj_desc->integer.value > (u64)ACPI_UINT32_MAX)) {
169		/*
170		 * We are executing in a 32-bit ACPI table.
171		 * Truncate the value to 32 bits by zeroing out the upper 32-bit field
172		 */
173		obj_desc->integer.value &= (u64)ACPI_UINT32_MAX;
174		return (TRUE);
175	}
176
177	return (FALSE);
178}
179
180/*******************************************************************************
181 *
182 * FUNCTION:    acpi_ex_acquire_global_lock
183 *
184 * PARAMETERS:  field_flags           - Flags with Lock rule:
185 *                                      always_lock or never_lock
186 *
187 * RETURN:      None
188 *
189 * DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field
190 *              flags specifiy that it is to be obtained before field access.
191 *
192 ******************************************************************************/
193
194void acpi_ex_acquire_global_lock(u32 field_flags)
195{
196	acpi_status status;
197
198	ACPI_FUNCTION_TRACE(ex_acquire_global_lock);
199
200	/* Only use the lock if the always_lock bit is set */
201
202	if (!(field_flags & AML_FIELD_LOCK_RULE_MASK)) {
203		return_VOID;
204	}
205
206	/* Attempt to get the global lock, wait forever */
207
208	status = acpi_ex_acquire_mutex_object(ACPI_WAIT_FOREVER,
209					      acpi_gbl_global_lock_mutex,
210					      acpi_os_get_thread_id());
211
212	if (ACPI_FAILURE(status)) {
213		ACPI_EXCEPTION((AE_INFO, status,
214				"Could not acquire Global Lock"));
215	}
216
217	return_VOID;
218}
219
220/*******************************************************************************
221 *
222 * FUNCTION:    acpi_ex_release_global_lock
223 *
224 * PARAMETERS:  field_flags           - Flags with Lock rule:
225 *                                      always_lock or never_lock
226 *
227 * RETURN:      None
228 *
229 * DESCRIPTION: Release the ACPI hardware Global Lock
230 *
231 ******************************************************************************/
232
233void acpi_ex_release_global_lock(u32 field_flags)
234{
235	acpi_status status;
236
237	ACPI_FUNCTION_TRACE(ex_release_global_lock);
238
239	/* Only use the lock if the always_lock bit is set */
240
241	if (!(field_flags & AML_FIELD_LOCK_RULE_MASK)) {
242		return_VOID;
243	}
244
245	/* Release the global lock */
246
247	status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
248	if (ACPI_FAILURE(status)) {
249
250		/* Report the error, but there isn't much else we can do */
251
252		ACPI_EXCEPTION((AE_INFO, status,
253				"Could not release Global Lock"));
254	}
255
256	return_VOID;
257}
258
259/*******************************************************************************
260 *
261 * FUNCTION:    acpi_ex_digits_needed
262 *
263 * PARAMETERS:  value           - Value to be represented
264 *              base            - Base of representation
265 *
266 * RETURN:      The number of digits.
267 *
268 * DESCRIPTION: Calculate the number of digits needed to represent the Value
269 *              in the given Base (Radix)
270 *
271 ******************************************************************************/
272
273static u32 acpi_ex_digits_needed(u64 value, u32 base)
274{
275	u32 num_digits;
276	u64 current_value;
277
278	ACPI_FUNCTION_TRACE(ex_digits_needed);
279
280	/* u64 is unsigned, so we don't worry about a '-' prefix */
281
282	if (value == 0) {
283		return_UINT32(1);
284	}
285
286	current_value = value;
287	num_digits = 0;
288
289	/* Count the digits in the requested base */
290
291	while (current_value) {
292		(void)acpi_ut_short_divide(current_value, base, &current_value,
293					   NULL);
294		num_digits++;
295	}
296
297	return_UINT32(num_digits);
298}
299
300/*******************************************************************************
301 *
302 * FUNCTION:    acpi_ex_eisa_id_to_string
303 *
304 * PARAMETERS:  compressed_id   - EISAID to be converted
305 *              out_string      - Where to put the converted string (8 bytes)
306 *
307 * RETURN:      None
308 *
309 * DESCRIPTION: Convert a numeric EISAID to string representation. Return
310 *              buffer must be large enough to hold the string. The string
311 *              returned is always exactly of length ACPI_EISAID_STRING_SIZE
312 *              (includes null terminator). The EISAID is always 32 bits.
313 *
314 ******************************************************************************/
315
316void acpi_ex_eisa_id_to_string(char *out_string, u64 compressed_id)
317{
318	u32 swapped_id;
319
320	ACPI_FUNCTION_ENTRY();
321
322	/* The EISAID should be a 32-bit integer */
323
324	if (compressed_id > ACPI_UINT32_MAX) {
325		ACPI_WARNING((AE_INFO,
326			      "Expected EISAID is larger than 32 bits: 0x%8.8X%8.8X, truncating",
327			      ACPI_FORMAT_UINT64(compressed_id)));
328	}
329
330	/* Swap ID to big-endian to get contiguous bits */
331
332	swapped_id = acpi_ut_dword_byte_swap((u32)compressed_id);
333
334	/* First 3 bytes are uppercase letters. Next 4 bytes are hexadecimal */
335
336	out_string[0] =
337	    (char)(0x40 + (((unsigned long)swapped_id >> 26) & 0x1F));
338	out_string[1] = (char)(0x40 + ((swapped_id >> 21) & 0x1F));
339	out_string[2] = (char)(0x40 + ((swapped_id >> 16) & 0x1F));
340	out_string[3] = acpi_ut_hex_to_ascii_char((u64) swapped_id, 12);
341	out_string[4] = acpi_ut_hex_to_ascii_char((u64) swapped_id, 8);
342	out_string[5] = acpi_ut_hex_to_ascii_char((u64) swapped_id, 4);
343	out_string[6] = acpi_ut_hex_to_ascii_char((u64) swapped_id, 0);
344	out_string[7] = 0;
345}
346
347/*******************************************************************************
348 *
349 * FUNCTION:    acpi_ex_integer_to_string
350 *
351 * PARAMETERS:  out_string      - Where to put the converted string. At least
352 *                                21 bytes are needed to hold the largest
353 *                                possible 64-bit integer.
354 *              value           - Value to be converted
355 *
356 * RETURN:      None, string
357 *
358 * DESCRIPTION: Convert a 64-bit integer to decimal string representation.
359 *              Assumes string buffer is large enough to hold the string. The
360 *              largest string is (ACPI_MAX64_DECIMAL_DIGITS + 1).
361 *
362 ******************************************************************************/
363
364void acpi_ex_integer_to_string(char *out_string, u64 value)
365{
366	u32 count;
367	u32 digits_needed;
368	u32 remainder;
369
370	ACPI_FUNCTION_ENTRY();
371
372	digits_needed = acpi_ex_digits_needed(value, 10);
373	out_string[digits_needed] = 0;
374
375	for (count = digits_needed; count > 0; count--) {
376		(void)acpi_ut_short_divide(value, 10, &value, &remainder);
377		out_string[count - 1] = (char)('0' + remainder);
378	}
379}
380
381/*******************************************************************************
382 *
383 * FUNCTION:    acpi_is_valid_space_id
384 *
385 * PARAMETERS:  space_id            - ID to be validated
386 *
387 * RETURN:      TRUE if valid/supported ID.
388 *
389 * DESCRIPTION: Validate an operation region space_ID.
390 *
391 ******************************************************************************/
392
393u8 acpi_is_valid_space_id(u8 space_id)
394{
395
396	if ((space_id >= ACPI_NUM_PREDEFINED_REGIONS) &&
397	    (space_id < ACPI_USER_REGION_BEGIN) &&
398	    (space_id != ACPI_ADR_SPACE_DATA_TABLE) &&
399	    (space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
400		return (FALSE);
401	}
402
403	return (TRUE);
404}
405
406#endif
407