xref: /linux/kernel/power/power.h (revision a79a588fc1761dc12a3064fc2f648ae66cea3c5a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/suspend.h>
3 #include <linux/suspend_ioctls.h>
4 #include <linux/utsname.h>
5 #include <linux/freezer.h>
6 #include <linux/compiler.h>
7 #include <linux/cpu.h>
8 #include <linux/cpuidle.h>
9 #include <linux/crypto.h>
10 
11 struct swsusp_info {
12 	struct new_utsname	uts;
13 	u32			version_code;
14 	unsigned long		num_physpages;
15 	int			cpus;
16 	unsigned long		image_pages;
17 	unsigned long		pages;
18 	unsigned long		size;
19 } __aligned(PAGE_SIZE);
20 
21 #if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
22 extern bool filesystem_freeze_enabled;
23 #endif
24 
25 #ifdef CONFIG_HIBERNATION
26 /* kernel/power/snapshot.c */
27 extern void __init hibernate_reserved_size_init(void);
28 extern void __init hibernate_image_size_init(void);
29 
30 #ifdef CONFIG_ARCH_HIBERNATION_HEADER
31 /* Maximum size of architecture specific data in a hibernation header */
32 #define MAX_ARCH_HEADER_SIZE	(sizeof(struct new_utsname) + 4)
33 
init_header_complete(struct swsusp_info * info)34 static inline int init_header_complete(struct swsusp_info *info)
35 {
36 	return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
37 }
38 
check_image_kernel(struct swsusp_info * info)39 static inline const char *check_image_kernel(struct swsusp_info *info)
40 {
41 	return arch_hibernation_header_restore(info) ?
42 			"architecture specific data" : NULL;
43 }
44 #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
45 
46 /*
47  * Keep some memory free so that I/O operations can succeed without paging
48  * [Might this be more than 4 MB?]
49  */
50 #define PAGES_FOR_IO	((4096 * 1024) >> PAGE_SHIFT)
51 
52 /*
53  * Keep 1 MB of memory free so that device drivers can allocate some pages in
54  * their .suspend() routines without breaking the suspend to disk.
55  */
56 #define SPARE_PAGES	((1024 * 1024) >> PAGE_SHIFT)
57 
58 asmlinkage int swsusp_save(void);
59 
60 /* kernel/power/hibernate.c */
61 extern bool freezer_test_done;
62 extern char hib_comp_algo[CRYPTO_MAX_ALG_NAME];
63 
64 /* kernel/power/swap.c */
65 extern unsigned int swsusp_header_flags;
66 
67 extern int hibernation_snapshot(int platform_mode);
68 extern int hibernation_restore(int platform_mode);
69 extern int hibernation_platform_enter(void);
70 
71 #ifdef CONFIG_STRICT_KERNEL_RWX
72 /* kernel/power/snapshot.c */
73 extern void enable_restore_image_protection(void);
74 #else
enable_restore_image_protection(void)75 static inline void enable_restore_image_protection(void) {}
76 #endif /* CONFIG_STRICT_KERNEL_RWX */
77 
78 extern bool hibernation_in_progress(void);
79 
80 #else /* !CONFIG_HIBERNATION */
81 
hibernate_reserved_size_init(void)82 static inline void hibernate_reserved_size_init(void) {}
hibernate_image_size_init(void)83 static inline void hibernate_image_size_init(void) {}
84 
hibernation_in_progress(void)85 static inline bool hibernation_in_progress(void) { return false; }
86 #endif /* !CONFIG_HIBERNATION */
87 
88 #define power_attr(_name) \
89 static struct kobj_attribute _name##_attr = {	\
90 	.attr	= {				\
91 		.name = __stringify(_name),	\
92 		.mode = 0644,			\
93 	},					\
94 	.show	= _name##_show,			\
95 	.store	= _name##_store,		\
96 }
97 
98 #define power_attr_ro(_name) \
99 static struct kobj_attribute _name##_attr = {	\
100 	.attr	= {				\
101 		.name = __stringify(_name),	\
102 		.mode = S_IRUGO,		\
103 	},					\
104 	.show	= _name##_show,			\
105 }
106 
107 /* Preferred image size in bytes (default 500 MB) */
108 extern unsigned long image_size;
109 /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
110 extern unsigned long reserved_size;
111 extern int in_suspend;
112 extern dev_t swsusp_resume_device;
113 extern sector_t swsusp_resume_block;
114 
115 extern int create_basic_memory_bitmaps(void);
116 extern void free_basic_memory_bitmaps(void);
117 extern int hibernate_preallocate_memory(void);
118 
119 extern void clear_or_poison_free_pages(void);
120 
121 /*
122  *	Auxiliary structure used for reading the snapshot image data and
123  *	metadata from and writing them to the list of page backup entries
124  *	(PBEs) which is the main data structure of swsusp.
125  *
126  *	Using struct snapshot_handle we can transfer the image, including its
127  *	metadata, as a continuous sequence of bytes with the help of
128  *	snapshot_read_next() and snapshot_write_next().
129  *
130  *	The code that writes the image to a storage or transfers it to
131  *	the user land is required to use snapshot_read_next() for this
132  *	purpose and it should not make any assumptions regarding the internal
133  *	structure of the image.  Similarly, the code that reads the image from
134  *	a storage or transfers it from the user land is required to use
135  *	snapshot_write_next().
136  *
137  *	This may allow us to change the internal structure of the image
138  *	in the future with considerably less effort.
139  */
140 
141 struct snapshot_handle {
142 	unsigned int	cur;	/* number of the block of PAGE_SIZE bytes the
143 				 * next operation will refer to (ie. current)
144 				 */
145 	void		*buffer;	/* address of the block to read from
146 					 * or write to
147 					 */
148 	int		sync_read;	/* Set to one to notify the caller of
149 					 * snapshot_write_next() that it may
150 					 * need to call wait_on_bio_chain()
151 					 */
152 };
153 
154 /* This macro returns the address from/to which the caller of
155  * snapshot_read_next()/snapshot_write_next() is allowed to
156  * read/write data after the function returns
157  */
158 #define data_of(handle)	((handle).buffer)
159 
160 extern unsigned int snapshot_additional_pages(struct zone *zone);
161 extern unsigned long snapshot_get_image_size(void);
162 extern int snapshot_read_next(struct snapshot_handle *handle);
163 extern int snapshot_write_next(struct snapshot_handle *handle);
164 int snapshot_write_finalize(struct snapshot_handle *handle);
165 extern int snapshot_image_loaded(struct snapshot_handle *handle);
166 
167 extern bool hibernate_acquire(void);
168 extern void hibernate_release(void);
169 
170 extern sector_t alloc_swapdev_block(int swap);
171 extern void free_all_swap_pages(int swap);
172 extern int swsusp_swap_in_use(void);
173 
174 /*
175  * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
176  * the image header.
177  */
178 #define SF_COMPRESSION_ALG_LZO	0 /* dummy, details given  below */
179 #define SF_PLATFORM_MODE	1
180 #define SF_NOCOMPRESS_MODE	2
181 #define SF_CRC32_MODE	        4
182 #define SF_HW_SIG		8
183 
184 /*
185  * Bit to indicate the compression algorithm to be used(for LZ4). The same
186  * could be checked while saving/loading image to/from disk to use the
187  * corresponding algorithms.
188  *
189  * By default, LZO compression is enabled if SF_CRC32_MODE is set. Use
190  * SF_COMPRESSION_ALG_LZ4 to override this behaviour and use LZ4.
191  *
192  * SF_CRC32_MODE, SF_COMPRESSION_ALG_LZO(dummy) -> Compression, LZO
193  * SF_CRC32_MODE, SF_COMPRESSION_ALG_LZ4 -> Compression, LZ4
194  */
195 #define SF_COMPRESSION_ALG_LZ4	16
196 
197 /* kernel/power/hibernate.c */
198 int swsusp_check(bool exclusive);
199 extern void swsusp_free(void);
200 extern int swsusp_read(unsigned int *flags_p);
201 extern int swsusp_write(unsigned int flags);
202 void swsusp_close(void);
203 #ifdef CONFIG_SUSPEND
204 extern int swsusp_unmark(void);
205 #else
swsusp_unmark(void)206 static inline int swsusp_unmark(void) { return 0; }
207 #endif
208 
209 struct __kernel_old_timeval;
210 /* kernel/power/swsusp.c */
211 extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
212 
213 #ifdef CONFIG_SUSPEND
214 /* kernel/power/suspend.c */
215 extern const char * const pm_labels[];
216 extern const char *pm_states[];
217 extern const char *mem_sleep_states[];
218 
219 extern int suspend_devices_and_enter(suspend_state_t state);
220 #else /* !CONFIG_SUSPEND */
221 #define mem_sleep_current	PM_SUSPEND_ON
222 
suspend_devices_and_enter(suspend_state_t state)223 static inline int suspend_devices_and_enter(suspend_state_t state)
224 {
225 	return -ENOSYS;
226 }
227 #endif /* !CONFIG_SUSPEND */
228 
229 #ifdef CONFIG_PM_TEST_SUSPEND
230 /* kernel/power/suspend_test.c */
231 extern void suspend_test_start(void);
232 extern void suspend_test_finish(const char *label);
233 #else /* !CONFIG_PM_TEST_SUSPEND */
suspend_test_start(void)234 static inline void suspend_test_start(void) {}
suspend_test_finish(const char * label)235 static inline void suspend_test_finish(const char *label) {}
236 #endif /* !CONFIG_PM_TEST_SUSPEND */
237 
238 #ifdef CONFIG_PM_SLEEP
239 /* kernel/power/main.c */
240 extern int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down);
241 extern int pm_notifier_call_chain(unsigned long val);
242 #endif
243 
244 #ifdef CONFIG_HIGHMEM
245 int restore_highmem(void);
246 #else
count_highmem_pages(void)247 static inline unsigned int count_highmem_pages(void) { return 0; }
restore_highmem(void)248 static inline int restore_highmem(void) { return 0; }
249 #endif
250 
251 /*
252  * Suspend test levels
253  */
254 enum {
255 	/* keep first */
256 	TEST_NONE,
257 	TEST_CORE,
258 	TEST_CPUS,
259 	TEST_PLATFORM,
260 	TEST_DEVICES,
261 	TEST_FREEZER,
262 	/* keep last */
263 	__TEST_AFTER_LAST
264 };
265 
266 #define TEST_FIRST	TEST_NONE
267 #define TEST_MAX	(__TEST_AFTER_LAST - 1)
268 
269 #ifdef CONFIG_PM_SLEEP_DEBUG
270 extern int pm_test_level;
271 #else
272 #define pm_test_level	(TEST_NONE)
273 #endif
274 
275 #ifdef CONFIG_SUSPEND_FREEZER
suspend_freeze_processes(void)276 static inline int suspend_freeze_processes(void)
277 {
278 	int error;
279 
280 	error = freeze_processes();
281 	/*
282 	 * freeze_processes() automatically thaws every task if freezing
283 	 * fails. So we need not do anything extra upon error.
284 	 */
285 	if (error)
286 		return error;
287 
288 	error = freeze_kernel_threads();
289 	/*
290 	 * freeze_kernel_threads() thaws only kernel threads upon freezing
291 	 * failure. So we have to thaw the userspace tasks ourselves.
292 	 */
293 	if (error)
294 		thaw_processes();
295 
296 	return error;
297 }
298 
suspend_thaw_processes(void)299 static inline void suspend_thaw_processes(void)
300 {
301 	thaw_processes();
302 }
303 #else
suspend_freeze_processes(void)304 static inline int suspend_freeze_processes(void)
305 {
306 	return 0;
307 }
308 
suspend_thaw_processes(void)309 static inline void suspend_thaw_processes(void)
310 {
311 }
312 #endif
313 
314 #ifdef CONFIG_PM_AUTOSLEEP
315 
316 /* kernel/power/autosleep.c */
317 extern int pm_autosleep_init(void);
318 extern int pm_autosleep_lock(void);
319 extern void pm_autosleep_unlock(void);
320 extern suspend_state_t pm_autosleep_state(void);
321 extern int pm_autosleep_set_state(suspend_state_t state);
322 
323 #else /* !CONFIG_PM_AUTOSLEEP */
324 
pm_autosleep_init(void)325 static inline int pm_autosleep_init(void) { return 0; }
pm_autosleep_lock(void)326 static inline int pm_autosleep_lock(void) { return 0; }
pm_autosleep_unlock(void)327 static inline void pm_autosleep_unlock(void) {}
pm_autosleep_state(void)328 static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
329 
330 #endif /* !CONFIG_PM_AUTOSLEEP */
331 
332 #ifdef CONFIG_PM_WAKELOCKS
333 
334 /* kernel/power/wakelock.c */
335 extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
336 extern int pm_wake_lock(const char *buf);
337 extern int pm_wake_unlock(const char *buf);
338 
339 #endif /* !CONFIG_PM_WAKELOCKS */
340 
pm_sleep_disable_secondary_cpus(void)341 static inline int pm_sleep_disable_secondary_cpus(void)
342 {
343 	cpuidle_pause();
344 	return suspend_disable_secondary_cpus();
345 }
346 
pm_sleep_enable_secondary_cpus(void)347 static inline void pm_sleep_enable_secondary_cpus(void)
348 {
349 	suspend_enable_secondary_cpus();
350 	cpuidle_resume();
351 }
352 
353 void dpm_save_errno(int err);
354