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