hwxfsleep.c 13 KB

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