main.c 13 KB

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