sleep.c 23 KB

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