hwsleep.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /******************************************************************************
  2. *
  3. * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support Functions
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2012, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include <linux/tboot.h>
  45. #include <linux/module.h>
  46. #define _COMPONENT ACPI_HARDWARE
  47. ACPI_MODULE_NAME("hwsleep")
  48. static unsigned int gts, bfs;
  49. module_param(gts, uint, 0644);
  50. module_param(bfs, uint, 0644);
  51. MODULE_PARM_DESC(gts, "Enable evaluation of _GTS on suspend.");
  52. MODULE_PARM_DESC(bfs, "Enable evaluation of _BFS on resume".);
  53. /*******************************************************************************
  54. *
  55. * FUNCTION: acpi_hw_execute_sleep_method
  56. *
  57. * PARAMETERS: method_name - Pathname of method to execute
  58. * integer_argument - Argument to pass to the method
  59. *
  60. * RETURN: None
  61. *
  62. * DESCRIPTION: Execute a sleep/wake related method, with one integer argument
  63. * and no return value.
  64. *
  65. ******************************************************************************/
  66. void acpi_hw_execute_sleep_method(char *method_name, u32 integer_argument)
  67. {
  68. struct acpi_object_list arg_list;
  69. union acpi_object arg;
  70. acpi_status status;
  71. ACPI_FUNCTION_TRACE(hw_execute_sleep_method);
  72. if (!ACPI_STRCMP(METHOD_NAME__GTS, method_name) && !gts)
  73. return_VOID;
  74. if (!ACPI_STRCMP(METHOD_NAME__BFS, method_name) && !bfs)
  75. return_VOID;
  76. /* One argument, integer_argument */
  77. arg_list.count = 1;
  78. arg_list.pointer = &arg;
  79. arg.type = ACPI_TYPE_INTEGER;
  80. arg.integer.value = (u64)integer_argument;
  81. status = acpi_evaluate_object(NULL, method_name, &arg_list, NULL);
  82. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  83. ACPI_EXCEPTION((AE_INFO, status, "While executing method %s",
  84. method_name));
  85. }
  86. return_VOID;
  87. }
  88. #if (!ACPI_REDUCED_HARDWARE)
  89. /*******************************************************************************
  90. *
  91. * FUNCTION: acpi_hw_legacy_sleep
  92. *
  93. * PARAMETERS: sleep_state - Which sleep state to enter
  94. *
  95. * RETURN: Status
  96. *
  97. * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
  98. * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
  99. *
  100. ******************************************************************************/
  101. acpi_status acpi_hw_legacy_sleep(u8 sleep_state)
  102. {
  103. struct acpi_bit_register_info *sleep_type_reg_info;
  104. struct acpi_bit_register_info *sleep_enable_reg_info;
  105. u32 pm1a_control;
  106. u32 pm1b_control;
  107. u32 in_value;
  108. acpi_status status;
  109. ACPI_FUNCTION_TRACE(hw_legacy_sleep);
  110. sleep_type_reg_info =
  111. acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
  112. sleep_enable_reg_info =
  113. acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
  114. /* Clear wake status */
  115. status =
  116. acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
  117. if (ACPI_FAILURE(status)) {
  118. return_ACPI_STATUS(status);
  119. }
  120. /* Clear all fixed and general purpose status bits */
  121. status = acpi_hw_clear_acpi_status();
  122. if (ACPI_FAILURE(status)) {
  123. return_ACPI_STATUS(status);
  124. }
  125. if (sleep_state != ACPI_STATE_S5) {
  126. /*
  127. * Disable BM arbitration. This feature is contained within an
  128. * optional register (PM2 Control), so ignore a BAD_ADDRESS
  129. * exception.
  130. */
  131. status = acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
  132. if (ACPI_FAILURE(status) && (status != AE_BAD_ADDRESS)) {
  133. return_ACPI_STATUS(status);
  134. }
  135. }
  136. /*
  137. * 1) Disable/Clear all GPEs
  138. * 2) Enable all wakeup GPEs
  139. */
  140. status = acpi_hw_disable_all_gpes();
  141. if (ACPI_FAILURE(status)) {
  142. return_ACPI_STATUS(status);
  143. }
  144. acpi_gbl_system_awake_and_running = FALSE;
  145. status = acpi_hw_enable_all_wakeup_gpes();
  146. if (ACPI_FAILURE(status)) {
  147. return_ACPI_STATUS(status);
  148. }
  149. /* Execute the _GTS method (Going To Sleep) */
  150. acpi_hw_execute_sleep_method(METHOD_NAME__GTS, sleep_state);
  151. /* Get current value of PM1A control */
  152. status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
  153. &pm1a_control);
  154. if (ACPI_FAILURE(status)) {
  155. return_ACPI_STATUS(status);
  156. }
  157. ACPI_DEBUG_PRINT((ACPI_DB_INIT,
  158. "Entering sleep state [S%u]\n", sleep_state));
  159. /* Clear the SLP_EN and SLP_TYP fields */
  160. pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
  161. sleep_enable_reg_info->access_bit_mask);
  162. pm1b_control = pm1a_control;
  163. /* Insert the SLP_TYP bits */
  164. pm1a_control |=
  165. (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
  166. pm1b_control |=
  167. (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
  168. /*
  169. * We split the writes of SLP_TYP and SLP_EN to workaround
  170. * poorly implemented hardware.
  171. */
  172. /* Write #1: write the SLP_TYP data to the PM1 Control registers */
  173. status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
  174. if (ACPI_FAILURE(status)) {
  175. return_ACPI_STATUS(status);
  176. }
  177. /* Insert the sleep enable (SLP_EN) bit */
  178. pm1a_control |= sleep_enable_reg_info->access_bit_mask;
  179. pm1b_control |= sleep_enable_reg_info->access_bit_mask;
  180. /* Flush caches, as per ACPI specification */
  181. ACPI_FLUSH_CPU_CACHE();
  182. tboot_sleep(sleep_state, pm1a_control, pm1b_control);
  183. /* Write #2: Write both SLP_TYP + SLP_EN */
  184. status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
  185. if (ACPI_FAILURE(status)) {
  186. return_ACPI_STATUS(status);
  187. }
  188. if (sleep_state > ACPI_STATE_S3) {
  189. /*
  190. * We wanted to sleep > S3, but it didn't happen (by virtue of the
  191. * fact that we are still executing!)
  192. *
  193. * Wait ten seconds, then try again. This is to get S4/S5 to work on
  194. * all machines.
  195. *
  196. * We wait so long to allow chipsets that poll this reg very slowly
  197. * to still read the right value. Ideally, this block would go
  198. * away entirely.
  199. */
  200. acpi_os_stall(10000000);
  201. status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
  202. sleep_enable_reg_info->
  203. access_bit_mask);
  204. if (ACPI_FAILURE(status)) {
  205. return_ACPI_STATUS(status);
  206. }
  207. }
  208. /* Wait for transition back to Working State */
  209. do {
  210. status =
  211. acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
  212. if (ACPI_FAILURE(status)) {
  213. return_ACPI_STATUS(status);
  214. }
  215. } while (!in_value);
  216. return_ACPI_STATUS(AE_OK);
  217. }
  218. /*******************************************************************************
  219. *
  220. * FUNCTION: acpi_hw_legacy_wake_prep
  221. *
  222. * PARAMETERS: sleep_state - Which sleep state we just exited
  223. *
  224. * RETURN: Status
  225. *
  226. * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
  227. * sleep.
  228. * Called with interrupts ENABLED.
  229. *
  230. ******************************************************************************/
  231. acpi_status acpi_hw_legacy_wake_prep(u8 sleep_state)
  232. {
  233. acpi_status status;
  234. struct acpi_bit_register_info *sleep_type_reg_info;
  235. struct acpi_bit_register_info *sleep_enable_reg_info;
  236. u32 pm1a_control;
  237. u32 pm1b_control;
  238. ACPI_FUNCTION_TRACE(hw_legacy_wake_prep);
  239. /*
  240. * Set SLP_TYPE and SLP_EN to state S0.
  241. * This is unclear from the ACPI Spec, but it is required
  242. * by some machines.
  243. */
  244. status = acpi_get_sleep_type_data(ACPI_STATE_S0,
  245. &acpi_gbl_sleep_type_a,
  246. &acpi_gbl_sleep_type_b);
  247. if (ACPI_SUCCESS(status)) {
  248. sleep_type_reg_info =
  249. acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
  250. sleep_enable_reg_info =
  251. acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
  252. /* Get current value of PM1A control */
  253. status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
  254. &pm1a_control);
  255. if (ACPI_SUCCESS(status)) {
  256. /* Clear the SLP_EN and SLP_TYP fields */
  257. pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
  258. sleep_enable_reg_info->
  259. access_bit_mask);
  260. pm1b_control = pm1a_control;
  261. /* Insert the SLP_TYP bits */
  262. pm1a_control |= (acpi_gbl_sleep_type_a <<
  263. sleep_type_reg_info->bit_position);
  264. pm1b_control |= (acpi_gbl_sleep_type_b <<
  265. sleep_type_reg_info->bit_position);
  266. /* Write the control registers and ignore any errors */
  267. (void)acpi_hw_write_pm1_control(pm1a_control,
  268. pm1b_control);
  269. }
  270. }
  271. acpi_hw_execute_sleep_method(METHOD_NAME__BFS, sleep_state);
  272. return_ACPI_STATUS(status);
  273. }
  274. /*******************************************************************************
  275. *
  276. * FUNCTION: acpi_hw_legacy_wake
  277. *
  278. * PARAMETERS: sleep_state - Which sleep state we just exited
  279. *
  280. * RETURN: Status
  281. *
  282. * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
  283. * Called with interrupts ENABLED.
  284. *
  285. ******************************************************************************/
  286. acpi_status acpi_hw_legacy_wake(u8 sleep_state)
  287. {
  288. acpi_status status;
  289. ACPI_FUNCTION_TRACE(hw_legacy_wake);
  290. /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
  291. acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
  292. acpi_hw_execute_sleep_method(METHOD_NAME__SST, ACPI_SST_WAKING);
  293. /*
  294. * GPEs must be enabled before _WAK is called as GPEs
  295. * might get fired there
  296. *
  297. * Restore the GPEs:
  298. * 1) Disable/Clear all GPEs
  299. * 2) Enable all runtime GPEs
  300. */
  301. status = acpi_hw_disable_all_gpes();
  302. if (ACPI_FAILURE(status)) {
  303. return_ACPI_STATUS(status);
  304. }
  305. status = acpi_hw_enable_all_runtime_gpes();
  306. if (ACPI_FAILURE(status)) {
  307. return_ACPI_STATUS(status);
  308. }
  309. /*
  310. * Now we can execute _WAK, etc. Some machines require that the GPEs
  311. * are enabled before the wake methods are executed.
  312. */
  313. acpi_hw_execute_sleep_method(METHOD_NAME__WAK, sleep_state);
  314. /*
  315. * Some BIOS code assumes that WAK_STS will be cleared on resume
  316. * and use it to determine whether the system is rebooting or
  317. * resuming. Clear WAK_STS for compatibility.
  318. */
  319. acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, 1);
  320. acpi_gbl_system_awake_and_running = TRUE;
  321. /* Enable power button */
  322. (void)
  323. acpi_write_bit_register(acpi_gbl_fixed_event_info
  324. [ACPI_EVENT_POWER_BUTTON].
  325. enable_register_id, ACPI_ENABLE_EVENT);
  326. (void)
  327. acpi_write_bit_register(acpi_gbl_fixed_event_info
  328. [ACPI_EVENT_POWER_BUTTON].
  329. status_register_id, ACPI_CLEAR_STATUS);
  330. /*
  331. * Enable BM arbitration. This feature is contained within an
  332. * optional register (PM2 Control), so ignore a BAD_ADDRESS
  333. * exception.
  334. */
  335. status = acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
  336. if (ACPI_FAILURE(status) && (status != AE_BAD_ADDRESS)) {
  337. return_ACPI_STATUS(status);
  338. }
  339. acpi_hw_execute_sleep_method(METHOD_NAME__SST, ACPI_SST_WORKING);
  340. return_ACPI_STATUS(status);
  341. }
  342. #endif /* !ACPI_REDUCED_HARDWARE */
  343. /*******************************************************************************
  344. *
  345. * FUNCTION: acpi_hw_extended_sleep
  346. *
  347. * PARAMETERS: sleep_state - Which sleep state to enter
  348. *
  349. * RETURN: Status
  350. *
  351. * DESCRIPTION: Enter a system sleep state via the extended FADT sleep
  352. * registers (V5 FADT).
  353. * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
  354. *
  355. ******************************************************************************/
  356. acpi_status acpi_hw_extended_sleep(u8 sleep_state)
  357. {
  358. acpi_status status;
  359. u8 sleep_type_value;
  360. u64 sleep_status;
  361. ACPI_FUNCTION_TRACE(hw_extended_sleep);
  362. /* Extended sleep registers must be valid */
  363. if (!acpi_gbl_FADT.sleep_control.address ||
  364. !acpi_gbl_FADT.sleep_status.address) {
  365. return_ACPI_STATUS(AE_NOT_EXIST);
  366. }
  367. /* Clear wake status (WAK_STS) */
  368. status = acpi_write(ACPI_X_WAKE_STATUS, &acpi_gbl_FADT.sleep_status);
  369. if (ACPI_FAILURE(status)) {
  370. return_ACPI_STATUS(status);
  371. }
  372. acpi_gbl_system_awake_and_running = FALSE;
  373. /* Execute the _GTS method (Going To Sleep) */
  374. acpi_hw_execute_sleep_method(METHOD_NAME__GTS, sleep_state);
  375. /* Flush caches, as per ACPI specification */
  376. ACPI_FLUSH_CPU_CACHE();
  377. /*
  378. * Set the SLP_TYP and SLP_EN bits.
  379. *
  380. * Note: We only use the first value returned by the \_Sx method
  381. * (acpi_gbl_sleep_type_a) - As per ACPI specification.
  382. */
  383. ACPI_DEBUG_PRINT((ACPI_DB_INIT,
  384. "Entering sleep state [S%u]\n", sleep_state));
  385. sleep_type_value =
  386. ((acpi_gbl_sleep_type_a << ACPI_X_SLEEP_TYPE_POSITION) &
  387. ACPI_X_SLEEP_TYPE_MASK);
  388. status = acpi_write((sleep_type_value | ACPI_X_SLEEP_ENABLE),
  389. &acpi_gbl_FADT.sleep_control);
  390. if (ACPI_FAILURE(status)) {
  391. return_ACPI_STATUS(status);
  392. }
  393. /* Wait for transition back to Working State */
  394. do {
  395. status = acpi_read(&sleep_status, &acpi_gbl_FADT.sleep_status);
  396. if (ACPI_FAILURE(status)) {
  397. return_ACPI_STATUS(status);
  398. }
  399. } while (!(((u8)sleep_status) & ACPI_X_WAKE_STATUS));
  400. return_ACPI_STATUS(AE_OK);
  401. }
  402. /*******************************************************************************
  403. *
  404. * FUNCTION: acpi_hw_extended_wake_prep
  405. *
  406. * PARAMETERS: sleep_state - Which sleep state we just exited
  407. *
  408. * RETURN: Status
  409. *
  410. * DESCRIPTION: Perform first part of OS-independent ACPI cleanup after
  411. * a sleep. Called with interrupts ENABLED.
  412. *
  413. ******************************************************************************/
  414. acpi_status acpi_hw_extended_wake_prep(u8 sleep_state)
  415. {
  416. acpi_status status;
  417. u8 sleep_type_value;
  418. ACPI_FUNCTION_TRACE(hw_extended_wake_prep);
  419. status = acpi_get_sleep_type_data(ACPI_STATE_S0,
  420. &acpi_gbl_sleep_type_a,
  421. &acpi_gbl_sleep_type_b);
  422. if (ACPI_SUCCESS(status)) {
  423. sleep_type_value =
  424. ((acpi_gbl_sleep_type_a << ACPI_X_SLEEP_TYPE_POSITION) &
  425. ACPI_X_SLEEP_TYPE_MASK);
  426. (void)acpi_write((sleep_type_value | ACPI_X_SLEEP_ENABLE),
  427. &acpi_gbl_FADT.sleep_control);
  428. }
  429. acpi_hw_execute_sleep_method(METHOD_NAME__BFS, sleep_state);
  430. return_ACPI_STATUS(AE_OK);
  431. }
  432. /*******************************************************************************
  433. *
  434. * FUNCTION: acpi_hw_extended_wake
  435. *
  436. * PARAMETERS: sleep_state - Which sleep state we just exited
  437. *
  438. * RETURN: Status
  439. *
  440. * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
  441. * Called with interrupts ENABLED.
  442. *
  443. ******************************************************************************/
  444. acpi_status acpi_hw_extended_wake(u8 sleep_state)
  445. {
  446. ACPI_FUNCTION_TRACE(hw_extended_wake);
  447. /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
  448. acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
  449. /* Execute the wake methods */
  450. acpi_hw_execute_sleep_method(METHOD_NAME__SST, ACPI_SST_WAKING);
  451. acpi_hw_execute_sleep_method(METHOD_NAME__WAK, sleep_state);
  452. /*
  453. * Some BIOS code assumes that WAK_STS will be cleared on resume
  454. * and use it to determine whether the system is rebooting or
  455. * resuming. Clear WAK_STS for compatibility.
  456. */
  457. (void)acpi_write(ACPI_X_WAKE_STATUS, &acpi_gbl_FADT.sleep_status);
  458. acpi_gbl_system_awake_and_running = TRUE;
  459. acpi_hw_execute_sleep_method(METHOD_NAME__SST, ACPI_SST_WORKING);
  460. return_ACPI_STATUS(AE_OK);
  461. }