drm/i915: s/INTEL_OUTPUT_DISPLAYPORT/INTEL_OUTPUT_DP/
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
index 6b2b965c254d9afb8edddc2ecadb9cb13d863818..907945545f6b5154a61c8f61991301d526bce6ed 100644 (file)
@@ -36,6 +36,7 @@
 #include "intel_drv.h"
 #include <drm/i915_drm.h>
 #include "i915_drv.h"
+#include "i915_gem_dmabuf.h"
 #include "intel_dsi.h"
 #include "i915_trace.h"
 #include <drm/drm_atomic.h>
 #include <drm/drm_rect.h>
 #include <linux/dma_remapping.h>
 #include <linux/reservation.h>
-#include <linux/dma-buf.h>
+
+static bool is_mmio_work(struct intel_flip_work *work)
+{
+       return work->mmio_work.func;
+}
 
 /* Primary plane formats for gen <= 3 */
 static const uint32_t i8xx_primary_formats[] = {
@@ -108,6 +113,8 @@ static void vlv_prepare_pll(struct intel_crtc *crtc,
                            const struct intel_crtc_state *pipe_config);
 static void chv_prepare_pll(struct intel_crtc *crtc,
                            const struct intel_crtc_state *pipe_config);
+static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
+static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
 static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
        struct intel_crtc_state *crtc_state);
 static void skylake_pfit_enable(struct intel_crtc *crtc);
@@ -116,9 +123,7 @@ static void ironlake_pfit_enable(struct intel_crtc *crtc);
 static void intel_modeset_setup_hw_state(struct drm_device *dev);
 static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
 static int ilk_max_pixel_rate(struct drm_atomic_state *state);
-static void intel_modeset_verify_crtc(struct drm_crtc *crtc,
-                                     struct drm_crtc_state *old_state,
-                                     struct drm_crtc_state *new_state);
+static int bxt_calc_cdclk(int max_pixclk);
 
 struct intel_limit {
        struct {
@@ -525,52 +530,6 @@ needs_modeset(struct drm_crtc_state *state)
        return drm_atomic_crtc_needs_modeset(state);
 }
 
-/**
- * Returns whether any output on the specified pipe is of the specified type
- */
-bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
-{
-       struct drm_device *dev = crtc->base.dev;
-       struct intel_encoder *encoder;
-
-       for_each_encoder_on_crtc(dev, &crtc->base, encoder)
-               if (encoder->type == type)
-                       return true;
-
-       return false;
-}
-
-/**
- * Returns whether any output on the specified pipe will have the specified
- * type after a staged modeset is complete, i.e., the same as
- * intel_pipe_has_type() but looking at encoder->new_crtc instead of
- * encoder->crtc.
- */
-static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
-                                     int type)
-{
-       struct drm_atomic_state *state = crtc_state->base.state;
-       struct drm_connector *connector;
-       struct drm_connector_state *connector_state;
-       struct intel_encoder *encoder;
-       int i, num_connectors = 0;
-
-       for_each_connector_in_state(state, connector, connector_state, i) {
-               if (connector_state->crtc != crtc_state->base.crtc)
-                       continue;
-
-               num_connectors++;
-
-               encoder = to_intel_encoder(connector_state->best_encoder);
-               if (encoder->type == type)
-                       return true;
-       }
-
-       WARN_ON(num_connectors == 0);
-
-       return false;
-}
-
 /*
  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
@@ -683,7 +642,7 @@ i9xx_select_p2_div(const struct intel_limit *limit,
 {
        struct drm_device *dev = crtc_state->base.crtc->dev;
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
                /*
                 * For LVDS just rely on its current settings for dual-channel.
                 * We haven't figured out how to reliably set up different
@@ -1075,7 +1034,7 @@ enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
 
 static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        i915_reg_t reg = PIPEDSL(pipe);
        u32 line1, line2;
        u32 line_mask;
@@ -1111,7 +1070,7 @@ static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
 static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
        enum pipe pipe = crtc->pipe;
 
@@ -1119,8 +1078,9 @@ static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
                i915_reg_t reg = PIPECONF(cpu_transcoder);
 
                /* Wait for the Pipe State to go off */
-               if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
-                            100))
+               if (intel_wait_for_register(dev_priv,
+                                           reg, I965_PIPECONF_ACTIVE, 0,
+                                           100))
                        WARN(1, "pipe_off wait timed out\n");
        } else {
                /* Wait for the display line to settle */
@@ -1229,7 +1189,7 @@ void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
 void assert_panel_unlocked(struct drm_i915_private *dev_priv,
                           enum pipe pipe)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        i915_reg_t pp_reg;
        u32 val;
        enum pipe panel_pipe = PIPE_A;
@@ -1271,7 +1231,7 @@ void assert_panel_unlocked(struct drm_i915_private *dev_priv,
 static void assert_cursor(struct drm_i915_private *dev_priv,
                          enum pipe pipe, bool state)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        bool cur_state;
 
        if (IS_845G(dev) || IS_I865G(dev))
@@ -1333,7 +1293,7 @@ static void assert_plane(struct drm_i915_private *dev_priv,
 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
                                   enum pipe pipe)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        int i;
 
        /* Primary planes are fixed to pipes on gen4+ */
@@ -1359,7 +1319,7 @@ static void assert_planes_disabled(struct drm_i915_private *dev_priv,
 static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
                                    enum pipe pipe)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        int sprite;
 
        if (INTEL_INFO(dev)->gen >= 9) {
@@ -1539,7 +1499,11 @@ static void _vlv_enable_pll(struct intel_crtc *crtc,
        POSTING_READ(DPLL(pipe));
        udelay(150);
 
-       if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
+       if (intel_wait_for_register(dev_priv,
+                                   DPLL(pipe),
+                                   DPLL_LOCK_VLV,
+                                   DPLL_LOCK_VLV,
+                                   1))
                DRM_ERROR("DPLL %d failed to lock\n", pipe);
 }
 
@@ -1588,7 +1552,9 @@ static void _chv_enable_pll(struct intel_crtc *crtc,
        I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
 
        /* Check PLL is locked */
-       if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
+       if (intel_wait_for_register(dev_priv,
+                                   DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
+                                   1))
                DRM_ERROR("PLL %d failed to lock\n", pipe);
 }
 
@@ -1634,9 +1600,10 @@ static int intel_num_dvo_pipes(struct drm_device *dev)
        struct intel_crtc *crtc;
        int count = 0;
 
-       for_each_intel_crtc(dev, crtc)
+       for_each_intel_crtc(dev, crtc) {
                count += crtc->base.state->active &&
-                       intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
+                       intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
+       }
 
        return count;
 }
@@ -1644,7 +1611,7 @@ static int intel_num_dvo_pipes(struct drm_device *dev)
 static void i9xx_enable_pll(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        i915_reg_t reg = DPLL(crtc->pipe);
        u32 dpll = crtc->config->dpll_hw_state.dpll;
 
@@ -1716,12 +1683,12 @@ static void i9xx_enable_pll(struct intel_crtc *crtc)
 static void i9xx_disable_pll(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe = crtc->pipe;
 
        /* Disable DVO 2x clock on both PLLs if necessary */
        if (IS_I830(dev) &&
-           intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
+           intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
            !intel_num_dvo_pipes(dev)) {
                I915_WRITE(DPLL(PIPE_B),
                           I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
@@ -1808,7 +1775,9 @@ void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
                BUG();
        }
 
-       if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
+       if (intel_wait_for_register(dev_priv,
+                                   dpll_reg, port_mask, expected_mask,
+                                   1000))
                WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
                     port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
 }
@@ -1816,7 +1785,7 @@ void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
                                           enum pipe pipe)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        i915_reg_t reg;
@@ -1849,7 +1818,7 @@ static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
                 * here for both 8bpc and 12bpc.
                 */
                val &= ~PIPECONF_BPC_MASK;
-               if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
+               if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
                        val |= PIPECONF_8BPC;
                else
                        val |= pipeconf_val & PIPECONF_BPC_MASK;
@@ -1858,7 +1827,7 @@ static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
        val &= ~TRANS_INTERLACE_MASK;
        if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
                if (HAS_PCH_IBX(dev_priv) &&
-                   intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
+                   intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
                        val |= TRANS_LEGACY_INTERLACED_ILK;
                else
                        val |= TRANS_INTERLACED;
@@ -1866,7 +1835,9 @@ static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
                val |= TRANS_PROGRESSIVE;
 
        I915_WRITE(reg, val | TRANS_ENABLE);
-       if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
+       if (intel_wait_for_register(dev_priv,
+                                   reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
+                                   100))
                DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
 }
 
@@ -1894,14 +1865,18 @@ static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
                val |= TRANS_PROGRESSIVE;
 
        I915_WRITE(LPT_TRANSCONF, val);
-       if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
+       if (intel_wait_for_register(dev_priv,
+                                   LPT_TRANSCONF,
+                                   TRANS_STATE_ENABLE,
+                                   TRANS_STATE_ENABLE,
+                                   100))
                DRM_ERROR("Failed to enable PCH transcoder\n");
 }
 
 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
                                            enum pipe pipe)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        i915_reg_t reg;
        uint32_t val;
 
@@ -1917,7 +1892,9 @@ static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
        val &= ~TRANS_ENABLE;
        I915_WRITE(reg, val);
        /* wait for PCH transcoder off, transcoder state */
-       if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
+       if (intel_wait_for_register(dev_priv,
+                                   reg, TRANS_STATE_ENABLE, 0,
+                                   50))
                DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
 
        if (HAS_PCH_CPT(dev)) {
@@ -1937,7 +1914,9 @@ static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
        val &= ~TRANS_ENABLE;
        I915_WRITE(LPT_TRANSCONF, val);
        /* wait for PCH transcoder off, transcoder state */
-       if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
+       if (intel_wait_for_register(dev_priv,
+                                   LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
+                                   50))
                DRM_ERROR("Failed to disable PCH transcoder\n");
 
        /* Workaround: clear timing override bit. */
@@ -1956,7 +1935,7 @@ static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
 static void intel_enable_pipe(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe = crtc->pipe;
        enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
        enum pipe pch_transcoder;
@@ -2029,7 +2008,7 @@ static void intel_enable_pipe(struct intel_crtc *crtc)
  */
 static void intel_disable_pipe(struct intel_crtc *crtc)
 {
-       struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
        enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
        enum pipe pipe = crtc->pipe;
        i915_reg_t reg;
@@ -2240,7 +2219,7 @@ intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
                           unsigned int rotation)
 {
        struct drm_device *dev = fb->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct drm_i915_gem_object *obj = intel_fb_obj(fb);
        struct i915_ggtt_view view;
        u32 alignment;
@@ -2523,12 +2502,26 @@ out_unref_obj:
        return false;
 }
 
+/* Update plane->state->fb to match plane->fb after driver-internal updates */
+static void
+update_state_fb(struct drm_plane *plane)
+{
+       if (plane->fb == plane->state->fb)
+               return;
+
+       if (plane->state->fb)
+               drm_framebuffer_unreference(plane->state->fb);
+       plane->state->fb = plane->fb;
+       if (plane->state->fb)
+               drm_framebuffer_reference(plane->state->fb);
+}
+
 static void
 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
                             struct intel_initial_plane_config *plane_config)
 {
        struct drm_device *dev = intel_crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct drm_crtc *c;
        struct intel_crtc *i;
        struct drm_i915_gem_object *obj;
@@ -2624,7 +2617,7 @@ static void i9xx_update_primary_plane(struct drm_plane *primary,
                                      const struct intel_plane_state *plane_state)
 {
        struct drm_device *dev = primary->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
        struct drm_framebuffer *fb = plane_state->base.fb;
        struct drm_i915_gem_object *obj = intel_fb_obj(fb);
@@ -2737,7 +2730,7 @@ static void i9xx_disable_primary_plane(struct drm_plane *primary,
                                       struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int plane = intel_crtc->plane;
 
@@ -2754,7 +2747,7 @@ static void ironlake_update_primary_plane(struct drm_plane *primary,
                                          const struct intel_plane_state *plane_state)
 {
        struct drm_device *dev = primary->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
        struct drm_framebuffer *fb = plane_state->base.fb;
        struct drm_i915_gem_object *obj = intel_fb_obj(fb);
@@ -2882,7 +2875,7 @@ u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
 static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
 {
        struct drm_device *dev = intel_crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
        I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
@@ -2992,7 +2985,7 @@ static void skylake_update_primary_plane(struct drm_plane *plane,
                                         const struct intel_plane_state *plane_state)
 {
        struct drm_device *dev = plane->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
        struct drm_framebuffer *fb = plane_state->base.fb;
        struct drm_i915_gem_object *obj = intel_fb_obj(fb);
@@ -3076,7 +3069,7 @@ static void skylake_disable_primary_plane(struct drm_plane *primary,
                                          struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = to_intel_crtc(crtc)->pipe;
 
        I915_WRITE(PLANE_CTL(pipe, 0), 0);
@@ -3095,6 +3088,14 @@ intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
        return -ENODEV;
 }
 
+static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
+{
+       struct intel_crtc *crtc;
+
+       for_each_intel_crtc(&dev_priv->drm, crtc)
+               intel_finish_page_flip_cs(dev_priv, crtc->pipe);
+}
+
 static void intel_update_primary_planes(struct drm_device *dev)
 {
        struct drm_crtc *crtc;
@@ -3125,16 +3126,23 @@ void intel_prepare_reset(struct drm_i915_private *dev_priv)
        if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
                return;
 
-       drm_modeset_lock_all(dev_priv->dev);
+       drm_modeset_lock_all(&dev_priv->drm);
        /*
         * Disabling the crtcs gracefully seems nicer. Also the
         * g33 docs say we should at least disable all the planes.
         */
-       intel_display_suspend(dev_priv->dev);
+       intel_display_suspend(&dev_priv->drm);
 }
 
 void intel_finish_reset(struct drm_i915_private *dev_priv)
 {
+       /*
+        * Flips in the rings will be nuked by the reset,
+        * so complete all pending flips so that user space
+        * will get its events and not get stuck.
+        */
+       intel_complete_page_flips(dev_priv);
+
        /* no reset support for gen2 */
        if (IS_GEN2(dev_priv))
                return;
@@ -3150,7 +3158,7 @@ void intel_finish_reset(struct drm_i915_private *dev_priv)
                 * FIXME: Atomic will make this obsolete since we won't schedule
                 * CS-based flips (which might get lost in gpu resets) any more.
                 */
-               intel_update_primary_planes(dev_priv->dev);
+               intel_update_primary_planes(&dev_priv->drm);
                return;
        }
 
@@ -3161,30 +3169,43 @@ void intel_finish_reset(struct drm_i915_private *dev_priv)
        intel_runtime_pm_disable_interrupts(dev_priv);
        intel_runtime_pm_enable_interrupts(dev_priv);
 
-       intel_modeset_init_hw(dev_priv->dev);
+       intel_modeset_init_hw(&dev_priv->drm);
 
        spin_lock_irq(&dev_priv->irq_lock);
        if (dev_priv->display.hpd_irq_setup)
                dev_priv->display.hpd_irq_setup(dev_priv);
        spin_unlock_irq(&dev_priv->irq_lock);
 
-       intel_display_resume(dev_priv->dev);
+       intel_display_resume(&dev_priv->drm);
 
        intel_hpd_init(dev_priv);
 
-       drm_modeset_unlock_all(dev_priv->dev);
+       drm_modeset_unlock_all(&dev_priv->drm);
 }
 
 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
 {
-       return !list_empty_careful(&to_intel_crtc(crtc)->flip_work);
+       struct drm_device *dev = crtc->dev;
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       unsigned reset_counter;
+       bool pending;
+
+       reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
+       if (intel_crtc->reset_counter != reset_counter)
+               return false;
+
+       spin_lock_irq(&dev->event_lock);
+       pending = to_intel_crtc(crtc)->flip_work != NULL;
+       spin_unlock_irq(&dev->event_lock);
+
+       return pending;
 }
 
 static void intel_update_pipe_config(struct intel_crtc *crtc,
                                     struct intel_crtc_state *old_crtc_state)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc_state *pipe_config =
                to_intel_crtc_state(crtc->base.state);
 
@@ -3225,7 +3246,7 @@ static void intel_update_pipe_config(struct intel_crtc *crtc,
 static void intel_fdi_normal_train(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        i915_reg_t reg;
@@ -3268,7 +3289,7 @@ static void intel_fdi_normal_train(struct drm_crtc *crtc)
 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        i915_reg_t reg;
@@ -3369,7 +3390,7 @@ static const int snb_b_fdi_train_param[] = {
 static void gen6_fdi_link_train(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        i915_reg_t reg;
@@ -3502,7 +3523,7 @@ static void gen6_fdi_link_train(struct drm_crtc *crtc)
 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        i915_reg_t reg;
@@ -3621,7 +3642,7 @@ train_done:
 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
 {
        struct drm_device *dev = intel_crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = intel_crtc->pipe;
        i915_reg_t reg;
        u32 temp;
@@ -3658,7 +3679,7 @@ static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
 {
        struct drm_device *dev = intel_crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = intel_crtc->pipe;
        i915_reg_t reg;
        u32 temp;
@@ -3688,7 +3709,7 @@ static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
 static void ironlake_fdi_disable(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        i915_reg_t reg;
@@ -3753,7 +3774,7 @@ bool intel_has_pending_fb_unpin(struct drm_device *dev)
                if (atomic_read(&crtc->unpin_work_count) == 0)
                        continue;
 
-               if (!list_empty_careful(&crtc->flip_work))
+               if (crtc->flip_work)
                        intel_wait_for_vblank(dev, crtc->pipe);
 
                return true;
@@ -3762,36 +3783,29 @@ bool intel_has_pending_fb_unpin(struct drm_device *dev)
        return false;
 }
 
-static void page_flip_completed(struct intel_crtc *intel_crtc, struct intel_flip_work *work)
+static void page_flip_completed(struct intel_crtc *intel_crtc)
 {
        struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
-       struct drm_plane_state *new_plane_state;
-       struct drm_plane *primary = intel_crtc->base.primary;
+       struct intel_flip_work *work = intel_crtc->flip_work;
+
+       intel_crtc->flip_work = NULL;
 
        if (work->event)
                drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
 
        drm_crtc_vblank_put(&intel_crtc->base);
 
-       new_plane_state = &work->old_plane_state[0]->base;
-       if (work->num_planes >= 1 &&
-           new_plane_state->plane == primary &&
-           new_plane_state->fb)
-               trace_i915_flip_complete(intel_crtc->plane,
-                                        intel_fb_obj(new_plane_state->fb));
-
-       if (work->can_async_unpin) {
-               list_del_init(&work->head);
-               wake_up_all(&dev_priv->pending_flip_queue);
-       }
-
+       wake_up_all(&dev_priv->pending_flip_queue);
        queue_work(dev_priv->wq, &work->unpin_work);
+
+       trace_i915_flip_complete(intel_crtc->plane,
+                                work->pending_flip_obj);
 }
 
 static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        long ret;
 
        WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
@@ -3804,7 +3818,18 @@ static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
        if (ret < 0)
                return ret;
 
-       WARN(ret == 0, "Stuck page flip\n");
+       if (ret == 0) {
+               struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+               struct intel_flip_work *work;
+
+               spin_lock_irq(&dev->event_lock);
+               work = intel_crtc->flip_work;
+               if (work && !is_mmio_work(work)) {
+                       WARN_ONCE(1, "Removing stuck page flip\n");
+                       page_flip_completed(intel_crtc);
+               }
+               spin_unlock_irq(&dev->event_lock);
+       }
 
        return 0;
 }
@@ -3943,7 +3968,7 @@ static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
                                                enum pipe pch_transcoder)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
 
        I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
@@ -3965,7 +3990,7 @@ static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
 
 static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t temp;
 
        temp = I915_READ(SOUTH_CHICKEN1);
@@ -4015,7 +4040,7 @@ intel_trans_dp_port_sel(struct drm_crtc *crtc)
        struct intel_encoder *encoder;
 
        for_each_encoder_on_crtc(dev, crtc, encoder) {
-               if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
+               if (encoder->type == INTEL_OUTPUT_DP ||
                    encoder->type == INTEL_OUTPUT_EDP)
                        return enc_to_dig_port(&encoder->base)->port;
        }
@@ -4034,7 +4059,7 @@ intel_trans_dp_port_sel(struct drm_crtc *crtc)
 static void ironlake_pch_enable(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        u32 temp;
@@ -4084,7 +4109,7 @@ static void ironlake_pch_enable(struct drm_crtc *crtc)
        intel_fdi_normal_train(crtc);
 
        /* For PCH DP, enable TRANS_DP_CTL */
-       if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
+       if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
                const struct drm_display_mode *adjusted_mode =
                        &intel_crtc->config->base.adjusted_mode;
                u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
@@ -4124,7 +4149,7 @@ static void ironlake_pch_enable(struct drm_crtc *crtc)
 static void lpt_pch_enable(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
 
@@ -4140,7 +4165,7 @@ static void lpt_pch_enable(struct drm_crtc *crtc)
 
 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        i915_reg_t dslreg = PIPEDSL(pipe);
        u32 temp;
 
@@ -4227,8 +4252,9 @@ int skl_update_scaler_crtc(struct intel_crtc_state *state)
        struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
        const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
 
-       DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
-                     intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
+       DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
+                     intel_crtc->base.base.id, intel_crtc->base.name,
+                     intel_crtc->pipe, SKL_CRTC_INDEX);
 
        return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
                &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
@@ -4258,9 +4284,9 @@ static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
 
        bool force_detach = !fb || !plane_state->visible;
 
-       DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
-                     intel_plane->base.base.id, intel_crtc->pipe,
-                     drm_plane_index(&intel_plane->base));
+       DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
+                     intel_plane->base.base.id, intel_plane->base.name,
+                     intel_crtc->pipe, drm_plane_index(&intel_plane->base));
 
        ret = skl_update_scaler(crtc_state, force_detach,
                                drm_plane_index(&intel_plane->base),
@@ -4276,8 +4302,9 @@ static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
 
        /* check colorkey */
        if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
-               DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
-                             intel_plane->base.base.id);
+               DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
+                             intel_plane->base.base.id,
+                             intel_plane->base.name);
                return -EINVAL;
        }
 
@@ -4296,8 +4323,9 @@ static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
        case DRM_FORMAT_VYUY:
                break;
        default:
-               DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
-                       intel_plane->base.base.id, fb->base.id, fb->pixel_format);
+               DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
+                             intel_plane->base.base.id, intel_plane->base.name,
+                             fb->base.id, fb->pixel_format);
                return -EINVAL;
        }
 
@@ -4315,7 +4343,7 @@ static void skylake_scaler_disable(struct intel_crtc *crtc)
 static void skylake_pfit_enable(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = crtc->pipe;
        struct intel_crtc_scaler_state *scaler_state =
                &crtc->config->scaler_state;
@@ -4343,7 +4371,7 @@ static void skylake_pfit_enable(struct intel_crtc *crtc)
 static void ironlake_pfit_enable(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = crtc->pipe;
 
        if (crtc->config->pch_pfit.enabled) {
@@ -4364,7 +4392,7 @@ static void ironlake_pfit_enable(struct intel_crtc *crtc)
 void hsw_enable_ips(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        if (!crtc->config->ips_enabled)
                return;
@@ -4392,7 +4420,9 @@ void hsw_enable_ips(struct intel_crtc *crtc)
                 * and don't wait for vblanks until the end of crtc_enable, then
                 * the HW state readout code will complain that the expected
                 * IPS_CTL value is not the one we read. */
-               if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
+               if (intel_wait_for_register(dev_priv,
+                                           IPS_CTL, IPS_ENABLE, IPS_ENABLE,
+                                           50))
                        DRM_ERROR("Timed out waiting for IPS enable\n");
        }
 }
@@ -4400,7 +4430,7 @@ void hsw_enable_ips(struct intel_crtc *crtc)
 void hsw_disable_ips(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        if (!crtc->config->ips_enabled)
                return;
@@ -4411,7 +4441,9 @@ void hsw_disable_ips(struct intel_crtc *crtc)
                WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
                mutex_unlock(&dev_priv->rps.hw_lock);
                /* wait for pcode to finish disabling IPS, which may take up to 42ms */
-               if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
+               if (intel_wait_for_register(dev_priv,
+                                           IPS_CTL, IPS_ENABLE, 0,
+                                           42))
                        DRM_ERROR("Timed out waiting for IPS disable\n");
        } else {
                I915_WRITE(IPS_CTL, 0);
@@ -4426,7 +4458,7 @@ static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
 {
        if (intel_crtc->overlay) {
                struct drm_device *dev = intel_crtc->base.dev;
-               struct drm_i915_private *dev_priv = dev->dev_private;
+               struct drm_i915_private *dev_priv = to_i915(dev);
 
                mutex_lock(&dev->struct_mutex);
                dev_priv->mm.interruptible = false;
@@ -4454,7 +4486,7 @@ static void
 intel_post_enable_primary(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
 
@@ -4486,7 +4518,7 @@ static void
 intel_pre_disable_primary(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
 
@@ -4513,7 +4545,7 @@ static void
 intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
 
@@ -4535,11 +4567,44 @@ intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
        }
 }
 
+static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
+{
+       struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
+       struct drm_atomic_state *old_state = old_crtc_state->base.state;
+       struct intel_crtc_state *pipe_config =
+               to_intel_crtc_state(crtc->base.state);
+       struct drm_device *dev = crtc->base.dev;
+       struct drm_plane *primary = crtc->base.primary;
+       struct drm_plane_state *old_pri_state =
+               drm_atomic_get_existing_plane_state(old_state, primary);
+
+       intel_frontbuffer_flip(dev, pipe_config->fb_bits);
+
+       crtc->wm.cxsr_allowed = true;
+
+       if (pipe_config->update_wm_post && pipe_config->base.active)
+               intel_update_watermarks(&crtc->base);
+
+       if (old_pri_state) {
+               struct intel_plane_state *primary_state =
+                       to_intel_plane_state(primary->state);
+               struct intel_plane_state *old_primary_state =
+                       to_intel_plane_state(old_pri_state);
+
+               intel_fbc_post_update(crtc);
+
+               if (primary_state->visible &&
+                   (needs_modeset(&pipe_config->base) ||
+                    !old_primary_state->visible))
+                       intel_post_enable_primary(&crtc->base);
+       }
+}
+
 static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
 {
        struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc_state *pipe_config =
                to_intel_crtc_state(crtc->base.state);
        struct drm_atomic_state *old_state = old_crtc_state->base.state;
@@ -4561,7 +4626,7 @@ static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
                        intel_pre_disable_primary(&crtc->base);
        }
 
-       if (pipe_config->disable_cxsr) {
+       if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
                crtc->wm.cxsr_allowed = false;
 
                /*
@@ -4642,7 +4707,7 @@ static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask
 static void ironlake_crtc_enable(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_encoder *encoder;
        int pipe = intel_crtc->pipe;
@@ -4670,7 +4735,7 @@ static void ironlake_crtc_enable(struct drm_crtc *crtc)
        if (intel_crtc->config->has_pch_encoder)
                intel_prepare_shared_dpll(intel_crtc);
 
-       if (intel_crtc->config->has_dp_encoder)
+       if (intel_crtc_has_dp_encoder(intel_crtc->config))
                intel_dp_set_m_n(intel_crtc, M1_N1);
 
        intel_set_pipe_timings(intel_crtc);
@@ -4739,7 +4804,7 @@ static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
 static void haswell_crtc_enable(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_encoder *encoder;
        int pipe = intel_crtc->pipe, hsw_workaround_pipe;
@@ -4754,10 +4819,14 @@ static void haswell_crtc_enable(struct drm_crtc *crtc)
                intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
                                                      false);
 
+       for_each_encoder_on_crtc(dev, crtc, encoder)
+               if (encoder->pre_pll_enable)
+                       encoder->pre_pll_enable(encoder);
+
        if (intel_crtc->config->shared_dpll)
                intel_enable_shared_dpll(intel_crtc);
 
-       if (intel_crtc->config->has_dp_encoder)
+       if (intel_crtc_has_dp_encoder(intel_crtc->config))
                intel_dp_set_m_n(intel_crtc, M1_N1);
 
        if (!intel_crtc->config->has_dsi_encoder)
@@ -4859,7 +4928,7 @@ static void haswell_crtc_enable(struct drm_crtc *crtc)
 static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = crtc->pipe;
 
        /* To avoid upsetting the power well on haswell only disable the pfit if
@@ -4874,7 +4943,7 @@ static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
 static void ironlake_crtc_disable(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_encoder *encoder;
        int pipe = intel_crtc->pipe;
@@ -4937,7 +5006,7 @@ static void ironlake_crtc_disable(struct drm_crtc *crtc)
 static void haswell_crtc_disable(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_encoder *encoder;
        enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
@@ -4989,7 +5058,7 @@ static void haswell_crtc_disable(struct drm_crtc *crtc)
 static void i9xx_pfit_enable(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc_state *pipe_config = crtc->config;
 
        if (!pipe_config->gmch_pfit.control)
@@ -5059,7 +5128,7 @@ intel_display_port_power_domain(struct intel_encoder *intel_encoder)
        case INTEL_OUTPUT_UNKNOWN:
                /* Only DDI platforms should ever use this output type */
                WARN_ON_ONCE(!HAS_DDI(dev));
-       case INTEL_OUTPUT_DISPLAYPORT:
+       case INTEL_OUTPUT_DP:
        case INTEL_OUTPUT_HDMI:
        case INTEL_OUTPUT_EDP:
                intel_dig_port = enc_to_dig_port(&intel_encoder->base);
@@ -5093,7 +5162,7 @@ intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
                 * run the DP detection too.
                 */
                WARN_ON_ONCE(!HAS_DDI(dev));
-       case INTEL_OUTPUT_DISPLAYPORT:
+       case INTEL_OUTPUT_DP:
        case INTEL_OUTPUT_EDP:
                intel_dig_port = enc_to_dig_port(&intel_encoder->base);
                return port_to_aux_power_domain(intel_dig_port->port);
@@ -5141,24 +5210,21 @@ static unsigned long
 modeset_get_crtc_power_domains(struct drm_crtc *crtc,
                               struct intel_crtc_state *crtc_state)
 {
-       struct drm_i915_private *dev_priv = crtc->dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(crtc->dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        enum intel_display_power_domain domain;
-       unsigned long domains, new_domains, old_domains, ms_domain = 0;
+       unsigned long domains, new_domains, old_domains;
 
        old_domains = intel_crtc->enabled_power_domains;
        intel_crtc->enabled_power_domains = new_domains =
                get_crtc_power_domains(crtc, crtc_state);
 
-       if (needs_modeset(&crtc_state->base))
-               ms_domain = BIT(POWER_DOMAIN_MODESET);
-
-       domains = (new_domains & ~old_domains) | ms_domain;
+       domains = new_domains & ~old_domains;
 
        for_each_power_domain(domain, domains)
                intel_display_power_get(dev_priv, domain);
 
-       return (old_domains & ~new_domains) | ms_domain;
+       return old_domains & ~new_domains;
 }
 
 static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
@@ -5189,14 +5255,14 @@ static int skl_calc_cdclk(int max_pixclk, int vco);
 
 static void intel_update_max_cdclk(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
                u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
                int max_cdclk, vco;
 
                vco = dev_priv->skl_preferred_vco_freq;
-               WARN_ON(vco != 8100 && vco != 8640);
+               WARN_ON(vco != 8100000 && vco != 8640000);
 
                /*
                 * Use the lower (vco 8640) cdclk values as a
@@ -5204,13 +5270,13 @@ static void intel_update_max_cdclk(struct drm_device *dev)
                 * if the preferred vco is 8100 instead.
                 */
                if (limit == SKL_DFSM_CDCLK_LIMIT_675)
-                       max_cdclk = 617140;
+                       max_cdclk = 617143;
                else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
                        max_cdclk = 540000;
                else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
                        max_cdclk = 432000;
                else
-                       max_cdclk = 308570;
+                       max_cdclk = 308571;
 
                dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
        } else if (IS_BROXTON(dev)) {
@@ -5250,13 +5316,14 @@ static void intel_update_max_cdclk(struct drm_device *dev)
 
 static void intel_update_cdclk(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
 
-       if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
-               DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d MHz\n",
-                                dev_priv->cdclk_freq, dev_priv->skl_vco_freq);
+       if (INTEL_GEN(dev_priv) >= 9)
+               DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
+                                dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
+                                dev_priv->cdclk_pll.ref);
        else
                DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
                                 dev_priv->cdclk_freq);
@@ -5277,51 +5344,99 @@ static int skl_cdclk_decimal(int cdclk)
        return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
 }
 
-static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
+static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
 {
-       uint32_t divider;
-       uint32_t ratio;
-       uint32_t current_cdclk;
-       int ret;
+       int ratio;
+
+       if (cdclk == dev_priv->cdclk_pll.ref)
+               return 0;
 
-       /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
        switch (cdclk) {
+       default:
+               MISSING_CASE(cdclk);
        case 144000:
+       case 288000:
+       case 384000:
+       case 576000:
+               ratio = 60;
+               break;
+       case 624000:
+               ratio = 65;
+               break;
+       }
+
+       return dev_priv->cdclk_pll.ref * ratio;
+}
+
+static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
+{
+       I915_WRITE(BXT_DE_PLL_ENABLE, 0);
+
+       /* Timeout 200us */
+       if (intel_wait_for_register(dev_priv,
+                                   BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
+                                   1))
+               DRM_ERROR("timeout waiting for DE PLL unlock\n");
+
+       dev_priv->cdclk_pll.vco = 0;
+}
+
+static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
+{
+       int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
+       u32 val;
+
+       val = I915_READ(BXT_DE_PLL_CTL);
+       val &= ~BXT_DE_PLL_RATIO_MASK;
+       val |= BXT_DE_PLL_RATIO(ratio);
+       I915_WRITE(BXT_DE_PLL_CTL, val);
+
+       I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
+
+       /* Timeout 200us */
+       if (intel_wait_for_register(dev_priv,
+                                   BXT_DE_PLL_ENABLE,
+                                   BXT_DE_PLL_LOCK,
+                                   BXT_DE_PLL_LOCK,
+                                   1))
+               DRM_ERROR("timeout waiting for DE PLL lock\n");
+
+       dev_priv->cdclk_pll.vco = vco;
+}
+
+static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
+{
+       u32 val, divider;
+       int vco, ret;
+
+       vco = bxt_de_pll_vco(dev_priv, cdclk);
+
+       DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
+
+       /* cdclk = vco / 2 / div{1,1.5,2,4} */
+       switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
+       case 8:
                divider = BXT_CDCLK_CD2X_DIV_SEL_4;
-               ratio = BXT_DE_PLL_RATIO(60);
                break;
-       case 288000:
+       case 4:
                divider = BXT_CDCLK_CD2X_DIV_SEL_2;
-               ratio = BXT_DE_PLL_RATIO(60);
                break;
-       case 384000:
+       case 3:
                divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
-               ratio = BXT_DE_PLL_RATIO(60);
-               break;
-       case 576000:
-               divider = BXT_CDCLK_CD2X_DIV_SEL_1;
-               ratio = BXT_DE_PLL_RATIO(60);
                break;
-       case 624000:
+       case 2:
                divider = BXT_CDCLK_CD2X_DIV_SEL_1;
-               ratio = BXT_DE_PLL_RATIO(65);
-               break;
-       case 19200:
-               /*
-                * Bypass frequency with DE PLL disabled. Init ratio, divider
-                * to suppress GCC warning.
-                */
-               ratio = 0;
-               divider = 0;
                break;
        default:
-               DRM_ERROR("unsupported CDCLK freq %d", cdclk);
+               WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
+               WARN_ON(vco != 0);
 
-               return;
+               divider = BXT_CDCLK_CD2X_DIV_SEL_1;
+               break;
        }
 
-       mutex_lock(&dev_priv->rps.hw_lock);
        /* Inform power controller of upcoming frequency change */
+       mutex_lock(&dev_priv->rps.hw_lock);
        ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
                                      0x80000000);
        mutex_unlock(&dev_priv->rps.hw_lock);
@@ -5332,52 +5447,26 @@ static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
                return;
        }
 
-       current_cdclk = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
-       /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
-       current_cdclk = current_cdclk * 500 + 1000;
+       if (dev_priv->cdclk_pll.vco != 0 &&
+           dev_priv->cdclk_pll.vco != vco)
+               bxt_de_pll_disable(dev_priv);
 
+       if (dev_priv->cdclk_pll.vco != vco)
+               bxt_de_pll_enable(dev_priv, vco);
+
+       val = divider | skl_cdclk_decimal(cdclk);
        /*
-        * DE PLL has to be disabled when
-        * - setting to 19.2MHz (bypass, PLL isn't used)
-        * - before setting to 624MHz (PLL needs toggling)
-        * - before setting to any frequency from 624MHz (PLL needs toggling)
-        */
-       if (cdclk == 19200 || cdclk == 624000 ||
-           current_cdclk == 624000) {
-               I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
-               /* Timeout 200us */
-               if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
-                            1))
-                       DRM_ERROR("timout waiting for DE PLL unlock\n");
-       }
-
-       if (cdclk != 19200) {
-               uint32_t val;
-
-               val = I915_READ(BXT_DE_PLL_CTL);
-               val &= ~BXT_DE_PLL_RATIO_MASK;
-               val |= ratio;
-               I915_WRITE(BXT_DE_PLL_CTL, val);
-
-               I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
-               /* Timeout 200us */
-               if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
-                       DRM_ERROR("timeout waiting for DE PLL lock\n");
-
-               val = divider | skl_cdclk_decimal(cdclk);
-               /*
-                * FIXME if only the cd2x divider needs changing, it could be done
-                * without shutting off the pipe (if only one pipe is active).
-                */
-               val |= BXT_CDCLK_CD2X_PIPE_NONE;
-               /*
-                * Disable SSA Precharge when CD clock frequency < 500 MHz,
-                * enable otherwise.
-                */
-               if (cdclk >= 500000)
-                       val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
-               I915_WRITE(CDCLK_CTL, val);
-       }
+        * FIXME if only the cd2x divider needs changing, it could be done
+        * without shutting off the pipe (if only one pipe is active).
+        */
+       val |= BXT_CDCLK_CD2X_PIPE_NONE;
+       /*
+        * Disable SSA Precharge when CD clock frequency < 500 MHz,
+        * enable otherwise.
+        */
+       if (cdclk >= 500000)
+               val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
+       I915_WRITE(CDCLK_CTL, val);
 
        mutex_lock(&dev_priv->rps.hw_lock);
        ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
@@ -5390,63 +5479,88 @@ static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
                return;
        }
 
-       intel_update_cdclk(dev_priv->dev);
+       intel_update_cdclk(&dev_priv->drm);
 }
 
-static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
+static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
 {
-       if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
-               return false;
+       u32 cdctl, expected;
 
-       /* TODO: Check for a valid CDCLK rate */
+       intel_update_cdclk(&dev_priv->drm);
 
-       return true;
-}
+       if (dev_priv->cdclk_pll.vco == 0 ||
+           dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
+               goto sanitize;
 
-bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
-{
-       return broxton_cdclk_is_enabled(dev_priv);
+       /* DPLL okay; verify the cdclock
+        *
+        * Some BIOS versions leave an incorrect decimal frequency value and
+        * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
+        * so sanitize this register.
+        */
+       cdctl = I915_READ(CDCLK_CTL);
+       /*
+        * Let's ignore the pipe field, since BIOS could have configured the
+        * dividers both synching to an active pipe, or asynchronously
+        * (PIPE_NONE).
+        */
+       cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
+
+       expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
+                  skl_cdclk_decimal(dev_priv->cdclk_freq);
+       /*
+        * Disable SSA Precharge when CD clock frequency < 500 MHz,
+        * enable otherwise.
+        */
+       if (dev_priv->cdclk_freq >= 500000)
+               expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
+
+       if (cdctl == expected)
+               /* All well; nothing to sanitize */
+               return;
+
+sanitize:
+       DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
+
+       /* force cdclk programming */
+       dev_priv->cdclk_freq = 0;
+
+       /* force full PLL disable + enable */
+       dev_priv->cdclk_pll.vco = -1;
 }
 
-void broxton_init_cdclk(struct drm_i915_private *dev_priv)
+void bxt_init_cdclk(struct drm_i915_private *dev_priv)
 {
-       /* check if cd clock is enabled */
-       if (broxton_cdclk_is_enabled(dev_priv)) {
-               DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
-               return;
-       }
+       bxt_sanitize_cdclk(dev_priv);
 
-       DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
+       if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
+               return;
 
        /*
         * FIXME:
         * - The initial CDCLK needs to be read from VBT.
         *   Need to make this change after VBT has changes for BXT.
-        * - check if setting the max (or any) cdclk freq is really necessary
-        *   here, it belongs to modeset time
         */
-       broxton_set_cdclk(dev_priv, 624000);
+       bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
 }
 
-void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
+void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
 {
-       /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
-       broxton_set_cdclk(dev_priv, 19200);
+       bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
 }
 
 static int skl_calc_cdclk(int max_pixclk, int vco)
 {
-       if (vco == 8640) {
+       if (vco == 8640000) {
                if (max_pixclk > 540000)
-                       return 617140;
+                       return 617143;
                else if (max_pixclk > 432000)
                        return 540000;
-               else if (max_pixclk > 308570)
+               else if (max_pixclk > 308571)
                        return 432000;
                else
-                       return 308570;
+                       return 308571;
        } else {
-               /* VCO 8100 */
                if (max_pixclk > 540000)
                        return 675000;
                else if (max_pixclk > 450000)
@@ -5463,35 +5577,37 @@ skl_dpll0_update(struct drm_i915_private *dev_priv)
 {
        u32 val;
 
+       dev_priv->cdclk_pll.ref = 24000;
+       dev_priv->cdclk_pll.vco = 0;
+
        val = I915_READ(LCPLL1_CTL);
-       if ((val & LCPLL_PLL_ENABLE) == 0) {
-               dev_priv->skl_vco_freq = 0;
+       if ((val & LCPLL_PLL_ENABLE) == 0)
                return;
-       }
 
-       WARN_ON((val & LCPLL_PLL_LOCK) == 0);
+       if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
+               return;
 
        val = I915_READ(DPLL_CTRL1);
 
-       WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
-                       DPLL_CTRL1_SSC(SKL_DPLL0) |
-                       DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
-               DPLL_CTRL1_OVERRIDE(SKL_DPLL0));
+       if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
+                           DPLL_CTRL1_SSC(SKL_DPLL0) |
+                           DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
+                   DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
+               return;
 
        switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
        case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
        case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
        case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
        case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
-               dev_priv->skl_vco_freq = 8100;
+               dev_priv->cdclk_pll.vco = 8100000;
                break;
        case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
        case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
-               dev_priv->skl_vco_freq = 8640;
+               dev_priv->cdclk_pll.vco = 8640000;
                break;
        default:
                MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
-               dev_priv->skl_vco_freq = 0;
                break;
        }
 }
@@ -5503,7 +5619,7 @@ void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
        dev_priv->skl_preferred_vco_freq = vco;
 
        if (changed)
-               intel_update_max_cdclk(dev_priv->dev);
+               intel_update_max_cdclk(&dev_priv->drm);
 }
 
 static void
@@ -5512,7 +5628,7 @@ skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
        int min_cdclk = skl_calc_cdclk(0, vco);
        u32 val;
 
-       WARN_ON(vco != 8100 && vco != 8640);
+       WARN_ON(vco != 8100000 && vco != 8640000);
 
        /* select the minimum CDCLK before enabling DPLL 0 */
        val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
@@ -5533,7 +5649,7 @@ skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
        val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
                 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
        val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
-       if (vco == 8640)
+       if (vco == 8640000)
                val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
                                            SKL_DPLL0);
        else
@@ -5545,10 +5661,12 @@ skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
 
        I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
 
-       if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
+       if (intel_wait_for_register(dev_priv,
+                                   LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
+                                   5))
                DRM_ERROR("DPLL0 not locked\n");
 
-       dev_priv->skl_vco_freq = vco;
+       dev_priv->cdclk_pll.vco = vco;
 
        /* We'll want to keep using the current vco from now on. */
        skl_set_preferred_cdclk_vco(dev_priv, vco);
@@ -5558,10 +5676,12 @@ static void
 skl_dpll0_disable(struct drm_i915_private *dev_priv)
 {
        I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
-       if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
+       if (intel_wait_for_register(dev_priv,
+                                  LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
+                                  1))
                DRM_ERROR("Couldn't disable DPLL0\n");
 
-       dev_priv->skl_vco_freq = 0;
+       dev_priv->cdclk_pll.vco = 0;
 }
 
 static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
@@ -5593,12 +5713,12 @@ static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
 
 static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        u32 freq_select, pcu_ack;
 
        WARN_ON((cdclk == 24000) != (vco == 0));
 
-       DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d MHz)\n", cdclk, vco);
+       DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
 
        if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
                DRM_ERROR("failed to inform PCU about cdclk change\n");
@@ -5616,24 +5736,24 @@ static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
                freq_select = CDCLK_FREQ_540;
                pcu_ack = 2;
                break;
-       case 308570:
+       case 308571:
        case 337500:
        default:
                freq_select = CDCLK_FREQ_337_308;
                pcu_ack = 0;
                break;
-       case 617140:
+       case 617143:
        case 675000:
                freq_select = CDCLK_FREQ_675_617;
                pcu_ack = 3;
                break;
        }
 
-       if (dev_priv->skl_vco_freq != 0 &&
-           dev_priv->skl_vco_freq != vco)
+       if (dev_priv->cdclk_pll.vco != 0 &&
+           dev_priv->cdclk_pll.vco != vco)
                skl_dpll0_disable(dev_priv);
 
-       if (dev_priv->skl_vco_freq != vco)
+       if (dev_priv->cdclk_pll.vco != vco)
                skl_dpll0_enable(dev_priv, vco);
 
        I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
@@ -5651,7 +5771,7 @@ static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
 
 void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
 {
-       skl_set_cdclk(dev_priv, 24000, 0);
+       skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
 }
 
 void skl_init_cdclk(struct drm_i915_private *dev_priv)
@@ -5660,20 +5780,20 @@ void skl_init_cdclk(struct drm_i915_private *dev_priv)
 
        skl_sanitize_cdclk(dev_priv);
 
-       if (dev_priv->cdclk_freq != 0 && dev_priv->skl_vco_freq != 0) {
+       if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
                /*
                 * Use the current vco as our initial
                 * guess as to what the preferred vco is.
                 */
                if (dev_priv->skl_preferred_vco_freq == 0)
                        skl_set_preferred_cdclk_vco(dev_priv,
-                                                   dev_priv->skl_vco_freq);
+                                                   dev_priv->cdclk_pll.vco);
                return;
        }
 
        vco = dev_priv->skl_preferred_vco_freq;
        if (vco == 0)
-               vco = 8100;
+               vco = 8100000;
        cdclk = skl_calc_cdclk(0, vco);
 
        skl_set_cdclk(dev_priv, cdclk, vco);
@@ -5691,19 +5811,12 @@ static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
        if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
                goto sanitize;
 
+       intel_update_cdclk(&dev_priv->drm);
        /* Is PLL enabled and locked ? */
-       if ((I915_READ(LCPLL1_CTL) & (LCPLL_PLL_ENABLE | LCPLL_PLL_LOCK)) !=
-           (LCPLL_PLL_ENABLE | LCPLL_PLL_LOCK))
-               goto sanitize;
-
-       if ((I915_READ(DPLL_CTRL1) & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
-                                     DPLL_CTRL1_SSC(SKL_DPLL0) |
-                                     DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
-           DPLL_CTRL1_OVERRIDE(SKL_DPLL0))
+       if (dev_priv->cdclk_pll.vco == 0 ||
+           dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
                goto sanitize;
 
-       intel_update_cdclk(dev_priv->dev);
-
        /* DPLL okay; verify the cdclock
         *
         * Noticed in some instances that the freq selection is correct but
@@ -5723,13 +5836,13 @@ sanitize:
        /* force cdclk programming */
        dev_priv->cdclk_freq = 0;
        /* force full PLL disable + enable */
-       dev_priv->skl_vco_freq = -1;
+       dev_priv->cdclk_pll.vco = -1;
 }
 
 /* Adjust CDclk dividers to allow high res or save power if possible */
 static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 val, cmd;
 
        WARN_ON(dev_priv->display.get_display_clock_speed(dev)
@@ -5794,7 +5907,7 @@ static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
 
 static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 val, cmd;
 
        WARN_ON(dev_priv->display.get_display_clock_speed(dev)
@@ -5863,12 +5976,8 @@ static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
                return 200000;
 }
 
-static int broxton_calc_cdclk(int max_pixclk)
+static int bxt_calc_cdclk(int max_pixclk)
 {
-       /*
-        * FIXME:
-        * - set 19.2MHz bypass frequency if there are no active pipes
-        */
        if (max_pixclk > 576000)
                return 624000;
        else if (max_pixclk > 384000)
@@ -5886,7 +5995,7 @@ static int intel_mode_max_pixclk(struct drm_device *dev,
                                 struct drm_atomic_state *state)
 {
        struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct drm_crtc *crtc;
        struct drm_crtc_state *crtc_state;
        unsigned max_pixclk = 0, i;
@@ -5913,7 +6022,7 @@ static int intel_mode_max_pixclk(struct drm_device *dev,
 static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
 {
        struct drm_device *dev = state->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int max_pixclk = intel_mode_max_pixclk(dev, state);
        struct intel_atomic_state *intel_state =
                to_intel_atomic_state(state);
@@ -5927,17 +6036,17 @@ static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
        return 0;
 }
 
-static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
+static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
 {
        int max_pixclk = ilk_max_pixel_rate(state);
        struct intel_atomic_state *intel_state =
                to_intel_atomic_state(state);
 
        intel_state->cdclk = intel_state->dev_cdclk =
-               broxton_calc_cdclk(max_pixclk);
+               bxt_calc_cdclk(max_pixclk);
 
        if (!intel_state->active_crtcs)
-               intel_state->dev_cdclk = broxton_calc_cdclk(0);
+               intel_state->dev_cdclk = bxt_calc_cdclk(0);
 
        return 0;
 }
@@ -5981,7 +6090,7 @@ static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
 static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
 {
        struct drm_device *dev = old_state->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_atomic_state *old_intel_state =
                to_intel_atomic_state(old_state);
        unsigned req_cdclk = old_intel_state->dev_cdclk;
@@ -6020,14 +6129,14 @@ static void valleyview_crtc_enable(struct drm_crtc *crtc)
        if (WARN_ON(intel_crtc->active))
                return;
 
-       if (intel_crtc->config->has_dp_encoder)
+       if (intel_crtc_has_dp_encoder(intel_crtc->config))
                intel_dp_set_m_n(intel_crtc, M1_N1);
 
        intel_set_pipe_timings(intel_crtc);
        intel_set_pipe_src_size(intel_crtc);
 
        if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
-               struct drm_i915_private *dev_priv = dev->dev_private;
+               struct drm_i915_private *dev_priv = to_i915(dev);
 
                I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
                I915_WRITE(CHV_CANVAS(pipe), 0);
@@ -6072,7 +6181,7 @@ static void valleyview_crtc_enable(struct drm_crtc *crtc)
 static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
        I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
@@ -6093,7 +6202,7 @@ static void i9xx_crtc_enable(struct drm_crtc *crtc)
 
        i9xx_set_pll_dividers(intel_crtc);
 
-       if (intel_crtc->config->has_dp_encoder)
+       if (intel_crtc_has_dp_encoder(intel_crtc->config))
                intel_dp_set_m_n(intel_crtc, M1_N1);
 
        intel_set_pipe_timings(intel_crtc);
@@ -6129,7 +6238,7 @@ static void i9xx_crtc_enable(struct drm_crtc *crtc)
 static void i9xx_pfit_disable(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        if (!crtc->config->gmch_pfit.control)
                return;
@@ -6144,7 +6253,7 @@ static void i9xx_pfit_disable(struct intel_crtc *crtc)
 static void i9xx_crtc_disable(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_encoder *encoder;
        int pipe = intel_crtc->pipe;
@@ -6199,7 +6308,7 @@ static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
                return;
 
        if (to_intel_plane_state(crtc->primary->state)->visible) {
-               WARN_ON(list_empty(&intel_crtc->flip_work));
+               WARN_ON(intel_crtc->flip_work);
 
                intel_pre_disable_primary_noatomic(crtc);
 
@@ -6209,8 +6318,8 @@ static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
 
        dev_priv->display.crtc_disable(crtc);
 
-       DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
-                     crtc->base.id);
+       DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
+                     crtc->base.id, crtc->name);
 
        WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
        crtc->state->active = false;
@@ -6251,12 +6360,6 @@ int intel_display_suspend(struct drm_device *dev)
                DRM_ERROR("Suspending crtc's failed with %i\n", ret);
        else
                dev_priv->modeset_restore_state = state;
-
-       /*
-        * Make sure all unpin_work completes before returning.
-        */
-       flush_workqueue(dev_priv->wq);
-
        return ret;
 }
 
@@ -6270,10 +6373,9 @@ void intel_encoder_destroy(struct drm_encoder *encoder)
 
 /* Cross check the actual hw state with our own modeset state tracking (and it's
  * internal consistency). */
-static void intel_connector_verify_state(struct intel_connector *connector,
-                                        struct drm_connector_state *conn_state)
+static void intel_connector_verify_state(struct intel_connector *connector)
 {
-       struct drm_crtc *crtc = conn_state->crtc;
+       struct drm_crtc *crtc = connector->base.state->crtc;
 
        DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
                      connector->base.base.id,
@@ -6281,6 +6383,7 @@ static void intel_connector_verify_state(struct intel_connector *connector,
 
        if (connector->get_hw_state(connector)) {
                struct intel_encoder *encoder = connector->encoder;
+               struct drm_connector_state *conn_state = connector->base.state;
 
                I915_STATE_WARN(!crtc,
                         "connector enabled without attached crtc\n");
@@ -6302,7 +6405,7 @@ static void intel_connector_verify_state(struct intel_connector *connector,
        } else {
                I915_STATE_WARN(crtc && crtc->state->active,
                        "attached crtc is active, but connector isn't\n");
-               I915_STATE_WARN(!crtc && conn_state->best_encoder,
+               I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
                        "best encoder set without crtc!\n");
        }
 }
@@ -6494,7 +6597,7 @@ static void hsw_compute_ips_config(struct intel_crtc *crtc,
                                   struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        pipe_config->ips_enabled = i915.enable_ips &&
                hsw_crtc_supports_ips(crtc) &&
@@ -6514,12 +6617,12 @@ static int intel_crtc_compute_config(struct intel_crtc *crtc,
                                     struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
+       int clock_limit = dev_priv->max_dotclk_freq;
 
-       /* FIXME should check pixel clock limits on all platforms */
        if (INTEL_INFO(dev)->gen < 4) {
-               int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
+               clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
 
                /*
                 * Enable double wide mode when the dot clock
@@ -6527,16 +6630,16 @@ static int intel_crtc_compute_config(struct intel_crtc *crtc,
                 */
                if (intel_crtc_supports_double_wide(crtc) &&
                    adjusted_mode->crtc_clock > clock_limit) {
-                       clock_limit *= 2;
+                       clock_limit = dev_priv->max_dotclk_freq;
                        pipe_config->double_wide = true;
                }
+       }
 
-               if (adjusted_mode->crtc_clock > clock_limit) {
-                       DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
-                                     adjusted_mode->crtc_clock, clock_limit,
-                                     yesno(pipe_config->double_wide));
-                       return -EINVAL;
-               }
+       if (adjusted_mode->crtc_clock > clock_limit) {
+               DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
+                             adjusted_mode->crtc_clock, clock_limit,
+                             yesno(pipe_config->double_wide));
+               return -EINVAL;
        }
 
        /*
@@ -6545,7 +6648,7 @@ static int intel_crtc_compute_config(struct intel_crtc *crtc,
         * - LVDS dual channel mode
         * - Double wide pipe
         */
-       if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
+       if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
             intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
                pipe_config->pipe_src_w &= ~1;
 
@@ -6572,21 +6675,21 @@ static int skylake_get_display_clock_speed(struct drm_device *dev)
 
        skl_dpll0_update(dev_priv);
 
-       if (dev_priv->skl_vco_freq == 0)
-               return 24000; /* 24MHz is the cd freq with NSSC ref */
+       if (dev_priv->cdclk_pll.vco == 0)
+               return dev_priv->cdclk_pll.ref;
 
        cdctl = I915_READ(CDCLK_CTL);
 
-       if (dev_priv->skl_vco_freq == 8640) {
+       if (dev_priv->cdclk_pll.vco == 8640000) {
                switch (cdctl & CDCLK_FREQ_SEL_MASK) {
                case CDCLK_FREQ_450_432:
                        return 432000;
                case CDCLK_FREQ_337_308:
-                       return 308570;
+                       return 308571;
                case CDCLK_FREQ_540:
                        return 540000;
                case CDCLK_FREQ_675_617:
-                       return 617140;
+                       return 617143;
                default:
                        MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
                }
@@ -6605,41 +6708,66 @@ static int skylake_get_display_clock_speed(struct drm_device *dev)
                }
        }
 
-       /* error case, do as if DPLL0 isn't enabled */
-       return 24000;
+       return dev_priv->cdclk_pll.ref;
+}
+
+static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
+{
+       u32 val;
+
+       dev_priv->cdclk_pll.ref = 19200;
+       dev_priv->cdclk_pll.vco = 0;
+
+       val = I915_READ(BXT_DE_PLL_ENABLE);
+       if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
+               return;
+
+       if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
+               return;
+
+       val = I915_READ(BXT_DE_PLL_CTL);
+       dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
+               dev_priv->cdclk_pll.ref;
 }
 
 static int broxton_get_display_clock_speed(struct drm_device *dev)
 {
        struct drm_i915_private *dev_priv = to_i915(dev);
-       uint32_t cdctl = I915_READ(CDCLK_CTL);
-       uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
-       uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
-       int cdclk;
+       u32 divider;
+       int div, vco;
 
-       if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
-               return 19200;
+       bxt_de_pll_update(dev_priv);
 
-       cdclk = 19200 * pll_ratio / 2;
+       vco = dev_priv->cdclk_pll.vco;
+       if (vco == 0)
+               return dev_priv->cdclk_pll.ref;
+
+       divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
 
-       switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
+       switch (divider) {
        case BXT_CDCLK_CD2X_DIV_SEL_1:
-               return cdclk;  /* 576MHz or 624MHz */
+               div = 2;
+               break;
        case BXT_CDCLK_CD2X_DIV_SEL_1_5:
-               return cdclk * 2 / 3; /* 384MHz */
+               div = 3;
+               break;
        case BXT_CDCLK_CD2X_DIV_SEL_2:
-               return cdclk / 2; /* 288MHz */
+               div = 4;
+               break;
        case BXT_CDCLK_CD2X_DIV_SEL_4:
-               return cdclk / 4; /* 144MHz */
+               div = 8;
+               break;
+       default:
+               MISSING_CASE(divider);
+               return dev_priv->cdclk_pll.ref;
        }
 
-       /* error case, do as if DE PLL isn't enabled */
-       return 19200;
+       return DIV_ROUND_CLOSEST(vco, div);
 }
 
 static int broadwell_get_display_clock_speed(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t lcpll = I915_READ(LCPLL_CTL);
        uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
 
@@ -6659,7 +6787,7 @@ static int broadwell_get_display_clock_speed(struct drm_device *dev)
 
 static int haswell_get_display_clock_speed(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t lcpll = I915_READ(LCPLL_CTL);
        uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
 
@@ -6793,7 +6921,7 @@ static int i830_get_display_clock_speed(struct drm_device *dev)
 
 static unsigned int intel_hpll_vco(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        static const unsigned int blb_vco[8] = {
                [0] = 3200000,
                [1] = 4000000,
@@ -7031,7 +7159,7 @@ static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
        crtc_state->dpll_hw_state.fp0 = fp;
 
        crtc->lowfreq_avail = false;
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
            reduced_clock) {
                crtc_state->dpll_hw_state.fp1 = fp2;
                crtc->lowfreq_avail = true;
@@ -7073,7 +7201,7 @@ static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
                                         struct intel_link_m_n *m_n)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = crtc->pipe;
 
        I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
@@ -7087,7 +7215,7 @@ static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
                                         struct intel_link_m_n *m2_n2)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = crtc->pipe;
        enum transcoder transcoder = crtc->config->cpu_transcoder;
 
@@ -7178,7 +7306,7 @@ static void vlv_prepare_pll(struct intel_crtc *crtc,
                            const struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe = crtc->pipe;
        u32 mdiv;
        u32 bestn, bestm1, bestm2, bestp1, bestp2;
@@ -7237,15 +7365,15 @@ static void vlv_prepare_pll(struct intel_crtc *crtc,
 
        /* Set HBR and RBR LPF coefficients */
        if (pipe_config->port_clock == 162000 ||
-           intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
-           intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
+           intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
+           intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
                vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
                                 0x009f0003);
        else
                vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
                                 0x00d0000f);
 
-       if (pipe_config->has_dp_encoder) {
+       if (intel_crtc_has_dp_encoder(pipe_config)) {
                /* Use SSC source */
                if (pipe == PIPE_A)
                        vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
@@ -7265,8 +7393,7 @@ static void vlv_prepare_pll(struct intel_crtc *crtc,
 
        coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
        coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
-       if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
-           intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
+       if (intel_crtc_has_dp_encoder(crtc->config))
                coreclk |= 0x01000000;
        vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
 
@@ -7278,7 +7405,7 @@ static void chv_prepare_pll(struct intel_crtc *crtc,
                            const struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe = crtc->pipe;
        enum dpio_channel port = vlv_pipe_to_channel(pipe);
        u32 loopfilter, tribuf_calcntr;
@@ -7440,19 +7567,15 @@ static void i9xx_compute_dpll(struct intel_crtc *crtc,
                              struct dpll *reduced_clock)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 dpll;
-       bool is_sdvo;
        struct dpll *clock = &crtc_state->dpll;
 
        i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
 
-       is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
-               intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
-
        dpll = DPLL_VGA_MODE_DIS;
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
                dpll |= DPLLB_MODE_LVDS;
        else
                dpll |= DPLLB_MODE_DAC_SERIAL;
@@ -7462,10 +7585,11 @@ static void i9xx_compute_dpll(struct intel_crtc *crtc,
                        << SDVO_MULTIPLIER_SHIFT_HIRES;
        }
 
-       if (is_sdvo)
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
+           intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
                dpll |= DPLL_SDVO_HIGH_SPEED;
 
-       if (crtc_state->has_dp_encoder)
+       if (intel_crtc_has_dp_encoder(crtc_state))
                dpll |= DPLL_SDVO_HIGH_SPEED;
 
        /* compute bitmask from p1 value */
@@ -7495,7 +7619,7 @@ static void i9xx_compute_dpll(struct intel_crtc *crtc,
 
        if (crtc_state->sdvo_tv_clock)
                dpll |= PLL_REF_INPUT_TVCLKINBC;
-       else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
+       else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
                 intel_panel_use_ssc(dev_priv))
                dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
        else
@@ -7516,7 +7640,7 @@ static void i8xx_compute_dpll(struct intel_crtc *crtc,
                              struct dpll *reduced_clock)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 dpll;
        struct dpll *clock = &crtc_state->dpll;
 
@@ -7524,7 +7648,7 @@ static void i8xx_compute_dpll(struct intel_crtc *crtc,
 
        dpll = DPLL_VGA_MODE_DIS;
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
                dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
        } else {
                if (clock->p1 == 2)
@@ -7535,10 +7659,10 @@ static void i8xx_compute_dpll(struct intel_crtc *crtc,
                        dpll |= PLL_P2_DIVIDE_BY_4;
        }
 
-       if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
+       if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
                dpll |= DPLL_DVO_2X_MODE;
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
            intel_panel_use_ssc(dev_priv))
                dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
        else
@@ -7551,7 +7675,7 @@ static void i8xx_compute_dpll(struct intel_crtc *crtc,
 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
 {
        struct drm_device *dev = intel_crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe = intel_crtc->pipe;
        enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
        const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
@@ -7568,7 +7692,7 @@ static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
                crtc_vtotal -= 1;
                crtc_vblank_end -= 1;
 
-               if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
+               if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
                        vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
                else
                        vsyncshift = adjusted_mode->crtc_hsync_start -
@@ -7613,7 +7737,7 @@ static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
 static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
 {
        struct drm_device *dev = intel_crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe = intel_crtc->pipe;
 
        /* pipesrc controls the size that is scaled from, which should
@@ -7628,7 +7752,7 @@ static void intel_get_pipe_timings(struct intel_crtc *crtc,
                                   struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
        uint32_t tmp;
 
@@ -7663,7 +7787,7 @@ static void intel_get_pipe_src_size(struct intel_crtc *crtc,
                                    struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 tmp;
 
        tmp = I915_READ(PIPESRC(crtc->pipe));
@@ -7701,7 +7825,7 @@ void intel_mode_from_pipe_config(struct drm_display_mode *mode,
 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
 {
        struct drm_device *dev = intel_crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t pipeconf;
 
        pipeconf = 0;
@@ -7747,7 +7871,7 @@ static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
 
        if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
                if (INTEL_INFO(dev)->gen < 4 ||
-                   intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
+                   intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
                        pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
                else
                        pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
@@ -7766,21 +7890,21 @@ static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
                                   struct intel_crtc_state *crtc_state)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        const struct intel_limit *limit;
        int refclk = 48000;
 
        memset(&crtc_state->dpll_hw_state, 0,
               sizeof(crtc_state->dpll_hw_state));
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
                if (intel_panel_use_ssc(dev_priv)) {
                        refclk = dev_priv->vbt.lvds_ssc_freq;
                        DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
                }
 
                limit = &intel_limits_i8xx_lvds;
-       } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
+       } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
                limit = &intel_limits_i8xx_dvo;
        } else {
                limit = &intel_limits_i8xx_dac;
@@ -7802,14 +7926,14 @@ static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
                                  struct intel_crtc_state *crtc_state)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        const struct intel_limit *limit;
        int refclk = 96000;
 
        memset(&crtc_state->dpll_hw_state, 0,
               sizeof(crtc_state->dpll_hw_state));
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
                if (intel_panel_use_ssc(dev_priv)) {
                        refclk = dev_priv->vbt.lvds_ssc_freq;
                        DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
@@ -7819,10 +7943,10 @@ static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
                        limit = &intel_limits_g4x_dual_channel_lvds;
                else
                        limit = &intel_limits_g4x_single_channel_lvds;
-       } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
-                  intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
+       } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
+                  intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
                limit = &intel_limits_g4x_hdmi;
-       } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
+       } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
                limit = &intel_limits_g4x_sdvo;
        } else {
                /* The option is for other outputs */
@@ -7845,14 +7969,14 @@ static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
                                  struct intel_crtc_state *crtc_state)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        const struct intel_limit *limit;
        int refclk = 96000;
 
        memset(&crtc_state->dpll_hw_state, 0,
               sizeof(crtc_state->dpll_hw_state));
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
                if (intel_panel_use_ssc(dev_priv)) {
                        refclk = dev_priv->vbt.lvds_ssc_freq;
                        DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
@@ -7879,14 +8003,14 @@ static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
                                   struct intel_crtc_state *crtc_state)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        const struct intel_limit *limit;
        int refclk = 96000;
 
        memset(&crtc_state->dpll_hw_state, 0,
               sizeof(crtc_state->dpll_hw_state));
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
                if (intel_panel_use_ssc(dev_priv)) {
                        refclk = dev_priv->vbt.lvds_ssc_freq;
                        DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
@@ -7955,7 +8079,7 @@ static void i9xx_get_pfit_config(struct intel_crtc *crtc,
                                 struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t tmp;
 
        if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
@@ -7982,7 +8106,7 @@ static void vlv_crtc_clock_get(struct intel_crtc *crtc,
                               struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = pipe_config->cpu_transcoder;
        struct dpll clock;
        u32 mdiv;
@@ -8010,7 +8134,7 @@ i9xx_get_initial_plane_config(struct intel_crtc *crtc,
                              struct intel_initial_plane_config *plane_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 val, base, offset;
        int pipe = crtc->pipe, plane = crtc->plane;
        int fourcc, pixel_format;
@@ -8078,7 +8202,7 @@ static void chv_crtc_clock_get(struct intel_crtc *crtc,
                               struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = pipe_config->cpu_transcoder;
        enum dpio_channel port = vlv_pipe_to_channel(pipe);
        struct dpll clock;
@@ -8112,7 +8236,7 @@ static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
                                 struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum intel_display_power_domain power_domain;
        uint32_t tmp;
        bool ret;
@@ -8223,14 +8347,16 @@ out:
 
 static void ironlake_init_pch_refclk(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_encoder *encoder;
+       int i;
        u32 val, final;
        bool has_lvds = false;
        bool has_cpu_edp = false;
        bool has_panel = false;
        bool has_ck505 = false;
        bool can_ssc = false;
+       bool using_ssc_source = false;
 
        /* We need to take the global config into account */
        for_each_intel_encoder(dev, encoder) {
@@ -8257,8 +8383,22 @@ static void ironlake_init_pch_refclk(struct drm_device *dev)
                can_ssc = true;
        }
 
-       DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
-                     has_panel, has_lvds, has_ck505);
+       /* Check if any DPLLs are using the SSC source */
+       for (i = 0; i < dev_priv->num_shared_dpll; i++) {
+               u32 temp = I915_READ(PCH_DPLL(i));
+
+               if (!(temp & DPLL_VCO_ENABLE))
+                       continue;
+
+               if ((temp & PLL_REF_INPUT_MASK) ==
+                   PLLB_REF_INPUT_SPREADSPECTRUMIN) {
+                       using_ssc_source = true;
+                       break;
+               }
+       }
+
+       DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
+                     has_panel, has_lvds, has_ck505, using_ssc_source);
 
        /* Ironlake: try to setup display ref clock before DPLL
         * enabling. This is only under driver's control after
@@ -8295,9 +8435,9 @@ static void ironlake_init_pch_refclk(struct drm_device *dev)
                                final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
                } else
                        final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
-       } else {
-               final |= DREF_SSC_SOURCE_DISABLE;
-               final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
+       } else if (using_ssc_source) {
+               final |= DREF_SSC_SOURCE_ENABLE;
+               final |= DREF_SSC1_ENABLE;
        }
 
        if (final == val)
@@ -8343,7 +8483,7 @@ static void ironlake_init_pch_refclk(struct drm_device *dev)
                POSTING_READ(PCH_DREF_CONTROL);
                udelay(200);
        } else {
-               DRM_DEBUG_KMS("Disabling SSC entirely\n");
+               DRM_DEBUG_KMS("Disabling CPU source output\n");
 
                val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
 
@@ -8354,16 +8494,20 @@ static void ironlake_init_pch_refclk(struct drm_device *dev)
                POSTING_READ(PCH_DREF_CONTROL);
                udelay(200);
 
-               /* Turn off the SSC source */
-               val &= ~DREF_SSC_SOURCE_MASK;
-               val |= DREF_SSC_SOURCE_DISABLE;
+               if (!using_ssc_source) {
+                       DRM_DEBUG_KMS("Disabling SSC source\n");
 
-               /* Turn off SSC1 */
-               val &= ~DREF_SSC1_ENABLE;
+                       /* Turn off the SSC source */
+                       val &= ~DREF_SSC_SOURCE_MASK;
+                       val |= DREF_SSC_SOURCE_DISABLE;
 
-               I915_WRITE(PCH_DREF_CONTROL, val);
-               POSTING_READ(PCH_DREF_CONTROL);
-               udelay(200);
+                       /* Turn off SSC1 */
+                       val &= ~DREF_SSC1_ENABLE;
+
+                       I915_WRITE(PCH_DREF_CONTROL, val);
+                       POSTING_READ(PCH_DREF_CONTROL);
+                       udelay(200);
+               }
        }
 
        BUG_ON(val != final);
@@ -8377,16 +8521,16 @@ static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
        tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
        I915_WRITE(SOUTH_CHICKEN2, tmp);
 
-       if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
-                              FDI_MPHY_IOSFSB_RESET_STATUS, 100))
+       if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
+                       FDI_MPHY_IOSFSB_RESET_STATUS, 100))
                DRM_ERROR("FDI mPHY reset assert timeout\n");
 
        tmp = I915_READ(SOUTH_CHICKEN2);
        tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
        I915_WRITE(SOUTH_CHICKEN2, tmp);
 
-       if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
-                               FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
+       if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
+                        FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
                DRM_ERROR("FDI mPHY reset de-assert timeout\n");
 }
 
@@ -8474,7 +8618,7 @@ static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
 static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
                                 bool with_fdi)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t reg, tmp;
 
        if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
@@ -8513,7 +8657,7 @@ static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
 /* Sequence to disable CLKOUT_DP */
 static void lpt_disable_clkout_dp(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t reg, tmp;
 
        mutex_lock(&dev_priv->sb_lock);
@@ -8634,7 +8778,7 @@ void intel_init_pch_refclk(struct drm_device *dev)
 
 static void ironlake_set_pipeconf(struct drm_crtc *crtc)
 {
-       struct drm_i915_private *dev_priv = crtc->dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(crtc->dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        uint32_t val;
@@ -8676,7 +8820,7 @@ static void ironlake_set_pipeconf(struct drm_crtc *crtc)
 
 static void haswell_set_pipeconf(struct drm_crtc *crtc)
 {
-       struct drm_i915_private *dev_priv = crtc->dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(crtc->dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
        u32 val = 0;
@@ -8695,7 +8839,7 @@ static void haswell_set_pipeconf(struct drm_crtc *crtc)
 
 static void haswell_set_pipemisc(struct drm_crtc *crtc)
 {
-       struct drm_i915_private *dev_priv = crtc->dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(crtc->dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
 
        if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
@@ -8748,37 +8892,13 @@ static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
 {
        struct drm_crtc *crtc = &intel_crtc->base;
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct drm_atomic_state *state = crtc_state->base.state;
-       struct drm_connector *connector;
-       struct drm_connector_state *connector_state;
-       struct intel_encoder *encoder;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 dpll, fp, fp2;
-       int factor, i;
-       bool is_lvds = false, is_sdvo = false;
-
-       for_each_connector_in_state(state, connector, connector_state, i) {
-               if (connector_state->crtc != crtc_state->base.crtc)
-                       continue;
-
-               encoder = to_intel_encoder(connector_state->best_encoder);
-
-               switch (encoder->type) {
-               case INTEL_OUTPUT_LVDS:
-                       is_lvds = true;
-                       break;
-               case INTEL_OUTPUT_SDVO:
-               case INTEL_OUTPUT_HDMI:
-                       is_sdvo = true;
-                       break;
-               default:
-                       break;
-               }
-       }
+       int factor;
 
        /* Enable autotuning of the PLL clock (if permissible) */
        factor = 21;
-       if (is_lvds) {
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
                if ((intel_panel_use_ssc(dev_priv) &&
                     dev_priv->vbt.lvds_ssc_freq == 100000) ||
                    (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
@@ -8802,7 +8922,7 @@ static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
 
        dpll = 0;
 
-       if (is_lvds)
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
                dpll |= DPLLB_MODE_LVDS;
        else
                dpll |= DPLLB_MODE_DAC_SERIAL;
@@ -8810,9 +8930,11 @@ static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
        dpll |= (crtc_state->pixel_multiplier - 1)
                << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
 
-       if (is_sdvo)
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
+           intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
                dpll |= DPLL_SDVO_HIGH_SPEED;
-       if (crtc_state->has_dp_encoder)
+
+       if (intel_crtc_has_dp_encoder(crtc_state))
                dpll |= DPLL_SDVO_HIGH_SPEED;
 
        /* compute bitmask from p1 value */
@@ -8835,7 +8957,8 @@ static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
                break;
        }
 
-       if (is_lvds && intel_panel_use_ssc(dev_priv))
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
+           intel_panel_use_ssc(dev_priv))
                dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
        else
                dpll |= PLL_REF_INPUT_DREFCLK;
@@ -8851,7 +8974,7 @@ static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
                                       struct intel_crtc_state *crtc_state)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct dpll reduced_clock;
        bool has_reduced_clock = false;
        struct intel_shared_dpll *pll;
@@ -8867,7 +8990,7 @@ static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
        if (!crtc_state->has_pch_encoder)
                return 0;
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
                if (intel_panel_use_ssc(dev_priv)) {
                        DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
                                      dev_priv->vbt.lvds_ssc_freq);
@@ -8906,7 +9029,7 @@ static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
                return -EINVAL;
        }
 
-       if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
+       if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
            has_reduced_clock)
                crtc->lowfreq_avail = true;
 
@@ -8917,7 +9040,7 @@ static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
                                         struct intel_link_m_n *m_n)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe = crtc->pipe;
 
        m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
@@ -8935,7 +9058,7 @@ static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
                                         struct intel_link_m_n *m2_n2)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe = crtc->pipe;
 
        if (INTEL_INFO(dev)->gen >= 5) {
@@ -8993,7 +9116,7 @@ static void skylake_get_pfit_config(struct intel_crtc *crtc,
                                    struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
        uint32_t ps_ctrl = 0;
        int id = -1;
@@ -9024,7 +9147,7 @@ skylake_get_initial_plane_config(struct intel_crtc *crtc,
                                 struct intel_initial_plane_config *plane_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 val, base, offset, stride_mult, tiling;
        int pipe = crtc->pipe;
        int fourcc, pixel_format;
@@ -9107,7 +9230,7 @@ static void ironlake_get_pfit_config(struct intel_crtc *crtc,
                                     struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t tmp;
 
        tmp = I915_READ(PF_CTL(crtc->pipe));
@@ -9132,7 +9255,7 @@ ironlake_get_initial_plane_config(struct intel_crtc *crtc,
                                  struct intel_initial_plane_config *plane_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 val, base, offset;
        int pipe = crtc->pipe;
        int fourcc, pixel_format;
@@ -9200,7 +9323,7 @@ static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
                                     struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum intel_display_power_domain power_domain;
        uint32_t tmp;
        bool ret;
@@ -9295,7 +9418,7 @@ out:
 
 static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        struct intel_crtc *crtc;
 
        for_each_intel_crtc(dev, crtc)
@@ -9329,7 +9452,7 @@ static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
 
 static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
 
        if (IS_HASWELL(dev))
                return I915_READ(D_COMP_HSW);
@@ -9339,7 +9462,7 @@ static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
 
 static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
 
        if (IS_HASWELL(dev)) {
                mutex_lock(&dev_priv->rps.hw_lock);
@@ -9374,8 +9497,8 @@ static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
                val |= LCPLL_CD_SOURCE_FCLK;
                I915_WRITE(LCPLL_CTL, val);
 
-               if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
-                                      LCPLL_CD_SOURCE_FCLK_DONE, 1))
+               if (wait_for_us(I915_READ(LCPLL_CTL) &
+                               LCPLL_CD_SOURCE_FCLK_DONE, 1))
                        DRM_ERROR("Switching to FCLK failed\n");
 
                val = I915_READ(LCPLL_CTL);
@@ -9385,7 +9508,7 @@ static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
        I915_WRITE(LCPLL_CTL, val);
        POSTING_READ(LCPLL_CTL);
 
-       if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
+       if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
                DRM_ERROR("LCPLL still locked\n");
 
        val = hsw_read_dcomp(dev_priv);
@@ -9440,7 +9563,9 @@ static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
        val &= ~LCPLL_PLL_DISABLE;
        I915_WRITE(LCPLL_CTL, val);
 
-       if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
+       if (intel_wait_for_register(dev_priv,
+                                   LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
+                                   5))
                DRM_ERROR("LCPLL not locked yet\n");
 
        if (val & LCPLL_CD_SOURCE_FCLK) {
@@ -9448,13 +9573,13 @@ static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
                val &= ~LCPLL_CD_SOURCE_FCLK;
                I915_WRITE(LCPLL_CTL, val);
 
-               if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
-                                       LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
+               if (wait_for_us((I915_READ(LCPLL_CTL) &
+                                LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
                        DRM_ERROR("Switching back to LCPLL failed\n");
        }
 
        intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
-       intel_update_cdclk(dev_priv->dev);
+       intel_update_cdclk(&dev_priv->drm);
 }
 
 /*
@@ -9482,7 +9607,7 @@ static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  */
 void hsw_enable_pc8(struct drm_i915_private *dev_priv)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        uint32_t val;
 
        DRM_DEBUG_KMS("Enabling package C8+\n");
@@ -9499,7 +9624,7 @@ void hsw_enable_pc8(struct drm_i915_private *dev_priv)
 
 void hsw_disable_pc8(struct drm_i915_private *dev_priv)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        uint32_t val;
 
        DRM_DEBUG_KMS("Disabling package C8+\n");
@@ -9514,21 +9639,21 @@ void hsw_disable_pc8(struct drm_i915_private *dev_priv)
        }
 }
 
-static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
+static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
 {
        struct drm_device *dev = old_state->dev;
        struct intel_atomic_state *old_intel_state =
                to_intel_atomic_state(old_state);
        unsigned int req_cdclk = old_intel_state->dev_cdclk;
 
-       broxton_set_cdclk(to_i915(dev), req_cdclk);
+       bxt_set_cdclk(to_i915(dev), req_cdclk);
 }
 
 /* compute the max rate for new configuration */
 static int ilk_max_pixel_rate(struct drm_atomic_state *state)
 {
        struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
-       struct drm_i915_private *dev_priv = state->dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(state->dev);
        struct drm_crtc *crtc;
        struct drm_crtc_state *cstate;
        struct intel_crtc_state *crtc_state;
@@ -9564,7 +9689,7 @@ static int ilk_max_pixel_rate(struct drm_atomic_state *state)
 
 static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        uint32_t val, data;
        int ret;
 
@@ -9846,7 +9971,7 @@ static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
                                     unsigned long *power_domain_mask)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum intel_display_power_domain power_domain;
        u32 tmp;
 
@@ -9897,7 +10022,7 @@ static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
                                         unsigned long *power_domain_mask)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum intel_display_power_domain power_domain;
        enum port port;
        enum transcoder cpu_transcoder;
@@ -9947,7 +10072,7 @@ static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
                                       struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_shared_dpll *pll;
        enum port port;
        uint32_t tmp;
@@ -9990,7 +10115,7 @@ static bool haswell_get_pipe_config(struct intel_crtc *crtc,
                                    struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum intel_display_power_domain power_domain;
        unsigned long power_domain_mask;
        bool active;
@@ -10066,7 +10191,7 @@ static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
                               const struct intel_plane_state *plane_state)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        uint32_t cntl = 0, size = 0;
 
@@ -10129,7 +10254,7 @@ static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
                               const struct intel_plane_state *plane_state)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        uint32_t cntl = 0;
@@ -10177,7 +10302,7 @@ static void intel_crtc_update_cursor(struct drm_crtc *crtc,
                                     const struct intel_plane_state *plane_state)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
        u32 base = intel_crtc->cursor_addr;
@@ -10344,7 +10469,7 @@ mode_fits_in_fbdev(struct drm_device *dev,
                   struct drm_display_mode *mode)
 {
 #ifdef CONFIG_DRM_FBDEV_EMULATION
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct drm_i915_gem_object *obj;
        struct drm_framebuffer *fb;
 
@@ -10614,7 +10739,7 @@ void intel_release_load_detect_pipe(struct drm_connector *connector,
 static int i9xx_pll_refclk(struct drm_device *dev,
                           const struct intel_crtc_state *pipe_config)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 dpll = pipe_config->dpll_hw_state.dpll;
 
        if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
@@ -10632,7 +10757,7 @@ static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
                                struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int pipe = pipe_config->cpu_transcoder;
        u32 dpll = pipe_config->dpll_hw_state.dpll;
        u32 fp;
@@ -10758,7 +10883,7 @@ static void ironlake_pch_clock_get(struct intel_crtc *crtc,
 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
                                             struct drm_crtc *crtc)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
        struct drm_display_mode *mode;
@@ -10810,38 +10935,6 @@ struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
        return mode;
 }
 
-void intel_mark_busy(struct drm_i915_private *dev_priv)
-{
-       if (dev_priv->mm.busy)
-               return;
-
-       intel_runtime_pm_get(dev_priv);
-       i915_update_gfx_val(dev_priv);
-       if (INTEL_GEN(dev_priv) >= 6)
-               gen6_rps_busy(dev_priv);
-       dev_priv->mm.busy = true;
-}
-
-void intel_mark_idle(struct drm_i915_private *dev_priv)
-{
-       if (!dev_priv->mm.busy)
-               return;
-
-       dev_priv->mm.busy = false;
-
-       if (INTEL_GEN(dev_priv) >= 6)
-               gen6_rps_idle(dev_priv);
-
-       intel_runtime_pm_put(dev_priv);
-}
-
-void intel_free_flip_work(struct intel_flip_work *work)
-{
-       kfree(work->old_connector_state);
-       kfree(work->new_connector_state);
-       kfree(work);
-}
-
 static void intel_crtc_destroy(struct drm_crtc *crtc)
 {
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
@@ -10849,162 +10942,117 @@ static void intel_crtc_destroy(struct drm_crtc *crtc)
        struct intel_flip_work *work;
 
        spin_lock_irq(&dev->event_lock);
-       while (!list_empty(&intel_crtc->flip_work)) {
-               work = list_first_entry(&intel_crtc->flip_work,
-                                       struct intel_flip_work, head);
-               list_del_init(&work->head);
-               spin_unlock_irq(&dev->event_lock);
+       work = intel_crtc->flip_work;
+       intel_crtc->flip_work = NULL;
+       spin_unlock_irq(&dev->event_lock);
 
+       if (work) {
                cancel_work_sync(&work->mmio_work);
                cancel_work_sync(&work->unpin_work);
-               intel_free_flip_work(work);
-
-               spin_lock_irq(&dev->event_lock);
+               kfree(work);
        }
-       spin_unlock_irq(&dev->event_lock);
 
        drm_crtc_cleanup(crtc);
 
        kfree(intel_crtc);
 }
 
-static void intel_crtc_post_flip_update(struct intel_flip_work *work,
-                                       struct drm_crtc *crtc)
-{
-       struct intel_crtc_state *crtc_state = work->new_crtc_state;
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
-       if (crtc_state->disable_cxsr)
-               intel_crtc->wm.cxsr_allowed = true;
-
-       if (crtc_state->update_wm_post && crtc_state->base.active)
-               intel_update_watermarks(crtc);
-
-       if (work->num_planes > 0 &&
-           work->old_plane_state[0]->base.plane == crtc->primary) {
-               struct intel_plane_state *plane_state =
-                       work->new_plane_state[0];
-
-               if (plane_state->visible &&
-                   (needs_modeset(&crtc_state->base) ||
-                    !work->old_plane_state[0]->visible))
-                       intel_post_enable_primary(crtc);
-       }
-}
-
 static void intel_unpin_work_fn(struct work_struct *__work)
 {
        struct intel_flip_work *work =
                container_of(__work, struct intel_flip_work, unpin_work);
-       struct drm_crtc *crtc = work->old_crtc_state->base.crtc;
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       int i;
-
-       if (work->fb_bits)
-               intel_frontbuffer_flip_complete(dev, work->fb_bits);
-
-       /*
-        * Unless work->can_async_unpin is false, there's no way to ensure
-        * that work->new_crtc_state contains valid memory during unpin
-        * because intel_atomic_commit may free it before this runs.
-        */
-       if (!work->can_async_unpin) {
-               intel_crtc_post_flip_update(work, crtc);
-
-               if (dev_priv->display.optimize_watermarks)
-                       dev_priv->display.optimize_watermarks(work->new_crtc_state);
-       }
-
-       if (work->fb_bits & to_intel_plane(crtc->primary)->frontbuffer_bit)
-               intel_fbc_post_update(intel_crtc);
-
-       if (work->put_power_domains)
-               modeset_put_power_domains(dev_priv, work->put_power_domains);
-
-       /* Make sure mmio work is completely finished before freeing all state here. */
-       flush_work(&work->mmio_work);
-
-       if (!work->can_async_unpin &&
-           (work->new_crtc_state->update_pipe ||
-            needs_modeset(&work->new_crtc_state->base))) {
-               /* This must be called before work is unpinned for serialization. */
-               intel_modeset_verify_crtc(crtc, &work->old_crtc_state->base,
-                                         &work->new_crtc_state->base);
-
-               for (i = 0; i < work->num_new_connectors; i++) {
-                       struct drm_connector_state *conn_state =
-                               work->new_connector_state[i];
-                       struct drm_connector *con = conn_state->connector;
-
-                       WARN_ON(!con);
-
-                       intel_connector_verify_state(to_intel_connector(con),
-                                                    conn_state);
-               }
-       }
-
-       for (i = 0; i < work->num_old_connectors; i++) {
-               struct drm_connector_state *old_con_state =
-                       work->old_connector_state[i];
-               struct drm_connector *con =
-                       old_con_state->connector;
+       struct intel_crtc *crtc = to_intel_crtc(work->crtc);
+       struct drm_device *dev = crtc->base.dev;
+       struct drm_plane *primary = crtc->base.primary;
 
-               con->funcs->atomic_destroy_state(con, old_con_state);
-       }
+       if (is_mmio_work(work))
+               flush_work(&work->mmio_work);
 
-       if (!work->can_async_unpin || !list_empty(&work->head)) {
-               spin_lock_irq(&dev->event_lock);
-               WARN(list_empty(&work->head) != work->can_async_unpin,
-                    "[CRTC:%i] Pin work %p async %i with %i planes, active %i -> %i ms %i\n",
-                    crtc->base.id, work, work->can_async_unpin, work->num_planes,
-                    work->old_crtc_state->base.active, work->new_crtc_state->base.active,
-                    needs_modeset(&work->new_crtc_state->base));
+       mutex_lock(&dev->struct_mutex);
+       intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
+       drm_gem_object_unreference(&work->pending_flip_obj->base);
 
-               if (!list_empty(&work->head))
-                       list_del(&work->head);
+       if (work->flip_queued_req)
+               i915_gem_request_assign(&work->flip_queued_req, NULL);
+       mutex_unlock(&dev->struct_mutex);
 
-               wake_up_all(&dev_priv->pending_flip_queue);
-               spin_unlock_irq(&dev->event_lock);
-       }
+       intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
+       intel_fbc_post_update(crtc);
+       drm_framebuffer_unreference(work->old_fb);
 
-       /* New crtc_state freed? */
-       if (work->free_new_crtc_state)
-               intel_crtc_destroy_state(crtc, &work->new_crtc_state->base);
+       BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
+       atomic_dec(&crtc->unpin_work_count);
 
-       intel_crtc_destroy_state(crtc, &work->old_crtc_state->base);
+       kfree(work);
+}
 
-       for (i = 0; i < work->num_planes; i++) {
-               struct intel_plane_state *old_plane_state =
-                       work->old_plane_state[i];
-               struct drm_framebuffer *old_fb = old_plane_state->base.fb;
-               struct drm_plane *plane = old_plane_state->base.plane;
-               struct drm_i915_gem_request *req;
+/* Is 'a' after or equal to 'b'? */
+static bool g4x_flip_count_after_eq(u32 a, u32 b)
+{
+       return !((a - b) & 0x80000000);
+}
 
-               req = old_plane_state->wait_req;
-               old_plane_state->wait_req = NULL;
-               if (req)
-                       i915_gem_request_unreference(req);
+static bool __pageflip_finished_cs(struct intel_crtc *crtc,
+                                  struct intel_flip_work *work)
+{
+       struct drm_device *dev = crtc->base.dev;
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       unsigned reset_counter;
 
-               fence_put(old_plane_state->base.fence);
-               old_plane_state->base.fence = NULL;
+       reset_counter = i915_reset_counter(&dev_priv->gpu_error);
+       if (crtc->reset_counter != reset_counter)
+               return true;
 
-               if (old_fb &&
-                   (plane->type != DRM_PLANE_TYPE_CURSOR ||
-                    !INTEL_INFO(dev_priv)->cursor_needs_physical)) {
-                       mutex_lock(&dev->struct_mutex);
-                       intel_unpin_fb_obj(old_fb, old_plane_state->base.rotation);
-                       mutex_unlock(&dev->struct_mutex);
-               }
+       /*
+        * The relevant registers doen't exist on pre-ctg.
+        * As the flip done interrupt doesn't trigger for mmio
+        * flips on gmch platforms, a flip count check isn't
+        * really needed there. But since ctg has the registers,
+        * include it in the check anyway.
+        */
+       if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
+               return true;
 
-               intel_plane_destroy_state(plane, &old_plane_state->base);
-       }
+       /*
+        * BDW signals flip done immediately if the plane
+        * is disabled, even if the plane enable is already
+        * armed to occur at the next vblank :(
+        */
 
-       if (!WARN_ON(atomic_read(&intel_crtc->unpin_work_count) == 0))
-               atomic_dec(&intel_crtc->unpin_work_count);
+       /*
+        * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
+        * used the same base address. In that case the mmio flip might
+        * have completed, but the CS hasn't even executed the flip yet.
+        *
+        * A flip count check isn't enough as the CS might have updated
+        * the base address just after start of vblank, but before we
+        * managed to process the interrupt. This means we'd complete the
+        * CS flip too soon.
+        *
+        * Combining both checks should get us a good enough result. It may
+        * still happen that the CS flip has been executed, but has not
+        * yet actually completed. But in case the base address is the same
+        * anyway, we don't really care.
+        */
+       return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
+               crtc->flip_work->gtt_offset &&
+               g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
+                                   crtc->flip_work->flip_count);
+}
 
-       intel_free_flip_work(work);
+static bool
+__pageflip_finished_mmio(struct intel_crtc *crtc,
+                              struct intel_flip_work *work)
+{
+       /*
+        * MMIO work completes when vblank is different from
+        * flip_queued_vblank.
+        *
+        * Reset counter value doesn't matter, this is handled by
+        * i915_wait_request finishing early, so no need to handle
+        * reset here.
+        */
+       return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
 }
 
 
@@ -11016,16 +11064,15 @@ static bool pageflip_finished(struct intel_crtc *crtc,
 
        smp_rmb();
 
-       /*
-        * MMIO work completes when vblank is different from
-        * flip_queued_vblank.
-        */
-       return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
+       if (is_mmio_work(work))
+               return __pageflip_finished_mmio(crtc, work);
+       else
+               return __pageflip_finished_cs(crtc, work);
 }
 
-void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
+void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
 {
-       struct drm_device *dev = dev_priv->dev;
+       struct drm_device *dev = &dev_priv->drm;
        struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_flip_work *work;
@@ -11040,88 +11087,746 @@ void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
         * lost pageflips) so needs the full irqsave spinlocks.
         */
        spin_lock_irqsave(&dev->event_lock, flags);
-       while (!list_empty(&intel_crtc->flip_work)) {
-               work = list_first_entry(&intel_crtc->flip_work,
-                                       struct intel_flip_work,
-                                       head);
+       work = intel_crtc->flip_work;
 
-               if (!pageflip_finished(intel_crtc, work) ||
-                   work_busy(&work->unpin_work))
-                       break;
+       if (work != NULL &&
+           !is_mmio_work(work) &&
+           pageflip_finished(intel_crtc, work))
+               page_flip_completed(intel_crtc);
 
-               page_flip_completed(intel_crtc, work);
-       }
        spin_unlock_irqrestore(&dev->event_lock, flags);
 }
 
-static void intel_mmio_flip_work_func(struct work_struct *w)
+void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
 {
-       struct intel_flip_work *work =
-               container_of(w, struct intel_flip_work, mmio_work);
-       struct drm_crtc *crtc = work->old_crtc_state->base.crtc;
+       struct drm_device *dev = &dev_priv->drm;
+       struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       struct intel_crtc_state *crtc_state = work->new_crtc_state;
-       struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct drm_i915_gem_request *req;
-       int i, ret;
-
-       if (!needs_modeset(&crtc_state->base) && crtc_state->update_pipe) {
-               work->put_power_domains =
-                       modeset_get_crtc_power_domains(crtc, crtc_state);
-       }
-
-       for (i = 0; i < work->num_planes; i++) {
-               struct intel_plane_state *old_plane_state = work->old_plane_state[i];
-
-               /* For framebuffer backed by dmabuf, wait for fence */
-               if (old_plane_state->base.fence)
-                       WARN_ON(fence_wait(old_plane_state->base.fence, false) < 0);
+       struct intel_flip_work *work;
+       unsigned long flags;
 
-               req = old_plane_state->wait_req;
-               if (!req)
-                       continue;
+       /* Ignore early vblank irqs */
+       if (!crtc)
+               return;
 
-               WARN_ON(__i915_wait_request(req, false, NULL,
-                                           &dev_priv->rps.mmioflips));
-       }
+       /*
+        * This is called both by irq handlers and the reset code (to complete
+        * lost pageflips) so needs the full irqsave spinlocks.
+        */
+       spin_lock_irqsave(&dev->event_lock, flags);
+       work = intel_crtc->flip_work;
 
-       ret = drm_crtc_vblank_get(crtc);
-       I915_STATE_WARN(ret < 0, "enabling vblank failed with %i\n", ret);
+       if (work != NULL &&
+           is_mmio_work(work) &&
+           pageflip_finished(intel_crtc, work))
+               page_flip_completed(intel_crtc);
 
-       if (work->num_planes &&
-           work->old_plane_state[0]->base.plane == crtc->primary)
-               intel_fbc_enable(intel_crtc, work->new_crtc_state, work->new_plane_state[0]);
+       spin_unlock_irqrestore(&dev->event_lock, flags);
+}
 
-       intel_frontbuffer_flip_prepare(dev, work->fb_bits);
+static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
+                                              struct intel_flip_work *work)
+{
+       work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
 
-       intel_pipe_update_start(intel_crtc);
-       if (!needs_modeset(&crtc_state->base)) {
-               if (crtc_state->base.color_mgmt_changed || crtc_state->update_pipe) {
-                       intel_color_set_csc(&crtc_state->base);
-                       intel_color_load_luts(&crtc_state->base);
-               }
+       /* Ensure that the work item is consistent when activating it ... */
+       smp_mb__before_atomic();
+       atomic_set(&work->pending, 1);
+}
 
-               if (crtc_state->update_pipe)
-                       intel_update_pipe_config(intel_crtc, work->old_crtc_state);
-               else if (INTEL_INFO(dev)->gen >= 9)
-                       skl_detach_scalers(intel_crtc);
-       }
+static int intel_gen2_queue_flip(struct drm_device *dev,
+                                struct drm_crtc *crtc,
+                                struct drm_framebuffer *fb,
+                                struct drm_i915_gem_object *obj,
+                                struct drm_i915_gem_request *req,
+                                uint32_t flags)
+{
+       struct intel_engine_cs *engine = req->engine;
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       u32 flip_mask;
+       int ret;
 
-       for (i = 0; i < work->num_planes; i++) {
-               struct intel_plane_state *new_plane_state = work->new_plane_state[i];
-               struct intel_plane *plane = to_intel_plane(new_plane_state->base.plane);
+       ret = intel_ring_begin(req, 6);
+       if (ret)
+               return ret;
 
-               if (new_plane_state->visible)
-                       plane->update_plane(&plane->base, crtc_state, new_plane_state);
-               else
-                       plane->disable_plane(&plane->base, crtc);
-       }
+       /* Can't queue multiple flips, so wait for the previous
+        * one to finish before executing the next.
+        */
+       if (intel_crtc->plane)
+               flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
+       else
+               flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
+       intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
+       intel_ring_emit(engine, MI_NOOP);
+       intel_ring_emit(engine, MI_DISPLAY_FLIP |
+                       MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
+       intel_ring_emit(engine, fb->pitches[0]);
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
+       intel_ring_emit(engine, 0); /* aux display base address, unused */
 
-       intel_pipe_update_end(intel_crtc, work);
+       return 0;
 }
 
-/**
+static int intel_gen3_queue_flip(struct drm_device *dev,
+                                struct drm_crtc *crtc,
+                                struct drm_framebuffer *fb,
+                                struct drm_i915_gem_object *obj,
+                                struct drm_i915_gem_request *req,
+                                uint32_t flags)
+{
+       struct intel_engine_cs *engine = req->engine;
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       u32 flip_mask;
+       int ret;
+
+       ret = intel_ring_begin(req, 6);
+       if (ret)
+               return ret;
+
+       if (intel_crtc->plane)
+               flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
+       else
+               flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
+       intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
+       intel_ring_emit(engine, MI_NOOP);
+       intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
+                       MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
+       intel_ring_emit(engine, fb->pitches[0]);
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
+       intel_ring_emit(engine, MI_NOOP);
+
+       return 0;
+}
+
+static int intel_gen4_queue_flip(struct drm_device *dev,
+                                struct drm_crtc *crtc,
+                                struct drm_framebuffer *fb,
+                                struct drm_i915_gem_object *obj,
+                                struct drm_i915_gem_request *req,
+                                uint32_t flags)
+{
+       struct intel_engine_cs *engine = req->engine;
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       uint32_t pf, pipesrc;
+       int ret;
+
+       ret = intel_ring_begin(req, 4);
+       if (ret)
+               return ret;
+
+       /* i965+ uses the linear or tiled offsets from the
+        * Display Registers (which do not change across a page-flip)
+        * so we need only reprogram the base address.
+        */
+       intel_ring_emit(engine, MI_DISPLAY_FLIP |
+                       MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
+       intel_ring_emit(engine, fb->pitches[0]);
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
+                       obj->tiling_mode);
+
+       /* XXX Enabling the panel-fitter across page-flip is so far
+        * untested on non-native modes, so ignore it for now.
+        * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
+        */
+       pf = 0;
+       pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
+       intel_ring_emit(engine, pf | pipesrc);
+
+       return 0;
+}
+
+static int intel_gen6_queue_flip(struct drm_device *dev,
+                                struct drm_crtc *crtc,
+                                struct drm_framebuffer *fb,
+                                struct drm_i915_gem_object *obj,
+                                struct drm_i915_gem_request *req,
+                                uint32_t flags)
+{
+       struct intel_engine_cs *engine = req->engine;
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       uint32_t pf, pipesrc;
+       int ret;
+
+       ret = intel_ring_begin(req, 4);
+       if (ret)
+               return ret;
+
+       intel_ring_emit(engine, MI_DISPLAY_FLIP |
+                       MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
+       intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
+
+       /* Contrary to the suggestions in the documentation,
+        * "Enable Panel Fitter" does not seem to be required when page
+        * flipping with a non-native mode, and worse causes a normal
+        * modeset to fail.
+        * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
+        */
+       pf = 0;
+       pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
+       intel_ring_emit(engine, pf | pipesrc);
+
+       return 0;
+}
+
+static int intel_gen7_queue_flip(struct drm_device *dev,
+                                struct drm_crtc *crtc,
+                                struct drm_framebuffer *fb,
+                                struct drm_i915_gem_object *obj,
+                                struct drm_i915_gem_request *req,
+                                uint32_t flags)
+{
+       struct intel_engine_cs *engine = req->engine;
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       uint32_t plane_bit = 0;
+       int len, ret;
+
+       switch (intel_crtc->plane) {
+       case PLANE_A:
+               plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
+               break;
+       case PLANE_B:
+               plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
+               break;
+       case PLANE_C:
+               plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
+               break;
+       default:
+               WARN_ONCE(1, "unknown plane in flip command\n");
+               return -ENODEV;
+       }
+
+       len = 4;
+       if (engine->id == RCS) {
+               len += 6;
+               /*
+                * On Gen 8, SRM is now taking an extra dword to accommodate
+                * 48bits addresses, and we need a NOOP for the batch size to
+                * stay even.
+                */
+               if (IS_GEN8(dev))
+                       len += 2;
+       }
+
+       /*
+        * BSpec MI_DISPLAY_FLIP for IVB:
+        * "The full packet must be contained within the same cache line."
+        *
+        * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
+        * cacheline, if we ever start emitting more commands before
+        * the MI_DISPLAY_FLIP we may need to first emit everything else,
+        * then do the cacheline alignment, and finally emit the
+        * MI_DISPLAY_FLIP.
+        */
+       ret = intel_ring_cacheline_align(req);
+       if (ret)
+               return ret;
+
+       ret = intel_ring_begin(req, len);
+       if (ret)
+               return ret;
+
+       /* Unmask the flip-done completion message. Note that the bspec says that
+        * we should do this for both the BCS and RCS, and that we must not unmask
+        * more than one flip event at any time (or ensure that one flip message
+        * can be sent by waiting for flip-done prior to queueing new flips).
+        * Experimentation says that BCS works despite DERRMR masking all
+        * flip-done completion events and that unmasking all planes at once
+        * for the RCS also doesn't appear to drop events. Setting the DERRMR
+        * to zero does lead to lockups within MI_DISPLAY_FLIP.
+        */
+       if (engine->id == RCS) {
+               intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
+               intel_ring_emit_reg(engine, DERRMR);
+               intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
+                                         DERRMR_PIPEB_PRI_FLIP_DONE |
+                                         DERRMR_PIPEC_PRI_FLIP_DONE));
+               if (IS_GEN8(dev))
+                       intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
+                                             MI_SRM_LRM_GLOBAL_GTT);
+               else
+                       intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
+                                             MI_SRM_LRM_GLOBAL_GTT);
+               intel_ring_emit_reg(engine, DERRMR);
+               intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
+               if (IS_GEN8(dev)) {
+                       intel_ring_emit(engine, 0);
+                       intel_ring_emit(engine, MI_NOOP);
+               }
+       }
+
+       intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
+       intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
+       intel_ring_emit(engine, (MI_NOOP));
+
+       return 0;
+}
+
+static bool use_mmio_flip(struct intel_engine_cs *engine,
+                         struct drm_i915_gem_object *obj)
+{
+       struct reservation_object *resv;
+
+       /*
+        * This is not being used for older platforms, because
+        * non-availability of flip done interrupt forces us to use
+        * CS flips. Older platforms derive flip done using some clever
+        * tricks involving the flip_pending status bits and vblank irqs.
+        * So using MMIO flips there would disrupt this mechanism.
+        */
+
+       if (engine == NULL)
+               return true;
+
+       if (INTEL_GEN(engine->i915) < 5)
+               return false;
+
+       if (i915.use_mmio_flip < 0)
+               return false;
+       else if (i915.use_mmio_flip > 0)
+               return true;
+       else if (i915.enable_execlists)
+               return true;
+
+       resv = i915_gem_object_get_dmabuf_resv(obj);
+       if (resv && !reservation_object_test_signaled_rcu(resv, false))
+               return true;
+
+       return engine != i915_gem_request_get_engine(obj->last_write_req);
+}
+
+static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
+                            unsigned int rotation,
+                            struct intel_flip_work *work)
+{
+       struct drm_device *dev = intel_crtc->base.dev;
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
+       const enum pipe pipe = intel_crtc->pipe;
+       u32 ctl, stride, tile_height;
+
+       ctl = I915_READ(PLANE_CTL(pipe, 0));
+       ctl &= ~PLANE_CTL_TILED_MASK;
+       switch (fb->modifier[0]) {
+       case DRM_FORMAT_MOD_NONE:
+               break;
+       case I915_FORMAT_MOD_X_TILED:
+               ctl |= PLANE_CTL_TILED_X;
+               break;
+       case I915_FORMAT_MOD_Y_TILED:
+               ctl |= PLANE_CTL_TILED_Y;
+               break;
+       case I915_FORMAT_MOD_Yf_TILED:
+               ctl |= PLANE_CTL_TILED_YF;
+               break;
+       default:
+               MISSING_CASE(fb->modifier[0]);
+       }
+
+       /*
+        * The stride is either expressed as a multiple of 64 bytes chunks for
+        * linear buffers or in number of tiles for tiled buffers.
+        */
+       if (intel_rotation_90_or_270(rotation)) {
+               /* stride = Surface height in tiles */
+               tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
+               stride = DIV_ROUND_UP(fb->height, tile_height);
+       } else {
+               stride = fb->pitches[0] /
+                       intel_fb_stride_alignment(dev_priv, fb->modifier[0],
+                                                 fb->pixel_format);
+       }
+
+       /*
+        * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
+        * PLANE_SURF updates, the update is then guaranteed to be atomic.
+        */
+       I915_WRITE(PLANE_CTL(pipe, 0), ctl);
+       I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
+
+       I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
+       POSTING_READ(PLANE_SURF(pipe, 0));
+}
+
+static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
+                            struct intel_flip_work *work)
+{
+       struct drm_device *dev = intel_crtc->base.dev;
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct intel_framebuffer *intel_fb =
+               to_intel_framebuffer(intel_crtc->base.primary->fb);
+       struct drm_i915_gem_object *obj = intel_fb->obj;
+       i915_reg_t reg = DSPCNTR(intel_crtc->plane);
+       u32 dspcntr;
+
+       dspcntr = I915_READ(reg);
+
+       if (obj->tiling_mode != I915_TILING_NONE)
+               dspcntr |= DISPPLANE_TILED;
+       else
+               dspcntr &= ~DISPPLANE_TILED;
+
+       I915_WRITE(reg, dspcntr);
+
+       I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
+       POSTING_READ(DSPSURF(intel_crtc->plane));
+}
+
+static void intel_mmio_flip_work_func(struct work_struct *w)
+{
+       struct intel_flip_work *work =
+               container_of(w, struct intel_flip_work, mmio_work);
+       struct intel_crtc *crtc = to_intel_crtc(work->crtc);
+       struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
+       struct intel_framebuffer *intel_fb =
+               to_intel_framebuffer(crtc->base.primary->fb);
+       struct drm_i915_gem_object *obj = intel_fb->obj;
+       struct reservation_object *resv;
+
+       if (work->flip_queued_req)
+               WARN_ON(__i915_wait_request(work->flip_queued_req,
+                                           false, NULL,
+                                           &dev_priv->rps.mmioflips));
+
+       /* For framebuffer backed by dmabuf, wait for fence */
+       resv = i915_gem_object_get_dmabuf_resv(obj);
+       if (resv)
+               WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
+                                                           MAX_SCHEDULE_TIMEOUT) < 0);
+
+       intel_pipe_update_start(crtc);
+
+       if (INTEL_GEN(dev_priv) >= 9)
+               skl_do_mmio_flip(crtc, work->rotation, work);
+       else
+               /* use_mmio_flip() retricts MMIO flips to ilk+ */
+               ilk_do_mmio_flip(crtc, work);
+
+       intel_pipe_update_end(crtc, work);
+}
+
+static int intel_default_queue_flip(struct drm_device *dev,
+                                   struct drm_crtc *crtc,
+                                   struct drm_framebuffer *fb,
+                                   struct drm_i915_gem_object *obj,
+                                   struct drm_i915_gem_request *req,
+                                   uint32_t flags)
+{
+       return -ENODEV;
+}
+
+static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
+                                     struct intel_crtc *intel_crtc,
+                                     struct intel_flip_work *work)
+{
+       u32 addr, vblank;
+
+       if (!atomic_read(&work->pending))
+               return false;
+
+       smp_rmb();
+
+       vblank = intel_crtc_get_vblank_counter(intel_crtc);
+       if (work->flip_ready_vblank == 0) {
+               if (work->flip_queued_req &&
+                   !i915_gem_request_completed(work->flip_queued_req))
+                       return false;
+
+               work->flip_ready_vblank = vblank;
+       }
+
+       if (vblank - work->flip_ready_vblank < 3)
+               return false;
+
+       /* Potential stall - if we see that the flip has happened,
+        * assume a missed interrupt. */
+       if (INTEL_GEN(dev_priv) >= 4)
+               addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
+       else
+               addr = I915_READ(DSPADDR(intel_crtc->plane));
+
+       /* There is a potential issue here with a false positive after a flip
+        * to the same address. We could address this by checking for a
+        * non-incrementing frame counter.
+        */
+       return addr == work->gtt_offset;
+}
+
+void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
+{
+       struct drm_device *dev = &dev_priv->drm;
+       struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct intel_flip_work *work;
+
+       WARN_ON(!in_interrupt());
+
+       if (crtc == NULL)
+               return;
+
+       spin_lock(&dev->event_lock);
+       work = intel_crtc->flip_work;
+
+       if (work != NULL && !is_mmio_work(work) &&
+           __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
+               WARN_ONCE(1,
+                         "Kicking stuck page flip: queued at %d, now %d\n",
+                       work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
+               page_flip_completed(intel_crtc);
+               work = NULL;
+       }
+
+       if (work != NULL && !is_mmio_work(work) &&
+           intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
+               intel_queue_rps_boost_for_request(work->flip_queued_req);
+       spin_unlock(&dev->event_lock);
+}
+
+static int intel_crtc_page_flip(struct drm_crtc *crtc,
+                               struct drm_framebuffer *fb,
+                               struct drm_pending_vblank_event *event,
+                               uint32_t page_flip_flags)
+{
+       struct drm_device *dev = crtc->dev;
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct drm_framebuffer *old_fb = crtc->primary->fb;
+       struct drm_i915_gem_object *obj = intel_fb_obj(fb);
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct drm_plane *primary = crtc->primary;
+       enum pipe pipe = intel_crtc->pipe;
+       struct intel_flip_work *work;
+       struct intel_engine_cs *engine;
+       bool mmio_flip;
+       struct drm_i915_gem_request *request = NULL;
+       int ret;
+
+       /*
+        * drm_mode_page_flip_ioctl() should already catch this, but double
+        * check to be safe.  In the future we may enable pageflipping from
+        * a disabled primary plane.
+        */
+       if (WARN_ON(intel_fb_obj(old_fb) == NULL))
+               return -EBUSY;
+
+       /* Can't change pixel format via MI display flips. */
+       if (fb->pixel_format != crtc->primary->fb->pixel_format)
+               return -EINVAL;
+
+       /*
+        * TILEOFF/LINOFF registers can't be changed via MI display flips.
+        * Note that pitch changes could also affect these register.
+        */
+       if (INTEL_INFO(dev)->gen > 3 &&
+           (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
+            fb->pitches[0] != crtc->primary->fb->pitches[0]))
+               return -EINVAL;
+
+       if (i915_terminally_wedged(&dev_priv->gpu_error))
+               goto out_hang;
+
+       work = kzalloc(sizeof(*work), GFP_KERNEL);
+       if (work == NULL)
+               return -ENOMEM;
+
+       work->event = event;
+       work->crtc = crtc;
+       work->old_fb = old_fb;
+       INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
+
+       ret = drm_crtc_vblank_get(crtc);
+       if (ret)
+               goto free_work;
+
+       /* We borrow the event spin lock for protecting flip_work */
+       spin_lock_irq(&dev->event_lock);
+       if (intel_crtc->flip_work) {
+               /* Before declaring the flip queue wedged, check if
+                * the hardware completed the operation behind our backs.
+                */
+               if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
+                       DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
+                       page_flip_completed(intel_crtc);
+               } else {
+                       DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
+                       spin_unlock_irq(&dev->event_lock);
+
+                       drm_crtc_vblank_put(crtc);
+                       kfree(work);
+                       return -EBUSY;
+               }
+       }
+       intel_crtc->flip_work = work;
+       spin_unlock_irq(&dev->event_lock);
+
+       if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
+               flush_workqueue(dev_priv->wq);
+
+       /* Reference the objects for the scheduled work. */
+       drm_framebuffer_reference(work->old_fb);
+       drm_gem_object_reference(&obj->base);
+
+       crtc->primary->fb = fb;
+       update_state_fb(crtc->primary);
+
+       intel_fbc_pre_update(intel_crtc, intel_crtc->config,
+                            to_intel_plane_state(primary->state));
+
+       work->pending_flip_obj = obj;
+
+       ret = i915_mutex_lock_interruptible(dev);
+       if (ret)
+               goto cleanup;
+
+       intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
+       if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
+               ret = -EIO;
+               goto cleanup;
+       }
+
+       atomic_inc(&intel_crtc->unpin_work_count);
+
+       if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
+               work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
+
+       if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
+               engine = &dev_priv->engine[BCS];
+               if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
+                       /* vlv: DISPLAY_FLIP fails to change tiling */
+                       engine = NULL;
+       } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
+               engine = &dev_priv->engine[BCS];
+       } else if (INTEL_INFO(dev)->gen >= 7) {
+               engine = i915_gem_request_get_engine(obj->last_write_req);
+               if (engine == NULL || engine->id != RCS)
+                       engine = &dev_priv->engine[BCS];
+       } else {
+               engine = &dev_priv->engine[RCS];
+       }
+
+       mmio_flip = use_mmio_flip(engine, obj);
+
+       /* When using CS flips, we want to emit semaphores between rings.
+        * However, when using mmio flips we will create a task to do the
+        * synchronisation, so all we want here is to pin the framebuffer
+        * into the display plane and skip any waits.
+        */
+       if (!mmio_flip) {
+               ret = i915_gem_object_sync(obj, engine, &request);
+               if (!ret && !request) {
+                       request = i915_gem_request_alloc(engine, NULL);
+                       ret = PTR_ERR_OR_ZERO(request);
+               }
+
+               if (ret)
+                       goto cleanup_pending;
+       }
+
+       ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
+       if (ret)
+               goto cleanup_pending;
+
+       work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
+                                                 obj, 0);
+       work->gtt_offset += intel_crtc->dspaddr_offset;
+       work->rotation = crtc->primary->state->rotation;
+
+       if (mmio_flip) {
+               INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
+
+               i915_gem_request_assign(&work->flip_queued_req,
+                                       obj->last_write_req);
+
+               schedule_work(&work->mmio_work);
+       } else {
+               i915_gem_request_assign(&work->flip_queued_req, request);
+               ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
+                                                  page_flip_flags);
+               if (ret)
+                       goto cleanup_unpin;
+
+               intel_mark_page_flip_active(intel_crtc, work);
+
+               i915_add_request_no_flush(request);
+       }
+
+       i915_gem_track_fb(intel_fb_obj(old_fb), obj,
+                         to_intel_plane(primary)->frontbuffer_bit);
+       mutex_unlock(&dev->struct_mutex);
+
+       intel_frontbuffer_flip_prepare(dev,
+                                      to_intel_plane(primary)->frontbuffer_bit);
+
+       trace_i915_flip_request(intel_crtc->plane, obj);
+
+       return 0;
+
+cleanup_unpin:
+       intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
+cleanup_pending:
+       if (!IS_ERR_OR_NULL(request))
+               i915_add_request_no_flush(request);
+       atomic_dec(&intel_crtc->unpin_work_count);
+       mutex_unlock(&dev->struct_mutex);
+cleanup:
+       crtc->primary->fb = old_fb;
+       update_state_fb(crtc->primary);
+
+       drm_gem_object_unreference_unlocked(&obj->base);
+       drm_framebuffer_unreference(work->old_fb);
+
+       spin_lock_irq(&dev->event_lock);
+       intel_crtc->flip_work = NULL;
+       spin_unlock_irq(&dev->event_lock);
+
+       drm_crtc_vblank_put(crtc);
+free_work:
+       kfree(work);
+
+       if (ret == -EIO) {
+               struct drm_atomic_state *state;
+               struct drm_plane_state *plane_state;
+
+out_hang:
+               state = drm_atomic_state_alloc(dev);
+               if (!state)
+                       return -ENOMEM;
+               state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
+
+retry:
+               plane_state = drm_atomic_get_plane_state(state, primary);
+               ret = PTR_ERR_OR_ZERO(plane_state);
+               if (!ret) {
+                       drm_atomic_set_fb_for_plane(plane_state, fb);
+
+                       ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
+                       if (!ret)
+                               ret = drm_atomic_commit(state);
+               }
+
+               if (ret == -EDEADLK) {
+                       drm_modeset_backoff(state->acquire_ctx);
+                       drm_atomic_state_clear(state);
+                       goto retry;
+               }
+
+               if (ret)
+                       drm_atomic_state_free(state);
+
+               if (ret == 0 && event) {
+                       spin_lock_irq(&dev->event_lock);
+                       drm_crtc_send_vblank_event(crtc, event);
+                       spin_unlock_irq(&dev->event_lock);
+               }
+       }
+       return ret;
+}
+
+
+/**
  * intel_wm_need_update - Check whether watermarks need updating
  * @plane: drm plane
  * @state: new plane state
@@ -11176,15 +11881,14 @@ int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
        struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_plane_state *old_plane_state =
                to_intel_plane_state(plane->state);
-       int idx = intel_crtc->base.base.id, ret;
        bool mode_changed = needs_modeset(crtc_state);
        bool was_crtc_enabled = crtc->state->active;
        bool is_crtc_enabled = crtc_state->active;
        bool turn_off, turn_on, visible, was_visible;
        struct drm_framebuffer *fb = plane_state->fb;
+       int ret;
 
-       if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
-           plane->type != DRM_PLANE_TYPE_CURSOR) {
+       if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
                ret = skl_update_scaler_plane(
                        to_intel_crtc_state(crtc_state),
                        to_intel_plane_state(plane_state));
@@ -11220,11 +11924,15 @@ int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
        turn_off = was_visible && (!visible || mode_changed);
        turn_on = visible && (!was_visible || mode_changed);
 
-       DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
-                        plane->base.id, fb ? fb->base.id : -1);
+       DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
+                        intel_crtc->base.base.id,
+                        intel_crtc->base.name,
+                        plane->base.id, plane->name,
+                        fb ? fb->base.id : -1);
 
-       DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
-                        plane->base.id, was_visible, visible,
+       DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
+                        plane->base.id, plane->name,
+                        was_visible, visible,
                         turn_off, turn_on, mode_changed);
 
        if (turn_on) {
@@ -11321,7 +12029,7 @@ static int intel_crtc_atomic_check(struct drm_crtc *crtc,
                                   struct drm_crtc_state *crtc_state)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_crtc_state *pipe_config =
                to_intel_crtc_state(crtc_state);
@@ -11397,6 +12105,8 @@ static int intel_crtc_atomic_check(struct drm_crtc *crtc,
 
 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
        .mode_set_base_atomic = intel_pipe_set_base_atomic,
+       .atomic_begin = intel_begin_crtc_commit,
+       .atomic_flush = intel_finish_crtc_commit,
        .atomic_check = intel_crtc_atomic_check,
 };
 
@@ -11513,7 +12223,8 @@ static void intel_dump_pipe_config(struct intel_crtc *crtc,
        struct intel_plane_state *state;
        struct drm_framebuffer *fb;
 
-       DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
+       DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
+                     crtc->base.base.id, crtc->base.name,
                      context, pipe_config, pipe_name(crtc->pipe));
 
        DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
@@ -11526,14 +12237,14 @@ static void intel_dump_pipe_config(struct intel_crtc *crtc,
                      pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
                      pipe_config->fdi_m_n.tu);
        DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
-                     pipe_config->has_dp_encoder,
+                     intel_crtc_has_dp_encoder(pipe_config),
                      pipe_config->lane_count,
                      pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
                      pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
                      pipe_config->dp_m_n.tu);
 
        DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
-                     pipe_config->has_dp_encoder,
+                     intel_crtc_has_dp_encoder(pipe_config),
                      pipe_config->lane_count,
                      pipe_config->dp_m2_n2.gmch_m,
                      pipe_config->dp_m2_n2.gmch_n,
@@ -11614,29 +12325,24 @@ static void intel_dump_pipe_config(struct intel_crtc *crtc,
                state = to_intel_plane_state(plane->state);
                fb = state->base.fb;
                if (!fb) {
-                       DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
-                               "disabled, scaler_id = %d\n",
-                               plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
-                               plane->base.id, intel_plane->pipe,
-                               (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
-                               drm_plane_index(plane), state->scaler_id);
+                       DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
+                                     plane->base.id, plane->name, state->scaler_id);
                        continue;
                }
 
-               DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
-                       plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
-                       plane->base.id, intel_plane->pipe,
-                       crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
-                       drm_plane_index(plane));
-               DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
-                       fb->base.id, fb->width, fb->height, fb->pixel_format);
-               DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
-                       state->scaler_id,
-                       state->src.x1 >> 16, state->src.y1 >> 16,
-                       drm_rect_width(&state->src) >> 16,
-                       drm_rect_height(&state->src) >> 16,
-                       state->dst.x1, state->dst.y1,
-                       drm_rect_width(&state->dst), drm_rect_height(&state->dst));
+               DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
+                             plane->base.id, plane->name);
+               DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
+                             fb->base.id, fb->width, fb->height,
+                             drm_get_format_name(fb->pixel_format));
+               DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
+                             state->scaler_id,
+                             state->src.x1 >> 16, state->src.y1 >> 16,
+                             drm_rect_width(&state->src) >> 16,
+                             drm_rect_height(&state->src) >> 16,
+                             state->dst.x1, state->dst.y1,
+                             drm_rect_width(&state->dst),
+                             drm_rect_height(&state->dst));
        }
 }
 
@@ -11671,7 +12377,7 @@ static bool check_digital_port_conflicts(struct drm_atomic_state *state)
                case INTEL_OUTPUT_UNKNOWN:
                        if (WARN_ON(!HAS_DDI(dev)))
                                break;
-               case INTEL_OUTPUT_DISPLAYPORT:
+               case INTEL_OUTPUT_DP:
                case INTEL_OUTPUT_HDMI:
                case INTEL_OUTPUT_EDP:
                        port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
@@ -11768,6 +12474,19 @@ intel_modeset_pipe_config(struct drm_crtc *crtc,
                               &pipe_config->pipe_src_w,
                               &pipe_config->pipe_src_h);
 
+       for_each_connector_in_state(state, connector, connector_state, i) {
+               if (connector_state->crtc != crtc)
+                       continue;
+
+               encoder = to_intel_encoder(connector_state->best_encoder);
+
+               /*
+                * Determine output_types before calling the .compute_config()
+                * hooks so that the hooks can use this information safely.
+                */
+               pipe_config->output_types |= 1 << encoder->type;
+       }
+
 encoder_retry:
        /* Ensure the port clock defaults are reset when retrying. */
        pipe_config->port_clock = 0;
@@ -12053,8 +12772,8 @@ intel_pipe_config_compare(struct drm_device *dev,
        PIPE_CONF_CHECK_I(fdi_lanes);
        PIPE_CONF_CHECK_M_N(fdi_m_n);
 
-       PIPE_CONF_CHECK_I(has_dp_encoder);
        PIPE_CONF_CHECK_I(lane_count);
+       PIPE_CONF_CHECK_X(lane_lat_optim_mask);
 
        if (INTEL_INFO(dev)->gen < 8) {
                PIPE_CONF_CHECK_M_N(dp_m_n);
@@ -12065,6 +12784,7 @@ intel_pipe_config_compare(struct drm_device *dev,
                PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
 
        PIPE_CONF_CHECK_I(has_dsi_encoder);
+       PIPE_CONF_CHECK_X(output_types);
 
        PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
        PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
@@ -12183,7 +12903,7 @@ static void verify_wm_state(struct drm_crtc *crtc,
                            struct drm_crtc_state *new_state)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct skl_ddb_allocation hw_ddb, *sw_ddb;
        struct skl_ddb_entry *hw_entry, *sw_entry;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
@@ -12236,8 +12956,7 @@ verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
                if (state->crtc != crtc)
                        continue;
 
-               intel_connector_verify_state(to_intel_connector(connector),
-                                            connector->state);
+               intel_connector_verify_state(to_intel_connector(connector));
 
                I915_STATE_WARN(state->best_encoder != encoder,
                     "connector's atomic encoder doesn't match legacy encoder\n");
@@ -12290,7 +13009,7 @@ verify_crtc_state(struct drm_crtc *crtc,
                  struct drm_crtc_state *new_crtc_state)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_encoder *encoder;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_crtc_state *pipe_config, *sw_config;
@@ -12298,13 +13017,13 @@ verify_crtc_state(struct drm_crtc *crtc,
        bool active;
 
        old_state = old_crtc_state->state;
-       __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
+       __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
        pipe_config = to_intel_crtc_state(old_crtc_state);
        memset(pipe_config, 0, sizeof(*pipe_config));
        pipe_config->base.crtc = crtc;
        pipe_config->base.state = old_state;
 
-       DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
+       DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
 
        active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
 
@@ -12333,8 +13052,10 @@ verify_crtc_state(struct drm_crtc *crtc,
                                "Encoder connected to wrong pipe %c\n",
                                pipe_name(pipe));
 
-               if (active)
+               if (active) {
+                       pipe_config->output_types |= 1 << encoder->type;
                        encoder->get_config(encoder, pipe_config);
+               }
        }
 
        if (!new_crtc_state->active)
@@ -12413,7 +13134,7 @@ verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
                         struct drm_crtc_state *old_crtc_state,
                         struct drm_crtc_state *new_crtc_state)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
        struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
 
@@ -12439,7 +13160,12 @@ intel_modeset_verify_crtc(struct drm_crtc *crtc,
                         struct drm_crtc_state *old_state,
                         struct drm_crtc_state *new_state)
 {
+       if (!needs_modeset(new_state) &&
+           !to_intel_crtc_state(new_state)->update_pipe)
+               return;
+
        verify_wm_state(crtc, new_state);
+       verify_connector_state(crtc->dev, crtc);
        verify_crtc_state(crtc, old_state, new_state);
        verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
 }
@@ -12447,7 +13173,7 @@ intel_modeset_verify_crtc(struct drm_crtc *crtc,
 static void
 verify_disabled_dpll_state(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int i;
 
        for (i = 0; i < dev_priv->num_shared_dpll; i++)
@@ -12494,7 +13220,7 @@ static void update_scanline_offset(struct intel_crtc *crtc)
 
                crtc->scanline_offset = vtotal - 1;
        } else if (HAS_DDI(dev) &&
-                  intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
+                  intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
                crtc->scanline_offset = 2;
        } else
                crtc->scanline_offset = 1;
@@ -12629,7 +13355,7 @@ static int intel_modeset_all_pipes(struct drm_atomic_state *state)
 static int intel_modeset_checks(struct drm_atomic_state *state)
 {
        struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
-       struct drm_i915_private *dev_priv = state->dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(state->dev);
        struct drm_crtc *crtc;
        struct drm_crtc_state *crtc_state;
        int ret = 0, i;
@@ -12661,7 +13387,7 @@ static int intel_modeset_checks(struct drm_atomic_state *state)
         */
        if (dev_priv->display.modeset_calc_cdclk) {
                if (!intel_state->cdclk_pll_vco)
-                       intel_state->cdclk_pll_vco = dev_priv->skl_vco_freq;
+                       intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
                if (!intel_state->cdclk_pll_vco)
                        intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
 
@@ -12670,7 +13396,7 @@ static int intel_modeset_checks(struct drm_atomic_state *state)
                        return ret;
 
                if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
-                   intel_state->cdclk_pll_vco != dev_priv->skl_vco_freq)
+                   intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
                        ret = intel_modeset_all_pipes(state);
 
                if (ret < 0)
@@ -12791,33 +13517,11 @@ static int intel_atomic_check(struct drm_device *dev,
        return calc_watermark_data(state);
 }
 
-static bool needs_work(struct drm_crtc_state *crtc_state)
-{
-       /* hw state checker needs to run */
-       if (needs_modeset(crtc_state))
-               return true;
-
-       /* unpin old fb's, possibly vblank update */
-       if (crtc_state->planes_changed)
-               return true;
-
-       /* pipe parameters need to be updated, and hw state checker */
-       if (to_intel_crtc_state(crtc_state)->update_pipe)
-               return true;
-
-       /* vblank event requested? */
-       if (crtc_state->event)
-               return true;
-
-       return false;
-}
-
 static int intel_atomic_prepare_commit(struct drm_device *dev,
                                       struct drm_atomic_state *state,
                                       bool nonblock)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct drm_plane_state *plane_state;
        struct drm_crtc_state *crtc_state;
        struct drm_plane *plane;
@@ -12825,49 +13529,15 @@ static int intel_atomic_prepare_commit(struct drm_device *dev,
        int i, ret;
 
        for_each_crtc_in_state(state, crtc, crtc_state, i) {
-               struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-               struct intel_flip_work *work;
-
-               if (!state->legacy_cursor_update) {
-                       ret = intel_crtc_wait_for_pending_flips(crtc);
-                       if (ret)
-                               return ret;
-
-                       if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
-                               flush_workqueue(dev_priv->wq);
-               }
-
-               /* test if we need to update something */
-               if (!needs_work(crtc_state))
+               if (state->legacy_cursor_update)
                        continue;
 
-               intel_state->work[i] = work =
-                       kzalloc(sizeof(**intel_state->work), GFP_KERNEL);
-
-               if (!work)
-                       return -ENOMEM;
-
-               if (needs_modeset(crtc_state) ||
-                   to_intel_crtc_state(crtc_state)->update_pipe) {
-                       work->num_old_connectors = hweight32(crtc->state->connector_mask);
-
-                       work->old_connector_state = kcalloc(work->num_old_connectors,
-                                                           sizeof(*work->old_connector_state),
-                                                           GFP_KERNEL);
-
-                       work->num_new_connectors = hweight32(crtc_state->connector_mask);
-                       work->new_connector_state = kcalloc(work->num_new_connectors,
-                                                           sizeof(*work->new_connector_state),
-                                                           GFP_KERNEL);
-
-                       if (!work->old_connector_state || !work->new_connector_state)
-                               return -ENOMEM;
-               }
-       }
+               ret = intel_crtc_wait_for_pending_flips(crtc);
+               if (ret)
+                       return ret;
 
-       if (intel_state->modeset && nonblock) {
-               DRM_DEBUG_ATOMIC("Nonblock modesets are not yet supported!\n");
-               return -EINVAL;
+               if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
+                       flush_workqueue(dev_priv->wq);
        }
 
        ret = mutex_lock_interruptible(&dev->struct_mutex);
@@ -12882,15 +13552,6 @@ static int intel_atomic_prepare_commit(struct drm_device *dev,
                        struct intel_plane_state *intel_plane_state =
                                to_intel_plane_state(plane_state);
 
-                       if (plane_state->fence) {
-                               long lret = fence_wait(plane_state->fence, true);
-
-                               if (lret < 0) {
-                                       ret = lret;
-                                       break;
-                               }
-                       }
-
                        if (!intel_plane_state->wait_req)
                                continue;
 
@@ -12920,215 +13581,128 @@ u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
        return dev->driver->get_vblank_counter(dev, crtc->pipe);
 }
 
-static void intel_prepare_work(struct drm_crtc *crtc,
-                              struct intel_flip_work *work,
-                              struct drm_atomic_state *state,
-                              struct drm_crtc_state *old_crtc_state)
+static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
+                                         struct drm_i915_private *dev_priv,
+                                         unsigned crtc_mask)
 {
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       struct drm_plane_state *old_plane_state;
-       struct drm_plane *plane;
-       int i, j = 0;
+       unsigned last_vblank_count[I915_MAX_PIPES];
+       enum pipe pipe;
+       int ret;
 
-       INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
-       INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
-       atomic_inc(&intel_crtc->unpin_work_count);
+       if (!crtc_mask)
+               return;
 
-       for_each_plane_in_state(state, plane, old_plane_state, i) {
-               struct intel_plane_state *old_state = to_intel_plane_state(old_plane_state);
-               struct intel_plane_state *new_state = to_intel_plane_state(plane->state);
+       for_each_pipe(dev_priv, pipe) {
+               struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
 
-               if (old_state->base.crtc != crtc &&
-                   new_state->base.crtc != crtc)
+               if (!((1 << pipe) & crtc_mask))
                        continue;
 
-               if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
-                       plane->fb = new_state->base.fb;
-                       crtc->x = new_state->base.src_x >> 16;
-                       crtc->y = new_state->base.src_y >> 16;
+               ret = drm_crtc_vblank_get(crtc);
+               if (WARN_ON(ret != 0)) {
+                       crtc_mask &= ~(1 << pipe);
+                       continue;
                }
 
-               old_state->wait_req = new_state->wait_req;
-               new_state->wait_req = NULL;
-
-               old_state->base.fence = new_state->base.fence;
-               new_state->base.fence = NULL;
-
-               /* remove plane state from the atomic state and move it to work */
-               old_plane_state->state = NULL;
-               state->planes[i] = NULL;
-               state->plane_states[i] = NULL;
-
-               work->old_plane_state[j] = old_state;
-               work->new_plane_state[j++] = new_state;
+               last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
        }
 
-       old_crtc_state->state = NULL;
-       state->crtcs[drm_crtc_index(crtc)] = NULL;
-       state->crtc_states[drm_crtc_index(crtc)] = NULL;
-
-       work->old_crtc_state = to_intel_crtc_state(old_crtc_state);
-       work->new_crtc_state = to_intel_crtc_state(crtc->state);
-       work->num_planes = j;
-
-       work->event = crtc->state->event;
-       crtc->state->event = NULL;
-
-       if (needs_modeset(crtc->state) || work->new_crtc_state->update_pipe) {
-               struct drm_connector *conn;
-               struct drm_connector_state *old_conn_state;
-               int k = 0;
-
-               j = 0;
-
-               /*
-                * intel_unpin_work_fn cannot depend on the connector list
-                * because it may be freed from underneath it, so add
-                * them all to the work struct while we're holding locks.
-                */
-               for_each_connector_in_state(state, conn, old_conn_state, i) {
-                       if (old_conn_state->crtc == crtc) {
-                               work->old_connector_state[j++] = old_conn_state;
-
-                               state->connectors[i] = NULL;
-                               state->connector_states[i] = NULL;
-                       }
-               }
-
-               /* If another crtc has stolen the connector from state,
-                * then for_each_connector_in_state is no longer reliable,
-                * so use drm_for_each_connector here.
-                */
-               drm_for_each_connector(conn, state->dev)
-                       if (conn->state->crtc == crtc)
-                               work->new_connector_state[k++] = conn->state;
-
-               WARN(j != work->num_old_connectors, "j = %i, expected %i\n", j, work->num_old_connectors);
-               WARN(k != work->num_new_connectors, "k = %i, expected %i\n", k, work->num_new_connectors);
-       } else if (!work->new_crtc_state->update_wm_post)
-               work->can_async_unpin = true;
-
-       work->fb_bits = work->new_crtc_state->fb_bits;
-}
-
-static void intel_schedule_unpin(struct drm_crtc *crtc,
-                                struct intel_atomic_state *state,
-                                struct intel_flip_work *work)
-{
-       struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-
-       to_intel_crtc(crtc)->config = work->new_crtc_state;
-
-       queue_work(dev_priv->wq, &work->unpin_work);
-}
+       for_each_pipe(dev_priv, pipe) {
+               struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
+               long lret;
 
-static void intel_schedule_flip(struct drm_crtc *crtc,
-                               struct intel_atomic_state *state,
-                               struct intel_flip_work *work,
-                               bool nonblock)
-{
-       struct intel_crtc_state *crtc_state = work->new_crtc_state;
+               if (!((1 << pipe) & crtc_mask))
+                       continue;
 
-       if (crtc_state->base.planes_changed ||
-           needs_modeset(&crtc_state->base) ||
-           crtc_state->update_pipe) {
-               if (nonblock)
-                       schedule_work(&work->mmio_work);
-               else
-                       intel_mmio_flip_work_func(&work->mmio_work);
-       } else {
-               int ret;
+               lret = wait_event_timeout(dev->vblank[pipe].queue,
+                               last_vblank_count[pipe] !=
+                                       drm_crtc_vblank_count(crtc),
+                               msecs_to_jiffies(50));
 
-               ret = drm_crtc_vblank_get(crtc);
-               I915_STATE_WARN(ret < 0, "enabling vblank failed with %i\n", ret);
+               WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
 
-               work->flip_queued_vblank = intel_crtc_get_vblank_counter(to_intel_crtc(crtc));
-               smp_mb__before_atomic();
-               atomic_set(&work->pending, 1);
+               drm_crtc_vblank_put(crtc);
        }
 }
 
-static void intel_schedule_update(struct drm_crtc *crtc,
-                                 struct intel_atomic_state *state,
-                                 struct intel_flip_work *work,
-                                 bool nonblock)
+static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
 {
-       struct drm_device *dev = crtc->dev;
-       struct intel_crtc_state *pipe_config = work->new_crtc_state;
+       /* fb updated, need to unpin old fb */
+       if (crtc_state->fb_changed)
+               return true;
 
-       if (!pipe_config->base.active && work->can_async_unpin) {
-               INIT_LIST_HEAD(&work->head);
-               intel_schedule_unpin(crtc, state, work);
-               return;
-       }
+       /* wm changes, need vblank before final wm's */
+       if (crtc_state->update_wm_post)
+               return true;
 
-       spin_lock_irq(&dev->event_lock);
-       list_add_tail(&work->head, &to_intel_crtc(crtc)->flip_work);
-       spin_unlock_irq(&dev->event_lock);
+       /*
+        * cxsr is re-enabled after vblank.
+        * This is already handled by crtc_state->update_wm_post,
+        * but added for clarity.
+        */
+       if (crtc_state->disable_cxsr)
+               return true;
 
-       if (!pipe_config->base.active)
-               intel_schedule_unpin(crtc, state, work);
-       else
-               intel_schedule_flip(crtc, state, work, nonblock);
+       return false;
 }
 
-/**
- * intel_atomic_commit - commit validated state object
- * @dev: DRM device
- * @state: the top-level driver state object
- * @nonblock: nonblocking commit
- *
- * This function commits a top-level state object that has been validated
- * with drm_atomic_helper_check().
- *
- * FIXME:  Atomic modeset support for i915 is not yet complete.  At the moment
- * we can only handle plane-related operations and do not yet support
- * nonblocking commit.
- *
- * RETURNS
- * Zero for success or -errno.
- */
-static int intel_atomic_commit(struct drm_device *dev,
-                              struct drm_atomic_state *state,
-                              bool nonblock)
+static void intel_atomic_commit_tail(struct drm_atomic_state *state)
 {
+       struct drm_device *dev = state->dev;
        struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct drm_crtc_state *old_crtc_state;
        struct drm_crtc *crtc;
-       int ret = 0, i;
+       struct intel_crtc_state *intel_cstate;
+       struct drm_plane *plane;
+       struct drm_plane_state *plane_state;
+       bool hw_check = intel_state->modeset;
+       unsigned long put_domains[I915_MAX_PIPES] = {};
+       unsigned crtc_vblank_mask = 0;
+       int i, ret;
 
-       ret = intel_atomic_prepare_commit(dev, state, nonblock);
-       if (ret) {
-               DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
-               return ret;
+       for_each_plane_in_state(state, plane, plane_state, i) {
+               struct intel_plane_state *intel_plane_state =
+                       to_intel_plane_state(plane_state);
+
+               if (!intel_plane_state->wait_req)
+                       continue;
+
+               ret = __i915_wait_request(intel_plane_state->wait_req,
+                                         true, NULL, NULL);
+               /* EIO should be eaten, and we can't get interrupted in the
+                * worker, and blocking commits have waited already. */
+               WARN_ON(ret);
        }
 
-       drm_atomic_helper_swap_state(dev, state);
-       dev_priv->wm.distrust_bios_wm = false;
-       dev_priv->wm.skl_results = intel_state->wm_results;
-       intel_shared_dpll_commit(state);
+       drm_atomic_helper_wait_for_dependencies(state);
 
        if (intel_state->modeset) {
                memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
                       sizeof(intel_state->min_pixclk));
                dev_priv->active_crtcs = intel_state->active_crtcs;
                dev_priv->atomic_cdclk_freq = intel_state->cdclk;
+
+               intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
        }
 
        for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
                struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
 
+               if (needs_modeset(crtc->state) ||
+                   to_intel_crtc_state(crtc->state)->update_pipe) {
+                       hw_check = true;
+
+                       put_domains[to_intel_crtc(crtc)->pipe] =
+                               modeset_get_crtc_power_domains(crtc,
+                                       to_intel_crtc_state(crtc->state));
+               }
+
                if (!needs_modeset(crtc->state))
                        continue;
 
                intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
 
-               intel_state->work[i]->put_power_domains =
-                       modeset_get_crtc_power_domains(crtc,
-                               to_intel_crtc_state(crtc->state));
-
                if (old_crtc_state->active) {
                        intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
                        dev_priv->display.crtc_disable(crtc);
@@ -13157,7 +13731,7 @@ static int intel_atomic_commit(struct drm_device *dev,
 
                if (dev_priv->display.modeset_commit_cdclk &&
                    (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
-                    intel_state->cdclk_pll_vco != dev_priv->skl_vco_freq))
+                    intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
                        dev_priv->display.modeset_commit_cdclk(state);
 
                intel_modeset_verify_disabled(dev);
@@ -13165,41 +13739,84 @@ static int intel_atomic_commit(struct drm_device *dev,
 
        /* Now enable the clocks, plane, pipe, and connectors that we set up. */
        for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
-               struct intel_flip_work *work = intel_state->work[i];
                struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
                bool modeset = needs_modeset(crtc->state);
+               struct intel_crtc_state *pipe_config =
+                       to_intel_crtc_state(crtc->state);
 
                if (modeset && crtc->state->active) {
                        update_scanline_offset(to_intel_crtc(crtc));
                        dev_priv->display.crtc_enable(crtc);
                }
 
+               /* Complete events for now disable pipes here. */
+               if (modeset && !crtc->state->active && crtc->state->event) {
+                       spin_lock_irq(&dev->event_lock);
+                       drm_crtc_send_vblank_event(crtc, crtc->state->event);
+                       spin_unlock_irq(&dev->event_lock);
+
+                       crtc->state->event = NULL;
+               }
+
                if (!modeset)
                        intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
 
-               if (!work) {
-                       if (!list_empty_careful(&intel_crtc->flip_work)) {
-                               spin_lock_irq(&dev->event_lock);
-                               if (!list_empty(&intel_crtc->flip_work))
-                                       work = list_last_entry(&intel_crtc->flip_work,
-                                                              struct intel_flip_work, head);
-
-                               if (work && work->new_crtc_state == to_intel_crtc_state(old_crtc_state)) {
-                                       work->free_new_crtc_state = true;
-                                       state->crtc_states[i] = NULL;
-                                       state->crtcs[i] = NULL;
-                               }
-                               spin_unlock_irq(&dev->event_lock);
-                       }
-                       continue;
-               }
+               if (crtc->state->active &&
+                   drm_atomic_get_existing_plane_state(state, crtc->primary))
+                       intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
+
+               if (crtc->state->active)
+                       drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
+
+               if (pipe_config->base.active && needs_vblank_wait(pipe_config))
+                       crtc_vblank_mask |= 1 << i;
+       }
+
+       /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
+        * already, but still need the state for the delayed optimization. To
+        * fix this:
+        * - wrap the optimization/post_plane_update stuff into a per-crtc work.
+        * - schedule that vblank worker _before_ calling hw_done
+        * - at the start of commit_tail, cancel it _synchrously
+        * - switch over to the vblank wait helper in the core after that since
+        *   we don't need out special handling any more.
+        */
+       if (!state->legacy_cursor_update)
+               intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
+
+       /*
+        * Now that the vblank has passed, we can go ahead and program the
+        * optimal watermarks on platforms that need two-step watermark
+        * programming.
+        *
+        * TODO: Move this (and other cleanup) to an async worker eventually.
+        */
+       for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
+               intel_cstate = to_intel_crtc_state(crtc->state);
+
+               if (dev_priv->display.optimize_watermarks)
+                       dev_priv->display.optimize_watermarks(intel_cstate);
+       }
+
+       for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
+               intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
+
+               if (put_domains[i])
+                       modeset_put_power_domains(dev_priv, put_domains[i]);
 
-               intel_state->work[i] = NULL;
-               intel_prepare_work(crtc, work, state, old_crtc_state);
-               intel_schedule_update(crtc, intel_state, work, nonblock);
+               intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
        }
 
-       /* FIXME: add subpixel order */
+       drm_atomic_helper_commit_hw_done(state);
+
+       if (intel_state->modeset)
+               intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
+
+       mutex_lock(&dev->struct_mutex);
+       drm_atomic_helper_cleanup_planes(dev, state);
+       mutex_unlock(&dev->struct_mutex);
+
+       drm_atomic_helper_commit_cleanup_done(state);
 
        drm_atomic_state_free(state);
 
@@ -13215,6 +13832,86 @@ static int intel_atomic_commit(struct drm_device *dev,
         * can happen also when the device is completely off.
         */
        intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
+}
+
+static void intel_atomic_commit_work(struct work_struct *work)
+{
+       struct drm_atomic_state *state = container_of(work,
+                                                     struct drm_atomic_state,
+                                                     commit_work);
+       intel_atomic_commit_tail(state);
+}
+
+static void intel_atomic_track_fbs(struct drm_atomic_state *state)
+{
+       struct drm_plane_state *old_plane_state;
+       struct drm_plane *plane;
+       struct drm_i915_gem_object *obj, *old_obj;
+       struct intel_plane *intel_plane;
+       int i;
+
+       mutex_lock(&state->dev->struct_mutex);
+       for_each_plane_in_state(state, plane, old_plane_state, i) {
+               obj = intel_fb_obj(plane->state->fb);
+               old_obj = intel_fb_obj(old_plane_state->fb);
+               intel_plane = to_intel_plane(plane);
+
+               i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
+       }
+       mutex_unlock(&state->dev->struct_mutex);
+}
+
+/**
+ * intel_atomic_commit - commit validated state object
+ * @dev: DRM device
+ * @state: the top-level driver state object
+ * @nonblock: nonblocking commit
+ *
+ * This function commits a top-level state object that has been validated
+ * with drm_atomic_helper_check().
+ *
+ * FIXME:  Atomic modeset support for i915 is not yet complete.  At the moment
+ * nonblocking commits are only safe for pure plane updates. Everything else
+ * should work though.
+ *
+ * RETURNS
+ * Zero for success or -errno.
+ */
+static int intel_atomic_commit(struct drm_device *dev,
+                              struct drm_atomic_state *state,
+                              bool nonblock)
+{
+       struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       int ret = 0;
+
+       if (intel_state->modeset && nonblock) {
+               DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
+               return -EINVAL;
+       }
+
+       ret = drm_atomic_helper_setup_commit(state, nonblock);
+       if (ret)
+               return ret;
+
+       INIT_WORK(&state->commit_work, intel_atomic_commit_work);
+
+       ret = intel_atomic_prepare_commit(dev, state, nonblock);
+       if (ret) {
+               DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
+               return ret;
+       }
+
+       drm_atomic_helper_swap_state(state, true);
+       dev_priv->wm.distrust_bios_wm = false;
+       dev_priv->wm.skl_results = intel_state->wm_results;
+       intel_shared_dpll_commit(state);
+       intel_atomic_track_fbs(state);
+
+       if (nonblock)
+               queue_work(system_unbound_wq, &state->commit_work);
+       else
+               intel_atomic_commit_tail(state);
 
        return 0;
 }
@@ -13228,8 +13925,8 @@ void intel_crtc_restore_mode(struct drm_crtc *crtc)
 
        state = drm_atomic_state_alloc(dev);
        if (!state) {
-               DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
-                             crtc->base.id);
+               DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
+                             crtc->base.id, crtc->name);
                return;
        }
 
@@ -13264,38 +13961,11 @@ static const struct drm_crtc_funcs intel_crtc_funcs = {
        .set_config = drm_atomic_helper_set_config,
        .set_property = drm_atomic_helper_crtc_set_property,
        .destroy = intel_crtc_destroy,
-       .page_flip = drm_atomic_helper_page_flip,
+       .page_flip = intel_crtc_page_flip,
        .atomic_duplicate_state = intel_crtc_duplicate_state,
        .atomic_destroy_state = intel_crtc_destroy_state,
 };
 
-static struct fence *intel_get_excl_fence(struct drm_i915_gem_object *obj)
-{
-       struct reservation_object *resv;
-
-
-       if (!obj->base.dma_buf)
-               return NULL;
-
-       resv = obj->base.dma_buf->resv;
-
-       /* For framebuffer backed by dmabuf, wait for fence */
-       while (1) {
-               struct fence *fence_excl, *ret = NULL;
-
-               rcu_read_lock();
-
-               fence_excl = rcu_dereference(resv->fence_excl);
-               if (fence_excl)
-                       ret = fence_get_rcu(fence_excl);
-
-               rcu_read_unlock();
-
-               if (ret == fence_excl)
-                       return ret;
-       }
-}
-
 /**
  * intel_prepare_plane_fb - Prepare fb for usage on plane
  * @plane: drm plane to prepare for
@@ -13316,23 +13986,14 @@ intel_prepare_plane_fb(struct drm_plane *plane,
 {
        struct drm_device *dev = plane->dev;
        struct drm_framebuffer *fb = new_state->fb;
-       struct intel_plane *intel_plane = to_intel_plane(plane);
        struct drm_i915_gem_object *obj = intel_fb_obj(fb);
        struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
-       struct drm_crtc *crtc = new_state->crtc ?: plane->state->crtc;
+       struct reservation_object *resv;
        int ret = 0;
 
        if (!obj && !old_obj)
                return 0;
 
-       if (WARN_ON(!new_state->state) || WARN_ON(!crtc) ||
-           WARN_ON(!to_intel_atomic_state(new_state->state)->work[to_intel_crtc(crtc)->pipe])) {
-               if (WARN_ON(old_obj != obj))
-                       return -EINVAL;
-
-               return 0;
-       }
-
        if (old_obj) {
                struct drm_crtc_state *crtc_state =
                        drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
@@ -13357,9 +14018,23 @@ intel_prepare_plane_fb(struct drm_plane *plane,
                }
        }
 
-       if (!obj) {
-               ret = 0;
-       } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
+       if (!obj)
+               return 0;
+
+       /* For framebuffer backed by dmabuf, wait for fence */
+       resv = i915_gem_object_get_dmabuf_resv(obj);
+       if (resv) {
+               long lret;
+
+               lret = reservation_object_wait_timeout_rcu(resv, false, true,
+                                                          MAX_SCHEDULE_TIMEOUT);
+               if (lret == -ERESTARTSYS)
+                       return lret;
+
+               WARN(lret < 0, "waiting returns %li\n", lret);
+       }
+
+       if (plane->type == DRM_PLANE_TYPE_CURSOR &&
            INTEL_INFO(dev)->cursor_needs_physical) {
                int align = IS_I830(dev) ? 16 * 1024 : 256;
                ret = i915_gem_object_attach_phys(obj, align);
@@ -13370,17 +14045,11 @@ intel_prepare_plane_fb(struct drm_plane *plane,
        }
 
        if (ret == 0) {
-               if (obj) {
-                       struct intel_plane_state *plane_state =
-                               to_intel_plane_state(new_state);
-
-                       i915_gem_request_assign(&plane_state->wait_req,
-                                               obj->last_write_req);
-
-                       plane_state->base.fence = intel_get_excl_fence(obj);
-               }
+               struct intel_plane_state *plane_state =
+                       to_intel_plane_state(new_state);
 
-               i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
+               i915_gem_request_assign(&plane_state->wait_req,
+                                       obj->last_write_req);
        }
 
        return ret;
@@ -13400,7 +14069,6 @@ intel_cleanup_plane_fb(struct drm_plane *plane,
                       const struct drm_plane_state *old_state)
 {
        struct drm_device *dev = plane->dev;
-       struct intel_plane *intel_plane = to_intel_plane(plane);
        struct intel_plane_state *old_intel_state;
        struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
        struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
@@ -13414,30 +14082,18 @@ intel_cleanup_plane_fb(struct drm_plane *plane,
            !INTEL_INFO(dev)->cursor_needs_physical))
                intel_unpin_fb_obj(old_state->fb, old_state->rotation);
 
-       /* prepare_fb aborted? */
-       if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
-           (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
-               i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
-
        i915_gem_request_assign(&old_intel_state->wait_req, NULL);
-
-       fence_put(old_intel_state->base.fence);
-       old_intel_state->base.fence = NULL;
 }
 
 int
 skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
 {
        int max_scale;
-       struct drm_device *dev;
-       struct drm_i915_private *dev_priv;
        int crtc_clock, cdclk;
 
        if (!intel_crtc || !crtc_state->base.enable)
                return DRM_PLANE_HELPER_NO_SCALING;
 
-       dev = intel_crtc->base.dev;
-       dev_priv = dev->dev_private;
        crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
        cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
 
@@ -13477,11 +14133,46 @@ intel_check_primary_plane(struct drm_plane *plane,
 
        return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
                                             &state->dst, &state->clip,
+                                            state->base.rotation,
                                             min_scale, max_scale,
                                             can_position, true,
                                             &state->visible);
 }
 
+static void intel_begin_crtc_commit(struct drm_crtc *crtc,
+                                   struct drm_crtc_state *old_crtc_state)
+{
+       struct drm_device *dev = crtc->dev;
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct intel_crtc_state *old_intel_state =
+               to_intel_crtc_state(old_crtc_state);
+       bool modeset = needs_modeset(crtc->state);
+
+       /* Perform vblank evasion around commit operation */
+       intel_pipe_update_start(intel_crtc);
+
+       if (modeset)
+               return;
+
+       if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
+               intel_color_set_csc(crtc->state);
+               intel_color_load_luts(crtc->state);
+       }
+
+       if (to_intel_crtc_state(crtc->state)->update_pipe)
+               intel_update_pipe_config(intel_crtc, old_intel_state);
+       else if (INTEL_INFO(dev)->gen >= 9)
+               skl_detach_scalers(intel_crtc);
+}
+
+static void intel_finish_crtc_commit(struct drm_crtc *crtc,
+                                    struct drm_crtc_state *old_crtc_state)
+{
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+
+       intel_pipe_update_end(intel_crtc, NULL);
+}
+
 /**
  * intel_plane_destroy - destroy a plane
  * @plane: plane to destroy
@@ -13491,9 +14182,11 @@ intel_check_primary_plane(struct drm_plane *plane,
  */
 void intel_plane_destroy(struct drm_plane *plane)
 {
-       struct intel_plane *intel_plane = to_intel_plane(plane);
+       if (!plane)
+               return;
+
        drm_plane_cleanup(plane);
-       kfree(intel_plane);
+       kfree(to_intel_plane(plane));
 }
 
 const struct drm_plane_funcs intel_plane_funcs = {
@@ -13565,10 +14258,24 @@ static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
                primary->disable_plane = i9xx_disable_primary_plane;
        }
 
-       ret = drm_universal_plane_init(dev, &primary->base, 0,
-                                      &intel_plane_funcs,
-                                      intel_primary_formats, num_formats,
-                                      DRM_PLANE_TYPE_PRIMARY, NULL);
+       if (INTEL_INFO(dev)->gen >= 9)
+               ret = drm_universal_plane_init(dev, &primary->base, 0,
+                                              &intel_plane_funcs,
+                                              intel_primary_formats, num_formats,
+                                              DRM_PLANE_TYPE_PRIMARY,
+                                              "plane 1%c", pipe_name(pipe));
+       else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
+               ret = drm_universal_plane_init(dev, &primary->base, 0,
+                                              &intel_plane_funcs,
+                                              intel_primary_formats, num_formats,
+                                              DRM_PLANE_TYPE_PRIMARY,
+                                              "primary %c", pipe_name(pipe));
+       else
+               ret = drm_universal_plane_init(dev, &primary->base, 0,
+                                              &intel_plane_funcs,
+                                              intel_primary_formats, num_formats,
+                                              DRM_PLANE_TYPE_PRIMARY,
+                                              "plane %c", plane_name(primary->plane));
        if (ret)
                goto fail;
 
@@ -13618,6 +14325,7 @@ intel_check_cursor_plane(struct drm_plane *plane,
 
        ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
                                            &state->dst, &state->clip,
+                                           state->base.rotation,
                                            DRM_PLANE_HELPER_NO_SCALING,
                                            DRM_PLANE_HELPER_NO_SCALING,
                                            true, true, &state->visible);
@@ -13726,7 +14434,8 @@ static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
                                       &intel_plane_funcs,
                                       intel_cursor_formats,
                                       ARRAY_SIZE(intel_cursor_formats),
-                                      DRM_PLANE_TYPE_CURSOR, NULL);
+                                      DRM_PLANE_TYPE_CURSOR,
+                                      "cursor %c", pipe_name(pipe));
        if (ret)
                goto fail;
 
@@ -13774,7 +14483,7 @@ static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_cr
 
 static void intel_crtc_init(struct drm_device *dev, int pipe)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_crtc *intel_crtc;
        struct intel_crtc_state *crtc_state = NULL;
        struct drm_plane *primary = NULL;
@@ -13792,8 +14501,6 @@ static void intel_crtc_init(struct drm_device *dev, int pipe)
        intel_crtc->base.state = &crtc_state->base;
        crtc_state->base.crtc = &intel_crtc->base;
 
-       INIT_LIST_HEAD(&intel_crtc->flip_work);
-
        /* initialize shared scalers */
        if (INTEL_INFO(dev)->gen >= 9) {
                if (pipe == PIPE_C)
@@ -13813,7 +14520,8 @@ static void intel_crtc_init(struct drm_device *dev, int pipe)
                goto fail;
 
        ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
-                                       cursor, &intel_crtc_funcs, NULL);
+                                       cursor, &intel_crtc_funcs,
+                                       "pipe %c", pipe_name(pipe));
        if (ret)
                goto fail;
 
@@ -13847,10 +14555,8 @@ static void intel_crtc_init(struct drm_device *dev, int pipe)
        return;
 
 fail:
-       if (primary)
-               drm_plane_cleanup(primary);
-       if (cursor)
-               drm_plane_cleanup(cursor);
+       intel_plane_destroy(primary);
+       intel_plane_destroy(cursor);
        kfree(crtc_state);
        kfree(intel_crtc);
 }
@@ -13876,11 +14582,8 @@ int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
        struct intel_crtc *crtc;
 
        drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
-
-       if (!drmmode_crtc) {
-               DRM_ERROR("no such CRTC id\n");
+       if (!drmmode_crtc)
                return -ENOENT;
-       }
 
        crtc = to_intel_crtc(drmmode_crtc);
        pipe_from_crtc_id->pipe = crtc->pipe;
@@ -13907,7 +14610,7 @@ static int intel_encoder_clones(struct intel_encoder *encoder)
 
 static bool has_edp_a(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        if (!IS_MOBILE(dev))
                return false;
@@ -13923,7 +14626,7 @@ static bool has_edp_a(struct drm_device *dev)
 
 static bool intel_crt_present(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        if (INTEL_INFO(dev)->gen >= 9)
                return false;
@@ -13949,10 +14652,15 @@ static bool intel_crt_present(struct drm_device *dev)
 
 static void intel_setup_outputs(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_encoder *encoder;
        bool dpd_is_edp = false;
 
+       /*
+        * intel_edp_init_connector() depends on this completing first, to
+        * prevent the registeration of both eDP and LVDS and the incorrect
+        * sharing of the PPS.
+        */
        intel_lvds_init(dev);
 
        if (intel_crt_present(dev))
@@ -14029,6 +14737,8 @@ static void intel_setup_outputs(struct drm_device *dev)
                if (I915_READ(PCH_DP_D) & DP_DETECTED)
                        intel_dp_init(dev, PCH_DP_D, PORT_D);
        } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
+               bool has_edp, has_port;
+
                /*
                 * The DP_DETECTED bit is the latched state of the DDC
                 * SDA pin at boot. However since eDP doesn't require DDC
@@ -14037,27 +14747,37 @@ static void intel_setup_outputs(struct drm_device *dev)
                 * Thus we can't rely on the DP_DETECTED bit alone to detect
                 * eDP ports. Consult the VBT as well as DP_DETECTED to
                 * detect eDP ports.
+                *
+                * Sadly the straps seem to be missing sometimes even for HDMI
+                * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
+                * and VBT for the presence of the port. Additionally we can't
+                * trust the port type the VBT declares as we've seen at least
+                * HDMI ports that the VBT claim are DP or eDP.
                 */
-               if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
-                   !intel_dp_is_edp(dev, PORT_B))
+               has_edp = intel_dp_is_edp(dev, PORT_B);
+               has_port = intel_bios_is_port_present(dev_priv, PORT_B);
+               if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
+                       has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
+               if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
                        intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
-               if (I915_READ(VLV_DP_B) & DP_DETECTED ||
-                   intel_dp_is_edp(dev, PORT_B))
-                       intel_dp_init(dev, VLV_DP_B, PORT_B);
 
-               if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
-                   !intel_dp_is_edp(dev, PORT_C))
+               has_edp = intel_dp_is_edp(dev, PORT_C);
+               has_port = intel_bios_is_port_present(dev_priv, PORT_C);
+               if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
+                       has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
+               if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
                        intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
-               if (I915_READ(VLV_DP_C) & DP_DETECTED ||
-                   intel_dp_is_edp(dev, PORT_C))
-                       intel_dp_init(dev, VLV_DP_C, PORT_C);
 
                if (IS_CHERRYVIEW(dev)) {
-                       /* eDP not supported on port D, so don't check VBT */
-                       if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
-                               intel_hdmi_init(dev, CHV_HDMID, PORT_D);
-                       if (I915_READ(CHV_DP_D) & DP_DETECTED)
+                       /*
+                        * eDP not supported on port D,
+                        * so no need to worry about it
+                        */
+                       has_port = intel_bios_is_port_present(dev_priv, PORT_D);
+                       if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
                                intel_dp_init(dev, CHV_DP_D, PORT_D);
+                       if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
+                               intel_hdmi_init(dev, CHV_HDMID, PORT_D);
                }
 
                intel_dsi_init(dev);
@@ -14360,8 +15080,7 @@ intel_user_framebuffer_create(struct drm_device *dev,
        struct drm_i915_gem_object *obj;
        struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
 
-       obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
-                                               mode_cmd.handles[0]));
+       obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
        if (&obj->base == NULL)
                return ERR_PTR(-ENOENT);
 
@@ -14540,15 +15259,43 @@ void intel_init_display_hooks(struct drm_i915_private *dev_priv)
                        valleyview_modeset_calc_cdclk;
        } else if (IS_BROXTON(dev_priv)) {
                dev_priv->display.modeset_commit_cdclk =
-                       broxton_modeset_commit_cdclk;
+                       bxt_modeset_commit_cdclk;
                dev_priv->display.modeset_calc_cdclk =
-                       broxton_modeset_calc_cdclk;
+                       bxt_modeset_calc_cdclk;
        } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
                dev_priv->display.modeset_commit_cdclk =
                        skl_modeset_commit_cdclk;
                dev_priv->display.modeset_calc_cdclk =
                        skl_modeset_calc_cdclk;
        }
+
+       switch (INTEL_INFO(dev_priv)->gen) {
+       case 2:
+               dev_priv->display.queue_flip = intel_gen2_queue_flip;
+               break;
+
+       case 3:
+               dev_priv->display.queue_flip = intel_gen3_queue_flip;
+               break;
+
+       case 4:
+       case 5:
+               dev_priv->display.queue_flip = intel_gen4_queue_flip;
+               break;
+
+       case 6:
+               dev_priv->display.queue_flip = intel_gen6_queue_flip;
+               break;
+       case 7:
+       case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
+               dev_priv->display.queue_flip = intel_gen7_queue_flip;
+               break;
+       case 9:
+               /* Drop through - unsupported since execlist only. */
+       default:
+               /* Default just returns -ENODEV to indicate unsupported */
+               dev_priv->display.queue_flip = intel_default_queue_flip;
+       }
 }
 
 /*
@@ -14558,7 +15305,7 @@ void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  */
 static void quirk_pipea_force(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        dev_priv->quirks |= QUIRK_PIPEA_FORCE;
        DRM_INFO("applying pipe a force quirk\n");
@@ -14566,7 +15313,7 @@ static void quirk_pipea_force(struct drm_device *dev)
 
 static void quirk_pipeb_force(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        dev_priv->quirks |= QUIRK_PIPEB_FORCE;
        DRM_INFO("applying pipe b force quirk\n");
@@ -14577,7 +15324,7 @@ static void quirk_pipeb_force(struct drm_device *dev)
  */
 static void quirk_ssc_force_disable(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
        DRM_INFO("applying lvds SSC disable quirk\n");
 }
@@ -14588,7 +15335,7 @@ static void quirk_ssc_force_disable(struct drm_device *dev)
  */
 static void quirk_invert_brightness(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
        DRM_INFO("applying inverted panel brightness quirk\n");
 }
@@ -14596,7 +15343,7 @@ static void quirk_invert_brightness(struct drm_device *dev)
 /* Some VBT's incorrectly indicate no backlight is present */
 static void quirk_backlight_present(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
        DRM_INFO("applying backlight present quirk\n");
 }
@@ -14722,7 +15469,7 @@ static void intel_init_quirks(struct drm_device *dev)
 /* Disable the VGA plane that we never use */
 static void i915_disable_vga(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u8 sr1;
        i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
 
@@ -14740,7 +15487,7 @@ static void i915_disable_vga(struct drm_device *dev)
 
 void intel_modeset_init_hw(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        intel_update_cdclk(dev);
 
@@ -14988,7 +15735,7 @@ static bool
 intel_check_plane_mapping(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        u32 val;
 
        if (INTEL_INFO(dev)->num_pipes == 1)
@@ -15028,7 +15775,7 @@ static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
 static void intel_sanitize_crtc(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
 
        /* Clear any frame start delays used for debugging left by the BIOS */
@@ -15061,8 +15808,8 @@ static void intel_sanitize_crtc(struct intel_crtc *crtc)
        if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
                bool plane;
 
-               DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
-                             crtc->base.base.id);
+               DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
+                             crtc->base.base.id, crtc->base.name);
 
                /* Pipe has the wrong plane attached and the plane is active.
                 * Temporarily change the plane mapping and disable everything
@@ -15153,7 +15900,7 @@ static void intel_sanitize_encoder(struct intel_encoder *encoder)
 
 void i915_redisable_vga_power_on(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
 
        if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
@@ -15164,7 +15911,7 @@ void i915_redisable_vga_power_on(struct drm_device *dev)
 
 void i915_redisable_vga(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        /* This function can be called both from intel_modeset_setup_hw_state or
         * at a very early point in our resume sequence, where the power well
@@ -15204,7 +15951,7 @@ static void readout_plane_state(struct intel_crtc *crtc)
 
 static void intel_modeset_readout_hw_state(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe;
        struct intel_crtc *crtc;
        struct intel_encoder *encoder;
@@ -15217,7 +15964,7 @@ static void intel_modeset_readout_hw_state(struct drm_device *dev)
                struct intel_crtc_state *crtc_state = crtc->config;
                int pixclk = 0;
 
-               __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
+               __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
                memset(crtc_state, 0, sizeof(*crtc_state));
                crtc_state->base.crtc = &crtc->base;
 
@@ -15246,8 +15993,8 @@ static void intel_modeset_readout_hw_state(struct drm_device *dev)
 
                readout_plane_state(crtc);
 
-               DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
-                             crtc->base.base.id,
+               DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
+                             crtc->base.base.id, crtc->base.name,
                              crtc->active ? "enabled" : "disabled");
        }
 
@@ -15273,6 +16020,7 @@ static void intel_modeset_readout_hw_state(struct drm_device *dev)
                if (encoder->get_hw_state(encoder, &pipe)) {
                        crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
                        encoder->base.crtc = &crtc->base;
+                       crtc->config->output_types |= 1 << encoder->type;
                        encoder->get_config(encoder, crtc->config);
                } else {
                        encoder->base.crtc = NULL;
@@ -15357,7 +16105,7 @@ static void intel_modeset_readout_hw_state(struct drm_device *dev)
 static void
 intel_modeset_setup_hw_state(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        enum pipe pipe;
        struct intel_crtc *crtc;
        struct intel_encoder *encoder;
@@ -15507,33 +16255,43 @@ void intel_modeset_gem_init(struct drm_device *dev)
                        DRM_ERROR("failed to pin boot fb on pipe %d\n",
                                  to_intel_crtc(c)->pipe);
                        drm_framebuffer_unreference(c->primary->fb);
-                       drm_framebuffer_unreference(c->primary->state->fb);
-                       c->primary->fb = c->primary->state->fb = NULL;
+                       c->primary->fb = NULL;
                        c->primary->crtc = c->primary->state->crtc = NULL;
+                       update_state_fb(c->primary);
                        c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
                }
        }
+}
+
+int intel_connector_register(struct drm_connector *connector)
+{
+       struct intel_connector *intel_connector = to_intel_connector(connector);
+       int ret;
+
+       ret = intel_backlight_device_register(intel_connector);
+       if (ret)
+               goto err;
+
+       return 0;
 
-       intel_backlight_register(dev);
+err:
+       return ret;
 }
 
-void intel_connector_unregister(struct intel_connector *intel_connector)
+void intel_connector_unregister(struct drm_connector *connector)
 {
-       struct drm_connector *connector = &intel_connector->base;
+       struct intel_connector *intel_connector = to_intel_connector(connector);
 
+       intel_backlight_device_unregister(intel_connector);
        intel_panel_destroy_backlight(connector);
-       drm_connector_unregister(connector);
 }
 
 void intel_modeset_cleanup(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct intel_connector *connector;
+       struct drm_i915_private *dev_priv = to_i915(dev);
 
        intel_disable_gt_powersave(dev_priv);
 
-       intel_backlight_unregister(dev);
-
        /*
         * Interrupts and polling as the first thing to avoid creating havoc.
         * Too much stuff here (turning of connectors, ...) would
@@ -15554,10 +16312,6 @@ void intel_modeset_cleanup(struct drm_device *dev)
        /* flush any delayed tasks or pending work */
        flush_scheduled_work();
 
-       /* destroy the backlight and sysfs files before encoders/connectors */
-       for_each_intel_connector(dev, connector)
-               connector->unregister(connector);
-
        drm_mode_config_cleanup(dev);
 
        intel_cleanup_overlay(dev_priv);
@@ -15567,14 +16321,6 @@ void intel_modeset_cleanup(struct drm_device *dev)
        intel_teardown_gmbus(dev);
 }
 
-/*
- * Return which encoder is currently attached for connector.
- */
-struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
-{
-       return &intel_attached_encoder(connector)->base;
-}
-
 void intel_connector_attach_encoder(struct intel_connector *connector,
                                    struct intel_encoder *encoder)
 {
@@ -15588,7 +16334,7 @@ void intel_connector_attach_encoder(struct intel_connector *connector,
  */
 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
        u16 gmch_ctrl;
 
@@ -15744,7 +16490,7 @@ intel_display_print_error_state(struct drm_i915_error_state_buf *m,
                                struct drm_device *dev,
                                struct intel_display_error_state *error)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        int i;
 
        if (!error)
This page took 0.106569 seconds and 5 git commands to generate.