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