Path: blob/main/sys/contrib/dev/acpica/components/hardware/hwesleep.c
48521 views
/******************************************************************************1*2* Name: hwesleep.c - ACPI Hardware Sleep/Wake Support functions for the3* extended FADT-V5 sleep 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 ("hwesleep")157158159/*******************************************************************************160*161* FUNCTION: AcpiHwExecuteSleepMethod162*163* PARAMETERS: MethodPathname - Pathname of method to execute164* IntegerArgument - Argument to pass to the method165*166* RETURN: None167*168* DESCRIPTION: Execute a sleep/wake related method with one integer argument169* and no return value.170*171******************************************************************************/172173void174AcpiHwExecuteSleepMethod (175char *MethodPathname,176UINT32 IntegerArgument)177{178ACPI_OBJECT_LIST ArgList;179ACPI_OBJECT Arg;180ACPI_STATUS Status;181182183ACPI_FUNCTION_TRACE (HwExecuteSleepMethod);184185186/* One argument, IntegerArgument; No return value expected */187188ArgList.Count = 1;189ArgList.Pointer = &Arg;190Arg.Type = ACPI_TYPE_INTEGER;191Arg.Integer.Value = (UINT64) IntegerArgument;192193Status = AcpiEvaluateObject (NULL, MethodPathname, &ArgList, NULL);194if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)195{196ACPI_EXCEPTION ((AE_INFO, Status, "While executing method %s",197MethodPathname));198}199200return_VOID;201}202203204/*******************************************************************************205*206* FUNCTION: AcpiHwExtendedSleep207*208* PARAMETERS: SleepState - Which sleep state to enter209*210* RETURN: Status211*212* DESCRIPTION: Enter a system sleep state via the extended FADT sleep213* registers (V5 FADT).214* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED215*216******************************************************************************/217218ACPI_STATUS219AcpiHwExtendedSleep (220UINT8 SleepState)221{222ACPI_STATUS Status;223UINT8 SleepControl;224UINT64 SleepStatus;225226227ACPI_FUNCTION_TRACE (HwExtendedSleep);228229230/* Extended sleep registers must be valid */231232if (!AcpiGbl_FADT.SleepControl.Address ||233!AcpiGbl_FADT.SleepStatus.Address)234{235return_ACPI_STATUS (AE_NOT_EXIST);236}237238/* Clear wake status (WAK_STS) */239240Status = AcpiWrite ((UINT64) ACPI_X_WAKE_STATUS,241&AcpiGbl_FADT.SleepStatus);242if (ACPI_FAILURE (Status))243{244return_ACPI_STATUS (Status);245}246247AcpiGbl_SystemAwakeAndRunning = FALSE;248249/*250* Set the SLP_TYP and SLP_EN bits.251*252* Note: We only use the first value returned by the \_Sx method253* (AcpiGbl_SleepTypeA) - As per ACPI specification.254*/255ACPI_DEBUG_PRINT ((ACPI_DB_INIT,256"Entering sleep state [S%u]\n", SleepState));257258SleepControl = ((AcpiGbl_SleepTypeA << ACPI_X_SLEEP_TYPE_POSITION) &259ACPI_X_SLEEP_TYPE_MASK) | ACPI_X_SLEEP_ENABLE;260261/* Flush caches, as per ACPI specification */262263if (SleepState < ACPI_STATE_S4)264{265ACPI_FLUSH_CPU_CACHE ();266}267268Status = AcpiOsEnterSleep (SleepState, SleepControl, 0);269if (Status == AE_CTRL_TERMINATE)270{271return_ACPI_STATUS (AE_OK);272}273if (ACPI_FAILURE (Status))274{275return_ACPI_STATUS (Status);276}277278Status = AcpiWrite ((UINT64) SleepControl, &AcpiGbl_FADT.SleepControl);279if (ACPI_FAILURE (Status))280{281return_ACPI_STATUS (Status);282}283284/* Wait for transition back to Working State */285286do287{288Status = AcpiRead (&SleepStatus, &AcpiGbl_FADT.SleepStatus);289if (ACPI_FAILURE (Status))290{291return_ACPI_STATUS (Status);292}293294} while (!(((UINT8) SleepStatus) & ACPI_X_WAKE_STATUS));295296return_ACPI_STATUS (AE_OK);297}298299300/*******************************************************************************301*302* FUNCTION: AcpiHwExtendedWakePrep303*304* PARAMETERS: SleepState - Which sleep state we just exited305*306* RETURN: Status307*308* DESCRIPTION: Perform first part of OS-independent ACPI cleanup after309* a sleep. Called with interrupts ENABLED.310*311******************************************************************************/312313ACPI_STATUS314AcpiHwExtendedWakePrep (315UINT8 SleepState)316{317UINT8 SleepTypeValue;318319320ACPI_FUNCTION_TRACE (HwExtendedWakePrep);321322323if (AcpiGbl_SleepTypeAS0 != ACPI_SLEEP_TYPE_INVALID)324{325SleepTypeValue = ((AcpiGbl_SleepTypeAS0 << ACPI_X_SLEEP_TYPE_POSITION) &326ACPI_X_SLEEP_TYPE_MASK);327328(void) AcpiWrite ((UINT64) (SleepTypeValue | ACPI_X_SLEEP_ENABLE),329&AcpiGbl_FADT.SleepControl);330}331332return_ACPI_STATUS (AE_OK);333}334335336/*******************************************************************************337*338* FUNCTION: AcpiHwExtendedWake339*340* PARAMETERS: SleepState - Which sleep state we just exited341*342* RETURN: Status343*344* DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep345* Called with interrupts ENABLED.346*347******************************************************************************/348349ACPI_STATUS350AcpiHwExtendedWake (351UINT8 SleepState)352{353ACPI_FUNCTION_TRACE (HwExtendedWake);354355356/* Ensure EnterSleepStatePrep -> EnterSleepState ordering */357358AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;359360/* Execute the wake methods */361362AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, ACPI_SST_WAKING);363AcpiHwExecuteSleepMethod (METHOD_PATHNAME__WAK, SleepState);364365/*366* Some BIOS code assumes that WAK_STS will be cleared on resume367* and use it to determine whether the system is rebooting or368* resuming. Clear WAK_STS for compatibility.369*/370(void) AcpiWrite ((UINT64) ACPI_X_WAKE_STATUS, &AcpiGbl_FADT.SleepStatus);371AcpiGbl_SystemAwakeAndRunning = TRUE;372373AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, ACPI_SST_WORKING);374return_ACPI_STATUS (AE_OK);375}376377378