From nobody Sun Dec 14 06:17:17 2025 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86E9733A026 for ; Wed, 12 Nov 2025 16:34:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762965298; cv=none; b=TmXm9te561GQzEEh0PcYV3wbV5JIwLrb4PhDOCSCODnl7JS7ZrJYsCZP7GE9YlPNIUiPKd0i3XfusC20Mvfs+FGozRAfydxAfteItHq/weH9qquDxH1+fwZXZnDxFyeG0bTuyM0WED7g1fV/R6xT1jZ7dGyX3cdno72i5lbxMVg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762965298; c=relaxed/simple; bh=m7pFqUbehQFZT3TAGqXcOGURwuIsoFlYDExsqXsTh2M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Q0+6B+t2fm7suhFCzBc2DnpiopB3QacsKSO62Thbk6KEk3Q+ffduD3ZNlIzG03yy4EKVyRFXKGAvWHr9oVQpbZ61md7vSUfW80EbAj/qIkm7I+iRhiCPmRdjx0orDxcvBo26A6g8KN4TqYizoVaRDqCA9nXhRJWTNQKd3hcj6vs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=N5q44cJc; arc=none smtp.client-ip=185.171.202.116 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="N5q44cJc" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id DA370C0F574; Wed, 12 Nov 2025 16:34:31 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 801DF6070B; Wed, 12 Nov 2025 16:34:53 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 0D1DD102F084F; Wed, 12 Nov 2025 17:34:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1762965292; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=AbBF9YK95hha/G3M+Fz+28OT4bWjR1IMawslI9+BHEU=; b=N5q44cJcURD+I6ZaGZujoY1LJ5r+oAAO9Dn/gr8R9vqI56UPqbOCi5YrhPJwh9oijOD6Ay zoRctqubfuVasY/rqGGVY/SK55fhWBh/60qss3SrDtXSUUuPiKZj+cPsJz+qJ4QdrEL3tZ q4b3nra6CdfdshIGXnTN5oWiimBDKoP2045flPsJH6oKIim+a3EWx6xMRXsEj4fzSKiqbZ coxHQdv9wghleS1WepYu3/3pOBZUTHviW2Oinv3gSSFpnxK+fr21OezJU0sN8DV9gTeuTs a0MRxSR5/SMlTzDhxOh+3vNxMqOQOb4hUk83sgPpMFmk+h2m1FcoOntk/hvt9w== From: Luca Ceresoli Date: Wed, 12 Nov 2025 17:34:34 +0100 Subject: [PATCH v3 1/2] drm/bridge: add drm_bridge_unplug() and drm_bridge_enter/exit() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251112-drm-bridge-atomic-vs-remove-v3-1-85db717ce094@bootlin.com> References: <20251112-drm-bridge-atomic-vs-remove-v3-0-85db717ce094@bootlin.com> In-Reply-To: <20251112-drm-bridge-atomic-vs-remove-v3-0-85db717ce094@bootlin.com> To: Maxime Ripard , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec , Maarten Lankhorst , Thomas Zimmermann , David Airlie Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Dmitry Baryshkov X-Mailer: b4 0.14.2 X-Last-TLS-Session-Version: TLSv1.3 To allow DRM bridges to be removable, add synchronization functions allowing to tell when a bridge hardware has been physically unplugged and to mark a critical section that should not be entered after that. This is inspired by the drm_dev_unplugged/enter/exit() functions for struct drm_device. Suggested-by: Maxime Ripard Link: https://lore.kernel.org/all/20250106-vigorous-talented-viper-fa49d9@h= ouat/ Signed-off-by: Luca Ceresoli Reviewed-by: Maxime Ripard --- Changes in v3: - call drm_bridge_remove() in drm_bridge_unplug() - Fixed kerneldoc variable names (reported here: https://lore.kernel.org/lkml/202509271654.j3IsjsAJ-lkp@intel.com/) --- drivers/gpu/drm/drm_bridge.c | 62 ++++++++++++++++++++++++++++++++++++++++= ++++ include/drm/drm_bridge.h | 12 +++++++++ 2 files changed, 74 insertions(+) diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c index 8f355df883d8ac8de9d361ec302f4ccbf3bca0d6..db40c26d1cb354f818f12a5a53f= 9baa6a7fc3574 100644 --- a/drivers/gpu/drm/drm_bridge.c +++ b/drivers/gpu/drm/drm_bridge.c @@ -27,6 +27,7 @@ #include #include #include +#include =20 #include #include @@ -202,6 +203,67 @@ static DEFINE_MUTEX(bridge_lock); static LIST_HEAD(bridge_list); static LIST_HEAD(bridge_lingering_list); =20 +DEFINE_STATIC_SRCU(drm_bridge_unplug_srcu); + +/** + * drm_bridge_enter - Enter DRM bridge critical section + * @bridge: DRM bridge + * @idx: Pointer to index that will be passed to the matching drm_bridge_e= xit() + * + * This function marks and protects the beginning of a section that should= not + * be entered after the bridge has been unplugged. The section end is mark= ed + * with drm_bridge_exit(). Calls to this function can be nested. + * + * Returns: + * True if it is OK to enter the section, false otherwise. + */ +bool drm_bridge_enter(struct drm_bridge *bridge, int *idx) +{ + *idx =3D srcu_read_lock(&drm_bridge_unplug_srcu); + + if (bridge->unplugged) { + srcu_read_unlock(&drm_bridge_unplug_srcu, *idx); + return false; + } + + return true; +} +EXPORT_SYMBOL(drm_bridge_enter); + +/** + * drm_bridge_exit - Exit DRM bridge critical section + * @idx: index returned by drm_bridge_enter() + * + * This function marks the end of a section that should not be entered aft= er + * the bridge has been unplugged. + */ +void drm_bridge_exit(int idx) +{ + srcu_read_unlock(&drm_bridge_unplug_srcu, idx); +} +EXPORT_SYMBOL(drm_bridge_exit); + +/** + * drm_bridge_unplug - declare a DRM bridge was unplugged and remove it + * @bridge: DRM bridge + * + * This tells the bridge has been physically unplugged and no operations on + * device resources must be done anymore. Entry-points can use + * drm_bridge_enter() and drm_bridge_exit() to protect device resources in + * a race free manner. + * + * Also unregisters the bridge. + */ +void drm_bridge_unplug(struct drm_bridge *bridge) +{ + bridge->unplugged =3D true; + + synchronize_srcu(&drm_bridge_unplug_srcu); + + drm_bridge_remove(bridge); +} +EXPORT_SYMBOL(drm_bridge_unplug); + static void __drm_bridge_free(struct kref *kref) { struct drm_bridge *bridge =3D container_of(kref, struct drm_bridge, refco= unt); diff --git a/include/drm/drm_bridge.h b/include/drm/drm_bridge.h index 0ff7ab4aa8689a022458f935a7ffb23a2b715802..d2683846cc619007f85a6f605b9= d23ce971e04b3 100644 --- a/include/drm/drm_bridge.h +++ b/include/drm/drm_bridge.h @@ -1143,6 +1143,14 @@ struct drm_bridge { */ struct kref refcount; =20 + /** + * @unplugged: + * + * Flag to tell if the bridge has been unplugged. + * See drm_bridge_enter() and drm_bridge_unplug(). + */ + bool unplugged; + /** @driver_private: pointer to the bridge driver's internal context */ void *driver_private; /** @ops: bitmask of operations supported by the bridge */ @@ -1278,6 +1286,10 @@ drm_priv_to_bridge(struct drm_private_obj *priv) return container_of(priv, struct drm_bridge, base); } =20 +bool drm_bridge_enter(struct drm_bridge *bridge, int *idx); +void drm_bridge_exit(int idx); +void drm_bridge_unplug(struct drm_bridge *bridge); + struct drm_bridge *drm_bridge_get(struct drm_bridge *bridge); void drm_bridge_put(struct drm_bridge *bridge); =20 --=20 2.51.1 From nobody Sun Dec 14 06:17:17 2025 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5545D328270 for ; Wed, 12 Nov 2025 16:34:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762965299; cv=none; b=dMCr3pTd4gxjYF6iXMM7yidPaPO5Gpw5fWmZPIxLdp3QUBsvl/jaH7kWeo//DO29u5sDtyp5ef0PcECczjY2GAX1P3Jb1B5UGiznZzHc0Hyqp8bX/4zi5QbkYPZ8uJt4YGX4rmso9Dm9XGejQesv9k+IVz6Z+zXy5nf4OJyW9ns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762965299; c=relaxed/simple; bh=iddbU73MIU0wsEL139orRGRzGpm285N28O0OK4pFHlQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IaFzETj3df4GRrt3zwyU9MYhRbz8acE4gvFU6vAxNy4twDsnkQuTfMDA4BFoe8T87Gg1uj2a1hmb2weVXBNsyEqsltoBRz8GF1PgawrUlfAnSykyw1xymRVQtxSQbisXkpmRWr3vQ8/N3D2wg/d78Y04SQ0iqVgfC27GXpgX2FI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=uXGm9niC; arc=none smtp.client-ip=185.246.84.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="uXGm9niC" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id C39D01A1A42; Wed, 12 Nov 2025 16:34:55 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 996F86070B; Wed, 12 Nov 2025 16:34:55 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 10D10102F1BA8; Wed, 12 Nov 2025 17:34:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1762965294; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=0pMT+aT4AJiznRBcaHknDkWgDDjg3dsj5pXkKOf+n0k=; b=uXGm9niCgFce1r9ejT7Q27ZwTUZBk8vJxQc2SmlAWHeACNSg4Aqfw3fZVCw4CbfNctuvB3 hzOZKd9NarrtUO0cuGkE1IDaGe1Mp7imOKTqORu/f7D4ANOyKZa1dv4mhT61OsCZPNALaL rRX4inqocvpaHXArIQyEdNPJXYcxTLq8szAKB7KwzdJo5ajk6XdxPu4P5njHRJ4zxUSzu4 xg8eXeXXqmYfYYegYgVY6xC14Yvds1tRcRDAZm1N0lifgSx8ZtBwE3YS7SmWHZxpIiVYqb F2KcpeH6cpUzwxkn9P2EPNg+gAC/o3hmd5M4ehrgZfvA/tdv1Z/fStH/7C86CQ== From: Luca Ceresoli Date: Wed, 12 Nov 2025 17:34:35 +0100 Subject: [PATCH v3 2/2] drm/bridge: ti-sn65dsi83: protect device resources on unplug Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251112-drm-bridge-atomic-vs-remove-v3-2-85db717ce094@bootlin.com> References: <20251112-drm-bridge-atomic-vs-remove-v3-0-85db717ce094@bootlin.com> In-Reply-To: <20251112-drm-bridge-atomic-vs-remove-v3-0-85db717ce094@bootlin.com> To: Maxime Ripard , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec , Maarten Lankhorst , Thomas Zimmermann , David Airlie Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Dmitry Baryshkov X-Mailer: b4 0.14.2 X-Last-TLS-Session-Version: TLSv1.3 To support hot-unplug of this bridge we need to protect access to device resources in case sn65dsi83_remove() happens concurrently to other code. Some care is needed for the case when the unplug happens before sn65dsi83_atomic_disable() has a chance to enter the critical section (i.e. a successful drm_bridge_enter() call), which occurs whenever the hardware is removed while the display is active. When that happens, sn65dsi83_atomic_disable() in unable to release the resources taken by sn65dsi83_atomic_pre_enable(). 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 sn65dsi83_atomic_pre_enable() * if sn65dsi83_atomic_disable() happens before sn65dsi83_remove() (typical non-hot-unplug case): * sn65dsi83_atomic_disable() can enter the critical section (drm_bridge_enter() returns 0) -> it releases and executes the devres action * if sn65dsi83_atomic_disable() happens after sn65dsi83_remove() (typical hot-unplug case): * sn65dsi83_remove() -> drm_bridge_unplug() prevents sn65dsi83_atomic_disable() from entering the critical section (drm_bridge_enter() returns nonzero), so sn65dsi83_atomic_disable() cannot release and execute the devres action * the devres action is executed at the end of sn65dsi83_remove() Signed-off-by: Luca Ceresoli Reviewed-by: Maxime Ripard --- Changes in v3: - Don't call drm_bridge_remove(), it's now in drm_bridge_unplug() - use gotos for error management in sn65dsi83_atomic_pre_enable() - simplify sn65dsi83_release_resources() comment Changes in v2: - Use a devres action instead of a flag --- drivers/gpu/drm/bridge/ti-sn65dsi83.c | 86 +++++++++++++++++++++++++++----= ---- 1 file changed, 66 insertions(+), 20 deletions(-) diff --git a/drivers/gpu/drm/bridge/ti-sn65dsi83.c b/drivers/gpu/drm/bridge= /ti-sn65dsi83.c index 033c44326552ab167d4e8d9b74957c585e4c6fb7..ac74b9e85b97604c95a255fc2c5= 9bd0e7a3137f5 100644 --- a/drivers/gpu/drm/bridge/ti-sn65dsi83.c +++ b/drivers/gpu/drm/bridge/ti-sn65dsi83.c @@ -406,6 +406,10 @@ static void sn65dsi83_reset_work(struct work_struct *w= s) { struct sn65dsi83 *ctx =3D container_of(ws, struct sn65dsi83, reset_work); int ret; + int idx; + + if (!drm_bridge_enter(&ctx->bridge, &idx)) + return; =20 /* Reset the pipe */ ret =3D sn65dsi83_reset_pipe(ctx); @@ -415,12 +419,18 @@ static void sn65dsi83_reset_work(struct work_struct *= ws) } if (ctx->irq) enable_irq(ctx->irq); + + drm_bridge_exit(idx); } =20 static void sn65dsi83_handle_errors(struct sn65dsi83 *ctx) { unsigned int irq_stat; int ret; + int idx; + + if (!drm_bridge_enter(&ctx->bridge, &idx)) + return; =20 /* * Schedule a reset in case of: @@ -441,6 +451,8 @@ static void sn65dsi83_handle_errors(struct sn65dsi83 *c= tx) =20 schedule_work(&ctx->reset_work); } + + drm_bridge_exit(idx); } =20 static void sn65dsi83_monitor_work(struct work_struct *work) @@ -463,6 +475,37 @@ static void sn65dsi83_monitor_stop(struct sn65dsi83 *c= tx) cancel_delayed_work_sync(&ctx->monitor_work); } =20 +/* + * Release resources taken by sn65dsi83_atomic_pre_enable(). + * + * Invoked by sn65dsi83_atomic_disable() normally, or by devres after + * sn65dsi83_remove() in case this happens befora atomic_disable. + */ +static void sn65dsi83_release_resources(void *data) +{ + struct sn65dsi83 *ctx =3D (struct sn65dsi83 *)data; + int ret; + + if (ctx->irq) { + /* Disable irq */ + regmap_write(ctx->regmap, REG_IRQ_EN, 0x0); + regmap_write(ctx->regmap, REG_IRQ_GLOBAL, 0x0); + } else { + /* Stop the polling task */ + sn65dsi83_monitor_stop(ctx); + } + + /* Put the chip in reset, pull EN line low, and assure 10ms reset low tim= ing. */ + gpiod_set_value_cansleep(ctx->enable_gpio, 0); + usleep_range(10000, 11000); + + ret =3D regulator_disable(ctx->vcc); + if (ret) + dev_err(ctx->dev, "Failed to disable vcc: %d\n", ret); + + regcache_mark_dirty(ctx->regmap); +} + static void sn65dsi83_atomic_pre_enable(struct drm_bridge *bridge, struct drm_atomic_state *state) { @@ -478,11 +521,15 @@ static void sn65dsi83_atomic_pre_enable(struct drm_br= idge *bridge, __le16 le16val; u16 val; int ret; + int idx; + + if (!drm_bridge_enter(bridge, &idx)) + return; =20 ret =3D regulator_enable(ctx->vcc); if (ret) { dev_err(ctx->dev, "Failed to enable vcc: %d\n", ret); - return; + goto err_exit; } =20 /* Deassert reset */ @@ -625,7 +672,7 @@ static void sn65dsi83_atomic_pre_enable(struct drm_brid= ge *bridge, dev_err(ctx->dev, "failed to lock PLL, ret=3D%i\n", ret); /* On failure, disable PLL again and exit. */ regmap_write(ctx->regmap, REG_RC_PLL_EN, 0x00); - return; + goto err_add_action; } =20 /* Trigger reset after CSR register update. */ @@ -633,6 +680,11 @@ static void sn65dsi83_atomic_pre_enable(struct drm_bri= dge *bridge, =20 /* Wait for 10ms after soft reset as specified in datasheet */ usleep_range(10000, 12000); + +err_add_action: + devm_add_action(ctx->dev, sn65dsi83_release_resources, ctx); +err_exit: + drm_bridge_exit(idx); } =20 static void sn65dsi83_atomic_enable(struct drm_bridge *bridge, @@ -640,6 +692,10 @@ static void sn65dsi83_atomic_enable(struct drm_bridge = *bridge, { struct sn65dsi83 *ctx =3D bridge_to_sn65dsi83(bridge); unsigned int pval; + int idx; + + if (!drm_bridge_enter(bridge, &idx)) + return; =20 /* Clear all errors that got asserted during initialization. */ regmap_read(ctx->regmap, REG_IRQ_STAT, &pval); @@ -659,32 +715,22 @@ static void sn65dsi83_atomic_enable(struct drm_bridge= *bridge, /* Use the polling task */ sn65dsi83_monitor_start(ctx); } + + drm_bridge_exit(idx); } =20 static void sn65dsi83_atomic_disable(struct drm_bridge *bridge, struct drm_atomic_state *state) { struct sn65dsi83 *ctx =3D bridge_to_sn65dsi83(bridge); - int ret; + int idx; =20 - if (ctx->irq) { - /* Disable irq */ - regmap_write(ctx->regmap, REG_IRQ_EN, 0x0); - regmap_write(ctx->regmap, REG_IRQ_GLOBAL, 0x0); - } else { - /* Stop the polling task */ - sn65dsi83_monitor_stop(ctx); - } - - /* Put the chip in reset, pull EN line low, and assure 10ms reset low tim= ing. */ - gpiod_set_value_cansleep(ctx->enable_gpio, 0); - usleep_range(10000, 11000); + if (!drm_bridge_enter(bridge, &idx)) + return; =20 - ret =3D regulator_disable(ctx->vcc); - if (ret) - dev_err(ctx->dev, "Failed to disable vcc: %d\n", ret); + devm_release_action(ctx->dev, sn65dsi83_release_resources, ctx); =20 - regcache_mark_dirty(ctx->regmap); + drm_bridge_exit(idx); } =20 static enum drm_mode_status @@ -1005,7 +1051,7 @@ static void sn65dsi83_remove(struct i2c_client *clien= t) { struct sn65dsi83 *ctx =3D i2c_get_clientdata(client); =20 - drm_bridge_remove(&ctx->bridge); + drm_bridge_unplug(&ctx->bridge); } =20 static const struct i2c_device_id sn65dsi83_id[] =3D { --=20 2.51.1