sleep.c 20 KB

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