drm/i915: evict VM instead of everything
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_gem_evict.c
index 425939b7d3430c4c7c368dd11854e9409827952f..3a3981eb30121fd6df7ef3e8b0fa5f1874ae9f0f 100644 (file)
@@ -37,7 +37,10 @@ mark_free(struct i915_vma *vma, struct list_head *unwind)
        if (vma->obj->pin_count)
                return false;
 
-       list_add(&vma->obj->exec_list, unwind);
+       if (WARN_ON(!list_empty(&vma->exec_list)))
+               return false;
+
+       list_add(&vma->exec_list, unwind);
        return drm_mm_scan_add_block(&vma->node);
 }
 
@@ -49,7 +52,6 @@ i915_gem_evict_something(struct drm_device *dev, struct i915_address_space *vm,
        drm_i915_private_t *dev_priv = dev->dev_private;
        struct list_head eviction_list, unwind_list;
        struct i915_vma *vma;
-       struct drm_i915_gem_object *obj;
        int ret = 0;
 
        trace_i915_gem_evict(dev, min_size, alignment, mappable);
@@ -104,18 +106,17 @@ i915_gem_evict_something(struct drm_device *dev, struct i915_address_space *vm,
 none:
        /* Nothing found, clean up and bail out! */
        while (!list_empty(&unwind_list)) {
-               obj = list_first_entry(&unwind_list,
-                                      struct drm_i915_gem_object,
+               vma = list_first_entry(&unwind_list,
+                                      struct i915_vma,
                                       exec_list);
-               vma = i915_gem_obj_to_vma(obj, vm);
                ret = drm_mm_scan_remove_block(&vma->node);
                BUG_ON(ret);
 
-               list_del_init(&obj->exec_list);
+               list_del_init(&vma->exec_list);
        }
 
        /* We expect the caller to unpin, evict all and try again, or give up.
-        * So calling i915_gem_evict_everything() is unnecessary.
+        * So calling i915_gem_evict_vm() is unnecessary.
         */
        return -ENOSPC;
 
@@ -125,39 +126,75 @@ found:
         * temporary list. */
        INIT_LIST_HEAD(&eviction_list);
        while (!list_empty(&unwind_list)) {
-               obj = list_first_entry(&unwind_list,
-                                      struct drm_i915_gem_object,
+               vma = list_first_entry(&unwind_list,
+                                      struct i915_vma,
                                       exec_list);
-               vma = i915_gem_obj_to_vma(obj, vm);
                if (drm_mm_scan_remove_block(&vma->node)) {
-                       list_move(&obj->exec_list, &eviction_list);
-                       drm_gem_object_reference(&obj->base);
+                       list_move(&vma->exec_list, &eviction_list);
+                       drm_gem_object_reference(&vma->obj->base);
                        continue;
                }
-               list_del_init(&obj->exec_list);
+               list_del_init(&vma->exec_list);
        }
 
        /* Unbinding will emit any required flushes */
        while (!list_empty(&eviction_list)) {
-               obj = list_first_entry(&eviction_list,
-                                      struct drm_i915_gem_object,
+               struct drm_gem_object *obj;
+               vma = list_first_entry(&eviction_list,
+                                      struct i915_vma,
                                       exec_list);
+
+               obj =  &vma->obj->base;
+               list_del_init(&vma->exec_list);
                if (ret == 0)
-                       ret = i915_vma_unbind(i915_gem_obj_to_vma(obj, vm));
+                       ret = i915_vma_unbind(vma);
 
-               list_del_init(&obj->exec_list);
-               drm_gem_object_unreference(&obj->base);
+               drm_gem_object_unreference(obj);
        }
 
        return ret;
 }
 
+/**
+ * i915_gem_evict_vm - Try to free up VM space
+ *
+ * @vm: Address space to evict from
+ * @do_idle: Boolean directing whether to idle first.
+ *
+ * VM eviction is about freeing up virtual address space. If one wants fine
+ * grained eviction, they should see evict something for more details. In terms
+ * of freeing up actual system memory, this function may not accomplish the
+ * desired result. An object may be shared in multiple address space, and this
+ * function will not assert those objects be freed.
+ *
+ * Using do_idle will result in a more complete eviction because it retires, and
+ * inactivates current BOs.
+ */
+int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle)
+{
+       struct i915_vma *vma, *next;
+       int ret;
+
+       if (do_idle) {
+               ret = i915_gpu_idle(vm->dev);
+               if (ret)
+                       return ret;
+
+               i915_gem_retire_requests(vm->dev);
+       }
+
+       list_for_each_entry_safe(vma, next, &vm->inactive_list, mm_list)
+               if (vma->obj->pin_count == 0)
+                       WARN_ON(i915_vma_unbind(vma));
+
+       return 0;
+}
+
 int
 i915_gem_evict_everything(struct drm_device *dev)
 {
        drm_i915_private_t *dev_priv = dev->dev_private;
        struct i915_address_space *vm;
-       struct i915_vma *vma, *next;
        bool lists_empty = true;
        int ret;
 
@@ -184,11 +221,8 @@ i915_gem_evict_everything(struct drm_device *dev)
        i915_gem_retire_requests(dev);
 
        /* Having flushed everything, unbind() should never raise an error */
-       list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
-               list_for_each_entry_safe(vma, next, &vm->inactive_list, mm_list)
-                       if (vma->obj->pin_count == 0)
-                               WARN_ON(i915_vma_unbind(vma));
-       }
+       list_for_each_entry(vm, &dev_priv->vm_list, global_link)
+               WARN_ON(i915_gem_evict_vm(vm, false));
 
        return 0;
 }
This page took 0.047674 seconds and 5 git commands to generate.