suspend.h 15 KB

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