hwxfsleep.c 13 KB

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