sleep.c 21 KB

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