main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /**
  31. * acpi_pm_prepare - Do preliminary suspend work.
  32. * @pm_state: suspend state we're entering.
  33. *
  34. * Make sure we support the state. If we do, and we need it, set the
  35. * firmware waking vector and do arch-specific nastiness to get the
  36. * wakeup code to the waking vector.
  37. */
  38. extern int acpi_sleep_prepare(u32 acpi_state);
  39. extern void acpi_power_off(void);
  40. static int acpi_pm_prepare(suspend_state_t pm_state)
  41. {
  42. u32 acpi_state = acpi_suspend_states[pm_state];
  43. if (!sleep_states[acpi_state]) {
  44. printk("acpi_pm_prepare does not support %d \n", pm_state);
  45. return -EPERM;
  46. }
  47. return acpi_sleep_prepare(acpi_state);
  48. }
  49. /**
  50. * acpi_pm_enter - Actually enter a sleep state.
  51. * @pm_state: State we're entering.
  52. *
  53. * Flush caches and go to sleep. For STR or STD, we have to call
  54. * arch-specific assembly, which in turn call acpi_enter_sleep_state().
  55. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  56. */
  57. static int acpi_pm_enter(suspend_state_t pm_state)
  58. {
  59. acpi_status status = AE_OK;
  60. unsigned long flags = 0;
  61. u32 acpi_state = acpi_suspend_states[pm_state];
  62. ACPI_FLUSH_CPU_CACHE();
  63. /* Do arch specific saving of state. */
  64. if (pm_state > PM_SUSPEND_STANDBY) {
  65. int error = acpi_save_state_mem();
  66. if (error)
  67. return error;
  68. }
  69. local_irq_save(flags);
  70. acpi_enable_wakeup_device(acpi_state);
  71. switch (pm_state) {
  72. case PM_SUSPEND_STANDBY:
  73. barrier();
  74. status = acpi_enter_sleep_state(acpi_state);
  75. break;
  76. case PM_SUSPEND_MEM:
  77. do_suspend_lowlevel();
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. /* ACPI 3.0 specs (P62) says that it's the responsabilty
  83. * of the OSPM to clear the status bit [ implying that the
  84. * POWER_BUTTON event should not reach userspace ]
  85. */
  86. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
  87. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  88. local_irq_restore(flags);
  89. printk(KERN_DEBUG "Back to C!\n");
  90. /* restore processor state
  91. * We should only be here if we're coming back from STR or STD.
  92. * And, in the case of the latter, the memory image should have already
  93. * been loaded from disk.
  94. */
  95. if (pm_state > PM_SUSPEND_STANDBY)
  96. acpi_restore_state_mem();
  97. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  98. }
  99. /**
  100. * acpi_pm_finish - Finish up suspend sequence.
  101. * @pm_state: State we're coming out of.
  102. *
  103. * This is called after we wake back up (or if entering the sleep state
  104. * failed).
  105. */
  106. static int acpi_pm_finish(suspend_state_t pm_state)
  107. {
  108. u32 acpi_state = acpi_suspend_states[pm_state];
  109. acpi_leave_sleep_state(acpi_state);
  110. acpi_disable_wakeup_device(acpi_state);
  111. /* reset firmware waking vector */
  112. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  113. if (init_8259A_after_S1) {
  114. printk("Broken toshiba laptop -> kicking interrupts\n");
  115. init_8259A(0);
  116. }
  117. return 0;
  118. }
  119. int acpi_suspend(u32 acpi_state)
  120. {
  121. suspend_state_t states[] = {
  122. [1] = PM_SUSPEND_STANDBY,
  123. [3] = PM_SUSPEND_MEM,
  124. [5] = PM_SUSPEND_MAX
  125. };
  126. if (acpi_state < 6 && states[acpi_state])
  127. return pm_suspend(states[acpi_state]);
  128. if (acpi_state == 4)
  129. return hibernate();
  130. return -EINVAL;
  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. .prepare = acpi_pm_prepare,
  148. .enter = acpi_pm_enter,
  149. .finish = acpi_pm_finish,
  150. };
  151. #ifdef CONFIG_SOFTWARE_SUSPEND
  152. static int acpi_hibernation_prepare(void)
  153. {
  154. return acpi_sleep_prepare(ACPI_STATE_S4);
  155. }
  156. static int acpi_hibernation_enter(void)
  157. {
  158. acpi_status status = AE_OK;
  159. unsigned long flags = 0;
  160. ACPI_FLUSH_CPU_CACHE();
  161. local_irq_save(flags);
  162. acpi_enable_wakeup_device(ACPI_STATE_S4);
  163. /* This shouldn't return. If it returns, we have a problem */
  164. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  165. local_irq_restore(flags);
  166. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  167. }
  168. static void acpi_hibernation_finish(void)
  169. {
  170. acpi_leave_sleep_state(ACPI_STATE_S4);
  171. acpi_disable_wakeup_device(ACPI_STATE_S4);
  172. /* reset firmware waking vector */
  173. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  174. if (init_8259A_after_S1) {
  175. printk("Broken toshiba laptop -> kicking interrupts\n");
  176. init_8259A(0);
  177. }
  178. }
  179. static struct hibernation_ops acpi_hibernation_ops = {
  180. .prepare = acpi_hibernation_prepare,
  181. .enter = acpi_hibernation_enter,
  182. .finish = acpi_hibernation_finish,
  183. };
  184. #endif /* CONFIG_SOFTWARE_SUSPEND */
  185. /*
  186. * Toshiba fails to preserve interrupts over S1, reinitialization
  187. * of 8259 is needed after S1 resume.
  188. */
  189. static int __init init_ints_after_s1(struct dmi_system_id *d)
  190. {
  191. printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
  192. init_8259A_after_S1 = 1;
  193. return 0;
  194. }
  195. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  196. {
  197. .callback = init_ints_after_s1,
  198. .ident = "Toshiba Satellite 4030cdt",
  199. .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
  200. },
  201. {},
  202. };
  203. int __init acpi_sleep_init(void)
  204. {
  205. int i = 0;
  206. dmi_check_system(acpisleep_dmi_table);
  207. if (acpi_disabled)
  208. return 0;
  209. printk(KERN_INFO PREFIX "(supports");
  210. for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
  211. acpi_status status;
  212. u8 type_a, type_b;
  213. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  214. if (ACPI_SUCCESS(status)) {
  215. sleep_states[i] = 1;
  216. printk(" S%d", i);
  217. }
  218. }
  219. printk(")\n");
  220. pm_set_ops(&acpi_pm_ops);
  221. #ifdef CONFIG_SOFTWARE_SUSPEND
  222. if (sleep_states[ACPI_STATE_S4])
  223. hibernation_set_ops(&acpi_hibernation_ops);
  224. #else
  225. sleep_states[ACPI_STATE_S4] = 0;
  226. #endif
  227. return 0;
  228. }