sleep.c 25 KB

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