1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2008-2018 Intel Corporation 4 */ 5 6 #include <linux/sched/mm.h> 7 #include <linux/stop_machine.h> 8 #include <linux/string_helpers.h> 9 10 #include <drm/intel/mchbar_regs.h> 11 #include <drm/intel/pci_config.h> 12 13 #include "display/intel_display_reset.h" 14 #include "display/intel_overlay.h" 15 #include "gem/i915_gem_context.h" 16 #include "gt/intel_gt_regs.h" 17 #include "gt/uc/intel_gsc_fw.h" 18 #include "uc/intel_guc.h" 19 20 #include "i915_drv.h" 21 #include "i915_file_private.h" 22 #include "i915_gpu_error.h" 23 #include "i915_irq.h" 24 #include "i915_reg.h" 25 #include "i915_wait_util.h" 26 #include "intel_breadcrumbs.h" 27 #include "intel_engine_pm.h" 28 #include "intel_engine_regs.h" 29 #include "intel_gt.h" 30 #include "intel_gt_pm.h" 31 #include "intel_gt_print.h" 32 #include "intel_gt_requests.h" 33 #include "intel_reset.h" 34 35 #define RESET_MAX_RETRIES 3 36 37 static void client_mark_guilty(struct i915_gem_context *ctx, bool banned) 38 { 39 struct drm_i915_file_private *file_priv = ctx->file_priv; 40 unsigned long prev_hang; 41 unsigned int score; 42 43 if (IS_ERR_OR_NULL(file_priv)) 44 return; 45 46 score = 0; 47 if (banned) 48 score = I915_CLIENT_SCORE_CONTEXT_BAN; 49 50 prev_hang = xchg(&file_priv->hang_timestamp, jiffies); 51 if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES)) 52 score += I915_CLIENT_SCORE_HANG_FAST; 53 54 if (score) { 55 atomic_add(score, &file_priv->ban_score); 56 57 drm_dbg(&ctx->i915->drm, 58 "client %s: gained %u ban score, now %u\n", 59 ctx->name, score, 60 atomic_read(&file_priv->ban_score)); 61 } 62 } 63 64 static bool mark_guilty(struct i915_request *rq) 65 { 66 struct i915_gem_context *ctx; 67 unsigned long prev_hang; 68 bool banned; 69 int i; 70 71 if (intel_context_is_closed(rq->context)) 72 return true; 73 74 rcu_read_lock(); 75 ctx = rcu_dereference(rq->context->gem_context); 76 if (ctx && !kref_get_unless_zero(&ctx->ref)) 77 ctx = NULL; 78 rcu_read_unlock(); 79 if (!ctx) 80 return intel_context_is_banned(rq->context); 81 82 atomic_inc(&ctx->guilty_count); 83 84 /* Cool contexts are too cool to be banned! (Used for reset testing.) */ 85 if (!i915_gem_context_is_bannable(ctx)) { 86 banned = false; 87 goto out; 88 } 89 90 drm_notice(&ctx->i915->drm, 91 "%s context reset due to GPU hang\n", 92 ctx->name); 93 94 /* Record the timestamp for the last N hangs */ 95 prev_hang = ctx->hang_timestamp[0]; 96 for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp) - 1; i++) 97 ctx->hang_timestamp[i] = ctx->hang_timestamp[i + 1]; 98 ctx->hang_timestamp[i] = jiffies; 99 100 /* If we have hung N+1 times in rapid succession, we ban the context! */ 101 banned = !i915_gem_context_is_recoverable(ctx); 102 if (time_before(jiffies, prev_hang + CONTEXT_FAST_HANG_JIFFIES)) 103 banned = true; 104 if (banned) 105 drm_dbg(&ctx->i915->drm, "context %s: guilty %d, banned\n", 106 ctx->name, atomic_read(&ctx->guilty_count)); 107 108 client_mark_guilty(ctx, banned); 109 110 out: 111 i915_gem_context_put(ctx); 112 return banned; 113 } 114 115 static void mark_innocent(struct i915_request *rq) 116 { 117 struct i915_gem_context *ctx; 118 119 rcu_read_lock(); 120 ctx = rcu_dereference(rq->context->gem_context); 121 if (ctx) 122 atomic_inc(&ctx->active_count); 123 rcu_read_unlock(); 124 } 125 126 void __i915_request_reset(struct i915_request *rq, bool guilty) 127 { 128 bool banned = false; 129 130 RQ_TRACE(rq, "guilty? %s\n", str_yes_no(guilty)); 131 GEM_BUG_ON(__i915_request_is_complete(rq)); 132 133 rcu_read_lock(); /* protect the GEM context */ 134 if (guilty) { 135 i915_request_set_error_once(rq, -EIO); 136 if (!i915_request_signaled(rq)) 137 __i915_request_skip(rq); 138 banned = mark_guilty(rq); 139 } else { 140 i915_request_set_error_once(rq, -EAGAIN); 141 mark_innocent(rq); 142 } 143 rcu_read_unlock(); 144 145 if (banned) 146 intel_context_ban(rq->context, rq); 147 } 148 149 static bool i915_in_reset(struct pci_dev *pdev) 150 { 151 u8 gdrst; 152 153 pci_read_config_byte(pdev, I915_GDRST, &gdrst); 154 return gdrst & GRDOM_RESET_STATUS; 155 } 156 157 static int i915_do_reset(struct intel_gt *gt, 158 intel_engine_mask_t engine_mask, 159 unsigned int retry) 160 { 161 struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev); 162 int err; 163 164 /* Assert reset for at least 50 usec, and wait for acknowledgement. */ 165 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE); 166 udelay(50); 167 err = _wait_for_atomic(i915_in_reset(pdev), 50000, 0); 168 169 /* Clear the reset request. */ 170 pci_write_config_byte(pdev, I915_GDRST, 0); 171 udelay(50); 172 if (!err) 173 err = _wait_for_atomic(!i915_in_reset(pdev), 50000, 0); 174 175 return err; 176 } 177 178 static bool g4x_reset_complete(struct pci_dev *pdev) 179 { 180 u8 gdrst; 181 182 pci_read_config_byte(pdev, I915_GDRST, &gdrst); 183 return (gdrst & GRDOM_RESET_ENABLE) == 0; 184 } 185 186 static int g33_do_reset(struct intel_gt *gt, 187 intel_engine_mask_t engine_mask, 188 unsigned int retry) 189 { 190 struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev); 191 192 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE); 193 return _wait_for_atomic(g4x_reset_complete(pdev), 50000, 0); 194 } 195 196 static int g4x_do_reset(struct intel_gt *gt, 197 intel_engine_mask_t engine_mask, 198 unsigned int retry) 199 { 200 struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev); 201 struct intel_uncore *uncore = gt->uncore; 202 int ret; 203 204 /* WaVcpClkGateDisableForMediaReset:ctg,elk */ 205 intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, 0, VCP_UNIT_CLOCK_GATE_DISABLE); 206 intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D); 207 208 pci_write_config_byte(pdev, I915_GDRST, 209 GRDOM_MEDIA | GRDOM_RESET_ENABLE); 210 ret = _wait_for_atomic(g4x_reset_complete(pdev), 50000, 0); 211 if (ret) { 212 GT_TRACE(gt, "Wait for media reset failed\n"); 213 goto out; 214 } 215 216 pci_write_config_byte(pdev, I915_GDRST, 217 GRDOM_RENDER | GRDOM_RESET_ENABLE); 218 ret = _wait_for_atomic(g4x_reset_complete(pdev), 50000, 0); 219 if (ret) { 220 GT_TRACE(gt, "Wait for render reset failed\n"); 221 goto out; 222 } 223 224 out: 225 pci_write_config_byte(pdev, I915_GDRST, 0); 226 227 intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE, 0); 228 intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D); 229 230 return ret; 231 } 232 233 static int ilk_do_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask, 234 unsigned int retry) 235 { 236 struct intel_uncore *uncore = gt->uncore; 237 int ret; 238 239 intel_uncore_write_fw(uncore, ILK_GDSR, 240 ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE); 241 ret = __intel_wait_for_register_fw(uncore, ILK_GDSR, 242 ILK_GRDOM_RESET_ENABLE, 0, 243 5000, 0, 244 NULL); 245 if (ret) { 246 GT_TRACE(gt, "Wait for render reset failed\n"); 247 goto out; 248 } 249 250 intel_uncore_write_fw(uncore, ILK_GDSR, 251 ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE); 252 ret = __intel_wait_for_register_fw(uncore, ILK_GDSR, 253 ILK_GRDOM_RESET_ENABLE, 0, 254 5000, 0, 255 NULL); 256 if (ret) { 257 GT_TRACE(gt, "Wait for media reset failed\n"); 258 goto out; 259 } 260 261 out: 262 intel_uncore_write_fw(uncore, ILK_GDSR, 0); 263 intel_uncore_posting_read_fw(uncore, ILK_GDSR); 264 return ret; 265 } 266 267 /* Reset the hardware domains (GENX_GRDOM_*) specified by mask */ 268 static int gen6_hw_domain_reset(struct intel_gt *gt, u32 hw_domain_mask) 269 { 270 struct intel_uncore *uncore = gt->uncore; 271 int loops; 272 int err; 273 274 /* 275 * On some platforms, e.g. Jasperlake, we see that the engine register 276 * state is not cleared until shortly after GDRST reports completion, 277 * causing a failure as we try to immediately resume while the internal 278 * state is still in flux. If we immediately repeat the reset, the 279 * second reset appears to serialise with the first, and since it is a 280 * no-op, the registers should retain their reset value. However, there 281 * is still a concern that upon leaving the second reset, the internal 282 * engine state is still in flux and not ready for resuming. 283 * 284 * Starting on MTL, there are some prep steps that we need to do when 285 * resetting some engines that need to be applied every time we write to 286 * GEN6_GDRST. As those are time consuming (tens of ms), we don't want 287 * to perform that twice, so, since the Jasperlake issue hasn't been 288 * observed on MTL, we avoid repeating the reset on newer platforms. 289 */ 290 loops = GRAPHICS_VER_FULL(gt->i915) < IP_VER(12, 70) ? 2 : 1; 291 292 /* 293 * GEN6_GDRST is not in the gt power well, no need to check 294 * for fifo space for the write or forcewake the chip for 295 * the read 296 */ 297 do { 298 intel_uncore_write_fw(uncore, GEN6_GDRST, hw_domain_mask); 299 300 /* Wait for the device to ack the reset requests. */ 301 err = __intel_wait_for_register_fw(uncore, GEN6_GDRST, 302 hw_domain_mask, 0, 303 2000, 0, 304 NULL); 305 } while (err == 0 && --loops); 306 if (err) 307 GT_TRACE(gt, 308 "Wait for 0x%08x engines reset failed\n", 309 hw_domain_mask); 310 311 /* 312 * As we have observed that the engine state is still volatile 313 * after GDRST is acked, impose a small delay to let everything settle. 314 */ 315 udelay(50); 316 317 return err; 318 } 319 320 static int __gen6_reset_engines(struct intel_gt *gt, 321 intel_engine_mask_t engine_mask, 322 unsigned int retry) 323 { 324 struct intel_engine_cs *engine; 325 u32 hw_mask; 326 327 if (engine_mask == ALL_ENGINES) { 328 hw_mask = GEN6_GRDOM_FULL; 329 } else { 330 intel_engine_mask_t tmp; 331 332 hw_mask = 0; 333 for_each_engine_masked(engine, gt, engine_mask, tmp) { 334 hw_mask |= engine->reset_domain; 335 } 336 } 337 338 return gen6_hw_domain_reset(gt, hw_mask); 339 } 340 341 static int gen6_reset_engines(struct intel_gt *gt, 342 intel_engine_mask_t engine_mask, 343 unsigned int retry) 344 { 345 unsigned long flags; 346 int ret; 347 348 spin_lock_irqsave(>->uncore->lock, flags); 349 ret = __gen6_reset_engines(gt, engine_mask, retry); 350 spin_unlock_irqrestore(>->uncore->lock, flags); 351 352 return ret; 353 } 354 355 static struct intel_engine_cs *find_sfc_paired_vecs_engine(struct intel_engine_cs *engine) 356 { 357 int vecs_id; 358 359 GEM_BUG_ON(engine->class != VIDEO_DECODE_CLASS); 360 361 vecs_id = _VECS((engine->instance) / 2); 362 363 return engine->gt->engine[vecs_id]; 364 } 365 366 struct sfc_lock_data { 367 i915_reg_t lock_reg; 368 i915_reg_t ack_reg; 369 i915_reg_t usage_reg; 370 u32 lock_bit; 371 u32 ack_bit; 372 u32 usage_bit; 373 u32 reset_bit; 374 }; 375 376 static void get_sfc_forced_lock_data(struct intel_engine_cs *engine, 377 struct sfc_lock_data *sfc_lock) 378 { 379 switch (engine->class) { 380 default: 381 MISSING_CASE(engine->class); 382 fallthrough; 383 case VIDEO_DECODE_CLASS: 384 sfc_lock->lock_reg = GEN11_VCS_SFC_FORCED_LOCK(engine->mmio_base); 385 sfc_lock->lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT; 386 387 sfc_lock->ack_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base); 388 sfc_lock->ack_bit = GEN11_VCS_SFC_LOCK_ACK_BIT; 389 390 sfc_lock->usage_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base); 391 sfc_lock->usage_bit = GEN11_VCS_SFC_USAGE_BIT; 392 sfc_lock->reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance); 393 394 break; 395 case VIDEO_ENHANCEMENT_CLASS: 396 sfc_lock->lock_reg = GEN11_VECS_SFC_FORCED_LOCK(engine->mmio_base); 397 sfc_lock->lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT; 398 399 sfc_lock->ack_reg = GEN11_VECS_SFC_LOCK_ACK(engine->mmio_base); 400 sfc_lock->ack_bit = GEN11_VECS_SFC_LOCK_ACK_BIT; 401 402 sfc_lock->usage_reg = GEN11_VECS_SFC_USAGE(engine->mmio_base); 403 sfc_lock->usage_bit = GEN11_VECS_SFC_USAGE_BIT; 404 sfc_lock->reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance); 405 406 break; 407 } 408 } 409 410 static int gen11_lock_sfc(struct intel_engine_cs *engine, 411 u32 *reset_mask, 412 u32 *unlock_mask) 413 { 414 struct intel_uncore *uncore = engine->uncore; 415 u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access; 416 struct sfc_lock_data sfc_lock; 417 bool lock_obtained, lock_to_other = false; 418 int ret; 419 420 switch (engine->class) { 421 case VIDEO_DECODE_CLASS: 422 if ((BIT(engine->instance) & vdbox_sfc_access) == 0) 423 return 0; 424 425 fallthrough; 426 case VIDEO_ENHANCEMENT_CLASS: 427 get_sfc_forced_lock_data(engine, &sfc_lock); 428 429 break; 430 default: 431 return 0; 432 } 433 434 if (!(intel_uncore_read_fw(uncore, sfc_lock.usage_reg) & sfc_lock.usage_bit)) { 435 struct intel_engine_cs *paired_vecs; 436 437 if (engine->class != VIDEO_DECODE_CLASS || 438 GRAPHICS_VER(engine->i915) != 12) 439 return 0; 440 441 /* 442 * Wa_14010733141 443 * 444 * If the VCS-MFX isn't using the SFC, we also need to check 445 * whether VCS-HCP is using it. If so, we need to issue a *VE* 446 * forced lock on the VE engine that shares the same SFC. 447 */ 448 if (!(intel_uncore_read_fw(uncore, 449 GEN12_HCP_SFC_LOCK_STATUS(engine->mmio_base)) & 450 GEN12_HCP_SFC_USAGE_BIT)) 451 return 0; 452 453 paired_vecs = find_sfc_paired_vecs_engine(engine); 454 get_sfc_forced_lock_data(paired_vecs, &sfc_lock); 455 lock_to_other = true; 456 *unlock_mask |= paired_vecs->mask; 457 } else { 458 *unlock_mask |= engine->mask; 459 } 460 461 /* 462 * If the engine is using an SFC, tell the engine that a software reset 463 * is going to happen. The engine will then try to force lock the SFC. 464 * If SFC ends up being locked to the engine we want to reset, we have 465 * to reset it as well (we will unlock it once the reset sequence is 466 * completed). 467 */ 468 intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, 0, sfc_lock.lock_bit); 469 470 ret = __intel_wait_for_register_fw(uncore, 471 sfc_lock.ack_reg, 472 sfc_lock.ack_bit, 473 sfc_lock.ack_bit, 474 1000, 0, NULL); 475 476 /* 477 * Was the SFC released while we were trying to lock it? 478 * 479 * We should reset both the engine and the SFC if: 480 * - We were locking the SFC to this engine and the lock succeeded 481 * OR 482 * - We were locking the SFC to a different engine (Wa_14010733141) 483 * but the SFC was released before the lock was obtained. 484 * 485 * Otherwise we need only reset the engine by itself and we can 486 * leave the SFC alone. 487 */ 488 lock_obtained = (intel_uncore_read_fw(uncore, sfc_lock.usage_reg) & 489 sfc_lock.usage_bit) != 0; 490 if (lock_obtained == lock_to_other) 491 return 0; 492 493 if (ret) { 494 ENGINE_TRACE(engine, "Wait for SFC forced lock ack failed\n"); 495 return ret; 496 } 497 498 *reset_mask |= sfc_lock.reset_bit; 499 return 0; 500 } 501 502 static void gen11_unlock_sfc(struct intel_engine_cs *engine) 503 { 504 struct intel_uncore *uncore = engine->uncore; 505 u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access; 506 struct sfc_lock_data sfc_lock = {}; 507 508 if (engine->class != VIDEO_DECODE_CLASS && 509 engine->class != VIDEO_ENHANCEMENT_CLASS) 510 return; 511 512 if (engine->class == VIDEO_DECODE_CLASS && 513 (BIT(engine->instance) & vdbox_sfc_access) == 0) 514 return; 515 516 get_sfc_forced_lock_data(engine, &sfc_lock); 517 518 intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, sfc_lock.lock_bit, 0); 519 } 520 521 static int __gen11_reset_engines(struct intel_gt *gt, 522 intel_engine_mask_t engine_mask, 523 unsigned int retry) 524 { 525 struct intel_engine_cs *engine; 526 intel_engine_mask_t tmp; 527 u32 reset_mask, unlock_mask = 0; 528 int ret; 529 530 if (engine_mask == ALL_ENGINES) { 531 reset_mask = GEN11_GRDOM_FULL; 532 } else { 533 reset_mask = 0; 534 for_each_engine_masked(engine, gt, engine_mask, tmp) { 535 reset_mask |= engine->reset_domain; 536 ret = gen11_lock_sfc(engine, &reset_mask, &unlock_mask); 537 if (ret) 538 goto sfc_unlock; 539 } 540 } 541 542 ret = gen6_hw_domain_reset(gt, reset_mask); 543 544 sfc_unlock: 545 /* 546 * We unlock the SFC based on the lock status and not the result of 547 * gen11_lock_sfc to make sure that we clean properly if something 548 * wrong happened during the lock (e.g. lock acquired after timeout 549 * expiration). 550 * 551 * Due to Wa_14010733141, we may have locked an SFC to an engine that 552 * wasn't being reset. So instead of calling gen11_unlock_sfc() 553 * on engine_mask, we instead call it on the mask of engines that our 554 * gen11_lock_sfc() calls told us actually had locks attempted. 555 */ 556 for_each_engine_masked(engine, gt, unlock_mask, tmp) 557 gen11_unlock_sfc(engine); 558 559 return ret; 560 } 561 562 static int gen8_engine_reset_prepare(struct intel_engine_cs *engine) 563 { 564 struct intel_uncore *uncore = engine->uncore; 565 const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base); 566 u32 request, mask, ack; 567 int ret; 568 569 if (I915_SELFTEST_ONLY(should_fail(&engine->reset_timeout, 1))) 570 return -ETIMEDOUT; 571 572 ack = intel_uncore_read_fw(uncore, reg); 573 if (ack & RESET_CTL_CAT_ERROR) { 574 /* 575 * For catastrophic errors, ready-for-reset sequence 576 * needs to be bypassed: HAS#396813 577 */ 578 request = RESET_CTL_CAT_ERROR; 579 mask = RESET_CTL_CAT_ERROR; 580 581 /* Catastrophic errors need to be cleared by HW */ 582 ack = 0; 583 } else if (!(ack & RESET_CTL_READY_TO_RESET)) { 584 request = RESET_CTL_REQUEST_RESET; 585 mask = RESET_CTL_READY_TO_RESET; 586 ack = RESET_CTL_READY_TO_RESET; 587 } else { 588 return 0; 589 } 590 591 intel_uncore_write_fw(uncore, reg, REG_MASKED_FIELD_ENABLE(request)); 592 ret = __intel_wait_for_register_fw(uncore, reg, mask, ack, 593 700, 0, NULL); 594 if (ret) 595 gt_err(engine->gt, 596 "%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n", 597 engine->name, request, 598 intel_uncore_read_fw(uncore, reg)); 599 600 return ret; 601 } 602 603 static void gen8_engine_reset_cancel(struct intel_engine_cs *engine) 604 { 605 intel_uncore_write_fw(engine->uncore, 606 RING_RESET_CTL(engine->mmio_base), 607 REG_MASKED_FIELD_DISABLE(RESET_CTL_REQUEST_RESET)); 608 } 609 610 static int gen8_reset_engines(struct intel_gt *gt, 611 intel_engine_mask_t engine_mask, 612 unsigned int retry) 613 { 614 struct intel_engine_cs *engine; 615 const bool reset_non_ready = retry >= 1; 616 intel_engine_mask_t tmp; 617 unsigned long flags; 618 int ret; 619 620 spin_lock_irqsave(>->uncore->lock, flags); 621 622 for_each_engine_masked(engine, gt, engine_mask, tmp) { 623 ret = gen8_engine_reset_prepare(engine); 624 if (ret && !reset_non_ready) 625 goto skip_reset; 626 627 /* 628 * If this is not the first failed attempt to prepare, 629 * we decide to proceed anyway. 630 * 631 * By doing so we risk context corruption and with 632 * some gens (kbl), possible system hang if reset 633 * happens during active bb execution. 634 * 635 * We rather take context corruption instead of 636 * failed reset with a wedged driver/gpu. And 637 * active bb execution case should be covered by 638 * stop_engines() we have before the reset. 639 */ 640 } 641 642 /* 643 * Wa_22011100796:dg2, whenever Full soft reset is required, 644 * reset all individual engines firstly, and then do a full soft reset. 645 * 646 * This is best effort, so ignore any error from the initial reset. 647 */ 648 if (IS_DG2(gt->i915) && engine_mask == ALL_ENGINES) 649 __gen11_reset_engines(gt, gt->info.engine_mask, 0); 650 651 if (GRAPHICS_VER(gt->i915) >= 11) 652 ret = __gen11_reset_engines(gt, engine_mask, retry); 653 else 654 ret = __gen6_reset_engines(gt, engine_mask, retry); 655 656 skip_reset: 657 for_each_engine_masked(engine, gt, engine_mask, tmp) 658 gen8_engine_reset_cancel(engine); 659 660 spin_unlock_irqrestore(>->uncore->lock, flags); 661 662 return ret; 663 } 664 665 static int mock_reset(struct intel_gt *gt, 666 intel_engine_mask_t mask, 667 unsigned int retry) 668 { 669 return 0; 670 } 671 672 typedef int (*reset_func)(struct intel_gt *, 673 intel_engine_mask_t engine_mask, 674 unsigned int retry); 675 676 static reset_func intel_get_gpu_reset(const struct intel_gt *gt) 677 { 678 struct drm_i915_private *i915 = gt->i915; 679 680 if (is_mock_gt(gt)) 681 return mock_reset; 682 else if (GRAPHICS_VER(i915) >= 8) 683 return gen8_reset_engines; 684 else if (GRAPHICS_VER(i915) >= 6) 685 return gen6_reset_engines; 686 else if (GRAPHICS_VER(i915) >= 5) 687 return ilk_do_reset; 688 else if (IS_G4X(i915)) 689 return g4x_do_reset; 690 else if (IS_G33(i915) || IS_PINEVIEW(i915)) 691 return g33_do_reset; 692 else if (GRAPHICS_VER(i915) >= 3) 693 return i915_do_reset; 694 else 695 return NULL; 696 } 697 698 static int __reset_guc(struct intel_gt *gt) 699 { 700 u32 guc_domain = 701 GRAPHICS_VER(gt->i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC; 702 703 return gen6_hw_domain_reset(gt, guc_domain); 704 } 705 706 static bool needs_wa_14015076503(struct intel_gt *gt, intel_engine_mask_t engine_mask) 707 { 708 if (MEDIA_VER_FULL(gt->i915) != IP_VER(13, 0) || !HAS_ENGINE(gt, GSC0)) 709 return false; 710 711 if (!__HAS_ENGINE(engine_mask, GSC0)) 712 return false; 713 714 return intel_gsc_uc_fw_init_done(>->uc.gsc); 715 } 716 717 static intel_engine_mask_t 718 wa_14015076503_start(struct intel_gt *gt, intel_engine_mask_t engine_mask, bool first) 719 { 720 if (!needs_wa_14015076503(gt, engine_mask)) 721 return engine_mask; 722 723 /* 724 * wa_14015076503: if the GSC FW is loaded, we need to alert it that 725 * we're going to do a GSC engine reset and then wait for 200ms for the 726 * FW to get ready for it. However, if this is the first ALL_ENGINES 727 * reset attempt and the GSC is not busy, we can try to instead reset 728 * the GuC and all the other engines individually to avoid the 200ms 729 * wait. 730 * Skipping the GSC engine is safe because, differently from other 731 * engines, the GSCCS only role is to forward the commands to the GSC 732 * FW, so it doesn't have any HW outside of the CS itself and therefore 733 * it has no state that we don't explicitly re-init on resume or on 734 * context switch LRC or power context). The HW for the GSC uC is 735 * managed by the GSC FW so we don't need to care about that. 736 */ 737 if (engine_mask == ALL_ENGINES && first && intel_engine_is_idle(gt->engine[GSC0])) { 738 __reset_guc(gt); 739 engine_mask = gt->info.engine_mask & ~BIT(GSC0); 740 } else { 741 intel_uncore_rmw(gt->uncore, 742 HECI_H_GS1(MTL_GSC_HECI2_BASE), 743 0, HECI_H_GS1_ER_PREP); 744 745 /* make sure the reset bit is clear when writing the CSR reg */ 746 intel_uncore_rmw(gt->uncore, 747 HECI_H_CSR(MTL_GSC_HECI2_BASE), 748 HECI_H_CSR_RST, HECI_H_CSR_IG); 749 msleep(200); 750 } 751 752 return engine_mask; 753 } 754 755 static void 756 wa_14015076503_end(struct intel_gt *gt, intel_engine_mask_t engine_mask) 757 { 758 if (!needs_wa_14015076503(gt, engine_mask)) 759 return; 760 761 intel_uncore_rmw(gt->uncore, 762 HECI_H_GS1(MTL_GSC_HECI2_BASE), 763 HECI_H_GS1_ER_PREP, 0); 764 } 765 766 static int __intel_gt_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask) 767 { 768 const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1; 769 reset_func reset; 770 int ret = -ETIMEDOUT; 771 int retry; 772 773 reset = intel_get_gpu_reset(gt); 774 if (!reset) 775 return -ENODEV; 776 777 /* 778 * If the power well sleeps during the reset, the reset 779 * request may be dropped and never completes (causing -EIO). 780 */ 781 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL); 782 for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) { 783 intel_engine_mask_t reset_mask; 784 785 reset_mask = wa_14015076503_start(gt, engine_mask, !retry); 786 787 GT_TRACE(gt, "engine_mask=%x\n", reset_mask); 788 ret = reset(gt, reset_mask, retry); 789 790 wa_14015076503_end(gt, reset_mask); 791 } 792 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL); 793 794 return ret; 795 } 796 797 bool intel_has_gpu_reset(const struct intel_gt *gt) 798 { 799 if (!gt->i915->params.reset) 800 return NULL; 801 802 return intel_get_gpu_reset(gt); 803 } 804 805 bool intel_has_reset_engine(const struct intel_gt *gt) 806 { 807 if (gt->i915->params.reset < 2) 808 return false; 809 810 return INTEL_INFO(gt->i915)->has_reset_engine; 811 } 812 813 int intel_reset_guc(struct intel_gt *gt) 814 { 815 int ret; 816 817 GEM_BUG_ON(!HAS_GT_UC(gt->i915)); 818 819 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL); 820 ret = __reset_guc(gt); 821 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL); 822 823 return ret; 824 } 825 826 /* 827 * Ensure irq handler finishes, and not run again. 828 * Also return the active request so that we only search for it once. 829 */ 830 static void reset_prepare_engine(struct intel_engine_cs *engine) 831 { 832 /* 833 * During the reset sequence, we must prevent the engine from 834 * entering RC6. As the context state is undefined until we restart 835 * the engine, if it does enter RC6 during the reset, the state 836 * written to the powercontext is undefined and so we may lose 837 * GPU state upon resume, i.e. fail to restart after a reset. 838 */ 839 intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL); 840 if (engine->reset.prepare) 841 engine->reset.prepare(engine); 842 } 843 844 static void revoke_mmaps(struct intel_gt *gt) 845 { 846 int i; 847 848 for (i = 0; i < gt->ggtt->num_fences; i++) { 849 struct drm_vma_offset_node *node; 850 struct i915_vma *vma; 851 u64 vma_offset; 852 853 vma = READ_ONCE(gt->ggtt->fence_regs[i].vma); 854 if (!vma) 855 continue; 856 857 if (!i915_vma_has_userfault(vma)) 858 continue; 859 860 GEM_BUG_ON(vma->fence != >->ggtt->fence_regs[i]); 861 862 if (!vma->mmo) 863 continue; 864 865 node = &vma->mmo->vma_node; 866 vma_offset = vma->gtt_view.partial.offset << PAGE_SHIFT; 867 868 unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping, 869 drm_vma_node_offset_addr(node) + vma_offset, 870 vma->size, 871 1); 872 } 873 } 874 875 static intel_engine_mask_t reset_prepare(struct intel_gt *gt) 876 { 877 struct intel_engine_cs *engine; 878 intel_engine_mask_t awake = 0; 879 enum intel_engine_id id; 880 881 /** 882 * For GuC mode with submission enabled, ensure submission 883 * is disabled before stopping ring. 884 * 885 * For GuC mode with submission disabled, ensure that GuC is not 886 * sanitized, do that after engine reset. reset_prepare() 887 * is followed by engine reset which in this mode requires GuC to 888 * process any CSB FIFO entries generated by the resets. 889 */ 890 if (intel_uc_uses_guc_submission(>->uc)) 891 intel_uc_reset_prepare(>->uc); 892 893 for_each_engine(engine, gt, id) { 894 if (intel_engine_pm_get_if_awake(engine)) 895 awake |= engine->mask; 896 reset_prepare_engine(engine); 897 } 898 899 return awake; 900 } 901 902 static void gt_revoke(struct intel_gt *gt) 903 { 904 revoke_mmaps(gt); 905 } 906 907 static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask) 908 { 909 struct intel_engine_cs *engine; 910 enum intel_engine_id id; 911 int err; 912 913 /* 914 * Everything depends on having the GTT running, so we need to start 915 * there. 916 */ 917 err = i915_ggtt_enable_hw(gt->i915); 918 if (err) 919 return err; 920 921 local_bh_disable(); 922 for_each_engine(engine, gt, id) 923 __intel_engine_reset(engine, stalled_mask & engine->mask); 924 local_bh_enable(); 925 926 intel_uc_reset(>->uc, ALL_ENGINES); 927 928 intel_ggtt_restore_fences(gt->ggtt); 929 930 return err; 931 } 932 933 static void reset_finish_engine(struct intel_engine_cs *engine) 934 { 935 if (engine->reset.finish) 936 engine->reset.finish(engine); 937 intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL); 938 939 intel_engine_signal_breadcrumbs(engine); 940 } 941 942 static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake) 943 { 944 struct intel_engine_cs *engine; 945 enum intel_engine_id id; 946 947 for_each_engine(engine, gt, id) { 948 reset_finish_engine(engine); 949 if (awake & engine->mask) 950 intel_engine_pm_put(engine); 951 } 952 953 intel_uc_reset_finish(>->uc); 954 } 955 956 static void nop_submit_request(struct i915_request *request) 957 { 958 RQ_TRACE(request, "-EIO\n"); 959 960 request = i915_request_mark_eio(request); 961 if (request) { 962 i915_request_submit(request); 963 intel_engine_signal_breadcrumbs(request->engine); 964 965 i915_request_put(request); 966 } 967 } 968 969 static void __intel_gt_set_wedged(struct intel_gt *gt) 970 { 971 struct intel_display *display = gt->i915->display; 972 struct intel_engine_cs *engine; 973 intel_engine_mask_t awake; 974 enum intel_engine_id id; 975 976 if (test_bit(I915_WEDGED, >->reset.flags)) 977 return; 978 979 GT_TRACE(gt, "start\n"); 980 981 /* 982 * First, stop submission to hw, but do not yet complete requests by 983 * rolling the global seqno forward (since this would complete requests 984 * for which we haven't set the fence error to EIO yet). 985 */ 986 awake = reset_prepare(gt); 987 988 /* Even if the GPU reset fails, it should still stop the engines */ 989 if (!intel_gt_gpu_reset_clobbers_display(gt) && 990 !intel_display_reset_test(display)) 991 intel_gt_reset_all_engines(gt); 992 993 for_each_engine(engine, gt, id) 994 engine->submit_request = nop_submit_request; 995 996 /* 997 * Make sure no request can slip through without getting completed by 998 * either this call here to intel_engine_write_global_seqno, or the one 999 * in nop_submit_request. 1000 */ 1001 synchronize_rcu_expedited(); 1002 set_bit(I915_WEDGED, >->reset.flags); 1003 1004 /* Mark all executing requests as skipped */ 1005 local_bh_disable(); 1006 for_each_engine(engine, gt, id) 1007 if (engine->reset.cancel) 1008 engine->reset.cancel(engine); 1009 intel_uc_cancel_requests(>->uc); 1010 local_bh_enable(); 1011 1012 reset_finish(gt, awake); 1013 1014 GT_TRACE(gt, "end\n"); 1015 } 1016 1017 static void set_wedged_work(struct work_struct *w) 1018 { 1019 struct intel_gt *gt = container_of(w, struct intel_gt, wedge); 1020 intel_wakeref_t wf; 1021 1022 with_intel_runtime_pm(gt->uncore->rpm, wf) 1023 __intel_gt_set_wedged(gt); 1024 } 1025 1026 void intel_gt_set_wedged(struct intel_gt *gt) 1027 { 1028 intel_wakeref_t wakeref; 1029 1030 if (test_bit(I915_WEDGED, >->reset.flags)) 1031 return; 1032 1033 wakeref = intel_runtime_pm_get(gt->uncore->rpm); 1034 mutex_lock(>->reset.mutex); 1035 1036 if (GEM_SHOW_DEBUG()) { 1037 struct drm_printer p = drm_dbg_printer(>->i915->drm, 1038 DRM_UT_DRIVER, NULL); 1039 struct intel_engine_cs *engine; 1040 enum intel_engine_id id; 1041 1042 drm_printf(&p, "called from %pS\n", (void *)_RET_IP_); 1043 for_each_engine(engine, gt, id) { 1044 if (intel_engine_is_idle(engine)) 1045 continue; 1046 1047 intel_engine_dump(engine, &p, "%s\n", engine->name); 1048 } 1049 } 1050 1051 __intel_gt_set_wedged(gt); 1052 1053 mutex_unlock(>->reset.mutex); 1054 intel_runtime_pm_put(gt->uncore->rpm, wakeref); 1055 } 1056 1057 static bool __intel_gt_unset_wedged(struct intel_gt *gt) 1058 { 1059 struct intel_gt_timelines *timelines = >->timelines; 1060 struct intel_timeline *tl; 1061 bool ok; 1062 1063 if (!test_bit(I915_WEDGED, >->reset.flags)) 1064 return true; 1065 1066 /* Never fully initialised, recovery impossible */ 1067 if (intel_gt_has_unrecoverable_error(gt)) 1068 return false; 1069 1070 GT_TRACE(gt, "start\n"); 1071 1072 /* 1073 * Before unwedging, make sure that all pending operations 1074 * are flushed and errored out - we may have requests waiting upon 1075 * third party fences. We marked all inflight requests as EIO, and 1076 * every execbuf since returned EIO, for consistency we want all 1077 * the currently pending requests to also be marked as EIO, which 1078 * is done inside our nop_submit_request - and so we must wait. 1079 * 1080 * No more can be submitted until we reset the wedged bit. 1081 */ 1082 spin_lock(&timelines->lock); 1083 list_for_each_entry(tl, &timelines->active_list, link) { 1084 struct dma_fence *fence; 1085 1086 fence = i915_active_fence_get(&tl->last_request); 1087 if (!fence) 1088 continue; 1089 1090 spin_unlock(&timelines->lock); 1091 1092 /* 1093 * All internal dependencies (i915_requests) will have 1094 * been flushed by the set-wedge, but we may be stuck waiting 1095 * for external fences. These should all be capped to 10s 1096 * (I915_FENCE_TIMEOUT) so this wait should not be unbounded 1097 * in the worst case. 1098 */ 1099 dma_fence_default_wait(fence, false, MAX_SCHEDULE_TIMEOUT); 1100 dma_fence_put(fence); 1101 1102 /* Restart iteration after dropping lock */ 1103 spin_lock(&timelines->lock); 1104 tl = list_entry(&timelines->active_list, typeof(*tl), link); 1105 } 1106 spin_unlock(&timelines->lock); 1107 1108 /* We must reset pending GPU events before restoring our submission */ 1109 ok = !HAS_EXECLISTS(gt->i915); /* XXX better agnosticism desired */ 1110 if (!intel_gt_gpu_reset_clobbers_display(gt)) 1111 ok = intel_gt_reset_all_engines(gt) == 0; 1112 if (!ok) { 1113 /* 1114 * Warn CI about the unrecoverable wedged condition. 1115 * Time for a reboot. 1116 */ 1117 add_taint_for_CI(gt->i915, TAINT_WARN); 1118 return false; 1119 } 1120 1121 /* 1122 * Undo nop_submit_request. We prevent all new i915 requests from 1123 * being queued (by disallowing execbuf whilst wedged) so having 1124 * waited for all active requests above, we know the system is idle 1125 * and do not have to worry about a thread being inside 1126 * engine->submit_request() as we swap over. So unlike installing 1127 * the nop_submit_request on reset, we can do this from normal 1128 * context and do not require stop_machine(). 1129 */ 1130 intel_engines_reset_default_submission(gt); 1131 1132 GT_TRACE(gt, "end\n"); 1133 1134 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ 1135 clear_bit(I915_WEDGED, >->reset.flags); 1136 1137 return true; 1138 } 1139 1140 bool intel_gt_unset_wedged(struct intel_gt *gt) 1141 { 1142 bool result; 1143 1144 mutex_lock(>->reset.mutex); 1145 result = __intel_gt_unset_wedged(gt); 1146 mutex_unlock(>->reset.mutex); 1147 1148 return result; 1149 } 1150 1151 static int do_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask) 1152 { 1153 int err, i; 1154 1155 err = intel_gt_reset_all_engines(gt); 1156 for (i = 0; err && i < RESET_MAX_RETRIES; i++) { 1157 msleep(10 * (i + 1)); 1158 err = intel_gt_reset_all_engines(gt); 1159 } 1160 if (err) 1161 return err; 1162 1163 return gt_reset(gt, stalled_mask); 1164 } 1165 1166 static int resume(struct intel_gt *gt) 1167 { 1168 struct intel_engine_cs *engine; 1169 enum intel_engine_id id; 1170 int ret; 1171 1172 for_each_engine(engine, gt, id) { 1173 ret = intel_engine_resume(engine); 1174 if (ret) 1175 return ret; 1176 } 1177 1178 return 0; 1179 } 1180 1181 bool intel_gt_gpu_reset_clobbers_display(struct intel_gt *gt) 1182 { 1183 struct drm_i915_private *i915 = gt->i915; 1184 1185 return INTEL_INFO(i915)->gpu_reset_clobbers_display; 1186 } 1187 1188 /** 1189 * intel_gt_reset - reset chip after a hang 1190 * @gt: #intel_gt to reset 1191 * @stalled_mask: mask of the stalled engines with the guilty requests 1192 * @reason: user error message for why we are resetting 1193 * 1194 * Reset the chip. Useful if a hang is detected. Marks the device as wedged 1195 * on failure. 1196 * 1197 * Procedure is fairly simple: 1198 * - reset the chip using the reset reg 1199 * - re-init context state 1200 * - re-init hardware status page 1201 * - re-init ring buffer 1202 * - re-init interrupt state 1203 * - re-init display 1204 */ 1205 void intel_gt_reset(struct intel_gt *gt, 1206 intel_engine_mask_t stalled_mask, 1207 const char *reason) 1208 { 1209 struct intel_display *display = gt->i915->display; 1210 intel_engine_mask_t awake; 1211 int ret; 1212 1213 GT_TRACE(gt, "flags=%lx\n", gt->reset.flags); 1214 1215 might_sleep(); 1216 GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, >->reset.flags)); 1217 1218 /* 1219 * FIXME: Revoking cpu mmap ptes cannot be done from a dma_fence 1220 * critical section like gpu reset. 1221 */ 1222 gt_revoke(gt); 1223 1224 mutex_lock(>->reset.mutex); 1225 1226 /* Clear any previous failed attempts at recovery. Time to try again. */ 1227 if (!__intel_gt_unset_wedged(gt)) 1228 goto unlock; 1229 1230 if (reason) 1231 gt_notice(gt, "Resetting chip for %s\n", reason); 1232 atomic_inc(>->i915->gpu_error.reset_count); 1233 1234 awake = reset_prepare(gt); 1235 1236 if (!intel_has_gpu_reset(gt)) { 1237 if (gt->i915->params.reset) 1238 gt_err(gt, "GPU reset not supported\n"); 1239 else 1240 gt_dbg(gt, "GPU reset disabled\n"); 1241 goto error; 1242 } 1243 1244 if (intel_gt_gpu_reset_clobbers_display(gt)) 1245 intel_irq_suspend(gt->i915); 1246 1247 if (do_reset(gt, stalled_mask)) { 1248 gt_err(gt, "Failed to reset chip\n"); 1249 goto taint; 1250 } 1251 1252 if (intel_gt_gpu_reset_clobbers_display(gt)) 1253 intel_irq_resume(gt->i915); 1254 1255 intel_overlay_reset(display); 1256 1257 /* sanitize uC after engine reset */ 1258 if (!intel_uc_uses_guc_submission(>->uc)) 1259 intel_uc_reset_prepare(>->uc); 1260 /* 1261 * Next we need to restore the context, but we don't use those 1262 * yet either... 1263 * 1264 * Ring buffer needs to be re-initialized in the KMS case, or if X 1265 * was running at the time of the reset (i.e. we weren't VT 1266 * switched away). 1267 */ 1268 ret = intel_gt_init_hw(gt); 1269 if (ret) { 1270 gt_err(gt, "Failed to initialise HW following reset (%d)\n", ret); 1271 goto taint; 1272 } 1273 1274 ret = resume(gt); 1275 if (ret) 1276 goto taint; 1277 1278 finish: 1279 reset_finish(gt, awake); 1280 unlock: 1281 mutex_unlock(>->reset.mutex); 1282 return; 1283 1284 taint: 1285 /* 1286 * History tells us that if we cannot reset the GPU now, we 1287 * never will. This then impacts everything that is run 1288 * subsequently. On failing the reset, we mark the driver 1289 * as wedged, preventing further execution on the GPU. 1290 * We also want to go one step further and add a taint to the 1291 * kernel so that any subsequent faults can be traced back to 1292 * this failure. This is important for CI, where if the 1293 * GPU/driver fails we would like to reboot and restart testing 1294 * rather than continue on into oblivion. For everyone else, 1295 * the system should still plod along, but they have been warned! 1296 */ 1297 add_taint_for_CI(gt->i915, TAINT_WARN); 1298 error: 1299 __intel_gt_set_wedged(gt); 1300 goto finish; 1301 } 1302 1303 /** 1304 * intel_gt_reset_all_engines() - Reset all engines in the given gt. 1305 * @gt: the GT to reset all engines for. 1306 * 1307 * This function resets all engines within the given gt. 1308 * 1309 * Returns: 1310 * Zero on success, negative error code on failure. 1311 */ 1312 int intel_gt_reset_all_engines(struct intel_gt *gt) 1313 { 1314 return __intel_gt_reset(gt, ALL_ENGINES); 1315 } 1316 1317 /** 1318 * intel_gt_reset_engine() - Reset a specific engine within a gt. 1319 * @engine: engine to be reset. 1320 * 1321 * This function resets the specified engine within a gt. 1322 * 1323 * Returns: 1324 * Zero on success, negative error code on failure. 1325 */ 1326 int intel_gt_reset_engine(struct intel_engine_cs *engine) 1327 { 1328 return __intel_gt_reset(engine->gt, engine->mask); 1329 } 1330 1331 int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg) 1332 { 1333 struct intel_gt *gt = engine->gt; 1334 int ret; 1335 1336 ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags); 1337 GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, >->reset.flags)); 1338 1339 if (intel_engine_uses_guc(engine)) 1340 return -ENODEV; 1341 1342 if (!intel_engine_pm_get_if_awake(engine)) 1343 return 0; 1344 1345 reset_prepare_engine(engine); 1346 1347 if (msg) 1348 drm_notice(&engine->i915->drm, 1349 "Resetting %s for %s\n", engine->name, msg); 1350 i915_increase_reset_engine_count(&engine->i915->gpu_error, engine); 1351 1352 ret = intel_gt_reset_engine(engine); 1353 if (ret) { 1354 /* If we fail here, we expect to fallback to a global reset */ 1355 ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret); 1356 goto out; 1357 } 1358 1359 /* 1360 * The request that caused the hang is stuck on elsp, we know the 1361 * active request and can drop it, adjust head to skip the offending 1362 * request to resume executing remaining requests in the queue. 1363 */ 1364 __intel_engine_reset(engine, true); 1365 1366 /* 1367 * The engine and its registers (and workarounds in case of render) 1368 * have been reset to their default values. Follow the init_ring 1369 * process to program RING_MODE, HWSP and re-enable submission. 1370 */ 1371 ret = intel_engine_resume(engine); 1372 1373 out: 1374 intel_engine_cancel_stop_cs(engine); 1375 reset_finish_engine(engine); 1376 intel_engine_pm_put_async(engine); 1377 return ret; 1378 } 1379 1380 /** 1381 * intel_engine_reset - reset GPU engine to recover from a hang 1382 * @engine: engine to reset 1383 * @msg: reason for GPU reset; or NULL for no drm_notice() 1384 * 1385 * Reset a specific GPU engine. Useful if a hang is detected. 1386 * Returns zero on successful reset or otherwise an error code. 1387 * 1388 * Procedure is: 1389 * - identifies the request that caused the hang and it is dropped 1390 * - reset engine (which will force the engine to idle) 1391 * - re-init/configure engine 1392 */ 1393 int intel_engine_reset(struct intel_engine_cs *engine, const char *msg) 1394 { 1395 int err; 1396 1397 local_bh_disable(); 1398 err = __intel_engine_reset_bh(engine, msg); 1399 local_bh_enable(); 1400 1401 return err; 1402 } 1403 1404 static void intel_gt_reset_global(struct intel_gt *gt, 1405 u32 engine_mask, 1406 const char *reason) 1407 { 1408 struct kobject *kobj = >->i915->drm.primary->kdev->kobj; 1409 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL }; 1410 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL }; 1411 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL }; 1412 struct intel_wedge_me w; 1413 1414 kobject_uevent_env(kobj, KOBJ_CHANGE, error_event); 1415 1416 GT_TRACE(gt, "resetting chip, engines=%x\n", engine_mask); 1417 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event); 1418 1419 /* Use a watchdog to ensure that our reset completes */ 1420 intel_wedge_on_timeout(&w, gt, 60 * HZ) { 1421 struct drm_i915_private *i915 = gt->i915; 1422 struct intel_display *display = i915->display; 1423 bool need_display_reset; 1424 bool reset_display; 1425 1426 need_display_reset = 1427 intel_display_reset_supported(display) && 1428 intel_gt_gpu_reset_clobbers_display(gt) && 1429 intel_has_gpu_reset(gt); 1430 1431 reset_display = 1432 intel_display_reset_test(display) || 1433 need_display_reset; 1434 1435 if (reset_display) { 1436 if (atomic_read(&i915->pending_fb_pin)) { 1437 drm_dbg_kms(&i915->drm, 1438 "Modeset potentially stuck, unbreaking through wedging\n"); 1439 1440 intel_gt_set_wedged(gt); 1441 } 1442 1443 intel_display_reset_prepare(display); 1444 } 1445 1446 intel_gt_reset(gt, engine_mask, reason); 1447 1448 if (reset_display) 1449 intel_display_reset_finish(display, !need_display_reset); 1450 } 1451 1452 if (!test_bit(I915_WEDGED, >->reset.flags)) 1453 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event); 1454 else 1455 drm_dev_wedged_event(>->i915->drm, 1456 DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET, 1457 NULL); 1458 } 1459 1460 /** 1461 * intel_gt_handle_error - handle a gpu error 1462 * @gt: the intel_gt 1463 * @engine_mask: mask representing engines that are hung 1464 * @flags: control flags 1465 * @fmt: Error message format string 1466 * 1467 * Do some basic checking of register state at error time and 1468 * dump it to the syslog. Also call i915_capture_error_state() to make 1469 * sure we get a record and make it available in debugfs. Fire a uevent 1470 * so userspace knows something bad happened (should trigger collection 1471 * of a ring dump etc.). 1472 */ 1473 void intel_gt_handle_error(struct intel_gt *gt, 1474 intel_engine_mask_t engine_mask, 1475 unsigned long flags, 1476 const char *fmt, ...) 1477 { 1478 struct intel_engine_cs *engine; 1479 intel_wakeref_t wakeref; 1480 intel_engine_mask_t tmp; 1481 char error_msg[80]; 1482 char *msg = NULL; 1483 1484 if (fmt) { 1485 va_list args; 1486 1487 va_start(args, fmt); 1488 vscnprintf(error_msg, sizeof(error_msg), fmt, args); 1489 va_end(args); 1490 1491 msg = error_msg; 1492 } 1493 1494 /* 1495 * In most cases it's guaranteed that we get here with an RPM 1496 * reference held, for example because there is a pending GPU 1497 * request that won't finish until the reset is done. This 1498 * isn't the case at least when we get here by doing a 1499 * simulated reset via debugfs, so get an RPM reference. 1500 */ 1501 wakeref = intel_runtime_pm_get(gt->uncore->rpm); 1502 1503 engine_mask &= gt->info.engine_mask; 1504 1505 if (flags & I915_ERROR_CAPTURE) { 1506 i915_capture_error_state(gt, engine_mask, CORE_DUMP_FLAG_NONE); 1507 intel_gt_clear_error_registers(gt, engine_mask); 1508 } 1509 1510 /* 1511 * Try engine reset when available. We fall back to full reset if 1512 * single reset fails. Display reset test needs a full reset. 1513 */ 1514 if (!intel_display_reset_test(gt->i915->display) && 1515 !intel_uc_uses_guc_submission(>->uc) && 1516 intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) { 1517 local_bh_disable(); 1518 for_each_engine_masked(engine, gt, engine_mask, tmp) { 1519 BUILD_BUG_ON(I915_RESET_BACKOFF >= I915_RESET_ENGINE); 1520 if (test_and_set_bit(I915_RESET_ENGINE + engine->id, 1521 >->reset.flags)) 1522 continue; 1523 1524 if (__intel_engine_reset_bh(engine, msg) == 0) 1525 engine_mask &= ~engine->mask; 1526 1527 clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id, 1528 >->reset.flags); 1529 } 1530 local_bh_enable(); 1531 } 1532 1533 if (!engine_mask) 1534 goto out; 1535 1536 /* Full reset needs the mutex, stop any other user trying to do so. */ 1537 if (test_and_set_bit(I915_RESET_BACKOFF, >->reset.flags)) { 1538 wait_event(gt->reset.queue, 1539 !test_bit(I915_RESET_BACKOFF, >->reset.flags)); 1540 goto out; /* piggy-back on the other reset */ 1541 } 1542 1543 /* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */ 1544 synchronize_rcu_expedited(); 1545 1546 /* 1547 * Prevent any other reset-engine attempt. We don't do this for GuC 1548 * submission the GuC owns the per-engine reset, not the i915. 1549 */ 1550 if (!intel_uc_uses_guc_submission(>->uc)) { 1551 for_each_engine(engine, gt, tmp) { 1552 while (test_and_set_bit(I915_RESET_ENGINE + engine->id, 1553 >->reset.flags)) 1554 wait_on_bit(>->reset.flags, 1555 I915_RESET_ENGINE + engine->id, 1556 TASK_UNINTERRUPTIBLE); 1557 } 1558 } 1559 1560 /* Flush everyone using a resource about to be clobbered */ 1561 synchronize_srcu_expedited(>->reset.backoff_srcu); 1562 1563 intel_gt_reset_global(gt, engine_mask, msg); 1564 1565 if (!intel_uc_uses_guc_submission(>->uc)) { 1566 for_each_engine(engine, gt, tmp) 1567 clear_bit_unlock(I915_RESET_ENGINE + engine->id, 1568 >->reset.flags); 1569 } 1570 clear_bit_unlock(I915_RESET_BACKOFF, >->reset.flags); 1571 smp_mb__after_atomic(); 1572 wake_up_all(>->reset.queue); 1573 1574 out: 1575 intel_runtime_pm_put(gt->uncore->rpm, wakeref); 1576 } 1577 1578 static int _intel_gt_reset_lock(struct intel_gt *gt, int *srcu, bool retry) 1579 { 1580 might_lock(>->reset.backoff_srcu); 1581 if (retry) 1582 might_sleep(); 1583 1584 rcu_read_lock(); 1585 while (test_bit(I915_RESET_BACKOFF, >->reset.flags)) { 1586 rcu_read_unlock(); 1587 1588 if (!retry) 1589 return -EBUSY; 1590 1591 if (wait_event_interruptible(gt->reset.queue, 1592 !test_bit(I915_RESET_BACKOFF, 1593 >->reset.flags))) 1594 return -EINTR; 1595 1596 rcu_read_lock(); 1597 } 1598 *srcu = srcu_read_lock(>->reset.backoff_srcu); 1599 rcu_read_unlock(); 1600 1601 return 0; 1602 } 1603 1604 int intel_gt_reset_trylock(struct intel_gt *gt, int *srcu) 1605 { 1606 return _intel_gt_reset_lock(gt, srcu, false); 1607 } 1608 1609 int intel_gt_reset_lock_interruptible(struct intel_gt *gt, int *srcu) 1610 { 1611 return _intel_gt_reset_lock(gt, srcu, true); 1612 } 1613 1614 void intel_gt_reset_unlock(struct intel_gt *gt, int tag) 1615 __releases(>->reset.backoff_srcu) 1616 { 1617 srcu_read_unlock(>->reset.backoff_srcu, tag); 1618 } 1619 1620 int intel_gt_terminally_wedged(struct intel_gt *gt) 1621 { 1622 might_sleep(); 1623 1624 if (!intel_gt_is_wedged(gt)) 1625 return 0; 1626 1627 if (intel_gt_has_unrecoverable_error(gt)) 1628 return -EIO; 1629 1630 /* Reset still in progress? Maybe we will recover? */ 1631 if (wait_event_interruptible(gt->reset.queue, 1632 !test_bit(I915_RESET_BACKOFF, 1633 >->reset.flags))) 1634 return -EINTR; 1635 1636 return intel_gt_is_wedged(gt) ? -EIO : 0; 1637 } 1638 1639 void intel_gt_set_wedged_on_init(struct intel_gt *gt) 1640 { 1641 BUILD_BUG_ON(I915_RESET_ENGINE + I915_NUM_ENGINES > 1642 I915_WEDGED_ON_INIT); 1643 intel_gt_set_wedged(gt); 1644 i915_disable_error_state(gt->i915, -ENODEV); 1645 set_bit(I915_WEDGED_ON_INIT, >->reset.flags); 1646 1647 /* Wedged on init is non-recoverable */ 1648 add_taint_for_CI(gt->i915, TAINT_WARN); 1649 } 1650 1651 void intel_gt_set_wedged_on_fini(struct intel_gt *gt) 1652 { 1653 intel_gt_set_wedged(gt); 1654 i915_disable_error_state(gt->i915, -ENODEV); 1655 set_bit(I915_WEDGED_ON_FINI, >->reset.flags); 1656 intel_gt_retire_requests(gt); /* cleanup any wedged requests */ 1657 } 1658 1659 void intel_gt_init_reset(struct intel_gt *gt) 1660 { 1661 init_waitqueue_head(>->reset.queue); 1662 mutex_init(>->reset.mutex); 1663 init_srcu_struct(>->reset.backoff_srcu); 1664 INIT_WORK(>->wedge, set_wedged_work); 1665 1666 /* 1667 * While undesirable to wait inside the shrinker, complain anyway. 1668 * 1669 * If we have to wait during shrinking, we guarantee forward progress 1670 * by forcing the reset. Therefore during the reset we must not 1671 * re-enter the shrinker. By declaring that we take the reset mutex 1672 * within the shrinker, we forbid ourselves from performing any 1673 * fs-reclaim or taking related locks during reset. 1674 */ 1675 i915_gem_shrinker_taints_mutex(gt->i915, >->reset.mutex); 1676 1677 /* no GPU until we are ready! */ 1678 __set_bit(I915_WEDGED, >->reset.flags); 1679 } 1680 1681 void intel_gt_fini_reset(struct intel_gt *gt) 1682 { 1683 cleanup_srcu_struct(>->reset.backoff_srcu); 1684 } 1685 1686 static void intel_wedge_me(struct work_struct *work) 1687 { 1688 struct intel_wedge_me *w = container_of(work, typeof(*w), work.work); 1689 1690 gt_err(w->gt, "%s timed out, cancelling all in-flight rendering.\n", w->name); 1691 set_wedged_work(&w->gt->wedge); 1692 } 1693 1694 void __intel_init_wedge(struct intel_wedge_me *w, 1695 struct intel_gt *gt, 1696 long timeout, 1697 const char *name) 1698 { 1699 w->gt = gt; 1700 w->name = name; 1701 1702 INIT_DELAYED_WORK_ONSTACK(&w->work, intel_wedge_me); 1703 queue_delayed_work(gt->i915->unordered_wq, &w->work, timeout); 1704 } 1705 1706 void __intel_fini_wedge(struct intel_wedge_me *w) 1707 { 1708 cancel_delayed_work_sync(&w->work); 1709 destroy_delayed_work_on_stack(&w->work); 1710 w->gt = NULL; 1711 } 1712 1713 /* 1714 * Wa_22011802037 requires that we (or the GuC) ensure that no command 1715 * streamers are executing MI_FORCE_WAKE while an engine reset is initiated. 1716 */ 1717 bool intel_engine_reset_needs_wa_22011802037(struct intel_gt *gt) 1718 { 1719 if (GRAPHICS_VER(gt->i915) < 11) 1720 return false; 1721 1722 if (IS_GFX_GT_IP_STEP(gt, IP_VER(12, 70), STEP_A0, STEP_B0)) 1723 return true; 1724 1725 if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70)) 1726 return false; 1727 1728 return true; 1729 } 1730 1731 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 1732 #include "selftest_reset.c" 1733 #include "selftest_hangcheck.c" 1734 #endif 1735