sleep.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sleep.c - ACPI sleep support.
  4. *
  5. * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
  6. * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
  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. * The ASUS ROG M16 from 2023 has many events which wake it from s2idle
  327. * resulting in excessive battery drain and risk of laptop overheating,
  328. * these events can be caused by the MMC or y AniMe display if installed.
  329. * The match is valid for all of the GU604V<x> range.
  330. */
  331. {
  332. .callback = init_default_s3,
  333. .ident = "ASUS ROG Zephyrus M16 (2023)",
  334. .matches = {
  335. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  336. DMI_MATCH(DMI_PRODUCT_NAME, "ROG Zephyrus M16 GU604V"),
  337. },
  338. },
  339. /*
  340. * https://bugzilla.kernel.org/show_bug.cgi?id=189431
  341. * Lenovo G50-45 is a platform later than 2012, but needs nvs memory
  342. * saving during S3.
  343. */
  344. {
  345. .callback = init_nvs_save_s3,
  346. .ident = "Lenovo G50-45",
  347. .matches = {
  348. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  349. DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
  350. },
  351. },
  352. {
  353. .callback = init_nvs_save_s3,
  354. .ident = "Lenovo G40-45",
  355. .matches = {
  356. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  357. DMI_MATCH(DMI_PRODUCT_NAME, "80E1"),
  358. },
  359. },
  360. /*
  361. * ThinkPad X1 Tablet(2016) cannot do suspend-to-idle using
  362. * the Low Power S0 Idle firmware interface (see
  363. * https://bugzilla.kernel.org/show_bug.cgi?id=199057).
  364. */
  365. {
  366. .callback = init_default_s3,
  367. .ident = "ThinkPad X1 Tablet(2016)",
  368. .matches = {
  369. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  370. DMI_MATCH(DMI_PRODUCT_NAME, "20GGA00L00"),
  371. },
  372. },
  373. {},
  374. };
  375. static bool ignore_blacklist;
  376. void __init acpi_sleep_no_blacklist(void)
  377. {
  378. ignore_blacklist = true;
  379. }
  380. static void __init acpi_sleep_dmi_check(void)
  381. {
  382. if (ignore_blacklist)
  383. return;
  384. if (dmi_get_bios_year() >= 2012)
  385. acpi_nvs_nosave_s3();
  386. dmi_check_system(acpisleep_dmi_table);
  387. }
  388. /**
  389. * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
  390. */
  391. static int acpi_pm_freeze(void)
  392. {
  393. acpi_disable_all_gpes();
  394. acpi_os_wait_events_complete();
  395. acpi_ec_block_transactions();
  396. return 0;
  397. }
  398. /**
  399. * acpi_pm_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
  400. */
  401. static int acpi_pm_pre_suspend(void)
  402. {
  403. acpi_pm_freeze();
  404. return suspend_nvs_save();
  405. }
  406. /**
  407. * __acpi_pm_prepare - Prepare the platform to enter the target state.
  408. *
  409. * If necessary, set the firmware waking vector and do arch-specific
  410. * nastiness to get the wakeup code to the waking vector.
  411. */
  412. static int __acpi_pm_prepare(void)
  413. {
  414. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  415. if (error)
  416. acpi_target_sleep_state = ACPI_STATE_S0;
  417. return error;
  418. }
  419. /**
  420. * acpi_pm_prepare - Prepare the platform to enter the target sleep
  421. * state and disable the GPEs.
  422. */
  423. static int acpi_pm_prepare(void)
  424. {
  425. int error = __acpi_pm_prepare();
  426. if (!error)
  427. error = acpi_pm_pre_suspend();
  428. return error;
  429. }
  430. /**
  431. * acpi_pm_finish - Instruct the platform to leave a sleep state.
  432. *
  433. * This is called after we wake back up (or if entering the sleep state
  434. * failed).
  435. */
  436. static void acpi_pm_finish(void)
  437. {
  438. struct acpi_device *pwr_btn_adev;
  439. u32 acpi_state = acpi_target_sleep_state;
  440. acpi_ec_unblock_transactions();
  441. suspend_nvs_free();
  442. if (acpi_state == ACPI_STATE_S0)
  443. return;
  444. pr_info("Waking up from system sleep state S%d\n", acpi_state);
  445. acpi_disable_wakeup_devices(acpi_state);
  446. acpi_leave_sleep_state(acpi_state);
  447. /* reset firmware waking vector */
  448. acpi_set_waking_vector(0);
  449. acpi_target_sleep_state = ACPI_STATE_S0;
  450. acpi_resume_power_resources();
  451. /* If we were woken with the fixed power button, provide a small
  452. * hint to userspace in the form of a wakeup event on the fixed power
  453. * button device (if it can be found).
  454. *
  455. * We delay the event generation til now, as the PM layer requires
  456. * timekeeping to be running before we generate events. */
  457. if (!pwr_btn_event_pending)
  458. return;
  459. pwr_btn_event_pending = false;
  460. pwr_btn_adev = acpi_dev_get_first_match_dev(ACPI_BUTTON_HID_POWERF,
  461. NULL, -1);
  462. if (pwr_btn_adev) {
  463. pm_wakeup_event(&pwr_btn_adev->dev, 0);
  464. acpi_dev_put(pwr_btn_adev);
  465. }
  466. }
  467. /**
  468. * acpi_pm_start - Start system PM transition.
  469. * @acpi_state: The target ACPI power state to transition to.
  470. */
  471. static void acpi_pm_start(u32 acpi_state)
  472. {
  473. acpi_target_sleep_state = acpi_state;
  474. acpi_sleep_tts_switch(acpi_target_sleep_state);
  475. acpi_scan_lock_acquire();
  476. }
  477. /**
  478. * acpi_pm_end - Finish up system PM transition.
  479. */
  480. static void acpi_pm_end(void)
  481. {
  482. acpi_turn_off_unused_power_resources();
  483. acpi_scan_lock_release();
  484. /*
  485. * This is necessary in case acpi_pm_finish() is not called during a
  486. * failing transition to a sleep state.
  487. */
  488. acpi_target_sleep_state = ACPI_STATE_S0;
  489. acpi_sleep_tts_switch(acpi_target_sleep_state);
  490. }
  491. #else /* !CONFIG_ACPI_SLEEP */
  492. #define sleep_no_lps0 (1)
  493. #define acpi_target_sleep_state ACPI_STATE_S0
  494. #define acpi_sleep_default_s3 (1)
  495. static inline void acpi_sleep_dmi_check(void) {}
  496. #endif /* CONFIG_ACPI_SLEEP */
  497. #ifdef CONFIG_SUSPEND
  498. static u32 acpi_suspend_states[] = {
  499. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  500. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  501. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  502. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  503. };
  504. /**
  505. * acpi_suspend_begin - Set the target system sleep state to the state
  506. * associated with given @pm_state, if supported.
  507. * @pm_state: The target system power management state.
  508. */
  509. static int acpi_suspend_begin(suspend_state_t pm_state)
  510. {
  511. u32 acpi_state = acpi_suspend_states[pm_state];
  512. int error;
  513. error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
  514. if (error)
  515. return error;
  516. if (!sleep_states[acpi_state]) {
  517. pr_err("ACPI does not support sleep state S%u\n", acpi_state);
  518. return -ENOSYS;
  519. }
  520. if (acpi_state > ACPI_STATE_S1)
  521. pm_set_suspend_via_firmware();
  522. acpi_pm_start(acpi_state);
  523. return 0;
  524. }
  525. /**
  526. * acpi_suspend_enter - Actually enter a sleep state.
  527. * @pm_state: ignored
  528. *
  529. * Flush caches and go to sleep. For STR we have to call arch-specific
  530. * assembly, which in turn call acpi_enter_sleep_state().
  531. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  532. */
  533. static int acpi_suspend_enter(suspend_state_t pm_state)
  534. {
  535. acpi_status status = AE_OK;
  536. u32 acpi_state = acpi_target_sleep_state;
  537. int error;
  538. trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
  539. switch (acpi_state) {
  540. case ACPI_STATE_S1:
  541. barrier();
  542. status = acpi_enter_sleep_state(acpi_state);
  543. break;
  544. case ACPI_STATE_S3:
  545. if (!acpi_suspend_lowlevel)
  546. return -ENOSYS;
  547. error = acpi_suspend_lowlevel();
  548. if (error)
  549. return error;
  550. pr_info("Low-level resume complete\n");
  551. pm_set_resume_via_firmware();
  552. break;
  553. }
  554. trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
  555. /* This violates the spec but is required for bug compatibility. */
  556. acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
  557. /* Reprogram control registers */
  558. acpi_leave_sleep_state_prep(acpi_state);
  559. /* ACPI 3.0 specs (P62) says that it's the responsibility
  560. * of the OSPM to clear the status bit [ implying that the
  561. * POWER_BUTTON event should not reach userspace ]
  562. *
  563. * However, we do generate a small hint for userspace in the form of
  564. * a wakeup event. We flag this condition for now and generate the
  565. * event later, as we're currently too early in resume to be able to
  566. * generate wakeup events.
  567. */
  568. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
  569. acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
  570. acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
  571. if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
  572. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  573. /* Flag for later */
  574. pwr_btn_event_pending = true;
  575. }
  576. }
  577. /*
  578. * Disable all GPE and clear their status bits before interrupts are
  579. * enabled. Some GPEs (like wakeup GPEs) have no handlers and this can
  580. * prevent them from producing spurious interrups.
  581. *
  582. * acpi_leave_sleep_state() will reenable specific GPEs later.
  583. *
  584. * Because this code runs on one CPU with disabled interrupts (all of
  585. * the other CPUs are offline at this time), it need not acquire any
  586. * sleeping locks which may trigger an implicit preemption point even
  587. * if there is no contention, so avoid doing that by using a low-level
  588. * library routine here.
  589. */
  590. acpi_hw_disable_all_gpes();
  591. /* Allow EC transactions to happen. */
  592. acpi_ec_unblock_transactions();
  593. suspend_nvs_restore();
  594. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  595. }
  596. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  597. {
  598. u32 acpi_state;
  599. switch (pm_state) {
  600. case PM_SUSPEND_ON:
  601. case PM_SUSPEND_STANDBY:
  602. case PM_SUSPEND_MEM:
  603. acpi_state = acpi_suspend_states[pm_state];
  604. return sleep_states[acpi_state];
  605. default:
  606. return 0;
  607. }
  608. }
  609. static const struct platform_suspend_ops acpi_suspend_ops = {
  610. .valid = acpi_suspend_state_valid,
  611. .begin = acpi_suspend_begin,
  612. .prepare_late = acpi_pm_prepare,
  613. .enter = acpi_suspend_enter,
  614. .wake = acpi_pm_finish,
  615. .end = acpi_pm_end,
  616. };
  617. /**
  618. * acpi_suspend_begin_old - Set the target system sleep state to the
  619. * state associated with given @pm_state, if supported, and
  620. * execute the _PTS control method. This function is used if the
  621. * pre-ACPI 2.0 suspend ordering has been requested.
  622. * @pm_state: The target suspend state for the system.
  623. */
  624. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  625. {
  626. int error = acpi_suspend_begin(pm_state);
  627. if (!error)
  628. error = __acpi_pm_prepare();
  629. return error;
  630. }
  631. /*
  632. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  633. * been requested.
  634. */
  635. static const struct platform_suspend_ops acpi_suspend_ops_old = {
  636. .valid = acpi_suspend_state_valid,
  637. .begin = acpi_suspend_begin_old,
  638. .prepare_late = acpi_pm_pre_suspend,
  639. .enter = acpi_suspend_enter,
  640. .wake = acpi_pm_finish,
  641. .end = acpi_pm_end,
  642. .recover = acpi_pm_finish,
  643. };
  644. static bool s2idle_wakeup;
  645. int acpi_s2idle_begin(void)
  646. {
  647. acpi_scan_lock_acquire();
  648. return 0;
  649. }
  650. int acpi_s2idle_prepare(void)
  651. {
  652. if (acpi_sci_irq_valid()) {
  653. int error;
  654. error = enable_irq_wake(acpi_sci_irq);
  655. if (error)
  656. pr_warn("Warning: Failed to enable wakeup from IRQ %d: %d\n",
  657. acpi_sci_irq, error);
  658. acpi_ec_set_gpe_wake_mask(ACPI_GPE_ENABLE);
  659. }
  660. acpi_enable_wakeup_devices(ACPI_STATE_S0);
  661. /* Change the configuration of GPEs to avoid spurious wakeup. */
  662. acpi_enable_all_wakeup_gpes();
  663. acpi_os_wait_events_complete();
  664. s2idle_wakeup = true;
  665. return 0;
  666. }
  667. bool acpi_s2idle_wake(void)
  668. {
  669. if (!acpi_sci_irq_valid())
  670. return pm_wakeup_pending();
  671. while (pm_wakeup_pending()) {
  672. /*
  673. * If IRQD_WAKEUP_ARMED is set for the SCI at this point, the
  674. * SCI has not triggered while suspended, so bail out (the
  675. * wakeup is pending anyway and the SCI is not the source of
  676. * it).
  677. */
  678. if (irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq))) {
  679. pm_pr_dbg("Wakeup unrelated to ACPI SCI\n");
  680. return true;
  681. }
  682. /*
  683. * If the status bit of any enabled fixed event is set, the
  684. * wakeup is regarded as valid.
  685. */
  686. if (acpi_any_fixed_event_status_set()) {
  687. pm_pr_dbg("ACPI fixed event wakeup\n");
  688. return true;
  689. }
  690. /* Check wakeups from drivers sharing the SCI. */
  691. if (acpi_check_wakeup_handlers()) {
  692. pm_pr_dbg("ACPI custom handler wakeup\n");
  693. return true;
  694. }
  695. /*
  696. * Check non-EC GPE wakeups and if there are none, cancel the
  697. * SCI-related wakeup and dispatch the EC GPE.
  698. */
  699. if (acpi_ec_dispatch_gpe()) {
  700. pm_pr_dbg("ACPI non-EC GPE wakeup\n");
  701. return true;
  702. }
  703. acpi_os_wait_events_complete();
  704. /*
  705. * The SCI is in the "suspended" state now and it cannot produce
  706. * new wakeup events till the rearming below, so if any of them
  707. * are pending here, they must be resulting from the processing
  708. * of EC events above or coming from somewhere else.
  709. */
  710. if (pm_wakeup_pending()) {
  711. pm_pr_dbg("Wakeup after ACPI Notify sync\n");
  712. return true;
  713. }
  714. pm_pr_dbg("Rearming ACPI SCI for wakeup\n");
  715. pm_wakeup_clear(acpi_sci_irq);
  716. rearm_wake_irq(acpi_sci_irq);
  717. }
  718. return false;
  719. }
  720. void acpi_s2idle_restore(void)
  721. {
  722. /*
  723. * Drain pending events before restoring the working-state configuration
  724. * of GPEs.
  725. */
  726. acpi_os_wait_events_complete(); /* synchronize GPE processing */
  727. acpi_ec_flush_work(); /* flush the EC driver's workqueues */
  728. acpi_os_wait_events_complete(); /* synchronize Notify handling */
  729. s2idle_wakeup = false;
  730. acpi_enable_all_runtime_gpes();
  731. acpi_disable_wakeup_devices(ACPI_STATE_S0);
  732. if (acpi_sci_irq_valid()) {
  733. acpi_ec_set_gpe_wake_mask(ACPI_GPE_DISABLE);
  734. disable_irq_wake(acpi_sci_irq);
  735. }
  736. }
  737. void acpi_s2idle_end(void)
  738. {
  739. acpi_scan_lock_release();
  740. }
  741. static const struct platform_s2idle_ops acpi_s2idle_ops = {
  742. .begin = acpi_s2idle_begin,
  743. .prepare = acpi_s2idle_prepare,
  744. .wake = acpi_s2idle_wake,
  745. .restore = acpi_s2idle_restore,
  746. .end = acpi_s2idle_end,
  747. };
  748. void __weak acpi_s2idle_setup(void)
  749. {
  750. if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
  751. pr_info("Efficient low-power S0 idle declared\n");
  752. s2idle_set_ops(&acpi_s2idle_ops);
  753. }
  754. static void __init acpi_sleep_suspend_setup(void)
  755. {
  756. bool suspend_ops_needed = false;
  757. int i;
  758. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
  759. if (acpi_sleep_state_supported(i)) {
  760. sleep_states[i] = 1;
  761. suspend_ops_needed = true;
  762. }
  763. if (suspend_ops_needed)
  764. suspend_set_ops(old_suspend_ordering ?
  765. &acpi_suspend_ops_old : &acpi_suspend_ops);
  766. acpi_s2idle_setup();
  767. }
  768. #else /* !CONFIG_SUSPEND */
  769. #define s2idle_wakeup (false)
  770. static inline void acpi_sleep_suspend_setup(void) {}
  771. #endif /* !CONFIG_SUSPEND */
  772. bool acpi_s2idle_wakeup(void)
  773. {
  774. return s2idle_wakeup;
  775. }
  776. #ifdef CONFIG_PM_SLEEP
  777. static u32 saved_bm_rld;
  778. static int acpi_save_bm_rld(void)
  779. {
  780. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
  781. return 0;
  782. }
  783. static void acpi_restore_bm_rld(void)
  784. {
  785. u32 resumed_bm_rld = 0;
  786. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
  787. if (resumed_bm_rld == saved_bm_rld)
  788. return;
  789. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
  790. }
  791. static struct syscore_ops acpi_sleep_syscore_ops = {
  792. .suspend = acpi_save_bm_rld,
  793. .resume = acpi_restore_bm_rld,
  794. };
  795. static void acpi_sleep_syscore_init(void)
  796. {
  797. register_syscore_ops(&acpi_sleep_syscore_ops);
  798. }
  799. #else
  800. static inline void acpi_sleep_syscore_init(void) {}
  801. #endif /* CONFIG_PM_SLEEP */
  802. #ifdef CONFIG_HIBERNATION
  803. static unsigned long s4_hardware_signature;
  804. static struct acpi_table_facs *facs;
  805. int acpi_check_s4_hw_signature = -1; /* Default behaviour is just to warn */
  806. static int acpi_hibernation_begin(pm_message_t stage)
  807. {
  808. if (!nvs_nosave) {
  809. int error = suspend_nvs_alloc();
  810. if (error)
  811. return error;
  812. }
  813. if (stage.event == PM_EVENT_HIBERNATE)
  814. pm_set_suspend_via_firmware();
  815. acpi_pm_start(ACPI_STATE_S4);
  816. return 0;
  817. }
  818. static int acpi_hibernation_enter(void)
  819. {
  820. acpi_status status = AE_OK;
  821. /* This shouldn't return. If it returns, we have a problem */
  822. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  823. /* Reprogram control registers */
  824. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  825. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  826. }
  827. static void acpi_hibernation_leave(void)
  828. {
  829. pm_set_resume_via_firmware();
  830. /*
  831. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  832. * enable it here.
  833. */
  834. acpi_enable();
  835. /* Reprogram control registers */
  836. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  837. /* Check the hardware signature */
  838. if (facs && s4_hardware_signature != facs->hardware_signature)
  839. pr_crit("Hardware changed while hibernated, success doubtful!\n");
  840. /* Restore the NVS memory area */
  841. suspend_nvs_restore();
  842. /* Allow EC transactions to happen. */
  843. acpi_ec_unblock_transactions();
  844. }
  845. static void acpi_pm_thaw(void)
  846. {
  847. acpi_ec_unblock_transactions();
  848. acpi_enable_all_runtime_gpes();
  849. }
  850. static const struct platform_hibernation_ops acpi_hibernation_ops = {
  851. .begin = acpi_hibernation_begin,
  852. .end = acpi_pm_end,
  853. .pre_snapshot = acpi_pm_prepare,
  854. .finish = acpi_pm_finish,
  855. .prepare = acpi_pm_prepare,
  856. .enter = acpi_hibernation_enter,
  857. .leave = acpi_hibernation_leave,
  858. .pre_restore = acpi_pm_freeze,
  859. .restore_cleanup = acpi_pm_thaw,
  860. };
  861. /**
  862. * acpi_hibernation_begin_old - Set the target system sleep state to
  863. * ACPI_STATE_S4 and execute the _PTS control method. This
  864. * function is used if the pre-ACPI 2.0 suspend ordering has been
  865. * requested.
  866. * @stage: The power management event message.
  867. */
  868. static int acpi_hibernation_begin_old(pm_message_t stage)
  869. {
  870. int error;
  871. /*
  872. * The _TTS object should always be evaluated before the _PTS object.
  873. * When the old_suspended_ordering is true, the _PTS object is
  874. * evaluated in the acpi_sleep_prepare.
  875. */
  876. acpi_sleep_tts_switch(ACPI_STATE_S4);
  877. error = acpi_sleep_prepare(ACPI_STATE_S4);
  878. if (error)
  879. return error;
  880. if (!nvs_nosave) {
  881. error = suspend_nvs_alloc();
  882. if (error)
  883. return error;
  884. }
  885. if (stage.event == PM_EVENT_HIBERNATE)
  886. pm_set_suspend_via_firmware();
  887. acpi_target_sleep_state = ACPI_STATE_S4;
  888. acpi_scan_lock_acquire();
  889. return 0;
  890. }
  891. /*
  892. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  893. * been requested.
  894. */
  895. static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
  896. .begin = acpi_hibernation_begin_old,
  897. .end = acpi_pm_end,
  898. .pre_snapshot = acpi_pm_pre_suspend,
  899. .prepare = acpi_pm_freeze,
  900. .finish = acpi_pm_finish,
  901. .enter = acpi_hibernation_enter,
  902. .leave = acpi_hibernation_leave,
  903. .pre_restore = acpi_pm_freeze,
  904. .restore_cleanup = acpi_pm_thaw,
  905. .recover = acpi_pm_finish,
  906. };
  907. static void acpi_sleep_hibernate_setup(void)
  908. {
  909. if (!acpi_sleep_state_supported(ACPI_STATE_S4))
  910. return;
  911. hibernation_set_ops(old_suspend_ordering ?
  912. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  913. sleep_states[ACPI_STATE_S4] = 1;
  914. if (!acpi_check_s4_hw_signature)
  915. return;
  916. acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
  917. if (facs) {
  918. /*
  919. * s4_hardware_signature is the local variable which is just
  920. * used to warn about mismatch after we're attempting to
  921. * resume (in violation of the ACPI specification.)
  922. */
  923. s4_hardware_signature = facs->hardware_signature;
  924. if (acpi_check_s4_hw_signature > 0) {
  925. /*
  926. * If we're actually obeying the ACPI specification
  927. * then the signature is written out as part of the
  928. * swsusp header, in order to allow the boot kernel
  929. * to gracefully decline to resume.
  930. */
  931. swsusp_hardware_signature = facs->hardware_signature;
  932. }
  933. }
  934. }
  935. #else /* !CONFIG_HIBERNATION */
  936. static inline void acpi_sleep_hibernate_setup(void) {}
  937. #endif /* !CONFIG_HIBERNATION */
  938. static int acpi_power_off_prepare(struct sys_off_data *data)
  939. {
  940. /* Prepare to power off the system */
  941. acpi_sleep_prepare(ACPI_STATE_S5);
  942. acpi_disable_all_gpes();
  943. acpi_os_wait_events_complete();
  944. return NOTIFY_DONE;
  945. }
  946. static int acpi_power_off(struct sys_off_data *data)
  947. {
  948. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  949. pr_debug("%s called\n", __func__);
  950. local_irq_disable();
  951. acpi_enter_sleep_state(ACPI_STATE_S5);
  952. return NOTIFY_DONE;
  953. }
  954. int __init acpi_sleep_init(void)
  955. {
  956. char supported[ACPI_S_STATE_COUNT * 3 + 1];
  957. char *pos = supported;
  958. int i;
  959. acpi_sleep_dmi_check();
  960. sleep_states[ACPI_STATE_S0] = 1;
  961. acpi_sleep_syscore_init();
  962. acpi_sleep_suspend_setup();
  963. acpi_sleep_hibernate_setup();
  964. if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
  965. sleep_states[ACPI_STATE_S5] = 1;
  966. register_sys_off_handler(SYS_OFF_MODE_POWER_OFF_PREPARE,
  967. SYS_OFF_PRIO_FIRMWARE,
  968. acpi_power_off_prepare, NULL);
  969. register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
  970. SYS_OFF_PRIO_FIRMWARE,
  971. acpi_power_off, NULL);
  972. /*
  973. * Windows uses S5 for reboot, so some BIOSes depend on it to
  974. * perform proper reboot.
  975. */
  976. register_sys_off_handler(SYS_OFF_MODE_RESTART_PREPARE,
  977. SYS_OFF_PRIO_FIRMWARE,
  978. acpi_power_off_prepare, NULL);
  979. } else {
  980. acpi_no_s5 = true;
  981. }
  982. supported[0] = 0;
  983. for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
  984. if (sleep_states[i])
  985. pos += sprintf(pos, " S%d", i);
  986. }
  987. pr_info("(supports%s)\n", supported);
  988. /*
  989. * Register the tts_notifier to reboot notifier list so that the _TTS
  990. * object can also be evaluated when the system enters S5.
  991. */
  992. register_reboot_notifier(&tts_notifier);
  993. return 0;
  994. }