hwsleep.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the
  5. * original/legacy sleep/PM 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("hwsleep")
  14. #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
  15. /*******************************************************************************
  16. *
  17. * FUNCTION: acpi_hw_legacy_sleep
  18. *
  19. * PARAMETERS: sleep_state - Which sleep state to enter
  20. *
  21. * RETURN: Status
  22. *
  23. * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
  24. * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
  25. *
  26. ******************************************************************************/
  27. acpi_status acpi_hw_legacy_sleep(u8 sleep_state)
  28. {
  29. struct acpi_bit_register_info *sleep_type_reg_info;
  30. struct acpi_bit_register_info *sleep_enable_reg_info;
  31. u32 pm1a_control;
  32. u32 pm1b_control;
  33. u32 in_value;
  34. acpi_status status;
  35. ACPI_FUNCTION_TRACE(hw_legacy_sleep);
  36. sleep_type_reg_info =
  37. acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
  38. sleep_enable_reg_info =
  39. acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
  40. /* Clear wake status */
  41. status = acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS,
  42. ACPI_CLEAR_STATUS);
  43. if (ACPI_FAILURE(status)) {
  44. return_ACPI_STATUS(status);
  45. }
  46. /* Disable all GPEs */
  47. status = acpi_hw_disable_all_gpes();
  48. if (ACPI_FAILURE(status)) {
  49. return_ACPI_STATUS(status);
  50. }
  51. status = acpi_hw_clear_acpi_status();
  52. if (ACPI_FAILURE(status)) {
  53. return_ACPI_STATUS(status);
  54. }
  55. acpi_gbl_system_awake_and_running = FALSE;
  56. /* Enable all wakeup GPEs */
  57. status = acpi_hw_enable_all_wakeup_gpes();
  58. if (ACPI_FAILURE(status)) {
  59. return_ACPI_STATUS(status);
  60. }
  61. /* Get current value of PM1A control */
  62. status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
  63. &pm1a_control);
  64. if (ACPI_FAILURE(status)) {
  65. return_ACPI_STATUS(status);
  66. }
  67. ACPI_DEBUG_PRINT((ACPI_DB_INIT,
  68. "Entering sleep state [S%u]\n", sleep_state));
  69. /* Clear the SLP_EN and SLP_TYP fields */
  70. pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
  71. sleep_enable_reg_info->access_bit_mask);
  72. pm1b_control = pm1a_control;
  73. /* Insert the SLP_TYP bits */
  74. pm1a_control |=
  75. (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
  76. pm1b_control |=
  77. (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
  78. /*
  79. * We split the writes of SLP_TYP and SLP_EN to workaround
  80. * poorly implemented hardware.
  81. */
  82. /* Write #1: write the SLP_TYP data to the PM1 Control registers */
  83. status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
  84. if (ACPI_FAILURE(status)) {
  85. return_ACPI_STATUS(status);
  86. }
  87. /* Insert the sleep enable (SLP_EN) bit */
  88. pm1a_control |= sleep_enable_reg_info->access_bit_mask;
  89. pm1b_control |= sleep_enable_reg_info->access_bit_mask;
  90. /* Flush caches, as per ACPI specification */
  91. if (sleep_state < ACPI_STATE_S4) {
  92. ACPI_FLUSH_CPU_CACHE();
  93. }
  94. status = acpi_os_enter_sleep(sleep_state, pm1a_control, pm1b_control);
  95. if (status == AE_CTRL_TERMINATE) {
  96. return_ACPI_STATUS(AE_OK);
  97. }
  98. if (ACPI_FAILURE(status)) {
  99. return_ACPI_STATUS(status);
  100. }
  101. /* Write #2: Write both SLP_TYP + SLP_EN */
  102. status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
  103. if (ACPI_FAILURE(status)) {
  104. return_ACPI_STATUS(status);
  105. }
  106. if (sleep_state > ACPI_STATE_S3) {
  107. /*
  108. * We wanted to sleep > S3, but it didn't happen (by virtue of the
  109. * fact that we are still executing!)
  110. *
  111. * Wait ten seconds, then try again. This is to get S4/S5 to work on
  112. * all machines.
  113. *
  114. * We wait so long to allow chipsets that poll this reg very slowly
  115. * to still read the right value. Ideally, this block would go
  116. * away entirely.
  117. */
  118. acpi_os_stall(10 * ACPI_USEC_PER_SEC);
  119. status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
  120. sleep_enable_reg_info->
  121. access_bit_mask);
  122. if (ACPI_FAILURE(status)) {
  123. return_ACPI_STATUS(status);
  124. }
  125. }
  126. /* Wait for transition back to Working State */
  127. do {
  128. status =
  129. acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
  130. if (ACPI_FAILURE(status)) {
  131. return_ACPI_STATUS(status);
  132. }
  133. } while (!in_value);
  134. return_ACPI_STATUS(AE_OK);
  135. }
  136. /*******************************************************************************
  137. *
  138. * FUNCTION: acpi_hw_legacy_wake_prep
  139. *
  140. * PARAMETERS: sleep_state - Which sleep state we just exited
  141. *
  142. * RETURN: Status
  143. *
  144. * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
  145. * sleep.
  146. * Called with interrupts ENABLED.
  147. *
  148. ******************************************************************************/
  149. acpi_status acpi_hw_legacy_wake_prep(u8 sleep_state)
  150. {
  151. acpi_status status = AE_OK;
  152. struct acpi_bit_register_info *sleep_type_reg_info;
  153. struct acpi_bit_register_info *sleep_enable_reg_info;
  154. u32 pm1a_control;
  155. u32 pm1b_control;
  156. ACPI_FUNCTION_TRACE(hw_legacy_wake_prep);
  157. /*
  158. * Set SLP_TYPE and SLP_EN to state S0.
  159. * This is unclear from the ACPI Spec, but it is required
  160. * by some machines.
  161. */
  162. if (acpi_gbl_sleep_type_a_s0 != ACPI_SLEEP_TYPE_INVALID) {
  163. sleep_type_reg_info =
  164. acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
  165. sleep_enable_reg_info =
  166. acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
  167. /* Get current value of PM1A control */
  168. status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
  169. &pm1a_control);
  170. if (ACPI_SUCCESS(status)) {
  171. /* Clear the SLP_EN and SLP_TYP fields */
  172. pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
  173. sleep_enable_reg_info->
  174. access_bit_mask);
  175. pm1b_control = pm1a_control;
  176. /* Insert the SLP_TYP bits */
  177. pm1a_control |= (acpi_gbl_sleep_type_a_s0 <<
  178. sleep_type_reg_info->bit_position);
  179. pm1b_control |= (acpi_gbl_sleep_type_b_s0 <<
  180. sleep_type_reg_info->bit_position);
  181. /* Write the control registers and ignore any errors */
  182. (void)acpi_hw_write_pm1_control(pm1a_control,
  183. pm1b_control);
  184. }
  185. }
  186. return_ACPI_STATUS(status);
  187. }
  188. /*******************************************************************************
  189. *
  190. * FUNCTION: acpi_hw_legacy_wake
  191. *
  192. * PARAMETERS: sleep_state - Which sleep state we just exited
  193. *
  194. * RETURN: Status
  195. *
  196. * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
  197. * Called with interrupts ENABLED.
  198. *
  199. ******************************************************************************/
  200. acpi_status acpi_hw_legacy_wake(u8 sleep_state)
  201. {
  202. acpi_status status;
  203. ACPI_FUNCTION_TRACE(hw_legacy_wake);
  204. /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
  205. acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
  206. acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WAKING);
  207. /*
  208. * GPEs must be enabled before _WAK is called as GPEs
  209. * might get fired there
  210. *
  211. * Restore the GPEs:
  212. * 1) Disable all GPEs
  213. * 2) Enable all runtime GPEs
  214. */
  215. status = acpi_hw_disable_all_gpes();
  216. if (ACPI_FAILURE(status)) {
  217. return_ACPI_STATUS(status);
  218. }
  219. status = acpi_hw_enable_all_runtime_gpes();
  220. if (ACPI_FAILURE(status)) {
  221. return_ACPI_STATUS(status);
  222. }
  223. /*
  224. * Now we can execute _WAK, etc. Some machines require that the GPEs
  225. * are enabled before the wake methods are executed.
  226. */
  227. acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK, sleep_state);
  228. /*
  229. * Some BIOS code assumes that WAK_STS will be cleared on resume
  230. * and use it to determine whether the system is rebooting or
  231. * resuming. Clear WAK_STS for compatibility.
  232. */
  233. (void)acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS,
  234. ACPI_CLEAR_STATUS);
  235. acpi_gbl_system_awake_and_running = TRUE;
  236. /* Enable power button */
  237. (void)
  238. acpi_write_bit_register(acpi_gbl_fixed_event_info
  239. [ACPI_EVENT_POWER_BUTTON].
  240. enable_register_id, ACPI_ENABLE_EVENT);
  241. (void)
  242. acpi_write_bit_register(acpi_gbl_fixed_event_info
  243. [ACPI_EVENT_POWER_BUTTON].
  244. status_register_id, ACPI_CLEAR_STATUS);
  245. /* Enable sleep button */
  246. (void)
  247. acpi_write_bit_register(acpi_gbl_fixed_event_info
  248. [ACPI_EVENT_SLEEP_BUTTON].
  249. enable_register_id, ACPI_ENABLE_EVENT);
  250. (void)
  251. acpi_write_bit_register(acpi_gbl_fixed_event_info
  252. [ACPI_EVENT_SLEEP_BUTTON].
  253. status_register_id, ACPI_CLEAR_STATUS);
  254. acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WORKING);
  255. return_ACPI_STATUS(status);
  256. }
  257. #endif /* !ACPI_REDUCED_HARDWARE */