vgem_fence.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/reservation.h>
  24. #include "vgem_drv.h"
  25. #define VGEM_FENCE_TIMEOUT (10*HZ)
  26. struct vgem_fence {
  27. struct dma_fence base;
  28. struct spinlock lock;
  29. struct timer_list timer;
  30. };
  31. static const char *vgem_fence_get_driver_name(struct dma_fence *fence)
  32. {
  33. return "vgem";
  34. }
  35. static const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
  36. {
  37. return "unbound";
  38. }
  39. static void vgem_fence_release(struct dma_fence *base)
  40. {
  41. struct vgem_fence *fence = container_of(base, typeof(*fence), base);
  42. del_timer_sync(&fence->timer);
  43. dma_fence_free(&fence->base);
  44. }
  45. static void vgem_fence_value_str(struct dma_fence *fence, char *str, int size)
  46. {
  47. snprintf(str, size, "%u", fence->seqno);
  48. }
  49. static void vgem_fence_timeline_value_str(struct dma_fence *fence, char *str,
  50. int size)
  51. {
  52. snprintf(str, size, "%u",
  53. dma_fence_is_signaled(fence) ? fence->seqno : 0);
  54. }
  55. static const struct dma_fence_ops vgem_fence_ops = {
  56. .get_driver_name = vgem_fence_get_driver_name,
  57. .get_timeline_name = vgem_fence_get_timeline_name,
  58. .release = vgem_fence_release,
  59. .fence_value_str = vgem_fence_value_str,
  60. .timeline_value_str = vgem_fence_timeline_value_str,
  61. };
  62. static void vgem_fence_timeout(struct timer_list *t)
  63. {
  64. struct vgem_fence *fence = from_timer(fence, t, timer);
  65. dma_fence_signal(&fence->base);
  66. }
  67. static struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
  68. unsigned int flags)
  69. {
  70. struct vgem_fence *fence;
  71. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  72. if (!fence)
  73. return NULL;
  74. spin_lock_init(&fence->lock);
  75. dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
  76. dma_fence_context_alloc(1), 1);
  77. timer_setup(&fence->timer, vgem_fence_timeout, 0);
  78. /* We force the fence to expire within 10s to prevent driver hangs */
  79. mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
  80. return &fence->base;
  81. }
  82. static int attach_dmabuf(struct drm_device *dev,
  83. struct drm_gem_object *obj)
  84. {
  85. struct dma_buf *dmabuf;
  86. if (obj->dma_buf)
  87. return 0;
  88. dmabuf = dev->driver->gem_prime_export(dev, obj, 0);
  89. if (IS_ERR(dmabuf))
  90. return PTR_ERR(dmabuf);
  91. obj->dma_buf = dmabuf;
  92. return 0;
  93. }
  94. /*
  95. * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH):
  96. *
  97. * Create and attach a fence to the vGEM handle. This fence is then exposed
  98. * via the dma-buf reservation object and visible to consumers of the exported
  99. * dma-buf. If the flags contain VGEM_FENCE_WRITE, the fence indicates the
  100. * vGEM buffer is being written to by the client and is exposed as an exclusive
  101. * fence, otherwise the fence indicates the client is current reading from the
  102. * buffer and all future writes should wait for the client to signal its
  103. * completion. Note that if a conflicting fence is already on the dma-buf (i.e.
  104. * an exclusive fence when adding a read, or any fence when adding a write),
  105. * -EBUSY is reported. Serialisation between operations should be handled
  106. * by waiting upon the dma-buf.
  107. *
  108. * This returns the handle for the new fence that must be signaled within 10
  109. * seconds (or otherwise it will automatically expire). See
  110. * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL).
  111. *
  112. * If the vGEM handle does not exist, vgem_fence_attach_ioctl returns -ENOENT.
  113. */
  114. int vgem_fence_attach_ioctl(struct drm_device *dev,
  115. void *data,
  116. struct drm_file *file)
  117. {
  118. struct drm_vgem_fence_attach *arg = data;
  119. struct vgem_file *vfile = file->driver_priv;
  120. struct reservation_object *resv;
  121. struct drm_gem_object *obj;
  122. struct dma_fence *fence;
  123. int ret;
  124. if (arg->flags & ~VGEM_FENCE_WRITE)
  125. return -EINVAL;
  126. if (arg->pad)
  127. return -EINVAL;
  128. obj = drm_gem_object_lookup(file, arg->handle);
  129. if (!obj)
  130. return -ENOENT;
  131. ret = attach_dmabuf(dev, obj);
  132. if (ret)
  133. goto err;
  134. fence = vgem_fence_create(vfile, arg->flags);
  135. if (!fence) {
  136. ret = -ENOMEM;
  137. goto err;
  138. }
  139. /* Check for a conflicting fence */
  140. resv = obj->dma_buf->resv;
  141. if (!reservation_object_test_signaled_rcu(resv,
  142. arg->flags & VGEM_FENCE_WRITE)) {
  143. ret = -EBUSY;
  144. goto err_fence;
  145. }
  146. /* Expose the fence via the dma-buf */
  147. ret = 0;
  148. reservation_object_lock(resv, NULL);
  149. if (arg->flags & VGEM_FENCE_WRITE)
  150. reservation_object_add_excl_fence(resv, fence);
  151. else if ((ret = reservation_object_reserve_shared(resv)) == 0)
  152. reservation_object_add_shared_fence(resv, fence);
  153. reservation_object_unlock(resv);
  154. /* Record the fence in our idr for later signaling */
  155. if (ret == 0) {
  156. mutex_lock(&vfile->fence_mutex);
  157. ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
  158. mutex_unlock(&vfile->fence_mutex);
  159. if (ret > 0) {
  160. arg->out_fence = ret;
  161. ret = 0;
  162. }
  163. }
  164. err_fence:
  165. if (ret) {
  166. dma_fence_signal(fence);
  167. dma_fence_put(fence);
  168. }
  169. err:
  170. drm_gem_object_put_unlocked(obj);
  171. return ret;
  172. }
  173. /*
  174. * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL):
  175. *
  176. * Signal and consume a fence ealier attached to a vGEM handle using
  177. * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH).
  178. *
  179. * All fences must be signaled within 10s of attachment or otherwise they
  180. * will automatically expire (and a vgem_fence_signal_ioctl returns -ETIMEDOUT).
  181. *
  182. * Signaling a fence indicates to all consumers of the dma-buf that the
  183. * client has completed the operation associated with the fence, and that the
  184. * buffer is then ready for consumption.
  185. *
  186. * If the fence does not exist (or has already been signaled by the client),
  187. * vgem_fence_signal_ioctl returns -ENOENT.
  188. */
  189. int vgem_fence_signal_ioctl(struct drm_device *dev,
  190. void *data,
  191. struct drm_file *file)
  192. {
  193. struct vgem_file *vfile = file->driver_priv;
  194. struct drm_vgem_fence_signal *arg = data;
  195. struct dma_fence *fence;
  196. int ret = 0;
  197. if (arg->flags)
  198. return -EINVAL;
  199. mutex_lock(&vfile->fence_mutex);
  200. fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
  201. mutex_unlock(&vfile->fence_mutex);
  202. if (!fence)
  203. return -ENOENT;
  204. if (IS_ERR(fence))
  205. return PTR_ERR(fence);
  206. if (dma_fence_is_signaled(fence))
  207. ret = -ETIMEDOUT;
  208. dma_fence_signal(fence);
  209. dma_fence_put(fence);
  210. return ret;
  211. }
  212. int vgem_fence_open(struct vgem_file *vfile)
  213. {
  214. mutex_init(&vfile->fence_mutex);
  215. idr_init(&vfile->fence_idr);
  216. return 0;
  217. }
  218. static int __vgem_fence_idr_fini(int id, void *p, void *data)
  219. {
  220. dma_fence_signal(p);
  221. dma_fence_put(p);
  222. return 0;
  223. }
  224. void vgem_fence_close(struct vgem_file *vfile)
  225. {
  226. idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
  227. idr_destroy(&vfile->fence_idr);
  228. }