sleep.c 22 KB

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