1 /* 2 * Copyright © 2008-2010 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 * Authors: 24 * Eric Anholt <eric@anholt.net> 25 * Zou Nan hai <nanhai.zou@intel.com> 26 * Xiang Hai hao<haihao.xiang@intel.com> 27 * 28 */ 29 30 #include <linux/log2.h> 31 32 #include <drm/i915_drm.h> 33 34 #include "gem/i915_gem_context.h" 35 36 #include "i915_drv.h" 37 #include "i915_trace.h" 38 #include "intel_context.h" 39 #include "intel_gt.h" 40 #include "intel_gt_irq.h" 41 #include "intel_gt_pm_irq.h" 42 #include "intel_reset.h" 43 #include "intel_ring.h" 44 #include "intel_workarounds.h" 45 46 /* Rough estimate of the typical request size, performing a flush, 47 * set-context and then emitting the batch. 48 */ 49 #define LEGACY_REQUEST_SIZE 200 50 51 static int 52 gen2_render_ring_flush(struct i915_request *rq, u32 mode) 53 { 54 unsigned int num_store_dw; 55 u32 cmd, *cs; 56 57 cmd = MI_FLUSH; 58 num_store_dw = 0; 59 if (mode & EMIT_INVALIDATE) 60 cmd |= MI_READ_FLUSH; 61 if (mode & EMIT_FLUSH) 62 num_store_dw = 4; 63 64 cs = intel_ring_begin(rq, 2 + 3 * num_store_dw); 65 if (IS_ERR(cs)) 66 return PTR_ERR(cs); 67 68 *cs++ = cmd; 69 while (num_store_dw--) { 70 *cs++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL; 71 *cs++ = intel_gt_scratch_offset(rq->engine->gt, 72 INTEL_GT_SCRATCH_FIELD_DEFAULT); 73 *cs++ = 0; 74 } 75 *cs++ = MI_FLUSH | MI_NO_WRITE_FLUSH; 76 77 intel_ring_advance(rq, cs); 78 79 return 0; 80 } 81 82 static int 83 gen4_render_ring_flush(struct i915_request *rq, u32 mode) 84 { 85 u32 cmd, *cs; 86 int i; 87 88 /* 89 * read/write caches: 90 * 91 * I915_GEM_DOMAIN_RENDER is always invalidated, but is 92 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is 93 * also flushed at 2d versus 3d pipeline switches. 94 * 95 * read-only caches: 96 * 97 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if 98 * MI_READ_FLUSH is set, and is always flushed on 965. 99 * 100 * I915_GEM_DOMAIN_COMMAND may not exist? 101 * 102 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is 103 * invalidated when MI_EXE_FLUSH is set. 104 * 105 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is 106 * invalidated with every MI_FLUSH. 107 * 108 * TLBs: 109 * 110 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND 111 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and 112 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER 113 * are flushed at any MI_FLUSH. 114 */ 115 116 cmd = MI_FLUSH; 117 if (mode & EMIT_INVALIDATE) { 118 cmd |= MI_EXE_FLUSH; 119 if (IS_G4X(rq->i915) || IS_GEN(rq->i915, 5)) 120 cmd |= MI_INVALIDATE_ISP; 121 } 122 123 i = 2; 124 if (mode & EMIT_INVALIDATE) 125 i += 20; 126 127 cs = intel_ring_begin(rq, i); 128 if (IS_ERR(cs)) 129 return PTR_ERR(cs); 130 131 *cs++ = cmd; 132 133 /* 134 * A random delay to let the CS invalidate take effect? Without this 135 * delay, the GPU relocation path fails as the CS does not see 136 * the updated contents. Just as important, if we apply the flushes 137 * to the EMIT_FLUSH branch (i.e. immediately after the relocation 138 * write and before the invalidate on the next batch), the relocations 139 * still fail. This implies that is a delay following invalidation 140 * that is required to reset the caches as opposed to a delay to 141 * ensure the memory is written. 142 */ 143 if (mode & EMIT_INVALIDATE) { 144 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE; 145 *cs++ = intel_gt_scratch_offset(rq->engine->gt, 146 INTEL_GT_SCRATCH_FIELD_DEFAULT) | 147 PIPE_CONTROL_GLOBAL_GTT; 148 *cs++ = 0; 149 *cs++ = 0; 150 151 for (i = 0; i < 12; i++) 152 *cs++ = MI_FLUSH; 153 154 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE; 155 *cs++ = intel_gt_scratch_offset(rq->engine->gt, 156 INTEL_GT_SCRATCH_FIELD_DEFAULT) | 157 PIPE_CONTROL_GLOBAL_GTT; 158 *cs++ = 0; 159 *cs++ = 0; 160 } 161 162 *cs++ = cmd; 163 164 intel_ring_advance(rq, cs); 165 166 return 0; 167 } 168 169 /* 170 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for 171 * implementing two workarounds on gen6. From section 1.4.7.1 172 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1: 173 * 174 * [DevSNB-C+{W/A}] Before any depth stall flush (including those 175 * produced by non-pipelined state commands), software needs to first 176 * send a PIPE_CONTROL with no bits set except Post-Sync Operation != 177 * 0. 178 * 179 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable 180 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required. 181 * 182 * And the workaround for these two requires this workaround first: 183 * 184 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent 185 * BEFORE the pipe-control with a post-sync op and no write-cache 186 * flushes. 187 * 188 * And this last workaround is tricky because of the requirements on 189 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM 190 * volume 2 part 1: 191 * 192 * "1 of the following must also be set: 193 * - Render Target Cache Flush Enable ([12] of DW1) 194 * - Depth Cache Flush Enable ([0] of DW1) 195 * - Stall at Pixel Scoreboard ([1] of DW1) 196 * - Depth Stall ([13] of DW1) 197 * - Post-Sync Operation ([13] of DW1) 198 * - Notify Enable ([8] of DW1)" 199 * 200 * The cache flushes require the workaround flush that triggered this 201 * one, so we can't use it. Depth stall would trigger the same. 202 * Post-sync nonzero is what triggered this second workaround, so we 203 * can't use that one either. Notify enable is IRQs, which aren't 204 * really our business. That leaves only stall at scoreboard. 205 */ 206 static int 207 gen6_emit_post_sync_nonzero_flush(struct i915_request *rq) 208 { 209 u32 scratch_addr = 210 intel_gt_scratch_offset(rq->engine->gt, 211 INTEL_GT_SCRATCH_FIELD_RENDER_FLUSH); 212 u32 *cs; 213 214 cs = intel_ring_begin(rq, 6); 215 if (IS_ERR(cs)) 216 return PTR_ERR(cs); 217 218 *cs++ = GFX_OP_PIPE_CONTROL(5); 219 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD; 220 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT; 221 *cs++ = 0; /* low dword */ 222 *cs++ = 0; /* high dword */ 223 *cs++ = MI_NOOP; 224 intel_ring_advance(rq, cs); 225 226 cs = intel_ring_begin(rq, 6); 227 if (IS_ERR(cs)) 228 return PTR_ERR(cs); 229 230 *cs++ = GFX_OP_PIPE_CONTROL(5); 231 *cs++ = PIPE_CONTROL_QW_WRITE; 232 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT; 233 *cs++ = 0; 234 *cs++ = 0; 235 *cs++ = MI_NOOP; 236 intel_ring_advance(rq, cs); 237 238 return 0; 239 } 240 241 static int 242 gen6_render_ring_flush(struct i915_request *rq, u32 mode) 243 { 244 u32 scratch_addr = 245 intel_gt_scratch_offset(rq->engine->gt, 246 INTEL_GT_SCRATCH_FIELD_RENDER_FLUSH); 247 u32 *cs, flags = 0; 248 int ret; 249 250 /* Force SNB workarounds for PIPE_CONTROL flushes */ 251 ret = gen6_emit_post_sync_nonzero_flush(rq); 252 if (ret) 253 return ret; 254 255 /* Just flush everything. Experiments have shown that reducing the 256 * number of bits based on the write domains has little performance 257 * impact. 258 */ 259 if (mode & EMIT_FLUSH) { 260 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH; 261 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH; 262 /* 263 * Ensure that any following seqno writes only happen 264 * when the render cache is indeed flushed. 265 */ 266 flags |= PIPE_CONTROL_CS_STALL; 267 } 268 if (mode & EMIT_INVALIDATE) { 269 flags |= PIPE_CONTROL_TLB_INVALIDATE; 270 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE; 271 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE; 272 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE; 273 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE; 274 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE; 275 /* 276 * TLB invalidate requires a post-sync write. 277 */ 278 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL; 279 } 280 281 cs = intel_ring_begin(rq, 4); 282 if (IS_ERR(cs)) 283 return PTR_ERR(cs); 284 285 *cs++ = GFX_OP_PIPE_CONTROL(4); 286 *cs++ = flags; 287 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT; 288 *cs++ = 0; 289 intel_ring_advance(rq, cs); 290 291 return 0; 292 } 293 294 static u32 *gen6_rcs_emit_breadcrumb(struct i915_request *rq, u32 *cs) 295 { 296 /* First we do the gen6_emit_post_sync_nonzero_flush w/a */ 297 *cs++ = GFX_OP_PIPE_CONTROL(4); 298 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD; 299 *cs++ = 0; 300 *cs++ = 0; 301 302 *cs++ = GFX_OP_PIPE_CONTROL(4); 303 *cs++ = PIPE_CONTROL_QW_WRITE; 304 *cs++ = intel_gt_scratch_offset(rq->engine->gt, 305 INTEL_GT_SCRATCH_FIELD_DEFAULT) | 306 PIPE_CONTROL_GLOBAL_GTT; 307 *cs++ = 0; 308 309 /* Finally we can flush and with it emit the breadcrumb */ 310 *cs++ = GFX_OP_PIPE_CONTROL(4); 311 *cs++ = (PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH | 312 PIPE_CONTROL_DEPTH_CACHE_FLUSH | 313 PIPE_CONTROL_DC_FLUSH_ENABLE | 314 PIPE_CONTROL_QW_WRITE | 315 PIPE_CONTROL_CS_STALL); 316 *cs++ = i915_request_active_timeline(rq)->hwsp_offset | 317 PIPE_CONTROL_GLOBAL_GTT; 318 *cs++ = rq->fence.seqno; 319 320 *cs++ = MI_USER_INTERRUPT; 321 *cs++ = MI_NOOP; 322 323 rq->tail = intel_ring_offset(rq, cs); 324 assert_ring_tail_valid(rq->ring, rq->tail); 325 326 return cs; 327 } 328 329 static int 330 gen7_render_ring_cs_stall_wa(struct i915_request *rq) 331 { 332 u32 *cs; 333 334 cs = intel_ring_begin(rq, 4); 335 if (IS_ERR(cs)) 336 return PTR_ERR(cs); 337 338 *cs++ = GFX_OP_PIPE_CONTROL(4); 339 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD; 340 *cs++ = 0; 341 *cs++ = 0; 342 intel_ring_advance(rq, cs); 343 344 return 0; 345 } 346 347 static int 348 gen7_render_ring_flush(struct i915_request *rq, u32 mode) 349 { 350 u32 scratch_addr = 351 intel_gt_scratch_offset(rq->engine->gt, 352 INTEL_GT_SCRATCH_FIELD_RENDER_FLUSH); 353 u32 *cs, flags = 0; 354 355 /* 356 * Ensure that any following seqno writes only happen when the render 357 * cache is indeed flushed. 358 * 359 * Workaround: 4th PIPE_CONTROL command (except the ones with only 360 * read-cache invalidate bits set) must have the CS_STALL bit set. We 361 * don't try to be clever and just set it unconditionally. 362 */ 363 flags |= PIPE_CONTROL_CS_STALL; 364 365 /* Just flush everything. Experiments have shown that reducing the 366 * number of bits based on the write domains has little performance 367 * impact. 368 */ 369 if (mode & EMIT_FLUSH) { 370 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH; 371 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH; 372 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE; 373 flags |= PIPE_CONTROL_FLUSH_ENABLE; 374 } 375 if (mode & EMIT_INVALIDATE) { 376 flags |= PIPE_CONTROL_TLB_INVALIDATE; 377 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE; 378 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE; 379 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE; 380 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE; 381 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE; 382 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR; 383 /* 384 * TLB invalidate requires a post-sync write. 385 */ 386 flags |= PIPE_CONTROL_QW_WRITE; 387 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB; 388 389 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD; 390 391 /* Workaround: we must issue a pipe_control with CS-stall bit 392 * set before a pipe_control command that has the state cache 393 * invalidate bit set. */ 394 gen7_render_ring_cs_stall_wa(rq); 395 } 396 397 cs = intel_ring_begin(rq, 4); 398 if (IS_ERR(cs)) 399 return PTR_ERR(cs); 400 401 *cs++ = GFX_OP_PIPE_CONTROL(4); 402 *cs++ = flags; 403 *cs++ = scratch_addr; 404 *cs++ = 0; 405 intel_ring_advance(rq, cs); 406 407 return 0; 408 } 409 410 static u32 *gen7_rcs_emit_breadcrumb(struct i915_request *rq, u32 *cs) 411 { 412 *cs++ = GFX_OP_PIPE_CONTROL(4); 413 *cs++ = (PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH | 414 PIPE_CONTROL_DEPTH_CACHE_FLUSH | 415 PIPE_CONTROL_DC_FLUSH_ENABLE | 416 PIPE_CONTROL_FLUSH_ENABLE | 417 PIPE_CONTROL_QW_WRITE | 418 PIPE_CONTROL_GLOBAL_GTT_IVB | 419 PIPE_CONTROL_CS_STALL); 420 *cs++ = i915_request_active_timeline(rq)->hwsp_offset; 421 *cs++ = rq->fence.seqno; 422 423 *cs++ = MI_USER_INTERRUPT; 424 *cs++ = MI_NOOP; 425 426 rq->tail = intel_ring_offset(rq, cs); 427 assert_ring_tail_valid(rq->ring, rq->tail); 428 429 return cs; 430 } 431 432 static u32 *gen6_xcs_emit_breadcrumb(struct i915_request *rq, u32 *cs) 433 { 434 GEM_BUG_ON(i915_request_active_timeline(rq)->hwsp_ggtt != rq->engine->status_page.vma); 435 GEM_BUG_ON(offset_in_page(i915_request_active_timeline(rq)->hwsp_offset) != I915_GEM_HWS_SEQNO_ADDR); 436 437 *cs++ = MI_FLUSH_DW | MI_FLUSH_DW_OP_STOREDW | MI_FLUSH_DW_STORE_INDEX; 438 *cs++ = I915_GEM_HWS_SEQNO_ADDR | MI_FLUSH_DW_USE_GTT; 439 *cs++ = rq->fence.seqno; 440 441 *cs++ = MI_USER_INTERRUPT; 442 443 rq->tail = intel_ring_offset(rq, cs); 444 assert_ring_tail_valid(rq->ring, rq->tail); 445 446 return cs; 447 } 448 449 #define GEN7_XCS_WA 32 450 static u32 *gen7_xcs_emit_breadcrumb(struct i915_request *rq, u32 *cs) 451 { 452 int i; 453 454 GEM_BUG_ON(i915_request_active_timeline(rq)->hwsp_ggtt != rq->engine->status_page.vma); 455 GEM_BUG_ON(offset_in_page(i915_request_active_timeline(rq)->hwsp_offset) != I915_GEM_HWS_SEQNO_ADDR); 456 457 *cs++ = MI_FLUSH_DW | MI_FLUSH_DW_OP_STOREDW | MI_FLUSH_DW_STORE_INDEX; 458 *cs++ = I915_GEM_HWS_SEQNO_ADDR | MI_FLUSH_DW_USE_GTT; 459 *cs++ = rq->fence.seqno; 460 461 for (i = 0; i < GEN7_XCS_WA; i++) { 462 *cs++ = MI_STORE_DWORD_INDEX; 463 *cs++ = I915_GEM_HWS_SEQNO_ADDR; 464 *cs++ = rq->fence.seqno; 465 } 466 467 *cs++ = MI_FLUSH_DW; 468 *cs++ = 0; 469 *cs++ = 0; 470 471 *cs++ = MI_USER_INTERRUPT; 472 *cs++ = MI_NOOP; 473 474 rq->tail = intel_ring_offset(rq, cs); 475 assert_ring_tail_valid(rq->ring, rq->tail); 476 477 return cs; 478 } 479 #undef GEN7_XCS_WA 480 481 static void set_hwstam(struct intel_engine_cs *engine, u32 mask) 482 { 483 /* 484 * Keep the render interrupt unmasked as this papers over 485 * lost interrupts following a reset. 486 */ 487 if (engine->class == RENDER_CLASS) { 488 if (INTEL_GEN(engine->i915) >= 6) 489 mask &= ~BIT(0); 490 else 491 mask &= ~I915_USER_INTERRUPT; 492 } 493 494 intel_engine_set_hwsp_writemask(engine, mask); 495 } 496 497 static void set_hws_pga(struct intel_engine_cs *engine, phys_addr_t phys) 498 { 499 struct drm_i915_private *dev_priv = engine->i915; 500 u32 addr; 501 502 addr = lower_32_bits(phys); 503 if (INTEL_GEN(dev_priv) >= 4) 504 addr |= (phys >> 28) & 0xf0; 505 506 I915_WRITE(HWS_PGA, addr); 507 } 508 509 static struct page *status_page(struct intel_engine_cs *engine) 510 { 511 struct drm_i915_gem_object *obj = engine->status_page.vma->obj; 512 513 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 514 return sg_page(obj->mm.pages->sgl); 515 } 516 517 static void ring_setup_phys_status_page(struct intel_engine_cs *engine) 518 { 519 set_hws_pga(engine, PFN_PHYS(page_to_pfn(status_page(engine)))); 520 set_hwstam(engine, ~0u); 521 } 522 523 static void set_hwsp(struct intel_engine_cs *engine, u32 offset) 524 { 525 struct drm_i915_private *dev_priv = engine->i915; 526 i915_reg_t hwsp; 527 528 /* 529 * The ring status page addresses are no longer next to the rest of 530 * the ring registers as of gen7. 531 */ 532 if (IS_GEN(dev_priv, 7)) { 533 switch (engine->id) { 534 /* 535 * No more rings exist on Gen7. Default case is only to shut up 536 * gcc switch check warning. 537 */ 538 default: 539 GEM_BUG_ON(engine->id); 540 /* fallthrough */ 541 case RCS0: 542 hwsp = RENDER_HWS_PGA_GEN7; 543 break; 544 case BCS0: 545 hwsp = BLT_HWS_PGA_GEN7; 546 break; 547 case VCS0: 548 hwsp = BSD_HWS_PGA_GEN7; 549 break; 550 case VECS0: 551 hwsp = VEBOX_HWS_PGA_GEN7; 552 break; 553 } 554 } else if (IS_GEN(dev_priv, 6)) { 555 hwsp = RING_HWS_PGA_GEN6(engine->mmio_base); 556 } else { 557 hwsp = RING_HWS_PGA(engine->mmio_base); 558 } 559 560 I915_WRITE(hwsp, offset); 561 POSTING_READ(hwsp); 562 } 563 564 static void flush_cs_tlb(struct intel_engine_cs *engine) 565 { 566 struct drm_i915_private *dev_priv = engine->i915; 567 568 if (!IS_GEN_RANGE(dev_priv, 6, 7)) 569 return; 570 571 /* ring should be idle before issuing a sync flush*/ 572 WARN_ON((ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0); 573 574 ENGINE_WRITE(engine, RING_INSTPM, 575 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE | 576 INSTPM_SYNC_FLUSH)); 577 if (intel_wait_for_register(engine->uncore, 578 RING_INSTPM(engine->mmio_base), 579 INSTPM_SYNC_FLUSH, 0, 580 1000)) 581 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n", 582 engine->name); 583 } 584 585 static void ring_setup_status_page(struct intel_engine_cs *engine) 586 { 587 set_hwsp(engine, i915_ggtt_offset(engine->status_page.vma)); 588 set_hwstam(engine, ~0u); 589 590 flush_cs_tlb(engine); 591 } 592 593 static bool stop_ring(struct intel_engine_cs *engine) 594 { 595 struct drm_i915_private *dev_priv = engine->i915; 596 597 if (INTEL_GEN(dev_priv) > 2) { 598 ENGINE_WRITE(engine, 599 RING_MI_MODE, _MASKED_BIT_ENABLE(STOP_RING)); 600 if (intel_wait_for_register(engine->uncore, 601 RING_MI_MODE(engine->mmio_base), 602 MODE_IDLE, 603 MODE_IDLE, 604 1000)) { 605 DRM_ERROR("%s : timed out trying to stop ring\n", 606 engine->name); 607 608 /* 609 * Sometimes we observe that the idle flag is not 610 * set even though the ring is empty. So double 611 * check before giving up. 612 */ 613 if (ENGINE_READ(engine, RING_HEAD) != 614 ENGINE_READ(engine, RING_TAIL)) 615 return false; 616 } 617 } 618 619 ENGINE_WRITE(engine, RING_HEAD, ENGINE_READ(engine, RING_TAIL)); 620 621 ENGINE_WRITE(engine, RING_HEAD, 0); 622 ENGINE_WRITE(engine, RING_TAIL, 0); 623 624 /* The ring must be empty before it is disabled */ 625 ENGINE_WRITE(engine, RING_CTL, 0); 626 627 return (ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR) == 0; 628 } 629 630 static int xcs_resume(struct intel_engine_cs *engine) 631 { 632 struct drm_i915_private *dev_priv = engine->i915; 633 struct intel_ring *ring = engine->legacy.ring; 634 int ret = 0; 635 636 GEM_TRACE("%s: ring:{HEAD:%04x, TAIL:%04x}\n", 637 engine->name, ring->head, ring->tail); 638 639 intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL); 640 641 /* WaClearRingBufHeadRegAtInit:ctg,elk */ 642 if (!stop_ring(engine)) { 643 /* G45 ring initialization often fails to reset head to zero */ 644 DRM_DEBUG_DRIVER("%s head not reset to zero " 645 "ctl %08x head %08x tail %08x start %08x\n", 646 engine->name, 647 ENGINE_READ(engine, RING_CTL), 648 ENGINE_READ(engine, RING_HEAD), 649 ENGINE_READ(engine, RING_TAIL), 650 ENGINE_READ(engine, RING_START)); 651 652 if (!stop_ring(engine)) { 653 DRM_ERROR("failed to set %s head to zero " 654 "ctl %08x head %08x tail %08x start %08x\n", 655 engine->name, 656 ENGINE_READ(engine, RING_CTL), 657 ENGINE_READ(engine, RING_HEAD), 658 ENGINE_READ(engine, RING_TAIL), 659 ENGINE_READ(engine, RING_START)); 660 ret = -EIO; 661 goto out; 662 } 663 } 664 665 if (HWS_NEEDS_PHYSICAL(dev_priv)) 666 ring_setup_phys_status_page(engine); 667 else 668 ring_setup_status_page(engine); 669 670 intel_engine_reset_breadcrumbs(engine); 671 672 /* Enforce ordering by reading HEAD register back */ 673 ENGINE_POSTING_READ(engine, RING_HEAD); 674 675 /* 676 * Initialize the ring. This must happen _after_ we've cleared the ring 677 * registers with the above sequence (the readback of the HEAD registers 678 * also enforces ordering), otherwise the hw might lose the new ring 679 * register values. 680 */ 681 ENGINE_WRITE(engine, RING_START, i915_ggtt_offset(ring->vma)); 682 683 /* Check that the ring offsets point within the ring! */ 684 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->head)); 685 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->tail)); 686 intel_ring_update_space(ring); 687 688 /* First wake the ring up to an empty/idle ring */ 689 ENGINE_WRITE(engine, RING_HEAD, ring->head); 690 ENGINE_WRITE(engine, RING_TAIL, ring->head); 691 ENGINE_POSTING_READ(engine, RING_TAIL); 692 693 ENGINE_WRITE(engine, RING_CTL, RING_CTL_SIZE(ring->size) | RING_VALID); 694 695 /* If the head is still not zero, the ring is dead */ 696 if (intel_wait_for_register(engine->uncore, 697 RING_CTL(engine->mmio_base), 698 RING_VALID, RING_VALID, 699 50)) { 700 DRM_ERROR("%s initialization failed " 701 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n", 702 engine->name, 703 ENGINE_READ(engine, RING_CTL), 704 ENGINE_READ(engine, RING_CTL) & RING_VALID, 705 ENGINE_READ(engine, RING_HEAD), ring->head, 706 ENGINE_READ(engine, RING_TAIL), ring->tail, 707 ENGINE_READ(engine, RING_START), 708 i915_ggtt_offset(ring->vma)); 709 ret = -EIO; 710 goto out; 711 } 712 713 if (INTEL_GEN(dev_priv) > 2) 714 ENGINE_WRITE(engine, 715 RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING)); 716 717 /* Now awake, let it get started */ 718 if (ring->tail != ring->head) { 719 ENGINE_WRITE(engine, RING_TAIL, ring->tail); 720 ENGINE_POSTING_READ(engine, RING_TAIL); 721 } 722 723 /* Papering over lost _interrupts_ immediately following the restart */ 724 intel_engine_queue_breadcrumbs(engine); 725 out: 726 intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL); 727 728 return ret; 729 } 730 731 static void reset_prepare(struct intel_engine_cs *engine) 732 { 733 struct intel_uncore *uncore = engine->uncore; 734 const u32 base = engine->mmio_base; 735 736 /* 737 * We stop engines, otherwise we might get failed reset and a 738 * dead gpu (on elk). Also as modern gpu as kbl can suffer 739 * from system hang if batchbuffer is progressing when 740 * the reset is issued, regardless of READY_TO_RESET ack. 741 * Thus assume it is best to stop engines on all gens 742 * where we have a gpu reset. 743 * 744 * WaKBLVECSSemaphoreWaitPoll:kbl (on ALL_ENGINES) 745 * 746 * WaMediaResetMainRingCleanup:ctg,elk (presumably) 747 * 748 * FIXME: Wa for more modern gens needs to be validated 749 */ 750 GEM_TRACE("%s\n", engine->name); 751 752 if (intel_engine_stop_cs(engine)) 753 GEM_TRACE("%s: timed out on STOP_RING\n", engine->name); 754 755 intel_uncore_write_fw(uncore, 756 RING_HEAD(base), 757 intel_uncore_read_fw(uncore, RING_TAIL(base))); 758 intel_uncore_posting_read_fw(uncore, RING_HEAD(base)); /* paranoia */ 759 760 intel_uncore_write_fw(uncore, RING_HEAD(base), 0); 761 intel_uncore_write_fw(uncore, RING_TAIL(base), 0); 762 intel_uncore_posting_read_fw(uncore, RING_TAIL(base)); 763 764 /* The ring must be empty before it is disabled */ 765 intel_uncore_write_fw(uncore, RING_CTL(base), 0); 766 767 /* Check acts as a post */ 768 if (intel_uncore_read_fw(uncore, RING_HEAD(base))) 769 GEM_TRACE("%s: ring head [%x] not parked\n", 770 engine->name, 771 intel_uncore_read_fw(uncore, RING_HEAD(base))); 772 } 773 774 static void reset_ring(struct intel_engine_cs *engine, bool stalled) 775 { 776 struct i915_request *pos, *rq; 777 unsigned long flags; 778 u32 head; 779 780 rq = NULL; 781 spin_lock_irqsave(&engine->active.lock, flags); 782 list_for_each_entry(pos, &engine->active.requests, sched.link) { 783 if (!i915_request_completed(pos)) { 784 rq = pos; 785 break; 786 } 787 } 788 789 /* 790 * The guilty request will get skipped on a hung engine. 791 * 792 * Users of client default contexts do not rely on logical 793 * state preserved between batches so it is safe to execute 794 * queued requests following the hang. Non default contexts 795 * rely on preserved state, so skipping a batch loses the 796 * evolution of the state and it needs to be considered corrupted. 797 * Executing more queued batches on top of corrupted state is 798 * risky. But we take the risk by trying to advance through 799 * the queued requests in order to make the client behaviour 800 * more predictable around resets, by not throwing away random 801 * amount of batches it has prepared for execution. Sophisticated 802 * clients can use gem_reset_stats_ioctl and dma fence status 803 * (exported via sync_file info ioctl on explicit fences) to observe 804 * when it loses the context state and should rebuild accordingly. 805 * 806 * The context ban, and ultimately the client ban, mechanism are safety 807 * valves if client submission ends up resulting in nothing more than 808 * subsequent hangs. 809 */ 810 811 if (rq) { 812 /* 813 * Try to restore the logical GPU state to match the 814 * continuation of the request queue. If we skip the 815 * context/PD restore, then the next request may try to execute 816 * assuming that its context is valid and loaded on the GPU and 817 * so may try to access invalid memory, prompting repeated GPU 818 * hangs. 819 * 820 * If the request was guilty, we still restore the logical 821 * state in case the next request requires it (e.g. the 822 * aliasing ppgtt), but skip over the hung batch. 823 * 824 * If the request was innocent, we try to replay the request 825 * with the restored context. 826 */ 827 __i915_request_reset(rq, stalled); 828 829 GEM_BUG_ON(rq->ring != engine->legacy.ring); 830 head = rq->head; 831 } else { 832 head = engine->legacy.ring->tail; 833 } 834 engine->legacy.ring->head = intel_ring_wrap(engine->legacy.ring, head); 835 836 spin_unlock_irqrestore(&engine->active.lock, flags); 837 } 838 839 static void reset_finish(struct intel_engine_cs *engine) 840 { 841 } 842 843 static int rcs_resume(struct intel_engine_cs *engine) 844 { 845 struct drm_i915_private *dev_priv = engine->i915; 846 847 /* 848 * Disable CONSTANT_BUFFER before it is loaded from the context 849 * image. For as it is loaded, it is executed and the stored 850 * address may no longer be valid, leading to a GPU hang. 851 * 852 * This imposes the requirement that userspace reload their 853 * CONSTANT_BUFFER on every batch, fortunately a requirement 854 * they are already accustomed to from before contexts were 855 * enabled. 856 */ 857 if (IS_GEN(dev_priv, 4)) 858 I915_WRITE(ECOSKPD, 859 _MASKED_BIT_ENABLE(ECO_CONSTANT_BUFFER_SR_DISABLE)); 860 861 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */ 862 if (IS_GEN_RANGE(dev_priv, 4, 6)) 863 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH)); 864 865 /* We need to disable the AsyncFlip performance optimisations in order 866 * to use MI_WAIT_FOR_EVENT within the CS. It should already be 867 * programmed to '1' on all products. 868 * 869 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv 870 */ 871 if (IS_GEN_RANGE(dev_priv, 6, 7)) 872 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE)); 873 874 /* Required for the hardware to program scanline values for waiting */ 875 /* WaEnableFlushTlbInvalidationMode:snb */ 876 if (IS_GEN(dev_priv, 6)) 877 I915_WRITE(GFX_MODE, 878 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT)); 879 880 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */ 881 if (IS_GEN(dev_priv, 7)) 882 I915_WRITE(GFX_MODE_GEN7, 883 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) | 884 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE)); 885 886 if (IS_GEN(dev_priv, 6)) { 887 /* From the Sandybridge PRM, volume 1 part 3, page 24: 888 * "If this bit is set, STCunit will have LRA as replacement 889 * policy. [...] This bit must be reset. LRA replacement 890 * policy is not supported." 891 */ 892 I915_WRITE(CACHE_MODE_0, 893 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB)); 894 } 895 896 if (IS_GEN_RANGE(dev_priv, 6, 7)) 897 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING)); 898 899 return xcs_resume(engine); 900 } 901 902 static void cancel_requests(struct intel_engine_cs *engine) 903 { 904 struct i915_request *request; 905 unsigned long flags; 906 907 spin_lock_irqsave(&engine->active.lock, flags); 908 909 /* Mark all submitted requests as skipped. */ 910 list_for_each_entry(request, &engine->active.requests, sched.link) { 911 if (!i915_request_signaled(request)) 912 dma_fence_set_error(&request->fence, -EIO); 913 914 i915_request_mark_complete(request); 915 } 916 917 /* Remaining _unready_ requests will be nop'ed when submitted */ 918 919 spin_unlock_irqrestore(&engine->active.lock, flags); 920 } 921 922 static void i9xx_submit_request(struct i915_request *request) 923 { 924 i915_request_submit(request); 925 wmb(); /* paranoid flush writes out of the WCB before mmio */ 926 927 ENGINE_WRITE(request->engine, RING_TAIL, 928 intel_ring_set_tail(request->ring, request->tail)); 929 } 930 931 static u32 *i9xx_emit_breadcrumb(struct i915_request *rq, u32 *cs) 932 { 933 GEM_BUG_ON(i915_request_active_timeline(rq)->hwsp_ggtt != rq->engine->status_page.vma); 934 GEM_BUG_ON(offset_in_page(i915_request_active_timeline(rq)->hwsp_offset) != I915_GEM_HWS_SEQNO_ADDR); 935 936 *cs++ = MI_FLUSH; 937 938 *cs++ = MI_STORE_DWORD_INDEX; 939 *cs++ = I915_GEM_HWS_SEQNO_ADDR; 940 *cs++ = rq->fence.seqno; 941 942 *cs++ = MI_USER_INTERRUPT; 943 *cs++ = MI_NOOP; 944 945 rq->tail = intel_ring_offset(rq, cs); 946 assert_ring_tail_valid(rq->ring, rq->tail); 947 948 return cs; 949 } 950 951 #define GEN5_WA_STORES 8 /* must be at least 1! */ 952 static u32 *gen5_emit_breadcrumb(struct i915_request *rq, u32 *cs) 953 { 954 int i; 955 956 GEM_BUG_ON(i915_request_active_timeline(rq)->hwsp_ggtt != rq->engine->status_page.vma); 957 GEM_BUG_ON(offset_in_page(i915_request_active_timeline(rq)->hwsp_offset) != I915_GEM_HWS_SEQNO_ADDR); 958 959 *cs++ = MI_FLUSH; 960 961 BUILD_BUG_ON(GEN5_WA_STORES < 1); 962 for (i = 0; i < GEN5_WA_STORES; i++) { 963 *cs++ = MI_STORE_DWORD_INDEX; 964 *cs++ = I915_GEM_HWS_SEQNO_ADDR; 965 *cs++ = rq->fence.seqno; 966 } 967 968 *cs++ = MI_USER_INTERRUPT; 969 970 rq->tail = intel_ring_offset(rq, cs); 971 assert_ring_tail_valid(rq->ring, rq->tail); 972 973 return cs; 974 } 975 #undef GEN5_WA_STORES 976 977 static void 978 gen5_irq_enable(struct intel_engine_cs *engine) 979 { 980 gen5_gt_enable_irq(engine->gt, engine->irq_enable_mask); 981 } 982 983 static void 984 gen5_irq_disable(struct intel_engine_cs *engine) 985 { 986 gen5_gt_disable_irq(engine->gt, engine->irq_enable_mask); 987 } 988 989 static void 990 i9xx_irq_enable(struct intel_engine_cs *engine) 991 { 992 engine->i915->irq_mask &= ~engine->irq_enable_mask; 993 intel_uncore_write(engine->uncore, GEN2_IMR, engine->i915->irq_mask); 994 intel_uncore_posting_read_fw(engine->uncore, GEN2_IMR); 995 } 996 997 static void 998 i9xx_irq_disable(struct intel_engine_cs *engine) 999 { 1000 engine->i915->irq_mask |= engine->irq_enable_mask; 1001 intel_uncore_write(engine->uncore, GEN2_IMR, engine->i915->irq_mask); 1002 } 1003 1004 static void 1005 i8xx_irq_enable(struct intel_engine_cs *engine) 1006 { 1007 struct drm_i915_private *i915 = engine->i915; 1008 1009 i915->irq_mask &= ~engine->irq_enable_mask; 1010 intel_uncore_write16(&i915->uncore, GEN2_IMR, i915->irq_mask); 1011 ENGINE_POSTING_READ16(engine, RING_IMR); 1012 } 1013 1014 static void 1015 i8xx_irq_disable(struct intel_engine_cs *engine) 1016 { 1017 struct drm_i915_private *i915 = engine->i915; 1018 1019 i915->irq_mask |= engine->irq_enable_mask; 1020 intel_uncore_write16(&i915->uncore, GEN2_IMR, i915->irq_mask); 1021 } 1022 1023 static int 1024 bsd_ring_flush(struct i915_request *rq, u32 mode) 1025 { 1026 u32 *cs; 1027 1028 cs = intel_ring_begin(rq, 2); 1029 if (IS_ERR(cs)) 1030 return PTR_ERR(cs); 1031 1032 *cs++ = MI_FLUSH; 1033 *cs++ = MI_NOOP; 1034 intel_ring_advance(rq, cs); 1035 return 0; 1036 } 1037 1038 static void 1039 gen6_irq_enable(struct intel_engine_cs *engine) 1040 { 1041 ENGINE_WRITE(engine, RING_IMR, 1042 ~(engine->irq_enable_mask | engine->irq_keep_mask)); 1043 1044 /* Flush/delay to ensure the RING_IMR is active before the GT IMR */ 1045 ENGINE_POSTING_READ(engine, RING_IMR); 1046 1047 gen5_gt_enable_irq(engine->gt, engine->irq_enable_mask); 1048 } 1049 1050 static void 1051 gen6_irq_disable(struct intel_engine_cs *engine) 1052 { 1053 ENGINE_WRITE(engine, RING_IMR, ~engine->irq_keep_mask); 1054 gen5_gt_disable_irq(engine->gt, engine->irq_enable_mask); 1055 } 1056 1057 static void 1058 hsw_vebox_irq_enable(struct intel_engine_cs *engine) 1059 { 1060 ENGINE_WRITE(engine, RING_IMR, ~engine->irq_enable_mask); 1061 1062 /* Flush/delay to ensure the RING_IMR is active before the GT IMR */ 1063 ENGINE_POSTING_READ(engine, RING_IMR); 1064 1065 gen6_gt_pm_unmask_irq(engine->gt, engine->irq_enable_mask); 1066 } 1067 1068 static void 1069 hsw_vebox_irq_disable(struct intel_engine_cs *engine) 1070 { 1071 ENGINE_WRITE(engine, RING_IMR, ~0); 1072 gen6_gt_pm_mask_irq(engine->gt, engine->irq_enable_mask); 1073 } 1074 1075 static int 1076 i965_emit_bb_start(struct i915_request *rq, 1077 u64 offset, u32 length, 1078 unsigned int dispatch_flags) 1079 { 1080 u32 *cs; 1081 1082 cs = intel_ring_begin(rq, 2); 1083 if (IS_ERR(cs)) 1084 return PTR_ERR(cs); 1085 1086 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags & 1087 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965); 1088 *cs++ = offset; 1089 intel_ring_advance(rq, cs); 1090 1091 return 0; 1092 } 1093 1094 /* Just userspace ABI convention to limit the wa batch bo to a resonable size */ 1095 #define I830_BATCH_LIMIT SZ_256K 1096 #define I830_TLB_ENTRIES (2) 1097 #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT) 1098 static int 1099 i830_emit_bb_start(struct i915_request *rq, 1100 u64 offset, u32 len, 1101 unsigned int dispatch_flags) 1102 { 1103 u32 *cs, cs_offset = 1104 intel_gt_scratch_offset(rq->engine->gt, 1105 INTEL_GT_SCRATCH_FIELD_DEFAULT); 1106 1107 GEM_BUG_ON(rq->engine->gt->scratch->size < I830_WA_SIZE); 1108 1109 cs = intel_ring_begin(rq, 6); 1110 if (IS_ERR(cs)) 1111 return PTR_ERR(cs); 1112 1113 /* Evict the invalid PTE TLBs */ 1114 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA; 1115 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096; 1116 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */ 1117 *cs++ = cs_offset; 1118 *cs++ = 0xdeadbeef; 1119 *cs++ = MI_NOOP; 1120 intel_ring_advance(rq, cs); 1121 1122 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) { 1123 if (len > I830_BATCH_LIMIT) 1124 return -ENOSPC; 1125 1126 cs = intel_ring_begin(rq, 6 + 2); 1127 if (IS_ERR(cs)) 1128 return PTR_ERR(cs); 1129 1130 /* Blit the batch (which has now all relocs applied) to the 1131 * stable batch scratch bo area (so that the CS never 1132 * stumbles over its tlb invalidation bug) ... 1133 */ 1134 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA | (6 - 2); 1135 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096; 1136 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096; 1137 *cs++ = cs_offset; 1138 *cs++ = 4096; 1139 *cs++ = offset; 1140 1141 *cs++ = MI_FLUSH; 1142 *cs++ = MI_NOOP; 1143 intel_ring_advance(rq, cs); 1144 1145 /* ... and execute it. */ 1146 offset = cs_offset; 1147 } 1148 1149 cs = intel_ring_begin(rq, 2); 1150 if (IS_ERR(cs)) 1151 return PTR_ERR(cs); 1152 1153 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT; 1154 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 : 1155 MI_BATCH_NON_SECURE); 1156 intel_ring_advance(rq, cs); 1157 1158 return 0; 1159 } 1160 1161 static int 1162 i915_emit_bb_start(struct i915_request *rq, 1163 u64 offset, u32 len, 1164 unsigned int dispatch_flags) 1165 { 1166 u32 *cs; 1167 1168 cs = intel_ring_begin(rq, 2); 1169 if (IS_ERR(cs)) 1170 return PTR_ERR(cs); 1171 1172 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT; 1173 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 : 1174 MI_BATCH_NON_SECURE); 1175 intel_ring_advance(rq, cs); 1176 1177 return 0; 1178 } 1179 1180 static void __ring_context_fini(struct intel_context *ce) 1181 { 1182 i915_vma_put(ce->state); 1183 } 1184 1185 static void ring_context_destroy(struct kref *ref) 1186 { 1187 struct intel_context *ce = container_of(ref, typeof(*ce), ref); 1188 1189 GEM_BUG_ON(intel_context_is_pinned(ce)); 1190 1191 if (ce->state) 1192 __ring_context_fini(ce); 1193 1194 intel_context_fini(ce); 1195 intel_context_free(ce); 1196 } 1197 1198 static struct i915_address_space *vm_alias(struct intel_context *ce) 1199 { 1200 struct i915_address_space *vm; 1201 1202 vm = ce->vm; 1203 if (i915_is_ggtt(vm)) 1204 vm = &i915_vm_to_ggtt(vm)->alias->vm; 1205 1206 return vm; 1207 } 1208 1209 static int __context_pin_ppgtt(struct intel_context *ce) 1210 { 1211 struct i915_address_space *vm; 1212 int err = 0; 1213 1214 vm = vm_alias(ce); 1215 if (vm) 1216 err = gen6_ppgtt_pin(i915_vm_to_ppgtt((vm))); 1217 1218 return err; 1219 } 1220 1221 static void __context_unpin_ppgtt(struct intel_context *ce) 1222 { 1223 struct i915_address_space *vm; 1224 1225 vm = vm_alias(ce); 1226 if (vm) 1227 gen6_ppgtt_unpin(i915_vm_to_ppgtt(vm)); 1228 } 1229 1230 static void ring_context_unpin(struct intel_context *ce) 1231 { 1232 __context_unpin_ppgtt(ce); 1233 } 1234 1235 static struct i915_vma * 1236 alloc_context_vma(struct intel_engine_cs *engine) 1237 { 1238 struct drm_i915_private *i915 = engine->i915; 1239 struct drm_i915_gem_object *obj; 1240 struct i915_vma *vma; 1241 int err; 1242 1243 obj = i915_gem_object_create_shmem(i915, engine->context_size); 1244 if (IS_ERR(obj)) 1245 return ERR_CAST(obj); 1246 1247 /* 1248 * Try to make the context utilize L3 as well as LLC. 1249 * 1250 * On VLV we don't have L3 controls in the PTEs so we 1251 * shouldn't touch the cache level, especially as that 1252 * would make the object snooped which might have a 1253 * negative performance impact. 1254 * 1255 * Snooping is required on non-llc platforms in execlist 1256 * mode, but since all GGTT accesses use PAT entry 0 we 1257 * get snooping anyway regardless of cache_level. 1258 * 1259 * This is only applicable for Ivy Bridge devices since 1260 * later platforms don't have L3 control bits in the PTE. 1261 */ 1262 if (IS_IVYBRIDGE(i915)) 1263 i915_gem_object_set_cache_coherency(obj, I915_CACHE_L3_LLC); 1264 1265 if (engine->default_state) { 1266 void *defaults, *vaddr; 1267 1268 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB); 1269 if (IS_ERR(vaddr)) { 1270 err = PTR_ERR(vaddr); 1271 goto err_obj; 1272 } 1273 1274 defaults = i915_gem_object_pin_map(engine->default_state, 1275 I915_MAP_WB); 1276 if (IS_ERR(defaults)) { 1277 err = PTR_ERR(defaults); 1278 goto err_map; 1279 } 1280 1281 memcpy(vaddr, defaults, engine->context_size); 1282 i915_gem_object_unpin_map(engine->default_state); 1283 1284 i915_gem_object_flush_map(obj); 1285 i915_gem_object_unpin_map(obj); 1286 } 1287 1288 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL); 1289 if (IS_ERR(vma)) { 1290 err = PTR_ERR(vma); 1291 goto err_obj; 1292 } 1293 1294 return vma; 1295 1296 err_map: 1297 i915_gem_object_unpin_map(obj); 1298 err_obj: 1299 i915_gem_object_put(obj); 1300 return ERR_PTR(err); 1301 } 1302 1303 static int ring_context_alloc(struct intel_context *ce) 1304 { 1305 struct intel_engine_cs *engine = ce->engine; 1306 1307 /* One ringbuffer to rule them all */ 1308 GEM_BUG_ON(!engine->legacy.ring); 1309 ce->ring = engine->legacy.ring; 1310 ce->timeline = intel_timeline_get(engine->legacy.timeline); 1311 1312 GEM_BUG_ON(ce->state); 1313 if (engine->context_size) { 1314 struct i915_vma *vma; 1315 1316 vma = alloc_context_vma(engine); 1317 if (IS_ERR(vma)) 1318 return PTR_ERR(vma); 1319 1320 ce->state = vma; 1321 } 1322 1323 return 0; 1324 } 1325 1326 static int ring_context_pin(struct intel_context *ce) 1327 { 1328 int err; 1329 1330 err = intel_context_active_acquire(ce); 1331 if (err) 1332 return err; 1333 1334 err = __context_pin_ppgtt(ce); 1335 if (err) 1336 goto err_active; 1337 1338 return 0; 1339 1340 err_active: 1341 intel_context_active_release(ce); 1342 return err; 1343 } 1344 1345 static void ring_context_reset(struct intel_context *ce) 1346 { 1347 intel_ring_reset(ce->ring, 0); 1348 } 1349 1350 static const struct intel_context_ops ring_context_ops = { 1351 .alloc = ring_context_alloc, 1352 1353 .pin = ring_context_pin, 1354 .unpin = ring_context_unpin, 1355 1356 .enter = intel_context_enter_engine, 1357 .exit = intel_context_exit_engine, 1358 1359 .reset = ring_context_reset, 1360 .destroy = ring_context_destroy, 1361 }; 1362 1363 static int load_pd_dir(struct i915_request *rq, const struct i915_ppgtt *ppgtt) 1364 { 1365 const struct intel_engine_cs * const engine = rq->engine; 1366 u32 *cs; 1367 1368 cs = intel_ring_begin(rq, 6); 1369 if (IS_ERR(cs)) 1370 return PTR_ERR(cs); 1371 1372 *cs++ = MI_LOAD_REGISTER_IMM(1); 1373 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine->mmio_base)); 1374 *cs++ = PP_DIR_DCLV_2G; 1375 1376 *cs++ = MI_LOAD_REGISTER_IMM(1); 1377 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base)); 1378 *cs++ = px_base(ppgtt->pd)->ggtt_offset << 10; 1379 1380 intel_ring_advance(rq, cs); 1381 1382 return 0; 1383 } 1384 1385 static int flush_pd_dir(struct i915_request *rq) 1386 { 1387 const struct intel_engine_cs * const engine = rq->engine; 1388 u32 *cs; 1389 1390 cs = intel_ring_begin(rq, 4); 1391 if (IS_ERR(cs)) 1392 return PTR_ERR(cs); 1393 1394 /* Stall until the page table load is complete */ 1395 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT; 1396 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base)); 1397 *cs++ = intel_gt_scratch_offset(rq->engine->gt, 1398 INTEL_GT_SCRATCH_FIELD_DEFAULT); 1399 *cs++ = MI_NOOP; 1400 1401 intel_ring_advance(rq, cs); 1402 return 0; 1403 } 1404 1405 static inline int mi_set_context(struct i915_request *rq, u32 flags) 1406 { 1407 struct drm_i915_private *i915 = rq->i915; 1408 struct intel_engine_cs *engine = rq->engine; 1409 enum intel_engine_id id; 1410 const int num_engines = 1411 IS_HASWELL(i915) ? RUNTIME_INFO(i915)->num_engines - 1 : 0; 1412 bool force_restore = false; 1413 int len; 1414 u32 *cs; 1415 1416 len = 4; 1417 if (IS_GEN(i915, 7)) 1418 len += 2 + (num_engines ? 4 * num_engines + 6 : 0); 1419 else if (IS_GEN(i915, 5)) 1420 len += 2; 1421 if (flags & MI_FORCE_RESTORE) { 1422 GEM_BUG_ON(flags & MI_RESTORE_INHIBIT); 1423 flags &= ~MI_FORCE_RESTORE; 1424 force_restore = true; 1425 len += 2; 1426 } 1427 1428 cs = intel_ring_begin(rq, len); 1429 if (IS_ERR(cs)) 1430 return PTR_ERR(cs); 1431 1432 /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */ 1433 if (IS_GEN(i915, 7)) { 1434 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE; 1435 if (num_engines) { 1436 struct intel_engine_cs *signaller; 1437 1438 *cs++ = MI_LOAD_REGISTER_IMM(num_engines); 1439 for_each_engine(signaller, engine->gt, id) { 1440 if (signaller == engine) 1441 continue; 1442 1443 *cs++ = i915_mmio_reg_offset( 1444 RING_PSMI_CTL(signaller->mmio_base)); 1445 *cs++ = _MASKED_BIT_ENABLE( 1446 GEN6_PSMI_SLEEP_MSG_DISABLE); 1447 } 1448 } 1449 } else if (IS_GEN(i915, 5)) { 1450 /* 1451 * This w/a is only listed for pre-production ilk a/b steppings, 1452 * but is also mentioned for programming the powerctx. To be 1453 * safe, just apply the workaround; we do not use SyncFlush so 1454 * this should never take effect and so be a no-op! 1455 */ 1456 *cs++ = MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN; 1457 } 1458 1459 if (force_restore) { 1460 /* 1461 * The HW doesn't handle being told to restore the current 1462 * context very well. Quite often it likes goes to go off and 1463 * sulk, especially when it is meant to be reloading PP_DIR. 1464 * A very simple fix to force the reload is to simply switch 1465 * away from the current context and back again. 1466 * 1467 * Note that the kernel_context will contain random state 1468 * following the INHIBIT_RESTORE. We accept this since we 1469 * never use the kernel_context state; it is merely a 1470 * placeholder we use to flush other contexts. 1471 */ 1472 *cs++ = MI_SET_CONTEXT; 1473 *cs++ = i915_ggtt_offset(engine->kernel_context->state) | 1474 MI_MM_SPACE_GTT | 1475 MI_RESTORE_INHIBIT; 1476 } 1477 1478 *cs++ = MI_NOOP; 1479 *cs++ = MI_SET_CONTEXT; 1480 *cs++ = i915_ggtt_offset(rq->hw_context->state) | flags; 1481 /* 1482 * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP 1483 * WaMiSetContext_Hang:snb,ivb,vlv 1484 */ 1485 *cs++ = MI_NOOP; 1486 1487 if (IS_GEN(i915, 7)) { 1488 if (num_engines) { 1489 struct intel_engine_cs *signaller; 1490 i915_reg_t last_reg = {}; /* keep gcc quiet */ 1491 1492 *cs++ = MI_LOAD_REGISTER_IMM(num_engines); 1493 for_each_engine(signaller, engine->gt, id) { 1494 if (signaller == engine) 1495 continue; 1496 1497 last_reg = RING_PSMI_CTL(signaller->mmio_base); 1498 *cs++ = i915_mmio_reg_offset(last_reg); 1499 *cs++ = _MASKED_BIT_DISABLE( 1500 GEN6_PSMI_SLEEP_MSG_DISABLE); 1501 } 1502 1503 /* Insert a delay before the next switch! */ 1504 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT; 1505 *cs++ = i915_mmio_reg_offset(last_reg); 1506 *cs++ = intel_gt_scratch_offset(engine->gt, 1507 INTEL_GT_SCRATCH_FIELD_DEFAULT); 1508 *cs++ = MI_NOOP; 1509 } 1510 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; 1511 } else if (IS_GEN(i915, 5)) { 1512 *cs++ = MI_SUSPEND_FLUSH; 1513 } 1514 1515 intel_ring_advance(rq, cs); 1516 1517 return 0; 1518 } 1519 1520 static int remap_l3_slice(struct i915_request *rq, int slice) 1521 { 1522 u32 *cs, *remap_info = rq->i915->l3_parity.remap_info[slice]; 1523 int i; 1524 1525 if (!remap_info) 1526 return 0; 1527 1528 cs = intel_ring_begin(rq, GEN7_L3LOG_SIZE/4 * 2 + 2); 1529 if (IS_ERR(cs)) 1530 return PTR_ERR(cs); 1531 1532 /* 1533 * Note: We do not worry about the concurrent register cacheline hang 1534 * here because no other code should access these registers other than 1535 * at initialization time. 1536 */ 1537 *cs++ = MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4); 1538 for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) { 1539 *cs++ = i915_mmio_reg_offset(GEN7_L3LOG(slice, i)); 1540 *cs++ = remap_info[i]; 1541 } 1542 *cs++ = MI_NOOP; 1543 intel_ring_advance(rq, cs); 1544 1545 return 0; 1546 } 1547 1548 static int remap_l3(struct i915_request *rq) 1549 { 1550 struct i915_gem_context *ctx = rq->gem_context; 1551 int i, err; 1552 1553 if (!ctx->remap_slice) 1554 return 0; 1555 1556 for (i = 0; i < MAX_L3_SLICES; i++) { 1557 if (!(ctx->remap_slice & BIT(i))) 1558 continue; 1559 1560 err = remap_l3_slice(rq, i); 1561 if (err) 1562 return err; 1563 } 1564 1565 ctx->remap_slice = 0; 1566 return 0; 1567 } 1568 1569 static int switch_context(struct i915_request *rq) 1570 { 1571 struct intel_context *ce = rq->hw_context; 1572 struct i915_address_space *vm = vm_alias(ce); 1573 int ret; 1574 1575 GEM_BUG_ON(HAS_EXECLISTS(rq->i915)); 1576 1577 if (vm) { 1578 ret = load_pd_dir(rq, i915_vm_to_ppgtt(vm)); 1579 if (ret) 1580 return ret; 1581 } 1582 1583 if (ce->state) { 1584 u32 flags; 1585 1586 GEM_BUG_ON(rq->engine->id != RCS0); 1587 1588 /* For resource streamer on HSW+ and power context elsewhere */ 1589 BUILD_BUG_ON(HSW_MI_RS_SAVE_STATE_EN != MI_SAVE_EXT_STATE_EN); 1590 BUILD_BUG_ON(HSW_MI_RS_RESTORE_STATE_EN != MI_RESTORE_EXT_STATE_EN); 1591 1592 flags = MI_SAVE_EXT_STATE_EN | MI_MM_SPACE_GTT; 1593 if (!i915_gem_context_is_kernel(rq->gem_context)) 1594 flags |= MI_RESTORE_EXT_STATE_EN; 1595 else 1596 flags |= MI_RESTORE_INHIBIT; 1597 1598 ret = mi_set_context(rq, flags); 1599 if (ret) 1600 return ret; 1601 } 1602 1603 if (vm) { 1604 struct intel_engine_cs *engine = rq->engine; 1605 1606 ret = engine->emit_flush(rq, EMIT_INVALIDATE); 1607 if (ret) 1608 return ret; 1609 1610 ret = flush_pd_dir(rq); 1611 if (ret) 1612 return ret; 1613 1614 /* 1615 * Not only do we need a full barrier (post-sync write) after 1616 * invalidating the TLBs, but we need to wait a little bit 1617 * longer. Whether this is merely delaying us, or the 1618 * subsequent flush is a key part of serialising with the 1619 * post-sync op, this extra pass appears vital before a 1620 * mm switch! 1621 */ 1622 ret = engine->emit_flush(rq, EMIT_INVALIDATE); 1623 if (ret) 1624 return ret; 1625 1626 ret = engine->emit_flush(rq, EMIT_FLUSH); 1627 if (ret) 1628 return ret; 1629 } 1630 1631 ret = remap_l3(rq); 1632 if (ret) 1633 return ret; 1634 1635 return 0; 1636 } 1637 1638 static int ring_request_alloc(struct i915_request *request) 1639 { 1640 int ret; 1641 1642 GEM_BUG_ON(!intel_context_is_pinned(request->hw_context)); 1643 GEM_BUG_ON(i915_request_timeline(request)->has_initial_breadcrumb); 1644 1645 /* 1646 * Flush enough space to reduce the likelihood of waiting after 1647 * we start building the request - in which case we will just 1648 * have to repeat work. 1649 */ 1650 request->reserved_space += LEGACY_REQUEST_SIZE; 1651 1652 /* Unconditionally invalidate GPU caches and TLBs. */ 1653 ret = request->engine->emit_flush(request, EMIT_INVALIDATE); 1654 if (ret) 1655 return ret; 1656 1657 ret = switch_context(request); 1658 if (ret) 1659 return ret; 1660 1661 request->reserved_space -= LEGACY_REQUEST_SIZE; 1662 return 0; 1663 } 1664 1665 static void gen6_bsd_submit_request(struct i915_request *request) 1666 { 1667 struct intel_uncore *uncore = request->engine->uncore; 1668 1669 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL); 1670 1671 /* Every tail move must follow the sequence below */ 1672 1673 /* Disable notification that the ring is IDLE. The GT 1674 * will then assume that it is busy and bring it out of rc6. 1675 */ 1676 intel_uncore_write_fw(uncore, GEN6_BSD_SLEEP_PSMI_CONTROL, 1677 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE)); 1678 1679 /* Clear the context id. Here be magic! */ 1680 intel_uncore_write64_fw(uncore, GEN6_BSD_RNCID, 0x0); 1681 1682 /* Wait for the ring not to be idle, i.e. for it to wake up. */ 1683 if (__intel_wait_for_register_fw(uncore, 1684 GEN6_BSD_SLEEP_PSMI_CONTROL, 1685 GEN6_BSD_SLEEP_INDICATOR, 1686 0, 1687 1000, 0, NULL)) 1688 DRM_ERROR("timed out waiting for the BSD ring to wake up\n"); 1689 1690 /* Now that the ring is fully powered up, update the tail */ 1691 i9xx_submit_request(request); 1692 1693 /* Let the ring send IDLE messages to the GT again, 1694 * and so let it sleep to conserve power when idle. 1695 */ 1696 intel_uncore_write_fw(uncore, GEN6_BSD_SLEEP_PSMI_CONTROL, 1697 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE)); 1698 1699 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL); 1700 } 1701 1702 static int mi_flush_dw(struct i915_request *rq, u32 flags) 1703 { 1704 u32 cmd, *cs; 1705 1706 cs = intel_ring_begin(rq, 4); 1707 if (IS_ERR(cs)) 1708 return PTR_ERR(cs); 1709 1710 cmd = MI_FLUSH_DW; 1711 1712 /* 1713 * We always require a command barrier so that subsequent 1714 * commands, such as breadcrumb interrupts, are strictly ordered 1715 * wrt the contents of the write cache being flushed to memory 1716 * (and thus being coherent from the CPU). 1717 */ 1718 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW; 1719 1720 /* 1721 * Bspec vol 1c.3 - blitter engine command streamer: 1722 * "If ENABLED, all TLBs will be invalidated once the flush 1723 * operation is complete. This bit is only valid when the 1724 * Post-Sync Operation field is a value of 1h or 3h." 1725 */ 1726 cmd |= flags; 1727 1728 *cs++ = cmd; 1729 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT; 1730 *cs++ = 0; 1731 *cs++ = MI_NOOP; 1732 1733 intel_ring_advance(rq, cs); 1734 1735 return 0; 1736 } 1737 1738 static int gen6_flush_dw(struct i915_request *rq, u32 mode, u32 invflags) 1739 { 1740 return mi_flush_dw(rq, mode & EMIT_INVALIDATE ? invflags : 0); 1741 } 1742 1743 static int gen6_bsd_ring_flush(struct i915_request *rq, u32 mode) 1744 { 1745 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB | MI_INVALIDATE_BSD); 1746 } 1747 1748 static int 1749 hsw_emit_bb_start(struct i915_request *rq, 1750 u64 offset, u32 len, 1751 unsigned int dispatch_flags) 1752 { 1753 u32 *cs; 1754 1755 cs = intel_ring_begin(rq, 2); 1756 if (IS_ERR(cs)) 1757 return PTR_ERR(cs); 1758 1759 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ? 1760 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW); 1761 /* bit0-7 is the length on GEN6+ */ 1762 *cs++ = offset; 1763 intel_ring_advance(rq, cs); 1764 1765 return 0; 1766 } 1767 1768 static int 1769 gen6_emit_bb_start(struct i915_request *rq, 1770 u64 offset, u32 len, 1771 unsigned int dispatch_flags) 1772 { 1773 u32 *cs; 1774 1775 cs = intel_ring_begin(rq, 2); 1776 if (IS_ERR(cs)) 1777 return PTR_ERR(cs); 1778 1779 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ? 1780 0 : MI_BATCH_NON_SECURE_I965); 1781 /* bit0-7 is the length on GEN6+ */ 1782 *cs++ = offset; 1783 intel_ring_advance(rq, cs); 1784 1785 return 0; 1786 } 1787 1788 /* Blitter support (SandyBridge+) */ 1789 1790 static int gen6_ring_flush(struct i915_request *rq, u32 mode) 1791 { 1792 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB); 1793 } 1794 1795 static void i9xx_set_default_submission(struct intel_engine_cs *engine) 1796 { 1797 engine->submit_request = i9xx_submit_request; 1798 engine->cancel_requests = cancel_requests; 1799 1800 engine->park = NULL; 1801 engine->unpark = NULL; 1802 } 1803 1804 static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine) 1805 { 1806 i9xx_set_default_submission(engine); 1807 engine->submit_request = gen6_bsd_submit_request; 1808 } 1809 1810 static void ring_destroy(struct intel_engine_cs *engine) 1811 { 1812 struct drm_i915_private *dev_priv = engine->i915; 1813 1814 WARN_ON(INTEL_GEN(dev_priv) > 2 && 1815 (ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0); 1816 1817 intel_engine_cleanup_common(engine); 1818 1819 intel_ring_unpin(engine->legacy.ring); 1820 intel_ring_put(engine->legacy.ring); 1821 1822 intel_timeline_unpin(engine->legacy.timeline); 1823 intel_timeline_put(engine->legacy.timeline); 1824 1825 kfree(engine); 1826 } 1827 1828 static void setup_irq(struct intel_engine_cs *engine) 1829 { 1830 struct drm_i915_private *i915 = engine->i915; 1831 1832 if (INTEL_GEN(i915) >= 6) { 1833 engine->irq_enable = gen6_irq_enable; 1834 engine->irq_disable = gen6_irq_disable; 1835 } else if (INTEL_GEN(i915) >= 5) { 1836 engine->irq_enable = gen5_irq_enable; 1837 engine->irq_disable = gen5_irq_disable; 1838 } else if (INTEL_GEN(i915) >= 3) { 1839 engine->irq_enable = i9xx_irq_enable; 1840 engine->irq_disable = i9xx_irq_disable; 1841 } else { 1842 engine->irq_enable = i8xx_irq_enable; 1843 engine->irq_disable = i8xx_irq_disable; 1844 } 1845 } 1846 1847 static void setup_common(struct intel_engine_cs *engine) 1848 { 1849 struct drm_i915_private *i915 = engine->i915; 1850 1851 /* gen8+ are only supported with execlists */ 1852 GEM_BUG_ON(INTEL_GEN(i915) >= 8); 1853 1854 setup_irq(engine); 1855 1856 engine->destroy = ring_destroy; 1857 1858 engine->resume = xcs_resume; 1859 engine->reset.prepare = reset_prepare; 1860 engine->reset.reset = reset_ring; 1861 engine->reset.finish = reset_finish; 1862 1863 engine->cops = &ring_context_ops; 1864 engine->request_alloc = ring_request_alloc; 1865 1866 /* 1867 * Using a global execution timeline; the previous final breadcrumb is 1868 * equivalent to our next initial bread so we can elide 1869 * engine->emit_init_breadcrumb(). 1870 */ 1871 engine->emit_fini_breadcrumb = i9xx_emit_breadcrumb; 1872 if (IS_GEN(i915, 5)) 1873 engine->emit_fini_breadcrumb = gen5_emit_breadcrumb; 1874 1875 engine->set_default_submission = i9xx_set_default_submission; 1876 1877 if (INTEL_GEN(i915) >= 6) 1878 engine->emit_bb_start = gen6_emit_bb_start; 1879 else if (INTEL_GEN(i915) >= 4) 1880 engine->emit_bb_start = i965_emit_bb_start; 1881 else if (IS_I830(i915) || IS_I845G(i915)) 1882 engine->emit_bb_start = i830_emit_bb_start; 1883 else 1884 engine->emit_bb_start = i915_emit_bb_start; 1885 } 1886 1887 static void setup_rcs(struct intel_engine_cs *engine) 1888 { 1889 struct drm_i915_private *i915 = engine->i915; 1890 1891 if (HAS_L3_DPF(i915)) 1892 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT; 1893 1894 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT; 1895 1896 if (INTEL_GEN(i915) >= 7) { 1897 engine->emit_flush = gen7_render_ring_flush; 1898 engine->emit_fini_breadcrumb = gen7_rcs_emit_breadcrumb; 1899 } else if (IS_GEN(i915, 6)) { 1900 engine->emit_flush = gen6_render_ring_flush; 1901 engine->emit_fini_breadcrumb = gen6_rcs_emit_breadcrumb; 1902 } else if (IS_GEN(i915, 5)) { 1903 engine->emit_flush = gen4_render_ring_flush; 1904 } else { 1905 if (INTEL_GEN(i915) < 4) 1906 engine->emit_flush = gen2_render_ring_flush; 1907 else 1908 engine->emit_flush = gen4_render_ring_flush; 1909 engine->irq_enable_mask = I915_USER_INTERRUPT; 1910 } 1911 1912 if (IS_HASWELL(i915)) 1913 engine->emit_bb_start = hsw_emit_bb_start; 1914 1915 engine->resume = rcs_resume; 1916 } 1917 1918 static void setup_vcs(struct intel_engine_cs *engine) 1919 { 1920 struct drm_i915_private *i915 = engine->i915; 1921 1922 if (INTEL_GEN(i915) >= 6) { 1923 /* gen6 bsd needs a special wa for tail updates */ 1924 if (IS_GEN(i915, 6)) 1925 engine->set_default_submission = gen6_bsd_set_default_submission; 1926 engine->emit_flush = gen6_bsd_ring_flush; 1927 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT; 1928 1929 if (IS_GEN(i915, 6)) 1930 engine->emit_fini_breadcrumb = gen6_xcs_emit_breadcrumb; 1931 else 1932 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb; 1933 } else { 1934 engine->emit_flush = bsd_ring_flush; 1935 if (IS_GEN(i915, 5)) 1936 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT; 1937 else 1938 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT; 1939 } 1940 } 1941 1942 static void setup_bcs(struct intel_engine_cs *engine) 1943 { 1944 struct drm_i915_private *i915 = engine->i915; 1945 1946 engine->emit_flush = gen6_ring_flush; 1947 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT; 1948 1949 if (IS_GEN(i915, 6)) 1950 engine->emit_fini_breadcrumb = gen6_xcs_emit_breadcrumb; 1951 else 1952 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb; 1953 } 1954 1955 static void setup_vecs(struct intel_engine_cs *engine) 1956 { 1957 struct drm_i915_private *i915 = engine->i915; 1958 1959 GEM_BUG_ON(INTEL_GEN(i915) < 7); 1960 1961 engine->emit_flush = gen6_ring_flush; 1962 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT; 1963 engine->irq_enable = hsw_vebox_irq_enable; 1964 engine->irq_disable = hsw_vebox_irq_disable; 1965 1966 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb; 1967 } 1968 1969 int intel_ring_submission_setup(struct intel_engine_cs *engine) 1970 { 1971 setup_common(engine); 1972 1973 switch (engine->class) { 1974 case RENDER_CLASS: 1975 setup_rcs(engine); 1976 break; 1977 case VIDEO_DECODE_CLASS: 1978 setup_vcs(engine); 1979 break; 1980 case COPY_ENGINE_CLASS: 1981 setup_bcs(engine); 1982 break; 1983 case VIDEO_ENHANCEMENT_CLASS: 1984 setup_vecs(engine); 1985 break; 1986 default: 1987 MISSING_CASE(engine->class); 1988 return -ENODEV; 1989 } 1990 1991 return 0; 1992 } 1993 1994 int intel_ring_submission_init(struct intel_engine_cs *engine) 1995 { 1996 struct intel_timeline *timeline; 1997 struct intel_ring *ring; 1998 int err; 1999 2000 timeline = intel_timeline_create(engine->gt, engine->status_page.vma); 2001 if (IS_ERR(timeline)) { 2002 err = PTR_ERR(timeline); 2003 goto err; 2004 } 2005 GEM_BUG_ON(timeline->has_initial_breadcrumb); 2006 2007 err = intel_timeline_pin(timeline); 2008 if (err) 2009 goto err_timeline; 2010 2011 ring = intel_engine_create_ring(engine, SZ_16K); 2012 if (IS_ERR(ring)) { 2013 err = PTR_ERR(ring); 2014 goto err_timeline_unpin; 2015 } 2016 2017 err = intel_ring_pin(ring); 2018 if (err) 2019 goto err_ring; 2020 2021 GEM_BUG_ON(engine->legacy.ring); 2022 engine->legacy.ring = ring; 2023 engine->legacy.timeline = timeline; 2024 2025 err = intel_engine_init_common(engine); 2026 if (err) 2027 goto err_ring_unpin; 2028 2029 GEM_BUG_ON(timeline->hwsp_ggtt != engine->status_page.vma); 2030 2031 return 0; 2032 2033 err_ring_unpin: 2034 intel_ring_unpin(ring); 2035 err_ring: 2036 intel_ring_put(ring); 2037 err_timeline_unpin: 2038 intel_timeline_unpin(timeline); 2039 err_timeline: 2040 intel_timeline_put(timeline); 2041 err: 2042 intel_engine_cleanup_common(engine); 2043 return err; 2044 } 2045