Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *  pm.h - Power management interface
0004  *
0005  *  Copyright (C) 2000 Andrew Henroid
0006  */
0007 
0008 #ifndef _LINUX_PM_H
0009 #define _LINUX_PM_H
0010 
0011 #include <linux/export.h>
0012 #include <linux/list.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/spinlock.h>
0015 #include <linux/wait.h>
0016 #include <linux/timer.h>
0017 #include <linux/hrtimer.h>
0018 #include <linux/completion.h>
0019 
0020 /*
0021  * Callbacks for platform drivers to implement.
0022  */
0023 extern void (*pm_power_off)(void);
0024 
0025 struct device; /* we have a circular dep with device.h */
0026 #ifdef CONFIG_VT_CONSOLE_SLEEP
0027 extern void pm_vt_switch_required(struct device *dev, bool required);
0028 extern void pm_vt_switch_unregister(struct device *dev);
0029 #else
0030 static inline void pm_vt_switch_required(struct device *dev, bool required)
0031 {
0032 }
0033 static inline void pm_vt_switch_unregister(struct device *dev)
0034 {
0035 }
0036 #endif /* CONFIG_VT_CONSOLE_SLEEP */
0037 
0038 #ifdef CONFIG_CXL_SUSPEND
0039 bool cxl_mem_active(void);
0040 #else
0041 static inline bool cxl_mem_active(void)
0042 {
0043     return false;
0044 }
0045 #endif
0046 
0047 /*
0048  * Device power management
0049  */
0050 
0051 
0052 #ifdef CONFIG_PM
0053 extern const char power_group_name[];       /* = "power" */
0054 #else
0055 #define power_group_name    NULL
0056 #endif
0057 
0058 typedef struct pm_message {
0059     int event;
0060 } pm_message_t;
0061 
0062 /**
0063  * struct dev_pm_ops - device PM callbacks.
0064  *
0065  * @prepare: The principal role of this callback is to prevent new children of
0066  *  the device from being registered after it has returned (the driver's
0067  *  subsystem and generally the rest of the kernel is supposed to prevent
0068  *  new calls to the probe method from being made too once @prepare() has
0069  *  succeeded).  If @prepare() detects a situation it cannot handle (e.g.
0070  *  registration of a child already in progress), it may return -EAGAIN, so
0071  *  that the PM core can execute it once again (e.g. after a new child has
0072  *  been registered) to recover from the race condition.
0073  *  This method is executed for all kinds of suspend transitions and is
0074  *  followed by one of the suspend callbacks: @suspend(), @freeze(), or
0075  *  @poweroff().  If the transition is a suspend to memory or standby (that
0076  *  is, not related to hibernation), the return value of @prepare() may be
0077  *  used to indicate to the PM core to leave the device in runtime suspend
0078  *  if applicable.  Namely, if @prepare() returns a positive number, the PM
0079  *  core will understand that as a declaration that the device appears to be
0080  *  runtime-suspended and it may be left in that state during the entire
0081  *  transition and during the subsequent resume if all of its descendants
0082  *  are left in runtime suspend too.  If that happens, @complete() will be
0083  *  executed directly after @prepare() and it must ensure the proper
0084  *  functioning of the device after the system resume.
0085  *  The PM core executes subsystem-level @prepare() for all devices before
0086  *  starting to invoke suspend callbacks for any of them, so generally
0087  *  devices may be assumed to be functional or to respond to runtime resume
0088  *  requests while @prepare() is being executed.  However, device drivers
0089  *  may NOT assume anything about the availability of user space at that
0090  *  time and it is NOT valid to request firmware from within @prepare()
0091  *  (it's too late to do that).  It also is NOT valid to allocate
0092  *  substantial amounts of memory from @prepare() in the GFP_KERNEL mode.
0093  *  [To work around these limitations, drivers may register suspend and
0094  *  hibernation notifiers to be executed before the freezing of tasks.]
0095  *
0096  * @complete: Undo the changes made by @prepare().  This method is executed for
0097  *  all kinds of resume transitions, following one of the resume callbacks:
0098  *  @resume(), @thaw(), @restore().  Also called if the state transition
0099  *  fails before the driver's suspend callback: @suspend(), @freeze() or
0100  *  @poweroff(), can be executed (e.g. if the suspend callback fails for one
0101  *  of the other devices that the PM core has unsuccessfully attempted to
0102  *  suspend earlier).
0103  *  The PM core executes subsystem-level @complete() after it has executed
0104  *  the appropriate resume callbacks for all devices.  If the corresponding
0105  *  @prepare() at the beginning of the suspend transition returned a
0106  *  positive number and the device was left in runtime suspend (without
0107  *  executing any suspend and resume callbacks for it), @complete() will be
0108  *  the only callback executed for the device during resume.  In that case,
0109  *  @complete() must be prepared to do whatever is necessary to ensure the
0110  *  proper functioning of the device after the system resume.  To this end,
0111  *  @complete() can check the power.direct_complete flag of the device to
0112  *  learn whether (unset) or not (set) the previous suspend and resume
0113  *  callbacks have been executed for it.
0114  *
0115  * @suspend: Executed before putting the system into a sleep state in which the
0116  *  contents of main memory are preserved.  The exact action to perform
0117  *  depends on the device's subsystem (PM domain, device type, class or bus
0118  *  type), but generally the device must be quiescent after subsystem-level
0119  *  @suspend() has returned, so that it doesn't do any I/O or DMA.
0120  *  Subsystem-level @suspend() is executed for all devices after invoking
0121  *  subsystem-level @prepare() for all of them.
0122  *
0123  * @suspend_late: Continue operations started by @suspend().  For a number of
0124  *  devices @suspend_late() may point to the same callback routine as the
0125  *  runtime suspend callback.
0126  *
0127  * @resume: Executed after waking the system up from a sleep state in which the
0128  *  contents of main memory were preserved.  The exact action to perform
0129  *  depends on the device's subsystem, but generally the driver is expected
0130  *  to start working again, responding to hardware events and software
0131  *  requests (the device itself may be left in a low-power state, waiting
0132  *  for a runtime resume to occur).  The state of the device at the time its
0133  *  driver's @resume() callback is run depends on the platform and subsystem
0134  *  the device belongs to.  On most platforms, there are no restrictions on
0135  *  availability of resources like clocks during @resume().
0136  *  Subsystem-level @resume() is executed for all devices after invoking
0137  *  subsystem-level @resume_noirq() for all of them.
0138  *
0139  * @resume_early: Prepare to execute @resume().  For a number of devices
0140  *  @resume_early() may point to the same callback routine as the runtime
0141  *  resume callback.
0142  *
0143  * @freeze: Hibernation-specific, executed before creating a hibernation image.
0144  *  Analogous to @suspend(), but it should not enable the device to signal
0145  *  wakeup events or change its power state.  The majority of subsystems
0146  *  (with the notable exception of the PCI bus type) expect the driver-level
0147  *  @freeze() to save the device settings in memory to be used by @restore()
0148  *  during the subsequent resume from hibernation.
0149  *  Subsystem-level @freeze() is executed for all devices after invoking
0150  *  subsystem-level @prepare() for all of them.
0151  *
0152  * @freeze_late: Continue operations started by @freeze().  Analogous to
0153  *  @suspend_late(), but it should not enable the device to signal wakeup
0154  *  events or change its power state.
0155  *
0156  * @thaw: Hibernation-specific, executed after creating a hibernation image OR
0157  *  if the creation of an image has failed.  Also executed after a failing
0158  *  attempt to restore the contents of main memory from such an image.
0159  *  Undo the changes made by the preceding @freeze(), so the device can be
0160  *  operated in the same way as immediately before the call to @freeze().
0161  *  Subsystem-level @thaw() is executed for all devices after invoking
0162  *  subsystem-level @thaw_noirq() for all of them.  It also may be executed
0163  *  directly after @freeze() in case of a transition error.
0164  *
0165  * @thaw_early: Prepare to execute @thaw().  Undo the changes made by the
0166  *  preceding @freeze_late().
0167  *
0168  * @poweroff: Hibernation-specific, executed after saving a hibernation image.
0169  *  Analogous to @suspend(), but it need not save the device's settings in
0170  *  memory.
0171  *  Subsystem-level @poweroff() is executed for all devices after invoking
0172  *  subsystem-level @prepare() for all of them.
0173  *
0174  * @poweroff_late: Continue operations started by @poweroff().  Analogous to
0175  *  @suspend_late(), but it need not save the device's settings in memory.
0176  *
0177  * @restore: Hibernation-specific, executed after restoring the contents of main
0178  *  memory from a hibernation image, analogous to @resume().
0179  *
0180  * @restore_early: Prepare to execute @restore(), analogous to @resume_early().
0181  *
0182  * @suspend_noirq: Complete the actions started by @suspend().  Carry out any
0183  *  additional operations required for suspending the device that might be
0184  *  racing with its driver's interrupt handler, which is guaranteed not to
0185  *  run while @suspend_noirq() is being executed.
0186  *  It generally is expected that the device will be in a low-power state
0187  *  (appropriate for the target system sleep state) after subsystem-level
0188  *  @suspend_noirq() has returned successfully.  If the device can generate
0189  *  system wakeup signals and is enabled to wake up the system, it should be
0190  *  configured to do so at that time.  However, depending on the platform
0191  *  and device's subsystem, @suspend() or @suspend_late() may be allowed to
0192  *  put the device into the low-power state and configure it to generate
0193  *  wakeup signals, in which case it generally is not necessary to define
0194  *  @suspend_noirq().
0195  *
0196  * @resume_noirq: Prepare for the execution of @resume() by carrying out any
0197  *  operations required for resuming the device that might be racing with
0198  *  its driver's interrupt handler, which is guaranteed not to run while
0199  *  @resume_noirq() is being executed.
0200  *
0201  * @freeze_noirq: Complete the actions started by @freeze().  Carry out any
0202  *  additional operations required for freezing the device that might be
0203  *  racing with its driver's interrupt handler, which is guaranteed not to
0204  *  run while @freeze_noirq() is being executed.
0205  *  The power state of the device should not be changed by either @freeze(),
0206  *  or @freeze_late(), or @freeze_noirq() and it should not be configured to
0207  *  signal system wakeup by any of these callbacks.
0208  *
0209  * @thaw_noirq: Prepare for the execution of @thaw() by carrying out any
0210  *  operations required for thawing the device that might be racing with its
0211  *  driver's interrupt handler, which is guaranteed not to run while
0212  *  @thaw_noirq() is being executed.
0213  *
0214  * @poweroff_noirq: Complete the actions started by @poweroff().  Analogous to
0215  *  @suspend_noirq(), but it need not save the device's settings in memory.
0216  *
0217  * @restore_noirq: Prepare for the execution of @restore() by carrying out any
0218  *  operations required for thawing the device that might be racing with its
0219  *  driver's interrupt handler, which is guaranteed not to run while
0220  *  @restore_noirq() is being executed.  Analogous to @resume_noirq().
0221  *
0222  * @runtime_suspend: Prepare the device for a condition in which it won't be
0223  *  able to communicate with the CPU(s) and RAM due to power management.
0224  *  This need not mean that the device should be put into a low-power state.
0225  *  For example, if the device is behind a link which is about to be turned
0226  *  off, the device may remain at full power.  If the device does go to low
0227  *  power and is capable of generating runtime wakeup events, remote wakeup
0228  *  (i.e., a hardware mechanism allowing the device to request a change of
0229  *  its power state via an interrupt) should be enabled for it.
0230  *
0231  * @runtime_resume: Put the device into the fully active state in response to a
0232  *  wakeup event generated by hardware or at the request of software.  If
0233  *  necessary, put the device into the full-power state and restore its
0234  *  registers, so that it is fully operational.
0235  *
0236  * @runtime_idle: Device appears to be inactive and it might be put into a
0237  *  low-power state if all of the necessary conditions are satisfied.
0238  *  Check these conditions, and return 0 if it's appropriate to let the PM
0239  *  core queue a suspend request for the device.
0240  *
0241  * Several device power state transitions are externally visible, affecting
0242  * the state of pending I/O queues and (for drivers that touch hardware)
0243  * interrupts, wakeups, DMA, and other hardware state.  There may also be
0244  * internal transitions to various low-power modes which are transparent
0245  * to the rest of the driver stack (such as a driver that's ON gating off
0246  * clocks which are not in active use).
0247  *
0248  * The externally visible transitions are handled with the help of callbacks
0249  * included in this structure in such a way that, typically, two levels of
0250  * callbacks are involved.  First, the PM core executes callbacks provided by PM
0251  * domains, device types, classes and bus types.  They are the subsystem-level
0252  * callbacks expected to execute callbacks provided by device drivers, although
0253  * they may choose not to do that.  If the driver callbacks are executed, they
0254  * have to collaborate with the subsystem-level callbacks to achieve the goals
0255  * appropriate for the given system transition, given transition phase and the
0256  * subsystem the device belongs to.
0257  *
0258  * All of the above callbacks, except for @complete(), return error codes.
0259  * However, the error codes returned by @resume(), @thaw(), @restore(),
0260  * @resume_noirq(), @thaw_noirq(), and @restore_noirq(), do not cause the PM
0261  * core to abort the resume transition during which they are returned.  The
0262  * error codes returned in those cases are only printed to the system logs for
0263  * debugging purposes.  Still, it is recommended that drivers only return error
0264  * codes from their resume methods in case of an unrecoverable failure (i.e.
0265  * when the device being handled refuses to resume and becomes unusable) to
0266  * allow the PM core to be modified in the future, so that it can avoid
0267  * attempting to handle devices that failed to resume and their children.
0268  *
0269  * It is allowed to unregister devices while the above callbacks are being
0270  * executed.  However, a callback routine MUST NOT try to unregister the device
0271  * it was called for, although it may unregister children of that device (for
0272  * example, if it detects that a child was unplugged while the system was
0273  * asleep).
0274  *
0275  * There also are callbacks related to runtime power management of devices.
0276  * Again, as a rule these callbacks are executed by the PM core for subsystems
0277  * (PM domains, device types, classes and bus types) and the subsystem-level
0278  * callbacks are expected to invoke the driver callbacks.  Moreover, the exact
0279  * actions to be performed by a device driver's callbacks generally depend on
0280  * the platform and subsystem the device belongs to.
0281  *
0282  * Refer to Documentation/power/runtime_pm.rst for more information about the
0283  * role of the @runtime_suspend(), @runtime_resume() and @runtime_idle()
0284  * callbacks in device runtime power management.
0285  */
0286 struct dev_pm_ops {
0287     int (*prepare)(struct device *dev);
0288     void (*complete)(struct device *dev);
0289     int (*suspend)(struct device *dev);
0290     int (*resume)(struct device *dev);
0291     int (*freeze)(struct device *dev);
0292     int (*thaw)(struct device *dev);
0293     int (*poweroff)(struct device *dev);
0294     int (*restore)(struct device *dev);
0295     int (*suspend_late)(struct device *dev);
0296     int (*resume_early)(struct device *dev);
0297     int (*freeze_late)(struct device *dev);
0298     int (*thaw_early)(struct device *dev);
0299     int (*poweroff_late)(struct device *dev);
0300     int (*restore_early)(struct device *dev);
0301     int (*suspend_noirq)(struct device *dev);
0302     int (*resume_noirq)(struct device *dev);
0303     int (*freeze_noirq)(struct device *dev);
0304     int (*thaw_noirq)(struct device *dev);
0305     int (*poweroff_noirq)(struct device *dev);
0306     int (*restore_noirq)(struct device *dev);
0307     int (*runtime_suspend)(struct device *dev);
0308     int (*runtime_resume)(struct device *dev);
0309     int (*runtime_idle)(struct device *dev);
0310 };
0311 
0312 #define SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0313     .suspend = pm_sleep_ptr(suspend_fn), \
0314     .resume = pm_sleep_ptr(resume_fn), \
0315     .freeze = pm_sleep_ptr(suspend_fn), \
0316     .thaw = pm_sleep_ptr(resume_fn), \
0317     .poweroff = pm_sleep_ptr(suspend_fn), \
0318     .restore = pm_sleep_ptr(resume_fn),
0319 
0320 #define LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0321     .suspend_late = pm_sleep_ptr(suspend_fn), \
0322     .resume_early = pm_sleep_ptr(resume_fn), \
0323     .freeze_late = pm_sleep_ptr(suspend_fn), \
0324     .thaw_early = pm_sleep_ptr(resume_fn), \
0325     .poweroff_late = pm_sleep_ptr(suspend_fn), \
0326     .restore_early = pm_sleep_ptr(resume_fn),
0327 
0328 #define NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0329     .suspend_noirq = pm_sleep_ptr(suspend_fn), \
0330     .resume_noirq = pm_sleep_ptr(resume_fn), \
0331     .freeze_noirq = pm_sleep_ptr(suspend_fn), \
0332     .thaw_noirq = pm_sleep_ptr(resume_fn), \
0333     .poweroff_noirq = pm_sleep_ptr(suspend_fn), \
0334     .restore_noirq = pm_sleep_ptr(resume_fn),
0335 
0336 #define RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
0337     .runtime_suspend = suspend_fn, \
0338     .runtime_resume = resume_fn, \
0339     .runtime_idle = idle_fn,
0340 
0341 #ifdef CONFIG_PM_SLEEP
0342 #define SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0343     SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
0344 #else
0345 #define SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
0346 #endif
0347 
0348 #ifdef CONFIG_PM_SLEEP
0349 #define SET_LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0350     LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
0351 #else
0352 #define SET_LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
0353 #endif
0354 
0355 #ifdef CONFIG_PM_SLEEP
0356 #define SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0357     NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
0358 #else
0359 #define SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
0360 #endif
0361 
0362 #ifdef CONFIG_PM
0363 #define SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
0364     RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn)
0365 #else
0366 #define SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn)
0367 #endif
0368 
0369 #define _DEFINE_DEV_PM_OPS(name, \
0370                suspend_fn, resume_fn, \
0371                runtime_suspend_fn, runtime_resume_fn, idle_fn) \
0372 const struct dev_pm_ops name = { \
0373     SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0374     RUNTIME_PM_OPS(runtime_suspend_fn, runtime_resume_fn, idle_fn) \
0375 }
0376 
0377 #ifdef CONFIG_PM
0378 #define _EXPORT_DEV_PM_OPS(name, suspend_fn, resume_fn, runtime_suspend_fn, \
0379                runtime_resume_fn, idle_fn, sec, ns)     \
0380     _DEFINE_DEV_PM_OPS(name, suspend_fn, resume_fn, runtime_suspend_fn, \
0381                runtime_resume_fn, idle_fn); \
0382     __EXPORT_SYMBOL(name, sec, ns)
0383 #else
0384 #define _EXPORT_DEV_PM_OPS(name, suspend_fn, resume_fn, runtime_suspend_fn, \
0385                runtime_resume_fn, idle_fn, sec, ns) \
0386 static __maybe_unused _DEFINE_DEV_PM_OPS(__static_##name, suspend_fn, \
0387                      resume_fn, runtime_suspend_fn, \
0388                      runtime_resume_fn, idle_fn)
0389 #endif
0390 
0391 /*
0392  * Use this if you want to use the same suspend and resume callbacks for suspend
0393  * to RAM and hibernation.
0394  *
0395  * If the underlying dev_pm_ops struct symbol has to be exported, use
0396  * EXPORT_SIMPLE_DEV_PM_OPS() or EXPORT_GPL_SIMPLE_DEV_PM_OPS() instead.
0397  */
0398 #define DEFINE_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
0399     _DEFINE_DEV_PM_OPS(name, suspend_fn, resume_fn, NULL, NULL, NULL)
0400 
0401 #define EXPORT_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
0402     _EXPORT_DEV_PM_OPS(name, suspend_fn, resume_fn, NULL, NULL, NULL, "", "")
0403 #define EXPORT_GPL_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
0404     _EXPORT_DEV_PM_OPS(name, suspend_fn, resume_fn, NULL, NULL, NULL, "_gpl", "")
0405 #define EXPORT_NS_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn, ns)    \
0406     _EXPORT_DEV_PM_OPS(name, suspend_fn, resume_fn, NULL, NULL, NULL, "", #ns)
0407 #define EXPORT_NS_GPL_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn, ns)    \
0408     _EXPORT_DEV_PM_OPS(name, suspend_fn, resume_fn, NULL, NULL, NULL, "_gpl", #ns)
0409 
0410 /* Deprecated. Use DEFINE_SIMPLE_DEV_PM_OPS() instead. */
0411 #define SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
0412 const struct dev_pm_ops __maybe_unused name = { \
0413     SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0414 }
0415 
0416 /*
0417  * Use this for defining a set of PM operations to be used in all situations
0418  * (system suspend, hibernation or runtime PM).
0419  * NOTE: In general, system suspend callbacks, .suspend() and .resume(), should
0420  * be different from the corresponding runtime PM callbacks, .runtime_suspend(),
0421  * and .runtime_resume(), because .runtime_suspend() always works on an already
0422  * quiescent device, while .suspend() should assume that the device may be doing
0423  * something when it is called (it should ensure that the device will be
0424  * quiescent after it has returned).  Therefore it's better to point the "late"
0425  * suspend and "early" resume callback pointers, .suspend_late() and
0426  * .resume_early(), to the same routines as .runtime_suspend() and
0427  * .runtime_resume(), respectively (and analogously for hibernation).
0428  *
0429  * Deprecated. You most likely don't want this macro. Use
0430  * DEFINE_RUNTIME_DEV_PM_OPS() instead.
0431  */
0432 #define UNIVERSAL_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \
0433 const struct dev_pm_ops __maybe_unused name = { \
0434     SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
0435     SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
0436 }
0437 
0438 #define pm_ptr(_ptr) PTR_IF(IS_ENABLED(CONFIG_PM), (_ptr))
0439 #define pm_sleep_ptr(_ptr) PTR_IF(IS_ENABLED(CONFIG_PM_SLEEP), (_ptr))
0440 
0441 /*
0442  * PM_EVENT_ messages
0443  *
0444  * The following PM_EVENT_ messages are defined for the internal use of the PM
0445  * core, in order to provide a mechanism allowing the high level suspend and
0446  * hibernation code to convey the necessary information to the device PM core
0447  * code:
0448  *
0449  * ON       No transition.
0450  *
0451  * FREEZE   System is going to hibernate, call ->prepare() and ->freeze()
0452  *      for all devices.
0453  *
0454  * SUSPEND  System is going to suspend, call ->prepare() and ->suspend()
0455  *      for all devices.
0456  *
0457  * HIBERNATE    Hibernation image has been saved, call ->prepare() and
0458  *      ->poweroff() for all devices.
0459  *
0460  * QUIESCE  Contents of main memory are going to be restored from a (loaded)
0461  *      hibernation image, call ->prepare() and ->freeze() for all
0462  *      devices.
0463  *
0464  * RESUME   System is resuming, call ->resume() and ->complete() for all
0465  *      devices.
0466  *
0467  * THAW     Hibernation image has been created, call ->thaw() and
0468  *      ->complete() for all devices.
0469  *
0470  * RESTORE  Contents of main memory have been restored from a hibernation
0471  *      image, call ->restore() and ->complete() for all devices.
0472  *
0473  * RECOVER  Creation of a hibernation image or restoration of the main
0474  *      memory contents from a hibernation image has failed, call
0475  *      ->thaw() and ->complete() for all devices.
0476  *
0477  * The following PM_EVENT_ messages are defined for internal use by
0478  * kernel subsystems.  They are never issued by the PM core.
0479  *
0480  * USER_SUSPEND     Manual selective suspend was issued by userspace.
0481  *
0482  * USER_RESUME      Manual selective resume was issued by userspace.
0483  *
0484  * REMOTE_WAKEUP    Remote-wakeup request was received from the device.
0485  *
0486  * AUTO_SUSPEND     Automatic (device idle) runtime suspend was
0487  *          initiated by the subsystem.
0488  *
0489  * AUTO_RESUME      Automatic (device needed) runtime resume was
0490  *          requested by a driver.
0491  */
0492 
0493 #define PM_EVENT_INVALID    (-1)
0494 #define PM_EVENT_ON     0x0000
0495 #define PM_EVENT_FREEZE     0x0001
0496 #define PM_EVENT_SUSPEND    0x0002
0497 #define PM_EVENT_HIBERNATE  0x0004
0498 #define PM_EVENT_QUIESCE    0x0008
0499 #define PM_EVENT_RESUME     0x0010
0500 #define PM_EVENT_THAW       0x0020
0501 #define PM_EVENT_RESTORE    0x0040
0502 #define PM_EVENT_RECOVER    0x0080
0503 #define PM_EVENT_USER       0x0100
0504 #define PM_EVENT_REMOTE     0x0200
0505 #define PM_EVENT_AUTO       0x0400
0506 
0507 #define PM_EVENT_SLEEP      (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE)
0508 #define PM_EVENT_USER_SUSPEND   (PM_EVENT_USER | PM_EVENT_SUSPEND)
0509 #define PM_EVENT_USER_RESUME    (PM_EVENT_USER | PM_EVENT_RESUME)
0510 #define PM_EVENT_REMOTE_RESUME  (PM_EVENT_REMOTE | PM_EVENT_RESUME)
0511 #define PM_EVENT_AUTO_SUSPEND   (PM_EVENT_AUTO | PM_EVENT_SUSPEND)
0512 #define PM_EVENT_AUTO_RESUME    (PM_EVENT_AUTO | PM_EVENT_RESUME)
0513 
0514 #define PMSG_INVALID    ((struct pm_message){ .event = PM_EVENT_INVALID, })
0515 #define PMSG_ON     ((struct pm_message){ .event = PM_EVENT_ON, })
0516 #define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, })
0517 #define PMSG_QUIESCE    ((struct pm_message){ .event = PM_EVENT_QUIESCE, })
0518 #define PMSG_SUSPEND    ((struct pm_message){ .event = PM_EVENT_SUSPEND, })
0519 #define PMSG_HIBERNATE  ((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
0520 #define PMSG_RESUME ((struct pm_message){ .event = PM_EVENT_RESUME, })
0521 #define PMSG_THAW   ((struct pm_message){ .event = PM_EVENT_THAW, })
0522 #define PMSG_RESTORE    ((struct pm_message){ .event = PM_EVENT_RESTORE, })
0523 #define PMSG_RECOVER    ((struct pm_message){ .event = PM_EVENT_RECOVER, })
0524 #define PMSG_USER_SUSPEND   ((struct pm_message) \
0525                     { .event = PM_EVENT_USER_SUSPEND, })
0526 #define PMSG_USER_RESUME    ((struct pm_message) \
0527                     { .event = PM_EVENT_USER_RESUME, })
0528 #define PMSG_REMOTE_RESUME  ((struct pm_message) \
0529                     { .event = PM_EVENT_REMOTE_RESUME, })
0530 #define PMSG_AUTO_SUSPEND   ((struct pm_message) \
0531                     { .event = PM_EVENT_AUTO_SUSPEND, })
0532 #define PMSG_AUTO_RESUME    ((struct pm_message) \
0533                     { .event = PM_EVENT_AUTO_RESUME, })
0534 
0535 #define PMSG_IS_AUTO(msg)   (((msg).event & PM_EVENT_AUTO) != 0)
0536 
0537 /*
0538  * Device run-time power management status.
0539  *
0540  * These status labels are used internally by the PM core to indicate the
0541  * current status of a device with respect to the PM core operations.  They do
0542  * not reflect the actual power state of the device or its status as seen by the
0543  * driver.
0544  *
0545  * RPM_ACTIVE       Device is fully operational.  Indicates that the device
0546  *          bus type's ->runtime_resume() callback has completed
0547  *          successfully.
0548  *
0549  * RPM_SUSPENDED    Device bus type's ->runtime_suspend() callback has
0550  *          completed successfully.  The device is regarded as
0551  *          suspended.
0552  *
0553  * RPM_RESUMING     Device bus type's ->runtime_resume() callback is being
0554  *          executed.
0555  *
0556  * RPM_SUSPENDING   Device bus type's ->runtime_suspend() callback is being
0557  *          executed.
0558  */
0559 
0560 enum rpm_status {
0561     RPM_INVALID = -1,
0562     RPM_ACTIVE = 0,
0563     RPM_RESUMING,
0564     RPM_SUSPENDED,
0565     RPM_SUSPENDING,
0566 };
0567 
0568 /*
0569  * Device run-time power management request types.
0570  *
0571  * RPM_REQ_NONE     Do nothing.
0572  *
0573  * RPM_REQ_IDLE     Run the device bus type's ->runtime_idle() callback
0574  *
0575  * RPM_REQ_SUSPEND  Run the device bus type's ->runtime_suspend() callback
0576  *
0577  * RPM_REQ_AUTOSUSPEND  Same as RPM_REQ_SUSPEND, but not until the device has
0578  *          been inactive for as long as power.autosuspend_delay
0579  *
0580  * RPM_REQ_RESUME   Run the device bus type's ->runtime_resume() callback
0581  */
0582 
0583 enum rpm_request {
0584     RPM_REQ_NONE = 0,
0585     RPM_REQ_IDLE,
0586     RPM_REQ_SUSPEND,
0587     RPM_REQ_AUTOSUSPEND,
0588     RPM_REQ_RESUME,
0589 };
0590 
0591 struct wakeup_source;
0592 struct wake_irq;
0593 struct pm_domain_data;
0594 
0595 struct pm_subsys_data {
0596     spinlock_t lock;
0597     unsigned int refcount;
0598 #ifdef CONFIG_PM_CLK
0599     unsigned int clock_op_might_sleep;
0600     struct mutex clock_mutex;
0601     struct list_head clock_list;
0602 #endif
0603 #ifdef CONFIG_PM_GENERIC_DOMAINS
0604     struct pm_domain_data *domain_data;
0605 #endif
0606 };
0607 
0608 /*
0609  * Driver flags to control system suspend/resume behavior.
0610  *
0611  * These flags can be set by device drivers at the probe time.  They need not be
0612  * cleared by the drivers as the driver core will take care of that.
0613  *
0614  * NO_DIRECT_COMPLETE: Do not apply direct-complete optimization to the device.
0615  * SMART_PREPARE: Take the driver ->prepare callback return value into account.
0616  * SMART_SUSPEND: Avoid resuming the device from runtime suspend.
0617  * MAY_SKIP_RESUME: Allow driver "noirq" and "early" callbacks to be skipped.
0618  *
0619  * See Documentation/driver-api/pm/devices.rst for details.
0620  */
0621 #define DPM_FLAG_NO_DIRECT_COMPLETE BIT(0)
0622 #define DPM_FLAG_SMART_PREPARE      BIT(1)
0623 #define DPM_FLAG_SMART_SUSPEND      BIT(2)
0624 #define DPM_FLAG_MAY_SKIP_RESUME    BIT(3)
0625 
0626 struct dev_pm_info {
0627     pm_message_t        power_state;
0628     unsigned int        can_wakeup:1;
0629     unsigned int        async_suspend:1;
0630     bool            in_dpm_list:1;  /* Owned by the PM core */
0631     bool            is_prepared:1;  /* Owned by the PM core */
0632     bool            is_suspended:1; /* Ditto */
0633     bool            is_noirq_suspended:1;
0634     bool            is_late_suspended:1;
0635     bool            no_pm:1;
0636     bool            early_init:1;   /* Owned by the PM core */
0637     bool            direct_complete:1;  /* Owned by the PM core */
0638     u32         driver_flags;
0639     spinlock_t      lock;
0640 #ifdef CONFIG_PM_SLEEP
0641     struct list_head    entry;
0642     struct completion   completion;
0643     struct wakeup_source    *wakeup;
0644     bool            wakeup_path:1;
0645     bool            syscore:1;
0646     bool            no_pm_callbacks:1;  /* Owned by the PM core */
0647     unsigned int        must_resume:1;  /* Owned by the PM core */
0648     unsigned int        may_skip_resume:1;  /* Set by subsystems */
0649 #else
0650     unsigned int        should_wakeup:1;
0651 #endif
0652 #ifdef CONFIG_PM
0653     struct hrtimer      suspend_timer;
0654     u64         timer_expires;
0655     struct work_struct  work;
0656     wait_queue_head_t   wait_queue;
0657     struct wake_irq     *wakeirq;
0658     atomic_t        usage_count;
0659     atomic_t        child_count;
0660     unsigned int        disable_depth:3;
0661     unsigned int        idle_notification:1;
0662     unsigned int        request_pending:1;
0663     unsigned int        deferred_resume:1;
0664     unsigned int        needs_force_resume:1;
0665     unsigned int        runtime_auto:1;
0666     bool            ignore_children:1;
0667     unsigned int        no_callbacks:1;
0668     unsigned int        irq_safe:1;
0669     unsigned int        use_autosuspend:1;
0670     unsigned int        timer_autosuspends:1;
0671     unsigned int        memalloc_noio:1;
0672     unsigned int        links_count;
0673     enum rpm_request    request;
0674     enum rpm_status     runtime_status;
0675     enum rpm_status     last_status;
0676     int         runtime_error;
0677     int         autosuspend_delay;
0678     u64         last_busy;
0679     u64         active_time;
0680     u64         suspended_time;
0681     u64         accounting_timestamp;
0682 #endif
0683     struct pm_subsys_data   *subsys_data;  /* Owned by the subsystem. */
0684     void (*set_latency_tolerance)(struct device *, s32);
0685     struct dev_pm_qos   *qos;
0686 };
0687 
0688 extern int dev_pm_get_subsys_data(struct device *dev);
0689 extern void dev_pm_put_subsys_data(struct device *dev);
0690 
0691 /**
0692  * struct dev_pm_domain - power management domain representation.
0693  *
0694  * @ops: Power management operations associated with this domain.
0695  * @start: Called when a user needs to start the device via the domain.
0696  * @detach: Called when removing a device from the domain.
0697  * @activate: Called before executing probe routines for bus types and drivers.
0698  * @sync: Called after successful driver probe.
0699  * @dismiss: Called after unsuccessful driver probe and after driver removal.
0700  *
0701  * Power domains provide callbacks that are executed during system suspend,
0702  * hibernation, system resume and during runtime PM transitions instead of
0703  * subsystem-level and driver-level callbacks.
0704  */
0705 struct dev_pm_domain {
0706     struct dev_pm_ops   ops;
0707     int (*start)(struct device *dev);
0708     void (*detach)(struct device *dev, bool power_off);
0709     int (*activate)(struct device *dev);
0710     void (*sync)(struct device *dev);
0711     void (*dismiss)(struct device *dev);
0712 };
0713 
0714 /*
0715  * The PM_EVENT_ messages are also used by drivers implementing the legacy
0716  * suspend framework, based on the ->suspend() and ->resume() callbacks common
0717  * for suspend and hibernation transitions, according to the rules below.
0718  */
0719 
0720 /* Necessary, because several drivers use PM_EVENT_PRETHAW */
0721 #define PM_EVENT_PRETHAW PM_EVENT_QUIESCE
0722 
0723 /*
0724  * One transition is triggered by resume(), after a suspend() call; the
0725  * message is implicit:
0726  *
0727  * ON       Driver starts working again, responding to hardware events
0728  *      and software requests.  The hardware may have gone through
0729  *      a power-off reset, or it may have maintained state from the
0730  *      previous suspend() which the driver will rely on while
0731  *      resuming.  On most platforms, there are no restrictions on
0732  *      availability of resources like clocks during resume().
0733  *
0734  * Other transitions are triggered by messages sent using suspend().  All
0735  * these transitions quiesce the driver, so that I/O queues are inactive.
0736  * That commonly entails turning off IRQs and DMA; there may be rules
0737  * about how to quiesce that are specific to the bus or the device's type.
0738  * (For example, network drivers mark the link state.)  Other details may
0739  * differ according to the message:
0740  *
0741  * SUSPEND  Quiesce, enter a low power device state appropriate for
0742  *      the upcoming system state (such as PCI_D3hot), and enable
0743  *      wakeup events as appropriate.
0744  *
0745  * HIBERNATE    Enter a low power device state appropriate for the hibernation
0746  *      state (eg. ACPI S4) and enable wakeup events as appropriate.
0747  *
0748  * FREEZE   Quiesce operations so that a consistent image can be saved;
0749  *      but do NOT otherwise enter a low power device state, and do
0750  *      NOT emit system wakeup events.
0751  *
0752  * PRETHAW  Quiesce as if for FREEZE; additionally, prepare for restoring
0753  *      the system from a snapshot taken after an earlier FREEZE.
0754  *      Some drivers will need to reset their hardware state instead
0755  *      of preserving it, to ensure that it's never mistaken for the
0756  *      state which that earlier snapshot had set up.
0757  *
0758  * A minimally power-aware driver treats all messages as SUSPEND, fully
0759  * reinitializes its device during resume() -- whether or not it was reset
0760  * during the suspend/resume cycle -- and can't issue wakeup events.
0761  *
0762  * More power-aware drivers may also use low power states at runtime as
0763  * well as during system sleep states like PM_SUSPEND_STANDBY.  They may
0764  * be able to use wakeup events to exit from runtime low-power states,
0765  * or from system low-power states such as standby or suspend-to-RAM.
0766  */
0767 
0768 #ifdef CONFIG_PM_SLEEP
0769 extern void device_pm_lock(void);
0770 extern void dpm_resume_start(pm_message_t state);
0771 extern void dpm_resume_end(pm_message_t state);
0772 extern void dpm_resume_noirq(pm_message_t state);
0773 extern void dpm_resume_early(pm_message_t state);
0774 extern void dpm_resume(pm_message_t state);
0775 extern void dpm_complete(pm_message_t state);
0776 
0777 extern void device_pm_unlock(void);
0778 extern int dpm_suspend_end(pm_message_t state);
0779 extern int dpm_suspend_start(pm_message_t state);
0780 extern int dpm_suspend_noirq(pm_message_t state);
0781 extern int dpm_suspend_late(pm_message_t state);
0782 extern int dpm_suspend(pm_message_t state);
0783 extern int dpm_prepare(pm_message_t state);
0784 
0785 extern void __suspend_report_result(const char *function, struct device *dev, void *fn, int ret);
0786 
0787 #define suspend_report_result(dev, fn, ret)             \
0788     do {                                \
0789         __suspend_report_result(__func__, dev, fn, ret);    \
0790     } while (0)
0791 
0792 extern int device_pm_wait_for_dev(struct device *sub, struct device *dev);
0793 extern void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *));
0794 
0795 extern int pm_generic_prepare(struct device *dev);
0796 extern int pm_generic_suspend_late(struct device *dev);
0797 extern int pm_generic_suspend_noirq(struct device *dev);
0798 extern int pm_generic_suspend(struct device *dev);
0799 extern int pm_generic_resume_early(struct device *dev);
0800 extern int pm_generic_resume_noirq(struct device *dev);
0801 extern int pm_generic_resume(struct device *dev);
0802 extern int pm_generic_freeze_noirq(struct device *dev);
0803 extern int pm_generic_freeze_late(struct device *dev);
0804 extern int pm_generic_freeze(struct device *dev);
0805 extern int pm_generic_thaw_noirq(struct device *dev);
0806 extern int pm_generic_thaw_early(struct device *dev);
0807 extern int pm_generic_thaw(struct device *dev);
0808 extern int pm_generic_restore_noirq(struct device *dev);
0809 extern int pm_generic_restore_early(struct device *dev);
0810 extern int pm_generic_restore(struct device *dev);
0811 extern int pm_generic_poweroff_noirq(struct device *dev);
0812 extern int pm_generic_poweroff_late(struct device *dev);
0813 extern int pm_generic_poweroff(struct device *dev);
0814 extern void pm_generic_complete(struct device *dev);
0815 
0816 extern bool dev_pm_skip_resume(struct device *dev);
0817 extern bool dev_pm_skip_suspend(struct device *dev);
0818 
0819 #else /* !CONFIG_PM_SLEEP */
0820 
0821 #define device_pm_lock() do {} while (0)
0822 #define device_pm_unlock() do {} while (0)
0823 
0824 static inline int dpm_suspend_start(pm_message_t state)
0825 {
0826     return 0;
0827 }
0828 
0829 #define suspend_report_result(dev, fn, ret) do {} while (0)
0830 
0831 static inline int device_pm_wait_for_dev(struct device *a, struct device *b)
0832 {
0833     return 0;
0834 }
0835 
0836 static inline void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
0837 {
0838 }
0839 
0840 #define pm_generic_prepare      NULL
0841 #define pm_generic_suspend_late     NULL
0842 #define pm_generic_suspend_noirq    NULL
0843 #define pm_generic_suspend      NULL
0844 #define pm_generic_resume_early     NULL
0845 #define pm_generic_resume_noirq     NULL
0846 #define pm_generic_resume       NULL
0847 #define pm_generic_freeze_noirq     NULL
0848 #define pm_generic_freeze_late      NULL
0849 #define pm_generic_freeze       NULL
0850 #define pm_generic_thaw_noirq       NULL
0851 #define pm_generic_thaw_early       NULL
0852 #define pm_generic_thaw         NULL
0853 #define pm_generic_restore_noirq    NULL
0854 #define pm_generic_restore_early    NULL
0855 #define pm_generic_restore      NULL
0856 #define pm_generic_poweroff_noirq   NULL
0857 #define pm_generic_poweroff_late    NULL
0858 #define pm_generic_poweroff     NULL
0859 #define pm_generic_complete     NULL
0860 #endif /* !CONFIG_PM_SLEEP */
0861 
0862 /* How to reorder dpm_list after device_move() */
0863 enum dpm_order {
0864     DPM_ORDER_NONE,
0865     DPM_ORDER_DEV_AFTER_PARENT,
0866     DPM_ORDER_PARENT_BEFORE_DEV,
0867     DPM_ORDER_DEV_LAST,
0868 };
0869 
0870 #endif /* _LINUX_PM_H */