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 17d307c4a8SRafael J. Wysocki #ifdef CONFIG_ARCH_HIBERNATION_HEADER 18d307c4a8SRafael J. Wysocki /* Maximum size of architecture specific data in a hibernation header */ 19d307c4a8SRafael J. Wysocki #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) 20d307c4a8SRafael J. Wysocki 21d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_save(void *addr, unsigned int max_size); 22d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_restore(void *addr); 23d307c4a8SRafael J. Wysocki 24d307c4a8SRafael J. Wysocki static inline int init_header_complete(struct swsusp_info *info) 25d307c4a8SRafael J. Wysocki { 26d307c4a8SRafael J. Wysocki return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE); 27d307c4a8SRafael J. Wysocki } 28d307c4a8SRafael J. Wysocki 29d307c4a8SRafael J. Wysocki static inline char *check_image_kernel(struct swsusp_info *info) 30d307c4a8SRafael J. Wysocki { 31d307c4a8SRafael J. Wysocki return arch_hibernation_header_restore(info) ? 32d307c4a8SRafael J. Wysocki "architecture specific data" : NULL; 33d307c4a8SRafael J. Wysocki } 34d307c4a8SRafael J. Wysocki #endif /* CONFIG_ARCH_HIBERNATION_HEADER */ 35d307c4a8SRafael J. Wysocki 3656f99bcbSRafael J. Wysocki /* 3756f99bcbSRafael J. Wysocki * Keep some memory free so that I/O operations can succeed without paging 3856f99bcbSRafael J. Wysocki * [Might this be more than 4 MB?] 3956f99bcbSRafael J. Wysocki */ 4056f99bcbSRafael J. Wysocki #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) 41d307c4a8SRafael J. Wysocki 4256f99bcbSRafael J. Wysocki /* 4356f99bcbSRafael J. Wysocki * Keep 1 MB of memory free so that device drivers can allocate some pages in 4456f99bcbSRafael J. Wysocki * their .suspend() routines without breaking the suspend to disk. 4556f99bcbSRafael J. Wysocki */ 4656f99bcbSRafael J. Wysocki #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT) 471da177e4SLinus Torvalds 488b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */ 497777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode); 50a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode); 517777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void); 521da177e4SLinus Torvalds #endif 53a6d70980SStephen Hemminger 5449c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long); 5549c3df6aSVivek Goyal 561da177e4SLinus Torvalds #define power_attr(_name) \ 57386f275fSKay Sievers static struct kobj_attribute _name##_attr = { \ 581da177e4SLinus Torvalds .attr = { \ 591da177e4SLinus Torvalds .name = __stringify(_name), \ 601da177e4SLinus Torvalds .mode = 0644, \ 611da177e4SLinus Torvalds }, \ 621da177e4SLinus Torvalds .show = _name##_show, \ 631da177e4SLinus Torvalds .store = _name##_store, \ 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 66853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */ 67853609b6SRafael J. Wysocki extern unsigned long image_size; 68f577eb30SRafael J. Wysocki extern int in_suspend; 6961159a31SRafael J. Wysocki extern dev_t swsusp_resume_device; 709a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block; 71f577eb30SRafael J. Wysocki 7225761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void); 7325761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void); 7425761b6eSRafael J. Wysocki 7574dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void); 7674dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void); 7764a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void); 78f577eb30SRafael J. Wysocki 79fb13a28bSRafael J. Wysocki /** 80fb13a28bSRafael J. Wysocki * Auxiliary structure used for reading the snapshot image data and 81fb13a28bSRafael J. Wysocki * metadata from and writing them to the list of page backup entries 82fb13a28bSRafael J. Wysocki * (PBEs) which is the main data structure of swsusp. 83fb13a28bSRafael J. Wysocki * 84fb13a28bSRafael J. Wysocki * Using struct snapshot_handle we can transfer the image, including its 85fb13a28bSRafael J. Wysocki * metadata, as a continuous sequence of bytes with the help of 86fb13a28bSRafael J. Wysocki * snapshot_read_next() and snapshot_write_next(). 87fb13a28bSRafael J. Wysocki * 88fb13a28bSRafael J. Wysocki * The code that writes the image to a storage or transfers it to 89fb13a28bSRafael J. Wysocki * the user land is required to use snapshot_read_next() for this 90fb13a28bSRafael J. Wysocki * purpose and it should not make any assumptions regarding the internal 91fb13a28bSRafael J. Wysocki * structure of the image. Similarly, the code that reads the image from 92fb13a28bSRafael J. Wysocki * a storage or transfers it from the user land is required to use 93fb13a28bSRafael J. Wysocki * snapshot_write_next(). 94fb13a28bSRafael J. Wysocki * 95fb13a28bSRafael J. Wysocki * This may allow us to change the internal structure of the image 96fb13a28bSRafael J. Wysocki * in the future with considerably less effort. 97fb13a28bSRafael J. Wysocki */ 98fb13a28bSRafael J. Wysocki 99f577eb30SRafael J. Wysocki struct snapshot_handle { 100fb13a28bSRafael J. Wysocki unsigned int cur; /* number of the block of PAGE_SIZE bytes the 101fb13a28bSRafael J. Wysocki * next operation will refer to (ie. current) 102fb13a28bSRafael J. Wysocki */ 103fb13a28bSRafael J. Wysocki void *buffer; /* address of the block to read from 104fb13a28bSRafael J. Wysocki * or write to 105fb13a28bSRafael J. Wysocki */ 106fb13a28bSRafael J. Wysocki int sync_read; /* Set to one to notify the caller of 107fb13a28bSRafael J. Wysocki * snapshot_write_next() that it may 108fb13a28bSRafael J. Wysocki * need to call wait_on_bio_chain() 109fb13a28bSRafael J. Wysocki */ 110f577eb30SRafael J. Wysocki }; 111f577eb30SRafael J. Wysocki 112fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of 113fb13a28bSRafael J. Wysocki * snapshot_read_next()/snapshot_write_next() is allowed to 114fb13a28bSRafael J. Wysocki * read/write data after the function returns 115fb13a28bSRafael J. Wysocki */ 116d3c1b24cSJiri Slaby #define data_of(handle) ((handle).buffer) 117f577eb30SRafael J. Wysocki 118b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone); 119af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void); 120d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle); 121d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle); 1228357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle); 123b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle); 12461159a31SRafael J. Wysocki 1250709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */ 1260709db60SRafael J. Wysocki extern atomic_t snapshot_device_available; 1270709db60SRafael J. Wysocki 128d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap); 129d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap); 130d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void); 13161159a31SRafael J. Wysocki 132a634cc10SRafael J. Wysocki /* 133a634cc10SRafael J. Wysocki * Flags that can be passed from the hibernatig hernel to the "boot" kernel in 134a634cc10SRafael J. Wysocki * the image header. 135a634cc10SRafael J. Wysocki */ 136a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE 1 137a634cc10SRafael J. Wysocki 1388b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */ 13974c7e2efSRandy Dunlap extern int swsusp_check(void); 14074c7e2efSRandy Dunlap extern void swsusp_free(void); 141a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p); 142a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags); 143c2dd0daeSAl Viro extern void swsusp_close(fmode_t); 1440d3a9abeSRafael J. Wysocki 145*8a0d613fSJiri Slaby /* kernel/power/block_io.c */ 146*8a0d613fSJiri Slaby extern struct block_device *hib_resume_bdev; 147*8a0d613fSJiri Slaby 148*8a0d613fSJiri Slaby extern int hib_bio_read_page(pgoff_t page_off, void *addr, 149*8a0d613fSJiri Slaby struct bio **bio_chain); 150*8a0d613fSJiri Slaby extern int hib_bio_write_page(pgoff_t page_off, void *addr, 151*8a0d613fSJiri Slaby struct bio **bio_chain); 152*8a0d613fSJiri Slaby extern int hib_wait_on_bio_chain(struct bio **bio_chain); 153*8a0d613fSJiri Slaby 1540d3a9abeSRafael J. Wysocki struct timeval; 155b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */ 1560d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1570d3a9abeSRafael J. Wysocki unsigned int, char *); 158b10d9117SRafael J. Wysocki 159296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND 160a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */ 161a9d70523SRafael J. Wysocki extern const char *const pm_states[]; 162a9d70523SRafael J. Wysocki 163a9d70523SRafael J. Wysocki extern bool valid_state(suspend_state_t state); 1646c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state); 165a9d70523SRafael J. Wysocki extern int enter_state(suspend_state_t state); 166296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */ 167296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state) 168296699deSRafael J. Wysocki { 169296699deSRafael J. Wysocki return -ENOSYS; 170296699deSRafael J. Wysocki } 171a9d70523SRafael J. Wysocki static inline int enter_state(suspend_state_t state) { return -ENOSYS; } 172a9d70523SRafael J. Wysocki static inline bool valid_state(suspend_state_t state) { return false; } 173296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */ 174296699deSRafael J. Wysocki 175a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND 176a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */ 177a9d70523SRafael J. Wysocki extern void suspend_test_start(void); 178a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label); 179a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */ 180a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {} 181a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {} 182a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */ 183a9d70523SRafael J. Wysocki 18482525756SAlan Stern #ifdef CONFIG_PM_SLEEP 18582525756SAlan Stern /* kernel/power/main.c */ 18682525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val); 18782525756SAlan Stern #endif 1882f8ed1c6SAdrian Bunk 1892f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM 1902f8ed1c6SAdrian Bunk int restore_highmem(void); 1912f8ed1c6SAdrian Bunk #else 1922f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; } 1932f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; } 1942f8ed1c6SAdrian Bunk #endif 1950e7d56e3SRafael J. Wysocki 1960e7d56e3SRafael J. Wysocki /* 1970e7d56e3SRafael J. Wysocki * Suspend test levels 1980e7d56e3SRafael J. Wysocki */ 1990e7d56e3SRafael J. Wysocki enum { 2000e7d56e3SRafael J. Wysocki /* keep first */ 2010e7d56e3SRafael J. Wysocki TEST_NONE, 2020e7d56e3SRafael J. Wysocki TEST_CORE, 2030e7d56e3SRafael J. Wysocki TEST_CPUS, 2040e7d56e3SRafael J. Wysocki TEST_PLATFORM, 2050e7d56e3SRafael J. Wysocki TEST_DEVICES, 2060e7d56e3SRafael J. Wysocki TEST_FREEZER, 2070e7d56e3SRafael J. Wysocki /* keep last */ 2080e7d56e3SRafael J. Wysocki __TEST_AFTER_LAST 2090e7d56e3SRafael J. Wysocki }; 2100e7d56e3SRafael J. Wysocki 2110e7d56e3SRafael J. Wysocki #define TEST_FIRST TEST_NONE 2120e7d56e3SRafael J. Wysocki #define TEST_MAX (__TEST_AFTER_LAST - 1) 2134cc79776SRafael J. Wysocki 2144cc79776SRafael J. Wysocki extern int pm_test_level; 215b28f5081SJohannes Berg 216b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER 217b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 218b28f5081SJohannes Berg { 219b28f5081SJohannes Berg return freeze_processes(); 220b28f5081SJohannes Berg } 221b28f5081SJohannes Berg 222b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 223b28f5081SJohannes Berg { 224b28f5081SJohannes Berg thaw_processes(); 225b28f5081SJohannes Berg } 226b28f5081SJohannes Berg #else 227b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 228b28f5081SJohannes Berg { 229b28f5081SJohannes Berg return 0; 230b28f5081SJohannes Berg } 231b28f5081SJohannes Berg 232b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 233b28f5081SJohannes Berg { 234b28f5081SJohannes Berg } 235b28f5081SJohannes Berg #endif 236