sleep.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. /*
  27. * Some HW-full platforms do not have _S5, so they may need
  28. * to leverage efi power off for a shutdown.
  29. */
  30. bool acpi_no_s5;
  31. static u8 sleep_states[ACPI_S_STATE_COUNT];
  32. static void acpi_sleep_tts_switch(u32 acpi_state)
  33. {
  34. acpi_status status;
  35. status = acpi_execute_simple_method(NULL, "\\_TTS", acpi_state);
  36. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  37. /*
  38. * OS can't evaluate the _TTS object correctly. Some warning
  39. * message will be printed. But it won't break anything.
  40. */
  41. printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
  42. }
  43. }
  44. static int tts_notify_reboot(struct notifier_block *this,
  45. unsigned long code, void *x)
  46. {
  47. acpi_sleep_tts_switch(ACPI_STATE_S5);
  48. return NOTIFY_DONE;
  49. }
  50. static struct notifier_block tts_notifier = {
  51. .notifier_call = tts_notify_reboot,
  52. .next = NULL,
  53. .priority = 0,
  54. };
  55. static int acpi_sleep_prepare(u32 acpi_state)
  56. {
  57. #ifdef CONFIG_ACPI_SLEEP
  58. /* do we have a wakeup address for S2 and S3? */
  59. if (acpi_state == ACPI_STATE_S3) {
  60. if (!acpi_wakeup_address)
  61. return -EFAULT;
  62. acpi_set_waking_vector(acpi_wakeup_address);
  63. }
  64. ACPI_FLUSH_CPU_CACHE();
  65. #endif
  66. printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
  67. acpi_state);
  68. acpi_enable_wakeup_devices(acpi_state);
  69. acpi_enter_sleep_state_prep(acpi_state);
  70. return 0;
  71. }
  72. static bool acpi_sleep_state_supported(u8 sleep_state)
  73. {
  74. acpi_status status;
  75. u8 type_a, type_b;
  76. status = acpi_get_sleep_type_data(sleep_state, &type_a, &type_b);
  77. return ACPI_SUCCESS(status) && (!acpi_gbl_reduced_hardware
  78. || (acpi_gbl_FADT.sleep_control.address
  79. && acpi_gbl_FADT.sleep_status.address));
  80. }
  81. #ifdef CONFIG_ACPI_SLEEP
  82. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  83. u32 acpi_target_system_state(void)
  84. {
  85. return acpi_target_sleep_state;
  86. }
  87. EXPORT_SYMBOL_GPL(acpi_target_system_state);
  88. static bool pwr_btn_event_pending;
  89. /*
  90. * The ACPI specification wants us to save NVS memory regions during hibernation
  91. * and to restore them during the subsequent resume. Windows does that also for
  92. * suspend to RAM. However, it is known that this mechanism does not work on
  93. * all machines, so we allow the user to disable it with the help of the
  94. * 'acpi_sleep=nonvs' kernel command line option.
  95. */
  96. static bool nvs_nosave;
  97. void __init acpi_nvs_nosave(void)
  98. {
  99. nvs_nosave = true;
  100. }
  101. /*
  102. * The ACPI specification wants us to save NVS memory regions during hibernation
  103. * but says nothing about saving NVS during S3. Not all versions of Windows
  104. * save NVS on S3 suspend either, and it is clear that not all systems need
  105. * NVS to be saved at S3 time. To improve suspend/resume time, allow the
  106. * user to disable saving NVS on S3 if their system does not require it, but
  107. * continue to save/restore NVS for S4 as specified.
  108. */
  109. static bool nvs_nosave_s3;
  110. void __init acpi_nvs_nosave_s3(void)
  111. {
  112. nvs_nosave_s3 = true;
  113. }
  114. static int __init init_nvs_save_s3(const struct dmi_system_id *d)
  115. {
  116. nvs_nosave_s3 = false;
  117. return 0;
  118. }
  119. /*
  120. * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
  121. * user to request that behavior by using the 'acpi_old_suspend_ordering'
  122. * kernel command line option that causes the following variable to be set.
  123. */
  124. static bool old_suspend_ordering;
  125. void __init acpi_old_suspend_ordering(void)
  126. {
  127. old_suspend_ordering = true;
  128. }
  129. static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
  130. {
  131. acpi_old_suspend_ordering();
  132. return 0;
  133. }
  134. static int __init init_nvs_nosave(const struct dmi_system_id *d)
  135. {
  136. acpi_nvs_nosave();
  137. return 0;
  138. }
  139. static const struct dmi_system_id acpisleep_dmi_table[] __initconst = {
  140. {
  141. .callback = init_old_suspend_ordering,
  142. .ident = "Abit KN9 (nForce4 variant)",
  143. .matches = {
  144. DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
  145. DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
  146. },
  147. },
  148. {
  149. .callback = init_old_suspend_ordering,
  150. .ident = "HP xw4600 Workstation",
  151. .matches = {
  152. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  153. DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
  154. },
  155. },
  156. {
  157. .callback = init_old_suspend_ordering,
  158. .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
  159. .matches = {
  160. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
  161. DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
  162. },
  163. },
  164. {
  165. .callback = init_old_suspend_ordering,
  166. .ident = "Panasonic CF51-2L",
  167. .matches = {
  168. DMI_MATCH(DMI_BOARD_VENDOR,
  169. "Matsushita Electric Industrial Co.,Ltd."),
  170. DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
  171. },
  172. },
  173. {
  174. .callback = init_nvs_nosave,
  175. .ident = "Sony Vaio VGN-FW41E_H",
  176. .matches = {
  177. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  178. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
  179. },
  180. },
  181. {
  182. .callback = init_nvs_nosave,
  183. .ident = "Sony Vaio VGN-FW21E",
  184. .matches = {
  185. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  186. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
  187. },
  188. },
  189. {
  190. .callback = init_nvs_nosave,
  191. .ident = "Sony Vaio VGN-FW21M",
  192. .matches = {
  193. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  194. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21M"),
  195. },
  196. },
  197. {
  198. .callback = init_nvs_nosave,
  199. .ident = "Sony Vaio VPCEB17FX",
  200. .matches = {
  201. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  202. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
  203. },
  204. },
  205. {
  206. .callback = init_nvs_nosave,
  207. .ident = "Sony Vaio VGN-SR11M",
  208. .matches = {
  209. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  210. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
  211. },
  212. },
  213. {
  214. .callback = init_nvs_nosave,
  215. .ident = "Everex StepNote Series",
  216. .matches = {
  217. DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
  218. DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
  219. },
  220. },
  221. {
  222. .callback = init_nvs_nosave,
  223. .ident = "Sony Vaio VPCEB1Z1E",
  224. .matches = {
  225. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  226. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
  227. },
  228. },
  229. {
  230. .callback = init_nvs_nosave,
  231. .ident = "Sony Vaio VGN-NW130D",
  232. .matches = {
  233. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  234. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
  235. },
  236. },
  237. {
  238. .callback = init_nvs_nosave,
  239. .ident = "Sony Vaio VPCCW29FX",
  240. .matches = {
  241. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  242. DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
  243. },
  244. },
  245. {
  246. .callback = init_nvs_nosave,
  247. .ident = "Averatec AV1020-ED2",
  248. .matches = {
  249. DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
  250. DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
  251. },
  252. },
  253. {
  254. .callback = init_old_suspend_ordering,
  255. .ident = "Asus A8N-SLI DELUXE",
  256. .matches = {
  257. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  258. DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
  259. },
  260. },
  261. {
  262. .callback = init_old_suspend_ordering,
  263. .ident = "Asus A8N-SLI Premium",
  264. .matches = {
  265. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  266. DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
  267. },
  268. },
  269. {
  270. .callback = init_nvs_nosave,
  271. .ident = "Sony Vaio VGN-SR26GN_P",
  272. .matches = {
  273. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  274. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
  275. },
  276. },
  277. {
  278. .callback = init_nvs_nosave,
  279. .ident = "Sony Vaio VPCEB1S1E",
  280. .matches = {
  281. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  282. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
  283. },
  284. },
  285. {
  286. .callback = init_nvs_nosave,
  287. .ident = "Sony Vaio VGN-FW520F",
  288. .matches = {
  289. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  290. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
  291. },
  292. },
  293. {
  294. .callback = init_nvs_nosave,
  295. .ident = "Asus K54C",
  296. .matches = {
  297. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  298. DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
  299. },
  300. },
  301. {
  302. .callback = init_nvs_nosave,
  303. .ident = "Asus K54HR",
  304. .matches = {
  305. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  306. DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
  307. },
  308. },
  309. /*
  310. * https://bugzilla.kernel.org/show_bug.cgi?id=189431
  311. * Lenovo G50-45 is a platform later than 2012, but needs nvs memory
  312. * saving during S3.
  313. */
  314. {
  315. .callback = init_nvs_save_s3,
  316. .ident = "Lenovo G50-45",
  317. .matches = {
  318. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  319. DMI_MATCH(DMI_PRODUCT_NAME, "80E3"),
  320. },
  321. },
  322. {},
  323. };
  324. static void __init acpi_sleep_dmi_check(void)
  325. {
  326. int year;
  327. if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year >= 2012)
  328. acpi_nvs_nosave_s3();
  329. dmi_check_system(acpisleep_dmi_table);
  330. }
  331. /**
  332. * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
  333. */
  334. static int acpi_pm_freeze(void)
  335. {
  336. acpi_disable_all_gpes();
  337. acpi_os_wait_events_complete();
  338. acpi_ec_block_transactions();
  339. return 0;
  340. }
  341. /**
  342. * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
  343. */
  344. static int acpi_pm_pre_suspend(void)
  345. {
  346. acpi_pm_freeze();
  347. return suspend_nvs_save();
  348. }
  349. /**
  350. * __acpi_pm_prepare - Prepare the platform to enter the target state.
  351. *
  352. * If necessary, set the firmware waking vector and do arch-specific
  353. * nastiness to get the wakeup code to the waking vector.
  354. */
  355. static int __acpi_pm_prepare(void)
  356. {
  357. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  358. if (error)
  359. acpi_target_sleep_state = ACPI_STATE_S0;
  360. return error;
  361. }
  362. /**
  363. * acpi_pm_prepare - Prepare the platform to enter the target sleep
  364. * state and disable the GPEs.
  365. */
  366. static int acpi_pm_prepare(void)
  367. {
  368. int error = __acpi_pm_prepare();
  369. if (!error)
  370. error = acpi_pm_pre_suspend();
  371. return error;
  372. }
  373. static int find_powerf_dev(struct device *dev, void *data)
  374. {
  375. struct acpi_device *device = to_acpi_device(dev);
  376. const char *hid = acpi_device_hid(device);
  377. return !strcmp(hid, ACPI_BUTTON_HID_POWERF);
  378. }
  379. /**
  380. * acpi_pm_finish - Instruct the platform to leave a sleep state.
  381. *
  382. * This is called after we wake back up (or if entering the sleep state
  383. * failed).
  384. */
  385. static void acpi_pm_finish(void)
  386. {
  387. struct device *pwr_btn_dev;
  388. u32 acpi_state = acpi_target_sleep_state;
  389. acpi_ec_unblock_transactions();
  390. suspend_nvs_free();
  391. if (acpi_state == ACPI_STATE_S0)
  392. return;
  393. printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
  394. acpi_state);
  395. acpi_disable_wakeup_devices(acpi_state);
  396. acpi_leave_sleep_state(acpi_state);
  397. /* reset firmware waking vector */
  398. acpi_set_waking_vector(0);
  399. acpi_target_sleep_state = ACPI_STATE_S0;
  400. acpi_resume_power_resources();
  401. /* If we were woken with the fixed power button, provide a small
  402. * hint to userspace in the form of a wakeup event on the fixed power
  403. * button device (if it can be found).
  404. *
  405. * We delay the event generation til now, as the PM layer requires
  406. * timekeeping to be running before we generate events. */
  407. if (!pwr_btn_event_pending)
  408. return;
  409. pwr_btn_event_pending = false;
  410. pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL,
  411. find_powerf_dev);
  412. if (pwr_btn_dev) {
  413. pm_wakeup_event(pwr_btn_dev, 0);
  414. put_device(pwr_btn_dev);
  415. }
  416. }
  417. /**
  418. * acpi_pm_start - Start system PM transition.
  419. */
  420. static void acpi_pm_start(u32 acpi_state)
  421. {
  422. acpi_target_sleep_state = acpi_state;
  423. acpi_sleep_tts_switch(acpi_target_sleep_state);
  424. acpi_scan_lock_acquire();
  425. }
  426. /**
  427. * acpi_pm_end - Finish up system PM transition.
  428. */
  429. static void acpi_pm_end(void)
  430. {
  431. acpi_turn_off_unused_power_resources();
  432. acpi_scan_lock_release();
  433. /*
  434. * This is necessary in case acpi_pm_finish() is not called during a
  435. * failing transition to a sleep state.
  436. */
  437. acpi_target_sleep_state = ACPI_STATE_S0;
  438. acpi_sleep_tts_switch(acpi_target_sleep_state);
  439. }
  440. #else /* !CONFIG_ACPI_SLEEP */
  441. #define acpi_target_sleep_state ACPI_STATE_S0
  442. static inline void acpi_sleep_dmi_check(void) {}
  443. #endif /* CONFIG_ACPI_SLEEP */
  444. #ifdef CONFIG_SUSPEND
  445. static u32 acpi_suspend_states[] = {
  446. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  447. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  448. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  449. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  450. };
  451. /**
  452. * acpi_suspend_begin - Set the target system sleep state to the state
  453. * associated with given @pm_state, if supported.
  454. */
  455. static int acpi_suspend_begin(suspend_state_t pm_state)
  456. {
  457. u32 acpi_state = acpi_suspend_states[pm_state];
  458. int error;
  459. error = (nvs_nosave || nvs_nosave_s3) ? 0 : suspend_nvs_alloc();
  460. if (error)
  461. return error;
  462. if (!sleep_states[acpi_state]) {
  463. pr_err("ACPI does not support sleep state S%u\n", acpi_state);
  464. return -ENOSYS;
  465. }
  466. if (acpi_state > ACPI_STATE_S1)
  467. pm_set_suspend_via_firmware();
  468. acpi_pm_start(acpi_state);
  469. return 0;
  470. }
  471. /**
  472. * acpi_suspend_enter - Actually enter a sleep state.
  473. * @pm_state: ignored
  474. *
  475. * Flush caches and go to sleep. For STR we have to call arch-specific
  476. * assembly, which in turn call acpi_enter_sleep_state().
  477. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  478. */
  479. static int acpi_suspend_enter(suspend_state_t pm_state)
  480. {
  481. acpi_status status = AE_OK;
  482. u32 acpi_state = acpi_target_sleep_state;
  483. int error;
  484. ACPI_FLUSH_CPU_CACHE();
  485. trace_suspend_resume(TPS("acpi_suspend"), acpi_state, true);
  486. switch (acpi_state) {
  487. case ACPI_STATE_S1:
  488. barrier();
  489. status = acpi_enter_sleep_state(acpi_state);
  490. break;
  491. case ACPI_STATE_S3:
  492. if (!acpi_suspend_lowlevel)
  493. return -ENOSYS;
  494. error = acpi_suspend_lowlevel();
  495. if (error)
  496. return error;
  497. pr_info(PREFIX "Low-level resume complete\n");
  498. pm_set_resume_via_firmware();
  499. break;
  500. }
  501. trace_suspend_resume(TPS("acpi_suspend"), acpi_state, false);
  502. /* This violates the spec but is required for bug compatibility. */
  503. acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
  504. /* Reprogram control registers */
  505. acpi_leave_sleep_state_prep(acpi_state);
  506. /* ACPI 3.0 specs (P62) says that it's the responsibility
  507. * of the OSPM to clear the status bit [ implying that the
  508. * POWER_BUTTON event should not reach userspace ]
  509. *
  510. * However, we do generate a small hint for userspace in the form of
  511. * a wakeup event. We flag this condition for now and generate the
  512. * event later, as we're currently too early in resume to be able to
  513. * generate wakeup events.
  514. */
  515. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
  516. acpi_event_status pwr_btn_status = ACPI_EVENT_FLAG_DISABLED;
  517. acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
  518. if (pwr_btn_status & ACPI_EVENT_FLAG_STATUS_SET) {
  519. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  520. /* Flag for later */
  521. pwr_btn_event_pending = true;
  522. }
  523. }
  524. /*
  525. * Disable and clear GPE status before interrupt is enabled. Some GPEs
  526. * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
  527. * acpi_leave_sleep_state will reenable specific GPEs later
  528. */
  529. acpi_disable_all_gpes();
  530. /* Allow EC transactions to happen. */
  531. acpi_ec_unblock_transactions();
  532. suspend_nvs_restore();
  533. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  534. }
  535. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  536. {
  537. u32 acpi_state;
  538. switch (pm_state) {
  539. case PM_SUSPEND_ON:
  540. case PM_SUSPEND_STANDBY:
  541. case PM_SUSPEND_MEM:
  542. acpi_state = acpi_suspend_states[pm_state];
  543. return sleep_states[acpi_state];
  544. default:
  545. return 0;
  546. }
  547. }
  548. static const struct platform_suspend_ops acpi_suspend_ops = {
  549. .valid = acpi_suspend_state_valid,
  550. .begin = acpi_suspend_begin,
  551. .prepare_late = acpi_pm_prepare,
  552. .enter = acpi_suspend_enter,
  553. .wake = acpi_pm_finish,
  554. .end = acpi_pm_end,
  555. };
  556. /**
  557. * acpi_suspend_begin_old - Set the target system sleep state to the
  558. * state associated with given @pm_state, if supported, and
  559. * execute the _PTS control method. This function is used if the
  560. * pre-ACPI 2.0 suspend ordering has been requested.
  561. */
  562. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  563. {
  564. int error = acpi_suspend_begin(pm_state);
  565. if (!error)
  566. error = __acpi_pm_prepare();
  567. return error;
  568. }
  569. /*
  570. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  571. * been requested.
  572. */
  573. static const struct platform_suspend_ops acpi_suspend_ops_old = {
  574. .valid = acpi_suspend_state_valid,
  575. .begin = acpi_suspend_begin_old,
  576. .prepare_late = acpi_pm_pre_suspend,
  577. .enter = acpi_suspend_enter,
  578. .wake = acpi_pm_finish,
  579. .end = acpi_pm_end,
  580. .recover = acpi_pm_finish,
  581. };
  582. static bool s2idle_in_progress;
  583. static bool s2idle_wakeup;
  584. /*
  585. * On platforms supporting the Low Power S0 Idle interface there is an ACPI
  586. * device object with the PNP0D80 compatible device ID (System Power Management
  587. * Controller) and a specific _DSM method under it. That method, if present,
  588. * can be used to indicate to the platform that the OS is transitioning into a
  589. * low-power state in which certain types of activity are not desirable or that
  590. * it is leaving such a state, which allows the platform to adjust its operation
  591. * mode accordingly.
  592. */
  593. static const struct acpi_device_id lps0_device_ids[] = {
  594. {"PNP0D80", },
  595. {"", },
  596. };
  597. #define ACPI_LPS0_DSM_UUID "c4eb40a0-6cd2-11e2-bcfd-0800200c9a66"
  598. #define ACPI_LPS0_GET_DEVICE_CONSTRAINTS 1
  599. #define ACPI_LPS0_SCREEN_OFF 3
  600. #define ACPI_LPS0_SCREEN_ON 4
  601. #define ACPI_LPS0_ENTRY 5
  602. #define ACPI_LPS0_EXIT 6
  603. #define ACPI_S2IDLE_FUNC_MASK ((1 << ACPI_LPS0_ENTRY) | (1 << ACPI_LPS0_EXIT))
  604. static acpi_handle lps0_device_handle;
  605. static guid_t lps0_dsm_guid;
  606. static char lps0_dsm_func_mask;
  607. /* Device constraint entry structure */
  608. struct lpi_device_info {
  609. char *name;
  610. int enabled;
  611. union acpi_object *package;
  612. };
  613. /* Constraint package structure */
  614. struct lpi_device_constraint {
  615. int uid;
  616. int min_dstate;
  617. int function_states;
  618. };
  619. struct lpi_constraints {
  620. acpi_handle handle;
  621. int min_dstate;
  622. };
  623. static struct lpi_constraints *lpi_constraints_table;
  624. static int lpi_constraints_table_size;
  625. static void lpi_device_get_constraints(void)
  626. {
  627. union acpi_object *out_obj;
  628. int i;
  629. out_obj = acpi_evaluate_dsm_typed(lps0_device_handle, &lps0_dsm_guid,
  630. 1, ACPI_LPS0_GET_DEVICE_CONSTRAINTS,
  631. NULL, ACPI_TYPE_PACKAGE);
  632. acpi_handle_debug(lps0_device_handle, "_DSM function 1 eval %s\n",
  633. out_obj ? "successful" : "failed");
  634. if (!out_obj)
  635. return;
  636. lpi_constraints_table = kcalloc(out_obj->package.count,
  637. sizeof(*lpi_constraints_table),
  638. GFP_KERNEL);
  639. if (!lpi_constraints_table)
  640. goto free_acpi_buffer;
  641. acpi_handle_debug(lps0_device_handle, "LPI: constraints list begin:\n");
  642. for (i = 0; i < out_obj->package.count; i++) {
  643. struct lpi_constraints *constraint;
  644. acpi_status status;
  645. union acpi_object *package = &out_obj->package.elements[i];
  646. struct lpi_device_info info = { };
  647. int package_count = 0, j;
  648. if (!package)
  649. continue;
  650. for (j = 0; j < package->package.count; ++j) {
  651. union acpi_object *element =
  652. &(package->package.elements[j]);
  653. switch (element->type) {
  654. case ACPI_TYPE_INTEGER:
  655. info.enabled = element->integer.value;
  656. break;
  657. case ACPI_TYPE_STRING:
  658. info.name = element->string.pointer;
  659. break;
  660. case ACPI_TYPE_PACKAGE:
  661. package_count = element->package.count;
  662. info.package = element->package.elements;
  663. break;
  664. }
  665. }
  666. if (!info.enabled || !info.package || !info.name)
  667. continue;
  668. constraint = &lpi_constraints_table[lpi_constraints_table_size];
  669. status = acpi_get_handle(NULL, info.name, &constraint->handle);
  670. if (ACPI_FAILURE(status))
  671. continue;
  672. acpi_handle_debug(lps0_device_handle,
  673. "index:%d Name:%s\n", i, info.name);
  674. constraint->min_dstate = -1;
  675. for (j = 0; j < package_count; ++j) {
  676. union acpi_object *info_obj = &info.package[j];
  677. union acpi_object *cnstr_pkg;
  678. union acpi_object *obj;
  679. struct lpi_device_constraint dev_info;
  680. switch (info_obj->type) {
  681. case ACPI_TYPE_INTEGER:
  682. /* version */
  683. break;
  684. case ACPI_TYPE_PACKAGE:
  685. if (info_obj->package.count < 2)
  686. break;
  687. cnstr_pkg = info_obj->package.elements;
  688. obj = &cnstr_pkg[0];
  689. dev_info.uid = obj->integer.value;
  690. obj = &cnstr_pkg[1];
  691. dev_info.min_dstate = obj->integer.value;
  692. acpi_handle_debug(lps0_device_handle,
  693. "uid:%d min_dstate:%s\n",
  694. dev_info.uid,
  695. acpi_power_state_string(dev_info.min_dstate));
  696. constraint->min_dstate = dev_info.min_dstate;
  697. break;
  698. }
  699. }
  700. if (constraint->min_dstate < 0) {
  701. acpi_handle_debug(lps0_device_handle,
  702. "Incomplete constraint defined\n");
  703. continue;
  704. }
  705. lpi_constraints_table_size++;
  706. }
  707. acpi_handle_debug(lps0_device_handle, "LPI: constraints list end\n");
  708. free_acpi_buffer:
  709. ACPI_FREE(out_obj);
  710. }
  711. static void lpi_check_constraints(void)
  712. {
  713. int i;
  714. for (i = 0; i < lpi_constraints_table_size; ++i) {
  715. struct acpi_device *adev;
  716. if (acpi_bus_get_device(lpi_constraints_table[i].handle, &adev))
  717. continue;
  718. acpi_handle_debug(adev->handle,
  719. "LPI: required min power state:%s current power state:%s\n",
  720. acpi_power_state_string(lpi_constraints_table[i].min_dstate),
  721. acpi_power_state_string(adev->power.state));
  722. if (!adev->flags.power_manageable) {
  723. acpi_handle_info(adev->handle, "LPI: Device not power manageble\n");
  724. continue;
  725. }
  726. if (adev->power.state < lpi_constraints_table[i].min_dstate)
  727. acpi_handle_info(adev->handle,
  728. "LPI: Constraint not met; min power state:%s current power state:%s\n",
  729. acpi_power_state_string(lpi_constraints_table[i].min_dstate),
  730. acpi_power_state_string(adev->power.state));
  731. }
  732. }
  733. static void acpi_sleep_run_lps0_dsm(unsigned int func)
  734. {
  735. union acpi_object *out_obj;
  736. if (!(lps0_dsm_func_mask & (1 << func)))
  737. return;
  738. out_obj = acpi_evaluate_dsm(lps0_device_handle, &lps0_dsm_guid, 1, func, NULL);
  739. ACPI_FREE(out_obj);
  740. acpi_handle_debug(lps0_device_handle, "_DSM function %u evaluation %s\n",
  741. func, out_obj ? "successful" : "failed");
  742. }
  743. static int lps0_device_attach(struct acpi_device *adev,
  744. const struct acpi_device_id *not_used)
  745. {
  746. union acpi_object *out_obj;
  747. if (lps0_device_handle)
  748. return 0;
  749. if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
  750. return 0;
  751. guid_parse(ACPI_LPS0_DSM_UUID, &lps0_dsm_guid);
  752. /* Check if the _DSM is present and as expected. */
  753. out_obj = acpi_evaluate_dsm(adev->handle, &lps0_dsm_guid, 1, 0, NULL);
  754. if (out_obj && out_obj->type == ACPI_TYPE_BUFFER) {
  755. char bitmask = *(char *)out_obj->buffer.pointer;
  756. if ((bitmask & ACPI_S2IDLE_FUNC_MASK) == ACPI_S2IDLE_FUNC_MASK) {
  757. lps0_dsm_func_mask = bitmask;
  758. lps0_device_handle = adev->handle;
  759. /*
  760. * Use suspend-to-idle by default if the default
  761. * suspend mode was not set from the command line.
  762. */
  763. if (mem_sleep_default > PM_SUSPEND_MEM)
  764. mem_sleep_current = PM_SUSPEND_TO_IDLE;
  765. }
  766. acpi_handle_debug(adev->handle, "_DSM function mask: 0x%x\n",
  767. bitmask);
  768. } else {
  769. acpi_handle_debug(adev->handle,
  770. "_DSM function 0 evaluation failed\n");
  771. }
  772. ACPI_FREE(out_obj);
  773. lpi_device_get_constraints();
  774. return 0;
  775. }
  776. static struct acpi_scan_handler lps0_handler = {
  777. .ids = lps0_device_ids,
  778. .attach = lps0_device_attach,
  779. };
  780. static int acpi_s2idle_begin(void)
  781. {
  782. acpi_scan_lock_acquire();
  783. s2idle_in_progress = true;
  784. return 0;
  785. }
  786. static int acpi_s2idle_prepare(void)
  787. {
  788. if (lps0_device_handle) {
  789. acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_OFF);
  790. acpi_sleep_run_lps0_dsm(ACPI_LPS0_ENTRY);
  791. } else {
  792. /*
  793. * The configuration of GPEs is changed here to avoid spurious
  794. * wakeups, but that should not be necessary if this is a
  795. * "low-power S0" platform and the low-power S0 _DSM is present.
  796. */
  797. acpi_enable_all_wakeup_gpes();
  798. acpi_os_wait_events_complete();
  799. }
  800. if (acpi_sci_irq_valid())
  801. enable_irq_wake(acpi_sci_irq);
  802. return 0;
  803. }
  804. static void acpi_s2idle_wake(void)
  805. {
  806. if (pm_debug_messages_on)
  807. lpi_check_constraints();
  808. /*
  809. * If IRQD_WAKEUP_ARMED is not set for the SCI at this point, it means
  810. * that the SCI has triggered while suspended, so cancel the wakeup in
  811. * case it has not been a wakeup event (the GPEs will be checked later).
  812. */
  813. if (acpi_sci_irq_valid() &&
  814. !irqd_is_wakeup_armed(irq_get_irq_data(acpi_sci_irq))) {
  815. pm_system_cancel_wakeup();
  816. s2idle_wakeup = true;
  817. }
  818. }
  819. static void acpi_s2idle_sync(void)
  820. {
  821. /*
  822. * Process all pending events in case there are any wakeup ones.
  823. *
  824. * The EC driver uses the system workqueue and an additional special
  825. * one, so those need to be flushed too.
  826. */
  827. acpi_ec_flush_work();
  828. acpi_os_wait_events_complete();
  829. s2idle_wakeup = false;
  830. }
  831. static void acpi_s2idle_restore(void)
  832. {
  833. if (acpi_sci_irq_valid())
  834. disable_irq_wake(acpi_sci_irq);
  835. if (lps0_device_handle) {
  836. acpi_sleep_run_lps0_dsm(ACPI_LPS0_EXIT);
  837. acpi_sleep_run_lps0_dsm(ACPI_LPS0_SCREEN_ON);
  838. } else {
  839. acpi_enable_all_runtime_gpes();
  840. }
  841. }
  842. static void acpi_s2idle_end(void)
  843. {
  844. s2idle_in_progress = false;
  845. acpi_scan_lock_release();
  846. }
  847. static const struct platform_s2idle_ops acpi_s2idle_ops = {
  848. .begin = acpi_s2idle_begin,
  849. .prepare = acpi_s2idle_prepare,
  850. .wake = acpi_s2idle_wake,
  851. .sync = acpi_s2idle_sync,
  852. .restore = acpi_s2idle_restore,
  853. .end = acpi_s2idle_end,
  854. };
  855. static void acpi_sleep_suspend_setup(void)
  856. {
  857. int i;
  858. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++)
  859. if (acpi_sleep_state_supported(i))
  860. sleep_states[i] = 1;
  861. suspend_set_ops(old_suspend_ordering ?
  862. &acpi_suspend_ops_old : &acpi_suspend_ops);
  863. acpi_scan_add_handler(&lps0_handler);
  864. s2idle_set_ops(&acpi_s2idle_ops);
  865. }
  866. #else /* !CONFIG_SUSPEND */
  867. #define s2idle_in_progress (false)
  868. #define s2idle_wakeup (false)
  869. #define lps0_device_handle (NULL)
  870. static inline void acpi_sleep_suspend_setup(void) {}
  871. #endif /* !CONFIG_SUSPEND */
  872. bool acpi_s2idle_wakeup(void)
  873. {
  874. return s2idle_wakeup;
  875. }
  876. bool acpi_sleep_no_ec_events(void)
  877. {
  878. return !s2idle_in_progress || !lps0_device_handle;
  879. }
  880. #ifdef CONFIG_PM_SLEEP
  881. static u32 saved_bm_rld;
  882. static int acpi_save_bm_rld(void)
  883. {
  884. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
  885. return 0;
  886. }
  887. static void acpi_restore_bm_rld(void)
  888. {
  889. u32 resumed_bm_rld = 0;
  890. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
  891. if (resumed_bm_rld == saved_bm_rld)
  892. return;
  893. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
  894. }
  895. static struct syscore_ops acpi_sleep_syscore_ops = {
  896. .suspend = acpi_save_bm_rld,
  897. .resume = acpi_restore_bm_rld,
  898. };
  899. static void acpi_sleep_syscore_init(void)
  900. {
  901. register_syscore_ops(&acpi_sleep_syscore_ops);
  902. }
  903. #else
  904. static inline void acpi_sleep_syscore_init(void) {}
  905. #endif /* CONFIG_PM_SLEEP */
  906. #ifdef CONFIG_HIBERNATION
  907. static unsigned long s4_hardware_signature;
  908. static struct acpi_table_facs *facs;
  909. static bool nosigcheck;
  910. void __init acpi_no_s4_hw_signature(void)
  911. {
  912. nosigcheck = true;
  913. }
  914. static int acpi_hibernation_begin(void)
  915. {
  916. int error;
  917. error = nvs_nosave ? 0 : suspend_nvs_alloc();
  918. if (!error)
  919. acpi_pm_start(ACPI_STATE_S4);
  920. return error;
  921. }
  922. static int acpi_hibernation_enter(void)
  923. {
  924. acpi_status status = AE_OK;
  925. ACPI_FLUSH_CPU_CACHE();
  926. /* This shouldn't return. If it returns, we have a problem */
  927. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  928. /* Reprogram control registers */
  929. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  930. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  931. }
  932. static void acpi_hibernation_leave(void)
  933. {
  934. pm_set_resume_via_firmware();
  935. /*
  936. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  937. * enable it here.
  938. */
  939. acpi_enable();
  940. /* Reprogram control registers */
  941. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  942. /* Check the hardware signature */
  943. if (facs && s4_hardware_signature != facs->hardware_signature)
  944. pr_crit("ACPI: Hardware changed while hibernated, success doubtful!\n");
  945. /* Restore the NVS memory area */
  946. suspend_nvs_restore();
  947. /* Allow EC transactions to happen. */
  948. acpi_ec_unblock_transactions();
  949. }
  950. static void acpi_pm_thaw(void)
  951. {
  952. acpi_ec_unblock_transactions();
  953. acpi_enable_all_runtime_gpes();
  954. }
  955. static const struct platform_hibernation_ops acpi_hibernation_ops = {
  956. .begin = acpi_hibernation_begin,
  957. .end = acpi_pm_end,
  958. .pre_snapshot = acpi_pm_prepare,
  959. .finish = acpi_pm_finish,
  960. .prepare = acpi_pm_prepare,
  961. .enter = acpi_hibernation_enter,
  962. .leave = acpi_hibernation_leave,
  963. .pre_restore = acpi_pm_freeze,
  964. .restore_cleanup = acpi_pm_thaw,
  965. };
  966. /**
  967. * acpi_hibernation_begin_old - Set the target system sleep state to
  968. * ACPI_STATE_S4 and execute the _PTS control method. This
  969. * function is used if the pre-ACPI 2.0 suspend ordering has been
  970. * requested.
  971. */
  972. static int acpi_hibernation_begin_old(void)
  973. {
  974. int error;
  975. /*
  976. * The _TTS object should always be evaluated before the _PTS object.
  977. * When the old_suspended_ordering is true, the _PTS object is
  978. * evaluated in the acpi_sleep_prepare.
  979. */
  980. acpi_sleep_tts_switch(ACPI_STATE_S4);
  981. error = acpi_sleep_prepare(ACPI_STATE_S4);
  982. if (!error) {
  983. if (!nvs_nosave)
  984. error = suspend_nvs_alloc();
  985. if (!error) {
  986. acpi_target_sleep_state = ACPI_STATE_S4;
  987. acpi_scan_lock_acquire();
  988. }
  989. }
  990. return error;
  991. }
  992. /*
  993. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  994. * been requested.
  995. */
  996. static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
  997. .begin = acpi_hibernation_begin_old,
  998. .end = acpi_pm_end,
  999. .pre_snapshot = acpi_pm_pre_suspend,
  1000. .prepare = acpi_pm_freeze,
  1001. .finish = acpi_pm_finish,
  1002. .enter = acpi_hibernation_enter,
  1003. .leave = acpi_hibernation_leave,
  1004. .pre_restore = acpi_pm_freeze,
  1005. .restore_cleanup = acpi_pm_thaw,
  1006. .recover = acpi_pm_finish,
  1007. };
  1008. static void acpi_sleep_hibernate_setup(void)
  1009. {
  1010. if (!acpi_sleep_state_supported(ACPI_STATE_S4))
  1011. return;
  1012. hibernation_set_ops(old_suspend_ordering ?
  1013. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  1014. sleep_states[ACPI_STATE_S4] = 1;
  1015. if (nosigcheck)
  1016. return;
  1017. acpi_get_table(ACPI_SIG_FACS, 1, (struct acpi_table_header **)&facs);
  1018. if (facs)
  1019. s4_hardware_signature = facs->hardware_signature;
  1020. }
  1021. #else /* !CONFIG_HIBERNATION */
  1022. static inline void acpi_sleep_hibernate_setup(void) {}
  1023. #endif /* !CONFIG_HIBERNATION */
  1024. static void acpi_power_off_prepare(void)
  1025. {
  1026. /* Prepare to power off the system */
  1027. acpi_sleep_prepare(ACPI_STATE_S5);
  1028. acpi_disable_all_gpes();
  1029. acpi_os_wait_events_complete();
  1030. }
  1031. static void acpi_power_off(void)
  1032. {
  1033. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  1034. printk(KERN_DEBUG "%s called\n", __func__);
  1035. local_irq_disable();
  1036. acpi_enter_sleep_state(ACPI_STATE_S5);
  1037. }
  1038. int __init acpi_sleep_init(void)
  1039. {
  1040. char supported[ACPI_S_STATE_COUNT * 3 + 1];
  1041. char *pos = supported;
  1042. int i;
  1043. acpi_sleep_dmi_check();
  1044. sleep_states[ACPI_STATE_S0] = 1;
  1045. acpi_sleep_syscore_init();
  1046. acpi_sleep_suspend_setup();
  1047. acpi_sleep_hibernate_setup();
  1048. if (acpi_sleep_state_supported(ACPI_STATE_S5)) {
  1049. sleep_states[ACPI_STATE_S5] = 1;
  1050. pm_power_off_prepare = acpi_power_off_prepare;
  1051. pm_power_off = acpi_power_off;
  1052. } else {
  1053. acpi_no_s5 = true;
  1054. }
  1055. supported[0] = 0;
  1056. for (i = 0; i < ACPI_S_STATE_COUNT; i++) {
  1057. if (sleep_states[i])
  1058. pos += sprintf(pos, " S%d", i);
  1059. }
  1060. pr_info(PREFIX "(supports%s)\n", supported);
  1061. /*
  1062. * Register the tts_notifier to reboot notifier list so that the _TTS
  1063. * object can also be evaluated when the system enters S5.
  1064. */
  1065. register_reboot_notifier(&tts_notifier);
  1066. return 0;
  1067. }