Path: blob/main/sys/contrib/dev/acpica/components/hardware/hwsleep.c
48521 views
/******************************************************************************1*2* Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the3* original/legacy sleep/PM registers.4*5*****************************************************************************/67/******************************************************************************8*9* 1. Copyright Notice10*11* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.12* All rights reserved.13*14* 2. License15*16* 2.1. This is your license from Intel Corp. under its intellectual property17* rights. You may have additional license terms from the party that provided18* you this software, covering your right to use that party's intellectual19* property rights.20*21* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a22* copy of the source code appearing in this file ("Covered Code") an23* irrevocable, perpetual, worldwide license under Intel's copyrights in the24* base code distributed originally by Intel ("Original Intel Code") to copy,25* make derivatives, distribute, use and display any portion of the Covered26* Code in any form, with the right to sublicense such rights; and27*28* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent29* license (with the right to sublicense), under only those claims of Intel30* patents that are infringed by the Original Intel Code, to make, use, sell,31* offer to sell, and import the Covered Code and derivative works thereof32* solely to the minimum extent necessary to exercise the above copyright33* license, and in no event shall the patent license extend to any additions34* to or modifications of the Original Intel Code. No other license or right35* is granted directly or by implication, estoppel or otherwise;36*37* The above copyright and patent license is granted only if the following38* conditions are met:39*40* 3. Conditions41*42* 3.1. Redistribution of Source with Rights to Further Distribute Source.43* Redistribution of source code of any substantial portion of the Covered44* Code or modification with rights to further distribute source must include45* the above Copyright Notice, the above License, this list of Conditions,46* and the following Disclaimer and Export Compliance provision. In addition,47* Licensee must cause all Covered Code to which Licensee contributes to48* contain a file documenting the changes Licensee made to create that Covered49* Code and the date of any change. Licensee must include in that file the50* documentation of any changes made by any predecessor Licensee. Licensee51* must include a prominent statement that the modification is derived,52* directly or indirectly, from Original Intel Code.53*54* 3.2. Redistribution of Source with no Rights to Further Distribute Source.55* Redistribution of source code of any substantial portion of the Covered56* Code or modification without rights to further distribute source must57* include the following Disclaimer and Export Compliance provision in the58* documentation and/or other materials provided with distribution. In59* addition, Licensee may not authorize further sublicense of source of any60* portion of the Covered Code, and must include terms to the effect that the61* license from Licensee to its licensee is limited to the intellectual62* property embodied in the software Licensee provides to its licensee, and63* not to intellectual property embodied in modifications its licensee may64* make.65*66* 3.3. Redistribution of Executable. Redistribution in executable form of any67* substantial portion of the Covered Code or modification must reproduce the68* above Copyright Notice, and the following Disclaimer and Export Compliance69* provision in the documentation and/or other materials provided with the70* distribution.71*72* 3.4. Intel retains all right, title, and interest in and to the Original73* Intel Code.74*75* 3.5. Neither the name Intel nor any other trademark owned or controlled by76* Intel shall be used in advertising or otherwise to promote the sale, use or77* other dealings in products derived from or relating to the Covered Code78* without prior written authorization from Intel.79*80* 4. Disclaimer and Export Compliance81*82* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED83* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE84* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,85* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY86* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY87* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A88* PARTICULAR PURPOSE.89*90* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES91* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR92* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,93* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY94* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL95* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS96* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY97* LIMITED REMEDY.98*99* 4.3. Licensee shall not export, either directly or indirectly, any of this100* software or system incorporating such software without first obtaining any101* required license or other approval from the U. S. Department of Commerce or102* any other agency or department of the United States Government. In the103* event Licensee exports any such software from the United States or104* re-exports any such software from a foreign destination, Licensee shall105* ensure that the distribution and export/re-export of the software is in106* compliance with all laws, regulations, orders, or other restrictions of the107* U.S. Export Administration Regulations. Licensee agrees that neither it nor108* any of its subsidiaries will export/re-export any technical data, process,109* software, or service, directly or indirectly, to any country for which the110* United States government or any agency thereof requires an export license,111* other governmental approval, or letter of assurance, without first obtaining112* such license, approval or letter.113*114*****************************************************************************115*116* Alternatively, you may choose to be licensed under the terms of the117* following license:118*119* Redistribution and use in source and binary forms, with or without120* modification, are permitted provided that the following conditions121* are met:122* 1. Redistributions of source code must retain the above copyright123* notice, this list of conditions, and the following disclaimer,124* without modification.125* 2. Redistributions in binary form must reproduce at minimum a disclaimer126* substantially similar to the "NO WARRANTY" disclaimer below127* ("Disclaimer") and any redistribution must be conditioned upon128* including a substantially similar Disclaimer requirement for further129* binary redistribution.130* 3. Neither the names of the above-listed copyright holders nor the names131* of any contributors may be used to endorse or promote products derived132* from this software without specific prior written permission.133*134* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS135* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT136* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR137* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT138* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,139* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT140* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,141* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY142* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT143* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE144* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.145*146* Alternatively, you may choose to be licensed under the terms of the147* GNU General Public License ("GPL") version 2 as published by the Free148* Software Foundation.149*150*****************************************************************************/151152#include <contrib/dev/acpica/include/acpi.h>153#include <contrib/dev/acpica/include/accommon.h>154155#define _COMPONENT ACPI_HARDWARE156ACPI_MODULE_NAME ("hwsleep")157158159#if (!ACPI_REDUCED_HARDWARE) /* Entire module */160/*******************************************************************************161*162* FUNCTION: AcpiHwLegacySleep163*164* PARAMETERS: SleepState - Which sleep state to enter165*166* RETURN: Status167*168* DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers169* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED170*171******************************************************************************/172173ACPI_STATUS174AcpiHwLegacySleep (175UINT8 SleepState)176{177ACPI_BIT_REGISTER_INFO *SleepTypeRegInfo;178ACPI_BIT_REGISTER_INFO *SleepEnableRegInfo;179UINT32 Pm1aControl;180UINT32 Pm1bControl;181UINT32 InValue;182ACPI_STATUS Status;183184185ACPI_FUNCTION_TRACE (HwLegacySleep);186187188SleepTypeRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);189SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);190191/* Clear wake status */192193Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS,194ACPI_CLEAR_STATUS);195if (ACPI_FAILURE (Status))196{197return_ACPI_STATUS (Status);198}199200/* Disable all GPEs */201202Status = AcpiHwDisableAllGpes ();203if (ACPI_FAILURE (Status))204{205return_ACPI_STATUS (Status);206}207Status = AcpiHwClearAcpiStatus();208if (ACPI_FAILURE(Status))209{210return_ACPI_STATUS(Status);211}212AcpiGbl_SystemAwakeAndRunning = FALSE;213214/* Enable all wakeup GPEs */215216Status = AcpiHwEnableAllWakeupGpes ();217if (ACPI_FAILURE (Status))218{219return_ACPI_STATUS (Status);220}221222/* Get current value of PM1A control */223224Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,225&Pm1aControl);226if (ACPI_FAILURE (Status))227{228return_ACPI_STATUS (Status);229}230ACPI_DEBUG_PRINT ((ACPI_DB_INIT,231"Entering sleep state [S%u]\n", SleepState));232233/* Clear the SLP_EN and SLP_TYP fields */234235Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |236SleepEnableRegInfo->AccessBitMask);237Pm1bControl = Pm1aControl;238239/* Insert the SLP_TYP bits */240241Pm1aControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);242Pm1bControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);243244/*245* We split the writes of SLP_TYP and SLP_EN to workaround246* poorly implemented hardware.247*/248249/* Write #1: write the SLP_TYP data to the PM1 Control registers */250251Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);252if (ACPI_FAILURE (Status))253{254return_ACPI_STATUS (Status);255}256257/* Insert the sleep enable (SLP_EN) bit */258259Pm1aControl |= SleepEnableRegInfo->AccessBitMask;260Pm1bControl |= SleepEnableRegInfo->AccessBitMask;261262/* Flush caches, as per ACPI specification */263264if (SleepState < ACPI_STATE_S4)265{266ACPI_FLUSH_CPU_CACHE ();267}268269Status = AcpiOsEnterSleep (SleepState, Pm1aControl, Pm1bControl);270if (Status == AE_CTRL_TERMINATE)271{272return_ACPI_STATUS (AE_OK);273}274if (ACPI_FAILURE (Status))275{276return_ACPI_STATUS (Status);277}278279/* Write #2: Write both SLP_TYP + SLP_EN */280281Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);282if (ACPI_FAILURE (Status))283{284return_ACPI_STATUS (Status);285}286287if (SleepState > ACPI_STATE_S3)288{289/*290* We wanted to sleep > S3, but it didn't happen (by virtue of the291* fact that we are still executing!)292*293* Wait ten seconds, then try again. This is to get S4/S5 to work on294* all machines.295*296* We wait so long to allow chipsets that poll this reg very slowly297* to still read the right value. Ideally, this block would go298* away entirely.299*/300AcpiOsStall (10 * ACPI_USEC_PER_SEC);301302Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_CONTROL,303SleepEnableRegInfo->AccessBitMask);304if (ACPI_FAILURE (Status))305{306return_ACPI_STATUS (Status);307}308}309310/* Wait for transition back to Working State */311312do313{314Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);315if (ACPI_FAILURE (Status))316{317return_ACPI_STATUS (Status);318}319320} while (!InValue);321322return_ACPI_STATUS (AE_OK);323}324325326/*******************************************************************************327*328* FUNCTION: AcpiHwLegacyWakePrep329*330* PARAMETERS: SleepState - Which sleep state we just exited331*332* RETURN: Status333*334* DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a335* sleep.336* Called with interrupts ENABLED.337*338******************************************************************************/339340ACPI_STATUS341AcpiHwLegacyWakePrep (342UINT8 SleepState)343{344ACPI_STATUS Status = AE_OK;345ACPI_BIT_REGISTER_INFO *SleepTypeRegInfo;346ACPI_BIT_REGISTER_INFO *SleepEnableRegInfo;347UINT32 Pm1aControl;348UINT32 Pm1bControl;349350351ACPI_FUNCTION_TRACE (HwLegacyWakePrep);352353/*354* Set SLP_TYPE and SLP_EN to state S0.355* This is unclear from the ACPI Spec, but it is required356* by some machines.357*/358if (AcpiGbl_SleepTypeAS0 != ACPI_SLEEP_TYPE_INVALID)359{360SleepTypeRegInfo =361AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);362SleepEnableRegInfo =363AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);364365/* Get current value of PM1A control */366367Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,368&Pm1aControl);369if (ACPI_SUCCESS (Status))370{371/* Clear the SLP_EN and SLP_TYP fields */372373Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |374SleepEnableRegInfo->AccessBitMask);375Pm1bControl = Pm1aControl;376377/* Insert the SLP_TYP bits */378379Pm1aControl |= (AcpiGbl_SleepTypeAS0 <<380SleepTypeRegInfo->BitPosition);381Pm1bControl |= (AcpiGbl_SleepTypeBS0 <<382SleepTypeRegInfo->BitPosition);383384/* Write the control registers and ignore any errors */385386(void) AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);387}388}389390return_ACPI_STATUS (Status);391}392393394/*******************************************************************************395*396* FUNCTION: AcpiHwLegacyWake397*398* PARAMETERS: SleepState - Which sleep state we just exited399*400* RETURN: Status401*402* DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep403* Called with interrupts ENABLED.404*405******************************************************************************/406407ACPI_STATUS408AcpiHwLegacyWake (409UINT8 SleepState)410{411ACPI_STATUS Status;412413414ACPI_FUNCTION_TRACE (HwLegacyWake);415416417/* Ensure EnterSleepStatePrep -> EnterSleepState ordering */418419AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;420AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, ACPI_SST_WAKING);421422/*423* GPEs must be enabled before _WAK is called as GPEs424* might get fired there425*426* Restore the GPEs:427* 1) Disable all GPEs428* 2) Enable all runtime GPEs429*/430Status = AcpiHwDisableAllGpes ();431if (ACPI_FAILURE (Status))432{433return_ACPI_STATUS (Status);434}435436Status = AcpiHwEnableAllRuntimeGpes ();437if (ACPI_FAILURE (Status))438{439return_ACPI_STATUS (Status);440}441442/*443* Now we can execute _WAK, etc. Some machines require that the GPEs444* are enabled before the wake methods are executed.445*/446AcpiHwExecuteSleepMethod (METHOD_PATHNAME__WAK, SleepState);447448/*449* Some BIOS code assumes that WAK_STS will be cleared on resume450* and use it to determine whether the system is rebooting or451* resuming. Clear WAK_STS for compatibility.452*/453(void) AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS,454ACPI_CLEAR_STATUS);455AcpiGbl_SystemAwakeAndRunning = TRUE;456457/* Enable power button */458459(void) AcpiWriteBitRegister(460AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId,461ACPI_ENABLE_EVENT);462463(void) AcpiWriteBitRegister(464AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId,465ACPI_CLEAR_STATUS);466467/* Enable sleep button */468469(void) AcpiWriteBitRegister (470AcpiGbl_FixedEventInfo[ACPI_EVENT_SLEEP_BUTTON].EnableRegisterId,471ACPI_ENABLE_EVENT);472473(void) AcpiWriteBitRegister (474AcpiGbl_FixedEventInfo[ACPI_EVENT_SLEEP_BUTTON].StatusRegisterId,475ACPI_CLEAR_STATUS);476477AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, ACPI_SST_WORKING);478return_ACPI_STATUS (Status);479}480481#endif /* !ACPI_REDUCED_HARDWARE */482483484