sleep.c 22 KB

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