hwsleep.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /******************************************************************************
  2. *
  3. * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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 <linux/module.h>
  43. #include <acpi/acpi.h>
  44. #define _COMPONENT ACPI_HARDWARE
  45. ACPI_MODULE_NAME ("hwsleep")
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_set_firmware_waking_vector
  49. *
  50. * PARAMETERS: physical_address - Physical address of ACPI real mode
  51. * entry point.
  52. *
  53. * RETURN: Status
  54. *
  55. * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
  56. *
  57. ******************************************************************************/
  58. acpi_status
  59. acpi_set_firmware_waking_vector (
  60. acpi_physical_address physical_address)
  61. {
  62. ACPI_FUNCTION_TRACE ("acpi_set_firmware_waking_vector");
  63. /* Set the vector */
  64. if (acpi_gbl_common_fACS.vector_width == 32) {
  65. *(ACPI_CAST_PTR (u32, acpi_gbl_common_fACS.firmware_waking_vector))
  66. = (u32) physical_address;
  67. }
  68. else {
  69. *acpi_gbl_common_fACS.firmware_waking_vector
  70. = physical_address;
  71. }
  72. return_ACPI_STATUS (AE_OK);
  73. }
  74. /*******************************************************************************
  75. *
  76. * FUNCTION: acpi_get_firmware_waking_vector
  77. *
  78. * PARAMETERS: *physical_address - Where the contents of
  79. * the firmware_waking_vector field of
  80. * the FACS will be returned.
  81. *
  82. * RETURN: Status, vector
  83. *
  84. * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
  85. *
  86. ******************************************************************************/
  87. #ifdef ACPI_FUTURE_USAGE
  88. acpi_status
  89. acpi_get_firmware_waking_vector (
  90. acpi_physical_address *physical_address)
  91. {
  92. ACPI_FUNCTION_TRACE ("acpi_get_firmware_waking_vector");
  93. if (!physical_address) {
  94. return_ACPI_STATUS (AE_BAD_PARAMETER);
  95. }
  96. /* Get the vector */
  97. if (acpi_gbl_common_fACS.vector_width == 32) {
  98. *physical_address = (acpi_physical_address)
  99. *(ACPI_CAST_PTR (u32, acpi_gbl_common_fACS.firmware_waking_vector));
  100. }
  101. else {
  102. *physical_address =
  103. *acpi_gbl_common_fACS.firmware_waking_vector;
  104. }
  105. return_ACPI_STATUS (AE_OK);
  106. }
  107. #endif
  108. /*******************************************************************************
  109. *
  110. * FUNCTION: acpi_enter_sleep_state_prep
  111. *
  112. * PARAMETERS: sleep_state - Which sleep state to enter
  113. *
  114. * RETURN: Status
  115. *
  116. * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
  117. * This function must execute with interrupts enabled.
  118. * We break sleeping into 2 stages so that OSPM can handle
  119. * various OS-specific tasks between the two steps.
  120. *
  121. ******************************************************************************/
  122. acpi_status
  123. acpi_enter_sleep_state_prep (
  124. u8 sleep_state)
  125. {
  126. acpi_status status;
  127. struct acpi_object_list arg_list;
  128. union acpi_object arg;
  129. ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state_prep");
  130. /*
  131. * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
  132. */
  133. status = acpi_get_sleep_type_data (sleep_state,
  134. &acpi_gbl_sleep_type_a, &acpi_gbl_sleep_type_b);
  135. if (ACPI_FAILURE (status)) {
  136. return_ACPI_STATUS (status);
  137. }
  138. /* Setup parameter object */
  139. arg_list.count = 1;
  140. arg_list.pointer = &arg;
  141. arg.type = ACPI_TYPE_INTEGER;
  142. arg.integer.value = sleep_state;
  143. /* Run the _PTS and _GTS methods */
  144. status = acpi_evaluate_object (NULL, METHOD_NAME__PTS, &arg_list, NULL);
  145. if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
  146. return_ACPI_STATUS (status);
  147. }
  148. status = acpi_evaluate_object (NULL, METHOD_NAME__GTS, &arg_list, NULL);
  149. if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
  150. return_ACPI_STATUS (status);
  151. }
  152. /* Setup the argument to _SST */
  153. switch (sleep_state) {
  154. case ACPI_STATE_S0:
  155. arg.integer.value = ACPI_SST_WORKING;
  156. break;
  157. case ACPI_STATE_S1:
  158. case ACPI_STATE_S2:
  159. case ACPI_STATE_S3:
  160. arg.integer.value = ACPI_SST_SLEEPING;
  161. break;
  162. case ACPI_STATE_S4:
  163. arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
  164. break;
  165. default:
  166. arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
  167. break;
  168. }
  169. /* Set the system indicators to show the desired sleep state. */
  170. status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
  171. if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
  172. ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
  173. acpi_format_exception (status)));
  174. }
  175. return_ACPI_STATUS (AE_OK);
  176. }
  177. /*******************************************************************************
  178. *
  179. * FUNCTION: acpi_enter_sleep_state
  180. *
  181. * PARAMETERS: sleep_state - Which sleep state to enter
  182. *
  183. * RETURN: Status
  184. *
  185. * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
  186. * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
  187. *
  188. ******************************************************************************/
  189. acpi_status asmlinkage
  190. acpi_enter_sleep_state (
  191. u8 sleep_state)
  192. {
  193. u32 PM1Acontrol;
  194. u32 PM1Bcontrol;
  195. struct acpi_bit_register_info *sleep_type_reg_info;
  196. struct acpi_bit_register_info *sleep_enable_reg_info;
  197. u32 in_value;
  198. acpi_status status;
  199. ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state");
  200. if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
  201. (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
  202. ACPI_REPORT_ERROR (("Sleep values out of range: A=%X B=%X\n",
  203. acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
  204. return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
  205. }
  206. sleep_type_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_TYPE_A);
  207. sleep_enable_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_ENABLE);
  208. /* Clear wake status */
  209. status = acpi_set_register (ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
  210. if (ACPI_FAILURE (status)) {
  211. return_ACPI_STATUS (status);
  212. }
  213. /* Clear all fixed and general purpose status bits */
  214. status = acpi_hw_clear_acpi_status (ACPI_MTX_DO_NOT_LOCK);
  215. if (ACPI_FAILURE (status)) {
  216. return_ACPI_STATUS (status);
  217. }
  218. /*
  219. * 1) Disable/Clear all GPEs
  220. * 2) Enable all wakeup GPEs
  221. */
  222. status = acpi_hw_disable_all_gpes (ACPI_ISR);
  223. if (ACPI_FAILURE (status)) {
  224. return_ACPI_STATUS (status);
  225. }
  226. acpi_gbl_system_awake_and_running = FALSE;
  227. status = acpi_hw_enable_all_wakeup_gpes (ACPI_ISR);
  228. if (ACPI_FAILURE (status)) {
  229. return_ACPI_STATUS (status);
  230. }
  231. /* Get current value of PM1A control */
  232. status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
  233. ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
  234. if (ACPI_FAILURE (status)) {
  235. return_ACPI_STATUS (status);
  236. }
  237. ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
  238. "Entering sleep state [S%d]\n", sleep_state));
  239. /* Clear SLP_EN and SLP_TYP fields */
  240. PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
  241. sleep_enable_reg_info->access_bit_mask);
  242. PM1Bcontrol = PM1Acontrol;
  243. /* Insert SLP_TYP bits */
  244. PM1Acontrol |= (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
  245. PM1Bcontrol |= (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
  246. /*
  247. * We split the writes of SLP_TYP and SLP_EN to workaround
  248. * poorly implemented hardware.
  249. */
  250. /* Write #1: fill in SLP_TYP data */
  251. status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
  252. ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
  253. if (ACPI_FAILURE (status)) {
  254. return_ACPI_STATUS (status);
  255. }
  256. status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
  257. ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
  258. if (ACPI_FAILURE (status)) {
  259. return_ACPI_STATUS (status);
  260. }
  261. /* Insert SLP_ENABLE bit */
  262. PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
  263. PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
  264. /* Write #2: SLP_TYP + SLP_EN */
  265. ACPI_FLUSH_CPU_CACHE ();
  266. status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
  267. ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
  268. if (ACPI_FAILURE (status)) {
  269. return_ACPI_STATUS (status);
  270. }
  271. status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
  272. ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
  273. if (ACPI_FAILURE (status)) {
  274. return_ACPI_STATUS (status);
  275. }
  276. if (sleep_state > ACPI_STATE_S3) {
  277. /*
  278. * We wanted to sleep > S3, but it didn't happen (by virtue of the
  279. * fact that we are still executing!)
  280. *
  281. * Wait ten seconds, then try again. This is to get S4/S5 to work on
  282. * all machines.
  283. *
  284. * We wait so long to allow chipsets that poll this reg very slowly to
  285. * still read the right value. Ideally, this block would go
  286. * away entirely.
  287. */
  288. acpi_os_stall (10000000);
  289. status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
  290. ACPI_REGISTER_PM1_CONTROL,
  291. sleep_enable_reg_info->access_bit_mask);
  292. if (ACPI_FAILURE (status)) {
  293. return_ACPI_STATUS (status);
  294. }
  295. }
  296. /* Wait until we enter sleep state */
  297. do {
  298. status = acpi_get_register (ACPI_BITREG_WAKE_STATUS, &in_value,
  299. ACPI_MTX_DO_NOT_LOCK);
  300. if (ACPI_FAILURE (status)) {
  301. return_ACPI_STATUS (status);
  302. }
  303. /* Spin until we wake */
  304. } while (!in_value);
  305. return_ACPI_STATUS (AE_OK);
  306. }
  307. EXPORT_SYMBOL(acpi_enter_sleep_state);
  308. /*******************************************************************************
  309. *
  310. * FUNCTION: acpi_enter_sleep_state_s4bios
  311. *
  312. * PARAMETERS: None
  313. *
  314. * RETURN: Status
  315. *
  316. * DESCRIPTION: Perform a S4 bios request.
  317. * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
  318. *
  319. ******************************************************************************/
  320. acpi_status asmlinkage
  321. acpi_enter_sleep_state_s4bios (
  322. void)
  323. {
  324. u32 in_value;
  325. acpi_status status;
  326. ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state_s4bios");
  327. status = acpi_set_register (ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
  328. if (ACPI_FAILURE (status)) {
  329. return_ACPI_STATUS (status);
  330. }
  331. status = acpi_hw_clear_acpi_status (ACPI_MTX_DO_NOT_LOCK);
  332. if (ACPI_FAILURE (status)) {
  333. return_ACPI_STATUS (status);
  334. }
  335. /*
  336. * 1) Disable/Clear all GPEs
  337. * 2) Enable all wakeup GPEs
  338. */
  339. status = acpi_hw_disable_all_gpes (ACPI_ISR);
  340. if (ACPI_FAILURE (status)) {
  341. return_ACPI_STATUS (status);
  342. }
  343. acpi_gbl_system_awake_and_running = FALSE;
  344. status = acpi_hw_enable_all_wakeup_gpes (ACPI_ISR);
  345. if (ACPI_FAILURE (status)) {
  346. return_ACPI_STATUS (status);
  347. }
  348. ACPI_FLUSH_CPU_CACHE ();
  349. status = acpi_os_write_port (acpi_gbl_FADT->smi_cmd,
  350. (u32) acpi_gbl_FADT->S4bios_req, 8);
  351. do {
  352. acpi_os_stall(1000);
  353. status = acpi_get_register (ACPI_BITREG_WAKE_STATUS, &in_value,
  354. ACPI_MTX_DO_NOT_LOCK);
  355. if (ACPI_FAILURE (status)) {
  356. return_ACPI_STATUS (status);
  357. }
  358. } while (!in_value);
  359. return_ACPI_STATUS (AE_OK);
  360. }
  361. EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios);
  362. /*******************************************************************************
  363. *
  364. * FUNCTION: acpi_leave_sleep_state
  365. *
  366. * PARAMETERS: sleep_state - Which sleep state we just exited
  367. *
  368. * RETURN: Status
  369. *
  370. * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
  371. * Called with interrupts ENABLED.
  372. *
  373. ******************************************************************************/
  374. acpi_status
  375. acpi_leave_sleep_state (
  376. u8 sleep_state)
  377. {
  378. struct acpi_object_list arg_list;
  379. union acpi_object arg;
  380. acpi_status status;
  381. struct acpi_bit_register_info *sleep_type_reg_info;
  382. struct acpi_bit_register_info *sleep_enable_reg_info;
  383. u32 PM1Acontrol;
  384. u32 PM1Bcontrol;
  385. ACPI_FUNCTION_TRACE ("acpi_leave_sleep_state");
  386. /*
  387. * Set SLP_TYPE and SLP_EN to state S0.
  388. * This is unclear from the ACPI Spec, but it is required
  389. * by some machines.
  390. */
  391. status = acpi_get_sleep_type_data (ACPI_STATE_S0,
  392. &acpi_gbl_sleep_type_a, &acpi_gbl_sleep_type_b);
  393. if (ACPI_SUCCESS (status)) {
  394. sleep_type_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_TYPE_A);
  395. sleep_enable_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_ENABLE);
  396. /* Get current value of PM1A control */
  397. status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
  398. ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
  399. if (ACPI_SUCCESS (status)) {
  400. /* Clear SLP_EN and SLP_TYP fields */
  401. PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
  402. sleep_enable_reg_info->access_bit_mask);
  403. PM1Bcontrol = PM1Acontrol;
  404. /* Insert SLP_TYP bits */
  405. PM1Acontrol |= (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
  406. PM1Bcontrol |= (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
  407. /* Just ignore any errors */
  408. (void) acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
  409. ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
  410. (void) acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
  411. ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
  412. }
  413. }
  414. /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
  415. acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
  416. /* Setup parameter object */
  417. arg_list.count = 1;
  418. arg_list.pointer = &arg;
  419. arg.type = ACPI_TYPE_INTEGER;
  420. /* Ignore any errors from these methods */
  421. arg.integer.value = ACPI_SST_WAKING;
  422. status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
  423. if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
  424. ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
  425. acpi_format_exception (status)));
  426. }
  427. arg.integer.value = sleep_state;
  428. status = acpi_evaluate_object (NULL, METHOD_NAME__BFS, &arg_list, NULL);
  429. if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
  430. ACPI_REPORT_ERROR (("Method _BFS failed, %s\n",
  431. acpi_format_exception (status)));
  432. }
  433. status = acpi_evaluate_object (NULL, METHOD_NAME__WAK, &arg_list, NULL);
  434. if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
  435. ACPI_REPORT_ERROR (("Method _WAK failed, %s\n",
  436. acpi_format_exception (status)));
  437. }
  438. /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
  439. /*
  440. * Restore the GPEs:
  441. * 1) Disable/Clear all GPEs
  442. * 2) Enable all runtime GPEs
  443. */
  444. status = acpi_hw_disable_all_gpes (ACPI_NOT_ISR);
  445. if (ACPI_FAILURE (status)) {
  446. return_ACPI_STATUS (status);
  447. }
  448. acpi_gbl_system_awake_and_running = TRUE;
  449. status = acpi_hw_enable_all_runtime_gpes (ACPI_NOT_ISR);
  450. if (ACPI_FAILURE (status)) {
  451. return_ACPI_STATUS (status);
  452. }
  453. /* Enable power button */
  454. (void) acpi_set_register(
  455. acpi_gbl_fixed_event_info[ACPI_EVENT_POWER_BUTTON].enable_register_id,
  456. 1, ACPI_MTX_DO_NOT_LOCK);
  457. (void) acpi_set_register(
  458. acpi_gbl_fixed_event_info[ACPI_EVENT_POWER_BUTTON].status_register_id,
  459. 1, ACPI_MTX_DO_NOT_LOCK);
  460. arg.integer.value = ACPI_SST_WORKING;
  461. status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
  462. if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
  463. ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
  464. acpi_format_exception (status)));
  465. }
  466. return_ACPI_STATUS (status);
  467. }