1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SUSPEND_H
3 #define _LINUX_SUSPEND_H
5 #include <linux/swap.h>
6 #include <linux/notifier.h>
7 #include <linux/init.h>
10 #include <linux/freezer.h>
11 #include <asm/errno.h>
14 extern void pm_set_vt_switch(int);
16 static inline void pm_set_vt_switch(int do_switch)
21 #ifdef CONFIG_VT_CONSOLE_SLEEP
22 extern void pm_prepare_console(void);
23 extern void pm_restore_console(void);
25 static inline void pm_prepare_console(void)
29 static inline void pm_restore_console(void)
34 typedef int __bitwise suspend_state_t;
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)
44 * struct platform_suspend_ops - Callbacks for managing platform dependent
45 * system sleep states.
47 * @valid: Callback to determine if given system sleep state is supported by
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.
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.
63 * @prepare: Prepare the platform for entering the system sleep state indicated
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).
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
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
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.
93 * @finish: Finish wake-up of the platform.
94 * @finish is called right prior to calling device drivers' regular suspend
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
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.
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
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.
121 struct 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);
128 void (*finish)(void);
129 bool (*suspend_again)(void);
131 void (*recover)(void);
134 struct platform_s2idle_ops {
136 int (*prepare)(void);
137 int (*prepare_late)(void);
140 void (*restore_early)(void);
141 void (*restore)(void);
145 #ifdef CONFIG_SUSPEND
146 extern suspend_state_t pm_suspend_target_state;
147 extern suspend_state_t mem_sleep_current;
148 extern suspend_state_t mem_sleep_default;
151 * suspend_set_ops - set platform dependent suspend operations
152 * @ops: The new suspend operations to set.
154 extern void suspend_set_ops(const struct platform_suspend_ops *ops);
155 extern int suspend_valid_only_mem(suspend_state_t state);
157 extern unsigned int pm_suspend_global_flags;
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)
163 static inline void pm_suspend_clear_flags(void)
165 pm_suspend_global_flags = 0;
168 static inline void pm_set_suspend_via_firmware(void)
170 pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_SUSPEND;
173 static inline void pm_set_resume_via_firmware(void)
175 pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_RESUME;
178 static inline void pm_set_suspend_no_platform(void)
180 pm_suspend_global_flags |= PM_SUSPEND_FLAG_NO_PLATFORM;
184 * pm_suspend_via_firmware - Check if platform firmware will suspend the system.
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.
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
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).
203 static inline bool pm_suspend_via_firmware(void)
205 return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_SUSPEND);
209 * pm_resume_via_firmware - Check if platform firmware has woken up the system.
211 * To be called during system-wide power management transitions from sleep
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
219 static inline bool pm_resume_via_firmware(void)
221 return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_RESUME);
225 * pm_suspend_no_platform - Check if platform may change device power states.
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.
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).
235 static inline bool pm_suspend_no_platform(void)
237 return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_NO_PLATFORM);
240 /* Suspend-to-idle state machnine. */
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. */
247 extern enum s2idle_states __read_mostly s2idle_state;
249 static inline bool idle_should_enter_s2idle(void)
251 return unlikely(s2idle_state == S2IDLE_STATE_ENTER);
254 extern bool pm_suspend_default_s2idle(void);
255 extern void __init pm_states_init(void);
256 extern void s2idle_set_ops(const struct platform_s2idle_ops *ops);
257 extern void s2idle_wake(void);
260 * arch_suspend_disable_irqs - disable IRQs for suspend
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.
266 extern void arch_suspend_disable_irqs(void);
269 * arch_suspend_enable_irqs - enable IRQs after suspend
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.
275 extern void arch_suspend_enable_irqs(void);
277 extern int pm_suspend(suspend_state_t state);
278 extern bool sync_on_suspend_enabled;
279 #else /* !CONFIG_SUSPEND */
280 #define suspend_valid_only_mem NULL
282 #define pm_suspend_target_state (PM_SUSPEND_ON)
284 static inline void pm_suspend_clear_flags(void) {}
285 static inline void pm_set_suspend_via_firmware(void) {}
286 static inline void pm_set_resume_via_firmware(void) {}
287 static inline bool pm_suspend_via_firmware(void) { return false; }
288 static inline bool pm_resume_via_firmware(void) { return false; }
289 static inline bool pm_suspend_no_platform(void) { return false; }
290 static inline bool pm_suspend_default_s2idle(void) { return false; }
292 static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
293 static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
294 static inline bool sync_on_suspend_enabled(void) { return true; }
295 static inline bool idle_should_enter_s2idle(void) { return false; }
296 static inline void __init pm_states_init(void) {}
297 static inline void s2idle_set_ops(const struct platform_s2idle_ops *ops) {}
298 static inline void s2idle_wake(void) {}
299 #endif /* !CONFIG_SUSPEND */
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.
306 void *address; /* address of the copy */
307 void *orig_address; /* original address of a page */
312 * struct platform_hibernation_ops - hibernation platform support
314 * The methods in this structure allow a platform to carry out special
315 * operations required by it during a hibernation transition.
317 * All the methods below, except for @recover(), must be implemented.
319 * @begin: Tell the platform driver that we're starting hibernation.
320 * Called right after shrinking memory and before freezing devices.
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.
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).
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).
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.
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).
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.
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).
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).
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.
363 struct platform_hibernation_ops {
364 int (*begin)(pm_message_t stage);
366 int (*pre_snapshot)(void);
367 void (*finish)(void);
368 int (*prepare)(void);
371 int (*pre_restore)(void);
372 void (*restore_cleanup)(void);
373 void (*recover)(void);
376 #ifdef CONFIG_HIBERNATION
377 /* kernel/power/snapshot.c */
378 extern void register_nosave_region(unsigned long b, unsigned long e);
379 extern int swsusp_page_is_forbidden(struct page *);
380 extern void swsusp_set_page_free(struct page *);
381 extern void swsusp_unset_page_free(struct page *);
382 extern unsigned long get_safe_page(gfp_t gfp_mask);
383 extern asmlinkage int swsusp_arch_suspend(void);
384 extern asmlinkage int swsusp_arch_resume(void);
386 extern u32 swsusp_hardware_signature;
387 extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
388 extern int hibernate(void);
389 extern bool system_entering_hibernation(void);
390 extern bool hibernation_available(void);
391 asmlinkage int swsusp_save(void);
392 extern struct pbe *restore_pblist;
393 int pfn_is_nosave(unsigned long pfn);
395 int hibernate_quiet_exec(int (*func)(void *data), void *data);
396 int hibernate_resume_nonboot_cpu_disable(void);
397 int arch_hibernation_header_save(void *addr, unsigned int max_size);
398 int arch_hibernation_header_restore(void *addr);
400 #else /* CONFIG_HIBERNATION */
401 static inline void register_nosave_region(unsigned long b, unsigned long e) {}
402 static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
403 static inline void swsusp_set_page_free(struct page *p) {}
404 static inline void swsusp_unset_page_free(struct page *p) {}
406 static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
407 static inline int hibernate(void) { return -ENOSYS; }
408 static inline bool system_entering_hibernation(void) { return false; }
409 static inline bool hibernation_available(void) { return false; }
411 static inline int hibernate_quiet_exec(int (*func)(void *data), void *data) {
414 #endif /* CONFIG_HIBERNATION */
416 int arch_resume_nosmt(void);
418 #ifdef CONFIG_HIBERNATION_SNAPSHOT_DEV
419 int is_hibernate_resume_dev(dev_t dev);
421 static inline int is_hibernate_resume_dev(dev_t dev) { return 0; }
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 */
432 extern struct mutex system_transition_mutex;
434 #ifdef CONFIG_PM_SLEEP
435 void save_processor_state(void);
436 void restore_processor_state(void);
438 /* kernel/power/main.c */
439 extern int register_pm_notifier(struct notifier_block *nb);
440 extern int unregister_pm_notifier(struct notifier_block *nb);
441 extern void ksys_sync_helper(void);
442 extern void pm_report_hw_sleep_time(u64 t);
443 extern void pm_report_max_hw_sleep(u64 t);
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); \
451 /* drivers/base/power/wakeup.c */
452 extern bool events_check_enabled;
454 static inline bool pm_suspended_storage(void)
456 return !gfp_has_io_fs(gfp_allowed_mask);
459 extern bool pm_wakeup_pending(void);
460 extern void pm_system_wakeup(void);
461 extern void pm_system_cancel_wakeup(void);
462 extern void pm_wakeup_clear(unsigned int irq_number);
463 extern void pm_system_irq_wakeup(unsigned int irq_number);
464 extern unsigned int pm_wakeup_irq(void);
465 extern bool pm_get_wakeup_count(unsigned int *count, bool block);
466 extern bool pm_save_wakeup_count(unsigned int count);
467 extern void pm_wakep_autosleep_enabled(bool set);
468 extern void pm_print_active_wakeup_sources(void);
470 extern unsigned int lock_system_sleep(void);
471 extern void unlock_system_sleep(unsigned int);
473 #else /* !CONFIG_PM_SLEEP */
475 static inline int register_pm_notifier(struct notifier_block *nb)
480 static inline int unregister_pm_notifier(struct notifier_block *nb)
485 static inline void pm_report_hw_sleep_time(u64 t) {};
486 static inline void pm_report_max_hw_sleep(u64 t) {};
488 static inline void ksys_sync_helper(void) {}
490 #define pm_notifier(fn, pri) do { (void)(fn); } while (0)
492 static inline bool pm_suspended_storage(void) { return false; }
493 static inline bool pm_wakeup_pending(void) { return false; }
494 static inline void pm_system_wakeup(void) {}
495 static inline void pm_wakeup_clear(bool reset) {}
496 static inline void pm_system_irq_wakeup(unsigned int irq_number) {}
498 static inline unsigned int lock_system_sleep(void) { return 0; }
499 static inline void unlock_system_sleep(unsigned int flags) {}
501 #endif /* !CONFIG_PM_SLEEP */
503 #ifdef CONFIG_PM_SLEEP_DEBUG
504 extern bool pm_print_times_enabled;
505 extern bool pm_debug_messages_on;
506 extern bool pm_debug_messages_should_print(void);
507 static inline int pm_dyn_debug_messages_on(void)
509 #ifdef CONFIG_DYNAMIC_DEBUG
516 #define pr_fmt(fmt) "PM: " fmt
518 #define __pm_pr_dbg(fmt, ...) \
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__); \
525 #define __pm_deferred_pr_dbg(fmt, ...) \
527 if (pm_debug_messages_should_print()) \
528 printk_deferred(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
531 #define pm_print_times_enabled (false)
532 #define pm_debug_messages_on (false)
534 #include <linux/printk.h>
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__)
543 * pm_pr_dbg - print pm sleep debug messages
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
550 * If pm_debug_messages_on is disabled and CONFIG_DYNAMIC_DEBUG is disabled,
551 * don't print message.
553 #define pm_pr_dbg(fmt, ...) \
554 __pm_pr_dbg(fmt, ##__VA_ARGS__)
556 #define pm_deferred_pr_dbg(fmt, ...) \
557 __pm_deferred_pr_dbg(fmt, ##__VA_ARGS__)
559 #ifdef CONFIG_PM_AUTOSLEEP
561 /* kernel/power/autosleep.c */
562 void queue_up_suspend_work(void);
564 #else /* !CONFIG_PM_AUTOSLEEP */
566 static inline void queue_up_suspend_work(void) {}
568 #endif /* !CONFIG_PM_AUTOSLEEP */
570 enum suspend_stat_step {
575 SUSPEND_SUSPEND_LATE,
576 SUSPEND_SUSPEND_NOIRQ,
577 SUSPEND_RESUME_NOIRQ,
578 SUSPEND_RESUME_EARLY,
582 void dpm_save_failed_dev(const char *name);
583 void dpm_save_failed_step(enum suspend_stat_step step);
585 #endif /* _LINUX_SUSPEND_H */