suspend.h 12 KB

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