sleep.c 24 KB

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