]> Git Repo - linux.git/blame - drivers/gpu/drm/i915/intel_ringbuffer.c
drm/i915: Split request submit/execute phase into two
[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>
760285e7 31#include <drm/drmP.h>
62fdfeaf 32#include "i915_drv.h"
760285e7 33#include <drm/i915_drm.h>
62fdfeaf 34#include "i915_trace.h"
881f47b6 35#include "intel_drv.h"
62fdfeaf 36
a0442461
CW
37/* Rough estimate of the typical request size, performing a flush,
38 * set-context and then emitting the batch.
39 */
40#define LEGACY_REQUEST_SIZE 200
41
82e104cc 42int __intel_ring_space(int head, int tail, int size)
c7dca47b 43{
4f54741e
DG
44 int space = head - tail;
45 if (space <= 0)
1cf0ba14 46 space += size;
4f54741e 47 return space - I915_RING_FREE_SPACE;
c7dca47b
CW
48}
49
32c04f16 50void intel_ring_update_space(struct intel_ring *ring)
ebd0fd4b 51{
32c04f16
CW
52 if (ring->last_retired_head != -1) {
53 ring->head = ring->last_retired_head;
54 ring->last_retired_head = -1;
ebd0fd4b
DG
55 }
56
32c04f16
CW
57 ring->space = __intel_ring_space(ring->head & HEAD_ADDR,
58 ring->tail, ring->size);
ebd0fd4b
DG
59}
60
b72f3acb 61static int
7c9cf4e3 62gen2_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
46f0f8d1 63{
7e37f889 64 struct intel_ring *ring = req->ring;
46f0f8d1
CW
65 u32 cmd;
66 int ret;
67
68 cmd = MI_FLUSH;
46f0f8d1 69
7c9cf4e3 70 if (mode & EMIT_INVALIDATE)
46f0f8d1
CW
71 cmd |= MI_READ_FLUSH;
72
5fb9de1a 73 ret = intel_ring_begin(req, 2);
46f0f8d1
CW
74 if (ret)
75 return ret;
76
b5321f30
CW
77 intel_ring_emit(ring, cmd);
78 intel_ring_emit(ring, MI_NOOP);
79 intel_ring_advance(ring);
46f0f8d1
CW
80
81 return 0;
82}
83
84static int
7c9cf4e3 85gen4_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
62fdfeaf 86{
7e37f889 87 struct intel_ring *ring = req->ring;
6f392d54 88 u32 cmd;
b72f3acb 89 int ret;
6f392d54 90
36d527de
CW
91 /*
92 * read/write caches:
93 *
94 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
95 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
96 * also flushed at 2d versus 3d pipeline switches.
97 *
98 * read-only caches:
99 *
100 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
101 * MI_READ_FLUSH is set, and is always flushed on 965.
102 *
103 * I915_GEM_DOMAIN_COMMAND may not exist?
104 *
105 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
106 * invalidated when MI_EXE_FLUSH is set.
107 *
108 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
109 * invalidated with every MI_FLUSH.
110 *
111 * TLBs:
112 *
113 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
114 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
115 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
116 * are flushed at any MI_FLUSH.
117 */
118
b5321f30 119 cmd = MI_FLUSH;
7c9cf4e3 120 if (mode & EMIT_INVALIDATE) {
36d527de 121 cmd |= MI_EXE_FLUSH;
b5321f30
CW
122 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
123 cmd |= MI_INVALIDATE_ISP;
124 }
70eac33e 125
5fb9de1a 126 ret = intel_ring_begin(req, 2);
36d527de
CW
127 if (ret)
128 return ret;
b72f3acb 129
b5321f30
CW
130 intel_ring_emit(ring, cmd);
131 intel_ring_emit(ring, MI_NOOP);
132 intel_ring_advance(ring);
b72f3acb
CW
133
134 return 0;
8187a2b7
ZN
135}
136
8d315287
JB
137/**
138 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
139 * implementing two workarounds on gen6. From section 1.4.7.1
140 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
141 *
142 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
143 * produced by non-pipelined state commands), software needs to first
144 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
145 * 0.
146 *
147 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
148 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
149 *
150 * And the workaround for these two requires this workaround first:
151 *
152 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
153 * BEFORE the pipe-control with a post-sync op and no write-cache
154 * flushes.
155 *
156 * And this last workaround is tricky because of the requirements on
157 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
158 * volume 2 part 1:
159 *
160 * "1 of the following must also be set:
161 * - Render Target Cache Flush Enable ([12] of DW1)
162 * - Depth Cache Flush Enable ([0] of DW1)
163 * - Stall at Pixel Scoreboard ([1] of DW1)
164 * - Depth Stall ([13] of DW1)
165 * - Post-Sync Operation ([13] of DW1)
166 * - Notify Enable ([8] of DW1)"
167 *
168 * The cache flushes require the workaround flush that triggered this
169 * one, so we can't use it. Depth stall would trigger the same.
170 * Post-sync nonzero is what triggered this second workaround, so we
171 * can't use that one either. Notify enable is IRQs, which aren't
172 * really our business. That leaves only stall at scoreboard.
173 */
174static int
f2cf1fcc 175intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
8d315287 176{
7e37f889 177 struct intel_ring *ring = req->ring;
b5321f30 178 u32 scratch_addr =
bde13ebd 179 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
8d315287
JB
180 int ret;
181
5fb9de1a 182 ret = intel_ring_begin(req, 6);
8d315287
JB
183 if (ret)
184 return ret;
185
b5321f30
CW
186 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
187 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
8d315287 188 PIPE_CONTROL_STALL_AT_SCOREBOARD);
b5321f30
CW
189 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
190 intel_ring_emit(ring, 0); /* low dword */
191 intel_ring_emit(ring, 0); /* high dword */
192 intel_ring_emit(ring, MI_NOOP);
193 intel_ring_advance(ring);
8d315287 194
5fb9de1a 195 ret = intel_ring_begin(req, 6);
8d315287
JB
196 if (ret)
197 return ret;
198
b5321f30
CW
199 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
200 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
201 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
202 intel_ring_emit(ring, 0);
203 intel_ring_emit(ring, 0);
204 intel_ring_emit(ring, MI_NOOP);
205 intel_ring_advance(ring);
8d315287
JB
206
207 return 0;
208}
209
210static int
7c9cf4e3 211gen6_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
8d315287 212{
7e37f889 213 struct intel_ring *ring = req->ring;
b5321f30 214 u32 scratch_addr =
bde13ebd 215 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
8d315287 216 u32 flags = 0;
8d315287
JB
217 int ret;
218
b3111509 219 /* Force SNB workarounds for PIPE_CONTROL flushes */
f2cf1fcc 220 ret = intel_emit_post_sync_nonzero_flush(req);
b3111509
PZ
221 if (ret)
222 return ret;
223
8d315287
JB
224 /* Just flush everything. Experiments have shown that reducing the
225 * number of bits based on the write domains has little performance
226 * impact.
227 */
7c9cf4e3 228 if (mode & EMIT_FLUSH) {
7d54a904
CW
229 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
230 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
231 /*
232 * Ensure that any following seqno writes only happen
233 * when the render cache is indeed flushed.
234 */
97f209bc 235 flags |= PIPE_CONTROL_CS_STALL;
7d54a904 236 }
7c9cf4e3 237 if (mode & EMIT_INVALIDATE) {
7d54a904
CW
238 flags |= PIPE_CONTROL_TLB_INVALIDATE;
239 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
240 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
241 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
242 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
243 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
244 /*
245 * TLB invalidate requires a post-sync write.
246 */
3ac78313 247 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
7d54a904 248 }
8d315287 249
5fb9de1a 250 ret = intel_ring_begin(req, 4);
8d315287
JB
251 if (ret)
252 return ret;
253
b5321f30
CW
254 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
255 intel_ring_emit(ring, flags);
256 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
257 intel_ring_emit(ring, 0);
258 intel_ring_advance(ring);
8d315287
JB
259
260 return 0;
261}
262
f3987631 263static int
f2cf1fcc 264gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
f3987631 265{
7e37f889 266 struct intel_ring *ring = req->ring;
f3987631
PZ
267 int ret;
268
5fb9de1a 269 ret = intel_ring_begin(req, 4);
f3987631
PZ
270 if (ret)
271 return ret;
272
b5321f30
CW
273 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
274 intel_ring_emit(ring,
275 PIPE_CONTROL_CS_STALL |
276 PIPE_CONTROL_STALL_AT_SCOREBOARD);
277 intel_ring_emit(ring, 0);
278 intel_ring_emit(ring, 0);
279 intel_ring_advance(ring);
f3987631
PZ
280
281 return 0;
282}
283
4772eaeb 284static int
7c9cf4e3 285gen7_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
4772eaeb 286{
7e37f889 287 struct intel_ring *ring = req->ring;
b5321f30 288 u32 scratch_addr =
bde13ebd 289 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
4772eaeb 290 u32 flags = 0;
4772eaeb
PZ
291 int ret;
292
f3987631
PZ
293 /*
294 * Ensure that any following seqno writes only happen when the render
295 * cache is indeed flushed.
296 *
297 * Workaround: 4th PIPE_CONTROL command (except the ones with only
298 * read-cache invalidate bits set) must have the CS_STALL bit set. We
299 * don't try to be clever and just set it unconditionally.
300 */
301 flags |= PIPE_CONTROL_CS_STALL;
302
4772eaeb
PZ
303 /* Just flush everything. Experiments have shown that reducing the
304 * number of bits based on the write domains has little performance
305 * impact.
306 */
7c9cf4e3 307 if (mode & EMIT_FLUSH) {
4772eaeb
PZ
308 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
309 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
965fd602 310 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
40a24488 311 flags |= PIPE_CONTROL_FLUSH_ENABLE;
4772eaeb 312 }
7c9cf4e3 313 if (mode & EMIT_INVALIDATE) {
4772eaeb
PZ
314 flags |= PIPE_CONTROL_TLB_INVALIDATE;
315 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
316 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
317 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
318 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
319 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
148b83d0 320 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
4772eaeb
PZ
321 /*
322 * TLB invalidate requires a post-sync write.
323 */
324 flags |= PIPE_CONTROL_QW_WRITE;
b9e1faa7 325 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
f3987631 326
add284a3
CW
327 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
328
f3987631
PZ
329 /* Workaround: we must issue a pipe_control with CS-stall bit
330 * set before a pipe_control command that has the state cache
331 * invalidate bit set. */
f2cf1fcc 332 gen7_render_ring_cs_stall_wa(req);
4772eaeb
PZ
333 }
334
5fb9de1a 335 ret = intel_ring_begin(req, 4);
4772eaeb
PZ
336 if (ret)
337 return ret;
338
b5321f30
CW
339 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
340 intel_ring_emit(ring, flags);
341 intel_ring_emit(ring, scratch_addr);
342 intel_ring_emit(ring, 0);
343 intel_ring_advance(ring);
4772eaeb
PZ
344
345 return 0;
346}
347
884ceace 348static int
f2cf1fcc 349gen8_emit_pipe_control(struct drm_i915_gem_request *req,
884ceace
KG
350 u32 flags, u32 scratch_addr)
351{
7e37f889 352 struct intel_ring *ring = req->ring;
884ceace
KG
353 int ret;
354
5fb9de1a 355 ret = intel_ring_begin(req, 6);
884ceace
KG
356 if (ret)
357 return ret;
358
b5321f30
CW
359 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
360 intel_ring_emit(ring, flags);
361 intel_ring_emit(ring, scratch_addr);
362 intel_ring_emit(ring, 0);
363 intel_ring_emit(ring, 0);
364 intel_ring_emit(ring, 0);
365 intel_ring_advance(ring);
884ceace
KG
366
367 return 0;
368}
369
a5f3d68e 370static int
7c9cf4e3 371gen8_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
a5f3d68e 372{
56c0f1a7 373 u32 scratch_addr =
bde13ebd 374 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
b5321f30 375 u32 flags = 0;
02c9f7e3 376 int ret;
a5f3d68e
BW
377
378 flags |= PIPE_CONTROL_CS_STALL;
379
7c9cf4e3 380 if (mode & EMIT_FLUSH) {
a5f3d68e
BW
381 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
382 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
965fd602 383 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
40a24488 384 flags |= PIPE_CONTROL_FLUSH_ENABLE;
a5f3d68e 385 }
7c9cf4e3 386 if (mode & EMIT_INVALIDATE) {
a5f3d68e
BW
387 flags |= PIPE_CONTROL_TLB_INVALIDATE;
388 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
389 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
390 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
391 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
392 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
393 flags |= PIPE_CONTROL_QW_WRITE;
394 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
02c9f7e3
KG
395
396 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
f2cf1fcc 397 ret = gen8_emit_pipe_control(req,
02c9f7e3
KG
398 PIPE_CONTROL_CS_STALL |
399 PIPE_CONTROL_STALL_AT_SCOREBOARD,
400 0);
401 if (ret)
402 return ret;
a5f3d68e
BW
403 }
404
f2cf1fcc 405 return gen8_emit_pipe_control(req, flags, scratch_addr);
a5f3d68e
BW
406}
407
0bc40be8 408static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
035dc1e0 409{
c033666a 410 struct drm_i915_private *dev_priv = engine->i915;
035dc1e0
SV
411 u32 addr;
412
413 addr = dev_priv->status_page_dmah->busaddr;
c033666a 414 if (INTEL_GEN(dev_priv) >= 4)
035dc1e0
SV
415 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
416 I915_WRITE(HWS_PGA, addr);
417}
418
0bc40be8 419static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
af75f269 420{
c033666a 421 struct drm_i915_private *dev_priv = engine->i915;
f0f59a00 422 i915_reg_t mmio;
af75f269
DL
423
424 /* The ring status page addresses are no longer next to the rest of
425 * the ring registers as of gen7.
426 */
c033666a 427 if (IS_GEN7(dev_priv)) {
0bc40be8 428 switch (engine->id) {
af75f269
DL
429 case RCS:
430 mmio = RENDER_HWS_PGA_GEN7;
431 break;
432 case BCS:
433 mmio = BLT_HWS_PGA_GEN7;
434 break;
435 /*
436 * VCS2 actually doesn't exist on Gen7. Only shut up
437 * gcc switch check warning
438 */
439 case VCS2:
440 case VCS:
441 mmio = BSD_HWS_PGA_GEN7;
442 break;
443 case VECS:
444 mmio = VEBOX_HWS_PGA_GEN7;
445 break;
446 }
c033666a 447 } else if (IS_GEN6(dev_priv)) {
0bc40be8 448 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
af75f269
DL
449 } else {
450 /* XXX: gen8 returns to sanity */
0bc40be8 451 mmio = RING_HWS_PGA(engine->mmio_base);
af75f269
DL
452 }
453
57e88531 454 I915_WRITE(mmio, engine->status_page.ggtt_offset);
af75f269
DL
455 POSTING_READ(mmio);
456
457 /*
458 * Flush the TLB for this page
459 *
460 * FIXME: These two bits have disappeared on gen8, so a question
461 * arises: do we still need this and if so how should we go about
462 * invalidating the TLB?
463 */
ac657f64 464 if (IS_GEN(dev_priv, 6, 7)) {
0bc40be8 465 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
af75f269
DL
466
467 /* ring should be idle before issuing a sync flush*/
0bc40be8 468 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
af75f269
DL
469
470 I915_WRITE(reg,
471 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
472 INSTPM_SYNC_FLUSH));
25ab57f4
CW
473 if (intel_wait_for_register(dev_priv,
474 reg, INSTPM_SYNC_FLUSH, 0,
475 1000))
af75f269 476 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
0bc40be8 477 engine->name);
af75f269
DL
478 }
479}
480
0bc40be8 481static bool stop_ring(struct intel_engine_cs *engine)
8187a2b7 482{
c033666a 483 struct drm_i915_private *dev_priv = engine->i915;
8187a2b7 484
21a2c58a 485 if (INTEL_GEN(dev_priv) > 2) {
0bc40be8 486 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
3d808eb1
CW
487 if (intel_wait_for_register(dev_priv,
488 RING_MI_MODE(engine->mmio_base),
489 MODE_IDLE,
490 MODE_IDLE,
491 1000)) {
0bc40be8
TU
492 DRM_ERROR("%s : timed out trying to stop ring\n",
493 engine->name);
9bec9b13
CW
494 /* Sometimes we observe that the idle flag is not
495 * set even though the ring is empty. So double
496 * check before giving up.
497 */
0bc40be8 498 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
9bec9b13 499 return false;
9991ae78
CW
500 }
501 }
b7884eb4 502
0bc40be8
TU
503 I915_WRITE_CTL(engine, 0);
504 I915_WRITE_HEAD(engine, 0);
c5efa1ad 505 I915_WRITE_TAIL(engine, 0);
8187a2b7 506
21a2c58a 507 if (INTEL_GEN(dev_priv) > 2) {
0bc40be8
TU
508 (void)I915_READ_CTL(engine);
509 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
9991ae78 510 }
a51435a3 511
0bc40be8 512 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
9991ae78 513}
8187a2b7 514
0bc40be8 515static int init_ring_common(struct intel_engine_cs *engine)
9991ae78 516{
c033666a 517 struct drm_i915_private *dev_priv = engine->i915;
7e37f889 518 struct intel_ring *ring = engine->buffer;
9991ae78
CW
519 int ret = 0;
520
59bad947 521 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
9991ae78 522
0bc40be8 523 if (!stop_ring(engine)) {
9991ae78 524 /* G45 ring initialization often fails to reset head to zero */
6fd0d56e
CW
525 DRM_DEBUG_KMS("%s head not reset to zero "
526 "ctl %08x head %08x tail %08x start %08x\n",
0bc40be8
TU
527 engine->name,
528 I915_READ_CTL(engine),
529 I915_READ_HEAD(engine),
530 I915_READ_TAIL(engine),
531 I915_READ_START(engine));
8187a2b7 532
0bc40be8 533 if (!stop_ring(engine)) {
6fd0d56e
CW
534 DRM_ERROR("failed to set %s head to zero "
535 "ctl %08x head %08x tail %08x start %08x\n",
0bc40be8
TU
536 engine->name,
537 I915_READ_CTL(engine),
538 I915_READ_HEAD(engine),
539 I915_READ_TAIL(engine),
540 I915_READ_START(engine));
9991ae78
CW
541 ret = -EIO;
542 goto out;
6fd0d56e 543 }
8187a2b7
ZN
544 }
545
3177659a 546 if (HWS_NEEDS_PHYSICAL(dev_priv))
0bc40be8 547 ring_setup_phys_status_page(engine);
3177659a
CS
548 else
549 intel_ring_setup_status_page(engine);
9991ae78 550
ad07dfcd 551 intel_engine_reset_breadcrumbs(engine);
821ed7df 552
ece4a17d 553 /* Enforce ordering by reading HEAD register back */
0bc40be8 554 I915_READ_HEAD(engine);
ece4a17d 555
0d8957c8
SV
556 /* Initialize the ring. This must happen _after_ we've cleared the ring
557 * registers with the above sequence (the readback of the HEAD registers
558 * also enforces ordering), otherwise the hw might lose the new ring
559 * register values. */
bde13ebd 560 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
95468892
CW
561
562 /* WaClearRingBufHeadRegAtInit:ctg,elk */
0bc40be8 563 if (I915_READ_HEAD(engine))
95468892 564 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
0bc40be8 565 engine->name, I915_READ_HEAD(engine));
821ed7df
CW
566
567 intel_ring_update_space(ring);
568 I915_WRITE_HEAD(engine, ring->head);
569 I915_WRITE_TAIL(engine, ring->tail);
570 (void)I915_READ_TAIL(engine);
95468892 571
62ae14b1 572 I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
8187a2b7 573
8187a2b7 574 /* If the head is still not zero, the ring is dead */
821ed7df
CW
575 if (intel_wait_for_register_fw(dev_priv, RING_CTL(engine->mmio_base),
576 RING_VALID, RING_VALID,
577 50)) {
e74cfed5 578 DRM_ERROR("%s initialization failed "
821ed7df 579 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
0bc40be8
TU
580 engine->name,
581 I915_READ_CTL(engine),
582 I915_READ_CTL(engine) & RING_VALID,
821ed7df
CW
583 I915_READ_HEAD(engine), ring->head,
584 I915_READ_TAIL(engine), ring->tail,
0bc40be8 585 I915_READ_START(engine),
bde13ebd 586 i915_ggtt_offset(ring->vma));
b7884eb4
SV
587 ret = -EIO;
588 goto out;
8187a2b7
ZN
589 }
590
fc0768ce 591 intel_engine_init_hangcheck(engine);
50f018df 592
b7884eb4 593out:
59bad947 594 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b7884eb4
SV
595
596 return ret;
8187a2b7
ZN
597}
598
821ed7df
CW
599static void reset_ring_common(struct intel_engine_cs *engine,
600 struct drm_i915_gem_request *request)
601{
602 struct intel_ring *ring = request->ring;
603
604 ring->head = request->postfix;
605 ring->last_retired_head = -1;
606}
607
e2be4faf 608static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
86d7f238 609{
7e37f889 610 struct intel_ring *ring = req->ring;
c033666a
CW
611 struct i915_workarounds *w = &req->i915->workarounds;
612 int ret, i;
888b5995 613
02235808 614 if (w->count == 0)
7225342a 615 return 0;
888b5995 616
7c9cf4e3 617 ret = req->engine->emit_flush(req, EMIT_BARRIER);
7225342a
MK
618 if (ret)
619 return ret;
888b5995 620
5fb9de1a 621 ret = intel_ring_begin(req, (w->count * 2 + 2));
7225342a
MK
622 if (ret)
623 return ret;
624
b5321f30 625 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
7225342a 626 for (i = 0; i < w->count; i++) {
b5321f30
CW
627 intel_ring_emit_reg(ring, w->reg[i].addr);
628 intel_ring_emit(ring, w->reg[i].value);
7225342a 629 }
b5321f30 630 intel_ring_emit(ring, MI_NOOP);
7225342a 631
b5321f30 632 intel_ring_advance(ring);
7225342a 633
7c9cf4e3 634 ret = req->engine->emit_flush(req, EMIT_BARRIER);
7225342a
MK
635 if (ret)
636 return ret;
888b5995 637
7225342a 638 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
888b5995 639
7225342a 640 return 0;
86d7f238
AS
641}
642
8753181e 643static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
8f0e2b9d
SV
644{
645 int ret;
646
e2be4faf 647 ret = intel_ring_workarounds_emit(req);
8f0e2b9d
SV
648 if (ret != 0)
649 return ret;
650
4e50f082 651 ret = i915_gem_render_state_emit(req);
8f0e2b9d 652 if (ret)
e26e1b97 653 return ret;
8f0e2b9d 654
e26e1b97 655 return 0;
8f0e2b9d
SV
656}
657
7225342a 658static int wa_add(struct drm_i915_private *dev_priv,
f0f59a00
VS
659 i915_reg_t addr,
660 const u32 mask, const u32 val)
7225342a
MK
661{
662 const u32 idx = dev_priv->workarounds.count;
663
664 if (WARN_ON(idx >= I915_MAX_WA_REGS))
665 return -ENOSPC;
666
667 dev_priv->workarounds.reg[idx].addr = addr;
668 dev_priv->workarounds.reg[idx].value = val;
669 dev_priv->workarounds.reg[idx].mask = mask;
670
671 dev_priv->workarounds.count++;
672
673 return 0;
86d7f238
AS
674}
675
ca5a0fbd 676#define WA_REG(addr, mask, val) do { \
cf4b0de6 677 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
7225342a
MK
678 if (r) \
679 return r; \
ca5a0fbd 680 } while (0)
7225342a
MK
681
682#define WA_SET_BIT_MASKED(addr, mask) \
26459343 683 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
7225342a
MK
684
685#define WA_CLR_BIT_MASKED(addr, mask) \
26459343 686 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
7225342a 687
98533251 688#define WA_SET_FIELD_MASKED(addr, mask, value) \
cf4b0de6 689 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
7225342a 690
cf4b0de6
DL
691#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
692#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
7225342a 693
cf4b0de6 694#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
7225342a 695
0bc40be8
TU
696static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
697 i915_reg_t reg)
33136b06 698{
c033666a 699 struct drm_i915_private *dev_priv = engine->i915;
33136b06 700 struct i915_workarounds *wa = &dev_priv->workarounds;
0bc40be8 701 const uint32_t index = wa->hw_whitelist_count[engine->id];
33136b06
AS
702
703 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
704 return -EINVAL;
705
0bc40be8 706 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
33136b06 707 i915_mmio_reg_offset(reg));
0bc40be8 708 wa->hw_whitelist_count[engine->id]++;
33136b06
AS
709
710 return 0;
711}
712
0bc40be8 713static int gen8_init_workarounds(struct intel_engine_cs *engine)
e9a64ada 714{
c033666a 715 struct drm_i915_private *dev_priv = engine->i915;
68c6198b
AS
716
717 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
e9a64ada 718
717d84d6
AS
719 /* WaDisableAsyncFlipPerfMode:bdw,chv */
720 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
721
d0581194
AS
722 /* WaDisablePartialInstShootdown:bdw,chv */
723 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
724 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
725
a340af58
AS
726 /* Use Force Non-Coherent whenever executing a 3D context. This is a
727 * workaround for for a possible hang in the unlikely event a TLB
728 * invalidation occurs during a PSD flush.
729 */
730 /* WaForceEnableNonCoherent:bdw,chv */
120f5d28 731 /* WaHdcDisableFetchWhenMasked:bdw,chv */
a340af58 732 WA_SET_BIT_MASKED(HDC_CHICKEN0,
120f5d28 733 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
a340af58
AS
734 HDC_FORCE_NON_COHERENT);
735
6def8fdd
AS
736 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
737 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
738 * polygons in the same 8x4 pixel/sample area to be processed without
739 * stalling waiting for the earlier ones to write to Hierarchical Z
740 * buffer."
741 *
742 * This optimization is off by default for BDW and CHV; turn it on.
743 */
744 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
745
48404636
AS
746 /* Wa4x4STCOptimizationDisable:bdw,chv */
747 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
748
7eebcde6
AS
749 /*
750 * BSpec recommends 8x4 when MSAA is used,
751 * however in practice 16x4 seems fastest.
752 *
753 * Note that PS/WM thread counts depend on the WIZ hashing
754 * disable bit, which we don't touch here, but it's good
755 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
756 */
757 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
758 GEN6_WIZ_HASHING_MASK,
759 GEN6_WIZ_HASHING_16x4);
760
e9a64ada
AS
761 return 0;
762}
763
0bc40be8 764static int bdw_init_workarounds(struct intel_engine_cs *engine)
86d7f238 765{
c033666a 766 struct drm_i915_private *dev_priv = engine->i915;
e9a64ada 767 int ret;
86d7f238 768
0bc40be8 769 ret = gen8_init_workarounds(engine);
e9a64ada
AS
770 if (ret)
771 return ret;
772
101b376d 773 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
d0581194 774 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
86d7f238 775
101b376d 776 /* WaDisableDopClockGating:bdw */
7225342a
MK
777 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
778 DOP_CLOCK_GATING_DISABLE);
86d7f238 779
7225342a
MK
780 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
781 GEN8_SAMPLER_POWER_BYPASS_DIS);
86d7f238 782
7225342a 783 WA_SET_BIT_MASKED(HDC_CHICKEN0,
35cb6f3b
DL
784 /* WaForceContextSaveRestoreNonCoherent:bdw */
785 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
35cb6f3b 786 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
c033666a 787 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
86d7f238 788
86d7f238
AS
789 return 0;
790}
791
0bc40be8 792static int chv_init_workarounds(struct intel_engine_cs *engine)
00e1e623 793{
c033666a 794 struct drm_i915_private *dev_priv = engine->i915;
e9a64ada 795 int ret;
00e1e623 796
0bc40be8 797 ret = gen8_init_workarounds(engine);
e9a64ada
AS
798 if (ret)
799 return ret;
800
00e1e623 801 /* WaDisableThreadStallDopClockGating:chv */
d0581194 802 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
00e1e623 803
d60de81d
KG
804 /* Improve HiZ throughput on CHV. */
805 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
806
7225342a
MK
807 return 0;
808}
809
0bc40be8 810static int gen9_init_workarounds(struct intel_engine_cs *engine)
3b106531 811{
c033666a 812 struct drm_i915_private *dev_priv = engine->i915;
e0f3fa09 813 int ret;
ab0dfafe 814
a8ab5ed5
TG
815 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
816 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
817
e5f81d65 818 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
9c4cbf82
MK
819 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
820 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
821
e5f81d65 822 /* WaDisableKillLogic:bxt,skl,kbl */
9c4cbf82
MK
823 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
824 ECOCHK_DIS_TLB);
825
e5f81d65
MK
826 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
827 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
ab0dfafe 828 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
950b2aae 829 FLOW_CONTROL_ENABLE |
ab0dfafe
HN
830 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
831
e5f81d65 832 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
8424171e
NH
833 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
834 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
835
a117f378
JN
836 /* WaDisableDgMirrorFixInHalfSliceChicken5:bxt */
837 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
a86eb582
DL
838 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
839 GEN9_DG_MIRROR_FIX_ENABLE);
1de4582f 840
a117f378
JN
841 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:bxt */
842 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
183c6dac
DL
843 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
844 GEN9_RHWO_OPTIMIZATION_DISABLE);
9b01435d
AS
845 /*
846 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
847 * but we do that in per ctx batchbuffer as there is an issue
848 * with this register not getting restored on ctx restore
849 */
183c6dac
DL
850 }
851
e5f81d65 852 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
bfd8ad4e 853 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
bfd8ad4e 854 GEN9_ENABLE_GPGPU_PREEMPTION);
cac23df4 855
e5f81d65
MK
856 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
857 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
60294683
AS
858 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
859 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
9370cd98 860
e5f81d65 861 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
e2db7071
DL
862 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
863 GEN9_CCS_TLB_PREFETCH_ENABLE);
864
0d0b8dcf
JN
865 /* WaDisableMaskBasedCammingInRCC:bxt */
866 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
38a39a7b
BW
867 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
868 PIXEL_MASK_CAMMING_DISABLE);
869
5b0e3659
MK
870 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
871 WA_SET_BIT_MASKED(HDC_CHICKEN0,
872 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
873 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
8ea6f892 874
bbaefe72
MK
875 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
876 * both tied to WaForceContextSaveRestoreNonCoherent
877 * in some hsds for skl. We keep the tie for all gen9. The
878 * documentation is a bit hazy and so we want to get common behaviour,
879 * even though there is no clear evidence we would need both on kbl/bxt.
880 * This area has been source of system hangs so we play it safe
881 * and mimic the skl regardless of what bspec says.
882 *
883 * Use Force Non-Coherent whenever executing a 3D context. This
884 * is a workaround for a possible hang in the unlikely event
885 * a TLB invalidation occurs during a PSD flush.
886 */
887
888 /* WaForceEnableNonCoherent:skl,bxt,kbl */
889 WA_SET_BIT_MASKED(HDC_CHICKEN0,
890 HDC_FORCE_NON_COHERENT);
891
892 /* WaDisableHDCInvalidation:skl,bxt,kbl */
893 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
894 BDW_DISABLE_HDC_INVALIDATION);
895
e5f81d65
MK
896 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
897 if (IS_SKYLAKE(dev_priv) ||
898 IS_KABYLAKE(dev_priv) ||
899 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
8c761609
AS
900 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
901 GEN8_SAMPLER_POWER_BYPASS_DIS);
8c761609 902
e5f81d65 903 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
6b6d5626
RB
904 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
905
e5f81d65 906 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
6ecf56ae
AS
907 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
908 GEN8_LQSC_FLUSH_COHERENT_LINES));
909
6bb62855 910 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
911 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
912 if (ret)
913 return ret;
914
e5f81d65 915 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
0bc40be8 916 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
e0f3fa09
AS
917 if (ret)
918 return ret;
919
e5f81d65 920 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
0bc40be8 921 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
3669ab61
AS
922 if (ret)
923 return ret;
924
3b106531
HN
925 return 0;
926}
927
0bc40be8 928static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
b7668791 929{
c033666a 930 struct drm_i915_private *dev_priv = engine->i915;
b7668791
DL
931 u8 vals[3] = { 0, 0, 0 };
932 unsigned int i;
933
934 for (i = 0; i < 3; i++) {
935 u8 ss;
936
937 /*
938 * Only consider slices where one, and only one, subslice has 7
939 * EUs
940 */
43b67998 941 if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
b7668791
DL
942 continue;
943
944 /*
945 * subslice_7eu[i] != 0 (because of the check above) and
946 * ss_max == 4 (maximum number of subslices possible per slice)
947 *
948 * -> 0 <= ss <= 3;
949 */
43b67998 950 ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
b7668791
DL
951 vals[i] = 3 - ss;
952 }
953
954 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
955 return 0;
956
957 /* Tune IZ hashing. See intel_device_info_runtime_init() */
958 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
959 GEN9_IZ_HASHING_MASK(2) |
960 GEN9_IZ_HASHING_MASK(1) |
961 GEN9_IZ_HASHING_MASK(0),
962 GEN9_IZ_HASHING(2, vals[2]) |
963 GEN9_IZ_HASHING(1, vals[1]) |
964 GEN9_IZ_HASHING(0, vals[0]));
965
966 return 0;
967}
968
0bc40be8 969static int skl_init_workarounds(struct intel_engine_cs *engine)
8d205494 970{
c033666a 971 struct drm_i915_private *dev_priv = engine->i915;
aa0011a8 972 int ret;
d0bbbc4f 973
0bc40be8 974 ret = gen9_init_workarounds(engine);
aa0011a8
AS
975 if (ret)
976 return ret;
8d205494 977
a78536e7
AS
978 /*
979 * Actual WA is to disable percontext preemption granularity control
980 * until D0 which is the default case so this is equivalent to
981 * !WaDisablePerCtxtPreemptionGranularityControl:skl
982 */
9fc736e8
JN
983 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
984 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
a78536e7 985
9c4cbf82 986 /* WaEnableGapsTsvCreditFix:skl */
a117f378
JN
987 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
988 GEN9_GAPS_TSV_CREDIT_DISABLE));
d0bbbc4f 989
eee8efb0
MK
990 /* WaDisableGafsUnitClkGating:skl */
991 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
992
4ba9c1f7
MK
993 /* WaInPlaceDecompressionHang:skl */
994 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
995 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
996 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
997
6107497e 998 /* WaDisableLSQCROPERFforOCL:skl */
0bc40be8 999 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
6107497e
AS
1000 if (ret)
1001 return ret;
1002
0bc40be8 1003 return skl_tune_iz_hashing(engine);
7225342a
MK
1004}
1005
0bc40be8 1006static int bxt_init_workarounds(struct intel_engine_cs *engine)
cae0437f 1007{
c033666a 1008 struct drm_i915_private *dev_priv = engine->i915;
aa0011a8 1009 int ret;
dfb601e6 1010
0bc40be8 1011 ret = gen9_init_workarounds(engine);
aa0011a8
AS
1012 if (ret)
1013 return ret;
cae0437f 1014
9c4cbf82
MK
1015 /* WaStoreMultiplePTEenable:bxt */
1016 /* This is a requirement according to Hardware specification */
c033666a 1017 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
9c4cbf82
MK
1018 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1019
1020 /* WaSetClckGatingDisableMedia:bxt */
c033666a 1021 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
9c4cbf82
MK
1022 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1023 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1024 }
1025
dfb601e6
NH
1026 /* WaDisableThreadStallDopClockGating:bxt */
1027 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1028 STALL_DOP_GATING_DISABLE);
1029
780f0aeb 1030 /* WaDisablePooledEuLoadBalancingFix:bxt */
1031 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1032 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1033 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1034 }
1035
983b4b9d 1036 /* WaDisableSbeCacheDispatchPortSharing:bxt */
c033666a 1037 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
983b4b9d
NH
1038 WA_SET_BIT_MASKED(
1039 GEN7_HALF_SLICE_CHICKEN1,
1040 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1041 }
1042
2c8580e4
AS
1043 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1044 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1045 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
a786d53a 1046 /* WaDisableLSQCROPERFforOCL:bxt */
c033666a 1047 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
0bc40be8 1048 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
2c8580e4
AS
1049 if (ret)
1050 return ret;
a786d53a 1051
0bc40be8 1052 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
a786d53a
AS
1053 if (ret)
1054 return ret;
2c8580e4
AS
1055 }
1056
050fc465 1057 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
c033666a 1058 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
36579cb6
ID
1059 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1060 L3_HIGH_PRIO_CREDITS(2));
050fc465 1061
575e3ccb
MA
1062 /* WaToEnableHwFixForPushConstHWBug:bxt */
1063 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
ad2bdb44
MK
1064 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1065 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1066
4ba9c1f7
MK
1067 /* WaInPlaceDecompressionHang:bxt */
1068 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
1069 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1070 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1071
cae0437f
NH
1072 return 0;
1073}
1074
e5f81d65
MK
1075static int kbl_init_workarounds(struct intel_engine_cs *engine)
1076{
e587f6cb 1077 struct drm_i915_private *dev_priv = engine->i915;
e5f81d65
MK
1078 int ret;
1079
1080 ret = gen9_init_workarounds(engine);
1081 if (ret)
1082 return ret;
1083
e587f6cb
MK
1084 /* WaEnableGapsTsvCreditFix:kbl */
1085 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1086 GEN9_GAPS_TSV_CREDIT_DISABLE));
1087
c0b730d5
MK
1088 /* WaDisableDynamicCreditSharing:kbl */
1089 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1090 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1091 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1092
8401d42f
MK
1093 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1094 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1095 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1096 HDC_FENCE_DEST_SLM_DISABLE);
1097
fe905819
MK
1098 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1099 * involving this register should also be added to WA batch as required.
1100 */
1101 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1102 /* WaDisableLSQCROPERFforOCL:kbl */
1103 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1104 GEN8_LQSC_RO_PERF_DIS);
1105
575e3ccb
MA
1106 /* WaToEnableHwFixForPushConstHWBug:kbl */
1107 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
ad2bdb44
MK
1108 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1109 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1110
4de5d7cc
MK
1111 /* WaDisableGafsUnitClkGating:kbl */
1112 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1113
954337aa
MK
1114 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1115 WA_SET_BIT_MASKED(
1116 GEN7_HALF_SLICE_CHICKEN1,
1117 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1118
4ba9c1f7
MK
1119 /* WaInPlaceDecompressionHang:kbl */
1120 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1121 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1122
fe905819
MK
1123 /* WaDisableLSQCROPERFforOCL:kbl */
1124 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1125 if (ret)
1126 return ret;
1127
e5f81d65
MK
1128 return 0;
1129}
1130
0bc40be8 1131int init_workarounds_ring(struct intel_engine_cs *engine)
7225342a 1132{
c033666a 1133 struct drm_i915_private *dev_priv = engine->i915;
7225342a 1134
0bc40be8 1135 WARN_ON(engine->id != RCS);
7225342a
MK
1136
1137 dev_priv->workarounds.count = 0;
33136b06 1138 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
7225342a 1139
c033666a 1140 if (IS_BROADWELL(dev_priv))
0bc40be8 1141 return bdw_init_workarounds(engine);
7225342a 1142
c033666a 1143 if (IS_CHERRYVIEW(dev_priv))
0bc40be8 1144 return chv_init_workarounds(engine);
00e1e623 1145
c033666a 1146 if (IS_SKYLAKE(dev_priv))
0bc40be8 1147 return skl_init_workarounds(engine);
cae0437f 1148
c033666a 1149 if (IS_BROXTON(dev_priv))
0bc40be8 1150 return bxt_init_workarounds(engine);
3b106531 1151
e5f81d65
MK
1152 if (IS_KABYLAKE(dev_priv))
1153 return kbl_init_workarounds(engine);
1154
00e1e623
VS
1155 return 0;
1156}
1157
0bc40be8 1158static int init_render_ring(struct intel_engine_cs *engine)
8187a2b7 1159{
c033666a 1160 struct drm_i915_private *dev_priv = engine->i915;
0bc40be8 1161 int ret = init_ring_common(engine);
9c33baa6
KZ
1162 if (ret)
1163 return ret;
a69ffdbf 1164
61a563a2 1165 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
ac657f64 1166 if (IS_GEN(dev_priv, 4, 6))
6b26c86d 1167 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1c8c38c5
CW
1168
1169 /* We need to disable the AsyncFlip performance optimisations in order
1170 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1171 * programmed to '1' on all products.
8693a824 1172 *
2441f877 1173 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1c8c38c5 1174 */
ac657f64 1175 if (IS_GEN(dev_priv, 6, 7))
1c8c38c5
CW
1176 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1177
f05bb0c7 1178 /* Required for the hardware to program scanline values for waiting */
01fa0302 1179 /* WaEnableFlushTlbInvalidationMode:snb */
c033666a 1180 if (IS_GEN6(dev_priv))
f05bb0c7 1181 I915_WRITE(GFX_MODE,
aa83e30d 1182 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
f05bb0c7 1183
01fa0302 1184 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
c033666a 1185 if (IS_GEN7(dev_priv))
1c8c38c5 1186 I915_WRITE(GFX_MODE_GEN7,
01fa0302 1187 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1c8c38c5 1188 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
78501eac 1189
c033666a 1190 if (IS_GEN6(dev_priv)) {
3a69ddd6
KG
1191 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1192 * "If this bit is set, STCunit will have LRA as replacement
1193 * policy. [...] This bit must be reset. LRA replacement
1194 * policy is not supported."
1195 */
1196 I915_WRITE(CACHE_MODE_0,
5e13a0c5 1197 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
84f9f938
BW
1198 }
1199
ac657f64 1200 if (IS_GEN(dev_priv, 6, 7))
6b26c86d 1201 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
84f9f938 1202
035ea405
VS
1203 if (INTEL_INFO(dev_priv)->gen >= 6)
1204 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
15b9f80e 1205
0bc40be8 1206 return init_workarounds_ring(engine);
8187a2b7
ZN
1207}
1208
0bc40be8 1209static void render_ring_cleanup(struct intel_engine_cs *engine)
c6df541c 1210{
c033666a 1211 struct drm_i915_private *dev_priv = engine->i915;
3e78998a 1212
19880c4a 1213 i915_vma_unpin_and_release(&dev_priv->semaphore);
c6df541c
CW
1214}
1215
caddfe71 1216static u32 *gen8_rcs_signal(struct drm_i915_gem_request *req, u32 *out)
3e78998a 1217{
ad7bdb2b 1218 struct drm_i915_private *dev_priv = req->i915;
3e78998a 1219 struct intel_engine_cs *waiter;
c3232b18 1220 enum intel_engine_id id;
3e78998a 1221
3b3f1650 1222 for_each_engine(waiter, dev_priv, id) {
ad7bdb2b 1223 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
3e78998a
BW
1224 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1225 continue;
1226
caddfe71
CW
1227 *out++ = GFX_OP_PIPE_CONTROL(6);
1228 *out++ = (PIPE_CONTROL_GLOBAL_GTT_IVB |
1229 PIPE_CONTROL_QW_WRITE |
1230 PIPE_CONTROL_CS_STALL);
1231 *out++ = lower_32_bits(gtt_offset);
1232 *out++ = upper_32_bits(gtt_offset);
1233 *out++ = req->global_seqno;
1234 *out++ = 0;
1235 *out++ = (MI_SEMAPHORE_SIGNAL |
1236 MI_SEMAPHORE_TARGET(waiter->hw_id));
1237 *out++ = 0;
3e78998a
BW
1238 }
1239
caddfe71 1240 return out;
3e78998a
BW
1241}
1242
caddfe71 1243static u32 *gen8_xcs_signal(struct drm_i915_gem_request *req, u32 *out)
3e78998a 1244{
ad7bdb2b 1245 struct drm_i915_private *dev_priv = req->i915;
3e78998a 1246 struct intel_engine_cs *waiter;
c3232b18 1247 enum intel_engine_id id;
3e78998a 1248
3b3f1650 1249 for_each_engine(waiter, dev_priv, id) {
ad7bdb2b 1250 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
3e78998a
BW
1251 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1252 continue;
1253
caddfe71
CW
1254 *out++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1255 *out++ = lower_32_bits(gtt_offset) | MI_FLUSH_DW_USE_GTT;
1256 *out++ = upper_32_bits(gtt_offset);
1257 *out++ = req->global_seqno;
1258 *out++ = (MI_SEMAPHORE_SIGNAL |
1259 MI_SEMAPHORE_TARGET(waiter->hw_id));
1260 *out++ = 0;
3e78998a
BW
1261 }
1262
caddfe71 1263 return out;
3e78998a
BW
1264}
1265
caddfe71 1266static u32 *gen6_signal(struct drm_i915_gem_request *req, u32 *out)
1ec14ad3 1267{
ad7bdb2b 1268 struct drm_i915_private *dev_priv = req->i915;
318f89ca 1269 struct intel_engine_cs *engine;
3b3f1650 1270 enum intel_engine_id id;
caddfe71 1271 int num_rings = 0;
024a43e1 1272
3b3f1650 1273 for_each_engine(engine, dev_priv, id) {
318f89ca
TU
1274 i915_reg_t mbox_reg;
1275
1276 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
1277 continue;
f0f59a00 1278
318f89ca 1279 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
f0f59a00 1280 if (i915_mmio_reg_valid(mbox_reg)) {
caddfe71
CW
1281 *out++ = MI_LOAD_REGISTER_IMM(1);
1282 *out++ = i915_mmio_reg_offset(mbox_reg);
1283 *out++ = req->global_seqno;
1284 num_rings++;
78325f2d
BW
1285 }
1286 }
caddfe71
CW
1287 if (num_rings & 1)
1288 *out++ = MI_NOOP;
024a43e1 1289
caddfe71 1290 return out;
1ec14ad3
CW
1291}
1292
b0411e7d
CW
1293static void i9xx_submit_request(struct drm_i915_gem_request *request)
1294{
1295 struct drm_i915_private *dev_priv = request->i915;
1296
caddfe71 1297 I915_WRITE_TAIL(request->engine, request->tail);
b0411e7d
CW
1298}
1299
caddfe71
CW
1300static void i9xx_emit_breadcrumb(struct drm_i915_gem_request *req,
1301 u32 *out)
1ec14ad3 1302{
caddfe71
CW
1303 *out++ = MI_STORE_DWORD_INDEX;
1304 *out++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
1305 *out++ = req->global_seqno;
1306 *out++ = MI_USER_INTERRUPT;
1ec14ad3 1307
caddfe71 1308 req->tail = intel_ring_offset(req->ring, out);
1ec14ad3
CW
1309}
1310
98f29e8d
CW
1311static const int i9xx_emit_breadcrumb_sz = 4;
1312
b0411e7d 1313/**
9b81d556 1314 * gen6_sema_emit_breadcrumb - Update the semaphore mailbox registers
b0411e7d
CW
1315 *
1316 * @request - request to write to the ring
1317 *
1318 * Update the mailbox registers in the *other* rings with the current seqno.
1319 * This acts like a signal in the canonical semaphore.
1320 */
caddfe71
CW
1321static void gen6_sema_emit_breadcrumb(struct drm_i915_gem_request *req,
1322 u32 *out)
b0411e7d 1323{
caddfe71
CW
1324 return i9xx_emit_breadcrumb(req,
1325 req->engine->semaphore.signal(req, out));
b0411e7d
CW
1326}
1327
caddfe71
CW
1328static void gen8_render_emit_breadcrumb(struct drm_i915_gem_request *req,
1329 u32 *out)
a58c01aa
CW
1330{
1331 struct intel_engine_cs *engine = req->engine;
9242f974 1332
caddfe71
CW
1333 if (engine->semaphore.signal)
1334 out = engine->semaphore.signal(req, out);
a58c01aa 1335
caddfe71
CW
1336 *out++ = GFX_OP_PIPE_CONTROL(6);
1337 *out++ = (PIPE_CONTROL_GLOBAL_GTT_IVB |
b5321f30 1338 PIPE_CONTROL_CS_STALL |
caddfe71
CW
1339 PIPE_CONTROL_QW_WRITE);
1340 *out++ = intel_hws_seqno_address(engine);
1341 *out++ = 0;
1342 *out++ = req->global_seqno;
a58c01aa 1343 /* We're thrashing one dword of HWS. */
caddfe71
CW
1344 *out++ = 0;
1345 *out++ = MI_USER_INTERRUPT;
1346 *out++ = MI_NOOP;
a58c01aa 1347
caddfe71 1348 req->tail = intel_ring_offset(req->ring, out);
a58c01aa
CW
1349}
1350
98f29e8d
CW
1351static const int gen8_render_emit_breadcrumb_sz = 8;
1352
c8c99b0f
BW
1353/**
1354 * intel_ring_sync - sync the waiter to the signaller on seqno
1355 *
1356 * @waiter - ring that is waiting
1357 * @signaller - ring which has, or will signal
1358 * @seqno - seqno which the waiter will block on
1359 */
5ee426ca
BW
1360
1361static int
ad7bdb2b
CW
1362gen8_ring_sync_to(struct drm_i915_gem_request *req,
1363 struct drm_i915_gem_request *signal)
5ee426ca 1364{
ad7bdb2b
CW
1365 struct intel_ring *ring = req->ring;
1366 struct drm_i915_private *dev_priv = req->i915;
1367 u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
6ef48d7f 1368 struct i915_hw_ppgtt *ppgtt;
5ee426ca
BW
1369 int ret;
1370
ad7bdb2b 1371 ret = intel_ring_begin(req, 4);
5ee426ca
BW
1372 if (ret)
1373 return ret;
1374
ad7bdb2b
CW
1375 intel_ring_emit(ring,
1376 MI_SEMAPHORE_WAIT |
1377 MI_SEMAPHORE_GLOBAL_GTT |
1378 MI_SEMAPHORE_SAD_GTE_SDD);
65e4760e 1379 intel_ring_emit(ring, signal->global_seqno);
ad7bdb2b
CW
1380 intel_ring_emit(ring, lower_32_bits(offset));
1381 intel_ring_emit(ring, upper_32_bits(offset));
1382 intel_ring_advance(ring);
6ef48d7f
CW
1383
1384 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1385 * pagetables and we must reload them before executing the batch.
1386 * We do this on the i915_switch_context() following the wait and
1387 * before the dispatch.
1388 */
ad7bdb2b
CW
1389 ppgtt = req->ctx->ppgtt;
1390 if (ppgtt && req->engine->id != RCS)
1391 ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
5ee426ca
BW
1392 return 0;
1393}
1394
c8c99b0f 1395static int
ad7bdb2b
CW
1396gen6_ring_sync_to(struct drm_i915_gem_request *req,
1397 struct drm_i915_gem_request *signal)
1ec14ad3 1398{
ad7bdb2b 1399 struct intel_ring *ring = req->ring;
c8c99b0f
BW
1400 u32 dw1 = MI_SEMAPHORE_MBOX |
1401 MI_SEMAPHORE_COMPARE |
1402 MI_SEMAPHORE_REGISTER;
318f89ca 1403 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
ebc348b2 1404 int ret;
1ec14ad3 1405
ebc348b2 1406 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
686cb5f9 1407
ad7bdb2b 1408 ret = intel_ring_begin(req, 4);
1ec14ad3
CW
1409 if (ret)
1410 return ret;
1411
ad7bdb2b 1412 intel_ring_emit(ring, dw1 | wait_mbox);
ddf07be7
CW
1413 /* Throughout all of the GEM code, seqno passed implies our current
1414 * seqno is >= the last seqno executed. However for hardware the
1415 * comparison is strictly greater than.
1416 */
65e4760e 1417 intel_ring_emit(ring, signal->global_seqno - 1);
ad7bdb2b
CW
1418 intel_ring_emit(ring, 0);
1419 intel_ring_emit(ring, MI_NOOP);
1420 intel_ring_advance(ring);
1ec14ad3
CW
1421
1422 return 0;
1423}
1424
f8973c21 1425static void
38a0f2db 1426gen5_seqno_barrier(struct intel_engine_cs *engine)
c6df541c 1427{
f8973c21
CW
1428 /* MI_STORE are internally buffered by the GPU and not flushed
1429 * either by MI_FLUSH or SyncFlush or any other combination of
1430 * MI commands.
c6df541c 1431 *
f8973c21
CW
1432 * "Only the submission of the store operation is guaranteed.
1433 * The write result will be complete (coherent) some time later
1434 * (this is practically a finite period but there is no guaranteed
1435 * latency)."
1436 *
1437 * Empirically, we observe that we need a delay of at least 75us to
1438 * be sure that the seqno write is visible by the CPU.
c6df541c 1439 */
f8973c21 1440 usleep_range(125, 250);
c6df541c
CW
1441}
1442
c04e0f3b
CW
1443static void
1444gen6_seqno_barrier(struct intel_engine_cs *engine)
4cd53c0c 1445{
c033666a 1446 struct drm_i915_private *dev_priv = engine->i915;
bcbdb6d0 1447
4cd53c0c
SV
1448 /* Workaround to force correct ordering between irq and seqno writes on
1449 * ivb (and maybe also on snb) by reading from a CS register (like
9b9ed309
CW
1450 * ACTHD) before reading the status page.
1451 *
1452 * Note that this effectively stalls the read by the time it takes to
1453 * do a memory transaction, which more or less ensures that the write
1454 * from the GPU has sufficient time to invalidate the CPU cacheline.
1455 * Alternatively we could delay the interrupt from the CS ring to give
1456 * the write time to land, but that would incur a delay after every
1457 * batch i.e. much more frequent than a delay when waiting for the
1458 * interrupt (with the same net latency).
bcbdb6d0
CW
1459 *
1460 * Also note that to prevent whole machine hangs on gen7, we have to
1461 * take the spinlock to guard against concurrent cacheline access.
9b9ed309 1462 */
bcbdb6d0 1463 spin_lock_irq(&dev_priv->uncore.lock);
c04e0f3b 1464 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
bcbdb6d0 1465 spin_unlock_irq(&dev_priv->uncore.lock);
4cd53c0c
SV
1466}
1467
31bb59cc
CW
1468static void
1469gen5_irq_enable(struct intel_engine_cs *engine)
e48d8634 1470{
31bb59cc 1471 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
SV
1472}
1473
1474static void
31bb59cc 1475gen5_irq_disable(struct intel_engine_cs *engine)
e48d8634 1476{
31bb59cc 1477 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
SV
1478}
1479
31bb59cc
CW
1480static void
1481i9xx_irq_enable(struct intel_engine_cs *engine)
62fdfeaf 1482{
c033666a 1483 struct drm_i915_private *dev_priv = engine->i915;
b13c2b96 1484
31bb59cc
CW
1485 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1486 I915_WRITE(IMR, dev_priv->irq_mask);
1487 POSTING_READ_FW(RING_IMR(engine->mmio_base));
62fdfeaf
EA
1488}
1489
8187a2b7 1490static void
31bb59cc 1491i9xx_irq_disable(struct intel_engine_cs *engine)
62fdfeaf 1492{
c033666a 1493 struct drm_i915_private *dev_priv = engine->i915;
62fdfeaf 1494
31bb59cc
CW
1495 dev_priv->irq_mask |= engine->irq_enable_mask;
1496 I915_WRITE(IMR, dev_priv->irq_mask);
62fdfeaf
EA
1497}
1498
31bb59cc
CW
1499static void
1500i8xx_irq_enable(struct intel_engine_cs *engine)
c2798b19 1501{
c033666a 1502 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 1503
31bb59cc
CW
1504 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1505 I915_WRITE16(IMR, dev_priv->irq_mask);
1506 POSTING_READ16(RING_IMR(engine->mmio_base));
c2798b19
CW
1507}
1508
1509static void
31bb59cc 1510i8xx_irq_disable(struct intel_engine_cs *engine)
c2798b19 1511{
c033666a 1512 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 1513
31bb59cc
CW
1514 dev_priv->irq_mask |= engine->irq_enable_mask;
1515 I915_WRITE16(IMR, dev_priv->irq_mask);
c2798b19
CW
1516}
1517
b72f3acb 1518static int
7c9cf4e3 1519bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
d1b851fc 1520{
7e37f889 1521 struct intel_ring *ring = req->ring;
b72f3acb
CW
1522 int ret;
1523
5fb9de1a 1524 ret = intel_ring_begin(req, 2);
b72f3acb
CW
1525 if (ret)
1526 return ret;
1527
b5321f30
CW
1528 intel_ring_emit(ring, MI_FLUSH);
1529 intel_ring_emit(ring, MI_NOOP);
1530 intel_ring_advance(ring);
b72f3acb 1531 return 0;
d1b851fc
ZN
1532}
1533
31bb59cc
CW
1534static void
1535gen6_irq_enable(struct intel_engine_cs *engine)
0f46832f 1536{
c033666a 1537 struct drm_i915_private *dev_priv = engine->i915;
0f46832f 1538
61ff75ac
CW
1539 I915_WRITE_IMR(engine,
1540 ~(engine->irq_enable_mask |
1541 engine->irq_keep_mask));
31bb59cc 1542 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
0f46832f
CW
1543}
1544
1545static void
31bb59cc 1546gen6_irq_disable(struct intel_engine_cs *engine)
0f46832f 1547{
c033666a 1548 struct drm_i915_private *dev_priv = engine->i915;
0f46832f 1549
61ff75ac 1550 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
31bb59cc 1551 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
d1b851fc
ZN
1552}
1553
31bb59cc
CW
1554static void
1555hsw_vebox_irq_enable(struct intel_engine_cs *engine)
a19d2933 1556{
c033666a 1557 struct drm_i915_private *dev_priv = engine->i915;
a19d2933 1558
31bb59cc 1559 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
f4e9af4f 1560 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
a19d2933
BW
1561}
1562
1563static void
31bb59cc 1564hsw_vebox_irq_disable(struct intel_engine_cs *engine)
a19d2933 1565{
c033666a 1566 struct drm_i915_private *dev_priv = engine->i915;
a19d2933 1567
31bb59cc 1568 I915_WRITE_IMR(engine, ~0);
f4e9af4f 1569 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
a19d2933
BW
1570}
1571
31bb59cc
CW
1572static void
1573gen8_irq_enable(struct intel_engine_cs *engine)
abd58f01 1574{
c033666a 1575 struct drm_i915_private *dev_priv = engine->i915;
abd58f01 1576
61ff75ac
CW
1577 I915_WRITE_IMR(engine,
1578 ~(engine->irq_enable_mask |
1579 engine->irq_keep_mask));
31bb59cc 1580 POSTING_READ_FW(RING_IMR(engine->mmio_base));
abd58f01
BW
1581}
1582
1583static void
31bb59cc 1584gen8_irq_disable(struct intel_engine_cs *engine)
abd58f01 1585{
c033666a 1586 struct drm_i915_private *dev_priv = engine->i915;
abd58f01 1587
61ff75ac 1588 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
abd58f01
BW
1589}
1590
d1b851fc 1591static int
803688ba
CW
1592i965_emit_bb_start(struct drm_i915_gem_request *req,
1593 u64 offset, u32 length,
1594 unsigned int dispatch_flags)
d1b851fc 1595{
7e37f889 1596 struct intel_ring *ring = req->ring;
e1f99ce6 1597 int ret;
78501eac 1598
5fb9de1a 1599 ret = intel_ring_begin(req, 2);
e1f99ce6
CW
1600 if (ret)
1601 return ret;
1602
b5321f30 1603 intel_ring_emit(ring,
65f56876
CW
1604 MI_BATCH_BUFFER_START |
1605 MI_BATCH_GTT |
8e004efc
JH
1606 (dispatch_flags & I915_DISPATCH_SECURE ?
1607 0 : MI_BATCH_NON_SECURE_I965));
b5321f30
CW
1608 intel_ring_emit(ring, offset);
1609 intel_ring_advance(ring);
78501eac 1610
d1b851fc
ZN
1611 return 0;
1612}
1613
b45305fc
SV
1614/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1615#define I830_BATCH_LIMIT (256*1024)
c4d69da1
CW
1616#define I830_TLB_ENTRIES (2)
1617#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
8187a2b7 1618static int
803688ba
CW
1619i830_emit_bb_start(struct drm_i915_gem_request *req,
1620 u64 offset, u32 len,
1621 unsigned int dispatch_flags)
62fdfeaf 1622{
7e37f889 1623 struct intel_ring *ring = req->ring;
bde13ebd 1624 u32 cs_offset = i915_ggtt_offset(req->engine->scratch);
c4e7a414 1625 int ret;
62fdfeaf 1626
5fb9de1a 1627 ret = intel_ring_begin(req, 6);
c4d69da1
CW
1628 if (ret)
1629 return ret;
62fdfeaf 1630
c4d69da1 1631 /* Evict the invalid PTE TLBs */
b5321f30
CW
1632 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1633 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1634 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1635 intel_ring_emit(ring, cs_offset);
1636 intel_ring_emit(ring, 0xdeadbeef);
1637 intel_ring_emit(ring, MI_NOOP);
1638 intel_ring_advance(ring);
b45305fc 1639
8e004efc 1640 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
b45305fc
SV
1641 if (len > I830_BATCH_LIMIT)
1642 return -ENOSPC;
1643
5fb9de1a 1644 ret = intel_ring_begin(req, 6 + 2);
b45305fc
SV
1645 if (ret)
1646 return ret;
c4d69da1
CW
1647
1648 /* Blit the batch (which has now all relocs applied) to the
1649 * stable batch scratch bo area (so that the CS never
1650 * stumbles over its tlb invalidation bug) ...
1651 */
b5321f30
CW
1652 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1653 intel_ring_emit(ring,
e2f80391 1654 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
b5321f30
CW
1655 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1656 intel_ring_emit(ring, cs_offset);
1657 intel_ring_emit(ring, 4096);
1658 intel_ring_emit(ring, offset);
e2f80391 1659
b5321f30
CW
1660 intel_ring_emit(ring, MI_FLUSH);
1661 intel_ring_emit(ring, MI_NOOP);
1662 intel_ring_advance(ring);
b45305fc
SV
1663
1664 /* ... and execute it. */
c4d69da1 1665 offset = cs_offset;
b45305fc 1666 }
e1f99ce6 1667
9d611c03 1668 ret = intel_ring_begin(req, 2);
c4d69da1
CW
1669 if (ret)
1670 return ret;
1671
b5321f30
CW
1672 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1673 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1674 0 : MI_BATCH_NON_SECURE));
1675 intel_ring_advance(ring);
c4d69da1 1676
fb3256da
SV
1677 return 0;
1678}
1679
1680static int
803688ba
CW
1681i915_emit_bb_start(struct drm_i915_gem_request *req,
1682 u64 offset, u32 len,
1683 unsigned int dispatch_flags)
fb3256da 1684{
7e37f889 1685 struct intel_ring *ring = req->ring;
fb3256da
SV
1686 int ret;
1687
5fb9de1a 1688 ret = intel_ring_begin(req, 2);
fb3256da
SV
1689 if (ret)
1690 return ret;
1691
b5321f30
CW
1692 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1693 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1694 0 : MI_BATCH_NON_SECURE));
1695 intel_ring_advance(ring);
62fdfeaf 1696
62fdfeaf
EA
1697 return 0;
1698}
1699
0bc40be8 1700static void cleanup_phys_status_page(struct intel_engine_cs *engine)
7d3fdfff 1701{
c033666a 1702 struct drm_i915_private *dev_priv = engine->i915;
7d3fdfff
VS
1703
1704 if (!dev_priv->status_page_dmah)
1705 return;
1706
91c8a326 1707 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
0bc40be8 1708 engine->status_page.page_addr = NULL;
7d3fdfff
VS
1709}
1710
0bc40be8 1711static void cleanup_status_page(struct intel_engine_cs *engine)
62fdfeaf 1712{
57e88531 1713 struct i915_vma *vma;
f8a7fde4 1714 struct drm_i915_gem_object *obj;
62fdfeaf 1715
57e88531
CW
1716 vma = fetch_and_zero(&engine->status_page.vma);
1717 if (!vma)
62fdfeaf 1718 return;
62fdfeaf 1719
f8a7fde4
CW
1720 obj = vma->obj;
1721
57e88531 1722 i915_vma_unpin(vma);
f8a7fde4
CW
1723 i915_vma_close(vma);
1724
1725 i915_gem_object_unpin_map(obj);
1726 __i915_gem_object_release_unless_active(obj);
62fdfeaf
EA
1727}
1728
0bc40be8 1729static int init_status_page(struct intel_engine_cs *engine)
62fdfeaf 1730{
57e88531
CW
1731 struct drm_i915_gem_object *obj;
1732 struct i915_vma *vma;
1733 unsigned int flags;
920cf419 1734 void *vaddr;
57e88531 1735 int ret;
e4ffd173 1736
920cf419 1737 obj = i915_gem_object_create_internal(engine->i915, 4096);
57e88531
CW
1738 if (IS_ERR(obj)) {
1739 DRM_ERROR("Failed to allocate status page\n");
1740 return PTR_ERR(obj);
1741 }
62fdfeaf 1742
57e88531
CW
1743 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1744 if (ret)
1745 goto err;
e3efda49 1746
57e88531
CW
1747 vma = i915_vma_create(obj, &engine->i915->ggtt.base, NULL);
1748 if (IS_ERR(vma)) {
1749 ret = PTR_ERR(vma);
1750 goto err;
e3efda49 1751 }
62fdfeaf 1752
57e88531
CW
1753 flags = PIN_GLOBAL;
1754 if (!HAS_LLC(engine->i915))
1755 /* On g33, we cannot place HWS above 256MiB, so
1756 * restrict its pinning to the low mappable arena.
1757 * Though this restriction is not documented for
1758 * gen4, gen5, or byt, they also behave similarly
1759 * and hang if the HWS is placed at the top of the
1760 * GTT. To generalise, it appears that all !llc
1761 * platforms have issues with us placing the HWS
1762 * above the mappable region (even though we never
1763 * actualy map it).
1764 */
1765 flags |= PIN_MAPPABLE;
1766 ret = i915_vma_pin(vma, 0, 4096, flags);
1767 if (ret)
1768 goto err;
62fdfeaf 1769
920cf419
CW
1770 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1771 if (IS_ERR(vaddr)) {
1772 ret = PTR_ERR(vaddr);
1773 goto err_unpin;
1774 }
1775
57e88531 1776 engine->status_page.vma = vma;
bde13ebd 1777 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
920cf419 1778 engine->status_page.page_addr = memset(vaddr, 0, 4096);
62fdfeaf 1779
bde13ebd
CW
1780 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1781 engine->name, i915_ggtt_offset(vma));
62fdfeaf 1782 return 0;
57e88531 1783
920cf419
CW
1784err_unpin:
1785 i915_vma_unpin(vma);
57e88531
CW
1786err:
1787 i915_gem_object_put(obj);
1788 return ret;
62fdfeaf
EA
1789}
1790
0bc40be8 1791static int init_phys_status_page(struct intel_engine_cs *engine)
6b8294a4 1792{
c033666a 1793 struct drm_i915_private *dev_priv = engine->i915;
6b8294a4 1794
57e88531
CW
1795 dev_priv->status_page_dmah =
1796 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
1797 if (!dev_priv->status_page_dmah)
1798 return -ENOMEM;
6b8294a4 1799
0bc40be8
TU
1800 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1801 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
6b8294a4
CW
1802
1803 return 0;
1804}
1805
aad29fbb 1806int intel_ring_pin(struct intel_ring *ring)
7ba717cf 1807{
a687a43a 1808 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
57e88531 1809 unsigned int flags = PIN_GLOBAL | PIN_OFFSET_BIAS | 4096;
9d80841e 1810 enum i915_map_type map;
57e88531 1811 struct i915_vma *vma = ring->vma;
8305216f 1812 void *addr;
7ba717cf
TD
1813 int ret;
1814
57e88531 1815 GEM_BUG_ON(ring->vaddr);
7ba717cf 1816
9d80841e
CW
1817 map = HAS_LLC(ring->engine->i915) ? I915_MAP_WB : I915_MAP_WC;
1818
1819 if (vma->obj->stolen)
57e88531 1820 flags |= PIN_MAPPABLE;
def0c5f6 1821
57e88531 1822 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
9d80841e 1823 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
57e88531
CW
1824 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1825 else
1826 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1827 if (unlikely(ret))
def0c5f6 1828 return ret;
57e88531 1829 }
7ba717cf 1830
57e88531
CW
1831 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1832 if (unlikely(ret))
1833 return ret;
def0c5f6 1834
9d80841e 1835 if (i915_vma_is_map_and_fenceable(vma))
57e88531
CW
1836 addr = (void __force *)i915_vma_pin_iomap(vma);
1837 else
9d80841e 1838 addr = i915_gem_object_pin_map(vma->obj, map);
57e88531
CW
1839 if (IS_ERR(addr))
1840 goto err;
7ba717cf 1841
32c04f16 1842 ring->vaddr = addr;
7ba717cf 1843 return 0;
d2cad535 1844
57e88531
CW
1845err:
1846 i915_vma_unpin(vma);
1847 return PTR_ERR(addr);
7ba717cf
TD
1848}
1849
aad29fbb
CW
1850void intel_ring_unpin(struct intel_ring *ring)
1851{
1852 GEM_BUG_ON(!ring->vma);
1853 GEM_BUG_ON(!ring->vaddr);
1854
9d80841e 1855 if (i915_vma_is_map_and_fenceable(ring->vma))
aad29fbb 1856 i915_vma_unpin_iomap(ring->vma);
57e88531
CW
1857 else
1858 i915_gem_object_unpin_map(ring->vma->obj);
aad29fbb
CW
1859 ring->vaddr = NULL;
1860
57e88531 1861 i915_vma_unpin(ring->vma);
2919d291
OM
1862}
1863
57e88531
CW
1864static struct i915_vma *
1865intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
62fdfeaf 1866{
05394f39 1867 struct drm_i915_gem_object *obj;
57e88531 1868 struct i915_vma *vma;
62fdfeaf 1869
c58b735f
CW
1870 obj = i915_gem_object_create_stolen(&dev_priv->drm, size);
1871 if (!obj)
57e88531
CW
1872 obj = i915_gem_object_create(&dev_priv->drm, size);
1873 if (IS_ERR(obj))
1874 return ERR_CAST(obj);
8187a2b7 1875
24f3a8cf
AG
1876 /* mark ring buffers as read-only from GPU side by default */
1877 obj->gt_ro = 1;
1878
57e88531
CW
1879 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
1880 if (IS_ERR(vma))
1881 goto err;
1882
1883 return vma;
e3efda49 1884
57e88531
CW
1885err:
1886 i915_gem_object_put(obj);
1887 return vma;
e3efda49
CW
1888}
1889
7e37f889
CW
1890struct intel_ring *
1891intel_engine_create_ring(struct intel_engine_cs *engine, int size)
01101fa7 1892{
7e37f889 1893 struct intel_ring *ring;
57e88531 1894 struct i915_vma *vma;
01101fa7 1895
8f942018 1896 GEM_BUG_ON(!is_power_of_2(size));
62ae14b1 1897 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
8f942018 1898
01101fa7 1899 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
57e88531 1900 if (!ring)
01101fa7
CW
1901 return ERR_PTR(-ENOMEM);
1902
4a570db5 1903 ring->engine = engine;
01101fa7 1904
675d9ad7
CW
1905 INIT_LIST_HEAD(&ring->request_list);
1906
01101fa7
CW
1907 ring->size = size;
1908 /* Workaround an erratum on the i830 which causes a hang if
1909 * the TAIL pointer points to within the last 2 cachelines
1910 * of the buffer.
1911 */
1912 ring->effective_size = size;
c033666a 1913 if (IS_I830(engine->i915) || IS_845G(engine->i915))
01101fa7
CW
1914 ring->effective_size -= 2 * CACHELINE_BYTES;
1915
1916 ring->last_retired_head = -1;
1917 intel_ring_update_space(ring);
1918
57e88531
CW
1919 vma = intel_ring_create_vma(engine->i915, size);
1920 if (IS_ERR(vma)) {
01101fa7 1921 kfree(ring);
57e88531 1922 return ERR_CAST(vma);
01101fa7 1923 }
57e88531 1924 ring->vma = vma;
01101fa7
CW
1925
1926 return ring;
1927}
1928
1929void
7e37f889 1930intel_ring_free(struct intel_ring *ring)
01101fa7 1931{
f8a7fde4
CW
1932 struct drm_i915_gem_object *obj = ring->vma->obj;
1933
1934 i915_vma_close(ring->vma);
1935 __i915_gem_object_release_unless_active(obj);
1936
01101fa7
CW
1937 kfree(ring);
1938}
1939
0cb26a8e
CW
1940static int intel_ring_context_pin(struct i915_gem_context *ctx,
1941 struct intel_engine_cs *engine)
1942{
1943 struct intel_context *ce = &ctx->engine[engine->id];
1944 int ret;
1945
91c8a326 1946 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
0cb26a8e
CW
1947
1948 if (ce->pin_count++)
1949 return 0;
1950
1951 if (ce->state) {
07c9a21a 1952 struct i915_vma *vma;
7abc98fa 1953
07c9a21a
CW
1954 vma = i915_gem_context_pin_legacy(ctx, PIN_HIGH);
1955 if (IS_ERR(vma)) {
1956 ret = PTR_ERR(vma);
0cb26a8e 1957 goto error;
07c9a21a 1958 }
0cb26a8e
CW
1959 }
1960
c7c3c07d
CW
1961 /* The kernel context is only used as a placeholder for flushing the
1962 * active context. It is never used for submitting user rendering and
1963 * as such never requires the golden render context, and so we can skip
1964 * emitting it when we switch to the kernel context. This is required
1965 * as during eviction we cannot allocate and pin the renderstate in
1966 * order to initialise the context.
1967 */
1968 if (ctx == ctx->i915->kernel_context)
1969 ce->initialised = true;
1970
9a6feaf0 1971 i915_gem_context_get(ctx);
0cb26a8e
CW
1972 return 0;
1973
1974error:
1975 ce->pin_count = 0;
1976 return ret;
1977}
1978
1979static void intel_ring_context_unpin(struct i915_gem_context *ctx,
1980 struct intel_engine_cs *engine)
1981{
1982 struct intel_context *ce = &ctx->engine[engine->id];
1983
91c8a326 1984 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
0cb26a8e
CW
1985
1986 if (--ce->pin_count)
1987 return;
1988
1989 if (ce->state)
bf3783e5 1990 i915_vma_unpin(ce->state);
0cb26a8e 1991
9a6feaf0 1992 i915_gem_context_put(ctx);
0cb26a8e
CW
1993}
1994
acd27845 1995static int intel_init_ring_buffer(struct intel_engine_cs *engine)
e3efda49 1996{
acd27845 1997 struct drm_i915_private *dev_priv = engine->i915;
32c04f16 1998 struct intel_ring *ring;
e3efda49
CW
1999 int ret;
2000
0bc40be8 2001 WARN_ON(engine->buffer);
bfc882b4 2002
019bf277
TU
2003 intel_engine_setup_common(engine);
2004
019bf277 2005 ret = intel_engine_init_common(engine);
688e6c72
CW
2006 if (ret)
2007 goto error;
e3efda49 2008
0cb26a8e
CW
2009 /* We may need to do things with the shrinker which
2010 * require us to immediately switch back to the default
2011 * context. This can cause a problem as pinning the
2012 * default context also requires GTT space which may not
2013 * be available. To avoid this we always pin the default
2014 * context.
2015 */
2016 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2017 if (ret)
2018 goto error;
2019
32c04f16
CW
2020 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
2021 if (IS_ERR(ring)) {
2022 ret = PTR_ERR(ring);
b0366a54
DG
2023 goto error;
2024 }
01101fa7 2025
3177659a
CS
2026 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
2027 WARN_ON(engine->id != RCS);
2028 ret = init_phys_status_page(engine);
e3efda49 2029 if (ret)
8ee14975 2030 goto error;
e3efda49 2031 } else {
3177659a 2032 ret = init_status_page(engine);
e3efda49 2033 if (ret)
8ee14975 2034 goto error;
e3efda49
CW
2035 }
2036
aad29fbb 2037 ret = intel_ring_pin(ring);
bfc882b4 2038 if (ret) {
57e88531 2039 intel_ring_free(ring);
bfc882b4 2040 goto error;
e3efda49 2041 }
57e88531 2042 engine->buffer = ring;
62fdfeaf 2043
8ee14975 2044 return 0;
351e3db2 2045
8ee14975 2046error:
7e37f889 2047 intel_engine_cleanup(engine);
8ee14975 2048 return ret;
62fdfeaf
EA
2049}
2050
7e37f889 2051void intel_engine_cleanup(struct intel_engine_cs *engine)
62fdfeaf 2052{
6402c330 2053 struct drm_i915_private *dev_priv;
33626e6a 2054
c033666a 2055 dev_priv = engine->i915;
6402c330 2056
0bc40be8 2057 if (engine->buffer) {
21a2c58a
CW
2058 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
2059 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
33626e6a 2060
aad29fbb 2061 intel_ring_unpin(engine->buffer);
7e37f889 2062 intel_ring_free(engine->buffer);
0bc40be8 2063 engine->buffer = NULL;
b0366a54 2064 }
78501eac 2065
0bc40be8
TU
2066 if (engine->cleanup)
2067 engine->cleanup(engine);
8d19215b 2068
3177659a 2069 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
0bc40be8
TU
2070 WARN_ON(engine->id != RCS);
2071 cleanup_phys_status_page(engine);
3177659a
CS
2072 } else {
2073 cleanup_status_page(engine);
7d3fdfff 2074 }
44e895a8 2075
96a945aa 2076 intel_engine_cleanup_common(engine);
0cb26a8e
CW
2077
2078 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2079
c033666a 2080 engine->i915 = NULL;
3b3f1650
AG
2081 dev_priv->engine[engine->id] = NULL;
2082 kfree(engine);
62fdfeaf
EA
2083}
2084
821ed7df
CW
2085void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
2086{
2087 struct intel_engine_cs *engine;
3b3f1650 2088 enum intel_engine_id id;
821ed7df 2089
3b3f1650 2090 for_each_engine(engine, dev_priv, id) {
821ed7df
CW
2091 engine->buffer->head = engine->buffer->tail;
2092 engine->buffer->last_retired_head = -1;
2093 }
2094}
2095
6689cb2b 2096int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
9d773091 2097{
6310346e
CW
2098 int ret;
2099
2100 /* Flush enough space to reduce the likelihood of waiting after
2101 * we start building the request - in which case we will just
2102 * have to repeat work.
2103 */
a0442461 2104 request->reserved_space += LEGACY_REQUEST_SIZE;
6310346e 2105
1dae2dfb 2106 request->ring = request->engine->buffer;
6310346e
CW
2107
2108 ret = intel_ring_begin(request, 0);
2109 if (ret)
2110 return ret;
2111
a0442461 2112 request->reserved_space -= LEGACY_REQUEST_SIZE;
6310346e 2113 return 0;
9d773091
CW
2114}
2115
987046ad
CW
2116static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
2117{
7e37f889 2118 struct intel_ring *ring = req->ring;
987046ad 2119 struct drm_i915_gem_request *target;
e95433c7
CW
2120 long timeout;
2121
2122 lockdep_assert_held(&req->i915->drm.struct_mutex);
987046ad 2123
1dae2dfb
CW
2124 intel_ring_update_space(ring);
2125 if (ring->space >= bytes)
987046ad
CW
2126 return 0;
2127
2128 /*
2129 * Space is reserved in the ringbuffer for finalising the request,
2130 * as that cannot be allowed to fail. During request finalisation,
2131 * reserved_space is set to 0 to stop the overallocation and the
2132 * assumption is that then we never need to wait (which has the
2133 * risk of failing with EINTR).
2134 *
2135 * See also i915_gem_request_alloc() and i915_add_request().
2136 */
0251a963 2137 GEM_BUG_ON(!req->reserved_space);
987046ad 2138
675d9ad7 2139 list_for_each_entry(target, &ring->request_list, ring_link) {
987046ad
CW
2140 unsigned space;
2141
987046ad 2142 /* Would completion of this request free enough space? */
1dae2dfb
CW
2143 space = __intel_ring_space(target->postfix, ring->tail,
2144 ring->size);
987046ad
CW
2145 if (space >= bytes)
2146 break;
79bbcc29 2147 }
29b1b415 2148
675d9ad7 2149 if (WARN_ON(&target->ring_link == &ring->request_list))
987046ad
CW
2150 return -ENOSPC;
2151
e95433c7
CW
2152 timeout = i915_wait_request(target,
2153 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
2154 MAX_SCHEDULE_TIMEOUT);
2155 if (timeout < 0)
2156 return timeout;
7da844c5 2157
7da844c5
CW
2158 i915_gem_request_retire_upto(target);
2159
2160 intel_ring_update_space(ring);
2161 GEM_BUG_ON(ring->space < bytes);
2162 return 0;
29b1b415
JH
2163}
2164
987046ad 2165int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
cbcc80df 2166{
7e37f889 2167 struct intel_ring *ring = req->ring;
1dae2dfb
CW
2168 int remain_actual = ring->size - ring->tail;
2169 int remain_usable = ring->effective_size - ring->tail;
987046ad
CW
2170 int bytes = num_dwords * sizeof(u32);
2171 int total_bytes, wait_bytes;
79bbcc29 2172 bool need_wrap = false;
29b1b415 2173
0251a963 2174 total_bytes = bytes + req->reserved_space;
29b1b415 2175
79bbcc29
JH
2176 if (unlikely(bytes > remain_usable)) {
2177 /*
2178 * Not enough space for the basic request. So need to flush
2179 * out the remainder and then wait for base + reserved.
2180 */
2181 wait_bytes = remain_actual + total_bytes;
2182 need_wrap = true;
987046ad
CW
2183 } else if (unlikely(total_bytes > remain_usable)) {
2184 /*
2185 * The base request will fit but the reserved space
2186 * falls off the end. So we don't need an immediate wrap
2187 * and only need to effectively wait for the reserved
2188 * size space from the start of ringbuffer.
2189 */
0251a963 2190 wait_bytes = remain_actual + req->reserved_space;
79bbcc29 2191 } else {
987046ad
CW
2192 /* No wrapping required, just waiting. */
2193 wait_bytes = total_bytes;
cbcc80df
MK
2194 }
2195
1dae2dfb 2196 if (wait_bytes > ring->space) {
987046ad 2197 int ret = wait_for_space(req, wait_bytes);
cbcc80df
MK
2198 if (unlikely(ret))
2199 return ret;
2200 }
2201
987046ad 2202 if (unlikely(need_wrap)) {
1dae2dfb
CW
2203 GEM_BUG_ON(remain_actual > ring->space);
2204 GEM_BUG_ON(ring->tail + remain_actual > ring->size);
78501eac 2205
987046ad 2206 /* Fill the tail with MI_NOOP */
1dae2dfb
CW
2207 memset(ring->vaddr + ring->tail, 0, remain_actual);
2208 ring->tail = 0;
2209 ring->space -= remain_actual;
987046ad 2210 }
304d695c 2211
1dae2dfb
CW
2212 ring->space -= bytes;
2213 GEM_BUG_ON(ring->space < 0);
304d695c 2214 return 0;
8187a2b7 2215}
78501eac 2216
753b1ad4 2217/* Align the ring tail to a cacheline boundary */
bba09b12 2218int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
753b1ad4 2219{
7e37f889 2220 struct intel_ring *ring = req->ring;
b5321f30
CW
2221 int num_dwords =
2222 (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
753b1ad4
VS
2223 int ret;
2224
2225 if (num_dwords == 0)
2226 return 0;
2227
18393f63 2228 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
5fb9de1a 2229 ret = intel_ring_begin(req, num_dwords);
753b1ad4
VS
2230 if (ret)
2231 return ret;
2232
2233 while (num_dwords--)
b5321f30 2234 intel_ring_emit(ring, MI_NOOP);
753b1ad4 2235
b5321f30 2236 intel_ring_advance(ring);
753b1ad4
VS
2237
2238 return 0;
2239}
2240
c5efa1ad 2241static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
881f47b6 2242{
c5efa1ad 2243 struct drm_i915_private *dev_priv = request->i915;
881f47b6 2244
76f8421f
CW
2245 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2246
881f47b6 2247 /* Every tail move must follow the sequence below */
12f55818
CW
2248
2249 /* Disable notification that the ring is IDLE. The GT
2250 * will then assume that it is busy and bring it out of rc6.
2251 */
76f8421f
CW
2252 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2253 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
12f55818
CW
2254
2255 /* Clear the context id. Here be magic! */
76f8421f 2256 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
0206e353 2257
12f55818 2258 /* Wait for the ring not to be idle, i.e. for it to wake up. */
76f8421f
CW
2259 if (intel_wait_for_register_fw(dev_priv,
2260 GEN6_BSD_SLEEP_PSMI_CONTROL,
2261 GEN6_BSD_SLEEP_INDICATOR,
2262 0,
2263 50))
12f55818 2264 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
0206e353 2265
12f55818 2266 /* Now that the ring is fully powered up, update the tail */
b0411e7d 2267 i9xx_submit_request(request);
12f55818
CW
2268
2269 /* Let the ring send IDLE messages to the GT again,
2270 * and so let it sleep to conserve power when idle.
2271 */
76f8421f
CW
2272 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2273 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2274
2275 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
881f47b6
XH
2276}
2277
7c9cf4e3 2278static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
881f47b6 2279{
7e37f889 2280 struct intel_ring *ring = req->ring;
71a77e07 2281 uint32_t cmd;
b72f3acb
CW
2282 int ret;
2283
5fb9de1a 2284 ret = intel_ring_begin(req, 4);
b72f3acb
CW
2285 if (ret)
2286 return ret;
2287
71a77e07 2288 cmd = MI_FLUSH_DW;
c033666a 2289 if (INTEL_GEN(req->i915) >= 8)
075b3bba 2290 cmd += 1;
f0a1fb10
CW
2291
2292 /* We always require a command barrier so that subsequent
2293 * commands, such as breadcrumb interrupts, are strictly ordered
2294 * wrt the contents of the write cache being flushed to memory
2295 * (and thus being coherent from the CPU).
2296 */
2297 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2298
9a289771
JB
2299 /*
2300 * Bspec vol 1c.5 - video engine command streamer:
2301 * "If ENABLED, all TLBs will be invalidated once the flush
2302 * operation is complete. This bit is only valid when the
2303 * Post-Sync Operation field is a value of 1h or 3h."
2304 */
7c9cf4e3 2305 if (mode & EMIT_INVALIDATE)
f0a1fb10
CW
2306 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2307
b5321f30
CW
2308 intel_ring_emit(ring, cmd);
2309 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
c033666a 2310 if (INTEL_GEN(req->i915) >= 8) {
b5321f30
CW
2311 intel_ring_emit(ring, 0); /* upper addr */
2312 intel_ring_emit(ring, 0); /* value */
075b3bba 2313 } else {
b5321f30
CW
2314 intel_ring_emit(ring, 0);
2315 intel_ring_emit(ring, MI_NOOP);
075b3bba 2316 }
b5321f30 2317 intel_ring_advance(ring);
b72f3acb 2318 return 0;
881f47b6
XH
2319}
2320
1c7a0623 2321static int
803688ba
CW
2322gen8_emit_bb_start(struct drm_i915_gem_request *req,
2323 u64 offset, u32 len,
2324 unsigned int dispatch_flags)
1c7a0623 2325{
7e37f889 2326 struct intel_ring *ring = req->ring;
b5321f30 2327 bool ppgtt = USES_PPGTT(req->i915) &&
8e004efc 2328 !(dispatch_flags & I915_DISPATCH_SECURE);
1c7a0623
BW
2329 int ret;
2330
5fb9de1a 2331 ret = intel_ring_begin(req, 4);
1c7a0623
BW
2332 if (ret)
2333 return ret;
2334
2335 /* FIXME(BDW): Address space and security selectors. */
b5321f30 2336 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
919032ec
AJ
2337 (dispatch_flags & I915_DISPATCH_RS ?
2338 MI_BATCH_RESOURCE_STREAMER : 0));
b5321f30
CW
2339 intel_ring_emit(ring, lower_32_bits(offset));
2340 intel_ring_emit(ring, upper_32_bits(offset));
2341 intel_ring_emit(ring, MI_NOOP);
2342 intel_ring_advance(ring);
1c7a0623
BW
2343
2344 return 0;
2345}
2346
d7d4eedd 2347static int
803688ba
CW
2348hsw_emit_bb_start(struct drm_i915_gem_request *req,
2349 u64 offset, u32 len,
2350 unsigned int dispatch_flags)
d7d4eedd 2351{
7e37f889 2352 struct intel_ring *ring = req->ring;
d7d4eedd
CW
2353 int ret;
2354
5fb9de1a 2355 ret = intel_ring_begin(req, 2);
d7d4eedd
CW
2356 if (ret)
2357 return ret;
2358
b5321f30 2359 intel_ring_emit(ring,
77072258 2360 MI_BATCH_BUFFER_START |
8e004efc 2361 (dispatch_flags & I915_DISPATCH_SECURE ?
919032ec
AJ
2362 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2363 (dispatch_flags & I915_DISPATCH_RS ?
2364 MI_BATCH_RESOURCE_STREAMER : 0));
d7d4eedd 2365 /* bit0-7 is the length on GEN6+ */
b5321f30
CW
2366 intel_ring_emit(ring, offset);
2367 intel_ring_advance(ring);
d7d4eedd
CW
2368
2369 return 0;
2370}
2371
881f47b6 2372static int
803688ba
CW
2373gen6_emit_bb_start(struct drm_i915_gem_request *req,
2374 u64 offset, u32 len,
2375 unsigned int dispatch_flags)
881f47b6 2376{
7e37f889 2377 struct intel_ring *ring = req->ring;
0206e353 2378 int ret;
ab6f8e32 2379
5fb9de1a 2380 ret = intel_ring_begin(req, 2);
0206e353
AJ
2381 if (ret)
2382 return ret;
e1f99ce6 2383
b5321f30 2384 intel_ring_emit(ring,
d7d4eedd 2385 MI_BATCH_BUFFER_START |
8e004efc
JH
2386 (dispatch_flags & I915_DISPATCH_SECURE ?
2387 0 : MI_BATCH_NON_SECURE_I965));
0206e353 2388 /* bit0-7 is the length on GEN6+ */
b5321f30
CW
2389 intel_ring_emit(ring, offset);
2390 intel_ring_advance(ring);
ab6f8e32 2391
0206e353 2392 return 0;
881f47b6
XH
2393}
2394
549f7365
CW
2395/* Blitter support (SandyBridge+) */
2396
7c9cf4e3 2397static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
8d19215b 2398{
7e37f889 2399 struct intel_ring *ring = req->ring;
71a77e07 2400 uint32_t cmd;
b72f3acb
CW
2401 int ret;
2402
5fb9de1a 2403 ret = intel_ring_begin(req, 4);
b72f3acb
CW
2404 if (ret)
2405 return ret;
2406
71a77e07 2407 cmd = MI_FLUSH_DW;
c033666a 2408 if (INTEL_GEN(req->i915) >= 8)
075b3bba 2409 cmd += 1;
f0a1fb10
CW
2410
2411 /* We always require a command barrier so that subsequent
2412 * commands, such as breadcrumb interrupts, are strictly ordered
2413 * wrt the contents of the write cache being flushed to memory
2414 * (and thus being coherent from the CPU).
2415 */
2416 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2417
9a289771
JB
2418 /*
2419 * Bspec vol 1c.3 - blitter engine command streamer:
2420 * "If ENABLED, all TLBs will be invalidated once the flush
2421 * operation is complete. This bit is only valid when the
2422 * Post-Sync Operation field is a value of 1h or 3h."
2423 */
7c9cf4e3 2424 if (mode & EMIT_INVALIDATE)
f0a1fb10 2425 cmd |= MI_INVALIDATE_TLB;
b5321f30
CW
2426 intel_ring_emit(ring, cmd);
2427 intel_ring_emit(ring,
e2f80391 2428 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
c033666a 2429 if (INTEL_GEN(req->i915) >= 8) {
b5321f30
CW
2430 intel_ring_emit(ring, 0); /* upper addr */
2431 intel_ring_emit(ring, 0); /* value */
075b3bba 2432 } else {
b5321f30
CW
2433 intel_ring_emit(ring, 0);
2434 intel_ring_emit(ring, MI_NOOP);
075b3bba 2435 }
b5321f30 2436 intel_ring_advance(ring);
fd3da6c9 2437
b72f3acb 2438 return 0;
8d19215b
ZN
2439}
2440
d9a64610
TU
2441static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2442 struct intel_engine_cs *engine)
2443{
db3d4019 2444 struct drm_i915_gem_object *obj;
1b9e6650 2445 int ret, i;
db3d4019 2446
39df9190 2447 if (!i915.semaphores)
db3d4019
TU
2448 return;
2449
51d545d0
CW
2450 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
2451 struct i915_vma *vma;
2452
91c8a326 2453 obj = i915_gem_object_create(&dev_priv->drm, 4096);
51d545d0
CW
2454 if (IS_ERR(obj))
2455 goto err;
db3d4019 2456
51d545d0
CW
2457 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
2458 if (IS_ERR(vma))
2459 goto err_obj;
2460
2461 ret = i915_gem_object_set_to_gtt_domain(obj, false);
2462 if (ret)
2463 goto err_obj;
2464
2465 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
2466 if (ret)
2467 goto err_obj;
2468
2469 dev_priv->semaphore = vma;
2470 }
d9a64610
TU
2471
2472 if (INTEL_GEN(dev_priv) >= 8) {
bde13ebd 2473 u32 offset = i915_ggtt_offset(dev_priv->semaphore);
1b9e6650 2474
ad7bdb2b 2475 engine->semaphore.sync_to = gen8_ring_sync_to;
d9a64610 2476 engine->semaphore.signal = gen8_xcs_signal;
1b9e6650
TU
2477
2478 for (i = 0; i < I915_NUM_ENGINES; i++) {
bde13ebd 2479 u32 ring_offset;
1b9e6650
TU
2480
2481 if (i != engine->id)
2482 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2483 else
2484 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2485
2486 engine->semaphore.signal_ggtt[i] = ring_offset;
2487 }
d9a64610 2488 } else if (INTEL_GEN(dev_priv) >= 6) {
ad7bdb2b 2489 engine->semaphore.sync_to = gen6_ring_sync_to;
d9a64610 2490 engine->semaphore.signal = gen6_signal;
4b8e38a9
TU
2491
2492 /*
2493 * The current semaphore is only applied on pre-gen8
2494 * platform. And there is no VCS2 ring on the pre-gen8
2495 * platform. So the semaphore between RCS and VCS2 is
2496 * initialized as INVALID. Gen8 will initialize the
2497 * sema between VCS2 and RCS later.
2498 */
318f89ca 2499 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
4b8e38a9
TU
2500 static const struct {
2501 u32 wait_mbox;
2502 i915_reg_t mbox_reg;
318f89ca
TU
2503 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
2504 [RCS_HW] = {
2505 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2506 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2507 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
4b8e38a9 2508 },
318f89ca
TU
2509 [VCS_HW] = {
2510 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2511 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2512 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
4b8e38a9 2513 },
318f89ca
TU
2514 [BCS_HW] = {
2515 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2516 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2517 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
4b8e38a9 2518 },
318f89ca
TU
2519 [VECS_HW] = {
2520 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2521 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2522 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
4b8e38a9
TU
2523 },
2524 };
2525 u32 wait_mbox;
2526 i915_reg_t mbox_reg;
2527
318f89ca 2528 if (i == engine->hw_id) {
4b8e38a9
TU
2529 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2530 mbox_reg = GEN6_NOSYNC;
2531 } else {
318f89ca
TU
2532 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
2533 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
4b8e38a9
TU
2534 }
2535
2536 engine->semaphore.mbox.wait[i] = wait_mbox;
2537 engine->semaphore.mbox.signal[i] = mbox_reg;
2538 }
d9a64610 2539 }
51d545d0
CW
2540
2541 return;
2542
2543err_obj:
2544 i915_gem_object_put(obj);
2545err:
2546 DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
2547 i915.semaphores = 0;
d9a64610
TU
2548}
2549
ed003078
CW
2550static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2551 struct intel_engine_cs *engine)
2552{
c78d6061
TU
2553 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2554
ed003078 2555 if (INTEL_GEN(dev_priv) >= 8) {
31bb59cc
CW
2556 engine->irq_enable = gen8_irq_enable;
2557 engine->irq_disable = gen8_irq_disable;
ed003078
CW
2558 engine->irq_seqno_barrier = gen6_seqno_barrier;
2559 } else if (INTEL_GEN(dev_priv) >= 6) {
31bb59cc
CW
2560 engine->irq_enable = gen6_irq_enable;
2561 engine->irq_disable = gen6_irq_disable;
ed003078
CW
2562 engine->irq_seqno_barrier = gen6_seqno_barrier;
2563 } else if (INTEL_GEN(dev_priv) >= 5) {
31bb59cc
CW
2564 engine->irq_enable = gen5_irq_enable;
2565 engine->irq_disable = gen5_irq_disable;
f8973c21 2566 engine->irq_seqno_barrier = gen5_seqno_barrier;
ed003078 2567 } else if (INTEL_GEN(dev_priv) >= 3) {
31bb59cc
CW
2568 engine->irq_enable = i9xx_irq_enable;
2569 engine->irq_disable = i9xx_irq_disable;
ed003078 2570 } else {
31bb59cc
CW
2571 engine->irq_enable = i8xx_irq_enable;
2572 engine->irq_disable = i8xx_irq_disable;
ed003078
CW
2573 }
2574}
2575
06a2fe22
TU
2576static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2577 struct intel_engine_cs *engine)
2578{
618e4ca7
CW
2579 intel_ring_init_irq(dev_priv, engine);
2580 intel_ring_init_semaphores(dev_priv, engine);
2581
1d8a1337 2582 engine->init_hw = init_ring_common;
821ed7df 2583 engine->reset_hw = reset_ring_common;
7445a2a4 2584
9b81d556 2585 engine->emit_breadcrumb = i9xx_emit_breadcrumb;
98f29e8d
CW
2586 engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
2587 if (i915.semaphores) {
2588 int num_rings;
2589
9b81d556 2590 engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
98f29e8d
CW
2591
2592 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
2593 if (INTEL_GEN(dev_priv) >= 8) {
2594 engine->emit_breadcrumb_sz += num_rings * 6;
2595 } else {
2596 engine->emit_breadcrumb_sz += num_rings * 3;
2597 if (num_rings & 1)
2598 engine->emit_breadcrumb_sz++;
2599 }
2600 }
ddd66c51 2601 engine->submit_request = i9xx_submit_request;
6f7bef75
CW
2602
2603 if (INTEL_GEN(dev_priv) >= 8)
803688ba 2604 engine->emit_bb_start = gen8_emit_bb_start;
6f7bef75 2605 else if (INTEL_GEN(dev_priv) >= 6)
803688ba 2606 engine->emit_bb_start = gen6_emit_bb_start;
6f7bef75 2607 else if (INTEL_GEN(dev_priv) >= 4)
803688ba 2608 engine->emit_bb_start = i965_emit_bb_start;
6f7bef75 2609 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
803688ba 2610 engine->emit_bb_start = i830_emit_bb_start;
6f7bef75 2611 else
803688ba 2612 engine->emit_bb_start = i915_emit_bb_start;
06a2fe22
TU
2613}
2614
8b3e2d36 2615int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
5c1143bb 2616{
8b3e2d36 2617 struct drm_i915_private *dev_priv = engine->i915;
3e78998a 2618 int ret;
5c1143bb 2619
06a2fe22
TU
2620 intel_ring_default_vfuncs(dev_priv, engine);
2621
61ff75ac
CW
2622 if (HAS_L3_DPF(dev_priv))
2623 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
f8973c21 2624
c033666a 2625 if (INTEL_GEN(dev_priv) >= 8) {
e2f80391 2626 engine->init_context = intel_rcs_ctx_init;
9b81d556 2627 engine->emit_breadcrumb = gen8_render_emit_breadcrumb;
98f29e8d 2628 engine->emit_breadcrumb_sz = gen8_render_emit_breadcrumb_sz;
c7fe7d25 2629 engine->emit_flush = gen8_render_ring_flush;
98f29e8d
CW
2630 if (i915.semaphores) {
2631 int num_rings;
2632
e2f80391 2633 engine->semaphore.signal = gen8_rcs_signal;
98f29e8d
CW
2634
2635 num_rings =
2636 hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
2637 engine->emit_breadcrumb_sz += num_rings * 6;
2638 }
c033666a 2639 } else if (INTEL_GEN(dev_priv) >= 6) {
e2f80391 2640 engine->init_context = intel_rcs_ctx_init;
c7fe7d25 2641 engine->emit_flush = gen7_render_ring_flush;
c033666a 2642 if (IS_GEN6(dev_priv))
c7fe7d25 2643 engine->emit_flush = gen6_render_ring_flush;
c033666a 2644 } else if (IS_GEN5(dev_priv)) {
c7fe7d25 2645 engine->emit_flush = gen4_render_ring_flush;
59465b5f 2646 } else {
c033666a 2647 if (INTEL_GEN(dev_priv) < 4)
c7fe7d25 2648 engine->emit_flush = gen2_render_ring_flush;
46f0f8d1 2649 else
c7fe7d25 2650 engine->emit_flush = gen4_render_ring_flush;
e2f80391 2651 engine->irq_enable_mask = I915_USER_INTERRUPT;
1ec14ad3 2652 }
707d9cf9 2653
c033666a 2654 if (IS_HASWELL(dev_priv))
803688ba 2655 engine->emit_bb_start = hsw_emit_bb_start;
6f7bef75 2656
e2f80391
TU
2657 engine->init_hw = init_render_ring;
2658 engine->cleanup = render_ring_cleanup;
59465b5f 2659
acd27845 2660 ret = intel_init_ring_buffer(engine);
99be1dfe
SV
2661 if (ret)
2662 return ret;
2663
f8973c21 2664 if (INTEL_GEN(dev_priv) >= 6) {
56c0f1a7 2665 ret = intel_engine_create_scratch(engine, 4096);
7d5ea807
CW
2666 if (ret)
2667 return ret;
2668 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
56c0f1a7 2669 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
99be1dfe
SV
2670 if (ret)
2671 return ret;
2672 }
2673
2674 return 0;
5c1143bb
XH
2675}
2676
8b3e2d36 2677int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
5c1143bb 2678{
8b3e2d36 2679 struct drm_i915_private *dev_priv = engine->i915;
58fa3835 2680
06a2fe22
TU
2681 intel_ring_default_vfuncs(dev_priv, engine);
2682
c033666a 2683 if (INTEL_GEN(dev_priv) >= 6) {
0fd2c201 2684 /* gen6 bsd needs a special wa for tail updates */
c033666a 2685 if (IS_GEN6(dev_priv))
c5efa1ad 2686 engine->submit_request = gen6_bsd_submit_request;
c7fe7d25 2687 engine->emit_flush = gen6_bsd_ring_flush;
c78d6061 2688 if (INTEL_GEN(dev_priv) < 8)
e2f80391 2689 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
58fa3835 2690 } else {
e2f80391 2691 engine->mmio_base = BSD_RING_BASE;
c7fe7d25 2692 engine->emit_flush = bsd_ring_flush;
8d228911 2693 if (IS_GEN5(dev_priv))
e2f80391 2694 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
8d228911 2695 else
e2f80391 2696 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
58fa3835 2697 }
58fa3835 2698
acd27845 2699 return intel_init_ring_buffer(engine);
5c1143bb 2700}
549f7365 2701
845f74a7 2702/**
62659920 2703 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
845f74a7 2704 */
8b3e2d36 2705int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
845f74a7 2706{
8b3e2d36 2707 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2708
2709 intel_ring_default_vfuncs(dev_priv, engine);
2710
c7fe7d25 2711 engine->emit_flush = gen6_bsd_ring_flush;
845f74a7 2712
acd27845 2713 return intel_init_ring_buffer(engine);
845f74a7
ZY
2714}
2715
8b3e2d36 2716int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
549f7365 2717{
8b3e2d36 2718 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2719
2720 intel_ring_default_vfuncs(dev_priv, engine);
2721
c7fe7d25 2722 engine->emit_flush = gen6_ring_flush;
c78d6061 2723 if (INTEL_GEN(dev_priv) < 8)
e2f80391 2724 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
549f7365 2725
acd27845 2726 return intel_init_ring_buffer(engine);
549f7365 2727}
a7b9761d 2728
8b3e2d36 2729int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
9a8a2213 2730{
8b3e2d36 2731 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2732
2733 intel_ring_default_vfuncs(dev_priv, engine);
2734
c7fe7d25 2735 engine->emit_flush = gen6_ring_flush;
abd58f01 2736
c78d6061 2737 if (INTEL_GEN(dev_priv) < 8) {
e2f80391 2738 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
31bb59cc
CW
2739 engine->irq_enable = hsw_vebox_irq_enable;
2740 engine->irq_disable = hsw_vebox_irq_disable;
abd58f01 2741 }
9a8a2213 2742
acd27845 2743 return intel_init_ring_buffer(engine);
9a8a2213 2744}
This page took 1.473788 seconds and 4 git commands to generate.