c6ca7ce
From d40a5938a10a3ba73bce6395729fefd8b8bb1c07 Mon Sep 17 00:00:00 2001
c6ca7ce
From: Peter Robinson <pbrobinson@gmail.com>
c6ca7ce
Date: Thu, 29 Jun 2017 10:05:05 +0100
c6ca7ce
Subject: [PATCH] drm/vc4: Fix VBLANK handling in crtc->enable() path
c6ca7ce
c6ca7ce
When we are enabling a CRTC, drm_crtc_vblank_get() is called before
c6ca7ce
drm_crtc_vblank_on(), which is not supposed to happen (hence the
c6ca7ce
WARN_ON() in the code). To solve the problem, we delay the 'update
c6ca7ce
display list' operation after the CRTC is actually enabled.
c6ca7ce
c6ca7ce
Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
c6ca7ce
---
c6ca7ce
 drivers/gpu/drm/vc4/vc4_crtc.c | 66 +++++++++++++++++++++++++++---------------
c6ca7ce
 1 file changed, 43 insertions(+), 23 deletions(-)
c6ca7ce
c6ca7ce
diff --git a/drivers/gpu/drm/vc4/vc4_crtc.c b/drivers/gpu/drm/vc4/vc4_crtc.c
c6ca7ce
index d86c8cce3182..316bd6210d69 100644
c6ca7ce
--- a/drivers/gpu/drm/vc4/vc4_crtc.c
c6ca7ce
+++ b/drivers/gpu/drm/vc4/vc4_crtc.c
c6ca7ce
@@ -530,6 +530,34 @@ static void vc4_crtc_disable(struct drm_crtc *crtc)
c6ca7ce
 		     SCALER_DISPSTATX_EMPTY);
c6ca7ce
 }
c6ca7ce
 
c6ca7ce
+static void vc4_crtc_update_dlist(struct drm_crtc *crtc)
c6ca7ce
+{
c6ca7ce
+	struct drm_device *dev = crtc->dev;
c6ca7ce
+	struct vc4_dev *vc4 = to_vc4_dev(dev);
c6ca7ce
+	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
c6ca7ce
+	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
c6ca7ce
+
c6ca7ce
+	if (crtc->state->event) {
c6ca7ce
+		unsigned long flags;
c6ca7ce
+
c6ca7ce
+		crtc->state->event->pipe = drm_crtc_index(crtc);
c6ca7ce
+
c6ca7ce
+		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
c6ca7ce
+
c6ca7ce
+		spin_lock_irqsave(&dev->event_lock, flags);
c6ca7ce
+		vc4_crtc->event = crtc->state->event;
c6ca7ce
+		crtc->state->event = NULL;
c6ca7ce
+
c6ca7ce
+		HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
c6ca7ce
+			  vc4_state->mm.start);
c6ca7ce
+
c6ca7ce
+		spin_unlock_irqrestore(&dev->event_lock, flags);
c6ca7ce
+	} else {
c6ca7ce
+		HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
c6ca7ce
+			  vc4_state->mm.start);
c6ca7ce
+	}
c6ca7ce
+}
c6ca7ce
+
c6ca7ce
 static void vc4_crtc_enable(struct drm_crtc *crtc)
c6ca7ce
 {
c6ca7ce
 	struct drm_device *dev = crtc->dev;
c6ca7ce
@@ -540,6 +568,12 @@ static void vc4_crtc_enable(struct drm_crtc *crtc)
c6ca7ce
 
c6ca7ce
 	require_hvs_enabled(dev);
c6ca7ce
 
c6ca7ce
+	/* Enable vblank irq handling before crtc is started otherwise
c6ca7ce
+	 * drm_crtc_get_vblank() fails in vc4_crtc_update_dlist().
c6ca7ce
+	 */
c6ca7ce
+	drm_crtc_vblank_on(crtc);
c6ca7ce
+	vc4_crtc_update_dlist(crtc);
c6ca7ce
+
c6ca7ce
 	/* Turn on the scaler, which will wait for vstart to start
c6ca7ce
 	 * compositing.
c6ca7ce
 	 */
c6ca7ce
@@ -551,9 +585,6 @@ static void vc4_crtc_enable(struct drm_crtc *crtc)
c6ca7ce
 	/* Turn on the pixel valve, which will emit the vstart signal. */
c6ca7ce
 	CRTC_WRITE(PV_V_CONTROL,
c6ca7ce
 		   CRTC_READ(PV_V_CONTROL) | PV_VCONTROL_VIDEN);
c6ca7ce
-
c6ca7ce
-	/* Enable vblank irq handling after crtc is started. */
c6ca7ce
-	drm_crtc_vblank_on(crtc);
c6ca7ce
 }
c6ca7ce
 
c6ca7ce
 static bool vc4_crtc_mode_fixup(struct drm_crtc *crtc,
c6ca7ce
@@ -608,7 +639,6 @@ static void vc4_crtc_atomic_flush(struct drm_crtc *crtc,
c6ca7ce
 {
c6ca7ce
 	struct drm_device *dev = crtc->dev;
c6ca7ce
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
c6ca7ce
-	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
c6ca7ce
 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
c6ca7ce
 	struct drm_plane *plane;
c6ca7ce
 	bool debug_dump_regs = false;
c6ca7ce
@@ -630,25 +660,15 @@ static void vc4_crtc_atomic_flush(struct drm_crtc *crtc,
c6ca7ce
 
c6ca7ce
 	WARN_ON_ONCE(dlist_next - dlist_start != vc4_state->mm.size);
c6ca7ce
 
c6ca7ce
-	if (crtc->state->event) {
c6ca7ce
-		unsigned long flags;
c6ca7ce
-
c6ca7ce
-		crtc->state->event->pipe = drm_crtc_index(crtc);
c6ca7ce
-
c6ca7ce
-		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
c6ca7ce
-
c6ca7ce
-		spin_lock_irqsave(&dev->event_lock, flags);
c6ca7ce
-		vc4_crtc->event = crtc->state->event;
c6ca7ce
-		crtc->state->event = NULL;
c6ca7ce
-
c6ca7ce
-		HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
c6ca7ce
-			  vc4_state->mm.start);
c6ca7ce
-
c6ca7ce
-		spin_unlock_irqrestore(&dev->event_lock, flags);
c6ca7ce
-	} else {
c6ca7ce
-		HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
c6ca7ce
-			  vc4_state->mm.start);
c6ca7ce
-	}
c6ca7ce
+	/* Only update DISPLIST if the CRTC was already running and is not
c6ca7ce
+	 * being disabled.
c6ca7ce
+	 * vc4_crtc_enable() takes care of updating the dlist just after
c6ca7ce
+	 * re-enabling VBLANK interrupts and before enabling the engine.
c6ca7ce
+	 * If the CRTC is being disabled, there's no point in updating this
c6ca7ce
+	 * information.
c6ca7ce
+	 */
c6ca7ce
+	if (crtc->state->active && old_state->active)
c6ca7ce
+		vc4_crtc_update_dlist(crtc);
c6ca7ce
 
c6ca7ce
 	if (debug_dump_regs) {
c6ca7ce
 		DRM_INFO("CRTC %d HVS after:\n", drm_crtc_index(crtc));
c6ca7ce
-- 
c6ca7ce
2.13.0
c6ca7ce