sleep.c 24 KB

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