[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250320-drm-bridge-refcount-v8-2-b3ddaa9f1368@bootlin.com>
Date: Thu, 20 Mar 2025 16:42:11 +0100
From: Luca Ceresoli <luca.ceresoli@...tlin.com>
To: Andrzej Hajda <andrzej.hajda@...el.com>,
Neil Armstrong <neil.armstrong@...aro.org>, Robert Foss <rfoss@...nel.org>,
Laurent Pinchart <Laurent.pinchart@...asonboard.com>,
Jonas Karlman <jonas@...boo.se>, Jernej Skrabec <jernej.skrabec@...il.com>,
Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>,
Maxime Ripard <mripard@...nel.org>, Thomas Zimmermann <tzimmermann@...e.de>,
David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>,
Marek Vasut <marex@...x.de>, Stefan Agner <stefan@...er.ch>,
Shawn Guo <shawnguo@...nel.org>, Sascha Hauer <s.hauer@...gutronix.de>,
Pengutronix Kernel Team <kernel@...gutronix.de>,
Fabio Estevam <festevam@...il.com>, Inki Dae <inki.dae@...sung.com>,
Jagan Teki <jagan@...rulasolutions.com>,
Marek Szyprowski <m.szyprowski@...sung.com>
Cc: Thomas Petazzoni <thomas.petazzoni@...tlin.com>,
Anusha Srivatsa <asrivats@...hat.com>,
Paul Kocialkowski <paulk@...-base.io>, Dmitry Baryshkov <lumag@...nel.org>,
Hervé Codina <herve.codina@...tlin.com>,
Hui Pu <Hui.Pu@...ealthcare.com>, dri-devel@...ts.freedesktop.org,
linux-kernel@...r.kernel.org, imx@...ts.linux.dev,
linux-arm-kernel@...ts.infradead.org,
Luca Ceresoli <luca.ceresoli@...tlin.com>
Subject: [PATCH v8 2/5] drm/bridge: add support for refcounting
DRM bridges are currently considered as a fixed element of a DRM card, and
thus their lifetime is assumed to extend for as long as the card
exists. New use cases, such as hot-pluggable hardware with video bridges,
require DRM bridges to be added to and removed from a DRM card without
tearing the card down. This is possible for connectors already (used by DP
MST), it is now needed for DRM bridges as well.
As a first preliminary step, make bridges reference-counted to allow a
struct drm_bridge (along with the private driver structure embedding it) to
stay allocated even after the driver has been removed, until the last
reference is put.
Signed-off-by: Luca Ceresoli <luca.ceresoli@...tlin.com>
---
Changes in v8:
- move back drm_bridge_get/put() to the .c file, make __drm_bridge_free
private
- move back drm_bridge_put_void() to .c, not used in this series outside
of drm_bridge.c
- remove drm_bridge_put_and_clear(), not used in this series
- minor docs and coding style tweaks
Changes in v7:
- export drm_bridge_put_void
- struct drm_bridge: use container pointer instead of container_offset
- remove drm_bridge_is_refcounted()
- remove all DRM_DEBUG()s
- drm_bridge_get/put: accept NULL pointer and return the bridge pointer to
allow pass-through calls
- extract to separate patches:
- the addition of drm_bridge_alloc
- the addition of drm_bridge_get/put() to drm_bridge_add/remove()
- the addition of drm_bridge_get/put() to drm_bridge_attach/detach()
- fix a typo, slightly improve kerneldoc
Changes in v6:
- use drm_warn, not WARN_ON (Jani Nikula)
- Add devm_drm_bridge_alloc() to replace drm_bridge_init() (similar to
drmm_encoder_alloc)
- Remove .destroy func: deallocation is done via the struct offset
computed by the devm_drm_bridge_alloc() macro
- use fixed free callback, as the same callback is used in all cases
anyway (remove free_cb, add bool is_refcounted)
- add drm_bridge_get/put() to drm_bridge_attach/detach() (add the bridge
to a list)
- make some DRM_DEBUG() strings more informative
This patch was added in v5.
---
drivers/gpu/drm/drm_bridge.c | 72 +++++++++++++++++++++++++++++++++++++++++++-
include/drm/drm_bridge.h | 19 ++++++++++++
2 files changed, 90 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c
index 84fa1a1330cbabd309526829fff70971cfed1dcd..2f2ecb73308e601b1a53ec8e7110933cef59b5da 100644
--- a/drivers/gpu/drm/drm_bridge.c
+++ b/drivers/gpu/drm/drm_bridge.c
@@ -199,23 +199,93 @@
static DEFINE_MUTEX(bridge_lock);
static LIST_HEAD(bridge_list);
+static void __drm_bridge_free(struct kref *kref)
+{
+ struct drm_bridge *bridge = container_of(kref, struct drm_bridge, refcount);
+
+ kfree(bridge->container);
+}
+
+/**
+ * drm_bridge_get - Acquire a bridge reference
+ * @bridge: DRM bridge
+ *
+ * This function increments the bridge's refcount.
+ *
+ * Returns:
+ * Pointer to @bridge.
+ */
+struct drm_bridge *drm_bridge_get(struct drm_bridge *bridge)
+{
+ if (!bridge)
+ return bridge;
+
+ kref_get(&bridge->refcount);
+
+ return bridge;
+}
+EXPORT_SYMBOL(drm_bridge_get);
+
+/**
+ * drm_bridge_put - Release a bridge reference
+ * @bridge: DRM bridge
+ *
+ * This function decrements the bridge's reference count and frees the
+ * object if the reference count drops to zero.
+ *
+ * Returns:
+ * Pointer to @bridge.
+ */
+struct drm_bridge *drm_bridge_put(struct drm_bridge *bridge)
+{
+ if (!bridge)
+ return bridge;
+
+ kref_put(&bridge->refcount, __drm_bridge_free);
+
+ return bridge;
+}
+EXPORT_SYMBOL(drm_bridge_put);
+
+/**
+ * drm_bridge_put_void - wrapper to drm_bridge_put() taking a void pointer
+ *
+ * @data: pointer to @struct drm_bridge, cast to a void pointer
+ *
+ * Wrapper of drm_bridge_put() to be used when a function taking a void
+ * pointer is needed, for example as a devm action.
+ */
+static void drm_bridge_put_void(void *data)
+{
+ struct drm_bridge *bridge = (struct drm_bridge *)data;
+
+ drm_bridge_put(bridge);
+}
+
void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
const struct drm_bridge_funcs *funcs)
{
void *container;
struct drm_bridge *bridge;
+ int err;
if (!funcs) {
dev_warn(dev, "Missing funcs pointer\n");
return ERR_PTR(-EINVAL);
}
- container = devm_kzalloc(dev, size, GFP_KERNEL);
+ container = kzalloc(size, GFP_KERNEL);
if (!container)
return ERR_PTR(-ENOMEM);
bridge = container + offset;
+ bridge->container = container;
bridge->funcs = funcs;
+ kref_init(&bridge->refcount);
+
+ err = devm_add_action_or_reset(dev, drm_bridge_put_void, bridge);
+ if (err)
+ return ERR_PTR(err);
return container;
}
diff --git a/include/drm/drm_bridge.h b/include/drm/drm_bridge.h
index a59277674d5a2937e324d3ce48f934418788053f..076f40c17dd86b224bbe6517b71f1aa6c24bfbd7 100644
--- a/include/drm/drm_bridge.h
+++ b/include/drm/drm_bridge.h
@@ -840,6 +840,18 @@ struct drm_bridge {
const struct drm_bridge_timings *timings;
/** @funcs: control functions */
const struct drm_bridge_funcs *funcs;
+
+ /**
+ * @container: Pointer to the private driver struct embedding this
+ * @struct drm_bridge.
+ */
+ void *container;
+
+ /**
+ * @refcount: reference count of users referencing this bridge.
+ */
+ struct kref refcount;
+
/** @driver_private: pointer to the bridge driver's internal context */
void *driver_private;
/** @ops: bitmask of operations supported by the bridge */
@@ -941,6 +953,9 @@ drm_priv_to_bridge(struct drm_private_obj *priv)
return container_of(priv, struct drm_bridge, base);
}
+struct drm_bridge *drm_bridge_get(struct drm_bridge *bridge);
+struct drm_bridge *drm_bridge_put(struct drm_bridge *bridge);
+
void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
const struct drm_bridge_funcs *funcs);
@@ -951,6 +966,10 @@ void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
* @member: the name of the &drm_bridge within @type
* @funcs: callbacks for this bridge
*
+ * The reference count of the returned bridge is initialized to 1. This
+ * reference will be automatically dropped via devm (by calling
+ * drm_bridge_put()) when @dev is removed.
+ *
* Returns:
* Pointer to new bridge, or ERR_PTR on failure.
*/
--
2.48.1
Powered by blists - more mailing lists