hwacpi.c revision 245582
167754Smsmith/******************************************************************************
267754Smsmith *
371867Smsmith * Module Name: hwacpi - ACPI Hardware Initialization/Mode Interface
467754Smsmith *
567754Smsmith *****************************************************************************/
667754Smsmith
7217365Sjkim/*
8245582Sjkim * Copyright (C) 2000 - 2013, Intel Corp.
970243Smsmith * All rights reserved.
1067754Smsmith *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
2567754Smsmith *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
2967754Smsmith *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
4367754Smsmith
4467754Smsmith#define __HWACPI_C__
4567754Smsmith
46193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
47193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
4867754Smsmith
4967754Smsmith
5077424Smsmith#define _COMPONENT          ACPI_HARDWARE
5191116Smsmith        ACPI_MODULE_NAME    ("hwacpi")
5267754Smsmith
5367754Smsmith
54231844Sjkim#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
5567754Smsmith/******************************************************************************
5667754Smsmith *
5767754Smsmith * FUNCTION:    AcpiHwSetMode
5867754Smsmith *
5967754Smsmith * PARAMETERS:  Mode            - SYS_MODE_ACPI or SYS_MODE_LEGACY
6067754Smsmith *
6167754Smsmith * RETURN:      Status
6267754Smsmith *
6391116Smsmith * DESCRIPTION: Transitions the system into the requested mode.
6467754Smsmith *
6567754Smsmith ******************************************************************************/
6667754Smsmith
6767754SmsmithACPI_STATUS
6867754SmsmithAcpiHwSetMode (
6967754Smsmith    UINT32                  Mode)
7067754Smsmith{
7167754Smsmith
7299679Siwasaki    ACPI_STATUS             Status;
7399679Siwasaki    UINT32                  Retry;
7467754Smsmith
7583174Smsmith
76167802Sjkim    ACPI_FUNCTION_TRACE (HwSetMode);
7767754Smsmith
78102550Siwasaki    /*
79102550Siwasaki     * ACPI 2.0 clarified that if SMI_CMD in FADT is zero,
80102550Siwasaki     * system does not support mode transition.
81102550Siwasaki     */
82167802Sjkim    if (!AcpiGbl_FADT.SmiCommand)
83102550Siwasaki    {
84167802Sjkim        ACPI_ERROR ((AE_INFO, "No SMI_CMD in FADT, mode transition failed"));
85102550Siwasaki        return_ACPI_STATUS (AE_NO_HARDWARE_RESPONSE);
86102550Siwasaki    }
87102550Siwasaki
88102550Siwasaki    /*
89102550Siwasaki     * ACPI 2.0 clarified the meaning of ACPI_ENABLE and ACPI_DISABLE
90102550Siwasaki     * in FADT: If it is zero, enabling or disabling is not supported.
91102550Siwasaki     * As old systems may have used zero for mode transition,
92102550Siwasaki     * we make sure both the numbers are zero to determine these
93102550Siwasaki     * transitions are not supported.
94102550Siwasaki     */
95167802Sjkim    if (!AcpiGbl_FADT.AcpiEnable && !AcpiGbl_FADT.AcpiDisable)
96102550Siwasaki    {
97167802Sjkim        ACPI_ERROR ((AE_INFO,
98193267Sjkim            "No ACPI mode transition supported in this system "
99193267Sjkim            "(enable/disable both zero)"));
100102550Siwasaki        return_ACPI_STATUS (AE_OK);
101102550Siwasaki    }
102102550Siwasaki
10399679Siwasaki    switch (Mode)
10499679Siwasaki    {
10599679Siwasaki    case ACPI_SYS_MODE_ACPI:
10667754Smsmith
10767754Smsmith        /* BIOS should have disabled ALL fixed and GP events */
10867754Smsmith
109193267Sjkim        Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
110167802Sjkim                        (UINT32) AcpiGbl_FADT.AcpiEnable, 8);
11182367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Attempting to enable ACPI mode\n"));
11299679Siwasaki        break;
11399679Siwasaki
11499679Siwasaki    case ACPI_SYS_MODE_LEGACY:
11599679Siwasaki
11667754Smsmith        /*
11767754Smsmith         * BIOS should clear all fixed status bits and restore fixed event
11867754Smsmith         * enable bits to default
11967754Smsmith         */
120193267Sjkim        Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
121167802Sjkim                    (UINT32) AcpiGbl_FADT.AcpiDisable, 8);
12282367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
12382367Smsmith                    "Attempting to enable Legacy (non-ACPI) mode\n"));
12499679Siwasaki        break;
12599679Siwasaki
12699679Siwasaki    default:
12799679Siwasaki        return_ACPI_STATUS (AE_BAD_PARAMETER);
12867754Smsmith    }
12967754Smsmith
13099679Siwasaki    if (ACPI_FAILURE (Status))
13199679Siwasaki    {
132167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status,
133167802Sjkim            "Could not write ACPI mode change"));
13499679Siwasaki        return_ACPI_STATUS (Status);
13599679Siwasaki    }
13683174Smsmith
13799679Siwasaki    /*
13899679Siwasaki     * Some hardware takes a LONG time to switch modes. Give them 3 sec to
13999679Siwasaki     * do so, but allow faster systems to proceed more quickly.
14099679Siwasaki     */
14199679Siwasaki    Retry = 3000;
14299679Siwasaki    while (Retry)
14399679Siwasaki    {
144245582Sjkim        if (AcpiHwGetMode () == Mode)
145102550Siwasaki        {
146151937Sjkim            ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Mode %X successfully enabled\n",
147151937Sjkim                Mode));
148123315Snjl            return_ACPI_STATUS (AE_OK);
14999679Siwasaki        }
150245582Sjkim        AcpiOsStall (ACPI_USEC_PER_MSEC);
15199679Siwasaki        Retry--;
15267754Smsmith    }
15367754Smsmith
154167802Sjkim    ACPI_ERROR ((AE_INFO, "Hardware did not change modes"));
155123315Snjl    return_ACPI_STATUS (AE_NO_HARDWARE_RESPONSE);
15667754Smsmith}
15767754Smsmith
15867754Smsmith
159151937Sjkim/*******************************************************************************
16067754Smsmith *
16167754Smsmith * FUNCTION:    AcpiHwGetMode
16267754Smsmith *
16367754Smsmith * PARAMETERS:  none
16467754Smsmith *
16567754Smsmith * RETURN:      SYS_MODE_ACPI or SYS_MODE_LEGACY
16667754Smsmith *
167241973Sjkim * DESCRIPTION: Return current operating state of system. Determined by
16867754Smsmith *              querying the SCI_EN bit.
16967754Smsmith *
17067754Smsmith ******************************************************************************/
17167754Smsmith
17267754SmsmithUINT32
173151937SjkimAcpiHwGetMode (
174151937Sjkim    void)
17567754Smsmith{
17699679Siwasaki    ACPI_STATUS             Status;
17799679Siwasaki    UINT32                  Value;
17867754Smsmith
17999679Siwasaki
180167802Sjkim    ACPI_FUNCTION_TRACE (HwGetMode);
18167754Smsmith
182123315Snjl
183123315Snjl    /*
184123315Snjl     * ACPI 2.0 clarified that if SMI_CMD in FADT is zero,
185123315Snjl     * system does not support mode transition.
186123315Snjl     */
187167802Sjkim    if (!AcpiGbl_FADT.SmiCommand)
188123315Snjl    {
189245582Sjkim        return_VALUE (ACPI_SYS_MODE_ACPI);
190123315Snjl    }
191123315Snjl
192193267Sjkim    Status = AcpiReadBitRegister (ACPI_BITREG_SCI_ENABLE, &Value);
19399679Siwasaki    if (ACPI_FAILURE (Status))
19499679Siwasaki    {
195245582Sjkim        return_VALUE (ACPI_SYS_MODE_LEGACY);
19699679Siwasaki    }
19767754Smsmith
19899679Siwasaki    if (Value)
19967754Smsmith    {
200245582Sjkim        return_VALUE (ACPI_SYS_MODE_ACPI);
20167754Smsmith    }
20267754Smsmith    else
20367754Smsmith    {
204245582Sjkim        return_VALUE (ACPI_SYS_MODE_LEGACY);
20567754Smsmith    }
20667754Smsmith}
207231844Sjkim
208231844Sjkim#endif /* !ACPI_REDUCED_HARDWARE */
209