Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Red Hat |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 3 | * Parts ported from amdgpu (fence wait code). |
| 4 | * Copyright 2016 Advanced Micro Devices, Inc. |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | * IN THE SOFTWARE. |
| 24 | * |
| 25 | * Authors: |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * DOC: Overview |
| 31 | * |
Daniel Vetter | 924fe8d | 2017-12-14 21:30:52 +0100 | [diff] [blame] | 32 | * DRM synchronisation objects (syncobj, see struct &drm_syncobj) are |
| 33 | * persistent objects that contain an optional fence. The fence can be updated |
| 34 | * with a new fence, or be NULL. |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 35 | * |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 36 | * syncobj's can be waited upon, where it will wait for the underlying |
| 37 | * fence. |
| 38 | * |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 39 | * syncobj's can be export to fd's and back, these fd's are opaque and |
| 40 | * have no other use case, except passing the syncobj between processes. |
| 41 | * |
| 42 | * Their primary use-case is to implement Vulkan fences and semaphores. |
| 43 | * |
| 44 | * syncobj have a kref reference count, but also have an optional file. |
| 45 | * The file is only created once the syncobj is exported. |
| 46 | * The file takes a reference on the kref. |
| 47 | */ |
| 48 | |
| 49 | #include <drm/drmP.h> |
| 50 | #include <linux/file.h> |
| 51 | #include <linux/fs.h> |
| 52 | #include <linux/anon_inodes.h> |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 53 | #include <linux/sync_file.h> |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 54 | #include <linux/sched/signal.h> |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 55 | |
| 56 | #include "drm_internal.h" |
| 57 | #include <drm/drm_syncobj.h> |
| 58 | |
| 59 | /** |
| 60 | * drm_syncobj_find - lookup and reference a sync object. |
| 61 | * @file_private: drm file private pointer |
| 62 | * @handle: sync object handle to lookup. |
| 63 | * |
Daniel Vetter | 924fe8d | 2017-12-14 21:30:52 +0100 | [diff] [blame] | 64 | * Returns a reference to the syncobj pointed to by handle or NULL. The |
| 65 | * reference must be released by calling drm_syncobj_put(). |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 66 | */ |
| 67 | struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private, |
| 68 | u32 handle) |
| 69 | { |
| 70 | struct drm_syncobj *syncobj; |
| 71 | |
| 72 | spin_lock(&file_private->syncobj_table_lock); |
| 73 | |
| 74 | /* Check if we currently have a reference on the object */ |
| 75 | syncobj = idr_find(&file_private->syncobj_idr, handle); |
| 76 | if (syncobj) |
| 77 | drm_syncobj_get(syncobj); |
| 78 | |
| 79 | spin_unlock(&file_private->syncobj_table_lock); |
| 80 | |
| 81 | return syncobj; |
| 82 | } |
| 83 | EXPORT_SYMBOL(drm_syncobj_find); |
| 84 | |
Jason Ekstrand | 9c19fb1 | 2017-08-28 07:39:25 -0700 | [diff] [blame] | 85 | static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj, |
| 86 | struct drm_syncobj_cb *cb, |
| 87 | drm_syncobj_func_t func) |
| 88 | { |
| 89 | cb->func = func; |
| 90 | list_add_tail(&cb->node, &syncobj->cb_list); |
| 91 | } |
| 92 | |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 93 | static int drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj, |
| 94 | struct dma_fence **fence, |
| 95 | struct drm_syncobj_cb *cb, |
| 96 | drm_syncobj_func_t func) |
| 97 | { |
| 98 | int ret; |
| 99 | |
| 100 | *fence = drm_syncobj_fence_get(syncobj); |
| 101 | if (*fence) |
| 102 | return 1; |
| 103 | |
| 104 | spin_lock(&syncobj->lock); |
| 105 | /* We've already tried once to get a fence and failed. Now that we |
| 106 | * have the lock, try one more time just to be sure we don't add a |
| 107 | * callback when a fence has already been set. |
| 108 | */ |
| 109 | if (syncobj->fence) { |
Ville Syrjälä | 563eaf5 | 2017-11-02 22:03:35 +0200 | [diff] [blame] | 110 | *fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, |
| 111 | lockdep_is_held(&syncobj->lock))); |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 112 | ret = 1; |
| 113 | } else { |
| 114 | *fence = NULL; |
| 115 | drm_syncobj_add_callback_locked(syncobj, cb, func); |
| 116 | ret = 0; |
| 117 | } |
| 118 | spin_unlock(&syncobj->lock); |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
Jason Ekstrand | 9c19fb1 | 2017-08-28 07:39:25 -0700 | [diff] [blame] | 123 | /** |
| 124 | * drm_syncobj_add_callback - adds a callback to syncobj::cb_list |
| 125 | * @syncobj: Sync object to which to add the callback |
| 126 | * @cb: Callback to add |
| 127 | * @func: Func to use when initializing the drm_syncobj_cb struct |
| 128 | * |
| 129 | * This adds a callback to be called next time the fence is replaced |
| 130 | */ |
| 131 | void drm_syncobj_add_callback(struct drm_syncobj *syncobj, |
| 132 | struct drm_syncobj_cb *cb, |
| 133 | drm_syncobj_func_t func) |
| 134 | { |
| 135 | spin_lock(&syncobj->lock); |
| 136 | drm_syncobj_add_callback_locked(syncobj, cb, func); |
| 137 | spin_unlock(&syncobj->lock); |
| 138 | } |
| 139 | EXPORT_SYMBOL(drm_syncobj_add_callback); |
| 140 | |
| 141 | /** |
| 142 | * drm_syncobj_add_callback - removes a callback to syncobj::cb_list |
| 143 | * @syncobj: Sync object from which to remove the callback |
| 144 | * @cb: Callback to remove |
| 145 | */ |
| 146 | void drm_syncobj_remove_callback(struct drm_syncobj *syncobj, |
| 147 | struct drm_syncobj_cb *cb) |
| 148 | { |
| 149 | spin_lock(&syncobj->lock); |
| 150 | list_del_init(&cb->node); |
| 151 | spin_unlock(&syncobj->lock); |
| 152 | } |
| 153 | EXPORT_SYMBOL(drm_syncobj_remove_callback); |
| 154 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 155 | /** |
| 156 | * drm_syncobj_replace_fence - replace fence in a sync object. |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 157 | * @syncobj: Sync object to replace fence in |
| 158 | * @fence: fence to install in sync file. |
| 159 | * |
| 160 | * This replaces the fence on a sync object. |
| 161 | */ |
Chris Wilson | 00fc2c2 | 2017-07-05 21:12:44 +0100 | [diff] [blame] | 162 | void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 163 | struct dma_fence *fence) |
| 164 | { |
Chris Wilson | 00fc2c2 | 2017-07-05 21:12:44 +0100 | [diff] [blame] | 165 | struct dma_fence *old_fence; |
Jason Ekstrand | 9c19fb1 | 2017-08-28 07:39:25 -0700 | [diff] [blame] | 166 | struct drm_syncobj_cb *cur, *tmp; |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 167 | |
| 168 | if (fence) |
| 169 | dma_fence_get(fence); |
Jason Ekstrand | 9c19fb1 | 2017-08-28 07:39:25 -0700 | [diff] [blame] | 170 | |
| 171 | spin_lock(&syncobj->lock); |
| 172 | |
Ville Syrjälä | 563eaf5 | 2017-11-02 22:03:35 +0200 | [diff] [blame] | 173 | old_fence = rcu_dereference_protected(syncobj->fence, |
| 174 | lockdep_is_held(&syncobj->lock)); |
| 175 | rcu_assign_pointer(syncobj->fence, fence); |
Jason Ekstrand | 9c19fb1 | 2017-08-28 07:39:25 -0700 | [diff] [blame] | 176 | |
| 177 | if (fence != old_fence) { |
| 178 | list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) { |
| 179 | list_del_init(&cur->node); |
| 180 | cur->func(syncobj, cur); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | spin_unlock(&syncobj->lock); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 185 | |
| 186 | dma_fence_put(old_fence); |
| 187 | } |
| 188 | EXPORT_SYMBOL(drm_syncobj_replace_fence); |
| 189 | |
Jason Ekstrand | 1fc0821 | 2017-08-25 10:52:25 -0700 | [diff] [blame] | 190 | struct drm_syncobj_null_fence { |
| 191 | struct dma_fence base; |
| 192 | spinlock_t lock; |
| 193 | }; |
| 194 | |
| 195 | static const char *drm_syncobj_null_fence_get_name(struct dma_fence *fence) |
| 196 | { |
| 197 | return "syncobjnull"; |
| 198 | } |
| 199 | |
| 200 | static bool drm_syncobj_null_fence_enable_signaling(struct dma_fence *fence) |
| 201 | { |
| 202 | dma_fence_enable_sw_signaling(fence); |
| 203 | return !dma_fence_is_signaled(fence); |
| 204 | } |
| 205 | |
| 206 | static const struct dma_fence_ops drm_syncobj_null_fence_ops = { |
| 207 | .get_driver_name = drm_syncobj_null_fence_get_name, |
| 208 | .get_timeline_name = drm_syncobj_null_fence_get_name, |
| 209 | .enable_signaling = drm_syncobj_null_fence_enable_signaling, |
Jason Ekstrand | 1fc0821 | 2017-08-25 10:52:25 -0700 | [diff] [blame] | 210 | .release = NULL, |
| 211 | }; |
| 212 | |
| 213 | static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj) |
| 214 | { |
| 215 | struct drm_syncobj_null_fence *fence; |
| 216 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
| 217 | if (fence == NULL) |
| 218 | return -ENOMEM; |
| 219 | |
| 220 | spin_lock_init(&fence->lock); |
| 221 | dma_fence_init(&fence->base, &drm_syncobj_null_fence_ops, |
| 222 | &fence->lock, 0, 0); |
| 223 | dma_fence_signal(&fence->base); |
| 224 | |
| 225 | drm_syncobj_replace_fence(syncobj, &fence->base); |
| 226 | |
| 227 | dma_fence_put(&fence->base); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Daniel Vetter | 924fe8d | 2017-12-14 21:30:52 +0100 | [diff] [blame] | 232 | /** |
| 233 | * drm_syncobj_find_fence - lookup and reference the fence in a sync object |
| 234 | * @file_private: drm file private pointer |
| 235 | * @handle: sync object handle to lookup. |
| 236 | * @fence: out parameter for the fence |
| 237 | * |
| 238 | * This is just a convenience function that combines drm_syncobj_find() and |
| 239 | * drm_syncobj_fence_get(). |
| 240 | * |
| 241 | * Returns 0 on success or a negative error value on failure. On success @fence |
| 242 | * contains a reference to the fence, which must be released by calling |
| 243 | * dma_fence_put(). |
| 244 | */ |
Jason Ekstrand | afaf592 | 2017-08-25 10:52:19 -0700 | [diff] [blame] | 245 | int drm_syncobj_find_fence(struct drm_file *file_private, |
| 246 | u32 handle, |
| 247 | struct dma_fence **fence) |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 248 | { |
| 249 | struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle); |
| 250 | int ret = 0; |
| 251 | |
| 252 | if (!syncobj) |
| 253 | return -ENOENT; |
| 254 | |
Jason Ekstrand | 309a548 | 2017-08-25 10:52:20 -0700 | [diff] [blame] | 255 | *fence = drm_syncobj_fence_get(syncobj); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 256 | if (!*fence) { |
| 257 | ret = -EINVAL; |
| 258 | } |
| 259 | drm_syncobj_put(syncobj); |
| 260 | return ret; |
| 261 | } |
Jason Ekstrand | afaf592 | 2017-08-25 10:52:19 -0700 | [diff] [blame] | 262 | EXPORT_SYMBOL(drm_syncobj_find_fence); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * drm_syncobj_free - free a sync object. |
| 266 | * @kref: kref to free. |
| 267 | * |
| 268 | * Only to be called from kref_put in drm_syncobj_put. |
| 269 | */ |
| 270 | void drm_syncobj_free(struct kref *kref) |
| 271 | { |
| 272 | struct drm_syncobj *syncobj = container_of(kref, |
| 273 | struct drm_syncobj, |
| 274 | refcount); |
Jason Ekstrand | 9c19fb1 | 2017-08-28 07:39:25 -0700 | [diff] [blame] | 275 | drm_syncobj_replace_fence(syncobj, NULL); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 276 | kfree(syncobj); |
| 277 | } |
| 278 | EXPORT_SYMBOL(drm_syncobj_free); |
| 279 | |
Marek Olšák | 1321fd2 | 2017-09-12 22:42:12 +0200 | [diff] [blame] | 280 | /** |
| 281 | * drm_syncobj_create - create a new syncobj |
| 282 | * @out_syncobj: returned syncobj |
| 283 | * @flags: DRM_SYNCOBJ_* flags |
| 284 | * @fence: if non-NULL, the syncobj will represent this fence |
Daniel Vetter | 924fe8d | 2017-12-14 21:30:52 +0100 | [diff] [blame] | 285 | * |
| 286 | * This is the first function to create a sync object. After creating, drivers |
| 287 | * probably want to make it available to userspace, either through |
| 288 | * drm_syncobj_get_handle() or drm_syncobj_get_fd(). |
| 289 | * |
| 290 | * Returns 0 on success or a negative error value on failure. |
Marek Olšák | 1321fd2 | 2017-09-12 22:42:12 +0200 | [diff] [blame] | 291 | */ |
| 292 | int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags, |
| 293 | struct dma_fence *fence) |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 294 | { |
| 295 | int ret; |
| 296 | struct drm_syncobj *syncobj; |
| 297 | |
| 298 | syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL); |
| 299 | if (!syncobj) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | kref_init(&syncobj->refcount); |
Jason Ekstrand | 9c19fb1 | 2017-08-28 07:39:25 -0700 | [diff] [blame] | 303 | INIT_LIST_HEAD(&syncobj->cb_list); |
| 304 | spin_lock_init(&syncobj->lock); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 305 | |
Jason Ekstrand | 1fc0821 | 2017-08-25 10:52:25 -0700 | [diff] [blame] | 306 | if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) { |
| 307 | ret = drm_syncobj_assign_null_handle(syncobj); |
| 308 | if (ret < 0) { |
| 309 | drm_syncobj_put(syncobj); |
| 310 | return ret; |
| 311 | } |
| 312 | } |
| 313 | |
Marek Olšák | 1321fd2 | 2017-09-12 22:42:12 +0200 | [diff] [blame] | 314 | if (fence) |
| 315 | drm_syncobj_replace_fence(syncobj, fence); |
| 316 | |
| 317 | *out_syncobj = syncobj; |
| 318 | return 0; |
| 319 | } |
| 320 | EXPORT_SYMBOL(drm_syncobj_create); |
| 321 | |
| 322 | /** |
| 323 | * drm_syncobj_get_handle - get a handle from a syncobj |
Daniel Vetter | 924fe8d | 2017-12-14 21:30:52 +0100 | [diff] [blame] | 324 | * @file_private: drm file private pointer |
| 325 | * @syncobj: Sync object to export |
| 326 | * @handle: out parameter with the new handle |
| 327 | * |
| 328 | * Exports a sync object created with drm_syncobj_create() as a handle on |
| 329 | * @file_private to userspace. |
| 330 | * |
| 331 | * Returns 0 on success or a negative error value on failure. |
Marek Olšák | 1321fd2 | 2017-09-12 22:42:12 +0200 | [diff] [blame] | 332 | */ |
| 333 | int drm_syncobj_get_handle(struct drm_file *file_private, |
| 334 | struct drm_syncobj *syncobj, u32 *handle) |
| 335 | { |
| 336 | int ret; |
| 337 | |
| 338 | /* take a reference to put in the idr */ |
| 339 | drm_syncobj_get(syncobj); |
| 340 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 341 | idr_preload(GFP_KERNEL); |
| 342 | spin_lock(&file_private->syncobj_table_lock); |
| 343 | ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT); |
| 344 | spin_unlock(&file_private->syncobj_table_lock); |
| 345 | |
| 346 | idr_preload_end(); |
| 347 | |
| 348 | if (ret < 0) { |
| 349 | drm_syncobj_put(syncobj); |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | *handle = ret; |
| 354 | return 0; |
| 355 | } |
Marek Olšák | 1321fd2 | 2017-09-12 22:42:12 +0200 | [diff] [blame] | 356 | EXPORT_SYMBOL(drm_syncobj_get_handle); |
| 357 | |
| 358 | static int drm_syncobj_create_as_handle(struct drm_file *file_private, |
| 359 | u32 *handle, uint32_t flags) |
| 360 | { |
| 361 | int ret; |
| 362 | struct drm_syncobj *syncobj; |
| 363 | |
| 364 | ret = drm_syncobj_create(&syncobj, flags, NULL); |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | |
| 368 | ret = drm_syncobj_get_handle(file_private, syncobj, handle); |
| 369 | drm_syncobj_put(syncobj); |
| 370 | return ret; |
| 371 | } |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 372 | |
| 373 | static int drm_syncobj_destroy(struct drm_file *file_private, |
| 374 | u32 handle) |
| 375 | { |
| 376 | struct drm_syncobj *syncobj; |
| 377 | |
| 378 | spin_lock(&file_private->syncobj_table_lock); |
| 379 | syncobj = idr_remove(&file_private->syncobj_idr, handle); |
| 380 | spin_unlock(&file_private->syncobj_table_lock); |
| 381 | |
| 382 | if (!syncobj) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | drm_syncobj_put(syncobj); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static int drm_syncobj_file_release(struct inode *inode, struct file *file) |
| 390 | { |
| 391 | struct drm_syncobj *syncobj = file->private_data; |
| 392 | |
| 393 | drm_syncobj_put(syncobj); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static const struct file_operations drm_syncobj_file_fops = { |
| 398 | .release = drm_syncobj_file_release, |
| 399 | }; |
| 400 | |
Daniel Vetter | 924fe8d | 2017-12-14 21:30:52 +0100 | [diff] [blame] | 401 | /** |
| 402 | * drm_syncobj_get_fd - get a file descriptor from a syncobj |
| 403 | * @syncobj: Sync object to export |
| 404 | * @p_fd: out parameter with the new file descriptor |
| 405 | * |
| 406 | * Exports a sync object created with drm_syncobj_create() as a file descriptor. |
| 407 | * |
| 408 | * Returns 0 on success or a negative error value on failure. |
| 409 | */ |
Marek Olšák | 684fd0a | 2017-09-12 22:42:13 +0200 | [diff] [blame] | 410 | int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd) |
| 411 | { |
Chris Wilson | e7cdf5c | 2017-12-19 12:07:00 +0000 | [diff] [blame] | 412 | struct file *file; |
Marek Olšák | 684fd0a | 2017-09-12 22:42:13 +0200 | [diff] [blame] | 413 | int fd; |
| 414 | |
| 415 | fd = get_unused_fd_flags(O_CLOEXEC); |
| 416 | if (fd < 0) |
| 417 | return fd; |
| 418 | |
Chris Wilson | e7cdf5c | 2017-12-19 12:07:00 +0000 | [diff] [blame] | 419 | file = anon_inode_getfile("syncobj_file", |
| 420 | &drm_syncobj_file_fops, |
| 421 | syncobj, 0); |
| 422 | if (IS_ERR(file)) { |
| 423 | put_unused_fd(fd); |
| 424 | return PTR_ERR(file); |
Marek Olšák | 684fd0a | 2017-09-12 22:42:13 +0200 | [diff] [blame] | 425 | } |
Chris Wilson | e7cdf5c | 2017-12-19 12:07:00 +0000 | [diff] [blame] | 426 | |
| 427 | drm_syncobj_get(syncobj); |
| 428 | fd_install(fd, file); |
| 429 | |
Marek Olšák | 684fd0a | 2017-09-12 22:42:13 +0200 | [diff] [blame] | 430 | *p_fd = fd; |
| 431 | return 0; |
| 432 | } |
| 433 | EXPORT_SYMBOL(drm_syncobj_get_fd); |
| 434 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 435 | static int drm_syncobj_handle_to_fd(struct drm_file *file_private, |
| 436 | u32 handle, int *p_fd) |
| 437 | { |
| 438 | struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle); |
| 439 | int ret; |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 440 | |
| 441 | if (!syncobj) |
| 442 | return -EINVAL; |
| 443 | |
Marek Olšák | 684fd0a | 2017-09-12 22:42:13 +0200 | [diff] [blame] | 444 | ret = drm_syncobj_get_fd(syncobj, p_fd); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 445 | drm_syncobj_put(syncobj); |
| 446 | return ret; |
| 447 | } |
| 448 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 449 | static int drm_syncobj_fd_to_handle(struct drm_file *file_private, |
| 450 | int fd, u32 *handle) |
| 451 | { |
Chris Wilson | e7cdf5c | 2017-12-19 12:07:00 +0000 | [diff] [blame] | 452 | struct drm_syncobj *syncobj; |
| 453 | struct file *file; |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 454 | int ret; |
| 455 | |
Chris Wilson | e7cdf5c | 2017-12-19 12:07:00 +0000 | [diff] [blame] | 456 | file = fget(fd); |
| 457 | if (!file) |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 458 | return -EINVAL; |
| 459 | |
Chris Wilson | e7cdf5c | 2017-12-19 12:07:00 +0000 | [diff] [blame] | 460 | if (file->f_op != &drm_syncobj_file_fops) { |
| 461 | fput(file); |
| 462 | return -EINVAL; |
| 463 | } |
| 464 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 465 | /* take a reference to put in the idr */ |
Chris Wilson | e7cdf5c | 2017-12-19 12:07:00 +0000 | [diff] [blame] | 466 | syncobj = file->private_data; |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 467 | drm_syncobj_get(syncobj); |
| 468 | |
| 469 | idr_preload(GFP_KERNEL); |
| 470 | spin_lock(&file_private->syncobj_table_lock); |
| 471 | ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT); |
| 472 | spin_unlock(&file_private->syncobj_table_lock); |
| 473 | idr_preload_end(); |
| 474 | |
Chris Wilson | e7cdf5c | 2017-12-19 12:07:00 +0000 | [diff] [blame] | 475 | if (ret > 0) { |
| 476 | *handle = ret; |
| 477 | ret = 0; |
| 478 | } else |
| 479 | drm_syncobj_put(syncobj); |
| 480 | |
| 481 | fput(file); |
| 482 | return ret; |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 483 | } |
| 484 | |
Ville Syrjälä | a32c94a | 2017-09-01 19:53:25 +0300 | [diff] [blame] | 485 | static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private, |
| 486 | int fd, int handle) |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 487 | { |
| 488 | struct dma_fence *fence = sync_file_get_fence(fd); |
| 489 | struct drm_syncobj *syncobj; |
| 490 | |
| 491 | if (!fence) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | syncobj = drm_syncobj_find(file_private, handle); |
| 495 | if (!syncobj) { |
| 496 | dma_fence_put(fence); |
| 497 | return -ENOENT; |
| 498 | } |
| 499 | |
Chris Wilson | 00fc2c2 | 2017-07-05 21:12:44 +0100 | [diff] [blame] | 500 | drm_syncobj_replace_fence(syncobj, fence); |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 501 | dma_fence_put(fence); |
| 502 | drm_syncobj_put(syncobj); |
| 503 | return 0; |
| 504 | } |
| 505 | |
Ville Syrjälä | a32c94a | 2017-09-01 19:53:25 +0300 | [diff] [blame] | 506 | static int drm_syncobj_export_sync_file(struct drm_file *file_private, |
| 507 | int handle, int *p_fd) |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 508 | { |
| 509 | int ret; |
| 510 | struct dma_fence *fence; |
| 511 | struct sync_file *sync_file; |
| 512 | int fd = get_unused_fd_flags(O_CLOEXEC); |
| 513 | |
| 514 | if (fd < 0) |
| 515 | return fd; |
| 516 | |
Jason Ekstrand | afaf592 | 2017-08-25 10:52:19 -0700 | [diff] [blame] | 517 | ret = drm_syncobj_find_fence(file_private, handle, &fence); |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 518 | if (ret) |
| 519 | goto err_put_fd; |
| 520 | |
| 521 | sync_file = sync_file_create(fence); |
| 522 | |
| 523 | dma_fence_put(fence); |
| 524 | |
| 525 | if (!sync_file) { |
| 526 | ret = -EINVAL; |
| 527 | goto err_put_fd; |
| 528 | } |
| 529 | |
| 530 | fd_install(fd, sync_file->file); |
| 531 | |
| 532 | *p_fd = fd; |
| 533 | return 0; |
| 534 | err_put_fd: |
| 535 | put_unused_fd(fd); |
| 536 | return ret; |
| 537 | } |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 538 | /** |
| 539 | * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 540 | * @file_private: drm file-private structure to set up |
| 541 | * |
| 542 | * Called at device open time, sets up the structure for handling refcounting |
| 543 | * of sync objects. |
| 544 | */ |
| 545 | void |
| 546 | drm_syncobj_open(struct drm_file *file_private) |
| 547 | { |
Chris Wilson | e86584c | 2018-02-12 14:55:33 +0000 | [diff] [blame] | 548 | idr_init_base(&file_private->syncobj_idr, 1); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 549 | spin_lock_init(&file_private->syncobj_table_lock); |
| 550 | } |
| 551 | |
| 552 | static int |
| 553 | drm_syncobj_release_handle(int id, void *ptr, void *data) |
| 554 | { |
| 555 | struct drm_syncobj *syncobj = ptr; |
| 556 | |
| 557 | drm_syncobj_put(syncobj); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * drm_syncobj_release - release file-private sync object resources |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 563 | * @file_private: drm file-private structure to clean up |
| 564 | * |
| 565 | * Called at close time when the filp is going away. |
| 566 | * |
| 567 | * Releases any remaining references on objects by this filp. |
| 568 | */ |
| 569 | void |
| 570 | drm_syncobj_release(struct drm_file *file_private) |
| 571 | { |
| 572 | idr_for_each(&file_private->syncobj_idr, |
| 573 | &drm_syncobj_release_handle, file_private); |
| 574 | idr_destroy(&file_private->syncobj_idr); |
| 575 | } |
| 576 | |
| 577 | int |
| 578 | drm_syncobj_create_ioctl(struct drm_device *dev, void *data, |
| 579 | struct drm_file *file_private) |
| 580 | { |
| 581 | struct drm_syncobj_create *args = data; |
| 582 | |
| 583 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 584 | return -ENODEV; |
| 585 | |
| 586 | /* no valid flags yet */ |
Jason Ekstrand | 1fc0821 | 2017-08-25 10:52:25 -0700 | [diff] [blame] | 587 | if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED) |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 588 | return -EINVAL; |
| 589 | |
Marek Olšák | 1321fd2 | 2017-09-12 22:42:12 +0200 | [diff] [blame] | 590 | return drm_syncobj_create_as_handle(file_private, |
| 591 | &args->handle, args->flags); |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | int |
| 595 | drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data, |
| 596 | struct drm_file *file_private) |
| 597 | { |
| 598 | struct drm_syncobj_destroy *args = data; |
| 599 | |
| 600 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 601 | return -ENODEV; |
| 602 | |
| 603 | /* make sure padding is empty */ |
| 604 | if (args->pad) |
| 605 | return -EINVAL; |
| 606 | return drm_syncobj_destroy(file_private, args->handle); |
| 607 | } |
| 608 | |
| 609 | int |
| 610 | drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data, |
| 611 | struct drm_file *file_private) |
| 612 | { |
| 613 | struct drm_syncobj_handle *args = data; |
| 614 | |
| 615 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 616 | return -ENODEV; |
| 617 | |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 618 | if (args->pad) |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 619 | return -EINVAL; |
| 620 | |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 621 | if (args->flags != 0 && |
| 622 | args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE) |
| 623 | return -EINVAL; |
| 624 | |
| 625 | if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE) |
| 626 | return drm_syncobj_export_sync_file(file_private, args->handle, |
| 627 | &args->fd); |
| 628 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 629 | return drm_syncobj_handle_to_fd(file_private, args->handle, |
| 630 | &args->fd); |
| 631 | } |
| 632 | |
| 633 | int |
| 634 | drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data, |
| 635 | struct drm_file *file_private) |
| 636 | { |
| 637 | struct drm_syncobj_handle *args = data; |
| 638 | |
| 639 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 640 | return -ENODEV; |
| 641 | |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 642 | if (args->pad) |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 643 | return -EINVAL; |
| 644 | |
Dave Airlie | 3ee45a3 | 2017-04-26 04:09:02 +0100 | [diff] [blame] | 645 | if (args->flags != 0 && |
| 646 | args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE) |
| 647 | return -EINVAL; |
| 648 | |
| 649 | if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE) |
| 650 | return drm_syncobj_import_sync_file_fence(file_private, |
| 651 | args->fd, |
| 652 | args->handle); |
| 653 | |
Dave Airlie | e908342 | 2017-04-04 13:26:24 +1000 | [diff] [blame] | 654 | return drm_syncobj_fd_to_handle(file_private, args->fd, |
| 655 | &args->handle); |
| 656 | } |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 657 | |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 658 | struct syncobj_wait_entry { |
| 659 | struct task_struct *task; |
| 660 | struct dma_fence *fence; |
| 661 | struct dma_fence_cb fence_cb; |
| 662 | struct drm_syncobj_cb syncobj_cb; |
| 663 | }; |
| 664 | |
| 665 | static void syncobj_wait_fence_func(struct dma_fence *fence, |
| 666 | struct dma_fence_cb *cb) |
| 667 | { |
| 668 | struct syncobj_wait_entry *wait = |
| 669 | container_of(cb, struct syncobj_wait_entry, fence_cb); |
| 670 | |
| 671 | wake_up_process(wait->task); |
| 672 | } |
| 673 | |
| 674 | static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj, |
| 675 | struct drm_syncobj_cb *cb) |
| 676 | { |
| 677 | struct syncobj_wait_entry *wait = |
| 678 | container_of(cb, struct syncobj_wait_entry, syncobj_cb); |
| 679 | |
| 680 | /* This happens inside the syncobj lock */ |
Ville Syrjälä | 563eaf5 | 2017-11-02 22:03:35 +0200 | [diff] [blame] | 681 | wait->fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, |
| 682 | lockdep_is_held(&syncobj->lock))); |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 683 | wake_up_process(wait->task); |
| 684 | } |
| 685 | |
| 686 | static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, |
| 687 | uint32_t count, |
| 688 | uint32_t flags, |
| 689 | signed long timeout, |
| 690 | uint32_t *idx) |
| 691 | { |
| 692 | struct syncobj_wait_entry *entries; |
| 693 | struct dma_fence *fence; |
| 694 | signed long ret; |
| 695 | uint32_t signaled_count, i; |
| 696 | |
| 697 | entries = kcalloc(count, sizeof(*entries), GFP_KERNEL); |
| 698 | if (!entries) |
| 699 | return -ENOMEM; |
| 700 | |
| 701 | /* Walk the list of sync objects and initialize entries. We do |
| 702 | * this up-front so that we can properly return -EINVAL if there is |
| 703 | * a syncobj with a missing fence and then never have the chance of |
| 704 | * returning -EINVAL again. |
| 705 | */ |
| 706 | signaled_count = 0; |
| 707 | for (i = 0; i < count; ++i) { |
| 708 | entries[i].task = current; |
| 709 | entries[i].fence = drm_syncobj_fence_get(syncobjs[i]); |
| 710 | if (!entries[i].fence) { |
| 711 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) { |
| 712 | continue; |
| 713 | } else { |
| 714 | ret = -EINVAL; |
| 715 | goto cleanup_entries; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | if (dma_fence_is_signaled(entries[i].fence)) { |
| 720 | if (signaled_count == 0 && idx) |
| 721 | *idx = i; |
| 722 | signaled_count++; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* Initialize ret to the max of timeout and 1. That way, the |
| 727 | * default return value indicates a successful wait and not a |
| 728 | * timeout. |
| 729 | */ |
| 730 | ret = max_t(signed long, timeout, 1); |
| 731 | |
| 732 | if (signaled_count == count || |
| 733 | (signaled_count > 0 && |
| 734 | !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL))) |
| 735 | goto cleanup_entries; |
| 736 | |
| 737 | /* There's a very annoying laxness in the dma_fence API here, in |
| 738 | * that backends are not required to automatically report when a |
| 739 | * fence is signaled prior to fence->ops->enable_signaling() being |
| 740 | * called. So here if we fail to match signaled_count, we need to |
| 741 | * fallthough and try a 0 timeout wait! |
| 742 | */ |
| 743 | |
| 744 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) { |
| 745 | for (i = 0; i < count; ++i) { |
| 746 | drm_syncobj_fence_get_or_add_callback(syncobjs[i], |
| 747 | &entries[i].fence, |
| 748 | &entries[i].syncobj_cb, |
| 749 | syncobj_wait_syncobj_func); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | do { |
| 754 | set_current_state(TASK_INTERRUPTIBLE); |
| 755 | |
| 756 | signaled_count = 0; |
| 757 | for (i = 0; i < count; ++i) { |
| 758 | fence = entries[i].fence; |
| 759 | if (!fence) |
| 760 | continue; |
| 761 | |
| 762 | if (dma_fence_is_signaled(fence) || |
| 763 | (!entries[i].fence_cb.func && |
| 764 | dma_fence_add_callback(fence, |
| 765 | &entries[i].fence_cb, |
| 766 | syncobj_wait_fence_func))) { |
| 767 | /* The fence has been signaled */ |
| 768 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) { |
| 769 | signaled_count++; |
| 770 | } else { |
| 771 | if (idx) |
| 772 | *idx = i; |
| 773 | goto done_waiting; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | if (signaled_count == count) |
| 779 | goto done_waiting; |
| 780 | |
| 781 | if (timeout == 0) { |
| 782 | /* If we are doing a 0 timeout wait and we got |
| 783 | * here, then we just timed out. |
| 784 | */ |
| 785 | ret = 0; |
| 786 | goto done_waiting; |
| 787 | } |
| 788 | |
| 789 | ret = schedule_timeout(ret); |
| 790 | |
| 791 | if (ret > 0 && signal_pending(current)) |
| 792 | ret = -ERESTARTSYS; |
| 793 | } while (ret > 0); |
| 794 | |
| 795 | done_waiting: |
| 796 | __set_current_state(TASK_RUNNING); |
| 797 | |
| 798 | cleanup_entries: |
| 799 | for (i = 0; i < count; ++i) { |
| 800 | if (entries[i].syncobj_cb.func) |
| 801 | drm_syncobj_remove_callback(syncobjs[i], |
| 802 | &entries[i].syncobj_cb); |
| 803 | if (entries[i].fence_cb.func) |
| 804 | dma_fence_remove_callback(entries[i].fence, |
| 805 | &entries[i].fence_cb); |
| 806 | dma_fence_put(entries[i].fence); |
| 807 | } |
| 808 | kfree(entries); |
| 809 | |
| 810 | return ret; |
| 811 | } |
| 812 | |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 813 | /** |
| 814 | * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value |
| 815 | * |
| 816 | * @timeout_nsec: timeout nsec component in ns, 0 for poll |
| 817 | * |
| 818 | * Calculate the timeout in jiffies from an absolute time in sec/nsec. |
| 819 | */ |
| 820 | static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec) |
| 821 | { |
| 822 | ktime_t abs_timeout, now; |
| 823 | u64 timeout_ns, timeout_jiffies64; |
| 824 | |
| 825 | /* make 0 timeout means poll - absolute 0 doesn't seem valid */ |
| 826 | if (timeout_nsec == 0) |
| 827 | return 0; |
| 828 | |
| 829 | abs_timeout = ns_to_ktime(timeout_nsec); |
| 830 | now = ktime_get(); |
| 831 | |
| 832 | if (!ktime_after(abs_timeout, now)) |
| 833 | return 0; |
| 834 | |
| 835 | timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now)); |
| 836 | |
| 837 | timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns); |
| 838 | /* clamp timeout to avoid infinite timeout */ |
| 839 | if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1) |
| 840 | return MAX_SCHEDULE_TIMEOUT - 1; |
| 841 | |
| 842 | return timeout_jiffies64 + 1; |
| 843 | } |
| 844 | |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 845 | static int drm_syncobj_array_wait(struct drm_device *dev, |
| 846 | struct drm_file *file_private, |
| 847 | struct drm_syncobj_wait *wait, |
| 848 | struct drm_syncobj **syncobjs) |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 849 | { |
| 850 | signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec); |
| 851 | signed long ret = 0; |
| 852 | uint32_t first = ~0; |
| 853 | |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 854 | ret = drm_syncobj_array_wait_timeout(syncobjs, |
| 855 | wait->count_handles, |
| 856 | wait->flags, |
| 857 | timeout, &first); |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 858 | if (ret < 0) |
| 859 | return ret; |
| 860 | |
| 861 | wait->first_signaled = first; |
| 862 | if (ret == 0) |
| 863 | return -ETIME; |
| 864 | return 0; |
| 865 | } |
| 866 | |
Jason Ekstrand | 3e6fb72 | 2017-08-25 10:52:26 -0700 | [diff] [blame] | 867 | static int drm_syncobj_array_find(struct drm_file *file_private, |
Ville Syrjälä | 9e55446 | 2017-09-01 19:53:26 +0300 | [diff] [blame] | 868 | void __user *user_handles, |
| 869 | uint32_t count_handles, |
Jason Ekstrand | 3e6fb72 | 2017-08-25 10:52:26 -0700 | [diff] [blame] | 870 | struct drm_syncobj ***syncobjs_out) |
| 871 | { |
| 872 | uint32_t i, *handles; |
| 873 | struct drm_syncobj **syncobjs; |
| 874 | int ret; |
| 875 | |
| 876 | handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL); |
| 877 | if (handles == NULL) |
| 878 | return -ENOMEM; |
| 879 | |
| 880 | if (copy_from_user(handles, user_handles, |
| 881 | sizeof(uint32_t) * count_handles)) { |
| 882 | ret = -EFAULT; |
| 883 | goto err_free_handles; |
| 884 | } |
| 885 | |
| 886 | syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL); |
| 887 | if (syncobjs == NULL) { |
| 888 | ret = -ENOMEM; |
| 889 | goto err_free_handles; |
| 890 | } |
| 891 | |
| 892 | for (i = 0; i < count_handles; i++) { |
| 893 | syncobjs[i] = drm_syncobj_find(file_private, handles[i]); |
| 894 | if (!syncobjs[i]) { |
| 895 | ret = -ENOENT; |
| 896 | goto err_put_syncobjs; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | kfree(handles); |
| 901 | *syncobjs_out = syncobjs; |
| 902 | return 0; |
| 903 | |
| 904 | err_put_syncobjs: |
| 905 | while (i-- > 0) |
| 906 | drm_syncobj_put(syncobjs[i]); |
| 907 | kfree(syncobjs); |
| 908 | err_free_handles: |
| 909 | kfree(handles); |
| 910 | |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | static void drm_syncobj_array_free(struct drm_syncobj **syncobjs, |
| 915 | uint32_t count) |
| 916 | { |
| 917 | uint32_t i; |
| 918 | for (i = 0; i < count; i++) |
| 919 | drm_syncobj_put(syncobjs[i]); |
| 920 | kfree(syncobjs); |
| 921 | } |
| 922 | |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 923 | int |
| 924 | drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, |
| 925 | struct drm_file *file_private) |
| 926 | { |
| 927 | struct drm_syncobj_wait *args = data; |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 928 | struct drm_syncobj **syncobjs; |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 929 | int ret = 0; |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 930 | |
| 931 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 932 | return -ENODEV; |
| 933 | |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 934 | if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL | |
| 935 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)) |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 936 | return -EINVAL; |
| 937 | |
| 938 | if (args->count_handles == 0) |
| 939 | return -EINVAL; |
| 940 | |
Jason Ekstrand | 3e6fb72 | 2017-08-25 10:52:26 -0700 | [diff] [blame] | 941 | ret = drm_syncobj_array_find(file_private, |
| 942 | u64_to_user_ptr(args->handles), |
| 943 | args->count_handles, |
| 944 | &syncobjs); |
| 945 | if (ret < 0) |
| 946 | return ret; |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 947 | |
Jason Ekstrand | e7aca503 | 2017-08-25 10:52:24 -0700 | [diff] [blame] | 948 | ret = drm_syncobj_array_wait(dev, file_private, |
| 949 | args, syncobjs); |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 950 | |
Jason Ekstrand | 3e6fb72 | 2017-08-25 10:52:26 -0700 | [diff] [blame] | 951 | drm_syncobj_array_free(syncobjs, args->count_handles); |
Dave Airlie | 5e60a10 | 2017-08-25 10:52:22 -0700 | [diff] [blame] | 952 | |
| 953 | return ret; |
| 954 | } |
Jason Ekstrand | aa4035d | 2017-08-28 14:10:27 -0700 | [diff] [blame] | 955 | |
| 956 | int |
| 957 | drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, |
| 958 | struct drm_file *file_private) |
| 959 | { |
| 960 | struct drm_syncobj_array *args = data; |
| 961 | struct drm_syncobj **syncobjs; |
| 962 | uint32_t i; |
| 963 | int ret; |
| 964 | |
| 965 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 966 | return -ENODEV; |
| 967 | |
| 968 | if (args->pad != 0) |
| 969 | return -EINVAL; |
| 970 | |
| 971 | if (args->count_handles == 0) |
| 972 | return -EINVAL; |
| 973 | |
| 974 | ret = drm_syncobj_array_find(file_private, |
| 975 | u64_to_user_ptr(args->handles), |
| 976 | args->count_handles, |
| 977 | &syncobjs); |
| 978 | if (ret < 0) |
| 979 | return ret; |
| 980 | |
| 981 | for (i = 0; i < args->count_handles; i++) |
| 982 | drm_syncobj_replace_fence(syncobjs[i], NULL); |
| 983 | |
| 984 | drm_syncobj_array_free(syncobjs, args->count_handles); |
| 985 | |
| 986 | return 0; |
| 987 | } |
Jason Ekstrand | ffa9443 | 2017-08-28 14:10:28 -0700 | [diff] [blame] | 988 | |
| 989 | int |
| 990 | drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, |
| 991 | struct drm_file *file_private) |
| 992 | { |
| 993 | struct drm_syncobj_array *args = data; |
| 994 | struct drm_syncobj **syncobjs; |
| 995 | uint32_t i; |
| 996 | int ret; |
| 997 | |
| 998 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 999 | return -ENODEV; |
| 1000 | |
| 1001 | if (args->pad != 0) |
| 1002 | return -EINVAL; |
| 1003 | |
| 1004 | if (args->count_handles == 0) |
| 1005 | return -EINVAL; |
| 1006 | |
| 1007 | ret = drm_syncobj_array_find(file_private, |
| 1008 | u64_to_user_ptr(args->handles), |
| 1009 | args->count_handles, |
| 1010 | &syncobjs); |
| 1011 | if (ret < 0) |
| 1012 | return ret; |
| 1013 | |
| 1014 | for (i = 0; i < args->count_handles; i++) { |
| 1015 | ret = drm_syncobj_assign_null_handle(syncobjs[i]); |
| 1016 | if (ret < 0) |
| 1017 | break; |
| 1018 | } |
| 1019 | |
| 1020 | drm_syncobj_array_free(syncobjs, args->count_handles); |
| 1021 | |
| 1022 | return ret; |
| 1023 | } |