sleep.c 23 KB

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