vgem_fence.c 7.1 KB

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