1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_SUSPEND_H
3#define _LINUX_SUSPEND_H
4
5#include <linux/swap.h>
6#include <linux/notifier.h>
7#include <linux/init.h>
8#include <linux/pm.h>
9#include <linux/mm.h>
10#include <linux/freezer.h>
11#include <asm/errno.h>
12
13#ifdef CONFIG_VT
14extern void pm_set_vt_switch(int);
15#else
16static inline void pm_set_vt_switch(int do_switch)
17{
18}
19#endif
20
21#ifdef CONFIG_VT_CONSOLE_SLEEP
22extern void pm_prepare_console(void);
23extern void pm_restore_console(void);
24#else
25static inline void pm_prepare_console(void)
26{
27}
28
29static inline void pm_restore_console(void)
30{
31}
32#endif
33
34typedef int __bitwise suspend_state_t;
35
36#define PM_SUSPEND_ON		((__force suspend_state_t) 0)
37#define PM_SUSPEND_TO_IDLE	((__force suspend_state_t) 1)
38#define PM_SUSPEND_STANDBY	((__force suspend_state_t) 2)
39#define PM_SUSPEND_MEM		((__force suspend_state_t) 3)
40#define PM_SUSPEND_MIN		PM_SUSPEND_TO_IDLE
41#define PM_SUSPEND_MAX		((__force suspend_state_t) 4)
42
43/**
44 * struct platform_suspend_ops - Callbacks for managing platform dependent
45 *	system sleep states.
46 *
47 * @valid: Callback to determine if given system sleep state is supported by
48 *	the platform.
49 *	Valid (ie. supported) states are advertised in /sys/power/state.  Note
50 *	that it still may be impossible to enter given system sleep state if the
51 *	conditions aren't right.
52 *	There is the %suspend_valid_only_mem function available that can be
53 *	assigned to this if the platform only supports mem sleep.
54 *
55 * @begin: Initialise a transition to given system sleep state.
56 *	@begin() is executed right prior to suspending devices.  The information
57 *	conveyed to the platform code by @begin() should be disregarded by it as
58 *	soon as @end() is executed.  If @begin() fails (ie. returns nonzero),
59 *	@prepare(), @enter() and @finish() will not be called by the PM core.
60 *	This callback is optional.  However, if it is implemented, the argument
61 *	passed to @enter() is redundant and should be ignored.
62 *
63 * @prepare: Prepare the platform for entering the system sleep state indicated
64 *	by @begin().
65 *	@prepare() is called right after devices have been suspended (ie. the
66 *	appropriate .suspend() method has been executed for each device) and
67 *	before device drivers' late suspend callbacks are executed.  It returns
68 *	0 on success or a negative error code otherwise, in which case the
69 *	system cannot enter the desired sleep state (@prepare_late(), @enter(),
70 *	and @wake() will not be called in that case).
71 *
72 * @prepare_late: Finish preparing the platform for entering the system sleep
73 *	state indicated by @begin().
74 *	@prepare_late is called before disabling nonboot CPUs and after
75 *	device drivers' late suspend callbacks have been executed.  It returns
76 *	0 on success or a negative error code otherwise, in which case the
77 *	system cannot enter the desired sleep state (@enter() will not be
78 *	executed).
79 *
80 * @enter: Enter the system sleep state indicated by @begin() or represented by
81 *	the argument if @begin() is not implemented.
82 *	This callback is mandatory.  It returns 0 on success or a negative
83 *	error code otherwise, in which case the system cannot enter the desired
84 *	sleep state.
85 *
86 * @wake: Called when the system has just left a sleep state, right after
87 *	the nonboot CPUs have been enabled and before device drivers' early
88 *	resume callbacks are executed.
89 *	This callback is optional, but should be implemented by the platforms
90 *	that implement @prepare_late().  If implemented, it is always called
91 *	after @prepare_late and @enter(), even if one of them fails.
92 *
93 * @finish: Finish wake-up of the platform.
94 *	@finish is called right prior to calling device drivers' regular suspend
95 *	callbacks.
96 *	This callback is optional, but should be implemented by the platforms
97 *	that implement @prepare().  If implemented, it is always called after
98 *	@enter() and @wake(), even if any of them fails.  It is executed after
99 *	a failing @prepare.
100 *
101 * @suspend_again: Returns whether the system should suspend again (true) or
102 *	not (false). If the platform wants to poll sensors or execute some
103 *	code during suspended without invoking userspace and most of devices,
104 *	suspend_again callback is the place assuming that periodic-wakeup or
105 *	alarm-wakeup is already setup. This allows to execute some codes while
106 *	being kept suspended in the view of userland and devices.
107 *
108 * @end: Called by the PM core right after resuming devices, to indicate to
109 *	the platform that the system has returned to the working state or
110 *	the transition to the sleep state has been aborted.
111 *	This callback is optional, but should be implemented by the platforms
112 *	that implement @begin().  Accordingly, platforms implementing @begin()
113 *	should also provide a @end() which cleans up transitions aborted before
114 *	@enter().
115 *
116 * @recover: Recover the platform from a suspend failure.
117 *	Called by the PM core if the suspending of devices fails.
118 *	This callback is optional and should only be implemented by platforms
119 *	which require special recovery actions in that situation.
120 */
121struct platform_suspend_ops {
122	int (*valid)(suspend_state_t state);
123	int (*begin)(suspend_state_t state);
124	int (*prepare)(void);
125	int (*prepare_late)(void);
126	int (*enter)(suspend_state_t state);
127	void (*wake)(void);
128	void (*finish)(void);
129	bool (*suspend_again)(void);
130	void (*end)(void);
131	void (*recover)(void);
132};
133
134struct platform_s2idle_ops {
135	int (*begin)(void);
136	int (*prepare)(void);
137	int (*prepare_late)(void);
138	void (*check)(void);
139	bool (*wake)(void);
140	void (*restore_early)(void);
141	void (*restore)(void);
142	void (*end)(void);
143};
144
145#ifdef CONFIG_SUSPEND
146extern suspend_state_t pm_suspend_target_state;
147extern suspend_state_t mem_sleep_current;
148extern suspend_state_t mem_sleep_default;
149
150/**
151 * suspend_set_ops - set platform dependent suspend operations
152 * @ops: The new suspend operations to set.
153 */
154extern void suspend_set_ops(const struct platform_suspend_ops *ops);
155extern int suspend_valid_only_mem(suspend_state_t state);
156
157extern unsigned int pm_suspend_global_flags;
158
159#define PM_SUSPEND_FLAG_FW_SUSPEND	BIT(0)
160#define PM_SUSPEND_FLAG_FW_RESUME	BIT(1)
161#define PM_SUSPEND_FLAG_NO_PLATFORM	BIT(2)
162
163static inline void pm_suspend_clear_flags(void)
164{
165	pm_suspend_global_flags = 0;
166}
167
168static inline void pm_set_suspend_via_firmware(void)
169{
170	pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_SUSPEND;
171}
172
173static inline void pm_set_resume_via_firmware(void)
174{
175	pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_RESUME;
176}
177
178static inline void pm_set_suspend_no_platform(void)
179{
180	pm_suspend_global_flags |= PM_SUSPEND_FLAG_NO_PLATFORM;
181}
182
183/**
184 * pm_suspend_via_firmware - Check if platform firmware will suspend the system.
185 *
186 * To be called during system-wide power management transitions to sleep states
187 * or during the subsequent system-wide transitions back to the working state.
188 *
189 * Return 'true' if the platform firmware is going to be invoked at the end of
190 * the system-wide power management transition (to a sleep state) in progress in
191 * order to complete it, or if the platform firmware has been invoked in order
192 * to complete the last (or preceding) transition of the system to a sleep
193 * state.
194 *
195 * This matters if the caller needs or wants to carry out some special actions
196 * depending on whether or not control will be passed to the platform firmware
197 * subsequently (for example, the device may need to be reset before letting the
198 * platform firmware manipulate it, which is not necessary when the platform
199 * firmware is not going to be invoked) or when such special actions may have
200 * been carried out during the preceding transition of the system to a sleep
201 * state (as they may need to be taken into account).
202 */
203static inline bool pm_suspend_via_firmware(void)
204{
205	return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_SUSPEND);
206}
207
208/**
209 * pm_resume_via_firmware - Check if platform firmware has woken up the system.
210 *
211 * To be called during system-wide power management transitions from sleep
212 * states.
213 *
214 * Return 'true' if the platform firmware has passed control to the kernel at
215 * the beginning of the system-wide power management transition in progress, so
216 * the event that woke up the system from sleep has been handled by the platform
217 * firmware.
218 */
219static inline bool pm_resume_via_firmware(void)
220{
221	return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_RESUME);
222}
223
224/**
225 * pm_suspend_no_platform - Check if platform may change device power states.
226 *
227 * To be called during system-wide power management transitions to sleep states
228 * or during the subsequent system-wide transitions back to the working state.
229 *
230 * Return 'true' if the power states of devices remain under full control of the
231 * kernel throughout the system-wide suspend and resume cycle in progress (that
232 * is, if a device is put into a certain power state during suspend, it can be
233 * expected to remain in that state during resume).
234 */
235static inline bool pm_suspend_no_platform(void)
236{
237	return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_NO_PLATFORM);
238}
239
240/* Suspend-to-idle state machnine. */
241enum s2idle_states {
242	S2IDLE_STATE_NONE,      /* Not suspended/suspending. */
243	S2IDLE_STATE_ENTER,     /* Enter suspend-to-idle. */
244	S2IDLE_STATE_WAKE,      /* Wake up from suspend-to-idle. */
245};
246
247extern enum s2idle_states __read_mostly s2idle_state;
248
249static inline bool idle_should_enter_s2idle(void)
250{
251	return unlikely(s2idle_state == S2IDLE_STATE_ENTER);
252}
253
254extern bool pm_suspend_default_s2idle(void);
255extern void __init pm_states_init(void);
256extern void s2idle_set_ops(const struct platform_s2idle_ops *ops);
257extern void s2idle_wake(void);
258
259/**
260 * arch_suspend_disable_irqs - disable IRQs for suspend
261 *
262 * Disables IRQs (in the default case). This is a weak symbol in the common
263 * code and thus allows architectures to override it if more needs to be
264 * done. Not called for suspend to disk.
265 */
266extern void arch_suspend_disable_irqs(void);
267
268/**
269 * arch_suspend_enable_irqs - enable IRQs after suspend
270 *
271 * Enables IRQs (in the default case). This is a weak symbol in the common
272 * code and thus allows architectures to override it if more needs to be
273 * done. Not called for suspend to disk.
274 */
275extern void arch_suspend_enable_irqs(void);
276
277extern int pm_suspend(suspend_state_t state);
278extern bool sync_on_suspend_enabled;
279#else /* !CONFIG_SUSPEND */
280#define suspend_valid_only_mem	NULL
281
282#define pm_suspend_target_state	(PM_SUSPEND_ON)
283
284static inline void pm_suspend_clear_flags(void) {}
285static inline void pm_set_suspend_via_firmware(void) {}
286static inline void pm_set_resume_via_firmware(void) {}
287static inline bool pm_suspend_via_firmware(void) { return false; }
288static inline bool pm_resume_via_firmware(void) { return false; }
289static inline bool pm_suspend_no_platform(void) { return false; }
290static inline bool pm_suspend_default_s2idle(void) { return false; }
291
292static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
293static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
294static inline bool sync_on_suspend_enabled(void) { return true; }
295static inline bool idle_should_enter_s2idle(void) { return false; }
296static inline void __init pm_states_init(void) {}
297static inline void s2idle_set_ops(const struct platform_s2idle_ops *ops) {}
298static inline void s2idle_wake(void) {}
299#endif /* !CONFIG_SUSPEND */
300
301/* struct pbe is used for creating lists of pages that should be restored
302 * atomically during the resume from disk, because the page frames they have
303 * occupied before the suspend are in use.
304 */
305struct pbe {
306	void *address;		/* address of the copy */
307	void *orig_address;	/* original address of a page */
308	struct pbe *next;
309};
310
311/**
312 * struct platform_hibernation_ops - hibernation platform support
313 *
314 * The methods in this structure allow a platform to carry out special
315 * operations required by it during a hibernation transition.
316 *
317 * All the methods below, except for @recover(), must be implemented.
318 *
319 * @begin: Tell the platform driver that we're starting hibernation.
320 *	Called right after shrinking memory and before freezing devices.
321 *
322 * @end: Called by the PM core right after resuming devices, to indicate to
323 *	the platform that the system has returned to the working state.
324 *
325 * @pre_snapshot: Prepare the platform for creating the hibernation image.
326 *	Called right after devices have been frozen and before the nonboot
327 *	CPUs are disabled (runs with IRQs on).
328 *
329 * @finish: Restore the previous state of the platform after the hibernation
330 *	image has been created *or* put the platform into the normal operation
331 *	mode after the hibernation (the same method is executed in both cases).
332 *	Called right after the nonboot CPUs have been enabled and before
333 *	thawing devices (runs with IRQs on).
334 *
335 * @prepare: Prepare the platform for entering the low power state.
336 *	Called right after the hibernation image has been saved and before
337 *	devices are prepared for entering the low power state.
338 *
339 * @enter: Put the system into the low power state after the hibernation image
340 *	has been saved to disk.
341 *	Called after the nonboot CPUs have been disabled and all of the low
342 *	level devices have been shut down (runs with IRQs off).
343 *
344 * @leave: Perform the first stage of the cleanup after the system sleep state
345 *	indicated by @set_target() has been left.
346 *	Called right after the control has been passed from the boot kernel to
347 *	the image kernel, before the nonboot CPUs are enabled and before devices
348 *	are resumed.  Executed with interrupts disabled.
349 *
350 * @pre_restore: Prepare system for the restoration from a hibernation image.
351 *	Called right after devices have been frozen and before the nonboot
352 *	CPUs are disabled (runs with IRQs on).
353 *
354 * @restore_cleanup: Clean up after a failing image restoration.
355 *	Called right after the nonboot CPUs have been enabled and before
356 *	thawing devices (runs with IRQs on).
357 *
358 * @recover: Recover the platform from a failure to suspend devices.
359 *	Called by the PM core if the suspending of devices during hibernation
360 *	fails.  This callback is optional and should only be implemented by
361 *	platforms which require special recovery actions in that situation.
362 */
363struct platform_hibernation_ops {
364	int (*begin)(pm_message_t stage);
365	void (*end)(void);
366	int (*pre_snapshot)(void);
367	void (*finish)(void);
368	int (*prepare)(void);
369	int (*enter)(void);
370	void (*leave)(void);
371	int (*pre_restore)(void);
372	void (*restore_cleanup)(void);
373	void (*recover)(void);
374};
375
376#ifdef CONFIG_HIBERNATION
377/* kernel/power/snapshot.c */
378extern void register_nosave_region(unsigned long b, unsigned long e);
379extern int swsusp_page_is_forbidden(struct page *);
380extern void swsusp_set_page_free(struct page *);
381extern void swsusp_unset_page_free(struct page *);
382extern unsigned long get_safe_page(gfp_t gfp_mask);
383extern asmlinkage int swsusp_arch_suspend(void);
384extern asmlinkage int swsusp_arch_resume(void);
385
386extern u32 swsusp_hardware_signature;
387extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
388extern int hibernate(void);
389extern bool system_entering_hibernation(void);
390extern bool hibernation_available(void);
391asmlinkage int swsusp_save(void);
392extern struct pbe *restore_pblist;
393int pfn_is_nosave(unsigned long pfn);
394
395int hibernate_quiet_exec(int (*func)(void *data), void *data);
396int hibernate_resume_nonboot_cpu_disable(void);
397int arch_hibernation_header_save(void *addr, unsigned int max_size);
398int arch_hibernation_header_restore(void *addr);
399
400#else /* CONFIG_HIBERNATION */
401static inline void register_nosave_region(unsigned long b, unsigned long e) {}
402static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
403static inline void swsusp_set_page_free(struct page *p) {}
404static inline void swsusp_unset_page_free(struct page *p) {}
405
406static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
407static inline int hibernate(void) { return -ENOSYS; }
408static inline bool system_entering_hibernation(void) { return false; }
409static inline bool hibernation_available(void) { return false; }
410
411static inline int hibernate_quiet_exec(int (*func)(void *data), void *data) {
412	return -ENOTSUPP;
413}
414#endif /* CONFIG_HIBERNATION */
415
416int arch_resume_nosmt(void);
417
418#ifdef CONFIG_HIBERNATION_SNAPSHOT_DEV
419int is_hibernate_resume_dev(dev_t dev);
420#else
421static inline int is_hibernate_resume_dev(dev_t dev) { return 0; }
422#endif
423
424/* Hibernation and suspend events */
425#define PM_HIBERNATION_PREPARE	0x0001 /* Going to hibernate */
426#define PM_POST_HIBERNATION	0x0002 /* Hibernation finished */
427#define PM_SUSPEND_PREPARE	0x0003 /* Going to suspend the system */
428#define PM_POST_SUSPEND		0x0004 /* Suspend finished */
429#define PM_RESTORE_PREPARE	0x0005 /* Going to restore a saved image */
430#define PM_POST_RESTORE		0x0006 /* Restore failed */
431
432extern struct mutex system_transition_mutex;
433
434#ifdef CONFIG_PM_SLEEP
435void save_processor_state(void);
436void restore_processor_state(void);
437
438/* kernel/power/main.c */
439extern int register_pm_notifier(struct notifier_block *nb);
440extern int unregister_pm_notifier(struct notifier_block *nb);
441extern void ksys_sync_helper(void);
442extern void pm_report_hw_sleep_time(u64 t);
443extern void pm_report_max_hw_sleep(u64 t);
444
445#define pm_notifier(fn, pri) {				\
446	static struct notifier_block fn##_nb =			\
447		{ .notifier_call = fn, .priority = pri };	\
448	register_pm_notifier(&fn##_nb);			\
449}
450
451/* drivers/base/power/wakeup.c */
452extern bool events_check_enabled;
453
454static inline bool pm_suspended_storage(void)
455{
456	return !gfp_has_io_fs(gfp_allowed_mask);
457}
458
459extern bool pm_wakeup_pending(void);
460extern void pm_system_wakeup(void);
461extern void pm_system_cancel_wakeup(void);
462extern void pm_wakeup_clear(unsigned int irq_number);
463extern void pm_system_irq_wakeup(unsigned int irq_number);
464extern unsigned int pm_wakeup_irq(void);
465extern bool pm_get_wakeup_count(unsigned int *count, bool block);
466extern bool pm_save_wakeup_count(unsigned int count);
467extern void pm_wakep_autosleep_enabled(bool set);
468extern void pm_print_active_wakeup_sources(void);
469
470extern unsigned int lock_system_sleep(void);
471extern void unlock_system_sleep(unsigned int);
472
473#else /* !CONFIG_PM_SLEEP */
474
475static inline int register_pm_notifier(struct notifier_block *nb)
476{
477	return 0;
478}
479
480static inline int unregister_pm_notifier(struct notifier_block *nb)
481{
482	return 0;
483}
484
485static inline void pm_report_hw_sleep_time(u64 t) {};
486static inline void pm_report_max_hw_sleep(u64 t) {};
487
488static inline void ksys_sync_helper(void) {}
489
490#define pm_notifier(fn, pri)	do { (void)(fn); } while (0)
491
492static inline bool pm_suspended_storage(void) { return false; }
493static inline bool pm_wakeup_pending(void) { return false; }
494static inline void pm_system_wakeup(void) {}
495static inline void pm_wakeup_clear(bool reset) {}
496static inline void pm_system_irq_wakeup(unsigned int irq_number) {}
497
498static inline unsigned int lock_system_sleep(void) { return 0; }
499static inline void unlock_system_sleep(unsigned int flags) {}
500
501#endif /* !CONFIG_PM_SLEEP */
502
503#ifdef CONFIG_PM_SLEEP_DEBUG
504extern bool pm_print_times_enabled;
505extern bool pm_debug_messages_on;
506extern bool pm_debug_messages_should_print(void);
507static inline int pm_dyn_debug_messages_on(void)
508{
509#ifdef CONFIG_DYNAMIC_DEBUG
510	return 1;
511#else
512	return 0;
513#endif
514}
515#ifndef pr_fmt
516#define pr_fmt(fmt) "PM: " fmt
517#endif
518#define __pm_pr_dbg(fmt, ...)					\
519	do {							\
520		if (pm_debug_messages_should_print())		\
521			printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__);	\
522		else if (pm_dyn_debug_messages_on())		\
523			pr_debug(fmt, ##__VA_ARGS__);	\
524	} while (0)
525#define __pm_deferred_pr_dbg(fmt, ...)				\
526	do {							\
527		if (pm_debug_messages_should_print())		\
528			printk_deferred(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__);	\
529	} while (0)
530#else
531#define pm_print_times_enabled	(false)
532#define pm_debug_messages_on	(false)
533
534#include <linux/printk.h>
535
536#define __pm_pr_dbg(fmt, ...) \
537	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
538#define __pm_deferred_pr_dbg(fmt, ...) \
539	no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
540#endif
541
542/**
543 * pm_pr_dbg - print pm sleep debug messages
544 *
545 * If pm_debug_messages_on is enabled and the system is entering/leaving
546 *      suspend, print message.
547 * If pm_debug_messages_on is disabled and CONFIG_DYNAMIC_DEBUG is enabled,
548 *	print message only from instances explicitly enabled on dynamic debug's
549 *	control.
550 * If pm_debug_messages_on is disabled and CONFIG_DYNAMIC_DEBUG is disabled,
551 *	don't print message.
552 */
553#define pm_pr_dbg(fmt, ...) \
554	__pm_pr_dbg(fmt, ##__VA_ARGS__)
555
556#define pm_deferred_pr_dbg(fmt, ...) \
557	__pm_deferred_pr_dbg(fmt, ##__VA_ARGS__)
558
559#ifdef CONFIG_PM_AUTOSLEEP
560
561/* kernel/power/autosleep.c */
562void queue_up_suspend_work(void);
563
564#else /* !CONFIG_PM_AUTOSLEEP */
565
566static inline void queue_up_suspend_work(void) {}
567
568#endif /* !CONFIG_PM_AUTOSLEEP */
569
570enum suspend_stat_step {
571	SUSPEND_WORKING = 0,
572	SUSPEND_FREEZE,
573	SUSPEND_PREPARE,
574	SUSPEND_SUSPEND,
575	SUSPEND_SUSPEND_LATE,
576	SUSPEND_SUSPEND_NOIRQ,
577	SUSPEND_RESUME_NOIRQ,
578	SUSPEND_RESUME_EARLY,
579	SUSPEND_RESUME
580};
581
582void dpm_save_failed_dev(const char *name);
583void dpm_save_failed_step(enum suspend_stat_step step);
584
585#endif /* _LINUX_SUSPEND_H */
586