drm/vc4: Rework the encoder retrieval code
authorMaxime Ripard <maxime@cerno.tech>
Tue, 16 Mar 2021 12:51:52 +0000 (13:51 +0100)
committerPhil Elwell <8911409+pelwell@users.noreply.github.com>
Wed, 21 Apr 2021 13:13:56 +0000 (14:13 +0100)
Due to a FIFO that cannot be flushed between the pixelvalve and the HDMI
controllers on BCM2711, we need to carefully disable both at boot time
if they were left enabled by the firmware.

However, at the time we're running that code, the struct drm_connector
encoder pointer isn't set yet, and thus we cannot retrieve the encoder
associated to our CRTC.

We can however make use of the fact that we have a less flexible setup
than what DRM allows where we have a 1:1 relationship between our CRTCs
and encoders (and connectors), and thus store the crtc associated to our
encoder at boot time.

We cannot do that at the time the encoders are probed though, since the
CRTCs won't be probed yet and thus we don't know at that time which CRTC
index we're going to get, so let's do this in two passes: we can first
bind all the components and then once they all are bound, we can iterate
over all the encoders to find their associated CRTC and set the pointer.

This is similar to what we're doing to set the possible_crtcs field.

Fixes: 875a4d536842 ("drm/vc4: drv: Disable the CRTC at boot time")
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
drivers/gpu/drm/vc4/vc4_crtc.c
drivers/gpu/drm/vc4/vc4_drv.c
drivers/gpu/drm/vc4/vc4_drv.h

index 4efd3892192a01a1494531c29890c95b2dbacae1..133c96ee482994edb928e9d8d9ed92942594827e 100644 (file)
@@ -272,6 +272,19 @@ static u32 vc4_crtc_get_fifo_full_level_bits(struct vc4_crtc *vc4_crtc,
                                   PV_CONTROL_FIFO_LEVEL);
 }
 
+static struct drm_encoder *vc4_get_connector_encoder(struct drm_connector *connector)
+{
+       struct drm_encoder *encoder;
+
+       if (drm_WARN_ON(connector->dev, hweight32(connector->possible_encoders) != 1))
+               return NULL;
+
+       drm_connector_for_each_possible_encoder(connector, encoder)
+               return encoder;
+
+       return NULL;
+}
+
 /*
  * Returns the encoder attached to the CRTC.
  *
@@ -286,9 +299,17 @@ static struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc)
 
        drm_connector_list_iter_begin(crtc->dev, &conn_iter);
        drm_for_each_connector_iter(connector, &conn_iter) {
-               if (connector->state->crtc == crtc) {
+               struct drm_encoder *encoder;
+               struct vc4_encoder *vc4_encoder;
+
+               encoder = vc4_get_connector_encoder(connector);
+               if (!encoder)
+                       continue;
+
+               vc4_encoder = to_vc4_encoder(encoder);
+               if (vc4_encoder->crtc == crtc) {
                        drm_connector_list_iter_end(&conn_iter);
-                       return connector->encoder;
+                       return encoder;
                }
        }
        drm_connector_list_iter_end(&conn_iter);
index 617c113b033fba646340765db3d41a79aedc5dd1..8baa90837e9e6201a16809e63363529a1225bf09 100644 (file)
@@ -226,6 +226,41 @@ static int compare_dev(struct device *dev, void *data)
        return dev == data;
 }
 
+static struct drm_crtc *vc4_drv_find_crtc(struct drm_device *drm,
+                                         struct drm_encoder *encoder)
+{
+       struct drm_crtc *crtc;
+
+       if (WARN_ON(hweight32(encoder->possible_crtcs) != 1))
+               return NULL;
+
+       drm_for_each_crtc(crtc, drm) {
+               if (!drm_encoder_crtc_ok(encoder, crtc))
+                       continue;
+
+               return crtc;
+       }
+
+       return NULL;
+}
+
+static void vc4_drv_set_encoder_data(struct drm_device *drm)
+{
+       struct drm_encoder *encoder;
+
+       drm_for_each_encoder(encoder, drm) {
+               struct vc4_encoder *vc4_encoder;
+               struct drm_crtc *crtc;
+
+               crtc = vc4_drv_find_crtc(drm, encoder);
+               if (WARN_ON(!crtc))
+                       return;
+
+               vc4_encoder = to_vc4_encoder(encoder);
+               vc4_encoder->crtc = crtc;
+       }
+}
+
 static void vc4_match_add_drivers(struct device *dev,
                                  struct component_match **match,
                                  struct platform_driver *const *drivers,
@@ -308,6 +343,7 @@ static int vc4_drm_bind(struct device *dev)
        ret = component_bind_all(dev, drm);
        if (ret)
                return ret;
+       vc4_drv_set_encoder_data(drm);
 
        if (!vc4->firmware_kms) {
                ret = vc4_plane_create_additional_planes(drm);
index 7a70838595b2f484ace675d596e1fcd5df0594df..ff4e71f79e494174b8ebafe0739b80e82798d621 100644 (file)
@@ -445,6 +445,16 @@ enum vc4_encoder_type {
 
 struct vc4_encoder {
        struct drm_encoder base;
+
+       /*
+        * At boot time, we need to be able to retrieve the CRTC for a given
+        * connector in order to run the disable hooks below to avoid the stuck
+        * pixel issue. Unfortunately the drm_connector->encoder pointer is
+        * NULL at that time so we can't move up the chain, so we'll store it
+        * ourselves here.
+        */
+       struct drm_crtc *crtc;
+
        enum vc4_encoder_type type;
        u32 clock_select;