sleep.c 19 KB

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