main.c 5.2 KB

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