To support hot-unplug of this bridge we need to protect access to device
resources in case device removal happens concurrently to other code.

Some care is needed for the case when drm_bridge_unplug() happens before
ti_sn_bridge_atomic_post_disable(), so the latter cannot release supplier
resources taken by ti_sn_bridge_atomic_pre_enable(): runtime PM (and in
turn regulators) and refclk.

To ensure those resources are released exactly once on device removal:

 * move the code to release them to a dedicated function
 * register that function when the resources are taken in
   ti_sn_bridge_atomic_pre_enable()
 * if ti_sn_bridge_atomic_post_disable() happens before drm_bridge_unplug()
   (typical non-hot-unplug case):
   * ti_sn_bridge_atomic_post_disable() can enter the critical section
     (drm_bridge_enter() returns 0) -> it releases and executes the
     devres action
 * if ti_sn_bridge_atomic_post_disable() happens after drm_bridge_unplug()
   (typical hot-unplug case):
   * ti_sn_bridge_remove() -> drm_bridge_unplug() defuses
     ti_sn_bridge_atomic_post_disable() so it cannot execute the devres
     action
   * the devres action is executed just after ti_sn_bridge_remove()

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

---

New patch in v3

This commit basically replicates the same change done on the ti-sn65dsi83
driver, now upstream:
https://lore.kernel.org/all/20251112-drm-bridge-atomic-vs-remove-v3-2-85db717ce...@bootlin.com/
---
 drivers/gpu/drm/bridge/ti-sn65dsi86.c | 50 ++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/bridge/ti-sn65dsi86.c 
b/drivers/gpu/drm/bridge/ti-sn65dsi86.c
index 4fd9fde77d2b..75e36bf261b0 100644
--- a/drivers/gpu/drm/bridge/ti-sn65dsi86.c
+++ b/drivers/gpu/drm/bridge/ti-sn65dsi86.c
@@ -852,9 +852,15 @@ static void ti_sn_bridge_atomic_disable(struct drm_bridge 
*bridge,
                                        struct drm_atomic_commit *state)
 {
        struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+       int idx;
+
+       if (!drm_bridge_enter(bridge, &idx))
+               return;
 
        /* disable video stream */
        regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE, 0);
+
+       drm_bridge_exit(idx);
 }
 
 static void ti_sn_bridge_set_dsi_rate(struct ti_sn65dsi86 *pdata,
@@ -1124,11 +1130,16 @@ static void ti_sn_bridge_atomic_enable(struct 
drm_bridge *bridge,
        int ret = -EINVAL;
        int max_dp_lanes;
        unsigned int bpp;
+       int idx;
+
+       if (!drm_bridge_enter(bridge, &idx))
+               return;
 
        connector = drm_atomic_get_new_connector_for_encoder(state,
                                                             bridge->encoder);
        if (!connector) {
                dev_err_ratelimited(pdata->dev, "Could not get the 
connector\n");
+               drm_bridge_exit(idx);
                return;
        }
 
@@ -1206,6 +1217,7 @@ static void ti_sn_bridge_atomic_enable(struct drm_bridge 
*bridge,
        }
        if (ret) {
                DRM_DEV_ERROR(pdata->dev, "%s (%d)\n", last_err_str, ret);
+               drm_bridge_exit(idx);
                return;
        }
 
@@ -1215,26 +1227,57 @@ static void ti_sn_bridge_atomic_enable(struct 
drm_bridge *bridge,
        /* enable video stream */
        regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE,
                           VSTREAM_ENABLE);
+
+       drm_bridge_exit(idx);
+}
+
+/*
+ * Release resources taken by ti_sn_bridge_atomic_pre_enable().
+ *
+ * Invoked by ti_sn_bridge_atomic_post_disable() normally, or by devres
+ * after ti_sn_bridge_remove() in case this happens befora
+ * atomic_post_disable.
+ */
+static void ti_sn_bridge_release_resources(void *data)
+{
+       struct ti_sn65dsi86 *pdata = (struct ti_sn65dsi86 *)data;
+
+       if (!pdata->refclk)
+               ti_sn65dsi86_disable_comms(pdata);
+
+       pm_runtime_put_sync(pdata->dev);
 }
 
 static void ti_sn_bridge_atomic_pre_enable(struct drm_bridge *bridge,
                                           struct drm_atomic_commit *state)
 {
        struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+       int idx;
+
+       if (!drm_bridge_enter(bridge, &idx))
+               return;
 
        pm_runtime_get_sync(pdata->dev);
 
        if (!pdata->refclk)
                ti_sn65dsi86_enable_comms(pdata, state);
 
+       devm_add_action(pdata->dev, ti_sn_bridge_release_resources, pdata);
+
        /* td7: min 100 us after enable before DSI data */
        usleep_range(100, 110);
+
+       drm_bridge_exit(idx);
 }
 
 static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,
                                             struct drm_atomic_commit *state)
 {
        struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+       int idx;
+
+       if (!drm_bridge_enter(bridge, &idx))
+               return;
 
        /* semi auto link training mode OFF */
        regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0);
@@ -1243,10 +1286,9 @@ static void ti_sn_bridge_atomic_post_disable(struct 
drm_bridge *bridge,
        /* disable DP PLL */
        regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);
 
-       if (!pdata->refclk)
-               ti_sn65dsi86_disable_comms(pdata);
+       devm_release_action(pdata->dev, ti_sn_bridge_release_resources, pdata);
 
-       pm_runtime_put_sync(pdata->dev);
+       drm_bridge_exit(idx);
 }
 
 static enum drm_connector_status
@@ -1514,7 +1556,7 @@ static void ti_sn_bridge_remove(struct auxiliary_device 
*adev)
        if (!pdata)
                return;
 
-       drm_bridge_remove(&pdata->bridge);
+       drm_bridge_unplug(&pdata->bridge);
 
        of_node_put(pdata->host_node);
 }

-- 
2.56.1

Reply via email to