suspend.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <asm/errno.h>
  9. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_VT) && defined(CONFIG_VT_CONSOLE)
  10. extern void pm_set_vt_switch(int);
  11. extern int pm_prepare_console(void);
  12. extern void pm_restore_console(void);
  13. #else
  14. static inline void pm_set_vt_switch(int do_switch)
  15. {
  16. }
  17. static inline int pm_prepare_console(void)
  18. {
  19. return 0;
  20. }
  21. static inline void pm_restore_console(void)
  22. {
  23. }
  24. #endif
  25. typedef int __bitwise suspend_state_t;
  26. #define PM_SUSPEND_ON ((__force suspend_state_t) 0)
  27. #define PM_SUSPEND_STANDBY ((__force suspend_state_t) 1)
  28. #define PM_SUSPEND_MEM ((__force suspend_state_t) 3)
  29. #define PM_SUSPEND_MAX ((__force suspend_state_t) 4)
  30. enum suspend_stat_step {
  31. SUSPEND_FREEZE = 1,
  32. SUSPEND_PREPARE,
  33. SUSPEND_SUSPEND,
  34. SUSPEND_SUSPEND_NOIRQ,
  35. SUSPEND_RESUME_NOIRQ,
  36. SUSPEND_RESUME
  37. };
  38. struct suspend_stats {
  39. int success;
  40. int fail;
  41. int failed_freeze;
  42. int failed_prepare;
  43. int failed_suspend;
  44. int failed_suspend_noirq;
  45. int failed_resume;
  46. int failed_resume_noirq;
  47. #define REC_FAILED_NUM 2
  48. int last_failed_dev;
  49. char failed_devs[REC_FAILED_NUM][40];
  50. int last_failed_errno;
  51. int errno[REC_FAILED_NUM];
  52. int last_failed_step;
  53. enum suspend_stat_step failed_steps[REC_FAILED_NUM];
  54. };
  55. extern struct suspend_stats suspend_stats;
  56. static inline void dpm_save_failed_dev(const char *name)
  57. {
  58. strlcpy(suspend_stats.failed_devs[suspend_stats.last_failed_dev],
  59. name,
  60. sizeof(suspend_stats.failed_devs[0]));
  61. suspend_stats.last_failed_dev++;
  62. suspend_stats.last_failed_dev %= REC_FAILED_NUM;
  63. }
  64. static inline void dpm_save_failed_errno(int err)
  65. {
  66. suspend_stats.errno[suspend_stats.last_failed_errno] = err;
  67. suspend_stats.last_failed_errno++;
  68. suspend_stats.last_failed_errno %= REC_FAILED_NUM;
  69. }
  70. static inline void dpm_save_failed_step(enum suspend_stat_step step)
  71. {
  72. suspend_stats.failed_steps[suspend_stats.last_failed_step] = step;
  73. suspend_stats.last_failed_step++;
  74. suspend_stats.last_failed_step %= REC_FAILED_NUM;
  75. }
  76. /**
  77. * struct platform_suspend_ops - Callbacks for managing platform dependent
  78. * system sleep states.
  79. *
  80. * @valid: Callback to determine if given system sleep state is supported by
  81. * the platform.
  82. * Valid (ie. supported) states are advertised in /sys/power/state. Note
  83. * that it still may be impossible to enter given system sleep state if the
  84. * conditions aren't right.
  85. * There is the %suspend_valid_only_mem function available that can be
  86. * assigned to this if the platform only supports mem sleep.
  87. *
  88. * @begin: Initialise a transition to given system sleep state.
  89. * @begin() is executed right prior to suspending devices. The information
  90. * conveyed to the platform code by @begin() should be disregarded by it as
  91. * soon as @end() is executed. If @begin() fails (ie. returns nonzero),
  92. * @prepare(), @enter() and @finish() will not be called by the PM core.
  93. * This callback is optional. However, if it is implemented, the argument
  94. * passed to @enter() is redundant and should be ignored.
  95. *
  96. * @prepare: Prepare the platform for entering the system sleep state indicated
  97. * by @begin().
  98. * @prepare() is called right after devices have been suspended (ie. the
  99. * appropriate .suspend() method has been executed for each device) and
  100. * before device drivers' late suspend callbacks are executed. It returns
  101. * 0 on success or a negative error code otherwise, in which case the
  102. * system cannot enter the desired sleep state (@prepare_late(), @enter(),
  103. * and @wake() will not be called in that case).
  104. *
  105. * @prepare_late: Finish preparing the platform for entering the system sleep
  106. * state indicated by @begin().
  107. * @prepare_late is called before disabling nonboot CPUs and after
  108. * device drivers' late suspend callbacks have been executed. It returns
  109. * 0 on success or a negative error code otherwise, in which case the
  110. * system cannot enter the desired sleep state (@enter() will not be
  111. * executed).
  112. *
  113. * @enter: Enter the system sleep state indicated by @begin() or represented by
  114. * the argument if @begin() is not implemented.
  115. * This callback is mandatory. It returns 0 on success or a negative
  116. * error code otherwise, in which case the system cannot enter the desired
  117. * sleep state.
  118. *
  119. * @wake: Called when the system has just left a sleep state, right after
  120. * the nonboot CPUs have been enabled and before device drivers' early
  121. * resume callbacks are executed.
  122. * This callback is optional, but should be implemented by the platforms
  123. * that implement @prepare_late(). If implemented, it is always called
  124. * after @prepare_late and @enter(), even if one of them fails.
  125. *
  126. * @finish: Finish wake-up of the platform.
  127. * @finish is called right prior to calling device drivers' regular suspend
  128. * callbacks.
  129. * This callback is optional, but should be implemented by the platforms
  130. * that implement @prepare(). If implemented, it is always called after
  131. * @enter() and @wake(), even if any of them fails. It is executed after
  132. * a failing @prepare.
  133. *
  134. * @suspend_again: Returns whether the system should suspend again (true) or
  135. * not (false). If the platform wants to poll sensors or execute some
  136. * code during suspended without invoking userspace and most of devices,
  137. * suspend_again callback is the place assuming that periodic-wakeup or
  138. * alarm-wakeup is already setup. This allows to execute some codes while
  139. * being kept suspended in the view of userland and devices.
  140. *
  141. * @end: Called by the PM core right after resuming devices, to indicate to
  142. * the platform that the system has returned to the working state or
  143. * the transition to the sleep state has been aborted.
  144. * This callback is optional, but should be implemented by the platforms
  145. * that implement @begin(). Accordingly, platforms implementing @begin()
  146. * should also provide a @end() which cleans up transitions aborted before
  147. * @enter().
  148. *
  149. * @recover: Recover the platform from a suspend failure.
  150. * Called by the PM core if the suspending of devices fails.
  151. * This callback is optional and should only be implemented by platforms
  152. * which require special recovery actions in that situation.
  153. */
  154. struct platform_suspend_ops {
  155. int (*valid)(suspend_state_t state);
  156. int (*begin)(suspend_state_t state);
  157. int (*prepare)(void);
  158. int (*prepare_late)(void);
  159. int (*enter)(suspend_state_t state);
  160. void (*wake)(void);
  161. void (*finish)(void);
  162. bool (*suspend_again)(void);
  163. void (*end)(void);
  164. void (*recover)(void);
  165. };
  166. #ifdef CONFIG_SUSPEND
  167. /**
  168. * suspend_set_ops - set platform dependent suspend operations
  169. * @ops: The new suspend operations to set.
  170. */
  171. extern void suspend_set_ops(const struct platform_suspend_ops *ops);
  172. extern int suspend_valid_only_mem(suspend_state_t state);
  173. /**
  174. * arch_suspend_disable_irqs - disable IRQs for suspend
  175. *
  176. * Disables IRQs (in the default case). This is a weak symbol in the common
  177. * code and thus allows architectures to override it if more needs to be
  178. * done. Not called for suspend to disk.
  179. */
  180. extern void arch_suspend_disable_irqs(void);
  181. /**
  182. * arch_suspend_enable_irqs - enable IRQs after suspend
  183. *
  184. * Enables IRQs (in the default case). This is a weak symbol in the common
  185. * code and thus allows architectures to override it if more needs to be
  186. * done. Not called for suspend to disk.
  187. */
  188. extern void arch_suspend_enable_irqs(void);
  189. extern int pm_suspend(suspend_state_t state);
  190. #else /* !CONFIG_SUSPEND */
  191. #define suspend_valid_only_mem NULL
  192. static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
  193. static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
  194. #endif /* !CONFIG_SUSPEND */
  195. /* struct pbe is used for creating lists of pages that should be restored
  196. * atomically during the resume from disk, because the page frames they have
  197. * occupied before the suspend are in use.
  198. */
  199. struct pbe {
  200. void *address; /* address of the copy */
  201. void *orig_address; /* original address of a page */
  202. struct pbe *next;
  203. };
  204. /* mm/page_alloc.c */
  205. extern void mark_free_pages(struct zone *zone);
  206. /**
  207. * struct platform_hibernation_ops - hibernation platform support
  208. *
  209. * The methods in this structure allow a platform to carry out special
  210. * operations required by it during a hibernation transition.
  211. *
  212. * All the methods below, except for @recover(), must be implemented.
  213. *
  214. * @begin: Tell the platform driver that we're starting hibernation.
  215. * Called right after shrinking memory and before freezing devices.
  216. *
  217. * @end: Called by the PM core right after resuming devices, to indicate to
  218. * the platform that the system has returned to the working state.
  219. *
  220. * @pre_snapshot: Prepare the platform for creating the hibernation image.
  221. * Called right after devices have been frozen and before the nonboot
  222. * CPUs are disabled (runs with IRQs on).
  223. *
  224. * @finish: Restore the previous state of the platform after the hibernation
  225. * image has been created *or* put the platform into the normal operation
  226. * mode after the hibernation (the same method is executed in both cases).
  227. * Called right after the nonboot CPUs have been enabled and before
  228. * thawing devices (runs with IRQs on).
  229. *
  230. * @prepare: Prepare the platform for entering the low power state.
  231. * Called right after the hibernation image has been saved and before
  232. * devices are prepared for entering the low power state.
  233. *
  234. * @enter: Put the system into the low power state after the hibernation image
  235. * has been saved to disk.
  236. * Called after the nonboot CPUs have been disabled and all of the low
  237. * level devices have been shut down (runs with IRQs off).
  238. *
  239. * @leave: Perform the first stage of the cleanup after the system sleep state
  240. * indicated by @set_target() has been left.
  241. * Called right after the control has been passed from the boot kernel to
  242. * the image kernel, before the nonboot CPUs are enabled and before devices
  243. * are resumed. Executed with interrupts disabled.
  244. *
  245. * @pre_restore: Prepare system for the restoration from a hibernation image.
  246. * Called right after devices have been frozen and before the nonboot
  247. * CPUs are disabled (runs with IRQs on).
  248. *
  249. * @restore_cleanup: Clean up after a failing image restoration.
  250. * Called right after the nonboot CPUs have been enabled and before
  251. * thawing devices (runs with IRQs on).
  252. *
  253. * @recover: Recover the platform from a failure to suspend devices.
  254. * Called by the PM core if the suspending of devices during hibernation
  255. * fails. This callback is optional and should only be implemented by
  256. * platforms which require special recovery actions in that situation.
  257. */
  258. struct platform_hibernation_ops {
  259. int (*begin)(void);
  260. void (*end)(void);
  261. int (*pre_snapshot)(void);
  262. void (*finish)(void);
  263. int (*prepare)(void);
  264. int (*enter)(void);
  265. void (*leave)(void);
  266. int (*pre_restore)(void);
  267. void (*restore_cleanup)(void);
  268. void (*recover)(void);
  269. };
  270. #ifdef CONFIG_HIBERNATION
  271. /* kernel/power/snapshot.c */
  272. extern void __register_nosave_region(unsigned long b, unsigned long e, int km);
  273. static inline void __init register_nosave_region(unsigned long b, unsigned long e)
  274. {
  275. __register_nosave_region(b, e, 0);
  276. }
  277. static inline void __init register_nosave_region_late(unsigned long b, unsigned long e)
  278. {
  279. __register_nosave_region(b, e, 1);
  280. }
  281. extern int swsusp_page_is_forbidden(struct page *);
  282. extern void swsusp_set_page_free(struct page *);
  283. extern void swsusp_unset_page_free(struct page *);
  284. extern unsigned long get_safe_page(gfp_t gfp_mask);
  285. extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
  286. extern int hibernate(void);
  287. extern bool system_entering_hibernation(void);
  288. #else /* CONFIG_HIBERNATION */
  289. static inline void register_nosave_region(unsigned long b, unsigned long e) {}
  290. static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
  291. static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
  292. static inline void swsusp_set_page_free(struct page *p) {}
  293. static inline void swsusp_unset_page_free(struct page *p) {}
  294. static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
  295. static inline int hibernate(void) { return -ENOSYS; }
  296. static inline bool system_entering_hibernation(void) { return false; }
  297. #endif /* CONFIG_HIBERNATION */
  298. /* Hibernation and suspend events */
  299. #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
  300. #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
  301. #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
  302. #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
  303. #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
  304. #define PM_POST_RESTORE 0x0006 /* Restore failed */
  305. #ifdef CONFIG_PM_SLEEP
  306. void save_processor_state(void);
  307. void restore_processor_state(void);
  308. /* kernel/power/main.c */
  309. extern int register_pm_notifier(struct notifier_block *nb);
  310. extern int unregister_pm_notifier(struct notifier_block *nb);
  311. #define pm_notifier(fn, pri) { \
  312. static struct notifier_block fn##_nb = \
  313. { .notifier_call = fn, .priority = pri }; \
  314. register_pm_notifier(&fn##_nb); \
  315. }
  316. /* drivers/base/power/wakeup.c */
  317. extern bool events_check_enabled;
  318. extern bool pm_wakeup_pending(void);
  319. extern bool pm_get_wakeup_count(unsigned int *count);
  320. extern bool pm_save_wakeup_count(unsigned int count);
  321. #else /* !CONFIG_PM_SLEEP */
  322. static inline int register_pm_notifier(struct notifier_block *nb)
  323. {
  324. return 0;
  325. }
  326. static inline int unregister_pm_notifier(struct notifier_block *nb)
  327. {
  328. return 0;
  329. }
  330. #define pm_notifier(fn, pri) do { (void)(fn); } while (0)
  331. static inline bool pm_wakeup_pending(void) { return false; }
  332. #endif /* !CONFIG_PM_SLEEP */
  333. extern struct mutex pm_mutex;
  334. #ifndef CONFIG_HIBERNATE_CALLBACKS
  335. static inline void lock_system_sleep(void) {}
  336. static inline void unlock_system_sleep(void) {}
  337. #else
  338. /* Let some subsystems like memory hotadd exclude hibernation */
  339. static inline void lock_system_sleep(void)
  340. {
  341. mutex_lock(&pm_mutex);
  342. }
  343. static inline void unlock_system_sleep(void)
  344. {
  345. mutex_unlock(&pm_mutex);
  346. }
  347. #endif
  348. #endif /* _LINUX_SUSPEND_H */