sleep.c 21 KB

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