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 487777fab9SRafael J. Wysocki /* kernel/power/disk.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); 7772a97e08SRafael J. Wysocki extern unsigned int count_data_pages(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 loff_t offset; /* number of the last byte ready for reading 101fb13a28bSRafael J. Wysocki * or writing in the sequence 102fb13a28bSRafael J. Wysocki */ 103fb13a28bSRafael J. Wysocki unsigned int cur; /* number of the block of PAGE_SIZE bytes the 104fb13a28bSRafael J. Wysocki * next operation will refer to (ie. current) 105fb13a28bSRafael J. Wysocki */ 106fb13a28bSRafael J. Wysocki unsigned int cur_offset; /* offset with respect to the current 107fb13a28bSRafael J. Wysocki * block (for the next operation) 108fb13a28bSRafael J. Wysocki */ 109fb13a28bSRafael J. Wysocki unsigned int prev; /* number of the block of PAGE_SIZE bytes that 110fb13a28bSRafael J. Wysocki * was the current one previously 111fb13a28bSRafael J. Wysocki */ 112fb13a28bSRafael J. Wysocki void *buffer; /* address of the block to read from 113fb13a28bSRafael J. Wysocki * or write to 114fb13a28bSRafael J. Wysocki */ 115fb13a28bSRafael J. Wysocki unsigned int buf_offset; /* location to read from or write to, 116fb13a28bSRafael J. Wysocki * given as a displacement from 'buffer' 117fb13a28bSRafael J. Wysocki */ 118fb13a28bSRafael J. Wysocki int sync_read; /* Set to one to notify the caller of 119fb13a28bSRafael J. Wysocki * snapshot_write_next() that it may 120fb13a28bSRafael J. Wysocki * need to call wait_on_bio_chain() 121fb13a28bSRafael J. Wysocki */ 122f577eb30SRafael J. Wysocki }; 123f577eb30SRafael J. Wysocki 124fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of 125fb13a28bSRafael J. Wysocki * snapshot_read_next()/snapshot_write_next() is allowed to 126fb13a28bSRafael J. Wysocki * read/write data after the function returns 127fb13a28bSRafael J. Wysocki */ 128f577eb30SRafael J. Wysocki #define data_of(handle) ((handle).buffer + (handle).buf_offset) 129f577eb30SRafael J. Wysocki 130b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone); 131af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void); 132f577eb30SRafael J. Wysocki extern int snapshot_read_next(struct snapshot_handle *handle, size_t count); 133f577eb30SRafael J. Wysocki extern int snapshot_write_next(struct snapshot_handle *handle, size_t count); 1348357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle); 135b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle); 13661159a31SRafael J. Wysocki 1370709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */ 1380709db60SRafael J. Wysocki extern atomic_t snapshot_device_available; 1390709db60SRafael J. Wysocki 140d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap); 141d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap); 142d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void); 14361159a31SRafael J. Wysocki 144a634cc10SRafael J. Wysocki /* 145a634cc10SRafael J. Wysocki * Flags that can be passed from the hibernatig hernel to the "boot" kernel in 146a634cc10SRafael J. Wysocki * the image header. 147a634cc10SRafael J. Wysocki */ 148a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE 1 149a634cc10SRafael J. Wysocki 150a634cc10SRafael J. Wysocki /* kernel/power/disk.c */ 15174c7e2efSRandy Dunlap extern int swsusp_check(void); 15261159a31SRafael J. Wysocki extern int swsusp_shrink_memory(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); 156*c2dd0daeSAl Viro extern void swsusp_close(fmode_t); 1570d3a9abeSRafael J. Wysocki 1580d3a9abeSRafael J. Wysocki struct timeval; 159b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */ 1600d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1610d3a9abeSRafael J. Wysocki unsigned int, char *); 162b10d9117SRafael J. Wysocki 163296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND 164b10d9117SRafael J. Wysocki /* kernel/power/main.c */ 1656c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(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 } 171296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */ 172296699deSRafael J. Wysocki 17382525756SAlan Stern #ifdef CONFIG_PM_SLEEP 17482525756SAlan Stern /* kernel/power/main.c */ 17582525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val); 17682525756SAlan Stern #endif 1772f8ed1c6SAdrian Bunk 1782f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM 1792f8ed1c6SAdrian Bunk unsigned int count_highmem_pages(void); 1802f8ed1c6SAdrian Bunk int restore_highmem(void); 1812f8ed1c6SAdrian Bunk #else 1822f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; } 1832f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; } 1842f8ed1c6SAdrian Bunk #endif 1850e7d56e3SRafael J. Wysocki 1860e7d56e3SRafael J. Wysocki /* 1870e7d56e3SRafael J. Wysocki * Suspend test levels 1880e7d56e3SRafael J. Wysocki */ 1890e7d56e3SRafael J. Wysocki enum { 1900e7d56e3SRafael J. Wysocki /* keep first */ 1910e7d56e3SRafael J. Wysocki TEST_NONE, 1920e7d56e3SRafael J. Wysocki TEST_CORE, 1930e7d56e3SRafael J. Wysocki TEST_CPUS, 1940e7d56e3SRafael J. Wysocki TEST_PLATFORM, 1950e7d56e3SRafael J. Wysocki TEST_DEVICES, 1960e7d56e3SRafael J. Wysocki TEST_FREEZER, 1970e7d56e3SRafael J. Wysocki /* keep last */ 1980e7d56e3SRafael J. Wysocki __TEST_AFTER_LAST 1990e7d56e3SRafael J. Wysocki }; 2000e7d56e3SRafael J. Wysocki 2010e7d56e3SRafael J. Wysocki #define TEST_FIRST TEST_NONE 2020e7d56e3SRafael J. Wysocki #define TEST_MAX (__TEST_AFTER_LAST - 1) 2034cc79776SRafael J. Wysocki 2044cc79776SRafael J. Wysocki extern int pm_test_level; 205b28f5081SJohannes Berg 206b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER 207b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 208b28f5081SJohannes Berg { 209b28f5081SJohannes Berg return freeze_processes(); 210b28f5081SJohannes Berg } 211b28f5081SJohannes Berg 212b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 213b28f5081SJohannes Berg { 214b28f5081SJohannes Berg thaw_processes(); 215b28f5081SJohannes Berg } 216b28f5081SJohannes Berg #else 217b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 218b28f5081SJohannes Berg { 219b28f5081SJohannes Berg return 0; 220b28f5081SJohannes Berg } 221b28f5081SJohannes Berg 222b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 223b28f5081SJohannes Berg { 224b28f5081SJohannes Berg } 225b28f5081SJohannes Berg #endif 226