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