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