sleep.c 20 KB

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