]> Git Repo - linux.git/blame - drivers/gpu/drm/i915/intel_ringbuffer.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / gpu / drm / i915 / 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 <[email protected]>
25 * Zou Nan hai <[email protected]>
26 * Xiang Hai hao<[email protected]>
27 *
28 */
29
a4d8a0fe 30#include <linux/log2.h>
7c2fa7fa 31
760285e7 32#include <drm/drmP.h>
760285e7 33#include <drm/i915_drm.h>
7c2fa7fa
CW
34
35#include "i915_drv.h"
36#include "i915_gem_render_state.h"
62fdfeaf 37#include "i915_trace.h"
881f47b6 38#include "intel_drv.h"
7d3c425f 39#include "intel_workarounds.h"
62fdfeaf 40
a0442461
CW
41/* Rough estimate of the typical request size, performing a flush,
42 * set-context and then emitting the batch.
43 */
44#define LEGACY_REQUEST_SIZE 200
45
605d5b32
CW
46static unsigned int __intel_ring_space(unsigned int head,
47 unsigned int tail,
48 unsigned int size)
c7dca47b 49{
605d5b32
CW
50 /*
51 * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
52 * same cacheline, the Head Pointer must not be greater than the Tail
53 * Pointer."
54 */
55 GEM_BUG_ON(!is_power_of_2(size));
56 return (head - tail - CACHELINE_BYTES) & (size - 1);
c7dca47b
CW
57}
58
95aebcb2 59unsigned int intel_ring_update_space(struct intel_ring *ring)
ebd0fd4b 60{
95aebcb2
CW
61 unsigned int space;
62
63 space = __intel_ring_space(ring->head, ring->emit, ring->size);
64
65 ring->space = space;
66 return space;
ebd0fd4b
DG
67}
68
b72f3acb 69static int
e61e0f51 70gen2_render_ring_flush(struct i915_request *rq, u32 mode)
46f0f8d1 71{
73dec95e 72 u32 cmd, *cs;
46f0f8d1
CW
73
74 cmd = MI_FLUSH;
46f0f8d1 75
7c9cf4e3 76 if (mode & EMIT_INVALIDATE)
46f0f8d1
CW
77 cmd |= MI_READ_FLUSH;
78
e61e0f51 79 cs = intel_ring_begin(rq, 2);
73dec95e
TU
80 if (IS_ERR(cs))
81 return PTR_ERR(cs);
46f0f8d1 82
73dec95e
TU
83 *cs++ = cmd;
84 *cs++ = MI_NOOP;
e61e0f51 85 intel_ring_advance(rq, cs);
46f0f8d1
CW
86
87 return 0;
88}
89
90static int
e61e0f51 91gen4_render_ring_flush(struct i915_request *rq, u32 mode)
62fdfeaf 92{
73dec95e 93 u32 cmd, *cs;
fb5bbae9 94 int i;
6f392d54 95
36d527de
CW
96 /*
97 * read/write caches:
98 *
99 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
100 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
101 * also flushed at 2d versus 3d pipeline switches.
102 *
103 * read-only caches:
104 *
105 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
106 * MI_READ_FLUSH is set, and is always flushed on 965.
107 *
108 * I915_GEM_DOMAIN_COMMAND may not exist?
109 *
110 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
111 * invalidated when MI_EXE_FLUSH is set.
112 *
113 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
114 * invalidated with every MI_FLUSH.
115 *
116 * TLBs:
117 *
118 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
119 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
120 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
121 * are flushed at any MI_FLUSH.
122 */
123
b5321f30 124 cmd = MI_FLUSH;
7c9cf4e3 125 if (mode & EMIT_INVALIDATE) {
36d527de 126 cmd |= MI_EXE_FLUSH;
e61e0f51 127 if (IS_G4X(rq->i915) || IS_GEN5(rq->i915))
b5321f30
CW
128 cmd |= MI_INVALIDATE_ISP;
129 }
70eac33e 130
fb5bbae9
CW
131 i = 2;
132 if (mode & EMIT_INVALIDATE)
133 i += 20;
134
135 cs = intel_ring_begin(rq, i);
73dec95e
TU
136 if (IS_ERR(cs))
137 return PTR_ERR(cs);
b72f3acb 138
73dec95e 139 *cs++ = cmd;
fb5bbae9
CW
140
141 /*
142 * A random delay to let the CS invalidate take effect? Without this
143 * delay, the GPU relocation path fails as the CS does not see
144 * the updated contents. Just as important, if we apply the flushes
145 * to the EMIT_FLUSH branch (i.e. immediately after the relocation
146 * write and before the invalidate on the next batch), the relocations
147 * still fail. This implies that is a delay following invalidation
148 * that is required to reset the caches as opposed to a delay to
149 * ensure the memory is written.
150 */
151 if (mode & EMIT_INVALIDATE) {
152 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE;
153 *cs++ = i915_ggtt_offset(rq->engine->scratch) |
154 PIPE_CONTROL_GLOBAL_GTT;
155 *cs++ = 0;
156 *cs++ = 0;
157
158 for (i = 0; i < 12; i++)
159 *cs++ = MI_FLUSH;
160
161 *cs++ = GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE;
162 *cs++ = i915_ggtt_offset(rq->engine->scratch) |
163 PIPE_CONTROL_GLOBAL_GTT;
164 *cs++ = 0;
165 *cs++ = 0;
166 }
167
168 *cs++ = cmd;
169
e61e0f51 170 intel_ring_advance(rq, cs);
b72f3acb
CW
171
172 return 0;
8187a2b7
ZN
173}
174
179f4025 175/*
8d315287
JB
176 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
177 * implementing two workarounds on gen6. From section 1.4.7.1
178 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
179 *
180 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
181 * produced by non-pipelined state commands), software needs to first
182 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
183 * 0.
184 *
185 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
186 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
187 *
188 * And the workaround for these two requires this workaround first:
189 *
190 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
191 * BEFORE the pipe-control with a post-sync op and no write-cache
192 * flushes.
193 *
194 * And this last workaround is tricky because of the requirements on
195 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
196 * volume 2 part 1:
197 *
198 * "1 of the following must also be set:
199 * - Render Target Cache Flush Enable ([12] of DW1)
200 * - Depth Cache Flush Enable ([0] of DW1)
201 * - Stall at Pixel Scoreboard ([1] of DW1)
202 * - Depth Stall ([13] of DW1)
203 * - Post-Sync Operation ([13] of DW1)
204 * - Notify Enable ([8] of DW1)"
205 *
206 * The cache flushes require the workaround flush that triggered this
207 * one, so we can't use it. Depth stall would trigger the same.
208 * Post-sync nonzero is what triggered this second workaround, so we
209 * can't use that one either. Notify enable is IRQs, which aren't
210 * really our business. That leaves only stall at scoreboard.
211 */
212static int
e61e0f51 213intel_emit_post_sync_nonzero_flush(struct i915_request *rq)
8d315287 214{
b5321f30 215 u32 scratch_addr =
e61e0f51 216 i915_ggtt_offset(rq->engine->scratch) + 2 * CACHELINE_BYTES;
73dec95e
TU
217 u32 *cs;
218
e61e0f51 219 cs = intel_ring_begin(rq, 6);
73dec95e
TU
220 if (IS_ERR(cs))
221 return PTR_ERR(cs);
222
223 *cs++ = GFX_OP_PIPE_CONTROL(5);
224 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
225 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
226 *cs++ = 0; /* low dword */
227 *cs++ = 0; /* high dword */
228 *cs++ = MI_NOOP;
e61e0f51 229 intel_ring_advance(rq, cs);
73dec95e 230
e61e0f51 231 cs = intel_ring_begin(rq, 6);
73dec95e
TU
232 if (IS_ERR(cs))
233 return PTR_ERR(cs);
234
235 *cs++ = GFX_OP_PIPE_CONTROL(5);
236 *cs++ = PIPE_CONTROL_QW_WRITE;
237 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
238 *cs++ = 0;
239 *cs++ = 0;
240 *cs++ = MI_NOOP;
e61e0f51 241 intel_ring_advance(rq, cs);
8d315287
JB
242
243 return 0;
244}
245
246static int
e61e0f51 247gen6_render_ring_flush(struct i915_request *rq, u32 mode)
8d315287 248{
b5321f30 249 u32 scratch_addr =
e61e0f51 250 i915_ggtt_offset(rq->engine->scratch) + 2 * CACHELINE_BYTES;
73dec95e 251 u32 *cs, flags = 0;
8d315287
JB
252 int ret;
253
b3111509 254 /* Force SNB workarounds for PIPE_CONTROL flushes */
e61e0f51 255 ret = intel_emit_post_sync_nonzero_flush(rq);
b3111509
PZ
256 if (ret)
257 return ret;
258
8d315287
JB
259 /* Just flush everything. Experiments have shown that reducing the
260 * number of bits based on the write domains has little performance
261 * impact.
262 */
7c9cf4e3 263 if (mode & EMIT_FLUSH) {
7d54a904
CW
264 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
265 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
266 /*
267 * Ensure that any following seqno writes only happen
268 * when the render cache is indeed flushed.
269 */
97f209bc 270 flags |= PIPE_CONTROL_CS_STALL;
7d54a904 271 }
7c9cf4e3 272 if (mode & EMIT_INVALIDATE) {
7d54a904
CW
273 flags |= PIPE_CONTROL_TLB_INVALIDATE;
274 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
275 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
276 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
277 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
278 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
279 /*
280 * TLB invalidate requires a post-sync write.
281 */
3ac78313 282 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
7d54a904 283 }
8d315287 284
e61e0f51 285 cs = intel_ring_begin(rq, 4);
73dec95e
TU
286 if (IS_ERR(cs))
287 return PTR_ERR(cs);
8d315287 288
73dec95e
TU
289 *cs++ = GFX_OP_PIPE_CONTROL(4);
290 *cs++ = flags;
291 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
292 *cs++ = 0;
e61e0f51 293 intel_ring_advance(rq, cs);
8d315287
JB
294
295 return 0;
296}
297
f3987631 298static int
e61e0f51 299gen7_render_ring_cs_stall_wa(struct i915_request *rq)
f3987631 300{
73dec95e 301 u32 *cs;
f3987631 302
e61e0f51 303 cs = intel_ring_begin(rq, 4);
73dec95e
TU
304 if (IS_ERR(cs))
305 return PTR_ERR(cs);
f3987631 306
73dec95e
TU
307 *cs++ = GFX_OP_PIPE_CONTROL(4);
308 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
309 *cs++ = 0;
310 *cs++ = 0;
e61e0f51 311 intel_ring_advance(rq, cs);
f3987631
PZ
312
313 return 0;
314}
315
4772eaeb 316static int
e61e0f51 317gen7_render_ring_flush(struct i915_request *rq, u32 mode)
4772eaeb 318{
b5321f30 319 u32 scratch_addr =
e61e0f51 320 i915_ggtt_offset(rq->engine->scratch) + 2 * CACHELINE_BYTES;
73dec95e 321 u32 *cs, flags = 0;
4772eaeb 322
f3987631
PZ
323 /*
324 * Ensure that any following seqno writes only happen when the render
325 * cache is indeed flushed.
326 *
327 * Workaround: 4th PIPE_CONTROL command (except the ones with only
328 * read-cache invalidate bits set) must have the CS_STALL bit set. We
329 * don't try to be clever and just set it unconditionally.
330 */
331 flags |= PIPE_CONTROL_CS_STALL;
332
4772eaeb
PZ
333 /* Just flush everything. Experiments have shown that reducing the
334 * number of bits based on the write domains has little performance
335 * impact.
336 */
7c9cf4e3 337 if (mode & EMIT_FLUSH) {
4772eaeb
PZ
338 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
339 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
965fd602 340 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
40a24488 341 flags |= PIPE_CONTROL_FLUSH_ENABLE;
4772eaeb 342 }
7c9cf4e3 343 if (mode & EMIT_INVALIDATE) {
4772eaeb
PZ
344 flags |= PIPE_CONTROL_TLB_INVALIDATE;
345 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
346 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
347 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
348 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
349 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
148b83d0 350 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
4772eaeb
PZ
351 /*
352 * TLB invalidate requires a post-sync write.
353 */
354 flags |= PIPE_CONTROL_QW_WRITE;
b9e1faa7 355 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
f3987631 356
add284a3
CW
357 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
358
f3987631
PZ
359 /* Workaround: we must issue a pipe_control with CS-stall bit
360 * set before a pipe_control command that has the state cache
361 * invalidate bit set. */
e61e0f51 362 gen7_render_ring_cs_stall_wa(rq);
4772eaeb
PZ
363 }
364
e61e0f51 365 cs = intel_ring_begin(rq, 4);
73dec95e
TU
366 if (IS_ERR(cs))
367 return PTR_ERR(cs);
4772eaeb 368
73dec95e
TU
369 *cs++ = GFX_OP_PIPE_CONTROL(4);
370 *cs++ = flags;
371 *cs++ = scratch_addr;
372 *cs++ = 0;
e61e0f51 373 intel_ring_advance(rq, cs);
4772eaeb
PZ
374
375 return 0;
376}
377
0bc40be8 378static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
035dc1e0 379{
c033666a 380 struct drm_i915_private *dev_priv = engine->i915;
d6acae36
CW
381 struct page *page = virt_to_page(engine->status_page.page_addr);
382 phys_addr_t phys = PFN_PHYS(page_to_pfn(page));
035dc1e0
SV
383 u32 addr;
384
d6acae36 385 addr = lower_32_bits(phys);
c033666a 386 if (INTEL_GEN(dev_priv) >= 4)
d6acae36
CW
387 addr |= (phys >> 28) & 0xf0;
388
035dc1e0
SV
389 I915_WRITE(HWS_PGA, addr);
390}
391
0bc40be8 392static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
af75f269 393{
c033666a 394 struct drm_i915_private *dev_priv = engine->i915;
f0f59a00 395 i915_reg_t mmio;
af75f269
DL
396
397 /* The ring status page addresses are no longer next to the rest of
398 * the ring registers as of gen7.
399 */
c033666a 400 if (IS_GEN7(dev_priv)) {
0bc40be8 401 switch (engine->id) {
a2d3d265
MT
402 /*
403 * No more rings exist on Gen7. Default case is only to shut up
404 * gcc switch check warning.
405 */
406 default:
407 GEM_BUG_ON(engine->id);
af75f269
DL
408 case RCS:
409 mmio = RENDER_HWS_PGA_GEN7;
410 break;
411 case BCS:
412 mmio = BLT_HWS_PGA_GEN7;
413 break;
af75f269
DL
414 case VCS:
415 mmio = BSD_HWS_PGA_GEN7;
416 break;
417 case VECS:
418 mmio = VEBOX_HWS_PGA_GEN7;
419 break;
420 }
c033666a 421 } else if (IS_GEN6(dev_priv)) {
0bc40be8 422 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
af75f269 423 } else {
0bc40be8 424 mmio = RING_HWS_PGA(engine->mmio_base);
af75f269
DL
425 }
426
a4a71701
CW
427 if (INTEL_GEN(dev_priv) >= 6) {
428 u32 mask = ~0u;
429
430 /*
431 * Keep the render interrupt unmasked as this papers over
432 * lost interrupts following a reset.
433 */
434 if (engine->id == RCS)
435 mask &= ~BIT(0);
436
437 I915_WRITE(RING_HWSTAM(engine->mmio_base), mask);
438 }
c5498089 439
57e88531 440 I915_WRITE(mmio, engine->status_page.ggtt_offset);
af75f269
DL
441 POSTING_READ(mmio);
442
79e6770c 443 /* Flush the TLB for this page */
ac657f64 444 if (IS_GEN(dev_priv, 6, 7)) {
0bc40be8 445 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
af75f269
DL
446
447 /* ring should be idle before issuing a sync flush*/
0bc40be8 448 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
af75f269
DL
449
450 I915_WRITE(reg,
451 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
452 INSTPM_SYNC_FLUSH));
25ab57f4
CW
453 if (intel_wait_for_register(dev_priv,
454 reg, INSTPM_SYNC_FLUSH, 0,
455 1000))
af75f269 456 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
0bc40be8 457 engine->name);
af75f269
DL
458 }
459}
460
0bc40be8 461static bool stop_ring(struct intel_engine_cs *engine)
8187a2b7 462{
c033666a 463 struct drm_i915_private *dev_priv = engine->i915;
8187a2b7 464
21a2c58a 465 if (INTEL_GEN(dev_priv) > 2) {
0bc40be8 466 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
3d808eb1
CW
467 if (intel_wait_for_register(dev_priv,
468 RING_MI_MODE(engine->mmio_base),
469 MODE_IDLE,
470 MODE_IDLE,
471 1000)) {
0bc40be8
TU
472 DRM_ERROR("%s : timed out trying to stop ring\n",
473 engine->name);
9bec9b13
CW
474 /* Sometimes we observe that the idle flag is not
475 * set even though the ring is empty. So double
476 * check before giving up.
477 */
0bc40be8 478 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
9bec9b13 479 return false;
9991ae78
CW
480 }
481 }
b7884eb4 482
11caf551
CW
483 I915_WRITE_HEAD(engine, I915_READ_TAIL(engine));
484
0bc40be8 485 I915_WRITE_HEAD(engine, 0);
c5efa1ad 486 I915_WRITE_TAIL(engine, 0);
8187a2b7 487
11caf551
CW
488 /* The ring must be empty before it is disabled */
489 I915_WRITE_CTL(engine, 0);
490
0bc40be8 491 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
9991ae78 492}
8187a2b7 493
0bc40be8 494static int init_ring_common(struct intel_engine_cs *engine)
9991ae78 495{
c033666a 496 struct drm_i915_private *dev_priv = engine->i915;
7e37f889 497 struct intel_ring *ring = engine->buffer;
9991ae78
CW
498 int ret = 0;
499
59bad947 500 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
9991ae78 501
0bc40be8 502 if (!stop_ring(engine)) {
9991ae78 503 /* G45 ring initialization often fails to reset head to zero */
8177e112
CW
504 DRM_DEBUG_DRIVER("%s head not reset to zero "
505 "ctl %08x head %08x tail %08x start %08x\n",
506 engine->name,
507 I915_READ_CTL(engine),
508 I915_READ_HEAD(engine),
509 I915_READ_TAIL(engine),
510 I915_READ_START(engine));
8187a2b7 511
0bc40be8 512 if (!stop_ring(engine)) {
6fd0d56e
CW
513 DRM_ERROR("failed to set %s head to zero "
514 "ctl %08x head %08x tail %08x start %08x\n",
0bc40be8
TU
515 engine->name,
516 I915_READ_CTL(engine),
517 I915_READ_HEAD(engine),
518 I915_READ_TAIL(engine),
519 I915_READ_START(engine));
9991ae78
CW
520 ret = -EIO;
521 goto out;
6fd0d56e 522 }
8187a2b7
ZN
523 }
524
3177659a 525 if (HWS_NEEDS_PHYSICAL(dev_priv))
0bc40be8 526 ring_setup_phys_status_page(engine);
3177659a
CS
527 else
528 intel_ring_setup_status_page(engine);
9991ae78 529
ad07dfcd 530 intel_engine_reset_breadcrumbs(engine);
821ed7df 531
ece4a17d 532 /* Enforce ordering by reading HEAD register back */
0bc40be8 533 I915_READ_HEAD(engine);
ece4a17d 534
0d8957c8
SV
535 /* Initialize the ring. This must happen _after_ we've cleared the ring
536 * registers with the above sequence (the readback of the HEAD registers
537 * also enforces ordering), otherwise the hw might lose the new ring
538 * register values. */
bde13ebd 539 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
95468892
CW
540
541 /* WaClearRingBufHeadRegAtInit:ctg,elk */
0bc40be8 542 if (I915_READ_HEAD(engine))
8177e112
CW
543 DRM_DEBUG_DRIVER("%s initialization failed [head=%08x], fudging\n",
544 engine->name, I915_READ_HEAD(engine));
821ed7df 545
41d37680
CW
546 /* Check that the ring offsets point within the ring! */
547 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->head));
548 GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->tail));
549
821ed7df
CW
550 intel_ring_update_space(ring);
551 I915_WRITE_HEAD(engine, ring->head);
552 I915_WRITE_TAIL(engine, ring->tail);
553 (void)I915_READ_TAIL(engine);
95468892 554
62ae14b1 555 I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
8187a2b7 556
8187a2b7 557 /* If the head is still not zero, the ring is dead */
f42bb651
CW
558 if (intel_wait_for_register(dev_priv, RING_CTL(engine->mmio_base),
559 RING_VALID, RING_VALID,
560 50)) {
e74cfed5 561 DRM_ERROR("%s initialization failed "
821ed7df 562 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
0bc40be8
TU
563 engine->name,
564 I915_READ_CTL(engine),
565 I915_READ_CTL(engine) & RING_VALID,
821ed7df
CW
566 I915_READ_HEAD(engine), ring->head,
567 I915_READ_TAIL(engine), ring->tail,
0bc40be8 568 I915_READ_START(engine),
bde13ebd 569 i915_ggtt_offset(ring->vma));
b7884eb4
SV
570 ret = -EIO;
571 goto out;
8187a2b7
ZN
572 }
573
7836cd02
CW
574 if (INTEL_GEN(dev_priv) > 2)
575 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
576
d6fee0de
CW
577 /* Papering over lost _interrupts_ immediately following the restart */
578 intel_engine_wakeup(engine);
b7884eb4 579out:
59bad947 580 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b7884eb4
SV
581
582 return ret;
8187a2b7
ZN
583}
584
5adfb772 585static struct i915_request *reset_prepare(struct intel_engine_cs *engine)
821ed7df 586{
3f6e9822
CW
587 intel_engine_stop_cs(engine);
588
5adfb772
CW
589 if (engine->irq_seqno_barrier)
590 engine->irq_seqno_barrier(engine);
591
592 return i915_gem_find_active_request(engine);
593}
594
b3ee09a4 595static void skip_request(struct i915_request *rq)
5adfb772 596{
b3ee09a4
CW
597 void *vaddr = rq->ring->vaddr;
598 u32 head;
5adfb772 599
b3ee09a4
CW
600 head = rq->infix;
601 if (rq->postfix < head) {
602 memset32(vaddr + head, MI_NOOP,
603 (rq->ring->size - head) / sizeof(u32));
604 head = 0;
605 }
606 memset32(vaddr + head, MI_NOOP, (rq->postfix - head) / sizeof(u32));
607}
608
609static void reset_ring(struct intel_engine_cs *engine, struct i915_request *rq)
610{
611 GEM_TRACE("%s seqno=%x\n", engine->name, rq ? rq->global_seqno : 0);
67e64564
CW
612
613 /*
614 * Try to restore the logical GPU state to match the continuation
c0dcb203
CW
615 * of the request queue. If we skip the context/PD restore, then
616 * the next request may try to execute assuming that its context
617 * is valid and loaded on the GPU and so may try to access invalid
618 * memory, prompting repeated GPU hangs.
619 *
620 * If the request was guilty, we still restore the logical state
621 * in case the next request requires it (e.g. the aliasing ppgtt),
622 * but skip over the hung batch.
623 *
624 * If the request was innocent, we try to replay the request with
625 * the restored context.
626 */
b3ee09a4 627 if (rq) {
c0dcb203 628 /* If the rq hung, jump to its breadcrumb and skip the batch */
b3ee09a4
CW
629 rq->ring->head = intel_ring_wrap(rq->ring, rq->head);
630 if (rq->fence.error == -EIO)
631 skip_request(rq);
c0dcb203 632 }
821ed7df
CW
633}
634
5adfb772
CW
635static void reset_finish(struct intel_engine_cs *engine)
636{
637}
638
e61e0f51 639static int intel_rcs_ctx_init(struct i915_request *rq)
8f0e2b9d
SV
640{
641 int ret;
642
59b449d5 643 ret = intel_ctx_workarounds_emit(rq);
8f0e2b9d
SV
644 if (ret != 0)
645 return ret;
646
e61e0f51 647 ret = i915_gem_render_state_emit(rq);
8f0e2b9d 648 if (ret)
e26e1b97 649 return ret;
8f0e2b9d 650
e26e1b97 651 return 0;
8f0e2b9d
SV
652}
653
0bc40be8 654static int init_render_ring(struct intel_engine_cs *engine)
8187a2b7 655{
c033666a 656 struct drm_i915_private *dev_priv = engine->i915;
0bc40be8 657 int ret = init_ring_common(engine);
9c33baa6
KZ
658 if (ret)
659 return ret;
a69ffdbf 660
f4ecfbfc 661 intel_whitelist_workarounds_apply(engine);
59b449d5 662
61a563a2 663 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
ac657f64 664 if (IS_GEN(dev_priv, 4, 6))
6b26c86d 665 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1c8c38c5
CW
666
667 /* We need to disable the AsyncFlip performance optimisations in order
668 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
669 * programmed to '1' on all products.
8693a824 670 *
2441f877 671 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1c8c38c5 672 */
ac657f64 673 if (IS_GEN(dev_priv, 6, 7))
1c8c38c5
CW
674 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
675
f05bb0c7 676 /* Required for the hardware to program scanline values for waiting */
01fa0302 677 /* WaEnableFlushTlbInvalidationMode:snb */
c033666a 678 if (IS_GEN6(dev_priv))
f05bb0c7 679 I915_WRITE(GFX_MODE,
aa83e30d 680 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
f05bb0c7 681
01fa0302 682 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
c033666a 683 if (IS_GEN7(dev_priv))
1c8c38c5 684 I915_WRITE(GFX_MODE_GEN7,
01fa0302 685 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1c8c38c5 686 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
78501eac 687
c033666a 688 if (IS_GEN6(dev_priv)) {
3a69ddd6
KG
689 /* From the Sandybridge PRM, volume 1 part 3, page 24:
690 * "If this bit is set, STCunit will have LRA as replacement
691 * policy. [...] This bit must be reset. LRA replacement
692 * policy is not supported."
693 */
694 I915_WRITE(CACHE_MODE_0,
5e13a0c5 695 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
84f9f938
BW
696 }
697
ac657f64 698 if (IS_GEN(dev_priv, 6, 7))
6b26c86d 699 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
84f9f938 700
c56b89f1 701 if (INTEL_GEN(dev_priv) >= 6)
035ea405 702 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
15b9f80e 703
59b449d5 704 return 0;
8187a2b7
ZN
705}
706
e61e0f51 707static u32 *gen6_signal(struct i915_request *rq, u32 *cs)
1ec14ad3 708{
e61e0f51 709 struct drm_i915_private *dev_priv = rq->i915;
318f89ca 710 struct intel_engine_cs *engine;
3b3f1650 711 enum intel_engine_id id;
caddfe71 712 int num_rings = 0;
024a43e1 713
3b3f1650 714 for_each_engine(engine, dev_priv, id) {
318f89ca
TU
715 i915_reg_t mbox_reg;
716
717 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
718 continue;
f0f59a00 719
e61e0f51 720 mbox_reg = rq->engine->semaphore.mbox.signal[engine->hw_id];
f0f59a00 721 if (i915_mmio_reg_valid(mbox_reg)) {
73dec95e
TU
722 *cs++ = MI_LOAD_REGISTER_IMM(1);
723 *cs++ = i915_mmio_reg_offset(mbox_reg);
e61e0f51 724 *cs++ = rq->global_seqno;
caddfe71 725 num_rings++;
78325f2d
BW
726 }
727 }
caddfe71 728 if (num_rings & 1)
73dec95e 729 *cs++ = MI_NOOP;
024a43e1 730
73dec95e 731 return cs;
1ec14ad3
CW
732}
733
27a5f61b
CW
734static void cancel_requests(struct intel_engine_cs *engine)
735{
e61e0f51 736 struct i915_request *request;
27a5f61b
CW
737 unsigned long flags;
738
a89d1f92 739 spin_lock_irqsave(&engine->timeline.lock, flags);
27a5f61b
CW
740
741 /* Mark all submitted requests as skipped. */
a89d1f92 742 list_for_each_entry(request, &engine->timeline.requests, link) {
27a5f61b 743 GEM_BUG_ON(!request->global_seqno);
e61e0f51 744 if (!i915_request_completed(request))
27a5f61b
CW
745 dma_fence_set_error(&request->fence, -EIO);
746 }
747 /* Remaining _unready_ requests will be nop'ed when submitted */
748
a89d1f92 749 spin_unlock_irqrestore(&engine->timeline.lock, flags);
27a5f61b
CW
750}
751
e61e0f51 752static void i9xx_submit_request(struct i915_request *request)
b0411e7d
CW
753{
754 struct drm_i915_private *dev_priv = request->i915;
755
e61e0f51 756 i915_request_submit(request);
d55ac5bf 757
e6ba9992
CW
758 I915_WRITE_TAIL(request->engine,
759 intel_ring_set_tail(request->ring, request->tail));
b0411e7d
CW
760}
761
e61e0f51 762static void i9xx_emit_breadcrumb(struct i915_request *rq, u32 *cs)
1ec14ad3 763{
73dec95e
TU
764 *cs++ = MI_STORE_DWORD_INDEX;
765 *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
e61e0f51 766 *cs++ = rq->global_seqno;
73dec95e 767 *cs++ = MI_USER_INTERRUPT;
1ec14ad3 768
e61e0f51
CW
769 rq->tail = intel_ring_offset(rq, cs);
770 assert_ring_tail_valid(rq->ring, rq->tail);
1ec14ad3
CW
771}
772
98f29e8d
CW
773static const int i9xx_emit_breadcrumb_sz = 4;
774
e61e0f51 775static void gen6_sema_emit_breadcrumb(struct i915_request *rq, u32 *cs)
b0411e7d 776{
e61e0f51 777 return i9xx_emit_breadcrumb(rq, rq->engine->semaphore.signal(rq, cs));
b0411e7d
CW
778}
779
c8c99b0f 780static int
e61e0f51 781gen6_ring_sync_to(struct i915_request *rq, struct i915_request *signal)
1ec14ad3 782{
c8c99b0f
BW
783 u32 dw1 = MI_SEMAPHORE_MBOX |
784 MI_SEMAPHORE_COMPARE |
785 MI_SEMAPHORE_REGISTER;
e61e0f51 786 u32 wait_mbox = signal->engine->semaphore.mbox.wait[rq->engine->hw_id];
73dec95e 787 u32 *cs;
1ec14ad3 788
ebc348b2 789 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
686cb5f9 790
e61e0f51 791 cs = intel_ring_begin(rq, 4);
73dec95e
TU
792 if (IS_ERR(cs))
793 return PTR_ERR(cs);
1ec14ad3 794
73dec95e 795 *cs++ = dw1 | wait_mbox;
ddf07be7
CW
796 /* Throughout all of the GEM code, seqno passed implies our current
797 * seqno is >= the last seqno executed. However for hardware the
798 * comparison is strictly greater than.
799 */
73dec95e
TU
800 *cs++ = signal->global_seqno - 1;
801 *cs++ = 0;
802 *cs++ = MI_NOOP;
e61e0f51 803 intel_ring_advance(rq, cs);
1ec14ad3
CW
804
805 return 0;
806}
807
f8973c21 808static void
38a0f2db 809gen5_seqno_barrier(struct intel_engine_cs *engine)
c6df541c 810{
f8973c21
CW
811 /* MI_STORE are internally buffered by the GPU and not flushed
812 * either by MI_FLUSH or SyncFlush or any other combination of
813 * MI commands.
c6df541c 814 *
f8973c21
CW
815 * "Only the submission of the store operation is guaranteed.
816 * The write result will be complete (coherent) some time later
817 * (this is practically a finite period but there is no guaranteed
818 * latency)."
819 *
820 * Empirically, we observe that we need a delay of at least 75us to
821 * be sure that the seqno write is visible by the CPU.
c6df541c 822 */
f8973c21 823 usleep_range(125, 250);
c6df541c
CW
824}
825
c04e0f3b
CW
826static void
827gen6_seqno_barrier(struct intel_engine_cs *engine)
4cd53c0c 828{
c033666a 829 struct drm_i915_private *dev_priv = engine->i915;
bcbdb6d0 830
4cd53c0c
SV
831 /* Workaround to force correct ordering between irq and seqno writes on
832 * ivb (and maybe also on snb) by reading from a CS register (like
9b9ed309
CW
833 * ACTHD) before reading the status page.
834 *
835 * Note that this effectively stalls the read by the time it takes to
836 * do a memory transaction, which more or less ensures that the write
837 * from the GPU has sufficient time to invalidate the CPU cacheline.
838 * Alternatively we could delay the interrupt from the CS ring to give
839 * the write time to land, but that would incur a delay after every
840 * batch i.e. much more frequent than a delay when waiting for the
841 * interrupt (with the same net latency).
bcbdb6d0
CW
842 *
843 * Also note that to prevent whole machine hangs on gen7, we have to
844 * take the spinlock to guard against concurrent cacheline access.
9b9ed309 845 */
bcbdb6d0 846 spin_lock_irq(&dev_priv->uncore.lock);
c04e0f3b 847 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
bcbdb6d0 848 spin_unlock_irq(&dev_priv->uncore.lock);
4cd53c0c
SV
849}
850
31bb59cc
CW
851static void
852gen5_irq_enable(struct intel_engine_cs *engine)
e48d8634 853{
31bb59cc 854 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
SV
855}
856
857static void
31bb59cc 858gen5_irq_disable(struct intel_engine_cs *engine)
e48d8634 859{
31bb59cc 860 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
SV
861}
862
31bb59cc
CW
863static void
864i9xx_irq_enable(struct intel_engine_cs *engine)
62fdfeaf 865{
c033666a 866 struct drm_i915_private *dev_priv = engine->i915;
b13c2b96 867
31bb59cc
CW
868 dev_priv->irq_mask &= ~engine->irq_enable_mask;
869 I915_WRITE(IMR, dev_priv->irq_mask);
870 POSTING_READ_FW(RING_IMR(engine->mmio_base));
62fdfeaf
EA
871}
872
8187a2b7 873static void
31bb59cc 874i9xx_irq_disable(struct intel_engine_cs *engine)
62fdfeaf 875{
c033666a 876 struct drm_i915_private *dev_priv = engine->i915;
62fdfeaf 877
31bb59cc
CW
878 dev_priv->irq_mask |= engine->irq_enable_mask;
879 I915_WRITE(IMR, dev_priv->irq_mask);
62fdfeaf
EA
880}
881
31bb59cc
CW
882static void
883i8xx_irq_enable(struct intel_engine_cs *engine)
c2798b19 884{
c033666a 885 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 886
31bb59cc
CW
887 dev_priv->irq_mask &= ~engine->irq_enable_mask;
888 I915_WRITE16(IMR, dev_priv->irq_mask);
889 POSTING_READ16(RING_IMR(engine->mmio_base));
c2798b19
CW
890}
891
892static void
31bb59cc 893i8xx_irq_disable(struct intel_engine_cs *engine)
c2798b19 894{
c033666a 895 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 896
31bb59cc
CW
897 dev_priv->irq_mask |= engine->irq_enable_mask;
898 I915_WRITE16(IMR, dev_priv->irq_mask);
c2798b19
CW
899}
900
b72f3acb 901static int
e61e0f51 902bsd_ring_flush(struct i915_request *rq, u32 mode)
d1b851fc 903{
73dec95e 904 u32 *cs;
b72f3acb 905
e61e0f51 906 cs = intel_ring_begin(rq, 2);
73dec95e
TU
907 if (IS_ERR(cs))
908 return PTR_ERR(cs);
b72f3acb 909
73dec95e
TU
910 *cs++ = MI_FLUSH;
911 *cs++ = MI_NOOP;
e61e0f51 912 intel_ring_advance(rq, cs);
b72f3acb 913 return 0;
d1b851fc
ZN
914}
915
31bb59cc
CW
916static void
917gen6_irq_enable(struct intel_engine_cs *engine)
0f46832f 918{
c033666a 919 struct drm_i915_private *dev_priv = engine->i915;
0f46832f 920
61ff75ac
CW
921 I915_WRITE_IMR(engine,
922 ~(engine->irq_enable_mask |
923 engine->irq_keep_mask));
31bb59cc 924 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
0f46832f
CW
925}
926
927static void
31bb59cc 928gen6_irq_disable(struct intel_engine_cs *engine)
0f46832f 929{
c033666a 930 struct drm_i915_private *dev_priv = engine->i915;
0f46832f 931
61ff75ac 932 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
31bb59cc 933 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
d1b851fc
ZN
934}
935
31bb59cc
CW
936static void
937hsw_vebox_irq_enable(struct intel_engine_cs *engine)
a19d2933 938{
c033666a 939 struct drm_i915_private *dev_priv = engine->i915;
a19d2933 940
31bb59cc 941 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
f4e9af4f 942 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
a19d2933
BW
943}
944
945static void
31bb59cc 946hsw_vebox_irq_disable(struct intel_engine_cs *engine)
a19d2933 947{
c033666a 948 struct drm_i915_private *dev_priv = engine->i915;
a19d2933 949
31bb59cc 950 I915_WRITE_IMR(engine, ~0);
f4e9af4f 951 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
a19d2933
BW
952}
953
d1b851fc 954static int
e61e0f51 955i965_emit_bb_start(struct i915_request *rq,
803688ba
CW
956 u64 offset, u32 length,
957 unsigned int dispatch_flags)
d1b851fc 958{
73dec95e 959 u32 *cs;
78501eac 960
e61e0f51 961 cs = intel_ring_begin(rq, 2);
73dec95e
TU
962 if (IS_ERR(cs))
963 return PTR_ERR(cs);
e1f99ce6 964
73dec95e
TU
965 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
966 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
967 *cs++ = offset;
e61e0f51 968 intel_ring_advance(rq, cs);
78501eac 969
d1b851fc
ZN
970 return 0;
971}
972
b45305fc
SV
973/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
974#define I830_BATCH_LIMIT (256*1024)
c4d69da1
CW
975#define I830_TLB_ENTRIES (2)
976#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
8187a2b7 977static int
e61e0f51 978i830_emit_bb_start(struct i915_request *rq,
803688ba
CW
979 u64 offset, u32 len,
980 unsigned int dispatch_flags)
62fdfeaf 981{
e61e0f51 982 u32 *cs, cs_offset = i915_ggtt_offset(rq->engine->scratch);
62fdfeaf 983
e61e0f51 984 cs = intel_ring_begin(rq, 6);
73dec95e
TU
985 if (IS_ERR(cs))
986 return PTR_ERR(cs);
62fdfeaf 987
c4d69da1 988 /* Evict the invalid PTE TLBs */
73dec95e
TU
989 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
990 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
991 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
992 *cs++ = cs_offset;
993 *cs++ = 0xdeadbeef;
994 *cs++ = MI_NOOP;
e61e0f51 995 intel_ring_advance(rq, cs);
b45305fc 996
8e004efc 997 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
b45305fc
SV
998 if (len > I830_BATCH_LIMIT)
999 return -ENOSPC;
1000
e61e0f51 1001 cs = intel_ring_begin(rq, 6 + 2);
73dec95e
TU
1002 if (IS_ERR(cs))
1003 return PTR_ERR(cs);
c4d69da1
CW
1004
1005 /* Blit the batch (which has now all relocs applied) to the
1006 * stable batch scratch bo area (so that the CS never
1007 * stumbles over its tlb invalidation bug) ...
1008 */
73dec95e
TU
1009 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
1010 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
1011 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
1012 *cs++ = cs_offset;
1013 *cs++ = 4096;
1014 *cs++ = offset;
1015
1016 *cs++ = MI_FLUSH;
1017 *cs++ = MI_NOOP;
e61e0f51 1018 intel_ring_advance(rq, cs);
b45305fc
SV
1019
1020 /* ... and execute it. */
c4d69da1 1021 offset = cs_offset;
b45305fc 1022 }
e1f99ce6 1023
e61e0f51 1024 cs = intel_ring_begin(rq, 2);
73dec95e
TU
1025 if (IS_ERR(cs))
1026 return PTR_ERR(cs);
c4d69da1 1027
73dec95e
TU
1028 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1029 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1030 MI_BATCH_NON_SECURE);
e61e0f51 1031 intel_ring_advance(rq, cs);
c4d69da1 1032
fb3256da
SV
1033 return 0;
1034}
1035
1036static int
e61e0f51 1037i915_emit_bb_start(struct i915_request *rq,
803688ba
CW
1038 u64 offset, u32 len,
1039 unsigned int dispatch_flags)
fb3256da 1040{
73dec95e 1041 u32 *cs;
fb3256da 1042
e61e0f51 1043 cs = intel_ring_begin(rq, 2);
73dec95e
TU
1044 if (IS_ERR(cs))
1045 return PTR_ERR(cs);
fb3256da 1046
73dec95e
TU
1047 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1048 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1049 MI_BATCH_NON_SECURE);
e61e0f51 1050 intel_ring_advance(rq, cs);
62fdfeaf 1051
62fdfeaf
EA
1052 return 0;
1053}
1054
5503cb0d 1055int intel_ring_pin(struct intel_ring *ring)
7ba717cf 1056{
57e88531 1057 struct i915_vma *vma = ring->vma;
5503cb0d
CW
1058 enum i915_map_type map =
1059 HAS_LLC(vma->vm->i915) ? I915_MAP_WB : I915_MAP_WC;
d822bb18 1060 unsigned int flags;
8305216f 1061 void *addr;
7ba717cf
TD
1062 int ret;
1063
57e88531 1064 GEM_BUG_ON(ring->vaddr);
7ba717cf 1065
d3ef1af6 1066 flags = PIN_GLOBAL;
496bcce3
JB
1067
1068 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
1069 flags |= PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
1070
9d80841e 1071 if (vma->obj->stolen)
57e88531 1072 flags |= PIN_MAPPABLE;
2edd4e69
CW
1073 else
1074 flags |= PIN_HIGH;
def0c5f6 1075
57e88531 1076 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
9d80841e 1077 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
57e88531
CW
1078 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1079 else
1080 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1081 if (unlikely(ret))
def0c5f6 1082 return ret;
57e88531 1083 }
7ba717cf 1084
7a859c65 1085 ret = i915_vma_pin(vma, 0, 0, flags);
57e88531
CW
1086 if (unlikely(ret))
1087 return ret;
def0c5f6 1088
9d80841e 1089 if (i915_vma_is_map_and_fenceable(vma))
57e88531
CW
1090 addr = (void __force *)i915_vma_pin_iomap(vma);
1091 else
9d80841e 1092 addr = i915_gem_object_pin_map(vma->obj, map);
57e88531
CW
1093 if (IS_ERR(addr))
1094 goto err;
7ba717cf 1095
3d574a6b
CW
1096 vma->obj->pin_global++;
1097
32c04f16 1098 ring->vaddr = addr;
7ba717cf 1099 return 0;
d2cad535 1100
57e88531
CW
1101err:
1102 i915_vma_unpin(vma);
1103 return PTR_ERR(addr);
7ba717cf
TD
1104}
1105
e6ba9992
CW
1106void intel_ring_reset(struct intel_ring *ring, u32 tail)
1107{
41d37680
CW
1108 GEM_BUG_ON(!intel_ring_offset_valid(ring, tail));
1109
e6ba9992
CW
1110 ring->tail = tail;
1111 ring->head = tail;
1112 ring->emit = tail;
1113 intel_ring_update_space(ring);
1114}
1115
aad29fbb
CW
1116void intel_ring_unpin(struct intel_ring *ring)
1117{
1118 GEM_BUG_ON(!ring->vma);
1119 GEM_BUG_ON(!ring->vaddr);
1120
e6ba9992
CW
1121 /* Discard any unused bytes beyond that submitted to hw. */
1122 intel_ring_reset(ring, ring->tail);
1123
9d80841e 1124 if (i915_vma_is_map_and_fenceable(ring->vma))
aad29fbb 1125 i915_vma_unpin_iomap(ring->vma);
57e88531
CW
1126 else
1127 i915_gem_object_unpin_map(ring->vma->obj);
aad29fbb
CW
1128 ring->vaddr = NULL;
1129
3d574a6b 1130 ring->vma->obj->pin_global--;
57e88531 1131 i915_vma_unpin(ring->vma);
2919d291
OM
1132}
1133
57e88531
CW
1134static struct i915_vma *
1135intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
62fdfeaf 1136{
250f8c81 1137 struct i915_address_space *vm = &dev_priv->ggtt.vm;
05394f39 1138 struct drm_i915_gem_object *obj;
57e88531 1139 struct i915_vma *vma;
62fdfeaf 1140
187685cb 1141 obj = i915_gem_object_create_stolen(dev_priv, size);
c58b735f 1142 if (!obj)
2d6c4c84 1143 obj = i915_gem_object_create_internal(dev_priv, size);
57e88531
CW
1144 if (IS_ERR(obj))
1145 return ERR_CAST(obj);
8187a2b7 1146
250f8c81
JB
1147 /*
1148 * Mark ring buffers as read-only from GPU side (so no stray overwrites)
1149 * if supported by the platform's GGTT.
1150 */
1151 if (vm->has_read_only)
3e977ac6 1152 i915_gem_object_set_readonly(obj);
24f3a8cf 1153
250f8c81 1154 vma = i915_vma_instance(obj, vm, NULL);
57e88531
CW
1155 if (IS_ERR(vma))
1156 goto err;
1157
1158 return vma;
e3efda49 1159
57e88531
CW
1160err:
1161 i915_gem_object_put(obj);
1162 return vma;
e3efda49
CW
1163}
1164
7e37f889 1165struct intel_ring *
65fcb806 1166intel_engine_create_ring(struct intel_engine_cs *engine,
a89d1f92 1167 struct i915_timeline *timeline,
65fcb806 1168 int size)
01101fa7 1169{
7e37f889 1170 struct intel_ring *ring;
57e88531 1171 struct i915_vma *vma;
01101fa7 1172
8f942018 1173 GEM_BUG_ON(!is_power_of_2(size));
62ae14b1 1174 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
a89d1f92 1175 GEM_BUG_ON(timeline == &engine->timeline);
b887d615 1176 lockdep_assert_held(&engine->i915->drm.struct_mutex);
8f942018 1177
01101fa7 1178 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
57e88531 1179 if (!ring)
01101fa7
CW
1180 return ERR_PTR(-ENOMEM);
1181
675d9ad7 1182 INIT_LIST_HEAD(&ring->request_list);
a89d1f92 1183 ring->timeline = i915_timeline_get(timeline);
675d9ad7 1184
01101fa7
CW
1185 ring->size = size;
1186 /* Workaround an erratum on the i830 which causes a hang if
1187 * the TAIL pointer points to within the last 2 cachelines
1188 * of the buffer.
1189 */
1190 ring->effective_size = size;
2a307c2e 1191 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
01101fa7
CW
1192 ring->effective_size -= 2 * CACHELINE_BYTES;
1193
01101fa7
CW
1194 intel_ring_update_space(ring);
1195
57e88531
CW
1196 vma = intel_ring_create_vma(engine->i915, size);
1197 if (IS_ERR(vma)) {
01101fa7 1198 kfree(ring);
57e88531 1199 return ERR_CAST(vma);
01101fa7 1200 }
57e88531 1201 ring->vma = vma;
01101fa7
CW
1202
1203 return ring;
1204}
1205
1206void
7e37f889 1207intel_ring_free(struct intel_ring *ring)
01101fa7 1208{
f8a7fde4
CW
1209 struct drm_i915_gem_object *obj = ring->vma->obj;
1210
1211 i915_vma_close(ring->vma);
1212 __i915_gem_object_release_unless_active(obj);
1213
a89d1f92 1214 i915_timeline_put(ring->timeline);
01101fa7
CW
1215 kfree(ring);
1216}
1217
1fc44d9b
CW
1218static void intel_ring_context_destroy(struct intel_context *ce)
1219{
1220 GEM_BUG_ON(ce->pin_count);
1221
efe79d48
CW
1222 if (!ce->state)
1223 return;
1224
1225 GEM_BUG_ON(i915_gem_object_is_active(ce->state->obj));
1226 i915_gem_object_put(ce->state->obj);
1fc44d9b
CW
1227}
1228
a2bbf714
CW
1229static int __context_pin_ppgtt(struct i915_gem_context *ctx)
1230{
1231 struct i915_hw_ppgtt *ppgtt;
1232 int err = 0;
1233
1234 ppgtt = ctx->ppgtt ?: ctx->i915->mm.aliasing_ppgtt;
1235 if (ppgtt)
1236 err = gen6_ppgtt_pin(ppgtt);
1237
1238 return err;
1239}
1240
1241static void __context_unpin_ppgtt(struct i915_gem_context *ctx)
1242{
1243 struct i915_hw_ppgtt *ppgtt;
1244
1245 ppgtt = ctx->ppgtt ?: ctx->i915->mm.aliasing_ppgtt;
1246 if (ppgtt)
1247 gen6_ppgtt_unpin(ppgtt);
1248}
1249
1fc44d9b 1250static int __context_pin(struct intel_context *ce)
e8a9c58f 1251{
d901e8e6
CW
1252 struct i915_vma *vma;
1253 int err;
1254
1255 vma = ce->state;
1256 if (!vma)
1257 return 0;
e8a9c58f 1258
f4e15af7
CW
1259 /*
1260 * Clear this page out of any CPU caches for coherent swap-in/out.
e8a9c58f
CW
1261 * We only want to do this on the first bind so that we do not stall
1262 * on an active context (which by nature is already on the GPU).
1263 */
1264 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
d901e8e6
CW
1265 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1266 if (err)
1267 return err;
e8a9c58f
CW
1268 }
1269
7a859c65 1270 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
d901e8e6
CW
1271 if (err)
1272 return err;
1273
1274 /*
1275 * And mark is as a globally pinned object to let the shrinker know
1276 * it cannot reclaim the object until we release it.
1277 */
1278 vma->obj->pin_global++;
1279
1280 return 0;
1281}
1282
1283static void __context_unpin(struct intel_context *ce)
1284{
1285 struct i915_vma *vma;
1286
1287 vma = ce->state;
1288 if (!vma)
1289 return;
1290
1291 vma->obj->pin_global--;
1292 i915_vma_unpin(vma);
1293}
1294
1295static void intel_ring_context_unpin(struct intel_context *ce)
1296{
a2bbf714 1297 __context_unpin_ppgtt(ce->gem_context);
d901e8e6
CW
1298 __context_unpin(ce);
1299
1300 i915_gem_context_put(ce->gem_context);
e8a9c58f
CW
1301}
1302
3204c343
CW
1303static struct i915_vma *
1304alloc_context_vma(struct intel_engine_cs *engine)
1305{
1306 struct drm_i915_private *i915 = engine->i915;
1307 struct drm_i915_gem_object *obj;
1308 struct i915_vma *vma;
d2b4b979 1309 int err;
3204c343 1310
63ffbcda 1311 obj = i915_gem_object_create(i915, engine->context_size);
3204c343
CW
1312 if (IS_ERR(obj))
1313 return ERR_CAST(obj);
1314
d2b4b979
CW
1315 if (engine->default_state) {
1316 void *defaults, *vaddr;
1317
1318 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1319 if (IS_ERR(vaddr)) {
1320 err = PTR_ERR(vaddr);
1321 goto err_obj;
1322 }
1323
1324 defaults = i915_gem_object_pin_map(engine->default_state,
1325 I915_MAP_WB);
1326 if (IS_ERR(defaults)) {
1327 err = PTR_ERR(defaults);
1328 goto err_map;
1329 }
1330
1331 memcpy(vaddr, defaults, engine->context_size);
1332
1333 i915_gem_object_unpin_map(engine->default_state);
1334 i915_gem_object_unpin_map(obj);
1335 }
1336
3204c343
CW
1337 /*
1338 * Try to make the context utilize L3 as well as LLC.
1339 *
1340 * On VLV we don't have L3 controls in the PTEs so we
1341 * shouldn't touch the cache level, especially as that
1342 * would make the object snooped which might have a
1343 * negative performance impact.
1344 *
1345 * Snooping is required on non-llc platforms in execlist
1346 * mode, but since all GGTT accesses use PAT entry 0 we
1347 * get snooping anyway regardless of cache_level.
1348 *
1349 * This is only applicable for Ivy Bridge devices since
1350 * later platforms don't have L3 control bits in the PTE.
1351 */
1352 if (IS_IVYBRIDGE(i915)) {
1353 /* Ignore any error, regard it as a simple optimisation */
1354 i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
1355 }
1356
82ad6443 1357 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
d2b4b979
CW
1358 if (IS_ERR(vma)) {
1359 err = PTR_ERR(vma);
1360 goto err_obj;
1361 }
3204c343
CW
1362
1363 return vma;
d2b4b979
CW
1364
1365err_map:
1366 i915_gem_object_unpin_map(obj);
1367err_obj:
1368 i915_gem_object_put(obj);
1369 return ERR_PTR(err);
3204c343
CW
1370}
1371
1fc44d9b
CW
1372static struct intel_context *
1373__ring_context_pin(struct intel_engine_cs *engine,
1374 struct i915_gem_context *ctx,
1375 struct intel_context *ce)
0cb26a8e 1376{
1fc44d9b 1377 int err;
0cb26a8e 1378
63ffbcda 1379 if (!ce->state && engine->context_size) {
3204c343
CW
1380 struct i915_vma *vma;
1381
1382 vma = alloc_context_vma(engine);
1383 if (IS_ERR(vma)) {
1fc44d9b 1384 err = PTR_ERR(vma);
266a240b 1385 goto err;
3204c343
CW
1386 }
1387
1388 ce->state = vma;
1389 }
1390
d901e8e6
CW
1391 err = __context_pin(ce);
1392 if (err)
1393 goto err;
0cb26a8e 1394
a2bbf714
CW
1395 err = __context_pin_ppgtt(ce->gem_context);
1396 if (err)
1397 goto err_unpin;
1398
9a6feaf0 1399 i915_gem_context_get(ctx);
0cb26a8e 1400
266a240b 1401 /* One ringbuffer to rule them all */
1fc44d9b
CW
1402 GEM_BUG_ON(!engine->buffer);
1403 ce->ring = engine->buffer;
1404
1405 return ce;
266a240b 1406
a2bbf714
CW
1407err_unpin:
1408 __context_unpin(ce);
266a240b 1409err:
0cb26a8e 1410 ce->pin_count = 0;
1fc44d9b 1411 return ERR_PTR(err);
0cb26a8e
CW
1412}
1413
1fc44d9b
CW
1414static const struct intel_context_ops ring_context_ops = {
1415 .unpin = intel_ring_context_unpin,
1416 .destroy = intel_ring_context_destroy,
1417};
1418
1419static struct intel_context *
1420intel_ring_context_pin(struct intel_engine_cs *engine,
1421 struct i915_gem_context *ctx)
0cb26a8e 1422{
ab82a063 1423 struct intel_context *ce = to_intel_context(ctx, engine);
0cb26a8e 1424
91c8a326 1425 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
0cb26a8e 1426
1fc44d9b
CW
1427 if (likely(ce->pin_count++))
1428 return ce;
1429 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
0cb26a8e 1430
1fc44d9b 1431 ce->ops = &ring_context_ops;
0cb26a8e 1432
1fc44d9b 1433 return __ring_context_pin(engine, ctx, ce);
0cb26a8e
CW
1434}
1435
acd27845 1436static int intel_init_ring_buffer(struct intel_engine_cs *engine)
e3efda49 1437{
a89d1f92 1438 struct i915_timeline *timeline;
d9d117e4
CW
1439 struct intel_ring *ring;
1440 unsigned int size;
1a5788bf 1441 int err;
bfc882b4 1442
019bf277
TU
1443 intel_engine_setup_common(engine);
1444
a89d1f92
CW
1445 timeline = i915_timeline_create(engine->i915, engine->name);
1446 if (IS_ERR(timeline)) {
1447 err = PTR_ERR(timeline);
1448 goto err;
1449 }
1450
1451 ring = intel_engine_create_ring(engine, timeline, 32 * PAGE_SIZE);
1452 i915_timeline_put(timeline);
d822bb18 1453 if (IS_ERR(ring)) {
1a5788bf 1454 err = PTR_ERR(ring);
486e93f7 1455 goto err;
d822bb18
CW
1456 }
1457
5503cb0d 1458 err = intel_ring_pin(ring);
1a5788bf
CW
1459 if (err)
1460 goto err_ring;
1461
1462 GEM_BUG_ON(engine->buffer);
57e88531 1463 engine->buffer = ring;
62fdfeaf 1464
d9d117e4
CW
1465 size = PAGE_SIZE;
1466 if (HAS_BROKEN_CS_TLB(engine->i915))
1467 size = I830_WA_SIZE;
1468 err = intel_engine_create_scratch(engine, size);
1fc44d9b
CW
1469 if (err)
1470 goto err_unpin;
1471
d9d117e4
CW
1472 err = intel_engine_init_common(engine);
1473 if (err)
1474 goto err_scratch;
1475
8ee14975 1476 return 0;
351e3db2 1477
d9d117e4
CW
1478err_scratch:
1479 intel_engine_cleanup_scratch(engine);
1fc44d9b
CW
1480err_unpin:
1481 intel_ring_unpin(ring);
1a5788bf
CW
1482err_ring:
1483 intel_ring_free(ring);
1a5788bf
CW
1484err:
1485 intel_engine_cleanup_common(engine);
1486 return err;
62fdfeaf
EA
1487}
1488
7e37f889 1489void intel_engine_cleanup(struct intel_engine_cs *engine)
62fdfeaf 1490{
1a5788bf 1491 struct drm_i915_private *dev_priv = engine->i915;
6402c330 1492
1a5788bf
CW
1493 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
1494 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
33626e6a 1495
1a5788bf
CW
1496 intel_ring_unpin(engine->buffer);
1497 intel_ring_free(engine->buffer);
78501eac 1498
0bc40be8
TU
1499 if (engine->cleanup)
1500 engine->cleanup(engine);
8d19215b 1501
96a945aa 1502 intel_engine_cleanup_common(engine);
0cb26a8e 1503
3b3f1650
AG
1504 dev_priv->engine[engine->id] = NULL;
1505 kfree(engine);
62fdfeaf
EA
1506}
1507
821ed7df
CW
1508void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
1509{
1510 struct intel_engine_cs *engine;
3b3f1650 1511 enum intel_engine_id id;
821ed7df 1512
e6ba9992 1513 /* Restart from the beginning of the rings for convenience */
fe085f13 1514 for_each_engine(engine, dev_priv, id)
e6ba9992 1515 intel_ring_reset(engine->buffer, 0);
821ed7df
CW
1516}
1517
b3ee09a4
CW
1518static int load_pd_dir(struct i915_request *rq,
1519 const struct i915_hw_ppgtt *ppgtt)
1520{
1521 const struct intel_engine_cs * const engine = rq->engine;
1522 u32 *cs;
1523
1524 cs = intel_ring_begin(rq, 6);
1525 if (IS_ERR(cs))
1526 return PTR_ERR(cs);
1527
1528 *cs++ = MI_LOAD_REGISTER_IMM(1);
1529 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1530 *cs++ = PP_DIR_DCLV_2G;
1531
1532 *cs++ = MI_LOAD_REGISTER_IMM(1);
1533 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1534 *cs++ = ppgtt->pd.base.ggtt_offset << 10;
1535
1536 intel_ring_advance(rq, cs);
1537
1538 return 0;
1539}
1540
d9d117e4
CW
1541static int flush_pd_dir(struct i915_request *rq)
1542{
1543 const struct intel_engine_cs * const engine = rq->engine;
1544 u32 *cs;
1545
1546 cs = intel_ring_begin(rq, 4);
1547 if (IS_ERR(cs))
1548 return PTR_ERR(cs);
1549
1550 /* Stall until the page table load is complete */
1551 *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
1552 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1553 *cs++ = i915_ggtt_offset(engine->scratch);
1554 *cs++ = MI_NOOP;
1555
1556 intel_ring_advance(rq, cs);
1557 return 0;
1558}
1559
e61e0f51 1560static inline int mi_set_context(struct i915_request *rq, u32 flags)
8911a31c
CW
1561{
1562 struct drm_i915_private *i915 = rq->i915;
1563 struct intel_engine_cs *engine = rq->engine;
1564 enum intel_engine_id id;
1565 const int num_rings =
1566 /* Use an extended w/a on gen7 if signalling from other rings */
1567 (HAS_LEGACY_SEMAPHORES(i915) && IS_GEN7(i915)) ?
1568 INTEL_INFO(i915)->num_rings - 1 :
1569 0;
1fc719d1 1570 bool force_restore = false;
8911a31c
CW
1571 int len;
1572 u32 *cs;
1573
1574 flags |= MI_MM_SPACE_GTT;
1575 if (IS_HASWELL(i915))
1576 /* These flags are for resource streamer on HSW+ */
1577 flags |= HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN;
1578 else
1579 flags |= MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN;
1580
1581 len = 4;
1582 if (IS_GEN7(i915))
1583 len += 2 + (num_rings ? 4*num_rings + 6 : 0);
1fc719d1
CW
1584 if (flags & MI_FORCE_RESTORE) {
1585 GEM_BUG_ON(flags & MI_RESTORE_INHIBIT);
1586 flags &= ~MI_FORCE_RESTORE;
1587 force_restore = true;
1588 len += 2;
1589 }
8911a31c
CW
1590
1591 cs = intel_ring_begin(rq, len);
1592 if (IS_ERR(cs))
1593 return PTR_ERR(cs);
1594
1595 /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
1596 if (IS_GEN7(i915)) {
1597 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1598 if (num_rings) {
1599 struct intel_engine_cs *signaller;
1600
1601 *cs++ = MI_LOAD_REGISTER_IMM(num_rings);
1602 for_each_engine(signaller, i915, id) {
1603 if (signaller == engine)
1604 continue;
1605
1606 *cs++ = i915_mmio_reg_offset(
1607 RING_PSMI_CTL(signaller->mmio_base));
1608 *cs++ = _MASKED_BIT_ENABLE(
1609 GEN6_PSMI_SLEEP_MSG_DISABLE);
1610 }
1611 }
1612 }
1613
1fc719d1
CW
1614 if (force_restore) {
1615 /*
1616 * The HW doesn't handle being told to restore the current
1617 * context very well. Quite often it likes goes to go off and
1618 * sulk, especially when it is meant to be reloading PP_DIR.
1619 * A very simple fix to force the reload is to simply switch
1620 * away from the current context and back again.
1621 *
1622 * Note that the kernel_context will contain random state
1623 * following the INHIBIT_RESTORE. We accept this since we
1624 * never use the kernel_context state; it is merely a
1625 * placeholder we use to flush other contexts.
1626 */
1627 *cs++ = MI_SET_CONTEXT;
1628 *cs++ = i915_ggtt_offset(to_intel_context(i915->kernel_context,
1629 engine)->state) |
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
1643 if (IS_GEN7(i915)) {
1644 if (num_rings) {
1645 struct intel_engine_cs *signaller;
1646 i915_reg_t last_reg = {}; /* keep gcc quiet */
1647
1648 *cs++ = MI_LOAD_REGISTER_IMM(num_rings);
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);
1662 *cs++ = i915_ggtt_offset(engine->scratch);
1663 *cs++ = MI_NOOP;
1664 }
1665 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1666 }
1667
1668 intel_ring_advance(rq, cs);
1669
1670 return 0;
1671}
1672
e61e0f51 1673static int remap_l3(struct i915_request *rq, int slice)
8911a31c
CW
1674{
1675 u32 *cs, *remap_info = rq->i915->l3_parity.remap_info[slice];
1676 int i;
1677
1678 if (!remap_info)
1679 return 0;
1680
1681 cs = intel_ring_begin(rq, GEN7_L3LOG_SIZE/4 * 2 + 2);
1682 if (IS_ERR(cs))
1683 return PTR_ERR(cs);
1684
1685 /*
1686 * Note: We do not worry about the concurrent register cacheline hang
1687 * here because no other code should access these registers other than
1688 * at initialization time.
1689 */
1690 *cs++ = MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4);
1691 for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
1692 *cs++ = i915_mmio_reg_offset(GEN7_L3LOG(slice, i));
1693 *cs++ = remap_info[i];
1694 }
1695 *cs++ = MI_NOOP;
1696 intel_ring_advance(rq, cs);
1697
1698 return 0;
1699}
1700
e61e0f51 1701static int switch_context(struct i915_request *rq)
8911a31c
CW
1702{
1703 struct intel_engine_cs *engine = rq->engine;
b3ee09a4
CW
1704 struct i915_gem_context *ctx = rq->gem_context;
1705 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
1706 unsigned int unwind_mm = 0;
8911a31c
CW
1707 u32 hw_flags = 0;
1708 int ret, i;
1709
1710 lockdep_assert_held(&rq->i915->drm.struct_mutex);
1711 GEM_BUG_ON(HAS_EXECLISTS(rq->i915));
1712
b3ee09a4 1713 if (ppgtt) {
e2a13d1b
CW
1714 int loops;
1715
1716 /*
1717 * Baytail takes a little more convincing that it really needs
1718 * to reload the PD between contexts. It is not just a little
1719 * longer, as adding more stalls after the load_pd_dir (i.e.
1720 * adding a long loop around flush_pd_dir) is not as effective
1721 * as reloading the PD umpteen times. 32 is derived from
1722 * experimentation (gem_exec_parallel/fds) and has no good
1723 * explanation.
1724 */
1725 loops = 1;
1726 if (engine->id == BCS && IS_VALLEYVIEW(engine->i915))
1727 loops = 32;
1728
1729 do {
1730 ret = load_pd_dir(rq, ppgtt);
1731 if (ret)
1732 goto err;
1733 } while (--loops);
8911a31c 1734
b3ee09a4
CW
1735 if (intel_engine_flag(engine) & ppgtt->pd_dirty_rings) {
1736 unwind_mm = intel_engine_flag(engine);
1737 ppgtt->pd_dirty_rings &= ~unwind_mm;
1738 hw_flags = MI_FORCE_RESTORE;
1739 }
8911a31c
CW
1740 }
1741
b3ee09a4 1742 if (rq->hw_context->state) {
8911a31c
CW
1743 GEM_BUG_ON(engine->id != RCS);
1744
1745 /*
1746 * The kernel context(s) is treated as pure scratch and is not
1747 * expected to retain any state (as we sacrifice it during
1748 * suspend and on resume it may be corrupted). This is ok,
1749 * as nothing actually executes using the kernel context; it
1750 * is purely used for flushing user contexts.
1751 */
b3ee09a4 1752 if (i915_gem_context_is_kernel(ctx))
8911a31c
CW
1753 hw_flags = MI_RESTORE_INHIBIT;
1754
1755 ret = mi_set_context(rq, hw_flags);
1756 if (ret)
1757 goto err_mm;
8911a31c 1758 }
8911a31c 1759
d9d117e4 1760 if (ppgtt) {
06348d30
CW
1761 ret = engine->emit_flush(rq, EMIT_INVALIDATE);
1762 if (ret)
1763 goto err_mm;
1764
d9d117e4
CW
1765 ret = flush_pd_dir(rq);
1766 if (ret)
1767 goto err_mm;
06348d30
CW
1768
1769 /*
1770 * Not only do we need a full barrier (post-sync write) after
1771 * invalidating the TLBs, but we need to wait a little bit
1772 * longer. Whether this is merely delaying us, or the
1773 * subsequent flush is a key part of serialising with the
1774 * post-sync op, this extra pass appears vital before a
1775 * mm switch!
1776 */
1777 ret = engine->emit_flush(rq, EMIT_INVALIDATE);
1778 if (ret)
1779 goto err_mm;
1780
1781 ret = engine->emit_flush(rq, EMIT_FLUSH);
1782 if (ret)
1783 goto err_mm;
8911a31c
CW
1784 }
1785
b3ee09a4 1786 if (ctx->remap_slice) {
8911a31c 1787 for (i = 0; i < MAX_L3_SLICES; i++) {
b3ee09a4 1788 if (!(ctx->remap_slice & BIT(i)))
8911a31c
CW
1789 continue;
1790
1791 ret = remap_l3(rq, i);
1792 if (ret)
b3ee09a4 1793 goto err_mm;
8911a31c
CW
1794 }
1795
b3ee09a4 1796 ctx->remap_slice = 0;
8911a31c
CW
1797 }
1798
1799 return 0;
1800
8911a31c 1801err_mm:
b3ee09a4
CW
1802 if (unwind_mm)
1803 ppgtt->pd_dirty_rings |= unwind_mm;
8911a31c
CW
1804err:
1805 return ret;
1806}
1807
e61e0f51 1808static int ring_request_alloc(struct i915_request *request)
9d773091 1809{
fd138212 1810 int ret;
6310346e 1811
1fc44d9b 1812 GEM_BUG_ON(!request->hw_context->pin_count);
e8a9c58f 1813
6310346e
CW
1814 /* Flush enough space to reduce the likelihood of waiting after
1815 * we start building the request - in which case we will just
1816 * have to repeat work.
1817 */
a0442461 1818 request->reserved_space += LEGACY_REQUEST_SIZE;
6310346e 1819
fd138212
CW
1820 ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
1821 if (ret)
1822 return ret;
6310346e 1823
8911a31c 1824 ret = switch_context(request);
3fef5cda
CW
1825 if (ret)
1826 return ret;
1827
a0442461 1828 request->reserved_space -= LEGACY_REQUEST_SIZE;
6310346e 1829 return 0;
9d773091
CW
1830}
1831
fd138212 1832static noinline int wait_for_space(struct intel_ring *ring, unsigned int bytes)
987046ad 1833{
e61e0f51 1834 struct i915_request *target;
e95433c7
CW
1835 long timeout;
1836
fd138212 1837 lockdep_assert_held(&ring->vma->vm->i915->drm.struct_mutex);
987046ad 1838
95aebcb2 1839 if (intel_ring_update_space(ring) >= bytes)
987046ad
CW
1840 return 0;
1841
36620032 1842 GEM_BUG_ON(list_empty(&ring->request_list));
675d9ad7 1843 list_for_each_entry(target, &ring->request_list, ring_link) {
987046ad 1844 /* Would completion of this request free enough space? */
605d5b32
CW
1845 if (bytes <= __intel_ring_space(target->postfix,
1846 ring->emit, ring->size))
987046ad 1847 break;
79bbcc29 1848 }
29b1b415 1849
675d9ad7 1850 if (WARN_ON(&target->ring_link == &ring->request_list))
987046ad
CW
1851 return -ENOSPC;
1852
e61e0f51 1853 timeout = i915_request_wait(target,
e95433c7
CW
1854 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1855 MAX_SCHEDULE_TIMEOUT);
1856 if (timeout < 0)
1857 return timeout;
7da844c5 1858
e61e0f51 1859 i915_request_retire_upto(target);
7da844c5
CW
1860
1861 intel_ring_update_space(ring);
1862 GEM_BUG_ON(ring->space < bytes);
1863 return 0;
29b1b415
JH
1864}
1865
fd138212
CW
1866int intel_ring_wait_for_space(struct intel_ring *ring, unsigned int bytes)
1867{
1868 GEM_BUG_ON(bytes > ring->effective_size);
1869 if (unlikely(bytes > ring->effective_size - ring->emit))
1870 bytes += ring->size - ring->emit;
1871
1872 if (unlikely(bytes > ring->space)) {
1873 int ret = wait_for_space(ring, bytes);
1874 if (unlikely(ret))
1875 return ret;
1876 }
1877
1878 GEM_BUG_ON(ring->space < bytes);
1879 return 0;
1880}
1881
e61e0f51 1882u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
cbcc80df 1883{
e61e0f51 1884 struct intel_ring *ring = rq->ring;
5e5655c3
CW
1885 const unsigned int remain_usable = ring->effective_size - ring->emit;
1886 const unsigned int bytes = num_dwords * sizeof(u32);
1887 unsigned int need_wrap = 0;
1888 unsigned int total_bytes;
73dec95e 1889 u32 *cs;
29b1b415 1890
6492ca79
CW
1891 /* Packets must be qword aligned. */
1892 GEM_BUG_ON(num_dwords & 1);
1893
e61e0f51 1894 total_bytes = bytes + rq->reserved_space;
5e5655c3 1895 GEM_BUG_ON(total_bytes > ring->effective_size);
29b1b415 1896
5e5655c3
CW
1897 if (unlikely(total_bytes > remain_usable)) {
1898 const int remain_actual = ring->size - ring->emit;
1899
1900 if (bytes > remain_usable) {
1901 /*
1902 * Not enough space for the basic request. So need to
1903 * flush out the remainder and then wait for
1904 * base + reserved.
1905 */
1906 total_bytes += remain_actual;
1907 need_wrap = remain_actual | 1;
1908 } else {
1909 /*
1910 * The base request will fit but the reserved space
1911 * falls off the end. So we don't need an immediate
1912 * wrap and only need to effectively wait for the
1913 * reserved size from the start of ringbuffer.
1914 */
e61e0f51 1915 total_bytes = rq->reserved_space + remain_actual;
5e5655c3 1916 }
cbcc80df
MK
1917 }
1918
5e5655c3 1919 if (unlikely(total_bytes > ring->space)) {
fd138212
CW
1920 int ret;
1921
1922 /*
1923 * Space is reserved in the ringbuffer for finalising the
1924 * request, as that cannot be allowed to fail. During request
1925 * finalisation, reserved_space is set to 0 to stop the
1926 * overallocation and the assumption is that then we never need
1927 * to wait (which has the risk of failing with EINTR).
1928 *
e61e0f51 1929 * See also i915_request_alloc() and i915_request_add().
fd138212 1930 */
e61e0f51 1931 GEM_BUG_ON(!rq->reserved_space);
fd138212
CW
1932
1933 ret = wait_for_space(ring, total_bytes);
cbcc80df 1934 if (unlikely(ret))
73dec95e 1935 return ERR_PTR(ret);
cbcc80df
MK
1936 }
1937
987046ad 1938 if (unlikely(need_wrap)) {
5e5655c3
CW
1939 need_wrap &= ~1;
1940 GEM_BUG_ON(need_wrap > ring->space);
1941 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
46b86332 1942 GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
78501eac 1943
987046ad 1944 /* Fill the tail with MI_NOOP */
46b86332 1945 memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
5e5655c3 1946 ring->space -= need_wrap;
46b86332 1947 ring->emit = 0;
987046ad 1948 }
304d695c 1949
e6ba9992 1950 GEM_BUG_ON(ring->emit > ring->size - bytes);
605d5b32 1951 GEM_BUG_ON(ring->space < bytes);
e6ba9992 1952 cs = ring->vaddr + ring->emit;
46b86332 1953 GEM_DEBUG_EXEC(memset32(cs, POISON_INUSE, bytes / sizeof(*cs)));
e6ba9992 1954 ring->emit += bytes;
1dae2dfb 1955 ring->space -= bytes;
73dec95e
TU
1956
1957 return cs;
8187a2b7 1958}
78501eac 1959
753b1ad4 1960/* Align the ring tail to a cacheline boundary */
e61e0f51 1961int intel_ring_cacheline_align(struct i915_request *rq)
753b1ad4 1962{
1f177a13
CW
1963 int num_dwords;
1964 void *cs;
753b1ad4 1965
1f177a13 1966 num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
753b1ad4
VS
1967 if (num_dwords == 0)
1968 return 0;
1969
1f177a13
CW
1970 num_dwords = CACHELINE_DWORDS - num_dwords;
1971 GEM_BUG_ON(num_dwords & 1);
1972
e61e0f51 1973 cs = intel_ring_begin(rq, num_dwords);
73dec95e
TU
1974 if (IS_ERR(cs))
1975 return PTR_ERR(cs);
753b1ad4 1976
1f177a13 1977 memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
e61e0f51 1978 intel_ring_advance(rq, cs);
753b1ad4 1979
1f177a13 1980 GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));
753b1ad4
VS
1981 return 0;
1982}
1983
e61e0f51 1984static void gen6_bsd_submit_request(struct i915_request *request)
881f47b6 1985{
c5efa1ad 1986 struct drm_i915_private *dev_priv = request->i915;
881f47b6 1987
76f8421f
CW
1988 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
1989
881f47b6 1990 /* Every tail move must follow the sequence below */
12f55818
CW
1991
1992 /* Disable notification that the ring is IDLE. The GT
1993 * will then assume that it is busy and bring it out of rc6.
1994 */
76f8421f
CW
1995 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1996 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
12f55818
CW
1997
1998 /* Clear the context id. Here be magic! */
76f8421f 1999 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
0206e353 2000
12f55818 2001 /* Wait for the ring not to be idle, i.e. for it to wake up. */
02b312d0
CW
2002 if (__intel_wait_for_register_fw(dev_priv,
2003 GEN6_BSD_SLEEP_PSMI_CONTROL,
2004 GEN6_BSD_SLEEP_INDICATOR,
2005 0,
2006 1000, 0, NULL))
12f55818 2007 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
0206e353 2008
12f55818 2009 /* Now that the ring is fully powered up, update the tail */
b0411e7d 2010 i9xx_submit_request(request);
12f55818
CW
2011
2012 /* Let the ring send IDLE messages to the GT again,
2013 * and so let it sleep to conserve power when idle.
2014 */
76f8421f
CW
2015 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2016 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2017
2018 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
881f47b6
XH
2019}
2020
06348d30 2021static int mi_flush_dw(struct i915_request *rq, u32 flags)
881f47b6 2022{
73dec95e 2023 u32 cmd, *cs;
b72f3acb 2024
e61e0f51 2025 cs = intel_ring_begin(rq, 4);
73dec95e
TU
2026 if (IS_ERR(cs))
2027 return PTR_ERR(cs);
b72f3acb 2028
71a77e07 2029 cmd = MI_FLUSH_DW;
f0a1fb10 2030
70b73f9a
CW
2031 /*
2032 * We always require a command barrier so that subsequent
f0a1fb10
CW
2033 * commands, such as breadcrumb interrupts, are strictly ordered
2034 * wrt the contents of the write cache being flushed to memory
2035 * (and thus being coherent from the CPU).
2036 */
2037 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2038
9a289771 2039 /*
70b73f9a 2040 * Bspec vol 1c.3 - blitter engine command streamer:
9a289771
JB
2041 * "If ENABLED, all TLBs will be invalidated once the flush
2042 * operation is complete. This bit is only valid when the
2043 * Post-Sync Operation field is a value of 1h or 3h."
2044 */
70b73f9a 2045 cmd |= flags;
f0a1fb10 2046
73dec95e
TU
2047 *cs++ = cmd;
2048 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
79e6770c 2049 *cs++ = 0;
73dec95e 2050 *cs++ = MI_NOOP;
70b73f9a 2051
e61e0f51 2052 intel_ring_advance(rq, cs);
70b73f9a 2053
1c7a0623
BW
2054 return 0;
2055}
2056
70b73f9a
CW
2057static int gen6_flush_dw(struct i915_request *rq, u32 mode, u32 invflags)
2058{
06348d30 2059 return mi_flush_dw(rq, mode & EMIT_INVALIDATE ? invflags : 0);
70b73f9a
CW
2060}
2061
2062static int gen6_bsd_ring_flush(struct i915_request *rq, u32 mode)
2063{
2064 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB | MI_INVALIDATE_BSD);
2065}
2066
d7d4eedd 2067static int
e61e0f51 2068hsw_emit_bb_start(struct i915_request *rq,
803688ba
CW
2069 u64 offset, u32 len,
2070 unsigned int dispatch_flags)
d7d4eedd 2071{
73dec95e 2072 u32 *cs;
d7d4eedd 2073
e61e0f51 2074 cs = intel_ring_begin(rq, 2);
73dec95e
TU
2075 if (IS_ERR(cs))
2076 return PTR_ERR(cs);
d7d4eedd 2077
73dec95e 2078 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
08e3e21a 2079 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW);
d7d4eedd 2080 /* bit0-7 is the length on GEN6+ */
73dec95e 2081 *cs++ = offset;
e61e0f51 2082 intel_ring_advance(rq, cs);
d7d4eedd
CW
2083
2084 return 0;
2085}
2086
881f47b6 2087static int
e61e0f51 2088gen6_emit_bb_start(struct i915_request *rq,
803688ba
CW
2089 u64 offset, u32 len,
2090 unsigned int dispatch_flags)
881f47b6 2091{
73dec95e 2092 u32 *cs;
ab6f8e32 2093
e61e0f51 2094 cs = intel_ring_begin(rq, 2);
73dec95e
TU
2095 if (IS_ERR(cs))
2096 return PTR_ERR(cs);
e1f99ce6 2097
73dec95e
TU
2098 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
2099 0 : MI_BATCH_NON_SECURE_I965);
0206e353 2100 /* bit0-7 is the length on GEN6+ */
73dec95e 2101 *cs++ = offset;
e61e0f51 2102 intel_ring_advance(rq, cs);
ab6f8e32 2103
0206e353 2104 return 0;
881f47b6
XH
2105}
2106
549f7365
CW
2107/* Blitter support (SandyBridge+) */
2108
e61e0f51 2109static int gen6_ring_flush(struct i915_request *rq, u32 mode)
8d19215b 2110{
70b73f9a 2111 return gen6_flush_dw(rq, mode, MI_INVALIDATE_TLB);
8d19215b
ZN
2112}
2113
d9a64610
TU
2114static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2115 struct intel_engine_cs *engine)
2116{
79e6770c 2117 int i;
db3d4019 2118
93c6e966 2119 if (!HAS_LEGACY_SEMAPHORES(dev_priv))
db3d4019
TU
2120 return;
2121
79e6770c
CW
2122 GEM_BUG_ON(INTEL_GEN(dev_priv) < 6);
2123 engine->semaphore.sync_to = gen6_ring_sync_to;
2124 engine->semaphore.signal = gen6_signal;
51d545d0 2125
79e6770c
CW
2126 /*
2127 * The current semaphore is only applied on pre-gen8
2128 * platform. And there is no VCS2 ring on the pre-gen8
2129 * platform. So the semaphore between RCS and VCS2 is
2130 * initialized as INVALID.
2131 */
2132 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
2133 static const struct {
4b8e38a9
TU
2134 u32 wait_mbox;
2135 i915_reg_t mbox_reg;
79e6770c
CW
2136 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
2137 [RCS_HW] = {
2138 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2139 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2140 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
2141 },
2142 [VCS_HW] = {
2143 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2144 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2145 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
2146 },
2147 [BCS_HW] = {
2148 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2149 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2150 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
2151 },
2152 [VECS_HW] = {
2153 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2154 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2155 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
2156 },
2157 };
2158 u32 wait_mbox;
2159 i915_reg_t mbox_reg;
4b8e38a9 2160
79e6770c
CW
2161 if (i == engine->hw_id) {
2162 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2163 mbox_reg = GEN6_NOSYNC;
2164 } else {
2165 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
2166 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
4b8e38a9 2167 }
51d545d0 2168
79e6770c
CW
2169 engine->semaphore.mbox.wait[i] = wait_mbox;
2170 engine->semaphore.mbox.signal[i] = mbox_reg;
2171 }
d9a64610
TU
2172}
2173
ed003078
CW
2174static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2175 struct intel_engine_cs *engine)
2176{
79e6770c 2177 if (INTEL_GEN(dev_priv) >= 6) {
31bb59cc
CW
2178 engine->irq_enable = gen6_irq_enable;
2179 engine->irq_disable = gen6_irq_disable;
ed003078
CW
2180 engine->irq_seqno_barrier = gen6_seqno_barrier;
2181 } else if (INTEL_GEN(dev_priv) >= 5) {
31bb59cc
CW
2182 engine->irq_enable = gen5_irq_enable;
2183 engine->irq_disable = gen5_irq_disable;
f8973c21 2184 engine->irq_seqno_barrier = gen5_seqno_barrier;
ed003078 2185 } else if (INTEL_GEN(dev_priv) >= 3) {
31bb59cc
CW
2186 engine->irq_enable = i9xx_irq_enable;
2187 engine->irq_disable = i9xx_irq_disable;
ed003078 2188 } else {
31bb59cc
CW
2189 engine->irq_enable = i8xx_irq_enable;
2190 engine->irq_disable = i8xx_irq_disable;
ed003078
CW
2191 }
2192}
2193
ff44ad51
CW
2194static void i9xx_set_default_submission(struct intel_engine_cs *engine)
2195{
2196 engine->submit_request = i9xx_submit_request;
27a5f61b 2197 engine->cancel_requests = cancel_requests;
aba5e278
CW
2198
2199 engine->park = NULL;
2200 engine->unpark = NULL;
ff44ad51
CW
2201}
2202
2203static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
2204{
aba5e278 2205 i9xx_set_default_submission(engine);
ff44ad51
CW
2206 engine->submit_request = gen6_bsd_submit_request;
2207}
2208
06a2fe22
TU
2209static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2210 struct intel_engine_cs *engine)
2211{
79e6770c
CW
2212 /* gen8+ are only supported with execlists */
2213 GEM_BUG_ON(INTEL_GEN(dev_priv) >= 8);
2214
618e4ca7
CW
2215 intel_ring_init_irq(dev_priv, engine);
2216 intel_ring_init_semaphores(dev_priv, engine);
2217
1d8a1337 2218 engine->init_hw = init_ring_common;
5adfb772
CW
2219 engine->reset.prepare = reset_prepare;
2220 engine->reset.reset = reset_ring;
2221 engine->reset.finish = reset_finish;
7445a2a4 2222
e8a9c58f 2223 engine->context_pin = intel_ring_context_pin;
f73e7399
CW
2224 engine->request_alloc = ring_request_alloc;
2225
9b81d556 2226 engine->emit_breadcrumb = i9xx_emit_breadcrumb;
98f29e8d 2227 engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
93c6e966 2228 if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
98f29e8d
CW
2229 int num_rings;
2230
9b81d556 2231 engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
98f29e8d 2232
c58949f4 2233 num_rings = INTEL_INFO(dev_priv)->num_rings - 1;
79e6770c
CW
2234 engine->emit_breadcrumb_sz += num_rings * 3;
2235 if (num_rings & 1)
2236 engine->emit_breadcrumb_sz++;
98f29e8d 2237 }
ff44ad51
CW
2238
2239 engine->set_default_submission = i9xx_set_default_submission;
6f7bef75 2240
79e6770c 2241 if (INTEL_GEN(dev_priv) >= 6)
803688ba 2242 engine->emit_bb_start = gen6_emit_bb_start;
6f7bef75 2243 else if (INTEL_GEN(dev_priv) >= 4)
803688ba 2244 engine->emit_bb_start = i965_emit_bb_start;
2a307c2e 2245 else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
803688ba 2246 engine->emit_bb_start = i830_emit_bb_start;
6f7bef75 2247 else
803688ba 2248 engine->emit_bb_start = i915_emit_bb_start;
06a2fe22
TU
2249}
2250
8b3e2d36 2251int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
5c1143bb 2252{
8b3e2d36 2253 struct drm_i915_private *dev_priv = engine->i915;
3e78998a 2254 int ret;
5c1143bb 2255
06a2fe22
TU
2256 intel_ring_default_vfuncs(dev_priv, engine);
2257
61ff75ac
CW
2258 if (HAS_L3_DPF(dev_priv))
2259 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
f8973c21 2260
fa6f071d
DCS
2261 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2262
79e6770c 2263 if (INTEL_GEN(dev_priv) >= 6) {
e2f80391 2264 engine->init_context = intel_rcs_ctx_init;
c7fe7d25 2265 engine->emit_flush = gen7_render_ring_flush;
c033666a 2266 if (IS_GEN6(dev_priv))
c7fe7d25 2267 engine->emit_flush = gen6_render_ring_flush;
c033666a 2268 } else if (IS_GEN5(dev_priv)) {
c7fe7d25 2269 engine->emit_flush = gen4_render_ring_flush;
59465b5f 2270 } else {
c033666a 2271 if (INTEL_GEN(dev_priv) < 4)
c7fe7d25 2272 engine->emit_flush = gen2_render_ring_flush;
46f0f8d1 2273 else
c7fe7d25 2274 engine->emit_flush = gen4_render_ring_flush;
e2f80391 2275 engine->irq_enable_mask = I915_USER_INTERRUPT;
1ec14ad3 2276 }
707d9cf9 2277
c033666a 2278 if (IS_HASWELL(dev_priv))
803688ba 2279 engine->emit_bb_start = hsw_emit_bb_start;
6f7bef75 2280
e2f80391 2281 engine->init_hw = init_render_ring;
59465b5f 2282
acd27845 2283 ret = intel_init_ring_buffer(engine);
99be1dfe
SV
2284 if (ret)
2285 return ret;
2286
99be1dfe 2287 return 0;
5c1143bb
XH
2288}
2289
8b3e2d36 2290int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
5c1143bb 2291{
8b3e2d36 2292 struct drm_i915_private *dev_priv = engine->i915;
58fa3835 2293
06a2fe22
TU
2294 intel_ring_default_vfuncs(dev_priv, engine);
2295
c033666a 2296 if (INTEL_GEN(dev_priv) >= 6) {
0fd2c201 2297 /* gen6 bsd needs a special wa for tail updates */
c033666a 2298 if (IS_GEN6(dev_priv))
ff44ad51 2299 engine->set_default_submission = gen6_bsd_set_default_submission;
c7fe7d25 2300 engine->emit_flush = gen6_bsd_ring_flush;
79e6770c 2301 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
58fa3835 2302 } else {
c7fe7d25 2303 engine->emit_flush = bsd_ring_flush;
8d228911 2304 if (IS_GEN5(dev_priv))
e2f80391 2305 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
8d228911 2306 else
e2f80391 2307 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
58fa3835 2308 }
58fa3835 2309
acd27845 2310 return intel_init_ring_buffer(engine);
5c1143bb 2311}
549f7365 2312
8b3e2d36 2313int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
549f7365 2314{
8b3e2d36 2315 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2316
2317 intel_ring_default_vfuncs(dev_priv, engine);
2318
c7fe7d25 2319 engine->emit_flush = gen6_ring_flush;
79e6770c 2320 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
549f7365 2321
acd27845 2322 return intel_init_ring_buffer(engine);
549f7365 2323}
a7b9761d 2324
8b3e2d36 2325int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
9a8a2213 2326{
8b3e2d36 2327 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2328
2329 intel_ring_default_vfuncs(dev_priv, engine);
2330
c7fe7d25 2331 engine->emit_flush = gen6_ring_flush;
79e6770c
CW
2332 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2333 engine->irq_enable = hsw_vebox_irq_enable;
2334 engine->irq_disable = hsw_vebox_irq_disable;
9a8a2213 2335
acd27845 2336 return intel_init_ring_buffer(engine);
9a8a2213 2337}
This page took 1.653024 seconds and 4 git commands to generate.