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