Revert "drm/i915: Make intel_display_suspend atomic, v2."
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
index d0f3cbc87474c1ff9aff408fd692426a7e70c5d4..cc7fad9905b9a9a15fb485816f7f3cf6d84b37b1 100644 (file)
 #include <drm/drm_rect.h>
 #include <linux/dma_remapping.h>
 
-/* Primary plane formats supported by all gen */
-#define COMMON_PRIMARY_FORMATS \
-       DRM_FORMAT_C8, \
-       DRM_FORMAT_RGB565, \
-       DRM_FORMAT_XRGB8888, \
-       DRM_FORMAT_ARGB8888
-
 /* Primary plane formats for gen <= 3 */
-static const uint32_t intel_primary_formats_gen2[] = {
-       COMMON_PRIMARY_FORMATS,
+static const uint32_t i8xx_primary_formats[] = {
+       DRM_FORMAT_C8,
+       DRM_FORMAT_RGB565,
        DRM_FORMAT_XRGB1555,
-       DRM_FORMAT_ARGB1555,
+       DRM_FORMAT_XRGB8888,
 };
 
 /* Primary plane formats for gen >= 4 */
-static const uint32_t intel_primary_formats_gen4[] = {
-       COMMON_PRIMARY_FORMATS, \
+static const uint32_t i965_primary_formats[] = {
+       DRM_FORMAT_C8,
+       DRM_FORMAT_RGB565,
+       DRM_FORMAT_XRGB8888,
+       DRM_FORMAT_XBGR8888,
+       DRM_FORMAT_XRGB2101010,
+       DRM_FORMAT_XBGR2101010,
+};
+
+static const uint32_t skl_primary_formats[] = {
+       DRM_FORMAT_C8,
+       DRM_FORMAT_RGB565,
+       DRM_FORMAT_XRGB8888,
        DRM_FORMAT_XBGR8888,
+       DRM_FORMAT_ARGB8888,
        DRM_FORMAT_ABGR8888,
        DRM_FORMAT_XRGB2101010,
-       DRM_FORMAT_ARGB2101010,
        DRM_FORMAT_XBGR2101010,
-       DRM_FORMAT_ABGR2101010,
 };
 
 /* Cursor formats */
@@ -82,9 +86,7 @@ static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
                                   struct intel_crtc_state *pipe_config);
 
-static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
-                         int x, int y, struct drm_framebuffer *old_fb,
-                         struct drm_atomic_state *state);
+static int intel_set_mode(struct drm_atomic_state *state);
 static int intel_framebuffer_init(struct drm_device *dev,
                                  struct intel_framebuffer *ifb,
                                  struct drm_mode_fb_cmd2 *mode_cmd,
@@ -103,6 +105,12 @@ 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 *crtc);
 static void intel_finish_crtc_commit(struct drm_crtc *crtc);
+static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
+       struct intel_crtc_state *crtc_state);
+static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
+                          int num_connectors);
+static void intel_crtc_enable_planes(struct drm_crtc *crtc);
+static void intel_crtc_disable_planes(struct drm_crtc *crtc);
 
 static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
 {
@@ -400,6 +408,18 @@ static const intel_limit_t intel_limits_chv = {
        .p2 = { .p2_slow = 1, .p2_fast = 14 },
 };
 
+static const intel_limit_t intel_limits_bxt = {
+       /* FIXME: find real dot limits */
+       .dot = { .min = 0, .max = INT_MAX },
+       .vco = { .min = 4800000, .max = 6480000 },
+       .n = { .min = 1, .max = 1 },
+       .m1 = { .min = 2, .max = 2 },
+       /* FIXME: find real m2 limits */
+       .m2 = { .min = 2 << 22, .max = 255 << 22 },
+       .p1 = { .min = 2, .max = 4 },
+       .p2 = { .p2_slow = 1, .p2_fast = 20 },
+};
+
 static void vlv_clock(int refclk, intel_clock_t *clock)
 {
        clock->m = clock->m1 * clock->m2;
@@ -410,6 +430,12 @@ static void vlv_clock(int refclk, intel_clock_t *clock)
        clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
 }
 
+static bool
+needs_modeset(struct drm_crtc_state *state)
+{
+       return state->mode_changed || state->active_changed;
+}
+
 /**
  * Returns whether any output on the specified pipe is of the specified type
  */
@@ -435,15 +461,12 @@ 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 (i = 0; i < state->num_connector; i++) {
-               if (!state->connectors[i])
-                       continue;
-
-               connector_state = state->connector_states[i];
+       for_each_connector_in_state(state, connector, connector_state, i) {
                if (connector_state->crtc != crtc_state->base.crtc)
                        continue;
 
@@ -511,7 +534,9 @@ intel_limit(struct intel_crtc_state *crtc_state, int refclk)
        struct drm_device *dev = crtc_state->base.crtc->dev;
        const intel_limit_t *limit;
 
-       if (HAS_PCH_SPLIT(dev))
+       if (IS_BROXTON(dev))
+               limit = &intel_limits_bxt;
+       else if (HAS_PCH_SPLIT(dev))
                limit = intel_ironlake_limit(crtc_state, refclk);
        else if (IS_G4X(dev)) {
                limit = intel_g4x_limit(crtc_state);
@@ -596,11 +621,11 @@ static bool intel_PLL_is_valid(struct drm_device *dev,
        if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
                INTELPllInvalid("m1 out of range\n");
 
-       if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
+       if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
                if (clock->m1 <= clock->m2)
                        INTELPllInvalid("m1 <= m2\n");
 
-       if (!IS_VALLEYVIEW(dev)) {
+       if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
                if (clock->p < limit->p.min || limit->p.max < clock->p)
                        INTELPllInvalid("p out of range\n");
                if (clock->m < limit->m.min || limit->m.max < clock->m)
@@ -953,6 +978,15 @@ chv_find_best_dpll(const intel_limit_t *limit,
        return found;
 }
 
+bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
+                       intel_clock_t *best_clock)
+{
+       int refclk = i9xx_get_refclk(crtc_state, 0);
+
+       return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
+                                 target_clock, refclk, NULL, best_clock);
+}
+
 bool intel_crtc_active(struct drm_crtc *crtc)
 {
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
@@ -1111,9 +1145,9 @@ static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
        u32 val;
        bool cur_state;
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
        val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 
        cur_state = val & DSI_PLL_VCO_EN;
        I915_STATE_WARN(cur_state != state,
@@ -1632,13 +1666,15 @@ static void chv_enable_pll(struct intel_crtc *crtc,
 
        BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
 
        /* Enable back the 10bit clock to display controller */
        tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
        tmp |= DPIO_DCLKP_EN;
        vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
 
+       mutex_unlock(&dev_priv->sb_lock);
+
        /*
         * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
         */
@@ -1654,8 +1690,6 @@ static void chv_enable_pll(struct intel_crtc *crtc,
        /* not sure when this should be written */
        I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
        POSTING_READ(DPLL_MD(pipe));
-
-       mutex_unlock(&dev_priv->dpio_lock);
 }
 
 static int intel_num_dvo_pipes(struct drm_device *dev)
@@ -1664,7 +1698,7 @@ static int intel_num_dvo_pipes(struct drm_device *dev)
        int count = 0;
 
        for_each_intel_crtc(dev, crtc)
-               count += crtc->active &&
+               count += crtc->base.state->active &&
                        intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
 
        return count;
@@ -1745,7 +1779,7 @@ static void i9xx_disable_pll(struct intel_crtc *crtc)
        /* Disable DVO 2x clock on both PLLs if necessary */
        if (IS_I830(dev) &&
            intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
-           intel_num_dvo_pipes(dev) == 1) {
+           !intel_num_dvo_pipes(dev)) {
                I915_WRITE(DPLL(PIPE_B),
                           I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
                I915_WRITE(DPLL(PIPE_A),
@@ -1797,7 +1831,7 @@ static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
        I915_WRITE(DPLL(pipe), val);
        POSTING_READ(DPLL(pipe));
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
 
        /* Disable 10bit clock to display controller */
        val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
@@ -1815,11 +1849,12 @@ static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
                vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
        }
 
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 }
 
 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
-               struct intel_digital_port *dport)
+                        struct intel_digital_port *dport,
+                        unsigned int expected_mask)
 {
        u32 port_mask;
        int dpll_reg;
@@ -1832,6 +1867,7 @@ void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
        case PORT_C:
                port_mask = DPLL_PORTC_READY_MASK;
                dpll_reg = DPLL(0);
+               expected_mask <<= 4;
                break;
        case PORT_D:
                port_mask = DPLL_PORTD_READY_MASK;
@@ -1841,9 +1877,9 @@ void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
                BUG();
        }
 
-       if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
-               WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
-                    port_name(dport->port), I915_READ(dpll_reg));
+       if (wait_for((I915_READ(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);
 }
 
 static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
@@ -2104,7 +2140,7 @@ static void intel_enable_pipe(struct intel_crtc *crtc)
         * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
         * need the check.
         */
-       if (!HAS_PCH_SPLIT(dev_priv->dev))
+       if (HAS_GMCH_DISPLAY(dev_priv->dev))
                if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
                        assert_dsi_pll_enabled(dev_priv);
                else
@@ -2179,20 +2215,6 @@ static void intel_disable_pipe(struct intel_crtc *crtc)
                intel_wait_for_pipe_off(crtc);
 }
 
-/*
- * Plane regs are double buffered, going from enabled->disabled needs a
- * trigger in order to latch.  The display address reg provides this.
- */
-void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
-                              enum plane plane)
-{
-       struct drm_device *dev = dev_priv->dev;
-       u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
-
-       I915_WRITE(reg, I915_READ(reg));
-       POSTING_READ(reg);
-}
-
 /**
  * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  * @plane:  plane to be enabled
@@ -2209,45 +2231,7 @@ static void intel_enable_primary_hw_plane(struct drm_plane *plane,
 
        /* If the pipe isn't enabled, we can't pump pixels and may hang */
        assert_pipe_enabled(dev_priv, intel_crtc->pipe);
-
-       if (intel_crtc->primary_enabled)
-               return;
-
-       intel_crtc->primary_enabled = true;
-
-       dev_priv->display.update_primary_plane(crtc, plane->fb,
-                                              crtc->x, crtc->y);
-
-       /*
-        * 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 (IS_BROADWELL(dev))
-               intel_wait_for_vblank(dev, intel_crtc->pipe);
-}
-
-/**
- * intel_disable_primary_hw_plane - disable the primary hardware plane
- * @plane: plane to be disabled
- * @crtc: crtc for the plane
- *
- * Disable @plane on @crtc, making sure that the pipe is running first.
- */
-static void intel_disable_primary_hw_plane(struct drm_plane *plane,
-                                          struct drm_crtc *crtc)
-{
-       struct drm_device *dev = plane->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
-       if (WARN_ON(!intel_crtc->active))
-               return;
-
-       if (!intel_crtc->primary_enabled)
-               return;
-
-       intel_crtc->primary_enabled = false;
+       to_intel_plane_state(plane->state)->visible = true;
 
        dev_priv->display.update_primary_plane(crtc, plane->fb,
                                               crtc->x, crtc->y);
@@ -2338,13 +2322,6 @@ intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
        info->pitch = fb->pitches[0];
        info->fb_modifier = fb->modifier[0];
 
-       if (!(info->fb_modifier == I915_FORMAT_MOD_Y_TILED ||
-             info->fb_modifier == I915_FORMAT_MOD_Yf_TILED)) {
-               DRM_DEBUG_KMS(
-                             "Y or Yf tiling is needed for 90/270 rotation!\n");
-               return -EINVAL;
-       }
-
        return 0;
 }
 
@@ -2654,9 +2631,9 @@ valid_fb:
                dev_priv->preserve_bios_swizzle = true;
 
        primary->fb = fb;
-       primary->state->crtc = &intel_crtc->base;
-       primary->crtc = &intel_crtc->base;
+       primary->crtc = primary->state->crtc = &intel_crtc->base;
        update_state_fb(primary);
+       intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
        obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
 }
 
@@ -2667,6 +2644,8 @@ static void i9xx_update_primary_plane(struct drm_crtc *crtc,
        struct drm_device *dev = crtc->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct drm_plane *primary = crtc->primary;
+       bool visible = to_intel_plane_state(primary->state)->visible;
        struct drm_i915_gem_object *obj;
        int plane = intel_crtc->plane;
        unsigned long linear_offset;
@@ -2674,7 +2653,7 @@ static void i9xx_update_primary_plane(struct drm_crtc *crtc,
        u32 reg = DSPCNTR(plane);
        int pixel_size;
 
-       if (!intel_crtc->primary_enabled) {
+       if (!visible || !fb) {
                I915_WRITE(reg, 0);
                if (INTEL_INFO(dev)->gen >= 4)
                        I915_WRITE(DSPSURF(plane), 0);
@@ -2718,26 +2697,21 @@ static void i9xx_update_primary_plane(struct drm_crtc *crtc,
                dspcntr |= DISPPLANE_8BPP;
                break;
        case DRM_FORMAT_XRGB1555:
-       case DRM_FORMAT_ARGB1555:
                dspcntr |= DISPPLANE_BGRX555;
                break;
        case DRM_FORMAT_RGB565:
                dspcntr |= DISPPLANE_BGRX565;
                break;
        case DRM_FORMAT_XRGB8888:
-       case DRM_FORMAT_ARGB8888:
                dspcntr |= DISPPLANE_BGRX888;
                break;
        case DRM_FORMAT_XBGR8888:
-       case DRM_FORMAT_ABGR8888:
                dspcntr |= DISPPLANE_RGBX888;
                break;
        case DRM_FORMAT_XRGB2101010:
-       case DRM_FORMAT_ARGB2101010:
                dspcntr |= DISPPLANE_BGRX101010;
                break;
        case DRM_FORMAT_XBGR2101010:
-       case DRM_FORMAT_ABGR2101010:
                dspcntr |= DISPPLANE_RGBX101010;
                break;
        default:
@@ -2796,6 +2770,8 @@ static void ironlake_update_primary_plane(struct drm_crtc *crtc,
        struct drm_device *dev = crtc->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct drm_plane *primary = crtc->primary;
+       bool visible = to_intel_plane_state(primary->state)->visible;
        struct drm_i915_gem_object *obj;
        int plane = intel_crtc->plane;
        unsigned long linear_offset;
@@ -2803,7 +2779,7 @@ static void ironlake_update_primary_plane(struct drm_crtc *crtc,
        u32 reg = DSPCNTR(plane);
        int pixel_size;
 
-       if (!intel_crtc->primary_enabled) {
+       if (!visible || !fb) {
                I915_WRITE(reg, 0);
                I915_WRITE(DSPSURF(plane), 0);
                POSTING_READ(reg);
@@ -2831,19 +2807,15 @@ static void ironlake_update_primary_plane(struct drm_crtc *crtc,
                dspcntr |= DISPPLANE_BGRX565;
                break;
        case DRM_FORMAT_XRGB8888:
-       case DRM_FORMAT_ARGB8888:
                dspcntr |= DISPPLANE_BGRX888;
                break;
        case DRM_FORMAT_XBGR8888:
-       case DRM_FORMAT_ABGR8888:
                dspcntr |= DISPPLANE_RGBX888;
                break;
        case DRM_FORMAT_XRGB2101010:
-       case DRM_FORMAT_ARGB2101010:
                dspcntr |= DISPPLANE_BGRX101010;
                break;
        case DRM_FORMAT_XBGR2101010:
-       case DRM_FORMAT_ABGR2101010:
                dspcntr |= DISPPLANE_RGBX101010;
                break;
        default:
@@ -2936,92 +2908,221 @@ unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
        return i915_gem_obj_ggtt_offset_view(obj, view);
 }
 
-static void skylake_update_primary_plane(struct drm_crtc *crtc,
-                                        struct drm_framebuffer *fb,
-                                        int x, int y)
+/*
+ * This function detaches (aka. unbinds) unused scalers in hardware
+ */
+void skl_detach_scalers(struct intel_crtc *intel_crtc)
 {
-       struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       struct drm_i915_gem_object *obj;
-       int pipe = intel_crtc->pipe;
-       u32 plane_ctl, stride_div;
-       unsigned long surf_addr;
+       struct drm_device *dev;
+       struct drm_i915_private *dev_priv;
+       struct intel_crtc_scaler_state *scaler_state;
+       int i;
 
-       if (!intel_crtc->primary_enabled) {
-               I915_WRITE(PLANE_CTL(pipe, 0), 0);
-               I915_WRITE(PLANE_SURF(pipe, 0), 0);
-               POSTING_READ(PLANE_CTL(pipe, 0));
+       if (!intel_crtc || !intel_crtc->config)
                return;
-       }
 
-       plane_ctl = PLANE_CTL_ENABLE |
-                   PLANE_CTL_PIPE_GAMMA_ENABLE |
-                   PLANE_CTL_PIPE_CSC_ENABLE;
+       dev = intel_crtc->base.dev;
+       dev_priv = dev->dev_private;
+       scaler_state = &intel_crtc->config->scaler_state;
 
-       switch (fb->pixel_format) {
+       /* loop through and disable scalers that aren't in use */
+       for (i = 0; i < intel_crtc->num_scalers; i++) {
+               if (!scaler_state->scalers[i].in_use) {
+                       I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
+                       I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
+                       I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
+                       DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
+                               intel_crtc->base.base.id, intel_crtc->pipe, i);
+               }
+       }
+}
+
+u32 skl_plane_ctl_format(uint32_t pixel_format)
+{
+       switch (pixel_format) {
+       case DRM_FORMAT_C8:
+               return PLANE_CTL_FORMAT_INDEXED;
        case DRM_FORMAT_RGB565:
-               plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
-               break;
-       case DRM_FORMAT_XRGB8888:
-               plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
-               break;
-       case DRM_FORMAT_ARGB8888:
-               plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
-               plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
-               break;
+               return PLANE_CTL_FORMAT_RGB_565;
        case DRM_FORMAT_XBGR8888:
-               plane_ctl |= PLANE_CTL_ORDER_RGBX;
-               plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
-               break;
+               return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
+       case DRM_FORMAT_XRGB8888:
+               return PLANE_CTL_FORMAT_XRGB_8888;
+       /*
+        * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
+        * to be already pre-multiplied. We need to add a knob (or a different
+        * DRM_FORMAT) for user-space to configure that.
+        */
        case DRM_FORMAT_ABGR8888:
-               plane_ctl |= PLANE_CTL_ORDER_RGBX;
-               plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
-               plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
-               break;
+               return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
+                       PLANE_CTL_ALPHA_SW_PREMULTIPLY;
+       case DRM_FORMAT_ARGB8888:
+               return PLANE_CTL_FORMAT_XRGB_8888 |
+                       PLANE_CTL_ALPHA_SW_PREMULTIPLY;
        case DRM_FORMAT_XRGB2101010:
-               plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
-               break;
+               return PLANE_CTL_FORMAT_XRGB_2101010;
        case DRM_FORMAT_XBGR2101010:
-               plane_ctl |= PLANE_CTL_ORDER_RGBX;
-               plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
-               break;
+               return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
+       case DRM_FORMAT_YUYV:
+               return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
+       case DRM_FORMAT_YVYU:
+               return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
+       case DRM_FORMAT_UYVY:
+               return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
+       case DRM_FORMAT_VYUY:
+               return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
        default:
-               BUG();
+               MISSING_CASE(pixel_format);
        }
 
-       switch (fb->modifier[0]) {
+       return 0;
+}
+
+u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
+{
+       switch (fb_modifier) {
        case DRM_FORMAT_MOD_NONE:
                break;
        case I915_FORMAT_MOD_X_TILED:
-               plane_ctl |= PLANE_CTL_TILED_X;
-               break;
+               return PLANE_CTL_TILED_X;
        case I915_FORMAT_MOD_Y_TILED:
-               plane_ctl |= PLANE_CTL_TILED_Y;
-               break;
+               return PLANE_CTL_TILED_Y;
        case I915_FORMAT_MOD_Yf_TILED:
-               plane_ctl |= PLANE_CTL_TILED_YF;
+               return PLANE_CTL_TILED_YF;
+       default:
+               MISSING_CASE(fb_modifier);
+       }
+
+       return 0;
+}
+
+u32 skl_plane_ctl_rotation(unsigned int rotation)
+{
+       switch (rotation) {
+       case BIT(DRM_ROTATE_0):
                break;
+       /*
+        * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
+        * while i915 HW rotation is clockwise, thats why this swapping.
+        */
+       case BIT(DRM_ROTATE_90):
+               return PLANE_CTL_ROTATE_270;
+       case BIT(DRM_ROTATE_180):
+               return PLANE_CTL_ROTATE_180;
+       case BIT(DRM_ROTATE_270):
+               return PLANE_CTL_ROTATE_90;
        default:
-               MISSING_CASE(fb->modifier[0]);
+               MISSING_CASE(rotation);
+       }
+
+       return 0;
+}
+
+static void skylake_update_primary_plane(struct drm_crtc *crtc,
+                                        struct drm_framebuffer *fb,
+                                        int x, int y)
+{
+       struct drm_device *dev = crtc->dev;
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct drm_plane *plane = crtc->primary;
+       bool visible = to_intel_plane_state(plane->state)->visible;
+       struct drm_i915_gem_object *obj;
+       int pipe = intel_crtc->pipe;
+       u32 plane_ctl, stride_div, stride;
+       u32 tile_height, plane_offset, plane_size;
+       unsigned int rotation;
+       int x_offset, y_offset;
+       unsigned long surf_addr;
+       struct intel_crtc_state *crtc_state = intel_crtc->config;
+       struct intel_plane_state *plane_state;
+       int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
+       int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
+       int scaler_id = -1;
+
+       plane_state = to_intel_plane_state(plane->state);
+
+       if (!visible || !fb) {
+               I915_WRITE(PLANE_CTL(pipe, 0), 0);
+               I915_WRITE(PLANE_SURF(pipe, 0), 0);
+               POSTING_READ(PLANE_CTL(pipe, 0));
+               return;
        }
 
+       plane_ctl = PLANE_CTL_ENABLE |
+                   PLANE_CTL_PIPE_GAMMA_ENABLE |
+                   PLANE_CTL_PIPE_CSC_ENABLE;
+
+       plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
+       plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
        plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
-       if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
-               plane_ctl |= PLANE_CTL_ROTATE_180;
+
+       rotation = plane->state->rotation;
+       plane_ctl |= skl_plane_ctl_rotation(rotation);
 
        obj = intel_fb_obj(fb);
        stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
                                               fb->pixel_format);
-       surf_addr = intel_plane_obj_offset(to_intel_plane(crtc->primary), obj);
+       surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
+
+       /*
+        * FIXME: intel_plane_state->src, dst aren't set when transitional
+        * update_plane helpers are called from legacy paths.
+        * Once full atomic crtc is available, below check can be avoided.
+        */
+       if (drm_rect_width(&plane_state->src)) {
+               scaler_id = plane_state->scaler_id;
+               src_x = plane_state->src.x1 >> 16;
+               src_y = plane_state->src.y1 >> 16;
+               src_w = drm_rect_width(&plane_state->src) >> 16;
+               src_h = drm_rect_height(&plane_state->src) >> 16;
+               dst_x = plane_state->dst.x1;
+               dst_y = plane_state->dst.y1;
+               dst_w = drm_rect_width(&plane_state->dst);
+               dst_h = drm_rect_height(&plane_state->dst);
+
+               WARN_ON(x != src_x || y != src_y);
+       } else {
+               src_w = intel_crtc->config->pipe_src_w;
+               src_h = intel_crtc->config->pipe_src_h;
+       }
+
+       if (intel_rotation_90_or_270(rotation)) {
+               /* stride = Surface height in tiles */
+               tile_height = intel_tile_height(dev, fb->pixel_format,
+                                               fb->modifier[0]);
+               stride = DIV_ROUND_UP(fb->height, tile_height);
+               x_offset = stride * tile_height - y - src_h;
+               y_offset = x;
+               plane_size = (src_w - 1) << 16 | (src_h - 1);
+       } else {
+               stride = fb->pitches[0] / stride_div;
+               x_offset = x;
+               y_offset = y;
+               plane_size = (src_h - 1) << 16 | (src_w - 1);
+       }
+       plane_offset = y_offset << 16 | x_offset;
 
        I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
-       I915_WRITE(PLANE_POS(pipe, 0), 0);
-       I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
-       I915_WRITE(PLANE_SIZE(pipe, 0),
-                  (intel_crtc->config->pipe_src_h - 1) << 16 |
-                  (intel_crtc->config->pipe_src_w - 1));
-       I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
+       I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
+       I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
+       I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
+
+       if (scaler_id >= 0) {
+               uint32_t ps_ctrl = 0;
+
+               WARN_ON(!dst_w || !dst_h);
+               ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
+                       crtc_state->scaler_state.scalers[scaler_id].mode;
+               I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
+               I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
+               I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
+               I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
+               I915_WRITE(PLANE_POS(pipe, 0), 0);
+       } else {
+               I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
+       }
+
        I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
 
        POSTING_READ(PLANE_SURF(pipe, 0));
@@ -3081,9 +3182,6 @@ static void intel_update_primary_planes(struct drm_device *dev)
 
 void intel_prepare_reset(struct drm_device *dev)
 {
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       struct intel_crtc *crtc;
-
        /* no reset support for gen2 */
        if (IS_GEN2(dev))
                return;
@@ -3093,15 +3191,11 @@ void intel_prepare_reset(struct drm_device *dev)
                return;
 
        drm_modeset_lock_all(dev);
-
        /*
         * Disabling the crtcs gracefully seems nicer. Also the
         * g33 docs say we should at least disable all the planes.
         */
-       for_each_intel_crtc(dev, crtc) {
-               if (crtc->active)
-                       dev_priv->display.crtc_disable(&crtc->base);
-       }
+       intel_display_suspend(dev);
 }
 
 void intel_finish_reset(struct drm_device *dev)
@@ -3152,27 +3246,30 @@ void intel_finish_reset(struct drm_device *dev)
        drm_modeset_unlock_all(dev);
 }
 
-static int
+static void
 intel_finish_fb(struct drm_framebuffer *old_fb)
 {
        struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
-       struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
        bool was_interruptible = dev_priv->mm.interruptible;
        int ret;
 
        /* Big Hammer, we also need to ensure that any pending
         * MI_WAIT_FOR_EVENT inside a user batch buffer on the
         * current scanout is retired before unpinning the old
-        * framebuffer.
+        * framebuffer. Note that we rely on userspace rendering
+        * into the buffer attached to the pipe they are waiting
+        * on. If not, userspace generates a GPU hang with IPEHR
+        * point to the MI_WAIT_FOR_EVENT.
         *
         * This should only fail upon a hung GPU, in which case we
         * can safely continue.
         */
        dev_priv->mm.interruptible = false;
-       ret = i915_gem_object_finish_gpu(obj);
+       ret = i915_gem_object_wait_rendering(obj, true);
        dev_priv->mm.interruptible = was_interruptible;
 
-       return ret;
+       WARN_ON(ret);
 }
 
 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
@@ -3824,7 +3921,7 @@ static void lpt_program_iclkip(struct drm_crtc *crtc)
        u32 divsel, phaseinc, auxdiv, phasedir = 0;
        u32 temp;
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
 
        /* It is necessary to ungate the pixclk gate prior to programming
         * the divisors, and gate it back when it is done.
@@ -3901,7 +3998,7 @@ static void lpt_program_iclkip(struct drm_crtc *crtc)
 
        I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
 
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 }
 
 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
@@ -4039,8 +4136,7 @@ static void ironlake_pch_enable(struct drm_crtc *crtc)
                temp &= ~(TRANS_DP_PORT_SEL_MASK |
                          TRANS_DP_SYNC_MASK |
                          TRANS_DP_BPC_MASK);
-               temp |= (TRANS_DP_OUTPUT_ENABLE |
-                        TRANS_DP_ENH_FRAMING);
+               temp |= TRANS_DP_OUTPUT_ENABLE;
                temp |= bpc << 9; /* same format but at 11:9 */
 
                if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
@@ -4085,34 +4181,16 @@ static void lpt_pch_enable(struct drm_crtc *crtc)
        lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
 }
 
-void intel_put_shared_dpll(struct intel_crtc *crtc)
-{
-       struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
-
-       if (pll == NULL)
-               return;
-
-       if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
-               WARN(1, "bad %s crtc mask\n", pll->name);
-               return;
-       }
-
-       pll->config.crtc_mask &= ~(1 << crtc->pipe);
-       if (pll->config.crtc_mask == 0) {
-               WARN_ON(pll->on);
-               WARN_ON(pll->active);
-       }
-
-       crtc->config->shared_dpll = DPLL_ID_PRIVATE;
-}
-
 struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
                                                struct intel_crtc_state *crtc_state)
 {
        struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
        struct intel_shared_dpll *pll;
+       struct intel_shared_dpll_config *shared_dpll;
        enum intel_dpll_id i;
 
+       shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
+
        if (HAS_PCH_IBX(dev_priv->dev)) {
                /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
                i = (enum intel_dpll_id) crtc->pipe;
@@ -4121,7 +4199,27 @@ struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
                DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
                              crtc->base.base.id, pll->name);
 
-               WARN_ON(pll->new_config->crtc_mask);
+               WARN_ON(shared_dpll[i].crtc_mask);
+
+               goto found;
+       }
+
+       if (IS_BROXTON(dev_priv->dev)) {
+               /* PLL is attached to port in bxt */
+               struct intel_encoder *encoder;
+               struct intel_digital_port *intel_dig_port;
+
+               encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
+               if (WARN_ON(!encoder))
+                       return NULL;
+
+               intel_dig_port = enc_to_dig_port(&encoder->base);
+               /* 1:1 mapping between ports and PLLs */
+               i = (enum intel_dpll_id)intel_dig_port->port;
+               pll = &dev_priv->shared_dplls[i];
+               DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
+                       crtc->base.base.id, pll->name);
+               WARN_ON(shared_dpll[i].crtc_mask);
 
                goto found;
        }
@@ -4130,15 +4228,15 @@ struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
                pll = &dev_priv->shared_dplls[i];
 
                /* Only want to check enabled timings first */
-               if (pll->new_config->crtc_mask == 0)
+               if (shared_dpll[i].crtc_mask == 0)
                        continue;
 
                if (memcmp(&crtc_state->dpll_hw_state,
-                          &pll->new_config->hw_state,
-                          sizeof(pll->new_config->hw_state)) == 0) {
+                          &shared_dpll[i].hw_state,
+                          sizeof(crtc_state->dpll_hw_state)) == 0) {
                        DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
                                      crtc->base.base.id, pll->name,
-                                     pll->new_config->crtc_mask,
+                                     shared_dpll[i].crtc_mask,
                                      pll->active);
                        goto found;
                }
@@ -4147,7 +4245,7 @@ struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
        /* Ok no matching timings, maybe there's a free one? */
        for (i = 0; i < dev_priv->num_shared_dpll; i++) {
                pll = &dev_priv->shared_dplls[i];
-               if (pll->new_config->crtc_mask == 0) {
+               if (shared_dpll[i].crtc_mask == 0) {
                        DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
                                      crtc->base.base.id, pll->name);
                        goto found;
@@ -4157,83 +4255,33 @@ struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
        return NULL;
 
 found:
-       if (pll->new_config->crtc_mask == 0)
-               pll->new_config->hw_state = crtc_state->dpll_hw_state;
+       if (shared_dpll[i].crtc_mask == 0)
+               shared_dpll[i].hw_state =
+                       crtc_state->dpll_hw_state;
 
        crtc_state->shared_dpll = i;
        DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
                         pipe_name(crtc->pipe));
 
-       pll->new_config->crtc_mask |= 1 << crtc->pipe;
+       shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
 
        return pll;
 }
 
-/**
- * intel_shared_dpll_start_config - start a new PLL staged config
- * @dev_priv: DRM device
- * @clear_pipes: mask of pipes that will have their PLLs freed
- *
- * Starts a new PLL staged config, copying the current config but
- * releasing the references of pipes specified in clear_pipes.
- */
-static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
-                                         unsigned clear_pipes)
-{
-       struct intel_shared_dpll *pll;
-       enum intel_dpll_id i;
-
-       for (i = 0; i < dev_priv->num_shared_dpll; i++) {
-               pll = &dev_priv->shared_dplls[i];
-
-               pll->new_config = kmemdup(&pll->config, sizeof pll->config,
-                                         GFP_KERNEL);
-               if (!pll->new_config)
-                       goto cleanup;
-
-               pll->new_config->crtc_mask &= ~clear_pipes;
-       }
-
-       return 0;
-
-cleanup:
-       while (--i >= 0) {
-               pll = &dev_priv->shared_dplls[i];
-               kfree(pll->new_config);
-               pll->new_config = NULL;
-       }
-
-       return -ENOMEM;
-}
-
-static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
+static void intel_shared_dpll_commit(struct drm_atomic_state *state)
 {
+       struct drm_i915_private *dev_priv = to_i915(state->dev);
+       struct intel_shared_dpll_config *shared_dpll;
        struct intel_shared_dpll *pll;
        enum intel_dpll_id i;
 
-       for (i = 0; i < dev_priv->num_shared_dpll; i++) {
-               pll = &dev_priv->shared_dplls[i];
-
-               WARN_ON(pll->new_config == &pll->config);
-
-               pll->config = *pll->new_config;
-               kfree(pll->new_config);
-               pll->new_config = NULL;
-       }
-}
-
-static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
-{
-       struct intel_shared_dpll *pll;
-       enum intel_dpll_id i;
+       if (!to_intel_atomic_state(state)->dpll_set)
+               return;
 
+       shared_dpll = to_intel_atomic_state(state)->shared_dpll;
        for (i = 0; i < dev_priv->num_shared_dpll; i++) {
                pll = &dev_priv->shared_dplls[i];
-
-               WARN_ON(pll->new_config == &pll->config);
-
-               kfree(pll->new_config);
-               pll->new_config = NULL;
+               pll->config = shared_dpll[i];
        }
 }
 
@@ -4251,16 +4299,180 @@ static void cpt_verify_modeset(struct drm_device *dev, int pipe)
        }
 }
 
-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;
-       int pipe = crtc->pipe;
+/**
+ * skl_update_scaler_users - Stages update to crtc's scaler state
+ * @intel_crtc: crtc
+ * @crtc_state: crtc_state
+ * @plane: plane (NULL indicates crtc is requesting update)
+ * @plane_state: plane's state
+ * @force_detach: request unconditional detachment of scaler
+ *
+ * This function updates scaler state for requested plane or crtc.
+ * To request scaler usage update for a plane, caller shall pass plane pointer.
+ * To request scaler usage update for crtc, caller shall pass plane pointer
+ * as NULL.
+ *
+ * Return
+ *     0 - scaler_usage updated successfully
+ *    error - requested scaling cannot be supported or other error condition
+ */
+int
+skl_update_scaler_users(
+       struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
+       struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
+       int force_detach)
+{
+       int need_scaling;
+       int idx;
+       int src_w, src_h, dst_w, dst_h;
+       int *scaler_id;
+       struct drm_framebuffer *fb;
+       struct intel_crtc_scaler_state *scaler_state;
+       unsigned int rotation;
 
-       if (crtc->config->pch_pfit.enabled) {
-               I915_WRITE(PS_CTL(pipe), PS_ENABLE);
-               I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
-               I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
+       if (!intel_crtc || !crtc_state)
+               return 0;
+
+       scaler_state = &crtc_state->scaler_state;
+
+       idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
+       fb = intel_plane ? plane_state->base.fb : NULL;
+
+       if (intel_plane) {
+               src_w = drm_rect_width(&plane_state->src) >> 16;
+               src_h = drm_rect_height(&plane_state->src) >> 16;
+               dst_w = drm_rect_width(&plane_state->dst);
+               dst_h = drm_rect_height(&plane_state->dst);
+               scaler_id = &plane_state->scaler_id;
+               rotation = plane_state->base.rotation;
+       } else {
+               struct drm_display_mode *adjusted_mode =
+                       &crtc_state->base.adjusted_mode;
+               src_w = crtc_state->pipe_src_w;
+               src_h = crtc_state->pipe_src_h;
+               dst_w = adjusted_mode->hdisplay;
+               dst_h = adjusted_mode->vdisplay;
+               scaler_id = &scaler_state->scaler_id;
+               rotation = DRM_ROTATE_0;
+       }
+
+       need_scaling = intel_rotation_90_or_270(rotation) ?
+               (src_h != dst_w || src_w != dst_h):
+               (src_w != dst_w || src_h != dst_h);
+
+       /*
+        * if plane is being disabled or scaler is no more required or force detach
+        *  - free scaler binded to this plane/crtc
+        *  - in order to do this, update crtc->scaler_usage
+        *
+        * Here scaler state in crtc_state is set free so that
+        * scaler can be assigned to other user. Actual register
+        * update to free the scaler is done in plane/panel-fit programming.
+        * For this purpose crtc/plane_state->scaler_id isn't reset here.
+        */
+       if (force_detach || !need_scaling || (intel_plane &&
+               (!fb || !plane_state->visible))) {
+               if (*scaler_id >= 0) {
+                       scaler_state->scaler_users &= ~(1 << idx);
+                       scaler_state->scalers[*scaler_id].in_use = 0;
+
+                       DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
+                               "crtc_state = %p scaler_users = 0x%x\n",
+                               intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
+                               intel_plane ? intel_plane->base.base.id :
+                               intel_crtc->base.base.id, crtc_state,
+                               scaler_state->scaler_users);
+                       *scaler_id = -1;
+               }
+               return 0;
+       }
+
+       /* range checks */
+       if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
+               dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
+
+               src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
+               dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
+               DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
+                       "size is out of scaler range\n",
+                       intel_plane ? "PLANE" : "CRTC",
+                       intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
+                       intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
+               return -EINVAL;
+       }
+
+       /* check colorkey */
+       if (WARN_ON(intel_plane &&
+               intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
+               DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
+                       intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
+               return -EINVAL;
+       }
+
+       /* Check src format */
+       if (intel_plane) {
+               switch (fb->pixel_format) {
+               case DRM_FORMAT_RGB565:
+               case DRM_FORMAT_XBGR8888:
+               case DRM_FORMAT_XRGB8888:
+               case DRM_FORMAT_ABGR8888:
+               case DRM_FORMAT_ARGB8888:
+               case DRM_FORMAT_XRGB2101010:
+               case DRM_FORMAT_XBGR2101010:
+               case DRM_FORMAT_YUYV:
+               case DRM_FORMAT_YVYU:
+               case DRM_FORMAT_UYVY:
+               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);
+                       return -EINVAL;
+               }
+       }
+
+       /* mark this plane as a scaler user in crtc_state */
+       scaler_state->scaler_users |= (1 << idx);
+       DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
+               "crtc_state = %p scaler_users = 0x%x\n",
+               intel_plane ? "PLANE" : "CRTC",
+               intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
+               src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
+       return 0;
+}
+
+static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
+{
+       struct drm_device *dev = crtc->base.dev;
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       int pipe = crtc->pipe;
+       struct intel_crtc_scaler_state *scaler_state =
+               &crtc->config->scaler_state;
+
+       DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
+
+       /* To update pfit, first update scaler state */
+       skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
+       intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
+       skl_detach_scalers(crtc);
+       if (!enable)
+               return;
+
+       if (crtc->config->pch_pfit.enabled) {
+               int id;
+
+               if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
+                       DRM_ERROR("Requesting pfit without getting a scaler first\n");
+                       return;
+               }
+
+               id = scaler_state->scaler_id;
+               I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
+                       PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
+               I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
+               I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
+
+               DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
        }
 }
 
@@ -4299,38 +4511,6 @@ static void intel_enable_sprite_planes(struct drm_crtc *crtc)
        }
 }
 
-/*
- * Disable a plane internally without actually modifying the plane's state.
- * This will allow us to easily restore the plane later by just reprogramming
- * its state.
- */
-static void disable_plane_internal(struct drm_plane *plane)
-{
-       struct intel_plane *intel_plane = to_intel_plane(plane);
-       struct drm_plane_state *state =
-               plane->funcs->atomic_duplicate_state(plane);
-       struct intel_plane_state *intel_state = to_intel_plane_state(state);
-
-       intel_state->visible = false;
-       intel_plane->commit_plane(plane, intel_state);
-
-       intel_plane_destroy_state(plane, state);
-}
-
-static void intel_disable_sprite_planes(struct drm_crtc *crtc)
-{
-       struct drm_device *dev = crtc->dev;
-       enum pipe pipe = to_intel_crtc(crtc)->pipe;
-       struct drm_plane *plane;
-       struct intel_plane *intel_plane;
-
-       drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
-               intel_plane = to_intel_plane(plane);
-               if (plane->fb && intel_plane->pipe == pipe)
-                       disable_plane_internal(plane);
-       }
-}
-
 void hsw_enable_ips(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
@@ -4401,10 +4581,10 @@ static void intel_crtc_load_lut(struct drm_crtc *crtc)
        bool reenable_ips = false;
 
        /* The clocks have to be on to load the palette. */
-       if (!crtc->state->enable || !intel_crtc->active)
+       if (!crtc->state->active)
                return;
 
-       if (!HAS_PCH_SPLIT(dev_priv->dev)) {
+       if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
                if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
                        assert_dsi_pll_enabled(dev_priv);
                else
@@ -4436,9 +4616,9 @@ static void intel_crtc_load_lut(struct drm_crtc *crtc)
                hsw_enable_ips(intel_crtc);
 }
 
-static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
+static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
 {
-       if (!enable && intel_crtc->overlay) {
+       if (intel_crtc->overlay) {
                struct drm_device *dev = intel_crtc->base.dev;
                struct drm_i915_private *dev_priv = dev->dev_private;
 
@@ -4454,27 +4634,128 @@ static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
         */
 }
 
-static void intel_crtc_enable_planes(struct drm_crtc *crtc)
+/**
+ * intel_post_enable_primary - Perform operations after enabling primary plane
+ * @crtc: the CRTC whose primary plane was just enabled
+ *
+ * Performs potentially sleeping operations that must be done after the primary
+ * plane is enabled, such as updating FBC and IPS.  Note that this may be
+ * called due to an explicit primary plane update, or due to an implicit
+ * re-enable that is caused when a sprite plane is updated to no longer
+ * completely hide the primary plane.
+ */
+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 intel_crtc *intel_crtc = to_intel_crtc(crtc);
        int pipe = intel_crtc->pipe;
 
-       intel_enable_primary_hw_plane(crtc->primary, crtc);
-       intel_enable_sprite_planes(crtc);
-       intel_crtc_update_cursor(crtc, true);
-       intel_crtc_dpms_overlay(intel_crtc, true);
+       /*
+        * 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 (IS_BROADWELL(dev))
+               intel_wait_for_vblank(dev, pipe);
 
+       /*
+        * FIXME IPS should be fine as long as one plane is
+        * enabled, but in practice it seems to have problems
+        * when going from primary only to sprite only and vice
+        * versa.
+        */
        hsw_enable_ips(intel_crtc);
 
        mutex_lock(&dev->struct_mutex);
        intel_fbc_update(dev);
        mutex_unlock(&dev->struct_mutex);
 
+       /*
+        * Gen2 reports pipe underruns whenever all planes are disabled.
+        * So don't enable underrun reporting before at least some planes
+        * are enabled.
+        * FIXME: Need to fix the logic to work when we turn off all planes
+        * but leave the pipe running.
+        */
+       if (IS_GEN2(dev))
+               intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
+
+       /* Underruns don't raise interrupts, so check manually. */
+       if (HAS_GMCH_DISPLAY(dev))
+               i9xx_check_fifo_underruns(dev_priv);
+}
+
+/**
+ * intel_pre_disable_primary - Perform operations before disabling primary plane
+ * @crtc: the CRTC whose primary plane is to be disabled
+ *
+ * Performs potentially sleeping operations that must be done before the
+ * primary plane is disabled, such as updating FBC and IPS.  Note that this may
+ * be called due to an explicit primary plane update, or due to an implicit
+ * disable that is caused when a sprite plane completely hides the primary
+ * plane.
+ */
+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 intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       int pipe = intel_crtc->pipe;
+
+       /*
+        * Gen2 reports pipe underruns whenever all planes are disabled.
+        * So diasble underrun reporting before all the planes get disabled.
+        * FIXME: Need to fix the logic to work when we turn off all planes
+        * but leave the pipe running.
+        */
+       if (IS_GEN2(dev))
+               intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
+
+       /*
+        * Vblank time updates from the shadow to live plane control register
+        * are blocked if the memory self-refresh mode is active at that
+        * moment. So to make sure the plane gets truly disabled, disable
+        * first the self-refresh mode. The self-refresh enable bit in turn
+        * will be checked/applied by the HW only at the next frame start
+        * event which is after the vblank start event, so we need to have a
+        * wait-for-vblank between disabling the plane and the pipe.
+        */
+       if (HAS_GMCH_DISPLAY(dev))
+               intel_set_memory_cxsr(dev_priv, false);
+
+       mutex_lock(&dev->struct_mutex);
+       if (dev_priv->fbc.crtc == intel_crtc)
+               intel_fbc_disable(dev);
+       mutex_unlock(&dev->struct_mutex);
+
+       /*
+        * FIXME IPS should be fine as long as one plane is
+        * enabled, but in practice it seems to have problems
+        * when going from primary only to sprite only and vice
+        * versa.
+        */
+       hsw_disable_ips(intel_crtc);
+}
+
+static void intel_crtc_enable_planes(struct drm_crtc *crtc)
+{
+       struct drm_device *dev = crtc->dev;
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       int pipe = intel_crtc->pipe;
+
+       intel_enable_primary_hw_plane(crtc->primary, crtc);
+       intel_enable_sprite_planes(crtc);
+       intel_crtc_update_cursor(crtc, true);
+
+       intel_post_enable_primary(crtc);
+
        /*
         * FIXME: Once we grow proper nuclear flip support out of this we need
         * to compute the mask of flip planes precisely. For the time being
-        * consider this a flip from a NULL plane.
+        * consider this a flip to a NULL plane.
         */
        intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
 }
@@ -4482,21 +4763,23 @@ static void intel_crtc_enable_planes(struct drm_crtc *crtc)
 static void intel_crtc_disable_planes(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct intel_plane *intel_plane;
        int pipe = intel_crtc->pipe;
 
        intel_crtc_wait_for_pending_flips(crtc);
 
-       if (dev_priv->fbc.crtc == intel_crtc)
-               intel_fbc_disable(dev);
+       intel_pre_disable_primary(crtc);
 
-       hsw_disable_ips(intel_crtc);
+       intel_crtc_dpms_overlay_disable(intel_crtc);
+       for_each_intel_plane(dev, intel_plane) {
+               if (intel_plane->pipe == pipe) {
+                       struct drm_crtc *from = intel_plane->base.crtc;
 
-       intel_crtc_dpms_overlay(intel_crtc, false);
-       intel_crtc_update_cursor(crtc, false);
-       intel_disable_sprite_planes(crtc);
-       intel_disable_primary_hw_plane(crtc->primary, crtc);
+                       intel_plane->disable_plane(&intel_plane->base,
+                                                  from ?: crtc, true);
+               }
+       }
 
        /*
         * FIXME: Once we grow proper nuclear flip support out of this we need
@@ -4514,9 +4797,7 @@ static void ironlake_crtc_enable(struct drm_crtc *crtc)
        struct intel_encoder *encoder;
        int pipe = intel_crtc->pipe;
 
-       WARN_ON(!crtc->state->enable);
-
-       if (intel_crtc->active)
+       if (WARN_ON(intel_crtc->active))
                return;
 
        if (intel_crtc->config->has_pch_encoder)
@@ -4575,8 +4856,6 @@ static void ironlake_crtc_enable(struct drm_crtc *crtc)
 
        if (HAS_PCH_CPT(dev))
                cpt_verify_modeset(dev, intel_crtc->pipe);
-
-       intel_crtc_enable_planes(crtc);
 }
 
 /* IPS only exists on ULT machines and is tied to pipe A. */
@@ -4585,46 +4864,17 @@ static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
        return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
 }
 
-/*
- * This implements the workaround described in the "notes" section of the mode
- * set sequence documentation. When going from no pipes or single pipe to
- * multiple pipes, and planes are enabled after the pipe, we need to wait at
- * least 2 vblanks on the first pipe before enabling planes on the second pipe.
- */
-static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
-{
-       struct drm_device *dev = crtc->base.dev;
-       struct intel_crtc *crtc_it, *other_active_crtc = NULL;
-
-       /* We want to get the other_active_crtc only if there's only 1 other
-        * active crtc. */
-       for_each_intel_crtc(dev, crtc_it) {
-               if (!crtc_it->active || crtc_it == crtc)
-                       continue;
-
-               if (other_active_crtc)
-                       return;
-
-               other_active_crtc = crtc_it;
-       }
-       if (!other_active_crtc)
-               return;
-
-       intel_wait_for_vblank(dev, other_active_crtc->pipe);
-       intel_wait_for_vblank(dev, other_active_crtc->pipe);
-}
-
 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 intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct intel_encoder *encoder;
-       int pipe = intel_crtc->pipe;
+       int pipe = intel_crtc->pipe, hsw_workaround_pipe;
+       struct intel_crtc_state *pipe_config =
+               to_intel_crtc_state(crtc->state);
 
-       WARN_ON(!crtc->state->enable);
-
-       if (intel_crtc->active)
+       if (WARN_ON(intel_crtc->active))
                return;
 
        if (intel_crtc_to_shared_dpll(intel_crtc))
@@ -4664,10 +4914,12 @@ static void haswell_crtc_enable(struct drm_crtc *crtc)
 
        intel_ddi_enable_pipe_clock(intel_crtc);
 
-       if (IS_SKYLAKE(dev))
-               skylake_pfit_enable(intel_crtc);
-       else
+       if (INTEL_INFO(dev)->gen == 9)
+               skylake_pfit_update(intel_crtc, 1);
+       else if (INTEL_INFO(dev)->gen < 9)
                ironlake_pfit_enable(intel_crtc);
+       else
+               MISSING_CASE(INTEL_INFO(dev)->gen);
 
        /*
         * On ILK+ LUT must be loaded before the pipe is running but with
@@ -4697,22 +4949,10 @@ static void haswell_crtc_enable(struct drm_crtc *crtc)
 
        /* If we change the relative order between pipe/planes enabling, we need
         * to change the workaround. */
-       haswell_mode_set_planes_workaround(intel_crtc);
-       intel_crtc_enable_planes(crtc);
-}
-
-static void skylake_pfit_disable(struct intel_crtc *crtc)
-{
-       struct drm_device *dev = crtc->base.dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       int pipe = crtc->pipe;
-
-       /* To avoid upsetting the power well on haswell only disable the pfit if
-        * it's in use. The hw state code will make sure we get this right. */
-       if (crtc->config->pch_pfit.enabled) {
-               I915_WRITE(PS_CTL(pipe), 0);
-               I915_WRITE(PS_WIN_POS(pipe), 0);
-               I915_WRITE(PS_WIN_SZ(pipe), 0);
+       hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
+       if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
+               intel_wait_for_vblank(dev, hsw_workaround_pipe);
+               intel_wait_for_vblank(dev, hsw_workaround_pipe);
        }
 }
 
@@ -4740,11 +4980,9 @@ static void ironlake_crtc_disable(struct drm_crtc *crtc)
        int pipe = intel_crtc->pipe;
        u32 reg, temp;
 
-       if (!intel_crtc->active)
+       if (WARN_ON(!intel_crtc->active))
                return;
 
-       intel_crtc_disable_planes(crtc);
-
        for_each_encoder_on_crtc(dev, crtc, encoder)
                encoder->disable(encoder);
 
@@ -4758,13 +4996,14 @@ static void ironlake_crtc_disable(struct drm_crtc *crtc)
 
        ironlake_pfit_disable(intel_crtc);
 
+       if (intel_crtc->config->has_pch_encoder)
+               ironlake_fdi_disable(crtc);
+
        for_each_encoder_on_crtc(dev, crtc, encoder)
                if (encoder->post_disable)
                        encoder->post_disable(encoder);
 
        if (intel_crtc->config->has_pch_encoder) {
-               ironlake_fdi_disable(crtc);
-
                ironlake_disable_pch_transcoder(dev_priv, pipe);
 
                if (HAS_PCH_CPT(dev)) {
@@ -4804,11 +5043,9 @@ static void haswell_crtc_disable(struct drm_crtc *crtc)
        struct intel_encoder *encoder;
        enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
 
-       if (!intel_crtc->active)
+       if (WARN_ON(!intel_crtc->active))
                return;
 
-       intel_crtc_disable_planes(crtc);
-
        for_each_encoder_on_crtc(dev, crtc, encoder) {
                intel_opregion_notify_encoder(encoder, false);
                encoder->disable(encoder);
@@ -4827,10 +5064,12 @@ static void haswell_crtc_disable(struct drm_crtc *crtc)
 
        intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
 
-       if (IS_SKYLAKE(dev))
-               skylake_pfit_disable(intel_crtc);
-       else
+       if (INTEL_INFO(dev)->gen == 9)
+               skylake_pfit_update(intel_crtc, 0);
+       else if (INTEL_INFO(dev)->gen < 9)
                ironlake_pfit_disable(intel_crtc);
+       else
+               MISSING_CASE(INTEL_INFO(dev)->gen);
 
        intel_ddi_disable_pipe_clock(intel_crtc);
 
@@ -4854,13 +5093,6 @@ static void haswell_crtc_disable(struct drm_crtc *crtc)
                intel_disable_shared_dpll(intel_crtc);
 }
 
-static void ironlake_crtc_off(struct drm_crtc *crtc)
-{
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       intel_put_shared_dpll(intel_crtc);
-}
-
-
 static void i9xx_pfit_enable(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
@@ -4994,69 +5226,512 @@ static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
        intel_display_set_init_power(dev_priv, false);
 }
 
-/* returns HPLL frequency in kHz */
-static int valleyview_get_vco(struct drm_i915_private *dev_priv)
+static void intel_update_max_cdclk(struct drm_device *dev)
 {
-       int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
+       struct drm_i915_private *dev_priv = dev->dev_private;
 
-       /* Obtain SKU information */
-       mutex_lock(&dev_priv->dpio_lock);
-       hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
-               CCK_FUSE_HPLL_FREQ_MASK;
-       mutex_unlock(&dev_priv->dpio_lock);
+       if (IS_SKYLAKE(dev)) {
+               u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
 
-       return vco_freq[hpll_freq] * 1000;
+               if (limit == SKL_DFSM_CDCLK_LIMIT_675)
+                       dev_priv->max_cdclk_freq = 675000;
+               else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
+                       dev_priv->max_cdclk_freq = 540000;
+               else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
+                       dev_priv->max_cdclk_freq = 450000;
+               else
+                       dev_priv->max_cdclk_freq = 337500;
+       } else if (IS_BROADWELL(dev))  {
+               /*
+                * FIXME with extra cooling we can allow
+                * 540 MHz for ULX and 675 Mhz for ULT.
+                * How can we know if extra cooling is
+                * available? PCI ID, VTB, something else?
+                */
+               if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
+                       dev_priv->max_cdclk_freq = 450000;
+               else if (IS_BDW_ULX(dev))
+                       dev_priv->max_cdclk_freq = 450000;
+               else if (IS_BDW_ULT(dev))
+                       dev_priv->max_cdclk_freq = 540000;
+               else
+                       dev_priv->max_cdclk_freq = 675000;
+       } else if (IS_VALLEYVIEW(dev)) {
+               dev_priv->max_cdclk_freq = 400000;
+       } else {
+               /* otherwise assume cdclk is fixed */
+               dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
+       }
+
+       DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
+                        dev_priv->max_cdclk_freq);
 }
 
-static void vlv_update_cdclk(struct drm_device *dev)
+static void intel_update_cdclk(struct drm_device *dev)
 {
        struct drm_i915_private *dev_priv = dev->dev_private;
 
-       dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
+       dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
        DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
-                        dev_priv->vlv_cdclk_freq);
+                        dev_priv->cdclk_freq);
 
        /*
         * Program the gmbus_freq based on the cdclk frequency.
         * BSpec erroneously claims we should aim for 4MHz, but
         * in fact 1MHz is the correct frequency.
         */
-       I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
+       if (IS_VALLEYVIEW(dev)) {
+               /*
+                * Program the gmbus_freq based on the cdclk frequency.
+                * BSpec erroneously claims we should aim for 4MHz, but
+                * in fact 1MHz is the correct frequency.
+                */
+               I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
+       }
+
+       if (dev_priv->max_cdclk_freq == 0)
+               intel_update_max_cdclk(dev);
 }
 
-/* Adjust CDclk dividers to allow high res or save power if possible */
-static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
+static void broxton_set_cdclk(struct drm_device *dev, int frequency)
 {
        struct drm_i915_private *dev_priv = dev->dev_private;
-       u32 val, cmd;
+       uint32_t divider;
+       uint32_t ratio;
+       uint32_t current_freq;
+       int ret;
 
-       WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
+       /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
+       switch (frequency) {
+       case 144000:
+               divider = BXT_CDCLK_CD2X_DIV_SEL_4;
+               ratio = BXT_DE_PLL_RATIO(60);
+               break;
+       case 288000:
+               divider = BXT_CDCLK_CD2X_DIV_SEL_2;
+               ratio = BXT_DE_PLL_RATIO(60);
+               break;
+       case 384000:
+               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:
+               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", frequency);
 
-       if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
-               cmd = 2;
-       else if (cdclk == 266667)
-               cmd = 1;
-       else
-               cmd = 0;
+               return;
+       }
 
        mutex_lock(&dev_priv->rps.hw_lock);
-       val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
-       val &= ~DSPFREQGUAR_MASK;
-       val |= (cmd << DSPFREQGUAR_SHIFT);
-       vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
-       if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
-                     DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
+       /* Inform power controller of upcoming frequency change */
+       ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
+                                     0x80000000);
+       mutex_unlock(&dev_priv->rps.hw_lock);
+
+       if (ret) {
+               DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
+                         ret, frequency);
+               return;
+       }
+
+       current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
+       /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
+       current_freq = current_freq * 500 + 1000;
+
+       /*
+        * 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 (frequency == 19200 || frequency == 624000 ||
+           current_freq == 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 (frequency != 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 = I915_READ(CDCLK_CTL);
+               val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
+               val |= divider;
+               /*
+                * Disable SSA Precharge when CD clock frequency < 500 MHz,
+                * enable otherwise.
+                */
+               val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
+               if (frequency >= 500000)
+                       val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
+
+               val &= ~CDCLK_FREQ_DECIMAL_MASK;
+               /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
+               val |= (frequency - 1000) / 500;
+               I915_WRITE(CDCLK_CTL, val);
+       }
+
+       mutex_lock(&dev_priv->rps.hw_lock);
+       ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
+                                     DIV_ROUND_UP(frequency, 25000));
+       mutex_unlock(&dev_priv->rps.hw_lock);
+
+       if (ret) {
+               DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
+                         ret, frequency);
+               return;
+       }
+
+       intel_update_cdclk(dev);
+}
+
+void broxton_init_cdclk(struct drm_device *dev)
+{
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       uint32_t val;
+
+       /*
+        * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
+        * or else the reset will hang because there is no PCH to respond.
+        * Move the handshake programming to initialization sequence.
+        * Previously was left up to BIOS.
+        */
+       val = I915_READ(HSW_NDE_RSTWRN_OPT);
+       val &= ~RESET_PCH_HANDSHAKE_ENABLE;
+       I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
+
+       /* Enable PG1 for cdclk */
+       intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
+
+       /* check if cd clock is enabled */
+       if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
+               DRM_DEBUG_KMS("Display already initialized\n");
+               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, 624000);
+
+       I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
+       POSTING_READ(DBUF_CTL);
+
+       udelay(10);
+
+       if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
+               DRM_ERROR("DBuf power enable timeout!\n");
+}
+
+void broxton_uninit_cdclk(struct drm_device *dev)
+{
+       struct drm_i915_private *dev_priv = dev->dev_private;
+
+       I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
+       POSTING_READ(DBUF_CTL);
+
+       udelay(10);
+
+       if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
+               DRM_ERROR("DBuf power disable timeout!\n");
+
+       /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
+       broxton_set_cdclk(dev, 19200);
+
+       intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
+}
+
+static const struct skl_cdclk_entry {
+       unsigned int freq;
+       unsigned int vco;
+} skl_cdclk_frequencies[] = {
+       { .freq = 308570, .vco = 8640 },
+       { .freq = 337500, .vco = 8100 },
+       { .freq = 432000, .vco = 8640 },
+       { .freq = 450000, .vco = 8100 },
+       { .freq = 540000, .vco = 8100 },
+       { .freq = 617140, .vco = 8640 },
+       { .freq = 675000, .vco = 8100 },
+};
+
+static unsigned int skl_cdclk_decimal(unsigned int freq)
+{
+       return (freq - 1000) / 500;
+}
+
+static unsigned int skl_cdclk_get_vco(unsigned int freq)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
+               const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
+
+               if (e->freq == freq)
+                       return e->vco;
+       }
+
+       return 8100;
+}
+
+static void
+skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
+{
+       unsigned int min_freq;
+       u32 val;
+
+       /* select the minimum CDCLK before enabling DPLL 0 */
+       val = I915_READ(CDCLK_CTL);
+       val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
+       val |= CDCLK_FREQ_337_308;
+
+       if (required_vco == 8640)
+               min_freq = 308570;
+       else
+               min_freq = 337500;
+
+       val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
+
+       I915_WRITE(CDCLK_CTL, val);
+       POSTING_READ(CDCLK_CTL);
+
+       /*
+        * We always enable DPLL0 with the lowest link rate possible, but still
+        * taking into account the VCO required to operate the eDP panel at the
+        * desired frequency. The usual DP link rates operate with a VCO of
+        * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
+        * The modeset code is responsible for the selection of the exact link
+        * rate later on, with the constraint of choosing a frequency that
+        * works with required_vco.
+        */
+       val = I915_READ(DPLL_CTRL1);
+
+       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 (required_vco == 8640)
+               val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
+                                           SKL_DPLL0);
+       else
+               val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
+                                           SKL_DPLL0);
+
+       I915_WRITE(DPLL_CTRL1, val);
+       POSTING_READ(DPLL_CTRL1);
+
+       I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
+
+       if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
+               DRM_ERROR("DPLL0 not locked\n");
+}
+
+static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
+{
+       int ret;
+       u32 val;
+
+       /* inform PCU we want to change CDCLK */
+       val = SKL_CDCLK_PREPARE_FOR_CHANGE;
+       mutex_lock(&dev_priv->rps.hw_lock);
+       ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
+       mutex_unlock(&dev_priv->rps.hw_lock);
+
+       return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
+}
+
+static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
+{
+       unsigned int i;
+
+       for (i = 0; i < 15; i++) {
+               if (skl_cdclk_pcu_ready(dev_priv))
+                       return true;
+               udelay(10);
+       }
+
+       return false;
+}
+
+static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
+{
+       struct drm_device *dev = dev_priv->dev;
+       u32 freq_select, pcu_ack;
+
+       DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
+
+       if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
+               DRM_ERROR("failed to inform PCU about cdclk change\n");
+               return;
+       }
+
+       /* set CDCLK_CTL */
+       switch(freq) {
+       case 450000:
+       case 432000:
+               freq_select = CDCLK_FREQ_450_432;
+               pcu_ack = 1;
+               break;
+       case 540000:
+               freq_select = CDCLK_FREQ_540;
+               pcu_ack = 2;
+               break;
+       case 308570:
+       case 337500:
+       default:
+               freq_select = CDCLK_FREQ_337_308;
+               pcu_ack = 0;
+               break;
+       case 617140:
+       case 675000:
+               freq_select = CDCLK_FREQ_675_617;
+               pcu_ack = 3;
+               break;
+       }
+
+       I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
+       POSTING_READ(CDCLK_CTL);
+
+       /* inform PCU of the change */
+       mutex_lock(&dev_priv->rps.hw_lock);
+       sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
+       mutex_unlock(&dev_priv->rps.hw_lock);
+
+       intel_update_cdclk(dev);
+}
+
+void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
+{
+       /* disable DBUF power */
+       I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
+       POSTING_READ(DBUF_CTL);
+
+       udelay(10);
+
+       if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
+               DRM_ERROR("DBuf power disable timeout\n");
+
+       /* disable DPLL0 */
+       I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
+       if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
+               DRM_ERROR("Couldn't disable DPLL0\n");
+
+       intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
+}
+
+void skl_init_cdclk(struct drm_i915_private *dev_priv)
+{
+       u32 val;
+       unsigned int required_vco;
+
+       /* enable PCH reset handshake */
+       val = I915_READ(HSW_NDE_RSTWRN_OPT);
+       I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
+
+       /* enable PG1 and Misc I/O */
+       intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
+
+       /* DPLL0 already enabed !? */
+       if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
+               DRM_DEBUG_DRIVER("DPLL0 already running\n");
+               return;
+       }
+
+       /* enable DPLL0 */
+       required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
+       skl_dpll0_enable(dev_priv, required_vco);
+
+       /* set CDCLK to the frequency the BIOS chose */
+       skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
+
+       /* enable DBUF power */
+       I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
+       POSTING_READ(DBUF_CTL);
+
+       udelay(10);
+
+       if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
+               DRM_ERROR("DBuf power enable timeout\n");
+}
+
+/* returns HPLL frequency in kHz */
+static int valleyview_get_vco(struct drm_i915_private *dev_priv)
+{
+       int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
+
+       /* Obtain SKU information */
+       mutex_lock(&dev_priv->sb_lock);
+       hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
+               CCK_FUSE_HPLL_FREQ_MASK;
+       mutex_unlock(&dev_priv->sb_lock);
+
+       return vco_freq[hpll_freq] * 1000;
+}
+
+/* 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;
+       u32 val, cmd;
+
+       WARN_ON(dev_priv->display.get_display_clock_speed(dev)
+                                       != dev_priv->cdclk_freq);
+
+       if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
+               cmd = 2;
+       else if (cdclk == 266667)
+               cmd = 1;
+       else
+               cmd = 0;
+
+       mutex_lock(&dev_priv->rps.hw_lock);
+       val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
+       val &= ~DSPFREQGUAR_MASK;
+       val |= (cmd << DSPFREQGUAR_SHIFT);
+       vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
+       if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
+                     DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
                     50)) {
                DRM_ERROR("timed out waiting for CDclk change\n");
        }
        mutex_unlock(&dev_priv->rps.hw_lock);
 
+       mutex_lock(&dev_priv->sb_lock);
+
        if (cdclk == 400000) {
                u32 divider;
 
                divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
 
-               mutex_lock(&dev_priv->dpio_lock);
                /* adjust cdclk divider */
                val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
                val &= ~DISPLAY_FREQUENCY_VALUES;
@@ -5067,10 +5742,8 @@ static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
                              DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
                             50))
                        DRM_ERROR("timed out waiting for CDclk change\n");
-               mutex_unlock(&dev_priv->dpio_lock);
        }
 
-       mutex_lock(&dev_priv->dpio_lock);
        /* adjust self-refresh exit latency value */
        val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
        val &= ~0x7f;
@@ -5084,9 +5757,10 @@ static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
        else
                val |= 3000 / 250; /* 3.0 usec */
        vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
-       mutex_unlock(&dev_priv->dpio_lock);
 
-       vlv_update_cdclk(dev);
+       mutex_unlock(&dev_priv->sb_lock);
+
+       intel_update_cdclk(dev);
 }
 
 static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
@@ -5094,7 +5768,8 @@ static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
        struct drm_i915_private *dev_priv = dev->dev_private;
        u32 val, cmd;
 
-       WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
+       WARN_ON(dev_priv->display.get_display_clock_speed(dev)
+                                               != dev_priv->cdclk_freq);
 
        switch (cdclk) {
        case 333333:
@@ -5126,7 +5801,7 @@ static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
        }
        mutex_unlock(&dev_priv->rps.hw_lock);
 
-       vlv_update_cdclk(dev);
+       intel_update_cdclk(dev);
 }
 
 static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
@@ -5159,37 +5834,94 @@ static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
                return 200000;
 }
 
-/* compute the max pixel clock for new configuration */
-static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
+static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
+                             int max_pixclk)
+{
+       /*
+        * FIXME:
+        * - remove the guardband, it's not needed on BXT
+        * - set 19.2MHz bypass frequency if there are no active pipes
+        */
+       if (max_pixclk > 576000*9/10)
+               return 624000;
+       else if (max_pixclk > 384000*9/10)
+               return 576000;
+       else if (max_pixclk > 288000*9/10)
+               return 384000;
+       else if (max_pixclk > 144000*9/10)
+               return 288000;
+       else
+               return 144000;
+}
+
+/* Compute the max pixel clock for new configuration. Uses atomic state if
+ * that's non-NULL, look at current state otherwise. */
+static int intel_mode_max_pixclk(struct drm_device *dev,
+                                struct drm_atomic_state *state)
 {
-       struct drm_device *dev = dev_priv->dev;
        struct intel_crtc *intel_crtc;
+       struct intel_crtc_state *crtc_state;
        int max_pixclk = 0;
 
        for_each_intel_crtc(dev, intel_crtc) {
-               if (intel_crtc->new_enabled)
-                       max_pixclk = max(max_pixclk,
-                                        intel_crtc->new_config->base.adjusted_mode.crtc_clock);
+               if (state)
+                       crtc_state =
+                               intel_atomic_get_crtc_state(state, intel_crtc);
+               else
+                       crtc_state = intel_crtc->config;
+               if (IS_ERR(crtc_state))
+                       return PTR_ERR(crtc_state);
+
+               if (!crtc_state->base.enable)
+                       continue;
+
+               max_pixclk = max(max_pixclk,
+                                crtc_state->base.adjusted_mode.crtc_clock);
        }
 
        return max_pixclk;
 }
 
-static void valleyview_modeset_global_pipes(struct drm_device *dev,
-                                           unsigned *prepare_pipes)
+static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct intel_crtc *intel_crtc;
-       int max_pixclk = intel_mode_max_pixclk(dev_priv);
+       struct drm_i915_private *dev_priv = to_i915(state->dev);
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *crtc_state;
+       int max_pixclk = intel_mode_max_pixclk(state->dev, state);
+       int cdclk, ret = 0;
 
-       if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
-           dev_priv->vlv_cdclk_freq)
-               return;
+       if (max_pixclk < 0)
+               return max_pixclk;
 
-       /* disable/enable all currently active pipes while we change cdclk */
-       for_each_intel_crtc(dev, intel_crtc)
-               if (intel_crtc->base.state->enable)
-                       *prepare_pipes |= (1 << intel_crtc->pipe);
+       if (IS_VALLEYVIEW(dev_priv))
+               cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
+       else
+               cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
+
+       if (cdclk == dev_priv->cdclk_freq)
+               return 0;
+
+       /* add all active pipes to the state */
+       for_each_crtc(state->dev, crtc) {
+               crtc_state = drm_atomic_get_crtc_state(state, crtc);
+               if (IS_ERR(crtc_state))
+                       return PTR_ERR(crtc_state);
+
+               if (!crtc_state->active || needs_modeset(crtc_state))
+                       continue;
+
+               crtc_state->mode_changed = true;
+
+               ret = drm_atomic_add_affected_connectors(state, crtc);
+               if (ret)
+                       break;
+
+               ret = drm_atomic_add_affected_planes(state, crtc);
+               if (ret)
+                       break;
+       }
+
+       return ret;
 }
 
 static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
@@ -5201,7 +5933,7 @@ static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
        else
                default_credits = PFI_CREDIT(8);
 
-       if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
+       if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
                /* CHV suggested value is 31 or 63 */
                if (IS_CHERRYVIEW(dev_priv))
                        credits = PFI_CREDIT_31;
@@ -5228,14 +5960,21 @@ static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
        WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
 }
 
-static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
+static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
 {
-       struct drm_device *dev = state->dev;
+       struct drm_device *dev = old_state->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
-       int max_pixclk = intel_mode_max_pixclk(dev_priv);
-       int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
+       int max_pixclk = intel_mode_max_pixclk(dev, NULL);
+       int req_cdclk;
+
+       /* The path in intel_mode_max_pixclk() with a NULL atomic state should
+        * never fail. */
+       if (WARN_ON(max_pixclk < 0))
+               return;
 
-       if (req_cdclk != dev_priv->vlv_cdclk_freq) {
+       req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
+
+       if (req_cdclk != dev_priv->cdclk_freq) {
                /*
                 * FIXME: We can end up here with all power domains off, yet
                 * with a CDCLK frequency other than the minimum. To account
@@ -5267,9 +6006,7 @@ static void valleyview_crtc_enable(struct drm_crtc *crtc)
        int pipe = intel_crtc->pipe;
        bool is_dsi;
 
-       WARN_ON(!crtc->state->enable);
-
-       if (intel_crtc->active)
+       if (WARN_ON(intel_crtc->active))
                return;
 
        is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
@@ -5326,11 +6063,6 @@ static void valleyview_crtc_enable(struct drm_crtc *crtc)
 
        for_each_encoder_on_crtc(dev, crtc, encoder)
                encoder->enable(encoder);
-
-       intel_crtc_enable_planes(crtc);
-
-       /* Underruns don't raise interrupts, so check manually. */
-       i9xx_check_fifo_underruns(dev_priv);
 }
 
 static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
@@ -5350,9 +6082,7 @@ static void i9xx_crtc_enable(struct drm_crtc *crtc)
        struct intel_encoder *encoder;
        int pipe = intel_crtc->pipe;
 
-       WARN_ON(!crtc->state->enable);
-
-       if (intel_crtc->active)
+       if (WARN_ON(intel_crtc->active))
                return;
 
        i9xx_set_pll_dividers(intel_crtc);
@@ -5387,21 +6117,6 @@ static void i9xx_crtc_enable(struct drm_crtc *crtc)
 
        for_each_encoder_on_crtc(dev, crtc, encoder)
                encoder->enable(encoder);
-
-       intel_crtc_enable_planes(crtc);
-
-       /*
-        * Gen2 reports pipe underruns whenever all planes are disabled.
-        * So don't enable underrun reporting before at least some planes
-        * are enabled.
-        * FIXME: Need to fix the logic to work when we turn off all planes
-        * but leave the pipe running.
-        */
-       if (IS_GEN2(dev))
-               intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
-
-       /* Underruns don't raise interrupts, so check manually. */
-       i9xx_check_fifo_underruns(dev_priv);
 }
 
 static void i9xx_pfit_disable(struct intel_crtc *crtc)
@@ -5427,30 +6142,9 @@ static void i9xx_crtc_disable(struct drm_crtc *crtc)
        struct intel_encoder *encoder;
        int pipe = intel_crtc->pipe;
 
-       if (!intel_crtc->active)
+       if (WARN_ON(!intel_crtc->active))
                return;
 
-       /*
-        * Gen2 reports pipe underruns whenever all planes are disabled.
-        * So diasble underrun reporting before all the planes get disabled.
-        * FIXME: Need to fix the logic to work when we turn off all planes
-        * but leave the pipe running.
-        */
-       if (IS_GEN2(dev))
-               intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
-
-       /*
-        * Vblank time updates from the shadow to live plane control register
-        * are blocked if the memory self-refresh mode is active at that
-        * moment. So to make sure the plane gets truly disabled, disable
-        * first the self-refresh mode. The self-refresh enable bit in turn
-        * will be checked/applied by the HW only at the next frame start
-        * event which is after the vblank start event, so we need to have a
-        * wait-for-vblank between disabling the plane and the pipe.
-        */
-       intel_set_memory_cxsr(dev_priv, false);
-       intel_crtc_disable_planes(crtc);
-
        /*
         * On gen2 planes are double buffered but the pipe isn't, so we must
         * wait for planes to fully turn off before disabling the pipe.
@@ -5493,38 +6187,77 @@ static void i9xx_crtc_disable(struct drm_crtc *crtc)
        mutex_unlock(&dev->struct_mutex);
 }
 
-static void i9xx_crtc_off(struct drm_crtc *crtc)
+/*
+ * turn all crtc's off, but do not adjust state
+ * This has to be paired with a call to intel_modeset_setup_hw_state.
+ */
+void intel_display_suspend(struct drm_device *dev)
 {
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct drm_crtc *crtc;
+
+       for_each_crtc(dev, crtc) {
+               struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+               enum intel_display_power_domain domain;
+               unsigned long domains;
+
+               if (!intel_crtc->active)
+                       continue;
+
+               intel_crtc_disable_planes(crtc);
+               dev_priv->display.crtc_disable(crtc);
+
+               domains = intel_crtc->enabled_power_domains;
+               for_each_power_domain(domain, domains)
+                       intel_display_power_put(dev_priv, domain);
+               intel_crtc->enabled_power_domains = 0;
+       }
 }
 
 /* Master function to enable/disable CRTC and corresponding power wells */
-void intel_crtc_control(struct drm_crtc *crtc, bool enable)
+int intel_crtc_control(struct drm_crtc *crtc, bool enable)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_mode_config *config = &dev->mode_config;
+       struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       enum intel_display_power_domain domain;
-       unsigned long domains;
+       struct intel_crtc_state *pipe_config;
+       struct drm_atomic_state *state;
+       int ret;
 
-       if (enable) {
-               if (!intel_crtc->active) {
-                       domains = get_crtc_power_domains(crtc);
-                       for_each_power_domain(domain, domains)
-                               intel_display_power_get(dev_priv, domain);
-                       intel_crtc->enabled_power_domains = domains;
+       if (enable == intel_crtc->active)
+               return 0;
 
-                       dev_priv->display.crtc_enable(crtc);
-               }
-       } else {
-               if (intel_crtc->active) {
-                       dev_priv->display.crtc_disable(crtc);
+       if (enable && !crtc->state->enable)
+               return 0;
 
-                       domains = intel_crtc->enabled_power_domains;
-                       for_each_power_domain(domain, domains)
-                               intel_display_power_put(dev_priv, domain);
-                       intel_crtc->enabled_power_domains = 0;
-               }
+       /* this function should be called with drm_modeset_lock_all for now */
+       if (WARN_ON(!ctx))
+               return -EIO;
+       lockdep_assert_held(&ctx->ww_ctx);
+
+       state = drm_atomic_state_alloc(dev);
+       if (WARN_ON(!state))
+               return -ENOMEM;
+
+       state->acquire_ctx = ctx;
+       state->allow_modeset = true;
+
+       pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
+       if (IS_ERR(pipe_config)) {
+               ret = PTR_ERR(pipe_config);
+               goto err;
        }
+       pipe_config->base.active = enable;
+
+       ret = intel_set_mode(state);
+       if (!ret)
+               return ret;
+
+err:
+       DRM_ERROR("Updating crtc active failed with %i\n", ret);
+       drm_atomic_state_free(state);
+       return ret;
 }
 
 /**
@@ -5542,33 +6275,6 @@ void intel_crtc_update_dpms(struct drm_crtc *crtc)
        intel_crtc_control(crtc, enable);
 }
 
-static void intel_crtc_disable(struct drm_crtc *crtc)
-{
-       struct drm_device *dev = crtc->dev;
-       struct drm_connector *connector;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-
-       /* crtc should still be enabled when we disable it. */
-       WARN_ON(!crtc->state->enable);
-
-       dev_priv->display.crtc_disable(crtc);
-       dev_priv->display.off(crtc);
-
-       crtc->primary->funcs->disable_plane(crtc->primary);
-
-       /* Update computed state. */
-       list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
-               if (!connector->encoder || !connector->encoder->crtc)
-                       continue;
-
-               if (connector->encoder->crtc != crtc)
-                       continue;
-
-               connector->dpms = DRM_MODE_DPMS_OFF;
-               to_intel_encoder(connector->encoder)->connectors_active = false;
-       }
-}
-
 void intel_encoder_destroy(struct drm_encoder *encoder)
 {
        struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
@@ -5695,65 +6401,80 @@ bool intel_connector_get_hw_state(struct intel_connector *connector)
        return encoder->get_hw_state(encoder, &pipe);
 }
 
-static int pipe_required_fdi_lanes(struct drm_device *dev, enum pipe pipe)
+static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
 {
-       struct intel_crtc *crtc =
-               to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
-
-       if (crtc->base.state->enable &&
-           crtc->config->has_pch_encoder)
-               return crtc->config->fdi_lanes;
+       if (crtc_state->base.enable && crtc_state->has_pch_encoder)
+               return crtc_state->fdi_lanes;
 
        return 0;
 }
 
-static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
+static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
                                     struct intel_crtc_state *pipe_config)
 {
+       struct drm_atomic_state *state = pipe_config->base.state;
+       struct intel_crtc *other_crtc;
+       struct intel_crtc_state *other_crtc_state;
+
        DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
                      pipe_name(pipe), pipe_config->fdi_lanes);
        if (pipe_config->fdi_lanes > 4) {
                DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
                              pipe_name(pipe), pipe_config->fdi_lanes);
-               return false;
+               return -EINVAL;
        }
 
        if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
                if (pipe_config->fdi_lanes > 2) {
                        DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
                                      pipe_config->fdi_lanes);
-                       return false;
+                       return -EINVAL;
                } else {
-                       return true;
+                       return 0;
                }
        }
 
        if (INTEL_INFO(dev)->num_pipes == 2)
-               return true;
+               return 0;
 
        /* Ivybridge 3 pipe is really complicated */
        switch (pipe) {
        case PIPE_A:
-               return true;
+               return 0;
        case PIPE_B:
-               if (pipe_config->fdi_lanes > 2 &&
-                   pipe_required_fdi_lanes(dev, PIPE_C) > 0) {
+               if (pipe_config->fdi_lanes <= 2)
+                       return 0;
+
+               other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
+               other_crtc_state =
+                       intel_atomic_get_crtc_state(state, other_crtc);
+               if (IS_ERR(other_crtc_state))
+                       return PTR_ERR(other_crtc_state);
+
+               if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
                        DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
                                      pipe_name(pipe), pipe_config->fdi_lanes);
-                       return false;
+                       return -EINVAL;
                }
-               return true;
+               return 0;
        case PIPE_C:
                if (pipe_config->fdi_lanes > 2) {
                        DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
                                      pipe_name(pipe), pipe_config->fdi_lanes);
-                       return false;
+                       return -EINVAL;
                }
-               if (pipe_required_fdi_lanes(dev, PIPE_B) > 2) {
+
+               other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
+               other_crtc_state =
+                       intel_atomic_get_crtc_state(state, other_crtc);
+               if (IS_ERR(other_crtc_state))
+                       return PTR_ERR(other_crtc_state);
+
+               if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
                        DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
-                       return false;
+                       return -EINVAL;
                }
-               return true;
+               return 0;
        default:
                BUG();
        }
@@ -5765,8 +6486,8 @@ static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
 {
        struct drm_device *dev = intel_crtc->base.dev;
        struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
-       int lane, link_bw, fdi_dotclock;
-       bool setup_ok, needs_recompute = false;
+       int lane, link_bw, fdi_dotclock, ret;
+       bool needs_recompute = false;
 
 retry:
        /* FDI is a binary signal running at ~2.7GHz, encoding
@@ -5788,9 +6509,9 @@ retry:
        intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
                               link_bw, &pipe_config->fdi_m_n);
 
-       setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
-                                           intel_crtc->pipe, pipe_config);
-       if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
+       ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
+                                      intel_crtc->pipe, pipe_config);
+       if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
                pipe_config->pipe_bpp -= 2*3;
                DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
                              pipe_config->pipe_bpp);
@@ -5803,15 +6524,39 @@ retry:
        if (needs_recompute)
                return RETRY;
 
-       return setup_ok ? 0 : -EINVAL;
+       return ret;
+}
+
+static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
+                                    struct intel_crtc_state *pipe_config)
+{
+       if (pipe_config->pipe_bpp > 24)
+               return false;
+
+       /* HSW can handle pixel rate up to cdclk? */
+       if (IS_HASWELL(dev_priv->dev))
+               return true;
+
+       /*
+        * We compare against max which means we must take
+        * the increased cdclk requirement into account when
+        * calculating the new cdclk.
+        *
+        * Should measure whether using a lower cdclk w/o IPS
+        */
+       return ilk_pipe_pixel_rate(pipe_config) <=
+               dev_priv->max_cdclk_freq * 95 / 100;
 }
 
 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;
+
        pipe_config->ips_enabled = i915.enable_ips &&
-                                  hsw_crtc_supports_ips(crtc) &&
-                                  pipe_config->pipe_bpp <= 24;
+               hsw_crtc_supports_ips(crtc) &&
+               pipe_config_supports_ips(dev_priv, pipe_config);
 }
 
 static int intel_crtc_compute_config(struct intel_crtc *crtc,
@@ -5820,11 +6565,11 @@ static int intel_crtc_compute_config(struct intel_crtc *crtc,
        struct drm_device *dev = crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
+       int ret;
 
        /* FIXME should check pixel clock limits on all platforms */
        if (INTEL_INFO(dev)->gen < 4) {
-               int clock_limit =
-                       dev_priv->display.get_display_clock_speed(dev);
+               int clock_limit = dev_priv->max_cdclk_freq;
 
                /*
                 * Enable pixel doubling when the dot clock
@@ -5860,21 +6605,105 @@ static int intel_crtc_compute_config(struct intel_crtc *crtc,
                adjusted_mode->hsync_start == adjusted_mode->hdisplay)
                return -EINVAL;
 
-       if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
-               pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
-       } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
-               /* only a 8bpc pipe, with 6bpc dither through the panel fitter
-                * for lvds. */
-               pipe_config->pipe_bpp = 8*3;
-       }
-
        if (HAS_IPS(dev))
                hsw_compute_ips_config(crtc, pipe_config);
 
        if (pipe_config->has_pch_encoder)
                return ironlake_fdi_compute_config(crtc, pipe_config);
 
-       return 0;
+       /* FIXME: remove below call once atomic mode set is place and all crtc
+        * related checks called from atomic_crtc_check function */
+       ret = 0;
+       DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
+               crtc, pipe_config->base.state);
+       ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
+
+       return ret;
+}
+
+static int skylake_get_display_clock_speed(struct drm_device *dev)
+{
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
+       uint32_t cdctl = I915_READ(CDCLK_CTL);
+       uint32_t linkrate;
+
+       if (!(lcpll1 & LCPLL_PLL_ENABLE))
+               return 24000; /* 24MHz is the cd freq with NSSC ref */
+
+       if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
+               return 540000;
+
+       linkrate = (I915_READ(DPLL_CTRL1) &
+                   DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
+
+       if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
+           linkrate == DPLL_CTRL1_LINK_RATE_1080) {
+               /* vco 8640 */
+               switch (cdctl & CDCLK_FREQ_SEL_MASK) {
+               case CDCLK_FREQ_450_432:
+                       return 432000;
+               case CDCLK_FREQ_337_308:
+                       return 308570;
+               case CDCLK_FREQ_675_617:
+                       return 617140;
+               default:
+                       WARN(1, "Unknown cd freq selection\n");
+               }
+       } else {
+               /* vco 8100 */
+               switch (cdctl & CDCLK_FREQ_SEL_MASK) {
+               case CDCLK_FREQ_450_432:
+                       return 450000;
+               case CDCLK_FREQ_337_308:
+                       return 337500;
+               case CDCLK_FREQ_675_617:
+                       return 675000;
+               default:
+                       WARN(1, "Unknown cd freq selection\n");
+               }
+       }
+
+       /* error case, do as if DPLL0 isn't enabled */
+       return 24000;
+}
+
+static int broadwell_get_display_clock_speed(struct drm_device *dev)
+{
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       uint32_t lcpll = I915_READ(LCPLL_CTL);
+       uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
+
+       if (lcpll & LCPLL_CD_SOURCE_FCLK)
+               return 800000;
+       else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
+               return 450000;
+       else if (freq == LCPLL_CLK_FREQ_450)
+               return 450000;
+       else if (freq == LCPLL_CLK_FREQ_54O_BDW)
+               return 540000;
+       else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
+               return 337500;
+       else
+               return 675000;
+}
+
+static int haswell_get_display_clock_speed(struct drm_device *dev)
+{
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       uint32_t lcpll = I915_READ(LCPLL_CTL);
+       uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
+
+       if (lcpll & LCPLL_CD_SOURCE_FCLK)
+               return 800000;
+       else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
+               return 450000;
+       else if (freq == LCPLL_CLK_FREQ_450)
+               return 450000;
+       else if (IS_HSW_ULT(dev))
+               return 337500;
+       else
+               return 540000;
 }
 
 static int valleyview_get_display_clock_speed(struct drm_device *dev)
@@ -5886,9 +6715,9 @@ static int valleyview_get_display_clock_speed(struct drm_device *dev)
        if (dev_priv->hpll_freq == 0)
                dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
        val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 
        divider = val & DISPLAY_FREQUENCY_VALUES;
 
@@ -5899,6 +6728,11 @@ static int valleyview_get_display_clock_speed(struct drm_device *dev)
        return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
 }
 
+static int ilk_get_display_clock_speed(struct drm_device *dev)
+{
+       return 450000;
+}
+
 static int i945_get_display_clock_speed(struct drm_device *dev)
 {
        return 400000;
@@ -5906,7 +6740,7 @@ static int i945_get_display_clock_speed(struct drm_device *dev)
 
 static int i915_get_display_clock_speed(struct drm_device *dev)
 {
-       return 333000;
+       return 333333;
 }
 
 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
@@ -5922,19 +6756,19 @@ static int pnv_get_display_clock_speed(struct drm_device *dev)
 
        switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
        case GC_DISPLAY_CLOCK_267_MHZ_PNV:
-               return 267000;
+               return 266667;
        case GC_DISPLAY_CLOCK_333_MHZ_PNV:
-               return 333000;
+               return 333333;
        case GC_DISPLAY_CLOCK_444_MHZ_PNV:
-               return 444000;
+               return 444444;
        case GC_DISPLAY_CLOCK_200_MHZ_PNV:
                return 200000;
        default:
                DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
        case GC_DISPLAY_CLOCK_133_MHZ_PNV:
-               return 133000;
+               return 133333;
        case GC_DISPLAY_CLOCK_167_MHZ_PNV:
-               return 167000;
+               return 166667;
        }
 }
 
@@ -5945,11 +6779,11 @@ static int i915gm_get_display_clock_speed(struct drm_device *dev)
        pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
 
        if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
-               return 133000;
+               return 133333;
        else {
                switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
                case GC_DISPLAY_CLOCK_333_MHZ:
-                       return 333000;
+                       return 333333;
                default:
                case GC_DISPLAY_CLOCK_190_200_MHZ:
                        return 190000;
@@ -5959,23 +6793,40 @@ static int i915gm_get_display_clock_speed(struct drm_device *dev)
 
 static int i865_get_display_clock_speed(struct drm_device *dev)
 {
-       return 266000;
+       return 266667;
 }
 
-static int i855_get_display_clock_speed(struct drm_device *dev)
+static int i85x_get_display_clock_speed(struct drm_device *dev)
 {
        u16 hpllcc = 0;
+
+       /*
+        * 852GM/852GMV only supports 133 MHz and the HPLLCC
+        * encoding is different :(
+        * FIXME is this the right way to detect 852GM/852GMV?
+        */
+       if (dev->pdev->revision == 0x1)
+               return 133333;
+
+       pci_bus_read_config_word(dev->pdev->bus,
+                                PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
+
        /* Assume that the hardware is in the high speed state.  This
         * should be the default.
         */
        switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
        case GC_CLOCK_133_200:
+       case GC_CLOCK_133_200_2:
        case GC_CLOCK_100_200:
                return 200000;
        case GC_CLOCK_166_250:
                return 250000;
        case GC_CLOCK_100_133:
-               return 133000;
+               return 133333;
+       case GC_CLOCK_133_266:
+       case GC_CLOCK_133_266_2:
+       case GC_CLOCK_166_266:
+               return 266667;
        }
 
        /* Shouldn't happen */
@@ -5984,7 +6835,176 @@ static int i855_get_display_clock_speed(struct drm_device *dev)
 
 static int i830_get_display_clock_speed(struct drm_device *dev)
 {
-       return 133000;
+       return 133333;
+}
+
+static unsigned int intel_hpll_vco(struct drm_device *dev)
+{
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       static const unsigned int blb_vco[8] = {
+               [0] = 3200000,
+               [1] = 4000000,
+               [2] = 5333333,
+               [3] = 4800000,
+               [4] = 6400000,
+       };
+       static const unsigned int pnv_vco[8] = {
+               [0] = 3200000,
+               [1] = 4000000,
+               [2] = 5333333,
+               [3] = 4800000,
+               [4] = 2666667,
+       };
+       static const unsigned int cl_vco[8] = {
+               [0] = 3200000,
+               [1] = 4000000,
+               [2] = 5333333,
+               [3] = 6400000,
+               [4] = 3333333,
+               [5] = 3566667,
+               [6] = 4266667,
+       };
+       static const unsigned int elk_vco[8] = {
+               [0] = 3200000,
+               [1] = 4000000,
+               [2] = 5333333,
+               [3] = 4800000,
+       };
+       static const unsigned int ctg_vco[8] = {
+               [0] = 3200000,
+               [1] = 4000000,
+               [2] = 5333333,
+               [3] = 6400000,
+               [4] = 2666667,
+               [5] = 4266667,
+       };
+       const unsigned int *vco_table;
+       unsigned int vco;
+       uint8_t tmp = 0;
+
+       /* FIXME other chipsets? */
+       if (IS_GM45(dev))
+               vco_table = ctg_vco;
+       else if (IS_G4X(dev))
+               vco_table = elk_vco;
+       else if (IS_CRESTLINE(dev))
+               vco_table = cl_vco;
+       else if (IS_PINEVIEW(dev))
+               vco_table = pnv_vco;
+       else if (IS_G33(dev))
+               vco_table = blb_vco;
+       else
+               return 0;
+
+       tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
+
+       vco = vco_table[tmp & 0x7];
+       if (vco == 0)
+               DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
+       else
+               DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
+
+       return vco;
+}
+
+static int gm45_get_display_clock_speed(struct drm_device *dev)
+{
+       unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
+       uint16_t tmp = 0;
+
+       pci_read_config_word(dev->pdev, GCFGC, &tmp);
+
+       cdclk_sel = (tmp >> 12) & 0x1;
+
+       switch (vco) {
+       case 2666667:
+       case 4000000:
+       case 5333333:
+               return cdclk_sel ? 333333 : 222222;
+       case 3200000:
+               return cdclk_sel ? 320000 : 228571;
+       default:
+               DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
+               return 222222;
+       }
+}
+
+static int i965gm_get_display_clock_speed(struct drm_device *dev)
+{
+       static const uint8_t div_3200[] = { 16, 10,  8 };
+       static const uint8_t div_4000[] = { 20, 12, 10 };
+       static const uint8_t div_5333[] = { 24, 16, 14 };
+       const uint8_t *div_table;
+       unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
+       uint16_t tmp = 0;
+
+       pci_read_config_word(dev->pdev, GCFGC, &tmp);
+
+       cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
+
+       if (cdclk_sel >= ARRAY_SIZE(div_3200))
+               goto fail;
+
+       switch (vco) {
+       case 3200000:
+               div_table = div_3200;
+               break;
+       case 4000000:
+               div_table = div_4000;
+               break;
+       case 5333333:
+               div_table = div_5333;
+               break;
+       default:
+               goto fail;
+       }
+
+       return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
+
+ fail:
+       DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
+       return 200000;
+}
+
+static int g33_get_display_clock_speed(struct drm_device *dev)
+{
+       static const uint8_t div_3200[] = { 12, 10,  8,  7, 5, 16 };
+       static const uint8_t div_4000[] = { 14, 12, 10,  8, 6, 20 };
+       static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
+       static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
+       const uint8_t *div_table;
+       unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
+       uint16_t tmp = 0;
+
+       pci_read_config_word(dev->pdev, GCFGC, &tmp);
+
+       cdclk_sel = (tmp >> 4) & 0x7;
+
+       if (cdclk_sel >= ARRAY_SIZE(div_3200))
+               goto fail;
+
+       switch (vco) {
+       case 3200000:
+               div_table = div_3200;
+               break;
+       case 4000000:
+               div_table = div_4000;
+               break;
+       case 4800000:
+               div_table = div_4800;
+               break;
+       case 5333333:
+               div_table = div_5333;
+               break;
+       default:
+               goto fail;
+       }
+
+       return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
+
+ fail:
+       DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
+       return 190476;
 }
 
 static void
@@ -6037,7 +7057,7 @@ static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
 
        WARN_ON(!crtc_state->base.state);
 
-       if (IS_VALLEYVIEW(dev)) {
+       if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
                refclk = 100000;
        } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
            intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
@@ -6225,7 +7245,7 @@ static void vlv_prepare_pll(struct intel_crtc *crtc,
        u32 bestn, bestm1, bestm2, bestp1, bestp2;
        u32 coreclk, reg_val;
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
 
        bestn = pipe_config->dpll.n;
        bestm1 = pipe_config->dpll.m1;
@@ -6303,7 +7323,7 @@ static void vlv_prepare_pll(struct intel_crtc *crtc,
        vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
 
        vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 }
 
 static void chv_update_pll(struct intel_crtc *crtc,
@@ -6348,7 +7368,7 @@ static void chv_prepare_pll(struct intel_crtc *crtc,
        I915_WRITE(dpll_reg,
                   pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
 
        /* p1 and p2 divider */
        vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
@@ -6421,7 +7441,7 @@ static void chv_prepare_pll(struct intel_crtc *crtc,
                        vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
                        DPIO_AFC_RECAL);
 
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 }
 
 /**
@@ -6792,14 +7812,14 @@ static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
        struct intel_encoder *encoder;
        const intel_limit_t *limit;
        struct drm_atomic_state *state = crtc_state->base.state;
+       struct drm_connector *connector;
        struct drm_connector_state *connector_state;
        int i;
 
-       for (i = 0; i < state->num_connector; i++) {
-               if (!state->connectors[i])
-                       continue;
+       memset(&crtc_state->dpll_hw_state, 0,
+              sizeof(crtc_state->dpll_hw_state));
 
-               connector_state = state->connector_states[i];
+       for_each_connector_in_state(state, connector, connector_state, i) {
                if (connector_state->crtc != &crtc->base)
                        continue;
 
@@ -6922,9 +7942,9 @@ static void vlv_crtc_clock_get(struct intel_crtc *crtc,
        if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
                return;
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
        mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 
        clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
        clock.m2 = mdiv & DPIO_M2DIV_MASK;
@@ -7018,12 +8038,12 @@ static void chv_crtc_clock_get(struct intel_crtc *crtc,
        u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
        int refclk = 100000;
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
        cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
        pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
        pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
        pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 
        clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
        clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
@@ -7389,7 +8409,7 @@ static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
                 with_fdi, "LP PCH doesn't have FDI\n"))
                with_fdi = false;
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
 
        tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
        tmp &= ~SBI_SSCCTL_DISABLE;
@@ -7415,7 +8435,7 @@ static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
        tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
        intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
 
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 }
 
 /* Sequence to disable CLKOUT_DP */
@@ -7424,7 +8444,7 @@ static void lpt_disable_clkout_dp(struct drm_device *dev)
        struct drm_i915_private *dev_priv = dev->dev_private;
        uint32_t reg, tmp;
 
-       mutex_lock(&dev_priv->dpio_lock);
+       mutex_lock(&dev_priv->sb_lock);
 
        reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
               SBI_GEN0 : SBI_DBUFF0;
@@ -7443,7 +8463,7 @@ static void lpt_disable_clkout_dp(struct drm_device *dev)
                intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
        }
 
-       mutex_unlock(&dev_priv->dpio_lock);
+       mutex_unlock(&dev_priv->sb_lock);
 }
 
 static void lpt_init_pch_refclk(struct drm_device *dev)
@@ -7483,16 +8503,13 @@ static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
        struct drm_device *dev = crtc_state->base.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;
        int num_connectors = 0, i;
        bool is_lvds = false;
 
-       for (i = 0; i < state->num_connector; i++) {
-               if (!state->connectors[i])
-                       continue;
-
-               connector_state = state->connector_states[i];
+       for_each_connector_in_state(state, connector, connector_state, i) {
                if (connector_state->crtc != crtc_state->base.crtc)
                        continue;
 
@@ -7746,17 +8763,14 @@ static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
        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;
        uint32_t dpll;
        int factor, num_connectors = 0, i;
        bool is_lvds = false, is_sdvo = false;
 
-       for (i = 0; i < state->num_connector; i++) {
-               if (!state->connectors[i])
-                       continue;
-
-               connector_state = state->connector_states[i];
+       for_each_connector_in_state(state, connector, connector_state, i) {
                if (connector_state->crtc != crtc_state->base.crtc)
                        continue;
 
@@ -7846,6 +8860,9 @@ static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
        bool is_lvds = false;
        struct intel_shared_dpll *pll;
 
+       memset(&crtc_state->dpll_hw_state, 0,
+              sizeof(crtc_state->dpll_hw_state));
+
        is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
 
        WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
@@ -7980,14 +8997,28 @@ static void skylake_get_pfit_config(struct intel_crtc *crtc,
 {
        struct drm_device *dev = crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
-       uint32_t tmp;
+       struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
+       uint32_t ps_ctrl = 0;
+       int id = -1;
+       int i;
 
-       tmp = I915_READ(PS_CTL(crtc->pipe));
+       /* find scaler attached to this pipe */
+       for (i = 0; i < crtc->num_scalers; i++) {
+               ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
+               if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
+                       id = i;
+                       pipe_config->pch_pfit.enabled = true;
+                       pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
+                       pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
+                       break;
+               }
+       }
 
-       if (tmp & PS_ENABLE) {
-               pipe_config->pch_pfit.enabled = true;
-               pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
-               pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
+       scaler_state->scaler_id = id;
+       if (id >= 0) {
+               scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
+       } else {
+               scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
        }
 }
 
@@ -8411,6 +9442,7 @@ static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
        }
 
        intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
+       intel_update_cdclk(dev_priv->dev);
 }
 
 /*
@@ -8472,6 +9504,199 @@ void hsw_disable_pc8(struct drm_i915_private *dev_priv)
        intel_prepare_ddi(dev);
 }
 
+static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
+{
+       struct drm_device *dev = old_state->dev;
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       int max_pixclk = intel_mode_max_pixclk(dev, NULL);
+       int req_cdclk;
+
+       /* see the comment in valleyview_modeset_global_resources */
+       if (WARN_ON(max_pixclk < 0))
+               return;
+
+       req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
+
+       if (req_cdclk != dev_priv->cdclk_freq)
+               broxton_set_cdclk(dev, req_cdclk);
+}
+
+/* compute the max rate for new configuration */
+static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
+{
+       struct drm_device *dev = dev_priv->dev;
+       struct intel_crtc *intel_crtc;
+       struct drm_crtc *crtc;
+       int max_pixel_rate = 0;
+       int pixel_rate;
+
+       for_each_crtc(dev, crtc) {
+               if (!crtc->state->enable)
+                       continue;
+
+               intel_crtc = to_intel_crtc(crtc);
+               pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
+
+               /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
+               if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
+                       pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
+
+               max_pixel_rate = max(max_pixel_rate, pixel_rate);
+       }
+
+       return max_pixel_rate;
+}
+
+static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
+{
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       uint32_t val, data;
+       int ret;
+
+       if (WARN((I915_READ(LCPLL_CTL) &
+                 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
+                  LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
+                  LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
+                  LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
+                "trying to change cdclk frequency with cdclk not enabled\n"))
+               return;
+
+       mutex_lock(&dev_priv->rps.hw_lock);
+       ret = sandybridge_pcode_write(dev_priv,
+                                     BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
+       mutex_unlock(&dev_priv->rps.hw_lock);
+       if (ret) {
+               DRM_ERROR("failed to inform pcode about cdclk change\n");
+               return;
+       }
+
+       val = I915_READ(LCPLL_CTL);
+       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))
+               DRM_ERROR("Switching to FCLK failed\n");
+
+       val = I915_READ(LCPLL_CTL);
+       val &= ~LCPLL_CLK_FREQ_MASK;
+
+       switch (cdclk) {
+       case 450000:
+               val |= LCPLL_CLK_FREQ_450;
+               data = 0;
+               break;
+       case 540000:
+               val |= LCPLL_CLK_FREQ_54O_BDW;
+               data = 1;
+               break;
+       case 337500:
+               val |= LCPLL_CLK_FREQ_337_5_BDW;
+               data = 2;
+               break;
+       case 675000:
+               val |= LCPLL_CLK_FREQ_675_BDW;
+               data = 3;
+               break;
+       default:
+               WARN(1, "invalid cdclk frequency\n");
+               return;
+       }
+
+       I915_WRITE(LCPLL_CTL, val);
+
+       val = I915_READ(LCPLL_CTL);
+       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))
+               DRM_ERROR("Switching back to LCPLL failed\n");
+
+       mutex_lock(&dev_priv->rps.hw_lock);
+       sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
+       mutex_unlock(&dev_priv->rps.hw_lock);
+
+       intel_update_cdclk(dev);
+
+       WARN(cdclk != dev_priv->cdclk_freq,
+            "cdclk requested %d kHz but got %d kHz\n",
+            cdclk, dev_priv->cdclk_freq);
+}
+
+static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
+                             int max_pixel_rate)
+{
+       int cdclk;
+
+       /*
+        * FIXME should also account for plane ratio
+        * once 64bpp pixel formats are supported.
+        */
+       if (max_pixel_rate > 540000)
+               cdclk = 675000;
+       else if (max_pixel_rate > 450000)
+               cdclk = 540000;
+       else if (max_pixel_rate > 337500)
+               cdclk = 450000;
+       else
+               cdclk = 337500;
+
+       /*
+        * FIXME move the cdclk caclulation to
+        * compute_config() so we can fail gracegully.
+        */
+       if (cdclk > dev_priv->max_cdclk_freq) {
+               DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
+                         cdclk, dev_priv->max_cdclk_freq);
+               cdclk = dev_priv->max_cdclk_freq;
+       }
+
+       return cdclk;
+}
+
+static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
+{
+       struct drm_i915_private *dev_priv = to_i915(state->dev);
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *crtc_state;
+       int max_pixclk = ilk_max_pixel_rate(dev_priv);
+       int cdclk, i;
+
+       cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
+
+       if (cdclk == dev_priv->cdclk_freq)
+               return 0;
+
+       /* add all active pipes to the state */
+       for_each_crtc(state->dev, crtc) {
+               if (!crtc->state->enable)
+                       continue;
+
+               crtc_state = drm_atomic_get_crtc_state(state, crtc);
+               if (IS_ERR(crtc_state))
+                       return PTR_ERR(crtc_state);
+       }
+
+       /* disable/enable all currently active pipes while we change cdclk */
+       for_each_crtc_in_state(state, crtc, crtc_state, i)
+               if (crtc_state->enable)
+                       crtc_state->mode_changed = true;
+
+       return 0;
+}
+
+static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
+{
+       struct drm_device *dev = state->dev;
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
+       int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
+
+       if (req_cdclk != dev_priv->cdclk_freq)
+               broadwell_set_cdclk(dev, req_cdclk);
+}
+
 static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
                                      struct intel_crtc_state *crtc_state)
 {
@@ -8483,6 +9708,28 @@ static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
        return 0;
 }
 
+static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
+                               enum port port,
+                               struct intel_crtc_state *pipe_config)
+{
+       switch (port) {
+       case PORT_A:
+               pipe_config->ddi_pll_sel = SKL_DPLL0;
+               pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
+               break;
+       case PORT_B:
+               pipe_config->ddi_pll_sel = SKL_DPLL1;
+               pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
+               break;
+       case PORT_C:
+               pipe_config->ddi_pll_sel = SKL_DPLL2;
+               pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
+               break;
+       default:
+               DRM_ERROR("Incorrect port type\n");
+       }
+}
+
 static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
                                enum port port,
                                struct intel_crtc_state *pipe_config)
@@ -8545,6 +9792,8 @@ static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
 
        if (IS_SKYLAKE(dev))
                skylake_get_ddi_pll(dev_priv, port, pipe_config);
+       else if (IS_BROXTON(dev))
+               bxt_get_ddi_pll(dev_priv, port, pipe_config);
        else
                haswell_get_ddi_pll(dev_priv, port, pipe_config);
 
@@ -8621,12 +9870,24 @@ static bool haswell_get_pipe_config(struct intel_crtc *crtc,
 
        intel_get_pipe_timings(crtc, pipe_config);
 
+       if (INTEL_INFO(dev)->gen >= 9) {
+               skl_init_scalers(dev, crtc, pipe_config);
+       }
+
        pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
+
+       if (INTEL_INFO(dev)->gen >= 9) {
+               pipe_config->scaler_state.scaler_id = -1;
+               pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
+       }
+
        if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
-               if (IS_SKYLAKE(dev))
+               if (INTEL_INFO(dev)->gen == 9)
                        skylake_get_pfit_config(crtc, pipe_config);
-               else
+               else if (INTEL_INFO(dev)->gen < 9)
                        ironlake_get_pfit_config(crtc, pipe_config);
+               else
+                       MISSING_CASE(INTEL_INFO(dev)->gen);
        }
 
        if (IS_HASWELL(dev))
@@ -8978,6 +10239,41 @@ mode_fits_in_fbdev(struct drm_device *dev,
 #endif
 }
 
+static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
+                                          struct drm_crtc *crtc,
+                                          struct drm_display_mode *mode,
+                                          struct drm_framebuffer *fb,
+                                          int x, int y)
+{
+       struct drm_plane_state *plane_state;
+       int hdisplay, vdisplay;
+       int ret;
+
+       plane_state = drm_atomic_get_plane_state(state, crtc->primary);
+       if (IS_ERR(plane_state))
+               return PTR_ERR(plane_state);
+
+       if (mode)
+               drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
+       else
+               hdisplay = vdisplay = 0;
+
+       ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
+       if (ret)
+               return ret;
+       drm_atomic_set_fb_for_plane(plane_state, fb);
+       plane_state->crtc_x = 0;
+       plane_state->crtc_y = 0;
+       plane_state->crtc_w = hdisplay;
+       plane_state->crtc_h = vdisplay;
+       plane_state->src_x = x << 16;
+       plane_state->src_y = y << 16;
+       plane_state->src_w = hdisplay << 16;
+       plane_state->src_h = vdisplay << 16;
+
+       return 0;
+}
+
 bool intel_get_load_detect_pipe(struct drm_connector *connector,
                                struct drm_display_mode *mode,
                                struct intel_load_detect_pipe *old,
@@ -8994,6 +10290,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
        struct drm_mode_config *config = &dev->mode_config;
        struct drm_atomic_state *state = NULL;
        struct drm_connector_state *connector_state;
+       struct intel_crtc_state *crtc_state;
        int ret, i = -1;
 
        DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
@@ -9003,7 +10300,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
 retry:
        ret = drm_modeset_lock(&config->connection_mutex, ctx);
        if (ret)
-               goto fail_unlock;
+               goto fail;
 
        /*
         * Algorithm gets a little messy:
@@ -9021,10 +10318,10 @@ retry:
 
                ret = drm_modeset_lock(&crtc->mutex, ctx);
                if (ret)
-                       goto fail_unlock;
+                       goto fail;
                ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
                if (ret)
-                       goto fail_unlock;
+                       goto fail;
 
                old->dpms_mode = connector->dpms;
                old->load_detect_temp = false;
@@ -9043,9 +10340,6 @@ retry:
                        continue;
                if (possible_crtc->state->enable)
                        continue;
-               /* This can occur when applying the pipe A quirk on resume. */
-               if (to_intel_crtc(possible_crtc)->new_enabled)
-                       continue;
 
                crtc = possible_crtc;
                break;
@@ -9056,21 +10350,17 @@ retry:
         */
        if (!crtc) {
                DRM_DEBUG_KMS("no pipe available for load-detect\n");
-               goto fail_unlock;
+               goto fail;
        }
 
        ret = drm_modeset_lock(&crtc->mutex, ctx);
        if (ret)
-               goto fail_unlock;
+               goto fail;
        ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
        if (ret)
-               goto fail_unlock;
-       intel_encoder->new_crtc = to_intel_crtc(crtc);
-       to_intel_connector(connector)->new_encoder = intel_encoder;
+               goto fail;
 
        intel_crtc = to_intel_crtc(crtc);
-       intel_crtc->new_enabled = true;
-       intel_crtc->new_config = intel_crtc->config;
        old->dpms_mode = connector->dpms;
        old->load_detect_temp = true;
        old->release_fb = NULL;
@@ -9090,6 +10380,14 @@ retry:
        connector_state->crtc = crtc;
        connector_state->best_encoder = &intel_encoder->base;
 
+       crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
+       if (IS_ERR(crtc_state)) {
+               ret = PTR_ERR(crtc_state);
+               goto fail;
+       }
+
+       crtc_state->base.active = crtc_state->base.enable = true;
+
        if (!mode)
                mode = &load_detect_mode;
 
@@ -9112,7 +10410,13 @@ retry:
                goto fail;
        }
 
-       if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
+       ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
+       if (ret)
+               goto fail;
+
+       drm_mode_copy(&crtc_state->base.mode, mode);
+
+       if (intel_set_mode(state)) {
                DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
                if (old->release_fb)
                        old->release_fb->funcs->destroy(old->release_fb);
@@ -9124,17 +10428,9 @@ retry:
        intel_wait_for_vblank(dev, intel_crtc->pipe);
        return true;
 
- fail:
-       intel_crtc->new_enabled = crtc->state->enable;
-       if (intel_crtc->new_enabled)
-               intel_crtc->new_config = intel_crtc->config;
-       else
-               intel_crtc->new_config = NULL;
-fail_unlock:
-       if (state) {
-               drm_atomic_state_free(state);
-               state = NULL;
-       }
+fail:
+       drm_atomic_state_free(state);
+       state = NULL;
 
        if (ret == -EDEADLK) {
                drm_modeset_backoff(ctx);
@@ -9156,6 +10452,8 @@ void intel_release_load_detect_pipe(struct drm_connector *connector,
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct drm_atomic_state *state;
        struct drm_connector_state *connector_state;
+       struct intel_crtc_state *crtc_state;
+       int ret;
 
        DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
                      connector->base.id, connector->name,
@@ -9172,17 +10470,23 @@ void intel_release_load_detect_pipe(struct drm_connector *connector,
                if (IS_ERR(connector_state))
                        goto fail;
 
-               to_intel_connector(connector)->new_encoder = NULL;
-               intel_encoder->new_crtc = NULL;
-               intel_crtc->new_enabled = false;
-               intel_crtc->new_config = NULL;
+               crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
+               if (IS_ERR(crtc_state))
+                       goto fail;
 
                connector_state->best_encoder = NULL;
                connector_state->crtc = NULL;
 
-               intel_set_mode(crtc, NULL, 0, 0, NULL, state);
+               crtc_state->base.enable = crtc_state->base.active = false;
 
-               drm_atomic_state_free(state);
+               ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
+                                                     0, 0);
+               if (ret)
+                       goto fail;
+
+               ret = intel_set_mode(state);
+               if (ret)
+                       goto fail;
 
                if (old->release_fb) {
                        drm_framebuffer_unregister_private(old->release_fb);
@@ -9466,14 +10770,6 @@ void intel_mark_idle(struct drm_device *dev)
        intel_runtime_pm_put(dev_priv);
 }
 
-static void intel_crtc_set_state(struct intel_crtc *crtc,
-                                struct intel_crtc_state *crtc_state)
-{
-       kfree(crtc->config);
-       crtc->config = crtc_state;
-       crtc->base.state = &crtc_state->base;
-}
-
 static void intel_crtc_destroy(struct drm_crtc *crtc)
 {
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
@@ -9490,7 +10786,6 @@ static void intel_crtc_destroy(struct drm_crtc *crtc)
                kfree(work);
        }
 
-       intel_crtc_set_state(intel_crtc, NULL);
        drm_crtc_cleanup(crtc);
 
        kfree(intel_crtc);
@@ -9907,7 +11202,7 @@ static bool use_mmio_flip(struct intel_engine_cs *ring,
        else if (i915.enable_execlists)
                return true;
        else
-               return ring != i915_gem_request_get_ring(obj->last_read_req);
+               return ring != i915_gem_request_get_ring(obj->last_write_req);
 }
 
 static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
@@ -9915,23 +11210,34 @@ static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
        struct drm_device *dev = intel_crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
-       struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
-       struct drm_i915_gem_object *obj = intel_fb->obj;
        const enum pipe pipe = intel_crtc->pipe;
        u32 ctl, stride;
 
        ctl = I915_READ(PLANE_CTL(pipe, 0));
        ctl &= ~PLANE_CTL_TILED_MASK;
-       if (obj->tiling_mode == I915_TILING_X)
+       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.
         */
-       stride = fb->pitches[0] >> 6;
-       if (obj->tiling_mode == I915_TILING_X)
-               stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
+       stride = fb->pitches[0] /
+                intel_fb_stride_alignment(dev, fb->modifier[0],
+                                          fb->pixel_format);
 
        /*
         * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
@@ -9996,22 +11302,19 @@ static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
 
 static void intel_mmio_flip_work_func(struct work_struct *work)
 {
-       struct intel_crtc *crtc =
-               container_of(work, struct intel_crtc, mmio_flip.work);
-       struct intel_mmio_flip *mmio_flip;
+       struct intel_mmio_flip *mmio_flip =
+               container_of(work, struct intel_mmio_flip, work);
 
-       mmio_flip = &crtc->mmio_flip;
        if (mmio_flip->req)
                WARN_ON(__i915_wait_request(mmio_flip->req,
-                                           crtc->reset_counter,
-                                           false, NULL, NULL) != 0);
+                                           mmio_flip->crtc->reset_counter,
+                                           false, NULL,
+                                           &mmio_flip->i915->rps.mmioflips));
 
-       intel_do_mmio_flip(crtc);
-       if (mmio_flip->req) {
-               mutex_lock(&crtc->base.dev->struct_mutex);
-               i915_gem_request_assign(&mmio_flip->req, NULL);
-               mutex_unlock(&crtc->base.dev->struct_mutex);
-       }
+       intel_do_mmio_flip(mmio_flip->crtc);
+
+       i915_gem_request_unreference__unlocked(mmio_flip->req);
+       kfree(mmio_flip);
 }
 
 static int intel_queue_mmio_flip(struct drm_device *dev,
@@ -10021,12 +11324,18 @@ static int intel_queue_mmio_flip(struct drm_device *dev,
                                 struct intel_engine_cs *ring,
                                 uint32_t flags)
 {
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct intel_mmio_flip *mmio_flip;
+
+       mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
+       if (mmio_flip == NULL)
+               return -ENOMEM;
 
-       i915_gem_request_assign(&intel_crtc->mmio_flip.req,
-                               obj->last_write_req);
+       mmio_flip->i915 = to_i915(dev);
+       mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
+       mmio_flip->crtc = to_intel_crtc(crtc);
 
-       schedule_work(&intel_crtc->mmio_flip.work);
+       INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
+       schedule_work(&mmio_flip->work);
 
        return 0;
 }
@@ -10085,6 +11394,7 @@ void intel_check_page_flip(struct drm_device *dev, int pipe)
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct intel_unpin_work *work;
 
        WARN_ON(!in_interrupt());
 
@@ -10092,12 +11402,16 @@ void intel_check_page_flip(struct drm_device *dev, int pipe)
                return;
 
        spin_lock(&dev->event_lock);
-       if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
+       work = intel_crtc->unpin_work;
+       if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
                WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
-                        intel_crtc->unpin_work->flip_queued_vblank,
-                        drm_vblank_count(dev, pipe));
+                        work->flip_queued_vblank, drm_vblank_count(dev, pipe));
                page_flip_completed(intel_crtc);
+               work = NULL;
        }
+       if (work != NULL &&
+           drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
+               intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
        spin_unlock(&dev->event_lock);
 }
 
@@ -10115,6 +11429,7 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
        enum pipe pipe = intel_crtc->pipe;
        struct intel_unpin_work *work;
        struct intel_engine_cs *ring;
+       bool mmio_flip;
        int ret;
 
        /*
@@ -10205,22 +11520,30 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
        } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
                ring = &dev_priv->ring[BCS];
        } else if (INTEL_INFO(dev)->gen >= 7) {
-               ring = i915_gem_request_get_ring(obj->last_read_req);
+               ring = i915_gem_request_get_ring(obj->last_write_req);
                if (ring == NULL || ring->id != RCS)
                        ring = &dev_priv->ring[BCS];
        } else {
                ring = &dev_priv->ring[RCS];
        }
 
+       mmio_flip = use_mmio_flip(ring, 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.
+        */
        ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
-                                        crtc->primary->state, ring);
+                                        crtc->primary->state,
+                                        mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
        if (ret)
                goto cleanup_pending;
 
        work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
                                                  + intel_crtc->dspaddr_offset;
 
-       if (use_mmio_flip(ring, obj)) {
+       if (mmio_flip) {
                ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
                                            page_flip_flags);
                if (ret)
@@ -10229,6 +11552,12 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
                i915_gem_request_assign(&work->flip_queued_req,
                                        obj->last_write_req);
        } else {
+               if (obj->last_write_req) {
+                       ret = i915_gem_check_olr(obj->last_write_req);
+                       if (ret)
+                               goto cleanup_unpin;
+               }
+
                ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
                                                   page_flip_flags);
                if (ret)
@@ -10284,91 +11613,32 @@ out_hang:
        return ret;
 }
 
-static struct drm_crtc_helper_funcs intel_helper_funcs = {
+static const struct drm_crtc_helper_funcs intel_helper_funcs = {
        .mode_set_base_atomic = intel_pipe_set_base_atomic,
        .load_lut = intel_crtc_load_lut,
        .atomic_begin = intel_begin_crtc_commit,
        .atomic_flush = intel_finish_crtc_commit,
 };
 
-/**
- * intel_modeset_update_staged_output_state
- *
- * Updates the staged output configuration state, e.g. after we've read out the
- * current hw state.
- */
-static void intel_modeset_update_staged_output_state(struct drm_device *dev)
-{
-       struct intel_crtc *crtc;
-       struct intel_encoder *encoder;
-       struct intel_connector *connector;
-
-       for_each_intel_connector(dev, connector) {
-               connector->new_encoder =
-                       to_intel_encoder(connector->base.encoder);
-       }
-
-       for_each_intel_encoder(dev, encoder) {
-               encoder->new_crtc =
-                       to_intel_crtc(encoder->base.crtc);
-       }
-
-       for_each_intel_crtc(dev, crtc) {
-               crtc->new_enabled = crtc->base.state->enable;
-
-               if (crtc->new_enabled)
-                       crtc->new_config = crtc->config;
-               else
-                       crtc->new_config = NULL;
-       }
-}
-
 /* Transitional helper to copy current connector/encoder state to
  * connector->state. This is needed so that code that is partially
  * converted to atomic does the right thing.
  */
 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
-{
-       struct intel_connector *connector;
-
-       for_each_intel_connector(dev, connector) {
-               if (connector->base.encoder) {
-                       connector->base.state->best_encoder =
-                               connector->base.encoder;
-                       connector->base.state->crtc =
-                               connector->base.encoder->crtc;
-               } else {
-                       connector->base.state->best_encoder = NULL;
-                       connector->base.state->crtc = NULL;
-               }
-       }
-}
-
-/**
- * intel_modeset_commit_output_state
- *
- * This function copies the stage display pipe configuration to the real one.
- */
-static void intel_modeset_commit_output_state(struct drm_device *dev)
-{
-       struct intel_crtc *crtc;
-       struct intel_encoder *encoder;
+{
        struct intel_connector *connector;
 
        for_each_intel_connector(dev, connector) {
-               connector->base.encoder = &connector->new_encoder->base;
-       }
-
-       for_each_intel_encoder(dev, encoder) {
-               encoder->base.crtc = &encoder->new_crtc->base;
-       }
-
-       for_each_intel_crtc(dev, crtc) {
-               crtc->base.state->enable = crtc->new_enabled;
-               crtc->base.enabled = crtc->new_enabled;
+               if (connector->base.encoder) {
+                       connector->base.state->best_encoder =
+                               connector->base.encoder;
+                       connector->base.state->crtc =
+                               connector->base.encoder->crtc;
+               } else {
+                       connector->base.state->best_encoder = NULL;
+                       connector->base.state->crtc = NULL;
+               }
        }
-
-       intel_modeset_update_connector_atomic_state(dev);
 }
 
 static void
@@ -10399,64 +11669,33 @@ connected_sink_compute_bpp(struct intel_connector *connector,
 
 static int
 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
-                         struct drm_framebuffer *fb,
                          struct intel_crtc_state *pipe_config)
 {
        struct drm_device *dev = crtc->base.dev;
        struct drm_atomic_state *state;
-       struct intel_connector *connector;
+       struct drm_connector *connector;
+       struct drm_connector_state *connector_state;
        int bpp, i;
 
-       switch (fb->pixel_format) {
-       case DRM_FORMAT_C8:
-               bpp = 8*3; /* since we go through a colormap */
-               break;
-       case DRM_FORMAT_XRGB1555:
-       case DRM_FORMAT_ARGB1555:
-               /* checked in intel_framebuffer_init already */
-               if (WARN_ON(INTEL_INFO(dev)->gen > 3))
-                       return -EINVAL;
-       case DRM_FORMAT_RGB565:
-               bpp = 6*3; /* min is 18bpp */
-               break;
-       case DRM_FORMAT_XBGR8888:
-       case DRM_FORMAT_ABGR8888:
-               /* checked in intel_framebuffer_init already */
-               if (WARN_ON(INTEL_INFO(dev)->gen < 4))
-                       return -EINVAL;
-       case DRM_FORMAT_XRGB8888:
-       case DRM_FORMAT_ARGB8888:
-               bpp = 8*3;
-               break;
-       case DRM_FORMAT_XRGB2101010:
-       case DRM_FORMAT_ARGB2101010:
-       case DRM_FORMAT_XBGR2101010:
-       case DRM_FORMAT_ABGR2101010:
-               /* checked in intel_framebuffer_init already */
-               if (WARN_ON(INTEL_INFO(dev)->gen < 4))
-                       return -EINVAL;
+       if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
                bpp = 10*3;
-               break;
-       /* TODO: gen4+ supports 16 bpc floating point, too. */
-       default:
-               DRM_DEBUG_KMS("unsupported depth\n");
-               return -EINVAL;
-       }
+       else if (INTEL_INFO(dev)->gen >= 5)
+               bpp = 12*3;
+       else
+               bpp = 8*3;
+
 
        pipe_config->pipe_bpp = bpp;
 
        state = pipe_config->base.state;
 
        /* Clamp display bpp to EDID value */
-       for (i = 0; i < state->num_connector; i++) {
-               if (!state->connectors[i])
-                       continue;
-
-               connector = to_intel_connector(state->connectors[i]);
-               if (state->connector_states[i]->crtc != &crtc->base)
+       for_each_connector_in_state(state, connector, connector_state, i) {
+               if (connector_state->crtc != &crtc->base)
                        continue;
 
-               connected_sink_compute_bpp(connector, pipe_config);
+               connected_sink_compute_bpp(to_intel_connector(connector),
+                                          pipe_config);
        }
 
        return bpp;
@@ -10477,8 +11716,14 @@ static void intel_dump_pipe_config(struct intel_crtc *crtc,
                                   struct intel_crtc_state *pipe_config,
                                   const char *context)
 {
-       DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
-                     context, pipe_name(crtc->pipe));
+       struct drm_device *dev = crtc->base.dev;
+       struct drm_plane *plane;
+       struct intel_plane *intel_plane;
+       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,
+                     context, pipe_config, pipe_name(crtc->pipe));
 
        DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
        DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
@@ -10515,6 +11760,10 @@ static void intel_dump_pipe_config(struct intel_crtc *crtc,
        DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
        DRM_DEBUG_KMS("pipe src size: %dx%d\n",
                      pipe_config->pipe_src_w, pipe_config->pipe_src_h);
+       DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
+                     crtc->num_scalers,
+                     pipe_config->scaler_state.scaler_users,
+                     pipe_config->scaler_state.scaler_id);
        DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
                      pipe_config->gmch_pfit.control,
                      pipe_config->gmch_pfit.pgm_ratios,
@@ -10525,6 +11774,73 @@ static void intel_dump_pipe_config(struct intel_crtc *crtc,
                      pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
        DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
        DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
+
+       if (IS_BROXTON(dev)) {
+               DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
+                             "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
+                             "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
+                             pipe_config->ddi_pll_sel,
+                             pipe_config->dpll_hw_state.ebb0,
+                             pipe_config->dpll_hw_state.pll0,
+                             pipe_config->dpll_hw_state.pll1,
+                             pipe_config->dpll_hw_state.pll2,
+                             pipe_config->dpll_hw_state.pll3,
+                             pipe_config->dpll_hw_state.pll6,
+                             pipe_config->dpll_hw_state.pll8,
+                             pipe_config->dpll_hw_state.pcsdw12);
+       } else if (IS_SKYLAKE(dev)) {
+               DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
+                             "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
+                             pipe_config->ddi_pll_sel,
+                             pipe_config->dpll_hw_state.ctrl1,
+                             pipe_config->dpll_hw_state.cfgcr1,
+                             pipe_config->dpll_hw_state.cfgcr2);
+       } else if (HAS_DDI(dev)) {
+               DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
+                             pipe_config->ddi_pll_sel,
+                             pipe_config->dpll_hw_state.wrpll);
+       } else {
+               DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
+                             "fp0: 0x%x, fp1: 0x%x\n",
+                             pipe_config->dpll_hw_state.dpll,
+                             pipe_config->dpll_hw_state.dpll_md,
+                             pipe_config->dpll_hw_state.fp0,
+                             pipe_config->dpll_hw_state.fp1);
+       }
+
+       DRM_DEBUG_KMS("planes on this crtc\n");
+       list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
+               intel_plane = to_intel_plane(plane);
+               if (intel_plane->pipe != crtc->pipe)
+                       continue;
+
+               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);
+                       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));
+       }
 }
 
 static bool encoders_cloneable(const struct intel_encoder *a,
@@ -10535,16 +11851,21 @@ static bool encoders_cloneable(const struct intel_encoder *a,
                          b->cloneable & (1 << a->type));
 }
 
-static bool check_single_encoder_cloning(struct intel_crtc *crtc,
+static bool check_single_encoder_cloning(struct drm_atomic_state *state,
+                                        struct intel_crtc *crtc,
                                         struct intel_encoder *encoder)
 {
-       struct drm_device *dev = crtc->base.dev;
        struct intel_encoder *source_encoder;
+       struct drm_connector *connector;
+       struct drm_connector_state *connector_state;
+       int i;
 
-       for_each_intel_encoder(dev, source_encoder) {
-               if (source_encoder->new_crtc != crtc)
+       for_each_connector_in_state(state, connector, connector_state, i) {
+               if (connector_state->crtc != &crtc->base)
                        continue;
 
+               source_encoder =
+                       to_intel_encoder(connector_state->best_encoder);
                if (!encoders_cloneable(encoder, source_encoder))
                        return false;
        }
@@ -10552,39 +11873,47 @@ static bool check_single_encoder_cloning(struct intel_crtc *crtc,
        return true;
 }
 
-static bool check_encoder_cloning(struct intel_crtc *crtc)
+static bool check_encoder_cloning(struct drm_atomic_state *state,
+                                 struct intel_crtc *crtc)
 {
-       struct drm_device *dev = crtc->base.dev;
        struct intel_encoder *encoder;
+       struct drm_connector *connector;
+       struct drm_connector_state *connector_state;
+       int i;
 
-       for_each_intel_encoder(dev, encoder) {
-               if (encoder->new_crtc != crtc)
+       for_each_connector_in_state(state, connector, connector_state, i) {
+               if (connector_state->crtc != &crtc->base)
                        continue;
 
-               if (!check_single_encoder_cloning(crtc, encoder))
+               encoder = to_intel_encoder(connector_state->best_encoder);
+               if (!check_single_encoder_cloning(state, crtc, encoder))
                        return false;
        }
 
        return true;
 }
 
-static bool check_digital_port_conflicts(struct drm_device *dev)
+static bool check_digital_port_conflicts(struct drm_atomic_state *state)
 {
-       struct intel_connector *connector;
+       struct drm_device *dev = state->dev;
+       struct intel_encoder *encoder;
+       struct drm_connector *connector;
+       struct drm_connector_state *connector_state;
        unsigned int used_ports = 0;
+       int i;
 
        /*
         * Walk the connector list instead of the encoder
         * list to detect the problem on ddi platforms
         * where there's just one encoder per digital port.
         */
-       for_each_intel_connector(dev, connector) {
-               struct intel_encoder *encoder = connector->new_encoder;
-
-               if (!encoder)
+       for_each_connector_in_state(state, connector, connector_state, i) {
+               if (!connector_state->best_encoder)
                        continue;
 
-               WARN_ON(!encoder->new_crtc);
+               encoder = to_intel_encoder(connector_state->best_encoder);
+
+               WARN_ON(!connector_state->crtc);
 
                switch (encoder->type) {
                        unsigned int port_mask;
@@ -10613,51 +11942,73 @@ static void
 clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
 {
        struct drm_crtc_state tmp_state;
+       struct intel_crtc_scaler_state scaler_state;
+       struct intel_dpll_hw_state dpll_hw_state;
+       enum intel_dpll_id shared_dpll;
+       uint32_t ddi_pll_sel;
+
+       /* FIXME: before the switch to atomic started, a new pipe_config was
+        * kzalloc'd. Code that depends on any field being zero should be
+        * fixed, so that the crtc_state can be safely duplicated. For now,
+        * only fields that are know to not cause problems are preserved. */
 
-       /* Clear only the intel specific part of the crtc state */
        tmp_state = crtc_state->base;
+       scaler_state = crtc_state->scaler_state;
+       shared_dpll = crtc_state->shared_dpll;
+       dpll_hw_state = crtc_state->dpll_hw_state;
+       ddi_pll_sel = crtc_state->ddi_pll_sel;
+
        memset(crtc_state, 0, sizeof *crtc_state);
+
        crtc_state->base = tmp_state;
+       crtc_state->scaler_state = scaler_state;
+       crtc_state->shared_dpll = shared_dpll;
+       crtc_state->dpll_hw_state = dpll_hw_state;
+       crtc_state->ddi_pll_sel = ddi_pll_sel;
 }
 
-static struct intel_crtc_state *
+static int
 intel_modeset_pipe_config(struct drm_crtc *crtc,
-                         struct drm_framebuffer *fb,
-                         struct drm_display_mode *mode,
                          struct drm_atomic_state *state)
 {
-       struct drm_device *dev = crtc->dev;
+       struct drm_crtc_state *crtc_state;
+       struct intel_crtc_state *pipe_config;
        struct intel_encoder *encoder;
-       struct intel_connector *connector;
+       struct drm_connector *connector;
        struct drm_connector_state *connector_state;
-       struct intel_crtc_state *pipe_config;
-       int plane_bpp, ret = -EINVAL;
+       int base_bpp, ret = -EINVAL;
        int i;
        bool retry = true;
 
-       if (!check_encoder_cloning(to_intel_crtc(crtc))) {
+       if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
                DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
-               return ERR_PTR(-EINVAL);
+               return -EINVAL;
        }
 
-       if (!check_digital_port_conflicts(dev)) {
+       if (!check_digital_port_conflicts(state)) {
                DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
-               return ERR_PTR(-EINVAL);
+               return -EINVAL;
        }
 
-       pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
-       if (IS_ERR(pipe_config))
-               return pipe_config;
+       crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
+       if (WARN_ON(!crtc_state))
+               return -EINVAL;
 
-       clear_intel_crtc_state(pipe_config);
+       pipe_config = to_intel_crtc_state(crtc_state);
 
-       pipe_config->base.crtc = crtc;
-       drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
-       drm_mode_copy(&pipe_config->base.mode, mode);
+       /*
+        * XXX: Add all connectors to make the crtc state match the encoders.
+        */
+       if (!needs_modeset(&pipe_config->base)) {
+               ret = drm_atomic_add_affected_connectors(state, crtc);
+               if (ret)
+                       return ret;
+       }
+
+       clear_intel_crtc_state(pipe_config);
 
        pipe_config->cpu_transcoder =
                (enum transcoder) to_intel_crtc(crtc)->pipe;
-       pipe_config->shared_dpll = DPLL_ID_PRIVATE;
 
        /*
         * Sanitize sync polarity flags based on requested ones. If neither
@@ -10676,9 +12027,9 @@ intel_modeset_pipe_config(struct drm_crtc *crtc,
         * plane pixel format and any sink constraints into account. Returns the
         * source plane bpp so that dithering can be selected on mismatches
         * after encoders and crtc also have had their say. */
-       plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
-                                             fb, pipe_config);
-       if (plane_bpp < 0)
+       base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
+                                            pipe_config);
+       if (base_bpp < 0)
                goto fail;
 
        /*
@@ -10706,12 +12057,7 @@ encoder_retry:
         * adjust it according to limitations or connector properties, and also
         * a chance to reject the mode entirely.
         */
-       for (i = 0; i < state->num_connector; i++) {
-               connector = to_intel_connector(state->connectors[i]);
-               if (!connector)
-                       continue;
-
-               connector_state = state->connector_states[i];
+       for_each_connector_in_state(state, connector, connector_state, i) {
                if (connector_state->crtc != crtc)
                        continue;
 
@@ -10746,101 +12092,25 @@ encoder_retry:
                goto encoder_retry;
        }
 
-       pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
+       pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
        DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
-                     plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
-
-       return pipe_config;
-fail:
-       return ERR_PTR(ret);
-}
-
-/* Computes which crtcs are affected and sets the relevant bits in the mask. For
- * simplicity we use the crtc's pipe number (because it's easier to obtain). */
-static void
-intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
-                            unsigned *prepare_pipes, unsigned *disable_pipes)
-{
-       struct intel_crtc *intel_crtc;
-       struct drm_device *dev = crtc->dev;
-       struct intel_encoder *encoder;
-       struct intel_connector *connector;
-       struct drm_crtc *tmp_crtc;
-
-       *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
-
-       /* Check which crtcs have changed outputs connected to them, these need
-        * to be part of the prepare_pipes mask. We don't (yet) support global
-        * modeset across multiple crtcs, so modeset_pipes will only have one
-        * bit set at most. */
-       for_each_intel_connector(dev, connector) {
-               if (connector->base.encoder == &connector->new_encoder->base)
-                       continue;
+                     base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
 
-               if (connector->base.encoder) {
-                       tmp_crtc = connector->base.encoder->crtc;
-
-                       *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
-               }
-
-               if (connector->new_encoder)
-                       *prepare_pipes |=
-                               1 << connector->new_encoder->new_crtc->pipe;
-       }
-
-       for_each_intel_encoder(dev, encoder) {
-               if (encoder->base.crtc == &encoder->new_crtc->base)
-                       continue;
-
-               if (encoder->base.crtc) {
-                       tmp_crtc = encoder->base.crtc;
-
-                       *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
-               }
-
-               if (encoder->new_crtc)
-                       *prepare_pipes |= 1 << encoder->new_crtc->pipe;
-       }
-
-       /* Check for pipes that will be enabled/disabled ... */
-       for_each_intel_crtc(dev, intel_crtc) {
-               if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
-                       continue;
-
-               if (!intel_crtc->new_enabled)
-                       *disable_pipes |= 1 << intel_crtc->pipe;
-               else
-                       *prepare_pipes |= 1 << intel_crtc->pipe;
+       /* Check if we need to force a modeset */
+       if (pipe_config->has_audio !=
+           to_intel_crtc_state(crtc->state)->has_audio) {
+               pipe_config->base.mode_changed = true;
+               ret = drm_atomic_add_affected_planes(state, crtc);
        }
 
-
-       /* set_mode is also used to update properties on life display pipes. */
-       intel_crtc = to_intel_crtc(crtc);
-       if (intel_crtc->new_enabled)
-               *prepare_pipes |= 1 << intel_crtc->pipe;
-
-       /*
-        * For simplicity do a full modeset on any pipe where the output routing
-        * changed. We could be more clever, but that would require us to be
-        * more careful with calling the relevant encoder->mode_set functions.
-        */
-       if (*prepare_pipes)
-               *modeset_pipes = *prepare_pipes;
-
-       /* ... and mask these out. */
-       *modeset_pipes &= ~(*disable_pipes);
-       *prepare_pipes &= ~(*disable_pipes);
-
        /*
-        * HACK: We don't (yet) fully support global modesets. intel_set_config
-        * obies this rule, but the modeset restore mode of
-        * intel_modeset_setup_hw_state does not.
+        * Note we have an issue here with infoframes: current code
+        * only updates them on the full mode set path per hw
+        * requirements.  So here we should be checking for any
+        * required changes and forcing a mode set.
         */
-       *modeset_pipes &= 1 << intel_crtc->pipe;
-       *prepare_pipes &= 1 << intel_crtc->pipe;
-
-       DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
-                     *modeset_pipes, *prepare_pipes, *disable_pipes);
+fail:
+       return ret;
 }
 
 static bool intel_crtc_in_use(struct drm_crtc *crtc)
@@ -10856,55 +12126,64 @@ static bool intel_crtc_in_use(struct drm_crtc *crtc)
 }
 
 static void
-intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
+intel_modeset_update_state(struct drm_atomic_state *state)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_device *dev = state->dev;
        struct intel_encoder *intel_encoder;
-       struct intel_crtc *intel_crtc;
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *crtc_state;
        struct drm_connector *connector;
 
-       intel_shared_dpll_commit(dev_priv);
+       intel_shared_dpll_commit(state);
 
        for_each_intel_encoder(dev, intel_encoder) {
                if (!intel_encoder->base.crtc)
                        continue;
 
-               intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
+               crtc = intel_encoder->base.crtc;
+               crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
+               if (!crtc_state || !needs_modeset(crtc->state))
+                       continue;
 
-               if (prepare_pipes & (1 << intel_crtc->pipe))
-                       intel_encoder->connectors_active = false;
+               intel_encoder->connectors_active = false;
        }
 
-       intel_modeset_commit_output_state(dev);
+       drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
 
        /* Double check state. */
-       for_each_intel_crtc(dev, intel_crtc) {
-               WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
-               WARN_ON(intel_crtc->new_config &&
-                       intel_crtc->new_config != intel_crtc->config);
-               WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
+       for_each_crtc(dev, crtc) {
+               WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
+
+               to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
+
+               /* Update hwmode for vblank functions */
+               if (crtc->state->active)
+                       crtc->hwmode = crtc->state->adjusted_mode;
+               else
+                       crtc->hwmode.crtc_clock = 0;
        }
 
        list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
                if (!connector->encoder || !connector->encoder->crtc)
                        continue;
 
-               intel_crtc = to_intel_crtc(connector->encoder->crtc);
+               crtc = connector->encoder->crtc;
+               crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
+               if (!crtc_state || !needs_modeset(crtc->state))
+                       continue;
 
-               if (prepare_pipes & (1 << intel_crtc->pipe)) {
+               if (crtc->state->active) {
                        struct drm_property *dpms_property =
                                dev->mode_config.dpms_property;
 
                        connector->dpms = DRM_MODE_DPMS_ON;
-                       drm_object_property_set_value(&connector->base,
-                                                        dpms_property,
-                                                        DRM_MODE_DPMS_ON);
+                       drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
 
                        intel_encoder = to_intel_encoder(connector->encoder);
                        intel_encoder->connectors_active = true;
-               }
+               } else
+                       connector->dpms = DRM_MODE_DPMS_OFF;
        }
-
 }
 
 static bool intel_fuzzy_clock_check(int clock1, int clock2)
@@ -11087,6 +12366,8 @@ intel_pipe_config_compare(struct drm_device *dev,
                PIPE_CONF_CHECK_I(pch_pfit.size);
        }
 
+       PIPE_CONF_CHECK_I(scaler_state.scaler_id);
+
        /* BDW+ don't expose a synchronous way to read the state */
        if (IS_HASWELL(dev))
                PIPE_CONF_CHECK_I(ips_enabled);
@@ -11177,11 +12458,14 @@ check_connector_state(struct drm_device *dev)
        struct intel_connector *connector;
 
        for_each_intel_connector(dev, connector) {
+               struct drm_encoder *encoder = connector->base.encoder;
+               struct drm_connector_state *state = connector->base.state;
+
                /* This also checks the encoder/connector hw state with the
                 * ->get_hw_state callbacks. */
                intel_connector_check_state(connector);
 
-               I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
+               I915_STATE_WARN(state->best_encoder != encoder,
                     "connector's staged encoder doesn't match current encoder\n");
        }
 }
@@ -11201,8 +12485,6 @@ check_encoder_state(struct drm_device *dev)
                              encoder->base.base.id,
                              encoder->base.name);
 
-               I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
-                    "encoder's stage crtc doesn't match current crtc\n");
                I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
                     "encoder's active_connectors set, but no crtc\n");
 
@@ -11212,6 +12494,9 @@ check_encoder_state(struct drm_device *dev)
                        enabled = true;
                        if (connector->base.dpms != DRM_MODE_DPMS_OFF)
                                active = true;
+
+                       I915_STATE_WARN(connector->base.state->crtc != encoder->base.crtc,
+                            "encoder's stage crtc doesn't match current crtc\n");
                }
                /*
                 * for MST connectors if we unplug the connector is gone
@@ -11306,6 +12591,10 @@ check_crtc_state(struct drm_device *dev)
                     "crtc active state doesn't match with hw state "
                     "(expected %i, found %i)\n", crtc->active, active);
 
+               I915_STATE_WARN(crtc->active != crtc->base.state->active,
+                    "transitional active state does not match atomic hw state "
+                    "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
+
                if (active &&
                    !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
                        I915_STATE_WARN(1, "pipe state doesn't match!\n");
@@ -11426,117 +12715,125 @@ static void update_scanline_offset(struct intel_crtc *crtc)
                crtc->scanline_offset = 1;
 }
 
-static struct intel_crtc_state *
-intel_modeset_compute_config(struct drm_crtc *crtc,
-                            struct drm_display_mode *mode,
-                            struct drm_framebuffer *fb,
-                            struct drm_atomic_state *state,
-                            unsigned *modeset_pipes,
-                            unsigned *prepare_pipes,
-                            unsigned *disable_pipes)
+static int intel_modeset_setup_plls(struct drm_atomic_state *state)
 {
-       struct drm_device *dev = crtc->dev;
-       struct intel_crtc_state *pipe_config = NULL;
+       struct drm_device *dev = state->dev;
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       unsigned clear_pipes = 0;
        struct intel_crtc *intel_crtc;
+       struct intel_crtc_state *intel_crtc_state;
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *crtc_state;
        int ret = 0;
+       int i;
 
-       ret = drm_atomic_add_affected_connectors(state, crtc);
-       if (ret)
-               return ERR_PTR(ret);
+       if (!dev_priv->display.crtc_compute_clock)
+               return 0;
 
-       intel_modeset_affected_pipes(crtc, modeset_pipes,
-                                    prepare_pipes, disable_pipes);
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               intel_crtc = to_intel_crtc(crtc);
+               intel_crtc_state = to_intel_crtc_state(crtc_state);
 
-       for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
-               pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
-               if (IS_ERR(pipe_config))
-                       return pipe_config;
+               if (needs_modeset(crtc_state)) {
+                       clear_pipes |= 1 << intel_crtc->pipe;
+                       intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
+               }
+       }
 
-               pipe_config->base.enable = false;
+       if (clear_pipes) {
+               struct intel_shared_dpll_config *shared_dpll =
+                       intel_atomic_get_shared_dpll_state(state);
+
+               for (i = 0; i < dev_priv->num_shared_dpll; i++)
+                       shared_dpll[i].crtc_mask &= ~clear_pipes;
        }
 
-       /*
-        * Note this needs changes when we start tracking multiple modes
-        * and crtcs.  At that point we'll need to compute the whole config
-        * (i.e. one pipe_config for each crtc) rather than just the one
-        * for this crtc.
-        */
-       for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
-               /* FIXME: For now we still expect modeset_pipes has at most
-                * one bit set. */
-               if (WARN_ON(&intel_crtc->base != crtc))
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               if (!needs_modeset(crtc_state) || !crtc_state->enable)
                        continue;
 
-               pipe_config = intel_modeset_pipe_config(crtc, fb, mode, state);
-               if (IS_ERR(pipe_config))
-                       return pipe_config;
+               intel_crtc = to_intel_crtc(crtc);
+               intel_crtc_state = to_intel_crtc_state(crtc_state);
 
-               intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
-                                      "[modeset]");
+               ret = dev_priv->display.crtc_compute_clock(intel_crtc,
+                                                          intel_crtc_state);
+               if (ret)
+                       return ret;
        }
 
-       return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
+       return ret;
 }
 
-static int __intel_set_mode_setup_plls(struct drm_device *dev,
-                                      unsigned modeset_pipes,
-                                      unsigned disable_pipes)
+/*
+ * This implements the workaround described in the "notes" section of the mode
+ * set sequence documentation. When going from no pipes or single pipe to
+ * multiple pipes, and planes are enabled after the pipe, we need to wait at
+ * least 2 vblanks on the first pipe before enabling planes on the second pipe.
+ */
+static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
 {
-       struct drm_i915_private *dev_priv = to_i915(dev);
-       unsigned clear_pipes = modeset_pipes | disable_pipes;
+       struct drm_crtc_state *crtc_state;
        struct intel_crtc *intel_crtc;
-       int ret = 0;
+       struct drm_crtc *crtc;
+       struct intel_crtc_state *first_crtc_state = NULL;
+       struct intel_crtc_state *other_crtc_state = NULL;
+       enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
+       int i;
 
-       if (!dev_priv->display.crtc_compute_clock)
-               return 0;
+       /* look at all crtc's that are going to be enabled in during modeset */
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               intel_crtc = to_intel_crtc(crtc);
 
-       ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
-       if (ret)
-               goto done;
+               if (!crtc_state->active || !needs_modeset(crtc_state))
+                       continue;
 
-       for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
-               struct intel_crtc_state *state = intel_crtc->new_config;
-               ret = dev_priv->display.crtc_compute_clock(intel_crtc,
-                                                          state);
-               if (ret) {
-                       intel_shared_dpll_abort_config(dev_priv);
-                       goto done;
+               if (first_crtc_state) {
+                       other_crtc_state = to_intel_crtc_state(crtc_state);
+                       break;
+               } else {
+                       first_crtc_state = to_intel_crtc_state(crtc_state);
+                       first_pipe = intel_crtc->pipe;
                }
        }
 
-done:
-       return ret;
-}
+       /* No workaround needed? */
+       if (!first_crtc_state)
+               return 0;
 
-static int __intel_set_mode(struct drm_crtc *crtc,
-                           struct drm_display_mode *mode,
-                           int x, int y, struct drm_framebuffer *fb,
-                           struct intel_crtc_state *pipe_config,
-                           unsigned modeset_pipes,
-                           unsigned prepare_pipes,
-                           unsigned disable_pipes)
-{
-       struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct drm_display_mode *saved_mode;
-       struct intel_crtc_state *crtc_state_copy = NULL;
-       struct intel_crtc *intel_crtc;
-       int ret = 0;
+       /* w/a possibly needed, check how many crtc's are already enabled. */
+       for_each_intel_crtc(state->dev, intel_crtc) {
+               struct intel_crtc_state *pipe_config;
 
-       saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
-       if (!saved_mode)
-               return -ENOMEM;
+               pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
+               if (IS_ERR(pipe_config))
+                       return PTR_ERR(pipe_config);
+
+               pipe_config->hsw_workaround_pipe = INVALID_PIPE;
+
+               if (!pipe_config->base.active ||
+                   needs_modeset(&pipe_config->base))
+                       continue;
 
-       crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
-       if (!crtc_state_copy) {
-               ret = -ENOMEM;
-               goto done;
+               /* 2 or more enabled crtcs means no need for w/a */
+               if (enabled_pipe != INVALID_PIPE)
+                       return 0;
+
+               enabled_pipe = intel_crtc->pipe;
        }
 
-       *saved_mode = crtc->mode;
+       if (enabled_pipe != INVALID_PIPE)
+               first_crtc_state->hsw_workaround_pipe = enabled_pipe;
+       else if (other_crtc_state)
+               other_crtc_state->hsw_workaround_pipe = first_pipe;
+
+       return 0;
+}
 
-       if (modeset_pipes)
-               to_intel_crtc(crtc)->new_config = pipe_config;
+/* Code that should eventually be part of atomic_check() */
+static int intel_modeset_checks(struct drm_atomic_state *state)
+{
+       struct drm_device *dev = state->dev;
+       int ret;
 
        /*
         * See if the config requires any additional preparation, e.g.
@@ -11545,155 +12842,148 @@ static int __intel_set_mode(struct drm_crtc *crtc,
         * mode set on this crtc.  For other crtcs we need to use the
         * adjusted_mode bits in the crtc directly.
         */
-       if (IS_VALLEYVIEW(dev)) {
-               valleyview_modeset_global_pipes(dev, &prepare_pipes);
+       if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
+               if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
+                       ret = valleyview_modeset_global_pipes(state);
+               else
+                       ret = broadwell_modeset_global_pipes(state);
+
+               if (ret)
+                       return ret;
+       }
+
+       ret = intel_modeset_setup_plls(state);
+       if (ret)
+               return ret;
+
+       if (IS_HASWELL(dev))
+               ret = haswell_mode_set_planes_workaround(state);
+
+       return ret;
+}
+
+static int
+intel_modeset_compute_config(struct drm_atomic_state *state)
+{
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *crtc_state;
+       int ret, i;
+
+       ret = drm_atomic_helper_check_modeset(state->dev, state);
+       if (ret)
+               return ret;
+
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               if (!crtc_state->enable &&
+                   WARN_ON(crtc_state->active))
+                       crtc_state->active = false;
 
-               /* may have added more to prepare_pipes than we should */
-               prepare_pipes &= ~disable_pipes;
+               if (!crtc_state->enable)
+                       continue;
+
+               ret = intel_modeset_pipe_config(crtc, state);
+               if (ret)
+                       return ret;
+
+               intel_dump_pipe_config(to_intel_crtc(crtc),
+                                      to_intel_crtc_state(crtc_state),
+                                      "[modeset]");
        }
 
-       ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
+       ret = intel_modeset_checks(state);
        if (ret)
-               goto done;
+               return ret;
 
-       for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
-               intel_crtc_disable(&intel_crtc->base);
+       return drm_atomic_helper_check_planes(state->dev, state);
+}
 
-       for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
-               if (intel_crtc->base.state->enable)
-                       dev_priv->display.crtc_disable(&intel_crtc->base);
-       }
+static int __intel_set_mode(struct drm_atomic_state *state)
+{
+       struct drm_device *dev = state->dev;
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *crtc_state;
+       int ret = 0;
+       int i;
 
-       /* crtc->mode is already used by the ->mode_set callbacks, hence we need
-        * to set it here already despite that we pass it down the callchain.
-        *
-        * Note we'll need to fix this up when we start tracking multiple
-        * pipes; here we assume a single modeset_pipe and only track the
-        * single crtc and mode.
-        */
-       if (modeset_pipes) {
-               crtc->mode = *mode;
-               /* mode_set/enable/disable functions rely on a correct pipe
-                * config. */
-               intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
+       ret = drm_atomic_helper_prepare_planes(dev, state);
+       if (ret)
+               return ret;
 
-               /*
-                * Calculate and store various constants which
-                * are later needed by vblank and swap-completion
-                * timestamping. They are derived from true hwmode.
-                */
-               drm_calc_timestamping_constants(crtc,
-                                               &pipe_config->base.adjusted_mode);
+       drm_atomic_helper_swap_state(dev, state);
+
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               if (!needs_modeset(crtc->state) || !crtc_state->active)
+                       continue;
+
+               intel_crtc_disable_planes(crtc);
+               dev_priv->display.crtc_disable(crtc);
        }
 
        /* Only after disabling all output pipelines that will be changed can we
         * update the the output configuration. */
-       intel_modeset_update_state(dev, prepare_pipes);
-
-       modeset_update_crtc_power_domains(pipe_config->base.state);
+       intel_modeset_update_state(state);
 
-       /* Set up the DPLL and any encoders state that needs to adjust or depend
-        * on the DPLL.
-        */
-       for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
-               struct drm_plane *primary = intel_crtc->base.primary;
-               int vdisplay, hdisplay;
+       /* The state has been swaped above, so state actually contains the
+        * old state now. */
 
-               drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
-               ret = primary->funcs->update_plane(primary, &intel_crtc->base,
-                                                  fb, 0, 0,
-                                                  hdisplay, vdisplay,
-                                                  x << 16, y << 16,
-                                                  hdisplay << 16, vdisplay << 16);
-       }
+       modeset_update_crtc_power_domains(state);
 
        /* Now enable the clocks, plane, pipe, and connectors that we set up. */
-       for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
-               update_scanline_offset(intel_crtc);
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               drm_atomic_helper_commit_planes_on_crtc(crtc_state);
+
+               if (!needs_modeset(crtc->state) || !crtc->state->active)
+                       continue;
+
+               update_scanline_offset(to_intel_crtc(crtc));
 
-               dev_priv->display.crtc_enable(&intel_crtc->base);
+               dev_priv->display.crtc_enable(crtc);
+               intel_crtc_enable_planes(crtc);
        }
 
        /* FIXME: add subpixel order */
-done:
-       if (ret && crtc->state->enable)
-               crtc->mode = *saved_mode;
-
-       if (ret == 0 && pipe_config) {
-               struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
 
-               /* The pipe_config will be freed with the atomic state, so
-                * make a copy. */
-               memcpy(crtc_state_copy, intel_crtc->config,
-                      sizeof *crtc_state_copy);
-               intel_crtc->config = crtc_state_copy;
-               intel_crtc->base.state = &crtc_state_copy->base;
+       drm_atomic_helper_cleanup_planes(dev, state);
 
-               if (modeset_pipes)
-                       intel_crtc->new_config = intel_crtc->config;
-       } else {
-               kfree(crtc_state_copy);
-       }
+       drm_atomic_state_free(state);
 
-       kfree(saved_mode);
-       return ret;
+       return 0;
 }
 
-static int intel_set_mode_pipes(struct drm_crtc *crtc,
-                               struct drm_display_mode *mode,
-                               int x, int y, struct drm_framebuffer *fb,
-                               struct intel_crtc_state *pipe_config,
-                               unsigned modeset_pipes,
-                               unsigned prepare_pipes,
-                               unsigned disable_pipes)
+static int intel_set_mode_checked(struct drm_atomic_state *state)
 {
+       struct drm_device *dev = state->dev;
        int ret;
 
-       ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
-                              prepare_pipes, disable_pipes);
-
+       ret = __intel_set_mode(state);
        if (ret == 0)
-               intel_modeset_check_state(crtc->dev);
+               intel_modeset_check_state(dev);
 
        return ret;
 }
 
-static int intel_set_mode(struct drm_crtc *crtc,
-                         struct drm_display_mode *mode,
-                         int x, int y, struct drm_framebuffer *fb,
-                         struct drm_atomic_state *state)
+static int intel_set_mode(struct drm_atomic_state *state)
 {
-       struct intel_crtc_state *pipe_config;
-       unsigned modeset_pipes, prepare_pipes, disable_pipes;
-       int ret = 0;
-
-       pipe_config = intel_modeset_compute_config(crtc, mode, fb, state,
-                                                  &modeset_pipes,
-                                                  &prepare_pipes,
-                                                  &disable_pipes);
-
-       if (IS_ERR(pipe_config)) {
-               ret = PTR_ERR(pipe_config);
-               goto out;
-       }
+       int ret;
 
-       ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
-                                  modeset_pipes, prepare_pipes,
-                                  disable_pipes);
+       ret = intel_modeset_compute_config(state);
        if (ret)
-               goto out;
+               return ret;
 
-out:
-       return ret;
+       return intel_set_mode_checked(state);
 }
 
 void intel_crtc_restore_mode(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
        struct drm_atomic_state *state;
+       struct intel_crtc *intel_crtc;
        struct intel_encoder *encoder;
        struct intel_connector *connector;
        struct drm_connector_state *connector_state;
+       struct intel_crtc_state *crtc_state;
+       int ret;
 
        state = drm_atomic_state_alloc(dev);
        if (!state) {
@@ -11710,11 +13000,11 @@ void intel_crtc_restore_mode(struct drm_crtc *crtc)
         * need to copy the staged config to the atomic state, otherwise the
         * mode set will just reapply the state the HW is already in. */
        for_each_intel_encoder(dev, encoder) {
-               if (&encoder->new_crtc->base != crtc)
+               if (encoder->base.crtc != crtc)
                        continue;
 
                for_each_intel_connector(dev, connector) {
-                       if (connector->new_encoder != encoder)
+                       if (connector->base.state->best_encoder != &encoder->base)
                                continue;
 
                        connector_state = drm_atomic_get_connector_state(state, &connector->base);
@@ -11727,190 +13017,55 @@ void intel_crtc_restore_mode(struct drm_crtc *crtc)
                        }
 
                        connector_state->crtc = crtc;
-                       connector_state->best_encoder = &encoder->base;
                }
        }
 
-       intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
-                      state);
-
-       drm_atomic_state_free(state);
-}
-
-#undef for_each_intel_crtc_masked
-
-static void intel_set_config_free(struct intel_set_config *config)
-{
-       if (!config)
-               return;
-
-       kfree(config->save_connector_encoders);
-       kfree(config->save_encoder_crtcs);
-       kfree(config->save_crtc_enabled);
-       kfree(config);
-}
-
-static int intel_set_config_save_state(struct drm_device *dev,
-                                      struct intel_set_config *config)
-{
-       struct drm_crtc *crtc;
-       struct drm_encoder *encoder;
-       struct drm_connector *connector;
-       int count;
-
-       config->save_crtc_enabled =
-               kcalloc(dev->mode_config.num_crtc,
-                       sizeof(bool), GFP_KERNEL);
-       if (!config->save_crtc_enabled)
-               return -ENOMEM;
-
-       config->save_encoder_crtcs =
-               kcalloc(dev->mode_config.num_encoder,
-                       sizeof(struct drm_crtc *), GFP_KERNEL);
-       if (!config->save_encoder_crtcs)
-               return -ENOMEM;
-
-       config->save_connector_encoders =
-               kcalloc(dev->mode_config.num_connector,
-                       sizeof(struct drm_encoder *), GFP_KERNEL);
-       if (!config->save_connector_encoders)
-               return -ENOMEM;
-
-       /* Copy data. Note that driver private data is not affected.
-        * Should anything bad happen only the expected state is
-        * restored, not the drivers personal bookkeeping.
-        */
-       count = 0;
-       for_each_crtc(dev, crtc) {
-               config->save_crtc_enabled[count++] = crtc->state->enable;
-       }
+       for_each_intel_crtc(dev, intel_crtc) {
+               crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
+               if (IS_ERR(crtc_state)) {
+                       DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
+                                     intel_crtc->base.base.id,
+                                     PTR_ERR(crtc_state));
+                       continue;
+               }
 
-       count = 0;
-       list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
-               config->save_encoder_crtcs[count++] = encoder->crtc;
+               if (&intel_crtc->base == crtc)
+                       drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
        }
 
-       count = 0;
-       list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
-               config->save_connector_encoders[count++] = connector->encoder;
-       }
+       intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
+                                       crtc->primary->fb, crtc->x, crtc->y);
 
-       return 0;
+       ret = intel_set_mode(state);
+       if (ret)
+               drm_atomic_state_free(state);
 }
 
-static void intel_set_config_restore_state(struct drm_device *dev,
-                                          struct intel_set_config *config)
-{
-       struct intel_crtc *crtc;
-       struct intel_encoder *encoder;
-       struct intel_connector *connector;
-       int count;
-
-       count = 0;
-       for_each_intel_crtc(dev, crtc) {
-               crtc->new_enabled = config->save_crtc_enabled[count++];
-
-               if (crtc->new_enabled)
-                       crtc->new_config = crtc->config;
-               else
-                       crtc->new_config = NULL;
-       }
-
-       count = 0;
-       for_each_intel_encoder(dev, encoder) {
-               encoder->new_crtc =
-                       to_intel_crtc(config->save_encoder_crtcs[count++]);
-       }
-
-       count = 0;
-       for_each_intel_connector(dev, connector) {
-               connector->new_encoder =
-                       to_intel_encoder(config->save_connector_encoders[count++]);
-       }
-}
+#undef for_each_intel_crtc_masked
 
-static bool
-is_crtc_connector_off(struct drm_mode_set *set)
+static bool intel_connector_in_mode_set(struct intel_connector *connector,
+                                       struct drm_mode_set *set)
 {
-       int i;
-
-       if (set->num_connectors == 0)
-               return false;
-
-       if (WARN_ON(set->connectors == NULL))
-               return false;
+       int ro;
 
-       for (i = 0; i < set->num_connectors; i++)
-               if (set->connectors[i]->encoder &&
-                   set->connectors[i]->encoder->crtc == set->crtc &&
-                   set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
+       for (ro = 0; ro < set->num_connectors; ro++)
+               if (set->connectors[ro] == &connector->base)
                        return true;
 
        return false;
 }
 
-static void
-intel_set_config_compute_mode_changes(struct drm_mode_set *set,
-                                     struct intel_set_config *config)
-{
-
-       /* We should be able to check here if the fb has the same properties
-        * and then just flip_or_move it */
-       if (is_crtc_connector_off(set)) {
-               config->mode_changed = true;
-       } else if (set->crtc->primary->fb != set->fb) {
-               /*
-                * If we have no fb, we can only flip as long as the crtc is
-                * active, otherwise we need a full mode set.  The crtc may
-                * be active if we've only disabled the primary plane, or
-                * in fastboot situations.
-                */
-               if (set->crtc->primary->fb == NULL) {
-                       struct intel_crtc *intel_crtc =
-                               to_intel_crtc(set->crtc);
-
-                       if (intel_crtc->active) {
-                               DRM_DEBUG_KMS("crtc has no fb, will flip\n");
-                               config->fb_changed = true;
-                       } else {
-                               DRM_DEBUG_KMS("inactive crtc, full mode set\n");
-                               config->mode_changed = true;
-                       }
-               } else if (set->fb == NULL) {
-                       config->mode_changed = true;
-               } else if (set->fb->pixel_format !=
-                          set->crtc->primary->fb->pixel_format) {
-                       config->mode_changed = true;
-               } else {
-                       config->fb_changed = true;
-               }
-       }
-
-       if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
-               config->fb_changed = true;
-
-       if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
-               DRM_DEBUG_KMS("modes are different, full mode set\n");
-               drm_mode_debug_printmodeline(&set->crtc->mode);
-               drm_mode_debug_printmodeline(set->mode);
-               config->mode_changed = true;
-       }
-
-       DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
-                       set->crtc->base.id, config->mode_changed, config->fb_changed);
-}
-
 static int
 intel_modeset_stage_output_state(struct drm_device *dev,
                                 struct drm_mode_set *set,
-                                struct intel_set_config *config,
                                 struct drm_atomic_state *state)
 {
        struct intel_connector *connector;
+       struct drm_connector *drm_connector;
        struct drm_connector_state *connector_state;
-       struct intel_encoder *encoder;
-       struct intel_crtc *crtc;
-       int ro;
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *crtc_state;
+       int i, ret;
 
        /* The upper layers ensure that we either disable a crtc or have a list
         * of connectors. For paranoia, double-check this. */
@@ -11918,169 +13073,119 @@ intel_modeset_stage_output_state(struct drm_device *dev,
        WARN_ON(set->fb && (set->num_connectors == 0));
 
        for_each_intel_connector(dev, connector) {
-               /* Otherwise traverse passed in connector list and get encoders
-                * for them. */
-               for (ro = 0; ro < set->num_connectors; ro++) {
-                       if (set->connectors[ro] == &connector->base) {
-                               connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
-                               break;
-                       }
+               bool in_mode_set = intel_connector_in_mode_set(connector, set);
+
+               if (!in_mode_set && connector->base.state->crtc != set->crtc)
+                       continue;
+
+               connector_state =
+                       drm_atomic_get_connector_state(state, &connector->base);
+               if (IS_ERR(connector_state))
+                       return PTR_ERR(connector_state);
+
+               if (in_mode_set) {
+                       int pipe = to_intel_crtc(set->crtc)->pipe;
+                       connector_state->best_encoder =
+                               &intel_find_encoder(connector, pipe)->base;
                }
 
+               if (connector->base.state->crtc != set->crtc)
+                       continue;
+
                /* If we disable the crtc, disable all its connectors. Also, if
                 * the connector is on the changing crtc but not on the new
                 * connector list, disable it. */
-               if ((!set->fb || ro == set->num_connectors) &&
-                   connector->base.encoder &&
-                   connector->base.encoder->crtc == set->crtc) {
-                       connector->new_encoder = NULL;
+               if (!set->fb || !in_mode_set) {
+                       connector_state->best_encoder = NULL;
 
                        DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
                                connector->base.base.id,
                                connector->base.name);
                }
-
-
-               if (&connector->new_encoder->base != connector->base.encoder) {
-                       DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
-                                     connector->base.base.id,
-                                     connector->base.name);
-                       config->mode_changed = true;
-               }
        }
        /* connector->new_encoder is now updated for all connectors. */
 
-       /* Update crtc of enabled connectors. */
-       for_each_intel_connector(dev, connector) {
-               struct drm_crtc *new_crtc;
+       for_each_connector_in_state(state, drm_connector, connector_state, i) {
+               connector = to_intel_connector(drm_connector);
+
+               if (!connector_state->best_encoder) {
+                       ret = drm_atomic_set_crtc_for_connector(connector_state,
+                                                               NULL);
+                       if (ret)
+                               return ret;
 
-               if (!connector->new_encoder)
                        continue;
+               }
 
-               new_crtc = connector->new_encoder->base.crtc;
+               if (intel_connector_in_mode_set(connector, set)) {
+                       struct drm_crtc *crtc = connector->base.state->crtc;
 
-               for (ro = 0; ro < set->num_connectors; ro++) {
-                       if (set->connectors[ro] == &connector->base)
-                               new_crtc = set->crtc;
+                       /* If this connector was in a previous crtc, add it
+                        * to the state. We might need to disable it. */
+                       if (crtc) {
+                               crtc_state =
+                                       drm_atomic_get_crtc_state(state, crtc);
+                               if (IS_ERR(crtc_state))
+                                       return PTR_ERR(crtc_state);
+                       }
+
+                       ret = drm_atomic_set_crtc_for_connector(connector_state,
+                                                               set->crtc);
+                       if (ret)
+                               return ret;
                }
 
                /* Make sure the new CRTC will work with the encoder */
-               if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
-                                        new_crtc)) {
+               if (!drm_encoder_crtc_ok(connector_state->best_encoder,
+                                        connector_state->crtc)) {
                        return -EINVAL;
                }
-               connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
-
-               connector_state =
-                       drm_atomic_get_connector_state(state, &connector->base);
-               if (IS_ERR(connector_state))
-                       return PTR_ERR(connector_state);
-
-               connector_state->crtc = new_crtc;
-               connector_state->best_encoder = &connector->new_encoder->base;
 
                DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
                        connector->base.base.id,
                        connector->base.name,
-                       new_crtc->base.id);
-       }
-
-       /* Check for any encoders that needs to be disabled. */
-       for_each_intel_encoder(dev, encoder) {
-               int num_connectors = 0;
-               for_each_intel_connector(dev, connector) {
-                       if (connector->new_encoder == encoder) {
-                               WARN_ON(!connector->new_encoder->new_crtc);
-                               num_connectors++;
-                       }
-               }
-
-               if (num_connectors == 0)
-                       encoder->new_crtc = NULL;
-               else if (num_connectors > 1)
-                       return -EINVAL;
-
-               /* Only now check for crtc changes so we don't miss encoders
-                * that will be disabled. */
-               if (&encoder->new_crtc->base != encoder->base.crtc) {
-                       DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
-                                     encoder->base.base.id,
-                                     encoder->base.name);
-                       config->mode_changed = true;
-               }
-       }
-       /* Now we've also updated encoder->new_crtc for all encoders. */
-       for_each_intel_connector(dev, connector) {
-               connector_state =
-                       drm_atomic_get_connector_state(state, &connector->base);
-               if (IS_ERR(connector_state))
-                       return PTR_ERR(connector_state);
+                       connector_state->crtc->base.id);
 
-               if (connector->new_encoder) {
-                       if (connector->new_encoder != connector->encoder)
-                               connector->encoder = connector->new_encoder;
-               } else {
-                       connector_state->crtc = NULL;
-               }
+               if (connector_state->best_encoder != &connector->encoder->base)
+                       connector->encoder =
+                               to_intel_encoder(connector_state->best_encoder);
        }
-       for_each_intel_crtc(dev, crtc) {
-               crtc->new_enabled = false;
 
-               for_each_intel_encoder(dev, encoder) {
-                       if (encoder->new_crtc == crtc) {
-                               crtc->new_enabled = true;
-                               break;
-                       }
-               }
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               bool has_connectors;
 
-               if (crtc->new_enabled != crtc->base.state->enable) {
-                       DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
-                                     crtc->base.base.id,
-                                     crtc->new_enabled ? "en" : "dis");
-                       config->mode_changed = true;
-               }
+               ret = drm_atomic_add_affected_connectors(state, crtc);
+               if (ret)
+                       return ret;
 
-               if (crtc->new_enabled)
-                       crtc->new_config = crtc->config;
-               else
-                       crtc->new_config = NULL;
+               has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
+               if (has_connectors != crtc_state->enable)
+                       crtc_state->enable =
+                       crtc_state->active = has_connectors;
        }
 
-       return 0;
-}
-
-static void disable_crtc_nofb(struct intel_crtc *crtc)
-{
-       struct drm_device *dev = crtc->base.dev;
-       struct intel_encoder *encoder;
-       struct intel_connector *connector;
+       ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
+                                             set->fb, set->x, set->y);
+       if (ret)
+               return ret;
 
-       DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
-                     pipe_name(crtc->pipe));
+       crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
+       if (IS_ERR(crtc_state))
+               return PTR_ERR(crtc_state);
 
-       for_each_intel_connector(dev, connector) {
-               if (connector->new_encoder &&
-                   connector->new_encoder->new_crtc == crtc)
-                       connector->new_encoder = NULL;
-       }
+       if (set->mode)
+               drm_mode_copy(&crtc_state->mode, set->mode);
 
-       for_each_intel_encoder(dev, encoder) {
-               if (encoder->new_crtc == crtc)
-                       encoder->new_crtc = NULL;
-       }
+       if (set->num_connectors)
+               crtc_state->active = true;
 
-       crtc->new_enabled = false;
-       crtc->new_config = NULL;
+       return 0;
 }
 
 static int intel_crtc_set_config(struct drm_mode_set *set)
 {
        struct drm_device *dev;
-       struct drm_mode_set save_set;
        struct drm_atomic_state *state = NULL;
-       struct intel_set_config *config;
-       struct intel_crtc_state *pipe_config;
-       unsigned modeset_pipes, prepare_pipes, disable_pipes;
        int ret;
 
        BUG_ON(!set);
@@ -12101,129 +13206,31 @@ static int intel_crtc_set_config(struct drm_mode_set *set)
 
        dev = set->crtc->dev;
 
-       ret = -ENOMEM;
-       config = kzalloc(sizeof(*config), GFP_KERNEL);
-       if (!config)
-               goto out_config;
-
-       ret = intel_set_config_save_state(dev, config);
-       if (ret)
-               goto out_config;
-
-       save_set.crtc = set->crtc;
-       save_set.mode = &set->crtc->mode;
-       save_set.x = set->crtc->x;
-       save_set.y = set->crtc->y;
-       save_set.fb = set->crtc->primary->fb;
-
-       /* Compute whether we need a full modeset, only an fb base update or no
-        * change at all. In the future we might also check whether only the
-        * mode changed, e.g. for LVDS where we only change the panel fitter in
-        * such cases. */
-       intel_set_config_compute_mode_changes(set, config);
-
        state = drm_atomic_state_alloc(dev);
-       if (!state) {
-               ret = -ENOMEM;
-               goto out_config;
-       }
+       if (!state)
+               return -ENOMEM;
 
        state->acquire_ctx = dev->mode_config.acquire_ctx;
 
-       ret = intel_modeset_stage_output_state(dev, set, config, state);
+       ret = intel_modeset_stage_output_state(dev, set, state);
        if (ret)
-               goto fail;
-
-       pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
-                                                  set->fb, state,
-                                                  &modeset_pipes,
-                                                  &prepare_pipes,
-                                                  &disable_pipes);
-       if (IS_ERR(pipe_config)) {
-               ret = PTR_ERR(pipe_config);
-               goto fail;
-       } else if (pipe_config) {
-               if (pipe_config->has_audio !=
-                   to_intel_crtc(set->crtc)->config->has_audio)
-                       config->mode_changed = true;
+               goto out;
 
-               /*
-                * Note we have an issue here with infoframes: current code
-                * only updates them on the full mode set path per hw
-                * requirements.  So here we should be checking for any
-                * required changes and forcing a mode set.
-                */
-       }
+       ret = intel_modeset_compute_config(state);
+       if (ret)
+               goto out;
 
        intel_update_pipe_size(to_intel_crtc(set->crtc));
 
-       if (config->mode_changed) {
-               ret = intel_set_mode_pipes(set->crtc, set->mode,
-                                          set->x, set->y, set->fb, pipe_config,
-                                          modeset_pipes, prepare_pipes,
-                                          disable_pipes);
-       } else if (config->fb_changed) {
-               struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
-               struct drm_plane *primary = set->crtc->primary;
-               int vdisplay, hdisplay;
-
-               drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
-               ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
-                                                  0, 0, hdisplay, vdisplay,
-                                                  set->x << 16, set->y << 16,
-                                                  hdisplay << 16, vdisplay << 16);
-
-               /*
-                * We need to make sure the primary plane is re-enabled if it
-                * has previously been turned off.
-                */
-               if (!intel_crtc->primary_enabled && ret == 0) {
-                       WARN_ON(!intel_crtc->active);
-                       intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
-               }
-
-               /*
-                * In the fastboot case this may be our only check of the
-                * state after boot.  It would be better to only do it on
-                * the first update, but we don't have a nice way of doing that
-                * (and really, set_config isn't used much for high freq page
-                * flipping, so increasing its cost here shouldn't be a big
-                * deal).
-                */
-               if (i915.fastboot && ret == 0)
-                       intel_modeset_check_state(set->crtc->dev);
-       }
-
+       ret = intel_set_mode_checked(state);
        if (ret) {
                DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
                              set->crtc->base.id, ret);
-fail:
-               intel_set_config_restore_state(dev, config);
-
-               drm_atomic_state_clear(state);
-
-               /*
-                * HACK: if the pipe was on, but we didn't have a framebuffer,
-                * force the pipe off to avoid oopsing in the modeset code
-                * due to fb==NULL. This should only happen during boot since
-                * we don't yet reconstruct the FB from the hardware state.
-                */
-               if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
-                       disable_crtc_nofb(to_intel_crtc(save_set.crtc));
-
-               /* Try to restore the config */
-               if (config->mode_changed &&
-                   intel_set_mode(save_set.crtc, save_set.mode,
-                                  save_set.x, save_set.y, save_set.fb,
-                                  state))
-                       DRM_ERROR("failed to restore config after modeset failure\n");
        }
 
-out_config:
-       if (state)
+out:
+       if (ret)
                drm_atomic_state_free(state);
-
-       intel_set_config_free(config);
        return ret;
 }
 
@@ -12326,6 +13333,8 @@ static void intel_shared_dpll_init(struct drm_device *dev)
 {
        struct drm_i915_private *dev_priv = dev->dev_private;
 
+       intel_update_cdclk(dev);
+
        if (HAS_DDI(dev))
                intel_ddi_pll_init(dev);
        else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
@@ -12444,6 +13453,36 @@ intel_cleanup_plane_fb(struct drm_plane *plane,
        }
 }
 
+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)
+               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 = dev_priv->display.get_display_clock_speed(dev);
+
+       if (!crtc_clock || !cdclk)
+               return DRM_PLANE_HELPER_NO_SCALING;
+
+       /*
+        * skl max scale is lower of:
+        *    close to 3 but not 3, -1 is for that purpose
+        *            or
+        *    cdclk/crtc_clock
+        */
+       max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
+
+       return max_scale;
+}
+
 static int
 intel_check_primary_plane(struct drm_plane *plane,
                          struct intel_plane_state *state)
@@ -12452,24 +13491,43 @@ intel_check_primary_plane(struct drm_plane *plane,
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_crtc *crtc = state->base.crtc;
        struct intel_crtc *intel_crtc;
+       struct intel_crtc_state *crtc_state;
        struct drm_framebuffer *fb = state->base.fb;
        struct drm_rect *dest = &state->dst;
        struct drm_rect *src = &state->src;
        const struct drm_rect *clip = &state->clip;
+       bool can_position = false;
+       int max_scale = DRM_PLANE_HELPER_NO_SCALING;
+       int min_scale = DRM_PLANE_HELPER_NO_SCALING;
        int ret;
 
        crtc = crtc ? crtc : plane->crtc;
        intel_crtc = to_intel_crtc(crtc);
+       crtc_state = state->base.state ?
+               intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
+
+       if (INTEL_INFO(dev)->gen >= 9) {
+               /* use scaler when colorkey is not required */
+               if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
+                       min_scale = 1;
+                       max_scale = skl_max_scale(intel_crtc, crtc_state);
+               }
+               can_position = true;
+       }
 
        ret = drm_plane_helper_check_update(plane, crtc, fb,
                                            src, dest, clip,
-                                           DRM_PLANE_HELPER_NO_SCALING,
-                                           DRM_PLANE_HELPER_NO_SCALING,
-                                           false, true, &state->visible);
+                                           min_scale,
+                                           max_scale,
+                                           can_position, true,
+                                           &state->visible);
        if (ret)
                return ret;
 
        if (intel_crtc->active) {
+               struct intel_plane_state *old_state =
+                       to_intel_plane_state(plane->state);
+
                intel_crtc->atomic.wait_for_flips = true;
 
                /*
@@ -12482,23 +13540,30 @@ intel_check_primary_plane(struct drm_plane *plane,
                 * one is done too late. We eventually need to unify
                 * this.
                 */
-               if (intel_crtc->primary_enabled &&
+               if (state->visible &&
                    INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
                    dev_priv->fbc.crtc == intel_crtc &&
                    state->base.rotation != BIT(DRM_ROTATE_0)) {
                        intel_crtc->atomic.disable_fbc = true;
                }
 
-               if (state->visible) {
+               if (state->visible && !old_state->visible) {
                        /*
                         * 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 (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
+                       if (IS_BROADWELL(dev))
                                intel_crtc->atomic.wait_vblank = true;
+
+                       if (crtc_state && !needs_modeset(&crtc_state->base))
+                               intel_crtc->atomic.post_enable_primary = true;
                }
 
+               if (!state->visible && old_state->visible &&
+                   crtc_state && !needs_modeset(&crtc_state->base))
+                       intel_crtc->atomic.pre_disable_primary = true;
+
                intel_crtc->atomic.fb_bits |=
                        INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
 
@@ -12508,6 +13573,13 @@ intel_check_primary_plane(struct drm_plane *plane,
                        intel_crtc->atomic.update_wm = true;
        }
 
+       if (INTEL_INFO(dev)->gen >= 9) {
+               ret = skl_update_scaler_users(intel_crtc, crtc_state,
+                       to_intel_plane(plane), state, 0);
+               if (ret)
+                       return ret;
+       }
+
        return 0;
 }
 
@@ -12530,32 +13602,32 @@ intel_commit_primary_plane(struct drm_plane *plane,
        crtc->y = src->y1 >> 16;
 
        if (intel_crtc->active) {
-               if (state->visible) {
+               if (state->visible)
                        /* FIXME: kill this fastboot hack */
                        intel_update_pipe_size(intel_crtc);
 
-                       intel_crtc->primary_enabled = true;
-
-                       dev_priv->display.update_primary_plane(crtc, plane->fb,
-                                       crtc->x, crtc->y);
-               } else {
-                       /*
-                        * If clipping results in a non-visible primary plane,
-                        * we'll disable the primary plane.  Note that this is
-                        * a bit different than what happens if userspace
-                        * explicitly disables the plane by passing fb=0
-                        * because plane->fb still gets set and pinned.
-                        */
-                       intel_disable_primary_hw_plane(plane, crtc);
-               }
+               dev_priv->display.update_primary_plane(crtc, plane->fb,
+                                                      crtc->x, crtc->y);
        }
 }
 
+static void
+intel_disable_primary_plane(struct drm_plane *plane,
+                           struct drm_crtc *crtc,
+                           bool force)
+{
+       struct drm_device *dev = plane->dev;
+       struct drm_i915_private *dev_priv = dev->dev_private;
+
+       dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
+}
+
 static void intel_begin_crtc_commit(struct drm_crtc *crtc)
 {
        struct drm_device *dev = crtc->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct drm_crtc_state *crtc_state = intel_crtc->base.state;
        struct intel_plane *intel_plane;
        struct drm_plane *p;
        unsigned fb_bits = 0;
@@ -12599,7 +13671,7 @@ static void intel_begin_crtc_commit(struct drm_crtc *crtc)
        intel_runtime_pm_get(dev_priv);
 
        /* Perform vblank evasion around commit operation */
-       if (intel_crtc->active)
+       if (crtc_state->active && !needs_modeset(crtc_state))
                intel_crtc->atomic.evade =
                        intel_pipe_update_start(intel_crtc,
                                                &intel_crtc->atomic.start_vbl_count);
@@ -12618,7 +13690,7 @@ static void intel_finish_crtc_commit(struct drm_crtc *crtc)
 
        intel_runtime_pm_put(dev_priv);
 
-       if (intel_crtc->atomic.wait_vblank)
+       if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
                intel_wait_for_vblank(dev, intel_crtc->pipe);
 
        intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
@@ -12655,8 +13727,8 @@ void intel_plane_destroy(struct drm_plane *plane)
 }
 
 const struct drm_plane_funcs intel_plane_funcs = {
-       .update_plane = drm_plane_helper_update,
-       .disable_plane = drm_plane_helper_disable,
+       .update_plane = drm_atomic_helper_update_plane,
+       .disable_plane = drm_atomic_helper_disable_plane,
        .destroy = intel_plane_destroy,
        .set_property = drm_atomic_helper_plane_set_property,
        .atomic_get_property = intel_plane_atomic_get_property,
@@ -12687,19 +13759,28 @@ static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
 
        primary->can_scale = false;
        primary->max_downscale = 1;
+       if (INTEL_INFO(dev)->gen >= 9) {
+               primary->can_scale = true;
+               state->scaler_id = -1;
+       }
        primary->pipe = pipe;
        primary->plane = pipe;
        primary->check_plane = intel_check_primary_plane;
        primary->commit_plane = intel_commit_primary_plane;
+       primary->disable_plane = intel_disable_primary_plane;
+       primary->ckey.flags = I915_SET_COLORKEY_NONE;
        if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
                primary->plane = !pipe;
 
-       if (INTEL_INFO(dev)->gen <= 3) {
-               intel_primary_formats = intel_primary_formats_gen2;
-               num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
+       if (INTEL_INFO(dev)->gen >= 9) {
+               intel_primary_formats = skl_primary_formats;
+               num_formats = ARRAY_SIZE(skl_primary_formats);
+       } else if (INTEL_INFO(dev)->gen >= 4) {
+               intel_primary_formats = i965_primary_formats;
+               num_formats = ARRAY_SIZE(i965_primary_formats);
        } else {
-               intel_primary_formats = intel_primary_formats_gen4;
-               num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
+               intel_primary_formats = i8xx_primary_formats;
+               num_formats = ARRAY_SIZE(i8xx_primary_formats);
        }
 
        drm_universal_plane_init(dev, &primary->base, 0,
@@ -12707,23 +13788,32 @@ static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
                                 intel_primary_formats, num_formats,
                                 DRM_PLANE_TYPE_PRIMARY);
 
-       if (INTEL_INFO(dev)->gen >= 4) {
-               if (!dev->mode_config.rotation_property)
-                       dev->mode_config.rotation_property =
-                               drm_mode_create_rotation_property(dev,
-                                                       BIT(DRM_ROTATE_0) |
-                                                       BIT(DRM_ROTATE_180));
-               if (dev->mode_config.rotation_property)
-                       drm_object_attach_property(&primary->base.base,
-                               dev->mode_config.rotation_property,
-                               state->base.rotation);
-       }
+       if (INTEL_INFO(dev)->gen >= 4)
+               intel_create_rotation_property(dev, primary);
 
        drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
 
        return &primary->base;
 }
 
+void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
+{
+       if (!dev->mode_config.rotation_property) {
+               unsigned long flags = BIT(DRM_ROTATE_0) |
+                       BIT(DRM_ROTATE_180);
+
+               if (INTEL_INFO(dev)->gen >= 9)
+                       flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
+
+               dev->mode_config.rotation_property =
+                       drm_mode_create_rotation_property(dev, flags);
+       }
+       if (dev->mode_config.rotation_property)
+               drm_object_attach_property(&plane->base.base,
+                               dev->mode_config.rotation_property,
+                               plane->base.state->rotation);
+}
+
 static int
 intel_check_cursor_plane(struct drm_plane *plane,
                         struct intel_plane_state *state)
@@ -12785,6 +13875,22 @@ finish:
        return ret;
 }
 
+static void
+intel_disable_cursor_plane(struct drm_plane *plane,
+                          struct drm_crtc *crtc,
+                          bool force)
+{
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+
+       if (!force) {
+               plane->fb = NULL;
+               intel_crtc->cursor_bo = NULL;
+               intel_crtc->cursor_addr = 0;
+       }
+
+       intel_crtc_update_cursor(crtc, false);
+}
+
 static void
 intel_commit_cursor_plane(struct drm_plane *plane,
                          struct intel_plane_state *state)
@@ -12843,6 +13949,7 @@ static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
        cursor->plane = pipe;
        cursor->check_plane = intel_check_cursor_plane;
        cursor->commit_plane = intel_commit_cursor_plane;
+       cursor->disable_plane = intel_disable_cursor_plane;
 
        drm_universal_plane_init(dev, &cursor->base, 0,
                                 &intel_plane_funcs,
@@ -12862,11 +13969,32 @@ static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
                                state->base.rotation);
        }
 
+       if (INTEL_INFO(dev)->gen >=9)
+               state->scaler_id = -1;
+
        drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
 
        return &cursor->base;
 }
 
+static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
+       struct intel_crtc_state *crtc_state)
+{
+       int i;
+       struct intel_scaler *intel_scaler;
+       struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
+
+       for (i = 0; i < intel_crtc->num_scalers; i++) {
+               intel_scaler = &scaler_state->scalers[i];
+               intel_scaler->in_use = 0;
+               intel_scaler->id = i;
+
+               intel_scaler->mode = PS_SCALER_MODE_DYN;
+       }
+
+       scaler_state->scaler_id = -1;
+}
+
 static void intel_crtc_init(struct drm_device *dev, int pipe)
 {
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -12883,9 +14011,20 @@ static void intel_crtc_init(struct drm_device *dev, int pipe)
        crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
        if (!crtc_state)
                goto fail;
-       intel_crtc_set_state(intel_crtc, crtc_state);
+       intel_crtc->config = crtc_state;
+       intel_crtc->base.state = &crtc_state->base;
        crtc_state->base.crtc = &intel_crtc->base;
 
+       /* initialize shared scalers */
+       if (INTEL_INFO(dev)->gen >= 9) {
+               if (pipe == PIPE_C)
+                       intel_crtc->num_scalers = 1;
+               else
+                       intel_crtc->num_scalers = SKL_NUM_SCALERS;
+
+               skl_init_scalers(dev, intel_crtc, crtc_state);
+       }
+
        primary = intel_primary_plane_create(dev, pipe);
        if (!primary)
                goto fail;
@@ -12926,8 +14065,6 @@ static void intel_crtc_init(struct drm_device *dev, int pipe)
        dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
        dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
 
-       INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
-
        drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
 
        WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
@@ -13038,7 +14175,16 @@ static void intel_setup_outputs(struct drm_device *dev)
        if (intel_crt_present(dev))
                intel_crt_init(dev);
 
-       if (HAS_DDI(dev)) {
+       if (IS_BROXTON(dev)) {
+               /*
+                * FIXME: Broxton doesn't support port detection via the
+                * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
+                * detect the ports.
+                */
+               intel_ddi_init(dev, PORT_A);
+               intel_ddi_init(dev, PORT_B);
+               intel_ddi_init(dev, PORT_C);
+       } else if (HAS_DDI(dev)) {
                int found;
 
                /*
@@ -13315,25 +14461,35 @@ static int intel_framebuffer_init(struct drm_device *dev,
        case DRM_FORMAT_ARGB8888:
                break;
        case DRM_FORMAT_XRGB1555:
-       case DRM_FORMAT_ARGB1555:
                if (INTEL_INFO(dev)->gen > 3) {
                        DRM_DEBUG("unsupported pixel format: %s\n",
                                  drm_get_format_name(mode_cmd->pixel_format));
                        return -EINVAL;
                }
                break;
-       case DRM_FORMAT_XBGR8888:
        case DRM_FORMAT_ABGR8888:
+               if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
+                       DRM_DEBUG("unsupported pixel format: %s\n",
+                                 drm_get_format_name(mode_cmd->pixel_format));
+                       return -EINVAL;
+               }
+               break;
+       case DRM_FORMAT_XBGR8888:
        case DRM_FORMAT_XRGB2101010:
-       case DRM_FORMAT_ARGB2101010:
        case DRM_FORMAT_XBGR2101010:
-       case DRM_FORMAT_ABGR2101010:
                if (INTEL_INFO(dev)->gen < 4) {
                        DRM_DEBUG("unsupported pixel format: %s\n",
                                  drm_get_format_name(mode_cmd->pixel_format));
                        return -EINVAL;
                }
                break;
+       case DRM_FORMAT_ABGR2101010:
+               if (!IS_VALLEYVIEW(dev)) {
+                       DRM_DEBUG("unsupported pixel format: %s\n",
+                                 drm_get_format_name(mode_cmd->pixel_format));
+                       return -EINVAL;
+               }
+               break;
        case DRM_FORMAT_YUYV:
        case DRM_FORMAT_UYVY:
        case DRM_FORMAT_YVYU:
@@ -13400,6 +14556,8 @@ static const struct drm_mode_config_funcs intel_mode_funcs = {
        .output_poll_changed = intel_fbdev_output_poll_changed,
        .atomic_check = intel_atomic_check,
        .atomic_commit = intel_atomic_commit,
+       .atomic_state_alloc = intel_atomic_state_alloc,
+       .atomic_state_clear = intel_atomic_state_clear,
 };
 
 /* Set up chip specific display functions */
@@ -13426,7 +14584,6 @@ static void intel_init_display(struct drm_device *dev)
                        haswell_crtc_compute_clock;
                dev_priv->display.crtc_enable = haswell_crtc_enable;
                dev_priv->display.crtc_disable = haswell_crtc_disable;
-               dev_priv->display.off = ironlake_crtc_off;
                dev_priv->display.update_primary_plane =
                        skylake_update_primary_plane;
        } else if (HAS_DDI(dev)) {
@@ -13437,7 +14594,6 @@ static void intel_init_display(struct drm_device *dev)
                        haswell_crtc_compute_clock;
                dev_priv->display.crtc_enable = haswell_crtc_enable;
                dev_priv->display.crtc_disable = haswell_crtc_disable;
-               dev_priv->display.off = ironlake_crtc_off;
                dev_priv->display.update_primary_plane =
                        ironlake_update_primary_plane;
        } else if (HAS_PCH_SPLIT(dev)) {
@@ -13448,7 +14604,6 @@ static void intel_init_display(struct drm_device *dev)
                        ironlake_crtc_compute_clock;
                dev_priv->display.crtc_enable = ironlake_crtc_enable;
                dev_priv->display.crtc_disable = ironlake_crtc_disable;
-               dev_priv->display.off = ironlake_crtc_off;
                dev_priv->display.update_primary_plane =
                        ironlake_update_primary_plane;
        } else if (IS_VALLEYVIEW(dev)) {
@@ -13458,7 +14613,6 @@ static void intel_init_display(struct drm_device *dev)
                dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
                dev_priv->display.crtc_enable = valleyview_crtc_enable;
                dev_priv->display.crtc_disable = i9xx_crtc_disable;
-               dev_priv->display.off = i9xx_crtc_off;
                dev_priv->display.update_primary_plane =
                        i9xx_update_primary_plane;
        } else {
@@ -13468,18 +14622,42 @@ static void intel_init_display(struct drm_device *dev)
                dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
                dev_priv->display.crtc_enable = i9xx_crtc_enable;
                dev_priv->display.crtc_disable = i9xx_crtc_disable;
-               dev_priv->display.off = i9xx_crtc_off;
                dev_priv->display.update_primary_plane =
                        i9xx_update_primary_plane;
        }
 
        /* Returns the core display clock speed */
-       if (IS_VALLEYVIEW(dev))
+       if (IS_SKYLAKE(dev))
+               dev_priv->display.get_display_clock_speed =
+                       skylake_get_display_clock_speed;
+       else if (IS_BROADWELL(dev))
+               dev_priv->display.get_display_clock_speed =
+                       broadwell_get_display_clock_speed;
+       else if (IS_HASWELL(dev))
+               dev_priv->display.get_display_clock_speed =
+                       haswell_get_display_clock_speed;
+       else if (IS_VALLEYVIEW(dev))
                dev_priv->display.get_display_clock_speed =
                        valleyview_get_display_clock_speed;
-       else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
+       else if (IS_GEN5(dev))
+               dev_priv->display.get_display_clock_speed =
+                       ilk_get_display_clock_speed;
+       else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
+                IS_GEN6(dev) || IS_IVYBRIDGE(dev))
                dev_priv->display.get_display_clock_speed =
                        i945_get_display_clock_speed;
+       else if (IS_GM45(dev))
+               dev_priv->display.get_display_clock_speed =
+                       gm45_get_display_clock_speed;
+       else if (IS_CRESTLINE(dev))
+               dev_priv->display.get_display_clock_speed =
+                       i965gm_get_display_clock_speed;
+       else if (IS_PINEVIEW(dev))
+               dev_priv->display.get_display_clock_speed =
+                       pnv_get_display_clock_speed;
+       else if (IS_G33(dev) || IS_G4X(dev))
+               dev_priv->display.get_display_clock_speed =
+                       g33_get_display_clock_speed;
        else if (IS_I915G(dev))
                dev_priv->display.get_display_clock_speed =
                        i915_get_display_clock_speed;
@@ -13497,10 +14675,12 @@ static void intel_init_display(struct drm_device *dev)
                        i865_get_display_clock_speed;
        else if (IS_I85X(dev))
                dev_priv->display.get_display_clock_speed =
-                       i855_get_display_clock_speed;
-       else /* 852, 830 */
+                       i85x_get_display_clock_speed;
+       else { /* 830 */
+               WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
                dev_priv->display.get_display_clock_speed =
                        i830_get_display_clock_speed;
+       }
 
        if (IS_GEN5(dev)) {
                dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
@@ -13511,9 +14691,15 @@ static void intel_init_display(struct drm_device *dev)
                dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
        } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
                dev_priv->display.fdi_link_train = hsw_fdi_link_train;
+               if (IS_BROADWELL(dev))
+                       dev_priv->display.modeset_global_resources =
+                               broadwell_modeset_global_resources;
        } else if (IS_VALLEYVIEW(dev)) {
                dev_priv->display.modeset_global_resources =
                        valleyview_modeset_global_resources;
+       } else if (IS_BROXTON(dev)) {
+               dev_priv->display.modeset_global_resources =
+                       broxton_modeset_global_resources;
        }
 
        switch (INTEL_INFO(dev)->gen) {
@@ -13732,13 +14918,9 @@ static void i915_disable_vga(struct drm_device *dev)
 
 void intel_modeset_init_hw(struct drm_device *dev)
 {
+       intel_update_cdclk(dev);
        intel_prepare_ddi(dev);
-
-       if (IS_VALLEYVIEW(dev))
-               vlv_update_cdclk(dev);
-
        intel_init_clock_gating(dev);
-
        intel_enable_gt_powersave(dev);
 }
 
@@ -13922,9 +15104,10 @@ static void intel_sanitize_crtc(struct intel_crtc *crtc)
                 * Temporarily change the plane mapping and disable everything
                 * ...  */
                plane = crtc->plane;
+               to_intel_plane_state(crtc->base.primary->state)->visible = true;
+               crtc->base.primary->crtc = &crtc->base;
                crtc->plane = !plane;
-               crtc->primary_enabled = true;
-               dev_priv->display.crtc_disable(&crtc->base);
+               intel_crtc_control(&crtc->base, false);
                crtc->plane = plane;
 
                /* ... and break all links. */
@@ -13945,6 +15128,7 @@ static void intel_sanitize_crtc(struct intel_crtc *crtc)
 
                WARN_ON(crtc->active);
                crtc->base.state->enable = false;
+               crtc->base.state->active = false;
                crtc->base.enabled = false;
        }
 
@@ -13961,7 +15145,7 @@ static void intel_sanitize_crtc(struct intel_crtc *crtc)
         * have active connectors/encoders. */
        intel_crtc_update_dpms(&crtc->base);
 
-       if (crtc->active != crtc->base.state->enable) {
+       if (crtc->active != crtc->base.state->active) {
                struct intel_encoder *encoder;
 
                /* This can happen either due to bugs in the get_hw_state
@@ -13973,6 +15157,7 @@ static void intel_sanitize_crtc(struct intel_crtc *crtc)
                              crtc->active ? "enabled" : "disabled");
 
                crtc->base.state->enable = crtc->active;
+               crtc->base.state->active = crtc->active;
                crtc->base.enabled = crtc->active;
 
                /* Because we only establish the connector -> encoder ->
@@ -14084,92 +15269,225 @@ static bool primary_get_hw_state(struct intel_crtc *crtc)
 {
        struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
 
-       if (!crtc->active)
+       if (!crtc->base.enabled)
                return false;
 
        return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
 }
 
-static void intel_modeset_readout_hw_state(struct drm_device *dev)
+static int readout_hw_crtc_state(struct drm_atomic_state *state,
+                                struct intel_crtc *crtc)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       enum pipe pipe;
-       struct intel_crtc *crtc;
-       struct intel_encoder *encoder;
-       struct intel_connector *connector;
-       int i;
+       struct drm_i915_private *dev_priv = to_i915(state->dev);
+       struct intel_crtc_state *crtc_state;
+       struct drm_plane *primary = crtc->base.primary;
+       struct drm_plane_state *drm_plane_state;
+       struct intel_plane_state *plane_state;
+       int ret;
 
-       for_each_intel_crtc(dev, crtc) {
-               memset(crtc->config, 0, sizeof(*crtc->config));
+       crtc_state = intel_atomic_get_crtc_state(state, crtc);
+       if (IS_ERR(crtc_state))
+               return PTR_ERR(crtc_state);
 
-               crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
+       ret = drm_atomic_add_affected_planes(state, &crtc->base);
+       if (ret)
+               return ret;
 
-               crtc->active = dev_priv->display.get_pipe_config(crtc,
-                                                                crtc->config);
+       memset(crtc_state, 0, sizeof(*crtc_state));
+       crtc_state->base.crtc = &crtc->base;
+       crtc_state->base.state = state;
 
-               crtc->base.state->enable = crtc->active;
-               crtc->base.enabled = crtc->active;
-               crtc->primary_enabled = primary_get_hw_state(crtc);
+       crtc_state->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
 
-               DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
-                             crtc->base.base.id,
-                             crtc->active ? "enabled" : "disabled");
-       }
+       crtc_state->base.enable = crtc_state->base.active =
+       crtc->base.enabled = dev_priv->display.get_pipe_config(crtc, crtc_state);
+
+       /* update transitional state */
+       crtc->active = crtc_state->base.active;
+       crtc->config = crtc_state;
+
+       drm_plane_state = drm_atomic_get_plane_state(state, primary);
+       if (IS_ERR(drm_plane_state))
+               return PTR_ERR(drm_plane_state);
+
+       plane_state = to_intel_plane_state(drm_plane_state);
+       plane_state->visible = primary_get_hw_state(crtc);
+
+       if (plane_state->visible) {
+               primary->crtc = &crtc->base;
+               crtc_state->base.plane_mask |= 1 << drm_plane_index(primary);
+       } else
+               crtc_state->base.plane_mask &= ~(1 << drm_plane_index(primary));
+
+       DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
+                     crtc->base.base.id,
+                     crtc_state->base.active ? "enabled" : "disabled");
 
+       return 0;
+}
+
+static int readout_hw_pll_state(struct drm_atomic_state *state)
+{
+       struct drm_i915_private *dev_priv = to_i915(state->dev);
+       struct intel_shared_dpll_config *shared_dpll;
+       struct intel_crtc *crtc;
+       struct intel_crtc_state *crtc_state;
+       int i;
+
+       shared_dpll = intel_atomic_get_shared_dpll_state(state);
        for (i = 0; i < dev_priv->num_shared_dpll; i++) {
                struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
 
                pll->on = pll->get_hw_state(dev_priv, pll,
-                                           &pll->config.hw_state);
+                                           &shared_dpll[i].hw_state);
+
                pll->active = 0;
-               pll->config.crtc_mask = 0;
-               for_each_intel_crtc(dev, crtc) {
-                       if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
+               shared_dpll[i].crtc_mask = 0;
+
+               for_each_intel_crtc(state->dev, crtc) {
+                       crtc_state = intel_atomic_get_crtc_state(state, crtc);
+                       if (IS_ERR(crtc_state))
+                               return PTR_ERR(crtc_state);
+
+                       if (crtc_state->base.active &&
+                           crtc_state->shared_dpll == i) {
                                pll->active++;
-                               pll->config.crtc_mask |= 1 << crtc->pipe;
+                               shared_dpll[i].crtc_mask |=
+                                       1 << crtc->pipe;
                        }
                }
 
                DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
-                             pll->name, pll->config.crtc_mask, pll->on);
+                             pll->name, shared_dpll[i].crtc_mask,
+                             pll->on);
 
-               if (pll->config.crtc_mask)
+               if (shared_dpll[i].crtc_mask)
                        intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
        }
 
-       for_each_intel_encoder(dev, encoder) {
-               pipe = 0;
+       return 0;
+}
 
-               if (encoder->get_hw_state(encoder, &pipe)) {
-                       crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
-                       encoder->base.crtc = &crtc->base;
-                       encoder->get_config(encoder, crtc->config);
-               } else {
-                       encoder->base.crtc = NULL;
-               }
+static struct drm_connector_state *
+get_connector_state_for_encoder(struct drm_atomic_state *state,
+                               struct intel_encoder *encoder)
+{
+       struct drm_connector *connector;
+       struct drm_connector_state *connector_state;
+       int i;
 
-               encoder->connectors_active = false;
-               DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
-                             encoder->base.base.id,
-                             encoder->base.name,
-                             encoder->base.crtc ? "enabled" : "disabled",
-                             pipe_name(pipe));
-       }
+       for_each_connector_in_state(state, connector, connector_state, i)
+               if (connector_state->best_encoder == &encoder->base)
+                       return connector_state;
+
+       return NULL;
+}
+
+static int readout_hw_connector_encoder_state(struct drm_atomic_state *state)
+{
+       struct drm_device *dev = state->dev;
+       struct drm_i915_private *dev_priv = to_i915(state->dev);
+       struct intel_crtc *crtc;
+       struct drm_crtc_state *drm_crtc_state;
+       struct intel_crtc_state *crtc_state;
+       struct intel_encoder *encoder;
+       struct intel_connector *connector;
+       struct drm_connector_state *connector_state;
+       enum pipe pipe;
 
        for_each_intel_connector(dev, connector) {
+               connector_state =
+                       drm_atomic_get_connector_state(state, &connector->base);
+               if (IS_ERR(connector_state))
+                       return PTR_ERR(connector_state);
+
                if (connector->get_hw_state(connector)) {
                        connector->base.dpms = DRM_MODE_DPMS_ON;
-                       connector->encoder->connectors_active = true;
                        connector->base.encoder = &connector->encoder->base;
                } else {
                        connector->base.dpms = DRM_MODE_DPMS_OFF;
                        connector->base.encoder = NULL;
                }
+
+               /* We'll update the crtc field when reading encoder state */
+               connector_state->crtc = NULL;
+
+               connector_state->best_encoder = connector->base.encoder;
+
                DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
                              connector->base.base.id,
                              connector->base.name,
                              connector->base.encoder ? "enabled" : "disabled");
        }
+
+       for_each_intel_encoder(dev, encoder) {
+               pipe = 0;
+
+               connector_state =
+                       get_connector_state_for_encoder(state, encoder);
+
+               encoder->connectors_active = !!connector_state;
+
+               if (encoder->get_hw_state(encoder, &pipe)) {
+                       encoder->base.crtc =
+                               dev_priv->pipe_to_crtc_mapping[pipe];
+                       crtc = to_intel_crtc(encoder->base.crtc);
+
+                       drm_crtc_state =
+                               state->crtc_states[drm_crtc_index(&crtc->base)];
+                       crtc_state = to_intel_crtc_state(drm_crtc_state);
+
+                       encoder->get_config(encoder, crtc_state);
+
+                       if (connector_state)
+                               connector_state->crtc = &crtc->base;
+               } else {
+                       encoder->base.crtc = NULL;
+               }
+
+               DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
+                             encoder->base.base.id,
+                             encoder->base.name,
+                             encoder->base.crtc ? "enabled" : "disabled",
+                             pipe_name(pipe));
+       }
+
+       return 0;
+}
+
+static struct drm_atomic_state *
+intel_modeset_readout_hw_state(struct drm_device *dev)
+{
+       struct intel_crtc *crtc;
+       int ret = 0;
+
+       struct drm_atomic_state *state;
+
+       state = drm_atomic_state_alloc(dev);
+       if (!state)
+               return ERR_PTR(-ENOMEM);
+
+       state->acquire_ctx = dev->mode_config.acquire_ctx;
+
+       for_each_intel_crtc(dev, crtc) {
+               ret = readout_hw_crtc_state(state, crtc);
+               if (ret)
+                       goto err_free;
+       }
+
+       ret = readout_hw_pll_state(state);
+       if (ret)
+               goto err_free;
+
+       ret = readout_hw_connector_encoder_state(state);
+       if (ret)
+               goto err_free;
+
+       return state;
+
+err_free:
+       drm_atomic_state_free(state);
+       return ERR_PTR(ret);
 }
 
 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
@@ -14178,37 +15496,57 @@ void intel_modeset_setup_hw_state(struct drm_device *dev,
                                  bool force_restore)
 {
        struct drm_i915_private *dev_priv = dev->dev_private;
-       enum pipe pipe;
-       struct intel_crtc *crtc;
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *crtc_state;
        struct intel_encoder *encoder;
+       struct drm_atomic_state *state;
+       struct intel_shared_dpll_config shared_dplls[I915_NUM_PLLS];
        int i;
 
-       intel_modeset_readout_hw_state(dev);
-
-       /*
-        * Now that we have the config, copy it to each CRTC struct
-        * Note that this could go away if we move to using crtc_config
-        * checking everywhere.
-        */
-       for_each_intel_crtc(dev, crtc) {
-               if (crtc->active && i915.fastboot) {
-                       intel_mode_from_pipe_config(&crtc->base.mode,
-                                                   crtc->config);
-                       DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
-                                     crtc->base.base.id);
-                       drm_mode_debug_printmodeline(&crtc->base.mode);
-               }
+       state = intel_modeset_readout_hw_state(dev);
+       if (IS_ERR(state)) {
+               DRM_ERROR("Failed to read out hw state\n");
+               return;
        }
 
+       drm_atomic_helper_swap_state(dev, state);
+
+       /* swap sw/hw dpll state */
+       intel_atomic_duplicate_dpll_state(dev_priv, shared_dplls);
+       intel_shared_dpll_commit(state);
+       memcpy(to_intel_atomic_state(state)->shared_dpll,
+              shared_dplls, sizeof(*shared_dplls) * dev_priv->num_shared_dpll);
+
        /* HW state is read out, now we need to sanitize this mess. */
        for_each_intel_encoder(dev, encoder) {
                intel_sanitize_encoder(encoder);
        }
 
-       for_each_pipe(dev_priv, pipe) {
-               crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
-               intel_sanitize_crtc(crtc);
-               intel_dump_pipe_config(crtc, crtc->config,
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+
+               /* prevent unnneeded restores with force_restore */
+               crtc_state->active_changed =
+               crtc_state->mode_changed =
+               crtc_state->planes_changed = false;
+
+               if (crtc->enabled) {
+                       intel_mode_from_pipe_config(&crtc->state->mode,
+                               to_intel_crtc_state(crtc->state));
+
+                       drm_mode_copy(&crtc->mode, &crtc->state->mode);
+                       drm_mode_copy(&crtc->hwmode,
+                                     &crtc->state->adjusted_mode);
+               }
+
+               intel_sanitize_crtc(intel_crtc);
+
+               /*
+                * sanitize_crtc may have forced an update of crtc->state,
+                * so reload in intel_dump_pipe_config
+                */
+               intel_dump_pipe_config(intel_crtc,
+                                      to_intel_crtc_state(crtc->state),
                                       "[setup_hw_state]");
        }
 
@@ -14232,20 +15570,17 @@ void intel_modeset_setup_hw_state(struct drm_device *dev,
                ilk_wm_get_hw_state(dev);
 
        if (force_restore) {
-               i915_redisable_vga(dev);
+               int ret;
 
-               /*
-                * We need to use raw interfaces for restoring state to avoid
-                * checking (bogus) intermediate states.
-                */
-               for_each_pipe(dev_priv, pipe) {
-                       struct drm_crtc *crtc =
-                               dev_priv->pipe_to_crtc_mapping[pipe];
+               i915_redisable_vga(dev);
 
-                       intel_crtc_restore_mode(crtc);
+               ret = intel_set_mode(state);
+               if (ret) {
+                       DRM_ERROR("Failed to restore previous mode\n");
+                       drm_atomic_state_free(state);
                }
        } else {
-               intel_modeset_update_staged_output_state(dev);
+               drm_atomic_state_free(state);
        }
 
        intel_modeset_check_state(dev);
@@ -14297,7 +15632,9 @@ void intel_modeset_gem_init(struct drm_device *dev)
                                  to_intel_crtc(c)->pipe);
                        drm_framebuffer_unreference(c->primary->fb);
                        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));
                }
        }
 
This page took 0.110644 seconds and 5 git commands to generate.