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