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