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