blob: a924a9f9eb4d3d5681cde3ee69374270181007ab [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));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100518 if (intel_wait_for_register(dev_priv,
519 reg, INSTPM_SYNC_FLUSH, 0,
520 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000521 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000522 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000523 }
524}
525
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000526static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100527{
Chris Wilsonc0336662016-05-06 15:40:21 +0100528 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100529
Chris Wilsonc0336662016-05-06 15:40:21 +0100530 if (!IS_GEN2(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000531 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100532 if (intel_wait_for_register(dev_priv,
533 RING_MI_MODE(engine->mmio_base),
534 MODE_IDLE,
535 MODE_IDLE,
536 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000537 DRM_ERROR("%s : timed out trying to stop ring\n",
538 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100539 /* Sometimes we observe that the idle flag is not
540 * set even though the ring is empty. So double
541 * check before giving up.
542 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000543 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100544 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100545 }
546 }
547
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000548 I915_WRITE_CTL(engine, 0);
549 I915_WRITE_HEAD(engine, 0);
550 engine->write_tail(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100551
Chris Wilsonc0336662016-05-06 15:40:21 +0100552 if (!IS_GEN2(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000553 (void)I915_READ_CTL(engine);
554 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
Chris Wilson9991ae72014-04-02 16:36:07 +0100555 }
556
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000557 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100558}
559
Tomas Elffc0768c2016-03-21 16:26:59 +0000560void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
561{
562 memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
563}
564
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000565static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800566{
Chris Wilsonc0336662016-05-06 15:40:21 +0100567 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000568 struct intel_ringbuffer *ringbuf = engine->buffer;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100569 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200570 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800571
Mika Kuoppala59bad942015-01-16 11:34:40 +0200572 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200573
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000574 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100575 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000576 DRM_DEBUG_KMS("%s head not reset to zero "
577 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000578 engine->name,
579 I915_READ_CTL(engine),
580 I915_READ_HEAD(engine),
581 I915_READ_TAIL(engine),
582 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800583
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000584 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000585 DRM_ERROR("failed to set %s head to zero "
586 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000587 engine->name,
588 I915_READ_CTL(engine),
589 I915_READ_HEAD(engine),
590 I915_READ_TAIL(engine),
591 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100592 ret = -EIO;
593 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000594 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700595 }
596
Chris Wilsonc0336662016-05-06 15:40:21 +0100597 if (I915_NEED_GFX_HWS(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000598 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100599 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000600 ring_setup_phys_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100601
Jiri Kosinaece4a172014-08-07 16:29:53 +0200602 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000603 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200604
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200605 /* Initialize the ring. This must happen _after_ we've cleared the ring
606 * registers with the above sequence (the readback of the HEAD registers
607 * also enforces ordering), otherwise the hw might lose the new ring
608 * register values. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000609 I915_WRITE_START(engine, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100610
611 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000612 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100613 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000614 engine->name, I915_READ_HEAD(engine));
615 I915_WRITE_HEAD(engine, 0);
616 (void)I915_READ_HEAD(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100617
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000618 I915_WRITE_CTL(engine,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100619 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000620 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800621
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800622 /* If the head is still not zero, the ring is dead */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000623 if (wait_for((I915_READ_CTL(engine) & RING_VALID) != 0 &&
624 I915_READ_START(engine) == i915_gem_obj_ggtt_offset(obj) &&
625 (I915_READ_HEAD(engine) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000626 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100627 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000628 engine->name,
629 I915_READ_CTL(engine),
630 I915_READ_CTL(engine) & RING_VALID,
631 I915_READ_HEAD(engine), I915_READ_TAIL(engine),
632 I915_READ_START(engine),
633 (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200634 ret = -EIO;
635 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800636 }
637
Dave Gordonebd0fd42014-11-27 11:22:49 +0000638 ringbuf->last_retired_head = -1;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000639 ringbuf->head = I915_READ_HEAD(engine);
640 ringbuf->tail = I915_READ_TAIL(engine) & TAIL_ADDR;
Dave Gordonebd0fd42014-11-27 11:22:49 +0000641 intel_ring_update_space(ringbuf);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000642
Tomas Elffc0768c2016-03-21 16:26:59 +0000643 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100644
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200645out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200646 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200647
648 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700649}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800650
Chris Wilsonf8291952016-07-01 17:23:18 +0100651void intel_fini_pipe_control(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100652{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000653 if (engine->scratch.obj == NULL)
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100654 return;
655
Chris Wilsonf8291952016-07-01 17:23:18 +0100656 i915_gem_object_ggtt_unpin(engine->scratch.obj);
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
Chris Wilson7d5ea802016-07-01 17:23:20 +0100661int intel_init_pipe_control(struct intel_engine_cs *engine, int size)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000662{
Chris Wilsonf8291952016-07-01 17:23:18 +0100663 struct drm_i915_gem_object *obj;
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 Wilson7d5ea802016-07-01 17:23:20 +0100668 obj = i915_gem_object_create_stolen(engine->i915->dev, size);
Chris Wilsonde8fe162016-07-01 17:23:19 +0100669 if (!obj)
Chris Wilson7d5ea802016-07-01 17:23:20 +0100670 obj = i915_gem_object_create(engine->i915->dev, size);
Chris Wilsonf8291952016-07-01 17:23:18 +0100671 if (IS_ERR(obj)) {
672 DRM_ERROR("Failed to allocate scratch page\n");
673 ret = PTR_ERR(obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000674 goto err;
675 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100676
Chris Wilsonf8291952016-07-01 17:23:18 +0100677 ret = i915_gem_obj_ggtt_pin(obj, 4096, PIN_HIGH);
Daniel Vettera9cc7262014-02-14 14:01:13 +0100678 if (ret)
679 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000680
Chris Wilsonf8291952016-07-01 17:23:18 +0100681 engine->scratch.obj = obj;
682 engine->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200683 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000684 engine->name, engine->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000685 return 0;
686
Chris Wilsonc6df5412010-12-15 09:56:50 +0000687err_unref:
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000688 drm_gem_object_unreference(&engine->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000689err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000690 return ret;
691}
692
John Harrisone2be4fa2015-05-29 17:43:54 +0100693static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100694{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000695 struct intel_engine_cs *engine = req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +0100696 struct i915_workarounds *w = &req->i915->workarounds;
697 int ret, i;
Arun Siluvery888b5992014-08-26 14:44:51 +0100698
Francisco Jerez02235802015-10-07 14:44:01 +0300699 if (w->count == 0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300700 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100701
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000702 engine->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100703 ret = intel_ring_flush_all_caches(req);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100704 if (ret)
705 return ret;
706
John Harrison5fb9de12015-05-29 17:44:07 +0100707 ret = intel_ring_begin(req, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300708 if (ret)
709 return ret;
710
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000711 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300712 for (i = 0; i < w->count; i++) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000713 intel_ring_emit_reg(engine, w->reg[i].addr);
714 intel_ring_emit(engine, w->reg[i].value);
Mika Kuoppala72253422014-10-07 17:21:26 +0300715 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000716 intel_ring_emit(engine, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300717
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000718 intel_ring_advance(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +0300719
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000720 engine->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100721 ret = intel_ring_flush_all_caches(req);
Mika Kuoppala72253422014-10-07 17:21:26 +0300722 if (ret)
723 return ret;
724
725 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
726
727 return 0;
728}
729
John Harrison87531812015-05-29 17:43:44 +0100730static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100731{
732 int ret;
733
John Harrisone2be4fa2015-05-29 17:43:54 +0100734 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100735 if (ret != 0)
736 return ret;
737
John Harrisonbe013632015-05-29 17:43:45 +0100738 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100739 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000740 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100741
Chris Wilsone26e1b92016-01-29 16:49:05 +0000742 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100743}
744
Mika Kuoppala72253422014-10-07 17:21:26 +0300745static int wa_add(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200746 i915_reg_t addr,
747 const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300748{
749 const u32 idx = dev_priv->workarounds.count;
750
751 if (WARN_ON(idx >= I915_MAX_WA_REGS))
752 return -ENOSPC;
753
754 dev_priv->workarounds.reg[idx].addr = addr;
755 dev_priv->workarounds.reg[idx].value = val;
756 dev_priv->workarounds.reg[idx].mask = mask;
757
758 dev_priv->workarounds.count++;
759
760 return 0;
761}
762
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100763#define WA_REG(addr, mask, val) do { \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000764 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300765 if (r) \
766 return r; \
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100767 } while (0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300768
769#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000770 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300771
772#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000773 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300774
Damien Lespiau98533252014-12-08 17:33:51 +0000775#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000776 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300777
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000778#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
779#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300780
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000781#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300782
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000783static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
784 i915_reg_t reg)
Arun Siluvery33136b02016-01-21 21:43:47 +0000785{
Chris Wilsonc0336662016-05-06 15:40:21 +0100786 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery33136b02016-01-21 21:43:47 +0000787 struct i915_workarounds *wa = &dev_priv->workarounds;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000788 const uint32_t index = wa->hw_whitelist_count[engine->id];
Arun Siluvery33136b02016-01-21 21:43:47 +0000789
790 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
791 return -EINVAL;
792
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000793 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
Arun Siluvery33136b02016-01-21 21:43:47 +0000794 i915_mmio_reg_offset(reg));
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000795 wa->hw_whitelist_count[engine->id]++;
Arun Siluvery33136b02016-01-21 21:43:47 +0000796
797 return 0;
798}
799
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000800static int gen8_init_workarounds(struct intel_engine_cs *engine)
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100801{
Chris Wilsonc0336662016-05-06 15:40:21 +0100802 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery68c61982015-09-25 17:40:38 +0100803
804 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100805
Arun Siluvery717d84d2015-09-25 17:40:39 +0100806 /* WaDisableAsyncFlipPerfMode:bdw,chv */
807 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
808
Arun Siluveryd0581192015-09-25 17:40:40 +0100809 /* WaDisablePartialInstShootdown:bdw,chv */
810 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
811 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
812
Arun Siluverya340af52015-09-25 17:40:45 +0100813 /* Use Force Non-Coherent whenever executing a 3D context. This is a
814 * workaround for for a possible hang in the unlikely event a TLB
815 * invalidation occurs during a PSD flush.
816 */
817 /* WaForceEnableNonCoherent:bdw,chv */
Arun Siluvery120f5d22015-09-25 17:40:46 +0100818 /* WaHdcDisableFetchWhenMasked:bdw,chv */
Arun Siluverya340af52015-09-25 17:40:45 +0100819 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Arun Siluvery120f5d22015-09-25 17:40:46 +0100820 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Arun Siluverya340af52015-09-25 17:40:45 +0100821 HDC_FORCE_NON_COHERENT);
822
Arun Siluvery6def8fd2015-09-25 17:40:42 +0100823 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
824 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
825 * polygons in the same 8x4 pixel/sample area to be processed without
826 * stalling waiting for the earlier ones to write to Hierarchical Z
827 * buffer."
828 *
829 * This optimization is off by default for BDW and CHV; turn it on.
830 */
831 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
832
Arun Siluvery48404632015-09-25 17:40:43 +0100833 /* Wa4x4STCOptimizationDisable:bdw,chv */
834 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
835
Arun Siluvery7eebcde2015-09-25 17:40:44 +0100836 /*
837 * BSpec recommends 8x4 when MSAA is used,
838 * however in practice 16x4 seems fastest.
839 *
840 * Note that PS/WM thread counts depend on the WIZ hashing
841 * disable bit, which we don't touch here, but it's good
842 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
843 */
844 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
845 GEN6_WIZ_HASHING_MASK,
846 GEN6_WIZ_HASHING_16x4);
847
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100848 return 0;
849}
850
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000851static int bdw_init_workarounds(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +0300852{
Chris Wilsonc0336662016-05-06 15:40:21 +0100853 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100854 int ret;
Mika Kuoppala72253422014-10-07 17:21:26 +0300855
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000856 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100857 if (ret)
858 return ret;
859
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700860 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Arun Siluveryd0581192015-09-25 17:40:40 +0100861 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100862
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700863 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300864 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
865 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100866
Mika Kuoppala72253422014-10-07 17:21:26 +0300867 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
868 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100869
Mika Kuoppala72253422014-10-07 17:21:26 +0300870 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000871 /* WaForceContextSaveRestoreNonCoherent:bdw */
872 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000873 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Chris Wilsonc0336662016-05-06 15:40:21 +0100874 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100875
Arun Siluvery86d7f232014-08-26 14:44:50 +0100876 return 0;
877}
878
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000879static int chv_init_workarounds(struct intel_engine_cs *engine)
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300880{
Chris Wilsonc0336662016-05-06 15:40:21 +0100881 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100882 int ret;
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300883
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000884 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100885 if (ret)
886 return ret;
887
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300888 /* WaDisableThreadStallDopClockGating:chv */
Arun Siluveryd0581192015-09-25 17:40:40 +0100889 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300890
Kenneth Graunked60de812015-01-10 18:02:22 -0800891 /* Improve HiZ throughput on CHV. */
892 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
893
Mika Kuoppala72253422014-10-07 17:21:26 +0300894 return 0;
895}
896
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000897static int gen9_init_workarounds(struct intel_engine_cs *engine)
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000898{
Chris Wilsonc0336662016-05-06 15:40:21 +0100899 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000900 int ret;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000901
Tim Gorea8ab5ed2016-06-13 12:15:01 +0100902 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
903 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
904
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300905 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300906 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
907 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
908
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300909 /* WaDisableKillLogic:bxt,skl,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300910 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
911 ECOCHK_DIS_TLB);
912
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300913 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
914 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000915 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Tim Gore950b2aa2016-03-16 16:13:46 +0000916 FLOW_CONTROL_ENABLE |
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000917 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
918
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300919 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Nick Hoath84241712015-02-05 10:47:20 +0000920 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
921 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
922
Jani Nikulae87a0052015-10-20 15:22:02 +0300923 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100924 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
925 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Damien Lespiaua86eb582015-02-11 18:21:44 +0000926 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
927 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000928
Jani Nikulae87a0052015-10-20 15:22:02 +0300929 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100930 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
931 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Damien Lespiau183c6da2015-02-09 19:33:11 +0000932 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
933 GEN9_RHWO_OPTIMIZATION_DISABLE);
Arun Siluvery9b014352015-07-14 15:01:30 +0100934 /*
935 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
936 * but we do that in per ctx batchbuffer as there is an issue
937 * with this register not getting restored on ctx restore
938 */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000939 }
940
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300941 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl */
942 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
Tim Gorebfd8ad42016-04-19 15:45:52 +0100943 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
944 GEN9_ENABLE_YV12_BUGFIX |
945 GEN9_ENABLE_GPGPU_PREEMPTION);
Nick Hoathcac23df2015-02-05 10:47:22 +0000946
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300947 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
948 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
Arun Siluvery60294682015-09-25 14:33:37 +0100949 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
950 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
Damien Lespiau9370cd92015-02-09 19:33:17 +0000951
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300952 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000953 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
954 GEN9_CCS_TLB_PREFETCH_ENABLE);
955
Imre Deak5a2ae952015-05-19 15:04:59 +0300956 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100957 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_C0) ||
958 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200959 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
960 PIXEL_MASK_CAMMING_DISABLE);
961
Mika Kuoppala5b0e3652016-06-07 17:18:57 +0300962 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
963 WA_SET_BIT_MASKED(HDC_CHICKEN0,
964 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
965 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
Imre Deak8ea6f892015-05-19 17:05:42 +0300966
Mika Kuoppalabbaefe72016-06-07 17:18:58 +0300967 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
968 * both tied to WaForceContextSaveRestoreNonCoherent
969 * in some hsds for skl. We keep the tie for all gen9. The
970 * documentation is a bit hazy and so we want to get common behaviour,
971 * even though there is no clear evidence we would need both on kbl/bxt.
972 * This area has been source of system hangs so we play it safe
973 * and mimic the skl regardless of what bspec says.
974 *
975 * Use Force Non-Coherent whenever executing a 3D context. This
976 * is a workaround for a possible hang in the unlikely event
977 * a TLB invalidation occurs during a PSD flush.
978 */
979
980 /* WaForceEnableNonCoherent:skl,bxt,kbl */
981 WA_SET_BIT_MASKED(HDC_CHICKEN0,
982 HDC_FORCE_NON_COHERENT);
983
984 /* WaDisableHDCInvalidation:skl,bxt,kbl */
985 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
986 BDW_DISABLE_HDC_INVALIDATION);
987
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300988 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
989 if (IS_SKYLAKE(dev_priv) ||
990 IS_KABYLAKE(dev_priv) ||
991 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
Arun Siluvery8c761602015-09-08 10:31:48 +0100992 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
993 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery8c761602015-09-08 10:31:48 +0100994
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300995 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
Robert Beckett6b6d5622015-09-08 10:31:52 +0100996 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
997
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300998 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
Arun Siluvery6ecf56a2016-01-21 21:43:54 +0000999 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
1000 GEN8_LQSC_FLUSH_COHERENT_LINES));
1001
arun.siluvery@linux.intel.com6bb628552016-06-06 09:52:49 +01001002 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
1003 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
1004 if (ret)
1005 return ret;
1006
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001007 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001008 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Arun Siluverye0f3fa02016-01-21 21:43:48 +00001009 if (ret)
1010 return ret;
1011
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001012 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001013 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
Arun Siluvery3669ab62016-01-21 21:43:49 +00001014 if (ret)
1015 return ret;
1016
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001017 return 0;
1018}
1019
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001020static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +00001021{
Chris Wilsonc0336662016-05-06 15:40:21 +01001022 struct drm_i915_private *dev_priv = engine->i915;
Damien Lespiaub7668792015-02-14 18:30:29 +00001023 u8 vals[3] = { 0, 0, 0 };
1024 unsigned int i;
1025
1026 for (i = 0; i < 3; i++) {
1027 u8 ss;
1028
1029 /*
1030 * Only consider slices where one, and only one, subslice has 7
1031 * EUs
1032 */
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +08001033 if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
Damien Lespiaub7668792015-02-14 18:30:29 +00001034 continue;
1035
1036 /*
1037 * subslice_7eu[i] != 0 (because of the check above) and
1038 * ss_max == 4 (maximum number of subslices possible per slice)
1039 *
1040 * -> 0 <= ss <= 3;
1041 */
1042 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1043 vals[i] = 3 - ss;
1044 }
1045
1046 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1047 return 0;
1048
1049 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1050 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1051 GEN9_IZ_HASHING_MASK(2) |
1052 GEN9_IZ_HASHING_MASK(1) |
1053 GEN9_IZ_HASHING_MASK(0),
1054 GEN9_IZ_HASHING(2, vals[2]) |
1055 GEN9_IZ_HASHING(1, vals[1]) |
1056 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001057
Mika Kuoppala72253422014-10-07 17:21:26 +03001058 return 0;
1059}
1060
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001061static int skl_init_workarounds(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +00001062{
Chris Wilsonc0336662016-05-06 15:40:21 +01001063 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001064 int ret;
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001065
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001066 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001067 if (ret)
1068 return ret;
Damien Lespiau8d205492015-02-09 19:33:15 +00001069
Arun Siluverya78536e2016-01-21 21:43:53 +00001070 /*
1071 * Actual WA is to disable percontext preemption granularity control
1072 * until D0 which is the default case so this is equivalent to
1073 * !WaDisablePerCtxtPreemptionGranularityControl:skl
1074 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001075 if (IS_SKL_REVID(dev_priv, SKL_REVID_E0, REVID_FOREVER)) {
Arun Siluverya78536e2016-01-21 21:43:53 +00001076 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1077 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1078 }
1079
Mika Kuoppala71dce582016-06-07 17:19:14 +03001080 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001081 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1082 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1083 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1084 }
1085
1086 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1087 * involving this register should also be added to WA batch as required.
1088 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001089 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001090 /* WaDisableLSQCROPERFforOCL:skl */
1091 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1092 GEN8_LQSC_RO_PERF_DIS);
1093
1094 /* WaEnableGapsTsvCreditFix:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001095 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, REVID_FOREVER)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001096 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1097 GEN9_GAPS_TSV_CREDIT_DISABLE));
1098 }
1099
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001100 /* WaDisablePowerCompilerClockGating:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001101 if (IS_SKL_REVID(dev_priv, SKL_REVID_B0, SKL_REVID_B0))
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001102 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1103 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1104
Jani Nikulae87a0052015-10-20 15:22:02 +03001105 /* WaBarrierPerformanceFixDisable:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001106 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_D0))
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001107 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1108 HDC_FENCE_DEST_SLM_DISABLE |
1109 HDC_BARRIER_PERFORMANCE_DISABLE);
1110
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001111 /* WaDisableSbeCacheDispatchPortSharing:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001112 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0))
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001113 WA_SET_BIT_MASKED(
1114 GEN7_HALF_SLICE_CHICKEN1,
1115 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001116
Mika Kuoppalaeee8efb2016-06-07 17:18:53 +03001117 /* WaDisableGafsUnitClkGating:skl */
1118 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1119
Arun Siluvery61074972016-01-21 21:43:52 +00001120 /* WaDisableLSQCROPERFforOCL:skl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001121 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluvery61074972016-01-21 21:43:52 +00001122 if (ret)
1123 return ret;
1124
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001125 return skl_tune_iz_hashing(engine);
Damien Lespiau8d205492015-02-09 19:33:15 +00001126}
1127
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001128static int bxt_init_workarounds(struct intel_engine_cs *engine)
Nick Hoathcae04372015-03-17 11:39:38 +02001129{
Chris Wilsonc0336662016-05-06 15:40:21 +01001130 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001131 int ret;
Nick Hoathdfb601e2015-04-10 13:12:24 +01001132
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001133 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001134 if (ret)
1135 return ret;
Nick Hoathcae04372015-03-17 11:39:38 +02001136
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001137 /* WaStoreMultiplePTEenable:bxt */
1138 /* This is a requirement according to Hardware specification */
Chris Wilsonc0336662016-05-06 15:40:21 +01001139 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001140 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1141
1142 /* WaSetClckGatingDisableMedia:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001143 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001144 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1145 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1146 }
1147
Nick Hoathdfb601e2015-04-10 13:12:24 +01001148 /* WaDisableThreadStallDopClockGating:bxt */
1149 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1150 STALL_DOP_GATING_DISABLE);
1151
arun.siluvery@linux.intel.com780f0ae2016-06-03 11:16:10 +01001152 /* WaDisablePooledEuLoadBalancingFix:bxt */
1153 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1154 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1155 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1156 }
1157
Nick Hoath983b4b92015-04-10 13:12:25 +01001158 /* WaDisableSbeCacheDispatchPortSharing:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001159 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
Nick Hoath983b4b92015-04-10 13:12:25 +01001160 WA_SET_BIT_MASKED(
1161 GEN7_HALF_SLICE_CHICKEN1,
1162 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1163 }
1164
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001165 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1166 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1167 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
Arun Siluverya786d532016-01-21 21:43:51 +00001168 /* WaDisableLSQCROPERFforOCL:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001169 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001170 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001171 if (ret)
1172 return ret;
Arun Siluverya786d532016-01-21 21:43:51 +00001173
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001174 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluverya786d532016-01-21 21:43:51 +00001175 if (ret)
1176 return ret;
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001177 }
1178
Tim Gore050fc462016-04-22 09:46:01 +01001179 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001180 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
Imre Deak36579cb2016-05-03 15:54:20 +03001181 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1182 L3_HIGH_PRIO_CREDITS(2));
Tim Gore050fc462016-04-22 09:46:01 +01001183
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001184 /* WaInsertDummyPushConstPs:bxt */
1185 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
1186 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1187 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1188
Nick Hoathcae04372015-03-17 11:39:38 +02001189 return 0;
1190}
1191
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001192static int kbl_init_workarounds(struct intel_engine_cs *engine)
1193{
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001194 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001195 int ret;
1196
1197 ret = gen9_init_workarounds(engine);
1198 if (ret)
1199 return ret;
1200
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001201 /* WaEnableGapsTsvCreditFix:kbl */
1202 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1203 GEN9_GAPS_TSV_CREDIT_DISABLE));
1204
Mika Kuoppalac0b730d2016-06-07 17:19:06 +03001205 /* WaDisableDynamicCreditSharing:kbl */
1206 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1207 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1208 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1209
Mika Kuoppala8401d422016-06-07 17:19:00 +03001210 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1211 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1212 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1213 HDC_FENCE_DEST_SLM_DISABLE);
1214
Mika Kuoppalafe905812016-06-07 17:19:03 +03001215 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1216 * involving this register should also be added to WA batch as required.
1217 */
1218 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1219 /* WaDisableLSQCROPERFforOCL:kbl */
1220 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1221 GEN8_LQSC_RO_PERF_DIS);
1222
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001223 /* WaInsertDummyPushConstPs:kbl */
1224 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1225 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1226 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1227
Mika Kuoppala4de5d7c2016-06-07 17:19:11 +03001228 /* WaDisableGafsUnitClkGating:kbl */
1229 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1230
Mika Kuoppala954337a2016-06-07 17:19:12 +03001231 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1232 WA_SET_BIT_MASKED(
1233 GEN7_HALF_SLICE_CHICKEN1,
1234 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1235
Mika Kuoppalafe905812016-06-07 17:19:03 +03001236 /* WaDisableLSQCROPERFforOCL:kbl */
1237 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1238 if (ret)
1239 return ret;
1240
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001241 return 0;
1242}
1243
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001244int init_workarounds_ring(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +03001245{
Chris Wilsonc0336662016-05-06 15:40:21 +01001246 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala72253422014-10-07 17:21:26 +03001247
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001248 WARN_ON(engine->id != RCS);
Mika Kuoppala72253422014-10-07 17:21:26 +03001249
1250 dev_priv->workarounds.count = 0;
Arun Siluvery33136b02016-01-21 21:43:47 +00001251 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
Mika Kuoppala72253422014-10-07 17:21:26 +03001252
Chris Wilsonc0336662016-05-06 15:40:21 +01001253 if (IS_BROADWELL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001254 return bdw_init_workarounds(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +03001255
Chris Wilsonc0336662016-05-06 15:40:21 +01001256 if (IS_CHERRYVIEW(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001257 return chv_init_workarounds(engine);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001258
Chris Wilsonc0336662016-05-06 15:40:21 +01001259 if (IS_SKYLAKE(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001260 return skl_init_workarounds(engine);
Nick Hoathcae04372015-03-17 11:39:38 +02001261
Chris Wilsonc0336662016-05-06 15:40:21 +01001262 if (IS_BROXTON(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001263 return bxt_init_workarounds(engine);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001264
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001265 if (IS_KABYLAKE(dev_priv))
1266 return kbl_init_workarounds(engine);
1267
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001268 return 0;
1269}
1270
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001271static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001272{
Chris Wilsonc0336662016-05-06 15:40:21 +01001273 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001274 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001275 if (ret)
1276 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001277
Akash Goel61a563a2014-03-25 18:01:50 +05301278 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001279 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001280 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001281
1282 /* We need to disable the AsyncFlip performance optimisations in order
1283 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1284 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001285 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001286 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001287 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001288 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001289 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1290
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001291 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301292 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +01001293 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001294 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001295 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001296
Akash Goel01fa0302014-03-24 23:00:04 +05301297 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +01001298 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001299 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301300 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001301 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001302
Chris Wilsonc0336662016-05-06 15:40:21 +01001303 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001304 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1305 * "If this bit is set, STCunit will have LRA as replacement
1306 * policy. [...] This bit must be reset. LRA replacement
1307 * policy is not supported."
1308 */
1309 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001310 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001311 }
1312
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001313 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001314 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001315
Chris Wilson61ff75a2016-07-01 17:23:28 +01001316 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -07001317
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001318 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001319}
1320
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001321static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001322{
Chris Wilsonc0336662016-05-06 15:40:21 +01001323 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001324
1325 if (dev_priv->semaphore_obj) {
1326 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1327 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1328 dev_priv->semaphore_obj = NULL;
1329 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001330
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001331 intel_fini_pipe_control(engine);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001332}
1333
John Harrisonf7169682015-05-29 17:44:05 +01001334static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001335 unsigned int num_dwords)
1336{
1337#define MBOX_UPDATE_DWORDS 8
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001338 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001339 struct drm_i915_private *dev_priv = signaller_req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001340 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001341 enum intel_engine_id id;
1342 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001343
Chris Wilsonc0336662016-05-06 15:40:21 +01001344 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawsky3e789982014-06-30 09:53:37 -07001345 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1346#undef MBOX_UPDATE_DWORDS
1347
John Harrison5fb9de12015-05-29 17:44:07 +01001348 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001349 if (ret)
1350 return ret;
1351
Dave Gordonc3232b12016-03-23 18:19:53 +00001352 for_each_engine_id(waiter, dev_priv, id) {
Dave Gordonc3232b12016-03-23 18:19:53 +00001353 u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001354 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1355 continue;
1356
1357 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1358 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1359 PIPE_CONTROL_QW_WRITE |
Chris Wilsonf9a4ea32016-04-29 13:18:24 +01001360 PIPE_CONTROL_CS_STALL);
Ben Widawsky3e789982014-06-30 09:53:37 -07001361 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1362 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
Chris Wilson1b7744e2016-07-01 17:23:17 +01001363 intel_ring_emit(signaller, signaller_req->seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001364 intel_ring_emit(signaller, 0);
1365 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
Chris Wilson215a7e32016-04-29 13:18:23 +01001366 MI_SEMAPHORE_TARGET(waiter->hw_id));
Ben Widawsky3e789982014-06-30 09:53:37 -07001367 intel_ring_emit(signaller, 0);
1368 }
1369
1370 return 0;
1371}
1372
John Harrisonf7169682015-05-29 17:44:05 +01001373static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001374 unsigned int num_dwords)
1375{
1376#define MBOX_UPDATE_DWORDS 6
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001377 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001378 struct drm_i915_private *dev_priv = signaller_req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001379 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001380 enum intel_engine_id id;
1381 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001382
Chris Wilsonc0336662016-05-06 15:40:21 +01001383 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawsky3e789982014-06-30 09:53:37 -07001384 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1385#undef MBOX_UPDATE_DWORDS
1386
John Harrison5fb9de12015-05-29 17:44:07 +01001387 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001388 if (ret)
1389 return ret;
1390
Dave Gordonc3232b12016-03-23 18:19:53 +00001391 for_each_engine_id(waiter, dev_priv, id) {
Dave Gordonc3232b12016-03-23 18:19:53 +00001392 u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001393 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1394 continue;
1395
1396 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1397 MI_FLUSH_DW_OP_STOREDW);
1398 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1399 MI_FLUSH_DW_USE_GTT);
1400 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
Chris Wilson1b7744e2016-07-01 17:23:17 +01001401 intel_ring_emit(signaller, signaller_req->seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001402 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
Chris Wilson215a7e32016-04-29 13:18:23 +01001403 MI_SEMAPHORE_TARGET(waiter->hw_id));
Ben Widawsky3e789982014-06-30 09:53:37 -07001404 intel_ring_emit(signaller, 0);
1405 }
1406
1407 return 0;
1408}
1409
John Harrisonf7169682015-05-29 17:44:05 +01001410static int gen6_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001411 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001412{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001413 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001414 struct drm_i915_private *dev_priv = signaller_req->i915;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001415 struct intel_engine_cs *useless;
Dave Gordonc3232b12016-03-23 18:19:53 +00001416 enum intel_engine_id id;
1417 int ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001418
Ben Widawskya1444b72014-06-30 09:53:35 -07001419#define MBOX_UPDATE_DWORDS 3
Chris Wilsonc0336662016-05-06 15:40:21 +01001420 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawskya1444b72014-06-30 09:53:35 -07001421 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1422#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001423
John Harrison5fb9de12015-05-29 17:44:07 +01001424 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky024a43e2014-04-29 14:52:30 -07001425 if (ret)
1426 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001427
Dave Gordonc3232b12016-03-23 18:19:53 +00001428 for_each_engine_id(useless, dev_priv, id) {
1429 i915_reg_t mbox_reg = signaller->semaphore.mbox.signal[id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001430
1431 if (i915_mmio_reg_valid(mbox_reg)) {
Ben Widawsky78325f22014-04-29 14:52:29 -07001432 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001433 intel_ring_emit_reg(signaller, mbox_reg);
Chris Wilson1b7744e2016-07-01 17:23:17 +01001434 intel_ring_emit(signaller, signaller_req->seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001435 }
1436 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001437
Ben Widawskya1444b72014-06-30 09:53:35 -07001438 /* If num_dwords was rounded, make sure the tail pointer is correct */
1439 if (num_rings % 2 == 0)
1440 intel_ring_emit(signaller, MI_NOOP);
1441
Ben Widawsky024a43e2014-04-29 14:52:30 -07001442 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001443}
1444
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001445/**
1446 * gen6_add_request - Update the semaphore mailbox registers
John Harrisonee044a82015-05-29 17:44:00 +01001447 *
1448 * @request - request to write to the ring
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001449 *
1450 * Update the mailbox registers in the *other* rings with the current seqno.
1451 * This acts like a signal in the canonical semaphore.
1452 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001453static int
John Harrisonee044a82015-05-29 17:44:00 +01001454gen6_add_request(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001455{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001456 struct intel_engine_cs *engine = req->engine;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001457 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001458
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001459 if (engine->semaphore.signal)
1460 ret = engine->semaphore.signal(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001461 else
John Harrison5fb9de12015-05-29 17:44:07 +01001462 ret = intel_ring_begin(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001463
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001464 if (ret)
1465 return ret;
1466
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001467 intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
1468 intel_ring_emit(engine,
1469 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson1b7744e2016-07-01 17:23:17 +01001470 intel_ring_emit(engine, req->seqno);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001471 intel_ring_emit(engine, MI_USER_INTERRUPT);
1472 __intel_ring_advance(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001473
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001474 return 0;
1475}
1476
Chris Wilsona58c01a2016-04-29 13:18:21 +01001477static int
1478gen8_render_add_request(struct drm_i915_gem_request *req)
1479{
1480 struct intel_engine_cs *engine = req->engine;
1481 int ret;
1482
1483 if (engine->semaphore.signal)
1484 ret = engine->semaphore.signal(req, 8);
1485 else
1486 ret = intel_ring_begin(req, 8);
1487 if (ret)
1488 return ret;
1489
1490 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
1491 intel_ring_emit(engine, (PIPE_CONTROL_GLOBAL_GTT_IVB |
1492 PIPE_CONTROL_CS_STALL |
1493 PIPE_CONTROL_QW_WRITE));
1494 intel_ring_emit(engine, intel_hws_seqno_address(req->engine));
1495 intel_ring_emit(engine, 0);
1496 intel_ring_emit(engine, i915_gem_request_get_seqno(req));
1497 /* We're thrashing one dword of HWS. */
1498 intel_ring_emit(engine, 0);
1499 intel_ring_emit(engine, MI_USER_INTERRUPT);
1500 intel_ring_emit(engine, MI_NOOP);
1501 __intel_ring_advance(engine);
1502
1503 return 0;
1504}
1505
Chris Wilsonc0336662016-05-06 15:40:21 +01001506static inline bool i915_gem_has_seqno_wrapped(struct drm_i915_private *dev_priv,
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001507 u32 seqno)
1508{
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001509 return dev_priv->last_seqno < seqno;
1510}
1511
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001512/**
1513 * intel_ring_sync - sync the waiter to the signaller on seqno
1514 *
1515 * @waiter - ring that is waiting
1516 * @signaller - ring which has, or will signal
1517 * @seqno - seqno which the waiter will block on
1518 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001519
1520static int
John Harrison599d9242015-05-29 17:44:04 +01001521gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001522 struct intel_engine_cs *signaller,
1523 u32 seqno)
1524{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001525 struct intel_engine_cs *waiter = waiter_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001526 struct drm_i915_private *dev_priv = waiter_req->i915;
Tvrtko Ursulinc38c6512016-06-29 16:09:30 +01001527 u64 offset = GEN8_WAIT_OFFSET(waiter, signaller->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001528 struct i915_hw_ppgtt *ppgtt;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001529 int ret;
1530
John Harrison5fb9de12015-05-29 17:44:07 +01001531 ret = intel_ring_begin(waiter_req, 4);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001532 if (ret)
1533 return ret;
1534
1535 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1536 MI_SEMAPHORE_GLOBAL_GTT |
1537 MI_SEMAPHORE_SAD_GTE_SDD);
1538 intel_ring_emit(waiter, seqno);
Tvrtko Ursulinc38c6512016-06-29 16:09:30 +01001539 intel_ring_emit(waiter, lower_32_bits(offset));
1540 intel_ring_emit(waiter, upper_32_bits(offset));
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001541 intel_ring_advance(waiter);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001542
1543 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1544 * pagetables and we must reload them before executing the batch.
1545 * We do this on the i915_switch_context() following the wait and
1546 * before the dispatch.
1547 */
1548 ppgtt = waiter_req->ctx->ppgtt;
1549 if (ppgtt && waiter_req->engine->id != RCS)
1550 ppgtt->pd_dirty_rings |= intel_engine_flag(waiter_req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001551 return 0;
1552}
1553
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001554static int
John Harrison599d9242015-05-29 17:44:04 +01001555gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001556 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001557 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001558{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001559 struct intel_engine_cs *waiter = waiter_req->engine;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001560 u32 dw1 = MI_SEMAPHORE_MBOX |
1561 MI_SEMAPHORE_COMPARE |
1562 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001563 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1564 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001565
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001566 /* Throughout all of the GEM code, seqno passed implies our current
1567 * seqno is >= the last seqno executed. However for hardware the
1568 * comparison is strictly greater than.
1569 */
1570 seqno -= 1;
1571
Ben Widawskyebc348b2014-04-29 14:52:28 -07001572 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001573
John Harrison5fb9de12015-05-29 17:44:07 +01001574 ret = intel_ring_begin(waiter_req, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001575 if (ret)
1576 return ret;
1577
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001578 /* If seqno wrap happened, omit the wait with no-ops */
Chris Wilsonc0336662016-05-06 15:40:21 +01001579 if (likely(!i915_gem_has_seqno_wrapped(waiter_req->i915, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001580 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001581 intel_ring_emit(waiter, seqno);
1582 intel_ring_emit(waiter, 0);
1583 intel_ring_emit(waiter, MI_NOOP);
1584 } else {
1585 intel_ring_emit(waiter, MI_NOOP);
1586 intel_ring_emit(waiter, MI_NOOP);
1587 intel_ring_emit(waiter, MI_NOOP);
1588 intel_ring_emit(waiter, MI_NOOP);
1589 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001590 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001591
1592 return 0;
1593}
1594
Chris Wilsonf8973c22016-07-01 17:23:21 +01001595static void
1596gen5_seqno_barrier(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001597{
Chris Wilsonf8973c22016-07-01 17:23:21 +01001598 /* MI_STORE are internally buffered by the GPU and not flushed
1599 * either by MI_FLUSH or SyncFlush or any other combination of
1600 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001601 *
Chris Wilsonf8973c22016-07-01 17:23:21 +01001602 * "Only the submission of the store operation is guaranteed.
1603 * The write result will be complete (coherent) some time later
1604 * (this is practically a finite period but there is no guaranteed
1605 * latency)."
1606 *
1607 * Empirically, we observe that we need a delay of at least 75us to
1608 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001609 */
Chris Wilsonf8973c22016-07-01 17:23:21 +01001610 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001611}
1612
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001613static void
1614gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001615{
Chris Wilsonc0336662016-05-06 15:40:21 +01001616 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001617
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001618 /* Workaround to force correct ordering between irq and seqno writes on
1619 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +01001620 * ACTHD) before reading the status page.
1621 *
1622 * Note that this effectively stalls the read by the time it takes to
1623 * do a memory transaction, which more or less ensures that the write
1624 * from the GPU has sufficient time to invalidate the CPU cacheline.
1625 * Alternatively we could delay the interrupt from the CS ring to give
1626 * the write time to land, but that would incur a delay after every
1627 * batch i.e. much more frequent than a delay when waiting for the
1628 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001629 *
1630 * Also note that to prevent whole machine hangs on gen7, we have to
1631 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +01001632 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001633 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001634 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001635 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001636}
1637
Chris Wilson31bb59c2016-07-01 17:23:27 +01001638static void
1639gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001640{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001641 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +02001642}
1643
1644static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001645gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001646{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001647 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001648}
1649
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001650static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001651i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001652{
Chris Wilsonc0336662016-05-06 15:40:21 +01001653 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001654
Chris Wilson31bb59c2016-07-01 17:23:27 +01001655 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1656 I915_WRITE(IMR, dev_priv->irq_mask);
1657 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +01001658}
1659
1660static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001661i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001662{
Chris Wilsonc0336662016-05-06 15:40:21 +01001663 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001664
Chris Wilson31bb59c2016-07-01 17:23:27 +01001665 dev_priv->irq_mask |= engine->irq_enable_mask;
1666 I915_WRITE(IMR, dev_priv->irq_mask);
1667}
1668
1669static void
1670i8xx_irq_enable(struct intel_engine_cs *engine)
1671{
1672 struct drm_i915_private *dev_priv = engine->i915;
1673
1674 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1675 I915_WRITE16(IMR, dev_priv->irq_mask);
1676 POSTING_READ16(RING_IMR(engine->mmio_base));
1677}
1678
1679static void
1680i8xx_irq_disable(struct intel_engine_cs *engine)
1681{
1682 struct drm_i915_private *dev_priv = engine->i915;
1683
1684 dev_priv->irq_mask |= engine->irq_enable_mask;
1685 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001686}
1687
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001688static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001689bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001690 u32 invalidate_domains,
1691 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001692{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001693 struct intel_engine_cs *engine = req->engine;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001694 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001695
John Harrison5fb9de12015-05-29 17:44:07 +01001696 ret = intel_ring_begin(req, 2);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001697 if (ret)
1698 return ret;
1699
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001700 intel_ring_emit(engine, MI_FLUSH);
1701 intel_ring_emit(engine, MI_NOOP);
1702 intel_ring_advance(engine);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001703 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001704}
1705
Chris Wilson3cce4692010-10-27 16:11:02 +01001706static int
John Harrisonee044a82015-05-29 17:44:00 +01001707i9xx_add_request(struct drm_i915_gem_request *req)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001708{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001709 struct intel_engine_cs *engine = req->engine;
Chris Wilson3cce4692010-10-27 16:11:02 +01001710 int ret;
1711
John Harrison5fb9de12015-05-29 17:44:07 +01001712 ret = intel_ring_begin(req, 4);
Chris Wilson3cce4692010-10-27 16:11:02 +01001713 if (ret)
1714 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +01001715
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001716 intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
1717 intel_ring_emit(engine,
1718 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson1b7744e2016-07-01 17:23:17 +01001719 intel_ring_emit(engine, req->seqno);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001720 intel_ring_emit(engine, MI_USER_INTERRUPT);
1721 __intel_ring_advance(engine);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001722
Chris Wilson3cce4692010-10-27 16:11:02 +01001723 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001724}
1725
Chris Wilson0f468322011-01-04 17:35:21 +00001726static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001727gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001728{
Chris Wilsonc0336662016-05-06 15:40:21 +01001729 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001730
Chris Wilson61ff75a2016-07-01 17:23:28 +01001731 I915_WRITE_IMR(engine,
1732 ~(engine->irq_enable_mask |
1733 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001734 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001735}
1736
1737static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001738gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001739{
Chris Wilsonc0336662016-05-06 15:40:21 +01001740 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001741
Chris Wilson61ff75a2016-07-01 17:23:28 +01001742 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001743 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001744}
1745
1746static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001747hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001748{
Chris Wilsonc0336662016-05-06 15:40:21 +01001749 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001750
Chris Wilson31bb59c2016-07-01 17:23:27 +01001751 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
1752 gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
1753}
1754
1755static void
1756hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1757{
1758 struct drm_i915_private *dev_priv = engine->i915;
1759
1760 I915_WRITE_IMR(engine, ~0);
1761 gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
1762}
1763
1764static void
1765gen8_irq_enable(struct intel_engine_cs *engine)
1766{
1767 struct drm_i915_private *dev_priv = engine->i915;
1768
Chris Wilson61ff75a2016-07-01 17:23:28 +01001769 I915_WRITE_IMR(engine,
1770 ~(engine->irq_enable_mask |
1771 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001772 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1773}
1774
1775static void
1776gen8_irq_disable(struct intel_engine_cs *engine)
1777{
1778 struct drm_i915_private *dev_priv = engine->i915;
1779
Chris Wilson61ff75a2016-07-01 17:23:28 +01001780 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001781}
1782
Zou Nan haid1b851f2010-05-21 09:08:57 +08001783static int
John Harrison53fddaf2015-05-29 17:44:02 +01001784i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001785 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001786 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001787{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001788 struct intel_engine_cs *engine = req->engine;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001789 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001790
John Harrison5fb9de12015-05-29 17:44:07 +01001791 ret = intel_ring_begin(req, 2);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001792 if (ret)
1793 return ret;
1794
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001795 intel_ring_emit(engine,
Chris Wilson65f56872012-04-17 16:38:12 +01001796 MI_BATCH_BUFFER_START |
1797 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001798 (dispatch_flags & I915_DISPATCH_SECURE ?
1799 0 : MI_BATCH_NON_SECURE_I965));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001800 intel_ring_emit(engine, offset);
1801 intel_ring_advance(engine);
Chris Wilson78501ea2010-10-27 12:18:21 +01001802
Zou Nan haid1b851f2010-05-21 09:08:57 +08001803 return 0;
1804}
1805
Daniel Vetterb45305f2012-12-17 16:21:27 +01001806/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1807#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001808#define I830_TLB_ENTRIES (2)
1809#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001810static int
John Harrison53fddaf2015-05-29 17:44:02 +01001811i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001812 u64 offset, u32 len,
1813 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001814{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001815 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001816 u32 cs_offset = engine->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001817 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001818
John Harrison5fb9de12015-05-29 17:44:07 +01001819 ret = intel_ring_begin(req, 6);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001820 if (ret)
1821 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001822
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001823 /* Evict the invalid PTE TLBs */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001824 intel_ring_emit(engine, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1825 intel_ring_emit(engine, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1826 intel_ring_emit(engine, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1827 intel_ring_emit(engine, cs_offset);
1828 intel_ring_emit(engine, 0xdeadbeef);
1829 intel_ring_emit(engine, MI_NOOP);
1830 intel_ring_advance(engine);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001831
John Harrison8e004ef2015-02-13 11:48:10 +00001832 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001833 if (len > I830_BATCH_LIMIT)
1834 return -ENOSPC;
1835
John Harrison5fb9de12015-05-29 17:44:07 +01001836 ret = intel_ring_begin(req, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001837 if (ret)
1838 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001839
1840 /* Blit the batch (which has now all relocs applied) to the
1841 * stable batch scratch bo area (so that the CS never
1842 * stumbles over its tlb invalidation bug) ...
1843 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001844 intel_ring_emit(engine, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1845 intel_ring_emit(engine,
1846 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
1847 intel_ring_emit(engine, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1848 intel_ring_emit(engine, cs_offset);
1849 intel_ring_emit(engine, 4096);
1850 intel_ring_emit(engine, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001851
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001852 intel_ring_emit(engine, MI_FLUSH);
1853 intel_ring_emit(engine, MI_NOOP);
1854 intel_ring_advance(engine);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001855
1856 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001857 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001858 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001859
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001860 ret = intel_ring_begin(req, 2);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001861 if (ret)
1862 return ret;
1863
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001864 intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1865 intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1866 0 : MI_BATCH_NON_SECURE));
1867 intel_ring_advance(engine);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001868
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001869 return 0;
1870}
1871
1872static int
John Harrison53fddaf2015-05-29 17:44:02 +01001873i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001874 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001875 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001876{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001877 struct intel_engine_cs *engine = req->engine;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001878 int ret;
1879
John Harrison5fb9de12015-05-29 17:44:07 +01001880 ret = intel_ring_begin(req, 2);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001881 if (ret)
1882 return ret;
1883
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001884 intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1885 intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1886 0 : MI_BATCH_NON_SECURE));
1887 intel_ring_advance(engine);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001888
Eric Anholt62fdfea2010-05-21 13:26:39 -07001889 return 0;
1890}
1891
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001892static void cleanup_phys_status_page(struct intel_engine_cs *engine)
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001893{
Chris Wilsonc0336662016-05-06 15:40:21 +01001894 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001895
1896 if (!dev_priv->status_page_dmah)
1897 return;
1898
Chris Wilsonc0336662016-05-06 15:40:21 +01001899 drm_pci_free(dev_priv->dev, dev_priv->status_page_dmah);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001900 engine->status_page.page_addr = NULL;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001901}
1902
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001903static void cleanup_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001904{
Chris Wilson05394f32010-11-08 19:18:58 +00001905 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001906
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001907 obj = engine->status_page.obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001908 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001909 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001910
Chris Wilson9da3da62012-06-01 15:20:22 +01001911 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001912 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001913 drm_gem_object_unreference(&obj->base);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001914 engine->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001915}
1916
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001917static int init_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001918{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001919 struct drm_i915_gem_object *obj = engine->status_page.obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001920
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001921 if (obj == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001922 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001923 int ret;
1924
Chris Wilsonc0336662016-05-06 15:40:21 +01001925 obj = i915_gem_object_create(engine->i915->dev, 4096);
Chris Wilsonfe3db792016-04-25 13:32:13 +01001926 if (IS_ERR(obj)) {
Chris Wilsone3efda42014-04-09 09:19:41 +01001927 DRM_ERROR("Failed to allocate status page\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01001928 return PTR_ERR(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001929 }
1930
1931 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1932 if (ret)
1933 goto err_unref;
1934
Chris Wilson1f767e02014-07-03 17:33:03 -04001935 flags = 0;
Chris Wilsonc0336662016-05-06 15:40:21 +01001936 if (!HAS_LLC(engine->i915))
Chris Wilson1f767e02014-07-03 17:33:03 -04001937 /* On g33, we cannot place HWS above 256MiB, so
1938 * restrict its pinning to the low mappable arena.
1939 * Though this restriction is not documented for
1940 * gen4, gen5, or byt, they also behave similarly
1941 * and hang if the HWS is placed at the top of the
1942 * GTT. To generalise, it appears that all !llc
1943 * platforms have issues with us placing the HWS
1944 * above the mappable region (even though we never
1945 * actualy map it).
1946 */
1947 flags |= PIN_MAPPABLE;
1948 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001949 if (ret) {
1950err_unref:
1951 drm_gem_object_unreference(&obj->base);
1952 return ret;
1953 }
1954
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001955 engine->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001956 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001957
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001958 engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1959 engine->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
1960 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001961
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001962 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001963 engine->name, engine->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001964
1965 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001966}
1967
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001968static int init_phys_status_page(struct intel_engine_cs *engine)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001969{
Chris Wilsonc0336662016-05-06 15:40:21 +01001970 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001971
1972 if (!dev_priv->status_page_dmah) {
1973 dev_priv->status_page_dmah =
Chris Wilsonc0336662016-05-06 15:40:21 +01001974 drm_pci_alloc(dev_priv->dev, PAGE_SIZE, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001975 if (!dev_priv->status_page_dmah)
1976 return -ENOMEM;
1977 }
1978
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001979 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1980 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001981
1982 return 0;
1983}
1984
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001985void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1986{
Chris Wilson3d77e9b2016-04-28 09:56:40 +01001987 GEM_BUG_ON(ringbuf->vma == NULL);
1988 GEM_BUG_ON(ringbuf->virtual_start == NULL);
1989
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001990 if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
Chris Wilson0a798eb2016-04-08 12:11:11 +01001991 i915_gem_object_unpin_map(ringbuf->obj);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001992 else
Chris Wilson3d77e9b2016-04-28 09:56:40 +01001993 i915_vma_unpin_iomap(ringbuf->vma);
Dave Gordon83052162016-04-12 14:46:16 +01001994 ringbuf->virtual_start = NULL;
Chris Wilson3d77e9b2016-04-28 09:56:40 +01001995
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001996 i915_gem_object_ggtt_unpin(ringbuf->obj);
Chris Wilson3d77e9b2016-04-28 09:56:40 +01001997 ringbuf->vma = NULL;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001998}
1999
Chris Wilsonc0336662016-05-06 15:40:21 +01002000int intel_pin_and_map_ringbuffer_obj(struct drm_i915_private *dev_priv,
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002001 struct intel_ringbuffer *ringbuf)
2002{
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002003 struct drm_i915_gem_object *obj = ringbuf->obj;
Chris Wilsona687a432016-04-13 17:35:11 +01002004 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
2005 unsigned flags = PIN_OFFSET_BIAS | 4096;
Dave Gordon83052162016-04-12 14:46:16 +01002006 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002007 int ret;
2008
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002009 if (HAS_LLC(dev_priv) && !obj->stolen) {
Chris Wilsona687a432016-04-13 17:35:11 +01002010 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002011 if (ret)
2012 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002013
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002014 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsond2cad532016-04-08 12:11:10 +01002015 if (ret)
2016 goto err_unpin;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002017
Dave Gordon83052162016-04-12 14:46:16 +01002018 addr = i915_gem_object_pin_map(obj);
2019 if (IS_ERR(addr)) {
2020 ret = PTR_ERR(addr);
Chris Wilsond2cad532016-04-08 12:11:10 +01002021 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002022 }
2023 } else {
Chris Wilsona687a432016-04-13 17:35:11 +01002024 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
2025 flags | PIN_MAPPABLE);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002026 if (ret)
2027 return ret;
2028
2029 ret = i915_gem_object_set_to_gtt_domain(obj, true);
Chris Wilsond2cad532016-04-08 12:11:10 +01002030 if (ret)
2031 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002032
Daniele Ceraolo Spurioff3dc082016-01-27 15:43:49 +00002033 /* Access through the GTT requires the device to be awake. */
2034 assert_rpm_wakelock_held(dev_priv);
2035
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002036 addr = i915_vma_pin_iomap(i915_gem_obj_to_ggtt(obj));
2037 if (IS_ERR(addr)) {
2038 ret = PTR_ERR(addr);
Chris Wilsond2cad532016-04-08 12:11:10 +01002039 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002040 }
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002041 }
2042
Dave Gordon83052162016-04-12 14:46:16 +01002043 ringbuf->virtual_start = addr;
Tvrtko Ursulin0eb973d2016-01-15 15:10:28 +00002044 ringbuf->vma = i915_gem_obj_to_ggtt(obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002045 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01002046
2047err_unpin:
2048 i915_gem_object_ggtt_unpin(obj);
2049 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002050}
2051
Chris Wilson01101fa2015-09-03 13:01:39 +01002052static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01002053{
Oscar Mateo2919d292014-07-03 16:28:02 +01002054 drm_gem_object_unreference(&ringbuf->obj->base);
2055 ringbuf->obj = NULL;
2056}
2057
Chris Wilson01101fa2015-09-03 13:01:39 +01002058static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
2059 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01002060{
Chris Wilsone3efda42014-04-09 09:19:41 +01002061 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002062
2063 obj = NULL;
2064 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002065 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002066 if (obj == NULL)
Dave Gordond37cd8a2016-04-22 19:14:32 +01002067 obj = i915_gem_object_create(dev, ringbuf->size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002068 if (IS_ERR(obj))
2069 return PTR_ERR(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01002070
Akash Goel24f3a8c2014-06-17 10:59:42 +05302071 /* mark ring buffers as read-only from GPU side by default */
2072 obj->gt_ro = 1;
2073
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002074 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002075
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002076 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002077}
2078
Chris Wilson01101fa2015-09-03 13:01:39 +01002079struct intel_ringbuffer *
2080intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
2081{
2082 struct intel_ringbuffer *ring;
2083 int ret;
2084
2085 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson608c1a52015-09-03 13:01:40 +01002086 if (ring == NULL) {
2087 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2088 engine->name);
Chris Wilson01101fa2015-09-03 13:01:39 +01002089 return ERR_PTR(-ENOMEM);
Chris Wilson608c1a52015-09-03 13:01:40 +01002090 }
Chris Wilson01101fa2015-09-03 13:01:39 +01002091
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002092 ring->engine = engine;
Chris Wilson608c1a52015-09-03 13:01:40 +01002093 list_add(&ring->link, &engine->buffers);
Chris Wilson01101fa2015-09-03 13:01:39 +01002094
2095 ring->size = size;
2096 /* Workaround an erratum on the i830 which causes a hang if
2097 * the TAIL pointer points to within the last 2 cachelines
2098 * of the buffer.
2099 */
2100 ring->effective_size = size;
Chris Wilsonc0336662016-05-06 15:40:21 +01002101 if (IS_I830(engine->i915) || IS_845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01002102 ring->effective_size -= 2 * CACHELINE_BYTES;
2103
2104 ring->last_retired_head = -1;
2105 intel_ring_update_space(ring);
2106
Chris Wilsonc0336662016-05-06 15:40:21 +01002107 ret = intel_alloc_ringbuffer_obj(engine->i915->dev, ring);
Chris Wilson01101fa2015-09-03 13:01:39 +01002108 if (ret) {
Chris Wilson608c1a52015-09-03 13:01:40 +01002109 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
2110 engine->name, ret);
2111 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002112 kfree(ring);
2113 return ERR_PTR(ret);
2114 }
2115
2116 return ring;
2117}
2118
2119void
2120intel_ringbuffer_free(struct intel_ringbuffer *ring)
2121{
2122 intel_destroy_ringbuffer_obj(ring);
Chris Wilson608c1a52015-09-03 13:01:40 +01002123 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002124 kfree(ring);
2125}
2126
Chris Wilson0cb26a82016-06-24 14:55:53 +01002127static int intel_ring_context_pin(struct i915_gem_context *ctx,
2128 struct intel_engine_cs *engine)
2129{
2130 struct intel_context *ce = &ctx->engine[engine->id];
2131 int ret;
2132
2133 lockdep_assert_held(&ctx->i915->dev->struct_mutex);
2134
2135 if (ce->pin_count++)
2136 return 0;
2137
2138 if (ce->state) {
2139 ret = i915_gem_obj_ggtt_pin(ce->state, ctx->ggtt_alignment, 0);
2140 if (ret)
2141 goto error;
2142 }
2143
Chris Wilsonc7c3c072016-06-24 14:55:54 +01002144 /* The kernel context is only used as a placeholder for flushing the
2145 * active context. It is never used for submitting user rendering and
2146 * as such never requires the golden render context, and so we can skip
2147 * emitting it when we switch to the kernel context. This is required
2148 * as during eviction we cannot allocate and pin the renderstate in
2149 * order to initialise the context.
2150 */
2151 if (ctx == ctx->i915->kernel_context)
2152 ce->initialised = true;
2153
Chris Wilson0cb26a82016-06-24 14:55:53 +01002154 i915_gem_context_reference(ctx);
2155 return 0;
2156
2157error:
2158 ce->pin_count = 0;
2159 return ret;
2160}
2161
2162static void intel_ring_context_unpin(struct i915_gem_context *ctx,
2163 struct intel_engine_cs *engine)
2164{
2165 struct intel_context *ce = &ctx->engine[engine->id];
2166
2167 lockdep_assert_held(&ctx->i915->dev->struct_mutex);
2168
2169 if (--ce->pin_count)
2170 return;
2171
2172 if (ce->state)
2173 i915_gem_object_ggtt_unpin(ce->state);
2174
2175 i915_gem_context_unreference(ctx);
2176}
2177
Ben Widawskyc43b5632012-04-16 14:07:40 -07002178static int intel_init_ring_buffer(struct drm_device *dev,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002179 struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002180{
Chris Wilsonc0336662016-05-06 15:40:21 +01002181 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002182 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002183 int ret;
2184
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002185 WARN_ON(engine->buffer);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002186
Chris Wilsonc0336662016-05-06 15:40:21 +01002187 engine->i915 = dev_priv;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002188 INIT_LIST_HEAD(&engine->active_list);
2189 INIT_LIST_HEAD(&engine->request_list);
2190 INIT_LIST_HEAD(&engine->execlist_queue);
2191 INIT_LIST_HEAD(&engine->buffers);
2192 i915_gem_batch_pool_init(dev, &engine->batch_pool);
2193 memset(engine->semaphore.sync_seqno, 0,
2194 sizeof(engine->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002195
Chris Wilson688e6c72016-07-01 17:23:15 +01002196 ret = intel_engine_init_breadcrumbs(engine);
2197 if (ret)
2198 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002199
Chris Wilson0cb26a82016-06-24 14:55:53 +01002200 /* We may need to do things with the shrinker which
2201 * require us to immediately switch back to the default
2202 * context. This can cause a problem as pinning the
2203 * default context also requires GTT space which may not
2204 * be available. To avoid this we always pin the default
2205 * context.
2206 */
2207 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2208 if (ret)
2209 goto error;
2210
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002211 ringbuf = intel_engine_create_ringbuffer(engine, 32 * PAGE_SIZE);
Dave Gordonb0366a52015-12-08 15:02:36 +00002212 if (IS_ERR(ringbuf)) {
2213 ret = PTR_ERR(ringbuf);
2214 goto error;
2215 }
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002216 engine->buffer = ringbuf;
Chris Wilson01101fa2015-09-03 13:01:39 +01002217
Chris Wilsonc0336662016-05-06 15:40:21 +01002218 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002219 ret = init_status_page(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002220 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002221 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002222 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002223 WARN_ON(engine->id != RCS);
2224 ret = init_phys_status_page(engine);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002225 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002226 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002227 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002228
Chris Wilsonc0336662016-05-06 15:40:21 +01002229 ret = intel_pin_and_map_ringbuffer_obj(dev_priv, ringbuf);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002230 if (ret) {
2231 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002232 engine->name, ret);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002233 intel_destroy_ringbuffer_obj(ringbuf);
2234 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002235 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002236
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002237 ret = i915_cmd_parser_init_ring(engine);
Brad Volkin44e895a2014-05-10 14:10:43 -07002238 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002239 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002240
Oscar Mateo8ee14972014-05-22 14:13:34 +01002241 return 0;
2242
2243error:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002244 intel_cleanup_engine(engine);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002245 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002246}
2247
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002248void intel_cleanup_engine(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002249{
John Harrison6402c332014-10-31 12:00:26 +00002250 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01002251
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002252 if (!intel_engine_initialized(engine))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002253 return;
2254
Chris Wilsonc0336662016-05-06 15:40:21 +01002255 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00002256
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002257 if (engine->buffer) {
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002258 intel_stop_engine(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01002259 WARN_ON(!IS_GEN2(dev_priv) && (I915_READ_MODE(engine) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002260
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002261 intel_unpin_ringbuffer_obj(engine->buffer);
2262 intel_ringbuffer_free(engine->buffer);
2263 engine->buffer = NULL;
Dave Gordonb0366a52015-12-08 15:02:36 +00002264 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002265
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002266 if (engine->cleanup)
2267 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08002268
Chris Wilsonc0336662016-05-06 15:40:21 +01002269 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002270 cleanup_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002271 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002272 WARN_ON(engine->id != RCS);
2273 cleanup_phys_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002274 }
Brad Volkin44e895a2014-05-10 14:10:43 -07002275
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002276 i915_cmd_parser_fini_ring(engine);
2277 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson688e6c72016-07-01 17:23:15 +01002278 intel_engine_fini_breadcrumbs(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002279
2280 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2281
Chris Wilsonc0336662016-05-06 15:40:21 +01002282 engine->i915 = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002283}
2284
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002285int intel_engine_idle(struct intel_engine_cs *engine)
Chris Wilson3e960502012-11-27 16:22:54 +00002286{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002287 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002288
Chris Wilson3e960502012-11-27 16:22:54 +00002289 /* Wait upon the last request to be completed */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002290 if (list_empty(&engine->request_list))
Chris Wilson3e960502012-11-27 16:22:54 +00002291 return 0;
2292
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002293 req = list_entry(engine->request_list.prev,
2294 struct drm_i915_gem_request,
2295 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002296
Chris Wilsonb4716182015-04-27 13:41:17 +01002297 /* Make sure we do not trigger any retires */
2298 return __i915_wait_request(req,
Chris Wilsonc19ae982016-04-13 17:35:03 +01002299 req->i915->mm.interruptible,
Chris Wilsonb4716182015-04-27 13:41:17 +01002300 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002301}
2302
John Harrison6689cb22015-03-19 12:30:08 +00002303int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002304{
Chris Wilson63103462016-04-28 09:56:49 +01002305 int ret;
2306
2307 /* Flush enough space to reduce the likelihood of waiting after
2308 * we start building the request - in which case we will just
2309 * have to repeat work.
2310 */
Chris Wilsona0442462016-04-29 09:07:05 +01002311 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002312
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002313 request->ringbuf = request->engine->buffer;
Chris Wilson63103462016-04-28 09:56:49 +01002314
2315 ret = intel_ring_begin(request, 0);
2316 if (ret)
2317 return ret;
2318
Chris Wilsona0442462016-04-29 09:07:05 +01002319 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002320 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002321}
2322
Chris Wilson987046a2016-04-28 09:56:46 +01002323static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002324{
Chris Wilson987046a2016-04-28 09:56:46 +01002325 struct intel_ringbuffer *ringbuf = req->ringbuf;
2326 struct intel_engine_cs *engine = req->engine;
2327 struct drm_i915_gem_request *target;
2328
2329 intel_ring_update_space(ringbuf);
2330 if (ringbuf->space >= bytes)
2331 return 0;
2332
2333 /*
2334 * Space is reserved in the ringbuffer for finalising the request,
2335 * as that cannot be allowed to fail. During request finalisation,
2336 * reserved_space is set to 0 to stop the overallocation and the
2337 * assumption is that then we never need to wait (which has the
2338 * risk of failing with EINTR).
2339 *
2340 * See also i915_gem_request_alloc() and i915_add_request().
2341 */
Chris Wilson0251a962016-04-28 09:56:47 +01002342 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01002343
2344 list_for_each_entry(target, &engine->request_list, list) {
2345 unsigned space;
2346
2347 /*
2348 * The request queue is per-engine, so can contain requests
2349 * from multiple ringbuffers. Here, we must ignore any that
2350 * aren't from the ringbuffer we're considering.
2351 */
2352 if (target->ringbuf != ringbuf)
2353 continue;
2354
2355 /* Would completion of this request free enough space? */
2356 space = __intel_ring_space(target->postfix, ringbuf->tail,
2357 ringbuf->size);
2358 if (space >= bytes)
2359 break;
2360 }
2361
2362 if (WARN_ON(&target->list == &engine->request_list))
2363 return -ENOSPC;
2364
2365 return i915_wait_request(target);
2366}
2367
2368int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
2369{
2370 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison79bbcc22015-06-30 12:40:55 +01002371 int remain_actual = ringbuf->size - ringbuf->tail;
Chris Wilson987046a2016-04-28 09:56:46 +01002372 int remain_usable = ringbuf->effective_size - ringbuf->tail;
2373 int bytes = num_dwords * sizeof(u32);
2374 int total_bytes, wait_bytes;
John Harrison79bbcc22015-06-30 12:40:55 +01002375 bool need_wrap = false;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002376
Chris Wilson0251a962016-04-28 09:56:47 +01002377 total_bytes = bytes + req->reserved_space;
John Harrison29b1b412015-06-18 13:10:09 +01002378
John Harrison79bbcc22015-06-30 12:40:55 +01002379 if (unlikely(bytes > remain_usable)) {
2380 /*
2381 * Not enough space for the basic request. So need to flush
2382 * out the remainder and then wait for base + reserved.
2383 */
2384 wait_bytes = remain_actual + total_bytes;
2385 need_wrap = true;
Chris Wilson987046a2016-04-28 09:56:46 +01002386 } else if (unlikely(total_bytes > remain_usable)) {
2387 /*
2388 * The base request will fit but the reserved space
2389 * falls off the end. So we don't need an immediate wrap
2390 * and only need to effectively wait for the reserved
2391 * size space from the start of ringbuffer.
2392 */
Chris Wilson0251a962016-04-28 09:56:47 +01002393 wait_bytes = remain_actual + req->reserved_space;
John Harrison79bbcc22015-06-30 12:40:55 +01002394 } else {
Chris Wilson987046a2016-04-28 09:56:46 +01002395 /* No wrapping required, just waiting. */
2396 wait_bytes = total_bytes;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002397 }
2398
Chris Wilson987046a2016-04-28 09:56:46 +01002399 if (wait_bytes > ringbuf->space) {
2400 int ret = wait_for_space(req, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002401 if (unlikely(ret))
2402 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +01002403
Chris Wilson987046a2016-04-28 09:56:46 +01002404 intel_ring_update_space(ringbuf);
Chris Wilsone075a322016-05-13 11:57:22 +01002405 if (unlikely(ringbuf->space < wait_bytes))
2406 return -EAGAIN;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002407 }
2408
Chris Wilson987046a2016-04-28 09:56:46 +01002409 if (unlikely(need_wrap)) {
2410 GEM_BUG_ON(remain_actual > ringbuf->space);
2411 GEM_BUG_ON(ringbuf->tail + remain_actual > ringbuf->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002412
Chris Wilson987046a2016-04-28 09:56:46 +01002413 /* Fill the tail with MI_NOOP */
2414 memset(ringbuf->virtual_start + ringbuf->tail,
2415 0, remain_actual);
2416 ringbuf->tail = 0;
2417 ringbuf->space -= remain_actual;
2418 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002419
Chris Wilson987046a2016-04-28 09:56:46 +01002420 ringbuf->space -= bytes;
2421 GEM_BUG_ON(ringbuf->space < 0);
Chris Wilson304d6952014-01-02 14:32:35 +00002422 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002423}
2424
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002425/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01002426int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002427{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002428 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002429 int num_dwords = (engine->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002430 int ret;
2431
2432 if (num_dwords == 0)
2433 return 0;
2434
Chris Wilson18393f62014-04-09 09:19:40 +01002435 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
John Harrison5fb9de12015-05-29 17:44:07 +01002436 ret = intel_ring_begin(req, num_dwords);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002437 if (ret)
2438 return ret;
2439
2440 while (num_dwords--)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002441 intel_ring_emit(engine, MI_NOOP);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002442
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002443 intel_ring_advance(engine);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002444
2445 return 0;
2446}
2447
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002448void intel_ring_init_seqno(struct intel_engine_cs *engine, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002449{
Chris Wilsonc0336662016-05-06 15:40:21 +01002450 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002451
Chris Wilson29dcb572016-04-07 07:29:13 +01002452 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
2453 * so long as the semaphore value in the register/page is greater
2454 * than the sync value), so whenever we reset the seqno,
2455 * so long as we reset the tracking semaphore value to 0, it will
2456 * always be before the next request's seqno. If we don't reset
2457 * the semaphore value, then when the seqno moves backwards all
2458 * future waits will complete instantly (causing rendering corruption).
2459 */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01002460 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002461 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
2462 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
Chris Wilsond04bce42016-04-07 07:29:12 +01002463 if (HAS_VEBOX(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002464 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002465 }
Chris Wilsona058d932016-04-07 07:29:15 +01002466 if (dev_priv->semaphore_obj) {
2467 struct drm_i915_gem_object *obj = dev_priv->semaphore_obj;
2468 struct page *page = i915_gem_object_get_dirty_page(obj, 0);
2469 void *semaphores = kmap(page);
2470 memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
2471 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
2472 kunmap(page);
2473 }
Chris Wilson29dcb572016-04-07 07:29:13 +01002474 memset(engine->semaphore.sync_seqno, 0,
2475 sizeof(engine->semaphore.sync_seqno));
Chris Wilson297b0c52010-10-22 17:02:41 +01002476
Chris Wilson1b7744e2016-07-01 17:23:17 +01002477 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
2478 if (engine->irq_seqno_barrier)
2479 engine->irq_seqno_barrier(engine);
Chris Wilson01347122016-04-07 07:29:16 +01002480 engine->last_submitted_seqno = seqno;
Chris Wilson29dcb572016-04-07 07:29:13 +01002481
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002482 engine->hangcheck.seqno = seqno;
Chris Wilson688e6c72016-07-01 17:23:15 +01002483
2484 /* After manually advancing the seqno, fake the interrupt in case
2485 * there are any waiters for that seqno.
2486 */
2487 rcu_read_lock();
2488 intel_engine_wakeup(engine);
2489 rcu_read_unlock();
Chris Wilson549f7362010-10-19 11:19:32 +01002490}
2491
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002492static void gen6_bsd_ring_write_tail(struct intel_engine_cs *engine,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002493 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002494{
Chris Wilsonc0336662016-05-06 15:40:21 +01002495 struct drm_i915_private *dev_priv = engine->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002496
Chris Wilson76f84212016-06-30 15:33:45 +01002497 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2498
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002499 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002500
Chris Wilson12f55812012-07-05 17:14:01 +01002501 /* Disable notification that the ring is IDLE. The GT
2502 * will then assume that it is busy and bring it out of rc6.
2503 */
Chris Wilson76f84212016-06-30 15:33:45 +01002504 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2505 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01002506
2507 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01002508 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01002509
2510 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson76f84212016-06-30 15:33:45 +01002511 if (intel_wait_for_register_fw(dev_priv,
2512 GEN6_BSD_SLEEP_PSMI_CONTROL,
2513 GEN6_BSD_SLEEP_INDICATOR,
2514 0,
2515 50))
Chris Wilson12f55812012-07-05 17:14:01 +01002516 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002517
Chris Wilson12f55812012-07-05 17:14:01 +01002518 /* Now that the ring is fully powered up, update the tail */
Chris Wilson76f84212016-06-30 15:33:45 +01002519 I915_WRITE_FW(RING_TAIL(engine->mmio_base), value);
2520 POSTING_READ_FW(RING_TAIL(engine->mmio_base));
Chris Wilson12f55812012-07-05 17:14:01 +01002521
2522 /* Let the ring send IDLE messages to the GT again,
2523 * and so let it sleep to conserve power when idle.
2524 */
Chris Wilson76f84212016-06-30 15:33:45 +01002525 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2526 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2527
2528 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002529}
2530
John Harrisona84c3ae2015-05-29 17:43:57 +01002531static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002532 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002533{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002534 struct intel_engine_cs *engine = req->engine;
Chris Wilson71a77e02011-02-02 12:13:49 +00002535 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002536 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002537
John Harrison5fb9de12015-05-29 17:44:07 +01002538 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002539 if (ret)
2540 return ret;
2541
Chris Wilson71a77e02011-02-02 12:13:49 +00002542 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002543 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002544 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002545
2546 /* We always require a command barrier so that subsequent
2547 * commands, such as breadcrumb interrupts, are strictly ordered
2548 * wrt the contents of the write cache being flushed to memory
2549 * (and thus being coherent from the CPU).
2550 */
2551 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2552
Jesse Barnes9a289772012-10-26 09:42:42 -07002553 /*
2554 * Bspec vol 1c.5 - video engine command streamer:
2555 * "If ENABLED, all TLBs will be invalidated once the flush
2556 * operation is complete. This bit is only valid when the
2557 * Post-Sync Operation field is a value of 1h or 3h."
2558 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002559 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002560 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2561
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002562 intel_ring_emit(engine, cmd);
2563 intel_ring_emit(engine,
2564 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002565 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002566 intel_ring_emit(engine, 0); /* upper addr */
2567 intel_ring_emit(engine, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002568 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002569 intel_ring_emit(engine, 0);
2570 intel_ring_emit(engine, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002571 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002572 intel_ring_advance(engine);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002573 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002574}
2575
2576static int
John Harrison53fddaf2015-05-29 17:44:02 +01002577gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002578 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002579 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002580{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002581 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002582 bool ppgtt = USES_PPGTT(engine->dev) &&
John Harrison8e004ef2015-02-13 11:48:10 +00002583 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002584 int ret;
2585
John Harrison5fb9de12015-05-29 17:44:07 +01002586 ret = intel_ring_begin(req, 4);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002587 if (ret)
2588 return ret;
2589
2590 /* FIXME(BDW): Address space and security selectors. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002591 intel_ring_emit(engine, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002592 (dispatch_flags & I915_DISPATCH_RS ?
2593 MI_BATCH_RESOURCE_STREAMER : 0));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002594 intel_ring_emit(engine, lower_32_bits(offset));
2595 intel_ring_emit(engine, upper_32_bits(offset));
2596 intel_ring_emit(engine, MI_NOOP);
2597 intel_ring_advance(engine);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002598
2599 return 0;
2600}
2601
2602static int
John Harrison53fddaf2015-05-29 17:44:02 +01002603hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002604 u64 offset, u32 len,
2605 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002606{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002607 struct intel_engine_cs *engine = req->engine;
Akshay Joshi0206e352011-08-16 15:34:10 -04002608 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002609
John Harrison5fb9de12015-05-29 17:44:07 +01002610 ret = intel_ring_begin(req, 2);
Akshay Joshi0206e352011-08-16 15:34:10 -04002611 if (ret)
2612 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002613
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002614 intel_ring_emit(engine,
Chris Wilson77072252014-09-10 12:18:27 +01002615 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002616 (dispatch_flags & I915_DISPATCH_SECURE ?
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002617 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2618 (dispatch_flags & I915_DISPATCH_RS ?
2619 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002620 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002621 intel_ring_emit(engine, offset);
2622 intel_ring_advance(engine);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002623
2624 return 0;
2625}
2626
2627static int
John Harrison53fddaf2015-05-29 17:44:02 +01002628gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002629 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002630 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002631{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002632 struct intel_engine_cs *engine = req->engine;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002633 int ret;
2634
John Harrison5fb9de12015-05-29 17:44:07 +01002635 ret = intel_ring_begin(req, 2);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002636 if (ret)
2637 return ret;
2638
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002639 intel_ring_emit(engine,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002640 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002641 (dispatch_flags & I915_DISPATCH_SECURE ?
2642 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002643 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002644 intel_ring_emit(engine, offset);
2645 intel_ring_advance(engine);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002646
Akshay Joshi0206e352011-08-16 15:34:10 -04002647 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002648}
2649
Chris Wilson549f7362010-10-19 11:19:32 +01002650/* Blitter support (SandyBridge+) */
2651
John Harrisona84c3ae2015-05-29 17:43:57 +01002652static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002653 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002654{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002655 struct intel_engine_cs *engine = req->engine;
Chris Wilson71a77e02011-02-02 12:13:49 +00002656 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002657 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002658
John Harrison5fb9de12015-05-29 17:44:07 +01002659 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002660 if (ret)
2661 return ret;
2662
Chris Wilson71a77e02011-02-02 12:13:49 +00002663 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002664 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002665 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002666
2667 /* We always require a command barrier so that subsequent
2668 * commands, such as breadcrumb interrupts, are strictly ordered
2669 * wrt the contents of the write cache being flushed to memory
2670 * (and thus being coherent from the CPU).
2671 */
2672 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2673
Jesse Barnes9a289772012-10-26 09:42:42 -07002674 /*
2675 * Bspec vol 1c.3 - blitter engine command streamer:
2676 * "If ENABLED, all TLBs will be invalidated once the flush
2677 * operation is complete. This bit is only valid when the
2678 * Post-Sync Operation field is a value of 1h or 3h."
2679 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002680 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002681 cmd |= MI_INVALIDATE_TLB;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002682 intel_ring_emit(engine, cmd);
2683 intel_ring_emit(engine,
2684 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002685 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002686 intel_ring_emit(engine, 0); /* upper addr */
2687 intel_ring_emit(engine, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002688 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002689 intel_ring_emit(engine, 0);
2690 intel_ring_emit(engine, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002691 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002692 intel_ring_advance(engine);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002693
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002694 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002695}
2696
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002697static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2698 struct intel_engine_cs *engine)
2699{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002700 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002701 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002702
2703 if (!i915_semaphore_is_enabled(dev_priv))
2704 return;
2705
2706 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore_obj) {
2707 obj = i915_gem_object_create(dev_priv->dev, 4096);
2708 if (IS_ERR(obj)) {
2709 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2710 i915.semaphores = 0;
2711 } else {
2712 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2713 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2714 if (ret != 0) {
2715 drm_gem_object_unreference(&obj->base);
2716 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2717 i915.semaphores = 0;
2718 } else {
2719 dev_priv->semaphore_obj = obj;
2720 }
2721 }
2722 }
2723
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002724 if (!i915_semaphore_is_enabled(dev_priv))
2725 return;
2726
2727 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002728 u64 offset = i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj);
2729
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002730 engine->semaphore.sync_to = gen8_ring_sync;
2731 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002732
2733 for (i = 0; i < I915_NUM_ENGINES; i++) {
2734 u64 ring_offset;
2735
2736 if (i != engine->id)
2737 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2738 else
2739 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2740
2741 engine->semaphore.signal_ggtt[i] = ring_offset;
2742 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002743 } else if (INTEL_GEN(dev_priv) >= 6) {
2744 engine->semaphore.sync_to = gen6_ring_sync;
2745 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002746
2747 /*
2748 * The current semaphore is only applied on pre-gen8
2749 * platform. And there is no VCS2 ring on the pre-gen8
2750 * platform. So the semaphore between RCS and VCS2 is
2751 * initialized as INVALID. Gen8 will initialize the
2752 * sema between VCS2 and RCS later.
2753 */
2754 for (i = 0; i < I915_NUM_ENGINES; i++) {
2755 static const struct {
2756 u32 wait_mbox;
2757 i915_reg_t mbox_reg;
2758 } sem_data[I915_NUM_ENGINES][I915_NUM_ENGINES] = {
2759 [RCS] = {
2760 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2761 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2762 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
2763 },
2764 [VCS] = {
2765 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2766 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2767 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
2768 },
2769 [BCS] = {
2770 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2771 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2772 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
2773 },
2774 [VECS] = {
2775 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2776 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2777 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
2778 },
2779 };
2780 u32 wait_mbox;
2781 i915_reg_t mbox_reg;
2782
2783 if (i == engine->id || i == VCS2) {
2784 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2785 mbox_reg = GEN6_NOSYNC;
2786 } else {
2787 wait_mbox = sem_data[engine->id][i].wait_mbox;
2788 mbox_reg = sem_data[engine->id][i].mbox_reg;
2789 }
2790
2791 engine->semaphore.mbox.wait[i] = wait_mbox;
2792 engine->semaphore.mbox.signal[i] = mbox_reg;
2793 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002794 }
2795}
2796
Chris Wilsoned003072016-07-01 09:18:13 +01002797static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2798 struct intel_engine_cs *engine)
2799{
2800 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002801 engine->irq_enable = gen8_irq_enable;
2802 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002803 engine->irq_seqno_barrier = gen6_seqno_barrier;
2804 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002805 engine->irq_enable = gen6_irq_enable;
2806 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002807 engine->irq_seqno_barrier = gen6_seqno_barrier;
2808 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002809 engine->irq_enable = gen5_irq_enable;
2810 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002811 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002812 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002813 engine->irq_enable = i9xx_irq_enable;
2814 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002815 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002816 engine->irq_enable = i8xx_irq_enable;
2817 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002818 }
2819}
2820
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002821static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2822 struct intel_engine_cs *engine)
2823{
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002824 engine->init_hw = init_ring_common;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002825 engine->write_tail = ring_write_tail;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002826
Chris Wilson6f7bef72016-07-01 09:18:12 +01002827 engine->add_request = i9xx_add_request;
2828 if (INTEL_GEN(dev_priv) >= 6)
2829 engine->add_request = gen6_add_request;
2830
2831 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002832 engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002833 else if (INTEL_GEN(dev_priv) >= 6)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002834 engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002835 else if (INTEL_GEN(dev_priv) >= 4)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002836 engine->dispatch_execbuffer = i965_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002837 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
2838 engine->dispatch_execbuffer = i830_dispatch_execbuffer;
2839 else
2840 engine->dispatch_execbuffer = i915_dispatch_execbuffer;
Tvrtko Ursulinb9700322016-06-29 16:09:23 +01002841
Chris Wilsoned003072016-07-01 09:18:13 +01002842 intel_ring_init_irq(dev_priv, engine);
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002843 intel_ring_init_semaphores(dev_priv, engine);
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002844}
2845
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002846int intel_init_render_ring_buffer(struct drm_device *dev)
2847{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002848 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002849 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002850 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002851
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002852 engine->name = "render ring";
2853 engine->id = RCS;
2854 engine->exec_id = I915_EXEC_RENDER;
Chris Wilson215a7e32016-04-29 13:18:23 +01002855 engine->hw_id = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002856 engine->mmio_base = RENDER_RING_BASE;
Daniel Vetter59465b52012-04-11 22:12:48 +02002857
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002858 intel_ring_default_vfuncs(dev_priv, engine);
2859
Chris Wilsonf8973c22016-07-01 17:23:21 +01002860 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Chris Wilson61ff75a2016-07-01 17:23:28 +01002861 if (HAS_L3_DPF(dev_priv))
2862 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002863
Chris Wilsonc0336662016-05-06 15:40:21 +01002864 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002865 engine->init_context = intel_rcs_ctx_init;
Chris Wilsona58c01a2016-04-29 13:18:21 +01002866 engine->add_request = gen8_render_add_request;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002867 engine->flush = gen8_render_ring_flush;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002868 if (i915_semaphore_is_enabled(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002869 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilsonc0336662016-05-06 15:40:21 +01002870 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002871 engine->init_context = intel_rcs_ctx_init;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002872 engine->flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002873 if (IS_GEN6(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002874 engine->flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002875 } else if (IS_GEN5(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002876 engine->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002877 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002878 if (INTEL_GEN(dev_priv) < 4)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002879 engine->flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002880 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002881 engine->flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002882 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002883 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002884
Chris Wilsonc0336662016-05-06 15:40:21 +01002885 if (IS_HASWELL(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002886 engine->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002887
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002888 engine->init_hw = init_render_ring;
2889 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002890
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002891 ret = intel_init_ring_buffer(dev, engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002892 if (ret)
2893 return ret;
2894
Chris Wilsonf8973c22016-07-01 17:23:21 +01002895 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson7d5ea802016-07-01 17:23:20 +01002896 ret = intel_init_pipe_control(engine, 4096);
2897 if (ret)
2898 return ret;
2899 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
2900 ret = intel_init_pipe_control(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002901 if (ret)
2902 return ret;
2903 }
2904
2905 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002906}
2907
2908int intel_init_bsd_ring_buffer(struct drm_device *dev)
2909{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002910 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002911 struct intel_engine_cs *engine = &dev_priv->engine[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002912
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002913 engine->name = "bsd ring";
2914 engine->id = VCS;
2915 engine->exec_id = I915_EXEC_BSD;
Chris Wilson215a7e32016-04-29 13:18:23 +01002916 engine->hw_id = 1;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002917
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002918 intel_ring_default_vfuncs(dev_priv, engine);
2919
Chris Wilsonc0336662016-05-06 15:40:21 +01002920 if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002921 engine->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002922 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002923 if (IS_GEN6(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002924 engine->write_tail = gen6_bsd_ring_write_tail;
2925 engine->flush = gen6_bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002926 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002927 engine->irq_enable_mask =
Ben Widawskyabd58f02013-11-02 21:07:09 -07002928 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002929 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002930 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002931 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002932 engine->mmio_base = BSD_RING_BASE;
2933 engine->flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002934 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002935 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002936 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002937 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002938 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002939
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002940 return intel_init_ring_buffer(dev, engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002941}
Chris Wilson549f7362010-10-19 11:19:32 +01002942
Zhao Yakui845f74a2014-04-17 10:37:37 +08002943/**
Damien Lespiau62659922015-01-29 14:13:40 +00002944 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002945 */
2946int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2947{
2948 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002949 struct intel_engine_cs *engine = &dev_priv->engine[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002950
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002951 engine->name = "bsd2 ring";
2952 engine->id = VCS2;
2953 engine->exec_id = I915_EXEC_BSD;
Chris Wilson215a7e32016-04-29 13:18:23 +01002954 engine->hw_id = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002955 engine->mmio_base = GEN8_BSD2_RING_BASE;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002956
2957 intel_ring_default_vfuncs(dev_priv, engine);
2958
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002959 engine->flush = gen6_bsd_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002960 engine->irq_enable_mask =
Zhao Yakui845f74a2014-04-17 10:37:37 +08002961 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002962
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002963 return intel_init_ring_buffer(dev, engine);
Zhao Yakui845f74a2014-04-17 10:37:37 +08002964}
2965
Chris Wilson549f7362010-10-19 11:19:32 +01002966int intel_init_blt_ring_buffer(struct drm_device *dev)
2967{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002968 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002969 struct intel_engine_cs *engine = &dev_priv->engine[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002970
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002971 engine->name = "blitter ring";
2972 engine->id = BCS;
2973 engine->exec_id = I915_EXEC_BLT;
Chris Wilson215a7e32016-04-29 13:18:23 +01002974 engine->hw_id = 2;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002975 engine->mmio_base = BLT_RING_BASE;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002976
2977 intel_ring_default_vfuncs(dev_priv, engine);
2978
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002979 engine->flush = gen6_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002980 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002981 engine->irq_enable_mask =
Ben Widawskyabd58f02013-11-02 21:07:09 -07002982 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002983 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002984 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002985
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002986 return intel_init_ring_buffer(dev, engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002987}
Chris Wilsona7b97612012-07-20 12:41:08 +01002988
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002989int intel_init_vebox_ring_buffer(struct drm_device *dev)
2990{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002991 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002992 struct intel_engine_cs *engine = &dev_priv->engine[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002993
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002994 engine->name = "video enhancement ring";
2995 engine->id = VECS;
2996 engine->exec_id = I915_EXEC_VEBOX;
Chris Wilson215a7e32016-04-29 13:18:23 +01002997 engine->hw_id = 3;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002998 engine->mmio_base = VEBOX_RING_BASE;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002999
3000 intel_ring_default_vfuncs(dev_priv, engine);
3001
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003002 engine->flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003003
Chris Wilsonc0336662016-05-06 15:40:21 +01003004 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003005 engine->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08003006 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003007 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003008 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01003009 engine->irq_enable = hsw_vebox_irq_enable;
3010 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003011 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003012
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003013 return intel_init_ring_buffer(dev, engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003014}
3015
Chris Wilsona7b97612012-07-20 12:41:08 +01003016int
John Harrison4866d722015-05-29 17:43:55 +01003017intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01003018{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003019 struct intel_engine_cs *engine = req->engine;
Chris Wilsona7b97612012-07-20 12:41:08 +01003020 int ret;
3021
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003022 if (!engine->gpu_caches_dirty)
Chris Wilsona7b97612012-07-20 12:41:08 +01003023 return 0;
3024
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003025 ret = engine->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01003026 if (ret)
3027 return ret;
3028
John Harrisona84c3ae2015-05-29 17:43:57 +01003029 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01003030
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003031 engine->gpu_caches_dirty = false;
Chris Wilsona7b97612012-07-20 12:41:08 +01003032 return 0;
3033}
3034
3035int
John Harrison2f200552015-05-29 17:43:53 +01003036intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01003037{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003038 struct intel_engine_cs *engine = req->engine;
Chris Wilsona7b97612012-07-20 12:41:08 +01003039 uint32_t flush_domains;
3040 int ret;
3041
3042 flush_domains = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003043 if (engine->gpu_caches_dirty)
Chris Wilsona7b97612012-07-20 12:41:08 +01003044 flush_domains = I915_GEM_GPU_DOMAINS;
3045
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003046 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003047 if (ret)
3048 return ret;
3049
John Harrisona84c3ae2015-05-29 17:43:57 +01003050 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003051
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003052 engine->gpu_caches_dirty = false;
Chris Wilsona7b97612012-07-20 12:41:08 +01003053 return 0;
3054}
Chris Wilsone3efda42014-04-09 09:19:41 +01003055
3056void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00003057intel_stop_engine(struct intel_engine_cs *engine)
Chris Wilsone3efda42014-04-09 09:19:41 +01003058{
3059 int ret;
3060
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00003061 if (!intel_engine_initialized(engine))
Chris Wilsone3efda42014-04-09 09:19:41 +01003062 return;
3063
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003064 ret = intel_engine_idle(engine);
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003065 if (ret)
Chris Wilsone3efda42014-04-09 09:19:41 +01003066 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003067 engine->name, ret);
Chris Wilsone3efda42014-04-09 09:19:41 +01003068
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003069 stop_ring(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01003070}