return 0;
 }
 
-static inline bool
-i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
-{
-       return i915_gem_obj_bound_any(obj) && !obj->active;
-}
-
 int
 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
                            struct drm_file *file)
        if (ret)
                goto unref;
 
-       if (read_domains & I915_GEM_DOMAIN_GTT) {
+       if (read_domains & I915_GEM_DOMAIN_GTT)
                ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
-
-               /* Silently promote "you're not bound, there was nothing to do"
-                * to success, since the client was just asking us to
-                * make sure everything was done.
-                */
-               if (ret == -EINVAL)
-                       ret = 0;
-       } else {
+       else
                ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
-       }
 
 unref:
        drm_gem_object_unreference(&obj->base);
 int
 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
 {
-       struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
-       struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
        uint32_t old_write_domain, old_read_domains;
+       struct i915_vma *vma;
        int ret;
 
-       /* Not valid to be called on unbound objects. */
-       if (vma == NULL)
-               return -EINVAL;
-
        if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
                return 0;
 
                return ret;
 
        i915_gem_object_retire(obj);
+
+       /* Flush and acquire obj->pages so that we are coherent through
+        * direct access in memory with previous cached writes through
+        * shmemfs and that our cache domain tracking remains valid.
+        * For example, if the obj->filp was moved to swap without us
+        * being notified and releasing the pages, we would mistakenly
+        * continue to assume that the obj remained out of the CPU cached
+        * domain.
+        */
+       ret = i915_gem_object_get_pages(obj);
+       if (ret)
+               return ret;
+
        i915_gem_object_flush_cpu_write_domain(obj, false);
 
        /* Serialise direct access to this object with the barriers for
                                            old_write_domain);
 
        /* And bump the LRU for this access */
-       if (i915_gem_object_is_inactive(obj))
+       vma = i915_gem_obj_to_ggtt(obj);
+       if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
                list_move_tail(&vma->mm_list,
-                              &dev_priv->gtt.base.inactive_list);
+                              &to_i915(obj->base.dev)->gtt.base.inactive_list);
 
        return 0;
 }