sleep.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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 acpisleep_dmi_table[] __initdata = {
  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_start - Start system PM transition.
  382. */
  383. static void acpi_pm_start(u32 acpi_state)
  384. {
  385. acpi_target_sleep_state = acpi_state;
  386. acpi_sleep_tts_switch(acpi_target_sleep_state);
  387. acpi_scan_lock_acquire();
  388. }
  389. /**
  390. * acpi_pm_end - Finish up system PM transition.
  391. */
  392. static void acpi_pm_end(void)
  393. {
  394. acpi_scan_lock_release();
  395. /*
  396. * This is necessary in case acpi_pm_finish() is not called during a
  397. * failing transition to a sleep state.
  398. */
  399. acpi_target_sleep_state = ACPI_STATE_S0;
  400. acpi_sleep_tts_switch(acpi_target_sleep_state);
  401. }
  402. #else /* !CONFIG_ACPI_SLEEP */
  403. #define acpi_target_sleep_state ACPI_STATE_S0
  404. static inline void acpi_sleep_dmi_check(void) {}
  405. #endif /* CONFIG_ACPI_SLEEP */
  406. #ifdef CONFIG_SUSPEND
  407. static u32 acpi_suspend_states[] = {
  408. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  409. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  410. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  411. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  412. };
  413. /**
  414. * acpi_suspend_begin - Set the target system sleep state to the state
  415. * associated with given @pm_state, if supported.
  416. */
  417. static int acpi_suspend_begin(suspend_state_t pm_state)
  418. {
  419. u32 acpi_state = acpi_suspend_states[pm_state];
  420. int error;
  421. error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
  422. if (error)
  423. return error;
  424. if (!sleep_states[acpi_state]) {
  425. pr_err("ACPI does not support sleep state S%u\n", acpi_state);
  426. return -ENOSYS;
  427. }
  428. acpi_pm_start(acpi_state);
  429. return 0;
  430. }
  431. /**
  432. * acpi_suspend_enter - Actually enter a sleep state.
  433. * @pm_state: ignored
  434. *
  435. * Flush caches and go to sleep. For STR we have to call arch-specific
  436. * assembly, which in turn call acpi_enter_sleep_state().
  437. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  438. */
  439. static int acpi_suspend_enter(suspend_state_t pm_state)
  440. {
  441. acpi_status status = AE_OK;
  442. u32 acpi_state = acpi_target_sleep_state;
  443. int error;
  444. ACPI_FLUSH_CPU_CACHE();
  445. switch (acpi_state) {
  446. case ACPI_STATE_S1:
  447. barrier();
  448. status = acpi_enter_sleep_state(acpi_state);
  449. break;
  450. case ACPI_STATE_S3:
  451. if (!acpi_suspend_lowlevel)
  452. return -ENOSYS;
  453. error = acpi_suspend_lowlevel();
  454. if (error)
  455. return error;
  456. pr_info(PREFIX "Low-level resume complete\n");
  457. break;
  458. }
  459. /* This violates the spec but is required for bug compatibility. */
  460. acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
  461. /* Reprogram control registers */
  462. acpi_leave_sleep_state_prep(acpi_state);
  463. /* ACPI 3.0 specs (P62) says that it's the responsibility
  464. * of the OSPM to clear the status bit [ implying that the
  465. * POWER_BUTTON event should not reach userspace ]
  466. *
  467. * However, we do generate a small hint for userspace in the form of
  468. * a wakeup event. We flag this condition for now and generate the
  469. * event later, as we're currently too early in resume to be able to
  470. * generate wakeup events.
  471. */
  472. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
  473. acpi_event_status pwr_btn_status;
  474. acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
  475. if (pwr_btn_status & ACPI_EVENT_FLAG_SET) {
  476. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  477. /* Flag for later */
  478. pwr_btn_event_pending = true;
  479. }
  480. }
  481. /*
  482. * Disable and clear GPE status before interrupt is enabled. Some GPEs
  483. * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
  484. * acpi_leave_sleep_state will reenable specific GPEs later
  485. */
  486. acpi_disable_all_gpes();
  487. /* Allow EC transactions to happen. */
  488. acpi_ec_unblock_transactions_early();
  489. suspend_nvs_restore();
  490. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  491. }
  492. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  493. {
  494. u32 acpi_state;
  495. switch (pm_state) {
  496. case PM_SUSPEND_ON:
  497. case PM_SUSPEND_STANDBY:
  498. case PM_SUSPEND_MEM:
  499. acpi_state = acpi_suspend_states[pm_state];
  500. return sleep_states[acpi_state];
  501. default:
  502. return 0;
  503. }
  504. }
  505. static const struct platform_suspend_ops acpi_suspend_ops = {
  506. .valid = acpi_suspend_state_valid,
  507. .begin = acpi_suspend_begin,
  508. .prepare_late = acpi_pm_prepare,
  509. .enter = acpi_suspend_enter,
  510. .wake = acpi_pm_finish,
  511. .end = acpi_pm_end,
  512. };
  513. /**
  514. * acpi_suspend_begin_old - Set the target system sleep state to the
  515. * state associated with given @pm_state, if supported, and
  516. * execute the _PTS control method. This function is used if the
  517. * pre-ACPI 2.0 suspend ordering has been requested.
  518. */
  519. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  520. {
  521. int error = acpi_suspend_begin(pm_state);
  522. if (!error)
  523. error = __acpi_pm_prepare();
  524. return error;
  525. }
  526. /*
  527. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  528. * been requested.
  529. */
  530. static const struct platform_suspend_ops acpi_suspend_ops_old = {
  531. .valid = acpi_suspend_state_valid,
  532. .begin = acpi_suspend_begin_old,
  533. .prepare_late = acpi_pm_pre_suspend,
  534. .enter = acpi_suspend_enter,
  535. .wake = acpi_pm_finish,
  536. .end = acpi_pm_end,
  537. .recover = acpi_pm_finish,
  538. };
  539. static void acpi_sleep_suspend_setup(void)
  540. {
  541. int i;
  542. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
  543. acpi_status status;
  544. u8 type_a, type_b;
  545. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  546. if (ACPI_SUCCESS(status)) {
  547. sleep_states[i] = 1;
  548. }
  549. }
  550. suspend_set_ops(old_suspend_ordering ?
  551. &acpi_suspend_ops_old : &acpi_suspend_ops);
  552. }
  553. #else /* !CONFIG_SUSPEND */
  554. static inline void acpi_sleep_suspend_setup(void) {}
  555. #endif /* !CONFIG_SUSPEND */
  556. #ifdef CONFIG_HIBERNATION
  557. static unsigned long s4_hardware_signature;
  558. static struct acpi_table_facs *facs;
  559. static bool nosigcheck;
  560. void __init acpi_no_s4_hw_signature(void)
  561. {
  562. nosigcheck = true;
  563. }
  564. static int acpi_hibernation_begin(void)
  565. {
  566. int error;
  567. error = nvs_nosave ? 0 : suspend_nvs_alloc();
  568. if (!error)
  569. acpi_pm_start(ACPI_STATE_S4);
  570. return error;
  571. }
  572. static int acpi_hibernation_enter(void)
  573. {
  574. acpi_status status = AE_OK;
  575. ACPI_FLUSH_CPU_CACHE();
  576. /* This shouldn't return. If it returns, we have a problem */
  577. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  578. /* Reprogram control registers */
  579. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  580. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  581. }
  582. static void acpi_hibernation_leave(void)
  583. {
  584. /*
  585. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  586. * enable it here.
  587. */
  588. acpi_enable();
  589. /* Reprogram control registers */
  590. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  591. /* Check the hardware signature */
  592. if (facs && s4_hardware_signature != facs->hardware_signature) {
  593. printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
  594. "cannot resume!\n");
  595. panic("ACPI S4 hardware signature mismatch");
  596. }
  597. /* Restore the NVS memory area */
  598. suspend_nvs_restore();
  599. /* Allow EC transactions to happen. */
  600. acpi_ec_unblock_transactions_early();
  601. }
  602. static void acpi_pm_thaw(void)
  603. {
  604. acpi_ec_unblock_transactions();
  605. acpi_enable_all_runtime_gpes();
  606. }
  607. static const struct platform_hibernation_ops acpi_hibernation_ops = {
  608. .begin = acpi_hibernation_begin,
  609. .end = acpi_pm_end,
  610. .pre_snapshot = acpi_pm_prepare,
  611. .finish = acpi_pm_finish,
  612. .prepare = acpi_pm_prepare,
  613. .enter = acpi_hibernation_enter,
  614. .leave = acpi_hibernation_leave,
  615. .pre_restore = acpi_pm_freeze,
  616. .restore_cleanup = acpi_pm_thaw,
  617. };
  618. /**
  619. * acpi_hibernation_begin_old - Set the target system sleep state to
  620. * ACPI_STATE_S4 and execute the _PTS control method. This
  621. * function is used if the pre-ACPI 2.0 suspend ordering has been
  622. * requested.
  623. */
  624. static int acpi_hibernation_begin_old(void)
  625. {
  626. int error;
  627. /*
  628. * The _TTS object should always be evaluated before the _PTS object.
  629. * When the old_suspended_ordering is true, the _PTS object is
  630. * evaluated in the acpi_sleep_prepare.
  631. */
  632. acpi_sleep_tts_switch(ACPI_STATE_S4);
  633. error = acpi_sleep_prepare(ACPI_STATE_S4);
  634. if (!error) {
  635. if (!nvs_nosave)
  636. error = suspend_nvs_alloc();
  637. if (!error) {
  638. acpi_target_sleep_state = ACPI_STATE_S4;
  639. acpi_scan_lock_acquire();
  640. }
  641. }
  642. return error;
  643. }
  644. /*
  645. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  646. * been requested.
  647. */
  648. static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
  649. .begin = acpi_hibernation_begin_old,
  650. .end = acpi_pm_end,
  651. .pre_snapshot = acpi_pm_pre_suspend,
  652. .prepare = acpi_pm_freeze,
  653. .finish = acpi_pm_finish,
  654. .enter = acpi_hibernation_enter,
  655. .leave = acpi_hibernation_leave,
  656. .pre_restore = acpi_pm_freeze,
  657. .restore_cleanup = acpi_pm_thaw,
  658. .recover = acpi_pm_finish,
  659. };
  660. static void acpi_sleep_hibernate_setup(void)
  661. {
  662. acpi_status status;
  663. u8 type_a, type_b;
  664. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  665. if (ACPI_FAILURE(status))
  666. return;
  667. hibernation_set_ops(old_suspend_ordering ?
  668. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  669. sleep_states[ACPI_STATE_S4] = 1;
  670. if (nosigcheck)
  671. return;
  672. acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
  673. if (facs)
  674. s4_hardware_signature = facs->hardware_signature;
  675. }
  676. #else /* !CONFIG_HIBERNATION */
  677. static inline void acpi_sleep_hibernate_setup(void) {}
  678. #endif /* !CONFIG_HIBERNATION */
  679. int acpi_suspend(u32 acpi_state)
  680. {
  681. suspend_state_t states[] = {
  682. [1] = PM_SUSPEND_STANDBY,
  683. [3] = PM_SUSPEND_MEM,
  684. [5] = PM_SUSPEND_MAX
  685. };
  686. if (acpi_state < 6 && states[acpi_state])
  687. return pm_suspend(states[acpi_state]);
  688. if (acpi_state == 4)
  689. return hibernate();
  690. return -EINVAL;
  691. }
  692. static void acpi_power_off_prepare(void)
  693. {
  694. /* Prepare to power off the system */
  695. acpi_sleep_prepare(ACPI_STATE_S5);
  696. acpi_disable_all_gpes();
  697. }
  698. static void acpi_power_off(void)
  699. {
  700. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  701. printk(KERN_DEBUG "%s called\n", __func__);
  702. local_irq_disable();
  703. acpi_enter_sleep_state(ACPI_STATE_S5);
  704. }
  705. int __init acpi_sleep_init(void)
  706. {
  707. acpi_status status;
  708. u8 type_a, type_b;
  709. char supported[ACPI_S_STATE_COUNT * 3 + 1];
  710. char *pos = supported;
  711. int i;
  712. if (acpi_disabled)
  713. return 0;
  714. acpi_sleep_dmi_check();
  715. sleep_states[ACPI_STATE_S0] = 1;
  716. acpi_sleep_suspend_setup();
  717. acpi_sleep_hibernate_setup();
  718. status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
  719. if (ACPI_SUCCESS(status)) {
  720. sleep_states[ACPI_STATE_S5] = 1;
  721. pm_power_off_prepare = acpi_power_off_prepare;
  722. pm_power_off = acpi_power_off;
  723. }
  724. supported[0] = 0;
  725. for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
  726. if (sleep_states[i])
  727. pos += sprintf(pos, " S%d", i);
  728. }
  729. pr_info(PREFIX "(supports%s)\n", supported);
  730. /*
  731. * Register the tts_notifier to reboot notifier list so that the _TTS
  732. * object can also be evaluated when the system enters S5.
  733. */
  734. register_reboot_notifier(&tts_notifier);
  735. return 0;
  736. }