suspend.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. * @suspend_again: Returns whether the system should suspend again (true) or
  89. * not (false). If the platform wants to poll sensors or execute some
  90. * code during suspended without invoking userspace and most of devices,
  91. * suspend_again callback is the place assuming that periodic-wakeup or
  92. * alarm-wakeup is already setup. This allows to execute some codes while
  93. * being kept suspended in the view of userland and devices.
  94. *
  95. * @end: Called by the PM core right after resuming devices, to indicate to
  96. * the platform that the system has returned to the working state or
  97. * the transition to the sleep state has been aborted.
  98. * This callback is optional, but should be implemented by the platforms
  99. * that implement @begin(). Accordingly, platforms implementing @begin()
  100. * should also provide a @end() which cleans up transitions aborted before
  101. * @enter().
  102. *
  103. * @recover: Recover the platform from a suspend failure.
  104. * Called by the PM core if the suspending of devices fails.
  105. * This callback is optional and should only be implemented by platforms
  106. * which require special recovery actions in that situation.
  107. */
  108. struct platform_suspend_ops {
  109. int (*valid)(suspend_state_t state);
  110. int (*begin)(suspend_state_t state);
  111. int (*prepare)(void);
  112. int (*prepare_late)(void);
  113. int (*enter)(suspend_state_t state);
  114. void (*wake)(void);
  115. void (*finish)(void);
  116. bool (*suspend_again)(void);
  117. void (*end)(void);
  118. void (*recover)(void);
  119. };
  120. #ifdef CONFIG_SUSPEND
  121. /**
  122. * suspend_set_ops - set platform dependent suspend operations
  123. * @ops: The new suspend operations to set.
  124. */
  125. extern void suspend_set_ops(const struct platform_suspend_ops *ops);
  126. extern int suspend_valid_only_mem(suspend_state_t state);
  127. /**
  128. * arch_suspend_disable_irqs - disable IRQs for suspend
  129. *
  130. * Disables 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_disable_irqs(void);
  135. /**
  136. * arch_suspend_enable_irqs - enable IRQs after suspend
  137. *
  138. * Enables IRQs (in the default case). This is a weak symbol in the common
  139. * code and thus allows architectures to override it if more needs to be
  140. * done. Not called for suspend to disk.
  141. */
  142. extern void arch_suspend_enable_irqs(void);
  143. extern int pm_suspend(suspend_state_t state);
  144. #else /* !CONFIG_SUSPEND */
  145. #define suspend_valid_only_mem NULL
  146. static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
  147. static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
  148. #endif /* !CONFIG_SUSPEND */
  149. /* struct pbe is used for creating lists of pages that should be restored
  150. * atomically during the resume from disk, because the page frames they have
  151. * occupied before the suspend are in use.
  152. */
  153. struct pbe {
  154. void *address; /* address of the copy */
  155. void *orig_address; /* original address of a page */
  156. struct pbe *next;
  157. };
  158. /* mm/page_alloc.c */
  159. extern void mark_free_pages(struct zone *zone);
  160. /**
  161. * struct platform_hibernation_ops - hibernation platform support
  162. *
  163. * The methods in this structure allow a platform to carry out special
  164. * operations required by it during a hibernation transition.
  165. *
  166. * All the methods below, except for @recover(), must be implemented.
  167. *
  168. * @begin: Tell the platform driver that we're starting hibernation.
  169. * Called right after shrinking memory and before freezing devices.
  170. *
  171. * @end: Called by the PM core right after resuming devices, to indicate to
  172. * the platform that the system has returned to the working state.
  173. *
  174. * @pre_snapshot: Prepare the platform for creating the hibernation image.
  175. * Called right after devices have been frozen and before the nonboot
  176. * CPUs are disabled (runs with IRQs on).
  177. *
  178. * @finish: Restore the previous state of the platform after the hibernation
  179. * image has been created *or* put the platform into the normal operation
  180. * mode after the hibernation (the same method is executed in both cases).
  181. * Called right after the nonboot CPUs have been enabled and before
  182. * thawing devices (runs with IRQs on).
  183. *
  184. * @prepare: Prepare the platform for entering the low power state.
  185. * Called right after the hibernation image has been saved and before
  186. * devices are prepared for entering the low power state.
  187. *
  188. * @enter: Put the system into the low power state after the hibernation image
  189. * has been saved to disk.
  190. * Called after the nonboot CPUs have been disabled and all of the low
  191. * level devices have been shut down (runs with IRQs off).
  192. *
  193. * @leave: Perform the first stage of the cleanup after the system sleep state
  194. * indicated by @set_target() has been left.
  195. * Called right after the control has been passed from the boot kernel to
  196. * the image kernel, before the nonboot CPUs are enabled and before devices
  197. * are resumed. Executed with interrupts disabled.
  198. *
  199. * @pre_restore: Prepare system for the restoration from a hibernation image.
  200. * Called right after devices have been frozen and before the nonboot
  201. * CPUs are disabled (runs with IRQs on).
  202. *
  203. * @restore_cleanup: Clean up after a failing image restoration.
  204. * Called right after the nonboot CPUs have been enabled and before
  205. * thawing devices (runs with IRQs on).
  206. *
  207. * @recover: Recover the platform from a failure to suspend devices.
  208. * Called by the PM core if the suspending of devices during hibernation
  209. * fails. This callback is optional and should only be implemented by
  210. * platforms which require special recovery actions in that situation.
  211. */
  212. struct platform_hibernation_ops {
  213. int (*begin)(void);
  214. void (*end)(void);
  215. int (*pre_snapshot)(void);
  216. void (*finish)(void);
  217. int (*prepare)(void);
  218. int (*enter)(void);
  219. void (*leave)(void);
  220. int (*pre_restore)(void);
  221. void (*restore_cleanup)(void);
  222. void (*recover)(void);
  223. };
  224. #ifdef CONFIG_HIBERNATION
  225. /* kernel/power/snapshot.c */
  226. extern void __register_nosave_region(unsigned long b, unsigned long e, int km);
  227. static inline void __init register_nosave_region(unsigned long b, unsigned long e)
  228. {
  229. __register_nosave_region(b, e, 0);
  230. }
  231. static inline void __init register_nosave_region_late(unsigned long b, unsigned long e)
  232. {
  233. __register_nosave_region(b, e, 1);
  234. }
  235. extern int swsusp_page_is_forbidden(struct page *);
  236. extern void swsusp_set_page_free(struct page *);
  237. extern void swsusp_unset_page_free(struct page *);
  238. extern unsigned long get_safe_page(gfp_t gfp_mask);
  239. extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
  240. extern int hibernate(void);
  241. extern bool system_entering_hibernation(void);
  242. #else /* CONFIG_HIBERNATION */
  243. static inline void register_nosave_region(unsigned long b, unsigned long e) {}
  244. static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
  245. static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
  246. static inline void swsusp_set_page_free(struct page *p) {}
  247. static inline void swsusp_unset_page_free(struct page *p) {}
  248. static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
  249. static inline int hibernate(void) { return -ENOSYS; }
  250. static inline bool system_entering_hibernation(void) { return false; }
  251. #endif /* CONFIG_HIBERNATION */
  252. /* Hibernation and suspend events */
  253. #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
  254. #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
  255. #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
  256. #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
  257. #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
  258. #define PM_POST_RESTORE 0x0006 /* Restore failed */
  259. #ifdef CONFIG_PM_SLEEP
  260. void save_processor_state(void);
  261. void restore_processor_state(void);
  262. /* kernel/power/main.c */
  263. extern int register_pm_notifier(struct notifier_block *nb);
  264. extern int unregister_pm_notifier(struct notifier_block *nb);
  265. #define pm_notifier(fn, pri) { \
  266. static struct notifier_block fn##_nb = \
  267. { .notifier_call = fn, .priority = pri }; \
  268. register_pm_notifier(&fn##_nb); \
  269. }
  270. /* drivers/base/power/wakeup.c */
  271. extern bool events_check_enabled;
  272. extern bool pm_wakeup_pending(void);
  273. extern bool pm_get_wakeup_count(unsigned int *count);
  274. extern bool pm_save_wakeup_count(unsigned int count);
  275. #else /* !CONFIG_PM_SLEEP */
  276. static inline int register_pm_notifier(struct notifier_block *nb)
  277. {
  278. return 0;
  279. }
  280. static inline int unregister_pm_notifier(struct notifier_block *nb)
  281. {
  282. return 0;
  283. }
  284. #define pm_notifier(fn, pri) do { (void)(fn); } while (0)
  285. static inline bool pm_wakeup_pending(void) { return false; }
  286. #endif /* !CONFIG_PM_SLEEP */
  287. extern struct mutex pm_mutex;
  288. #ifndef CONFIG_HIBERNATE_CALLBACKS
  289. static inline void lock_system_sleep(void) {}
  290. static inline void unlock_system_sleep(void) {}
  291. #else
  292. /* Let some subsystems like memory hotadd exclude hibernation */
  293. static inline void lock_system_sleep(void)
  294. {
  295. mutex_lock(&pm_mutex);
  296. }
  297. static inline void unlock_system_sleep(void)
  298. {
  299. mutex_unlock(&pm_mutex);
  300. }
  301. #endif
  302. #endif /* _LINUX_SUSPEND_H */