exutils.c revision 99679
167754Smsmith 267754Smsmith/****************************************************************************** 367754Smsmith * 477424Smsmith * Module Name: exutils - interpreter/scanner utilities 599679Siwasaki * $Revision: 100 $ 667754Smsmith * 767754Smsmith *****************************************************************************/ 867754Smsmith 967754Smsmith/****************************************************************************** 1067754Smsmith * 1167754Smsmith * 1. Copyright Notice 1267754Smsmith * 1391116Smsmith * Some or all of this work - Copyright (c) 1999 - 2002, Intel Corp. 1470243Smsmith * All rights reserved. 1567754Smsmith * 1667754Smsmith * 2. License 1767754Smsmith * 1867754Smsmith * 2.1. This is your license from Intel Corp. under its intellectual property 1967754Smsmith * rights. You may have additional license terms from the party that provided 2067754Smsmith * you this software, covering your right to use that party's intellectual 2167754Smsmith * property rights. 2267754Smsmith * 2367754Smsmith * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 2467754Smsmith * copy of the source code appearing in this file ("Covered Code") an 2567754Smsmith * irrevocable, perpetual, worldwide license under Intel's copyrights in the 2667754Smsmith * base code distributed originally by Intel ("Original Intel Code") to copy, 2767754Smsmith * make derivatives, distribute, use and display any portion of the Covered 2867754Smsmith * Code in any form, with the right to sublicense such rights; and 2967754Smsmith * 3067754Smsmith * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 3167754Smsmith * license (with the right to sublicense), under only those claims of Intel 3267754Smsmith * patents that are infringed by the Original Intel Code, to make, use, sell, 3367754Smsmith * offer to sell, and import the Covered Code and derivative works thereof 3467754Smsmith * solely to the minimum extent necessary to exercise the above copyright 3567754Smsmith * license, and in no event shall the patent license extend to any additions 3667754Smsmith * to or modifications of the Original Intel Code. No other license or right 3767754Smsmith * is granted directly or by implication, estoppel or otherwise; 3867754Smsmith * 3967754Smsmith * The above copyright and patent license is granted only if the following 4067754Smsmith * conditions are met: 4167754Smsmith * 4267754Smsmith * 3. Conditions 4367754Smsmith * 4467754Smsmith * 3.1. Redistribution of Source with Rights to Further Distribute Source. 4567754Smsmith * Redistribution of source code of any substantial portion of the Covered 4667754Smsmith * Code or modification with rights to further distribute source must include 4767754Smsmith * the above Copyright Notice, the above License, this list of Conditions, 4867754Smsmith * and the following Disclaimer and Export Compliance provision. In addition, 4967754Smsmith * Licensee must cause all Covered Code to which Licensee contributes to 5067754Smsmith * contain a file documenting the changes Licensee made to create that Covered 5167754Smsmith * Code and the date of any change. Licensee must include in that file the 5267754Smsmith * documentation of any changes made by any predecessor Licensee. Licensee 5367754Smsmith * must include a prominent statement that the modification is derived, 5467754Smsmith * directly or indirectly, from Original Intel Code. 5567754Smsmith * 5667754Smsmith * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 5767754Smsmith * Redistribution of source code of any substantial portion of the Covered 5867754Smsmith * Code or modification without rights to further distribute source must 5967754Smsmith * include the following Disclaimer and Export Compliance provision in the 6067754Smsmith * documentation and/or other materials provided with distribution. In 6167754Smsmith * addition, Licensee may not authorize further sublicense of source of any 6267754Smsmith * portion of the Covered Code, and must include terms to the effect that the 6367754Smsmith * license from Licensee to its licensee is limited to the intellectual 6467754Smsmith * property embodied in the software Licensee provides to its licensee, and 6567754Smsmith * not to intellectual property embodied in modifications its licensee may 6667754Smsmith * make. 6767754Smsmith * 6867754Smsmith * 3.3. Redistribution of Executable. Redistribution in executable form of any 6967754Smsmith * substantial portion of the Covered Code or modification must reproduce the 7067754Smsmith * above Copyright Notice, and the following Disclaimer and Export Compliance 7167754Smsmith * provision in the documentation and/or other materials provided with the 7267754Smsmith * distribution. 7367754Smsmith * 7467754Smsmith * 3.4. Intel retains all right, title, and interest in and to the Original 7567754Smsmith * Intel Code. 7667754Smsmith * 7767754Smsmith * 3.5. Neither the name Intel nor any other trademark owned or controlled by 7867754Smsmith * Intel shall be used in advertising or otherwise to promote the sale, use or 7967754Smsmith * other dealings in products derived from or relating to the Covered Code 8067754Smsmith * without prior written authorization from Intel. 8167754Smsmith * 8267754Smsmith * 4. Disclaimer and Export Compliance 8367754Smsmith * 8467754Smsmith * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 8567754Smsmith * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 8667754Smsmith * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 8767754Smsmith * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 8867754Smsmith * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 8967754Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 9067754Smsmith * PARTICULAR PURPOSE. 9167754Smsmith * 9267754Smsmith * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 9367754Smsmith * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 9467754Smsmith * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 9567754Smsmith * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 9667754Smsmith * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 9767754Smsmith * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 9867754Smsmith * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 9967754Smsmith * LIMITED REMEDY. 10067754Smsmith * 10167754Smsmith * 4.3. Licensee shall not export, either directly or indirectly, any of this 10267754Smsmith * software or system incorporating such software without first obtaining any 10367754Smsmith * required license or other approval from the U. S. Department of Commerce or 10467754Smsmith * any other agency or department of the United States Government. In the 10567754Smsmith * event Licensee exports any such software from the United States or 10667754Smsmith * re-exports any such software from a foreign destination, Licensee shall 10767754Smsmith * ensure that the distribution and export/re-export of the software is in 10867754Smsmith * compliance with all laws, regulations, orders, or other restrictions of the 10967754Smsmith * U.S. Export Administration Regulations. Licensee agrees that neither it nor 11067754Smsmith * any of its subsidiaries will export/re-export any technical data, process, 11167754Smsmith * software, or service, directly or indirectly, to any country for which the 11267754Smsmith * United States government or any agency thereof requires an export license, 11367754Smsmith * other governmental approval, or letter of assurance, without first obtaining 11467754Smsmith * such license, approval or letter. 11567754Smsmith * 11667754Smsmith *****************************************************************************/ 11767754Smsmith 11877424Smsmith#define __EXUTILS_C__ 11967754Smsmith 12083174Smsmith/* 12183174Smsmith * DEFINE_AML_GLOBALS is tested in amlcode.h 12283174Smsmith * to determine whether certain global names should be "defined" or only 12383174Smsmith * "declared" in the current compilation. This enhances maintainability 12483174Smsmith * by enabling a single header file to embody all knowledge of the names 12583174Smsmith * in question. 12683174Smsmith * 12783174Smsmith * Exactly one module of any executable should #define DEFINE_GLOBALS 12883174Smsmith * before #including the header files which use this convention. The 12983174Smsmith * names in question will be defined and initialized in that module, 13083174Smsmith * and declared as extern in all other modules which #include those 13183174Smsmith * header files. 13283174Smsmith */ 13383174Smsmith 13483174Smsmith#define DEFINE_AML_GLOBALS 13583174Smsmith 13667754Smsmith#include "acpi.h" 13767754Smsmith#include "acinterp.h" 13867754Smsmith#include "amlcode.h" 13967754Smsmith#include "acevents.h" 14067754Smsmith 14177424Smsmith#define _COMPONENT ACPI_EXECUTER 14291116Smsmith ACPI_MODULE_NAME ("exutils") 14367754Smsmith 14467754Smsmith 14567754Smsmith/******************************************************************************* 14667754Smsmith * 14777424Smsmith * FUNCTION: AcpiExEnterInterpreter 14867754Smsmith * 14967754Smsmith * PARAMETERS: None 15067754Smsmith * 15191116Smsmith * DESCRIPTION: Enter the interpreter execution region. Failure to enter 15291116Smsmith * the interpreter region is a fatal system error 15367754Smsmith * 15467754Smsmith ******************************************************************************/ 15567754Smsmith 15677424SmsmithACPI_STATUS 15777424SmsmithAcpiExEnterInterpreter (void) 15867754Smsmith{ 15977424Smsmith ACPI_STATUS Status; 16067754Smsmith 16191116Smsmith ACPI_FUNCTION_TRACE ("ExEnterInterpreter"); 16267754Smsmith 16367754Smsmith 16477424Smsmith Status = AcpiUtAcquireMutex (ACPI_MTX_EXECUTE); 16591116Smsmith if (ACPI_FAILURE (Status)) 16691116Smsmith { 16799679Siwasaki ACPI_REPORT_ERROR (("Could not acquire interpreter mutex\n")); 16891116Smsmith } 16991116Smsmith 17077424Smsmith return_ACPI_STATUS (Status); 17167754Smsmith} 17267754Smsmith 17367754Smsmith 17467754Smsmith/******************************************************************************* 17567754Smsmith * 17677424Smsmith * FUNCTION: AcpiExExitInterpreter 17767754Smsmith * 17867754Smsmith * PARAMETERS: None 17967754Smsmith * 18067754Smsmith * DESCRIPTION: Exit the interpreter execution region 18167754Smsmith * 18267754Smsmith * Cases where the interpreter is unlocked: 18367754Smsmith * 1) Completion of the execution of a control method 18467754Smsmith * 2) Method blocked on a Sleep() AML opcode 18567754Smsmith * 3) Method blocked on an Acquire() AML opcode 18667754Smsmith * 4) Method blocked on a Wait() AML opcode 18767754Smsmith * 5) Method blocked to acquire the global lock 18867754Smsmith * 6) Method blocked to execute a serialized control method that is 18967754Smsmith * already executing 19067754Smsmith * 7) About to invoke a user-installed opregion handler 19167754Smsmith * 19267754Smsmith ******************************************************************************/ 19367754Smsmith 19467754Smsmithvoid 19577424SmsmithAcpiExExitInterpreter (void) 19667754Smsmith{ 19791116Smsmith ACPI_STATUS Status; 19867754Smsmith 19967754Smsmith 20091116Smsmith ACPI_FUNCTION_TRACE ("ExExitInterpreter"); 20167754Smsmith 20291116Smsmith 20391116Smsmith Status = AcpiUtReleaseMutex (ACPI_MTX_EXECUTE); 20499679Siwasaki if (ACPI_FAILURE (Status)) 20599679Siwasaki { 20699679Siwasaki ACPI_REPORT_ERROR (("Could not release interpreter mutex\n")); 20799679Siwasaki } 20891116Smsmith 20967754Smsmith return_VOID; 21067754Smsmith} 21167754Smsmith 21267754Smsmith 21367754Smsmith/******************************************************************************* 21467754Smsmith * 21577424Smsmith * FUNCTION: AcpiExValidateObjectType 21667754Smsmith * 21767754Smsmith * PARAMETERS: Type Object type to validate 21867754Smsmith * 21967754Smsmith * DESCRIPTION: Determine if a type is a valid ACPI object type 22067754Smsmith * 22167754Smsmith ******************************************************************************/ 22267754Smsmith 22367754SmsmithBOOLEAN 22477424SmsmithAcpiExValidateObjectType ( 22567754Smsmith ACPI_OBJECT_TYPE Type) 22667754Smsmith{ 22767754Smsmith 22891116Smsmith ACPI_FUNCTION_ENTRY (); 22983174Smsmith 23083174Smsmith 23167754Smsmith if ((Type > ACPI_TYPE_MAX && Type < INTERNAL_TYPE_BEGIN) || 23267754Smsmith (Type > INTERNAL_TYPE_MAX)) 23367754Smsmith { 23467754Smsmith return (FALSE); 23567754Smsmith } 23667754Smsmith 23767754Smsmith return (TRUE); 23867754Smsmith} 23967754Smsmith 24067754Smsmith 24167754Smsmith/******************************************************************************* 24267754Smsmith * 24377424Smsmith * FUNCTION: AcpiExTruncateFor32bitTable 24467754Smsmith * 24569450Smsmith * PARAMETERS: ObjDesc - Object to be truncated 24667754Smsmith * 24769450Smsmith * RETURN: none 24867754Smsmith * 24969450Smsmith * DESCRIPTION: Truncate a number to 32-bits if the currently executing method 25069450Smsmith * belongs to a 32-bit ACPI table. 25167754Smsmith * 25267754Smsmith ******************************************************************************/ 25367754Smsmith 25469450Smsmithvoid 25577424SmsmithAcpiExTruncateFor32bitTable ( 25699679Siwasaki ACPI_OPERAND_OBJECT *ObjDesc) 25767754Smsmith{ 25867754Smsmith 25991116Smsmith ACPI_FUNCTION_ENTRY (); 26083174Smsmith 26183174Smsmith 26269450Smsmith /* 26369450Smsmith * Object must be a valid number and we must be executing 26469450Smsmith * a control method 26569450Smsmith */ 26669450Smsmith if ((!ObjDesc) || 26799679Siwasaki (ACPI_GET_OBJECT_TYPE (ObjDesc) != ACPI_TYPE_INTEGER)) 26869450Smsmith { 26969450Smsmith return; 27069450Smsmith } 27169450Smsmith 27299679Siwasaki if (AcpiGbl_IntegerByteWidth == 4) 27369450Smsmith { 27469450Smsmith /* 27569450Smsmith * We are running a method that exists in a 32-bit ACPI table. 27669450Smsmith * Truncate the value to 32 bits by zeroing out the upper 32-bit field 27769450Smsmith */ 27871867Smsmith ObjDesc->Integer.Value &= (ACPI_INTEGER) ACPI_UINT32_MAX; 27969450Smsmith } 28067754Smsmith} 28167754Smsmith 28267754Smsmith 28367754Smsmith/******************************************************************************* 28467754Smsmith * 28577424Smsmith * FUNCTION: AcpiExAcquireGlobalLock 28667754Smsmith * 28791116Smsmith * PARAMETERS: FieldFlags - Flags with Lock rule: 28887031Smsmith * AlwaysLock or NeverLock 28967754Smsmith * 29067754Smsmith * RETURN: TRUE/FALSE indicating whether the lock was actually acquired 29167754Smsmith * 29267754Smsmith * DESCRIPTION: Obtain the global lock and keep track of this fact via two 29367754Smsmith * methods. A global variable keeps the state of the lock, and 29467754Smsmith * the state is returned to the caller. 29567754Smsmith * 29667754Smsmith ******************************************************************************/ 29767754Smsmith 29867754SmsmithBOOLEAN 29977424SmsmithAcpiExAcquireGlobalLock ( 30087031Smsmith UINT32 FieldFlags) 30167754Smsmith{ 30267754Smsmith BOOLEAN Locked = FALSE; 30367754Smsmith ACPI_STATUS Status; 30467754Smsmith 30567754Smsmith 30691116Smsmith ACPI_FUNCTION_TRACE ("ExAcquireGlobalLock"); 30767754Smsmith 30867754Smsmith 30987031Smsmith /* Only attempt lock if the AlwaysLock bit is set */ 31067754Smsmith 31187031Smsmith if (FieldFlags & AML_FIELD_LOCK_RULE_MASK) 31267754Smsmith { 31391116Smsmith /* We should attempt to get the lock, wait forever */ 31467754Smsmith 31591116Smsmith Status = AcpiEvAcquireGlobalLock (ACPI_UINT32_MAX); 31677424Smsmith if (ACPI_SUCCESS (Status)) 31767754Smsmith { 31877424Smsmith Locked = TRUE; 31967754Smsmith } 32077424Smsmith else 32167754Smsmith { 32282367Smsmith ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not acquire Global Lock, %s\n", 32380062Smsmith AcpiFormatException (Status))); 32467754Smsmith } 32567754Smsmith } 32667754Smsmith 32767754Smsmith return_VALUE (Locked); 32867754Smsmith} 32967754Smsmith 33067754Smsmith 33167754Smsmith/******************************************************************************* 33267754Smsmith * 33377424Smsmith * FUNCTION: AcpiExReleaseGlobalLock 33467754Smsmith * 33567754Smsmith * PARAMETERS: LockedByMe - Return value from corresponding call to 33667754Smsmith * AcquireGlobalLock. 33767754Smsmith * 33867754Smsmith * RETURN: Status 33967754Smsmith * 34067754Smsmith * DESCRIPTION: Release the global lock if it is locked. 34167754Smsmith * 34267754Smsmith ******************************************************************************/ 34367754Smsmith 34499679Siwasakivoid 34577424SmsmithAcpiExReleaseGlobalLock ( 34667754Smsmith BOOLEAN LockedByMe) 34767754Smsmith{ 34899679Siwasaki ACPI_STATUS Status; 34967754Smsmith 35099679Siwasaki 35191116Smsmith ACPI_FUNCTION_TRACE ("ExReleaseGlobalLock"); 35267754Smsmith 35367754Smsmith 35467754Smsmith /* Only attempt unlock if the caller locked it */ 35567754Smsmith 35667754Smsmith if (LockedByMe) 35767754Smsmith { 35877424Smsmith /* OK, now release the lock */ 35967754Smsmith 36099679Siwasaki Status = AcpiEvReleaseGlobalLock (); 36199679Siwasaki if (ACPI_FAILURE (Status)) 36299679Siwasaki { 36399679Siwasaki /* Report the error, but there isn't much else we can do */ 36499679Siwasaki 36599679Siwasaki ACPI_REPORT_ERROR (("Could not release ACPI Global Lock\n")); 36699679Siwasaki } 36767754Smsmith } 36867754Smsmith} 36967754Smsmith 37067754Smsmith 37167754Smsmith/******************************************************************************* 37267754Smsmith * 37377424Smsmith * FUNCTION: AcpiExDigitsNeeded 37467754Smsmith * 37584491Smsmith * PARAMETERS: Value - Value to be represented 37684491Smsmith * Base - Base of representation 37767754Smsmith * 37884491Smsmith * RETURN: the number of digits needed to represent Value in Base 37967754Smsmith * 38067754Smsmith ******************************************************************************/ 38167754Smsmith 38267754SmsmithUINT32 38377424SmsmithAcpiExDigitsNeeded ( 38484491Smsmith ACPI_INTEGER Value, 38584491Smsmith UINT32 Base) 38667754Smsmith{ 38799679Siwasaki UINT32 NumDigits; 38899679Siwasaki ACPI_INTEGER CurrentValue; 38999679Siwasaki ACPI_INTEGER Quotient; 39067754Smsmith 39167754Smsmith 39291116Smsmith ACPI_FUNCTION_TRACE ("ExDigitsNeeded"); 39367754Smsmith 39467754Smsmith 39599679Siwasaki /* 39699679Siwasaki * ACPI_INTEGER is unsigned, so we don't worry about a '-' 39799679Siwasaki */ 39899679Siwasaki CurrentValue = Value; 39999679Siwasaki NumDigits = 0; 40099679Siwasaki 40199679Siwasaki while (CurrentValue) 40267754Smsmith { 40399679Siwasaki (void) AcpiUtShortDivide (&CurrentValue, Base, &Quotient, NULL); 40499679Siwasaki NumDigits++; 40599679Siwasaki CurrentValue = Quotient; 40667754Smsmith } 40767754Smsmith 40867754Smsmith return_VALUE (NumDigits); 40967754Smsmith} 41067754Smsmith 41167754Smsmith 41267754Smsmith/******************************************************************************* 41367754Smsmith * 41477424Smsmith * FUNCTION: AcpiExEisaIdToString 41567754Smsmith * 41667754Smsmith * PARAMETERS: NumericId - EISA ID to be converted 41767754Smsmith * OutString - Where to put the converted string (8 bytes) 41867754Smsmith * 41967754Smsmith * DESCRIPTION: Convert a numeric EISA ID to string representation 42067754Smsmith * 42167754Smsmith ******************************************************************************/ 42267754Smsmith 42399679Siwasakivoid 42477424SmsmithAcpiExEisaIdToString ( 42567754Smsmith UINT32 NumericId, 42667754Smsmith NATIVE_CHAR *OutString) 42767754Smsmith{ 42899679Siwasaki UINT32 EisaId; 42967754Smsmith 43083174Smsmith 43191116Smsmith ACPI_FUNCTION_ENTRY (); 43283174Smsmith 43383174Smsmith 43499679Siwasaki /* Swap ID to big-endian to get contiguous bits */ 43567754Smsmith 43699679Siwasaki EisaId = AcpiUtDwordByteSwap (NumericId); 43767754Smsmith 43899679Siwasaki OutString[0] = (char) ('@' + ((EisaId >> 26) & 0x1f)); 43999679Siwasaki OutString[1] = (char) ('@' + ((EisaId >> 21) & 0x1f)); 44099679Siwasaki OutString[2] = (char) ('@' + ((EisaId >> 16) & 0x1f)); 44199679Siwasaki OutString[3] = AcpiUtHexToAsciiChar ((ACPI_INTEGER) EisaId, 12); 44299679Siwasaki OutString[4] = AcpiUtHexToAsciiChar ((ACPI_INTEGER) EisaId, 8); 44399679Siwasaki OutString[5] = AcpiUtHexToAsciiChar ((ACPI_INTEGER) EisaId, 4); 44499679Siwasaki OutString[6] = AcpiUtHexToAsciiChar ((ACPI_INTEGER) EisaId, 0); 44567754Smsmith OutString[7] = 0; 44667754Smsmith} 44767754Smsmith 44867754Smsmith 44967754Smsmith/******************************************************************************* 45067754Smsmith * 45177424Smsmith * FUNCTION: AcpiExUnsignedIntegerToString 45267754Smsmith * 45367754Smsmith * PARAMETERS: Value - Value to be converted 45467754Smsmith * OutString - Where to put the converted string (8 bytes) 45567754Smsmith * 45667754Smsmith * RETURN: Convert a number to string representation 45767754Smsmith * 45867754Smsmith ******************************************************************************/ 45967754Smsmith 46099679Siwasakivoid 46177424SmsmithAcpiExUnsignedIntegerToString ( 46267754Smsmith ACPI_INTEGER Value, 46367754Smsmith NATIVE_CHAR *OutString) 46467754Smsmith{ 46567754Smsmith UINT32 Count; 46667754Smsmith UINT32 DigitsNeeded; 46784491Smsmith UINT32 Remainder; 46899679Siwasaki ACPI_INTEGER Quotient; 46967754Smsmith 47067754Smsmith 47191116Smsmith ACPI_FUNCTION_ENTRY (); 47283174Smsmith 47383174Smsmith 47477424Smsmith DigitsNeeded = AcpiExDigitsNeeded (Value, 10); 47584491Smsmith OutString[DigitsNeeded] = 0; 47667754Smsmith 47767754Smsmith for (Count = DigitsNeeded; Count > 0; Count--) 47867754Smsmith { 47999679Siwasaki (void) AcpiUtShortDivide (&Value, 10, &Quotient, &Remainder); 48099679Siwasaki OutString[Count-1] = (NATIVE_CHAR) ('0' + Remainder);\ 48199679Siwasaki Value = Quotient; 48267754Smsmith } 48367754Smsmith} 48467754Smsmith 48567754Smsmith 486