main.c 9.6 KB

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