main.c 9.8 KB

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