sleep.c 24 KB

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