[PATCH RFC 09/19] drm/bridge: Make the bridge chain a double-linked list

Boris Brezillon boris.brezillon at collabora.com
Thu Aug 8 15:11:40 UTC 2019


So each element in the chain can easily access its predecessor. This
will be needed for the work we are doing on bus format negociation
between elements of the bridge chain.

Signed-off-by: Boris Brezillon <boris.brezillon at collabora.com>
---
 drivers/gpu/drm/drm_bridge.c  | 278 +++++++++++++---------------------
 drivers/gpu/drm/drm_encoder.c |  15 +-
 include/drm/drm_bridge.h      |   4 +-
 include/drm/drm_encoder.h     |   4 +-
 4 files changed, 116 insertions(+), 185 deletions(-)

diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c
index b96bbba05ba9..ed2410f49581 100644
--- a/drivers/gpu/drm/drm_bridge.c
+++ b/drivers/gpu/drm/drm_bridge.c
@@ -55,7 +55,7 @@
  * just provide additional hooks to get the desired output at the end of the
  * encoder chain.
  *
- * Bridges can also be chained up using the &drm_bridge.next pointer.
+ * Bridges can also be chained up using the &drm_bridge.chain_node field.
  *
  * Both legacy CRTC helpers and the new atomic modeset helpers support bridges.
  */
@@ -114,6 +114,7 @@ EXPORT_SYMBOL(drm_bridge_remove);
 int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 		      struct drm_bridge *previous)
 {
+	LIST_HEAD(tmp_list);
 	int ret;
 
 	if (!encoder || !bridge)
@@ -127,6 +128,7 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 
 	bridge->dev = encoder->dev;
 	bridge->encoder = encoder;
+	list_add_tail(&bridge->chain_node, &tmp_list);
 
 	if (bridge->funcs->attach) {
 		ret = bridge->funcs->attach(bridge);
@@ -138,9 +140,9 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 	}
 
 	if (previous)
-		previous->next = bridge;
+		list_splice(&tmp_list, &previous->chain_node);
 	else
-		encoder->bridge = bridge;
+		list_splice(&tmp_list, &encoder->bridge_chain);
 
 	return 0;
 }
@@ -157,6 +159,7 @@ void drm_bridge_detach(struct drm_bridge *bridge)
 	if (bridge->funcs->detach)
 		bridge->funcs->detach(bridge);
 
+	list_del(&bridge->chain_node);
 	bridge->dev = NULL;
 }
 
@@ -171,7 +174,8 @@ void drm_bridge_detach(struct drm_bridge *bridge)
 struct drm_bridge *
 drm_bridge_chain_get_first_bridge(struct drm_encoder *encoder)
 {
-	return encoder->bridge;
+	return list_first_entry_or_null(&encoder->bridge_chain,
+					struct drm_bridge, chain_node);
 }
 EXPORT_SYMBOL(drm_bridge_chain_get_first_bridge);
 
@@ -186,12 +190,11 @@ EXPORT_SYMBOL(drm_bridge_chain_get_first_bridge);
 struct drm_bridge *
 drm_bridge_chain_get_last_bridge(struct drm_encoder *encoder)
 {
-	struct drm_bridge *bridge = encoder->bridge;
+	if (list_empty(&encoder->bridge_chain))
+		return NULL;
 
-	while(bridge && bridge->next)
-		bridge = bridge->next;
-
-	return bridge;
+	return list_last_entry(&encoder->bridge_chain, struct drm_bridge,
+			       chain_node);
 }
 EXPORT_SYMBOL(drm_bridge_chain_get_last_bridge);
 
@@ -205,10 +208,11 @@ EXPORT_SYMBOL(drm_bridge_chain_get_last_bridge);
 struct drm_bridge *
 drm_bridge_chain_get_next_bridge(struct drm_bridge *bridge)
 {
-	if (!bridge || !bridge->encoder)
-		return NULL;
+	if (!bridge || !bridge->encoder ||
+	    list_is_last(&bridge->encoder->bridge_chain, &bridge->chain_node))
+                return NULL;
 
-	return bridge->next;
+	return list_next_entry(bridge, chain_node);
 }
 EXPORT_SYMBOL(drm_bridge_chain_get_next_bridge);
 
@@ -223,98 +227,6 @@ EXPORT_SYMBOL(drm_bridge_chain_get_next_bridge);
  * For detailed specification of the bridge callbacks see &drm_bridge_funcs.
  */
 
-static bool drm_bridge_mode_fixup(struct drm_bridge *bridge,
-				  const struct drm_display_mode *mode,
-				  struct drm_display_mode *adjusted_mode)
-{
-	bool ret = true;
-
-	if (!bridge)
-		return true;
-
-	if (bridge->funcs->mode_fixup)
-		ret = bridge->funcs->mode_fixup(bridge, mode, adjusted_mode);
-
-	ret = ret && drm_bridge_mode_fixup(bridge->next, mode, adjusted_mode);
-
-	return ret;
-}
-
-static enum drm_mode_status
-drm_bridge_mode_valid(struct drm_bridge *bridge,
-		      const struct drm_display_mode *mode)
-{
-	enum drm_mode_status ret = MODE_OK;
-
-	if (!bridge)
-		return ret;
-
-	if (bridge->funcs->mode_valid)
-		ret = bridge->funcs->mode_valid(bridge, mode);
-
-	if (ret != MODE_OK)
-		return ret;
-
-	return drm_bridge_mode_valid(bridge->next, mode);
-}
-
-static void drm_bridge_disable(struct drm_bridge *bridge)
-{
-	if (!bridge)
-		return;
-
-	drm_bridge_disable(bridge->next);
-
-	if (bridge->funcs->disable)
-		bridge->funcs->disable(bridge);
-}
-
-static void drm_bridge_post_disable(struct drm_bridge *bridge)
-{
-	if (!bridge)
-		return;
-
-	if (bridge->funcs->post_disable)
-		bridge->funcs->post_disable(bridge);
-
-	drm_bridge_post_disable(bridge->next);
-}
-
-static void drm_bridge_mode_set(struct drm_bridge *bridge,
-				const struct drm_display_mode *mode,
-				const struct drm_display_mode *adjusted_mode)
-{
-	if (!bridge)
-		return;
-
-	if (bridge->funcs->mode_set)
-		bridge->funcs->mode_set(bridge, mode, adjusted_mode);
-
-	drm_bridge_mode_set(bridge->next, mode, adjusted_mode);
-}
-
-static void drm_bridge_pre_enable(struct drm_bridge *bridge)
-{
-	if (!bridge)
-		return;
-
-	drm_bridge_pre_enable(bridge->next);
-
-	if (bridge->funcs->pre_enable)
-		bridge->funcs->pre_enable(bridge);
-}
-
-static void drm_bridge_enable(struct drm_bridge *bridge)
-{
-	if (!bridge)
-		return;
-
-	if (bridge->funcs->enable)
-		bridge->funcs->enable(bridge);
-
-	drm_bridge_enable(bridge->next);
-}
-
 /**
  * drm_bridge_chain_mode_fixup - fixup proposed mode for all bridges in the
  *				 encoder chain
@@ -332,7 +244,17 @@ bool drm_bridge_chain_mode_fixup(struct drm_encoder *encoder,
 				 const struct drm_display_mode *mode,
 				 struct drm_display_mode *adjusted_mode)
 {
-	return drm_bridge_mode_fixup(encoder->bridge, mode, adjusted_mode);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
+		if (!bridge->funcs->mode_fixup)
+			continue;
+
+		if (!bridge->funcs->mode_fixup(bridge, mode, adjusted_mode))
+			return false;
+	}
+
+	return true;
 }
 EXPORT_SYMBOL(drm_bridge_chain_mode_fixup);
 
@@ -353,7 +275,20 @@ enum drm_mode_status
 drm_bridge_chain_mode_valid(struct drm_encoder *encoder,
 			    const struct drm_display_mode *mode)
 {
-	return drm_bridge_mode_valid(encoder->bridge, mode);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
+		enum drm_mode_status ret;
+
+		if (!bridge->funcs->mode_valid)
+			continue;
+
+		ret = bridge->funcs->mode_valid(bridge, mode);
+		if (ret != MODE_OK)
+			return ret;
+	}
+
+	return MODE_OK;
 }
 EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
 
@@ -367,7 +302,13 @@ EXPORT_SYMBOL(drm_bridge_chain_mode_valid);
  */
 void drm_bridge_chain_disable(struct drm_encoder *encoder)
 {
-	drm_bridge_disable(encoder->bridge);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry_reverse(bridge, &encoder->bridge_chain,
+				    chain_node) {
+		if (bridge->funcs->disable)
+			bridge->funcs->disable(bridge);
+	}
 }
 EXPORT_SYMBOL(drm_bridge_chain_disable);
 
@@ -382,7 +323,12 @@ EXPORT_SYMBOL(drm_bridge_chain_disable);
  */
 void drm_bridge_chain_post_disable(struct drm_encoder *encoder)
 {
-	drm_bridge_post_disable(encoder->bridge);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
+		if (bridge->funcs->post_disable)
+			bridge->funcs->post_disable(bridge);
+	}
 }
 EXPORT_SYMBOL(drm_bridge_chain_post_disable);
 
@@ -400,7 +346,12 @@ void drm_bridge_chain_mode_set(struct drm_encoder *encoder,
 			       const struct drm_display_mode *mode,
 			       const struct drm_display_mode *adjusted_mode)
 {
-	drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
+		if (bridge->funcs->mode_set)
+			bridge->funcs->mode_set(bridge, mode, adjusted_mode);
+	}
 }
 EXPORT_SYMBOL(drm_bridge_chain_mode_set);
 
@@ -415,7 +366,13 @@ EXPORT_SYMBOL(drm_bridge_chain_mode_set);
  */
 void drm_bridge_chain_pre_enable(struct drm_encoder *encoder)
 {
-	drm_bridge_pre_enable(encoder->bridge);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry_reverse(bridge, &encoder->bridge_chain,
+				    chain_node) {
+		if (bridge->funcs->pre_enable)
+			bridge->funcs->pre_enable(bridge);
+	}
 }
 EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
 
@@ -429,66 +386,15 @@ EXPORT_SYMBOL(drm_bridge_chain_pre_enable);
  */
 void drm_bridge_chain_enable(struct drm_encoder *encoder)
 {
-	drm_bridge_enable(encoder->bridge);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
+		if (bridge->funcs->enable)
+			bridge->funcs->enable(bridge);
+	}
 }
 EXPORT_SYMBOL(drm_bridge_chain_enable);
 
-static void drm_atomic_bridge_disable(struct drm_bridge *bridge,
-				      struct drm_atomic_state *state)
-{
-	if (!bridge)
-		return;
-
-	drm_atomic_bridge_disable(bridge->next, state);
-
-	if (bridge->funcs->atomic_disable)
-		bridge->funcs->atomic_disable(bridge, state);
-	else if (bridge->funcs->disable)
-		bridge->funcs->disable(bridge);
-}
-
-static void drm_atomic_bridge_post_disable(struct drm_bridge *bridge,
-					   struct drm_atomic_state *state)
-{
-	if (!bridge)
-		return;
-
-	if (bridge->funcs->atomic_post_disable)
-		bridge->funcs->atomic_post_disable(bridge, state);
-	else if (bridge->funcs->post_disable)
-		bridge->funcs->post_disable(bridge);
-
-	drm_atomic_bridge_post_disable(bridge->next, state);
-}
-
-static void drm_atomic_bridge_pre_enable(struct drm_bridge *bridge,
-					 struct drm_atomic_state *state)
-{
-	if (!bridge)
-		return;
-
-	drm_atomic_bridge_pre_enable(bridge->next, state);
-
-	if (bridge->funcs->atomic_pre_enable)
-		bridge->funcs->atomic_pre_enable(bridge, state);
-	else if (bridge->funcs->pre_enable)
-		bridge->funcs->pre_enable(bridge);
-}
-
-static void drm_atomic_bridge_enable(struct drm_bridge *bridge,
-				     struct drm_atomic_state *state)
-{
-	if (!bridge)
-		return;
-
-	if (bridge->funcs->atomic_enable)
-		bridge->funcs->atomic_enable(bridge, state);
-	else if (bridge->funcs->enable)
-		bridge->funcs->enable(bridge);
-
-	drm_atomic_bridge_enable(bridge->next, state);
-}
-
 /**
  * drm_atomic_bridge_chain_disable - disables all bridges in the encoder chain
  * @encoder: encoder object
@@ -502,7 +408,15 @@ static void drm_atomic_bridge_enable(struct drm_bridge *bridge,
 void drm_atomic_bridge_chain_disable(struct drm_encoder *encoder,
 				     struct drm_atomic_state *state)
 {
-	drm_atomic_bridge_disable(encoder->bridge, state);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry_reverse(bridge, &encoder->bridge_chain,
+				    chain_node) {
+		if (bridge->funcs->atomic_disable)
+			bridge->funcs->atomic_disable(bridge, state);
+		else if (bridge->funcs->disable)
+			bridge->funcs->disable(bridge);
+	}
 }
 EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
 
@@ -520,7 +434,14 @@ EXPORT_SYMBOL(drm_atomic_bridge_chain_disable);
 void drm_atomic_bridge_chain_post_disable(struct drm_encoder *encoder,
 					  struct drm_atomic_state *state)
 {
-	drm_atomic_bridge_post_disable(encoder->bridge, state);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
+		if (bridge->funcs->atomic_post_disable)
+			bridge->funcs->atomic_post_disable(bridge, state);
+		else if (bridge->funcs->post_disable)
+			bridge->funcs->post_disable(bridge);
+	}
 }
 EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
 
@@ -538,7 +459,15 @@ EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable);
 void drm_atomic_bridge_chain_pre_enable(struct drm_encoder *encoder,
 					struct drm_atomic_state *state)
 {
-	drm_atomic_bridge_pre_enable(encoder->bridge, state);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry_reverse(bridge, &encoder->bridge_chain,
+				    chain_node) {
+		if (bridge->funcs->atomic_pre_enable)
+			bridge->funcs->atomic_pre_enable(bridge, state);
+		else if (bridge->funcs->pre_enable)
+			bridge->funcs->pre_enable(bridge);
+	}
 }
 EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
 
@@ -555,7 +484,14 @@ EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable);
 void drm_atomic_bridge_chain_enable(struct drm_encoder *encoder,
 				    struct drm_atomic_state *state)
 {
-	drm_atomic_bridge_enable(encoder->bridge, state);
+	struct drm_bridge *bridge;
+
+	list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
+		if (bridge->funcs->atomic_enable)
+			bridge->funcs->atomic_enable(bridge, state);
+		else if (bridge->funcs->enable)
+			bridge->funcs->enable(bridge);
+	}
 }
 EXPORT_SYMBOL(drm_atomic_bridge_chain_enable);
 
diff --git a/drivers/gpu/drm/drm_encoder.c b/drivers/gpu/drm/drm_encoder.c
index 80d88a55302e..e555281f43d4 100644
--- a/drivers/gpu/drm/drm_encoder.c
+++ b/drivers/gpu/drm/drm_encoder.c
@@ -140,6 +140,7 @@ int drm_encoder_init(struct drm_device *dev,
 		goto out_put;
 	}
 
+	INIT_LIST_HEAD(&encoder->bridge_chain);
 	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
 	encoder->index = dev->mode_config.num_encoder++;
 
@@ -160,22 +161,16 @@ EXPORT_SYMBOL(drm_encoder_init);
 void drm_encoder_cleanup(struct drm_encoder *encoder)
 {
 	struct drm_device *dev = encoder->dev;
+	struct drm_bridge *bridge, *next;
 
 	/* Note that the encoder_list is considered to be static; should we
 	 * remove the drm_encoder at runtime we would have to decrement all
 	 * the indices on the drm_encoder after us in the encoder_list.
 	 */
 
-	if (encoder->bridge) {
-		struct drm_bridge *bridge = encoder->bridge;
-		struct drm_bridge *next;
-
-		while (bridge) {
-			next = bridge->next;
-			drm_bridge_detach(bridge);
-			bridge = next;
-		}
-	}
+	list_for_each_entry_safe(bridge, next, &encoder->bridge_chain,
+				 chain_node)
+		drm_bridge_detach(bridge);
 
 	drm_mode_object_unregister(dev, &encoder->base);
 	kfree(encoder->name);
diff --git a/include/drm/drm_bridge.h b/include/drm/drm_bridge.h
index d429bd1c00f9..f40181274ed7 100644
--- a/include/drm/drm_bridge.h
+++ b/include/drm/drm_bridge.h
@@ -383,8 +383,8 @@ struct drm_bridge {
 	struct drm_device *dev;
 	/** @encoder: encoder to which this bridge is connected */
 	struct drm_encoder *encoder;
-	/** @next: the next bridge in the encoder chain */
-	struct drm_bridge *next;
+	/** @chain_node: used to form a bridge chain */
+	struct list_head chain_node;
 #ifdef CONFIG_OF
 	/** @of_node: device node pointer to the bridge */
 	struct device_node *of_node;
diff --git a/include/drm/drm_encoder.h b/include/drm/drm_encoder.h
index d986ff1197ce..0899f7f34e3a 100644
--- a/include/drm/drm_encoder.h
+++ b/include/drm/drm_encoder.h
@@ -89,7 +89,7 @@ struct drm_encoder_funcs {
  * @head: list management
  * @base: base KMS object
  * @name: human readable name, can be overwritten by the driver
- * @bridge: bridge associated to the encoder
+ * @bridge_chain: bridges associated to the encoder
  * @funcs: control functions
  * @helper_private: mid-layer private data
  *
@@ -172,7 +172,7 @@ struct drm_encoder {
 	 * &drm_connector_state.crtc.
 	 */
 	struct drm_crtc *crtc;
-	struct drm_bridge *bridge;
+	struct list_head bridge_chain;
 	const struct drm_encoder_funcs *funcs;
 	const struct drm_encoder_helper_funcs *helper_private;
 
-- 
2.21.0



More information about the dri-devel mailing list