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