Back to home page

OSCL-LXR

 
 

    


0001 .. _device_link:
0002 
0003 ============
0004 Device links
0005 ============
0006 
0007 By default, the driver core only enforces dependencies between devices
0008 that are borne out of a parent/child relationship within the device
0009 hierarchy: When suspending, resuming or shutting down the system, devices
0010 are ordered based on this relationship, i.e. children are always suspended
0011 before their parent, and the parent is always resumed before its children.
0012 
0013 Sometimes there is a need to represent device dependencies beyond the
0014 mere parent/child relationship, e.g. between siblings, and have the
0015 driver core automatically take care of them.
0016 
0017 Secondly, the driver core by default does not enforce any driver presence
0018 dependencies, i.e. that one device must be bound to a driver before
0019 another one can probe or function correctly.
0020 
0021 Often these two dependency types come together, so a device depends on
0022 another one both with regards to driver presence *and* with regards to
0023 suspend/resume and shutdown ordering.
0024 
0025 Device links allow representation of such dependencies in the driver core.
0026 
0027 In its standard or *managed* form, a device link combines *both* dependency
0028 types:  It guarantees correct suspend/resume and shutdown ordering between a
0029 "supplier" device and its "consumer" devices, and it guarantees driver
0030 presence on the supplier.  The consumer devices are not probed before the
0031 supplier is bound to a driver, and they're unbound before the supplier
0032 is unbound.
0033 
0034 When driver presence on the supplier is irrelevant and only correct
0035 suspend/resume and shutdown ordering is needed, the device link may
0036 simply be set up with the ``DL_FLAG_STATELESS`` flag.  In other words,
0037 enforcing driver presence on the supplier is optional.
0038 
0039 Another optional feature is runtime PM integration:  By setting the
0040 ``DL_FLAG_PM_RUNTIME`` flag on addition of the device link, the PM core
0041 is instructed to runtime resume the supplier and keep it active
0042 whenever and for as long as the consumer is runtime resumed.
0043 
0044 Usage
0045 =====
0046 
0047 The earliest point in time when device links can be added is after
0048 :c:func:`device_add()` has been called for the supplier and
0049 :c:func:`device_initialize()` has been called for the consumer.
0050 
0051 It is legal to add them later, but care must be taken that the system
0052 remains in a consistent state:  E.g. a device link cannot be added in
0053 the midst of a suspend/resume transition, so either commencement of
0054 such a transition needs to be prevented with :c:func:`lock_system_sleep()`,
0055 or the device link needs to be added from a function which is guaranteed
0056 not to run in parallel to a suspend/resume transition, such as from a
0057 device ``->probe`` callback or a boot-time PCI quirk.
0058 
0059 Another example for an inconsistent state would be a device link that
0060 represents a driver presence dependency, yet is added from the consumer's
0061 ``->probe`` callback while the supplier hasn't started to probe yet:  Had the
0062 driver core known about the device link earlier, it wouldn't have probed the
0063 consumer in the first place.  The onus is thus on the consumer to check
0064 presence of the supplier after adding the link, and defer probing on
0065 non-presence.  [Note that it is valid to create a link from the consumer's
0066 ``->probe`` callback while the supplier is still probing, but the consumer must
0067 know that the supplier is functional already at the link creation time (that is
0068 the case, for instance, if the consumer has just acquired some resources that
0069 would not have been available had the supplier not been functional then).]
0070 
0071 If a device link with ``DL_FLAG_STATELESS`` set (i.e. a stateless device link)
0072 is added in the ``->probe`` callback of the supplier or consumer driver, it is
0073 typically deleted in its ``->remove`` callback for symmetry.  That way, if the
0074 driver is compiled as a module, the device link is added on module load and
0075 orderly deleted on unload.  The same restrictions that apply to device link
0076 addition (e.g. exclusion of a parallel suspend/resume transition) apply equally
0077 to deletion.  Device links managed by the driver core are deleted automatically
0078 by it.
0079 
0080 Several flags may be specified on device link addition, two of which
0081 have already been mentioned above:  ``DL_FLAG_STATELESS`` to express that no
0082 driver presence dependency is needed (but only correct suspend/resume and
0083 shutdown ordering) and ``DL_FLAG_PM_RUNTIME`` to express that runtime PM
0084 integration is desired.
0085 
0086 Two other flags are specifically targeted at use cases where the device
0087 link is added from the consumer's ``->probe`` callback:  ``DL_FLAG_RPM_ACTIVE``
0088 can be specified to runtime resume the supplier and prevent it from suspending
0089 before the consumer is runtime suspended.  ``DL_FLAG_AUTOREMOVE_CONSUMER``
0090 causes the device link to be automatically purged when the consumer fails to
0091 probe or later unbinds.
0092 
0093 Similarly, when the device link is added from supplier's ``->probe`` callback,
0094 ``DL_FLAG_AUTOREMOVE_SUPPLIER`` causes the device link to be automatically
0095 purged when the supplier fails to probe or later unbinds.
0096 
0097 If neither ``DL_FLAG_AUTOREMOVE_CONSUMER`` nor ``DL_FLAG_AUTOREMOVE_SUPPLIER``
0098 is set, ``DL_FLAG_AUTOPROBE_CONSUMER`` can be used to request the driver core
0099 to probe for a driver for the consumer driver on the link automatically after
0100 a driver has been bound to the supplier device.
0101 
0102 Note, however, that any combinations of ``DL_FLAG_AUTOREMOVE_CONSUMER``,
0103 ``DL_FLAG_AUTOREMOVE_SUPPLIER`` or ``DL_FLAG_AUTOPROBE_CONSUMER`` with
0104 ``DL_FLAG_STATELESS`` are invalid and cannot be used.
0105 
0106 Limitations
0107 ===========
0108 
0109 Driver authors should be aware that a driver presence dependency for managed
0110 device links (i.e. when ``DL_FLAG_STATELESS`` is not specified on link addition)
0111 may cause probing of the consumer to be deferred indefinitely.  This can become
0112 a problem if the consumer is required to probe before a certain initcall level
0113 is reached.  Worse, if the supplier driver is blacklisted or missing, the
0114 consumer will never be probed.
0115 
0116 Moreover, managed device links cannot be deleted directly.  They are deleted
0117 by the driver core when they are not necessary any more in accordance with the
0118 ``DL_FLAG_AUTOREMOVE_CONSUMER`` and ``DL_FLAG_AUTOREMOVE_SUPPLIER`` flags.
0119 However, stateless device links (i.e. device links with ``DL_FLAG_STATELESS``
0120 set) are expected to be removed by whoever called :c:func:`device_link_add()`
0121 to add them with the help of either :c:func:`device_link_del()` or
0122 :c:func:`device_link_remove()`.
0123 
0124 Passing ``DL_FLAG_RPM_ACTIVE`` along with ``DL_FLAG_STATELESS`` to
0125 :c:func:`device_link_add()` may cause the PM-runtime usage counter of the
0126 supplier device to remain nonzero after a subsequent invocation of either
0127 :c:func:`device_link_del()` or :c:func:`device_link_remove()` to remove the
0128 device link returned by it.  This happens if :c:func:`device_link_add()` is
0129 called twice in a row for the same consumer-supplier pair without removing the
0130 link between these calls, in which case allowing the PM-runtime usage counter
0131 of the supplier to drop on an attempt to remove the link may cause it to be
0132 suspended while the consumer is still PM-runtime-active and that has to be
0133 avoided.  [To work around this limitation it is sufficient to let the consumer
0134 runtime suspend at least once, or call :c:func:`pm_runtime_set_suspended()` for
0135 it with PM-runtime disabled, between the :c:func:`device_link_add()` and
0136 :c:func:`device_link_del()` or :c:func:`device_link_remove()` calls.]
0137 
0138 Sometimes drivers depend on optional resources.  They are able to operate
0139 in a degraded mode (reduced feature set or performance) when those resources
0140 are not present.  An example is an SPI controller that can use a DMA engine
0141 or work in PIO mode.  The controller can determine presence of the optional
0142 resources at probe time but on non-presence there is no way to know whether
0143 they will become available in the near future (due to a supplier driver
0144 probing) or never.  Consequently it cannot be determined whether to defer
0145 probing or not.  It would be possible to notify drivers when optional
0146 resources become available after probing, but it would come at a high cost
0147 for drivers as switching between modes of operation at runtime based on the
0148 availability of such resources would be much more complex than a mechanism
0149 based on probe deferral.  In any case optional resources are beyond the
0150 scope of device links.
0151 
0152 Examples
0153 ========
0154 
0155 * An MMU device exists alongside a busmaster device, both are in the same
0156   power domain.  The MMU implements DMA address translation for the busmaster
0157   device and shall be runtime resumed and kept active whenever and as long
0158   as the busmaster device is active.  The busmaster device's driver shall
0159   not bind before the MMU is bound.  To achieve this, a device link with
0160   runtime PM integration is added from the busmaster device (consumer)
0161   to the MMU device (supplier).  The effect with regards to runtime PM
0162   is the same as if the MMU was the parent of the master device.
0163 
0164   The fact that both devices share the same power domain would normally
0165   suggest usage of a struct dev_pm_domain or struct generic_pm_domain,
0166   however these are not independent devices that happen to share a power
0167   switch, but rather the MMU device serves the busmaster device and is
0168   useless without it.  A device link creates a synthetic hierarchical
0169   relationship between the devices and is thus more apt.
0170 
0171 * A Thunderbolt host controller comprises a number of PCIe hotplug ports
0172   and an NHI device to manage the PCIe switch.  On resume from system sleep,
0173   the NHI device needs to re-establish PCI tunnels to attached devices
0174   before the hotplug ports can resume.  If the hotplug ports were children
0175   of the NHI, this resume order would automatically be enforced by the
0176   PM core, but unfortunately they're aunts.  The solution is to add
0177   device links from the hotplug ports (consumers) to the NHI device
0178   (supplier).  A driver presence dependency is not necessary for this
0179   use case.
0180 
0181 * Discrete GPUs in hybrid graphics laptops often feature an HDA controller
0182   for HDMI/DP audio.  In the device hierarchy the HDA controller is a sibling
0183   of the VGA device, yet both share the same power domain and the HDA
0184   controller is only ever needed when an HDMI/DP display is attached to the
0185   VGA device.  A device link from the HDA controller (consumer) to the
0186   VGA device (supplier) aptly represents this relationship.
0187 
0188 * ACPI allows definition of a device start order by way of _DEP objects.
0189   A classical example is when ACPI power management methods on one device
0190   are implemented in terms of I\ :sup:`2`\ C accesses and require a specific
0191   I\ :sup:`2`\ C controller to be present and functional for the power
0192   management of the device in question to work.
0193 
0194 * In some SoCs a functional dependency exists from display, video codec and
0195   video processing IP cores on transparent memory access IP cores that handle
0196   burst access and compression/decompression.
0197 
0198 Alternatives
0199 ============
0200 
0201 * A struct dev_pm_domain can be used to override the bus,
0202   class or device type callbacks.  It is intended for devices sharing
0203   a single on/off switch, however it does not guarantee a specific
0204   suspend/resume ordering, this needs to be implemented separately.
0205   It also does not by itself track the runtime PM status of the involved
0206   devices and turn off the power switch only when all of them are runtime
0207   suspended.  Furthermore it cannot be used to enforce a specific shutdown
0208   ordering or a driver presence dependency.
0209 
0210 * A struct generic_pm_domain is a lot more heavyweight than a
0211   device link and does not allow for shutdown ordering or driver presence
0212   dependencies.  It also cannot be used on ACPI systems.
0213 
0214 Implementation
0215 ==============
0216 
0217 The device hierarchy, which -- as the name implies -- is a tree,
0218 becomes a directed acyclic graph once device links are added.
0219 
0220 Ordering of these devices during suspend/resume is determined by the
0221 dpm_list.  During shutdown it is determined by the devices_kset.  With
0222 no device links present, the two lists are a flattened, one-dimensional
0223 representations of the device tree such that a device is placed behind
0224 all its ancestors.  That is achieved by traversing the ACPI namespace
0225 or OpenFirmware device tree top-down and appending devices to the lists
0226 as they are discovered.
0227 
0228 Once device links are added, the lists need to satisfy the additional
0229 constraint that a device is placed behind all its suppliers, recursively.
0230 To ensure this, upon addition of the device link the consumer and the
0231 entire sub-graph below it (all children and consumers of the consumer)
0232 are moved to the end of the list.  (Call to :c:func:`device_reorder_to_tail()`
0233 from :c:func:`device_link_add()`.)
0234 
0235 To prevent introduction of dependency loops into the graph, it is
0236 verified upon device link addition that the supplier is not dependent
0237 on the consumer or any children or consumers of the consumer.
0238 (Call to :c:func:`device_is_dependent()` from :c:func:`device_link_add()`.)
0239 If that constraint is violated, :c:func:`device_link_add()` will return
0240 ``NULL`` and a ``WARNING`` will be logged.
0241 
0242 Notably this also prevents the addition of a device link from a parent
0243 device to a child.  However the converse is allowed, i.e. a device link
0244 from a child to a parent.  Since the driver core already guarantees
0245 correct suspend/resume and shutdown ordering between parent and child,
0246 such a device link only makes sense if a driver presence dependency is
0247 needed on top of that.  In this case driver authors should weigh
0248 carefully if a device link is at all the right tool for the purpose.
0249 A more suitable approach might be to simply use deferred probing or
0250 add a device flag causing the parent driver to be probed before the
0251 child one.
0252 
0253 State machine
0254 =============
0255 
0256 .. kernel-doc:: include/linux/device.h
0257    :functions: device_link_state
0258 
0259 ::
0260 
0261                  .=============================.
0262                  |                             |
0263                  v                             |
0264  DORMANT <=> AVAILABLE <=> CONSUMER_PROBE => ACTIVE
0265     ^                                          |
0266     |                                          |
0267     '============ SUPPLIER_UNBIND <============'
0268 
0269 * The initial state of a device link is automatically determined by
0270   :c:func:`device_link_add()` based on the driver presence on the supplier
0271   and consumer.  If the link is created before any devices are probed, it
0272   is set to ``DL_STATE_DORMANT``.
0273 
0274 * When a supplier device is bound to a driver, links to its consumers
0275   progress to ``DL_STATE_AVAILABLE``.
0276   (Call to :c:func:`device_links_driver_bound()` from
0277   :c:func:`driver_bound()`.)
0278 
0279 * Before a consumer device is probed, presence of supplier drivers is
0280   verified by checking the consumer device is not in the wait_for_suppliers
0281   list and by checking that links to suppliers are in ``DL_STATE_AVAILABLE``
0282   state.  The state of the links is updated to ``DL_STATE_CONSUMER_PROBE``.
0283   (Call to :c:func:`device_links_check_suppliers()` from
0284   :c:func:`really_probe()`.)
0285   This prevents the supplier from unbinding.
0286   (Call to :c:func:`wait_for_device_probe()` from
0287   :c:func:`device_links_unbind_consumers()`.)
0288 
0289 * If the probe fails, links to suppliers revert back to ``DL_STATE_AVAILABLE``.
0290   (Call to :c:func:`device_links_no_driver()` from :c:func:`really_probe()`.)
0291 
0292 * If the probe succeeds, links to suppliers progress to ``DL_STATE_ACTIVE``.
0293   (Call to :c:func:`device_links_driver_bound()` from :c:func:`driver_bound()`.)
0294 
0295 * When the consumer's driver is later on removed, links to suppliers revert
0296   back to ``DL_STATE_AVAILABLE``.
0297   (Call to :c:func:`__device_links_no_driver()` from
0298   :c:func:`device_links_driver_cleanup()`, which in turn is called from
0299   :c:func:`__device_release_driver()`.)
0300 
0301 * Before a supplier's driver is removed, links to consumers that are not
0302   bound to a driver are updated to ``DL_STATE_SUPPLIER_UNBIND``.
0303   (Call to :c:func:`device_links_busy()` from
0304   :c:func:`__device_release_driver()`.)
0305   This prevents the consumers from binding.
0306   (Call to :c:func:`device_links_check_suppliers()` from
0307   :c:func:`really_probe()`.)
0308   Consumers that are bound are freed from their driver; consumers that are
0309   probing are waited for until they are done.
0310   (Call to :c:func:`device_links_unbind_consumers()` from
0311   :c:func:`__device_release_driver()`.)
0312   Once all links to consumers are in ``DL_STATE_SUPPLIER_UNBIND`` state,
0313   the supplier driver is released and the links revert to ``DL_STATE_DORMANT``.
0314   (Call to :c:func:`device_links_driver_cleanup()` from
0315   :c:func:`__device_release_driver()`.)
0316 
0317 API
0318 ===
0319 
0320 See device_link_add(), device_link_del() and device_link_remove().