sleep.c 25 KB

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