main.c 14 KB

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