Sync ACPICA with Intel's version 20160831.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / executer / exutils.c
blobedaee5526beced63e073b62b3c008a71cbd476c6
1 /******************************************************************************
3 * Module Name: exutils - interpreter/scanner utilities
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
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.
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.
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.
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.
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.
58 #define DEFINE_AML_GLOBALS
60 #include "acpi.h"
61 #include "accommon.h"
62 #include "acinterp.h"
63 #include "amlcode.h"
65 #define _COMPONENT ACPI_EXECUTER
66 ACPI_MODULE_NAME ("exutils")
68 /* Local prototypes */
70 static UINT32
71 AcpiExDigitsNeeded (
72 UINT64 Value,
73 UINT32 Base);
76 #ifndef ACPI_NO_METHOD_EXECUTION
77 /*******************************************************************************
79 * FUNCTION: AcpiExEnterInterpreter
81 * PARAMETERS: None
83 * RETURN: None
85 * DESCRIPTION: Enter the interpreter execution region. Failure to enter
86 * the interpreter region is a fatal system error. Used in
87 * conjunction with ExitInterpreter.
89 ******************************************************************************/
91 void
92 AcpiExEnterInterpreter (
93 void)
95 ACPI_STATUS Status;
98 ACPI_FUNCTION_TRACE (ExEnterInterpreter);
101 Status = AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER);
102 if (ACPI_FAILURE (Status))
104 ACPI_ERROR ((AE_INFO, "Could not acquire AML Interpreter mutex"));
106 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
107 if (ACPI_FAILURE (Status))
109 ACPI_ERROR ((AE_INFO, "Could not acquire AML Namespace mutex"));
112 return_VOID;
116 /*******************************************************************************
118 * FUNCTION: AcpiExExitInterpreter
120 * PARAMETERS: None
122 * RETURN: None
124 * DESCRIPTION: Exit the interpreter execution region. This is the top level
125 * routine used to exit the interpreter when all processing has
126 * been completed, or when the method blocks.
128 * Cases where the interpreter is unlocked internally:
129 * 1) Method will be blocked on a Sleep() AML opcode
130 * 2) Method will be blocked on an Acquire() AML opcode
131 * 3) Method will be blocked on a Wait() AML opcode
132 * 4) Method will be blocked to acquire the global lock
133 * 5) Method will be blocked waiting to execute a serialized control
134 * method that is currently executing
135 * 6) About to invoke a user-installed opregion handler
137 ******************************************************************************/
139 void
140 AcpiExExitInterpreter (
141 void)
143 ACPI_STATUS Status;
146 ACPI_FUNCTION_TRACE (ExExitInterpreter);
149 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
150 if (ACPI_FAILURE (Status))
152 ACPI_ERROR ((AE_INFO, "Could not release AML Namespace mutex"));
154 Status = AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER);
155 if (ACPI_FAILURE (Status))
157 ACPI_ERROR ((AE_INFO, "Could not release AML Interpreter mutex"));
160 return_VOID;
164 /*******************************************************************************
166 * FUNCTION: AcpiExTruncateFor32bitTable
168 * PARAMETERS: ObjDesc - Object to be truncated
170 * RETURN: TRUE if a truncation was performed, FALSE otherwise.
172 * DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is
173 * 32-bit, as determined by the revision of the DSDT.
175 ******************************************************************************/
177 BOOLEAN
178 AcpiExTruncateFor32bitTable (
179 ACPI_OPERAND_OBJECT *ObjDesc)
182 ACPI_FUNCTION_ENTRY ();
186 * Object must be a valid number and we must be executing
187 * a control method. Object could be NS node for AML_INT_NAMEPATH_OP.
189 if ((!ObjDesc) ||
190 (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) != ACPI_DESC_TYPE_OPERAND) ||
191 (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
193 return (FALSE);
196 if ((AcpiGbl_IntegerByteWidth == 4) &&
197 (ObjDesc->Integer.Value > (UINT64) ACPI_UINT32_MAX))
200 * We are executing in a 32-bit ACPI table. Truncate
201 * the value to 32 bits by zeroing out the upper 32-bit field
203 ObjDesc->Integer.Value &= (UINT64) ACPI_UINT32_MAX;
204 return (TRUE);
207 return (FALSE);
211 /*******************************************************************************
213 * FUNCTION: AcpiExAcquireGlobalLock
215 * PARAMETERS: FieldFlags - Flags with Lock rule:
216 * AlwaysLock or NeverLock
218 * RETURN: None
220 * DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field
221 * flags specifiy that it is to be obtained before field access.
223 ******************************************************************************/
225 void
226 AcpiExAcquireGlobalLock (
227 UINT32 FieldFlags)
229 ACPI_STATUS Status;
232 ACPI_FUNCTION_TRACE (ExAcquireGlobalLock);
235 /* Only use the lock if the AlwaysLock bit is set */
237 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))
239 return_VOID;
242 /* Attempt to get the global lock, wait forever */
244 Status = AcpiExAcquireMutexObject (ACPI_WAIT_FOREVER,
245 AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());
247 if (ACPI_FAILURE (Status))
249 ACPI_EXCEPTION ((AE_INFO, Status,
250 "Could not acquire Global Lock"));
253 return_VOID;
257 /*******************************************************************************
259 * FUNCTION: AcpiExReleaseGlobalLock
261 * PARAMETERS: FieldFlags - Flags with Lock rule:
262 * AlwaysLock or NeverLock
264 * RETURN: None
266 * DESCRIPTION: Release the ACPI hardware Global Lock
268 ******************************************************************************/
270 void
271 AcpiExReleaseGlobalLock (
272 UINT32 FieldFlags)
274 ACPI_STATUS Status;
277 ACPI_FUNCTION_TRACE (ExReleaseGlobalLock);
280 /* Only use the lock if the AlwaysLock bit is set */
282 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))
284 return_VOID;
287 /* Release the global lock */
289 Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);
290 if (ACPI_FAILURE (Status))
292 /* Report the error, but there isn't much else we can do */
294 ACPI_EXCEPTION ((AE_INFO, Status,
295 "Could not release Global Lock"));
298 return_VOID;
302 /*******************************************************************************
304 * FUNCTION: AcpiExDigitsNeeded
306 * PARAMETERS: Value - Value to be represented
307 * Base - Base of representation
309 * RETURN: The number of digits.
311 * DESCRIPTION: Calculate the number of digits needed to represent the Value
312 * in the given Base (Radix)
314 ******************************************************************************/
316 static UINT32
317 AcpiExDigitsNeeded (
318 UINT64 Value,
319 UINT32 Base)
321 UINT32 NumDigits;
322 UINT64 CurrentValue;
325 ACPI_FUNCTION_TRACE (ExDigitsNeeded);
328 /* UINT64 is unsigned, so we don't worry about a '-' prefix */
330 if (Value == 0)
332 return_UINT32 (1);
335 CurrentValue = Value;
336 NumDigits = 0;
338 /* Count the digits in the requested base */
340 while (CurrentValue)
342 (void) AcpiUtShortDivide (CurrentValue, Base, &CurrentValue, NULL);
343 NumDigits++;
346 return_UINT32 (NumDigits);
350 /*******************************************************************************
352 * FUNCTION: AcpiExEisaIdToString
354 * PARAMETERS: OutString - Where to put the converted string (8 bytes)
355 * CompressedId - EISAID to be converted
357 * RETURN: None
359 * DESCRIPTION: Convert a numeric EISAID to string representation. Return
360 * buffer must be large enough to hold the string. The string
361 * returned is always exactly of length ACPI_EISAID_STRING_SIZE
362 * (includes null terminator). The EISAID is always 32 bits.
364 ******************************************************************************/
366 void
367 AcpiExEisaIdToString (
368 char *OutString,
369 UINT64 CompressedId)
371 UINT32 SwappedId;
374 ACPI_FUNCTION_ENTRY ();
377 /* The EISAID should be a 32-bit integer */
379 if (CompressedId > ACPI_UINT32_MAX)
381 ACPI_WARNING ((AE_INFO,
382 "Expected EISAID is larger than 32 bits: "
383 "0x%8.8X%8.8X, truncating",
384 ACPI_FORMAT_UINT64 (CompressedId)));
387 /* Swap ID to big-endian to get contiguous bits */
389 SwappedId = AcpiUtDwordByteSwap ((UINT32) CompressedId);
391 /* First 3 bytes are uppercase letters. Next 4 bytes are hexadecimal */
393 OutString[0] = (char) (0x40 + (((unsigned long) SwappedId >> 26) & 0x1F));
394 OutString[1] = (char) (0x40 + ((SwappedId >> 21) & 0x1F));
395 OutString[2] = (char) (0x40 + ((SwappedId >> 16) & 0x1F));
396 OutString[3] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 12);
397 OutString[4] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 8);
398 OutString[5] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 4);
399 OutString[6] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 0);
400 OutString[7] = 0;
404 /*******************************************************************************
406 * FUNCTION: AcpiExIntegerToString
408 * PARAMETERS: OutString - Where to put the converted string. At least
409 * 21 bytes are needed to hold the largest
410 * possible 64-bit integer.
411 * Value - Value to be converted
413 * RETURN: Converted string in OutString
415 * DESCRIPTION: Convert a 64-bit integer to decimal string representation.
416 * Assumes string buffer is large enough to hold the string. The
417 * largest string is (ACPI_MAX64_DECIMAL_DIGITS + 1).
419 ******************************************************************************/
421 void
422 AcpiExIntegerToString (
423 char *OutString,
424 UINT64 Value)
426 UINT32 Count;
427 UINT32 DigitsNeeded;
428 UINT32 Remainder;
431 ACPI_FUNCTION_ENTRY ();
434 DigitsNeeded = AcpiExDigitsNeeded (Value, 10);
435 OutString[DigitsNeeded] = 0;
437 for (Count = DigitsNeeded; Count > 0; Count--)
439 (void) AcpiUtShortDivide (Value, 10, &Value, &Remainder);
440 OutString[Count-1] = (char) ('0' + Remainder);\
445 /*******************************************************************************
447 * FUNCTION: AcpiExPciClsToString
449 * PARAMETERS: OutString - Where to put the converted string (7 bytes)
450 * ClassCode - PCI class code to be converted (3 bytes)
452 * RETURN: Converted string in OutString
454 * DESCRIPTION: Convert 3-bytes PCI class code to string representation.
455 * Return buffer must be large enough to hold the string. The
456 * string returned is always exactly of length
457 * ACPI_PCICLS_STRING_SIZE (includes null terminator).
459 ******************************************************************************/
461 void
462 AcpiExPciClsToString (
463 char *OutString,
464 UINT8 ClassCode[3])
467 ACPI_FUNCTION_ENTRY ();
470 /* All 3 bytes are hexadecimal */
472 OutString[0] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[0], 4);
473 OutString[1] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[0], 0);
474 OutString[2] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[1], 4);
475 OutString[3] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[1], 0);
476 OutString[4] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[2], 4);
477 OutString[5] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[2], 0);
478 OutString[6] = 0;
482 /*******************************************************************************
484 * FUNCTION: AcpiIsValidSpaceId
486 * PARAMETERS: SpaceId - ID to be validated
488 * RETURN: TRUE if SpaceId is a valid/supported ID.
490 * DESCRIPTION: Validate an operation region SpaceID.
492 ******************************************************************************/
494 BOOLEAN
495 AcpiIsValidSpaceId (
496 UINT8 SpaceId)
499 if ((SpaceId >= ACPI_NUM_PREDEFINED_REGIONS) &&
500 (SpaceId < ACPI_USER_REGION_BEGIN) &&
501 (SpaceId != ACPI_ADR_SPACE_DATA_TABLE) &&
502 (SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE))
504 return (FALSE);
507 return (TRUE);
510 #endif