1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2019 Intel Corporation 4 */ 5 6 #include <drm/drm_managed.h> 7 #include <drm/intel-gtt.h> 8 9 #include "gem/i915_gem_internal.h" 10 #include "gem/i915_gem_lmem.h" 11 #include "pxp/intel_pxp.h" 12 13 #include "i915_drv.h" 14 #include "i915_perf_oa_regs.h" 15 #include "intel_context.h" 16 #include "intel_engine_pm.h" 17 #include "intel_engine_regs.h" 18 #include "intel_ggtt_gmch.h" 19 #include "intel_gt.h" 20 #include "intel_gt_buffer_pool.h" 21 #include "intel_gt_clock_utils.h" 22 #include "intel_gt_debugfs.h" 23 #include "intel_gt_mcr.h" 24 #include "intel_gt_pm.h" 25 #include "intel_gt_regs.h" 26 #include "intel_gt_requests.h" 27 #include "intel_migrate.h" 28 #include "intel_mocs.h" 29 #include "intel_pci_config.h" 30 #include "intel_pm.h" 31 #include "intel_rc6.h" 32 #include "intel_renderstate.h" 33 #include "intel_rps.h" 34 #include "intel_sa_media.h" 35 #include "intel_gt_sysfs.h" 36 #include "intel_uncore.h" 37 #include "shmem_utils.h" 38 39 void intel_gt_common_init_early(struct intel_gt *gt) 40 { 41 spin_lock_init(gt->irq_lock); 42 43 INIT_LIST_HEAD(>->closed_vma); 44 spin_lock_init(>->closed_lock); 45 46 init_llist_head(>->watchdog.list); 47 INIT_WORK(>->watchdog.work, intel_gt_watchdog_work); 48 49 intel_gt_init_buffer_pool(gt); 50 intel_gt_init_reset(gt); 51 intel_gt_init_requests(gt); 52 intel_gt_init_timelines(gt); 53 mutex_init(>->tlb.invalidate_lock); 54 seqcount_mutex_init(>->tlb.seqno, >->tlb.invalidate_lock); 55 intel_gt_pm_init_early(gt); 56 57 intel_wopcm_init_early(>->wopcm); 58 intel_uc_init_early(>->uc); 59 intel_rps_init_early(>->rps); 60 } 61 62 /* Preliminary initialization of Tile 0 */ 63 int intel_root_gt_init_early(struct drm_i915_private *i915) 64 { 65 struct intel_gt *gt = to_gt(i915); 66 67 gt->i915 = i915; 68 gt->uncore = &i915->uncore; 69 gt->irq_lock = drmm_kzalloc(&i915->drm, sizeof(*gt->irq_lock), GFP_KERNEL); 70 if (!gt->irq_lock) 71 return -ENOMEM; 72 73 intel_gt_common_init_early(gt); 74 75 return 0; 76 } 77 78 static int intel_gt_probe_lmem(struct intel_gt *gt) 79 { 80 struct drm_i915_private *i915 = gt->i915; 81 unsigned int instance = gt->info.id; 82 int id = INTEL_REGION_LMEM_0 + instance; 83 struct intel_memory_region *mem; 84 int err; 85 86 mem = intel_gt_setup_lmem(gt); 87 if (IS_ERR(mem)) { 88 err = PTR_ERR(mem); 89 if (err == -ENODEV) 90 return 0; 91 92 drm_err(&i915->drm, 93 "Failed to setup region(%d) type=%d\n", 94 err, INTEL_MEMORY_LOCAL); 95 return err; 96 } 97 98 mem->id = id; 99 mem->instance = instance; 100 101 intel_memory_region_set_name(mem, "local%u", mem->instance); 102 103 GEM_BUG_ON(!HAS_REGION(i915, id)); 104 GEM_BUG_ON(i915->mm.regions[id]); 105 i915->mm.regions[id] = mem; 106 107 return 0; 108 } 109 110 int intel_gt_assign_ggtt(struct intel_gt *gt) 111 { 112 gt->ggtt = drmm_kzalloc(>->i915->drm, sizeof(*gt->ggtt), GFP_KERNEL); 113 114 return gt->ggtt ? 0 : -ENOMEM; 115 } 116 117 int intel_gt_init_mmio(struct intel_gt *gt) 118 { 119 intel_gt_init_clock_frequency(gt); 120 121 intel_uc_init_mmio(>->uc); 122 intel_sseu_info_init(gt); 123 intel_gt_mcr_init(gt); 124 125 return intel_engines_init_mmio(gt); 126 } 127 128 static void init_unused_ring(struct intel_gt *gt, u32 base) 129 { 130 struct intel_uncore *uncore = gt->uncore; 131 132 intel_uncore_write(uncore, RING_CTL(base), 0); 133 intel_uncore_write(uncore, RING_HEAD(base), 0); 134 intel_uncore_write(uncore, RING_TAIL(base), 0); 135 intel_uncore_write(uncore, RING_START(base), 0); 136 } 137 138 static void init_unused_rings(struct intel_gt *gt) 139 { 140 struct drm_i915_private *i915 = gt->i915; 141 142 if (IS_I830(i915)) { 143 init_unused_ring(gt, PRB1_BASE); 144 init_unused_ring(gt, SRB0_BASE); 145 init_unused_ring(gt, SRB1_BASE); 146 init_unused_ring(gt, SRB2_BASE); 147 init_unused_ring(gt, SRB3_BASE); 148 } else if (GRAPHICS_VER(i915) == 2) { 149 init_unused_ring(gt, SRB0_BASE); 150 init_unused_ring(gt, SRB1_BASE); 151 } else if (GRAPHICS_VER(i915) == 3) { 152 init_unused_ring(gt, PRB1_BASE); 153 init_unused_ring(gt, PRB2_BASE); 154 } 155 } 156 157 int intel_gt_init_hw(struct intel_gt *gt) 158 { 159 struct drm_i915_private *i915 = gt->i915; 160 struct intel_uncore *uncore = gt->uncore; 161 int ret; 162 163 gt->last_init_time = ktime_get(); 164 165 /* Double layer security blanket, see i915_gem_init() */ 166 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL); 167 168 if (HAS_EDRAM(i915) && GRAPHICS_VER(i915) < 9) 169 intel_uncore_rmw(uncore, HSW_IDICR, 0, IDIHASHMSK(0xf)); 170 171 if (IS_HASWELL(i915)) 172 intel_uncore_write(uncore, 173 HSW_MI_PREDICATE_RESULT_2, 174 IS_HSW_GT3(i915) ? 175 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); 176 177 /* Apply the GT workarounds... */ 178 intel_gt_apply_workarounds(gt); 179 /* ...and determine whether they are sticking. */ 180 intel_gt_verify_workarounds(gt, "init"); 181 182 intel_gt_init_swizzling(gt); 183 184 /* 185 * At least 830 can leave some of the unused rings 186 * "active" (ie. head != tail) after resume which 187 * will prevent c3 entry. Makes sure all unused rings 188 * are totally idle. 189 */ 190 init_unused_rings(gt); 191 192 ret = i915_ppgtt_init_hw(gt); 193 if (ret) { 194 drm_err(&i915->drm, "Enabling PPGTT failed (%d)\n", ret); 195 goto out; 196 } 197 198 /* We can't enable contexts until all firmware is loaded */ 199 ret = intel_uc_init_hw(>->uc); 200 if (ret) { 201 i915_probe_error(i915, "Enabling uc failed (%d)\n", ret); 202 goto out; 203 } 204 205 intel_mocs_init(gt); 206 207 out: 208 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL); 209 return ret; 210 } 211 212 static void rmw_set(struct intel_uncore *uncore, i915_reg_t reg, u32 set) 213 { 214 intel_uncore_rmw(uncore, reg, 0, set); 215 } 216 217 static void rmw_clear(struct intel_uncore *uncore, i915_reg_t reg, u32 clr) 218 { 219 intel_uncore_rmw(uncore, reg, clr, 0); 220 } 221 222 static void clear_register(struct intel_uncore *uncore, i915_reg_t reg) 223 { 224 intel_uncore_rmw(uncore, reg, 0, 0); 225 } 226 227 static void gen6_clear_engine_error_register(struct intel_engine_cs *engine) 228 { 229 GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0); 230 GEN6_RING_FAULT_REG_POSTING_READ(engine); 231 } 232 233 i915_reg_t intel_gt_perf_limit_reasons_reg(struct intel_gt *gt) 234 { 235 /* GT0_PERF_LIMIT_REASONS is available only for Gen11+ */ 236 if (GRAPHICS_VER(gt->i915) < 11) 237 return INVALID_MMIO_REG; 238 239 return gt->type == GT_MEDIA ? 240 MTL_MEDIA_PERF_LIMIT_REASONS : GT0_PERF_LIMIT_REASONS; 241 } 242 243 void 244 intel_gt_clear_error_registers(struct intel_gt *gt, 245 intel_engine_mask_t engine_mask) 246 { 247 struct drm_i915_private *i915 = gt->i915; 248 struct intel_uncore *uncore = gt->uncore; 249 u32 eir; 250 251 if (GRAPHICS_VER(i915) != 2) 252 clear_register(uncore, PGTBL_ER); 253 254 if (GRAPHICS_VER(i915) < 4) 255 clear_register(uncore, IPEIR(RENDER_RING_BASE)); 256 else 257 clear_register(uncore, IPEIR_I965); 258 259 clear_register(uncore, EIR); 260 eir = intel_uncore_read(uncore, EIR); 261 if (eir) { 262 /* 263 * some errors might have become stuck, 264 * mask them. 265 */ 266 drm_dbg(>->i915->drm, "EIR stuck: 0x%08x, masking\n", eir); 267 rmw_set(uncore, EMR, eir); 268 intel_uncore_write(uncore, GEN2_IIR, 269 I915_MASTER_ERROR_INTERRUPT); 270 } 271 272 if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50)) { 273 intel_gt_mcr_multicast_rmw(gt, XEHP_RING_FAULT_REG, 274 RING_FAULT_VALID, 0); 275 intel_gt_mcr_read_any(gt, XEHP_RING_FAULT_REG); 276 } else if (GRAPHICS_VER(i915) >= 12) { 277 rmw_clear(uncore, GEN12_RING_FAULT_REG, RING_FAULT_VALID); 278 intel_uncore_posting_read(uncore, GEN12_RING_FAULT_REG); 279 } else if (GRAPHICS_VER(i915) >= 8) { 280 rmw_clear(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID); 281 intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG); 282 } else if (GRAPHICS_VER(i915) >= 6) { 283 struct intel_engine_cs *engine; 284 enum intel_engine_id id; 285 286 for_each_engine_masked(engine, gt, engine_mask, id) 287 gen6_clear_engine_error_register(engine); 288 } 289 } 290 291 static void gen6_check_faults(struct intel_gt *gt) 292 { 293 struct intel_engine_cs *engine; 294 enum intel_engine_id id; 295 u32 fault; 296 297 for_each_engine(engine, gt, id) { 298 fault = GEN6_RING_FAULT_REG_READ(engine); 299 if (fault & RING_FAULT_VALID) { 300 drm_dbg(&engine->i915->drm, "Unexpected fault\n" 301 "\tAddr: 0x%08lx\n" 302 "\tAddress space: %s\n" 303 "\tSource ID: %d\n" 304 "\tType: %d\n", 305 fault & PAGE_MASK, 306 fault & RING_FAULT_GTTSEL_MASK ? 307 "GGTT" : "PPGTT", 308 RING_FAULT_SRCID(fault), 309 RING_FAULT_FAULT_TYPE(fault)); 310 } 311 } 312 } 313 314 static void xehp_check_faults(struct intel_gt *gt) 315 { 316 u32 fault; 317 318 /* 319 * Although the fault register now lives in an MCR register range, 320 * the GAM registers are special and we only truly need to read 321 * the "primary" GAM instance rather than handling each instance 322 * individually. intel_gt_mcr_read_any() will automatically steer 323 * toward the primary instance. 324 */ 325 fault = intel_gt_mcr_read_any(gt, XEHP_RING_FAULT_REG); 326 if (fault & RING_FAULT_VALID) { 327 u32 fault_data0, fault_data1; 328 u64 fault_addr; 329 330 fault_data0 = intel_gt_mcr_read_any(gt, XEHP_FAULT_TLB_DATA0); 331 fault_data1 = intel_gt_mcr_read_any(gt, XEHP_FAULT_TLB_DATA1); 332 333 fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) | 334 ((u64)fault_data0 << 12); 335 336 drm_dbg(>->i915->drm, "Unexpected fault\n" 337 "\tAddr: 0x%08x_%08x\n" 338 "\tAddress space: %s\n" 339 "\tEngine ID: %d\n" 340 "\tSource ID: %d\n" 341 "\tType: %d\n", 342 upper_32_bits(fault_addr), lower_32_bits(fault_addr), 343 fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT", 344 GEN8_RING_FAULT_ENGINE_ID(fault), 345 RING_FAULT_SRCID(fault), 346 RING_FAULT_FAULT_TYPE(fault)); 347 } 348 } 349 350 static void gen8_check_faults(struct intel_gt *gt) 351 { 352 struct intel_uncore *uncore = gt->uncore; 353 i915_reg_t fault_reg, fault_data0_reg, fault_data1_reg; 354 u32 fault; 355 356 if (GRAPHICS_VER(gt->i915) >= 12) { 357 fault_reg = GEN12_RING_FAULT_REG; 358 fault_data0_reg = GEN12_FAULT_TLB_DATA0; 359 fault_data1_reg = GEN12_FAULT_TLB_DATA1; 360 } else { 361 fault_reg = GEN8_RING_FAULT_REG; 362 fault_data0_reg = GEN8_FAULT_TLB_DATA0; 363 fault_data1_reg = GEN8_FAULT_TLB_DATA1; 364 } 365 366 fault = intel_uncore_read(uncore, fault_reg); 367 if (fault & RING_FAULT_VALID) { 368 u32 fault_data0, fault_data1; 369 u64 fault_addr; 370 371 fault_data0 = intel_uncore_read(uncore, fault_data0_reg); 372 fault_data1 = intel_uncore_read(uncore, fault_data1_reg); 373 374 fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) | 375 ((u64)fault_data0 << 12); 376 377 drm_dbg(&uncore->i915->drm, "Unexpected fault\n" 378 "\tAddr: 0x%08x_%08x\n" 379 "\tAddress space: %s\n" 380 "\tEngine ID: %d\n" 381 "\tSource ID: %d\n" 382 "\tType: %d\n", 383 upper_32_bits(fault_addr), lower_32_bits(fault_addr), 384 fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT", 385 GEN8_RING_FAULT_ENGINE_ID(fault), 386 RING_FAULT_SRCID(fault), 387 RING_FAULT_FAULT_TYPE(fault)); 388 } 389 } 390 391 void intel_gt_check_and_clear_faults(struct intel_gt *gt) 392 { 393 struct drm_i915_private *i915 = gt->i915; 394 395 /* From GEN8 onwards we only have one 'All Engine Fault Register' */ 396 if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50)) 397 xehp_check_faults(gt); 398 else if (GRAPHICS_VER(i915) >= 8) 399 gen8_check_faults(gt); 400 else if (GRAPHICS_VER(i915) >= 6) 401 gen6_check_faults(gt); 402 else 403 return; 404 405 intel_gt_clear_error_registers(gt, ALL_ENGINES); 406 } 407 408 void intel_gt_flush_ggtt_writes(struct intel_gt *gt) 409 { 410 struct intel_uncore *uncore = gt->uncore; 411 intel_wakeref_t wakeref; 412 413 /* 414 * No actual flushing is required for the GTT write domain for reads 415 * from the GTT domain. Writes to it "immediately" go to main memory 416 * as far as we know, so there's no chipset flush. It also doesn't 417 * land in the GPU render cache. 418 * 419 * However, we do have to enforce the order so that all writes through 420 * the GTT land before any writes to the device, such as updates to 421 * the GATT itself. 422 * 423 * We also have to wait a bit for the writes to land from the GTT. 424 * An uncached read (i.e. mmio) seems to be ideal for the round-trip 425 * timing. This issue has only been observed when switching quickly 426 * between GTT writes and CPU reads from inside the kernel on recent hw, 427 * and it appears to only affect discrete GTT blocks (i.e. on LLC 428 * system agents we cannot reproduce this behaviour, until Cannonlake 429 * that was!). 430 */ 431 432 wmb(); 433 434 if (INTEL_INFO(gt->i915)->has_coherent_ggtt) 435 return; 436 437 intel_gt_chipset_flush(gt); 438 439 with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) { 440 unsigned long flags; 441 442 spin_lock_irqsave(&uncore->lock, flags); 443 intel_uncore_posting_read_fw(uncore, 444 RING_HEAD(RENDER_RING_BASE)); 445 spin_unlock_irqrestore(&uncore->lock, flags); 446 } 447 } 448 449 void intel_gt_chipset_flush(struct intel_gt *gt) 450 { 451 wmb(); 452 if (GRAPHICS_VER(gt->i915) < 6) 453 intel_ggtt_gmch_flush(); 454 } 455 456 void intel_gt_driver_register(struct intel_gt *gt) 457 { 458 intel_gsc_init(>->gsc, gt->i915); 459 460 intel_rps_driver_register(>->rps); 461 462 intel_gt_debugfs_register(gt); 463 intel_gt_sysfs_register(gt); 464 } 465 466 static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size) 467 { 468 struct drm_i915_private *i915 = gt->i915; 469 struct drm_i915_gem_object *obj; 470 struct i915_vma *vma; 471 int ret; 472 473 obj = i915_gem_object_create_lmem(i915, size, 474 I915_BO_ALLOC_VOLATILE | 475 I915_BO_ALLOC_GPU_ONLY); 476 if (IS_ERR(obj)) 477 obj = i915_gem_object_create_stolen(i915, size); 478 if (IS_ERR(obj)) 479 obj = i915_gem_object_create_internal(i915, size); 480 if (IS_ERR(obj)) { 481 drm_err(&i915->drm, "Failed to allocate scratch page\n"); 482 return PTR_ERR(obj); 483 } 484 485 vma = i915_vma_instance(obj, >->ggtt->vm, NULL); 486 if (IS_ERR(vma)) { 487 ret = PTR_ERR(vma); 488 goto err_unref; 489 } 490 491 ret = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH); 492 if (ret) 493 goto err_unref; 494 495 gt->scratch = i915_vma_make_unshrinkable(vma); 496 497 return 0; 498 499 err_unref: 500 i915_gem_object_put(obj); 501 return ret; 502 } 503 504 static void intel_gt_fini_scratch(struct intel_gt *gt) 505 { 506 i915_vma_unpin_and_release(>->scratch, 0); 507 } 508 509 static struct i915_address_space *kernel_vm(struct intel_gt *gt) 510 { 511 if (INTEL_PPGTT(gt->i915) > INTEL_PPGTT_ALIASING) 512 return &i915_ppgtt_create(gt, I915_BO_ALLOC_PM_EARLY)->vm; 513 else 514 return i915_vm_get(>->ggtt->vm); 515 } 516 517 static int __engines_record_defaults(struct intel_gt *gt) 518 { 519 struct i915_request *requests[I915_NUM_ENGINES] = {}; 520 struct intel_engine_cs *engine; 521 enum intel_engine_id id; 522 int err = 0; 523 524 /* 525 * As we reset the gpu during very early sanitisation, the current 526 * register state on the GPU should reflect its defaults values. 527 * We load a context onto the hw (with restore-inhibit), then switch 528 * over to a second context to save that default register state. We 529 * can then prime every new context with that state so they all start 530 * from the same default HW values. 531 */ 532 533 for_each_engine(engine, gt, id) { 534 struct intel_renderstate so; 535 struct intel_context *ce; 536 struct i915_request *rq; 537 538 /* We must be able to switch to something! */ 539 GEM_BUG_ON(!engine->kernel_context); 540 541 ce = intel_context_create(engine); 542 if (IS_ERR(ce)) { 543 err = PTR_ERR(ce); 544 goto out; 545 } 546 547 err = intel_renderstate_init(&so, ce); 548 if (err) 549 goto err; 550 551 rq = i915_request_create(ce); 552 if (IS_ERR(rq)) { 553 err = PTR_ERR(rq); 554 goto err_fini; 555 } 556 557 err = intel_engine_emit_ctx_wa(rq); 558 if (err) 559 goto err_rq; 560 561 err = intel_renderstate_emit(&so, rq); 562 if (err) 563 goto err_rq; 564 565 err_rq: 566 requests[id] = i915_request_get(rq); 567 i915_request_add(rq); 568 err_fini: 569 intel_renderstate_fini(&so, ce); 570 err: 571 if (err) { 572 intel_context_put(ce); 573 goto out; 574 } 575 } 576 577 /* Flush the default context image to memory, and enable powersaving. */ 578 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) { 579 err = -EIO; 580 goto out; 581 } 582 583 for (id = 0; id < ARRAY_SIZE(requests); id++) { 584 struct i915_request *rq; 585 struct file *state; 586 587 rq = requests[id]; 588 if (!rq) 589 continue; 590 591 if (rq->fence.error) { 592 err = -EIO; 593 goto out; 594 } 595 596 GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT, &rq->context->flags)); 597 if (!rq->context->state) 598 continue; 599 600 /* Keep a copy of the state's backing pages; free the obj */ 601 state = shmem_create_from_object(rq->context->state->obj); 602 if (IS_ERR(state)) { 603 err = PTR_ERR(state); 604 goto out; 605 } 606 rq->engine->default_state = state; 607 } 608 609 out: 610 /* 611 * If we have to abandon now, we expect the engines to be idle 612 * and ready to be torn-down. The quickest way we can accomplish 613 * this is by declaring ourselves wedged. 614 */ 615 if (err) 616 intel_gt_set_wedged(gt); 617 618 for (id = 0; id < ARRAY_SIZE(requests); id++) { 619 struct intel_context *ce; 620 struct i915_request *rq; 621 622 rq = requests[id]; 623 if (!rq) 624 continue; 625 626 ce = rq->context; 627 i915_request_put(rq); 628 intel_context_put(ce); 629 } 630 return err; 631 } 632 633 static int __engines_verify_workarounds(struct intel_gt *gt) 634 { 635 struct intel_engine_cs *engine; 636 enum intel_engine_id id; 637 int err = 0; 638 639 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) 640 return 0; 641 642 for_each_engine(engine, gt, id) { 643 if (intel_engine_verify_workarounds(engine, "load")) 644 err = -EIO; 645 } 646 647 /* Flush and restore the kernel context for safety */ 648 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) 649 err = -EIO; 650 651 return err; 652 } 653 654 static void __intel_gt_disable(struct intel_gt *gt) 655 { 656 intel_gt_set_wedged_on_fini(gt); 657 658 intel_gt_suspend_prepare(gt); 659 intel_gt_suspend_late(gt); 660 661 GEM_BUG_ON(intel_gt_pm_is_awake(gt)); 662 } 663 664 int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout) 665 { 666 long remaining_timeout; 667 668 /* If the device is asleep, we have no requests outstanding */ 669 if (!intel_gt_pm_is_awake(gt)) 670 return 0; 671 672 while ((timeout = intel_gt_retire_requests_timeout(gt, timeout, 673 &remaining_timeout)) > 0) { 674 cond_resched(); 675 if (signal_pending(current)) 676 return -EINTR; 677 } 678 679 return timeout ? timeout : intel_uc_wait_for_idle(>->uc, 680 remaining_timeout); 681 } 682 683 int intel_gt_init(struct intel_gt *gt) 684 { 685 int err; 686 687 err = i915_inject_probe_error(gt->i915, -ENODEV); 688 if (err) 689 return err; 690 691 intel_gt_init_workarounds(gt); 692 693 /* 694 * This is just a security blanket to placate dragons. 695 * On some systems, we very sporadically observe that the first TLBs 696 * used by the CS may be stale, despite us poking the TLB reset. If 697 * we hold the forcewake during initialisation these problems 698 * just magically go away. 699 */ 700 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL); 701 702 err = intel_gt_init_scratch(gt, 703 GRAPHICS_VER(gt->i915) == 2 ? SZ_256K : SZ_4K); 704 if (err) 705 goto out_fw; 706 707 intel_gt_pm_init(gt); 708 709 gt->vm = kernel_vm(gt); 710 if (!gt->vm) { 711 err = -ENOMEM; 712 goto err_pm; 713 } 714 715 intel_set_mocs_index(gt); 716 717 err = intel_engines_init(gt); 718 if (err) 719 goto err_engines; 720 721 err = intel_uc_init(>->uc); 722 if (err) 723 goto err_engines; 724 725 err = intel_gt_resume(gt); 726 if (err) 727 goto err_uc_init; 728 729 err = intel_gt_init_hwconfig(gt); 730 if (err) 731 drm_err(>->i915->drm, "Failed to retrieve hwconfig table: %pe\n", 732 ERR_PTR(err)); 733 734 err = __engines_record_defaults(gt); 735 if (err) 736 goto err_gt; 737 738 err = __engines_verify_workarounds(gt); 739 if (err) 740 goto err_gt; 741 742 intel_uc_init_late(>->uc); 743 744 err = i915_inject_probe_error(gt->i915, -EIO); 745 if (err) 746 goto err_gt; 747 748 intel_migrate_init(>->migrate, gt); 749 750 intel_pxp_init(>->pxp); 751 752 goto out_fw; 753 err_gt: 754 __intel_gt_disable(gt); 755 intel_uc_fini_hw(>->uc); 756 err_uc_init: 757 intel_uc_fini(>->uc); 758 err_engines: 759 intel_engines_release(gt); 760 i915_vm_put(fetch_and_zero(>->vm)); 761 err_pm: 762 intel_gt_pm_fini(gt); 763 intel_gt_fini_scratch(gt); 764 out_fw: 765 if (err) 766 intel_gt_set_wedged_on_init(gt); 767 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL); 768 return err; 769 } 770 771 void intel_gt_driver_remove(struct intel_gt *gt) 772 { 773 __intel_gt_disable(gt); 774 775 intel_migrate_fini(>->migrate); 776 intel_uc_driver_remove(>->uc); 777 778 intel_engines_release(gt); 779 780 intel_gt_flush_buffer_pool(gt); 781 } 782 783 void intel_gt_driver_unregister(struct intel_gt *gt) 784 { 785 intel_wakeref_t wakeref; 786 787 intel_gt_sysfs_unregister(gt); 788 intel_rps_driver_unregister(>->rps); 789 intel_gsc_fini(>->gsc); 790 791 intel_pxp_fini(>->pxp); 792 793 /* 794 * Upon unregistering the device to prevent any new users, cancel 795 * all in-flight requests so that we can quickly unbind the active 796 * resources. 797 */ 798 intel_gt_set_wedged_on_fini(gt); 799 800 /* Scrub all HW state upon release */ 801 with_intel_runtime_pm(gt->uncore->rpm, wakeref) 802 __intel_gt_reset(gt, ALL_ENGINES); 803 } 804 805 void intel_gt_driver_release(struct intel_gt *gt) 806 { 807 struct i915_address_space *vm; 808 809 vm = fetch_and_zero(>->vm); 810 if (vm) /* FIXME being called twice on error paths :( */ 811 i915_vm_put(vm); 812 813 intel_wa_list_free(>->wa_list); 814 intel_gt_pm_fini(gt); 815 intel_gt_fini_scratch(gt); 816 intel_gt_fini_buffer_pool(gt); 817 intel_gt_fini_hwconfig(gt); 818 } 819 820 void intel_gt_driver_late_release_all(struct drm_i915_private *i915) 821 { 822 struct intel_gt *gt; 823 unsigned int id; 824 825 /* We need to wait for inflight RCU frees to release their grip */ 826 rcu_barrier(); 827 828 for_each_gt(gt, i915, id) { 829 intel_uc_driver_late_release(>->uc); 830 intel_gt_fini_requests(gt); 831 intel_gt_fini_reset(gt); 832 intel_gt_fini_timelines(gt); 833 mutex_destroy(>->tlb.invalidate_lock); 834 intel_engines_free(gt); 835 } 836 } 837 838 static int intel_gt_tile_setup(struct intel_gt *gt, phys_addr_t phys_addr) 839 { 840 int ret; 841 842 if (!gt_is_root(gt)) { 843 struct intel_uncore *uncore; 844 spinlock_t *irq_lock; 845 846 uncore = drmm_kzalloc(>->i915->drm, sizeof(*uncore), GFP_KERNEL); 847 if (!uncore) 848 return -ENOMEM; 849 850 irq_lock = drmm_kzalloc(>->i915->drm, sizeof(*irq_lock), GFP_KERNEL); 851 if (!irq_lock) 852 return -ENOMEM; 853 854 gt->uncore = uncore; 855 gt->irq_lock = irq_lock; 856 857 intel_gt_common_init_early(gt); 858 } 859 860 intel_uncore_init_early(gt->uncore, gt); 861 862 ret = intel_uncore_setup_mmio(gt->uncore, phys_addr); 863 if (ret) 864 return ret; 865 866 gt->phys_addr = phys_addr; 867 868 return 0; 869 } 870 871 int intel_gt_probe_all(struct drm_i915_private *i915) 872 { 873 struct pci_dev *pdev = to_pci_dev(i915->drm.dev); 874 struct intel_gt *gt = &i915->gt0; 875 const struct intel_gt_definition *gtdef; 876 phys_addr_t phys_addr; 877 unsigned int mmio_bar; 878 unsigned int i; 879 int ret; 880 881 mmio_bar = intel_mmio_bar(GRAPHICS_VER(i915)); 882 phys_addr = pci_resource_start(pdev, mmio_bar); 883 884 /* 885 * We always have at least one primary GT on any device 886 * and it has been already initialized early during probe 887 * in i915_driver_probe() 888 */ 889 gt->i915 = i915; 890 gt->name = "Primary GT"; 891 gt->info.engine_mask = RUNTIME_INFO(i915)->platform_engine_mask; 892 893 drm_dbg(&i915->drm, "Setting up %s\n", gt->name); 894 ret = intel_gt_tile_setup(gt, phys_addr); 895 if (ret) 896 return ret; 897 898 i915->gt[0] = gt; 899 900 if (!HAS_EXTRA_GT_LIST(i915)) 901 return 0; 902 903 for (i = 1, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1]; 904 gtdef->name != NULL; 905 i++, gtdef = &INTEL_INFO(i915)->extra_gt_list[i - 1]) { 906 gt = drmm_kzalloc(&i915->drm, sizeof(*gt), GFP_KERNEL); 907 if (!gt) { 908 ret = -ENOMEM; 909 goto err; 910 } 911 912 gt->i915 = i915; 913 gt->name = gtdef->name; 914 gt->type = gtdef->type; 915 gt->info.engine_mask = gtdef->engine_mask; 916 gt->info.id = i; 917 918 drm_dbg(&i915->drm, "Setting up %s\n", gt->name); 919 if (GEM_WARN_ON(range_overflows_t(resource_size_t, 920 gtdef->mapping_base, 921 SZ_16M, 922 pci_resource_len(pdev, mmio_bar)))) { 923 ret = -ENODEV; 924 goto err; 925 } 926 927 switch (gtdef->type) { 928 case GT_TILE: 929 ret = intel_gt_tile_setup(gt, phys_addr + gtdef->mapping_base); 930 break; 931 932 case GT_MEDIA: 933 ret = intel_sa_mediagt_setup(gt, phys_addr + gtdef->mapping_base, 934 gtdef->gsi_offset); 935 break; 936 937 case GT_PRIMARY: 938 /* Primary GT should not appear in extra GT list */ 939 default: 940 MISSING_CASE(gtdef->type); 941 ret = -ENODEV; 942 } 943 944 if (ret) 945 goto err; 946 947 i915->gt[i] = gt; 948 } 949 950 return 0; 951 952 err: 953 i915_probe_error(i915, "Failed to initialize %s! (%d)\n", gtdef->name, ret); 954 intel_gt_release_all(i915); 955 956 return ret; 957 } 958 959 int intel_gt_tiles_init(struct drm_i915_private *i915) 960 { 961 struct intel_gt *gt; 962 unsigned int id; 963 int ret; 964 965 for_each_gt(gt, i915, id) { 966 ret = intel_gt_probe_lmem(gt); 967 if (ret) 968 return ret; 969 } 970 971 return 0; 972 } 973 974 void intel_gt_release_all(struct drm_i915_private *i915) 975 { 976 struct intel_gt *gt; 977 unsigned int id; 978 979 for_each_gt(gt, i915, id) 980 i915->gt[id] = NULL; 981 } 982 983 void intel_gt_info_print(const struct intel_gt_info *info, 984 struct drm_printer *p) 985 { 986 drm_printf(p, "available engines: %x\n", info->engine_mask); 987 988 intel_sseu_dump(&info->sseu, p); 989 } 990 991 struct reg_and_bit { 992 union { 993 i915_reg_t reg; 994 i915_mcr_reg_t mcr_reg; 995 }; 996 u32 bit; 997 }; 998 999 static struct reg_and_bit 1000 get_reg_and_bit(const struct intel_engine_cs *engine, const bool gen8, 1001 const i915_reg_t *regs, const unsigned int num) 1002 { 1003 const unsigned int class = engine->class; 1004 struct reg_and_bit rb = { }; 1005 1006 if (drm_WARN_ON_ONCE(&engine->i915->drm, 1007 class >= num || !regs[class].reg)) 1008 return rb; 1009 1010 rb.reg = regs[class]; 1011 if (gen8 && class == VIDEO_DECODE_CLASS) 1012 rb.reg.reg += 4 * engine->instance; /* GEN8_M2TCR */ 1013 else 1014 rb.bit = engine->instance; 1015 1016 rb.bit = BIT(rb.bit); 1017 1018 return rb; 1019 } 1020 1021 /* 1022 * HW architecture suggest typical invalidation time at 40us, 1023 * with pessimistic cases up to 100us and a recommendation to 1024 * cap at 1ms. We go a bit higher just in case. 1025 */ 1026 #define TLB_INVAL_TIMEOUT_US 100 1027 #define TLB_INVAL_TIMEOUT_MS 4 1028 1029 /* 1030 * On Xe_HP the TLB invalidation registers are located at the same MMIO offsets 1031 * but are now considered MCR registers. Since they exist within a GAM range, 1032 * the primary instance of the register rolls up the status from each unit. 1033 */ 1034 static int wait_for_invalidate(struct intel_gt *gt, struct reg_and_bit rb) 1035 { 1036 if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 50)) 1037 return intel_gt_mcr_wait_for_reg_fw(gt, rb.mcr_reg, rb.bit, 0, 1038 TLB_INVAL_TIMEOUT_US, 1039 TLB_INVAL_TIMEOUT_MS); 1040 else 1041 return __intel_wait_for_register_fw(gt->uncore, rb.reg, rb.bit, 0, 1042 TLB_INVAL_TIMEOUT_US, 1043 TLB_INVAL_TIMEOUT_MS, 1044 NULL); 1045 } 1046 1047 static void mmio_invalidate_full(struct intel_gt *gt) 1048 { 1049 static const i915_reg_t gen8_regs[] = { 1050 [RENDER_CLASS] = GEN8_RTCR, 1051 [VIDEO_DECODE_CLASS] = GEN8_M1TCR, /* , GEN8_M2TCR */ 1052 [VIDEO_ENHANCEMENT_CLASS] = GEN8_VTCR, 1053 [COPY_ENGINE_CLASS] = GEN8_BTCR, 1054 }; 1055 static const i915_reg_t gen12_regs[] = { 1056 [RENDER_CLASS] = GEN12_GFX_TLB_INV_CR, 1057 [VIDEO_DECODE_CLASS] = GEN12_VD_TLB_INV_CR, 1058 [VIDEO_ENHANCEMENT_CLASS] = GEN12_VE_TLB_INV_CR, 1059 [COPY_ENGINE_CLASS] = GEN12_BLT_TLB_INV_CR, 1060 [COMPUTE_CLASS] = GEN12_COMPCTX_TLB_INV_CR, 1061 }; 1062 static const i915_mcr_reg_t xehp_regs[] = { 1063 [RENDER_CLASS] = XEHP_GFX_TLB_INV_CR, 1064 [VIDEO_DECODE_CLASS] = XEHP_VD_TLB_INV_CR, 1065 [VIDEO_ENHANCEMENT_CLASS] = XEHP_VE_TLB_INV_CR, 1066 [COPY_ENGINE_CLASS] = XEHP_BLT_TLB_INV_CR, 1067 [COMPUTE_CLASS] = XEHP_COMPCTX_TLB_INV_CR, 1068 }; 1069 struct drm_i915_private *i915 = gt->i915; 1070 struct intel_uncore *uncore = gt->uncore; 1071 struct intel_engine_cs *engine; 1072 intel_engine_mask_t awake, tmp; 1073 enum intel_engine_id id; 1074 const i915_reg_t *regs; 1075 unsigned int num = 0; 1076 1077 if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50)) { 1078 regs = NULL; 1079 num = ARRAY_SIZE(xehp_regs); 1080 } else if (GRAPHICS_VER(i915) == 12) { 1081 regs = gen12_regs; 1082 num = ARRAY_SIZE(gen12_regs); 1083 } else if (GRAPHICS_VER(i915) >= 8 && GRAPHICS_VER(i915) <= 11) { 1084 regs = gen8_regs; 1085 num = ARRAY_SIZE(gen8_regs); 1086 } else if (GRAPHICS_VER(i915) < 8) { 1087 return; 1088 } 1089 1090 if (drm_WARN_ONCE(&i915->drm, !num, 1091 "Platform does not implement TLB invalidation!")) 1092 return; 1093 1094 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL); 1095 1096 spin_lock_irq(&uncore->lock); /* serialise invalidate with GT reset */ 1097 1098 awake = 0; 1099 for_each_engine(engine, gt, id) { 1100 struct reg_and_bit rb; 1101 1102 if (!intel_engine_pm_is_awake(engine)) 1103 continue; 1104 1105 if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50)) { 1106 intel_gt_mcr_multicast_write_fw(gt, 1107 xehp_regs[engine->class], 1108 BIT(engine->instance)); 1109 } else { 1110 rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num); 1111 if (!i915_mmio_reg_offset(rb.reg)) 1112 continue; 1113 1114 intel_uncore_write_fw(uncore, rb.reg, rb.bit); 1115 } 1116 awake |= engine->mask; 1117 } 1118 1119 GT_TRACE(gt, "invalidated engines %08x\n", awake); 1120 1121 /* Wa_2207587034:tgl,dg1,rkl,adl-s,adl-p */ 1122 if (awake && 1123 (IS_TIGERLAKE(i915) || 1124 IS_DG1(i915) || 1125 IS_ROCKETLAKE(i915) || 1126 IS_ALDERLAKE_S(i915) || 1127 IS_ALDERLAKE_P(i915))) 1128 intel_uncore_write_fw(uncore, GEN12_OA_TLB_INV_CR, 1); 1129 1130 spin_unlock_irq(&uncore->lock); 1131 1132 for_each_engine_masked(engine, gt, awake, tmp) { 1133 struct reg_and_bit rb; 1134 1135 if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50)) { 1136 rb.mcr_reg = xehp_regs[engine->class]; 1137 rb.bit = BIT(engine->instance); 1138 } else { 1139 rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num); 1140 } 1141 1142 if (wait_for_invalidate(gt, rb)) 1143 drm_err_ratelimited(>->i915->drm, 1144 "%s TLB invalidation did not complete in %ums!\n", 1145 engine->name, TLB_INVAL_TIMEOUT_MS); 1146 } 1147 1148 /* 1149 * Use delayed put since a) we mostly expect a flurry of TLB 1150 * invalidations so it is good to avoid paying the forcewake cost and 1151 * b) it works around a bug in Icelake which cannot cope with too rapid 1152 * transitions. 1153 */ 1154 intel_uncore_forcewake_put_delayed(uncore, FORCEWAKE_ALL); 1155 } 1156 1157 static bool tlb_seqno_passed(const struct intel_gt *gt, u32 seqno) 1158 { 1159 u32 cur = intel_gt_tlb_seqno(gt); 1160 1161 /* Only skip if a *full* TLB invalidate barrier has passed */ 1162 return (s32)(cur - ALIGN(seqno, 2)) > 0; 1163 } 1164 1165 void intel_gt_invalidate_tlb(struct intel_gt *gt, u32 seqno) 1166 { 1167 intel_wakeref_t wakeref; 1168 1169 if (I915_SELFTEST_ONLY(gt->awake == -ENODEV)) 1170 return; 1171 1172 if (intel_gt_is_wedged(gt)) 1173 return; 1174 1175 if (tlb_seqno_passed(gt, seqno)) 1176 return; 1177 1178 with_intel_gt_pm_if_awake(gt, wakeref) { 1179 mutex_lock(>->tlb.invalidate_lock); 1180 if (tlb_seqno_passed(gt, seqno)) 1181 goto unlock; 1182 1183 mmio_invalidate_full(gt); 1184 1185 write_seqcount_invalidate(>->tlb.seqno); 1186 unlock: 1187 mutex_unlock(>->tlb.invalidate_lock); 1188 } 1189 } 1190