drm/i915: Read out hrawclk from CCK on vlv/chv
[cascardo/linux.git] / drivers / gpu / drm / i915 / intel_display.c
index a66220a..62d36a7 100644 (file)
@@ -169,49 +169,62 @@ static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
        return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
 }
 
-int
-intel_pch_rawclk(struct drm_device *dev)
+static int
+intel_pch_rawclk(struct drm_i915_private *dev_priv)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
-
-       WARN_ON(!HAS_PCH_SPLIT(dev));
+       return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
+}
 
-       return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
+static int
+intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
+{
+       return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
+                                     CCK_DISPLAY_REF_CLOCK_CONTROL);
 }
 
-/* hrawclock is 1/4 the FSB frequency */
-int intel_hrawclk(struct drm_device *dev)
+static int
+intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
        uint32_t clkcfg;
 
-       /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
-       if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
-               return 200;
-
+       /* hrawclock is 1/4 the FSB frequency */
        clkcfg = I915_READ(CLKCFG);
        switch (clkcfg & CLKCFG_FSB_MASK) {
        case CLKCFG_FSB_400:
-               return 100;
+               return 100000;
        case CLKCFG_FSB_533:
-               return 133;
+               return 133333;
        case CLKCFG_FSB_667:
-               return 166;
+               return 166667;
        case CLKCFG_FSB_800:
-               return 200;
+               return 200000;
        case CLKCFG_FSB_1067:
-               return 266;
+               return 266667;
        case CLKCFG_FSB_1333:
-               return 333;
+               return 333333;
        /* these two are just a guess; one of them might be right */
        case CLKCFG_FSB_1600:
        case CLKCFG_FSB_1600_ALT:
-               return 400;
+               return 400000;
        default:
-               return 133;
+               return 133333;
        }
 }
 
+static void intel_update_rawclk(struct drm_i915_private *dev_priv)
+{
+       if (HAS_PCH_SPLIT(dev_priv))
+               dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
+       else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
+               dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
+       else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
+               dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
+       else
+               return; /* no rawclk on other platforms, or no need to know it */
+
+       DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
+}
+
 static void intel_update_czclk(struct drm_i915_private *dev_priv)
 {
        if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
@@ -224,13 +237,15 @@ static void intel_update_czclk(struct drm_i915_private *dev_priv)
 }
 
 static inline u32 /* units of 100MHz */
-intel_fdi_link_freq(struct drm_device *dev)
+intel_fdi_link_freq(struct drm_i915_private *dev_priv,
+                   const struct intel_crtc_state *pipe_config)
 {
-       if (IS_GEN5(dev)) {
-               struct drm_i915_private *dev_priv = dev->dev_private;
-               return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
-       } else
-               return 27;
+       if (HAS_DDI(dev_priv))
+               return pipe_config->port_clock; /* SPLL */
+       else if (IS_GEN5(dev_priv))
+               return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
+       else
+               return 270000;
 }
 
 static const intel_limit_t intel_limits_i8xx_dac = {
@@ -1343,18 +1358,21 @@ void assert_pipe(struct drm_i915_private *dev_priv,
        bool cur_state;
        enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
                                                                      pipe);
+       enum intel_display_power_domain power_domain;
 
        /* if we need the pipe quirk it must be always on */
        if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
            (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
                state = true;
 
-       if (!intel_display_power_is_enabled(dev_priv,
-                               POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
-               cur_state = false;
-       } else {
+       power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
+       if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
                u32 val = I915_READ(PIPECONF(cpu_transcoder));
                cur_state = !!(val & PIPECONF_ENABLE);
+
+               intel_display_power_put(dev_priv, power_domain);
+       } else {
+               cur_state = false;
        }
 
        I915_STATE_WARN(cur_state != state,
@@ -2222,8 +2240,8 @@ static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
        return IS_GEN2(dev_priv) ? 2048 : 4096;
 }
 
-static unsigned int intel_tile_width(const struct drm_i915_private *dev_priv,
-                                    uint64_t fb_modifier, unsigned int cpp)
+static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
+                                          uint64_t fb_modifier, unsigned int cpp)
 {
        switch (fb_modifier) {
        case DRM_FORMAT_MOD_NONE:
@@ -2266,7 +2284,21 @@ unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
                return 1;
        else
                return intel_tile_size(dev_priv) /
-                       intel_tile_width(dev_priv, fb_modifier, cpp);
+                       intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
+}
+
+/* Return the tile dimensions in pixel units */
+static void intel_tile_dims(const struct drm_i915_private *dev_priv,
+                           unsigned int *tile_width,
+                           unsigned int *tile_height,
+                           uint64_t fb_modifier,
+                           unsigned int cpp)
+{
+       unsigned int tile_width_bytes =
+               intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
+
+       *tile_width = tile_width_bytes / cpp;
+       *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
 }
 
 unsigned int
@@ -2279,48 +2311,54 @@ intel_fb_align_height(struct drm_device *dev, unsigned int height,
        return ALIGN(height, tile_height);
 }
 
-static void
-intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
-                       const struct drm_plane_state *plane_state)
+unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
 {
-       struct drm_i915_private *dev_priv = to_i915(fb->dev);
-       struct intel_rotation_info *info = &view->params.rotated;
-       unsigned int tile_size, tile_width, tile_height, cpp;
-
-       *view = i915_ggtt_view_normal;
+       unsigned int size = 0;
+       int i;
 
-       if (!plane_state)
-               return;
+       for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
+               size += rot_info->plane[i].width * rot_info->plane[i].height;
 
-       if (!intel_rotation_90_or_270(plane_state->rotation))
-               return;
+       return size;
+}
 
-       *view = i915_ggtt_view_rotated;
+static void
+intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
+                       const struct drm_framebuffer *fb,
+                       unsigned int rotation)
+{
+       if (intel_rotation_90_or_270(rotation)) {
+               *view = i915_ggtt_view_rotated;
+               view->params.rotated = to_intel_framebuffer(fb)->rot_info;
+       } else {
+               *view = i915_ggtt_view_normal;
+       }
+}
 
-       info->height = fb->height;
-       info->pixel_format = fb->pixel_format;
-       info->pitch = fb->pitches[0];
-       info->uv_offset = fb->offsets[1];
-       info->fb_modifier = fb->modifier[0];
+static void
+intel_fill_fb_info(struct drm_i915_private *dev_priv,
+                  struct drm_framebuffer *fb)
+{
+       struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
+       unsigned int tile_size, tile_width, tile_height, cpp;
 
        tile_size = intel_tile_size(dev_priv);
 
        cpp = drm_format_plane_cpp(fb->pixel_format, 0);
-       tile_width = intel_tile_width(dev_priv, fb->modifier[0], cpp);
-       tile_height = tile_size / tile_width;
+       intel_tile_dims(dev_priv, &tile_width, &tile_height,
+                       fb->modifier[0], cpp);
 
-       info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_width);
-       info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
-       info->size = info->width_pages * info->height_pages * tile_size;
+       info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
+       info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
 
        if (info->pixel_format == DRM_FORMAT_NV12) {
                cpp = drm_format_plane_cpp(fb->pixel_format, 1);
-               tile_width = intel_tile_width(dev_priv, fb->modifier[1], cpp);
-               tile_height = tile_size / tile_width;
+               intel_tile_dims(dev_priv, &tile_width, &tile_height,
+                               fb->modifier[1], cpp);
 
-               info->width_pages_uv = DIV_ROUND_UP(fb->pitches[1], tile_width);
-               info->height_pages_uv = DIV_ROUND_UP(fb->height / 2, tile_height);
-               info->size_uv = info->width_pages_uv * info->height_pages_uv * tile_size;
+               info->uv_offset = fb->offsets[1];
+               info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
+               info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
        }
 }
 
@@ -2357,9 +2395,8 @@ static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv
 }
 
 int
-intel_pin_and_fence_fb_obj(struct drm_plane *plane,
-                          struct drm_framebuffer *fb,
-                          const struct drm_plane_state *plane_state)
+intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
+                          unsigned int rotation)
 {
        struct drm_device *dev = fb->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -2372,7 +2409,7 @@ intel_pin_and_fence_fb_obj(struct drm_plane *plane,
 
        alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
 
-       intel_fill_fb_ggtt_view(&view, fb, plane_state);
+       intel_fill_fb_ggtt_view(&view, fb, rotation);
 
        /* Note that the w/a also requires 64 PTE of padding following the
         * bo. We currently fill all unused PTE with the shadow page and so
@@ -2430,15 +2467,14 @@ err_pm:
        return ret;
 }
 
-static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
-                              const struct drm_plane_state *plane_state)
+static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
 {
        struct drm_i915_gem_object *obj = intel_fb_obj(fb);
        struct i915_ggtt_view view;
 
        WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
 
-       intel_fill_fb_ggtt_view(&view, fb, plane_state);
+       intel_fill_fb_ggtt_view(&view, fb, rotation);
 
        if (view.type == I915_GGTT_VIEW_NORMAL)
                i915_gem_object_unpin_fence(obj);
@@ -2446,38 +2482,93 @@ static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
        i915_gem_object_unpin_from_display_plane(obj, &view);
 }
 
-/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
- * is assumed to be a power-of-two. */
-u32 intel_compute_tile_offset(struct drm_i915_private *dev_priv,
-                             int *x, int *y,
-                             uint64_t fb_modifier,
-                             unsigned int cpp,
-                             unsigned int pitch)
+/*
+ * Adjust the tile offset by moving the difference into
+ * the x/y offsets.
+ *
+ * Input tile dimensions and pitch must already be
+ * rotated to match x and y, and in pixel units.
+ */
+static u32 intel_adjust_tile_offset(int *x, int *y,
+                                   unsigned int tile_width,
+                                   unsigned int tile_height,
+                                   unsigned int tile_size,
+                                   unsigned int pitch_tiles,
+                                   u32 old_offset,
+                                   u32 new_offset)
+{
+       unsigned int tiles;
+
+       WARN_ON(old_offset & (tile_size - 1));
+       WARN_ON(new_offset & (tile_size - 1));
+       WARN_ON(new_offset > old_offset);
+
+       tiles = (old_offset - new_offset) / tile_size;
+
+       *y += tiles / pitch_tiles * tile_height;
+       *x += tiles % pitch_tiles * tile_width;
+
+       return new_offset;
+}
+
+/*
+ * Computes the linear offset to the base tile and adjusts
+ * x, y. bytes per pixel is assumed to be a power-of-two.
+ *
+ * In the 90/270 rotated case, x and y are assumed
+ * to be already rotated to match the rotated GTT view, and
+ * pitch is the tile_height aligned framebuffer height.
+ */
+u32 intel_compute_tile_offset(int *x, int *y,
+                             const struct drm_framebuffer *fb, int plane,
+                             unsigned int pitch,
+                             unsigned int rotation)
 {
+       const struct drm_i915_private *dev_priv = to_i915(fb->dev);
+       uint64_t fb_modifier = fb->modifier[plane];
+       unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
+       u32 offset, offset_aligned, alignment;
+
+       alignment = intel_surf_alignment(dev_priv, fb_modifier);
+       if (alignment)
+               alignment--;
+
        if (fb_modifier != DRM_FORMAT_MOD_NONE) {
                unsigned int tile_size, tile_width, tile_height;
-               unsigned int tile_rows, tiles;
+               unsigned int tile_rows, tiles, pitch_tiles;
 
                tile_size = intel_tile_size(dev_priv);
-               tile_width = intel_tile_width(dev_priv, fb_modifier, cpp);
-               tile_height = tile_size / tile_width;
+               intel_tile_dims(dev_priv, &tile_width, &tile_height,
+                               fb_modifier, cpp);
+
+               if (intel_rotation_90_or_270(rotation)) {
+                       pitch_tiles = pitch / tile_height;
+                       swap(tile_width, tile_height);
+               } else {
+                       pitch_tiles = pitch / (tile_width * cpp);
+               }
 
                tile_rows = *y / tile_height;
                *y %= tile_height;
 
-               tiles = *x / (tile_width/cpp);
-               *x %= tile_width/cpp;
+               tiles = *x / tile_width;
+               *x %= tile_width;
 
-               return tile_rows * pitch * tile_height + tiles * tile_size;
-       } else {
-               unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
-               unsigned int offset;
+               offset = (tile_rows * pitch_tiles + tiles) * tile_size;
+               offset_aligned = offset & ~alignment;
 
+               intel_adjust_tile_offset(x, y, tile_width, tile_height,
+                                        tile_size, pitch_tiles,
+                                        offset, offset_aligned);
+       } else {
                offset = *y * pitch + *x * cpp;
+               offset_aligned = offset & ~alignment;
+
                *y = (offset & alignment) / pitch;
                *x = ((offset & alignment) - *y * pitch) / cpp;
-               return offset & ~alignment;
        }
+
+       return offset_aligned;
 }
 
 static int i9xx_format_to_fourcc(int format)
@@ -2551,12 +2642,16 @@ intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
        if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
                return false;
 
+       mutex_lock(&dev->struct_mutex);
+
        obj = i915_gem_object_create_stolen_for_preallocated(dev,
                                                             base_aligned,
                                                             base_aligned,
                                                             size_aligned);
-       if (!obj)
+       if (!obj) {
+               mutex_unlock(&dev->struct_mutex);
                return false;
+       }
 
        obj->tiling_mode = plane_config->tiling;
        if (obj->tiling_mode == I915_TILING_X)
@@ -2569,12 +2664,12 @@ intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
        mode_cmd.modifier[0] = fb->modifier[0];
        mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
 
-       mutex_lock(&dev->struct_mutex);
        if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
                                   &mode_cmd, obj)) {
                DRM_DEBUG_KMS("intel fb init failed\n");
                goto out_unref_obj;
        }
+
        mutex_unlock(&dev->struct_mutex);
 
        DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
@@ -2709,6 +2804,7 @@ static void i9xx_update_primary_plane(struct drm_plane *primary,
        u32 linear_offset;
        u32 dspcntr;
        i915_reg_t reg = DSPCNTR(plane);
+       unsigned int rotation = plane_state->base.rotation;
        int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
        int x = plane_state->src.x1 >> 16;
        int y = plane_state->src.y1 >> 16;
@@ -2773,15 +2869,14 @@ static void i9xx_update_primary_plane(struct drm_plane *primary,
 
        if (INTEL_INFO(dev)->gen >= 4) {
                intel_crtc->dspaddr_offset =
-                       intel_compute_tile_offset(dev_priv, &x, &y,
-                                                 fb->modifier[0], cpp,
-                                                 fb->pitches[0]);
+                       intel_compute_tile_offset(&x, &y, fb, 0,
+                                                 fb->pitches[0], rotation);
                linear_offset -= intel_crtc->dspaddr_offset;
        } else {
                intel_crtc->dspaddr_offset = linear_offset;
        }
 
-       if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
+       if (rotation == BIT(DRM_ROTATE_180)) {
                dspcntr |= DISPPLANE_ROTATE_180;
 
                x += (crtc_state->pipe_src_w - 1);
@@ -2839,6 +2934,7 @@ static void ironlake_update_primary_plane(struct drm_plane *primary,
        u32 linear_offset;
        u32 dspcntr;
        i915_reg_t reg = DSPCNTR(plane);
+       unsigned int rotation = plane_state->base.rotation;
        int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
        int x = plane_state->src.x1 >> 16;
        int y = plane_state->src.y1 >> 16;
@@ -2880,11 +2976,10 @@ static void ironlake_update_primary_plane(struct drm_plane *primary,
 
        linear_offset = y * fb->pitches[0] + x * cpp;
        intel_crtc->dspaddr_offset =
-               intel_compute_tile_offset(dev_priv, &x, &y,
-                                         fb->modifier[0], cpp,
-                                         fb->pitches[0]);
+               intel_compute_tile_offset(&x, &y, fb, 0,
+                                         fb->pitches[0], rotation);
        linear_offset -= intel_crtc->dspaddr_offset;
-       if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
+       if (rotation == BIT(DRM_ROTATE_180)) {
                dspcntr |= DISPPLANE_ROTATE_180;
 
                if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
@@ -2924,7 +3019,7 @@ u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
        } else {
                int cpp = drm_format_plane_cpp(pixel_format, 0);
 
-               return intel_tile_width(dev_priv, fb_modifier, cpp);
+               return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
        }
 }
 
@@ -2937,7 +3032,7 @@ u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
        u64 offset;
 
        intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
-                               intel_plane->base.state);
+                               intel_plane->base.state->rotation);
 
        vma = i915_gem_obj_to_ggtt_view(obj, &view);
        if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
@@ -3156,9 +3251,6 @@ static void skylake_disable_primary_plane(struct drm_plane *primary,
        struct drm_i915_private *dev_priv = dev->dev_private;
        int pipe = to_intel_crtc(crtc)->pipe;
 
-       if (dev_priv->fbc.deactivate)
-               dev_priv->fbc.deactivate(dev_priv);
-
        I915_WRITE(PLANE_CTL(pipe, 0), 0);
        I915_WRITE(PLANE_SURF(pipe, 0), 0);
        POSTING_READ(PLANE_SURF(pipe, 0));
@@ -3951,37 +4043,35 @@ static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
 /* Program iCLKIP clock to the desired frequency */
 static void lpt_program_iclkip(struct drm_crtc *crtc)
 {
-       struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct drm_i915_private *dev_priv = to_i915(crtc->dev);
        int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
        u32 divsel, phaseinc, auxdiv, phasedir = 0;
        u32 temp;
 
        lpt_disable_iclkip(dev_priv);
 
-       /* 20MHz is a corner case which is out of range for the 7-bit divisor */
-       if (clock == 20000) {
-               auxdiv = 1;
-               divsel = 0x41;
-               phaseinc = 0x20;
-       } else {
-               /* The iCLK virtual clock root frequency is in MHz,
-                * but the adjusted_mode->crtc_clock in in KHz. To get the
-                * divisors, it is necessary to divide one by another, so we
-                * convert the virtual clock precision to KHz here for higher
-                * precision.
-                */
+       /* The iCLK virtual clock root frequency is in MHz,
+        * but the adjusted_mode->crtc_clock in in KHz. To get the
+        * divisors, it is necessary to divide one by another, so we
+        * convert the virtual clock precision to KHz here for higher
+        * precision.
+        */
+       for (auxdiv = 0; auxdiv < 2; auxdiv++) {
                u32 iclk_virtual_root_freq = 172800 * 1000;
                u32 iclk_pi_range = 64;
-               u32 desired_divisor, msb_divisor_value, pi_value;
+               u32 desired_divisor;
 
-               desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq, clock);
-               msb_divisor_value = desired_divisor / iclk_pi_range;
-               pi_value = desired_divisor % iclk_pi_range;
+               desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
+                                                   clock << auxdiv);
+               divsel = (desired_divisor / iclk_pi_range) - 2;
+               phaseinc = desired_divisor % iclk_pi_range;
 
-               auxdiv = 0;
-               divsel = msb_divisor_value - 2;
-               phaseinc = pi_value;
+               /*
+                * Near 20MHz is a corner case which is
+                * out of range for the 7-bit divisor
+                */
+               if (divsel <= 0x7f)
+                       break;
        }
 
        /* This should not happen with any sane values */
@@ -4028,6 +4118,43 @@ static void lpt_program_iclkip(struct drm_crtc *crtc)
        I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
 }
 
+int lpt_get_iclkip(struct drm_i915_private *dev_priv)
+{
+       u32 divsel, phaseinc, auxdiv;
+       u32 iclk_virtual_root_freq = 172800 * 1000;
+       u32 iclk_pi_range = 64;
+       u32 desired_divisor;
+       u32 temp;
+
+       if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
+               return 0;
+
+       mutex_lock(&dev_priv->sb_lock);
+
+       temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
+       if (temp & SBI_SSCCTL_DISABLE) {
+               mutex_unlock(&dev_priv->sb_lock);
+               return 0;
+       }
+
+       temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
+       divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
+               SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
+       phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
+               SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
+
+       temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
+       auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
+               SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
+
+       mutex_unlock(&dev_priv->sb_lock);
+
+       desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
+
+       return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
+                                desired_divisor << auxdiv);
+}
+
 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
                                                enum pipe pch_transcoder)
 {
@@ -4788,9 +4915,6 @@ static void intel_post_plane_update(struct intel_crtc *crtc)
                to_intel_crtc_state(crtc->base.state);
        struct drm_device *dev = crtc->base.dev;
 
-       if (atomic->wait_vblank)
-               intel_wait_for_vblank(dev, crtc->pipe);
-
        intel_frontbuffer_flip(dev, atomic->fb_bits);
 
        crtc->wm.cxsr_allowed = true;
@@ -4807,29 +4931,77 @@ static void intel_post_plane_update(struct intel_crtc *crtc)
        memset(atomic, 0, sizeof(*atomic));
 }
 
-static void intel_pre_plane_update(struct intel_crtc *crtc)
+static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
 {
+       struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
        struct drm_device *dev = crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
        struct intel_crtc_state *pipe_config =
                to_intel_crtc_state(crtc->base.state);
+       struct drm_atomic_state *old_state = old_crtc_state->base.state;
+       struct drm_plane *primary = crtc->base.primary;
+       struct drm_plane_state *old_pri_state =
+               drm_atomic_get_existing_plane_state(old_state, primary);
+       bool modeset = needs_modeset(&pipe_config->base);
 
        if (atomic->update_fbc)
                intel_fbc_pre_update(crtc);
 
-       if (crtc->atomic.disable_ips)
-               hsw_disable_ips(crtc);
+       if (old_pri_state) {
+               struct intel_plane_state *primary_state =
+                       to_intel_plane_state(primary->state);
+               struct intel_plane_state *old_primary_state =
+                       to_intel_plane_state(old_pri_state);
 
-       if (atomic->pre_disable_primary)
-               intel_pre_disable_primary(&crtc->base);
+               if (old_primary_state->visible &&
+                   (modeset || !primary_state->visible))
+                       intel_pre_disable_primary(&crtc->base);
+       }
 
        if (pipe_config->disable_cxsr) {
                crtc->wm.cxsr_allowed = false;
-               intel_set_memory_cxsr(dev_priv, false);
+
+               if (old_crtc_state->base.active)
+                       intel_set_memory_cxsr(dev_priv, false);
+       }
+
+       /*
+        * IVB workaround: must disable low power watermarks for at least
+        * one frame before enabling scaling.  LP watermarks can be re-enabled
+        * when scaling is disabled.
+        *
+        * WaCxSRDisabledForSpriteScaling:ivb
+        */
+       if (pipe_config->disable_lp_wm) {
+               ilk_disable_lp_wm(dev);
+               intel_wait_for_vblank(dev, crtc->pipe);
        }
 
-       if (!needs_modeset(&pipe_config->base) && pipe_config->wm_changed)
+       /*
+        * If we're doing a modeset, we're done.  No need to do any pre-vblank
+        * watermark programming here.
+        */
+       if (needs_modeset(&pipe_config->base))
+               return;
+
+       /*
+        * For platforms that support atomic watermarks, program the
+        * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
+        * will be the intermediate values that are safe for both pre- and
+        * post- vblank; when vblank happens, the 'active' values will be set
+        * to the final 'target' values and we'll do this again to get the
+        * optimal watermarks.  For gen9+ platforms, the values we program here
+        * will be the final target values which will get automatically latched
+        * at vblank time; no further programming will be necessary.
+        *
+        * If a platform hasn't been transitioned to atomic watermarks yet,
+        * we'll continue to update watermarks the old way, if flags tell
+        * us to.
+        */
+       if (dev_priv->display.initial_watermarks != NULL)
+               dev_priv->display.initial_watermarks(pipe_config);
+       else if (pipe_config->wm_changed)
                intel_update_watermarks(&crtc->base);
 }
 
@@ -4908,7 +5080,8 @@ static void ironlake_crtc_enable(struct drm_crtc *crtc)
         */
        intel_crtc_load_lut(crtc);
 
-       intel_update_watermarks(crtc);
+       if (dev_priv->display.initial_watermarks != NULL)
+               dev_priv->display.initial_watermarks(intel_crtc->config);
        intel_enable_pipe(intel_crtc);
 
        if (intel_crtc->config->has_pch_encoder)
@@ -5007,7 +5180,10 @@ static void haswell_crtc_enable(struct drm_crtc *crtc)
        if (!intel_crtc->config->has_dsi_encoder)
                intel_ddi_enable_transcoder_func(crtc);
 
-       intel_update_watermarks(crtc);
+       if (dev_priv->display.initial_watermarks != NULL)
+               dev_priv->display.initial_watermarks(pipe_config);
+       else
+               intel_update_watermarks(crtc);
        intel_enable_pipe(intel_crtc);
 
        if (intel_crtc->config->has_pch_encoder)
@@ -5291,31 +5467,37 @@ intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
        }
 }
 
-static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
+static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
+                                           struct intel_crtc_state *crtc_state)
 {
        struct drm_device *dev = crtc->dev;
-       struct intel_encoder *intel_encoder;
+       struct drm_encoder *encoder;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        enum pipe pipe = intel_crtc->pipe;
        unsigned long mask;
-       enum transcoder transcoder = intel_crtc->config->cpu_transcoder;
+       enum transcoder transcoder = crtc_state->cpu_transcoder;
 
-       if (!crtc->state->active)
+       if (!crtc_state->base.active)
                return 0;
 
        mask = BIT(POWER_DOMAIN_PIPE(pipe));
        mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
-       if (intel_crtc->config->pch_pfit.enabled ||
-           intel_crtc->config->pch_pfit.force_thru)
+       if (crtc_state->pch_pfit.enabled ||
+           crtc_state->pch_pfit.force_thru)
                mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
 
-       for_each_encoder_on_crtc(dev, crtc, intel_encoder)
+       drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
+               struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
+
                mask |= BIT(intel_display_port_power_domain(intel_encoder));
+       }
 
        return mask;
 }
 
-static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
+static unsigned long
+modeset_get_crtc_power_domains(struct drm_crtc *crtc,
+                              struct intel_crtc_state *crtc_state)
 {
        struct drm_i915_private *dev_priv = crtc->dev->dev_private;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
@@ -5323,7 +5505,8 @@ static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
        unsigned long domains, new_domains, old_domains;
 
        old_domains = intel_crtc->enabled_power_domains;
-       intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
+       intel_crtc->enabled_power_domains = new_domains =
+               get_crtc_power_domains(crtc, crtc_state);
 
        domains = new_domains & ~old_domains;
 
@@ -5342,31 +5525,6 @@ static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
                intel_display_power_put(dev_priv, domain);
 }
 
-static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
-{
-       struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
-       struct drm_device *dev = state->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       unsigned long put_domains[I915_MAX_PIPES] = {};
-       struct drm_crtc_state *crtc_state;
-       struct drm_crtc *crtc;
-       int i;
-
-       for_each_crtc_in_state(state, crtc, crtc_state, i) {
-               if (needs_modeset(crtc->state))
-                       put_domains[to_intel_crtc(crtc)->pipe] =
-                               modeset_get_crtc_power_domains(crtc);
-       }
-
-       if (dev_priv->display.modeset_commit_cdclk &&
-           intel_state->dev_cdclk != dev_priv->cdclk_freq)
-               dev_priv->display.modeset_commit_cdclk(state);
-
-       for (i = 0; i < I915_MAX_PIPES; i++)
-               if (put_domains[i])
-                       modeset_put_power_domains(dev_priv, put_domains[i]);
-}
-
 static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
 {
        int max_cdclk_freq = dev_priv->max_cdclk_freq;
@@ -6029,8 +6187,7 @@ static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
                return 144000;
 }
 
-/* Compute the max pixel clock for new configuration. Uses atomic state if
- * that's non-NULL, look at current state otherwise. */
+/* Compute the max pixel clock for new configuration. */
 static int intel_mode_max_pixclk(struct drm_device *dev,
                                 struct drm_atomic_state *state)
 {
@@ -6053,9 +6210,6 @@ static int intel_mode_max_pixclk(struct drm_device *dev,
                intel_state->min_pixclk[i] = pixclk;
        }
 
-       if (!intel_state->active_crtcs)
-               return 0;
-
        for_each_pipe(dev_priv, pipe)
                max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
 
@@ -6383,55 +6537,16 @@ static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  */
 int intel_display_suspend(struct drm_device *dev)
 {
-       struct drm_mode_config *config = &dev->mode_config;
-       struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct drm_atomic_state *state;
-       struct drm_crtc *crtc;
-       unsigned crtc_mask = 0;
-       int ret = 0;
-
-       if (WARN_ON(!ctx))
-               return 0;
-
-       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;
-
-       for_each_crtc(dev, crtc) {
-               struct drm_crtc_state *crtc_state =
-                       drm_atomic_get_crtc_state(state, crtc);
-
-               ret = PTR_ERR_OR_ZERO(crtc_state);
-               if (ret)
-                       goto free;
-
-               if (!crtc_state->active)
-                       continue;
-
-               crtc_state->active = false;
-               crtc_mask |= 1 << drm_crtc_index(crtc);
-       }
-
-       if (crtc_mask) {
-               ret = drm_atomic_commit(state);
-
-               if (!ret) {
-                       for_each_crtc(dev, crtc)
-                               if (crtc_mask & (1 << drm_crtc_index(crtc)))
-                                       crtc->state->active = true;
-
-                       return ret;
-               }
-       }
+       int ret;
 
-free:
+       state = drm_atomic_helper_suspend(dev);
+       ret = PTR_ERR_OR_ZERO(state);
        if (ret)
                DRM_ERROR("Suspending crtc's failed with %i\n", ret);
-       drm_atomic_state_free(state);
+       else
+               dev_priv->modeset_restore_state = state;
        return ret;
 }
 
@@ -6615,7 +6730,7 @@ retry:
         * Hence the bw of each lane in terms of the mode signal
         * is:
         */
-       link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
+       link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
 
        fdi_dotclock = adjusted_mode->crtc_clock;
 
@@ -6627,8 +6742,7 @@ retry:
        intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
                               link_bw, &pipe_config->fdi_m_n);
 
-       ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
-                                      intel_crtc->pipe, pipe_config);
+       ret = ironlake_check_fdi_lanes(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",
@@ -8171,18 +8285,22 @@ static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
 {
        struct drm_device *dev = crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
+       enum intel_display_power_domain power_domain;
        uint32_t tmp;
+       bool ret;
 
-       if (!intel_display_power_is_enabled(dev_priv,
-                                           POWER_DOMAIN_PIPE(crtc->pipe)))
+       power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
+       if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
                return false;
 
        pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
        pipe_config->shared_dpll = DPLL_ID_PRIVATE;
 
+       ret = false;
+
        tmp = I915_READ(PIPECONF(crtc->pipe));
        if (!(tmp & PIPECONF_ENABLE))
-               return false;
+               goto out;
 
        if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
                switch (tmp & PIPECONF_BPC_MASK) {
@@ -8262,7 +8380,12 @@ static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
        pipe_config->base.adjusted_mode.crtc_clock =
                pipe_config->port_clock / pipe_config->pixel_multiplier;
 
-       return true;
+       ret = true;
+
+out:
+       intel_display_power_put(dev_priv, power_domain);
+
+       return ret;
 }
 
 static void ironlake_init_pch_refclk(struct drm_device *dev)
@@ -9366,18 +9489,21 @@ static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
 {
        struct drm_device *dev = crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
+       enum intel_display_power_domain power_domain;
        uint32_t tmp;
+       bool ret;
 
-       if (!intel_display_power_is_enabled(dev_priv,
-                                           POWER_DOMAIN_PIPE(crtc->pipe)))
+       power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
+       if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
                return false;
 
        pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
        pipe_config->shared_dpll = DPLL_ID_PRIVATE;
 
+       ret = false;
        tmp = I915_READ(PIPECONF(crtc->pipe));
        if (!(tmp & PIPECONF_ENABLE))
-               return false;
+               goto out;
 
        switch (tmp & PIPECONF_BPC_MASK) {
        case PIPECONF_6BPC:
@@ -9440,7 +9566,12 @@ static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
 
        ironlake_get_pfit_config(crtc, pipe_config);
 
-       return true;
+       ret = true;
+
+out:
+       intel_display_power_put(dev_priv, power_domain);
+
+       return ret;
 }
 
 static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
@@ -9706,9 +9837,6 @@ static int ilk_max_pixel_rate(struct drm_atomic_state *state)
                intel_state->min_pixclk[i] = pixel_rate;
        }
 
-       if (!intel_state->active_crtcs)
-               return 0;
-
        for_each_pipe(dev_priv, pipe)
                max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
 
@@ -9742,8 +9870,8 @@ static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
        val |= LCPLL_CD_SOURCE_FCLK;
        I915_WRITE(LCPLL_CTL, val);
 
-       if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
-                              LCPLL_CD_SOURCE_FCLK_DONE, 1))
+       if (wait_for_us(I915_READ(LCPLL_CTL) &
+                       LCPLL_CD_SOURCE_FCLK_DONE, 1))
                DRM_ERROR("Switching to FCLK failed\n");
 
        val = I915_READ(LCPLL_CTL);
@@ -9777,8 +9905,8 @@ static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
        val &= ~LCPLL_CD_SOURCE_FCLK;
        I915_WRITE(LCPLL_CTL, val);
 
-       if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
-                               LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
+       if (wait_for_us((I915_READ(LCPLL_CTL) &
+                       LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
                DRM_ERROR("Switching back to LCPLL failed\n");
 
        mutex_lock(&dev_priv->rps.hw_lock);
@@ -9838,8 +9966,13 @@ static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
 static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
                                      struct intel_crtc_state *crtc_state)
 {
-       if (!intel_ddi_pll_select(crtc, crtc_state))
-               return -EINVAL;
+       struct intel_encoder *intel_encoder =
+               intel_ddi_get_crtc_new_encoder(crtc_state);
+
+       if (intel_encoder->type != INTEL_OUTPUT_DSI) {
+               if (!intel_ddi_pll_select(crtc, crtc_state))
+                       return -EINVAL;
+       }
 
        crtc->lowfreq_avail = false;
 
@@ -9967,12 +10100,17 @@ static bool haswell_get_pipe_config(struct intel_crtc *crtc,
 {
        struct drm_device *dev = crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
-       enum intel_display_power_domain pfit_domain;
+       enum intel_display_power_domain power_domain;
+       unsigned long power_domain_mask;
        uint32_t tmp;
+       bool ret;
 
-       if (!intel_display_power_is_enabled(dev_priv,
-                                        POWER_DOMAIN_PIPE(crtc->pipe)))
+       power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
+       if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
                return false;
+       power_domain_mask = BIT(power_domain);
+
+       ret = false;
 
        pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
        pipe_config->shared_dpll = DPLL_ID_PRIVATE;
@@ -9999,13 +10137,14 @@ static bool haswell_get_pipe_config(struct intel_crtc *crtc,
                        pipe_config->cpu_transcoder = TRANSCODER_EDP;
        }
 
-       if (!intel_display_power_is_enabled(dev_priv,
-                       POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
-               return false;
+       power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
+       if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
+               goto out;
+       power_domain_mask |= BIT(power_domain);
 
        tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
        if (!(tmp & PIPECONF_ENABLE))
-               return false;
+               goto out;
 
        haswell_get_ddi_port_state(crtc, pipe_config);
 
@@ -10015,14 +10154,14 @@ static bool haswell_get_pipe_config(struct intel_crtc *crtc,
                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)) {
+       power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
+       if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
+               power_domain_mask |= BIT(power_domain);
                if (INTEL_INFO(dev)->gen >= 9)
                        skylake_get_pfit_config(crtc, pipe_config);
                else
@@ -10040,7 +10179,13 @@ static bool haswell_get_pipe_config(struct intel_crtc *crtc,
                pipe_config->pixel_multiplier = 1;
        }
 
-       return true;
+       ret = true;
+
+out:
+       for_each_power_domain(power_domain, power_domain_mask)
+               intel_display_power_put(dev_priv, power_domain);
+
+       return ret;
 }
 
 static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
@@ -10361,6 +10506,7 @@ mode_fits_in_fbdev(struct drm_device *dev,
        if (obj->base.size < mode->vdisplay * fb->pitches[0])
                return NULL;
 
+       drm_framebuffer_reference(fb);
        return fb;
 #else
        return NULL;
@@ -10416,7 +10562,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
        struct drm_device *dev = encoder->dev;
        struct drm_framebuffer *fb;
        struct drm_mode_config *config = &dev->mode_config;
-       struct drm_atomic_state *state = NULL;
+       struct drm_atomic_state *state = NULL, *restore_state = NULL;
        struct drm_connector_state *connector_state;
        struct intel_crtc_state *crtc_state;
        int ret, i = -1;
@@ -10425,6 +10571,8 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
                      connector->base.id, connector->name,
                      encoder->base.id, encoder->name);
 
+       old->restore_state = NULL;
+
 retry:
        ret = drm_modeset_lock(&config->connection_mutex, ctx);
        if (ret)
@@ -10441,24 +10589,15 @@ retry:
         */
 
        /* See if we already have a CRTC for this connector */
-       if (encoder->crtc) {
-               crtc = encoder->crtc;
+       if (connector->state->crtc) {
+               crtc = connector->state->crtc;
 
                ret = drm_modeset_lock(&crtc->mutex, ctx);
-               if (ret)
-                       goto fail;
-               ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
                if (ret)
                        goto fail;
 
-               old->dpms_mode = connector->dpms;
-               old->load_detect_temp = false;
-
                /* Make sure the crtc and connector are running */
-               if (connector->dpms != DRM_MODE_DPMS_ON)
-                       connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
-
-               return true;
+               goto found;
        }
 
        /* Find an unused one (if possible) */
@@ -10466,8 +10605,15 @@ retry:
                i++;
                if (!(encoder->possible_crtcs & (1 << i)))
                        continue;
-               if (possible_crtc->state->enable)
+
+               ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
+               if (ret)
+                       goto fail;
+
+               if (possible_crtc->state->enable) {
+                       drm_modeset_unlock(&possible_crtc->mutex);
                        continue;
+               }
 
                crtc = possible_crtc;
                break;
@@ -10481,23 +10627,22 @@ retry:
                goto fail;
        }
 
-       ret = drm_modeset_lock(&crtc->mutex, ctx);
-       if (ret)
-               goto fail;
+found:
+       intel_crtc = to_intel_crtc(crtc);
+
        ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
        if (ret)
                goto fail;
 
-       intel_crtc = to_intel_crtc(crtc);
-       old->dpms_mode = connector->dpms;
-       old->load_detect_temp = true;
-       old->release_fb = NULL;
-
        state = drm_atomic_state_alloc(dev);
-       if (!state)
-               return false;
+       restore_state = drm_atomic_state_alloc(dev);
+       if (!state || !restore_state) {
+               ret = -ENOMEM;
+               goto fail;
+       }
 
        state->acquire_ctx = ctx;
+       restore_state->acquire_ctx = ctx;
 
        connector_state = drm_atomic_get_connector_state(state, connector);
        if (IS_ERR(connector_state)) {
@@ -10505,8 +10650,9 @@ retry:
                goto fail;
        }
 
-       connector_state->crtc = crtc;
-       connector_state->best_encoder = &intel_encoder->base;
+       ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
+       if (ret)
+               goto fail;
 
        crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
        if (IS_ERR(crtc_state)) {
@@ -10530,7 +10676,6 @@ retry:
        if (fb == NULL) {
                DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
                fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
-               old->release_fb = fb;
        } else
                DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
        if (IS_ERR(fb)) {
@@ -10542,15 +10687,29 @@ retry:
        if (ret)
                goto fail;
 
-       drm_mode_copy(&crtc_state->base.mode, mode);
+       drm_framebuffer_unreference(fb);
+
+       ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
+       if (ret)
+               goto fail;
+
+       ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
+       if (!ret)
+               ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
+       if (!ret)
+               ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
+       if (ret) {
+               DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
+               goto fail;
+       }
 
-       if (drm_atomic_commit(state)) {
+       ret = drm_atomic_commit(state);
+       if (ret) {
                DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
-               if (old->release_fb)
-                       old->release_fb->funcs->destroy(old->release_fb);
                goto fail;
        }
-       crtc->primary->crtc = crtc;
+
+       old->restore_state = restore_state;
 
        /* let the connector get through one full cycle before testing */
        intel_wait_for_vblank(dev, intel_crtc->pipe);
@@ -10558,7 +10717,8 @@ retry:
 
 fail:
        drm_atomic_state_free(state);
-       state = NULL;
+       drm_atomic_state_free(restore_state);
+       restore_state = state = NULL;
 
        if (ret == -EDEADLK) {
                drm_modeset_backoff(ctx);
@@ -10572,66 +10732,24 @@ void intel_release_load_detect_pipe(struct drm_connector *connector,
                                    struct intel_load_detect_pipe *old,
                                    struct drm_modeset_acquire_ctx *ctx)
 {
-       struct drm_device *dev = connector->dev;
        struct intel_encoder *intel_encoder =
                intel_attached_encoder(connector);
        struct drm_encoder *encoder = &intel_encoder->base;
-       struct drm_crtc *crtc = encoder->crtc;
-       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;
+       struct drm_atomic_state *state = old->restore_state;
        int ret;
 
        DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
                      connector->base.id, connector->name,
                      encoder->base.id, encoder->name);
 
-       if (old->load_detect_temp) {
-               state = drm_atomic_state_alloc(dev);
-               if (!state)
-                       goto fail;
-
-               state->acquire_ctx = ctx;
-
-               connector_state = drm_atomic_get_connector_state(state, connector);
-               if (IS_ERR(connector_state))
-                       goto fail;
-
-               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;
-
-               crtc_state->base.enable = crtc_state->base.active = false;
-
-               ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
-                                                     0, 0);
-               if (ret)
-                       goto fail;
-
-               ret = drm_atomic_commit(state);
-               if (ret)
-                       goto fail;
-
-               if (old->release_fb) {
-                       drm_framebuffer_unregister_private(old->release_fb);
-                       drm_framebuffer_unreference(old->release_fb);
-               }
-
+       if (!state)
                return;
-       }
-
-       /* Switch crtc and encoder back off if necessary */
-       if (old->dpms_mode != DRM_MODE_DPMS_ON)
-               connector->funcs->dpms(connector, old->dpms_mode);
 
-       return;
-fail:
-       DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
-       drm_atomic_state_free(state);
+       ret = drm_atomic_commit(state);
+       if (ret) {
+               DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
+               drm_atomic_state_free(state);
+       }
 }
 
 static int i9xx_pll_refclk(struct drm_device *dev,
@@ -10762,19 +10880,18 @@ int intel_dotclock_calculate(int link_freq,
 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
                                   struct intel_crtc_state *pipe_config)
 {
-       struct drm_device *dev = crtc->base.dev;
+       struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
 
        /* read out port_clock from the DPLL */
        i9xx_crtc_clock_get(crtc, pipe_config);
 
        /*
-        * This value does not include pixel_multiplier.
-        * We will check that port_clock and adjusted_mode.crtc_clock
-        * agree once we know their relationship in the encoder's
-        * get_config() function.
+        * In case there is an active pipe without active ports,
+        * we may need some idea for the dotclock anyway.
+        * Calculate one based on the FDI configuration.
         */
        pipe_config->base.adjusted_mode.crtc_clock =
-               intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
+               intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
                                         &pipe_config->fdi_m_n);
 }
 
@@ -10893,7 +11010,7 @@ static void intel_unpin_work_fn(struct work_struct *__work)
        struct drm_plane *primary = crtc->base.primary;
 
        mutex_lock(&dev->struct_mutex);
-       intel_unpin_fb_obj(work->old_fb, primary->state);
+       intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
        drm_gem_object_unreference(&work->pending_flip_obj->base);
 
        if (work->flip_queued_req)
@@ -10982,6 +11099,12 @@ static bool page_flip_finished(struct intel_crtc *crtc)
        if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
                return 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 :(
+        */
+
        /*
         * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
         * used the same base address. In that case the mmio flip might
@@ -11659,8 +11782,7 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
                        goto cleanup_pending;
        }
 
-       ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
-                                        crtc->primary->state);
+       ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
        if (ret)
                goto cleanup_pending;
 
@@ -11710,7 +11832,7 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
        return 0;
 
 cleanup_unpin:
-       intel_unpin_fb_obj(fb, crtc->primary->state);
+       intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
 cleanup_pending:
        if (!IS_ERR_OR_NULL(request))
                i915_gem_request_cancel(request);
@@ -11823,10 +11945,10 @@ int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct drm_plane *plane = plane_state->plane;
        struct drm_device *dev = crtc->dev;
+       struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_plane_state *old_plane_state =
                to_intel_plane_state(plane->state);
        int idx = intel_crtc->base.base.id, ret;
-       int i = drm_plane_index(plane);
        bool mode_changed = needs_modeset(crtc_state);
        bool was_crtc_enabled = crtc->state->active;
        bool is_crtc_enabled = crtc_state->active;
@@ -11859,6 +11981,9 @@ int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
        if (!was_visible && !visible)
                return 0;
 
+       if (fb != old_plane_state->base.fb)
+               pipe_config->fb_changed = true;
+
        turn_off = was_visible && (!visible || mode_changed);
        turn_on = visible && (!was_visible || mode_changed);
 
@@ -11873,45 +11998,26 @@ int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
                pipe_config->wm_changed = true;
 
                /* must disable cxsr around plane enable/disable */
-               if (plane->type != DRM_PLANE_TYPE_CURSOR) {
-                       if (is_crtc_enabled)
-                               intel_crtc->atomic.wait_vblank = true;
+               if (plane->type != DRM_PLANE_TYPE_CURSOR)
                        pipe_config->disable_cxsr = true;
-               }
        } else if (intel_wm_need_update(plane, plane_state)) {
                pipe_config->wm_changed = true;
        }
 
+       /* Pre-gen9 platforms need two-step watermark updates */
+       if (pipe_config->wm_changed && INTEL_INFO(dev)->gen < 9 &&
+           dev_priv->display.optimize_watermarks)
+               to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
+
        if (visible || was_visible)
                intel_crtc->atomic.fb_bits |=
                        to_intel_plane(plane)->frontbuffer_bit;
 
        switch (plane->type) {
        case DRM_PLANE_TYPE_PRIMARY:
-               intel_crtc->atomic.pre_disable_primary = turn_off;
                intel_crtc->atomic.post_enable_primary = turn_on;
                intel_crtc->atomic.update_fbc = true;
 
-               if (turn_off) {
-                       /*
-                        * FIXME: Actually if we will still have any other
-                        * plane enabled on the pipe we could let IPS enabled
-                        * still, but for now lets consider that when we make
-                        * primary invisible by setting DSPCNTR to 0 on
-                        * update_primary_plane function IPS needs to be
-                        * disable.
-                        */
-                       intel_crtc->atomic.disable_ips = 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 (turn_on && IS_BROADWELL(dev))
-                       intel_crtc->atomic.wait_vblank = true;
-
                break;
        case DRM_PLANE_TYPE_CURSOR:
                break;
@@ -11924,13 +12030,8 @@ int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
                 */
                if (IS_IVYBRIDGE(dev) &&
                    needs_scaling(to_intel_plane_state(plane_state)) &&
-                   !needs_scaling(old_plane_state)) {
-                       to_intel_crtc_state(crtc_state)->disable_lp_wm = true;
-               } else if (turn_off && !mode_changed) {
-                       intel_crtc->atomic.wait_vblank = true;
-                       intel_crtc->atomic.update_sprite_watermarks |=
-                               1 << i;
-               }
+                   !needs_scaling(old_plane_state))
+                       pipe_config->disable_lp_wm = true;
 
                break;
        }
@@ -12018,9 +12119,30 @@ static int intel_crtc_atomic_check(struct drm_crtc *crtc,
 
        ret = 0;
        if (dev_priv->display.compute_pipe_wm) {
-               ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
-               if (ret)
+               ret = dev_priv->display.compute_pipe_wm(pipe_config);
+               if (ret) {
+                       DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
+                       return ret;
+               }
+       }
+
+       if (dev_priv->display.compute_intermediate_wm &&
+           !to_intel_atomic_state(state)->skip_intermediate_wm) {
+               if (WARN_ON(!dev_priv->display.compute_pipe_wm))
+                       return 0;
+
+               /*
+                * Calculate 'intermediate' watermarks that satisfy both the
+                * old state and the new state.  We can program these
+                * immediately.
+                */
+               ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
+                                                               intel_crtc,
+                                                               pipe_config);
+               if (ret) {
+                       DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
                        return ret;
+               }
        }
 
        if (INTEL_INFO(dev)->gen >= 9) {
@@ -12230,7 +12352,7 @@ static void intel_dump_pipe_config(struct intel_crtc *crtc,
                              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 spll: 0x%x\n",
+               DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
                              pipe_config->ddi_pll_sel,
                              pipe_config->dpll_hw_state.wrpll,
                              pipe_config->dpll_hw_state.spll);
@@ -12798,6 +12920,24 @@ intel_pipe_config_compare(struct drm_device *dev,
        return ret;
 }
 
+static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
+                                          const struct intel_crtc_state *pipe_config)
+{
+       if (pipe_config->has_pch_encoder) {
+               int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
+                                                           &pipe_config->fdi_m_n);
+               int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
+
+               /*
+                * FDI already provided one idea for the dotclock.
+                * Yell if the encoder disagrees.
+                */
+               WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
+                    "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
+                    fdi_dotclock, dotclock);
+       }
+}
+
 static void check_wm_state(struct drm_device *dev)
 {
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -12971,6 +13111,8 @@ check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
                if (!crtc->state->active)
                        continue;
 
+               intel_pipe_config_sanity_check(dev_priv, pipe_config);
+
                sw_config = to_intel_crtc_state(crtc->state);
                if (!intel_pipe_config_compare(dev, sw_config,
                                               pipe_config, false)) {
@@ -13043,18 +13185,6 @@ intel_modeset_check_state(struct drm_device *dev,
        check_shared_dpll_state(dev);
 }
 
-void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
-                                    int dotclock)
-{
-       /*
-        * FDI already provided one idea for the dotclock.
-        * Yell if the encoder disagrees.
-        */
-       WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
-            "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
-            pipe_config->base.adjusted_mode.crtc_clock, dotclock);
-}
-
 static void update_scanline_offset(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
@@ -13098,8 +13228,6 @@ static void intel_modeset_clear_plls(struct drm_atomic_state *state)
        struct drm_device *dev = state->dev;
        struct drm_i915_private *dev_priv = to_i915(dev);
        struct intel_shared_dpll_config *shared_dpll = NULL;
-       struct intel_crtc *intel_crtc;
-       struct intel_crtc_state *intel_crtc_state;
        struct drm_crtc *crtc;
        struct drm_crtc_state *crtc_state;
        int i;
@@ -13108,21 +13236,21 @@ static void intel_modeset_clear_plls(struct drm_atomic_state *state)
                return;
 
        for_each_crtc_in_state(state, crtc, crtc_state, i) {
-               int dpll;
-
-               intel_crtc = to_intel_crtc(crtc);
-               intel_crtc_state = to_intel_crtc_state(crtc_state);
-               dpll = intel_crtc_state->shared_dpll;
+               struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+               int old_dpll = to_intel_crtc_state(crtc->state)->shared_dpll;
 
-               if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
+               if (!needs_modeset(crtc_state))
                        continue;
 
-               intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
+               to_intel_crtc_state(crtc_state)->shared_dpll = DPLL_ID_PRIVATE;
+
+               if (old_dpll == DPLL_ID_PRIVATE)
+                       continue;
 
                if (!shared_dpll)
                        shared_dpll = intel_atomic_get_shared_dpll_state(state);
 
-               shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
+               shared_dpll[old_dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
        }
 }
 
@@ -13258,6 +13386,9 @@ static int intel_modeset_checks(struct drm_atomic_state *state)
 
                if (ret < 0)
                        return ret;
+
+               DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
+                             intel_state->cdclk, intel_state->dev_cdclk);
        } else
                to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
 
@@ -13463,6 +13594,71 @@ static int intel_atomic_prepare_commit(struct drm_device *dev,
        return ret;
 }
 
+static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
+                                         struct drm_i915_private *dev_priv,
+                                         unsigned crtc_mask)
+{
+       unsigned last_vblank_count[I915_MAX_PIPES];
+       enum pipe pipe;
+       int ret;
+
+       if (!crtc_mask)
+               return;
+
+       for_each_pipe(dev_priv, pipe) {
+               struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
+
+               if (!((1 << pipe) & crtc_mask))
+                       continue;
+
+               ret = drm_crtc_vblank_get(crtc);
+               if (WARN_ON(ret != 0)) {
+                       crtc_mask &= ~(1 << pipe);
+                       continue;
+               }
+
+               last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
+       }
+
+       for_each_pipe(dev_priv, pipe) {
+               struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
+               long lret;
+
+               if (!((1 << pipe) & crtc_mask))
+                       continue;
+
+               lret = wait_event_timeout(dev->vblank[pipe].queue,
+                               last_vblank_count[pipe] !=
+                                       drm_crtc_vblank_count(crtc),
+                               msecs_to_jiffies(50));
+
+               WARN_ON(!lret);
+
+               drm_crtc_vblank_put(crtc);
+       }
+}
+
+static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
+{
+       /* fb updated, need to unpin old fb */
+       if (crtc_state->fb_changed)
+               return true;
+
+       /* wm changes, need vblank before final wm's */
+       if (crtc_state->wm_changed)
+               return true;
+
+       /*
+        * cxsr is re-enabled after vblank.
+        * This is already handled by crtc_state->wm_changed,
+        * but added for clarity.
+        */
+       if (crtc_state->disable_cxsr)
+               return true;
+
+       return false;
+}
+
 /**
  * intel_atomic_commit - commit validated state object
  * @dev: DRM device
@@ -13487,8 +13683,11 @@ static int intel_atomic_commit(struct drm_device *dev,
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_crtc_state *crtc_state;
        struct drm_crtc *crtc;
+       struct intel_crtc_state *intel_cstate;
        int ret = 0, i;
        bool hw_check = intel_state->modeset;
+       unsigned long put_domains[I915_MAX_PIPES] = {};
+       unsigned crtc_vblank_mask = 0;
 
        ret = intel_atomic_prepare_commit(dev, state, async);
        if (ret) {
@@ -13504,15 +13703,26 @@ static int intel_atomic_commit(struct drm_device *dev,
                       sizeof(intel_state->min_pixclk));
                dev_priv->active_crtcs = intel_state->active_crtcs;
                dev_priv->atomic_cdclk_freq = intel_state->cdclk;
+
+               intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
        }
 
        for_each_crtc_in_state(state, crtc, crtc_state, i) {
                struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
 
+               if (needs_modeset(crtc->state) ||
+                   to_intel_crtc_state(crtc->state)->update_pipe) {
+                       hw_check = true;
+
+                       put_domains[to_intel_crtc(crtc)->pipe] =
+                               modeset_get_crtc_power_domains(crtc,
+                                       to_intel_crtc_state(crtc->state));
+               }
+
                if (!needs_modeset(crtc->state))
                        continue;
 
-               intel_pre_plane_update(intel_crtc);
+               intel_pre_plane_update(to_intel_crtc_state(crtc_state));
 
                if (crtc_state->active) {
                        intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
@@ -13541,34 +13751,27 @@ static int intel_atomic_commit(struct drm_device *dev,
                intel_shared_dpll_commit(state);
 
                drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
-               modeset_update_crtc_power_domains(state);
+
+               if (dev_priv->display.modeset_commit_cdclk &&
+                   intel_state->dev_cdclk != dev_priv->cdclk_freq)
+                       dev_priv->display.modeset_commit_cdclk(state);
        }
 
        /* Now enable the clocks, plane, pipe, and connectors that we set up. */
        for_each_crtc_in_state(state, crtc, crtc_state, i) {
                struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
                bool modeset = needs_modeset(crtc->state);
-               bool update_pipe = !modeset &&
-                       to_intel_crtc_state(crtc->state)->update_pipe;
-               unsigned long put_domains = 0;
-
-               if (modeset)
-                       intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
+               struct intel_crtc_state *pipe_config =
+                       to_intel_crtc_state(crtc->state);
+               bool update_pipe = !modeset && pipe_config->update_pipe;
 
                if (modeset && crtc->state->active) {
                        update_scanline_offset(to_intel_crtc(crtc));
                        dev_priv->display.crtc_enable(crtc);
                }
 
-               if (update_pipe) {
-                       put_domains = modeset_get_crtc_power_domains(crtc);
-
-                       /* make sure intel_modeset_check_state runs */
-                       hw_check = true;
-               }
-
                if (!modeset)
-                       intel_pre_plane_update(intel_crtc);
+                       intel_pre_plane_update(to_intel_crtc_state(crtc_state));
 
                if (crtc->state->active && intel_crtc->atomic.update_fbc)
                        intel_fbc_enable(intel_crtc);
@@ -13577,18 +13780,38 @@ static int intel_atomic_commit(struct drm_device *dev,
                    (crtc->state->planes_changed || update_pipe))
                        drm_atomic_helper_commit_planes_on_crtc(crtc_state);
 
-               if (put_domains)
-                       modeset_put_power_domains(dev_priv, put_domains);
+               if (pipe_config->base.active && needs_vblank_wait(pipe_config))
+                       crtc_vblank_mask |= 1 << i;
+       }
+
+       /* FIXME: add subpixel order */
 
-               intel_post_plane_update(intel_crtc);
+       if (!state->legacy_cursor_update)
+               intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
 
-               if (modeset)
-                       intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               intel_post_plane_update(to_intel_crtc(crtc));
+
+               if (put_domains[i])
+                       modeset_put_power_domains(dev_priv, put_domains[i]);
        }
 
-       /* FIXME: add subpixel order */
+       if (intel_state->modeset)
+               intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
+
+       /*
+        * Now that the vblank has passed, we can go ahead and program the
+        * optimal watermarks on platforms that need two-step watermark
+        * programming.
+        *
+        * TODO: Move this (and other cleanup) to an async worker eventually.
+        */
+       for_each_crtc_in_state(state, crtc, crtc_state, i) {
+               intel_cstate = to_intel_crtc_state(crtc->state);
 
-       drm_atomic_helper_wait_for_vblanks(dev, state);
+               if (dev_priv->display.optimize_watermarks)
+                       dev_priv->display.optimize_watermarks(intel_cstate);
+       }
 
        mutex_lock(&dev->struct_mutex);
        drm_atomic_helper_cleanup_planes(dev, state);
@@ -13670,7 +13893,7 @@ static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
 {
        uint32_t val;
 
-       if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
+       if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
                return false;
 
        val = I915_READ(PCH_DPLL(pll->id));
@@ -13678,6 +13901,8 @@ static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
        hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
        hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
 
+       intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
+
        return val & DPLL_VCO_ENABLE;
 }
 
@@ -13837,7 +14062,7 @@ intel_prepare_plane_fb(struct drm_plane *plane,
                if (ret)
                        DRM_DEBUG_KMS("failed to attach phys object\n");
        } else {
-               ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
+               ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
        }
 
        if (ret == 0) {
@@ -13881,7 +14106,7 @@ intel_cleanup_plane_fb(struct drm_plane *plane,
 
        if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
            !INTEL_INFO(dev)->cursor_needs_physical))
-               intel_unpin_fb_obj(old_state->fb, old_state);
+               intel_unpin_fb_obj(old_state->fb, old_state->rotation);
 
        /* prepare_fb aborted? */
        if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
@@ -13889,7 +14114,6 @@ intel_cleanup_plane_fb(struct drm_plane *plane,
                i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
 
        i915_gem_request_assign(&old_intel_state->wait_req, NULL);
-
 }
 
 int
@@ -14821,6 +15045,8 @@ static int intel_framebuffer_init(struct drm_device *dev,
        drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
        intel_fb->obj = obj;
 
+       intel_fill_fb_info(dev_priv, &intel_fb->base);
+
        ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
        if (ret) {
                DRM_ERROR("framebuffer init failed %d\n", ret);
@@ -15258,7 +15484,7 @@ static void sanitize_watermarks(struct drm_device *dev)
        int i;
 
        /* Only supported on platforms that use atomic watermark design */
-       if (!dev_priv->display.program_watermarks)
+       if (!dev_priv->display.optimize_watermarks)
                return;
 
        /*
@@ -15279,6 +15505,13 @@ retry:
        if (WARN_ON(IS_ERR(state)))
                goto fail;
 
+       /*
+        * Hardware readout is the only time we don't want to calculate
+        * intermediate watermarks (since we don't trust the current
+        * watermarks).
+        */
+       to_intel_atomic_state(state)->skip_intermediate_wm = true;
+
        ret = intel_atomic_check(dev, state);
        if (ret) {
                /*
@@ -15301,7 +15534,8 @@ retry:
        for_each_crtc_in_state(state, crtc, cstate, i) {
                struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
 
-               dev_priv->display.program_watermarks(cs);
+               cs->wm.need_postvbl_update = true;
+               dev_priv->display.optimize_watermarks(cs);
        }
 
        drm_atomic_state_free(state);
@@ -15396,6 +15630,7 @@ void intel_modeset_init(struct drm_device *dev)
        }
 
        intel_update_czclk(dev_priv);
+       intel_update_rawclk(dev_priv);
        intel_update_cdclk(dev);
 
        intel_shared_dpll_init(dev);
@@ -15493,6 +15728,17 @@ static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
        return false;
 }
 
+static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
+{
+       struct drm_device *dev = encoder->base.dev;
+       struct intel_connector *connector;
+
+       for_each_connector_on_encoder(dev, &encoder->base, connector)
+               return true;
+
+       return false;
+}
+
 static void intel_sanitize_crtc(struct intel_crtc *crtc)
 {
        struct drm_device *dev = crtc->base.dev;
@@ -15567,6 +15813,7 @@ static void intel_sanitize_crtc(struct intel_crtc *crtc)
                crtc->base.state->active = crtc->active;
                crtc->base.enabled = crtc->active;
                crtc->base.state->connector_mask = 0;
+               crtc->base.state->encoder_mask = 0;
 
                /* Because we only establish the connector -> encoder ->
                 * crtc links if something is active, this means the
@@ -15602,7 +15849,6 @@ static void intel_sanitize_encoder(struct intel_encoder *encoder)
 {
        struct intel_connector *connector;
        struct drm_device *dev = encoder->base.dev;
-       bool active = false;
 
        /* We need to check both for a crtc link (meaning that the
         * encoder is active and trying to read from a pipe) and the
@@ -15610,15 +15856,7 @@ static void intel_sanitize_encoder(struct intel_encoder *encoder)
        bool has_active_crtc = encoder->base.crtc &&
                to_intel_crtc(encoder->base.crtc)->active;
 
-       for_each_intel_connector(dev, connector) {
-               if (connector->base.encoder != &encoder->base)
-                       continue;
-
-               active = true;
-               break;
-       }
-
-       if (active && !has_active_crtc) {
+       if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
                DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
                              encoder->base.base.id,
                              encoder->base.name);
@@ -15673,10 +15911,12 @@ void i915_redisable_vga(struct drm_device *dev)
         * level, just check if the power well is enabled instead of trying to
         * follow the "don't touch the power well if we don't need it" policy
         * the rest of the driver uses. */
-       if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
+       if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
                return;
 
        i915_redisable_vga_power_on(dev);
+
+       intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
 }
 
 static bool primary_get_hw_state(struct intel_plane *plane)
@@ -15806,6 +16046,8 @@ static void intel_modeset_readout_hw_state(struct drm_device *dev)
                                 */
                                encoder->base.crtc->state->connector_mask |=
                                        1 << drm_connector_index(&connector->base);
+                               encoder->base.crtc->state->encoder_mask |=
+                                       1 << drm_encoder_index(&encoder->base);
                        }
 
                } else {
@@ -15849,6 +16091,8 @@ static void intel_modeset_readout_hw_state(struct drm_device *dev)
                        drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
                        update_scanline_offset(crtc);
                }
+
+               intel_pipe_config_sanity_check(dev_priv, crtc->config);
        }
 }
 
@@ -15902,7 +16146,7 @@ intel_modeset_setup_hw_state(struct drm_device *dev)
        for_each_intel_crtc(dev, crtc) {
                unsigned long put_domains;
 
-               put_domains = modeset_get_crtc_power_domains(&crtc->base);
+               put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
                if (WARN_ON(put_domains))
                        modeset_put_power_domains(dev_priv, put_domains);
        }
@@ -15913,54 +16157,65 @@ intel_modeset_setup_hw_state(struct drm_device *dev)
 
 void intel_display_resume(struct drm_device *dev)
 {
-       struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
-       struct intel_connector *conn;
-       struct intel_plane *plane;
-       struct drm_crtc *crtc;
+       struct drm_i915_private *dev_priv = to_i915(dev);
+       struct drm_atomic_state *state = dev_priv->modeset_restore_state;
+       struct drm_modeset_acquire_ctx ctx;
        int ret;
+       bool setup = false;
 
-       if (!state)
-               return;
-
-       state->acquire_ctx = dev->mode_config.acquire_ctx;
+       dev_priv->modeset_restore_state = NULL;
 
-       /* preserve complete old state, including dpll */
-       intel_atomic_get_shared_dpll_state(state);
+       /*
+        * This is a cludge because with real atomic modeset mode_config.mutex
+        * won't be taken. Unfortunately some probed state like
+        * audio_codec_enable is still protected by mode_config.mutex, so lock
+        * it here for now.
+        */
+       mutex_lock(&dev->mode_config.mutex);
+       drm_modeset_acquire_init(&ctx, 0);
 
-       for_each_crtc(dev, crtc) {
-               struct drm_crtc_state *crtc_state =
-                       drm_atomic_get_crtc_state(state, crtc);
+retry:
+       ret = drm_modeset_lock_all_ctx(dev, &ctx);
 
-               ret = PTR_ERR_OR_ZERO(crtc_state);
-               if (ret)
-                       goto err;
+       if (ret == 0 && !setup) {
+               setup = true;
 
-               /* force a restore */
-               crtc_state->mode_changed = true;
+               intel_modeset_setup_hw_state(dev);
+               i915_redisable_vga(dev);
        }
 
-       for_each_intel_plane(dev, plane) {
-               ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
-               if (ret)
-                       goto err;
-       }
+       if (ret == 0 && state) {
+               struct drm_crtc_state *crtc_state;
+               struct drm_crtc *crtc;
+               int i;
 
-       for_each_intel_connector(dev, conn) {
-               ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
-               if (ret)
-                       goto err;
+               state->acquire_ctx = &ctx;
+
+               for_each_crtc_in_state(state, crtc, crtc_state, i) {
+                       /*
+                        * Force recalculation even if we restore
+                        * current state. With fast modeset this may not result
+                        * in a modeset when the state is compatible.
+                        */
+                       crtc_state->mode_changed = true;
+               }
+
+               ret = drm_atomic_commit(state);
        }
 
-       intel_modeset_setup_hw_state(dev);
+       if (ret == -EDEADLK) {
+               drm_modeset_backoff(&ctx);
+               goto retry;
+       }
 
-       i915_redisable_vga(dev);
-       ret = drm_atomic_commit(state);
-       if (!ret)
-               return;
+       drm_modeset_drop_locks(&ctx);
+       drm_modeset_acquire_fini(&ctx);
+       mutex_unlock(&dev->mode_config.mutex);
 
-err:
-       DRM_ERROR("Restoring old state failed with %i\n", ret);
-       drm_atomic_state_free(state);
+       if (ret) {
+               DRM_ERROR("Restoring old state failed with %i\n", ret);
+               drm_atomic_state_free(state);
+       }
 }
 
 void intel_modeset_gem_init(struct drm_device *dev)
@@ -15969,9 +16224,7 @@ void intel_modeset_gem_init(struct drm_device *dev)
        struct drm_i915_gem_object *obj;
        int ret;
 
-       mutex_lock(&dev->struct_mutex);
        intel_init_gt_powersave(dev);
-       mutex_unlock(&dev->struct_mutex);
 
        intel_modeset_init_hw(dev);
 
@@ -15988,9 +16241,8 @@ void intel_modeset_gem_init(struct drm_device *dev)
                        continue;
 
                mutex_lock(&dev->struct_mutex);
-               ret = intel_pin_and_fence_fb_obj(c->primary,
-                                                c->primary->fb,
-                                                c->primary->state);
+               ret = intel_pin_and_fence_fb_obj(c->primary->fb,
+                                                c->primary->state->rotation);
                mutex_unlock(&dev->struct_mutex);
                if (ret) {
                        DRM_ERROR("failed to pin boot fb on pipe %d\n",
@@ -16051,9 +16303,7 @@ void intel_modeset_cleanup(struct drm_device *dev)
 
        intel_cleanup_overlay(dev);
 
-       mutex_lock(&dev->struct_mutex);
        intel_cleanup_gt_powersave(dev);
-       mutex_unlock(&dev->struct_mutex);
 
        intel_teardown_gmbus(dev);
 }
@@ -16286,24 +16536,3 @@ intel_display_print_error_state(struct drm_i915_error_state_buf *m,
                err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
        }
 }
-
-void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
-{
-       struct intel_crtc *crtc;
-
-       for_each_intel_crtc(dev, crtc) {
-               struct intel_unpin_work *work;
-
-               spin_lock_irq(&dev->event_lock);
-
-               work = crtc->unpin_work;
-
-               if (work && work->event &&
-                   work->event->base.file_priv == file) {
-                       kfree(work->event);
-                       work->event = NULL;
-               }
-
-               spin_unlock_irq(&dev->event_lock);
-       }
-}