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 14b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 15d307c4a8SRafael J. Wysocki #ifdef CONFIG_ARCH_HIBERNATION_HEADER 16d307c4a8SRafael J. Wysocki /* Maximum size of architecture specific data in a hibernation header */ 17d307c4a8SRafael J. Wysocki #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) 18d307c4a8SRafael J. Wysocki 19d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_save(void *addr, unsigned int max_size); 20d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_restore(void *addr); 21d307c4a8SRafael J. Wysocki 22d307c4a8SRafael J. Wysocki static inline int init_header_complete(struct swsusp_info *info) 23d307c4a8SRafael J. Wysocki { 24d307c4a8SRafael J. Wysocki return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE); 25d307c4a8SRafael J. Wysocki } 26d307c4a8SRafael J. Wysocki 27d307c4a8SRafael J. Wysocki static inline char *check_image_kernel(struct swsusp_info *info) 28d307c4a8SRafael J. Wysocki { 29d307c4a8SRafael J. Wysocki return arch_hibernation_header_restore(info) ? 30d307c4a8SRafael J. Wysocki "architecture specific data" : NULL; 31d307c4a8SRafael J. Wysocki } 32d307c4a8SRafael J. Wysocki #endif /* CONFIG_ARCH_HIBERNATION_HEADER */ 33d307c4a8SRafael J. Wysocki 3456f99bcbSRafael J. Wysocki /* 3556f99bcbSRafael J. Wysocki * Keep some memory free so that I/O operations can succeed without paging 3656f99bcbSRafael J. Wysocki * [Might this be more than 4 MB?] 3756f99bcbSRafael J. Wysocki */ 3856f99bcbSRafael J. Wysocki #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) 39d307c4a8SRafael J. Wysocki 4056f99bcbSRafael J. Wysocki /* 4156f99bcbSRafael J. Wysocki * Keep 1 MB of memory free so that device drivers can allocate some pages in 4256f99bcbSRafael J. Wysocki * their .suspend() routines without breaking the suspend to disk. 4356f99bcbSRafael J. Wysocki */ 4456f99bcbSRafael J. Wysocki #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT) 451da177e4SLinus Torvalds 467777fab9SRafael J. Wysocki /* kernel/power/disk.c */ 477777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode); 48a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode); 497777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void); 501da177e4SLinus Torvalds #endif 51a6d70980SStephen Hemminger 5249c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long); 5349c3df6aSVivek Goyal 54a6d70980SStephen Hemminger extern struct mutex pm_mutex; 55a6d70980SStephen Hemminger 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); 7772a97e08SRafael J. Wysocki extern unsigned int count_data_pages(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); 131*af508b34SRafael 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 13737b2ba12SRafael J. Wysocki /* 13837b2ba12SRafael J. Wysocki * This structure is used to pass the values needed for the identification 13937b2ba12SRafael J. Wysocki * of the resume swap area from a user space to the kernel via the 14037b2ba12SRafael J. Wysocki * SNAPSHOT_SET_SWAP_AREA ioctl 14137b2ba12SRafael J. Wysocki */ 14237b2ba12SRafael J. Wysocki struct resume_swap_area { 14337b2ba12SRafael J. Wysocki loff_t offset; 14437b2ba12SRafael J. Wysocki u_int32_t dev; 14537b2ba12SRafael J. Wysocki } __attribute__((packed)); 14637b2ba12SRafael J. Wysocki 1476e1819d6SRafael J. Wysocki #define SNAPSHOT_IOC_MAGIC '3' 1486e1819d6SRafael J. Wysocki #define SNAPSHOT_FREEZE _IO(SNAPSHOT_IOC_MAGIC, 1) 1496e1819d6SRafael J. Wysocki #define SNAPSHOT_UNFREEZE _IO(SNAPSHOT_IOC_MAGIC, 2) 1506e1819d6SRafael J. Wysocki #define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *) 1516e1819d6SRafael J. Wysocki #define SNAPSHOT_ATOMIC_RESTORE _IO(SNAPSHOT_IOC_MAGIC, 4) 1526e1819d6SRafael J. Wysocki #define SNAPSHOT_FREE _IO(SNAPSHOT_IOC_MAGIC, 5) 1536e1819d6SRafael J. Wysocki #define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long) 1546e1819d6SRafael J. Wysocki #define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *) 1556e1819d6SRafael J. Wysocki #define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *) 1566e1819d6SRafael J. Wysocki #define SNAPSHOT_FREE_SWAP_PAGES _IO(SNAPSHOT_IOC_MAGIC, 9) 1576e1819d6SRafael J. Wysocki #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int) 1589b238205SLuca Tettamanti #define SNAPSHOT_S2RAM _IO(SNAPSHOT_IOC_MAGIC, 11) 1593592695cSStefan Seyfried #define SNAPSHOT_PMOPS _IOW(SNAPSHOT_IOC_MAGIC, 12, unsigned int) 16037b2ba12SRafael J. Wysocki #define SNAPSHOT_SET_SWAP_AREA _IOW(SNAPSHOT_IOC_MAGIC, 13, \ 16137b2ba12SRafael J. Wysocki struct resume_swap_area) 162*af508b34SRafael J. Wysocki #define SNAPSHOT_GET_IMAGE_SIZE _IOR(SNAPSHOT_IOC_MAGIC, 14, loff_t) 163*af508b34SRafael J. Wysocki #define SNAPSHOT_IOC_MAXNR 14 1643592695cSStefan Seyfried 1653592695cSStefan Seyfried #define PMOPS_PREPARE 1 1663592695cSStefan Seyfried #define PMOPS_ENTER 2 1673592695cSStefan Seyfried #define PMOPS_FINISH 3 1686e1819d6SRafael J. Wysocki 1690709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */ 1700709db60SRafael J. Wysocki extern atomic_t snapshot_device_available; 1710709db60SRafael J. Wysocki 172d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap); 173d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap); 174d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void); 17561159a31SRafael J. Wysocki 176a634cc10SRafael J. Wysocki /* 177a634cc10SRafael J. Wysocki * Flags that can be passed from the hibernatig hernel to the "boot" kernel in 178a634cc10SRafael J. Wysocki * the image header. 179a634cc10SRafael J. Wysocki */ 180a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE 1 181a634cc10SRafael J. Wysocki 182a634cc10SRafael J. Wysocki /* kernel/power/disk.c */ 18374c7e2efSRandy Dunlap extern int swsusp_check(void); 18461159a31SRafael J. Wysocki extern int swsusp_shrink_memory(void); 18574c7e2efSRandy Dunlap extern void swsusp_free(void); 18661159a31SRafael J. Wysocki extern int swsusp_resume(void); 187a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p); 188a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags); 18974c7e2efSRandy Dunlap extern void swsusp_close(void); 1900d3a9abeSRafael J. Wysocki 1910d3a9abeSRafael J. Wysocki struct timeval; 192b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */ 1930d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *, 1940d3a9abeSRafael J. Wysocki unsigned int, char *); 195b10d9117SRafael J. Wysocki 196296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND 197b10d9117SRafael J. Wysocki /* kernel/power/main.c */ 1986c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state); 199296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */ 200296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state) 201296699deSRafael J. Wysocki { 202296699deSRafael J. Wysocki return -ENOSYS; 203296699deSRafael J. Wysocki } 204296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */ 205296699deSRafael J. Wysocki 206296699deSRafael J. Wysocki /* kernel/power/common.c */ 207b10d9117SRafael J. Wysocki extern struct blocking_notifier_head pm_chain_head; 208b10d9117SRafael J. Wysocki 209b10d9117SRafael J. Wysocki static inline int pm_notifier_call_chain(unsigned long val) 210b10d9117SRafael J. Wysocki { 211b10d9117SRafael J. Wysocki return (blocking_notifier_call_chain(&pm_chain_head, val, NULL) 212b10d9117SRafael J. Wysocki == NOTIFY_BAD) ? -EINVAL : 0; 213b10d9117SRafael J. Wysocki } 214