]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/gpu/drm/i915/intel_ringbuffer.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc
[mirror_ubuntu-eoan-kernel.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 <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
27 *
28 */
29
a4d8a0fe 30#include <linux/log2.h>
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
DV
411 u32 addr;
412
413 addr = dev_priv->status_page_dmah->busaddr;
c033666a 414 if (INTEL_GEN(dev_priv) >= 4)
035dc1e0
DV
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
DV
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
DV
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
DV
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{
ec62ed3e
CW
602 /* Try to restore the logical GPU state to match the continuation
603 * of the request queue. If we skip the context/PD restore, then
604 * the next request may try to execute assuming that its context
605 * is valid and loaded on the GPU and so may try to access invalid
606 * memory, prompting repeated GPU hangs.
607 *
608 * If the request was guilty, we still restore the logical state
609 * in case the next request requires it (e.g. the aliasing ppgtt),
610 * but skip over the hung batch.
611 *
612 * If the request was innocent, we try to replay the request with
613 * the restored context.
614 */
615 if (request) {
616 struct drm_i915_private *dev_priv = request->i915;
617 struct intel_context *ce = &request->ctx->engine[engine->id];
618 struct i915_hw_ppgtt *ppgtt;
619
620 /* FIXME consider gen8 reset */
621
622 if (ce->state) {
623 I915_WRITE(CCID,
624 i915_ggtt_offset(ce->state) |
625 BIT(8) /* must be set! */ |
626 CCID_EXTENDED_STATE_SAVE |
627 CCID_EXTENDED_STATE_RESTORE |
628 CCID_EN);
629 }
630
631 ppgtt = request->ctx->ppgtt ?: engine->i915->mm.aliasing_ppgtt;
632 if (ppgtt) {
633 u32 pd_offset = ppgtt->pd.base.ggtt_offset << 10;
634
635 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
636 I915_WRITE(RING_PP_DIR_BASE(engine), pd_offset);
637
638 /* Wait for the PD reload to complete */
639 if (intel_wait_for_register(dev_priv,
640 RING_PP_DIR_BASE(engine),
641 BIT(0), 0,
642 10))
643 DRM_ERROR("Wait for reload of ppgtt page-directory timed out\n");
821ed7df 644
ec62ed3e
CW
645 ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
646 }
647
648 /* If the rq hung, jump to its breadcrumb and skip the batch */
649 if (request->fence.error == -EIO) {
650 struct intel_ring *ring = request->ring;
651
652 ring->head = request->postfix;
653 ring->last_retired_head = -1;
654 }
655 } else {
656 engine->legacy_active_context = NULL;
657 }
821ed7df
CW
658}
659
e2be4faf 660static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
86d7f238 661{
7e37f889 662 struct intel_ring *ring = req->ring;
c033666a
CW
663 struct i915_workarounds *w = &req->i915->workarounds;
664 int ret, i;
888b5995 665
02235808 666 if (w->count == 0)
7225342a 667 return 0;
888b5995 668
7c9cf4e3 669 ret = req->engine->emit_flush(req, EMIT_BARRIER);
7225342a
MK
670 if (ret)
671 return ret;
888b5995 672
5fb9de1a 673 ret = intel_ring_begin(req, (w->count * 2 + 2));
7225342a
MK
674 if (ret)
675 return ret;
676
b5321f30 677 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
7225342a 678 for (i = 0; i < w->count; i++) {
b5321f30
CW
679 intel_ring_emit_reg(ring, w->reg[i].addr);
680 intel_ring_emit(ring, w->reg[i].value);
7225342a 681 }
b5321f30 682 intel_ring_emit(ring, MI_NOOP);
7225342a 683
b5321f30 684 intel_ring_advance(ring);
7225342a 685
7c9cf4e3 686 ret = req->engine->emit_flush(req, EMIT_BARRIER);
7225342a
MK
687 if (ret)
688 return ret;
888b5995 689
7225342a 690 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
888b5995 691
7225342a 692 return 0;
86d7f238
AS
693}
694
8753181e 695static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
8f0e2b9d
DV
696{
697 int ret;
698
e2be4faf 699 ret = intel_ring_workarounds_emit(req);
8f0e2b9d
DV
700 if (ret != 0)
701 return ret;
702
4e50f082 703 ret = i915_gem_render_state_emit(req);
8f0e2b9d 704 if (ret)
e26e1b97 705 return ret;
8f0e2b9d 706
e26e1b97 707 return 0;
8f0e2b9d
DV
708}
709
7225342a 710static int wa_add(struct drm_i915_private *dev_priv,
f0f59a00
VS
711 i915_reg_t addr,
712 const u32 mask, const u32 val)
7225342a
MK
713{
714 const u32 idx = dev_priv->workarounds.count;
715
716 if (WARN_ON(idx >= I915_MAX_WA_REGS))
717 return -ENOSPC;
718
719 dev_priv->workarounds.reg[idx].addr = addr;
720 dev_priv->workarounds.reg[idx].value = val;
721 dev_priv->workarounds.reg[idx].mask = mask;
722
723 dev_priv->workarounds.count++;
724
725 return 0;
86d7f238
AS
726}
727
ca5a0fbd 728#define WA_REG(addr, mask, val) do { \
cf4b0de6 729 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
7225342a
MK
730 if (r) \
731 return r; \
ca5a0fbd 732 } while (0)
7225342a
MK
733
734#define WA_SET_BIT_MASKED(addr, mask) \
26459343 735 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
7225342a
MK
736
737#define WA_CLR_BIT_MASKED(addr, mask) \
26459343 738 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
7225342a 739
98533251 740#define WA_SET_FIELD_MASKED(addr, mask, value) \
cf4b0de6 741 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
7225342a 742
cf4b0de6
DL
743#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
744#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
7225342a 745
cf4b0de6 746#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
7225342a 747
0bc40be8
TU
748static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
749 i915_reg_t reg)
33136b06 750{
c033666a 751 struct drm_i915_private *dev_priv = engine->i915;
33136b06 752 struct i915_workarounds *wa = &dev_priv->workarounds;
0bc40be8 753 const uint32_t index = wa->hw_whitelist_count[engine->id];
33136b06
AS
754
755 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
756 return -EINVAL;
757
0bc40be8 758 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
33136b06 759 i915_mmio_reg_offset(reg));
0bc40be8 760 wa->hw_whitelist_count[engine->id]++;
33136b06
AS
761
762 return 0;
763}
764
0bc40be8 765static int gen8_init_workarounds(struct intel_engine_cs *engine)
e9a64ada 766{
c033666a 767 struct drm_i915_private *dev_priv = engine->i915;
68c6198b
AS
768
769 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
e9a64ada 770
717d84d6
AS
771 /* WaDisableAsyncFlipPerfMode:bdw,chv */
772 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
773
d0581194
AS
774 /* WaDisablePartialInstShootdown:bdw,chv */
775 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
776 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
777
a340af58
AS
778 /* Use Force Non-Coherent whenever executing a 3D context. This is a
779 * workaround for for a possible hang in the unlikely event a TLB
780 * invalidation occurs during a PSD flush.
781 */
782 /* WaForceEnableNonCoherent:bdw,chv */
120f5d28 783 /* WaHdcDisableFetchWhenMasked:bdw,chv */
a340af58 784 WA_SET_BIT_MASKED(HDC_CHICKEN0,
120f5d28 785 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
a340af58
AS
786 HDC_FORCE_NON_COHERENT);
787
6def8fdd
AS
788 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
789 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
790 * polygons in the same 8x4 pixel/sample area to be processed without
791 * stalling waiting for the earlier ones to write to Hierarchical Z
792 * buffer."
793 *
794 * This optimization is off by default for BDW and CHV; turn it on.
795 */
796 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
797
48404636
AS
798 /* Wa4x4STCOptimizationDisable:bdw,chv */
799 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
800
7eebcde6
AS
801 /*
802 * BSpec recommends 8x4 when MSAA is used,
803 * however in practice 16x4 seems fastest.
804 *
805 * Note that PS/WM thread counts depend on the WIZ hashing
806 * disable bit, which we don't touch here, but it's good
807 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
808 */
809 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
810 GEN6_WIZ_HASHING_MASK,
811 GEN6_WIZ_HASHING_16x4);
812
e9a64ada
AS
813 return 0;
814}
815
0bc40be8 816static int bdw_init_workarounds(struct intel_engine_cs *engine)
86d7f238 817{
c033666a 818 struct drm_i915_private *dev_priv = engine->i915;
e9a64ada 819 int ret;
86d7f238 820
0bc40be8 821 ret = gen8_init_workarounds(engine);
e9a64ada
AS
822 if (ret)
823 return ret;
824
101b376d 825 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
d0581194 826 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
86d7f238 827
101b376d 828 /* WaDisableDopClockGating:bdw */
7225342a
MK
829 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
830 DOP_CLOCK_GATING_DISABLE);
86d7f238 831
7225342a
MK
832 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
833 GEN8_SAMPLER_POWER_BYPASS_DIS);
86d7f238 834
7225342a 835 WA_SET_BIT_MASKED(HDC_CHICKEN0,
35cb6f3b
DL
836 /* WaForceContextSaveRestoreNonCoherent:bdw */
837 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
35cb6f3b 838 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
c033666a 839 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
86d7f238 840
86d7f238
AS
841 return 0;
842}
843
0bc40be8 844static int chv_init_workarounds(struct intel_engine_cs *engine)
00e1e623 845{
c033666a 846 struct drm_i915_private *dev_priv = engine->i915;
e9a64ada 847 int ret;
00e1e623 848
0bc40be8 849 ret = gen8_init_workarounds(engine);
e9a64ada
AS
850 if (ret)
851 return ret;
852
00e1e623 853 /* WaDisableThreadStallDopClockGating:chv */
d0581194 854 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
00e1e623 855
d60de81d
KG
856 /* Improve HiZ throughput on CHV. */
857 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
858
7225342a
MK
859 return 0;
860}
861
0bc40be8 862static int gen9_init_workarounds(struct intel_engine_cs *engine)
3b106531 863{
c033666a 864 struct drm_i915_private *dev_priv = engine->i915;
e0f3fa09 865 int ret;
ab0dfafe 866
a8ab5ed5
TG
867 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
868 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
869
e5f81d65 870 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
9c4cbf82
MK
871 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
872 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
873
e5f81d65 874 /* WaDisableKillLogic:bxt,skl,kbl */
9c4cbf82
MK
875 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
876 ECOCHK_DIS_TLB);
877
e5f81d65
MK
878 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
879 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
ab0dfafe 880 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
950b2aae 881 FLOW_CONTROL_ENABLE |
ab0dfafe
HN
882 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
883
e5f81d65 884 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
8424171e
NH
885 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
886 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
887
a117f378
JN
888 /* WaDisableDgMirrorFixInHalfSliceChicken5:bxt */
889 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
a86eb582
DL
890 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
891 GEN9_DG_MIRROR_FIX_ENABLE);
1de4582f 892
a117f378
JN
893 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:bxt */
894 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
183c6dac
DL
895 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
896 GEN9_RHWO_OPTIMIZATION_DISABLE);
9b01435d
AS
897 /*
898 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
899 * but we do that in per ctx batchbuffer as there is an issue
900 * with this register not getting restored on ctx restore
901 */
183c6dac
DL
902 }
903
e5f81d65 904 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
bfd8ad4e 905 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
bfd8ad4e 906 GEN9_ENABLE_GPGPU_PREEMPTION);
cac23df4 907
e5f81d65
MK
908 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
909 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
60294683
AS
910 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
911 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
9370cd98 912
e5f81d65 913 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
e2db7071
DL
914 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
915 GEN9_CCS_TLB_PREFETCH_ENABLE);
916
0d0b8dcf
JN
917 /* WaDisableMaskBasedCammingInRCC:bxt */
918 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
38a39a7b
BW
919 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
920 PIXEL_MASK_CAMMING_DISABLE);
921
5b0e3659
MK
922 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
923 WA_SET_BIT_MASKED(HDC_CHICKEN0,
924 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
925 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
8ea6f892 926
bbaefe72
MK
927 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
928 * both tied to WaForceContextSaveRestoreNonCoherent
929 * in some hsds for skl. We keep the tie for all gen9. The
930 * documentation is a bit hazy and so we want to get common behaviour,
931 * even though there is no clear evidence we would need both on kbl/bxt.
932 * This area has been source of system hangs so we play it safe
933 * and mimic the skl regardless of what bspec says.
934 *
935 * Use Force Non-Coherent whenever executing a 3D context. This
936 * is a workaround for a possible hang in the unlikely event
937 * a TLB invalidation occurs during a PSD flush.
938 */
939
940 /* WaForceEnableNonCoherent:skl,bxt,kbl */
941 WA_SET_BIT_MASKED(HDC_CHICKEN0,
942 HDC_FORCE_NON_COHERENT);
943
944 /* WaDisableHDCInvalidation:skl,bxt,kbl */
945 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
946 BDW_DISABLE_HDC_INVALIDATION);
947
e5f81d65
MK
948 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
949 if (IS_SKYLAKE(dev_priv) ||
950 IS_KABYLAKE(dev_priv) ||
951 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
8c761609
AS
952 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
953 GEN8_SAMPLER_POWER_BYPASS_DIS);
8c761609 954
e5f81d65 955 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
6b6d5626
RB
956 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
957
e5f81d65 958 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
6ecf56ae
AS
959 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
960 GEN8_LQSC_FLUSH_COHERENT_LINES));
961
6bb62855 962 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
963 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
964 if (ret)
965 return ret;
966
e5f81d65 967 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
0bc40be8 968 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
e0f3fa09
AS
969 if (ret)
970 return ret;
971
e5f81d65 972 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
0bc40be8 973 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
3669ab61
AS
974 if (ret)
975 return ret;
976
3b106531
HN
977 return 0;
978}
979
0bc40be8 980static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
b7668791 981{
c033666a 982 struct drm_i915_private *dev_priv = engine->i915;
b7668791
DL
983 u8 vals[3] = { 0, 0, 0 };
984 unsigned int i;
985
986 for (i = 0; i < 3; i++) {
987 u8 ss;
988
989 /*
990 * Only consider slices where one, and only one, subslice has 7
991 * EUs
992 */
43b67998 993 if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
b7668791
DL
994 continue;
995
996 /*
997 * subslice_7eu[i] != 0 (because of the check above) and
998 * ss_max == 4 (maximum number of subslices possible per slice)
999 *
1000 * -> 0 <= ss <= 3;
1001 */
43b67998 1002 ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
b7668791
DL
1003 vals[i] = 3 - ss;
1004 }
1005
1006 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1007 return 0;
1008
1009 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1010 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1011 GEN9_IZ_HASHING_MASK(2) |
1012 GEN9_IZ_HASHING_MASK(1) |
1013 GEN9_IZ_HASHING_MASK(0),
1014 GEN9_IZ_HASHING(2, vals[2]) |
1015 GEN9_IZ_HASHING(1, vals[1]) |
1016 GEN9_IZ_HASHING(0, vals[0]));
1017
1018 return 0;
1019}
1020
0bc40be8 1021static int skl_init_workarounds(struct intel_engine_cs *engine)
8d205494 1022{
c033666a 1023 struct drm_i915_private *dev_priv = engine->i915;
aa0011a8 1024 int ret;
d0bbbc4f 1025
0bc40be8 1026 ret = gen9_init_workarounds(engine);
aa0011a8
AS
1027 if (ret)
1028 return ret;
8d205494 1029
a78536e7
AS
1030 /*
1031 * Actual WA is to disable percontext preemption granularity control
1032 * until D0 which is the default case so this is equivalent to
1033 * !WaDisablePerCtxtPreemptionGranularityControl:skl
1034 */
9fc736e8
JN
1035 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1036 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
a78536e7 1037
9c4cbf82 1038 /* WaEnableGapsTsvCreditFix:skl */
a117f378
JN
1039 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1040 GEN9_GAPS_TSV_CREDIT_DISABLE));
d0bbbc4f 1041
eee8efb0
MK
1042 /* WaDisableGafsUnitClkGating:skl */
1043 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1044
4ba9c1f7
MK
1045 /* WaInPlaceDecompressionHang:skl */
1046 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
1047 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1048 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1049
6107497e 1050 /* WaDisableLSQCROPERFforOCL:skl */
0bc40be8 1051 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
6107497e
AS
1052 if (ret)
1053 return ret;
1054
0bc40be8 1055 return skl_tune_iz_hashing(engine);
7225342a
MK
1056}
1057
0bc40be8 1058static int bxt_init_workarounds(struct intel_engine_cs *engine)
cae0437f 1059{
c033666a 1060 struct drm_i915_private *dev_priv = engine->i915;
aa0011a8 1061 int ret;
dfb601e6 1062
0bc40be8 1063 ret = gen9_init_workarounds(engine);
aa0011a8
AS
1064 if (ret)
1065 return ret;
cae0437f 1066
9c4cbf82
MK
1067 /* WaStoreMultiplePTEenable:bxt */
1068 /* This is a requirement according to Hardware specification */
c033666a 1069 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
9c4cbf82
MK
1070 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1071
1072 /* WaSetClckGatingDisableMedia:bxt */
c033666a 1073 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
9c4cbf82
MK
1074 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1075 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1076 }
1077
dfb601e6
NH
1078 /* WaDisableThreadStallDopClockGating:bxt */
1079 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1080 STALL_DOP_GATING_DISABLE);
1081
780f0aeb 1082 /* WaDisablePooledEuLoadBalancingFix:bxt */
1083 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1084 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1085 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1086 }
1087
983b4b9d 1088 /* WaDisableSbeCacheDispatchPortSharing:bxt */
c033666a 1089 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
983b4b9d
NH
1090 WA_SET_BIT_MASKED(
1091 GEN7_HALF_SLICE_CHICKEN1,
1092 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1093 }
1094
2c8580e4
AS
1095 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1096 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1097 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
a786d53a 1098 /* WaDisableLSQCROPERFforOCL:bxt */
c033666a 1099 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
0bc40be8 1100 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
2c8580e4
AS
1101 if (ret)
1102 return ret;
a786d53a 1103
0bc40be8 1104 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
a786d53a
AS
1105 if (ret)
1106 return ret;
2c8580e4
AS
1107 }
1108
050fc465 1109 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
c033666a 1110 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
36579cb6
ID
1111 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1112 L3_HIGH_PRIO_CREDITS(2));
050fc465 1113
575e3ccb
MA
1114 /* WaToEnableHwFixForPushConstHWBug:bxt */
1115 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
ad2bdb44
MK
1116 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1117 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1118
4ba9c1f7
MK
1119 /* WaInPlaceDecompressionHang:bxt */
1120 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
1121 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1122 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1123
cae0437f
NH
1124 return 0;
1125}
1126
e5f81d65
MK
1127static int kbl_init_workarounds(struct intel_engine_cs *engine)
1128{
e587f6cb 1129 struct drm_i915_private *dev_priv = engine->i915;
e5f81d65
MK
1130 int ret;
1131
1132 ret = gen9_init_workarounds(engine);
1133 if (ret)
1134 return ret;
1135
e587f6cb
MK
1136 /* WaEnableGapsTsvCreditFix:kbl */
1137 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1138 GEN9_GAPS_TSV_CREDIT_DISABLE));
1139
c0b730d5
MK
1140 /* WaDisableDynamicCreditSharing:kbl */
1141 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1142 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1143 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1144
8401d42f
MK
1145 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1146 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1147 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1148 HDC_FENCE_DEST_SLM_DISABLE);
1149
575e3ccb
MA
1150 /* WaToEnableHwFixForPushConstHWBug:kbl */
1151 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
ad2bdb44
MK
1152 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1153 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1154
4de5d7cc
MK
1155 /* WaDisableGafsUnitClkGating:kbl */
1156 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1157
954337aa
MK
1158 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1159 WA_SET_BIT_MASKED(
1160 GEN7_HALF_SLICE_CHICKEN1,
1161 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1162
4ba9c1f7
MK
1163 /* WaInPlaceDecompressionHang:kbl */
1164 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1165 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1166
fe905819
MK
1167 /* WaDisableLSQCROPERFforOCL:kbl */
1168 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1169 if (ret)
1170 return ret;
1171
e5f81d65
MK
1172 return 0;
1173}
1174
0bc40be8 1175int init_workarounds_ring(struct intel_engine_cs *engine)
7225342a 1176{
c033666a 1177 struct drm_i915_private *dev_priv = engine->i915;
7225342a 1178
0bc40be8 1179 WARN_ON(engine->id != RCS);
7225342a
MK
1180
1181 dev_priv->workarounds.count = 0;
33136b06 1182 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
7225342a 1183
c033666a 1184 if (IS_BROADWELL(dev_priv))
0bc40be8 1185 return bdw_init_workarounds(engine);
7225342a 1186
c033666a 1187 if (IS_CHERRYVIEW(dev_priv))
0bc40be8 1188 return chv_init_workarounds(engine);
00e1e623 1189
c033666a 1190 if (IS_SKYLAKE(dev_priv))
0bc40be8 1191 return skl_init_workarounds(engine);
cae0437f 1192
c033666a 1193 if (IS_BROXTON(dev_priv))
0bc40be8 1194 return bxt_init_workarounds(engine);
3b106531 1195
e5f81d65
MK
1196 if (IS_KABYLAKE(dev_priv))
1197 return kbl_init_workarounds(engine);
1198
00e1e623
VS
1199 return 0;
1200}
1201
0bc40be8 1202static int init_render_ring(struct intel_engine_cs *engine)
8187a2b7 1203{
c033666a 1204 struct drm_i915_private *dev_priv = engine->i915;
0bc40be8 1205 int ret = init_ring_common(engine);
9c33baa6
KZ
1206 if (ret)
1207 return ret;
a69ffdbf 1208
61a563a2 1209 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
ac657f64 1210 if (IS_GEN(dev_priv, 4, 6))
6b26c86d 1211 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1c8c38c5
CW
1212
1213 /* We need to disable the AsyncFlip performance optimisations in order
1214 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1215 * programmed to '1' on all products.
8693a824 1216 *
2441f877 1217 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1c8c38c5 1218 */
ac657f64 1219 if (IS_GEN(dev_priv, 6, 7))
1c8c38c5
CW
1220 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1221
f05bb0c7 1222 /* Required for the hardware to program scanline values for waiting */
01fa0302 1223 /* WaEnableFlushTlbInvalidationMode:snb */
c033666a 1224 if (IS_GEN6(dev_priv))
f05bb0c7 1225 I915_WRITE(GFX_MODE,
aa83e30d 1226 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
f05bb0c7 1227
01fa0302 1228 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
c033666a 1229 if (IS_GEN7(dev_priv))
1c8c38c5 1230 I915_WRITE(GFX_MODE_GEN7,
01fa0302 1231 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1c8c38c5 1232 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
78501eac 1233
c033666a 1234 if (IS_GEN6(dev_priv)) {
3a69ddd6
KG
1235 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1236 * "If this bit is set, STCunit will have LRA as replacement
1237 * policy. [...] This bit must be reset. LRA replacement
1238 * policy is not supported."
1239 */
1240 I915_WRITE(CACHE_MODE_0,
5e13a0c5 1241 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
84f9f938
BW
1242 }
1243
ac657f64 1244 if (IS_GEN(dev_priv, 6, 7))
6b26c86d 1245 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
84f9f938 1246
035ea405
VS
1247 if (INTEL_INFO(dev_priv)->gen >= 6)
1248 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
15b9f80e 1249
0bc40be8 1250 return init_workarounds_ring(engine);
8187a2b7
ZN
1251}
1252
0bc40be8 1253static void render_ring_cleanup(struct intel_engine_cs *engine)
c6df541c 1254{
c033666a 1255 struct drm_i915_private *dev_priv = engine->i915;
3e78998a 1256
19880c4a 1257 i915_vma_unpin_and_release(&dev_priv->semaphore);
c6df541c
CW
1258}
1259
caddfe71 1260static u32 *gen8_rcs_signal(struct drm_i915_gem_request *req, u32 *out)
3e78998a 1261{
ad7bdb2b 1262 struct drm_i915_private *dev_priv = req->i915;
3e78998a 1263 struct intel_engine_cs *waiter;
c3232b18 1264 enum intel_engine_id id;
3e78998a 1265
3b3f1650 1266 for_each_engine(waiter, dev_priv, id) {
ad7bdb2b 1267 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
3e78998a
BW
1268 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1269 continue;
1270
caddfe71
CW
1271 *out++ = GFX_OP_PIPE_CONTROL(6);
1272 *out++ = (PIPE_CONTROL_GLOBAL_GTT_IVB |
1273 PIPE_CONTROL_QW_WRITE |
1274 PIPE_CONTROL_CS_STALL);
1275 *out++ = lower_32_bits(gtt_offset);
1276 *out++ = upper_32_bits(gtt_offset);
1277 *out++ = req->global_seqno;
1278 *out++ = 0;
1279 *out++ = (MI_SEMAPHORE_SIGNAL |
1280 MI_SEMAPHORE_TARGET(waiter->hw_id));
1281 *out++ = 0;
3e78998a
BW
1282 }
1283
caddfe71 1284 return out;
3e78998a
BW
1285}
1286
caddfe71 1287static u32 *gen8_xcs_signal(struct drm_i915_gem_request *req, u32 *out)
3e78998a 1288{
ad7bdb2b 1289 struct drm_i915_private *dev_priv = req->i915;
3e78998a 1290 struct intel_engine_cs *waiter;
c3232b18 1291 enum intel_engine_id id;
3e78998a 1292
3b3f1650 1293 for_each_engine(waiter, dev_priv, id) {
ad7bdb2b 1294 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
3e78998a
BW
1295 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1296 continue;
1297
caddfe71
CW
1298 *out++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1299 *out++ = lower_32_bits(gtt_offset) | MI_FLUSH_DW_USE_GTT;
1300 *out++ = upper_32_bits(gtt_offset);
1301 *out++ = req->global_seqno;
1302 *out++ = (MI_SEMAPHORE_SIGNAL |
1303 MI_SEMAPHORE_TARGET(waiter->hw_id));
1304 *out++ = 0;
3e78998a
BW
1305 }
1306
caddfe71 1307 return out;
3e78998a
BW
1308}
1309
caddfe71 1310static u32 *gen6_signal(struct drm_i915_gem_request *req, u32 *out)
1ec14ad3 1311{
ad7bdb2b 1312 struct drm_i915_private *dev_priv = req->i915;
318f89ca 1313 struct intel_engine_cs *engine;
3b3f1650 1314 enum intel_engine_id id;
caddfe71 1315 int num_rings = 0;
024a43e1 1316
3b3f1650 1317 for_each_engine(engine, dev_priv, id) {
318f89ca
TU
1318 i915_reg_t mbox_reg;
1319
1320 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
1321 continue;
f0f59a00 1322
318f89ca 1323 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
f0f59a00 1324 if (i915_mmio_reg_valid(mbox_reg)) {
caddfe71
CW
1325 *out++ = MI_LOAD_REGISTER_IMM(1);
1326 *out++ = i915_mmio_reg_offset(mbox_reg);
1327 *out++ = req->global_seqno;
1328 num_rings++;
78325f2d
BW
1329 }
1330 }
caddfe71
CW
1331 if (num_rings & 1)
1332 *out++ = MI_NOOP;
024a43e1 1333
caddfe71 1334 return out;
1ec14ad3
CW
1335}
1336
b0411e7d
CW
1337static void i9xx_submit_request(struct drm_i915_gem_request *request)
1338{
1339 struct drm_i915_private *dev_priv = request->i915;
1340
d55ac5bf
CW
1341 i915_gem_request_submit(request);
1342
caddfe71 1343 I915_WRITE_TAIL(request->engine, request->tail);
b0411e7d
CW
1344}
1345
caddfe71
CW
1346static void i9xx_emit_breadcrumb(struct drm_i915_gem_request *req,
1347 u32 *out)
1ec14ad3 1348{
caddfe71
CW
1349 *out++ = MI_STORE_DWORD_INDEX;
1350 *out++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
1351 *out++ = req->global_seqno;
1352 *out++ = MI_USER_INTERRUPT;
1ec14ad3 1353
caddfe71 1354 req->tail = intel_ring_offset(req->ring, out);
1ec14ad3
CW
1355}
1356
98f29e8d
CW
1357static const int i9xx_emit_breadcrumb_sz = 4;
1358
b0411e7d 1359/**
9b81d556 1360 * gen6_sema_emit_breadcrumb - Update the semaphore mailbox registers
b0411e7d
CW
1361 *
1362 * @request - request to write to the ring
1363 *
1364 * Update the mailbox registers in the *other* rings with the current seqno.
1365 * This acts like a signal in the canonical semaphore.
1366 */
caddfe71
CW
1367static void gen6_sema_emit_breadcrumb(struct drm_i915_gem_request *req,
1368 u32 *out)
b0411e7d 1369{
caddfe71
CW
1370 return i9xx_emit_breadcrumb(req,
1371 req->engine->semaphore.signal(req, out));
b0411e7d
CW
1372}
1373
caddfe71
CW
1374static void gen8_render_emit_breadcrumb(struct drm_i915_gem_request *req,
1375 u32 *out)
a58c01aa
CW
1376{
1377 struct intel_engine_cs *engine = req->engine;
9242f974 1378
caddfe71
CW
1379 if (engine->semaphore.signal)
1380 out = engine->semaphore.signal(req, out);
a58c01aa 1381
caddfe71
CW
1382 *out++ = GFX_OP_PIPE_CONTROL(6);
1383 *out++ = (PIPE_CONTROL_GLOBAL_GTT_IVB |
b5321f30 1384 PIPE_CONTROL_CS_STALL |
caddfe71
CW
1385 PIPE_CONTROL_QW_WRITE);
1386 *out++ = intel_hws_seqno_address(engine);
1387 *out++ = 0;
1388 *out++ = req->global_seqno;
a58c01aa 1389 /* We're thrashing one dword of HWS. */
caddfe71
CW
1390 *out++ = 0;
1391 *out++ = MI_USER_INTERRUPT;
1392 *out++ = MI_NOOP;
a58c01aa 1393
caddfe71 1394 req->tail = intel_ring_offset(req->ring, out);
a58c01aa
CW
1395}
1396
98f29e8d
CW
1397static const int gen8_render_emit_breadcrumb_sz = 8;
1398
c8c99b0f
BW
1399/**
1400 * intel_ring_sync - sync the waiter to the signaller on seqno
1401 *
1402 * @waiter - ring that is waiting
1403 * @signaller - ring which has, or will signal
1404 * @seqno - seqno which the waiter will block on
1405 */
5ee426ca
BW
1406
1407static int
ad7bdb2b
CW
1408gen8_ring_sync_to(struct drm_i915_gem_request *req,
1409 struct drm_i915_gem_request *signal)
5ee426ca 1410{
ad7bdb2b
CW
1411 struct intel_ring *ring = req->ring;
1412 struct drm_i915_private *dev_priv = req->i915;
1413 u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
6ef48d7f 1414 struct i915_hw_ppgtt *ppgtt;
5ee426ca
BW
1415 int ret;
1416
ad7bdb2b 1417 ret = intel_ring_begin(req, 4);
5ee426ca
BW
1418 if (ret)
1419 return ret;
1420
ad7bdb2b
CW
1421 intel_ring_emit(ring,
1422 MI_SEMAPHORE_WAIT |
1423 MI_SEMAPHORE_GLOBAL_GTT |
1424 MI_SEMAPHORE_SAD_GTE_SDD);
65e4760e 1425 intel_ring_emit(ring, signal->global_seqno);
ad7bdb2b
CW
1426 intel_ring_emit(ring, lower_32_bits(offset));
1427 intel_ring_emit(ring, upper_32_bits(offset));
1428 intel_ring_advance(ring);
6ef48d7f
CW
1429
1430 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1431 * pagetables and we must reload them before executing the batch.
1432 * We do this on the i915_switch_context() following the wait and
1433 * before the dispatch.
1434 */
ad7bdb2b
CW
1435 ppgtt = req->ctx->ppgtt;
1436 if (ppgtt && req->engine->id != RCS)
1437 ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
5ee426ca
BW
1438 return 0;
1439}
1440
c8c99b0f 1441static int
ad7bdb2b
CW
1442gen6_ring_sync_to(struct drm_i915_gem_request *req,
1443 struct drm_i915_gem_request *signal)
1ec14ad3 1444{
ad7bdb2b 1445 struct intel_ring *ring = req->ring;
c8c99b0f
BW
1446 u32 dw1 = MI_SEMAPHORE_MBOX |
1447 MI_SEMAPHORE_COMPARE |
1448 MI_SEMAPHORE_REGISTER;
318f89ca 1449 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
ebc348b2 1450 int ret;
1ec14ad3 1451
ebc348b2 1452 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
686cb5f9 1453
ad7bdb2b 1454 ret = intel_ring_begin(req, 4);
1ec14ad3
CW
1455 if (ret)
1456 return ret;
1457
ad7bdb2b 1458 intel_ring_emit(ring, dw1 | wait_mbox);
ddf07be7
CW
1459 /* Throughout all of the GEM code, seqno passed implies our current
1460 * seqno is >= the last seqno executed. However for hardware the
1461 * comparison is strictly greater than.
1462 */
65e4760e 1463 intel_ring_emit(ring, signal->global_seqno - 1);
ad7bdb2b
CW
1464 intel_ring_emit(ring, 0);
1465 intel_ring_emit(ring, MI_NOOP);
1466 intel_ring_advance(ring);
1ec14ad3
CW
1467
1468 return 0;
1469}
1470
f8973c21 1471static void
38a0f2db 1472gen5_seqno_barrier(struct intel_engine_cs *engine)
c6df541c 1473{
f8973c21
CW
1474 /* MI_STORE are internally buffered by the GPU and not flushed
1475 * either by MI_FLUSH or SyncFlush or any other combination of
1476 * MI commands.
c6df541c 1477 *
f8973c21
CW
1478 * "Only the submission of the store operation is guaranteed.
1479 * The write result will be complete (coherent) some time later
1480 * (this is practically a finite period but there is no guaranteed
1481 * latency)."
1482 *
1483 * Empirically, we observe that we need a delay of at least 75us to
1484 * be sure that the seqno write is visible by the CPU.
c6df541c 1485 */
f8973c21 1486 usleep_range(125, 250);
c6df541c
CW
1487}
1488
c04e0f3b
CW
1489static void
1490gen6_seqno_barrier(struct intel_engine_cs *engine)
4cd53c0c 1491{
c033666a 1492 struct drm_i915_private *dev_priv = engine->i915;
bcbdb6d0 1493
4cd53c0c
DV
1494 /* Workaround to force correct ordering between irq and seqno writes on
1495 * ivb (and maybe also on snb) by reading from a CS register (like
9b9ed309
CW
1496 * ACTHD) before reading the status page.
1497 *
1498 * Note that this effectively stalls the read by the time it takes to
1499 * do a memory transaction, which more or less ensures that the write
1500 * from the GPU has sufficient time to invalidate the CPU cacheline.
1501 * Alternatively we could delay the interrupt from the CS ring to give
1502 * the write time to land, but that would incur a delay after every
1503 * batch i.e. much more frequent than a delay when waiting for the
1504 * interrupt (with the same net latency).
bcbdb6d0
CW
1505 *
1506 * Also note that to prevent whole machine hangs on gen7, we have to
1507 * take the spinlock to guard against concurrent cacheline access.
9b9ed309 1508 */
bcbdb6d0 1509 spin_lock_irq(&dev_priv->uncore.lock);
c04e0f3b 1510 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
bcbdb6d0 1511 spin_unlock_irq(&dev_priv->uncore.lock);
4cd53c0c
DV
1512}
1513
31bb59cc
CW
1514static void
1515gen5_irq_enable(struct intel_engine_cs *engine)
e48d8634 1516{
31bb59cc 1517 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
DV
1518}
1519
1520static void
31bb59cc 1521gen5_irq_disable(struct intel_engine_cs *engine)
e48d8634 1522{
31bb59cc 1523 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
DV
1524}
1525
31bb59cc
CW
1526static void
1527i9xx_irq_enable(struct intel_engine_cs *engine)
62fdfeaf 1528{
c033666a 1529 struct drm_i915_private *dev_priv = engine->i915;
b13c2b96 1530
31bb59cc
CW
1531 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1532 I915_WRITE(IMR, dev_priv->irq_mask);
1533 POSTING_READ_FW(RING_IMR(engine->mmio_base));
62fdfeaf
EA
1534}
1535
8187a2b7 1536static void
31bb59cc 1537i9xx_irq_disable(struct intel_engine_cs *engine)
62fdfeaf 1538{
c033666a 1539 struct drm_i915_private *dev_priv = engine->i915;
62fdfeaf 1540
31bb59cc
CW
1541 dev_priv->irq_mask |= engine->irq_enable_mask;
1542 I915_WRITE(IMR, dev_priv->irq_mask);
62fdfeaf
EA
1543}
1544
31bb59cc
CW
1545static void
1546i8xx_irq_enable(struct intel_engine_cs *engine)
c2798b19 1547{
c033666a 1548 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 1549
31bb59cc
CW
1550 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1551 I915_WRITE16(IMR, dev_priv->irq_mask);
1552 POSTING_READ16(RING_IMR(engine->mmio_base));
c2798b19
CW
1553}
1554
1555static void
31bb59cc 1556i8xx_irq_disable(struct intel_engine_cs *engine)
c2798b19 1557{
c033666a 1558 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 1559
31bb59cc
CW
1560 dev_priv->irq_mask |= engine->irq_enable_mask;
1561 I915_WRITE16(IMR, dev_priv->irq_mask);
c2798b19
CW
1562}
1563
b72f3acb 1564static int
7c9cf4e3 1565bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
d1b851fc 1566{
7e37f889 1567 struct intel_ring *ring = req->ring;
b72f3acb
CW
1568 int ret;
1569
5fb9de1a 1570 ret = intel_ring_begin(req, 2);
b72f3acb
CW
1571 if (ret)
1572 return ret;
1573
b5321f30
CW
1574 intel_ring_emit(ring, MI_FLUSH);
1575 intel_ring_emit(ring, MI_NOOP);
1576 intel_ring_advance(ring);
b72f3acb 1577 return 0;
d1b851fc
ZN
1578}
1579
31bb59cc
CW
1580static void
1581gen6_irq_enable(struct intel_engine_cs *engine)
0f46832f 1582{
c033666a 1583 struct drm_i915_private *dev_priv = engine->i915;
0f46832f 1584
61ff75ac
CW
1585 I915_WRITE_IMR(engine,
1586 ~(engine->irq_enable_mask |
1587 engine->irq_keep_mask));
31bb59cc 1588 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
0f46832f
CW
1589}
1590
1591static void
31bb59cc 1592gen6_irq_disable(struct intel_engine_cs *engine)
0f46832f 1593{
c033666a 1594 struct drm_i915_private *dev_priv = engine->i915;
0f46832f 1595
61ff75ac 1596 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
31bb59cc 1597 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
d1b851fc
ZN
1598}
1599
31bb59cc
CW
1600static void
1601hsw_vebox_irq_enable(struct intel_engine_cs *engine)
a19d2933 1602{
c033666a 1603 struct drm_i915_private *dev_priv = engine->i915;
a19d2933 1604
31bb59cc 1605 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
f4e9af4f 1606 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
a19d2933
BW
1607}
1608
1609static void
31bb59cc 1610hsw_vebox_irq_disable(struct intel_engine_cs *engine)
a19d2933 1611{
c033666a 1612 struct drm_i915_private *dev_priv = engine->i915;
a19d2933 1613
31bb59cc 1614 I915_WRITE_IMR(engine, ~0);
f4e9af4f 1615 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
a19d2933
BW
1616}
1617
31bb59cc
CW
1618static void
1619gen8_irq_enable(struct intel_engine_cs *engine)
abd58f01 1620{
c033666a 1621 struct drm_i915_private *dev_priv = engine->i915;
abd58f01 1622
61ff75ac
CW
1623 I915_WRITE_IMR(engine,
1624 ~(engine->irq_enable_mask |
1625 engine->irq_keep_mask));
31bb59cc 1626 POSTING_READ_FW(RING_IMR(engine->mmio_base));
abd58f01
BW
1627}
1628
1629static void
31bb59cc 1630gen8_irq_disable(struct intel_engine_cs *engine)
abd58f01 1631{
c033666a 1632 struct drm_i915_private *dev_priv = engine->i915;
abd58f01 1633
61ff75ac 1634 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
abd58f01
BW
1635}
1636
d1b851fc 1637static int
803688ba
CW
1638i965_emit_bb_start(struct drm_i915_gem_request *req,
1639 u64 offset, u32 length,
1640 unsigned int dispatch_flags)
d1b851fc 1641{
7e37f889 1642 struct intel_ring *ring = req->ring;
e1f99ce6 1643 int ret;
78501eac 1644
5fb9de1a 1645 ret = intel_ring_begin(req, 2);
e1f99ce6
CW
1646 if (ret)
1647 return ret;
1648
b5321f30 1649 intel_ring_emit(ring,
65f56876
CW
1650 MI_BATCH_BUFFER_START |
1651 MI_BATCH_GTT |
8e004efc
JH
1652 (dispatch_flags & I915_DISPATCH_SECURE ?
1653 0 : MI_BATCH_NON_SECURE_I965));
b5321f30
CW
1654 intel_ring_emit(ring, offset);
1655 intel_ring_advance(ring);
78501eac 1656
d1b851fc
ZN
1657 return 0;
1658}
1659
b45305fc
DV
1660/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1661#define I830_BATCH_LIMIT (256*1024)
c4d69da1
CW
1662#define I830_TLB_ENTRIES (2)
1663#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
8187a2b7 1664static int
803688ba
CW
1665i830_emit_bb_start(struct drm_i915_gem_request *req,
1666 u64 offset, u32 len,
1667 unsigned int dispatch_flags)
62fdfeaf 1668{
7e37f889 1669 struct intel_ring *ring = req->ring;
bde13ebd 1670 u32 cs_offset = i915_ggtt_offset(req->engine->scratch);
c4e7a414 1671 int ret;
62fdfeaf 1672
5fb9de1a 1673 ret = intel_ring_begin(req, 6);
c4d69da1
CW
1674 if (ret)
1675 return ret;
62fdfeaf 1676
c4d69da1 1677 /* Evict the invalid PTE TLBs */
b5321f30
CW
1678 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1679 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1680 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1681 intel_ring_emit(ring, cs_offset);
1682 intel_ring_emit(ring, 0xdeadbeef);
1683 intel_ring_emit(ring, MI_NOOP);
1684 intel_ring_advance(ring);
b45305fc 1685
8e004efc 1686 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
b45305fc
DV
1687 if (len > I830_BATCH_LIMIT)
1688 return -ENOSPC;
1689
5fb9de1a 1690 ret = intel_ring_begin(req, 6 + 2);
b45305fc
DV
1691 if (ret)
1692 return ret;
c4d69da1
CW
1693
1694 /* Blit the batch (which has now all relocs applied) to the
1695 * stable batch scratch bo area (so that the CS never
1696 * stumbles over its tlb invalidation bug) ...
1697 */
b5321f30
CW
1698 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1699 intel_ring_emit(ring,
e2f80391 1700 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
b5321f30
CW
1701 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1702 intel_ring_emit(ring, cs_offset);
1703 intel_ring_emit(ring, 4096);
1704 intel_ring_emit(ring, offset);
e2f80391 1705
b5321f30
CW
1706 intel_ring_emit(ring, MI_FLUSH);
1707 intel_ring_emit(ring, MI_NOOP);
1708 intel_ring_advance(ring);
b45305fc
DV
1709
1710 /* ... and execute it. */
c4d69da1 1711 offset = cs_offset;
b45305fc 1712 }
e1f99ce6 1713
9d611c03 1714 ret = intel_ring_begin(req, 2);
c4d69da1
CW
1715 if (ret)
1716 return ret;
1717
b5321f30
CW
1718 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1719 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1720 0 : MI_BATCH_NON_SECURE));
1721 intel_ring_advance(ring);
c4d69da1 1722
fb3256da
DV
1723 return 0;
1724}
1725
1726static int
803688ba
CW
1727i915_emit_bb_start(struct drm_i915_gem_request *req,
1728 u64 offset, u32 len,
1729 unsigned int dispatch_flags)
fb3256da 1730{
7e37f889 1731 struct intel_ring *ring = req->ring;
fb3256da
DV
1732 int ret;
1733
5fb9de1a 1734 ret = intel_ring_begin(req, 2);
fb3256da
DV
1735 if (ret)
1736 return ret;
1737
b5321f30
CW
1738 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1739 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1740 0 : MI_BATCH_NON_SECURE));
1741 intel_ring_advance(ring);
62fdfeaf 1742
62fdfeaf
EA
1743 return 0;
1744}
1745
0bc40be8 1746static void cleanup_phys_status_page(struct intel_engine_cs *engine)
7d3fdfff 1747{
c033666a 1748 struct drm_i915_private *dev_priv = engine->i915;
7d3fdfff
VS
1749
1750 if (!dev_priv->status_page_dmah)
1751 return;
1752
91c8a326 1753 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
0bc40be8 1754 engine->status_page.page_addr = NULL;
7d3fdfff
VS
1755}
1756
0bc40be8 1757static void cleanup_status_page(struct intel_engine_cs *engine)
62fdfeaf 1758{
57e88531 1759 struct i915_vma *vma;
f8a7fde4 1760 struct drm_i915_gem_object *obj;
62fdfeaf 1761
57e88531
CW
1762 vma = fetch_and_zero(&engine->status_page.vma);
1763 if (!vma)
62fdfeaf 1764 return;
62fdfeaf 1765
f8a7fde4
CW
1766 obj = vma->obj;
1767
57e88531 1768 i915_vma_unpin(vma);
f8a7fde4
CW
1769 i915_vma_close(vma);
1770
1771 i915_gem_object_unpin_map(obj);
1772 __i915_gem_object_release_unless_active(obj);
62fdfeaf
EA
1773}
1774
0bc40be8 1775static int init_status_page(struct intel_engine_cs *engine)
62fdfeaf 1776{
57e88531
CW
1777 struct drm_i915_gem_object *obj;
1778 struct i915_vma *vma;
1779 unsigned int flags;
920cf419 1780 void *vaddr;
57e88531 1781 int ret;
e4ffd173 1782
f51455d4 1783 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
57e88531
CW
1784 if (IS_ERR(obj)) {
1785 DRM_ERROR("Failed to allocate status page\n");
1786 return PTR_ERR(obj);
1787 }
62fdfeaf 1788
57e88531
CW
1789 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1790 if (ret)
1791 goto err;
e3efda49 1792
a01cb37a 1793 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
57e88531
CW
1794 if (IS_ERR(vma)) {
1795 ret = PTR_ERR(vma);
1796 goto err;
e3efda49 1797 }
62fdfeaf 1798
57e88531
CW
1799 flags = PIN_GLOBAL;
1800 if (!HAS_LLC(engine->i915))
1801 /* On g33, we cannot place HWS above 256MiB, so
1802 * restrict its pinning to the low mappable arena.
1803 * Though this restriction is not documented for
1804 * gen4, gen5, or byt, they also behave similarly
1805 * and hang if the HWS is placed at the top of the
1806 * GTT. To generalise, it appears that all !llc
1807 * platforms have issues with us placing the HWS
1808 * above the mappable region (even though we never
1809 * actualy map it).
1810 */
1811 flags |= PIN_MAPPABLE;
1812 ret = i915_vma_pin(vma, 0, 4096, flags);
1813 if (ret)
1814 goto err;
62fdfeaf 1815
920cf419
CW
1816 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1817 if (IS_ERR(vaddr)) {
1818 ret = PTR_ERR(vaddr);
1819 goto err_unpin;
1820 }
1821
57e88531 1822 engine->status_page.vma = vma;
bde13ebd 1823 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
f51455d4 1824 engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
62fdfeaf 1825
bde13ebd
CW
1826 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1827 engine->name, i915_ggtt_offset(vma));
62fdfeaf 1828 return 0;
57e88531 1829
920cf419
CW
1830err_unpin:
1831 i915_vma_unpin(vma);
57e88531
CW
1832err:
1833 i915_gem_object_put(obj);
1834 return ret;
62fdfeaf
EA
1835}
1836
0bc40be8 1837static int init_phys_status_page(struct intel_engine_cs *engine)
6b8294a4 1838{
c033666a 1839 struct drm_i915_private *dev_priv = engine->i915;
6b8294a4 1840
57e88531
CW
1841 dev_priv->status_page_dmah =
1842 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
1843 if (!dev_priv->status_page_dmah)
1844 return -ENOMEM;
6b8294a4 1845
0bc40be8
TU
1846 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1847 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
6b8294a4
CW
1848
1849 return 0;
1850}
1851
d3ef1af6 1852int intel_ring_pin(struct intel_ring *ring, unsigned int offset_bias)
7ba717cf 1853{
d3ef1af6 1854 unsigned int flags;
9d80841e 1855 enum i915_map_type map;
57e88531 1856 struct i915_vma *vma = ring->vma;
8305216f 1857 void *addr;
7ba717cf
TD
1858 int ret;
1859
57e88531 1860 GEM_BUG_ON(ring->vaddr);
7ba717cf 1861
9d80841e
CW
1862 map = HAS_LLC(ring->engine->i915) ? I915_MAP_WB : I915_MAP_WC;
1863
d3ef1af6
DCS
1864 flags = PIN_GLOBAL;
1865 if (offset_bias)
1866 flags |= PIN_OFFSET_BIAS | offset_bias;
9d80841e 1867 if (vma->obj->stolen)
57e88531 1868 flags |= PIN_MAPPABLE;
def0c5f6 1869
57e88531 1870 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
9d80841e 1871 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
57e88531
CW
1872 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1873 else
1874 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1875 if (unlikely(ret))
def0c5f6 1876 return ret;
57e88531 1877 }
7ba717cf 1878
57e88531
CW
1879 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1880 if (unlikely(ret))
1881 return ret;
def0c5f6 1882
9d80841e 1883 if (i915_vma_is_map_and_fenceable(vma))
57e88531
CW
1884 addr = (void __force *)i915_vma_pin_iomap(vma);
1885 else
9d80841e 1886 addr = i915_gem_object_pin_map(vma->obj, map);
57e88531
CW
1887 if (IS_ERR(addr))
1888 goto err;
7ba717cf 1889
32c04f16 1890 ring->vaddr = addr;
7ba717cf 1891 return 0;
d2cad535 1892
57e88531
CW
1893err:
1894 i915_vma_unpin(vma);
1895 return PTR_ERR(addr);
7ba717cf
TD
1896}
1897
aad29fbb
CW
1898void intel_ring_unpin(struct intel_ring *ring)
1899{
1900 GEM_BUG_ON(!ring->vma);
1901 GEM_BUG_ON(!ring->vaddr);
1902
9d80841e 1903 if (i915_vma_is_map_and_fenceable(ring->vma))
aad29fbb 1904 i915_vma_unpin_iomap(ring->vma);
57e88531
CW
1905 else
1906 i915_gem_object_unpin_map(ring->vma->obj);
aad29fbb
CW
1907 ring->vaddr = NULL;
1908
57e88531 1909 i915_vma_unpin(ring->vma);
2919d291
OM
1910}
1911
57e88531
CW
1912static struct i915_vma *
1913intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
62fdfeaf 1914{
05394f39 1915 struct drm_i915_gem_object *obj;
57e88531 1916 struct i915_vma *vma;
62fdfeaf 1917
187685cb 1918 obj = i915_gem_object_create_stolen(dev_priv, size);
c58b735f 1919 if (!obj)
12d79d78 1920 obj = i915_gem_object_create(dev_priv, size);
57e88531
CW
1921 if (IS_ERR(obj))
1922 return ERR_CAST(obj);
8187a2b7 1923
24f3a8cf
AG
1924 /* mark ring buffers as read-only from GPU side by default */
1925 obj->gt_ro = 1;
1926
a01cb37a 1927 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
57e88531
CW
1928 if (IS_ERR(vma))
1929 goto err;
1930
1931 return vma;
e3efda49 1932
57e88531
CW
1933err:
1934 i915_gem_object_put(obj);
1935 return vma;
e3efda49
CW
1936}
1937
7e37f889
CW
1938struct intel_ring *
1939intel_engine_create_ring(struct intel_engine_cs *engine, int size)
01101fa7 1940{
7e37f889 1941 struct intel_ring *ring;
57e88531 1942 struct i915_vma *vma;
01101fa7 1943
8f942018 1944 GEM_BUG_ON(!is_power_of_2(size));
62ae14b1 1945 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
8f942018 1946
01101fa7 1947 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
57e88531 1948 if (!ring)
01101fa7
CW
1949 return ERR_PTR(-ENOMEM);
1950
4a570db5 1951 ring->engine = engine;
01101fa7 1952
675d9ad7
CW
1953 INIT_LIST_HEAD(&ring->request_list);
1954
01101fa7
CW
1955 ring->size = size;
1956 /* Workaround an erratum on the i830 which causes a hang if
1957 * the TAIL pointer points to within the last 2 cachelines
1958 * of the buffer.
1959 */
1960 ring->effective_size = size;
2a307c2e 1961 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
01101fa7
CW
1962 ring->effective_size -= 2 * CACHELINE_BYTES;
1963
1964 ring->last_retired_head = -1;
1965 intel_ring_update_space(ring);
1966
57e88531
CW
1967 vma = intel_ring_create_vma(engine->i915, size);
1968 if (IS_ERR(vma)) {
01101fa7 1969 kfree(ring);
57e88531 1970 return ERR_CAST(vma);
01101fa7 1971 }
57e88531 1972 ring->vma = vma;
01101fa7
CW
1973
1974 return ring;
1975}
1976
1977void
7e37f889 1978intel_ring_free(struct intel_ring *ring)
01101fa7 1979{
f8a7fde4
CW
1980 struct drm_i915_gem_object *obj = ring->vma->obj;
1981
1982 i915_vma_close(ring->vma);
1983 __i915_gem_object_release_unless_active(obj);
1984
01101fa7
CW
1985 kfree(ring);
1986}
1987
e8a9c58f
CW
1988static int context_pin(struct i915_gem_context *ctx, unsigned int flags)
1989{
1990 struct i915_vma *vma = ctx->engine[RCS].state;
1991 int ret;
1992
1993 /* Clear this page out of any CPU caches for coherent swap-in/out.
1994 * We only want to do this on the first bind so that we do not stall
1995 * on an active context (which by nature is already on the GPU).
1996 */
1997 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1998 ret = i915_gem_object_set_to_gtt_domain(vma->obj, false);
1999 if (ret)
2000 return ret;
2001 }
2002
2003 return i915_vma_pin(vma, 0, ctx->ggtt_alignment, PIN_GLOBAL | flags);
2004}
2005
2006static int intel_ring_context_pin(struct intel_engine_cs *engine,
2007 struct i915_gem_context *ctx)
0cb26a8e
CW
2008{
2009 struct intel_context *ce = &ctx->engine[engine->id];
2010 int ret;
2011
91c8a326 2012 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
0cb26a8e
CW
2013
2014 if (ce->pin_count++)
2015 return 0;
2016
2017 if (ce->state) {
e8a9c58f
CW
2018 unsigned int flags;
2019
2020 flags = 0;
984ff29f 2021 if (i915_gem_context_is_kernel(ctx))
e8a9c58f 2022 flags = PIN_HIGH;
7abc98fa 2023
e8a9c58f
CW
2024 ret = context_pin(ctx, flags);
2025 if (ret)
0cb26a8e 2026 goto error;
0abfe7e2
CW
2027
2028 ce->state->obj->mm.dirty = true;
0cb26a8e
CW
2029 }
2030
c7c3c07d
CW
2031 /* The kernel context is only used as a placeholder for flushing the
2032 * active context. It is never used for submitting user rendering and
2033 * as such never requires the golden render context, and so we can skip
2034 * emitting it when we switch to the kernel context. This is required
2035 * as during eviction we cannot allocate and pin the renderstate in
2036 * order to initialise the context.
2037 */
984ff29f 2038 if (i915_gem_context_is_kernel(ctx))
c7c3c07d
CW
2039 ce->initialised = true;
2040
9a6feaf0 2041 i915_gem_context_get(ctx);
0cb26a8e
CW
2042 return 0;
2043
2044error:
2045 ce->pin_count = 0;
2046 return ret;
2047}
2048
e8a9c58f
CW
2049static void intel_ring_context_unpin(struct intel_engine_cs *engine,
2050 struct i915_gem_context *ctx)
0cb26a8e
CW
2051{
2052 struct intel_context *ce = &ctx->engine[engine->id];
2053
91c8a326 2054 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
e8a9c58f 2055 GEM_BUG_ON(ce->pin_count == 0);
0cb26a8e
CW
2056
2057 if (--ce->pin_count)
2058 return;
2059
2060 if (ce->state)
bf3783e5 2061 i915_vma_unpin(ce->state);
0cb26a8e 2062
9a6feaf0 2063 i915_gem_context_put(ctx);
0cb26a8e
CW
2064}
2065
acd27845 2066static int intel_init_ring_buffer(struct intel_engine_cs *engine)
e3efda49 2067{
acd27845 2068 struct drm_i915_private *dev_priv = engine->i915;
32c04f16 2069 struct intel_ring *ring;
e3efda49
CW
2070 int ret;
2071
0bc40be8 2072 WARN_ON(engine->buffer);
bfc882b4 2073
019bf277
TU
2074 intel_engine_setup_common(engine);
2075
019bf277 2076 ret = intel_engine_init_common(engine);
688e6c72
CW
2077 if (ret)
2078 goto error;
e3efda49 2079
32c04f16
CW
2080 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
2081 if (IS_ERR(ring)) {
2082 ret = PTR_ERR(ring);
b0366a54
DG
2083 goto error;
2084 }
01101fa7 2085
3177659a
CS
2086 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
2087 WARN_ON(engine->id != RCS);
2088 ret = init_phys_status_page(engine);
e3efda49 2089 if (ret)
8ee14975 2090 goto error;
e3efda49 2091 } else {
3177659a 2092 ret = init_status_page(engine);
e3efda49 2093 if (ret)
8ee14975 2094 goto error;
e3efda49
CW
2095 }
2096
d3ef1af6 2097 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
f51455d4 2098 ret = intel_ring_pin(ring, I915_GTT_PAGE_SIZE);
bfc882b4 2099 if (ret) {
57e88531 2100 intel_ring_free(ring);
bfc882b4 2101 goto error;
e3efda49 2102 }
57e88531 2103 engine->buffer = ring;
62fdfeaf 2104
8ee14975 2105 return 0;
351e3db2 2106
8ee14975 2107error:
7e37f889 2108 intel_engine_cleanup(engine);
8ee14975 2109 return ret;
62fdfeaf
EA
2110}
2111
7e37f889 2112void intel_engine_cleanup(struct intel_engine_cs *engine)
62fdfeaf 2113{
6402c330 2114 struct drm_i915_private *dev_priv;
33626e6a 2115
c033666a 2116 dev_priv = engine->i915;
6402c330 2117
0bc40be8 2118 if (engine->buffer) {
21a2c58a
CW
2119 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
2120 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
33626e6a 2121
aad29fbb 2122 intel_ring_unpin(engine->buffer);
7e37f889 2123 intel_ring_free(engine->buffer);
0bc40be8 2124 engine->buffer = NULL;
b0366a54 2125 }
78501eac 2126
0bc40be8
TU
2127 if (engine->cleanup)
2128 engine->cleanup(engine);
8d19215b 2129
3177659a 2130 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
0bc40be8
TU
2131 WARN_ON(engine->id != RCS);
2132 cleanup_phys_status_page(engine);
3177659a
CS
2133 } else {
2134 cleanup_status_page(engine);
7d3fdfff 2135 }
44e895a8 2136
96a945aa 2137 intel_engine_cleanup_common(engine);
0cb26a8e 2138
c033666a 2139 engine->i915 = NULL;
3b3f1650
AG
2140 dev_priv->engine[engine->id] = NULL;
2141 kfree(engine);
62fdfeaf
EA
2142}
2143
821ed7df
CW
2144void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
2145{
2146 struct intel_engine_cs *engine;
3b3f1650 2147 enum intel_engine_id id;
821ed7df 2148
3b3f1650 2149 for_each_engine(engine, dev_priv, id) {
821ed7df
CW
2150 engine->buffer->head = engine->buffer->tail;
2151 engine->buffer->last_retired_head = -1;
2152 }
2153}
2154
f73e7399 2155static int ring_request_alloc(struct drm_i915_gem_request *request)
9d773091 2156{
6310346e
CW
2157 int ret;
2158
e8a9c58f
CW
2159 GEM_BUG_ON(!request->ctx->engine[request->engine->id].pin_count);
2160
6310346e
CW
2161 /* Flush enough space to reduce the likelihood of waiting after
2162 * we start building the request - in which case we will just
2163 * have to repeat work.
2164 */
a0442461 2165 request->reserved_space += LEGACY_REQUEST_SIZE;
6310346e 2166
e8a9c58f 2167 GEM_BUG_ON(!request->engine->buffer);
1dae2dfb 2168 request->ring = request->engine->buffer;
6310346e
CW
2169
2170 ret = intel_ring_begin(request, 0);
2171 if (ret)
2172 return ret;
2173
a0442461 2174 request->reserved_space -= LEGACY_REQUEST_SIZE;
6310346e 2175 return 0;
9d773091
CW
2176}
2177
987046ad
CW
2178static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
2179{
7e37f889 2180 struct intel_ring *ring = req->ring;
987046ad 2181 struct drm_i915_gem_request *target;
e95433c7
CW
2182 long timeout;
2183
2184 lockdep_assert_held(&req->i915->drm.struct_mutex);
987046ad 2185
1dae2dfb
CW
2186 intel_ring_update_space(ring);
2187 if (ring->space >= bytes)
987046ad
CW
2188 return 0;
2189
2190 /*
2191 * Space is reserved in the ringbuffer for finalising the request,
2192 * as that cannot be allowed to fail. During request finalisation,
2193 * reserved_space is set to 0 to stop the overallocation and the
2194 * assumption is that then we never need to wait (which has the
2195 * risk of failing with EINTR).
2196 *
2197 * See also i915_gem_request_alloc() and i915_add_request().
2198 */
0251a963 2199 GEM_BUG_ON(!req->reserved_space);
987046ad 2200
675d9ad7 2201 list_for_each_entry(target, &ring->request_list, ring_link) {
987046ad
CW
2202 unsigned space;
2203
987046ad 2204 /* Would completion of this request free enough space? */
1dae2dfb
CW
2205 space = __intel_ring_space(target->postfix, ring->tail,
2206 ring->size);
987046ad
CW
2207 if (space >= bytes)
2208 break;
79bbcc29 2209 }
29b1b415 2210
675d9ad7 2211 if (WARN_ON(&target->ring_link == &ring->request_list))
987046ad
CW
2212 return -ENOSPC;
2213
e95433c7
CW
2214 timeout = i915_wait_request(target,
2215 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
2216 MAX_SCHEDULE_TIMEOUT);
2217 if (timeout < 0)
2218 return timeout;
7da844c5 2219
7da844c5
CW
2220 i915_gem_request_retire_upto(target);
2221
2222 intel_ring_update_space(ring);
2223 GEM_BUG_ON(ring->space < bytes);
2224 return 0;
29b1b415
JH
2225}
2226
987046ad 2227int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
cbcc80df 2228{
7e37f889 2229 struct intel_ring *ring = req->ring;
1dae2dfb
CW
2230 int remain_actual = ring->size - ring->tail;
2231 int remain_usable = ring->effective_size - ring->tail;
987046ad
CW
2232 int bytes = num_dwords * sizeof(u32);
2233 int total_bytes, wait_bytes;
79bbcc29 2234 bool need_wrap = false;
29b1b415 2235
0251a963 2236 total_bytes = bytes + req->reserved_space;
29b1b415 2237
79bbcc29
JH
2238 if (unlikely(bytes > remain_usable)) {
2239 /*
2240 * Not enough space for the basic request. So need to flush
2241 * out the remainder and then wait for base + reserved.
2242 */
2243 wait_bytes = remain_actual + total_bytes;
2244 need_wrap = true;
987046ad
CW
2245 } else if (unlikely(total_bytes > remain_usable)) {
2246 /*
2247 * The base request will fit but the reserved space
2248 * falls off the end. So we don't need an immediate wrap
2249 * and only need to effectively wait for the reserved
2250 * size space from the start of ringbuffer.
2251 */
0251a963 2252 wait_bytes = remain_actual + req->reserved_space;
79bbcc29 2253 } else {
987046ad
CW
2254 /* No wrapping required, just waiting. */
2255 wait_bytes = total_bytes;
cbcc80df
MK
2256 }
2257
1dae2dfb 2258 if (wait_bytes > ring->space) {
987046ad 2259 int ret = wait_for_space(req, wait_bytes);
cbcc80df
MK
2260 if (unlikely(ret))
2261 return ret;
2262 }
2263
987046ad 2264 if (unlikely(need_wrap)) {
1dae2dfb
CW
2265 GEM_BUG_ON(remain_actual > ring->space);
2266 GEM_BUG_ON(ring->tail + remain_actual > ring->size);
78501eac 2267
987046ad 2268 /* Fill the tail with MI_NOOP */
1dae2dfb
CW
2269 memset(ring->vaddr + ring->tail, 0, remain_actual);
2270 ring->tail = 0;
2271 ring->space -= remain_actual;
987046ad 2272 }
304d695c 2273
1dae2dfb
CW
2274 ring->space -= bytes;
2275 GEM_BUG_ON(ring->space < 0);
304d695c 2276 return 0;
8187a2b7 2277}
78501eac 2278
753b1ad4 2279/* Align the ring tail to a cacheline boundary */
bba09b12 2280int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
753b1ad4 2281{
7e37f889 2282 struct intel_ring *ring = req->ring;
b5321f30
CW
2283 int num_dwords =
2284 (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
753b1ad4
VS
2285 int ret;
2286
2287 if (num_dwords == 0)
2288 return 0;
2289
18393f63 2290 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
5fb9de1a 2291 ret = intel_ring_begin(req, num_dwords);
753b1ad4
VS
2292 if (ret)
2293 return ret;
2294
2295 while (num_dwords--)
b5321f30 2296 intel_ring_emit(ring, MI_NOOP);
753b1ad4 2297
b5321f30 2298 intel_ring_advance(ring);
753b1ad4
VS
2299
2300 return 0;
2301}
2302
c5efa1ad 2303static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
881f47b6 2304{
c5efa1ad 2305 struct drm_i915_private *dev_priv = request->i915;
881f47b6 2306
76f8421f
CW
2307 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2308
881f47b6 2309 /* Every tail move must follow the sequence below */
12f55818
CW
2310
2311 /* Disable notification that the ring is IDLE. The GT
2312 * will then assume that it is busy and bring it out of rc6.
2313 */
76f8421f
CW
2314 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2315 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
12f55818
CW
2316
2317 /* Clear the context id. Here be magic! */
76f8421f 2318 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
0206e353 2319
12f55818 2320 /* Wait for the ring not to be idle, i.e. for it to wake up. */
76f8421f
CW
2321 if (intel_wait_for_register_fw(dev_priv,
2322 GEN6_BSD_SLEEP_PSMI_CONTROL,
2323 GEN6_BSD_SLEEP_INDICATOR,
2324 0,
2325 50))
12f55818 2326 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
0206e353 2327
12f55818 2328 /* Now that the ring is fully powered up, update the tail */
b0411e7d 2329 i9xx_submit_request(request);
12f55818
CW
2330
2331 /* Let the ring send IDLE messages to the GT again,
2332 * and so let it sleep to conserve power when idle.
2333 */
76f8421f
CW
2334 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2335 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2336
2337 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
881f47b6
XH
2338}
2339
7c9cf4e3 2340static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
881f47b6 2341{
7e37f889 2342 struct intel_ring *ring = req->ring;
71a77e07 2343 uint32_t cmd;
b72f3acb
CW
2344 int ret;
2345
5fb9de1a 2346 ret = intel_ring_begin(req, 4);
b72f3acb
CW
2347 if (ret)
2348 return ret;
2349
71a77e07 2350 cmd = MI_FLUSH_DW;
c033666a 2351 if (INTEL_GEN(req->i915) >= 8)
075b3bba 2352 cmd += 1;
f0a1fb10
CW
2353
2354 /* We always require a command barrier so that subsequent
2355 * commands, such as breadcrumb interrupts, are strictly ordered
2356 * wrt the contents of the write cache being flushed to memory
2357 * (and thus being coherent from the CPU).
2358 */
2359 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2360
9a289771
JB
2361 /*
2362 * Bspec vol 1c.5 - video engine command streamer:
2363 * "If ENABLED, all TLBs will be invalidated once the flush
2364 * operation is complete. This bit is only valid when the
2365 * Post-Sync Operation field is a value of 1h or 3h."
2366 */
7c9cf4e3 2367 if (mode & EMIT_INVALIDATE)
f0a1fb10
CW
2368 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2369
b5321f30
CW
2370 intel_ring_emit(ring, cmd);
2371 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
c033666a 2372 if (INTEL_GEN(req->i915) >= 8) {
b5321f30
CW
2373 intel_ring_emit(ring, 0); /* upper addr */
2374 intel_ring_emit(ring, 0); /* value */
075b3bba 2375 } else {
b5321f30
CW
2376 intel_ring_emit(ring, 0);
2377 intel_ring_emit(ring, MI_NOOP);
075b3bba 2378 }
b5321f30 2379 intel_ring_advance(ring);
b72f3acb 2380 return 0;
881f47b6
XH
2381}
2382
1c7a0623 2383static int
803688ba
CW
2384gen8_emit_bb_start(struct drm_i915_gem_request *req,
2385 u64 offset, u32 len,
2386 unsigned int dispatch_flags)
1c7a0623 2387{
7e37f889 2388 struct intel_ring *ring = req->ring;
b5321f30 2389 bool ppgtt = USES_PPGTT(req->i915) &&
8e004efc 2390 !(dispatch_flags & I915_DISPATCH_SECURE);
1c7a0623
BW
2391 int ret;
2392
5fb9de1a 2393 ret = intel_ring_begin(req, 4);
1c7a0623
BW
2394 if (ret)
2395 return ret;
2396
2397 /* FIXME(BDW): Address space and security selectors. */
b5321f30 2398 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
919032ec
AJ
2399 (dispatch_flags & I915_DISPATCH_RS ?
2400 MI_BATCH_RESOURCE_STREAMER : 0));
b5321f30
CW
2401 intel_ring_emit(ring, lower_32_bits(offset));
2402 intel_ring_emit(ring, upper_32_bits(offset));
2403 intel_ring_emit(ring, MI_NOOP);
2404 intel_ring_advance(ring);
1c7a0623
BW
2405
2406 return 0;
2407}
2408
d7d4eedd 2409static int
803688ba
CW
2410hsw_emit_bb_start(struct drm_i915_gem_request *req,
2411 u64 offset, u32 len,
2412 unsigned int dispatch_flags)
d7d4eedd 2413{
7e37f889 2414 struct intel_ring *ring = req->ring;
d7d4eedd
CW
2415 int ret;
2416
5fb9de1a 2417 ret = intel_ring_begin(req, 2);
d7d4eedd
CW
2418 if (ret)
2419 return ret;
2420
b5321f30 2421 intel_ring_emit(ring,
77072258 2422 MI_BATCH_BUFFER_START |
8e004efc 2423 (dispatch_flags & I915_DISPATCH_SECURE ?
919032ec
AJ
2424 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2425 (dispatch_flags & I915_DISPATCH_RS ?
2426 MI_BATCH_RESOURCE_STREAMER : 0));
d7d4eedd 2427 /* bit0-7 is the length on GEN6+ */
b5321f30
CW
2428 intel_ring_emit(ring, offset);
2429 intel_ring_advance(ring);
d7d4eedd
CW
2430
2431 return 0;
2432}
2433
881f47b6 2434static int
803688ba
CW
2435gen6_emit_bb_start(struct drm_i915_gem_request *req,
2436 u64 offset, u32 len,
2437 unsigned int dispatch_flags)
881f47b6 2438{
7e37f889 2439 struct intel_ring *ring = req->ring;
0206e353 2440 int ret;
ab6f8e32 2441
5fb9de1a 2442 ret = intel_ring_begin(req, 2);
0206e353
AJ
2443 if (ret)
2444 return ret;
e1f99ce6 2445
b5321f30 2446 intel_ring_emit(ring,
d7d4eedd 2447 MI_BATCH_BUFFER_START |
8e004efc
JH
2448 (dispatch_flags & I915_DISPATCH_SECURE ?
2449 0 : MI_BATCH_NON_SECURE_I965));
0206e353 2450 /* bit0-7 is the length on GEN6+ */
b5321f30
CW
2451 intel_ring_emit(ring, offset);
2452 intel_ring_advance(ring);
ab6f8e32 2453
0206e353 2454 return 0;
881f47b6
XH
2455}
2456
549f7365
CW
2457/* Blitter support (SandyBridge+) */
2458
7c9cf4e3 2459static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
8d19215b 2460{
7e37f889 2461 struct intel_ring *ring = req->ring;
71a77e07 2462 uint32_t cmd;
b72f3acb
CW
2463 int ret;
2464
5fb9de1a 2465 ret = intel_ring_begin(req, 4);
b72f3acb
CW
2466 if (ret)
2467 return ret;
2468
71a77e07 2469 cmd = MI_FLUSH_DW;
c033666a 2470 if (INTEL_GEN(req->i915) >= 8)
075b3bba 2471 cmd += 1;
f0a1fb10
CW
2472
2473 /* We always require a command barrier so that subsequent
2474 * commands, such as breadcrumb interrupts, are strictly ordered
2475 * wrt the contents of the write cache being flushed to memory
2476 * (and thus being coherent from the CPU).
2477 */
2478 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2479
9a289771
JB
2480 /*
2481 * Bspec vol 1c.3 - blitter engine command streamer:
2482 * "If ENABLED, all TLBs will be invalidated once the flush
2483 * operation is complete. This bit is only valid when the
2484 * Post-Sync Operation field is a value of 1h or 3h."
2485 */
7c9cf4e3 2486 if (mode & EMIT_INVALIDATE)
f0a1fb10 2487 cmd |= MI_INVALIDATE_TLB;
b5321f30
CW
2488 intel_ring_emit(ring, cmd);
2489 intel_ring_emit(ring,
e2f80391 2490 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
c033666a 2491 if (INTEL_GEN(req->i915) >= 8) {
b5321f30
CW
2492 intel_ring_emit(ring, 0); /* upper addr */
2493 intel_ring_emit(ring, 0); /* value */
075b3bba 2494 } else {
b5321f30
CW
2495 intel_ring_emit(ring, 0);
2496 intel_ring_emit(ring, MI_NOOP);
075b3bba 2497 }
b5321f30 2498 intel_ring_advance(ring);
fd3da6c9 2499
b72f3acb 2500 return 0;
8d19215b
ZN
2501}
2502
d9a64610
TU
2503static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2504 struct intel_engine_cs *engine)
2505{
db3d4019 2506 struct drm_i915_gem_object *obj;
1b9e6650 2507 int ret, i;
db3d4019 2508
39df9190 2509 if (!i915.semaphores)
db3d4019
TU
2510 return;
2511
51d545d0
CW
2512 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
2513 struct i915_vma *vma;
2514
f51455d4 2515 obj = i915_gem_object_create(dev_priv, PAGE_SIZE);
51d545d0
CW
2516 if (IS_ERR(obj))
2517 goto err;
db3d4019 2518
a01cb37a 2519 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
51d545d0
CW
2520 if (IS_ERR(vma))
2521 goto err_obj;
2522
2523 ret = i915_gem_object_set_to_gtt_domain(obj, false);
2524 if (ret)
2525 goto err_obj;
2526
2527 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
2528 if (ret)
2529 goto err_obj;
2530
2531 dev_priv->semaphore = vma;
2532 }
d9a64610
TU
2533
2534 if (INTEL_GEN(dev_priv) >= 8) {
bde13ebd 2535 u32 offset = i915_ggtt_offset(dev_priv->semaphore);
1b9e6650 2536
ad7bdb2b 2537 engine->semaphore.sync_to = gen8_ring_sync_to;
d9a64610 2538 engine->semaphore.signal = gen8_xcs_signal;
1b9e6650
TU
2539
2540 for (i = 0; i < I915_NUM_ENGINES; i++) {
bde13ebd 2541 u32 ring_offset;
1b9e6650
TU
2542
2543 if (i != engine->id)
2544 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2545 else
2546 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2547
2548 engine->semaphore.signal_ggtt[i] = ring_offset;
2549 }
d9a64610 2550 } else if (INTEL_GEN(dev_priv) >= 6) {
ad7bdb2b 2551 engine->semaphore.sync_to = gen6_ring_sync_to;
d9a64610 2552 engine->semaphore.signal = gen6_signal;
4b8e38a9
TU
2553
2554 /*
2555 * The current semaphore is only applied on pre-gen8
2556 * platform. And there is no VCS2 ring on the pre-gen8
2557 * platform. So the semaphore between RCS and VCS2 is
2558 * initialized as INVALID. Gen8 will initialize the
2559 * sema between VCS2 and RCS later.
2560 */
318f89ca 2561 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
4b8e38a9
TU
2562 static const struct {
2563 u32 wait_mbox;
2564 i915_reg_t mbox_reg;
318f89ca
TU
2565 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
2566 [RCS_HW] = {
2567 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2568 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2569 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
4b8e38a9 2570 },
318f89ca
TU
2571 [VCS_HW] = {
2572 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2573 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2574 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
4b8e38a9 2575 },
318f89ca
TU
2576 [BCS_HW] = {
2577 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2578 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2579 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
4b8e38a9 2580 },
318f89ca
TU
2581 [VECS_HW] = {
2582 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2583 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2584 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
4b8e38a9
TU
2585 },
2586 };
2587 u32 wait_mbox;
2588 i915_reg_t mbox_reg;
2589
318f89ca 2590 if (i == engine->hw_id) {
4b8e38a9
TU
2591 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2592 mbox_reg = GEN6_NOSYNC;
2593 } else {
318f89ca
TU
2594 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
2595 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
4b8e38a9
TU
2596 }
2597
2598 engine->semaphore.mbox.wait[i] = wait_mbox;
2599 engine->semaphore.mbox.signal[i] = mbox_reg;
2600 }
d9a64610 2601 }
51d545d0
CW
2602
2603 return;
2604
2605err_obj:
2606 i915_gem_object_put(obj);
2607err:
2608 DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
2609 i915.semaphores = 0;
d9a64610
TU
2610}
2611
ed003078
CW
2612static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2613 struct intel_engine_cs *engine)
2614{
c78d6061
TU
2615 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2616
ed003078 2617 if (INTEL_GEN(dev_priv) >= 8) {
31bb59cc
CW
2618 engine->irq_enable = gen8_irq_enable;
2619 engine->irq_disable = gen8_irq_disable;
ed003078
CW
2620 engine->irq_seqno_barrier = gen6_seqno_barrier;
2621 } else if (INTEL_GEN(dev_priv) >= 6) {
31bb59cc
CW
2622 engine->irq_enable = gen6_irq_enable;
2623 engine->irq_disable = gen6_irq_disable;
ed003078
CW
2624 engine->irq_seqno_barrier = gen6_seqno_barrier;
2625 } else if (INTEL_GEN(dev_priv) >= 5) {
31bb59cc
CW
2626 engine->irq_enable = gen5_irq_enable;
2627 engine->irq_disable = gen5_irq_disable;
f8973c21 2628 engine->irq_seqno_barrier = gen5_seqno_barrier;
ed003078 2629 } else if (INTEL_GEN(dev_priv) >= 3) {
31bb59cc
CW
2630 engine->irq_enable = i9xx_irq_enable;
2631 engine->irq_disable = i9xx_irq_disable;
ed003078 2632 } else {
31bb59cc
CW
2633 engine->irq_enable = i8xx_irq_enable;
2634 engine->irq_disable = i8xx_irq_disable;
ed003078
CW
2635 }
2636}
2637
06a2fe22
TU
2638static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2639 struct intel_engine_cs *engine)
2640{
618e4ca7
CW
2641 intel_ring_init_irq(dev_priv, engine);
2642 intel_ring_init_semaphores(dev_priv, engine);
2643
1d8a1337 2644 engine->init_hw = init_ring_common;
821ed7df 2645 engine->reset_hw = reset_ring_common;
7445a2a4 2646
e8a9c58f
CW
2647 engine->context_pin = intel_ring_context_pin;
2648 engine->context_unpin = intel_ring_context_unpin;
2649
f73e7399
CW
2650 engine->request_alloc = ring_request_alloc;
2651
9b81d556 2652 engine->emit_breadcrumb = i9xx_emit_breadcrumb;
98f29e8d
CW
2653 engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
2654 if (i915.semaphores) {
2655 int num_rings;
2656
9b81d556 2657 engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
98f29e8d
CW
2658
2659 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
2660 if (INTEL_GEN(dev_priv) >= 8) {
2661 engine->emit_breadcrumb_sz += num_rings * 6;
2662 } else {
2663 engine->emit_breadcrumb_sz += num_rings * 3;
2664 if (num_rings & 1)
2665 engine->emit_breadcrumb_sz++;
2666 }
2667 }
ddd66c51 2668 engine->submit_request = i9xx_submit_request;
6f7bef75
CW
2669
2670 if (INTEL_GEN(dev_priv) >= 8)
803688ba 2671 engine->emit_bb_start = gen8_emit_bb_start;
6f7bef75 2672 else if (INTEL_GEN(dev_priv) >= 6)
803688ba 2673 engine->emit_bb_start = gen6_emit_bb_start;
6f7bef75 2674 else if (INTEL_GEN(dev_priv) >= 4)
803688ba 2675 engine->emit_bb_start = i965_emit_bb_start;
2a307c2e 2676 else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
803688ba 2677 engine->emit_bb_start = i830_emit_bb_start;
6f7bef75 2678 else
803688ba 2679 engine->emit_bb_start = i915_emit_bb_start;
06a2fe22
TU
2680}
2681
8b3e2d36 2682int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
5c1143bb 2683{
8b3e2d36 2684 struct drm_i915_private *dev_priv = engine->i915;
3e78998a 2685 int ret;
5c1143bb 2686
06a2fe22
TU
2687 intel_ring_default_vfuncs(dev_priv, engine);
2688
61ff75ac
CW
2689 if (HAS_L3_DPF(dev_priv))
2690 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
f8973c21 2691
c033666a 2692 if (INTEL_GEN(dev_priv) >= 8) {
e2f80391 2693 engine->init_context = intel_rcs_ctx_init;
9b81d556 2694 engine->emit_breadcrumb = gen8_render_emit_breadcrumb;
98f29e8d 2695 engine->emit_breadcrumb_sz = gen8_render_emit_breadcrumb_sz;
c7fe7d25 2696 engine->emit_flush = gen8_render_ring_flush;
98f29e8d
CW
2697 if (i915.semaphores) {
2698 int num_rings;
2699
e2f80391 2700 engine->semaphore.signal = gen8_rcs_signal;
98f29e8d
CW
2701
2702 num_rings =
2703 hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
2704 engine->emit_breadcrumb_sz += num_rings * 6;
2705 }
c033666a 2706 } else if (INTEL_GEN(dev_priv) >= 6) {
e2f80391 2707 engine->init_context = intel_rcs_ctx_init;
c7fe7d25 2708 engine->emit_flush = gen7_render_ring_flush;
c033666a 2709 if (IS_GEN6(dev_priv))
c7fe7d25 2710 engine->emit_flush = gen6_render_ring_flush;
c033666a 2711 } else if (IS_GEN5(dev_priv)) {
c7fe7d25 2712 engine->emit_flush = gen4_render_ring_flush;
59465b5f 2713 } else {
c033666a 2714 if (INTEL_GEN(dev_priv) < 4)
c7fe7d25 2715 engine->emit_flush = gen2_render_ring_flush;
46f0f8d1 2716 else
c7fe7d25 2717 engine->emit_flush = gen4_render_ring_flush;
e2f80391 2718 engine->irq_enable_mask = I915_USER_INTERRUPT;
1ec14ad3 2719 }
707d9cf9 2720
c033666a 2721 if (IS_HASWELL(dev_priv))
803688ba 2722 engine->emit_bb_start = hsw_emit_bb_start;
6f7bef75 2723
e2f80391
TU
2724 engine->init_hw = init_render_ring;
2725 engine->cleanup = render_ring_cleanup;
59465b5f 2726
acd27845 2727 ret = intel_init_ring_buffer(engine);
99be1dfe
DV
2728 if (ret)
2729 return ret;
2730
f8973c21 2731 if (INTEL_GEN(dev_priv) >= 6) {
f51455d4 2732 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
7d5ea807
CW
2733 if (ret)
2734 return ret;
2735 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
56c0f1a7 2736 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
99be1dfe
DV
2737 if (ret)
2738 return ret;
2739 }
2740
2741 return 0;
5c1143bb
XH
2742}
2743
8b3e2d36 2744int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
5c1143bb 2745{
8b3e2d36 2746 struct drm_i915_private *dev_priv = engine->i915;
58fa3835 2747
06a2fe22
TU
2748 intel_ring_default_vfuncs(dev_priv, engine);
2749
c033666a 2750 if (INTEL_GEN(dev_priv) >= 6) {
0fd2c201 2751 /* gen6 bsd needs a special wa for tail updates */
c033666a 2752 if (IS_GEN6(dev_priv))
c5efa1ad 2753 engine->submit_request = gen6_bsd_submit_request;
c7fe7d25 2754 engine->emit_flush = gen6_bsd_ring_flush;
c78d6061 2755 if (INTEL_GEN(dev_priv) < 8)
e2f80391 2756 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
58fa3835 2757 } else {
e2f80391 2758 engine->mmio_base = BSD_RING_BASE;
c7fe7d25 2759 engine->emit_flush = bsd_ring_flush;
8d228911 2760 if (IS_GEN5(dev_priv))
e2f80391 2761 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
8d228911 2762 else
e2f80391 2763 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
58fa3835 2764 }
58fa3835 2765
acd27845 2766 return intel_init_ring_buffer(engine);
5c1143bb 2767}
549f7365 2768
845f74a7 2769/**
62659920 2770 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
845f74a7 2771 */
8b3e2d36 2772int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
845f74a7 2773{
8b3e2d36 2774 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2775
2776 intel_ring_default_vfuncs(dev_priv, engine);
2777
c7fe7d25 2778 engine->emit_flush = gen6_bsd_ring_flush;
845f74a7 2779
acd27845 2780 return intel_init_ring_buffer(engine);
845f74a7
ZY
2781}
2782
8b3e2d36 2783int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
549f7365 2784{
8b3e2d36 2785 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2786
2787 intel_ring_default_vfuncs(dev_priv, engine);
2788
c7fe7d25 2789 engine->emit_flush = gen6_ring_flush;
c78d6061 2790 if (INTEL_GEN(dev_priv) < 8)
e2f80391 2791 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
549f7365 2792
acd27845 2793 return intel_init_ring_buffer(engine);
549f7365 2794}
a7b9761d 2795
8b3e2d36 2796int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
9a8a2213 2797{
8b3e2d36 2798 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2799
2800 intel_ring_default_vfuncs(dev_priv, engine);
2801
c7fe7d25 2802 engine->emit_flush = gen6_ring_flush;
abd58f01 2803
c78d6061 2804 if (INTEL_GEN(dev_priv) < 8) {
e2f80391 2805 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
31bb59cc
CW
2806 engine->irq_enable = hsw_vebox_irq_enable;
2807 engine->irq_disable = hsw_vebox_irq_disable;
abd58f01 2808 }
9a8a2213 2809
acd27845 2810 return intel_init_ring_buffer(engine);
9a8a2213 2811}