suspend.h 11 KB

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