sleep.c 20 KB

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