xref: /linux/kernel/power/power.h (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg 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 
35d307c4a8SRafael J. Wysocki static inline 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 
7849c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long);
7949c3df6aSVivek Goyal 
801da177e4SLinus Torvalds #define power_attr(_name) \
81386f275fSKay Sievers static struct kobj_attribute _name##_attr = {	\
821da177e4SLinus Torvalds 	.attr	= {				\
831da177e4SLinus Torvalds 		.name = __stringify(_name),	\
841da177e4SLinus Torvalds 		.mode = 0644,			\
851da177e4SLinus Torvalds 	},					\
861da177e4SLinus Torvalds 	.show	= _name##_show,			\
871da177e4SLinus Torvalds 	.store	= _name##_store,		\
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
90a1e9ca69SRafael J. Wysocki #define power_attr_ro(_name) \
91a1e9ca69SRafael J. Wysocki static struct kobj_attribute _name##_attr = {	\
92a1e9ca69SRafael J. Wysocki 	.attr	= {				\
93a1e9ca69SRafael J. Wysocki 		.name = __stringify(_name),	\
94a1e9ca69SRafael J. Wysocki 		.mode = S_IRUGO,		\
95a1e9ca69SRafael J. Wysocki 	},					\
96a1e9ca69SRafael J. Wysocki 	.show	= _name##_show,			\
97a1e9ca69SRafael J. Wysocki }
98a1e9ca69SRafael J. Wysocki 
99853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */
100853609b6SRafael J. Wysocki extern unsigned long image_size;
101ddeb6487SRafael J. Wysocki /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
102ddeb6487SRafael J. Wysocki extern unsigned long reserved_size;
103f577eb30SRafael J. Wysocki extern int in_suspend;
10461159a31SRafael J. Wysocki extern dev_t swsusp_resume_device;
1059a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block;
106f577eb30SRafael J. Wysocki 
10725761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void);
10825761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void);
10925761b6eSRafael J. Wysocki 
11074dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void);
11174dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void);
11264a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void);
113f577eb30SRafael J. Wysocki 
1141ad1410fSAnisse Astier extern void clear_free_pages(void);
1151ad1410fSAnisse Astier 
116fb13a28bSRafael J. Wysocki /**
117fb13a28bSRafael J. Wysocki  *	Auxiliary structure used for reading the snapshot image data and
118fb13a28bSRafael J. Wysocki  *	metadata from and writing them to the list of page backup entries
119fb13a28bSRafael J. Wysocki  *	(PBEs) which is the main data structure of swsusp.
120fb13a28bSRafael J. Wysocki  *
121fb13a28bSRafael J. Wysocki  *	Using struct snapshot_handle we can transfer the image, including its
122fb13a28bSRafael J. Wysocki  *	metadata, as a continuous sequence of bytes with the help of
123fb13a28bSRafael J. Wysocki  *	snapshot_read_next() and snapshot_write_next().
124fb13a28bSRafael J. Wysocki  *
125fb13a28bSRafael J. Wysocki  *	The code that writes the image to a storage or transfers it to
126fb13a28bSRafael J. Wysocki  *	the user land is required to use snapshot_read_next() for this
127fb13a28bSRafael J. Wysocki  *	purpose and it should not make any assumptions regarding the internal
128fb13a28bSRafael J. Wysocki  *	structure of the image.  Similarly, the code that reads the image from
129fb13a28bSRafael J. Wysocki  *	a storage or transfers it from the user land is required to use
130fb13a28bSRafael J. Wysocki  *	snapshot_write_next().
131fb13a28bSRafael J. Wysocki  *
132fb13a28bSRafael J. Wysocki  *	This may allow us to change the internal structure of the image
133fb13a28bSRafael J. Wysocki  *	in the future with considerably less effort.
134fb13a28bSRafael J. Wysocki  */
135fb13a28bSRafael J. Wysocki 
136f577eb30SRafael J. Wysocki struct snapshot_handle {
137fb13a28bSRafael J. Wysocki 	unsigned int	cur;	/* number of the block of PAGE_SIZE bytes the
138fb13a28bSRafael J. Wysocki 				 * next operation will refer to (ie. current)
139fb13a28bSRafael J. Wysocki 				 */
140fb13a28bSRafael J. Wysocki 	void		*buffer;	/* address of the block to read from
141fb13a28bSRafael J. Wysocki 					 * or write to
142fb13a28bSRafael J. Wysocki 					 */
143fb13a28bSRafael J. Wysocki 	int		sync_read;	/* Set to one to notify the caller of
144fb13a28bSRafael J. Wysocki 					 * snapshot_write_next() that it may
145fb13a28bSRafael J. Wysocki 					 * need to call wait_on_bio_chain()
146fb13a28bSRafael J. Wysocki 					 */
147f577eb30SRafael J. Wysocki };
148f577eb30SRafael J. Wysocki 
149fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of
150fb13a28bSRafael J. Wysocki  * snapshot_read_next()/snapshot_write_next() is allowed to
151fb13a28bSRafael J. Wysocki  * read/write data after the function returns
152fb13a28bSRafael J. Wysocki  */
153d3c1b24cSJiri Slaby #define data_of(handle)	((handle).buffer)
154f577eb30SRafael J. Wysocki 
155b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone);
156af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void);
157d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle);
158d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle);
1598357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle);
160b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle);
16161159a31SRafael J. Wysocki 
1620709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */
1630709db60SRafael J. Wysocki extern atomic_t snapshot_device_available;
1640709db60SRafael J. Wysocki 
165d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap);
166d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap);
167d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void);
16861159a31SRafael J. Wysocki 
169a634cc10SRafael J. Wysocki /*
170a634cc10SRafael J. Wysocki  * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
171a634cc10SRafael J. Wysocki  * the image header.
172a634cc10SRafael J. Wysocki  */
173a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE	1
174f996fc96SBojan Smojver #define SF_NOCOMPRESS_MODE	2
175081a9d04SBojan Smojver #define SF_CRC32_MODE	        4
176a634cc10SRafael J. Wysocki 
1778b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
17874c7e2efSRandy Dunlap extern int swsusp_check(void);
17974c7e2efSRandy Dunlap extern void swsusp_free(void);
180a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p);
181a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags);
182c2dd0daeSAl Viro extern void swsusp_close(fmode_t);
18362c552ccSBojan Smojver #ifdef CONFIG_SUSPEND
18462c552ccSBojan Smojver extern int swsusp_unmark(void);
18562c552ccSBojan Smojver #endif
1860d3a9abeSRafael J. Wysocki 
1870d3a9abeSRafael J. Wysocki struct timeval;
188b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */
189db597605STina Ruchandani extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
190b10d9117SRafael J. Wysocki 
191296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND
192a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */
193406e7938SRafael J. Wysocki extern const char * const pm_labels[];
194d431cbc5SRafael J. Wysocki extern const char *pm_states[];
195406e7938SRafael J. Wysocki extern const char *mem_sleep_states[];
196a9d70523SRafael J. Wysocki 
1976c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state);
198296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
199406e7938SRafael J. Wysocki #define mem_sleep_current	PM_SUSPEND_ON
200406e7938SRafael J. Wysocki 
201296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state)
202296699deSRafael J. Wysocki {
203296699deSRafael J. Wysocki 	return -ENOSYS;
204296699deSRafael J. Wysocki }
205296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
206296699deSRafael J. Wysocki 
207a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND
208a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */
209a9d70523SRafael J. Wysocki extern void suspend_test_start(void);
210a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label);
211a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */
212a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {}
213a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {}
214a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */
215a9d70523SRafael J. Wysocki 
21682525756SAlan Stern #ifdef CONFIG_PM_SLEEP
21782525756SAlan Stern /* kernel/power/main.c */
218ea00f4f4SLianwei Wang extern int __pm_notifier_call_chain(unsigned long val, int nr_to_call,
219ea00f4f4SLianwei Wang 				    int *nr_calls);
22082525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val);
22182525756SAlan Stern #endif
2222f8ed1c6SAdrian Bunk 
2232f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM
2242f8ed1c6SAdrian Bunk int restore_highmem(void);
2252f8ed1c6SAdrian Bunk #else
2262f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; }
2272f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; }
2282f8ed1c6SAdrian Bunk #endif
2290e7d56e3SRafael J. Wysocki 
2300e7d56e3SRafael J. Wysocki /*
2310e7d56e3SRafael J. Wysocki  * Suspend test levels
2320e7d56e3SRafael J. Wysocki  */
2330e7d56e3SRafael J. Wysocki enum {
2340e7d56e3SRafael J. Wysocki 	/* keep first */
2350e7d56e3SRafael J. Wysocki 	TEST_NONE,
2360e7d56e3SRafael J. Wysocki 	TEST_CORE,
2370e7d56e3SRafael J. Wysocki 	TEST_CPUS,
2380e7d56e3SRafael J. Wysocki 	TEST_PLATFORM,
2390e7d56e3SRafael J. Wysocki 	TEST_DEVICES,
2400e7d56e3SRafael J. Wysocki 	TEST_FREEZER,
2410e7d56e3SRafael J. Wysocki 	/* keep last */
2420e7d56e3SRafael J. Wysocki 	__TEST_AFTER_LAST
2430e7d56e3SRafael J. Wysocki };
2440e7d56e3SRafael J. Wysocki 
2450e7d56e3SRafael J. Wysocki #define TEST_FIRST	TEST_NONE
2460e7d56e3SRafael J. Wysocki #define TEST_MAX	(__TEST_AFTER_LAST - 1)
2474cc79776SRafael J. Wysocki 
248e516a1dbSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_DEBUG
2494cc79776SRafael J. Wysocki extern int pm_test_level;
2508e6bcd9fSRafael J. Wysocki #else
2518e6bcd9fSRafael J. Wysocki #define pm_test_level	(TEST_NONE)
2528e6bcd9fSRafael J. Wysocki #endif
253b28f5081SJohannes Berg 
254b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER
255b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
256b28f5081SJohannes Berg {
257379e0be8SSrivatsa S. Bhat 	int error;
258379e0be8SSrivatsa S. Bhat 
259379e0be8SSrivatsa S. Bhat 	error = freeze_processes();
260379e0be8SSrivatsa S. Bhat 	/*
261379e0be8SSrivatsa S. Bhat 	 * freeze_processes() automatically thaws every task if freezing
262379e0be8SSrivatsa S. Bhat 	 * fails. So we need not do anything extra upon error.
263379e0be8SSrivatsa S. Bhat 	 */
264379e0be8SSrivatsa S. Bhat 	if (error)
2656f585f75SRafael J. Wysocki 		return error;
266379e0be8SSrivatsa S. Bhat 
267379e0be8SSrivatsa S. Bhat 	error = freeze_kernel_threads();
268379e0be8SSrivatsa S. Bhat 	/*
269379e0be8SSrivatsa S. Bhat 	 * freeze_kernel_threads() thaws only kernel threads upon freezing
270379e0be8SSrivatsa S. Bhat 	 * failure. So we have to thaw the userspace tasks ourselves.
271379e0be8SSrivatsa S. Bhat 	 */
272379e0be8SSrivatsa S. Bhat 	if (error)
273379e0be8SSrivatsa S. Bhat 		thaw_processes();
274379e0be8SSrivatsa S. Bhat 
275379e0be8SSrivatsa S. Bhat 	return error;
276b28f5081SJohannes Berg }
277b28f5081SJohannes Berg 
278b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
279b28f5081SJohannes Berg {
280b28f5081SJohannes Berg 	thaw_processes();
281b28f5081SJohannes Berg }
282b28f5081SJohannes Berg #else
283b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
284b28f5081SJohannes Berg {
285b28f5081SJohannes Berg 	return 0;
286b28f5081SJohannes Berg }
287b28f5081SJohannes Berg 
288b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
289b28f5081SJohannes Berg {
290b28f5081SJohannes Berg }
291b28f5081SJohannes Berg #endif
2927483b4a4SRafael J. Wysocki 
2937483b4a4SRafael J. Wysocki #ifdef CONFIG_PM_AUTOSLEEP
2947483b4a4SRafael J. Wysocki 
2957483b4a4SRafael J. Wysocki /* kernel/power/autosleep.c */
2967483b4a4SRafael J. Wysocki extern int pm_autosleep_init(void);
2977483b4a4SRafael J. Wysocki extern int pm_autosleep_lock(void);
2987483b4a4SRafael J. Wysocki extern void pm_autosleep_unlock(void);
2997483b4a4SRafael J. Wysocki extern suspend_state_t pm_autosleep_state(void);
3007483b4a4SRafael J. Wysocki extern int pm_autosleep_set_state(suspend_state_t state);
3017483b4a4SRafael J. Wysocki 
3027483b4a4SRafael J. Wysocki #else /* !CONFIG_PM_AUTOSLEEP */
3037483b4a4SRafael J. Wysocki 
3047483b4a4SRafael J. Wysocki static inline int pm_autosleep_init(void) { return 0; }
3057483b4a4SRafael J. Wysocki static inline int pm_autosleep_lock(void) { return 0; }
3067483b4a4SRafael J. Wysocki static inline void pm_autosleep_unlock(void) {}
3077483b4a4SRafael J. Wysocki static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
3087483b4a4SRafael J. Wysocki 
3097483b4a4SRafael J. Wysocki #endif /* !CONFIG_PM_AUTOSLEEP */
310b86ff982SRafael J. Wysocki 
311b86ff982SRafael J. Wysocki #ifdef CONFIG_PM_WAKELOCKS
312b86ff982SRafael J. Wysocki 
313b86ff982SRafael J. Wysocki /* kernel/power/wakelock.c */
314b86ff982SRafael J. Wysocki extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
315b86ff982SRafael J. Wysocki extern int pm_wake_lock(const char *buf);
316b86ff982SRafael J. Wysocki extern int pm_wake_unlock(const char *buf);
317b86ff982SRafael J. Wysocki 
318b86ff982SRafael J. Wysocki #endif /* !CONFIG_PM_WAKELOCKS */
319