suspend.h 19 KB

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