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
281 :c:func:`driver_bound()`.)281 :c:func:`driver_bound()`.)
282 282
283* Before a consumer device is probed, presence of supplier drivers is283* 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``
285 state. The state of the links is updated to ``DL_STATE_CONSUMER_PROBE``.286 state. The state of the links is updated to ``DL_STATE_CONSUMER_PROBE``.
286 (Call to :c:func:`device_links_check_suppliers()` from287 (Call to :c:func:`device_links_check_suppliers()` from
287 :c:func:`really_probe()`.)288 :c:func:`really_probe()`.)
drivers/base/core.c+88−0
@@ -44,6 +44,8 @@ early_param("sysfs.deprecated", sysfs_deprecated_setup); · 43 unmodified lines
44#endif44#endif
45 45
46/* Device links support. */46/* Device links support. */
47static LIST_HEAD(wait_for_suppliers);
48static DEFINE_MUTEX(wfs_lock);
47 49
48#ifdef CONFIG_SRCU50#ifdef CONFIG_SRCU
49static DEFINE_MUTEX(device_links_lock);51static DEFINE_MUTEX(device_links_lock);
@@ -430,6 +432,58 @@ struct device_link *device_link_add(struct device *consumer, · 380 unmodified lines
430}432}
431EXPORT_SYMBOL_GPL(device_link_add);433EXPORT_SYMBOL_GPL(device_link_add);
432 434
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 */
450static 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 */
475static 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
433static void device_link_free(struct device_link *link)487static void device_link_free(struct device_link *link)
434{488{
435 while (refcount_dec_not_one(&link->rpm_active))489 while (refcount_dec_not_one(&link->rpm_active))
@@ -564,6 +618,17 @@ int device_links_check_suppliers(struct device *dev) · 128 unmodified lines
564 struct device_link *link;618 struct device_link *link;
565 int ret = 0;619 int ret = 0;
566 620
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
567 device_links_write_lock();632 device_links_write_lock();
568 633
569 list_for_each_entry(link, &dev->links.suppliers, c_node) {634 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
848{913{
849 struct device_link *link, *ln;914 struct device_link *link, *ln;
850 915
916 mutex_lock(&wfs_lock);
917 list_del(&dev->links.needs_suppliers);
918 mutex_unlock(&wfs_lock);
919
851 /*920 /*
852 * Delete all of the remaining links from this device to any other921 * Delete all of the remaining links from this device to any other
853 * devices (either consumers or suppliers).922 * devices (either consumers or suppliers).
@@ -1712,6 +1781,7 @@ void device_initialize(struct device *dev) · 858 unmodified lines
1712#endif1781#endif
1713 INIT_LIST_HEAD(&dev->links.consumers);1782 INIT_LIST_HEAD(&dev->links.consumers);
1714 INIT_LIST_HEAD(&dev->links.suppliers);1783 INIT_LIST_HEAD(&dev->links.suppliers);
1784 INIT_LIST_HEAD(&dev->links.needs_suppliers);
1715 dev->links.status = DL_DEV_NO_DRIVER;1785 dev->links.status = DL_DEV_NO_DRIVER;
1716}1786}
1717EXPORT_SYMBOL_GPL(device_initialize);1787EXPORT_SYMBOL_GPL(device_initialize);
@@ -2202,6 +2272,24 @@ int device_add(struct device *dev) · 484 unmodified lines
2202 if (dev->fwnode && !dev->fwnode->dev)2272 if (dev->fwnode && !dev->fwnode->dev)
2203 dev->fwnode->dev = dev;2273 dev->fwnode->dev = dev;
2204 2274
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
2205 bus_probe_device(dev);2293 bus_probe_device(dev);
2206 if (parent)2294 if (parent)
2207 klist_add_tail(&dev->p->knode_parent,2295 klist_add_tail(&dev->p->knode_parent,
include/linux/device.h+2−0
@@ -1135,11 +1135,13 @@ enum dl_dev_state { · 1134 unmodified lines
1135 * struct dev_links_info - Device data related to device links.1135 * struct dev_links_info - Device data related to device links.
1136 * @suppliers: List of links to supplier devices.1136 * @suppliers: List of links to supplier devices.
1137 * @consumers: List of links to consumer devices.1137 * @consumers: List of links to consumer devices.
1138 * @needs_suppliers: Hook to global list of devices waiting for suppliers.
1138 * @status: Driver status information.1139 * @status: Driver status information.
1139 */1140 */
1140struct dev_links_info {1141struct dev_links_info {
1141 struct list_head suppliers;1142 struct list_head suppliers;
1142 struct list_head consumers;1143 struct list_head consumers;
1144 struct list_head needs_suppliers;
1143 enum dl_dev_state status;1145 enum dl_dev_state status;
1144};1146};
1145 1147
include/linux/fwnode.h+17−0
@@ -66,6 +66,21 @@ struct fwnode_reference_args { · 65 unmodified lines
66 * endpoint node.66 * endpoint node.
67 * @graph_get_port_parent: Return the parent node of a port node.67 * @graph_get_port_parent: Return the parent node of a port node.
68 * @graph_parse_endpoint: Parse endpoint for port and endpoint id.68 * @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.
69 */84 */
70struct fwnode_operations {85struct fwnode_operations {
71 struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);86 struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
@@ -103,6 +118,8 @@ struct fwnode_operations { · 31 unmodified lines
103 (*graph_get_port_parent)(struct fwnode_handle *fwnode);118 (*graph_get_port_parent)(struct fwnode_handle *fwnode);
104 int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,119 int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
105 struct fwnode_endpoint *endpoint);120 struct fwnode_endpoint *endpoint);
121 int (*add_links)(const struct fwnode_handle *fwnode,
122 struct device *dev);
106};123};
107 124
108#define fwnode_has_op(fwnode, op) \125#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