hwsleep.c 8.2 KB

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