hwxfsleep.c 13 KB

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