11da177e4SLinus Torvalds #include <linux/suspend.h> 23010f8caSRafael J. Wysocki #include <linux/suspend_ioctls.h> 31da177e4SLinus Torvalds #include <linux/utsname.h> 4b28f5081SJohannes Berg #include <linux/freezer.h> 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds struct swsusp_info { 71da177e4SLinus Torvalds struct new_utsname uts; 81da177e4SLinus Torvalds u32 version_code; 91da177e4SLinus Torvalds unsigned long num_physpages; 101da177e4SLinus Torvalds int cpus; 111da177e4SLinus Torvalds unsigned long image_pages; 127088a5c0SRafael J. Wysocki unsigned long pages; 136e1819d6SRafael J. Wysocki unsigned long size; 141da177e4SLinus Torvalds } __attribute__((aligned(PAGE_SIZE))); 151da177e4SLinus Torvalds 16b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 17ac5c24ecSRafael J. Wysocki /* kernel/power/snapshot.c */ 18ddeb6487SRafael J. Wysocki extern void __init hibernate_reserved_size_init(void); 19ac5c24ecSRafael J. Wysocki extern void __init hibernate_image_size_init(void); 20ac5c24ecSRafael J. Wysocki 21d307c4a8SRafael J. Wysocki #ifdef CONFIG_ARCH_HIBERNATION_HEADER 22d307c4a8SRafael J. Wysocki /* Maximum size of architecture specific data in a hibernation header */ 23d307c4a8SRafael J. Wysocki #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) 24d307c4a8SRafael J. Wysocki 25d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_save(void *addr, unsigned int max_size); 26d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_restore(void *addr); 27d307c4a8SRafael J. Wysocki 28d307c4a8SRafael J. Wysocki static inline int init_header_complete(struct swsusp_info *info) 29d307c4a8SRafael J. Wysocki { 30d307c4a8SRafael J. Wysocki return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE); 31d307c4a8SRafael J. Wysocki } 32d307c4a8SRafael J. Wysocki 33d307c4a8SRafael J. Wysocki static inline char *check_image_kernel(struct swsusp_info *info) 34d307c4a8SRafael J. Wysocki { 35d307c4a8SRafael J. Wysocki return arch_hibernation_header_restore(info) ? 36d307c4a8SRafael J. Wysocki "architecture specific data" : NULL; 37d307c4a8SRafael J. Wysocki } 38d307c4a8SRafael J. Wysocki #endif /* CONFIG_ARCH_HIBERNATION_HEADER */ 39d307c4a8SRafael J. Wysocki 4056f99bcbSRafael J. Wysocki /* 4156f99bcbSRafael J. Wysocki * Keep some memory free so that I/O operations can succeed without paging 4256f99bcbSRafael J. Wysocki * [Might this be more than 4 MB?] 4356f99bcbSRafael J. Wysocki */ 4456f99bcbSRafael J. Wysocki #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) 45d307c4a8SRafael J. Wysocki 4656f99bcbSRafael J. Wysocki /* 4756f99bcbSRafael J. Wysocki * Keep 1 MB of memory free so that device drivers can allocate some pages in 4856f99bcbSRafael J. Wysocki * their .suspend() routines without breaking the suspend to disk. 4956f99bcbSRafael J. Wysocki */ 5056f99bcbSRafael J. Wysocki #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT) 511da177e4SLinus Torvalds 528b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */ 537777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode); 54a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode); 557777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void); 56ac5c24ecSRafael J. Wysocki 57ac5c24ecSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */ 58ac5c24ecSRafael J. Wysocki 59ddeb6487SRafael J. Wysocki static inline void hibernate_reserved_size_init(void) {} 60ac5c24ecSRafael J. Wysocki static inline void hibernate_image_size_init(void) {} 61ac5c24ecSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */ 62a6d70980SStephen Hemminger 6349c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long); 6449c3df6aSVivek Goyal 651da177e4SLinus Torvalds #define power_attr(_name) \ 66386f275fSKay Sievers static struct kobj_attribute _name##_attr = { \ 671da177e4SLinus Torvalds .attr = { \ 681da177e4SLinus Torvalds .name = __stringify(_name), \ 691da177e4SLinus Torvalds .mode = 0644, \ 701da177e4SLinus Torvalds }, \ 711da177e4SLinus Torvalds .show = _name##_show, \ 721da177e4SLinus Torvalds .store = _name##_store, \ 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 75853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */ 76853609b6SRafael J. Wysocki extern unsigned long image_size; 77ddeb6487SRafael J. Wysocki /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */ 78ddeb6487SRafael J. Wysocki extern unsigned long reserved_size; 79f577eb30SRafael J. Wysocki extern int in_suspend; 8061159a31SRafael J. Wysocki extern dev_t swsusp_resume_device; 819a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block; 82f577eb30SRafael J. Wysocki 8325761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void); 8425761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void); 8525761b6eSRafael J. Wysocki 8674dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void); 8774dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void); 8864a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void); 89f577eb30SRafael J. Wysocki 90fb13a28bSRafael J. Wysocki /** 91fb13a28bSRafael J. Wysocki * Auxiliary structure used for reading the snapshot image data and 92fb13a28bSRafael J. Wysocki * metadata from and writing them to the list of page backup entries 93fb13a28bSRafael J. Wysocki * (PBEs) which is the main data structure of swsusp. 94fb13a28bSRafael J. Wysocki * 95fb13a28bSRafael J. Wysocki * Using struct snapshot_handle we can transfer the image, including its 96fb13a28bSRafael J. Wysocki * metadata, as a continuous sequence of bytes with the help of 97fb13a28bSRafael J. Wysocki * snapshot_read_next() and snapshot_write_next(). 98fb13a28bSRafael J. Wysocki * 99fb13a28bSRafael J. Wysocki * The code that writes the image to a storage or transfers it to 100fb13a28bSRafael J. Wysocki * the user land is required to use snapshot_read_next() for this 101fb13a28bSRafael J. Wysocki * purpose and it should not make any assumptions regarding the internal 102fb13a28bSRafael J. Wysocki * structure of the image. Similarly, the code that reads the image from 103fb13a28bSRafael J. Wysocki * a storage or transfers it from the user land is required to use 104fb13a28bSRafael J. Wysocki * snapshot_write_next(). 105fb13a28bSRafael J. Wysocki * 106fb13a28bSRafael J. Wysocki * This may allow us to change the internal structure of the image 107fb13a28bSRafael J. Wysocki * in the future with considerably less effort. 108fb13a28bSRafael J. Wysocki */ 109fb13a28bSRafael J. Wysocki 110f577eb30SRafael J. Wysocki struct snapshot_handle { 111fb13a28bSRafael J. Wysocki unsigned int cur; /* number of the block of PAGE_SIZE bytes the 112fb13a28bSRafael J. Wysocki * next operation will refer to (ie. current) 113fb13a28bSRafael J. Wysocki */ 114fb13a28bSRafael J. Wysocki void *buffer; /* address of the block to read from 115fb13a28bSRafael J. Wysocki * or write to 116fb13a28bSRafael J. Wysocki */ 117fb13a28bSRafael J. Wysocki int sync_read; /* Set to one to notify the caller of 118fb13a28bSRafael J. Wysocki * snapshot_write_next() that it may 119fb13a28bSRafael J. Wysocki * need to call wait_on_bio_chain() 120fb13a28bSRafael J. Wysocki */ 121f577eb30SRafael J. Wysocki }; 122f577eb30SRafael J. Wysocki 123fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of 124fb13a28bSRafael J. Wysocki * snapshot_read_next()/snapshot_write_next() is allowed to 125fb13a28bSRafael J. Wysocki * read/write data after the function returns 126fb13a28bSRafael J. Wysocki */ 127d3c1b24cSJiri Slaby #define data_of(handle) ((handle).buffer) 128f577eb30SRafael J. Wysocki 129b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone); 130af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void); 131d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle); 132d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle); 1338357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle); 134b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle); 13561159a31SRafael J. Wysocki 1360709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */ 1370709db60SRafael J. Wysocki extern atomic_t snapshot_device_available; 1380709db60SRafael J. Wysocki 139d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap); 140d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap); 141d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void); 14261159a31SRafael J. Wysocki 143a634cc10SRafael J. Wysocki /* 144a634cc10SRafael J. Wysocki * Flags that can be passed from the hibernatig hernel to the "boot" kernel in 145a634cc10SRafael J. Wysocki * the image header. 146a634cc10SRafael J. Wysocki */ 147a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE 1 148f996fc96SBojan Smojver #define SF_NOCOMPRESS_MODE 2 149*081a9d04SBojan Smojver #define SF_CRC32_MODE 4 150a634cc10SRafael J. Wysocki 1518b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */ 15274c7e2efSRandy Dunlap extern int swsusp_check(void); 15374c7e2efSRandy Dunlap extern void swsusp_free(void); 154a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p); 155a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags); 156c2dd0daeSAl Viro extern void swsusp_close(fmode_t); 1570d3a9abeSRafael J. Wysocki 1588a0d613fSJiri Slaby /* kernel/power/block_io.c */ 1598a0d613fSJiri Slaby extern struct block_device *hib_resume_bdev; 1608a0d613fSJiri Slaby 1618a0d613fSJiri Slaby extern int hib_bio_read_page(pgoff_t page_off, void *addr, 1628a0d613fSJiri Slaby struct bio **bio_chain); 1638a0d613fSJiri Slaby extern int hib_bio_write_page(pgoff_t page_off, void *addr, 1648a0d613fSJiri Slaby struct bio **bio_chain); 1658a0d613fSJiri Slaby extern int hib_wait_on_bio_chain(struct bio **bio_chain); 1668a0d613fSJiri Slaby 1670d3a9abeSRafael J. Wysocki struct timeval; 168b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */ 1690d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1700d3a9abeSRafael J. Wysocki unsigned int, char *); 171b10d9117SRafael J. Wysocki 172296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND 173a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */ 174a9d70523SRafael J. Wysocki extern const char *const pm_states[]; 175a9d70523SRafael J. Wysocki 176a9d70523SRafael J. Wysocki extern bool valid_state(suspend_state_t state); 1776c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state); 178a9d70523SRafael J. Wysocki extern int enter_state(suspend_state_t state); 179296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */ 180296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state) 181296699deSRafael J. Wysocki { 182296699deSRafael J. Wysocki return -ENOSYS; 183296699deSRafael J. Wysocki } 184a9d70523SRafael J. Wysocki static inline int enter_state(suspend_state_t state) { return -ENOSYS; } 185a9d70523SRafael J. Wysocki static inline bool valid_state(suspend_state_t state) { return false; } 186296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */ 187296699deSRafael J. Wysocki 188a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND 189a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */ 190a9d70523SRafael J. Wysocki extern void suspend_test_start(void); 191a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label); 192a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */ 193a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {} 194a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {} 195a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */ 196a9d70523SRafael J. Wysocki 19782525756SAlan Stern #ifdef CONFIG_PM_SLEEP 19882525756SAlan Stern /* kernel/power/main.c */ 19982525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val); 20082525756SAlan Stern #endif 2012f8ed1c6SAdrian Bunk 2022f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM 2032f8ed1c6SAdrian Bunk int restore_highmem(void); 2042f8ed1c6SAdrian Bunk #else 2052f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; } 2062f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; } 2072f8ed1c6SAdrian Bunk #endif 2080e7d56e3SRafael J. Wysocki 2090e7d56e3SRafael J. Wysocki /* 2100e7d56e3SRafael J. Wysocki * Suspend test levels 2110e7d56e3SRafael J. Wysocki */ 2120e7d56e3SRafael J. Wysocki enum { 2130e7d56e3SRafael J. Wysocki /* keep first */ 2140e7d56e3SRafael J. Wysocki TEST_NONE, 2150e7d56e3SRafael J. Wysocki TEST_CORE, 2160e7d56e3SRafael J. Wysocki TEST_CPUS, 2170e7d56e3SRafael J. Wysocki TEST_PLATFORM, 2180e7d56e3SRafael J. Wysocki TEST_DEVICES, 2190e7d56e3SRafael J. Wysocki TEST_FREEZER, 2200e7d56e3SRafael J. Wysocki /* keep last */ 2210e7d56e3SRafael J. Wysocki __TEST_AFTER_LAST 2220e7d56e3SRafael J. Wysocki }; 2230e7d56e3SRafael J. Wysocki 2240e7d56e3SRafael J. Wysocki #define TEST_FIRST TEST_NONE 2250e7d56e3SRafael J. Wysocki #define TEST_MAX (__TEST_AFTER_LAST - 1) 2264cc79776SRafael J. Wysocki 2274cc79776SRafael J. Wysocki extern int pm_test_level; 228b28f5081SJohannes Berg 229b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER 230b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 231b28f5081SJohannes Berg { 2322aede851SRafael J. Wysocki int error = freeze_processes(); 2332aede851SRafael J. Wysocki return error ? : freeze_kernel_threads(); 234b28f5081SJohannes Berg } 235b28f5081SJohannes Berg 236b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 237b28f5081SJohannes Berg { 238b28f5081SJohannes Berg thaw_processes(); 239b28f5081SJohannes Berg } 240b28f5081SJohannes Berg #else 241b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 242b28f5081SJohannes Berg { 243b28f5081SJohannes Berg return 0; 244b28f5081SJohannes Berg } 245b28f5081SJohannes Berg 246b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 247b28f5081SJohannes Berg { 248b28f5081SJohannes Berg } 249b28f5081SJohannes Berg #endif 250