power.h 7.2 KB

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