xref: /linux/kernel/power/power.h (revision db597605821fccc49876705aea5db5443d67e53e)
11da177e4SLinus Torvalds #include <linux/suspend.h>
23010f8caSRafael J. Wysocki #include <linux/suspend_ioctls.h>
31da177e4SLinus Torvalds #include <linux/utsname.h>
4b28f5081SJohannes Berg #include <linux/freezer.h>
552f5684cSGideon Israel Dsouza #include <linux/compiler.h>
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds struct swsusp_info {
81da177e4SLinus Torvalds 	struct new_utsname	uts;
91da177e4SLinus Torvalds 	u32			version_code;
101da177e4SLinus Torvalds 	unsigned long		num_physpages;
111da177e4SLinus Torvalds 	int			cpus;
121da177e4SLinus Torvalds 	unsigned long		image_pages;
137088a5c0SRafael J. Wysocki 	unsigned long		pages;
146e1819d6SRafael J. Wysocki 	unsigned long		size;
1552f5684cSGideon Israel Dsouza } __aligned(PAGE_SIZE);
161da177e4SLinus Torvalds 
17b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
18ac5c24ecSRafael J. Wysocki /* kernel/power/snapshot.c */
19ddeb6487SRafael J. Wysocki extern void __init hibernate_reserved_size_init(void);
20ac5c24ecSRafael J. Wysocki extern void __init hibernate_image_size_init(void);
21ac5c24ecSRafael J. Wysocki 
22d307c4a8SRafael J. Wysocki #ifdef CONFIG_ARCH_HIBERNATION_HEADER
23d307c4a8SRafael J. Wysocki /* Maximum size of architecture specific data in a hibernation header */
24d307c4a8SRafael J. Wysocki #define MAX_ARCH_HEADER_SIZE	(sizeof(struct new_utsname) + 4)
25d307c4a8SRafael J. Wysocki 
26d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_save(void *addr, unsigned int max_size);
27d307c4a8SRafael J. Wysocki extern int arch_hibernation_header_restore(void *addr);
28d307c4a8SRafael J. Wysocki 
29d307c4a8SRafael J. Wysocki static inline int init_header_complete(struct swsusp_info *info)
30d307c4a8SRafael J. Wysocki {
31d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE);
32d307c4a8SRafael J. Wysocki }
33d307c4a8SRafael J. Wysocki 
34d307c4a8SRafael J. Wysocki static inline char *check_image_kernel(struct swsusp_info *info)
35d307c4a8SRafael J. Wysocki {
36d307c4a8SRafael J. Wysocki 	return arch_hibernation_header_restore(info) ?
37d307c4a8SRafael J. Wysocki 			"architecture specific data" : NULL;
38d307c4a8SRafael J. Wysocki }
39d307c4a8SRafael J. Wysocki #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
40d307c4a8SRafael J. Wysocki 
4156f99bcbSRafael J. Wysocki /*
4256f99bcbSRafael J. Wysocki  * Keep some memory free so that I/O operations can succeed without paging
4356f99bcbSRafael J. Wysocki  * [Might this be more than 4 MB?]
4456f99bcbSRafael J. Wysocki  */
4556f99bcbSRafael J. Wysocki #define PAGES_FOR_IO	((4096 * 1024) >> PAGE_SHIFT)
46d307c4a8SRafael J. Wysocki 
4756f99bcbSRafael J. Wysocki /*
4856f99bcbSRafael J. Wysocki  * Keep 1 MB of memory free so that device drivers can allocate some pages in
4956f99bcbSRafael J. Wysocki  * their .suspend() routines without breaking the suspend to disk.
5056f99bcbSRafael J. Wysocki  */
5156f99bcbSRafael J. Wysocki #define SPARE_PAGES	((1024 * 1024) >> PAGE_SHIFT)
521da177e4SLinus Torvalds 
5304b73469SRashika Kheria asmlinkage int swsusp_save(void);
5404b73469SRashika Kheria 
558b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
5697819a26SSrivatsa S. Bhat extern bool freezer_test_done;
5797819a26SSrivatsa S. Bhat 
587777fab9SRafael J. Wysocki extern int hibernation_snapshot(int platform_mode);
59a634cc10SRafael J. Wysocki extern int hibernation_restore(int platform_mode);
607777fab9SRafael J. Wysocki extern int hibernation_platform_enter(void);
61ac5c24ecSRafael J. Wysocki 
62ac5c24ecSRafael J. Wysocki #else /* !CONFIG_HIBERNATION */
63ac5c24ecSRafael J. Wysocki 
64ddeb6487SRafael J. Wysocki static inline void hibernate_reserved_size_init(void) {}
65ac5c24ecSRafael J. Wysocki static inline void hibernate_image_size_init(void) {}
66ac5c24ecSRafael J. Wysocki #endif /* !CONFIG_HIBERNATION */
67a6d70980SStephen Hemminger 
6849c3df6aSVivek Goyal extern int pfn_is_nosave(unsigned long);
6949c3df6aSVivek Goyal 
701da177e4SLinus Torvalds #define power_attr(_name) \
71386f275fSKay Sievers static struct kobj_attribute _name##_attr = {	\
721da177e4SLinus Torvalds 	.attr	= {				\
731da177e4SLinus Torvalds 		.name = __stringify(_name),	\
741da177e4SLinus Torvalds 		.mode = 0644,			\
751da177e4SLinus Torvalds 	},					\
761da177e4SLinus Torvalds 	.show	= _name##_show,			\
771da177e4SLinus Torvalds 	.store	= _name##_store,		\
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
80853609b6SRafael J. Wysocki /* Preferred image size in bytes (default 500 MB) */
81853609b6SRafael J. Wysocki extern unsigned long image_size;
82ddeb6487SRafael J. Wysocki /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
83ddeb6487SRafael J. Wysocki extern unsigned long reserved_size;
84f577eb30SRafael J. Wysocki extern int in_suspend;
8561159a31SRafael J. Wysocki extern dev_t swsusp_resume_device;
869a154d9dSRafael J. Wysocki extern sector_t swsusp_resume_block;
87f577eb30SRafael J. Wysocki 
8825761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_suspend(void);
8925761b6eSRafael J. Wysocki extern asmlinkage int swsusp_arch_resume(void);
9025761b6eSRafael J. Wysocki 
9174dfd666SRafael J. Wysocki extern int create_basic_memory_bitmaps(void);
9274dfd666SRafael J. Wysocki extern void free_basic_memory_bitmaps(void);
9364a473cbSRafael J. Wysocki extern int hibernate_preallocate_memory(void);
94f577eb30SRafael J. Wysocki 
95fb13a28bSRafael J. Wysocki /**
96fb13a28bSRafael J. Wysocki  *	Auxiliary structure used for reading the snapshot image data and
97fb13a28bSRafael J. Wysocki  *	metadata from and writing them to the list of page backup entries
98fb13a28bSRafael J. Wysocki  *	(PBEs) which is the main data structure of swsusp.
99fb13a28bSRafael J. Wysocki  *
100fb13a28bSRafael J. Wysocki  *	Using struct snapshot_handle we can transfer the image, including its
101fb13a28bSRafael J. Wysocki  *	metadata, as a continuous sequence of bytes with the help of
102fb13a28bSRafael J. Wysocki  *	snapshot_read_next() and snapshot_write_next().
103fb13a28bSRafael J. Wysocki  *
104fb13a28bSRafael J. Wysocki  *	The code that writes the image to a storage or transfers it to
105fb13a28bSRafael J. Wysocki  *	the user land is required to use snapshot_read_next() for this
106fb13a28bSRafael J. Wysocki  *	purpose and it should not make any assumptions regarding the internal
107fb13a28bSRafael J. Wysocki  *	structure of the image.  Similarly, the code that reads the image from
108fb13a28bSRafael J. Wysocki  *	a storage or transfers it from the user land is required to use
109fb13a28bSRafael J. Wysocki  *	snapshot_write_next().
110fb13a28bSRafael J. Wysocki  *
111fb13a28bSRafael J. Wysocki  *	This may allow us to change the internal structure of the image
112fb13a28bSRafael J. Wysocki  *	in the future with considerably less effort.
113fb13a28bSRafael J. Wysocki  */
114fb13a28bSRafael J. Wysocki 
115f577eb30SRafael J. Wysocki struct snapshot_handle {
116fb13a28bSRafael J. Wysocki 	unsigned int	cur;	/* number of the block of PAGE_SIZE bytes the
117fb13a28bSRafael J. Wysocki 				 * next operation will refer to (ie. current)
118fb13a28bSRafael J. Wysocki 				 */
119fb13a28bSRafael J. Wysocki 	void		*buffer;	/* address of the block to read from
120fb13a28bSRafael J. Wysocki 					 * or write to
121fb13a28bSRafael J. Wysocki 					 */
122fb13a28bSRafael J. Wysocki 	int		sync_read;	/* Set to one to notify the caller of
123fb13a28bSRafael J. Wysocki 					 * snapshot_write_next() that it may
124fb13a28bSRafael J. Wysocki 					 * need to call wait_on_bio_chain()
125fb13a28bSRafael J. Wysocki 					 */
126f577eb30SRafael J. Wysocki };
127f577eb30SRafael J. Wysocki 
128fb13a28bSRafael J. Wysocki /* This macro returns the address from/to which the caller of
129fb13a28bSRafael J. Wysocki  * snapshot_read_next()/snapshot_write_next() is allowed to
130fb13a28bSRafael J. Wysocki  * read/write data after the function returns
131fb13a28bSRafael J. Wysocki  */
132d3c1b24cSJiri Slaby #define data_of(handle)	((handle).buffer)
133f577eb30SRafael J. Wysocki 
134b788db79SRafael J. Wysocki extern unsigned int snapshot_additional_pages(struct zone *zone);
135af508b34SRafael J. Wysocki extern unsigned long snapshot_get_image_size(void);
136d3c1b24cSJiri Slaby extern int snapshot_read_next(struct snapshot_handle *handle);
137d3c1b24cSJiri Slaby extern int snapshot_write_next(struct snapshot_handle *handle);
1388357376dSRafael J. Wysocki extern void snapshot_write_finalize(struct snapshot_handle *handle);
139b788db79SRafael J. Wysocki extern int snapshot_image_loaded(struct snapshot_handle *handle);
14061159a31SRafael J. Wysocki 
1410709db60SRafael J. Wysocki /* If unset, the snapshot device cannot be open. */
1420709db60SRafael J. Wysocki extern atomic_t snapshot_device_available;
1430709db60SRafael J. Wysocki 
144d1d241ccSRafael J. Wysocki extern sector_t alloc_swapdev_block(int swap);
145d1d241ccSRafael J. Wysocki extern void free_all_swap_pages(int swap);
146d1d241ccSRafael J. Wysocki extern int swsusp_swap_in_use(void);
14761159a31SRafael J. Wysocki 
148a634cc10SRafael J. Wysocki /*
149a634cc10SRafael J. Wysocki  * Flags that can be passed from the hibernatig hernel to the "boot" kernel in
150a634cc10SRafael J. Wysocki  * the image header.
151a634cc10SRafael J. Wysocki  */
152a634cc10SRafael J. Wysocki #define SF_PLATFORM_MODE	1
153f996fc96SBojan Smojver #define SF_NOCOMPRESS_MODE	2
154081a9d04SBojan Smojver #define SF_CRC32_MODE	        4
155a634cc10SRafael J. Wysocki 
1568b759b84SRafael J. Wysocki /* kernel/power/hibernate.c */
15774c7e2efSRandy Dunlap extern int swsusp_check(void);
15874c7e2efSRandy Dunlap extern void swsusp_free(void);
159a634cc10SRafael J. Wysocki extern int swsusp_read(unsigned int *flags_p);
160a634cc10SRafael J. Wysocki extern int swsusp_write(unsigned int flags);
161c2dd0daeSAl Viro extern void swsusp_close(fmode_t);
16262c552ccSBojan Smojver #ifdef CONFIG_SUSPEND
16362c552ccSBojan Smojver extern int swsusp_unmark(void);
16462c552ccSBojan Smojver #endif
1650d3a9abeSRafael J. Wysocki 
1668a0d613fSJiri Slaby /* kernel/power/block_io.c */
1678a0d613fSJiri Slaby extern struct block_device *hib_resume_bdev;
1688a0d613fSJiri Slaby 
1698a0d613fSJiri Slaby extern int hib_bio_read_page(pgoff_t page_off, void *addr,
1708a0d613fSJiri Slaby 		struct bio **bio_chain);
1718a0d613fSJiri Slaby extern int hib_bio_write_page(pgoff_t page_off, void *addr,
1728a0d613fSJiri Slaby 		struct bio **bio_chain);
1738a0d613fSJiri Slaby extern int hib_wait_on_bio_chain(struct bio **bio_chain);
1748a0d613fSJiri Slaby 
1750d3a9abeSRafael J. Wysocki struct timeval;
176b10d9117SRafael J. Wysocki /* kernel/power/swsusp.c */
177*db597605STina Ruchandani extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *);
178b10d9117SRafael J. Wysocki 
179296699deSRafael J. Wysocki #ifdef CONFIG_SUSPEND
180a9d70523SRafael J. Wysocki /* kernel/power/suspend.c */
18162109b43SRafael J. Wysocki extern const char *pm_labels[];
182d431cbc5SRafael J. Wysocki extern const char *pm_states[];
183a9d70523SRafael J. Wysocki 
1846c961dfbSRafael J. Wysocki extern int suspend_devices_and_enter(suspend_state_t state);
185296699deSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
186296699deSRafael J. Wysocki static inline int suspend_devices_and_enter(suspend_state_t state)
187296699deSRafael J. Wysocki {
188296699deSRafael J. Wysocki 	return -ENOSYS;
189296699deSRafael J. Wysocki }
190296699deSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
191296699deSRafael J. Wysocki 
192a9d70523SRafael J. Wysocki #ifdef CONFIG_PM_TEST_SUSPEND
193a9d70523SRafael J. Wysocki /* kernel/power/suspend_test.c */
194a9d70523SRafael J. Wysocki extern void suspend_test_start(void);
195a9d70523SRafael J. Wysocki extern void suspend_test_finish(const char *label);
196a9d70523SRafael J. Wysocki #else /* !CONFIG_PM_TEST_SUSPEND */
197a9d70523SRafael J. Wysocki static inline void suspend_test_start(void) {}
198a9d70523SRafael J. Wysocki static inline void suspend_test_finish(const char *label) {}
199a9d70523SRafael J. Wysocki #endif /* !CONFIG_PM_TEST_SUSPEND */
200a9d70523SRafael J. Wysocki 
20182525756SAlan Stern #ifdef CONFIG_PM_SLEEP
20282525756SAlan Stern /* kernel/power/main.c */
20382525756SAlan Stern extern int pm_notifier_call_chain(unsigned long val);
20482525756SAlan Stern #endif
2052f8ed1c6SAdrian Bunk 
2062f8ed1c6SAdrian Bunk #ifdef CONFIG_HIGHMEM
2072f8ed1c6SAdrian Bunk int restore_highmem(void);
2082f8ed1c6SAdrian Bunk #else
2092f8ed1c6SAdrian Bunk static inline unsigned int count_highmem_pages(void) { return 0; }
2102f8ed1c6SAdrian Bunk static inline int restore_highmem(void) { return 0; }
2112f8ed1c6SAdrian Bunk #endif
2120e7d56e3SRafael J. Wysocki 
2130e7d56e3SRafael J. Wysocki /*
2140e7d56e3SRafael J. Wysocki  * Suspend test levels
2150e7d56e3SRafael J. Wysocki  */
2160e7d56e3SRafael J. Wysocki enum {
2170e7d56e3SRafael J. Wysocki 	/* keep first */
2180e7d56e3SRafael J. Wysocki 	TEST_NONE,
2190e7d56e3SRafael J. Wysocki 	TEST_CORE,
2200e7d56e3SRafael J. Wysocki 	TEST_CPUS,
2210e7d56e3SRafael J. Wysocki 	TEST_PLATFORM,
2220e7d56e3SRafael J. Wysocki 	TEST_DEVICES,
2230e7d56e3SRafael J. Wysocki 	TEST_FREEZER,
2240e7d56e3SRafael J. Wysocki 	/* keep last */
2250e7d56e3SRafael J. Wysocki 	__TEST_AFTER_LAST
2260e7d56e3SRafael J. Wysocki };
2270e7d56e3SRafael J. Wysocki 
2280e7d56e3SRafael J. Wysocki #define TEST_FIRST	TEST_NONE
2290e7d56e3SRafael J. Wysocki #define TEST_MAX	(__TEST_AFTER_LAST - 1)
2304cc79776SRafael J. Wysocki 
2314cc79776SRafael J. Wysocki extern int pm_test_level;
232b28f5081SJohannes Berg 
233b28f5081SJohannes Berg #ifdef CONFIG_SUSPEND_FREEZER
234b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
235b28f5081SJohannes Berg {
236379e0be8SSrivatsa S. Bhat 	int error;
237379e0be8SSrivatsa S. Bhat 
238379e0be8SSrivatsa S. Bhat 	error = freeze_processes();
239379e0be8SSrivatsa S. Bhat 	/*
240379e0be8SSrivatsa S. Bhat 	 * freeze_processes() automatically thaws every task if freezing
241379e0be8SSrivatsa S. Bhat 	 * fails. So we need not do anything extra upon error.
242379e0be8SSrivatsa S. Bhat 	 */
243379e0be8SSrivatsa S. Bhat 	if (error)
2446f585f75SRafael J. Wysocki 		return error;
245379e0be8SSrivatsa S. Bhat 
246379e0be8SSrivatsa S. Bhat 	error = freeze_kernel_threads();
247379e0be8SSrivatsa S. Bhat 	/*
248379e0be8SSrivatsa S. Bhat 	 * freeze_kernel_threads() thaws only kernel threads upon freezing
249379e0be8SSrivatsa S. Bhat 	 * failure. So we have to thaw the userspace tasks ourselves.
250379e0be8SSrivatsa S. Bhat 	 */
251379e0be8SSrivatsa S. Bhat 	if (error)
252379e0be8SSrivatsa S. Bhat 		thaw_processes();
253379e0be8SSrivatsa S. Bhat 
254379e0be8SSrivatsa S. Bhat 	return error;
255b28f5081SJohannes Berg }
256b28f5081SJohannes Berg 
257b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
258b28f5081SJohannes Berg {
259b28f5081SJohannes Berg 	thaw_processes();
260b28f5081SJohannes Berg }
261b28f5081SJohannes Berg #else
262b28f5081SJohannes Berg static inline int suspend_freeze_processes(void)
263b28f5081SJohannes Berg {
264b28f5081SJohannes Berg 	return 0;
265b28f5081SJohannes Berg }
266b28f5081SJohannes Berg 
267b28f5081SJohannes Berg static inline void suspend_thaw_processes(void)
268b28f5081SJohannes Berg {
269b28f5081SJohannes Berg }
270b28f5081SJohannes Berg #endif
2717483b4a4SRafael J. Wysocki 
2727483b4a4SRafael J. Wysocki #ifdef CONFIG_PM_AUTOSLEEP
2737483b4a4SRafael J. Wysocki 
2747483b4a4SRafael J. Wysocki /* kernel/power/autosleep.c */
2757483b4a4SRafael J. Wysocki extern int pm_autosleep_init(void);
2767483b4a4SRafael J. Wysocki extern int pm_autosleep_lock(void);
2777483b4a4SRafael J. Wysocki extern void pm_autosleep_unlock(void);
2787483b4a4SRafael J. Wysocki extern suspend_state_t pm_autosleep_state(void);
2797483b4a4SRafael J. Wysocki extern int pm_autosleep_set_state(suspend_state_t state);
2807483b4a4SRafael J. Wysocki 
2817483b4a4SRafael J. Wysocki #else /* !CONFIG_PM_AUTOSLEEP */
2827483b4a4SRafael J. Wysocki 
2837483b4a4SRafael J. Wysocki static inline int pm_autosleep_init(void) { return 0; }
2847483b4a4SRafael J. Wysocki static inline int pm_autosleep_lock(void) { return 0; }
2857483b4a4SRafael J. Wysocki static inline void pm_autosleep_unlock(void) {}
2867483b4a4SRafael J. Wysocki static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
2877483b4a4SRafael J. Wysocki 
2887483b4a4SRafael J. Wysocki #endif /* !CONFIG_PM_AUTOSLEEP */
289b86ff982SRafael J. Wysocki 
290b86ff982SRafael J. Wysocki #ifdef CONFIG_PM_WAKELOCKS
291b86ff982SRafael J. Wysocki 
292b86ff982SRafael J. Wysocki /* kernel/power/wakelock.c */
293b86ff982SRafael J. Wysocki extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
294b86ff982SRafael J. Wysocki extern int pm_wake_lock(const char *buf);
295b86ff982SRafael J. Wysocki extern int pm_wake_unlock(const char *buf);
296b86ff982SRafael J. Wysocki 
297b86ff982SRafael J. Wysocki #endif /* !CONFIG_PM_WAKELOCKS */
298