Supporting bridge hotplug requires the ability to detach all bridges from
the one that got removed to the end of the pipeline. Add a function for
this.

Take care to not try detaching non-attached bridges. This is needed because
when two or more bridges are removed not in the backwards order,
drm_encoder_cleanup_from() is called more than once for bridges closer to
the panel.

Signed-off-by: Luca Ceresoli <[email protected]>

---

This replaces "drm/encoder: add drm_encoder_cleanup_from()" from v2 as
discussed with Maxime in v1:
https://lore.kernel.org/all/20260609-hypnotic-honeybee-of-adventure-d89b7c@houat/
---
 drivers/gpu/drm/drm_bridge.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c
index e0a1e016b37c..a29f3e60bb24 100644
--- a/drivers/gpu/drm/drm_bridge.c
+++ b/drivers/gpu/drm/drm_bridge.c
@@ -504,6 +504,40 @@ int devm_drm_bridge_add(struct device *dev, struct 
drm_bridge *bridge)
 }
 EXPORT_SYMBOL(devm_drm_bridge_add);
 
+/**
+ * drm_bridge_detach_from - detach a given bridge and all the following
+ *                          from an encoder chain
+ * @bridge: first bridge to remove
+ *
+ * Removes from an encoder all the bridges starting with a given bridge
+ * and until the end of the chain.
+ *
+ * Does nothing if the bridge is not attached to an encoder chain.
+ */
+static void drm_bridge_detach_from(struct drm_bridge *bridge)
+{
+       struct drm_encoder *encoder = bridge->encoder;
+       struct drm_bridge *next;
+       LIST_HEAD(tmplist);
+
+       /*
+        * We need the bridge_chain_mutex to modify the chain, but
+        * drm_bridge_detach() will call DRM_MODESET_LOCK_ALL_BEGIN() (in
+        * drm_modeset_lock_fini()), resulting in a possible ABBA circular
+        * deadlock. Avoid it by first moving all the bridges to a
+        * temporary list holding the lock, and then calling
+        * drm_bridge_detach() without the lock.
+        */
+       mutex_lock(&encoder->bridge_chain_mutex);
+       if (!list_empty(&bridge->chain_node))
+               list_for_each_entry_safe_from(bridge, next, 
&encoder->bridge_chain, chain_node)
+                       list_move_tail(&bridge->chain_node, &tmplist);
+       mutex_unlock(&encoder->bridge_chain_mutex);
+
+       while (!list_empty(&tmplist))
+               drm_bridge_detach(list_first_entry(&tmplist, struct drm_bridge, 
chain_node));
+}
+
 /**
  * drm_bridge_remove - unregister a bridge
  *

-- 
2.56.1

Reply via email to