1 // SPDX-License-Identifier: MIT
3 * Copyright © 2020 Intel Corporation
6 #include "gen2_engine_cs.h"
9 #include "intel_engine.h"
10 #include "intel_engine_regs.h"
11 #include "intel_gpu_commands.h"
13 #include "intel_gt_irq.h"
14 #include "intel_ring.h"
16 int gen2_emit_flush(struct i915_request *rq, u32 mode)
18 unsigned int num_store_dw = 12;
22 if (mode & EMIT_INVALIDATE)
25 cs = intel_ring_begin(rq, 2 + 4 * num_store_dw);
30 while (num_store_dw--) {
31 *cs++ = MI_STORE_DWORD_INDEX;
32 *cs++ = I915_GEM_HWS_SCRATCH * sizeof(u32);
34 *cs++ = MI_FLUSH | MI_NO_WRITE_FLUSH;
38 intel_ring_advance(rq, cs);
43 int gen4_emit_flush_rcs(struct i915_request *rq, u32 mode)
51 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
52 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
53 * also flushed at 2d versus 3d pipeline switches.
57 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
58 * MI_READ_FLUSH is set, and is always flushed on 965.
60 * I915_GEM_DOMAIN_COMMAND may not exist?
62 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
63 * invalidated when MI_EXE_FLUSH is set.
65 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
66 * invalidated with every MI_FLUSH.
70 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
71 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
72 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
73 * are flushed at any MI_FLUSH.
77 if (mode & EMIT_INVALIDATE) {
79 if (IS_G4X(rq->engine->i915) || GRAPHICS_VER(rq->engine->i915) == 5)
80 cmd |= MI_INVALIDATE_ISP;
84 if (mode & EMIT_INVALIDATE)
87 cs = intel_ring_begin(rq, i);
94 * A random delay to let the CS invalidate take effect? Without this
95 * delay, the GPU relocation path fails as the CS does not see
96 * the updated contents. Just as important, if we apply the flushes
97 * to the EMIT_FLUSH branch (i.e. immediately after the relocation
98 * write and before the invalidate on the next batch), the relocations
99 * still fail. This implies that is a delay following invalidation
100 * that is required to reset the caches as opposed to a delay to
101 * ensure the memory is written.
103 if (mode & EMIT_INVALIDATE) {
104 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE;
105 *cs++ = intel_gt_scratch_offset(rq->engine->gt,
106 INTEL_GT_SCRATCH_FIELD_DEFAULT) |
107 PIPE_CONTROL_GLOBAL_GTT;
111 for (i = 0; i < 12; i++)
114 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE;
115 *cs++ = intel_gt_scratch_offset(rq->engine->gt,
116 INTEL_GT_SCRATCH_FIELD_DEFAULT) |
117 PIPE_CONTROL_GLOBAL_GTT;
124 intel_ring_advance(rq, cs);
129 int gen4_emit_flush_vcs(struct i915_request *rq, u32 mode)
133 cs = intel_ring_begin(rq, 2);
139 intel_ring_advance(rq, cs);
144 static u32 *__gen2_emit_breadcrumb(struct i915_request *rq, u32 *cs,
147 GEM_BUG_ON(i915_request_active_timeline(rq)->hwsp_ggtt != rq->engine->status_page.vma);
148 GEM_BUG_ON(offset_in_page(rq->hwsp_seqno) != I915_GEM_HWS_SEQNO_ADDR);
153 *cs++ = MI_STORE_DWORD_INDEX;
154 *cs++ = I915_GEM_HWS_SCRATCH * sizeof(u32);
155 *cs++ = rq->fence.seqno;
159 *cs++ = MI_STORE_DWORD_INDEX;
160 *cs++ = I915_GEM_HWS_SEQNO_ADDR;
161 *cs++ = rq->fence.seqno;
164 *cs++ = MI_USER_INTERRUPT;
166 rq->tail = intel_ring_offset(rq, cs);
167 assert_ring_tail_valid(rq->ring, rq->tail);
172 u32 *gen3_emit_breadcrumb(struct i915_request *rq, u32 *cs)
174 return __gen2_emit_breadcrumb(rq, cs, 16, 8);
177 u32 *gen5_emit_breadcrumb(struct i915_request *rq, u32 *cs)
179 return __gen2_emit_breadcrumb(rq, cs, 8, 8);
182 /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
183 #define I830_BATCH_LIMIT SZ_256K
184 #define I830_TLB_ENTRIES (2)
185 #define I830_WA_SIZE max(I830_TLB_ENTRIES * SZ_4K, I830_BATCH_LIMIT)
186 int i830_emit_bb_start(struct i915_request *rq,
188 unsigned int dispatch_flags)
191 intel_gt_scratch_offset(rq->engine->gt,
192 INTEL_GT_SCRATCH_FIELD_DEFAULT);
194 GEM_BUG_ON(rq->engine->gt->scratch->size < I830_WA_SIZE);
196 cs = intel_ring_begin(rq, 6);
200 /* Evict the invalid PTE TLBs */
201 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
202 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
203 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
207 intel_ring_advance(rq, cs);
209 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
210 if (len > I830_BATCH_LIMIT)
213 cs = intel_ring_begin(rq, 6 + 2);
218 * Blit the batch (which has now all relocs applied) to the
219 * stable batch scratch bo area (so that the CS never
220 * stumbles over its tlb invalidation bug) ...
222 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA | (6 - 2);
223 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
224 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
231 intel_ring_advance(rq, cs);
233 /* ... and execute it. */
237 if (!(dispatch_flags & I915_DISPATCH_SECURE))
238 offset |= MI_BATCH_NON_SECURE;
240 cs = intel_ring_begin(rq, 2);
244 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
246 intel_ring_advance(rq, cs);
251 int gen3_emit_bb_start(struct i915_request *rq,
253 unsigned int dispatch_flags)
257 if (!(dispatch_flags & I915_DISPATCH_SECURE))
258 offset |= MI_BATCH_NON_SECURE;
260 cs = intel_ring_begin(rq, 2);
264 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
266 intel_ring_advance(rq, cs);
271 int gen4_emit_bb_start(struct i915_request *rq,
272 u64 offset, u32 length,
273 unsigned int dispatch_flags)
278 security = MI_BATCH_NON_SECURE_I965;
279 if (dispatch_flags & I915_DISPATCH_SECURE)
282 cs = intel_ring_begin(rq, 2);
286 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | security;
288 intel_ring_advance(rq, cs);
293 void gen2_irq_enable(struct intel_engine_cs *engine)
295 struct drm_i915_private *i915 = engine->i915;
297 i915->irq_mask &= ~engine->irq_enable_mask;
298 intel_uncore_write16(&i915->uncore, GEN2_IMR, i915->irq_mask);
299 ENGINE_POSTING_READ16(engine, RING_IMR);
302 void gen2_irq_disable(struct intel_engine_cs *engine)
304 struct drm_i915_private *i915 = engine->i915;
306 i915->irq_mask |= engine->irq_enable_mask;
307 intel_uncore_write16(&i915->uncore, GEN2_IMR, i915->irq_mask);
310 void gen3_irq_enable(struct intel_engine_cs *engine)
312 engine->i915->irq_mask &= ~engine->irq_enable_mask;
313 intel_uncore_write(engine->uncore, GEN2_IMR, engine->i915->irq_mask);
314 intel_uncore_posting_read_fw(engine->uncore, GEN2_IMR);
317 void gen3_irq_disable(struct intel_engine_cs *engine)
319 engine->i915->irq_mask |= engine->irq_enable_mask;
320 intel_uncore_write(engine->uncore, GEN2_IMR, engine->i915->irq_mask);
323 void gen5_irq_enable(struct intel_engine_cs *engine)
325 gen5_gt_enable_irq(engine->gt, engine->irq_enable_mask);
328 void gen5_irq_disable(struct intel_engine_cs *engine)
330 gen5_gt_disable_irq(engine->gt, engine->irq_enable_mask);