sleep.c 21 KB

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