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 1756f99bcbSRafael J. Wysocki /* 1856f99bcbSRafael J. Wysocki * Keep some memory free so that I/O operations can succeed without paging 1956f99bcbSRafael J. Wysocki * [Might this be more than 4 MB?] 2056f99bcbSRafael J. Wysocki */ 2156f99bcbSRafael J. Wysocki #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) 2256f99bcbSRafael J. Wysocki /* 2356f99bcbSRafael J. Wysocki * Keep 1 MB of memory free so that device drivers can allocate some pages in 2456f99bcbSRafael J. Wysocki * their .suspend() routines without breaking the suspend to disk. 2556f99bcbSRafael J. Wysocki */ 2656f99bcbSRafael J. Wysocki #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT) 271da177e4SLinus Torvalds 287777fab9SRafael J. Wysocki /* kernel/power/disk.c */ 297777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode); 30a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode); 317777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void); 321da177e4SLinus Torvalds #endif 33a6d70980SStephen Hemminger 3449c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long); 3549c3df6aSVivek Goyal 36a6d70980SStephen Hemminger extern struct mutex pm_mutex; 37a6d70980SStephen Hemminger 381da177e4SLinus Torvalds #define power_attr(_name) \ 391da177e4SLinus Torvalds static struct subsys_attribute _name##_attr = { \ 401da177e4SLinus Torvalds .attr = { \ 411da177e4SLinus Torvalds .name = __stringify(_name), \ 421da177e4SLinus Torvalds .mode = 0644, \ 431da177e4SLinus Torvalds }, \ 441da177e4SLinus Torvalds .show = _name##_show, \ 451da177e4SLinus Torvalds .store = _name##_store, \ 461da177e4SLinus Torvalds } 471da177e4SLinus Torvalds 48823bccfcSGreg Kroah-Hartman extern struct kset power_subsys; 491da177e4SLinus Torvalds 50853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */ 51853609b6SRafael J. Wysocki extern unsigned long image_size; 52f577eb30SRafael J. Wysocki extern int in_suspend; 5361159a31SRafael J. Wysocki extern dev_t swsusp_resume_device; 549a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block; 55f577eb30SRafael J. Wysocki 5625761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void); 5725761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void); 5825761b6eSRafael J. Wysocki 5974dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void); 6074dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void); 6172a97e08SRafael J. Wysocki extern unsigned int count_data_pages(void); 62f577eb30SRafael J. Wysocki 63fb13a28bSRafael J. Wysocki /** 64fb13a28bSRafael J. Wysocki * Auxiliary structure used for reading the snapshot image data and 65fb13a28bSRafael J. Wysocki * metadata from and writing them to the list of page backup entries 66fb13a28bSRafael J. Wysocki * (PBEs) which is the main data structure of swsusp. 67fb13a28bSRafael J. Wysocki * 68fb13a28bSRafael J. Wysocki * Using struct snapshot_handle we can transfer the image, including its 69fb13a28bSRafael J. Wysocki * metadata, as a continuous sequence of bytes with the help of 70fb13a28bSRafael J. Wysocki * snapshot_read_next() and snapshot_write_next(). 71fb13a28bSRafael J. Wysocki * 72fb13a28bSRafael J. Wysocki * The code that writes the image to a storage or transfers it to 73fb13a28bSRafael J. Wysocki * the user land is required to use snapshot_read_next() for this 74fb13a28bSRafael J. Wysocki * purpose and it should not make any assumptions regarding the internal 75fb13a28bSRafael J. Wysocki * structure of the image. Similarly, the code that reads the image from 76fb13a28bSRafael J. Wysocki * a storage or transfers it from the user land is required to use 77fb13a28bSRafael J. Wysocki * snapshot_write_next(). 78fb13a28bSRafael J. Wysocki * 79fb13a28bSRafael J. Wysocki * This may allow us to change the internal structure of the image 80fb13a28bSRafael J. Wysocki * in the future with considerably less effort. 81fb13a28bSRafael J. Wysocki */ 82fb13a28bSRafael J. Wysocki 83f577eb30SRafael J. Wysocki struct snapshot_handle { 84fb13a28bSRafael J. Wysocki loff_t offset; /* number of the last byte ready for reading 85fb13a28bSRafael J. Wysocki * or writing in the sequence 86fb13a28bSRafael J. Wysocki */ 87fb13a28bSRafael J. Wysocki unsigned int cur; /* number of the block of PAGE_SIZE bytes the 88fb13a28bSRafael J. Wysocki * next operation will refer to (ie. current) 89fb13a28bSRafael J. Wysocki */ 90fb13a28bSRafael J. Wysocki unsigned int cur_offset; /* offset with respect to the current 91fb13a28bSRafael J. Wysocki * block (for the next operation) 92fb13a28bSRafael J. Wysocki */ 93fb13a28bSRafael J. Wysocki unsigned int prev; /* number of the block of PAGE_SIZE bytes that 94fb13a28bSRafael J. Wysocki * was the current one previously 95fb13a28bSRafael J. Wysocki */ 96fb13a28bSRafael J. Wysocki void *buffer; /* address of the block to read from 97fb13a28bSRafael J. Wysocki * or write to 98fb13a28bSRafael J. Wysocki */ 99fb13a28bSRafael J. Wysocki unsigned int buf_offset; /* location to read from or write to, 100fb13a28bSRafael J. Wysocki * given as a displacement from 'buffer' 101fb13a28bSRafael J. Wysocki */ 102fb13a28bSRafael J. Wysocki int sync_read; /* Set to one to notify the caller of 103fb13a28bSRafael J. Wysocki * snapshot_write_next() that it may 104fb13a28bSRafael J. Wysocki * need to call wait_on_bio_chain() 105fb13a28bSRafael J. Wysocki */ 106f577eb30SRafael J. Wysocki }; 107f577eb30SRafael J. Wysocki 108fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of 109fb13a28bSRafael J. Wysocki * snapshot_read_next()/snapshot_write_next() is allowed to 110fb13a28bSRafael J. Wysocki * read/write data after the function returns 111fb13a28bSRafael J. Wysocki */ 112f577eb30SRafael J. Wysocki #define data_of(handle) ((handle).buffer + (handle).buf_offset) 113f577eb30SRafael J. Wysocki 114b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone); 115f577eb30SRafael J. Wysocki extern int snapshot_read_next(struct snapshot_handle *handle, size_t count); 116f577eb30SRafael J. Wysocki extern int snapshot_write_next(struct snapshot_handle *handle, size_t count); 1178357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle); 118b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle); 11961159a31SRafael J. Wysocki 12037b2ba12SRafael J. Wysocki /* 12137b2ba12SRafael J. Wysocki * This structure is used to pass the values needed for the identification 12237b2ba12SRafael J. Wysocki * of the resume swap area from a user space to the kernel via the 12337b2ba12SRafael J. Wysocki * SNAPSHOT_SET_SWAP_AREA ioctl 12437b2ba12SRafael J. Wysocki */ 12537b2ba12SRafael J. Wysocki struct resume_swap_area { 12637b2ba12SRafael J. Wysocki loff_t offset; 12737b2ba12SRafael J. Wysocki u_int32_t dev; 12837b2ba12SRafael J. Wysocki } __attribute__((packed)); 12937b2ba12SRafael J. Wysocki 1306e1819d6SRafael J. Wysocki #define SNAPSHOT_IOC_MAGIC '3' 1316e1819d6SRafael J. Wysocki #define SNAPSHOT_FREEZE _IO(SNAPSHOT_IOC_MAGIC, 1) 1326e1819d6SRafael J. Wysocki #define SNAPSHOT_UNFREEZE _IO(SNAPSHOT_IOC_MAGIC, 2) 1336e1819d6SRafael J. Wysocki #define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *) 1346e1819d6SRafael J. Wysocki #define SNAPSHOT_ATOMIC_RESTORE _IO(SNAPSHOT_IOC_MAGIC, 4) 1356e1819d6SRafael J. Wysocki #define SNAPSHOT_FREE _IO(SNAPSHOT_IOC_MAGIC, 5) 1366e1819d6SRafael J. Wysocki #define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long) 1376e1819d6SRafael J. Wysocki #define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *) 1386e1819d6SRafael J. Wysocki #define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *) 1396e1819d6SRafael J. Wysocki #define SNAPSHOT_FREE_SWAP_PAGES _IO(SNAPSHOT_IOC_MAGIC, 9) 1406e1819d6SRafael J. Wysocki #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int) 1419b238205SLuca Tettamanti #define SNAPSHOT_S2RAM _IO(SNAPSHOT_IOC_MAGIC, 11) 1423592695cSStefan Seyfried #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int) 14337b2ba12SRafael J. Wysocki #define SNAPSHOT_SET_SWAP_AREA _IOW(SNAPSHOT_IOC_MAGIC, 13, \ 14437b2ba12SRafael J. Wysocki struct resume_swap_area) 14537b2ba12SRafael J. Wysocki #define SNAPSHOT_IOC_MAXNR 13 1463592695cSStefan Seyfried 1473592695cSStefan Seyfried #define PMOPS_PREPARE 1 1483592695cSStefan Seyfried #define PMOPS_ENTER 2 1493592695cSStefan Seyfried #define PMOPS_FINISH 3 1506e1819d6SRafael J. Wysocki 1510709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */ 1520709db60SRafael J. Wysocki extern atomic_t snapshot_device_available; 1530709db60SRafael J. Wysocki 154d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap); 155d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap); 156d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void); 15761159a31SRafael J. Wysocki 158a634cc10SRafael J. Wysocki /* 159a634cc10SRafael J. Wysocki * Flags that can be passed from the hibernatig hernel to the "boot" kernel in 160a634cc10SRafael J. Wysocki * the image header. 161a634cc10SRafael J. Wysocki */ 162a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE 1 163a634cc10SRafael J. Wysocki 164a634cc10SRafael J. Wysocki /* kernel/power/disk.c */ 16574c7e2efSRandy Dunlap extern int swsusp_check(void); 16661159a31SRafael J. Wysocki extern int swsusp_shrink_memory(void); 16774c7e2efSRandy Dunlap extern void swsusp_free(void); 16861159a31SRafael J. Wysocki extern int swsusp_suspend(void); 16961159a31SRafael J. Wysocki extern int swsusp_resume(void); 170a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p); 171a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags); 17274c7e2efSRandy Dunlap extern void swsusp_close(void); 1739b238205SLuca Tettamanti extern int suspend_enter(suspend_state_t state); 1740d3a9abeSRafael J. Wysocki 1750d3a9abeSRafael J. Wysocki struct timeval; 176*b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */ 1770d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1780d3a9abeSRafael J. Wysocki unsigned int, char *); 179*b10d9117SRafael J. Wysocki 180*b10d9117SRafael J. Wysocki /* kernel/power/main.c */ 181*b10d9117SRafael J. Wysocki extern struct blocking_notifier_head pm_chain_head; 182*b10d9117SRafael J. Wysocki 183*b10d9117SRafael J. Wysocki static inline int pm_notifier_call_chain(unsigned long val) 184*b10d9117SRafael J. Wysocki { 185*b10d9117SRafael J. Wysocki return (blocking_notifier_call_chain(&pm_chain_head, val, NULL) 186*b10d9117SRafael J. Wysocki == NOTIFY_BAD) ? -EINVAL : 0; 187*b10d9117SRafael J. Wysocki } 188