sleep.c 20 KB

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