main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <acpi/acpi_bus.h>
  18. #include <acpi/acpi_drivers.h>
  19. #include "sleep.h"
  20. u8 sleep_states[ACPI_S_STATE_COUNT];
  21. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  22. #ifdef CONFIG_SUSPEND
  23. static struct pm_ops acpi_pm_ops;
  24. extern void do_suspend_lowlevel(void);
  25. static u32 acpi_suspend_states[] = {
  26. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  27. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  28. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  29. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  30. };
  31. static int init_8259A_after_S1;
  32. /**
  33. * acpi_pm_set_target - Set the target system sleep state to the state
  34. * associated with given @pm_state, if supported.
  35. */
  36. static int acpi_pm_set_target(suspend_state_t pm_state)
  37. {
  38. u32 acpi_state = acpi_suspend_states[pm_state];
  39. int error = 0;
  40. if (sleep_states[acpi_state]) {
  41. acpi_target_sleep_state = acpi_state;
  42. } else {
  43. printk(KERN_ERR "ACPI does not support this state: %d\n",
  44. pm_state);
  45. error = -ENOSYS;
  46. }
  47. return error;
  48. }
  49. /**
  50. * acpi_pm_prepare - Do preliminary suspend work.
  51. * @pm_state: ignored
  52. *
  53. * If necessary, set the firmware waking vector and do arch-specific
  54. * nastiness to get the wakeup code to the waking vector.
  55. */
  56. static int acpi_pm_prepare(suspend_state_t pm_state)
  57. {
  58. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  59. if (error)
  60. acpi_target_sleep_state = ACPI_STATE_S0;
  61. return error;
  62. }
  63. /**
  64. * acpi_pm_enter - Actually enter a sleep state.
  65. * @pm_state: ignored
  66. *
  67. * Flush caches and go to sleep. For STR we have to call arch-specific
  68. * assembly, which in turn call acpi_enter_sleep_state().
  69. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  70. */
  71. static int acpi_pm_enter(suspend_state_t pm_state)
  72. {
  73. acpi_status status = AE_OK;
  74. unsigned long flags = 0;
  75. u32 acpi_state = acpi_target_sleep_state;
  76. ACPI_FLUSH_CPU_CACHE();
  77. /* Do arch specific saving of state. */
  78. if (acpi_state == ACPI_STATE_S3) {
  79. int error = acpi_save_state_mem();
  80. if (error) {
  81. acpi_target_sleep_state = ACPI_STATE_S0;
  82. return error;
  83. }
  84. }
  85. local_irq_save(flags);
  86. acpi_enable_wakeup_device(acpi_state);
  87. switch (acpi_state) {
  88. case ACPI_STATE_S1:
  89. barrier();
  90. status = acpi_enter_sleep_state(acpi_state);
  91. break;
  92. case ACPI_STATE_S3:
  93. do_suspend_lowlevel();
  94. break;
  95. }
  96. /* ACPI 3.0 specs (P62) says that it's the responsabilty
  97. * of the OSPM to clear the status bit [ implying that the
  98. * POWER_BUTTON event should not reach userspace ]
  99. */
  100. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
  101. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  102. local_irq_restore(flags);
  103. printk(KERN_DEBUG "Back to C!\n");
  104. /* restore processor state */
  105. if (acpi_state == ACPI_STATE_S3)
  106. acpi_restore_state_mem();
  107. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  108. }
  109. /**
  110. * acpi_pm_finish - Finish up suspend sequence.
  111. * @pm_state: ignored
  112. *
  113. * This is called after we wake back up (or if entering the sleep state
  114. * failed).
  115. */
  116. static int acpi_pm_finish(suspend_state_t pm_state)
  117. {
  118. u32 acpi_state = acpi_target_sleep_state;
  119. acpi_leave_sleep_state(acpi_state);
  120. acpi_disable_wakeup_device(acpi_state);
  121. /* reset firmware waking vector */
  122. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  123. acpi_target_sleep_state = ACPI_STATE_S0;
  124. #ifdef CONFIG_X86
  125. if (init_8259A_after_S1) {
  126. printk("Broken toshiba laptop -> kicking interrupts\n");
  127. init_8259A(0);
  128. }
  129. #endif
  130. return 0;
  131. }
  132. static int acpi_pm_state_valid(suspend_state_t pm_state)
  133. {
  134. u32 acpi_state;
  135. switch (pm_state) {
  136. case PM_SUSPEND_ON:
  137. case PM_SUSPEND_STANDBY:
  138. case PM_SUSPEND_MEM:
  139. acpi_state = acpi_suspend_states[pm_state];
  140. return sleep_states[acpi_state];
  141. default:
  142. return 0;
  143. }
  144. }
  145. static struct pm_ops acpi_pm_ops = {
  146. .valid = acpi_pm_state_valid,
  147. .set_target = acpi_pm_set_target,
  148. .prepare = acpi_pm_prepare,
  149. .enter = acpi_pm_enter,
  150. .finish = acpi_pm_finish,
  151. };
  152. /*
  153. * Toshiba fails to preserve interrupts over S1, reinitialization
  154. * of 8259 is needed after S1 resume.
  155. */
  156. static int __init init_ints_after_s1(struct dmi_system_id *d)
  157. {
  158. printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
  159. init_8259A_after_S1 = 1;
  160. return 0;
  161. }
  162. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  163. {
  164. .callback = init_ints_after_s1,
  165. .ident = "Toshiba Satellite 4030cdt",
  166. .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
  167. },
  168. {},
  169. };
  170. #endif /* CONFIG_SUSPEND */
  171. #ifdef CONFIG_HIBERNATION
  172. static int acpi_hibernation_prepare(void)
  173. {
  174. return acpi_sleep_prepare(ACPI_STATE_S4);
  175. }
  176. static int acpi_hibernation_enter(void)
  177. {
  178. acpi_status status = AE_OK;
  179. unsigned long flags = 0;
  180. ACPI_FLUSH_CPU_CACHE();
  181. local_irq_save(flags);
  182. acpi_enable_wakeup_device(ACPI_STATE_S4);
  183. /* This shouldn't return. If it returns, we have a problem */
  184. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  185. local_irq_restore(flags);
  186. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  187. }
  188. static void acpi_hibernation_finish(void)
  189. {
  190. acpi_leave_sleep_state(ACPI_STATE_S4);
  191. acpi_disable_wakeup_device(ACPI_STATE_S4);
  192. /* reset firmware waking vector */
  193. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  194. }
  195. static int acpi_hibernation_pre_restore(void)
  196. {
  197. acpi_status status;
  198. status = acpi_hw_disable_all_gpes();
  199. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  200. }
  201. static void acpi_hibernation_restore_cleanup(void)
  202. {
  203. acpi_hw_enable_all_runtime_gpes();
  204. }
  205. static struct hibernation_ops acpi_hibernation_ops = {
  206. .prepare = acpi_hibernation_prepare,
  207. .enter = acpi_hibernation_enter,
  208. .finish = acpi_hibernation_finish,
  209. .pre_restore = acpi_hibernation_pre_restore,
  210. .restore_cleanup = acpi_hibernation_restore_cleanup,
  211. };
  212. #endif /* CONFIG_HIBERNATION */
  213. int acpi_suspend(u32 acpi_state)
  214. {
  215. suspend_state_t states[] = {
  216. [1] = PM_SUSPEND_STANDBY,
  217. [3] = PM_SUSPEND_MEM,
  218. [5] = PM_SUSPEND_MAX
  219. };
  220. if (acpi_state < 6 && states[acpi_state])
  221. return pm_suspend(states[acpi_state]);
  222. if (acpi_state == 4)
  223. return hibernate();
  224. return -EINVAL;
  225. }
  226. /**
  227. * acpi_pm_device_sleep_state - return preferred power state of ACPI device
  228. * in the system sleep state given by %acpi_target_sleep_state
  229. * @dev: device to examine
  230. * @wake: if set, the device should be able to wake up the system
  231. * @d_min_p: used to store the upper limit of allowed states range
  232. * Return value: preferred power state of the device on success, -ENODEV on
  233. * failure (ie. if there's no 'struct acpi_device' for @dev)
  234. *
  235. * Find the lowest power (highest number) ACPI device power state that
  236. * device @dev can be in while the system is in the sleep state represented
  237. * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
  238. * able to wake up the system from this sleep state. If @d_min_p is set,
  239. * the highest power (lowest number) device power state of @dev allowed
  240. * in this system sleep state is stored at the location pointed to by it.
  241. *
  242. * The caller must ensure that @dev is valid before using this function.
  243. * The caller is also responsible for figuring out if the device is
  244. * supposed to be able to wake up the system and passing this information
  245. * via @wake.
  246. */
  247. int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
  248. {
  249. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  250. struct acpi_device *adev;
  251. char acpi_method[] = "_SxD";
  252. unsigned long d_min, d_max;
  253. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  254. printk(KERN_ERR "ACPI handle has no context!\n");
  255. return -ENODEV;
  256. }
  257. acpi_method[2] = '0' + acpi_target_sleep_state;
  258. /*
  259. * If the sleep state is S0, we will return D3, but if the device has
  260. * _S0W, we will use the value from _S0W
  261. */
  262. d_min = ACPI_STATE_D0;
  263. d_max = ACPI_STATE_D3;
  264. /*
  265. * If present, _SxD methods return the minimum D-state (highest power
  266. * state) we can use for the corresponding S-states. Otherwise, the
  267. * minimum D-state is D0 (ACPI 3.x).
  268. *
  269. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  270. * provided -- that's our fault recovery, we ignore retval.
  271. */
  272. if (acpi_target_sleep_state > ACPI_STATE_S0)
  273. acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
  274. /*
  275. * If _PRW says we can wake up the system from the target sleep state,
  276. * the D-state returned by _SxD is sufficient for that (we assume a
  277. * wakeup-aware driver if wake is set). Still, if _SxW exists
  278. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  279. * can wake the system. _S0W may be valid, too.
  280. */
  281. if (acpi_target_sleep_state == ACPI_STATE_S0 ||
  282. (wake && adev->wakeup.state.enabled &&
  283. adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
  284. acpi_method[3] = 'W';
  285. acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
  286. /* Sanity check */
  287. if (d_max < d_min)
  288. d_min = d_max;
  289. }
  290. if (d_min_p)
  291. *d_min_p = d_min;
  292. return d_max;
  293. }
  294. int __init acpi_sleep_init(void)
  295. {
  296. acpi_status status;
  297. u8 type_a, type_b;
  298. #ifdef CONFIG_SUSPEND
  299. int i = 0;
  300. dmi_check_system(acpisleep_dmi_table);
  301. #endif
  302. if (acpi_disabled)
  303. return 0;
  304. #ifdef CONFIG_SUSPEND
  305. printk(KERN_INFO PREFIX "(supports");
  306. for (i = ACPI_STATE_S0; i < ACPI_STATE_S4; i++) {
  307. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  308. if (ACPI_SUCCESS(status)) {
  309. sleep_states[i] = 1;
  310. printk(" S%d", i);
  311. }
  312. }
  313. printk(")\n");
  314. pm_set_ops(&acpi_pm_ops);
  315. #endif
  316. #ifdef CONFIG_HIBERNATION
  317. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  318. if (ACPI_SUCCESS(status)) {
  319. hibernation_set_ops(&acpi_hibernation_ops);
  320. sleep_states[ACPI_STATE_S4] = 1;
  321. }
  322. #else
  323. sleep_states[ACPI_STATE_S4] = 0;
  324. #endif
  325. return 0;
  326. }