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 */ 5397819a26SSrivatsa S. Bhat extern bool freezer_test_done; 5497819a26SSrivatsa S. Bhat 557777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode); 56a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode); 577777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void); 58ac5c24ecSRafael J. Wysocki 59ac5c24ecSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */ 60ac5c24ecSRafael J. Wysocki 61ddeb6487SRafael J. Wysocki static inline void hibernate_reserved_size_init(void) {} 62ac5c24ecSRafael J. Wysocki static inline void hibernate_image_size_init(void) {} 63ac5c24ecSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */ 64a6d70980SStephen Hemminger 6549c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long); 6649c3df6aSVivek Goyal 671da177e4SLinus Torvalds #define power_attr(_name) \ 68386f275fSKay Sievers static struct kobj_attribute _name##_attr = { \ 691da177e4SLinus Torvalds .attr = { \ 701da177e4SLinus Torvalds .name = __stringify(_name), \ 711da177e4SLinus Torvalds .mode = 0644, \ 721da177e4SLinus Torvalds }, \ 731da177e4SLinus Torvalds .show = _name##_show, \ 741da177e4SLinus Torvalds .store = _name##_store, \ 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 77853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */ 78853609b6SRafael J. Wysocki extern unsigned long image_size; 79ddeb6487SRafael J. Wysocki /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */ 80ddeb6487SRafael J. Wysocki extern unsigned long reserved_size; 81f577eb30SRafael J. Wysocki extern int in_suspend; 8261159a31SRafael J. Wysocki extern dev_t swsusp_resume_device; 839a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block; 84f577eb30SRafael J. Wysocki 8525761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void); 8625761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void); 8725761b6eSRafael J. Wysocki 8874dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void); 8974dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void); 9064a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void); 91f577eb30SRafael J. Wysocki 92fb13a28bSRafael J. Wysocki /** 93fb13a28bSRafael J. Wysocki * Auxiliary structure used for reading the snapshot image data and 94fb13a28bSRafael J. Wysocki * metadata from and writing them to the list of page backup entries 95fb13a28bSRafael J. Wysocki * (PBEs) which is the main data structure of swsusp. 96fb13a28bSRafael J. Wysocki * 97fb13a28bSRafael J. Wysocki * Using struct snapshot_handle we can transfer the image, including its 98fb13a28bSRafael J. Wysocki * metadata, as a continuous sequence of bytes with the help of 99fb13a28bSRafael J. Wysocki * snapshot_read_next() and snapshot_write_next(). 100fb13a28bSRafael J. Wysocki * 101fb13a28bSRafael J. Wysocki * The code that writes the image to a storage or transfers it to 102fb13a28bSRafael J. Wysocki * the user land is required to use snapshot_read_next() for this 103fb13a28bSRafael J. Wysocki * purpose and it should not make any assumptions regarding the internal 104fb13a28bSRafael J. Wysocki * structure of the image. Similarly, the code that reads the image from 105fb13a28bSRafael J. Wysocki * a storage or transfers it from the user land is required to use 106fb13a28bSRafael J. Wysocki * snapshot_write_next(). 107fb13a28bSRafael J. Wysocki * 108fb13a28bSRafael J. Wysocki * This may allow us to change the internal structure of the image 109fb13a28bSRafael J. Wysocki * in the future with considerably less effort. 110fb13a28bSRafael J. Wysocki */ 111fb13a28bSRafael J. Wysocki 112f577eb30SRafael J. Wysocki struct snapshot_handle { 113fb13a28bSRafael J. Wysocki unsigned int cur; /* number of the block of PAGE_SIZE bytes the 114fb13a28bSRafael J. Wysocki * next operation will refer to (ie. current) 115fb13a28bSRafael J. Wysocki */ 116fb13a28bSRafael J. Wysocki void *buffer; /* address of the block to read from 117fb13a28bSRafael J. Wysocki * or write to 118fb13a28bSRafael J. Wysocki */ 119fb13a28bSRafael J. Wysocki int sync_read; /* Set to one to notify the caller of 120fb13a28bSRafael J. Wysocki * snapshot_write_next() that it may 121fb13a28bSRafael J. Wysocki * need to call wait_on_bio_chain() 122fb13a28bSRafael J. Wysocki */ 123f577eb30SRafael J. Wysocki }; 124f577eb30SRafael J. Wysocki 125fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of 126fb13a28bSRafael J. Wysocki * snapshot_read_next()/snapshot_write_next() is allowed to 127fb13a28bSRafael J. Wysocki * read/write data after the function returns 128fb13a28bSRafael J. Wysocki */ 129d3c1b24cSJiri Slaby #define data_of(handle) ((handle).buffer) 130f577eb30SRafael J. Wysocki 131b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone); 132af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void); 133d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle); 134d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle); 1358357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle); 136b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle); 13761159a31SRafael J. Wysocki 1380709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */ 1390709db60SRafael J. Wysocki extern atomic_t snapshot_device_available; 1400709db60SRafael J. Wysocki 141d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap); 142d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap); 143d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void); 14461159a31SRafael J. Wysocki 145a634cc10SRafael J. Wysocki /* 146a634cc10SRafael J. Wysocki * Flags that can be passed from the hibernatig hernel to the "boot" kernel in 147a634cc10SRafael J. Wysocki * the image header. 148a634cc10SRafael J. Wysocki */ 149a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE 1 150f996fc96SBojan Smojver #define SF_NOCOMPRESS_MODE 2 151081a9d04SBojan Smojver #define SF_CRC32_MODE 4 152a634cc10SRafael J. Wysocki 1538b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */ 15474c7e2efSRandy Dunlap extern int swsusp_check(void); 15574c7e2efSRandy Dunlap extern void swsusp_free(void); 156a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p); 157a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags); 158c2dd0daeSAl Viro extern void swsusp_close(fmode_t); 1590d3a9abeSRafael J. Wysocki 1608a0d613fSJiri Slaby /* kernel/power/block_io.c */ 1618a0d613fSJiri Slaby extern struct block_device *hib_resume_bdev; 1628a0d613fSJiri Slaby 1638a0d613fSJiri Slaby extern int hib_bio_read_page(pgoff_t page_off, void *addr, 1648a0d613fSJiri Slaby struct bio **bio_chain); 1658a0d613fSJiri Slaby extern int hib_bio_write_page(pgoff_t page_off, void *addr, 1668a0d613fSJiri Slaby struct bio **bio_chain); 1678a0d613fSJiri Slaby extern int hib_wait_on_bio_chain(struct bio **bio_chain); 1688a0d613fSJiri Slaby 1690d3a9abeSRafael J. Wysocki struct timeval; 170b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */ 1710d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1720d3a9abeSRafael J. Wysocki unsigned int, char *); 173b10d9117SRafael J. Wysocki 174296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND 175a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */ 176a9d70523SRafael J. Wysocki extern const char *const pm_states[]; 177a9d70523SRafael J. Wysocki 178a9d70523SRafael J. Wysocki extern bool valid_state(suspend_state_t state); 1796c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state); 180296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */ 181296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state) 182296699deSRafael J. Wysocki { 183296699deSRafael J. Wysocki return -ENOSYS; 184296699deSRafael J. Wysocki } 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 { 232379e0be8SSrivatsa S. Bhat int error; 233379e0be8SSrivatsa S. Bhat 234379e0be8SSrivatsa S. Bhat error = freeze_processes(); 235379e0be8SSrivatsa S. Bhat /* 236379e0be8SSrivatsa S. Bhat * freeze_processes() automatically thaws every task if freezing 237379e0be8SSrivatsa S. Bhat * fails. So we need not do anything extra upon error. 238379e0be8SSrivatsa S. Bhat */ 239379e0be8SSrivatsa S. Bhat if (error) 2406f585f75SRafael J. Wysocki return error; 241379e0be8SSrivatsa S. Bhat 242379e0be8SSrivatsa S. Bhat error = freeze_kernel_threads(); 243379e0be8SSrivatsa S. Bhat /* 244379e0be8SSrivatsa S. Bhat * freeze_kernel_threads() thaws only kernel threads upon freezing 245379e0be8SSrivatsa S. Bhat * failure. So we have to thaw the userspace tasks ourselves. 246379e0be8SSrivatsa S. Bhat */ 247379e0be8SSrivatsa S. Bhat if (error) 248379e0be8SSrivatsa S. Bhat thaw_processes(); 249379e0be8SSrivatsa S. Bhat 250379e0be8SSrivatsa S. Bhat return error; 251b28f5081SJohannes Berg } 252b28f5081SJohannes Berg 253b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 254b28f5081SJohannes Berg { 255b28f5081SJohannes Berg thaw_processes(); 256b28f5081SJohannes Berg } 257b28f5081SJohannes Berg #else 258b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 259b28f5081SJohannes Berg { 260b28f5081SJohannes Berg return 0; 261b28f5081SJohannes Berg } 262b28f5081SJohannes Berg 263b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 264b28f5081SJohannes Berg { 265b28f5081SJohannes Berg } 266b28f5081SJohannes Berg #endif 267*7483b4a4SRafael J. Wysocki 268*7483b4a4SRafael J. Wysocki #ifdef CONFIG_PM_AUTOSLEEP 269*7483b4a4SRafael J. Wysocki 270*7483b4a4SRafael J. Wysocki /* kernel/power/autosleep.c */ 271*7483b4a4SRafael J. Wysocki extern int pm_autosleep_init(void); 272*7483b4a4SRafael J. Wysocki extern int pm_autosleep_lock(void); 273*7483b4a4SRafael J. Wysocki extern void pm_autosleep_unlock(void); 274*7483b4a4SRafael J. Wysocki extern suspend_state_t pm_autosleep_state(void); 275*7483b4a4SRafael J. Wysocki extern int pm_autosleep_set_state(suspend_state_t state); 276*7483b4a4SRafael J. Wysocki 277*7483b4a4SRafael J. Wysocki #else /* !CONFIG_PM_AUTOSLEEP */ 278*7483b4a4SRafael J. Wysocki 279*7483b4a4SRafael J. Wysocki static inline int pm_autosleep_init(void) { return 0; } 280*7483b4a4SRafael J. Wysocki static inline int pm_autosleep_lock(void) { return 0; } 281*7483b4a4SRafael J. Wysocki static inline void pm_autosleep_unlock(void) {} 282*7483b4a4SRafael J. Wysocki static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; } 283*7483b4a4SRafael J. Wysocki 284*7483b4a4SRafael J. Wysocki #endif /* !CONFIG_PM_AUTOSLEEP */ 285