main.c 12 KB

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