hwacpi.c revision 82367
171867Smsmith
267754Smsmith/******************************************************************************
367754Smsmith *
471867Smsmith * Module Name: hwacpi - ACPI Hardware Initialization/Mode Interface
582367Smsmith *              $Revision: 43 $
667754Smsmith *
767754Smsmith *****************************************************************************/
867754Smsmith
967754Smsmith/******************************************************************************
1067754Smsmith *
1167754Smsmith * 1. Copyright Notice
1267754Smsmith *
1371867Smsmith * Some or all of this work - Copyright (c) 1999, 2000, 2001, 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
11867754Smsmith#define __HWACPI_C__
11967754Smsmith
12067754Smsmith#include "acpi.h"
12167754Smsmith#include "achware.h"
12267754Smsmith
12367754Smsmith
12477424Smsmith#define _COMPONENT          ACPI_HARDWARE
12567754Smsmith        MODULE_NAME         ("hwacpi")
12667754Smsmith
12767754Smsmith
12867754Smsmith/******************************************************************************
12967754Smsmith *
13067754Smsmith * FUNCTION:    AcpiHwInitialize
13167754Smsmith *
13267754Smsmith * PARAMETERS:  None
13367754Smsmith *
13467754Smsmith * RETURN:      Status
13567754Smsmith *
13667754Smsmith * DESCRIPTION: Initialize and validate various ACPI registers
13767754Smsmith *
13867754Smsmith ******************************************************************************/
13967754Smsmith
14067754SmsmithACPI_STATUS
14167754SmsmithAcpiHwInitialize (
14267754Smsmith    void)
14367754Smsmith{
14467754Smsmith    ACPI_STATUS             Status = AE_OK;
14567754Smsmith    UINT32                  Index;
14667754Smsmith
14767754Smsmith
14867754Smsmith    FUNCTION_TRACE ("HwInitialize");
14967754Smsmith
15067754Smsmith
15167754Smsmith    /* We must have the ACPI tables by the time we get here */
15267754Smsmith
15369450Smsmith    if (!AcpiGbl_FADT)
15467754Smsmith    {
15567754Smsmith        AcpiGbl_RestoreAcpiChipset = FALSE;
15667754Smsmith
15782367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "No FADT!\n"));
15867754Smsmith
15967754Smsmith        return_ACPI_STATUS (AE_NO_ACPI_TABLES);
16067754Smsmith    }
16167754Smsmith
16267754Smsmith    /* Must support *some* mode! */
16367754Smsmith/*
16467754Smsmith    if (!(SystemFlags & SYS_MODES_MASK))
16567754Smsmith    {
16667754Smsmith        RestoreAcpiChipset = FALSE;
16767754Smsmith
16882367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
16982367Smsmith            "Supported modes uninitialized!\n"));
17067754Smsmith        return_ACPI_STATUS (AE_ERROR);
17167754Smsmith    }
17267754Smsmith
17367754Smsmith*/
17467754Smsmith
17567754Smsmith
17667754Smsmith    switch (AcpiGbl_SystemFlags & SYS_MODES_MASK)
17767754Smsmith    {
17867754Smsmith        /* Identify current ACPI/legacy mode   */
17967754Smsmith
18067754Smsmith    case (SYS_MODE_ACPI):
18167754Smsmith
18267754Smsmith        AcpiGbl_OriginalMode = SYS_MODE_ACPI;
18382367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "System supports ACPI mode only.\n"));
18467754Smsmith        break;
18567754Smsmith
18667754Smsmith
18767754Smsmith    case (SYS_MODE_LEGACY):
18867754Smsmith
18967754Smsmith        AcpiGbl_OriginalMode = SYS_MODE_LEGACY;
19082367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
19182367Smsmith            "Tables loaded from buffer, hardware assumed to support LEGACY mode only.\n"));
19267754Smsmith        break;
19367754Smsmith
19467754Smsmith
19567754Smsmith    case (SYS_MODE_ACPI | SYS_MODE_LEGACY):
19667754Smsmith
19767754Smsmith        if (AcpiHwGetMode () == SYS_MODE_ACPI)
19867754Smsmith        {
19967754Smsmith            AcpiGbl_OriginalMode = SYS_MODE_ACPI;
20067754Smsmith        }
20167754Smsmith        else
20267754Smsmith        {
20367754Smsmith            AcpiGbl_OriginalMode = SYS_MODE_LEGACY;
20467754Smsmith        }
20567754Smsmith
20682367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
20782367Smsmith            "System supports both ACPI and LEGACY modes.\n"));
20867754Smsmith
20982367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
21082367Smsmith            "System is currently in %s mode.\n",
21167754Smsmith            (AcpiGbl_OriginalMode == SYS_MODE_ACPI) ? "ACPI" : "LEGACY"));
21267754Smsmith        break;
21367754Smsmith    }
21467754Smsmith
21567754Smsmith
21667754Smsmith    if (AcpiGbl_SystemFlags & SYS_MODE_ACPI)
21767754Smsmith    {
21867754Smsmith        /* Target system supports ACPI mode */
21967754Smsmith
22067754Smsmith        /*
22169450Smsmith         * The purpose of this code is to save the initial state
22267754Smsmith         * of the ACPI event enable registers. An exit function will be
22367754Smsmith         * registered which will restore this state when the application
22467754Smsmith         * exits. The exit function will also clear all of the ACPI event
22567754Smsmith         * status bits prior to restoring the original mode.
22667754Smsmith         *
22767754Smsmith         * The location of the PM1aEvtBlk enable registers is defined as the
22869450Smsmith         * base of PM1aEvtBlk + DIV_2(PM1aEvtBlkLength). Since the spec further
22967754Smsmith         * fully defines the PM1aEvtBlk to be a total of 4 bytes, the offset
23067754Smsmith         * for the enable registers is always 2 from the base. It is hard
23167754Smsmith         * coded here. If this changes in the spec, this code will need to
23267754Smsmith         * be modified. The PM1bEvtBlk behaves as expected.
23367754Smsmith         */
23467754Smsmith
23580062Smsmith        AcpiGbl_Pm1EnableRegisterSave = (UINT16) AcpiHwRegisterRead (
23680062Smsmith                                                    ACPI_MTX_LOCK, PM1_EN);
23767754Smsmith
23867754Smsmith
23967754Smsmith        /*
24067754Smsmith         * The GPEs behave similarly, except that the length of the register
24167754Smsmith         * block is not fixed, so the buffer must be allocated with malloc
24267754Smsmith         */
24367754Smsmith
24471867Smsmith        if (ACPI_VALID_ADDRESS (AcpiGbl_FADT->XGpe0Blk.Address) &&
24570243Smsmith            AcpiGbl_FADT->Gpe0BlkLen)
24667754Smsmith        {
24769450Smsmith            /* GPE0 specified in FADT  */
24867754Smsmith
24980062Smsmith            AcpiGbl_Gpe0EnableRegisterSave = ACPI_MEM_ALLOCATE (
25080062Smsmith                                            DIV_2 (AcpiGbl_FADT->Gpe0BlkLen));
25167754Smsmith            if (!AcpiGbl_Gpe0EnableRegisterSave)
25267754Smsmith            {
25367754Smsmith                return_ACPI_STATUS (AE_NO_MEMORY);
25467754Smsmith            }
25567754Smsmith
25667754Smsmith            /* Save state of GPE0 enable bits */
25767754Smsmith
25869450Smsmith            for (Index = 0; Index < DIV_2 (AcpiGbl_FADT->Gpe0BlkLen); Index++)
25967754Smsmith            {
26067754Smsmith                AcpiGbl_Gpe0EnableRegisterSave[Index] =
26169450Smsmith                    (UINT8) AcpiHwRegisterRead (ACPI_MTX_LOCK, GPE0_EN_BLOCK | Index);
26267754Smsmith            }
26367754Smsmith        }
26467754Smsmith
26567754Smsmith        else
26667754Smsmith        {
26767754Smsmith            AcpiGbl_Gpe0EnableRegisterSave = NULL;
26867754Smsmith        }
26967754Smsmith
27071867Smsmith        if (ACPI_VALID_ADDRESS (AcpiGbl_FADT->XGpe1Blk.Address) &&
27170243Smsmith            AcpiGbl_FADT->Gpe1BlkLen)
27267754Smsmith        {
27367754Smsmith            /* GPE1 defined */
27467754Smsmith
27580062Smsmith            AcpiGbl_Gpe1EnableRegisterSave = ACPI_MEM_ALLOCATE (
27680062Smsmith                                            DIV_2 (AcpiGbl_FADT->Gpe1BlkLen));
27767754Smsmith            if (!AcpiGbl_Gpe1EnableRegisterSave)
27867754Smsmith            {
27967754Smsmith                return_ACPI_STATUS (AE_NO_MEMORY);
28067754Smsmith            }
28167754Smsmith
28267754Smsmith            /* save state of GPE1 enable bits */
28367754Smsmith
28469450Smsmith            for (Index = 0; Index < DIV_2 (AcpiGbl_FADT->Gpe1BlkLen); Index++)
28567754Smsmith            {
28667754Smsmith                AcpiGbl_Gpe1EnableRegisterSave[Index] =
28769450Smsmith                    (UINT8) AcpiHwRegisterRead (ACPI_MTX_LOCK, GPE1_EN_BLOCK | Index);
28867754Smsmith            }
28967754Smsmith        }
29067754Smsmith
29167754Smsmith        else
29267754Smsmith        {
29367754Smsmith            AcpiGbl_Gpe1EnableRegisterSave = NULL;
29467754Smsmith        }
29567754Smsmith    }
29667754Smsmith
29767754Smsmith    return_ACPI_STATUS (Status);
29867754Smsmith}
29967754Smsmith
30067754Smsmith
30167754Smsmith/******************************************************************************
30267754Smsmith *
30367754Smsmith * FUNCTION:    AcpiHwSetMode
30467754Smsmith *
30567754Smsmith * PARAMETERS:  Mode            - SYS_MODE_ACPI or SYS_MODE_LEGACY
30667754Smsmith *
30767754Smsmith * RETURN:      Status
30867754Smsmith *
30967754Smsmith * DESCRIPTION: Transitions the system into the requested mode or does nothing
31067754Smsmith *              if the system is already in that mode.
31167754Smsmith *
31267754Smsmith ******************************************************************************/
31367754Smsmith
31467754SmsmithACPI_STATUS
31567754SmsmithAcpiHwSetMode (
31667754Smsmith    UINT32                  Mode)
31767754Smsmith{
31867754Smsmith
31977424Smsmith    ACPI_STATUS             Status = AE_NO_HARDWARE_RESPONSE;
32067754Smsmith
32167754Smsmith    FUNCTION_TRACE ("HwSetMode");
32267754Smsmith
32367754Smsmith
32467754Smsmith    if (Mode == SYS_MODE_ACPI)
32567754Smsmith    {
32667754Smsmith        /* BIOS should have disabled ALL fixed and GP events */
32767754Smsmith
32880062Smsmith        AcpiOsWritePort (AcpiGbl_FADT->SmiCmd, AcpiGbl_FADT->AcpiEnable, 8);
32982367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Attempting to enable ACPI mode\n"));
33067754Smsmith    }
33167754Smsmith
33267754Smsmith    else if (Mode == SYS_MODE_LEGACY)
33367754Smsmith    {
33467754Smsmith        /*
33567754Smsmith         * BIOS should clear all fixed status bits and restore fixed event
33667754Smsmith         * enable bits to default
33767754Smsmith         */
33867754Smsmith
33980062Smsmith        AcpiOsWritePort (AcpiGbl_FADT->SmiCmd, AcpiGbl_FADT->AcpiDisable, 8);
34082367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
34182367Smsmith                    "Attempting to enable Legacy (non-ACPI) mode\n"));
34267754Smsmith    }
34367754Smsmith
34467754Smsmith    if (AcpiHwGetMode () == Mode)
34567754Smsmith    {
34682367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Mode %X successfully enabled\n", Mode));
34767754Smsmith        Status = AE_OK;
34867754Smsmith    }
34967754Smsmith
35067754Smsmith    return_ACPI_STATUS (Status);
35167754Smsmith}
35267754Smsmith
35367754Smsmith
35467754Smsmith/******************************************************************************
35567754Smsmith *
35667754Smsmith * FUNCTION:    AcpiHwGetMode
35767754Smsmith *
35867754Smsmith * PARAMETERS:  none
35967754Smsmith *
36067754Smsmith * RETURN:      SYS_MODE_ACPI or SYS_MODE_LEGACY
36167754Smsmith *
36267754Smsmith * DESCRIPTION: Return current operating state of system.  Determined by
36367754Smsmith *              querying the SCI_EN bit.
36467754Smsmith *
36567754Smsmith ******************************************************************************/
36667754Smsmith
36767754SmsmithUINT32
36867754SmsmithAcpiHwGetMode (void)
36967754Smsmith{
37067754Smsmith
37167754Smsmith    FUNCTION_TRACE ("HwGetMode");
37267754Smsmith
37367754Smsmith
37469450Smsmith    if (AcpiHwRegisterBitAccess (ACPI_READ, ACPI_MTX_LOCK, SCI_EN))
37567754Smsmith    {
37667754Smsmith        return_VALUE (SYS_MODE_ACPI);
37767754Smsmith    }
37867754Smsmith    else
37967754Smsmith    {
38067754Smsmith        return_VALUE (SYS_MODE_LEGACY);
38167754Smsmith    }
38267754Smsmith}
38367754Smsmith
38467754Smsmith/******************************************************************************
38567754Smsmith *
38667754Smsmith * FUNCTION:    AcpiHwGetModeCapabilities
38767754Smsmith *
38867754Smsmith * PARAMETERS:  none
38967754Smsmith *
39067754Smsmith * RETURN:      logical OR of SYS_MODE_ACPI and SYS_MODE_LEGACY determined at initial
39167754Smsmith *              system state.
39267754Smsmith *
39367754Smsmith * DESCRIPTION: Returns capablities of system
39467754Smsmith *
39567754Smsmith ******************************************************************************/
39667754Smsmith
39767754SmsmithUINT32
39867754SmsmithAcpiHwGetModeCapabilities (void)
39967754Smsmith{
40067754Smsmith
40167754Smsmith    FUNCTION_TRACE ("HwGetModeCapabilities");
40267754Smsmith
40367754Smsmith
40467754Smsmith    if (!(AcpiGbl_SystemFlags & SYS_MODES_MASK))
40567754Smsmith    {
40667754Smsmith        if (AcpiHwGetMode () == SYS_MODE_LEGACY)
40767754Smsmith        {
40867754Smsmith            /*
40967754Smsmith             * Assume that if this call is being made, AcpiInit has been called
41067754Smsmith             * and ACPI support has been established by the presence of the
41167754Smsmith             * tables.  Therefore since we're in SYS_MODE_LEGACY, the system
41267754Smsmith             * must support both modes
41367754Smsmith             */
41467754Smsmith
41567754Smsmith            AcpiGbl_SystemFlags |= (SYS_MODE_ACPI | SYS_MODE_LEGACY);
41667754Smsmith        }
41767754Smsmith
41867754Smsmith        else
41967754Smsmith        {
42067754Smsmith            /* TBD: [Investigate] !!! this may be unsafe... */
42167754Smsmith            /*
42267754Smsmith             * system is is ACPI mode, so try to switch back to LEGACY to see if
42367754Smsmith             * it is supported
42467754Smsmith             */
42567754Smsmith            AcpiHwSetMode (SYS_MODE_LEGACY);
42667754Smsmith
42767754Smsmith            if (AcpiHwGetMode () == SYS_MODE_LEGACY)
42867754Smsmith            {
42967754Smsmith                /* Now in SYS_MODE_LEGACY, so both are supported */
43067754Smsmith
43167754Smsmith                AcpiGbl_SystemFlags |= (SYS_MODE_ACPI | SYS_MODE_LEGACY);
43267754Smsmith                AcpiHwSetMode (SYS_MODE_ACPI);
43367754Smsmith            }
43467754Smsmith
43567754Smsmith            else
43667754Smsmith            {
43767754Smsmith                /* Still in SYS_MODE_ACPI so this must be an ACPI only system */
43867754Smsmith
43967754Smsmith                AcpiGbl_SystemFlags |= SYS_MODE_ACPI;
44067754Smsmith            }
44167754Smsmith        }
44267754Smsmith    }
44367754Smsmith
44467754Smsmith    return_VALUE (AcpiGbl_SystemFlags & SYS_MODES_MASK);
44567754Smsmith}
44667754Smsmith
44767754Smsmith
448