blob: 04a2d141e690592f183b8508de4f74341095f8eb [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
Dave Gordonebd0fd42014-11-27 11:22:49 +000050void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
51{
52 if (ringbuf->last_retired_head != -1) {
53 ringbuf->head = ringbuf->last_retired_head;
54 ringbuf->last_retired_head = -1;
55 }
56
57 ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
58 ringbuf->tail, ringbuf->size);
59}
60
Tvrtko Ursulin117897f2016-03-16 11:00:40 +000061bool intel_engine_stopped(struct intel_engine_cs *engine)
Chris Wilson09246732013-08-10 22:16:32 +010062{
Chris Wilsonc0336662016-05-06 15:40:21 +010063 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +000064 return dev_priv->gpu_error.stop_rings & intel_engine_flag(engine);
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020065}
Chris Wilson09246732013-08-10 22:16:32 +010066
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000067static void __intel_ring_advance(struct intel_engine_cs *engine)
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020068{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000069 struct intel_ringbuffer *ringbuf = engine->buffer;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010070 ringbuf->tail &= ringbuf->size - 1;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +000071 if (intel_engine_stopped(engine))
Chris Wilson09246732013-08-10 22:16:32 +010072 return;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000073 engine->write_tail(engine, ringbuf->tail);
Chris Wilson09246732013-08-10 22:16:32 +010074}
75
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000076static int
John Harrisona84c3ae2015-05-29 17:43:57 +010077gen2_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010078 u32 invalidate_domains,
79 u32 flush_domains)
80{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000081 struct intel_engine_cs *engine = req->engine;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010082 u32 cmd;
83 int ret;
84
85 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +020086 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010087 cmd |= MI_NO_WRITE_FLUSH;
88
89 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
90 cmd |= MI_READ_FLUSH;
91
John Harrison5fb9de12015-05-29 17:44:07 +010092 ret = intel_ring_begin(req, 2);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010093 if (ret)
94 return ret;
95
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000096 intel_ring_emit(engine, cmd);
97 intel_ring_emit(engine, MI_NOOP);
98 intel_ring_advance(engine);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010099
100 return 0;
101}
102
103static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100104gen4_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100105 u32 invalidate_domains,
106 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700107{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000108 struct intel_engine_cs *engine = req->engine;
Chris Wilson6f392d52010-08-07 11:01:22 +0100109 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000110 int ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100111
Chris Wilson36d527d2011-03-19 22:26:49 +0000112 /*
113 * read/write caches:
114 *
115 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
116 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
117 * also flushed at 2d versus 3d pipeline switches.
118 *
119 * read-only caches:
120 *
121 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
122 * MI_READ_FLUSH is set, and is always flushed on 965.
123 *
124 * I915_GEM_DOMAIN_COMMAND may not exist?
125 *
126 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
127 * invalidated when MI_EXE_FLUSH is set.
128 *
129 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
130 * invalidated with every MI_FLUSH.
131 *
132 * TLBs:
133 *
134 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
135 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
136 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
137 * are flushed at any MI_FLUSH.
138 */
139
140 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100141 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000142 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000143 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
144 cmd |= MI_EXE_FLUSH;
145
146 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
Chris Wilsonc0336662016-05-06 15:40:21 +0100147 (IS_G4X(req->i915) || IS_GEN5(req->i915)))
Chris Wilson36d527d2011-03-19 22:26:49 +0000148 cmd |= MI_INVALIDATE_ISP;
149
John Harrison5fb9de12015-05-29 17:44:07 +0100150 ret = intel_ring_begin(req, 2);
Chris Wilson36d527d2011-03-19 22:26:49 +0000151 if (ret)
152 return ret;
153
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000154 intel_ring_emit(engine, cmd);
155 intel_ring_emit(engine, MI_NOOP);
156 intel_ring_advance(engine);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000157
158 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800159}
160
Jesse Barnes8d315282011-10-16 10:23:31 +0200161/**
162 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
163 * implementing two workarounds on gen6. From section 1.4.7.1
164 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
165 *
166 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
167 * produced by non-pipelined state commands), software needs to first
168 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
169 * 0.
170 *
171 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
172 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
173 *
174 * And the workaround for these two requires this workaround first:
175 *
176 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
177 * BEFORE the pipe-control with a post-sync op and no write-cache
178 * flushes.
179 *
180 * And this last workaround is tricky because of the requirements on
181 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
182 * volume 2 part 1:
183 *
184 * "1 of the following must also be set:
185 * - Render Target Cache Flush Enable ([12] of DW1)
186 * - Depth Cache Flush Enable ([0] of DW1)
187 * - Stall at Pixel Scoreboard ([1] of DW1)
188 * - Depth Stall ([13] of DW1)
189 * - Post-Sync Operation ([13] of DW1)
190 * - Notify Enable ([8] of DW1)"
191 *
192 * The cache flushes require the workaround flush that triggered this
193 * one, so we can't use it. Depth stall would trigger the same.
194 * Post-sync nonzero is what triggered this second workaround, so we
195 * can't use that one either. Notify enable is IRQs, which aren't
196 * really our business. That leaves only stall at scoreboard.
197 */
198static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100199intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200200{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000201 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000202 u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200203 int ret;
204
John Harrison5fb9de12015-05-29 17:44:07 +0100205 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200206 if (ret)
207 return ret;
208
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000209 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(5));
210 intel_ring_emit(engine, PIPE_CONTROL_CS_STALL |
Jesse Barnes8d315282011-10-16 10:23:31 +0200211 PIPE_CONTROL_STALL_AT_SCOREBOARD);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000212 intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
213 intel_ring_emit(engine, 0); /* low dword */
214 intel_ring_emit(engine, 0); /* high dword */
215 intel_ring_emit(engine, MI_NOOP);
216 intel_ring_advance(engine);
Jesse Barnes8d315282011-10-16 10:23:31 +0200217
John Harrison5fb9de12015-05-29 17:44:07 +0100218 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200219 if (ret)
220 return ret;
221
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000222 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(5));
223 intel_ring_emit(engine, PIPE_CONTROL_QW_WRITE);
224 intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
225 intel_ring_emit(engine, 0);
226 intel_ring_emit(engine, 0);
227 intel_ring_emit(engine, MI_NOOP);
228 intel_ring_advance(engine);
Jesse Barnes8d315282011-10-16 10:23:31 +0200229
230 return 0;
231}
232
233static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100234gen6_render_ring_flush(struct drm_i915_gem_request *req,
235 u32 invalidate_domains, u32 flush_domains)
Jesse Barnes8d315282011-10-16 10:23:31 +0200236{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000237 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8d315282011-10-16 10:23:31 +0200238 u32 flags = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000239 u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200240 int ret;
241
Paulo Zanonib3111502012-08-17 18:35:42 -0300242 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100243 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300244 if (ret)
245 return ret;
246
Jesse Barnes8d315282011-10-16 10:23:31 +0200247 /* Just flush everything. Experiments have shown that reducing the
248 * number of bits based on the write domains has little performance
249 * impact.
250 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100251 if (flush_domains) {
252 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
253 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
254 /*
255 * Ensure that any following seqno writes only happen
256 * when the render cache is indeed flushed.
257 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200258 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100259 }
260 if (invalidate_domains) {
261 flags |= PIPE_CONTROL_TLB_INVALIDATE;
262 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
263 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
264 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
265 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
266 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
267 /*
268 * TLB invalidate requires a post-sync write.
269 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700270 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100271 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200272
John Harrison5fb9de12015-05-29 17:44:07 +0100273 ret = intel_ring_begin(req, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200274 if (ret)
275 return ret;
276
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000277 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
278 intel_ring_emit(engine, flags);
279 intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
280 intel_ring_emit(engine, 0);
281 intel_ring_advance(engine);
Jesse Barnes8d315282011-10-16 10:23:31 +0200282
283 return 0;
284}
285
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100286static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100287gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300288{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000289 struct intel_engine_cs *engine = req->engine;
Paulo Zanonif3987632012-08-17 18:35:43 -0300290 int ret;
291
John Harrison5fb9de12015-05-29 17:44:07 +0100292 ret = intel_ring_begin(req, 4);
Paulo Zanonif3987632012-08-17 18:35:43 -0300293 if (ret)
294 return ret;
295
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000296 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
297 intel_ring_emit(engine, PIPE_CONTROL_CS_STALL |
Paulo Zanonif3987632012-08-17 18:35:43 -0300298 PIPE_CONTROL_STALL_AT_SCOREBOARD);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000299 intel_ring_emit(engine, 0);
300 intel_ring_emit(engine, 0);
301 intel_ring_advance(engine);
Paulo Zanonif3987632012-08-17 18:35:43 -0300302
303 return 0;
304}
305
306static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100307gen7_render_ring_flush(struct drm_i915_gem_request *req,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300308 u32 invalidate_domains, u32 flush_domains)
309{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000310 struct intel_engine_cs *engine = req->engine;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300311 u32 flags = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000312 u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300313 int ret;
314
Paulo Zanonif3987632012-08-17 18:35:43 -0300315 /*
316 * Ensure that any following seqno writes only happen when the render
317 * cache is indeed flushed.
318 *
319 * Workaround: 4th PIPE_CONTROL command (except the ones with only
320 * read-cache invalidate bits set) must have the CS_STALL bit set. We
321 * don't try to be clever and just set it unconditionally.
322 */
323 flags |= PIPE_CONTROL_CS_STALL;
324
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300325 /* Just flush everything. Experiments have shown that reducing the
326 * number of bits based on the write domains has little performance
327 * impact.
328 */
329 if (flush_domains) {
330 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
331 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800332 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100333 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300334 }
335 if (invalidate_domains) {
336 flags |= PIPE_CONTROL_TLB_INVALIDATE;
337 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
338 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
339 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
340 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
341 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000342 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300343 /*
344 * TLB invalidate requires a post-sync write.
345 */
346 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200347 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300348
Chris Wilsonadd284a2014-12-16 08:44:32 +0000349 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
350
Paulo Zanonif3987632012-08-17 18:35:43 -0300351 /* Workaround: we must issue a pipe_control with CS-stall bit
352 * set before a pipe_control command that has the state cache
353 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100354 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300355 }
356
John Harrison5fb9de12015-05-29 17:44:07 +0100357 ret = intel_ring_begin(req, 4);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300358 if (ret)
359 return ret;
360
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000361 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
362 intel_ring_emit(engine, flags);
363 intel_ring_emit(engine, scratch_addr);
364 intel_ring_emit(engine, 0);
365 intel_ring_advance(engine);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300366
367 return 0;
368}
369
Ben Widawskya5f3d682013-11-02 21:07:27 -0700370static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100371gen8_emit_pipe_control(struct drm_i915_gem_request *req,
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300372 u32 flags, u32 scratch_addr)
373{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000374 struct intel_engine_cs *engine = req->engine;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300375 int ret;
376
John Harrison5fb9de12015-05-29 17:44:07 +0100377 ret = intel_ring_begin(req, 6);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300378 if (ret)
379 return ret;
380
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000381 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
382 intel_ring_emit(engine, flags);
383 intel_ring_emit(engine, scratch_addr);
384 intel_ring_emit(engine, 0);
385 intel_ring_emit(engine, 0);
386 intel_ring_emit(engine, 0);
387 intel_ring_advance(engine);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300388
389 return 0;
390}
391
392static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100393gen8_render_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700394 u32 invalidate_domains, u32 flush_domains)
395{
396 u32 flags = 0;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000397 u32 scratch_addr = req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800398 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700399
400 flags |= PIPE_CONTROL_CS_STALL;
401
402 if (flush_domains) {
403 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
404 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800405 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100406 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700407 }
408 if (invalidate_domains) {
409 flags |= PIPE_CONTROL_TLB_INVALIDATE;
410 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
411 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
412 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
413 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
414 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
415 flags |= PIPE_CONTROL_QW_WRITE;
416 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800417
418 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100419 ret = gen8_emit_pipe_control(req,
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800420 PIPE_CONTROL_CS_STALL |
421 PIPE_CONTROL_STALL_AT_SCOREBOARD,
422 0);
423 if (ret)
424 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700425 }
426
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100427 return gen8_emit_pipe_control(req, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700428}
429
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000430static void ring_write_tail(struct intel_engine_cs *engine,
Chris Wilson297b0c52010-10-22 17:02:41 +0100431 u32 value)
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;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000434 I915_WRITE_TAIL(engine, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800435}
436
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000437u64 intel_ring_get_active_head(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800438{
Chris Wilsonc0336662016-05-06 15:40:21 +0100439 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson50877442014-03-21 12:41:53 +0000440 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800441
Chris Wilsonc0336662016-05-06 15:40:21 +0100442 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000443 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
444 RING_ACTHD_UDW(engine->mmio_base));
Chris Wilsonc0336662016-05-06 15:40:21 +0100445 else if (INTEL_GEN(dev_priv) >= 4)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000446 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
Chris Wilson50877442014-03-21 12:41:53 +0000447 else
448 acthd = I915_READ(ACTHD);
449
450 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800451}
452
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000453static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200454{
Chris Wilsonc0336662016-05-06 15:40:21 +0100455 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200456 u32 addr;
457
458 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100459 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200460 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
461 I915_WRITE(HWS_PGA, addr);
462}
463
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000464static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000465{
Chris Wilsonc0336662016-05-06 15:40:21 +0100466 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200467 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000468
469 /* The ring status page addresses are no longer next to the rest of
470 * the ring registers as of gen7.
471 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100472 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000473 switch (engine->id) {
Damien Lespiauaf75f262015-02-10 19:32:17 +0000474 case RCS:
475 mmio = RENDER_HWS_PGA_GEN7;
476 break;
477 case BCS:
478 mmio = BLT_HWS_PGA_GEN7;
479 break;
480 /*
481 * VCS2 actually doesn't exist on Gen7. Only shut up
482 * gcc switch check warning
483 */
484 case VCS2:
485 case VCS:
486 mmio = BSD_HWS_PGA_GEN7;
487 break;
488 case VECS:
489 mmio = VEBOX_HWS_PGA_GEN7;
490 break;
491 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100492 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000493 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000494 } else {
495 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000496 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000497 }
498
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000499 I915_WRITE(mmio, (u32)engine->status_page.gfx_addr);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000500 POSTING_READ(mmio);
501
502 /*
503 * Flush the TLB for this page
504 *
505 * FIXME: These two bits have disappeared on gen8, so a question
506 * arises: do we still need this and if so how should we go about
507 * invalidating the TLB?
508 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100509 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000510 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000511
512 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000513 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000514
515 I915_WRITE(reg,
516 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
517 INSTPM_SYNC_FLUSH));
518 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
519 1000))
520 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000521 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000522 }
523}
524
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000525static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100526{
Chris Wilsonc0336662016-05-06 15:40:21 +0100527 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100528
Chris Wilsonc0336662016-05-06 15:40:21 +0100529 if (!IS_GEN2(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000530 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
531 if (wait_for((I915_READ_MODE(engine) & MODE_IDLE) != 0, 1000)) {
532 DRM_ERROR("%s : timed out trying to stop ring\n",
533 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100534 /* Sometimes we observe that the idle flag is not
535 * set even though the ring is empty. So double
536 * check before giving up.
537 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000538 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100539 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100540 }
541 }
542
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000543 I915_WRITE_CTL(engine, 0);
544 I915_WRITE_HEAD(engine, 0);
545 engine->write_tail(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100546
Chris Wilsonc0336662016-05-06 15:40:21 +0100547 if (!IS_GEN2(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000548 (void)I915_READ_CTL(engine);
549 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
Chris Wilson9991ae72014-04-02 16:36:07 +0100550 }
551
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000552 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100553}
554
Tomas Elffc0768c2016-03-21 16:26:59 +0000555void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
556{
557 memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
558}
559
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000560static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800561{
Chris Wilsonc0336662016-05-06 15:40:21 +0100562 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000563 struct intel_ringbuffer *ringbuf = engine->buffer;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100564 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200565 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800566
Mika Kuoppala59bad942015-01-16 11:34:40 +0200567 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200568
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000569 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100570 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000571 DRM_DEBUG_KMS("%s head not reset to zero "
572 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000573 engine->name,
574 I915_READ_CTL(engine),
575 I915_READ_HEAD(engine),
576 I915_READ_TAIL(engine),
577 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800578
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000579 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000580 DRM_ERROR("failed to set %s head to zero "
581 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000582 engine->name,
583 I915_READ_CTL(engine),
584 I915_READ_HEAD(engine),
585 I915_READ_TAIL(engine),
586 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100587 ret = -EIO;
588 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000589 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700590 }
591
Chris Wilsonc0336662016-05-06 15:40:21 +0100592 if (I915_NEED_GFX_HWS(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000593 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100594 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000595 ring_setup_phys_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100596
Jiri Kosinaece4a172014-08-07 16:29:53 +0200597 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000598 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200599
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200600 /* Initialize the ring. This must happen _after_ we've cleared the ring
601 * registers with the above sequence (the readback of the HEAD registers
602 * also enforces ordering), otherwise the hw might lose the new ring
603 * register values. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000604 I915_WRITE_START(engine, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100605
606 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000607 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100608 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000609 engine->name, I915_READ_HEAD(engine));
610 I915_WRITE_HEAD(engine, 0);
611 (void)I915_READ_HEAD(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100612
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000613 I915_WRITE_CTL(engine,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100614 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000615 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800616
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800617 /* If the head is still not zero, the ring is dead */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000618 if (wait_for((I915_READ_CTL(engine) & RING_VALID) != 0 &&
619 I915_READ_START(engine) == i915_gem_obj_ggtt_offset(obj) &&
620 (I915_READ_HEAD(engine) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000621 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100622 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000623 engine->name,
624 I915_READ_CTL(engine),
625 I915_READ_CTL(engine) & RING_VALID,
626 I915_READ_HEAD(engine), I915_READ_TAIL(engine),
627 I915_READ_START(engine),
628 (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200629 ret = -EIO;
630 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800631 }
632
Dave Gordonebd0fd42014-11-27 11:22:49 +0000633 ringbuf->last_retired_head = -1;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000634 ringbuf->head = I915_READ_HEAD(engine);
635 ringbuf->tail = I915_READ_TAIL(engine) & TAIL_ADDR;
Dave Gordonebd0fd42014-11-27 11:22:49 +0000636 intel_ring_update_space(ringbuf);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000637
Tomas Elffc0768c2016-03-21 16:26:59 +0000638 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100639
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200640out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200641 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200642
643 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700644}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800645
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100646void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000647intel_fini_pipe_control(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100648{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000649 if (engine->scratch.obj == NULL)
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100650 return;
651
Chris Wilsonc0336662016-05-06 15:40:21 +0100652 if (INTEL_GEN(engine->i915) >= 5) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000653 kunmap(sg_page(engine->scratch.obj->pages->sgl));
654 i915_gem_object_ggtt_unpin(engine->scratch.obj);
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100655 }
656
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000657 drm_gem_object_unreference(&engine->scratch.obj->base);
658 engine->scratch.obj = NULL;
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100659}
660
661int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000662intel_init_pipe_control(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000663{
Chris Wilsonc6df5412010-12-15 09:56:50 +0000664 int ret;
665
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000666 WARN_ON(engine->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000667
Chris Wilsonc0336662016-05-06 15:40:21 +0100668 engine->scratch.obj = i915_gem_object_create(engine->i915->dev, 4096);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100669 if (IS_ERR(engine->scratch.obj)) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000670 DRM_ERROR("Failed to allocate seqno page\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +0100671 ret = PTR_ERR(engine->scratch.obj);
672 engine->scratch.obj = NULL;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000673 goto err;
674 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100675
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000676 ret = i915_gem_object_set_cache_level(engine->scratch.obj,
677 I915_CACHE_LLC);
Daniel Vettera9cc7262014-02-14 14:01:13 +0100678 if (ret)
679 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000680
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000681 ret = i915_gem_obj_ggtt_pin(engine->scratch.obj, 4096, 0);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000682 if (ret)
683 goto err_unref;
684
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000685 engine->scratch.gtt_offset = i915_gem_obj_ggtt_offset(engine->scratch.obj);
686 engine->scratch.cpu_page = kmap(sg_page(engine->scratch.obj->pages->sgl));
687 if (engine->scratch.cpu_page == NULL) {
Wei Yongjun56b085a2013-05-28 17:51:44 +0800688 ret = -ENOMEM;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000689 goto err_unpin;
Wei Yongjun56b085a2013-05-28 17:51:44 +0800690 }
Chris Wilsonc6df5412010-12-15 09:56:50 +0000691
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200692 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000693 engine->name, engine->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000694 return 0;
695
696err_unpin:
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000697 i915_gem_object_ggtt_unpin(engine->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000698err_unref:
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000699 drm_gem_object_unreference(&engine->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000700err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000701 return ret;
702}
703
John Harrisone2be4fa2015-05-29 17:43:54 +0100704static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100705{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000706 struct intel_engine_cs *engine = req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +0100707 struct i915_workarounds *w = &req->i915->workarounds;
708 int ret, i;
Arun Siluvery888b5992014-08-26 14:44:51 +0100709
Francisco Jerez02235802015-10-07 14:44:01 +0300710 if (w->count == 0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300711 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100712
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000713 engine->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100714 ret = intel_ring_flush_all_caches(req);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100715 if (ret)
716 return ret;
717
John Harrison5fb9de12015-05-29 17:44:07 +0100718 ret = intel_ring_begin(req, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300719 if (ret)
720 return ret;
721
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000722 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300723 for (i = 0; i < w->count; i++) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000724 intel_ring_emit_reg(engine, w->reg[i].addr);
725 intel_ring_emit(engine, w->reg[i].value);
Mika Kuoppala72253422014-10-07 17:21:26 +0300726 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000727 intel_ring_emit(engine, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300728
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000729 intel_ring_advance(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +0300730
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000731 engine->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100732 ret = intel_ring_flush_all_caches(req);
Mika Kuoppala72253422014-10-07 17:21:26 +0300733 if (ret)
734 return ret;
735
736 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
737
738 return 0;
739}
740
John Harrison87531812015-05-29 17:43:44 +0100741static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100742{
743 int ret;
744
John Harrisone2be4fa2015-05-29 17:43:54 +0100745 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100746 if (ret != 0)
747 return ret;
748
John Harrisonbe013632015-05-29 17:43:45 +0100749 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100750 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000751 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100752
Chris Wilsone26e1b92016-01-29 16:49:05 +0000753 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100754}
755
Mika Kuoppala72253422014-10-07 17:21:26 +0300756static int wa_add(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200757 i915_reg_t addr,
758 const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300759{
760 const u32 idx = dev_priv->workarounds.count;
761
762 if (WARN_ON(idx >= I915_MAX_WA_REGS))
763 return -ENOSPC;
764
765 dev_priv->workarounds.reg[idx].addr = addr;
766 dev_priv->workarounds.reg[idx].value = val;
767 dev_priv->workarounds.reg[idx].mask = mask;
768
769 dev_priv->workarounds.count++;
770
771 return 0;
772}
773
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100774#define WA_REG(addr, mask, val) do { \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000775 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300776 if (r) \
777 return r; \
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100778 } while (0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300779
780#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000781 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300782
783#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000784 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300785
Damien Lespiau98533252014-12-08 17:33:51 +0000786#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000787 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300788
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000789#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
790#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300791
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000792#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300793
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000794static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
795 i915_reg_t reg)
Arun Siluvery33136b02016-01-21 21:43:47 +0000796{
Chris Wilsonc0336662016-05-06 15:40:21 +0100797 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery33136b02016-01-21 21:43:47 +0000798 struct i915_workarounds *wa = &dev_priv->workarounds;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000799 const uint32_t index = wa->hw_whitelist_count[engine->id];
Arun Siluvery33136b02016-01-21 21:43:47 +0000800
801 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
802 return -EINVAL;
803
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000804 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
Arun Siluvery33136b02016-01-21 21:43:47 +0000805 i915_mmio_reg_offset(reg));
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000806 wa->hw_whitelist_count[engine->id]++;
Arun Siluvery33136b02016-01-21 21:43:47 +0000807
808 return 0;
809}
810
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000811static int gen8_init_workarounds(struct intel_engine_cs *engine)
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100812{
Chris Wilsonc0336662016-05-06 15:40:21 +0100813 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery68c61982015-09-25 17:40:38 +0100814
815 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100816
Arun Siluvery717d84d2015-09-25 17:40:39 +0100817 /* WaDisableAsyncFlipPerfMode:bdw,chv */
818 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
819
Arun Siluveryd0581192015-09-25 17:40:40 +0100820 /* WaDisablePartialInstShootdown:bdw,chv */
821 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
822 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
823
Arun Siluverya340af52015-09-25 17:40:45 +0100824 /* Use Force Non-Coherent whenever executing a 3D context. This is a
825 * workaround for for a possible hang in the unlikely event a TLB
826 * invalidation occurs during a PSD flush.
827 */
828 /* WaForceEnableNonCoherent:bdw,chv */
Arun Siluvery120f5d22015-09-25 17:40:46 +0100829 /* WaHdcDisableFetchWhenMasked:bdw,chv */
Arun Siluverya340af52015-09-25 17:40:45 +0100830 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Arun Siluvery120f5d22015-09-25 17:40:46 +0100831 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Arun Siluverya340af52015-09-25 17:40:45 +0100832 HDC_FORCE_NON_COHERENT);
833
Arun Siluvery6def8fd2015-09-25 17:40:42 +0100834 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
835 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
836 * polygons in the same 8x4 pixel/sample area to be processed without
837 * stalling waiting for the earlier ones to write to Hierarchical Z
838 * buffer."
839 *
840 * This optimization is off by default for BDW and CHV; turn it on.
841 */
842 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
843
Arun Siluvery48404632015-09-25 17:40:43 +0100844 /* Wa4x4STCOptimizationDisable:bdw,chv */
845 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
846
Arun Siluvery7eebcde2015-09-25 17:40:44 +0100847 /*
848 * BSpec recommends 8x4 when MSAA is used,
849 * however in practice 16x4 seems fastest.
850 *
851 * Note that PS/WM thread counts depend on the WIZ hashing
852 * disable bit, which we don't touch here, but it's good
853 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
854 */
855 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
856 GEN6_WIZ_HASHING_MASK,
857 GEN6_WIZ_HASHING_16x4);
858
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100859 return 0;
860}
861
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000862static int bdw_init_workarounds(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +0300863{
Chris Wilsonc0336662016-05-06 15:40:21 +0100864 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100865 int ret;
Mika Kuoppala72253422014-10-07 17:21:26 +0300866
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000867 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100868 if (ret)
869 return ret;
870
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700871 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Arun Siluveryd0581192015-09-25 17:40:40 +0100872 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100873
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700874 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300875 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
876 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100877
Mika Kuoppala72253422014-10-07 17:21:26 +0300878 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
879 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100880
Mika Kuoppala72253422014-10-07 17:21:26 +0300881 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000882 /* WaForceContextSaveRestoreNonCoherent:bdw */
883 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000884 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Chris Wilsonc0336662016-05-06 15:40:21 +0100885 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100886
Arun Siluvery86d7f232014-08-26 14:44:50 +0100887 return 0;
888}
889
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000890static int chv_init_workarounds(struct intel_engine_cs *engine)
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300891{
Chris Wilsonc0336662016-05-06 15:40:21 +0100892 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100893 int ret;
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300894
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000895 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100896 if (ret)
897 return ret;
898
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300899 /* WaDisableThreadStallDopClockGating:chv */
Arun Siluveryd0581192015-09-25 17:40:40 +0100900 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300901
Kenneth Graunked60de812015-01-10 18:02:22 -0800902 /* Improve HiZ throughput on CHV. */
903 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
904
Mika Kuoppala72253422014-10-07 17:21:26 +0300905 return 0;
906}
907
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000908static int gen9_init_workarounds(struct intel_engine_cs *engine)
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000909{
Chris Wilsonc0336662016-05-06 15:40:21 +0100910 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000911 int ret;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000912
Tim Gorea8ab5ed2016-06-13 12:15:01 +0100913 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
914 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
915
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300916 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300917 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
918 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
919
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300920 /* WaDisableKillLogic:bxt,skl,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300921 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
922 ECOCHK_DIS_TLB);
923
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300924 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
925 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000926 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Tim Gore950b2aa2016-03-16 16:13:46 +0000927 FLOW_CONTROL_ENABLE |
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000928 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
929
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300930 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Nick Hoath84241712015-02-05 10:47:20 +0000931 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
932 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
933
Jani Nikulae87a0052015-10-20 15:22:02 +0300934 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100935 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
936 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Damien Lespiaua86eb582015-02-11 18:21:44 +0000937 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
938 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000939
Jani Nikulae87a0052015-10-20 15:22:02 +0300940 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100941 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
942 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Damien Lespiau183c6da2015-02-09 19:33:11 +0000943 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
944 GEN9_RHWO_OPTIMIZATION_DISABLE);
Arun Siluvery9b014352015-07-14 15:01:30 +0100945 /*
946 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
947 * but we do that in per ctx batchbuffer as there is an issue
948 * with this register not getting restored on ctx restore
949 */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000950 }
951
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300952 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl */
953 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
Tim Gorebfd8ad42016-04-19 15:45:52 +0100954 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
955 GEN9_ENABLE_YV12_BUGFIX |
956 GEN9_ENABLE_GPGPU_PREEMPTION);
Nick Hoathcac23df2015-02-05 10:47:22 +0000957
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300958 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
959 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
Arun Siluvery60294682015-09-25 14:33:37 +0100960 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
961 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
Damien Lespiau9370cd92015-02-09 19:33:17 +0000962
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300963 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000964 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
965 GEN9_CCS_TLB_PREFETCH_ENABLE);
966
Imre Deak5a2ae952015-05-19 15:04:59 +0300967 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100968 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_C0) ||
969 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200970 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
971 PIXEL_MASK_CAMMING_DISABLE);
972
Mika Kuoppala5b0e3652016-06-07 17:18:57 +0300973 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
974 WA_SET_BIT_MASKED(HDC_CHICKEN0,
975 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
976 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
Imre Deak8ea6f892015-05-19 17:05:42 +0300977
Mika Kuoppalabbaefe72016-06-07 17:18:58 +0300978 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
979 * both tied to WaForceContextSaveRestoreNonCoherent
980 * in some hsds for skl. We keep the tie for all gen9. The
981 * documentation is a bit hazy and so we want to get common behaviour,
982 * even though there is no clear evidence we would need both on kbl/bxt.
983 * This area has been source of system hangs so we play it safe
984 * and mimic the skl regardless of what bspec says.
985 *
986 * Use Force Non-Coherent whenever executing a 3D context. This
987 * is a workaround for a possible hang in the unlikely event
988 * a TLB invalidation occurs during a PSD flush.
989 */
990
991 /* WaForceEnableNonCoherent:skl,bxt,kbl */
992 WA_SET_BIT_MASKED(HDC_CHICKEN0,
993 HDC_FORCE_NON_COHERENT);
994
995 /* WaDisableHDCInvalidation:skl,bxt,kbl */
996 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
997 BDW_DISABLE_HDC_INVALIDATION);
998
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300999 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
1000 if (IS_SKYLAKE(dev_priv) ||
1001 IS_KABYLAKE(dev_priv) ||
1002 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
Arun Siluvery8c761602015-09-08 10:31:48 +01001003 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
1004 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery8c761602015-09-08 10:31:48 +01001005
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001006 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
Robert Beckett6b6d5622015-09-08 10:31:52 +01001007 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
1008
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001009 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
Arun Siluvery6ecf56a2016-01-21 21:43:54 +00001010 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
1011 GEN8_LQSC_FLUSH_COHERENT_LINES));
1012
arun.siluvery@linux.intel.com6bb628552016-06-06 09:52:49 +01001013 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
1014 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
1015 if (ret)
1016 return ret;
1017
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001018 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001019 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Arun Siluverye0f3fa02016-01-21 21:43:48 +00001020 if (ret)
1021 return ret;
1022
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001023 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001024 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
Arun Siluvery3669ab62016-01-21 21:43:49 +00001025 if (ret)
1026 return ret;
1027
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001028 return 0;
1029}
1030
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001031static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +00001032{
Chris Wilsonc0336662016-05-06 15:40:21 +01001033 struct drm_i915_private *dev_priv = engine->i915;
Damien Lespiaub7668792015-02-14 18:30:29 +00001034 u8 vals[3] = { 0, 0, 0 };
1035 unsigned int i;
1036
1037 for (i = 0; i < 3; i++) {
1038 u8 ss;
1039
1040 /*
1041 * Only consider slices where one, and only one, subslice has 7
1042 * EUs
1043 */
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +08001044 if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
Damien Lespiaub7668792015-02-14 18:30:29 +00001045 continue;
1046
1047 /*
1048 * subslice_7eu[i] != 0 (because of the check above) and
1049 * ss_max == 4 (maximum number of subslices possible per slice)
1050 *
1051 * -> 0 <= ss <= 3;
1052 */
1053 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1054 vals[i] = 3 - ss;
1055 }
1056
1057 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1058 return 0;
1059
1060 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1061 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1062 GEN9_IZ_HASHING_MASK(2) |
1063 GEN9_IZ_HASHING_MASK(1) |
1064 GEN9_IZ_HASHING_MASK(0),
1065 GEN9_IZ_HASHING(2, vals[2]) |
1066 GEN9_IZ_HASHING(1, vals[1]) |
1067 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001068
Mika Kuoppala72253422014-10-07 17:21:26 +03001069 return 0;
1070}
1071
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001072static int skl_init_workarounds(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +00001073{
Chris Wilsonc0336662016-05-06 15:40:21 +01001074 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001075 int ret;
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001076
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001077 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001078 if (ret)
1079 return ret;
Damien Lespiau8d205492015-02-09 19:33:15 +00001080
Arun Siluverya78536e2016-01-21 21:43:53 +00001081 /*
1082 * Actual WA is to disable percontext preemption granularity control
1083 * until D0 which is the default case so this is equivalent to
1084 * !WaDisablePerCtxtPreemptionGranularityControl:skl
1085 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001086 if (IS_SKL_REVID(dev_priv, SKL_REVID_E0, REVID_FOREVER)) {
Arun Siluverya78536e2016-01-21 21:43:53 +00001087 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1088 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1089 }
1090
Mika Kuoppala71dce582016-06-07 17:19:14 +03001091 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001092 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1093 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1094 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1095 }
1096
1097 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1098 * involving this register should also be added to WA batch as required.
1099 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001100 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001101 /* WaDisableLSQCROPERFforOCL:skl */
1102 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1103 GEN8_LQSC_RO_PERF_DIS);
1104
1105 /* WaEnableGapsTsvCreditFix:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001106 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, REVID_FOREVER)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001107 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1108 GEN9_GAPS_TSV_CREDIT_DISABLE));
1109 }
1110
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001111 /* WaDisablePowerCompilerClockGating:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001112 if (IS_SKL_REVID(dev_priv, SKL_REVID_B0, SKL_REVID_B0))
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001113 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1114 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1115
Jani Nikulae87a0052015-10-20 15:22:02 +03001116 /* WaBarrierPerformanceFixDisable:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001117 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_D0))
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001118 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1119 HDC_FENCE_DEST_SLM_DISABLE |
1120 HDC_BARRIER_PERFORMANCE_DISABLE);
1121
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001122 /* WaDisableSbeCacheDispatchPortSharing:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001123 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0))
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001124 WA_SET_BIT_MASKED(
1125 GEN7_HALF_SLICE_CHICKEN1,
1126 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001127
Mika Kuoppalaeee8efb2016-06-07 17:18:53 +03001128 /* WaDisableGafsUnitClkGating:skl */
1129 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1130
Arun Siluvery61074972016-01-21 21:43:52 +00001131 /* WaDisableLSQCROPERFforOCL:skl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001132 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluvery61074972016-01-21 21:43:52 +00001133 if (ret)
1134 return ret;
1135
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001136 return skl_tune_iz_hashing(engine);
Damien Lespiau8d205492015-02-09 19:33:15 +00001137}
1138
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001139static int bxt_init_workarounds(struct intel_engine_cs *engine)
Nick Hoathcae04372015-03-17 11:39:38 +02001140{
Chris Wilsonc0336662016-05-06 15:40:21 +01001141 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001142 int ret;
Nick Hoathdfb601e2015-04-10 13:12:24 +01001143
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001144 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001145 if (ret)
1146 return ret;
Nick Hoathcae04372015-03-17 11:39:38 +02001147
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001148 /* WaStoreMultiplePTEenable:bxt */
1149 /* This is a requirement according to Hardware specification */
Chris Wilsonc0336662016-05-06 15:40:21 +01001150 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001151 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1152
1153 /* WaSetClckGatingDisableMedia:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001154 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001155 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1156 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1157 }
1158
Nick Hoathdfb601e2015-04-10 13:12:24 +01001159 /* WaDisableThreadStallDopClockGating:bxt */
1160 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1161 STALL_DOP_GATING_DISABLE);
1162
arun.siluvery@linux.intel.com780f0ae2016-06-03 11:16:10 +01001163 /* WaDisablePooledEuLoadBalancingFix:bxt */
1164 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1165 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1166 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1167 }
1168
Nick Hoath983b4b92015-04-10 13:12:25 +01001169 /* WaDisableSbeCacheDispatchPortSharing:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001170 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
Nick Hoath983b4b92015-04-10 13:12:25 +01001171 WA_SET_BIT_MASKED(
1172 GEN7_HALF_SLICE_CHICKEN1,
1173 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1174 }
1175
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001176 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1177 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1178 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
Arun Siluverya786d532016-01-21 21:43:51 +00001179 /* WaDisableLSQCROPERFforOCL:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001180 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001181 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001182 if (ret)
1183 return ret;
Arun Siluverya786d532016-01-21 21:43:51 +00001184
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001185 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluverya786d532016-01-21 21:43:51 +00001186 if (ret)
1187 return ret;
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001188 }
1189
Tim Gore050fc462016-04-22 09:46:01 +01001190 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001191 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
Imre Deak36579cb2016-05-03 15:54:20 +03001192 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1193 L3_HIGH_PRIO_CREDITS(2));
Tim Gore050fc462016-04-22 09:46:01 +01001194
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001195 /* WaInsertDummyPushConstPs:bxt */
1196 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
1197 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1198 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1199
Nick Hoathcae04372015-03-17 11:39:38 +02001200 return 0;
1201}
1202
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001203static int kbl_init_workarounds(struct intel_engine_cs *engine)
1204{
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001205 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001206 int ret;
1207
1208 ret = gen9_init_workarounds(engine);
1209 if (ret)
1210 return ret;
1211
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001212 /* WaEnableGapsTsvCreditFix:kbl */
1213 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1214 GEN9_GAPS_TSV_CREDIT_DISABLE));
1215
Mika Kuoppalac0b730d2016-06-07 17:19:06 +03001216 /* WaDisableDynamicCreditSharing:kbl */
1217 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1218 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1219 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1220
Mika Kuoppala8401d422016-06-07 17:19:00 +03001221 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1222 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1223 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1224 HDC_FENCE_DEST_SLM_DISABLE);
1225
Mika Kuoppalafe905812016-06-07 17:19:03 +03001226 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1227 * involving this register should also be added to WA batch as required.
1228 */
1229 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1230 /* WaDisableLSQCROPERFforOCL:kbl */
1231 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1232 GEN8_LQSC_RO_PERF_DIS);
1233
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001234 /* WaInsertDummyPushConstPs:kbl */
1235 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1236 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1237 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1238
Mika Kuoppala4de5d7c2016-06-07 17:19:11 +03001239 /* WaDisableGafsUnitClkGating:kbl */
1240 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1241
Mika Kuoppala954337a2016-06-07 17:19:12 +03001242 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1243 WA_SET_BIT_MASKED(
1244 GEN7_HALF_SLICE_CHICKEN1,
1245 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1246
Mika Kuoppalafe905812016-06-07 17:19:03 +03001247 /* WaDisableLSQCROPERFforOCL:kbl */
1248 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1249 if (ret)
1250 return ret;
1251
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001252 return 0;
1253}
1254
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001255int init_workarounds_ring(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +03001256{
Chris Wilsonc0336662016-05-06 15:40:21 +01001257 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala72253422014-10-07 17:21:26 +03001258
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001259 WARN_ON(engine->id != RCS);
Mika Kuoppala72253422014-10-07 17:21:26 +03001260
1261 dev_priv->workarounds.count = 0;
Arun Siluvery33136b02016-01-21 21:43:47 +00001262 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
Mika Kuoppala72253422014-10-07 17:21:26 +03001263
Chris Wilsonc0336662016-05-06 15:40:21 +01001264 if (IS_BROADWELL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001265 return bdw_init_workarounds(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +03001266
Chris Wilsonc0336662016-05-06 15:40:21 +01001267 if (IS_CHERRYVIEW(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001268 return chv_init_workarounds(engine);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001269
Chris Wilsonc0336662016-05-06 15:40:21 +01001270 if (IS_SKYLAKE(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001271 return skl_init_workarounds(engine);
Nick Hoathcae04372015-03-17 11:39:38 +02001272
Chris Wilsonc0336662016-05-06 15:40:21 +01001273 if (IS_BROXTON(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001274 return bxt_init_workarounds(engine);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001275
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001276 if (IS_KABYLAKE(dev_priv))
1277 return kbl_init_workarounds(engine);
1278
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001279 return 0;
1280}
1281
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001282static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001283{
Chris Wilsonc0336662016-05-06 15:40:21 +01001284 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001285 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001286 if (ret)
1287 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001288
Akash Goel61a563a2014-03-25 18:01:50 +05301289 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001290 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001291 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001292
1293 /* We need to disable the AsyncFlip performance optimisations in order
1294 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1295 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001296 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001297 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001298 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001299 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001300 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1301
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001302 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301303 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +01001304 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001305 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001306 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001307
Akash Goel01fa0302014-03-24 23:00:04 +05301308 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +01001309 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001310 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301311 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001312 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001313
Chris Wilsonc0336662016-05-06 15:40:21 +01001314 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001315 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1316 * "If this bit is set, STCunit will have LRA as replacement
1317 * policy. [...] This bit must be reset. LRA replacement
1318 * policy is not supported."
1319 */
1320 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001321 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001322 }
1323
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001324 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001325 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001326
Chris Wilsonc0336662016-05-06 15:40:21 +01001327 if (HAS_L3_DPF(dev_priv))
1328 I915_WRITE_IMR(engine, ~GT_PARITY_ERROR(dev_priv));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001329
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001330 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001331}
1332
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001333static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001334{
Chris Wilsonc0336662016-05-06 15:40:21 +01001335 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001336
1337 if (dev_priv->semaphore_obj) {
1338 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1339 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1340 dev_priv->semaphore_obj = NULL;
1341 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001342
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001343 intel_fini_pipe_control(engine);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001344}
1345
John Harrisonf7169682015-05-29 17:44:05 +01001346static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001347 unsigned int num_dwords)
1348{
1349#define MBOX_UPDATE_DWORDS 8
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001350 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001351 struct drm_i915_private *dev_priv = signaller_req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001352 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001353 enum intel_engine_id id;
1354 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001355
Chris Wilsonc0336662016-05-06 15:40:21 +01001356 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawsky3e789982014-06-30 09:53:37 -07001357 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1358#undef MBOX_UPDATE_DWORDS
1359
John Harrison5fb9de12015-05-29 17:44:07 +01001360 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001361 if (ret)
1362 return ret;
1363
Dave Gordonc3232b12016-03-23 18:19:53 +00001364 for_each_engine_id(waiter, dev_priv, id) {
John Harrison6259cea2014-11-24 18:49:29 +00001365 u32 seqno;
Dave Gordonc3232b12016-03-23 18:19:53 +00001366 u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001367 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1368 continue;
1369
John Harrisonf7169682015-05-29 17:44:05 +01001370 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky3e789982014-06-30 09:53:37 -07001371 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1372 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1373 PIPE_CONTROL_QW_WRITE |
Chris Wilsonf9a4ea32016-04-29 13:18:24 +01001374 PIPE_CONTROL_CS_STALL);
Ben Widawsky3e789982014-06-30 09:53:37 -07001375 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1376 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001377 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001378 intel_ring_emit(signaller, 0);
1379 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
Chris Wilson215a7e32016-04-29 13:18:23 +01001380 MI_SEMAPHORE_TARGET(waiter->hw_id));
Ben Widawsky3e789982014-06-30 09:53:37 -07001381 intel_ring_emit(signaller, 0);
1382 }
1383
1384 return 0;
1385}
1386
John Harrisonf7169682015-05-29 17:44:05 +01001387static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001388 unsigned int num_dwords)
1389{
1390#define MBOX_UPDATE_DWORDS 6
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001391 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001392 struct drm_i915_private *dev_priv = signaller_req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001393 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001394 enum intel_engine_id id;
1395 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001396
Chris Wilsonc0336662016-05-06 15:40:21 +01001397 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawsky3e789982014-06-30 09:53:37 -07001398 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1399#undef MBOX_UPDATE_DWORDS
1400
John Harrison5fb9de12015-05-29 17:44:07 +01001401 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001402 if (ret)
1403 return ret;
1404
Dave Gordonc3232b12016-03-23 18:19:53 +00001405 for_each_engine_id(waiter, dev_priv, id) {
John Harrison6259cea2014-11-24 18:49:29 +00001406 u32 seqno;
Dave Gordonc3232b12016-03-23 18:19:53 +00001407 u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001408 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1409 continue;
1410
John Harrisonf7169682015-05-29 17:44:05 +01001411 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky3e789982014-06-30 09:53:37 -07001412 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1413 MI_FLUSH_DW_OP_STOREDW);
1414 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1415 MI_FLUSH_DW_USE_GTT);
1416 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001417 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001418 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
Chris Wilson215a7e32016-04-29 13:18:23 +01001419 MI_SEMAPHORE_TARGET(waiter->hw_id));
Ben Widawsky3e789982014-06-30 09:53:37 -07001420 intel_ring_emit(signaller, 0);
1421 }
1422
1423 return 0;
1424}
1425
John Harrisonf7169682015-05-29 17:44:05 +01001426static int gen6_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001427 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001428{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001429 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001430 struct drm_i915_private *dev_priv = signaller_req->i915;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001431 struct intel_engine_cs *useless;
Dave Gordonc3232b12016-03-23 18:19:53 +00001432 enum intel_engine_id id;
1433 int ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001434
Ben Widawskya1444b72014-06-30 09:53:35 -07001435#define MBOX_UPDATE_DWORDS 3
Chris Wilsonc0336662016-05-06 15:40:21 +01001436 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawskya1444b72014-06-30 09:53:35 -07001437 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1438#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001439
John Harrison5fb9de12015-05-29 17:44:07 +01001440 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky024a43e2014-04-29 14:52:30 -07001441 if (ret)
1442 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001443
Dave Gordonc3232b12016-03-23 18:19:53 +00001444 for_each_engine_id(useless, dev_priv, id) {
1445 i915_reg_t mbox_reg = signaller->semaphore.mbox.signal[id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001446
1447 if (i915_mmio_reg_valid(mbox_reg)) {
John Harrisonf7169682015-05-29 17:44:05 +01001448 u32 seqno = i915_gem_request_get_seqno(signaller_req);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001449
Ben Widawsky78325f22014-04-29 14:52:29 -07001450 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001451 intel_ring_emit_reg(signaller, mbox_reg);
John Harrison6259cea2014-11-24 18:49:29 +00001452 intel_ring_emit(signaller, seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001453 }
1454 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001455
Ben Widawskya1444b72014-06-30 09:53:35 -07001456 /* If num_dwords was rounded, make sure the tail pointer is correct */
1457 if (num_rings % 2 == 0)
1458 intel_ring_emit(signaller, MI_NOOP);
1459
Ben Widawsky024a43e2014-04-29 14:52:30 -07001460 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001461}
1462
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001463/**
1464 * gen6_add_request - Update the semaphore mailbox registers
John Harrisonee044a82015-05-29 17:44:00 +01001465 *
1466 * @request - request to write to the ring
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001467 *
1468 * Update the mailbox registers in the *other* rings with the current seqno.
1469 * This acts like a signal in the canonical semaphore.
1470 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001471static int
John Harrisonee044a82015-05-29 17:44:00 +01001472gen6_add_request(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001473{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001474 struct intel_engine_cs *engine = req->engine;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001475 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001476
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001477 if (engine->semaphore.signal)
1478 ret = engine->semaphore.signal(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001479 else
John Harrison5fb9de12015-05-29 17:44:07 +01001480 ret = intel_ring_begin(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001481
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001482 if (ret)
1483 return ret;
1484
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001485 intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
1486 intel_ring_emit(engine,
1487 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1488 intel_ring_emit(engine, i915_gem_request_get_seqno(req));
1489 intel_ring_emit(engine, MI_USER_INTERRUPT);
1490 __intel_ring_advance(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001491
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001492 return 0;
1493}
1494
Chris Wilsona58c01a2016-04-29 13:18:21 +01001495static int
1496gen8_render_add_request(struct drm_i915_gem_request *req)
1497{
1498 struct intel_engine_cs *engine = req->engine;
1499 int ret;
1500
1501 if (engine->semaphore.signal)
1502 ret = engine->semaphore.signal(req, 8);
1503 else
1504 ret = intel_ring_begin(req, 8);
1505 if (ret)
1506 return ret;
1507
1508 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
1509 intel_ring_emit(engine, (PIPE_CONTROL_GLOBAL_GTT_IVB |
1510 PIPE_CONTROL_CS_STALL |
1511 PIPE_CONTROL_QW_WRITE));
1512 intel_ring_emit(engine, intel_hws_seqno_address(req->engine));
1513 intel_ring_emit(engine, 0);
1514 intel_ring_emit(engine, i915_gem_request_get_seqno(req));
1515 /* We're thrashing one dword of HWS. */
1516 intel_ring_emit(engine, 0);
1517 intel_ring_emit(engine, MI_USER_INTERRUPT);
1518 intel_ring_emit(engine, MI_NOOP);
1519 __intel_ring_advance(engine);
1520
1521 return 0;
1522}
1523
Chris Wilsonc0336662016-05-06 15:40:21 +01001524static inline bool i915_gem_has_seqno_wrapped(struct drm_i915_private *dev_priv,
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001525 u32 seqno)
1526{
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001527 return dev_priv->last_seqno < seqno;
1528}
1529
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001530/**
1531 * intel_ring_sync - sync the waiter to the signaller on seqno
1532 *
1533 * @waiter - ring that is waiting
1534 * @signaller - ring which has, or will signal
1535 * @seqno - seqno which the waiter will block on
1536 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001537
1538static int
John Harrison599d9242015-05-29 17:44:04 +01001539gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001540 struct intel_engine_cs *signaller,
1541 u32 seqno)
1542{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001543 struct intel_engine_cs *waiter = waiter_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001544 struct drm_i915_private *dev_priv = waiter_req->i915;
Chris Wilson6ef48d72016-04-29 13:18:25 +01001545 struct i915_hw_ppgtt *ppgtt;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001546 int ret;
1547
John Harrison5fb9de12015-05-29 17:44:07 +01001548 ret = intel_ring_begin(waiter_req, 4);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001549 if (ret)
1550 return ret;
1551
1552 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1553 MI_SEMAPHORE_GLOBAL_GTT |
1554 MI_SEMAPHORE_SAD_GTE_SDD);
1555 intel_ring_emit(waiter, seqno);
1556 intel_ring_emit(waiter,
1557 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1558 intel_ring_emit(waiter,
1559 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1560 intel_ring_advance(waiter);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001561
1562 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1563 * pagetables and we must reload them before executing the batch.
1564 * We do this on the i915_switch_context() following the wait and
1565 * before the dispatch.
1566 */
1567 ppgtt = waiter_req->ctx->ppgtt;
1568 if (ppgtt && waiter_req->engine->id != RCS)
1569 ppgtt->pd_dirty_rings |= intel_engine_flag(waiter_req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001570 return 0;
1571}
1572
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001573static int
John Harrison599d9242015-05-29 17:44:04 +01001574gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001575 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001576 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001577{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001578 struct intel_engine_cs *waiter = waiter_req->engine;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001579 u32 dw1 = MI_SEMAPHORE_MBOX |
1580 MI_SEMAPHORE_COMPARE |
1581 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001582 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1583 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001584
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001585 /* Throughout all of the GEM code, seqno passed implies our current
1586 * seqno is >= the last seqno executed. However for hardware the
1587 * comparison is strictly greater than.
1588 */
1589 seqno -= 1;
1590
Ben Widawskyebc348b2014-04-29 14:52:28 -07001591 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001592
John Harrison5fb9de12015-05-29 17:44:07 +01001593 ret = intel_ring_begin(waiter_req, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001594 if (ret)
1595 return ret;
1596
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001597 /* If seqno wrap happened, omit the wait with no-ops */
Chris Wilsonc0336662016-05-06 15:40:21 +01001598 if (likely(!i915_gem_has_seqno_wrapped(waiter_req->i915, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001599 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001600 intel_ring_emit(waiter, seqno);
1601 intel_ring_emit(waiter, 0);
1602 intel_ring_emit(waiter, MI_NOOP);
1603 } else {
1604 intel_ring_emit(waiter, MI_NOOP);
1605 intel_ring_emit(waiter, MI_NOOP);
1606 intel_ring_emit(waiter, MI_NOOP);
1607 intel_ring_emit(waiter, MI_NOOP);
1608 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001609 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001610
1611 return 0;
1612}
1613
Chris Wilsonc6df5412010-12-15 09:56:50 +00001614#define PIPE_CONTROL_FLUSH(ring__, addr__) \
1615do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001616 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1617 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +00001618 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1619 intel_ring_emit(ring__, 0); \
1620 intel_ring_emit(ring__, 0); \
1621} while (0)
1622
1623static int
John Harrisonee044a82015-05-29 17:44:00 +01001624pc_render_add_request(struct drm_i915_gem_request *req)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001625{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001626 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001627 u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001628 int ret;
1629
1630 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1631 * incoherent with writes to memory, i.e. completely fubar,
1632 * so we need to use PIPE_NOTIFY instead.
1633 *
1634 * However, we also need to workaround the qword write
1635 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1636 * memory before requesting an interrupt.
1637 */
John Harrison5fb9de12015-05-29 17:44:07 +01001638 ret = intel_ring_begin(req, 32);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001639 if (ret)
1640 return ret;
1641
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001642 intel_ring_emit(engine,
1643 GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001644 PIPE_CONTROL_WRITE_FLUSH |
1645 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001646 intel_ring_emit(engine,
1647 engine->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1648 intel_ring_emit(engine, i915_gem_request_get_seqno(req));
1649 intel_ring_emit(engine, 0);
1650 PIPE_CONTROL_FLUSH(engine, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001651 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001652 PIPE_CONTROL_FLUSH(engine, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001653 scratch_addr += 2 * CACHELINE_BYTES;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001654 PIPE_CONTROL_FLUSH(engine, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001655 scratch_addr += 2 * CACHELINE_BYTES;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001656 PIPE_CONTROL_FLUSH(engine, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001657 scratch_addr += 2 * CACHELINE_BYTES;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001658 PIPE_CONTROL_FLUSH(engine, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001659 scratch_addr += 2 * CACHELINE_BYTES;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001660 PIPE_CONTROL_FLUSH(engine, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001661
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001662 intel_ring_emit(engine,
1663 GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001664 PIPE_CONTROL_WRITE_FLUSH |
1665 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001666 PIPE_CONTROL_NOTIFY);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001667 intel_ring_emit(engine,
1668 engine->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1669 intel_ring_emit(engine, i915_gem_request_get_seqno(req));
1670 intel_ring_emit(engine, 0);
1671 __intel_ring_advance(engine);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001672
Chris Wilsonc6df5412010-12-15 09:56:50 +00001673 return 0;
1674}
1675
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001676static void
1677gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001678{
Chris Wilsonc0336662016-05-06 15:40:21 +01001679 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001680
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001681 /* Workaround to force correct ordering between irq and seqno writes on
1682 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +01001683 * ACTHD) before reading the status page.
1684 *
1685 * Note that this effectively stalls the read by the time it takes to
1686 * do a memory transaction, which more or less ensures that the write
1687 * from the GPU has sufficient time to invalidate the CPU cacheline.
1688 * Alternatively we could delay the interrupt from the CS ring to give
1689 * the write time to land, but that would incur a delay after every
1690 * batch i.e. much more frequent than a delay when waiting for the
1691 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001692 *
1693 * Also note that to prevent whole machine hangs on gen7, we have to
1694 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +01001695 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001696 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001697 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001698 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001699}
1700
1701static u32
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001702ring_get_seqno(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001703{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001704 return intel_read_status_page(engine, I915_GEM_HWS_INDEX);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001705}
1706
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001707static void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001708ring_set_seqno(struct intel_engine_cs *engine, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001709{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001710 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001711}
1712
Chris Wilsonc6df5412010-12-15 09:56:50 +00001713static u32
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001714pc_render_get_seqno(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001715{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001716 return engine->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001717}
1718
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001719static void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001720pc_render_set_seqno(struct intel_engine_cs *engine, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001721{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001722 engine->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001723}
1724
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001725static bool
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001726gen5_ring_get_irq(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001727{
Chris Wilsonc0336662016-05-06 15:40:21 +01001728 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7338aef2012-04-24 21:48:47 +01001729 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001730
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001731 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Daniel Vettere48d8632012-04-11 22:12:54 +02001732 return false;
1733
Chris Wilson7338aef2012-04-24 21:48:47 +01001734 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001735 if (engine->irq_refcount++ == 0)
1736 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001737 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001738
1739 return true;
1740}
1741
1742static void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001743gen5_ring_put_irq(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001744{
Chris Wilsonc0336662016-05-06 15:40:21 +01001745 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7338aef2012-04-24 21:48:47 +01001746 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001747
Chris Wilson7338aef2012-04-24 21:48:47 +01001748 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001749 if (--engine->irq_refcount == 0)
1750 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001751 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001752}
1753
1754static bool
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001755i9xx_ring_get_irq(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001756{
Chris Wilsonc0336662016-05-06 15:40:21 +01001757 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7338aef2012-04-24 21:48:47 +01001758 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001759
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001760 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001761 return false;
1762
Chris Wilson7338aef2012-04-24 21:48:47 +01001763 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001764 if (engine->irq_refcount++ == 0) {
1765 dev_priv->irq_mask &= ~engine->irq_enable_mask;
Daniel Vetterf637fde2012-04-11 22:12:59 +02001766 I915_WRITE(IMR, dev_priv->irq_mask);
1767 POSTING_READ(IMR);
1768 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001769 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001770
1771 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001772}
1773
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001774static void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001775i9xx_ring_put_irq(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001776{
Chris Wilsonc0336662016-05-06 15:40:21 +01001777 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7338aef2012-04-24 21:48:47 +01001778 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001779
Chris Wilson7338aef2012-04-24 21:48:47 +01001780 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001781 if (--engine->irq_refcount == 0) {
1782 dev_priv->irq_mask |= engine->irq_enable_mask;
Daniel Vetterf637fde2012-04-11 22:12:59 +02001783 I915_WRITE(IMR, dev_priv->irq_mask);
1784 POSTING_READ(IMR);
1785 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001786 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001787}
1788
Chris Wilsonc2798b12012-04-22 21:13:57 +01001789static bool
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001790i8xx_ring_get_irq(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001791{
Chris Wilsonc0336662016-05-06 15:40:21 +01001792 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7338aef2012-04-24 21:48:47 +01001793 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001794
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001795 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonc2798b12012-04-22 21:13:57 +01001796 return false;
1797
Chris Wilson7338aef2012-04-24 21:48:47 +01001798 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001799 if (engine->irq_refcount++ == 0) {
1800 dev_priv->irq_mask &= ~engine->irq_enable_mask;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001801 I915_WRITE16(IMR, dev_priv->irq_mask);
1802 POSTING_READ16(IMR);
1803 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001804 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001805
1806 return true;
1807}
1808
1809static void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001810i8xx_ring_put_irq(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001811{
Chris Wilsonc0336662016-05-06 15:40:21 +01001812 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7338aef2012-04-24 21:48:47 +01001813 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001814
Chris Wilson7338aef2012-04-24 21:48:47 +01001815 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001816 if (--engine->irq_refcount == 0) {
1817 dev_priv->irq_mask |= engine->irq_enable_mask;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001818 I915_WRITE16(IMR, dev_priv->irq_mask);
1819 POSTING_READ16(IMR);
1820 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001821 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001822}
1823
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001824static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001825bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001826 u32 invalidate_domains,
1827 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001828{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001829 struct intel_engine_cs *engine = req->engine;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001830 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001831
John Harrison5fb9de12015-05-29 17:44:07 +01001832 ret = intel_ring_begin(req, 2);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001833 if (ret)
1834 return ret;
1835
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001836 intel_ring_emit(engine, MI_FLUSH);
1837 intel_ring_emit(engine, MI_NOOP);
1838 intel_ring_advance(engine);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001839 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001840}
1841
Chris Wilson3cce4692010-10-27 16:11:02 +01001842static int
John Harrisonee044a82015-05-29 17:44:00 +01001843i9xx_add_request(struct drm_i915_gem_request *req)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001844{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001845 struct intel_engine_cs *engine = req->engine;
Chris Wilson3cce4692010-10-27 16:11:02 +01001846 int ret;
1847
John Harrison5fb9de12015-05-29 17:44:07 +01001848 ret = intel_ring_begin(req, 4);
Chris Wilson3cce4692010-10-27 16:11:02 +01001849 if (ret)
1850 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +01001851
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001852 intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
1853 intel_ring_emit(engine,
1854 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1855 intel_ring_emit(engine, i915_gem_request_get_seqno(req));
1856 intel_ring_emit(engine, MI_USER_INTERRUPT);
1857 __intel_ring_advance(engine);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001858
Chris Wilson3cce4692010-10-27 16:11:02 +01001859 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001860}
1861
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001862static bool
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001863gen6_ring_get_irq(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001864{
Chris Wilsonc0336662016-05-06 15:40:21 +01001865 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7338aef2012-04-24 21:48:47 +01001866 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001867
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001868 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1869 return false;
Chris Wilson0f468322011-01-04 17:35:21 +00001870
Chris Wilson7338aef2012-04-24 21:48:47 +01001871 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001872 if (engine->irq_refcount++ == 0) {
Chris Wilsonc0336662016-05-06 15:40:21 +01001873 if (HAS_L3_DPF(dev_priv) && engine->id == RCS)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001874 I915_WRITE_IMR(engine,
1875 ~(engine->irq_enable_mask |
Chris Wilsonc0336662016-05-06 15:40:21 +01001876 GT_PARITY_ERROR(dev_priv)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001877 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001878 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
1879 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001880 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001881 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001882
1883 return true;
1884}
1885
1886static void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001887gen6_ring_put_irq(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001888{
Chris Wilsonc0336662016-05-06 15:40:21 +01001889 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7338aef2012-04-24 21:48:47 +01001890 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001891
Chris Wilson7338aef2012-04-24 21:48:47 +01001892 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001893 if (--engine->irq_refcount == 0) {
Chris Wilsonc0336662016-05-06 15:40:21 +01001894 if (HAS_L3_DPF(dev_priv) && engine->id == RCS)
1895 I915_WRITE_IMR(engine, ~GT_PARITY_ERROR(dev_priv));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001896 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001897 I915_WRITE_IMR(engine, ~0);
1898 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001899 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001900 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001901}
1902
Ben Widawskya19d2932013-05-28 19:22:30 -07001903static bool
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001904hsw_vebox_get_irq(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001905{
Chris Wilsonc0336662016-05-06 15:40:21 +01001906 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001907 unsigned long flags;
1908
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001909 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskya19d2932013-05-28 19:22:30 -07001910 return false;
1911
Daniel Vetter59cdb632013-07-04 23:35:28 +02001912 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001913 if (engine->irq_refcount++ == 0) {
1914 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
1915 gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001916 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001917 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001918
1919 return true;
1920}
1921
1922static void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001923hsw_vebox_put_irq(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001924{
Chris Wilsonc0336662016-05-06 15:40:21 +01001925 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001926 unsigned long flags;
1927
Daniel Vetter59cdb632013-07-04 23:35:28 +02001928 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001929 if (--engine->irq_refcount == 0) {
1930 I915_WRITE_IMR(engine, ~0);
1931 gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001932 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001933 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001934}
1935
Ben Widawskyabd58f02013-11-02 21:07:09 -07001936static bool
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001937gen8_ring_get_irq(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001938{
Chris Wilsonc0336662016-05-06 15:40:21 +01001939 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001940 unsigned long flags;
1941
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001942 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskyabd58f02013-11-02 21:07:09 -07001943 return false;
1944
1945 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001946 if (engine->irq_refcount++ == 0) {
Chris Wilsonc0336662016-05-06 15:40:21 +01001947 if (HAS_L3_DPF(dev_priv) && engine->id == RCS) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001948 I915_WRITE_IMR(engine,
1949 ~(engine->irq_enable_mask |
Ben Widawskyabd58f02013-11-02 21:07:09 -07001950 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1951 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001952 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001953 }
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001954 POSTING_READ(RING_IMR(engine->mmio_base));
Ben Widawskyabd58f02013-11-02 21:07:09 -07001955 }
1956 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1957
1958 return true;
1959}
1960
1961static void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001962gen8_ring_put_irq(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001963{
Chris Wilsonc0336662016-05-06 15:40:21 +01001964 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001965 unsigned long flags;
1966
1967 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001968 if (--engine->irq_refcount == 0) {
Chris Wilsonc0336662016-05-06 15:40:21 +01001969 if (HAS_L3_DPF(dev_priv) && engine->id == RCS) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001970 I915_WRITE_IMR(engine,
Ben Widawskyabd58f02013-11-02 21:07:09 -07001971 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1972 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001973 I915_WRITE_IMR(engine, ~0);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001974 }
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001975 POSTING_READ(RING_IMR(engine->mmio_base));
Ben Widawskyabd58f02013-11-02 21:07:09 -07001976 }
1977 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1978}
1979
Zou Nan haid1b851f2010-05-21 09:08:57 +08001980static int
John Harrison53fddaf2015-05-29 17:44:02 +01001981i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001982 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001983 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001984{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001985 struct intel_engine_cs *engine = req->engine;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001986 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001987
John Harrison5fb9de12015-05-29 17:44:07 +01001988 ret = intel_ring_begin(req, 2);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001989 if (ret)
1990 return ret;
1991
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001992 intel_ring_emit(engine,
Chris Wilson65f56872012-04-17 16:38:12 +01001993 MI_BATCH_BUFFER_START |
1994 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001995 (dispatch_flags & I915_DISPATCH_SECURE ?
1996 0 : MI_BATCH_NON_SECURE_I965));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001997 intel_ring_emit(engine, offset);
1998 intel_ring_advance(engine);
Chris Wilson78501ea2010-10-27 12:18:21 +01001999
Zou Nan haid1b851f2010-05-21 09:08:57 +08002000 return 0;
2001}
2002
Daniel Vetterb45305f2012-12-17 16:21:27 +01002003/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
2004#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002005#define I830_TLB_ENTRIES (2)
2006#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002007static int
John Harrison53fddaf2015-05-29 17:44:02 +01002008i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002009 u64 offset, u32 len,
2010 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002011{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002012 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002013 u32 cs_offset = engine->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00002014 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002015
John Harrison5fb9de12015-05-29 17:44:07 +01002016 ret = intel_ring_begin(req, 6);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002017 if (ret)
2018 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002019
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002020 /* Evict the invalid PTE TLBs */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002021 intel_ring_emit(engine, COLOR_BLT_CMD | BLT_WRITE_RGBA);
2022 intel_ring_emit(engine, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
2023 intel_ring_emit(engine, I830_TLB_ENTRIES << 16 | 4); /* load each page */
2024 intel_ring_emit(engine, cs_offset);
2025 intel_ring_emit(engine, 0xdeadbeef);
2026 intel_ring_emit(engine, MI_NOOP);
2027 intel_ring_advance(engine);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002028
John Harrison8e004ef2015-02-13 11:48:10 +00002029 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01002030 if (len > I830_BATCH_LIMIT)
2031 return -ENOSPC;
2032
John Harrison5fb9de12015-05-29 17:44:07 +01002033 ret = intel_ring_begin(req, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002034 if (ret)
2035 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002036
2037 /* Blit the batch (which has now all relocs applied) to the
2038 * stable batch scratch bo area (so that the CS never
2039 * stumbles over its tlb invalidation bug) ...
2040 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002041 intel_ring_emit(engine, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
2042 intel_ring_emit(engine,
2043 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
2044 intel_ring_emit(engine, DIV_ROUND_UP(len, 4096) << 16 | 4096);
2045 intel_ring_emit(engine, cs_offset);
2046 intel_ring_emit(engine, 4096);
2047 intel_ring_emit(engine, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002048
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002049 intel_ring_emit(engine, MI_FLUSH);
2050 intel_ring_emit(engine, MI_NOOP);
2051 intel_ring_advance(engine);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002052
2053 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002054 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01002055 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002056
Ville Syrjälä9d611c02015-12-14 18:23:49 +02002057 ret = intel_ring_begin(req, 2);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002058 if (ret)
2059 return ret;
2060
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002061 intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
2062 intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
2063 0 : MI_BATCH_NON_SECURE));
2064 intel_ring_advance(engine);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002065
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002066 return 0;
2067}
2068
2069static int
John Harrison53fddaf2015-05-29 17:44:02 +01002070i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002071 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002072 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002073{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002074 struct intel_engine_cs *engine = req->engine;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002075 int ret;
2076
John Harrison5fb9de12015-05-29 17:44:07 +01002077 ret = intel_ring_begin(req, 2);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002078 if (ret)
2079 return ret;
2080
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002081 intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
2082 intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
2083 0 : MI_BATCH_NON_SECURE));
2084 intel_ring_advance(engine);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002085
Eric Anholt62fdfea2010-05-21 13:26:39 -07002086 return 0;
2087}
2088
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002089static void cleanup_phys_status_page(struct intel_engine_cs *engine)
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002090{
Chris Wilsonc0336662016-05-06 15:40:21 +01002091 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002092
2093 if (!dev_priv->status_page_dmah)
2094 return;
2095
Chris Wilsonc0336662016-05-06 15:40:21 +01002096 drm_pci_free(dev_priv->dev, dev_priv->status_page_dmah);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002097 engine->status_page.page_addr = NULL;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002098}
2099
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002100static void cleanup_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002101{
Chris Wilson05394f32010-11-08 19:18:58 +00002102 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002103
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002104 obj = engine->status_page.obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002105 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002106 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002107
Chris Wilson9da3da62012-06-01 15:20:22 +01002108 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08002109 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002110 drm_gem_object_unreference(&obj->base);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002111 engine->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002112}
2113
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002114static int init_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002115{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002116 struct drm_i915_gem_object *obj = engine->status_page.obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002117
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002118 if (obj == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04002119 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01002120 int ret;
2121
Chris Wilsonc0336662016-05-06 15:40:21 +01002122 obj = i915_gem_object_create(engine->i915->dev, 4096);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002123 if (IS_ERR(obj)) {
Chris Wilsone3efda42014-04-09 09:19:41 +01002124 DRM_ERROR("Failed to allocate status page\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002125 return PTR_ERR(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01002126 }
2127
2128 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2129 if (ret)
2130 goto err_unref;
2131
Chris Wilson1f767e02014-07-03 17:33:03 -04002132 flags = 0;
Chris Wilsonc0336662016-05-06 15:40:21 +01002133 if (!HAS_LLC(engine->i915))
Chris Wilson1f767e02014-07-03 17:33:03 -04002134 /* On g33, we cannot place HWS above 256MiB, so
2135 * restrict its pinning to the low mappable arena.
2136 * Though this restriction is not documented for
2137 * gen4, gen5, or byt, they also behave similarly
2138 * and hang if the HWS is placed at the top of the
2139 * GTT. To generalise, it appears that all !llc
2140 * platforms have issues with us placing the HWS
2141 * above the mappable region (even though we never
2142 * actualy map it).
2143 */
2144 flags |= PIN_MAPPABLE;
2145 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01002146 if (ret) {
2147err_unref:
2148 drm_gem_object_unreference(&obj->base);
2149 return ret;
2150 }
2151
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002152 engine->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002153 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01002154
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002155 engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
2156 engine->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
2157 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002158
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002159 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002160 engine->name, engine->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002161
2162 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002163}
2164
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002165static int init_phys_status_page(struct intel_engine_cs *engine)
Chris Wilson6b8294a2012-11-16 11:43:20 +00002166{
Chris Wilsonc0336662016-05-06 15:40:21 +01002167 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002168
2169 if (!dev_priv->status_page_dmah) {
2170 dev_priv->status_page_dmah =
Chris Wilsonc0336662016-05-06 15:40:21 +01002171 drm_pci_alloc(dev_priv->dev, PAGE_SIZE, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002172 if (!dev_priv->status_page_dmah)
2173 return -ENOMEM;
2174 }
2175
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002176 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
2177 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002178
2179 return 0;
2180}
2181
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002182void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
2183{
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002184 GEM_BUG_ON(ringbuf->vma == NULL);
2185 GEM_BUG_ON(ringbuf->virtual_start == NULL);
2186
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002187 if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002188 i915_gem_object_unpin_map(ringbuf->obj);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002189 else
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002190 i915_vma_unpin_iomap(ringbuf->vma);
Dave Gordon83052162016-04-12 14:46:16 +01002191 ringbuf->virtual_start = NULL;
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002192
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002193 i915_gem_object_ggtt_unpin(ringbuf->obj);
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002194 ringbuf->vma = NULL;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002195}
2196
Chris Wilsonc0336662016-05-06 15:40:21 +01002197int intel_pin_and_map_ringbuffer_obj(struct drm_i915_private *dev_priv,
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002198 struct intel_ringbuffer *ringbuf)
2199{
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002200 struct drm_i915_gem_object *obj = ringbuf->obj;
Chris Wilsona687a432016-04-13 17:35:11 +01002201 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
2202 unsigned flags = PIN_OFFSET_BIAS | 4096;
Dave Gordon83052162016-04-12 14:46:16 +01002203 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002204 int ret;
2205
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002206 if (HAS_LLC(dev_priv) && !obj->stolen) {
Chris Wilsona687a432016-04-13 17:35:11 +01002207 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002208 if (ret)
2209 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002210
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002211 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsond2cad532016-04-08 12:11:10 +01002212 if (ret)
2213 goto err_unpin;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002214
Dave Gordon83052162016-04-12 14:46:16 +01002215 addr = i915_gem_object_pin_map(obj);
2216 if (IS_ERR(addr)) {
2217 ret = PTR_ERR(addr);
Chris Wilsond2cad532016-04-08 12:11:10 +01002218 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002219 }
2220 } else {
Chris Wilsona687a432016-04-13 17:35:11 +01002221 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
2222 flags | PIN_MAPPABLE);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002223 if (ret)
2224 return ret;
2225
2226 ret = i915_gem_object_set_to_gtt_domain(obj, true);
Chris Wilsond2cad532016-04-08 12:11:10 +01002227 if (ret)
2228 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002229
Daniele Ceraolo Spurioff3dc082016-01-27 15:43:49 +00002230 /* Access through the GTT requires the device to be awake. */
2231 assert_rpm_wakelock_held(dev_priv);
2232
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002233 addr = i915_vma_pin_iomap(i915_gem_obj_to_ggtt(obj));
2234 if (IS_ERR(addr)) {
2235 ret = PTR_ERR(addr);
Chris Wilsond2cad532016-04-08 12:11:10 +01002236 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002237 }
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002238 }
2239
Dave Gordon83052162016-04-12 14:46:16 +01002240 ringbuf->virtual_start = addr;
Tvrtko Ursulin0eb973d2016-01-15 15:10:28 +00002241 ringbuf->vma = i915_gem_obj_to_ggtt(obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002242 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01002243
2244err_unpin:
2245 i915_gem_object_ggtt_unpin(obj);
2246 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002247}
2248
Chris Wilson01101fa2015-09-03 13:01:39 +01002249static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01002250{
Oscar Mateo2919d292014-07-03 16:28:02 +01002251 drm_gem_object_unreference(&ringbuf->obj->base);
2252 ringbuf->obj = NULL;
2253}
2254
Chris Wilson01101fa2015-09-03 13:01:39 +01002255static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
2256 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01002257{
Chris Wilsone3efda42014-04-09 09:19:41 +01002258 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002259
2260 obj = NULL;
2261 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002262 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002263 if (obj == NULL)
Dave Gordond37cd8a2016-04-22 19:14:32 +01002264 obj = i915_gem_object_create(dev, ringbuf->size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002265 if (IS_ERR(obj))
2266 return PTR_ERR(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01002267
Akash Goel24f3a8c2014-06-17 10:59:42 +05302268 /* mark ring buffers as read-only from GPU side by default */
2269 obj->gt_ro = 1;
2270
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002271 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002272
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002273 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002274}
2275
Chris Wilson01101fa2015-09-03 13:01:39 +01002276struct intel_ringbuffer *
2277intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
2278{
2279 struct intel_ringbuffer *ring;
2280 int ret;
2281
2282 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson608c1a52015-09-03 13:01:40 +01002283 if (ring == NULL) {
2284 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2285 engine->name);
Chris Wilson01101fa2015-09-03 13:01:39 +01002286 return ERR_PTR(-ENOMEM);
Chris Wilson608c1a52015-09-03 13:01:40 +01002287 }
Chris Wilson01101fa2015-09-03 13:01:39 +01002288
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002289 ring->engine = engine;
Chris Wilson608c1a52015-09-03 13:01:40 +01002290 list_add(&ring->link, &engine->buffers);
Chris Wilson01101fa2015-09-03 13:01:39 +01002291
2292 ring->size = size;
2293 /* Workaround an erratum on the i830 which causes a hang if
2294 * the TAIL pointer points to within the last 2 cachelines
2295 * of the buffer.
2296 */
2297 ring->effective_size = size;
Chris Wilsonc0336662016-05-06 15:40:21 +01002298 if (IS_I830(engine->i915) || IS_845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01002299 ring->effective_size -= 2 * CACHELINE_BYTES;
2300
2301 ring->last_retired_head = -1;
2302 intel_ring_update_space(ring);
2303
Chris Wilsonc0336662016-05-06 15:40:21 +01002304 ret = intel_alloc_ringbuffer_obj(engine->i915->dev, ring);
Chris Wilson01101fa2015-09-03 13:01:39 +01002305 if (ret) {
Chris Wilson608c1a52015-09-03 13:01:40 +01002306 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
2307 engine->name, ret);
2308 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002309 kfree(ring);
2310 return ERR_PTR(ret);
2311 }
2312
2313 return ring;
2314}
2315
2316void
2317intel_ringbuffer_free(struct intel_ringbuffer *ring)
2318{
2319 intel_destroy_ringbuffer_obj(ring);
Chris Wilson608c1a52015-09-03 13:01:40 +01002320 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002321 kfree(ring);
2322}
2323
Chris Wilson0cb26a82016-06-24 14:55:53 +01002324static int intel_ring_context_pin(struct i915_gem_context *ctx,
2325 struct intel_engine_cs *engine)
2326{
2327 struct intel_context *ce = &ctx->engine[engine->id];
2328 int ret;
2329
2330 lockdep_assert_held(&ctx->i915->dev->struct_mutex);
2331
2332 if (ce->pin_count++)
2333 return 0;
2334
2335 if (ce->state) {
2336 ret = i915_gem_obj_ggtt_pin(ce->state, ctx->ggtt_alignment, 0);
2337 if (ret)
2338 goto error;
2339 }
2340
Chris Wilsonc7c3c072016-06-24 14:55:54 +01002341 /* The kernel context is only used as a placeholder for flushing the
2342 * active context. It is never used for submitting user rendering and
2343 * as such never requires the golden render context, and so we can skip
2344 * emitting it when we switch to the kernel context. This is required
2345 * as during eviction we cannot allocate and pin the renderstate in
2346 * order to initialise the context.
2347 */
2348 if (ctx == ctx->i915->kernel_context)
2349 ce->initialised = true;
2350
Chris Wilson0cb26a82016-06-24 14:55:53 +01002351 i915_gem_context_reference(ctx);
2352 return 0;
2353
2354error:
2355 ce->pin_count = 0;
2356 return ret;
2357}
2358
2359static void intel_ring_context_unpin(struct i915_gem_context *ctx,
2360 struct intel_engine_cs *engine)
2361{
2362 struct intel_context *ce = &ctx->engine[engine->id];
2363
2364 lockdep_assert_held(&ctx->i915->dev->struct_mutex);
2365
2366 if (--ce->pin_count)
2367 return;
2368
2369 if (ce->state)
2370 i915_gem_object_ggtt_unpin(ce->state);
2371
2372 i915_gem_context_unreference(ctx);
2373}
2374
Ben Widawskyc43b5632012-04-16 14:07:40 -07002375static int intel_init_ring_buffer(struct drm_device *dev,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002376 struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002377{
Chris Wilsonc0336662016-05-06 15:40:21 +01002378 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002379 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002380 int ret;
2381
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002382 WARN_ON(engine->buffer);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002383
Chris Wilsonc0336662016-05-06 15:40:21 +01002384 engine->i915 = dev_priv;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002385 INIT_LIST_HEAD(&engine->active_list);
2386 INIT_LIST_HEAD(&engine->request_list);
2387 INIT_LIST_HEAD(&engine->execlist_queue);
2388 INIT_LIST_HEAD(&engine->buffers);
2389 i915_gem_batch_pool_init(dev, &engine->batch_pool);
2390 memset(engine->semaphore.sync_seqno, 0,
2391 sizeof(engine->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002392
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002393 init_waitqueue_head(&engine->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002394
Chris Wilson0cb26a82016-06-24 14:55:53 +01002395 /* We may need to do things with the shrinker which
2396 * require us to immediately switch back to the default
2397 * context. This can cause a problem as pinning the
2398 * default context also requires GTT space which may not
2399 * be available. To avoid this we always pin the default
2400 * context.
2401 */
2402 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2403 if (ret)
2404 goto error;
2405
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002406 ringbuf = intel_engine_create_ringbuffer(engine, 32 * PAGE_SIZE);
Dave Gordonb0366a52015-12-08 15:02:36 +00002407 if (IS_ERR(ringbuf)) {
2408 ret = PTR_ERR(ringbuf);
2409 goto error;
2410 }
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002411 engine->buffer = ringbuf;
Chris Wilson01101fa2015-09-03 13:01:39 +01002412
Chris Wilsonc0336662016-05-06 15:40:21 +01002413 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002414 ret = init_status_page(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002415 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002416 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002417 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002418 WARN_ON(engine->id != RCS);
2419 ret = init_phys_status_page(engine);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002420 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002421 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002422 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002423
Chris Wilsonc0336662016-05-06 15:40:21 +01002424 ret = intel_pin_and_map_ringbuffer_obj(dev_priv, ringbuf);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002425 if (ret) {
2426 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002427 engine->name, ret);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002428 intel_destroy_ringbuffer_obj(ringbuf);
2429 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002430 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002431
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002432 ret = i915_cmd_parser_init_ring(engine);
Brad Volkin44e895a2014-05-10 14:10:43 -07002433 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002434 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002435
Oscar Mateo8ee14972014-05-22 14:13:34 +01002436 return 0;
2437
2438error:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002439 intel_cleanup_engine(engine);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002440 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002441}
2442
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002443void intel_cleanup_engine(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002444{
John Harrison6402c332014-10-31 12:00:26 +00002445 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01002446
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002447 if (!intel_engine_initialized(engine))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002448 return;
2449
Chris Wilsonc0336662016-05-06 15:40:21 +01002450 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00002451
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002452 if (engine->buffer) {
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002453 intel_stop_engine(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01002454 WARN_ON(!IS_GEN2(dev_priv) && (I915_READ_MODE(engine) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002455
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002456 intel_unpin_ringbuffer_obj(engine->buffer);
2457 intel_ringbuffer_free(engine->buffer);
2458 engine->buffer = NULL;
Dave Gordonb0366a52015-12-08 15:02:36 +00002459 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002460
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002461 if (engine->cleanup)
2462 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08002463
Chris Wilsonc0336662016-05-06 15:40:21 +01002464 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002465 cleanup_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002466 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002467 WARN_ON(engine->id != RCS);
2468 cleanup_phys_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002469 }
Brad Volkin44e895a2014-05-10 14:10:43 -07002470
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002471 i915_cmd_parser_fini_ring(engine);
2472 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002473
2474 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2475
Chris Wilsonc0336662016-05-06 15:40:21 +01002476 engine->i915 = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002477}
2478
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002479int intel_engine_idle(struct intel_engine_cs *engine)
Chris Wilson3e960502012-11-27 16:22:54 +00002480{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002481 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002482
Chris Wilson3e960502012-11-27 16:22:54 +00002483 /* Wait upon the last request to be completed */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002484 if (list_empty(&engine->request_list))
Chris Wilson3e960502012-11-27 16:22:54 +00002485 return 0;
2486
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002487 req = list_entry(engine->request_list.prev,
2488 struct drm_i915_gem_request,
2489 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002490
Chris Wilsonb4716182015-04-27 13:41:17 +01002491 /* Make sure we do not trigger any retires */
2492 return __i915_wait_request(req,
Chris Wilsonc19ae982016-04-13 17:35:03 +01002493 req->i915->mm.interruptible,
Chris Wilsonb4716182015-04-27 13:41:17 +01002494 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002495}
2496
John Harrison6689cb22015-03-19 12:30:08 +00002497int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002498{
Chris Wilson63103462016-04-28 09:56:49 +01002499 int ret;
2500
2501 /* Flush enough space to reduce the likelihood of waiting after
2502 * we start building the request - in which case we will just
2503 * have to repeat work.
2504 */
Chris Wilsona0442462016-04-29 09:07:05 +01002505 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002506
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002507 request->ringbuf = request->engine->buffer;
Chris Wilson63103462016-04-28 09:56:49 +01002508
2509 ret = intel_ring_begin(request, 0);
2510 if (ret)
2511 return ret;
2512
Chris Wilsona0442462016-04-29 09:07:05 +01002513 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002514 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002515}
2516
Chris Wilson987046a2016-04-28 09:56:46 +01002517static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002518{
Chris Wilson987046a2016-04-28 09:56:46 +01002519 struct intel_ringbuffer *ringbuf = req->ringbuf;
2520 struct intel_engine_cs *engine = req->engine;
2521 struct drm_i915_gem_request *target;
2522
2523 intel_ring_update_space(ringbuf);
2524 if (ringbuf->space >= bytes)
2525 return 0;
2526
2527 /*
2528 * Space is reserved in the ringbuffer for finalising the request,
2529 * as that cannot be allowed to fail. During request finalisation,
2530 * reserved_space is set to 0 to stop the overallocation and the
2531 * assumption is that then we never need to wait (which has the
2532 * risk of failing with EINTR).
2533 *
2534 * See also i915_gem_request_alloc() and i915_add_request().
2535 */
Chris Wilson0251a962016-04-28 09:56:47 +01002536 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01002537
2538 list_for_each_entry(target, &engine->request_list, list) {
2539 unsigned space;
2540
2541 /*
2542 * The request queue is per-engine, so can contain requests
2543 * from multiple ringbuffers. Here, we must ignore any that
2544 * aren't from the ringbuffer we're considering.
2545 */
2546 if (target->ringbuf != ringbuf)
2547 continue;
2548
2549 /* Would completion of this request free enough space? */
2550 space = __intel_ring_space(target->postfix, ringbuf->tail,
2551 ringbuf->size);
2552 if (space >= bytes)
2553 break;
2554 }
2555
2556 if (WARN_ON(&target->list == &engine->request_list))
2557 return -ENOSPC;
2558
2559 return i915_wait_request(target);
2560}
2561
2562int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
2563{
2564 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison79bbcc22015-06-30 12:40:55 +01002565 int remain_actual = ringbuf->size - ringbuf->tail;
Chris Wilson987046a2016-04-28 09:56:46 +01002566 int remain_usable = ringbuf->effective_size - ringbuf->tail;
2567 int bytes = num_dwords * sizeof(u32);
2568 int total_bytes, wait_bytes;
John Harrison79bbcc22015-06-30 12:40:55 +01002569 bool need_wrap = false;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002570
Chris Wilson0251a962016-04-28 09:56:47 +01002571 total_bytes = bytes + req->reserved_space;
John Harrison29b1b412015-06-18 13:10:09 +01002572
John Harrison79bbcc22015-06-30 12:40:55 +01002573 if (unlikely(bytes > remain_usable)) {
2574 /*
2575 * Not enough space for the basic request. So need to flush
2576 * out the remainder and then wait for base + reserved.
2577 */
2578 wait_bytes = remain_actual + total_bytes;
2579 need_wrap = true;
Chris Wilson987046a2016-04-28 09:56:46 +01002580 } else if (unlikely(total_bytes > remain_usable)) {
2581 /*
2582 * The base request will fit but the reserved space
2583 * falls off the end. So we don't need an immediate wrap
2584 * and only need to effectively wait for the reserved
2585 * size space from the start of ringbuffer.
2586 */
Chris Wilson0251a962016-04-28 09:56:47 +01002587 wait_bytes = remain_actual + req->reserved_space;
John Harrison79bbcc22015-06-30 12:40:55 +01002588 } else {
Chris Wilson987046a2016-04-28 09:56:46 +01002589 /* No wrapping required, just waiting. */
2590 wait_bytes = total_bytes;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002591 }
2592
Chris Wilson987046a2016-04-28 09:56:46 +01002593 if (wait_bytes > ringbuf->space) {
2594 int ret = wait_for_space(req, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002595 if (unlikely(ret))
2596 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +01002597
Chris Wilson987046a2016-04-28 09:56:46 +01002598 intel_ring_update_space(ringbuf);
Chris Wilsone075a322016-05-13 11:57:22 +01002599 if (unlikely(ringbuf->space < wait_bytes))
2600 return -EAGAIN;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002601 }
2602
Chris Wilson987046a2016-04-28 09:56:46 +01002603 if (unlikely(need_wrap)) {
2604 GEM_BUG_ON(remain_actual > ringbuf->space);
2605 GEM_BUG_ON(ringbuf->tail + remain_actual > ringbuf->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002606
Chris Wilson987046a2016-04-28 09:56:46 +01002607 /* Fill the tail with MI_NOOP */
2608 memset(ringbuf->virtual_start + ringbuf->tail,
2609 0, remain_actual);
2610 ringbuf->tail = 0;
2611 ringbuf->space -= remain_actual;
2612 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002613
Chris Wilson987046a2016-04-28 09:56:46 +01002614 ringbuf->space -= bytes;
2615 GEM_BUG_ON(ringbuf->space < 0);
Chris Wilson304d6952014-01-02 14:32:35 +00002616 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002617}
2618
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002619/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01002620int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002621{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002622 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002623 int num_dwords = (engine->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002624 int ret;
2625
2626 if (num_dwords == 0)
2627 return 0;
2628
Chris Wilson18393f62014-04-09 09:19:40 +01002629 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
John Harrison5fb9de12015-05-29 17:44:07 +01002630 ret = intel_ring_begin(req, num_dwords);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002631 if (ret)
2632 return ret;
2633
2634 while (num_dwords--)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002635 intel_ring_emit(engine, MI_NOOP);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002636
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002637 intel_ring_advance(engine);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002638
2639 return 0;
2640}
2641
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002642void intel_ring_init_seqno(struct intel_engine_cs *engine, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002643{
Chris Wilsonc0336662016-05-06 15:40:21 +01002644 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002645
Chris Wilson29dcb572016-04-07 07:29:13 +01002646 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
2647 * so long as the semaphore value in the register/page is greater
2648 * than the sync value), so whenever we reset the seqno,
2649 * so long as we reset the tracking semaphore value to 0, it will
2650 * always be before the next request's seqno. If we don't reset
2651 * the semaphore value, then when the seqno moves backwards all
2652 * future waits will complete instantly (causing rendering corruption).
2653 */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01002654 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002655 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
2656 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
Chris Wilsond04bce42016-04-07 07:29:12 +01002657 if (HAS_VEBOX(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002658 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002659 }
Chris Wilsona058d932016-04-07 07:29:15 +01002660 if (dev_priv->semaphore_obj) {
2661 struct drm_i915_gem_object *obj = dev_priv->semaphore_obj;
2662 struct page *page = i915_gem_object_get_dirty_page(obj, 0);
2663 void *semaphores = kmap(page);
2664 memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
2665 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
2666 kunmap(page);
2667 }
Chris Wilson29dcb572016-04-07 07:29:13 +01002668 memset(engine->semaphore.sync_seqno, 0,
2669 sizeof(engine->semaphore.sync_seqno));
Chris Wilson297b0c52010-10-22 17:02:41 +01002670
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002671 engine->set_seqno(engine, seqno);
Chris Wilson01347122016-04-07 07:29:16 +01002672 engine->last_submitted_seqno = seqno;
Chris Wilson29dcb572016-04-07 07:29:13 +01002673
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002674 engine->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01002675}
2676
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002677static void gen6_bsd_ring_write_tail(struct intel_engine_cs *engine,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002678 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002679{
Chris Wilsonc0336662016-05-06 15:40:21 +01002680 struct drm_i915_private *dev_priv = engine->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002681
2682 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002683
Chris Wilson12f55812012-07-05 17:14:01 +01002684 /* Disable notification that the ring is IDLE. The GT
2685 * will then assume that it is busy and bring it out of rc6.
2686 */
2687 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2688 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2689
2690 /* Clear the context id. Here be magic! */
2691 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2692
2693 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04002694 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01002695 GEN6_BSD_SLEEP_INDICATOR) == 0,
2696 50))
2697 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002698
Chris Wilson12f55812012-07-05 17:14:01 +01002699 /* Now that the ring is fully powered up, update the tail */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002700 I915_WRITE_TAIL(engine, value);
2701 POSTING_READ(RING_TAIL(engine->mmio_base));
Chris Wilson12f55812012-07-05 17:14:01 +01002702
2703 /* Let the ring send IDLE messages to the GT again,
2704 * and so let it sleep to conserve power when idle.
2705 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002706 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01002707 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002708}
2709
John Harrisona84c3ae2015-05-29 17:43:57 +01002710static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002711 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002712{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002713 struct intel_engine_cs *engine = req->engine;
Chris Wilson71a77e02011-02-02 12:13:49 +00002714 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002715 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002716
John Harrison5fb9de12015-05-29 17:44:07 +01002717 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002718 if (ret)
2719 return ret;
2720
Chris Wilson71a77e02011-02-02 12:13:49 +00002721 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002722 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002723 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002724
2725 /* We always require a command barrier so that subsequent
2726 * commands, such as breadcrumb interrupts, are strictly ordered
2727 * wrt the contents of the write cache being flushed to memory
2728 * (and thus being coherent from the CPU).
2729 */
2730 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2731
Jesse Barnes9a289772012-10-26 09:42:42 -07002732 /*
2733 * Bspec vol 1c.5 - video engine command streamer:
2734 * "If ENABLED, all TLBs will be invalidated once the flush
2735 * operation is complete. This bit is only valid when the
2736 * Post-Sync Operation field is a value of 1h or 3h."
2737 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002738 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002739 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2740
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002741 intel_ring_emit(engine, cmd);
2742 intel_ring_emit(engine,
2743 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002744 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002745 intel_ring_emit(engine, 0); /* upper addr */
2746 intel_ring_emit(engine, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002747 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002748 intel_ring_emit(engine, 0);
2749 intel_ring_emit(engine, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002750 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002751 intel_ring_advance(engine);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002752 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002753}
2754
2755static int
John Harrison53fddaf2015-05-29 17:44:02 +01002756gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002757 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002758 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002759{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002760 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002761 bool ppgtt = USES_PPGTT(engine->dev) &&
John Harrison8e004ef2015-02-13 11:48:10 +00002762 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002763 int ret;
2764
John Harrison5fb9de12015-05-29 17:44:07 +01002765 ret = intel_ring_begin(req, 4);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002766 if (ret)
2767 return ret;
2768
2769 /* FIXME(BDW): Address space and security selectors. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002770 intel_ring_emit(engine, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002771 (dispatch_flags & I915_DISPATCH_RS ?
2772 MI_BATCH_RESOURCE_STREAMER : 0));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002773 intel_ring_emit(engine, lower_32_bits(offset));
2774 intel_ring_emit(engine, upper_32_bits(offset));
2775 intel_ring_emit(engine, MI_NOOP);
2776 intel_ring_advance(engine);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002777
2778 return 0;
2779}
2780
2781static int
John Harrison53fddaf2015-05-29 17:44:02 +01002782hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002783 u64 offset, u32 len,
2784 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002785{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002786 struct intel_engine_cs *engine = req->engine;
Akshay Joshi0206e352011-08-16 15:34:10 -04002787 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002788
John Harrison5fb9de12015-05-29 17:44:07 +01002789 ret = intel_ring_begin(req, 2);
Akshay Joshi0206e352011-08-16 15:34:10 -04002790 if (ret)
2791 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002792
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002793 intel_ring_emit(engine,
Chris Wilson77072252014-09-10 12:18:27 +01002794 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002795 (dispatch_flags & I915_DISPATCH_SECURE ?
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002796 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2797 (dispatch_flags & I915_DISPATCH_RS ?
2798 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002799 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002800 intel_ring_emit(engine, offset);
2801 intel_ring_advance(engine);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002802
2803 return 0;
2804}
2805
2806static int
John Harrison53fddaf2015-05-29 17:44:02 +01002807gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002808 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002809 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002810{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002811 struct intel_engine_cs *engine = req->engine;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002812 int ret;
2813
John Harrison5fb9de12015-05-29 17:44:07 +01002814 ret = intel_ring_begin(req, 2);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002815 if (ret)
2816 return ret;
2817
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002818 intel_ring_emit(engine,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002819 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002820 (dispatch_flags & I915_DISPATCH_SECURE ?
2821 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002822 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002823 intel_ring_emit(engine, offset);
2824 intel_ring_advance(engine);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002825
Akshay Joshi0206e352011-08-16 15:34:10 -04002826 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002827}
2828
Chris Wilson549f7362010-10-19 11:19:32 +01002829/* Blitter support (SandyBridge+) */
2830
John Harrisona84c3ae2015-05-29 17:43:57 +01002831static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002832 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002833{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002834 struct intel_engine_cs *engine = req->engine;
Chris Wilson71a77e02011-02-02 12:13:49 +00002835 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002836 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002837
John Harrison5fb9de12015-05-29 17:44:07 +01002838 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002839 if (ret)
2840 return ret;
2841
Chris Wilson71a77e02011-02-02 12:13:49 +00002842 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002843 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002844 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002845
2846 /* We always require a command barrier so that subsequent
2847 * commands, such as breadcrumb interrupts, are strictly ordered
2848 * wrt the contents of the write cache being flushed to memory
2849 * (and thus being coherent from the CPU).
2850 */
2851 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2852
Jesse Barnes9a289772012-10-26 09:42:42 -07002853 /*
2854 * Bspec vol 1c.3 - blitter engine command streamer:
2855 * "If ENABLED, all TLBs will be invalidated once the flush
2856 * operation is complete. This bit is only valid when the
2857 * Post-Sync Operation field is a value of 1h or 3h."
2858 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002859 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002860 cmd |= MI_INVALIDATE_TLB;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002861 intel_ring_emit(engine, cmd);
2862 intel_ring_emit(engine,
2863 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002864 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002865 intel_ring_emit(engine, 0); /* upper addr */
2866 intel_ring_emit(engine, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002867 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002868 intel_ring_emit(engine, 0);
2869 intel_ring_emit(engine, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002870 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002871 intel_ring_advance(engine);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002872
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002873 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002874}
2875
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002876int intel_init_render_ring_buffer(struct drm_device *dev)
2877{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002878 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002879 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002880 struct drm_i915_gem_object *obj;
2881 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002882
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002883 engine->name = "render ring";
2884 engine->id = RCS;
2885 engine->exec_id = I915_EXEC_RENDER;
Chris Wilson215a7e32016-04-29 13:18:23 +01002886 engine->hw_id = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002887 engine->mmio_base = RENDER_RING_BASE;
Daniel Vetter59465b52012-04-11 22:12:48 +02002888
Chris Wilsonc0336662016-05-06 15:40:21 +01002889 if (INTEL_GEN(dev_priv) >= 8) {
2890 if (i915_semaphore_is_enabled(dev_priv)) {
Dave Gordond37cd8a2016-04-22 19:14:32 +01002891 obj = i915_gem_object_create(dev, 4096);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002892 if (IS_ERR(obj)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002893 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2894 i915.semaphores = 0;
2895 } else {
2896 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2897 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2898 if (ret != 0) {
2899 drm_gem_object_unreference(&obj->base);
2900 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2901 i915.semaphores = 0;
2902 } else
2903 dev_priv->semaphore_obj = obj;
2904 }
2905 }
Mika Kuoppala72253422014-10-07 17:21:26 +03002906
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002907 engine->init_context = intel_rcs_ctx_init;
Chris Wilsona58c01a2016-04-29 13:18:21 +01002908 engine->add_request = gen8_render_add_request;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002909 engine->flush = gen8_render_ring_flush;
2910 engine->irq_get = gen8_ring_get_irq;
2911 engine->irq_put = gen8_ring_put_irq;
2912 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Chris Wilsonc04e0f32016-04-09 10:57:54 +01002913 engine->get_seqno = ring_get_seqno;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002914 engine->set_seqno = ring_set_seqno;
Chris Wilsonc0336662016-05-06 15:40:21 +01002915 if (i915_semaphore_is_enabled(dev_priv)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002916 WARN_ON(!dev_priv->semaphore_obj);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002917 engine->semaphore.sync_to = gen8_ring_sync;
2918 engine->semaphore.signal = gen8_rcs_signal;
2919 GEN8_RING_SEMAPHORE_INIT(engine);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002920 }
Chris Wilsonc0336662016-05-06 15:40:21 +01002921 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002922 engine->init_context = intel_rcs_ctx_init;
2923 engine->add_request = gen6_add_request;
2924 engine->flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002925 if (IS_GEN6(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002926 engine->flush = gen6_render_ring_flush;
2927 engine->irq_get = gen6_ring_get_irq;
2928 engine->irq_put = gen6_ring_put_irq;
2929 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Chris Wilsonc04e0f32016-04-09 10:57:54 +01002930 engine->irq_seqno_barrier = gen6_seqno_barrier;
2931 engine->get_seqno = ring_get_seqno;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002932 engine->set_seqno = ring_set_seqno;
Chris Wilsonc0336662016-05-06 15:40:21 +01002933 if (i915_semaphore_is_enabled(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002934 engine->semaphore.sync_to = gen6_ring_sync;
2935 engine->semaphore.signal = gen6_signal;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002936 /*
2937 * The current semaphore is only applied on pre-gen8
2938 * platform. And there is no VCS2 ring on the pre-gen8
2939 * platform. So the semaphore between RCS and VCS2 is
2940 * initialized as INVALID. Gen8 will initialize the
2941 * sema between VCS2 and RCS later.
2942 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002943 engine->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2944 engine->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2945 engine->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2946 engine->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2947 engine->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2948 engine->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2949 engine->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2950 engine->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2951 engine->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2952 engine->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002953 }
Chris Wilsonc0336662016-05-06 15:40:21 +01002954 } else if (IS_GEN5(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002955 engine->add_request = pc_render_add_request;
2956 engine->flush = gen4_render_ring_flush;
2957 engine->get_seqno = pc_render_get_seqno;
2958 engine->set_seqno = pc_render_set_seqno;
2959 engine->irq_get = gen5_ring_get_irq;
2960 engine->irq_put = gen5_ring_put_irq;
2961 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
Ben Widawskycc609d52013-05-28 19:22:29 -07002962 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002963 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002964 engine->add_request = i9xx_add_request;
Chris Wilsonc0336662016-05-06 15:40:21 +01002965 if (INTEL_GEN(dev_priv) < 4)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002966 engine->flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002967 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002968 engine->flush = gen4_render_ring_flush;
2969 engine->get_seqno = ring_get_seqno;
2970 engine->set_seqno = ring_set_seqno;
Chris Wilsonc0336662016-05-06 15:40:21 +01002971 if (IS_GEN2(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002972 engine->irq_get = i8xx_ring_get_irq;
2973 engine->irq_put = i8xx_ring_put_irq;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002974 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002975 engine->irq_get = i9xx_ring_get_irq;
2976 engine->irq_put = i9xx_ring_put_irq;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002977 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002978 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002979 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002980 engine->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002981
Chris Wilsonc0336662016-05-06 15:40:21 +01002982 if (IS_HASWELL(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002983 engine->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01002984 else if (IS_GEN8(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002985 engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01002986 else if (INTEL_GEN(dev_priv) >= 6)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002987 engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01002988 else if (INTEL_GEN(dev_priv) >= 4)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002989 engine->dispatch_execbuffer = i965_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01002990 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002991 engine->dispatch_execbuffer = i830_dispatch_execbuffer;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002992 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002993 engine->dispatch_execbuffer = i915_dispatch_execbuffer;
2994 engine->init_hw = init_render_ring;
2995 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002996
Daniel Vetterb45305f2012-12-17 16:21:27 +01002997 /* Workaround batchbuffer to combat CS tlb bug. */
Chris Wilsonc0336662016-05-06 15:40:21 +01002998 if (HAS_BROKEN_CS_TLB(dev_priv)) {
Dave Gordond37cd8a2016-04-22 19:14:32 +01002999 obj = i915_gem_object_create(dev, I830_WA_SIZE);
Chris Wilsonfe3db792016-04-25 13:32:13 +01003000 if (IS_ERR(obj)) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01003001 DRM_ERROR("Failed to allocate batch bo\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01003002 return PTR_ERR(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01003003 }
3004
Daniel Vetterbe1fa122014-02-14 14:01:14 +01003005 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01003006 if (ret != 0) {
3007 drm_gem_object_unreference(&obj->base);
3008 DRM_ERROR("Failed to ping batch bo\n");
3009 return ret;
3010 }
3011
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003012 engine->scratch.obj = obj;
3013 engine->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01003014 }
3015
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003016 ret = intel_init_ring_buffer(dev, engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01003017 if (ret)
3018 return ret;
3019
Chris Wilsonc0336662016-05-06 15:40:21 +01003020 if (INTEL_GEN(dev_priv) >= 5) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003021 ret = intel_init_pipe_control(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01003022 if (ret)
3023 return ret;
3024 }
3025
3026 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003027}
3028
3029int intel_init_bsd_ring_buffer(struct drm_device *dev)
3030{
Jani Nikula4640c4f2014-03-31 14:27:19 +03003031 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003032 struct intel_engine_cs *engine = &dev_priv->engine[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003033
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003034 engine->name = "bsd ring";
3035 engine->id = VCS;
3036 engine->exec_id = I915_EXEC_BSD;
Chris Wilson215a7e32016-04-29 13:18:23 +01003037 engine->hw_id = 1;
Daniel Vetter58fa3832012-04-11 22:12:49 +02003038
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003039 engine->write_tail = ring_write_tail;
Chris Wilsonc0336662016-05-06 15:40:21 +01003040 if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003041 engine->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02003042 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01003043 if (IS_GEN6(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003044 engine->write_tail = gen6_bsd_ring_write_tail;
3045 engine->flush = gen6_bsd_ring_flush;
3046 engine->add_request = gen6_add_request;
Chris Wilsonc04e0f32016-04-09 10:57:54 +01003047 engine->irq_seqno_barrier = gen6_seqno_barrier;
3048 engine->get_seqno = ring_get_seqno;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003049 engine->set_seqno = ring_set_seqno;
Chris Wilsonc0336662016-05-06 15:40:21 +01003050 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003051 engine->irq_enable_mask =
Ben Widawskyabd58f02013-11-02 21:07:09 -07003052 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003053 engine->irq_get = gen8_ring_get_irq;
3054 engine->irq_put = gen8_ring_put_irq;
3055 engine->dispatch_execbuffer =
Ben Widawsky1c7a0622013-11-02 21:07:12 -07003056 gen8_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01003057 if (i915_semaphore_is_enabled(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003058 engine->semaphore.sync_to = gen8_ring_sync;
3059 engine->semaphore.signal = gen8_xcs_signal;
3060 GEN8_RING_SEMAPHORE_INIT(engine);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003061 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003062 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003063 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
3064 engine->irq_get = gen6_ring_get_irq;
3065 engine->irq_put = gen6_ring_put_irq;
3066 engine->dispatch_execbuffer =
Ben Widawsky1c7a0622013-11-02 21:07:12 -07003067 gen6_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01003068 if (i915_semaphore_is_enabled(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003069 engine->semaphore.sync_to = gen6_ring_sync;
3070 engine->semaphore.signal = gen6_signal;
3071 engine->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
3072 engine->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
3073 engine->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
3074 engine->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
3075 engine->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
3076 engine->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
3077 engine->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
3078 engine->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
3079 engine->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
3080 engine->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003081 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003082 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02003083 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003084 engine->mmio_base = BSD_RING_BASE;
3085 engine->flush = bsd_ring_flush;
3086 engine->add_request = i9xx_add_request;
3087 engine->get_seqno = ring_get_seqno;
3088 engine->set_seqno = ring_set_seqno;
Chris Wilsonc0336662016-05-06 15:40:21 +01003089 if (IS_GEN5(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003090 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
3091 engine->irq_get = gen5_ring_get_irq;
3092 engine->irq_put = gen5_ring_put_irq;
Daniel Vettere48d8632012-04-11 22:12:54 +02003093 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003094 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
3095 engine->irq_get = i9xx_ring_get_irq;
3096 engine->irq_put = i9xx_ring_put_irq;
Daniel Vettere48d8632012-04-11 22:12:54 +02003097 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003098 engine->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02003099 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003100 engine->init_hw = init_ring_common;
Daniel Vetter58fa3832012-04-11 22:12:49 +02003101
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003102 return intel_init_ring_buffer(dev, engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08003103}
Chris Wilson549f7362010-10-19 11:19:32 +01003104
Zhao Yakui845f74a2014-04-17 10:37:37 +08003105/**
Damien Lespiau62659922015-01-29 14:13:40 +00003106 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08003107 */
3108int intel_init_bsd2_ring_buffer(struct drm_device *dev)
3109{
3110 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003111 struct intel_engine_cs *engine = &dev_priv->engine[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08003112
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003113 engine->name = "bsd2 ring";
3114 engine->id = VCS2;
3115 engine->exec_id = I915_EXEC_BSD;
Chris Wilson215a7e32016-04-29 13:18:23 +01003116 engine->hw_id = 4;
Zhao Yakui845f74a2014-04-17 10:37:37 +08003117
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003118 engine->write_tail = ring_write_tail;
3119 engine->mmio_base = GEN8_BSD2_RING_BASE;
3120 engine->flush = gen6_bsd_ring_flush;
3121 engine->add_request = gen6_add_request;
Chris Wilsonc04e0f32016-04-09 10:57:54 +01003122 engine->irq_seqno_barrier = gen6_seqno_barrier;
3123 engine->get_seqno = ring_get_seqno;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003124 engine->set_seqno = ring_set_seqno;
3125 engine->irq_enable_mask =
Zhao Yakui845f74a2014-04-17 10:37:37 +08003126 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003127 engine->irq_get = gen8_ring_get_irq;
3128 engine->irq_put = gen8_ring_put_irq;
3129 engine->dispatch_execbuffer =
Zhao Yakui845f74a2014-04-17 10:37:37 +08003130 gen8_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01003131 if (i915_semaphore_is_enabled(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003132 engine->semaphore.sync_to = gen8_ring_sync;
3133 engine->semaphore.signal = gen8_xcs_signal;
3134 GEN8_RING_SEMAPHORE_INIT(engine);
Ben Widawsky3e789982014-06-30 09:53:37 -07003135 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003136 engine->init_hw = init_ring_common;
Zhao Yakui845f74a2014-04-17 10:37:37 +08003137
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003138 return intel_init_ring_buffer(dev, engine);
Zhao Yakui845f74a2014-04-17 10:37:37 +08003139}
3140
Chris Wilson549f7362010-10-19 11:19:32 +01003141int intel_init_blt_ring_buffer(struct drm_device *dev)
3142{
Jani Nikula4640c4f2014-03-31 14:27:19 +03003143 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003144 struct intel_engine_cs *engine = &dev_priv->engine[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01003145
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003146 engine->name = "blitter ring";
3147 engine->id = BCS;
3148 engine->exec_id = I915_EXEC_BLT;
Chris Wilson215a7e32016-04-29 13:18:23 +01003149 engine->hw_id = 2;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02003150
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003151 engine->mmio_base = BLT_RING_BASE;
3152 engine->write_tail = ring_write_tail;
3153 engine->flush = gen6_ring_flush;
3154 engine->add_request = gen6_add_request;
Chris Wilsonc04e0f32016-04-09 10:57:54 +01003155 engine->irq_seqno_barrier = gen6_seqno_barrier;
3156 engine->get_seqno = ring_get_seqno;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003157 engine->set_seqno = ring_set_seqno;
Chris Wilsonc0336662016-05-06 15:40:21 +01003158 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003159 engine->irq_enable_mask =
Ben Widawskyabd58f02013-11-02 21:07:09 -07003160 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003161 engine->irq_get = gen8_ring_get_irq;
3162 engine->irq_put = gen8_ring_put_irq;
3163 engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01003164 if (i915_semaphore_is_enabled(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003165 engine->semaphore.sync_to = gen8_ring_sync;
3166 engine->semaphore.signal = gen8_xcs_signal;
3167 GEN8_RING_SEMAPHORE_INIT(engine);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003168 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003169 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003170 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
3171 engine->irq_get = gen6_ring_get_irq;
3172 engine->irq_put = gen6_ring_put_irq;
3173 engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01003174 if (i915_semaphore_is_enabled(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003175 engine->semaphore.signal = gen6_signal;
3176 engine->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003177 /*
3178 * The current semaphore is only applied on pre-gen8
3179 * platform. And there is no VCS2 ring on the pre-gen8
3180 * platform. So the semaphore between BCS and VCS2 is
3181 * initialized as INVALID. Gen8 will initialize the
3182 * sema between BCS and VCS2 later.
3183 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003184 engine->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
3185 engine->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
3186 engine->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
3187 engine->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
3188 engine->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
3189 engine->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
3190 engine->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
3191 engine->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
3192 engine->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
3193 engine->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003194 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003195 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003196 engine->init_hw = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01003197
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003198 return intel_init_ring_buffer(dev, engine);
Chris Wilson549f7362010-10-19 11:19:32 +01003199}
Chris Wilsona7b97612012-07-20 12:41:08 +01003200
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003201int intel_init_vebox_ring_buffer(struct drm_device *dev)
3202{
Jani Nikula4640c4f2014-03-31 14:27:19 +03003203 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003204 struct intel_engine_cs *engine = &dev_priv->engine[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003205
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003206 engine->name = "video enhancement ring";
3207 engine->id = VECS;
3208 engine->exec_id = I915_EXEC_VEBOX;
Chris Wilson215a7e32016-04-29 13:18:23 +01003209 engine->hw_id = 3;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003210
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003211 engine->mmio_base = VEBOX_RING_BASE;
3212 engine->write_tail = ring_write_tail;
3213 engine->flush = gen6_ring_flush;
3214 engine->add_request = gen6_add_request;
Chris Wilsonc04e0f32016-04-09 10:57:54 +01003215 engine->irq_seqno_barrier = gen6_seqno_barrier;
3216 engine->get_seqno = ring_get_seqno;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003217 engine->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003218
Chris Wilsonc0336662016-05-06 15:40:21 +01003219 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003220 engine->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08003221 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003222 engine->irq_get = gen8_ring_get_irq;
3223 engine->irq_put = gen8_ring_put_irq;
3224 engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01003225 if (i915_semaphore_is_enabled(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003226 engine->semaphore.sync_to = gen8_ring_sync;
3227 engine->semaphore.signal = gen8_xcs_signal;
3228 GEN8_RING_SEMAPHORE_INIT(engine);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003229 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003230 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003231 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
3232 engine->irq_get = hsw_vebox_get_irq;
3233 engine->irq_put = hsw_vebox_put_irq;
3234 engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Chris Wilsonc0336662016-05-06 15:40:21 +01003235 if (i915_semaphore_is_enabled(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003236 engine->semaphore.sync_to = gen6_ring_sync;
3237 engine->semaphore.signal = gen6_signal;
3238 engine->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
3239 engine->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
3240 engine->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
3241 engine->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
3242 engine->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
3243 engine->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
3244 engine->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
3245 engine->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
3246 engine->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
3247 engine->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003248 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003249 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003250 engine->init_hw = init_ring_common;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003251
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003252 return intel_init_ring_buffer(dev, engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003253}
3254
Chris Wilsona7b97612012-07-20 12:41:08 +01003255int
John Harrison4866d722015-05-29 17:43:55 +01003256intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01003257{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003258 struct intel_engine_cs *engine = req->engine;
Chris Wilsona7b97612012-07-20 12:41:08 +01003259 int ret;
3260
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003261 if (!engine->gpu_caches_dirty)
Chris Wilsona7b97612012-07-20 12:41:08 +01003262 return 0;
3263
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003264 ret = engine->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01003265 if (ret)
3266 return ret;
3267
John Harrisona84c3ae2015-05-29 17:43:57 +01003268 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01003269
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003270 engine->gpu_caches_dirty = false;
Chris Wilsona7b97612012-07-20 12:41:08 +01003271 return 0;
3272}
3273
3274int
John Harrison2f200552015-05-29 17:43:53 +01003275intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01003276{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003277 struct intel_engine_cs *engine = req->engine;
Chris Wilsona7b97612012-07-20 12:41:08 +01003278 uint32_t flush_domains;
3279 int ret;
3280
3281 flush_domains = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003282 if (engine->gpu_caches_dirty)
Chris Wilsona7b97612012-07-20 12:41:08 +01003283 flush_domains = I915_GEM_GPU_DOMAINS;
3284
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003285 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003286 if (ret)
3287 return ret;
3288
John Harrisona84c3ae2015-05-29 17:43:57 +01003289 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003290
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003291 engine->gpu_caches_dirty = false;
Chris Wilsona7b97612012-07-20 12:41:08 +01003292 return 0;
3293}
Chris Wilsone3efda42014-04-09 09:19:41 +01003294
3295void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00003296intel_stop_engine(struct intel_engine_cs *engine)
Chris Wilsone3efda42014-04-09 09:19:41 +01003297{
3298 int ret;
3299
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00003300 if (!intel_engine_initialized(engine))
Chris Wilsone3efda42014-04-09 09:19:41 +01003301 return;
3302
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003303 ret = intel_engine_idle(engine);
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003304 if (ret)
Chris Wilsone3efda42014-04-09 09:19:41 +01003305 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003306 engine->name, ret);
Chris Wilsone3efda42014-04-09 09:19:41 +01003307
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003308 stop_ring(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01003309}