xref: /netbsd-src/sys/external/bsd/acpica/dist/hardware/hwxfsleep.c (revision 987b04d624d6d5e25e3e80d683a4ebe80fe47dcf)
1 /******************************************************************************
2  *
3  * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2023, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #define EXPORT_ACPI_INTERFACES
45 
46 #include "acpi.h"
47 #include "accommon.h"
48 
49 #define _COMPONENT          ACPI_HARDWARE
50         ACPI_MODULE_NAME    ("hwxfsleep")
51 
52 /* Local prototypes */
53 
54 static ACPI_STATUS
55 AcpiHwSetFirmwareWakingVector (
56     ACPI_TABLE_FACS         *Facs,
57     ACPI_PHYSICAL_ADDRESS   PhysicalAddress,
58     ACPI_PHYSICAL_ADDRESS   PhysicalAddress64);
59 
60 static ACPI_STATUS
61 AcpiHwSleepDispatch (
62     UINT8                   SleepState,
63     UINT32                  FunctionId);
64 
65 /*
66  * Dispatch table used to efficiently branch to the various sleep
67  * functions.
68  */
69 #define ACPI_SLEEP_FUNCTION_ID          0
70 #define ACPI_WAKE_PREP_FUNCTION_ID      1
71 #define ACPI_WAKE_FUNCTION_ID           2
72 
73 /* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
74 
75 static ACPI_SLEEP_FUNCTIONS         AcpiSleepDispatch[] =
76 {
77     {ACPI_STRUCT_INIT (LegacyFunction,
78                        ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacySleep)),
79      ACPI_STRUCT_INIT (ExtendedFunction,
80                        AcpiHwExtendedSleep) },
81     {ACPI_STRUCT_INIT (LegacyFunction,
82                        ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacyWakePrep)),
83      ACPI_STRUCT_INIT (ExtendedFunction,
84                        AcpiHwExtendedWakePrep) },
85     {ACPI_STRUCT_INIT (LegacyFunction,
86                        ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacyWake)),
87      ACPI_STRUCT_INIT (ExtendedFunction,
88                        AcpiHwExtendedWake) }
89 };
90 
91 
92 /*******************************************************************************
93  *
94  * FUNCTION:    AcpiHwSetFirmwareWakingVector
95  *
96  * PARAMETERS:  Facs                - Pointer to FACS table
97  *              PhysicalAddress     - 32-bit physical address of ACPI real mode
98  *                                    entry point
99  *              PhysicalAddress64   - 64-bit physical address of ACPI protected
100  *                                    mode entry point
101  *
102  * RETURN:      Status
103  *
104  * DESCRIPTION: Sets the FirmwareWakingVector fields of the FACS
105  *
106  ******************************************************************************/
107 
108 static ACPI_STATUS
109 AcpiHwSetFirmwareWakingVector (
110     ACPI_TABLE_FACS         *Facs,
111     ACPI_PHYSICAL_ADDRESS   PhysicalAddress,
112     ACPI_PHYSICAL_ADDRESS   PhysicalAddress64)
113 {
114     ACPI_FUNCTION_TRACE (AcpiHwSetFirmwareWakingVector);
115 
116 
117     /*
118      * According to the ACPI specification 2.0c and later, the 64-bit
119      * waking vector should be cleared and the 32-bit waking vector should
120      * be used, unless we want the wake-up code to be called by the BIOS in
121      * Protected Mode. Some systems (for example HP dv5-1004nr) are known
122      * to fail to resume if the 64-bit vector is used.
123      */
124 
125     /* Set the 32-bit vector */
126 
127     Facs->FirmwareWakingVector = (UINT32) PhysicalAddress;
128 
129     if (Facs->Length > 32)
130     {
131         if (Facs->Version >= 1)
132         {
133             /* Set the 64-bit vector */
134 
135             Facs->XFirmwareWakingVector = PhysicalAddress64;
136         }
137         else
138         {
139             /* Clear the 64-bit vector if it exists */
140 
141             Facs->XFirmwareWakingVector = 0;
142         }
143     }
144 
145     return_ACPI_STATUS (AE_OK);
146 }
147 
148 
149 /*******************************************************************************
150  *
151  * FUNCTION:    AcpiSetFirmwareWakingVector
152  *
153  * PARAMETERS:  PhysicalAddress     - 32-bit physical address of ACPI real mode
154  *                                    entry point
155  *              PhysicalAddress64   - 64-bit physical address of ACPI protected
156  *                                    mode entry point
157  *
158  * RETURN:      Status
159  *
160  * DESCRIPTION: Sets the FirmwareWakingVector fields of the FACS
161  *
162  ******************************************************************************/
163 
164 ACPI_STATUS
165 AcpiSetFirmwareWakingVector (
166     ACPI_PHYSICAL_ADDRESS   PhysicalAddress,
167     ACPI_PHYSICAL_ADDRESS   PhysicalAddress64)
168 {
169 
170     ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector);
171 
172     if (AcpiGbl_FACS)
173     {
174         (void) AcpiHwSetFirmwareWakingVector (AcpiGbl_FACS,
175             PhysicalAddress, PhysicalAddress64);
176     }
177 
178     return_ACPI_STATUS (AE_OK);
179 }
180 
181 ACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector)
182 
183 
184 /*
185  * These functions are removed for the ACPI_REDUCED_HARDWARE case:
186  *      AcpiEnterSleepStateS4bios
187  */
188 
189 #if (!ACPI_REDUCED_HARDWARE)
190 /*******************************************************************************
191  *
192  * FUNCTION:    AcpiEnterSleepStateS4bios
193  *
194  * PARAMETERS:  None
195  *
196  * RETURN:      Status
197  *
198  * DESCRIPTION: Perform a S4 bios request.
199  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
200  *
201  ******************************************************************************/
202 
203 ACPI_STATUS
204 AcpiEnterSleepStateS4bios (
205     void)
206 {
207     UINT32                  InValue;
208     ACPI_STATUS             Status;
209 
210 
211     ACPI_FUNCTION_TRACE (AcpiEnterSleepStateS4bios);
212 
213 
214     /* Clear the wake status bit (PM1) */
215 
216     Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
217     if (ACPI_FAILURE (Status))
218     {
219         return_ACPI_STATUS (Status);
220     }
221 
222     Status = AcpiHwClearAcpiStatus ();
223     if (ACPI_FAILURE (Status))
224     {
225         return_ACPI_STATUS (Status);
226     }
227 
228     /*
229      * 1) Disable all GPEs
230      * 2) Enable all wakeup GPEs
231      */
232     Status = AcpiHwDisableAllGpes ();
233     if (ACPI_FAILURE (Status))
234     {
235         return_ACPI_STATUS (Status);
236     }
237     AcpiGbl_SystemAwakeAndRunning = FALSE;
238 
239     Status = AcpiHwEnableAllWakeupGpes ();
240     if (ACPI_FAILURE (Status))
241     {
242         return_ACPI_STATUS (Status);
243     }
244 
245     ACPI_FLUSH_CPU_CACHE ();
246 
247     Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
248         (UINT32) AcpiGbl_FADT.S4BiosRequest, 8);
249     if (ACPI_FAILURE (Status))
250     {
251         return_ACPI_STATUS (Status);
252     }
253 
254     do {
255         AcpiOsStall (ACPI_USEC_PER_MSEC);
256         Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
257         if (ACPI_FAILURE (Status))
258         {
259             return_ACPI_STATUS (Status);
260         }
261 
262     } while (!InValue);
263 
264     return_ACPI_STATUS (AE_OK);
265 }
266 
267 ACPI_EXPORT_SYMBOL (AcpiEnterSleepStateS4bios)
268 
269 #endif /* !ACPI_REDUCED_HARDWARE */
270 
271 
272 /*******************************************************************************
273  *
274  * FUNCTION:    AcpiHwSleepDispatch
275  *
276  * PARAMETERS:  SleepState          - Which sleep state to enter/exit
277  *              FunctionId          - Sleep, WakePrep, or Wake
278  *
279  * RETURN:      Status from the invoked sleep handling function.
280  *
281  * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
282  *              function.
283  *
284  ******************************************************************************/
285 
286 static ACPI_STATUS
287 AcpiHwSleepDispatch (
288     UINT8                   SleepState,
289     UINT32                  FunctionId)
290 {
291     ACPI_STATUS             Status;
292     ACPI_SLEEP_FUNCTIONS    *SleepFunctions = &AcpiSleepDispatch[FunctionId];
293 
294 
295 #if (!ACPI_REDUCED_HARDWARE)
296     /*
297      * If the Hardware Reduced flag is set (from the FADT), we must
298      * use the extended sleep registers (FADT). Note: As per the ACPI
299      * specification, these extended registers are to be used for HW-reduced
300      * platforms only. They are not general-purpose replacements for the
301      * legacy PM register sleep support.
302      */
303     if (AcpiGbl_ReducedHardware)
304     {
305         Status = SleepFunctions->ExtendedFunction (SleepState);
306     }
307     else
308     {
309         /* Legacy sleep */
310 
311         Status = SleepFunctions->LegacyFunction (SleepState);
312     }
313 
314     return (Status);
315 
316 #else
317     /*
318      * For the case where reduced-hardware-only code is being generated,
319      * we know that only the extended sleep registers are available
320      */
321     Status = SleepFunctions->ExtendedFunction (SleepState);
322     return (Status);
323 
324 #endif /* !ACPI_REDUCED_HARDWARE */
325 }
326 
327 
328 /*******************************************************************************
329  *
330  * FUNCTION:    AcpiEnterSleepStatePrep
331  *
332  * PARAMETERS:  SleepState          - Which sleep state to enter
333  *
334  * RETURN:      Status
335  *
336  * DESCRIPTION: Prepare to enter a system sleep state.
337  *              This function must execute with interrupts enabled.
338  *              We break sleeping into 2 stages so that OSPM can handle
339  *              various OS-specific tasks between the two steps.
340  *
341  ******************************************************************************/
342 
343 ACPI_STATUS
344 AcpiEnterSleepStatePrep (
345     UINT8                   SleepState)
346 {
347     ACPI_STATUS             Status;
348     ACPI_OBJECT_LIST        ArgList;
349     ACPI_OBJECT             Arg;
350     UINT32                  SstValue;
351 
352 
353     ACPI_FUNCTION_TRACE (AcpiEnterSleepStatePrep);
354 
355 
356     Status = AcpiGetSleepTypeData (SleepState,
357         &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
358     if (ACPI_FAILURE (Status))
359     {
360         return_ACPI_STATUS (Status);
361     }
362 
363     Status = AcpiGetSleepTypeData (ACPI_STATE_S0,
364         &AcpiGbl_SleepTypeAS0, &AcpiGbl_SleepTypeBS0);
365     if (ACPI_FAILURE (Status)) {
366         AcpiGbl_SleepTypeAS0 = ACPI_SLEEP_TYPE_INVALID;
367     }
368 
369     /* Execute the _PTS method (Prepare To Sleep) */
370 
371     ArgList.Count = 1;
372     ArgList.Pointer = &Arg;
373     Arg.Type = ACPI_TYPE_INTEGER;
374     Arg.Integer.Value = SleepState;
375 
376     Status = AcpiEvaluateObject (NULL, METHOD_PATHNAME__PTS, &ArgList, NULL);
377     if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
378     {
379         return_ACPI_STATUS (Status);
380     }
381 
382     /* Setup the argument to the _SST method (System STatus) */
383 
384     switch (SleepState)
385     {
386     case ACPI_STATE_S0:
387 
388         SstValue = ACPI_SST_WORKING;
389         break;
390 
391     case ACPI_STATE_S1:
392     case ACPI_STATE_S2:
393     case ACPI_STATE_S3:
394 
395         SstValue = ACPI_SST_SLEEPING;
396         break;
397 
398     case ACPI_STATE_S4:
399 
400         SstValue = ACPI_SST_SLEEP_CONTEXT;
401         break;
402 
403     default:
404 
405         SstValue = ACPI_SST_INDICATOR_OFF; /* Default is off */
406         break;
407     }
408 
409     /*
410      * Set the system indicators to show the desired sleep state.
411      * _SST is an optional method (return no error if not found)
412      */
413     AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__SST), SstValue);
414     return_ACPI_STATUS (AE_OK);
415 }
416 
417 ACPI_EXPORT_SYMBOL (AcpiEnterSleepStatePrep)
418 
419 
420 /*******************************************************************************
421  *
422  * FUNCTION:    AcpiEnterSleepState
423  *
424  * PARAMETERS:  SleepState          - Which sleep state to enter
425  *
426  * RETURN:      Status
427  *
428  * DESCRIPTION: Enter a system sleep state
429  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
430  *
431  ******************************************************************************/
432 
433 ACPI_STATUS
434 AcpiEnterSleepState (
435     UINT8                   SleepState)
436 {
437     ACPI_STATUS             Status;
438 
439 
440     ACPI_FUNCTION_TRACE (AcpiEnterSleepState);
441 
442 
443     if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
444         (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
445     {
446         ACPI_ERROR ((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
447             AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
448         return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
449     }
450 
451     Status = AcpiHwSleepDispatch (SleepState, ACPI_SLEEP_FUNCTION_ID);
452     return_ACPI_STATUS (Status);
453 }
454 
455 ACPI_EXPORT_SYMBOL (AcpiEnterSleepState)
456 
457 
458 /*******************************************************************************
459  *
460  * FUNCTION:    AcpiLeaveSleepStatePrep
461  *
462  * PARAMETERS:  SleepState          - Which sleep state we are exiting
463  *
464  * RETURN:      Status
465  *
466  * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
467  *              sleep. Called with interrupts DISABLED.
468  *              We break wake/resume into 2 stages so that OSPM can handle
469  *              various OS-specific tasks between the two steps.
470  *
471  ******************************************************************************/
472 
473 ACPI_STATUS
474 AcpiLeaveSleepStatePrep (
475     UINT8                   SleepState)
476 {
477     ACPI_STATUS             Status;
478 
479 
480     ACPI_FUNCTION_TRACE (AcpiLeaveSleepStatePrep);
481 
482 
483     Status = AcpiHwSleepDispatch (SleepState, ACPI_WAKE_PREP_FUNCTION_ID);
484     return_ACPI_STATUS (Status);
485 }
486 
487 ACPI_EXPORT_SYMBOL (AcpiLeaveSleepStatePrep)
488 
489 
490 /*******************************************************************************
491  *
492  * FUNCTION:    AcpiLeaveSleepState
493  *
494  * PARAMETERS:  SleepState          - Which sleep state we are exiting
495  *
496  * RETURN:      Status
497  *
498  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
499  *              Called with interrupts ENABLED.
500  *
501  ******************************************************************************/
502 
503 ACPI_STATUS
504 AcpiLeaveSleepState (
505     UINT8                   SleepState)
506 {
507     ACPI_STATUS             Status;
508 
509 
510     ACPI_FUNCTION_TRACE (AcpiLeaveSleepState);
511 
512 
513     Status = AcpiHwSleepDispatch (SleepState, ACPI_WAKE_FUNCTION_ID);
514     return_ACPI_STATUS (Status);
515 }
516 
517 ACPI_EXPORT_SYMBOL (AcpiLeaveSleepState)
518