sleep.c 26 KB

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