hwsleep.c 11 KB

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