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); 77*64a473cbSRafael 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 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 1508b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */ 15174c7e2efSRandy Dunlap extern int swsusp_check(void); 15274c7e2efSRandy Dunlap extern void swsusp_free(void); 153a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p); 154a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags); 155c2dd0daeSAl Viro extern void swsusp_close(fmode_t); 1560d3a9abeSRafael J. Wysocki 1570d3a9abeSRafael J. Wysocki struct timeval; 158b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */ 1590d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1600d3a9abeSRafael J. Wysocki unsigned int, char *); 161b10d9117SRafael J. Wysocki 162296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND 163a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */ 164a9d70523SRafael J. Wysocki extern const char *const pm_states[]; 165a9d70523SRafael J. Wysocki 166a9d70523SRafael J. Wysocki extern bool valid_state(suspend_state_t state); 1676c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state); 168a9d70523SRafael J. Wysocki extern int enter_state(suspend_state_t state); 169296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */ 170296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state) 171296699deSRafael J. Wysocki { 172296699deSRafael J. Wysocki return -ENOSYS; 173296699deSRafael J. Wysocki } 174a9d70523SRafael J. Wysocki static inline int enter_state(suspend_state_t state) { return -ENOSYS; } 175a9d70523SRafael J. Wysocki static inline bool valid_state(suspend_state_t state) { return false; } 176296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */ 177296699deSRafael J. Wysocki 178a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND 179a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */ 180a9d70523SRafael J. Wysocki extern void suspend_test_start(void); 181a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label); 182a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */ 183a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {} 184a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {} 185a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */ 186a9d70523SRafael J. Wysocki 18782525756SAlan Stern #ifdef CONFIG_PM_SLEEP 18882525756SAlan Stern /* kernel/power/main.c */ 18982525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val); 19082525756SAlan Stern #endif 1912f8ed1c6SAdrian Bunk 1922f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM 1932f8ed1c6SAdrian Bunk int restore_highmem(void); 1942f8ed1c6SAdrian Bunk #else 1952f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; } 1962f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; } 1972f8ed1c6SAdrian Bunk #endif 1980e7d56e3SRafael J. Wysocki 1990e7d56e3SRafael J. Wysocki /* 2000e7d56e3SRafael J. Wysocki * Suspend test levels 2010e7d56e3SRafael J. Wysocki */ 2020e7d56e3SRafael J. Wysocki enum { 2030e7d56e3SRafael J. Wysocki /* keep first */ 2040e7d56e3SRafael J. Wysocki TEST_NONE, 2050e7d56e3SRafael J. Wysocki TEST_CORE, 2060e7d56e3SRafael J. Wysocki TEST_CPUS, 2070e7d56e3SRafael J. Wysocki TEST_PLATFORM, 2080e7d56e3SRafael J. Wysocki TEST_DEVICES, 2090e7d56e3SRafael J. Wysocki TEST_FREEZER, 2100e7d56e3SRafael J. Wysocki /* keep last */ 2110e7d56e3SRafael J. Wysocki __TEST_AFTER_LAST 2120e7d56e3SRafael J. Wysocki }; 2130e7d56e3SRafael J. Wysocki 2140e7d56e3SRafael J. Wysocki #define TEST_FIRST TEST_NONE 2150e7d56e3SRafael J. Wysocki #define TEST_MAX (__TEST_AFTER_LAST - 1) 2164cc79776SRafael J. Wysocki 2174cc79776SRafael J. Wysocki extern int pm_test_level; 218b28f5081SJohannes Berg 219b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER 220b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 221b28f5081SJohannes Berg { 222b28f5081SJohannes Berg return freeze_processes(); 223b28f5081SJohannes Berg } 224b28f5081SJohannes Berg 225b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 226b28f5081SJohannes Berg { 227b28f5081SJohannes Berg thaw_processes(); 228b28f5081SJohannes Berg } 229b28f5081SJohannes Berg #else 230b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 231b28f5081SJohannes Berg { 232b28f5081SJohannes Berg return 0; 233b28f5081SJohannes Berg } 234b28f5081SJohannes Berg 235b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 236b28f5081SJohannes Berg { 237b28f5081SJohannes Berg } 238b28f5081SJohannes Berg #endif 239