sleep.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 <linux/syscore_ops.h>
  22. #include <asm/io.h>
  23. #include <trace/events/power.h>
  24. #include "internal.h"
  25. #include "sleep.h"
  26. /*
  27. * Some HW-full platforms do not have _S5, so they may need
  28. * to leverage efi power off for a shutdown.
  29. */
  30. bool acpi_no_s5;
  31. static u8 sleep_states[ACPI_S_STATE_COUNT];
  32. static void acpi_sleep_tts_switch(u32 acpi_state)
  33. {
  34. acpi_status status;
  35. status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
  36. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  37. /*
  38. * OS can't evaluate the _TTS object correctly. Some warning
  39. * message will be printed. But it won't break anything.
  40. */
  41. printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
  42. }
  43. }
  44. static void acpi_sleep_pts_switch(u32 acpi_state)
  45. {
  46. acpi_status status;
  47. status = acpi_execute_simple_method(NULL, "\\_PTS", acpi_state);
  48. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  49. /*
  50. * OS can't evaluate the _PTS object correctly. Some warning
  51. * message will be printed. But it won't break anything.
  52. */
  53. printk(KERN_NOTICE "Failure in evaluating _PTS object\n");
  54. }
  55. }
  56. static int sleep_notify_reboot(struct notifier_block *this,
  57. unsigned long code, void *x)
  58. {
  59. acpi_sleep_tts_switch(ACPI_STATE_S5);
  60. acpi_sleep_pts_switch(ACPI_STATE_S5);
  61. return NOTIFY_DONE;
  62. }
  63. static struct notifier_block sleep_notifier = {
  64. .notifier_call = sleep_notify_reboot,
  65. .next = NULL,
  66. .priority = 0,
  67. };
  68. static int acpi_sleep_prepare(u32 acpi_state)
  69. {
  70. #ifdef CONFIG_ACPI_SLEEP
  71. /* do we have a wakeup address for S2 and S3? */
  72. if (acpi_state == ACPI_STATE_S3) {
  73. if (!acpi_wakeup_address)
  74. return -EFAULT;
  75. acpi_set_waking_vector(acpi_wakeup_address);
  76. }
  77. ACPI_FLUSH_CPU_CACHE();
  78. #endif
  79. printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
  80. acpi_state);
  81. acpi_enable_wakeup_devices(acpi_state);
  82. acpi_enter_sleep_state_prep(acpi_state);
  83. return 0;
  84. }
  85. static bool acpi_sleep_state_supported(u8 sleep_state)
  86. {
  87. acpi_status status;
  88. u8 type_a, type_b;
  89. status = acpi_get_sleep_type_data(sleep_state, &type_a, &type_b);
  90. return ACPI_SUCCESS(status) && (!acpi_gbl_reduced_hardware
  91. || (acpi_gbl_FADT.sleep_control.address
  92. && acpi_gbl_FADT.sleep_status.address));
  93. }
  94. #ifdef CONFIG_ACPI_SLEEP
  95. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  96. u32 acpi_target_system_state(void)
  97. {
  98. return acpi_target_sleep_state;
  99. }
  100. EXPORT_SYMBOL_GPL(acpi_target_system_state);
  101. static bool pwr_btn_event_pending;
  102. /*
  103. * The ACPI specification wants us to save NVS memory regions during hibernation
  104. * and to restore them during the subsequent resume. Windows does that also for
  105. * suspend to RAM. However, it is known that this mechanism does not work on
  106. * all machines, so we allow the user to disable it with the help of the
  107. * 'acpi_sleep=nonvs' kernel command line option.
  108. */
  109. static bool nvs_nosave;
  110. void __init acpi_nvs_nosave(void)
  111. {
  112. nvs_nosave = true;
  113. }
  114. /*
  115. * The ACPI specification wants us to save NVS memory regions during hibernation
  116. * but says nothing about saving NVS during S3. Not all versions of Windows
  117. * save NVS on S3 suspend either, and it is clear that not all systems need
  118. * NVS to be saved at S3 time. To improve suspend/resume time, allow the
  119. * user to disable saving NVS on S3 if their system does not require it, but
  120. * continue to save/restore NVS for S4 as specified.
  121. */
  122. static bool nvs_nosave_s3;
  123. void __init acpi_nvs_nosave_s3(void)
  124. {
  125. nvs_nosave_s3 = true;
  126. }
  127. /*
  128. * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
  129. * user to request that behavior by using the 'acpi_old_suspend_ordering'
  130. * kernel command line option that causes the following variable to be set.
  131. */
  132. static bool old_suspend_ordering;
  133. void __init acpi_old_suspend_ordering(void)
  134. {
  135. old_suspend_ordering = true;
  136. }
  137. static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
  138. {
  139. acpi_old_suspend_ordering();
  140. return 0;
  141. }
  142. static int __init init_nvs_nosave(const struct dmi_system_id *d)
  143. {
  144. acpi_nvs_nosave();
  145. return 0;
  146. }
  147. static struct dmi_system_id acpisleep_dmi_table[] __initdata = {
  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. };
  319. static void __init acpi_sleep_dmi_check(void)
  320. {
  321. int year;
  322. if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year >= 2012)
  323. acpi_nvs_nosave_s3();
  324. dmi_check_system(acpisleep_dmi_table);
  325. }
  326. /**
  327. * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
  328. */
  329. static int acpi_pm_freeze(void)
  330. {
  331. acpi_disable_all_gpes();
  332. acpi_os_wait_events_complete();
  333. acpi_ec_block_transactions();
  334. return 0;
  335. }
  336. /**
  337. * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
  338. */
  339. static int acpi_pm_pre_suspend(void)
  340. {
  341. acpi_pm_freeze();
  342. return suspend_nvs_save();
  343. }
  344. /**
  345. * __acpi_pm_prepare - Prepare the platform to enter the target state.
  346. *
  347. * If necessary, set the firmware waking vector and do arch-specific
  348. * nastiness to get the wakeup code to the waking vector.
  349. */
  350. static int __acpi_pm_prepare(void)
  351. {
  352. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  353. if (error)
  354. acpi_target_sleep_state = ACPI_STATE_S0;
  355. return error;
  356. }
  357. /**
  358. * acpi_pm_prepare - Prepare the platform to enter the target sleep
  359. * state and disable the GPEs.
  360. */
  361. static int acpi_pm_prepare(void)
  362. {
  363. int error = __acpi_pm_prepare();
  364. if (!error)
  365. error = acpi_pm_pre_suspend();
  366. return error;
  367. }
  368. static int find_powerf_dev(struct device *dev, void *data)
  369. {
  370. struct acpi_device *device = to_acpi_device(dev);
  371. const char *hid = acpi_device_hid(device);
  372. return !strcmp(hid, ACPI_BUTTON_HID_POWERF);
  373. }
  374. /**
  375. * acpi_pm_finish - Instruct the platform to leave a sleep state.
  376. *
  377. * This is called after we wake back up (or if entering the sleep state
  378. * failed).
  379. */
  380. static void acpi_pm_finish(void)
  381. {
  382. struct device *pwr_btn_dev;
  383. u32 acpi_state = acpi_target_sleep_state;
  384. acpi_ec_unblock_transactions();
  385. suspend_nvs_free();
  386. if (acpi_state == ACPI_STATE_S0)
  387. return;
  388. printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
  389. acpi_state);
  390. acpi_disable_wakeup_devices(acpi_state);
  391. acpi_leave_sleep_state(acpi_state);
  392. /* reset firmware waking vector */
  393. acpi_set_waking_vector(0);
  394. acpi_target_sleep_state = ACPI_STATE_S0;
  395. acpi_resume_power_resources();
  396. /* If we were woken with the fixed power button, provide a small
  397. * hint to userspace in the form of a wakeup event on the fixed power
  398. * button device (if it can be found).
  399. *
  400. * We delay the event generation til now, as the PM layer requires
  401. * timekeeping to be running before we generate events. */
  402. if (!pwr_btn_event_pending)
  403. return;
  404. pwr_btn_event_pending = false;
  405. pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL,
  406. find_powerf_dev);
  407. if (pwr_btn_dev) {
  408. pm_wakeup_event(pwr_btn_dev, 0);
  409. put_device(pwr_btn_dev);
  410. }
  411. }
  412. /**
  413. * acpi_pm_start - Start system PM transition.
  414. */
  415. static void acpi_pm_start(u32 acpi_state)
  416. {
  417. acpi_target_sleep_state = acpi_state;
  418. acpi_sleep_tts_switch(acpi_target_sleep_state);
  419. acpi_scan_lock_acquire();
  420. }
  421. /**
  422. * acpi_pm_end - Finish up system PM transition.
  423. */
  424. static void acpi_pm_end(void)
  425. {
  426. acpi_scan_lock_release();
  427. /*
  428. * This is necessary in case acpi_pm_finish() is not called during a
  429. * failing transition to a sleep state.
  430. */
  431. acpi_target_sleep_state = ACPI_STATE_S0;
  432. acpi_sleep_tts_switch(acpi_target_sleep_state);
  433. }
  434. #else /* !CONFIG_ACPI_SLEEP */
  435. #define acpi_target_sleep_state ACPI_STATE_S0
  436. static inline void acpi_sleep_dmi_check(void) {}
  437. #endif /* CONFIG_ACPI_SLEEP */
  438. #ifdef CONFIG_SUSPEND
  439. static u32 acpi_suspend_states[] = {
  440. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  441. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  442. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  443. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  444. };
  445. /**
  446. * acpi_suspend_begin - Set the target system sleep state to the state
  447. * associated with given @pm_state, if supported.
  448. */
  449. static int acpi_suspend_begin(suspend_state_t pm_state)
  450. {
  451. u32 acpi_state = acpi_suspend_states[pm_state];
  452. int error;
  453. error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
  454. if (error)
  455. return error;
  456. if (!sleep_states[acpi_state]) {
  457. pr_err("ACPI does not support sleep state S%u\n", acpi_state);
  458. return -ENOSYS;
  459. }
  460. if (acpi_state > ACPI_STATE_S1)
  461. pm_set_suspend_via_firmware();
  462. acpi_pm_start(acpi_state);
  463. return 0;
  464. }
  465. /**
  466. * acpi_suspend_enter - Actually enter a sleep state.
  467. * @pm_state: ignored
  468. *
  469. * Flush caches and go to sleep. For STR we have to call arch-specific
  470. * assembly, which in turn call acpi_enter_sleep_state().
  471. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  472. */
  473. static int acpi_suspend_enter(suspend_state_t pm_state)
  474. {
  475. acpi_status status = AE_OK;
  476. u32 acpi_state = acpi_target_sleep_state;
  477. int error;
  478. ACPI_FLUSH_CPU_CACHE();
  479. trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
  480. switch (acpi_state) {
  481. case ACPI_STATE_S1:
  482. barrier();
  483. status = acpi_enter_sleep_state(acpi_state);
  484. break;
  485. case ACPI_STATE_S3:
  486. if (!acpi_suspend_lowlevel)
  487. return -ENOSYS;
  488. error = acpi_suspend_lowlevel();
  489. if (error)
  490. return error;
  491. pr_info(PREFIX "Low-level resume complete\n");
  492. pm_set_resume_via_firmware();
  493. break;
  494. }
  495. trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
  496. /* This violates the spec but is required for bug compatibility. */
  497. acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
  498. /* Reprogram control registers */
  499. acpi_leave_sleep_state_prep(acpi_state);
  500. /* ACPI 3.0 specs (P62) says that it's the responsibility
  501. * of the OSPM to clear the status bit [ implying that the
  502. * POWER_BUTTON event should not reach userspace ]
  503. *
  504. * However, we do generate a small hint for userspace in the form of
  505. * a wakeup event. We flag this condition for now and generate the
  506. * event later, as we're currently too early in resume to be able to
  507. * generate wakeup events.
  508. */
  509. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
  510. acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
  511. acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
  512. if (pwr_btn_status & ACPI_EVENT_FLAG_SET) {
  513. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  514. /* Flag for later */
  515. pwr_btn_event_pending = true;
  516. }
  517. }
  518. /*
  519. * Disable and clear GPE status before interrupt is enabled. Some GPEs
  520. * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
  521. * acpi_leave_sleep_state will reenable specific GPEs later
  522. */
  523. acpi_disable_all_gpes();
  524. /* Allow EC transactions to happen. */
  525. acpi_ec_unblock_transactions_early();
  526. suspend_nvs_restore();
  527. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  528. }
  529. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  530. {
  531. u32 acpi_state;
  532. switch (pm_state) {
  533. case PM_SUSPEND_ON:
  534. case PM_SUSPEND_STANDBY:
  535. case PM_SUSPEND_MEM:
  536. acpi_state = acpi_suspend_states[pm_state];
  537. return sleep_states[acpi_state];
  538. default:
  539. return 0;
  540. }
  541. }
  542. static const struct platform_suspend_ops acpi_suspend_ops = {
  543. .valid = acpi_suspend_state_valid,
  544. .begin = acpi_suspend_begin,
  545. .prepare_late = acpi_pm_prepare,
  546. .enter = acpi_suspend_enter,
  547. .wake = acpi_pm_finish,
  548. .end = acpi_pm_end,
  549. };
  550. /**
  551. * acpi_suspend_begin_old - Set the target system sleep state to the
  552. * state associated with given @pm_state, if supported, and
  553. * execute the _PTS control method. This function is used if the
  554. * pre-ACPI 2.0 suspend ordering has been requested.
  555. */
  556. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  557. {
  558. int error = acpi_suspend_begin(pm_state);
  559. if (!error)
  560. error = __acpi_pm_prepare();
  561. return error;
  562. }
  563. /*
  564. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  565. * been requested.
  566. */
  567. static const struct platform_suspend_ops acpi_suspend_ops_old = {
  568. .valid = acpi_suspend_state_valid,
  569. .begin = acpi_suspend_begin_old,
  570. .prepare_late = acpi_pm_pre_suspend,
  571. .enter = acpi_suspend_enter,
  572. .wake = acpi_pm_finish,
  573. .end = acpi_pm_end,
  574. .recover = acpi_pm_finish,
  575. };
  576. static int acpi_freeze_begin(void)
  577. {
  578. acpi_scan_lock_acquire();
  579. return 0;
  580. }
  581. static int acpi_freeze_prepare(void)
  582. {
  583. acpi_enable_wakeup_devices(ACPI_STATE_S0);
  584. acpi_enable_all_wakeup_gpes();
  585. acpi_os_wait_events_complete();
  586. if (acpi_sci_irq_valid())
  587. enable_irq_wake(acpi_sci_irq);
  588. return 0;
  589. }
  590. static void acpi_freeze_restore(void)
  591. {
  592. acpi_disable_wakeup_devices(ACPI_STATE_S0);
  593. if (acpi_sci_irq_valid())
  594. disable_irq_wake(acpi_sci_irq);
  595. acpi_enable_all_runtime_gpes();
  596. }
  597. static void acpi_freeze_end(void)
  598. {
  599. acpi_scan_lock_release();
  600. }
  601. static const struct platform_freeze_ops acpi_freeze_ops = {
  602. .begin = acpi_freeze_begin,
  603. .prepare = acpi_freeze_prepare,
  604. .restore = acpi_freeze_restore,
  605. .end = acpi_freeze_end,
  606. };
  607. static void acpi_sleep_suspend_setup(void)
  608. {
  609. int i;
  610. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
  611. if (acpi_sleep_state_supported(i))
  612. sleep_states[i] = 1;
  613. suspend_set_ops(old_suspend_ordering ?
  614. &acpi_suspend_ops_old : &acpi_suspend_ops);
  615. freeze_set_ops(&acpi_freeze_ops);
  616. }
  617. #else /* !CONFIG_SUSPEND */
  618. static inline void acpi_sleep_suspend_setup(void) {}
  619. #endif /* !CONFIG_SUSPEND */
  620. #ifdef CONFIG_PM_SLEEP
  621. static u32 saved_bm_rld;
  622. static int acpi_save_bm_rld(void)
  623. {
  624. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
  625. return 0;
  626. }
  627. static void acpi_restore_bm_rld(void)
  628. {
  629. u32 resumed_bm_rld = 0;
  630. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
  631. if (resumed_bm_rld == saved_bm_rld)
  632. return;
  633. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
  634. }
  635. static struct syscore_ops acpi_sleep_syscore_ops = {
  636. .suspend = acpi_save_bm_rld,
  637. .resume = acpi_restore_bm_rld,
  638. };
  639. void acpi_sleep_syscore_init(void)
  640. {
  641. register_syscore_ops(&acpi_sleep_syscore_ops);
  642. }
  643. #else
  644. static inline void acpi_sleep_syscore_init(void) {}
  645. #endif /* CONFIG_PM_SLEEP */
  646. #ifdef CONFIG_HIBERNATION
  647. static unsigned long s4_hardware_signature;
  648. static struct acpi_table_facs *facs;
  649. static bool nosigcheck;
  650. void __init acpi_no_s4_hw_signature(void)
  651. {
  652. nosigcheck = true;
  653. }
  654. static int acpi_hibernation_begin(void)
  655. {
  656. int error;
  657. error = nvs_nosave ? 0 : suspend_nvs_alloc();
  658. if (!error)
  659. acpi_pm_start(ACPI_STATE_S4);
  660. return error;
  661. }
  662. static int acpi_hibernation_enter(void)
  663. {
  664. acpi_status status = AE_OK;
  665. ACPI_FLUSH_CPU_CACHE();
  666. /* This shouldn't return. If it returns, we have a problem */
  667. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  668. /* Reprogram control registers */
  669. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  670. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  671. }
  672. static void acpi_hibernation_leave(void)
  673. {
  674. pm_set_resume_via_firmware();
  675. /*
  676. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  677. * enable it here.
  678. */
  679. acpi_enable();
  680. /* Reprogram control registers */
  681. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  682. /* Check the hardware signature */
  683. if (facs && s4_hardware_signature != facs->hardware_signature)
  684. pr_crit("ACPI: Hardware changed while hibernated, success doubtful!\n");
  685. /* Restore the NVS memory area */
  686. suspend_nvs_restore();
  687. /* Allow EC transactions to happen. */
  688. acpi_ec_unblock_transactions_early();
  689. }
  690. static void acpi_pm_thaw(void)
  691. {
  692. acpi_ec_unblock_transactions();
  693. acpi_enable_all_runtime_gpes();
  694. }
  695. static const struct platform_hibernation_ops acpi_hibernation_ops = {
  696. .begin = acpi_hibernation_begin,
  697. .end = acpi_pm_end,
  698. .pre_snapshot = acpi_pm_prepare,
  699. .finish = acpi_pm_finish,
  700. .prepare = acpi_pm_prepare,
  701. .enter = acpi_hibernation_enter,
  702. .leave = acpi_hibernation_leave,
  703. .pre_restore = acpi_pm_freeze,
  704. .restore_cleanup = acpi_pm_thaw,
  705. };
  706. /**
  707. * acpi_hibernation_begin_old - Set the target system sleep state to
  708. * ACPI_STATE_S4 and execute the _PTS control method. This
  709. * function is used if the pre-ACPI 2.0 suspend ordering has been
  710. * requested.
  711. */
  712. static int acpi_hibernation_begin_old(void)
  713. {
  714. int error;
  715. /*
  716. * The _TTS object should always be evaluated before the _PTS object.
  717. * When the old_suspended_ordering is true, the _PTS object is
  718. * evaluated in the acpi_sleep_prepare.
  719. */
  720. acpi_sleep_tts_switch(ACPI_STATE_S4);
  721. error = acpi_sleep_prepare(ACPI_STATE_S4);
  722. if (!error) {
  723. if (!nvs_nosave)
  724. error = suspend_nvs_alloc();
  725. if (!error) {
  726. acpi_target_sleep_state = ACPI_STATE_S4;
  727. acpi_scan_lock_acquire();
  728. }
  729. }
  730. return error;
  731. }
  732. /*
  733. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  734. * been requested.
  735. */
  736. static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
  737. .begin = acpi_hibernation_begin_old,
  738. .end = acpi_pm_end,
  739. .pre_snapshot = acpi_pm_pre_suspend,
  740. .prepare = acpi_pm_freeze,
  741. .finish = acpi_pm_finish,
  742. .enter = acpi_hibernation_enter,
  743. .leave = acpi_hibernation_leave,
  744. .pre_restore = acpi_pm_freeze,
  745. .restore_cleanup = acpi_pm_thaw,
  746. .recover = acpi_pm_finish,
  747. };
  748. static void acpi_sleep_hibernate_setup(void)
  749. {
  750. if (!acpi_sleep_state_supported(ACPI_STATE_S4))
  751. return;
  752. hibernation_set_ops(old_suspend_ordering ?
  753. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  754. sleep_states[ACPI_STATE_S4] = 1;
  755. if (nosigcheck)
  756. return;
  757. acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
  758. if (facs)
  759. s4_hardware_signature = facs->hardware_signature;
  760. }
  761. #else /* !CONFIG_HIBERNATION */
  762. static inline void acpi_sleep_hibernate_setup(void) {}
  763. #endif /* !CONFIG_HIBERNATION */
  764. static void acpi_power_off_prepare(void)
  765. {
  766. /* Prepare to power off the system */
  767. acpi_sleep_prepare(ACPI_STATE_S5);
  768. acpi_disable_all_gpes();
  769. acpi_os_wait_events_complete();
  770. }
  771. static void acpi_power_off(void)
  772. {
  773. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  774. printk(KERN_DEBUG "%s called\n", __func__);
  775. local_irq_disable();
  776. acpi_enter_sleep_state(ACPI_STATE_S5);
  777. }
  778. int __init acpi_sleep_init(void)
  779. {
  780. char supported[ACPI_S_STATE_COUNT * 3 + 1];
  781. char *pos = supported;
  782. int i;
  783. acpi_sleep_dmi_check();
  784. sleep_states[ACPI_STATE_S0] = 1;
  785. acpi_sleep_syscore_init();
  786. acpi_sleep_suspend_setup();
  787. acpi_sleep_hibernate_setup();
  788. if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
  789. sleep_states[ACPI_STATE_S5] = 1;
  790. pm_power_off_prepare = acpi_power_off_prepare;
  791. pm_power_off = acpi_power_off;
  792. } else {
  793. acpi_no_s5 = true;
  794. }
  795. supported[0] = 0;
  796. for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
  797. if (sleep_states[i])
  798. pos += sprintf(pos, " S%d", i);
  799. }
  800. pr_info(PREFIX "(supports%s)\n", supported);
  801. /*
  802. * Register the sleep_notifier to reboot notifier list so that the _TTS
  803. * and _PTS object can also be evaluated when the system enters S5.
  804. */
  805. register_reboot_notifier(&sleep_notifier);
  806. return 0;
  807. }