sleep.c 22 KB

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