2 /******************************************************************************
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2007, Intel Corp.
14 * All rights reserved.
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
74 * 3.4. Intel retains all right, title, and interest in and to the Original
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
118 #include <contrib/dev/acpica/acpi.h>
119 #include <contrib/dev/acpica/actables.h>
121 #define _COMPONENT ACPI_HARDWARE
122 ACPI_MODULE_NAME ("hwsleep")
125 /*******************************************************************************
127 * FUNCTION: AcpiSetFirmwareWakingVector
129 * PARAMETERS: PhysicalAddress - Physical address of ACPI real mode
134 * DESCRIPTION: Access function for the FirmwareWakingVector field in FACS
136 ******************************************************************************/
139 AcpiSetFirmwareWakingVector (
140 ACPI_PHYSICAL_ADDRESS PhysicalAddress)
142 ACPI_TABLE_FACS *Facs;
146 ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector);
150 Status = AcpiGetTableByIndex (ACPI_TABLE_INDEX_FACS,
151 ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &Facs));
152 if (ACPI_FAILURE (Status))
154 return_ACPI_STATUS (Status);
159 if ((Facs->Length < 32) ||
160 (!(Facs->XFirmwareWakingVector)))
163 * ACPI 1.0 FACS or short table or optional X_ field is zero
165 Facs->FirmwareWakingVector = (UINT32) PhysicalAddress;
170 * ACPI 2.0 FACS with valid X_ field
172 Facs->XFirmwareWakingVector = PhysicalAddress;
175 return_ACPI_STATUS (AE_OK);
178 ACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector)
181 /*******************************************************************************
183 * FUNCTION: AcpiGetFirmwareWakingVector
185 * PARAMETERS: *PhysicalAddress - Where the contents of
186 * the FirmwareWakingVector field of
187 * the FACS will be returned.
189 * RETURN: Status, vector
191 * DESCRIPTION: Access function for the FirmwareWakingVector field in FACS
193 ******************************************************************************/
196 AcpiGetFirmwareWakingVector (
197 ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
199 ACPI_TABLE_FACS *Facs;
203 ACPI_FUNCTION_TRACE (AcpiGetFirmwareWakingVector);
206 if (!PhysicalAddress)
208 return_ACPI_STATUS (AE_BAD_PARAMETER);
213 Status = AcpiGetTableByIndex (ACPI_TABLE_INDEX_FACS,
214 ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &Facs));
215 if (ACPI_FAILURE (Status))
217 return_ACPI_STATUS (Status);
222 if ((Facs->Length < 32) ||
223 (!(Facs->XFirmwareWakingVector)))
226 * ACPI 1.0 FACS or short table or optional X_ field is zero
229 (ACPI_PHYSICAL_ADDRESS) Facs->FirmwareWakingVector;
234 * ACPI 2.0 FACS with valid X_ field
236 *PhysicalAddress = (ACPI_PHYSICAL_ADDRESS) Facs->XFirmwareWakingVector;
239 return_ACPI_STATUS (AE_OK);
242 ACPI_EXPORT_SYMBOL (AcpiGetFirmwareWakingVector)
245 /*******************************************************************************
247 * FUNCTION: AcpiEnterSleepStatePrep
249 * PARAMETERS: SleepState - Which sleep state to enter
253 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
254 * This function must execute with interrupts enabled.
255 * We break sleeping into 2 stages so that OSPM can handle
256 * various OS-specific tasks between the two steps.
258 ******************************************************************************/
261 AcpiEnterSleepStatePrep (
265 ACPI_OBJECT_LIST ArgList;
269 ACPI_FUNCTION_TRACE (AcpiEnterSleepStatePrep);
273 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
275 Status = AcpiGetSleepTypeData (SleepState,
276 &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
277 if (ACPI_FAILURE (Status))
279 return_ACPI_STATUS (Status);
282 /* Setup parameter object */
285 ArgList.Pointer = &Arg;
287 Arg.Type = ACPI_TYPE_INTEGER;
288 Arg.Integer.Value = SleepState;
290 /* Run the _PTS and _GTS methods */
292 Status = AcpiEvaluateObject (NULL, METHOD_NAME__PTS, &ArgList, NULL);
293 if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
295 return_ACPI_STATUS (Status);
298 Status = AcpiEvaluateObject (NULL, METHOD_NAME__GTS, &ArgList, NULL);
299 if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
301 return_ACPI_STATUS (Status);
304 /* Setup the argument to _SST */
309 Arg.Integer.Value = ACPI_SST_WORKING;
315 Arg.Integer.Value = ACPI_SST_SLEEPING;
319 Arg.Integer.Value = ACPI_SST_SLEEP_CONTEXT;
323 Arg.Integer.Value = ACPI_SST_INDICATOR_OFF; /* Default is off */
327 /* Set the system indicators to show the desired sleep state. */
329 Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
330 if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
332 ACPI_EXCEPTION ((AE_INFO, Status, "While executing method _SST"));
335 return_ACPI_STATUS (AE_OK);
338 ACPI_EXPORT_SYMBOL (AcpiEnterSleepStatePrep)
341 /*******************************************************************************
343 * FUNCTION: AcpiEnterSleepState
345 * PARAMETERS: SleepState - Which sleep state to enter
349 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
350 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
352 ******************************************************************************/
355 AcpiEnterSleepState (
360 ACPI_BIT_REGISTER_INFO *SleepTypeRegInfo;
361 ACPI_BIT_REGISTER_INFO *SleepEnableRegInfo;
367 ACPI_FUNCTION_TRACE (AcpiEnterSleepState);
370 if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
371 (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
373 ACPI_ERROR ((AE_INFO, "Sleep values out of range: A=%X B=%X",
374 AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
375 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
378 SleepTypeRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE_A);
379 SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
381 /* Clear wake status */
383 Status = AcpiSetRegister (ACPI_BITREG_WAKE_STATUS, 1);
384 if (ACPI_FAILURE (Status))
386 return_ACPI_STATUS (Status);
389 /* Clear all fixed and general purpose status bits */
391 Status = AcpiHwClearAcpiStatus ();
392 if (ACPI_FAILURE (Status))
394 return_ACPI_STATUS (Status);
397 if (SleepState != ACPI_STATE_S5)
399 /* Disable BM arbitration */
401 Status = AcpiSetRegister (ACPI_BITREG_ARB_DISABLE, 1);
402 if (ACPI_FAILURE (Status))
404 return_ACPI_STATUS (Status);
409 * 1) Disable/Clear all GPEs
410 * 2) Enable all wakeup GPEs
412 Status = AcpiHwDisableAllGpes ();
413 if (ACPI_FAILURE (Status))
415 return_ACPI_STATUS (Status);
417 AcpiGbl_SystemAwakeAndRunning = FALSE;
419 Status = AcpiHwEnableAllWakeupGpes ();
420 if (ACPI_FAILURE (Status))
422 return_ACPI_STATUS (Status);
425 /* Get current value of PM1A control */
427 Status = AcpiHwRegisterRead (ACPI_MTX_DO_NOT_LOCK,
428 ACPI_REGISTER_PM1_CONTROL, &PM1AControl);
429 if (ACPI_FAILURE (Status))
431 return_ACPI_STATUS (Status);
433 ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
434 "Entering sleep state [S%d]\n", SleepState));
436 /* Clear SLP_EN and SLP_TYP fields */
438 PM1AControl &= ~(SleepTypeRegInfo->AccessBitMask |
439 SleepEnableRegInfo->AccessBitMask);
440 PM1BControl = PM1AControl;
442 /* Insert SLP_TYP bits */
444 PM1AControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
445 PM1BControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
448 * We split the writes of SLP_TYP and SLP_EN to workaround
449 * poorly implemented hardware.
452 /* Write #1: fill in SLP_TYP data */
454 Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
455 ACPI_REGISTER_PM1A_CONTROL, PM1AControl);
456 if (ACPI_FAILURE (Status))
458 return_ACPI_STATUS (Status);
461 Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
462 ACPI_REGISTER_PM1B_CONTROL, PM1BControl);
463 if (ACPI_FAILURE (Status))
465 return_ACPI_STATUS (Status);
468 /* Insert SLP_ENABLE bit */
470 PM1AControl |= SleepEnableRegInfo->AccessBitMask;
471 PM1BControl |= SleepEnableRegInfo->AccessBitMask;
473 /* Write #2: SLP_TYP + SLP_EN */
475 ACPI_FLUSH_CPU_CACHE ();
477 Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
478 ACPI_REGISTER_PM1A_CONTROL, PM1AControl);
479 if (ACPI_FAILURE (Status))
481 return_ACPI_STATUS (Status);
484 Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
485 ACPI_REGISTER_PM1B_CONTROL, PM1BControl);
486 if (ACPI_FAILURE (Status))
488 return_ACPI_STATUS (Status);
491 if (SleepState > ACPI_STATE_S3)
494 * We wanted to sleep > S3, but it didn't happen (by virtue of the
495 * fact that we are still executing!)
497 * Wait ten seconds, then try again. This is to get S4/S5 to work on
500 * We wait so long to allow chipsets that poll this reg very slowly to
501 * still read the right value. Ideally, this block would go
504 AcpiOsStall (10000000);
506 Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
507 ACPI_REGISTER_PM1_CONTROL,
508 SleepEnableRegInfo->AccessBitMask);
509 if (ACPI_FAILURE (Status))
511 return_ACPI_STATUS (Status);
515 /* Wait until we enter sleep state */
520 Status = AcpiGetRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
521 if (ACPI_FAILURE (Status))
523 return_ACPI_STATUS (Status);
527 * Some BIOSs don't set WAK_STS at all. Give up waiting after
528 * 1000 retries if it still isn't set.
535 /* Spin until we wake */
539 return_ACPI_STATUS (AE_OK);
542 ACPI_EXPORT_SYMBOL (AcpiEnterSleepState)
545 /*******************************************************************************
547 * FUNCTION: AcpiEnterSleepStateS4bios
553 * DESCRIPTION: Perform a S4 bios request.
554 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
556 ******************************************************************************/
559 AcpiEnterSleepStateS4bios (
566 ACPI_FUNCTION_TRACE (AcpiEnterSleepStateS4bios);
569 Status = AcpiSetRegister (ACPI_BITREG_WAKE_STATUS, 1);
570 if (ACPI_FAILURE (Status))
572 return_ACPI_STATUS (Status);
575 Status = AcpiHwClearAcpiStatus ();
576 if (ACPI_FAILURE (Status))
578 return_ACPI_STATUS (Status);
582 * 1) Disable/Clear all GPEs
583 * 2) Enable all wakeup GPEs
585 Status = AcpiHwDisableAllGpes ();
586 if (ACPI_FAILURE (Status))
588 return_ACPI_STATUS (Status);
590 AcpiGbl_SystemAwakeAndRunning = FALSE;
592 Status = AcpiHwEnableAllWakeupGpes ();
593 if (ACPI_FAILURE (Status))
595 return_ACPI_STATUS (Status);
598 ACPI_FLUSH_CPU_CACHE ();
600 Status = AcpiOsWritePort (AcpiGbl_FADT.SmiCommand,
601 (UINT32) AcpiGbl_FADT.S4BiosRequest, 8);
605 Status = AcpiGetRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
606 if (ACPI_FAILURE (Status))
608 return_ACPI_STATUS (Status);
612 return_ACPI_STATUS (AE_OK);
615 ACPI_EXPORT_SYMBOL (AcpiEnterSleepStateS4bios)
618 /*******************************************************************************
620 * FUNCTION: AcpiLeaveSleepState
622 * PARAMETERS: SleepState - Which sleep state we just exited
626 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
627 * Called with interrupts ENABLED.
629 ******************************************************************************/
632 AcpiLeaveSleepState (
635 ACPI_OBJECT_LIST ArgList;
638 ACPI_BIT_REGISTER_INFO *SleepTypeRegInfo;
639 ACPI_BIT_REGISTER_INFO *SleepEnableRegInfo;
644 ACPI_FUNCTION_TRACE (AcpiLeaveSleepState);
648 * Set SLP_TYPE and SLP_EN to state S0.
649 * This is unclear from the ACPI Spec, but it is required
652 Status = AcpiGetSleepTypeData (ACPI_STATE_S0,
653 &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
654 if (ACPI_SUCCESS (Status))
656 SleepTypeRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE_A);
657 SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
659 /* Get current value of PM1A control */
661 Status = AcpiHwRegisterRead (ACPI_MTX_DO_NOT_LOCK,
662 ACPI_REGISTER_PM1_CONTROL, &PM1AControl);
663 if (ACPI_SUCCESS (Status))
665 /* Clear SLP_EN and SLP_TYP fields */
667 PM1AControl &= ~(SleepTypeRegInfo->AccessBitMask |
668 SleepEnableRegInfo->AccessBitMask);
669 PM1BControl = PM1AControl;
671 /* Insert SLP_TYP bits */
673 PM1AControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
674 PM1BControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
676 /* Just ignore any errors */
678 (void) AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
679 ACPI_REGISTER_PM1A_CONTROL, PM1AControl);
680 (void) AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
681 ACPI_REGISTER_PM1B_CONTROL, PM1BControl);
685 /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
687 AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
689 /* Setup parameter object */
692 ArgList.Pointer = &Arg;
693 Arg.Type = ACPI_TYPE_INTEGER;
695 /* Ignore any errors from these methods */
697 Arg.Integer.Value = ACPI_SST_WAKING;
698 Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
699 if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
701 ACPI_EXCEPTION ((AE_INFO, Status, "During Method _SST"));
704 Arg.Integer.Value = SleepState;
705 Status = AcpiEvaluateObject (NULL, METHOD_NAME__BFS, &ArgList, NULL);
706 if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
708 ACPI_EXCEPTION ((AE_INFO, Status, "During Method _BFS"));
711 Status = AcpiEvaluateObject (NULL, METHOD_NAME__WAK, &ArgList, NULL);
712 if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
714 ACPI_EXCEPTION ((AE_INFO, Status, "During Method _WAK"));
716 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
720 * 1) Disable/Clear all GPEs
721 * 2) Enable all runtime GPEs
723 Status = AcpiHwDisableAllGpes ();
724 if (ACPI_FAILURE (Status))
726 return_ACPI_STATUS (Status);
728 AcpiGbl_SystemAwakeAndRunning = TRUE;
730 Status = AcpiHwEnableAllRuntimeGpes ();
731 if (ACPI_FAILURE (Status))
733 return_ACPI_STATUS (Status);
736 /* Enable power button */
738 (void) AcpiSetRegister(
739 AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId, 1);
741 (void) AcpiSetRegister(
742 AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId, 1);
744 /* Enable BM arbitration */
746 Status = AcpiSetRegister (ACPI_BITREG_ARB_DISABLE, 0);
747 if (ACPI_FAILURE (Status))
749 return_ACPI_STATUS (Status);
752 Arg.Integer.Value = ACPI_SST_WORKING;
753 Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
754 if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
756 ACPI_EXCEPTION ((AE_INFO, Status, "During Method _SST"));
759 return_ACPI_STATUS (Status);
762 ACPI_EXPORT_SYMBOL (AcpiLeaveSleepState)