hwesleep.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Name: hwesleep.c - ACPI Hardware Sleep/Wake Support functions for the
  5. * extended FADT-V5 sleep registers.
  6. *
  7. * Copyright (C) 2000 - 2022, Intel Corp.
  8. *
  9. *****************************************************************************/
  10. #include <acpi/acpi.h>
  11. #include "accommon.h"
  12. #define _COMPONENT ACPI_HARDWARE
  13. ACPI_MODULE_NAME("hwesleep")
  14. /*******************************************************************************
  15. *
  16. * FUNCTION: acpi_hw_execute_sleep_method
  17. *
  18. * PARAMETERS: method_pathname - Pathname of method to execute
  19. * integer_argument - Argument to pass to the method
  20. *
  21. * RETURN: None
  22. *
  23. * DESCRIPTION: Execute a sleep/wake related method with one integer argument
  24. * and no return value.
  25. *
  26. ******************************************************************************/
  27. void acpi_hw_execute_sleep_method(char *method_pathname, u32 integer_argument)
  28. {
  29. struct acpi_object_list arg_list;
  30. union acpi_object arg;
  31. acpi_status status;
  32. ACPI_FUNCTION_TRACE(hw_execute_sleep_method);
  33. /* One argument, integer_argument; No return value expected */
  34. arg_list.count = 1;
  35. arg_list.pointer = &arg;
  36. arg.type = ACPI_TYPE_INTEGER;
  37. arg.integer.value = (u64)integer_argument;
  38. status = acpi_evaluate_object(NULL, method_pathname, &arg_list, NULL);
  39. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  40. ACPI_EXCEPTION((AE_INFO, status, "While executing method %s",
  41. method_pathname));
  42. }
  43. return_VOID;
  44. }
  45. /*******************************************************************************
  46. *
  47. * FUNCTION: acpi_hw_extended_sleep
  48. *
  49. * PARAMETERS: sleep_state - Which sleep state to enter
  50. *
  51. * RETURN: Status
  52. *
  53. * DESCRIPTION: Enter a system sleep state via the extended FADT sleep
  54. * registers (V5 FADT).
  55. * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
  56. *
  57. ******************************************************************************/
  58. acpi_status acpi_hw_extended_sleep(u8 sleep_state)
  59. {
  60. acpi_status status;
  61. u8 sleep_control;
  62. u64 sleep_status;
  63. ACPI_FUNCTION_TRACE(hw_extended_sleep);
  64. /* Extended sleep registers must be valid */
  65. if (!acpi_gbl_FADT.sleep_control.address ||
  66. !acpi_gbl_FADT.sleep_status.address) {
  67. return_ACPI_STATUS(AE_NOT_EXIST);
  68. }
  69. /* Clear wake status (WAK_STS) */
  70. status = acpi_write((u64)ACPI_X_WAKE_STATUS,
  71. &acpi_gbl_FADT.sleep_status);
  72. if (ACPI_FAILURE(status)) {
  73. return_ACPI_STATUS(status);
  74. }
  75. acpi_gbl_system_awake_and_running = FALSE;
  76. /*
  77. * Set the SLP_TYP and SLP_EN bits.
  78. *
  79. * Note: We only use the first value returned by the \_Sx method
  80. * (acpi_gbl_sleep_type_a) - As per ACPI specification.
  81. */
  82. ACPI_DEBUG_PRINT((ACPI_DB_INIT,
  83. "Entering sleep state [S%u]\n", sleep_state));
  84. sleep_control = ((acpi_gbl_sleep_type_a << ACPI_X_SLEEP_TYPE_POSITION) &
  85. ACPI_X_SLEEP_TYPE_MASK) | ACPI_X_SLEEP_ENABLE;
  86. /* Flush caches, as per ACPI specification */
  87. if (sleep_state < ACPI_STATE_S4) {
  88. ACPI_FLUSH_CPU_CACHE();
  89. }
  90. status = acpi_os_enter_sleep(sleep_state, sleep_control, 0);
  91. if (status == AE_CTRL_TERMINATE) {
  92. return_ACPI_STATUS(AE_OK);
  93. }
  94. if (ACPI_FAILURE(status)) {
  95. return_ACPI_STATUS(status);
  96. }
  97. status = acpi_write((u64)sleep_control, &acpi_gbl_FADT.sleep_control);
  98. if (ACPI_FAILURE(status)) {
  99. return_ACPI_STATUS(status);
  100. }
  101. /* Wait for transition back to Working State */
  102. do {
  103. status = acpi_read(&sleep_status, &acpi_gbl_FADT.sleep_status);
  104. if (ACPI_FAILURE(status)) {
  105. return_ACPI_STATUS(status);
  106. }
  107. } while (!(((u8)sleep_status) & ACPI_X_WAKE_STATUS));
  108. return_ACPI_STATUS(AE_OK);
  109. }
  110. /*******************************************************************************
  111. *
  112. * FUNCTION: acpi_hw_extended_wake_prep
  113. *
  114. * PARAMETERS: sleep_state - Which sleep state we just exited
  115. *
  116. * RETURN: Status
  117. *
  118. * DESCRIPTION: Perform first part of OS-independent ACPI cleanup after
  119. * a sleep. Called with interrupts ENABLED.
  120. *
  121. ******************************************************************************/
  122. acpi_status acpi_hw_extended_wake_prep(u8 sleep_state)
  123. {
  124. u8 sleep_type_value;
  125. ACPI_FUNCTION_TRACE(hw_extended_wake_prep);
  126. if (acpi_gbl_sleep_type_a_s0 != ACPI_SLEEP_TYPE_INVALID) {
  127. sleep_type_value =
  128. ((acpi_gbl_sleep_type_a_s0 << ACPI_X_SLEEP_TYPE_POSITION) &
  129. ACPI_X_SLEEP_TYPE_MASK);
  130. (void)acpi_write((u64)(sleep_type_value | ACPI_X_SLEEP_ENABLE),
  131. &acpi_gbl_FADT.sleep_control);
  132. }
  133. return_ACPI_STATUS(AE_OK);
  134. }
  135. /*******************************************************************************
  136. *
  137. * FUNCTION: acpi_hw_extended_wake
  138. *
  139. * PARAMETERS: sleep_state - Which sleep state we just exited
  140. *
  141. * RETURN: Status
  142. *
  143. * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
  144. * Called with interrupts ENABLED.
  145. *
  146. ******************************************************************************/
  147. acpi_status acpi_hw_extended_wake(u8 sleep_state)
  148. {
  149. ACPI_FUNCTION_TRACE(hw_extended_wake);
  150. /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
  151. acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
  152. /* Execute the wake methods */
  153. acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WAKING);
  154. acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK, sleep_state);
  155. /*
  156. * Some BIOS code assumes that WAK_STS will be cleared on resume
  157. * and use it to determine whether the system is rebooting or
  158. * resuming. Clear WAK_STS for compatibility.
  159. */
  160. (void)acpi_write((u64)ACPI_X_WAKE_STATUS, &acpi_gbl_FADT.sleep_status);
  161. acpi_gbl_system_awake_and_running = TRUE;
  162. acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WORKING);
  163. return_ACPI_STATUS(AE_OK);
  164. }