main.c 14 KB

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