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