xref: /linux/kernel/power/power.h (revision fb13a28b0f5ada60861868c4fa48a12bd0cb8dea)
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
171da177e4SLinus Torvalds extern int pm_suspend_disk(void);
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #else
201da177e4SLinus Torvalds static inline int pm_suspend_disk(void)
211da177e4SLinus Torvalds {
221da177e4SLinus Torvalds 	return -EPERM;
231da177e4SLinus Torvalds }
241da177e4SLinus Torvalds #endif
251da177e4SLinus Torvalds extern struct semaphore pm_sem;
261da177e4SLinus Torvalds #define power_attr(_name) \
271da177e4SLinus Torvalds static struct subsys_attribute _name##_attr = {	\
281da177e4SLinus Torvalds 	.attr	= {				\
291da177e4SLinus Torvalds 		.name = __stringify(_name),	\
301da177e4SLinus Torvalds 		.mode = 0644,			\
311da177e4SLinus Torvalds 	},					\
321da177e4SLinus Torvalds 	.show	= _name##_show,			\
331da177e4SLinus Torvalds 	.store	= _name##_store,		\
341da177e4SLinus Torvalds }
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds extern struct subsystem power_subsys;
371da177e4SLinus Torvalds 
3825761b6eSRafael J. Wysocki /* References to section boundaries */
3925761b6eSRafael J. Wysocki extern const void __nosave_begin, __nosave_end;
4025761b6eSRafael J. Wysocki 
4172a97e08SRafael J. Wysocki extern struct pbe *pagedir_nosave;
4272a97e08SRafael J. Wysocki 
43853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */
44853609b6SRafael J. Wysocki extern unsigned long image_size;
45f577eb30SRafael J. Wysocki extern int in_suspend;
4661159a31SRafael J. Wysocki extern dev_t swsusp_resume_device;
47f577eb30SRafael J. Wysocki 
4825761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void);
4925761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void);
5025761b6eSRafael J. Wysocki 
5172a97e08SRafael J. Wysocki extern unsigned int count_data_pages(void);
52f577eb30SRafael J. Wysocki 
53*fb13a28bSRafael J. Wysocki /**
54*fb13a28bSRafael J. Wysocki  *	Auxiliary structure used for reading the snapshot image data and
55*fb13a28bSRafael J. Wysocki  *	metadata from and writing them to the list of page backup entries
56*fb13a28bSRafael J. Wysocki  *	(PBEs) which is the main data structure of swsusp.
57*fb13a28bSRafael J. Wysocki  *
58*fb13a28bSRafael J. Wysocki  *	Using struct snapshot_handle we can transfer the image, including its
59*fb13a28bSRafael J. Wysocki  *	metadata, as a continuous sequence of bytes with the help of
60*fb13a28bSRafael J. Wysocki  *	snapshot_read_next() and snapshot_write_next().
61*fb13a28bSRafael J. Wysocki  *
62*fb13a28bSRafael J. Wysocki  *	The code that writes the image to a storage or transfers it to
63*fb13a28bSRafael J. Wysocki  *	the user land is required to use snapshot_read_next() for this
64*fb13a28bSRafael J. Wysocki  *	purpose and it should not make any assumptions regarding the internal
65*fb13a28bSRafael J. Wysocki  *	structure of the image.  Similarly, the code that reads the image from
66*fb13a28bSRafael J. Wysocki  *	a storage or transfers it from the user land is required to use
67*fb13a28bSRafael J. Wysocki  *	snapshot_write_next().
68*fb13a28bSRafael J. Wysocki  *
69*fb13a28bSRafael J. Wysocki  *	This may allow us to change the internal structure of the image
70*fb13a28bSRafael J. Wysocki  *	in the future with considerably less effort.
71*fb13a28bSRafael J. Wysocki  */
72*fb13a28bSRafael J. Wysocki 
73f577eb30SRafael J. Wysocki struct snapshot_handle {
74*fb13a28bSRafael J. Wysocki 	loff_t		offset;	/* number of the last byte ready for reading
75*fb13a28bSRafael J. Wysocki 				 * or writing in the sequence
76*fb13a28bSRafael J. Wysocki 				 */
77*fb13a28bSRafael J. Wysocki 	unsigned int	cur;	/* number of the block of PAGE_SIZE bytes the
78*fb13a28bSRafael J. Wysocki 				 * next operation will refer to (ie. current)
79*fb13a28bSRafael J. Wysocki 				 */
80*fb13a28bSRafael J. Wysocki 	unsigned int	cur_offset;	/* offset with respect to the current
81*fb13a28bSRafael J. Wysocki 					 * block (for the next operation)
82*fb13a28bSRafael J. Wysocki 					 */
83*fb13a28bSRafael J. Wysocki 	unsigned int	prev;	/* number of the block of PAGE_SIZE bytes that
84*fb13a28bSRafael J. Wysocki 				 * was the current one previously
85*fb13a28bSRafael J. Wysocki 				 */
86*fb13a28bSRafael J. Wysocki 	struct pbe	*pbe;	/* PBE that corresponds to 'buffer' */
87*fb13a28bSRafael J. Wysocki 	struct pbe	*last_pbe;	/* When the image is restored (eg. read
88*fb13a28bSRafael J. Wysocki 					 * from disk) we can store some image
89*fb13a28bSRafael J. Wysocki 					 * data directly in the page frames
90*fb13a28bSRafael J. Wysocki 					 * in which they were before suspend.
91*fb13a28bSRafael J. Wysocki 					 * In such a case the PBEs that
92*fb13a28bSRafael J. Wysocki 					 * correspond to them will be unused.
93*fb13a28bSRafael J. Wysocki 					 * This is the last PBE, so far, that
94*fb13a28bSRafael J. Wysocki 					 * does not correspond to such data.
95*fb13a28bSRafael J. Wysocki 					 */
96*fb13a28bSRafael J. Wysocki 	void		*buffer;	/* address of the block to read from
97*fb13a28bSRafael J. Wysocki 					 * or write to
98*fb13a28bSRafael J. Wysocki 					 */
99*fb13a28bSRafael J. Wysocki 	unsigned int	buf_offset;	/* location to read from or write to,
100*fb13a28bSRafael J. Wysocki 					 * given as a displacement from 'buffer'
101*fb13a28bSRafael J. Wysocki 					 */
102*fb13a28bSRafael J. Wysocki 	int		sync_read;	/* Set to one to notify the caller of
103*fb13a28bSRafael J. Wysocki 					 * snapshot_write_next() that it may
104*fb13a28bSRafael J. Wysocki 					 * need to call wait_on_bio_chain()
105*fb13a28bSRafael J. Wysocki 					 */
106f577eb30SRafael J. Wysocki };
107f577eb30SRafael J. Wysocki 
108*fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of
109*fb13a28bSRafael J. Wysocki  * snapshot_read_next()/snapshot_write_next() is allowed to
110*fb13a28bSRafael J. Wysocki  * read/write data after the function returns
111*fb13a28bSRafael J. Wysocki  */
112f577eb30SRafael J. Wysocki #define data_of(handle)	((handle).buffer + (handle).buf_offset)
113f577eb30SRafael J. Wysocki 
114f577eb30SRafael J. Wysocki extern int snapshot_read_next(struct snapshot_handle *handle, size_t count);
115f577eb30SRafael J. Wysocki extern int snapshot_write_next(struct snapshot_handle *handle, size_t count);
116f577eb30SRafael J. Wysocki int snapshot_image_loaded(struct snapshot_handle *handle);
11761159a31SRafael J. Wysocki 
1186e1819d6SRafael J. Wysocki #define SNAPSHOT_IOC_MAGIC	'3'
1196e1819d6SRafael J. Wysocki #define SNAPSHOT_FREEZE			_IO(SNAPSHOT_IOC_MAGIC, 1)
1206e1819d6SRafael J. Wysocki #define SNAPSHOT_UNFREEZE		_IO(SNAPSHOT_IOC_MAGIC, 2)
1216e1819d6SRafael J. Wysocki #define SNAPSHOT_ATOMIC_SNAPSHOT	_IOW(SNAPSHOT_IOC_MAGIC, 3, void *)
1226e1819d6SRafael J. Wysocki #define SNAPSHOT_ATOMIC_RESTORE		_IO(SNAPSHOT_IOC_MAGIC, 4)
1236e1819d6SRafael J. Wysocki #define SNAPSHOT_FREE			_IO(SNAPSHOT_IOC_MAGIC, 5)
1246e1819d6SRafael J. Wysocki #define SNAPSHOT_SET_IMAGE_SIZE		_IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long)
1256e1819d6SRafael J. Wysocki #define SNAPSHOT_AVAIL_SWAP		_IOR(SNAPSHOT_IOC_MAGIC, 7, void *)
1266e1819d6SRafael J. Wysocki #define SNAPSHOT_GET_SWAP_PAGE		_IOR(SNAPSHOT_IOC_MAGIC, 8, void *)
1276e1819d6SRafael J. Wysocki #define SNAPSHOT_FREE_SWAP_PAGES	_IO(SNAPSHOT_IOC_MAGIC, 9)
1286e1819d6SRafael J. Wysocki #define SNAPSHOT_SET_SWAP_FILE		_IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int)
1299b238205SLuca Tettamanti #define SNAPSHOT_S2RAM			_IO(SNAPSHOT_IOC_MAGIC, 11)
1309b238205SLuca Tettamanti #define SNAPSHOT_IOC_MAXNR	11
1316e1819d6SRafael J. Wysocki 
13261159a31SRafael J. Wysocki /**
13361159a31SRafael J. Wysocki  *	The bitmap is used for tracing allocated swap pages
13461159a31SRafael J. Wysocki  *
13561159a31SRafael J. Wysocki  *	The entire bitmap consists of a number of bitmap_page
13661159a31SRafael J. Wysocki  *	structures linked with the help of the .next member.
13761159a31SRafael J. Wysocki  *	Thus each page can be allocated individually, so we only
13861159a31SRafael J. Wysocki  *	need to make 0-order memory allocations to create
13961159a31SRafael J. Wysocki  *	the bitmap.
14061159a31SRafael J. Wysocki  */
14161159a31SRafael J. Wysocki 
14261159a31SRafael J. Wysocki #define BITMAP_PAGE_SIZE	(PAGE_SIZE - sizeof(void *))
14361159a31SRafael J. Wysocki #define BITMAP_PAGE_CHUNKS	(BITMAP_PAGE_SIZE / sizeof(long))
14461159a31SRafael J. Wysocki #define BITS_PER_CHUNK		(sizeof(long) * 8)
14561159a31SRafael J. Wysocki #define BITMAP_PAGE_BITS	(BITMAP_PAGE_CHUNKS * BITS_PER_CHUNK)
14661159a31SRafael J. Wysocki 
14761159a31SRafael J. Wysocki struct bitmap_page {
14861159a31SRafael J. Wysocki 	unsigned long		chunks[BITMAP_PAGE_CHUNKS];
14961159a31SRafael J. Wysocki 	struct bitmap_page	*next;
15061159a31SRafael J. Wysocki };
15161159a31SRafael J. Wysocki 
15261159a31SRafael J. Wysocki extern void free_bitmap(struct bitmap_page *bitmap);
15361159a31SRafael J. Wysocki extern struct bitmap_page *alloc_bitmap(unsigned int nr_bits);
15461159a31SRafael J. Wysocki extern unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap);
15561159a31SRafael J. Wysocki extern void free_all_swap_pages(int swap, struct bitmap_page *bitmap);
15661159a31SRafael J. Wysocki 
15774c7e2efSRandy Dunlap extern int swsusp_check(void);
15861159a31SRafael J. Wysocki extern int swsusp_shrink_memory(void);
15974c7e2efSRandy Dunlap extern void swsusp_free(void);
16061159a31SRafael J. Wysocki extern int swsusp_suspend(void);
16161159a31SRafael J. Wysocki extern int swsusp_resume(void);
16274c7e2efSRandy Dunlap extern int swsusp_read(void);
16374c7e2efSRandy Dunlap extern int swsusp_write(void);
16474c7e2efSRandy Dunlap extern void swsusp_close(void);
1659b238205SLuca Tettamanti extern int suspend_enter(suspend_state_t state);
166