blob: 44bee462ff5791ab2408da2b8b5a6d3b4065b6ea [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/suspend.h>
Rafael J. Wysocki3010f8c2007-10-26 01:05:05 +02003#include <linux/suspend_ioctls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/utsname.h>
Johannes Bergb28f5082008-01-15 23:17:00 -05005#include <linux/freezer.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07006#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008struct swsusp_info {
9 struct new_utsname uts;
10 u32 version_code;
11 unsigned long num_physpages;
12 int cpus;
13 unsigned long image_pages;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080014 unsigned long pages;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080015 unsigned long size;
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070016} __aligned(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +020018#ifdef CONFIG_HIBERNATION
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +020019/* kernel/power/snapshot.c */
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +020020extern void __init hibernate_reserved_size_init(void);
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +020021extern void __init hibernate_image_size_init(void);
22
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -070023#ifdef CONFIG_ARCH_HIBERNATION_HEADER
24/* Maximum size of architecture specific data in a hibernation header */
25#define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4)
26
27extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
28extern int arch_hibernation_header_restore(void *addr);
29
30static inline int init_header_complete(struct swsusp_info *info)
31{
32 return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
33}
34
35static inline char *check_image_kernel(struct swsusp_info *info)
36{
37 return arch_hibernation_header_restore(info) ?
38 "architecture specific data" : NULL;
39}
40#endif /* CONFIG_ARCH_HIBERNATION_HEADER */
41
Rafael J. Wysocki406f9922016-07-14 03:55:23 +020042extern int hibernate_resume_nonboot_cpu_disable(void);
43
Rafael J. Wysocki56f99bc2007-05-06 14:50:52 -070044/*
45 * Keep some memory free so that I/O operations can succeed without paging
46 * [Might this be more than 4 MB?]
47 */
48#define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT)
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -070049
Rafael J. Wysocki56f99bc2007-05-06 14:50:52 -070050/*
51 * Keep 1 MB of memory free so that device drivers can allocate some pages in
52 * their .suspend() routines without breaking the suspend to disk.
53 */
54#define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Rashika Kheria04b73462014-02-27 17:13:53 +053056asmlinkage int swsusp_save(void);
57
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +020058/* kernel/power/hibernate.c */
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010059extern bool freezer_test_done;
60
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070061extern int hibernation_snapshot(int platform_mode);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -070062extern int hibernation_restore(int platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070063extern int hibernation_platform_enter(void);
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +020064
Laura Abbott0f5bf6d2017-02-06 16:31:58 -080065#ifdef CONFIG_STRICT_KERNEL_RWX
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +020066/* kernel/power/snapshot.c */
67extern void enable_restore_image_protection(void);
68#else
69static inline void enable_restore_image_protection(void) {}
Laura Abbott0f5bf6d2017-02-06 16:31:58 -080070#endif /* CONFIG_STRICT_KERNEL_RWX */
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +020071
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +020072#else /* !CONFIG_HIBERNATION */
73
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +020074static inline void hibernate_reserved_size_init(void) {}
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +020075static inline void hibernate_image_size_init(void) {}
76#endif /* !CONFIG_HIBERNATION */
Stephen Hemmingera6d70982006-12-06 20:34:35 -080077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define power_attr(_name) \
Kay Sievers386f2752007-11-02 13:47:53 +010079static struct kobj_attribute _name##_attr = { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .attr = { \
81 .name = __stringify(_name), \
82 .mode = 0644, \
83 }, \
84 .show = _name##_show, \
85 .store = _name##_store, \
86}
87
Rafael J. Wysockia1e9ca62016-01-02 03:09:16 +010088#define power_attr_ro(_name) \
89static struct kobj_attribute _name##_attr = { \
90 .attr = { \
91 .name = __stringify(_name), \
92 .mode = S_IRUGO, \
93 }, \
94 .show = _name##_show, \
95}
96
Rafael J. Wysocki853609b2006-02-01 03:05:07 -080097/* Preferred image size in bytes (default 500 MB) */
98extern unsigned long image_size;
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +020099/* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
100extern unsigned long reserved_size;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800101extern int in_suspend;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800102extern dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800103extern sector_t swsusp_resume_block;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800104
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700105extern int create_basic_memory_bitmaps(void);
106extern void free_basic_memory_bitmaps(void);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200107extern int hibernate_preallocate_memory(void);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800108
Anisse Astier1ad14102016-09-09 10:43:32 +0200109extern void clear_free_pages(void);
110
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700111/**
112 * Auxiliary structure used for reading the snapshot image data and
113 * metadata from and writing them to the list of page backup entries
114 * (PBEs) which is the main data structure of swsusp.
115 *
116 * Using struct snapshot_handle we can transfer the image, including its
117 * metadata, as a continuous sequence of bytes with the help of
118 * snapshot_read_next() and snapshot_write_next().
119 *
120 * The code that writes the image to a storage or transfers it to
121 * the user land is required to use snapshot_read_next() for this
122 * purpose and it should not make any assumptions regarding the internal
123 * structure of the image. Similarly, the code that reads the image from
124 * a storage or transfers it from the user land is required to use
125 * snapshot_write_next().
126 *
127 * This may allow us to change the internal structure of the image
128 * in the future with considerably less effort.
129 */
130
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800131struct snapshot_handle {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700132 unsigned int cur; /* number of the block of PAGE_SIZE bytes the
133 * next operation will refer to (ie. current)
134 */
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700135 void *buffer; /* address of the block to read from
136 * or write to
137 */
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700138 int sync_read; /* Set to one to notify the caller of
139 * snapshot_write_next() that it may
140 * need to call wait_on_bio_chain()
141 */
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800142};
143
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700144/* This macro returns the address from/to which the caller of
145 * snapshot_read_next()/snapshot_write_next() is allowed to
146 * read/write data after the function returns
147 */
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200148#define data_of(handle) ((handle).buffer)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800149
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700150extern unsigned int snapshot_additional_pages(struct zone *zone);
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +0200151extern unsigned long snapshot_get_image_size(void);
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200152extern int snapshot_read_next(struct snapshot_handle *handle);
153extern int snapshot_write_next(struct snapshot_handle *handle);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800154extern void snapshot_write_finalize(struct snapshot_handle *handle);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700155extern int snapshot_image_loaded(struct snapshot_handle *handle);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800156
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700157/* If unset, the snapshot device cannot be open. */
158extern atomic_t snapshot_device_available;
159
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700160extern sector_t alloc_swapdev_block(int swap);
161extern void free_all_swap_pages(int swap);
162extern int swsusp_swap_in_use(void);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800163
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700164/*
165 * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
166 * the image header.
167 */
168#define SF_PLATFORM_MODE 1
Bojan Smojverf996fc92010-09-09 23:06:23 +0200169#define SF_NOCOMPRESS_MODE 2
Bojan Smojver081a9d02011-10-13 23:58:07 +0200170#define SF_CRC32_MODE 4
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700171
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +0200172/* kernel/power/hibernate.c */
Randy Dunlap74c7e2e2006-03-23 03:00:01 -0800173extern int swsusp_check(void);
Randy Dunlap74c7e2e2006-03-23 03:00:01 -0800174extern void swsusp_free(void);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700175extern int swsusp_read(unsigned int *flags_p);
176extern int swsusp_write(unsigned int flags);
Al Viroc2dd0da2007-10-08 13:21:10 -0400177extern void swsusp_close(fmode_t);
Bojan Smojver62c552c2012-06-16 00:09:58 +0200178#ifdef CONFIG_SUSPEND
179extern int swsusp_unmark(void);
180#endif
Rafael J. Wysocki0d3a9ab2006-12-06 20:34:32 -0800181
182struct timeval;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700183/* kernel/power/swsusp.c */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700184extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700185
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200186#ifdef CONFIG_SUSPEND
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200187/* kernel/power/suspend.c */
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100188extern const char * const pm_labels[];
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200189extern const char *pm_states[];
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100190extern const char *mem_sleep_states[];
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200191
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700192extern int suspend_devices_and_enter(suspend_state_t state);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200193#else /* !CONFIG_SUSPEND */
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100194#define mem_sleep_current PM_SUSPEND_ON
195
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200196static inline int suspend_devices_and_enter(suspend_state_t state)
197{
198 return -ENOSYS;
199}
200#endif /* !CONFIG_SUSPEND */
201
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200202#ifdef CONFIG_PM_TEST_SUSPEND
203/* kernel/power/suspend_test.c */
204extern void suspend_test_start(void);
205extern void suspend_test_finish(const char *label);
206#else /* !CONFIG_PM_TEST_SUSPEND */
207static inline void suspend_test_start(void) {}
208static inline void suspend_test_finish(const char *label) {}
209#endif /* !CONFIG_PM_TEST_SUSPEND */
210
Alan Stern82525752007-11-19 23:49:18 +0100211#ifdef CONFIG_PM_SLEEP
212/* kernel/power/main.c */
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700213extern int __pm_notifier_call_chain(unsigned long val, int nr_to_call,
214 int *nr_calls);
Alan Stern82525752007-11-19 23:49:18 +0100215extern int pm_notifier_call_chain(unsigned long val);
216#endif
Adrian Bunk2f8ed1c2007-11-19 23:36:20 +0100217
218#ifdef CONFIG_HIGHMEM
Adrian Bunk2f8ed1c2007-11-19 23:36:20 +0100219int restore_highmem(void);
220#else
221static inline unsigned int count_highmem_pages(void) { return 0; }
222static inline int restore_highmem(void) { return 0; }
223#endif
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100224
225/*
226 * Suspend test levels
227 */
228enum {
229 /* keep first */
230 TEST_NONE,
231 TEST_CORE,
232 TEST_CPUS,
233 TEST_PLATFORM,
234 TEST_DEVICES,
235 TEST_FREEZER,
236 /* keep last */
237 __TEST_AFTER_LAST
238};
239
240#define TEST_FIRST TEST_NONE
241#define TEST_MAX (__TEST_AFTER_LAST - 1)
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100242
Rafael J. Wysockie516a1d2017-07-21 14:44:02 +0200243#ifdef CONFIG_PM_SLEEP_DEBUG
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100244extern int pm_test_level;
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200245#else
246#define pm_test_level (TEST_NONE)
247#endif
Johannes Bergb28f5082008-01-15 23:17:00 -0500248
249#ifdef CONFIG_SUSPEND_FREEZER
250static inline int suspend_freeze_processes(void)
251{
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100252 int error;
253
254 error = freeze_processes();
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100255 /*
256 * freeze_processes() automatically thaws every task if freezing
257 * fails. So we need not do anything extra upon error.
258 */
259 if (error)
Rafael J. Wysocki6f585f72012-02-11 22:40:23 +0100260 return error;
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100261
262 error = freeze_kernel_threads();
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100263 /*
264 * freeze_kernel_threads() thaws only kernel threads upon freezing
265 * failure. So we have to thaw the userspace tasks ourselves.
266 */
267 if (error)
268 thaw_processes();
269
Srivatsa S. Bhat379e0be2012-02-03 22:22:41 +0100270 return error;
Johannes Bergb28f5082008-01-15 23:17:00 -0500271}
272
273static inline void suspend_thaw_processes(void)
274{
275 thaw_processes();
276}
277#else
278static inline int suspend_freeze_processes(void)
279{
280 return 0;
281}
282
283static inline void suspend_thaw_processes(void)
284{
285}
286#endif
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200287
288#ifdef CONFIG_PM_AUTOSLEEP
289
290/* kernel/power/autosleep.c */
291extern int pm_autosleep_init(void);
292extern int pm_autosleep_lock(void);
293extern void pm_autosleep_unlock(void);
294extern suspend_state_t pm_autosleep_state(void);
295extern int pm_autosleep_set_state(suspend_state_t state);
296
297#else /* !CONFIG_PM_AUTOSLEEP */
298
299static inline int pm_autosleep_init(void) { return 0; }
300static inline int pm_autosleep_lock(void) { return 0; }
301static inline void pm_autosleep_unlock(void) {}
302static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
303
304#endif /* !CONFIG_PM_AUTOSLEEP */
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200305
306#ifdef CONFIG_PM_WAKELOCKS
307
308/* kernel/power/wakelock.c */
309extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
310extern int pm_wake_lock(const char *buf);
311extern int pm_wake_unlock(const char *buf);
312
313#endif /* !CONFIG_PM_WAKELOCKS */