sleep.c 24 KB

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