suspend.h 10 KB

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