History

driver core: Add support for linking devices during device addition

The firmware corresponding to a device (dev.fwnode) might be able to provide functional dependency information between a device and its supplier and consumer devices. Tracking this functional dependency allows optimizing device probe order and informing a supplier when all its consumers have probed (and thereby actively managing their resources). The existing device links feature allows tracking and using supplier-consumer relationships. So, this patch adds the add_links() fwnode callback to allow firmware to create device links for each device as the device is added. However, when consumer devices are added, they might not have a supplier device to link to despite needing mandatory resources/functionality from one or more suppliers. A waiting_for_suppliers list is created to track such consumers and retry linking them when new devices get added.

Saravana Kannan committed 7 years ago

Commit e2ae9bc1 parent 372a67c4 files · +109 −1Browse files

Unified · Split

4 files
  1. Documentation/driver-api/device_link.rst +2 −1
  2. drivers/base/core.c +88 −0
  3. include/linux/device.h +2 −0
  4. include/linux/fwnode.h +17 −0
Documentation/driver-api/device_link.rst+2−1
@@ -281,7 +281,8 @@ State machine · 280 unmodified lines
281281 :c:func:`driver_bound()`.)
282282
283283 * Before a consumer device is probed, presence of supplier drivers is
284− verified by checking that links to suppliers are in ``DL_STATE_AVAILABLE``
284+ verified by checking the consumer device is not in the wait_for_suppliers
285+ list and by checking that links to suppliers are in ``DL_STATE_AVAILABLE``
285286 state. The state of the links is updated to ``DL_STATE_CONSUMER_PROBE``.
286287 (Call to :c:func:`device_links_check_suppliers()` from
287288 :c:func:`really_probe()`.)
drivers/base/core.c+88−0
@@ -44,6 +44,8 @@ early_param("sysfs.deprecated", sysfs_deprecated_setup); · 43 unmodified lines
4444 #endif
4545
4646 /* Device links support. */
47+static LIST_HEAD(wait_for_suppliers);
48+static DEFINE_MUTEX(wfs_lock);
4749
4850 #ifdef CONFIG_SRCU
4951 static DEFINE_MUTEX(device_links_lock);
@@ -430,6 +432,58 @@ struct device_link *device_link_add(struct device *consumer, · 380 unmodified lines
430432 }
431433 EXPORT_SYMBOL_GPL(device_link_add);
432434
435+/**
436+ * device_link_wait_for_supplier - Add device to wait_for_suppliers list
437+ * @consumer: Consumer device
438+ *
439+ * Marks the @consumer device as waiting for suppliers to become available by
440+ * adding it to the wait_for_suppliers list. The consumer device will never be
441+ * probed until it's removed from the wait_for_suppliers list.
442+ *
443+ * The caller is responsible for adding the links to the supplier devices once
444+ * they are available and removing the @consumer device from the
445+ * wait_for_suppliers list once links to all the suppliers have been created.
446+ *
447+ * This function is NOT meant to be called from the probe function of the
448+ * consumer but rather from code that creates/adds the consumer device.
449+ */
450+static void device_link_wait_for_supplier(struct device *consumer)
451+{
452+ mutex_lock(&wfs_lock);
453+ list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers);
454+ mutex_unlock(&wfs_lock);
455+}
456+
457+/**
458+ * device_link_add_missing_supplier_links - Add links from consumer devices to
459+ * supplier devices, leaving any
460+ * consumer with inactive suppliers on
461+ * the wait_for_suppliers list
462+ *
463+ * Loops through all consumers waiting on suppliers and tries to add all their
464+ * supplier links. If that succeeds, the consumer device is removed from
465+ * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
466+ * list. Devices left on the wait_for_suppliers list will not be probed.
467+ *
468+ * The fwnode add_links callback is expected to return 0 if it has found and
469+ * added all the supplier links for the consumer device. It should return an
470+ * error if it isn't able to do so.
471+ *
472+ * The caller of device_link_wait_for_supplier() is expected to call this once
473+ * it's aware of potential suppliers becoming available.
474+ */
475+static void device_link_add_missing_supplier_links(void)
476+{
477+ struct device *dev, *tmp;
478+
479+ mutex_lock(&wfs_lock);
480+ list_for_each_entry_safe(dev, tmp, &wait_for_suppliers,
481+ links.needs_suppliers)
482+ if (!fwnode_call_int_op(dev->fwnode, add_links, dev))
483+ list_del_init(&dev->links.needs_suppliers);
484+ mutex_unlock(&wfs_lock);
485+}
486+
433487 static void device_link_free(struct device_link *link)
434488 {
435489 while (refcount_dec_not_one(&link->rpm_active))
@@ -564,6 +618,17 @@ int device_links_check_suppliers(struct device *dev) · 128 unmodified lines
564618 struct device_link *link;
565619 int ret = 0;
566620
621+ /*
622+ * Device waiting for supplier to become available is not allowed to
623+ * probe.
624+ */
625+ mutex_lock(&wfs_lock);
626+ if (!list_empty(&dev->links.needs_suppliers)) {
627+ mutex_unlock(&wfs_lock);
628+ return -EPROBE_DEFER;
629+ }
630+ mutex_unlock(&wfs_lock);
631+
567632 device_links_write_lock();
568633
569634 list_for_each_entry(link, &dev->links.suppliers, c_node) {
@@ -848,6 +913,10 @@ static void device_links_purge(struct device *dev) · 278 unmodified lines
848913 {
849914 struct device_link *link, *ln;
850915
916+ mutex_lock(&wfs_lock);
917+ list_del(&dev->links.needs_suppliers);
918+ mutex_unlock(&wfs_lock);
919+
851920 /*
852921 * Delete all of the remaining links from this device to any other
853922 * devices (either consumers or suppliers).
@@ -1712,6 +1781,7 @@ void device_initialize(struct device *dev) · 858 unmodified lines
17121781 #endif
17131782 INIT_LIST_HEAD(&dev->links.consumers);
17141783 INIT_LIST_HEAD(&dev->links.suppliers);
1784+ INIT_LIST_HEAD(&dev->links.needs_suppliers);
17151785 dev->links.status = DL_DEV_NO_DRIVER;
17161786 }
17171787 EXPORT_SYMBOL_GPL(device_initialize);
@@ -2202,6 +2272,24 @@ int device_add(struct device *dev) · 484 unmodified lines
22022272 if (dev->fwnode && !dev->fwnode->dev)
22032273 dev->fwnode->dev = dev;
22042274
2275+ /*
2276+ * Check if any of the other devices (consumers) have been waiting for
2277+ * this device (supplier) to be added so that they can create a device
2278+ * link to it.
2279+ *
2280+ * This needs to happen after device_pm_add() because device_link_add()
2281+ * requires the supplier be registered before it's called.
2282+ *
2283+ * But this also needs to happe before bus_probe_device() to make sure
2284+ * waiting consumers can link to it before the driver is bound to the
2285+ * device and the driver sync_state callback is called for this device.
2286+ */
2287+ device_link_add_missing_supplier_links();
2288+
2289+ if (fwnode_has_op(dev->fwnode, add_links)
2290+ && fwnode_call_int_op(dev->fwnode, add_links, dev))
2291+ device_link_wait_for_supplier(dev);
2292+
22052293 bus_probe_device(dev);
22062294 if (parent)
22072295 klist_add_tail(&dev->p->knode_parent,
include/linux/device.h+2−0
@@ -1135,11 +1135,13 @@ enum dl_dev_state { · 1134 unmodified lines
11351135 * struct dev_links_info - Device data related to device links.
11361136 * @suppliers: List of links to supplier devices.
11371137 * @consumers: List of links to consumer devices.
1138+ * @needs_suppliers: Hook to global list of devices waiting for suppliers.
11381139 * @status: Driver status information.
11391140 */
11401141 struct dev_links_info {
11411142 struct list_head suppliers;
11421143 struct list_head consumers;
1144+ struct list_head needs_suppliers;
11431145 enum dl_dev_state status;
11441146 };
11451147
include/linux/fwnode.h+17−0
@@ -66,6 +66,21 @@ struct fwnode_reference_args { · 65 unmodified lines
6666 * endpoint node.
6767 * @graph_get_port_parent: Return the parent node of a port node.
6868 * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
69+ * @add_links: Called after the device corresponding to the fwnode is added
70+ * using device_add(). The function is expected to create device
71+ * links to all the suppliers of the device that are available at
72+ * the time this function is called. The function must NOT stop
73+ * at the first failed device link if other unlinked supplier
74+ * devices are present in the system. If some suppliers are not
75+ * yet available, this function will be called again when other
76+ * devices are added to allow creating device links to any newly
77+ * available suppliers.
78+ *
79+ * Return 0 if device links have been successfully created to all
80+ * the suppliers of this device or if the supplier information is
81+ * not known. Return an error if and only if the supplier
82+ * information is known but some of the suppliers are not yet
83+ * available to create device links to.
6984 */
7085 struct fwnode_operations {
7186 struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
@@ -103,6 +118,8 @@ struct fwnode_operations { · 31 unmodified lines
103118 (*graph_get_port_parent)(struct fwnode_handle *fwnode);
104119 int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
105120 struct fwnode_endpoint *endpoint);
121+ int (*add_links)(const struct fwnode_handle *fwnode,
122+ struct device *dev);
106123 };
107124
108125 #define fwnode_has_op(fwnode, op) \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t