sleep.c 22 KB

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