hwsleep.c 10 KB

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