hwsleep.c 11 KB

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