drm/i915/gen9: Update i915_drpc_info debugfs for coarse pg & forcewake info
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_ringbuffer.c
CommitLineData
62fdfeaf
EA
1/*
2 * Copyright © 2008-2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <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);
f8c417cd 644 i915_gem_object_put(engine->scratch.obj);
0bc40be8 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:
f8c417cd 675 i915_gem_object_put(engine->scratch.obj);
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
4ba9c1f7
MK
1107 /* WaInPlaceDecompressionHang:skl */
1108 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
1109 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1110 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1111
6107497e 1112 /* WaDisableLSQCROPERFforOCL:skl */
0bc40be8 1113 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
6107497e
AS
1114 if (ret)
1115 return ret;
1116
0bc40be8 1117 return skl_tune_iz_hashing(engine);
7225342a
MK
1118}
1119
0bc40be8 1120static int bxt_init_workarounds(struct intel_engine_cs *engine)
cae0437f 1121{
c033666a 1122 struct drm_i915_private *dev_priv = engine->i915;
aa0011a8 1123 int ret;
dfb601e6 1124
0bc40be8 1125 ret = gen9_init_workarounds(engine);
aa0011a8
AS
1126 if (ret)
1127 return ret;
cae0437f 1128
9c4cbf82
MK
1129 /* WaStoreMultiplePTEenable:bxt */
1130 /* This is a requirement according to Hardware specification */
c033666a 1131 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
9c4cbf82
MK
1132 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1133
1134 /* WaSetClckGatingDisableMedia:bxt */
c033666a 1135 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
9c4cbf82
MK
1136 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1137 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1138 }
1139
dfb601e6
NH
1140 /* WaDisableThreadStallDopClockGating:bxt */
1141 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1142 STALL_DOP_GATING_DISABLE);
1143
780f0aeb 1144 /* WaDisablePooledEuLoadBalancingFix:bxt */
1145 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1146 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1147 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1148 }
1149
983b4b9d 1150 /* WaDisableSbeCacheDispatchPortSharing:bxt */
c033666a 1151 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
983b4b9d
NH
1152 WA_SET_BIT_MASKED(
1153 GEN7_HALF_SLICE_CHICKEN1,
1154 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1155 }
1156
2c8580e4
AS
1157 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1158 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1159 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
a786d53a 1160 /* WaDisableLSQCROPERFforOCL:bxt */
c033666a 1161 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
0bc40be8 1162 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
2c8580e4
AS
1163 if (ret)
1164 return ret;
a786d53a 1165
0bc40be8 1166 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
a786d53a
AS
1167 if (ret)
1168 return ret;
2c8580e4
AS
1169 }
1170
050fc465 1171 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
c033666a 1172 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
36579cb6
ID
1173 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1174 L3_HIGH_PRIO_CREDITS(2));
050fc465 1175
ad2bdb44
MK
1176 /* WaInsertDummyPushConstPs:bxt */
1177 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
1178 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1179 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1180
4ba9c1f7
MK
1181 /* WaInPlaceDecompressionHang:bxt */
1182 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
1183 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1184 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1185
cae0437f
NH
1186 return 0;
1187}
1188
e5f81d65
MK
1189static int kbl_init_workarounds(struct intel_engine_cs *engine)
1190{
e587f6cb 1191 struct drm_i915_private *dev_priv = engine->i915;
e5f81d65
MK
1192 int ret;
1193
1194 ret = gen9_init_workarounds(engine);
1195 if (ret)
1196 return ret;
1197
e587f6cb
MK
1198 /* WaEnableGapsTsvCreditFix:kbl */
1199 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1200 GEN9_GAPS_TSV_CREDIT_DISABLE));
1201
c0b730d5
MK
1202 /* WaDisableDynamicCreditSharing:kbl */
1203 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1204 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1205 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1206
8401d42f
MK
1207 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1208 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1209 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1210 HDC_FENCE_DEST_SLM_DISABLE);
1211
fe905819
MK
1212 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1213 * involving this register should also be added to WA batch as required.
1214 */
1215 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1216 /* WaDisableLSQCROPERFforOCL:kbl */
1217 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1218 GEN8_LQSC_RO_PERF_DIS);
1219
ad2bdb44
MK
1220 /* WaInsertDummyPushConstPs:kbl */
1221 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1222 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1223 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1224
4de5d7cc
MK
1225 /* WaDisableGafsUnitClkGating:kbl */
1226 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1227
954337aa
MK
1228 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1229 WA_SET_BIT_MASKED(
1230 GEN7_HALF_SLICE_CHICKEN1,
1231 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1232
4ba9c1f7
MK
1233 /* WaInPlaceDecompressionHang:kbl */
1234 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1235 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1236
fe905819
MK
1237 /* WaDisableLSQCROPERFforOCL:kbl */
1238 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1239 if (ret)
1240 return ret;
1241
e5f81d65
MK
1242 return 0;
1243}
1244
0bc40be8 1245int init_workarounds_ring(struct intel_engine_cs *engine)
7225342a 1246{
c033666a 1247 struct drm_i915_private *dev_priv = engine->i915;
7225342a 1248
0bc40be8 1249 WARN_ON(engine->id != RCS);
7225342a
MK
1250
1251 dev_priv->workarounds.count = 0;
33136b06 1252 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
7225342a 1253
c033666a 1254 if (IS_BROADWELL(dev_priv))
0bc40be8 1255 return bdw_init_workarounds(engine);
7225342a 1256
c033666a 1257 if (IS_CHERRYVIEW(dev_priv))
0bc40be8 1258 return chv_init_workarounds(engine);
00e1e623 1259
c033666a 1260 if (IS_SKYLAKE(dev_priv))
0bc40be8 1261 return skl_init_workarounds(engine);
cae0437f 1262
c033666a 1263 if (IS_BROXTON(dev_priv))
0bc40be8 1264 return bxt_init_workarounds(engine);
3b106531 1265
e5f81d65
MK
1266 if (IS_KABYLAKE(dev_priv))
1267 return kbl_init_workarounds(engine);
1268
00e1e623
VS
1269 return 0;
1270}
1271
0bc40be8 1272static int init_render_ring(struct intel_engine_cs *engine)
8187a2b7 1273{
c033666a 1274 struct drm_i915_private *dev_priv = engine->i915;
0bc40be8 1275 int ret = init_ring_common(engine);
9c33baa6
KZ
1276 if (ret)
1277 return ret;
a69ffdbf 1278
61a563a2 1279 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
ac657f64 1280 if (IS_GEN(dev_priv, 4, 6))
6b26c86d 1281 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1c8c38c5
CW
1282
1283 /* We need to disable the AsyncFlip performance optimisations in order
1284 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1285 * programmed to '1' on all products.
8693a824 1286 *
2441f877 1287 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1c8c38c5 1288 */
ac657f64 1289 if (IS_GEN(dev_priv, 6, 7))
1c8c38c5
CW
1290 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1291
f05bb0c7 1292 /* Required for the hardware to program scanline values for waiting */
01fa0302 1293 /* WaEnableFlushTlbInvalidationMode:snb */
c033666a 1294 if (IS_GEN6(dev_priv))
f05bb0c7 1295 I915_WRITE(GFX_MODE,
aa83e30d 1296 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
f05bb0c7 1297
01fa0302 1298 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
c033666a 1299 if (IS_GEN7(dev_priv))
1c8c38c5 1300 I915_WRITE(GFX_MODE_GEN7,
01fa0302 1301 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1c8c38c5 1302 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
78501eac 1303
c033666a 1304 if (IS_GEN6(dev_priv)) {
3a69ddd6
KG
1305 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1306 * "If this bit is set, STCunit will have LRA as replacement
1307 * policy. [...] This bit must be reset. LRA replacement
1308 * policy is not supported."
1309 */
1310 I915_WRITE(CACHE_MODE_0,
5e13a0c5 1311 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
84f9f938
BW
1312 }
1313
ac657f64 1314 if (IS_GEN(dev_priv, 6, 7))
6b26c86d 1315 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
84f9f938 1316
035ea405
VS
1317 if (INTEL_INFO(dev_priv)->gen >= 6)
1318 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
15b9f80e 1319
0bc40be8 1320 return init_workarounds_ring(engine);
8187a2b7
ZN
1321}
1322
0bc40be8 1323static void render_ring_cleanup(struct intel_engine_cs *engine)
c6df541c 1324{
c033666a 1325 struct drm_i915_private *dev_priv = engine->i915;
3e78998a
BW
1326
1327 if (dev_priv->semaphore_obj) {
1328 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
f8c417cd 1329 i915_gem_object_put(dev_priv->semaphore_obj);
3e78998a
BW
1330 dev_priv->semaphore_obj = NULL;
1331 }
b45305fc 1332
0bc40be8 1333 intel_fini_pipe_control(engine);
c6df541c
CW
1334}
1335
f7169687 1336static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
3e78998a
BW
1337 unsigned int num_dwords)
1338{
1339#define MBOX_UPDATE_DWORDS 8
4a570db5 1340 struct intel_engine_cs *signaller = signaller_req->engine;
c033666a 1341 struct drm_i915_private *dev_priv = signaller_req->i915;
3e78998a 1342 struct intel_engine_cs *waiter;
c3232b18
DG
1343 enum intel_engine_id id;
1344 int ret, num_rings;
3e78998a 1345
c033666a 1346 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
3e78998a
BW
1347 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1348#undef MBOX_UPDATE_DWORDS
1349
5fb9de1a 1350 ret = intel_ring_begin(signaller_req, num_dwords);
3e78998a
BW
1351 if (ret)
1352 return ret;
1353
c3232b18 1354 for_each_engine_id(waiter, dev_priv, id) {
c3232b18 1355 u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
3e78998a
BW
1356 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1357 continue;
1358
1359 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1360 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1361 PIPE_CONTROL_QW_WRITE |
f9a4ea35 1362 PIPE_CONTROL_CS_STALL);
3e78998a
BW
1363 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1364 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
04769652 1365 intel_ring_emit(signaller, signaller_req->fence.seqno);
3e78998a
BW
1366 intel_ring_emit(signaller, 0);
1367 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
215a7e32 1368 MI_SEMAPHORE_TARGET(waiter->hw_id));
3e78998a
BW
1369 intel_ring_emit(signaller, 0);
1370 }
1371
1372 return 0;
1373}
1374
f7169687 1375static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
3e78998a
BW
1376 unsigned int num_dwords)
1377{
1378#define MBOX_UPDATE_DWORDS 6
4a570db5 1379 struct intel_engine_cs *signaller = signaller_req->engine;
c033666a 1380 struct drm_i915_private *dev_priv = signaller_req->i915;
3e78998a 1381 struct intel_engine_cs *waiter;
c3232b18
DG
1382 enum intel_engine_id id;
1383 int ret, num_rings;
3e78998a 1384
c033666a 1385 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
3e78998a
BW
1386 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1387#undef MBOX_UPDATE_DWORDS
1388
5fb9de1a 1389 ret = intel_ring_begin(signaller_req, num_dwords);
3e78998a
BW
1390 if (ret)
1391 return ret;
1392
c3232b18 1393 for_each_engine_id(waiter, dev_priv, id) {
c3232b18 1394 u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
3e78998a
BW
1395 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1396 continue;
1397
1398 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1399 MI_FLUSH_DW_OP_STOREDW);
1400 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1401 MI_FLUSH_DW_USE_GTT);
1402 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
04769652 1403 intel_ring_emit(signaller, signaller_req->fence.seqno);
3e78998a 1404 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
215a7e32 1405 MI_SEMAPHORE_TARGET(waiter->hw_id));
3e78998a
BW
1406 intel_ring_emit(signaller, 0);
1407 }
1408
1409 return 0;
1410}
1411
f7169687 1412static int gen6_signal(struct drm_i915_gem_request *signaller_req,
024a43e1 1413 unsigned int num_dwords)
1ec14ad3 1414{
4a570db5 1415 struct intel_engine_cs *signaller = signaller_req->engine;
c033666a 1416 struct drm_i915_private *dev_priv = signaller_req->i915;
a4872ba6 1417 struct intel_engine_cs *useless;
c3232b18
DG
1418 enum intel_engine_id id;
1419 int ret, num_rings;
78325f2d 1420
a1444b79 1421#define MBOX_UPDATE_DWORDS 3
c033666a 1422 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
a1444b79
BW
1423 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1424#undef MBOX_UPDATE_DWORDS
024a43e1 1425
5fb9de1a 1426 ret = intel_ring_begin(signaller_req, num_dwords);
024a43e1
BW
1427 if (ret)
1428 return ret;
024a43e1 1429
c3232b18
DG
1430 for_each_engine_id(useless, dev_priv, id) {
1431 i915_reg_t mbox_reg = signaller->semaphore.mbox.signal[id];
f0f59a00
VS
1432
1433 if (i915_mmio_reg_valid(mbox_reg)) {
78325f2d 1434 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
f92a9162 1435 intel_ring_emit_reg(signaller, mbox_reg);
04769652 1436 intel_ring_emit(signaller, signaller_req->fence.seqno);
78325f2d
BW
1437 }
1438 }
024a43e1 1439
a1444b79
BW
1440 /* If num_dwords was rounded, make sure the tail pointer is correct */
1441 if (num_rings % 2 == 0)
1442 intel_ring_emit(signaller, MI_NOOP);
1443
024a43e1 1444 return 0;
1ec14ad3
CW
1445}
1446
c8c99b0f
BW
1447/**
1448 * gen6_add_request - Update the semaphore mailbox registers
ee044a88
JH
1449 *
1450 * @request - request to write to the ring
c8c99b0f
BW
1451 *
1452 * Update the mailbox registers in the *other* rings with the current seqno.
1453 * This acts like a signal in the canonical semaphore.
1454 */
1ec14ad3 1455static int
ee044a88 1456gen6_add_request(struct drm_i915_gem_request *req)
1ec14ad3 1457{
4a570db5 1458 struct intel_engine_cs *engine = req->engine;
024a43e1 1459 int ret;
52ed2325 1460
e2f80391
TU
1461 if (engine->semaphore.signal)
1462 ret = engine->semaphore.signal(req, 4);
707d9cf9 1463 else
5fb9de1a 1464 ret = intel_ring_begin(req, 4);
707d9cf9 1465
1ec14ad3
CW
1466 if (ret)
1467 return ret;
1468
e2f80391
TU
1469 intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
1470 intel_ring_emit(engine,
1471 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
04769652 1472 intel_ring_emit(engine, req->fence.seqno);
e2f80391
TU
1473 intel_ring_emit(engine, MI_USER_INTERRUPT);
1474 __intel_ring_advance(engine);
1ec14ad3 1475
1ec14ad3
CW
1476 return 0;
1477}
1478
a58c01aa
CW
1479static int
1480gen8_render_add_request(struct drm_i915_gem_request *req)
1481{
1482 struct intel_engine_cs *engine = req->engine;
1483 int ret;
1484
1485 if (engine->semaphore.signal)
1486 ret = engine->semaphore.signal(req, 8);
1487 else
1488 ret = intel_ring_begin(req, 8);
1489 if (ret)
1490 return ret;
1491
1492 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
1493 intel_ring_emit(engine, (PIPE_CONTROL_GLOBAL_GTT_IVB |
1494 PIPE_CONTROL_CS_STALL |
1495 PIPE_CONTROL_QW_WRITE));
1496 intel_ring_emit(engine, intel_hws_seqno_address(req->engine));
1497 intel_ring_emit(engine, 0);
1498 intel_ring_emit(engine, i915_gem_request_get_seqno(req));
1499 /* We're thrashing one dword of HWS. */
1500 intel_ring_emit(engine, 0);
1501 intel_ring_emit(engine, MI_USER_INTERRUPT);
1502 intel_ring_emit(engine, MI_NOOP);
1503 __intel_ring_advance(engine);
1504
1505 return 0;
1506}
1507
c033666a 1508static inline bool i915_gem_has_seqno_wrapped(struct drm_i915_private *dev_priv,
f72b3435
MK
1509 u32 seqno)
1510{
f72b3435
MK
1511 return dev_priv->last_seqno < seqno;
1512}
1513
c8c99b0f
BW
1514/**
1515 * intel_ring_sync - sync the waiter to the signaller on seqno
1516 *
1517 * @waiter - ring that is waiting
1518 * @signaller - ring which has, or will signal
1519 * @seqno - seqno which the waiter will block on
1520 */
5ee426ca
BW
1521
1522static int
599d924c 1523gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
5ee426ca
BW
1524 struct intel_engine_cs *signaller,
1525 u32 seqno)
1526{
4a570db5 1527 struct intel_engine_cs *waiter = waiter_req->engine;
c033666a 1528 struct drm_i915_private *dev_priv = waiter_req->i915;
c38c651b 1529 u64 offset = GEN8_WAIT_OFFSET(waiter, signaller->id);
6ef48d7f 1530 struct i915_hw_ppgtt *ppgtt;
5ee426ca
BW
1531 int ret;
1532
5fb9de1a 1533 ret = intel_ring_begin(waiter_req, 4);
5ee426ca
BW
1534 if (ret)
1535 return ret;
1536
1537 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1538 MI_SEMAPHORE_GLOBAL_GTT |
1539 MI_SEMAPHORE_SAD_GTE_SDD);
1540 intel_ring_emit(waiter, seqno);
c38c651b
TU
1541 intel_ring_emit(waiter, lower_32_bits(offset));
1542 intel_ring_emit(waiter, upper_32_bits(offset));
5ee426ca 1543 intel_ring_advance(waiter);
6ef48d7f
CW
1544
1545 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1546 * pagetables and we must reload them before executing the batch.
1547 * We do this on the i915_switch_context() following the wait and
1548 * before the dispatch.
1549 */
1550 ppgtt = waiter_req->ctx->ppgtt;
1551 if (ppgtt && waiter_req->engine->id != RCS)
1552 ppgtt->pd_dirty_rings |= intel_engine_flag(waiter_req->engine);
5ee426ca
BW
1553 return 0;
1554}
1555
c8c99b0f 1556static int
599d924c 1557gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
a4872ba6 1558 struct intel_engine_cs *signaller,
686cb5f9 1559 u32 seqno)
1ec14ad3 1560{
4a570db5 1561 struct intel_engine_cs *waiter = waiter_req->engine;
c8c99b0f
BW
1562 u32 dw1 = MI_SEMAPHORE_MBOX |
1563 MI_SEMAPHORE_COMPARE |
1564 MI_SEMAPHORE_REGISTER;
ebc348b2
BW
1565 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1566 int ret;
1ec14ad3 1567
1500f7ea
BW
1568 /* Throughout all of the GEM code, seqno passed implies our current
1569 * seqno is >= the last seqno executed. However for hardware the
1570 * comparison is strictly greater than.
1571 */
1572 seqno -= 1;
1573
ebc348b2 1574 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
686cb5f9 1575
5fb9de1a 1576 ret = intel_ring_begin(waiter_req, 4);
1ec14ad3
CW
1577 if (ret)
1578 return ret;
1579
f72b3435 1580 /* If seqno wrap happened, omit the wait with no-ops */
c033666a 1581 if (likely(!i915_gem_has_seqno_wrapped(waiter_req->i915, seqno))) {
ebc348b2 1582 intel_ring_emit(waiter, dw1 | wait_mbox);
f72b3435
MK
1583 intel_ring_emit(waiter, seqno);
1584 intel_ring_emit(waiter, 0);
1585 intel_ring_emit(waiter, MI_NOOP);
1586 } else {
1587 intel_ring_emit(waiter, MI_NOOP);
1588 intel_ring_emit(waiter, MI_NOOP);
1589 intel_ring_emit(waiter, MI_NOOP);
1590 intel_ring_emit(waiter, MI_NOOP);
1591 }
c8c99b0f 1592 intel_ring_advance(waiter);
1ec14ad3
CW
1593
1594 return 0;
1595}
1596
f8973c21 1597static void
38a0f2db 1598gen5_seqno_barrier(struct intel_engine_cs *engine)
c6df541c 1599{
f8973c21
CW
1600 /* MI_STORE are internally buffered by the GPU and not flushed
1601 * either by MI_FLUSH or SyncFlush or any other combination of
1602 * MI commands.
c6df541c 1603 *
f8973c21
CW
1604 * "Only the submission of the store operation is guaranteed.
1605 * The write result will be complete (coherent) some time later
1606 * (this is practically a finite period but there is no guaranteed
1607 * latency)."
1608 *
1609 * Empirically, we observe that we need a delay of at least 75us to
1610 * be sure that the seqno write is visible by the CPU.
c6df541c 1611 */
f8973c21 1612 usleep_range(125, 250);
c6df541c
CW
1613}
1614
c04e0f3b
CW
1615static void
1616gen6_seqno_barrier(struct intel_engine_cs *engine)
4cd53c0c 1617{
c033666a 1618 struct drm_i915_private *dev_priv = engine->i915;
bcbdb6d0 1619
4cd53c0c
DV
1620 /* Workaround to force correct ordering between irq and seqno writes on
1621 * ivb (and maybe also on snb) by reading from a CS register (like
9b9ed309
CW
1622 * ACTHD) before reading the status page.
1623 *
1624 * Note that this effectively stalls the read by the time it takes to
1625 * do a memory transaction, which more or less ensures that the write
1626 * from the GPU has sufficient time to invalidate the CPU cacheline.
1627 * Alternatively we could delay the interrupt from the CS ring to give
1628 * the write time to land, but that would incur a delay after every
1629 * batch i.e. much more frequent than a delay when waiting for the
1630 * interrupt (with the same net latency).
bcbdb6d0
CW
1631 *
1632 * Also note that to prevent whole machine hangs on gen7, we have to
1633 * take the spinlock to guard against concurrent cacheline access.
9b9ed309 1634 */
bcbdb6d0 1635 spin_lock_irq(&dev_priv->uncore.lock);
c04e0f3b 1636 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
bcbdb6d0 1637 spin_unlock_irq(&dev_priv->uncore.lock);
4cd53c0c
DV
1638}
1639
31bb59cc
CW
1640static void
1641gen5_irq_enable(struct intel_engine_cs *engine)
e48d8634 1642{
31bb59cc 1643 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
DV
1644}
1645
1646static void
31bb59cc 1647gen5_irq_disable(struct intel_engine_cs *engine)
e48d8634 1648{
31bb59cc 1649 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
e48d8634
DV
1650}
1651
31bb59cc
CW
1652static void
1653i9xx_irq_enable(struct intel_engine_cs *engine)
62fdfeaf 1654{
c033666a 1655 struct drm_i915_private *dev_priv = engine->i915;
b13c2b96 1656
31bb59cc
CW
1657 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1658 I915_WRITE(IMR, dev_priv->irq_mask);
1659 POSTING_READ_FW(RING_IMR(engine->mmio_base));
62fdfeaf
EA
1660}
1661
8187a2b7 1662static void
31bb59cc 1663i9xx_irq_disable(struct intel_engine_cs *engine)
62fdfeaf 1664{
c033666a 1665 struct drm_i915_private *dev_priv = engine->i915;
62fdfeaf 1666
31bb59cc
CW
1667 dev_priv->irq_mask |= engine->irq_enable_mask;
1668 I915_WRITE(IMR, dev_priv->irq_mask);
62fdfeaf
EA
1669}
1670
31bb59cc
CW
1671static void
1672i8xx_irq_enable(struct intel_engine_cs *engine)
c2798b19 1673{
c033666a 1674 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 1675
31bb59cc
CW
1676 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1677 I915_WRITE16(IMR, dev_priv->irq_mask);
1678 POSTING_READ16(RING_IMR(engine->mmio_base));
c2798b19
CW
1679}
1680
1681static void
31bb59cc 1682i8xx_irq_disable(struct intel_engine_cs *engine)
c2798b19 1683{
c033666a 1684 struct drm_i915_private *dev_priv = engine->i915;
c2798b19 1685
31bb59cc
CW
1686 dev_priv->irq_mask |= engine->irq_enable_mask;
1687 I915_WRITE16(IMR, dev_priv->irq_mask);
c2798b19
CW
1688}
1689
b72f3acb 1690static int
a84c3ae1 1691bsd_ring_flush(struct drm_i915_gem_request *req,
78501eac
CW
1692 u32 invalidate_domains,
1693 u32 flush_domains)
d1b851fc 1694{
4a570db5 1695 struct intel_engine_cs *engine = req->engine;
b72f3acb
CW
1696 int ret;
1697
5fb9de1a 1698 ret = intel_ring_begin(req, 2);
b72f3acb
CW
1699 if (ret)
1700 return ret;
1701
e2f80391
TU
1702 intel_ring_emit(engine, MI_FLUSH);
1703 intel_ring_emit(engine, MI_NOOP);
1704 intel_ring_advance(engine);
b72f3acb 1705 return 0;
d1b851fc
ZN
1706}
1707
3cce469c 1708static int
ee044a88 1709i9xx_add_request(struct drm_i915_gem_request *req)
d1b851fc 1710{
4a570db5 1711 struct intel_engine_cs *engine = req->engine;
3cce469c
CW
1712 int ret;
1713
5fb9de1a 1714 ret = intel_ring_begin(req, 4);
3cce469c
CW
1715 if (ret)
1716 return ret;
6f392d54 1717
e2f80391
TU
1718 intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
1719 intel_ring_emit(engine,
1720 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
04769652 1721 intel_ring_emit(engine, req->fence.seqno);
e2f80391
TU
1722 intel_ring_emit(engine, MI_USER_INTERRUPT);
1723 __intel_ring_advance(engine);
d1b851fc 1724
3cce469c 1725 return 0;
d1b851fc
ZN
1726}
1727
31bb59cc
CW
1728static void
1729gen6_irq_enable(struct intel_engine_cs *engine)
0f46832f 1730{
c033666a 1731 struct drm_i915_private *dev_priv = engine->i915;
0f46832f 1732
61ff75ac
CW
1733 I915_WRITE_IMR(engine,
1734 ~(engine->irq_enable_mask |
1735 engine->irq_keep_mask));
31bb59cc 1736 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
0f46832f
CW
1737}
1738
1739static void
31bb59cc 1740gen6_irq_disable(struct intel_engine_cs *engine)
0f46832f 1741{
c033666a 1742 struct drm_i915_private *dev_priv = engine->i915;
0f46832f 1743
61ff75ac 1744 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
31bb59cc 1745 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
d1b851fc
ZN
1746}
1747
31bb59cc
CW
1748static void
1749hsw_vebox_irq_enable(struct intel_engine_cs *engine)
a19d2933 1750{
c033666a 1751 struct drm_i915_private *dev_priv = engine->i915;
a19d2933 1752
31bb59cc
CW
1753 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
1754 gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
a19d2933
BW
1755}
1756
1757static void
31bb59cc 1758hsw_vebox_irq_disable(struct intel_engine_cs *engine)
a19d2933 1759{
c033666a 1760 struct drm_i915_private *dev_priv = engine->i915;
a19d2933 1761
31bb59cc
CW
1762 I915_WRITE_IMR(engine, ~0);
1763 gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
a19d2933
BW
1764}
1765
31bb59cc
CW
1766static void
1767gen8_irq_enable(struct intel_engine_cs *engine)
abd58f01 1768{
c033666a 1769 struct drm_i915_private *dev_priv = engine->i915;
abd58f01 1770
61ff75ac
CW
1771 I915_WRITE_IMR(engine,
1772 ~(engine->irq_enable_mask |
1773 engine->irq_keep_mask));
31bb59cc 1774 POSTING_READ_FW(RING_IMR(engine->mmio_base));
abd58f01
BW
1775}
1776
1777static void
31bb59cc 1778gen8_irq_disable(struct intel_engine_cs *engine)
abd58f01 1779{
c033666a 1780 struct drm_i915_private *dev_priv = engine->i915;
abd58f01 1781
61ff75ac 1782 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
abd58f01
BW
1783}
1784
d1b851fc 1785static int
53fddaf7 1786i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
9bcb144c 1787 u64 offset, u32 length,
8e004efc 1788 unsigned dispatch_flags)
d1b851fc 1789{
4a570db5 1790 struct intel_engine_cs *engine = req->engine;
e1f99ce6 1791 int ret;
78501eac 1792
5fb9de1a 1793 ret = intel_ring_begin(req, 2);
e1f99ce6
CW
1794 if (ret)
1795 return ret;
1796
e2f80391 1797 intel_ring_emit(engine,
65f56876
CW
1798 MI_BATCH_BUFFER_START |
1799 MI_BATCH_GTT |
8e004efc
JH
1800 (dispatch_flags & I915_DISPATCH_SECURE ?
1801 0 : MI_BATCH_NON_SECURE_I965));
e2f80391
TU
1802 intel_ring_emit(engine, offset);
1803 intel_ring_advance(engine);
78501eac 1804
d1b851fc
ZN
1805 return 0;
1806}
1807
b45305fc
DV
1808/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1809#define I830_BATCH_LIMIT (256*1024)
c4d69da1
CW
1810#define I830_TLB_ENTRIES (2)
1811#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
8187a2b7 1812static int
53fddaf7 1813i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
8e004efc
JH
1814 u64 offset, u32 len,
1815 unsigned dispatch_flags)
62fdfeaf 1816{
4a570db5 1817 struct intel_engine_cs *engine = req->engine;
e2f80391 1818 u32 cs_offset = engine->scratch.gtt_offset;
c4e7a414 1819 int ret;
62fdfeaf 1820
5fb9de1a 1821 ret = intel_ring_begin(req, 6);
c4d69da1
CW
1822 if (ret)
1823 return ret;
62fdfeaf 1824
c4d69da1 1825 /* Evict the invalid PTE TLBs */
e2f80391
TU
1826 intel_ring_emit(engine, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1827 intel_ring_emit(engine, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1828 intel_ring_emit(engine, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1829 intel_ring_emit(engine, cs_offset);
1830 intel_ring_emit(engine, 0xdeadbeef);
1831 intel_ring_emit(engine, MI_NOOP);
1832 intel_ring_advance(engine);
b45305fc 1833
8e004efc 1834 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
b45305fc
DV
1835 if (len > I830_BATCH_LIMIT)
1836 return -ENOSPC;
1837
5fb9de1a 1838 ret = intel_ring_begin(req, 6 + 2);
b45305fc
DV
1839 if (ret)
1840 return ret;
c4d69da1
CW
1841
1842 /* Blit the batch (which has now all relocs applied) to the
1843 * stable batch scratch bo area (so that the CS never
1844 * stumbles over its tlb invalidation bug) ...
1845 */
e2f80391
TU
1846 intel_ring_emit(engine, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1847 intel_ring_emit(engine,
1848 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
1849 intel_ring_emit(engine, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1850 intel_ring_emit(engine, cs_offset);
1851 intel_ring_emit(engine, 4096);
1852 intel_ring_emit(engine, offset);
1853
1854 intel_ring_emit(engine, MI_FLUSH);
1855 intel_ring_emit(engine, MI_NOOP);
1856 intel_ring_advance(engine);
b45305fc
DV
1857
1858 /* ... and execute it. */
c4d69da1 1859 offset = cs_offset;
b45305fc 1860 }
e1f99ce6 1861
9d611c03 1862 ret = intel_ring_begin(req, 2);
c4d69da1
CW
1863 if (ret)
1864 return ret;
1865
e2f80391
TU
1866 intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1867 intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1868 0 : MI_BATCH_NON_SECURE));
1869 intel_ring_advance(engine);
c4d69da1 1870
fb3256da
DV
1871 return 0;
1872}
1873
1874static int
53fddaf7 1875i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
9bcb144c 1876 u64 offset, u32 len,
8e004efc 1877 unsigned dispatch_flags)
fb3256da 1878{
4a570db5 1879 struct intel_engine_cs *engine = req->engine;
fb3256da
DV
1880 int ret;
1881
5fb9de1a 1882 ret = intel_ring_begin(req, 2);
fb3256da
DV
1883 if (ret)
1884 return ret;
1885
e2f80391
TU
1886 intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1887 intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1888 0 : MI_BATCH_NON_SECURE));
1889 intel_ring_advance(engine);
62fdfeaf 1890
62fdfeaf
EA
1891 return 0;
1892}
1893
0bc40be8 1894static void cleanup_phys_status_page(struct intel_engine_cs *engine)
7d3fdfff 1895{
c033666a 1896 struct drm_i915_private *dev_priv = engine->i915;
7d3fdfff
VS
1897
1898 if (!dev_priv->status_page_dmah)
1899 return;
1900
91c8a326 1901 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
0bc40be8 1902 engine->status_page.page_addr = NULL;
7d3fdfff
VS
1903}
1904
0bc40be8 1905static void cleanup_status_page(struct intel_engine_cs *engine)
62fdfeaf 1906{
05394f39 1907 struct drm_i915_gem_object *obj;
62fdfeaf 1908
0bc40be8 1909 obj = engine->status_page.obj;
8187a2b7 1910 if (obj == NULL)
62fdfeaf 1911 return;
62fdfeaf 1912
9da3da66 1913 kunmap(sg_page(obj->pages->sgl));
d7f46fc4 1914 i915_gem_object_ggtt_unpin(obj);
f8c417cd 1915 i915_gem_object_put(obj);
0bc40be8 1916 engine->status_page.obj = NULL;
62fdfeaf
EA
1917}
1918
0bc40be8 1919static int init_status_page(struct intel_engine_cs *engine)
62fdfeaf 1920{
0bc40be8 1921 struct drm_i915_gem_object *obj = engine->status_page.obj;
62fdfeaf 1922
7d3fdfff 1923 if (obj == NULL) {
1f767e02 1924 unsigned flags;
e3efda49 1925 int ret;
e4ffd173 1926
91c8a326 1927 obj = i915_gem_object_create(&engine->i915->drm, 4096);
fe3db79b 1928 if (IS_ERR(obj)) {
e3efda49 1929 DRM_ERROR("Failed to allocate status page\n");
fe3db79b 1930 return PTR_ERR(obj);
e3efda49 1931 }
62fdfeaf 1932
e3efda49
CW
1933 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1934 if (ret)
1935 goto err_unref;
1936
1f767e02 1937 flags = 0;
c033666a 1938 if (!HAS_LLC(engine->i915))
1f767e02
CW
1939 /* On g33, we cannot place HWS above 256MiB, so
1940 * restrict its pinning to the low mappable arena.
1941 * Though this restriction is not documented for
1942 * gen4, gen5, or byt, they also behave similarly
1943 * and hang if the HWS is placed at the top of the
1944 * GTT. To generalise, it appears that all !llc
1945 * platforms have issues with us placing the HWS
1946 * above the mappable region (even though we never
1947 * actualy map it).
1948 */
1949 flags |= PIN_MAPPABLE;
1950 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
e3efda49
CW
1951 if (ret) {
1952err_unref:
f8c417cd 1953 i915_gem_object_put(obj);
e3efda49
CW
1954 return ret;
1955 }
1956
0bc40be8 1957 engine->status_page.obj = obj;
e3efda49 1958 }
62fdfeaf 1959
0bc40be8
TU
1960 engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1961 engine->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
1962 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
62fdfeaf 1963
8187a2b7 1964 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
0bc40be8 1965 engine->name, engine->status_page.gfx_addr);
62fdfeaf
EA
1966
1967 return 0;
62fdfeaf
EA
1968}
1969
0bc40be8 1970static int init_phys_status_page(struct intel_engine_cs *engine)
6b8294a4 1971{
c033666a 1972 struct drm_i915_private *dev_priv = engine->i915;
6b8294a4
CW
1973
1974 if (!dev_priv->status_page_dmah) {
1975 dev_priv->status_page_dmah =
91c8a326 1976 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
6b8294a4
CW
1977 if (!dev_priv->status_page_dmah)
1978 return -ENOMEM;
1979 }
1980
0bc40be8
TU
1981 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1982 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
6b8294a4
CW
1983
1984 return 0;
1985}
1986
7ba717cf 1987void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
2919d291 1988{
f2f0ed71
CW
1989 GEM_BUG_ON(!ringbuf->vma);
1990 GEM_BUG_ON(!ringbuf->vaddr);
3d77e9be 1991
def0c5f6 1992 if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
0a798eb9 1993 i915_gem_object_unpin_map(ringbuf->obj);
def0c5f6 1994 else
3d77e9be 1995 i915_vma_unpin_iomap(ringbuf->vma);
f2f0ed71 1996 ringbuf->vaddr = NULL;
3d77e9be 1997
2919d291 1998 i915_gem_object_ggtt_unpin(ringbuf->obj);
3d77e9be 1999 ringbuf->vma = NULL;
7ba717cf
TD
2000}
2001
c033666a 2002int intel_pin_and_map_ringbuffer_obj(struct drm_i915_private *dev_priv,
7ba717cf
TD
2003 struct intel_ringbuffer *ringbuf)
2004{
7ba717cf 2005 struct drm_i915_gem_object *obj = ringbuf->obj;
a687a43a
CW
2006 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
2007 unsigned flags = PIN_OFFSET_BIAS | 4096;
8305216f 2008 void *addr;
7ba717cf
TD
2009 int ret;
2010
def0c5f6 2011 if (HAS_LLC(dev_priv) && !obj->stolen) {
a687a43a 2012 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
def0c5f6
CW
2013 if (ret)
2014 return ret;
7ba717cf 2015
def0c5f6 2016 ret = i915_gem_object_set_to_cpu_domain(obj, true);
d2cad535
CW
2017 if (ret)
2018 goto err_unpin;
def0c5f6 2019
8305216f
DG
2020 addr = i915_gem_object_pin_map(obj);
2021 if (IS_ERR(addr)) {
2022 ret = PTR_ERR(addr);
d2cad535 2023 goto err_unpin;
def0c5f6
CW
2024 }
2025 } else {
a687a43a
CW
2026 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
2027 flags | PIN_MAPPABLE);
def0c5f6
CW
2028 if (ret)
2029 return ret;
7ba717cf 2030
def0c5f6 2031 ret = i915_gem_object_set_to_gtt_domain(obj, true);
d2cad535
CW
2032 if (ret)
2033 goto err_unpin;
def0c5f6 2034
ff3dc087
DCS
2035 /* Access through the GTT requires the device to be awake. */
2036 assert_rpm_wakelock_held(dev_priv);
2037
406ea8d2
CW
2038 addr = (void __force *)
2039 i915_vma_pin_iomap(i915_gem_obj_to_ggtt(obj));
3d77e9be
CW
2040 if (IS_ERR(addr)) {
2041 ret = PTR_ERR(addr);
d2cad535 2042 goto err_unpin;
def0c5f6 2043 }
7ba717cf
TD
2044 }
2045
f2f0ed71 2046 ringbuf->vaddr = addr;
0eb973d3 2047 ringbuf->vma = i915_gem_obj_to_ggtt(obj);
7ba717cf 2048 return 0;
d2cad535
CW
2049
2050err_unpin:
2051 i915_gem_object_ggtt_unpin(obj);
2052 return ret;
7ba717cf
TD
2053}
2054
01101fa7 2055static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
7ba717cf 2056{
f8c417cd 2057 i915_gem_object_put(ringbuf->obj);
2919d291
OM
2058 ringbuf->obj = NULL;
2059}
2060
01101fa7
CW
2061static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
2062 struct intel_ringbuffer *ringbuf)
62fdfeaf 2063{
05394f39 2064 struct drm_i915_gem_object *obj;
62fdfeaf 2065
ebc052e0
CW
2066 obj = NULL;
2067 if (!HAS_LLC(dev))
93b0a4e0 2068 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
ebc052e0 2069 if (obj == NULL)
d37cd8a8 2070 obj = i915_gem_object_create(dev, ringbuf->size);
fe3db79b
CW
2071 if (IS_ERR(obj))
2072 return PTR_ERR(obj);
8187a2b7 2073
24f3a8cf
AG
2074 /* mark ring buffers as read-only from GPU side by default */
2075 obj->gt_ro = 1;
2076
93b0a4e0 2077 ringbuf->obj = obj;
e3efda49 2078
7ba717cf 2079 return 0;
e3efda49
CW
2080}
2081
01101fa7
CW
2082struct intel_ringbuffer *
2083intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
2084{
2085 struct intel_ringbuffer *ring;
2086 int ret;
2087
2088 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
608c1a52
CW
2089 if (ring == NULL) {
2090 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2091 engine->name);
01101fa7 2092 return ERR_PTR(-ENOMEM);
608c1a52 2093 }
01101fa7 2094
4a570db5 2095 ring->engine = engine;
608c1a52 2096 list_add(&ring->link, &engine->buffers);
01101fa7
CW
2097
2098 ring->size = size;
2099 /* Workaround an erratum on the i830 which causes a hang if
2100 * the TAIL pointer points to within the last 2 cachelines
2101 * of the buffer.
2102 */
2103 ring->effective_size = size;
c033666a 2104 if (IS_I830(engine->i915) || IS_845G(engine->i915))
01101fa7
CW
2105 ring->effective_size -= 2 * CACHELINE_BYTES;
2106
2107 ring->last_retired_head = -1;
2108 intel_ring_update_space(ring);
2109
91c8a326 2110 ret = intel_alloc_ringbuffer_obj(&engine->i915->drm, ring);
01101fa7 2111 if (ret) {
608c1a52
CW
2112 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
2113 engine->name, ret);
2114 list_del(&ring->link);
01101fa7
CW
2115 kfree(ring);
2116 return ERR_PTR(ret);
2117 }
2118
2119 return ring;
2120}
2121
2122void
2123intel_ringbuffer_free(struct intel_ringbuffer *ring)
2124{
2125 intel_destroy_ringbuffer_obj(ring);
608c1a52 2126 list_del(&ring->link);
01101fa7
CW
2127 kfree(ring);
2128}
2129
0cb26a8e
CW
2130static int intel_ring_context_pin(struct i915_gem_context *ctx,
2131 struct intel_engine_cs *engine)
2132{
2133 struct intel_context *ce = &ctx->engine[engine->id];
2134 int ret;
2135
91c8a326 2136 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
0cb26a8e
CW
2137
2138 if (ce->pin_count++)
2139 return 0;
2140
2141 if (ce->state) {
2142 ret = i915_gem_obj_ggtt_pin(ce->state, ctx->ggtt_alignment, 0);
2143 if (ret)
2144 goto error;
2145 }
2146
c7c3c07d
CW
2147 /* The kernel context is only used as a placeholder for flushing the
2148 * active context. It is never used for submitting user rendering and
2149 * as such never requires the golden render context, and so we can skip
2150 * emitting it when we switch to the kernel context. This is required
2151 * as during eviction we cannot allocate and pin the renderstate in
2152 * order to initialise the context.
2153 */
2154 if (ctx == ctx->i915->kernel_context)
2155 ce->initialised = true;
2156
9a6feaf0 2157 i915_gem_context_get(ctx);
0cb26a8e
CW
2158 return 0;
2159
2160error:
2161 ce->pin_count = 0;
2162 return ret;
2163}
2164
2165static void intel_ring_context_unpin(struct i915_gem_context *ctx,
2166 struct intel_engine_cs *engine)
2167{
2168 struct intel_context *ce = &ctx->engine[engine->id];
2169
91c8a326 2170 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
0cb26a8e
CW
2171
2172 if (--ce->pin_count)
2173 return;
2174
2175 if (ce->state)
2176 i915_gem_object_ggtt_unpin(ce->state);
2177
9a6feaf0 2178 i915_gem_context_put(ctx);
0cb26a8e
CW
2179}
2180
acd27845 2181static int intel_init_ring_buffer(struct intel_engine_cs *engine)
e3efda49 2182{
acd27845 2183 struct drm_i915_private *dev_priv = engine->i915;
bfc882b4 2184 struct intel_ringbuffer *ringbuf;
e3efda49
CW
2185 int ret;
2186
0bc40be8 2187 WARN_ON(engine->buffer);
bfc882b4 2188
019bf277
TU
2189 intel_engine_setup_common(engine);
2190
0bc40be8
TU
2191 memset(engine->semaphore.sync_seqno, 0,
2192 sizeof(engine->semaphore.sync_seqno));
e3efda49 2193
019bf277 2194 ret = intel_engine_init_common(engine);
688e6c72
CW
2195 if (ret)
2196 goto error;
e3efda49 2197
0cb26a8e
CW
2198 /* We may need to do things with the shrinker which
2199 * require us to immediately switch back to the default
2200 * context. This can cause a problem as pinning the
2201 * default context also requires GTT space which may not
2202 * be available. To avoid this we always pin the default
2203 * context.
2204 */
2205 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2206 if (ret)
2207 goto error;
2208
0bc40be8 2209 ringbuf = intel_engine_create_ringbuffer(engine, 32 * PAGE_SIZE);
b0366a54
DG
2210 if (IS_ERR(ringbuf)) {
2211 ret = PTR_ERR(ringbuf);
2212 goto error;
2213 }
0bc40be8 2214 engine->buffer = ringbuf;
01101fa7 2215
c033666a 2216 if (I915_NEED_GFX_HWS(dev_priv)) {
0bc40be8 2217 ret = init_status_page(engine);
e3efda49 2218 if (ret)
8ee14975 2219 goto error;
e3efda49 2220 } else {
0bc40be8
TU
2221 WARN_ON(engine->id != RCS);
2222 ret = init_phys_status_page(engine);
e3efda49 2223 if (ret)
8ee14975 2224 goto error;
e3efda49
CW
2225 }
2226
c033666a 2227 ret = intel_pin_and_map_ringbuffer_obj(dev_priv, ringbuf);
bfc882b4
DV
2228 if (ret) {
2229 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
0bc40be8 2230 engine->name, ret);
bfc882b4
DV
2231 intel_destroy_ringbuffer_obj(ringbuf);
2232 goto error;
e3efda49 2233 }
62fdfeaf 2234
8ee14975 2235 return 0;
351e3db2 2236
8ee14975 2237error:
117897f4 2238 intel_cleanup_engine(engine);
8ee14975 2239 return ret;
62fdfeaf
EA
2240}
2241
117897f4 2242void intel_cleanup_engine(struct intel_engine_cs *engine)
62fdfeaf 2243{
6402c330 2244 struct drm_i915_private *dev_priv;
33626e6a 2245
117897f4 2246 if (!intel_engine_initialized(engine))
62fdfeaf
EA
2247 return;
2248
c033666a 2249 dev_priv = engine->i915;
6402c330 2250
0bc40be8 2251 if (engine->buffer) {
117897f4 2252 intel_stop_engine(engine);
c033666a 2253 WARN_ON(!IS_GEN2(dev_priv) && (I915_READ_MODE(engine) & MODE_IDLE) == 0);
33626e6a 2254
0bc40be8
TU
2255 intel_unpin_ringbuffer_obj(engine->buffer);
2256 intel_ringbuffer_free(engine->buffer);
2257 engine->buffer = NULL;
b0366a54 2258 }
78501eac 2259
0bc40be8
TU
2260 if (engine->cleanup)
2261 engine->cleanup(engine);
8d19215b 2262
c033666a 2263 if (I915_NEED_GFX_HWS(dev_priv)) {
0bc40be8 2264 cleanup_status_page(engine);
7d3fdfff 2265 } else {
0bc40be8
TU
2266 WARN_ON(engine->id != RCS);
2267 cleanup_phys_status_page(engine);
7d3fdfff 2268 }
44e895a8 2269
33a051a5 2270 intel_engine_cleanup_cmd_parser(engine);
0bc40be8 2271 i915_gem_batch_pool_fini(&engine->batch_pool);
688e6c72 2272 intel_engine_fini_breadcrumbs(engine);
0cb26a8e
CW
2273
2274 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2275
c033666a 2276 engine->i915 = NULL;
62fdfeaf
EA
2277}
2278
666796da 2279int intel_engine_idle(struct intel_engine_cs *engine)
3e960501 2280{
a4b3a571 2281 struct drm_i915_gem_request *req;
3e960501 2282
3e960501 2283 /* Wait upon the last request to be completed */
0bc40be8 2284 if (list_empty(&engine->request_list))
3e960501
CW
2285 return 0;
2286
0bc40be8
TU
2287 req = list_entry(engine->request_list.prev,
2288 struct drm_i915_gem_request,
2289 list);
b4716185
CW
2290
2291 /* Make sure we do not trigger any retires */
2292 return __i915_wait_request(req,
c19ae989 2293 req->i915->mm.interruptible,
b4716185 2294 NULL, NULL);
3e960501
CW
2295}
2296
6689cb2b 2297int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
9d773091 2298{
6310346e
CW
2299 int ret;
2300
2301 /* Flush enough space to reduce the likelihood of waiting after
2302 * we start building the request - in which case we will just
2303 * have to repeat work.
2304 */
a0442461 2305 request->reserved_space += LEGACY_REQUEST_SIZE;
6310346e 2306
4a570db5 2307 request->ringbuf = request->engine->buffer;
6310346e
CW
2308
2309 ret = intel_ring_begin(request, 0);
2310 if (ret)
2311 return ret;
2312
a0442461 2313 request->reserved_space -= LEGACY_REQUEST_SIZE;
6310346e 2314 return 0;
9d773091
CW
2315}
2316
987046ad
CW
2317static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
2318{
2319 struct intel_ringbuffer *ringbuf = req->ringbuf;
2320 struct intel_engine_cs *engine = req->engine;
2321 struct drm_i915_gem_request *target;
2322
2323 intel_ring_update_space(ringbuf);
2324 if (ringbuf->space >= bytes)
2325 return 0;
2326
2327 /*
2328 * Space is reserved in the ringbuffer for finalising the request,
2329 * as that cannot be allowed to fail. During request finalisation,
2330 * reserved_space is set to 0 to stop the overallocation and the
2331 * assumption is that then we never need to wait (which has the
2332 * risk of failing with EINTR).
2333 *
2334 * See also i915_gem_request_alloc() and i915_add_request().
2335 */
0251a963 2336 GEM_BUG_ON(!req->reserved_space);
987046ad
CW
2337
2338 list_for_each_entry(target, &engine->request_list, list) {
2339 unsigned space;
2340
79bbcc29 2341 /*
987046ad
CW
2342 * The request queue is per-engine, so can contain requests
2343 * from multiple ringbuffers. Here, we must ignore any that
2344 * aren't from the ringbuffer we're considering.
79bbcc29 2345 */
987046ad
CW
2346 if (target->ringbuf != ringbuf)
2347 continue;
2348
2349 /* Would completion of this request free enough space? */
2350 space = __intel_ring_space(target->postfix, ringbuf->tail,
2351 ringbuf->size);
2352 if (space >= bytes)
2353 break;
79bbcc29 2354 }
29b1b415 2355
987046ad
CW
2356 if (WARN_ON(&target->list == &engine->request_list))
2357 return -ENOSPC;
2358
2359 return i915_wait_request(target);
29b1b415
JH
2360}
2361
987046ad 2362int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
cbcc80df 2363{
987046ad 2364 struct intel_ringbuffer *ringbuf = req->ringbuf;
79bbcc29 2365 int remain_actual = ringbuf->size - ringbuf->tail;
987046ad
CW
2366 int remain_usable = ringbuf->effective_size - ringbuf->tail;
2367 int bytes = num_dwords * sizeof(u32);
2368 int total_bytes, wait_bytes;
79bbcc29 2369 bool need_wrap = false;
29b1b415 2370
0251a963 2371 total_bytes = bytes + req->reserved_space;
29b1b415 2372
79bbcc29
JH
2373 if (unlikely(bytes > remain_usable)) {
2374 /*
2375 * Not enough space for the basic request. So need to flush
2376 * out the remainder and then wait for base + reserved.
2377 */
2378 wait_bytes = remain_actual + total_bytes;
2379 need_wrap = true;
987046ad
CW
2380 } else if (unlikely(total_bytes > remain_usable)) {
2381 /*
2382 * The base request will fit but the reserved space
2383 * falls off the end. So we don't need an immediate wrap
2384 * and only need to effectively wait for the reserved
2385 * size space from the start of ringbuffer.
2386 */
0251a963 2387 wait_bytes = remain_actual + req->reserved_space;
79bbcc29 2388 } else {
987046ad
CW
2389 /* No wrapping required, just waiting. */
2390 wait_bytes = total_bytes;
cbcc80df
MK
2391 }
2392
987046ad
CW
2393 if (wait_bytes > ringbuf->space) {
2394 int ret = wait_for_space(req, wait_bytes);
cbcc80df
MK
2395 if (unlikely(ret))
2396 return ret;
79bbcc29 2397
987046ad 2398 intel_ring_update_space(ringbuf);
e075a32f
CW
2399 if (unlikely(ringbuf->space < wait_bytes))
2400 return -EAGAIN;
cbcc80df
MK
2401 }
2402
987046ad
CW
2403 if (unlikely(need_wrap)) {
2404 GEM_BUG_ON(remain_actual > ringbuf->space);
2405 GEM_BUG_ON(ringbuf->tail + remain_actual > ringbuf->size);
78501eac 2406
987046ad 2407 /* Fill the tail with MI_NOOP */
f2f0ed71 2408 memset(ringbuf->vaddr + ringbuf->tail, 0, remain_actual);
987046ad
CW
2409 ringbuf->tail = 0;
2410 ringbuf->space -= remain_actual;
2411 }
304d695c 2412
987046ad
CW
2413 ringbuf->space -= bytes;
2414 GEM_BUG_ON(ringbuf->space < 0);
304d695c 2415 return 0;
8187a2b7 2416}
78501eac 2417
753b1ad4 2418/* Align the ring tail to a cacheline boundary */
bba09b12 2419int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
753b1ad4 2420{
4a570db5 2421 struct intel_engine_cs *engine = req->engine;
e2f80391 2422 int num_dwords = (engine->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
753b1ad4
VS
2423 int ret;
2424
2425 if (num_dwords == 0)
2426 return 0;
2427
18393f63 2428 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
5fb9de1a 2429 ret = intel_ring_begin(req, num_dwords);
753b1ad4
VS
2430 if (ret)
2431 return ret;
2432
2433 while (num_dwords--)
e2f80391 2434 intel_ring_emit(engine, MI_NOOP);
753b1ad4 2435
e2f80391 2436 intel_ring_advance(engine);
753b1ad4
VS
2437
2438 return 0;
2439}
2440
0bc40be8 2441void intel_ring_init_seqno(struct intel_engine_cs *engine, u32 seqno)
498d2ac1 2442{
c033666a 2443 struct drm_i915_private *dev_priv = engine->i915;
498d2ac1 2444
29dcb570
CW
2445 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
2446 * so long as the semaphore value in the register/page is greater
2447 * than the sync value), so whenever we reset the seqno,
2448 * so long as we reset the tracking semaphore value to 0, it will
2449 * always be before the next request's seqno. If we don't reset
2450 * the semaphore value, then when the seqno moves backwards all
2451 * future waits will complete instantly (causing rendering corruption).
2452 */
7e22dbbb 2453 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
0bc40be8
TU
2454 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
2455 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
d04bce48 2456 if (HAS_VEBOX(dev_priv))
0bc40be8 2457 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
e1f99ce6 2458 }
a058d934
CW
2459 if (dev_priv->semaphore_obj) {
2460 struct drm_i915_gem_object *obj = dev_priv->semaphore_obj;
2461 struct page *page = i915_gem_object_get_dirty_page(obj, 0);
2462 void *semaphores = kmap(page);
2463 memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
2464 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
2465 kunmap(page);
2466 }
29dcb570
CW
2467 memset(engine->semaphore.sync_seqno, 0,
2468 sizeof(engine->semaphore.sync_seqno));
d97ed339 2469
1b7744e7
CW
2470 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
2471 if (engine->irq_seqno_barrier)
2472 engine->irq_seqno_barrier(engine);
01347126 2473 engine->last_submitted_seqno = seqno;
29dcb570 2474
0bc40be8 2475 engine->hangcheck.seqno = seqno;
688e6c72
CW
2476
2477 /* After manually advancing the seqno, fake the interrupt in case
2478 * there are any waiters for that seqno.
2479 */
2480 rcu_read_lock();
2481 intel_engine_wakeup(engine);
2482 rcu_read_unlock();
8187a2b7 2483}
62fdfeaf 2484
0bc40be8 2485static void gen6_bsd_ring_write_tail(struct intel_engine_cs *engine,
297b0c5b 2486 u32 value)
881f47b6 2487{
c033666a 2488 struct drm_i915_private *dev_priv = engine->i915;
881f47b6 2489
76f8421f
CW
2490 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2491
881f47b6 2492 /* Every tail move must follow the sequence below */
12f55818
CW
2493
2494 /* Disable notification that the ring is IDLE. The GT
2495 * will then assume that it is busy and bring it out of rc6.
2496 */
76f8421f
CW
2497 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2498 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
12f55818
CW
2499
2500 /* Clear the context id. Here be magic! */
76f8421f 2501 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
0206e353 2502
12f55818 2503 /* Wait for the ring not to be idle, i.e. for it to wake up. */
76f8421f
CW
2504 if (intel_wait_for_register_fw(dev_priv,
2505 GEN6_BSD_SLEEP_PSMI_CONTROL,
2506 GEN6_BSD_SLEEP_INDICATOR,
2507 0,
2508 50))
12f55818 2509 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
0206e353 2510
12f55818 2511 /* Now that the ring is fully powered up, update the tail */
76f8421f
CW
2512 I915_WRITE_FW(RING_TAIL(engine->mmio_base), value);
2513 POSTING_READ_FW(RING_TAIL(engine->mmio_base));
12f55818
CW
2514
2515 /* Let the ring send IDLE messages to the GT again,
2516 * and so let it sleep to conserve power when idle.
2517 */
76f8421f
CW
2518 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2519 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2520
2521 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
881f47b6
XH
2522}
2523
a84c3ae1 2524static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
ea251324 2525 u32 invalidate, u32 flush)
881f47b6 2526{
4a570db5 2527 struct intel_engine_cs *engine = req->engine;
71a77e07 2528 uint32_t cmd;
b72f3acb
CW
2529 int ret;
2530
5fb9de1a 2531 ret = intel_ring_begin(req, 4);
b72f3acb
CW
2532 if (ret)
2533 return ret;
2534
71a77e07 2535 cmd = MI_FLUSH_DW;
c033666a 2536 if (INTEL_GEN(req->i915) >= 8)
075b3bba 2537 cmd += 1;
f0a1fb10
CW
2538
2539 /* We always require a command barrier so that subsequent
2540 * commands, such as breadcrumb interrupts, are strictly ordered
2541 * wrt the contents of the write cache being flushed to memory
2542 * (and thus being coherent from the CPU).
2543 */
2544 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2545
9a289771
JB
2546 /*
2547 * Bspec vol 1c.5 - video engine command streamer:
2548 * "If ENABLED, all TLBs will be invalidated once the flush
2549 * operation is complete. This bit is only valid when the
2550 * Post-Sync Operation field is a value of 1h or 3h."
2551 */
71a77e07 2552 if (invalidate & I915_GEM_GPU_DOMAINS)
f0a1fb10
CW
2553 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2554
e2f80391
TU
2555 intel_ring_emit(engine, cmd);
2556 intel_ring_emit(engine,
2557 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
c033666a 2558 if (INTEL_GEN(req->i915) >= 8) {
e2f80391
TU
2559 intel_ring_emit(engine, 0); /* upper addr */
2560 intel_ring_emit(engine, 0); /* value */
075b3bba 2561 } else {
e2f80391
TU
2562 intel_ring_emit(engine, 0);
2563 intel_ring_emit(engine, MI_NOOP);
075b3bba 2564 }
e2f80391 2565 intel_ring_advance(engine);
b72f3acb 2566 return 0;
881f47b6
XH
2567}
2568
1c7a0623 2569static int
53fddaf7 2570gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
9bcb144c 2571 u64 offset, u32 len,
8e004efc 2572 unsigned dispatch_flags)
1c7a0623 2573{
4a570db5 2574 struct intel_engine_cs *engine = req->engine;
e2f80391 2575 bool ppgtt = USES_PPGTT(engine->dev) &&
8e004efc 2576 !(dispatch_flags & I915_DISPATCH_SECURE);
1c7a0623
BW
2577 int ret;
2578
5fb9de1a 2579 ret = intel_ring_begin(req, 4);
1c7a0623
BW
2580 if (ret)
2581 return ret;
2582
2583 /* FIXME(BDW): Address space and security selectors. */
e2f80391 2584 intel_ring_emit(engine, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
919032ec
AJ
2585 (dispatch_flags & I915_DISPATCH_RS ?
2586 MI_BATCH_RESOURCE_STREAMER : 0));
e2f80391
TU
2587 intel_ring_emit(engine, lower_32_bits(offset));
2588 intel_ring_emit(engine, upper_32_bits(offset));
2589 intel_ring_emit(engine, MI_NOOP);
2590 intel_ring_advance(engine);
1c7a0623
BW
2591
2592 return 0;
2593}
2594
d7d4eedd 2595static int
53fddaf7 2596hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
8e004efc
JH
2597 u64 offset, u32 len,
2598 unsigned dispatch_flags)
d7d4eedd 2599{
4a570db5 2600 struct intel_engine_cs *engine = req->engine;
d7d4eedd
CW
2601 int ret;
2602
5fb9de1a 2603 ret = intel_ring_begin(req, 2);
d7d4eedd
CW
2604 if (ret)
2605 return ret;
2606
e2f80391 2607 intel_ring_emit(engine,
77072258 2608 MI_BATCH_BUFFER_START |
8e004efc 2609 (dispatch_flags & I915_DISPATCH_SECURE ?
919032ec
AJ
2610 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2611 (dispatch_flags & I915_DISPATCH_RS ?
2612 MI_BATCH_RESOURCE_STREAMER : 0));
d7d4eedd 2613 /* bit0-7 is the length on GEN6+ */
e2f80391
TU
2614 intel_ring_emit(engine, offset);
2615 intel_ring_advance(engine);
d7d4eedd
CW
2616
2617 return 0;
2618}
2619
881f47b6 2620static int
53fddaf7 2621gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
9bcb144c 2622 u64 offset, u32 len,
8e004efc 2623 unsigned dispatch_flags)
881f47b6 2624{
4a570db5 2625 struct intel_engine_cs *engine = req->engine;
0206e353 2626 int ret;
ab6f8e32 2627
5fb9de1a 2628 ret = intel_ring_begin(req, 2);
0206e353
AJ
2629 if (ret)
2630 return ret;
e1f99ce6 2631
e2f80391 2632 intel_ring_emit(engine,
d7d4eedd 2633 MI_BATCH_BUFFER_START |
8e004efc
JH
2634 (dispatch_flags & I915_DISPATCH_SECURE ?
2635 0 : MI_BATCH_NON_SECURE_I965));
0206e353 2636 /* bit0-7 is the length on GEN6+ */
e2f80391
TU
2637 intel_ring_emit(engine, offset);
2638 intel_ring_advance(engine);
ab6f8e32 2639
0206e353 2640 return 0;
881f47b6
XH
2641}
2642
549f7365
CW
2643/* Blitter support (SandyBridge+) */
2644
a84c3ae1 2645static int gen6_ring_flush(struct drm_i915_gem_request *req,
ea251324 2646 u32 invalidate, u32 flush)
8d19215b 2647{
4a570db5 2648 struct intel_engine_cs *engine = req->engine;
71a77e07 2649 uint32_t cmd;
b72f3acb
CW
2650 int ret;
2651
5fb9de1a 2652 ret = intel_ring_begin(req, 4);
b72f3acb
CW
2653 if (ret)
2654 return ret;
2655
71a77e07 2656 cmd = MI_FLUSH_DW;
c033666a 2657 if (INTEL_GEN(req->i915) >= 8)
075b3bba 2658 cmd += 1;
f0a1fb10
CW
2659
2660 /* We always require a command barrier so that subsequent
2661 * commands, such as breadcrumb interrupts, are strictly ordered
2662 * wrt the contents of the write cache being flushed to memory
2663 * (and thus being coherent from the CPU).
2664 */
2665 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2666
9a289771
JB
2667 /*
2668 * Bspec vol 1c.3 - blitter engine command streamer:
2669 * "If ENABLED, all TLBs will be invalidated once the flush
2670 * operation is complete. This bit is only valid when the
2671 * Post-Sync Operation field is a value of 1h or 3h."
2672 */
71a77e07 2673 if (invalidate & I915_GEM_DOMAIN_RENDER)
f0a1fb10 2674 cmd |= MI_INVALIDATE_TLB;
e2f80391
TU
2675 intel_ring_emit(engine, cmd);
2676 intel_ring_emit(engine,
2677 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
c033666a 2678 if (INTEL_GEN(req->i915) >= 8) {
e2f80391
TU
2679 intel_ring_emit(engine, 0); /* upper addr */
2680 intel_ring_emit(engine, 0); /* value */
075b3bba 2681 } else {
e2f80391
TU
2682 intel_ring_emit(engine, 0);
2683 intel_ring_emit(engine, MI_NOOP);
075b3bba 2684 }
e2f80391 2685 intel_ring_advance(engine);
fd3da6c9 2686
b72f3acb 2687 return 0;
8d19215b
ZN
2688}
2689
d9a64610
TU
2690static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2691 struct intel_engine_cs *engine)
2692{
db3d4019 2693 struct drm_i915_gem_object *obj;
1b9e6650 2694 int ret, i;
db3d4019 2695
39df9190 2696 if (!i915.semaphores)
db3d4019
TU
2697 return;
2698
2699 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore_obj) {
91c8a326 2700 obj = i915_gem_object_create(&dev_priv->drm, 4096);
db3d4019
TU
2701 if (IS_ERR(obj)) {
2702 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2703 i915.semaphores = 0;
2704 } else {
2705 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2706 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2707 if (ret != 0) {
f8c417cd 2708 i915_gem_object_put(obj);
db3d4019
TU
2709 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2710 i915.semaphores = 0;
2711 } else {
2712 dev_priv->semaphore_obj = obj;
2713 }
2714 }
2715 }
2716
39df9190 2717 if (!i915.semaphores)
d9a64610
TU
2718 return;
2719
2720 if (INTEL_GEN(dev_priv) >= 8) {
1b9e6650
TU
2721 u64 offset = i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj);
2722
d9a64610
TU
2723 engine->semaphore.sync_to = gen8_ring_sync;
2724 engine->semaphore.signal = gen8_xcs_signal;
1b9e6650
TU
2725
2726 for (i = 0; i < I915_NUM_ENGINES; i++) {
2727 u64 ring_offset;
2728
2729 if (i != engine->id)
2730 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2731 else
2732 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2733
2734 engine->semaphore.signal_ggtt[i] = ring_offset;
2735 }
d9a64610
TU
2736 } else if (INTEL_GEN(dev_priv) >= 6) {
2737 engine->semaphore.sync_to = gen6_ring_sync;
2738 engine->semaphore.signal = gen6_signal;
4b8e38a9
TU
2739
2740 /*
2741 * The current semaphore is only applied on pre-gen8
2742 * platform. And there is no VCS2 ring on the pre-gen8
2743 * platform. So the semaphore between RCS and VCS2 is
2744 * initialized as INVALID. Gen8 will initialize the
2745 * sema between VCS2 and RCS later.
2746 */
2747 for (i = 0; i < I915_NUM_ENGINES; i++) {
2748 static const struct {
2749 u32 wait_mbox;
2750 i915_reg_t mbox_reg;
2751 } sem_data[I915_NUM_ENGINES][I915_NUM_ENGINES] = {
2752 [RCS] = {
2753 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2754 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2755 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
2756 },
2757 [VCS] = {
2758 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2759 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2760 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
2761 },
2762 [BCS] = {
2763 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2764 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2765 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
2766 },
2767 [VECS] = {
2768 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2769 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2770 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
2771 },
2772 };
2773 u32 wait_mbox;
2774 i915_reg_t mbox_reg;
2775
2776 if (i == engine->id || i == VCS2) {
2777 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2778 mbox_reg = GEN6_NOSYNC;
2779 } else {
2780 wait_mbox = sem_data[engine->id][i].wait_mbox;
2781 mbox_reg = sem_data[engine->id][i].mbox_reg;
2782 }
2783
2784 engine->semaphore.mbox.wait[i] = wait_mbox;
2785 engine->semaphore.mbox.signal[i] = mbox_reg;
2786 }
d9a64610
TU
2787 }
2788}
2789
ed003078
CW
2790static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2791 struct intel_engine_cs *engine)
2792{
c78d6061
TU
2793 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2794
ed003078 2795 if (INTEL_GEN(dev_priv) >= 8) {
31bb59cc
CW
2796 engine->irq_enable = gen8_irq_enable;
2797 engine->irq_disable = gen8_irq_disable;
ed003078
CW
2798 engine->irq_seqno_barrier = gen6_seqno_barrier;
2799 } else if (INTEL_GEN(dev_priv) >= 6) {
31bb59cc
CW
2800 engine->irq_enable = gen6_irq_enable;
2801 engine->irq_disable = gen6_irq_disable;
ed003078
CW
2802 engine->irq_seqno_barrier = gen6_seqno_barrier;
2803 } else if (INTEL_GEN(dev_priv) >= 5) {
31bb59cc
CW
2804 engine->irq_enable = gen5_irq_enable;
2805 engine->irq_disable = gen5_irq_disable;
f8973c21 2806 engine->irq_seqno_barrier = gen5_seqno_barrier;
ed003078 2807 } else if (INTEL_GEN(dev_priv) >= 3) {
31bb59cc
CW
2808 engine->irq_enable = i9xx_irq_enable;
2809 engine->irq_disable = i9xx_irq_disable;
ed003078 2810 } else {
31bb59cc
CW
2811 engine->irq_enable = i8xx_irq_enable;
2812 engine->irq_disable = i8xx_irq_disable;
ed003078
CW
2813 }
2814}
2815
06a2fe22
TU
2816static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2817 struct intel_engine_cs *engine)
2818{
1d8a1337 2819 engine->init_hw = init_ring_common;
06a2fe22 2820 engine->write_tail = ring_write_tail;
7445a2a4 2821
6f7bef75
CW
2822 engine->add_request = i9xx_add_request;
2823 if (INTEL_GEN(dev_priv) >= 6)
960ecaad 2824 engine->add_request = gen6_add_request;
6f7bef75
CW
2825
2826 if (INTEL_GEN(dev_priv) >= 8)
2827 engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2828 else if (INTEL_GEN(dev_priv) >= 6)
960ecaad 2829 engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
6f7bef75 2830 else if (INTEL_GEN(dev_priv) >= 4)
960ecaad 2831 engine->dispatch_execbuffer = i965_dispatch_execbuffer;
6f7bef75
CW
2832 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
2833 engine->dispatch_execbuffer = i830_dispatch_execbuffer;
2834 else
2835 engine->dispatch_execbuffer = i915_dispatch_execbuffer;
b9700325 2836
ed003078 2837 intel_ring_init_irq(dev_priv, engine);
d9a64610 2838 intel_ring_init_semaphores(dev_priv, engine);
06a2fe22
TU
2839}
2840
8b3e2d36 2841int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
5c1143bb 2842{
8b3e2d36 2843 struct drm_i915_private *dev_priv = engine->i915;
3e78998a 2844 int ret;
5c1143bb 2845
06a2fe22
TU
2846 intel_ring_default_vfuncs(dev_priv, engine);
2847
61ff75ac
CW
2848 if (HAS_L3_DPF(dev_priv))
2849 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
f8973c21 2850
c033666a 2851 if (INTEL_GEN(dev_priv) >= 8) {
e2f80391 2852 engine->init_context = intel_rcs_ctx_init;
a58c01aa 2853 engine->add_request = gen8_render_add_request;
e2f80391 2854 engine->flush = gen8_render_ring_flush;
39df9190 2855 if (i915.semaphores)
e2f80391 2856 engine->semaphore.signal = gen8_rcs_signal;
c033666a 2857 } else if (INTEL_GEN(dev_priv) >= 6) {
e2f80391 2858 engine->init_context = intel_rcs_ctx_init;
e2f80391 2859 engine->flush = gen7_render_ring_flush;
c033666a 2860 if (IS_GEN6(dev_priv))
e2f80391 2861 engine->flush = gen6_render_ring_flush;
c033666a 2862 } else if (IS_GEN5(dev_priv)) {
e2f80391 2863 engine->flush = gen4_render_ring_flush;
59465b5f 2864 } else {
c033666a 2865 if (INTEL_GEN(dev_priv) < 4)
e2f80391 2866 engine->flush = gen2_render_ring_flush;
46f0f8d1 2867 else
e2f80391 2868 engine->flush = gen4_render_ring_flush;
e2f80391 2869 engine->irq_enable_mask = I915_USER_INTERRUPT;
1ec14ad3 2870 }
707d9cf9 2871
c033666a 2872 if (IS_HASWELL(dev_priv))
e2f80391 2873 engine->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
6f7bef75 2874
e2f80391
TU
2875 engine->init_hw = init_render_ring;
2876 engine->cleanup = render_ring_cleanup;
59465b5f 2877
acd27845 2878 ret = intel_init_ring_buffer(engine);
99be1dfe
DV
2879 if (ret)
2880 return ret;
2881
f8973c21 2882 if (INTEL_GEN(dev_priv) >= 6) {
7d5ea807
CW
2883 ret = intel_init_pipe_control(engine, 4096);
2884 if (ret)
2885 return ret;
2886 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
2887 ret = intel_init_pipe_control(engine, I830_WA_SIZE);
99be1dfe
DV
2888 if (ret)
2889 return ret;
2890 }
2891
2892 return 0;
5c1143bb
XH
2893}
2894
8b3e2d36 2895int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
5c1143bb 2896{
8b3e2d36 2897 struct drm_i915_private *dev_priv = engine->i915;
58fa3835 2898
06a2fe22
TU
2899 intel_ring_default_vfuncs(dev_priv, engine);
2900
c033666a 2901 if (INTEL_GEN(dev_priv) >= 6) {
0fd2c201 2902 /* gen6 bsd needs a special wa for tail updates */
c033666a 2903 if (IS_GEN6(dev_priv))
e2f80391
TU
2904 engine->write_tail = gen6_bsd_ring_write_tail;
2905 engine->flush = gen6_bsd_ring_flush;
c78d6061 2906 if (INTEL_GEN(dev_priv) < 8)
e2f80391 2907 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
58fa3835 2908 } else {
e2f80391
TU
2909 engine->mmio_base = BSD_RING_BASE;
2910 engine->flush = bsd_ring_flush;
8d228911 2911 if (IS_GEN5(dev_priv))
e2f80391 2912 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
8d228911 2913 else
e2f80391 2914 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
58fa3835 2915 }
58fa3835 2916
acd27845 2917 return intel_init_ring_buffer(engine);
5c1143bb 2918}
549f7365 2919
845f74a7 2920/**
62659920 2921 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
845f74a7 2922 */
8b3e2d36 2923int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
845f74a7 2924{
8b3e2d36 2925 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2926
2927 intel_ring_default_vfuncs(dev_priv, engine);
2928
e2f80391 2929 engine->flush = gen6_bsd_ring_flush;
845f74a7 2930
acd27845 2931 return intel_init_ring_buffer(engine);
845f74a7
ZY
2932}
2933
8b3e2d36 2934int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
549f7365 2935{
8b3e2d36 2936 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2937
2938 intel_ring_default_vfuncs(dev_priv, engine);
2939
e2f80391 2940 engine->flush = gen6_ring_flush;
c78d6061 2941 if (INTEL_GEN(dev_priv) < 8)
e2f80391 2942 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
549f7365 2943
acd27845 2944 return intel_init_ring_buffer(engine);
549f7365 2945}
a7b9761d 2946
8b3e2d36 2947int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
9a8a2213 2948{
8b3e2d36 2949 struct drm_i915_private *dev_priv = engine->i915;
06a2fe22
TU
2950
2951 intel_ring_default_vfuncs(dev_priv, engine);
2952
e2f80391 2953 engine->flush = gen6_ring_flush;
abd58f01 2954
c78d6061 2955 if (INTEL_GEN(dev_priv) < 8) {
e2f80391 2956 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
31bb59cc
CW
2957 engine->irq_enable = hsw_vebox_irq_enable;
2958 engine->irq_disable = hsw_vebox_irq_disable;
abd58f01 2959 }
9a8a2213 2960
acd27845 2961 return intel_init_ring_buffer(engine);
9a8a2213
BW
2962}
2963
a7b9761d 2964int
4866d729 2965intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
a7b9761d 2966{
4a570db5 2967 struct intel_engine_cs *engine = req->engine;
a7b9761d
CW
2968 int ret;
2969
e2f80391 2970 if (!engine->gpu_caches_dirty)
a7b9761d
CW
2971 return 0;
2972
e2f80391 2973 ret = engine->flush(req, 0, I915_GEM_GPU_DOMAINS);
a7b9761d
CW
2974 if (ret)
2975 return ret;
2976
a84c3ae1 2977 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
a7b9761d 2978
e2f80391 2979 engine->gpu_caches_dirty = false;
a7b9761d
CW
2980 return 0;
2981}
2982
2983int
2f20055d 2984intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
a7b9761d 2985{
4a570db5 2986 struct intel_engine_cs *engine = req->engine;
a7b9761d
CW
2987 uint32_t flush_domains;
2988 int ret;
2989
2990 flush_domains = 0;
e2f80391 2991 if (engine->gpu_caches_dirty)
a7b9761d
CW
2992 flush_domains = I915_GEM_GPU_DOMAINS;
2993
e2f80391 2994 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
a7b9761d
CW
2995 if (ret)
2996 return ret;
2997
a84c3ae1 2998 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
a7b9761d 2999
e2f80391 3000 engine->gpu_caches_dirty = false;
a7b9761d
CW
3001 return 0;
3002}
e3efda49
CW
3003
3004void
117897f4 3005intel_stop_engine(struct intel_engine_cs *engine)
e3efda49
CW
3006{
3007 int ret;
3008
117897f4 3009 if (!intel_engine_initialized(engine))
e3efda49
CW
3010 return;
3011
666796da 3012 ret = intel_engine_idle(engine);
f4457ae7 3013 if (ret)
e3efda49 3014 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
0bc40be8 3015 engine->name, ret);
e3efda49 3016
0bc40be8 3017 stop_ring(engine);
e3efda49 3018}
This page took 0.812243 seconds and 5 git commands to generate.