sleep.c 20 KB

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