1 /* 2 * Copyright © 2016 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 */ 24 25 #include <drm/drm_print.h> 26 27 #include "gem/i915_gem_context.h" 28 29 #include "i915_drv.h" 30 31 #include "intel_context.h" 32 #include "intel_engine.h" 33 #include "intel_engine_pm.h" 34 #include "intel_engine_pool.h" 35 #include "intel_engine_user.h" 36 #include "intel_gt.h" 37 #include "intel_gt_requests.h" 38 #include "intel_gt_pm.h" 39 #include "intel_lrc.h" 40 #include "intel_reset.h" 41 #include "intel_ring.h" 42 43 /* Haswell does have the CXT_SIZE register however it does not appear to be 44 * valid. Now, docs explain in dwords what is in the context object. The full 45 * size is 70720 bytes, however, the power context and execlist context will 46 * never be saved (power context is stored elsewhere, and execlists don't work 47 * on HSW) - so the final size, including the extra state required for the 48 * Resource Streamer, is 66944 bytes, which rounds to 17 pages. 49 */ 50 #define HSW_CXT_TOTAL_SIZE (17 * PAGE_SIZE) 51 52 #define DEFAULT_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE) 53 #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE) 54 #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE) 55 #define GEN10_LR_CONTEXT_RENDER_SIZE (18 * PAGE_SIZE) 56 #define GEN11_LR_CONTEXT_RENDER_SIZE (14 * PAGE_SIZE) 57 58 #define GEN8_LR_CONTEXT_OTHER_SIZE ( 2 * PAGE_SIZE) 59 60 #define MAX_MMIO_BASES 3 61 struct engine_info { 62 unsigned int hw_id; 63 u8 class; 64 u8 instance; 65 /* mmio bases table *must* be sorted in reverse gen order */ 66 struct engine_mmio_base { 67 u32 gen : 8; 68 u32 base : 24; 69 } mmio_bases[MAX_MMIO_BASES]; 70 }; 71 72 static const struct engine_info intel_engines[] = { 73 [RCS0] = { 74 .hw_id = RCS0_HW, 75 .class = RENDER_CLASS, 76 .instance = 0, 77 .mmio_bases = { 78 { .gen = 1, .base = RENDER_RING_BASE } 79 }, 80 }, 81 [BCS0] = { 82 .hw_id = BCS0_HW, 83 .class = COPY_ENGINE_CLASS, 84 .instance = 0, 85 .mmio_bases = { 86 { .gen = 6, .base = BLT_RING_BASE } 87 }, 88 }, 89 [VCS0] = { 90 .hw_id = VCS0_HW, 91 .class = VIDEO_DECODE_CLASS, 92 .instance = 0, 93 .mmio_bases = { 94 { .gen = 11, .base = GEN11_BSD_RING_BASE }, 95 { .gen = 6, .base = GEN6_BSD_RING_BASE }, 96 { .gen = 4, .base = BSD_RING_BASE } 97 }, 98 }, 99 [VCS1] = { 100 .hw_id = VCS1_HW, 101 .class = VIDEO_DECODE_CLASS, 102 .instance = 1, 103 .mmio_bases = { 104 { .gen = 11, .base = GEN11_BSD2_RING_BASE }, 105 { .gen = 8, .base = GEN8_BSD2_RING_BASE } 106 }, 107 }, 108 [VCS2] = { 109 .hw_id = VCS2_HW, 110 .class = VIDEO_DECODE_CLASS, 111 .instance = 2, 112 .mmio_bases = { 113 { .gen = 11, .base = GEN11_BSD3_RING_BASE } 114 }, 115 }, 116 [VCS3] = { 117 .hw_id = VCS3_HW, 118 .class = VIDEO_DECODE_CLASS, 119 .instance = 3, 120 .mmio_bases = { 121 { .gen = 11, .base = GEN11_BSD4_RING_BASE } 122 }, 123 }, 124 [VECS0] = { 125 .hw_id = VECS0_HW, 126 .class = VIDEO_ENHANCEMENT_CLASS, 127 .instance = 0, 128 .mmio_bases = { 129 { .gen = 11, .base = GEN11_VEBOX_RING_BASE }, 130 { .gen = 7, .base = VEBOX_RING_BASE } 131 }, 132 }, 133 [VECS1] = { 134 .hw_id = VECS1_HW, 135 .class = VIDEO_ENHANCEMENT_CLASS, 136 .instance = 1, 137 .mmio_bases = { 138 { .gen = 11, .base = GEN11_VEBOX2_RING_BASE } 139 }, 140 }, 141 }; 142 143 /** 144 * intel_engine_context_size() - return the size of the context for an engine 145 * @gt: the gt 146 * @class: engine class 147 * 148 * Each engine class may require a different amount of space for a context 149 * image. 150 * 151 * Return: size (in bytes) of an engine class specific context image 152 * 153 * Note: this size includes the HWSP, which is part of the context image 154 * in LRC mode, but does not include the "shared data page" used with 155 * GuC submission. The caller should account for this if using the GuC. 156 */ 157 u32 intel_engine_context_size(struct intel_gt *gt, u8 class) 158 { 159 struct intel_uncore *uncore = gt->uncore; 160 u32 cxt_size; 161 162 BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE); 163 164 switch (class) { 165 case RENDER_CLASS: 166 switch (INTEL_GEN(gt->i915)) { 167 default: 168 MISSING_CASE(INTEL_GEN(gt->i915)); 169 return DEFAULT_LR_CONTEXT_RENDER_SIZE; 170 case 12: 171 case 11: 172 return GEN11_LR_CONTEXT_RENDER_SIZE; 173 case 10: 174 return GEN10_LR_CONTEXT_RENDER_SIZE; 175 case 9: 176 return GEN9_LR_CONTEXT_RENDER_SIZE; 177 case 8: 178 return GEN8_LR_CONTEXT_RENDER_SIZE; 179 case 7: 180 if (IS_HASWELL(gt->i915)) 181 return HSW_CXT_TOTAL_SIZE; 182 183 cxt_size = intel_uncore_read(uncore, GEN7_CXT_SIZE); 184 return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64, 185 PAGE_SIZE); 186 case 6: 187 cxt_size = intel_uncore_read(uncore, CXT_SIZE); 188 return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64, 189 PAGE_SIZE); 190 case 5: 191 case 4: 192 /* 193 * There is a discrepancy here between the size reported 194 * by the register and the size of the context layout 195 * in the docs. Both are described as authorative! 196 * 197 * The discrepancy is on the order of a few cachelines, 198 * but the total is under one page (4k), which is our 199 * minimum allocation anyway so it should all come 200 * out in the wash. 201 */ 202 cxt_size = intel_uncore_read(uncore, CXT_SIZE) + 1; 203 drm_dbg(>->i915->drm, 204 "gen%d CXT_SIZE = %d bytes [0x%08x]\n", 205 INTEL_GEN(gt->i915), cxt_size * 64, 206 cxt_size - 1); 207 return round_up(cxt_size * 64, PAGE_SIZE); 208 case 3: 209 case 2: 210 /* For the special day when i810 gets merged. */ 211 case 1: 212 return 0; 213 } 214 break; 215 default: 216 MISSING_CASE(class); 217 /* fall through */ 218 case VIDEO_DECODE_CLASS: 219 case VIDEO_ENHANCEMENT_CLASS: 220 case COPY_ENGINE_CLASS: 221 if (INTEL_GEN(gt->i915) < 8) 222 return 0; 223 return GEN8_LR_CONTEXT_OTHER_SIZE; 224 } 225 } 226 227 static u32 __engine_mmio_base(struct drm_i915_private *i915, 228 const struct engine_mmio_base *bases) 229 { 230 int i; 231 232 for (i = 0; i < MAX_MMIO_BASES; i++) 233 if (INTEL_GEN(i915) >= bases[i].gen) 234 break; 235 236 GEM_BUG_ON(i == MAX_MMIO_BASES); 237 GEM_BUG_ON(!bases[i].base); 238 239 return bases[i].base; 240 } 241 242 static void __sprint_engine_name(struct intel_engine_cs *engine) 243 { 244 /* 245 * Before we know what the uABI name for this engine will be, 246 * we still would like to keep track of this engine in the debug logs. 247 * We throw in a ' here as a reminder that this isn't its final name. 248 */ 249 GEM_WARN_ON(snprintf(engine->name, sizeof(engine->name), "%s'%u", 250 intel_engine_class_repr(engine->class), 251 engine->instance) >= sizeof(engine->name)); 252 } 253 254 void intel_engine_set_hwsp_writemask(struct intel_engine_cs *engine, u32 mask) 255 { 256 /* 257 * Though they added more rings on g4x/ilk, they did not add 258 * per-engine HWSTAM until gen6. 259 */ 260 if (INTEL_GEN(engine->i915) < 6 && engine->class != RENDER_CLASS) 261 return; 262 263 if (INTEL_GEN(engine->i915) >= 3) 264 ENGINE_WRITE(engine, RING_HWSTAM, mask); 265 else 266 ENGINE_WRITE16(engine, RING_HWSTAM, mask); 267 } 268 269 static void intel_engine_sanitize_mmio(struct intel_engine_cs *engine) 270 { 271 /* Mask off all writes into the unknown HWSP */ 272 intel_engine_set_hwsp_writemask(engine, ~0u); 273 } 274 275 static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id) 276 { 277 const struct engine_info *info = &intel_engines[id]; 278 struct drm_i915_private *i915 = gt->i915; 279 struct intel_engine_cs *engine; 280 281 BUILD_BUG_ON(MAX_ENGINE_CLASS >= BIT(GEN11_ENGINE_CLASS_WIDTH)); 282 BUILD_BUG_ON(MAX_ENGINE_INSTANCE >= BIT(GEN11_ENGINE_INSTANCE_WIDTH)); 283 284 if (GEM_DEBUG_WARN_ON(id >= ARRAY_SIZE(gt->engine))) 285 return -EINVAL; 286 287 if (GEM_DEBUG_WARN_ON(info->class > MAX_ENGINE_CLASS)) 288 return -EINVAL; 289 290 if (GEM_DEBUG_WARN_ON(info->instance > MAX_ENGINE_INSTANCE)) 291 return -EINVAL; 292 293 if (GEM_DEBUG_WARN_ON(gt->engine_class[info->class][info->instance])) 294 return -EINVAL; 295 296 engine = kzalloc(sizeof(*engine), GFP_KERNEL); 297 if (!engine) 298 return -ENOMEM; 299 300 BUILD_BUG_ON(BITS_PER_TYPE(engine->mask) < I915_NUM_ENGINES); 301 302 engine->id = id; 303 engine->legacy_idx = INVALID_ENGINE; 304 engine->mask = BIT(id); 305 engine->i915 = i915; 306 engine->gt = gt; 307 engine->uncore = gt->uncore; 308 engine->hw_id = engine->guc_id = info->hw_id; 309 engine->mmio_base = __engine_mmio_base(i915, info->mmio_bases); 310 311 engine->class = info->class; 312 engine->instance = info->instance; 313 __sprint_engine_name(engine); 314 315 engine->props.heartbeat_interval_ms = 316 CONFIG_DRM_I915_HEARTBEAT_INTERVAL; 317 engine->props.max_busywait_duration_ns = 318 CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT; 319 engine->props.preempt_timeout_ms = 320 CONFIG_DRM_I915_PREEMPT_TIMEOUT; 321 engine->props.stop_timeout_ms = 322 CONFIG_DRM_I915_STOP_TIMEOUT; 323 engine->props.timeslice_duration_ms = 324 CONFIG_DRM_I915_TIMESLICE_DURATION; 325 326 /* Override to uninterruptible for OpenCL workloads. */ 327 if (INTEL_GEN(i915) == 12 && engine->class == RENDER_CLASS) 328 engine->props.preempt_timeout_ms = 0; 329 330 engine->context_size = intel_engine_context_size(gt, engine->class); 331 if (WARN_ON(engine->context_size > BIT(20))) 332 engine->context_size = 0; 333 if (engine->context_size) 334 DRIVER_CAPS(i915)->has_logical_contexts = true; 335 336 /* Nothing to do here, execute in order of dependencies */ 337 engine->schedule = NULL; 338 339 ewma__engine_latency_init(&engine->latency); 340 seqlock_init(&engine->stats.lock, IPL_TTY); 341 342 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier); 343 344 /* Scrub mmio state on takeover */ 345 intel_engine_sanitize_mmio(engine); 346 347 gt->engine_class[info->class][info->instance] = engine; 348 gt->engine[id] = engine; 349 350 i915->engine[id] = engine; 351 352 return 0; 353 } 354 355 static void __setup_engine_capabilities(struct intel_engine_cs *engine) 356 { 357 struct drm_i915_private *i915 = engine->i915; 358 359 if (engine->class == VIDEO_DECODE_CLASS) { 360 /* 361 * HEVC support is present on first engine instance 362 * before Gen11 and on all instances afterwards. 363 */ 364 if (INTEL_GEN(i915) >= 11 || 365 (INTEL_GEN(i915) >= 9 && engine->instance == 0)) 366 engine->uabi_capabilities |= 367 I915_VIDEO_CLASS_CAPABILITY_HEVC; 368 369 /* 370 * SFC block is present only on even logical engine 371 * instances. 372 */ 373 if ((INTEL_GEN(i915) >= 11 && 374 RUNTIME_INFO(i915)->vdbox_sfc_access & engine->mask) || 375 (INTEL_GEN(i915) >= 9 && engine->instance == 0)) 376 engine->uabi_capabilities |= 377 I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC; 378 } else if (engine->class == VIDEO_ENHANCEMENT_CLASS) { 379 if (INTEL_GEN(i915) >= 9) 380 engine->uabi_capabilities |= 381 I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC; 382 } 383 } 384 385 static void intel_setup_engine_capabilities(struct intel_gt *gt) 386 { 387 struct intel_engine_cs *engine; 388 enum intel_engine_id id; 389 390 for_each_engine(engine, gt, id) 391 __setup_engine_capabilities(engine); 392 } 393 394 /** 395 * intel_engines_release() - free the resources allocated for Command Streamers 396 * @gt: pointer to struct intel_gt 397 */ 398 void intel_engines_release(struct intel_gt *gt) 399 { 400 struct intel_engine_cs *engine; 401 enum intel_engine_id id; 402 403 /* 404 * Before we release the resources held by engine, we must be certain 405 * that the HW is no longer accessing them -- having the GPU scribble 406 * to or read from a page being used for something else causes no end 407 * of fun. 408 * 409 * The GPU should be reset by this point, but assume the worst just 410 * in case we aborted before completely initialising the engines. 411 */ 412 GEM_BUG_ON(intel_gt_pm_is_awake(gt)); 413 if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display) 414 __intel_gt_reset(gt, ALL_ENGINES); 415 416 /* Decouple the backend; but keep the layout for late GPU resets */ 417 for_each_engine(engine, gt, id) { 418 intel_wakeref_wait_for_idle(&engine->wakeref); 419 GEM_BUG_ON(intel_engine_pm_is_awake(engine)); 420 421 if (!engine->release) 422 continue; 423 424 engine->release(engine); 425 engine->release = NULL; 426 427 memset(&engine->reset, 0, sizeof(engine->reset)); 428 429 gt->i915->engine[id] = NULL; 430 } 431 } 432 433 void intel_engines_free(struct intel_gt *gt) 434 { 435 struct intel_engine_cs *engine; 436 enum intel_engine_id id; 437 438 for_each_engine(engine, gt, id) { 439 kfree(engine); 440 gt->engine[id] = NULL; 441 } 442 } 443 444 /** 445 * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers 446 * @gt: pointer to struct intel_gt 447 * 448 * Return: non-zero if the initialization failed. 449 */ 450 int intel_engines_init_mmio(struct intel_gt *gt) 451 { 452 struct drm_i915_private *i915 = gt->i915; 453 struct intel_device_info *device_info = mkwrite_device_info(i915); 454 const unsigned int engine_mask = INTEL_INFO(i915)->engine_mask; 455 unsigned int mask = 0; 456 unsigned int i; 457 int err; 458 459 drm_WARN_ON(&i915->drm, engine_mask == 0); 460 drm_WARN_ON(&i915->drm, engine_mask & 461 GENMASK(BITS_PER_TYPE(mask) - 1, I915_NUM_ENGINES)); 462 463 if (i915_inject_probe_failure(i915)) 464 return -ENODEV; 465 466 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) { 467 if (!HAS_ENGINE(i915, i)) 468 continue; 469 470 err = intel_engine_setup(gt, i); 471 if (err) 472 goto cleanup; 473 474 mask |= BIT(i); 475 } 476 477 /* 478 * Catch failures to update intel_engines table when the new engines 479 * are added to the driver by a warning and disabling the forgotten 480 * engines. 481 */ 482 if (drm_WARN_ON(&i915->drm, mask != engine_mask)) 483 device_info->engine_mask = mask; 484 485 RUNTIME_INFO(i915)->num_engines = hweight32(mask); 486 487 intel_gt_check_and_clear_faults(gt); 488 489 intel_setup_engine_capabilities(gt); 490 491 return 0; 492 493 cleanup: 494 intel_engines_free(gt); 495 return err; 496 } 497 498 void intel_engine_init_execlists(struct intel_engine_cs *engine) 499 { 500 struct intel_engine_execlists * const execlists = &engine->execlists; 501 502 execlists->port_mask = 1; 503 GEM_BUG_ON(!is_power_of_2(execlists_num_ports(execlists))); 504 GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS); 505 506 memset(execlists->pending, 0, sizeof(execlists->pending)); 507 execlists->active = 508 memset(execlists->inflight, 0, sizeof(execlists->inflight)); 509 510 execlists->queue_priority_hint = INT_MIN; 511 execlists->queue = RB_ROOT_CACHED; 512 } 513 514 static void cleanup_status_page(struct intel_engine_cs *engine) 515 { 516 struct i915_vma *vma; 517 518 /* Prevent writes into HWSP after returning the page to the system */ 519 intel_engine_set_hwsp_writemask(engine, ~0u); 520 521 vma = fetch_and_zero(&engine->status_page.vma); 522 if (!vma) 523 return; 524 525 if (!HWS_NEEDS_PHYSICAL(engine->i915)) 526 i915_vma_unpin(vma); 527 528 i915_gem_object_unpin_map(vma->obj); 529 i915_gem_object_put(vma->obj); 530 } 531 532 static int pin_ggtt_status_page(struct intel_engine_cs *engine, 533 struct i915_vma *vma) 534 { 535 unsigned int flags; 536 537 if (!HAS_LLC(engine->i915) && i915_ggtt_has_aperture(engine->gt->ggtt)) 538 /* 539 * On g33, we cannot place HWS above 256MiB, so 540 * restrict its pinning to the low mappable arena. 541 * Though this restriction is not documented for 542 * gen4, gen5, or byt, they also behave similarly 543 * and hang if the HWS is placed at the top of the 544 * GTT. To generalise, it appears that all !llc 545 * platforms have issues with us placing the HWS 546 * above the mappable region (even though we never 547 * actually map it). 548 */ 549 flags = PIN_MAPPABLE; 550 else 551 flags = PIN_HIGH; 552 553 return i915_ggtt_pin(vma, 0, flags); 554 } 555 556 static int init_status_page(struct intel_engine_cs *engine) 557 { 558 struct drm_i915_gem_object *obj; 559 struct i915_vma *vma; 560 void *vaddr; 561 int ret; 562 563 /* 564 * Though the HWS register does support 36bit addresses, historically 565 * we have had hangs and corruption reported due to wild writes if 566 * the HWS is placed above 4G. We only allow objects to be allocated 567 * in GFP_DMA32 for i965, and no earlier physical address users had 568 * access to more than 4G. 569 */ 570 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE); 571 if (IS_ERR(obj)) { 572 drm_err(&engine->i915->drm, 573 "Failed to allocate status page\n"); 574 return PTR_ERR(obj); 575 } 576 577 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC); 578 579 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL); 580 if (IS_ERR(vma)) { 581 ret = PTR_ERR(vma); 582 goto err; 583 } 584 585 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB); 586 if (IS_ERR(vaddr)) { 587 ret = PTR_ERR(vaddr); 588 goto err; 589 } 590 591 engine->status_page.addr = memset(vaddr, 0, PAGE_SIZE); 592 engine->status_page.vma = vma; 593 594 if (!HWS_NEEDS_PHYSICAL(engine->i915)) { 595 ret = pin_ggtt_status_page(engine, vma); 596 if (ret) 597 goto err_unpin; 598 } 599 600 return 0; 601 602 err_unpin: 603 i915_gem_object_unpin_map(obj); 604 err: 605 i915_gem_object_put(obj); 606 return ret; 607 } 608 609 static int engine_setup_common(struct intel_engine_cs *engine) 610 { 611 int err; 612 613 init_llist_head(&engine->barrier_tasks); 614 615 err = init_status_page(engine); 616 if (err) 617 return err; 618 619 intel_engine_init_active(engine, ENGINE_PHYSICAL); 620 intel_engine_init_breadcrumbs(engine); 621 intel_engine_init_execlists(engine); 622 intel_engine_init_cmd_parser(engine); 623 intel_engine_init__pm(engine); 624 intel_engine_init_retire(engine); 625 626 intel_engine_pool_init(&engine->pool); 627 628 /* Use the whole device by default */ 629 engine->sseu = 630 intel_sseu_from_device_info(&RUNTIME_INFO(engine->i915)->sseu); 631 632 intel_engine_init_workarounds(engine); 633 intel_engine_init_whitelist(engine); 634 intel_engine_init_ctx_wa(engine); 635 636 return 0; 637 } 638 639 struct measure_breadcrumb { 640 struct i915_request rq; 641 struct intel_ring ring; 642 u32 cs[2048]; 643 }; 644 645 static int measure_breadcrumb_dw(struct intel_context *ce) 646 { 647 struct intel_engine_cs *engine = ce->engine; 648 struct measure_breadcrumb *frame; 649 int dw; 650 651 GEM_BUG_ON(!engine->gt->scratch); 652 653 frame = kzalloc(sizeof(*frame), GFP_KERNEL); 654 if (!frame) 655 return -ENOMEM; 656 657 frame->rq.i915 = engine->i915; 658 frame->rq.engine = engine; 659 frame->rq.context = ce; 660 rcu_assign_pointer(frame->rq.timeline, ce->timeline); 661 662 frame->ring.vaddr = frame->cs; 663 frame->ring.size = sizeof(frame->cs); 664 frame->ring.wrap = 665 BITS_PER_TYPE(frame->ring.size) - ilog2(frame->ring.size); 666 frame->ring.effective_size = frame->ring.size; 667 intel_ring_update_space(&frame->ring); 668 frame->rq.ring = &frame->ring; 669 670 mutex_lock(&ce->timeline->mutex); 671 spin_lock_irq(&engine->active.lock); 672 673 dw = engine->emit_fini_breadcrumb(&frame->rq, frame->cs) - frame->cs; 674 675 spin_unlock_irq(&engine->active.lock); 676 mutex_unlock(&ce->timeline->mutex); 677 678 GEM_BUG_ON(dw & 1); /* RING_TAIL must be qword aligned */ 679 680 kfree(frame); 681 return dw; 682 } 683 684 void 685 intel_engine_init_active(struct intel_engine_cs *engine, unsigned int subclass) 686 { 687 INIT_LIST_HEAD(&engine->active.requests); 688 INIT_LIST_HEAD(&engine->active.hold); 689 690 mtx_init(&engine->active.lock, IPL_TTY); 691 lockdep_set_subclass(&engine->active.lock, subclass); 692 693 /* 694 * Due to an interesting quirk in lockdep's internal debug tracking, 695 * after setting a subclass we must ensure the lock is used. Otherwise, 696 * nr_unused_locks is incremented once too often. 697 */ 698 #ifdef CONFIG_DEBUG_LOCK_ALLOC 699 local_irq_disable(); 700 lock_map_acquire(&engine->active.lock.dep_map); 701 lock_map_release(&engine->active.lock.dep_map); 702 local_irq_enable(); 703 #endif 704 } 705 706 static struct intel_context * 707 create_kernel_context(struct intel_engine_cs *engine) 708 { 709 static struct lock_class_key kernel; 710 struct intel_context *ce; 711 int err; 712 713 ce = intel_context_create(engine); 714 if (IS_ERR(ce)) 715 return ce; 716 717 __set_bit(CONTEXT_BARRIER_BIT, &ce->flags); 718 719 err = intel_context_pin(ce); /* perma-pin so it is always available */ 720 if (err) { 721 intel_context_put(ce); 722 return ERR_PTR(err); 723 } 724 725 /* 726 * Give our perma-pinned kernel timelines a separate lockdep class, 727 * so that we can use them from within the normal user timelines 728 * should we need to inject GPU operations during their request 729 * construction. 730 */ 731 lockdep_set_class(&ce->timeline->mutex, &kernel); 732 733 return ce; 734 } 735 736 /** 737 * intel_engines_init_common - initialize cengine state which might require hw access 738 * @engine: Engine to initialize. 739 * 740 * Initializes @engine@ structure members shared between legacy and execlists 741 * submission modes which do require hardware access. 742 * 743 * Typcally done at later stages of submission mode specific engine setup. 744 * 745 * Returns zero on success or an error code on failure. 746 */ 747 static int engine_init_common(struct intel_engine_cs *engine) 748 { 749 struct intel_context *ce; 750 int ret; 751 752 engine->set_default_submission(engine); 753 754 /* 755 * We may need to do things with the shrinker which 756 * require us to immediately switch back to the default 757 * context. This can cause a problem as pinning the 758 * default context also requires GTT space which may not 759 * be available. To avoid this we always pin the default 760 * context. 761 */ 762 ce = create_kernel_context(engine); 763 if (IS_ERR(ce)) 764 return PTR_ERR(ce); 765 766 ret = measure_breadcrumb_dw(ce); 767 if (ret < 0) 768 goto err_context; 769 770 engine->emit_fini_breadcrumb_dw = ret; 771 engine->kernel_context = ce; 772 773 return 0; 774 775 err_context: 776 intel_context_put(ce); 777 return ret; 778 } 779 780 int intel_engines_init(struct intel_gt *gt) 781 { 782 int (*setup)(struct intel_engine_cs *engine); 783 struct intel_engine_cs *engine; 784 enum intel_engine_id id; 785 int err; 786 787 if (HAS_EXECLISTS(gt->i915)) 788 setup = intel_execlists_submission_setup; 789 else 790 setup = intel_ring_submission_setup; 791 792 for_each_engine(engine, gt, id) { 793 err = engine_setup_common(engine); 794 if (err) 795 return err; 796 797 err = setup(engine); 798 if (err) 799 return err; 800 801 err = engine_init_common(engine); 802 if (err) 803 return err; 804 805 intel_engine_add_user(engine); 806 } 807 808 return 0; 809 } 810 811 /** 812 * intel_engines_cleanup_common - cleans up the engine state created by 813 * the common initiailizers. 814 * @engine: Engine to cleanup. 815 * 816 * This cleans up everything created by the common helpers. 817 */ 818 void intel_engine_cleanup_common(struct intel_engine_cs *engine) 819 { 820 GEM_BUG_ON(!list_empty(&engine->active.requests)); 821 tasklet_kill(&engine->execlists.tasklet); /* flush the callback */ 822 823 cleanup_status_page(engine); 824 825 intel_engine_fini_retire(engine); 826 intel_engine_pool_fini(&engine->pool); 827 intel_engine_fini_breadcrumbs(engine); 828 intel_engine_cleanup_cmd_parser(engine); 829 830 if (engine->default_state) 831 i915_gem_object_put(engine->default_state); 832 833 if (engine->kernel_context) { 834 intel_context_unpin(engine->kernel_context); 835 intel_context_put(engine->kernel_context); 836 } 837 GEM_BUG_ON(!llist_empty(&engine->barrier_tasks)); 838 839 intel_wa_list_free(&engine->ctx_wa_list); 840 intel_wa_list_free(&engine->wa_list); 841 intel_wa_list_free(&engine->whitelist); 842 } 843 844 /** 845 * intel_engine_resume - re-initializes the HW state of the engine 846 * @engine: Engine to resume. 847 * 848 * Returns zero on success or an error code on failure. 849 */ 850 int intel_engine_resume(struct intel_engine_cs *engine) 851 { 852 intel_engine_apply_workarounds(engine); 853 intel_engine_apply_whitelist(engine); 854 855 return engine->resume(engine); 856 } 857 858 u64 intel_engine_get_active_head(const struct intel_engine_cs *engine) 859 { 860 struct drm_i915_private *i915 = engine->i915; 861 862 u64 acthd; 863 864 if (INTEL_GEN(i915) >= 8) 865 acthd = ENGINE_READ64(engine, RING_ACTHD, RING_ACTHD_UDW); 866 else if (INTEL_GEN(i915) >= 4) 867 acthd = ENGINE_READ(engine, RING_ACTHD); 868 else 869 acthd = ENGINE_READ(engine, ACTHD); 870 871 return acthd; 872 } 873 874 u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine) 875 { 876 u64 bbaddr; 877 878 if (INTEL_GEN(engine->i915) >= 8) 879 bbaddr = ENGINE_READ64(engine, RING_BBADDR, RING_BBADDR_UDW); 880 else 881 bbaddr = ENGINE_READ(engine, RING_BBADDR); 882 883 return bbaddr; 884 } 885 886 static unsigned long stop_timeout(const struct intel_engine_cs *engine) 887 { 888 if (in_atomic() || irqs_disabled()) /* inside atomic preempt-reset? */ 889 return 0; 890 891 /* 892 * If we are doing a normal GPU reset, we can take our time and allow 893 * the engine to quiesce. We've stopped submission to the engine, and 894 * if we wait long enough an innocent context should complete and 895 * leave the engine idle. So they should not be caught unaware by 896 * the forthcoming GPU reset (which usually follows the stop_cs)! 897 */ 898 return READ_ONCE(engine->props.stop_timeout_ms); 899 } 900 901 int intel_engine_stop_cs(struct intel_engine_cs *engine) 902 { 903 struct intel_uncore *uncore = engine->uncore; 904 const u32 base = engine->mmio_base; 905 const i915_reg_t mode = RING_MI_MODE(base); 906 int err; 907 908 if (INTEL_GEN(engine->i915) < 3) 909 return -ENODEV; 910 911 ENGINE_TRACE(engine, "\n"); 912 913 intel_uncore_write_fw(uncore, mode, _MASKED_BIT_ENABLE(STOP_RING)); 914 915 err = 0; 916 if (__intel_wait_for_register_fw(uncore, 917 mode, MODE_IDLE, MODE_IDLE, 918 1000, stop_timeout(engine), 919 NULL)) { 920 ENGINE_TRACE(engine, "timed out on STOP_RING -> IDLE\n"); 921 err = -ETIMEDOUT; 922 } 923 924 /* A final mmio read to let GPU writes be hopefully flushed to memory */ 925 intel_uncore_posting_read_fw(uncore, mode); 926 927 return err; 928 } 929 930 void intel_engine_cancel_stop_cs(struct intel_engine_cs *engine) 931 { 932 ENGINE_TRACE(engine, "\n"); 933 934 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING)); 935 } 936 937 const char *i915_cache_level_str(struct drm_i915_private *i915, int type) 938 { 939 switch (type) { 940 case I915_CACHE_NONE: return " uncached"; 941 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped"; 942 case I915_CACHE_L3_LLC: return " L3+LLC"; 943 case I915_CACHE_WT: return " WT"; 944 default: return ""; 945 } 946 } 947 948 static u32 949 read_subslice_reg(const struct intel_engine_cs *engine, 950 int slice, int subslice, i915_reg_t reg) 951 { 952 struct drm_i915_private *i915 = engine->i915; 953 struct intel_uncore *uncore = engine->uncore; 954 u32 mcr_mask, mcr_ss, mcr, old_mcr, val; 955 enum forcewake_domains fw_domains; 956 957 if (INTEL_GEN(i915) >= 11) { 958 mcr_mask = GEN11_MCR_SLICE_MASK | GEN11_MCR_SUBSLICE_MASK; 959 mcr_ss = GEN11_MCR_SLICE(slice) | GEN11_MCR_SUBSLICE(subslice); 960 } else { 961 mcr_mask = GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK; 962 mcr_ss = GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice); 963 } 964 965 fw_domains = intel_uncore_forcewake_for_reg(uncore, reg, 966 FW_REG_READ); 967 fw_domains |= intel_uncore_forcewake_for_reg(uncore, 968 GEN8_MCR_SELECTOR, 969 FW_REG_READ | FW_REG_WRITE); 970 971 spin_lock_irq(&uncore->lock); 972 intel_uncore_forcewake_get__locked(uncore, fw_domains); 973 974 old_mcr = mcr = intel_uncore_read_fw(uncore, GEN8_MCR_SELECTOR); 975 976 mcr &= ~mcr_mask; 977 mcr |= mcr_ss; 978 intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, mcr); 979 980 val = intel_uncore_read_fw(uncore, reg); 981 982 mcr &= ~mcr_mask; 983 mcr |= old_mcr & mcr_mask; 984 985 intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, mcr); 986 987 intel_uncore_forcewake_put__locked(uncore, fw_domains); 988 spin_unlock_irq(&uncore->lock); 989 990 return val; 991 } 992 993 /* NB: please notice the memset */ 994 void intel_engine_get_instdone(const struct intel_engine_cs *engine, 995 struct intel_instdone *instdone) 996 { 997 struct drm_i915_private *i915 = engine->i915; 998 const struct sseu_dev_info *sseu = &RUNTIME_INFO(i915)->sseu; 999 struct intel_uncore *uncore = engine->uncore; 1000 u32 mmio_base = engine->mmio_base; 1001 int slice; 1002 int subslice; 1003 1004 memset(instdone, 0, sizeof(*instdone)); 1005 1006 switch (INTEL_GEN(i915)) { 1007 default: 1008 instdone->instdone = 1009 intel_uncore_read(uncore, RING_INSTDONE(mmio_base)); 1010 1011 if (engine->id != RCS0) 1012 break; 1013 1014 instdone->slice_common = 1015 intel_uncore_read(uncore, GEN7_SC_INSTDONE); 1016 if (INTEL_GEN(i915) >= 12) { 1017 instdone->slice_common_extra[0] = 1018 intel_uncore_read(uncore, GEN12_SC_INSTDONE_EXTRA); 1019 instdone->slice_common_extra[1] = 1020 intel_uncore_read(uncore, GEN12_SC_INSTDONE_EXTRA2); 1021 } 1022 for_each_instdone_slice_subslice(i915, sseu, slice, subslice) { 1023 instdone->sampler[slice][subslice] = 1024 read_subslice_reg(engine, slice, subslice, 1025 GEN7_SAMPLER_INSTDONE); 1026 instdone->row[slice][subslice] = 1027 read_subslice_reg(engine, slice, subslice, 1028 GEN7_ROW_INSTDONE); 1029 } 1030 break; 1031 case 7: 1032 instdone->instdone = 1033 intel_uncore_read(uncore, RING_INSTDONE(mmio_base)); 1034 1035 if (engine->id != RCS0) 1036 break; 1037 1038 instdone->slice_common = 1039 intel_uncore_read(uncore, GEN7_SC_INSTDONE); 1040 instdone->sampler[0][0] = 1041 intel_uncore_read(uncore, GEN7_SAMPLER_INSTDONE); 1042 instdone->row[0][0] = 1043 intel_uncore_read(uncore, GEN7_ROW_INSTDONE); 1044 1045 break; 1046 case 6: 1047 case 5: 1048 case 4: 1049 instdone->instdone = 1050 intel_uncore_read(uncore, RING_INSTDONE(mmio_base)); 1051 if (engine->id == RCS0) 1052 /* HACK: Using the wrong struct member */ 1053 instdone->slice_common = 1054 intel_uncore_read(uncore, GEN4_INSTDONE1); 1055 break; 1056 case 3: 1057 case 2: 1058 instdone->instdone = intel_uncore_read(uncore, GEN2_INSTDONE); 1059 break; 1060 } 1061 } 1062 1063 static bool ring_is_idle(struct intel_engine_cs *engine) 1064 { 1065 bool idle = true; 1066 1067 if (I915_SELFTEST_ONLY(!engine->mmio_base)) 1068 return true; 1069 1070 if (!intel_engine_pm_get_if_awake(engine)) 1071 return true; 1072 1073 /* First check that no commands are left in the ring */ 1074 if ((ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR) != 1075 (ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR)) 1076 idle = false; 1077 1078 /* No bit for gen2, so assume the CS parser is idle */ 1079 if (INTEL_GEN(engine->i915) > 2 && 1080 !(ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE)) 1081 idle = false; 1082 1083 intel_engine_pm_put(engine); 1084 1085 return idle; 1086 } 1087 1088 void intel_engine_flush_submission(struct intel_engine_cs *engine) 1089 { 1090 struct tasklet_struct *t = &engine->execlists.tasklet; 1091 1092 if (__tasklet_is_scheduled(t)) { 1093 local_bh_disable(); 1094 if (tasklet_trylock(t)) { 1095 /* Must wait for any GPU reset in progress. */ 1096 if (__tasklet_is_enabled(t)) 1097 t->func(t->data); 1098 tasklet_unlock(t); 1099 } 1100 local_bh_enable(); 1101 } 1102 1103 /* Otherwise flush the tasklet if it was running on another cpu */ 1104 tasklet_unlock_wait(t); 1105 } 1106 1107 /** 1108 * intel_engine_is_idle() - Report if the engine has finished process all work 1109 * @engine: the intel_engine_cs 1110 * 1111 * Return true if there are no requests pending, nothing left to be submitted 1112 * to hardware, and that the engine is idle. 1113 */ 1114 bool intel_engine_is_idle(struct intel_engine_cs *engine) 1115 { 1116 /* More white lies, if wedged, hw state is inconsistent */ 1117 if (intel_gt_is_wedged(engine->gt)) 1118 return true; 1119 1120 if (!intel_engine_pm_is_awake(engine)) 1121 return true; 1122 1123 /* Waiting to drain ELSP? */ 1124 if (execlists_active(&engine->execlists)) { 1125 #ifdef __linux__ 1126 synchronize_hardirq(engine->i915->drm.pdev->irq); 1127 #else 1128 intr_barrier(engine->i915->irqh); 1129 #endif 1130 1131 intel_engine_flush_submission(engine); 1132 1133 if (execlists_active(&engine->execlists)) 1134 return false; 1135 } 1136 1137 /* ELSP is empty, but there are ready requests? E.g. after reset */ 1138 if (!RB_EMPTY_ROOT(&engine->execlists.queue.rb_root)) 1139 return false; 1140 1141 /* Ring stopped? */ 1142 return ring_is_idle(engine); 1143 } 1144 1145 bool intel_engines_are_idle(struct intel_gt *gt) 1146 { 1147 struct intel_engine_cs *engine; 1148 enum intel_engine_id id; 1149 1150 /* 1151 * If the driver is wedged, HW state may be very inconsistent and 1152 * report that it is still busy, even though we have stopped using it. 1153 */ 1154 if (intel_gt_is_wedged(gt)) 1155 return true; 1156 1157 /* Already parked (and passed an idleness test); must still be idle */ 1158 if (!READ_ONCE(gt->awake)) 1159 return true; 1160 1161 for_each_engine(engine, gt, id) { 1162 if (!intel_engine_is_idle(engine)) 1163 return false; 1164 } 1165 1166 return true; 1167 } 1168 1169 void intel_engines_reset_default_submission(struct intel_gt *gt) 1170 { 1171 struct intel_engine_cs *engine; 1172 enum intel_engine_id id; 1173 1174 for_each_engine(engine, gt, id) 1175 engine->set_default_submission(engine); 1176 } 1177 1178 bool intel_engine_can_store_dword(struct intel_engine_cs *engine) 1179 { 1180 switch (INTEL_GEN(engine->i915)) { 1181 case 2: 1182 return false; /* uses physical not virtual addresses */ 1183 case 3: 1184 /* maybe only uses physical not virtual addresses */ 1185 return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915)); 1186 case 4: 1187 return !IS_I965G(engine->i915); /* who knows! */ 1188 case 6: 1189 return engine->class != VIDEO_DECODE_CLASS; /* b0rked */ 1190 default: 1191 return true; 1192 } 1193 } 1194 1195 static int print_sched_attr(struct drm_i915_private *i915, 1196 const struct i915_sched_attr *attr, 1197 char *buf, int x, int len) 1198 { 1199 if (attr->priority == I915_PRIORITY_INVALID) 1200 return x; 1201 1202 x += snprintf(buf + x, len - x, 1203 " prio=%d", attr->priority); 1204 1205 return x; 1206 } 1207 1208 static void print_request(struct drm_printer *m, 1209 struct i915_request *rq, 1210 const char *prefix) 1211 { 1212 const char *name = rq->fence.ops->get_timeline_name(&rq->fence); 1213 char buf[80] = ""; 1214 int x = 0; 1215 1216 x = print_sched_attr(rq->i915, &rq->sched.attr, buf, x, sizeof(buf)); 1217 1218 drm_printf(m, "%s %llx:%llx%s%s %s @ %dms: %s\n", 1219 prefix, 1220 rq->fence.context, rq->fence.seqno, 1221 i915_request_completed(rq) ? "!" : 1222 i915_request_started(rq) ? "*" : 1223 "", 1224 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, 1225 &rq->fence.flags) ? "+" : 1226 test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, 1227 &rq->fence.flags) ? "-" : 1228 "", 1229 buf, 1230 jiffies_to_msecs(jiffies - rq->emitted_jiffies), 1231 name); 1232 } 1233 1234 static void hexdump(struct drm_printer *m, const void *buf, size_t len) 1235 { 1236 STUB(); 1237 #ifdef notyet 1238 const size_t rowsize = 8 * sizeof(u32); 1239 const void *prev = NULL; 1240 bool skip = false; 1241 size_t pos; 1242 1243 for (pos = 0; pos < len; pos += rowsize) { 1244 char line[128]; 1245 1246 if (prev && !memcmp(prev, buf + pos, rowsize)) { 1247 if (!skip) { 1248 drm_printf(m, "*\n"); 1249 skip = true; 1250 } 1251 continue; 1252 } 1253 1254 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos, 1255 rowsize, sizeof(u32), 1256 line, sizeof(line), 1257 false) >= sizeof(line)); 1258 drm_printf(m, "[%04zx] %s\n", pos, line); 1259 1260 prev = buf + pos; 1261 skip = false; 1262 } 1263 #endif 1264 } 1265 1266 static struct intel_timeline *get_timeline(struct i915_request *rq) 1267 { 1268 struct intel_timeline *tl; 1269 1270 /* 1271 * Even though we are holding the engine->active.lock here, there 1272 * is no control over the submission queue per-se and we are 1273 * inspecting the active state at a random point in time, with an 1274 * unknown queue. Play safe and make sure the timeline remains valid. 1275 * (Only being used for pretty printing, one extra kref shouldn't 1276 * cause a camel stampede!) 1277 */ 1278 rcu_read_lock(); 1279 tl = rcu_dereference(rq->timeline); 1280 if (!kref_get_unless_zero(&tl->kref)) 1281 tl = NULL; 1282 rcu_read_unlock(); 1283 1284 return tl; 1285 } 1286 1287 static const char *repr_timer(const struct timeout *t) 1288 { 1289 if (!READ_ONCE(t->to_time)) 1290 return "inactive"; 1291 1292 if (timer_pending(t)) 1293 return "active"; 1294 1295 return "expired"; 1296 } 1297 1298 static void intel_engine_print_registers(struct intel_engine_cs *engine, 1299 struct drm_printer *m) 1300 { 1301 struct drm_i915_private *dev_priv = engine->i915; 1302 struct intel_engine_execlists * const execlists = &engine->execlists; 1303 u64 addr; 1304 1305 if (engine->id == RENDER_CLASS && IS_GEN_RANGE(dev_priv, 4, 7)) 1306 drm_printf(m, "\tCCID: 0x%08x\n", ENGINE_READ(engine, CCID)); 1307 if (HAS_EXECLISTS(dev_priv)) { 1308 drm_printf(m, "\tEL_STAT_HI: 0x%08x\n", 1309 ENGINE_READ(engine, RING_EXECLIST_STATUS_HI)); 1310 drm_printf(m, "\tEL_STAT_LO: 0x%08x\n", 1311 ENGINE_READ(engine, RING_EXECLIST_STATUS_LO)); 1312 } 1313 drm_printf(m, "\tRING_START: 0x%08x\n", 1314 ENGINE_READ(engine, RING_START)); 1315 drm_printf(m, "\tRING_HEAD: 0x%08x\n", 1316 ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR); 1317 drm_printf(m, "\tRING_TAIL: 0x%08x\n", 1318 ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR); 1319 drm_printf(m, "\tRING_CTL: 0x%08x%s\n", 1320 ENGINE_READ(engine, RING_CTL), 1321 ENGINE_READ(engine, RING_CTL) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : ""); 1322 if (INTEL_GEN(engine->i915) > 2) { 1323 drm_printf(m, "\tRING_MODE: 0x%08x%s\n", 1324 ENGINE_READ(engine, RING_MI_MODE), 1325 ENGINE_READ(engine, RING_MI_MODE) & (MODE_IDLE) ? " [idle]" : ""); 1326 } 1327 1328 if (INTEL_GEN(dev_priv) >= 6) { 1329 drm_printf(m, "\tRING_IMR: 0x%08x\n", 1330 ENGINE_READ(engine, RING_IMR)); 1331 drm_printf(m, "\tRING_ESR: 0x%08x\n", 1332 ENGINE_READ(engine, RING_ESR)); 1333 drm_printf(m, "\tRING_EMR: 0x%08x\n", 1334 ENGINE_READ(engine, RING_EMR)); 1335 drm_printf(m, "\tRING_EIR: 0x%08x\n", 1336 ENGINE_READ(engine, RING_EIR)); 1337 } 1338 1339 addr = intel_engine_get_active_head(engine); 1340 drm_printf(m, "\tACTHD: 0x%08x_%08x\n", 1341 upper_32_bits(addr), lower_32_bits(addr)); 1342 addr = intel_engine_get_last_batch_head(engine); 1343 drm_printf(m, "\tBBADDR: 0x%08x_%08x\n", 1344 upper_32_bits(addr), lower_32_bits(addr)); 1345 if (INTEL_GEN(dev_priv) >= 8) 1346 addr = ENGINE_READ64(engine, RING_DMA_FADD, RING_DMA_FADD_UDW); 1347 else if (INTEL_GEN(dev_priv) >= 4) 1348 addr = ENGINE_READ(engine, RING_DMA_FADD); 1349 else 1350 addr = ENGINE_READ(engine, DMA_FADD_I8XX); 1351 drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n", 1352 upper_32_bits(addr), lower_32_bits(addr)); 1353 if (INTEL_GEN(dev_priv) >= 4) { 1354 drm_printf(m, "\tIPEIR: 0x%08x\n", 1355 ENGINE_READ(engine, RING_IPEIR)); 1356 drm_printf(m, "\tIPEHR: 0x%08x\n", 1357 ENGINE_READ(engine, RING_IPEHR)); 1358 } else { 1359 drm_printf(m, "\tIPEIR: 0x%08x\n", ENGINE_READ(engine, IPEIR)); 1360 drm_printf(m, "\tIPEHR: 0x%08x\n", ENGINE_READ(engine, IPEHR)); 1361 } 1362 1363 if (HAS_EXECLISTS(dev_priv)) { 1364 struct i915_request * const *port, *rq; 1365 const u32 *hws = 1366 &engine->status_page.addr[I915_HWS_CSB_BUF0_INDEX]; 1367 const u8 num_entries = execlists->csb_size; 1368 unsigned int idx; 1369 u8 read, write; 1370 1371 drm_printf(m, "\tExeclist tasklet queued? %s (%s), preempt? %s, timeslice? %s\n", 1372 yesno(test_bit(TASKLET_STATE_SCHED, 1373 &engine->execlists.tasklet.state)), 1374 enableddisabled(!atomic_read(&engine->execlists.tasklet.count)), 1375 repr_timer(&engine->execlists.preempt), 1376 repr_timer(&engine->execlists.timer)); 1377 1378 read = execlists->csb_head; 1379 write = READ_ONCE(*execlists->csb_write); 1380 1381 drm_printf(m, "\tExeclist status: 0x%08x %08x; CSB read:%d, write:%d, entries:%d\n", 1382 ENGINE_READ(engine, RING_EXECLIST_STATUS_LO), 1383 ENGINE_READ(engine, RING_EXECLIST_STATUS_HI), 1384 read, write, num_entries); 1385 1386 if (read >= num_entries) 1387 read = 0; 1388 if (write >= num_entries) 1389 write = 0; 1390 if (read > write) 1391 write += num_entries; 1392 while (read < write) { 1393 idx = ++read % num_entries; 1394 drm_printf(m, "\tExeclist CSB[%d]: 0x%08x, context: %d\n", 1395 idx, hws[idx * 2], hws[idx * 2 + 1]); 1396 } 1397 1398 execlists_active_lock_bh(execlists); 1399 rcu_read_lock(); 1400 for (port = execlists->active; (rq = *port); port++) { 1401 char hdr[160]; 1402 int len; 1403 1404 len = scnprintf(hdr, sizeof(hdr), 1405 "\t\tActive[%d]: ", 1406 (int)(port - execlists->active)); 1407 if (!i915_request_signaled(rq)) { 1408 struct intel_timeline *tl = get_timeline(rq); 1409 1410 len += scnprintf(hdr + len, sizeof(hdr) - len, 1411 "ring:{start:%08x, hwsp:%08x, seqno:%08x, runtime:%llums}, ", 1412 i915_ggtt_offset(rq->ring->vma), 1413 tl ? tl->hwsp_offset : 0, 1414 hwsp_seqno(rq), 1415 DIV_ROUND_CLOSEST_ULL(intel_context_get_total_runtime_ns(rq->context), 1416 1000 * 1000)); 1417 1418 if (tl) 1419 intel_timeline_put(tl); 1420 } 1421 scnprintf(hdr + len, sizeof(hdr) - len, "rq: "); 1422 print_request(m, rq, hdr); 1423 } 1424 for (port = execlists->pending; (rq = *port); port++) { 1425 struct intel_timeline *tl = get_timeline(rq); 1426 char hdr[80]; 1427 1428 snprintf(hdr, sizeof(hdr), 1429 "\t\tPending[%d] ring:{start:%08x, hwsp:%08x, seqno:%08x}, rq: ", 1430 (int)(port - execlists->pending), 1431 i915_ggtt_offset(rq->ring->vma), 1432 tl ? tl->hwsp_offset : 0, 1433 hwsp_seqno(rq)); 1434 print_request(m, rq, hdr); 1435 1436 if (tl) 1437 intel_timeline_put(tl); 1438 } 1439 rcu_read_unlock(); 1440 execlists_active_unlock_bh(execlists); 1441 } else if (INTEL_GEN(dev_priv) > 6) { 1442 drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n", 1443 ENGINE_READ(engine, RING_PP_DIR_BASE)); 1444 drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n", 1445 ENGINE_READ(engine, RING_PP_DIR_BASE_READ)); 1446 drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n", 1447 ENGINE_READ(engine, RING_PP_DIR_DCLV)); 1448 } 1449 } 1450 1451 static void print_request_ring(struct drm_printer *m, struct i915_request *rq) 1452 { 1453 void *ring; 1454 int size; 1455 1456 drm_printf(m, 1457 "[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]:\n", 1458 rq->head, rq->postfix, rq->tail, 1459 rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u, 1460 rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u); 1461 1462 size = rq->tail - rq->head; 1463 if (rq->tail < rq->head) 1464 size += rq->ring->size; 1465 1466 ring = kmalloc(size, GFP_ATOMIC); 1467 if (ring) { 1468 const void *vaddr = rq->ring->vaddr; 1469 unsigned int head = rq->head; 1470 unsigned int len = 0; 1471 1472 if (rq->tail < head) { 1473 len = rq->ring->size - head; 1474 memcpy(ring, vaddr + head, len); 1475 head = 0; 1476 } 1477 memcpy(ring + len, vaddr + head, size - len); 1478 1479 hexdump(m, ring, size); 1480 kfree(ring); 1481 } 1482 } 1483 1484 static unsigned long list_count(struct list_head *list) 1485 { 1486 struct list_head *pos; 1487 unsigned long count = 0; 1488 1489 list_for_each(pos, list) 1490 count++; 1491 1492 return count; 1493 } 1494 1495 void intel_engine_dump(struct intel_engine_cs *engine, 1496 struct drm_printer *m, 1497 const char *header, ...) 1498 { 1499 struct i915_gpu_error * const error = &engine->i915->gpu_error; 1500 struct i915_request *rq; 1501 intel_wakeref_t wakeref; 1502 unsigned long flags; 1503 1504 if (header) { 1505 va_list ap; 1506 1507 va_start(ap, header); 1508 drm_vprintf(m, header, &ap); 1509 va_end(ap); 1510 } 1511 1512 if (intel_gt_is_wedged(engine->gt)) 1513 drm_printf(m, "*** WEDGED ***\n"); 1514 1515 drm_printf(m, "\tAwake? %d\n", atomic_read(&engine->wakeref.count)); 1516 drm_printf(m, "\tBarriers?: %s\n", 1517 yesno(!llist_empty(&engine->barrier_tasks))); 1518 drm_printf(m, "\tLatency: %luus\n", 1519 ewma__engine_latency_read(&engine->latency)); 1520 1521 rcu_read_lock(); 1522 rq = READ_ONCE(engine->heartbeat.systole); 1523 if (rq) 1524 drm_printf(m, "\tHeartbeat: %d ms ago\n", 1525 jiffies_to_msecs(jiffies - rq->emitted_jiffies)); 1526 rcu_read_unlock(); 1527 drm_printf(m, "\tReset count: %d (global %d)\n", 1528 i915_reset_engine_count(error, engine), 1529 i915_reset_count(error)); 1530 1531 drm_printf(m, "\tRequests:\n"); 1532 1533 spin_lock_irqsave(&engine->active.lock, flags); 1534 rq = intel_engine_find_active_request(engine); 1535 if (rq) { 1536 struct intel_timeline *tl = get_timeline(rq); 1537 1538 print_request(m, rq, "\t\tactive "); 1539 1540 drm_printf(m, "\t\tring->start: 0x%08x\n", 1541 i915_ggtt_offset(rq->ring->vma)); 1542 drm_printf(m, "\t\tring->head: 0x%08x\n", 1543 rq->ring->head); 1544 drm_printf(m, "\t\tring->tail: 0x%08x\n", 1545 rq->ring->tail); 1546 drm_printf(m, "\t\tring->emit: 0x%08x\n", 1547 rq->ring->emit); 1548 drm_printf(m, "\t\tring->space: 0x%08x\n", 1549 rq->ring->space); 1550 1551 if (tl) { 1552 drm_printf(m, "\t\tring->hwsp: 0x%08x\n", 1553 tl->hwsp_offset); 1554 intel_timeline_put(tl); 1555 } 1556 1557 print_request_ring(m, rq); 1558 1559 if (rq->context->lrc_reg_state) { 1560 drm_printf(m, "Logical Ring Context:\n"); 1561 hexdump(m, rq->context->lrc_reg_state, PAGE_SIZE); 1562 } 1563 } 1564 drm_printf(m, "\tOn hold?: %lu\n", list_count(&engine->active.hold)); 1565 spin_unlock_irqrestore(&engine->active.lock, flags); 1566 1567 drm_printf(m, "\tMMIO base: 0x%08x\n", engine->mmio_base); 1568 wakeref = intel_runtime_pm_get_if_in_use(engine->uncore->rpm); 1569 if (wakeref) { 1570 intel_engine_print_registers(engine, m); 1571 intel_runtime_pm_put(engine->uncore->rpm, wakeref); 1572 } else { 1573 drm_printf(m, "\tDevice is asleep; skipping register dump\n"); 1574 } 1575 1576 intel_execlists_show_requests(engine, m, print_request, 8); 1577 1578 drm_printf(m, "HWSP:\n"); 1579 hexdump(m, engine->status_page.addr, PAGE_SIZE); 1580 1581 drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine))); 1582 1583 intel_engine_print_breadcrumbs(engine, m); 1584 } 1585 1586 /** 1587 * intel_enable_engine_stats() - Enable engine busy tracking on engine 1588 * @engine: engine to enable stats collection 1589 * 1590 * Start collecting the engine busyness data for @engine. 1591 * 1592 * Returns 0 on success or a negative error code. 1593 */ 1594 int intel_enable_engine_stats(struct intel_engine_cs *engine) 1595 { 1596 struct intel_engine_execlists *execlists = &engine->execlists; 1597 unsigned long flags; 1598 int err = 0; 1599 1600 if (!intel_engine_supports_stats(engine)) 1601 return -ENODEV; 1602 1603 execlists_active_lock_bh(execlists); 1604 write_seqlock_irqsave(&engine->stats.lock, flags); 1605 1606 if (unlikely(engine->stats.enabled == ~0)) { 1607 err = -EBUSY; 1608 goto unlock; 1609 } 1610 1611 if (engine->stats.enabled++ == 0) { 1612 struct i915_request * const *port; 1613 struct i915_request *rq; 1614 1615 engine->stats.enabled_at = ktime_get(); 1616 1617 /* XXX submission method oblivious? */ 1618 for (port = execlists->active; (rq = *port); port++) 1619 engine->stats.active++; 1620 1621 for (port = execlists->pending; (rq = *port); port++) { 1622 /* Exclude any contexts already counted in active */ 1623 if (!intel_context_inflight_count(rq->context)) 1624 engine->stats.active++; 1625 } 1626 1627 if (engine->stats.active) 1628 engine->stats.start = engine->stats.enabled_at; 1629 } 1630 1631 unlock: 1632 write_sequnlock_irqrestore(&engine->stats.lock, flags); 1633 execlists_active_unlock_bh(execlists); 1634 1635 return err; 1636 } 1637 1638 static ktime_t __intel_engine_get_busy_time(struct intel_engine_cs *engine) 1639 { 1640 ktime_t total = engine->stats.total; 1641 1642 /* 1643 * If the engine is executing something at the moment 1644 * add it to the total. 1645 */ 1646 if (engine->stats.active) 1647 total = ktime_add(total, 1648 ktime_sub(ktime_get(), engine->stats.start)); 1649 1650 return total; 1651 } 1652 1653 /** 1654 * intel_engine_get_busy_time() - Return current accumulated engine busyness 1655 * @engine: engine to report on 1656 * 1657 * Returns accumulated time @engine was busy since engine stats were enabled. 1658 */ 1659 ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine) 1660 { 1661 unsigned int seq; 1662 ktime_t total; 1663 1664 do { 1665 seq = read_seqbegin(&engine->stats.lock); 1666 total = __intel_engine_get_busy_time(engine); 1667 } while (read_seqretry(&engine->stats.lock, seq)); 1668 1669 return total; 1670 } 1671 1672 /** 1673 * intel_disable_engine_stats() - Disable engine busy tracking on engine 1674 * @engine: engine to disable stats collection 1675 * 1676 * Stops collecting the engine busyness data for @engine. 1677 */ 1678 void intel_disable_engine_stats(struct intel_engine_cs *engine) 1679 { 1680 unsigned long flags; 1681 1682 if (!intel_engine_supports_stats(engine)) 1683 return; 1684 1685 write_seqlock_irqsave(&engine->stats.lock, flags); 1686 WARN_ON_ONCE(engine->stats.enabled == 0); 1687 if (--engine->stats.enabled == 0) { 1688 engine->stats.total = __intel_engine_get_busy_time(engine); 1689 engine->stats.active = 0; 1690 } 1691 write_sequnlock_irqrestore(&engine->stats.lock, flags); 1692 } 1693 1694 static bool match_ring(struct i915_request *rq) 1695 { 1696 u32 ring = ENGINE_READ(rq->engine, RING_START); 1697 1698 return ring == i915_ggtt_offset(rq->ring->vma); 1699 } 1700 1701 struct i915_request * 1702 intel_engine_find_active_request(struct intel_engine_cs *engine) 1703 { 1704 struct i915_request *request, *active = NULL; 1705 1706 /* 1707 * We are called by the error capture, reset and to dump engine 1708 * state at random points in time. In particular, note that neither is 1709 * crucially ordered with an interrupt. After a hang, the GPU is dead 1710 * and we assume that no more writes can happen (we waited long enough 1711 * for all writes that were in transaction to be flushed) - adding an 1712 * extra delay for a recent interrupt is pointless. Hence, we do 1713 * not need an engine->irq_seqno_barrier() before the seqno reads. 1714 * At all other times, we must assume the GPU is still running, but 1715 * we only care about the snapshot of this moment. 1716 */ 1717 lockdep_assert_held(&engine->active.lock); 1718 1719 rcu_read_lock(); 1720 request = execlists_active(&engine->execlists); 1721 if (request) { 1722 struct intel_timeline *tl = request->context->timeline; 1723 1724 list_for_each_entry_from_reverse(request, &tl->requests, link) { 1725 if (i915_request_completed(request)) 1726 break; 1727 1728 active = request; 1729 } 1730 } 1731 rcu_read_unlock(); 1732 if (active) 1733 return active; 1734 1735 list_for_each_entry(request, &engine->active.requests, sched.link) { 1736 if (i915_request_completed(request)) 1737 continue; 1738 1739 if (!i915_request_started(request)) 1740 continue; 1741 1742 /* More than one preemptible request may match! */ 1743 if (!match_ring(request)) 1744 continue; 1745 1746 active = request; 1747 break; 1748 } 1749 1750 return active; 1751 } 1752 1753 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 1754 #include "mock_engine.c" 1755 #include "selftest_engine.c" 1756 #include "selftest_engine_cs.c" 1757 #endif 1758