]> git.karo-electronics.de Git - linux-beck.git/blobdiff - drivers/gpu/drm/i915/intel_display.c
drm/i915: Unify unpin_work and mmio_work into flip_work, v2.
[linux-beck.git] / drivers / gpu / drm / i915 / intel_display.c
index 176d23fa49c3d0783e92ab4751f5632f20c3d072..ae253bb57c25c197a4671026a319c30311cdceb9 100644 (file)
 #include <linux/reservation.h>
 #include <linux/dma-buf.h>
 
+static bool is_mmio_work(struct intel_flip_work *work)
+{
+       return work->mmio_work.func;
+}
+
 /* Primary plane formats for gen <= 3 */
 static const uint32_t i8xx_primary_formats[] = {
        DRM_FORMAT_C8,
@@ -3110,15 +3115,10 @@ intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
 
 static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
 {
-       struct drm_crtc *crtc;
-
-       for_each_crtc(dev_priv->dev, crtc) {
-               struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-               enum plane plane = intel_crtc->plane;
+       struct intel_crtc *crtc;
 
-               intel_prepare_page_flip(dev_priv, plane);
-               intel_finish_page_flip_plane(dev_priv, plane);
-       }
+       for_each_intel_crtc(dev_priv->dev, crtc)
+               intel_finish_page_flip_cs(dev_priv, crtc->pipe);
 }
 
 static void intel_update_primary_planes(struct drm_device *dev)
@@ -3220,7 +3220,7 @@ static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
                return false;
 
        spin_lock_irq(&dev->event_lock);
-       pending = to_intel_crtc(crtc)->unpin_work != NULL;
+       pending = to_intel_crtc(crtc)->flip_work != NULL;
        spin_unlock_irq(&dev->event_lock);
 
        return pending;
@@ -3799,7 +3799,7 @@ bool intel_has_pending_fb_unpin(struct drm_device *dev)
                if (atomic_read(&crtc->unpin_work_count) == 0)
                        continue;
 
-               if (crtc->unpin_work)
+               if (crtc->flip_work)
                        intel_wait_for_vblank(dev, crtc->pipe);
 
                return true;
@@ -3811,11 +3811,9 @@ bool intel_has_pending_fb_unpin(struct drm_device *dev)
 static void page_flip_completed(struct intel_crtc *intel_crtc)
 {
        struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
-       struct intel_unpin_work *work = intel_crtc->unpin_work;
+       struct intel_flip_work *work = intel_crtc->flip_work;
 
-       /* ensure that the unpin work is consistent wrt ->pending. */
-       smp_rmb();
-       intel_crtc->unpin_work = NULL;
+       intel_crtc->flip_work = NULL;
 
        if (work->event)
                drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
@@ -3823,7 +3821,7 @@ static void page_flip_completed(struct intel_crtc *intel_crtc)
        drm_crtc_vblank_put(&intel_crtc->base);
 
        wake_up_all(&dev_priv->pending_flip_queue);
-       queue_work(dev_priv->wq, &work->work);
+       queue_work(dev_priv->wq, &work->unpin_work);
 
        trace_i915_flip_complete(intel_crtc->plane,
                                 work->pending_flip_obj);
@@ -3847,9 +3845,11 @@ static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
 
        if (ret == 0) {
                struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+               struct intel_flip_work *work;
 
                spin_lock_irq(&dev->event_lock);
-               if (intel_crtc->unpin_work) {
+               work = intel_crtc->flip_work;
+               if (work && !is_mmio_work(work)) {
                        WARN_ONCE(1, "Removing stuck page flip\n");
                        page_flip_completed(intel_crtc);
                }
@@ -5429,19 +5429,18 @@ static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
                if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
                        DRM_ERROR("timeout waiting for DE PLL lock\n");
 
-               val = I915_READ(CDCLK_CTL);
-               val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
-               val |= divider;
+               val = divider | skl_cdclk_decimal(cdclk);
+               /*
+                * FIXME if only the cd2x divider needs changing, it could be done
+                * without shutting off the pipe (if only one pipe is active).
+                */
+               val |= BXT_CDCLK_CD2X_PIPE_NONE;
                /*
                 * Disable SSA Precharge when CD clock frequency < 500 MHz,
                 * enable otherwise.
                 */
-               val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
                if (cdclk >= 500000)
                        val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
-
-               val &= ~CDCLK_FREQ_DECIMAL_MASK;
-               val |= skl_cdclk_decimal(cdclk);
                I915_WRITE(CDCLK_CTL, val);
        }
 
@@ -6233,7 +6232,7 @@ static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
                return;
 
        if (to_intel_plane_state(crtc->primary->state)->visible) {
-               WARN_ON(intel_crtc->unpin_work);
+               WARN_ON(intel_crtc->flip_work);
 
                intel_pre_disable_primary_noatomic(crtc);
 
@@ -10829,15 +10828,16 @@ static void intel_crtc_destroy(struct drm_crtc *crtc)
 {
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct drm_device *dev = crtc->dev;
-       struct intel_unpin_work *work;
+       struct intel_flip_work *work;
 
        spin_lock_irq(&dev->event_lock);
-       work = intel_crtc->unpin_work;
-       intel_crtc->unpin_work = NULL;
+       work = intel_crtc->flip_work;
+       intel_crtc->flip_work = NULL;
        spin_unlock_irq(&dev->event_lock);
 
        if (work) {
-               cancel_work_sync(&work->work);
+               cancel_work_sync(&work->mmio_work);
+               cancel_work_sync(&work->unpin_work);
                kfree(work);
        }
 
@@ -10848,12 +10848,15 @@ static void intel_crtc_destroy(struct drm_crtc *crtc)
 
 static void intel_unpin_work_fn(struct work_struct *__work)
 {
-       struct intel_unpin_work *work =
-               container_of(__work, struct intel_unpin_work, work);
+       struct intel_flip_work *work =
+               container_of(__work, struct intel_flip_work, unpin_work);
        struct intel_crtc *crtc = to_intel_crtc(work->crtc);
        struct drm_device *dev = crtc->base.dev;
        struct drm_plane *primary = crtc->base.primary;
 
+       if (is_mmio_work(work))
+               flush_work(&work->mmio_work);
+
        mutex_lock(&dev->struct_mutex);
        intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
        drm_gem_object_unreference(&work->pending_flip_obj->base);
@@ -10872,59 +10875,14 @@ static void intel_unpin_work_fn(struct work_struct *__work)
        kfree(work);
 }
 
-static void do_intel_finish_page_flip(struct drm_i915_private *dev_priv,
-                                     struct drm_crtc *crtc)
-{
-       struct drm_device *dev = dev_priv->dev;
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       struct intel_unpin_work *work;
-       unsigned long flags;
-
-       /* Ignore early vblank irqs */
-       if (intel_crtc == NULL)
-               return;
-
-       /*
-        * This is called both by irq handlers and the reset code (to complete
-        * lost pageflips) so needs the full irqsave spinlocks.
-        */
-       spin_lock_irqsave(&dev->event_lock, flags);
-       work = intel_crtc->unpin_work;
-
-       /* Ensure we don't miss a work->pending update ... */
-       smp_rmb();
-
-       if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
-               spin_unlock_irqrestore(&dev->event_lock, flags);
-               return;
-       }
-
-       page_flip_completed(intel_crtc);
-
-       spin_unlock_irqrestore(&dev->event_lock, flags);
-}
-
-void intel_finish_page_flip(struct drm_i915_private *dev_priv, int pipe)
-{
-       struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
-
-       do_intel_finish_page_flip(dev_priv, crtc);
-}
-
-void intel_finish_page_flip_plane(struct drm_i915_private *dev_priv, int plane)
-{
-       struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
-
-       do_intel_finish_page_flip(dev_priv, crtc);
-}
-
 /* Is 'a' after or equal to 'b'? */
 static bool g4x_flip_count_after_eq(u32 a, u32 b)
 {
        return !((a - b) & 0x80000000);
 }
 
-static bool page_flip_finished(struct intel_crtc *crtc)
+static bool __pageflip_finished_cs(struct intel_crtc *crtc,
+                                  struct intel_flip_work *work)
 {
        struct drm_device *dev = crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -10966,40 +10924,103 @@ static bool page_flip_finished(struct intel_crtc *crtc)
         * anyway, we don't really care.
         */
        return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
-               crtc->unpin_work->gtt_offset &&
+               crtc->flip_work->gtt_offset &&
                g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
-                                   crtc->unpin_work->flip_count);
+                                   crtc->flip_work->flip_count);
+}
+
+static bool
+__pageflip_finished_mmio(struct intel_crtc *crtc,
+                              struct intel_flip_work *work)
+{
+       /*
+        * MMIO work completes when vblank is different from
+        * flip_queued_vblank.
+        *
+        * Reset counter value doesn't matter, this is handled by
+        * i915_wait_request finishing early, so no need to handle
+        * reset here.
+        */
+       return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
+}
+
+
+static bool pageflip_finished(struct intel_crtc *crtc,
+                             struct intel_flip_work *work)
+{
+       if (!atomic_read(&work->pending))
+               return false;
+
+       smp_rmb();
+
+       if (is_mmio_work(work))
+               return __pageflip_finished_mmio(crtc, work);
+       else
+               return __pageflip_finished_cs(crtc, work);
 }
 
-void intel_prepare_page_flip(struct drm_i915_private *dev_priv, int plane)
+void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
 {
        struct drm_device *dev = dev_priv->dev;
-       struct intel_crtc *intel_crtc =
-               to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
+       struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct intel_flip_work *work;
        unsigned long flags;
 
+       /* Ignore early vblank irqs */
+       if (!crtc)
+               return;
+
+       /*
+        * This is called both by irq handlers and the reset code (to complete
+        * lost pageflips) so needs the full irqsave spinlocks.
+        */
+       spin_lock_irqsave(&dev->event_lock, flags);
+       work = intel_crtc->flip_work;
+
+       if (work != NULL &&
+           !is_mmio_work(work) &&
+           pageflip_finished(intel_crtc, work))
+               page_flip_completed(intel_crtc);
+
+       spin_unlock_irqrestore(&dev->event_lock, flags);
+}
+
+void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
+{
+       struct drm_device *dev = dev_priv->dev;
+       struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
+       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
+       struct intel_flip_work *work;
+       unsigned long flags;
+
+       /* Ignore early vblank irqs */
+       if (!crtc)
+               return;
 
        /*
         * This is called both by irq handlers and the reset code (to complete
         * lost pageflips) so needs the full irqsave spinlocks.
-        *
-        * NB: An MMIO update of the plane base pointer will also
-        * generate a page-flip completion irq, i.e. every modeset
-        * is also accompanied by a spurious intel_prepare_page_flip().
         */
        spin_lock_irqsave(&dev->event_lock, flags);
-       if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
-               atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
+       work = intel_crtc->flip_work;
+
+       if (work != NULL &&
+           is_mmio_work(work) &&
+           pageflip_finished(intel_crtc, work))
+               page_flip_completed(intel_crtc);
+
        spin_unlock_irqrestore(&dev->event_lock, flags);
 }
 
-static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
+static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
+                                              struct intel_flip_work *work)
 {
+       work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
+
        /* Ensure that the work item is consistent when activating it ... */
-       smp_wmb();
-       atomic_set(&work->pending, INTEL_FLIP_PENDING);
-       /* and that it is marked active as soon as the irq could fire. */
-       smp_wmb();
+       smp_mb__before_atomic();
+       atomic_set(&work->pending, 1);
 }
 
 static int intel_gen2_queue_flip(struct drm_device *dev,
@@ -11030,10 +11051,9 @@ static int intel_gen2_queue_flip(struct drm_device *dev,
        intel_ring_emit(engine, MI_DISPLAY_FLIP |
                        MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
        intel_ring_emit(engine, fb->pitches[0]);
-       intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
        intel_ring_emit(engine, 0); /* aux display base address, unused */
 
-       intel_mark_page_flip_active(intel_crtc->unpin_work);
        return 0;
 }
 
@@ -11062,10 +11082,9 @@ static int intel_gen3_queue_flip(struct drm_device *dev,
        intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
                        MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
        intel_ring_emit(engine, fb->pitches[0]);
-       intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
        intel_ring_emit(engine, MI_NOOP);
 
-       intel_mark_page_flip_active(intel_crtc->unpin_work);
        return 0;
 }
 
@@ -11093,7 +11112,7 @@ static int intel_gen4_queue_flip(struct drm_device *dev,
        intel_ring_emit(engine, MI_DISPLAY_FLIP |
                        MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
        intel_ring_emit(engine, fb->pitches[0]);
-       intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
                        obj->tiling_mode);
 
        /* XXX Enabling the panel-fitter across page-flip is so far
@@ -11104,7 +11123,6 @@ static int intel_gen4_queue_flip(struct drm_device *dev,
        pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
        intel_ring_emit(engine, pf | pipesrc);
 
-       intel_mark_page_flip_active(intel_crtc->unpin_work);
        return 0;
 }
 
@@ -11128,7 +11146,7 @@ static int intel_gen6_queue_flip(struct drm_device *dev,
        intel_ring_emit(engine, MI_DISPLAY_FLIP |
                        MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
        intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
-       intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
 
        /* Contrary to the suggestions in the documentation,
         * "Enable Panel Fitter" does not seem to be required when page
@@ -11140,7 +11158,6 @@ static int intel_gen6_queue_flip(struct drm_device *dev,
        pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
        intel_ring_emit(engine, pf | pipesrc);
 
-       intel_mark_page_flip_active(intel_crtc->unpin_work);
        return 0;
 }
 
@@ -11232,10 +11249,9 @@ static int intel_gen7_queue_flip(struct drm_device *dev,
 
        intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
        intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
-       intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
+       intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
        intel_ring_emit(engine, (MI_NOOP));
 
-       intel_mark_page_flip_active(intel_crtc->unpin_work);
        return 0;
 }
 
@@ -11272,7 +11288,7 @@ static bool use_mmio_flip(struct intel_engine_cs *engine,
 
 static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
                             unsigned int rotation,
-                            struct intel_unpin_work *work)
+                            struct intel_flip_work *work)
 {
        struct drm_device *dev = intel_crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -11324,7 +11340,7 @@ static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
 }
 
 static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
-                            struct intel_unpin_work *work)
+                            struct intel_flip_work *work)
 {
        struct drm_device *dev = intel_crtc->base.dev;
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -11347,48 +11363,20 @@ static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
        POSTING_READ(DSPSURF(intel_crtc->plane));
 }
 
-/*
- * XXX: This is the temporary way to update the plane registers until we get
- * around to using the usual plane update functions for MMIO flips
- */
-static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
-{
-       struct intel_crtc *crtc = mmio_flip->crtc;
-       struct intel_unpin_work *work;
-
-       spin_lock_irq(&crtc->base.dev->event_lock);
-       work = crtc->unpin_work;
-       spin_unlock_irq(&crtc->base.dev->event_lock);
-       if (work == NULL)
-               return;
-
-       intel_mark_page_flip_active(work);
-
-       intel_pipe_update_start(crtc);
-
-       if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
-               skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
-       else
-               /* use_mmio_flip() retricts MMIO flips to ilk+ */
-               ilk_do_mmio_flip(crtc, work);
-
-       intel_pipe_update_end(crtc);
-}
-
-static void intel_mmio_flip_work_func(struct work_struct *work)
+static void intel_mmio_flip_work_func(struct work_struct *w)
 {
-       struct intel_mmio_flip *mmio_flip =
-               container_of(work, struct intel_mmio_flip, work);
+       struct intel_flip_work *work =
+               container_of(w, struct intel_flip_work, mmio_work);
+       struct intel_crtc *crtc = to_intel_crtc(work->crtc);
+       struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
        struct intel_framebuffer *intel_fb =
-               to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
+               to_intel_framebuffer(crtc->base.primary->fb);
        struct drm_i915_gem_object *obj = intel_fb->obj;
 
-       if (mmio_flip->req) {
-               WARN_ON(__i915_wait_request(mmio_flip->req,
+       if (work->flip_queued_req)
+               WARN_ON(__i915_wait_request(work->flip_queued_req,
                                            false, NULL,
-                                           &mmio_flip->i915->rps.mmioflips));
-               i915_gem_request_unreference(mmio_flip->req);
-       }
+                                           &dev_priv->rps.mmioflips));
 
        /* For framebuffer backed by dmabuf, wait for fence */
        if (obj->base.dma_buf)
@@ -11396,29 +11384,15 @@ static void intel_mmio_flip_work_func(struct work_struct *work)
                                                            false, false,
                                                            MAX_SCHEDULE_TIMEOUT) < 0);
 
-       intel_do_mmio_flip(mmio_flip);
-       kfree(mmio_flip);
-}
-
-static int intel_queue_mmio_flip(struct drm_device *dev,
-                                struct drm_crtc *crtc,
-                                struct drm_i915_gem_object *obj)
-{
-       struct intel_mmio_flip *mmio_flip;
-
-       mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
-       if (mmio_flip == NULL)
-               return -ENOMEM;
-
-       mmio_flip->i915 = to_i915(dev);
-       mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
-       mmio_flip->crtc = to_intel_crtc(crtc);
-       mmio_flip->rotation = crtc->primary->state->rotation;
+       intel_pipe_update_start(crtc);
 
-       INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
-       schedule_work(&mmio_flip->work);
+       if (INTEL_GEN(dev_priv) >= 9)
+               skl_do_mmio_flip(crtc, work->rotation, work);
+       else
+               /* use_mmio_flip() retricts MMIO flips to ilk+ */
+               ilk_do_mmio_flip(crtc, work);
 
-       return 0;
+       intel_pipe_update_end(crtc, work);
 }
 
 static int intel_default_queue_flip(struct drm_device *dev,
@@ -11431,37 +11405,32 @@ static int intel_default_queue_flip(struct drm_device *dev,
        return -ENODEV;
 }
 
-static bool __intel_pageflip_stall_check(struct drm_device *dev,
-                                        struct drm_crtc *crtc)
+static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
+                                     struct intel_crtc *intel_crtc,
+                                     struct intel_flip_work *work)
 {
-       struct drm_i915_private *dev_priv = dev->dev_private;
-       struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       struct intel_unpin_work *work = intel_crtc->unpin_work;
-       u32 addr;
-
-       if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
-               return true;
+       u32 addr, vblank;
 
-       if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
+       if (!atomic_read(&work->pending))
                return false;
 
-       if (!work->enable_stall_check)
-               return false;
+       smp_rmb();
 
+       vblank = intel_crtc_get_vblank_counter(intel_crtc);
        if (work->flip_ready_vblank == 0) {
                if (work->flip_queued_req &&
                    !i915_gem_request_completed(work->flip_queued_req, true))
                        return false;
 
-               work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
+               work->flip_ready_vblank = vblank;
        }
 
-       if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
+       if (vblank - work->flip_ready_vblank < 3)
                return false;
 
        /* Potential stall - if we see that the flip has happened,
         * assume a missed interrupt. */
-       if (INTEL_INFO(dev)->gen >= 4)
+       if (INTEL_GEN(dev_priv) >= 4)
                addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
        else
                addr = I915_READ(DSPADDR(intel_crtc->plane));
@@ -11478,7 +11447,7 @@ void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
        struct drm_device *dev = dev_priv->dev;
        struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-       struct intel_unpin_work *work;
+       struct intel_flip_work *work;
 
        WARN_ON(!in_interrupt());
 
@@ -11486,15 +11455,19 @@ void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
                return;
 
        spin_lock(&dev->event_lock);
-       work = intel_crtc->unpin_work;
-       if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
-               WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
-                        work->flip_queued_vblank, drm_vblank_count(dev, pipe));
+       work = intel_crtc->flip_work;
+
+       if (work != NULL && !is_mmio_work(work) &&
+           __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
+               WARN_ONCE(1,
+                         "Kicking stuck page flip: queued at %d, now %d\n",
+                       work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
                page_flip_completed(intel_crtc);
                work = NULL;
        }
-       if (work != NULL &&
-           drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
+
+       if (work != NULL && !is_mmio_work(work) &&
+           intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
                intel_queue_rps_boost_for_request(work->flip_queued_req);
        spin_unlock(&dev->event_lock);
 }
@@ -11511,7 +11484,7 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
        struct drm_plane *primary = crtc->primary;
        enum pipe pipe = intel_crtc->pipe;
-       struct intel_unpin_work *work;
+       struct intel_flip_work *work;
        struct intel_engine_cs *engine;
        bool mmio_flip;
        struct drm_i915_gem_request *request = NULL;
@@ -11548,19 +11521,19 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
        work->event = event;
        work->crtc = crtc;
        work->old_fb = old_fb;
-       INIT_WORK(&work->work, intel_unpin_work_fn);
+       INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
 
        ret = drm_crtc_vblank_get(crtc);
        if (ret)
                goto free_work;
 
-       /* We borrow the event spin lock for protecting unpin_work */
+       /* We borrow the event spin lock for protecting flip_work */
        spin_lock_irq(&dev->event_lock);
-       if (intel_crtc->unpin_work) {
+       if (intel_crtc->flip_work) {
                /* Before declaring the flip queue wedged, check if
                 * the hardware completed the operation behind our backs.
                 */
-               if (__intel_pageflip_stall_check(dev, crtc)) {
+               if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
                        DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
                        page_flip_completed(intel_crtc);
                } else {
@@ -11572,7 +11545,7 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
                        return -EBUSY;
                }
        }
-       intel_crtc->unpin_work = work;
+       intel_crtc->flip_work = work;
        spin_unlock_irq(&dev->event_lock);
 
        if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
@@ -11627,6 +11600,11 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
         */
        if (!mmio_flip) {
                ret = i915_gem_object_sync(obj, engine, &request);
+               if (!ret && !request) {
+                       request = i915_gem_request_alloc(engine, NULL);
+                       ret = PTR_ERR_OR_ZERO(request);
+               }
+
                if (ret)
                        goto cleanup_pending;
        }
@@ -11638,38 +11616,28 @@ static int intel_crtc_page_flip(struct drm_crtc *crtc,
        work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
                                                  obj, 0);
        work->gtt_offset += intel_crtc->dspaddr_offset;
+       work->rotation = crtc->primary->state->rotation;
 
        if (mmio_flip) {
-               ret = intel_queue_mmio_flip(dev, crtc, obj);
-               if (ret)
-                       goto cleanup_unpin;
+               INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
 
                i915_gem_request_assign(&work->flip_queued_req,
                                        obj->last_write_req);
-       } else {
-               if (!request) {
-                       request = i915_gem_request_alloc(engine, NULL);
-                       if (IS_ERR(request)) {
-                               ret = PTR_ERR(request);
-                               goto cleanup_unpin;
-                       }
-               }
 
+               schedule_work(&work->mmio_work);
+       } else {
+               i915_gem_request_assign(&work->flip_queued_req, request);
                ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
                                                   page_flip_flags);
                if (ret)
                        goto cleanup_unpin;
 
-               i915_gem_request_assign(&work->flip_queued_req, request);
-       }
+               intel_mark_page_flip_active(intel_crtc, work);
 
-       if (request)
                i915_add_request_no_flush(request);
+       }
 
-       work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
-       work->enable_stall_check = true;
-
-       i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
+       i915_gem_track_fb(intel_fb_obj(old_fb), obj,
                          to_intel_plane(primary)->frontbuffer_bit);
        mutex_unlock(&dev->struct_mutex);
 
@@ -11695,7 +11663,7 @@ cleanup:
        drm_framebuffer_unreference(work->old_fb);
 
        spin_lock_irq(&dev->event_lock);
-       intel_crtc->unpin_work = NULL;
+       intel_crtc->flip_work = NULL;
        spin_unlock_irq(&dev->event_lock);
 
        drm_crtc_vblank_put(crtc);
@@ -11999,6 +11967,9 @@ static int intel_crtc_atomic_check(struct drm_crtc *crtc,
                        DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
                        return ret;
                }
+       } else if (dev_priv->display.compute_intermediate_wm) {
+               if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
+                       pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
        }
 
        if (INTEL_INFO(dev)->gen >= 9) {
@@ -12025,11 +11996,16 @@ static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
        struct intel_connector *connector;
 
        for_each_intel_connector(dev, connector) {
+               if (connector->base.state->crtc)
+                       drm_connector_unreference(&connector->base);
+
                if (connector->base.encoder) {
                        connector->base.state->best_encoder =
                                connector->base.encoder;
                        connector->base.state->crtc =
                                connector->base.encoder->crtc;
+
+                       drm_connector_reference(&connector->base);
                } else {
                        connector->base.state->best_encoder = NULL;
                        connector->base.state->crtc = NULL;
@@ -13344,14 +13320,13 @@ static int intel_atomic_check(struct drm_device *dev,
                if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
                        crtc_state->mode_changed = true;
 
-               if (!crtc_state->enable) {
-                       if (needs_modeset(crtc_state))
-                               any_ms = true;
+               if (!needs_modeset(crtc_state))
                        continue;
-               }
 
-               if (!needs_modeset(crtc_state))
+               if (!crtc_state->enable) {
+                       any_ms = true;
                        continue;
+               }
 
                /* FIXME: For only active_changed we shouldn't need to do any
                 * state recomputation at all. */
@@ -13375,13 +13350,12 @@ static int intel_atomic_check(struct drm_device *dev,
                        to_intel_crtc_state(crtc_state)->update_pipe = true;
                }
 
-               if (needs_modeset(crtc_state)) {
+               if (needs_modeset(crtc_state))
                        any_ms = true;
 
-                       ret = drm_atomic_add_affected_planes(state, crtc);
-                       if (ret)
-                               return ret;
-               }
+               ret = drm_atomic_add_affected_planes(state, crtc);
+               if (ret)
+                       return ret;
 
                intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
                                       needs_modeset(crtc_state) ?
@@ -13406,7 +13380,7 @@ static int intel_atomic_check(struct drm_device *dev,
 
 static int intel_atomic_prepare_commit(struct drm_device *dev,
                                       struct drm_atomic_state *state,
-                                      bool async)
+                                      bool nonblock)
 {
        struct drm_i915_private *dev_priv = dev->dev_private;
        struct drm_plane_state *plane_state;
@@ -13415,8 +13389,8 @@ static int intel_atomic_prepare_commit(struct drm_device *dev,
        struct drm_crtc *crtc;
        int i, ret;
 
-       if (async) {
-               DRM_DEBUG_KMS("i915 does not yet support async commit\n");
+       if (nonblock) {
+               DRM_DEBUG_KMS("i915 does not yet support nonblocking commit\n");
                return -EINVAL;
        }
 
@@ -13439,7 +13413,7 @@ static int intel_atomic_prepare_commit(struct drm_device *dev,
        ret = drm_atomic_helper_prepare_planes(dev, state);
        mutex_unlock(&dev->struct_mutex);
 
-       if (!ret && !async) {
+       if (!ret && !nonblock) {
                for_each_plane_in_state(state, plane, plane_state, i) {
                        struct intel_plane_state *intel_plane_state =
                                to_intel_plane_state(plane_state);
@@ -13463,6 +13437,16 @@ static int intel_atomic_prepare_commit(struct drm_device *dev,
        return ret;
 }
 
+u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
+{
+       struct drm_device *dev = crtc->base.dev;
+
+       if (!dev->max_vblank_count)
+               return drm_accurate_vblank_count(&crtc->base);
+
+       return dev->driver->get_vblank_counter(dev, crtc->pipe);
+}
+
 static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
                                          struct drm_i915_private *dev_priv,
                                          unsigned crtc_mask)
@@ -13532,21 +13516,21 @@ static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  * intel_atomic_commit - commit validated state object
  * @dev: DRM device
  * @state: the top-level driver state object
- * @async: asynchronous commit
+ * @nonblock: nonblocking commit
  *
  * This function commits a top-level state object that has been validated
  * with drm_atomic_helper_check().
  *
  * FIXME:  Atomic modeset support for i915 is not yet complete.  At the moment
  * we can only handle plane-related operations and do not yet support
- * asynchronous commit.
+ * nonblocking commit.
  *
  * RETURNS
  * Zero for success or -errno.
  */
 static int intel_atomic_commit(struct drm_device *dev,
                               struct drm_atomic_state *state,
-                              bool async)
+                              bool nonblock)
 {
        struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
        struct drm_i915_private *dev_priv = dev->dev_private;
@@ -13558,7 +13542,7 @@ static int intel_atomic_commit(struct drm_device *dev,
        unsigned long put_domains[I915_MAX_PIPES] = {};
        unsigned crtc_vblank_mask = 0;
 
-       ret = intel_atomic_prepare_commit(dev, state, async);
+       ret = intel_atomic_prepare_commit(dev, state, nonblock);
        if (ret) {
                DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
                return ret;
@@ -13975,7 +13959,7 @@ static void intel_finish_crtc_commit(struct drm_crtc *crtc,
 {
        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
 
-       intel_pipe_update_end(intel_crtc);
+       intel_pipe_update_end(intel_crtc, NULL);
 }
 
 /**
@@ -15962,6 +15946,9 @@ retry:
 
                state->acquire_ctx = &ctx;
 
+               /* ignore any reset values/BIOS leftovers in the WM registers */
+               to_intel_atomic_state(state)->skip_intermediate_wm = true;
+
                for_each_crtc_in_state(state, crtc, crtc_state, i) {
                        /*
                         * Force recalculation even if we restore