power.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. struct swsusp_info {
  8. struct new_utsname uts;
  9. u32 version_code;
  10. unsigned long num_physpages;
  11. int cpus;
  12. unsigned long image_pages;
  13. unsigned long pages;
  14. unsigned long size;
  15. } __aligned(PAGE_SIZE);
  16. #ifdef CONFIG_HIBERNATION
  17. /* kernel/power/snapshot.c */
  18. extern void __init hibernate_reserved_size_init(void);
  19. extern void __init hibernate_image_size_init(void);
  20. #ifdef CONFIG_ARCH_HIBERNATION_HEADER
  21. /* Maximum size of architecture specific data in a hibernation header */
  22. #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4)
  23. extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
  24. extern int arch_hibernation_header_restore(void *addr);
  25. static inline int init_header_complete(struct swsusp_info *info)
  26. {
  27. return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
  28. }
  29. static inline char *check_image_kernel(struct swsusp_info *info)
  30. {
  31. return arch_hibernation_header_restore(info) ?
  32. "architecture specific data" : NULL;
  33. }
  34. #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
  35. extern int hibernate_resume_nonboot_cpu_disable(void);
  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 int hibernation_snapshot(int platform_mode);
  50. extern int hibernation_restore(int platform_mode);
  51. extern int hibernation_platform_enter(void);
  52. #ifdef CONFIG_STRICT_KERNEL_RWX
  53. /* kernel/power/snapshot.c */
  54. extern void enable_restore_image_protection(void);
  55. #else
  56. static inline void enable_restore_image_protection(void) {}
  57. #endif /* CONFIG_STRICT_KERNEL_RWX */
  58. #else /* !CONFIG_HIBERNATION */
  59. static inline void hibernate_reserved_size_init(void) {}
  60. static inline void hibernate_image_size_init(void) {}
  61. #endif /* !CONFIG_HIBERNATION */
  62. extern int pfn_is_nosave(unsigned long);
  63. #define power_attr(_name) \
  64. static struct kobj_attribute _name##_attr = { \
  65. .attr = { \
  66. .name = __stringify(_name), \
  67. .mode = 0644, \
  68. }, \
  69. .show = _name##_show, \
  70. .store = _name##_store, \
  71. }
  72. #define power_attr_ro(_name) \
  73. static struct kobj_attribute _name##_attr = { \
  74. .attr = { \
  75. .name = __stringify(_name), \
  76. .mode = S_IRUGO, \
  77. }, \
  78. .show = _name##_show, \
  79. }
  80. /* Preferred image size in bytes (default 500 MB) */
  81. extern unsigned long image_size;
  82. /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
  83. extern unsigned long reserved_size;
  84. extern int in_suspend;
  85. extern dev_t swsusp_resume_device;
  86. extern sector_t swsusp_resume_block;
  87. extern int create_basic_memory_bitmaps(void);
  88. extern void free_basic_memory_bitmaps(void);
  89. extern int hibernate_preallocate_memory(void);
  90. extern void clear_free_pages(void);
  91. /**
  92. * Auxiliary structure used for reading the snapshot image data and
  93. * metadata from and writing them to the list of page backup entries
  94. * (PBEs) which is the main data structure of swsusp.
  95. *
  96. * Using struct snapshot_handle we can transfer the image, including its
  97. * metadata, as a continuous sequence of bytes with the help of
  98. * snapshot_read_next() and snapshot_write_next().
  99. *
  100. * The code that writes the image to a storage or transfers it to
  101. * the user land is required to use snapshot_read_next() for this
  102. * purpose and it should not make any assumptions regarding the internal
  103. * structure of the image. Similarly, the code that reads the image from
  104. * a storage or transfers it from the user land is required to use
  105. * snapshot_write_next().
  106. *
  107. * This may allow us to change the internal structure of the image
  108. * in the future with considerably less effort.
  109. */
  110. struct snapshot_handle {
  111. unsigned int cur; /* number of the block of PAGE_SIZE bytes the
  112. * next operation will refer to (ie. current)
  113. */
  114. void *buffer; /* address of the block to read from
  115. * or write to
  116. */
  117. int sync_read; /* Set to one to notify the caller of
  118. * snapshot_write_next() that it may
  119. * need to call wait_on_bio_chain()
  120. */
  121. };
  122. /* This macro returns the address from/to which the caller of
  123. * snapshot_read_next()/snapshot_write_next() is allowed to
  124. * read/write data after the function returns
  125. */
  126. #define data_of(handle) ((handle).buffer)
  127. extern unsigned int snapshot_additional_pages(struct zone *zone);
  128. extern unsigned long snapshot_get_image_size(void);
  129. extern int snapshot_read_next(struct snapshot_handle *handle);
  130. extern int snapshot_write_next(struct snapshot_handle *handle);
  131. extern void snapshot_write_finalize(struct snapshot_handle *handle);
  132. extern int snapshot_image_loaded(struct snapshot_handle *handle);
  133. /* If unset, the snapshot device cannot be open. */
  134. extern atomic_t snapshot_device_available;
  135. extern sector_t alloc_swapdev_block(int swap);
  136. extern void free_all_swap_pages(int swap);
  137. extern int swsusp_swap_in_use(void);
  138. /*
  139. * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
  140. * the image header.
  141. */
  142. #define SF_PLATFORM_MODE 1
  143. #define SF_NOCOMPRESS_MODE 2
  144. #define SF_CRC32_MODE 4
  145. /* kernel/power/hibernate.c */
  146. extern int swsusp_check(void);
  147. extern void swsusp_free(void);
  148. extern int swsusp_read(unsigned int *flags_p);
  149. extern int swsusp_write(unsigned int flags);
  150. extern void swsusp_close(fmode_t);
  151. #ifdef CONFIG_SUSPEND
  152. extern int swsusp_unmark(void);
  153. #endif
  154. struct timeval;
  155. /* kernel/power/swsusp.c */
  156. extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
  157. #ifdef CONFIG_SUSPEND
  158. /* kernel/power/suspend.c */
  159. extern const char * const pm_labels[];
  160. extern const char *pm_states[];
  161. extern const char *mem_sleep_states[];
  162. extern int suspend_devices_and_enter(suspend_state_t state);
  163. #else /* !CONFIG_SUSPEND */
  164. #define mem_sleep_current PM_SUSPEND_ON
  165. static inline int suspend_devices_and_enter(suspend_state_t state)
  166. {
  167. return -ENOSYS;
  168. }
  169. #endif /* !CONFIG_SUSPEND */
  170. #ifdef CONFIG_PM_TEST_SUSPEND
  171. /* kernel/power/suspend_test.c */
  172. extern void suspend_test_start(void);
  173. extern void suspend_test_finish(const char *label);
  174. #else /* !CONFIG_PM_TEST_SUSPEND */
  175. static inline void suspend_test_start(void) {}
  176. static inline void suspend_test_finish(const char *label) {}
  177. #endif /* !CONFIG_PM_TEST_SUSPEND */
  178. #ifdef CONFIG_PM_SLEEP
  179. /* kernel/power/main.c */
  180. extern int __pm_notifier_call_chain(unsigned long val, int nr_to_call,
  181. int *nr_calls);
  182. extern int pm_notifier_call_chain(unsigned long val);
  183. #endif
  184. #ifdef CONFIG_HIGHMEM
  185. int restore_highmem(void);
  186. #else
  187. static inline unsigned int count_highmem_pages(void) { return 0; }
  188. static inline int restore_highmem(void) { return 0; }
  189. #endif
  190. /*
  191. * Suspend test levels
  192. */
  193. enum {
  194. /* keep first */
  195. TEST_NONE,
  196. TEST_CORE,
  197. TEST_CPUS,
  198. TEST_PLATFORM,
  199. TEST_DEVICES,
  200. TEST_FREEZER,
  201. /* keep last */
  202. __TEST_AFTER_LAST
  203. };
  204. #define TEST_FIRST TEST_NONE
  205. #define TEST_MAX (__TEST_AFTER_LAST - 1)
  206. #ifdef CONFIG_PM_SLEEP_DEBUG
  207. extern int pm_test_level;
  208. #else
  209. #define pm_test_level (TEST_NONE)
  210. #endif
  211. #ifdef CONFIG_SUSPEND_FREEZER
  212. static inline int suspend_freeze_processes(void)
  213. {
  214. int error;
  215. error = freeze_processes();
  216. /*
  217. * freeze_processes() automatically thaws every task if freezing
  218. * fails. So we need not do anything extra upon error.
  219. */
  220. if (error)
  221. return error;
  222. error = freeze_kernel_threads();
  223. /*
  224. * freeze_kernel_threads() thaws only kernel threads upon freezing
  225. * failure. So we have to thaw the userspace tasks ourselves.
  226. */
  227. if (error)
  228. thaw_processes();
  229. return error;
  230. }
  231. static inline void suspend_thaw_processes(void)
  232. {
  233. thaw_processes();
  234. }
  235. #else
  236. static inline int suspend_freeze_processes(void)
  237. {
  238. return 0;
  239. }
  240. static inline void suspend_thaw_processes(void)
  241. {
  242. }
  243. #endif
  244. #ifdef CONFIG_PM_AUTOSLEEP
  245. /* kernel/power/autosleep.c */
  246. extern int pm_autosleep_init(void);
  247. extern int pm_autosleep_lock(void);
  248. extern void pm_autosleep_unlock(void);
  249. extern suspend_state_t pm_autosleep_state(void);
  250. extern int pm_autosleep_set_state(suspend_state_t state);
  251. #else /* !CONFIG_PM_AUTOSLEEP */
  252. static inline int pm_autosleep_init(void) { return 0; }
  253. static inline int pm_autosleep_lock(void) { return 0; }
  254. static inline void pm_autosleep_unlock(void) {}
  255. static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
  256. #endif /* !CONFIG_PM_AUTOSLEEP */
  257. #ifdef CONFIG_PM_WAKELOCKS
  258. /* kernel/power/wakelock.c */
  259. extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
  260. extern int pm_wake_lock(const char *buf);
  261. extern int pm_wake_unlock(const char *buf);
  262. #endif /* !CONFIG_PM_WAKELOCKS */