]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/gpu/drm/i915/gt/intel_ringbuffer.c
drm/i915: Pull kref into i915_address_space
[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{
c033666a 979 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 980
31bb59cc 981 dev_priv->irq_mask &= ~engine->irq_enable_mask;
9d9523d8 982 I915_WRITE16(GEN2_IMR, dev_priv->irq_mask);
31bb59cc 983 POSTING_READ16(RING_IMR(engine->mmio_base));
c2798b19
CW
984}
985
986static void
31bb59cc 987i8xx_irq_disable(struct intel_engine_cs *engine)
c2798b19 988{
c033666a 989 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 990
31bb59cc 991 dev_priv->irq_mask |= engine->irq_enable_mask;
9d9523d8 992 I915_WRITE16(GEN2_IMR, dev_priv->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
1fc44d9b 1352static int __context_pin(struct intel_context *ce)
e8a9c58f 1353{
d901e8e6
CW
1354 struct i915_vma *vma;
1355 int err;
1356
1357 vma = ce->state;
1358 if (!vma)
1359 return 0;
e8a9c58f 1360
7a859c65 1361 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
d901e8e6
CW
1362 if (err)
1363 return err;
1364
1365 /*
1366 * And mark is as a globally pinned object to let the shrinker know
1367 * it cannot reclaim the object until we release it.
1368 */
1369 vma->obj->pin_global++;
a679f58d 1370 vma->obj->mm.dirty = true;
d901e8e6
CW
1371
1372 return 0;
1373}
1374
1375static void __context_unpin(struct intel_context *ce)
1376{
1377 struct i915_vma *vma;
1378
1379 vma = ce->state;
1380 if (!vma)
1381 return;
1382
1383 vma->obj->pin_global--;
1384 i915_vma_unpin(vma);
1385}
1386
4dc84b77 1387static void ring_context_unpin(struct intel_context *ce)
d901e8e6 1388{
a2bbf714 1389 __context_unpin_ppgtt(ce->gem_context);
d901e8e6 1390 __context_unpin(ce);
e8a9c58f
CW
1391}
1392
3204c343
CW
1393static struct i915_vma *
1394alloc_context_vma(struct intel_engine_cs *engine)
1395{
1396 struct drm_i915_private *i915 = engine->i915;
1397 struct drm_i915_gem_object *obj;
1398 struct i915_vma *vma;
d2b4b979 1399 int err;
3204c343 1400
8475355f 1401 obj = i915_gem_object_create_shmem(i915, engine->context_size);
3204c343
CW
1402 if (IS_ERR(obj))
1403 return ERR_CAST(obj);
1404
a679f58d
CW
1405 /*
1406 * Try to make the context utilize L3 as well as LLC.
1407 *
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.
1412 *
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.
1416 *
1417 * This is only applicable for Ivy Bridge devices since
1418 * later platforms don't have L3 control bits in the PTE.
1419 */
1420 if (IS_IVYBRIDGE(i915))
1421 i915_gem_object_set_cache_coherency(obj, I915_CACHE_L3_LLC);
1422
d2b4b979
CW
1423 if (engine->default_state) {
1424 void *defaults, *vaddr;
1425
1426 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1427 if (IS_ERR(vaddr)) {
1428 err = PTR_ERR(vaddr);
1429 goto err_obj;
1430 }
1431
1432 defaults = i915_gem_object_pin_map(engine->default_state,
1433 I915_MAP_WB);
1434 if (IS_ERR(defaults)) {
1435 err = PTR_ERR(defaults);
1436 goto err_map;
1437 }
1438
1439 memcpy(vaddr, defaults, engine->context_size);
d2b4b979 1440 i915_gem_object_unpin_map(engine->default_state);
d2b4b979 1441
a679f58d
CW
1442 i915_gem_object_flush_map(obj);
1443 i915_gem_object_unpin_map(obj);
3204c343
CW
1444 }
1445
82ad6443 1446 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
d2b4b979
CW
1447 if (IS_ERR(vma)) {
1448 err = PTR_ERR(vma);
1449 goto err_obj;
1450 }
3204c343
CW
1451
1452 return vma;
d2b4b979
CW
1453
1454err_map:
1455 i915_gem_object_unpin_map(obj);
1456err_obj:
1457 i915_gem_object_put(obj);
1458 return ERR_PTR(err);
3204c343
CW
1459}
1460
95f697eb 1461static int ring_context_pin(struct intel_context *ce)
0cb26a8e 1462{
95f697eb 1463 struct intel_engine_cs *engine = ce->engine;
1fc44d9b 1464 int err;
0cb26a8e 1465
7e3d9a59
CW
1466 /* One ringbuffer to rule them all */
1467 GEM_BUG_ON(!engine->buffer);
1468 ce->ring = engine->buffer;
1469
63ffbcda 1470 if (!ce->state && engine->context_size) {
3204c343
CW
1471 struct i915_vma *vma;
1472
1473 vma = alloc_context_vma(engine);
95f697eb
CW
1474 if (IS_ERR(vma))
1475 return PTR_ERR(vma);
3204c343
CW
1476
1477 ce->state = vma;
1478 }
1479
d901e8e6
CW
1480 err = __context_pin(ce);
1481 if (err)
95f697eb 1482 return err;
0cb26a8e 1483
a2bbf714
CW
1484 err = __context_pin_ppgtt(ce->gem_context);
1485 if (err)
1486 goto err_unpin;
1487
95f697eb 1488 return 0;
266a240b 1489
a2bbf714
CW
1490err_unpin:
1491 __context_unpin(ce);
95f697eb 1492 return err;
0cb26a8e
CW
1493}
1494
9726920b
CW
1495static void ring_context_reset(struct intel_context *ce)
1496{
1497 intel_ring_reset(ce->ring, 0);
1498}
1499
4dc84b77 1500static const struct intel_context_ops ring_context_ops = {
95f697eb 1501 .pin = ring_context_pin,
4dc84b77 1502 .unpin = ring_context_unpin,
9726920b 1503
6eee33e8
CW
1504 .enter = intel_context_enter_engine,
1505 .exit = intel_context_exit_engine,
1506
9726920b 1507 .reset = ring_context_reset,
4dc84b77
CW
1508 .destroy = ring_context_destroy,
1509};
1510
b3ee09a4
CW
1511static int load_pd_dir(struct i915_request *rq,
1512 const struct i915_hw_ppgtt *ppgtt)
1513{
1514 const struct intel_engine_cs * const engine = rq->engine;
1515 u32 *cs;
1516
1517 cs = intel_ring_begin(rq, 6);
1518 if (IS_ERR(cs))
1519 return PTR_ERR(cs);
1520
1521 *cs++ = MI_LOAD_REGISTER_IMM(1);
baba6e57 1522 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine->mmio_base));
b3ee09a4
CW
1523 *cs++ = PP_DIR_DCLV_2G;
1524
1525 *cs++ = MI_LOAD_REGISTER_IMM(1);
baba6e57 1526 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base));
b3ee09a4
CW
1527 *cs++ = ppgtt->pd.base.ggtt_offset << 10;
1528
1529 intel_ring_advance(rq, cs);
1530
1531 return 0;
1532}
1533
d9d117e4
CW
1534static int flush_pd_dir(struct i915_request *rq)
1535{
1536 const struct intel_engine_cs * const engine = rq->engine;
1537 u32 *cs;
1538
1539 cs = intel_ring_begin(rq, 4);
1540 if (IS_ERR(cs))
1541 return PTR_ERR(cs);
1542
1543 /* Stall until the page table load is complete */
1544 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
baba6e57 1545 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine->mmio_base));
51797499 1546 *cs++ = i915_scratch_offset(rq->i915);
d9d117e4
CW
1547 *cs++ = MI_NOOP;
1548
1549 intel_ring_advance(rq, cs);
1550 return 0;
1551}
1552
e61e0f51 1553static inline int mi_set_context(struct i915_request *rq, u32 flags)
8911a31c
CW
1554{
1555 struct drm_i915_private *i915 = rq->i915;
1556 struct intel_engine_cs *engine = rq->engine;
1557 enum intel_engine_id id;
8a68d464
CW
1558 const int num_engines =
1559 IS_HSW_GT1(i915) ? RUNTIME_INFO(i915)->num_engines - 1 : 0;
1fc719d1 1560 bool force_restore = false;
8911a31c
CW
1561 int len;
1562 u32 *cs;
1563
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;
1568 else
1215d28e 1569 /* We need to save the extended state for powersaving modes */
8911a31c
CW
1570 flags |= MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN;
1571
1572 len = 4;
cf819eff 1573 if (IS_GEN(i915, 7))
8a68d464 1574 len += 2 + (num_engines ? 4 * num_engines + 6 : 0);
1215d28e
CW
1575 else if (IS_GEN(i915, 5))
1576 len += 2;
1fc719d1
CW
1577 if (flags & MI_FORCE_RESTORE) {
1578 GEM_BUG_ON(flags & MI_RESTORE_INHIBIT);
1579 flags &= ~MI_FORCE_RESTORE;
1580 force_restore = true;
1581 len += 2;
1582 }
8911a31c
CW
1583
1584 cs = intel_ring_begin(rq, len);
1585 if (IS_ERR(cs))
1586 return PTR_ERR(cs);
1587
1588 /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
cf819eff 1589 if (IS_GEN(i915, 7)) {
8911a31c 1590 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
8a68d464 1591 if (num_engines) {
8911a31c
CW
1592 struct intel_engine_cs *signaller;
1593
8a68d464 1594 *cs++ = MI_LOAD_REGISTER_IMM(num_engines);
8911a31c
CW
1595 for_each_engine(signaller, i915, id) {
1596 if (signaller == engine)
1597 continue;
1598
1599 *cs++ = i915_mmio_reg_offset(
1600 RING_PSMI_CTL(signaller->mmio_base));
1601 *cs++ = _MASKED_BIT_ENABLE(
1602 GEN6_PSMI_SLEEP_MSG_DISABLE);
1603 }
1604 }
1215d28e
CW
1605 } else if (IS_GEN(i915, 5)) {
1606 /*
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!
1611 */
1612 *cs++ = MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN;
8911a31c
CW
1613 }
1614
1fc719d1
CW
1615 if (force_restore) {
1616 /*
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.
1622 *
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.
1627 */
1628 *cs++ = MI_SET_CONTEXT;
9dbfea98 1629 *cs++ = i915_ggtt_offset(engine->kernel_context->state) |
1fc719d1
CW
1630 MI_MM_SPACE_GTT |
1631 MI_RESTORE_INHIBIT;
1632 }
1633
8911a31c
CW
1634 *cs++ = MI_NOOP;
1635 *cs++ = MI_SET_CONTEXT;
1fc44d9b 1636 *cs++ = i915_ggtt_offset(rq->hw_context->state) | flags;
8911a31c
CW
1637 /*
1638 * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
1639 * WaMiSetContext_Hang:snb,ivb,vlv
1640 */
1641 *cs++ = MI_NOOP;
1642
cf819eff 1643 if (IS_GEN(i915, 7)) {
8a68d464 1644 if (num_engines) {
8911a31c
CW
1645 struct intel_engine_cs *signaller;
1646 i915_reg_t last_reg = {}; /* keep gcc quiet */
1647
8a68d464 1648 *cs++ = MI_LOAD_REGISTER_IMM(num_engines);
8911a31c
CW
1649 for_each_engine(signaller, i915, id) {
1650 if (signaller == engine)
1651 continue;
1652
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);
1657 }
1658
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);
51797499 1662 *cs++ = i915_scratch_offset(rq->i915);
8911a31c
CW
1663 *cs++ = MI_NOOP;
1664 }
1665 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1215d28e
CW
1666 } else if (IS_GEN(i915, 5)) {
1667 *cs++ = MI_SUSPEND_FLUSH;
8911a31c
CW
1668 }
1669
1670 intel_ring_advance(rq, cs);
1671
1672 return 0;
1673}
1674
e61e0f51 1675static int remap_l3(struct i915_request *rq, int slice)
8911a31c
CW
1676{
1677 u32 *cs, *remap_info = rq->i915->l3_parity.remap_info[slice];
1678 int i;
1679
1680 if (!remap_info)
1681 return 0;
1682
1683 cs = intel_ring_begin(rq, GEN7_L3LOG_SIZE/4 * 2 + 2);
1684 if (IS_ERR(cs))
1685 return PTR_ERR(cs);
1686
1687 /*
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.
1691 */
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];
1696 }
1697 *cs++ = MI_NOOP;
1698 intel_ring_advance(rq, cs);
1699
1700 return 0;
1701}
1702
e61e0f51 1703static int switch_context(struct i915_request *rq)
8911a31c
CW
1704{
1705 struct intel_engine_cs *engine = rq->engine;
b3ee09a4 1706 struct i915_gem_context *ctx = rq->gem_context;
e568ac38
CW
1707 struct i915_address_space *vm =
1708 ctx->vm ?: &rq->i915->mm.aliasing_ppgtt->vm;
b3ee09a4 1709 unsigned int unwind_mm = 0;
8911a31c
CW
1710 u32 hw_flags = 0;
1711 int ret, i;
1712
8911a31c
CW
1713 GEM_BUG_ON(HAS_EXECLISTS(rq->i915));
1714
e568ac38
CW
1715 if (vm) {
1716 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
e2a13d1b
CW
1717 int loops;
1718
1719 /*
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
1726 * explanation.
1727 */
1728 loops = 1;
8a68d464 1729 if (engine->id == BCS0 && IS_VALLEYVIEW(engine->i915))
e2a13d1b
CW
1730 loops = 32;
1731
1732 do {
1733 ret = load_pd_dir(rq, ppgtt);
1734 if (ret)
1735 goto err;
1736 } while (--loops);
8911a31c 1737
8a68d464
CW
1738 if (ppgtt->pd_dirty_engines & engine->mask) {
1739 unwind_mm = engine->mask;
1740 ppgtt->pd_dirty_engines &= ~unwind_mm;
b3ee09a4
CW
1741 hw_flags = MI_FORCE_RESTORE;
1742 }
8911a31c
CW
1743 }
1744
b3ee09a4 1745 if (rq->hw_context->state) {
8a68d464 1746 GEM_BUG_ON(engine->id != RCS0);
8911a31c
CW
1747
1748 /*
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.
1754 */
b3ee09a4 1755 if (i915_gem_context_is_kernel(ctx))
8911a31c
CW
1756 hw_flags = MI_RESTORE_INHIBIT;
1757
1758 ret = mi_set_context(rq, hw_flags);
1759 if (ret)
1760 goto err_mm;
8911a31c 1761 }
8911a31c 1762
e568ac38 1763 if (vm) {
06348d30
CW
1764 ret = engine->emit_flush(rq, EMIT_INVALIDATE);
1765 if (ret)
1766 goto err_mm;
1767
d9d117e4
CW
1768 ret = flush_pd_dir(rq);
1769 if (ret)
1770 goto err_mm;
06348d30
CW
1771
1772 /*
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
1778 * mm switch!
1779 */
1780 ret = engine->emit_flush(rq, EMIT_INVALIDATE);
1781 if (ret)
1782 goto err_mm;
1783
1784 ret = engine->emit_flush(rq, EMIT_FLUSH);
1785 if (ret)
1786 goto err_mm;
8911a31c
CW
1787 }
1788
b3ee09a4 1789 if (ctx->remap_slice) {
8911a31c 1790 for (i = 0; i < MAX_L3_SLICES; i++) {
b3ee09a4 1791 if (!(ctx->remap_slice & BIT(i)))
8911a31c
CW
1792 continue;
1793
1794 ret = remap_l3(rq, i);
1795 if (ret)
b3ee09a4 1796 goto err_mm;
8911a31c
CW
1797 }
1798
b3ee09a4 1799 ctx->remap_slice = 0;
8911a31c
CW
1800 }
1801
1802 return 0;
1803
8911a31c 1804err_mm:
b3ee09a4 1805 if (unwind_mm)
e568ac38 1806 i915_vm_to_ppgtt(vm)->pd_dirty_engines |= unwind_mm;
8911a31c
CW
1807err:
1808 return ret;
1809}
1810
e61e0f51 1811static int ring_request_alloc(struct i915_request *request)
9d773091 1812{
fd138212 1813 int ret;
6310346e 1814
08819549 1815 GEM_BUG_ON(!intel_context_is_pinned(request->hw_context));
85474441 1816 GEM_BUG_ON(request->timeline->has_initial_breadcrumb);
e8a9c58f 1817
5f5800a7
CW
1818 /*
1819 * Flush enough space to reduce the likelihood of waiting after
6310346e
CW
1820 * we start building the request - in which case we will just
1821 * have to repeat work.
1822 */
a0442461 1823 request->reserved_space += LEGACY_REQUEST_SIZE;
6310346e 1824
928f8f42
CW
1825 /* Unconditionally invalidate GPU caches and TLBs. */
1826 ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
fd138212
CW
1827 if (ret)
1828 return ret;
6310346e 1829
928f8f42 1830 ret = switch_context(request);
3fef5cda
CW
1831 if (ret)
1832 return ret;
1833
a0442461 1834 request->reserved_space -= LEGACY_REQUEST_SIZE;
6310346e 1835 return 0;
9d773091
CW
1836}
1837
fd138212 1838static noinline int wait_for_space(struct intel_ring *ring, unsigned int bytes)
987046ad 1839{
e61e0f51 1840 struct i915_request *target;
e95433c7
CW
1841 long timeout;
1842
95aebcb2 1843 if (intel_ring_update_space(ring) >= bytes)
987046ad
CW
1844 return 0;
1845
36620032 1846 GEM_BUG_ON(list_empty(&ring->request_list));
675d9ad7 1847 list_for_each_entry(target, &ring->request_list, ring_link) {
987046ad 1848 /* Would completion of this request free enough space? */
605d5b32
CW
1849 if (bytes <= __intel_ring_space(target->postfix,
1850 ring->emit, ring->size))
987046ad 1851 break;
79bbcc29 1852 }
29b1b415 1853
675d9ad7 1854 if (WARN_ON(&target->ring_link == &ring->request_list))
987046ad
CW
1855 return -ENOSPC;
1856
e61e0f51 1857 timeout = i915_request_wait(target,
e95433c7
CW
1858 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1859 MAX_SCHEDULE_TIMEOUT);
1860 if (timeout < 0)
1861 return timeout;
7da844c5 1862
e61e0f51 1863 i915_request_retire_upto(target);
7da844c5
CW
1864
1865 intel_ring_update_space(ring);
1866 GEM_BUG_ON(ring->space < bytes);
1867 return 0;
29b1b415
JH
1868}
1869
e61e0f51 1870u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
cbcc80df 1871{
e61e0f51 1872 struct intel_ring *ring = rq->ring;
5e5655c3
CW
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;
73dec95e 1877 u32 *cs;
29b1b415 1878
6492ca79
CW
1879 /* Packets must be qword aligned. */
1880 GEM_BUG_ON(num_dwords & 1);
1881
e61e0f51 1882 total_bytes = bytes + rq->reserved_space;
5e5655c3 1883 GEM_BUG_ON(total_bytes > ring->effective_size);
29b1b415 1884
5e5655c3
CW
1885 if (unlikely(total_bytes > remain_usable)) {
1886 const int remain_actual = ring->size - ring->emit;
1887
1888 if (bytes > remain_usable) {
1889 /*
1890 * Not enough space for the basic request. So need to
1891 * flush out the remainder and then wait for
1892 * base + reserved.
1893 */
1894 total_bytes += remain_actual;
1895 need_wrap = remain_actual | 1;
1896 } else {
1897 /*
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.
1902 */
e61e0f51 1903 total_bytes = rq->reserved_space + remain_actual;
5e5655c3 1904 }
cbcc80df
MK
1905 }
1906
5e5655c3 1907 if (unlikely(total_bytes > ring->space)) {
fd138212
CW
1908 int ret;
1909
1910 /*
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).
1916 *
e61e0f51 1917 * See also i915_request_alloc() and i915_request_add().
fd138212 1918 */
e61e0f51 1919 GEM_BUG_ON(!rq->reserved_space);
fd138212
CW
1920
1921 ret = wait_for_space(ring, total_bytes);
cbcc80df 1922 if (unlikely(ret))
73dec95e 1923 return ERR_PTR(ret);
cbcc80df
MK
1924 }
1925
987046ad 1926 if (unlikely(need_wrap)) {
5e5655c3
CW
1927 need_wrap &= ~1;
1928 GEM_BUG_ON(need_wrap > ring->space);
1929 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
46b86332 1930 GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
78501eac 1931
987046ad 1932 /* Fill the tail with MI_NOOP */
46b86332 1933 memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
5e5655c3 1934 ring->space -= need_wrap;
46b86332 1935 ring->emit = 0;
987046ad 1936 }
304d695c 1937
e6ba9992 1938 GEM_BUG_ON(ring->emit > ring->size - bytes);
605d5b32 1939 GEM_BUG_ON(ring->space < bytes);
e6ba9992 1940 cs = ring->vaddr + ring->emit;
46b86332 1941 GEM_DEBUG_EXEC(memset32(cs, POISON_INUSE, bytes / sizeof(*cs)));
e6ba9992 1942 ring->emit += bytes;
1dae2dfb 1943 ring->space -= bytes;
73dec95e
TU
1944
1945 return cs;
8187a2b7 1946}
78501eac 1947
753b1ad4 1948/* Align the ring tail to a cacheline boundary */
e61e0f51 1949int intel_ring_cacheline_align(struct i915_request *rq)
753b1ad4 1950{
1f177a13
CW
1951 int num_dwords;
1952 void *cs;
753b1ad4 1953
1f177a13 1954 num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
753b1ad4
VS
1955 if (num_dwords == 0)
1956 return 0;
1957
1f177a13
CW
1958 num_dwords = CACHELINE_DWORDS - num_dwords;
1959 GEM_BUG_ON(num_dwords & 1);
1960
e61e0f51 1961 cs = intel_ring_begin(rq, num_dwords);
73dec95e
TU
1962 if (IS_ERR(cs))
1963 return PTR_ERR(cs);
753b1ad4 1964
1f177a13 1965 memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
e61e0f51 1966 intel_ring_advance(rq, cs);
753b1ad4 1967
1f177a13 1968 GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));
753b1ad4
VS
1969 return 0;
1970}
1971
e61e0f51 1972static void gen6_bsd_submit_request(struct i915_request *request)
881f47b6 1973{
baba6e57 1974 struct intel_uncore *uncore = request->engine->uncore;
881f47b6 1975
d2d551c0 1976 intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
76f8421f 1977
881f47b6 1978 /* Every tail move must follow the sequence below */
12f55818
CW
1979
1980 /* Disable notification that the ring is IDLE. The GT
1981 * will then assume that it is busy and bring it out of rc6.
1982 */
d2d551c0
DCS
1983 intel_uncore_write_fw(uncore, GEN6_BSD_SLEEP_PSMI_CONTROL,
1984 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
12f55818
CW
1985
1986 /* Clear the context id. Here be magic! */
d2d551c0 1987 intel_uncore_write64_fw(uncore, GEN6_BSD_RNCID, 0x0);
0206e353 1988
12f55818 1989 /* Wait for the ring not to be idle, i.e. for it to wake up. */
d2d551c0 1990 if (__intel_wait_for_register_fw(uncore,
02b312d0
CW
1991 GEN6_BSD_SLEEP_PSMI_CONTROL,
1992 GEN6_BSD_SLEEP_INDICATOR,
1993 0,
1994 1000, 0, NULL))
12f55818 1995 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
0206e353 1996
12f55818 1997 /* Now that the ring is fully powered up, update the tail */
b0411e7d 1998 i9xx_submit_request(request);
12f55818
CW
1999
2000 /* Let the ring send IDLE messages to the GT again,
2001 * and so let it sleep to conserve power when idle.
2002 */
d2d551c0
DCS
2003 intel_uncore_write_fw(uncore, GEN6_BSD_SLEEP_PSMI_CONTROL,
2004 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
76f8421f 2005
d2d551c0 2006 intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
881f47b6
XH
2007}
2008
06348d30 2009static int mi_flush_dw(struct i915_request *rq, u32 flags)
881f47b6 2010{
73dec95e 2011 u32 cmd, *cs;
b72f3acb 2012
e61e0f51 2013 cs = intel_ring_begin(rq, 4);
73dec95e
TU
2014 if (IS_ERR(cs))
2015 return PTR_ERR(cs);
b72f3acb 2016
71a77e07 2017 cmd = MI_FLUSH_DW;
f0a1fb10 2018
70b73f9a
CW
2019 /*
2020 * We always require a command barrier so that subsequent
f0a1fb10
CW
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).
2024 */
2025 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2026
9a289771 2027 /*
70b73f9a 2028 * Bspec vol 1c.3 - blitter engine command streamer:
9a289771
JB
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."
2032 */
70b73f9a 2033 cmd |= flags;
f0a1fb10 2034
73dec95e
TU
2035 *cs++ = cmd;
2036 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
79e6770c 2037 *cs++ = 0;
73dec95e 2038 *cs++ = MI_NOOP;
70b73f9a 2039
e61e0f51 2040 intel_ring_advance(rq, cs);
70b73f9a 2041
1c7a0623
BW
2042 return 0;
2043}
2044
70b73f9a
CW
2045static int gen6_flush_dw(struct i915_request *rq, u32 mode, u32 invflags)
2046{
06348d30 2047 return mi_flush_dw(rq, mode & EMIT_INVALIDATE ? invflags : 0);
70b73f9a
CW
2048}
2049
2050static int gen6_bsd_ring_flush(struct i915_request *rq, u32 mode)
2051{
2052 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB | MI_INVALIDATE_BSD);
2053}
2054
d7d4eedd 2055static int
e61e0f51 2056hsw_emit_bb_start(struct i915_request *rq,
803688ba
CW
2057 u64 offset, u32 len,
2058 unsigned int dispatch_flags)
d7d4eedd 2059{
73dec95e 2060 u32 *cs;
d7d4eedd 2061
e61e0f51 2062 cs = intel_ring_begin(rq, 2);
73dec95e
TU
2063 if (IS_ERR(cs))
2064 return PTR_ERR(cs);
d7d4eedd 2065
73dec95e 2066 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
08e3e21a 2067 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW);
d7d4eedd 2068 /* bit0-7 is the length on GEN6+ */
73dec95e 2069 *cs++ = offset;
e61e0f51 2070 intel_ring_advance(rq, cs);
d7d4eedd
CW
2071
2072 return 0;
2073}
2074
881f47b6 2075static int
e61e0f51 2076gen6_emit_bb_start(struct i915_request *rq,
803688ba
CW
2077 u64 offset, u32 len,
2078 unsigned int dispatch_flags)
881f47b6 2079{
73dec95e 2080 u32 *cs;
ab6f8e32 2081
e61e0f51 2082 cs = intel_ring_begin(rq, 2);
73dec95e
TU
2083 if (IS_ERR(cs))
2084 return PTR_ERR(cs);
e1f99ce6 2085
73dec95e
TU
2086 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
2087 0 : MI_BATCH_NON_SECURE_I965);
0206e353 2088 /* bit0-7 is the length on GEN6+ */
73dec95e 2089 *cs++ = offset;
e61e0f51 2090 intel_ring_advance(rq, cs);
ab6f8e32 2091
0206e353 2092 return 0;
881f47b6
XH
2093}
2094
549f7365
CW
2095/* Blitter support (SandyBridge+) */
2096
e61e0f51 2097static int gen6_ring_flush(struct i915_request *rq, u32 mode)
8d19215b 2098{
70b73f9a 2099 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB);
8d19215b
ZN
2100}
2101
ff44ad51
CW
2102static void i9xx_set_default_submission(struct intel_engine_cs *engine)
2103{
2104 engine->submit_request = i9xx_submit_request;
27a5f61b 2105 engine->cancel_requests = cancel_requests;
aba5e278
CW
2106
2107 engine->park = NULL;
2108 engine->unpark = NULL;
ff44ad51
CW
2109}
2110
2111static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
2112{
aba5e278 2113 i9xx_set_default_submission(engine);
ff44ad51
CW
2114 engine->submit_request = gen6_bsd_submit_request;
2115}
2116
45b9c968
CW
2117static void ring_destroy(struct intel_engine_cs *engine)
2118{
2119 struct drm_i915_private *dev_priv = engine->i915;
2120
2121 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
2122 (ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE) == 0);
2123
2124 intel_ring_unpin(engine->buffer);
2125 intel_ring_put(engine->buffer);
2126
2127 intel_engine_cleanup_common(engine);
2128 kfree(engine);
2129}
2130
11334c6a
CW
2131static void setup_irq(struct intel_engine_cs *engine)
2132{
2133 struct drm_i915_private *i915 = engine->i915;
2134
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;
2144 } else {
2145 engine->irq_enable = i8xx_irq_enable;
2146 engine->irq_disable = i8xx_irq_disable;
2147 }
2148}
2149
2150static void setup_common(struct intel_engine_cs *engine)
06a2fe22 2151{
11334c6a
CW
2152 struct drm_i915_private *i915 = engine->i915;
2153
79e6770c 2154 /* gen8+ are only supported with execlists */
11334c6a 2155 GEM_BUG_ON(INTEL_GEN(i915) >= 8);
79e6770c 2156
11334c6a 2157 setup_irq(engine);
618e4ca7 2158
45b9c968
CW
2159 engine->destroy = ring_destroy;
2160
79ffac85 2161 engine->resume = xcs_resume;
5adfb772
CW
2162 engine->reset.prepare = reset_prepare;
2163 engine->reset.reset = reset_ring;
2164 engine->reset.finish = reset_finish;
7445a2a4 2165
4dc84b77 2166 engine->cops = &ring_context_ops;
f73e7399
CW
2167 engine->request_alloc = ring_request_alloc;
2168
85474441
CW
2169 /*
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().
2173 */
2174 engine->emit_fini_breadcrumb = i9xx_emit_breadcrumb;
11334c6a 2175 if (IS_GEN(i915, 5))
85474441 2176 engine->emit_fini_breadcrumb = gen5_emit_breadcrumb;
ff44ad51
CW
2177
2178 engine->set_default_submission = i9xx_set_default_submission;
6f7bef75 2179
11334c6a 2180 if (INTEL_GEN(i915) >= 6)
803688ba 2181 engine->emit_bb_start = gen6_emit_bb_start;
11334c6a 2182 else if (INTEL_GEN(i915) >= 4)
803688ba 2183 engine->emit_bb_start = i965_emit_bb_start;
11334c6a 2184 else if (IS_I830(i915) || IS_I845G(i915))
803688ba 2185 engine->emit_bb_start = i830_emit_bb_start;
6f7bef75 2186 else
803688ba 2187 engine->emit_bb_start = i915_emit_bb_start;
06a2fe22
TU
2188}
2189
11334c6a 2190static void setup_rcs(struct intel_engine_cs *engine)
5c1143bb 2191{
11334c6a 2192 struct drm_i915_private *i915 = engine->i915;
06a2fe22 2193
11334c6a 2194 if (HAS_L3_DPF(i915))
61ff75ac 2195 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
f8973c21 2196
fa6f071d
DCS
2197 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2198
11334c6a 2199 if (INTEL_GEN(i915) >= 7) {
e2f80391 2200 engine->init_context = intel_rcs_ctx_init;
c7fe7d25 2201 engine->emit_flush = gen7_render_ring_flush;
85474441 2202 engine->emit_fini_breadcrumb = gen7_rcs_emit_breadcrumb;
11334c6a 2203 } else if (IS_GEN(i915, 6)) {
caa5915b
CW
2204 engine->init_context = intel_rcs_ctx_init;
2205 engine->emit_flush = gen6_render_ring_flush;
85474441 2206 engine->emit_fini_breadcrumb = gen6_rcs_emit_breadcrumb;
11334c6a 2207 } else if (IS_GEN(i915, 5)) {
c7fe7d25 2208 engine->emit_flush = gen4_render_ring_flush;
59465b5f 2209 } else {
11334c6a 2210 if (INTEL_GEN(i915) < 4)
c7fe7d25 2211 engine->emit_flush = gen2_render_ring_flush;
46f0f8d1 2212 else
c7fe7d25 2213 engine->emit_flush = gen4_render_ring_flush;
e2f80391 2214 engine->irq_enable_mask = I915_USER_INTERRUPT;
1ec14ad3 2215 }
707d9cf9 2216
11334c6a 2217 if (IS_HASWELL(i915))
803688ba 2218 engine->emit_bb_start = hsw_emit_bb_start;
6f7bef75 2219
79ffac85 2220 engine->resume = rcs_resume;
5c1143bb
XH
2221}
2222
11334c6a 2223static void setup_vcs(struct intel_engine_cs *engine)
5c1143bb 2224{
11334c6a 2225 struct drm_i915_private *i915 = engine->i915;
06a2fe22 2226
11334c6a 2227 if (INTEL_GEN(i915) >= 6) {
0fd2c201 2228 /* gen6 bsd needs a special wa for tail updates */
11334c6a 2229 if (IS_GEN(i915, 6))
ff44ad51 2230 engine->set_default_submission = gen6_bsd_set_default_submission;
c7fe7d25 2231 engine->emit_flush = gen6_bsd_ring_flush;
79e6770c 2232 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
caa5915b 2233
11334c6a 2234 if (IS_GEN(i915, 6))
85474441 2235 engine->emit_fini_breadcrumb = gen6_xcs_emit_breadcrumb;
9fa4973e 2236 else
85474441 2237 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
58fa3835 2238 } else {
c7fe7d25 2239 engine->emit_flush = bsd_ring_flush;
11334c6a 2240 if (IS_GEN(i915, 5))
e2f80391 2241 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
8d228911 2242 else
e2f80391 2243 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
58fa3835 2244 }
5c1143bb 2245}
549f7365 2246
11334c6a 2247static void setup_bcs(struct intel_engine_cs *engine)
549f7365 2248{
11334c6a 2249 struct drm_i915_private *i915 = engine->i915;
06a2fe22 2250
c7fe7d25 2251 engine->emit_flush = gen6_ring_flush;
79e6770c 2252 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
549f7365 2253
11334c6a 2254 if (IS_GEN(i915, 6))
85474441 2255 engine->emit_fini_breadcrumb = gen6_xcs_emit_breadcrumb;
9fa4973e 2256 else
85474441 2257 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
549f7365 2258}
a7b9761d 2259
11334c6a 2260static void setup_vecs(struct intel_engine_cs *engine)
9a8a2213 2261{
11334c6a 2262 struct drm_i915_private *i915 = engine->i915;
caa5915b 2263
11334c6a 2264 GEM_BUG_ON(INTEL_GEN(i915) < 7);
06a2fe22 2265
c7fe7d25 2266 engine->emit_flush = gen6_ring_flush;
79e6770c
CW
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;
9a8a2213 2270
85474441 2271 engine->emit_fini_breadcrumb = gen7_xcs_emit_breadcrumb;
11334c6a
CW
2272}
2273
2274int intel_ring_submission_setup(struct intel_engine_cs *engine)
2275{
2276 setup_common(engine);
2277
2278 switch (engine->class) {
2279 case RENDER_CLASS:
2280 setup_rcs(engine);
2281 break;
2282 case VIDEO_DECODE_CLASS:
2283 setup_vcs(engine);
2284 break;
2285 case COPY_ENGINE_CLASS:
2286 setup_bcs(engine);
2287 break;
2288 case VIDEO_ENHANCEMENT_CLASS:
2289 setup_vecs(engine);
2290 break;
2291 default:
2292 MISSING_CASE(engine->class);
2293 return -ENODEV;
2294 }
2295
2296 return 0;
2297}
2298
2299int intel_ring_submission_init(struct intel_engine_cs *engine)
2300{
2301 struct i915_timeline *timeline;
2302 struct intel_ring *ring;
2303 int err;
2304
2305 timeline = i915_timeline_create(engine->i915, engine->status_page.vma);
2306 if (IS_ERR(timeline)) {
2307 err = PTR_ERR(timeline);
2308 goto err;
2309 }
2310 GEM_BUG_ON(timeline->has_initial_breadcrumb);
2311
2312 ring = intel_engine_create_ring(engine, timeline, 32 * PAGE_SIZE);
2313 i915_timeline_put(timeline);
2314 if (IS_ERR(ring)) {
2315 err = PTR_ERR(ring);
2316 goto err;
2317 }
2318
2319 err = intel_ring_pin(ring);
2320 if (err)
2321 goto err_ring;
caa5915b 2322
11334c6a
CW
2323 GEM_BUG_ON(engine->buffer);
2324 engine->buffer = ring;
2325
2326 err = intel_engine_init_common(engine);
2327 if (err)
2328 goto err_unpin;
2329
2330 GEM_BUG_ON(ring->timeline->hwsp_ggtt != engine->status_page.vma);
2331
2332 return 0;
2333
2334err_unpin:
2335 intel_ring_unpin(ring);
2336err_ring:
2337 intel_ring_put(ring);
2338err:
2339 intel_engine_cleanup_common(engine);
2340 return err;
9a8a2213 2341}