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 52*04b73469SRashika Kheria asmlinkage int swsusp_save(void); 53*04b73469SRashika Kheria 548b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */ 5597819a26SSrivatsa S. Bhat extern bool freezer_test_done; 5697819a26SSrivatsa S. Bhat 577777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode); 58a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode); 597777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void); 60ac5c24ecSRafael J. Wysocki 61ac5c24ecSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */ 62ac5c24ecSRafael J. Wysocki 63ddeb6487SRafael J. Wysocki static inline void hibernate_reserved_size_init(void) {} 64ac5c24ecSRafael J. Wysocki static inline void hibernate_image_size_init(void) {} 65ac5c24ecSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */ 66a6d70980SStephen Hemminger 6749c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long); 6849c3df6aSVivek Goyal 691da177e4SLinus Torvalds #define power_attr(_name) \ 70386f275fSKay Sievers static struct kobj_attribute _name##_attr = { \ 711da177e4SLinus Torvalds .attr = { \ 721da177e4SLinus Torvalds .name = __stringify(_name), \ 731da177e4SLinus Torvalds .mode = 0644, \ 741da177e4SLinus Torvalds }, \ 751da177e4SLinus Torvalds .show = _name##_show, \ 761da177e4SLinus Torvalds .store = _name##_store, \ 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 79853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */ 80853609b6SRafael J. Wysocki extern unsigned long image_size; 81ddeb6487SRafael J. Wysocki /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */ 82ddeb6487SRafael J. Wysocki extern unsigned long reserved_size; 83f577eb30SRafael J. Wysocki extern int in_suspend; 8461159a31SRafael J. Wysocki extern dev_t swsusp_resume_device; 859a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block; 86f577eb30SRafael J. Wysocki 8725761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void); 8825761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void); 8925761b6eSRafael J. Wysocki 9074dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void); 9174dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void); 9264a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void); 93f577eb30SRafael J. Wysocki 94fb13a28bSRafael J. Wysocki /** 95fb13a28bSRafael J. Wysocki * Auxiliary structure used for reading the snapshot image data and 96fb13a28bSRafael J. Wysocki * metadata from and writing them to the list of page backup entries 97fb13a28bSRafael J. Wysocki * (PBEs) which is the main data structure of swsusp. 98fb13a28bSRafael J. Wysocki * 99fb13a28bSRafael J. Wysocki * Using struct snapshot_handle we can transfer the image, including its 100fb13a28bSRafael J. Wysocki * metadata, as a continuous sequence of bytes with the help of 101fb13a28bSRafael J. Wysocki * snapshot_read_next() and snapshot_write_next(). 102fb13a28bSRafael J. Wysocki * 103fb13a28bSRafael J. Wysocki * The code that writes the image to a storage or transfers it to 104fb13a28bSRafael J. Wysocki * the user land is required to use snapshot_read_next() for this 105fb13a28bSRafael J. Wysocki * purpose and it should not make any assumptions regarding the internal 106fb13a28bSRafael J. Wysocki * structure of the image. Similarly, the code that reads the image from 107fb13a28bSRafael J. Wysocki * a storage or transfers it from the user land is required to use 108fb13a28bSRafael J. Wysocki * snapshot_write_next(). 109fb13a28bSRafael J. Wysocki * 110fb13a28bSRafael J. Wysocki * This may allow us to change the internal structure of the image 111fb13a28bSRafael J. Wysocki * in the future with considerably less effort. 112fb13a28bSRafael J. Wysocki */ 113fb13a28bSRafael J. Wysocki 114f577eb30SRafael J. Wysocki struct snapshot_handle { 115fb13a28bSRafael J. Wysocki unsigned int cur; /* number of the block of PAGE_SIZE bytes the 116fb13a28bSRafael J. Wysocki * next operation will refer to (ie. current) 117fb13a28bSRafael J. Wysocki */ 118fb13a28bSRafael J. Wysocki void *buffer; /* address of the block to read from 119fb13a28bSRafael J. Wysocki * or write to 120fb13a28bSRafael J. Wysocki */ 121fb13a28bSRafael J. Wysocki int sync_read; /* Set to one to notify the caller of 122fb13a28bSRafael J. Wysocki * snapshot_write_next() that it may 123fb13a28bSRafael J. Wysocki * need to call wait_on_bio_chain() 124fb13a28bSRafael J. Wysocki */ 125f577eb30SRafael J. Wysocki }; 126f577eb30SRafael J. Wysocki 127fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of 128fb13a28bSRafael J. Wysocki * snapshot_read_next()/snapshot_write_next() is allowed to 129fb13a28bSRafael J. Wysocki * read/write data after the function returns 130fb13a28bSRafael J. Wysocki */ 131d3c1b24cSJiri Slaby #define data_of(handle) ((handle).buffer) 132f577eb30SRafael J. Wysocki 133b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone); 134af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void); 135d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle); 136d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle); 1378357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle); 138b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle); 13961159a31SRafael J. Wysocki 1400709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */ 1410709db60SRafael J. Wysocki extern atomic_t snapshot_device_available; 1420709db60SRafael J. Wysocki 143d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap); 144d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap); 145d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void); 14661159a31SRafael J. Wysocki 147a634cc10SRafael J. Wysocki /* 148a634cc10SRafael J. Wysocki * Flags that can be passed from the hibernatig hernel to the "boot" kernel in 149a634cc10SRafael J. Wysocki * the image header. 150a634cc10SRafael J. Wysocki */ 151a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE 1 152f996fc96SBojan Smojver #define SF_NOCOMPRESS_MODE 2 153081a9d04SBojan Smojver #define SF_CRC32_MODE 4 154a634cc10SRafael J. Wysocki 1558b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */ 15674c7e2efSRandy Dunlap extern int swsusp_check(void); 15774c7e2efSRandy Dunlap extern void swsusp_free(void); 158a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p); 159a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags); 160c2dd0daeSAl Viro extern void swsusp_close(fmode_t); 16162c552ccSBojan Smojver #ifdef CONFIG_SUSPEND 16262c552ccSBojan Smojver extern int swsusp_unmark(void); 16362c552ccSBojan Smojver #endif 1640d3a9abeSRafael J. Wysocki 1658a0d613fSJiri Slaby /* kernel/power/block_io.c */ 1668a0d613fSJiri Slaby extern struct block_device *hib_resume_bdev; 1678a0d613fSJiri Slaby 1688a0d613fSJiri Slaby extern int hib_bio_read_page(pgoff_t page_off, void *addr, 1698a0d613fSJiri Slaby struct bio **bio_chain); 1708a0d613fSJiri Slaby extern int hib_bio_write_page(pgoff_t page_off, void *addr, 1718a0d613fSJiri Slaby struct bio **bio_chain); 1728a0d613fSJiri Slaby extern int hib_wait_on_bio_chain(struct bio **bio_chain); 1738a0d613fSJiri Slaby 1740d3a9abeSRafael J. Wysocki struct timeval; 175b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */ 1760d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1770d3a9abeSRafael J. Wysocki unsigned int, char *); 178b10d9117SRafael J. Wysocki 179296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND 180a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */ 181a9d70523SRafael J. Wysocki extern const char *const pm_states[]; 182a9d70523SRafael J. Wysocki 183a9d70523SRafael J. Wysocki extern bool valid_state(suspend_state_t state); 1846c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state); 185296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */ 186296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state) 187296699deSRafael J. Wysocki { 188296699deSRafael J. Wysocki return -ENOSYS; 189296699deSRafael J. Wysocki } 190a9d70523SRafael J. Wysocki static inline bool valid_state(suspend_state_t state) { return false; } 191296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */ 192296699deSRafael J. Wysocki 193a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND 194a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */ 195a9d70523SRafael J. Wysocki extern void suspend_test_start(void); 196a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label); 197a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */ 198a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {} 199a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {} 200a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */ 201a9d70523SRafael J. Wysocki 20282525756SAlan Stern #ifdef CONFIG_PM_SLEEP 20382525756SAlan Stern /* kernel/power/main.c */ 20482525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val); 20582525756SAlan Stern #endif 2062f8ed1c6SAdrian Bunk 2072f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM 2082f8ed1c6SAdrian Bunk int restore_highmem(void); 2092f8ed1c6SAdrian Bunk #else 2102f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; } 2112f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; } 2122f8ed1c6SAdrian Bunk #endif 2130e7d56e3SRafael J. Wysocki 2140e7d56e3SRafael J. Wysocki /* 2150e7d56e3SRafael J. Wysocki * Suspend test levels 2160e7d56e3SRafael J. Wysocki */ 2170e7d56e3SRafael J. Wysocki enum { 2180e7d56e3SRafael J. Wysocki /* keep first */ 2190e7d56e3SRafael J. Wysocki TEST_NONE, 2200e7d56e3SRafael J. Wysocki TEST_CORE, 2210e7d56e3SRafael J. Wysocki TEST_CPUS, 2220e7d56e3SRafael J. Wysocki TEST_PLATFORM, 2230e7d56e3SRafael J. Wysocki TEST_DEVICES, 2240e7d56e3SRafael J. Wysocki TEST_FREEZER, 2250e7d56e3SRafael J. Wysocki /* keep last */ 2260e7d56e3SRafael J. Wysocki __TEST_AFTER_LAST 2270e7d56e3SRafael J. Wysocki }; 2280e7d56e3SRafael J. Wysocki 2290e7d56e3SRafael J. Wysocki #define TEST_FIRST TEST_NONE 2300e7d56e3SRafael J. Wysocki #define TEST_MAX (__TEST_AFTER_LAST - 1) 2314cc79776SRafael J. Wysocki 2324cc79776SRafael J. Wysocki extern int pm_test_level; 233b28f5081SJohannes Berg 234b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER 235b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 236b28f5081SJohannes Berg { 237379e0be8SSrivatsa S. Bhat int error; 238379e0be8SSrivatsa S. Bhat 239379e0be8SSrivatsa S. Bhat error = freeze_processes(); 240379e0be8SSrivatsa S. Bhat /* 241379e0be8SSrivatsa S. Bhat * freeze_processes() automatically thaws every task if freezing 242379e0be8SSrivatsa S. Bhat * fails. So we need not do anything extra upon error. 243379e0be8SSrivatsa S. Bhat */ 244379e0be8SSrivatsa S. Bhat if (error) 2456f585f75SRafael J. Wysocki return error; 246379e0be8SSrivatsa S. Bhat 247379e0be8SSrivatsa S. Bhat error = freeze_kernel_threads(); 248379e0be8SSrivatsa S. Bhat /* 249379e0be8SSrivatsa S. Bhat * freeze_kernel_threads() thaws only kernel threads upon freezing 250379e0be8SSrivatsa S. Bhat * failure. So we have to thaw the userspace tasks ourselves. 251379e0be8SSrivatsa S. Bhat */ 252379e0be8SSrivatsa S. Bhat if (error) 253379e0be8SSrivatsa S. Bhat thaw_processes(); 254379e0be8SSrivatsa S. Bhat 255379e0be8SSrivatsa S. Bhat return error; 256b28f5081SJohannes Berg } 257b28f5081SJohannes Berg 258b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 259b28f5081SJohannes Berg { 260b28f5081SJohannes Berg thaw_processes(); 261b28f5081SJohannes Berg } 262b28f5081SJohannes Berg #else 263b28f5081SJohannes Berg static inline int suspend_freeze_processes(void) 264b28f5081SJohannes Berg { 265b28f5081SJohannes Berg return 0; 266b28f5081SJohannes Berg } 267b28f5081SJohannes Berg 268b28f5081SJohannes Berg static inline void suspend_thaw_processes(void) 269b28f5081SJohannes Berg { 270b28f5081SJohannes Berg } 271b28f5081SJohannes Berg #endif 2727483b4a4SRafael J. Wysocki 2737483b4a4SRafael J. Wysocki #ifdef CONFIG_PM_AUTOSLEEP 2747483b4a4SRafael J. Wysocki 2757483b4a4SRafael J. Wysocki /* kernel/power/autosleep.c */ 2767483b4a4SRafael J. Wysocki extern int pm_autosleep_init(void); 2777483b4a4SRafael J. Wysocki extern int pm_autosleep_lock(void); 2787483b4a4SRafael J. Wysocki extern void pm_autosleep_unlock(void); 2797483b4a4SRafael J. Wysocki extern suspend_state_t pm_autosleep_state(void); 2807483b4a4SRafael J. Wysocki extern int pm_autosleep_set_state(suspend_state_t state); 2817483b4a4SRafael J. Wysocki 2827483b4a4SRafael J. Wysocki #else /* !CONFIG_PM_AUTOSLEEP */ 2837483b4a4SRafael J. Wysocki 2847483b4a4SRafael J. Wysocki static inline int pm_autosleep_init(void) { return 0; } 2857483b4a4SRafael J. Wysocki static inline int pm_autosleep_lock(void) { return 0; } 2867483b4a4SRafael J. Wysocki static inline void pm_autosleep_unlock(void) {} 2877483b4a4SRafael J. Wysocki static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; } 2887483b4a4SRafael J. Wysocki 2897483b4a4SRafael J. Wysocki #endif /* !CONFIG_PM_AUTOSLEEP */ 290b86ff982SRafael J. Wysocki 291b86ff982SRafael J. Wysocki #ifdef CONFIG_PM_WAKELOCKS 292b86ff982SRafael J. Wysocki 293b86ff982SRafael J. Wysocki /* kernel/power/wakelock.c */ 294b86ff982SRafael J. Wysocki extern ssize_t pm_show_wakelocks(char *buf, bool show_active); 295b86ff982SRafael J. Wysocki extern int pm_wake_lock(const char *buf); 296b86ff982SRafael J. Wysocki extern int pm_wake_unlock(const char *buf); 297b86ff982SRafael J. Wysocki 298b86ff982SRafael J. Wysocki #endif /* !CONFIG_PM_WAKELOCKS */ 299