162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci.. include:: <isonum.txt>
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _driverapi_pm_devices:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci==============================
762306a36Sopenharmony_ciDevice Power Management Basics
862306a36Sopenharmony_ci==============================
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci:Copyright: |copy| 2010-2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
1162306a36Sopenharmony_ci:Copyright: |copy| 2010 Alan Stern <stern@rowland.harvard.edu>
1262306a36Sopenharmony_ci:Copyright: |copy| 2016 Intel Corporation
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci:Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciMost of the code in Linux is device drivers, so most of the Linux power
1862306a36Sopenharmony_cimanagement (PM) code is also driver-specific.  Most drivers will do very
1962306a36Sopenharmony_cilittle; others, especially for platforms with small batteries (like cell
2062306a36Sopenharmony_ciphones), will do a lot.
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciThis writeup gives an overview of how drivers interact with system-wide
2362306a36Sopenharmony_cipower management goals, emphasizing the models and interfaces that are
2462306a36Sopenharmony_cishared by everything that hooks up to the driver model core.  Read it as
2562306a36Sopenharmony_cibackground for the domain-specific work you'd do with any specific driver.
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciTwo Models for Device Power Management
2962306a36Sopenharmony_ci======================================
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciDrivers will use one or both of these models to put devices into low-power
3262306a36Sopenharmony_cistates:
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci    System Sleep model:
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	Drivers can enter low-power states as part of entering system-wide
3762306a36Sopenharmony_ci	low-power states like "suspend" (also known as "suspend-to-RAM"), or
3862306a36Sopenharmony_ci	(mostly for systems with disks) "hibernation" (also known as
3962306a36Sopenharmony_ci	"suspend-to-disk").
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	This is something that device, bus, and class drivers collaborate on
4262306a36Sopenharmony_ci	by implementing various role-specific suspend and resume methods to
4362306a36Sopenharmony_ci	cleanly power down hardware and software subsystems, then reactivate
4462306a36Sopenharmony_ci	them without loss of data.
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	Some drivers can manage hardware wakeup events, which make the system
4762306a36Sopenharmony_ci	leave the low-power state.  This feature may be enabled or disabled
4862306a36Sopenharmony_ci	using the relevant :file:`/sys/devices/.../power/wakeup` file (for
4962306a36Sopenharmony_ci	Ethernet drivers the ioctl interface used by ethtool may also be used
5062306a36Sopenharmony_ci	for this purpose); enabling it may cost some power usage, but let the
5162306a36Sopenharmony_ci	whole system enter low-power states more often.
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci    Runtime Power Management model:
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	Devices may also be put into low-power states while the system is
5662306a36Sopenharmony_ci	running, independently of other power management activity in principle.
5762306a36Sopenharmony_ci	However, devices are not generally independent of each other (for
5862306a36Sopenharmony_ci	example, a parent device cannot be suspended unless all of its child
5962306a36Sopenharmony_ci	devices have been suspended).  Moreover, depending on the bus type the
6062306a36Sopenharmony_ci	device is on, it may be necessary to carry out some bus-specific
6162306a36Sopenharmony_ci	operations on the device for this purpose.  Devices put into low power
6262306a36Sopenharmony_ci	states at run time may require special handling during system-wide power
6362306a36Sopenharmony_ci	transitions (suspend or hibernation).
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	For these reasons not only the device driver itself, but also the
6662306a36Sopenharmony_ci	appropriate subsystem (bus type, device type or device class) driver and
6762306a36Sopenharmony_ci	the PM core are involved in runtime power management.  As in the system
6862306a36Sopenharmony_ci	sleep power management case, they need to collaborate by implementing
6962306a36Sopenharmony_ci	various role-specific suspend and resume methods, so that the hardware
7062306a36Sopenharmony_ci	is cleanly powered down and reactivated without data or service loss.
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciThere's not a lot to be said about those low-power states except that they are
7362306a36Sopenharmony_civery system-specific, and often device-specific.  Also, that if enough devices
7462306a36Sopenharmony_cihave been put into low-power states (at runtime), the effect may be very similar
7562306a36Sopenharmony_cito entering some system-wide low-power state (system sleep) ... and that
7662306a36Sopenharmony_cisynergies exist, so that several drivers using runtime PM might put the system
7762306a36Sopenharmony_ciinto a state where even deeper power saving options are available.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciMost suspended devices will have quiesced all I/O: no more DMA or IRQs (except
8062306a36Sopenharmony_cifor wakeup events), no more data read or written, and requests from upstream
8162306a36Sopenharmony_cidrivers are no longer accepted.  A given bus or platform may have different
8262306a36Sopenharmony_cirequirements though.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciExamples of hardware wakeup events include an alarm from a real time clock,
8562306a36Sopenharmony_cinetwork wake-on-LAN packets, keyboard or mouse activity, and media insertion
8662306a36Sopenharmony_cior removal (for PCMCIA, MMC/SD, USB, and so on).
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciInterfaces for Entering System Sleep States
8962306a36Sopenharmony_ci===========================================
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ciThere are programming interfaces provided for subsystems (bus type, device type,
9262306a36Sopenharmony_cidevice class) and device drivers to allow them to participate in the power
9362306a36Sopenharmony_cimanagement of devices they are concerned with.  These interfaces cover both
9462306a36Sopenharmony_cisystem sleep and runtime power management.
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciDevice Power Management Operations
9862306a36Sopenharmony_ci----------------------------------
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciDevice power management operations, at the subsystem level as well as at the
10162306a36Sopenharmony_cidevice driver level, are implemented by defining and populating objects of type
10262306a36Sopenharmony_cistruct dev_pm_ops defined in :file:`include/linux/pm.h`.  The roles of the
10362306a36Sopenharmony_cimethods included in it will be explained in what follows.  For now, it should be
10462306a36Sopenharmony_cisufficient to remember that the last three methods are specific to runtime power
10562306a36Sopenharmony_cimanagement while the remaining ones are used during system-wide power
10662306a36Sopenharmony_citransitions.
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ciThere also is a deprecated "old" or "legacy" interface for power management
10962306a36Sopenharmony_cioperations available at least for some subsystems.  This approach does not use
11062306a36Sopenharmony_cistruct dev_pm_ops objects and it is suitable only for implementing system
11162306a36Sopenharmony_cisleep power management methods in a limited way.  Therefore it is not described
11262306a36Sopenharmony_ciin this document, so please refer directly to the source code for more
11362306a36Sopenharmony_ciinformation about it.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ciSubsystem-Level Methods
11762306a36Sopenharmony_ci-----------------------
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciThe core methods to suspend and resume devices reside in
12062306a36Sopenharmony_cistruct dev_pm_ops pointed to by the :c:member:`ops` member of
12162306a36Sopenharmony_cistruct dev_pm_domain, or by the :c:member:`pm` member of struct bus_type,
12262306a36Sopenharmony_cistruct device_type and struct class.  They are mostly of interest to the
12362306a36Sopenharmony_cipeople writing infrastructure for platforms and buses, like PCI or USB, or
12462306a36Sopenharmony_cidevice type and device class drivers.  They also are relevant to the writers of
12562306a36Sopenharmony_cidevice drivers whose subsystems (PM domains, device types, device classes and
12662306a36Sopenharmony_cibus types) don't provide all power management methods.
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciBus drivers implement these methods as appropriate for the hardware and the
12962306a36Sopenharmony_cidrivers using it; PCI works differently from USB, and so on.  Not many people
13062306a36Sopenharmony_ciwrite subsystem-level drivers; most driver code is a "device driver" that builds
13162306a36Sopenharmony_cion top of bus-specific framework code.
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ciFor more information on these driver calls, see the description later;
13462306a36Sopenharmony_cithey are called in phases for every device, respecting the parent-child
13562306a36Sopenharmony_cisequencing in the driver model tree.
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci:file:`/sys/devices/.../power/wakeup` files
13962306a36Sopenharmony_ci-------------------------------------------
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ciAll device objects in the driver model contain fields that control the handling
14262306a36Sopenharmony_ciof system wakeup events (hardware signals that can force the system out of a
14362306a36Sopenharmony_cisleep state).  These fields are initialized by bus or device driver code using
14462306a36Sopenharmony_ci:c:func:`device_set_wakeup_capable()` and :c:func:`device_set_wakeup_enable()`,
14562306a36Sopenharmony_cidefined in :file:`include/linux/pm_wakeup.h`.
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciThe :c:member:`power.can_wakeup` flag just records whether the device (and its
14862306a36Sopenharmony_cidriver) can physically support wakeup events.  The
14962306a36Sopenharmony_ci:c:func:`device_set_wakeup_capable()` routine affects this flag.  The
15062306a36Sopenharmony_ci:c:member:`power.wakeup` field is a pointer to an object of type
15162306a36Sopenharmony_cistruct wakeup_source used for controlling whether or not the device should use
15262306a36Sopenharmony_ciits system wakeup mechanism and for notifying the PM core of system wakeup
15362306a36Sopenharmony_cievents signaled by the device.  This object is only present for wakeup-capable
15462306a36Sopenharmony_cidevices (i.e. devices whose :c:member:`can_wakeup` flags are set) and is created
15562306a36Sopenharmony_ci(or removed) by :c:func:`device_set_wakeup_capable()`.
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ciWhether or not a device is capable of issuing wakeup events is a hardware
15862306a36Sopenharmony_cimatter, and the kernel is responsible for keeping track of it.  By contrast,
15962306a36Sopenharmony_ciwhether or not a wakeup-capable device should issue wakeup events is a policy
16062306a36Sopenharmony_cidecision, and it is managed by user space through a sysfs attribute: the
16162306a36Sopenharmony_ci:file:`power/wakeup` file.  User space can write the "enabled" or "disabled"
16262306a36Sopenharmony_cistrings to it to indicate whether or not, respectively, the device is supposed
16362306a36Sopenharmony_cito signal system wakeup.  This file is only present if the
16462306a36Sopenharmony_ci:c:member:`power.wakeup` object exists for the given device and is created (or
16562306a36Sopenharmony_ciremoved) along with that object, by :c:func:`device_set_wakeup_capable()`.
16662306a36Sopenharmony_ciReads from the file will return the corresponding string.
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ciThe initial value in the :file:`power/wakeup` file is "disabled" for the
16962306a36Sopenharmony_cimajority of devices; the major exceptions are power buttons, keyboards, and
17062306a36Sopenharmony_ciEthernet adapters whose WoL (wake-on-LAN) feature has been set up with ethtool.
17162306a36Sopenharmony_ciIt should also default to "enabled" for devices that don't generate wakeup
17262306a36Sopenharmony_cirequests on their own but merely forward wakeup requests from one bus to another
17362306a36Sopenharmony_ci(like PCI Express ports).
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciThe :c:func:`device_may_wakeup()` routine returns true only if the
17662306a36Sopenharmony_ci:c:member:`power.wakeup` object exists and the corresponding :file:`power/wakeup`
17762306a36Sopenharmony_cifile contains the "enabled" string.  This information is used by subsystems,
17862306a36Sopenharmony_cilike the PCI bus type code, to see whether or not to enable the devices' wakeup
17962306a36Sopenharmony_cimechanisms.  If device wakeup mechanisms are enabled or disabled directly by
18062306a36Sopenharmony_cidrivers, they also should use :c:func:`device_may_wakeup()` to decide what to do
18162306a36Sopenharmony_ciduring a system sleep transition.  Device drivers, however, are not expected to
18262306a36Sopenharmony_cicall :c:func:`device_set_wakeup_enable()` directly in any case.
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ciIt ought to be noted that system wakeup is conceptually different from "remote
18562306a36Sopenharmony_ciwakeup" used by runtime power management, although it may be supported by the
18662306a36Sopenharmony_cisame physical mechanism.  Remote wakeup is a feature allowing devices in
18762306a36Sopenharmony_cilow-power states to trigger specific interrupts to signal conditions in which
18862306a36Sopenharmony_cithey should be put into the full-power state.  Those interrupts may or may not
18962306a36Sopenharmony_cibe used to signal system wakeup events, depending on the hardware design.  On
19062306a36Sopenharmony_cisome systems it is impossible to trigger them from system sleep states.  In any
19162306a36Sopenharmony_cicase, remote wakeup should always be enabled for runtime power management for
19262306a36Sopenharmony_ciall devices and drivers that support it.
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci:file:`/sys/devices/.../power/control` files
19662306a36Sopenharmony_ci--------------------------------------------
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ciEach device in the driver model has a flag to control whether it is subject to
19962306a36Sopenharmony_ciruntime power management.  This flag, :c:member:`runtime_auto`, is initialized
20062306a36Sopenharmony_ciby the bus type (or generally subsystem) code using :c:func:`pm_runtime_allow()`
20162306a36Sopenharmony_cior :c:func:`pm_runtime_forbid()`; the default is to allow runtime power
20262306a36Sopenharmony_cimanagement.
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ciThe setting can be adjusted by user space by writing either "on" or "auto" to
20562306a36Sopenharmony_cithe device's :file:`power/control` sysfs file.  Writing "auto" calls
20662306a36Sopenharmony_ci:c:func:`pm_runtime_allow()`, setting the flag and allowing the device to be
20762306a36Sopenharmony_ciruntime power-managed by its driver.  Writing "on" calls
20862306a36Sopenharmony_ci:c:func:`pm_runtime_forbid()`, clearing the flag, returning the device to full
20962306a36Sopenharmony_cipower if it was in a low-power state, and preventing the
21062306a36Sopenharmony_cidevice from being runtime power-managed.  User space can check the current value
21162306a36Sopenharmony_ciof the :c:member:`runtime_auto` flag by reading that file.
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ciThe device's :c:member:`runtime_auto` flag has no effect on the handling of
21462306a36Sopenharmony_cisystem-wide power transitions.  In particular, the device can (and in the
21562306a36Sopenharmony_cimajority of cases should and will) be put into a low-power state during a
21662306a36Sopenharmony_cisystem-wide transition to a sleep state even though its :c:member:`runtime_auto`
21762306a36Sopenharmony_ciflag is clear.
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciFor more information about the runtime power management framework, refer to
22062306a36Sopenharmony_ciDocumentation/power/runtime_pm.rst.
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ciCalling Drivers to Enter and Leave System Sleep States
22462306a36Sopenharmony_ci======================================================
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ciWhen the system goes into a sleep state, each device's driver is asked to
22762306a36Sopenharmony_cisuspend the device by putting it into a state compatible with the target
22862306a36Sopenharmony_cisystem state.  That's usually some version of "off", but the details are
22962306a36Sopenharmony_cisystem-specific.  Also, wakeup-enabled devices will usually stay partly
23062306a36Sopenharmony_cifunctional in order to wake the system.
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ciWhen the system leaves that low-power state, the device's driver is asked to
23362306a36Sopenharmony_ciresume it by returning it to full power.  The suspend and resume operations
23462306a36Sopenharmony_cialways go together, and both are multi-phase operations.
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ciFor simple drivers, suspend might quiesce the device using class code
23762306a36Sopenharmony_ciand then turn its hardware as "off" as possible during suspend_noirq.  The
23862306a36Sopenharmony_cimatching resume calls would then completely reinitialize the hardware
23962306a36Sopenharmony_cibefore reactivating its class I/O queues.
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ciMore power-aware drivers might prepare the devices for triggering system wakeup
24262306a36Sopenharmony_cievents.
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ciCall Sequence Guarantees
24662306a36Sopenharmony_ci------------------------
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ciTo ensure that bridges and similar links needing to talk to a device are
24962306a36Sopenharmony_ciavailable when the device is suspended or resumed, the device hierarchy is
25062306a36Sopenharmony_ciwalked in a bottom-up order to suspend devices.  A top-down order is
25162306a36Sopenharmony_ciused to resume those devices.
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ciThe ordering of the device hierarchy is defined by the order in which devices
25462306a36Sopenharmony_ciget registered:  a child can never be registered, probed or resumed before
25562306a36Sopenharmony_ciits parent; and can't be removed or suspended after that parent.
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ciThe policy is that the device hierarchy should match hardware bus topology.
25862306a36Sopenharmony_ci[Or at least the control bus, for devices which use multiple busses.]
25962306a36Sopenharmony_ciIn particular, this means that a device registration may fail if the parent of
26062306a36Sopenharmony_cithe device is suspending (i.e. has been chosen by the PM core as the next
26162306a36Sopenharmony_cidevice to suspend) or has already suspended, as well as after all of the other
26262306a36Sopenharmony_cidevices have been suspended.  Device drivers must be prepared to cope with such
26362306a36Sopenharmony_cisituations.
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ciSystem Power Management Phases
26762306a36Sopenharmony_ci------------------------------
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ciSuspending or resuming the system is done in several phases.  Different phases
27062306a36Sopenharmony_ciare used for suspend-to-idle, shallow (standby), and deep ("suspend-to-RAM")
27162306a36Sopenharmony_cisleep states and the hibernation state ("suspend-to-disk").  Each phase involves
27262306a36Sopenharmony_ciexecuting callbacks for every device before the next phase begins.  Not all
27362306a36Sopenharmony_cibuses or classes support all these callbacks and not all drivers use all the
27462306a36Sopenharmony_cicallbacks.  The various phases always run after tasks have been frozen and
27562306a36Sopenharmony_cibefore they are unfrozen.  Furthermore, the ``*_noirq`` phases run at a time
27662306a36Sopenharmony_ciwhen IRQ handlers have been disabled (except for those marked with the
27762306a36Sopenharmony_ciIRQF_NO_SUSPEND flag).
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ciAll phases use PM domain, bus, type, class or driver callbacks (that is, methods
28062306a36Sopenharmony_cidefined in ``dev->pm_domain->ops``, ``dev->bus->pm``, ``dev->type->pm``,
28162306a36Sopenharmony_ci``dev->class->pm`` or ``dev->driver->pm``).  These callbacks are regarded by the
28262306a36Sopenharmony_ciPM core as mutually exclusive.  Moreover, PM domain callbacks always take
28362306a36Sopenharmony_ciprecedence over all of the other callbacks and, for example, type callbacks take
28462306a36Sopenharmony_ciprecedence over bus, class and driver callbacks.  To be precise, the following
28562306a36Sopenharmony_cirules are used to determine which callback to execute in the given phase:
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci    1.	If ``dev->pm_domain`` is present, the PM core will choose the callback
28862306a36Sopenharmony_ci	provided by ``dev->pm_domain->ops`` for execution.
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci    2.	Otherwise, if both ``dev->type`` and ``dev->type->pm`` are present, the
29162306a36Sopenharmony_ci	callback provided by ``dev->type->pm`` will be chosen for execution.
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci    3.	Otherwise, if both ``dev->class`` and ``dev->class->pm`` are present,
29462306a36Sopenharmony_ci	the callback provided by ``dev->class->pm`` will be chosen for
29562306a36Sopenharmony_ci	execution.
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci    4.	Otherwise, if both ``dev->bus`` and ``dev->bus->pm`` are present, the
29862306a36Sopenharmony_ci	callback provided by ``dev->bus->pm`` will be chosen for execution.
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ciThis allows PM domains and device types to override callbacks provided by bus
30162306a36Sopenharmony_citypes or device classes if necessary.
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ciThe PM domain, type, class and bus callbacks may in turn invoke device- or
30462306a36Sopenharmony_cidriver-specific methods stored in ``dev->driver->pm``, but they don't have to do
30562306a36Sopenharmony_cithat.
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ciIf the subsystem callback chosen for execution is not present, the PM core will
30862306a36Sopenharmony_ciexecute the corresponding method from the ``dev->driver->pm`` set instead if
30962306a36Sopenharmony_cithere is one.
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ciEntering System Suspend
31362306a36Sopenharmony_ci-----------------------
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ciWhen the system goes into the freeze, standby or memory sleep state,
31662306a36Sopenharmony_cithe phases are: ``prepare``, ``suspend``, ``suspend_late``, ``suspend_noirq``.
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci    1.	The ``prepare`` phase is meant to prevent races by preventing new
31962306a36Sopenharmony_ci	devices from being registered; the PM core would never know that all the
32062306a36Sopenharmony_ci	children of a device had been suspended if new children could be
32162306a36Sopenharmony_ci	registered at will.  [By contrast, from the PM core's perspective,
32262306a36Sopenharmony_ci	devices may be unregistered at any time.]  Unlike the other
32362306a36Sopenharmony_ci	suspend-related phases, during the ``prepare`` phase the device
32462306a36Sopenharmony_ci	hierarchy is traversed top-down.
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	After the ``->prepare`` callback method returns, no new children may be
32762306a36Sopenharmony_ci	registered below the device.  The method may also prepare the device or
32862306a36Sopenharmony_ci	driver in some way for the upcoming system power transition, but it
32962306a36Sopenharmony_ci	should not put the device into a low-power state.  Moreover, if the
33062306a36Sopenharmony_ci	device supports runtime power management, the ``->prepare`` callback
33162306a36Sopenharmony_ci	method must not update its state in case it is necessary to resume it
33262306a36Sopenharmony_ci	from runtime suspend later on.
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	For devices supporting runtime power management, the return value of the
33562306a36Sopenharmony_ci	prepare callback can be used to indicate to the PM core that it may
33662306a36Sopenharmony_ci	safely leave the device in runtime suspend (if runtime-suspended
33762306a36Sopenharmony_ci	already), provided that all of the device's descendants are also left in
33862306a36Sopenharmony_ci	runtime suspend.  Namely, if the prepare callback returns a positive
33962306a36Sopenharmony_ci	number and that happens for all of the descendants of the device too,
34062306a36Sopenharmony_ci	and all of them (including the device itself) are runtime-suspended, the
34162306a36Sopenharmony_ci	PM core will skip the ``suspend``, ``suspend_late`` and
34262306a36Sopenharmony_ci	``suspend_noirq`` phases as well as all of the corresponding phases of
34362306a36Sopenharmony_ci	the subsequent device resume for all of these devices.	In that case,
34462306a36Sopenharmony_ci	the ``->complete`` callback will be the next one invoked after the
34562306a36Sopenharmony_ci	``->prepare`` callback and is entirely responsible for putting the
34662306a36Sopenharmony_ci	device into a consistent state as appropriate.
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	Note that this direct-complete procedure applies even if the device is
34962306a36Sopenharmony_ci	disabled for runtime PM; only the runtime-PM status matters.  It follows
35062306a36Sopenharmony_ci	that if a device has system-sleep callbacks but does not support runtime
35162306a36Sopenharmony_ci	PM, then its prepare callback must never return a positive value.  This
35262306a36Sopenharmony_ci	is because all such devices are initially set to runtime-suspended with
35362306a36Sopenharmony_ci	runtime PM disabled.
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	This feature also can be controlled by device drivers by using the
35662306a36Sopenharmony_ci	``DPM_FLAG_NO_DIRECT_COMPLETE`` and ``DPM_FLAG_SMART_PREPARE`` driver
35762306a36Sopenharmony_ci	power management flags.  [Typically, they are set at the time the driver
35862306a36Sopenharmony_ci	is probed against the device in question by passing them to the
35962306a36Sopenharmony_ci	:c:func:`dev_pm_set_driver_flags` helper function.]  If the first of
36062306a36Sopenharmony_ci	these flags is set, the PM core will not apply the direct-complete
36162306a36Sopenharmony_ci	procedure described above to the given device and, consequenty, to any
36262306a36Sopenharmony_ci	of its ancestors.  The second flag, when set, informs the middle layer
36362306a36Sopenharmony_ci	code (bus types, device types, PM domains, classes) that it should take
36462306a36Sopenharmony_ci	the return value of the ``->prepare`` callback provided by the driver
36562306a36Sopenharmony_ci	into account and it may only return a positive value from its own
36662306a36Sopenharmony_ci	``->prepare`` callback if the driver's one also has returned a positive
36762306a36Sopenharmony_ci	value.
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci    2.	The ``->suspend`` methods should quiesce the device to stop it from
37062306a36Sopenharmony_ci	performing I/O.  They also may save the device registers and put it into
37162306a36Sopenharmony_ci	the appropriate low-power state, depending on the bus type the device is
37262306a36Sopenharmony_ci	on, and they may enable wakeup events.
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	However, for devices supporting runtime power management, the
37562306a36Sopenharmony_ci	``->suspend`` methods provided by subsystems (bus types and PM domains
37662306a36Sopenharmony_ci	in particular) must follow an additional rule regarding what can be done
37762306a36Sopenharmony_ci	to the devices before their drivers' ``->suspend`` methods are called.
37862306a36Sopenharmony_ci	Namely, they may resume the devices from runtime suspend by
37962306a36Sopenharmony_ci	calling :c:func:`pm_runtime_resume` for them, if that is necessary, but
38062306a36Sopenharmony_ci	they must not update the state of the devices in any other way at that
38162306a36Sopenharmony_ci	time (in case the drivers need to resume the devices from runtime
38262306a36Sopenharmony_ci	suspend in their ``->suspend`` methods).  In fact, the PM core prevents
38362306a36Sopenharmony_ci	subsystems or drivers from putting devices into runtime suspend at
38462306a36Sopenharmony_ci	these times by calling :c:func:`pm_runtime_get_noresume` before issuing
38562306a36Sopenharmony_ci	the ``->prepare`` callback (and calling :c:func:`pm_runtime_put` after
38662306a36Sopenharmony_ci	issuing the ``->complete`` callback).
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci    3.	For a number of devices it is convenient to split suspend into the
38962306a36Sopenharmony_ci	"quiesce device" and "save device state" phases, in which cases
39062306a36Sopenharmony_ci	``suspend_late`` is meant to do the latter.  It is always executed after
39162306a36Sopenharmony_ci	runtime power management has been disabled for the device in question.
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci    4.	The ``suspend_noirq`` phase occurs after IRQ handlers have been disabled,
39462306a36Sopenharmony_ci	which means that the driver's interrupt handler will not be called while
39562306a36Sopenharmony_ci	the callback method is running.  The ``->suspend_noirq`` methods should
39662306a36Sopenharmony_ci	save the values of the device's registers that weren't saved previously
39762306a36Sopenharmony_ci	and finally put the device into the appropriate low-power state.
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	The majority of subsystems and device drivers need not implement this
40062306a36Sopenharmony_ci	callback.  However, bus types allowing devices to share interrupt
40162306a36Sopenharmony_ci	vectors, like PCI, generally need it; otherwise a driver might encounter
40262306a36Sopenharmony_ci	an error during the suspend phase by fielding a shared interrupt
40362306a36Sopenharmony_ci	generated by some other device after its own device had been set to low
40462306a36Sopenharmony_ci	power.
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ciAt the end of these phases, drivers should have stopped all I/O transactions
40762306a36Sopenharmony_ci(DMA, IRQs), saved enough state that they can re-initialize or restore previous
40862306a36Sopenharmony_cistate (as needed by the hardware), and placed the device into a low-power state.
40962306a36Sopenharmony_ciOn many platforms they will gate off one or more clock sources; sometimes they
41062306a36Sopenharmony_ciwill also switch off power supplies or reduce voltages.  [Drivers supporting
41162306a36Sopenharmony_ciruntime PM may already have performed some or all of these steps.]
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ciIf :c:func:`device_may_wakeup()` returns ``true``, the device should be
41462306a36Sopenharmony_ciprepared for generating hardware wakeup signals to trigger a system wakeup event
41562306a36Sopenharmony_ciwhen the system is in the sleep state.  For example, :c:func:`enable_irq_wake()`
41662306a36Sopenharmony_cimight identify GPIO signals hooked up to a switch or other external hardware,
41762306a36Sopenharmony_ciand :c:func:`pci_enable_wake()` does something similar for the PCI PME signal.
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ciIf any of these callbacks returns an error, the system won't enter the desired
42062306a36Sopenharmony_cilow-power state.  Instead, the PM core will unwind its actions by resuming all
42162306a36Sopenharmony_cithe devices that were suspended.
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ciLeaving System Suspend
42562306a36Sopenharmony_ci----------------------
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ciWhen resuming from freeze, standby or memory sleep, the phases are:
42862306a36Sopenharmony_ci``resume_noirq``, ``resume_early``, ``resume``, ``complete``.
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci    1.	The ``->resume_noirq`` callback methods should perform any actions
43162306a36Sopenharmony_ci	needed before the driver's interrupt handlers are invoked.  This
43262306a36Sopenharmony_ci	generally means undoing the actions of the ``suspend_noirq`` phase.  If
43362306a36Sopenharmony_ci	the bus type permits devices to share interrupt vectors, like PCI, the
43462306a36Sopenharmony_ci	method should bring the device and its driver into a state in which the
43562306a36Sopenharmony_ci	driver can recognize if the device is the source of incoming interrupts,
43662306a36Sopenharmony_ci	if any, and handle them correctly.
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	For example, the PCI bus type's ``->pm.resume_noirq()`` puts the device
43962306a36Sopenharmony_ci	into the full-power state (D0 in the PCI terminology) and restores the
44062306a36Sopenharmony_ci	standard configuration registers of the device.  Then it calls the
44162306a36Sopenharmony_ci	device driver's ``->pm.resume_noirq()`` method to perform device-specific
44262306a36Sopenharmony_ci	actions.
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci    2.	The ``->resume_early`` methods should prepare devices for the execution
44562306a36Sopenharmony_ci	of the resume methods.  This generally involves undoing the actions of
44662306a36Sopenharmony_ci	the preceding ``suspend_late`` phase.
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci    3.	The ``->resume`` methods should bring the device back to its operating
44962306a36Sopenharmony_ci	state, so that it can perform normal I/O.  This generally involves
45062306a36Sopenharmony_ci	undoing the actions of the ``suspend`` phase.
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci    4.	The ``complete`` phase should undo the actions of the ``prepare`` phase.
45362306a36Sopenharmony_ci        For this reason, unlike the other resume-related phases, during the
45462306a36Sopenharmony_ci        ``complete`` phase the device hierarchy is traversed bottom-up.
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	Note, however, that new children may be registered below the device as
45762306a36Sopenharmony_ci	soon as the ``->resume`` callbacks occur; it's not necessary to wait
45862306a36Sopenharmony_ci	until the ``complete`` phase runs.
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	Moreover, if the preceding ``->prepare`` callback returned a positive
46162306a36Sopenharmony_ci	number, the device may have been left in runtime suspend throughout the
46262306a36Sopenharmony_ci	whole system suspend and resume (its ``->suspend``, ``->suspend_late``,
46362306a36Sopenharmony_ci	``->suspend_noirq``, ``->resume_noirq``,
46462306a36Sopenharmony_ci	``->resume_early``, and ``->resume`` callbacks may have been
46562306a36Sopenharmony_ci	skipped).  In that case, the ``->complete`` callback is entirely
46662306a36Sopenharmony_ci	responsible for putting the device into a consistent state after system
46762306a36Sopenharmony_ci	suspend if necessary.  [For example, it may need to queue up a runtime
46862306a36Sopenharmony_ci	resume request for the device for this purpose.]  To check if that is
46962306a36Sopenharmony_ci	the case, the ``->complete`` callback can consult the device's
47062306a36Sopenharmony_ci	``power.direct_complete`` flag.  If that flag is set when the
47162306a36Sopenharmony_ci	``->complete`` callback is being run then the direct-complete mechanism
47262306a36Sopenharmony_ci	was used, and special actions may be required to make the device work
47362306a36Sopenharmony_ci	correctly afterward.
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ciAt the end of these phases, drivers should be as functional as they were before
47662306a36Sopenharmony_cisuspending: I/O can be performed using DMA and IRQs, and the relevant clocks are
47762306a36Sopenharmony_cigated on.
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ciHowever, the details here may again be platform-specific.  For example,
48062306a36Sopenharmony_cisome systems support multiple "run" states, and the mode in effect at
48162306a36Sopenharmony_cithe end of resume might not be the one which preceded suspension.
48262306a36Sopenharmony_ciThat means availability of certain clocks or power supplies changed,
48362306a36Sopenharmony_ciwhich could easily affect how a driver works.
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ciDrivers need to be able to handle hardware which has been reset since all of the
48662306a36Sopenharmony_cisuspend methods were called, for example by complete reinitialization.
48762306a36Sopenharmony_ciThis may be the hardest part, and the one most protected by NDA'd documents
48862306a36Sopenharmony_ciand chip errata.  It's simplest if the hardware state hasn't changed since
48962306a36Sopenharmony_cithe suspend was carried out, but that can only be guaranteed if the target
49062306a36Sopenharmony_cisystem sleep entered was suspend-to-idle.  For the other system sleep states
49162306a36Sopenharmony_cithat may not be the case (and usually isn't for ACPI-defined system sleep
49262306a36Sopenharmony_cistates, like S3).
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ciDrivers must also be prepared to notice that the device has been removed
49562306a36Sopenharmony_ciwhile the system was powered down, whenever that's physically possible.
49662306a36Sopenharmony_ciPCMCIA, MMC, USB, Firewire, SCSI, and even IDE are common examples of busses
49762306a36Sopenharmony_ciwhere common Linux platforms will see such removal.  Details of how drivers
49862306a36Sopenharmony_ciwill notice and handle such removals are currently bus-specific, and often
49962306a36Sopenharmony_ciinvolve a separate thread.
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ciThese callbacks may return an error value, but the PM core will ignore such
50262306a36Sopenharmony_cierrors since there's nothing it can do about them other than printing them in
50362306a36Sopenharmony_cithe system log.
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ciEntering Hibernation
50762306a36Sopenharmony_ci--------------------
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ciHibernating the system is more complicated than putting it into sleep states,
51062306a36Sopenharmony_cibecause it involves creating and saving a system image.  Therefore there are
51162306a36Sopenharmony_cimore phases for hibernation, with a different set of callbacks.  These phases
51262306a36Sopenharmony_cialways run after tasks have been frozen and enough memory has been freed.
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ciThe general procedure for hibernation is to quiesce all devices ("freeze"),
51562306a36Sopenharmony_cicreate an image of the system memory while everything is stable, reactivate all
51662306a36Sopenharmony_cidevices ("thaw"), write the image to permanent storage, and finally shut down
51762306a36Sopenharmony_cithe system ("power off").  The phases used to accomplish this are: ``prepare``,
51862306a36Sopenharmony_ci``freeze``, ``freeze_late``, ``freeze_noirq``, ``thaw_noirq``, ``thaw_early``,
51962306a36Sopenharmony_ci``thaw``, ``complete``, ``prepare``, ``poweroff``, ``poweroff_late``,
52062306a36Sopenharmony_ci``poweroff_noirq``.
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci    1.	The ``prepare`` phase is discussed in the "Entering System Suspend"
52362306a36Sopenharmony_ci	section above.
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci    2.	The ``->freeze`` methods should quiesce the device so that it doesn't
52662306a36Sopenharmony_ci	generate IRQs or DMA, and they may need to save the values of device
52762306a36Sopenharmony_ci	registers.  However the device does not have to be put in a low-power
52862306a36Sopenharmony_ci	state, and to save time it's best not to do so.  Also, the device should
52962306a36Sopenharmony_ci	not be prepared to generate wakeup events.
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci    3.	The ``freeze_late`` phase is analogous to the ``suspend_late`` phase
53262306a36Sopenharmony_ci	described earlier, except that the device should not be put into a
53362306a36Sopenharmony_ci	low-power state and should not be allowed to generate wakeup events.
53462306a36Sopenharmony_ci
53562306a36Sopenharmony_ci    4.	The ``freeze_noirq`` phase is analogous to the ``suspend_noirq`` phase
53662306a36Sopenharmony_ci	discussed earlier, except again that the device should not be put into
53762306a36Sopenharmony_ci	a low-power state and should not be allowed to generate wakeup events.
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ciAt this point the system image is created.  All devices should be inactive and
54062306a36Sopenharmony_cithe contents of memory should remain undisturbed while this happens, so that the
54162306a36Sopenharmony_ciimage forms an atomic snapshot of the system state.
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci    5.	The ``thaw_noirq`` phase is analogous to the ``resume_noirq`` phase
54462306a36Sopenharmony_ci	discussed earlier.  The main difference is that its methods can assume
54562306a36Sopenharmony_ci	the device is in the same state as at the end of the ``freeze_noirq``
54662306a36Sopenharmony_ci	phase.
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci    6.	The ``thaw_early`` phase is analogous to the ``resume_early`` phase
54962306a36Sopenharmony_ci	described above.  Its methods should undo the actions of the preceding
55062306a36Sopenharmony_ci	``freeze_late``, if necessary.
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_ci    7.	The ``thaw`` phase is analogous to the ``resume`` phase discussed
55362306a36Sopenharmony_ci	earlier.  Its methods should bring the device back to an operating
55462306a36Sopenharmony_ci	state, so that it can be used for saving the image if necessary.
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci    8.	The ``complete`` phase is discussed in the "Leaving System Suspend"
55762306a36Sopenharmony_ci	section above.
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ciAt this point the system image is saved, and the devices then need to be
56062306a36Sopenharmony_ciprepared for the upcoming system shutdown.  This is much like suspending them
56162306a36Sopenharmony_cibefore putting the system into the suspend-to-idle, shallow or deep sleep state,
56262306a36Sopenharmony_ciand the phases are similar.
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci    9.	The ``prepare`` phase is discussed above.
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ci    10.	The ``poweroff`` phase is analogous to the ``suspend`` phase.
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci    11.	The ``poweroff_late`` phase is analogous to the ``suspend_late`` phase.
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci    12.	The ``poweroff_noirq`` phase is analogous to the ``suspend_noirq`` phase.
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ciThe ``->poweroff``, ``->poweroff_late`` and ``->poweroff_noirq`` callbacks
57362306a36Sopenharmony_cishould do essentially the same things as the ``->suspend``, ``->suspend_late``
57462306a36Sopenharmony_ciand ``->suspend_noirq`` callbacks, respectively.  A notable difference is
57562306a36Sopenharmony_cithat they need not store the device register values, because the registers
57662306a36Sopenharmony_cishould already have been stored during the ``freeze``, ``freeze_late`` or
57762306a36Sopenharmony_ci``freeze_noirq`` phases.  Also, on many machines the firmware will power-down
57862306a36Sopenharmony_cithe entire system, so it is not necessary for the callback to put the device in
57962306a36Sopenharmony_cia low-power state.
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_ciLeaving Hibernation
58362306a36Sopenharmony_ci-------------------
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ciResuming from hibernation is, again, more complicated than resuming from a sleep
58662306a36Sopenharmony_cistate in which the contents of main memory are preserved, because it requires
58762306a36Sopenharmony_cia system image to be loaded into memory and the pre-hibernation memory contents
58862306a36Sopenharmony_cito be restored before control can be passed back to the image kernel.
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ciAlthough in principle the image might be loaded into memory and the
59162306a36Sopenharmony_cipre-hibernation memory contents restored by the boot loader, in practice this
59262306a36Sopenharmony_cican't be done because boot loaders aren't smart enough and there is no
59362306a36Sopenharmony_ciestablished protocol for passing the necessary information.  So instead, the
59462306a36Sopenharmony_ciboot loader loads a fresh instance of the kernel, called "the restore kernel",
59562306a36Sopenharmony_ciinto memory and passes control to it in the usual way.  Then the restore kernel
59662306a36Sopenharmony_cireads the system image, restores the pre-hibernation memory contents, and passes
59762306a36Sopenharmony_cicontrol to the image kernel.  Thus two different kernel instances are involved
59862306a36Sopenharmony_ciin resuming from hibernation.  In fact, the restore kernel may be completely
59962306a36Sopenharmony_cidifferent from the image kernel: a different configuration and even a different
60062306a36Sopenharmony_civersion.  This has important consequences for device drivers and their
60162306a36Sopenharmony_cisubsystems.
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ciTo be able to load the system image into memory, the restore kernel needs to
60462306a36Sopenharmony_ciinclude at least a subset of device drivers allowing it to access the storage
60562306a36Sopenharmony_cimedium containing the image, although it doesn't need to include all of the
60662306a36Sopenharmony_cidrivers present in the image kernel.  After the image has been loaded, the
60762306a36Sopenharmony_cidevices managed by the boot kernel need to be prepared for passing control back
60862306a36Sopenharmony_cito the image kernel.  This is very similar to the initial steps involved in
60962306a36Sopenharmony_cicreating a system image, and it is accomplished in the same way, using
61062306a36Sopenharmony_ci``prepare``, ``freeze``, and ``freeze_noirq`` phases.  However, the devices
61162306a36Sopenharmony_ciaffected by these phases are only those having drivers in the restore kernel;
61262306a36Sopenharmony_ciother devices will still be in whatever state the boot loader left them.
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ciShould the restoration of the pre-hibernation memory contents fail, the restore
61562306a36Sopenharmony_cikernel would go through the "thawing" procedure described above, using the
61662306a36Sopenharmony_ci``thaw_noirq``, ``thaw_early``, ``thaw``, and ``complete`` phases, and then
61762306a36Sopenharmony_cicontinue running normally.  This happens only rarely.  Most often the
61862306a36Sopenharmony_cipre-hibernation memory contents are restored successfully and control is passed
61962306a36Sopenharmony_cito the image kernel, which then becomes responsible for bringing the system back
62062306a36Sopenharmony_cito the working state.
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ciTo achieve this, the image kernel must restore the devices' pre-hibernation
62362306a36Sopenharmony_cifunctionality.  The operation is much like waking up from a sleep state (with
62462306a36Sopenharmony_cithe memory contents preserved), although it involves different phases:
62562306a36Sopenharmony_ci``restore_noirq``, ``restore_early``, ``restore``, ``complete``.
62662306a36Sopenharmony_ci
62762306a36Sopenharmony_ci    1.	The ``restore_noirq`` phase is analogous to the ``resume_noirq`` phase.
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci    2.	The ``restore_early`` phase is analogous to the ``resume_early`` phase.
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci    3.	The ``restore`` phase is analogous to the ``resume`` phase.
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci    4.	The ``complete`` phase is discussed above.
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ciThe main difference from ``resume[_early|_noirq]`` is that
63662306a36Sopenharmony_ci``restore[_early|_noirq]`` must assume the device has been accessed and
63762306a36Sopenharmony_cireconfigured by the boot loader or the restore kernel.  Consequently, the state
63862306a36Sopenharmony_ciof the device may be different from the state remembered from the ``freeze``,
63962306a36Sopenharmony_ci``freeze_late`` and ``freeze_noirq`` phases.  The device may even need to be
64062306a36Sopenharmony_cireset and completely re-initialized.  In many cases this difference doesn't
64162306a36Sopenharmony_cimatter, so the ``->resume[_early|_noirq]`` and ``->restore[_early|_norq]``
64262306a36Sopenharmony_cimethod pointers can be set to the same routines.  Nevertheless, different
64362306a36Sopenharmony_cicallback pointers are used in case there is a situation where it actually does
64462306a36Sopenharmony_cimatter.
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ciPower Management Notifiers
64862306a36Sopenharmony_ci==========================
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_ciThere are some operations that cannot be carried out by the power management
65162306a36Sopenharmony_cicallbacks discussed above, because the callbacks occur too late or too early.
65262306a36Sopenharmony_ciTo handle these cases, subsystems and device drivers may register power
65362306a36Sopenharmony_cimanagement notifiers that are called before tasks are frozen and after they have
65462306a36Sopenharmony_cibeen thawed.  Generally speaking, the PM notifiers are suitable for performing
65562306a36Sopenharmony_ciactions that either require user space to be available, or at least won't
65662306a36Sopenharmony_ciinterfere with user space.
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ciFor details refer to Documentation/driver-api/pm/notifiers.rst.
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ciDevice Low-Power (suspend) States
66262306a36Sopenharmony_ci=================================
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ciDevice low-power states aren't standard.  One device might only handle
66562306a36Sopenharmony_ci"on" and "off", while another might support a dozen different versions of
66662306a36Sopenharmony_ci"on" (how many engines are active?), plus a state that gets back to "on"
66762306a36Sopenharmony_cifaster than from a full "off".
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ciSome buses define rules about what different suspend states mean.  PCI
67062306a36Sopenharmony_cigives one example: after the suspend sequence completes, a non-legacy
67162306a36Sopenharmony_ciPCI device may not perform DMA or issue IRQs, and any wakeup events it
67262306a36Sopenharmony_ciissues would be issued through the PME# bus signal.  Plus, there are
67362306a36Sopenharmony_ciseveral PCI-standard device states, some of which are optional.
67462306a36Sopenharmony_ci
67562306a36Sopenharmony_ciIn contrast, integrated system-on-chip processors often use IRQs as the
67662306a36Sopenharmony_ciwakeup event sources (so drivers would call :c:func:`enable_irq_wake`) and
67762306a36Sopenharmony_cimight be able to treat DMA completion as a wakeup event (sometimes DMA can stay
67862306a36Sopenharmony_ciactive too, it'd only be the CPU and some peripherals that sleep).
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ciSome details here may be platform-specific.  Systems may have devices that
68162306a36Sopenharmony_cican be fully active in certain sleep states, such as an LCD display that's
68262306a36Sopenharmony_cirefreshed using DMA while most of the system is sleeping lightly ... and
68362306a36Sopenharmony_ciits frame buffer might even be updated by a DSP or other non-Linux CPU while
68462306a36Sopenharmony_cithe Linux control processor stays idle.
68562306a36Sopenharmony_ci
68662306a36Sopenharmony_ciMoreover, the specific actions taken may depend on the target system state.
68762306a36Sopenharmony_ciOne target system state might allow a given device to be very operational;
68862306a36Sopenharmony_cianother might require a hard shut down with re-initialization on resume.
68962306a36Sopenharmony_ciAnd two different target systems might use the same device in different
69062306a36Sopenharmony_ciways; the aforementioned LCD might be active in one product's "standby",
69162306a36Sopenharmony_cibut a different product using the same SOC might work differently.
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_ciDevice Power Management Domains
69562306a36Sopenharmony_ci===============================
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ciSometimes devices share reference clocks or other power resources.  In those
69862306a36Sopenharmony_cicases it generally is not possible to put devices into low-power states
69962306a36Sopenharmony_ciindividually.  Instead, a set of devices sharing a power resource can be put
70062306a36Sopenharmony_ciinto a low-power state together at the same time by turning off the shared
70162306a36Sopenharmony_cipower resource.  Of course, they also need to be put into the full-power state
70262306a36Sopenharmony_citogether, by turning the shared power resource on.  A set of devices with this
70362306a36Sopenharmony_ciproperty is often referred to as a power domain. A power domain may also be
70462306a36Sopenharmony_cinested inside another power domain. The nested domain is referred to as the
70562306a36Sopenharmony_cisub-domain of the parent domain.
70662306a36Sopenharmony_ci
70762306a36Sopenharmony_ciSupport for power domains is provided through the :c:member:`pm_domain` field of
70862306a36Sopenharmony_cistruct device.  This field is a pointer to an object of type
70962306a36Sopenharmony_cistruct dev_pm_domain, defined in :file:`include/linux/pm.h`, providing a set
71062306a36Sopenharmony_ciof power management callbacks analogous to the subsystem-level and device driver
71162306a36Sopenharmony_cicallbacks that are executed for the given device during all power transitions,
71262306a36Sopenharmony_ciinstead of the respective subsystem-level callbacks.  Specifically, if a
71362306a36Sopenharmony_cidevice's :c:member:`pm_domain` pointer is not NULL, the ``->suspend()`` callback
71462306a36Sopenharmony_cifrom the object pointed to by it will be executed instead of its subsystem's
71562306a36Sopenharmony_ci(e.g. bus type's) ``->suspend()`` callback and analogously for all of the
71662306a36Sopenharmony_ciremaining callbacks.  In other words, power management domain callbacks, if
71762306a36Sopenharmony_cidefined for the given device, always take precedence over the callbacks provided
71862306a36Sopenharmony_ciby the device's subsystem (e.g. bus type).
71962306a36Sopenharmony_ci
72062306a36Sopenharmony_ciThe support for device power management domains is only relevant to platforms
72162306a36Sopenharmony_cineeding to use the same device driver power management callbacks in many
72262306a36Sopenharmony_cidifferent power domain configurations and wanting to avoid incorporating the
72362306a36Sopenharmony_cisupport for power domains into subsystem-level callbacks, for example by
72462306a36Sopenharmony_cimodifying the platform bus type.  Other platforms need not implement it or take
72562306a36Sopenharmony_ciit into account in any way.
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ciDevices may be defined as IRQ-safe which indicates to the PM core that their
72862306a36Sopenharmony_ciruntime PM callbacks may be invoked with disabled interrupts (see
72962306a36Sopenharmony_ciDocumentation/power/runtime_pm.rst for more information).  If an
73062306a36Sopenharmony_ciIRQ-safe device belongs to a PM domain, the runtime PM of the domain will be
73162306a36Sopenharmony_cidisallowed, unless the domain itself is defined as IRQ-safe. However, it
73262306a36Sopenharmony_cimakes sense to define a PM domain as IRQ-safe only if all the devices in it
73362306a36Sopenharmony_ciare IRQ-safe. Moreover, if an IRQ-safe domain has a parent domain, the runtime
73462306a36Sopenharmony_ciPM of the parent is only allowed if the parent itself is IRQ-safe too with the
73562306a36Sopenharmony_ciadditional restriction that all child domains of an IRQ-safe parent must also
73662306a36Sopenharmony_cibe IRQ-safe.
73762306a36Sopenharmony_ci
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ciRuntime Power Management
74062306a36Sopenharmony_ci========================
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ciMany devices are able to dynamically power down while the system is still
74362306a36Sopenharmony_cirunning. This feature is useful for devices that are not being used, and
74462306a36Sopenharmony_cican offer significant power savings on a running system.  These devices
74562306a36Sopenharmony_cioften support a range of runtime power states, which might use names such
74662306a36Sopenharmony_cias "off", "sleep", "idle", "active", and so on.  Those states will in some
74762306a36Sopenharmony_cicases (like PCI) be partially constrained by the bus the device uses, and will
74862306a36Sopenharmony_ciusually include hardware states that are also used in system sleep states.
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ciA system-wide power transition can be started while some devices are in low
75162306a36Sopenharmony_cipower states due to runtime power management.  The system sleep PM callbacks
75262306a36Sopenharmony_cishould recognize such situations and react to them appropriately, but the
75362306a36Sopenharmony_cinecessary actions are subsystem-specific.
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ciIn some cases the decision may be made at the subsystem level while in other
75662306a36Sopenharmony_cicases the device driver may be left to decide.  In some cases it may be
75762306a36Sopenharmony_cidesirable to leave a suspended device in that state during a system-wide power
75862306a36Sopenharmony_citransition, but in other cases the device must be put back into the full-power
75962306a36Sopenharmony_cistate temporarily, for example so that its system wakeup capability can be
76062306a36Sopenharmony_cidisabled.  This all depends on the hardware and the design of the subsystem and
76162306a36Sopenharmony_cidevice driver in question.
76262306a36Sopenharmony_ci
76362306a36Sopenharmony_ciIf it is necessary to resume a device from runtime suspend during a system-wide
76462306a36Sopenharmony_citransition into a sleep state, that can be done by calling
76562306a36Sopenharmony_ci:c:func:`pm_runtime_resume` from the ``->suspend`` callback (or the ``->freeze``
76662306a36Sopenharmony_cior ``->poweroff`` callback for transitions related to hibernation) of either the
76762306a36Sopenharmony_cidevice's driver or its subsystem (for example, a bus type or a PM domain).
76862306a36Sopenharmony_ciHowever, subsystems must not otherwise change the runtime status of devices
76962306a36Sopenharmony_cifrom their ``->prepare`` and ``->suspend`` callbacks (or equivalent) *before*
77062306a36Sopenharmony_ciinvoking device drivers' ``->suspend`` callbacks (or equivalent).
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci.. _smart_suspend_flag:
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ciThe ``DPM_FLAG_SMART_SUSPEND`` Driver Flag
77562306a36Sopenharmony_ci------------------------------------------
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ciSome bus types and PM domains have a policy to resume all devices from runtime
77862306a36Sopenharmony_cisuspend upfront in their ``->suspend`` callbacks, but that may not be really
77962306a36Sopenharmony_cinecessary if the device's driver can cope with runtime-suspended devices.
78062306a36Sopenharmony_ciThe driver can indicate this by setting ``DPM_FLAG_SMART_SUSPEND`` in
78162306a36Sopenharmony_ci:c:member:`power.driver_flags` at probe time, with the assistance of the
78262306a36Sopenharmony_ci:c:func:`dev_pm_set_driver_flags` helper routine.
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_ciSetting that flag causes the PM core and middle-layer code
78562306a36Sopenharmony_ci(bus types, PM domains etc.) to skip the ``->suspend_late`` and
78662306a36Sopenharmony_ci``->suspend_noirq`` callbacks provided by the driver if the device remains in
78762306a36Sopenharmony_ciruntime suspend throughout those phases of the system-wide suspend (and
78862306a36Sopenharmony_cisimilarly for the "freeze" and "poweroff" parts of system hibernation).
78962306a36Sopenharmony_ci[Otherwise the same driver
79062306a36Sopenharmony_cicallback might be executed twice in a row for the same device, which would not
79162306a36Sopenharmony_cibe valid in general.]  If the middle-layer system-wide PM callbacks are present
79262306a36Sopenharmony_cifor the device then they are responsible for skipping these driver callbacks;
79362306a36Sopenharmony_ciif not then the PM core skips them.  The subsystem callback routines can
79462306a36Sopenharmony_cidetermine whether they need to skip the driver callbacks by testing the return
79562306a36Sopenharmony_civalue from the :c:func:`dev_pm_skip_suspend` helper function.
79662306a36Sopenharmony_ci
79762306a36Sopenharmony_ciIn addition, with ``DPM_FLAG_SMART_SUSPEND`` set, the driver's ``->thaw_noirq``
79862306a36Sopenharmony_ciand ``->thaw_early`` callbacks are skipped in hibernation if the device remained
79962306a36Sopenharmony_ciin runtime suspend throughout the preceding "freeze" transition.  Again, if the
80062306a36Sopenharmony_cimiddle-layer callbacks are present for the device, they are responsible for
80162306a36Sopenharmony_cidoing this, otherwise the PM core takes care of it.
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci
80462306a36Sopenharmony_ciThe ``DPM_FLAG_MAY_SKIP_RESUME`` Driver Flag
80562306a36Sopenharmony_ci--------------------------------------------
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ciDuring system-wide resume from a sleep state it's easiest to put devices into
80862306a36Sopenharmony_cithe full-power state, as explained in Documentation/power/runtime_pm.rst.
80962306a36Sopenharmony_ci[Refer to that document for more information regarding this particular issue as
81062306a36Sopenharmony_ciwell as for information on the device runtime power management framework in
81162306a36Sopenharmony_cigeneral.]  However, it often is desirable to leave devices in suspend after
81262306a36Sopenharmony_cisystem transitions to the working state, especially if those devices had been in
81362306a36Sopenharmony_ciruntime suspend before the preceding system-wide suspend (or analogous)
81462306a36Sopenharmony_citransition.
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ciTo that end, device drivers can use the ``DPM_FLAG_MAY_SKIP_RESUME`` flag to
81762306a36Sopenharmony_ciindicate to the PM core and middle-layer code that they allow their "noirq" and
81862306a36Sopenharmony_ci"early" resume callbacks to be skipped if the device can be left in suspend
81962306a36Sopenharmony_ciafter system-wide PM transitions to the working state.  Whether or not that is
82062306a36Sopenharmony_cithe case generally depends on the state of the device before the given system
82162306a36Sopenharmony_cisuspend-resume cycle and on the type of the system transition under way.
82262306a36Sopenharmony_ciIn particular, the "thaw" and "restore" transitions related to hibernation are
82362306a36Sopenharmony_cinot affected by ``DPM_FLAG_MAY_SKIP_RESUME`` at all.  [All callbacks are
82462306a36Sopenharmony_ciissued during the "restore" transition regardless of the flag settings,
82562306a36Sopenharmony_ciand whether or not any driver callbacks
82662306a36Sopenharmony_ciare skipped during the "thaw" transition depends whether or not the
82762306a36Sopenharmony_ci``DPM_FLAG_SMART_SUSPEND`` flag is set (see `above <smart_suspend_flag_>`_).
82862306a36Sopenharmony_ciIn addition, a device is not allowed to remain in runtime suspend if any of its
82962306a36Sopenharmony_cichildren will be returned to full power.]
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ciThe ``DPM_FLAG_MAY_SKIP_RESUME`` flag is taken into account in combination with
83262306a36Sopenharmony_cithe :c:member:`power.may_skip_resume` status bit set by the PM core during the
83362306a36Sopenharmony_ci"suspend" phase of suspend-type transitions.  If the driver or the middle layer
83462306a36Sopenharmony_cihas a reason to prevent the driver's "noirq" and "early" resume callbacks from
83562306a36Sopenharmony_cibeing skipped during the subsequent system resume transition, it should
83662306a36Sopenharmony_ciclear :c:member:`power.may_skip_resume` in its ``->suspend``, ``->suspend_late``
83762306a36Sopenharmony_cior ``->suspend_noirq`` callback.  [Note that the drivers setting
83862306a36Sopenharmony_ci``DPM_FLAG_SMART_SUSPEND`` need to clear :c:member:`power.may_skip_resume` in
83962306a36Sopenharmony_citheir ``->suspend`` callback in case the other two are skipped.]
84062306a36Sopenharmony_ci
84162306a36Sopenharmony_ciSetting the :c:member:`power.may_skip_resume` status bit along with the
84262306a36Sopenharmony_ci``DPM_FLAG_MAY_SKIP_RESUME`` flag is necessary, but generally not sufficient,
84362306a36Sopenharmony_cifor the driver's "noirq" and "early" resume callbacks to be skipped.  Whether or
84462306a36Sopenharmony_cinot they should be skipped can be determined by evaluating the
84562306a36Sopenharmony_ci:c:func:`dev_pm_skip_resume` helper function.
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ciIf that function returns ``true``, the driver's "noirq" and "early" resume
84862306a36Sopenharmony_cicallbacks should be skipped and the device's runtime PM status will be set to
84962306a36Sopenharmony_ci"suspended" by the PM core.  Otherwise, if the device was runtime-suspended
85062306a36Sopenharmony_ciduring the preceding system-wide suspend transition and its
85162306a36Sopenharmony_ci``DPM_FLAG_SMART_SUSPEND`` is set, its runtime PM status will be set to
85262306a36Sopenharmony_ci"active" by the PM core.  [Hence, the drivers that do not set
85362306a36Sopenharmony_ci``DPM_FLAG_SMART_SUSPEND`` should not expect the runtime PM status of their
85462306a36Sopenharmony_cidevices to be changed from "suspended" to "active" by the PM core during
85562306a36Sopenharmony_cisystem-wide resume-type transitions.]
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_ciIf the ``DPM_FLAG_MAY_SKIP_RESUME`` flag is not set for a device, but
85862306a36Sopenharmony_ci``DPM_FLAG_SMART_SUSPEND`` is set and the driver's "late" and "noirq" suspend
85962306a36Sopenharmony_cicallbacks are skipped, its system-wide "noirq" and "early" resume callbacks, if
86062306a36Sopenharmony_cipresent, are invoked as usual and the device's runtime PM status is set to
86162306a36Sopenharmony_ci"active" by the PM core before enabling runtime PM for it.  In that case, the
86262306a36Sopenharmony_cidriver must be prepared to cope with the invocation of its system-wide resume
86362306a36Sopenharmony_cicallbacks back-to-back with its ``->runtime_suspend`` one (without the
86462306a36Sopenharmony_ciintervening ``->runtime_resume`` and system-wide suspend callbacks) and the
86562306a36Sopenharmony_cifinal state of the device must reflect the "active" runtime PM status in that
86662306a36Sopenharmony_cicase.  [Note that this is not a problem at all if the driver's
86762306a36Sopenharmony_ci``->suspend_late`` callback pointer points to the same function as its
86862306a36Sopenharmony_ci``->runtime_suspend`` one and its ``->resume_early`` callback pointer points to
86962306a36Sopenharmony_cithe same function as the ``->runtime_resume`` one, while none of the other
87062306a36Sopenharmony_cisystem-wide suspend-resume callbacks of the driver are present, for example.]
87162306a36Sopenharmony_ci
87262306a36Sopenharmony_ciLikewise, if ``DPM_FLAG_MAY_SKIP_RESUME`` is set for a device, its driver's
87362306a36Sopenharmony_cisystem-wide "noirq" and "early" resume callbacks may be skipped while its "late"
87462306a36Sopenharmony_ciand "noirq" suspend callbacks may have been executed (in principle, regardless
87562306a36Sopenharmony_ciof whether or not ``DPM_FLAG_SMART_SUSPEND`` is set).  In that case, the driver
87662306a36Sopenharmony_cineeds to be able to cope with the invocation of its ``->runtime_resume``
87762306a36Sopenharmony_cicallback back-to-back with its "late" and "noirq" suspend ones.  [For instance,
87862306a36Sopenharmony_cithat is not a concern if the driver sets both ``DPM_FLAG_SMART_SUSPEND`` and
87962306a36Sopenharmony_ci``DPM_FLAG_MAY_SKIP_RESUME`` and uses the same pair of suspend/resume callback
88062306a36Sopenharmony_cifunctions for runtime PM and system-wide suspend/resume.]
881