main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. 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((acpi_physical_address)
  34. virt_to_phys((void *)
  35. acpi_wakeup_address));
  36. }
  37. ACPI_FLUSH_CPU_CACHE();
  38. acpi_enable_wakeup_device_prep(acpi_state);
  39. #endif
  40. acpi_enter_sleep_state_prep(acpi_state);
  41. return 0;
  42. }
  43. #ifdef CONFIG_SUSPEND
  44. static struct platform_suspend_ops acpi_pm_ops;
  45. extern void do_suspend_lowlevel(void);
  46. static u32 acpi_suspend_states[] = {
  47. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  48. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  49. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  50. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  51. };
  52. static int init_8259A_after_S1;
  53. /**
  54. * acpi_pm_set_target - Set the target system sleep state to the state
  55. * associated with given @pm_state, if supported.
  56. */
  57. static int acpi_pm_set_target(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_pm_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_pm_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. /**
  84. * acpi_pm_enter - Actually enter a sleep state.
  85. * @pm_state: ignored
  86. *
  87. * Flush caches and go to sleep. For STR we have to call arch-specific
  88. * assembly, which in turn call acpi_enter_sleep_state().
  89. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  90. */
  91. static int acpi_pm_enter(suspend_state_t pm_state)
  92. {
  93. acpi_status status = AE_OK;
  94. unsigned long flags = 0;
  95. u32 acpi_state = acpi_target_sleep_state;
  96. ACPI_FLUSH_CPU_CACHE();
  97. /* Do arch specific saving of state. */
  98. if (acpi_state == ACPI_STATE_S3) {
  99. int error = acpi_save_state_mem();
  100. if (error) {
  101. acpi_target_sleep_state = ACPI_STATE_S0;
  102. return error;
  103. }
  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. /* ACPI 3.0 specs (P62) says that it's the responsabilty
  117. * of the OSPM to clear the status bit [ implying that the
  118. * POWER_BUTTON event should not reach userspace ]
  119. */
  120. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
  121. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  122. local_irq_restore(flags);
  123. printk(KERN_DEBUG "Back to C!\n");
  124. /* restore processor state */
  125. if (acpi_state == ACPI_STATE_S3)
  126. acpi_restore_state_mem();
  127. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  128. }
  129. /**
  130. * acpi_pm_finish - Finish up suspend sequence.
  131. *
  132. * This is called after we wake back up (or if entering the sleep state
  133. * failed).
  134. */
  135. static void acpi_pm_finish(void)
  136. {
  137. u32 acpi_state = acpi_target_sleep_state;
  138. acpi_leave_sleep_state(acpi_state);
  139. acpi_disable_wakeup_device(acpi_state);
  140. /* reset firmware waking vector */
  141. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  142. acpi_target_sleep_state = ACPI_STATE_S0;
  143. #ifdef CONFIG_X86
  144. if (init_8259A_after_S1) {
  145. printk("Broken toshiba laptop -> kicking interrupts\n");
  146. init_8259A(0);
  147. }
  148. #endif
  149. }
  150. static int acpi_pm_state_valid(suspend_state_t pm_state)
  151. {
  152. u32 acpi_state;
  153. switch (pm_state) {
  154. case PM_SUSPEND_ON:
  155. case PM_SUSPEND_STANDBY:
  156. case PM_SUSPEND_MEM:
  157. acpi_state = acpi_suspend_states[pm_state];
  158. return sleep_states[acpi_state];
  159. default:
  160. return 0;
  161. }
  162. }
  163. static struct platform_suspend_ops acpi_pm_ops = {
  164. .valid = acpi_pm_state_valid,
  165. .set_target = acpi_pm_set_target,
  166. .prepare = acpi_pm_prepare,
  167. .enter = acpi_pm_enter,
  168. .finish = acpi_pm_finish,
  169. };
  170. /*
  171. * Toshiba fails to preserve interrupts over S1, reinitialization
  172. * of 8259 is needed after S1 resume.
  173. */
  174. static int __init init_ints_after_s1(const struct dmi_system_id *d)
  175. {
  176. printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
  177. init_8259A_after_S1 = 1;
  178. return 0;
  179. }
  180. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  181. {
  182. .callback = init_ints_after_s1,
  183. .ident = "Toshiba Satellite 4030cdt",
  184. .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
  185. },
  186. {},
  187. };
  188. #endif /* CONFIG_SUSPEND */
  189. #ifdef CONFIG_HIBERNATION
  190. static int acpi_hibernation_start(void)
  191. {
  192. acpi_target_sleep_state = ACPI_STATE_S4;
  193. return 0;
  194. }
  195. static int acpi_hibernation_prepare(void)
  196. {
  197. return acpi_sleep_prepare(ACPI_STATE_S4);
  198. }
  199. static int acpi_hibernation_enter(void)
  200. {
  201. acpi_status status = AE_OK;
  202. unsigned long flags = 0;
  203. ACPI_FLUSH_CPU_CACHE();
  204. local_irq_save(flags);
  205. acpi_enable_wakeup_device(ACPI_STATE_S4);
  206. /* This shouldn't return. If it returns, we have a problem */
  207. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  208. local_irq_restore(flags);
  209. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  210. }
  211. static void acpi_hibernation_leave(void)
  212. {
  213. /*
  214. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  215. * enable it here.
  216. */
  217. acpi_enable();
  218. }
  219. static void acpi_hibernation_finish(void)
  220. {
  221. /*
  222. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  223. * enable it here.
  224. */
  225. acpi_enable();
  226. acpi_leave_sleep_state(ACPI_STATE_S4);
  227. acpi_disable_wakeup_device(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 int acpi_hibernation_pre_restore(void)
  233. {
  234. acpi_status status;
  235. status = acpi_hw_disable_all_gpes();
  236. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  237. }
  238. static void acpi_hibernation_restore_cleanup(void)
  239. {
  240. acpi_hw_enable_all_runtime_gpes();
  241. }
  242. static struct platform_hibernation_ops acpi_hibernation_ops = {
  243. .start = acpi_hibernation_start,
  244. .pre_snapshot = acpi_hibernation_prepare,
  245. .finish = acpi_hibernation_finish,
  246. .prepare = acpi_hibernation_prepare,
  247. .enter = acpi_hibernation_enter,
  248. .leave = acpi_hibernation_leave,
  249. .pre_restore = acpi_hibernation_pre_restore,
  250. .restore_cleanup = acpi_hibernation_restore_cleanup,
  251. };
  252. #endif /* CONFIG_HIBERNATION */
  253. int acpi_suspend(u32 acpi_state)
  254. {
  255. suspend_state_t states[] = {
  256. [1] = PM_SUSPEND_STANDBY,
  257. [3] = PM_SUSPEND_MEM,
  258. [5] = PM_SUSPEND_MAX
  259. };
  260. if (acpi_state < 6 && states[acpi_state])
  261. return pm_suspend(states[acpi_state]);
  262. if (acpi_state == 4)
  263. return hibernate();
  264. return -EINVAL;
  265. }
  266. #ifdef CONFIG_PM_SLEEP
  267. /**
  268. * acpi_pm_device_sleep_state - return preferred power state of ACPI device
  269. * in the system sleep state given by %acpi_target_sleep_state
  270. * @dev: device to examine
  271. * @wake: if set, the device should be able to wake up the system
  272. * @d_min_p: used to store the upper limit of allowed states range
  273. * Return value: preferred power state of the device on success, -ENODEV on
  274. * failure (ie. if there's no 'struct acpi_device' for @dev)
  275. *
  276. * Find the lowest power (highest number) ACPI device power state that
  277. * device @dev can be in while the system is in the sleep state represented
  278. * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
  279. * able to wake up the system from this sleep state. If @d_min_p is set,
  280. * the highest power (lowest number) device power state of @dev allowed
  281. * in this system sleep state is stored at the location pointed to by it.
  282. *
  283. * The caller must ensure that @dev is valid before using this function.
  284. * The caller is also responsible for figuring out if the device is
  285. * supposed to be able to wake up the system and passing this information
  286. * via @wake.
  287. */
  288. int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
  289. {
  290. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  291. struct acpi_device *adev;
  292. char acpi_method[] = "_SxD";
  293. unsigned long d_min, d_max;
  294. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  295. printk(KERN_DEBUG "ACPI handle has no context!\n");
  296. return -ENODEV;
  297. }
  298. acpi_method[2] = '0' + acpi_target_sleep_state;
  299. /*
  300. * If the sleep state is S0, we will return D3, but if the device has
  301. * _S0W, we will use the value from _S0W
  302. */
  303. d_min = ACPI_STATE_D0;
  304. d_max = ACPI_STATE_D3;
  305. /*
  306. * If present, _SxD methods return the minimum D-state (highest power
  307. * state) we can use for the corresponding S-states. Otherwise, the
  308. * minimum D-state is D0 (ACPI 3.x).
  309. *
  310. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  311. * provided -- that's our fault recovery, we ignore retval.
  312. */
  313. if (acpi_target_sleep_state > ACPI_STATE_S0)
  314. acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
  315. /*
  316. * If _PRW says we can wake up the system from the target sleep state,
  317. * the D-state returned by _SxD is sufficient for that (we assume a
  318. * wakeup-aware driver if wake is set). Still, if _SxW exists
  319. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  320. * can wake the system. _S0W may be valid, too.
  321. */
  322. if (acpi_target_sleep_state == ACPI_STATE_S0 ||
  323. (wake && adev->wakeup.state.enabled &&
  324. adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
  325. acpi_method[3] = 'W';
  326. acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
  327. /* Sanity check */
  328. if (d_max < d_min)
  329. d_min = d_max;
  330. }
  331. if (d_min_p)
  332. *d_min_p = d_min;
  333. return d_max;
  334. }
  335. #endif
  336. static void acpi_power_off_prepare(void)
  337. {
  338. /* Prepare to power off the system */
  339. acpi_sleep_prepare(ACPI_STATE_S5);
  340. }
  341. static void acpi_power_off(void)
  342. {
  343. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  344. printk("%s called\n", __FUNCTION__);
  345. local_irq_disable();
  346. acpi_enter_sleep_state(ACPI_STATE_S5);
  347. }
  348. int __init acpi_sleep_init(void)
  349. {
  350. acpi_status status;
  351. u8 type_a, type_b;
  352. #ifdef CONFIG_SUSPEND
  353. int i = 0;
  354. dmi_check_system(acpisleep_dmi_table);
  355. #endif
  356. if (acpi_disabled)
  357. return 0;
  358. sleep_states[ACPI_STATE_S0] = 1;
  359. printk(KERN_INFO PREFIX "(supports S0");
  360. #ifdef CONFIG_SUSPEND
  361. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
  362. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  363. if (ACPI_SUCCESS(status)) {
  364. sleep_states[i] = 1;
  365. printk(" S%d", i);
  366. }
  367. }
  368. suspend_set_ops(&acpi_pm_ops);
  369. #endif
  370. #ifdef CONFIG_HIBERNATION
  371. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  372. if (ACPI_SUCCESS(status)) {
  373. hibernation_set_ops(&acpi_hibernation_ops);
  374. sleep_states[ACPI_STATE_S4] = 1;
  375. printk(" S4");
  376. }
  377. #endif
  378. status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
  379. if (ACPI_SUCCESS(status)) {
  380. sleep_states[ACPI_STATE_S5] = 1;
  381. printk(" S5");
  382. pm_power_off_prepare = acpi_power_off_prepare;
  383. pm_power_off = acpi_power_off;
  384. }
  385. printk(")\n");
  386. return 0;
  387. }