These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / gpu / drm / vmwgfx / vmwgfx_dmabuf.c
index 914b375..299925a 100644 (file)
@@ -1,6 +1,6 @@
 /**************************************************************************
  *
- * Copyright © 2011 VMware, Inc., Palo Alto, CA., USA
+ * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
  * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
 
 
 /**
- * vmw_dmabuf_to_placement - Validate a buffer to placement.
+ * vmw_dmabuf_pin_in_placement - Validate a buffer to placement.
  *
  * @dev_priv:  Driver private.
  * @buf:  DMA buffer to move.
- * @pin:  Pin buffer if true.
+ * @placement:  The placement to pin it.
  * @interruptible:  Use interruptible wait.
  *
- * May only be called by the current master since it assumes that the
- * master lock is the current master's lock.
- * This function takes the master's lock in write mode.
- * Flushes and unpins the query bo to avoid failures.
- *
  * Returns
  *  -ERESTARTSYS if interrupted by a signal.
  */
-int vmw_dmabuf_to_placement(struct vmw_private *dev_priv,
-                           struct vmw_dma_buffer *buf,
-                           struct ttm_placement *placement,
-                           bool interruptible)
+int vmw_dmabuf_pin_in_placement(struct vmw_private *dev_priv,
+                               struct vmw_dma_buffer *buf,
+                               struct ttm_placement *placement,
+                               bool interruptible)
 {
        struct ttm_buffer_object *bo = &buf->base;
        int ret;
@@ -66,6 +61,8 @@ int vmw_dmabuf_to_placement(struct vmw_private *dev_priv,
                goto err;
 
        ret = ttm_bo_validate(bo, placement, interruptible, false);
+       if (!ret)
+               vmw_bo_pin_reserved(buf, true);
 
        ttm_bo_unreserve(bo);
 
@@ -75,12 +72,10 @@ err:
 }
 
 /**
- * vmw_dmabuf_to_vram_or_gmr - Move a buffer to vram or gmr.
+ * vmw_dmabuf_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
  *
- * May only be called by the current master since it assumes that the
- * master lock is the current master's lock.
- * This function takes the master's lock in write mode.
- * Flushes and unpins the query bo if @pin == true to avoid failures.
+ * This function takes the reservation_sem in write mode.
+ * Flushes and unpins the query bo to avoid failures.
  *
  * @dev_priv:  Driver private.
  * @buf:  DMA buffer to move.
@@ -90,55 +85,34 @@ err:
  * Returns
  * -ERESTARTSYS if interrupted by a signal.
  */
-int vmw_dmabuf_to_vram_or_gmr(struct vmw_private *dev_priv,
-                             struct vmw_dma_buffer *buf,
-                             bool pin, bool interruptible)
+int vmw_dmabuf_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
+                                 struct vmw_dma_buffer *buf,
+                                 bool interruptible)
 {
        struct ttm_buffer_object *bo = &buf->base;
-       struct ttm_placement *placement;
        int ret;
 
        ret = ttm_write_lock(&dev_priv->reservation_sem, interruptible);
        if (unlikely(ret != 0))
                return ret;
 
-       if (pin)
-               vmw_execbuf_release_pinned_bo(dev_priv);
+       vmw_execbuf_release_pinned_bo(dev_priv);
 
        ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
        if (unlikely(ret != 0))
                goto err;
 
-       /**
-        * Put BO in VRAM if there is space, otherwise as a GMR.
-        * If there is no space in VRAM and GMR ids are all used up,
-        * start evicting GMRs to make room. If the DMA buffer can't be
-        * used as a GMR, this will return -ENOMEM.
-        */
-
-       if (pin)
-               placement = &vmw_vram_gmr_ne_placement;
-       else
-               placement = &vmw_vram_gmr_placement;
-
-       ret = ttm_bo_validate(bo, placement, interruptible, false);
+       ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, interruptible,
+                             false);
        if (likely(ret == 0) || ret == -ERESTARTSYS)
-               goto err_unreserve;
-
+               goto out_unreserve;
 
-       /**
-        * If that failed, try VRAM again, this time evicting
-        * previous contents.
-        */
-
-       if (pin)
-               placement = &vmw_vram_ne_placement;
-       else
-               placement = &vmw_vram_placement;
+       ret = ttm_bo_validate(bo, &vmw_vram_placement, interruptible, false);
 
-       ret = ttm_bo_validate(bo, placement, interruptible, false);
+out_unreserve:
+       if (!ret)
+               vmw_bo_pin_reserved(buf, true);
 
-err_unreserve:
        ttm_bo_unreserve(bo);
 err:
        ttm_write_unlock(&dev_priv->reservation_sem);
@@ -146,67 +120,50 @@ err:
 }
 
 /**
- * vmw_dmabuf_to_vram - Move a buffer to vram.
+ * vmw_dmabuf_pin_in_vram - Move a buffer to vram.
  *
- * May only be called by the current master since it assumes that the
- * master lock is the current master's lock.
- * This function takes the master's lock in write mode.
+ * This function takes the reservation_sem in write mode.
+ * Flushes and unpins the query bo to avoid failures.
  *
  * @dev_priv:  Driver private.
  * @buf:  DMA buffer to move.
- * @pin:  Pin buffer in vram if true.
  * @interruptible:  Use interruptible wait.
  *
  * Returns
  * -ERESTARTSYS if interrupted by a signal.
  */
-int vmw_dmabuf_to_vram(struct vmw_private *dev_priv,
-                      struct vmw_dma_buffer *buf,
-                      bool pin, bool interruptible)
+int vmw_dmabuf_pin_in_vram(struct vmw_private *dev_priv,
+                          struct vmw_dma_buffer *buf,
+                          bool interruptible)
 {
-       struct ttm_placement *placement;
-
-       if (pin)
-               placement = &vmw_vram_ne_placement;
-       else
-               placement = &vmw_vram_placement;
-
-       return vmw_dmabuf_to_placement(dev_priv, buf,
-                                      placement,
-                                      interruptible);
+       return vmw_dmabuf_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
+                                          interruptible);
 }
 
 /**
- * vmw_dmabuf_to_start_of_vram - Move a buffer to start of vram.
+ * vmw_dmabuf_pin_in_start_of_vram - Move a buffer to start of vram.
  *
- * May only be called by the current master since it assumes that the
- * master lock is the current master's lock.
- * This function takes the master's lock in write mode.
- * Flushes and unpins the query bo if @pin == true to avoid failures.
+ * This function takes the reservation_sem in write mode.
+ * Flushes and unpins the query bo to avoid failures.
  *
  * @dev_priv:  Driver private.
- * @buf:  DMA buffer to move.
- * @pin:  Pin buffer in vram if true.
+ * @buf:  DMA buffer to pin.
  * @interruptible:  Use interruptible wait.
  *
  * Returns
  * -ERESTARTSYS if interrupted by a signal.
  */
-int vmw_dmabuf_to_start_of_vram(struct vmw_private *dev_priv,
-                               struct vmw_dma_buffer *buf,
-                               bool pin, bool interruptible)
+int vmw_dmabuf_pin_in_start_of_vram(struct vmw_private *dev_priv,
+                                   struct vmw_dma_buffer *buf,
+                                   bool interruptible)
 {
        struct ttm_buffer_object *bo = &buf->base;
        struct ttm_placement placement;
        struct ttm_place place;
        int ret = 0;
 
-       if (pin)
-               place = vmw_vram_ne_placement.placement[0];
-       else
-               place = vmw_vram_placement.placement[0];
+       place = vmw_vram_placement.placement[0];
        place.lpfn = bo->num_pages;
-
        placement.num_placement = 1;
        placement.placement = &place;
        placement.num_busy_placement = 1;
@@ -216,13 +173,16 @@ int vmw_dmabuf_to_start_of_vram(struct vmw_private *dev_priv,
        if (unlikely(ret != 0))
                return ret;
 
-       if (pin)
-               vmw_execbuf_release_pinned_bo(dev_priv);
+       vmw_execbuf_release_pinned_bo(dev_priv);
        ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
        if (unlikely(ret != 0))
                goto err_unlock;
 
-       /* Is this buffer already in vram but not at the start of it? */
+       /*
+        * Is this buffer already in vram but not at the start of it?
+        * In that case, evict it first because TTM isn't good at handling
+        * that situation.
+        */
        if (bo->mem.mem_type == TTM_PL_VRAM &&
            bo->mem.start < bo->num_pages &&
            bo->mem.start > 0)
@@ -230,8 +190,10 @@ int vmw_dmabuf_to_start_of_vram(struct vmw_private *dev_priv,
 
        ret = ttm_bo_validate(bo, &placement, interruptible, false);
 
-       /* For some reason we didn't up at the start of vram */
+       /* For some reason we didn't end up at the start of vram */
        WARN_ON(ret == 0 && bo->offset != 0);
+       if (!ret)
+               vmw_bo_pin_reserved(buf, true);
 
        ttm_bo_unreserve(bo);
 err_unlock:
@@ -240,13 +202,10 @@ err_unlock:
        return ret;
 }
 
-
 /**
- * vmw_dmabuf_upin - Unpin the buffer given buffer, does not move the buffer.
+ * vmw_dmabuf_unpin - Unpin the buffer given buffer, does not move the buffer.
  *
- * May only be called by the current master since it assumes that the
- * master lock is the current master's lock.
- * This function takes the master's lock in write mode.
+ * This function takes the reservation_sem in write mode.
  *
  * @dev_priv:  Driver private.
  * @buf:  DMA buffer to unpin.
@@ -259,16 +218,25 @@ int vmw_dmabuf_unpin(struct vmw_private *dev_priv,
                     struct vmw_dma_buffer *buf,
                     bool interruptible)
 {
-       /*
-        * We could in theory early out if the buffer is
-        * unpinned but we need to lock and reserve the buffer
-        * anyways so we don't gain much by that.
-        */
-       return vmw_dmabuf_to_placement(dev_priv, buf,
-                                      &vmw_evictable_placement,
-                                      interruptible);
-}
+       struct ttm_buffer_object *bo = &buf->base;
+       int ret;
+
+       ret = ttm_read_lock(&dev_priv->reservation_sem, interruptible);
+       if (unlikely(ret != 0))
+               return ret;
 
+       ret = ttm_bo_reserve(bo, interruptible, false, false, NULL);
+       if (unlikely(ret != 0))
+               goto err;
+
+       vmw_bo_pin_reserved(buf, false);
+
+       ttm_bo_unreserve(bo);
+
+err:
+       ttm_read_unlock(&dev_priv->reservation_sem);
+       return ret;
+}
 
 /**
  * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
@@ -291,21 +259,31 @@ void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
 
 
 /**
- * vmw_bo_pin - Pin or unpin a buffer object without moving it.
+ * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
  *
- * @bo: The buffer object. Must be reserved.
+ * @vbo: The buffer object. Must be reserved.
  * @pin: Whether to pin or unpin.
  *
  */
-void vmw_bo_pin(struct ttm_buffer_object *bo, bool pin)
+void vmw_bo_pin_reserved(struct vmw_dma_buffer *vbo, bool pin)
 {
        struct ttm_place pl;
        struct ttm_placement placement;
+       struct ttm_buffer_object *bo = &vbo->base;
        uint32_t old_mem_type = bo->mem.mem_type;
        int ret;
 
        lockdep_assert_held(&bo->resv->lock.base);
 
+       if (pin) {
+               if (vbo->pin_count++ > 0)
+                       return;
+       } else {
+               WARN_ON(vbo->pin_count <= 0);
+               if (--vbo->pin_count > 0)
+                       return;
+       }
+
        pl.fpfn = 0;
        pl.lpfn = 0;
        pl.flags = TTM_PL_FLAG_VRAM | VMW_PL_FLAG_GMR | VMW_PL_FLAG_MOB