blob: acbabbdececdf28de4a48f8485ebd0731a852d62 [file] [log] [blame]
Eric Anholt62fdfea2010-05-21 13:26:39 -07001/*
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
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +080030#include <linux/log2.h>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070032#include "i915_drv.h"
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/i915_drm.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070034#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010035#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070036
Chris Wilsona0442462016-04-29 09:07:05 +010037/* 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
Oscar Mateo82e104c2014-07-24 17:04:26 +010042int __intel_ring_space(int head, int tail, int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010043{
Dave Gordon4f547412014-11-27 11:22:48 +000044 int space = head - tail;
45 if (space <= 0)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010046 space += size;
Dave Gordon4f547412014-11-27 11:22:48 +000047 return space - I915_RING_FREE_SPACE;
Chris Wilson1cf0ba12014-05-05 09:07:33 +010048}
49
Chris Wilson7e37f882016-08-02 22:50:21 +010050void intel_ring_update_space(struct intel_ring *ringbuf)
Dave Gordonebd0fd42014-11-27 11:22:49 +000051{
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
Chris Wilsonb5321f32016-08-02 22:50:18 +010061static void __intel_engine_submit(struct intel_engine_cs *engine)
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020062{
Chris Wilson7e37f882016-08-02 22:50:21 +010063 struct intel_ring *ring = engine->buffer;
64
65 ring->tail &= ring->size - 1;
66 engine->write_tail(engine, ring->tail);
Chris Wilson09246732013-08-10 22:16:32 +010067}
68
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000069static int
John Harrisona84c3ae2015-05-29 17:43:57 +010070gen2_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010071 u32 invalidate_domains,
72 u32 flush_domains)
73{
Chris Wilson7e37f882016-08-02 22:50:21 +010074 struct intel_ring *ring = req->ring;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010075 u32 cmd;
76 int ret;
77
78 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +020079 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010080 cmd |= MI_NO_WRITE_FLUSH;
81
82 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
83 cmd |= MI_READ_FLUSH;
84
John Harrison5fb9de12015-05-29 17:44:07 +010085 ret = intel_ring_begin(req, 2);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010086 if (ret)
87 return ret;
88
Chris Wilsonb5321f32016-08-02 22:50:18 +010089 intel_ring_emit(ring, cmd);
90 intel_ring_emit(ring, MI_NOOP);
91 intel_ring_advance(ring);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010092
93 return 0;
94}
95
96static int
John Harrisona84c3ae2015-05-29 17:43:57 +010097gen4_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010098 u32 invalidate_domains,
99 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700100{
Chris Wilson7e37f882016-08-02 22:50:21 +0100101 struct intel_ring *ring = req->ring;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100102 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000103 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100104
Chris Wilson36d527d2011-03-19 22:26:49 +0000105 /*
106 * read/write caches:
107 *
108 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
109 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
110 * also flushed at 2d versus 3d pipeline switches.
111 *
112 * read-only caches:
113 *
114 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
115 * MI_READ_FLUSH is set, and is always flushed on 965.
116 *
117 * I915_GEM_DOMAIN_COMMAND may not exist?
118 *
119 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
120 * invalidated when MI_EXE_FLUSH is set.
121 *
122 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
123 * invalidated with every MI_FLUSH.
124 *
125 * TLBs:
126 *
127 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
128 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
129 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
130 * are flushed at any MI_FLUSH.
131 */
132
Chris Wilsonb5321f32016-08-02 22:50:18 +0100133 cmd = MI_FLUSH;
134 if (invalidate_domains) {
Chris Wilson36d527d2011-03-19 22:26:49 +0000135 cmd |= MI_EXE_FLUSH;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100136 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
137 cmd |= MI_INVALIDATE_ISP;
138 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000139
John Harrison5fb9de12015-05-29 17:44:07 +0100140 ret = intel_ring_begin(req, 2);
Chris Wilson36d527d2011-03-19 22:26:49 +0000141 if (ret)
142 return ret;
143
Chris Wilsonb5321f32016-08-02 22:50:18 +0100144 intel_ring_emit(ring, cmd);
145 intel_ring_emit(ring, MI_NOOP);
146 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000147
148 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800149}
150
Jesse Barnes8d315282011-10-16 10:23:31 +0200151/**
152 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
153 * implementing two workarounds on gen6. From section 1.4.7.1
154 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
155 *
156 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
157 * produced by non-pipelined state commands), software needs to first
158 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
159 * 0.
160 *
161 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
162 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
163 *
164 * And the workaround for these two requires this workaround first:
165 *
166 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
167 * BEFORE the pipe-control with a post-sync op and no write-cache
168 * flushes.
169 *
170 * And this last workaround is tricky because of the requirements on
171 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
172 * volume 2 part 1:
173 *
174 * "1 of the following must also be set:
175 * - Render Target Cache Flush Enable ([12] of DW1)
176 * - Depth Cache Flush Enable ([0] of DW1)
177 * - Stall at Pixel Scoreboard ([1] of DW1)
178 * - Depth Stall ([13] of DW1)
179 * - Post-Sync Operation ([13] of DW1)
180 * - Notify Enable ([8] of DW1)"
181 *
182 * The cache flushes require the workaround flush that triggered this
183 * one, so we can't use it. Depth stall would trigger the same.
184 * Post-sync nonzero is what triggered this second workaround, so we
185 * can't use that one either. Notify enable is IRQs, which aren't
186 * really our business. That leaves only stall at scoreboard.
187 */
188static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100189intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200190{
Chris Wilson7e37f882016-08-02 22:50:21 +0100191 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100192 u32 scratch_addr =
193 req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200194 int ret;
195
John Harrison5fb9de12015-05-29 17:44:07 +0100196 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200197 if (ret)
198 return ret;
199
Chris Wilsonb5321f32016-08-02 22:50:18 +0100200 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
201 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
Jesse Barnes8d315282011-10-16 10:23:31 +0200202 PIPE_CONTROL_STALL_AT_SCOREBOARD);
Chris Wilsonb5321f32016-08-02 22:50:18 +0100203 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
204 intel_ring_emit(ring, 0); /* low dword */
205 intel_ring_emit(ring, 0); /* high dword */
206 intel_ring_emit(ring, MI_NOOP);
207 intel_ring_advance(ring);
Jesse Barnes8d315282011-10-16 10:23:31 +0200208
John Harrison5fb9de12015-05-29 17:44:07 +0100209 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200210 if (ret)
211 return ret;
212
Chris Wilsonb5321f32016-08-02 22:50:18 +0100213 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
214 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
215 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
216 intel_ring_emit(ring, 0);
217 intel_ring_emit(ring, 0);
218 intel_ring_emit(ring, MI_NOOP);
219 intel_ring_advance(ring);
Jesse Barnes8d315282011-10-16 10:23:31 +0200220
221 return 0;
222}
223
224static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100225gen6_render_ring_flush(struct drm_i915_gem_request *req,
226 u32 invalidate_domains, u32 flush_domains)
Jesse Barnes8d315282011-10-16 10:23:31 +0200227{
Chris Wilson7e37f882016-08-02 22:50:21 +0100228 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100229 u32 scratch_addr =
230 req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200231 u32 flags = 0;
Jesse Barnes8d315282011-10-16 10:23:31 +0200232 int ret;
233
Paulo Zanonib3111502012-08-17 18:35:42 -0300234 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100235 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300236 if (ret)
237 return ret;
238
Jesse Barnes8d315282011-10-16 10:23:31 +0200239 /* Just flush everything. Experiments have shown that reducing the
240 * number of bits based on the write domains has little performance
241 * impact.
242 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100243 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 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200250 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100251 }
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 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700262 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100263 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200264
John Harrison5fb9de12015-05-29 17:44:07 +0100265 ret = intel_ring_begin(req, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200266 if (ret)
267 return ret;
268
Chris Wilsonb5321f32016-08-02 22:50:18 +0100269 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
270 intel_ring_emit(ring, flags);
271 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
272 intel_ring_emit(ring, 0);
273 intel_ring_advance(ring);
Jesse Barnes8d315282011-10-16 10:23:31 +0200274
275 return 0;
276}
277
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100278static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100279gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300280{
Chris Wilson7e37f882016-08-02 22:50:21 +0100281 struct intel_ring *ring = req->ring;
Paulo Zanonif3987632012-08-17 18:35:43 -0300282 int ret;
283
John Harrison5fb9de12015-05-29 17:44:07 +0100284 ret = intel_ring_begin(req, 4);
Paulo Zanonif3987632012-08-17 18:35:43 -0300285 if (ret)
286 return ret;
287
Chris Wilsonb5321f32016-08-02 22:50:18 +0100288 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
289 intel_ring_emit(ring,
290 PIPE_CONTROL_CS_STALL |
291 PIPE_CONTROL_STALL_AT_SCOREBOARD);
292 intel_ring_emit(ring, 0);
293 intel_ring_emit(ring, 0);
294 intel_ring_advance(ring);
Paulo Zanonif3987632012-08-17 18:35:43 -0300295
296 return 0;
297}
298
299static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100300gen7_render_ring_flush(struct drm_i915_gem_request *req,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300301 u32 invalidate_domains, u32 flush_domains)
302{
Chris Wilson7e37f882016-08-02 22:50:21 +0100303 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100304 u32 scratch_addr =
305 req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300306 u32 flags = 0;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300307 int ret;
308
Paulo Zanonif3987632012-08-17 18:35:43 -0300309 /*
310 * Ensure that any following seqno writes only happen when the render
311 * cache is indeed flushed.
312 *
313 * Workaround: 4th PIPE_CONTROL command (except the ones with only
314 * read-cache invalidate bits set) must have the CS_STALL bit set. We
315 * don't try to be clever and just set it unconditionally.
316 */
317 flags |= PIPE_CONTROL_CS_STALL;
318
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300319 /* Just flush everything. Experiments have shown that reducing the
320 * number of bits based on the write domains has little performance
321 * impact.
322 */
323 if (flush_domains) {
324 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
325 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800326 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100327 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300328 }
329 if (invalidate_domains) {
330 flags |= PIPE_CONTROL_TLB_INVALIDATE;
331 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
332 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
333 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
334 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
335 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000336 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300337 /*
338 * TLB invalidate requires a post-sync write.
339 */
340 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200341 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300342
Chris Wilsonadd284a2014-12-16 08:44:32 +0000343 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
344
Paulo Zanonif3987632012-08-17 18:35:43 -0300345 /* Workaround: we must issue a pipe_control with CS-stall bit
346 * set before a pipe_control command that has the state cache
347 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100348 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300349 }
350
John Harrison5fb9de12015-05-29 17:44:07 +0100351 ret = intel_ring_begin(req, 4);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300352 if (ret)
353 return ret;
354
Chris Wilsonb5321f32016-08-02 22:50:18 +0100355 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
356 intel_ring_emit(ring, flags);
357 intel_ring_emit(ring, scratch_addr);
358 intel_ring_emit(ring, 0);
359 intel_ring_advance(ring);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300360
361 return 0;
362}
363
Ben Widawskya5f3d682013-11-02 21:07:27 -0700364static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100365gen8_emit_pipe_control(struct drm_i915_gem_request *req,
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300366 u32 flags, u32 scratch_addr)
367{
Chris Wilson7e37f882016-08-02 22:50:21 +0100368 struct intel_ring *ring = req->ring;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300369 int ret;
370
John Harrison5fb9de12015-05-29 17:44:07 +0100371 ret = intel_ring_begin(req, 6);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300372 if (ret)
373 return ret;
374
Chris Wilsonb5321f32016-08-02 22:50:18 +0100375 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
376 intel_ring_emit(ring, flags);
377 intel_ring_emit(ring, scratch_addr);
378 intel_ring_emit(ring, 0);
379 intel_ring_emit(ring, 0);
380 intel_ring_emit(ring, 0);
381 intel_ring_advance(ring);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300382
383 return 0;
384}
385
386static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100387gen8_render_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700388 u32 invalidate_domains, u32 flush_domains)
389{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000390 u32 scratch_addr = req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100391 u32 flags = 0;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800392 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700393
394 flags |= PIPE_CONTROL_CS_STALL;
395
396 if (flush_domains) {
397 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
398 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800399 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100400 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700401 }
402 if (invalidate_domains) {
403 flags |= PIPE_CONTROL_TLB_INVALIDATE;
404 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
405 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
406 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
407 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
408 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
409 flags |= PIPE_CONTROL_QW_WRITE;
410 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800411
412 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100413 ret = gen8_emit_pipe_control(req,
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800414 PIPE_CONTROL_CS_STALL |
415 PIPE_CONTROL_STALL_AT_SCOREBOARD,
416 0);
417 if (ret)
418 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700419 }
420
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100421 return gen8_emit_pipe_control(req, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700422}
423
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000424static void ring_write_tail(struct intel_engine_cs *engine,
Chris Wilson297b0c52010-10-22 17:02:41 +0100425 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800426{
Chris Wilsonc0336662016-05-06 15:40:21 +0100427 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000428 I915_WRITE_TAIL(engine, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800429}
430
Chris Wilson7e37f882016-08-02 22:50:21 +0100431u64 intel_engine_get_active_head(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800432{
Chris Wilsonc0336662016-05-06 15:40:21 +0100433 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson50877442014-03-21 12:41:53 +0000434 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800435
Chris Wilsonc0336662016-05-06 15:40:21 +0100436 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000437 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
438 RING_ACTHD_UDW(engine->mmio_base));
Chris Wilsonc0336662016-05-06 15:40:21 +0100439 else if (INTEL_GEN(dev_priv) >= 4)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000440 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
Chris Wilson50877442014-03-21 12:41:53 +0000441 else
442 acthd = I915_READ(ACTHD);
443
444 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800445}
446
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000447static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200448{
Chris Wilsonc0336662016-05-06 15:40:21 +0100449 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200450 u32 addr;
451
452 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100453 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200454 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
455 I915_WRITE(HWS_PGA, addr);
456}
457
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000458static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000459{
Chris Wilsonc0336662016-05-06 15:40:21 +0100460 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200461 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000462
463 /* The ring status page addresses are no longer next to the rest of
464 * the ring registers as of gen7.
465 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100466 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000467 switch (engine->id) {
Damien Lespiauaf75f262015-02-10 19:32:17 +0000468 case RCS:
469 mmio = RENDER_HWS_PGA_GEN7;
470 break;
471 case BCS:
472 mmio = BLT_HWS_PGA_GEN7;
473 break;
474 /*
475 * VCS2 actually doesn't exist on Gen7. Only shut up
476 * gcc switch check warning
477 */
478 case VCS2:
479 case VCS:
480 mmio = BSD_HWS_PGA_GEN7;
481 break;
482 case VECS:
483 mmio = VEBOX_HWS_PGA_GEN7;
484 break;
485 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100486 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000487 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000488 } else {
489 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000490 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000491 }
492
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000493 I915_WRITE(mmio, (u32)engine->status_page.gfx_addr);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000494 POSTING_READ(mmio);
495
496 /*
497 * Flush the TLB for this page
498 *
499 * FIXME: These two bits have disappeared on gen8, so a question
500 * arises: do we still need this and if so how should we go about
501 * invalidating the TLB?
502 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100503 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000504 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000505
506 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000507 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000508
509 I915_WRITE(reg,
510 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
511 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100512 if (intel_wait_for_register(dev_priv,
513 reg, INSTPM_SYNC_FLUSH, 0,
514 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000515 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000516 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000517 }
518}
519
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000520static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100521{
Chris Wilsonc0336662016-05-06 15:40:21 +0100522 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100523
Chris Wilsonc0336662016-05-06 15:40:21 +0100524 if (!IS_GEN2(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000525 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100526 if (intel_wait_for_register(dev_priv,
527 RING_MI_MODE(engine->mmio_base),
528 MODE_IDLE,
529 MODE_IDLE,
530 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000531 DRM_ERROR("%s : timed out trying to stop ring\n",
532 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100533 /* Sometimes we observe that the idle flag is not
534 * set even though the ring is empty. So double
535 * check before giving up.
536 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000537 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100538 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100539 }
540 }
541
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000542 I915_WRITE_CTL(engine, 0);
543 I915_WRITE_HEAD(engine, 0);
544 engine->write_tail(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100545
Chris Wilsonc0336662016-05-06 15:40:21 +0100546 if (!IS_GEN2(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000547 (void)I915_READ_CTL(engine);
548 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
Chris Wilson9991ae72014-04-02 16:36:07 +0100549 }
550
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000551 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100552}
553
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000554static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800555{
Chris Wilsonc0336662016-05-06 15:40:21 +0100556 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +0100557 struct intel_ring *ring = engine->buffer;
558 struct drm_i915_gem_object *obj = ring->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200559 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800560
Mika Kuoppala59bad942015-01-16 11:34:40 +0200561 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200562
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000563 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100564 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000565 DRM_DEBUG_KMS("%s head not reset to zero "
566 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000567 engine->name,
568 I915_READ_CTL(engine),
569 I915_READ_HEAD(engine),
570 I915_READ_TAIL(engine),
571 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800572
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000573 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000574 DRM_ERROR("failed to set %s head to zero "
575 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000576 engine->name,
577 I915_READ_CTL(engine),
578 I915_READ_HEAD(engine),
579 I915_READ_TAIL(engine),
580 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100581 ret = -EIO;
582 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000583 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700584 }
585
Chris Wilsonc0336662016-05-06 15:40:21 +0100586 if (I915_NEED_GFX_HWS(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000587 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100588 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000589 ring_setup_phys_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100590
Jiri Kosinaece4a172014-08-07 16:29:53 +0200591 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000592 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200593
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200594 /* Initialize the ring. This must happen _after_ we've cleared the ring
595 * registers with the above sequence (the readback of the HEAD registers
596 * also enforces ordering), otherwise the hw might lose the new ring
597 * register values. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000598 I915_WRITE_START(engine, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100599
600 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000601 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100602 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000603 engine->name, I915_READ_HEAD(engine));
604 I915_WRITE_HEAD(engine, 0);
605 (void)I915_READ_HEAD(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100606
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000607 I915_WRITE_CTL(engine,
Chris Wilson7e37f882016-08-02 22:50:21 +0100608 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000609 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800610
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800611 /* If the head is still not zero, the ring is dead */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000612 if (wait_for((I915_READ_CTL(engine) & RING_VALID) != 0 &&
613 I915_READ_START(engine) == i915_gem_obj_ggtt_offset(obj) &&
614 (I915_READ_HEAD(engine) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000615 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100616 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000617 engine->name,
618 I915_READ_CTL(engine),
619 I915_READ_CTL(engine) & RING_VALID,
620 I915_READ_HEAD(engine), I915_READ_TAIL(engine),
621 I915_READ_START(engine),
622 (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200623 ret = -EIO;
624 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800625 }
626
Chris Wilson7e37f882016-08-02 22:50:21 +0100627 ring->last_retired_head = -1;
628 ring->head = I915_READ_HEAD(engine);
629 ring->tail = I915_READ_TAIL(engine) & TAIL_ADDR;
630 intel_ring_update_space(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000631
Tomas Elffc0768c2016-03-21 16:26:59 +0000632 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100633
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200634out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200635 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200636
637 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700638}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800639
Chris Wilsonf8291952016-07-01 17:23:18 +0100640void intel_fini_pipe_control(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100641{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000642 if (engine->scratch.obj == NULL)
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100643 return;
644
Chris Wilsonf8291952016-07-01 17:23:18 +0100645 i915_gem_object_ggtt_unpin(engine->scratch.obj);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100646 i915_gem_object_put(engine->scratch.obj);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000647 engine->scratch.obj = NULL;
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100648}
649
Chris Wilson7d5ea802016-07-01 17:23:20 +0100650int intel_init_pipe_control(struct intel_engine_cs *engine, int size)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000651{
Chris Wilsonf8291952016-07-01 17:23:18 +0100652 struct drm_i915_gem_object *obj;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000653 int ret;
654
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000655 WARN_ON(engine->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000656
Chris Wilson91c8a322016-07-05 10:40:23 +0100657 obj = i915_gem_object_create_stolen(&engine->i915->drm, size);
Chris Wilsonde8fe162016-07-01 17:23:19 +0100658 if (!obj)
Chris Wilson91c8a322016-07-05 10:40:23 +0100659 obj = i915_gem_object_create(&engine->i915->drm, size);
Chris Wilsonf8291952016-07-01 17:23:18 +0100660 if (IS_ERR(obj)) {
661 DRM_ERROR("Failed to allocate scratch page\n");
662 ret = PTR_ERR(obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000663 goto err;
664 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100665
Chris Wilsonf8291952016-07-01 17:23:18 +0100666 ret = i915_gem_obj_ggtt_pin(obj, 4096, PIN_HIGH);
Daniel Vettera9cc7262014-02-14 14:01:13 +0100667 if (ret)
668 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000669
Chris Wilsonf8291952016-07-01 17:23:18 +0100670 engine->scratch.obj = obj;
671 engine->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200672 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000673 engine->name, engine->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000674 return 0;
675
Chris Wilsonc6df5412010-12-15 09:56:50 +0000676err_unref:
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100677 i915_gem_object_put(engine->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000678err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000679 return ret;
680}
681
John Harrisone2be4fa2015-05-29 17:43:54 +0100682static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100683{
Chris Wilson7e37f882016-08-02 22:50:21 +0100684 struct intel_ring *ring = req->ring;
Chris Wilsonc0336662016-05-06 15:40:21 +0100685 struct i915_workarounds *w = &req->i915->workarounds;
686 int ret, i;
Arun Siluvery888b5992014-08-26 14:44:51 +0100687
Francisco Jerez02235802015-10-07 14:44:01 +0300688 if (w->count == 0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300689 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100690
Chris Wilsonb5321f32016-08-02 22:50:18 +0100691 req->engine->gpu_caches_dirty = true;
Chris Wilson7e37f882016-08-02 22:50:21 +0100692 ret = intel_engine_flush_all_caches(req);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100693 if (ret)
694 return ret;
695
John Harrison5fb9de12015-05-29 17:44:07 +0100696 ret = intel_ring_begin(req, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300697 if (ret)
698 return ret;
699
Chris Wilsonb5321f32016-08-02 22:50:18 +0100700 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300701 for (i = 0; i < w->count; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +0100702 intel_ring_emit_reg(ring, w->reg[i].addr);
703 intel_ring_emit(ring, w->reg[i].value);
Mika Kuoppala72253422014-10-07 17:21:26 +0300704 }
Chris Wilsonb5321f32016-08-02 22:50:18 +0100705 intel_ring_emit(ring, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300706
Chris Wilsonb5321f32016-08-02 22:50:18 +0100707 intel_ring_advance(ring);
Mika Kuoppala72253422014-10-07 17:21:26 +0300708
Chris Wilsonb5321f32016-08-02 22:50:18 +0100709 req->engine->gpu_caches_dirty = true;
Chris Wilson7e37f882016-08-02 22:50:21 +0100710 ret = intel_engine_flush_all_caches(req);
Mika Kuoppala72253422014-10-07 17:21:26 +0300711 if (ret)
712 return ret;
713
714 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
715
716 return 0;
717}
718
John Harrison87531812015-05-29 17:43:44 +0100719static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100720{
721 int ret;
722
John Harrisone2be4fa2015-05-29 17:43:54 +0100723 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100724 if (ret != 0)
725 return ret;
726
John Harrisonbe013632015-05-29 17:43:45 +0100727 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100728 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000729 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100730
Chris Wilsone26e1b92016-01-29 16:49:05 +0000731 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100732}
733
Mika Kuoppala72253422014-10-07 17:21:26 +0300734static int wa_add(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200735 i915_reg_t addr,
736 const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300737{
738 const u32 idx = dev_priv->workarounds.count;
739
740 if (WARN_ON(idx >= I915_MAX_WA_REGS))
741 return -ENOSPC;
742
743 dev_priv->workarounds.reg[idx].addr = addr;
744 dev_priv->workarounds.reg[idx].value = val;
745 dev_priv->workarounds.reg[idx].mask = mask;
746
747 dev_priv->workarounds.count++;
748
749 return 0;
750}
751
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100752#define WA_REG(addr, mask, val) do { \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000753 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300754 if (r) \
755 return r; \
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100756 } while (0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300757
758#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000759 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300760
761#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000762 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300763
Damien Lespiau98533252014-12-08 17:33:51 +0000764#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000765 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300766
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000767#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
768#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300769
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000770#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300771
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000772static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
773 i915_reg_t reg)
Arun Siluvery33136b02016-01-21 21:43:47 +0000774{
Chris Wilsonc0336662016-05-06 15:40:21 +0100775 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery33136b02016-01-21 21:43:47 +0000776 struct i915_workarounds *wa = &dev_priv->workarounds;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000777 const uint32_t index = wa->hw_whitelist_count[engine->id];
Arun Siluvery33136b02016-01-21 21:43:47 +0000778
779 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
780 return -EINVAL;
781
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000782 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
Arun Siluvery33136b02016-01-21 21:43:47 +0000783 i915_mmio_reg_offset(reg));
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000784 wa->hw_whitelist_count[engine->id]++;
Arun Siluvery33136b02016-01-21 21:43:47 +0000785
786 return 0;
787}
788
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000789static int gen8_init_workarounds(struct intel_engine_cs *engine)
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100790{
Chris Wilsonc0336662016-05-06 15:40:21 +0100791 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery68c61982015-09-25 17:40:38 +0100792
793 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100794
Arun Siluvery717d84d2015-09-25 17:40:39 +0100795 /* WaDisableAsyncFlipPerfMode:bdw,chv */
796 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
797
Arun Siluveryd0581192015-09-25 17:40:40 +0100798 /* WaDisablePartialInstShootdown:bdw,chv */
799 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
800 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
801
Arun Siluverya340af52015-09-25 17:40:45 +0100802 /* Use Force Non-Coherent whenever executing a 3D context. This is a
803 * workaround for for a possible hang in the unlikely event a TLB
804 * invalidation occurs during a PSD flush.
805 */
806 /* WaForceEnableNonCoherent:bdw,chv */
Arun Siluvery120f5d22015-09-25 17:40:46 +0100807 /* WaHdcDisableFetchWhenMasked:bdw,chv */
Arun Siluverya340af52015-09-25 17:40:45 +0100808 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Arun Siluvery120f5d22015-09-25 17:40:46 +0100809 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Arun Siluverya340af52015-09-25 17:40:45 +0100810 HDC_FORCE_NON_COHERENT);
811
Arun Siluvery6def8fd2015-09-25 17:40:42 +0100812 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
813 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
814 * polygons in the same 8x4 pixel/sample area to be processed without
815 * stalling waiting for the earlier ones to write to Hierarchical Z
816 * buffer."
817 *
818 * This optimization is off by default for BDW and CHV; turn it on.
819 */
820 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
821
Arun Siluvery48404632015-09-25 17:40:43 +0100822 /* Wa4x4STCOptimizationDisable:bdw,chv */
823 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
824
Arun Siluvery7eebcde2015-09-25 17:40:44 +0100825 /*
826 * BSpec recommends 8x4 when MSAA is used,
827 * however in practice 16x4 seems fastest.
828 *
829 * Note that PS/WM thread counts depend on the WIZ hashing
830 * disable bit, which we don't touch here, but it's good
831 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
832 */
833 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
834 GEN6_WIZ_HASHING_MASK,
835 GEN6_WIZ_HASHING_16x4);
836
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100837 return 0;
838}
839
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000840static int bdw_init_workarounds(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +0300841{
Chris Wilsonc0336662016-05-06 15:40:21 +0100842 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100843 int ret;
Mika Kuoppala72253422014-10-07 17:21:26 +0300844
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000845 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100846 if (ret)
847 return ret;
848
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700849 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Arun Siluveryd0581192015-09-25 17:40:40 +0100850 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100851
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700852 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300853 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
854 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100855
Mika Kuoppala72253422014-10-07 17:21:26 +0300856 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
857 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100858
Mika Kuoppala72253422014-10-07 17:21:26 +0300859 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000860 /* WaForceContextSaveRestoreNonCoherent:bdw */
861 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000862 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Chris Wilsonc0336662016-05-06 15:40:21 +0100863 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100864
Arun Siluvery86d7f232014-08-26 14:44:50 +0100865 return 0;
866}
867
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000868static int chv_init_workarounds(struct intel_engine_cs *engine)
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300869{
Chris Wilsonc0336662016-05-06 15:40:21 +0100870 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100871 int ret;
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300872
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000873 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100874 if (ret)
875 return ret;
876
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300877 /* WaDisableThreadStallDopClockGating:chv */
Arun Siluveryd0581192015-09-25 17:40:40 +0100878 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300879
Kenneth Graunked60de812015-01-10 18:02:22 -0800880 /* Improve HiZ throughput on CHV. */
881 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
882
Mika Kuoppala72253422014-10-07 17:21:26 +0300883 return 0;
884}
885
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000886static int gen9_init_workarounds(struct intel_engine_cs *engine)
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000887{
Chris Wilsonc0336662016-05-06 15:40:21 +0100888 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000889 int ret;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000890
Tim Gorea8ab5ed2016-06-13 12:15:01 +0100891 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
892 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
893
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300894 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300895 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
896 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
897
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300898 /* WaDisableKillLogic:bxt,skl,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300899 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
900 ECOCHK_DIS_TLB);
901
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300902 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
903 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000904 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Tim Gore950b2aa2016-03-16 16:13:46 +0000905 FLOW_CONTROL_ENABLE |
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000906 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
907
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300908 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Nick Hoath84241712015-02-05 10:47:20 +0000909 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
910 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
911
Jani Nikulae87a0052015-10-20 15:22:02 +0300912 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100913 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
914 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Damien Lespiaua86eb582015-02-11 18:21:44 +0000915 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
916 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000917
Jani Nikulae87a0052015-10-20 15:22:02 +0300918 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100919 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
920 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Damien Lespiau183c6da2015-02-09 19:33:11 +0000921 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
922 GEN9_RHWO_OPTIMIZATION_DISABLE);
Arun Siluvery9b014352015-07-14 15:01:30 +0100923 /*
924 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
925 * but we do that in per ctx batchbuffer as there is an issue
926 * with this register not getting restored on ctx restore
927 */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000928 }
929
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300930 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl */
931 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
Tim Gorebfd8ad42016-04-19 15:45:52 +0100932 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
933 GEN9_ENABLE_YV12_BUGFIX |
934 GEN9_ENABLE_GPGPU_PREEMPTION);
Nick Hoathcac23df2015-02-05 10:47:22 +0000935
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300936 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
937 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
Arun Siluvery60294682015-09-25 14:33:37 +0100938 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
939 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
Damien Lespiau9370cd92015-02-09 19:33:17 +0000940
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300941 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000942 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
943 GEN9_CCS_TLB_PREFETCH_ENABLE);
944
Imre Deak5a2ae952015-05-19 15:04:59 +0300945 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100946 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_C0) ||
947 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200948 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
949 PIXEL_MASK_CAMMING_DISABLE);
950
Mika Kuoppala5b0e3652016-06-07 17:18:57 +0300951 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
952 WA_SET_BIT_MASKED(HDC_CHICKEN0,
953 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
954 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
Imre Deak8ea6f892015-05-19 17:05:42 +0300955
Mika Kuoppalabbaefe72016-06-07 17:18:58 +0300956 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
957 * both tied to WaForceContextSaveRestoreNonCoherent
958 * in some hsds for skl. We keep the tie for all gen9. The
959 * documentation is a bit hazy and so we want to get common behaviour,
960 * even though there is no clear evidence we would need both on kbl/bxt.
961 * This area has been source of system hangs so we play it safe
962 * and mimic the skl regardless of what bspec says.
963 *
964 * Use Force Non-Coherent whenever executing a 3D context. This
965 * is a workaround for a possible hang in the unlikely event
966 * a TLB invalidation occurs during a PSD flush.
967 */
968
969 /* WaForceEnableNonCoherent:skl,bxt,kbl */
970 WA_SET_BIT_MASKED(HDC_CHICKEN0,
971 HDC_FORCE_NON_COHERENT);
972
973 /* WaDisableHDCInvalidation:skl,bxt,kbl */
974 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
975 BDW_DISABLE_HDC_INVALIDATION);
976
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300977 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
978 if (IS_SKYLAKE(dev_priv) ||
979 IS_KABYLAKE(dev_priv) ||
980 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
Arun Siluvery8c761602015-09-08 10:31:48 +0100981 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
982 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery8c761602015-09-08 10:31:48 +0100983
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300984 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
Robert Beckett6b6d5622015-09-08 10:31:52 +0100985 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
986
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300987 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
Arun Siluvery6ecf56a2016-01-21 21:43:54 +0000988 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
989 GEN8_LQSC_FLUSH_COHERENT_LINES));
990
arun.siluvery@linux.intel.com6bb628552016-06-06 09:52:49 +0100991 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
992 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
993 if (ret)
994 return ret;
995
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300996 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000997 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000998 if (ret)
999 return ret;
1000
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001001 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001002 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
Arun Siluvery3669ab62016-01-21 21:43:49 +00001003 if (ret)
1004 return ret;
1005
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001006 return 0;
1007}
1008
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001009static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +00001010{
Chris Wilsonc0336662016-05-06 15:40:21 +01001011 struct drm_i915_private *dev_priv = engine->i915;
Damien Lespiaub7668792015-02-14 18:30:29 +00001012 u8 vals[3] = { 0, 0, 0 };
1013 unsigned int i;
1014
1015 for (i = 0; i < 3; i++) {
1016 u8 ss;
1017
1018 /*
1019 * Only consider slices where one, and only one, subslice has 7
1020 * EUs
1021 */
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +08001022 if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
Damien Lespiaub7668792015-02-14 18:30:29 +00001023 continue;
1024
1025 /*
1026 * subslice_7eu[i] != 0 (because of the check above) and
1027 * ss_max == 4 (maximum number of subslices possible per slice)
1028 *
1029 * -> 0 <= ss <= 3;
1030 */
1031 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1032 vals[i] = 3 - ss;
1033 }
1034
1035 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1036 return 0;
1037
1038 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1039 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1040 GEN9_IZ_HASHING_MASK(2) |
1041 GEN9_IZ_HASHING_MASK(1) |
1042 GEN9_IZ_HASHING_MASK(0),
1043 GEN9_IZ_HASHING(2, vals[2]) |
1044 GEN9_IZ_HASHING(1, vals[1]) |
1045 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001046
Mika Kuoppala72253422014-10-07 17:21:26 +03001047 return 0;
1048}
1049
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001050static int skl_init_workarounds(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +00001051{
Chris Wilsonc0336662016-05-06 15:40:21 +01001052 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001053 int ret;
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001054
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001055 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001056 if (ret)
1057 return ret;
Damien Lespiau8d205492015-02-09 19:33:15 +00001058
Arun Siluverya78536e2016-01-21 21:43:53 +00001059 /*
1060 * Actual WA is to disable percontext preemption granularity control
1061 * until D0 which is the default case so this is equivalent to
1062 * !WaDisablePerCtxtPreemptionGranularityControl:skl
1063 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001064 if (IS_SKL_REVID(dev_priv, SKL_REVID_E0, REVID_FOREVER)) {
Arun Siluverya78536e2016-01-21 21:43:53 +00001065 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1066 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1067 }
1068
Mika Kuoppala71dce582016-06-07 17:19:14 +03001069 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001070 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1071 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1072 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1073 }
1074
1075 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1076 * involving this register should also be added to WA batch as required.
1077 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001078 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001079 /* WaDisableLSQCROPERFforOCL:skl */
1080 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1081 GEN8_LQSC_RO_PERF_DIS);
1082
1083 /* WaEnableGapsTsvCreditFix:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001084 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, REVID_FOREVER)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001085 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1086 GEN9_GAPS_TSV_CREDIT_DISABLE));
1087 }
1088
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001089 /* WaDisablePowerCompilerClockGating:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001090 if (IS_SKL_REVID(dev_priv, SKL_REVID_B0, SKL_REVID_B0))
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001091 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1092 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1093
Jani Nikulae87a0052015-10-20 15:22:02 +03001094 /* WaBarrierPerformanceFixDisable:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001095 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_D0))
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001096 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1097 HDC_FENCE_DEST_SLM_DISABLE |
1098 HDC_BARRIER_PERFORMANCE_DISABLE);
1099
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001100 /* WaDisableSbeCacheDispatchPortSharing:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001101 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0))
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001102 WA_SET_BIT_MASKED(
1103 GEN7_HALF_SLICE_CHICKEN1,
1104 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001105
Mika Kuoppalaeee8efb2016-06-07 17:18:53 +03001106 /* WaDisableGafsUnitClkGating:skl */
1107 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1108
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +03001109 /* WaInPlaceDecompressionHang:skl */
1110 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
1111 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1112 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1113
Arun Siluvery61074972016-01-21 21:43:52 +00001114 /* WaDisableLSQCROPERFforOCL:skl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001115 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluvery61074972016-01-21 21:43:52 +00001116 if (ret)
1117 return ret;
1118
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001119 return skl_tune_iz_hashing(engine);
Damien Lespiau8d205492015-02-09 19:33:15 +00001120}
1121
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001122static int bxt_init_workarounds(struct intel_engine_cs *engine)
Nick Hoathcae04372015-03-17 11:39:38 +02001123{
Chris Wilsonc0336662016-05-06 15:40:21 +01001124 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001125 int ret;
Nick Hoathdfb601e2015-04-10 13:12:24 +01001126
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001127 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001128 if (ret)
1129 return ret;
Nick Hoathcae04372015-03-17 11:39:38 +02001130
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001131 /* WaStoreMultiplePTEenable:bxt */
1132 /* This is a requirement according to Hardware specification */
Chris Wilsonc0336662016-05-06 15:40:21 +01001133 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001134 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1135
1136 /* WaSetClckGatingDisableMedia:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001137 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001138 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1139 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1140 }
1141
Nick Hoathdfb601e2015-04-10 13:12:24 +01001142 /* WaDisableThreadStallDopClockGating:bxt */
1143 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1144 STALL_DOP_GATING_DISABLE);
1145
arun.siluvery@linux.intel.com780f0ae2016-06-03 11:16:10 +01001146 /* WaDisablePooledEuLoadBalancingFix:bxt */
1147 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1148 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1149 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1150 }
1151
Nick Hoath983b4b92015-04-10 13:12:25 +01001152 /* WaDisableSbeCacheDispatchPortSharing:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001153 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
Nick Hoath983b4b92015-04-10 13:12:25 +01001154 WA_SET_BIT_MASKED(
1155 GEN7_HALF_SLICE_CHICKEN1,
1156 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1157 }
1158
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001159 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1160 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1161 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
Arun Siluverya786d532016-01-21 21:43:51 +00001162 /* WaDisableLSQCROPERFforOCL:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001163 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001164 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001165 if (ret)
1166 return ret;
Arun Siluverya786d532016-01-21 21:43:51 +00001167
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001168 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluverya786d532016-01-21 21:43:51 +00001169 if (ret)
1170 return ret;
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001171 }
1172
Tim Gore050fc462016-04-22 09:46:01 +01001173 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001174 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
Imre Deak36579cb2016-05-03 15:54:20 +03001175 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1176 L3_HIGH_PRIO_CREDITS(2));
Tim Gore050fc462016-04-22 09:46:01 +01001177
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001178 /* WaInsertDummyPushConstPs:bxt */
1179 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
1180 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1181 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1182
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +03001183 /* WaInPlaceDecompressionHang:bxt */
1184 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
1185 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1186 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1187
Nick Hoathcae04372015-03-17 11:39:38 +02001188 return 0;
1189}
1190
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001191static int kbl_init_workarounds(struct intel_engine_cs *engine)
1192{
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001193 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001194 int ret;
1195
1196 ret = gen9_init_workarounds(engine);
1197 if (ret)
1198 return ret;
1199
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001200 /* WaEnableGapsTsvCreditFix:kbl */
1201 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1202 GEN9_GAPS_TSV_CREDIT_DISABLE));
1203
Mika Kuoppalac0b730d2016-06-07 17:19:06 +03001204 /* WaDisableDynamicCreditSharing:kbl */
1205 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1206 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1207 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1208
Mika Kuoppala8401d422016-06-07 17:19:00 +03001209 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1210 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1211 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1212 HDC_FENCE_DEST_SLM_DISABLE);
1213
Mika Kuoppalafe905812016-06-07 17:19:03 +03001214 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1215 * involving this register should also be added to WA batch as required.
1216 */
1217 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1218 /* WaDisableLSQCROPERFforOCL:kbl */
1219 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1220 GEN8_LQSC_RO_PERF_DIS);
1221
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001222 /* WaInsertDummyPushConstPs:kbl */
1223 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1224 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1225 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1226
Mika Kuoppala4de5d7c2016-06-07 17:19:11 +03001227 /* WaDisableGafsUnitClkGating:kbl */
1228 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1229
Mika Kuoppala954337a2016-06-07 17:19:12 +03001230 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1231 WA_SET_BIT_MASKED(
1232 GEN7_HALF_SLICE_CHICKEN1,
1233 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1234
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +03001235 /* WaInPlaceDecompressionHang:kbl */
1236 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1237 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1238
Mika Kuoppalafe905812016-06-07 17:19:03 +03001239 /* WaDisableLSQCROPERFforOCL:kbl */
1240 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1241 if (ret)
1242 return ret;
1243
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001244 return 0;
1245}
1246
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001247int init_workarounds_ring(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +03001248{
Chris Wilsonc0336662016-05-06 15:40:21 +01001249 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala72253422014-10-07 17:21:26 +03001250
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001251 WARN_ON(engine->id != RCS);
Mika Kuoppala72253422014-10-07 17:21:26 +03001252
1253 dev_priv->workarounds.count = 0;
Arun Siluvery33136b02016-01-21 21:43:47 +00001254 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
Mika Kuoppala72253422014-10-07 17:21:26 +03001255
Chris Wilsonc0336662016-05-06 15:40:21 +01001256 if (IS_BROADWELL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001257 return bdw_init_workarounds(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +03001258
Chris Wilsonc0336662016-05-06 15:40:21 +01001259 if (IS_CHERRYVIEW(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001260 return chv_init_workarounds(engine);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001261
Chris Wilsonc0336662016-05-06 15:40:21 +01001262 if (IS_SKYLAKE(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001263 return skl_init_workarounds(engine);
Nick Hoathcae04372015-03-17 11:39:38 +02001264
Chris Wilsonc0336662016-05-06 15:40:21 +01001265 if (IS_BROXTON(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001266 return bxt_init_workarounds(engine);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001267
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001268 if (IS_KABYLAKE(dev_priv))
1269 return kbl_init_workarounds(engine);
1270
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001271 return 0;
1272}
1273
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001274static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001275{
Chris Wilsonc0336662016-05-06 15:40:21 +01001276 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001277 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001278 if (ret)
1279 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001280
Akash Goel61a563a2014-03-25 18:01:50 +05301281 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001282 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001283 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001284
1285 /* We need to disable the AsyncFlip performance optimisations in order
1286 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1287 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001288 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001289 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001290 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001291 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001292 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1293
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001294 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301295 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +01001296 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001297 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001298 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001299
Akash Goel01fa0302014-03-24 23:00:04 +05301300 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +01001301 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001302 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301303 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001304 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001305
Chris Wilsonc0336662016-05-06 15:40:21 +01001306 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001307 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1308 * "If this bit is set, STCunit will have LRA as replacement
1309 * policy. [...] This bit must be reset. LRA replacement
1310 * policy is not supported."
1311 */
1312 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001313 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001314 }
1315
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001316 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001317 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001318
Ville Syrjälä035ea402016-07-12 19:24:47 +03001319 if (INTEL_INFO(dev_priv)->gen >= 6)
1320 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -07001321
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001322 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001323}
1324
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001325static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001326{
Chris Wilsonc0336662016-05-06 15:40:21 +01001327 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001328
1329 if (dev_priv->semaphore_obj) {
1330 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001331 i915_gem_object_put(dev_priv->semaphore_obj);
Ben Widawsky3e789982014-06-30 09:53:37 -07001332 dev_priv->semaphore_obj = NULL;
1333 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001334
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001335 intel_fini_pipe_control(engine);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001336}
1337
John Harrisonf7169682015-05-29 17:44:05 +01001338static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001339 unsigned int num_dwords)
1340{
1341#define MBOX_UPDATE_DWORDS 8
Chris Wilson7e37f882016-08-02 22:50:21 +01001342 struct intel_ring *signaller = signaller_req->ring;
Chris Wilsonc0336662016-05-06 15:40:21 +01001343 struct drm_i915_private *dev_priv = signaller_req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001344 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001345 enum intel_engine_id id;
1346 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001347
Chris Wilsonc0336662016-05-06 15:40:21 +01001348 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawsky3e789982014-06-30 09:53:37 -07001349 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1350#undef MBOX_UPDATE_DWORDS
1351
John Harrison5fb9de12015-05-29 17:44:07 +01001352 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001353 if (ret)
1354 return ret;
1355
Dave Gordonc3232b12016-03-23 18:19:53 +00001356 for_each_engine_id(waiter, dev_priv, id) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001357 u64 gtt_offset =
1358 signaller_req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001359 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1360 continue;
1361
1362 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
Chris Wilsonb5321f32016-08-02 22:50:18 +01001363 intel_ring_emit(signaller,
1364 PIPE_CONTROL_GLOBAL_GTT_IVB |
1365 PIPE_CONTROL_QW_WRITE |
1366 PIPE_CONTROL_CS_STALL);
Ben Widawsky3e789982014-06-30 09:53:37 -07001367 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1368 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
Chris Wilson04769652016-07-20 09:21:11 +01001369 intel_ring_emit(signaller, signaller_req->fence.seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001370 intel_ring_emit(signaller, 0);
Chris Wilsonb5321f32016-08-02 22:50:18 +01001371 intel_ring_emit(signaller,
1372 MI_SEMAPHORE_SIGNAL |
1373 MI_SEMAPHORE_TARGET(waiter->hw_id));
Ben Widawsky3e789982014-06-30 09:53:37 -07001374 intel_ring_emit(signaller, 0);
1375 }
1376
1377 return 0;
1378}
1379
John Harrisonf7169682015-05-29 17:44:05 +01001380static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001381 unsigned int num_dwords)
1382{
1383#define MBOX_UPDATE_DWORDS 6
Chris Wilson7e37f882016-08-02 22:50:21 +01001384 struct intel_ring *signaller = signaller_req->ring;
Chris Wilsonc0336662016-05-06 15:40:21 +01001385 struct drm_i915_private *dev_priv = signaller_req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001386 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001387 enum intel_engine_id id;
1388 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001389
Chris Wilsonc0336662016-05-06 15:40:21 +01001390 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawsky3e789982014-06-30 09:53:37 -07001391 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1392#undef MBOX_UPDATE_DWORDS
1393
John Harrison5fb9de12015-05-29 17:44:07 +01001394 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001395 if (ret)
1396 return ret;
1397
Dave Gordonc3232b12016-03-23 18:19:53 +00001398 for_each_engine_id(waiter, dev_priv, id) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001399 u64 gtt_offset =
1400 signaller_req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001401 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1402 continue;
1403
Chris Wilsonb5321f32016-08-02 22:50:18 +01001404 intel_ring_emit(signaller,
1405 (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW);
1406 intel_ring_emit(signaller,
1407 lower_32_bits(gtt_offset) |
1408 MI_FLUSH_DW_USE_GTT);
Ben Widawsky3e789982014-06-30 09:53:37 -07001409 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
Chris Wilson04769652016-07-20 09:21:11 +01001410 intel_ring_emit(signaller, signaller_req->fence.seqno);
Chris Wilsonb5321f32016-08-02 22:50:18 +01001411 intel_ring_emit(signaller,
1412 MI_SEMAPHORE_SIGNAL |
1413 MI_SEMAPHORE_TARGET(waiter->hw_id));
Ben Widawsky3e789982014-06-30 09:53:37 -07001414 intel_ring_emit(signaller, 0);
1415 }
1416
1417 return 0;
1418}
1419
John Harrisonf7169682015-05-29 17:44:05 +01001420static int gen6_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001421 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001422{
Chris Wilson7e37f882016-08-02 22:50:21 +01001423 struct intel_ring *signaller = signaller_req->ring;
Chris Wilsonc0336662016-05-06 15:40:21 +01001424 struct drm_i915_private *dev_priv = signaller_req->i915;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001425 struct intel_engine_cs *useless;
Dave Gordonc3232b12016-03-23 18:19:53 +00001426 enum intel_engine_id id;
1427 int ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001428
Ben Widawskya1444b72014-06-30 09:53:35 -07001429#define MBOX_UPDATE_DWORDS 3
Chris Wilsonc0336662016-05-06 15:40:21 +01001430 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawskya1444b72014-06-30 09:53:35 -07001431 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1432#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001433
John Harrison5fb9de12015-05-29 17:44:07 +01001434 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky024a43e2014-04-29 14:52:30 -07001435 if (ret)
1436 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001437
Dave Gordonc3232b12016-03-23 18:19:53 +00001438 for_each_engine_id(useless, dev_priv, id) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01001439 i915_reg_t mbox_reg =
1440 signaller_req->engine->semaphore.mbox.signal[id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001441
1442 if (i915_mmio_reg_valid(mbox_reg)) {
Ben Widawsky78325f22014-04-29 14:52:29 -07001443 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001444 intel_ring_emit_reg(signaller, mbox_reg);
Chris Wilson04769652016-07-20 09:21:11 +01001445 intel_ring_emit(signaller, signaller_req->fence.seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001446 }
1447 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001448
Ben Widawskya1444b72014-06-30 09:53:35 -07001449 /* If num_dwords was rounded, make sure the tail pointer is correct */
1450 if (num_rings % 2 == 0)
1451 intel_ring_emit(signaller, MI_NOOP);
1452
Ben Widawsky024a43e2014-04-29 14:52:30 -07001453 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001454}
1455
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001456/**
1457 * gen6_add_request - Update the semaphore mailbox registers
John Harrisonee044a82015-05-29 17:44:00 +01001458 *
1459 * @request - request to write to the ring
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001460 *
1461 * Update the mailbox registers in the *other* rings with the current seqno.
1462 * This acts like a signal in the canonical semaphore.
1463 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001464static int
John Harrisonee044a82015-05-29 17:44:00 +01001465gen6_add_request(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001466{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001467 struct intel_engine_cs *engine = req->engine;
Chris Wilson7e37f882016-08-02 22:50:21 +01001468 struct intel_ring *ring = req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001469 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001470
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001471 if (engine->semaphore.signal)
1472 ret = engine->semaphore.signal(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001473 else
John Harrison5fb9de12015-05-29 17:44:07 +01001474 ret = intel_ring_begin(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001475
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001476 if (ret)
1477 return ret;
1478
Chris Wilsonb5321f32016-08-02 22:50:18 +01001479 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1480 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1481 intel_ring_emit(ring, req->fence.seqno);
1482 intel_ring_emit(ring, MI_USER_INTERRUPT);
1483 __intel_engine_submit(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001484
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001485 return 0;
1486}
1487
Chris Wilsona58c01a2016-04-29 13:18:21 +01001488static int
1489gen8_render_add_request(struct drm_i915_gem_request *req)
1490{
1491 struct intel_engine_cs *engine = req->engine;
Chris Wilson7e37f882016-08-02 22:50:21 +01001492 struct intel_ring *ring = req->ring;
Chris Wilsona58c01a2016-04-29 13:18:21 +01001493 int ret;
1494
1495 if (engine->semaphore.signal)
1496 ret = engine->semaphore.signal(req, 8);
1497 else
1498 ret = intel_ring_begin(req, 8);
1499 if (ret)
1500 return ret;
1501
Chris Wilsonb5321f32016-08-02 22:50:18 +01001502 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
1503 intel_ring_emit(ring, (PIPE_CONTROL_GLOBAL_GTT_IVB |
1504 PIPE_CONTROL_CS_STALL |
1505 PIPE_CONTROL_QW_WRITE));
1506 intel_ring_emit(ring, intel_hws_seqno_address(engine));
1507 intel_ring_emit(ring, 0);
1508 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsona58c01a2016-04-29 13:18:21 +01001509 /* We're thrashing one dword of HWS. */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001510 intel_ring_emit(ring, 0);
1511 intel_ring_emit(ring, MI_USER_INTERRUPT);
1512 intel_ring_emit(ring, MI_NOOP);
1513 __intel_engine_submit(engine);
Chris Wilsona58c01a2016-04-29 13:18:21 +01001514
1515 return 0;
1516}
1517
Chris Wilsonc0336662016-05-06 15:40:21 +01001518static inline bool i915_gem_has_seqno_wrapped(struct drm_i915_private *dev_priv,
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001519 u32 seqno)
1520{
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001521 return dev_priv->last_seqno < seqno;
1522}
1523
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001524/**
1525 * intel_ring_sync - sync the waiter to the signaller on seqno
1526 *
1527 * @waiter - ring that is waiting
1528 * @signaller - ring which has, or will signal
1529 * @seqno - seqno which the waiter will block on
1530 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001531
1532static int
John Harrison599d9242015-05-29 17:44:04 +01001533gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001534 struct intel_engine_cs *signaller,
1535 u32 seqno)
1536{
Chris Wilson7e37f882016-08-02 22:50:21 +01001537 struct intel_ring *waiter = waiter_req->ring;
Chris Wilsonc0336662016-05-06 15:40:21 +01001538 struct drm_i915_private *dev_priv = waiter_req->i915;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001539 u64 offset = GEN8_WAIT_OFFSET(waiter_req->engine, signaller->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001540 struct i915_hw_ppgtt *ppgtt;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001541 int ret;
1542
John Harrison5fb9de12015-05-29 17:44:07 +01001543 ret = intel_ring_begin(waiter_req, 4);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001544 if (ret)
1545 return ret;
1546
1547 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1548 MI_SEMAPHORE_GLOBAL_GTT |
1549 MI_SEMAPHORE_SAD_GTE_SDD);
1550 intel_ring_emit(waiter, seqno);
Tvrtko Ursulinc38c6512016-06-29 16:09:30 +01001551 intel_ring_emit(waiter, lower_32_bits(offset));
1552 intel_ring_emit(waiter, upper_32_bits(offset));
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001553 intel_ring_advance(waiter);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001554
1555 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1556 * pagetables and we must reload them before executing the batch.
1557 * We do this on the i915_switch_context() following the wait and
1558 * before the dispatch.
1559 */
1560 ppgtt = waiter_req->ctx->ppgtt;
1561 if (ppgtt && waiter_req->engine->id != RCS)
1562 ppgtt->pd_dirty_rings |= intel_engine_flag(waiter_req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001563 return 0;
1564}
1565
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001566static int
John Harrison599d9242015-05-29 17:44:04 +01001567gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001568 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001569 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001570{
Chris Wilson7e37f882016-08-02 22:50:21 +01001571 struct intel_ring *waiter = waiter_req->ring;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001572 u32 dw1 = MI_SEMAPHORE_MBOX |
1573 MI_SEMAPHORE_COMPARE |
1574 MI_SEMAPHORE_REGISTER;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001575 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter_req->engine->id];
Ben Widawskyebc348b2014-04-29 14:52:28 -07001576 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001577
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001578 /* Throughout all of the GEM code, seqno passed implies our current
1579 * seqno is >= the last seqno executed. However for hardware the
1580 * comparison is strictly greater than.
1581 */
1582 seqno -= 1;
1583
Ben Widawskyebc348b2014-04-29 14:52:28 -07001584 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001585
John Harrison5fb9de12015-05-29 17:44:07 +01001586 ret = intel_ring_begin(waiter_req, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001587 if (ret)
1588 return ret;
1589
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001590 /* If seqno wrap happened, omit the wait with no-ops */
Chris Wilsonc0336662016-05-06 15:40:21 +01001591 if (likely(!i915_gem_has_seqno_wrapped(waiter_req->i915, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001592 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001593 intel_ring_emit(waiter, seqno);
1594 intel_ring_emit(waiter, 0);
1595 intel_ring_emit(waiter, MI_NOOP);
1596 } else {
1597 intel_ring_emit(waiter, MI_NOOP);
1598 intel_ring_emit(waiter, MI_NOOP);
1599 intel_ring_emit(waiter, MI_NOOP);
1600 intel_ring_emit(waiter, MI_NOOP);
1601 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001602 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001603
1604 return 0;
1605}
1606
Chris Wilsonf8973c22016-07-01 17:23:21 +01001607static void
Dave Gordon38a0f2d2016-07-20 18:16:06 +01001608gen5_seqno_barrier(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001609{
Chris Wilsonf8973c22016-07-01 17:23:21 +01001610 /* MI_STORE are internally buffered by the GPU and not flushed
1611 * either by MI_FLUSH or SyncFlush or any other combination of
1612 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001613 *
Chris Wilsonf8973c22016-07-01 17:23:21 +01001614 * "Only the submission of the store operation is guaranteed.
1615 * The write result will be complete (coherent) some time later
1616 * (this is practically a finite period but there is no guaranteed
1617 * latency)."
1618 *
1619 * Empirically, we observe that we need a delay of at least 75us to
1620 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001621 */
Chris Wilsonf8973c22016-07-01 17:23:21 +01001622 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001623}
1624
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001625static void
1626gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001627{
Chris Wilsonc0336662016-05-06 15:40:21 +01001628 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001629
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001630 /* Workaround to force correct ordering between irq and seqno writes on
1631 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +01001632 * ACTHD) before reading the status page.
1633 *
1634 * Note that this effectively stalls the read by the time it takes to
1635 * do a memory transaction, which more or less ensures that the write
1636 * from the GPU has sufficient time to invalidate the CPU cacheline.
1637 * Alternatively we could delay the interrupt from the CS ring to give
1638 * the write time to land, but that would incur a delay after every
1639 * batch i.e. much more frequent than a delay when waiting for the
1640 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001641 *
1642 * Also note that to prevent whole machine hangs on gen7, we have to
1643 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +01001644 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001645 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001646 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001647 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001648}
1649
Chris Wilson31bb59c2016-07-01 17:23:27 +01001650static void
1651gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001652{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001653 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +02001654}
1655
1656static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001657gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001658{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001659 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001660}
1661
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001662static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001663i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001664{
Chris Wilsonc0336662016-05-06 15:40:21 +01001665 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001666
Chris Wilson31bb59c2016-07-01 17:23:27 +01001667 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1668 I915_WRITE(IMR, dev_priv->irq_mask);
1669 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +01001670}
1671
1672static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001673i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001674{
Chris Wilsonc0336662016-05-06 15:40:21 +01001675 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001676
Chris Wilson31bb59c2016-07-01 17:23:27 +01001677 dev_priv->irq_mask |= engine->irq_enable_mask;
1678 I915_WRITE(IMR, dev_priv->irq_mask);
1679}
1680
1681static void
1682i8xx_irq_enable(struct intel_engine_cs *engine)
1683{
1684 struct drm_i915_private *dev_priv = engine->i915;
1685
1686 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1687 I915_WRITE16(IMR, dev_priv->irq_mask);
1688 POSTING_READ16(RING_IMR(engine->mmio_base));
1689}
1690
1691static void
1692i8xx_irq_disable(struct intel_engine_cs *engine)
1693{
1694 struct drm_i915_private *dev_priv = engine->i915;
1695
1696 dev_priv->irq_mask |= engine->irq_enable_mask;
1697 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001698}
1699
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001700static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001701bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001702 u32 invalidate_domains,
1703 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001704{
Chris Wilson7e37f882016-08-02 22:50:21 +01001705 struct intel_ring *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001706 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001707
John Harrison5fb9de12015-05-29 17:44:07 +01001708 ret = intel_ring_begin(req, 2);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001709 if (ret)
1710 return ret;
1711
Chris Wilsonb5321f32016-08-02 22:50:18 +01001712 intel_ring_emit(ring, MI_FLUSH);
1713 intel_ring_emit(ring, MI_NOOP);
1714 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001715 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001716}
1717
Chris Wilson3cce4692010-10-27 16:11:02 +01001718static int
John Harrisonee044a82015-05-29 17:44:00 +01001719i9xx_add_request(struct drm_i915_gem_request *req)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001720{
Chris Wilson7e37f882016-08-02 22:50:21 +01001721 struct intel_ring *ring = req->ring;
Chris Wilson3cce4692010-10-27 16:11:02 +01001722 int ret;
1723
John Harrison5fb9de12015-05-29 17:44:07 +01001724 ret = intel_ring_begin(req, 4);
Chris Wilson3cce4692010-10-27 16:11:02 +01001725 if (ret)
1726 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001727
Chris Wilsonb5321f32016-08-02 22:50:18 +01001728 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1729 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1730 intel_ring_emit(ring, req->fence.seqno);
1731 intel_ring_emit(ring, MI_USER_INTERRUPT);
1732 __intel_engine_submit(req->engine);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001733
Chris Wilson3cce4692010-10-27 16:11:02 +01001734 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001735}
1736
Chris Wilson0f468322011-01-04 17:35:21 +00001737static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001738gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001739{
Chris Wilsonc0336662016-05-06 15:40:21 +01001740 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001741
Chris Wilson61ff75a2016-07-01 17:23:28 +01001742 I915_WRITE_IMR(engine,
1743 ~(engine->irq_enable_mask |
1744 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001745 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001746}
1747
1748static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001749gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001750{
Chris Wilsonc0336662016-05-06 15:40:21 +01001751 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001752
Chris Wilson61ff75a2016-07-01 17:23:28 +01001753 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001754 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001755}
1756
1757static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001758hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001759{
Chris Wilsonc0336662016-05-06 15:40:21 +01001760 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001761
Chris Wilson31bb59c2016-07-01 17:23:27 +01001762 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
1763 gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
1764}
1765
1766static void
1767hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1768{
1769 struct drm_i915_private *dev_priv = engine->i915;
1770
1771 I915_WRITE_IMR(engine, ~0);
1772 gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
1773}
1774
1775static void
1776gen8_irq_enable(struct intel_engine_cs *engine)
1777{
1778 struct drm_i915_private *dev_priv = engine->i915;
1779
Chris Wilson61ff75a2016-07-01 17:23:28 +01001780 I915_WRITE_IMR(engine,
1781 ~(engine->irq_enable_mask |
1782 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001783 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1784}
1785
1786static void
1787gen8_irq_disable(struct intel_engine_cs *engine)
1788{
1789 struct drm_i915_private *dev_priv = engine->i915;
1790
Chris Wilson61ff75a2016-07-01 17:23:28 +01001791 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001792}
1793
Zou Nan haid1b851f2010-05-21 09:08:57 +08001794static int
John Harrison53fddaf2015-05-29 17:44:02 +01001795i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001796 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001797 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001798{
Chris Wilson7e37f882016-08-02 22:50:21 +01001799 struct intel_ring *ring = req->ring;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001800 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001801
John Harrison5fb9de12015-05-29 17:44:07 +01001802 ret = intel_ring_begin(req, 2);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001803 if (ret)
1804 return ret;
1805
Chris Wilsonb5321f32016-08-02 22:50:18 +01001806 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001807 MI_BATCH_BUFFER_START |
1808 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001809 (dispatch_flags & I915_DISPATCH_SECURE ?
1810 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonb5321f32016-08-02 22:50:18 +01001811 intel_ring_emit(ring, offset);
1812 intel_ring_advance(ring);
Chris Wilson78501ea2010-10-27 12:18:21 +01001813
Zou Nan haid1b851f2010-05-21 09:08:57 +08001814 return 0;
1815}
1816
Daniel Vetterb45305f2012-12-17 16:21:27 +01001817/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1818#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001819#define I830_TLB_ENTRIES (2)
1820#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001821static int
John Harrison53fddaf2015-05-29 17:44:02 +01001822i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001823 u64 offset, u32 len,
1824 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001825{
Chris Wilson7e37f882016-08-02 22:50:21 +01001826 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01001827 u32 cs_offset = req->engine->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001828 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001829
John Harrison5fb9de12015-05-29 17:44:07 +01001830 ret = intel_ring_begin(req, 6);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001831 if (ret)
1832 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001833
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001834 /* Evict the invalid PTE TLBs */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001835 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1836 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1837 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1838 intel_ring_emit(ring, cs_offset);
1839 intel_ring_emit(ring, 0xdeadbeef);
1840 intel_ring_emit(ring, MI_NOOP);
1841 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001842
John Harrison8e004ef2015-02-13 11:48:10 +00001843 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001844 if (len > I830_BATCH_LIMIT)
1845 return -ENOSPC;
1846
John Harrison5fb9de12015-05-29 17:44:07 +01001847 ret = intel_ring_begin(req, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001848 if (ret)
1849 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001850
1851 /* Blit the batch (which has now all relocs applied) to the
1852 * stable batch scratch bo area (so that the CS never
1853 * stumbles over its tlb invalidation bug) ...
1854 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001855 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1856 intel_ring_emit(ring,
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001857 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilsonb5321f32016-08-02 22:50:18 +01001858 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1859 intel_ring_emit(ring, cs_offset);
1860 intel_ring_emit(ring, 4096);
1861 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001862
Chris Wilsonb5321f32016-08-02 22:50:18 +01001863 intel_ring_emit(ring, MI_FLUSH);
1864 intel_ring_emit(ring, MI_NOOP);
1865 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001866
1867 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001868 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001869 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001870
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001871 ret = intel_ring_begin(req, 2);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001872 if (ret)
1873 return ret;
1874
Chris Wilsonb5321f32016-08-02 22:50:18 +01001875 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1876 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1877 0 : MI_BATCH_NON_SECURE));
1878 intel_ring_advance(ring);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001879
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001880 return 0;
1881}
1882
1883static int
John Harrison53fddaf2015-05-29 17:44:02 +01001884i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001885 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001886 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001887{
Chris Wilson7e37f882016-08-02 22:50:21 +01001888 struct intel_ring *ring = req->ring;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001889 int ret;
1890
John Harrison5fb9de12015-05-29 17:44:07 +01001891 ret = intel_ring_begin(req, 2);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001892 if (ret)
1893 return ret;
1894
Chris Wilsonb5321f32016-08-02 22:50:18 +01001895 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1896 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1897 0 : MI_BATCH_NON_SECURE));
1898 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001899
Eric Anholt62fdfea2010-05-21 13:26:39 -07001900 return 0;
1901}
1902
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001903static void cleanup_phys_status_page(struct intel_engine_cs *engine)
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001904{
Chris Wilsonc0336662016-05-06 15:40:21 +01001905 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001906
1907 if (!dev_priv->status_page_dmah)
1908 return;
1909
Chris Wilson91c8a322016-07-05 10:40:23 +01001910 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001911 engine->status_page.page_addr = NULL;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001912}
1913
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001914static void cleanup_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001915{
Chris Wilson05394f32010-11-08 19:18:58 +00001916 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001917
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001918 obj = engine->status_page.obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001919 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001920 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001921
Chris Wilson9da3da62012-06-01 15:20:22 +01001922 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001923 i915_gem_object_ggtt_unpin(obj);
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001924 i915_gem_object_put(obj);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001925 engine->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001926}
1927
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001928static int init_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001929{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001930 struct drm_i915_gem_object *obj = engine->status_page.obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001931
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001932 if (obj == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001933 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001934 int ret;
1935
Chris Wilson91c8a322016-07-05 10:40:23 +01001936 obj = i915_gem_object_create(&engine->i915->drm, 4096);
Chris Wilsonfe3db792016-04-25 13:32:13 +01001937 if (IS_ERR(obj)) {
Chris Wilsone3efda42014-04-09 09:19:41 +01001938 DRM_ERROR("Failed to allocate status page\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01001939 return PTR_ERR(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001940 }
1941
1942 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1943 if (ret)
1944 goto err_unref;
1945
Chris Wilson1f767e02014-07-03 17:33:03 -04001946 flags = 0;
Chris Wilsonc0336662016-05-06 15:40:21 +01001947 if (!HAS_LLC(engine->i915))
Chris Wilson1f767e02014-07-03 17:33:03 -04001948 /* On g33, we cannot place HWS above 256MiB, so
1949 * restrict its pinning to the low mappable arena.
1950 * Though this restriction is not documented for
1951 * gen4, gen5, or byt, they also behave similarly
1952 * and hang if the HWS is placed at the top of the
1953 * GTT. To generalise, it appears that all !llc
1954 * platforms have issues with us placing the HWS
1955 * above the mappable region (even though we never
1956 * actualy map it).
1957 */
1958 flags |= PIN_MAPPABLE;
1959 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001960 if (ret) {
1961err_unref:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001962 i915_gem_object_put(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001963 return ret;
1964 }
1965
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001966 engine->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001967 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001968
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001969 engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1970 engine->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
1971 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001972
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001973 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001974 engine->name, engine->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001975
1976 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001977}
1978
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001979static int init_phys_status_page(struct intel_engine_cs *engine)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001980{
Chris Wilsonc0336662016-05-06 15:40:21 +01001981 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001982
1983 if (!dev_priv->status_page_dmah) {
1984 dev_priv->status_page_dmah =
Chris Wilson91c8a322016-07-05 10:40:23 +01001985 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001986 if (!dev_priv->status_page_dmah)
1987 return -ENOMEM;
1988 }
1989
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001990 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1991 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001992
1993 return 0;
1994}
1995
Chris Wilson7e37f882016-08-02 22:50:21 +01001996void intel_unpin_ring(struct intel_ring *ringbuf)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001997{
Chris Wilsonf2f0ed72016-07-20 13:31:56 +01001998 GEM_BUG_ON(!ringbuf->vma);
1999 GEM_BUG_ON(!ringbuf->vaddr);
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002000
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002001 if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002002 i915_gem_object_unpin_map(ringbuf->obj);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002003 else
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002004 i915_vma_unpin_iomap(ringbuf->vma);
Chris Wilsonf2f0ed72016-07-20 13:31:56 +01002005 ringbuf->vaddr = NULL;
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002006
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002007 i915_gem_object_ggtt_unpin(ringbuf->obj);
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002008 ringbuf->vma = NULL;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002009}
2010
Chris Wilson7e37f882016-08-02 22:50:21 +01002011int intel_pin_and_map_ring(struct drm_i915_private *dev_priv,
2012 struct intel_ring *ringbuf)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002013{
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002014 struct drm_i915_gem_object *obj = ringbuf->obj;
Chris Wilsona687a432016-04-13 17:35:11 +01002015 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
2016 unsigned flags = PIN_OFFSET_BIAS | 4096;
Dave Gordon83052162016-04-12 14:46:16 +01002017 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002018 int ret;
2019
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002020 if (HAS_LLC(dev_priv) && !obj->stolen) {
Chris Wilsona687a432016-04-13 17:35:11 +01002021 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002022 if (ret)
2023 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002024
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002025 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsond2cad532016-04-08 12:11:10 +01002026 if (ret)
2027 goto err_unpin;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002028
Dave Gordon83052162016-04-12 14:46:16 +01002029 addr = i915_gem_object_pin_map(obj);
2030 if (IS_ERR(addr)) {
2031 ret = PTR_ERR(addr);
Chris Wilsond2cad532016-04-08 12:11:10 +01002032 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002033 }
2034 } else {
Chris Wilsona687a432016-04-13 17:35:11 +01002035 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
2036 flags | PIN_MAPPABLE);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002037 if (ret)
2038 return ret;
2039
2040 ret = i915_gem_object_set_to_gtt_domain(obj, true);
Chris Wilsond2cad532016-04-08 12:11:10 +01002041 if (ret)
2042 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002043
Daniele Ceraolo Spurioff3dc082016-01-27 15:43:49 +00002044 /* Access through the GTT requires the device to be awake. */
2045 assert_rpm_wakelock_held(dev_priv);
2046
Chris Wilson406ea8d2016-07-20 13:31:55 +01002047 addr = (void __force *)
2048 i915_vma_pin_iomap(i915_gem_obj_to_ggtt(obj));
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002049 if (IS_ERR(addr)) {
2050 ret = PTR_ERR(addr);
Chris Wilsond2cad532016-04-08 12:11:10 +01002051 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002052 }
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002053 }
2054
Chris Wilsonf2f0ed72016-07-20 13:31:56 +01002055 ringbuf->vaddr = addr;
Tvrtko Ursulin0eb973d2016-01-15 15:10:28 +00002056 ringbuf->vma = i915_gem_obj_to_ggtt(obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002057 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01002058
2059err_unpin:
2060 i915_gem_object_ggtt_unpin(obj);
2061 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002062}
2063
Chris Wilson7e37f882016-08-02 22:50:21 +01002064static void intel_destroy_ringbuffer_obj(struct intel_ring *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01002065{
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002066 i915_gem_object_put(ringbuf->obj);
Oscar Mateo2919d292014-07-03 16:28:02 +01002067 ringbuf->obj = NULL;
2068}
2069
Chris Wilson01101fa2015-09-03 13:01:39 +01002070static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
Chris Wilson7e37f882016-08-02 22:50:21 +01002071 struct intel_ring *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01002072{
Chris Wilsone3efda42014-04-09 09:19:41 +01002073 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002074
2075 obj = NULL;
2076 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002077 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002078 if (obj == NULL)
Dave Gordond37cd8a2016-04-22 19:14:32 +01002079 obj = i915_gem_object_create(dev, ringbuf->size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002080 if (IS_ERR(obj))
2081 return PTR_ERR(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01002082
Akash Goel24f3a8c2014-06-17 10:59:42 +05302083 /* mark ring buffers as read-only from GPU side by default */
2084 obj->gt_ro = 1;
2085
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002086 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002087
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002088 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002089}
2090
Chris Wilson7e37f882016-08-02 22:50:21 +01002091struct intel_ring *
2092intel_engine_create_ring(struct intel_engine_cs *engine, int size)
Chris Wilson01101fa2015-09-03 13:01:39 +01002093{
Chris Wilson7e37f882016-08-02 22:50:21 +01002094 struct intel_ring *ring;
Chris Wilson01101fa2015-09-03 13:01:39 +01002095 int ret;
2096
2097 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson608c1a52015-09-03 13:01:40 +01002098 if (ring == NULL) {
2099 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2100 engine->name);
Chris Wilson01101fa2015-09-03 13:01:39 +01002101 return ERR_PTR(-ENOMEM);
Chris Wilson608c1a52015-09-03 13:01:40 +01002102 }
Chris Wilson01101fa2015-09-03 13:01:39 +01002103
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002104 ring->engine = engine;
Chris Wilson608c1a52015-09-03 13:01:40 +01002105 list_add(&ring->link, &engine->buffers);
Chris Wilson01101fa2015-09-03 13:01:39 +01002106
2107 ring->size = size;
2108 /* Workaround an erratum on the i830 which causes a hang if
2109 * the TAIL pointer points to within the last 2 cachelines
2110 * of the buffer.
2111 */
2112 ring->effective_size = size;
Chris Wilsonc0336662016-05-06 15:40:21 +01002113 if (IS_I830(engine->i915) || IS_845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01002114 ring->effective_size -= 2 * CACHELINE_BYTES;
2115
2116 ring->last_retired_head = -1;
2117 intel_ring_update_space(ring);
2118
Chris Wilson91c8a322016-07-05 10:40:23 +01002119 ret = intel_alloc_ringbuffer_obj(&engine->i915->drm, ring);
Chris Wilson01101fa2015-09-03 13:01:39 +01002120 if (ret) {
Chris Wilson608c1a52015-09-03 13:01:40 +01002121 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
2122 engine->name, ret);
2123 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002124 kfree(ring);
2125 return ERR_PTR(ret);
2126 }
2127
2128 return ring;
2129}
2130
2131void
Chris Wilson7e37f882016-08-02 22:50:21 +01002132intel_ring_free(struct intel_ring *ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01002133{
2134 intel_destroy_ringbuffer_obj(ring);
Chris Wilson608c1a52015-09-03 13:01:40 +01002135 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002136 kfree(ring);
2137}
2138
Chris Wilson0cb26a82016-06-24 14:55:53 +01002139static int intel_ring_context_pin(struct i915_gem_context *ctx,
2140 struct intel_engine_cs *engine)
2141{
2142 struct intel_context *ce = &ctx->engine[engine->id];
2143 int ret;
2144
Chris Wilson91c8a322016-07-05 10:40:23 +01002145 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002146
2147 if (ce->pin_count++)
2148 return 0;
2149
2150 if (ce->state) {
2151 ret = i915_gem_obj_ggtt_pin(ce->state, ctx->ggtt_alignment, 0);
2152 if (ret)
2153 goto error;
2154 }
2155
Chris Wilsonc7c3c072016-06-24 14:55:54 +01002156 /* The kernel context is only used as a placeholder for flushing the
2157 * active context. It is never used for submitting user rendering and
2158 * as such never requires the golden render context, and so we can skip
2159 * emitting it when we switch to the kernel context. This is required
2160 * as during eviction we cannot allocate and pin the renderstate in
2161 * order to initialise the context.
2162 */
2163 if (ctx == ctx->i915->kernel_context)
2164 ce->initialised = true;
2165
Chris Wilson9a6feaf2016-07-20 13:31:50 +01002166 i915_gem_context_get(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002167 return 0;
2168
2169error:
2170 ce->pin_count = 0;
2171 return ret;
2172}
2173
2174static void intel_ring_context_unpin(struct i915_gem_context *ctx,
2175 struct intel_engine_cs *engine)
2176{
2177 struct intel_context *ce = &ctx->engine[engine->id];
2178
Chris Wilson91c8a322016-07-05 10:40:23 +01002179 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002180
2181 if (--ce->pin_count)
2182 return;
2183
2184 if (ce->state)
2185 i915_gem_object_ggtt_unpin(ce->state);
2186
Chris Wilson9a6feaf2016-07-20 13:31:50 +01002187 i915_gem_context_put(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002188}
2189
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002190static int intel_init_ring_buffer(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002191{
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002192 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +01002193 struct intel_ring *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002194 int ret;
2195
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002196 WARN_ON(engine->buffer);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002197
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002198 intel_engine_setup_common(engine);
2199
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002200 memset(engine->semaphore.sync_seqno, 0,
2201 sizeof(engine->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002202
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002203 ret = intel_engine_init_common(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +01002204 if (ret)
2205 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002206
Chris Wilson0cb26a82016-06-24 14:55:53 +01002207 /* We may need to do things with the shrinker which
2208 * require us to immediately switch back to the default
2209 * context. This can cause a problem as pinning the
2210 * default context also requires GTT space which may not
2211 * be available. To avoid this we always pin the default
2212 * context.
2213 */
2214 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2215 if (ret)
2216 goto error;
2217
Chris Wilson7e37f882016-08-02 22:50:21 +01002218 ringbuf = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
Dave Gordonb0366a52015-12-08 15:02:36 +00002219 if (IS_ERR(ringbuf)) {
2220 ret = PTR_ERR(ringbuf);
2221 goto error;
2222 }
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002223 engine->buffer = ringbuf;
Chris Wilson01101fa2015-09-03 13:01:39 +01002224
Chris Wilsonc0336662016-05-06 15:40:21 +01002225 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002226 ret = init_status_page(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002227 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002228 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002229 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002230 WARN_ON(engine->id != RCS);
2231 ret = init_phys_status_page(engine);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002232 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002233 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002234 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002235
Chris Wilson7e37f882016-08-02 22:50:21 +01002236 ret = intel_pin_and_map_ring(dev_priv, ringbuf);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002237 if (ret) {
2238 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002239 engine->name, ret);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002240 intel_destroy_ringbuffer_obj(ringbuf);
2241 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002242 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002243
Oscar Mateo8ee14972014-05-22 14:13:34 +01002244 return 0;
2245
2246error:
Chris Wilson7e37f882016-08-02 22:50:21 +01002247 intel_engine_cleanup(engine);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002248 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002249}
2250
Chris Wilson7e37f882016-08-02 22:50:21 +01002251void intel_engine_cleanup(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002252{
John Harrison6402c332014-10-31 12:00:26 +00002253 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01002254
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002255 if (!intel_engine_initialized(engine))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002256 return;
2257
Chris Wilsonc0336662016-05-06 15:40:21 +01002258 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00002259
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002260 if (engine->buffer) {
Chris Wilson7e37f882016-08-02 22:50:21 +01002261 intel_engine_stop(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01002262 WARN_ON(!IS_GEN2(dev_priv) && (I915_READ_MODE(engine) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002263
Chris Wilson7e37f882016-08-02 22:50:21 +01002264 intel_unpin_ring(engine->buffer);
2265 intel_ring_free(engine->buffer);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002266 engine->buffer = NULL;
Dave Gordonb0366a52015-12-08 15:02:36 +00002267 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002268
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002269 if (engine->cleanup)
2270 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08002271
Chris Wilsonc0336662016-05-06 15:40:21 +01002272 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002273 cleanup_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002274 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002275 WARN_ON(engine->id != RCS);
2276 cleanup_phys_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002277 }
Brad Volkin44e895a2014-05-10 14:10:43 -07002278
Chris Wilson33a051a2016-07-27 09:07:26 +01002279 intel_engine_cleanup_cmd_parser(engine);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002280 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson688e6c72016-07-01 17:23:15 +01002281 intel_engine_fini_breadcrumbs(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002282
2283 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2284
Chris Wilsonc0336662016-05-06 15:40:21 +01002285 engine->i915 = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002286}
2287
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002288int intel_engine_idle(struct intel_engine_cs *engine)
Chris Wilson3e960502012-11-27 16:22:54 +00002289{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002290 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002291
Chris Wilson3e960502012-11-27 16:22:54 +00002292 /* Wait upon the last request to be completed */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002293 if (list_empty(&engine->request_list))
Chris Wilson3e960502012-11-27 16:22:54 +00002294 return 0;
2295
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002296 req = list_entry(engine->request_list.prev,
2297 struct drm_i915_gem_request,
2298 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002299
Chris Wilsonb4716182015-04-27 13:41:17 +01002300 /* Make sure we do not trigger any retires */
2301 return __i915_wait_request(req,
Chris Wilsonc19ae982016-04-13 17:35:03 +01002302 req->i915->mm.interruptible,
Chris Wilsonb4716182015-04-27 13:41:17 +01002303 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002304}
2305
John Harrison6689cb22015-03-19 12:30:08 +00002306int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002307{
Chris Wilson63103462016-04-28 09:56:49 +01002308 int ret;
2309
2310 /* Flush enough space to reduce the likelihood of waiting after
2311 * we start building the request - in which case we will just
2312 * have to repeat work.
2313 */
Chris Wilsona0442462016-04-29 09:07:05 +01002314 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002315
Chris Wilson1dae2df2016-08-02 22:50:19 +01002316 request->ring = request->engine->buffer;
Chris Wilson63103462016-04-28 09:56:49 +01002317
2318 ret = intel_ring_begin(request, 0);
2319 if (ret)
2320 return ret;
2321
Chris Wilsona0442462016-04-29 09:07:05 +01002322 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002323 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002324}
2325
Chris Wilson987046a2016-04-28 09:56:46 +01002326static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002327{
Chris Wilson7e37f882016-08-02 22:50:21 +01002328 struct intel_ring *ring = req->ring;
Chris Wilson987046a2016-04-28 09:56:46 +01002329 struct intel_engine_cs *engine = req->engine;
2330 struct drm_i915_gem_request *target;
2331
Chris Wilson1dae2df2016-08-02 22:50:19 +01002332 intel_ring_update_space(ring);
2333 if (ring->space >= bytes)
Chris Wilson987046a2016-04-28 09:56:46 +01002334 return 0;
2335
2336 /*
2337 * Space is reserved in the ringbuffer for finalising the request,
2338 * as that cannot be allowed to fail. During request finalisation,
2339 * reserved_space is set to 0 to stop the overallocation and the
2340 * assumption is that then we never need to wait (which has the
2341 * risk of failing with EINTR).
2342 *
2343 * See also i915_gem_request_alloc() and i915_add_request().
2344 */
Chris Wilson0251a962016-04-28 09:56:47 +01002345 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01002346
2347 list_for_each_entry(target, &engine->request_list, list) {
2348 unsigned space;
2349
2350 /*
2351 * The request queue is per-engine, so can contain requests
2352 * from multiple ringbuffers. Here, we must ignore any that
2353 * aren't from the ringbuffer we're considering.
2354 */
Chris Wilson1dae2df2016-08-02 22:50:19 +01002355 if (target->ring != ring)
Chris Wilson987046a2016-04-28 09:56:46 +01002356 continue;
2357
2358 /* Would completion of this request free enough space? */
Chris Wilson1dae2df2016-08-02 22:50:19 +01002359 space = __intel_ring_space(target->postfix, ring->tail,
2360 ring->size);
Chris Wilson987046a2016-04-28 09:56:46 +01002361 if (space >= bytes)
2362 break;
2363 }
2364
2365 if (WARN_ON(&target->list == &engine->request_list))
2366 return -ENOSPC;
2367
2368 return i915_wait_request(target);
2369}
2370
2371int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
2372{
Chris Wilson7e37f882016-08-02 22:50:21 +01002373 struct intel_ring *ring = req->ring;
Chris Wilson1dae2df2016-08-02 22:50:19 +01002374 int remain_actual = ring->size - ring->tail;
2375 int remain_usable = ring->effective_size - ring->tail;
Chris Wilson987046a2016-04-28 09:56:46 +01002376 int bytes = num_dwords * sizeof(u32);
2377 int total_bytes, wait_bytes;
John Harrison79bbcc22015-06-30 12:40:55 +01002378 bool need_wrap = false;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002379
Chris Wilson0251a962016-04-28 09:56:47 +01002380 total_bytes = bytes + req->reserved_space;
John Harrison29b1b412015-06-18 13:10:09 +01002381
John Harrison79bbcc22015-06-30 12:40:55 +01002382 if (unlikely(bytes > remain_usable)) {
2383 /*
2384 * Not enough space for the basic request. So need to flush
2385 * out the remainder and then wait for base + reserved.
2386 */
2387 wait_bytes = remain_actual + total_bytes;
2388 need_wrap = true;
Chris Wilson987046a2016-04-28 09:56:46 +01002389 } else if (unlikely(total_bytes > remain_usable)) {
2390 /*
2391 * The base request will fit but the reserved space
2392 * falls off the end. So we don't need an immediate wrap
2393 * and only need to effectively wait for the reserved
2394 * size space from the start of ringbuffer.
2395 */
Chris Wilson0251a962016-04-28 09:56:47 +01002396 wait_bytes = remain_actual + req->reserved_space;
John Harrison79bbcc22015-06-30 12:40:55 +01002397 } else {
Chris Wilson987046a2016-04-28 09:56:46 +01002398 /* No wrapping required, just waiting. */
2399 wait_bytes = total_bytes;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002400 }
2401
Chris Wilson1dae2df2016-08-02 22:50:19 +01002402 if (wait_bytes > ring->space) {
Chris Wilson987046a2016-04-28 09:56:46 +01002403 int ret = wait_for_space(req, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002404 if (unlikely(ret))
2405 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +01002406
Chris Wilson1dae2df2016-08-02 22:50:19 +01002407 intel_ring_update_space(ring);
2408 if (unlikely(ring->space < wait_bytes))
Chris Wilsone075a322016-05-13 11:57:22 +01002409 return -EAGAIN;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002410 }
2411
Chris Wilson987046a2016-04-28 09:56:46 +01002412 if (unlikely(need_wrap)) {
Chris Wilson1dae2df2016-08-02 22:50:19 +01002413 GEM_BUG_ON(remain_actual > ring->space);
2414 GEM_BUG_ON(ring->tail + remain_actual > ring->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002415
Chris Wilson987046a2016-04-28 09:56:46 +01002416 /* Fill the tail with MI_NOOP */
Chris Wilson1dae2df2016-08-02 22:50:19 +01002417 memset(ring->vaddr + ring->tail, 0, remain_actual);
2418 ring->tail = 0;
2419 ring->space -= remain_actual;
Chris Wilson987046a2016-04-28 09:56:46 +01002420 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002421
Chris Wilson1dae2df2016-08-02 22:50:19 +01002422 ring->space -= bytes;
2423 GEM_BUG_ON(ring->space < 0);
Chris Wilson304d6952014-01-02 14:32:35 +00002424 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002425}
2426
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002427/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01002428int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002429{
Chris Wilson7e37f882016-08-02 22:50:21 +01002430 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002431 int num_dwords =
2432 (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002433 int ret;
2434
2435 if (num_dwords == 0)
2436 return 0;
2437
Chris Wilson18393f62014-04-09 09:19:40 +01002438 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
John Harrison5fb9de12015-05-29 17:44:07 +01002439 ret = intel_ring_begin(req, num_dwords);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002440 if (ret)
2441 return ret;
2442
2443 while (num_dwords--)
Chris Wilsonb5321f32016-08-02 22:50:18 +01002444 intel_ring_emit(ring, MI_NOOP);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002445
Chris Wilsonb5321f32016-08-02 22:50:18 +01002446 intel_ring_advance(ring);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002447
2448 return 0;
2449}
2450
Chris Wilson7e37f882016-08-02 22:50:21 +01002451void intel_engine_init_seqno(struct intel_engine_cs *engine, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002452{
Chris Wilsonc0336662016-05-06 15:40:21 +01002453 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002454
Chris Wilson29dcb572016-04-07 07:29:13 +01002455 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
2456 * so long as the semaphore value in the register/page is greater
2457 * than the sync value), so whenever we reset the seqno,
2458 * so long as we reset the tracking semaphore value to 0, it will
2459 * always be before the next request's seqno. If we don't reset
2460 * the semaphore value, then when the seqno moves backwards all
2461 * future waits will complete instantly (causing rendering corruption).
2462 */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01002463 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002464 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
2465 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
Chris Wilsond04bce42016-04-07 07:29:12 +01002466 if (HAS_VEBOX(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002467 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002468 }
Chris Wilsona058d932016-04-07 07:29:15 +01002469 if (dev_priv->semaphore_obj) {
2470 struct drm_i915_gem_object *obj = dev_priv->semaphore_obj;
2471 struct page *page = i915_gem_object_get_dirty_page(obj, 0);
2472 void *semaphores = kmap(page);
2473 memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
2474 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
2475 kunmap(page);
2476 }
Chris Wilson29dcb572016-04-07 07:29:13 +01002477 memset(engine->semaphore.sync_seqno, 0,
2478 sizeof(engine->semaphore.sync_seqno));
Chris Wilson297b0c52010-10-22 17:02:41 +01002479
Chris Wilson1b7744e2016-07-01 17:23:17 +01002480 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
2481 if (engine->irq_seqno_barrier)
2482 engine->irq_seqno_barrier(engine);
Chris Wilson01347122016-04-07 07:29:16 +01002483 engine->last_submitted_seqno = seqno;
Chris Wilson29dcb572016-04-07 07:29:13 +01002484
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002485 engine->hangcheck.seqno = seqno;
Chris Wilson688e6c72016-07-01 17:23:15 +01002486
2487 /* After manually advancing the seqno, fake the interrupt in case
2488 * there are any waiters for that seqno.
2489 */
2490 rcu_read_lock();
2491 intel_engine_wakeup(engine);
2492 rcu_read_unlock();
Chris Wilson549f7362010-10-19 11:19:32 +01002493}
2494
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002495static void gen6_bsd_ring_write_tail(struct intel_engine_cs *engine,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002496 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002497{
Chris Wilsonc0336662016-05-06 15:40:21 +01002498 struct drm_i915_private *dev_priv = engine->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002499
Chris Wilson76f84212016-06-30 15:33:45 +01002500 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2501
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002502 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002503
Chris Wilson12f55812012-07-05 17:14:01 +01002504 /* Disable notification that the ring is IDLE. The GT
2505 * will then assume that it is busy and bring it out of rc6.
2506 */
Chris Wilson76f84212016-06-30 15:33:45 +01002507 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2508 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01002509
2510 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01002511 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01002512
2513 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson76f84212016-06-30 15:33:45 +01002514 if (intel_wait_for_register_fw(dev_priv,
2515 GEN6_BSD_SLEEP_PSMI_CONTROL,
2516 GEN6_BSD_SLEEP_INDICATOR,
2517 0,
2518 50))
Chris Wilson12f55812012-07-05 17:14:01 +01002519 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002520
Chris Wilson12f55812012-07-05 17:14:01 +01002521 /* Now that the ring is fully powered up, update the tail */
Chris Wilson76f84212016-06-30 15:33:45 +01002522 I915_WRITE_FW(RING_TAIL(engine->mmio_base), value);
2523 POSTING_READ_FW(RING_TAIL(engine->mmio_base));
Chris Wilson12f55812012-07-05 17:14:01 +01002524
2525 /* Let the ring send IDLE messages to the GT again,
2526 * and so let it sleep to conserve power when idle.
2527 */
Chris Wilson76f84212016-06-30 15:33:45 +01002528 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2529 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2530
2531 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002532}
2533
John Harrisona84c3ae2015-05-29 17:43:57 +01002534static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002535 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002536{
Chris Wilson7e37f882016-08-02 22:50:21 +01002537 struct intel_ring *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002538 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002539 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002540
John Harrison5fb9de12015-05-29 17:44:07 +01002541 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002542 if (ret)
2543 return ret;
2544
Chris Wilson71a77e02011-02-02 12:13:49 +00002545 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002546 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002547 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002548
2549 /* We always require a command barrier so that subsequent
2550 * commands, such as breadcrumb interrupts, are strictly ordered
2551 * wrt the contents of the write cache being flushed to memory
2552 * (and thus being coherent from the CPU).
2553 */
2554 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2555
Jesse Barnes9a289772012-10-26 09:42:42 -07002556 /*
2557 * Bspec vol 1c.5 - video engine command streamer:
2558 * "If ENABLED, all TLBs will be invalidated once the flush
2559 * operation is complete. This bit is only valid when the
2560 * Post-Sync Operation field is a value of 1h or 3h."
2561 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002562 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002563 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2564
Chris Wilsonb5321f32016-08-02 22:50:18 +01002565 intel_ring_emit(ring, cmd);
2566 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002567 if (INTEL_GEN(req->i915) >= 8) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002568 intel_ring_emit(ring, 0); /* upper addr */
2569 intel_ring_emit(ring, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002570 } else {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002571 intel_ring_emit(ring, 0);
2572 intel_ring_emit(ring, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002573 }
Chris Wilsonb5321f32016-08-02 22:50:18 +01002574 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002575 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002576}
2577
2578static int
John Harrison53fddaf2015-05-29 17:44:02 +01002579gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002580 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002581 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002582{
Chris Wilson7e37f882016-08-02 22:50:21 +01002583 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002584 bool ppgtt = USES_PPGTT(req->i915) &&
John Harrison8e004ef2015-02-13 11:48:10 +00002585 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002586 int ret;
2587
John Harrison5fb9de12015-05-29 17:44:07 +01002588 ret = intel_ring_begin(req, 4);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002589 if (ret)
2590 return ret;
2591
2592 /* FIXME(BDW): Address space and security selectors. */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002593 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002594 (dispatch_flags & I915_DISPATCH_RS ?
2595 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsonb5321f32016-08-02 22:50:18 +01002596 intel_ring_emit(ring, lower_32_bits(offset));
2597 intel_ring_emit(ring, upper_32_bits(offset));
2598 intel_ring_emit(ring, MI_NOOP);
2599 intel_ring_advance(ring);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002600
2601 return 0;
2602}
2603
2604static int
John Harrison53fddaf2015-05-29 17:44:02 +01002605hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002606 u64 offset, u32 len,
2607 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002608{
Chris Wilson7e37f882016-08-02 22:50:21 +01002609 struct intel_ring *ring = req->ring;
Akshay Joshi0206e352011-08-16 15:34:10 -04002610 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002611
John Harrison5fb9de12015-05-29 17:44:07 +01002612 ret = intel_ring_begin(req, 2);
Akshay Joshi0206e352011-08-16 15:34:10 -04002613 if (ret)
2614 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002615
Chris Wilsonb5321f32016-08-02 22:50:18 +01002616 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002617 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002618 (dispatch_flags & I915_DISPATCH_SECURE ?
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002619 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2620 (dispatch_flags & I915_DISPATCH_RS ?
2621 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002622 /* bit0-7 is the length on GEN6+ */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002623 intel_ring_emit(ring, offset);
2624 intel_ring_advance(ring);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002625
2626 return 0;
2627}
2628
2629static int
John Harrison53fddaf2015-05-29 17:44:02 +01002630gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002631 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002632 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002633{
Chris Wilson7e37f882016-08-02 22:50:21 +01002634 struct intel_ring *ring = req->ring;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002635 int ret;
2636
John Harrison5fb9de12015-05-29 17:44:07 +01002637 ret = intel_ring_begin(req, 2);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002638 if (ret)
2639 return ret;
2640
Chris Wilsonb5321f32016-08-02 22:50:18 +01002641 intel_ring_emit(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002642 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002643 (dispatch_flags & I915_DISPATCH_SECURE ?
2644 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002645 /* bit0-7 is the length on GEN6+ */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002646 intel_ring_emit(ring, offset);
2647 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002648
Akshay Joshi0206e352011-08-16 15:34:10 -04002649 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002650}
2651
Chris Wilson549f7362010-10-19 11:19:32 +01002652/* Blitter support (SandyBridge+) */
2653
John Harrisona84c3ae2015-05-29 17:43:57 +01002654static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002655 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002656{
Chris Wilson7e37f882016-08-02 22:50:21 +01002657 struct intel_ring *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002658 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002659 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002660
John Harrison5fb9de12015-05-29 17:44:07 +01002661 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002662 if (ret)
2663 return ret;
2664
Chris Wilson71a77e02011-02-02 12:13:49 +00002665 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002666 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002667 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002668
2669 /* We always require a command barrier so that subsequent
2670 * commands, such as breadcrumb interrupts, are strictly ordered
2671 * wrt the contents of the write cache being flushed to memory
2672 * (and thus being coherent from the CPU).
2673 */
2674 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2675
Jesse Barnes9a289772012-10-26 09:42:42 -07002676 /*
2677 * Bspec vol 1c.3 - blitter engine command streamer:
2678 * "If ENABLED, all TLBs will be invalidated once the flush
2679 * operation is complete. This bit is only valid when the
2680 * Post-Sync Operation field is a value of 1h or 3h."
2681 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002682 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002683 cmd |= MI_INVALIDATE_TLB;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002684 intel_ring_emit(ring, cmd);
2685 intel_ring_emit(ring,
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002686 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002687 if (INTEL_GEN(req->i915) >= 8) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002688 intel_ring_emit(ring, 0); /* upper addr */
2689 intel_ring_emit(ring, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002690 } else {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002691 intel_ring_emit(ring, 0);
2692 intel_ring_emit(ring, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002693 }
Chris Wilsonb5321f32016-08-02 22:50:18 +01002694 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002695
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002696 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002697}
2698
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002699static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2700 struct intel_engine_cs *engine)
2701{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002702 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002703 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002704
Chris Wilson39df9192016-07-20 13:31:57 +01002705 if (!i915.semaphores)
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002706 return;
2707
2708 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore_obj) {
Chris Wilson91c8a322016-07-05 10:40:23 +01002709 obj = i915_gem_object_create(&dev_priv->drm, 4096);
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002710 if (IS_ERR(obj)) {
2711 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2712 i915.semaphores = 0;
2713 } else {
2714 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2715 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2716 if (ret != 0) {
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002717 i915_gem_object_put(obj);
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002718 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2719 i915.semaphores = 0;
2720 } else {
2721 dev_priv->semaphore_obj = obj;
2722 }
2723 }
2724 }
2725
Chris Wilson39df9192016-07-20 13:31:57 +01002726 if (!i915.semaphores)
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002727 return;
2728
2729 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002730 u64 offset = i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj);
2731
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002732 engine->semaphore.sync_to = gen8_ring_sync;
2733 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002734
2735 for (i = 0; i < I915_NUM_ENGINES; i++) {
2736 u64 ring_offset;
2737
2738 if (i != engine->id)
2739 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2740 else
2741 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2742
2743 engine->semaphore.signal_ggtt[i] = ring_offset;
2744 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002745 } else if (INTEL_GEN(dev_priv) >= 6) {
2746 engine->semaphore.sync_to = gen6_ring_sync;
2747 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002748
2749 /*
2750 * The current semaphore is only applied on pre-gen8
2751 * platform. And there is no VCS2 ring on the pre-gen8
2752 * platform. So the semaphore between RCS and VCS2 is
2753 * initialized as INVALID. Gen8 will initialize the
2754 * sema between VCS2 and RCS later.
2755 */
2756 for (i = 0; i < I915_NUM_ENGINES; i++) {
2757 static const struct {
2758 u32 wait_mbox;
2759 i915_reg_t mbox_reg;
2760 } sem_data[I915_NUM_ENGINES][I915_NUM_ENGINES] = {
2761 [RCS] = {
2762 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2763 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2764 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
2765 },
2766 [VCS] = {
2767 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2768 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2769 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
2770 },
2771 [BCS] = {
2772 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2773 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2774 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
2775 },
2776 [VECS] = {
2777 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2778 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2779 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
2780 },
2781 };
2782 u32 wait_mbox;
2783 i915_reg_t mbox_reg;
2784
2785 if (i == engine->id || i == VCS2) {
2786 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2787 mbox_reg = GEN6_NOSYNC;
2788 } else {
2789 wait_mbox = sem_data[engine->id][i].wait_mbox;
2790 mbox_reg = sem_data[engine->id][i].mbox_reg;
2791 }
2792
2793 engine->semaphore.mbox.wait[i] = wait_mbox;
2794 engine->semaphore.mbox.signal[i] = mbox_reg;
2795 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002796 }
2797}
2798
Chris Wilsoned003072016-07-01 09:18:13 +01002799static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2800 struct intel_engine_cs *engine)
2801{
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002802 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2803
Chris Wilsoned003072016-07-01 09:18:13 +01002804 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002805 engine->irq_enable = gen8_irq_enable;
2806 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002807 engine->irq_seqno_barrier = gen6_seqno_barrier;
2808 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002809 engine->irq_enable = gen6_irq_enable;
2810 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002811 engine->irq_seqno_barrier = gen6_seqno_barrier;
2812 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002813 engine->irq_enable = gen5_irq_enable;
2814 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002815 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002816 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002817 engine->irq_enable = i9xx_irq_enable;
2818 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002819 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002820 engine->irq_enable = i8xx_irq_enable;
2821 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002822 }
2823}
2824
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002825static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2826 struct intel_engine_cs *engine)
2827{
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002828 engine->init_hw = init_ring_common;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002829 engine->write_tail = ring_write_tail;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002830
Chris Wilson6f7bef72016-07-01 09:18:12 +01002831 engine->add_request = i9xx_add_request;
2832 if (INTEL_GEN(dev_priv) >= 6)
2833 engine->add_request = gen6_add_request;
2834
2835 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002836 engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002837 else if (INTEL_GEN(dev_priv) >= 6)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002838 engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002839 else if (INTEL_GEN(dev_priv) >= 4)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002840 engine->dispatch_execbuffer = i965_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002841 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
2842 engine->dispatch_execbuffer = i830_dispatch_execbuffer;
2843 else
2844 engine->dispatch_execbuffer = i915_dispatch_execbuffer;
Tvrtko Ursulinb9700322016-06-29 16:09:23 +01002845
Chris Wilsoned003072016-07-01 09:18:13 +01002846 intel_ring_init_irq(dev_priv, engine);
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002847 intel_ring_init_semaphores(dev_priv, engine);
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002848}
2849
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002850int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002851{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002852 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07002853 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002854
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002855 intel_ring_default_vfuncs(dev_priv, engine);
2856
Chris Wilson61ff75a2016-07-01 17:23:28 +01002857 if (HAS_L3_DPF(dev_priv))
2858 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002859
Chris Wilsonc0336662016-05-06 15:40:21 +01002860 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002861 engine->init_context = intel_rcs_ctx_init;
Chris Wilsona58c01a2016-04-29 13:18:21 +01002862 engine->add_request = gen8_render_add_request;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002863 engine->flush = gen8_render_ring_flush;
Chris Wilson39df9192016-07-20 13:31:57 +01002864 if (i915.semaphores)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002865 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilsonc0336662016-05-06 15:40:21 +01002866 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002867 engine->init_context = intel_rcs_ctx_init;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002868 engine->flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002869 if (IS_GEN6(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002870 engine->flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002871 } else if (IS_GEN5(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002872 engine->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002873 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002874 if (INTEL_GEN(dev_priv) < 4)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002875 engine->flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002876 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002877 engine->flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002878 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002879 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002880
Chris Wilsonc0336662016-05-06 15:40:21 +01002881 if (IS_HASWELL(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002882 engine->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002883
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002884 engine->init_hw = init_render_ring;
2885 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002886
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002887 ret = intel_init_ring_buffer(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002888 if (ret)
2889 return ret;
2890
Chris Wilsonf8973c22016-07-01 17:23:21 +01002891 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson7d5ea802016-07-01 17:23:20 +01002892 ret = intel_init_pipe_control(engine, 4096);
2893 if (ret)
2894 return ret;
2895 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
2896 ret = intel_init_pipe_control(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002897 if (ret)
2898 return ret;
2899 }
2900
2901 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002902}
2903
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002904int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002905{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002906 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002907
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002908 intel_ring_default_vfuncs(dev_priv, engine);
2909
Chris Wilsonc0336662016-05-06 15:40:21 +01002910 if (INTEL_GEN(dev_priv) >= 6) {
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002911 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002912 if (IS_GEN6(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002913 engine->write_tail = gen6_bsd_ring_write_tail;
2914 engine->flush = gen6_bsd_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002915 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002916 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002917 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002918 engine->mmio_base = BSD_RING_BASE;
2919 engine->flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002920 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002921 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002922 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002923 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002924 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002925
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002926 return intel_init_ring_buffer(engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002927}
Chris Wilson549f7362010-10-19 11:19:32 +01002928
Zhao Yakui845f74a2014-04-17 10:37:37 +08002929/**
Damien Lespiau62659922015-01-29 14:13:40 +00002930 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002931 */
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002932int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002933{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002934 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002935
2936 intel_ring_default_vfuncs(dev_priv, engine);
2937
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002938 engine->flush = gen6_bsd_ring_flush;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002939
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002940 return intel_init_ring_buffer(engine);
Zhao Yakui845f74a2014-04-17 10:37:37 +08002941}
2942
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002943int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
Chris Wilson549f7362010-10-19 11:19:32 +01002944{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002945 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002946
2947 intel_ring_default_vfuncs(dev_priv, engine);
2948
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002949 engine->flush = gen6_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002950 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002951 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002952
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002953 return intel_init_ring_buffer(engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002954}
Chris Wilsona7b97612012-07-20 12:41:08 +01002955
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002956int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002957{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002958 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002959
2960 intel_ring_default_vfuncs(dev_priv, engine);
2961
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002962 engine->flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002963
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002964 if (INTEL_GEN(dev_priv) < 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002965 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01002966 engine->irq_enable = hsw_vebox_irq_enable;
2967 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002968 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002969
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002970 return intel_init_ring_buffer(engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002971}
2972
Chris Wilsona7b97612012-07-20 12:41:08 +01002973int
Chris Wilson7e37f882016-08-02 22:50:21 +01002974intel_engine_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002975{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002976 struct intel_engine_cs *engine = req->engine;
Chris Wilsona7b97612012-07-20 12:41:08 +01002977 int ret;
2978
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002979 if (!engine->gpu_caches_dirty)
Chris Wilsona7b97612012-07-20 12:41:08 +01002980 return 0;
2981
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002982 ret = engine->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002983 if (ret)
2984 return ret;
2985
John Harrisona84c3ae2015-05-29 17:43:57 +01002986 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002987
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002988 engine->gpu_caches_dirty = false;
Chris Wilsona7b97612012-07-20 12:41:08 +01002989 return 0;
2990}
2991
2992int
Chris Wilson7e37f882016-08-02 22:50:21 +01002993intel_engine_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002994{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002995 struct intel_engine_cs *engine = req->engine;
Chris Wilsona7b97612012-07-20 12:41:08 +01002996 uint32_t flush_domains;
2997 int ret;
2998
2999 flush_domains = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003000 if (engine->gpu_caches_dirty)
Chris Wilsona7b97612012-07-20 12:41:08 +01003001 flush_domains = I915_GEM_GPU_DOMAINS;
3002
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003003 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003004 if (ret)
3005 return ret;
3006
John Harrisona84c3ae2015-05-29 17:43:57 +01003007 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003008
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003009 engine->gpu_caches_dirty = false;
Chris Wilsona7b97612012-07-20 12:41:08 +01003010 return 0;
3011}
Chris Wilsone3efda42014-04-09 09:19:41 +01003012
Chris Wilson7e37f882016-08-02 22:50:21 +01003013void intel_engine_stop(struct intel_engine_cs *engine)
Chris Wilsone3efda42014-04-09 09:19:41 +01003014{
3015 int ret;
3016
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00003017 if (!intel_engine_initialized(engine))
Chris Wilsone3efda42014-04-09 09:19:41 +01003018 return;
3019
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003020 ret = intel_engine_idle(engine);
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003021 if (ret)
Chris Wilsone3efda42014-04-09 09:19:41 +01003022 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003023 engine->name, ret);
Chris Wilsone3efda42014-04-09 09:19:41 +01003024
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003025 stop_ring(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01003026}