sleep.c 24 KB

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