suspend.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. #ifndef _LINUX_SUSPEND_H
  2. #define _LINUX_SUSPEND_H
  3. #include <linux/swap.h>
  4. #include <linux/notifier.h>
  5. #include <linux/init.h>
  6. #include <linux/pm.h>
  7. #include <linux/mm.h>
  8. #include <linux/freezer.h>
  9. #include <asm/errno.h>
  10. #ifdef CONFIG_VT
  11. extern void pm_set_vt_switch(int);
  12. #else
  13. static inline void pm_set_vt_switch(int do_switch)
  14. {
  15. }
  16. #endif
  17. #ifdef CONFIG_VT_CONSOLE_SLEEP
  18. extern void pm_prepare_console(void);
  19. extern void pm_restore_console(void);
  20. #else
  21. static inline void pm_prepare_console(void)
  22. {
  23. }
  24. static inline void pm_restore_console(void)
  25. {
  26. }
  27. #endif
  28. typedef int __bitwise suspend_state_t;
  29. #define PM_SUSPEND_ON ((__force suspend_state_t) 0)
  30. #define PM_SUSPEND_TO_IDLE ((__force suspend_state_t) 1)
  31. #define PM_SUSPEND_STANDBY ((__force suspend_state_t) 2)
  32. #define PM_SUSPEND_MEM ((__force suspend_state_t) 3)
  33. #define PM_SUSPEND_MIN PM_SUSPEND_TO_IDLE
  34. #define PM_SUSPEND_MAX ((__force suspend_state_t) 4)
  35. enum suspend_stat_step {
  36. SUSPEND_FREEZE = 1,
  37. SUSPEND_PREPARE,
  38. SUSPEND_SUSPEND,
  39. SUSPEND_SUSPEND_LATE,
  40. SUSPEND_SUSPEND_NOIRQ,
  41. SUSPEND_RESUME_NOIRQ,
  42. SUSPEND_RESUME_EARLY,
  43. SUSPEND_RESUME
  44. };
  45. struct suspend_stats {
  46. int success;
  47. int fail;
  48. int failed_freeze;
  49. int failed_prepare;
  50. int failed_suspend;
  51. int failed_suspend_late;
  52. int failed_suspend_noirq;
  53. int failed_resume;
  54. int failed_resume_early;
  55. int failed_resume_noirq;
  56. #define REC_FAILED_NUM 2
  57. int last_failed_dev;
  58. char failed_devs[REC_FAILED_NUM][40];
  59. int last_failed_errno;
  60. int errno[REC_FAILED_NUM];
  61. int last_failed_step;
  62. enum suspend_stat_step failed_steps[REC_FAILED_NUM];
  63. };
  64. extern struct suspend_stats suspend_stats;
  65. static inline void dpm_save_failed_dev(const char *name)
  66. {
  67. strlcpy(suspend_stats.failed_devs[suspend_stats.last_failed_dev],
  68. name,
  69. sizeof(suspend_stats.failed_devs[0]));
  70. suspend_stats.last_failed_dev++;
  71. suspend_stats.last_failed_dev %= REC_FAILED_NUM;
  72. }
  73. static inline void dpm_save_failed_errno(int err)
  74. {
  75. suspend_stats.errno[suspend_stats.last_failed_errno] = err;
  76. suspend_stats.last_failed_errno++;
  77. suspend_stats.last_failed_errno %= REC_FAILED_NUM;
  78. }
  79. static inline void dpm_save_failed_step(enum suspend_stat_step step)
  80. {
  81. suspend_stats.failed_steps[suspend_stats.last_failed_step] = step;
  82. suspend_stats.last_failed_step++;
  83. suspend_stats.last_failed_step %= REC_FAILED_NUM;
  84. }
  85. /**
  86. * struct platform_suspend_ops - Callbacks for managing platform dependent
  87. * system sleep states.
  88. *
  89. * @valid: Callback to determine if given system sleep state is supported by
  90. * the platform.
  91. * Valid (ie. supported) states are advertised in /sys/power/state. Note
  92. * that it still may be impossible to enter given system sleep state if the
  93. * conditions aren't right.
  94. * There is the %suspend_valid_only_mem function available that can be
  95. * assigned to this if the platform only supports mem sleep.
  96. *
  97. * @begin: Initialise a transition to given system sleep state.
  98. * @begin() is executed right prior to suspending devices. The information
  99. * conveyed to the platform code by @begin() should be disregarded by it as
  100. * soon as @end() is executed. If @begin() fails (ie. returns nonzero),
  101. * @prepare(), @enter() and @finish() will not be called by the PM core.
  102. * This callback is optional. However, if it is implemented, the argument
  103. * passed to @enter() is redundant and should be ignored.
  104. *
  105. * @prepare: Prepare the platform for entering the system sleep state indicated
  106. * by @begin().
  107. * @prepare() is called right after devices have been suspended (ie. the
  108. * appropriate .suspend() method has been executed for each device) and
  109. * before device drivers' late suspend callbacks are executed. It returns
  110. * 0 on success or a negative error code otherwise, in which case the
  111. * system cannot enter the desired sleep state (@prepare_late(), @enter(),
  112. * and @wake() will not be called in that case).
  113. *
  114. * @prepare_late: Finish preparing the platform for entering the system sleep
  115. * state indicated by @begin().
  116. * @prepare_late is called before disabling nonboot CPUs and after
  117. * device drivers' late suspend callbacks have been executed. It returns
  118. * 0 on success or a negative error code otherwise, in which case the
  119. * system cannot enter the desired sleep state (@enter() will not be
  120. * executed).
  121. *
  122. * @enter: Enter the system sleep state indicated by @begin() or represented by
  123. * the argument if @begin() is not implemented.
  124. * This callback is mandatory. It returns 0 on success or a negative
  125. * error code otherwise, in which case the system cannot enter the desired
  126. * sleep state.
  127. *
  128. * @wake: Called when the system has just left a sleep state, right after
  129. * the nonboot CPUs have been enabled and before device drivers' early
  130. * resume callbacks are executed.
  131. * This callback is optional, but should be implemented by the platforms
  132. * that implement @prepare_late(). If implemented, it is always called
  133. * after @prepare_late and @enter(), even if one of them fails.
  134. *
  135. * @finish: Finish wake-up of the platform.
  136. * @finish is called right prior to calling device drivers' regular suspend
  137. * callbacks.
  138. * This callback is optional, but should be implemented by the platforms
  139. * that implement @prepare(). If implemented, it is always called after
  140. * @enter() and @wake(), even if any of them fails. It is executed after
  141. * a failing @prepare.
  142. *
  143. * @suspend_again: Returns whether the system should suspend again (true) or
  144. * not (false). If the platform wants to poll sensors or execute some
  145. * code during suspended without invoking userspace and most of devices,
  146. * suspend_again callback is the place assuming that periodic-wakeup or
  147. * alarm-wakeup is already setup. This allows to execute some codes while
  148. * being kept suspended in the view of userland and devices.
  149. *
  150. * @end: Called by the PM core right after resuming devices, to indicate to
  151. * the platform that the system has returned to the working state or
  152. * the transition to the sleep state has been aborted.
  153. * This callback is optional, but should be implemented by the platforms
  154. * that implement @begin(). Accordingly, platforms implementing @begin()
  155. * should also provide a @end() which cleans up transitions aborted before
  156. * @enter().
  157. *
  158. * @recover: Recover the platform from a suspend failure.
  159. * Called by the PM core if the suspending of devices fails.
  160. * This callback is optional and should only be implemented by platforms
  161. * which require special recovery actions in that situation.
  162. */
  163. struct platform_suspend_ops {
  164. int (*valid)(suspend_state_t state);
  165. int (*begin)(suspend_state_t state);
  166. int (*prepare)(void);
  167. int (*prepare_late)(void);
  168. int (*enter)(suspend_state_t state);
  169. void (*wake)(void);
  170. void (*finish)(void);
  171. bool (*suspend_again)(void);
  172. void (*end)(void);
  173. void (*recover)(void);
  174. };
  175. struct platform_s2idle_ops {
  176. int (*begin)(void);
  177. int (*prepare)(void);
  178. void (*wake)(void);
  179. void (*sync)(void);
  180. void (*restore)(void);
  181. void (*end)(void);
  182. };
  183. #ifdef CONFIG_SUSPEND
  184. extern suspend_state_t mem_sleep_current;
  185. extern suspend_state_t mem_sleep_default;
  186. /**
  187. * suspend_set_ops - set platform dependent suspend operations
  188. * @ops: The new suspend operations to set.
  189. */
  190. extern void suspend_set_ops(const struct platform_suspend_ops *ops);
  191. extern int suspend_valid_only_mem(suspend_state_t state);
  192. extern unsigned int pm_suspend_global_flags;
  193. #define PM_SUSPEND_FLAG_FW_SUSPEND (1 << 0)
  194. #define PM_SUSPEND_FLAG_FW_RESUME (1 << 1)
  195. static inline void pm_suspend_clear_flags(void)
  196. {
  197. pm_suspend_global_flags = 0;
  198. }
  199. static inline void pm_set_suspend_via_firmware(void)
  200. {
  201. pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_SUSPEND;
  202. }
  203. static inline void pm_set_resume_via_firmware(void)
  204. {
  205. pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_RESUME;
  206. }
  207. static inline bool pm_suspend_via_firmware(void)
  208. {
  209. return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_SUSPEND);
  210. }
  211. static inline bool pm_resume_via_firmware(void)
  212. {
  213. return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_RESUME);
  214. }
  215. /* Suspend-to-idle state machnine. */
  216. enum s2idle_states {
  217. S2IDLE_STATE_NONE, /* Not suspended/suspending. */
  218. S2IDLE_STATE_ENTER, /* Enter suspend-to-idle. */
  219. S2IDLE_STATE_WAKE, /* Wake up from suspend-to-idle. */
  220. };
  221. extern enum s2idle_states __read_mostly s2idle_state;
  222. static inline bool idle_should_enter_s2idle(void)
  223. {
  224. return unlikely(s2idle_state == S2IDLE_STATE_ENTER);
  225. }
  226. extern void __init pm_states_init(void);
  227. extern void s2idle_set_ops(const struct platform_s2idle_ops *ops);
  228. extern void s2idle_wake(void);
  229. /**
  230. * arch_suspend_disable_irqs - disable IRQs for suspend
  231. *
  232. * Disables IRQs (in the default case). This is a weak symbol in the common
  233. * code and thus allows architectures to override it if more needs to be
  234. * done. Not called for suspend to disk.
  235. */
  236. extern void arch_suspend_disable_irqs(void);
  237. /**
  238. * arch_suspend_enable_irqs - enable IRQs after suspend
  239. *
  240. * Enables IRQs (in the default case). This is a weak symbol in the common
  241. * code and thus allows architectures to override it if more needs to be
  242. * done. Not called for suspend to disk.
  243. */
  244. extern void arch_suspend_enable_irqs(void);
  245. extern int pm_suspend(suspend_state_t state);
  246. #else /* !CONFIG_SUSPEND */
  247. #define suspend_valid_only_mem NULL
  248. static inline void pm_suspend_clear_flags(void) {}
  249. static inline void pm_set_suspend_via_firmware(void) {}
  250. static inline void pm_set_resume_via_firmware(void) {}
  251. static inline bool pm_suspend_via_firmware(void) { return false; }
  252. static inline bool pm_resume_via_firmware(void) { return false; }
  253. static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
  254. static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
  255. static inline bool idle_should_enter_s2idle(void) { return false; }
  256. static inline void __init pm_states_init(void) {}
  257. static inline void s2idle_set_ops(const struct platform_s2idle_ops *ops) {}
  258. static inline void s2idle_wake(void) {}
  259. #endif /* !CONFIG_SUSPEND */
  260. /* struct pbe is used for creating lists of pages that should be restored
  261. * atomically during the resume from disk, because the page frames they have
  262. * occupied before the suspend are in use.
  263. */
  264. struct pbe {
  265. void *address; /* address of the copy */
  266. void *orig_address; /* original address of a page */
  267. struct pbe *next;
  268. };
  269. /* mm/page_alloc.c */
  270. extern void mark_free_pages(struct zone *zone);
  271. /**
  272. * struct platform_hibernation_ops - hibernation platform support
  273. *
  274. * The methods in this structure allow a platform to carry out special
  275. * operations required by it during a hibernation transition.
  276. *
  277. * All the methods below, except for @recover(), must be implemented.
  278. *
  279. * @begin: Tell the platform driver that we're starting hibernation.
  280. * Called right after shrinking memory and before freezing devices.
  281. *
  282. * @end: Called by the PM core right after resuming devices, to indicate to
  283. * the platform that the system has returned to the working state.
  284. *
  285. * @pre_snapshot: Prepare the platform for creating the hibernation image.
  286. * Called right after devices have been frozen and before the nonboot
  287. * CPUs are disabled (runs with IRQs on).
  288. *
  289. * @finish: Restore the previous state of the platform after the hibernation
  290. * image has been created *or* put the platform into the normal operation
  291. * mode after the hibernation (the same method is executed in both cases).
  292. * Called right after the nonboot CPUs have been enabled and before
  293. * thawing devices (runs with IRQs on).
  294. *
  295. * @prepare: Prepare the platform for entering the low power state.
  296. * Called right after the hibernation image has been saved and before
  297. * devices are prepared for entering the low power state.
  298. *
  299. * @enter: Put the system into the low power state after the hibernation image
  300. * has been saved to disk.
  301. * Called after the nonboot CPUs have been disabled and all of the low
  302. * level devices have been shut down (runs with IRQs off).
  303. *
  304. * @leave: Perform the first stage of the cleanup after the system sleep state
  305. * indicated by @set_target() has been left.
  306. * Called right after the control has been passed from the boot kernel to
  307. * the image kernel, before the nonboot CPUs are enabled and before devices
  308. * are resumed. Executed with interrupts disabled.
  309. *
  310. * @pre_restore: Prepare system for the restoration from a hibernation image.
  311. * Called right after devices have been frozen and before the nonboot
  312. * CPUs are disabled (runs with IRQs on).
  313. *
  314. * @restore_cleanup: Clean up after a failing image restoration.
  315. * Called right after the nonboot CPUs have been enabled and before
  316. * thawing devices (runs with IRQs on).
  317. *
  318. * @recover: Recover the platform from a failure to suspend devices.
  319. * Called by the PM core if the suspending of devices during hibernation
  320. * fails. This callback is optional and should only be implemented by
  321. * platforms which require special recovery actions in that situation.
  322. */
  323. struct platform_hibernation_ops {
  324. int (*begin)(void);
  325. void (*end)(void);
  326. int (*pre_snapshot)(void);
  327. void (*finish)(void);
  328. int (*prepare)(void);
  329. int (*enter)(void);
  330. void (*leave)(void);
  331. int (*pre_restore)(void);
  332. void (*restore_cleanup)(void);
  333. void (*recover)(void);
  334. };
  335. #ifdef CONFIG_HIBERNATION
  336. /* kernel/power/snapshot.c */
  337. extern void __register_nosave_region(unsigned long b, unsigned long e, int km);
  338. static inline void __init register_nosave_region(unsigned long b, unsigned long e)
  339. {
  340. __register_nosave_region(b, e, 0);
  341. }
  342. static inline void __init register_nosave_region_late(unsigned long b, unsigned long e)
  343. {
  344. __register_nosave_region(b, e, 1);
  345. }
  346. extern int swsusp_page_is_forbidden(struct page *);
  347. extern void swsusp_set_page_free(struct page *);
  348. extern void swsusp_unset_page_free(struct page *);
  349. extern unsigned long get_safe_page(gfp_t gfp_mask);
  350. extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
  351. extern int hibernate(void);
  352. extern bool system_entering_hibernation(void);
  353. extern bool hibernation_available(void);
  354. asmlinkage int swsusp_save(void);
  355. extern struct pbe *restore_pblist;
  356. #else /* CONFIG_HIBERNATION */
  357. static inline void register_nosave_region(unsigned long b, unsigned long e) {}
  358. static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
  359. static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
  360. static inline void swsusp_set_page_free(struct page *p) {}
  361. static inline void swsusp_unset_page_free(struct page *p) {}
  362. static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
  363. static inline int hibernate(void) { return -ENOSYS; }
  364. static inline bool system_entering_hibernation(void) { return false; }
  365. static inline bool hibernation_available(void) { return false; }
  366. #endif /* CONFIG_HIBERNATION */
  367. /* Hibernation and suspend events */
  368. #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
  369. #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
  370. #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
  371. #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
  372. #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
  373. #define PM_POST_RESTORE 0x0006 /* Restore failed */
  374. extern struct mutex pm_mutex;
  375. #ifdef CONFIG_PM_SLEEP
  376. void save_processor_state(void);
  377. void restore_processor_state(void);
  378. /* kernel/power/main.c */
  379. extern int register_pm_notifier(struct notifier_block *nb);
  380. extern int unregister_pm_notifier(struct notifier_block *nb);
  381. #define pm_notifier(fn, pri) { \
  382. static struct notifier_block fn##_nb = \
  383. { .notifier_call = fn, .priority = pri }; \
  384. register_pm_notifier(&fn##_nb); \
  385. }
  386. /* drivers/base/power/wakeup.c */
  387. extern bool events_check_enabled;
  388. extern unsigned int pm_wakeup_irq;
  389. extern suspend_state_t pm_suspend_target_state;
  390. extern bool pm_wakeup_pending(void);
  391. extern void pm_system_wakeup(void);
  392. extern void pm_system_cancel_wakeup(void);
  393. extern void pm_wakeup_clear(bool reset);
  394. extern void pm_system_irq_wakeup(unsigned int irq_number);
  395. extern bool pm_get_wakeup_count(unsigned int *count, bool block);
  396. extern bool pm_save_wakeup_count(unsigned int count);
  397. extern void pm_wakep_autosleep_enabled(bool set);
  398. extern void pm_print_active_wakeup_sources(void);
  399. static inline void lock_system_sleep(void)
  400. {
  401. current->flags |= PF_FREEZER_SKIP;
  402. mutex_lock(&pm_mutex);
  403. }
  404. static inline void unlock_system_sleep(void)
  405. {
  406. /*
  407. * Don't use freezer_count() because we don't want the call to
  408. * try_to_freeze() here.
  409. *
  410. * Reason:
  411. * Fundamentally, we just don't need it, because freezing condition
  412. * doesn't come into effect until we release the pm_mutex lock,
  413. * since the freezer always works with pm_mutex held.
  414. *
  415. * More importantly, in the case of hibernation,
  416. * unlock_system_sleep() gets called in snapshot_read() and
  417. * snapshot_write() when the freezing condition is still in effect.
  418. * Which means, if we use try_to_freeze() here, it would make them
  419. * enter the refrigerator, thus causing hibernation to lockup.
  420. */
  421. current->flags &= ~PF_FREEZER_SKIP;
  422. mutex_unlock(&pm_mutex);
  423. }
  424. #else /* !CONFIG_PM_SLEEP */
  425. static inline int register_pm_notifier(struct notifier_block *nb)
  426. {
  427. return 0;
  428. }
  429. static inline int unregister_pm_notifier(struct notifier_block *nb)
  430. {
  431. return 0;
  432. }
  433. #define pm_notifier(fn, pri) do { (void)(fn); } while (0)
  434. static inline bool pm_wakeup_pending(void) { return false; }
  435. static inline void pm_system_wakeup(void) {}
  436. static inline void pm_wakeup_clear(bool reset) {}
  437. static inline void pm_system_irq_wakeup(unsigned int irq_number) {}
  438. static inline void lock_system_sleep(void) {}
  439. static inline void unlock_system_sleep(void) {}
  440. #endif /* !CONFIG_PM_SLEEP */
  441. #ifdef CONFIG_PM_SLEEP_DEBUG
  442. extern bool pm_print_times_enabled;
  443. extern bool pm_debug_messages_on;
  444. extern __printf(2, 3) void __pm_pr_dbg(bool defer, const char *fmt, ...);
  445. #else
  446. #define pm_print_times_enabled (false)
  447. #define pm_debug_messages_on (false)
  448. #include <linux/printk.h>
  449. #define __pm_pr_dbg(defer, fmt, ...) \
  450. no_printk(KERN_DEBUG fmt, ##__VA_ARGS__)
  451. #endif
  452. #define pm_pr_dbg(fmt, ...) \
  453. __pm_pr_dbg(false, fmt, ##__VA_ARGS__)
  454. #define pm_deferred_pr_dbg(fmt, ...) \
  455. __pm_pr_dbg(true, fmt, ##__VA_ARGS__)
  456. #ifdef CONFIG_PM_AUTOSLEEP
  457. /* kernel/power/autosleep.c */
  458. void queue_up_suspend_work(void);
  459. #else /* !CONFIG_PM_AUTOSLEEP */
  460. static inline void queue_up_suspend_work(void) {}
  461. #endif /* !CONFIG_PM_AUTOSLEEP */
  462. #ifdef CONFIG_ARCH_SAVE_PAGE_KEYS
  463. /*
  464. * The ARCH_SAVE_PAGE_KEYS functions can be used by an architecture
  465. * to save/restore additional information to/from the array of page
  466. * frame numbers in the hibernation image. For s390 this is used to
  467. * save and restore the storage key for each page that is included
  468. * in the hibernation image.
  469. */
  470. unsigned long page_key_additional_pages(unsigned long pages);
  471. int page_key_alloc(unsigned long pages);
  472. void page_key_free(void);
  473. void page_key_read(unsigned long *pfn);
  474. void page_key_memorize(unsigned long *pfn);
  475. void page_key_write(void *address);
  476. #else /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
  477. static inline unsigned long page_key_additional_pages(unsigned long pages)
  478. {
  479. return 0;
  480. }
  481. static inline int page_key_alloc(unsigned long pages)
  482. {
  483. return 0;
  484. }
  485. static inline void page_key_free(void) {}
  486. static inline void page_key_read(unsigned long *pfn) {}
  487. static inline void page_key_memorize(unsigned long *pfn) {}
  488. static inline void page_key_write(void *address) {}
  489. #endif /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
  490. #endif /* _LINUX_SUSPEND_H */