main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. static int acpi_sleep_prepare(u32 acpi_state)
  23. {
  24. #ifdef CONFIG_ACPI_SLEEP
  25. /* do we have a wakeup address for S2 and S3? */
  26. if (acpi_state == ACPI_STATE_S3) {
  27. if (!acpi_wakeup_address) {
  28. return -EFAULT;
  29. }
  30. acpi_set_firmware_waking_vector(
  31. (acpi_physical_address)acpi_wakeup_address);
  32. }
  33. ACPI_FLUSH_CPU_CACHE();
  34. acpi_enable_wakeup_device_prep(acpi_state);
  35. #endif
  36. printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
  37. acpi_state);
  38. acpi_enter_sleep_state_prep(acpi_state);
  39. return 0;
  40. }
  41. #ifdef CONFIG_PM_SLEEP
  42. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  43. /*
  44. * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
  45. * user to request that behavior by using the 'acpi_old_suspend_ordering'
  46. * kernel command line option that causes the following variable to be set.
  47. */
  48. static bool old_suspend_ordering;
  49. void __init acpi_old_suspend_ordering(void)
  50. {
  51. old_suspend_ordering = true;
  52. }
  53. /**
  54. * acpi_pm_disable_gpes - Disable the GPEs.
  55. */
  56. static int acpi_pm_disable_gpes(void)
  57. {
  58. acpi_hw_disable_all_gpes();
  59. return 0;
  60. }
  61. /**
  62. * __acpi_pm_prepare - Prepare the platform to enter the target state.
  63. *
  64. * If necessary, set the firmware waking vector and do arch-specific
  65. * nastiness to get the wakeup code to the waking vector.
  66. */
  67. static int __acpi_pm_prepare(void)
  68. {
  69. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  70. if (error)
  71. acpi_target_sleep_state = ACPI_STATE_S0;
  72. return error;
  73. }
  74. /**
  75. * acpi_pm_prepare - Prepare the platform to enter the target sleep
  76. * state and disable the GPEs.
  77. */
  78. static int acpi_pm_prepare(void)
  79. {
  80. int error = __acpi_pm_prepare();
  81. if (!error)
  82. acpi_hw_disable_all_gpes();
  83. return error;
  84. }
  85. /**
  86. * acpi_pm_finish - Instruct the platform to leave a sleep state.
  87. *
  88. * This is called after we wake back up (or if entering the sleep state
  89. * failed).
  90. */
  91. static void acpi_pm_finish(void)
  92. {
  93. u32 acpi_state = acpi_target_sleep_state;
  94. if (acpi_state == ACPI_STATE_S0)
  95. return;
  96. printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
  97. acpi_state);
  98. acpi_disable_wakeup_device(acpi_state);
  99. acpi_leave_sleep_state(acpi_state);
  100. /* reset firmware waking vector */
  101. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  102. acpi_target_sleep_state = ACPI_STATE_S0;
  103. }
  104. /**
  105. * acpi_pm_end - Finish up suspend sequence.
  106. */
  107. static void acpi_pm_end(void)
  108. {
  109. /*
  110. * This is necessary in case acpi_pm_finish() is not called during a
  111. * failing transition to a sleep state.
  112. */
  113. acpi_target_sleep_state = ACPI_STATE_S0;
  114. }
  115. #endif /* CONFIG_PM_SLEEP */
  116. #ifdef CONFIG_SUSPEND
  117. extern void do_suspend_lowlevel(void);
  118. static u32 acpi_suspend_states[] = {
  119. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  120. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  121. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  122. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  123. };
  124. /**
  125. * acpi_suspend_begin - Set the target system sleep state to the state
  126. * associated with given @pm_state, if supported.
  127. */
  128. static int acpi_suspend_begin(suspend_state_t pm_state)
  129. {
  130. u32 acpi_state = acpi_suspend_states[pm_state];
  131. int error = 0;
  132. if (sleep_states[acpi_state]) {
  133. acpi_target_sleep_state = acpi_state;
  134. } else {
  135. printk(KERN_ERR "ACPI does not support this state: %d\n",
  136. pm_state);
  137. error = -ENOSYS;
  138. }
  139. return error;
  140. }
  141. /**
  142. * acpi_suspend_enter - Actually enter a sleep state.
  143. * @pm_state: ignored
  144. *
  145. * Flush caches and go to sleep. For STR we have to call arch-specific
  146. * assembly, which in turn call acpi_enter_sleep_state().
  147. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  148. */
  149. static int acpi_suspend_enter(suspend_state_t pm_state)
  150. {
  151. acpi_status status = AE_OK;
  152. unsigned long flags = 0;
  153. u32 acpi_state = acpi_target_sleep_state;
  154. ACPI_FLUSH_CPU_CACHE();
  155. /* Do arch specific saving of state. */
  156. if (acpi_state == ACPI_STATE_S3) {
  157. int error = acpi_save_state_mem();
  158. if (error)
  159. return error;
  160. }
  161. local_irq_save(flags);
  162. acpi_enable_wakeup_device(acpi_state);
  163. switch (acpi_state) {
  164. case ACPI_STATE_S1:
  165. barrier();
  166. status = acpi_enter_sleep_state(acpi_state);
  167. break;
  168. case ACPI_STATE_S3:
  169. do_suspend_lowlevel();
  170. break;
  171. }
  172. /* Reprogram control registers and execute _BFS */
  173. acpi_leave_sleep_state_prep(acpi_state);
  174. /* ACPI 3.0 specs (P62) says that it's the responsibility
  175. * of the OSPM to clear the status bit [ implying that the
  176. * POWER_BUTTON event should not reach userspace ]
  177. */
  178. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
  179. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  180. /*
  181. * Disable and clear GPE status before interrupt is enabled. Some GPEs
  182. * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
  183. * acpi_leave_sleep_state will reenable specific GPEs later
  184. */
  185. acpi_hw_disable_all_gpes();
  186. local_irq_restore(flags);
  187. printk(KERN_DEBUG "Back to C!\n");
  188. /* restore processor state */
  189. if (acpi_state == ACPI_STATE_S3)
  190. acpi_restore_state_mem();
  191. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  192. }
  193. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  194. {
  195. u32 acpi_state;
  196. switch (pm_state) {
  197. case PM_SUSPEND_ON:
  198. case PM_SUSPEND_STANDBY:
  199. case PM_SUSPEND_MEM:
  200. acpi_state = acpi_suspend_states[pm_state];
  201. return sleep_states[acpi_state];
  202. default:
  203. return 0;
  204. }
  205. }
  206. static struct platform_suspend_ops acpi_suspend_ops = {
  207. .valid = acpi_suspend_state_valid,
  208. .begin = acpi_suspend_begin,
  209. .prepare = acpi_pm_prepare,
  210. .enter = acpi_suspend_enter,
  211. .finish = acpi_pm_finish,
  212. .end = acpi_pm_end,
  213. };
  214. /**
  215. * acpi_suspend_begin_old - Set the target system sleep state to the
  216. * state associated with given @pm_state, if supported, and
  217. * execute the _PTS control method. This function is used if the
  218. * pre-ACPI 2.0 suspend ordering has been requested.
  219. */
  220. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  221. {
  222. int error = acpi_suspend_begin(pm_state);
  223. if (!error)
  224. error = __acpi_pm_prepare();
  225. return error;
  226. }
  227. /*
  228. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  229. * been requested.
  230. */
  231. static struct platform_suspend_ops acpi_suspend_ops_old = {
  232. .valid = acpi_suspend_state_valid,
  233. .begin = acpi_suspend_begin_old,
  234. .prepare = acpi_pm_disable_gpes,
  235. .enter = acpi_suspend_enter,
  236. .finish = acpi_pm_finish,
  237. .end = acpi_pm_end,
  238. .recover = acpi_pm_finish,
  239. };
  240. static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
  241. {
  242. old_suspend_ordering = true;
  243. return 0;
  244. }
  245. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  246. {
  247. .callback = init_old_suspend_ordering,
  248. .ident = "Abit KN9 (nForce4 variant)",
  249. .matches = {
  250. DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
  251. DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
  252. },
  253. },
  254. {},
  255. };
  256. #endif /* CONFIG_SUSPEND */
  257. #ifdef CONFIG_HIBERNATION
  258. static unsigned long s4_hardware_signature;
  259. static struct acpi_table_facs *facs;
  260. static bool nosigcheck;
  261. void __init acpi_no_s4_hw_signature(void)
  262. {
  263. nosigcheck = true;
  264. }
  265. static int acpi_hibernation_begin(void)
  266. {
  267. acpi_target_sleep_state = ACPI_STATE_S4;
  268. return 0;
  269. }
  270. static int acpi_hibernation_enter(void)
  271. {
  272. acpi_status status = AE_OK;
  273. unsigned long flags = 0;
  274. ACPI_FLUSH_CPU_CACHE();
  275. local_irq_save(flags);
  276. acpi_enable_wakeup_device(ACPI_STATE_S4);
  277. /* This shouldn't return. If it returns, we have a problem */
  278. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  279. /* Reprogram control registers and execute _BFS */
  280. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  281. local_irq_restore(flags);
  282. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  283. }
  284. static void acpi_hibernation_leave(void)
  285. {
  286. /*
  287. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  288. * enable it here.
  289. */
  290. acpi_enable();
  291. /* Reprogram control registers and execute _BFS */
  292. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  293. /* Check the hardware signature */
  294. if (facs && s4_hardware_signature != facs->hardware_signature) {
  295. printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
  296. "cannot resume!\n");
  297. panic("ACPI S4 hardware signature mismatch");
  298. }
  299. }
  300. static void acpi_pm_enable_gpes(void)
  301. {
  302. acpi_hw_enable_all_runtime_gpes();
  303. }
  304. static struct platform_hibernation_ops acpi_hibernation_ops = {
  305. .begin = acpi_hibernation_begin,
  306. .end = acpi_pm_end,
  307. .pre_snapshot = acpi_pm_prepare,
  308. .finish = acpi_pm_finish,
  309. .prepare = acpi_pm_prepare,
  310. .enter = acpi_hibernation_enter,
  311. .leave = acpi_hibernation_leave,
  312. .pre_restore = acpi_pm_disable_gpes,
  313. .restore_cleanup = acpi_pm_enable_gpes,
  314. };
  315. /**
  316. * acpi_hibernation_begin_old - Set the target system sleep state to
  317. * ACPI_STATE_S4 and execute the _PTS control method. This
  318. * function is used if the pre-ACPI 2.0 suspend ordering has been
  319. * requested.
  320. */
  321. static int acpi_hibernation_begin_old(void)
  322. {
  323. int error = acpi_sleep_prepare(ACPI_STATE_S4);
  324. if (!error)
  325. acpi_target_sleep_state = ACPI_STATE_S4;
  326. return error;
  327. }
  328. /*
  329. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  330. * been requested.
  331. */
  332. static struct platform_hibernation_ops acpi_hibernation_ops_old = {
  333. .begin = acpi_hibernation_begin_old,
  334. .end = acpi_pm_end,
  335. .pre_snapshot = acpi_pm_disable_gpes,
  336. .finish = acpi_pm_finish,
  337. .prepare = acpi_pm_disable_gpes,
  338. .enter = acpi_hibernation_enter,
  339. .leave = acpi_hibernation_leave,
  340. .pre_restore = acpi_pm_disable_gpes,
  341. .restore_cleanup = acpi_pm_enable_gpes,
  342. .recover = acpi_pm_finish,
  343. };
  344. #endif /* CONFIG_HIBERNATION */
  345. int acpi_suspend(u32 acpi_state)
  346. {
  347. suspend_state_t states[] = {
  348. [1] = PM_SUSPEND_STANDBY,
  349. [3] = PM_SUSPEND_MEM,
  350. [5] = PM_SUSPEND_MAX
  351. };
  352. if (acpi_state < 6 && states[acpi_state])
  353. return pm_suspend(states[acpi_state]);
  354. if (acpi_state == 4)
  355. return hibernate();
  356. return -EINVAL;
  357. }
  358. #ifdef CONFIG_PM_SLEEP
  359. /**
  360. * acpi_pm_device_sleep_state - return preferred power state of ACPI device
  361. * in the system sleep state given by %acpi_target_sleep_state
  362. * @dev: device to examine; its driver model wakeup flags control
  363. * whether it should be able to wake up the system
  364. * @d_min_p: used to store the upper limit of allowed states range
  365. * Return value: preferred power state of the device on success, -ENODEV on
  366. * failure (ie. if there's no 'struct acpi_device' for @dev)
  367. *
  368. * Find the lowest power (highest number) ACPI device power state that
  369. * device @dev can be in while the system is in the sleep state represented
  370. * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
  371. * able to wake up the system from this sleep state. If @d_min_p is set,
  372. * the highest power (lowest number) device power state of @dev allowed
  373. * in this system sleep state is stored at the location pointed to by it.
  374. *
  375. * The caller must ensure that @dev is valid before using this function.
  376. * The caller is also responsible for figuring out if the device is
  377. * supposed to be able to wake up the system and passing this information
  378. * via @wake.
  379. */
  380. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
  381. {
  382. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  383. struct acpi_device *adev;
  384. char acpi_method[] = "_SxD";
  385. unsigned long d_min, d_max;
  386. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  387. printk(KERN_DEBUG "ACPI handle has no context!\n");
  388. return -ENODEV;
  389. }
  390. acpi_method[2] = '0' + acpi_target_sleep_state;
  391. /*
  392. * If the sleep state is S0, we will return D3, but if the device has
  393. * _S0W, we will use the value from _S0W
  394. */
  395. d_min = ACPI_STATE_D0;
  396. d_max = ACPI_STATE_D3;
  397. /*
  398. * If present, _SxD methods return the minimum D-state (highest power
  399. * state) we can use for the corresponding S-states. Otherwise, the
  400. * minimum D-state is D0 (ACPI 3.x).
  401. *
  402. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  403. * provided -- that's our fault recovery, we ignore retval.
  404. */
  405. if (acpi_target_sleep_state > ACPI_STATE_S0)
  406. acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
  407. /*
  408. * If _PRW says we can wake up the system from the target sleep state,
  409. * the D-state returned by _SxD is sufficient for that (we assume a
  410. * wakeup-aware driver if wake is set). Still, if _SxW exists
  411. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  412. * can wake the system. _S0W may be valid, too.
  413. */
  414. if (acpi_target_sleep_state == ACPI_STATE_S0 ||
  415. (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
  416. adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
  417. acpi_status status;
  418. acpi_method[3] = 'W';
  419. status = acpi_evaluate_integer(handle, acpi_method, NULL,
  420. &d_max);
  421. if (ACPI_FAILURE(status)) {
  422. d_max = d_min;
  423. } else if (d_max < d_min) {
  424. /* Warn the user of the broken DSDT */
  425. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  426. acpi_method);
  427. /* Sanitize it */
  428. d_min = d_max;
  429. }
  430. }
  431. if (d_min_p)
  432. *d_min_p = d_min;
  433. return d_max;
  434. }
  435. /**
  436. * acpi_pm_device_sleep_wake - enable or disable the system wake-up
  437. * capability of given device
  438. * @dev: device to handle
  439. * @enable: 'true' - enable, 'false' - disable the wake-up capability
  440. */
  441. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  442. {
  443. acpi_handle handle;
  444. struct acpi_device *adev;
  445. if (!device_may_wakeup(dev))
  446. return -EINVAL;
  447. handle = DEVICE_ACPI_HANDLE(dev);
  448. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  449. printk(KERN_DEBUG "ACPI handle has no context!\n");
  450. return -ENODEV;
  451. }
  452. return enable ?
  453. acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
  454. acpi_disable_wakeup_device_power(adev);
  455. }
  456. #endif
  457. static void acpi_power_off_prepare(void)
  458. {
  459. /* Prepare to power off the system */
  460. acpi_sleep_prepare(ACPI_STATE_S5);
  461. acpi_hw_disable_all_gpes();
  462. }
  463. static void acpi_power_off(void)
  464. {
  465. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  466. printk("%s called\n", __func__);
  467. local_irq_disable();
  468. acpi_enable_wakeup_device(ACPI_STATE_S5);
  469. acpi_enter_sleep_state(ACPI_STATE_S5);
  470. }
  471. int __init acpi_sleep_init(void)
  472. {
  473. acpi_status status;
  474. u8 type_a, type_b;
  475. #ifdef CONFIG_SUSPEND
  476. int i = 0;
  477. dmi_check_system(acpisleep_dmi_table);
  478. #endif
  479. if (acpi_disabled)
  480. return 0;
  481. sleep_states[ACPI_STATE_S0] = 1;
  482. printk(KERN_INFO PREFIX "(supports S0");
  483. #ifdef CONFIG_SUSPEND
  484. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
  485. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  486. if (ACPI_SUCCESS(status)) {
  487. sleep_states[i] = 1;
  488. printk(" S%d", i);
  489. }
  490. }
  491. suspend_set_ops(old_suspend_ordering ?
  492. &acpi_suspend_ops_old : &acpi_suspend_ops);
  493. #endif
  494. #ifdef CONFIG_HIBERNATION
  495. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  496. if (ACPI_SUCCESS(status)) {
  497. hibernation_set_ops(old_suspend_ordering ?
  498. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  499. sleep_states[ACPI_STATE_S4] = 1;
  500. printk(" S4");
  501. if (!nosigcheck) {
  502. acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
  503. (struct acpi_table_header **)&facs);
  504. if (facs)
  505. s4_hardware_signature =
  506. facs->hardware_signature;
  507. }
  508. }
  509. #endif
  510. status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
  511. if (ACPI_SUCCESS(status)) {
  512. sleep_states[ACPI_STATE_S5] = 1;
  513. printk(" S5");
  514. pm_power_off_prepare = acpi_power_off_prepare;
  515. pm_power_off = acpi_power_off;
  516. }
  517. printk(")\n");
  518. return 0;
  519. }