1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2008-2021 Intel Corporation 4 */ 5 6 #include <drm/drm_cache.h> 7 8 #include "gem/i915_gem_internal.h" 9 10 #include "gen2_engine_cs.h" 11 #include "gen6_engine_cs.h" 12 #include "gen6_ppgtt.h" 13 #include "gen7_renderclear.h" 14 #include "i915_drv.h" 15 #include "i915_irq.h" 16 #include "i915_mitigations.h" 17 #include "i915_reg.h" 18 #include "intel_breadcrumbs.h" 19 #include "intel_context.h" 20 #include "intel_engine_regs.h" 21 #include "intel_gt.h" 22 #include "intel_gt_irq.h" 23 #include "intel_gt_regs.h" 24 #include "intel_reset.h" 25 #include "intel_ring.h" 26 #include "shmem_utils.h" 27 #include "intel_engine_heartbeat.h" 28 #include "intel_engine_pm.h" 29 #include "intel_gt_print.h" 30 31 /* Rough estimate of the typical request size, performing a flush, 32 * set-context and then emitting the batch. 33 */ 34 #define LEGACY_REQUEST_SIZE 200 35 36 static void set_hwstam(struct intel_engine_cs *engine, u32 mask) 37 { 38 /* 39 * Keep the render interrupt unmasked as this papers over 40 * lost interrupts following a reset. 41 */ 42 if (engine->class == RENDER_CLASS) { 43 if (GRAPHICS_VER(engine->i915) >= 6) 44 mask &= ~BIT(0); 45 else 46 mask &= ~I915_USER_INTERRUPT; 47 } 48 49 intel_engine_set_hwsp_writemask(engine, mask); 50 } 51 52 static void set_hws_pga(struct intel_engine_cs *engine, phys_addr_t phys) 53 { 54 u32 addr; 55 56 addr = lower_32_bits(phys); 57 if (GRAPHICS_VER(engine->i915) >= 4) 58 addr |= (phys >> 28) & 0xf0; 59 60 intel_uncore_write(engine->uncore, HWS_PGA, addr); 61 } 62 63 static struct page *status_page(struct intel_engine_cs *engine) 64 { 65 struct drm_i915_gem_object *obj = engine->status_page.vma->obj; 66 67 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 68 return sg_page(obj->mm.pages->sgl); 69 } 70 71 static void ring_setup_phys_status_page(struct intel_engine_cs *engine) 72 { 73 set_hws_pga(engine, PFN_PHYS(page_to_pfn(status_page(engine)))); 74 set_hwstam(engine, ~0u); 75 } 76 77 static void set_hwsp(struct intel_engine_cs *engine, u32 offset) 78 { 79 i915_reg_t hwsp; 80 81 /* 82 * The ring status page addresses are no longer next to the rest of 83 * the ring registers as of gen7. 84 */ 85 if (GRAPHICS_VER(engine->i915) == 7) { 86 switch (engine->id) { 87 /* 88 * No more rings exist on Gen7. Default case is only to shut up 89 * gcc switch check warning. 90 */ 91 default: 92 GEM_BUG_ON(engine->id); 93 fallthrough; 94 case RCS0: 95 hwsp = RENDER_HWS_PGA_GEN7; 96 break; 97 case BCS0: 98 hwsp = BLT_HWS_PGA_GEN7; 99 break; 100 case VCS0: 101 hwsp = BSD_HWS_PGA_GEN7; 102 break; 103 case VECS0: 104 hwsp = VEBOX_HWS_PGA_GEN7; 105 break; 106 } 107 } else if (GRAPHICS_VER(engine->i915) == 6) { 108 hwsp = RING_HWS_PGA_GEN6(engine->mmio_base); 109 } else { 110 hwsp = RING_HWS_PGA(engine->mmio_base); 111 } 112 113 intel_uncore_write_fw(engine->uncore, hwsp, offset); 114 intel_uncore_posting_read_fw(engine->uncore, hwsp); 115 } 116 117 static void flush_cs_tlb(struct intel_engine_cs *engine) 118 { 119 if (!IS_GRAPHICS_VER(engine->i915, 6, 7)) 120 return; 121 122 /* ring should be idle before issuing a sync flush*/ 123 if ((ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0) 124 drm_warn(&engine->i915->drm, "%s not idle before sync flush!\n", 125 engine->name); 126 127 ENGINE_WRITE_FW(engine, RING_INSTPM, 128 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE | 129 INSTPM_SYNC_FLUSH)); 130 if (__intel_wait_for_register_fw(engine->uncore, 131 RING_INSTPM(engine->mmio_base), 132 INSTPM_SYNC_FLUSH, 0, 133 2000, 0, NULL)) 134 ENGINE_TRACE(engine, 135 "wait for SyncFlush to complete for TLB invalidation timed out\n"); 136 } 137 138 static void ring_setup_status_page(struct intel_engine_cs *engine) 139 { 140 set_hwsp(engine, i915_ggtt_offset(engine->status_page.vma)); 141 set_hwstam(engine, ~0u); 142 143 flush_cs_tlb(engine); 144 } 145 146 static struct i915_address_space *vm_alias(struct i915_address_space *vm) 147 { 148 if (i915_is_ggtt(vm)) 149 vm = &i915_vm_to_ggtt(vm)->alias->vm; 150 151 return vm; 152 } 153 154 static u32 pp_dir(struct i915_address_space *vm) 155 { 156 return to_gen6_ppgtt(i915_vm_to_ppgtt(vm))->pp_dir; 157 } 158 159 static void set_pp_dir(struct intel_engine_cs *engine) 160 { 161 struct i915_address_space *vm = vm_alias(engine->gt->vm); 162 163 if (!vm) 164 return; 165 166 ENGINE_WRITE_FW(engine, RING_PP_DIR_DCLV, PP_DIR_DCLV_2G); 167 ENGINE_WRITE_FW(engine, RING_PP_DIR_BASE, pp_dir(vm)); 168 169 if (GRAPHICS_VER(engine->i915) >= 7) { 170 ENGINE_WRITE_FW(engine, 171 RING_MODE_GEN7, 172 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); 173 } 174 } 175 176 static bool stop_ring(struct intel_engine_cs *engine) 177 { 178 /* Empty the ring by skipping to the end */ 179 ENGINE_WRITE_FW(engine, RING_HEAD, ENGINE_READ_FW(engine, RING_TAIL)); 180 ENGINE_POSTING_READ(engine, RING_HEAD); 181 182 /* The ring must be empty before it is disabled */ 183 ENGINE_WRITE_FW(engine, RING_CTL, 0); 184 ENGINE_POSTING_READ(engine, RING_CTL); 185 186 /* Then reset the disabled ring */ 187 ENGINE_WRITE_FW(engine, RING_HEAD, 0); 188 ENGINE_WRITE_FW(engine, RING_TAIL, 0); 189 190 return (ENGINE_READ_FW(engine, RING_HEAD) & HEAD_ADDR) == 0; 191 } 192 193 static int xcs_resume(struct intel_engine_cs *engine) 194 { 195 struct intel_ring *ring = engine->legacy.ring; 196 ktime_t kt; 197 198 ENGINE_TRACE(engine, "ring:{HEAD:%04x, TAIL:%04x}\n", 199 ring->head, ring->tail); 200 201 /* 202 * Double check the ring is empty & disabled before we resume. Called 203 * from atomic context during PCI probe, so _hardirq(). 204 */ 205 intel_synchronize_hardirq(engine->i915); 206 if (!stop_ring(engine)) 207 goto err; 208 209 if (HWS_NEEDS_PHYSICAL(engine->i915)) 210 ring_setup_phys_status_page(engine); 211 else 212 ring_setup_status_page(engine); 213 214 intel_breadcrumbs_reset(engine->breadcrumbs); 215 216 /* Enforce ordering by reading HEAD register back */ 217 ENGINE_POSTING_READ(engine, RING_HEAD); 218 219 /* 220 * Initialize the ring. This must happen _after_ we've cleared the ring 221 * registers with the above sequence (the readback of the HEAD registers 222 * also enforces ordering), otherwise the hw might lose the new ring 223 * register values. 224 */ 225 ENGINE_WRITE_FW(engine, RING_START, i915_ggtt_offset(ring->vma)); 226 227 /* Check that the ring offsets point within the ring! */ 228 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->head)); 229 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->tail)); 230 intel_ring_update_space(ring); 231 232 set_pp_dir(engine); 233 234 /* 235 * First wake the ring up to an empty/idle ring. 236 * Use 50ms of delay to let the engine write successfully 237 * for all platforms. Experimented with different values and 238 * determined that 50ms works best based on testing. 239 */ 240 for ((kt) = ktime_get() + (50 * NSEC_PER_MSEC); 241 ktime_before(ktime_get(), (kt)); cpu_relax()) { 242 /* 243 * In case of resets fails because engine resumes from 244 * incorrect RING_HEAD and then GPU may be then fed 245 * to invalid instructions, which may lead to unrecoverable 246 * hang. So at first write doesn't succeed then try again. 247 */ 248 ENGINE_WRITE_FW(engine, RING_HEAD, ring->head); 249 if (ENGINE_READ_FW(engine, RING_HEAD) == ring->head) 250 break; 251 } 252 253 ENGINE_WRITE_FW(engine, RING_TAIL, ring->head); 254 if (ENGINE_READ_FW(engine, RING_HEAD) != ENGINE_READ_FW(engine, RING_TAIL)) { 255 ENGINE_TRACE(engine, "failed to reset empty ring: [%x, %x]: %x\n", 256 ENGINE_READ_FW(engine, RING_HEAD), 257 ENGINE_READ_FW(engine, RING_TAIL), 258 ring->head); 259 goto err; 260 } 261 262 ENGINE_WRITE_FW(engine, RING_CTL, 263 RING_CTL_SIZE(ring->size) | RING_VALID); 264 265 /* If the head is still not zero, the ring is dead */ 266 if (__intel_wait_for_register_fw(engine->uncore, 267 RING_CTL(engine->mmio_base), 268 RING_VALID, RING_VALID, 269 5000, 0, NULL)) { 270 ENGINE_TRACE(engine, "failed to restart\n"); 271 goto err; 272 } 273 274 if (GRAPHICS_VER(engine->i915) > 2) { 275 ENGINE_WRITE_FW(engine, 276 RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING)); 277 ENGINE_POSTING_READ(engine, RING_MI_MODE); 278 } 279 280 /* Now awake, let it get started */ 281 if (ring->tail != ring->head) { 282 ENGINE_WRITE_FW(engine, RING_TAIL, ring->tail); 283 ENGINE_POSTING_READ(engine, RING_TAIL); 284 } 285 286 /* Papering over lost _interrupts_ immediately following the restart */ 287 intel_engine_signal_breadcrumbs(engine); 288 return 0; 289 290 err: 291 gt_err(engine->gt, "%s initialization failed\n", engine->name); 292 ENGINE_TRACE(engine, 293 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n", 294 ENGINE_READ(engine, RING_CTL), 295 ENGINE_READ(engine, RING_CTL) & RING_VALID, 296 ENGINE_READ(engine, RING_HEAD), ring->head, 297 ENGINE_READ(engine, RING_TAIL), ring->tail, 298 ENGINE_READ(engine, RING_START), 299 i915_ggtt_offset(ring->vma)); 300 GEM_TRACE_DUMP(); 301 return -EIO; 302 } 303 304 static void sanitize_hwsp(struct intel_engine_cs *engine) 305 { 306 struct intel_timeline *tl; 307 308 list_for_each_entry(tl, &engine->status_page.timelines, engine_link) 309 intel_timeline_reset_seqno(tl); 310 } 311 312 static void xcs_sanitize(struct intel_engine_cs *engine) 313 { 314 /* 315 * Poison residual state on resume, in case the suspend didn't! 316 * 317 * We have to assume that across suspend/resume (or other loss 318 * of control) that the contents of our pinned buffers has been 319 * lost, replaced by garbage. Since this doesn't always happen, 320 * let's poison such state so that we more quickly spot when 321 * we falsely assume it has been preserved. 322 */ 323 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) 324 memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE); 325 326 /* 327 * The kernel_context HWSP is stored in the status_page. As above, 328 * that may be lost on resume/initialisation, and so we need to 329 * reset the value in the HWSP. 330 */ 331 sanitize_hwsp(engine); 332 333 /* And scrub the dirty cachelines for the HWSP */ 334 drm_clflush_virt_range(engine->status_page.addr, PAGE_SIZE); 335 336 intel_engine_reset_pinned_contexts(engine); 337 } 338 339 static void reset_prepare(struct intel_engine_cs *engine) 340 { 341 /* 342 * We stop engines, otherwise we might get failed reset and a 343 * dead gpu (on elk). Also as modern gpu as kbl can suffer 344 * from system hang if batchbuffer is progressing when 345 * the reset is issued, regardless of READY_TO_RESET ack. 346 * Thus assume it is best to stop engines on all gens 347 * where we have a gpu reset. 348 * 349 * WaKBLVECSSemaphoreWaitPoll:kbl (on ALL_ENGINES) 350 * 351 * WaMediaResetMainRingCleanup:ctg,elk (presumably) 352 * WaClearRingBufHeadRegAtInit:ctg,elk 353 * 354 * FIXME: Wa for more modern gens needs to be validated 355 */ 356 ENGINE_TRACE(engine, "\n"); 357 intel_engine_stop_cs(engine); 358 359 if (!stop_ring(engine)) { 360 /* G45 ring initialization often fails to reset head to zero */ 361 ENGINE_TRACE(engine, 362 "HEAD not reset to zero, " 363 "{ CTL:%08x, HEAD:%08x, TAIL:%08x, START:%08x }\n", 364 ENGINE_READ_FW(engine, RING_CTL), 365 ENGINE_READ_FW(engine, RING_HEAD), 366 ENGINE_READ_FW(engine, RING_TAIL), 367 ENGINE_READ_FW(engine, RING_START)); 368 /* 369 * Sometimes engine head failed to set to zero even after writing into it. 370 * Use wait_for_atomic() with 20ms delay to let engine resumes from 371 * correct RING_HEAD. Experimented different values and determined 372 * that 20ms works best based on testing. 373 */ 374 if (wait_for_atomic((!stop_ring(engine) == 0), 20)) { 375 drm_err(&engine->i915->drm, 376 "failed to set %s head to zero " 377 "ctl %08x head %08x tail %08x start %08x\n", 378 engine->name, 379 ENGINE_READ_FW(engine, RING_CTL), 380 ENGINE_READ_FW(engine, RING_HEAD), 381 ENGINE_READ_FW(engine, RING_TAIL), 382 ENGINE_READ_FW(engine, RING_START)); 383 } 384 } 385 } 386 387 static void reset_rewind(struct intel_engine_cs *engine, bool stalled) 388 { 389 struct i915_request *pos, *rq; 390 unsigned long flags; 391 u32 head; 392 393 rq = NULL; 394 spin_lock_irqsave(&engine->sched_engine->lock, flags); 395 rcu_read_lock(); 396 list_for_each_entry(pos, &engine->sched_engine->requests, sched.link) { 397 if (!__i915_request_is_complete(pos)) { 398 rq = pos; 399 break; 400 } 401 } 402 rcu_read_unlock(); 403 404 /* 405 * The guilty request will get skipped on a hung engine. 406 * 407 * Users of client default contexts do not rely on logical 408 * state preserved between batches so it is safe to execute 409 * queued requests following the hang. Non default contexts 410 * rely on preserved state, so skipping a batch loses the 411 * evolution of the state and it needs to be considered corrupted. 412 * Executing more queued batches on top of corrupted state is 413 * risky. But we take the risk by trying to advance through 414 * the queued requests in order to make the client behaviour 415 * more predictable around resets, by not throwing away random 416 * amount of batches it has prepared for execution. Sophisticated 417 * clients can use gem_reset_stats_ioctl and dma fence status 418 * (exported via sync_file info ioctl on explicit fences) to observe 419 * when it loses the context state and should rebuild accordingly. 420 * 421 * The context ban, and ultimately the client ban, mechanism are safety 422 * valves if client submission ends up resulting in nothing more than 423 * subsequent hangs. 424 */ 425 426 if (rq) { 427 /* 428 * Try to restore the logical GPU state to match the 429 * continuation of the request queue. If we skip the 430 * context/PD restore, then the next request may try to execute 431 * assuming that its context is valid and loaded on the GPU and 432 * so may try to access invalid memory, prompting repeated GPU 433 * hangs. 434 * 435 * If the request was guilty, we still restore the logical 436 * state in case the next request requires it (e.g. the 437 * aliasing ppgtt), but skip over the hung batch. 438 * 439 * If the request was innocent, we try to replay the request 440 * with the restored context. 441 */ 442 __i915_request_reset(rq, stalled); 443 444 GEM_BUG_ON(rq->ring != engine->legacy.ring); 445 head = rq->head; 446 } else { 447 head = engine->legacy.ring->tail; 448 } 449 engine->legacy.ring->head = intel_ring_wrap(engine->legacy.ring, head); 450 451 spin_unlock_irqrestore(&engine->sched_engine->lock, flags); 452 } 453 454 static void reset_finish(struct intel_engine_cs *engine) 455 { 456 } 457 458 static void reset_cancel(struct intel_engine_cs *engine) 459 { 460 struct i915_request *request; 461 unsigned long flags; 462 463 spin_lock_irqsave(&engine->sched_engine->lock, flags); 464 465 /* Mark all submitted requests as skipped. */ 466 list_for_each_entry(request, &engine->sched_engine->requests, sched.link) 467 i915_request_put(i915_request_mark_eio(request)); 468 intel_engine_signal_breadcrumbs(engine); 469 470 /* Remaining _unready_ requests will be nop'ed when submitted */ 471 472 spin_unlock_irqrestore(&engine->sched_engine->lock, flags); 473 } 474 475 static void i9xx_submit_request(struct i915_request *request) 476 { 477 i915_request_submit(request); 478 wmb(); /* paranoid flush writes out of the WCB before mmio */ 479 480 ENGINE_WRITE(request->engine, RING_TAIL, 481 intel_ring_set_tail(request->ring, request->tail)); 482 } 483 484 static void __ring_context_fini(struct intel_context *ce) 485 { 486 i915_vma_put(ce->state); 487 } 488 489 static void ring_context_destroy(struct kref *ref) 490 { 491 struct intel_context *ce = container_of(ref, typeof(*ce), ref); 492 493 GEM_BUG_ON(intel_context_is_pinned(ce)); 494 495 if (ce->state) 496 __ring_context_fini(ce); 497 498 intel_context_fini(ce); 499 intel_context_free(ce); 500 } 501 502 static int ring_context_init_default_state(struct intel_context *ce, 503 struct i915_gem_ww_ctx *ww) 504 { 505 struct drm_i915_gem_object *obj = ce->state->obj; 506 void *vaddr; 507 508 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB); 509 if (IS_ERR(vaddr)) 510 return PTR_ERR(vaddr); 511 512 shmem_read(ce->default_state, 0, vaddr, ce->engine->context_size); 513 514 i915_gem_object_flush_map(obj); 515 __i915_gem_object_release_map(obj); 516 517 __set_bit(CONTEXT_VALID_BIT, &ce->flags); 518 return 0; 519 } 520 521 static int ring_context_pre_pin(struct intel_context *ce, 522 struct i915_gem_ww_ctx *ww, 523 void **unused) 524 { 525 struct i915_address_space *vm; 526 int err = 0; 527 528 if (ce->default_state && 529 !test_bit(CONTEXT_VALID_BIT, &ce->flags)) { 530 err = ring_context_init_default_state(ce, ww); 531 if (err) 532 return err; 533 } 534 535 vm = vm_alias(ce->vm); 536 if (vm) 537 err = gen6_ppgtt_pin(i915_vm_to_ppgtt((vm)), ww); 538 539 return err; 540 } 541 542 static void __context_unpin_ppgtt(struct intel_context *ce) 543 { 544 struct i915_address_space *vm; 545 546 vm = vm_alias(ce->vm); 547 if (vm) 548 gen6_ppgtt_unpin(i915_vm_to_ppgtt(vm)); 549 } 550 551 static void ring_context_unpin(struct intel_context *ce) 552 { 553 } 554 555 static void ring_context_post_unpin(struct intel_context *ce) 556 { 557 __context_unpin_ppgtt(ce); 558 } 559 560 static struct i915_vma * 561 alloc_context_vma(struct intel_engine_cs *engine) 562 { 563 struct drm_i915_private *i915 = engine->i915; 564 struct drm_i915_gem_object *obj; 565 struct i915_vma *vma; 566 int err; 567 568 obj = i915_gem_object_create_shmem(i915, engine->context_size); 569 if (IS_ERR(obj)) 570 return ERR_CAST(obj); 571 572 /* 573 * Try to make the context utilize L3 as well as LLC. 574 * 575 * On VLV we don't have L3 controls in the PTEs so we 576 * shouldn't touch the cache level, especially as that 577 * would make the object snooped which might have a 578 * negative performance impact. 579 * 580 * Snooping is required on non-llc platforms in execlist 581 * mode, but since all GGTT accesses use PAT entry 0 we 582 * get snooping anyway regardless of cache_level. 583 * 584 * This is only applicable for Ivy Bridge devices since 585 * later platforms don't have L3 control bits in the PTE. 586 */ 587 if (IS_IVYBRIDGE(i915)) 588 i915_gem_object_set_cache_coherency(obj, I915_CACHE_L3_LLC); 589 590 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL); 591 if (IS_ERR(vma)) { 592 err = PTR_ERR(vma); 593 goto err_obj; 594 } 595 596 return vma; 597 598 err_obj: 599 i915_gem_object_put(obj); 600 return ERR_PTR(err); 601 } 602 603 static int ring_context_alloc(struct intel_context *ce) 604 { 605 struct intel_engine_cs *engine = ce->engine; 606 607 if (!intel_context_has_own_state(ce)) 608 ce->default_state = engine->default_state; 609 610 /* One ringbuffer to rule them all */ 611 GEM_BUG_ON(!engine->legacy.ring); 612 ce->ring = engine->legacy.ring; 613 ce->timeline = intel_timeline_get(engine->legacy.timeline); 614 615 GEM_BUG_ON(ce->state); 616 if (engine->context_size) { 617 struct i915_vma *vma; 618 619 vma = alloc_context_vma(engine); 620 if (IS_ERR(vma)) 621 return PTR_ERR(vma); 622 623 ce->state = vma; 624 } 625 626 return 0; 627 } 628 629 static int ring_context_pin(struct intel_context *ce, void *unused) 630 { 631 return 0; 632 } 633 634 static void ring_context_reset(struct intel_context *ce) 635 { 636 intel_ring_reset(ce->ring, ce->ring->emit); 637 clear_bit(CONTEXT_VALID_BIT, &ce->flags); 638 } 639 640 static void ring_context_revoke(struct intel_context *ce, 641 struct i915_request *rq, 642 unsigned int preempt_timeout_ms) 643 { 644 struct intel_engine_cs *engine; 645 646 if (!rq || !i915_request_is_active(rq)) 647 return; 648 649 engine = rq->engine; 650 lockdep_assert_held(&engine->sched_engine->lock); 651 list_for_each_entry_continue(rq, &engine->sched_engine->requests, 652 sched.link) 653 if (rq->context == ce) { 654 i915_request_set_error_once(rq, -EIO); 655 __i915_request_skip(rq); 656 } 657 } 658 659 static void ring_context_cancel_request(struct intel_context *ce, 660 struct i915_request *rq) 661 { 662 struct intel_engine_cs *engine = NULL; 663 664 i915_request_active_engine(rq, &engine); 665 666 if (engine && intel_engine_pulse(engine)) 667 intel_gt_handle_error(engine->gt, engine->mask, 0, 668 "request cancellation by %s", 669 current->comm); 670 } 671 672 static const struct intel_context_ops ring_context_ops = { 673 .alloc = ring_context_alloc, 674 675 .cancel_request = ring_context_cancel_request, 676 677 .revoke = ring_context_revoke, 678 679 .pre_pin = ring_context_pre_pin, 680 .pin = ring_context_pin, 681 .unpin = ring_context_unpin, 682 .post_unpin = ring_context_post_unpin, 683 684 .enter = intel_context_enter_engine, 685 .exit = intel_context_exit_engine, 686 687 .reset = ring_context_reset, 688 .destroy = ring_context_destroy, 689 }; 690 691 static int load_pd_dir(struct i915_request *rq, 692 struct i915_address_space *vm, 693 u32 valid) 694 { 695 const struct intel_engine_cs * const engine = rq->engine; 696 u32 *cs; 697 698 cs = intel_ring_begin(rq, 12); 699 if (IS_ERR(cs)) 700 return PTR_ERR(cs); 701 702 *cs++ = MI_LOAD_REGISTER_IMM(1); 703 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine->mmio_base)); 704 *cs++ = valid; 705 706 *cs++ = MI_LOAD_REGISTER_IMM(1); 707 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base)); 708 *cs++ = pp_dir(vm); 709 710 /* Stall until the page table load is complete? */ 711 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT; 712 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base)); 713 *cs++ = intel_gt_scratch_offset(engine->gt, 714 INTEL_GT_SCRATCH_FIELD_DEFAULT); 715 716 *cs++ = MI_LOAD_REGISTER_IMM(1); 717 *cs++ = i915_mmio_reg_offset(RING_INSTPM(engine->mmio_base)); 718 *cs++ = _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE); 719 720 intel_ring_advance(rq, cs); 721 722 return rq->engine->emit_flush(rq, EMIT_FLUSH); 723 } 724 725 static int mi_set_context(struct i915_request *rq, 726 struct intel_context *ce, 727 u32 flags) 728 { 729 struct intel_engine_cs *engine = rq->engine; 730 struct drm_i915_private *i915 = engine->i915; 731 enum intel_engine_id id; 732 const int num_engines = 733 IS_HASWELL(i915) ? engine->gt->info.num_engines - 1 : 0; 734 bool force_restore = false; 735 int len; 736 u32 *cs; 737 738 len = 4; 739 if (GRAPHICS_VER(i915) == 7) 740 len += 2 + (num_engines ? 4 * num_engines + 6 : 0); 741 else if (GRAPHICS_VER(i915) == 5) 742 len += 2; 743 if (flags & MI_FORCE_RESTORE) { 744 GEM_BUG_ON(flags & MI_RESTORE_INHIBIT); 745 flags &= ~MI_FORCE_RESTORE; 746 force_restore = true; 747 len += 2; 748 } 749 750 cs = intel_ring_begin(rq, len); 751 if (IS_ERR(cs)) 752 return PTR_ERR(cs); 753 754 /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */ 755 if (GRAPHICS_VER(i915) == 7) { 756 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE; 757 if (num_engines) { 758 struct intel_engine_cs *signaller; 759 760 *cs++ = MI_LOAD_REGISTER_IMM(num_engines); 761 for_each_engine(signaller, engine->gt, id) { 762 if (signaller == engine) 763 continue; 764 765 *cs++ = i915_mmio_reg_offset( 766 RING_PSMI_CTL(signaller->mmio_base)); 767 *cs++ = _MASKED_BIT_ENABLE( 768 GEN6_PSMI_SLEEP_MSG_DISABLE); 769 } 770 } 771 } else if (GRAPHICS_VER(i915) == 5) { 772 /* 773 * This w/a is only listed for pre-production ilk a/b steppings, 774 * but is also mentioned for programming the powerctx. To be 775 * safe, just apply the workaround; we do not use SyncFlush so 776 * this should never take effect and so be a no-op! 777 */ 778 *cs++ = MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN; 779 } 780 781 if (force_restore) { 782 /* 783 * The HW doesn't handle being told to restore the current 784 * context very well. Quite often it likes goes to go off and 785 * sulk, especially when it is meant to be reloading PP_DIR. 786 * A very simple fix to force the reload is to simply switch 787 * away from the current context and back again. 788 * 789 * Note that the kernel_context will contain random state 790 * following the INHIBIT_RESTORE. We accept this since we 791 * never use the kernel_context state; it is merely a 792 * placeholder we use to flush other contexts. 793 */ 794 *cs++ = MI_SET_CONTEXT; 795 *cs++ = i915_ggtt_offset(engine->kernel_context->state) | 796 MI_MM_SPACE_GTT | 797 MI_RESTORE_INHIBIT; 798 } 799 800 *cs++ = MI_NOOP; 801 *cs++ = MI_SET_CONTEXT; 802 *cs++ = i915_ggtt_offset(ce->state) | flags; 803 /* 804 * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP 805 * WaMiSetContext_Hang:snb,ivb,vlv 806 */ 807 *cs++ = MI_NOOP; 808 809 if (GRAPHICS_VER(i915) == 7) { 810 if (num_engines) { 811 struct intel_engine_cs *signaller; 812 i915_reg_t last_reg = INVALID_MMIO_REG; /* keep gcc quiet */ 813 814 *cs++ = MI_LOAD_REGISTER_IMM(num_engines); 815 for_each_engine(signaller, engine->gt, id) { 816 if (signaller == engine) 817 continue; 818 819 last_reg = RING_PSMI_CTL(signaller->mmio_base); 820 *cs++ = i915_mmio_reg_offset(last_reg); 821 *cs++ = _MASKED_BIT_DISABLE( 822 GEN6_PSMI_SLEEP_MSG_DISABLE); 823 } 824 825 /* Insert a delay before the next switch! */ 826 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT; 827 *cs++ = i915_mmio_reg_offset(last_reg); 828 *cs++ = intel_gt_scratch_offset(engine->gt, 829 INTEL_GT_SCRATCH_FIELD_DEFAULT); 830 *cs++ = MI_NOOP; 831 } 832 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; 833 } else if (GRAPHICS_VER(i915) == 5) { 834 *cs++ = MI_SUSPEND_FLUSH; 835 } 836 837 intel_ring_advance(rq, cs); 838 839 return 0; 840 } 841 842 static int remap_l3_slice(struct i915_request *rq, int slice) 843 { 844 #define L3LOG_DW (GEN7_L3LOG_SIZE / sizeof(u32)) 845 u32 *cs, *remap_info = rq->i915->l3_parity.remap_info[slice]; 846 int i; 847 848 if (!remap_info) 849 return 0; 850 851 cs = intel_ring_begin(rq, L3LOG_DW * 2 + 2); 852 if (IS_ERR(cs)) 853 return PTR_ERR(cs); 854 855 /* 856 * Note: We do not worry about the concurrent register cacheline hang 857 * here because no other code should access these registers other than 858 * at initialization time. 859 */ 860 *cs++ = MI_LOAD_REGISTER_IMM(L3LOG_DW); 861 for (i = 0; i < L3LOG_DW; i++) { 862 *cs++ = i915_mmio_reg_offset(GEN7_L3LOG(slice, i)); 863 *cs++ = remap_info[i]; 864 } 865 *cs++ = MI_NOOP; 866 intel_ring_advance(rq, cs); 867 868 return 0; 869 #undef L3LOG_DW 870 } 871 872 static int remap_l3(struct i915_request *rq) 873 { 874 struct i915_gem_context *ctx = i915_request_gem_context(rq); 875 int i, err; 876 877 if (!ctx || !ctx->remap_slice) 878 return 0; 879 880 for (i = 0; i < MAX_L3_SLICES; i++) { 881 if (!(ctx->remap_slice & BIT(i))) 882 continue; 883 884 err = remap_l3_slice(rq, i); 885 if (err) 886 return err; 887 } 888 889 ctx->remap_slice = 0; 890 return 0; 891 } 892 893 static int switch_mm(struct i915_request *rq, struct i915_address_space *vm) 894 { 895 int ret; 896 897 if (!vm) 898 return 0; 899 900 ret = rq->engine->emit_flush(rq, EMIT_FLUSH); 901 if (ret) 902 return ret; 903 904 /* 905 * Not only do we need a full barrier (post-sync write) after 906 * invalidating the TLBs, but we need to wait a little bit 907 * longer. Whether this is merely delaying us, or the 908 * subsequent flush is a key part of serialising with the 909 * post-sync op, this extra pass appears vital before a 910 * mm switch! 911 */ 912 ret = load_pd_dir(rq, vm, PP_DIR_DCLV_2G); 913 if (ret) 914 return ret; 915 916 return rq->engine->emit_flush(rq, EMIT_INVALIDATE); 917 } 918 919 static int clear_residuals(struct i915_request *rq) 920 { 921 struct intel_engine_cs *engine = rq->engine; 922 int ret; 923 924 ret = switch_mm(rq, vm_alias(engine->kernel_context->vm)); 925 if (ret) 926 return ret; 927 928 if (engine->kernel_context->state) { 929 ret = mi_set_context(rq, 930 engine->kernel_context, 931 MI_MM_SPACE_GTT | MI_RESTORE_INHIBIT); 932 if (ret) 933 return ret; 934 } 935 936 ret = engine->emit_bb_start(rq, 937 i915_vma_offset(engine->wa_ctx.vma), 0, 938 0); 939 if (ret) 940 return ret; 941 942 ret = engine->emit_flush(rq, EMIT_FLUSH); 943 if (ret) 944 return ret; 945 946 /* Always invalidate before the next switch_mm() */ 947 return engine->emit_flush(rq, EMIT_INVALIDATE); 948 } 949 950 static int switch_context(struct i915_request *rq) 951 { 952 struct intel_engine_cs *engine = rq->engine; 953 struct intel_context *ce = rq->context; 954 void **residuals = NULL; 955 int ret; 956 957 GEM_BUG_ON(HAS_EXECLISTS(engine->i915)); 958 959 if (engine->wa_ctx.vma && ce != engine->kernel_context) { 960 if (engine->wa_ctx.vma->private != ce && 961 i915_mitigate_clear_residuals()) { 962 ret = clear_residuals(rq); 963 if (ret) 964 return ret; 965 966 residuals = &engine->wa_ctx.vma->private; 967 } 968 } 969 970 ret = switch_mm(rq, vm_alias(ce->vm)); 971 if (ret) 972 return ret; 973 974 if (ce->state) { 975 u32 flags; 976 977 GEM_BUG_ON(engine->id != RCS0); 978 979 /* For resource streamer on HSW+ and power context elsewhere */ 980 BUILD_BUG_ON(HSW_MI_RS_SAVE_STATE_EN != MI_SAVE_EXT_STATE_EN); 981 BUILD_BUG_ON(HSW_MI_RS_RESTORE_STATE_EN != MI_RESTORE_EXT_STATE_EN); 982 983 flags = MI_SAVE_EXT_STATE_EN | MI_MM_SPACE_GTT; 984 if (test_bit(CONTEXT_VALID_BIT, &ce->flags)) 985 flags |= MI_RESTORE_EXT_STATE_EN; 986 else 987 flags |= MI_RESTORE_INHIBIT; 988 989 ret = mi_set_context(rq, ce, flags); 990 if (ret) 991 return ret; 992 } 993 994 ret = remap_l3(rq); 995 if (ret) 996 return ret; 997 998 /* 999 * Now past the point of no return, this request _will_ be emitted. 1000 * 1001 * Or at least this preamble will be emitted, the request may be 1002 * interrupted prior to submitting the user payload. If so, we 1003 * still submit the "empty" request in order to preserve global 1004 * state tracking such as this, our tracking of the current 1005 * dirty context. 1006 */ 1007 if (residuals) { 1008 intel_context_put(*residuals); 1009 *residuals = intel_context_get(ce); 1010 } 1011 1012 return 0; 1013 } 1014 1015 static int ring_request_alloc(struct i915_request *request) 1016 { 1017 int ret; 1018 1019 GEM_BUG_ON(!intel_context_is_pinned(request->context)); 1020 GEM_BUG_ON(i915_request_timeline(request)->has_initial_breadcrumb); 1021 1022 /* 1023 * Flush enough space to reduce the likelihood of waiting after 1024 * we start building the request - in which case we will just 1025 * have to repeat work. 1026 */ 1027 request->reserved_space += LEGACY_REQUEST_SIZE; 1028 1029 /* Unconditionally invalidate GPU caches and TLBs. */ 1030 ret = request->engine->emit_flush(request, EMIT_INVALIDATE); 1031 if (ret) 1032 return ret; 1033 1034 ret = switch_context(request); 1035 if (ret) 1036 return ret; 1037 1038 request->reserved_space -= LEGACY_REQUEST_SIZE; 1039 return 0; 1040 } 1041 1042 static void gen6_bsd_submit_request(struct i915_request *request) 1043 { 1044 struct intel_uncore *uncore = request->engine->uncore; 1045 1046 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL); 1047 1048 /* Every tail move must follow the sequence below */ 1049 1050 /* Disable notification that the ring is IDLE. The GT 1051 * will then assume that it is busy and bring it out of rc6. 1052 */ 1053 intel_uncore_write_fw(uncore, RING_PSMI_CTL(GEN6_BSD_RING_BASE), 1054 _MASKED_BIT_ENABLE(GEN6_PSMI_SLEEP_MSG_DISABLE)); 1055 1056 /* Clear the context id. Here be magic! */ 1057 intel_uncore_write64_fw(uncore, GEN6_BSD_RNCID, 0x0); 1058 1059 /* Wait for the ring not to be idle, i.e. for it to wake up. */ 1060 if (__intel_wait_for_register_fw(uncore, 1061 RING_PSMI_CTL(GEN6_BSD_RING_BASE), 1062 GEN6_BSD_SLEEP_INDICATOR, 1063 0, 1064 1000, 0, NULL)) 1065 drm_err(&uncore->i915->drm, 1066 "timed out waiting for the BSD ring to wake up\n"); 1067 1068 /* Now that the ring is fully powered up, update the tail */ 1069 i9xx_submit_request(request); 1070 1071 /* Let the ring send IDLE messages to the GT again, 1072 * and so let it sleep to conserve power when idle. 1073 */ 1074 intel_uncore_write_fw(uncore, RING_PSMI_CTL(GEN6_BSD_RING_BASE), 1075 _MASKED_BIT_DISABLE(GEN6_PSMI_SLEEP_MSG_DISABLE)); 1076 1077 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL); 1078 } 1079 1080 static void i9xx_set_default_submission(struct intel_engine_cs *engine) 1081 { 1082 engine->submit_request = i9xx_submit_request; 1083 } 1084 1085 static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine) 1086 { 1087 engine->submit_request = gen6_bsd_submit_request; 1088 } 1089 1090 static void ring_release(struct intel_engine_cs *engine) 1091 { 1092 struct drm_i915_private *i915 = engine->i915; 1093 1094 drm_WARN_ON(&i915->drm, GRAPHICS_VER(i915) > 2 && 1095 (ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0); 1096 1097 intel_engine_cleanup_common(engine); 1098 1099 if (engine->wa_ctx.vma) { 1100 intel_context_put(engine->wa_ctx.vma->private); 1101 i915_vma_unpin_and_release(&engine->wa_ctx.vma, 0); 1102 } 1103 1104 intel_ring_unpin(engine->legacy.ring); 1105 intel_ring_put(engine->legacy.ring); 1106 1107 intel_timeline_unpin(engine->legacy.timeline); 1108 intel_timeline_put(engine->legacy.timeline); 1109 } 1110 1111 static void irq_handler(struct intel_engine_cs *engine, u16 iir) 1112 { 1113 intel_engine_signal_breadcrumbs(engine); 1114 } 1115 1116 static void setup_irq(struct intel_engine_cs *engine) 1117 { 1118 struct drm_i915_private *i915 = engine->i915; 1119 1120 intel_engine_set_irq_handler(engine, irq_handler); 1121 1122 if (GRAPHICS_VER(i915) >= 6) { 1123 engine->irq_enable = gen6_irq_enable; 1124 engine->irq_disable = gen6_irq_disable; 1125 } else if (GRAPHICS_VER(i915) >= 5) { 1126 engine->irq_enable = gen5_irq_enable; 1127 engine->irq_disable = gen5_irq_disable; 1128 } else { 1129 engine->irq_enable = gen2_irq_enable; 1130 engine->irq_disable = gen2_irq_disable; 1131 } 1132 } 1133 1134 static void add_to_engine(struct i915_request *rq) 1135 { 1136 lockdep_assert_held(&rq->engine->sched_engine->lock); 1137 list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests); 1138 } 1139 1140 static void remove_from_engine(struct i915_request *rq) 1141 { 1142 spin_lock_irq(&rq->engine->sched_engine->lock); 1143 list_del_init(&rq->sched.link); 1144 1145 /* Prevent further __await_execution() registering a cb, then flush */ 1146 set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags); 1147 1148 spin_unlock_irq(&rq->engine->sched_engine->lock); 1149 1150 i915_request_notify_execute_cb_imm(rq); 1151 } 1152 1153 static void setup_common(struct intel_engine_cs *engine) 1154 { 1155 struct drm_i915_private *i915 = engine->i915; 1156 1157 /* gen8+ are only supported with execlists */ 1158 GEM_BUG_ON(GRAPHICS_VER(i915) >= 8); 1159 1160 setup_irq(engine); 1161 1162 engine->resume = xcs_resume; 1163 engine->sanitize = xcs_sanitize; 1164 1165 engine->reset.prepare = reset_prepare; 1166 engine->reset.rewind = reset_rewind; 1167 engine->reset.cancel = reset_cancel; 1168 engine->reset.finish = reset_finish; 1169 1170 engine->add_active_request = add_to_engine; 1171 engine->remove_active_request = remove_from_engine; 1172 1173 engine->cops = &ring_context_ops; 1174 engine->request_alloc = ring_request_alloc; 1175 1176 /* 1177 * Using a global execution timeline; the previous final breadcrumb is 1178 * equivalent to our next initial bread so we can elide 1179 * engine->emit_init_breadcrumb(). 1180 */ 1181 engine->emit_fini_breadcrumb = gen2_emit_breadcrumb; 1182 if (GRAPHICS_VER(i915) == 5) 1183 engine->emit_fini_breadcrumb = gen5_emit_breadcrumb; 1184 1185 engine->set_default_submission = i9xx_set_default_submission; 1186 1187 if (GRAPHICS_VER(i915) >= 6) 1188 engine->emit_bb_start = gen6_emit_bb_start; 1189 else if (GRAPHICS_VER(i915) >= 4) 1190 engine->emit_bb_start = gen4_emit_bb_start; 1191 else if (IS_I830(i915) || IS_I845G(i915)) 1192 engine->emit_bb_start = i830_emit_bb_start; 1193 else 1194 engine->emit_bb_start = gen2_emit_bb_start; 1195 } 1196 1197 static void setup_rcs(struct intel_engine_cs *engine) 1198 { 1199 struct drm_i915_private *i915 = engine->i915; 1200 1201 if (HAS_L3_DPF(i915)) 1202 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT; 1203 1204 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT; 1205 1206 if (GRAPHICS_VER(i915) >= 7) { 1207 engine->emit_flush = gen7_emit_flush_rcs; 1208 engine->emit_fini_breadcrumb = gen7_emit_breadcrumb_rcs; 1209 } else if (GRAPHICS_VER(i915) == 6) { 1210 engine->emit_flush = gen6_emit_flush_rcs; 1211 engine->emit_fini_breadcrumb = gen6_emit_breadcrumb_rcs; 1212 } else if (GRAPHICS_VER(i915) == 5) { 1213 engine->emit_flush = gen4_emit_flush_rcs; 1214 } else { 1215 if (GRAPHICS_VER(i915) < 4) 1216 engine->emit_flush = gen2_emit_flush; 1217 else 1218 engine->emit_flush = gen4_emit_flush_rcs; 1219 engine->irq_enable_mask = I915_USER_INTERRUPT; 1220 } 1221 1222 if (IS_HASWELL(i915)) 1223 engine->emit_bb_start = hsw_emit_bb_start; 1224 } 1225 1226 static void setup_vcs(struct intel_engine_cs *engine) 1227 { 1228 struct drm_i915_private *i915 = engine->i915; 1229 1230 if (GRAPHICS_VER(i915) >= 6) { 1231 /* gen6 bsd needs a special wa for tail updates */ 1232 if (GRAPHICS_VER(i915) == 6) 1233 engine->set_default_submission = gen6_bsd_set_default_submission; 1234 engine->emit_flush = gen6_emit_flush_vcs; 1235 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT; 1236 1237 if (GRAPHICS_VER(i915) == 6) 1238 engine->emit_fini_breadcrumb = gen6_emit_breadcrumb_xcs; 1239 else 1240 engine->emit_fini_breadcrumb = gen7_emit_breadcrumb_xcs; 1241 } else { 1242 engine->emit_flush = gen4_emit_flush_vcs; 1243 if (GRAPHICS_VER(i915) == 5) 1244 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT; 1245 else 1246 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT; 1247 } 1248 } 1249 1250 static void setup_bcs(struct intel_engine_cs *engine) 1251 { 1252 struct drm_i915_private *i915 = engine->i915; 1253 1254 engine->emit_flush = gen6_emit_flush_xcs; 1255 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT; 1256 1257 if (GRAPHICS_VER(i915) == 6) 1258 engine->emit_fini_breadcrumb = gen6_emit_breadcrumb_xcs; 1259 else 1260 engine->emit_fini_breadcrumb = gen7_emit_breadcrumb_xcs; 1261 } 1262 1263 static void setup_vecs(struct intel_engine_cs *engine) 1264 { 1265 struct drm_i915_private *i915 = engine->i915; 1266 1267 GEM_BUG_ON(GRAPHICS_VER(i915) < 7); 1268 1269 engine->emit_flush = gen6_emit_flush_xcs; 1270 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT; 1271 engine->irq_enable = hsw_irq_enable_vecs; 1272 engine->irq_disable = hsw_irq_disable_vecs; 1273 1274 engine->emit_fini_breadcrumb = gen7_emit_breadcrumb_xcs; 1275 } 1276 1277 static int gen7_ctx_switch_bb_setup(struct intel_engine_cs * const engine, 1278 struct i915_vma * const vma) 1279 { 1280 return gen7_setup_clear_gpr_bb(engine, vma); 1281 } 1282 1283 static int gen7_ctx_switch_bb_init(struct intel_engine_cs *engine, 1284 struct i915_gem_ww_ctx *ww, 1285 struct i915_vma *vma) 1286 { 1287 int err; 1288 1289 err = i915_vma_pin_ww(vma, ww, 0, 0, PIN_USER | PIN_HIGH); 1290 if (err) 1291 return err; 1292 1293 err = i915_vma_sync(vma); 1294 if (err) 1295 goto err_unpin; 1296 1297 err = gen7_ctx_switch_bb_setup(engine, vma); 1298 if (err) 1299 goto err_unpin; 1300 1301 engine->wa_ctx.vma = vma; 1302 return 0; 1303 1304 err_unpin: 1305 i915_vma_unpin(vma); 1306 return err; 1307 } 1308 1309 static struct i915_vma *gen7_ctx_vma(struct intel_engine_cs *engine) 1310 { 1311 struct drm_i915_gem_object *obj; 1312 struct i915_vma *vma; 1313 int size, err; 1314 1315 if (GRAPHICS_VER(engine->i915) != 7 || engine->class != RENDER_CLASS) 1316 return NULL; 1317 1318 err = gen7_ctx_switch_bb_setup(engine, NULL /* probe size */); 1319 if (err < 0) 1320 return ERR_PTR(err); 1321 if (!err) 1322 return NULL; 1323 1324 size = ALIGN(err, PAGE_SIZE); 1325 1326 obj = i915_gem_object_create_internal(engine->i915, size); 1327 if (IS_ERR(obj)) 1328 return ERR_CAST(obj); 1329 1330 vma = i915_vma_instance(obj, engine->gt->vm, NULL); 1331 if (IS_ERR(vma)) { 1332 i915_gem_object_put(obj); 1333 return ERR_CAST(vma); 1334 } 1335 1336 vma->private = intel_context_create(engine); /* dummy residuals */ 1337 if (IS_ERR(vma->private)) { 1338 err = PTR_ERR(vma->private); 1339 vma->private = NULL; 1340 i915_gem_object_put(obj); 1341 return ERR_PTR(err); 1342 } 1343 1344 return vma; 1345 } 1346 1347 int intel_ring_submission_setup(struct intel_engine_cs *engine) 1348 { 1349 struct i915_gem_ww_ctx ww; 1350 struct intel_timeline *timeline; 1351 struct intel_ring *ring; 1352 struct i915_vma *gen7_wa_vma; 1353 int err; 1354 1355 setup_common(engine); 1356 1357 switch (engine->class) { 1358 case RENDER_CLASS: 1359 setup_rcs(engine); 1360 break; 1361 case VIDEO_DECODE_CLASS: 1362 setup_vcs(engine); 1363 break; 1364 case COPY_ENGINE_CLASS: 1365 setup_bcs(engine); 1366 break; 1367 case VIDEO_ENHANCEMENT_CLASS: 1368 setup_vecs(engine); 1369 break; 1370 default: 1371 MISSING_CASE(engine->class); 1372 return -ENODEV; 1373 } 1374 1375 timeline = intel_timeline_create_from_engine(engine, 1376 I915_GEM_HWS_SEQNO_ADDR); 1377 if (IS_ERR(timeline)) { 1378 err = PTR_ERR(timeline); 1379 goto err; 1380 } 1381 GEM_BUG_ON(timeline->has_initial_breadcrumb); 1382 1383 ring = intel_engine_create_ring(engine, SZ_16K); 1384 if (IS_ERR(ring)) { 1385 err = PTR_ERR(ring); 1386 goto err_timeline; 1387 } 1388 1389 GEM_BUG_ON(engine->legacy.ring); 1390 engine->legacy.ring = ring; 1391 engine->legacy.timeline = timeline; 1392 1393 gen7_wa_vma = gen7_ctx_vma(engine); 1394 if (IS_ERR(gen7_wa_vma)) { 1395 err = PTR_ERR(gen7_wa_vma); 1396 goto err_ring; 1397 } 1398 1399 i915_gem_ww_ctx_init(&ww, false); 1400 1401 retry: 1402 err = i915_gem_object_lock(timeline->hwsp_ggtt->obj, &ww); 1403 if (!err && gen7_wa_vma) 1404 err = i915_gem_object_lock(gen7_wa_vma->obj, &ww); 1405 if (!err) 1406 err = i915_gem_object_lock(engine->legacy.ring->vma->obj, &ww); 1407 if (!err) 1408 err = intel_timeline_pin(timeline, &ww); 1409 if (!err) { 1410 err = intel_ring_pin(ring, &ww); 1411 if (err) 1412 intel_timeline_unpin(timeline); 1413 } 1414 if (err) 1415 goto out; 1416 1417 GEM_BUG_ON(timeline->hwsp_ggtt != engine->status_page.vma); 1418 1419 if (gen7_wa_vma) { 1420 err = gen7_ctx_switch_bb_init(engine, &ww, gen7_wa_vma); 1421 if (err) { 1422 intel_ring_unpin(ring); 1423 intel_timeline_unpin(timeline); 1424 } 1425 } 1426 1427 out: 1428 if (err == -EDEADLK) { 1429 err = i915_gem_ww_ctx_backoff(&ww); 1430 if (!err) 1431 goto retry; 1432 } 1433 i915_gem_ww_ctx_fini(&ww); 1434 if (err) 1435 goto err_gen7_put; 1436 1437 /* Finally, take ownership and responsibility for cleanup! */ 1438 engine->release = ring_release; 1439 1440 return 0; 1441 1442 err_gen7_put: 1443 if (gen7_wa_vma) { 1444 intel_context_put(gen7_wa_vma->private); 1445 i915_gem_object_put(gen7_wa_vma->obj); 1446 } 1447 err_ring: 1448 intel_ring_put(ring); 1449 err_timeline: 1450 intel_timeline_put(timeline); 1451 err: 1452 intel_engine_cleanup_common(engine); 1453 return err; 1454 } 1455 1456 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 1457 #include "selftest_ring_submission.c" 1458 #endif 1459