hwxfsleep.c 12 KB

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