main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 <asm/io.h>
  18. #include <acpi/acpi_bus.h>
  19. #include <acpi/acpi_drivers.h>
  20. #include "sleep.h"
  21. u8 sleep_states[ACPI_S_STATE_COUNT];
  22. #ifdef CONFIG_PM_SLEEP
  23. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  24. #endif
  25. int acpi_sleep_prepare(u32 acpi_state)
  26. {
  27. #ifdef CONFIG_ACPI_SLEEP
  28. /* do we have a wakeup address for S2 and S3? */
  29. if (acpi_state == ACPI_STATE_S3) {
  30. if (!acpi_wakeup_address) {
  31. return -EFAULT;
  32. }
  33. acpi_set_firmware_waking_vector((acpi_physical_address)
  34. virt_to_phys((void *)
  35. acpi_wakeup_address));
  36. }
  37. ACPI_FLUSH_CPU_CACHE();
  38. acpi_enable_wakeup_device_prep(acpi_state);
  39. #endif
  40. acpi_gpe_sleep_prepare(acpi_state);
  41. acpi_enter_sleep_state_prep(acpi_state);
  42. return 0;
  43. }
  44. #ifdef CONFIG_SUSPEND
  45. static struct platform_suspend_ops acpi_pm_ops;
  46. extern void do_suspend_lowlevel(void);
  47. static u32 acpi_suspend_states[] = {
  48. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  49. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  50. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  51. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  52. };
  53. static int init_8259A_after_S1;
  54. /**
  55. * acpi_pm_set_target - Set the target system sleep state to the state
  56. * associated with given @pm_state, if supported.
  57. */
  58. static int acpi_pm_set_target(suspend_state_t pm_state)
  59. {
  60. u32 acpi_state = acpi_suspend_states[pm_state];
  61. int error = 0;
  62. if (sleep_states[acpi_state]) {
  63. acpi_target_sleep_state = acpi_state;
  64. } else {
  65. printk(KERN_ERR "ACPI does not support this state: %d\n",
  66. pm_state);
  67. error = -ENOSYS;
  68. }
  69. return error;
  70. }
  71. /**
  72. * acpi_pm_prepare - Do preliminary suspend work.
  73. *
  74. * If necessary, set the firmware waking vector and do arch-specific
  75. * nastiness to get the wakeup code to the waking vector.
  76. */
  77. static int acpi_pm_prepare(void)
  78. {
  79. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  80. if (error)
  81. acpi_target_sleep_state = ACPI_STATE_S0;
  82. return error;
  83. }
  84. /**
  85. * acpi_pm_enter - Actually enter a sleep state.
  86. * @pm_state: ignored
  87. *
  88. * Flush caches and go to sleep. For STR we have to call arch-specific
  89. * assembly, which in turn call acpi_enter_sleep_state().
  90. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  91. */
  92. static int acpi_pm_enter(suspend_state_t pm_state)
  93. {
  94. acpi_status status = AE_OK;
  95. unsigned long flags = 0;
  96. u32 acpi_state = acpi_target_sleep_state;
  97. ACPI_FLUSH_CPU_CACHE();
  98. /* Do arch specific saving of state. */
  99. if (acpi_state == ACPI_STATE_S3) {
  100. int error = acpi_save_state_mem();
  101. if (error) {
  102. acpi_target_sleep_state = ACPI_STATE_S0;
  103. return error;
  104. }
  105. }
  106. local_irq_save(flags);
  107. acpi_enable_wakeup_device(acpi_state);
  108. switch (acpi_state) {
  109. case ACPI_STATE_S1:
  110. barrier();
  111. status = acpi_enter_sleep_state(acpi_state);
  112. break;
  113. case ACPI_STATE_S3:
  114. do_suspend_lowlevel();
  115. break;
  116. }
  117. /* ACPI 3.0 specs (P62) says that it's the responsabilty
  118. * of the OSPM to clear the status bit [ implying that the
  119. * POWER_BUTTON event should not reach userspace ]
  120. */
  121. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
  122. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  123. local_irq_restore(flags);
  124. printk(KERN_DEBUG "Back to C!\n");
  125. /* restore processor state */
  126. if (acpi_state == ACPI_STATE_S3)
  127. acpi_restore_state_mem();
  128. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  129. }
  130. /**
  131. * acpi_pm_finish - Finish up suspend sequence.
  132. *
  133. * This is called after we wake back up (or if entering the sleep state
  134. * failed).
  135. */
  136. static void acpi_pm_finish(void)
  137. {
  138. u32 acpi_state = acpi_target_sleep_state;
  139. acpi_leave_sleep_state(acpi_state);
  140. acpi_disable_wakeup_device(acpi_state);
  141. /* reset firmware waking vector */
  142. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  143. acpi_target_sleep_state = ACPI_STATE_S0;
  144. #ifdef CONFIG_X86
  145. if (init_8259A_after_S1) {
  146. printk("Broken toshiba laptop -> kicking interrupts\n");
  147. init_8259A(0);
  148. }
  149. #endif
  150. }
  151. static int acpi_pm_state_valid(suspend_state_t pm_state)
  152. {
  153. u32 acpi_state;
  154. switch (pm_state) {
  155. case PM_SUSPEND_ON:
  156. case PM_SUSPEND_STANDBY:
  157. case PM_SUSPEND_MEM:
  158. acpi_state = acpi_suspend_states[pm_state];
  159. return sleep_states[acpi_state];
  160. default:
  161. return 0;
  162. }
  163. }
  164. static struct platform_suspend_ops acpi_pm_ops = {
  165. .valid = acpi_pm_state_valid,
  166. .set_target = acpi_pm_set_target,
  167. .prepare = acpi_pm_prepare,
  168. .enter = acpi_pm_enter,
  169. .finish = acpi_pm_finish,
  170. };
  171. /*
  172. * Toshiba fails to preserve interrupts over S1, reinitialization
  173. * of 8259 is needed after S1 resume.
  174. */
  175. static int __init init_ints_after_s1(const struct dmi_system_id *d)
  176. {
  177. printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident);
  178. init_8259A_after_S1 = 1;
  179. return 0;
  180. }
  181. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  182. {
  183. .callback = init_ints_after_s1,
  184. .ident = "Toshiba Satellite 4030cdt",
  185. .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),},
  186. },
  187. {},
  188. };
  189. #endif /* CONFIG_SUSPEND */
  190. #ifdef CONFIG_HIBERNATION
  191. static int acpi_hibernation_start(void)
  192. {
  193. acpi_target_sleep_state = ACPI_STATE_S4;
  194. return 0;
  195. }
  196. static int acpi_hibernation_prepare(void)
  197. {
  198. return acpi_sleep_prepare(ACPI_STATE_S4);
  199. }
  200. static int acpi_hibernation_enter(void)
  201. {
  202. acpi_status status = AE_OK;
  203. unsigned long flags = 0;
  204. ACPI_FLUSH_CPU_CACHE();
  205. local_irq_save(flags);
  206. acpi_enable_wakeup_device(ACPI_STATE_S4);
  207. /* This shouldn't return. If it returns, we have a problem */
  208. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  209. local_irq_restore(flags);
  210. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  211. }
  212. static void acpi_hibernation_leave(void)
  213. {
  214. /*
  215. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  216. * enable it here.
  217. */
  218. acpi_enable();
  219. }
  220. static void acpi_hibernation_finish(void)
  221. {
  222. acpi_leave_sleep_state(ACPI_STATE_S4);
  223. acpi_disable_wakeup_device(ACPI_STATE_S4);
  224. /* reset firmware waking vector */
  225. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  226. acpi_target_sleep_state = ACPI_STATE_S0;
  227. }
  228. static int acpi_hibernation_pre_restore(void)
  229. {
  230. acpi_status status;
  231. status = acpi_hw_disable_all_gpes();
  232. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  233. }
  234. static void acpi_hibernation_restore_cleanup(void)
  235. {
  236. acpi_hw_enable_all_runtime_gpes();
  237. }
  238. static struct platform_hibernation_ops acpi_hibernation_ops = {
  239. .start = acpi_hibernation_start,
  240. .pre_snapshot = acpi_hibernation_prepare,
  241. .finish = acpi_hibernation_finish,
  242. .prepare = acpi_hibernation_prepare,
  243. .enter = acpi_hibernation_enter,
  244. .leave = acpi_hibernation_leave,
  245. .pre_restore = acpi_hibernation_pre_restore,
  246. .restore_cleanup = acpi_hibernation_restore_cleanup,
  247. };
  248. #endif /* CONFIG_HIBERNATION */
  249. int acpi_suspend(u32 acpi_state)
  250. {
  251. suspend_state_t states[] = {
  252. [1] = PM_SUSPEND_STANDBY,
  253. [3] = PM_SUSPEND_MEM,
  254. [5] = PM_SUSPEND_MAX
  255. };
  256. if (acpi_state < 6 && states[acpi_state])
  257. return pm_suspend(states[acpi_state]);
  258. if (acpi_state == 4)
  259. return hibernate();
  260. return -EINVAL;
  261. }
  262. #ifdef CONFIG_PM_SLEEP
  263. /**
  264. * acpi_pm_device_sleep_state - return preferred power state of ACPI device
  265. * in the system sleep state given by %acpi_target_sleep_state
  266. * @dev: device to examine
  267. * @wake: if set, the device should be able to wake up the system
  268. * @d_min_p: used to store the upper limit of allowed states range
  269. * Return value: preferred power state of the device on success, -ENODEV on
  270. * failure (ie. if there's no 'struct acpi_device' for @dev)
  271. *
  272. * Find the lowest power (highest number) ACPI device power state that
  273. * device @dev can be in while the system is in the sleep state represented
  274. * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
  275. * able to wake up the system from this sleep state. If @d_min_p is set,
  276. * the highest power (lowest number) device power state of @dev allowed
  277. * in this system sleep state is stored at the location pointed to by it.
  278. *
  279. * The caller must ensure that @dev is valid before using this function.
  280. * The caller is also responsible for figuring out if the device is
  281. * supposed to be able to wake up the system and passing this information
  282. * via @wake.
  283. */
  284. int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
  285. {
  286. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  287. struct acpi_device *adev;
  288. char acpi_method[] = "_SxD";
  289. unsigned long d_min, d_max;
  290. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  291. printk(KERN_DEBUG "ACPI handle has no context!\n");
  292. return -ENODEV;
  293. }
  294. acpi_method[2] = '0' + acpi_target_sleep_state;
  295. /*
  296. * If the sleep state is S0, we will return D3, but if the device has
  297. * _S0W, we will use the value from _S0W
  298. */
  299. d_min = ACPI_STATE_D0;
  300. d_max = ACPI_STATE_D3;
  301. /*
  302. * If present, _SxD methods return the minimum D-state (highest power
  303. * state) we can use for the corresponding S-states. Otherwise, the
  304. * minimum D-state is D0 (ACPI 3.x).
  305. *
  306. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  307. * provided -- that's our fault recovery, we ignore retval.
  308. */
  309. if (acpi_target_sleep_state > ACPI_STATE_S0)
  310. acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
  311. /*
  312. * If _PRW says we can wake up the system from the target sleep state,
  313. * the D-state returned by _SxD is sufficient for that (we assume a
  314. * wakeup-aware driver if wake is set). Still, if _SxW exists
  315. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  316. * can wake the system. _S0W may be valid, too.
  317. */
  318. if (acpi_target_sleep_state == ACPI_STATE_S0 ||
  319. (wake && adev->wakeup.state.enabled &&
  320. adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
  321. acpi_method[3] = 'W';
  322. acpi_evaluate_integer(handle, acpi_method, NULL, &d_max);
  323. /* Sanity check */
  324. if (d_max < d_min)
  325. d_min = d_max;
  326. }
  327. if (d_min_p)
  328. *d_min_p = d_min;
  329. return d_max;
  330. }
  331. #endif
  332. static void acpi_power_off_prepare(void)
  333. {
  334. /* Prepare to power off the system */
  335. acpi_sleep_prepare(ACPI_STATE_S5);
  336. }
  337. static void acpi_power_off(void)
  338. {
  339. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  340. printk("%s called\n", __FUNCTION__);
  341. local_irq_disable();
  342. acpi_enter_sleep_state(ACPI_STATE_S5);
  343. }
  344. int __init acpi_sleep_init(void)
  345. {
  346. acpi_status status;
  347. u8 type_a, type_b;
  348. #ifdef CONFIG_SUSPEND
  349. int i = 0;
  350. dmi_check_system(acpisleep_dmi_table);
  351. #endif
  352. if (acpi_disabled)
  353. return 0;
  354. sleep_states[ACPI_STATE_S0] = 1;
  355. printk(KERN_INFO PREFIX "(supports S0");
  356. #ifdef CONFIG_SUSPEND
  357. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
  358. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  359. if (ACPI_SUCCESS(status)) {
  360. sleep_states[i] = 1;
  361. printk(" S%d", i);
  362. }
  363. }
  364. suspend_set_ops(&acpi_pm_ops);
  365. #endif
  366. #ifdef CONFIG_HIBERNATION
  367. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  368. if (ACPI_SUCCESS(status)) {
  369. hibernation_set_ops(&acpi_hibernation_ops);
  370. sleep_states[ACPI_STATE_S4] = 1;
  371. printk(" S4");
  372. }
  373. #endif
  374. status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
  375. if (ACPI_SUCCESS(status)) {
  376. sleep_states[ACPI_STATE_S5] = 1;
  377. printk(" S5");
  378. pm_power_off_prepare = acpi_power_off_prepare;
  379. pm_power_off = acpi_power_off;
  380. }
  381. printk(")\n");
  382. return 0;
  383. }