hwxfsleep.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /******************************************************************************
  2. *
  3. * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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. #define EXPORT_ACPI_INTERFACES
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #define _COMPONENT ACPI_HARDWARE
  46. ACPI_MODULE_NAME("hwxfsleep")
  47. /* Local prototypes */
  48. #if (!ACPI_REDUCED_HARDWARE)
  49. static acpi_status
  50. acpi_hw_set_firmware_waking_vectors(struct acpi_table_facs *facs,
  51. acpi_physical_address physical_address,
  52. acpi_physical_address physical_address64);
  53. #endif
  54. static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id);
  55. /*
  56. * Dispatch table used to efficiently branch to the various sleep
  57. * functions.
  58. */
  59. #define ACPI_SLEEP_FUNCTION_ID 0
  60. #define ACPI_WAKE_PREP_FUNCTION_ID 1
  61. #define ACPI_WAKE_FUNCTION_ID 2
  62. /* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
  63. static struct acpi_sleep_functions acpi_sleep_dispatch[] = {
  64. {ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_sleep),
  65. acpi_hw_extended_sleep},
  66. {ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake_prep),
  67. acpi_hw_extended_wake_prep},
  68. {ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake), acpi_hw_extended_wake}
  69. };
  70. /*
  71. * These functions are removed for the ACPI_REDUCED_HARDWARE case:
  72. * acpi_set_firmware_waking_vectors
  73. * acpi_set_firmware_waking_vector
  74. * acpi_set_firmware_waking_vector64
  75. * acpi_enter_sleep_state_s4bios
  76. */
  77. #if (!ACPI_REDUCED_HARDWARE)
  78. /*******************************************************************************
  79. *
  80. * FUNCTION: acpi_hw_set_firmware_waking_vectors
  81. *
  82. * PARAMETERS: facs - Pointer to FACS table
  83. * physical_address - 32-bit physical address of ACPI real mode
  84. * entry point.
  85. * physical_address64 - 64-bit physical address of ACPI protected
  86. * mode entry point.
  87. *
  88. * RETURN: Status
  89. *
  90. * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
  91. *
  92. ******************************************************************************/
  93. static acpi_status
  94. acpi_hw_set_firmware_waking_vectors(struct acpi_table_facs *facs,
  95. acpi_physical_address physical_address,
  96. acpi_physical_address physical_address64)
  97. {
  98. ACPI_FUNCTION_TRACE(acpi_hw_set_firmware_waking_vectors);
  99. /*
  100. * According to the ACPI specification 2.0c and later, the 64-bit
  101. * waking vector should be cleared and the 32-bit waking vector should
  102. * be used, unless we want the wake-up code to be called by the BIOS in
  103. * Protected Mode. Some systems (for example HP dv5-1004nr) are known
  104. * to fail to resume if the 64-bit vector is used.
  105. */
  106. /* Set the 32-bit vector */
  107. facs->firmware_waking_vector = (u32)physical_address;
  108. if (facs->length > 32) {
  109. if (facs->version >= 1) {
  110. /* Set the 64-bit vector */
  111. facs->xfirmware_waking_vector = physical_address64;
  112. } else {
  113. /* Clear the 64-bit vector if it exists */
  114. facs->xfirmware_waking_vector = 0;
  115. }
  116. }
  117. return_ACPI_STATUS(AE_OK);
  118. }
  119. /*******************************************************************************
  120. *
  121. * FUNCTION: acpi_set_firmware_waking_vectors
  122. *
  123. * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
  124. * entry point.
  125. * physical_address64 - 64-bit physical address of ACPI protected
  126. * mode entry point.
  127. *
  128. * RETURN: Status
  129. *
  130. * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
  131. *
  132. ******************************************************************************/
  133. acpi_status
  134. acpi_set_firmware_waking_vectors(acpi_physical_address physical_address,
  135. acpi_physical_address physical_address64)
  136. {
  137. ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vectors);
  138. if (acpi_gbl_FACS) {
  139. (void)acpi_hw_set_firmware_waking_vectors(acpi_gbl_FACS,
  140. physical_address,
  141. physical_address64);
  142. }
  143. return_ACPI_STATUS(AE_OK);
  144. }
  145. ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vectors)
  146. /*******************************************************************************
  147. *
  148. * FUNCTION: acpi_set_firmware_waking_vector
  149. *
  150. * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
  151. * entry point.
  152. *
  153. * RETURN: Status
  154. *
  155. * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS
  156. *
  157. ******************************************************************************/
  158. acpi_status acpi_set_firmware_waking_vector(u32 physical_address)
  159. {
  160. acpi_status status;
  161. ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
  162. status = acpi_set_firmware_waking_vectors((acpi_physical_address)
  163. physical_address, 0);
  164. return_ACPI_STATUS(status);
  165. }
  166. ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
  167. #if ACPI_MACHINE_WIDTH == 64
  168. /*******************************************************************************
  169. *
  170. * FUNCTION: acpi_set_firmware_waking_vector64
  171. *
  172. * PARAMETERS: physical_address - 64-bit physical address of ACPI protected
  173. * mode entry point.
  174. *
  175. * RETURN: Status
  176. *
  177. * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if
  178. * it exists in the table. This function is intended for use with
  179. * 64-bit host operating systems.
  180. *
  181. ******************************************************************************/
  182. acpi_status acpi_set_firmware_waking_vector64(u64 physical_address)
  183. {
  184. acpi_status status;
  185. ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64);
  186. status = acpi_set_firmware_waking_vectors(0,
  187. (acpi_physical_address)
  188. physical_address);
  189. return_ACPI_STATUS(status);
  190. }
  191. ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64)
  192. #endif
  193. /*******************************************************************************
  194. *
  195. * FUNCTION: acpi_enter_sleep_state_s4bios
  196. *
  197. * PARAMETERS: None
  198. *
  199. * RETURN: Status
  200. *
  201. * DESCRIPTION: Perform a S4 bios request.
  202. * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
  203. *
  204. ******************************************************************************/
  205. acpi_status acpi_enter_sleep_state_s4bios(void)
  206. {
  207. u32 in_value;
  208. acpi_status status;
  209. ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
  210. /* Clear the wake status bit (PM1) */
  211. status =
  212. acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
  213. if (ACPI_FAILURE(status)) {
  214. return_ACPI_STATUS(status);
  215. }
  216. status = acpi_hw_clear_acpi_status();
  217. if (ACPI_FAILURE(status)) {
  218. return_ACPI_STATUS(status);
  219. }
  220. /*
  221. * 1) Disable/Clear all GPEs
  222. * 2) Enable all wakeup GPEs
  223. */
  224. status = acpi_hw_disable_all_gpes();
  225. if (ACPI_FAILURE(status)) {
  226. return_ACPI_STATUS(status);
  227. }
  228. acpi_gbl_system_awake_and_running = FALSE;
  229. status = acpi_hw_enable_all_wakeup_gpes();
  230. if (ACPI_FAILURE(status)) {
  231. return_ACPI_STATUS(status);
  232. }
  233. ACPI_FLUSH_CPU_CACHE();
  234. status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
  235. (u32)acpi_gbl_FADT.s4_bios_request, 8);
  236. do {
  237. acpi_os_stall(ACPI_USEC_PER_MSEC);
  238. status =
  239. acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
  240. if (ACPI_FAILURE(status)) {
  241. return_ACPI_STATUS(status);
  242. }
  243. } while (!in_value);
  244. return_ACPI_STATUS(AE_OK);
  245. }
  246. ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
  247. #endif /* !ACPI_REDUCED_HARDWARE */
  248. /*******************************************************************************
  249. *
  250. * FUNCTION: acpi_hw_sleep_dispatch
  251. *
  252. * PARAMETERS: sleep_state - Which sleep state to enter/exit
  253. * function_id - Sleep, wake_prep, or Wake
  254. *
  255. * RETURN: Status from the invoked sleep handling function.
  256. *
  257. * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
  258. * function.
  259. *
  260. ******************************************************************************/
  261. static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id)
  262. {
  263. acpi_status status;
  264. struct acpi_sleep_functions *sleep_functions =
  265. &acpi_sleep_dispatch[function_id];
  266. #if (!ACPI_REDUCED_HARDWARE)
  267. /*
  268. * If the Hardware Reduced flag is set (from the FADT), we must
  269. * use the extended sleep registers (FADT). Note: As per the ACPI
  270. * specification, these extended registers are to be used for HW-reduced
  271. * platforms only. They are not general-purpose replacements for the
  272. * legacy PM register sleep support.
  273. */
  274. if (acpi_gbl_reduced_hardware) {
  275. status = sleep_functions->extended_function(sleep_state);
  276. } else {
  277. /* Legacy sleep */
  278. status = sleep_functions->legacy_function(sleep_state);
  279. }
  280. return (status);
  281. #else
  282. /*
  283. * For the case where reduced-hardware-only code is being generated,
  284. * we know that only the extended sleep registers are available
  285. */
  286. status = sleep_functions->extended_function(sleep_state);
  287. return (status);
  288. #endif /* !ACPI_REDUCED_HARDWARE */
  289. }
  290. /*******************************************************************************
  291. *
  292. * FUNCTION: acpi_enter_sleep_state_prep
  293. *
  294. * PARAMETERS: sleep_state - Which sleep state to enter
  295. *
  296. * RETURN: Status
  297. *
  298. * DESCRIPTION: Prepare to enter a system sleep state.
  299. * This function must execute with interrupts enabled.
  300. * We break sleeping into 2 stages so that OSPM can handle
  301. * various OS-specific tasks between the two steps.
  302. *
  303. ******************************************************************************/
  304. acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
  305. {
  306. acpi_status status;
  307. struct acpi_object_list arg_list;
  308. union acpi_object arg;
  309. u32 sst_value;
  310. ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
  311. status = acpi_get_sleep_type_data(sleep_state,
  312. &acpi_gbl_sleep_type_a,
  313. &acpi_gbl_sleep_type_b);
  314. if (ACPI_FAILURE(status)) {
  315. return_ACPI_STATUS(status);
  316. }
  317. /* Execute the _PTS method (Prepare To Sleep) */
  318. arg_list.count = 1;
  319. arg_list.pointer = &arg;
  320. arg.type = ACPI_TYPE_INTEGER;
  321. arg.integer.value = sleep_state;
  322. status =
  323. acpi_evaluate_object(NULL, METHOD_PATHNAME__PTS, &arg_list, NULL);
  324. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  325. return_ACPI_STATUS(status);
  326. }
  327. /* Setup the argument to the _SST method (System STatus) */
  328. switch (sleep_state) {
  329. case ACPI_STATE_S0:
  330. sst_value = ACPI_SST_WORKING;
  331. break;
  332. case ACPI_STATE_S1:
  333. case ACPI_STATE_S2:
  334. case ACPI_STATE_S3:
  335. sst_value = ACPI_SST_SLEEPING;
  336. break;
  337. case ACPI_STATE_S4:
  338. sst_value = ACPI_SST_SLEEP_CONTEXT;
  339. break;
  340. default:
  341. sst_value = ACPI_SST_INDICATOR_OFF; /* Default is off */
  342. break;
  343. }
  344. /*
  345. * Set the system indicators to show the desired sleep state.
  346. * _SST is an optional method (return no error if not found)
  347. */
  348. acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, sst_value);
  349. return_ACPI_STATUS(AE_OK);
  350. }
  351. ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
  352. /*******************************************************************************
  353. *
  354. * FUNCTION: acpi_enter_sleep_state
  355. *
  356. * PARAMETERS: sleep_state - Which sleep state to enter
  357. *
  358. * RETURN: Status
  359. *
  360. * DESCRIPTION: Enter a system sleep state
  361. * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
  362. *
  363. ******************************************************************************/
  364. acpi_status acpi_enter_sleep_state(u8 sleep_state)
  365. {
  366. acpi_status status;
  367. ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
  368. if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
  369. (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
  370. ACPI_ERROR((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
  371. acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
  372. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  373. }
  374. status = acpi_hw_sleep_dispatch(sleep_state, ACPI_SLEEP_FUNCTION_ID);
  375. return_ACPI_STATUS(status);
  376. }
  377. ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
  378. /*******************************************************************************
  379. *
  380. * FUNCTION: acpi_leave_sleep_state_prep
  381. *
  382. * PARAMETERS: sleep_state - Which sleep state we are exiting
  383. *
  384. * RETURN: Status
  385. *
  386. * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
  387. * sleep. Called with interrupts DISABLED.
  388. * We break wake/resume into 2 stages so that OSPM can handle
  389. * various OS-specific tasks between the two steps.
  390. *
  391. ******************************************************************************/
  392. acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
  393. {
  394. acpi_status status;
  395. ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
  396. status =
  397. acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_PREP_FUNCTION_ID);
  398. return_ACPI_STATUS(status);
  399. }
  400. ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state_prep)
  401. /*******************************************************************************
  402. *
  403. * FUNCTION: acpi_leave_sleep_state
  404. *
  405. * PARAMETERS: sleep_state - Which sleep state we are exiting
  406. *
  407. * RETURN: Status
  408. *
  409. * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
  410. * Called with interrupts ENABLED.
  411. *
  412. ******************************************************************************/
  413. acpi_status acpi_leave_sleep_state(u8 sleep_state)
  414. {
  415. acpi_status status;
  416. ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
  417. status = acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_FUNCTION_ID);
  418. return_ACPI_STATUS(status);
  419. }
  420. ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)