1 /* 2 * Copyright © 2008-2015 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 */ 24 25 #include <linux/dma-fence-array.h> 26 #include <linux/dma-fence-chain.h> 27 #include <linux/irq_work.h> 28 #include <linux/prefetch.h> 29 #include <linux/sched.h> 30 #include <linux/sched/clock.h> 31 #include <linux/sched/signal.h> 32 #include <linux/sched/mm.h> 33 34 #include "gem/i915_gem_context.h" 35 #include "gt/intel_breadcrumbs.h" 36 #include "gt/intel_context.h" 37 #include "gt/intel_engine.h" 38 #include "gt/intel_engine_heartbeat.h" 39 #include "gt/intel_gpu_commands.h" 40 #include "gt/intel_reset.h" 41 #include "gt/intel_ring.h" 42 #include "gt/intel_rps.h" 43 44 #include "i915_active.h" 45 #include "i915_drv.h" 46 #include "i915_trace.h" 47 #include "intel_pm.h" 48 49 struct execute_cb { 50 struct irq_work work; 51 struct i915_sw_fence *fence; 52 struct i915_request *signal; 53 }; 54 55 static struct kmem_cache *slab_requests; 56 static struct kmem_cache *slab_execute_cbs; 57 58 static const char *i915_fence_get_driver_name(struct dma_fence *fence) 59 { 60 return dev_name(to_request(fence)->engine->i915->drm.dev); 61 } 62 63 static const char *i915_fence_get_timeline_name(struct dma_fence *fence) 64 { 65 const struct i915_gem_context *ctx; 66 67 /* 68 * The timeline struct (as part of the ppgtt underneath a context) 69 * may be freed when the request is no longer in use by the GPU. 70 * We could extend the life of a context to beyond that of all 71 * fences, possibly keeping the hw resource around indefinitely, 72 * or we just give them a false name. Since 73 * dma_fence_ops.get_timeline_name is a debug feature, the occasional 74 * lie seems justifiable. 75 */ 76 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 77 return "signaled"; 78 79 ctx = i915_request_gem_context(to_request(fence)); 80 if (!ctx) 81 return "[" DRIVER_NAME "]"; 82 83 return ctx->name; 84 } 85 86 static bool i915_fence_signaled(struct dma_fence *fence) 87 { 88 return i915_request_completed(to_request(fence)); 89 } 90 91 static bool i915_fence_enable_signaling(struct dma_fence *fence) 92 { 93 return i915_request_enable_breadcrumb(to_request(fence)); 94 } 95 96 static signed long i915_fence_wait(struct dma_fence *fence, 97 bool interruptible, 98 signed long timeout) 99 { 100 return i915_request_wait(to_request(fence), 101 interruptible | I915_WAIT_PRIORITY, 102 timeout); 103 } 104 105 struct kmem_cache *i915_request_slab_cache(void) 106 { 107 return slab_requests; 108 } 109 110 static void i915_fence_release(struct dma_fence *fence) 111 { 112 struct i915_request *rq = to_request(fence); 113 114 GEM_BUG_ON(rq->guc_prio != GUC_PRIO_INIT && 115 rq->guc_prio != GUC_PRIO_FINI); 116 117 /* 118 * The request is put onto a RCU freelist (i.e. the address 119 * is immediately reused), mark the fences as being freed now. 120 * Otherwise the debugobjects for the fences are only marked as 121 * freed when the slab cache itself is freed, and so we would get 122 * caught trying to reuse dead objects. 123 */ 124 i915_sw_fence_fini(&rq->submit); 125 i915_sw_fence_fini(&rq->semaphore); 126 127 /* 128 * Keep one request on each engine for reserved use under mempressure, 129 * do not use with virtual engines as this really is only needed for 130 * kernel contexts. 131 */ 132 if (!intel_engine_is_virtual(rq->engine) && 133 !cmpxchg(&rq->engine->request_pool, NULL, rq)) { 134 intel_context_put(rq->context); 135 return; 136 } 137 138 intel_context_put(rq->context); 139 140 kmem_cache_free(slab_requests, rq); 141 } 142 143 const struct dma_fence_ops i915_fence_ops = { 144 .get_driver_name = i915_fence_get_driver_name, 145 .get_timeline_name = i915_fence_get_timeline_name, 146 .enable_signaling = i915_fence_enable_signaling, 147 .signaled = i915_fence_signaled, 148 .wait = i915_fence_wait, 149 .release = i915_fence_release, 150 }; 151 152 static void irq_execute_cb(struct irq_work *wrk) 153 { 154 struct execute_cb *cb = container_of(wrk, typeof(*cb), work); 155 156 i915_sw_fence_complete(cb->fence); 157 kmem_cache_free(slab_execute_cbs, cb); 158 } 159 160 static __always_inline void 161 __notify_execute_cb(struct i915_request *rq, bool (*fn)(struct irq_work *wrk)) 162 { 163 struct execute_cb *cb, *cn; 164 165 if (llist_empty(&rq->execute_cb)) 166 return; 167 168 llist_for_each_entry_safe(cb, cn, 169 llist_del_all(&rq->execute_cb), 170 work.node.llist) 171 fn(&cb->work); 172 } 173 174 static void __notify_execute_cb_irq(struct i915_request *rq) 175 { 176 __notify_execute_cb(rq, irq_work_queue); 177 } 178 179 static bool irq_work_imm(struct irq_work *wrk) 180 { 181 wrk->func(wrk); 182 return false; 183 } 184 185 void i915_request_notify_execute_cb_imm(struct i915_request *rq) 186 { 187 __notify_execute_cb(rq, irq_work_imm); 188 } 189 190 static void free_capture_list(struct i915_request *request) 191 { 192 struct i915_capture_list *capture; 193 194 capture = fetch_and_zero(&request->capture_list); 195 while (capture) { 196 struct i915_capture_list *next = capture->next; 197 198 kfree(capture); 199 capture = next; 200 } 201 } 202 203 static void __i915_request_fill(struct i915_request *rq, u8 val) 204 { 205 void *vaddr = rq->ring->vaddr; 206 u32 head; 207 208 head = rq->infix; 209 if (rq->postfix < head) { 210 memset(vaddr + head, val, rq->ring->size - head); 211 head = 0; 212 } 213 memset(vaddr + head, val, rq->postfix - head); 214 } 215 216 /** 217 * i915_request_active_engine 218 * @rq: request to inspect 219 * @active: pointer in which to return the active engine 220 * 221 * Fills the currently active engine to the @active pointer if the request 222 * is active and still not completed. 223 * 224 * Returns true if request was active or false otherwise. 225 */ 226 bool 227 i915_request_active_engine(struct i915_request *rq, 228 struct intel_engine_cs **active) 229 { 230 struct intel_engine_cs *engine, *locked; 231 bool ret = false; 232 233 /* 234 * Serialise with __i915_request_submit() so that it sees 235 * is-banned?, or we know the request is already inflight. 236 * 237 * Note that rq->engine is unstable, and so we double 238 * check that we have acquired the lock on the final engine. 239 */ 240 locked = READ_ONCE(rq->engine); 241 spin_lock_irq(&locked->sched_engine->lock); 242 while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) { 243 spin_unlock(&locked->sched_engine->lock); 244 locked = engine; 245 spin_lock(&locked->sched_engine->lock); 246 } 247 248 if (i915_request_is_active(rq)) { 249 if (!__i915_request_is_complete(rq)) 250 *active = locked; 251 ret = true; 252 } 253 254 spin_unlock_irq(&locked->sched_engine->lock); 255 256 return ret; 257 } 258 259 static void __rq_init_watchdog(struct i915_request *rq) 260 { 261 rq->watchdog.timer.function = NULL; 262 } 263 264 static enum hrtimer_restart __rq_watchdog_expired(struct hrtimer *hrtimer) 265 { 266 struct i915_request *rq = 267 container_of(hrtimer, struct i915_request, watchdog.timer); 268 struct intel_gt *gt = rq->engine->gt; 269 270 if (!i915_request_completed(rq)) { 271 if (llist_add(&rq->watchdog.link, >->watchdog.list)) 272 schedule_work(>->watchdog.work); 273 } else { 274 i915_request_put(rq); 275 } 276 277 return HRTIMER_NORESTART; 278 } 279 280 static void __rq_arm_watchdog(struct i915_request *rq) 281 { 282 struct i915_request_watchdog *wdg = &rq->watchdog; 283 struct intel_context *ce = rq->context; 284 285 if (!ce->watchdog.timeout_us) 286 return; 287 288 i915_request_get(rq); 289 290 hrtimer_init(&wdg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 291 wdg->timer.function = __rq_watchdog_expired; 292 hrtimer_start_range_ns(&wdg->timer, 293 ns_to_ktime(ce->watchdog.timeout_us * 294 NSEC_PER_USEC), 295 NSEC_PER_MSEC, 296 HRTIMER_MODE_REL); 297 } 298 299 static void __rq_cancel_watchdog(struct i915_request *rq) 300 { 301 struct i915_request_watchdog *wdg = &rq->watchdog; 302 303 if (wdg->timer.function && hrtimer_try_to_cancel(&wdg->timer) > 0) 304 i915_request_put(rq); 305 } 306 307 bool i915_request_retire(struct i915_request *rq) 308 { 309 if (!__i915_request_is_complete(rq)) 310 return false; 311 312 RQ_TRACE(rq, "\n"); 313 314 GEM_BUG_ON(!i915_sw_fence_signaled(&rq->submit)); 315 trace_i915_request_retire(rq); 316 i915_request_mark_complete(rq); 317 318 __rq_cancel_watchdog(rq); 319 320 /* 321 * We know the GPU must have read the request to have 322 * sent us the seqno + interrupt, so use the position 323 * of tail of the request to update the last known position 324 * of the GPU head. 325 * 326 * Note this requires that we are always called in request 327 * completion order. 328 */ 329 GEM_BUG_ON(!list_is_first(&rq->link, 330 &i915_request_timeline(rq)->requests)); 331 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) 332 /* Poison before we release our space in the ring */ 333 __i915_request_fill(rq, POISON_FREE); 334 rq->ring->head = rq->postfix; 335 336 if (!i915_request_signaled(rq)) { 337 spin_lock_irq(&rq->lock); 338 dma_fence_signal_locked(&rq->fence); 339 spin_unlock_irq(&rq->lock); 340 } 341 342 if (test_and_set_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags)) 343 atomic_dec(&rq->engine->gt->rps.num_waiters); 344 345 /* 346 * We only loosely track inflight requests across preemption, 347 * and so we may find ourselves attempting to retire a _completed_ 348 * request that we have removed from the HW and put back on a run 349 * queue. 350 * 351 * As we set I915_FENCE_FLAG_ACTIVE on the request, this should be 352 * after removing the breadcrumb and signaling it, so that we do not 353 * inadvertently attach the breadcrumb to a completed request. 354 */ 355 rq->engine->remove_active_request(rq); 356 GEM_BUG_ON(!llist_empty(&rq->execute_cb)); 357 358 __list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */ 359 360 intel_context_exit(rq->context); 361 intel_context_unpin(rq->context); 362 363 free_capture_list(rq); 364 i915_sched_node_fini(&rq->sched); 365 i915_request_put(rq); 366 367 return true; 368 } 369 370 void i915_request_retire_upto(struct i915_request *rq) 371 { 372 struct intel_timeline * const tl = i915_request_timeline(rq); 373 struct i915_request *tmp; 374 375 RQ_TRACE(rq, "\n"); 376 GEM_BUG_ON(!__i915_request_is_complete(rq)); 377 378 do { 379 tmp = list_first_entry(&tl->requests, typeof(*tmp), link); 380 GEM_BUG_ON(!i915_request_completed(tmp)); 381 } while (i915_request_retire(tmp) && tmp != rq); 382 } 383 384 static struct i915_request * const * 385 __engine_active(struct intel_engine_cs *engine) 386 { 387 return READ_ONCE(engine->execlists.active); 388 } 389 390 static bool __request_in_flight(const struct i915_request *signal) 391 { 392 struct i915_request * const *port, *rq; 393 bool inflight = false; 394 395 if (!i915_request_is_ready(signal)) 396 return false; 397 398 /* 399 * Even if we have unwound the request, it may still be on 400 * the GPU (preempt-to-busy). If that request is inside an 401 * unpreemptible critical section, it will not be removed. Some 402 * GPU functions may even be stuck waiting for the paired request 403 * (__await_execution) to be submitted and cannot be preempted 404 * until the bond is executing. 405 * 406 * As we know that there are always preemption points between 407 * requests, we know that only the currently executing request 408 * may be still active even though we have cleared the flag. 409 * However, we can't rely on our tracking of ELSP[0] to know 410 * which request is currently active and so maybe stuck, as 411 * the tracking maybe an event behind. Instead assume that 412 * if the context is still inflight, then it is still active 413 * even if the active flag has been cleared. 414 * 415 * To further complicate matters, if there a pending promotion, the HW 416 * may either perform a context switch to the second inflight execlists, 417 * or it may switch to the pending set of execlists. In the case of the 418 * latter, it may send the ACK and we process the event copying the 419 * pending[] over top of inflight[], _overwriting_ our *active. Since 420 * this implies the HW is arbitrating and not struck in *active, we do 421 * not worry about complete accuracy, but we do require no read/write 422 * tearing of the pointer [the read of the pointer must be valid, even 423 * as the array is being overwritten, for which we require the writes 424 * to avoid tearing.] 425 * 426 * Note that the read of *execlists->active may race with the promotion 427 * of execlists->pending[] to execlists->inflight[], overwritting 428 * the value at *execlists->active. This is fine. The promotion implies 429 * that we received an ACK from the HW, and so the context is not 430 * stuck -- if we do not see ourselves in *active, the inflight status 431 * is valid. If instead we see ourselves being copied into *active, 432 * we are inflight and may signal the callback. 433 */ 434 if (!intel_context_inflight(signal->context)) 435 return false; 436 437 rcu_read_lock(); 438 for (port = __engine_active(signal->engine); 439 (rq = READ_ONCE(*port)); /* may race with promotion of pending[] */ 440 port++) { 441 if (rq->context == signal->context) { 442 inflight = i915_seqno_passed(rq->fence.seqno, 443 signal->fence.seqno); 444 break; 445 } 446 } 447 rcu_read_unlock(); 448 449 return inflight; 450 } 451 452 static int 453 __await_execution(struct i915_request *rq, 454 struct i915_request *signal, 455 gfp_t gfp) 456 { 457 struct execute_cb *cb; 458 459 if (i915_request_is_active(signal)) 460 return 0; 461 462 cb = kmem_cache_alloc(slab_execute_cbs, gfp); 463 if (!cb) 464 return -ENOMEM; 465 466 cb->fence = &rq->submit; 467 i915_sw_fence_await(cb->fence); 468 init_irq_work(&cb->work, irq_execute_cb); 469 470 /* 471 * Register the callback first, then see if the signaler is already 472 * active. This ensures that if we race with the 473 * __notify_execute_cb from i915_request_submit() and we are not 474 * included in that list, we get a second bite of the cherry and 475 * execute it ourselves. After this point, a future 476 * i915_request_submit() will notify us. 477 * 478 * In i915_request_retire() we set the ACTIVE bit on a completed 479 * request (then flush the execute_cb). So by registering the 480 * callback first, then checking the ACTIVE bit, we serialise with 481 * the completed/retired request. 482 */ 483 if (llist_add(&cb->work.node.llist, &signal->execute_cb)) { 484 if (i915_request_is_active(signal) || 485 __request_in_flight(signal)) 486 i915_request_notify_execute_cb_imm(signal); 487 } 488 489 return 0; 490 } 491 492 static bool fatal_error(int error) 493 { 494 switch (error) { 495 case 0: /* not an error! */ 496 case -EAGAIN: /* innocent victim of a GT reset (__i915_request_reset) */ 497 case -ETIMEDOUT: /* waiting for Godot (timer_i915_sw_fence_wake) */ 498 return false; 499 default: 500 return true; 501 } 502 } 503 504 void __i915_request_skip(struct i915_request *rq) 505 { 506 GEM_BUG_ON(!fatal_error(rq->fence.error)); 507 508 if (rq->infix == rq->postfix) 509 return; 510 511 RQ_TRACE(rq, "error: %d\n", rq->fence.error); 512 513 /* 514 * As this request likely depends on state from the lost 515 * context, clear out all the user operations leaving the 516 * breadcrumb at the end (so we get the fence notifications). 517 */ 518 __i915_request_fill(rq, 0); 519 rq->infix = rq->postfix; 520 } 521 522 bool i915_request_set_error_once(struct i915_request *rq, int error) 523 { 524 int old; 525 526 GEM_BUG_ON(!IS_ERR_VALUE((long)error)); 527 528 if (i915_request_signaled(rq)) 529 return false; 530 531 old = READ_ONCE(rq->fence.error); 532 do { 533 if (fatal_error(old)) 534 return false; 535 } while (!try_cmpxchg(&rq->fence.error, &old, error)); 536 537 return true; 538 } 539 540 struct i915_request *i915_request_mark_eio(struct i915_request *rq) 541 { 542 if (__i915_request_is_complete(rq)) 543 return NULL; 544 545 GEM_BUG_ON(i915_request_signaled(rq)); 546 547 /* As soon as the request is completed, it may be retired */ 548 rq = i915_request_get(rq); 549 550 i915_request_set_error_once(rq, -EIO); 551 i915_request_mark_complete(rq); 552 553 return rq; 554 } 555 556 bool __i915_request_submit(struct i915_request *request) 557 { 558 struct intel_engine_cs *engine = request->engine; 559 bool result = false; 560 561 RQ_TRACE(request, "\n"); 562 563 GEM_BUG_ON(!irqs_disabled()); 564 lockdep_assert_held(&engine->sched_engine->lock); 565 566 /* 567 * With the advent of preempt-to-busy, we frequently encounter 568 * requests that we have unsubmitted from HW, but left running 569 * until the next ack and so have completed in the meantime. On 570 * resubmission of that completed request, we can skip 571 * updating the payload, and execlists can even skip submitting 572 * the request. 573 * 574 * We must remove the request from the caller's priority queue, 575 * and the caller must only call us when the request is in their 576 * priority queue, under the sched_engine->lock. This ensures that the 577 * request has *not* yet been retired and we can safely move 578 * the request into the engine->active.list where it will be 579 * dropped upon retiring. (Otherwise if resubmit a *retired* 580 * request, this would be a horrible use-after-free.) 581 */ 582 if (__i915_request_is_complete(request)) { 583 list_del_init(&request->sched.link); 584 goto active; 585 } 586 587 if (unlikely(intel_context_is_banned(request->context))) 588 i915_request_set_error_once(request, -EIO); 589 590 if (unlikely(fatal_error(request->fence.error))) 591 __i915_request_skip(request); 592 593 /* 594 * Are we using semaphores when the gpu is already saturated? 595 * 596 * Using semaphores incurs a cost in having the GPU poll a 597 * memory location, busywaiting for it to change. The continual 598 * memory reads can have a noticeable impact on the rest of the 599 * system with the extra bus traffic, stalling the cpu as it too 600 * tries to access memory across the bus (perf stat -e bus-cycles). 601 * 602 * If we installed a semaphore on this request and we only submit 603 * the request after the signaler completed, that indicates the 604 * system is overloaded and using semaphores at this time only 605 * increases the amount of work we are doing. If so, we disable 606 * further use of semaphores until we are idle again, whence we 607 * optimistically try again. 608 */ 609 if (request->sched.semaphores && 610 i915_sw_fence_signaled(&request->semaphore)) 611 engine->saturated |= request->sched.semaphores; 612 613 engine->emit_fini_breadcrumb(request, 614 request->ring->vaddr + request->postfix); 615 616 trace_i915_request_execute(request); 617 if (engine->bump_serial) 618 engine->bump_serial(engine); 619 else 620 engine->serial++; 621 622 result = true; 623 624 GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags)); 625 engine->add_active_request(request); 626 active: 627 clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags); 628 set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags); 629 630 /* 631 * XXX Rollback bonded-execution on __i915_request_unsubmit()? 632 * 633 * In the future, perhaps when we have an active time-slicing scheduler, 634 * it will be interesting to unsubmit parallel execution and remove 635 * busywaits from the GPU until their master is restarted. This is 636 * quite hairy, we have to carefully rollback the fence and do a 637 * preempt-to-idle cycle on the target engine, all the while the 638 * master execute_cb may refire. 639 */ 640 __notify_execute_cb_irq(request); 641 642 /* We may be recursing from the signal callback of another i915 fence */ 643 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags)) 644 i915_request_enable_breadcrumb(request); 645 646 return result; 647 } 648 649 void i915_request_submit(struct i915_request *request) 650 { 651 struct intel_engine_cs *engine = request->engine; 652 unsigned long flags; 653 654 /* Will be called from irq-context when using foreign fences. */ 655 spin_lock_irqsave(&engine->sched_engine->lock, flags); 656 657 __i915_request_submit(request); 658 659 spin_unlock_irqrestore(&engine->sched_engine->lock, flags); 660 } 661 662 void __i915_request_unsubmit(struct i915_request *request) 663 { 664 struct intel_engine_cs *engine = request->engine; 665 666 /* 667 * Only unwind in reverse order, required so that the per-context list 668 * is kept in seqno/ring order. 669 */ 670 RQ_TRACE(request, "\n"); 671 672 GEM_BUG_ON(!irqs_disabled()); 673 lockdep_assert_held(&engine->sched_engine->lock); 674 675 /* 676 * Before we remove this breadcrumb from the signal list, we have 677 * to ensure that a concurrent dma_fence_enable_signaling() does not 678 * attach itself. We first mark the request as no longer active and 679 * make sure that is visible to other cores, and then remove the 680 * breadcrumb if attached. 681 */ 682 GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags)); 683 clear_bit_unlock(I915_FENCE_FLAG_ACTIVE, &request->fence.flags); 684 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags)) 685 i915_request_cancel_breadcrumb(request); 686 687 /* We've already spun, don't charge on resubmitting. */ 688 if (request->sched.semaphores && __i915_request_has_started(request)) 689 request->sched.semaphores = 0; 690 691 /* 692 * We don't need to wake_up any waiters on request->execute, they 693 * will get woken by any other event or us re-adding this request 694 * to the engine timeline (__i915_request_submit()). The waiters 695 * should be quite adapt at finding that the request now has a new 696 * global_seqno to the one they went to sleep on. 697 */ 698 } 699 700 void i915_request_unsubmit(struct i915_request *request) 701 { 702 struct intel_engine_cs *engine = request->engine; 703 unsigned long flags; 704 705 /* Will be called from irq-context when using foreign fences. */ 706 spin_lock_irqsave(&engine->sched_engine->lock, flags); 707 708 __i915_request_unsubmit(request); 709 710 spin_unlock_irqrestore(&engine->sched_engine->lock, flags); 711 } 712 713 void i915_request_cancel(struct i915_request *rq, int error) 714 { 715 if (!i915_request_set_error_once(rq, error)) 716 return; 717 718 set_bit(I915_FENCE_FLAG_SENTINEL, &rq->fence.flags); 719 720 intel_context_cancel_request(rq->context, rq); 721 } 722 723 static int __i915_sw_fence_call 724 submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state) 725 { 726 struct i915_request *request = 727 container_of(fence, typeof(*request), submit); 728 729 switch (state) { 730 case FENCE_COMPLETE: 731 trace_i915_request_submit(request); 732 733 if (unlikely(fence->error)) 734 i915_request_set_error_once(request, fence->error); 735 else 736 __rq_arm_watchdog(request); 737 738 /* 739 * We need to serialize use of the submit_request() callback 740 * with its hotplugging performed during an emergency 741 * i915_gem_set_wedged(). We use the RCU mechanism to mark the 742 * critical section in order to force i915_gem_set_wedged() to 743 * wait until the submit_request() is completed before 744 * proceeding. 745 */ 746 rcu_read_lock(); 747 request->engine->submit_request(request); 748 rcu_read_unlock(); 749 break; 750 751 case FENCE_FREE: 752 i915_request_put(request); 753 break; 754 } 755 756 return NOTIFY_DONE; 757 } 758 759 static int __i915_sw_fence_call 760 semaphore_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state) 761 { 762 struct i915_request *rq = container_of(fence, typeof(*rq), semaphore); 763 764 switch (state) { 765 case FENCE_COMPLETE: 766 break; 767 768 case FENCE_FREE: 769 i915_request_put(rq); 770 break; 771 } 772 773 return NOTIFY_DONE; 774 } 775 776 static void retire_requests(struct intel_timeline *tl) 777 { 778 struct i915_request *rq, *rn; 779 780 list_for_each_entry_safe(rq, rn, &tl->requests, link) 781 if (!i915_request_retire(rq)) 782 break; 783 } 784 785 static noinline struct i915_request * 786 request_alloc_slow(struct intel_timeline *tl, 787 struct i915_request **rsvd, 788 gfp_t gfp) 789 { 790 struct i915_request *rq; 791 792 /* If we cannot wait, dip into our reserves */ 793 if (!gfpflags_allow_blocking(gfp)) { 794 rq = xchg(rsvd, NULL); 795 if (!rq) /* Use the normal failure path for one final WARN */ 796 goto out; 797 798 return rq; 799 } 800 801 if (list_empty(&tl->requests)) 802 goto out; 803 804 /* Move our oldest request to the slab-cache (if not in use!) */ 805 rq = list_first_entry(&tl->requests, typeof(*rq), link); 806 i915_request_retire(rq); 807 808 rq = kmem_cache_alloc(slab_requests, 809 gfp | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 810 if (rq) 811 return rq; 812 813 /* Ratelimit ourselves to prevent oom from malicious clients */ 814 rq = list_last_entry(&tl->requests, typeof(*rq), link); 815 cond_synchronize_rcu(rq->rcustate); 816 817 /* Retire our old requests in the hope that we free some */ 818 retire_requests(tl); 819 820 out: 821 return kmem_cache_alloc(slab_requests, gfp); 822 } 823 824 static void __i915_request_ctor(void *arg) 825 { 826 struct i915_request *rq = arg; 827 828 spin_lock_init(&rq->lock); 829 i915_sched_node_init(&rq->sched); 830 i915_sw_fence_init(&rq->submit, submit_notify); 831 i915_sw_fence_init(&rq->semaphore, semaphore_notify); 832 833 rq->capture_list = NULL; 834 835 init_llist_head(&rq->execute_cb); 836 } 837 838 struct i915_request * 839 __i915_request_create(struct intel_context *ce, gfp_t gfp) 840 { 841 struct intel_timeline *tl = ce->timeline; 842 struct i915_request *rq; 843 u32 seqno; 844 int ret; 845 846 might_alloc(gfp); 847 848 /* Check that the caller provided an already pinned context */ 849 __intel_context_pin(ce); 850 851 /* 852 * Beware: Dragons be flying overhead. 853 * 854 * We use RCU to look up requests in flight. The lookups may 855 * race with the request being allocated from the slab freelist. 856 * That is the request we are writing to here, may be in the process 857 * of being read by __i915_active_request_get_rcu(). As such, 858 * we have to be very careful when overwriting the contents. During 859 * the RCU lookup, we change chase the request->engine pointer, 860 * read the request->global_seqno and increment the reference count. 861 * 862 * The reference count is incremented atomically. If it is zero, 863 * the lookup knows the request is unallocated and complete. Otherwise, 864 * it is either still in use, or has been reallocated and reset 865 * with dma_fence_init(). This increment is safe for release as we 866 * check that the request we have a reference to and matches the active 867 * request. 868 * 869 * Before we increment the refcount, we chase the request->engine 870 * pointer. We must not call kmem_cache_zalloc() or else we set 871 * that pointer to NULL and cause a crash during the lookup. If 872 * we see the request is completed (based on the value of the 873 * old engine and seqno), the lookup is complete and reports NULL. 874 * If we decide the request is not completed (new engine or seqno), 875 * then we grab a reference and double check that it is still the 876 * active request - which it won't be and restart the lookup. 877 * 878 * Do not use kmem_cache_zalloc() here! 879 */ 880 rq = kmem_cache_alloc(slab_requests, 881 gfp | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 882 if (unlikely(!rq)) { 883 rq = request_alloc_slow(tl, &ce->engine->request_pool, gfp); 884 if (!rq) { 885 ret = -ENOMEM; 886 goto err_unreserve; 887 } 888 } 889 890 /* 891 * Hold a reference to the intel_context over life of an i915_request. 892 * Without this an i915_request can exist after the context has been 893 * destroyed (e.g. request retired, context closed, but user space holds 894 * a reference to the request from an out fence). In the case of GuC 895 * submission + virtual engine, the engine that the request references 896 * is also destroyed which can trigger bad pointer dref in fence ops 897 * (e.g. i915_fence_get_driver_name). We could likely change these 898 * functions to avoid touching the engine but let's just be safe and 899 * hold the intel_context reference. In execlist mode the request always 900 * eventually points to a physical engine so this isn't an issue. 901 */ 902 rq->context = intel_context_get(ce); 903 rq->engine = ce->engine; 904 rq->ring = ce->ring; 905 rq->execution_mask = ce->engine->mask; 906 907 ret = intel_timeline_get_seqno(tl, rq, &seqno); 908 if (ret) 909 goto err_free; 910 911 dma_fence_init(&rq->fence, &i915_fence_ops, &rq->lock, 912 tl->fence_context, seqno); 913 914 RCU_INIT_POINTER(rq->timeline, tl); 915 rq->hwsp_seqno = tl->hwsp_seqno; 916 GEM_BUG_ON(__i915_request_is_complete(rq)); 917 918 rq->rcustate = get_state_synchronize_rcu(); /* acts as smp_mb() */ 919 920 rq->guc_prio = GUC_PRIO_INIT; 921 922 /* We bump the ref for the fence chain */ 923 i915_sw_fence_reinit(&i915_request_get(rq)->submit); 924 i915_sw_fence_reinit(&i915_request_get(rq)->semaphore); 925 926 i915_sched_node_reinit(&rq->sched); 927 928 /* No zalloc, everything must be cleared after use */ 929 rq->batch = NULL; 930 __rq_init_watchdog(rq); 931 GEM_BUG_ON(rq->capture_list); 932 GEM_BUG_ON(!llist_empty(&rq->execute_cb)); 933 934 /* 935 * Reserve space in the ring buffer for all the commands required to 936 * eventually emit this request. This is to guarantee that the 937 * i915_request_add() call can't fail. Note that the reserve may need 938 * to be redone if the request is not actually submitted straight 939 * away, e.g. because a GPU scheduler has deferred it. 940 * 941 * Note that due to how we add reserved_space to intel_ring_begin() 942 * we need to double our request to ensure that if we need to wrap 943 * around inside i915_request_add() there is sufficient space at 944 * the beginning of the ring as well. 945 */ 946 rq->reserved_space = 947 2 * rq->engine->emit_fini_breadcrumb_dw * sizeof(u32); 948 949 /* 950 * Record the position of the start of the request so that 951 * should we detect the updated seqno part-way through the 952 * GPU processing the request, we never over-estimate the 953 * position of the head. 954 */ 955 rq->head = rq->ring->emit; 956 957 ret = rq->engine->request_alloc(rq); 958 if (ret) 959 goto err_unwind; 960 961 rq->infix = rq->ring->emit; /* end of header; start of user payload */ 962 963 intel_context_mark_active(ce); 964 list_add_tail_rcu(&rq->link, &tl->requests); 965 966 return rq; 967 968 err_unwind: 969 ce->ring->emit = rq->head; 970 971 /* Make sure we didn't add ourselves to external state before freeing */ 972 GEM_BUG_ON(!list_empty(&rq->sched.signalers_list)); 973 GEM_BUG_ON(!list_empty(&rq->sched.waiters_list)); 974 975 err_free: 976 intel_context_put(ce); 977 kmem_cache_free(slab_requests, rq); 978 err_unreserve: 979 intel_context_unpin(ce); 980 return ERR_PTR(ret); 981 } 982 983 struct i915_request * 984 i915_request_create(struct intel_context *ce) 985 { 986 struct i915_request *rq; 987 struct intel_timeline *tl; 988 989 tl = intel_context_timeline_lock(ce); 990 if (IS_ERR(tl)) 991 return ERR_CAST(tl); 992 993 /* Move our oldest request to the slab-cache (if not in use!) */ 994 rq = list_first_entry(&tl->requests, typeof(*rq), link); 995 if (!list_is_last(&rq->link, &tl->requests)) 996 i915_request_retire(rq); 997 998 intel_context_enter(ce); 999 rq = __i915_request_create(ce, GFP_KERNEL); 1000 intel_context_exit(ce); /* active reference transferred to request */ 1001 if (IS_ERR(rq)) 1002 goto err_unlock; 1003 1004 /* Check that we do not interrupt ourselves with a new request */ 1005 rq->cookie = lockdep_pin_lock(&tl->mutex); 1006 1007 return rq; 1008 1009 err_unlock: 1010 intel_context_timeline_unlock(tl); 1011 return rq; 1012 } 1013 1014 static int 1015 i915_request_await_start(struct i915_request *rq, struct i915_request *signal) 1016 { 1017 struct dma_fence *fence; 1018 int err; 1019 1020 if (i915_request_timeline(rq) == rcu_access_pointer(signal->timeline)) 1021 return 0; 1022 1023 if (i915_request_started(signal)) 1024 return 0; 1025 1026 /* 1027 * The caller holds a reference on @signal, but we do not serialise 1028 * against it being retired and removed from the lists. 1029 * 1030 * We do not hold a reference to the request before @signal, and 1031 * so must be very careful to ensure that it is not _recycled_ as 1032 * we follow the link backwards. 1033 */ 1034 fence = NULL; 1035 rcu_read_lock(); 1036 do { 1037 struct list_head *pos = READ_ONCE(signal->link.prev); 1038 struct i915_request *prev; 1039 1040 /* Confirm signal has not been retired, the link is valid */ 1041 if (unlikely(__i915_request_has_started(signal))) 1042 break; 1043 1044 /* Is signal the earliest request on its timeline? */ 1045 if (pos == &rcu_dereference(signal->timeline)->requests) 1046 break; 1047 1048 /* 1049 * Peek at the request before us in the timeline. That 1050 * request will only be valid before it is retired, so 1051 * after acquiring a reference to it, confirm that it is 1052 * still part of the signaler's timeline. 1053 */ 1054 prev = list_entry(pos, typeof(*prev), link); 1055 if (!i915_request_get_rcu(prev)) 1056 break; 1057 1058 /* After the strong barrier, confirm prev is still attached */ 1059 if (unlikely(READ_ONCE(prev->link.next) != &signal->link)) { 1060 i915_request_put(prev); 1061 break; 1062 } 1063 1064 fence = &prev->fence; 1065 } while (0); 1066 rcu_read_unlock(); 1067 if (!fence) 1068 return 0; 1069 1070 err = 0; 1071 if (!intel_timeline_sync_is_later(i915_request_timeline(rq), fence)) 1072 err = i915_sw_fence_await_dma_fence(&rq->submit, 1073 fence, 0, 1074 I915_FENCE_GFP); 1075 dma_fence_put(fence); 1076 1077 return err; 1078 } 1079 1080 static intel_engine_mask_t 1081 already_busywaiting(struct i915_request *rq) 1082 { 1083 /* 1084 * Polling a semaphore causes bus traffic, delaying other users of 1085 * both the GPU and CPU. We want to limit the impact on others, 1086 * while taking advantage of early submission to reduce GPU 1087 * latency. Therefore we restrict ourselves to not using more 1088 * than one semaphore from each source, and not using a semaphore 1089 * if we have detected the engine is saturated (i.e. would not be 1090 * submitted early and cause bus traffic reading an already passed 1091 * semaphore). 1092 * 1093 * See the are-we-too-late? check in __i915_request_submit(). 1094 */ 1095 return rq->sched.semaphores | READ_ONCE(rq->engine->saturated); 1096 } 1097 1098 static int 1099 __emit_semaphore_wait(struct i915_request *to, 1100 struct i915_request *from, 1101 u32 seqno) 1102 { 1103 const int has_token = GRAPHICS_VER(to->engine->i915) >= 12; 1104 u32 hwsp_offset; 1105 int len, err; 1106 u32 *cs; 1107 1108 GEM_BUG_ON(GRAPHICS_VER(to->engine->i915) < 8); 1109 GEM_BUG_ON(i915_request_has_initial_breadcrumb(to)); 1110 1111 /* We need to pin the signaler's HWSP until we are finished reading. */ 1112 err = intel_timeline_read_hwsp(from, to, &hwsp_offset); 1113 if (err) 1114 return err; 1115 1116 len = 4; 1117 if (has_token) 1118 len += 2; 1119 1120 cs = intel_ring_begin(to, len); 1121 if (IS_ERR(cs)) 1122 return PTR_ERR(cs); 1123 1124 /* 1125 * Using greater-than-or-equal here means we have to worry 1126 * about seqno wraparound. To side step that issue, we swap 1127 * the timeline HWSP upon wrapping, so that everyone listening 1128 * for the old (pre-wrap) values do not see the much smaller 1129 * (post-wrap) values than they were expecting (and so wait 1130 * forever). 1131 */ 1132 *cs++ = (MI_SEMAPHORE_WAIT | 1133 MI_SEMAPHORE_GLOBAL_GTT | 1134 MI_SEMAPHORE_POLL | 1135 MI_SEMAPHORE_SAD_GTE_SDD) + 1136 has_token; 1137 *cs++ = seqno; 1138 *cs++ = hwsp_offset; 1139 *cs++ = 0; 1140 if (has_token) { 1141 *cs++ = 0; 1142 *cs++ = MI_NOOP; 1143 } 1144 1145 intel_ring_advance(to, cs); 1146 return 0; 1147 } 1148 1149 static bool 1150 can_use_semaphore_wait(struct i915_request *to, struct i915_request *from) 1151 { 1152 return to->engine->gt->ggtt == from->engine->gt->ggtt; 1153 } 1154 1155 static int 1156 emit_semaphore_wait(struct i915_request *to, 1157 struct i915_request *from, 1158 gfp_t gfp) 1159 { 1160 const intel_engine_mask_t mask = READ_ONCE(from->engine)->mask; 1161 struct i915_sw_fence *wait = &to->submit; 1162 1163 if (!can_use_semaphore_wait(to, from)) 1164 goto await_fence; 1165 1166 if (!intel_context_use_semaphores(to->context)) 1167 goto await_fence; 1168 1169 if (i915_request_has_initial_breadcrumb(to)) 1170 goto await_fence; 1171 1172 /* 1173 * If this or its dependents are waiting on an external fence 1174 * that may fail catastrophically, then we want to avoid using 1175 * sempahores as they bypass the fence signaling metadata, and we 1176 * lose the fence->error propagation. 1177 */ 1178 if (from->sched.flags & I915_SCHED_HAS_EXTERNAL_CHAIN) 1179 goto await_fence; 1180 1181 /* Just emit the first semaphore we see as request space is limited. */ 1182 if (already_busywaiting(to) & mask) 1183 goto await_fence; 1184 1185 if (i915_request_await_start(to, from) < 0) 1186 goto await_fence; 1187 1188 /* Only submit our spinner after the signaler is running! */ 1189 if (__await_execution(to, from, gfp)) 1190 goto await_fence; 1191 1192 if (__emit_semaphore_wait(to, from, from->fence.seqno)) 1193 goto await_fence; 1194 1195 to->sched.semaphores |= mask; 1196 wait = &to->semaphore; 1197 1198 await_fence: 1199 return i915_sw_fence_await_dma_fence(wait, 1200 &from->fence, 0, 1201 I915_FENCE_GFP); 1202 } 1203 1204 static bool intel_timeline_sync_has_start(struct intel_timeline *tl, 1205 struct dma_fence *fence) 1206 { 1207 return __intel_timeline_sync_is_later(tl, 1208 fence->context, 1209 fence->seqno - 1); 1210 } 1211 1212 static int intel_timeline_sync_set_start(struct intel_timeline *tl, 1213 const struct dma_fence *fence) 1214 { 1215 return __intel_timeline_sync_set(tl, fence->context, fence->seqno - 1); 1216 } 1217 1218 static int 1219 __i915_request_await_execution(struct i915_request *to, 1220 struct i915_request *from) 1221 { 1222 int err; 1223 1224 GEM_BUG_ON(intel_context_is_barrier(from->context)); 1225 1226 /* Submit both requests at the same time */ 1227 err = __await_execution(to, from, I915_FENCE_GFP); 1228 if (err) 1229 return err; 1230 1231 /* Squash repeated depenendices to the same timelines */ 1232 if (intel_timeline_sync_has_start(i915_request_timeline(to), 1233 &from->fence)) 1234 return 0; 1235 1236 /* 1237 * Wait until the start of this request. 1238 * 1239 * The execution cb fires when we submit the request to HW. But in 1240 * many cases this may be long before the request itself is ready to 1241 * run (consider that we submit 2 requests for the same context, where 1242 * the request of interest is behind an indefinite spinner). So we hook 1243 * up to both to reduce our queues and keep the execution lag minimised 1244 * in the worst case, though we hope that the await_start is elided. 1245 */ 1246 err = i915_request_await_start(to, from); 1247 if (err < 0) 1248 return err; 1249 1250 /* 1251 * Ensure both start together [after all semaphores in signal] 1252 * 1253 * Now that we are queued to the HW at roughly the same time (thanks 1254 * to the execute cb) and are ready to run at roughly the same time 1255 * (thanks to the await start), our signaler may still be indefinitely 1256 * delayed by waiting on a semaphore from a remote engine. If our 1257 * signaler depends on a semaphore, so indirectly do we, and we do not 1258 * want to start our payload until our signaler also starts theirs. 1259 * So we wait. 1260 * 1261 * However, there is also a second condition for which we need to wait 1262 * for the precise start of the signaler. Consider that the signaler 1263 * was submitted in a chain of requests following another context 1264 * (with just an ordinary intra-engine fence dependency between the 1265 * two). In this case the signaler is queued to HW, but not for 1266 * immediate execution, and so we must wait until it reaches the 1267 * active slot. 1268 */ 1269 if (can_use_semaphore_wait(to, from) && 1270 intel_engine_has_semaphores(to->engine) && 1271 !i915_request_has_initial_breadcrumb(to)) { 1272 err = __emit_semaphore_wait(to, from, from->fence.seqno - 1); 1273 if (err < 0) 1274 return err; 1275 } 1276 1277 /* Couple the dependency tree for PI on this exposed to->fence */ 1278 if (to->engine->sched_engine->schedule) { 1279 err = i915_sched_node_add_dependency(&to->sched, 1280 &from->sched, 1281 I915_DEPENDENCY_WEAK); 1282 if (err < 0) 1283 return err; 1284 } 1285 1286 return intel_timeline_sync_set_start(i915_request_timeline(to), 1287 &from->fence); 1288 } 1289 1290 static void mark_external(struct i915_request *rq) 1291 { 1292 /* 1293 * The downside of using semaphores is that we lose metadata passing 1294 * along the signaling chain. This is particularly nasty when we 1295 * need to pass along a fatal error such as EFAULT or EDEADLK. For 1296 * fatal errors we want to scrub the request before it is executed, 1297 * which means that we cannot preload the request onto HW and have 1298 * it wait upon a semaphore. 1299 */ 1300 rq->sched.flags |= I915_SCHED_HAS_EXTERNAL_CHAIN; 1301 } 1302 1303 static int 1304 __i915_request_await_external(struct i915_request *rq, struct dma_fence *fence) 1305 { 1306 mark_external(rq); 1307 return i915_sw_fence_await_dma_fence(&rq->submit, fence, 1308 i915_fence_context_timeout(rq->engine->i915, 1309 fence->context), 1310 I915_FENCE_GFP); 1311 } 1312 1313 static int 1314 i915_request_await_external(struct i915_request *rq, struct dma_fence *fence) 1315 { 1316 struct dma_fence *iter; 1317 int err = 0; 1318 1319 if (!to_dma_fence_chain(fence)) 1320 return __i915_request_await_external(rq, fence); 1321 1322 dma_fence_chain_for_each(iter, fence) { 1323 struct dma_fence_chain *chain = to_dma_fence_chain(iter); 1324 1325 if (!dma_fence_is_i915(chain->fence)) { 1326 err = __i915_request_await_external(rq, iter); 1327 break; 1328 } 1329 1330 err = i915_request_await_dma_fence(rq, chain->fence); 1331 if (err < 0) 1332 break; 1333 } 1334 1335 dma_fence_put(iter); 1336 return err; 1337 } 1338 1339 static inline bool is_parallel_rq(struct i915_request *rq) 1340 { 1341 return intel_context_is_parallel(rq->context); 1342 } 1343 1344 static inline struct intel_context *request_to_parent(struct i915_request *rq) 1345 { 1346 return intel_context_to_parent(rq->context); 1347 } 1348 1349 static bool is_same_parallel_context(struct i915_request *to, 1350 struct i915_request *from) 1351 { 1352 if (is_parallel_rq(to)) 1353 return request_to_parent(to) == request_to_parent(from); 1354 1355 return false; 1356 } 1357 1358 int 1359 i915_request_await_execution(struct i915_request *rq, 1360 struct dma_fence *fence) 1361 { 1362 struct dma_fence **child = &fence; 1363 unsigned int nchild = 1; 1364 int ret; 1365 1366 if (dma_fence_is_array(fence)) { 1367 struct dma_fence_array *array = to_dma_fence_array(fence); 1368 1369 /* XXX Error for signal-on-any fence arrays */ 1370 1371 child = array->fences; 1372 nchild = array->num_fences; 1373 GEM_BUG_ON(!nchild); 1374 } 1375 1376 do { 1377 fence = *child++; 1378 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 1379 continue; 1380 1381 if (fence->context == rq->fence.context) 1382 continue; 1383 1384 /* 1385 * We don't squash repeated fence dependencies here as we 1386 * want to run our callback in all cases. 1387 */ 1388 1389 if (dma_fence_is_i915(fence)) { 1390 if (is_same_parallel_context(rq, to_request(fence))) 1391 continue; 1392 ret = __i915_request_await_execution(rq, 1393 to_request(fence)); 1394 } else { 1395 ret = i915_request_await_external(rq, fence); 1396 } 1397 if (ret < 0) 1398 return ret; 1399 } while (--nchild); 1400 1401 return 0; 1402 } 1403 1404 static int 1405 await_request_submit(struct i915_request *to, struct i915_request *from) 1406 { 1407 /* 1408 * If we are waiting on a virtual engine, then it may be 1409 * constrained to execute on a single engine *prior* to submission. 1410 * When it is submitted, it will be first submitted to the virtual 1411 * engine and then passed to the physical engine. We cannot allow 1412 * the waiter to be submitted immediately to the physical engine 1413 * as it may then bypass the virtual request. 1414 */ 1415 if (to->engine == READ_ONCE(from->engine)) 1416 return i915_sw_fence_await_sw_fence_gfp(&to->submit, 1417 &from->submit, 1418 I915_FENCE_GFP); 1419 else 1420 return __i915_request_await_execution(to, from); 1421 } 1422 1423 static int 1424 i915_request_await_request(struct i915_request *to, struct i915_request *from) 1425 { 1426 int ret; 1427 1428 GEM_BUG_ON(to == from); 1429 GEM_BUG_ON(to->timeline == from->timeline); 1430 1431 if (i915_request_completed(from)) { 1432 i915_sw_fence_set_error_once(&to->submit, from->fence.error); 1433 return 0; 1434 } 1435 1436 if (to->engine->sched_engine->schedule) { 1437 ret = i915_sched_node_add_dependency(&to->sched, 1438 &from->sched, 1439 I915_DEPENDENCY_EXTERNAL); 1440 if (ret < 0) 1441 return ret; 1442 } 1443 1444 if (!intel_engine_uses_guc(to->engine) && 1445 is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask))) 1446 ret = await_request_submit(to, from); 1447 else 1448 ret = emit_semaphore_wait(to, from, I915_FENCE_GFP); 1449 if (ret < 0) 1450 return ret; 1451 1452 return 0; 1453 } 1454 1455 int 1456 i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence) 1457 { 1458 struct dma_fence **child = &fence; 1459 unsigned int nchild = 1; 1460 int ret; 1461 1462 /* 1463 * Note that if the fence-array was created in signal-on-any mode, 1464 * we should *not* decompose it into its individual fences. However, 1465 * we don't currently store which mode the fence-array is operating 1466 * in. Fortunately, the only user of signal-on-any is private to 1467 * amdgpu and we should not see any incoming fence-array from 1468 * sync-file being in signal-on-any mode. 1469 */ 1470 if (dma_fence_is_array(fence)) { 1471 struct dma_fence_array *array = to_dma_fence_array(fence); 1472 1473 child = array->fences; 1474 nchild = array->num_fences; 1475 GEM_BUG_ON(!nchild); 1476 } 1477 1478 do { 1479 fence = *child++; 1480 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 1481 continue; 1482 1483 /* 1484 * Requests on the same timeline are explicitly ordered, along 1485 * with their dependencies, by i915_request_add() which ensures 1486 * that requests are submitted in-order through each ring. 1487 */ 1488 if (fence->context == rq->fence.context) 1489 continue; 1490 1491 /* Squash repeated waits to the same timelines */ 1492 if (fence->context && 1493 intel_timeline_sync_is_later(i915_request_timeline(rq), 1494 fence)) 1495 continue; 1496 1497 if (dma_fence_is_i915(fence)) { 1498 if (is_same_parallel_context(rq, to_request(fence))) 1499 continue; 1500 ret = i915_request_await_request(rq, to_request(fence)); 1501 } else { 1502 ret = i915_request_await_external(rq, fence); 1503 } 1504 if (ret < 0) 1505 return ret; 1506 1507 /* Record the latest fence used against each timeline */ 1508 if (fence->context) 1509 intel_timeline_sync_set(i915_request_timeline(rq), 1510 fence); 1511 } while (--nchild); 1512 1513 return 0; 1514 } 1515 1516 /** 1517 * i915_request_await_object - set this request to (async) wait upon a bo 1518 * @to: request we are wishing to use 1519 * @obj: object which may be in use on another ring. 1520 * @write: whether the wait is on behalf of a writer 1521 * 1522 * This code is meant to abstract object synchronization with the GPU. 1523 * Conceptually we serialise writes between engines inside the GPU. 1524 * We only allow one engine to write into a buffer at any time, but 1525 * multiple readers. To ensure each has a coherent view of memory, we must: 1526 * 1527 * - If there is an outstanding write request to the object, the new 1528 * request must wait for it to complete (either CPU or in hw, requests 1529 * on the same ring will be naturally ordered). 1530 * 1531 * - If we are a write request (pending_write_domain is set), the new 1532 * request must wait for outstanding read requests to complete. 1533 * 1534 * Returns 0 if successful, else propagates up the lower layer error. 1535 */ 1536 int 1537 i915_request_await_object(struct i915_request *to, 1538 struct drm_i915_gem_object *obj, 1539 bool write) 1540 { 1541 struct dma_resv_iter cursor; 1542 struct dma_fence *fence; 1543 int ret = 0; 1544 1545 dma_resv_for_each_fence(&cursor, obj->base.resv, write, fence) { 1546 ret = i915_request_await_dma_fence(to, fence); 1547 if (ret) 1548 break; 1549 } 1550 1551 return ret; 1552 } 1553 1554 static struct i915_request * 1555 __i915_request_ensure_parallel_ordering(struct i915_request *rq, 1556 struct intel_timeline *timeline) 1557 { 1558 struct i915_request *prev; 1559 1560 GEM_BUG_ON(!is_parallel_rq(rq)); 1561 1562 prev = request_to_parent(rq)->parallel.last_rq; 1563 if (prev) { 1564 if (!__i915_request_is_complete(prev)) { 1565 i915_sw_fence_await_sw_fence(&rq->submit, 1566 &prev->submit, 1567 &rq->submitq); 1568 1569 if (rq->engine->sched_engine->schedule) 1570 __i915_sched_node_add_dependency(&rq->sched, 1571 &prev->sched, 1572 &rq->dep, 1573 0); 1574 } 1575 i915_request_put(prev); 1576 } 1577 1578 request_to_parent(rq)->parallel.last_rq = i915_request_get(rq); 1579 1580 return to_request(__i915_active_fence_set(&timeline->last_request, 1581 &rq->fence)); 1582 } 1583 1584 static struct i915_request * 1585 __i915_request_ensure_ordering(struct i915_request *rq, 1586 struct intel_timeline *timeline) 1587 { 1588 struct i915_request *prev; 1589 1590 GEM_BUG_ON(is_parallel_rq(rq)); 1591 1592 prev = to_request(__i915_active_fence_set(&timeline->last_request, 1593 &rq->fence)); 1594 1595 if (prev && !__i915_request_is_complete(prev)) { 1596 bool uses_guc = intel_engine_uses_guc(rq->engine); 1597 bool pow2 = is_power_of_2(READ_ONCE(prev->engine)->mask | 1598 rq->engine->mask); 1599 bool same_context = prev->context == rq->context; 1600 1601 /* 1602 * The requests are supposed to be kept in order. However, 1603 * we need to be wary in case the timeline->last_request 1604 * is used as a barrier for external modification to this 1605 * context. 1606 */ 1607 GEM_BUG_ON(same_context && 1608 i915_seqno_passed(prev->fence.seqno, 1609 rq->fence.seqno)); 1610 1611 if ((same_context && uses_guc) || (!uses_guc && pow2)) 1612 i915_sw_fence_await_sw_fence(&rq->submit, 1613 &prev->submit, 1614 &rq->submitq); 1615 else 1616 __i915_sw_fence_await_dma_fence(&rq->submit, 1617 &prev->fence, 1618 &rq->dmaq); 1619 if (rq->engine->sched_engine->schedule) 1620 __i915_sched_node_add_dependency(&rq->sched, 1621 &prev->sched, 1622 &rq->dep, 1623 0); 1624 } 1625 1626 return prev; 1627 } 1628 1629 static struct i915_request * 1630 __i915_request_add_to_timeline(struct i915_request *rq) 1631 { 1632 struct intel_timeline *timeline = i915_request_timeline(rq); 1633 struct i915_request *prev; 1634 1635 /* 1636 * Dependency tracking and request ordering along the timeline 1637 * is special cased so that we can eliminate redundant ordering 1638 * operations while building the request (we know that the timeline 1639 * itself is ordered, and here we guarantee it). 1640 * 1641 * As we know we will need to emit tracking along the timeline, 1642 * we embed the hooks into our request struct -- at the cost of 1643 * having to have specialised no-allocation interfaces (which will 1644 * be beneficial elsewhere). 1645 * 1646 * A second benefit to open-coding i915_request_await_request is 1647 * that we can apply a slight variant of the rules specialised 1648 * for timelines that jump between engines (such as virtual engines). 1649 * If we consider the case of virtual engine, we must emit a dma-fence 1650 * to prevent scheduling of the second request until the first is 1651 * complete (to maximise our greedy late load balancing) and this 1652 * precludes optimising to use semaphores serialisation of a single 1653 * timeline across engines. 1654 * 1655 * We do not order parallel submission requests on the timeline as each 1656 * parallel submission context has its own timeline and the ordering 1657 * rules for parallel requests are that they must be submitted in the 1658 * order received from the execbuf IOCTL. So rather than using the 1659 * timeline we store a pointer to last request submitted in the 1660 * relationship in the gem context and insert a submission fence 1661 * between that request and request passed into this function or 1662 * alternatively we use completion fence if gem context has a single 1663 * timeline and this is the first submission of an execbuf IOCTL. 1664 */ 1665 if (likely(!is_parallel_rq(rq))) 1666 prev = __i915_request_ensure_ordering(rq, timeline); 1667 else 1668 prev = __i915_request_ensure_parallel_ordering(rq, timeline); 1669 1670 /* 1671 * Make sure that no request gazumped us - if it was allocated after 1672 * our i915_request_alloc() and called __i915_request_add() before 1673 * us, the timeline will hold its seqno which is later than ours. 1674 */ 1675 GEM_BUG_ON(timeline->seqno != rq->fence.seqno); 1676 1677 return prev; 1678 } 1679 1680 /* 1681 * NB: This function is not allowed to fail. Doing so would mean the the 1682 * request is not being tracked for completion but the work itself is 1683 * going to happen on the hardware. This would be a Bad Thing(tm). 1684 */ 1685 struct i915_request *__i915_request_commit(struct i915_request *rq) 1686 { 1687 struct intel_engine_cs *engine = rq->engine; 1688 struct intel_ring *ring = rq->ring; 1689 u32 *cs; 1690 1691 RQ_TRACE(rq, "\n"); 1692 1693 /* 1694 * To ensure that this call will not fail, space for its emissions 1695 * should already have been reserved in the ring buffer. Let the ring 1696 * know that it is time to use that space up. 1697 */ 1698 GEM_BUG_ON(rq->reserved_space > ring->space); 1699 rq->reserved_space = 0; 1700 rq->emitted_jiffies = jiffies; 1701 1702 /* 1703 * Record the position of the start of the breadcrumb so that 1704 * should we detect the updated seqno part-way through the 1705 * GPU processing the request, we never over-estimate the 1706 * position of the ring's HEAD. 1707 */ 1708 cs = intel_ring_begin(rq, engine->emit_fini_breadcrumb_dw); 1709 GEM_BUG_ON(IS_ERR(cs)); 1710 rq->postfix = intel_ring_offset(rq, cs); 1711 1712 return __i915_request_add_to_timeline(rq); 1713 } 1714 1715 void __i915_request_queue_bh(struct i915_request *rq) 1716 { 1717 i915_sw_fence_commit(&rq->semaphore); 1718 i915_sw_fence_commit(&rq->submit); 1719 } 1720 1721 void __i915_request_queue(struct i915_request *rq, 1722 const struct i915_sched_attr *attr) 1723 { 1724 /* 1725 * Let the backend know a new request has arrived that may need 1726 * to adjust the existing execution schedule due to a high priority 1727 * request - i.e. we may want to preempt the current request in order 1728 * to run a high priority dependency chain *before* we can execute this 1729 * request. 1730 * 1731 * This is called before the request is ready to run so that we can 1732 * decide whether to preempt the entire chain so that it is ready to 1733 * run at the earliest possible convenience. 1734 */ 1735 if (attr && rq->engine->sched_engine->schedule) 1736 rq->engine->sched_engine->schedule(rq, attr); 1737 1738 local_bh_disable(); 1739 __i915_request_queue_bh(rq); 1740 local_bh_enable(); /* kick tasklets */ 1741 } 1742 1743 void i915_request_add(struct i915_request *rq) 1744 { 1745 struct intel_timeline * const tl = i915_request_timeline(rq); 1746 struct i915_sched_attr attr = {}; 1747 struct i915_gem_context *ctx; 1748 1749 lockdep_assert_held(&tl->mutex); 1750 lockdep_unpin_lock(&tl->mutex, rq->cookie); 1751 1752 trace_i915_request_add(rq); 1753 __i915_request_commit(rq); 1754 1755 /* XXX placeholder for selftests */ 1756 rcu_read_lock(); 1757 ctx = rcu_dereference(rq->context->gem_context); 1758 if (ctx) 1759 attr = ctx->sched; 1760 rcu_read_unlock(); 1761 1762 __i915_request_queue(rq, &attr); 1763 1764 mutex_unlock(&tl->mutex); 1765 } 1766 1767 static unsigned long local_clock_ns(unsigned int *cpu) 1768 { 1769 unsigned long t; 1770 1771 /* 1772 * Cheaply and approximately convert from nanoseconds to microseconds. 1773 * The result and subsequent calculations are also defined in the same 1774 * approximate microseconds units. The principal source of timing 1775 * error here is from the simple truncation. 1776 * 1777 * Note that local_clock() is only defined wrt to the current CPU; 1778 * the comparisons are no longer valid if we switch CPUs. Instead of 1779 * blocking preemption for the entire busywait, we can detect the CPU 1780 * switch and use that as indicator of system load and a reason to 1781 * stop busywaiting, see busywait_stop(). 1782 */ 1783 *cpu = get_cpu(); 1784 t = local_clock(); 1785 put_cpu(); 1786 1787 return t; 1788 } 1789 1790 static bool busywait_stop(unsigned long timeout, unsigned int cpu) 1791 { 1792 unsigned int this_cpu; 1793 1794 if (time_after(local_clock_ns(&this_cpu), timeout)) 1795 return true; 1796 1797 return this_cpu != cpu; 1798 } 1799 1800 static bool __i915_spin_request(struct i915_request * const rq, int state) 1801 { 1802 unsigned long timeout_ns; 1803 unsigned int cpu; 1804 1805 /* 1806 * Only wait for the request if we know it is likely to complete. 1807 * 1808 * We don't track the timestamps around requests, nor the average 1809 * request length, so we do not have a good indicator that this 1810 * request will complete within the timeout. What we do know is the 1811 * order in which requests are executed by the context and so we can 1812 * tell if the request has been started. If the request is not even 1813 * running yet, it is a fair assumption that it will not complete 1814 * within our relatively short timeout. 1815 */ 1816 if (!i915_request_is_running(rq)) 1817 return false; 1818 1819 /* 1820 * When waiting for high frequency requests, e.g. during synchronous 1821 * rendering split between the CPU and GPU, the finite amount of time 1822 * required to set up the irq and wait upon it limits the response 1823 * rate. By busywaiting on the request completion for a short while we 1824 * can service the high frequency waits as quick as possible. However, 1825 * if it is a slow request, we want to sleep as quickly as possible. 1826 * The tradeoff between waiting and sleeping is roughly the time it 1827 * takes to sleep on a request, on the order of a microsecond. 1828 */ 1829 1830 timeout_ns = READ_ONCE(rq->engine->props.max_busywait_duration_ns); 1831 timeout_ns += local_clock_ns(&cpu); 1832 do { 1833 if (dma_fence_is_signaled(&rq->fence)) 1834 return true; 1835 1836 if (signal_pending_state(state, current)) 1837 break; 1838 1839 if (busywait_stop(timeout_ns, cpu)) 1840 break; 1841 1842 cpu_relax(); 1843 } while (!need_resched()); 1844 1845 return false; 1846 } 1847 1848 struct request_wait { 1849 struct dma_fence_cb cb; 1850 struct task_struct *tsk; 1851 }; 1852 1853 static void request_wait_wake(struct dma_fence *fence, struct dma_fence_cb *cb) 1854 { 1855 struct request_wait *wait = container_of(cb, typeof(*wait), cb); 1856 1857 wake_up_process(fetch_and_zero(&wait->tsk)); 1858 } 1859 1860 /** 1861 * i915_request_wait - wait until execution of request has finished 1862 * @rq: the request to wait upon 1863 * @flags: how to wait 1864 * @timeout: how long to wait in jiffies 1865 * 1866 * i915_request_wait() waits for the request to be completed, for a 1867 * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an 1868 * unbounded wait). 1869 * 1870 * Returns the remaining time (in jiffies) if the request completed, which may 1871 * be zero or -ETIME if the request is unfinished after the timeout expires. 1872 * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is 1873 * pending before the request completes. 1874 */ 1875 long i915_request_wait(struct i915_request *rq, 1876 unsigned int flags, 1877 long timeout) 1878 { 1879 const int state = flags & I915_WAIT_INTERRUPTIBLE ? 1880 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; 1881 struct request_wait wait; 1882 1883 might_sleep(); 1884 GEM_BUG_ON(timeout < 0); 1885 1886 if (dma_fence_is_signaled(&rq->fence)) 1887 return timeout; 1888 1889 if (!timeout) 1890 return -ETIME; 1891 1892 trace_i915_request_wait_begin(rq, flags); 1893 1894 /* 1895 * We must never wait on the GPU while holding a lock as we 1896 * may need to perform a GPU reset. So while we don't need to 1897 * serialise wait/reset with an explicit lock, we do want 1898 * lockdep to detect potential dependency cycles. 1899 */ 1900 mutex_acquire(&rq->engine->gt->reset.mutex.dep_map, 0, 0, _THIS_IP_); 1901 1902 /* 1903 * Optimistic spin before touching IRQs. 1904 * 1905 * We may use a rather large value here to offset the penalty of 1906 * switching away from the active task. Frequently, the client will 1907 * wait upon an old swapbuffer to throttle itself to remain within a 1908 * frame of the gpu. If the client is running in lockstep with the gpu, 1909 * then it should not be waiting long at all, and a sleep now will incur 1910 * extra scheduler latency in producing the next frame. To try to 1911 * avoid adding the cost of enabling/disabling the interrupt to the 1912 * short wait, we first spin to see if the request would have completed 1913 * in the time taken to setup the interrupt. 1914 * 1915 * We need upto 5us to enable the irq, and upto 20us to hide the 1916 * scheduler latency of a context switch, ignoring the secondary 1917 * impacts from a context switch such as cache eviction. 1918 * 1919 * The scheme used for low-latency IO is called "hybrid interrupt 1920 * polling". The suggestion there is to sleep until just before you 1921 * expect to be woken by the device interrupt and then poll for its 1922 * completion. That requires having a good predictor for the request 1923 * duration, which we currently lack. 1924 */ 1925 if (CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT && 1926 __i915_spin_request(rq, state)) 1927 goto out; 1928 1929 /* 1930 * This client is about to stall waiting for the GPU. In many cases 1931 * this is undesirable and limits the throughput of the system, as 1932 * many clients cannot continue processing user input/output whilst 1933 * blocked. RPS autotuning may take tens of milliseconds to respond 1934 * to the GPU load and thus incurs additional latency for the client. 1935 * We can circumvent that by promoting the GPU frequency to maximum 1936 * before we sleep. This makes the GPU throttle up much more quickly 1937 * (good for benchmarks and user experience, e.g. window animations), 1938 * but at a cost of spending more power processing the workload 1939 * (bad for battery). 1940 */ 1941 if (flags & I915_WAIT_PRIORITY && !i915_request_started(rq)) 1942 intel_rps_boost(rq); 1943 1944 wait.tsk = current; 1945 if (dma_fence_add_callback(&rq->fence, &wait.cb, request_wait_wake)) 1946 goto out; 1947 1948 /* 1949 * Flush the submission tasklet, but only if it may help this request. 1950 * 1951 * We sometimes experience some latency between the HW interrupts and 1952 * tasklet execution (mostly due to ksoftirqd latency, but it can also 1953 * be due to lazy CS events), so lets run the tasklet manually if there 1954 * is a chance it may submit this request. If the request is not ready 1955 * to run, as it is waiting for other fences to be signaled, flushing 1956 * the tasklet is busy work without any advantage for this client. 1957 * 1958 * If the HW is being lazy, this is the last chance before we go to 1959 * sleep to catch any pending events. We will check periodically in 1960 * the heartbeat to flush the submission tasklets as a last resort 1961 * for unhappy HW. 1962 */ 1963 if (i915_request_is_ready(rq)) 1964 __intel_engine_flush_submission(rq->engine, false); 1965 1966 for (;;) { 1967 set_current_state(state); 1968 1969 if (dma_fence_is_signaled(&rq->fence)) 1970 break; 1971 1972 if (signal_pending_state(state, current)) { 1973 timeout = -ERESTARTSYS; 1974 break; 1975 } 1976 1977 if (!timeout) { 1978 timeout = -ETIME; 1979 break; 1980 } 1981 1982 timeout = io_schedule_timeout(timeout); 1983 } 1984 __set_current_state(TASK_RUNNING); 1985 1986 if (READ_ONCE(wait.tsk)) 1987 dma_fence_remove_callback(&rq->fence, &wait.cb); 1988 GEM_BUG_ON(!list_empty(&wait.cb.node)); 1989 1990 out: 1991 mutex_release(&rq->engine->gt->reset.mutex.dep_map, _THIS_IP_); 1992 trace_i915_request_wait_end(rq); 1993 return timeout; 1994 } 1995 1996 static int print_sched_attr(const struct i915_sched_attr *attr, 1997 char *buf, int x, int len) 1998 { 1999 if (attr->priority == I915_PRIORITY_INVALID) 2000 return x; 2001 2002 x += snprintf(buf + x, len - x, 2003 " prio=%d", attr->priority); 2004 2005 return x; 2006 } 2007 2008 static char queue_status(const struct i915_request *rq) 2009 { 2010 if (i915_request_is_active(rq)) 2011 return 'E'; 2012 2013 if (i915_request_is_ready(rq)) 2014 return intel_engine_is_virtual(rq->engine) ? 'V' : 'R'; 2015 2016 return 'U'; 2017 } 2018 2019 static const char *run_status(const struct i915_request *rq) 2020 { 2021 if (__i915_request_is_complete(rq)) 2022 return "!"; 2023 2024 if (__i915_request_has_started(rq)) 2025 return "*"; 2026 2027 if (!i915_sw_fence_signaled(&rq->semaphore)) 2028 return "&"; 2029 2030 return ""; 2031 } 2032 2033 static const char *fence_status(const struct i915_request *rq) 2034 { 2035 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags)) 2036 return "+"; 2037 2038 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags)) 2039 return "-"; 2040 2041 return ""; 2042 } 2043 2044 void i915_request_show(struct drm_printer *m, 2045 const struct i915_request *rq, 2046 const char *prefix, 2047 int indent) 2048 { 2049 const char *name = rq->fence.ops->get_timeline_name((struct dma_fence *)&rq->fence); 2050 char buf[80] = ""; 2051 int x = 0; 2052 2053 /* 2054 * The prefix is used to show the queue status, for which we use 2055 * the following flags: 2056 * 2057 * U [Unready] 2058 * - initial status upon being submitted by the user 2059 * 2060 * - the request is not ready for execution as it is waiting 2061 * for external fences 2062 * 2063 * R [Ready] 2064 * - all fences the request was waiting on have been signaled, 2065 * and the request is now ready for execution and will be 2066 * in a backend queue 2067 * 2068 * - a ready request may still need to wait on semaphores 2069 * [internal fences] 2070 * 2071 * V [Ready/virtual] 2072 * - same as ready, but queued over multiple backends 2073 * 2074 * E [Executing] 2075 * - the request has been transferred from the backend queue and 2076 * submitted for execution on HW 2077 * 2078 * - a completed request may still be regarded as executing, its 2079 * status may not be updated until it is retired and removed 2080 * from the lists 2081 */ 2082 2083 x = print_sched_attr(&rq->sched.attr, buf, x, sizeof(buf)); 2084 2085 drm_printf(m, "%s%.*s%c %llx:%lld%s%s %s @ %dms: %s\n", 2086 prefix, indent, " ", 2087 queue_status(rq), 2088 rq->fence.context, rq->fence.seqno, 2089 run_status(rq), 2090 fence_status(rq), 2091 buf, 2092 jiffies_to_msecs(jiffies - rq->emitted_jiffies), 2093 name); 2094 } 2095 2096 static bool engine_match_ring(struct intel_engine_cs *engine, struct i915_request *rq) 2097 { 2098 u32 ring = ENGINE_READ(engine, RING_START); 2099 2100 return ring == i915_ggtt_offset(rq->ring->vma); 2101 } 2102 2103 static bool match_ring(struct i915_request *rq) 2104 { 2105 struct intel_engine_cs *engine; 2106 bool found; 2107 int i; 2108 2109 if (!intel_engine_is_virtual(rq->engine)) 2110 return engine_match_ring(rq->engine, rq); 2111 2112 found = false; 2113 i = 0; 2114 while ((engine = intel_engine_get_sibling(rq->engine, i++))) { 2115 found = engine_match_ring(engine, rq); 2116 if (found) 2117 break; 2118 } 2119 2120 return found; 2121 } 2122 2123 enum i915_request_state i915_test_request_state(struct i915_request *rq) 2124 { 2125 if (i915_request_completed(rq)) 2126 return I915_REQUEST_COMPLETE; 2127 2128 if (!i915_request_started(rq)) 2129 return I915_REQUEST_PENDING; 2130 2131 if (match_ring(rq)) 2132 return I915_REQUEST_ACTIVE; 2133 2134 return I915_REQUEST_QUEUED; 2135 } 2136 2137 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 2138 #include "selftests/mock_request.c" 2139 #include "selftests/i915_request.c" 2140 #endif 2141 2142 void i915_request_module_exit(void) 2143 { 2144 kmem_cache_destroy(slab_execute_cbs); 2145 kmem_cache_destroy(slab_requests); 2146 } 2147 2148 int __init i915_request_module_init(void) 2149 { 2150 slab_requests = 2151 kmem_cache_create("i915_request", 2152 sizeof(struct i915_request), 2153 __alignof__(struct i915_request), 2154 SLAB_HWCACHE_ALIGN | 2155 SLAB_RECLAIM_ACCOUNT | 2156 SLAB_TYPESAFE_BY_RCU, 2157 __i915_request_ctor); 2158 if (!slab_requests) 2159 return -ENOMEM; 2160 2161 slab_execute_cbs = KMEM_CACHE(execute_cb, 2162 SLAB_HWCACHE_ALIGN | 2163 SLAB_RECLAIM_ACCOUNT | 2164 SLAB_TYPESAFE_BY_RCU); 2165 if (!slab_execute_cbs) 2166 goto err_requests; 2167 2168 return 0; 2169 2170 err_requests: 2171 kmem_cache_destroy(slab_requests); 2172 return -ENOMEM; 2173 } 2174