167754Smsmith/******************************************************************************
267754Smsmith *
371867Smsmith * Module Name: hwacpi - ACPI Hardware Initialization/Mode Interface
467754Smsmith *
567754Smsmith *****************************************************************************/
667754Smsmith
7217365Sjkim/*
8306536Sjkim * Copyright (C) 2000 - 2016, 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
44193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
45193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
4667754Smsmith
4767754Smsmith
4877424Smsmith#define _COMPONENT          ACPI_HARDWARE
4991116Smsmith        ACPI_MODULE_NAME    ("hwacpi")
5067754Smsmith
5167754Smsmith
52231844Sjkim#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
5367754Smsmith/******************************************************************************
5467754Smsmith *
5567754Smsmith * FUNCTION:    AcpiHwSetMode
5667754Smsmith *
5767754Smsmith * PARAMETERS:  Mode            - SYS_MODE_ACPI or SYS_MODE_LEGACY
5867754Smsmith *
5967754Smsmith * RETURN:      Status
6067754Smsmith *
6191116Smsmith * DESCRIPTION: Transitions the system into the requested mode.
6267754Smsmith *
6367754Smsmith ******************************************************************************/
6467754Smsmith
6567754SmsmithACPI_STATUS
6667754SmsmithAcpiHwSetMode (
6767754Smsmith    UINT32                  Mode)
6867754Smsmith{
6967754Smsmith
7099679Siwasaki    ACPI_STATUS             Status;
7199679Siwasaki    UINT32                  Retry;
7267754Smsmith
7383174Smsmith
74167802Sjkim    ACPI_FUNCTION_TRACE (HwSetMode);
7567754Smsmith
76246849Sjkim
77246849Sjkim    /* If the Hardware Reduced flag is set, machine is always in acpi mode */
78246849Sjkim
79246849Sjkim    if (AcpiGbl_ReducedHardware)
80246849Sjkim    {
81246849Sjkim        return_ACPI_STATUS (AE_OK);
82246849Sjkim    }
83246849Sjkim
84102550Siwasaki    /*
85102550Siwasaki     * ACPI 2.0 clarified that if SMI_CMD in FADT is zero,
86102550Siwasaki     * system does not support mode transition.
87102550Siwasaki     */
88167802Sjkim    if (!AcpiGbl_FADT.SmiCommand)
89102550Siwasaki    {
90167802Sjkim        ACPI_ERROR ((AE_INFO, "No SMI_CMD in FADT, mode transition failed"));
91102550Siwasaki        return_ACPI_STATUS (AE_NO_HARDWARE_RESPONSE);
92102550Siwasaki    }
93102550Siwasaki
94102550Siwasaki    /*
95102550Siwasaki     * ACPI 2.0 clarified the meaning of ACPI_ENABLE and ACPI_DISABLE
96102550Siwasaki     * in FADT: If it is zero, enabling or disabling is not supported.
97102550Siwasaki     * As old systems may have used zero for mode transition,
98102550Siwasaki     * we make sure both the numbers are zero to determine these
99102550Siwasaki     * transitions are not supported.
100102550Siwasaki     */
101167802Sjkim    if (!AcpiGbl_FADT.AcpiEnable && !AcpiGbl_FADT.AcpiDisable)
102102550Siwasaki    {
103167802Sjkim        ACPI_ERROR ((AE_INFO,
104193267Sjkim            "No ACPI mode transition supported in this system "
105193267Sjkim            "(enable/disable both zero)"));
106102550Siwasaki        return_ACPI_STATUS (AE_OK);
107102550Siwasaki    }
108102550Siwasaki
10999679Siwasaki    switch (Mode)
11099679Siwasaki    {
11199679Siwasaki    case ACPI_SYS_MODE_ACPI:
11267754Smsmith
11367754Smsmith        /* BIOS should have disabled ALL fixed and GP events */
11467754Smsmith
115193267Sjkim        Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
116306536Sjkim            (UINT32) AcpiGbl_FADT.AcpiEnable, 8);
11782367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Attempting to enable ACPI mode\n"));
11899679Siwasaki        break;
11999679Siwasaki
12099679Siwasaki    case ACPI_SYS_MODE_LEGACY:
12167754Smsmith        /*
12267754Smsmith         * BIOS should clear all fixed status bits and restore fixed event
12367754Smsmith         * enable bits to default
12467754Smsmith         */
125193267Sjkim        Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
126306536Sjkim            (UINT32) AcpiGbl_FADT.AcpiDisable, 8);
12782367Smsmith        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
128306536Sjkim            "Attempting to enable Legacy (non-ACPI) mode\n"));
12999679Siwasaki        break;
13099679Siwasaki
13199679Siwasaki    default:
132250838Sjkim
13399679Siwasaki        return_ACPI_STATUS (AE_BAD_PARAMETER);
13467754Smsmith    }
13567754Smsmith
13699679Siwasaki    if (ACPI_FAILURE (Status))
13799679Siwasaki    {
138167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status,
139167802Sjkim            "Could not write ACPI mode change"));
14099679Siwasaki        return_ACPI_STATUS (Status);
14199679Siwasaki    }
14283174Smsmith
14399679Siwasaki    /*
14499679Siwasaki     * Some hardware takes a LONG time to switch modes. Give them 3 sec to
14599679Siwasaki     * do so, but allow faster systems to proceed more quickly.
14699679Siwasaki     */
14799679Siwasaki    Retry = 3000;
14899679Siwasaki    while (Retry)
14999679Siwasaki    {
150245582Sjkim        if (AcpiHwGetMode () == Mode)
151102550Siwasaki        {
152306536Sjkim            ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
153306536Sjkim                "Mode %X successfully enabled\n", Mode));
154123315Snjl            return_ACPI_STATUS (AE_OK);
15599679Siwasaki        }
156245582Sjkim        AcpiOsStall (ACPI_USEC_PER_MSEC);
15799679Siwasaki        Retry--;
15867754Smsmith    }
15967754Smsmith
160167802Sjkim    ACPI_ERROR ((AE_INFO, "Hardware did not change modes"));
161123315Snjl    return_ACPI_STATUS (AE_NO_HARDWARE_RESPONSE);
16267754Smsmith}
16367754Smsmith
16467754Smsmith
165151937Sjkim/*******************************************************************************
16667754Smsmith *
16767754Smsmith * FUNCTION:    AcpiHwGetMode
16867754Smsmith *
16967754Smsmith * PARAMETERS:  none
17067754Smsmith *
17167754Smsmith * RETURN:      SYS_MODE_ACPI or SYS_MODE_LEGACY
17267754Smsmith *
173241973Sjkim * DESCRIPTION: Return current operating state of system. Determined by
17467754Smsmith *              querying the SCI_EN bit.
17567754Smsmith *
17667754Smsmith ******************************************************************************/
17767754Smsmith
17867754SmsmithUINT32
179151937SjkimAcpiHwGetMode (
180151937Sjkim    void)
18167754Smsmith{
18299679Siwasaki    ACPI_STATUS             Status;
18399679Siwasaki    UINT32                  Value;
18467754Smsmith
18599679Siwasaki
186167802Sjkim    ACPI_FUNCTION_TRACE (HwGetMode);
18767754Smsmith
188123315Snjl
189246849Sjkim    /* If the Hardware Reduced flag is set, machine is always in acpi mode */
190246849Sjkim
191246849Sjkim    if (AcpiGbl_ReducedHardware)
192246849Sjkim    {
193246849Sjkim        return_UINT32 (ACPI_SYS_MODE_ACPI);
194246849Sjkim    }
195246849Sjkim
196123315Snjl    /*
197123315Snjl     * ACPI 2.0 clarified that if SMI_CMD in FADT is zero,
198123315Snjl     * system does not support mode transition.
199123315Snjl     */
200167802Sjkim    if (!AcpiGbl_FADT.SmiCommand)
201123315Snjl    {
202246849Sjkim        return_UINT32 (ACPI_SYS_MODE_ACPI);
203123315Snjl    }
204123315Snjl
205193267Sjkim    Status = AcpiReadBitRegister (ACPI_BITREG_SCI_ENABLE, &Value);
20699679Siwasaki    if (ACPI_FAILURE (Status))
20799679Siwasaki    {
208246849Sjkim        return_UINT32 (ACPI_SYS_MODE_LEGACY);
20999679Siwasaki    }
21067754Smsmith
21199679Siwasaki    if (Value)
21267754Smsmith    {
213246849Sjkim        return_UINT32 (ACPI_SYS_MODE_ACPI);
21467754Smsmith    }
21567754Smsmith    else
21667754Smsmith    {
217246849Sjkim        return_UINT32 (ACPI_SYS_MODE_LEGACY);
21867754Smsmith    }
21967754Smsmith}
220231844Sjkim
221231844Sjkim#endif /* !ACPI_REDUCED_HARDWARE */
222