Merge branch 'for-4.16/nfit' into libnvdimm-for-next
[sfrench/cifs-2.6.git] / include / linux / suspend.h
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
14 extern void pm_set_vt_switch(int);
15 #else
16 static inline void pm_set_vt_switch(int do_switch)
17 {
18 }
19 #endif
20
21 #ifdef CONFIG_VT_CONSOLE_SLEEP
22 extern void pm_prepare_console(void);
23 extern void pm_restore_console(void);
24 #else
25 static inline void pm_prepare_console(void)
26 {
27 }
28
29 static inline void pm_restore_console(void)
30 {
31 }
32 #endif
33
34 typedef 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 enum suspend_stat_step {
44         SUSPEND_FREEZE = 1,
45         SUSPEND_PREPARE,
46         SUSPEND_SUSPEND,
47         SUSPEND_SUSPEND_LATE,
48         SUSPEND_SUSPEND_NOIRQ,
49         SUSPEND_RESUME_NOIRQ,
50         SUSPEND_RESUME_EARLY,
51         SUSPEND_RESUME
52 };
53
54 struct suspend_stats {
55         int     success;
56         int     fail;
57         int     failed_freeze;
58         int     failed_prepare;
59         int     failed_suspend;
60         int     failed_suspend_late;
61         int     failed_suspend_noirq;
62         int     failed_resume;
63         int     failed_resume_early;
64         int     failed_resume_noirq;
65 #define REC_FAILED_NUM  2
66         int     last_failed_dev;
67         char    failed_devs[REC_FAILED_NUM][40];
68         int     last_failed_errno;
69         int     errno[REC_FAILED_NUM];
70         int     last_failed_step;
71         enum suspend_stat_step  failed_steps[REC_FAILED_NUM];
72 };
73
74 extern struct suspend_stats suspend_stats;
75
76 static inline void dpm_save_failed_dev(const char *name)
77 {
78         strlcpy(suspend_stats.failed_devs[suspend_stats.last_failed_dev],
79                 name,
80                 sizeof(suspend_stats.failed_devs[0]));
81         suspend_stats.last_failed_dev++;
82         suspend_stats.last_failed_dev %= REC_FAILED_NUM;
83 }
84
85 static inline void dpm_save_failed_errno(int err)
86 {
87         suspend_stats.errno[suspend_stats.last_failed_errno] = err;
88         suspend_stats.last_failed_errno++;
89         suspend_stats.last_failed_errno %= REC_FAILED_NUM;
90 }
91
92 static inline void dpm_save_failed_step(enum suspend_stat_step step)
93 {
94         suspend_stats.failed_steps[suspend_stats.last_failed_step] = step;
95         suspend_stats.last_failed_step++;
96         suspend_stats.last_failed_step %= REC_FAILED_NUM;
97 }
98
99 /**
100  * struct platform_suspend_ops - Callbacks for managing platform dependent
101  *      system sleep states.
102  *
103  * @valid: Callback to determine if given system sleep state is supported by
104  *      the platform.
105  *      Valid (ie. supported) states are advertised in /sys/power/state.  Note
106  *      that it still may be impossible to enter given system sleep state if the
107  *      conditions aren't right.
108  *      There is the %suspend_valid_only_mem function available that can be
109  *      assigned to this if the platform only supports mem sleep.
110  *
111  * @begin: Initialise a transition to given system sleep state.
112  *      @begin() is executed right prior to suspending devices.  The information
113  *      conveyed to the platform code by @begin() should be disregarded by it as
114  *      soon as @end() is executed.  If @begin() fails (ie. returns nonzero),
115  *      @prepare(), @enter() and @finish() will not be called by the PM core.
116  *      This callback is optional.  However, if it is implemented, the argument
117  *      passed to @enter() is redundant and should be ignored.
118  *
119  * @prepare: Prepare the platform for entering the system sleep state indicated
120  *      by @begin().
121  *      @prepare() is called right after devices have been suspended (ie. the
122  *      appropriate .suspend() method has been executed for each device) and
123  *      before device drivers' late suspend callbacks are executed.  It returns
124  *      0 on success or a negative error code otherwise, in which case the
125  *      system cannot enter the desired sleep state (@prepare_late(), @enter(),
126  *      and @wake() will not be called in that case).
127  *
128  * @prepare_late: Finish preparing the platform for entering the system sleep
129  *      state indicated by @begin().
130  *      @prepare_late is called before disabling nonboot CPUs and after
131  *      device drivers' late suspend callbacks have been executed.  It returns
132  *      0 on success or a negative error code otherwise, in which case the
133  *      system cannot enter the desired sleep state (@enter() will not be
134  *      executed).
135  *
136  * @enter: Enter the system sleep state indicated by @begin() or represented by
137  *      the argument if @begin() is not implemented.
138  *      This callback is mandatory.  It returns 0 on success or a negative
139  *      error code otherwise, in which case the system cannot enter the desired
140  *      sleep state.
141  *
142  * @wake: Called when the system has just left a sleep state, right after
143  *      the nonboot CPUs have been enabled and before device drivers' early
144  *      resume callbacks are executed.
145  *      This callback is optional, but should be implemented by the platforms
146  *      that implement @prepare_late().  If implemented, it is always called
147  *      after @prepare_late and @enter(), even if one of them fails.
148  *
149  * @finish: Finish wake-up of the platform.
150  *      @finish is called right prior to calling device drivers' regular suspend
151  *      callbacks.
152  *      This callback is optional, but should be implemented by the platforms
153  *      that implement @prepare().  If implemented, it is always called after
154  *      @enter() and @wake(), even if any of them fails.  It is executed after
155  *      a failing @prepare.
156  *
157  * @suspend_again: Returns whether the system should suspend again (true) or
158  *      not (false). If the platform wants to poll sensors or execute some
159  *      code during suspended without invoking userspace and most of devices,
160  *      suspend_again callback is the place assuming that periodic-wakeup or
161  *      alarm-wakeup is already setup. This allows to execute some codes while
162  *      being kept suspended in the view of userland and devices.
163  *
164  * @end: Called by the PM core right after resuming devices, to indicate to
165  *      the platform that the system has returned to the working state or
166  *      the transition to the sleep state has been aborted.
167  *      This callback is optional, but should be implemented by the platforms
168  *      that implement @begin().  Accordingly, platforms implementing @begin()
169  *      should also provide a @end() which cleans up transitions aborted before
170  *      @enter().
171  *
172  * @recover: Recover the platform from a suspend failure.
173  *      Called by the PM core if the suspending of devices fails.
174  *      This callback is optional and should only be implemented by platforms
175  *      which require special recovery actions in that situation.
176  */
177 struct platform_suspend_ops {
178         int (*valid)(suspend_state_t state);
179         int (*begin)(suspend_state_t state);
180         int (*prepare)(void);
181         int (*prepare_late)(void);
182         int (*enter)(suspend_state_t state);
183         void (*wake)(void);
184         void (*finish)(void);
185         bool (*suspend_again)(void);
186         void (*end)(void);
187         void (*recover)(void);
188 };
189
190 struct platform_s2idle_ops {
191         int (*begin)(void);
192         int (*prepare)(void);
193         void (*wake)(void);
194         void (*sync)(void);
195         void (*restore)(void);
196         void (*end)(void);
197 };
198
199 #ifdef CONFIG_SUSPEND
200 extern suspend_state_t mem_sleep_current;
201 extern suspend_state_t mem_sleep_default;
202
203 /**
204  * suspend_set_ops - set platform dependent suspend operations
205  * @ops: The new suspend operations to set.
206  */
207 extern void suspend_set_ops(const struct platform_suspend_ops *ops);
208 extern int suspend_valid_only_mem(suspend_state_t state);
209
210 extern unsigned int pm_suspend_global_flags;
211
212 #define PM_SUSPEND_FLAG_FW_SUSPEND      (1 << 0)
213 #define PM_SUSPEND_FLAG_FW_RESUME       (1 << 1)
214
215 static inline void pm_suspend_clear_flags(void)
216 {
217         pm_suspend_global_flags = 0;
218 }
219
220 static inline void pm_set_suspend_via_firmware(void)
221 {
222         pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_SUSPEND;
223 }
224
225 static inline void pm_set_resume_via_firmware(void)
226 {
227         pm_suspend_global_flags |= PM_SUSPEND_FLAG_FW_RESUME;
228 }
229
230 static inline bool pm_suspend_via_firmware(void)
231 {
232         return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_SUSPEND);
233 }
234
235 static inline bool pm_resume_via_firmware(void)
236 {
237         return !!(pm_suspend_global_flags & PM_SUSPEND_FLAG_FW_RESUME);
238 }
239
240 /* Suspend-to-idle state machnine. */
241 enum 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
247 extern enum s2idle_states __read_mostly s2idle_state;
248
249 static inline bool idle_should_enter_s2idle(void)
250 {
251         return unlikely(s2idle_state == S2IDLE_STATE_ENTER);
252 }
253
254 extern void __init pm_states_init(void);
255 extern void s2idle_set_ops(const struct platform_s2idle_ops *ops);
256 extern void s2idle_wake(void);
257
258 /**
259  * arch_suspend_disable_irqs - disable IRQs for suspend
260  *
261  * Disables IRQs (in the default case). This is a weak symbol in the common
262  * code and thus allows architectures to override it if more needs to be
263  * done. Not called for suspend to disk.
264  */
265 extern void arch_suspend_disable_irqs(void);
266
267 /**
268  * arch_suspend_enable_irqs - enable IRQs after suspend
269  *
270  * Enables IRQs (in the default case). This is a weak symbol in the common
271  * code and thus allows architectures to override it if more needs to be
272  * done. Not called for suspend to disk.
273  */
274 extern void arch_suspend_enable_irqs(void);
275
276 extern int pm_suspend(suspend_state_t state);
277 #else /* !CONFIG_SUSPEND */
278 #define suspend_valid_only_mem  NULL
279
280 static inline void pm_suspend_clear_flags(void) {}
281 static inline void pm_set_suspend_via_firmware(void) {}
282 static inline void pm_set_resume_via_firmware(void) {}
283 static inline bool pm_suspend_via_firmware(void) { return false; }
284 static inline bool pm_resume_via_firmware(void) { return false; }
285
286 static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
287 static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
288 static inline bool idle_should_enter_s2idle(void) { return false; }
289 static inline void __init pm_states_init(void) {}
290 static inline void s2idle_set_ops(const struct platform_s2idle_ops *ops) {}
291 static inline void s2idle_wake(void) {}
292 #endif /* !CONFIG_SUSPEND */
293
294 /* struct pbe is used for creating lists of pages that should be restored
295  * atomically during the resume from disk, because the page frames they have
296  * occupied before the suspend are in use.
297  */
298 struct pbe {
299         void *address;          /* address of the copy */
300         void *orig_address;     /* original address of a page */
301         struct pbe *next;
302 };
303
304 /* mm/page_alloc.c */
305 extern void mark_free_pages(struct zone *zone);
306
307 /**
308  * struct platform_hibernation_ops - hibernation platform support
309  *
310  * The methods in this structure allow a platform to carry out special
311  * operations required by it during a hibernation transition.
312  *
313  * All the methods below, except for @recover(), must be implemented.
314  *
315  * @begin: Tell the platform driver that we're starting hibernation.
316  *      Called right after shrinking memory and before freezing devices.
317  *
318  * @end: Called by the PM core right after resuming devices, to indicate to
319  *      the platform that the system has returned to the working state.
320  *
321  * @pre_snapshot: Prepare the platform for creating the hibernation image.
322  *      Called right after devices have been frozen and before the nonboot
323  *      CPUs are disabled (runs with IRQs on).
324  *
325  * @finish: Restore the previous state of the platform after the hibernation
326  *      image has been created *or* put the platform into the normal operation
327  *      mode after the hibernation (the same method is executed in both cases).
328  *      Called right after the nonboot CPUs have been enabled and before
329  *      thawing devices (runs with IRQs on).
330  *
331  * @prepare: Prepare the platform for entering the low power state.
332  *      Called right after the hibernation image has been saved and before
333  *      devices are prepared for entering the low power state.
334  *
335  * @enter: Put the system into the low power state after the hibernation image
336  *      has been saved to disk.
337  *      Called after the nonboot CPUs have been disabled and all of the low
338  *      level devices have been shut down (runs with IRQs off).
339  *
340  * @leave: Perform the first stage of the cleanup after the system sleep state
341  *      indicated by @set_target() has been left.
342  *      Called right after the control has been passed from the boot kernel to
343  *      the image kernel, before the nonboot CPUs are enabled and before devices
344  *      are resumed.  Executed with interrupts disabled.
345  *
346  * @pre_restore: Prepare system for the restoration from a hibernation image.
347  *      Called right after devices have been frozen and before the nonboot
348  *      CPUs are disabled (runs with IRQs on).
349  *
350  * @restore_cleanup: Clean up after a failing image restoration.
351  *      Called right after the nonboot CPUs have been enabled and before
352  *      thawing devices (runs with IRQs on).
353  *
354  * @recover: Recover the platform from a failure to suspend devices.
355  *      Called by the PM core if the suspending of devices during hibernation
356  *      fails.  This callback is optional and should only be implemented by
357  *      platforms which require special recovery actions in that situation.
358  */
359 struct platform_hibernation_ops {
360         int (*begin)(void);
361         void (*end)(void);
362         int (*pre_snapshot)(void);
363         void (*finish)(void);
364         int (*prepare)(void);
365         int (*enter)(void);
366         void (*leave)(void);
367         int (*pre_restore)(void);
368         void (*restore_cleanup)(void);
369         void (*recover)(void);
370 };
371
372 #ifdef CONFIG_HIBERNATION
373 /* kernel/power/snapshot.c */
374 extern void __register_nosave_region(unsigned long b, unsigned long e, int km);
375 static inline void __init register_nosave_region(unsigned long b, unsigned long e)
376 {
377         __register_nosave_region(b, e, 0);
378 }
379 static inline void __init register_nosave_region_late(unsigned long b, unsigned long e)
380 {
381         __register_nosave_region(b, e, 1);
382 }
383 extern int swsusp_page_is_forbidden(struct page *);
384 extern void swsusp_set_page_free(struct page *);
385 extern void swsusp_unset_page_free(struct page *);
386 extern unsigned long get_safe_page(gfp_t gfp_mask);
387
388 extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
389 extern int hibernate(void);
390 extern bool system_entering_hibernation(void);
391 extern bool hibernation_available(void);
392 asmlinkage int swsusp_save(void);
393 extern struct pbe *restore_pblist;
394 #else /* CONFIG_HIBERNATION */
395 static inline void register_nosave_region(unsigned long b, unsigned long e) {}
396 static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
397 static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
398 static inline void swsusp_set_page_free(struct page *p) {}
399 static inline void swsusp_unset_page_free(struct page *p) {}
400
401 static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
402 static inline int hibernate(void) { return -ENOSYS; }
403 static inline bool system_entering_hibernation(void) { return false; }
404 static inline bool hibernation_available(void) { return false; }
405 #endif /* CONFIG_HIBERNATION */
406
407 /* Hibernation and suspend events */
408 #define PM_HIBERNATION_PREPARE  0x0001 /* Going to hibernate */
409 #define PM_POST_HIBERNATION     0x0002 /* Hibernation finished */
410 #define PM_SUSPEND_PREPARE      0x0003 /* Going to suspend the system */
411 #define PM_POST_SUSPEND         0x0004 /* Suspend finished */
412 #define PM_RESTORE_PREPARE      0x0005 /* Going to restore a saved image */
413 #define PM_POST_RESTORE         0x0006 /* Restore failed */
414
415 extern struct mutex pm_mutex;
416
417 #ifdef CONFIG_PM_SLEEP
418 void save_processor_state(void);
419 void restore_processor_state(void);
420
421 /* kernel/power/main.c */
422 extern int register_pm_notifier(struct notifier_block *nb);
423 extern int unregister_pm_notifier(struct notifier_block *nb);
424
425 #define pm_notifier(fn, pri) {                          \
426         static struct notifier_block fn##_nb =                  \
427                 { .notifier_call = fn, .priority = pri };       \
428         register_pm_notifier(&fn##_nb);                 \
429 }
430
431 /* drivers/base/power/wakeup.c */
432 extern bool events_check_enabled;
433 extern unsigned int pm_wakeup_irq;
434 extern suspend_state_t pm_suspend_target_state;
435
436 extern bool pm_wakeup_pending(void);
437 extern void pm_system_wakeup(void);
438 extern void pm_system_cancel_wakeup(void);
439 extern void pm_wakeup_clear(bool reset);
440 extern void pm_system_irq_wakeup(unsigned int irq_number);
441 extern bool pm_get_wakeup_count(unsigned int *count, bool block);
442 extern bool pm_save_wakeup_count(unsigned int count);
443 extern void pm_wakep_autosleep_enabled(bool set);
444 extern void pm_print_active_wakeup_sources(void);
445
446 static inline void lock_system_sleep(void)
447 {
448         current->flags |= PF_FREEZER_SKIP;
449         mutex_lock(&pm_mutex);
450 }
451
452 static inline void unlock_system_sleep(void)
453 {
454         /*
455          * Don't use freezer_count() because we don't want the call to
456          * try_to_freeze() here.
457          *
458          * Reason:
459          * Fundamentally, we just don't need it, because freezing condition
460          * doesn't come into effect until we release the pm_mutex lock,
461          * since the freezer always works with pm_mutex held.
462          *
463          * More importantly, in the case of hibernation,
464          * unlock_system_sleep() gets called in snapshot_read() and
465          * snapshot_write() when the freezing condition is still in effect.
466          * Which means, if we use try_to_freeze() here, it would make them
467          * enter the refrigerator, thus causing hibernation to lockup.
468          */
469         current->flags &= ~PF_FREEZER_SKIP;
470         mutex_unlock(&pm_mutex);
471 }
472
473 #else /* !CONFIG_PM_SLEEP */
474
475 static inline int register_pm_notifier(struct notifier_block *nb)
476 {
477         return 0;
478 }
479
480 static inline int unregister_pm_notifier(struct notifier_block *nb)
481 {
482         return 0;
483 }
484
485 #define pm_notifier(fn, pri)    do { (void)(fn); } while (0)
486
487 static inline bool pm_wakeup_pending(void) { return false; }
488 static inline void pm_system_wakeup(void) {}
489 static inline void pm_wakeup_clear(bool reset) {}
490 static inline void pm_system_irq_wakeup(unsigned int irq_number) {}
491
492 static inline void lock_system_sleep(void) {}
493 static inline void unlock_system_sleep(void) {}
494
495 #endif /* !CONFIG_PM_SLEEP */
496
497 #ifdef CONFIG_PM_SLEEP_DEBUG
498 extern bool pm_print_times_enabled;
499 extern bool pm_debug_messages_on;
500 extern __printf(2, 3) void __pm_pr_dbg(bool defer, const char *fmt, ...);
501 #else
502 #define pm_print_times_enabled  (false)
503 #define pm_debug_messages_on    (false)
504
505 #include <linux/printk.h>
506
507 #define __pm_pr_dbg(defer, fmt, ...) \
508         no_printk(KERN_DEBUG fmt, ##__VA_ARGS__)
509 #endif
510
511 #define pm_pr_dbg(fmt, ...) \
512         __pm_pr_dbg(false, fmt, ##__VA_ARGS__)
513
514 #define pm_deferred_pr_dbg(fmt, ...) \
515         __pm_pr_dbg(true, fmt, ##__VA_ARGS__)
516
517 #ifdef CONFIG_PM_AUTOSLEEP
518
519 /* kernel/power/autosleep.c */
520 void queue_up_suspend_work(void);
521
522 #else /* !CONFIG_PM_AUTOSLEEP */
523
524 static inline void queue_up_suspend_work(void) {}
525
526 #endif /* !CONFIG_PM_AUTOSLEEP */
527
528 #ifdef CONFIG_ARCH_SAVE_PAGE_KEYS
529 /*
530  * The ARCH_SAVE_PAGE_KEYS functions can be used by an architecture
531  * to save/restore additional information to/from the array of page
532  * frame numbers in the hibernation image. For s390 this is used to
533  * save and restore the storage key for each page that is included
534  * in the hibernation image.
535  */
536 unsigned long page_key_additional_pages(unsigned long pages);
537 int page_key_alloc(unsigned long pages);
538 void page_key_free(void);
539 void page_key_read(unsigned long *pfn);
540 void page_key_memorize(unsigned long *pfn);
541 void page_key_write(void *address);
542
543 #else /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
544
545 static inline unsigned long page_key_additional_pages(unsigned long pages)
546 {
547         return 0;
548 }
549
550 static inline int  page_key_alloc(unsigned long pages)
551 {
552         return 0;
553 }
554
555 static inline void page_key_free(void) {}
556 static inline void page_key_read(unsigned long *pfn) {}
557 static inline void page_key_memorize(unsigned long *pfn) {}
558 static inline void page_key_write(void *address) {}
559
560 #endif /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
561
562 #endif /* _LINUX_SUSPEND_H */