drm/tegra: dc: Wait for idle when disabled
authorThierry Reding <treding@nvidia.com>
Mon, 8 Dec 2014 15:03:53 +0000 (16:03 +0100)
committerThierry Reding <treding@nvidia.com>
Tue, 27 Jan 2015 09:14:43 +0000 (10:14 +0100)
When disabling the display controller, stop it and wait for it to become
idle. Doing so ensures that no further accesses to the framebuffer occur
and the buffers can be safely unmapped or freed.

Signed-off-by: Thierry Reding <treding@nvidia.com>
drivers/gpu/drm/tegra/dc.c

index 915bbdc350ac5434156fcc8fffd8e55bfe0f7d25..f8ad5ed6e0e7987523bcfb302ca8dec0ba1f3857 100644 (file)
@@ -53,6 +53,26 @@ static void tegra_dc_cursor_commit(struct tegra_dc *dc)
        tegra_dc_writel(dc, CURSOR_ACT_REQ, DC_CMD_STATE_CONTROL);
 }
 
+/*
+ * Reads the active copy of a register. This takes the dc->lock spinlock to
+ * prevent races with the VBLANK processing which also needs access to the
+ * active copy of some registers.
+ */
+static u32 tegra_dc_readl_active(struct tegra_dc *dc, unsigned long offset)
+{
+       unsigned long flags;
+       u32 value;
+
+       spin_lock_irqsave(&dc->lock, flags);
+
+       tegra_dc_writel(dc, READ_MUX, DC_CMD_STATE_ACCESS);
+       value = tegra_dc_readl(dc, offset);
+       tegra_dc_writel(dc, 0, DC_CMD_STATE_ACCESS);
+
+       spin_unlock_irqrestore(&dc->lock, flags);
+       return value;
+}
+
 /*
  * Double-buffered registers have two copies: ASSEMBLY and ACTIVE. When the
  * *_ACT_REQ bits are set the ASSEMBLY copy is latched into the ACTIVE copy.
@@ -935,12 +955,47 @@ static const struct drm_crtc_funcs tegra_crtc_funcs = {
        .destroy = tegra_dc_destroy,
 };
 
+static void tegra_dc_stop(struct tegra_dc *dc)
+{
+       u32 value;
+
+       /* stop the display controller */
+       value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
+       value &= ~DISP_CTRL_MODE_MASK;
+       tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
+
+       tegra_dc_commit(dc);
+}
+
+static bool tegra_dc_idle(struct tegra_dc *dc)
+{
+       u32 value;
+
+       value = tegra_dc_readl_active(dc, DC_CMD_DISPLAY_COMMAND);
+
+       return (value & DISP_CTRL_MODE_MASK) == 0;
+}
+
+static int tegra_dc_wait_idle(struct tegra_dc *dc, unsigned long timeout)
+{
+       timeout = jiffies + msecs_to_jiffies(timeout);
+
+       while (time_before(jiffies, timeout)) {
+               if (tegra_dc_idle(dc))
+                       return 0;
+
+               usleep_range(1000, 2000);
+       }
+
+       dev_dbg(dc->dev, "timeout waiting for DC to become idle\n");
+       return -ETIMEDOUT;
+}
+
 static void tegra_crtc_disable(struct drm_crtc *crtc)
 {
        struct tegra_dc *dc = to_tegra_dc(crtc);
        struct drm_device *drm = crtc->dev;
        struct drm_plane *plane;
-       u32 value;
 
        drm_for_each_legacy_plane(plane, &drm->mode_config.plane_list) {
                if (plane->crtc == crtc) {
@@ -954,10 +1009,15 @@ static void tegra_crtc_disable(struct drm_crtc *crtc)
                }
        }
 
-       /* stop the display controller */
-       value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
-       value &= ~DISP_CTRL_MODE_MASK;
-       tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
+       if (!tegra_dc_idle(dc)) {
+               tegra_dc_stop(dc);
+
+               /*
+                * Ignore the return value, there isn't anything useful to do
+                * in case this fails.
+                */
+               tegra_dc_wait_idle(dc, 100);
+       }
 
        drm_crtc_vblank_off(crtc);
        tegra_dc_commit(dc);