1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright 2011-2014 VMware, Inc., Palo Alto, CA., USA 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 #include <linux/sched/signal.h> 29 30 #include "vmwgfx_drv.h" 31 32 #define VMW_FENCE_WRAP (1 << 31) 33 34 struct vmw_fence_manager { 35 int num_fence_objects; 36 struct vmw_private *dev_priv; 37 spinlock_t lock; 38 struct list_head fence_list; 39 struct work_struct work; 40 bool fifo_down; 41 struct list_head cleanup_list; 42 uint32_t pending_actions[VMW_ACTION_MAX]; 43 struct mutex goal_irq_mutex; 44 bool goal_irq_on; /* Protected by @goal_irq_mutex */ 45 bool seqno_valid; /* Protected by @lock, and may not be set to true 46 without the @goal_irq_mutex held. */ 47 u64 ctx; 48 }; 49 50 struct vmw_user_fence { 51 struct ttm_base_object base; 52 struct vmw_fence_obj fence; 53 }; 54 55 /** 56 * struct vmw_event_fence_action - fence action that delivers a drm event. 57 * 58 * @action: A struct vmw_fence_action to hook up to a fence. 59 * @event: A pointer to the pending event. 60 * @fence: A referenced pointer to the fence to keep it alive while @action 61 * hangs on it. 62 * @dev: Pointer to a struct drm_device so we can access the event stuff. 63 * @tv_sec: If non-null, the variable pointed to will be assigned 64 * current time tv_sec val when the fence signals. 65 * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will 66 * be assigned the current time tv_usec val when the fence signals. 67 */ 68 struct vmw_event_fence_action { 69 struct vmw_fence_action action; 70 71 struct drm_pending_event *event; 72 struct vmw_fence_obj *fence; 73 struct drm_device *dev; 74 75 uint32_t *tv_sec; 76 uint32_t *tv_usec; 77 }; 78 79 static struct vmw_fence_manager * 80 fman_from_fence(struct vmw_fence_obj *fence) 81 { 82 return container_of(fence->base.lock, struct vmw_fence_manager, lock); 83 } 84 85 /* 86 * Note on fencing subsystem usage of irqs: 87 * Typically the vmw_fences_update function is called 88 * 89 * a) When a new fence seqno has been submitted by the fifo code. 90 * b) On-demand when we have waiters. Sleeping waiters will switch on the 91 * ANY_FENCE irq and call vmw_fences_update function each time an ANY_FENCE 92 * irq is received. When the last fence waiter is gone, that IRQ is masked 93 * away. 94 * 95 * In situations where there are no waiters and we don't submit any new fences, 96 * fence objects may not be signaled. This is perfectly OK, since there are 97 * no consumers of the signaled data, but that is NOT ok when there are fence 98 * actions attached to a fence. The fencing subsystem then makes use of the 99 * FENCE_GOAL irq and sets the fence goal seqno to that of the next fence 100 * which has an action attached, and each time vmw_fences_update is called, 101 * the subsystem makes sure the fence goal seqno is updated. 102 * 103 * The fence goal seqno irq is on as long as there are unsignaled fence 104 * objects with actions attached to them. 105 */ 106 107 static void vmw_fence_obj_destroy(struct dma_fence *f) 108 { 109 struct vmw_fence_obj *fence = 110 container_of(f, struct vmw_fence_obj, base); 111 112 struct vmw_fence_manager *fman = fman_from_fence(fence); 113 114 spin_lock(&fman->lock); 115 list_del_init(&fence->head); 116 --fman->num_fence_objects; 117 spin_unlock(&fman->lock); 118 fence->destroy(fence); 119 } 120 121 static const char *vmw_fence_get_driver_name(struct dma_fence *f) 122 { 123 return "vmwgfx"; 124 } 125 126 static const char *vmw_fence_get_timeline_name(struct dma_fence *f) 127 { 128 return "svga"; 129 } 130 131 static bool vmw_fence_enable_signaling(struct dma_fence *f) 132 { 133 struct vmw_fence_obj *fence = 134 container_of(f, struct vmw_fence_obj, base); 135 136 struct vmw_fence_manager *fman = fman_from_fence(fence); 137 struct vmw_private *dev_priv = fman->dev_priv; 138 139 u32 seqno = vmw_fence_read(dev_priv); 140 if (seqno - fence->base.seqno < VMW_FENCE_WRAP) 141 return false; 142 143 return true; 144 } 145 146 struct vmwgfx_wait_cb { 147 struct dma_fence_cb base; 148 struct task_struct *task; 149 }; 150 151 static void 152 vmwgfx_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb) 153 { 154 struct vmwgfx_wait_cb *wait = 155 container_of(cb, struct vmwgfx_wait_cb, base); 156 157 wake_up_process(wait->task); 158 } 159 160 static void __vmw_fences_update(struct vmw_fence_manager *fman); 161 162 static long vmw_fence_wait(struct dma_fence *f, bool intr, signed long timeout) 163 { 164 struct vmw_fence_obj *fence = 165 container_of(f, struct vmw_fence_obj, base); 166 167 struct vmw_fence_manager *fman = fman_from_fence(fence); 168 struct vmw_private *dev_priv = fman->dev_priv; 169 struct vmwgfx_wait_cb cb; 170 long ret = timeout; 171 172 if (likely(vmw_fence_obj_signaled(fence))) 173 return timeout; 174 175 vmw_seqno_waiter_add(dev_priv); 176 177 spin_lock(f->lock); 178 179 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags)) 180 goto out; 181 182 if (intr && signal_pending(current)) { 183 ret = -ERESTARTSYS; 184 goto out; 185 } 186 187 cb.base.func = vmwgfx_wait_cb; 188 cb.task = current; 189 list_add(&cb.base.node, &f->cb_list); 190 191 for (;;) { 192 __vmw_fences_update(fman); 193 194 /* 195 * We can use the barrier free __set_current_state() since 196 * DMA_FENCE_FLAG_SIGNALED_BIT + wakeup is protected by the 197 * fence spinlock. 198 */ 199 if (intr) 200 __set_current_state(TASK_INTERRUPTIBLE); 201 else 202 __set_current_state(TASK_UNINTERRUPTIBLE); 203 204 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &f->flags)) { 205 if (ret == 0 && timeout > 0) 206 ret = 1; 207 break; 208 } 209 210 if (intr && signal_pending(current)) { 211 ret = -ERESTARTSYS; 212 break; 213 } 214 215 if (ret == 0) 216 break; 217 218 spin_unlock(f->lock); 219 220 ret = schedule_timeout(ret); 221 222 spin_lock(f->lock); 223 } 224 __set_current_state(TASK_RUNNING); 225 if (!list_empty(&cb.base.node)) 226 list_del(&cb.base.node); 227 228 out: 229 spin_unlock(f->lock); 230 231 vmw_seqno_waiter_remove(dev_priv); 232 233 return ret; 234 } 235 236 static const struct dma_fence_ops vmw_fence_ops = { 237 .get_driver_name = vmw_fence_get_driver_name, 238 .get_timeline_name = vmw_fence_get_timeline_name, 239 .enable_signaling = vmw_fence_enable_signaling, 240 .wait = vmw_fence_wait, 241 .release = vmw_fence_obj_destroy, 242 }; 243 244 245 /* 246 * Execute signal actions on fences recently signaled. 247 * This is done from a workqueue so we don't have to execute 248 * signal actions from atomic context. 249 */ 250 251 static void vmw_fence_work_func(struct work_struct *work) 252 { 253 struct vmw_fence_manager *fman = 254 container_of(work, struct vmw_fence_manager, work); 255 struct list_head list; 256 struct vmw_fence_action *action, *next_action; 257 bool seqno_valid; 258 259 do { 260 INIT_LIST_HEAD(&list); 261 mutex_lock(&fman->goal_irq_mutex); 262 263 spin_lock(&fman->lock); 264 list_splice_init(&fman->cleanup_list, &list); 265 seqno_valid = fman->seqno_valid; 266 spin_unlock(&fman->lock); 267 268 if (!seqno_valid && fman->goal_irq_on) { 269 fman->goal_irq_on = false; 270 vmw_goal_waiter_remove(fman->dev_priv); 271 } 272 mutex_unlock(&fman->goal_irq_mutex); 273 274 if (list_empty(&list)) 275 return; 276 277 /* 278 * At this point, only we should be able to manipulate the 279 * list heads of the actions we have on the private list. 280 * hence fman::lock not held. 281 */ 282 283 list_for_each_entry_safe(action, next_action, &list, head) { 284 list_del_init(&action->head); 285 if (action->cleanup) 286 action->cleanup(action); 287 } 288 } while (1); 289 } 290 291 struct vmw_fence_manager *vmw_fence_manager_init(struct vmw_private *dev_priv) 292 { 293 struct vmw_fence_manager *fman = kzalloc(sizeof(*fman), GFP_KERNEL); 294 295 if (unlikely(!fman)) 296 return NULL; 297 298 fman->dev_priv = dev_priv; 299 spin_lock_init(&fman->lock); 300 INIT_LIST_HEAD(&fman->fence_list); 301 INIT_LIST_HEAD(&fman->cleanup_list); 302 INIT_WORK(&fman->work, &vmw_fence_work_func); 303 fman->fifo_down = true; 304 mutex_init(&fman->goal_irq_mutex); 305 fman->ctx = dma_fence_context_alloc(1); 306 307 return fman; 308 } 309 310 void vmw_fence_manager_takedown(struct vmw_fence_manager *fman) 311 { 312 bool lists_empty; 313 314 (void) cancel_work_sync(&fman->work); 315 316 spin_lock(&fman->lock); 317 lists_empty = list_empty(&fman->fence_list) && 318 list_empty(&fman->cleanup_list); 319 spin_unlock(&fman->lock); 320 321 BUG_ON(!lists_empty); 322 kfree(fman); 323 } 324 325 static int vmw_fence_obj_init(struct vmw_fence_manager *fman, 326 struct vmw_fence_obj *fence, u32 seqno, 327 void (*destroy) (struct vmw_fence_obj *fence)) 328 { 329 int ret = 0; 330 331 dma_fence_init(&fence->base, &vmw_fence_ops, &fman->lock, 332 fman->ctx, seqno); 333 INIT_LIST_HEAD(&fence->seq_passed_actions); 334 fence->destroy = destroy; 335 336 spin_lock(&fman->lock); 337 if (unlikely(fman->fifo_down)) { 338 ret = -EBUSY; 339 goto out_unlock; 340 } 341 list_add_tail(&fence->head, &fman->fence_list); 342 ++fman->num_fence_objects; 343 344 out_unlock: 345 spin_unlock(&fman->lock); 346 return ret; 347 348 } 349 350 static void vmw_fences_perform_actions(struct vmw_fence_manager *fman, 351 struct list_head *list) 352 { 353 struct vmw_fence_action *action, *next_action; 354 355 list_for_each_entry_safe(action, next_action, list, head) { 356 list_del_init(&action->head); 357 fman->pending_actions[action->type]--; 358 if (action->seq_passed != NULL) 359 action->seq_passed(action); 360 361 /* 362 * Add the cleanup action to the cleanup list so that 363 * it will be performed by a worker task. 364 */ 365 366 list_add_tail(&action->head, &fman->cleanup_list); 367 } 368 } 369 370 /** 371 * vmw_fence_goal_new_locked - Figure out a new device fence goal 372 * seqno if needed. 373 * 374 * @fman: Pointer to a fence manager. 375 * @passed_seqno: The seqno the device currently signals as passed. 376 * 377 * This function should be called with the fence manager lock held. 378 * It is typically called when we have a new passed_seqno, and 379 * we might need to update the fence goal. It checks to see whether 380 * the current fence goal has already passed, and, in that case, 381 * scans through all unsignaled fences to get the next fence object with an 382 * action attached, and sets the seqno of that fence as a new fence goal. 383 * 384 * returns true if the device goal seqno was updated. False otherwise. 385 */ 386 static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman, 387 u32 passed_seqno) 388 { 389 u32 goal_seqno; 390 struct vmw_fence_obj *fence; 391 392 if (likely(!fman->seqno_valid)) 393 return false; 394 395 goal_seqno = vmw_fifo_mem_read(fman->dev_priv, SVGA_FIFO_FENCE_GOAL); 396 if (likely(passed_seqno - goal_seqno >= VMW_FENCE_WRAP)) 397 return false; 398 399 fman->seqno_valid = false; 400 list_for_each_entry(fence, &fman->fence_list, head) { 401 if (!list_empty(&fence->seq_passed_actions)) { 402 fman->seqno_valid = true; 403 vmw_fifo_mem_write(fman->dev_priv, 404 SVGA_FIFO_FENCE_GOAL, 405 fence->base.seqno); 406 break; 407 } 408 } 409 410 return true; 411 } 412 413 414 /** 415 * vmw_fence_goal_check_locked - Replace the device fence goal seqno if 416 * needed. 417 * 418 * @fence: Pointer to a struct vmw_fence_obj the seqno of which should be 419 * considered as a device fence goal. 420 * 421 * This function should be called with the fence manager lock held. 422 * It is typically called when an action has been attached to a fence to 423 * check whether the seqno of that fence should be used for a fence 424 * goal interrupt. This is typically needed if the current fence goal is 425 * invalid, or has a higher seqno than that of the current fence object. 426 * 427 * returns true if the device goal seqno was updated. False otherwise. 428 */ 429 static bool vmw_fence_goal_check_locked(struct vmw_fence_obj *fence) 430 { 431 struct vmw_fence_manager *fman = fman_from_fence(fence); 432 u32 goal_seqno; 433 434 if (dma_fence_is_signaled_locked(&fence->base)) 435 return false; 436 437 goal_seqno = vmw_fifo_mem_read(fman->dev_priv, SVGA_FIFO_FENCE_GOAL); 438 if (likely(fman->seqno_valid && 439 goal_seqno - fence->base.seqno < VMW_FENCE_WRAP)) 440 return false; 441 442 vmw_fifo_mem_write(fman->dev_priv, SVGA_FIFO_FENCE_GOAL, 443 fence->base.seqno); 444 fman->seqno_valid = true; 445 446 return true; 447 } 448 449 static void __vmw_fences_update(struct vmw_fence_manager *fman) 450 { 451 struct vmw_fence_obj *fence, *next_fence; 452 struct list_head action_list; 453 bool needs_rerun; 454 uint32_t seqno, new_seqno; 455 456 seqno = vmw_fence_read(fman->dev_priv); 457 rerun: 458 list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) { 459 if (seqno - fence->base.seqno < VMW_FENCE_WRAP) { 460 list_del_init(&fence->head); 461 dma_fence_signal_locked(&fence->base); 462 INIT_LIST_HEAD(&action_list); 463 list_splice_init(&fence->seq_passed_actions, 464 &action_list); 465 vmw_fences_perform_actions(fman, &action_list); 466 } else 467 break; 468 } 469 470 /* 471 * Rerun if the fence goal seqno was updated, and the 472 * hardware might have raced with that update, so that 473 * we missed a fence_goal irq. 474 */ 475 476 needs_rerun = vmw_fence_goal_new_locked(fman, seqno); 477 if (unlikely(needs_rerun)) { 478 new_seqno = vmw_fence_read(fman->dev_priv); 479 if (new_seqno != seqno) { 480 seqno = new_seqno; 481 goto rerun; 482 } 483 } 484 485 if (!list_empty(&fman->cleanup_list)) 486 (void) schedule_work(&fman->work); 487 } 488 489 void vmw_fences_update(struct vmw_fence_manager *fman) 490 { 491 spin_lock(&fman->lock); 492 __vmw_fences_update(fman); 493 spin_unlock(&fman->lock); 494 } 495 496 bool vmw_fence_obj_signaled(struct vmw_fence_obj *fence) 497 { 498 struct vmw_fence_manager *fman = fman_from_fence(fence); 499 500 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags)) 501 return true; 502 503 vmw_fences_update(fman); 504 505 return dma_fence_is_signaled(&fence->base); 506 } 507 508 int vmw_fence_obj_wait(struct vmw_fence_obj *fence, bool lazy, 509 bool interruptible, unsigned long timeout) 510 { 511 long ret = dma_fence_wait_timeout(&fence->base, interruptible, timeout); 512 513 if (likely(ret > 0)) 514 return 0; 515 else if (ret == 0) 516 return -EBUSY; 517 else 518 return ret; 519 } 520 521 static void vmw_fence_destroy(struct vmw_fence_obj *fence) 522 { 523 dma_fence_free(&fence->base); 524 } 525 526 int vmw_fence_create(struct vmw_fence_manager *fman, 527 uint32_t seqno, 528 struct vmw_fence_obj **p_fence) 529 { 530 struct vmw_fence_obj *fence; 531 int ret; 532 533 fence = kzalloc(sizeof(*fence), GFP_KERNEL); 534 if (unlikely(!fence)) 535 return -ENOMEM; 536 537 ret = vmw_fence_obj_init(fman, fence, seqno, 538 vmw_fence_destroy); 539 if (unlikely(ret != 0)) 540 goto out_err_init; 541 542 *p_fence = fence; 543 return 0; 544 545 out_err_init: 546 kfree(fence); 547 return ret; 548 } 549 550 551 static void vmw_user_fence_destroy(struct vmw_fence_obj *fence) 552 { 553 struct vmw_user_fence *ufence = 554 container_of(fence, struct vmw_user_fence, fence); 555 556 ttm_base_object_kfree(ufence, base); 557 } 558 559 static void vmw_user_fence_base_release(struct ttm_base_object **p_base) 560 { 561 struct ttm_base_object *base = *p_base; 562 struct vmw_user_fence *ufence = 563 container_of(base, struct vmw_user_fence, base); 564 struct vmw_fence_obj *fence = &ufence->fence; 565 566 *p_base = NULL; 567 vmw_fence_obj_unreference(&fence); 568 } 569 570 int vmw_user_fence_create(struct drm_file *file_priv, 571 struct vmw_fence_manager *fman, 572 uint32_t seqno, 573 struct vmw_fence_obj **p_fence, 574 uint32_t *p_handle) 575 { 576 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 577 struct vmw_user_fence *ufence; 578 struct vmw_fence_obj *tmp; 579 int ret; 580 581 ufence = kzalloc(sizeof(*ufence), GFP_KERNEL); 582 if (unlikely(!ufence)) { 583 ret = -ENOMEM; 584 goto out_no_object; 585 } 586 587 ret = vmw_fence_obj_init(fman, &ufence->fence, seqno, 588 vmw_user_fence_destroy); 589 if (unlikely(ret != 0)) { 590 kfree(ufence); 591 goto out_no_object; 592 } 593 594 /* 595 * The base object holds a reference which is freed in 596 * vmw_user_fence_base_release. 597 */ 598 tmp = vmw_fence_obj_reference(&ufence->fence); 599 600 ret = ttm_base_object_init(tfile, &ufence->base, false, 601 VMW_RES_FENCE, 602 &vmw_user_fence_base_release); 603 604 605 if (unlikely(ret != 0)) { 606 /* 607 * Free the base object's reference 608 */ 609 vmw_fence_obj_unreference(&tmp); 610 goto out_err; 611 } 612 613 *p_fence = &ufence->fence; 614 *p_handle = ufence->base.handle; 615 616 return 0; 617 out_err: 618 tmp = &ufence->fence; 619 vmw_fence_obj_unreference(&tmp); 620 out_no_object: 621 return ret; 622 } 623 624 /* 625 * vmw_fence_fifo_down - signal all unsignaled fence objects. 626 */ 627 628 void vmw_fence_fifo_down(struct vmw_fence_manager *fman) 629 { 630 struct list_head action_list; 631 int ret; 632 633 /* 634 * The list may be altered while we traverse it, so always 635 * restart when we've released the fman->lock. 636 */ 637 638 spin_lock(&fman->lock); 639 fman->fifo_down = true; 640 while (!list_empty(&fman->fence_list)) { 641 struct vmw_fence_obj *fence = 642 list_entry(fman->fence_list.prev, struct vmw_fence_obj, 643 head); 644 dma_fence_get(&fence->base); 645 spin_unlock(&fman->lock); 646 647 ret = vmw_fence_obj_wait(fence, false, false, 648 VMW_FENCE_WAIT_TIMEOUT); 649 650 if (unlikely(ret != 0)) { 651 list_del_init(&fence->head); 652 dma_fence_signal(&fence->base); 653 INIT_LIST_HEAD(&action_list); 654 list_splice_init(&fence->seq_passed_actions, 655 &action_list); 656 vmw_fences_perform_actions(fman, &action_list); 657 } 658 659 BUG_ON(!list_empty(&fence->head)); 660 dma_fence_put(&fence->base); 661 spin_lock(&fman->lock); 662 } 663 spin_unlock(&fman->lock); 664 } 665 666 void vmw_fence_fifo_up(struct vmw_fence_manager *fman) 667 { 668 spin_lock(&fman->lock); 669 fman->fifo_down = false; 670 spin_unlock(&fman->lock); 671 } 672 673 674 /** 675 * vmw_fence_obj_lookup - Look up a user-space fence object 676 * 677 * @tfile: A struct ttm_object_file identifying the caller. 678 * @handle: A handle identifying the fence object. 679 * @return: A struct vmw_user_fence base ttm object on success or 680 * an error pointer on failure. 681 * 682 * The fence object is looked up and type-checked. The caller needs 683 * to have opened the fence object first, but since that happens on 684 * creation and fence objects aren't shareable, that's not an 685 * issue currently. 686 */ 687 static struct ttm_base_object * 688 vmw_fence_obj_lookup(struct ttm_object_file *tfile, u32 handle) 689 { 690 struct ttm_base_object *base = ttm_base_object_lookup(tfile, handle); 691 692 if (!base) { 693 pr_err("Invalid fence object handle 0x%08lx.\n", 694 (unsigned long)handle); 695 return ERR_PTR(-EINVAL); 696 } 697 698 if (base->refcount_release != vmw_user_fence_base_release) { 699 pr_err("Invalid fence object handle 0x%08lx.\n", 700 (unsigned long)handle); 701 ttm_base_object_unref(&base); 702 return ERR_PTR(-EINVAL); 703 } 704 705 return base; 706 } 707 708 709 int vmw_fence_obj_wait_ioctl(struct drm_device *dev, void *data, 710 struct drm_file *file_priv) 711 { 712 struct drm_vmw_fence_wait_arg *arg = 713 (struct drm_vmw_fence_wait_arg *)data; 714 unsigned long timeout; 715 struct ttm_base_object *base; 716 struct vmw_fence_obj *fence; 717 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 718 int ret; 719 uint64_t wait_timeout = ((uint64_t)arg->timeout_us * HZ); 720 721 /* 722 * 64-bit division not present on 32-bit systems, so do an 723 * approximation. (Divide by 1000000). 724 */ 725 726 wait_timeout = (wait_timeout >> 20) + (wait_timeout >> 24) - 727 (wait_timeout >> 26); 728 729 if (!arg->cookie_valid) { 730 arg->cookie_valid = 1; 731 arg->kernel_cookie = jiffies + wait_timeout; 732 } 733 734 base = vmw_fence_obj_lookup(tfile, arg->handle); 735 if (IS_ERR(base)) 736 return PTR_ERR(base); 737 738 fence = &(container_of(base, struct vmw_user_fence, base)->fence); 739 740 timeout = jiffies; 741 if (time_after_eq(timeout, (unsigned long)arg->kernel_cookie)) { 742 ret = ((vmw_fence_obj_signaled(fence)) ? 743 0 : -EBUSY); 744 goto out; 745 } 746 747 timeout = (unsigned long)arg->kernel_cookie - timeout; 748 749 ret = vmw_fence_obj_wait(fence, arg->lazy, true, timeout); 750 751 out: 752 ttm_base_object_unref(&base); 753 754 /* 755 * Optionally unref the fence object. 756 */ 757 758 if (ret == 0 && (arg->wait_options & DRM_VMW_WAIT_OPTION_UNREF)) 759 return ttm_ref_object_base_unref(tfile, arg->handle); 760 return ret; 761 } 762 763 int vmw_fence_obj_signaled_ioctl(struct drm_device *dev, void *data, 764 struct drm_file *file_priv) 765 { 766 struct drm_vmw_fence_signaled_arg *arg = 767 (struct drm_vmw_fence_signaled_arg *) data; 768 struct ttm_base_object *base; 769 struct vmw_fence_obj *fence; 770 struct vmw_fence_manager *fman; 771 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; 772 struct vmw_private *dev_priv = vmw_priv(dev); 773 774 base = vmw_fence_obj_lookup(tfile, arg->handle); 775 if (IS_ERR(base)) 776 return PTR_ERR(base); 777 778 fence = &(container_of(base, struct vmw_user_fence, base)->fence); 779 fman = fman_from_fence(fence); 780 781 arg->signaled = vmw_fence_obj_signaled(fence); 782 783 arg->signaled_flags = arg->flags; 784 spin_lock(&fman->lock); 785 arg->passed_seqno = dev_priv->last_read_seqno; 786 spin_unlock(&fman->lock); 787 788 ttm_base_object_unref(&base); 789 790 return 0; 791 } 792 793 794 int vmw_fence_obj_unref_ioctl(struct drm_device *dev, void *data, 795 struct drm_file *file_priv) 796 { 797 struct drm_vmw_fence_arg *arg = 798 (struct drm_vmw_fence_arg *) data; 799 800 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile, 801 arg->handle); 802 } 803 804 /** 805 * vmw_event_fence_action_seq_passed 806 * 807 * @action: The struct vmw_fence_action embedded in a struct 808 * vmw_event_fence_action. 809 * 810 * This function is called when the seqno of the fence where @action is 811 * attached has passed. It queues the event on the submitter's event list. 812 * This function is always called from atomic context. 813 */ 814 static void vmw_event_fence_action_seq_passed(struct vmw_fence_action *action) 815 { 816 struct vmw_event_fence_action *eaction = 817 container_of(action, struct vmw_event_fence_action, action); 818 struct drm_device *dev = eaction->dev; 819 struct drm_pending_event *event = eaction->event; 820 821 if (unlikely(event == NULL)) 822 return; 823 824 spin_lock_irq(&dev->event_lock); 825 826 if (likely(eaction->tv_sec != NULL)) { 827 struct timespec64 ts; 828 829 ktime_get_ts64(&ts); 830 /* monotonic time, so no y2038 overflow */ 831 *eaction->tv_sec = ts.tv_sec; 832 *eaction->tv_usec = ts.tv_nsec / NSEC_PER_USEC; 833 } 834 835 drm_send_event_locked(dev, eaction->event); 836 eaction->event = NULL; 837 spin_unlock_irq(&dev->event_lock); 838 } 839 840 /** 841 * vmw_event_fence_action_cleanup 842 * 843 * @action: The struct vmw_fence_action embedded in a struct 844 * vmw_event_fence_action. 845 * 846 * This function is the struct vmw_fence_action destructor. It's typically 847 * called from a workqueue. 848 */ 849 static void vmw_event_fence_action_cleanup(struct vmw_fence_action *action) 850 { 851 struct vmw_event_fence_action *eaction = 852 container_of(action, struct vmw_event_fence_action, action); 853 854 vmw_fence_obj_unreference(&eaction->fence); 855 kfree(eaction); 856 } 857 858 859 /** 860 * vmw_fence_obj_add_action - Add an action to a fence object. 861 * 862 * @fence: The fence object. 863 * @action: The action to add. 864 * 865 * Note that the action callbacks may be executed before this function 866 * returns. 867 */ 868 static void vmw_fence_obj_add_action(struct vmw_fence_obj *fence, 869 struct vmw_fence_action *action) 870 { 871 struct vmw_fence_manager *fman = fman_from_fence(fence); 872 bool run_update = false; 873 874 mutex_lock(&fman->goal_irq_mutex); 875 spin_lock(&fman->lock); 876 877 fman->pending_actions[action->type]++; 878 if (dma_fence_is_signaled_locked(&fence->base)) { 879 struct list_head action_list; 880 881 INIT_LIST_HEAD(&action_list); 882 list_add_tail(&action->head, &action_list); 883 vmw_fences_perform_actions(fman, &action_list); 884 } else { 885 list_add_tail(&action->head, &fence->seq_passed_actions); 886 887 /* 888 * This function may set fman::seqno_valid, so it must 889 * be run with the goal_irq_mutex held. 890 */ 891 run_update = vmw_fence_goal_check_locked(fence); 892 } 893 894 spin_unlock(&fman->lock); 895 896 if (run_update) { 897 if (!fman->goal_irq_on) { 898 fman->goal_irq_on = true; 899 vmw_goal_waiter_add(fman->dev_priv); 900 } 901 vmw_fences_update(fman); 902 } 903 mutex_unlock(&fman->goal_irq_mutex); 904 905 } 906 907 /** 908 * vmw_event_fence_action_queue - Post an event for sending when a fence 909 * object seqno has passed. 910 * 911 * @file_priv: The file connection on which the event should be posted. 912 * @fence: The fence object on which to post the event. 913 * @event: Event to be posted. This event should've been alloced 914 * using k[mz]alloc, and should've been completely initialized. 915 * @tv_sec: If non-null, the variable pointed to will be assigned 916 * current time tv_sec val when the fence signals. 917 * @tv_usec: Must be set if @tv_sec is set, and the variable pointed to will 918 * be assigned the current time tv_usec val when the fence signals. 919 * @interruptible: Interruptible waits if possible. 920 * 921 * As a side effect, the object pointed to by @event may have been 922 * freed when this function returns. If this function returns with 923 * an error code, the caller needs to free that object. 924 */ 925 926 int vmw_event_fence_action_queue(struct drm_file *file_priv, 927 struct vmw_fence_obj *fence, 928 struct drm_pending_event *event, 929 uint32_t *tv_sec, 930 uint32_t *tv_usec, 931 bool interruptible) 932 { 933 struct vmw_event_fence_action *eaction; 934 struct vmw_fence_manager *fman = fman_from_fence(fence); 935 936 eaction = kzalloc(sizeof(*eaction), GFP_KERNEL); 937 if (unlikely(!eaction)) 938 return -ENOMEM; 939 940 eaction->event = event; 941 942 eaction->action.seq_passed = vmw_event_fence_action_seq_passed; 943 eaction->action.cleanup = vmw_event_fence_action_cleanup; 944 eaction->action.type = VMW_ACTION_EVENT; 945 946 eaction->fence = vmw_fence_obj_reference(fence); 947 eaction->dev = &fman->dev_priv->drm; 948 eaction->tv_sec = tv_sec; 949 eaction->tv_usec = tv_usec; 950 951 vmw_fence_obj_add_action(fence, &eaction->action); 952 953 return 0; 954 } 955 956 struct vmw_event_fence_pending { 957 struct drm_pending_event base; 958 struct drm_vmw_event_fence event; 959 }; 960 961 static int vmw_event_fence_action_create(struct drm_file *file_priv, 962 struct vmw_fence_obj *fence, 963 uint32_t flags, 964 uint64_t user_data, 965 bool interruptible) 966 { 967 struct vmw_event_fence_pending *event; 968 struct vmw_fence_manager *fman = fman_from_fence(fence); 969 struct drm_device *dev = &fman->dev_priv->drm; 970 int ret; 971 972 event = kzalloc(sizeof(*event), GFP_KERNEL); 973 if (unlikely(!event)) { 974 DRM_ERROR("Failed to allocate an event.\n"); 975 ret = -ENOMEM; 976 goto out_no_space; 977 } 978 979 event->event.base.type = DRM_VMW_EVENT_FENCE_SIGNALED; 980 event->event.base.length = sizeof(*event); 981 event->event.user_data = user_data; 982 983 ret = drm_event_reserve_init(dev, file_priv, &event->base, &event->event.base); 984 985 if (unlikely(ret != 0)) { 986 DRM_ERROR("Failed to allocate event space for this file.\n"); 987 kfree(event); 988 goto out_no_space; 989 } 990 991 if (flags & DRM_VMW_FE_FLAG_REQ_TIME) 992 ret = vmw_event_fence_action_queue(file_priv, fence, 993 &event->base, 994 &event->event.tv_sec, 995 &event->event.tv_usec, 996 interruptible); 997 else 998 ret = vmw_event_fence_action_queue(file_priv, fence, 999 &event->base, 1000 NULL, 1001 NULL, 1002 interruptible); 1003 if (ret != 0) 1004 goto out_no_queue; 1005 1006 return 0; 1007 1008 out_no_queue: 1009 drm_event_cancel_free(dev, &event->base); 1010 out_no_space: 1011 return ret; 1012 } 1013 1014 int vmw_fence_event_ioctl(struct drm_device *dev, void *data, 1015 struct drm_file *file_priv) 1016 { 1017 struct vmw_private *dev_priv = vmw_priv(dev); 1018 struct drm_vmw_fence_event_arg *arg = 1019 (struct drm_vmw_fence_event_arg *) data; 1020 struct vmw_fence_obj *fence = NULL; 1021 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv); 1022 struct ttm_object_file *tfile = vmw_fp->tfile; 1023 struct drm_vmw_fence_rep __user *user_fence_rep = 1024 (struct drm_vmw_fence_rep __user *)(unsigned long) 1025 arg->fence_rep; 1026 uint32_t handle; 1027 int ret; 1028 1029 /* 1030 * Look up an existing fence object, 1031 * and if user-space wants a new reference, 1032 * add one. 1033 */ 1034 if (arg->handle) { 1035 struct ttm_base_object *base = 1036 vmw_fence_obj_lookup(tfile, arg->handle); 1037 1038 if (IS_ERR(base)) 1039 return PTR_ERR(base); 1040 1041 fence = &(container_of(base, struct vmw_user_fence, 1042 base)->fence); 1043 (void) vmw_fence_obj_reference(fence); 1044 1045 if (user_fence_rep != NULL) { 1046 ret = ttm_ref_object_add(vmw_fp->tfile, base, 1047 NULL, false); 1048 if (unlikely(ret != 0)) { 1049 DRM_ERROR("Failed to reference a fence " 1050 "object.\n"); 1051 goto out_no_ref_obj; 1052 } 1053 handle = base->handle; 1054 } 1055 ttm_base_object_unref(&base); 1056 } 1057 1058 /* 1059 * Create a new fence object. 1060 */ 1061 if (!fence) { 1062 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, 1063 &fence, 1064 (user_fence_rep) ? 1065 &handle : NULL); 1066 if (unlikely(ret != 0)) { 1067 DRM_ERROR("Fence event failed to create fence.\n"); 1068 return ret; 1069 } 1070 } 1071 1072 BUG_ON(fence == NULL); 1073 1074 ret = vmw_event_fence_action_create(file_priv, fence, 1075 arg->flags, 1076 arg->user_data, 1077 true); 1078 if (unlikely(ret != 0)) { 1079 if (ret != -ERESTARTSYS) 1080 DRM_ERROR("Failed to attach event to fence.\n"); 1081 goto out_no_create; 1082 } 1083 1084 vmw_execbuf_copy_fence_user(dev_priv, vmw_fp, 0, user_fence_rep, fence, 1085 handle, -1); 1086 vmw_fence_obj_unreference(&fence); 1087 return 0; 1088 out_no_create: 1089 if (user_fence_rep != NULL) 1090 ttm_ref_object_base_unref(tfile, handle); 1091 out_no_ref_obj: 1092 vmw_fence_obj_unreference(&fence); 1093 return ret; 1094 } 1095