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
11struct 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)
22extern bool filesystem_freeze_enabled;
23#endif
24
25#ifdef CONFIG_HIBERNATION
26/* kernel/power/snapshot.c */
27extern void __init hibernate_reserved_size_init(void);
28extern 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
34static inline int init_header_complete(struct swsusp_info *info)
35{
36 return arch_hibernation_header_save(addr: info, MAX_ARCH_HEADER_SIZE);
37}
38
39static inline const char *check_image_kernel(struct swsusp_info *info)
40{
41 return arch_hibernation_header_restore(addr: 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
58asmlinkage int swsusp_save(void);
59
60/* kernel/power/hibernate.c */
61extern bool freezer_test_done;
62extern char hib_comp_algo[CRYPTO_MAX_ALG_NAME];
63
64/* kernel/power/swap.c */
65extern unsigned int swsusp_header_flags;
66
67extern int hibernation_snapshot(int platform_mode);
68extern int hibernation_restore(int platform_mode);
69extern int hibernation_platform_enter(void);
70
71#ifdef CONFIG_STRICT_KERNEL_RWX
72/* kernel/power/snapshot.c */
73extern void enable_restore_image_protection(void);
74#else
75static inline void enable_restore_image_protection(void) {}
76#endif /* CONFIG_STRICT_KERNEL_RWX */
77
78extern bool hibernation_in_progress(void);
79
80#else /* !CONFIG_HIBERNATION */
81
82static inline void hibernate_reserved_size_init(void) {}
83static inline void hibernate_image_size_init(void) {}
84
85static inline bool hibernation_in_progress(void) { return false; }
86#endif /* !CONFIG_HIBERNATION */
87
88#define power_attr(_name) \
89static 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) \
99static 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) */
108extern unsigned long image_size;
109/* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
110extern unsigned long reserved_size;
111extern int in_suspend;
112extern dev_t swsusp_resume_device;
113extern sector_t swsusp_resume_block;
114
115extern int create_basic_memory_bitmaps(void);
116extern void free_basic_memory_bitmaps(void);
117extern int hibernate_preallocate_memory(void);
118
119extern 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
141struct 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
160extern unsigned int snapshot_additional_pages(struct zone *zone);
161extern unsigned long snapshot_get_image_size(void);
162extern int snapshot_read_next(struct snapshot_handle *handle);
163extern int snapshot_write_next(struct snapshot_handle *handle);
164int snapshot_write_finalize(struct snapshot_handle *handle);
165extern int snapshot_image_loaded(struct snapshot_handle *handle);
166
167extern bool hibernate_acquire(void);
168extern void hibernate_release(void);
169
170extern sector_t alloc_swapdev_block(int swap);
171extern void free_all_swap_pages(int swap);
172extern 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 */
198int swsusp_check(bool exclusive);
199extern void swsusp_free(void);
200extern int swsusp_read(unsigned int *flags_p);
201extern int swsusp_write(unsigned int flags);
202void swsusp_close(void);
203#ifdef CONFIG_SUSPEND
204extern int swsusp_unmark(void);
205#else
206static inline int swsusp_unmark(void) { return 0; }
207#endif
208
209struct __kernel_old_timeval;
210/* kernel/power/swsusp.c */
211extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
212
213#ifdef CONFIG_SUSPEND
214/* kernel/power/suspend.c */
215extern const char * const pm_labels[];
216extern const char *pm_states[];
217extern const char *mem_sleep_states[];
218
219extern int suspend_devices_and_enter(suspend_state_t state);
220#else /* !CONFIG_SUSPEND */
221#define mem_sleep_current PM_SUSPEND_ON
222
223static 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 */
231extern void suspend_test_start(void);
232extern void suspend_test_finish(const char *label);
233#else /* !CONFIG_PM_TEST_SUSPEND */
234static inline void suspend_test_start(void) {}
235static 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 */
240extern int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down);
241extern int pm_notifier_call_chain(unsigned long val);
242#endif
243
244#ifdef CONFIG_HIGHMEM
245int restore_highmem(void);
246#else
247static inline unsigned int count_highmem_pages(void) { return 0; }
248static inline int restore_highmem(void) { return 0; }
249#endif
250
251/*
252 * Suspend test levels
253 */
254enum {
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
270extern int pm_test_level;
271#else
272#define pm_test_level (TEST_NONE)
273#endif
274
275#ifdef CONFIG_SUSPEND_FREEZER
276static 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
299static inline void suspend_thaw_processes(void)
300{
301 thaw_processes();
302}
303#else
304static inline int suspend_freeze_processes(void)
305{
306 return 0;
307}
308
309static inline void suspend_thaw_processes(void)
310{
311}
312#endif
313
314#ifdef CONFIG_PM_AUTOSLEEP
315
316/* kernel/power/autosleep.c */
317extern int pm_autosleep_init(void);
318extern int pm_autosleep_lock(void);
319extern void pm_autosleep_unlock(void);
320extern suspend_state_t pm_autosleep_state(void);
321extern int pm_autosleep_set_state(suspend_state_t state);
322
323#else /* !CONFIG_PM_AUTOSLEEP */
324
325static inline int pm_autosleep_init(void) { return 0; }
326static inline int pm_autosleep_lock(void) { return 0; }
327static inline void pm_autosleep_unlock(void) {}
328static 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 */
335extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
336extern int pm_wake_lock(const char *buf);
337extern int pm_wake_unlock(const char *buf);
338
339#endif /* !CONFIG_PM_WAKELOCKS */
340
341static inline int pm_sleep_disable_secondary_cpus(void)
342{
343 cpuidle_pause();
344 return suspend_disable_secondary_cpus();
345}
346
347static inline void pm_sleep_enable_secondary_cpus(void)
348{
349 suspend_enable_secondary_cpus();
350 cpuidle_resume();
351}
352
353void dpm_save_errno(int err);
354