sleep.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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 <linux/reboot.h>
  18. #include <asm/io.h>
  19. #include <acpi/acpi_bus.h>
  20. #include <acpi/acpi_drivers.h>
  21. #include "internal.h"
  22. #include "sleep.h"
  23. u8 sleep_states[ACPI_S_STATE_COUNT];
  24. static void acpi_sleep_tts_switch(u32 acpi_state)
  25. {
  26. union acpi_object in_arg = { ACPI_TYPE_INTEGER };
  27. struct acpi_object_list arg_list = { 1, &in_arg };
  28. acpi_status status = AE_OK;
  29. in_arg.integer.value = acpi_state;
  30. status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
  31. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  32. /*
  33. * OS can't evaluate the _TTS object correctly. Some warning
  34. * message will be printed. But it won't break anything.
  35. */
  36. printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
  37. }
  38. }
  39. static int tts_notify_reboot(struct notifier_block *this,
  40. unsigned long code, void *x)
  41. {
  42. acpi_sleep_tts_switch(ACPI_STATE_S5);
  43. return NOTIFY_DONE;
  44. }
  45. static struct notifier_block tts_notifier = {
  46. .notifier_call = tts_notify_reboot,
  47. .next = NULL,
  48. .priority = 0,
  49. };
  50. static int acpi_sleep_prepare(u32 acpi_state)
  51. {
  52. #ifdef CONFIG_ACPI_SLEEP
  53. /* do we have a wakeup address for S2 and S3? */
  54. if (acpi_state == ACPI_STATE_S3) {
  55. if (!acpi_wakeup_address) {
  56. return -EFAULT;
  57. }
  58. acpi_set_firmware_waking_vector(
  59. (acpi_physical_address)acpi_wakeup_address);
  60. }
  61. ACPI_FLUSH_CPU_CACHE();
  62. #endif
  63. printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
  64. acpi_state);
  65. acpi_enable_wakeup_devices(acpi_state);
  66. acpi_enter_sleep_state_prep(acpi_state);
  67. return 0;
  68. }
  69. #ifdef CONFIG_ACPI_SLEEP
  70. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  71. /*
  72. * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
  73. * user to request that behavior by using the 'acpi_old_suspend_ordering'
  74. * kernel command line option that causes the following variable to be set.
  75. */
  76. static bool old_suspend_ordering;
  77. void __init acpi_old_suspend_ordering(void)
  78. {
  79. old_suspend_ordering = true;
  80. }
  81. /**
  82. * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
  83. */
  84. static int acpi_pm_freeze(void)
  85. {
  86. acpi_disable_all_gpes();
  87. acpi_os_wait_events_complete(NULL);
  88. acpi_ec_block_transactions();
  89. return 0;
  90. }
  91. /**
  92. * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
  93. */
  94. static int acpi_pm_pre_suspend(void)
  95. {
  96. acpi_pm_freeze();
  97. suspend_nvs_save();
  98. return 0;
  99. }
  100. /**
  101. * __acpi_pm_prepare - Prepare the platform to enter the target state.
  102. *
  103. * If necessary, set the firmware waking vector and do arch-specific
  104. * nastiness to get the wakeup code to the waking vector.
  105. */
  106. static int __acpi_pm_prepare(void)
  107. {
  108. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  109. if (error)
  110. acpi_target_sleep_state = ACPI_STATE_S0;
  111. return error;
  112. }
  113. /**
  114. * acpi_pm_prepare - Prepare the platform to enter the target sleep
  115. * state and disable the GPEs.
  116. */
  117. static int acpi_pm_prepare(void)
  118. {
  119. int error = __acpi_pm_prepare();
  120. if (!error)
  121. acpi_pm_pre_suspend();
  122. return error;
  123. }
  124. /**
  125. * acpi_pm_finish - Instruct the platform to leave a sleep state.
  126. *
  127. * This is called after we wake back up (or if entering the sleep state
  128. * failed).
  129. */
  130. static void acpi_pm_finish(void)
  131. {
  132. u32 acpi_state = acpi_target_sleep_state;
  133. acpi_ec_unblock_transactions();
  134. if (acpi_state == ACPI_STATE_S0)
  135. return;
  136. printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
  137. acpi_state);
  138. acpi_disable_wakeup_devices(acpi_state);
  139. acpi_leave_sleep_state(acpi_state);
  140. /* reset firmware waking vector */
  141. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  142. acpi_target_sleep_state = ACPI_STATE_S0;
  143. }
  144. /**
  145. * acpi_pm_end - Finish up suspend sequence.
  146. */
  147. static void acpi_pm_end(void)
  148. {
  149. suspend_nvs_free();
  150. /*
  151. * This is necessary in case acpi_pm_finish() is not called during a
  152. * failing transition to a sleep state.
  153. */
  154. acpi_target_sleep_state = ACPI_STATE_S0;
  155. acpi_sleep_tts_switch(acpi_target_sleep_state);
  156. }
  157. #else /* !CONFIG_ACPI_SLEEP */
  158. #define acpi_target_sleep_state ACPI_STATE_S0
  159. #endif /* CONFIG_ACPI_SLEEP */
  160. #ifdef CONFIG_SUSPEND
  161. extern void do_suspend_lowlevel(void);
  162. static u32 acpi_suspend_states[] = {
  163. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  164. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  165. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  166. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  167. };
  168. /**
  169. * acpi_suspend_begin - Set the target system sleep state to the state
  170. * associated with given @pm_state, if supported.
  171. */
  172. static int acpi_suspend_begin(suspend_state_t pm_state)
  173. {
  174. u32 acpi_state = acpi_suspend_states[pm_state];
  175. int error = 0;
  176. error = suspend_nvs_alloc();
  177. if (error)
  178. return error;
  179. if (sleep_states[acpi_state]) {
  180. acpi_target_sleep_state = acpi_state;
  181. acpi_sleep_tts_switch(acpi_target_sleep_state);
  182. } else {
  183. printk(KERN_ERR "ACPI does not support this state: %d\n",
  184. pm_state);
  185. error = -ENOSYS;
  186. }
  187. return error;
  188. }
  189. /**
  190. * acpi_suspend_enter - Actually enter a sleep state.
  191. * @pm_state: ignored
  192. *
  193. * Flush caches and go to sleep. For STR we have to call arch-specific
  194. * assembly, which in turn call acpi_enter_sleep_state().
  195. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  196. */
  197. static int acpi_suspend_enter(suspend_state_t pm_state)
  198. {
  199. acpi_status status = AE_OK;
  200. unsigned long flags = 0;
  201. u32 acpi_state = acpi_target_sleep_state;
  202. ACPI_FLUSH_CPU_CACHE();
  203. /* Do arch specific saving of state. */
  204. if (acpi_state == ACPI_STATE_S3) {
  205. int error = acpi_save_state_mem();
  206. if (error)
  207. return error;
  208. }
  209. local_irq_save(flags);
  210. switch (acpi_state) {
  211. case ACPI_STATE_S1:
  212. barrier();
  213. status = acpi_enter_sleep_state(acpi_state);
  214. break;
  215. case ACPI_STATE_S3:
  216. do_suspend_lowlevel();
  217. break;
  218. }
  219. /* This violates the spec but is required for bug compatibility. */
  220. acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
  221. /* Reprogram control registers and execute _BFS */
  222. acpi_leave_sleep_state_prep(acpi_state);
  223. /* ACPI 3.0 specs (P62) says that it's the responsibility
  224. * of the OSPM to clear the status bit [ implying that the
  225. * POWER_BUTTON event should not reach userspace ]
  226. */
  227. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
  228. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  229. /*
  230. * Disable and clear GPE status before interrupt is enabled. Some GPEs
  231. * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
  232. * acpi_leave_sleep_state will reenable specific GPEs later
  233. */
  234. acpi_disable_all_gpes();
  235. /* Allow EC transactions to happen. */
  236. acpi_ec_unblock_transactions_early();
  237. local_irq_restore(flags);
  238. printk(KERN_DEBUG "Back to C!\n");
  239. /* restore processor state */
  240. if (acpi_state == ACPI_STATE_S3)
  241. acpi_restore_state_mem();
  242. suspend_nvs_restore();
  243. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  244. }
  245. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  246. {
  247. u32 acpi_state;
  248. switch (pm_state) {
  249. case PM_SUSPEND_ON:
  250. case PM_SUSPEND_STANDBY:
  251. case PM_SUSPEND_MEM:
  252. acpi_state = acpi_suspend_states[pm_state];
  253. return sleep_states[acpi_state];
  254. default:
  255. return 0;
  256. }
  257. }
  258. static struct platform_suspend_ops acpi_suspend_ops = {
  259. .valid = acpi_suspend_state_valid,
  260. .begin = acpi_suspend_begin,
  261. .prepare_late = acpi_pm_prepare,
  262. .enter = acpi_suspend_enter,
  263. .wake = acpi_pm_finish,
  264. .end = acpi_pm_end,
  265. };
  266. /**
  267. * acpi_suspend_begin_old - Set the target system sleep state to the
  268. * state associated with given @pm_state, if supported, and
  269. * execute the _PTS control method. This function is used if the
  270. * pre-ACPI 2.0 suspend ordering has been requested.
  271. */
  272. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  273. {
  274. int error = acpi_suspend_begin(pm_state);
  275. if (!error)
  276. error = __acpi_pm_prepare();
  277. return error;
  278. }
  279. /*
  280. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  281. * been requested.
  282. */
  283. static struct platform_suspend_ops acpi_suspend_ops_old = {
  284. .valid = acpi_suspend_state_valid,
  285. .begin = acpi_suspend_begin_old,
  286. .prepare_late = acpi_pm_pre_suspend,
  287. .enter = acpi_suspend_enter,
  288. .wake = acpi_pm_finish,
  289. .end = acpi_pm_end,
  290. .recover = acpi_pm_finish,
  291. };
  292. static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
  293. {
  294. old_suspend_ordering = true;
  295. return 0;
  296. }
  297. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  298. {
  299. .callback = init_old_suspend_ordering,
  300. .ident = "Abit KN9 (nForce4 variant)",
  301. .matches = {
  302. DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
  303. DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
  304. },
  305. },
  306. {
  307. .callback = init_old_suspend_ordering,
  308. .ident = "HP xw4600 Workstation",
  309. .matches = {
  310. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  311. DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
  312. },
  313. },
  314. {
  315. .callback = init_old_suspend_ordering,
  316. .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
  317. .matches = {
  318. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
  319. DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
  320. },
  321. },
  322. {
  323. .callback = init_old_suspend_ordering,
  324. .ident = "Panasonic CF51-2L",
  325. .matches = {
  326. DMI_MATCH(DMI_BOARD_VENDOR,
  327. "Matsushita Electric Industrial Co.,Ltd."),
  328. DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
  329. },
  330. },
  331. {},
  332. };
  333. #endif /* CONFIG_SUSPEND */
  334. #ifdef CONFIG_HIBERNATION
  335. /*
  336. * The ACPI specification wants us to save NVS memory regions during hibernation
  337. * and to restore them during the subsequent resume. However, it is not certain
  338. * if this mechanism is going to work on all machines, so we allow the user to
  339. * disable this mechanism using the 'acpi_sleep=s4_nonvs' kernel command line
  340. * option.
  341. */
  342. static bool s4_no_nvs;
  343. void __init acpi_s4_no_nvs(void)
  344. {
  345. s4_no_nvs = true;
  346. }
  347. static unsigned long s4_hardware_signature;
  348. static struct acpi_table_facs *facs;
  349. static bool nosigcheck;
  350. void __init acpi_no_s4_hw_signature(void)
  351. {
  352. nosigcheck = true;
  353. }
  354. static int acpi_hibernation_begin(void)
  355. {
  356. int error;
  357. error = s4_no_nvs ? 0 : suspend_nvs_alloc();
  358. if (!error) {
  359. acpi_target_sleep_state = ACPI_STATE_S4;
  360. acpi_sleep_tts_switch(acpi_target_sleep_state);
  361. }
  362. return error;
  363. }
  364. static int acpi_hibernation_enter(void)
  365. {
  366. acpi_status status = AE_OK;
  367. unsigned long flags = 0;
  368. ACPI_FLUSH_CPU_CACHE();
  369. local_irq_save(flags);
  370. /* This shouldn't return. If it returns, we have a problem */
  371. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  372. /* Reprogram control registers and execute _BFS */
  373. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  374. local_irq_restore(flags);
  375. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  376. }
  377. static void acpi_hibernation_leave(void)
  378. {
  379. /*
  380. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  381. * enable it here.
  382. */
  383. acpi_enable();
  384. /* Reprogram control registers and execute _BFS */
  385. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  386. /* Check the hardware signature */
  387. if (facs && s4_hardware_signature != facs->hardware_signature) {
  388. printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
  389. "cannot resume!\n");
  390. panic("ACPI S4 hardware signature mismatch");
  391. }
  392. /* Restore the NVS memory area */
  393. suspend_nvs_restore();
  394. /* Allow EC transactions to happen. */
  395. acpi_ec_unblock_transactions_early();
  396. }
  397. static void acpi_pm_thaw(void)
  398. {
  399. acpi_ec_unblock_transactions();
  400. acpi_enable_all_runtime_gpes();
  401. }
  402. static struct platform_hibernation_ops acpi_hibernation_ops = {
  403. .begin = acpi_hibernation_begin,
  404. .end = acpi_pm_end,
  405. .pre_snapshot = acpi_pm_prepare,
  406. .finish = acpi_pm_finish,
  407. .prepare = acpi_pm_prepare,
  408. .enter = acpi_hibernation_enter,
  409. .leave = acpi_hibernation_leave,
  410. .pre_restore = acpi_pm_freeze,
  411. .restore_cleanup = acpi_pm_thaw,
  412. };
  413. /**
  414. * acpi_hibernation_begin_old - Set the target system sleep state to
  415. * ACPI_STATE_S4 and execute the _PTS control method. This
  416. * function is used if the pre-ACPI 2.0 suspend ordering has been
  417. * requested.
  418. */
  419. static int acpi_hibernation_begin_old(void)
  420. {
  421. int error;
  422. /*
  423. * The _TTS object should always be evaluated before the _PTS object.
  424. * When the old_suspended_ordering is true, the _PTS object is
  425. * evaluated in the acpi_sleep_prepare.
  426. */
  427. acpi_sleep_tts_switch(ACPI_STATE_S4);
  428. error = acpi_sleep_prepare(ACPI_STATE_S4);
  429. if (!error) {
  430. if (!s4_no_nvs)
  431. error = suspend_nvs_alloc();
  432. if (!error)
  433. acpi_target_sleep_state = ACPI_STATE_S4;
  434. }
  435. return error;
  436. }
  437. /*
  438. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  439. * been requested.
  440. */
  441. static struct platform_hibernation_ops acpi_hibernation_ops_old = {
  442. .begin = acpi_hibernation_begin_old,
  443. .end = acpi_pm_end,
  444. .pre_snapshot = acpi_pm_pre_suspend,
  445. .prepare = acpi_pm_freeze,
  446. .finish = acpi_pm_finish,
  447. .enter = acpi_hibernation_enter,
  448. .leave = acpi_hibernation_leave,
  449. .pre_restore = acpi_pm_freeze,
  450. .restore_cleanup = acpi_pm_thaw,
  451. .recover = acpi_pm_finish,
  452. };
  453. #endif /* CONFIG_HIBERNATION */
  454. int acpi_suspend(u32 acpi_state)
  455. {
  456. suspend_state_t states[] = {
  457. [1] = PM_SUSPEND_STANDBY,
  458. [3] = PM_SUSPEND_MEM,
  459. [5] = PM_SUSPEND_MAX
  460. };
  461. if (acpi_state < 6 && states[acpi_state])
  462. return pm_suspend(states[acpi_state]);
  463. if (acpi_state == 4)
  464. return hibernate();
  465. return -EINVAL;
  466. }
  467. #ifdef CONFIG_PM_SLEEP
  468. /**
  469. * acpi_pm_device_sleep_state - return preferred power state of ACPI device
  470. * in the system sleep state given by %acpi_target_sleep_state
  471. * @dev: device to examine; its driver model wakeup flags control
  472. * whether it should be able to wake up the system
  473. * @d_min_p: used to store the upper limit of allowed states range
  474. * Return value: preferred power state of the device on success, -ENODEV on
  475. * failure (ie. if there's no 'struct acpi_device' for @dev)
  476. *
  477. * Find the lowest power (highest number) ACPI device power state that
  478. * device @dev can be in while the system is in the sleep state represented
  479. * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
  480. * able to wake up the system from this sleep state. If @d_min_p is set,
  481. * the highest power (lowest number) device power state of @dev allowed
  482. * in this system sleep state is stored at the location pointed to by it.
  483. *
  484. * The caller must ensure that @dev is valid before using this function.
  485. * The caller is also responsible for figuring out if the device is
  486. * supposed to be able to wake up the system and passing this information
  487. * via @wake.
  488. */
  489. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
  490. {
  491. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  492. struct acpi_device *adev;
  493. char acpi_method[] = "_SxD";
  494. unsigned long long d_min, d_max;
  495. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  496. printk(KERN_DEBUG "ACPI handle has no context!\n");
  497. return -ENODEV;
  498. }
  499. acpi_method[2] = '0' + acpi_target_sleep_state;
  500. /*
  501. * If the sleep state is S0, we will return D3, but if the device has
  502. * _S0W, we will use the value from _S0W
  503. */
  504. d_min = ACPI_STATE_D0;
  505. d_max = ACPI_STATE_D3;
  506. /*
  507. * If present, _SxD methods return the minimum D-state (highest power
  508. * state) we can use for the corresponding S-states. Otherwise, the
  509. * minimum D-state is D0 (ACPI 3.x).
  510. *
  511. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  512. * provided -- that's our fault recovery, we ignore retval.
  513. */
  514. if (acpi_target_sleep_state > ACPI_STATE_S0)
  515. acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
  516. /*
  517. * If _PRW says we can wake up the system from the target sleep state,
  518. * the D-state returned by _SxD is sufficient for that (we assume a
  519. * wakeup-aware driver if wake is set). Still, if _SxW exists
  520. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  521. * can wake the system. _S0W may be valid, too.
  522. */
  523. if (acpi_target_sleep_state == ACPI_STATE_S0 ||
  524. (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
  525. adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
  526. acpi_status status;
  527. acpi_method[3] = 'W';
  528. status = acpi_evaluate_integer(handle, acpi_method, NULL,
  529. &d_max);
  530. if (ACPI_FAILURE(status)) {
  531. d_max = d_min;
  532. } else if (d_max < d_min) {
  533. /* Warn the user of the broken DSDT */
  534. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  535. acpi_method);
  536. /* Sanitize it */
  537. d_min = d_max;
  538. }
  539. }
  540. if (d_min_p)
  541. *d_min_p = d_min;
  542. return d_max;
  543. }
  544. /**
  545. * acpi_pm_device_sleep_wake - enable or disable the system wake-up
  546. * capability of given device
  547. * @dev: device to handle
  548. * @enable: 'true' - enable, 'false' - disable the wake-up capability
  549. */
  550. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  551. {
  552. acpi_handle handle;
  553. struct acpi_device *adev;
  554. int error;
  555. if (!device_can_wakeup(dev))
  556. return -EINVAL;
  557. handle = DEVICE_ACPI_HANDLE(dev);
  558. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  559. dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
  560. return -ENODEV;
  561. }
  562. if (enable) {
  563. error = acpi_enable_wakeup_device_power(adev,
  564. acpi_target_sleep_state);
  565. if (!error)
  566. acpi_enable_gpe(adev->wakeup.gpe_device,
  567. adev->wakeup.gpe_number,
  568. ACPI_GPE_TYPE_WAKE);
  569. } else {
  570. acpi_disable_gpe(adev->wakeup.gpe_device, adev->wakeup.gpe_number,
  571. ACPI_GPE_TYPE_WAKE);
  572. error = acpi_disable_wakeup_device_power(adev);
  573. }
  574. if (!error)
  575. dev_info(dev, "wake-up capability %s by ACPI\n",
  576. enable ? "enabled" : "disabled");
  577. return error;
  578. }
  579. #endif
  580. static void acpi_power_off_prepare(void)
  581. {
  582. /* Prepare to power off the system */
  583. acpi_sleep_prepare(ACPI_STATE_S5);
  584. acpi_disable_all_gpes();
  585. }
  586. static void acpi_power_off(void)
  587. {
  588. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  589. printk(KERN_DEBUG "%s called\n", __func__);
  590. local_irq_disable();
  591. acpi_enter_sleep_state(ACPI_STATE_S5);
  592. }
  593. /*
  594. * ACPI 2.0 created the optional _GTS and _BFS,
  595. * but industry adoption has been neither rapid nor broad.
  596. *
  597. * Linux gets into trouble when it executes poorly validated
  598. * paths through the BIOS, so disable _GTS and _BFS by default,
  599. * but do speak up and offer the option to enable them.
  600. */
  601. void __init acpi_gts_bfs_check(void)
  602. {
  603. acpi_handle dummy;
  604. if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_NAME__GTS, &dummy)))
  605. {
  606. printk(KERN_NOTICE PREFIX "BIOS offers _GTS\n");
  607. printk(KERN_NOTICE PREFIX "If \"acpi.gts=1\" improves suspend, "
  608. "please notify linux-acpi@vger.kernel.org\n");
  609. }
  610. if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_NAME__BFS, &dummy)))
  611. {
  612. printk(KERN_NOTICE PREFIX "BIOS offers _BFS\n");
  613. printk(KERN_NOTICE PREFIX "If \"acpi.bfs=1\" improves resume, "
  614. "please notify linux-acpi@vger.kernel.org\n");
  615. }
  616. }
  617. int __init acpi_sleep_init(void)
  618. {
  619. acpi_status status;
  620. u8 type_a, type_b;
  621. #ifdef CONFIG_SUSPEND
  622. int i = 0;
  623. dmi_check_system(acpisleep_dmi_table);
  624. #endif
  625. if (acpi_disabled)
  626. return 0;
  627. sleep_states[ACPI_STATE_S0] = 1;
  628. printk(KERN_INFO PREFIX "(supports S0");
  629. #ifdef CONFIG_SUSPEND
  630. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
  631. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  632. if (ACPI_SUCCESS(status)) {
  633. sleep_states[i] = 1;
  634. printk(" S%d", i);
  635. }
  636. }
  637. suspend_set_ops(old_suspend_ordering ?
  638. &acpi_suspend_ops_old : &acpi_suspend_ops);
  639. #endif
  640. #ifdef CONFIG_HIBERNATION
  641. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  642. if (ACPI_SUCCESS(status)) {
  643. hibernation_set_ops(old_suspend_ordering ?
  644. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  645. sleep_states[ACPI_STATE_S4] = 1;
  646. printk(" S4");
  647. if (!nosigcheck) {
  648. acpi_get_table(ACPI_SIG_FACS, 1,
  649. (struct acpi_table_header **)&facs);
  650. if (facs)
  651. s4_hardware_signature =
  652. facs->hardware_signature;
  653. }
  654. }
  655. #endif
  656. status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
  657. if (ACPI_SUCCESS(status)) {
  658. sleep_states[ACPI_STATE_S5] = 1;
  659. printk(" S5");
  660. pm_power_off_prepare = acpi_power_off_prepare;
  661. pm_power_off = acpi_power_off;
  662. }
  663. printk(")\n");
  664. /*
  665. * Register the tts_notifier to reboot notifier list so that the _TTS
  666. * object can also be evaluated when the system enters S5.
  667. */
  668. register_reboot_notifier(&tts_notifier);
  669. acpi_gts_bfs_check();
  670. return 0;
  671. }