suspend.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #ifdef CONFIG_VT
  10. extern void pm_set_vt_switch(int);
  11. #else
  12. static inline void pm_set_vt_switch(int do_switch)
  13. {
  14. }
  15. #endif
  16. #ifdef CONFIG_VT_CONSOLE_SLEEP
  17. extern int pm_prepare_console(void);
  18. extern void pm_restore_console(void);
  19. #else
  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. enum suspend_stat_step {
  34. SUSPEND_FREEZE = 1,
  35. SUSPEND_PREPARE,
  36. SUSPEND_SUSPEND,
  37. SUSPEND_SUSPEND_NOIRQ,
  38. SUSPEND_RESUME_NOIRQ,
  39. SUSPEND_RESUME
  40. };
  41. struct suspend_stats {
  42. int success;
  43. int fail;
  44. int failed_freeze;
  45. int failed_prepare;
  46. int failed_suspend;
  47. int failed_suspend_noirq;
  48. int failed_resume;
  49. int failed_resume_noirq;
  50. #define REC_FAILED_NUM 2
  51. int last_failed_dev;
  52. char failed_devs[REC_FAILED_NUM][40];
  53. int last_failed_errno;
  54. int errno[REC_FAILED_NUM];
  55. int last_failed_step;
  56. enum suspend_stat_step failed_steps[REC_FAILED_NUM];
  57. };
  58. extern struct suspend_stats suspend_stats;
  59. static inline void dpm_save_failed_dev(const char *name)
  60. {
  61. strlcpy(suspend_stats.failed_devs[suspend_stats.last_failed_dev],
  62. name,
  63. sizeof(suspend_stats.failed_devs[0]));
  64. suspend_stats.last_failed_dev++;
  65. suspend_stats.last_failed_dev %= REC_FAILED_NUM;
  66. }
  67. static inline void dpm_save_failed_errno(int err)
  68. {
  69. suspend_stats.errno[suspend_stats.last_failed_errno] = err;
  70. suspend_stats.last_failed_errno++;
  71. suspend_stats.last_failed_errno %= REC_FAILED_NUM;
  72. }
  73. static inline void dpm_save_failed_step(enum suspend_stat_step step)
  74. {
  75. suspend_stats.failed_steps[suspend_stats.last_failed_step] = step;
  76. suspend_stats.last_failed_step++;
  77. suspend_stats.last_failed_step %= REC_FAILED_NUM;
  78. }
  79. /**
  80. * struct platform_suspend_ops - Callbacks for managing platform dependent
  81. * system sleep states.
  82. *
  83. * @valid: Callback to determine if given system sleep state is supported by
  84. * the platform.
  85. * Valid (ie. supported) states are advertised in /sys/power/state. Note
  86. * that it still may be impossible to enter given system sleep state if the
  87. * conditions aren't right.
  88. * There is the %suspend_valid_only_mem function available that can be
  89. * assigned to this if the platform only supports mem sleep.
  90. *
  91. * @begin: Initialise a transition to given system sleep state.
  92. * @begin() is executed right prior to suspending devices. The information
  93. * conveyed to the platform code by @begin() should be disregarded by it as
  94. * soon as @end() is executed. If @begin() fails (ie. returns nonzero),
  95. * @prepare(), @enter() and @finish() will not be called by the PM core.
  96. * This callback is optional. However, if it is implemented, the argument
  97. * passed to @enter() is redundant and should be ignored.
  98. *
  99. * @prepare: Prepare the platform for entering the system sleep state indicated
  100. * by @begin().
  101. * @prepare() is called right after devices have been suspended (ie. the
  102. * appropriate .suspend() method has been executed for each device) and
  103. * before device drivers' late suspend callbacks are executed. It returns
  104. * 0 on success or a negative error code otherwise, in which case the
  105. * system cannot enter the desired sleep state (@prepare_late(), @enter(),
  106. * and @wake() will not be called in that case).
  107. *
  108. * @prepare_late: Finish preparing the platform for entering the system sleep
  109. * state indicated by @begin().
  110. * @prepare_late is called before disabling nonboot CPUs and after
  111. * device drivers' late suspend callbacks have been executed. It returns
  112. * 0 on success or a negative error code otherwise, in which case the
  113. * system cannot enter the desired sleep state (@enter() will not be
  114. * executed).
  115. *
  116. * @enter: Enter the system sleep state indicated by @begin() or represented by
  117. * the argument if @begin() is not implemented.
  118. * This callback is mandatory. It returns 0 on success or a negative
  119. * error code otherwise, in which case the system cannot enter the desired
  120. * sleep state.
  121. *
  122. * @wake: Called when the system has just left a sleep state, right after
  123. * the nonboot CPUs have been enabled and before device drivers' early
  124. * resume callbacks are executed.
  125. * This callback is optional, but should be implemented by the platforms
  126. * that implement @prepare_late(). If implemented, it is always called
  127. * after @prepare_late and @enter(), even if one of them fails.
  128. *
  129. * @finish: Finish wake-up of the platform.
  130. * @finish is called right prior to calling device drivers' regular suspend
  131. * callbacks.
  132. * This callback is optional, but should be implemented by the platforms
  133. * that implement @prepare(). If implemented, it is always called after
  134. * @enter() and @wake(), even if any of them fails. It is executed after
  135. * a failing @prepare.
  136. *
  137. * @suspend_again: Returns whether the system should suspend again (true) or
  138. * not (false). If the platform wants to poll sensors or execute some
  139. * code during suspended without invoking userspace and most of devices,
  140. * suspend_again callback is the place assuming that periodic-wakeup or
  141. * alarm-wakeup is already setup. This allows to execute some codes while
  142. * being kept suspended in the view of userland and devices.
  143. *
  144. * @end: Called by the PM core right after resuming devices, to indicate to
  145. * the platform that the system has returned to the working state or
  146. * the transition to the sleep state has been aborted.
  147. * This callback is optional, but should be implemented by the platforms
  148. * that implement @begin(). Accordingly, platforms implementing @begin()
  149. * should also provide a @end() which cleans up transitions aborted before
  150. * @enter().
  151. *
  152. * @recover: Recover the platform from a suspend failure.
  153. * Called by the PM core if the suspending of devices fails.
  154. * This callback is optional and should only be implemented by platforms
  155. * which require special recovery actions in that situation.
  156. */
  157. struct platform_suspend_ops {
  158. int (*valid)(suspend_state_t state);
  159. int (*begin)(suspend_state_t state);
  160. int (*prepare)(void);
  161. int (*prepare_late)(void);
  162. int (*enter)(suspend_state_t state);
  163. void (*wake)(void);
  164. void (*finish)(void);
  165. bool (*suspend_again)(void);
  166. void (*end)(void);
  167. void (*recover)(void);
  168. };
  169. #ifdef CONFIG_SUSPEND
  170. /**
  171. * suspend_set_ops - set platform dependent suspend operations
  172. * @ops: The new suspend operations to set.
  173. */
  174. extern void suspend_set_ops(const struct platform_suspend_ops *ops);
  175. extern int suspend_valid_only_mem(suspend_state_t state);
  176. /**
  177. * arch_suspend_disable_irqs - disable IRQs for suspend
  178. *
  179. * Disables IRQs (in the default case). This is a weak symbol in the common
  180. * code and thus allows architectures to override it if more needs to be
  181. * done. Not called for suspend to disk.
  182. */
  183. extern void arch_suspend_disable_irqs(void);
  184. /**
  185. * arch_suspend_enable_irqs - enable IRQs after suspend
  186. *
  187. * Enables IRQs (in the default case). This is a weak symbol in the common
  188. * code and thus allows architectures to override it if more needs to be
  189. * done. Not called for suspend to disk.
  190. */
  191. extern void arch_suspend_enable_irqs(void);
  192. extern int pm_suspend(suspend_state_t state);
  193. #else /* !CONFIG_SUSPEND */
  194. #define suspend_valid_only_mem NULL
  195. static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
  196. static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
  197. #endif /* !CONFIG_SUSPEND */
  198. /* struct pbe is used for creating lists of pages that should be restored
  199. * atomically during the resume from disk, because the page frames they have
  200. * occupied before the suspend are in use.
  201. */
  202. struct pbe {
  203. void *address; /* address of the copy */
  204. void *orig_address; /* original address of a page */
  205. struct pbe *next;
  206. };
  207. /* mm/page_alloc.c */
  208. extern void mark_free_pages(struct zone *zone);
  209. /**
  210. * struct platform_hibernation_ops - hibernation platform support
  211. *
  212. * The methods in this structure allow a platform to carry out special
  213. * operations required by it during a hibernation transition.
  214. *
  215. * All the methods below, except for @recover(), must be implemented.
  216. *
  217. * @begin: Tell the platform driver that we're starting hibernation.
  218. * Called right after shrinking memory and before freezing devices.
  219. *
  220. * @end: Called by the PM core right after resuming devices, to indicate to
  221. * the platform that the system has returned to the working state.
  222. *
  223. * @pre_snapshot: Prepare the platform for creating the hibernation image.
  224. * Called right after devices have been frozen and before the nonboot
  225. * CPUs are disabled (runs with IRQs on).
  226. *
  227. * @finish: Restore the previous state of the platform after the hibernation
  228. * image has been created *or* put the platform into the normal operation
  229. * mode after the hibernation (the same method is executed in both cases).
  230. * Called right after the nonboot CPUs have been enabled and before
  231. * thawing devices (runs with IRQs on).
  232. *
  233. * @prepare: Prepare the platform for entering the low power state.
  234. * Called right after the hibernation image has been saved and before
  235. * devices are prepared for entering the low power state.
  236. *
  237. * @enter: Put the system into the low power state after the hibernation image
  238. * has been saved to disk.
  239. * Called after the nonboot CPUs have been disabled and all of the low
  240. * level devices have been shut down (runs with IRQs off).
  241. *
  242. * @leave: Perform the first stage of the cleanup after the system sleep state
  243. * indicated by @set_target() has been left.
  244. * Called right after the control has been passed from the boot kernel to
  245. * the image kernel, before the nonboot CPUs are enabled and before devices
  246. * are resumed. Executed with interrupts disabled.
  247. *
  248. * @pre_restore: Prepare system for the restoration from a hibernation image.
  249. * Called right after devices have been frozen and before the nonboot
  250. * CPUs are disabled (runs with IRQs on).
  251. *
  252. * @restore_cleanup: Clean up after a failing image restoration.
  253. * Called right after the nonboot CPUs have been enabled and before
  254. * thawing devices (runs with IRQs on).
  255. *
  256. * @recover: Recover the platform from a failure to suspend devices.
  257. * Called by the PM core if the suspending of devices during hibernation
  258. * fails. This callback is optional and should only be implemented by
  259. * platforms which require special recovery actions in that situation.
  260. */
  261. struct platform_hibernation_ops {
  262. int (*begin)(void);
  263. void (*end)(void);
  264. int (*pre_snapshot)(void);
  265. void (*finish)(void);
  266. int (*prepare)(void);
  267. int (*enter)(void);
  268. void (*leave)(void);
  269. int (*pre_restore)(void);
  270. void (*restore_cleanup)(void);
  271. void (*recover)(void);
  272. };
  273. #ifdef CONFIG_HIBERNATION
  274. /* kernel/power/snapshot.c */
  275. extern void __register_nosave_region(unsigned long b, unsigned long e, int km);
  276. static inline void __init register_nosave_region(unsigned long b, unsigned long e)
  277. {
  278. __register_nosave_region(b, e, 0);
  279. }
  280. static inline void __init register_nosave_region_late(unsigned long b, unsigned long e)
  281. {
  282. __register_nosave_region(b, e, 1);
  283. }
  284. extern int swsusp_page_is_forbidden(struct page *);
  285. extern void swsusp_set_page_free(struct page *);
  286. extern void swsusp_unset_page_free(struct page *);
  287. extern unsigned long get_safe_page(gfp_t gfp_mask);
  288. extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
  289. extern int hibernate(void);
  290. extern bool system_entering_hibernation(void);
  291. #else /* CONFIG_HIBERNATION */
  292. static inline void register_nosave_region(unsigned long b, unsigned long e) {}
  293. static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
  294. static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
  295. static inline void swsusp_set_page_free(struct page *p) {}
  296. static inline void swsusp_unset_page_free(struct page *p) {}
  297. static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
  298. static inline int hibernate(void) { return -ENOSYS; }
  299. static inline bool system_entering_hibernation(void) { return false; }
  300. #endif /* CONFIG_HIBERNATION */
  301. /* Hibernation and suspend events */
  302. #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
  303. #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
  304. #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
  305. #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
  306. #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
  307. #define PM_POST_RESTORE 0x0006 /* Restore failed */
  308. #ifdef CONFIG_PM_SLEEP
  309. void save_processor_state(void);
  310. void restore_processor_state(void);
  311. /* kernel/power/main.c */
  312. extern int register_pm_notifier(struct notifier_block *nb);
  313. extern int unregister_pm_notifier(struct notifier_block *nb);
  314. #define pm_notifier(fn, pri) { \
  315. static struct notifier_block fn##_nb = \
  316. { .notifier_call = fn, .priority = pri }; \
  317. register_pm_notifier(&fn##_nb); \
  318. }
  319. /* drivers/base/power/wakeup.c */
  320. extern bool events_check_enabled;
  321. extern bool pm_wakeup_pending(void);
  322. extern bool pm_get_wakeup_count(unsigned int *count);
  323. extern bool pm_save_wakeup_count(unsigned int count);
  324. #else /* !CONFIG_PM_SLEEP */
  325. static inline int register_pm_notifier(struct notifier_block *nb)
  326. {
  327. return 0;
  328. }
  329. static inline int unregister_pm_notifier(struct notifier_block *nb)
  330. {
  331. return 0;
  332. }
  333. #define pm_notifier(fn, pri) do { (void)(fn); } while (0)
  334. static inline bool pm_wakeup_pending(void) { return false; }
  335. #endif /* !CONFIG_PM_SLEEP */
  336. extern struct mutex pm_mutex;
  337. #ifndef CONFIG_HIBERNATE_CALLBACKS
  338. static inline void lock_system_sleep(void) {}
  339. static inline void unlock_system_sleep(void) {}
  340. #else
  341. /* Let some subsystems like memory hotadd exclude hibernation */
  342. static inline void lock_system_sleep(void)
  343. {
  344. /* simplified freezer_do_not_count() */
  345. current->flags |= PF_FREEZER_SKIP;
  346. mutex_lock(&pm_mutex);
  347. }
  348. static inline void unlock_system_sleep(void)
  349. {
  350. mutex_unlock(&pm_mutex);
  351. /* simplified freezer_count() */
  352. current->flags &= ~PF_FREEZER_SKIP;
  353. }
  354. #endif
  355. #ifdef CONFIG_ARCH_SAVE_PAGE_KEYS
  356. /*
  357. * The ARCH_SAVE_PAGE_KEYS functions can be used by an architecture
  358. * to save/restore additional information to/from the array of page
  359. * frame numbers in the hibernation image. For s390 this is used to
  360. * save and restore the storage key for each page that is included
  361. * in the hibernation image.
  362. */
  363. unsigned long page_key_additional_pages(unsigned long pages);
  364. int page_key_alloc(unsigned long pages);
  365. void page_key_free(void);
  366. void page_key_read(unsigned long *pfn);
  367. void page_key_memorize(unsigned long *pfn);
  368. void page_key_write(void *address);
  369. #else /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
  370. static inline unsigned long page_key_additional_pages(unsigned long pages)
  371. {
  372. return 0;
  373. }
  374. static inline int page_key_alloc(unsigned long pages)
  375. {
  376. return 0;
  377. }
  378. static inline void page_key_free(void) {}
  379. static inline void page_key_read(unsigned long *pfn) {}
  380. static inline void page_key_memorize(unsigned long *pfn) {}
  381. static inline void page_key_write(void *address) {}
  382. #endif /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
  383. #endif /* _LINUX_SUSPEND_H */