main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * sleep.c - ACPI sleep support.
  3. *
  4. * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
  5. * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
  6. * Copyright (c) 2000-2003 Patrick Mochel
  7. * Copyright (c) 2003 Open Source Development Lab
  8. *
  9. * This file is released under the GPLv2.
  10. *
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/irq.h>
  14. #include <linux/dmi.h>
  15. #include <linux/device.h>
  16. #include <linux/suspend.h>
  17. #include <asm/io.h>
  18. #include <acpi/acpi_bus.h>
  19. #include <acpi/acpi_drivers.h>
  20. #include "sleep.h"
  21. u8 sleep_states[ACPI_S_STATE_COUNT];
  22. #ifdef CONFIG_PM_SLEEP
  23. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  24. #endif
  25. static int acpi_sleep_prepare(u32 acpi_state)
  26. {
  27. #ifdef CONFIG_ACPI_SLEEP
  28. /* do we have a wakeup address for S2 and S3? */
  29. if (acpi_state == ACPI_STATE_S3) {
  30. if (!acpi_wakeup_address) {
  31. return -EFAULT;
  32. }
  33. acpi_set_firmware_waking_vector(
  34. (acpi_physical_address)acpi_wakeup_address);
  35. }
  36. ACPI_FLUSH_CPU_CACHE();
  37. acpi_enable_wakeup_device_prep(acpi_state);
  38. #endif
  39. printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
  40. acpi_state);
  41. acpi_enter_sleep_state_prep(acpi_state);
  42. return 0;
  43. }
  44. #ifdef CONFIG_SUSPEND
  45. static struct platform_suspend_ops acpi_suspend_ops;
  46. extern void do_suspend_lowlevel(void);
  47. static u32 acpi_suspend_states[] = {
  48. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  49. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  50. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  51. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  52. };
  53. /**
  54. * acpi_suspend_begin - Set the target system sleep state to the state
  55. * associated with given @pm_state, if supported.
  56. */
  57. static int acpi_suspend_begin(suspend_state_t pm_state)
  58. {
  59. u32 acpi_state = acpi_suspend_states[pm_state];
  60. int error = 0;
  61. if (sleep_states[acpi_state]) {
  62. acpi_target_sleep_state = acpi_state;
  63. } else {
  64. printk(KERN_ERR "ACPI does not support this state: %d\n",
  65. pm_state);
  66. error = -ENOSYS;
  67. }
  68. return error;
  69. }
  70. /**
  71. * acpi_suspend_prepare - Do preliminary suspend work.
  72. *
  73. * If necessary, set the firmware waking vector and do arch-specific
  74. * nastiness to get the wakeup code to the waking vector.
  75. */
  76. static int acpi_suspend_prepare(void)
  77. {
  78. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  79. if (error) {
  80. acpi_target_sleep_state = ACPI_STATE_S0;
  81. return error;
  82. }
  83. return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
  84. }
  85. /**
  86. * acpi_suspend_enter - Actually enter a sleep state.
  87. * @pm_state: ignored
  88. *
  89. * Flush caches and go to sleep. For STR we have to call arch-specific
  90. * assembly, which in turn call acpi_enter_sleep_state().
  91. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  92. */
  93. static int acpi_suspend_enter(suspend_state_t pm_state)
  94. {
  95. acpi_status status = AE_OK;
  96. unsigned long flags = 0;
  97. u32 acpi_state = acpi_target_sleep_state;
  98. ACPI_FLUSH_CPU_CACHE();
  99. /* Do arch specific saving of state. */
  100. if (acpi_state == ACPI_STATE_S3) {
  101. int error = acpi_save_state_mem();
  102. if (error)
  103. return error;
  104. }
  105. local_irq_save(flags);
  106. acpi_enable_wakeup_device(acpi_state);
  107. switch (acpi_state) {
  108. case ACPI_STATE_S1:
  109. barrier();
  110. status = acpi_enter_sleep_state(acpi_state);
  111. break;
  112. case ACPI_STATE_S3:
  113. do_suspend_lowlevel();
  114. break;
  115. }
  116. /* Reprogram control registers and execute _BFS */
  117. acpi_leave_sleep_state_prep(acpi_state);
  118. /* ACPI 3.0 specs (P62) says that it's the responsibility
  119. * of the OSPM to clear the status bit [ implying that the
  120. * POWER_BUTTON event should not reach userspace ]
  121. */
  122. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
  123. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  124. /*
  125. * Disable and clear GPE status before interrupt is enabled. Some GPEs
  126. * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
  127. * acpi_leave_sleep_state will reenable specific GPEs later
  128. */
  129. acpi_hw_disable_all_gpes();
  130. local_irq_restore(flags);
  131. printk(KERN_DEBUG "Back to C!\n");
  132. /* restore processor state */
  133. if (acpi_state == ACPI_STATE_S3)
  134. acpi_restore_state_mem();
  135. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  136. }
  137. /**
  138. * acpi_suspend_finish - Instruct the platform to leave a sleep state.
  139. *
  140. * This is called after we wake back up (or if entering the sleep state
  141. * failed).
  142. */
  143. static void acpi_suspend_finish(void)
  144. {
  145. u32 acpi_state = acpi_target_sleep_state;
  146. acpi_disable_wakeup_device(acpi_state);
  147. acpi_leave_sleep_state(acpi_state);
  148. /* reset firmware waking vector */
  149. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  150. acpi_target_sleep_state = ACPI_STATE_S0;
  151. }
  152. /**
  153. * acpi_suspend_end - Finish up suspend sequence.
  154. */
  155. static void acpi_suspend_end(void)
  156. {
  157. /*
  158. * This is necessary in case acpi_suspend_finish() is not called during a
  159. * failing transition to a sleep state.
  160. */
  161. acpi_target_sleep_state = ACPI_STATE_S0;
  162. }
  163. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  164. {
  165. u32 acpi_state;
  166. switch (pm_state) {
  167. case PM_SUSPEND_ON:
  168. case PM_SUSPEND_STANDBY:
  169. case PM_SUSPEND_MEM:
  170. acpi_state = acpi_suspend_states[pm_state];
  171. return sleep_states[acpi_state];
  172. default:
  173. return 0;
  174. }
  175. }
  176. static struct platform_suspend_ops acpi_suspend_ops = {
  177. .valid = acpi_suspend_state_valid,
  178. .begin = acpi_suspend_begin,
  179. .prepare = acpi_suspend_prepare,
  180. .enter = acpi_suspend_enter,
  181. .finish = acpi_suspend_finish,
  182. .end = acpi_suspend_end,
  183. };
  184. #endif /* CONFIG_SUSPEND */
  185. #ifdef CONFIG_HIBERNATION
  186. static int acpi_hibernation_begin(void)
  187. {
  188. acpi_target_sleep_state = ACPI_STATE_S4;
  189. return 0;
  190. }
  191. static int acpi_hibernation_prepare(void)
  192. {
  193. int error = acpi_sleep_prepare(ACPI_STATE_S4);
  194. if (error) {
  195. acpi_target_sleep_state = ACPI_STATE_S0;
  196. return error;
  197. }
  198. return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
  199. }
  200. static int acpi_hibernation_enter(void)
  201. {
  202. acpi_status status = AE_OK;
  203. unsigned long flags = 0;
  204. ACPI_FLUSH_CPU_CACHE();
  205. local_irq_save(flags);
  206. acpi_enable_wakeup_device(ACPI_STATE_S4);
  207. /* This shouldn't return. If it returns, we have a problem */
  208. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  209. /* Reprogram control registers and execute _BFS */
  210. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  211. local_irq_restore(flags);
  212. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  213. }
  214. static void acpi_hibernation_leave(void)
  215. {
  216. /*
  217. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  218. * enable it here.
  219. */
  220. acpi_enable();
  221. /* Reprogram control registers and execute _BFS */
  222. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  223. }
  224. static void acpi_hibernation_finish(void)
  225. {
  226. acpi_disable_wakeup_device(ACPI_STATE_S4);
  227. acpi_leave_sleep_state(ACPI_STATE_S4);
  228. /* reset firmware waking vector */
  229. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  230. acpi_target_sleep_state = ACPI_STATE_S0;
  231. }
  232. static void acpi_hibernation_end(void)
  233. {
  234. /*
  235. * This is necessary in case acpi_hibernation_finish() is not called
  236. * during a failing transition to the sleep state.
  237. */
  238. acpi_target_sleep_state = ACPI_STATE_S0;
  239. }
  240. static int acpi_hibernation_pre_restore(void)
  241. {
  242. acpi_status status;
  243. status = acpi_hw_disable_all_gpes();
  244. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  245. }
  246. static void acpi_hibernation_restore_cleanup(void)
  247. {
  248. acpi_hw_enable_all_runtime_gpes();
  249. }
  250. static struct platform_hibernation_ops acpi_hibernation_ops = {
  251. .begin = acpi_hibernation_begin,
  252. .end = acpi_hibernation_end,
  253. .pre_snapshot = acpi_hibernation_prepare,
  254. .finish = acpi_hibernation_finish,
  255. .prepare = acpi_hibernation_prepare,
  256. .enter = acpi_hibernation_enter,
  257. .leave = acpi_hibernation_leave,
  258. .pre_restore = acpi_hibernation_pre_restore,
  259. .restore_cleanup = acpi_hibernation_restore_cleanup,
  260. };
  261. #endif /* CONFIG_HIBERNATION */
  262. int acpi_suspend(u32 acpi_state)
  263. {
  264. suspend_state_t states[] = {
  265. [1] = PM_SUSPEND_STANDBY,
  266. [3] = PM_SUSPEND_MEM,
  267. [5] = PM_SUSPEND_MAX
  268. };
  269. if (acpi_state < 6 && states[acpi_state])
  270. return pm_suspend(states[acpi_state]);
  271. if (acpi_state == 4)
  272. return hibernate();
  273. return -EINVAL;
  274. }
  275. #ifdef CONFIG_PM_SLEEP
  276. /**
  277. * acpi_pm_device_sleep_state - return preferred power state of ACPI device
  278. * in the system sleep state given by %acpi_target_sleep_state
  279. * @dev: device to examine; its driver model wakeup flags control
  280. * whether it should be able to wake up the system
  281. * @d_min_p: used to store the upper limit of allowed states range
  282. * Return value: preferred power state of the device on success, -ENODEV on
  283. * failure (ie. if there's no 'struct acpi_device' for @dev)
  284. *
  285. * Find the lowest power (highest number) ACPI device power state that
  286. * device @dev can be in while the system is in the sleep state represented
  287. * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
  288. * able to wake up the system from this sleep state. If @d_min_p is set,
  289. * the highest power (lowest number) device power state of @dev allowed
  290. * in this system sleep state is stored at the location pointed to by it.
  291. *
  292. * The caller must ensure that @dev is valid before using this function.
  293. * The caller is also responsible for figuring out if the device is
  294. * supposed to be able to wake up the system and passing this information
  295. * via @wake.
  296. */
  297. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
  298. {
  299. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  300. struct acpi_device *adev;
  301. char acpi_method[] = "_SxD";
  302. unsigned long d_min, d_max;
  303. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  304. printk(KERN_DEBUG "ACPI handle has no context!\n");
  305. return -ENODEV;
  306. }
  307. acpi_method[2] = '0' + acpi_target_sleep_state;
  308. /*
  309. * If the sleep state is S0, we will return D3, but if the device has
  310. * _S0W, we will use the value from _S0W
  311. */
  312. d_min = ACPI_STATE_D0;
  313. d_max = ACPI_STATE_D3;
  314. /*
  315. * If present, _SxD methods return the minimum D-state (highest power
  316. * state) we can use for the corresponding S-states. Otherwise, the
  317. * minimum D-state is D0 (ACPI 3.x).
  318. *
  319. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  320. * provided -- that's our fault recovery, we ignore retval.
  321. */
  322. if (acpi_target_sleep_state > ACPI_STATE_S0)
  323. acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
  324. /*
  325. * If _PRW says we can wake up the system from the target sleep state,
  326. * the D-state returned by _SxD is sufficient for that (we assume a
  327. * wakeup-aware driver if wake is set). Still, if _SxW exists
  328. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  329. * can wake the system. _S0W may be valid, too.
  330. */
  331. if (acpi_target_sleep_state == ACPI_STATE_S0 ||
  332. (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
  333. adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
  334. acpi_status status;
  335. acpi_method[3] = 'W';
  336. status = acpi_evaluate_integer(handle, acpi_method, NULL,
  337. &d_max);
  338. if (ACPI_FAILURE(status)) {
  339. d_max = d_min;
  340. } else if (d_max < d_min) {
  341. /* Warn the user of the broken DSDT */
  342. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  343. acpi_method);
  344. /* Sanitize it */
  345. d_min = d_max;
  346. }
  347. }
  348. if (d_min_p)
  349. *d_min_p = d_min;
  350. return d_max;
  351. }
  352. #endif
  353. static void acpi_power_off_prepare(void)
  354. {
  355. /* Prepare to power off the system */
  356. acpi_sleep_prepare(ACPI_STATE_S5);
  357. acpi_hw_disable_all_gpes();
  358. }
  359. static void acpi_power_off(void)
  360. {
  361. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  362. printk("%s called\n", __func__);
  363. local_irq_disable();
  364. acpi_enable_wakeup_device(ACPI_STATE_S5);
  365. acpi_enter_sleep_state(ACPI_STATE_S5);
  366. }
  367. int __init acpi_sleep_init(void)
  368. {
  369. acpi_status status;
  370. u8 type_a, type_b;
  371. #ifdef CONFIG_SUSPEND
  372. int i = 0;
  373. #endif
  374. if (acpi_disabled)
  375. return 0;
  376. sleep_states[ACPI_STATE_S0] = 1;
  377. printk(KERN_INFO PREFIX "(supports S0");
  378. #ifdef CONFIG_SUSPEND
  379. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
  380. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  381. if (ACPI_SUCCESS(status)) {
  382. sleep_states[i] = 1;
  383. printk(" S%d", i);
  384. }
  385. }
  386. suspend_set_ops(&acpi_suspend_ops);
  387. #endif
  388. #ifdef CONFIG_HIBERNATION
  389. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  390. if (ACPI_SUCCESS(status)) {
  391. hibernation_set_ops(&acpi_hibernation_ops);
  392. sleep_states[ACPI_STATE_S4] = 1;
  393. printk(" S4");
  394. }
  395. #endif
  396. status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
  397. if (ACPI_SUCCESS(status)) {
  398. sleep_states[ACPI_STATE_S5] = 1;
  399. printk(" S5");
  400. pm_power_off_prepare = acpi_power_off_prepare;
  401. pm_power_off = acpi_power_off;
  402. }
  403. printk(")\n");
  404. return 0;
  405. }