sleep.c 25 KB

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