]> Pileus Git - ~andy/linux/blobdiff - drivers/gpu/drm/i915/intel_display.c
drm/i915: WARN if the pipe won't turn off
[~andy/linux] / drivers / gpu / drm / i915 / intel_display.c
index 33a519117e2f0689a7cfefb0424515657affe300..453bc7cf1841ac18f3fcfdf364e2b360472dec26 100644 (file)
@@ -1006,7 +1006,7 @@ void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
                /* Wait for the Pipe State to go off */
                if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
                             100))
-                       DRM_DEBUG_KMS("pipe_off wait timed out\n");
+                       WARN(1, "pipe_off wait timed out\n");
        } else {
                u32 last_line, line_mask;
                int reg = PIPEDSL(pipe);
@@ -1024,7 +1024,7 @@ void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
                } while (((I915_READ(reg) & line_mask) != last_line) &&
                         time_after(timeout, jiffies));
                if (time_after(jiffies, timeout))
-                       DRM_DEBUG_KMS("pipe_off wait timed out\n");
+                       WARN(1, "pipe_off wait timed out\n");
        }
 }
 
@@ -2201,16 +2201,17 @@ intel_finish_fb(struct drm_framebuffer *old_fb)
 
 static int
 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
-                   struct drm_framebuffer *old_fb)
+                   struct drm_framebuffer *fb)
 {
        struct drm_device *dev = crtc->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_i915_master_private *master_priv;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct drm_framebuffer *old_fb;
        int ret;
 
        /* no fb bound */
-       if (!crtc->fb) {
+       if (!fb) {
                DRM_ERROR("No FB bound\n");
                return 0;
        }
@@ -2224,7 +2225,7 @@ intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
 
        mutex_lock(&dev->struct_mutex);
        ret = intel_pin_and_fence_fb_obj(dev,
-                                        to_intel_framebuffer(crtc->fb)->obj,
+                                        to_intel_framebuffer(fb)->obj,
                                         NULL);
        if (ret != 0) {
                mutex_unlock(&dev->struct_mutex);
@@ -2232,17 +2233,20 @@ intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
                return ret;
        }
 
-       if (old_fb)
-               intel_finish_fb(old_fb);
+       if (crtc->fb)
+               intel_finish_fb(crtc->fb);
 
-       ret = dev_priv->display.update_plane(crtc, crtc->fb, x, y);
+       ret = dev_priv->display.update_plane(crtc, fb, x, y);
        if (ret) {
-               intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
+               intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
                mutex_unlock(&dev->struct_mutex);
                DRM_ERROR("failed to update base address\n");
                return ret;
        }
 
+       old_fb = crtc->fb;
+       crtc->fb = fb;
+
        if (old_fb) {
                intel_wait_for_vblank(dev, intel_crtc->pipe);
                intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
@@ -3215,6 +3219,8 @@ static void ironlake_crtc_enable(struct drm_crtc *crtc)
        u32 temp;
        bool is_pch_port;
 
+       WARN_ON(!crtc->enabled);
+
        /* XXX: For compatability with the crtc helper code, call the encoder's
         * enable function unconditionally for now. */
        if (intel_crtc->active)
@@ -3391,6 +3397,8 @@ static void i9xx_crtc_enable(struct drm_crtc *crtc)
        int pipe = intel_crtc->pipe;
        int plane = intel_crtc->plane;
 
+       WARN_ON(!crtc->enabled);
+
        /* XXX: For compatability with the crtc helper code, call the encoder's
         * enable function unconditionally for now. */
        if (intel_crtc->active)
@@ -3454,34 +3462,13 @@ static void i9xx_crtc_off(struct drm_crtc *crtc)
 {
 }
 
-/**
- * Sets the power management mode of the pipe and plane.
- */
-void intel_crtc_update_dpms(struct drm_crtc *crtc)
+static void intel_crtc_update_sarea(struct drm_crtc *crtc,
+                                   bool enabled)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_i915_master_private *master_priv;
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       struct intel_encoder *intel_encoder;
        int pipe = intel_crtc->pipe;
-       bool enabled, enable = false;
-       int mode;
-
-       for_each_encoder_on_crtc(dev, crtc, intel_encoder)
-               enable |= intel_encoder->connectors_active;
-
-       mode = enable ? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF;
-
-       if (intel_crtc->dpms_mode == mode)
-               return;
-
-       intel_crtc->dpms_mode = mode;
-
-       if (enable)
-               dev_priv->display.crtc_enable(crtc);
-       else
-               dev_priv->display.crtc_disable(crtc);
 
        if (!dev->primary->master)
                return;
@@ -3490,8 +3477,6 @@ void intel_crtc_update_dpms(struct drm_crtc *crtc)
        if (!master_priv->sarea_priv)
                return;
 
-       enabled = crtc->enabled && enable;
-
        switch (pipe) {
        case 0:
                master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
@@ -3507,14 +3492,42 @@ void intel_crtc_update_dpms(struct drm_crtc *crtc)
        }
 }
 
+/**
+ * Sets the power management mode of the pipe and plane.
+ */
+void intel_crtc_update_dpms(struct drm_crtc *crtc)
+{
+       struct drm_device *dev = crtc->dev;
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       struct intel_encoder *intel_encoder;
+       bool enable = false;
+
+       for_each_encoder_on_crtc(dev, crtc, intel_encoder)
+               enable |= intel_encoder->connectors_active;
+
+       if (enable)
+               dev_priv->display.crtc_enable(crtc);
+       else
+               dev_priv->display.crtc_disable(crtc);
+
+       intel_crtc_update_sarea(crtc, enable);
+}
+
+static void intel_crtc_noop(struct drm_crtc *crtc)
+{
+}
+
 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->disable is only called when we have no encoders, hence this
-        * will disable the pipe. */
-       intel_crtc_update_dpms(crtc);
+       /* crtc should still be enabled when we disable it. */
+       WARN_ON(!crtc->enabled);
+
+       dev_priv->display.crtc_disable(crtc);
+       intel_crtc_update_sarea(crtc, false);
        dev_priv->display.off(crtc);
 
        assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
@@ -3524,6 +3537,19 @@ static void intel_crtc_disable(struct drm_crtc *crtc)
                mutex_lock(&dev->struct_mutex);
                intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
                mutex_unlock(&dev->struct_mutex);
+               crtc->fb = NULL;
+       }
+
+       /* 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;
        }
 }
 
@@ -3531,13 +3557,6 @@ void intel_encoder_noop(struct drm_encoder *encoder)
 {
 }
 
-void intel_encoder_disable(struct drm_encoder *encoder)
-{
-       struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
-
-       intel_encoder->disable(intel_encoder);
-}
-
 void intel_encoder_destroy(struct drm_encoder *encoder)
 {
        struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
@@ -3562,6 +3581,41 @@ void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
        }
 }
 
+/* Cross check the actual hw state with our own modeset state tracking (and it's
+ * internal consistency). */
+void intel_connector_check_state(struct intel_connector *connector)
+{
+       if (connector->get_hw_state(connector)) {
+               struct intel_encoder *encoder = connector->encoder;
+               struct drm_crtc *crtc;
+               bool encoder_enabled;
+               enum pipe pipe;
+
+               DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
+                             connector->base.base.id,
+                             drm_get_connector_name(&connector->base));
+
+               WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
+                    "wrong connector dpms state\n");
+               WARN(connector->base.encoder != &encoder->base,
+                    "active connector not linked to encoder\n");
+               WARN(!encoder->connectors_active,
+                    "encoder->connectors_active not set\n");
+
+               encoder_enabled = encoder->get_hw_state(encoder, &pipe);
+               WARN(!encoder_enabled, "encoder not enabled\n");
+               if (WARN_ON(!encoder->base.crtc))
+                       return;
+
+               crtc = encoder->base.crtc;
+
+               WARN(!crtc->enabled, "crtc not enabled\n");
+               WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
+               WARN(pipe != to_intel_crtc(crtc)->pipe,
+                    "encoder active on the wrong pipe\n");
+       }
+}
+
 /* Even simpler default implementation, if there's really no special case to
  * consider. */
 void intel_connector_dpms(struct drm_connector *connector, int mode)
@@ -3582,6 +3636,19 @@ void intel_connector_dpms(struct drm_connector *connector, int mode)
                intel_encoder_dpms(encoder, mode);
        else
                encoder->connectors_active = false;
+
+       intel_connector_check_state(to_intel_connector(connector));
+}
+
+/* Simple connector->get_hw_state implementation for encoders that support only
+ * one connector and no cloning and hence the encoder state determines the state
+ * of the connector. */
+bool intel_connector_get_hw_state(struct intel_connector *connector)
+{
+       enum pipe pipe = 0;
+       struct intel_encoder *encoder = connector->encoder;
+
+       return encoder->get_hw_state(encoder, &pipe);
 }
 
 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
@@ -3737,6 +3804,7 @@ static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  * true if they don't match).
  */
 static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
+                                        struct drm_framebuffer *fb,
                                         unsigned int *pipe_bpp,
                                         struct drm_display_mode *mode)
 {
@@ -3806,7 +3874,7 @@ static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
         * also stays within the max display bpc discovered above.
         */
 
-       switch (crtc->fb->depth) {
+       switch (fb->depth) {
        case 8:
                bpc = 8; /* since we go through a colormap */
                break;
@@ -4225,7 +4293,7 @@ static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
                              struct drm_display_mode *mode,
                              struct drm_display_mode *adjusted_mode,
                              int x, int y,
-                             struct drm_framebuffer *old_fb)
+                             struct drm_framebuffer *fb)
 {
        struct drm_device *dev = crtc->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -4415,7 +4483,7 @@ static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
        I915_WRITE(DSPCNTR(plane), dspcntr);
        POSTING_READ(DSPCNTR(plane));
 
-       ret = intel_pipe_set_base(crtc, x, y, old_fb);
+       ret = intel_pipe_set_base(crtc, x, y, fb);
 
        intel_update_watermarks(dev);
 
@@ -4573,7 +4641,7 @@ static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
                                  struct drm_display_mode *mode,
                                  struct drm_display_mode *adjusted_mode,
                                  int x, int y,
-                                 struct drm_framebuffer *old_fb)
+                                 struct drm_framebuffer *fb)
 {
        struct drm_device *dev = crtc->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -4693,7 +4761,7 @@ static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
        /* determine panel color depth */
        temp = I915_READ(PIPECONF(pipe));
        temp &= ~PIPE_BPC_MASK;
-       dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
+       dither = intel_choose_pipe_bpp_dither(crtc, fb, &pipe_bpp, mode);
        switch (pipe_bpp) {
        case 18:
                temp |= PIPE_6BPC;
@@ -4962,7 +5030,7 @@ static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
        I915_WRITE(DSPCNTR(plane), dspcntr);
        POSTING_READ(DSPCNTR(plane));
 
-       ret = intel_pipe_set_base(crtc, x, y, old_fb);
+       ret = intel_pipe_set_base(crtc, x, y, fb);
 
        intel_update_watermarks(dev);
 
@@ -4975,7 +5043,7 @@ static int intel_crtc_mode_set(struct drm_crtc *crtc,
                               struct drm_display_mode *mode,
                               struct drm_display_mode *adjusted_mode,
                               int x, int y,
-                              struct drm_framebuffer *old_fb)
+                              struct drm_framebuffer *fb)
 {
        struct drm_device *dev = crtc->dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -4986,14 +5054,9 @@ static int intel_crtc_mode_set(struct drm_crtc *crtc,
        drm_vblank_pre_modeset(dev, pipe);
 
        ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
-                                             x, y, old_fb);
+                                             x, y, fb);
        drm_vblank_post_modeset(dev, pipe);
 
-       if (ret)
-               intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
-       else
-               intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
-
        return ret;
 }
 
@@ -5427,8 +5490,6 @@ static int intel_crtc_cursor_set(struct drm_crtc *crtc,
        uint32_t addr;
        int ret;
 
-       DRM_DEBUG_KMS("\n");
-
        /* if we want to turn off the cursor ignore width and height */
        if (!handle) {
                DRM_DEBUG_KMS("cursor off\n");
@@ -5685,7 +5746,7 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
        struct drm_encoder *encoder = &intel_encoder->base;
        struct drm_crtc *crtc = NULL;
        struct drm_device *dev = encoder->dev;
-       struct drm_framebuffer *old_fb;
+       struct drm_framebuffer *fb;
        int i = -1;
 
        DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
@@ -5746,8 +5807,6 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
        if (!mode)
                mode = &load_detect_mode;
 
-       old_fb = crtc->fb;
-
        /* We need a framebuffer large enough to accommodate all accesses
         * that the plane may generate whilst we perform load detection.
         * We can not rely on the fbcon either being present (we get called
@@ -5755,19 +5814,19 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
         * not even exist) or that it is large enough to satisfy the
         * requested mode.
         */
-       crtc->fb = mode_fits_in_fbdev(dev, mode);
-       if (crtc->fb == NULL) {
+       fb = mode_fits_in_fbdev(dev, mode);
+       if (fb == NULL) {
                DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
-               crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
-               old->release_fb = crtc->fb;
+               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(crtc->fb)) {
+       if (IS_ERR(fb)) {
                DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
                goto fail;
        }
 
-       if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
+       if (!intel_set_mode(crtc, mode, 0, 0, fb)) {
                DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
                if (old->release_fb)
                        old->release_fb->funcs->destroy(old->release_fb);
@@ -5781,7 +5840,6 @@ bool intel_get_load_detect_pipe(struct drm_connector *connector,
 fail:
        connector->encoder = NULL;
        encoder->crtc = NULL;
-       crtc->fb = old_fb;
        return false;
 }
 
@@ -6522,74 +6580,34 @@ free_work:
        return ret;
 }
 
-static void intel_sanitize_modesetting(struct drm_device *dev,
-                                      int pipe, int plane)
-{
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       u32 reg, val;
-       int i;
-
-       /* Clear any frame start delays used for debugging left by the BIOS */
-       for_each_pipe(i) {
-               reg = PIPECONF(i);
-               I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
-       }
-
-       if (HAS_PCH_SPLIT(dev))
-               return;
-
-       /* Who knows what state these registers were left in by the BIOS or
-        * grub?
-        *
-        * If we leave the registers in a conflicting state (e.g. with the
-        * display plane reading from the other pipe than the one we intend
-        * to use) then when we attempt to teardown the active mode, we will
-        * not disable the pipes and planes in the correct order -- leaving
-        * a plane reading from a disabled pipe and possibly leading to
-        * undefined behaviour.
-        */
-
-       reg = DSPCNTR(plane);
-       val = I915_READ(reg);
-
-       if ((val & DISPLAY_PLANE_ENABLE) == 0)
-               return;
-       if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
-               return;
+static struct drm_crtc_helper_funcs intel_helper_funcs = {
+       .mode_set_base_atomic = intel_pipe_set_base_atomic,
+       .load_lut = intel_crtc_load_lut,
+       .disable = intel_crtc_noop,
+};
 
-       /* This display plane is active and attached to the other CPU pipe. */
-       pipe = !pipe;
+bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
+{
+       struct intel_encoder *other_encoder;
+       struct drm_crtc *crtc = &encoder->new_crtc->base;
 
-       /* Disable the plane and wait for it to stop reading from the pipe. */
-       intel_disable_plane(dev_priv, plane, pipe);
-       intel_disable_pipe(dev_priv, pipe);
-}
+       if (WARN_ON(!crtc))
+               return false;
 
-static void intel_crtc_reset(struct drm_crtc *crtc)
-{
-       struct drm_device *dev = crtc->dev;
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       list_for_each_entry(other_encoder,
+                           &crtc->dev->mode_config.encoder_list,
+                           base.head) {
 
-       /* Reset flags back to the 'unknown' status so that they
-        * will be correctly set on the initial modeset.
-        */
-       intel_crtc->dpms_mode = -1;
+               if (&other_encoder->new_crtc->base != crtc ||
+                   encoder == other_encoder)
+                       continue;
+               else
+                       return true;
+       }
 
-       /* We need to fix up any BIOS configuration that conflicts with
-        * our expectations.
-        */
-       intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
+       return false;
 }
 
-static struct drm_crtc_helper_funcs intel_helper_funcs = {
-       .mode_fixup = intel_crtc_mode_fixup,
-       .mode_set = intel_crtc_mode_set,
-       .mode_set_base = intel_pipe_set_base,
-       .mode_set_base_atomic = intel_pipe_set_base_atomic,
-       .load_lut = intel_crtc_load_lut,
-       .disable = intel_crtc_disable,
-};
-
 static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
                                  struct drm_crtc *crtc)
 {
@@ -6612,118 +6630,379 @@ static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
        return false;
 }
 
-static int
-intel_crtc_helper_disable(struct drm_crtc *crtc)
+static void
+intel_crtc_prepare_encoders(struct drm_device *dev)
+{
+       struct intel_encoder *encoder;
+
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
+               /* Disable unused encoders */
+               if (encoder->base.crtc == NULL)
+                       encoder->disable(encoder);
+       }
+}
+
+/**
+ * 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_encoder *encoder;
+       struct intel_connector *connector;
+
+       list_for_each_entry(connector, &dev->mode_config.connector_list,
+                           base.head) {
+               connector->new_encoder =
+                       to_intel_encoder(connector->base.encoder);
+       }
+
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list,
+                           base.head) {
+               encoder->new_crtc =
+                       to_intel_crtc(encoder->base.crtc);
+       }
+}
+
+/**
+ * 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_encoder *encoder;
+       struct intel_connector *connector;
+
+       list_for_each_entry(connector, &dev->mode_config.connector_list,
+                           base.head) {
+               connector->base.encoder = &connector->new_encoder->base;
+       }
+
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list,
+                           base.head) {
+               encoder->base.crtc = &encoder->new_crtc->base;
+       }
+}
+
+static struct drm_display_mode *
+intel_modeset_adjusted_mode(struct drm_crtc *crtc,
+                           struct drm_display_mode *mode)
 {
        struct drm_device *dev = crtc->dev;
-       struct drm_connector *connector;
-       struct drm_encoder *encoder;
+       struct drm_display_mode *adjusted_mode;
+       struct drm_encoder_helper_funcs *encoder_funcs;
+       struct intel_encoder *encoder;
 
-       /* Decouple all encoders and their attached connectors from this crtc */
-       list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
-               if (encoder->crtc != crtc)
-                       continue;
+       adjusted_mode = drm_mode_duplicate(dev, mode);
+       if (!adjusted_mode)
+               return ERR_PTR(-ENOMEM);
 
-               list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
-                       if (connector->encoder != encoder)
-                               continue;
+       /* Pass our mode to the connectors and the CRTC to give them a chance to
+        * adjust it according to limitations or connector properties, and also
+        * a chance to reject the mode entirely.
+        */
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list,
+                           base.head) {
 
-                       connector->encoder = NULL;
+               if (&encoder->new_crtc->base != crtc)
+                       continue;
+               encoder_funcs = encoder->base.helper_private;
+               if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
+                                               adjusted_mode))) {
+                       DRM_DEBUG_KMS("Encoder fixup failed\n");
+                       goto fail;
                }
        }
 
-       drm_helper_disable_unused_functions(dev);
-       return 0;
+       if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
+               DRM_DEBUG_KMS("CRTC fixup failed\n");
+               goto fail;
+       }
+       DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
+
+       return adjusted_mode;
+fail:
+       drm_mode_destroy(dev, adjusted_mode);
+       return ERR_PTR(-EINVAL);
 }
 
-static int intel_crtc_set_config(struct drm_mode_set *set)
+/* 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 drm_device *dev;
-       struct drm_crtc *save_crtcs, *new_crtc, *crtc;
-       struct drm_encoder *save_encoders, *new_encoder, *encoder;
-       struct drm_framebuffer *old_fb = NULL;
-       bool mode_changed = false; /* if true do a full mode set */
-       bool fb_changed = false; /* if true and !mode_changed just do a flip */
-       struct drm_connector *save_connectors, *connector;
-       int count = 0, ro, fail = 0;
-       struct drm_crtc_helper_funcs *crtc_funcs;
-       struct drm_mode_set save_set;
-       int ret;
-       int i;
+       struct intel_crtc *intel_crtc;
+       struct drm_device *dev = crtc->dev;
+       struct intel_encoder *encoder;
+       struct intel_connector *connector;
+       struct drm_crtc *tmp_crtc;
 
-       DRM_DEBUG_KMS("\n");
+       *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
 
-       if (!set)
-               return -EINVAL;
+       /* 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. */
+       list_for_each_entry(connector, &dev->mode_config.connector_list,
+                           base.head) {
+               if (connector->base.encoder == &connector->new_encoder->base)
+                       continue;
 
-       if (!set->crtc)
-               return -EINVAL;
+               if (connector->base.encoder) {
+                       tmp_crtc = connector->base.encoder->crtc;
 
-       if (!set->crtc->helper_private)
-               return -EINVAL;
+                       *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
+               }
 
-       crtc_funcs = set->crtc->helper_private;
+               if (connector->new_encoder)
+                       *prepare_pipes |=
+                               1 << connector->new_encoder->new_crtc->pipe;
+       }
 
-       if (!set->mode)
-               set->fb = NULL;
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list,
+                           base.head) {
+               if (encoder->base.crtc == &encoder->new_crtc->base)
+                       continue;
 
-       if (set->fb) {
-               DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
-                               set->crtc->base.id, set->fb->base.id,
-                               (int)set->num_connectors, set->x, set->y);
-       } else {
-               DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
-               return intel_crtc_helper_disable(set->crtc);
+               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;
        }
 
-       dev = set->crtc->dev;
+       /* Check for any pipes that will be fully disabled ... */
+       list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
+                           base.head) {
+               bool used = false;
 
-       /* Allocate space for the backup of all (non-pointer) crtc, encoder and
-        * connector data. */
-       save_crtcs = kzalloc(dev->mode_config.num_crtc *
-                            sizeof(struct drm_crtc), GFP_KERNEL);
-       if (!save_crtcs)
-               return -ENOMEM;
+               /* Don't try to disable disabled crtcs. */
+               if (!intel_crtc->base.enabled)
+                       continue;
 
-       save_encoders = kzalloc(dev->mode_config.num_encoder *
-                               sizeof(struct drm_encoder), GFP_KERNEL);
-       if (!save_encoders) {
-               kfree(save_crtcs);
-               return -ENOMEM;
+               list_for_each_entry(encoder, &dev->mode_config.encoder_list,
+                                   base.head) {
+                       if (encoder->new_crtc == intel_crtc)
+                               used = true;
+               }
+
+               if (!used)
+                       *disable_pipes |= 1 << intel_crtc->pipe;
        }
 
-       save_connectors = kzalloc(dev->mode_config.num_connector *
-                               sizeof(struct drm_connector), GFP_KERNEL);
-       if (!save_connectors) {
-               kfree(save_crtcs);
-               kfree(save_encoders);
-               return -ENOMEM;
+
+       /* set_mode is also used to update properties on life display pipes. */
+       intel_crtc = to_intel_crtc(crtc);
+       if (crtc->enabled)
+               *prepare_pipes |= 1 << intel_crtc->pipe;
+
+       /* We only support modeset on one single crtc, hence we need to do that
+        * only for the passed in crtc iff we change anything else than just
+        * disable crtcs.
+        *
+        * This is actually not true, to be fully compatible with the old crtc
+        * helper we automatically disable _any_ output (i.e. doesn't need to be
+        * connected to the crtc we're modesetting on) if it's disconnected.
+        * Which is a rather nutty api (since changed the output configuration
+        * without userspace's explicit request can lead to confusion), but
+        * alas. Hence we currently need to modeset on all pipes we prepare. */
+       if (*prepare_pipes)
+               *modeset_pipes = *prepare_pipes;
+
+       /* ... and mask these out. */
+       *modeset_pipes &= ~(*disable_pipes);
+       *prepare_pipes &= ~(*disable_pipes);
+}
+
+#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
+       list_for_each_entry((intel_crtc), \
+                           &(dev)->mode_config.crtc_list, \
+                           base.head) \
+               if (mask & (1 <<(intel_crtc)->pipe)) \
+
+bool intel_set_mode(struct drm_crtc *crtc,
+                   struct drm_display_mode *mode,
+                   int x, int y, struct drm_framebuffer *fb)
+{
+       struct drm_device *dev = crtc->dev;
+       drm_i915_private_t *dev_priv = dev->dev_private;
+       struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
+       struct drm_encoder_helper_funcs *encoder_funcs;
+       struct drm_encoder *encoder;
+       struct intel_crtc *intel_crtc;
+       unsigned disable_pipes, prepare_pipes, modeset_pipes;
+       bool ret = true;
+
+       intel_modeset_affected_pipes(crtc, &modeset_pipes,
+                                    &prepare_pipes, &disable_pipes);
+
+       DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
+                     modeset_pipes, prepare_pipes, disable_pipes);
+
+       for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
+               intel_crtc_disable(&intel_crtc->base);
+
+       intel_modeset_commit_output_state(dev);
+
+       list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
+                           base.head)
+               intel_crtc->base.enabled = drm_helper_crtc_in_use(crtc);
+
+       saved_hwmode = crtc->hwmode;
+       saved_mode = crtc->mode;
+
+       /* Hack: Because we don't (yet) support global modeset on multiple
+        * crtcs, we don't keep track of the new mode for more than one crtc.
+        * Hence simply check whether any bit is set in modeset_pipes in all the
+        * pieces of code that are not yet converted to deal with mutliple crtcs
+        * changing their mode at the same time. */
+       adjusted_mode = NULL;
+       if (modeset_pipes) {
+               adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
+               if (IS_ERR(adjusted_mode)) {
+                       return false;
+               }
+
+               intel_crtc_prepare_encoders(dev);
+       }
+
+       for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
+               dev_priv->display.crtc_disable(&intel_crtc->base);
+
+       if (modeset_pipes) {
+               crtc->mode = *mode;
+               crtc->x = x;
+               crtc->y = y;
        }
 
+       /* 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) {
+               ret = !intel_crtc_mode_set(&intel_crtc->base,
+                                          mode, adjusted_mode,
+                                          x, y, fb);
+               if (!ret)
+                   goto done;
+
+               list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+
+                       if (encoder->crtc != &intel_crtc->base)
+                               continue;
+
+                       DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
+                               encoder->base.id, drm_get_encoder_name(encoder),
+                               mode->base.id, mode->name);
+                       encoder_funcs = encoder->helper_private;
+                       encoder_funcs->mode_set(encoder, mode, adjusted_mode);
+               }
+       }
+
+       /* Now enable the clocks, plane, pipe, and connectors that we set up. */
+       for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
+               dev_priv->display.crtc_enable(&intel_crtc->base);
+
+       if (modeset_pipes) {
+               /* Store real post-adjustment hardware mode. */
+               crtc->hwmode = *adjusted_mode;
+
+               /* 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);
+       }
+
+       /* FIXME: add subpixel order */
+done:
+       drm_mode_destroy(dev, adjusted_mode);
+       if (!ret && crtc->enabled) {
+               crtc->hwmode = saved_hwmode;
+               crtc->mode = saved_mode;
+       }
+
+       return ret;
+}
+
+#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);
+}
+
+static int intel_set_config_save_state(struct drm_device *dev,
+                                      struct intel_set_config *config)
+{
+       struct drm_encoder *encoder;
+       struct drm_connector *connector;
+       int count;
+
+       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;
-       list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
-               save_crtcs[count++] = *crtc;
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+               config->save_encoder_crtcs[count++] = encoder->crtc;
        }
 
        count = 0;
-       list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
-               save_encoders[count++] = *encoder;
+       list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
+               config->save_connector_encoders[count++] = connector->encoder;
        }
 
+       return 0;
+}
+
+static void intel_set_config_restore_state(struct drm_device *dev,
+                                          struct intel_set_config *config)
+{
+       struct intel_encoder *encoder;
+       struct intel_connector *connector;
+       int count;
+
        count = 0;
-       list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
-               save_connectors[count++] = *connector;
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
+               encoder->new_crtc =
+                       to_intel_crtc(config->save_encoder_crtcs[count++]);
        }
 
-       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->fb;
+       count = 0;
+       list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
+               connector->new_encoder =
+                       to_intel_encoder(config->save_connector_encoders[count++]);
+       }
+}
+
+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 */
@@ -6731,120 +7010,206 @@ static int intel_crtc_set_config(struct drm_mode_set *set)
                /* If we have no fb then treat it as a full mode set */
                if (set->crtc->fb == NULL) {
                        DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
-                       mode_changed = true;
+                       config->mode_changed = true;
                } else if (set->fb == NULL) {
-                       mode_changed = true;
+                       config->mode_changed = true;
                } else if (set->fb->depth != set->crtc->fb->depth) {
-                       mode_changed = true;
+                       config->mode_changed = true;
                } else if (set->fb->bits_per_pixel !=
                           set->crtc->fb->bits_per_pixel) {
-                       mode_changed = true;
+                       config->mode_changed = true;
                } else
-                       fb_changed = true;
+                       config->fb_changed = true;
        }
 
-       if (set->x != set->crtc->x || set->y != set->crtc->y)
-               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);
-               mode_changed = true;
+               config->mode_changed = true;
        }
+}
+
+static int
+intel_modeset_stage_output_state(struct drm_device *dev,
+                                struct drm_mode_set *set,
+                                struct intel_set_config *config)
+{
+       struct drm_crtc *new_crtc;
+       struct intel_connector *connector;
+       struct intel_encoder *encoder;
+       int count, ro;
+
+       /* The upper layers ensure that we either disabl a crtc or have a list
+        * of connectors. For paranoia, double-check this. */
+       WARN_ON(!set->fb && (set->num_connectors != 0));
+       WARN_ON(set->fb && (set->num_connectors == 0));
 
-       /* a) traverse passed in connector list and get encoders for them */
        count = 0;
-       list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
-               struct drm_connector_helper_funcs *connector_funcs =
-                       connector->helper_private;
-               new_encoder = connector->encoder;
+       list_for_each_entry(connector, &dev->mode_config.connector_list,
+                           base.head) {
+               /* Otherwise traverse passed in connector list and get encoders
+                * for them. */
                for (ro = 0; ro < set->num_connectors; ro++) {
-                       if (set->connectors[ro] == connector) {
-                               new_encoder = connector_funcs->best_encoder(connector);
-                               /* if we can't get an encoder for a connector
-                                  we are setting now - then fail */
-                               if (new_encoder == NULL)
-                                       /* don't break so fail path works correct */
-                                       fail = 1;
+                       if (set->connectors[ro] == &connector->base) {
+                               connector->new_encoder = connector->encoder;
                                break;
                        }
                }
 
-               if (new_encoder != connector->encoder) {
+               /* 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;
+
+                       DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
+                               connector->base.base.id,
+                               drm_get_connector_name(&connector->base));
+               }
+
+
+               if (&connector->new_encoder->base != connector->base.encoder) {
                        DRM_DEBUG_KMS("encoder changed, full mode switch\n");
-                       mode_changed = true;
-                       /* If the encoder is reused for another connector, then
-                        * the appropriate crtc will be set later.
-                        */
-                       if (connector->encoder)
-                               connector->encoder->crtc = NULL;
-                       connector->encoder = new_encoder;
+                       config->mode_changed = true;
                }
-       }
 
-       if (fail) {
-               ret = -EINVAL;
-               goto fail;
+               /* Disable all disconnected encoders. */
+               if (connector->base.status == connector_status_disconnected)
+                       connector->new_encoder = NULL;
        }
+       /* connector->new_encoder is now updated for all connectors. */
 
+       /* Update crtc of enabled connectors. */
        count = 0;
-       list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
-               if (!connector->encoder)
+       list_for_each_entry(connector, &dev->mode_config.connector_list,
+                           base.head) {
+               if (!connector->new_encoder)
                        continue;
 
-               if (connector->encoder->crtc == set->crtc)
-                       new_crtc = NULL;
-               else
-                       new_crtc = connector->encoder->crtc;
+               new_crtc = connector->new_encoder->base.crtc;
 
                for (ro = 0; ro < set->num_connectors; ro++) {
-                       if (set->connectors[ro] == connector)
+                       if (set->connectors[ro] == &connector->base)
                                new_crtc = set->crtc;
                }
 
                /* Make sure the new CRTC will work with the encoder */
-               if (new_crtc &&
-                   !intel_encoder_crtc_ok(connector->encoder, new_crtc)) {
-                       ret = -EINVAL;
-                       goto fail;
+               if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
+                                          new_crtc)) {
+                       return -EINVAL;
                }
-               if (new_crtc != connector->encoder->crtc) {
-                       DRM_DEBUG_KMS("crtc changed, full mode switch\n");
-                       mode_changed = true;
-                       connector->encoder->crtc = new_crtc;
+               connector->encoder->new_crtc = to_intel_crtc(new_crtc);
+
+               DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
+                       connector->base.base.id,
+                       drm_get_connector_name(&connector->base),
+                       new_crtc->base.id);
+       }
+
+       /* Check for any encoders that needs to be disabled. */
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list,
+                           base.head) {
+               list_for_each_entry(connector,
+                                   &dev->mode_config.connector_list,
+                                   base.head) {
+                       if (connector->new_encoder == encoder) {
+                               WARN_ON(!connector->new_encoder->new_crtc);
+
+                               goto next_encoder;
+                       }
                }
-               if (new_crtc) {
-                       DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
-                               connector->base.id, drm_get_connector_name(connector),
-                               new_crtc->base.id);
-               } else {
-                       DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
-                               connector->base.id, drm_get_connector_name(connector));
+               encoder->new_crtc = NULL;
+next_encoder:
+               /* 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("crtc changed, full mode switch\n");
+                       config->mode_changed = true;
                }
        }
+       /* Now we've also updated encoder->new_crtc for all encoders. */
 
-       /* mode_set_base is not a required function */
-       if (fb_changed && !crtc_funcs->mode_set_base)
-               mode_changed = true;
+       return 0;
+}
 
-       if (mode_changed) {
-               set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
-               if (set->crtc->enabled) {
+static int intel_crtc_set_config(struct drm_mode_set *set)
+{
+       struct drm_device *dev;
+       struct drm_mode_set save_set;
+       struct intel_set_config *config;
+       int ret;
+       int i;
+
+       BUG_ON(!set);
+       BUG_ON(!set->crtc);
+       BUG_ON(!set->crtc->helper_private);
+
+       if (!set->mode)
+               set->fb = NULL;
+
+       /* The fb helper likes to play gross jokes with ->mode_set_config.
+        * Unfortunately the crtc helper doesn't do much at all for this case,
+        * so we have to cope with this madness until the fb helper is fixed up. */
+       if (set->fb && set->num_connectors == 0)
+               return 0;
+
+       if (set->fb) {
+               DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
+                               set->crtc->base.id, set->fb->base.id,
+                               (int)set->num_connectors, set->x, set->y);
+       } else {
+               DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
+       }
+
+       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->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);
+
+       ret = intel_modeset_stage_output_state(dev, set, config);
+       if (ret)
+               goto fail;
+
+       if (config->mode_changed) {
+               if (set->mode) {
                        DRM_DEBUG_KMS("attempting to set mode from"
                                        " userspace\n");
                        drm_mode_debug_printmodeline(set->mode);
-                       old_fb = set->crtc->fb;
-                       set->crtc->fb = set->fb;
-                       if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
-                                                     set->x, set->y,
-                                                     old_fb)) {
-                               DRM_ERROR("failed to set mode on [CRTC:%d]\n",
-                                         set->crtc->base.id);
-                               set->crtc->fb = old_fb;
-                               ret = -EINVAL;
-                               goto fail;
-                       }
+               }
+
+               if (!intel_set_mode(set->crtc, set->mode,
+                                   set->x, set->y, set->fb)) {
+                       DRM_ERROR("failed to set mode on [CRTC:%d]\n",
+                                 set->crtc->base.id);
+                       ret = -EINVAL;
+                       goto fail;
+               }
+
+               if (set->crtc->enabled) {
                        DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
                        for (i = 0; i < set->num_connectors; i++) {
                                DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
@@ -6852,58 +7217,30 @@ static int intel_crtc_set_config(struct drm_mode_set *set)
                                set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
                        }
                }
-               drm_helper_disable_unused_functions(dev);
-       } else if (fb_changed) {
-               set->crtc->x = set->x;
-               set->crtc->y = set->y;
-
-               old_fb = set->crtc->fb;
-               if (set->crtc->fb != set->fb)
-                       set->crtc->fb = set->fb;
-               ret = crtc_funcs->mode_set_base(set->crtc,
-                                               set->x, set->y, old_fb);
-               if (ret != 0) {
-                       set->crtc->fb = old_fb;
-                       goto fail;
-               }
+       } else if (config->fb_changed) {
+               ret = intel_pipe_set_base(set->crtc,
+                                         set->x, set->y, set->fb);
        }
 
-       kfree(save_connectors);
-       kfree(save_encoders);
-       kfree(save_crtcs);
+       intel_set_config_free(config);
+
        return 0;
 
 fail:
-       /* Restore all previous data. */
-       count = 0;
-       list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
-               *crtc = save_crtcs[count++];
-       }
-
-       count = 0;
-       list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
-               *encoder = save_encoders[count++];
-       }
-
-       count = 0;
-       list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
-               *connector = save_connectors[count++];
-       }
+       intel_set_config_restore_state(dev, config);
 
        /* Try to restore the config */
-       if (mode_changed &&
-           !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
-                                     save_set.y, save_set.fb))
+       if (config->mode_changed &&
+           !intel_set_mode(save_set.crtc, save_set.mode,
+                           save_set.x, save_set.y, save_set.fb))
                DRM_ERROR("failed to restore config after modeset failure\n");
 
-       kfree(save_connectors);
-       kfree(save_encoders);
-       kfree(save_crtcs);
+out_config:
+       intel_set_config_free(config);
        return ret;
 }
 
 static const struct drm_crtc_funcs intel_crtc_funcs = {
-       .reset = intel_crtc_reset,
        .cursor_set = intel_crtc_cursor_set,
        .cursor_move = intel_crtc_cursor_move,
        .gamma_set = intel_crtc_gamma_set,
@@ -6961,13 +7298,8 @@ 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;
 
-       intel_crtc_reset(&intel_crtc->base);
-       intel_crtc->active = true; /* force the pipe off on setup_init_config */
        intel_crtc->bpp = 24; /* default for pre-Ironlake */
 
-       intel_helper_funcs.prepare = dev_priv->display.crtc_disable;
-       intel_helper_funcs.commit = dev_priv->display.crtc_enable;
-
        drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
 }
 
@@ -7173,9 +7505,6 @@ static void intel_setup_outputs(struct drm_device *dev)
                        intel_encoder_clones(encoder);
        }
 
-       /* disable all the possible outputs/crtcs before entering KMS mode */
-       drm_helper_disable_unused_functions(dev);
-
        if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
                ironlake_init_pch_refclk(dev);
 }
@@ -7534,11 +7863,256 @@ void intel_modeset_init(struct drm_device *dev)
        intel_setup_outputs(dev);
 }
 
+static void
+intel_connector_break_all_links(struct intel_connector *connector)
+{
+       connector->base.dpms = DRM_MODE_DPMS_OFF;
+       connector->base.encoder = NULL;
+       connector->encoder->connectors_active = false;
+       connector->encoder->base.crtc = NULL;
+}
+
+static void intel_enable_pipe_a(struct drm_device *dev)
+{
+       struct intel_connector *connector;
+       struct drm_connector *crt = NULL;
+       struct intel_load_detect_pipe load_detect_temp;
+
+       /* We can't just switch on the pipe A, we need to set things up with a
+        * proper mode and output configuration. As a gross hack, enable pipe A
+        * by enabling the load detect pipe once. */
+       list_for_each_entry(connector,
+                           &dev->mode_config.connector_list,
+                           base.head) {
+               if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
+                       crt = &connector->base;
+                       break;
+               }
+       }
+
+       if (!crt)
+               return;
+
+       if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
+               intel_release_load_detect_pipe(crt, &load_detect_temp);
+
+
+}
+
+static void intel_sanitize_crtc(struct intel_crtc *crtc)
+{
+       struct drm_device *dev = crtc->base.dev;
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       u32 reg, val;
+
+       /* Clear any frame start delays used for debugging left by the BIOS */
+       reg = PIPECONF(crtc->pipe);
+       I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
+
+       /* We need to sanitize the plane -> pipe mapping first because this will
+        * disable the crtc (and hence change the state) if it is wrong. */
+       if (!HAS_PCH_SPLIT(dev)) {
+               struct intel_connector *connector;
+               bool plane;
+
+               reg = DSPCNTR(crtc->plane);
+               val = I915_READ(reg);
+
+               if ((val & DISPLAY_PLANE_ENABLE) == 0 &&
+                   (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
+                       goto ok;
+
+               DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
+                             crtc->base.base.id);
+
+               /* Pipe has the wrong plane attached and the plane is active.
+                * Temporarily change the plane mapping and disable everything
+                * ...  */
+               plane = crtc->plane;
+               crtc->plane = !plane;
+               dev_priv->display.crtc_disable(&crtc->base);
+               crtc->plane = plane;
+
+               /* ... and break all links. */
+               list_for_each_entry(connector, &dev->mode_config.connector_list,
+                                   base.head) {
+                       if (connector->encoder->base.crtc != &crtc->base)
+                               continue;
+
+                       intel_connector_break_all_links(connector);
+               }
+
+               WARN_ON(crtc->active);
+               crtc->base.enabled = false;
+       }
+ok:
+
+       if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
+           crtc->pipe == PIPE_A && !crtc->active) {
+               /* BIOS forgot to enable pipe A, this mostly happens after
+                * resume. Force-enable the pipe to fix this, the update_dpms
+                * call below we restore the pipe to the right state, but leave
+                * the required bits on. */
+               intel_enable_pipe_a(dev);
+       }
+
+       /* Adjust the state of the output pipe according to whether we
+        * have active connectors/encoders. */
+       intel_crtc_update_dpms(&crtc->base);
+
+       if (crtc->active != crtc->base.enabled) {
+               struct intel_encoder *encoder;
+
+               /* This can happen either due to bugs in the get_hw_state
+                * functions or because the pipe is force-enabled due to the
+                * pipe A quirk. */
+               DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
+                             crtc->base.base.id,
+                             crtc->base.enabled ? "enabled" : "disabled",
+                             crtc->active ? "enabled" : "disabled");
+
+               crtc->base.enabled = crtc->active;
+
+               /* Because we only establish the connector -> encoder ->
+                * crtc links if something is active, this means the
+                * crtc is now deactivated. Break the links. connector
+                * -> encoder links are only establish when things are
+                *  actually up, hence no need to break them. */
+               WARN_ON(crtc->active);
+
+               for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
+                       WARN_ON(encoder->connectors_active);
+                       encoder->base.crtc = NULL;
+               }
+       }
+}
+
+static void intel_sanitize_encoder(struct intel_encoder *encoder)
+{
+       struct intel_connector *connector;
+       struct drm_device *dev = encoder->base.dev;
+
+       /* We need to check both for a crtc link (meaning that the
+        * encoder is active and trying to read from a pipe) and the
+        * pipe itself being active. */
+       bool has_active_crtc = encoder->base.crtc &&
+               to_intel_crtc(encoder->base.crtc)->active;
+
+       if (encoder->connectors_active && !has_active_crtc) {
+               DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
+                             encoder->base.base.id,
+                             drm_get_encoder_name(&encoder->base));
+
+               /* Connector is active, but has no active pipe. This is
+                * fallout from our resume register restoring. Disable
+                * the encoder manually again. */
+               if (encoder->base.crtc) {
+                       DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
+                                     encoder->base.base.id,
+                                     drm_get_encoder_name(&encoder->base));
+                       encoder->disable(encoder);
+               }
+
+               /* Inconsistent output/port/pipe state happens presumably due to
+                * a bug in one of the get_hw_state functions. Or someplace else
+                * in our code, like the register restore mess on resume. Clamp
+                * things to off as a safer default. */
+               list_for_each_entry(connector,
+                                   &dev->mode_config.connector_list,
+                                   base.head) {
+                       if (connector->encoder != encoder)
+                               continue;
+
+                       intel_connector_break_all_links(connector);
+               }
+       }
+       /* Enabled encoders without active connectors will be fixed in
+        * the crtc fixup. */
+}
+
+/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
+ * and i915 state tracking structures. */
+void intel_modeset_setup_hw_state(struct drm_device *dev)
+{
+       struct drm_i915_private *dev_priv = dev->dev_private;
+       enum pipe pipe;
+       u32 tmp;
+       struct intel_crtc *crtc;
+       struct intel_encoder *encoder;
+       struct intel_connector *connector;
+
+       for_each_pipe(pipe) {
+               crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
+
+               tmp = I915_READ(PIPECONF(pipe));
+               if (tmp & PIPECONF_ENABLE)
+                       crtc->active = true;
+               else
+                       crtc->active = false;
+
+               crtc->base.enabled = crtc->active;
+
+               DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
+                             crtc->base.base.id,
+                             crtc->active ? "enabled" : "disabled");
+       }
+
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list,
+                           base.head) {
+               pipe = 0;
+
+               if (encoder->get_hw_state(encoder, &pipe)) {
+                       encoder->base.crtc =
+                               dev_priv->pipe_to_crtc_mapping[pipe];
+               } else {
+                       encoder->base.crtc = NULL;
+               }
+
+               encoder->connectors_active = false;
+               DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
+                             encoder->base.base.id,
+                             drm_get_encoder_name(&encoder->base),
+                             encoder->base.crtc ? "enabled" : "disabled",
+                             pipe);
+       }
+
+       list_for_each_entry(connector, &dev->mode_config.connector_list,
+                           base.head) {
+               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;
+               }
+               DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
+                             connector->base.base.id,
+                             drm_get_connector_name(&connector->base),
+                             connector->base.encoder ? "enabled" : "disabled");
+       }
+
+       /* HW state is read out, now we need to sanitize this mess. */
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list,
+                           base.head) {
+               intel_sanitize_encoder(encoder);
+       }
+
+       for_each_pipe(pipe) {
+               crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
+               intel_sanitize_crtc(crtc);
+       }
+
+       intel_modeset_update_staged_output_state(dev);
+}
+
 void intel_modeset_gem_init(struct drm_device *dev)
 {
        intel_modeset_init_hw(dev);
 
        intel_setup_overlay(dev);
+
+       intel_modeset_setup_hw_state(dev);
 }
 
 void intel_modeset_cleanup(struct drm_device *dev)