sleep.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 <asm/io.h>
  19. #include <acpi/acpi_bus.h>
  20. #include <acpi/acpi_drivers.h>
  21. #include "internal.h"
  22. #include "sleep.h"
  23. u8 sleep_states[ACPI_S_STATE_COUNT];
  24. static void acpi_sleep_tts_switch(u32 acpi_state)
  25. {
  26. union acpi_object in_arg = { ACPI_TYPE_INTEGER };
  27. struct acpi_object_list arg_list = { 1, &in_arg };
  28. acpi_status status = AE_OK;
  29. in_arg.integer.value = acpi_state;
  30. status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
  31. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  32. /*
  33. * OS can't evaluate the _TTS object correctly. Some warning
  34. * message will be printed. But it won't break anything.
  35. */
  36. printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
  37. }
  38. }
  39. static int tts_notify_reboot(struct notifier_block *this,
  40. unsigned long code, void *x)
  41. {
  42. acpi_sleep_tts_switch(ACPI_STATE_S5);
  43. return NOTIFY_DONE;
  44. }
  45. static struct notifier_block tts_notifier = {
  46. .notifier_call = tts_notify_reboot,
  47. .next = NULL,
  48. .priority = 0,
  49. };
  50. static int acpi_sleep_prepare(u32 acpi_state)
  51. {
  52. #ifdef CONFIG_ACPI_SLEEP
  53. /* do we have a wakeup address for S2 and S3? */
  54. if (acpi_state == ACPI_STATE_S3) {
  55. if (!acpi_wakeup_address) {
  56. return -EFAULT;
  57. }
  58. acpi_set_firmware_waking_vector(
  59. (acpi_physical_address)acpi_wakeup_address);
  60. }
  61. ACPI_FLUSH_CPU_CACHE();
  62. acpi_enable_wakeup_device_prep(acpi_state);
  63. #endif
  64. printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
  65. 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. /*
  72. * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
  73. * user to request that behavior by using the 'acpi_old_suspend_ordering'
  74. * kernel command line option that causes the following variable to be set.
  75. */
  76. static bool old_suspend_ordering;
  77. void __init acpi_old_suspend_ordering(void)
  78. {
  79. old_suspend_ordering = true;
  80. }
  81. /**
  82. * acpi_pm_disable_gpes - Disable the GPEs.
  83. */
  84. static int acpi_pm_disable_gpes(void)
  85. {
  86. acpi_disable_all_gpes();
  87. return 0;
  88. }
  89. /**
  90. * __acpi_pm_prepare - Prepare the platform to enter the target state.
  91. *
  92. * If necessary, set the firmware waking vector and do arch-specific
  93. * nastiness to get the wakeup code to the waking vector.
  94. */
  95. static int __acpi_pm_prepare(void)
  96. {
  97. int error = acpi_sleep_prepare(acpi_target_sleep_state);
  98. if (error)
  99. acpi_target_sleep_state = ACPI_STATE_S0;
  100. return error;
  101. }
  102. /**
  103. * acpi_pm_prepare - Prepare the platform to enter the target sleep
  104. * state and disable the GPEs.
  105. */
  106. static int acpi_pm_prepare(void)
  107. {
  108. int error = __acpi_pm_prepare();
  109. if (!error)
  110. acpi_disable_all_gpes();
  111. return error;
  112. }
  113. /**
  114. * acpi_pm_finish - Instruct the platform to leave a sleep state.
  115. *
  116. * This is called after we wake back up (or if entering the sleep state
  117. * failed).
  118. */
  119. static void acpi_pm_finish(void)
  120. {
  121. u32 acpi_state = acpi_target_sleep_state;
  122. if (acpi_state == ACPI_STATE_S0)
  123. return;
  124. printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
  125. acpi_state);
  126. acpi_disable_wakeup_device(acpi_state);
  127. acpi_leave_sleep_state(acpi_state);
  128. /* reset firmware waking vector */
  129. acpi_set_firmware_waking_vector((acpi_physical_address) 0);
  130. acpi_target_sleep_state = ACPI_STATE_S0;
  131. }
  132. /**
  133. * acpi_pm_end - Finish up suspend sequence.
  134. */
  135. static void acpi_pm_end(void)
  136. {
  137. /*
  138. * This is necessary in case acpi_pm_finish() is not called during a
  139. * failing transition to a sleep state.
  140. */
  141. acpi_target_sleep_state = ACPI_STATE_S0;
  142. acpi_sleep_tts_switch(acpi_target_sleep_state);
  143. }
  144. #else /* !CONFIG_ACPI_SLEEP */
  145. #define acpi_target_sleep_state ACPI_STATE_S0
  146. #endif /* CONFIG_ACPI_SLEEP */
  147. #ifdef CONFIG_SUSPEND
  148. /*
  149. * According to the ACPI specification the BIOS should make sure that ACPI is
  150. * enabled and SCI_EN bit is set on wake-up from S1 - S3 sleep states. Still,
  151. * some BIOSes don't do that and therefore we use acpi_enable() to enable ACPI
  152. * on such systems during resume. Unfortunately that doesn't help in
  153. * particularly pathological cases in which SCI_EN has to be set directly on
  154. * resume, although the specification states very clearly that this flag is
  155. * owned by the hardware. The set_sci_en_on_resume variable will be set in such
  156. * cases.
  157. */
  158. static bool set_sci_en_on_resume;
  159. extern void do_suspend_lowlevel(void);
  160. static u32 acpi_suspend_states[] = {
  161. [PM_SUSPEND_ON] = ACPI_STATE_S0,
  162. [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
  163. [PM_SUSPEND_MEM] = ACPI_STATE_S3,
  164. [PM_SUSPEND_MAX] = ACPI_STATE_S5
  165. };
  166. /**
  167. * acpi_suspend_begin - Set the target system sleep state to the state
  168. * associated with given @pm_state, if supported.
  169. */
  170. static int acpi_suspend_begin(suspend_state_t pm_state)
  171. {
  172. u32 acpi_state = acpi_suspend_states[pm_state];
  173. int error = 0;
  174. if (sleep_states[acpi_state]) {
  175. acpi_target_sleep_state = acpi_state;
  176. acpi_sleep_tts_switch(acpi_target_sleep_state);
  177. } else {
  178. printk(KERN_ERR "ACPI does not support this state: %d\n",
  179. pm_state);
  180. error = -ENOSYS;
  181. }
  182. return error;
  183. }
  184. /**
  185. * acpi_suspend_enter - Actually enter a sleep state.
  186. * @pm_state: ignored
  187. *
  188. * Flush caches and go to sleep. For STR we have to call arch-specific
  189. * assembly, which in turn call acpi_enter_sleep_state().
  190. * It's unfortunate, but it works. Please fix if you're feeling frisky.
  191. */
  192. static int acpi_suspend_enter(suspend_state_t pm_state)
  193. {
  194. acpi_status status = AE_OK;
  195. unsigned long flags = 0;
  196. u32 acpi_state = acpi_target_sleep_state;
  197. ACPI_FLUSH_CPU_CACHE();
  198. /* Do arch specific saving of state. */
  199. if (acpi_state == ACPI_STATE_S3) {
  200. int error = acpi_save_state_mem();
  201. if (error)
  202. return error;
  203. }
  204. local_irq_save(flags);
  205. acpi_enable_wakeup_device(acpi_state);
  206. switch (acpi_state) {
  207. case ACPI_STATE_S1:
  208. barrier();
  209. status = acpi_enter_sleep_state(acpi_state);
  210. break;
  211. case ACPI_STATE_S3:
  212. do_suspend_lowlevel();
  213. break;
  214. }
  215. /* If ACPI is not enabled by the BIOS, we need to enable it here. */
  216. if (set_sci_en_on_resume)
  217. acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
  218. else
  219. acpi_enable();
  220. /* Reprogram control registers and execute _BFS */
  221. acpi_leave_sleep_state_prep(acpi_state);
  222. /* ACPI 3.0 specs (P62) says that it's the responsibility
  223. * of the OSPM to clear the status bit [ implying that the
  224. * POWER_BUTTON event should not reach userspace ]
  225. */
  226. if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3))
  227. acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
  228. /*
  229. * Disable and clear GPE status before interrupt is enabled. Some GPEs
  230. * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
  231. * acpi_leave_sleep_state will reenable specific GPEs later
  232. */
  233. acpi_disable_all_gpes();
  234. local_irq_restore(flags);
  235. printk(KERN_DEBUG "Back to C!\n");
  236. /* restore processor state */
  237. if (acpi_state == ACPI_STATE_S3)
  238. acpi_restore_state_mem();
  239. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  240. }
  241. static int acpi_suspend_state_valid(suspend_state_t pm_state)
  242. {
  243. u32 acpi_state;
  244. switch (pm_state) {
  245. case PM_SUSPEND_ON:
  246. case PM_SUSPEND_STANDBY:
  247. case PM_SUSPEND_MEM:
  248. acpi_state = acpi_suspend_states[pm_state];
  249. return sleep_states[acpi_state];
  250. default:
  251. return 0;
  252. }
  253. }
  254. static struct platform_suspend_ops acpi_suspend_ops = {
  255. .valid = acpi_suspend_state_valid,
  256. .begin = acpi_suspend_begin,
  257. .prepare_late = acpi_pm_prepare,
  258. .enter = acpi_suspend_enter,
  259. .wake = acpi_pm_finish,
  260. .end = acpi_pm_end,
  261. };
  262. /**
  263. * acpi_suspend_begin_old - Set the target system sleep state to the
  264. * state associated with given @pm_state, if supported, and
  265. * execute the _PTS control method. This function is used if the
  266. * pre-ACPI 2.0 suspend ordering has been requested.
  267. */
  268. static int acpi_suspend_begin_old(suspend_state_t pm_state)
  269. {
  270. int error = acpi_suspend_begin(pm_state);
  271. if (!error)
  272. error = __acpi_pm_prepare();
  273. return error;
  274. }
  275. /*
  276. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  277. * been requested.
  278. */
  279. static struct platform_suspend_ops acpi_suspend_ops_old = {
  280. .valid = acpi_suspend_state_valid,
  281. .begin = acpi_suspend_begin_old,
  282. .prepare_late = acpi_pm_disable_gpes,
  283. .enter = acpi_suspend_enter,
  284. .wake = acpi_pm_finish,
  285. .end = acpi_pm_end,
  286. .recover = acpi_pm_finish,
  287. };
  288. static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
  289. {
  290. old_suspend_ordering = true;
  291. return 0;
  292. }
  293. static int __init init_set_sci_en_on_resume(const struct dmi_system_id *d)
  294. {
  295. set_sci_en_on_resume = true;
  296. return 0;
  297. }
  298. static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
  299. {
  300. .callback = init_old_suspend_ordering,
  301. .ident = "Abit KN9 (nForce4 variant)",
  302. .matches = {
  303. DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
  304. DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
  305. },
  306. },
  307. {
  308. .callback = init_old_suspend_ordering,
  309. .ident = "HP xw4600 Workstation",
  310. .matches = {
  311. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  312. DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
  313. },
  314. },
  315. {
  316. .callback = init_set_sci_en_on_resume,
  317. .ident = "Apple MacBook 1,1",
  318. .matches = {
  319. DMI_MATCH(DMI_SYS_VENDOR, "Apple Computer, Inc."),
  320. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook1,1"),
  321. },
  322. },
  323. {
  324. .callback = init_set_sci_en_on_resume,
  325. .ident = "Apple MacMini 1,1",
  326. .matches = {
  327. DMI_MATCH(DMI_SYS_VENDOR, "Apple Computer, Inc."),
  328. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini1,1"),
  329. },
  330. },
  331. {
  332. .callback = init_old_suspend_ordering,
  333. .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
  334. .matches = {
  335. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
  336. DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
  337. },
  338. },
  339. {
  340. .callback = init_set_sci_en_on_resume,
  341. .ident = "Toshiba Satellite L300",
  342. .matches = {
  343. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  344. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite L300"),
  345. },
  346. },
  347. {
  348. .callback = init_set_sci_en_on_resume,
  349. .ident = "Hewlett-Packard HP G7000 Notebook PC",
  350. .matches = {
  351. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  352. DMI_MATCH(DMI_PRODUCT_NAME, "HP G7000 Notebook PC"),
  353. },
  354. },
  355. {
  356. .callback = init_set_sci_en_on_resume,
  357. .ident = "Hewlett-Packard HP Pavilion dv3 Notebook PC",
  358. .matches = {
  359. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  360. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion dv3 Notebook PC"),
  361. },
  362. },
  363. {
  364. .callback = init_set_sci_en_on_resume,
  365. .ident = "Hewlett-Packard Pavilion dv4",
  366. .matches = {
  367. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  368. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion dv4"),
  369. },
  370. },
  371. {
  372. .callback = init_set_sci_en_on_resume,
  373. .ident = "Hewlett-Packard Pavilion dv7",
  374. .matches = {
  375. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  376. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion dv7"),
  377. },
  378. },
  379. {
  380. .callback = init_set_sci_en_on_resume,
  381. .ident = "Hewlett-Packard Compaq Presario C700 Notebook PC",
  382. .matches = {
  383. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  384. DMI_MATCH(DMI_PRODUCT_NAME, "Compaq Presario C700 Notebook PC"),
  385. },
  386. },
  387. {
  388. .callback = init_set_sci_en_on_resume,
  389. .ident = "Hewlett-Packard Compaq Presario CQ40 Notebook PC",
  390. .matches = {
  391. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  392. DMI_MATCH(DMI_PRODUCT_NAME, "Compaq Presario CQ40 Notebook PC"),
  393. },
  394. },
  395. {
  396. .callback = init_old_suspend_ordering,
  397. .ident = "Panasonic CF51-2L",
  398. .matches = {
  399. DMI_MATCH(DMI_BOARD_VENDOR,
  400. "Matsushita Electric Industrial Co.,Ltd."),
  401. DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
  402. },
  403. },
  404. {},
  405. };
  406. #endif /* CONFIG_SUSPEND */
  407. #ifdef CONFIG_HIBERNATION
  408. /*
  409. * The ACPI specification wants us to save NVS memory regions during hibernation
  410. * and to restore them during the subsequent resume. However, it is not certain
  411. * if this mechanism is going to work on all machines, so we allow the user to
  412. * disable this mechanism using the 'acpi_sleep=s4_nonvs' kernel command line
  413. * option.
  414. */
  415. static bool s4_no_nvs;
  416. void __init acpi_s4_no_nvs(void)
  417. {
  418. s4_no_nvs = true;
  419. }
  420. static unsigned long s4_hardware_signature;
  421. static struct acpi_table_facs *facs;
  422. static bool nosigcheck;
  423. void __init acpi_no_s4_hw_signature(void)
  424. {
  425. nosigcheck = true;
  426. }
  427. static int acpi_hibernation_begin(void)
  428. {
  429. int error;
  430. error = s4_no_nvs ? 0 : hibernate_nvs_alloc();
  431. if (!error) {
  432. acpi_target_sleep_state = ACPI_STATE_S4;
  433. acpi_sleep_tts_switch(acpi_target_sleep_state);
  434. }
  435. return error;
  436. }
  437. static int acpi_hibernation_pre_snapshot(void)
  438. {
  439. int error = acpi_pm_prepare();
  440. if (!error)
  441. hibernate_nvs_save();
  442. return error;
  443. }
  444. static int acpi_hibernation_enter(void)
  445. {
  446. acpi_status status = AE_OK;
  447. unsigned long flags = 0;
  448. ACPI_FLUSH_CPU_CACHE();
  449. local_irq_save(flags);
  450. acpi_enable_wakeup_device(ACPI_STATE_S4);
  451. /* This shouldn't return. If it returns, we have a problem */
  452. status = acpi_enter_sleep_state(ACPI_STATE_S4);
  453. /* Reprogram control registers and execute _BFS */
  454. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  455. local_irq_restore(flags);
  456. return ACPI_SUCCESS(status) ? 0 : -EFAULT;
  457. }
  458. static void acpi_hibernation_finish(void)
  459. {
  460. hibernate_nvs_free();
  461. acpi_pm_finish();
  462. }
  463. static void acpi_hibernation_leave(void)
  464. {
  465. /*
  466. * If ACPI is not enabled by the BIOS and the boot kernel, we need to
  467. * enable it here.
  468. */
  469. acpi_enable();
  470. /* Reprogram control registers and execute _BFS */
  471. acpi_leave_sleep_state_prep(ACPI_STATE_S4);
  472. /* Check the hardware signature */
  473. if (facs && s4_hardware_signature != facs->hardware_signature) {
  474. printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
  475. "cannot resume!\n");
  476. panic("ACPI S4 hardware signature mismatch");
  477. }
  478. /* Restore the NVS memory area */
  479. hibernate_nvs_restore();
  480. }
  481. static void acpi_pm_enable_gpes(void)
  482. {
  483. acpi_enable_all_runtime_gpes();
  484. }
  485. static struct platform_hibernation_ops acpi_hibernation_ops = {
  486. .begin = acpi_hibernation_begin,
  487. .end = acpi_pm_end,
  488. .pre_snapshot = acpi_hibernation_pre_snapshot,
  489. .finish = acpi_hibernation_finish,
  490. .prepare = acpi_pm_prepare,
  491. .enter = acpi_hibernation_enter,
  492. .leave = acpi_hibernation_leave,
  493. .pre_restore = acpi_pm_disable_gpes,
  494. .restore_cleanup = acpi_pm_enable_gpes,
  495. };
  496. /**
  497. * acpi_hibernation_begin_old - Set the target system sleep state to
  498. * ACPI_STATE_S4 and execute the _PTS control method. This
  499. * function is used if the pre-ACPI 2.0 suspend ordering has been
  500. * requested.
  501. */
  502. static int acpi_hibernation_begin_old(void)
  503. {
  504. int error;
  505. /*
  506. * The _TTS object should always be evaluated before the _PTS object.
  507. * When the old_suspended_ordering is true, the _PTS object is
  508. * evaluated in the acpi_sleep_prepare.
  509. */
  510. acpi_sleep_tts_switch(ACPI_STATE_S4);
  511. error = acpi_sleep_prepare(ACPI_STATE_S4);
  512. if (!error) {
  513. if (!s4_no_nvs)
  514. error = hibernate_nvs_alloc();
  515. if (!error)
  516. acpi_target_sleep_state = ACPI_STATE_S4;
  517. }
  518. return error;
  519. }
  520. static int acpi_hibernation_pre_snapshot_old(void)
  521. {
  522. int error = acpi_pm_disable_gpes();
  523. if (!error)
  524. hibernate_nvs_save();
  525. return error;
  526. }
  527. /*
  528. * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
  529. * been requested.
  530. */
  531. static struct platform_hibernation_ops acpi_hibernation_ops_old = {
  532. .begin = acpi_hibernation_begin_old,
  533. .end = acpi_pm_end,
  534. .pre_snapshot = acpi_hibernation_pre_snapshot_old,
  535. .finish = acpi_hibernation_finish,
  536. .prepare = acpi_pm_disable_gpes,
  537. .enter = acpi_hibernation_enter,
  538. .leave = acpi_hibernation_leave,
  539. .pre_restore = acpi_pm_disable_gpes,
  540. .restore_cleanup = acpi_pm_enable_gpes,
  541. .recover = acpi_pm_finish,
  542. };
  543. #endif /* CONFIG_HIBERNATION */
  544. int acpi_suspend(u32 acpi_state)
  545. {
  546. suspend_state_t states[] = {
  547. [1] = PM_SUSPEND_STANDBY,
  548. [3] = PM_SUSPEND_MEM,
  549. [5] = PM_SUSPEND_MAX
  550. };
  551. if (acpi_state < 6 && states[acpi_state])
  552. return pm_suspend(states[acpi_state]);
  553. if (acpi_state == 4)
  554. return hibernate();
  555. return -EINVAL;
  556. }
  557. #ifdef CONFIG_PM_SLEEP
  558. /**
  559. * acpi_pm_device_sleep_state - return preferred power state of ACPI device
  560. * in the system sleep state given by %acpi_target_sleep_state
  561. * @dev: device to examine; its driver model wakeup flags control
  562. * whether it should be able to wake up the system
  563. * @d_min_p: used to store the upper limit of allowed states range
  564. * Return value: preferred power state of the device on success, -ENODEV on
  565. * failure (ie. if there's no 'struct acpi_device' for @dev)
  566. *
  567. * Find the lowest power (highest number) ACPI device power state that
  568. * device @dev can be in while the system is in the sleep state represented
  569. * by %acpi_target_sleep_state. If @wake is nonzero, the device should be
  570. * able to wake up the system from this sleep state. If @d_min_p is set,
  571. * the highest power (lowest number) device power state of @dev allowed
  572. * in this system sleep state is stored at the location pointed to by it.
  573. *
  574. * The caller must ensure that @dev is valid before using this function.
  575. * The caller is also responsible for figuring out if the device is
  576. * supposed to be able to wake up the system and passing this information
  577. * via @wake.
  578. */
  579. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
  580. {
  581. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  582. struct acpi_device *adev;
  583. char acpi_method[] = "_SxD";
  584. unsigned long long d_min, d_max;
  585. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  586. printk(KERN_DEBUG "ACPI handle has no context!\n");
  587. return -ENODEV;
  588. }
  589. acpi_method[2] = '0' + acpi_target_sleep_state;
  590. /*
  591. * If the sleep state is S0, we will return D3, but if the device has
  592. * _S0W, we will use the value from _S0W
  593. */
  594. d_min = ACPI_STATE_D0;
  595. d_max = ACPI_STATE_D3;
  596. /*
  597. * If present, _SxD methods return the minimum D-state (highest power
  598. * state) we can use for the corresponding S-states. Otherwise, the
  599. * minimum D-state is D0 (ACPI 3.x).
  600. *
  601. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  602. * provided -- that's our fault recovery, we ignore retval.
  603. */
  604. if (acpi_target_sleep_state > ACPI_STATE_S0)
  605. acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
  606. /*
  607. * If _PRW says we can wake up the system from the target sleep state,
  608. * the D-state returned by _SxD is sufficient for that (we assume a
  609. * wakeup-aware driver if wake is set). Still, if _SxW exists
  610. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  611. * can wake the system. _S0W may be valid, too.
  612. */
  613. if (acpi_target_sleep_state == ACPI_STATE_S0 ||
  614. (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
  615. adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
  616. acpi_status status;
  617. acpi_method[3] = 'W';
  618. status = acpi_evaluate_integer(handle, acpi_method, NULL,
  619. &d_max);
  620. if (ACPI_FAILURE(status)) {
  621. d_max = d_min;
  622. } else if (d_max < d_min) {
  623. /* Warn the user of the broken DSDT */
  624. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  625. acpi_method);
  626. /* Sanitize it */
  627. d_min = d_max;
  628. }
  629. }
  630. if (d_min_p)
  631. *d_min_p = d_min;
  632. return d_max;
  633. }
  634. /**
  635. * acpi_pm_device_sleep_wake - enable or disable the system wake-up
  636. * capability of given device
  637. * @dev: device to handle
  638. * @enable: 'true' - enable, 'false' - disable the wake-up capability
  639. */
  640. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  641. {
  642. acpi_handle handle;
  643. struct acpi_device *adev;
  644. int error;
  645. if (!device_can_wakeup(dev))
  646. return -EINVAL;
  647. handle = DEVICE_ACPI_HANDLE(dev);
  648. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  649. dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
  650. return -ENODEV;
  651. }
  652. error = enable ?
  653. acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
  654. acpi_disable_wakeup_device_power(adev);
  655. if (!error)
  656. dev_info(dev, "wake-up capability %s by ACPI\n",
  657. enable ? "enabled" : "disabled");
  658. return error;
  659. }
  660. #endif
  661. static void acpi_power_off_prepare(void)
  662. {
  663. /* Prepare to power off the system */
  664. acpi_sleep_prepare(ACPI_STATE_S5);
  665. acpi_disable_all_gpes();
  666. }
  667. static void acpi_power_off(void)
  668. {
  669. /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
  670. printk(KERN_DEBUG "%s called\n", __func__);
  671. local_irq_disable();
  672. acpi_enable_wakeup_device(ACPI_STATE_S5);
  673. acpi_enter_sleep_state(ACPI_STATE_S5);
  674. }
  675. /*
  676. * ACPI 2.0 created the optional _GTS and _BFS,
  677. * but industry adoption has been neither rapid nor broad.
  678. *
  679. * Linux gets into trouble when it executes poorly validated
  680. * paths through the BIOS, so disable _GTS and _BFS by default,
  681. * but do speak up and offer the option to enable them.
  682. */
  683. void __init acpi_gts_bfs_check(void)
  684. {
  685. acpi_handle dummy;
  686. if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_NAME__GTS, &dummy)))
  687. {
  688. printk(KERN_NOTICE PREFIX "BIOS offers _GTS\n");
  689. printk(KERN_NOTICE PREFIX "If \"acpi.gts=1\" improves suspend, "
  690. "please notify linux-acpi@vger.kernel.org\n");
  691. }
  692. if (ACPI_SUCCESS(acpi_get_handle(ACPI_ROOT_OBJECT, METHOD_NAME__BFS, &dummy)))
  693. {
  694. printk(KERN_NOTICE PREFIX "BIOS offers _BFS\n");
  695. printk(KERN_NOTICE PREFIX "If \"acpi.bfs=1\" improves resume, "
  696. "please notify linux-acpi@vger.kernel.org\n");
  697. }
  698. }
  699. int __init acpi_sleep_init(void)
  700. {
  701. acpi_status status;
  702. u8 type_a, type_b;
  703. #ifdef CONFIG_SUSPEND
  704. int i = 0;
  705. dmi_check_system(acpisleep_dmi_table);
  706. #endif
  707. if (acpi_disabled)
  708. return 0;
  709. sleep_states[ACPI_STATE_S0] = 1;
  710. printk(KERN_INFO PREFIX "(supports S0");
  711. #ifdef CONFIG_SUSPEND
  712. for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
  713. status = acpi_get_sleep_type_data(i, &type_a, &type_b);
  714. if (ACPI_SUCCESS(status)) {
  715. sleep_states[i] = 1;
  716. printk(" S%d", i);
  717. }
  718. }
  719. suspend_set_ops(old_suspend_ordering ?
  720. &acpi_suspend_ops_old : &acpi_suspend_ops);
  721. #endif
  722. #ifdef CONFIG_HIBERNATION
  723. status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
  724. if (ACPI_SUCCESS(status)) {
  725. hibernation_set_ops(old_suspend_ordering ?
  726. &acpi_hibernation_ops_old : &acpi_hibernation_ops);
  727. sleep_states[ACPI_STATE_S4] = 1;
  728. printk(" S4");
  729. if (!nosigcheck) {
  730. acpi_get_table(ACPI_SIG_FACS, 1,
  731. (struct acpi_table_header **)&facs);
  732. if (facs)
  733. s4_hardware_signature =
  734. facs->hardware_signature;
  735. }
  736. }
  737. #endif
  738. status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
  739. if (ACPI_SUCCESS(status)) {
  740. sleep_states[ACPI_STATE_S5] = 1;
  741. printk(" S5");
  742. pm_power_off_prepare = acpi_power_off_prepare;
  743. pm_power_off = acpi_power_off;
  744. }
  745. printk(")\n");
  746. /*
  747. * Register the tts_notifier to reboot notifier list so that the _TTS
  748. * object can also be evaluated when the system enters S5.
  749. */
  750. register_reboot_notifier(&tts_notifier);
  751. acpi_gts_bfs_check();
  752. return 0;
  753. }