sleep.c 20 KB

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