sleep.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sleep.c - ACPI sleep support.
  4. *
  5. * Copyright (c) 2005 Alexey Starikovskiy <[email protected]>
  6. * Copyright (c) 2004 David Shaohua Li <[email protected]>
  7. * Copyright (c) 2000-2003 Patrick Mochel
  8. * Copyright (c) 2003 Open Source Development Lab
  9. */
  10. #define pr_fmt(fmt) "ACPI: PM: " fmt
  11. #include <linux/delay.h>
  12. #include <linux/irq.h>
  13. #include <linux/dmi.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/suspend.h>
  17. #include <linux/reboot.h>
  18. #include <linux/acpi.h>
  19. #include <linux/module.h>
  20. #include <linux/syscore_ops.h>
  21. #include <asm/io.h>
  22. #include <trace/events/power.h>
  23. #include "internal.h"
  24. #include "sleep.h"
  25. /*
  26. * Some HW-full platforms do not have _S5, so they may need
  27. * to leverage efi power off for a shutdown.
  28. */
  29. bool acpi_no_s5;
  30. static u8 sleep_states[ACPI_S_STATE_COUNT];
  31. static void acpi_sleep_tts_switch(u32 acpi_state)
  32. {
  33. acpi_status status;
  34. status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
  35. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  36. /*
  37. * OS can't evaluate the _TTS object correctly. Some warning
  38. * message will be printed. But it won't break anything.
  39. */
  40. pr_notice("Failure in evaluating _TTS object\n");
  41. }
  42. }
  43. static int tts_notify_reboot(struct notifier_block *this,
  44. unsigned long code, void *x)
  45. {
  46. acpi_sleep_tts_switch(ACPI_STATE_S5);
  47. return NOTIFY_DONE;
  48. }
  49. static struct notifier_block tts_notifier = {
  50. .notifier_call = tts_notify_reboot,
  51. .next = NULL,
  52. .priority = 0,
  53. };
  54. #ifndef acpi_skip_set_wakeup_address
  55. #define acpi_skip_set_wakeup_address() false
  56. #endif
  57. static int acpi_sleep_prepare(u32 acpi_state)
  58. {
  59. #ifdef CONFIG_ACPI_SLEEP
  60. unsigned long acpi_wakeup_address;
  61. /* do we have a wakeup address for S2 and S3? */
  62. if (acpi_state == ACPI_STATE_S3 && !acpi_skip_set_wakeup_address()) {
  63. acpi_wakeup_address = acpi_get_wakeup_address();
  64. if (!acpi_wakeup_address)
  65. return -EFAULT;
  66. acpi_set_waking_vector(acpi_wakeup_address);
  67. }
  68. #endif
  69. pr_info("Preparing to enter system sleep state S%d\n", acpi_state);
  70. acpi_enable_wakeup_devices(acpi_state);
  71. acpi_enter_sleep_state_prep(acpi_state);
  72. return 0;
  73. }
  74. bool acpi_sleep_state_supported(u8 sleep_state)
  75. {
  76. acpi_status status;
  77. u8 type_a, type_b;
  78. status = acpi_get_sleep_type_data(sleep_state, &type_a, &type_b);
  79. return ACPI_SUCCESS(status) && (!acpi_gbl_reduced_hardware
  80. || (acpi_gbl_FADT.sleep_control.address
  81. && acpi_gbl_FADT.sleep_status.address));
  82. }
  83. #ifdef CONFIG_ACPI_SLEEP
  84. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  85. u32 acpi_target_system_state(void)
  86. {
  87. return acpi_target_sleep_state;
  88. }
  89. EXPORT_SYMBOL_GPL(acpi_target_system_state);
  90. static bool pwr_btn_event_pending;
  91. /*
  92. * The ACPI specification wants us to save NVS memory regions during hibernation
  93. * and to restore them during the subsequent resume. Windows does that also for
  94. * suspend to RAM. However, it is known that this mechanism does not work on
  95. * all machines, so we allow the user to disable it with the help of the
  96. * 'acpi_sleep=nonvs' kernel command line option.
  97. */
  98. static bool nvs_nosave;
  99. void __init acpi_nvs_nosave(void)
  100. {
  101. nvs_nosave = true;
  102. }
  103. /*
  104. * The ACPI specification wants us to save NVS memory regions during hibernation
  105. * but says nothing about saving NVS during S3. Not all versions of Windows
  106. * save NVS on S3 suspend either, and it is clear that not all systems need
  107. * NVS to be saved at S3 time. To improve suspend/resume time, allow the
  108. * user to disable saving NVS on S3 if their system does not require it, but
  109. * continue to save/restore NVS for S4 as specified.
  110. */
  111. static bool nvs_nosave_s3;
  112. void __init acpi_nvs_nosave_s3(void)
  113. {
  114. nvs_nosave_s3 = true;
  115. }
  116. static int __init init_nvs_save_s3(const struct dmi_system_id *d)
  117. {
  118. nvs_nosave_s3 = false;
  119. return 0;
  120. }
  121. /*
  122. * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
  123. * user to request that behavior by using the 'acpi_old_suspend_ordering'
  124. * kernel command line option that causes the following variable to be set.
  125. */
  126. static bool old_suspend_ordering;
  127. void __init acpi_old_suspend_ordering(void)
  128. {
  129. old_suspend_ordering = true;
  130. }
  131. static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
  132. {
  133. acpi_old_suspend_ordering();
  134. return 0;
  135. }
  136. static int __init init_nvs_nosave(const struct dmi_system_id *d)
  137. {
  138. acpi_nvs_nosave();
  139. return 0;
  140. }
  141. bool acpi_sleep_default_s3;
  142. static int __init init_default_s3(const struct dmi_system_id *d)
  143. {
  144. acpi_sleep_default_s3 = true;
  145. return 0;
  146. }
  147. static const struct dmi_system_id acpisleep_dmi_table[] __initconst = {
  148. {
  149. .callback = init_old_suspend_ordering,
  150. .ident = "Abit KN9 (nForce4 variant)",
  151. .matches = {
  152. DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
  153. DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
  154. },
  155. },
  156. {
  157. .callback = init_old_suspend_ordering,
  158. .ident = "HP xw4600 Workstation",
  159. .matches = {
  160. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  161. DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
  162. },
  163. },
  164. {
  165. .callback = init_old_suspend_ordering,
  166. .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
  167. .matches = {
  168. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
  169. DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
  170. },
  171. },
  172. {
  173. .callback = init_old_suspend_ordering,
  174. .ident = "Panasonic CF51-2L",
  175. .matches = {
  176. DMI_MATCH(DMI_BOARD_VENDOR,
  177. "Matsushita Electric Industrial Co.,Ltd."),
  178. DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
  179. },
  180. },
  181. {
  182. .callback = init_nvs_nosave,
  183. .ident = "Sony Vaio VGN-FW41E_H",
  184. .matches = {
  185. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  186. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
  187. },
  188. },
  189. {
  190. .callback = init_nvs_nosave,
  191. .ident = "Sony Vaio VGN-FW21E",
  192. .matches = {
  193. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  194. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
  195. },
  196. },
  197. {
  198. .callback = init_nvs_nosave,
  199. .ident = "Sony Vaio VGN-FW21M",
  200. .matches = {
  201. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  202. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
  203. },
  204. },
  205. {
  206. .callback = init_nvs_nosave,
  207. .ident = "Sony Vaio VPCEB17FX",
  208. .matches = {
  209. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  210. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
  211. },
  212. },
  213. {
  214. .callback = init_nvs_nosave,
  215. .ident = "Sony Vaio VGN-SR11M",
  216. .matches = {
  217. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  218. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
  219. },
  220. },
  221. {
  222. .callback = init_nvs_nosave,
  223. .ident = "Everex StepNote Series",
  224. .matches = {
  225. DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
  226. DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
  227. },
  228. },
  229. {
  230. .callback = init_nvs_nosave,
  231. .ident = "Sony Vaio VPCEB1Z1E",
  232. .matches = {
  233. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  234. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
  235. },
  236. },
  237. {
  238. .callback = init_nvs_nosave,
  239. .ident = "Sony Vaio VGN-NW130D",
  240. .matches = {
  241. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  242. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
  243. },
  244. },
  245. {
  246. .callback = init_nvs_nosave,
  247. .ident = "Sony Vaio VPCCW29FX",
  248. .matches = {
  249. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  250. DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
  251. },
  252. },
  253. {
  254. .callback = init_nvs_nosave,
  255. .ident = "Averatec AV1020-ED2",
  256. .matches = {
  257. DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
  258. DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
  259. },
  260. },
  261. {
  262. .callback = init_old_suspend_ordering,
  263. .ident = "Asus A8N-SLI DELUXE",
  264. .matches = {
  265. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  266. DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
  267. },
  268. },
  269. {
  270. .callback = init_old_suspend_ordering,
  271. .ident = "Asus A8N-SLI Premium",
  272. .matches = {
  273. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  274. DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
  275. },
  276. },
  277. {
  278. .callback = init_nvs_nosave,
  279. .ident = "Sony Vaio VGN-SR26GN_P",
  280. .matches = {
  281. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  282. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
  283. },
  284. },
  285. {
  286. .callback = init_nvs_nosave,
  287. .ident = "Sony Vaio VPCEB1S1E",
  288. .matches = {
  289. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  290. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
  291. },
  292. },
  293. {
  294. .callback = init_nvs_nosave,
  295. .ident = "Sony Vaio VGN-FW520F",
  296. .matches = {
  297. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  298. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
  299. },
  300. },
  301. {
  302. .callback = init_nvs_nosave,
  303. .ident = "Asus K54C",
  304. .matches = {
  305. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  306. DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
  307. },
  308. },
  309. {
  310. .callback = init_nvs_nosave,
  311. .ident = "Asus K54HR",
  312. .matches = {
  313. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  314. DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
  315. },
  316. },
  317. {
  318. .callback = init_nvs_save_s3,
  319. .ident = "Asus 1025C",
  320. .matches = {
  321. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  322. DMI_MATCH(DMI_PRODUCT_NAME, "1025C"),
  323. },
  324. },
  325. /*
  326. * https://bugzilla.kernel.org/show_bug.cgi?id=189431
  327. * Lenovo G50-45 is a platform later than 2012, but needs nvs memory
  328. * saving during S3.
  329. */
  330. {
  331. .callback = init_nvs_save_s3,
  332. .ident = "Lenovo G50-45",
  333. .matches = {
  334. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  335. DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
  336. },
  337. },
  338. {
  339. .callback = init_nvs_save_s3,
  340. .ident = "Lenovo G40-45",
  341. .matches = {
  342. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  343. DMI_MATCH(DMI_PRODUCT_NAME, "80E1"),
  344. },
  345. },
  346. /*
  347. * ThinkPad X1 Tablet(2016) cannot do suspend-to-idle using
  348. * the Low Power S0 Idle firmware interface (see
  349. * https://bugzilla.kernel.org/show_bug.cgi?id=199057).
  350. */
  351. {
  352. .callback = init_default_s3,
  353. .ident = "ThinkPad X1 Tablet(2016)",
  354. .matches = {
  355. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  356. DMI_MATCH(DMI_PRODUCT_NAME, "20GGA00L00"),
  357. },
  358. },
  359. /*
  360. * ASUS B1400CEAE hangs on resume from suspend (see
  361. * https://bugzilla.kernel.org/show_bug.cgi?id=215742).
  362. */
  363. {
  364. .callback = init_default_s3,
  365. .ident = "ASUS B1400CEAE",
  366. .matches = {
  367. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  368. DMI_MATCH(DMI_PRODUCT_NAME, "ASUS EXPERTBOOK B1400CEAE"),
  369. },
  370. },
  371. {},
  372. };
  373. static bool ignore_blacklist;
  374. void __init acpi_sleep_no_blacklist(void)
  375. {
  376. ignore_blacklist = true;
  377. }
  378. static void __init acpi_sleep_dmi_check(void)
  379. {
  380. if (ignore_blacklist)
  381. return;
  382. if (dmi_get_bios_year() >= 2012)
  383. acpi_nvs_nosave_s3();
  384. dmi_check_system(acpisleep_dmi_table);
  385. }
  386. /**
  387. * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
  388. */
  389. static int acpi_pm_freeze(void)
  390. {
  391. acpi_disable_all_gpes();
  392. acpi_os_wait_events_complete();
  393. acpi_ec_block_transactions();
  394. return 0;
  395. }
  396. /**
  397. * acpi_pm_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
  398. */
  399. static int acpi_pm_pre_suspend(void)
  400. {
  401. acpi_pm_freeze();
  402. return suspend_nvs_save();
  403. }
  404. /**
  405. * __acpi_pm_prepare - Prepare the platform to enter the target state.
  406. *
  407. * If necessary, set the firmware waking vector and do arch-specific
  408. * nastiness to get the wakeup code to the waking vector.
  409. */
  410. static int __acpi_pm_prepare(void)
  411. {
  412. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  413. if (error)
  414. acpi_target_sleep_state = ACPI_STATE_S0;
  415. return error;
  416. }
  417. /**
  418. * acpi_pm_prepare - Prepare the platform to enter the target sleep
  419. * state and disable the GPEs.
  420. */
  421. static int acpi_pm_prepare(void)
  422. {
  423. int error = __acpi_pm_prepare();
  424. if (!error)
  425. error = acpi_pm_pre_suspend();
  426. return error;
  427. }
  428. /**
  429. * acpi_pm_finish - Instruct the platform to leave a sleep state.
  430. *
  431. * This is called after we wake back up (or if entering the sleep state
  432. * failed).
  433. */
  434. static void acpi_pm_finish(void)
  435. {
  436. struct acpi_device *pwr_btn_adev;
  437. u32 acpi_state = acpi_target_sleep_state;
  438. acpi_ec_unblock_transactions();
  439. suspend_nvs_free();
  440. if (acpi_state == ACPI_STATE_S0)
  441. return;
  442. pr_info("Waking up from system sleep state S%d\n", acpi_state);
  443. acpi_disable_wakeup_devices(acpi_state);
  444. acpi_leave_sleep_state(acpi_state);
  445. /* reset firmware waking vector */
  446. acpi_set_waking_vector(0);
  447. acpi_target_sleep_state = ACPI_STATE_S0;
  448. acpi_resume_power_resources();
  449. /* If we were woken with the fixed power button, provide a small
  450. * hint to userspace in the form of a wakeup event on the fixed power
  451. * button device (if it can be found).
  452. *
  453. * We delay the event generation til now, as the PM layer requires
  454. * timekeeping to be running before we generate events. */
  455. if (!pwr_btn_event_pending)
  456. return;
  457. pwr_btn_event_pending = false;
  458. pwr_btn_adev = acpi_dev_get_first_match_dev(ACPI_BUTTON_HID_POWERF,
  459. NULL, -1);
  460. if (pwr_btn_adev) {
  461. pm_wakeup_event(&pwr_btn_adev->dev, 0);
  462. acpi_dev_put(pwr_btn_adev);
  463. }
  464. }
  465. /**
  466. * acpi_pm_start - Start system PM transition.
  467. */
  468. static void acpi_pm_start(u32 acpi_state)
  469. {
  470. acpi_target_sleep_state = acpi_state;
  471. acpi_sleep_tts_switch(acpi_target_sleep_state);
  472. acpi_scan_lock_acquire();
  473. }
  474. /**
  475. * acpi_pm_end - Finish up system PM transition.
  476. */
  477. static void acpi_pm_end(void)
  478. {
  479. acpi_turn_off_unused_power_resources();
  480. acpi_scan_lock_release();
  481. /*
  482. * This is necessary in case acpi_pm_finish() is not called during a
  483. * failing transition to a sleep state.
  484. */
  485. acpi_target_sleep_state = ACPI_STATE_S0;
  486. acpi_sleep_tts_switch(acpi_target_sleep_state);
  487. }
  488. #else /* !CONFIG_ACPI_SLEEP */
  489. #define sleep_no_lps0 (1)
  490. #define acpi_target_sleep_state ACPI_STATE_S0
  491. #define acpi_sleep_default_s3 (1)
  492. static inline void acpi_sleep_dmi_check(void) {}
  493. #endif /* CONFIG_ACPI_SLEEP */
  494. #ifdef CONFIG_SUSPEND
  495. static u32 acpi_suspend_states[] = {
  496. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  497. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  498. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  499. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  500. };
  501. /**
  502. * acpi_suspend_begin - Set the target system sleep state to the state
  503. * associated with given @pm_state, if supported.
  504. */
  505. static int acpi_suspend_begin(suspend_state_t pm_state)
  506. {
  507. u32 acpi_state = acpi_suspend_states[pm_state];
  508. int error;
  509. error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
  510. if (error)
  511. return error;
  512. if (!sleep_states[acpi_state]) {
  513. pr_err("ACPI does not support sleep state S%u\n", acpi_state);
  514. return -ENOSYS;
  515. }
  516. if (acpi_state > ACPI_STATE_S1)
  517. pm_set_suspend_via_firmware();
  518. acpi_pm_start(acpi_state);
  519. return 0;
  520. }
  521. /**
  522. * acpi_suspend_enter - Actually enter a sleep state.
  523. * @pm_state: ignored
  524. *
  525. * Flush caches and go to sleep. For STR we have to call arch-specific
  526. * assembly, which in turn call acpi_enter_sleep_state().
  527. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  528. */
  529. static int acpi_suspend_enter(suspend_state_t pm_state)
  530. {
  531. acpi_status status = AE_OK;
  532. u32 acpi_state = acpi_target_sleep_state;
  533. int error;
  534. trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
  535. switch (acpi_state) {
  536. case ACPI_STATE_S1:
  537. barrier();
  538. status = acpi_enter_sleep_state(acpi_state);
  539. break;
  540. case ACPI_STATE_S3:
  541. if (!acpi_suspend_lowlevel)
  542. return -ENOSYS;
  543. error = acpi_suspend_lowlevel();
  544. if (error)
  545. return error;
  546. pr_info("Low-level resume complete\n");
  547. pm_set_resume_via_firmware();
  548. break;
  549. }
  550. trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
  551. /* This violates the spec but is required for bug compatibility. */
  552. acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
  553. /* Reprogram control registers */
  554. acpi_leave_sleep_state_prep(acpi_state);
  555. /* ACPI 3.0 specs (P62) says that it's the responsibility
  556. * of the OSPM to clear the status bit [ implying that the
  557. * POWER_BUTTON event should not reach userspace ]
  558. *
  559. * However, we do generate a small hint for userspace in the form of
  560. * a wakeup event. We flag this condition for now and generate the
  561. * event later, as we're currently too early in resume to be able to
  562. * generate wakeup events.
  563. */
  564. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
  565. acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
  566. acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
  567. if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
  568. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  569. /* Flag for later */
  570. pwr_btn_event_pending = true;
  571. }
  572. }
  573. /*
  574. * Disable all GPE and clear their status bits before interrupts are
  575. * enabled. Some GPEs (like wakeup GPEs) have no handlers and this can
  576. * prevent them from producing spurious interrups.
  577. *
  578. * acpi_leave_sleep_state() will reenable specific GPEs later.
  579. *
  580. * Because this code runs on one CPU with disabled interrupts (all of
  581. * the other CPUs are offline at this time), it need not acquire any
  582. * sleeping locks which may trigger an implicit preemption point even
  583. * if there is no contention, so avoid doing that by using a low-level
  584. * library routine here.
  585. */
  586. acpi_hw_disable_all_gpes();
  587. /* Allow EC transactions to happen. */
  588. acpi_ec_unblock_transactions();
  589. suspend_nvs_restore();
  590. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  591. }
  592. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  593. {
  594. u32 acpi_state;
  595. switch (pm_state) {
  596. case PM_SUSPEND_ON:
  597. case PM_SUSPEND_STANDBY:
  598. case PM_SUSPEND_MEM:
  599. acpi_state = acpi_suspend_states[pm_state];
  600. return sleep_states[acpi_state];
  601. default:
  602. return 0;
  603. }
  604. }
  605. static const struct platform_suspend_ops acpi_suspend_ops = {
  606. .valid = acpi_suspend_state_valid,
  607. .begin = acpi_suspend_begin,
  608. .prepare_late = acpi_pm_prepare,
  609. .enter = acpi_suspend_enter,
  610. .wake = acpi_pm_finish,
  611. .end = acpi_pm_end,
  612. };
  613. /**
  614. * acpi_suspend_begin_old - Set the target system sleep state to the
  615. * state associated with given @pm_state, if supported, and
  616. * execute the _PTS control method. This function is used if the
  617. * pre-ACPI 2.0 suspend ordering has been requested.
  618. */
  619. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  620. {
  621. int error = acpi_suspend_begin(pm_state);
  622. if (!error)
  623. error = __acpi_pm_prepare();
  624. return error;
  625. }
  626. /*
  627. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  628. * been requested.
  629. */
  630. static const struct platform_suspend_ops acpi_suspend_ops_old = {
  631. .valid = acpi_suspend_state_valid,
  632. .begin = acpi_suspend_begin_old,
  633. .prepare_late = acpi_pm_pre_suspend,
  634. .enter = acpi_suspend_enter,
  635. .wake = acpi_pm_finish,
  636. .end = acpi_pm_end,
  637. .recover = acpi_pm_finish,
  638. };
  639. static bool s2idle_wakeup;
  640. int acpi_s2idle_begin(void)
  641. {
  642. acpi_scan_lock_acquire();
  643. return 0;
  644. }
  645. int acpi_s2idle_prepare(void)
  646. {
  647. if (acpi_sci_irq_valid()) {
  648. enable_irq_wake(acpi_sci_irq);
  649. acpi_ec_set_gpe_wake_mask(ACPI_GPE_ENABLE);
  650. }
  651. acpi_enable_wakeup_devices(ACPI_STATE_S0);
  652. /* Change the configuration of GPEs to avoid spurious wakeup. */
  653. acpi_enable_all_wakeup_gpes();
  654. acpi_os_wait_events_complete();
  655. s2idle_wakeup = true;
  656. return 0;
  657. }
  658. bool acpi_s2idle_wake(void)
  659. {
  660. if (!acpi_sci_irq_valid())
  661. return pm_wakeup_pending();
  662. while (pm_wakeup_pending()) {
  663. /*
  664. * If IRQD_WAKEUP_ARMED is set for the SCI at this point, the
  665. * SCI has not triggered while suspended, so bail out (the
  666. * wakeup is pending anyway and the SCI is not the source of
  667. * it).
  668. */
  669. if (irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq))) {
  670. pm_pr_dbg("Wakeup unrelated to ACPI SCI\n");
  671. return true;
  672. }
  673. /*
  674. * If the status bit of any enabled fixed event is set, the
  675. * wakeup is regarded as valid.
  676. */
  677. if (acpi_any_fixed_event_status_set()) {
  678. pm_pr_dbg("ACPI fixed event wakeup\n");
  679. return true;
  680. }
  681. /* Check wakeups from drivers sharing the SCI. */
  682. if (acpi_check_wakeup_handlers()) {
  683. pm_pr_dbg("ACPI custom handler wakeup\n");
  684. return true;
  685. }
  686. /*
  687. * Check non-EC GPE wakeups and if there are none, cancel the
  688. * SCI-related wakeup and dispatch the EC GPE.
  689. */
  690. if (acpi_ec_dispatch_gpe()) {
  691. pm_pr_dbg("ACPI non-EC GPE wakeup\n");
  692. return true;
  693. }
  694. acpi_os_wait_events_complete();
  695. /*
  696. * The SCI is in the "suspended" state now and it cannot produce
  697. * new wakeup events till the rearming below, so if any of them
  698. * are pending here, they must be resulting from the processing
  699. * of EC events above or coming from somewhere else.
  700. */
  701. if (pm_wakeup_pending()) {
  702. pm_pr_dbg("Wakeup after ACPI Notify sync\n");
  703. return true;
  704. }
  705. pm_pr_dbg("Rearming ACPI SCI for wakeup\n");
  706. pm_wakeup_clear(acpi_sci_irq);
  707. rearm_wake_irq(acpi_sci_irq);
  708. }
  709. return false;
  710. }
  711. void acpi_s2idle_restore(void)
  712. {
  713. /*
  714. * Drain pending events before restoring the working-state configuration
  715. * of GPEs.
  716. */
  717. acpi_os_wait_events_complete(); /* synchronize GPE processing */
  718. acpi_ec_flush_work(); /* flush the EC driver's workqueues */
  719. acpi_os_wait_events_complete(); /* synchronize Notify handling */
  720. s2idle_wakeup = false;
  721. acpi_enable_all_runtime_gpes();
  722. acpi_disable_wakeup_devices(ACPI_STATE_S0);
  723. if (acpi_sci_irq_valid()) {
  724. acpi_ec_set_gpe_wake_mask(ACPI_GPE_DISABLE);
  725. disable_irq_wake(acpi_sci_irq);
  726. }
  727. }
  728. void acpi_s2idle_end(void)
  729. {
  730. acpi_scan_lock_release();
  731. }
  732. static const struct platform_s2idle_ops acpi_s2idle_ops = {
  733. .begin = acpi_s2idle_begin,
  734. .prepare = acpi_s2idle_prepare,
  735. .wake = acpi_s2idle_wake,
  736. .restore = acpi_s2idle_restore,
  737. .end = acpi_s2idle_end,
  738. };
  739. void __weak acpi_s2idle_setup(void)
  740. {
  741. if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
  742. pr_info("Efficient low-power S0 idle declared\n");
  743. s2idle_set_ops(&acpi_s2idle_ops);
  744. }
  745. static void acpi_sleep_suspend_setup(void)
  746. {
  747. bool suspend_ops_needed = false;
  748. int i;
  749. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
  750. if (acpi_sleep_state_supported(i)) {
  751. sleep_states[i] = 1;
  752. suspend_ops_needed = true;
  753. }
  754. if (suspend_ops_needed)
  755. suspend_set_ops(old_suspend_ordering ?
  756. &acpi_suspend_ops_old : &acpi_suspend_ops);
  757. acpi_s2idle_setup();
  758. }
  759. #else /* !CONFIG_SUSPEND */
  760. #define s2idle_wakeup (false)
  761. static inline void acpi_sleep_suspend_setup(void) {}
  762. #endif /* !CONFIG_SUSPEND */
  763. bool acpi_s2idle_wakeup(void)
  764. {
  765. return s2idle_wakeup;
  766. }
  767. #ifdef CONFIG_PM_SLEEP
  768. static u32 saved_bm_rld;
  769. static int acpi_save_bm_rld(void)
  770. {
  771. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
  772. return 0;
  773. }
  774. static void acpi_restore_bm_rld(void)
  775. {
  776. u32 resumed_bm_rld = 0;
  777. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
  778. if (resumed_bm_rld == saved_bm_rld)
  779. return;
  780. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
  781. }
  782. static struct syscore_ops acpi_sleep_syscore_ops = {
  783. .suspend = acpi_save_bm_rld,
  784. .resume = acpi_restore_bm_rld,
  785. };
  786. static void acpi_sleep_syscore_init(void)
  787. {
  788. register_syscore_ops(&acpi_sleep_syscore_ops);
  789. }
  790. #else
  791. static inline void acpi_sleep_syscore_init(void) {}
  792. #endif /* CONFIG_PM_SLEEP */
  793. #ifdef CONFIG_HIBERNATION
  794. static unsigned long s4_hardware_signature;
  795. static struct acpi_table_facs *facs;
  796. int acpi_check_s4_hw_signature = -1; /* Default behaviour is just to warn */
  797. static int acpi_hibernation_begin(pm_message_t stage)
  798. {
  799. if (!nvs_nosave) {
  800. int error = suspend_nvs_alloc();
  801. if (error)
  802. return error;
  803. }
  804. if (stage.event == PM_EVENT_HIBERNATE)
  805. pm_set_suspend_via_firmware();
  806. acpi_pm_start(ACPI_STATE_S4);
  807. return 0;
  808. }
  809. static int acpi_hibernation_enter(void)
  810. {
  811. acpi_status status = AE_OK;
  812. /* This shouldn't return. If it returns, we have a problem */
  813. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  814. /* Reprogram control registers */
  815. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  816. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  817. }
  818. static void acpi_hibernation_leave(void)
  819. {
  820. pm_set_resume_via_firmware();
  821. /*
  822. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  823. * enable it here.
  824. */
  825. acpi_enable();
  826. /* Reprogram control registers */
  827. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  828. /* Check the hardware signature */
  829. if (facs && s4_hardware_signature != facs->hardware_signature)
  830. pr_crit("Hardware changed while hibernated, success doubtful!\n");
  831. /* Restore the NVS memory area */
  832. suspend_nvs_restore();
  833. /* Allow EC transactions to happen. */
  834. acpi_ec_unblock_transactions();
  835. }
  836. static void acpi_pm_thaw(void)
  837. {
  838. acpi_ec_unblock_transactions();
  839. acpi_enable_all_runtime_gpes();
  840. }
  841. static const struct platform_hibernation_ops acpi_hibernation_ops = {
  842. .begin = acpi_hibernation_begin,
  843. .end = acpi_pm_end,
  844. .pre_snapshot = acpi_pm_prepare,
  845. .finish = acpi_pm_finish,
  846. .prepare = acpi_pm_prepare,
  847. .enter = acpi_hibernation_enter,
  848. .leave = acpi_hibernation_leave,
  849. .pre_restore = acpi_pm_freeze,
  850. .restore_cleanup = acpi_pm_thaw,
  851. };
  852. /**
  853. * acpi_hibernation_begin_old - Set the target system sleep state to
  854. * ACPI_STATE_S4 and execute the _PTS control method. This
  855. * function is used if the pre-ACPI 2.0 suspend ordering has been
  856. * requested.
  857. */
  858. static int acpi_hibernation_begin_old(pm_message_t stage)
  859. {
  860. int error;
  861. /*
  862. * The _TTS object should always be evaluated before the _PTS object.
  863. * When the old_suspended_ordering is true, the _PTS object is
  864. * evaluated in the acpi_sleep_prepare.
  865. */
  866. acpi_sleep_tts_switch(ACPI_STATE_S4);
  867. error = acpi_sleep_prepare(ACPI_STATE_S4);
  868. if (error)
  869. return error;
  870. if (!nvs_nosave) {
  871. error = suspend_nvs_alloc();
  872. if (error)
  873. return error;
  874. }
  875. if (stage.event == PM_EVENT_HIBERNATE)
  876. pm_set_suspend_via_firmware();
  877. acpi_target_sleep_state = ACPI_STATE_S4;
  878. acpi_scan_lock_acquire();
  879. return 0;
  880. }
  881. /*
  882. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  883. * been requested.
  884. */
  885. static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
  886. .begin = acpi_hibernation_begin_old,
  887. .end = acpi_pm_end,
  888. .pre_snapshot = acpi_pm_pre_suspend,
  889. .prepare = acpi_pm_freeze,
  890. .finish = acpi_pm_finish,
  891. .enter = acpi_hibernation_enter,
  892. .leave = acpi_hibernation_leave,
  893. .pre_restore = acpi_pm_freeze,
  894. .restore_cleanup = acpi_pm_thaw,
  895. .recover = acpi_pm_finish,
  896. };
  897. static void acpi_sleep_hibernate_setup(void)
  898. {
  899. if (!acpi_sleep_state_supported(ACPI_STATE_S4))
  900. return;
  901. hibernation_set_ops(old_suspend_ordering ?
  902. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  903. sleep_states[ACPI_STATE_S4] = 1;
  904. if (!acpi_check_s4_hw_signature)
  905. return;
  906. acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
  907. if (facs) {
  908. /*
  909. * s4_hardware_signature is the local variable which is just
  910. * used to warn about mismatch after we're attempting to
  911. * resume (in violation of the ACPI specification.)
  912. */
  913. s4_hardware_signature = facs->hardware_signature;
  914. if (acpi_check_s4_hw_signature > 0) {
  915. /*
  916. * If we're actually obeying the ACPI specification
  917. * then the signature is written out as part of the
  918. * swsusp header, in order to allow the boot kernel
  919. * to gracefully decline to resume.
  920. */
  921. swsusp_hardware_signature = facs->hardware_signature;
  922. }
  923. }
  924. }
  925. #else /* !CONFIG_HIBERNATION */
  926. static inline void acpi_sleep_hibernate_setup(void) {}
  927. #endif /* !CONFIG_HIBERNATION */
  928. static int acpi_power_off_prepare(struct sys_off_data *data)
  929. {
  930. /* Prepare to power off the system */
  931. acpi_sleep_prepare(ACPI_STATE_S5);
  932. acpi_disable_all_gpes();
  933. acpi_os_wait_events_complete();
  934. return NOTIFY_DONE;
  935. }
  936. static int acpi_power_off(struct sys_off_data *data)
  937. {
  938. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  939. pr_debug("%s called\n", __func__);
  940. local_irq_disable();
  941. acpi_enter_sleep_state(ACPI_STATE_S5);
  942. return NOTIFY_DONE;
  943. }
  944. int __init acpi_sleep_init(void)
  945. {
  946. char supported[ACPI_S_STATE_COUNT * 3 + 1];
  947. char *pos = supported;
  948. int i;
  949. acpi_sleep_dmi_check();
  950. sleep_states[ACPI_STATE_S0] = 1;
  951. acpi_sleep_syscore_init();
  952. acpi_sleep_suspend_setup();
  953. acpi_sleep_hibernate_setup();
  954. if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
  955. sleep_states[ACPI_STATE_S5] = 1;
  956. register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE,
  957. SYS_OFF_PRIO_FIRMWARE,
  958. acpi_power_off_prepare, NULL);
  959. register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
  960. SYS_OFF_PRIO_FIRMWARE,
  961. acpi_power_off, NULL);
  962. /*
  963. * Windows uses S5 for reboot, so some BIOSes depend on it to
  964. * perform proper reboot.
  965. */
  966. register_sys_off_handler(SYS_OFF_MODE_RESTART_PREPARE,
  967. SYS_OFF_PRIO_FIRMWARE,
  968. acpi_power_off_prepare, NULL);
  969. } else {
  970. acpi_no_s5 = true;
  971. }
  972. supported[0] = 0;
  973. for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
  974. if (sleep_states[i])
  975. pos += sprintf(pos, " S%d", i);
  976. }
  977. pr_info("(supports%s)\n", supported);
  978. /*
  979. * Register the tts_notifier to reboot notifier list so that the _TTS
  980. * object can also be evaluated when the system enters S5.
  981. */
  982. register_reboot_notifier(&tts_notifier);
  983. return 0;
  984. }