2 * Copyright © 2008-2010 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
30 #include <linux/log2.h>
32 #include <drm/i915_drm.h>
34 #include "gem/i915_gem_context.h"
37 #include "i915_gem_render_state.h"
38 #include "i915_trace.h"
39 #include "intel_context.h"
40 #include "intel_reset.h"
41 #include "intel_workarounds.h"
43 /* Rough estimate of the typical request size, performing a flush,
44 * set-context and then emitting the batch.
46 #define LEGACY_REQUEST_SIZE 200
48 unsigned int intel_ring_update_space(struct intel_ring *ring)
52 space = __intel_ring_space(ring->head, ring->emit, ring->size);
59 gen2_render_ring_flush(struct i915_request *rq, u32 mode)
61 unsigned int num_store_dw;
66 if (mode & EMIT_INVALIDATE)
68 if (mode & EMIT_FLUSH)
71 cs = intel_ring_begin(rq, 2 + 3 * num_store_dw);
76 while (num_store_dw--) {
77 *cs++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
78 *cs++ = i915_scratch_offset(rq->i915);
81 *cs++ = MI_FLUSH | MI_NO_WRITE_FLUSH;
83 intel_ring_advance(rq, cs);
89 gen4_render_ring_flush(struct i915_request *rq, u32 mode)
97 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
98 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
99 * also flushed at 2d versus 3d pipeline switches.
103 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
104 * MI_READ_FLUSH is set, and is always flushed on 965.
106 * I915_GEM_DOMAIN_COMMAND may not exist?
108 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
109 * invalidated when MI_EXE_FLUSH is set.
111 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
112 * invalidated with every MI_FLUSH.
116 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
117 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
118 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
119 * are flushed at any MI_FLUSH.
123 if (mode & EMIT_INVALIDATE) {
125 if (IS_G4X(rq->i915) || IS_GEN(rq->i915, 5))
126 cmd |= MI_INVALIDATE_ISP;
130 if (mode & EMIT_INVALIDATE)
133 cs = intel_ring_begin(rq, i);
140 * A random delay to let the CS invalidate take effect? Without this
141 * delay, the GPU relocation path fails as the CS does not see
142 * the updated contents. Just as important, if we apply the flushes
143 * to the EMIT_FLUSH branch (i.e. immediately after the relocation
144 * write and before the invalidate on the next batch), the relocations
145 * still fail. This implies that is a delay following invalidation
146 * that is required to reset the caches as opposed to a delay to
147 * ensure the memory is written.
149 if (mode & EMIT_INVALIDATE) {
150 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE;
151 *cs++ = i915_scratch_offset(rq->i915) | PIPE_CONTROL_GLOBAL_GTT;
155 for (i = 0; i < 12; i++)
158 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE;
159 *cs++ = i915_scratch_offset(rq->i915) | PIPE_CONTROL_GLOBAL_GTT;
166 intel_ring_advance(rq, cs);
172 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
173 * implementing two workarounds on gen6. From section 1.4.7.1
174 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
176 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
177 * produced by non-pipelined state commands), software needs to first
178 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
181 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
182 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
184 * And the workaround for these two requires this workaround first:
186 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
187 * BEFORE the pipe-control with a post-sync op and no write-cache
190 * And this last workaround is tricky because of the requirements on
191 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
194 * "1 of the following must also be set:
195 * - Render Target Cache Flush Enable ([12] of DW1)
196 * - Depth Cache Flush Enable ([0] of DW1)
197 * - Stall at Pixel Scoreboard ([1] of DW1)
198 * - Depth Stall ([13] of DW1)
199 * - Post-Sync Operation ([13] of DW1)
200 * - Notify Enable ([8] of DW1)"
202 * The cache flushes require the workaround flush that triggered this
203 * one, so we can't use it. Depth stall would trigger the same.
204 * Post-sync nonzero is what triggered this second workaround, so we
205 * can't use that one either. Notify enable is IRQs, which aren't
206 * really our business. That leaves only stall at scoreboard.
209 gen6_emit_post_sync_nonzero_flush(struct i915_request *rq)
211 u32 scratch_addr = i915_scratch_offset(rq->i915) + 2 * CACHELINE_BYTES;
214 cs = intel_ring_begin(rq, 6);
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 */
224 intel_ring_advance(rq, cs);
226 cs = intel_ring_begin(rq, 6);
230 *cs++ = GFX_OP_PIPE_CONTROL(5);
231 *cs++ = PIPE_CONTROL_QW_WRITE;
232 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
236 intel_ring_advance(rq, cs);
242 gen6_render_ring_flush(struct i915_request *rq, u32 mode)
244 u32 scratch_addr = i915_scratch_offset(rq->i915) + 2 * CACHELINE_BYTES;
248 /* Force SNB workarounds for PIPE_CONTROL flushes */
249 ret = gen6_emit_post_sync_nonzero_flush(rq);
253 /* Just flush everything. Experiments have shown that reducing the
254 * number of bits based on the write domains has little performance
257 if (mode & EMIT_FLUSH) {
258 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
259 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
261 * Ensure that any following seqno writes only happen
262 * when the render cache is indeed flushed.
264 flags |= PIPE_CONTROL_CS_STALL;
266 if (mode & EMIT_INVALIDATE) {
267 flags |= PIPE_CONTROL_TLB_INVALIDATE;
268 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
269 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
270 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
271 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
272 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
274 * TLB invalidate requires a post-sync write.
276 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
279 cs = intel_ring_begin(rq, 4);
283 *cs++ = GFX_OP_PIPE_CONTROL(4);
285 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
287 intel_ring_advance(rq, cs);
292 static u32 *gen6_rcs_emit_breadcrumb(struct i915_request *rq, u32 *cs)
294 /* First we do the gen6_emit_post_sync_nonzero_flush w/a */
295 *cs++ = GFX_OP_PIPE_CONTROL(4);
296 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
300 *cs++ = GFX_OP_PIPE_CONTROL(4);
301 *cs++ = PIPE_CONTROL_QW_WRITE;
302 *cs++ = i915_scratch_offset(rq->i915) | PIPE_CONTROL_GLOBAL_GTT;
305 /* Finally we can flush and with it emit the breadcrumb */
306 *cs++ = GFX_OP_PIPE_CONTROL(4);
307 *cs++ = (PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH |
308 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
309 PIPE_CONTROL_DC_FLUSH_ENABLE |
310 PIPE_CONTROL_QW_WRITE |
311 PIPE_CONTROL_CS_STALL);
312 *cs++ = rq->timeline->hwsp_offset | PIPE_CONTROL_GLOBAL_GTT;
313 *cs++ = rq->fence.seqno;
315 *cs++ = MI_USER_INTERRUPT;
318 rq->tail = intel_ring_offset(rq, cs);
319 assert_ring_tail_valid(rq->ring, rq->tail);
325 gen7_render_ring_cs_stall_wa(struct i915_request *rq)
329 cs = intel_ring_begin(rq, 4);
333 *cs++ = GFX_OP_PIPE_CONTROL(4);
334 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
337 intel_ring_advance(rq, cs);
343 gen7_render_ring_flush(struct i915_request *rq, u32 mode)
345 u32 scratch_addr = i915_scratch_offset(rq->i915) + 2 * CACHELINE_BYTES;
349 * Ensure that any following seqno writes only happen when the render
350 * cache is indeed flushed.
352 * Workaround: 4th PIPE_CONTROL command (except the ones with only
353 * read-cache invalidate bits set) must have the CS_STALL bit set. We
354 * don't try to be clever and just set it unconditionally.
356 flags |= PIPE_CONTROL_CS_STALL;
358 /* Just flush everything. Experiments have shown that reducing the
359 * number of bits based on the write domains has little performance
362 if (mode & EMIT_FLUSH) {
363 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
364 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
365 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
366 flags |= PIPE_CONTROL_FLUSH_ENABLE;
368 if (mode & EMIT_INVALIDATE) {
369 flags |= PIPE_CONTROL_TLB_INVALIDATE;
370 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
371 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
372 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
373 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
374 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
375 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
377 * TLB invalidate requires a post-sync write.
379 flags |= PIPE_CONTROL_QW_WRITE;
380 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
382 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
384 /* Workaround: we must issue a pipe_control with CS-stall bit
385 * set before a pipe_control command that has the state cache
386 * invalidate bit set. */
387 gen7_render_ring_cs_stall_wa(rq);
390 cs = intel_ring_begin(rq, 4);
394 *cs++ = GFX_OP_PIPE_CONTROL(4);
396 *cs++ = scratch_addr;
398 intel_ring_advance(rq, cs);
403 static u32 *gen7_rcs_emit_breadcrumb(struct i915_request *rq, u32 *cs)
405 *cs++ = GFX_OP_PIPE_CONTROL(4);
406 *cs++ = (PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH |
407 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
408 PIPE_CONTROL_DC_FLUSH_ENABLE |
409 PIPE_CONTROL_FLUSH_ENABLE |
410 PIPE_CONTROL_QW_WRITE |
411 PIPE_CONTROL_GLOBAL_GTT_IVB |
412 PIPE_CONTROL_CS_STALL);
413 *cs++ = rq->timeline->hwsp_offset;
414 *cs++ = rq->fence.seqno;
416 *cs++ = MI_USER_INTERRUPT;
419 rq->tail = intel_ring_offset(rq, cs);
420 assert_ring_tail_valid(rq->ring, rq->tail);
425 static u32 *gen6_xcs_emit_breadcrumb(struct i915_request *rq, u32 *cs)
427 GEM_BUG_ON(rq->timeline->hwsp_ggtt != rq->engine->status_page.vma);
428 GEM_BUG_ON(offset_in_page(rq->timeline->hwsp_offset) != I915_GEM_HWS_SEQNO_ADDR);
430 *cs++ = MI_FLUSH_DW | MI_FLUSH_DW_OP_STOREDW | MI_FLUSH_DW_STORE_INDEX;
431 *cs++ = I915_GEM_HWS_SEQNO_ADDR | MI_FLUSH_DW_USE_GTT;
432 *cs++ = rq->fence.seqno;
434 *cs++ = MI_USER_INTERRUPT;
436 rq->tail = intel_ring_offset(rq, cs);
437 assert_ring_tail_valid(rq->ring, rq->tail);
442 #define GEN7_XCS_WA 32
443 static u32 *gen7_xcs_emit_breadcrumb(struct i915_request *rq, u32 *cs)
447 GEM_BUG_ON(rq->timeline->hwsp_ggtt != rq->engine->status_page.vma);
448 GEM_BUG_ON(offset_in_page(rq->timeline->hwsp_offset) != I915_GEM_HWS_SEQNO_ADDR);
450 *cs++ = MI_FLUSH_DW | MI_FLUSH_DW_OP_STOREDW | MI_FLUSH_DW_STORE_INDEX;
451 *cs++ = I915_GEM_HWS_SEQNO_ADDR | MI_FLUSH_DW_USE_GTT;
452 *cs++ = rq->fence.seqno;
454 for (i = 0; i < GEN7_XCS_WA; i++) {
455 *cs++ = MI_STORE_DWORD_INDEX;
456 *cs++ = I915_GEM_HWS_SEQNO_ADDR;
457 *cs++ = rq->fence.seqno;
464 *cs++ = MI_USER_INTERRUPT;
467 rq->tail = intel_ring_offset(rq, cs);
468 assert_ring_tail_valid(rq->ring, rq->tail);
474 static void set_hwstam(struct intel_engine_cs *engine, u32 mask)
477 * Keep the render interrupt unmasked as this papers over
478 * lost interrupts following a reset.
480 if (engine->class == RENDER_CLASS) {
481 if (INTEL_GEN(engine->i915) >= 6)
484 mask &= ~I915_USER_INTERRUPT;
487 intel_engine_set_hwsp_writemask(engine, mask);
490 static void set_hws_pga(struct intel_engine_cs *engine, phys_addr_t phys)
492 struct drm_i915_private *dev_priv = engine->i915;
495 addr = lower_32_bits(phys);
496 if (INTEL_GEN(dev_priv) >= 4)
497 addr |= (phys >> 28) & 0xf0;
499 I915_WRITE(HWS_PGA, addr);
502 static struct page *status_page(struct intel_engine_cs *engine)
504 struct drm_i915_gem_object *obj = engine->status_page.vma->obj;
506 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
507 return sg_page(obj->mm.pages->sgl);
510 static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
512 set_hws_pga(engine, PFN_PHYS(page_to_pfn(status_page(engine))));
513 set_hwstam(engine, ~0u);
516 static void set_hwsp(struct intel_engine_cs *engine, u32 offset)
518 struct drm_i915_private *dev_priv = engine->i915;
522 * The ring status page addresses are no longer next to the rest of
523 * the ring registers as of gen7.
525 if (IS_GEN(dev_priv, 7)) {
526 switch (engine->id) {
528 * No more rings exist on Gen7. Default case is only to shut up
529 * gcc switch check warning.
532 GEM_BUG_ON(engine->id);
535 hwsp = RENDER_HWS_PGA_GEN7;
538 hwsp = BLT_HWS_PGA_GEN7;
541 hwsp = BSD_HWS_PGA_GEN7;
544 hwsp = VEBOX_HWS_PGA_GEN7;
547 } else if (IS_GEN(dev_priv, 6)) {
548 hwsp = RING_HWS_PGA_GEN6(engine->mmio_base);
550 hwsp = RING_HWS_PGA(engine->mmio_base);
553 I915_WRITE(hwsp, offset);
557 static void flush_cs_tlb(struct intel_engine_cs *engine)
559 struct drm_i915_private *dev_priv = engine->i915;
561 if (!IS_GEN_RANGE(dev_priv, 6, 7))
564 /* ring should be idle before issuing a sync flush*/
565 WARN_ON((ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0);
567 ENGINE_WRITE(engine, RING_INSTPM,
568 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
570 if (intel_wait_for_register(engine->uncore,
571 RING_INSTPM(engine->mmio_base),
572 INSTPM_SYNC_FLUSH, 0,
574 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
578 static void ring_setup_status_page(struct intel_engine_cs *engine)
580 set_hwsp(engine, i915_ggtt_offset(engine->status_page.vma));
581 set_hwstam(engine, ~0u);
583 flush_cs_tlb(engine);
586 static bool stop_ring(struct intel_engine_cs *engine)
588 struct drm_i915_private *dev_priv = engine->i915;
590 if (INTEL_GEN(dev_priv) > 2) {
592 RING_MI_MODE, _MASKED_BIT_ENABLE(STOP_RING));
593 if (intel_wait_for_register(engine->uncore,
594 RING_MI_MODE(engine->mmio_base),
598 DRM_ERROR("%s : timed out trying to stop ring\n",
602 * Sometimes we observe that the idle flag is not
603 * set even though the ring is empty. So double
604 * check before giving up.
606 if (ENGINE_READ(engine, RING_HEAD) !=
607 ENGINE_READ(engine, RING_TAIL))
612 ENGINE_WRITE(engine, RING_HEAD, ENGINE_READ(engine, RING_TAIL));
614 ENGINE_WRITE(engine, RING_HEAD, 0);
615 ENGINE_WRITE(engine, RING_TAIL, 0);
617 /* The ring must be empty before it is disabled */
618 ENGINE_WRITE(engine, RING_CTL, 0);
620 return (ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR) == 0;
623 static int xcs_resume(struct intel_engine_cs *engine)
625 struct drm_i915_private *dev_priv = engine->i915;
626 struct intel_ring *ring = engine->buffer;
629 GEM_TRACE("%s: ring:{HEAD:%04x, TAIL:%04x}\n",
630 engine->name, ring->head, ring->tail);
632 intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
634 if (!stop_ring(engine)) {
635 /* G45 ring initialization often fails to reset head to zero */
636 DRM_DEBUG_DRIVER("%s head not reset to zero "
637 "ctl %08x head %08x tail %08x start %08x\n",
639 ENGINE_READ(engine, RING_CTL),
640 ENGINE_READ(engine, RING_HEAD),
641 ENGINE_READ(engine, RING_TAIL),
642 ENGINE_READ(engine, RING_START));
644 if (!stop_ring(engine)) {
645 DRM_ERROR("failed to set %s head to zero "
646 "ctl %08x head %08x tail %08x start %08x\n",
648 ENGINE_READ(engine, RING_CTL),
649 ENGINE_READ(engine, RING_HEAD),
650 ENGINE_READ(engine, RING_TAIL),
651 ENGINE_READ(engine, RING_START));
657 if (HWS_NEEDS_PHYSICAL(dev_priv))
658 ring_setup_phys_status_page(engine);
660 ring_setup_status_page(engine);
662 intel_engine_reset_breadcrumbs(engine);
664 /* Enforce ordering by reading HEAD register back */
665 ENGINE_READ(engine, RING_HEAD);
667 /* Initialize the ring. This must happen _after_ we've cleared the ring
668 * registers with the above sequence (the readback of the HEAD registers
669 * also enforces ordering), otherwise the hw might lose the new ring
670 * register values. */
671 ENGINE_WRITE(engine, RING_START, i915_ggtt_offset(ring->vma));
673 /* WaClearRingBufHeadRegAtInit:ctg,elk */
674 if (ENGINE_READ(engine, RING_HEAD))
675 DRM_DEBUG_DRIVER("%s initialization failed [head=%08x], fudging\n",
676 engine->name, ENGINE_READ(engine, RING_HEAD));
678 /* Check that the ring offsets point within the ring! */
679 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->head));
680 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->tail));
681 intel_ring_update_space(ring);
683 /* First wake the ring up to an empty/idle ring */
684 ENGINE_WRITE(engine, RING_HEAD, ring->head);
685 ENGINE_WRITE(engine, RING_TAIL, ring->head);
686 ENGINE_POSTING_READ(engine, RING_TAIL);
688 ENGINE_WRITE(engine, RING_CTL, RING_CTL_SIZE(ring->size) | RING_VALID);
690 /* If the head is still not zero, the ring is dead */
691 if (intel_wait_for_register(engine->uncore,
692 RING_CTL(engine->mmio_base),
693 RING_VALID, RING_VALID,
695 DRM_ERROR("%s initialization failed "
696 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
698 ENGINE_READ(engine, RING_CTL),
699 ENGINE_READ(engine, RING_CTL) & RING_VALID,
700 ENGINE_READ(engine, RING_HEAD), ring->head,
701 ENGINE_READ(engine, RING_TAIL), ring->tail,
702 ENGINE_READ(engine, RING_START),
703 i915_ggtt_offset(ring->vma));
708 if (INTEL_GEN(dev_priv) > 2)
710 RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
712 /* Now awake, let it get started */
713 if (ring->tail != ring->head) {
714 ENGINE_WRITE(engine, RING_TAIL, ring->tail);
715 ENGINE_POSTING_READ(engine, RING_TAIL);
718 /* Papering over lost _interrupts_ immediately following the restart */
719 intel_engine_queue_breadcrumbs(engine);
721 intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
726 static void reset_prepare(struct intel_engine_cs *engine)
728 intel_engine_stop_cs(engine);
731 static void reset_ring(struct intel_engine_cs *engine, bool stalled)
733 struct i915_timeline *tl = &engine->timeline;
734 struct i915_request *pos, *rq;
739 spin_lock_irqsave(&tl->lock, flags);
740 list_for_each_entry(pos, &tl->requests, link) {
741 if (!i915_request_completed(pos)) {
748 * The guilty request will get skipped on a hung engine.
750 * Users of client default contexts do not rely on logical
751 * state preserved between batches so it is safe to execute
752 * queued requests following the hang. Non default contexts
753 * rely on preserved state, so skipping a batch loses the
754 * evolution of the state and it needs to be considered corrupted.
755 * Executing more queued batches on top of corrupted state is
756 * risky. But we take the risk by trying to advance through
757 * the queued requests in order to make the client behaviour
758 * more predictable around resets, by not throwing away random
759 * amount of batches it has prepared for execution. Sophisticated
760 * clients can use gem_reset_stats_ioctl and dma fence status
761 * (exported via sync_file info ioctl on explicit fences) to observe
762 * when it loses the context state and should rebuild accordingly.
764 * The context ban, and ultimately the client ban, mechanism are safety
765 * valves if client submission ends up resulting in nothing more than
771 * Try to restore the logical GPU state to match the
772 * continuation of the request queue. If we skip the
773 * context/PD restore, then the next request may try to execute
774 * assuming that its context is valid and loaded on the GPU and
775 * so may try to access invalid memory, prompting repeated GPU
778 * If the request was guilty, we still restore the logical
779 * state in case the next request requires it (e.g. the
780 * aliasing ppgtt), but skip over the hung batch.
782 * If the request was innocent, we try to replay the request
783 * with the restored context.
785 i915_reset_request(rq, stalled);
787 GEM_BUG_ON(rq->ring != engine->buffer);
790 head = engine->buffer->tail;
792 engine->buffer->head = intel_ring_wrap(engine->buffer, head);
794 spin_unlock_irqrestore(&tl->lock, flags);
797 static void reset_finish(struct intel_engine_cs *engine)
801 static int intel_rcs_ctx_init(struct i915_request *rq)
805 ret = intel_engine_emit_ctx_wa(rq);
809 ret = i915_gem_render_state_emit(rq);
816 static int rcs_resume(struct intel_engine_cs *engine)
818 struct drm_i915_private *dev_priv = engine->i915;
821 * Disable CONSTANT_BUFFER before it is loaded from the context
822 * image. For as it is loaded, it is executed and the stored
823 * address may no longer be valid, leading to a GPU hang.
825 * This imposes the requirement that userspace reload their
826 * CONSTANT_BUFFER on every batch, fortunately a requirement
827 * they are already accustomed to from before contexts were
830 if (IS_GEN(dev_priv, 4))
832 _MASKED_BIT_ENABLE(ECO_CONSTANT_BUFFER_SR_DISABLE));
834 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
835 if (IS_GEN_RANGE(dev_priv, 4, 6))
836 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
838 /* We need to disable the AsyncFlip performance optimisations in order
839 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
840 * programmed to '1' on all products.
842 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
844 if (IS_GEN_RANGE(dev_priv, 6, 7))
845 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
847 /* Required for the hardware to program scanline values for waiting */
848 /* WaEnableFlushTlbInvalidationMode:snb */
849 if (IS_GEN(dev_priv, 6))
851 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
853 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
854 if (IS_GEN(dev_priv, 7))
855 I915_WRITE(GFX_MODE_GEN7,
856 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
857 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
859 if (IS_GEN(dev_priv, 6)) {
860 /* From the Sandybridge PRM, volume 1 part 3, page 24:
861 * "If this bit is set, STCunit will have LRA as replacement
862 * policy. [...] This bit must be reset. LRA replacement
863 * policy is not supported."
865 I915_WRITE(CACHE_MODE_0,
866 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
869 if (IS_GEN_RANGE(dev_priv, 6, 7))
870 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
872 return xcs_resume(engine);
875 static void cancel_requests(struct intel_engine_cs *engine)
877 struct i915_request *request;
880 spin_lock_irqsave(&engine->timeline.lock, flags);
882 /* Mark all submitted requests as skipped. */
883 list_for_each_entry(request, &engine->timeline.requests, link) {
884 if (!i915_request_signaled(request))
885 dma_fence_set_error(&request->fence, -EIO);
887 i915_request_mark_complete(request);
890 /* Remaining _unready_ requests will be nop'ed when submitted */
892 spin_unlock_irqrestore(&engine->timeline.lock, flags);
895 static void i9xx_submit_request(struct i915_request *request)
897 i915_request_submit(request);
899 ENGINE_WRITE(request->engine, RING_TAIL,
900 intel_ring_set_tail(request->ring, request->tail));
903 static u32 *i9xx_emit_breadcrumb(struct i915_request *rq, u32 *cs)
905 GEM_BUG_ON(rq->timeline->hwsp_ggtt != rq->engine->status_page.vma);
906 GEM_BUG_ON(offset_in_page(rq->timeline->hwsp_offset) != I915_GEM_HWS_SEQNO_ADDR);
910 *cs++ = MI_STORE_DWORD_INDEX;
911 *cs++ = I915_GEM_HWS_SEQNO_ADDR;
912 *cs++ = rq->fence.seqno;
914 *cs++ = MI_USER_INTERRUPT;
917 rq->tail = intel_ring_offset(rq, cs);
918 assert_ring_tail_valid(rq->ring, rq->tail);
923 #define GEN5_WA_STORES 8 /* must be at least 1! */
924 static u32 *gen5_emit_breadcrumb(struct i915_request *rq, u32 *cs)
928 GEM_BUG_ON(rq->timeline->hwsp_ggtt != rq->engine->status_page.vma);
929 GEM_BUG_ON(offset_in_page(rq->timeline->hwsp_offset) != I915_GEM_HWS_SEQNO_ADDR);
933 BUILD_BUG_ON(GEN5_WA_STORES < 1);
934 for (i = 0; i < GEN5_WA_STORES; i++) {
935 *cs++ = MI_STORE_DWORD_INDEX;
936 *cs++ = I915_GEM_HWS_SEQNO_ADDR;
937 *cs++ = rq->fence.seqno;
940 *cs++ = MI_USER_INTERRUPT;
942 rq->tail = intel_ring_offset(rq, cs);
943 assert_ring_tail_valid(rq->ring, rq->tail);
947 #undef GEN5_WA_STORES
950 gen5_irq_enable(struct intel_engine_cs *engine)
952 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
956 gen5_irq_disable(struct intel_engine_cs *engine)
958 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
962 i9xx_irq_enable(struct intel_engine_cs *engine)
964 engine->i915->irq_mask &= ~engine->irq_enable_mask;
965 intel_uncore_write(engine->uncore, GEN2_IMR, engine->i915->irq_mask);
966 intel_uncore_posting_read_fw(engine->uncore, GEN2_IMR);
970 i9xx_irq_disable(struct intel_engine_cs *engine)
972 engine->i915->irq_mask |= engine->irq_enable_mask;
973 intel_uncore_write(engine->uncore, GEN2_IMR, engine->i915->irq_mask);
977 i8xx_irq_enable(struct intel_engine_cs *engine)
979 struct drm_i915_private *dev_priv = engine->i915;
981 dev_priv->irq_mask &= ~engine->irq_enable_mask;
982 I915_WRITE16(GEN2_IMR, dev_priv->irq_mask);
983 POSTING_READ16(RING_IMR(engine->mmio_base));
987 i8xx_irq_disable(struct intel_engine_cs *engine)
989 struct drm_i915_private *dev_priv = engine->i915;
991 dev_priv->irq_mask |= engine->irq_enable_mask;
992 I915_WRITE16(GEN2_IMR, dev_priv->irq_mask);
996 bsd_ring_flush(struct i915_request *rq, u32 mode)
1000 cs = intel_ring_begin(rq, 2);
1006 intel_ring_advance(rq, cs);
1011 gen6_irq_enable(struct intel_engine_cs *engine)
1013 ENGINE_WRITE(engine, RING_IMR,
1014 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1016 /* Flush/delay to ensure the RING_IMR is active before the GT IMR */
1017 ENGINE_POSTING_READ(engine, RING_IMR);
1019 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
1023 gen6_irq_disable(struct intel_engine_cs *engine)
1025 ENGINE_WRITE(engine, RING_IMR, ~engine->irq_keep_mask);
1026 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
1030 hsw_vebox_irq_enable(struct intel_engine_cs *engine)
1032 ENGINE_WRITE(engine, RING_IMR, ~engine->irq_enable_mask);
1034 /* Flush/delay to ensure the RING_IMR is active before the GT IMR */
1035 ENGINE_POSTING_READ(engine, RING_IMR);
1037 gen6_unmask_pm_irq(engine->i915, engine->irq_enable_mask);
1041 hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1043 ENGINE_WRITE(engine, RING_IMR, ~0);
1044 gen6_mask_pm_irq(engine->i915, engine->irq_enable_mask);
1048 i965_emit_bb_start(struct i915_request *rq,
1049 u64 offset, u32 length,
1050 unsigned int dispatch_flags)
1054 cs = intel_ring_begin(rq, 2);
1058 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
1059 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
1061 intel_ring_advance(rq, cs);
1066 /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1067 #define I830_BATCH_LIMIT SZ_256K
1068 #define I830_TLB_ENTRIES (2)
1069 #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
1071 i830_emit_bb_start(struct i915_request *rq,
1072 u64 offset, u32 len,
1073 unsigned int dispatch_flags)
1075 u32 *cs, cs_offset = i915_scratch_offset(rq->i915);
1077 GEM_BUG_ON(rq->i915->gt.scratch->size < I830_WA_SIZE);
1079 cs = intel_ring_begin(rq, 6);
1083 /* Evict the invalid PTE TLBs */
1084 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
1085 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
1086 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
1090 intel_ring_advance(rq, cs);
1092 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
1093 if (len > I830_BATCH_LIMIT)
1096 cs = intel_ring_begin(rq, 6 + 2);
1100 /* Blit the batch (which has now all relocs applied) to the
1101 * stable batch scratch bo area (so that the CS never
1102 * stumbles over its tlb invalidation bug) ...
1104 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
1105 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
1106 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
1113 intel_ring_advance(rq, cs);
1115 /* ... and execute it. */
1119 cs = intel_ring_begin(rq, 2);
1123 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1124 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1125 MI_BATCH_NON_SECURE);
1126 intel_ring_advance(rq, cs);
1132 i915_emit_bb_start(struct i915_request *rq,
1133 u64 offset, u32 len,
1134 unsigned int dispatch_flags)
1138 cs = intel_ring_begin(rq, 2);
1142 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1143 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1144 MI_BATCH_NON_SECURE);
1145 intel_ring_advance(rq, cs);
1150 int intel_ring_pin(struct intel_ring *ring)
1152 struct i915_vma *vma = ring->vma;
1153 enum i915_map_type map = i915_coherent_map_type(vma->vm->i915);
1158 GEM_BUG_ON(ring->vaddr);
1160 ret = i915_timeline_pin(ring->timeline);
1166 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
1167 flags |= PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
1169 if (vma->obj->stolen)
1170 flags |= PIN_MAPPABLE;
1174 ret = i915_vma_pin(vma, 0, 0, flags);
1176 goto unpin_timeline;
1178 if (i915_vma_is_map_and_fenceable(vma))
1179 addr = (void __force *)i915_vma_pin_iomap(vma);
1181 addr = i915_gem_object_pin_map(vma->obj, map);
1183 ret = PTR_ERR(addr);
1187 vma->obj->pin_global++;
1193 i915_vma_unpin(vma);
1195 i915_timeline_unpin(ring->timeline);
1199 void intel_ring_reset(struct intel_ring *ring, u32 tail)
1201 GEM_BUG_ON(!intel_ring_offset_valid(ring, tail));
1206 intel_ring_update_space(ring);
1209 void intel_ring_unpin(struct intel_ring *ring)
1211 GEM_BUG_ON(!ring->vma);
1212 GEM_BUG_ON(!ring->vaddr);
1214 /* Discard any unused bytes beyond that submitted to hw. */
1215 intel_ring_reset(ring, ring->tail);
1217 if (i915_vma_is_map_and_fenceable(ring->vma))
1218 i915_vma_unpin_iomap(ring->vma);
1220 i915_gem_object_unpin_map(ring->vma->obj);
1223 ring->vma->obj->pin_global--;
1224 i915_vma_unpin(ring->vma);
1226 i915_timeline_unpin(ring->timeline);
1229 static struct i915_vma *
1230 intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
1232 struct i915_address_space *vm = &dev_priv->ggtt.vm;
1233 struct drm_i915_gem_object *obj;
1234 struct i915_vma *vma;
1236 obj = i915_gem_object_create_stolen(dev_priv, size);
1238 obj = i915_gem_object_create_internal(dev_priv, size);
1240 return ERR_CAST(obj);
1243 * Mark ring buffers as read-only from GPU side (so no stray overwrites)
1244 * if supported by the platform's GGTT.
1246 if (vm->has_read_only)
1247 i915_gem_object_set_readonly(obj);
1249 vma = i915_vma_instance(obj, vm, NULL);
1256 i915_gem_object_put(obj);
1261 intel_engine_create_ring(struct intel_engine_cs *engine,
1262 struct i915_timeline *timeline,
1265 struct intel_ring *ring;
1266 struct i915_vma *vma;
1268 GEM_BUG_ON(!is_power_of_2(size));
1269 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
1270 GEM_BUG_ON(timeline == &engine->timeline);
1271 lockdep_assert_held(&engine->i915->drm.struct_mutex);
1273 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1275 return ERR_PTR(-ENOMEM);
1277 kref_init(&ring->ref);
1278 INIT_LIST_HEAD(&ring->request_list);
1279 ring->timeline = i915_timeline_get(timeline);
1282 /* Workaround an erratum on the i830 which causes a hang if
1283 * the TAIL pointer points to within the last 2 cachelines
1286 ring->effective_size = size;
1287 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
1288 ring->effective_size -= 2 * CACHELINE_BYTES;
1290 intel_ring_update_space(ring);
1292 vma = intel_ring_create_vma(engine->i915, size);
1295 return ERR_CAST(vma);
1302 void intel_ring_free(struct kref *ref)
1304 struct intel_ring *ring = container_of(ref, typeof(*ring), ref);
1306 i915_vma_close(ring->vma);
1307 i915_vma_put(ring->vma);
1309 i915_timeline_put(ring->timeline);
1313 static void __ring_context_fini(struct intel_context *ce)
1315 GEM_BUG_ON(i915_gem_object_is_active(ce->state->obj));
1316 i915_gem_object_put(ce->state->obj);
1319 static void ring_context_destroy(struct kref *ref)
1321 struct intel_context *ce = container_of(ref, typeof(*ce), ref);
1323 GEM_BUG_ON(intel_context_is_pinned(ce));
1326 __ring_context_fini(ce);
1328 intel_context_free(ce);
1331 static int __context_pin_ppgtt(struct i915_gem_context *ctx)
1333 struct i915_address_space *vm;
1336 vm = ctx->vm ?: &ctx->i915->mm.aliasing_ppgtt->vm;
1338 err = gen6_ppgtt_pin(i915_vm_to_ppgtt((vm)));
1343 static void __context_unpin_ppgtt(struct i915_gem_context *ctx)
1345 struct i915_address_space *vm;
1347 vm = ctx->vm ?: &ctx->i915->mm.aliasing_ppgtt->vm;
1349 gen6_ppgtt_unpin(i915_vm_to_ppgtt(vm));
1352 static int __context_pin(struct intel_context *ce)
1354 struct i915_vma *vma;
1361 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
1366 * And mark is as a globally pinned object to let the shrinker know
1367 * it cannot reclaim the object until we release it.
1369 vma->obj->pin_global++;
1370 vma->obj->mm.dirty = true;
1375 static void __context_unpin(struct intel_context *ce)
1377 struct i915_vma *vma;
1383 vma->obj->pin_global--;
1384 i915_vma_unpin(vma);
1387 static void ring_context_unpin(struct intel_context *ce)
1389 __context_unpin_ppgtt(ce->gem_context);
1390 __context_unpin(ce);
1393 static struct i915_vma *
1394 alloc_context_vma(struct intel_engine_cs *engine)
1396 struct drm_i915_private *i915 = engine->i915;
1397 struct drm_i915_gem_object *obj;
1398 struct i915_vma *vma;
1401 obj = i915_gem_object_create_shmem(i915, engine->context_size);
1403 return ERR_CAST(obj);
1406 * Try to make the context utilize L3 as well as LLC.
1408 * On VLV we don't have L3 controls in the PTEs so we
1409 * shouldn't touch the cache level, especially as that
1410 * would make the object snooped which might have a
1411 * negative performance impact.
1413 * Snooping is required on non-llc platforms in execlist
1414 * mode, but since all GGTT accesses use PAT entry 0 we
1415 * get snooping anyway regardless of cache_level.
1417 * This is only applicable for Ivy Bridge devices since
1418 * later platforms don't have L3 control bits in the PTE.
1420 if (IS_IVYBRIDGE(i915))
1421 i915_gem_object_set_cache_coherency(obj, I915_CACHE_L3_LLC);
1423 if (engine->default_state) {
1424 void *defaults, *vaddr;
1426 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1427 if (IS_ERR(vaddr)) {
1428 err = PTR_ERR(vaddr);
1432 defaults = i915_gem_object_pin_map(engine->default_state,
1434 if (IS_ERR(defaults)) {
1435 err = PTR_ERR(defaults);
1439 memcpy(vaddr, defaults, engine->context_size);
1440 i915_gem_object_unpin_map(engine->default_state);
1442 i915_gem_object_flush_map(obj);
1443 i915_gem_object_unpin_map(obj);
1446 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
1455 i915_gem_object_unpin_map(obj);
1457 i915_gem_object_put(obj);
1458 return ERR_PTR(err);
1461 static int ring_context_pin(struct intel_context *ce)
1463 struct intel_engine_cs *engine = ce->engine;
1466 /* One ringbuffer to rule them all */
1467 GEM_BUG_ON(!engine->buffer);
1468 ce->ring = engine->buffer;
1470 if (!ce->state && engine->context_size) {
1471 struct i915_vma *vma;
1473 vma = alloc_context_vma(engine);
1475 return PTR_ERR(vma);
1480 err = __context_pin(ce);
1484 err = __context_pin_ppgtt(ce->gem_context);
1491 __context_unpin(ce);
1495 static void ring_context_reset(struct intel_context *ce)
1497 intel_ring_reset(ce->ring, 0);
1500 static const struct intel_context_ops ring_context_ops = {
1501 .pin = ring_context_pin,
1502 .unpin = ring_context_unpin,
1504 .enter = intel_context_enter_engine,
1505 .exit = intel_context_exit_engine,
1507 .reset = ring_context_reset,
1508 .destroy = ring_context_destroy,
1511 static int load_pd_dir(struct i915_request *rq,
1512 const struct i915_hw_ppgtt *ppgtt)
1514 const struct intel_engine_cs * const engine = rq->engine;
1517 cs = intel_ring_begin(rq, 6);
1521 *cs++ = MI_LOAD_REGISTER_IMM(1);
1522 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine->mmio_base));
1523 *cs++ = PP_DIR_DCLV_2G;
1525 *cs++ = MI_LOAD_REGISTER_IMM(1);
1526 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base));
1527 *cs++ = ppgtt->pd.base.ggtt_offset << 10;
1529 intel_ring_advance(rq, cs);
1534 static int flush_pd_dir(struct i915_request *rq)
1536 const struct intel_engine_cs * const engine = rq->engine;
1539 cs = intel_ring_begin(rq, 4);
1543 /* Stall until the page table load is complete */
1544 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
1545 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base));
1546 *cs++ = i915_scratch_offset(rq->i915);
1549 intel_ring_advance(rq, cs);
1553 static inline int mi_set_context(struct i915_request *rq, u32 flags)
1555 struct drm_i915_private *i915 = rq->i915;
1556 struct intel_engine_cs *engine = rq->engine;
1557 enum intel_engine_id id;
1558 const int num_engines =
1559 IS_HSW_GT1(i915) ? RUNTIME_INFO(i915)->num_engines - 1 : 0;
1560 bool force_restore = false;
1564 flags |= MI_MM_SPACE_GTT;
1565 if (IS_HASWELL(i915))
1566 /* These flags are for resource streamer on HSW+ */
1567 flags |= HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN;
1569 /* We need to save the extended state for powersaving modes */
1570 flags |= MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN;
1573 if (IS_GEN(i915, 7))
1574 len += 2 + (num_engines ? 4 * num_engines + 6 : 0);
1575 else if (IS_GEN(i915, 5))
1577 if (flags & MI_FORCE_RESTORE) {
1578 GEM_BUG_ON(flags & MI_RESTORE_INHIBIT);
1579 flags &= ~MI_FORCE_RESTORE;
1580 force_restore = true;
1584 cs = intel_ring_begin(rq, len);
1588 /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
1589 if (IS_GEN(i915, 7)) {
1590 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1592 struct intel_engine_cs *signaller;
1594 *cs++ = MI_LOAD_REGISTER_IMM(num_engines);
1595 for_each_engine(signaller, i915, id) {
1596 if (signaller == engine)
1599 *cs++ = i915_mmio_reg_offset(
1600 RING_PSMI_CTL(signaller->mmio_base));
1601 *cs++ = _MASKED_BIT_ENABLE(
1602 GEN6_PSMI_SLEEP_MSG_DISABLE);
1605 } else if (IS_GEN(i915, 5)) {
1607 * This w/a is only listed for pre-production ilk a/b steppings,
1608 * but is also mentioned for programming the powerctx. To be
1609 * safe, just apply the workaround; we do not use SyncFlush so
1610 * this should never take effect and so be a no-op!
1612 *cs++ = MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN;
1615 if (force_restore) {
1617 * The HW doesn't handle being told to restore the current
1618 * context very well. Quite often it likes goes to go off and
1619 * sulk, especially when it is meant to be reloading PP_DIR.
1620 * A very simple fix to force the reload is to simply switch
1621 * away from the current context and back again.
1623 * Note that the kernel_context will contain random state
1624 * following the INHIBIT_RESTORE. We accept this since we
1625 * never use the kernel_context state; it is merely a
1626 * placeholder we use to flush other contexts.
1628 *cs++ = MI_SET_CONTEXT;
1629 *cs++ = i915_ggtt_offset(engine->kernel_context->state) |
1635 *cs++ = MI_SET_CONTEXT;
1636 *cs++ = i915_ggtt_offset(rq->hw_context->state) | flags;
1638 * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
1639 * WaMiSetContext_Hang:snb,ivb,vlv
1643 if (IS_GEN(i915, 7)) {
1645 struct intel_engine_cs *signaller;
1646 i915_reg_t last_reg = {}; /* keep gcc quiet */
1648 *cs++ = MI_LOAD_REGISTER_IMM(num_engines);
1649 for_each_engine(signaller, i915, id) {
1650 if (signaller == engine)
1653 last_reg = RING_PSMI_CTL(signaller->mmio_base);
1654 *cs++ = i915_mmio_reg_offset(last_reg);
1655 *cs++ = _MASKED_BIT_DISABLE(
1656 GEN6_PSMI_SLEEP_MSG_DISABLE);
1659 /* Insert a delay before the next switch! */
1660 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
1661 *cs++ = i915_mmio_reg_offset(last_reg);
1662 *cs++ = i915_scratch_offset(rq->i915);
1665 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1666 } else if (IS_GEN(i915, 5)) {
1667 *cs++ = MI_SUSPEND_FLUSH;
1670 intel_ring_advance(rq, cs);
1675 static int remap_l3(struct i915_request *rq, int slice)
1677 u32 *cs, *remap_info = rq->i915->l3_parity.remap_info[slice];
1683 cs = intel_ring_begin(rq, GEN7_L3LOG_SIZE/4 * 2 + 2);
1688 * Note: We do not worry about the concurrent register cacheline hang
1689 * here because no other code should access these registers other than
1690 * at initialization time.
1692 *cs++ = MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4);
1693 for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
1694 *cs++ = i915_mmio_reg_offset(GEN7_L3LOG(slice, i));
1695 *cs++ = remap_info[i];
1698 intel_ring_advance(rq, cs);
1703 static int switch_context(struct i915_request *rq)
1705 struct intel_engine_cs *engine = rq->engine;
1706 struct i915_gem_context *ctx = rq->gem_context;
1707 struct i915_address_space *vm =
1708 ctx->vm ?: &rq->i915->mm.aliasing_ppgtt->vm;
1709 unsigned int unwind_mm = 0;
1713 GEM_BUG_ON(HAS_EXECLISTS(rq->i915));
1716 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1720 * Baytail takes a little more convincing that it really needs
1721 * to reload the PD between contexts. It is not just a little
1722 * longer, as adding more stalls after the load_pd_dir (i.e.
1723 * adding a long loop around flush_pd_dir) is not as effective
1724 * as reloading the PD umpteen times. 32 is derived from
1725 * experimentation (gem_exec_parallel/fds) and has no good
1729 if (engine->id == BCS0 && IS_VALLEYVIEW(engine->i915))
1733 ret = load_pd_dir(rq, ppgtt);
1738 if (ppgtt->pd_dirty_engines & engine->mask) {
1739 unwind_mm = engine->mask;
1740 ppgtt->pd_dirty_engines &= ~unwind_mm;
1741 hw_flags = MI_FORCE_RESTORE;
1745 if (rq->hw_context->state) {
1746 GEM_BUG_ON(engine->id != RCS0);
1749 * The kernel context(s) is treated as pure scratch and is not
1750 * expected to retain any state (as we sacrifice it during
1751 * suspend and on resume it may be corrupted). This is ok,
1752 * as nothing actually executes using the kernel context; it
1753 * is purely used for flushing user contexts.
1755 if (i915_gem_context_is_kernel(ctx))
1756 hw_flags = MI_RESTORE_INHIBIT;
1758 ret = mi_set_context(rq, hw_flags);
1764 ret = engine->emit_flush(rq, EMIT_INVALIDATE);
1768 ret = flush_pd_dir(rq);
1773 * Not only do we need a full barrier (post-sync write) after
1774 * invalidating the TLBs, but we need to wait a little bit
1775 * longer. Whether this is merely delaying us, or the
1776 * subsequent flush is a key part of serialising with the
1777 * post-sync op, this extra pass appears vital before a
1780 ret = engine->emit_flush(rq, EMIT_INVALIDATE);
1784 ret = engine->emit_flush(rq, EMIT_FLUSH);
1789 if (ctx->remap_slice) {
1790 for (i = 0; i < MAX_L3_SLICES; i++) {
1791 if (!(ctx->remap_slice & BIT(i)))
1794 ret = remap_l3(rq, i);
1799 ctx->remap_slice = 0;
1806 i915_vm_to_ppgtt(vm)->pd_dirty_engines |= unwind_mm;
1811 static int ring_request_alloc(struct i915_request *request)
1815 GEM_BUG_ON(!intel_context_is_pinned(request->hw_context));
1816 GEM_BUG_ON(request->timeline->has_initial_breadcrumb);
1819 * Flush enough space to reduce the likelihood of waiting after
1820 * we start building the request - in which case we will just
1821 * have to repeat work.
1823 request->reserved_space += LEGACY_REQUEST_SIZE;
1825 /* Unconditionally invalidate GPU caches and TLBs. */
1826 ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
1830 ret = switch_context(request);
1834 request->reserved_space -= LEGACY_REQUEST_SIZE;
1838 static noinline int wait_for_space(struct intel_ring *ring, unsigned int bytes)
1840 struct i915_request *target;
1843 if (intel_ring_update_space(ring) >= bytes)
1846 GEM_BUG_ON(list_empty(&ring->request_list));
1847 list_for_each_entry(target, &ring->request_list, ring_link) {
1848 /* Would completion of this request free enough space? */
1849 if (bytes <= __intel_ring_space(target->postfix,
1850 ring->emit, ring->size))
1854 if (WARN_ON(&target->ring_link == &ring->request_list))
1857 timeout = i915_request_wait(target,
1858 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1859 MAX_SCHEDULE_TIMEOUT);
1863 i915_request_retire_upto(target);
1865 intel_ring_update_space(ring);
1866 GEM_BUG_ON(ring->space < bytes);
1870 u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
1872 struct intel_ring *ring = rq->ring;
1873 const unsigned int remain_usable = ring->effective_size - ring->emit;
1874 const unsigned int bytes = num_dwords * sizeof(u32);
1875 unsigned int need_wrap = 0;
1876 unsigned int total_bytes;
1879 /* Packets must be qword aligned. */
1880 GEM_BUG_ON(num_dwords & 1);
1882 total_bytes = bytes + rq->reserved_space;
1883 GEM_BUG_ON(total_bytes > ring->effective_size);
1885 if (unlikely(total_bytes > remain_usable)) {
1886 const int remain_actual = ring->size - ring->emit;
1888 if (bytes > remain_usable) {
1890 * Not enough space for the basic request. So need to
1891 * flush out the remainder and then wait for
1894 total_bytes += remain_actual;
1895 need_wrap = remain_actual | 1;
1898 * The base request will fit but the reserved space
1899 * falls off the end. So we don't need an immediate
1900 * wrap and only need to effectively wait for the
1901 * reserved size from the start of ringbuffer.
1903 total_bytes = rq->reserved_space + remain_actual;
1907 if (unlikely(total_bytes > ring->space)) {
1911 * Space is reserved in the ringbuffer for finalising the
1912 * request, as that cannot be allowed to fail. During request
1913 * finalisation, reserved_space is set to 0 to stop the
1914 * overallocation and the assumption is that then we never need
1915 * to wait (which has the risk of failing with EINTR).
1917 * See also i915_request_alloc() and i915_request_add().
1919 GEM_BUG_ON(!rq->reserved_space);
1921 ret = wait_for_space(ring, total_bytes);
1923 return ERR_PTR(ret);
1926 if (unlikely(need_wrap)) {
1928 GEM_BUG_ON(need_wrap > ring->space);
1929 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
1930 GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
1932 /* Fill the tail with MI_NOOP */
1933 memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
1934 ring->space -= need_wrap;
1938 GEM_BUG_ON(ring->emit > ring->size - bytes);
1939 GEM_BUG_ON(ring->space < bytes);
1940 cs = ring->vaddr + ring->emit;
1941 GEM_DEBUG_EXEC(memset32(cs, POISON_INUSE, bytes / sizeof(*cs)));
1942 ring->emit += bytes;
1943 ring->space -= bytes;
1948 /* Align the ring tail to a cacheline boundary */
1949 int intel_ring_cacheline_align(struct i915_request *rq)
1954 num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
1955 if (num_dwords == 0)
1958 num_dwords = CACHELINE_DWORDS - num_dwords;
1959 GEM_BUG_ON(num_dwords & 1);
1961 cs = intel_ring_begin(rq, num_dwords);
1965 memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
1966 intel_ring_advance(rq, cs);
1968 GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));
1972 static void gen6_bsd_submit_request(struct i915_request *request)
1974 struct intel_uncore *uncore = request->engine->uncore;
1976 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
1978 /* Every tail move must follow the sequence below */
1980 /* Disable notification that the ring is IDLE. The GT
1981 * will then assume that it is busy and bring it out of rc6.
1983 intel_uncore_write_fw(uncore, GEN6_BSD_SLEEP_PSMI_CONTROL,
1984 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1986 /* Clear the context id. Here be magic! */
1987 intel_uncore_write64_fw(uncore, GEN6_BSD_RNCID, 0x0);
1989 /* Wait for the ring not to be idle, i.e. for it to wake up. */
1990 if (__intel_wait_for_register_fw(uncore,
1991 GEN6_BSD_SLEEP_PSMI_CONTROL,
1992 GEN6_BSD_SLEEP_INDICATOR,
1995 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
1997 /* Now that the ring is fully powered up, update the tail */
1998 i9xx_submit_request(request);
2000 /* Let the ring send IDLE messages to the GT again,
2001 * and so let it sleep to conserve power when idle.
2003 intel_uncore_write_fw(uncore, GEN6_BSD_SLEEP_PSMI_CONTROL,
2004 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2006 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
2009 static int mi_flush_dw(struct i915_request *rq, u32 flags)
2013 cs = intel_ring_begin(rq, 4);
2020 * We always require a command barrier so that subsequent
2021 * commands, such as breadcrumb interrupts, are strictly ordered
2022 * wrt the contents of the write cache being flushed to memory
2023 * (and thus being coherent from the CPU).
2025 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2028 * Bspec vol 1c.3 - blitter engine command streamer:
2029 * "If ENABLED, all TLBs will be invalidated once the flush
2030 * operation is complete. This bit is only valid when the
2031 * Post-Sync Operation field is a value of 1h or 3h."
2036 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
2040 intel_ring_advance(rq, cs);
2045 static int gen6_flush_dw(struct i915_request *rq, u32 mode, u32 invflags)
2047 return mi_flush_dw(rq, mode & EMIT_INVALIDATE ? invflags : 0);
2050 static int gen6_bsd_ring_flush(struct i915_request *rq, u32 mode)
2052 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB | MI_INVALIDATE_BSD);
2056 hsw_emit_bb_start(struct i915_request *rq,
2057 u64 offset, u32 len,
2058 unsigned int dispatch_flags)
2062 cs = intel_ring_begin(rq, 2);
2066 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
2067 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW);
2068 /* bit0-7 is the length on GEN6+ */
2070 intel_ring_advance(rq, cs);
2076 gen6_emit_bb_start(struct i915_request *rq,
2077 u64 offset, u32 len,
2078 unsigned int dispatch_flags)
2082 cs = intel_ring_begin(rq, 2);
2086 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
2087 0 : MI_BATCH_NON_SECURE_I965);
2088 /* bit0-7 is the length on GEN6+ */
2090 intel_ring_advance(rq, cs);
2095 /* Blitter support (SandyBridge+) */
2097 static int gen6_ring_flush(struct i915_request *rq, u32 mode)
2099 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB);
2102 static void i9xx_set_default_submission(struct intel_engine_cs *engine)
2104 engine->submit_request = i9xx_submit_request;
2105 engine->cancel_requests = cancel_requests;
2107 engine->park = NULL;
2108 engine->unpark = NULL;
2111 static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
2113 i9xx_set_default_submission(engine);
2114 engine->submit_request = gen6_bsd_submit_request;
2117 static void ring_destroy(struct intel_engine_cs *engine)
2119 struct drm_i915_private *dev_priv = engine->i915;
2121 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
2122 (ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0);
2124 intel_ring_unpin(engine->buffer);
2125 intel_ring_put(engine->buffer);
2127 intel_engine_cleanup_common(engine);
2131 static void setup_irq(struct intel_engine_cs *engine)
2133 struct drm_i915_private *i915 = engine->i915;
2135 if (INTEL_GEN(i915) >= 6) {
2136 engine->irq_enable = gen6_irq_enable;
2137 engine->irq_disable = gen6_irq_disable;
2138 } else if (INTEL_GEN(i915) >= 5) {
2139 engine->irq_enable = gen5_irq_enable;
2140 engine->irq_disable = gen5_irq_disable;
2141 } else if (INTEL_GEN(i915) >= 3) {
2142 engine->irq_enable = i9xx_irq_enable;
2143 engine->irq_disable = i9xx_irq_disable;
2145 engine->irq_enable = i8xx_irq_enable;
2146 engine->irq_disable = i8xx_irq_disable;
2150 static void setup_common(struct intel_engine_cs *engine)
2152 struct drm_i915_private *i915 = engine->i915;
2154 /* gen8+ are only supported with execlists */
2155 GEM_BUG_ON(INTEL_GEN(i915) >= 8);
2159 engine->destroy = ring_destroy;
2161 engine->resume = xcs_resume;
2162 engine->reset.prepare = reset_prepare;
2163 engine->reset.reset = reset_ring;
2164 engine->reset.finish = reset_finish;
2166 engine->cops = &ring_context_ops;
2167 engine->request_alloc = ring_request_alloc;
2170 * Using a global execution timeline; the previous final breadcrumb is
2171 * equivalent to our next initial bread so we can elide
2172 * engine->emit_init_breadcrumb().
2174 engine->emit_fini_breadcrumb = i9xx_emit_breadcrumb;
2175 if (IS_GEN(i915, 5))
2176 engine->emit_fini_breadcrumb = gen5_emit_breadcrumb;
2178 engine->set_default_submission = i9xx_set_default_submission;
2180 if (INTEL_GEN(i915) >= 6)
2181 engine->emit_bb_start = gen6_emit_bb_start;
2182 else if (INTEL_GEN(i915) >= 4)
2183 engine->emit_bb_start = i965_emit_bb_start;
2184 else if (IS_I830(i915) || IS_I845G(i915))
2185 engine->emit_bb_start = i830_emit_bb_start;
2187 engine->emit_bb_start = i915_emit_bb_start;
2190 static void setup_rcs(struct intel_engine_cs *engine)
2192 struct drm_i915_private *i915 = engine->i915;
2194 if (HAS_L3_DPF(i915))
2195 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2197 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2199 if (INTEL_GEN(i915) >= 7) {
2200 engine->init_context = intel_rcs_ctx_init;
2201 engine->emit_flush = gen7_render_ring_flush;
2202 engine->emit_fini_breadcrumb = gen7_rcs_emit_breadcrumb;
2203 } else if (IS_GEN(i915, 6)) {
2204 engine->init_context = intel_rcs_ctx_init;
2205 engine->emit_flush = gen6_render_ring_flush;
2206 engine->emit_fini_breadcrumb = gen6_rcs_emit_breadcrumb;
2207 } else if (IS_GEN(i915, 5)) {
2208 engine->emit_flush = gen4_render_ring_flush;
2210 if (INTEL_GEN(i915) < 4)
2211 engine->emit_flush = gen2_render_ring_flush;
2213 engine->emit_flush = gen4_render_ring_flush;
2214 engine->irq_enable_mask = I915_USER_INTERRUPT;
2217 if (IS_HASWELL(i915))
2218 engine->emit_bb_start = hsw_emit_bb_start;
2220 engine->resume = rcs_resume;
2223 static void setup_vcs(struct intel_engine_cs *engine)
2225 struct drm_i915_private *i915 = engine->i915;
2227 if (INTEL_GEN(i915) >= 6) {
2228 /* gen6 bsd needs a special wa for tail updates */
2229 if (IS_GEN(i915, 6))
2230 engine->set_default_submission = gen6_bsd_set_default_submission;
2231 engine->emit_flush = gen6_bsd_ring_flush;
2232 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2234 if (IS_GEN(i915, 6))
2235 engine->emit_fini_breadcrumb = gen6_xcs_emit_breadcrumb;
2237 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
2239 engine->emit_flush = bsd_ring_flush;
2240 if (IS_GEN(i915, 5))
2241 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2243 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2247 static void setup_bcs(struct intel_engine_cs *engine)
2249 struct drm_i915_private *i915 = engine->i915;
2251 engine->emit_flush = gen6_ring_flush;
2252 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2254 if (IS_GEN(i915, 6))
2255 engine->emit_fini_breadcrumb = gen6_xcs_emit_breadcrumb;
2257 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
2260 static void setup_vecs(struct intel_engine_cs *engine)
2262 struct drm_i915_private *i915 = engine->i915;
2264 GEM_BUG_ON(INTEL_GEN(i915) < 7);
2266 engine->emit_flush = gen6_ring_flush;
2267 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2268 engine->irq_enable = hsw_vebox_irq_enable;
2269 engine->irq_disable = hsw_vebox_irq_disable;
2271 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
2274 int intel_ring_submission_setup(struct intel_engine_cs *engine)
2276 setup_common(engine);
2278 switch (engine->class) {
2282 case VIDEO_DECODE_CLASS:
2285 case COPY_ENGINE_CLASS:
2288 case VIDEO_ENHANCEMENT_CLASS:
2292 MISSING_CASE(engine->class);
2299 int intel_ring_submission_init(struct intel_engine_cs *engine)
2301 struct i915_timeline *timeline;
2302 struct intel_ring *ring;
2305 timeline = i915_timeline_create(engine->i915, engine->status_page.vma);
2306 if (IS_ERR(timeline)) {
2307 err = PTR_ERR(timeline);
2310 GEM_BUG_ON(timeline->has_initial_breadcrumb);
2312 ring = intel_engine_create_ring(engine, timeline, 32 * PAGE_SIZE);
2313 i915_timeline_put(timeline);
2315 err = PTR_ERR(ring);
2319 err = intel_ring_pin(ring);
2323 GEM_BUG_ON(engine->buffer);
2324 engine->buffer = ring;
2326 err = intel_engine_init_common(engine);
2330 GEM_BUG_ON(ring->timeline->hwsp_ggtt != engine->status_page.vma);
2335 intel_ring_unpin(ring);
2337 intel_ring_put(ring);
2339 intel_engine_cleanup_common(engine);