sleep.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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 <linux/pm_runtime.h>
  21. #include <linux/pm_qos.h>
  22. #include <asm/io.h>
  23. #include <acpi/acpi_bus.h>
  24. #include <acpi/acpi_drivers.h>
  25. #include "internal.h"
  26. #include "sleep.h"
  27. u8 wake_sleep_flags = ACPI_NO_OPTIONAL_METHODS;
  28. static unsigned int gts, bfs;
  29. static int set_param_wake_flag(const char *val, struct kernel_param *kp)
  30. {
  31. int ret = param_set_int(val, kp);
  32. if (ret)
  33. return ret;
  34. if (kp->arg == (const char *)&gts) {
  35. if (gts)
  36. wake_sleep_flags |= ACPI_EXECUTE_GTS;
  37. else
  38. wake_sleep_flags &= ~ACPI_EXECUTE_GTS;
  39. }
  40. if (kp->arg == (const char *)&bfs) {
  41. if (bfs)
  42. wake_sleep_flags |= ACPI_EXECUTE_BFS;
  43. else
  44. wake_sleep_flags &= ~ACPI_EXECUTE_BFS;
  45. }
  46. return ret;
  47. }
  48. module_param_call(gts, set_param_wake_flag, param_get_int, &gts, 0644);
  49. module_param_call(bfs, set_param_wake_flag, param_get_int, &bfs, 0644);
  50. MODULE_PARM_DESC(gts, "Enable evaluation of _GTS on suspend.");
  51. MODULE_PARM_DESC(bfs, "Enable evaluation of _BFS on resume".);
  52. static u8 sleep_states[ACPI_S_STATE_COUNT];
  53. static bool pwr_btn_event_pending;
  54. static void acpi_sleep_tts_switch(u32 acpi_state)
  55. {
  56. union acpi_object in_arg = { ACPI_TYPE_INTEGER };
  57. struct acpi_object_list arg_list = { 1, &in_arg };
  58. acpi_status status = AE_OK;
  59. in_arg.integer.value = acpi_state;
  60. status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
  61. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  62. /*
  63. * OS can't evaluate the _TTS object correctly. Some warning
  64. * message will be printed. But it won't break anything.
  65. */
  66. printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
  67. }
  68. }
  69. static int tts_notify_reboot(struct notifier_block *this,
  70. unsigned long code, void *x)
  71. {
  72. acpi_sleep_tts_switch(ACPI_STATE_S5);
  73. return NOTIFY_DONE;
  74. }
  75. static struct notifier_block tts_notifier = {
  76. .notifier_call = tts_notify_reboot,
  77. .next = NULL,
  78. .priority = 0,
  79. };
  80. static int acpi_sleep_prepare(u32 acpi_state)
  81. {
  82. #ifdef CONFIG_ACPI_SLEEP
  83. /* do we have a wakeup address for S2 and S3? */
  84. if (acpi_state == ACPI_STATE_S3) {
  85. if (!acpi_wakeup_address) {
  86. return -EFAULT;
  87. }
  88. acpi_set_firmware_waking_vector(
  89. (acpi_physical_address)acpi_wakeup_address);
  90. }
  91. ACPI_FLUSH_CPU_CACHE();
  92. #endif
  93. printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
  94. acpi_state);
  95. acpi_enable_wakeup_devices(acpi_state);
  96. acpi_enter_sleep_state_prep(acpi_state);
  97. return 0;
  98. }
  99. #ifdef CONFIG_ACPI_SLEEP
  100. static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  101. /*
  102. * The ACPI specification wants us to save NVS memory regions during hibernation
  103. * and to restore them during the subsequent resume. Windows does that also for
  104. * suspend to RAM. However, it is known that this mechanism does not work on
  105. * all machines, so we allow the user to disable it with the help of the
  106. * 'acpi_sleep=nonvs' kernel command line option.
  107. */
  108. static bool nvs_nosave;
  109. void __init acpi_nvs_nosave(void)
  110. {
  111. nvs_nosave = true;
  112. }
  113. /*
  114. * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
  115. * user to request that behavior by using the 'acpi_old_suspend_ordering'
  116. * kernel command line option that causes the following variable to be set.
  117. */
  118. static bool old_suspend_ordering;
  119. void __init acpi_old_suspend_ordering(void)
  120. {
  121. old_suspend_ordering = true;
  122. }
  123. static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
  124. {
  125. acpi_old_suspend_ordering();
  126. return 0;
  127. }
  128. static int __init init_nvs_nosave(const struct dmi_system_id *d)
  129. {
  130. acpi_nvs_nosave();
  131. return 0;
  132. }
  133. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  134. {
  135. .callback = init_old_suspend_ordering,
  136. .ident = "Abit KN9 (nForce4 variant)",
  137. .matches = {
  138. DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
  139. DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
  140. },
  141. },
  142. {
  143. .callback = init_old_suspend_ordering,
  144. .ident = "HP xw4600 Workstation",
  145. .matches = {
  146. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  147. DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
  148. },
  149. },
  150. {
  151. .callback = init_old_suspend_ordering,
  152. .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
  153. .matches = {
  154. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
  155. DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
  156. },
  157. },
  158. {
  159. .callback = init_old_suspend_ordering,
  160. .ident = "Panasonic CF51-2L",
  161. .matches = {
  162. DMI_MATCH(DMI_BOARD_VENDOR,
  163. "Matsushita Electric Industrial Co.,Ltd."),
  164. DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
  165. },
  166. },
  167. {
  168. .callback = init_nvs_nosave,
  169. .ident = "Sony Vaio VGN-FW41E_H",
  170. .matches = {
  171. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  172. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW41E_H"),
  173. },
  174. },
  175. {
  176. .callback = init_nvs_nosave,
  177. .ident = "Sony Vaio VGN-FW21E",
  178. .matches = {
  179. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  180. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
  181. },
  182. },
  183. {
  184. .callback = init_nvs_nosave,
  185. .ident = "Sony Vaio VPCEB17FX",
  186. .matches = {
  187. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  188. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
  189. },
  190. },
  191. {
  192. .callback = init_nvs_nosave,
  193. .ident = "Sony Vaio VGN-SR11M",
  194. .matches = {
  195. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  196. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
  197. },
  198. },
  199. {
  200. .callback = init_nvs_nosave,
  201. .ident = "Everex StepNote Series",
  202. .matches = {
  203. DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
  204. DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
  205. },
  206. },
  207. {
  208. .callback = init_nvs_nosave,
  209. .ident = "Sony Vaio VPCEB1Z1E",
  210. .matches = {
  211. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  212. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
  213. },
  214. },
  215. {
  216. .callback = init_nvs_nosave,
  217. .ident = "Sony Vaio VGN-NW130D",
  218. .matches = {
  219. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  220. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
  221. },
  222. },
  223. {
  224. .callback = init_nvs_nosave,
  225. .ident = "Sony Vaio VPCCW29FX",
  226. .matches = {
  227. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  228. DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
  229. },
  230. },
  231. {
  232. .callback = init_nvs_nosave,
  233. .ident = "Averatec AV1020-ED2",
  234. .matches = {
  235. DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
  236. DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
  237. },
  238. },
  239. {
  240. .callback = init_old_suspend_ordering,
  241. .ident = "Asus A8N-SLI DELUXE",
  242. .matches = {
  243. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  244. DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
  245. },
  246. },
  247. {
  248. .callback = init_old_suspend_ordering,
  249. .ident = "Asus A8N-SLI Premium",
  250. .matches = {
  251. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  252. DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
  253. },
  254. },
  255. {
  256. .callback = init_nvs_nosave,
  257. .ident = "Sony Vaio VGN-SR26GN_P",
  258. .matches = {
  259. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  260. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
  261. },
  262. },
  263. {
  264. .callback = init_nvs_nosave,
  265. .ident = "Sony Vaio VPCEB1S1E",
  266. .matches = {
  267. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  268. DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
  269. },
  270. },
  271. {
  272. .callback = init_nvs_nosave,
  273. .ident = "Sony Vaio VGN-FW520F",
  274. .matches = {
  275. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  276. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
  277. },
  278. },
  279. {
  280. .callback = init_nvs_nosave,
  281. .ident = "Asus K54C",
  282. .matches = {
  283. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  284. DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
  285. },
  286. },
  287. {
  288. .callback = init_nvs_nosave,
  289. .ident = "Asus K54HR",
  290. .matches = {
  291. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  292. DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
  293. },
  294. },
  295. {},
  296. };
  297. static void acpi_sleep_dmi_check(void)
  298. {
  299. dmi_check_system(acpisleep_dmi_table);
  300. }
  301. /**
  302. * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
  303. */
  304. static int acpi_pm_freeze(void)
  305. {
  306. acpi_disable_all_gpes();
  307. acpi_os_wait_events_complete(NULL);
  308. acpi_ec_block_transactions();
  309. return 0;
  310. }
  311. /**
  312. * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
  313. */
  314. static int acpi_pm_pre_suspend(void)
  315. {
  316. acpi_pm_freeze();
  317. return suspend_nvs_save();
  318. }
  319. /**
  320. * __acpi_pm_prepare - Prepare the platform to enter the target state.
  321. *
  322. * If necessary, set the firmware waking vector and do arch-specific
  323. * nastiness to get the wakeup code to the waking vector.
  324. */
  325. static int __acpi_pm_prepare(void)
  326. {
  327. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  328. if (error)
  329. acpi_target_sleep_state = ACPI_STATE_S0;
  330. return error;
  331. }
  332. /**
  333. * acpi_pm_prepare - Prepare the platform to enter the target sleep
  334. * state and disable the GPEs.
  335. */
  336. static int acpi_pm_prepare(void)
  337. {
  338. int error = __acpi_pm_prepare();
  339. if (!error)
  340. error = acpi_pm_pre_suspend();
  341. return error;
  342. }
  343. static int find_powerf_dev(struct device *dev, void *data)
  344. {
  345. struct acpi_device *device = to_acpi_device(dev);
  346. const char *hid = acpi_device_hid(device);
  347. return !strcmp(hid, ACPI_BUTTON_HID_POWERF);
  348. }
  349. /**
  350. * acpi_pm_finish - Instruct the platform to leave a sleep state.
  351. *
  352. * This is called after we wake back up (or if entering the sleep state
  353. * failed).
  354. */
  355. static void acpi_pm_finish(void)
  356. {
  357. struct device *pwr_btn_dev;
  358. u32 acpi_state = acpi_target_sleep_state;
  359. acpi_ec_unblock_transactions();
  360. suspend_nvs_free();
  361. if (acpi_state == ACPI_STATE_S0)
  362. return;
  363. printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
  364. acpi_state);
  365. acpi_disable_wakeup_devices(acpi_state);
  366. acpi_leave_sleep_state(acpi_state);
  367. /* reset firmware waking vector */
  368. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  369. acpi_target_sleep_state = ACPI_STATE_S0;
  370. /* If we were woken with the fixed power button, provide a small
  371. * hint to userspace in the form of a wakeup event on the fixed power
  372. * button device (if it can be found).
  373. *
  374. * We delay the event generation til now, as the PM layer requires
  375. * timekeeping to be running before we generate events. */
  376. if (!pwr_btn_event_pending)
  377. return;
  378. pwr_btn_event_pending = false;
  379. pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL,
  380. find_powerf_dev);
  381. if (pwr_btn_dev) {
  382. pm_wakeup_event(pwr_btn_dev, 0);
  383. put_device(pwr_btn_dev);
  384. }
  385. }
  386. /**
  387. * acpi_pm_end - Finish up suspend sequence.
  388. */
  389. static void acpi_pm_end(void)
  390. {
  391. /*
  392. * This is necessary in case acpi_pm_finish() is not called during a
  393. * failing transition to a sleep state.
  394. */
  395. acpi_target_sleep_state = ACPI_STATE_S0;
  396. acpi_sleep_tts_switch(acpi_target_sleep_state);
  397. }
  398. #else /* !CONFIG_ACPI_SLEEP */
  399. #define acpi_target_sleep_state ACPI_STATE_S0
  400. static inline void acpi_sleep_dmi_check(void) {}
  401. #endif /* CONFIG_ACPI_SLEEP */
  402. #ifdef CONFIG_SUSPEND
  403. static u32 acpi_suspend_states[] = {
  404. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  405. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  406. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  407. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  408. };
  409. /**
  410. * acpi_suspend_begin - Set the target system sleep state to the state
  411. * associated with given @pm_state, if supported.
  412. */
  413. static int acpi_suspend_begin(suspend_state_t pm_state)
  414. {
  415. u32 acpi_state = acpi_suspend_states[pm_state];
  416. int error = 0;
  417. error = nvs_nosave ? 0 : suspend_nvs_alloc();
  418. if (error)
  419. return error;
  420. if (sleep_states[acpi_state]) {
  421. acpi_target_sleep_state = acpi_state;
  422. acpi_sleep_tts_switch(acpi_target_sleep_state);
  423. } else {
  424. printk(KERN_ERR "ACPI does not support this state: %d\n",
  425. pm_state);
  426. error = -ENOSYS;
  427. }
  428. return error;
  429. }
  430. /**
  431. * acpi_suspend_enter - Actually enter a sleep state.
  432. * @pm_state: ignored
  433. *
  434. * Flush caches and go to sleep. For STR we have to call arch-specific
  435. * assembly, which in turn call acpi_enter_sleep_state().
  436. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  437. */
  438. static int acpi_suspend_enter(suspend_state_t pm_state)
  439. {
  440. acpi_status status = AE_OK;
  441. u32 acpi_state = acpi_target_sleep_state;
  442. int error;
  443. ACPI_FLUSH_CPU_CACHE();
  444. switch (acpi_state) {
  445. case ACPI_STATE_S1:
  446. barrier();
  447. status = acpi_enter_sleep_state(acpi_state, wake_sleep_flags);
  448. break;
  449. case ACPI_STATE_S3:
  450. error = acpi_suspend_lowlevel();
  451. if (error)
  452. return error;
  453. pr_info(PREFIX "Low-level resume complete\n");
  454. break;
  455. }
  456. /* This violates the spec but is required for bug compatibility. */
  457. acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
  458. /* Reprogram control registers and execute _BFS */
  459. acpi_leave_sleep_state_prep(acpi_state, wake_sleep_flags);
  460. /* ACPI 3.0 specs (P62) says that it's the responsibility
  461. * of the OSPM to clear the status bit [ implying that the
  462. * POWER_BUTTON event should not reach userspace ]
  463. *
  464. * However, we do generate a small hint for userspace in the form of
  465. * a wakeup event. We flag this condition for now and generate the
  466. * event later, as we're currently too early in resume to be able to
  467. * generate wakeup events.
  468. */
  469. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
  470. acpi_event_status pwr_btn_status;
  471. acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
  472. if (pwr_btn_status & ACPI_EVENT_FLAG_SET) {
  473. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  474. /* Flag for later */
  475. pwr_btn_event_pending = true;
  476. }
  477. }
  478. /*
  479. * Disable and clear GPE status before interrupt is enabled. Some GPEs
  480. * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
  481. * acpi_leave_sleep_state will reenable specific GPEs later
  482. */
  483. acpi_disable_all_gpes();
  484. /* Allow EC transactions to happen. */
  485. acpi_ec_unblock_transactions_early();
  486. suspend_nvs_restore();
  487. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  488. }
  489. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  490. {
  491. u32 acpi_state;
  492. switch (pm_state) {
  493. case PM_SUSPEND_ON:
  494. case PM_SUSPEND_STANDBY:
  495. case PM_SUSPEND_MEM:
  496. acpi_state = acpi_suspend_states[pm_state];
  497. return sleep_states[acpi_state];
  498. default:
  499. return 0;
  500. }
  501. }
  502. static const struct platform_suspend_ops acpi_suspend_ops = {
  503. .valid = acpi_suspend_state_valid,
  504. .begin = acpi_suspend_begin,
  505. .prepare_late = acpi_pm_prepare,
  506. .enter = acpi_suspend_enter,
  507. .wake = acpi_pm_finish,
  508. .end = acpi_pm_end,
  509. };
  510. /**
  511. * acpi_suspend_begin_old - Set the target system sleep state to the
  512. * state associated with given @pm_state, if supported, and
  513. * execute the _PTS control method. This function is used if the
  514. * pre-ACPI 2.0 suspend ordering has been requested.
  515. */
  516. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  517. {
  518. int error = acpi_suspend_begin(pm_state);
  519. if (!error)
  520. error = __acpi_pm_prepare();
  521. return error;
  522. }
  523. /*
  524. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  525. * been requested.
  526. */
  527. static const struct platform_suspend_ops acpi_suspend_ops_old = {
  528. .valid = acpi_suspend_state_valid,
  529. .begin = acpi_suspend_begin_old,
  530. .prepare_late = acpi_pm_pre_suspend,
  531. .enter = acpi_suspend_enter,
  532. .wake = acpi_pm_finish,
  533. .end = acpi_pm_end,
  534. .recover = acpi_pm_finish,
  535. };
  536. #endif /* CONFIG_SUSPEND */
  537. #ifdef CONFIG_HIBERNATION
  538. static unsigned long s4_hardware_signature;
  539. static struct acpi_table_facs *facs;
  540. static bool nosigcheck;
  541. void __init acpi_no_s4_hw_signature(void)
  542. {
  543. nosigcheck = true;
  544. }
  545. static int acpi_hibernation_begin(void)
  546. {
  547. int error;
  548. error = nvs_nosave ? 0 : suspend_nvs_alloc();
  549. if (!error) {
  550. acpi_target_sleep_state = ACPI_STATE_S4;
  551. acpi_sleep_tts_switch(acpi_target_sleep_state);
  552. }
  553. return error;
  554. }
  555. static int acpi_hibernation_enter(void)
  556. {
  557. acpi_status status = AE_OK;
  558. ACPI_FLUSH_CPU_CACHE();
  559. /* This shouldn't return. If it returns, we have a problem */
  560. status = acpi_enter_sleep_state(ACPI_STATE_S4, wake_sleep_flags);
  561. /* Reprogram control registers and execute _BFS */
  562. acpi_leave_sleep_state_prep(ACPI_STATE_S4, wake_sleep_flags);
  563. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  564. }
  565. static void acpi_hibernation_leave(void)
  566. {
  567. /*
  568. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  569. * enable it here.
  570. */
  571. acpi_enable();
  572. /* Reprogram control registers and execute _BFS */
  573. acpi_leave_sleep_state_prep(ACPI_STATE_S4, wake_sleep_flags);
  574. /* Check the hardware signature */
  575. if (facs && s4_hardware_signature != facs->hardware_signature) {
  576. printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
  577. "cannot resume!\n");
  578. panic("ACPI S4 hardware signature mismatch");
  579. }
  580. /* Restore the NVS memory area */
  581. suspend_nvs_restore();
  582. /* Allow EC transactions to happen. */
  583. acpi_ec_unblock_transactions_early();
  584. }
  585. static void acpi_pm_thaw(void)
  586. {
  587. acpi_ec_unblock_transactions();
  588. acpi_enable_all_runtime_gpes();
  589. }
  590. static const struct platform_hibernation_ops acpi_hibernation_ops = {
  591. .begin = acpi_hibernation_begin,
  592. .end = acpi_pm_end,
  593. .pre_snapshot = acpi_pm_prepare,
  594. .finish = acpi_pm_finish,
  595. .prepare = acpi_pm_prepare,
  596. .enter = acpi_hibernation_enter,
  597. .leave = acpi_hibernation_leave,
  598. .pre_restore = acpi_pm_freeze,
  599. .restore_cleanup = acpi_pm_thaw,
  600. };
  601. /**
  602. * acpi_hibernation_begin_old - Set the target system sleep state to
  603. * ACPI_STATE_S4 and execute the _PTS control method. This
  604. * function is used if the pre-ACPI 2.0 suspend ordering has been
  605. * requested.
  606. */
  607. static int acpi_hibernation_begin_old(void)
  608. {
  609. int error;
  610. /*
  611. * The _TTS object should always be evaluated before the _PTS object.
  612. * When the old_suspended_ordering is true, the _PTS object is
  613. * evaluated in the acpi_sleep_prepare.
  614. */
  615. acpi_sleep_tts_switch(ACPI_STATE_S4);
  616. error = acpi_sleep_prepare(ACPI_STATE_S4);
  617. if (!error) {
  618. if (!nvs_nosave)
  619. error = suspend_nvs_alloc();
  620. if (!error)
  621. acpi_target_sleep_state = ACPI_STATE_S4;
  622. }
  623. return error;
  624. }
  625. /*
  626. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  627. * been requested.
  628. */
  629. static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
  630. .begin = acpi_hibernation_begin_old,
  631. .end = acpi_pm_end,
  632. .pre_snapshot = acpi_pm_pre_suspend,
  633. .prepare = acpi_pm_freeze,
  634. .finish = acpi_pm_finish,
  635. .enter = acpi_hibernation_enter,
  636. .leave = acpi_hibernation_leave,
  637. .pre_restore = acpi_pm_freeze,
  638. .restore_cleanup = acpi_pm_thaw,
  639. .recover = acpi_pm_finish,
  640. };
  641. #endif /* CONFIG_HIBERNATION */
  642. int acpi_suspend(u32 acpi_state)
  643. {
  644. suspend_state_t states[] = {
  645. [1] = PM_SUSPEND_STANDBY,
  646. [3] = PM_SUSPEND_MEM,
  647. [5] = PM_SUSPEND_MAX
  648. };
  649. if (acpi_state < 6 && states[acpi_state])
  650. return pm_suspend(states[acpi_state]);
  651. if (acpi_state == 4)
  652. return hibernate();
  653. return -EINVAL;
  654. }
  655. #ifdef CONFIG_PM
  656. /**
  657. * acpi_pm_device_sleep_state - return preferred power state of ACPI device
  658. * in the system sleep state given by %acpi_target_sleep_state
  659. * @dev: device to examine; its driver model wakeup flags control
  660. * whether it should be able to wake up the system
  661. * @d_min_p: used to store the upper limit of allowed states range
  662. * Return value: preferred power state of the device on success, -ENODEV on
  663. * failure (ie. if there's no 'struct acpi_device' for @dev)
  664. *
  665. * Find the lowest power (highest number) ACPI device power state that
  666. * device @dev can be in while the system is in the sleep state represented
  667. * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
  668. * able to wake up the system from this sleep state. If @d_min_p is set,
  669. * the highest power (lowest number) device power state of @dev allowed
  670. * in this system sleep state is stored at the location pointed to by it.
  671. *
  672. * The caller must ensure that @dev is valid before using this function.
  673. * The caller is also responsible for figuring out if the device is
  674. * supposed to be able to wake up the system and passing this information
  675. * via @wake.
  676. */
  677. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
  678. {
  679. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  680. struct acpi_device *adev;
  681. char acpi_method[] = "_SxD";
  682. unsigned long long d_min, d_max;
  683. bool wakeup = false;
  684. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  685. printk(KERN_DEBUG "ACPI handle has no context!\n");
  686. return -ENODEV;
  687. }
  688. if (d_max_in > ACPI_STATE_D3_HOT) {
  689. enum pm_qos_flags_status stat;
  690. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  691. if (stat == PM_QOS_FLAGS_ALL)
  692. d_max_in = ACPI_STATE_D3_HOT;
  693. }
  694. acpi_method[2] = '0' + acpi_target_sleep_state;
  695. /*
  696. * If the sleep state is S0, we will return D3, but if the device has
  697. * _S0W, we will use the value from _S0W
  698. */
  699. d_min = ACPI_STATE_D0;
  700. d_max = ACPI_STATE_D3;
  701. /*
  702. * If present, _SxD methods return the minimum D-state (highest power
  703. * state) we can use for the corresponding S-states. Otherwise, the
  704. * minimum D-state is D0 (ACPI 3.x).
  705. *
  706. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  707. * provided -- that's our fault recovery, we ignore retval.
  708. */
  709. if (acpi_target_sleep_state > ACPI_STATE_S0) {
  710. acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
  711. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  712. && adev->wakeup.sleep_state >= acpi_target_sleep_state;
  713. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  714. PM_QOS_FLAGS_NONE) {
  715. wakeup = adev->wakeup.flags.valid;
  716. }
  717. /*
  718. * If _PRW says we can wake up the system from the target sleep state,
  719. * the D-state returned by _SxD is sufficient for that (we assume a
  720. * wakeup-aware driver if wake is set). Still, if _SxW exists
  721. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  722. * can wake the system. _S0W may be valid, too.
  723. */
  724. if (wakeup) {
  725. acpi_status status;
  726. acpi_method[3] = 'W';
  727. status = acpi_evaluate_integer(handle, acpi_method, NULL,
  728. &d_max);
  729. if (ACPI_FAILURE(status)) {
  730. if (acpi_target_sleep_state != ACPI_STATE_S0 ||
  731. status != AE_NOT_FOUND)
  732. d_max = d_min;
  733. } else if (d_max < d_min) {
  734. /* Warn the user of the broken DSDT */
  735. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  736. acpi_method);
  737. /* Sanitize it */
  738. d_min = d_max;
  739. }
  740. }
  741. if (d_min_p)
  742. *d_min_p = d_min;
  743. return d_max;
  744. }
  745. #endif /* CONFIG_PM */
  746. #ifdef CONFIG_PM_SLEEP
  747. /**
  748. * acpi_pm_device_run_wake - Enable/disable wake-up for given device.
  749. * @phys_dev: Device to enable/disable the platform to wake-up the system for.
  750. * @enable: Whether enable or disable the wake-up functionality.
  751. *
  752. * Find the ACPI device object corresponding to @pci_dev and try to
  753. * enable/disable the GPE associated with it.
  754. */
  755. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  756. {
  757. struct acpi_device *dev;
  758. acpi_handle handle;
  759. if (!device_run_wake(phys_dev))
  760. return -EINVAL;
  761. handle = DEVICE_ACPI_HANDLE(phys_dev);
  762. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
  763. dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
  764. __func__);
  765. return -ENODEV;
  766. }
  767. if (enable) {
  768. acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);
  769. acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
  770. } else {
  771. acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
  772. acpi_disable_wakeup_device_power(dev);
  773. }
  774. return 0;
  775. }
  776. /**
  777. * acpi_pm_device_sleep_wake - enable or disable the system wake-up
  778. * capability of given device
  779. * @dev: device to handle
  780. * @enable: 'true' - enable, 'false' - disable the wake-up capability
  781. */
  782. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  783. {
  784. acpi_handle handle;
  785. struct acpi_device *adev;
  786. int error;
  787. if (!device_can_wakeup(dev))
  788. return -EINVAL;
  789. handle = DEVICE_ACPI_HANDLE(dev);
  790. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  791. dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
  792. return -ENODEV;
  793. }
  794. error = enable ?
  795. acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
  796. acpi_disable_wakeup_device_power(adev);
  797. if (!error)
  798. dev_info(dev, "wake-up capability %s by ACPI\n",
  799. enable ? "enabled" : "disabled");
  800. return error;
  801. }
  802. #endif /* CONFIG_PM_SLEEP */
  803. static void acpi_power_off_prepare(void)
  804. {
  805. /* Prepare to power off the system */
  806. acpi_sleep_prepare(ACPI_STATE_S5);
  807. acpi_disable_all_gpes();
  808. }
  809. static void acpi_power_off(void)
  810. {
  811. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  812. printk(KERN_DEBUG "%s called\n", __func__);
  813. local_irq_disable();
  814. acpi_enter_sleep_state(ACPI_STATE_S5, wake_sleep_flags);
  815. }
  816. /*
  817. * ACPI 2.0 created the optional _GTS and _BFS,
  818. * but industry adoption has been neither rapid nor broad.
  819. *
  820. * Linux gets into trouble when it executes poorly validated
  821. * paths through the BIOS, so disable _GTS and _BFS by default,
  822. * but do speak up and offer the option to enable them.
  823. */
  824. static void __init acpi_gts_bfs_check(void)
  825. {
  826. acpi_handle dummy;
  827. if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_PATHNAME__GTS, &dummy)))
  828. {
  829. printk(KERN_NOTICE PREFIX "BIOS offers _GTS\n");
  830. printk(KERN_NOTICE PREFIX "If \"acpi.gts=1\" improves suspend, "
  831. "please notify linux-acpi@vger.kernel.org\n");
  832. }
  833. if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_PATHNAME__BFS, &dummy)))
  834. {
  835. printk(KERN_NOTICE PREFIX "BIOS offers _BFS\n");
  836. printk(KERN_NOTICE PREFIX "If \"acpi.bfs=1\" improves resume, "
  837. "please notify linux-acpi@vger.kernel.org\n");
  838. }
  839. }
  840. int __init acpi_sleep_init(void)
  841. {
  842. acpi_status status;
  843. u8 type_a, type_b;
  844. #ifdef CONFIG_SUSPEND
  845. int i = 0;
  846. #endif
  847. if (acpi_disabled)
  848. return 0;
  849. acpi_sleep_dmi_check();
  850. sleep_states[ACPI_STATE_S0] = 1;
  851. printk(KERN_INFO PREFIX "(supports S0");
  852. #ifdef CONFIG_SUSPEND
  853. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
  854. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  855. if (ACPI_SUCCESS(status)) {
  856. sleep_states[i] = 1;
  857. printk(" S%d", i);
  858. }
  859. }
  860. suspend_set_ops(old_suspend_ordering ?
  861. &acpi_suspend_ops_old : &acpi_suspend_ops);
  862. #endif
  863. #ifdef CONFIG_HIBERNATION
  864. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  865. if (ACPI_SUCCESS(status)) {
  866. hibernation_set_ops(old_suspend_ordering ?
  867. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  868. sleep_states[ACPI_STATE_S4] = 1;
  869. printk(" S4");
  870. if (!nosigcheck) {
  871. acpi_get_table(ACPI_SIG_FACS, 1,
  872. (struct acpi_table_header **)&facs);
  873. if (facs)
  874. s4_hardware_signature =
  875. facs->hardware_signature;
  876. }
  877. }
  878. #endif
  879. status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
  880. if (ACPI_SUCCESS(status)) {
  881. sleep_states[ACPI_STATE_S5] = 1;
  882. printk(" S5");
  883. pm_power_off_prepare = acpi_power_off_prepare;
  884. pm_power_off = acpi_power_off;
  885. }
  886. printk(")\n");
  887. /*
  888. * Register the tts_notifier to reboot notifier list so that the _TTS
  889. * object can also be evaluated when the system enters S5.
  890. */
  891. register_reboot_notifier(&tts_notifier);
  892. acpi_gts_bfs_check();
  893. return 0;
  894. }