Merge tag 'mm-hotfixes-stable-2023-05-03-16-27' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / linux / pm.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  pm.h - Power management interface
4  *
5  *  Copyright (C) 2000 Andrew Henroid
6  */
7
8 #ifndef _LINUX_PM_H
9 #define _LINUX_PM_H
10
11 #include <linux/export.h>
12 #include <linux/list.h>
13 #include <linux/workqueue.h>
14 #include <linux/spinlock.h>
15 #include <linux/wait.h>
16 #include <linux/timer.h>
17 #include <linux/hrtimer.h>
18 #include <linux/completion.h>
19
20 /*
21  * Callbacks for platform drivers to implement.
22  */
23 extern void (*pm_power_off)(void);
24
25 struct device; /* we have a circular dep with device.h */
26 #ifdef CONFIG_VT_CONSOLE_SLEEP
27 extern void pm_vt_switch_required(struct device *dev, bool required);
28 extern void pm_vt_switch_unregister(struct device *dev);
29 #else
30 static inline void pm_vt_switch_required(struct device *dev, bool required)
31 {
32 }
33 static inline void pm_vt_switch_unregister(struct device *dev)
34 {
35 }
36 #endif /* CONFIG_VT_CONSOLE_SLEEP */
37
38 #ifdef CONFIG_CXL_SUSPEND
39 bool cxl_mem_active(void);
40 #else
41 static inline bool cxl_mem_active(void)
42 {
43         return false;
44 }
45 #endif
46
47 /*
48  * Device power management
49  */
50
51
52 #ifdef CONFIG_PM
53 extern const char power_group_name[];           /* = "power" */
54 #else
55 #define power_group_name        NULL
56 #endif
57
58 typedef struct pm_message {
59         int event;
60 } pm_message_t;
61
62 /**
63  * struct dev_pm_ops - device PM callbacks.
64  *
65  * @prepare: The principal role of this callback is to prevent new children of
66  *      the device from being registered after it has returned (the driver's
67  *      subsystem and generally the rest of the kernel is supposed to prevent
68  *      new calls to the probe method from being made too once @prepare() has
69  *      succeeded).  If @prepare() detects a situation it cannot handle (e.g.
70  *      registration of a child already in progress), it may return -EAGAIN, so
71  *      that the PM core can execute it once again (e.g. after a new child has
72  *      been registered) to recover from the race condition.
73  *      This method is executed for all kinds of suspend transitions and is
74  *      followed by one of the suspend callbacks: @suspend(), @freeze(), or
75  *      @poweroff().  If the transition is a suspend to memory or standby (that
76  *      is, not related to hibernation), the return value of @prepare() may be
77  *      used to indicate to the PM core to leave the device in runtime suspend
78  *      if applicable.  Namely, if @prepare() returns a positive number, the PM
79  *      core will understand that as a declaration that the device appears to be
80  *      runtime-suspended and it may be left in that state during the entire
81  *      transition and during the subsequent resume if all of its descendants
82  *      are left in runtime suspend too.  If that happens, @complete() will be
83  *      executed directly after @prepare() and it must ensure the proper
84  *      functioning of the device after the system resume.
85  *      The PM core executes subsystem-level @prepare() for all devices before
86  *      starting to invoke suspend callbacks for any of them, so generally
87  *      devices may be assumed to be functional or to respond to runtime resume
88  *      requests while @prepare() is being executed.  However, device drivers
89  *      may NOT assume anything about the availability of user space at that
90  *      time and it is NOT valid to request firmware from within @prepare()
91  *      (it's too late to do that).  It also is NOT valid to allocate
92  *      substantial amounts of memory from @prepare() in the GFP_KERNEL mode.
93  *      [To work around these limitations, drivers may register suspend and
94  *      hibernation notifiers to be executed before the freezing of tasks.]
95  *
96  * @complete: Undo the changes made by @prepare().  This method is executed for
97  *      all kinds of resume transitions, following one of the resume callbacks:
98  *      @resume(), @thaw(), @restore().  Also called if the state transition
99  *      fails before the driver's suspend callback: @suspend(), @freeze() or
100  *      @poweroff(), can be executed (e.g. if the suspend callback fails for one
101  *      of the other devices that the PM core has unsuccessfully attempted to
102  *      suspend earlier).
103  *      The PM core executes subsystem-level @complete() after it has executed
104  *      the appropriate resume callbacks for all devices.  If the corresponding
105  *      @prepare() at the beginning of the suspend transition returned a
106  *      positive number and the device was left in runtime suspend (without
107  *      executing any suspend and resume callbacks for it), @complete() will be
108  *      the only callback executed for the device during resume.  In that case,
109  *      @complete() must be prepared to do whatever is necessary to ensure the
110  *      proper functioning of the device after the system resume.  To this end,
111  *      @complete() can check the power.direct_complete flag of the device to
112  *      learn whether (unset) or not (set) the previous suspend and resume
113  *      callbacks have been executed for it.
114  *
115  * @suspend: Executed before putting the system into a sleep state in which the
116  *      contents of main memory are preserved.  The exact action to perform
117  *      depends on the device's subsystem (PM domain, device type, class or bus
118  *      type), but generally the device must be quiescent after subsystem-level
119  *      @suspend() has returned, so that it doesn't do any I/O or DMA.
120  *      Subsystem-level @suspend() is executed for all devices after invoking
121  *      subsystem-level @prepare() for all of them.
122  *
123  * @suspend_late: Continue operations started by @suspend().  For a number of
124  *      devices @suspend_late() may point to the same callback routine as the
125  *      runtime suspend callback.
126  *
127  * @resume: Executed after waking the system up from a sleep state in which the
128  *      contents of main memory were preserved.  The exact action to perform
129  *      depends on the device's subsystem, but generally the driver is expected
130  *      to start working again, responding to hardware events and software
131  *      requests (the device itself may be left in a low-power state, waiting
132  *      for a runtime resume to occur).  The state of the device at the time its
133  *      driver's @resume() callback is run depends on the platform and subsystem
134  *      the device belongs to.  On most platforms, there are no restrictions on
135  *      availability of resources like clocks during @resume().
136  *      Subsystem-level @resume() is executed for all devices after invoking
137  *      subsystem-level @resume_noirq() for all of them.
138  *
139  * @resume_early: Prepare to execute @resume().  For a number of devices
140  *      @resume_early() may point to the same callback routine as the runtime
141  *      resume callback.
142  *
143  * @freeze: Hibernation-specific, executed before creating a hibernation image.
144  *      Analogous to @suspend(), but it should not enable the device to signal
145  *      wakeup events or change its power state.  The majority of subsystems
146  *      (with the notable exception of the PCI bus type) expect the driver-level
147  *      @freeze() to save the device settings in memory to be used by @restore()
148  *      during the subsequent resume from hibernation.
149  *      Subsystem-level @freeze() is executed for all devices after invoking
150  *      subsystem-level @prepare() for all of them.
151  *
152  * @freeze_late: Continue operations started by @freeze().  Analogous to
153  *      @suspend_late(), but it should not enable the device to signal wakeup
154  *      events or change its power state.
155  *
156  * @thaw: Hibernation-specific, executed after creating a hibernation image OR
157  *      if the creation of an image has failed.  Also executed after a failing
158  *      attempt to restore the contents of main memory from such an image.
159  *      Undo the changes made by the preceding @freeze(), so the device can be
160  *      operated in the same way as immediately before the call to @freeze().
161  *      Subsystem-level @thaw() is executed for all devices after invoking
162  *      subsystem-level @thaw_noirq() for all of them.  It also may be executed
163  *      directly after @freeze() in case of a transition error.
164  *
165  * @thaw_early: Prepare to execute @thaw().  Undo the changes made by the
166  *      preceding @freeze_late().
167  *
168  * @poweroff: Hibernation-specific, executed after saving a hibernation image.
169  *      Analogous to @suspend(), but it need not save the device's settings in
170  *      memory.
171  *      Subsystem-level @poweroff() is executed for all devices after invoking
172  *      subsystem-level @prepare() for all of them.
173  *
174  * @poweroff_late: Continue operations started by @poweroff().  Analogous to
175  *      @suspend_late(), but it need not save the device's settings in memory.
176  *
177  * @restore: Hibernation-specific, executed after restoring the contents of main
178  *      memory from a hibernation image, analogous to @resume().
179  *
180  * @restore_early: Prepare to execute @restore(), analogous to @resume_early().
181  *
182  * @suspend_noirq: Complete the actions started by @suspend().  Carry out any
183  *      additional operations required for suspending the device that might be
184  *      racing with its driver's interrupt handler, which is guaranteed not to
185  *      run while @suspend_noirq() is being executed.
186  *      It generally is expected that the device will be in a low-power state
187  *      (appropriate for the target system sleep state) after subsystem-level
188  *      @suspend_noirq() has returned successfully.  If the device can generate
189  *      system wakeup signals and is enabled to wake up the system, it should be
190  *      configured to do so at that time.  However, depending on the platform
191  *      and device's subsystem, @suspend() or @suspend_late() may be allowed to
192  *      put the device into the low-power state and configure it to generate
193  *      wakeup signals, in which case it generally is not necessary to define
194  *      @suspend_noirq().
195  *
196  * @resume_noirq: Prepare for the execution of @resume() by carrying out any
197  *      operations required for resuming the device that might be racing with
198  *      its driver's interrupt handler, which is guaranteed not to run while
199  *      @resume_noirq() is being executed.
200  *
201  * @freeze_noirq: Complete the actions started by @freeze().  Carry out any
202  *      additional operations required for freezing the device that might be
203  *      racing with its driver's interrupt handler, which is guaranteed not to
204  *      run while @freeze_noirq() is being executed.
205  *      The power state of the device should not be changed by either @freeze(),
206  *      or @freeze_late(), or @freeze_noirq() and it should not be configured to
207  *      signal system wakeup by any of these callbacks.
208  *
209  * @thaw_noirq: Prepare for the execution of @thaw() by carrying out any
210  *      operations required for thawing the device that might be racing with its
211  *      driver's interrupt handler, which is guaranteed not to run while
212  *      @thaw_noirq() is being executed.
213  *
214  * @poweroff_noirq: Complete the actions started by @poweroff().  Analogous to
215  *      @suspend_noirq(), but it need not save the device's settings in memory.
216  *
217  * @restore_noirq: Prepare for the execution of @restore() by carrying out any
218  *      operations required for thawing the device that might be racing with its
219  *      driver's interrupt handler, which is guaranteed not to run while
220  *      @restore_noirq() is being executed.  Analogous to @resume_noirq().
221  *
222  * @runtime_suspend: Prepare the device for a condition in which it won't be
223  *      able to communicate with the CPU(s) and RAM due to power management.
224  *      This need not mean that the device should be put into a low-power state.
225  *      For example, if the device is behind a link which is about to be turned
226  *      off, the device may remain at full power.  If the device does go to low
227  *      power and is capable of generating runtime wakeup events, remote wakeup
228  *      (i.e., a hardware mechanism allowing the device to request a change of
229  *      its power state via an interrupt) should be enabled for it.
230  *
231  * @runtime_resume: Put the device into the fully active state in response to a
232  *      wakeup event generated by hardware or at the request of software.  If
233  *      necessary, put the device into the full-power state and restore its
234  *      registers, so that it is fully operational.
235  *
236  * @runtime_idle: Device appears to be inactive and it might be put into a
237  *      low-power state if all of the necessary conditions are satisfied.
238  *      Check these conditions, and return 0 if it's appropriate to let the PM
239  *      core queue a suspend request for the device.
240  *
241  * Several device power state transitions are externally visible, affecting
242  * the state of pending I/O queues and (for drivers that touch hardware)
243  * interrupts, wakeups, DMA, and other hardware state.  There may also be
244  * internal transitions to various low-power modes which are transparent
245  * to the rest of the driver stack (such as a driver that's ON gating off
246  * clocks which are not in active use).
247  *
248  * The externally visible transitions are handled with the help of callbacks
249  * included in this structure in such a way that, typically, two levels of
250  * callbacks are involved.  First, the PM core executes callbacks provided by PM
251  * domains, device types, classes and bus types.  They are the subsystem-level
252  * callbacks expected to execute callbacks provided by device drivers, although
253  * they may choose not to do that.  If the driver callbacks are executed, they
254  * have to collaborate with the subsystem-level callbacks to achieve the goals
255  * appropriate for the given system transition, given transition phase and the
256  * subsystem the device belongs to.
257  *
258  * All of the above callbacks, except for @complete(), return error codes.
259  * However, the error codes returned by @resume(), @thaw(), @restore(),
260  * @resume_noirq(), @thaw_noirq(), and @restore_noirq(), do not cause the PM
261  * core to abort the resume transition during which they are returned.  The
262  * error codes returned in those cases are only printed to the system logs for
263  * debugging purposes.  Still, it is recommended that drivers only return error
264  * codes from their resume methods in case of an unrecoverable failure (i.e.
265  * when the device being handled refuses to resume and becomes unusable) to
266  * allow the PM core to be modified in the future, so that it can avoid
267  * attempting to handle devices that failed to resume and their children.
268  *
269  * It is allowed to unregister devices while the above callbacks are being
270  * executed.  However, a callback routine MUST NOT try to unregister the device
271  * it was called for, although it may unregister children of that device (for
272  * example, if it detects that a child was unplugged while the system was
273  * asleep).
274  *
275  * There also are callbacks related to runtime power management of devices.
276  * Again, as a rule these callbacks are executed by the PM core for subsystems
277  * (PM domains, device types, classes and bus types) and the subsystem-level
278  * callbacks are expected to invoke the driver callbacks.  Moreover, the exact
279  * actions to be performed by a device driver's callbacks generally depend on
280  * the platform and subsystem the device belongs to.
281  *
282  * Refer to Documentation/power/runtime_pm.rst for more information about the
283  * role of the @runtime_suspend(), @runtime_resume() and @runtime_idle()
284  * callbacks in device runtime power management.
285  */
286 struct dev_pm_ops {
287         int (*prepare)(struct device *dev);
288         void (*complete)(struct device *dev);
289         int (*suspend)(struct device *dev);
290         int (*resume)(struct device *dev);
291         int (*freeze)(struct device *dev);
292         int (*thaw)(struct device *dev);
293         int (*poweroff)(struct device *dev);
294         int (*restore)(struct device *dev);
295         int (*suspend_late)(struct device *dev);
296         int (*resume_early)(struct device *dev);
297         int (*freeze_late)(struct device *dev);
298         int (*thaw_early)(struct device *dev);
299         int (*poweroff_late)(struct device *dev);
300         int (*restore_early)(struct device *dev);
301         int (*suspend_noirq)(struct device *dev);
302         int (*resume_noirq)(struct device *dev);
303         int (*freeze_noirq)(struct device *dev);
304         int (*thaw_noirq)(struct device *dev);
305         int (*poweroff_noirq)(struct device *dev);
306         int (*restore_noirq)(struct device *dev);
307         int (*runtime_suspend)(struct device *dev);
308         int (*runtime_resume)(struct device *dev);
309         int (*runtime_idle)(struct device *dev);
310 };
311
312 #define SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
313         .suspend = pm_sleep_ptr(suspend_fn), \
314         .resume = pm_sleep_ptr(resume_fn), \
315         .freeze = pm_sleep_ptr(suspend_fn), \
316         .thaw = pm_sleep_ptr(resume_fn), \
317         .poweroff = pm_sleep_ptr(suspend_fn), \
318         .restore = pm_sleep_ptr(resume_fn),
319
320 #define LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
321         .suspend_late = pm_sleep_ptr(suspend_fn), \
322         .resume_early = pm_sleep_ptr(resume_fn), \
323         .freeze_late = pm_sleep_ptr(suspend_fn), \
324         .thaw_early = pm_sleep_ptr(resume_fn), \
325         .poweroff_late = pm_sleep_ptr(suspend_fn), \
326         .restore_early = pm_sleep_ptr(resume_fn),
327
328 #define NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
329         .suspend_noirq = pm_sleep_ptr(suspend_fn), \
330         .resume_noirq = pm_sleep_ptr(resume_fn), \
331         .freeze_noirq = pm_sleep_ptr(suspend_fn), \
332         .thaw_noirq = pm_sleep_ptr(resume_fn), \
333         .poweroff_noirq = pm_sleep_ptr(suspend_fn), \
334         .restore_noirq = pm_sleep_ptr(resume_fn),
335
336 #define RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
337         .runtime_suspend = suspend_fn, \
338         .runtime_resume = resume_fn, \
339         .runtime_idle = idle_fn,
340
341 #ifdef CONFIG_PM_SLEEP
342 #define SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
343         SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
344 #else
345 #define SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
346 #endif
347
348 #ifdef CONFIG_PM_SLEEP
349 #define SET_LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
350         LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
351 #else
352 #define SET_LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
353 #endif
354
355 #ifdef CONFIG_PM_SLEEP
356 #define SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
357         NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
358 #else
359 #define SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
360 #endif
361
362 #ifdef CONFIG_PM
363 #define SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
364         RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn)
365 #else
366 #define SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn)
367 #endif
368
369 #define _DEFINE_DEV_PM_OPS(name, \
370                            suspend_fn, resume_fn, \
371                            runtime_suspend_fn, runtime_resume_fn, idle_fn) \
372 const struct dev_pm_ops name = { \
373         SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
374         RUNTIME_PM_OPS(runtime_suspend_fn, runtime_resume_fn, idle_fn) \
375 }
376
377 #ifdef CONFIG_PM
378 #define _EXPORT_DEV_PM_OPS(name, sec, ns)                               \
379         const struct dev_pm_ops name;                                   \
380         __EXPORT_SYMBOL(name, sec, ns);                                 \
381         const struct dev_pm_ops name
382 #define EXPORT_PM_FN_GPL(name)          EXPORT_SYMBOL_GPL(name)
383 #define EXPORT_PM_FN_NS_GPL(name, ns)   EXPORT_SYMBOL_NS_GPL(name, ns)
384 #else
385 #define _EXPORT_DEV_PM_OPS(name, sec, ns)                               \
386         static __maybe_unused const struct dev_pm_ops __static_##name
387 #define EXPORT_PM_FN_GPL(name)
388 #define EXPORT_PM_FN_NS_GPL(name, ns)
389 #endif
390
391 #define EXPORT_DEV_PM_OPS(name) _EXPORT_DEV_PM_OPS(name, "", "")
392 #define EXPORT_GPL_DEV_PM_OPS(name) _EXPORT_DEV_PM_OPS(name, "_gpl", "")
393 #define EXPORT_NS_DEV_PM_OPS(name, ns) _EXPORT_DEV_PM_OPS(name, "", #ns)
394 #define EXPORT_NS_GPL_DEV_PM_OPS(name, ns) _EXPORT_DEV_PM_OPS(name, "_gpl", #ns)
395
396 /*
397  * Use this if you want to use the same suspend and resume callbacks for suspend
398  * to RAM and hibernation.
399  *
400  * If the underlying dev_pm_ops struct symbol has to be exported, use
401  * EXPORT_SIMPLE_DEV_PM_OPS() or EXPORT_GPL_SIMPLE_DEV_PM_OPS() instead.
402  */
403 #define DEFINE_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
404         _DEFINE_DEV_PM_OPS(name, suspend_fn, resume_fn, NULL, NULL, NULL)
405
406 #define EXPORT_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
407         EXPORT_DEV_PM_OPS(name) = { \
408                 SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
409         }
410 #define EXPORT_GPL_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
411         EXPORT_GPL_DEV_PM_OPS(name) = { \
412                 SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
413         }
414 #define EXPORT_NS_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn, ns)    \
415         EXPORT_NS_DEV_PM_OPS(name, ns) = { \
416                 SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
417         }
418 #define EXPORT_NS_GPL_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn, ns)        \
419         EXPORT_NS_GPL_DEV_PM_OPS(name, ns) = { \
420                 SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
421         }
422
423 /* Deprecated. Use DEFINE_SIMPLE_DEV_PM_OPS() instead. */
424 #define SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
425 const struct dev_pm_ops __maybe_unused name = { \
426         SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
427 }
428
429 /*
430  * Use this for defining a set of PM operations to be used in all situations
431  * (system suspend, hibernation or runtime PM).
432  * NOTE: In general, system suspend callbacks, .suspend() and .resume(), should
433  * be different from the corresponding runtime PM callbacks, .runtime_suspend(),
434  * and .runtime_resume(), because .runtime_suspend() always works on an already
435  * quiescent device, while .suspend() should assume that the device may be doing
436  * something when it is called (it should ensure that the device will be
437  * quiescent after it has returned).  Therefore it's better to point the "late"
438  * suspend and "early" resume callback pointers, .suspend_late() and
439  * .resume_early(), to the same routines as .runtime_suspend() and
440  * .runtime_resume(), respectively (and analogously for hibernation).
441  *
442  * Deprecated. You most likely don't want this macro. Use
443  * DEFINE_RUNTIME_DEV_PM_OPS() instead.
444  */
445 #define UNIVERSAL_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \
446 const struct dev_pm_ops __maybe_unused name = { \
447         SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
448         SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
449 }
450
451 #define pm_ptr(_ptr) PTR_IF(IS_ENABLED(CONFIG_PM), (_ptr))
452 #define pm_sleep_ptr(_ptr) PTR_IF(IS_ENABLED(CONFIG_PM_SLEEP), (_ptr))
453
454 /*
455  * PM_EVENT_ messages
456  *
457  * The following PM_EVENT_ messages are defined for the internal use of the PM
458  * core, in order to provide a mechanism allowing the high level suspend and
459  * hibernation code to convey the necessary information to the device PM core
460  * code:
461  *
462  * ON           No transition.
463  *
464  * FREEZE       System is going to hibernate, call ->prepare() and ->freeze()
465  *              for all devices.
466  *
467  * SUSPEND      System is going to suspend, call ->prepare() and ->suspend()
468  *              for all devices.
469  *
470  * HIBERNATE    Hibernation image has been saved, call ->prepare() and
471  *              ->poweroff() for all devices.
472  *
473  * QUIESCE      Contents of main memory are going to be restored from a (loaded)
474  *              hibernation image, call ->prepare() and ->freeze() for all
475  *              devices.
476  *
477  * RESUME       System is resuming, call ->resume() and ->complete() for all
478  *              devices.
479  *
480  * THAW         Hibernation image has been created, call ->thaw() and
481  *              ->complete() for all devices.
482  *
483  * RESTORE      Contents of main memory have been restored from a hibernation
484  *              image, call ->restore() and ->complete() for all devices.
485  *
486  * RECOVER      Creation of a hibernation image or restoration of the main
487  *              memory contents from a hibernation image has failed, call
488  *              ->thaw() and ->complete() for all devices.
489  *
490  * The following PM_EVENT_ messages are defined for internal use by
491  * kernel subsystems.  They are never issued by the PM core.
492  *
493  * USER_SUSPEND         Manual selective suspend was issued by userspace.
494  *
495  * USER_RESUME          Manual selective resume was issued by userspace.
496  *
497  * REMOTE_WAKEUP        Remote-wakeup request was received from the device.
498  *
499  * AUTO_SUSPEND         Automatic (device idle) runtime suspend was
500  *                      initiated by the subsystem.
501  *
502  * AUTO_RESUME          Automatic (device needed) runtime resume was
503  *                      requested by a driver.
504  */
505
506 #define PM_EVENT_INVALID        (-1)
507 #define PM_EVENT_ON             0x0000
508 #define PM_EVENT_FREEZE         0x0001
509 #define PM_EVENT_SUSPEND        0x0002
510 #define PM_EVENT_HIBERNATE      0x0004
511 #define PM_EVENT_QUIESCE        0x0008
512 #define PM_EVENT_RESUME         0x0010
513 #define PM_EVENT_THAW           0x0020
514 #define PM_EVENT_RESTORE        0x0040
515 #define PM_EVENT_RECOVER        0x0080
516 #define PM_EVENT_USER           0x0100
517 #define PM_EVENT_REMOTE         0x0200
518 #define PM_EVENT_AUTO           0x0400
519
520 #define PM_EVENT_SLEEP          (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE)
521 #define PM_EVENT_USER_SUSPEND   (PM_EVENT_USER | PM_EVENT_SUSPEND)
522 #define PM_EVENT_USER_RESUME    (PM_EVENT_USER | PM_EVENT_RESUME)
523 #define PM_EVENT_REMOTE_RESUME  (PM_EVENT_REMOTE | PM_EVENT_RESUME)
524 #define PM_EVENT_AUTO_SUSPEND   (PM_EVENT_AUTO | PM_EVENT_SUSPEND)
525 #define PM_EVENT_AUTO_RESUME    (PM_EVENT_AUTO | PM_EVENT_RESUME)
526
527 #define PMSG_INVALID    ((struct pm_message){ .event = PM_EVENT_INVALID, })
528 #define PMSG_ON         ((struct pm_message){ .event = PM_EVENT_ON, })
529 #define PMSG_FREEZE     ((struct pm_message){ .event = PM_EVENT_FREEZE, })
530 #define PMSG_QUIESCE    ((struct pm_message){ .event = PM_EVENT_QUIESCE, })
531 #define PMSG_SUSPEND    ((struct pm_message){ .event = PM_EVENT_SUSPEND, })
532 #define PMSG_HIBERNATE  ((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
533 #define PMSG_RESUME     ((struct pm_message){ .event = PM_EVENT_RESUME, })
534 #define PMSG_THAW       ((struct pm_message){ .event = PM_EVENT_THAW, })
535 #define PMSG_RESTORE    ((struct pm_message){ .event = PM_EVENT_RESTORE, })
536 #define PMSG_RECOVER    ((struct pm_message){ .event = PM_EVENT_RECOVER, })
537 #define PMSG_USER_SUSPEND       ((struct pm_message) \
538                                         { .event = PM_EVENT_USER_SUSPEND, })
539 #define PMSG_USER_RESUME        ((struct pm_message) \
540                                         { .event = PM_EVENT_USER_RESUME, })
541 #define PMSG_REMOTE_RESUME      ((struct pm_message) \
542                                         { .event = PM_EVENT_REMOTE_RESUME, })
543 #define PMSG_AUTO_SUSPEND       ((struct pm_message) \
544                                         { .event = PM_EVENT_AUTO_SUSPEND, })
545 #define PMSG_AUTO_RESUME        ((struct pm_message) \
546                                         { .event = PM_EVENT_AUTO_RESUME, })
547
548 #define PMSG_IS_AUTO(msg)       (((msg).event & PM_EVENT_AUTO) != 0)
549
550 /*
551  * Device run-time power management status.
552  *
553  * These status labels are used internally by the PM core to indicate the
554  * current status of a device with respect to the PM core operations.  They do
555  * not reflect the actual power state of the device or its status as seen by the
556  * driver.
557  *
558  * RPM_ACTIVE           Device is fully operational.  Indicates that the device
559  *                      bus type's ->runtime_resume() callback has completed
560  *                      successfully.
561  *
562  * RPM_SUSPENDED        Device bus type's ->runtime_suspend() callback has
563  *                      completed successfully.  The device is regarded as
564  *                      suspended.
565  *
566  * RPM_RESUMING         Device bus type's ->runtime_resume() callback is being
567  *                      executed.
568  *
569  * RPM_SUSPENDING       Device bus type's ->runtime_suspend() callback is being
570  *                      executed.
571  */
572
573 enum rpm_status {
574         RPM_INVALID = -1,
575         RPM_ACTIVE = 0,
576         RPM_RESUMING,
577         RPM_SUSPENDED,
578         RPM_SUSPENDING,
579 };
580
581 /*
582  * Device run-time power management request types.
583  *
584  * RPM_REQ_NONE         Do nothing.
585  *
586  * RPM_REQ_IDLE         Run the device bus type's ->runtime_idle() callback
587  *
588  * RPM_REQ_SUSPEND      Run the device bus type's ->runtime_suspend() callback
589  *
590  * RPM_REQ_AUTOSUSPEND  Same as RPM_REQ_SUSPEND, but not until the device has
591  *                      been inactive for as long as power.autosuspend_delay
592  *
593  * RPM_REQ_RESUME       Run the device bus type's ->runtime_resume() callback
594  */
595
596 enum rpm_request {
597         RPM_REQ_NONE = 0,
598         RPM_REQ_IDLE,
599         RPM_REQ_SUSPEND,
600         RPM_REQ_AUTOSUSPEND,
601         RPM_REQ_RESUME,
602 };
603
604 struct wakeup_source;
605 struct wake_irq;
606 struct pm_domain_data;
607
608 struct pm_subsys_data {
609         spinlock_t lock;
610         unsigned int refcount;
611 #ifdef CONFIG_PM_CLK
612         unsigned int clock_op_might_sleep;
613         struct mutex clock_mutex;
614         struct list_head clock_list;
615 #endif
616 #ifdef CONFIG_PM_GENERIC_DOMAINS
617         struct pm_domain_data *domain_data;
618 #endif
619 };
620
621 /*
622  * Driver flags to control system suspend/resume behavior.
623  *
624  * These flags can be set by device drivers at the probe time.  They need not be
625  * cleared by the drivers as the driver core will take care of that.
626  *
627  * NO_DIRECT_COMPLETE: Do not apply direct-complete optimization to the device.
628  * SMART_PREPARE: Take the driver ->prepare callback return value into account.
629  * SMART_SUSPEND: Avoid resuming the device from runtime suspend.
630  * MAY_SKIP_RESUME: Allow driver "noirq" and "early" callbacks to be skipped.
631  *
632  * See Documentation/driver-api/pm/devices.rst for details.
633  */
634 #define DPM_FLAG_NO_DIRECT_COMPLETE     BIT(0)
635 #define DPM_FLAG_SMART_PREPARE          BIT(1)
636 #define DPM_FLAG_SMART_SUSPEND          BIT(2)
637 #define DPM_FLAG_MAY_SKIP_RESUME        BIT(3)
638
639 struct dev_pm_info {
640         pm_message_t            power_state;
641         unsigned int            can_wakeup:1;
642         unsigned int            async_suspend:1;
643         bool                    in_dpm_list:1;  /* Owned by the PM core */
644         bool                    is_prepared:1;  /* Owned by the PM core */
645         bool                    is_suspended:1; /* Ditto */
646         bool                    is_noirq_suspended:1;
647         bool                    is_late_suspended:1;
648         bool                    no_pm:1;
649         bool                    early_init:1;   /* Owned by the PM core */
650         bool                    direct_complete:1;      /* Owned by the PM core */
651         u32                     driver_flags;
652         spinlock_t              lock;
653 #ifdef CONFIG_PM_SLEEP
654         struct list_head        entry;
655         struct completion       completion;
656         struct wakeup_source    *wakeup;
657         bool                    wakeup_path:1;
658         bool                    syscore:1;
659         bool                    no_pm_callbacks:1;      /* Owned by the PM core */
660         unsigned int            must_resume:1;  /* Owned by the PM core */
661         unsigned int            may_skip_resume:1;      /* Set by subsystems */
662 #else
663         unsigned int            should_wakeup:1;
664 #endif
665 #ifdef CONFIG_PM
666         struct hrtimer          suspend_timer;
667         u64                     timer_expires;
668         struct work_struct      work;
669         wait_queue_head_t       wait_queue;
670         struct wake_irq         *wakeirq;
671         atomic_t                usage_count;
672         atomic_t                child_count;
673         unsigned int            disable_depth:3;
674         unsigned int            idle_notification:1;
675         unsigned int            request_pending:1;
676         unsigned int            deferred_resume:1;
677         unsigned int            needs_force_resume:1;
678         unsigned int            runtime_auto:1;
679         bool                    ignore_children:1;
680         unsigned int            no_callbacks:1;
681         unsigned int            irq_safe:1;
682         unsigned int            use_autosuspend:1;
683         unsigned int            timer_autosuspends:1;
684         unsigned int            memalloc_noio:1;
685         unsigned int            links_count;
686         enum rpm_request        request;
687         enum rpm_status         runtime_status;
688         enum rpm_status         last_status;
689         int                     runtime_error;
690         int                     autosuspend_delay;
691         u64                     last_busy;
692         u64                     active_time;
693         u64                     suspended_time;
694         u64                     accounting_timestamp;
695 #endif
696         struct pm_subsys_data   *subsys_data;  /* Owned by the subsystem. */
697         void (*set_latency_tolerance)(struct device *, s32);
698         struct dev_pm_qos       *qos;
699 };
700
701 extern int dev_pm_get_subsys_data(struct device *dev);
702 extern void dev_pm_put_subsys_data(struct device *dev);
703
704 /**
705  * struct dev_pm_domain - power management domain representation.
706  *
707  * @ops: Power management operations associated with this domain.
708  * @start: Called when a user needs to start the device via the domain.
709  * @detach: Called when removing a device from the domain.
710  * @activate: Called before executing probe routines for bus types and drivers.
711  * @sync: Called after successful driver probe.
712  * @dismiss: Called after unsuccessful driver probe and after driver removal.
713  *
714  * Power domains provide callbacks that are executed during system suspend,
715  * hibernation, system resume and during runtime PM transitions instead of
716  * subsystem-level and driver-level callbacks.
717  */
718 struct dev_pm_domain {
719         struct dev_pm_ops       ops;
720         int (*start)(struct device *dev);
721         void (*detach)(struct device *dev, bool power_off);
722         int (*activate)(struct device *dev);
723         void (*sync)(struct device *dev);
724         void (*dismiss)(struct device *dev);
725 };
726
727 /*
728  * The PM_EVENT_ messages are also used by drivers implementing the legacy
729  * suspend framework, based on the ->suspend() and ->resume() callbacks common
730  * for suspend and hibernation transitions, according to the rules below.
731  */
732
733 /* Necessary, because several drivers use PM_EVENT_PRETHAW */
734 #define PM_EVENT_PRETHAW PM_EVENT_QUIESCE
735
736 /*
737  * One transition is triggered by resume(), after a suspend() call; the
738  * message is implicit:
739  *
740  * ON           Driver starts working again, responding to hardware events
741  *              and software requests.  The hardware may have gone through
742  *              a power-off reset, or it may have maintained state from the
743  *              previous suspend() which the driver will rely on while
744  *              resuming.  On most platforms, there are no restrictions on
745  *              availability of resources like clocks during resume().
746  *
747  * Other transitions are triggered by messages sent using suspend().  All
748  * these transitions quiesce the driver, so that I/O queues are inactive.
749  * That commonly entails turning off IRQs and DMA; there may be rules
750  * about how to quiesce that are specific to the bus or the device's type.
751  * (For example, network drivers mark the link state.)  Other details may
752  * differ according to the message:
753  *
754  * SUSPEND      Quiesce, enter a low power device state appropriate for
755  *              the upcoming system state (such as PCI_D3hot), and enable
756  *              wakeup events as appropriate.
757  *
758  * HIBERNATE    Enter a low power device state appropriate for the hibernation
759  *              state (eg. ACPI S4) and enable wakeup events as appropriate.
760  *
761  * FREEZE       Quiesce operations so that a consistent image can be saved;
762  *              but do NOT otherwise enter a low power device state, and do
763  *              NOT emit system wakeup events.
764  *
765  * PRETHAW      Quiesce as if for FREEZE; additionally, prepare for restoring
766  *              the system from a snapshot taken after an earlier FREEZE.
767  *              Some drivers will need to reset their hardware state instead
768  *              of preserving it, to ensure that it's never mistaken for the
769  *              state which that earlier snapshot had set up.
770  *
771  * A minimally power-aware driver treats all messages as SUSPEND, fully
772  * reinitializes its device during resume() -- whether or not it was reset
773  * during the suspend/resume cycle -- and can't issue wakeup events.
774  *
775  * More power-aware drivers may also use low power states at runtime as
776  * well as during system sleep states like PM_SUSPEND_STANDBY.  They may
777  * be able to use wakeup events to exit from runtime low-power states,
778  * or from system low-power states such as standby or suspend-to-RAM.
779  */
780
781 #ifdef CONFIG_PM_SLEEP
782 extern void device_pm_lock(void);
783 extern void dpm_resume_start(pm_message_t state);
784 extern void dpm_resume_end(pm_message_t state);
785 extern void dpm_resume_noirq(pm_message_t state);
786 extern void dpm_resume_early(pm_message_t state);
787 extern void dpm_resume(pm_message_t state);
788 extern void dpm_complete(pm_message_t state);
789
790 extern void device_pm_unlock(void);
791 extern int dpm_suspend_end(pm_message_t state);
792 extern int dpm_suspend_start(pm_message_t state);
793 extern int dpm_suspend_noirq(pm_message_t state);
794 extern int dpm_suspend_late(pm_message_t state);
795 extern int dpm_suspend(pm_message_t state);
796 extern int dpm_prepare(pm_message_t state);
797
798 extern void __suspend_report_result(const char *function, struct device *dev, void *fn, int ret);
799
800 #define suspend_report_result(dev, fn, ret)                             \
801         do {                                                            \
802                 __suspend_report_result(__func__, dev, fn, ret);        \
803         } while (0)
804
805 extern int device_pm_wait_for_dev(struct device *sub, struct device *dev);
806 extern void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *));
807
808 extern int pm_generic_prepare(struct device *dev);
809 extern int pm_generic_suspend_late(struct device *dev);
810 extern int pm_generic_suspend_noirq(struct device *dev);
811 extern int pm_generic_suspend(struct device *dev);
812 extern int pm_generic_resume_early(struct device *dev);
813 extern int pm_generic_resume_noirq(struct device *dev);
814 extern int pm_generic_resume(struct device *dev);
815 extern int pm_generic_freeze_noirq(struct device *dev);
816 extern int pm_generic_freeze_late(struct device *dev);
817 extern int pm_generic_freeze(struct device *dev);
818 extern int pm_generic_thaw_noirq(struct device *dev);
819 extern int pm_generic_thaw_early(struct device *dev);
820 extern int pm_generic_thaw(struct device *dev);
821 extern int pm_generic_restore_noirq(struct device *dev);
822 extern int pm_generic_restore_early(struct device *dev);
823 extern int pm_generic_restore(struct device *dev);
824 extern int pm_generic_poweroff_noirq(struct device *dev);
825 extern int pm_generic_poweroff_late(struct device *dev);
826 extern int pm_generic_poweroff(struct device *dev);
827 extern void pm_generic_complete(struct device *dev);
828
829 extern bool dev_pm_skip_resume(struct device *dev);
830 extern bool dev_pm_skip_suspend(struct device *dev);
831
832 #else /* !CONFIG_PM_SLEEP */
833
834 #define device_pm_lock() do {} while (0)
835 #define device_pm_unlock() do {} while (0)
836
837 static inline int dpm_suspend_start(pm_message_t state)
838 {
839         return 0;
840 }
841
842 #define suspend_report_result(dev, fn, ret)     do {} while (0)
843
844 static inline int device_pm_wait_for_dev(struct device *a, struct device *b)
845 {
846         return 0;
847 }
848
849 static inline void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
850 {
851 }
852
853 #define pm_generic_prepare              NULL
854 #define pm_generic_suspend_late         NULL
855 #define pm_generic_suspend_noirq        NULL
856 #define pm_generic_suspend              NULL
857 #define pm_generic_resume_early         NULL
858 #define pm_generic_resume_noirq         NULL
859 #define pm_generic_resume               NULL
860 #define pm_generic_freeze_noirq         NULL
861 #define pm_generic_freeze_late          NULL
862 #define pm_generic_freeze               NULL
863 #define pm_generic_thaw_noirq           NULL
864 #define pm_generic_thaw_early           NULL
865 #define pm_generic_thaw                 NULL
866 #define pm_generic_restore_noirq        NULL
867 #define pm_generic_restore_early        NULL
868 #define pm_generic_restore              NULL
869 #define pm_generic_poweroff_noirq       NULL
870 #define pm_generic_poweroff_late        NULL
871 #define pm_generic_poweroff             NULL
872 #define pm_generic_complete             NULL
873 #endif /* !CONFIG_PM_SLEEP */
874
875 /* How to reorder dpm_list after device_move() */
876 enum dpm_order {
877         DPM_ORDER_NONE,
878         DPM_ORDER_DEV_AFTER_PARENT,
879         DPM_ORDER_PARENT_BEFORE_DEV,
880         DPM_ORDER_DEV_LAST,
881 };
882
883 #endif /* _LINUX_PM_H */