sleep.c 20 KB

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