#include "intel_ring.h"
 #include "shmem_utils.h"
 
+static void dbg_poison_ce(struct intel_context *ce)
+{
+       if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
+               return;
+
+       if (ce->state) {
+               struct drm_i915_gem_object *obj = ce->state->obj;
+               int type = i915_coherent_map_type(ce->engine->i915);
+               void *map;
+
+               map = i915_gem_object_pin_map(obj, type);
+               if (!IS_ERR(map)) {
+                       memset(map, CONTEXT_REDZONE, obj->base.size);
+                       i915_gem_object_flush_map(obj);
+                       i915_gem_object_unpin_map(obj);
+               }
+       }
+}
+
 static int __engine_unpark(struct intel_wakeref *wf)
 {
        struct intel_engine_cs *engine =
        if (ce) {
                GEM_BUG_ON(test_bit(CONTEXT_VALID_BIT, &ce->flags));
 
+               /* Flush all pending HW writes before we touch the context */
+               while (unlikely(intel_context_inflight(ce)))
+                       intel_engine_flush_submission(engine);
+
                /* First poison the image to verify we never fully trust it */
-               if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) && ce->state) {
-                       struct drm_i915_gem_object *obj = ce->state->obj;
-                       int type = i915_coherent_map_type(engine->i915);
-                       void *map;
-
-                       map = i915_gem_object_pin_map(obj, type);
-                       if (!IS_ERR(map)) {
-                               memset(map, CONTEXT_REDZONE, obj->base.size);
-                               i915_gem_object_flush_map(obj);
-                               i915_gem_object_unpin_map(obj);
-                       }
-               }
+               dbg_poison_ce(ce);
 
+               /* Scrub the context image after our loss of control */
                ce->ops->reset(ce);
        }
 
 
 
 static void
 execlists_check_context(const struct intel_context *ce,
-                       const struct intel_engine_cs *engine)
+                       const struct intel_engine_cs *engine,
+                       const char *when)
 {
        const struct intel_ring *ring = ce->ring;
        u32 *regs = ce->lrc_reg_state;
                valid = false;
        }
 
-       WARN_ONCE(!valid, "Invalid lrc state found before submission\n");
+       WARN_ONCE(!valid, "Invalid lrc state found %s submission\n", when);
 }
 
 static void restore_default_state(struct intel_context *ce,
                reset_active(rq, engine);
 
        if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
-               execlists_check_context(ce, engine);
+               execlists_check_context(ce, engine, "before");
 
        if (ce->tag) {
                /* Use a fixed tag for OA and friends */
         * refrain from doing non-trivial work here.
         */
 
+       if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
+               execlists_check_context(ce, engine, "after");
+
        /*
         * If we have just completed this context, the engine may now be
         * idle and we want to re-enter powersaving.
 
 static void execlists_sanitize(struct intel_engine_cs *engine)
 {
+       GEM_BUG_ON(execlists_active(&engine->execlists));
+
        /*
         * Poison residual state on resume, in case the suspend didn't!
         *