main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. local_irq_restore(flags);
  91. printk(KERN_DEBUG "Back to C!\n");
  92. /* restore processor state
  93. * We should only be here if we're coming back from STR or STD.
  94. * And, in the case of the latter, the memory image should have already
  95. * been loaded from disk.
  96. */
  97. if (pm_state > PM_SUSPEND_STANDBY)
  98. acpi_restore_state_mem();
  99. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  100. }
  101. /**
  102. * acpi_pm_finish - Finish up suspend sequence.
  103. * @pm_state: State we're coming out of.
  104. *
  105. * This is called after we wake back up (or if entering the sleep state
  106. * failed).
  107. */
  108. static int acpi_pm_finish(suspend_state_t pm_state)
  109. {
  110. u32 acpi_state = acpi_suspend_states[pm_state];
  111. acpi_leave_sleep_state(acpi_state);
  112. acpi_disable_wakeup_device(acpi_state);
  113. /* reset firmware waking vector */
  114. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  115. if (init_8259A_after_S1) {
  116. printk("Broken toshiba laptop -> kicking interrupts\n");
  117. init_8259A(0);
  118. }
  119. return 0;
  120. }
  121. int acpi_suspend(u32 acpi_state)
  122. {
  123. suspend_state_t states[] = {
  124. [1] = PM_SUSPEND_STANDBY,
  125. [3] = PM_SUSPEND_MEM,
  126. [4] = PM_SUSPEND_DISK,
  127. [5] = PM_SUSPEND_MAX
  128. };
  129. if (acpi_state < 6 && states[acpi_state])
  130. return pm_suspend(states[acpi_state]);
  131. return -EINVAL;
  132. }
  133. static struct pm_ops acpi_pm_ops = {
  134. .prepare = acpi_pm_prepare,
  135. .enter = acpi_pm_enter,
  136. .finish = acpi_pm_finish,
  137. };
  138. /*
  139. * Toshiba fails to preserve interrupts over S1, reinitialization
  140. * of 8259 is needed after S1 resume.
  141. */
  142. static int __init init_ints_after_s1(struct dmi_system_id *d)
  143. {
  144. printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
  145. init_8259A_after_S1 = 1;
  146. return 0;
  147. }
  148. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  149. {
  150. .callback = init_ints_after_s1,
  151. .ident = "Toshiba Satellite 4030cdt",
  152. .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
  153. },
  154. {},
  155. };
  156. static int __init acpi_sleep_init(void)
  157. {
  158. int i = 0;
  159. dmi_check_system(acpisleep_dmi_table);
  160. if (acpi_disabled)
  161. return 0;
  162. printk(KERN_INFO PREFIX "(supports");
  163. for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
  164. acpi_status status;
  165. u8 type_a, type_b;
  166. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  167. if (ACPI_SUCCESS(status)) {
  168. sleep_states[i] = 1;
  169. printk(" S%d", i);
  170. }
  171. if (i == ACPI_STATE_S4) {
  172. if (sleep_states[i])
  173. acpi_pm_ops.pm_disk_mode = PM_DISK_PLATFORM;
  174. }
  175. }
  176. printk(")\n");
  177. pm_set_ops(&acpi_pm_ops);
  178. return 0;
  179. }
  180. late_initcall(acpi_sleep_init);