power.h 9.0 KB

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