vgem_fence.c 7.6 KB

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