suspend.h 9.5 KB

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