main.c 5.4 KB

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