suspend.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /**
  31. * struct platform_suspend_ops - Callbacks for managing platform dependent
  32. * system sleep states.
  33. *
  34. * @valid: Callback to determine if given system sleep state is supported by
  35. * the platform.
  36. * Valid (ie. supported) states are advertised in /sys/power/state. Note
  37. * that it still may be impossible to enter given system sleep state if the
  38. * conditions aren't right.
  39. * There is the %suspend_valid_only_mem function available that can be
  40. * assigned to this if the platform only supports mem sleep.
  41. *
  42. * @begin: Initialise a transition to given system sleep state.
  43. * @begin() is executed right prior to suspending devices. The information
  44. * conveyed to the platform code by @begin() should be disregarded by it as
  45. * soon as @end() is executed. If @begin() fails (ie. returns nonzero),
  46. * @prepare(), @enter() and @finish() will not be called by the PM core.
  47. * This callback is optional. However, if it is implemented, the argument
  48. * passed to @enter() is redundant and should be ignored.
  49. *
  50. * @prepare: Prepare the platform for entering the system sleep state indicated
  51. * by @begin().
  52. * @prepare() is called right after devices have been suspended (ie. the
  53. * appropriate .suspend() method has been executed for each device) and
  54. * before device drivers' late suspend callbacks are executed. It returns
  55. * 0 on success or a negative error code otherwise, in which case the
  56. * system cannot enter the desired sleep state (@prepare_late(), @enter(),
  57. * and @wake() will not be called in that case).
  58. *
  59. * @prepare_late: Finish preparing the platform for entering the system sleep
  60. * state indicated by @begin().
  61. * @prepare_late is called before disabling nonboot CPUs and after
  62. * device drivers' late suspend callbacks have been executed. It returns
  63. * 0 on success or a negative error code otherwise, in which case the
  64. * system cannot enter the desired sleep state (@enter() will not be
  65. * executed).
  66. *
  67. * @enter: Enter the system sleep state indicated by @begin() or represented by
  68. * the argument if @begin() is not implemented.
  69. * This callback is mandatory. It returns 0 on success or a negative
  70. * error code otherwise, in which case the system cannot enter the desired
  71. * sleep state.
  72. *
  73. * @wake: Called when the system has just left a sleep state, right after
  74. * the nonboot CPUs have been enabled and before device drivers' early
  75. * resume callbacks are executed.
  76. * This callback is optional, but should be implemented by the platforms
  77. * that implement @prepare_late(). If implemented, it is always called
  78. * after @prepare_late and @enter(), even if one of them fails.
  79. *
  80. * @finish: Finish wake-up of the platform.
  81. * @finish is called right prior to calling device drivers' regular suspend
  82. * callbacks.
  83. * This callback is optional, but should be implemented by the platforms
  84. * that implement @prepare(). If implemented, it is always called after
  85. * @enter() and @wake(), even if any of them fails. It is executed after
  86. * a failing @prepare.
  87. *
  88. * @end: Called by the PM core right after resuming devices, to indicate to
  89. * the platform that the system has returned to the working state or
  90. * the transition to the sleep state has been aborted.
  91. * This callback is optional, but should be implemented by the platforms
  92. * that implement @begin(). Accordingly, platforms implementing @begin()
  93. * should also provide a @end() which cleans up transitions aborted before
  94. * @enter().
  95. *
  96. * @recover: Recover the platform from a suspend failure.
  97. * Called by the PM core if the suspending of devices fails.
  98. * This callback is optional and should only be implemented by platforms
  99. * which require special recovery actions in that situation.
  100. */
  101. struct platform_suspend_ops {
  102. int (*valid)(suspend_state_t state);
  103. int (*begin)(suspend_state_t state);
  104. int (*prepare)(void);
  105. int (*prepare_late)(void);
  106. int (*enter)(suspend_state_t state);
  107. void (*wake)(void);
  108. void (*finish)(void);
  109. void (*end)(void);
  110. void (*recover)(void);
  111. };
  112. #ifdef CONFIG_SUSPEND
  113. /**
  114. * suspend_set_ops - set platform dependent suspend operations
  115. * @ops: The new suspend operations to set.
  116. */
  117. extern void suspend_set_ops(const struct platform_suspend_ops *ops);
  118. extern int suspend_valid_only_mem(suspend_state_t state);
  119. /**
  120. * arch_suspend_disable_irqs - disable IRQs for suspend
  121. *
  122. * Disables IRQs (in the default case). This is a weak symbol in the common
  123. * code and thus allows architectures to override it if more needs to be
  124. * done. Not called for suspend to disk.
  125. */
  126. extern void arch_suspend_disable_irqs(void);
  127. /**
  128. * arch_suspend_enable_irqs - enable IRQs after suspend
  129. *
  130. * Enables IRQs (in the default case). This is a weak symbol in the common
  131. * code and thus allows architectures to override it if more needs to be
  132. * done. Not called for suspend to disk.
  133. */
  134. extern void arch_suspend_enable_irqs(void);
  135. extern int pm_suspend(suspend_state_t state);
  136. #else /* !CONFIG_SUSPEND */
  137. #define suspend_valid_only_mem NULL
  138. static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
  139. static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
  140. #endif /* !CONFIG_SUSPEND */
  141. /* struct pbe is used for creating lists of pages that should be restored
  142. * atomically during the resume from disk, because the page frames they have
  143. * occupied before the suspend are in use.
  144. */
  145. struct pbe {
  146. void *address; /* address of the copy */
  147. void *orig_address; /* original address of a page */
  148. struct pbe *next;
  149. };
  150. /* mm/page_alloc.c */
  151. extern void mark_free_pages(struct zone *zone);
  152. /**
  153. * struct platform_hibernation_ops - hibernation platform support
  154. *
  155. * The methods in this structure allow a platform to carry out special
  156. * operations required by it during a hibernation transition.
  157. *
  158. * All the methods below, except for @recover(), must be implemented.
  159. *
  160. * @begin: Tell the platform driver that we're starting hibernation.
  161. * Called right after shrinking memory and before freezing devices.
  162. *
  163. * @end: Called by the PM core right after resuming devices, to indicate to
  164. * the platform that the system has returned to the working state.
  165. *
  166. * @pre_snapshot: Prepare the platform for creating the hibernation image.
  167. * Called right after devices have been frozen and before the nonboot
  168. * CPUs are disabled (runs with IRQs on).
  169. *
  170. * @finish: Restore the previous state of the platform after the hibernation
  171. * image has been created *or* put the platform into the normal operation
  172. * mode after the hibernation (the same method is executed in both cases).
  173. * Called right after the nonboot CPUs have been enabled and before
  174. * thawing devices (runs with IRQs on).
  175. *
  176. * @prepare: Prepare the platform for entering the low power state.
  177. * Called right after the hibernation image has been saved and before
  178. * devices are prepared for entering the low power state.
  179. *
  180. * @enter: Put the system into the low power state after the hibernation image
  181. * has been saved to disk.
  182. * Called after the nonboot CPUs have been disabled and all of the low
  183. * level devices have been shut down (runs with IRQs off).
  184. *
  185. * @leave: Perform the first stage of the cleanup after the system sleep state
  186. * indicated by @set_target() has been left.
  187. * Called right after the control has been passed from the boot kernel to
  188. * the image kernel, before the nonboot CPUs are enabled and before devices
  189. * are resumed. Executed with interrupts disabled.
  190. *
  191. * @pre_restore: Prepare system for the restoration from a hibernation image.
  192. * Called right after devices have been frozen and before the nonboot
  193. * CPUs are disabled (runs with IRQs on).
  194. *
  195. * @restore_cleanup: Clean up after a failing image restoration.
  196. * Called right after the nonboot CPUs have been enabled and before
  197. * thawing devices (runs with IRQs on).
  198. *
  199. * @recover: Recover the platform from a failure to suspend devices.
  200. * Called by the PM core if the suspending of devices during hibernation
  201. * fails. This callback is optional and should only be implemented by
  202. * platforms which require special recovery actions in that situation.
  203. */
  204. struct platform_hibernation_ops {
  205. int (*begin)(void);
  206. void (*end)(void);
  207. int (*pre_snapshot)(void);
  208. void (*finish)(void);
  209. int (*prepare)(void);
  210. int (*enter)(void);
  211. void (*leave)(void);
  212. int (*pre_restore)(void);
  213. void (*restore_cleanup)(void);
  214. void (*recover)(void);
  215. };
  216. #ifdef CONFIG_HIBERNATION
  217. /* kernel/power/snapshot.c */
  218. extern void __register_nosave_region(unsigned long b, unsigned long e, int km);
  219. static inline void __init register_nosave_region(unsigned long b, unsigned long e)
  220. {
  221. __register_nosave_region(b, e, 0);
  222. }
  223. static inline void __init register_nosave_region_late(unsigned long b, unsigned long e)
  224. {
  225. __register_nosave_region(b, e, 1);
  226. }
  227. extern int swsusp_page_is_forbidden(struct page *);
  228. extern void swsusp_set_page_free(struct page *);
  229. extern void swsusp_unset_page_free(struct page *);
  230. extern unsigned long get_safe_page(gfp_t gfp_mask);
  231. extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
  232. extern int hibernate(void);
  233. extern bool system_entering_hibernation(void);
  234. #else /* CONFIG_HIBERNATION */
  235. static inline void register_nosave_region(unsigned long b, unsigned long e) {}
  236. static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
  237. static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
  238. static inline void swsusp_set_page_free(struct page *p) {}
  239. static inline void swsusp_unset_page_free(struct page *p) {}
  240. static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
  241. static inline int hibernate(void) { return -ENOSYS; }
  242. static inline bool system_entering_hibernation(void) { return false; }
  243. #endif /* CONFIG_HIBERNATION */
  244. #ifdef CONFIG_PM_SLEEP
  245. void save_processor_state(void);
  246. void restore_processor_state(void);
  247. /* kernel/power/main.c */
  248. extern int register_pm_notifier(struct notifier_block *nb);
  249. extern int unregister_pm_notifier(struct notifier_block *nb);
  250. #define pm_notifier(fn, pri) { \
  251. static struct notifier_block fn##_nb = \
  252. { .notifier_call = fn, .priority = pri }; \
  253. register_pm_notifier(&fn##_nb); \
  254. }
  255. /* drivers/base/power/wakeup.c */
  256. extern bool events_check_enabled;
  257. extern bool pm_wakeup_pending(void);
  258. extern bool pm_get_wakeup_count(unsigned int *count);
  259. extern bool pm_save_wakeup_count(unsigned int count);
  260. #else /* !CONFIG_PM_SLEEP */
  261. static inline int register_pm_notifier(struct notifier_block *nb)
  262. {
  263. return 0;
  264. }
  265. static inline int unregister_pm_notifier(struct notifier_block *nb)
  266. {
  267. return 0;
  268. }
  269. #define pm_notifier(fn, pri) do { (void)(fn); } while (0)
  270. static inline bool pm_wakeup_pending(void) { return false; }
  271. #endif /* !CONFIG_PM_SLEEP */
  272. extern struct mutex pm_mutex;
  273. #ifndef CONFIG_HIBERNATE_CALLBACKS
  274. static inline void lock_system_sleep(void) {}
  275. static inline void unlock_system_sleep(void) {}
  276. #else
  277. /* Let some subsystems like memory hotadd exclude hibernation */
  278. static inline void lock_system_sleep(void)
  279. {
  280. mutex_lock(&pm_mutex);
  281. }
  282. static inline void unlock_system_sleep(void)
  283. {
  284. mutex_unlock(&pm_mutex);
  285. }
  286. #endif
  287. #endif /* _LINUX_SUSPEND_H */