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