xref: /linux/kernel/power/power.h (revision 03b6c9a3e8805606c0bb4ad41855fac3bf85c3b9)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #include <linux/suspend.h>
33010f8caSRafael J. Wysocki #include <linux/suspend_ioctls.h>
41da177e4SLinus Torvalds #include <linux/utsname.h>
5b28f5081SJohannes Berg #include <linux/freezer.h>
652f5684cSGideon Israel Dsouza #include <linux/compiler.h>
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds struct swsusp_info {
91da177e4SLinus Torvalds 	struct new_utsname	uts;
101da177e4SLinus Torvalds 	u32			version_code;
111da177e4SLinus Torvalds 	unsigned long		num_physpages;
121da177e4SLinus Torvalds 	int			cpus;
131da177e4SLinus Torvalds 	unsigned long		image_pages;
147088a5c0SRafael J. Wysocki 	unsigned long		pages;
156e1819d6SRafael J. Wysocki 	unsigned long		size;
1652f5684cSGideon Israel Dsouza } __aligned(PAGE_SIZE);
171da177e4SLinus Torvalds 
18b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
19ac5c24ecSRafael J. Wysocki /* kernel/power/snapshot.c */
20ddeb6487SRafael J. Wysocki extern void __init hibernate_reserved_size_init(void);
21ac5c24ecSRafael J. Wysocki extern void __init hibernate_image_size_init(void);
22ac5c24ecSRafael J. Wysocki 
23d307c4a8SRafael J. Wysocki #ifdef CONFIG_ARCH_HIBERNATION_HEADER
24d307c4a8SRafael J. Wysocki /* Maximum size of architecture specific data in a hibernation header */
25d307c4a8SRafael J. Wysocki #define MAX_ARCH_HEADER_SIZE	(sizeof(struct new_utsname) + 4)
26d307c4a8SRafael J. Wysocki 
27d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
28d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_restore(void *addr);
29d307c4a8SRafael J. Wysocki 
30d307c4a8SRafael J. Wysocki static inline int init_header_complete(struct swsusp_info *info)
31d307c4a8SRafael J. Wysocki {
32d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
33d307c4a8SRafael J. Wysocki }
34d307c4a8SRafael J. Wysocki 
3502d7f400SAlexey Dobriyan static inline const char *check_image_kernel(struct swsusp_info *info)
36d307c4a8SRafael J. Wysocki {
37d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_restore(info) ?
38d307c4a8SRafael J. Wysocki 			"architecture specific data" : NULL;
39d307c4a8SRafael J. Wysocki }
40d307c4a8SRafael J. Wysocki #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
41d307c4a8SRafael J. Wysocki 
42406f992eSRafael J. Wysocki extern int hibernate_resume_nonboot_cpu_disable(void);
43406f992eSRafael J. Wysocki 
4456f99bcbSRafael J. Wysocki /*
4556f99bcbSRafael J. Wysocki  * Keep some memory free so that I/O operations can succeed without paging
4656f99bcbSRafael J. Wysocki  * [Might this be more than 4 MB?]
4756f99bcbSRafael J. Wysocki  */
4856f99bcbSRafael J. Wysocki #define PAGES_FOR_IO	((4096 * 1024) >> PAGE_SHIFT)
49d307c4a8SRafael J. Wysocki 
5056f99bcbSRafael J. Wysocki /*
5156f99bcbSRafael J. Wysocki  * Keep 1 MB of memory free so that device drivers can allocate some pages in
5256f99bcbSRafael J. Wysocki  * their .suspend() routines without breaking the suspend to disk.
5356f99bcbSRafael J. Wysocki  */
5456f99bcbSRafael J. Wysocki #define SPARE_PAGES	((1024 * 1024) >> PAGE_SHIFT)
551da177e4SLinus Torvalds 
5604b73469SRashika Kheria asmlinkage int swsusp_save(void);
5704b73469SRashika Kheria 
588b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
5997819a26SSrivatsa S. Bhat extern bool freezer_test_done;
6097819a26SSrivatsa S. Bhat 
617777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode);
62a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode);
637777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void);
64ac5c24ecSRafael J. Wysocki 
650f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
664c0b6c10SRafael J. Wysocki /* kernel/power/snapshot.c */
674c0b6c10SRafael J. Wysocki extern void enable_restore_image_protection(void);
684c0b6c10SRafael J. Wysocki #else
694c0b6c10SRafael J. Wysocki static inline void enable_restore_image_protection(void) {}
700f5bf6d0SLaura Abbott #endif /* CONFIG_STRICT_KERNEL_RWX */
714c0b6c10SRafael J. Wysocki 
72ac5c24ecSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */
73ac5c24ecSRafael J. Wysocki 
74ddeb6487SRafael J. Wysocki static inline void hibernate_reserved_size_init(void) {}
75ac5c24ecSRafael J. Wysocki static inline void hibernate_image_size_init(void) {}
76ac5c24ecSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */
77a6d70980SStephen Hemminger 
781da177e4SLinus Torvalds #define power_attr(_name) \
79386f275fSKay Sievers static struct kobj_attribute _name##_attr = {	\
801da177e4SLinus Torvalds 	.attr	= {				\
811da177e4SLinus Torvalds 		.name = __stringify(_name),	\
821da177e4SLinus Torvalds 		.mode = 0644,			\
831da177e4SLinus Torvalds 	},					\
841da177e4SLinus Torvalds 	.show	= _name##_show,			\
851da177e4SLinus Torvalds 	.store	= _name##_store,		\
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
88a1e9ca69SRafael J. Wysocki #define power_attr_ro(_name) \
89a1e9ca69SRafael J. Wysocki static struct kobj_attribute _name##_attr = {	\
90a1e9ca69SRafael J. Wysocki 	.attr	= {				\
91a1e9ca69SRafael J. Wysocki 		.name = __stringify(_name),	\
92a1e9ca69SRafael J. Wysocki 		.mode = S_IRUGO,		\
93a1e9ca69SRafael J. Wysocki 	},					\
94a1e9ca69SRafael J. Wysocki 	.show	= _name##_show,			\
95a1e9ca69SRafael J. Wysocki }
96a1e9ca69SRafael J. Wysocki 
97853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */
98853609b6SRafael J. Wysocki extern unsigned long image_size;
99ddeb6487SRafael J. Wysocki /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
100ddeb6487SRafael J. Wysocki extern unsigned long reserved_size;
101f577eb30SRafael J. Wysocki extern int in_suspend;
10261159a31SRafael J. Wysocki extern dev_t swsusp_resume_device;
1039a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block;
104f577eb30SRafael J. Wysocki 
10574dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void);
10674dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void);
10764a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void);
108f577eb30SRafael J. Wysocki 
109*03b6c9a3SVlastimil Babka extern void clear_or_poison_free_pages(void);
1101ad1410fSAnisse Astier 
111fb13a28bSRafael J. Wysocki /**
112fb13a28bSRafael J. Wysocki  *	Auxiliary structure used for reading the snapshot image data and
113fb13a28bSRafael J. Wysocki  *	metadata from and writing them to the list of page backup entries
114fb13a28bSRafael J. Wysocki  *	(PBEs) which is the main data structure of swsusp.
115fb13a28bSRafael J. Wysocki  *
116fb13a28bSRafael J. Wysocki  *	Using struct snapshot_handle we can transfer the image, including its
117fb13a28bSRafael J. Wysocki  *	metadata, as a continuous sequence of bytes with the help of
118fb13a28bSRafael J. Wysocki  *	snapshot_read_next() and snapshot_write_next().
119fb13a28bSRafael J. Wysocki  *
120fb13a28bSRafael J. Wysocki  *	The code that writes the image to a storage or transfers it to
121fb13a28bSRafael J. Wysocki  *	the user land is required to use snapshot_read_next() for this
122fb13a28bSRafael J. Wysocki  *	purpose and it should not make any assumptions regarding the internal
123fb13a28bSRafael J. Wysocki  *	structure of the image.  Similarly, the code that reads the image from
124fb13a28bSRafael J. Wysocki  *	a storage or transfers it from the user land is required to use
125fb13a28bSRafael J. Wysocki  *	snapshot_write_next().
126fb13a28bSRafael J. Wysocki  *
127fb13a28bSRafael J. Wysocki  *	This may allow us to change the internal structure of the image
128fb13a28bSRafael J. Wysocki  *	in the future with considerably less effort.
129fb13a28bSRafael J. Wysocki  */
130fb13a28bSRafael J. Wysocki 
131f577eb30SRafael J. Wysocki struct snapshot_handle {
132fb13a28bSRafael J. Wysocki 	unsigned int	cur;	/* number of the block of PAGE_SIZE bytes the
133fb13a28bSRafael J. Wysocki 				 * next operation will refer to (ie. current)
134fb13a28bSRafael J. Wysocki 				 */
135fb13a28bSRafael J. Wysocki 	void		*buffer;	/* address of the block to read from
136fb13a28bSRafael J. Wysocki 					 * or write to
137fb13a28bSRafael J. Wysocki 					 */
138fb13a28bSRafael J. Wysocki 	int		sync_read;	/* Set to one to notify the caller of
139fb13a28bSRafael J. Wysocki 					 * snapshot_write_next() that it may
140fb13a28bSRafael J. Wysocki 					 * need to call wait_on_bio_chain()
141fb13a28bSRafael J. Wysocki 					 */
142f577eb30SRafael J. Wysocki };
143f577eb30SRafael J. Wysocki 
144fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of
145fb13a28bSRafael J. Wysocki  * snapshot_read_next()/snapshot_write_next() is allowed to
146fb13a28bSRafael J. Wysocki  * read/write data after the function returns
147fb13a28bSRafael J. Wysocki  */
148d3c1b24cSJiri Slaby #define data_of(handle)	((handle).buffer)
149f577eb30SRafael J. Wysocki 
150b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone);
151af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void);
152d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle);
153d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle);
1548357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle);
155b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle);
15661159a31SRafael J. Wysocki 
157ab7e9b06SDomenico Andreoli extern bool hibernate_acquire(void);
158ab7e9b06SDomenico Andreoli extern void hibernate_release(void);
1590709db60SRafael J. Wysocki 
160d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap);
161d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap);
162d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void);
16361159a31SRafael J. Wysocki 
164a634cc10SRafael J. Wysocki /*
165a634cc10SRafael J. Wysocki  * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
166a634cc10SRafael J. Wysocki  * the image header.
167a634cc10SRafael J. Wysocki  */
168a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE	1
169f996fc96SBojan Smojver #define SF_NOCOMPRESS_MODE	2
170081a9d04SBojan Smojver #define SF_CRC32_MODE	        4
171a634cc10SRafael J. Wysocki 
1728b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
17374c7e2efSRandy Dunlap extern int swsusp_check(void);
17474c7e2efSRandy Dunlap extern void swsusp_free(void);
175a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p);
176a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags);
177c2dd0daeSAl Viro extern void swsusp_close(fmode_t);
17862c552ccSBojan Smojver #ifdef CONFIG_SUSPEND
17962c552ccSBojan Smojver extern int swsusp_unmark(void);
18062c552ccSBojan Smojver #endif
1810d3a9abeSRafael J. Wysocki 
18275d319c0SArnd Bergmann struct __kernel_old_timeval;
183b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */
184db597605STina Ruchandani extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
185b10d9117SRafael J. Wysocki 
186296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND
187a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */
188406e7938SRafael J. Wysocki extern const char * const pm_labels[];
189d431cbc5SRafael J. Wysocki extern const char *pm_states[];
190406e7938SRafael J. Wysocki extern const char *mem_sleep_states[];
191a9d70523SRafael J. Wysocki 
1926c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state);
193296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
194406e7938SRafael J. Wysocki #define mem_sleep_current	PM_SUSPEND_ON
195406e7938SRafael J. Wysocki 
196296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state)
197296699deSRafael J. Wysocki {
198296699deSRafael J. Wysocki 	return -ENOSYS;
199296699deSRafael J. Wysocki }
200296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
201296699deSRafael J. Wysocki 
202a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND
203a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */
204a9d70523SRafael J. Wysocki extern void suspend_test_start(void);
205a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label);
206a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */
207a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {}
208a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {}
209a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */
210a9d70523SRafael J. Wysocki 
21182525756SAlan Stern #ifdef CONFIG_PM_SLEEP
21282525756SAlan Stern /* kernel/power/main.c */
21370d93298SPeter Zijlstra extern int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down);
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 
242e516a1dbSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_DEBUG
2434cc79776SRafael J. Wysocki extern int pm_test_level;
2448e6bcd9fSRafael J. Wysocki #else
2458e6bcd9fSRafael J. Wysocki #define pm_test_level	(TEST_NONE)
2468e6bcd9fSRafael J. Wysocki #endif
247b28f5081SJohannes Berg 
248b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER
249b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
250b28f5081SJohannes Berg {
251379e0be8SSrivatsa S. Bhat 	int error;
252379e0be8SSrivatsa S. Bhat 
253379e0be8SSrivatsa S. Bhat 	error = freeze_processes();
254379e0be8SSrivatsa S. Bhat 	/*
255379e0be8SSrivatsa S. Bhat 	 * freeze_processes() automatically thaws every task if freezing
256379e0be8SSrivatsa S. Bhat 	 * fails. So we need not do anything extra upon error.
257379e0be8SSrivatsa S. Bhat 	 */
258379e0be8SSrivatsa S. Bhat 	if (error)
2596f585f75SRafael J. Wysocki 		return error;
260379e0be8SSrivatsa S. Bhat 
261379e0be8SSrivatsa S. Bhat 	error = freeze_kernel_threads();
262379e0be8SSrivatsa S. Bhat 	/*
263379e0be8SSrivatsa S. Bhat 	 * freeze_kernel_threads() thaws only kernel threads upon freezing
264379e0be8SSrivatsa S. Bhat 	 * failure. So we have to thaw the userspace tasks ourselves.
265379e0be8SSrivatsa S. Bhat 	 */
266379e0be8SSrivatsa S. Bhat 	if (error)
267379e0be8SSrivatsa S. Bhat 		thaw_processes();
268379e0be8SSrivatsa S. Bhat 
269379e0be8SSrivatsa S. Bhat 	return error;
270b28f5081SJohannes Berg }
271b28f5081SJohannes Berg 
272b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
273b28f5081SJohannes Berg {
274b28f5081SJohannes Berg 	thaw_processes();
275b28f5081SJohannes Berg }
276b28f5081SJohannes Berg #else
277b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
278b28f5081SJohannes Berg {
279b28f5081SJohannes Berg 	return 0;
280b28f5081SJohannes Berg }
281b28f5081SJohannes Berg 
282b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
283b28f5081SJohannes Berg {
284b28f5081SJohannes Berg }
285b28f5081SJohannes Berg #endif
2867483b4a4SRafael J. Wysocki 
2877483b4a4SRafael J. Wysocki #ifdef CONFIG_PM_AUTOSLEEP
2887483b4a4SRafael J. Wysocki 
2897483b4a4SRafael J. Wysocki /* kernel/power/autosleep.c */
2907483b4a4SRafael J. Wysocki extern int pm_autosleep_init(void);
2917483b4a4SRafael J. Wysocki extern int pm_autosleep_lock(void);
2927483b4a4SRafael J. Wysocki extern void pm_autosleep_unlock(void);
2937483b4a4SRafael J. Wysocki extern suspend_state_t pm_autosleep_state(void);
2947483b4a4SRafael J. Wysocki extern int pm_autosleep_set_state(suspend_state_t state);
2957483b4a4SRafael J. Wysocki 
2967483b4a4SRafael J. Wysocki #else /* !CONFIG_PM_AUTOSLEEP */
2977483b4a4SRafael J. Wysocki 
2987483b4a4SRafael J. Wysocki static inline int pm_autosleep_init(void) { return 0; }
2997483b4a4SRafael J. Wysocki static inline int pm_autosleep_lock(void) { return 0; }
3007483b4a4SRafael J. Wysocki static inline void pm_autosleep_unlock(void) {}
3017483b4a4SRafael J. Wysocki static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
3027483b4a4SRafael J. Wysocki 
3037483b4a4SRafael J. Wysocki #endif /* !CONFIG_PM_AUTOSLEEP */
304b86ff982SRafael J. Wysocki 
305b86ff982SRafael J. Wysocki #ifdef CONFIG_PM_WAKELOCKS
306b86ff982SRafael J. Wysocki 
307b86ff982SRafael J. Wysocki /* kernel/power/wakelock.c */
308b86ff982SRafael J. Wysocki extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
309b86ff982SRafael J. Wysocki extern int pm_wake_lock(const char *buf);
310b86ff982SRafael J. Wysocki extern int pm_wake_unlock(const char *buf);
311b86ff982SRafael J. Wysocki 
312b86ff982SRafael J. Wysocki #endif /* !CONFIG_PM_WAKELOCKS */
313