hwsleep.c 11 KB

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