xref: /linux/kernel/power/power.h (revision 2f8ed1c60b06b797bf79a1dc540f0bed8c9d75a0)
11da177e4SLinus Torvalds #include <linux/suspend.h>
23010f8caSRafael J. Wysocki #include <linux/suspend_ioctls.h>
31da177e4SLinus Torvalds #include <linux/utsname.h>
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds struct swsusp_info {
61da177e4SLinus Torvalds 	struct new_utsname	uts;
71da177e4SLinus Torvalds 	u32			version_code;
81da177e4SLinus Torvalds 	unsigned long		num_physpages;
91da177e4SLinus Torvalds 	int			cpus;
101da177e4SLinus Torvalds 	unsigned long		image_pages;
117088a5c0SRafael J. Wysocki 	unsigned long		pages;
126e1819d6SRafael J. Wysocki 	unsigned long		size;
131da177e4SLinus Torvalds } __attribute__((aligned(PAGE_SIZE)));
141da177e4SLinus Torvalds 
15b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
16d307c4a8SRafael J. Wysocki #ifdef CONFIG_ARCH_HIBERNATION_HEADER
17d307c4a8SRafael J. Wysocki /* Maximum size of architecture specific data in a hibernation header */
18d307c4a8SRafael J. Wysocki #define MAX_ARCH_HEADER_SIZE	(sizeof(struct new_utsname) + 4)
19d307c4a8SRafael J. Wysocki 
20d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
21d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_restore(void *addr);
22d307c4a8SRafael J. Wysocki 
23d307c4a8SRafael J. Wysocki static inline int init_header_complete(struct swsusp_info *info)
24d307c4a8SRafael J. Wysocki {
25d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
26d307c4a8SRafael J. Wysocki }
27d307c4a8SRafael J. Wysocki 
28d307c4a8SRafael J. Wysocki static inline char *check_image_kernel(struct swsusp_info *info)
29d307c4a8SRafael J. Wysocki {
30d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_restore(info) ?
31d307c4a8SRafael J. Wysocki 			"architecture specific data" : NULL;
32d307c4a8SRafael J. Wysocki }
33d307c4a8SRafael J. Wysocki #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
34d307c4a8SRafael J. Wysocki 
3556f99bcbSRafael J. Wysocki /*
3656f99bcbSRafael J. Wysocki  * Keep some memory free so that I/O operations can succeed without paging
3756f99bcbSRafael J. Wysocki  * [Might this be more than 4 MB?]
3856f99bcbSRafael J. Wysocki  */
3956f99bcbSRafael J. Wysocki #define PAGES_FOR_IO	((4096 * 1024) >> PAGE_SHIFT)
40d307c4a8SRafael J. Wysocki 
4156f99bcbSRafael J. Wysocki /*
4256f99bcbSRafael J. Wysocki  * Keep 1 MB of memory free so that device drivers can allocate some pages in
4356f99bcbSRafael J. Wysocki  * their .suspend() routines without breaking the suspend to disk.
4456f99bcbSRafael J. Wysocki  */
4556f99bcbSRafael J. Wysocki #define SPARE_PAGES	((1024 * 1024) >> PAGE_SHIFT)
461da177e4SLinus Torvalds 
477777fab9SRafael J. Wysocki /* kernel/power/disk.c */
487777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode);
49a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode);
507777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void);
511da177e4SLinus Torvalds #endif
52a6d70980SStephen Hemminger 
5349c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long);
5449c3df6aSVivek Goyal 
55a6d70980SStephen Hemminger extern struct mutex pm_mutex;
56a6d70980SStephen Hemminger 
571da177e4SLinus Torvalds #define power_attr(_name) \
58386f275fSKay Sievers static struct kobj_attribute _name##_attr = {	\
591da177e4SLinus Torvalds 	.attr	= {				\
601da177e4SLinus Torvalds 		.name = __stringify(_name),	\
611da177e4SLinus Torvalds 		.mode = 0644,			\
621da177e4SLinus Torvalds 	},					\
631da177e4SLinus Torvalds 	.show	= _name##_show,			\
641da177e4SLinus Torvalds 	.store	= _name##_store,		\
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
67853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */
68853609b6SRafael J. Wysocki extern unsigned long image_size;
69f577eb30SRafael J. Wysocki extern int in_suspend;
7061159a31SRafael J. Wysocki extern dev_t swsusp_resume_device;
719a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block;
72f577eb30SRafael J. Wysocki 
7325761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void);
7425761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void);
7525761b6eSRafael J. Wysocki 
7674dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void);
7774dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void);
7872a97e08SRafael J. Wysocki extern unsigned int count_data_pages(void);
79f577eb30SRafael J. Wysocki 
80fb13a28bSRafael J. Wysocki /**
81fb13a28bSRafael J. Wysocki  *	Auxiliary structure used for reading the snapshot image data and
82fb13a28bSRafael J. Wysocki  *	metadata from and writing them to the list of page backup entries
83fb13a28bSRafael J. Wysocki  *	(PBEs) which is the main data structure of swsusp.
84fb13a28bSRafael J. Wysocki  *
85fb13a28bSRafael J. Wysocki  *	Using struct snapshot_handle we can transfer the image, including its
86fb13a28bSRafael J. Wysocki  *	metadata, as a continuous sequence of bytes with the help of
87fb13a28bSRafael J. Wysocki  *	snapshot_read_next() and snapshot_write_next().
88fb13a28bSRafael J. Wysocki  *
89fb13a28bSRafael J. Wysocki  *	The code that writes the image to a storage or transfers it to
90fb13a28bSRafael J. Wysocki  *	the user land is required to use snapshot_read_next() for this
91fb13a28bSRafael J. Wysocki  *	purpose and it should not make any assumptions regarding the internal
92fb13a28bSRafael J. Wysocki  *	structure of the image.  Similarly, the code that reads the image from
93fb13a28bSRafael J. Wysocki  *	a storage or transfers it from the user land is required to use
94fb13a28bSRafael J. Wysocki  *	snapshot_write_next().
95fb13a28bSRafael J. Wysocki  *
96fb13a28bSRafael J. Wysocki  *	This may allow us to change the internal structure of the image
97fb13a28bSRafael J. Wysocki  *	in the future with considerably less effort.
98fb13a28bSRafael J. Wysocki  */
99fb13a28bSRafael J. Wysocki 
100f577eb30SRafael J. Wysocki struct snapshot_handle {
101fb13a28bSRafael J. Wysocki 	loff_t		offset;	/* number of the last byte ready for reading
102fb13a28bSRafael J. Wysocki 				 * or writing in the sequence
103fb13a28bSRafael J. Wysocki 				 */
104fb13a28bSRafael J. Wysocki 	unsigned int	cur;	/* number of the block of PAGE_SIZE bytes the
105fb13a28bSRafael J. Wysocki 				 * next operation will refer to (ie. current)
106fb13a28bSRafael J. Wysocki 				 */
107fb13a28bSRafael J. Wysocki 	unsigned int	cur_offset;	/* offset with respect to the current
108fb13a28bSRafael J. Wysocki 					 * block (for the next operation)
109fb13a28bSRafael J. Wysocki 					 */
110fb13a28bSRafael J. Wysocki 	unsigned int	prev;	/* number of the block of PAGE_SIZE bytes that
111fb13a28bSRafael J. Wysocki 				 * was the current one previously
112fb13a28bSRafael J. Wysocki 				 */
113fb13a28bSRafael J. Wysocki 	void		*buffer;	/* address of the block to read from
114fb13a28bSRafael J. Wysocki 					 * or write to
115fb13a28bSRafael J. Wysocki 					 */
116fb13a28bSRafael J. Wysocki 	unsigned int	buf_offset;	/* location to read from or write to,
117fb13a28bSRafael J. Wysocki 					 * given as a displacement from 'buffer'
118fb13a28bSRafael J. Wysocki 					 */
119fb13a28bSRafael J. Wysocki 	int		sync_read;	/* Set to one to notify the caller of
120fb13a28bSRafael J. Wysocki 					 * snapshot_write_next() that it may
121fb13a28bSRafael J. Wysocki 					 * need to call wait_on_bio_chain()
122fb13a28bSRafael J. Wysocki 					 */
123f577eb30SRafael J. Wysocki };
124f577eb30SRafael J. Wysocki 
125fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of
126fb13a28bSRafael J. Wysocki  * snapshot_read_next()/snapshot_write_next() is allowed to
127fb13a28bSRafael J. Wysocki  * read/write data after the function returns
128fb13a28bSRafael J. Wysocki  */
129f577eb30SRafael J. Wysocki #define data_of(handle)	((handle).buffer + (handle).buf_offset)
130f577eb30SRafael J. Wysocki 
131b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone);
132af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void);
133f577eb30SRafael J. Wysocki extern int snapshot_read_next(struct snapshot_handle *handle, size_t count);
134f577eb30SRafael J. Wysocki extern int snapshot_write_next(struct snapshot_handle *handle, size_t count);
1358357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle);
136b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle);
13761159a31SRafael J. Wysocki 
1380709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */
1390709db60SRafael J. Wysocki extern atomic_t snapshot_device_available;
1400709db60SRafael J. Wysocki 
141d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap);
142d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap);
143d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void);
14461159a31SRafael J. Wysocki 
145a634cc10SRafael J. Wysocki /*
146a634cc10SRafael J. Wysocki  * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
147a634cc10SRafael J. Wysocki  * the image header.
148a634cc10SRafael J. Wysocki  */
149a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE	1
150a634cc10SRafael J. Wysocki 
151a634cc10SRafael J. Wysocki /* kernel/power/disk.c */
15274c7e2efSRandy Dunlap extern int swsusp_check(void);
15361159a31SRafael J. Wysocki extern int swsusp_shrink_memory(void);
15474c7e2efSRandy Dunlap extern void swsusp_free(void);
15561159a31SRafael J. Wysocki extern int swsusp_resume(void);
156a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p);
157a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags);
15874c7e2efSRandy Dunlap extern void swsusp_close(void);
1590d3a9abeSRafael J. Wysocki 
1600d3a9abeSRafael J. Wysocki struct timeval;
161b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */
1620d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *,
1630d3a9abeSRafael J. Wysocki 				unsigned int, char *);
164b10d9117SRafael J. Wysocki 
165296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND
166b10d9117SRafael J. Wysocki /* kernel/power/main.c */
1676c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state);
168296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
169296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state)
170296699deSRafael J. Wysocki {
171296699deSRafael J. Wysocki 	return -ENOSYS;
172296699deSRafael J. Wysocki }
173296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
174296699deSRafael J. Wysocki 
175296699deSRafael J. Wysocki /* kernel/power/common.c */
176b10d9117SRafael J. Wysocki extern struct blocking_notifier_head pm_chain_head;
177b10d9117SRafael J. Wysocki 
178b10d9117SRafael J. Wysocki static inline int pm_notifier_call_chain(unsigned long val)
179b10d9117SRafael J. Wysocki {
180b10d9117SRafael J. Wysocki 	return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
181b10d9117SRafael J. Wysocki 			== NOTIFY_BAD) ? -EINVAL : 0;
182b10d9117SRafael J. Wysocki }
183*2f8ed1c6SAdrian Bunk 
184*2f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM
185*2f8ed1c6SAdrian Bunk unsigned int count_highmem_pages(void);
186*2f8ed1c6SAdrian Bunk int restore_highmem(void);
187*2f8ed1c6SAdrian Bunk #else
188*2f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; }
189*2f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; }
190*2f8ed1c6SAdrian Bunk #endif
191