dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / uts / intel / io / acpica / executer / exutils.c
blob4843a208914394384013403ad03dcafc836c6845
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"));
107 return_VOID;
111 /*******************************************************************************
113 * FUNCTION: AcpiExExitInterpreter
115 * PARAMETERS: None
117 * RETURN: None
119 * DESCRIPTION: Exit the interpreter execution region. This is the top level
120 * routine used to exit the interpreter when all processing has
121 * been completed, or when the method blocks.
123 * Cases where the interpreter is unlocked internally:
124 * 1) Method will be blocked on a Sleep() AML opcode
125 * 2) Method will be blocked on an Acquire() AML opcode
126 * 3) Method will be blocked on a Wait() AML opcode
127 * 4) Method will be blocked to acquire the global lock
128 * 5) Method will be blocked waiting to execute a serialized control
129 * method that is currently executing
130 * 6) About to invoke a user-installed opregion handler
132 ******************************************************************************/
134 void
135 AcpiExExitInterpreter (
136 void)
138 ACPI_STATUS Status;
141 ACPI_FUNCTION_TRACE (ExExitInterpreter);
144 Status = AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER);
145 if (ACPI_FAILURE (Status))
147 ACPI_ERROR ((AE_INFO, "Could not release AML Interpreter mutex"));
150 return_VOID;
154 /*******************************************************************************
156 * FUNCTION: AcpiExTruncateFor32bitTable
158 * PARAMETERS: ObjDesc - Object to be truncated
160 * RETURN: TRUE if a truncation was performed, FALSE otherwise.
162 * DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is
163 * 32-bit, as determined by the revision of the DSDT.
165 ******************************************************************************/
167 BOOLEAN
168 AcpiExTruncateFor32bitTable (
169 ACPI_OPERAND_OBJECT *ObjDesc)
172 ACPI_FUNCTION_ENTRY ();
176 * Object must be a valid number and we must be executing
177 * a control method. Object could be NS node for AML_INT_NAMEPATH_OP.
179 if ((!ObjDesc) ||
180 (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) != ACPI_DESC_TYPE_OPERAND) ||
181 (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
183 return (FALSE);
186 if ((AcpiGbl_IntegerByteWidth == 4) &&
187 (ObjDesc->Integer.Value > (UINT64) ACPI_UINT32_MAX))
190 * We are executing in a 32-bit ACPI table. Truncate
191 * the value to 32 bits by zeroing out the upper 32-bit field
193 ObjDesc->Integer.Value &= (UINT64) ACPI_UINT32_MAX;
194 return (TRUE);
197 return (FALSE);
201 /*******************************************************************************
203 * FUNCTION: AcpiExAcquireGlobalLock
205 * PARAMETERS: FieldFlags - Flags with Lock rule:
206 * AlwaysLock or NeverLock
208 * RETURN: None
210 * DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field
211 * flags specifiy that it is to be obtained before field access.
213 ******************************************************************************/
215 void
216 AcpiExAcquireGlobalLock (
217 UINT32 FieldFlags)
219 ACPI_STATUS Status;
222 ACPI_FUNCTION_TRACE (ExAcquireGlobalLock);
225 /* Only use the lock if the AlwaysLock bit is set */
227 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))
229 return_VOID;
232 /* Attempt to get the global lock, wait forever */
234 Status = AcpiExAcquireMutexObject (ACPI_WAIT_FOREVER,
235 AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());
237 if (ACPI_FAILURE (Status))
239 ACPI_EXCEPTION ((AE_INFO, Status,
240 "Could not acquire Global Lock"));
243 return_VOID;
247 /*******************************************************************************
249 * FUNCTION: AcpiExReleaseGlobalLock
251 * PARAMETERS: FieldFlags - Flags with Lock rule:
252 * AlwaysLock or NeverLock
254 * RETURN: None
256 * DESCRIPTION: Release the ACPI hardware Global Lock
258 ******************************************************************************/
260 void
261 AcpiExReleaseGlobalLock (
262 UINT32 FieldFlags)
264 ACPI_STATUS Status;
267 ACPI_FUNCTION_TRACE (ExReleaseGlobalLock);
270 /* Only use the lock if the AlwaysLock bit is set */
272 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))
274 return_VOID;
277 /* Release the global lock */
279 Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);
280 if (ACPI_FAILURE (Status))
282 /* Report the error, but there isn't much else we can do */
284 ACPI_EXCEPTION ((AE_INFO, Status,
285 "Could not release Global Lock"));
288 return_VOID;
292 /*******************************************************************************
294 * FUNCTION: AcpiExDigitsNeeded
296 * PARAMETERS: Value - Value to be represented
297 * Base - Base of representation
299 * RETURN: The number of digits.
301 * DESCRIPTION: Calculate the number of digits needed to represent the Value
302 * in the given Base (Radix)
304 ******************************************************************************/
306 static UINT32
307 AcpiExDigitsNeeded (
308 UINT64 Value,
309 UINT32 Base)
311 UINT32 NumDigits;
312 UINT64 CurrentValue;
315 ACPI_FUNCTION_TRACE (ExDigitsNeeded);
318 /* UINT64 is unsigned, so we don't worry about a '-' prefix */
320 if (Value == 0)
322 return_UINT32 (1);
325 CurrentValue = Value;
326 NumDigits = 0;
328 /* Count the digits in the requested base */
330 while (CurrentValue)
332 (void) AcpiUtShortDivide (CurrentValue, Base, &CurrentValue, NULL);
333 NumDigits++;
336 return_UINT32 (NumDigits);
340 /*******************************************************************************
342 * FUNCTION: AcpiExEisaIdToString
344 * PARAMETERS: OutString - Where to put the converted string (8 bytes)
345 * CompressedId - EISAID to be converted
347 * RETURN: None
349 * DESCRIPTION: Convert a numeric EISAID to string representation. Return
350 * buffer must be large enough to hold the string. The string
351 * returned is always exactly of length ACPI_EISAID_STRING_SIZE
352 * (includes null terminator). The EISAID is always 32 bits.
354 ******************************************************************************/
356 void
357 AcpiExEisaIdToString (
358 char *OutString,
359 UINT64 CompressedId)
361 UINT32 SwappedId;
364 ACPI_FUNCTION_ENTRY ();
367 /* The EISAID should be a 32-bit integer */
369 if (CompressedId > ACPI_UINT32_MAX)
371 ACPI_WARNING ((AE_INFO,
372 "Expected EISAID is larger than 32 bits: "
373 "0x%8.8X%8.8X, truncating",
374 ACPI_FORMAT_UINT64 (CompressedId)));
377 /* Swap ID to big-endian to get contiguous bits */
379 SwappedId = AcpiUtDwordByteSwap ((UINT32) CompressedId);
381 /* First 3 bytes are uppercase letters. Next 4 bytes are hexadecimal */
383 OutString[0] = (char) (0x40 + (((unsigned long) SwappedId >> 26) & 0x1F));
384 OutString[1] = (char) (0x40 + ((SwappedId >> 21) & 0x1F));
385 OutString[2] = (char) (0x40 + ((SwappedId >> 16) & 0x1F));
386 OutString[3] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 12);
387 OutString[4] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 8);
388 OutString[5] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 4);
389 OutString[6] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 0);
390 OutString[7] = 0;
394 /*******************************************************************************
396 * FUNCTION: AcpiExIntegerToString
398 * PARAMETERS: OutString - Where to put the converted string. At least
399 * 21 bytes are needed to hold the largest
400 * possible 64-bit integer.
401 * Value - Value to be converted
403 * RETURN: Converted string in OutString
405 * DESCRIPTION: Convert a 64-bit integer to decimal string representation.
406 * Assumes string buffer is large enough to hold the string. The
407 * largest string is (ACPI_MAX64_DECIMAL_DIGITS + 1).
409 ******************************************************************************/
411 void
412 AcpiExIntegerToString (
413 char *OutString,
414 UINT64 Value)
416 UINT32 Count;
417 UINT32 DigitsNeeded;
418 UINT32 Remainder;
421 ACPI_FUNCTION_ENTRY ();
424 DigitsNeeded = AcpiExDigitsNeeded (Value, 10);
425 OutString[DigitsNeeded] = 0;
427 for (Count = DigitsNeeded; Count > 0; Count--)
429 (void) AcpiUtShortDivide (Value, 10, &Value, &Remainder);
430 OutString[Count-1] = (char) ('0' + Remainder);\
435 /*******************************************************************************
437 * FUNCTION: AcpiExPciClsToString
439 * PARAMETERS: OutString - Where to put the converted string (7 bytes)
440 * ClassCode - PCI class code to be converted (3 bytes)
442 * RETURN: Converted string in OutString
444 * DESCRIPTION: Convert 3-bytes PCI class code to string representation.
445 * Return buffer must be large enough to hold the string. The
446 * string returned is always exactly of length
447 * ACPI_PCICLS_STRING_SIZE (includes null terminator).
449 ******************************************************************************/
451 void
452 AcpiExPciClsToString (
453 char *OutString,
454 UINT8 ClassCode[3])
457 ACPI_FUNCTION_ENTRY ();
460 /* All 3 bytes are hexadecimal */
462 OutString[0] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[0], 4);
463 OutString[1] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[0], 0);
464 OutString[2] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[1], 4);
465 OutString[3] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[1], 0);
466 OutString[4] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[2], 4);
467 OutString[5] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[2], 0);
468 OutString[6] = 0;
472 /*******************************************************************************
474 * FUNCTION: AcpiIsValidSpaceId
476 * PARAMETERS: SpaceId - ID to be validated
478 * RETURN: TRUE if SpaceId is a valid/supported ID.
480 * DESCRIPTION: Validate an operation region SpaceID.
482 ******************************************************************************/
484 BOOLEAN
485 AcpiIsValidSpaceId (
486 UINT8 SpaceId)
489 if ((SpaceId >= ACPI_NUM_PREDEFINED_REGIONS) &&
490 (SpaceId < ACPI_USER_REGION_BEGIN) &&
491 (SpaceId != ACPI_ADR_SPACE_DATA_TABLE) &&
492 (SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE))
494 return (FALSE);
497 return (TRUE);
500 #endif