power.h 9.2 KB

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