main.c 13 KB

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