exutils.c revision 138287
167754Smsmith
267754Smsmith/******************************************************************************
367754Smsmith *
477424Smsmith * Module Name: exutils - interpreter/scanner utilities
5138287Smarks *              $Revision: 112 $
667754Smsmith *
767754Smsmith *****************************************************************************/
867754Smsmith
967754Smsmith/******************************************************************************
1067754Smsmith *
1167754Smsmith * 1. Copyright Notice
1267754Smsmith *
13126372Snjl * Some or all of this work - Copyright (c) 1999 - 2004, 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
145100966Siwasaki#ifndef ACPI_NO_METHOD_EXECUTION
146100966Siwasaki
147100966Siwasaki/*******************************************************************************
148100966Siwasaki *
14977424Smsmith * FUNCTION:    AcpiExEnterInterpreter
15067754Smsmith *
15167754Smsmith * PARAMETERS:  None
15267754Smsmith *
15391116Smsmith * DESCRIPTION: Enter the interpreter execution region.  Failure to enter
15491116Smsmith *              the interpreter region is a fatal system error
15567754Smsmith *
15667754Smsmith ******************************************************************************/
15767754Smsmith
15877424SmsmithACPI_STATUS
15977424SmsmithAcpiExEnterInterpreter (void)
16067754Smsmith{
16177424Smsmith    ACPI_STATUS             Status;
16267754Smsmith
16391116Smsmith    ACPI_FUNCTION_TRACE ("ExEnterInterpreter");
16467754Smsmith
16567754Smsmith
16677424Smsmith    Status = AcpiUtAcquireMutex (ACPI_MTX_EXECUTE);
16791116Smsmith    if (ACPI_FAILURE (Status))
16891116Smsmith    {
16999679Siwasaki        ACPI_REPORT_ERROR (("Could not acquire interpreter mutex\n"));
17091116Smsmith    }
17191116Smsmith
17277424Smsmith    return_ACPI_STATUS (Status);
17367754Smsmith}
17467754Smsmith
17567754Smsmith
17667754Smsmith/*******************************************************************************
17767754Smsmith *
17877424Smsmith * FUNCTION:    AcpiExExitInterpreter
17967754Smsmith *
18067754Smsmith * PARAMETERS:  None
18167754Smsmith *
18267754Smsmith * DESCRIPTION: Exit the interpreter execution region
18367754Smsmith *
18467754Smsmith * Cases where the interpreter is unlocked:
18567754Smsmith *      1) Completion of the execution of a control method
18667754Smsmith *      2) Method blocked on a Sleep() AML opcode
18767754Smsmith *      3) Method blocked on an Acquire() AML opcode
18867754Smsmith *      4) Method blocked on a Wait() AML opcode
18967754Smsmith *      5) Method blocked to acquire the global lock
19067754Smsmith *      6) Method blocked to execute a serialized control method that is
19167754Smsmith *          already executing
19267754Smsmith *      7) About to invoke a user-installed opregion handler
19367754Smsmith *
19467754Smsmith ******************************************************************************/
19567754Smsmith
19667754Smsmithvoid
19777424SmsmithAcpiExExitInterpreter (void)
19867754Smsmith{
19991116Smsmith    ACPI_STATUS             Status;
20067754Smsmith
20167754Smsmith
20291116Smsmith    ACPI_FUNCTION_TRACE ("ExExitInterpreter");
20367754Smsmith
20491116Smsmith
20591116Smsmith    Status = AcpiUtReleaseMutex (ACPI_MTX_EXECUTE);
20699679Siwasaki    if (ACPI_FAILURE (Status))
20799679Siwasaki    {
20899679Siwasaki        ACPI_REPORT_ERROR (("Could not release interpreter mutex\n"));
20999679Siwasaki    }
21091116Smsmith
21167754Smsmith    return_VOID;
21267754Smsmith}
21367754Smsmith
21467754Smsmith
21567754Smsmith/*******************************************************************************
21667754Smsmith *
21777424Smsmith * FUNCTION:    AcpiExTruncateFor32bitTable
21867754Smsmith *
21969450Smsmith * PARAMETERS:  ObjDesc         - Object to be truncated
22067754Smsmith *
22169450Smsmith * RETURN:      none
22267754Smsmith *
22369450Smsmith * DESCRIPTION: Truncate a number to 32-bits if the currently executing method
22469450Smsmith *              belongs to a 32-bit ACPI table.
22567754Smsmith *
22667754Smsmith ******************************************************************************/
22767754Smsmith
22869450Smsmithvoid
22977424SmsmithAcpiExTruncateFor32bitTable (
23099679Siwasaki    ACPI_OPERAND_OBJECT     *ObjDesc)
23167754Smsmith{
23267754Smsmith
23391116Smsmith    ACPI_FUNCTION_ENTRY ();
23483174Smsmith
23583174Smsmith
23669450Smsmith    /*
23769450Smsmith     * Object must be a valid number and we must be executing
23869450Smsmith     * a control method
23969450Smsmith     */
24069450Smsmith    if ((!ObjDesc) ||
24199679Siwasaki        (ACPI_GET_OBJECT_TYPE (ObjDesc) != ACPI_TYPE_INTEGER))
24269450Smsmith    {
24369450Smsmith        return;
24469450Smsmith    }
24569450Smsmith
24699679Siwasaki    if (AcpiGbl_IntegerByteWidth == 4)
24769450Smsmith    {
24869450Smsmith        /*
24969450Smsmith         * We are running a method that exists in a 32-bit ACPI table.
25069450Smsmith         * Truncate the value to 32 bits by zeroing out the upper 32-bit field
25169450Smsmith         */
25271867Smsmith        ObjDesc->Integer.Value &= (ACPI_INTEGER) ACPI_UINT32_MAX;
25369450Smsmith    }
25467754Smsmith}
25567754Smsmith
25667754Smsmith
25767754Smsmith/*******************************************************************************
25867754Smsmith *
25977424Smsmith * FUNCTION:    AcpiExAcquireGlobalLock
26067754Smsmith *
26191116Smsmith * PARAMETERS:  FieldFlags            - Flags with Lock rule:
26287031Smsmith *                                      AlwaysLock or NeverLock
26367754Smsmith *
26467754Smsmith * RETURN:      TRUE/FALSE indicating whether the lock was actually acquired
26567754Smsmith *
26667754Smsmith * DESCRIPTION: Obtain the global lock and keep track of this fact via two
26767754Smsmith *              methods.  A global variable keeps the state of the lock, and
26867754Smsmith *              the state is returned to the caller.
26967754Smsmith *
27067754Smsmith ******************************************************************************/
27167754Smsmith
27267754SmsmithBOOLEAN
27377424SmsmithAcpiExAcquireGlobalLock (
27487031Smsmith    UINT32                  FieldFlags)
27567754Smsmith{
27667754Smsmith    BOOLEAN                 Locked = FALSE;
27767754Smsmith    ACPI_STATUS             Status;
27867754Smsmith
27967754Smsmith
28091116Smsmith    ACPI_FUNCTION_TRACE ("ExAcquireGlobalLock");
28167754Smsmith
28267754Smsmith
28387031Smsmith    /* Only attempt lock if the AlwaysLock bit is set */
28467754Smsmith
28587031Smsmith    if (FieldFlags & AML_FIELD_LOCK_RULE_MASK)
28667754Smsmith    {
28791116Smsmith        /* We should attempt to get the lock, wait forever */
28867754Smsmith
289107325Siwasaki        Status = AcpiEvAcquireGlobalLock (ACPI_WAIT_FOREVER);
29077424Smsmith        if (ACPI_SUCCESS (Status))
29167754Smsmith        {
29277424Smsmith            Locked = TRUE;
29367754Smsmith        }
29477424Smsmith        else
29567754Smsmith        {
29682367Smsmith            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not acquire Global Lock, %s\n",
29780062Smsmith                AcpiFormatException (Status)));
29867754Smsmith        }
29967754Smsmith    }
30067754Smsmith
30167754Smsmith    return_VALUE (Locked);
30267754Smsmith}
30367754Smsmith
30467754Smsmith
30567754Smsmith/*******************************************************************************
30667754Smsmith *
30777424Smsmith * FUNCTION:    AcpiExReleaseGlobalLock
30867754Smsmith *
30967754Smsmith * PARAMETERS:  LockedByMe      - Return value from corresponding call to
31067754Smsmith *                                AcquireGlobalLock.
31167754Smsmith *
31267754Smsmith * RETURN:      Status
31367754Smsmith *
31467754Smsmith * DESCRIPTION: Release the global lock if it is locked.
31567754Smsmith *
31667754Smsmith ******************************************************************************/
31767754Smsmith
31899679Siwasakivoid
31977424SmsmithAcpiExReleaseGlobalLock (
32067754Smsmith    BOOLEAN                 LockedByMe)
32167754Smsmith{
32299679Siwasaki    ACPI_STATUS             Status;
32367754Smsmith
32499679Siwasaki
32591116Smsmith    ACPI_FUNCTION_TRACE ("ExReleaseGlobalLock");
32667754Smsmith
32767754Smsmith
32867754Smsmith    /* Only attempt unlock if the caller locked it */
32967754Smsmith
33067754Smsmith    if (LockedByMe)
33167754Smsmith    {
33277424Smsmith        /* OK, now release the lock */
33367754Smsmith
33499679Siwasaki        Status = AcpiEvReleaseGlobalLock ();
33599679Siwasaki        if (ACPI_FAILURE (Status))
33699679Siwasaki        {
33799679Siwasaki            /* Report the error, but there isn't much else we can do */
33899679Siwasaki
339102550Siwasaki            ACPI_REPORT_ERROR (("Could not release ACPI Global Lock, %s\n",
340102550Siwasaki                AcpiFormatException (Status)));
34199679Siwasaki        }
34267754Smsmith    }
343100966Siwasaki
344100966Siwasaki    return_VOID;
34567754Smsmith}
34667754Smsmith
34767754Smsmith
34867754Smsmith/*******************************************************************************
34967754Smsmith *
35077424Smsmith * FUNCTION:    AcpiExDigitsNeeded
35167754Smsmith *
35284491Smsmith * PARAMETERS:  Value           - Value to be represented
35384491Smsmith *              Base            - Base of representation
35467754Smsmith *
35584491Smsmith * RETURN:      the number of digits needed to represent Value in Base
35667754Smsmith *
35767754Smsmith ******************************************************************************/
35867754Smsmith
35967754SmsmithUINT32
36077424SmsmithAcpiExDigitsNeeded (
36184491Smsmith    ACPI_INTEGER            Value,
36284491Smsmith    UINT32                  Base)
36367754Smsmith{
36499679Siwasaki    UINT32                  NumDigits;
36599679Siwasaki    ACPI_INTEGER            CurrentValue;
36667754Smsmith
36767754Smsmith
36891116Smsmith    ACPI_FUNCTION_TRACE ("ExDigitsNeeded");
36967754Smsmith
37067754Smsmith
371138287Smarks    /* ACPI_INTEGER is unsigned, so we don't worry about a '-' prefix */
372138287Smarks
373138287Smarks    if (Value == 0)
374117521Snjl    {
375123315Snjl        return_VALUE (1);
376117521Snjl    }
377117521Snjl
378138287Smarks    CurrentValue = Value;
37999679Siwasaki    NumDigits = 0;
38099679Siwasaki
381138287Smarks    /* Count the digits in the requested base */
382138287Smarks
38399679Siwasaki    while (CurrentValue)
38467754Smsmith    {
385138287Smarks        (void) AcpiUtShortDivide (CurrentValue, Base, &CurrentValue, NULL);
38699679Siwasaki        NumDigits++;
38767754Smsmith    }
38867754Smsmith
38967754Smsmith    return_VALUE (NumDigits);
39067754Smsmith}
39167754Smsmith
39267754Smsmith
39367754Smsmith/*******************************************************************************
39467754Smsmith *
39577424Smsmith * FUNCTION:    AcpiExEisaIdToString
39667754Smsmith *
39767754Smsmith * PARAMETERS:  NumericId       - EISA ID to be converted
39867754Smsmith *              OutString       - Where to put the converted string (8 bytes)
39967754Smsmith *
40067754Smsmith * DESCRIPTION: Convert a numeric EISA ID to string representation
40167754Smsmith *
40267754Smsmith ******************************************************************************/
40367754Smsmith
40499679Siwasakivoid
40577424SmsmithAcpiExEisaIdToString (
40667754Smsmith    UINT32                  NumericId,
407114237Snjl    char                    *OutString)
40867754Smsmith{
40999679Siwasaki    UINT32                  EisaId;
41067754Smsmith
41183174Smsmith
41291116Smsmith    ACPI_FUNCTION_ENTRY ();
41383174Smsmith
41483174Smsmith
41599679Siwasaki    /* Swap ID to big-endian to get contiguous bits */
41667754Smsmith
41799679Siwasaki    EisaId = AcpiUtDwordByteSwap (NumericId);
41867754Smsmith
419107325Siwasaki    OutString[0] = (char) ('@' + (((unsigned long) EisaId >> 26) & 0x1f));
42099679Siwasaki    OutString[1] = (char) ('@' + ((EisaId >> 21) & 0x1f));
42199679Siwasaki    OutString[2] = (char) ('@' + ((EisaId >> 16) & 0x1f));
42299679Siwasaki    OutString[3] = AcpiUtHexToAsciiChar ((ACPI_INTEGER) EisaId, 12);
42399679Siwasaki    OutString[4] = AcpiUtHexToAsciiChar ((ACPI_INTEGER) EisaId, 8);
42499679Siwasaki    OutString[5] = AcpiUtHexToAsciiChar ((ACPI_INTEGER) EisaId, 4);
42599679Siwasaki    OutString[6] = AcpiUtHexToAsciiChar ((ACPI_INTEGER) EisaId, 0);
42667754Smsmith    OutString[7] = 0;
42767754Smsmith}
42867754Smsmith
42967754Smsmith
43067754Smsmith/*******************************************************************************
43167754Smsmith *
43277424Smsmith * FUNCTION:    AcpiExUnsignedIntegerToString
43367754Smsmith *
43467754Smsmith * PARAMETERS:  Value           - Value to be converted
43567754Smsmith *              OutString       - Where to put the converted string (8 bytes)
43667754Smsmith *
43767754Smsmith * RETURN:      Convert a number to string representation
43867754Smsmith *
43967754Smsmith ******************************************************************************/
44067754Smsmith
44199679Siwasakivoid
44277424SmsmithAcpiExUnsignedIntegerToString (
44367754Smsmith    ACPI_INTEGER            Value,
444114237Snjl    char                    *OutString)
44567754Smsmith{
44667754Smsmith    UINT32                  Count;
44767754Smsmith    UINT32                  DigitsNeeded;
44884491Smsmith    UINT32                  Remainder;
44967754Smsmith
45067754Smsmith
45191116Smsmith    ACPI_FUNCTION_ENTRY ();
45283174Smsmith
45383174Smsmith
45477424Smsmith    DigitsNeeded = AcpiExDigitsNeeded (Value, 10);
45584491Smsmith    OutString[DigitsNeeded] = 0;
45667754Smsmith
45767754Smsmith    for (Count = DigitsNeeded; Count > 0; Count--)
45867754Smsmith    {
459138287Smarks        (void) AcpiUtShortDivide (Value, 10, &Value, &Remainder);
460114237Snjl        OutString[Count-1] = (char) ('0' + Remainder);\
46167754Smsmith    }
46267754Smsmith}
46367754Smsmith
464100966Siwasaki#endif
465