]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/gpu/drm/i915/gt/intel_ringbuffer.c
drm/i915: Stop retiring along engine
[mirror_ubuntu-focal-kernel.git] / drivers / gpu / drm / i915 / gt / intel_ringbuffer.c
CommitLineData
62fdfeaf
EA
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
a4d8a0fe 30#include <linux/log2.h>
7c2fa7fa 31
760285e7 32#include <drm/i915_drm.h>
7c2fa7fa 33
10be98a7
CW
34#include "gem/i915_gem_context.h"
35
7c2fa7fa
CW
36#include "i915_drv.h"
37#include "i915_gem_render_state.h"
62fdfeaf 38#include "i915_trace.h"
10be98a7 39#include "intel_context.h"
112ed2d3 40#include "intel_reset.h"
7d3c425f 41#include "intel_workarounds.h"
62fdfeaf 42
a0442461
CW
43/* Rough estimate of the typical request size, performing a flush,
44 * set-context and then emitting the batch.
45 */
46#define LEGACY_REQUEST_SIZE 200
47
95aebcb2 48unsigned int intel_ring_update_space(struct intel_ring *ring)
ebd0fd4b 49{
95aebcb2
CW
50 unsigned int space;
51
52 space = __intel_ring_space(ring->head, ring->emit, ring->size);
53
54 ring->space = space;
55 return space;
ebd0fd4b
DG
56}
57
b72f3acb 58static int
e61e0f51 59gen2_render_ring_flush(struct i915_request *rq, u32 mode)
46f0f8d1 60{
a889580c 61 unsigned int num_store_dw;
73dec95e 62 u32 cmd, *cs;
46f0f8d1
CW
63
64 cmd = MI_FLUSH;
a889580c 65 num_store_dw = 0;
7c9cf4e3 66 if (mode & EMIT_INVALIDATE)
46f0f8d1 67 cmd |= MI_READ_FLUSH;
a889580c
CW
68 if (mode & EMIT_FLUSH)
69 num_store_dw = 4;
46f0f8d1 70
a889580c 71 cs = intel_ring_begin(rq, 2 + 3 * num_store_dw);
73dec95e
TU
72 if (IS_ERR(cs))
73 return PTR_ERR(cs);
46f0f8d1 74
73dec95e 75 *cs++ = cmd;
a889580c
CW
76 while (num_store_dw--) {
77 *cs++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
78 *cs++ = i915_scratch_offset(rq->i915);
79 *cs++ = 0;
80 }
81 *cs++ = MI_FLUSH | MI_NO_WRITE_FLUSH;
82
e61e0f51 83 intel_ring_advance(rq, cs);
46f0f8d1
CW
84
85 return 0;
86}
87
88static int
e61e0f51 89gen4_render_ring_flush(struct i915_request *rq, u32 mode)
62fdfeaf 90{
73dec95e 91 u32 cmd, *cs;
55f99bf2 92 int i;
6f392d54 93
36d527de
CW
94 /*
95 * read/write caches:
96 *
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.
100 *
101 * read-only caches:
102 *
103 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
104 * MI_READ_FLUSH is set, and is always flushed on 965.
105 *
106 * I915_GEM_DOMAIN_COMMAND may not exist?
107 *
108 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
109 * invalidated when MI_EXE_FLUSH is set.
110 *
111 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
112 * invalidated with every MI_FLUSH.
113 *
114 * TLBs:
115 *
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.
120 */
121
b5321f30 122 cmd = MI_FLUSH;
7c9cf4e3 123 if (mode & EMIT_INVALIDATE) {
36d527de 124 cmd |= MI_EXE_FLUSH;
cf819eff 125 if (IS_G4X(rq->i915) || IS_GEN(rq->i915, 5))
b5321f30
CW
126 cmd |= MI_INVALIDATE_ISP;
127 }
70eac33e 128
55f99bf2
CW
129 i = 2;
130 if (mode & EMIT_INVALIDATE)
131 i += 20;
132
133 cs = intel_ring_begin(rq, i);
73dec95e
TU
134 if (IS_ERR(cs))
135 return PTR_ERR(cs);
b72f3acb 136
73dec95e 137 *cs++ = cmd;
55f99bf2
CW
138
139 /*
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.
148 */
149 if (mode & EMIT_INVALIDATE) {
150 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE;
51797499 151 *cs++ = i915_scratch_offset(rq->i915) | PIPE_CONTROL_GLOBAL_GTT;
55f99bf2
CW
152 *cs++ = 0;
153 *cs++ = 0;
154
155 for (i = 0; i < 12; i++)
156 *cs++ = MI_FLUSH;
157
158 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE;
51797499 159 *cs++ = i915_scratch_offset(rq->i915) | PIPE_CONTROL_GLOBAL_GTT;
55f99bf2
CW
160 *cs++ = 0;
161 *cs++ = 0;
162 }
163
164 *cs++ = cmd;
165
e61e0f51 166 intel_ring_advance(rq, cs);
b72f3acb
CW
167
168 return 0;
8187a2b7
ZN
169}
170
179f4025 171/*
8d315287
JB
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:
175 *
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 !=
179 * 0.
180 *
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.
183 *
184 * And the workaround for these two requires this workaround first:
185 *
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
188 * flushes.
189 *
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
192 * volume 2 part 1:
193 *
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)"
201 *
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.
207 */
208static int
caa5915b 209gen6_emit_post_sync_nonzero_flush(struct i915_request *rq)
8d315287 210{
51797499 211 u32 scratch_addr = i915_scratch_offset(rq->i915) + 2 * CACHELINE_BYTES;
73dec95e
TU
212 u32 *cs;
213
e61e0f51 214 cs = intel_ring_begin(rq, 6);
73dec95e
TU
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;
e61e0f51 224 intel_ring_advance(rq, cs);
73dec95e 225
e61e0f51 226 cs = intel_ring_begin(rq, 6);
73dec95e
TU
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;
e61e0f51 236 intel_ring_advance(rq, cs);
8d315287
JB
237
238 return 0;
239}
240
241static int
e61e0f51 242gen6_render_ring_flush(struct i915_request *rq, u32 mode)
8d315287 243{
51797499 244 u32 scratch_addr = i915_scratch_offset(rq->i915) + 2 * CACHELINE_BYTES;
73dec95e 245 u32 *cs, flags = 0;
8d315287
JB
246 int ret;
247
b3111509 248 /* Force SNB workarounds for PIPE_CONTROL flushes */
caa5915b 249 ret = gen6_emit_post_sync_nonzero_flush(rq);
b3111509
PZ
250 if (ret)
251 return ret;
252
8d315287
JB
253 /* Just flush everything. Experiments have shown that reducing the
254 * number of bits based on the write domains has little performance
255 * impact.
256 */
7c9cf4e3 257 if (mode & EMIT_FLUSH) {
7d54a904
CW
258 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
259 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
260 /*
261 * Ensure that any following seqno writes only happen
262 * when the render cache is indeed flushed.
263 */
97f209bc 264 flags |= PIPE_CONTROL_CS_STALL;
7d54a904 265 }
7c9cf4e3 266 if (mode & EMIT_INVALIDATE) {
7d54a904
CW
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;
273 /*
274 * TLB invalidate requires a post-sync write.
275 */
3ac78313 276 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
7d54a904 277 }
8d315287 278
e61e0f51 279 cs = intel_ring_begin(rq, 4);
73dec95e
TU
280 if (IS_ERR(cs))
281 return PTR_ERR(cs);
8d315287 282
73dec95e
TU
283 *cs++ = GFX_OP_PIPE_CONTROL(4);
284 *cs++ = flags;
285 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
286 *cs++ = 0;
e61e0f51 287 intel_ring_advance(rq, cs);
8d315287
JB
288
289 return 0;
290}
291
e1a73a54 292static u32 *gen6_rcs_emit_breadcrumb(struct i915_request *rq, u32 *cs)
caa5915b
CW
293{
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;
297 *cs++ = 0;
298 *cs++ = 0;
299
300 *cs++ = GFX_OP_PIPE_CONTROL(4);
301 *cs++ = PIPE_CONTROL_QW_WRITE;
302 *cs++ = i915_scratch_offset(rq->i915) | PIPE_CONTROL_GLOBAL_GTT;
303 *cs++ = 0;
304
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);
5013eb8c
CW
312 *cs++ = rq->timeline->hwsp_offset | PIPE_CONTROL_GLOBAL_GTT;
313 *cs++ = rq->fence.seqno;
314
caa5915b
CW
315 *cs++ = MI_USER_INTERRUPT;
316 *cs++ = MI_NOOP;
317
318 rq->tail = intel_ring_offset(rq, cs);
319 assert_ring_tail_valid(rq->ring, rq->tail);
e1a73a54
CW
320
321 return cs;
caa5915b 322}
caa5915b 323
f3987631 324static int
e61e0f51 325gen7_render_ring_cs_stall_wa(struct i915_request *rq)
f3987631 326{
73dec95e 327 u32 *cs;
f3987631 328
e61e0f51 329 cs = intel_ring_begin(rq, 4);
73dec95e
TU
330 if (IS_ERR(cs))
331 return PTR_ERR(cs);
f3987631 332
73dec95e
TU
333 *cs++ = GFX_OP_PIPE_CONTROL(4);
334 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
335 *cs++ = 0;
336 *cs++ = 0;
e61e0f51 337 intel_ring_advance(rq, cs);
f3987631
PZ
338
339 return 0;
340}
341
4772eaeb 342static int
e61e0f51 343gen7_render_ring_flush(struct i915_request *rq, u32 mode)
4772eaeb 344{
51797499 345 u32 scratch_addr = i915_scratch_offset(rq->i915) + 2 * CACHELINE_BYTES;
73dec95e 346 u32 *cs, flags = 0;
4772eaeb 347
f3987631
PZ
348 /*
349 * Ensure that any following seqno writes only happen when the render
350 * cache is indeed flushed.
351 *
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.
355 */
356 flags |= PIPE_CONTROL_CS_STALL;
357
4772eaeb
PZ
358 /* Just flush everything. Experiments have shown that reducing the
359 * number of bits based on the write domains has little performance
360 * impact.
361 */
7c9cf4e3 362 if (mode & EMIT_FLUSH) {
4772eaeb
PZ
363 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
364 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
965fd602 365 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
40a24488 366 flags |= PIPE_CONTROL_FLUSH_ENABLE;
4772eaeb 367 }
7c9cf4e3 368 if (mode & EMIT_INVALIDATE) {
4772eaeb
PZ
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;
148b83d0 375 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
4772eaeb
PZ
376 /*
377 * TLB invalidate requires a post-sync write.
378 */
379 flags |= PIPE_CONTROL_QW_WRITE;
b9e1faa7 380 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
f3987631 381
add284a3
CW
382 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
383
f3987631
PZ
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. */
e61e0f51 387 gen7_render_ring_cs_stall_wa(rq);
4772eaeb
PZ
388 }
389
e61e0f51 390 cs = intel_ring_begin(rq, 4);
73dec95e
TU
391 if (IS_ERR(cs))
392 return PTR_ERR(cs);
4772eaeb 393
73dec95e
TU
394 *cs++ = GFX_OP_PIPE_CONTROL(4);
395 *cs++ = flags;
396 *cs++ = scratch_addr;
397 *cs++ = 0;
e61e0f51 398 intel_ring_advance(rq, cs);
4772eaeb
PZ
399
400 return 0;
401}
402
e1a73a54 403static u32 *gen7_rcs_emit_breadcrumb(struct i915_request *rq, u32 *cs)
caa5915b
CW
404{
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);
5013eb8c
CW
413 *cs++ = rq->timeline->hwsp_offset;
414 *cs++ = rq->fence.seqno;
415
caa5915b
CW
416 *cs++ = MI_USER_INTERRUPT;
417 *cs++ = MI_NOOP;
418
419 rq->tail = intel_ring_offset(rq, cs);
420 assert_ring_tail_valid(rq->ring, rq->tail);
e1a73a54
CW
421
422 return cs;
caa5915b 423}
caa5915b 424
e1a73a54 425static u32 *gen6_xcs_emit_breadcrumb(struct i915_request *rq, u32 *cs)
caa5915b 426{
5013eb8c
CW
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);
429
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;
433
caa5915b
CW
434 *cs++ = MI_USER_INTERRUPT;
435
436 rq->tail = intel_ring_offset(rq, cs);
437 assert_ring_tail_valid(rq->ring, rq->tail);
e1a73a54
CW
438
439 return cs;
caa5915b 440}
caa5915b 441
1212bd82 442#define GEN7_XCS_WA 32
e1a73a54 443static u32 *gen7_xcs_emit_breadcrumb(struct i915_request *rq, u32 *cs)
1212bd82
CW
444{
445 int i;
446
5013eb8c
CW
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);
449
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;
453
1212bd82
CW
454 for (i = 0; i < GEN7_XCS_WA; i++) {
455 *cs++ = MI_STORE_DWORD_INDEX;
5013eb8c
CW
456 *cs++ = I915_GEM_HWS_SEQNO_ADDR;
457 *cs++ = rq->fence.seqno;
1212bd82
CW
458 }
459
460 *cs++ = MI_FLUSH_DW;
461 *cs++ = 0;
462 *cs++ = 0;
463
464 *cs++ = MI_USER_INTERRUPT;
519a0194 465 *cs++ = MI_NOOP;
1212bd82
CW
466
467 rq->tail = intel_ring_offset(rq, cs);
468 assert_ring_tail_valid(rq->ring, rq->tail);
e1a73a54
CW
469
470 return cs;
1212bd82 471}
1212bd82
CW
472#undef GEN7_XCS_WA
473
060f2322
CW
474static void set_hwstam(struct intel_engine_cs *engine, u32 mask)
475{
476 /*
477 * Keep the render interrupt unmasked as this papers over
478 * lost interrupts following a reset.
479 */
480 if (engine->class == RENDER_CLASS) {
481 if (INTEL_GEN(engine->i915) >= 6)
482 mask &= ~BIT(0);
483 else
484 mask &= ~I915_USER_INTERRUPT;
485 }
486
487 intel_engine_set_hwsp_writemask(engine, mask);
488}
489
490static void set_hws_pga(struct intel_engine_cs *engine, phys_addr_t phys)
035dc1e0 491{
c033666a 492 struct drm_i915_private *dev_priv = engine->i915;
035dc1e0
DV
493 u32 addr;
494
d6acae36 495 addr = lower_32_bits(phys);
c033666a 496 if (INTEL_GEN(dev_priv) >= 4)
d6acae36
CW
497 addr |= (phys >> 28) & 0xf0;
498
035dc1e0
DV
499 I915_WRITE(HWS_PGA, addr);
500}
501
0ca88ba0 502static struct page *status_page(struct intel_engine_cs *engine)
060f2322 503{
0ca88ba0 504 struct drm_i915_gem_object *obj = engine->status_page.vma->obj;
060f2322 505
0ca88ba0
CW
506 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
507 return sg_page(obj->mm.pages->sgl);
508}
509
510static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
511{
512 set_hws_pga(engine, PFN_PHYS(page_to_pfn(status_page(engine))));
060f2322
CW
513 set_hwstam(engine, ~0u);
514}
515
516static void set_hwsp(struct intel_engine_cs *engine, u32 offset)
af75f269 517{
c033666a 518 struct drm_i915_private *dev_priv = engine->i915;
060f2322 519 i915_reg_t hwsp;
af75f269 520
060f2322
CW
521 /*
522 * The ring status page addresses are no longer next to the rest of
af75f269
DL
523 * the ring registers as of gen7.
524 */
cf819eff 525 if (IS_GEN(dev_priv, 7)) {
0bc40be8 526 switch (engine->id) {
a2d3d265
MT
527 /*
528 * No more rings exist on Gen7. Default case is only to shut up
529 * gcc switch check warning.
530 */
531 default:
532 GEM_BUG_ON(engine->id);
8a68d464
CW
533 /* fallthrough */
534 case RCS0:
060f2322 535 hwsp = RENDER_HWS_PGA_GEN7;
af75f269 536 break;
8a68d464 537 case BCS0:
060f2322 538 hwsp = BLT_HWS_PGA_GEN7;
af75f269 539 break;
8a68d464 540 case VCS0:
060f2322 541 hwsp = BSD_HWS_PGA_GEN7;
af75f269 542 break;
8a68d464 543 case VECS0:
060f2322 544 hwsp = VEBOX_HWS_PGA_GEN7;
af75f269
DL
545 break;
546 }
cf819eff 547 } else if (IS_GEN(dev_priv, 6)) {
060f2322 548 hwsp = RING_HWS_PGA_GEN6(engine->mmio_base);
af75f269 549 } else {
060f2322 550 hwsp = RING_HWS_PGA(engine->mmio_base);
a4a71701 551 }
c5498089 552
060f2322
CW
553 I915_WRITE(hwsp, offset);
554 POSTING_READ(hwsp);
555}
af75f269 556
060f2322
CW
557static void flush_cs_tlb(struct intel_engine_cs *engine)
558{
559 struct drm_i915_private *dev_priv = engine->i915;
060f2322
CW
560
561 if (!IS_GEN_RANGE(dev_priv, 6, 7))
562 return;
563
564 /* ring should be idle before issuing a sync flush*/
baba6e57
DCS
565 WARN_ON((ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0);
566
567 ENGINE_WRITE(engine, RING_INSTPM,
568 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
569 INSTPM_SYNC_FLUSH));
570 if (intel_wait_for_register(engine->uncore,
571 RING_INSTPM(engine->mmio_base),
572 INSTPM_SYNC_FLUSH, 0,
060f2322
CW
573 1000))
574 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
575 engine->name);
576}
af75f269 577
060f2322
CW
578static void ring_setup_status_page(struct intel_engine_cs *engine)
579{
0ca88ba0 580 set_hwsp(engine, i915_ggtt_offset(engine->status_page.vma));
060f2322 581 set_hwstam(engine, ~0u);
af75f269 582
060f2322 583 flush_cs_tlb(engine);
af75f269
DL
584}
585
0bc40be8 586static bool stop_ring(struct intel_engine_cs *engine)
8187a2b7 587{
c033666a 588 struct drm_i915_private *dev_priv = engine->i915;
8187a2b7 589
21a2c58a 590 if (INTEL_GEN(dev_priv) > 2) {
baba6e57
DCS
591 ENGINE_WRITE(engine,
592 RING_MI_MODE, _MASKED_BIT_ENABLE(STOP_RING));
593 if (intel_wait_for_register(engine->uncore,
3d808eb1
CW
594 RING_MI_MODE(engine->mmio_base),
595 MODE_IDLE,
596 MODE_IDLE,
597 1000)) {
0bc40be8
TU
598 DRM_ERROR("%s : timed out trying to stop ring\n",
599 engine->name);
baba6e57
DCS
600
601 /*
602 * Sometimes we observe that the idle flag is not
9bec9b13
CW
603 * set even though the ring is empty. So double
604 * check before giving up.
605 */
baba6e57
DCS
606 if (ENGINE_READ(engine, RING_HEAD) !=
607 ENGINE_READ(engine, RING_TAIL))
9bec9b13 608 return false;
9991ae78
CW
609 }
610 }
b7884eb4 611
baba6e57 612 ENGINE_WRITE(engine, RING_HEAD, ENGINE_READ(engine, RING_TAIL));
11caf551 613
baba6e57
DCS
614 ENGINE_WRITE(engine, RING_HEAD, 0);
615 ENGINE_WRITE(engine, RING_TAIL, 0);
8187a2b7 616
11caf551 617 /* The ring must be empty before it is disabled */
baba6e57 618 ENGINE_WRITE(engine, RING_CTL, 0);
11caf551 619
baba6e57 620 return (ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR) == 0;
9991ae78 621}
8187a2b7 622
79ffac85 623static int xcs_resume(struct intel_engine_cs *engine)
9991ae78 624{
c033666a 625 struct drm_i915_private *dev_priv = engine->i915;
7e37f889 626 struct intel_ring *ring = engine->buffer;
9991ae78
CW
627 int ret = 0;
628
79ffac85
CW
629 GEM_TRACE("%s: ring:{HEAD:%04x, TAIL:%04x}\n",
630 engine->name, ring->head, ring->tail);
631
baba6e57 632 intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
9991ae78 633
0bc40be8 634 if (!stop_ring(engine)) {
9991ae78 635 /* G45 ring initialization often fails to reset head to zero */
8177e112
CW
636 DRM_DEBUG_DRIVER("%s head not reset to zero "
637 "ctl %08x head %08x tail %08x start %08x\n",
638 engine->name,
baba6e57
DCS
639 ENGINE_READ(engine, RING_CTL),
640 ENGINE_READ(engine, RING_HEAD),
641 ENGINE_READ(engine, RING_TAIL),
642 ENGINE_READ(engine, RING_START));
8187a2b7 643
0bc40be8 644 if (!stop_ring(engine)) {
6fd0d56e
CW
645 DRM_ERROR("failed to set %s head to zero "
646 "ctl %08x head %08x tail %08x start %08x\n",
0bc40be8 647 engine->name,
baba6e57
DCS
648 ENGINE_READ(engine, RING_CTL),
649 ENGINE_READ(engine, RING_HEAD),
650 ENGINE_READ(engine, RING_TAIL),
651 ENGINE_READ(engine, RING_START));
9991ae78
CW
652 ret = -EIO;
653 goto out;
6fd0d56e 654 }
8187a2b7
ZN
655 }
656
3177659a 657 if (HWS_NEEDS_PHYSICAL(dev_priv))
0bc40be8 658 ring_setup_phys_status_page(engine);
3177659a 659 else
060f2322 660 ring_setup_status_page(engine);
9991ae78 661
ad07dfcd 662 intel_engine_reset_breadcrumbs(engine);
821ed7df 663
ece4a17d 664 /* Enforce ordering by reading HEAD register back */
baba6e57 665 ENGINE_READ(engine, RING_HEAD);
ece4a17d 666
0d8957c8
DV
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. */
baba6e57 671 ENGINE_WRITE(engine, RING_START, i915_ggtt_offset(ring->vma));
95468892
CW
672
673 /* WaClearRingBufHeadRegAtInit:ctg,elk */
baba6e57 674 if (ENGINE_READ(engine, RING_HEAD))
8177e112 675 DRM_DEBUG_DRIVER("%s initialization failed [head=%08x], fudging\n",
baba6e57 676 engine->name, ENGINE_READ(engine, RING_HEAD));
821ed7df 677
41d37680
CW
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));
821ed7df 681 intel_ring_update_space(ring);
b7f21899
CW
682
683 /* First wake the ring up to an empty/idle ring */
baba6e57
DCS
684 ENGINE_WRITE(engine, RING_HEAD, ring->head);
685 ENGINE_WRITE(engine, RING_TAIL, ring->head);
686 ENGINE_POSTING_READ(engine, RING_TAIL);
95468892 687
baba6e57 688 ENGINE_WRITE(engine, RING_CTL, RING_CTL_SIZE(ring->size) | RING_VALID);
8187a2b7 689
8187a2b7 690 /* If the head is still not zero, the ring is dead */
baba6e57 691 if (intel_wait_for_register(engine->uncore,
97a04e0d 692 RING_CTL(engine->mmio_base),
f42bb651
CW
693 RING_VALID, RING_VALID,
694 50)) {
e74cfed5 695 DRM_ERROR("%s initialization failed "
821ed7df 696 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
0bc40be8 697 engine->name,
baba6e57
DCS
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),
bde13ebd 703 i915_ggtt_offset(ring->vma));
b7884eb4
DV
704 ret = -EIO;
705 goto out;
8187a2b7
ZN
706 }
707
7836cd02 708 if (INTEL_GEN(dev_priv) > 2)
baba6e57
DCS
709 ENGINE_WRITE(engine,
710 RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
7836cd02 711
b7f21899
CW
712 /* Now awake, let it get started */
713 if (ring->tail != ring->head) {
baba6e57
DCS
714 ENGINE_WRITE(engine, RING_TAIL, ring->tail);
715 ENGINE_POSTING_READ(engine, RING_TAIL);
b7f21899
CW
716 }
717
d6fee0de 718 /* Papering over lost _interrupts_ immediately following the restart */
52c0fdb2 719 intel_engine_queue_breadcrumbs(engine);
b7884eb4 720out:
baba6e57 721 intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
b7884eb4
DV
722
723 return ret;
8187a2b7
ZN
724}
725
eb8d0f5a 726static void reset_prepare(struct intel_engine_cs *engine)
821ed7df 727{
3f6e9822 728 intel_engine_stop_cs(engine);
5adfb772
CW
729}
730
eb8d0f5a 731static void reset_ring(struct intel_engine_cs *engine, bool stalled)
5adfb772 732{
eb8d0f5a
CW
733 struct i915_timeline *tl = &engine->timeline;
734 struct i915_request *pos, *rq;
735 unsigned long flags;
b3ee09a4 736 u32 head;
5adfb772 737
eb8d0f5a
CW
738 rq = NULL;
739 spin_lock_irqsave(&tl->lock, flags);
740 list_for_each_entry(pos, &tl->requests, link) {
5013eb8c 741 if (!i915_request_completed(pos)) {
eb8d0f5a
CW
742 rq = pos;
743 break;
744 }
b3ee09a4 745 }
67e64564
CW
746
747 /*
eb8d0f5a 748 * The guilty request will get skipped on a hung engine.
c0dcb203 749 *
eb8d0f5a
CW
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.
c0dcb203 763 *
eb8d0f5a
CW
764 * The context ban, and ultimately the client ban, mechanism are safety
765 * valves if client submission ends up resulting in nothing more than
766 * subsequent hangs.
c0dcb203 767 */
eb8d0f5a 768
b3ee09a4 769 if (rq) {
eb8d0f5a
CW
770 /*
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
776 * hangs.
777 *
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.
781 *
782 * If the request was innocent, we try to replay the request
783 * with the restored context.
784 */
785 i915_reset_request(rq, stalled);
786
787 GEM_BUG_ON(rq->ring != engine->buffer);
788 head = rq->head;
789 } else {
790 head = engine->buffer->tail;
c0dcb203 791 }
eb8d0f5a
CW
792 engine->buffer->head = intel_ring_wrap(engine->buffer, head);
793
794 spin_unlock_irqrestore(&tl->lock, flags);
821ed7df
CW
795}
796
5adfb772
CW
797static void reset_finish(struct intel_engine_cs *engine)
798{
799}
800
e61e0f51 801static int intel_rcs_ctx_init(struct i915_request *rq)
8f0e2b9d
DV
802{
803 int ret;
804
452420d2 805 ret = intel_engine_emit_ctx_wa(rq);
8f0e2b9d
DV
806 if (ret != 0)
807 return ret;
808
e61e0f51 809 ret = i915_gem_render_state_emit(rq);
8f0e2b9d 810 if (ret)
e26e1b97 811 return ret;
8f0e2b9d 812
e26e1b97 813 return 0;
8f0e2b9d
DV
814}
815
79ffac85 816static int rcs_resume(struct intel_engine_cs *engine)
8187a2b7 817{
c033666a 818 struct drm_i915_private *dev_priv = engine->i915;
a69ffdbf 819
9ce9bdb0
CW
820 /*
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.
824 *
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
828 * enabled.
829 */
830 if (IS_GEN(dev_priv, 4))
831 I915_WRITE(ECOSKPD,
832 _MASKED_BIT_ENABLE(ECO_CONSTANT_BUFFER_SR_DISABLE));
833
61a563a2 834 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
00690008 835 if (IS_GEN_RANGE(dev_priv, 4, 6))
6b26c86d 836 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1c8c38c5
CW
837
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.
8693a824 841 *
2441f877 842 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1c8c38c5 843 */
00690008 844 if (IS_GEN_RANGE(dev_priv, 6, 7))
1c8c38c5
CW
845 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
846
f05bb0c7 847 /* Required for the hardware to program scanline values for waiting */
01fa0302 848 /* WaEnableFlushTlbInvalidationMode:snb */
cf819eff 849 if (IS_GEN(dev_priv, 6))
f05bb0c7 850 I915_WRITE(GFX_MODE,
aa83e30d 851 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
f05bb0c7 852
01fa0302 853 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
cf819eff 854 if (IS_GEN(dev_priv, 7))
1c8c38c5 855 I915_WRITE(GFX_MODE_GEN7,
01fa0302 856 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1c8c38c5 857 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
78501eac 858
cf819eff 859 if (IS_GEN(dev_priv, 6)) {
3a69ddd6
KG
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."
864 */
865 I915_WRITE(CACHE_MODE_0,
5e13a0c5 866 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
84f9f938
BW
867 }
868
00690008 869 if (IS_GEN_RANGE(dev_priv, 6, 7))
6b26c86d 870 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
84f9f938 871
79ffac85 872 return xcs_resume(engine);
8187a2b7
ZN
873}
874
27a5f61b
CW
875static void cancel_requests(struct intel_engine_cs *engine)
876{
e61e0f51 877 struct i915_request *request;
27a5f61b
CW
878 unsigned long flags;
879
a89d1f92 880 spin_lock_irqsave(&engine->timeline.lock, flags);
27a5f61b
CW
881
882 /* Mark all submitted requests as skipped. */
a89d1f92 883 list_for_each_entry(request, &engine->timeline.requests, link) {
5013eb8c
CW
884 if (!i915_request_signaled(request))
885 dma_fence_set_error(&request->fence, -EIO);
3800960a 886
5013eb8c 887 i915_request_mark_complete(request);
27a5f61b 888 }
3800960a 889
27a5f61b
CW
890 /* Remaining _unready_ requests will be nop'ed when submitted */
891
a89d1f92 892 spin_unlock_irqrestore(&engine->timeline.lock, flags);
27a5f61b
CW
893}
894
e61e0f51 895static void i9xx_submit_request(struct i915_request *request)
b0411e7d 896{
e61e0f51 897 i915_request_submit(request);
d55ac5bf 898
baba6e57
DCS
899 ENGINE_WRITE(request->engine, RING_TAIL,
900 intel_ring_set_tail(request->ring, request->tail));
b0411e7d
CW
901}
902
e1a73a54 903static u32 *i9xx_emit_breadcrumb(struct i915_request *rq, u32 *cs)
1ec14ad3 904{
5013eb8c
CW
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);
907
caa5915b
CW
908 *cs++ = MI_FLUSH;
909
5013eb8c
CW
910 *cs++ = MI_STORE_DWORD_INDEX;
911 *cs++ = I915_GEM_HWS_SEQNO_ADDR;
912 *cs++ = rq->fence.seqno;
913
73dec95e 914 *cs++ = MI_USER_INTERRUPT;
519a0194 915 *cs++ = MI_NOOP;
1ec14ad3 916
e61e0f51
CW
917 rq->tail = intel_ring_offset(rq, cs);
918 assert_ring_tail_valid(rq->ring, rq->tail);
e1a73a54
CW
919
920 return cs;
1ec14ad3 921}
98f29e8d 922
835051d3 923#define GEN5_WA_STORES 8 /* must be at least 1! */
e1a73a54 924static u32 *gen5_emit_breadcrumb(struct i915_request *rq, u32 *cs)
c6df541c 925{
835051d3
CW
926 int i;
927
5013eb8c
CW
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);
930
835051d3
CW
931 *cs++ = MI_FLUSH;
932
933 BUILD_BUG_ON(GEN5_WA_STORES < 1);
934 for (i = 0; i < GEN5_WA_STORES; i++) {
935 *cs++ = MI_STORE_DWORD_INDEX;
b300fde8
CW
936 *cs++ = I915_GEM_HWS_SEQNO_ADDR;
937 *cs++ = rq->fence.seqno;
835051d3
CW
938 }
939
940 *cs++ = MI_USER_INTERRUPT;
941
942 rq->tail = intel_ring_offset(rq, cs);
943 assert_ring_tail_valid(rq->ring, rq->tail);
e1a73a54
CW
944
945 return cs;
c6df541c 946}
835051d3 947#undef GEN5_WA_STORES
c6df541c 948
31bb59cc
CW
949static void
950gen5_irq_enable(struct intel_engine_cs *engine)
e48d8634 951{
31bb59cc 952 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
DV
953}
954
955static void
31bb59cc 956gen5_irq_disable(struct intel_engine_cs *engine)
e48d8634 957{
31bb59cc 958 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
DV
959}
960
31bb59cc
CW
961static void
962i9xx_irq_enable(struct intel_engine_cs *engine)
62fdfeaf 963{
baba6e57 964 engine->i915->irq_mask &= ~engine->irq_enable_mask;
9d9523d8
PZ
965 intel_uncore_write(engine->uncore, GEN2_IMR, engine->i915->irq_mask);
966 intel_uncore_posting_read_fw(engine->uncore, GEN2_IMR);
62fdfeaf
EA
967}
968
8187a2b7 969static void
31bb59cc 970i9xx_irq_disable(struct intel_engine_cs *engine)
62fdfeaf 971{
baba6e57 972 engine->i915->irq_mask |= engine->irq_enable_mask;
9d9523d8 973 intel_uncore_write(engine->uncore, GEN2_IMR, engine->i915->irq_mask);
62fdfeaf
EA
974}
975
31bb59cc
CW
976static void
977i8xx_irq_enable(struct intel_engine_cs *engine)
c2798b19 978{
e44d62d1 979 struct drm_i915_private *i915 = engine->i915;
c2798b19 980
e44d62d1
TU
981 i915->irq_mask &= ~engine->irq_enable_mask;
982 intel_uncore_write16(&i915->uncore, GEN2_IMR, i915->irq_mask);
983 ENGINE_POSTING_READ16(engine, RING_IMR);
c2798b19
CW
984}
985
986static void
31bb59cc 987i8xx_irq_disable(struct intel_engine_cs *engine)
c2798b19 988{
4f5fd91f 989 struct drm_i915_private *i915 = engine->i915;
c2798b19 990
4f5fd91f
TU
991 i915->irq_mask |= engine->irq_enable_mask;
992 intel_uncore_write16(&i915->uncore, GEN2_IMR, i915->irq_mask);
c2798b19
CW
993}
994
b72f3acb 995static int
e61e0f51 996bsd_ring_flush(struct i915_request *rq, u32 mode)
d1b851fc 997{
73dec95e 998 u32 *cs;
b72f3acb 999
e61e0f51 1000 cs = intel_ring_begin(rq, 2);
73dec95e
TU
1001 if (IS_ERR(cs))
1002 return PTR_ERR(cs);
b72f3acb 1003
73dec95e
TU
1004 *cs++ = MI_FLUSH;
1005 *cs++ = MI_NOOP;
e61e0f51 1006 intel_ring_advance(rq, cs);
b72f3acb 1007 return 0;
d1b851fc
ZN
1008}
1009
31bb59cc
CW
1010static void
1011gen6_irq_enable(struct intel_engine_cs *engine)
0f46832f 1012{
baba6e57
DCS
1013 ENGINE_WRITE(engine, RING_IMR,
1014 ~(engine->irq_enable_mask | engine->irq_keep_mask));
476af9c2
CW
1015
1016 /* Flush/delay to ensure the RING_IMR is active before the GT IMR */
baba6e57 1017 ENGINE_POSTING_READ(engine, RING_IMR);
476af9c2 1018
baba6e57 1019 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
0f46832f
CW
1020}
1021
1022static void
31bb59cc 1023gen6_irq_disable(struct intel_engine_cs *engine)
0f46832f 1024{
baba6e57
DCS
1025 ENGINE_WRITE(engine, RING_IMR, ~engine->irq_keep_mask);
1026 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
d1b851fc
ZN
1027}
1028
31bb59cc
CW
1029static void
1030hsw_vebox_irq_enable(struct intel_engine_cs *engine)
a19d2933 1031{
baba6e57 1032 ENGINE_WRITE(engine, RING_IMR, ~engine->irq_enable_mask);
e4fc69f2
CW
1033
1034 /* Flush/delay to ensure the RING_IMR is active before the GT IMR */
baba6e57 1035 ENGINE_POSTING_READ(engine, RING_IMR);
e4fc69f2 1036
baba6e57 1037 gen6_unmask_pm_irq(engine->i915, engine->irq_enable_mask);
a19d2933
BW
1038}
1039
1040static void
31bb59cc 1041hsw_vebox_irq_disable(struct intel_engine_cs *engine)
a19d2933 1042{
baba6e57
DCS
1043 ENGINE_WRITE(engine, RING_IMR, ~0);
1044 gen6_mask_pm_irq(engine->i915, engine->irq_enable_mask);
a19d2933
BW
1045}
1046
d1b851fc 1047static int
e61e0f51 1048i965_emit_bb_start(struct i915_request *rq,
803688ba
CW
1049 u64 offset, u32 length,
1050 unsigned int dispatch_flags)
d1b851fc 1051{
73dec95e 1052 u32 *cs;
78501eac 1053
e61e0f51 1054 cs = intel_ring_begin(rq, 2);
73dec95e
TU
1055 if (IS_ERR(cs))
1056 return PTR_ERR(cs);
e1f99ce6 1057
73dec95e
TU
1058 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
1059 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
1060 *cs++ = offset;
e61e0f51 1061 intel_ring_advance(rq, cs);
78501eac 1062
d1b851fc
ZN
1063 return 0;
1064}
1065
b45305fc 1066/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
51797499 1067#define I830_BATCH_LIMIT SZ_256K
c4d69da1
CW
1068#define I830_TLB_ENTRIES (2)
1069#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
8187a2b7 1070static int
e61e0f51 1071i830_emit_bb_start(struct i915_request *rq,
803688ba
CW
1072 u64 offset, u32 len,
1073 unsigned int dispatch_flags)
62fdfeaf 1074{
51797499
CW
1075 u32 *cs, cs_offset = i915_scratch_offset(rq->i915);
1076
1077 GEM_BUG_ON(rq->i915->gt.scratch->size < I830_WA_SIZE);
62fdfeaf 1078
e61e0f51 1079 cs = intel_ring_begin(rq, 6);
73dec95e
TU
1080 if (IS_ERR(cs))
1081 return PTR_ERR(cs);
62fdfeaf 1082
c4d69da1 1083 /* Evict the invalid PTE TLBs */
73dec95e
TU
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 */
1087 *cs++ = cs_offset;
1088 *cs++ = 0xdeadbeef;
1089 *cs++ = MI_NOOP;
e61e0f51 1090 intel_ring_advance(rq, cs);
b45305fc 1091
8e004efc 1092 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
b45305fc
DV
1093 if (len > I830_BATCH_LIMIT)
1094 return -ENOSPC;
1095
e61e0f51 1096 cs = intel_ring_begin(rq, 6 + 2);
73dec95e
TU
1097 if (IS_ERR(cs))
1098 return PTR_ERR(cs);
c4d69da1
CW
1099
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) ...
1103 */
73dec95e
TU
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;
1107 *cs++ = cs_offset;
1108 *cs++ = 4096;
1109 *cs++ = offset;
1110
1111 *cs++ = MI_FLUSH;
1112 *cs++ = MI_NOOP;
e61e0f51 1113 intel_ring_advance(rq, cs);
b45305fc
DV
1114
1115 /* ... and execute it. */
c4d69da1 1116 offset = cs_offset;
b45305fc 1117 }
e1f99ce6 1118
e61e0f51 1119 cs = intel_ring_begin(rq, 2);
73dec95e
TU
1120 if (IS_ERR(cs))
1121 return PTR_ERR(cs);
c4d69da1 1122
73dec95e
TU
1123 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1124 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1125 MI_BATCH_NON_SECURE);
e61e0f51 1126 intel_ring_advance(rq, cs);
c4d69da1 1127
fb3256da
DV
1128 return 0;
1129}
1130
1131static int
e61e0f51 1132i915_emit_bb_start(struct i915_request *rq,
803688ba
CW
1133 u64 offset, u32 len,
1134 unsigned int dispatch_flags)
fb3256da 1135{
73dec95e 1136 u32 *cs;
fb3256da 1137
e61e0f51 1138 cs = intel_ring_begin(rq, 2);
73dec95e
TU
1139 if (IS_ERR(cs))
1140 return PTR_ERR(cs);
fb3256da 1141
73dec95e
TU
1142 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1143 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1144 MI_BATCH_NON_SECURE);
e61e0f51 1145 intel_ring_advance(rq, cs);
62fdfeaf 1146
62fdfeaf
EA
1147 return 0;
1148}
1149
5503cb0d 1150int intel_ring_pin(struct intel_ring *ring)
7ba717cf 1151{
57e88531 1152 struct i915_vma *vma = ring->vma;
89d5efcc 1153 enum i915_map_type map = i915_coherent_map_type(vma->vm->i915);
d822bb18 1154 unsigned int flags;
8305216f 1155 void *addr;
7ba717cf
TD
1156 int ret;
1157
57e88531 1158 GEM_BUG_ON(ring->vaddr);
7ba717cf 1159
5013eb8c
CW
1160 ret = i915_timeline_pin(ring->timeline);
1161 if (ret)
1162 return ret;
1163
d3ef1af6 1164 flags = PIN_GLOBAL;
496bcce3
JB
1165
1166 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
1167 flags |= PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
1168
9d80841e 1169 if (vma->obj->stolen)
57e88531 1170 flags |= PIN_MAPPABLE;
2edd4e69
CW
1171 else
1172 flags |= PIN_HIGH;
def0c5f6 1173
7a859c65 1174 ret = i915_vma_pin(vma, 0, 0, flags);
57e88531 1175 if (unlikely(ret))
5013eb8c 1176 goto unpin_timeline;
def0c5f6 1177
9d80841e 1178 if (i915_vma_is_map_and_fenceable(vma))
57e88531
CW
1179 addr = (void __force *)i915_vma_pin_iomap(vma);
1180 else
9d80841e 1181 addr = i915_gem_object_pin_map(vma->obj, map);
5013eb8c
CW
1182 if (IS_ERR(addr)) {
1183 ret = PTR_ERR(addr);
1184 goto unpin_ring;
1185 }
7ba717cf 1186
3d574a6b
CW
1187 vma->obj->pin_global++;
1188
32c04f16 1189 ring->vaddr = addr;
7ba717cf 1190 return 0;
d2cad535 1191
5013eb8c 1192unpin_ring:
57e88531 1193 i915_vma_unpin(vma);
5013eb8c
CW
1194unpin_timeline:
1195 i915_timeline_unpin(ring->timeline);
1196 return ret;
7ba717cf
TD
1197}
1198
e6ba9992
CW
1199void intel_ring_reset(struct intel_ring *ring, u32 tail)
1200{
41d37680
CW
1201 GEM_BUG_ON(!intel_ring_offset_valid(ring, tail));
1202
e6ba9992
CW
1203 ring->tail = tail;
1204 ring->head = tail;
1205 ring->emit = tail;
1206 intel_ring_update_space(ring);
1207}
1208
aad29fbb
CW
1209void intel_ring_unpin(struct intel_ring *ring)
1210{
1211 GEM_BUG_ON(!ring->vma);
1212 GEM_BUG_ON(!ring->vaddr);
1213
e6ba9992
CW
1214 /* Discard any unused bytes beyond that submitted to hw. */
1215 intel_ring_reset(ring, ring->tail);
1216
9d80841e 1217 if (i915_vma_is_map_and_fenceable(ring->vma))
aad29fbb 1218 i915_vma_unpin_iomap(ring->vma);
57e88531
CW
1219 else
1220 i915_gem_object_unpin_map(ring->vma->obj);
aad29fbb
CW
1221 ring->vaddr = NULL;
1222
3d574a6b 1223 ring->vma->obj->pin_global--;
57e88531 1224 i915_vma_unpin(ring->vma);
5013eb8c
CW
1225
1226 i915_timeline_unpin(ring->timeline);
2919d291
OM
1227}
1228
57e88531
CW
1229static struct i915_vma *
1230intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
62fdfeaf 1231{
250f8c81 1232 struct i915_address_space *vm = &dev_priv->ggtt.vm;
05394f39 1233 struct drm_i915_gem_object *obj;
57e88531 1234 struct i915_vma *vma;
62fdfeaf 1235
187685cb 1236 obj = i915_gem_object_create_stolen(dev_priv, size);
c58b735f 1237 if (!obj)
2d6c4c84 1238 obj = i915_gem_object_create_internal(dev_priv, size);
57e88531
CW
1239 if (IS_ERR(obj))
1240 return ERR_CAST(obj);
8187a2b7 1241
250f8c81
JB
1242 /*
1243 * Mark ring buffers as read-only from GPU side (so no stray overwrites)
1244 * if supported by the platform's GGTT.
1245 */
1246 if (vm->has_read_only)
3e977ac6 1247 i915_gem_object_set_readonly(obj);
24f3a8cf 1248
250f8c81 1249 vma = i915_vma_instance(obj, vm, NULL);
57e88531
CW
1250 if (IS_ERR(vma))
1251 goto err;
1252
1253 return vma;
e3efda49 1254
57e88531
CW
1255err:
1256 i915_gem_object_put(obj);
1257 return vma;
e3efda49
CW
1258}
1259
7e37f889 1260struct intel_ring *
65fcb806 1261intel_engine_create_ring(struct intel_engine_cs *engine,
a89d1f92 1262 struct i915_timeline *timeline,
65fcb806 1263 int size)
01101fa7 1264{
7e37f889 1265 struct intel_ring *ring;
57e88531 1266 struct i915_vma *vma;
01101fa7 1267
8f942018 1268 GEM_BUG_ON(!is_power_of_2(size));
62ae14b1 1269 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
a89d1f92 1270 GEM_BUG_ON(timeline == &engine->timeline);
b887d615 1271 lockdep_assert_held(&engine->i915->drm.struct_mutex);
8f942018 1272
01101fa7 1273 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
57e88531 1274 if (!ring)
01101fa7
CW
1275 return ERR_PTR(-ENOMEM);
1276
65baf0ef 1277 kref_init(&ring->ref);
675d9ad7 1278 INIT_LIST_HEAD(&ring->request_list);
a89d1f92 1279 ring->timeline = i915_timeline_get(timeline);
675d9ad7 1280
01101fa7
CW
1281 ring->size = size;
1282 /* Workaround an erratum on the i830 which causes a hang if
1283 * the TAIL pointer points to within the last 2 cachelines
1284 * of the buffer.
1285 */
1286 ring->effective_size = size;
2a307c2e 1287 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
01101fa7
CW
1288 ring->effective_size -= 2 * CACHELINE_BYTES;
1289
01101fa7
CW
1290 intel_ring_update_space(ring);
1291
57e88531
CW
1292 vma = intel_ring_create_vma(engine->i915, size);
1293 if (IS_ERR(vma)) {
01101fa7 1294 kfree(ring);
57e88531 1295 return ERR_CAST(vma);
01101fa7 1296 }
57e88531 1297 ring->vma = vma;
01101fa7
CW
1298
1299 return ring;
1300}
1301
65baf0ef 1302void intel_ring_free(struct kref *ref)
01101fa7 1303{
65baf0ef 1304 struct intel_ring *ring = container_of(ref, typeof(*ring), ref);
f8a7fde4
CW
1305
1306 i915_vma_close(ring->vma);
c017cf6b 1307 i915_vma_put(ring->vma);
f8a7fde4 1308
a89d1f92 1309 i915_timeline_put(ring->timeline);
01101fa7
CW
1310 kfree(ring);
1311}
1312
c4d52feb
CW
1313static void __ring_context_fini(struct intel_context *ce)
1314{
1315 GEM_BUG_ON(i915_gem_object_is_active(ce->state->obj));
1316 i915_gem_object_put(ce->state->obj);
1317}
1318
4c5896dc 1319static void ring_context_destroy(struct kref *ref)
1fc44d9b 1320{
4c5896dc
CW
1321 struct intel_context *ce = container_of(ref, typeof(*ce), ref);
1322
08819549 1323 GEM_BUG_ON(intel_context_is_pinned(ce));
1fc44d9b 1324
c4d52feb
CW
1325 if (ce->state)
1326 __ring_context_fini(ce);
efe79d48 1327
c4d52feb 1328 intel_context_free(ce);
1fc44d9b
CW
1329}
1330
a2bbf714
CW
1331static int __context_pin_ppgtt(struct i915_gem_context *ctx)
1332{
e568ac38 1333 struct i915_address_space *vm;
a2bbf714
CW
1334 int err = 0;
1335
e568ac38
CW
1336 vm = ctx->vm ?: &ctx->i915->mm.aliasing_ppgtt->vm;
1337 if (vm)
1338 err = gen6_ppgtt_pin(i915_vm_to_ppgtt((vm)));
a2bbf714
CW
1339
1340 return err;
1341}
1342
1343static void __context_unpin_ppgtt(struct i915_gem_context *ctx)
1344{
e568ac38 1345 struct i915_address_space *vm;
a2bbf714 1346
e568ac38
CW
1347 vm = ctx->vm ?: &ctx->i915->mm.aliasing_ppgtt->vm;
1348 if (vm)
1349 gen6_ppgtt_unpin(i915_vm_to_ppgtt(vm));
a2bbf714
CW
1350}
1351
4dc84b77 1352static void ring_context_unpin(struct intel_context *ce)
d901e8e6 1353{
a2bbf714 1354 __context_unpin_ppgtt(ce->gem_context);
e8a9c58f
CW
1355}
1356
3204c343
CW
1357static struct i915_vma *
1358alloc_context_vma(struct intel_engine_cs *engine)
1359{
1360 struct drm_i915_private *i915 = engine->i915;
1361 struct drm_i915_gem_object *obj;
1362 struct i915_vma *vma;
d2b4b979 1363 int err;
3204c343 1364
8475355f 1365 obj = i915_gem_object_create_shmem(i915, engine->context_size);
3204c343
CW
1366 if (IS_ERR(obj))
1367 return ERR_CAST(obj);
1368
a679f58d
CW
1369 /*
1370 * Try to make the context utilize L3 as well as LLC.
1371 *
1372 * On VLV we don't have L3 controls in the PTEs so we
1373 * shouldn't touch the cache level, especially as that
1374 * would make the object snooped which might have a
1375 * negative performance impact.
1376 *
1377 * Snooping is required on non-llc platforms in execlist
1378 * mode, but since all GGTT accesses use PAT entry 0 we
1379 * get snooping anyway regardless of cache_level.
1380 *
1381 * This is only applicable for Ivy Bridge devices since
1382 * later platforms don't have L3 control bits in the PTE.
1383 */
1384 if (IS_IVYBRIDGE(i915))
1385 i915_gem_object_set_cache_coherency(obj, I915_CACHE_L3_LLC);
1386
d2b4b979
CW
1387 if (engine->default_state) {
1388 void *defaults, *vaddr;
1389
1390 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1391 if (IS_ERR(vaddr)) {
1392 err = PTR_ERR(vaddr);
1393 goto err_obj;
1394 }
1395
1396 defaults = i915_gem_object_pin_map(engine->default_state,
1397 I915_MAP_WB);
1398 if (IS_ERR(defaults)) {
1399 err = PTR_ERR(defaults);
1400 goto err_map;
1401 }
1402
1403 memcpy(vaddr, defaults, engine->context_size);
d2b4b979 1404 i915_gem_object_unpin_map(engine->default_state);
d2b4b979 1405
a679f58d
CW
1406 i915_gem_object_flush_map(obj);
1407 i915_gem_object_unpin_map(obj);
3204c343
CW
1408 }
1409
82ad6443 1410 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
d2b4b979
CW
1411 if (IS_ERR(vma)) {
1412 err = PTR_ERR(vma);
1413 goto err_obj;
1414 }
3204c343
CW
1415
1416 return vma;
d2b4b979
CW
1417
1418err_map:
1419 i915_gem_object_unpin_map(obj);
1420err_obj:
1421 i915_gem_object_put(obj);
1422 return ERR_PTR(err);
3204c343
CW
1423}
1424
95f697eb 1425static int ring_context_pin(struct intel_context *ce)
0cb26a8e 1426{
95f697eb 1427 struct intel_engine_cs *engine = ce->engine;
1fc44d9b 1428 int err;
0cb26a8e 1429
7e3d9a59
CW
1430 /* One ringbuffer to rule them all */
1431 GEM_BUG_ON(!engine->buffer);
1432 ce->ring = engine->buffer;
1433
63ffbcda 1434 if (!ce->state && engine->context_size) {
3204c343
CW
1435 struct i915_vma *vma;
1436
1437 vma = alloc_context_vma(engine);
95f697eb
CW
1438 if (IS_ERR(vma))
1439 return PTR_ERR(vma);
3204c343
CW
1440
1441 ce->state = vma;
1442 }
1443
ce476c80 1444 err = intel_context_active_acquire(ce, PIN_HIGH);
d901e8e6 1445 if (err)
95f697eb 1446 return err;
0cb26a8e 1447
a2bbf714
CW
1448 err = __context_pin_ppgtt(ce->gem_context);
1449 if (err)
ce476c80 1450 goto err_active;
a2bbf714 1451
95f697eb 1452 return 0;
266a240b 1453
ce476c80
CW
1454err_active:
1455 intel_context_active_release(ce);
95f697eb 1456 return err;
0cb26a8e
CW
1457}
1458
9726920b
CW
1459static void ring_context_reset(struct intel_context *ce)
1460{
1461 intel_ring_reset(ce->ring, 0);
1462}
1463
4dc84b77 1464static const struct intel_context_ops ring_context_ops = {
95f697eb 1465 .pin = ring_context_pin,
4dc84b77 1466 .unpin = ring_context_unpin,
9726920b 1467
6eee33e8
CW
1468 .enter = intel_context_enter_engine,
1469 .exit = intel_context_exit_engine,
1470
9726920b 1471 .reset = ring_context_reset,
4dc84b77
CW
1472 .destroy = ring_context_destroy,
1473};
1474
ab53497b 1475static int load_pd_dir(struct i915_request *rq, const struct i915_ppgtt *ppgtt)
b3ee09a4
CW
1476{
1477 const struct intel_engine_cs * const engine = rq->engine;
1478 u32 *cs;
1479
1480 cs = intel_ring_begin(rq, 6);
1481 if (IS_ERR(cs))
1482 return PTR_ERR(cs);
1483
1484 *cs++ = MI_LOAD_REGISTER_IMM(1);
baba6e57 1485 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine->mmio_base));
b3ee09a4
CW
1486 *cs++ = PP_DIR_DCLV_2G;
1487
1488 *cs++ = MI_LOAD_REGISTER_IMM(1);
baba6e57 1489 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base));
b3ee09a4
CW
1490 *cs++ = ppgtt->pd.base.ggtt_offset << 10;
1491
1492 intel_ring_advance(rq, cs);
1493
1494 return 0;
1495}
1496
d9d117e4
CW
1497static int flush_pd_dir(struct i915_request *rq)
1498{
1499 const struct intel_engine_cs * const engine = rq->engine;
1500 u32 *cs;
1501
1502 cs = intel_ring_begin(rq, 4);
1503 if (IS_ERR(cs))
1504 return PTR_ERR(cs);
1505
1506 /* Stall until the page table load is complete */
1507 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
baba6e57 1508 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base));
51797499 1509 *cs++ = i915_scratch_offset(rq->i915);
d9d117e4
CW
1510 *cs++ = MI_NOOP;
1511
1512 intel_ring_advance(rq, cs);
1513 return 0;
1514}
1515
e61e0f51 1516static inline int mi_set_context(struct i915_request *rq, u32 flags)
8911a31c
CW
1517{
1518 struct drm_i915_private *i915 = rq->i915;
1519 struct intel_engine_cs *engine = rq->engine;
1520 enum intel_engine_id id;
8a68d464
CW
1521 const int num_engines =
1522 IS_HSW_GT1(i915) ? RUNTIME_INFO(i915)->num_engines - 1 : 0;
1fc719d1 1523 bool force_restore = false;
8911a31c
CW
1524 int len;
1525 u32 *cs;
1526
1527 flags |= MI_MM_SPACE_GTT;
1528 if (IS_HASWELL(i915))
1529 /* These flags are for resource streamer on HSW+ */
1530 flags |= HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN;
1531 else
1215d28e 1532 /* We need to save the extended state for powersaving modes */
8911a31c
CW
1533 flags |= MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN;
1534
1535 len = 4;
cf819eff 1536 if (IS_GEN(i915, 7))
8a68d464 1537 len += 2 + (num_engines ? 4 * num_engines + 6 : 0);
1215d28e
CW
1538 else if (IS_GEN(i915, 5))
1539 len += 2;
1fc719d1
CW
1540 if (flags & MI_FORCE_RESTORE) {
1541 GEM_BUG_ON(flags & MI_RESTORE_INHIBIT);
1542 flags &= ~MI_FORCE_RESTORE;
1543 force_restore = true;
1544 len += 2;
1545 }
8911a31c
CW
1546
1547 cs = intel_ring_begin(rq, len);
1548 if (IS_ERR(cs))
1549 return PTR_ERR(cs);
1550
1551 /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
cf819eff 1552 if (IS_GEN(i915, 7)) {
8911a31c 1553 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
8a68d464 1554 if (num_engines) {
8911a31c
CW
1555 struct intel_engine_cs *signaller;
1556
8a68d464 1557 *cs++ = MI_LOAD_REGISTER_IMM(num_engines);
8911a31c
CW
1558 for_each_engine(signaller, i915, id) {
1559 if (signaller == engine)
1560 continue;
1561
1562 *cs++ = i915_mmio_reg_offset(
1563 RING_PSMI_CTL(signaller->mmio_base));
1564 *cs++ = _MASKED_BIT_ENABLE(
1565 GEN6_PSMI_SLEEP_MSG_DISABLE);
1566 }
1567 }
1215d28e
CW
1568 } else if (IS_GEN(i915, 5)) {
1569 /*
1570 * This w/a is only listed for pre-production ilk a/b steppings,
1571 * but is also mentioned for programming the powerctx. To be
1572 * safe, just apply the workaround; we do not use SyncFlush so
1573 * this should never take effect and so be a no-op!
1574 */
1575 *cs++ = MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN;
8911a31c
CW
1576 }
1577
1fc719d1
CW
1578 if (force_restore) {
1579 /*
1580 * The HW doesn't handle being told to restore the current
1581 * context very well. Quite often it likes goes to go off and
1582 * sulk, especially when it is meant to be reloading PP_DIR.
1583 * A very simple fix to force the reload is to simply switch
1584 * away from the current context and back again.
1585 *
1586 * Note that the kernel_context will contain random state
1587 * following the INHIBIT_RESTORE. We accept this since we
1588 * never use the kernel_context state; it is merely a
1589 * placeholder we use to flush other contexts.
1590 */
1591 *cs++ = MI_SET_CONTEXT;
9dbfea98 1592 *cs++ = i915_ggtt_offset(engine->kernel_context->state) |
1fc719d1
CW
1593 MI_MM_SPACE_GTT |
1594 MI_RESTORE_INHIBIT;
1595 }
1596
8911a31c
CW
1597 *cs++ = MI_NOOP;
1598 *cs++ = MI_SET_CONTEXT;
1fc44d9b 1599 *cs++ = i915_ggtt_offset(rq->hw_context->state) | flags;
8911a31c
CW
1600 /*
1601 * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
1602 * WaMiSetContext_Hang:snb,ivb,vlv
1603 */
1604 *cs++ = MI_NOOP;
1605
cf819eff 1606 if (IS_GEN(i915, 7)) {
8a68d464 1607 if (num_engines) {
8911a31c
CW
1608 struct intel_engine_cs *signaller;
1609 i915_reg_t last_reg = {}; /* keep gcc quiet */
1610
8a68d464 1611 *cs++ = MI_LOAD_REGISTER_IMM(num_engines);
8911a31c
CW
1612 for_each_engine(signaller, i915, id) {
1613 if (signaller == engine)
1614 continue;
1615
1616 last_reg = RING_PSMI_CTL(signaller->mmio_base);
1617 *cs++ = i915_mmio_reg_offset(last_reg);
1618 *cs++ = _MASKED_BIT_DISABLE(
1619 GEN6_PSMI_SLEEP_MSG_DISABLE);
1620 }
1621
1622 /* Insert a delay before the next switch! */
1623 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
1624 *cs++ = i915_mmio_reg_offset(last_reg);
51797499 1625 *cs++ = i915_scratch_offset(rq->i915);
8911a31c
CW
1626 *cs++ = MI_NOOP;
1627 }
1628 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1215d28e
CW
1629 } else if (IS_GEN(i915, 5)) {
1630 *cs++ = MI_SUSPEND_FLUSH;
8911a31c
CW
1631 }
1632
1633 intel_ring_advance(rq, cs);
1634
1635 return 0;
1636}
1637
e61e0f51 1638static int remap_l3(struct i915_request *rq, int slice)
8911a31c
CW
1639{
1640 u32 *cs, *remap_info = rq->i915->l3_parity.remap_info[slice];
1641 int i;
1642
1643 if (!remap_info)
1644 return 0;
1645
1646 cs = intel_ring_begin(rq, GEN7_L3LOG_SIZE/4 * 2 + 2);
1647 if (IS_ERR(cs))
1648 return PTR_ERR(cs);
1649
1650 /*
1651 * Note: We do not worry about the concurrent register cacheline hang
1652 * here because no other code should access these registers other than
1653 * at initialization time.
1654 */
1655 *cs++ = MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4);
1656 for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
1657 *cs++ = i915_mmio_reg_offset(GEN7_L3LOG(slice, i));
1658 *cs++ = remap_info[i];
1659 }
1660 *cs++ = MI_NOOP;
1661 intel_ring_advance(rq, cs);
1662
1663 return 0;
1664}
1665
e61e0f51 1666static int switch_context(struct i915_request *rq)
8911a31c
CW
1667{
1668 struct intel_engine_cs *engine = rq->engine;
b3ee09a4 1669 struct i915_gem_context *ctx = rq->gem_context;
e568ac38
CW
1670 struct i915_address_space *vm =
1671 ctx->vm ?: &rq->i915->mm.aliasing_ppgtt->vm;
b3ee09a4 1672 unsigned int unwind_mm = 0;
8911a31c
CW
1673 u32 hw_flags = 0;
1674 int ret, i;
1675
8911a31c
CW
1676 GEM_BUG_ON(HAS_EXECLISTS(rq->i915));
1677
e568ac38 1678 if (vm) {
ab53497b 1679 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
e2a13d1b
CW
1680 int loops;
1681
1682 /*
1683 * Baytail takes a little more convincing that it really needs
1684 * to reload the PD between contexts. It is not just a little
1685 * longer, as adding more stalls after the load_pd_dir (i.e.
1686 * adding a long loop around flush_pd_dir) is not as effective
1687 * as reloading the PD umpteen times. 32 is derived from
1688 * experimentation (gem_exec_parallel/fds) and has no good
1689 * explanation.
1690 */
1691 loops = 1;
8a68d464 1692 if (engine->id == BCS0 && IS_VALLEYVIEW(engine->i915))
e2a13d1b
CW
1693 loops = 32;
1694
1695 do {
1696 ret = load_pd_dir(rq, ppgtt);
1697 if (ret)
1698 goto err;
1699 } while (--loops);
8911a31c 1700
8a68d464
CW
1701 if (ppgtt->pd_dirty_engines & engine->mask) {
1702 unwind_mm = engine->mask;
1703 ppgtt->pd_dirty_engines &= ~unwind_mm;
b3ee09a4
CW
1704 hw_flags = MI_FORCE_RESTORE;
1705 }
8911a31c
CW
1706 }
1707
b3ee09a4 1708 if (rq->hw_context->state) {
8a68d464 1709 GEM_BUG_ON(engine->id != RCS0);
8911a31c
CW
1710
1711 /*
1712 * The kernel context(s) is treated as pure scratch and is not
1713 * expected to retain any state (as we sacrifice it during
1714 * suspend and on resume it may be corrupted). This is ok,
1715 * as nothing actually executes using the kernel context; it
1716 * is purely used for flushing user contexts.
1717 */
b3ee09a4 1718 if (i915_gem_context_is_kernel(ctx))
8911a31c
CW
1719 hw_flags = MI_RESTORE_INHIBIT;
1720
1721 ret = mi_set_context(rq, hw_flags);
1722 if (ret)
1723 goto err_mm;
8911a31c 1724 }
8911a31c 1725
e568ac38 1726 if (vm) {
06348d30
CW
1727 ret = engine->emit_flush(rq, EMIT_INVALIDATE);
1728 if (ret)
1729 goto err_mm;
1730
d9d117e4
CW
1731 ret = flush_pd_dir(rq);
1732 if (ret)
1733 goto err_mm;
06348d30
CW
1734
1735 /*
1736 * Not only do we need a full barrier (post-sync write) after
1737 * invalidating the TLBs, but we need to wait a little bit
1738 * longer. Whether this is merely delaying us, or the
1739 * subsequent flush is a key part of serialising with the
1740 * post-sync op, this extra pass appears vital before a
1741 * mm switch!
1742 */
1743 ret = engine->emit_flush(rq, EMIT_INVALIDATE);
1744 if (ret)
1745 goto err_mm;
1746
1747 ret = engine->emit_flush(rq, EMIT_FLUSH);
1748 if (ret)
1749 goto err_mm;
8911a31c
CW
1750 }
1751
b3ee09a4 1752 if (ctx->remap_slice) {
8911a31c 1753 for (i = 0; i < MAX_L3_SLICES; i++) {
b3ee09a4 1754 if (!(ctx->remap_slice & BIT(i)))
8911a31c
CW
1755 continue;
1756
1757 ret = remap_l3(rq, i);
1758 if (ret)
b3ee09a4 1759 goto err_mm;
8911a31c
CW
1760 }
1761
b3ee09a4 1762 ctx->remap_slice = 0;
8911a31c
CW
1763 }
1764
1765 return 0;
1766
8911a31c 1767err_mm:
b3ee09a4 1768 if (unwind_mm)
e568ac38 1769 i915_vm_to_ppgtt(vm)->pd_dirty_engines |= unwind_mm;
8911a31c
CW
1770err:
1771 return ret;
1772}
1773
e61e0f51 1774static int ring_request_alloc(struct i915_request *request)
9d773091 1775{
fd138212 1776 int ret;
6310346e 1777
08819549 1778 GEM_BUG_ON(!intel_context_is_pinned(request->hw_context));
85474441 1779 GEM_BUG_ON(request->timeline->has_initial_breadcrumb);
e8a9c58f 1780
5f5800a7
CW
1781 /*
1782 * Flush enough space to reduce the likelihood of waiting after
6310346e
CW
1783 * we start building the request - in which case we will just
1784 * have to repeat work.
1785 */
a0442461 1786 request->reserved_space += LEGACY_REQUEST_SIZE;
6310346e 1787
928f8f42
CW
1788 /* Unconditionally invalidate GPU caches and TLBs. */
1789 ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
fd138212
CW
1790 if (ret)
1791 return ret;
6310346e 1792
928f8f42 1793 ret = switch_context(request);
3fef5cda
CW
1794 if (ret)
1795 return ret;
1796
a0442461 1797 request->reserved_space -= LEGACY_REQUEST_SIZE;
6310346e 1798 return 0;
9d773091
CW
1799}
1800
fd138212 1801static noinline int wait_for_space(struct intel_ring *ring, unsigned int bytes)
987046ad 1802{
e61e0f51 1803 struct i915_request *target;
e95433c7
CW
1804 long timeout;
1805
95aebcb2 1806 if (intel_ring_update_space(ring) >= bytes)
987046ad
CW
1807 return 0;
1808
36620032 1809 GEM_BUG_ON(list_empty(&ring->request_list));
675d9ad7 1810 list_for_each_entry(target, &ring->request_list, ring_link) {
987046ad 1811 /* Would completion of this request free enough space? */
605d5b32
CW
1812 if (bytes <= __intel_ring_space(target->postfix,
1813 ring->emit, ring->size))
987046ad 1814 break;
79bbcc29 1815 }
29b1b415 1816
675d9ad7 1817 if (WARN_ON(&target->ring_link == &ring->request_list))
987046ad
CW
1818 return -ENOSPC;
1819
e61e0f51 1820 timeout = i915_request_wait(target,
e95433c7
CW
1821 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1822 MAX_SCHEDULE_TIMEOUT);
1823 if (timeout < 0)
1824 return timeout;
7da844c5 1825
e61e0f51 1826 i915_request_retire_upto(target);
7da844c5
CW
1827
1828 intel_ring_update_space(ring);
1829 GEM_BUG_ON(ring->space < bytes);
1830 return 0;
29b1b415
JH
1831}
1832
e61e0f51 1833u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
cbcc80df 1834{
e61e0f51 1835 struct intel_ring *ring = rq->ring;
5e5655c3
CW
1836 const unsigned int remain_usable = ring->effective_size - ring->emit;
1837 const unsigned int bytes = num_dwords * sizeof(u32);
1838 unsigned int need_wrap = 0;
1839 unsigned int total_bytes;
73dec95e 1840 u32 *cs;
29b1b415 1841
6492ca79
CW
1842 /* Packets must be qword aligned. */
1843 GEM_BUG_ON(num_dwords & 1);
1844
e61e0f51 1845 total_bytes = bytes + rq->reserved_space;
5e5655c3 1846 GEM_BUG_ON(total_bytes > ring->effective_size);
29b1b415 1847
5e5655c3
CW
1848 if (unlikely(total_bytes > remain_usable)) {
1849 const int remain_actual = ring->size - ring->emit;
1850
1851 if (bytes > remain_usable) {
1852 /*
1853 * Not enough space for the basic request. So need to
1854 * flush out the remainder and then wait for
1855 * base + reserved.
1856 */
1857 total_bytes += remain_actual;
1858 need_wrap = remain_actual | 1;
1859 } else {
1860 /*
1861 * The base request will fit but the reserved space
1862 * falls off the end. So we don't need an immediate
1863 * wrap and only need to effectively wait for the
1864 * reserved size from the start of ringbuffer.
1865 */
e61e0f51 1866 total_bytes = rq->reserved_space + remain_actual;
5e5655c3 1867 }
cbcc80df
MK
1868 }
1869
5e5655c3 1870 if (unlikely(total_bytes > ring->space)) {
fd138212
CW
1871 int ret;
1872
1873 /*
1874 * Space is reserved in the ringbuffer for finalising the
1875 * request, as that cannot be allowed to fail. During request
1876 * finalisation, reserved_space is set to 0 to stop the
1877 * overallocation and the assumption is that then we never need
1878 * to wait (which has the risk of failing with EINTR).
1879 *
e61e0f51 1880 * See also i915_request_alloc() and i915_request_add().
fd138212 1881 */
e61e0f51 1882 GEM_BUG_ON(!rq->reserved_space);
fd138212
CW
1883
1884 ret = wait_for_space(ring, total_bytes);
cbcc80df 1885 if (unlikely(ret))
73dec95e 1886 return ERR_PTR(ret);
cbcc80df
MK
1887 }
1888
987046ad 1889 if (unlikely(need_wrap)) {
5e5655c3
CW
1890 need_wrap &= ~1;
1891 GEM_BUG_ON(need_wrap > ring->space);
1892 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
46b86332 1893 GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
78501eac 1894
987046ad 1895 /* Fill the tail with MI_NOOP */
46b86332 1896 memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
5e5655c3 1897 ring->space -= need_wrap;
46b86332 1898 ring->emit = 0;
987046ad 1899 }
304d695c 1900
e6ba9992 1901 GEM_BUG_ON(ring->emit > ring->size - bytes);
605d5b32 1902 GEM_BUG_ON(ring->space < bytes);
e6ba9992 1903 cs = ring->vaddr + ring->emit;
46b86332 1904 GEM_DEBUG_EXEC(memset32(cs, POISON_INUSE, bytes / sizeof(*cs)));
e6ba9992 1905 ring->emit += bytes;
1dae2dfb 1906 ring->space -= bytes;
73dec95e
TU
1907
1908 return cs;
8187a2b7 1909}
78501eac 1910
753b1ad4 1911/* Align the ring tail to a cacheline boundary */
e61e0f51 1912int intel_ring_cacheline_align(struct i915_request *rq)
753b1ad4 1913{
1f177a13
CW
1914 int num_dwords;
1915 void *cs;
753b1ad4 1916
1f177a13 1917 num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
753b1ad4
VS
1918 if (num_dwords == 0)
1919 return 0;
1920
1f177a13
CW
1921 num_dwords = CACHELINE_DWORDS - num_dwords;
1922 GEM_BUG_ON(num_dwords & 1);
1923
e61e0f51 1924 cs = intel_ring_begin(rq, num_dwords);
73dec95e
TU
1925 if (IS_ERR(cs))
1926 return PTR_ERR(cs);
753b1ad4 1927
1f177a13 1928 memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
e61e0f51 1929 intel_ring_advance(rq, cs);
753b1ad4 1930
1f177a13 1931 GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));
753b1ad4
VS
1932 return 0;
1933}
1934
e61e0f51 1935static void gen6_bsd_submit_request(struct i915_request *request)
881f47b6 1936{
baba6e57 1937 struct intel_uncore *uncore = request->engine->uncore;
881f47b6 1938
d2d551c0 1939 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
76f8421f 1940
881f47b6 1941 /* Every tail move must follow the sequence below */
12f55818
CW
1942
1943 /* Disable notification that the ring is IDLE. The GT
1944 * will then assume that it is busy and bring it out of rc6.
1945 */
d2d551c0
DCS
1946 intel_uncore_write_fw(uncore, GEN6_BSD_SLEEP_PSMI_CONTROL,
1947 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
12f55818
CW
1948
1949 /* Clear the context id. Here be magic! */
d2d551c0 1950 intel_uncore_write64_fw(uncore, GEN6_BSD_RNCID, 0x0);
0206e353 1951
12f55818 1952 /* Wait for the ring not to be idle, i.e. for it to wake up. */
d2d551c0 1953 if (__intel_wait_for_register_fw(uncore,
02b312d0
CW
1954 GEN6_BSD_SLEEP_PSMI_CONTROL,
1955 GEN6_BSD_SLEEP_INDICATOR,
1956 0,
1957 1000, 0, NULL))
12f55818 1958 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
0206e353 1959
12f55818 1960 /* Now that the ring is fully powered up, update the tail */
b0411e7d 1961 i9xx_submit_request(request);
12f55818
CW
1962
1963 /* Let the ring send IDLE messages to the GT again,
1964 * and so let it sleep to conserve power when idle.
1965 */
d2d551c0
DCS
1966 intel_uncore_write_fw(uncore, GEN6_BSD_SLEEP_PSMI_CONTROL,
1967 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
76f8421f 1968
d2d551c0 1969 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
881f47b6
XH
1970}
1971
06348d30 1972static int mi_flush_dw(struct i915_request *rq, u32 flags)
881f47b6 1973{
73dec95e 1974 u32 cmd, *cs;
b72f3acb 1975
e61e0f51 1976 cs = intel_ring_begin(rq, 4);
73dec95e
TU
1977 if (IS_ERR(cs))
1978 return PTR_ERR(cs);
b72f3acb 1979
71a77e07 1980 cmd = MI_FLUSH_DW;
f0a1fb10 1981
70b73f9a
CW
1982 /*
1983 * We always require a command barrier so that subsequent
f0a1fb10
CW
1984 * commands, such as breadcrumb interrupts, are strictly ordered
1985 * wrt the contents of the write cache being flushed to memory
1986 * (and thus being coherent from the CPU).
1987 */
1988 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1989
9a289771 1990 /*
70b73f9a 1991 * Bspec vol 1c.3 - blitter engine command streamer:
9a289771
JB
1992 * "If ENABLED, all TLBs will be invalidated once the flush
1993 * operation is complete. This bit is only valid when the
1994 * Post-Sync Operation field is a value of 1h or 3h."
1995 */
70b73f9a 1996 cmd |= flags;
f0a1fb10 1997
73dec95e
TU
1998 *cs++ = cmd;
1999 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
79e6770c 2000 *cs++ = 0;
73dec95e 2001 *cs++ = MI_NOOP;
70b73f9a 2002
e61e0f51 2003 intel_ring_advance(rq, cs);
70b73f9a 2004
1c7a0623
BW
2005 return 0;
2006}
2007
70b73f9a
CW
2008static int gen6_flush_dw(struct i915_request *rq, u32 mode, u32 invflags)
2009{
06348d30 2010 return mi_flush_dw(rq, mode & EMIT_INVALIDATE ? invflags : 0);
70b73f9a
CW
2011}
2012
2013static int gen6_bsd_ring_flush(struct i915_request *rq, u32 mode)
2014{
2015 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB | MI_INVALIDATE_BSD);
2016}
2017
d7d4eedd 2018static int
e61e0f51 2019hsw_emit_bb_start(struct i915_request *rq,
803688ba
CW
2020 u64 offset, u32 len,
2021 unsigned int dispatch_flags)
d7d4eedd 2022{
73dec95e 2023 u32 *cs;
d7d4eedd 2024
e61e0f51 2025 cs = intel_ring_begin(rq, 2);
73dec95e
TU
2026 if (IS_ERR(cs))
2027 return PTR_ERR(cs);
d7d4eedd 2028
73dec95e 2029 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
08e3e21a 2030 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW);
d7d4eedd 2031 /* bit0-7 is the length on GEN6+ */
73dec95e 2032 *cs++ = offset;
e61e0f51 2033 intel_ring_advance(rq, cs);
d7d4eedd
CW
2034
2035 return 0;
2036}
2037
881f47b6 2038static int
e61e0f51 2039gen6_emit_bb_start(struct i915_request *rq,
803688ba
CW
2040 u64 offset, u32 len,
2041 unsigned int dispatch_flags)
881f47b6 2042{
73dec95e 2043 u32 *cs;
ab6f8e32 2044
e61e0f51 2045 cs = intel_ring_begin(rq, 2);
73dec95e
TU
2046 if (IS_ERR(cs))
2047 return PTR_ERR(cs);
e1f99ce6 2048
73dec95e
TU
2049 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
2050 0 : MI_BATCH_NON_SECURE_I965);
0206e353 2051 /* bit0-7 is the length on GEN6+ */
73dec95e 2052 *cs++ = offset;
e61e0f51 2053 intel_ring_advance(rq, cs);
ab6f8e32 2054
0206e353 2055 return 0;
881f47b6
XH
2056}
2057
549f7365
CW
2058/* Blitter support (SandyBridge+) */
2059
e61e0f51 2060static int gen6_ring_flush(struct i915_request *rq, u32 mode)
8d19215b 2061{
70b73f9a 2062 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB);
8d19215b
ZN
2063}
2064
ff44ad51
CW
2065static void i9xx_set_default_submission(struct intel_engine_cs *engine)
2066{
2067 engine->submit_request = i9xx_submit_request;
27a5f61b 2068 engine->cancel_requests = cancel_requests;
aba5e278
CW
2069
2070 engine->park = NULL;
2071 engine->unpark = NULL;
ff44ad51
CW
2072}
2073
2074static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
2075{
aba5e278 2076 i9xx_set_default_submission(engine);
ff44ad51
CW
2077 engine->submit_request = gen6_bsd_submit_request;
2078}
2079
45b9c968
CW
2080static void ring_destroy(struct intel_engine_cs *engine)
2081{
2082 struct drm_i915_private *dev_priv = engine->i915;
2083
2084 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
2085 (ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0);
2086
2087 intel_ring_unpin(engine->buffer);
2088 intel_ring_put(engine->buffer);
2089
2090 intel_engine_cleanup_common(engine);
2091 kfree(engine);
2092}
2093
11334c6a
CW
2094static void setup_irq(struct intel_engine_cs *engine)
2095{
2096 struct drm_i915_private *i915 = engine->i915;
2097
2098 if (INTEL_GEN(i915) >= 6) {
2099 engine->irq_enable = gen6_irq_enable;
2100 engine->irq_disable = gen6_irq_disable;
2101 } else if (INTEL_GEN(i915) >= 5) {
2102 engine->irq_enable = gen5_irq_enable;
2103 engine->irq_disable = gen5_irq_disable;
2104 } else if (INTEL_GEN(i915) >= 3) {
2105 engine->irq_enable = i9xx_irq_enable;
2106 engine->irq_disable = i9xx_irq_disable;
2107 } else {
2108 engine->irq_enable = i8xx_irq_enable;
2109 engine->irq_disable = i8xx_irq_disable;
2110 }
2111}
2112
2113static void setup_common(struct intel_engine_cs *engine)
06a2fe22 2114{
11334c6a
CW
2115 struct drm_i915_private *i915 = engine->i915;
2116
79e6770c 2117 /* gen8+ are only supported with execlists */
11334c6a 2118 GEM_BUG_ON(INTEL_GEN(i915) >= 8);
79e6770c 2119
11334c6a 2120 setup_irq(engine);
618e4ca7 2121
45b9c968
CW
2122 engine->destroy = ring_destroy;
2123
79ffac85 2124 engine->resume = xcs_resume;
5adfb772
CW
2125 engine->reset.prepare = reset_prepare;
2126 engine->reset.reset = reset_ring;
2127 engine->reset.finish = reset_finish;
7445a2a4 2128
4dc84b77 2129 engine->cops = &ring_context_ops;
f73e7399
CW
2130 engine->request_alloc = ring_request_alloc;
2131
85474441
CW
2132 /*
2133 * Using a global execution timeline; the previous final breadcrumb is
2134 * equivalent to our next initial bread so we can elide
2135 * engine->emit_init_breadcrumb().
2136 */
2137 engine->emit_fini_breadcrumb = i9xx_emit_breadcrumb;
11334c6a 2138 if (IS_GEN(i915, 5))
85474441 2139 engine->emit_fini_breadcrumb = gen5_emit_breadcrumb;
ff44ad51
CW
2140
2141 engine->set_default_submission = i9xx_set_default_submission;
6f7bef75 2142
11334c6a 2143 if (INTEL_GEN(i915) >= 6)
803688ba 2144 engine->emit_bb_start = gen6_emit_bb_start;
11334c6a 2145 else if (INTEL_GEN(i915) >= 4)
803688ba 2146 engine->emit_bb_start = i965_emit_bb_start;
11334c6a 2147 else if (IS_I830(i915) || IS_I845G(i915))
803688ba 2148 engine->emit_bb_start = i830_emit_bb_start;
6f7bef75 2149 else
803688ba 2150 engine->emit_bb_start = i915_emit_bb_start;
06a2fe22
TU
2151}
2152
11334c6a 2153static void setup_rcs(struct intel_engine_cs *engine)
5c1143bb 2154{
11334c6a 2155 struct drm_i915_private *i915 = engine->i915;
06a2fe22 2156
11334c6a 2157 if (HAS_L3_DPF(i915))
61ff75ac 2158 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
f8973c21 2159
fa6f071d
DCS
2160 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2161
11334c6a 2162 if (INTEL_GEN(i915) >= 7) {
e2f80391 2163 engine->init_context = intel_rcs_ctx_init;
c7fe7d25 2164 engine->emit_flush = gen7_render_ring_flush;
85474441 2165 engine->emit_fini_breadcrumb = gen7_rcs_emit_breadcrumb;
11334c6a 2166 } else if (IS_GEN(i915, 6)) {
caa5915b
CW
2167 engine->init_context = intel_rcs_ctx_init;
2168 engine->emit_flush = gen6_render_ring_flush;
85474441 2169 engine->emit_fini_breadcrumb = gen6_rcs_emit_breadcrumb;
11334c6a 2170 } else if (IS_GEN(i915, 5)) {
c7fe7d25 2171 engine->emit_flush = gen4_render_ring_flush;
59465b5f 2172 } else {
11334c6a 2173 if (INTEL_GEN(i915) < 4)
c7fe7d25 2174 engine->emit_flush = gen2_render_ring_flush;
46f0f8d1 2175 else
c7fe7d25 2176 engine->emit_flush = gen4_render_ring_flush;
e2f80391 2177 engine->irq_enable_mask = I915_USER_INTERRUPT;
1ec14ad3 2178 }
707d9cf9 2179
11334c6a 2180 if (IS_HASWELL(i915))
803688ba 2181 engine->emit_bb_start = hsw_emit_bb_start;
6f7bef75 2182
79ffac85 2183 engine->resume = rcs_resume;
5c1143bb
XH
2184}
2185
11334c6a 2186static void setup_vcs(struct intel_engine_cs *engine)
5c1143bb 2187{
11334c6a 2188 struct drm_i915_private *i915 = engine->i915;
06a2fe22 2189
11334c6a 2190 if (INTEL_GEN(i915) >= 6) {
0fd2c201 2191 /* gen6 bsd needs a special wa for tail updates */
11334c6a 2192 if (IS_GEN(i915, 6))
ff44ad51 2193 engine->set_default_submission = gen6_bsd_set_default_submission;
c7fe7d25 2194 engine->emit_flush = gen6_bsd_ring_flush;
79e6770c 2195 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
caa5915b 2196
11334c6a 2197 if (IS_GEN(i915, 6))
85474441 2198 engine->emit_fini_breadcrumb = gen6_xcs_emit_breadcrumb;
9fa4973e 2199 else
85474441 2200 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
58fa3835 2201 } else {
c7fe7d25 2202 engine->emit_flush = bsd_ring_flush;
11334c6a 2203 if (IS_GEN(i915, 5))
e2f80391 2204 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
8d228911 2205 else
e2f80391 2206 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
58fa3835 2207 }
5c1143bb 2208}
549f7365 2209
11334c6a 2210static void setup_bcs(struct intel_engine_cs *engine)
549f7365 2211{
11334c6a 2212 struct drm_i915_private *i915 = engine->i915;
06a2fe22 2213
c7fe7d25 2214 engine->emit_flush = gen6_ring_flush;
79e6770c 2215 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
549f7365 2216
11334c6a 2217 if (IS_GEN(i915, 6))
85474441 2218 engine->emit_fini_breadcrumb = gen6_xcs_emit_breadcrumb;
9fa4973e 2219 else
85474441 2220 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
549f7365 2221}
a7b9761d 2222
11334c6a 2223static void setup_vecs(struct intel_engine_cs *engine)
9a8a2213 2224{
11334c6a 2225 struct drm_i915_private *i915 = engine->i915;
caa5915b 2226
11334c6a 2227 GEM_BUG_ON(INTEL_GEN(i915) < 7);
06a2fe22 2228
c7fe7d25 2229 engine->emit_flush = gen6_ring_flush;
79e6770c
CW
2230 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2231 engine->irq_enable = hsw_vebox_irq_enable;
2232 engine->irq_disable = hsw_vebox_irq_disable;
9a8a2213 2233
85474441 2234 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
11334c6a
CW
2235}
2236
2237int intel_ring_submission_setup(struct intel_engine_cs *engine)
2238{
2239 setup_common(engine);
2240
2241 switch (engine->class) {
2242 case RENDER_CLASS:
2243 setup_rcs(engine);
2244 break;
2245 case VIDEO_DECODE_CLASS:
2246 setup_vcs(engine);
2247 break;
2248 case COPY_ENGINE_CLASS:
2249 setup_bcs(engine);
2250 break;
2251 case VIDEO_ENHANCEMENT_CLASS:
2252 setup_vecs(engine);
2253 break;
2254 default:
2255 MISSING_CASE(engine->class);
2256 return -ENODEV;
2257 }
2258
2259 return 0;
2260}
2261
2262int intel_ring_submission_init(struct intel_engine_cs *engine)
2263{
2264 struct i915_timeline *timeline;
2265 struct intel_ring *ring;
2266 int err;
2267
2268 timeline = i915_timeline_create(engine->i915, engine->status_page.vma);
2269 if (IS_ERR(timeline)) {
2270 err = PTR_ERR(timeline);
2271 goto err;
2272 }
2273 GEM_BUG_ON(timeline->has_initial_breadcrumb);
2274
2275 ring = intel_engine_create_ring(engine, timeline, 32 * PAGE_SIZE);
2276 i915_timeline_put(timeline);
2277 if (IS_ERR(ring)) {
2278 err = PTR_ERR(ring);
2279 goto err;
2280 }
2281
2282 err = intel_ring_pin(ring);
2283 if (err)
2284 goto err_ring;
caa5915b 2285
11334c6a
CW
2286 GEM_BUG_ON(engine->buffer);
2287 engine->buffer = ring;
2288
2289 err = intel_engine_init_common(engine);
2290 if (err)
2291 goto err_unpin;
2292
2293 GEM_BUG_ON(ring->timeline->hwsp_ggtt != engine->status_page.vma);
2294
2295 return 0;
2296
2297err_unpin:
2298 intel_ring_unpin(ring);
2299err_ring:
2300 intel_ring_put(ring);
2301err:
2302 intel_engine_cleanup_common(engine);
2303 return err;
9a8a2213 2304}