main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * sleep.c - ACPI sleep support.
  3. *
  4. * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
  5. * Copyright (c) 2000-2003 Patrick Mochel
  6. * Copyright (c) 2003 Open Source Development Lab
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/irq.h>
  13. #include <linux/dmi.h>
  14. #include <linux/device.h>
  15. #include <linux/suspend.h>
  16. #include <asm/io.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. };
  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. static int acpi_pm_prepare(suspend_state_t pm_state)
  40. {
  41. u32 acpi_state = acpi_suspend_states[pm_state];
  42. if (!sleep_states[acpi_state])
  43. return -EPERM;
  44. /* do we have a wakeup address for S2 and S3? */
  45. /* Here, we support only S4BIOS, those we set the wakeup address */
  46. /* S4OS is only supported for now via swsusp.. */
  47. if (pm_state == PM_SUSPEND_MEM || pm_state == PM_SUSPEND_DISK) {
  48. if (!acpi_wakeup_address)
  49. return -EFAULT;
  50. acpi_set_firmware_waking_vector(
  51. (acpi_physical_address) virt_to_phys(
  52. (void *)acpi_wakeup_address));
  53. }
  54. ACPI_FLUSH_CPU_CACHE();
  55. acpi_enable_wakeup_device_prep(acpi_state);
  56. acpi_enter_sleep_state_prep(acpi_state);
  57. return 0;
  58. }
  59. /**
  60. * acpi_pm_enter - Actually enter a sleep state.
  61. * @pm_state: State we're entering.
  62. *
  63. * Flush caches and go to sleep. For STR or STD, we have to call
  64. * arch-specific assembly, which in turn call acpi_enter_sleep_state().
  65. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  66. */
  67. static int acpi_pm_enter(suspend_state_t pm_state)
  68. {
  69. acpi_status status = AE_OK;
  70. unsigned long flags = 0;
  71. u32 acpi_state = acpi_suspend_states[pm_state];
  72. ACPI_FLUSH_CPU_CACHE();
  73. /* Do arch specific saving of state. */
  74. if (pm_state > PM_SUSPEND_STANDBY) {
  75. int error = acpi_save_state_mem();
  76. if (error)
  77. return error;
  78. }
  79. local_irq_save(flags);
  80. acpi_enable_wakeup_device(acpi_state);
  81. switch (pm_state)
  82. {
  83. case PM_SUSPEND_STANDBY:
  84. barrier();
  85. status = acpi_enter_sleep_state(acpi_state);
  86. break;
  87. case PM_SUSPEND_MEM:
  88. do_suspend_lowlevel();
  89. break;
  90. case PM_SUSPEND_DISK:
  91. if (acpi_pm_ops.pm_disk_mode == PM_DISK_PLATFORM)
  92. status = acpi_enter_sleep_state(acpi_state);
  93. else
  94. do_suspend_lowlevel_s4bios();
  95. break;
  96. default:
  97. return -EINVAL;
  98. }
  99. local_irq_restore(flags);
  100. printk(KERN_DEBUG "Back to C!\n");
  101. /* restore processor state
  102. * We should only be here if we're coming back from STR or STD.
  103. * And, in the case of the latter, the memory image should have already
  104. * been loaded from disk.
  105. */
  106. if (pm_state > PM_SUSPEND_STANDBY)
  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: State we're coming out of.
  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_suspend_states[pm_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. if (init_8259A_after_S1) {
  125. printk("Broken toshiba laptop -> kicking interrupts\n");
  126. init_8259A(0);
  127. }
  128. return 0;
  129. }
  130. int acpi_suspend(u32 acpi_state)
  131. {
  132. suspend_state_t states[] = {
  133. [1] = PM_SUSPEND_STANDBY,
  134. [3] = PM_SUSPEND_MEM,
  135. [4] = PM_SUSPEND_DISK,
  136. };
  137. if (acpi_state <= 4 && states[acpi_state])
  138. return pm_suspend(states[acpi_state]);
  139. return -EINVAL;
  140. }
  141. static struct pm_ops acpi_pm_ops = {
  142. .prepare = acpi_pm_prepare,
  143. .enter = acpi_pm_enter,
  144. .finish = acpi_pm_finish,
  145. };
  146. /*
  147. * Toshiba fails to preserve interrupts over S1, reinitialization
  148. * of 8259 is needed after S1 resume.
  149. */
  150. static int __init init_ints_after_s1(struct dmi_system_id *d)
  151. {
  152. printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
  153. init_8259A_after_S1 = 1;
  154. return 0;
  155. }
  156. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  157. {
  158. .callback = init_ints_after_s1,
  159. .ident = "Toshiba Satellite 4030cdt",
  160. .matches = { DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"), },
  161. },
  162. { },
  163. };
  164. static int __init acpi_sleep_init(void)
  165. {
  166. int i = 0;
  167. dmi_check_system(acpisleep_dmi_table);
  168. if (acpi_disabled)
  169. return 0;
  170. printk(KERN_INFO PREFIX "(supports");
  171. for (i=0; i < ACPI_S_STATE_COUNT; i++) {
  172. acpi_status status;
  173. u8 type_a, type_b;
  174. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  175. if (ACPI_SUCCESS(status)) {
  176. sleep_states[i] = 1;
  177. printk(" S%d", i);
  178. }
  179. if (i == ACPI_STATE_S4) {
  180. if (acpi_gbl_FACS->S4bios_f) {
  181. sleep_states[i] = 1;
  182. printk(" S4bios");
  183. acpi_pm_ops.pm_disk_mode = PM_DISK_FIRMWARE;
  184. }
  185. if (sleep_states[i])
  186. acpi_pm_ops.pm_disk_mode = PM_DISK_PLATFORM;
  187. }
  188. }
  189. printk(")\n");
  190. pm_set_ops(&acpi_pm_ops);
  191. return 0;
  192. }
  193. late_initcall(acpi_sleep_init);