hwesleep.c revision 1.13 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Name: hwesleep.c - ACPI Hardware Sleep/Wake Support functions for the
4 1.1 christos * extended FADT-V5 sleep registers.
5 1.1 christos *
6 1.1 christos *****************************************************************************/
7 1.1 christos
8 1.1 christos /*
9 1.13 christos * Copyright (C) 2000 - 2023, Intel Corp.
10 1.1 christos * All rights reserved.
11 1.1 christos *
12 1.1 christos * Redistribution and use in source and binary forms, with or without
13 1.1 christos * modification, are permitted provided that the following conditions
14 1.1 christos * are met:
15 1.1 christos * 1. Redistributions of source code must retain the above copyright
16 1.1 christos * notice, this list of conditions, and the following disclaimer,
17 1.1 christos * without modification.
18 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
20 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
21 1.1 christos * including a substantially similar Disclaimer requirement for further
22 1.1 christos * binary redistribution.
23 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
24 1.1 christos * of any contributors may be used to endorse or promote products derived
25 1.1 christos * from this software without specific prior written permission.
26 1.1 christos *
27 1.1 christos * Alternatively, this software may be distributed under the terms of the
28 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
29 1.1 christos * Software Foundation.
30 1.1 christos *
31 1.1 christos * NO WARRANTY
32 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 1.10 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
43 1.1 christos */
44 1.1 christos
45 1.1 christos #include "acpi.h"
46 1.1 christos #include "accommon.h"
47 1.1 christos
48 1.1 christos #define _COMPONENT ACPI_HARDWARE
49 1.1 christos ACPI_MODULE_NAME ("hwesleep")
50 1.1 christos
51 1.1 christos
52 1.1 christos /*******************************************************************************
53 1.1 christos *
54 1.1 christos * FUNCTION: AcpiHwExecuteSleepMethod
55 1.1 christos *
56 1.1 christos * PARAMETERS: MethodPathname - Pathname of method to execute
57 1.1 christos * IntegerArgument - Argument to pass to the method
58 1.1 christos *
59 1.1 christos * RETURN: None
60 1.1 christos *
61 1.1 christos * DESCRIPTION: Execute a sleep/wake related method with one integer argument
62 1.1 christos * and no return value.
63 1.1 christos *
64 1.1 christos ******************************************************************************/
65 1.1 christos
66 1.1 christos void
67 1.1 christos AcpiHwExecuteSleepMethod (
68 1.13 christos const char *MethodPathname,
69 1.1 christos UINT32 IntegerArgument)
70 1.1 christos {
71 1.1 christos ACPI_OBJECT_LIST ArgList;
72 1.1 christos ACPI_OBJECT Arg;
73 1.1 christos ACPI_STATUS Status;
74 1.1 christos
75 1.1 christos
76 1.1 christos ACPI_FUNCTION_TRACE (HwExecuteSleepMethod);
77 1.1 christos
78 1.1 christos
79 1.1 christos /* One argument, IntegerArgument; No return value expected */
80 1.1 christos
81 1.1 christos ArgList.Count = 1;
82 1.1 christos ArgList.Pointer = &Arg;
83 1.1 christos Arg.Type = ACPI_TYPE_INTEGER;
84 1.1 christos Arg.Integer.Value = (UINT64) IntegerArgument;
85 1.1 christos
86 1.1 christos Status = AcpiEvaluateObject (NULL, MethodPathname, &ArgList, NULL);
87 1.1 christos if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
88 1.1 christos {
89 1.1 christos ACPI_EXCEPTION ((AE_INFO, Status, "While executing method %s",
90 1.1 christos MethodPathname));
91 1.1 christos }
92 1.1 christos
93 1.1 christos return_VOID;
94 1.1 christos }
95 1.1 christos
96 1.1 christos
97 1.1 christos /*******************************************************************************
98 1.1 christos *
99 1.1 christos * FUNCTION: AcpiHwExtendedSleep
100 1.1 christos *
101 1.1 christos * PARAMETERS: SleepState - Which sleep state to enter
102 1.1 christos *
103 1.1 christos * RETURN: Status
104 1.1 christos *
105 1.1 christos * DESCRIPTION: Enter a system sleep state via the extended FADT sleep
106 1.1 christos * registers (V5 FADT).
107 1.1 christos * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
108 1.1 christos *
109 1.1 christos ******************************************************************************/
110 1.1 christos
111 1.1 christos ACPI_STATUS
112 1.1 christos AcpiHwExtendedSleep (
113 1.1 christos UINT8 SleepState)
114 1.1 christos {
115 1.1 christos ACPI_STATUS Status;
116 1.6 christos UINT8 SleepControl;
117 1.1 christos UINT64 SleepStatus;
118 1.1 christos
119 1.1 christos
120 1.1 christos ACPI_FUNCTION_TRACE (HwExtendedSleep);
121 1.1 christos
122 1.1 christos
123 1.1 christos /* Extended sleep registers must be valid */
124 1.1 christos
125 1.1 christos if (!AcpiGbl_FADT.SleepControl.Address ||
126 1.1 christos !AcpiGbl_FADT.SleepStatus.Address)
127 1.1 christos {
128 1.1 christos return_ACPI_STATUS (AE_NOT_EXIST);
129 1.1 christos }
130 1.1 christos
131 1.1 christos /* Clear wake status (WAK_STS) */
132 1.1 christos
133 1.5 christos Status = AcpiWrite ((UINT64) ACPI_X_WAKE_STATUS,
134 1.5 christos &AcpiGbl_FADT.SleepStatus);
135 1.1 christos if (ACPI_FAILURE (Status))
136 1.1 christos {
137 1.1 christos return_ACPI_STATUS (Status);
138 1.1 christos }
139 1.1 christos
140 1.1 christos AcpiGbl_SystemAwakeAndRunning = FALSE;
141 1.1 christos
142 1.1 christos /*
143 1.1 christos * Set the SLP_TYP and SLP_EN bits.
144 1.1 christos *
145 1.1 christos * Note: We only use the first value returned by the \_Sx method
146 1.1 christos * (AcpiGbl_SleepTypeA) - As per ACPI specification.
147 1.1 christos */
148 1.1 christos ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
149 1.1 christos "Entering sleep state [S%u]\n", SleepState));
150 1.1 christos
151 1.6 christos SleepControl = ((AcpiGbl_SleepTypeA << ACPI_X_SLEEP_TYPE_POSITION) &
152 1.6 christos ACPI_X_SLEEP_TYPE_MASK) | ACPI_X_SLEEP_ENABLE;
153 1.6 christos
154 1.6 christos /* Flush caches, as per ACPI specification */
155 1.6 christos
156 1.6 christos ACPI_FLUSH_CPU_CACHE ();
157 1.6 christos
158 1.6 christos Status = AcpiOsEnterSleep (SleepState, SleepControl, 0);
159 1.6 christos if (Status == AE_CTRL_TERMINATE)
160 1.6 christos {
161 1.6 christos return_ACPI_STATUS (AE_OK);
162 1.6 christos }
163 1.6 christos if (ACPI_FAILURE (Status))
164 1.6 christos {
165 1.6 christos return_ACPI_STATUS (Status);
166 1.6 christos }
167 1.1 christos
168 1.6 christos Status = AcpiWrite ((UINT64) SleepControl, &AcpiGbl_FADT.SleepControl);
169 1.1 christos if (ACPI_FAILURE (Status))
170 1.1 christos {
171 1.1 christos return_ACPI_STATUS (Status);
172 1.1 christos }
173 1.1 christos
174 1.1 christos /* Wait for transition back to Working State */
175 1.1 christos
176 1.1 christos do
177 1.1 christos {
178 1.1 christos Status = AcpiRead (&SleepStatus, &AcpiGbl_FADT.SleepStatus);
179 1.1 christos if (ACPI_FAILURE (Status))
180 1.1 christos {
181 1.1 christos return_ACPI_STATUS (Status);
182 1.1 christos }
183 1.1 christos
184 1.1 christos } while (!(((UINT8) SleepStatus) & ACPI_X_WAKE_STATUS));
185 1.1 christos
186 1.1 christos return_ACPI_STATUS (AE_OK);
187 1.1 christos }
188 1.1 christos
189 1.1 christos
190 1.1 christos /*******************************************************************************
191 1.1 christos *
192 1.1 christos * FUNCTION: AcpiHwExtendedWakePrep
193 1.1 christos *
194 1.1 christos * PARAMETERS: SleepState - Which sleep state we just exited
195 1.1 christos *
196 1.1 christos * RETURN: Status
197 1.1 christos *
198 1.1 christos * DESCRIPTION: Perform first part of OS-independent ACPI cleanup after
199 1.1 christos * a sleep. Called with interrupts ENABLED.
200 1.1 christos *
201 1.1 christos ******************************************************************************/
202 1.1 christos
203 1.1 christos ACPI_STATUS
204 1.1 christos AcpiHwExtendedWakePrep (
205 1.1 christos UINT8 SleepState)
206 1.1 christos {
207 1.1 christos UINT8 SleepTypeValue;
208 1.1 christos
209 1.1 christos
210 1.1 christos ACPI_FUNCTION_TRACE (HwExtendedWakePrep);
211 1.1 christos
212 1.1 christos
213 1.11 christos if (AcpiGbl_SleepTypeAS0 != ACPI_SLEEP_TYPE_INVALID)
214 1.1 christos {
215 1.11 christos SleepTypeValue = ((AcpiGbl_SleepTypeAS0 << ACPI_X_SLEEP_TYPE_POSITION) &
216 1.1 christos ACPI_X_SLEEP_TYPE_MASK);
217 1.1 christos
218 1.1 christos (void) AcpiWrite ((UINT64) (SleepTypeValue | ACPI_X_SLEEP_ENABLE),
219 1.1 christos &AcpiGbl_FADT.SleepControl);
220 1.1 christos }
221 1.1 christos
222 1.1 christos return_ACPI_STATUS (AE_OK);
223 1.1 christos }
224 1.1 christos
225 1.1 christos
226 1.1 christos /*******************************************************************************
227 1.1 christos *
228 1.1 christos * FUNCTION: AcpiHwExtendedWake
229 1.1 christos *
230 1.1 christos * PARAMETERS: SleepState - Which sleep state we just exited
231 1.1 christos *
232 1.1 christos * RETURN: Status
233 1.1 christos *
234 1.1 christos * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
235 1.1 christos * Called with interrupts ENABLED.
236 1.1 christos *
237 1.1 christos ******************************************************************************/
238 1.1 christos
239 1.1 christos ACPI_STATUS
240 1.1 christos AcpiHwExtendedWake (
241 1.1 christos UINT8 SleepState)
242 1.1 christos {
243 1.1 christos ACPI_FUNCTION_TRACE (HwExtendedWake);
244 1.1 christos
245 1.1 christos
246 1.1 christos /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
247 1.1 christos
248 1.1 christos AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
249 1.1 christos
250 1.1 christos /* Execute the wake methods */
251 1.1 christos
252 1.2 christos AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__SST), ACPI_SST_WAKING);
253 1.2 christos AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__WAK), SleepState);
254 1.1 christos
255 1.1 christos /*
256 1.1 christos * Some BIOS code assumes that WAK_STS will be cleared on resume
257 1.1 christos * and use it to determine whether the system is rebooting or
258 1.1 christos * resuming. Clear WAK_STS for compatibility.
259 1.1 christos */
260 1.1 christos (void) AcpiWrite ((UINT64) ACPI_X_WAKE_STATUS, &AcpiGbl_FADT.SleepStatus);
261 1.1 christos AcpiGbl_SystemAwakeAndRunning = TRUE;
262 1.1 christos
263 1.2 christos AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__SST), ACPI_SST_WORKING);
264 1.1 christos return_ACPI_STATUS (AE_OK);
265 1.1 christos }
266