hwsleep.c 16 KB

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