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