power.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/suspend.h>
  3. #include <linux/suspend_ioctls.h>
  4. #include <linux/utsname.h>
  5. #include <linux/freezer.h>
  6. #include <linux/compiler.h>
  7. #include <linux/cpu.h>
  8. #include <linux/cpuidle.h>
  9. #include <linux/crypto.h>
  10. struct swsusp_info {
  11. struct new_utsname uts;
  12. u32 version_code;
  13. unsigned long num_physpages;
  14. int cpus;
  15. unsigned long image_pages;
  16. unsigned long pages;
  17. unsigned long size;
  18. } __aligned(PAGE_SIZE);
  19. #ifdef CONFIG_HIBERNATION
  20. /* kernel/power/snapshot.c */
  21. extern void __init hibernate_reserved_size_init(void);
  22. extern void __init hibernate_image_size_init(void);
  23. #ifdef CONFIG_ARCH_HIBERNATION_HEADER
  24. /* Maximum size of architecture specific data in a hibernation header */
  25. #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4)
  26. static inline int init_header_complete(struct swsusp_info *info)
  27. {
  28. return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
  29. }
  30. static inline const char *check_image_kernel(struct swsusp_info *info)
  31. {
  32. return arch_hibernation_header_restore(info) ?
  33. "architecture specific data" : NULL;
  34. }
  35. #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
  36. /*
  37. * Keep some memory free so that I/O operations can succeed without paging
  38. * [Might this be more than 4 MB?]
  39. */
  40. #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT)
  41. /*
  42. * Keep 1 MB of memory free so that device drivers can allocate some pages in
  43. * their .suspend() routines without breaking the suspend to disk.
  44. */
  45. #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT)
  46. asmlinkage int swsusp_save(void);
  47. /* kernel/power/hibernate.c */
  48. extern bool freezer_test_done;
  49. extern char hib_comp_algo[CRYPTO_MAX_ALG_NAME];
  50. /* kernel/power/swap.c */
  51. extern unsigned int swsusp_header_flags;
  52. extern int hibernation_snapshot(int platform_mode);
  53. extern int hibernation_restore(int platform_mode);
  54. extern int hibernation_platform_enter(void);
  55. #ifdef CONFIG_STRICT_KERNEL_RWX
  56. /* kernel/power/snapshot.c */
  57. extern void enable_restore_image_protection(void);
  58. #else
  59. static inline void enable_restore_image_protection(void) {}
  60. #endif /* CONFIG_STRICT_KERNEL_RWX */
  61. extern bool hibernation_in_progress(void);
  62. #else /* !CONFIG_HIBERNATION */
  63. static inline void hibernate_reserved_size_init(void) {}
  64. static inline void hibernate_image_size_init(void) {}
  65. static inline bool hibernation_in_progress(void) { return false; }
  66. #endif /* !CONFIG_HIBERNATION */
  67. #define power_attr(_name) \
  68. static struct kobj_attribute _name##_attr = { \
  69. .attr = { \
  70. .name = __stringify(_name), \
  71. .mode = 0644, \
  72. }, \
  73. .show = _name##_show, \
  74. .store = _name##_store, \
  75. }
  76. #define power_attr_ro(_name) \
  77. static struct kobj_attribute _name##_attr = { \
  78. .attr = { \
  79. .name = __stringify(_name), \
  80. .mode = S_IRUGO, \
  81. }, \
  82. .show = _name##_show, \
  83. }
  84. /* Preferred image size in bytes (default 500 MB) */
  85. extern unsigned long image_size;
  86. /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
  87. extern unsigned long reserved_size;
  88. extern int in_suspend;
  89. extern dev_t swsusp_resume_device;
  90. extern sector_t swsusp_resume_block;
  91. extern int create_basic_memory_bitmaps(void);
  92. extern void free_basic_memory_bitmaps(void);
  93. extern int hibernate_preallocate_memory(void);
  94. extern void clear_or_poison_free_pages(void);
  95. /**
  96. * Auxiliary structure used for reading the snapshot image data and
  97. * metadata from and writing them to the list of page backup entries
  98. * (PBEs) which is the main data structure of swsusp.
  99. *
  100. * Using struct snapshot_handle we can transfer the image, including its
  101. * metadata, as a continuous sequence of bytes with the help of
  102. * snapshot_read_next() and snapshot_write_next().
  103. *
  104. * The code that writes the image to a storage or transfers it to
  105. * the user land is required to use snapshot_read_next() for this
  106. * purpose and it should not make any assumptions regarding the internal
  107. * structure of the image. Similarly, the code that reads the image from
  108. * a storage or transfers it from the user land is required to use
  109. * snapshot_write_next().
  110. *
  111. * This may allow us to change the internal structure of the image
  112. * in the future with considerably less effort.
  113. */
  114. struct snapshot_handle {
  115. unsigned int cur; /* number of the block of PAGE_SIZE bytes the
  116. * next operation will refer to (ie. current)
  117. */
  118. void *buffer; /* address of the block to read from
  119. * or write to
  120. */
  121. int sync_read; /* Set to one to notify the caller of
  122. * snapshot_write_next() that it may
  123. * need to call wait_on_bio_chain()
  124. */
  125. };
  126. /* This macro returns the address from/to which the caller of
  127. * snapshot_read_next()/snapshot_write_next() is allowed to
  128. * read/write data after the function returns
  129. */
  130. #define data_of(handle) ((handle).buffer)
  131. extern unsigned int snapshot_additional_pages(struct zone *zone);
  132. extern unsigned long snapshot_get_image_size(void);
  133. extern int snapshot_read_next(struct snapshot_handle *handle);
  134. extern int snapshot_write_next(struct snapshot_handle *handle);
  135. int snapshot_write_finalize(struct snapshot_handle *handle);
  136. extern int snapshot_image_loaded(struct snapshot_handle *handle);
  137. extern bool hibernate_acquire(void);
  138. extern void hibernate_release(void);
  139. extern sector_t alloc_swapdev_block(int swap);
  140. extern void free_all_swap_pages(int swap);
  141. extern int swsusp_swap_in_use(void);
  142. /*
  143. * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
  144. * the image header.
  145. */
  146. #define SF_COMPRESSION_ALG_LZO 0 /* dummy, details given below */
  147. #define SF_PLATFORM_MODE 1
  148. #define SF_NOCOMPRESS_MODE 2
  149. #define SF_CRC32_MODE 4
  150. #define SF_HW_SIG 8
  151. /*
  152. * Bit to indicate the compression algorithm to be used(for LZ4). The same
  153. * could be checked while saving/loading image to/from disk to use the
  154. * corresponding algorithms.
  155. *
  156. * By default, LZO compression is enabled if SF_CRC32_MODE is set. Use
  157. * SF_COMPRESSION_ALG_LZ4 to override this behaviour and use LZ4.
  158. *
  159. * SF_CRC32_MODE, SF_COMPRESSION_ALG_LZO(dummy) -> Compression, LZO
  160. * SF_CRC32_MODE, SF_COMPRESSION_ALG_LZ4 -> Compression, LZ4
  161. */
  162. #define SF_COMPRESSION_ALG_LZ4 16
  163. /* kernel/power/hibernate.c */
  164. int swsusp_check(bool exclusive);
  165. extern void swsusp_free(void);
  166. extern int swsusp_read(unsigned int *flags_p);
  167. extern int swsusp_write(unsigned int flags);
  168. void swsusp_close(void);
  169. #ifdef CONFIG_SUSPEND
  170. extern int swsusp_unmark(void);
  171. #else
  172. static inline int swsusp_unmark(void) { return 0; }
  173. #endif
  174. struct __kernel_old_timeval;
  175. /* kernel/power/swsusp.c */
  176. extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
  177. #ifdef CONFIG_SUSPEND
  178. /* kernel/power/suspend.c */
  179. extern const char * const pm_labels[];
  180. extern const char *pm_states[];
  181. extern const char *mem_sleep_states[];
  182. extern int suspend_devices_and_enter(suspend_state_t state);
  183. #else /* !CONFIG_SUSPEND */
  184. #define mem_sleep_current PM_SUSPEND_ON
  185. static inline int suspend_devices_and_enter(suspend_state_t state)
  186. {
  187. return -ENOSYS;
  188. }
  189. #endif /* !CONFIG_SUSPEND */
  190. #ifdef CONFIG_PM_TEST_SUSPEND
  191. /* kernel/power/suspend_test.c */
  192. extern void suspend_test_start(void);
  193. extern void suspend_test_finish(const char *label);
  194. #else /* !CONFIG_PM_TEST_SUSPEND */
  195. static inline void suspend_test_start(void) {}
  196. static inline void suspend_test_finish(const char *label) {}
  197. #endif /* !CONFIG_PM_TEST_SUSPEND */
  198. #ifdef CONFIG_PM_SLEEP
  199. /* kernel/power/main.c */
  200. extern int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down);
  201. extern int pm_notifier_call_chain(unsigned long val);
  202. void pm_restrict_gfp_mask(void);
  203. void pm_restore_gfp_mask(void);
  204. #else
  205. static inline void pm_restrict_gfp_mask(void) {}
  206. static inline void pm_restore_gfp_mask(void) {}
  207. #endif
  208. #ifdef CONFIG_HIGHMEM
  209. int restore_highmem(void);
  210. #else
  211. static inline unsigned int count_highmem_pages(void) { return 0; }
  212. static inline int restore_highmem(void) { return 0; }
  213. #endif
  214. /*
  215. * Suspend test levels
  216. */
  217. enum {
  218. /* keep first */
  219. TEST_NONE,
  220. TEST_CORE,
  221. TEST_CPUS,
  222. TEST_PLATFORM,
  223. TEST_DEVICES,
  224. TEST_FREEZER,
  225. /* keep last */
  226. __TEST_AFTER_LAST
  227. };
  228. #define TEST_FIRST TEST_NONE
  229. #define TEST_MAX (__TEST_AFTER_LAST - 1)
  230. #ifdef CONFIG_PM_SLEEP_DEBUG
  231. extern int pm_test_level;
  232. #else
  233. #define pm_test_level (TEST_NONE)
  234. #endif
  235. #ifdef CONFIG_SUSPEND_FREEZER
  236. static inline int suspend_freeze_processes(void)
  237. {
  238. int error;
  239. error = freeze_processes();
  240. /*
  241. * freeze_processes() automatically thaws every task if freezing
  242. * fails. So we need not do anything extra upon error.
  243. */
  244. if (error)
  245. return error;
  246. error = freeze_kernel_threads();
  247. /*
  248. * freeze_kernel_threads() thaws only kernel threads upon freezing
  249. * failure. So we have to thaw the userspace tasks ourselves.
  250. */
  251. if (error)
  252. thaw_processes();
  253. return error;
  254. }
  255. static inline void suspend_thaw_processes(void)
  256. {
  257. thaw_processes();
  258. }
  259. #else
  260. static inline int suspend_freeze_processes(void)
  261. {
  262. return 0;
  263. }
  264. static inline void suspend_thaw_processes(void)
  265. {
  266. }
  267. #endif
  268. #ifdef CONFIG_PM_AUTOSLEEP
  269. /* kernel/power/autosleep.c */
  270. extern int pm_autosleep_init(void);
  271. extern int pm_autosleep_lock(void);
  272. extern void pm_autosleep_unlock(void);
  273. extern suspend_state_t pm_autosleep_state(void);
  274. extern int pm_autosleep_set_state(suspend_state_t state);
  275. #else /* !CONFIG_PM_AUTOSLEEP */
  276. static inline int pm_autosleep_init(void) { return 0; }
  277. static inline int pm_autosleep_lock(void) { return 0; }
  278. static inline void pm_autosleep_unlock(void) {}
  279. static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
  280. #endif /* !CONFIG_PM_AUTOSLEEP */
  281. #ifdef CONFIG_PM_WAKELOCKS
  282. /* kernel/power/wakelock.c */
  283. extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
  284. extern int pm_wake_lock(const char *buf);
  285. extern int pm_wake_unlock(const char *buf);
  286. #endif /* !CONFIG_PM_WAKELOCKS */
  287. static inline int pm_sleep_disable_secondary_cpus(void)
  288. {
  289. cpuidle_pause();
  290. return suspend_disable_secondary_cpus();
  291. }
  292. static inline void pm_sleep_enable_secondary_cpus(void)
  293. {
  294. suspend_enable_secondary_cpus();
  295. cpuidle_resume();
  296. }
  297. void dpm_save_errno(int err);