vgem_fence.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software")
  6. * to deal in the software without restriction, including without limitation
  7. * on the rights to use, copy, modify, merge, publish, distribute, sub
  8. * license, and/or sell copies of the Software, and to permit persons to whom
  9. * them Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <linux/dma-buf.h>
  23. #include <linux/dma-resv.h>
  24. #include <drm/drm_file.h>
  25. #include "vgem_drv.h"
  26. #define VGEM_FENCE_TIMEOUT (10*HZ)
  27. struct vgem_fence {
  28. struct dma_fence base;
  29. struct spinlock lock;
  30. struct timer_list timer;
  31. };
  32. static const char *vgem_fence_get_driver_name(struct dma_fence *fence)
  33. {
  34. return "vgem";
  35. }
  36. static const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
  37. {
  38. return "unbound";
  39. }
  40. static void vgem_fence_release(struct dma_fence *base)
  41. {
  42. struct vgem_fence *fence = container_of(base, typeof(*fence), base);
  43. timer_delete_sync(&fence->timer);
  44. dma_fence_free(&fence->base);
  45. }
  46. static const struct dma_fence_ops vgem_fence_ops = {
  47. .get_driver_name = vgem_fence_get_driver_name,
  48. .get_timeline_name = vgem_fence_get_timeline_name,
  49. .release = vgem_fence_release,
  50. };
  51. static void vgem_fence_timeout(struct timer_list *t)
  52. {
  53. struct vgem_fence *fence = timer_container_of(fence, t, timer);
  54. dma_fence_signal(&fence->base);
  55. }
  56. static struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
  57. unsigned int flags)
  58. {
  59. struct vgem_fence *fence;
  60. fence = kzalloc_obj(*fence);
  61. if (!fence)
  62. return NULL;
  63. spin_lock_init(&fence->lock);
  64. dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
  65. dma_fence_context_alloc(1), 1);
  66. timer_setup(&fence->timer, vgem_fence_timeout, TIMER_IRQSAFE);
  67. /* We force the fence to expire within 10s to prevent driver hangs */
  68. mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
  69. return &fence->base;
  70. }
  71. /*
  72. * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH):
  73. *
  74. * Create and attach a fence to the vGEM handle. This fence is then exposed
  75. * via the dma-buf reservation object and visible to consumers of the exported
  76. * dma-buf. If the flags contain VGEM_FENCE_WRITE, the fence indicates the
  77. * vGEM buffer is being written to by the client and is exposed as an exclusive
  78. * fence, otherwise the fence indicates the client is current reading from the
  79. * buffer and all future writes should wait for the client to signal its
  80. * completion. Note that if a conflicting fence is already on the dma-buf (i.e.
  81. * an exclusive fence when adding a read, or any fence when adding a write),
  82. * -EBUSY is reported. Serialisation between operations should be handled
  83. * by waiting upon the dma-buf.
  84. *
  85. * This returns the handle for the new fence that must be signaled within 10
  86. * seconds (or otherwise it will automatically expire). See
  87. * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL).
  88. *
  89. * If the vGEM handle does not exist, vgem_fence_attach_ioctl returns -ENOENT.
  90. */
  91. int vgem_fence_attach_ioctl(struct drm_device *dev,
  92. void *data,
  93. struct drm_file *file)
  94. {
  95. struct drm_vgem_fence_attach *arg = data;
  96. struct vgem_file *vfile = file->driver_priv;
  97. struct dma_resv *resv;
  98. struct drm_gem_object *obj;
  99. enum dma_resv_usage usage;
  100. struct dma_fence *fence;
  101. int ret;
  102. if (arg->flags & ~VGEM_FENCE_WRITE)
  103. return -EINVAL;
  104. if (arg->pad)
  105. return -EINVAL;
  106. obj = drm_gem_object_lookup(file, arg->handle);
  107. if (!obj)
  108. return -ENOENT;
  109. fence = vgem_fence_create(vfile, arg->flags);
  110. if (!fence) {
  111. ret = -ENOMEM;
  112. goto err;
  113. }
  114. /* Check for a conflicting fence */
  115. resv = obj->resv;
  116. usage = dma_resv_usage_rw(arg->flags & VGEM_FENCE_WRITE);
  117. if (!dma_resv_test_signaled(resv, usage)) {
  118. ret = -EBUSY;
  119. goto err_fence;
  120. }
  121. /* Expose the fence via the dma-buf */
  122. dma_resv_lock(resv, NULL);
  123. ret = dma_resv_reserve_fences(resv, 1);
  124. if (!ret)
  125. dma_resv_add_fence(resv, fence, arg->flags & VGEM_FENCE_WRITE ?
  126. DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
  127. dma_resv_unlock(resv);
  128. /* Record the fence in our idr for later signaling */
  129. if (ret == 0) {
  130. mutex_lock(&vfile->fence_mutex);
  131. ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
  132. mutex_unlock(&vfile->fence_mutex);
  133. if (ret > 0) {
  134. arg->out_fence = ret;
  135. ret = 0;
  136. }
  137. }
  138. err_fence:
  139. if (ret) {
  140. dma_fence_signal(fence);
  141. dma_fence_put(fence);
  142. }
  143. err:
  144. drm_gem_object_put(obj);
  145. return ret;
  146. }
  147. /*
  148. * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL):
  149. *
  150. * Signal and consume a fence ealier attached to a vGEM handle using
  151. * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH).
  152. *
  153. * All fences must be signaled within 10s of attachment or otherwise they
  154. * will automatically expire (and a vgem_fence_signal_ioctl returns -ETIMEDOUT).
  155. *
  156. * Signaling a fence indicates to all consumers of the dma-buf that the
  157. * client has completed the operation associated with the fence, and that the
  158. * buffer is then ready for consumption.
  159. *
  160. * If the fence does not exist (or has already been signaled by the client),
  161. * vgem_fence_signal_ioctl returns -ENOENT.
  162. */
  163. int vgem_fence_signal_ioctl(struct drm_device *dev,
  164. void *data,
  165. struct drm_file *file)
  166. {
  167. struct vgem_file *vfile = file->driver_priv;
  168. struct drm_vgem_fence_signal *arg = data;
  169. struct dma_fence *fence;
  170. int ret = 0;
  171. if (arg->flags)
  172. return -EINVAL;
  173. mutex_lock(&vfile->fence_mutex);
  174. fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
  175. mutex_unlock(&vfile->fence_mutex);
  176. if (!fence)
  177. return -ENOENT;
  178. if (IS_ERR(fence))
  179. return PTR_ERR(fence);
  180. if (dma_fence_is_signaled(fence))
  181. ret = -ETIMEDOUT;
  182. dma_fence_signal(fence);
  183. dma_fence_put(fence);
  184. return ret;
  185. }
  186. int vgem_fence_open(struct vgem_file *vfile)
  187. {
  188. mutex_init(&vfile->fence_mutex);
  189. idr_init_base(&vfile->fence_idr, 1);
  190. return 0;
  191. }
  192. static int __vgem_fence_idr_fini(int id, void *p, void *data)
  193. {
  194. dma_fence_signal(p);
  195. dma_fence_put(p);
  196. return 0;
  197. }
  198. void vgem_fence_close(struct vgem_file *vfile)
  199. {
  200. idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
  201. idr_destroy(&vfile->fence_idr);
  202. mutex_destroy(&vfile->fence_mutex);
  203. }