b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017 Red Hat |
| 3 | * Parts ported from amdgpu (fence wait code). |
| 4 | * Copyright 2016 Advanced Micro Devices, Inc. |
| 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 | * |
| 32 | * DRM synchronisation objects (syncobj, see struct &drm_syncobj) provide a |
| 33 | * container for a synchronization primitive which can be used by userspace |
| 34 | * to explicitly synchronize GPU commands, can be shared between userspace |
| 35 | * processes, and can be shared between different DRM drivers. |
| 36 | * Their primary use-case is to implement Vulkan fences and semaphores. |
| 37 | * The syncobj userspace API provides ioctls for several operations: |
| 38 | * |
| 39 | * - Creation and destruction of syncobjs |
| 40 | * - Import and export of syncobjs to/from a syncobj file descriptor |
| 41 | * - Import and export a syncobj's underlying fence to/from a sync file |
| 42 | * - Reset a syncobj (set its fence to NULL) |
| 43 | * - Signal a syncobj (set a trivially signaled fence) |
| 44 | * - Wait for a syncobj's fence to appear and be signaled |
| 45 | * |
| 46 | * At it's core, a syncobj is simply a wrapper around a pointer to a struct |
| 47 | * &dma_fence which may be NULL. |
| 48 | * When a syncobj is first created, its pointer is either NULL or a pointer |
| 49 | * to an already signaled fence depending on whether the |
| 50 | * &DRM_SYNCOBJ_CREATE_SIGNALED flag is passed to |
| 51 | * &DRM_IOCTL_SYNCOBJ_CREATE. |
| 52 | * When GPU work which signals a syncobj is enqueued in a DRM driver, |
| 53 | * the syncobj fence is replaced with a fence which will be signaled by the |
| 54 | * completion of that work. |
| 55 | * When GPU work which waits on a syncobj is enqueued in a DRM driver, the |
| 56 | * driver retrieves syncobj's current fence at the time the work is enqueued |
| 57 | * waits on that fence before submitting the work to hardware. |
| 58 | * If the syncobj's fence is NULL, the enqueue operation is expected to fail. |
| 59 | * All manipulation of the syncobjs's fence happens in terms of the current |
| 60 | * fence at the time the ioctl is called by userspace regardless of whether |
| 61 | * that operation is an immediate host-side operation (signal or reset) or |
| 62 | * or an operation which is enqueued in some driver queue. |
| 63 | * &DRM_IOCTL_SYNCOBJ_RESET and &DRM_IOCTL_SYNCOBJ_SIGNAL can be used to |
| 64 | * manipulate a syncobj from the host by resetting its pointer to NULL or |
| 65 | * setting its pointer to a fence which is already signaled. |
| 66 | * |
| 67 | * |
| 68 | * Host-side wait on syncobjs |
| 69 | * -------------------------- |
| 70 | * |
| 71 | * &DRM_IOCTL_SYNCOBJ_WAIT takes an array of syncobj handles and does a |
| 72 | * host-side wait on all of the syncobj fences simultaneously. |
| 73 | * If &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL is set, the wait ioctl will wait on |
| 74 | * all of the syncobj fences to be signaled before it returns. |
| 75 | * Otherwise, it returns once at least one syncobj fence has been signaled |
| 76 | * and the index of a signaled fence is written back to the client. |
| 77 | * |
| 78 | * Unlike the enqueued GPU work dependencies which fail if they see a NULL |
| 79 | * fence in a syncobj, if &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT is set, |
| 80 | * the host-side wait will first wait for the syncobj to receive a non-NULL |
| 81 | * fence and then wait on that fence. |
| 82 | * If &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT is not set and any one of the |
| 83 | * syncobjs in the array has a NULL fence, -EINVAL will be returned. |
| 84 | * Assuming the syncobj starts off with a NULL fence, this allows a client |
| 85 | * to do a host wait in one thread (or process) which waits on GPU work |
| 86 | * submitted in another thread (or process) without having to manually |
| 87 | * synchronize between the two. |
| 88 | * This requirement is inherited from the Vulkan fence API. |
| 89 | * |
| 90 | * |
| 91 | * Import/export of syncobjs |
| 92 | * ------------------------- |
| 93 | * |
| 94 | * &DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE and &DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD |
| 95 | * provide two mechanisms for import/export of syncobjs. |
| 96 | * |
| 97 | * The first lets the client import or export an entire syncobj to a file |
| 98 | * descriptor. |
| 99 | * These fd's are opaque and have no other use case, except passing the |
| 100 | * syncobj between processes. |
| 101 | * All exported file descriptors and any syncobj handles created as a |
| 102 | * result of importing those file descriptors own a reference to the |
| 103 | * same underlying struct &drm_syncobj and the syncobj can be used |
| 104 | * persistently across all the processes with which it is shared. |
| 105 | * The syncobj is freed only once the last reference is dropped. |
| 106 | * Unlike dma-buf, importing a syncobj creates a new handle (with its own |
| 107 | * reference) for every import instead of de-duplicating. |
| 108 | * The primary use-case of this persistent import/export is for shared |
| 109 | * Vulkan fences and semaphores. |
| 110 | * |
| 111 | * The second import/export mechanism, which is indicated by |
| 112 | * &DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE or |
| 113 | * &DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE lets the client |
| 114 | * import/export the syncobj's current fence from/to a &sync_file. |
| 115 | * When a syncobj is exported to a sync file, that sync file wraps the |
| 116 | * sycnobj's fence at the time of export and any later signal or reset |
| 117 | * operations on the syncobj will not affect the exported sync file. |
| 118 | * When a sync file is imported into a syncobj, the syncobj's fence is set |
| 119 | * to the fence wrapped by that sync file. |
| 120 | * Because sync files are immutable, resetting or signaling the syncobj |
| 121 | * will not affect any sync files whose fences have been imported into the |
| 122 | * syncobj. |
| 123 | */ |
| 124 | |
| 125 | #include <linux/anon_inodes.h> |
| 126 | #include <linux/file.h> |
| 127 | #include <linux/fs.h> |
| 128 | #include <linux/sched/signal.h> |
| 129 | #include <linux/sync_file.h> |
| 130 | #include <linux/uaccess.h> |
| 131 | |
| 132 | #include <drm/drm.h> |
| 133 | #include <drm/drm_drv.h> |
| 134 | #include <drm/drm_file.h> |
| 135 | #include <drm/drm_gem.h> |
| 136 | #include <drm/drm_print.h> |
| 137 | #include <drm/drm_syncobj.h> |
| 138 | |
| 139 | #include "drm_internal.h" |
| 140 | |
| 141 | struct syncobj_wait_entry { |
| 142 | struct list_head node; |
| 143 | struct task_struct *task; |
| 144 | struct dma_fence *fence; |
| 145 | struct dma_fence_cb fence_cb; |
| 146 | u64 point; |
| 147 | }; |
| 148 | |
| 149 | static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj, |
| 150 | struct syncobj_wait_entry *wait); |
| 151 | |
| 152 | /** |
| 153 | * drm_syncobj_find - lookup and reference a sync object. |
| 154 | * @file_private: drm file private pointer |
| 155 | * @handle: sync object handle to lookup. |
| 156 | * |
| 157 | * Returns a reference to the syncobj pointed to by handle or NULL. The |
| 158 | * reference must be released by calling drm_syncobj_put(). |
| 159 | */ |
| 160 | struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private, |
| 161 | u32 handle) |
| 162 | { |
| 163 | struct drm_syncobj *syncobj; |
| 164 | |
| 165 | spin_lock(&file_private->syncobj_table_lock); |
| 166 | |
| 167 | /* Check if we currently have a reference on the object */ |
| 168 | syncobj = idr_find(&file_private->syncobj_idr, handle); |
| 169 | if (syncobj) |
| 170 | drm_syncobj_get(syncobj); |
| 171 | |
| 172 | spin_unlock(&file_private->syncobj_table_lock); |
| 173 | |
| 174 | return syncobj; |
| 175 | } |
| 176 | EXPORT_SYMBOL(drm_syncobj_find); |
| 177 | |
| 178 | static void drm_syncobj_fence_add_wait(struct drm_syncobj *syncobj, |
| 179 | struct syncobj_wait_entry *wait) |
| 180 | { |
| 181 | struct dma_fence *fence; |
| 182 | |
| 183 | if (wait->fence) |
| 184 | return; |
| 185 | |
| 186 | spin_lock(&syncobj->lock); |
| 187 | /* We've already tried once to get a fence and failed. Now that we |
| 188 | * have the lock, try one more time just to be sure we don't add a |
| 189 | * callback when a fence has already been set. |
| 190 | */ |
| 191 | fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, 1)); |
| 192 | if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) { |
| 193 | dma_fence_put(fence); |
| 194 | list_add_tail(&wait->node, &syncobj->cb_list); |
| 195 | } else if (!fence) { |
| 196 | wait->fence = dma_fence_get_stub(); |
| 197 | } else { |
| 198 | wait->fence = fence; |
| 199 | } |
| 200 | spin_unlock(&syncobj->lock); |
| 201 | } |
| 202 | |
| 203 | static void drm_syncobj_remove_wait(struct drm_syncobj *syncobj, |
| 204 | struct syncobj_wait_entry *wait) |
| 205 | { |
| 206 | if (!wait->node.next) |
| 207 | return; |
| 208 | |
| 209 | spin_lock(&syncobj->lock); |
| 210 | list_del_init(&wait->node); |
| 211 | spin_unlock(&syncobj->lock); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * drm_syncobj_add_point - add new timeline point to the syncobj |
| 216 | * @syncobj: sync object to add timeline point do |
| 217 | * @chain: chain node to use to add the point |
| 218 | * @fence: fence to encapsulate in the chain node |
| 219 | * @point: sequence number to use for the point |
| 220 | * |
| 221 | * Add the chain node as new timeline point to the syncobj. |
| 222 | */ |
| 223 | void drm_syncobj_add_point(struct drm_syncobj *syncobj, |
| 224 | struct dma_fence_chain *chain, |
| 225 | struct dma_fence *fence, |
| 226 | uint64_t point) |
| 227 | { |
| 228 | struct syncobj_wait_entry *cur, *tmp; |
| 229 | struct dma_fence *prev; |
| 230 | |
| 231 | dma_fence_get(fence); |
| 232 | |
| 233 | spin_lock(&syncobj->lock); |
| 234 | |
| 235 | prev = drm_syncobj_fence_get(syncobj); |
| 236 | /* You are adding an unorder point to timeline, which could cause payload returned from query_ioctl is 0! */ |
| 237 | if (prev && prev->seqno >= point) |
| 238 | DRM_ERROR("You are adding an unorder point to timeline!\n"); |
| 239 | dma_fence_chain_init(chain, prev, fence, point); |
| 240 | rcu_assign_pointer(syncobj->fence, &chain->base); |
| 241 | |
| 242 | list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) |
| 243 | syncobj_wait_syncobj_func(syncobj, cur); |
| 244 | spin_unlock(&syncobj->lock); |
| 245 | |
| 246 | /* Walk the chain once to trigger garbage collection */ |
| 247 | dma_fence_chain_for_each(fence, prev); |
| 248 | dma_fence_put(prev); |
| 249 | } |
| 250 | EXPORT_SYMBOL(drm_syncobj_add_point); |
| 251 | |
| 252 | /** |
| 253 | * drm_syncobj_replace_fence - replace fence in a sync object. |
| 254 | * @syncobj: Sync object to replace fence in |
| 255 | * @fence: fence to install in sync file. |
| 256 | * |
| 257 | * This replaces the fence on a sync object. |
| 258 | */ |
| 259 | void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, |
| 260 | struct dma_fence *fence) |
| 261 | { |
| 262 | struct dma_fence *old_fence; |
| 263 | struct syncobj_wait_entry *cur, *tmp; |
| 264 | |
| 265 | if (fence) |
| 266 | dma_fence_get(fence); |
| 267 | |
| 268 | spin_lock(&syncobj->lock); |
| 269 | |
| 270 | old_fence = rcu_dereference_protected(syncobj->fence, |
| 271 | lockdep_is_held(&syncobj->lock)); |
| 272 | rcu_assign_pointer(syncobj->fence, fence); |
| 273 | |
| 274 | if (fence != old_fence) { |
| 275 | list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) |
| 276 | syncobj_wait_syncobj_func(syncobj, cur); |
| 277 | } |
| 278 | |
| 279 | spin_unlock(&syncobj->lock); |
| 280 | |
| 281 | dma_fence_put(old_fence); |
| 282 | } |
| 283 | EXPORT_SYMBOL(drm_syncobj_replace_fence); |
| 284 | |
| 285 | /** |
| 286 | * drm_syncobj_assign_null_handle - assign a stub fence to the sync object |
| 287 | * @syncobj: sync object to assign the fence on |
| 288 | * |
| 289 | * Assign a already signaled stub fence to the sync object. |
| 290 | */ |
| 291 | static void drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj) |
| 292 | { |
| 293 | struct dma_fence *fence = dma_fence_get_stub(); |
| 294 | |
| 295 | drm_syncobj_replace_fence(syncobj, fence); |
| 296 | dma_fence_put(fence); |
| 297 | } |
| 298 | |
| 299 | /* 5s default for wait submission */ |
| 300 | #define DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT 5000000000ULL |
| 301 | /** |
| 302 | * drm_syncobj_find_fence - lookup and reference the fence in a sync object |
| 303 | * @file_private: drm file private pointer |
| 304 | * @handle: sync object handle to lookup. |
| 305 | * @point: timeline point |
| 306 | * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not |
| 307 | * @fence: out parameter for the fence |
| 308 | * |
| 309 | * This is just a convenience function that combines drm_syncobj_find() and |
| 310 | * drm_syncobj_fence_get(). |
| 311 | * |
| 312 | * Returns 0 on success or a negative error value on failure. On success @fence |
| 313 | * contains a reference to the fence, which must be released by calling |
| 314 | * dma_fence_put(). |
| 315 | */ |
| 316 | int drm_syncobj_find_fence(struct drm_file *file_private, |
| 317 | u32 handle, u64 point, u64 flags, |
| 318 | struct dma_fence **fence) |
| 319 | { |
| 320 | struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle); |
| 321 | struct syncobj_wait_entry wait; |
| 322 | u64 timeout = nsecs_to_jiffies64(DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT); |
| 323 | int ret; |
| 324 | |
| 325 | if (!syncobj) |
| 326 | return -ENOENT; |
| 327 | |
| 328 | /* Waiting for userspace with locks help is illegal cause that can |
| 329 | * trivial deadlock with page faults for example. Make lockdep complain |
| 330 | * about it early on. |
| 331 | */ |
| 332 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) { |
| 333 | might_sleep(); |
| 334 | lockdep_assert_none_held_once(); |
| 335 | } |
| 336 | |
| 337 | *fence = drm_syncobj_fence_get(syncobj); |
| 338 | |
| 339 | if (*fence) { |
| 340 | ret = dma_fence_chain_find_seqno(fence, point); |
| 341 | if (!ret) { |
| 342 | /* If the requested seqno is already signaled |
| 343 | * drm_syncobj_find_fence may return a NULL |
| 344 | * fence. To make sure the recipient gets |
| 345 | * signalled, use a new fence instead. |
| 346 | */ |
| 347 | if (!*fence) |
| 348 | *fence = dma_fence_get_stub(); |
| 349 | |
| 350 | goto out; |
| 351 | } |
| 352 | dma_fence_put(*fence); |
| 353 | } else { |
| 354 | ret = -EINVAL; |
| 355 | } |
| 356 | |
| 357 | if (!(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)) |
| 358 | goto out; |
| 359 | |
| 360 | memset(&wait, 0, sizeof(wait)); |
| 361 | wait.task = current; |
| 362 | wait.point = point; |
| 363 | drm_syncobj_fence_add_wait(syncobj, &wait); |
| 364 | |
| 365 | do { |
| 366 | set_current_state(TASK_INTERRUPTIBLE); |
| 367 | if (wait.fence) { |
| 368 | ret = 0; |
| 369 | break; |
| 370 | } |
| 371 | if (timeout == 0) { |
| 372 | ret = -ETIME; |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | if (signal_pending(current)) { |
| 377 | ret = -ERESTARTSYS; |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | timeout = schedule_timeout(timeout); |
| 382 | } while (1); |
| 383 | |
| 384 | __set_current_state(TASK_RUNNING); |
| 385 | *fence = wait.fence; |
| 386 | |
| 387 | if (wait.node.next) |
| 388 | drm_syncobj_remove_wait(syncobj, &wait); |
| 389 | |
| 390 | out: |
| 391 | drm_syncobj_put(syncobj); |
| 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | EXPORT_SYMBOL(drm_syncobj_find_fence); |
| 396 | |
| 397 | /** |
| 398 | * drm_syncobj_free - free a sync object. |
| 399 | * @kref: kref to free. |
| 400 | * |
| 401 | * Only to be called from kref_put in drm_syncobj_put. |
| 402 | */ |
| 403 | void drm_syncobj_free(struct kref *kref) |
| 404 | { |
| 405 | struct drm_syncobj *syncobj = container_of(kref, |
| 406 | struct drm_syncobj, |
| 407 | refcount); |
| 408 | drm_syncobj_replace_fence(syncobj, NULL); |
| 409 | kfree(syncobj); |
| 410 | } |
| 411 | EXPORT_SYMBOL(drm_syncobj_free); |
| 412 | |
| 413 | /** |
| 414 | * drm_syncobj_create - create a new syncobj |
| 415 | * @out_syncobj: returned syncobj |
| 416 | * @flags: DRM_SYNCOBJ_* flags |
| 417 | * @fence: if non-NULL, the syncobj will represent this fence |
| 418 | * |
| 419 | * This is the first function to create a sync object. After creating, drivers |
| 420 | * probably want to make it available to userspace, either through |
| 421 | * drm_syncobj_get_handle() or drm_syncobj_get_fd(). |
| 422 | * |
| 423 | * Returns 0 on success or a negative error value on failure. |
| 424 | */ |
| 425 | int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags, |
| 426 | struct dma_fence *fence) |
| 427 | { |
| 428 | struct drm_syncobj *syncobj; |
| 429 | |
| 430 | syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL); |
| 431 | if (!syncobj) |
| 432 | return -ENOMEM; |
| 433 | |
| 434 | kref_init(&syncobj->refcount); |
| 435 | INIT_LIST_HEAD(&syncobj->cb_list); |
| 436 | spin_lock_init(&syncobj->lock); |
| 437 | |
| 438 | if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) |
| 439 | drm_syncobj_assign_null_handle(syncobj); |
| 440 | |
| 441 | if (fence) |
| 442 | drm_syncobj_replace_fence(syncobj, fence); |
| 443 | |
| 444 | *out_syncobj = syncobj; |
| 445 | return 0; |
| 446 | } |
| 447 | EXPORT_SYMBOL(drm_syncobj_create); |
| 448 | |
| 449 | /** |
| 450 | * drm_syncobj_get_handle - get a handle from a syncobj |
| 451 | * @file_private: drm file private pointer |
| 452 | * @syncobj: Sync object to export |
| 453 | * @handle: out parameter with the new handle |
| 454 | * |
| 455 | * Exports a sync object created with drm_syncobj_create() as a handle on |
| 456 | * @file_private to userspace. |
| 457 | * |
| 458 | * Returns 0 on success or a negative error value on failure. |
| 459 | */ |
| 460 | int drm_syncobj_get_handle(struct drm_file *file_private, |
| 461 | struct drm_syncobj *syncobj, u32 *handle) |
| 462 | { |
| 463 | int ret; |
| 464 | |
| 465 | /* take a reference to put in the idr */ |
| 466 | drm_syncobj_get(syncobj); |
| 467 | |
| 468 | idr_preload(GFP_KERNEL); |
| 469 | spin_lock(&file_private->syncobj_table_lock); |
| 470 | ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT); |
| 471 | spin_unlock(&file_private->syncobj_table_lock); |
| 472 | |
| 473 | idr_preload_end(); |
| 474 | |
| 475 | if (ret < 0) { |
| 476 | drm_syncobj_put(syncobj); |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | *handle = ret; |
| 481 | return 0; |
| 482 | } |
| 483 | EXPORT_SYMBOL(drm_syncobj_get_handle); |
| 484 | |
| 485 | static int drm_syncobj_create_as_handle(struct drm_file *file_private, |
| 486 | u32 *handle, uint32_t flags) |
| 487 | { |
| 488 | int ret; |
| 489 | struct drm_syncobj *syncobj; |
| 490 | |
| 491 | ret = drm_syncobj_create(&syncobj, flags, NULL); |
| 492 | if (ret) |
| 493 | return ret; |
| 494 | |
| 495 | ret = drm_syncobj_get_handle(file_private, syncobj, handle); |
| 496 | drm_syncobj_put(syncobj); |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static int drm_syncobj_destroy(struct drm_file *file_private, |
| 501 | u32 handle) |
| 502 | { |
| 503 | struct drm_syncobj *syncobj; |
| 504 | |
| 505 | spin_lock(&file_private->syncobj_table_lock); |
| 506 | syncobj = idr_remove(&file_private->syncobj_idr, handle); |
| 507 | spin_unlock(&file_private->syncobj_table_lock); |
| 508 | |
| 509 | if (!syncobj) |
| 510 | return -EINVAL; |
| 511 | |
| 512 | drm_syncobj_put(syncobj); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int drm_syncobj_file_release(struct inode *inode, struct file *file) |
| 517 | { |
| 518 | struct drm_syncobj *syncobj = file->private_data; |
| 519 | |
| 520 | drm_syncobj_put(syncobj); |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static const struct file_operations drm_syncobj_file_fops = { |
| 525 | .release = drm_syncobj_file_release, |
| 526 | }; |
| 527 | |
| 528 | /** |
| 529 | * drm_syncobj_get_fd - get a file descriptor from a syncobj |
| 530 | * @syncobj: Sync object to export |
| 531 | * @p_fd: out parameter with the new file descriptor |
| 532 | * |
| 533 | * Exports a sync object created with drm_syncobj_create() as a file descriptor. |
| 534 | * |
| 535 | * Returns 0 on success or a negative error value on failure. |
| 536 | */ |
| 537 | int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd) |
| 538 | { |
| 539 | struct file *file; |
| 540 | int fd; |
| 541 | |
| 542 | fd = get_unused_fd_flags(O_CLOEXEC); |
| 543 | if (fd < 0) |
| 544 | return fd; |
| 545 | |
| 546 | file = anon_inode_getfile("syncobj_file", |
| 547 | &drm_syncobj_file_fops, |
| 548 | syncobj, 0); |
| 549 | if (IS_ERR(file)) { |
| 550 | put_unused_fd(fd); |
| 551 | return PTR_ERR(file); |
| 552 | } |
| 553 | |
| 554 | drm_syncobj_get(syncobj); |
| 555 | fd_install(fd, file); |
| 556 | |
| 557 | *p_fd = fd; |
| 558 | return 0; |
| 559 | } |
| 560 | EXPORT_SYMBOL(drm_syncobj_get_fd); |
| 561 | |
| 562 | static int drm_syncobj_handle_to_fd(struct drm_file *file_private, |
| 563 | u32 handle, int *p_fd) |
| 564 | { |
| 565 | struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle); |
| 566 | int ret; |
| 567 | |
| 568 | if (!syncobj) |
| 569 | return -EINVAL; |
| 570 | |
| 571 | ret = drm_syncobj_get_fd(syncobj, p_fd); |
| 572 | drm_syncobj_put(syncobj); |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | static int drm_syncobj_fd_to_handle(struct drm_file *file_private, |
| 577 | int fd, u32 *handle) |
| 578 | { |
| 579 | struct drm_syncobj *syncobj; |
| 580 | struct fd f = fdget(fd); |
| 581 | int ret; |
| 582 | |
| 583 | if (!f.file) |
| 584 | return -EINVAL; |
| 585 | |
| 586 | if (f.file->f_op != &drm_syncobj_file_fops) { |
| 587 | fdput(f); |
| 588 | return -EINVAL; |
| 589 | } |
| 590 | |
| 591 | /* take a reference to put in the idr */ |
| 592 | syncobj = f.file->private_data; |
| 593 | drm_syncobj_get(syncobj); |
| 594 | |
| 595 | idr_preload(GFP_KERNEL); |
| 596 | spin_lock(&file_private->syncobj_table_lock); |
| 597 | ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT); |
| 598 | spin_unlock(&file_private->syncobj_table_lock); |
| 599 | idr_preload_end(); |
| 600 | |
| 601 | if (ret > 0) { |
| 602 | *handle = ret; |
| 603 | ret = 0; |
| 604 | } else |
| 605 | drm_syncobj_put(syncobj); |
| 606 | |
| 607 | fdput(f); |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private, |
| 612 | int fd, int handle) |
| 613 | { |
| 614 | struct dma_fence *fence = sync_file_get_fence(fd); |
| 615 | struct drm_syncobj *syncobj; |
| 616 | |
| 617 | if (!fence) |
| 618 | return -EINVAL; |
| 619 | |
| 620 | syncobj = drm_syncobj_find(file_private, handle); |
| 621 | if (!syncobj) { |
| 622 | dma_fence_put(fence); |
| 623 | return -ENOENT; |
| 624 | } |
| 625 | |
| 626 | drm_syncobj_replace_fence(syncobj, fence); |
| 627 | dma_fence_put(fence); |
| 628 | drm_syncobj_put(syncobj); |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static int drm_syncobj_export_sync_file(struct drm_file *file_private, |
| 633 | int handle, int *p_fd) |
| 634 | { |
| 635 | int ret; |
| 636 | struct dma_fence *fence; |
| 637 | struct sync_file *sync_file; |
| 638 | int fd = get_unused_fd_flags(O_CLOEXEC); |
| 639 | |
| 640 | if (fd < 0) |
| 641 | return fd; |
| 642 | |
| 643 | ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence); |
| 644 | if (ret) |
| 645 | goto err_put_fd; |
| 646 | |
| 647 | sync_file = sync_file_create(fence); |
| 648 | |
| 649 | dma_fence_put(fence); |
| 650 | |
| 651 | if (!sync_file) { |
| 652 | ret = -EINVAL; |
| 653 | goto err_put_fd; |
| 654 | } |
| 655 | |
| 656 | fd_install(fd, sync_file->file); |
| 657 | |
| 658 | *p_fd = fd; |
| 659 | return 0; |
| 660 | err_put_fd: |
| 661 | put_unused_fd(fd); |
| 662 | return ret; |
| 663 | } |
| 664 | /** |
| 665 | * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time |
| 666 | * @file_private: drm file-private structure to set up |
| 667 | * |
| 668 | * Called at device open time, sets up the structure for handling refcounting |
| 669 | * of sync objects. |
| 670 | */ |
| 671 | void |
| 672 | drm_syncobj_open(struct drm_file *file_private) |
| 673 | { |
| 674 | idr_init_base(&file_private->syncobj_idr, 1); |
| 675 | spin_lock_init(&file_private->syncobj_table_lock); |
| 676 | } |
| 677 | |
| 678 | static int |
| 679 | drm_syncobj_release_handle(int id, void *ptr, void *data) |
| 680 | { |
| 681 | struct drm_syncobj *syncobj = ptr; |
| 682 | |
| 683 | drm_syncobj_put(syncobj); |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * drm_syncobj_release - release file-private sync object resources |
| 689 | * @file_private: drm file-private structure to clean up |
| 690 | * |
| 691 | * Called at close time when the filp is going away. |
| 692 | * |
| 693 | * Releases any remaining references on objects by this filp. |
| 694 | */ |
| 695 | void |
| 696 | drm_syncobj_release(struct drm_file *file_private) |
| 697 | { |
| 698 | idr_for_each(&file_private->syncobj_idr, |
| 699 | &drm_syncobj_release_handle, file_private); |
| 700 | idr_destroy(&file_private->syncobj_idr); |
| 701 | } |
| 702 | |
| 703 | int |
| 704 | drm_syncobj_create_ioctl(struct drm_device *dev, void *data, |
| 705 | struct drm_file *file_private) |
| 706 | { |
| 707 | struct drm_syncobj_create *args = data; |
| 708 | |
| 709 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 710 | return -EOPNOTSUPP; |
| 711 | |
| 712 | /* no valid flags yet */ |
| 713 | if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED) |
| 714 | return -EINVAL; |
| 715 | |
| 716 | return drm_syncobj_create_as_handle(file_private, |
| 717 | &args->handle, args->flags); |
| 718 | } |
| 719 | |
| 720 | int |
| 721 | drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data, |
| 722 | struct drm_file *file_private) |
| 723 | { |
| 724 | struct drm_syncobj_destroy *args = data; |
| 725 | |
| 726 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 727 | return -EOPNOTSUPP; |
| 728 | |
| 729 | /* make sure padding is empty */ |
| 730 | if (args->pad) |
| 731 | return -EINVAL; |
| 732 | return drm_syncobj_destroy(file_private, args->handle); |
| 733 | } |
| 734 | |
| 735 | int |
| 736 | drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data, |
| 737 | struct drm_file *file_private) |
| 738 | { |
| 739 | struct drm_syncobj_handle *args = data; |
| 740 | |
| 741 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 742 | return -EOPNOTSUPP; |
| 743 | |
| 744 | if (args->pad) |
| 745 | return -EINVAL; |
| 746 | |
| 747 | if (args->flags != 0 && |
| 748 | args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE) |
| 752 | return drm_syncobj_export_sync_file(file_private, args->handle, |
| 753 | &args->fd); |
| 754 | |
| 755 | return drm_syncobj_handle_to_fd(file_private, args->handle, |
| 756 | &args->fd); |
| 757 | } |
| 758 | |
| 759 | int |
| 760 | drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data, |
| 761 | struct drm_file *file_private) |
| 762 | { |
| 763 | struct drm_syncobj_handle *args = data; |
| 764 | |
| 765 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 766 | return -EOPNOTSUPP; |
| 767 | |
| 768 | if (args->pad) |
| 769 | return -EINVAL; |
| 770 | |
| 771 | if (args->flags != 0 && |
| 772 | args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE) |
| 776 | return drm_syncobj_import_sync_file_fence(file_private, |
| 777 | args->fd, |
| 778 | args->handle); |
| 779 | |
| 780 | return drm_syncobj_fd_to_handle(file_private, args->fd, |
| 781 | &args->handle); |
| 782 | } |
| 783 | |
| 784 | static int drm_syncobj_transfer_to_timeline(struct drm_file *file_private, |
| 785 | struct drm_syncobj_transfer *args) |
| 786 | { |
| 787 | struct drm_syncobj *timeline_syncobj = NULL; |
| 788 | struct dma_fence *fence; |
| 789 | struct dma_fence_chain *chain; |
| 790 | int ret; |
| 791 | |
| 792 | timeline_syncobj = drm_syncobj_find(file_private, args->dst_handle); |
| 793 | if (!timeline_syncobj) { |
| 794 | return -ENOENT; |
| 795 | } |
| 796 | ret = drm_syncobj_find_fence(file_private, args->src_handle, |
| 797 | args->src_point, args->flags, |
| 798 | &fence); |
| 799 | if (ret) |
| 800 | goto err; |
| 801 | chain = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL); |
| 802 | if (!chain) { |
| 803 | ret = -ENOMEM; |
| 804 | goto err1; |
| 805 | } |
| 806 | drm_syncobj_add_point(timeline_syncobj, chain, fence, args->dst_point); |
| 807 | err1: |
| 808 | dma_fence_put(fence); |
| 809 | err: |
| 810 | drm_syncobj_put(timeline_syncobj); |
| 811 | |
| 812 | return ret; |
| 813 | } |
| 814 | |
| 815 | static int |
| 816 | drm_syncobj_transfer_to_binary(struct drm_file *file_private, |
| 817 | struct drm_syncobj_transfer *args) |
| 818 | { |
| 819 | struct drm_syncobj *binary_syncobj = NULL; |
| 820 | struct dma_fence *fence; |
| 821 | int ret; |
| 822 | |
| 823 | binary_syncobj = drm_syncobj_find(file_private, args->dst_handle); |
| 824 | if (!binary_syncobj) |
| 825 | return -ENOENT; |
| 826 | ret = drm_syncobj_find_fence(file_private, args->src_handle, |
| 827 | args->src_point, args->flags, &fence); |
| 828 | if (ret) |
| 829 | goto err; |
| 830 | drm_syncobj_replace_fence(binary_syncobj, fence); |
| 831 | dma_fence_put(fence); |
| 832 | err: |
| 833 | drm_syncobj_put(binary_syncobj); |
| 834 | |
| 835 | return ret; |
| 836 | } |
| 837 | int |
| 838 | drm_syncobj_transfer_ioctl(struct drm_device *dev, void *data, |
| 839 | struct drm_file *file_private) |
| 840 | { |
| 841 | struct drm_syncobj_transfer *args = data; |
| 842 | int ret; |
| 843 | |
| 844 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE)) |
| 845 | return -EOPNOTSUPP; |
| 846 | |
| 847 | if (args->pad) |
| 848 | return -EINVAL; |
| 849 | |
| 850 | if (args->dst_point) |
| 851 | ret = drm_syncobj_transfer_to_timeline(file_private, args); |
| 852 | else |
| 853 | ret = drm_syncobj_transfer_to_binary(file_private, args); |
| 854 | |
| 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | static void syncobj_wait_fence_func(struct dma_fence *fence, |
| 859 | struct dma_fence_cb *cb) |
| 860 | { |
| 861 | struct syncobj_wait_entry *wait = |
| 862 | container_of(cb, struct syncobj_wait_entry, fence_cb); |
| 863 | |
| 864 | wake_up_process(wait->task); |
| 865 | } |
| 866 | |
| 867 | static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj, |
| 868 | struct syncobj_wait_entry *wait) |
| 869 | { |
| 870 | struct dma_fence *fence; |
| 871 | |
| 872 | /* This happens inside the syncobj lock */ |
| 873 | fence = rcu_dereference_protected(syncobj->fence, |
| 874 | lockdep_is_held(&syncobj->lock)); |
| 875 | dma_fence_get(fence); |
| 876 | if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) { |
| 877 | dma_fence_put(fence); |
| 878 | return; |
| 879 | } else if (!fence) { |
| 880 | wait->fence = dma_fence_get_stub(); |
| 881 | } else { |
| 882 | wait->fence = fence; |
| 883 | } |
| 884 | |
| 885 | wake_up_process(wait->task); |
| 886 | list_del_init(&wait->node); |
| 887 | } |
| 888 | |
| 889 | static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, |
| 890 | void __user *user_points, |
| 891 | uint32_t count, |
| 892 | uint32_t flags, |
| 893 | signed long timeout, |
| 894 | uint32_t *idx) |
| 895 | { |
| 896 | struct syncobj_wait_entry *entries; |
| 897 | struct dma_fence *fence; |
| 898 | uint64_t *points; |
| 899 | uint32_t signaled_count, i; |
| 900 | |
| 901 | if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT | |
| 902 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) |
| 903 | lockdep_assert_none_held_once(); |
| 904 | |
| 905 | points = kmalloc_array(count, sizeof(*points), GFP_KERNEL); |
| 906 | if (points == NULL) |
| 907 | return -ENOMEM; |
| 908 | |
| 909 | if (!user_points) { |
| 910 | memset(points, 0, count * sizeof(uint64_t)); |
| 911 | |
| 912 | } else if (copy_from_user(points, user_points, |
| 913 | sizeof(uint64_t) * count)) { |
| 914 | timeout = -EFAULT; |
| 915 | goto err_free_points; |
| 916 | } |
| 917 | |
| 918 | entries = kcalloc(count, sizeof(*entries), GFP_KERNEL); |
| 919 | if (!entries) { |
| 920 | timeout = -ENOMEM; |
| 921 | goto err_free_points; |
| 922 | } |
| 923 | /* Walk the list of sync objects and initialize entries. We do |
| 924 | * this up-front so that we can properly return -EINVAL if there is |
| 925 | * a syncobj with a missing fence and then never have the chance of |
| 926 | * returning -EINVAL again. |
| 927 | */ |
| 928 | signaled_count = 0; |
| 929 | for (i = 0; i < count; ++i) { |
| 930 | struct dma_fence *fence; |
| 931 | |
| 932 | entries[i].task = current; |
| 933 | entries[i].point = points[i]; |
| 934 | fence = drm_syncobj_fence_get(syncobjs[i]); |
| 935 | if (!fence || dma_fence_chain_find_seqno(&fence, points[i])) { |
| 936 | dma_fence_put(fence); |
| 937 | if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT | |
| 938 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) { |
| 939 | continue; |
| 940 | } else { |
| 941 | timeout = -EINVAL; |
| 942 | goto cleanup_entries; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | if (fence) |
| 947 | entries[i].fence = fence; |
| 948 | else |
| 949 | entries[i].fence = dma_fence_get_stub(); |
| 950 | |
| 951 | if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) || |
| 952 | dma_fence_is_signaled(entries[i].fence)) { |
| 953 | if (signaled_count == 0 && idx) |
| 954 | *idx = i; |
| 955 | signaled_count++; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (signaled_count == count || |
| 960 | (signaled_count > 0 && |
| 961 | !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL))) |
| 962 | goto cleanup_entries; |
| 963 | |
| 964 | /* There's a very annoying laxness in the dma_fence API here, in |
| 965 | * that backends are not required to automatically report when a |
| 966 | * fence is signaled prior to fence->ops->enable_signaling() being |
| 967 | * called. So here if we fail to match signaled_count, we need to |
| 968 | * fallthough and try a 0 timeout wait! |
| 969 | */ |
| 970 | |
| 971 | if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT | |
| 972 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) { |
| 973 | for (i = 0; i < count; ++i) |
| 974 | drm_syncobj_fence_add_wait(syncobjs[i], &entries[i]); |
| 975 | } |
| 976 | |
| 977 | do { |
| 978 | set_current_state(TASK_INTERRUPTIBLE); |
| 979 | |
| 980 | signaled_count = 0; |
| 981 | for (i = 0; i < count; ++i) { |
| 982 | fence = entries[i].fence; |
| 983 | if (!fence) |
| 984 | continue; |
| 985 | |
| 986 | if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) || |
| 987 | dma_fence_is_signaled(fence) || |
| 988 | (!entries[i].fence_cb.func && |
| 989 | dma_fence_add_callback(fence, |
| 990 | &entries[i].fence_cb, |
| 991 | syncobj_wait_fence_func))) { |
| 992 | /* The fence has been signaled */ |
| 993 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) { |
| 994 | signaled_count++; |
| 995 | } else { |
| 996 | if (idx) |
| 997 | *idx = i; |
| 998 | goto done_waiting; |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | if (signaled_count == count) |
| 1004 | goto done_waiting; |
| 1005 | |
| 1006 | if (timeout == 0) { |
| 1007 | timeout = -ETIME; |
| 1008 | goto done_waiting; |
| 1009 | } |
| 1010 | |
| 1011 | if (signal_pending(current)) { |
| 1012 | timeout = -ERESTARTSYS; |
| 1013 | goto done_waiting; |
| 1014 | } |
| 1015 | |
| 1016 | timeout = schedule_timeout(timeout); |
| 1017 | } while (1); |
| 1018 | |
| 1019 | done_waiting: |
| 1020 | __set_current_state(TASK_RUNNING); |
| 1021 | |
| 1022 | cleanup_entries: |
| 1023 | for (i = 0; i < count; ++i) { |
| 1024 | drm_syncobj_remove_wait(syncobjs[i], &entries[i]); |
| 1025 | if (entries[i].fence_cb.func) |
| 1026 | dma_fence_remove_callback(entries[i].fence, |
| 1027 | &entries[i].fence_cb); |
| 1028 | dma_fence_put(entries[i].fence); |
| 1029 | } |
| 1030 | kfree(entries); |
| 1031 | |
| 1032 | err_free_points: |
| 1033 | kfree(points); |
| 1034 | |
| 1035 | return timeout; |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value |
| 1040 | * |
| 1041 | * @timeout_nsec: timeout nsec component in ns, 0 for poll |
| 1042 | * |
| 1043 | * Calculate the timeout in jiffies from an absolute time in sec/nsec. |
| 1044 | */ |
| 1045 | signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec) |
| 1046 | { |
| 1047 | ktime_t abs_timeout, now; |
| 1048 | u64 timeout_ns, timeout_jiffies64; |
| 1049 | |
| 1050 | /* make 0 timeout means poll - absolute 0 doesn't seem valid */ |
| 1051 | if (timeout_nsec == 0) |
| 1052 | return 0; |
| 1053 | |
| 1054 | abs_timeout = ns_to_ktime(timeout_nsec); |
| 1055 | now = ktime_get(); |
| 1056 | |
| 1057 | if (!ktime_after(abs_timeout, now)) |
| 1058 | return 0; |
| 1059 | |
| 1060 | timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now)); |
| 1061 | |
| 1062 | timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns); |
| 1063 | /* clamp timeout to avoid infinite timeout */ |
| 1064 | if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1) |
| 1065 | return MAX_SCHEDULE_TIMEOUT - 1; |
| 1066 | |
| 1067 | return timeout_jiffies64 + 1; |
| 1068 | } |
| 1069 | EXPORT_SYMBOL(drm_timeout_abs_to_jiffies); |
| 1070 | |
| 1071 | static int drm_syncobj_array_wait(struct drm_device *dev, |
| 1072 | struct drm_file *file_private, |
| 1073 | struct drm_syncobj_wait *wait, |
| 1074 | struct drm_syncobj_timeline_wait *timeline_wait, |
| 1075 | struct drm_syncobj **syncobjs, bool timeline) |
| 1076 | { |
| 1077 | signed long timeout = 0; |
| 1078 | uint32_t first = ~0; |
| 1079 | |
| 1080 | if (!timeline) { |
| 1081 | timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec); |
| 1082 | timeout = drm_syncobj_array_wait_timeout(syncobjs, |
| 1083 | NULL, |
| 1084 | wait->count_handles, |
| 1085 | wait->flags, |
| 1086 | timeout, &first); |
| 1087 | if (timeout < 0) |
| 1088 | return timeout; |
| 1089 | wait->first_signaled = first; |
| 1090 | } else { |
| 1091 | timeout = drm_timeout_abs_to_jiffies(timeline_wait->timeout_nsec); |
| 1092 | timeout = drm_syncobj_array_wait_timeout(syncobjs, |
| 1093 | u64_to_user_ptr(timeline_wait->points), |
| 1094 | timeline_wait->count_handles, |
| 1095 | timeline_wait->flags, |
| 1096 | timeout, &first); |
| 1097 | if (timeout < 0) |
| 1098 | return timeout; |
| 1099 | timeline_wait->first_signaled = first; |
| 1100 | } |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | static int drm_syncobj_array_find(struct drm_file *file_private, |
| 1105 | void __user *user_handles, |
| 1106 | uint32_t count_handles, |
| 1107 | struct drm_syncobj ***syncobjs_out) |
| 1108 | { |
| 1109 | uint32_t i, *handles; |
| 1110 | struct drm_syncobj **syncobjs; |
| 1111 | int ret; |
| 1112 | |
| 1113 | handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL); |
| 1114 | if (handles == NULL) |
| 1115 | return -ENOMEM; |
| 1116 | |
| 1117 | if (copy_from_user(handles, user_handles, |
| 1118 | sizeof(uint32_t) * count_handles)) { |
| 1119 | ret = -EFAULT; |
| 1120 | goto err_free_handles; |
| 1121 | } |
| 1122 | |
| 1123 | syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL); |
| 1124 | if (syncobjs == NULL) { |
| 1125 | ret = -ENOMEM; |
| 1126 | goto err_free_handles; |
| 1127 | } |
| 1128 | |
| 1129 | for (i = 0; i < count_handles; i++) { |
| 1130 | syncobjs[i] = drm_syncobj_find(file_private, handles[i]); |
| 1131 | if (!syncobjs[i]) { |
| 1132 | ret = -ENOENT; |
| 1133 | goto err_put_syncobjs; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | kfree(handles); |
| 1138 | *syncobjs_out = syncobjs; |
| 1139 | return 0; |
| 1140 | |
| 1141 | err_put_syncobjs: |
| 1142 | while (i-- > 0) |
| 1143 | drm_syncobj_put(syncobjs[i]); |
| 1144 | kfree(syncobjs); |
| 1145 | err_free_handles: |
| 1146 | kfree(handles); |
| 1147 | |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | static void drm_syncobj_array_free(struct drm_syncobj **syncobjs, |
| 1152 | uint32_t count) |
| 1153 | { |
| 1154 | uint32_t i; |
| 1155 | for (i = 0; i < count; i++) |
| 1156 | drm_syncobj_put(syncobjs[i]); |
| 1157 | kfree(syncobjs); |
| 1158 | } |
| 1159 | |
| 1160 | int |
| 1161 | drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, |
| 1162 | struct drm_file *file_private) |
| 1163 | { |
| 1164 | struct drm_syncobj_wait *args = data; |
| 1165 | struct drm_syncobj **syncobjs; |
| 1166 | int ret = 0; |
| 1167 | |
| 1168 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 1169 | return -EOPNOTSUPP; |
| 1170 | |
| 1171 | if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL | |
| 1172 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | if (args->count_handles == 0) |
| 1176 | return -EINVAL; |
| 1177 | |
| 1178 | ret = drm_syncobj_array_find(file_private, |
| 1179 | u64_to_user_ptr(args->handles), |
| 1180 | args->count_handles, |
| 1181 | &syncobjs); |
| 1182 | if (ret < 0) |
| 1183 | return ret; |
| 1184 | |
| 1185 | ret = drm_syncobj_array_wait(dev, file_private, |
| 1186 | args, NULL, syncobjs, false); |
| 1187 | |
| 1188 | drm_syncobj_array_free(syncobjs, args->count_handles); |
| 1189 | |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | int |
| 1194 | drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, |
| 1195 | struct drm_file *file_private) |
| 1196 | { |
| 1197 | struct drm_syncobj_timeline_wait *args = data; |
| 1198 | struct drm_syncobj **syncobjs; |
| 1199 | int ret = 0; |
| 1200 | |
| 1201 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE)) |
| 1202 | return -EOPNOTSUPP; |
| 1203 | |
| 1204 | if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL | |
| 1205 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT | |
| 1206 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) |
| 1207 | return -EINVAL; |
| 1208 | |
| 1209 | if (args->count_handles == 0) |
| 1210 | return -EINVAL; |
| 1211 | |
| 1212 | ret = drm_syncobj_array_find(file_private, |
| 1213 | u64_to_user_ptr(args->handles), |
| 1214 | args->count_handles, |
| 1215 | &syncobjs); |
| 1216 | if (ret < 0) |
| 1217 | return ret; |
| 1218 | |
| 1219 | ret = drm_syncobj_array_wait(dev, file_private, |
| 1220 | NULL, args, syncobjs, true); |
| 1221 | |
| 1222 | drm_syncobj_array_free(syncobjs, args->count_handles); |
| 1223 | |
| 1224 | return ret; |
| 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | int |
| 1229 | drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, |
| 1230 | struct drm_file *file_private) |
| 1231 | { |
| 1232 | struct drm_syncobj_array *args = data; |
| 1233 | struct drm_syncobj **syncobjs; |
| 1234 | uint32_t i; |
| 1235 | int ret; |
| 1236 | |
| 1237 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 1238 | return -EOPNOTSUPP; |
| 1239 | |
| 1240 | if (args->pad != 0) |
| 1241 | return -EINVAL; |
| 1242 | |
| 1243 | if (args->count_handles == 0) |
| 1244 | return -EINVAL; |
| 1245 | |
| 1246 | ret = drm_syncobj_array_find(file_private, |
| 1247 | u64_to_user_ptr(args->handles), |
| 1248 | args->count_handles, |
| 1249 | &syncobjs); |
| 1250 | if (ret < 0) |
| 1251 | return ret; |
| 1252 | |
| 1253 | for (i = 0; i < args->count_handles; i++) |
| 1254 | drm_syncobj_replace_fence(syncobjs[i], NULL); |
| 1255 | |
| 1256 | drm_syncobj_array_free(syncobjs, args->count_handles); |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | int |
| 1262 | drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, |
| 1263 | struct drm_file *file_private) |
| 1264 | { |
| 1265 | struct drm_syncobj_array *args = data; |
| 1266 | struct drm_syncobj **syncobjs; |
| 1267 | uint32_t i; |
| 1268 | int ret; |
| 1269 | |
| 1270 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) |
| 1271 | return -EOPNOTSUPP; |
| 1272 | |
| 1273 | if (args->pad != 0) |
| 1274 | return -EINVAL; |
| 1275 | |
| 1276 | if (args->count_handles == 0) |
| 1277 | return -EINVAL; |
| 1278 | |
| 1279 | ret = drm_syncobj_array_find(file_private, |
| 1280 | u64_to_user_ptr(args->handles), |
| 1281 | args->count_handles, |
| 1282 | &syncobjs); |
| 1283 | if (ret < 0) |
| 1284 | return ret; |
| 1285 | |
| 1286 | for (i = 0; i < args->count_handles; i++) |
| 1287 | drm_syncobj_assign_null_handle(syncobjs[i]); |
| 1288 | |
| 1289 | drm_syncobj_array_free(syncobjs, args->count_handles); |
| 1290 | |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
| 1294 | int |
| 1295 | drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, |
| 1296 | struct drm_file *file_private) |
| 1297 | { |
| 1298 | struct drm_syncobj_timeline_array *args = data; |
| 1299 | struct drm_syncobj **syncobjs; |
| 1300 | struct dma_fence_chain **chains; |
| 1301 | uint64_t *points; |
| 1302 | uint32_t i, j; |
| 1303 | int ret; |
| 1304 | |
| 1305 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE)) |
| 1306 | return -EOPNOTSUPP; |
| 1307 | |
| 1308 | if (args->pad != 0) |
| 1309 | return -EINVAL; |
| 1310 | |
| 1311 | if (args->count_handles == 0) |
| 1312 | return -EINVAL; |
| 1313 | |
| 1314 | ret = drm_syncobj_array_find(file_private, |
| 1315 | u64_to_user_ptr(args->handles), |
| 1316 | args->count_handles, |
| 1317 | &syncobjs); |
| 1318 | if (ret < 0) |
| 1319 | return ret; |
| 1320 | |
| 1321 | points = kmalloc_array(args->count_handles, sizeof(*points), |
| 1322 | GFP_KERNEL); |
| 1323 | if (!points) { |
| 1324 | ret = -ENOMEM; |
| 1325 | goto out; |
| 1326 | } |
| 1327 | if (!u64_to_user_ptr(args->points)) { |
| 1328 | memset(points, 0, args->count_handles * sizeof(uint64_t)); |
| 1329 | } else if (copy_from_user(points, u64_to_user_ptr(args->points), |
| 1330 | sizeof(uint64_t) * args->count_handles)) { |
| 1331 | ret = -EFAULT; |
| 1332 | goto err_points; |
| 1333 | } |
| 1334 | |
| 1335 | chains = kmalloc_array(args->count_handles, sizeof(void *), GFP_KERNEL); |
| 1336 | if (!chains) { |
| 1337 | ret = -ENOMEM; |
| 1338 | goto err_points; |
| 1339 | } |
| 1340 | for (i = 0; i < args->count_handles; i++) { |
| 1341 | chains[i] = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL); |
| 1342 | if (!chains[i]) { |
| 1343 | for (j = 0; j < i; j++) |
| 1344 | kfree(chains[j]); |
| 1345 | ret = -ENOMEM; |
| 1346 | goto err_chains; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | for (i = 0; i < args->count_handles; i++) { |
| 1351 | struct dma_fence *fence = dma_fence_get_stub(); |
| 1352 | |
| 1353 | drm_syncobj_add_point(syncobjs[i], chains[i], |
| 1354 | fence, points[i]); |
| 1355 | dma_fence_put(fence); |
| 1356 | } |
| 1357 | err_chains: |
| 1358 | kfree(chains); |
| 1359 | err_points: |
| 1360 | kfree(points); |
| 1361 | out: |
| 1362 | drm_syncobj_array_free(syncobjs, args->count_handles); |
| 1363 | |
| 1364 | return ret; |
| 1365 | } |
| 1366 | |
| 1367 | int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, |
| 1368 | struct drm_file *file_private) |
| 1369 | { |
| 1370 | struct drm_syncobj_timeline_array *args = data; |
| 1371 | struct drm_syncobj **syncobjs; |
| 1372 | uint64_t __user *points = u64_to_user_ptr(args->points); |
| 1373 | uint32_t i; |
| 1374 | int ret; |
| 1375 | |
| 1376 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE)) |
| 1377 | return -EOPNOTSUPP; |
| 1378 | |
| 1379 | if (args->pad != 0) |
| 1380 | return -EINVAL; |
| 1381 | |
| 1382 | if (args->count_handles == 0) |
| 1383 | return -EINVAL; |
| 1384 | |
| 1385 | ret = drm_syncobj_array_find(file_private, |
| 1386 | u64_to_user_ptr(args->handles), |
| 1387 | args->count_handles, |
| 1388 | &syncobjs); |
| 1389 | if (ret < 0) |
| 1390 | return ret; |
| 1391 | |
| 1392 | for (i = 0; i < args->count_handles; i++) { |
| 1393 | struct dma_fence_chain *chain; |
| 1394 | struct dma_fence *fence; |
| 1395 | uint64_t point; |
| 1396 | |
| 1397 | fence = drm_syncobj_fence_get(syncobjs[i]); |
| 1398 | chain = to_dma_fence_chain(fence); |
| 1399 | if (chain) { |
| 1400 | struct dma_fence *iter, *last_signaled = NULL; |
| 1401 | |
| 1402 | dma_fence_chain_for_each(iter, fence) { |
| 1403 | if (iter->context != fence->context) { |
| 1404 | dma_fence_put(iter); |
| 1405 | /* It is most likely that timeline has |
| 1406 | * unorder points. */ |
| 1407 | break; |
| 1408 | } |
| 1409 | dma_fence_put(last_signaled); |
| 1410 | last_signaled = dma_fence_get(iter); |
| 1411 | } |
| 1412 | point = dma_fence_is_signaled(last_signaled) ? |
| 1413 | last_signaled->seqno : |
| 1414 | to_dma_fence_chain(last_signaled)->prev_seqno; |
| 1415 | dma_fence_put(last_signaled); |
| 1416 | } else { |
| 1417 | point = 0; |
| 1418 | } |
| 1419 | ret = copy_to_user(&points[i], &point, sizeof(uint64_t)); |
| 1420 | ret = ret ? -EFAULT : 0; |
| 1421 | if (ret) |
| 1422 | break; |
| 1423 | } |
| 1424 | drm_syncobj_array_free(syncobjs, args->count_handles); |
| 1425 | |
| 1426 | return ret; |
| 1427 | } |