xref: /linux/kernel/power/power.h (revision 406f992e4a372dafbe3c2cff7efbb2002a5c8ebd)
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>
552f5684cSGideon Israel Dsouza #include <linux/compiler.h>
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds struct swsusp_info {
81da177e4SLinus Torvalds 	struct new_utsname	uts;
91da177e4SLinus Torvalds 	u32			version_code;
101da177e4SLinus Torvalds 	unsigned long		num_physpages;
111da177e4SLinus Torvalds 	int			cpus;
121da177e4SLinus Torvalds 	unsigned long		image_pages;
137088a5c0SRafael J. Wysocki 	unsigned long		pages;
146e1819d6SRafael J. Wysocki 	unsigned long		size;
1552f5684cSGideon Israel Dsouza } __aligned(PAGE_SIZE);
161da177e4SLinus Torvalds 
17b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
18ac5c24ecSRafael J. Wysocki /* kernel/power/snapshot.c */
19ddeb6487SRafael J. Wysocki extern void __init hibernate_reserved_size_init(void);
20ac5c24ecSRafael J. Wysocki extern void __init hibernate_image_size_init(void);
21ac5c24ecSRafael J. Wysocki 
22d307c4a8SRafael J. Wysocki #ifdef CONFIG_ARCH_HIBERNATION_HEADER
23d307c4a8SRafael J. Wysocki /* Maximum size of architecture specific data in a hibernation header */
24d307c4a8SRafael J. Wysocki #define MAX_ARCH_HEADER_SIZE	(sizeof(struct new_utsname) + 4)
25d307c4a8SRafael J. Wysocki 
26d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
27d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_restore(void *addr);
28d307c4a8SRafael J. Wysocki 
29d307c4a8SRafael J. Wysocki static inline int init_header_complete(struct swsusp_info *info)
30d307c4a8SRafael J. Wysocki {
31d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
32d307c4a8SRafael J. Wysocki }
33d307c4a8SRafael J. Wysocki 
34d307c4a8SRafael J. Wysocki static inline char *check_image_kernel(struct swsusp_info *info)
35d307c4a8SRafael J. Wysocki {
36d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_restore(info) ?
37d307c4a8SRafael J. Wysocki 			"architecture specific data" : NULL;
38d307c4a8SRafael J. Wysocki }
39d307c4a8SRafael J. Wysocki #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
40d307c4a8SRafael J. Wysocki 
41*406f992eSRafael J. Wysocki extern int hibernate_resume_nonboot_cpu_disable(void);
42*406f992eSRafael J. Wysocki 
4356f99bcbSRafael J. Wysocki /*
4456f99bcbSRafael J. Wysocki  * Keep some memory free so that I/O operations can succeed without paging
4556f99bcbSRafael J. Wysocki  * [Might this be more than 4 MB?]
4656f99bcbSRafael J. Wysocki  */
4756f99bcbSRafael J. Wysocki #define PAGES_FOR_IO	((4096 * 1024) >> PAGE_SHIFT)
48d307c4a8SRafael J. Wysocki 
4956f99bcbSRafael J. Wysocki /*
5056f99bcbSRafael J. Wysocki  * Keep 1 MB of memory free so that device drivers can allocate some pages in
5156f99bcbSRafael J. Wysocki  * their .suspend() routines without breaking the suspend to disk.
5256f99bcbSRafael J. Wysocki  */
5356f99bcbSRafael J. Wysocki #define SPARE_PAGES	((1024 * 1024) >> PAGE_SHIFT)
541da177e4SLinus Torvalds 
5504b73469SRashika Kheria asmlinkage int swsusp_save(void);
5604b73469SRashika Kheria 
578b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
5897819a26SSrivatsa S. Bhat extern bool freezer_test_done;
5997819a26SSrivatsa S. Bhat 
607777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode);
61a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode);
627777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void);
63ac5c24ecSRafael J. Wysocki 
644c0b6c10SRafael J. Wysocki #ifdef CONFIG_DEBUG_RODATA
654c0b6c10SRafael J. Wysocki /* kernel/power/snapshot.c */
664c0b6c10SRafael J. Wysocki extern void enable_restore_image_protection(void);
674c0b6c10SRafael J. Wysocki #else
684c0b6c10SRafael J. Wysocki static inline void enable_restore_image_protection(void) {}
694c0b6c10SRafael J. Wysocki #endif /* CONFIG_DEBUG_RODATA */
704c0b6c10SRafael J. Wysocki 
71ac5c24ecSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */
72ac5c24ecSRafael J. Wysocki 
73ddeb6487SRafael J. Wysocki static inline void hibernate_reserved_size_init(void) {}
74ac5c24ecSRafael J. Wysocki static inline void hibernate_image_size_init(void) {}
75ac5c24ecSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */
76a6d70980SStephen Hemminger 
7749c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long);
7849c3df6aSVivek Goyal 
791da177e4SLinus Torvalds #define power_attr(_name) \
80386f275fSKay Sievers static struct kobj_attribute _name##_attr = {	\
811da177e4SLinus Torvalds 	.attr	= {				\
821da177e4SLinus Torvalds 		.name = __stringify(_name),	\
831da177e4SLinus Torvalds 		.mode = 0644,			\
841da177e4SLinus Torvalds 	},					\
851da177e4SLinus Torvalds 	.show	= _name##_show,			\
861da177e4SLinus Torvalds 	.store	= _name##_store,		\
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
89a1e9ca69SRafael J. Wysocki #define power_attr_ro(_name) \
90a1e9ca69SRafael J. Wysocki static struct kobj_attribute _name##_attr = {	\
91a1e9ca69SRafael J. Wysocki 	.attr	= {				\
92a1e9ca69SRafael J. Wysocki 		.name = __stringify(_name),	\
93a1e9ca69SRafael J. Wysocki 		.mode = S_IRUGO,		\
94a1e9ca69SRafael J. Wysocki 	},					\
95a1e9ca69SRafael J. Wysocki 	.show	= _name##_show,			\
96a1e9ca69SRafael J. Wysocki }
97a1e9ca69SRafael J. Wysocki 
98853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */
99853609b6SRafael J. Wysocki extern unsigned long image_size;
100ddeb6487SRafael J. Wysocki /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
101ddeb6487SRafael J. Wysocki extern unsigned long reserved_size;
102f577eb30SRafael J. Wysocki extern int in_suspend;
10361159a31SRafael J. Wysocki extern dev_t swsusp_resume_device;
1049a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block;
105f577eb30SRafael J. Wysocki 
10625761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void);
10725761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void);
10825761b6eSRafael J. Wysocki 
10974dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void);
11074dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void);
11164a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void);
112f577eb30SRafael J. Wysocki 
113fb13a28bSRafael J. Wysocki /**
114fb13a28bSRafael J. Wysocki  *	Auxiliary structure used for reading the snapshot image data and
115fb13a28bSRafael J. Wysocki  *	metadata from and writing them to the list of page backup entries
116fb13a28bSRafael J. Wysocki  *	(PBEs) which is the main data structure of swsusp.
117fb13a28bSRafael J. Wysocki  *
118fb13a28bSRafael J. Wysocki  *	Using struct snapshot_handle we can transfer the image, including its
119fb13a28bSRafael J. Wysocki  *	metadata, as a continuous sequence of bytes with the help of
120fb13a28bSRafael J. Wysocki  *	snapshot_read_next() and snapshot_write_next().
121fb13a28bSRafael J. Wysocki  *
122fb13a28bSRafael J. Wysocki  *	The code that writes the image to a storage or transfers it to
123fb13a28bSRafael J. Wysocki  *	the user land is required to use snapshot_read_next() for this
124fb13a28bSRafael J. Wysocki  *	purpose and it should not make any assumptions regarding the internal
125fb13a28bSRafael J. Wysocki  *	structure of the image.  Similarly, the code that reads the image from
126fb13a28bSRafael J. Wysocki  *	a storage or transfers it from the user land is required to use
127fb13a28bSRafael J. Wysocki  *	snapshot_write_next().
128fb13a28bSRafael J. Wysocki  *
129fb13a28bSRafael J. Wysocki  *	This may allow us to change the internal structure of the image
130fb13a28bSRafael J. Wysocki  *	in the future with considerably less effort.
131fb13a28bSRafael J. Wysocki  */
132fb13a28bSRafael J. Wysocki 
133f577eb30SRafael J. Wysocki struct snapshot_handle {
134fb13a28bSRafael J. Wysocki 	unsigned int	cur;	/* number of the block of PAGE_SIZE bytes the
135fb13a28bSRafael J. Wysocki 				 * next operation will refer to (ie. current)
136fb13a28bSRafael J. Wysocki 				 */
137fb13a28bSRafael J. Wysocki 	void		*buffer;	/* address of the block to read from
138fb13a28bSRafael J. Wysocki 					 * or write to
139fb13a28bSRafael J. Wysocki 					 */
140fb13a28bSRafael J. Wysocki 	int		sync_read;	/* Set to one to notify the caller of
141fb13a28bSRafael J. Wysocki 					 * snapshot_write_next() that it may
142fb13a28bSRafael J. Wysocki 					 * need to call wait_on_bio_chain()
143fb13a28bSRafael J. Wysocki 					 */
144f577eb30SRafael J. Wysocki };
145f577eb30SRafael J. Wysocki 
146fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of
147fb13a28bSRafael J. Wysocki  * snapshot_read_next()/snapshot_write_next() is allowed to
148fb13a28bSRafael J. Wysocki  * read/write data after the function returns
149fb13a28bSRafael J. Wysocki  */
150d3c1b24cSJiri Slaby #define data_of(handle)	((handle).buffer)
151f577eb30SRafael J. Wysocki 
152b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone);
153af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void);
154d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle);
155d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle);
1568357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle);
157b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle);
15861159a31SRafael J. Wysocki 
1590709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */
1600709db60SRafael J. Wysocki extern atomic_t snapshot_device_available;
1610709db60SRafael J. Wysocki 
162d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap);
163d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap);
164d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void);
16561159a31SRafael J. Wysocki 
166a634cc10SRafael J. Wysocki /*
167a634cc10SRafael J. Wysocki  * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
168a634cc10SRafael J. Wysocki  * the image header.
169a634cc10SRafael J. Wysocki  */
170a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE	1
171f996fc96SBojan Smojver #define SF_NOCOMPRESS_MODE	2
172081a9d04SBojan Smojver #define SF_CRC32_MODE	        4
173a634cc10SRafael J. Wysocki 
1748b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
17574c7e2efSRandy Dunlap extern int swsusp_check(void);
17674c7e2efSRandy Dunlap extern void swsusp_free(void);
177a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p);
178a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags);
179c2dd0daeSAl Viro extern void swsusp_close(fmode_t);
18062c552ccSBojan Smojver #ifdef CONFIG_SUSPEND
18162c552ccSBojan Smojver extern int swsusp_unmark(void);
18262c552ccSBojan Smojver #endif
1830d3a9abeSRafael J. Wysocki 
1840d3a9abeSRafael J. Wysocki struct timeval;
185b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */
186db597605STina Ruchandani extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
187b10d9117SRafael J. Wysocki 
188296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND
189a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */
19062109b43SRafael J. Wysocki extern const char *pm_labels[];
191d431cbc5SRafael J. Wysocki extern const char *pm_states[];
192a9d70523SRafael J. Wysocki 
1936c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state);
194296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
195296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state)
196296699deSRafael J. Wysocki {
197296699deSRafael J. Wysocki 	return -ENOSYS;
198296699deSRafael J. Wysocki }
199296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
200296699deSRafael J. Wysocki 
201a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND
202a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */
203a9d70523SRafael J. Wysocki extern void suspend_test_start(void);
204a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label);
205a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */
206a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {}
207a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {}
208a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */
209a9d70523SRafael J. Wysocki 
21082525756SAlan Stern #ifdef CONFIG_PM_SLEEP
21182525756SAlan Stern /* kernel/power/main.c */
212ea00f4f4SLianwei Wang extern int __pm_notifier_call_chain(unsigned long val, int nr_to_call,
213ea00f4f4SLianwei Wang 				    int *nr_calls);
21482525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val);
21582525756SAlan Stern #endif
2162f8ed1c6SAdrian Bunk 
2172f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM
2182f8ed1c6SAdrian Bunk int restore_highmem(void);
2192f8ed1c6SAdrian Bunk #else
2202f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; }
2212f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; }
2222f8ed1c6SAdrian Bunk #endif
2230e7d56e3SRafael J. Wysocki 
2240e7d56e3SRafael J. Wysocki /*
2250e7d56e3SRafael J. Wysocki  * Suspend test levels
2260e7d56e3SRafael J. Wysocki  */
2270e7d56e3SRafael J. Wysocki enum {
2280e7d56e3SRafael J. Wysocki 	/* keep first */
2290e7d56e3SRafael J. Wysocki 	TEST_NONE,
2300e7d56e3SRafael J. Wysocki 	TEST_CORE,
2310e7d56e3SRafael J. Wysocki 	TEST_CPUS,
2320e7d56e3SRafael J. Wysocki 	TEST_PLATFORM,
2330e7d56e3SRafael J. Wysocki 	TEST_DEVICES,
2340e7d56e3SRafael J. Wysocki 	TEST_FREEZER,
2350e7d56e3SRafael J. Wysocki 	/* keep last */
2360e7d56e3SRafael J. Wysocki 	__TEST_AFTER_LAST
2370e7d56e3SRafael J. Wysocki };
2380e7d56e3SRafael J. Wysocki 
2390e7d56e3SRafael J. Wysocki #define TEST_FIRST	TEST_NONE
2400e7d56e3SRafael J. Wysocki #define TEST_MAX	(__TEST_AFTER_LAST - 1)
2414cc79776SRafael J. Wysocki 
2424cc79776SRafael J. Wysocki extern int pm_test_level;
243b28f5081SJohannes Berg 
244b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER
245b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
246b28f5081SJohannes Berg {
247379e0be8SSrivatsa S. Bhat 	int error;
248379e0be8SSrivatsa S. Bhat 
249379e0be8SSrivatsa S. Bhat 	error = freeze_processes();
250379e0be8SSrivatsa S. Bhat 	/*
251379e0be8SSrivatsa S. Bhat 	 * freeze_processes() automatically thaws every task if freezing
252379e0be8SSrivatsa S. Bhat 	 * fails. So we need not do anything extra upon error.
253379e0be8SSrivatsa S. Bhat 	 */
254379e0be8SSrivatsa S. Bhat 	if (error)
2556f585f75SRafael J. Wysocki 		return error;
256379e0be8SSrivatsa S. Bhat 
257379e0be8SSrivatsa S. Bhat 	error = freeze_kernel_threads();
258379e0be8SSrivatsa S. Bhat 	/*
259379e0be8SSrivatsa S. Bhat 	 * freeze_kernel_threads() thaws only kernel threads upon freezing
260379e0be8SSrivatsa S. Bhat 	 * failure. So we have to thaw the userspace tasks ourselves.
261379e0be8SSrivatsa S. Bhat 	 */
262379e0be8SSrivatsa S. Bhat 	if (error)
263379e0be8SSrivatsa S. Bhat 		thaw_processes();
264379e0be8SSrivatsa S. Bhat 
265379e0be8SSrivatsa S. Bhat 	return error;
266b28f5081SJohannes Berg }
267b28f5081SJohannes Berg 
268b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
269b28f5081SJohannes Berg {
270b28f5081SJohannes Berg 	thaw_processes();
271b28f5081SJohannes Berg }
272b28f5081SJohannes Berg #else
273b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
274b28f5081SJohannes Berg {
275b28f5081SJohannes Berg 	return 0;
276b28f5081SJohannes Berg }
277b28f5081SJohannes Berg 
278b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
279b28f5081SJohannes Berg {
280b28f5081SJohannes Berg }
281b28f5081SJohannes Berg #endif
2827483b4a4SRafael J. Wysocki 
2837483b4a4SRafael J. Wysocki #ifdef CONFIG_PM_AUTOSLEEP
2847483b4a4SRafael J. Wysocki 
2857483b4a4SRafael J. Wysocki /* kernel/power/autosleep.c */
2867483b4a4SRafael J. Wysocki extern int pm_autosleep_init(void);
2877483b4a4SRafael J. Wysocki extern int pm_autosleep_lock(void);
2887483b4a4SRafael J. Wysocki extern void pm_autosleep_unlock(void);
2897483b4a4SRafael J. Wysocki extern suspend_state_t pm_autosleep_state(void);
2907483b4a4SRafael J. Wysocki extern int pm_autosleep_set_state(suspend_state_t state);
2917483b4a4SRafael J. Wysocki 
2927483b4a4SRafael J. Wysocki #else /* !CONFIG_PM_AUTOSLEEP */
2937483b4a4SRafael J. Wysocki 
2947483b4a4SRafael J. Wysocki static inline int pm_autosleep_init(void) { return 0; }
2957483b4a4SRafael J. Wysocki static inline int pm_autosleep_lock(void) { return 0; }
2967483b4a4SRafael J. Wysocki static inline void pm_autosleep_unlock(void) {}
2977483b4a4SRafael J. Wysocki static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
2987483b4a4SRafael J. Wysocki 
2997483b4a4SRafael J. Wysocki #endif /* !CONFIG_PM_AUTOSLEEP */
300b86ff982SRafael J. Wysocki 
301b86ff982SRafael J. Wysocki #ifdef CONFIG_PM_WAKELOCKS
302b86ff982SRafael J. Wysocki 
303b86ff982SRafael J. Wysocki /* kernel/power/wakelock.c */
304b86ff982SRafael J. Wysocki extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
305b86ff982SRafael J. Wysocki extern int pm_wake_lock(const char *buf);
306b86ff982SRafael J. Wysocki extern int pm_wake_unlock(const char *buf);
307b86ff982SRafael J. Wysocki 
308b86ff982SRafael J. Wysocki #endif /* !CONFIG_PM_WAKELOCKS */
309