power.h 8.4 KB

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