11da177e4SLinus Torvalds #include <linux/suspend.h> 21da177e4SLinus Torvalds #include <linux/utsname.h> 31da177e4SLinus Torvalds 41da177e4SLinus Torvalds struct swsusp_info { 51da177e4SLinus Torvalds struct new_utsname uts; 61da177e4SLinus Torvalds u32 version_code; 71da177e4SLinus Torvalds unsigned long num_physpages; 81da177e4SLinus Torvalds int cpus; 91da177e4SLinus Torvalds unsigned long image_pages; 107088a5c0SRafael J. Wysocki unsigned long pages; 116e1819d6SRafael J. Wysocki unsigned long size; 121da177e4SLinus Torvalds } __attribute__((aligned(PAGE_SIZE))); 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #ifdef CONFIG_SOFTWARE_SUSPEND 17*56f99bcbSRafael J. Wysocki /* 18*56f99bcbSRafael J. Wysocki * Keep some memory free so that I/O operations can succeed without paging 19*56f99bcbSRafael J. Wysocki * [Might this be more than 4 MB?] 20*56f99bcbSRafael J. Wysocki */ 21*56f99bcbSRafael J. Wysocki #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) 22*56f99bcbSRafael J. Wysocki /* 23*56f99bcbSRafael J. Wysocki * Keep 1 MB of memory free so that device drivers can allocate some pages in 24*56f99bcbSRafael J. Wysocki * their .suspend() routines without breaking the suspend to disk. 25*56f99bcbSRafael J. Wysocki */ 26*56f99bcbSRafael J. Wysocki #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT) 271da177e4SLinus Torvalds 28*56f99bcbSRafael J. Wysocki extern int pm_suspend_disk(void); 291da177e4SLinus Torvalds #else 301da177e4SLinus Torvalds static inline int pm_suspend_disk(void) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds return -EPERM; 331da177e4SLinus Torvalds } 341da177e4SLinus Torvalds #endif 35a6d70980SStephen Hemminger 3649c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long); 3749c3df6aSVivek Goyal 38a6d70980SStephen Hemminger extern struct mutex pm_mutex; 39a6d70980SStephen Hemminger 401da177e4SLinus Torvalds #define power_attr(_name) \ 411da177e4SLinus Torvalds static struct subsys_attribute _name##_attr = { \ 421da177e4SLinus Torvalds .attr = { \ 431da177e4SLinus Torvalds .name = __stringify(_name), \ 441da177e4SLinus Torvalds .mode = 0644, \ 451da177e4SLinus Torvalds }, \ 461da177e4SLinus Torvalds .show = _name##_show, \ 471da177e4SLinus Torvalds .store = _name##_store, \ 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 50823bccfcSGreg Kroah-Hartman extern struct kset power_subsys; 511da177e4SLinus Torvalds 52853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */ 53853609b6SRafael J. Wysocki extern unsigned long image_size; 54f577eb30SRafael J. Wysocki extern int in_suspend; 5561159a31SRafael J. Wysocki extern dev_t swsusp_resume_device; 569a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block; 57f577eb30SRafael J. Wysocki 5825761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void); 5925761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void); 6025761b6eSRafael J. Wysocki 6174dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void); 6274dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void); 6372a97e08SRafael J. Wysocki extern unsigned int count_data_pages(void); 64f577eb30SRafael J. Wysocki 65fb13a28bSRafael J. Wysocki /** 66fb13a28bSRafael J. Wysocki * Auxiliary structure used for reading the snapshot image data and 67fb13a28bSRafael J. Wysocki * metadata from and writing them to the list of page backup entries 68fb13a28bSRafael J. Wysocki * (PBEs) which is the main data structure of swsusp. 69fb13a28bSRafael J. Wysocki * 70fb13a28bSRafael J. Wysocki * Using struct snapshot_handle we can transfer the image, including its 71fb13a28bSRafael J. Wysocki * metadata, as a continuous sequence of bytes with the help of 72fb13a28bSRafael J. Wysocki * snapshot_read_next() and snapshot_write_next(). 73fb13a28bSRafael J. Wysocki * 74fb13a28bSRafael J. Wysocki * The code that writes the image to a storage or transfers it to 75fb13a28bSRafael J. Wysocki * the user land is required to use snapshot_read_next() for this 76fb13a28bSRafael J. Wysocki * purpose and it should not make any assumptions regarding the internal 77fb13a28bSRafael J. Wysocki * structure of the image. Similarly, the code that reads the image from 78fb13a28bSRafael J. Wysocki * a storage or transfers it from the user land is required to use 79fb13a28bSRafael J. Wysocki * snapshot_write_next(). 80fb13a28bSRafael J. Wysocki * 81fb13a28bSRafael J. Wysocki * This may allow us to change the internal structure of the image 82fb13a28bSRafael J. Wysocki * in the future with considerably less effort. 83fb13a28bSRafael J. Wysocki */ 84fb13a28bSRafael J. Wysocki 85f577eb30SRafael J. Wysocki struct snapshot_handle { 86fb13a28bSRafael J. Wysocki loff_t offset; /* number of the last byte ready for reading 87fb13a28bSRafael J. Wysocki * or writing in the sequence 88fb13a28bSRafael J. Wysocki */ 89fb13a28bSRafael J. Wysocki unsigned int cur; /* number of the block of PAGE_SIZE bytes the 90fb13a28bSRafael J. Wysocki * next operation will refer to (ie. current) 91fb13a28bSRafael J. Wysocki */ 92fb13a28bSRafael J. Wysocki unsigned int cur_offset; /* offset with respect to the current 93fb13a28bSRafael J. Wysocki * block (for the next operation) 94fb13a28bSRafael J. Wysocki */ 95fb13a28bSRafael J. Wysocki unsigned int prev; /* number of the block of PAGE_SIZE bytes that 96fb13a28bSRafael J. Wysocki * was the current one previously 97fb13a28bSRafael J. Wysocki */ 98fb13a28bSRafael J. Wysocki void *buffer; /* address of the block to read from 99fb13a28bSRafael J. Wysocki * or write to 100fb13a28bSRafael J. Wysocki */ 101fb13a28bSRafael J. Wysocki unsigned int buf_offset; /* location to read from or write to, 102fb13a28bSRafael J. Wysocki * given as a displacement from 'buffer' 103fb13a28bSRafael J. Wysocki */ 104fb13a28bSRafael J. Wysocki int sync_read; /* Set to one to notify the caller of 105fb13a28bSRafael J. Wysocki * snapshot_write_next() that it may 106fb13a28bSRafael J. Wysocki * need to call wait_on_bio_chain() 107fb13a28bSRafael J. Wysocki */ 108f577eb30SRafael J. Wysocki }; 109f577eb30SRafael J. Wysocki 110fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of 111fb13a28bSRafael J. Wysocki * snapshot_read_next()/snapshot_write_next() is allowed to 112fb13a28bSRafael J. Wysocki * read/write data after the function returns 113fb13a28bSRafael J. Wysocki */ 114f577eb30SRafael J. Wysocki #define data_of(handle) ((handle).buffer + (handle).buf_offset) 115f577eb30SRafael J. Wysocki 116b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone); 117f577eb30SRafael J. Wysocki extern int snapshot_read_next(struct snapshot_handle *handle, size_t count); 118f577eb30SRafael J. Wysocki extern int snapshot_write_next(struct snapshot_handle *handle, size_t count); 1198357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle); 120b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle); 12161159a31SRafael J. Wysocki 12237b2ba12SRafael J. Wysocki /* 12337b2ba12SRafael J. Wysocki * This structure is used to pass the values needed for the identification 12437b2ba12SRafael J. Wysocki * of the resume swap area from a user space to the kernel via the 12537b2ba12SRafael J. Wysocki * SNAPSHOT_SET_SWAP_AREA ioctl 12637b2ba12SRafael J. Wysocki */ 12737b2ba12SRafael J. Wysocki struct resume_swap_area { 12837b2ba12SRafael J. Wysocki loff_t offset; 12937b2ba12SRafael J. Wysocki u_int32_t dev; 13037b2ba12SRafael J. Wysocki } __attribute__((packed)); 13137b2ba12SRafael J. Wysocki 1326e1819d6SRafael J. Wysocki #define SNAPSHOT_IOC_MAGIC '3' 1336e1819d6SRafael J. Wysocki #define SNAPSHOT_FREEZE _IO(SNAPSHOT_IOC_MAGIC, 1) 1346e1819d6SRafael J. Wysocki #define SNAPSHOT_UNFREEZE _IO(SNAPSHOT_IOC_MAGIC, 2) 1356e1819d6SRafael J. Wysocki #define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *) 1366e1819d6SRafael J. Wysocki #define SNAPSHOT_ATOMIC_RESTORE _IO(SNAPSHOT_IOC_MAGIC, 4) 1376e1819d6SRafael J. Wysocki #define SNAPSHOT_FREE _IO(SNAPSHOT_IOC_MAGIC, 5) 1386e1819d6SRafael J. Wysocki #define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long) 1396e1819d6SRafael J. Wysocki #define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *) 1406e1819d6SRafael J. Wysocki #define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *) 1416e1819d6SRafael J. Wysocki #define SNAPSHOT_FREE_SWAP_PAGES _IO(SNAPSHOT_IOC_MAGIC, 9) 1426e1819d6SRafael J. Wysocki #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int) 1439b238205SLuca Tettamanti #define SNAPSHOT_S2RAM _IO(SNAPSHOT_IOC_MAGIC, 11) 1443592695cSStefan Seyfried #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int) 14537b2ba12SRafael J. Wysocki #define SNAPSHOT_SET_SWAP_AREA _IOW(SNAPSHOT_IOC_MAGIC, 13, \ 14637b2ba12SRafael J. Wysocki struct resume_swap_area) 14737b2ba12SRafael J. Wysocki #define SNAPSHOT_IOC_MAXNR 13 1483592695cSStefan Seyfried 1493592695cSStefan Seyfried #define PMOPS_PREPARE 1 1503592695cSStefan Seyfried #define PMOPS_ENTER 2 1513592695cSStefan Seyfried #define PMOPS_FINISH 3 1526e1819d6SRafael J. Wysocki 1530709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */ 1540709db60SRafael J. Wysocki extern atomic_t snapshot_device_available; 1550709db60SRafael J. Wysocki 156d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap); 157d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap); 158d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void); 15961159a31SRafael J. Wysocki 16074c7e2efSRandy Dunlap extern int swsusp_check(void); 16161159a31SRafael J. Wysocki extern int swsusp_shrink_memory(void); 16274c7e2efSRandy Dunlap extern void swsusp_free(void); 16361159a31SRafael J. Wysocki extern int swsusp_suspend(void); 16461159a31SRafael J. Wysocki extern int swsusp_resume(void); 16574c7e2efSRandy Dunlap extern int swsusp_read(void); 16674c7e2efSRandy Dunlap extern int swsusp_write(void); 16774c7e2efSRandy Dunlap extern void swsusp_close(void); 1689b238205SLuca Tettamanti extern int suspend_enter(suspend_state_t state); 1690d3a9abeSRafael J. Wysocki 1700d3a9abeSRafael J. Wysocki struct timeval; 1710d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1720d3a9abeSRafael J. Wysocki unsigned int, char *); 173