xref: /linux/kernel/power/power.h (revision 62c552ccc3eda1198632a4f344aa32623d226bab)
11da177e4SLinus Torvalds #include <linux/suspend.h>
23010f8caSRafael J. Wysocki #include <linux/suspend_ioctls.h>
31da177e4SLinus Torvalds #include <linux/utsname.h>
4b28f5081SJohannes Berg #include <linux/freezer.h>
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds struct swsusp_info {
71da177e4SLinus Torvalds 	struct new_utsname	uts;
81da177e4SLinus Torvalds 	u32			version_code;
91da177e4SLinus Torvalds 	unsigned long		num_physpages;
101da177e4SLinus Torvalds 	int			cpus;
111da177e4SLinus Torvalds 	unsigned long		image_pages;
127088a5c0SRafael J. Wysocki 	unsigned long		pages;
136e1819d6SRafael J. Wysocki 	unsigned long		size;
141da177e4SLinus Torvalds } __attribute__((aligned(PAGE_SIZE)));
151da177e4SLinus Torvalds 
16b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
17ac5c24ecSRafael J. Wysocki /* kernel/power/snapshot.c */
18ddeb6487SRafael J. Wysocki extern void __init hibernate_reserved_size_init(void);
19ac5c24ecSRafael J. Wysocki extern void __init hibernate_image_size_init(void);
20ac5c24ecSRafael J. Wysocki 
21d307c4a8SRafael J. Wysocki #ifdef CONFIG_ARCH_HIBERNATION_HEADER
22d307c4a8SRafael J. Wysocki /* Maximum size of architecture specific data in a hibernation header */
23d307c4a8SRafael J. Wysocki #define MAX_ARCH_HEADER_SIZE	(sizeof(struct new_utsname) + 4)
24d307c4a8SRafael J. Wysocki 
25d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
26d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_restore(void *addr);
27d307c4a8SRafael J. Wysocki 
28d307c4a8SRafael J. Wysocki static inline int init_header_complete(struct swsusp_info *info)
29d307c4a8SRafael J. Wysocki {
30d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
31d307c4a8SRafael J. Wysocki }
32d307c4a8SRafael J. Wysocki 
33d307c4a8SRafael J. Wysocki static inline char *check_image_kernel(struct swsusp_info *info)
34d307c4a8SRafael J. Wysocki {
35d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_restore(info) ?
36d307c4a8SRafael J. Wysocki 			"architecture specific data" : NULL;
37d307c4a8SRafael J. Wysocki }
38d307c4a8SRafael J. Wysocki #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
39d307c4a8SRafael J. Wysocki 
4056f99bcbSRafael J. Wysocki /*
4156f99bcbSRafael J. Wysocki  * Keep some memory free so that I/O operations can succeed without paging
4256f99bcbSRafael J. Wysocki  * [Might this be more than 4 MB?]
4356f99bcbSRafael J. Wysocki  */
4456f99bcbSRafael J. Wysocki #define PAGES_FOR_IO	((4096 * 1024) >> PAGE_SHIFT)
45d307c4a8SRafael J. Wysocki 
4656f99bcbSRafael J. Wysocki /*
4756f99bcbSRafael J. Wysocki  * Keep 1 MB of memory free so that device drivers can allocate some pages in
4856f99bcbSRafael J. Wysocki  * their .suspend() routines without breaking the suspend to disk.
4956f99bcbSRafael J. Wysocki  */
5056f99bcbSRafael J. Wysocki #define SPARE_PAGES	((1024 * 1024) >> PAGE_SHIFT)
511da177e4SLinus Torvalds 
528b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
5397819a26SSrivatsa S. Bhat extern bool freezer_test_done;
5497819a26SSrivatsa S. Bhat 
557777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode);
56a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode);
577777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void);
58ac5c24ecSRafael J. Wysocki 
59ac5c24ecSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */
60ac5c24ecSRafael J. Wysocki 
61ddeb6487SRafael J. Wysocki static inline void hibernate_reserved_size_init(void) {}
62ac5c24ecSRafael J. Wysocki static inline void hibernate_image_size_init(void) {}
63ac5c24ecSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */
64a6d70980SStephen Hemminger 
6549c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long);
6649c3df6aSVivek Goyal 
671da177e4SLinus Torvalds #define power_attr(_name) \
68386f275fSKay Sievers static struct kobj_attribute _name##_attr = {	\
691da177e4SLinus Torvalds 	.attr	= {				\
701da177e4SLinus Torvalds 		.name = __stringify(_name),	\
711da177e4SLinus Torvalds 		.mode = 0644,			\
721da177e4SLinus Torvalds 	},					\
731da177e4SLinus Torvalds 	.show	= _name##_show,			\
741da177e4SLinus Torvalds 	.store	= _name##_store,		\
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
77853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */
78853609b6SRafael J. Wysocki extern unsigned long image_size;
79ddeb6487SRafael J. Wysocki /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
80ddeb6487SRafael J. Wysocki extern unsigned long reserved_size;
81f577eb30SRafael J. Wysocki extern int in_suspend;
8261159a31SRafael J. Wysocki extern dev_t swsusp_resume_device;
839a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block;
84f577eb30SRafael J. Wysocki 
8525761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void);
8625761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void);
8725761b6eSRafael J. Wysocki 
8874dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void);
8974dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void);
9064a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void);
91f577eb30SRafael J. Wysocki 
92fb13a28bSRafael J. Wysocki /**
93fb13a28bSRafael J. Wysocki  *	Auxiliary structure used for reading the snapshot image data and
94fb13a28bSRafael J. Wysocki  *	metadata from and writing them to the list of page backup entries
95fb13a28bSRafael J. Wysocki  *	(PBEs) which is the main data structure of swsusp.
96fb13a28bSRafael J. Wysocki  *
97fb13a28bSRafael J. Wysocki  *	Using struct snapshot_handle we can transfer the image, including its
98fb13a28bSRafael J. Wysocki  *	metadata, as a continuous sequence of bytes with the help of
99fb13a28bSRafael J. Wysocki  *	snapshot_read_next() and snapshot_write_next().
100fb13a28bSRafael J. Wysocki  *
101fb13a28bSRafael J. Wysocki  *	The code that writes the image to a storage or transfers it to
102fb13a28bSRafael J. Wysocki  *	the user land is required to use snapshot_read_next() for this
103fb13a28bSRafael J. Wysocki  *	purpose and it should not make any assumptions regarding the internal
104fb13a28bSRafael J. Wysocki  *	structure of the image.  Similarly, the code that reads the image from
105fb13a28bSRafael J. Wysocki  *	a storage or transfers it from the user land is required to use
106fb13a28bSRafael J. Wysocki  *	snapshot_write_next().
107fb13a28bSRafael J. Wysocki  *
108fb13a28bSRafael J. Wysocki  *	This may allow us to change the internal structure of the image
109fb13a28bSRafael J. Wysocki  *	in the future with considerably less effort.
110fb13a28bSRafael J. Wysocki  */
111fb13a28bSRafael J. Wysocki 
112f577eb30SRafael J. Wysocki struct snapshot_handle {
113fb13a28bSRafael J. Wysocki 	unsigned int	cur;	/* number of the block of PAGE_SIZE bytes the
114fb13a28bSRafael J. Wysocki 				 * next operation will refer to (ie. current)
115fb13a28bSRafael J. Wysocki 				 */
116fb13a28bSRafael J. Wysocki 	void		*buffer;	/* address of the block to read from
117fb13a28bSRafael J. Wysocki 					 * or write to
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  */
129d3c1b24cSJiri Slaby #define data_of(handle)	((handle).buffer)
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);
133d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle);
134d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle);
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
150f996fc96SBojan Smojver #define SF_NOCOMPRESS_MODE	2
151081a9d04SBojan Smojver #define SF_CRC32_MODE	        4
152a634cc10SRafael J. Wysocki 
1538b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
15474c7e2efSRandy Dunlap extern int swsusp_check(void);
15574c7e2efSRandy Dunlap extern void swsusp_free(void);
156a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p);
157a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags);
158c2dd0daeSAl Viro extern void swsusp_close(fmode_t);
159*62c552ccSBojan Smojver #ifdef CONFIG_SUSPEND
160*62c552ccSBojan Smojver extern int swsusp_unmark(void);
161*62c552ccSBojan Smojver #endif
1620d3a9abeSRafael J. Wysocki 
1638a0d613fSJiri Slaby /* kernel/power/block_io.c */
1648a0d613fSJiri Slaby extern struct block_device *hib_resume_bdev;
1658a0d613fSJiri Slaby 
1668a0d613fSJiri Slaby extern int hib_bio_read_page(pgoff_t page_off, void *addr,
1678a0d613fSJiri Slaby 		struct bio **bio_chain);
1688a0d613fSJiri Slaby extern int hib_bio_write_page(pgoff_t page_off, void *addr,
1698a0d613fSJiri Slaby 		struct bio **bio_chain);
1708a0d613fSJiri Slaby extern int hib_wait_on_bio_chain(struct bio **bio_chain);
1718a0d613fSJiri Slaby 
1720d3a9abeSRafael J. Wysocki struct timeval;
173b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */
1740d3a9abeSRafael J. Wysocki extern void swsusp_show_speed(struct timeval *, struct timeval *,
1750d3a9abeSRafael J. Wysocki 				unsigned int, char *);
176b10d9117SRafael J. Wysocki 
177296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND
178a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */
179a9d70523SRafael J. Wysocki extern const char *const pm_states[];
180a9d70523SRafael J. Wysocki 
181a9d70523SRafael J. Wysocki extern bool valid_state(suspend_state_t state);
1826c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state);
183296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
184296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state)
185296699deSRafael J. Wysocki {
186296699deSRafael J. Wysocki 	return -ENOSYS;
187296699deSRafael J. Wysocki }
188a9d70523SRafael J. Wysocki static inline bool valid_state(suspend_state_t state) { return false; }
189296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
190296699deSRafael J. Wysocki 
191a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND
192a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */
193a9d70523SRafael J. Wysocki extern void suspend_test_start(void);
194a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label);
195a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */
196a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {}
197a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {}
198a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */
199a9d70523SRafael J. Wysocki 
20082525756SAlan Stern #ifdef CONFIG_PM_SLEEP
20182525756SAlan Stern /* kernel/power/main.c */
20282525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val);
20382525756SAlan Stern #endif
2042f8ed1c6SAdrian Bunk 
2052f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM
2062f8ed1c6SAdrian Bunk int restore_highmem(void);
2072f8ed1c6SAdrian Bunk #else
2082f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; }
2092f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; }
2102f8ed1c6SAdrian Bunk #endif
2110e7d56e3SRafael J. Wysocki 
2120e7d56e3SRafael J. Wysocki /*
2130e7d56e3SRafael J. Wysocki  * Suspend test levels
2140e7d56e3SRafael J. Wysocki  */
2150e7d56e3SRafael J. Wysocki enum {
2160e7d56e3SRafael J. Wysocki 	/* keep first */
2170e7d56e3SRafael J. Wysocki 	TEST_NONE,
2180e7d56e3SRafael J. Wysocki 	TEST_CORE,
2190e7d56e3SRafael J. Wysocki 	TEST_CPUS,
2200e7d56e3SRafael J. Wysocki 	TEST_PLATFORM,
2210e7d56e3SRafael J. Wysocki 	TEST_DEVICES,
2220e7d56e3SRafael J. Wysocki 	TEST_FREEZER,
2230e7d56e3SRafael J. Wysocki 	/* keep last */
2240e7d56e3SRafael J. Wysocki 	__TEST_AFTER_LAST
2250e7d56e3SRafael J. Wysocki };
2260e7d56e3SRafael J. Wysocki 
2270e7d56e3SRafael J. Wysocki #define TEST_FIRST	TEST_NONE
2280e7d56e3SRafael J. Wysocki #define TEST_MAX	(__TEST_AFTER_LAST - 1)
2294cc79776SRafael J. Wysocki 
2304cc79776SRafael J. Wysocki extern int pm_test_level;
231b28f5081SJohannes Berg 
232b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER
233b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
234b28f5081SJohannes Berg {
235379e0be8SSrivatsa S. Bhat 	int error;
236379e0be8SSrivatsa S. Bhat 
237379e0be8SSrivatsa S. Bhat 	error = freeze_processes();
238379e0be8SSrivatsa S. Bhat 	/*
239379e0be8SSrivatsa S. Bhat 	 * freeze_processes() automatically thaws every task if freezing
240379e0be8SSrivatsa S. Bhat 	 * fails. So we need not do anything extra upon error.
241379e0be8SSrivatsa S. Bhat 	 */
242379e0be8SSrivatsa S. Bhat 	if (error)
2436f585f75SRafael J. Wysocki 		return error;
244379e0be8SSrivatsa S. Bhat 
245379e0be8SSrivatsa S. Bhat 	error = freeze_kernel_threads();
246379e0be8SSrivatsa S. Bhat 	/*
247379e0be8SSrivatsa S. Bhat 	 * freeze_kernel_threads() thaws only kernel threads upon freezing
248379e0be8SSrivatsa S. Bhat 	 * failure. So we have to thaw the userspace tasks ourselves.
249379e0be8SSrivatsa S. Bhat 	 */
250379e0be8SSrivatsa S. Bhat 	if (error)
251379e0be8SSrivatsa S. Bhat 		thaw_processes();
252379e0be8SSrivatsa S. Bhat 
253379e0be8SSrivatsa S. Bhat 	return error;
254b28f5081SJohannes Berg }
255b28f5081SJohannes Berg 
256b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
257b28f5081SJohannes Berg {
258b28f5081SJohannes Berg 	thaw_processes();
259b28f5081SJohannes Berg }
260b28f5081SJohannes Berg #else
261b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
262b28f5081SJohannes Berg {
263b28f5081SJohannes Berg 	return 0;
264b28f5081SJohannes Berg }
265b28f5081SJohannes Berg 
266b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
267b28f5081SJohannes Berg {
268b28f5081SJohannes Berg }
269b28f5081SJohannes Berg #endif
2707483b4a4SRafael J. Wysocki 
2717483b4a4SRafael J. Wysocki #ifdef CONFIG_PM_AUTOSLEEP
2727483b4a4SRafael J. Wysocki 
2737483b4a4SRafael J. Wysocki /* kernel/power/autosleep.c */
2747483b4a4SRafael J. Wysocki extern int pm_autosleep_init(void);
2757483b4a4SRafael J. Wysocki extern int pm_autosleep_lock(void);
2767483b4a4SRafael J. Wysocki extern void pm_autosleep_unlock(void);
2777483b4a4SRafael J. Wysocki extern suspend_state_t pm_autosleep_state(void);
2787483b4a4SRafael J. Wysocki extern int pm_autosleep_set_state(suspend_state_t state);
2797483b4a4SRafael J. Wysocki 
2807483b4a4SRafael J. Wysocki #else /* !CONFIG_PM_AUTOSLEEP */
2817483b4a4SRafael J. Wysocki 
2827483b4a4SRafael J. Wysocki static inline int pm_autosleep_init(void) { return 0; }
2837483b4a4SRafael J. Wysocki static inline int pm_autosleep_lock(void) { return 0; }
2847483b4a4SRafael J. Wysocki static inline void pm_autosleep_unlock(void) {}
2857483b4a4SRafael J. Wysocki static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
2867483b4a4SRafael J. Wysocki 
2877483b4a4SRafael J. Wysocki #endif /* !CONFIG_PM_AUTOSLEEP */
288b86ff982SRafael J. Wysocki 
289b86ff982SRafael J. Wysocki #ifdef CONFIG_PM_WAKELOCKS
290b86ff982SRafael J. Wysocki 
291b86ff982SRafael J. Wysocki /* kernel/power/wakelock.c */
292b86ff982SRafael J. Wysocki extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
293b86ff982SRafael J. Wysocki extern int pm_wake_lock(const char *buf);
294b86ff982SRafael J. Wysocki extern int pm_wake_unlock(const char *buf);
295b86ff982SRafael J. Wysocki 
296b86ff982SRafael J. Wysocki #endif /* !CONFIG_PM_WAKELOCKS */
297