main.c 14 KB

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