xref: /linux/include/linux/suspend.h (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
295d9ffbeSRafael J. Wysocki #ifndef _LINUX_SUSPEND_H
395d9ffbeSRafael J. Wysocki #define _LINUX_SUSPEND_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/swap.h>
61da177e4SLinus Torvalds #include <linux/notifier.h>
71da177e4SLinus Torvalds #include <linux/init.h>
81da177e4SLinus Torvalds #include <linux/pm.h>
97be98234SRafael J. Wysocki #include <linux/mm.h>
1033e638b9SSrivatsa S. Bhat #include <linux/freezer.h>
1195d9ffbeSRafael J. Wysocki #include <asm/errno.h>
1295d9ffbeSRafael J. Wysocki 
1337cce26bSH Hartley Sweeten #ifdef CONFIG_VT
14b6f448e9SAndres Salomon extern void pm_set_vt_switch(int);
1595d9ffbeSRafael J. Wysocki #else
pm_set_vt_switch(int do_switch)16b6f448e9SAndres Salomon static inline void pm_set_vt_switch(int do_switch)
17b6f448e9SAndres Salomon {
18b6f448e9SAndres Salomon }
1937cce26bSH Hartley Sweeten #endif
20b6f448e9SAndres Salomon 
2137cce26bSH Hartley Sweeten #ifdef CONFIG_VT_CONSOLE_SLEEP
22ca5f2b4cSBorislav Petkov extern void pm_prepare_console(void);
2337cce26bSH Hartley Sweeten extern void pm_restore_console(void);
2437cce26bSH Hartley Sweeten #else
pm_prepare_console(void)25ca5f2b4cSBorislav Petkov static inline void pm_prepare_console(void)
26b6f448e9SAndres Salomon {
27b6f448e9SAndres Salomon }
28b6f448e9SAndres Salomon 
pm_restore_console(void)29b6f448e9SAndres Salomon static inline void pm_restore_console(void)
30b6f448e9SAndres Salomon {
31b6f448e9SAndres Salomon }
3295d9ffbeSRafael J. Wysocki #endif
3395d9ffbeSRafael J. Wysocki 
3495d9ffbeSRafael J. Wysocki typedef int __bitwise suspend_state_t;
3595d9ffbeSRafael J. Wysocki 
3695d9ffbeSRafael J. Wysocki #define PM_SUSPEND_ON		((__force suspend_state_t) 0)
37690cbb90SRafael J. Wysocki #define PM_SUSPEND_TO_IDLE	((__force suspend_state_t) 1)
387e73c5aeSZhang Rui #define PM_SUSPEND_STANDBY	((__force suspend_state_t) 2)
3995d9ffbeSRafael J. Wysocki #define PM_SUSPEND_MEM		((__force suspend_state_t) 3)
40690cbb90SRafael J. Wysocki #define PM_SUSPEND_MIN		PM_SUSPEND_TO_IDLE
4195d9ffbeSRafael J. Wysocki #define PM_SUSPEND_MAX		((__force suspend_state_t) 4)
4295d9ffbeSRafael J. Wysocki 
4395d9ffbeSRafael J. Wysocki /**
4426398a70SRafael J. Wysocki  * struct platform_suspend_ops - Callbacks for managing platform dependent
4526398a70SRafael J. Wysocki  *	system sleep states.
4695d9ffbeSRafael J. Wysocki  *
4795d9ffbeSRafael J. Wysocki  * @valid: Callback to determine if given system sleep state is supported by
4895d9ffbeSRafael J. Wysocki  *	the platform.
4995d9ffbeSRafael J. Wysocki  *	Valid (ie. supported) states are advertised in /sys/power/state.  Note
5095d9ffbeSRafael J. Wysocki  *	that it still may be impossible to enter given system sleep state if the
5195d9ffbeSRafael J. Wysocki  *	conditions aren't right.
5226398a70SRafael J. Wysocki  *	There is the %suspend_valid_only_mem function available that can be
5326398a70SRafael J. Wysocki  *	assigned to this if the platform only supports mem sleep.
5495d9ffbeSRafael J. Wysocki  *
55c697eeceSRafael J. Wysocki  * @begin: Initialise a transition to given system sleep state.
56c697eeceSRafael J. Wysocki  *	@begin() is executed right prior to suspending devices.  The information
57c697eeceSRafael J. Wysocki  *	conveyed to the platform code by @begin() should be disregarded by it as
58c697eeceSRafael J. Wysocki  *	soon as @end() is executed.  If @begin() fails (ie. returns nonzero),
5995d9ffbeSRafael J. Wysocki  *	@prepare(), @enter() and @finish() will not be called by the PM core.
6095d9ffbeSRafael J. Wysocki  *	This callback is optional.  However, if it is implemented, the argument
61c697eeceSRafael J. Wysocki  *	passed to @enter() is redundant and should be ignored.
6295d9ffbeSRafael J. Wysocki  *
6395d9ffbeSRafael J. Wysocki  * @prepare: Prepare the platform for entering the system sleep state indicated
64c697eeceSRafael J. Wysocki  *	by @begin().
6595d9ffbeSRafael J. Wysocki  *	@prepare() is called right after devices have been suspended (ie. the
6695d9ffbeSRafael J. Wysocki  *	appropriate .suspend() method has been executed for each device) and
676a7c7eafSRafael J. Wysocki  *	before device drivers' late suspend callbacks are executed.  It returns
686a7c7eafSRafael J. Wysocki  *	0 on success or a negative error code otherwise, in which case the
696a7c7eafSRafael J. Wysocki  *	system cannot enter the desired sleep state (@prepare_late(), @enter(),
70ce441011SRafael J. Wysocki  *	and @wake() will not be called in that case).
716a7c7eafSRafael J. Wysocki  *
726a7c7eafSRafael J. Wysocki  * @prepare_late: Finish preparing the platform for entering the system sleep
736a7c7eafSRafael J. Wysocki  *	state indicated by @begin().
746a7c7eafSRafael J. Wysocki  *	@prepare_late is called before disabling nonboot CPUs and after
756a7c7eafSRafael J. Wysocki  *	device drivers' late suspend callbacks have been executed.  It returns
766a7c7eafSRafael J. Wysocki  *	0 on success or a negative error code otherwise, in which case the
77ce441011SRafael J. Wysocki  *	system cannot enter the desired sleep state (@enter() will not be
78ce441011SRafael J. Wysocki  *	executed).
7995d9ffbeSRafael J. Wysocki  *
80c697eeceSRafael J. Wysocki  * @enter: Enter the system sleep state indicated by @begin() or represented by
81c697eeceSRafael J. Wysocki  *	the argument if @begin() is not implemented.
8295d9ffbeSRafael J. Wysocki  *	This callback is mandatory.  It returns 0 on success or a negative
8395d9ffbeSRafael J. Wysocki  *	error code otherwise, in which case the system cannot enter the desired
8495d9ffbeSRafael J. Wysocki  *	sleep state.
8595d9ffbeSRafael J. Wysocki  *
866a7c7eafSRafael J. Wysocki  * @wake: Called when the system has just left a sleep state, right after
876a7c7eafSRafael J. Wysocki  *	the nonboot CPUs have been enabled and before device drivers' early
886a7c7eafSRafael J. Wysocki  *	resume callbacks are executed.
896a7c7eafSRafael J. Wysocki  *	This callback is optional, but should be implemented by the platforms
906a7c7eafSRafael J. Wysocki  *	that implement @prepare_late().  If implemented, it is always called
91ce441011SRafael J. Wysocki  *	after @prepare_late and @enter(), even if one of them fails.
926a7c7eafSRafael J. Wysocki  *
936a7c7eafSRafael J. Wysocki  * @finish: Finish wake-up of the platform.
946a7c7eafSRafael J. Wysocki  *	@finish is called right prior to calling device drivers' regular suspend
956a7c7eafSRafael J. Wysocki  *	callbacks.
9695d9ffbeSRafael J. Wysocki  *	This callback is optional, but should be implemented by the platforms
9795d9ffbeSRafael J. Wysocki  *	that implement @prepare().  If implemented, it is always called after
98ce441011SRafael J. Wysocki  *	@enter() and @wake(), even if any of them fails.  It is executed after
99ce441011SRafael J. Wysocki  *	a failing @prepare.
100c697eeceSRafael J. Wysocki  *
1013b5fe852SMyungJoo Ham  * @suspend_again: Returns whether the system should suspend again (true) or
1023b5fe852SMyungJoo Ham  *	not (false). If the platform wants to poll sensors or execute some
1033b5fe852SMyungJoo Ham  *	code during suspended without invoking userspace and most of devices,
1043b5fe852SMyungJoo Ham  *	suspend_again callback is the place assuming that periodic-wakeup or
1053b5fe852SMyungJoo Ham  *	alarm-wakeup is already setup. This allows to execute some codes while
1063b5fe852SMyungJoo Ham  *	being kept suspended in the view of userland and devices.
1073b5fe852SMyungJoo Ham  *
108c697eeceSRafael J. Wysocki  * @end: Called by the PM core right after resuming devices, to indicate to
109c697eeceSRafael J. Wysocki  *	the platform that the system has returned to the working state or
110c697eeceSRafael J. Wysocki  *	the transition to the sleep state has been aborted.
111c697eeceSRafael J. Wysocki  *	This callback is optional, but should be implemented by the platforms
1126a7c7eafSRafael J. Wysocki  *	that implement @begin().  Accordingly, platforms implementing @begin()
1136a7c7eafSRafael J. Wysocki  *	should also provide a @end() which cleans up transitions aborted before
114c697eeceSRafael J. Wysocki  *	@enter().
115d8f3de0dSRafael J. Wysocki  *
116d8f3de0dSRafael J. Wysocki  * @recover: Recover the platform from a suspend failure.
117d8f3de0dSRafael J. Wysocki  *	Called by the PM core if the suspending of devices fails.
118d8f3de0dSRafael J. Wysocki  *	This callback is optional and should only be implemented by platforms
119d8f3de0dSRafael J. Wysocki  *	which require special recovery actions in that situation.
12095d9ffbeSRafael J. Wysocki  */
12126398a70SRafael J. Wysocki struct platform_suspend_ops {
12295d9ffbeSRafael J. Wysocki 	int (*valid)(suspend_state_t state);
123c697eeceSRafael J. Wysocki 	int (*begin)(suspend_state_t state);
124e6c5eb95SRafael J. Wysocki 	int (*prepare)(void);
1256a7c7eafSRafael J. Wysocki 	int (*prepare_late)(void);
12695d9ffbeSRafael J. Wysocki 	int (*enter)(suspend_state_t state);
1276a7c7eafSRafael J. Wysocki 	void (*wake)(void);
128e6c5eb95SRafael J. Wysocki 	void (*finish)(void);
1293b5fe852SMyungJoo Ham 	bool (*suspend_again)(void);
130c697eeceSRafael J. Wysocki 	void (*end)(void);
131d8f3de0dSRafael J. Wysocki 	void (*recover)(void);
13295d9ffbeSRafael J. Wysocki };
13395d9ffbeSRafael J. Wysocki 
13423d5855fSRafael J. Wysocki struct platform_s2idle_ops {
1351f0b6386SRafael J. Wysocki 	int (*begin)(void);
136a8d46b9eSRafael J. Wysocki 	int (*prepare)(void);
137ac9eafbeSRafael J. Wysocki 	int (*prepare_late)(void);
138811d59fdSMario Limonciello 	void (*check)(void);
139e3728b50SRafael J. Wysocki 	bool (*wake)(void);
140ac9eafbeSRafael J. Wysocki 	void (*restore_early)(void);
141a8d46b9eSRafael J. Wysocki 	void (*restore)(void);
1421f0b6386SRafael J. Wysocki 	void (*end)(void);
1431f0b6386SRafael J. Wysocki };
1441f0b6386SRafael J. Wysocki 
14595d9ffbeSRafael J. Wysocki #ifdef CONFIG_SUSPEND
1462e41e3caSKai-Heng Feng extern suspend_state_t pm_suspend_target_state;
147e870c6c8SRafael J. Wysocki extern suspend_state_t mem_sleep_current;
148e870c6c8SRafael J. Wysocki extern suspend_state_t mem_sleep_default;
149e870c6c8SRafael J. Wysocki 
15095d9ffbeSRafael J. Wysocki /**
15126398a70SRafael J. Wysocki  * suspend_set_ops - set platform dependent suspend operations
15226398a70SRafael J. Wysocki  * @ops: The new suspend operations to set.
15395d9ffbeSRafael J. Wysocki  */
1542f55ac07SLionel Debroux extern void suspend_set_ops(const struct platform_suspend_ops *ops);
15526398a70SRafael J. Wysocki extern int suspend_valid_only_mem(suspend_state_t state);
15638106313SRafael J. Wysocki 
157ef25ba04SRafael J. Wysocki extern unsigned int pm_suspend_global_flags;
158ef25ba04SRafael J. Wysocki 
159471a739aSRafael J. Wysocki #define PM_SUSPEND_FLAG_FW_SUSPEND	BIT(0)
160471a739aSRafael J. Wysocki #define PM_SUSPEND_FLAG_FW_RESUME	BIT(1)
161471a739aSRafael J. Wysocki #define PM_SUSPEND_FLAG_NO_PLATFORM	BIT(2)
162ef25ba04SRafael J. Wysocki 
pm_suspend_clear_flags(void)163ef25ba04SRafael J. Wysocki static inline void pm_suspend_clear_flags(void)
164ef25ba04SRafael J. Wysocki {
165ef25ba04SRafael J. Wysocki 	pm_suspend_global_flags = 0;
166ef25ba04SRafael J. Wysocki }
167ef25ba04SRafael J. Wysocki 
pm_set_suspend_via_firmware(void)168ef25ba04SRafael J. Wysocki static inline void pm_set_suspend_via_firmware(void)
169ef25ba04SRafael J. Wysocki {
170ef25ba04SRafael J. Wysocki 	pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_SUSPEND;
171ef25ba04SRafael J. Wysocki }
172ef25ba04SRafael J. Wysocki 
pm_set_resume_via_firmware(void)173ef25ba04SRafael J. Wysocki static inline void pm_set_resume_via_firmware(void)
174ef25ba04SRafael J. Wysocki {
175ef25ba04SRafael J. Wysocki 	pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_RESUME;
176ef25ba04SRafael J. Wysocki }
177ef25ba04SRafael J. Wysocki 
pm_set_suspend_no_platform(void)178471a739aSRafael J. Wysocki static inline void pm_set_suspend_no_platform(void)
179471a739aSRafael J. Wysocki {
180471a739aSRafael J. Wysocki 	pm_suspend_global_flags |= PM_SUSPEND_FLAG_NO_PLATFORM;
181471a739aSRafael J. Wysocki }
182471a739aSRafael J. Wysocki 
183a6137347SRafael J. Wysocki /**
184a6137347SRafael J. Wysocki  * pm_suspend_via_firmware - Check if platform firmware will suspend the system.
185a6137347SRafael J. Wysocki  *
186a6137347SRafael J. Wysocki  * To be called during system-wide power management transitions to sleep states
187a6137347SRafael J. Wysocki  * or during the subsequent system-wide transitions back to the working state.
188a6137347SRafael J. Wysocki  *
189a6137347SRafael J. Wysocki  * Return 'true' if the platform firmware is going to be invoked at the end of
190a6137347SRafael J. Wysocki  * the system-wide power management transition (to a sleep state) in progress in
191a6137347SRafael J. Wysocki  * order to complete it, or if the platform firmware has been invoked in order
192a6137347SRafael J. Wysocki  * to complete the last (or preceding) transition of the system to a sleep
193a6137347SRafael J. Wysocki  * state.
194a6137347SRafael J. Wysocki  *
195a6137347SRafael J. Wysocki  * This matters if the caller needs or wants to carry out some special actions
196a6137347SRafael J. Wysocki  * depending on whether or not control will be passed to the platform firmware
197a6137347SRafael J. Wysocki  * subsequently (for example, the device may need to be reset before letting the
198a6137347SRafael J. Wysocki  * platform firmware manipulate it, which is not necessary when the platform
199a6137347SRafael J. Wysocki  * firmware is not going to be invoked) or when such special actions may have
200a6137347SRafael J. Wysocki  * been carried out during the preceding transition of the system to a sleep
201a6137347SRafael J. Wysocki  * state (as they may need to be taken into account).
202a6137347SRafael J. Wysocki  */
pm_suspend_via_firmware(void)203ef25ba04SRafael J. Wysocki static inline bool pm_suspend_via_firmware(void)
204ef25ba04SRafael J. Wysocki {
205ef25ba04SRafael J. Wysocki 	return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_SUSPEND);
206ef25ba04SRafael J. Wysocki }
207ef25ba04SRafael J. Wysocki 
208a6137347SRafael J. Wysocki /**
209a6137347SRafael J. Wysocki  * pm_resume_via_firmware - Check if platform firmware has woken up the system.
210a6137347SRafael J. Wysocki  *
211a6137347SRafael J. Wysocki  * To be called during system-wide power management transitions from sleep
212a6137347SRafael J. Wysocki  * states.
213a6137347SRafael J. Wysocki  *
214a6137347SRafael J. Wysocki  * Return 'true' if the platform firmware has passed control to the kernel at
215a6137347SRafael J. Wysocki  * the beginning of the system-wide power management transition in progress, so
216a6137347SRafael J. Wysocki  * the event that woke up the system from sleep has been handled by the platform
217a6137347SRafael J. Wysocki  * firmware.
218a6137347SRafael J. Wysocki  */
pm_resume_via_firmware(void)219ef25ba04SRafael J. Wysocki static inline bool pm_resume_via_firmware(void)
220ef25ba04SRafael J. Wysocki {
221ef25ba04SRafael J. Wysocki 	return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_RESUME);
222ef25ba04SRafael J. Wysocki }
223ef25ba04SRafael J. Wysocki 
224471a739aSRafael J. Wysocki /**
225471a739aSRafael J. Wysocki  * pm_suspend_no_platform - Check if platform may change device power states.
226471a739aSRafael J. Wysocki  *
227471a739aSRafael J. Wysocki  * To be called during system-wide power management transitions to sleep states
228471a739aSRafael J. Wysocki  * or during the subsequent system-wide transitions back to the working state.
229471a739aSRafael J. Wysocki  *
230471a739aSRafael J. Wysocki  * Return 'true' if the power states of devices remain under full control of the
231471a739aSRafael J. Wysocki  * kernel throughout the system-wide suspend and resume cycle in progress (that
232471a739aSRafael J. Wysocki  * is, if a device is put into a certain power state during suspend, it can be
233471a739aSRafael J. Wysocki  * expected to remain in that state during resume).
234471a739aSRafael J. Wysocki  */
pm_suspend_no_platform(void)235471a739aSRafael J. Wysocki static inline bool pm_suspend_no_platform(void)
236471a739aSRafael J. Wysocki {
237471a739aSRafael J. Wysocki 	return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_NO_PLATFORM);
238471a739aSRafael J. Wysocki }
239471a739aSRafael J. Wysocki 
24038106313SRafael J. Wysocki /* Suspend-to-idle state machnine. */
241f02f4f9dSRafael J. Wysocki enum s2idle_states {
242f02f4f9dSRafael J. Wysocki 	S2IDLE_STATE_NONE,      /* Not suspended/suspending. */
243f02f4f9dSRafael J. Wysocki 	S2IDLE_STATE_ENTER,     /* Enter suspend-to-idle. */
244f02f4f9dSRafael J. Wysocki 	S2IDLE_STATE_WAKE,      /* Wake up from suspend-to-idle. */
24538106313SRafael J. Wysocki };
24638106313SRafael J. Wysocki 
247f02f4f9dSRafael J. Wysocki extern enum s2idle_states __read_mostly s2idle_state;
24838106313SRafael J. Wysocki 
idle_should_enter_s2idle(void)249f02f4f9dSRafael J. Wysocki static inline bool idle_should_enter_s2idle(void)
25038106313SRafael J. Wysocki {
251f02f4f9dSRafael J. Wysocki 	return unlikely(s2idle_state == S2IDLE_STATE_ENTER);
25238106313SRafael J. Wysocki }
25338106313SRafael J. Wysocki 
2540b385a0cSRafael J. Wysocki extern bool pm_suspend_default_s2idle(void);
255fa7fd6faSSudeep Holla extern void __init pm_states_init(void);
25623d5855fSRafael J. Wysocki extern void s2idle_set_ops(const struct platform_s2idle_ops *ops);
257f02f4f9dSRafael J. Wysocki extern void s2idle_wake(void);
25895d9ffbeSRafael J. Wysocki 
25995d9ffbeSRafael J. Wysocki /**
26095d9ffbeSRafael J. Wysocki  * arch_suspend_disable_irqs - disable IRQs for suspend
26195d9ffbeSRafael J. Wysocki  *
26295d9ffbeSRafael J. Wysocki  * Disables IRQs (in the default case). This is a weak symbol in the common
26395d9ffbeSRafael J. Wysocki  * code and thus allows architectures to override it if more needs to be
26495d9ffbeSRafael J. Wysocki  * done. Not called for suspend to disk.
26595d9ffbeSRafael J. Wysocki  */
26695d9ffbeSRafael J. Wysocki extern void arch_suspend_disable_irqs(void);
26795d9ffbeSRafael J. Wysocki 
26895d9ffbeSRafael J. Wysocki /**
26995d9ffbeSRafael J. Wysocki  * arch_suspend_enable_irqs - enable IRQs after suspend
27095d9ffbeSRafael J. Wysocki  *
27195d9ffbeSRafael J. Wysocki  * Enables IRQs (in the default case). This is a weak symbol in the common
27295d9ffbeSRafael J. Wysocki  * code and thus allows architectures to override it if more needs to be
27395d9ffbeSRafael J. Wysocki  * done. Not called for suspend to disk.
27495d9ffbeSRafael J. Wysocki  */
27595d9ffbeSRafael J. Wysocki extern void arch_suspend_enable_irqs(void);
27695d9ffbeSRafael J. Wysocki 
27795d9ffbeSRafael J. Wysocki extern int pm_suspend(suspend_state_t state);
278c052bf82SJonas Meurer extern bool sync_on_suspend_enabled;
27995d9ffbeSRafael J. Wysocki #else /* !CONFIG_SUSPEND */
28095d9ffbeSRafael J. Wysocki #define suspend_valid_only_mem	NULL
28195d9ffbeSRafael J. Wysocki 
2822e41e3caSKai-Heng Feng #define pm_suspend_target_state	(PM_SUSPEND_ON)
2832e41e3caSKai-Heng Feng 
pm_suspend_clear_flags(void)284ef25ba04SRafael J. Wysocki static inline void pm_suspend_clear_flags(void) {}
pm_set_suspend_via_firmware(void)285ef25ba04SRafael J. Wysocki static inline void pm_set_suspend_via_firmware(void) {}
pm_set_resume_via_firmware(void)286ef25ba04SRafael J. Wysocki static inline void pm_set_resume_via_firmware(void) {}
pm_suspend_via_firmware(void)287ef25ba04SRafael J. Wysocki static inline bool pm_suspend_via_firmware(void) { return false; }
pm_resume_via_firmware(void)288ef25ba04SRafael J. Wysocki static inline bool pm_resume_via_firmware(void) { return false; }
pm_suspend_no_platform(void)28910a08fd6SRafael J. Wysocki static inline bool pm_suspend_no_platform(void) { return false; }
pm_suspend_default_s2idle(void)2900b385a0cSRafael J. Wysocki static inline bool pm_suspend_default_s2idle(void) { return false; }
291ef25ba04SRafael J. Wysocki 
suspend_set_ops(const struct platform_suspend_ops * ops)2922f55ac07SLionel Debroux static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
pm_suspend(suspend_state_t state)29395d9ffbeSRafael J. Wysocki static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
sync_on_suspend_enabled(void)294c052bf82SJonas Meurer static inline bool sync_on_suspend_enabled(void) { return true; }
idle_should_enter_s2idle(void)295f02f4f9dSRafael J. Wysocki static inline bool idle_should_enter_s2idle(void) { return false; }
pm_states_init(void)296fa7fd6faSSudeep Holla static inline void __init pm_states_init(void) {}
s2idle_set_ops(const struct platform_s2idle_ops * ops)29723d5855fSRafael J. Wysocki static inline void s2idle_set_ops(const struct platform_s2idle_ops *ops) {}
s2idle_wake(void)298f02f4f9dSRafael J. Wysocki static inline void s2idle_wake(void) {}
29995d9ffbeSRafael J. Wysocki #endif /* !CONFIG_SUSPEND */
3001da177e4SLinus Torvalds 
3018357376dSRafael J. Wysocki /* struct pbe is used for creating lists of pages that should be restored
3028357376dSRafael J. Wysocki  * atomically during the resume from disk, because the page frames they have
3038357376dSRafael J. Wysocki  * occupied before the suspend are in use.
3048357376dSRafael J. Wysocki  */
305dcbb5a54SRafael J. Wysocki struct pbe {
3068357376dSRafael J. Wysocki 	void *address;		/* address of the copy */
3078357376dSRafael J. Wysocki 	void *orig_address;	/* original address of a page */
3087088a5c0SRafael J. Wysocki 	struct pbe *next;
309dcbb5a54SRafael J. Wysocki };
3101da177e4SLinus Torvalds 
311a3d25c27SRafael J. Wysocki /**
312b3dac3b3SRafael J. Wysocki  * struct platform_hibernation_ops - hibernation platform support
313a3d25c27SRafael J. Wysocki  *
314caea99efSRafael J. Wysocki  * The methods in this structure allow a platform to carry out special
315caea99efSRafael J. Wysocki  * operations required by it during a hibernation transition.
316a3d25c27SRafael J. Wysocki  *
317d8f3de0dSRafael J. Wysocki  * All the methods below, except for @recover(), must be implemented.
318a3d25c27SRafael J. Wysocki  *
319caea99efSRafael J. Wysocki  * @begin: Tell the platform driver that we're starting hibernation.
32074f270afSRafael J. Wysocki  *	Called right after shrinking memory and before freezing devices.
32174f270afSRafael J. Wysocki  *
322caea99efSRafael J. Wysocki  * @end: Called by the PM core right after resuming devices, to indicate to
323caea99efSRafael J. Wysocki  *	the platform that the system has returned to the working state.
324caea99efSRafael J. Wysocki  *
32574f270afSRafael J. Wysocki  * @pre_snapshot: Prepare the platform for creating the hibernation image.
32674f270afSRafael J. Wysocki  *	Called right after devices have been frozen and before the nonboot
32774f270afSRafael J. Wysocki  *	CPUs are disabled (runs with IRQs on).
32874f270afSRafael J. Wysocki  *
32974f270afSRafael J. Wysocki  * @finish: Restore the previous state of the platform after the hibernation
33074f270afSRafael J. Wysocki  *	image has been created *or* put the platform into the normal operation
33174f270afSRafael J. Wysocki  *	mode after the hibernation (the same method is executed in both cases).
33274f270afSRafael J. Wysocki  *	Called right after the nonboot CPUs have been enabled and before
33374f270afSRafael J. Wysocki  *	thawing devices (runs with IRQs on).
33474f270afSRafael J. Wysocki  *
33574f270afSRafael J. Wysocki  * @prepare: Prepare the platform for entering the low power state.
33674f270afSRafael J. Wysocki  *	Called right after the hibernation image has been saved and before
33774f270afSRafael J. Wysocki  *	devices are prepared for entering the low power state.
33874f270afSRafael J. Wysocki  *
33974f270afSRafael J. Wysocki  * @enter: Put the system into the low power state after the hibernation image
34074f270afSRafael J. Wysocki  *	has been saved to disk.
34174f270afSRafael J. Wysocki  *	Called after the nonboot CPUs have been disabled and all of the low
34274f270afSRafael J. Wysocki  *	level devices have been shut down (runs with IRQs off).
34374f270afSRafael J. Wysocki  *
344c7e0831dSRafael J. Wysocki  * @leave: Perform the first stage of the cleanup after the system sleep state
345c7e0831dSRafael J. Wysocki  *	indicated by @set_target() has been left.
346c7e0831dSRafael J. Wysocki  *	Called right after the control has been passed from the boot kernel to
347c7e0831dSRafael J. Wysocki  *	the image kernel, before the nonboot CPUs are enabled and before devices
348c7e0831dSRafael J. Wysocki  *	are resumed.  Executed with interrupts disabled.
349c7e0831dSRafael J. Wysocki  *
35074f270afSRafael J. Wysocki  * @pre_restore: Prepare system for the restoration from a hibernation image.
35174f270afSRafael J. Wysocki  *	Called right after devices have been frozen and before the nonboot
35274f270afSRafael J. Wysocki  *	CPUs are disabled (runs with IRQs on).
35374f270afSRafael J. Wysocki  *
35474f270afSRafael J. Wysocki  * @restore_cleanup: Clean up after a failing image restoration.
35574f270afSRafael J. Wysocki  *	Called right after the nonboot CPUs have been enabled and before
35674f270afSRafael J. Wysocki  *	thawing devices (runs with IRQs on).
357d8f3de0dSRafael J. Wysocki  *
358d8f3de0dSRafael J. Wysocki  * @recover: Recover the platform from a failure to suspend devices.
359d8f3de0dSRafael J. Wysocki  *	Called by the PM core if the suspending of devices during hibernation
360d8f3de0dSRafael J. Wysocki  *	fails.  This callback is optional and should only be implemented by
361d8f3de0dSRafael J. Wysocki  *	platforms which require special recovery actions in that situation.
362a3d25c27SRafael J. Wysocki  */
363b3dac3b3SRafael J. Wysocki struct platform_hibernation_ops {
364bb186901SRafael J. Wysocki 	int (*begin)(pm_message_t stage);
365caea99efSRafael J. Wysocki 	void (*end)(void);
36674f270afSRafael J. Wysocki 	int (*pre_snapshot)(void);
36774f270afSRafael J. Wysocki 	void (*finish)(void);
368a3d25c27SRafael J. Wysocki 	int (*prepare)(void);
369a3d25c27SRafael J. Wysocki 	int (*enter)(void);
370c7e0831dSRafael J. Wysocki 	void (*leave)(void);
371a634cc10SRafael J. Wysocki 	int (*pre_restore)(void);
372a634cc10SRafael J. Wysocki 	void (*restore_cleanup)(void);
373d8f3de0dSRafael J. Wysocki 	void (*recover)(void);
374a3d25c27SRafael J. Wysocki };
375a3d25c27SRafael J. Wysocki 
376b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
37774dfd666SRafael J. Wysocki /* kernel/power/snapshot.c */
37833569ef3SAmadeusz Sławiński extern void register_nosave_region(unsigned long b, unsigned long e);
37974dfd666SRafael J. Wysocki extern int swsusp_page_is_forbidden(struct page *);
38074dfd666SRafael J. Wysocki extern void swsusp_set_page_free(struct page *);
38174dfd666SRafael J. Wysocki extern void swsusp_unset_page_free(struct page *);
38274dfd666SRafael J. Wysocki extern unsigned long get_safe_page(gfp_t gfp_mask);
383328008a7SArnd Bergmann extern asmlinkage int swsusp_arch_suspend(void);
384328008a7SArnd Bergmann extern asmlinkage int swsusp_arch_resume(void);
385a3d25c27SRafael J. Wysocki 
38674d95555SDavid Woodhouse extern u32 swsusp_hardware_signature;
387073ef1f6SLionel Debroux extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
388a3d25c27SRafael J. Wysocki extern int hibernate(void);
389abfe2d7bSRafael J. Wysocki extern bool system_entering_hibernation(void);
390a6e15a39SKees Cook extern bool hibernation_available(void);
391603fb42aSSebastian Capella asmlinkage int swsusp_save(void);
392603fb42aSSebastian Capella extern struct pbe *restore_pblist;
3931ec0cd82SMathieu Malaterre int pfn_is_nosave(unsigned long pfn);
39448001ea5SDan Williams 
39548001ea5SDan Williams int hibernate_quiet_exec(int (*func)(void *data), void *data);
3968a3e82d3SArnd Bergmann int hibernate_resume_nonboot_cpu_disable(void);
3978a3e82d3SArnd Bergmann int arch_hibernation_header_save(void *addr, unsigned int max_size);
3988a3e82d3SArnd Bergmann int arch_hibernation_header_restore(void *addr);
3998a3e82d3SArnd Bergmann 
400b0cb1a19SRafael J. Wysocki #else /* CONFIG_HIBERNATION */
register_nosave_region(unsigned long b,unsigned long e)4011f112ceeSRafael J. Wysocki static inline void register_nosave_region(unsigned long b, unsigned long e) {}
swsusp_page_is_forbidden(struct page * p)40274dfd666SRafael J. Wysocki static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
swsusp_set_page_free(struct page * p)40374dfd666SRafael J. Wysocki static inline void swsusp_set_page_free(struct page *p) {}
swsusp_unset_page_free(struct page * p)40474dfd666SRafael J. Wysocki static inline void swsusp_unset_page_free(struct page *p) {}
405a3d25c27SRafael J. Wysocki 
hibernation_set_ops(const struct platform_hibernation_ops * ops)406073ef1f6SLionel Debroux static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
hibernate(void)407a3d25c27SRafael J. Wysocki static inline int hibernate(void) { return -ENOSYS; }
system_entering_hibernation(void)408fce2b111SCornelia Huck static inline bool system_entering_hibernation(void) { return false; }
hibernation_available(void)409a6e15a39SKees Cook static inline bool hibernation_available(void) { return false; }
41048001ea5SDan Williams 
hibernate_quiet_exec(int (* func)(void * data),void * data)41148001ea5SDan Williams static inline int hibernate_quiet_exec(int (*func)(void *data), void *data) {
41248001ea5SDan Williams 	return -ENOTSUPP;
41348001ea5SDan Williams }
414fce2b111SCornelia Huck #endif /* CONFIG_HIBERNATION */
415fce2b111SCornelia Huck 
416ab23ed6eSArnd Bergmann int arch_resume_nosmt(void);
417ab23ed6eSArnd Bergmann 
418ad1e4f74SDomenico Andreoli #ifdef CONFIG_HIBERNATION_SNAPSHOT_DEV
419bb3247a3SChristoph Hellwig int is_hibernate_resume_dev(dev_t dev);
420ad1e4f74SDomenico Andreoli #else
is_hibernate_resume_dev(dev_t dev)421bb3247a3SChristoph Hellwig static inline int is_hibernate_resume_dev(dev_t dev) { return 0; }
422ad1e4f74SDomenico Andreoli #endif
423ad1e4f74SDomenico Andreoli 
42435eb6db1SAmerigo Wang /* Hibernation and suspend events */
42535eb6db1SAmerigo Wang #define PM_HIBERNATION_PREPARE	0x0001 /* Going to hibernate */
42635eb6db1SAmerigo Wang #define PM_POST_HIBERNATION	0x0002 /* Hibernation finished */
42735eb6db1SAmerigo Wang #define PM_SUSPEND_PREPARE	0x0003 /* Going to suspend the system */
42835eb6db1SAmerigo Wang #define PM_POST_SUSPEND		0x0004 /* Suspend finished */
42935eb6db1SAmerigo Wang #define PM_RESTORE_PREPARE	0x0005 /* Going to restore a saved image */
43035eb6db1SAmerigo Wang #define PM_POST_RESTORE		0x0006 /* Restore failed */
43135eb6db1SAmerigo Wang 
43255f2503cSPingfan Liu extern struct mutex system_transition_mutex;
4339b6fc5dcSSrivatsa S. Bhat 
434296699deSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
4351da177e4SLinus Torvalds void save_processor_state(void);
4361da177e4SLinus Torvalds void restore_processor_state(void);
43725761b6eSRafael J. Wysocki 
438b10d9117SRafael J. Wysocki /* kernel/power/main.c */
43982525756SAlan Stern extern int register_pm_notifier(struct notifier_block *nb);
44082525756SAlan Stern extern int unregister_pm_notifier(struct notifier_block *nb);
441b5dee313SHarry Pan extern void ksys_sync_helper(void);
442b52124a7SMario Limonciello extern void pm_report_hw_sleep_time(u64 t);
443b52124a7SMario Limonciello extern void pm_report_max_hw_sleep(u64 t);
444b10d9117SRafael J. Wysocki 
445b10d9117SRafael J. Wysocki #define pm_notifier(fn, pri) {				\
446b10d9117SRafael J. Wysocki 	static struct notifier_block fn##_nb =			\
447b10d9117SRafael J. Wysocki 		{ .notifier_call = fn, .priority = pri };	\
448b10d9117SRafael J. Wysocki 	register_pm_notifier(&fn##_nb);			\
449b10d9117SRafael J. Wysocki }
450c125e96fSRafael J. Wysocki 
451c125e96fSRafael J. Wysocki /* drivers/base/power/wakeup.c */
452c125e96fSRafael J. Wysocki extern bool events_check_enabled;
453c125e96fSRafael J. Wysocki 
pm_suspended_storage(void)45407f44ac3SKefeng Wang static inline bool pm_suspended_storage(void)
45507f44ac3SKefeng Wang {
45607f44ac3SKefeng Wang 	return !gfp_has_io_fs(gfp_allowed_mask);
45707f44ac3SKefeng Wang }
45807f44ac3SKefeng Wang 
459a2867e08SRafael J. Wysocki extern bool pm_wakeup_pending(void);
460068765baSRafael J. Wysocki extern void pm_system_wakeup(void);
46133e4f80eSRafael J. Wysocki extern void pm_system_cancel_wakeup(void);
462cb1f65c1SRafael J. Wysocki extern void pm_wakeup_clear(unsigned int irq_number);
463a6f5f0ddSAlexandra Yates extern void pm_system_irq_wakeup(unsigned int irq_number);
464cb1f65c1SRafael J. Wysocki extern unsigned int pm_wakeup_irq(void);
4657483b4a4SRafael J. Wysocki extern bool pm_get_wakeup_count(unsigned int *count, bool block);
466074037ecSRafael J. Wysocki extern bool pm_save_wakeup_count(unsigned int count);
46755850945SRafael J. Wysocki extern void pm_wakep_autosleep_enabled(bool set);
468bb177fedSJulius Werner extern void pm_print_active_wakeup_sources(void);
4699b6fc5dcSSrivatsa S. Bhat 
4705950e5d5SPeter Zijlstra extern unsigned int lock_system_sleep(void);
4715950e5d5SPeter Zijlstra extern void unlock_system_sleep(unsigned int);
4729b6fc5dcSSrivatsa S. Bhat 
473296699deSRafael J. Wysocki #else /* !CONFIG_PM_SLEEP */
474b10d9117SRafael J. Wysocki 
register_pm_notifier(struct notifier_block * nb)475b10d9117SRafael J. Wysocki static inline int register_pm_notifier(struct notifier_block *nb)
476b10d9117SRafael J. Wysocki {
477b10d9117SRafael J. Wysocki 	return 0;
478b10d9117SRafael J. Wysocki }
479b10d9117SRafael J. Wysocki 
unregister_pm_notifier(struct notifier_block * nb)480b10d9117SRafael J. Wysocki static inline int unregister_pm_notifier(struct notifier_block *nb)
481b10d9117SRafael J. Wysocki {
482b10d9117SRafael J. Wysocki 	return 0;
483b10d9117SRafael J. Wysocki }
484b10d9117SRafael J. Wysocki 
pm_report_hw_sleep_time(u64 t)485b52124a7SMario Limonciello static inline void pm_report_hw_sleep_time(u64 t) {};
pm_report_max_hw_sleep(u64 t)486b52124a7SMario Limonciello static inline void pm_report_max_hw_sleep(u64 t) {};
487b52124a7SMario Limonciello 
ksys_sync_helper(void)488b5dee313SHarry Pan static inline void ksys_sync_helper(void) {}
489b5dee313SHarry Pan 
490b10d9117SRafael J. Wysocki #define pm_notifier(fn, pri)	do { (void)(fn); } while (0)
491dbeeec5fSRafael J. Wysocki 
pm_suspended_storage(void)49207f44ac3SKefeng Wang static inline bool pm_suspended_storage(void) { return false; }
pm_wakeup_pending(void)493a2867e08SRafael J. Wysocki static inline bool pm_wakeup_pending(void) { return false; }
pm_system_wakeup(void)494068765baSRafael J. Wysocki static inline void pm_system_wakeup(void) {}
pm_wakeup_clear(bool reset)49533e4f80eSRafael J. Wysocki static inline void pm_wakeup_clear(bool reset) {}
pm_system_irq_wakeup(unsigned int irq_number)496a6f5f0ddSAlexandra Yates static inline void pm_system_irq_wakeup(unsigned int irq_number) {}
497b10d9117SRafael J. Wysocki 
lock_system_sleep(void)4985950e5d5SPeter Zijlstra static inline unsigned int lock_system_sleep(void) { return 0; }
unlock_system_sleep(unsigned int flags)4995950e5d5SPeter Zijlstra static inline void unlock_system_sleep(unsigned int flags) {}
5006ad696d2SAndi Kleen 
5019b6fc5dcSSrivatsa S. Bhat #endif /* !CONFIG_PM_SLEEP */
50289081d17SHuang Ying 
503b2df1d4fSRafael J. Wysocki #ifdef CONFIG_PM_SLEEP_DEBUG
504b2df1d4fSRafael J. Wysocki extern bool pm_print_times_enabled;
505726fb6b4SSrinivas Pandruvada extern bool pm_debug_messages_on;
506cdb8c100SMario Limonciello extern bool pm_debug_messages_should_print(void);
pm_dyn_debug_messages_on(void)507ce1cb680SDavid Cohen static inline int pm_dyn_debug_messages_on(void)
508ce1cb680SDavid Cohen {
509ce1cb680SDavid Cohen #ifdef CONFIG_DYNAMIC_DEBUG
510ce1cb680SDavid Cohen 	return 1;
511ce1cb680SDavid Cohen #else
512ce1cb680SDavid Cohen 	return 0;
513ce1cb680SDavid Cohen #endif
514ce1cb680SDavid Cohen }
515ce1cb680SDavid Cohen #ifndef pr_fmt
516ce1cb680SDavid Cohen #define pr_fmt(fmt) "PM: " fmt
517ce1cb680SDavid Cohen #endif
518ce1cb680SDavid Cohen #define __pm_pr_dbg(fmt, ...)					\
519ce1cb680SDavid Cohen 	do {							\
520cdb8c100SMario Limonciello 		if (pm_debug_messages_should_print())		\
521ce1cb680SDavid Cohen 			printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__);	\
522ce1cb680SDavid Cohen 		else if (pm_dyn_debug_messages_on())		\
523ce1cb680SDavid Cohen 			pr_debug(fmt, ##__VA_ARGS__);	\
524ce1cb680SDavid Cohen 	} while (0)
525ce1cb680SDavid Cohen #define __pm_deferred_pr_dbg(fmt, ...)				\
526ce1cb680SDavid Cohen 	do {							\
527cdb8c100SMario Limonciello 		if (pm_debug_messages_should_print())		\
528ce1cb680SDavid Cohen 			printk_deferred(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__);	\
529ce1cb680SDavid Cohen 	} while (0)
530b2df1d4fSRafael J. Wysocki #else
531b2df1d4fSRafael J. Wysocki #define pm_print_times_enabled	(false)
532726fb6b4SSrinivas Pandruvada #define pm_debug_messages_on	(false)
5338d8b2441SRafael J. Wysocki 
5348d8b2441SRafael J. Wysocki #include <linux/printk.h>
5358d8b2441SRafael J. Wysocki 
536ce1cb680SDavid Cohen #define __pm_pr_dbg(fmt, ...) \
537ce1cb680SDavid Cohen 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
538ce1cb680SDavid Cohen #define __pm_deferred_pr_dbg(fmt, ...) \
539ce1cb680SDavid Cohen 	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
540b2df1d4fSRafael J. Wysocki #endif
541b2df1d4fSRafael J. Wysocki 
542ce1cb680SDavid Cohen /**
543ce1cb680SDavid Cohen  * pm_pr_dbg - print pm sleep debug messages
544ce1cb680SDavid Cohen  *
545cdb8c100SMario Limonciello  * If pm_debug_messages_on is enabled and the system is entering/leaving
546cdb8c100SMario Limonciello  *      suspend, print message.
547ce1cb680SDavid Cohen  * If pm_debug_messages_on is disabled and CONFIG_DYNAMIC_DEBUG is enabled,
548ce1cb680SDavid Cohen  *	print message only from instances explicitly enabled on dynamic debug's
549ce1cb680SDavid Cohen  *	control.
550ce1cb680SDavid Cohen  * If pm_debug_messages_on is disabled and CONFIG_DYNAMIC_DEBUG is disabled,
551ce1cb680SDavid Cohen  *	don't print message.
552ce1cb680SDavid Cohen  */
553cb08e035SRafael J. Wysocki #define pm_pr_dbg(fmt, ...) \
554ce1cb680SDavid Cohen 	__pm_pr_dbg(fmt, ##__VA_ARGS__)
555cb08e035SRafael J. Wysocki 
556cb08e035SRafael J. Wysocki #define pm_deferred_pr_dbg(fmt, ...) \
557ce1cb680SDavid Cohen 	__pm_deferred_pr_dbg(fmt, ##__VA_ARGS__)
558cb08e035SRafael J. Wysocki 
5597483b4a4SRafael J. Wysocki #ifdef CONFIG_PM_AUTOSLEEP
5607483b4a4SRafael J. Wysocki 
5617483b4a4SRafael J. Wysocki /* kernel/power/autosleep.c */
5627483b4a4SRafael J. Wysocki void queue_up_suspend_work(void);
5637483b4a4SRafael J. Wysocki 
5647483b4a4SRafael J. Wysocki #else /* !CONFIG_PM_AUTOSLEEP */
5657483b4a4SRafael J. Wysocki 
queue_up_suspend_work(void)5667483b4a4SRafael J. Wysocki static inline void queue_up_suspend_work(void) {}
5677483b4a4SRafael J. Wysocki 
5687483b4a4SRafael J. Wysocki #endif /* !CONFIG_PM_AUTOSLEEP */
5697483b4a4SRafael J. Wysocki 
570*9ff544faSRafael J. Wysocki enum suspend_stat_step {
571*9ff544faSRafael J. Wysocki 	SUSPEND_WORKING = 0,
572*9ff544faSRafael J. Wysocki 	SUSPEND_FREEZE,
573*9ff544faSRafael J. Wysocki 	SUSPEND_PREPARE,
574*9ff544faSRafael J. Wysocki 	SUSPEND_SUSPEND,
575*9ff544faSRafael J. Wysocki 	SUSPEND_SUSPEND_LATE,
576*9ff544faSRafael J. Wysocki 	SUSPEND_SUSPEND_NOIRQ,
577*9ff544faSRafael J. Wysocki 	SUSPEND_RESUME_NOIRQ,
578*9ff544faSRafael J. Wysocki 	SUSPEND_RESUME_EARLY,
579*9ff544faSRafael J. Wysocki 	SUSPEND_RESUME
580*9ff544faSRafael J. Wysocki };
581*9ff544faSRafael J. Wysocki 
582*9ff544faSRafael J. Wysocki void dpm_save_failed_dev(const char *name);
583*9ff544faSRafael J. Wysocki void dpm_save_failed_step(enum suspend_stat_step step);
584*9ff544faSRafael J. Wysocki 
58595d9ffbeSRafael J. Wysocki #endif /* _LINUX_SUSPEND_H */
586