blob: 05e01446b00b321bc92411751a74ba02d32c1de5 [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
Chris Wilson605d5b32017-05-04 14:08:44 +010042static unsigned int __intel_ring_space(unsigned int head,
43 unsigned int tail,
44 unsigned int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010045{
Chris Wilson605d5b32017-05-04 14:08:44 +010046 /*
47 * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
48 * same cacheline, the Head Pointer must not be greater than the Tail
49 * Pointer."
50 */
51 GEM_BUG_ON(!is_power_of_2(size));
52 return (head - tail - CACHELINE_BYTES) & (size - 1);
Chris Wilson1cf0ba12014-05-05 09:07:33 +010053}
54
Chris Wilson95aebcb2017-05-04 14:08:45 +010055unsigned int intel_ring_update_space(struct intel_ring *ring)
Dave Gordonebd0fd42014-11-27 11:22:49 +000056{
Chris Wilson95aebcb2017-05-04 14:08:45 +010057 unsigned int space;
58
59 space = __intel_ring_space(ring->head, ring->emit, ring->size);
60
61 ring->space = space;
62 return space;
Dave Gordonebd0fd42014-11-27 11:22:49 +000063}
64
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000065static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010066gen2_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010067{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000068 u32 cmd, *cs;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010069
70 cmd = MI_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010071
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010072 if (mode & EMIT_INVALIDATE)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010073 cmd |= MI_READ_FLUSH;
74
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000075 cs = intel_ring_begin(req, 2);
76 if (IS_ERR(cs))
77 return PTR_ERR(cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010078
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000079 *cs++ = cmd;
80 *cs++ = MI_NOOP;
81 intel_ring_advance(req, cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010082
83 return 0;
84}
85
86static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010087gen4_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Eric Anholt62fdfea2010-05-21 13:26:39 -070088{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000089 u32 cmd, *cs;
Chris Wilson6f392d52010-08-07 11:01:22 +010090
Chris Wilson36d527d2011-03-19 22:26:49 +000091 /*
92 * read/write caches:
93 *
94 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
95 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
96 * also flushed at 2d versus 3d pipeline switches.
97 *
98 * read-only caches:
99 *
100 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
101 * MI_READ_FLUSH is set, and is always flushed on 965.
102 *
103 * I915_GEM_DOMAIN_COMMAND may not exist?
104 *
105 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
106 * invalidated when MI_EXE_FLUSH is set.
107 *
108 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
109 * invalidated with every MI_FLUSH.
110 *
111 * TLBs:
112 *
113 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
114 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
115 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
116 * are flushed at any MI_FLUSH.
117 */
118
Chris Wilsonb5321f32016-08-02 22:50:18 +0100119 cmd = MI_FLUSH;
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100120 if (mode & EMIT_INVALIDATE) {
Chris Wilson36d527d2011-03-19 22:26:49 +0000121 cmd |= MI_EXE_FLUSH;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100122 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
123 cmd |= MI_INVALIDATE_ISP;
124 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000125
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000126 cs = intel_ring_begin(req, 2);
127 if (IS_ERR(cs))
128 return PTR_ERR(cs);
Chris Wilson36d527d2011-03-19 22:26:49 +0000129
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000130 *cs++ = cmd;
131 *cs++ = MI_NOOP;
132 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000133
134 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800135}
136
Jesse Barnes8d315282011-10-16 10:23:31 +0200137/**
138 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
139 * implementing two workarounds on gen6. From section 1.4.7.1
140 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
141 *
142 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
143 * produced by non-pipelined state commands), software needs to first
144 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
145 * 0.
146 *
147 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
148 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
149 *
150 * And the workaround for these two requires this workaround first:
151 *
152 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
153 * BEFORE the pipe-control with a post-sync op and no write-cache
154 * flushes.
155 *
156 * And this last workaround is tricky because of the requirements on
157 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
158 * volume 2 part 1:
159 *
160 * "1 of the following must also be set:
161 * - Render Target Cache Flush Enable ([12] of DW1)
162 * - Depth Cache Flush Enable ([0] of DW1)
163 * - Stall at Pixel Scoreboard ([1] of DW1)
164 * - Depth Stall ([13] of DW1)
165 * - Post-Sync Operation ([13] of DW1)
166 * - Notify Enable ([8] of DW1)"
167 *
168 * The cache flushes require the workaround flush that triggered this
169 * one, so we can't use it. Depth stall would trigger the same.
170 * Post-sync nonzero is what triggered this second workaround, so we
171 * can't use that one either. Notify enable is IRQs, which aren't
172 * really our business. That leaves only stall at scoreboard.
173 */
174static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100175intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200176{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100177 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100178 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000179 u32 *cs;
Jesse Barnes8d315282011-10-16 10:23:31 +0200180
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000181 cs = intel_ring_begin(req, 6);
182 if (IS_ERR(cs))
183 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200184
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000185 *cs++ = GFX_OP_PIPE_CONTROL(5);
186 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
187 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
188 *cs++ = 0; /* low dword */
189 *cs++ = 0; /* high dword */
190 *cs++ = MI_NOOP;
191 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200192
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000193 cs = intel_ring_begin(req, 6);
194 if (IS_ERR(cs))
195 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200196
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000197 *cs++ = GFX_OP_PIPE_CONTROL(5);
198 *cs++ = PIPE_CONTROL_QW_WRITE;
199 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
200 *cs++ = 0;
201 *cs++ = 0;
202 *cs++ = MI_NOOP;
203 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200204
205 return 0;
206}
207
208static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100209gen6_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Jesse Barnes8d315282011-10-16 10:23:31 +0200210{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100211 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100212 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000213 u32 *cs, flags = 0;
Jesse Barnes8d315282011-10-16 10:23:31 +0200214 int ret;
215
Paulo Zanonib3111502012-08-17 18:35:42 -0300216 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100217 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300218 if (ret)
219 return ret;
220
Jesse Barnes8d315282011-10-16 10:23:31 +0200221 /* Just flush everything. Experiments have shown that reducing the
222 * number of bits based on the write domains has little performance
223 * impact.
224 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100225 if (mode & EMIT_FLUSH) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100226 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
227 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
228 /*
229 * Ensure that any following seqno writes only happen
230 * when the render cache is indeed flushed.
231 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200232 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100233 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100234 if (mode & EMIT_INVALIDATE) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100235 flags |= PIPE_CONTROL_TLB_INVALIDATE;
236 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
237 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
238 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
239 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
240 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
241 /*
242 * TLB invalidate requires a post-sync write.
243 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700244 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100245 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200246
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000247 cs = intel_ring_begin(req, 4);
248 if (IS_ERR(cs))
249 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200250
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000251 *cs++ = GFX_OP_PIPE_CONTROL(4);
252 *cs++ = flags;
253 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
254 *cs++ = 0;
255 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200256
257 return 0;
258}
259
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100260static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100261gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300262{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000263 u32 *cs;
Paulo Zanonif3987632012-08-17 18:35:43 -0300264
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000265 cs = intel_ring_begin(req, 4);
266 if (IS_ERR(cs))
267 return PTR_ERR(cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300268
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000269 *cs++ = GFX_OP_PIPE_CONTROL(4);
270 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
271 *cs++ = 0;
272 *cs++ = 0;
273 intel_ring_advance(req, cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300274
275 return 0;
276}
277
278static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100279gen7_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300280{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100281 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100282 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000283 u32 *cs, flags = 0;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300284
Paulo Zanonif3987632012-08-17 18:35:43 -0300285 /*
286 * Ensure that any following seqno writes only happen when the render
287 * cache is indeed flushed.
288 *
289 * Workaround: 4th PIPE_CONTROL command (except the ones with only
290 * read-cache invalidate bits set) must have the CS_STALL bit set. We
291 * don't try to be clever and just set it unconditionally.
292 */
293 flags |= PIPE_CONTROL_CS_STALL;
294
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300295 /* Just flush everything. Experiments have shown that reducing the
296 * number of bits based on the write domains has little performance
297 * impact.
298 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100299 if (mode & EMIT_FLUSH) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300300 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
301 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800302 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100303 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300304 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100305 if (mode & EMIT_INVALIDATE) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300306 flags |= PIPE_CONTROL_TLB_INVALIDATE;
307 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
308 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
309 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
310 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
311 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000312 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300313 /*
314 * TLB invalidate requires a post-sync write.
315 */
316 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200317 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300318
Chris Wilsonadd284a2014-12-16 08:44:32 +0000319 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
320
Paulo Zanonif3987632012-08-17 18:35:43 -0300321 /* Workaround: we must issue a pipe_control with CS-stall bit
322 * set before a pipe_control command that has the state cache
323 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100324 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300325 }
326
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000327 cs = intel_ring_begin(req, 4);
328 if (IS_ERR(cs))
329 return PTR_ERR(cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300330
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000331 *cs++ = GFX_OP_PIPE_CONTROL(4);
332 *cs++ = flags;
333 *cs++ = scratch_addr;
334 *cs++ = 0;
335 intel_ring_advance(req, cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300336
337 return 0;
338}
339
Ben Widawskya5f3d682013-11-02 21:07:27 -0700340static int
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000341gen8_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300342{
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000343 u32 flags;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000344 u32 *cs;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300345
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000346 cs = intel_ring_begin(req, mode & EMIT_INVALIDATE ? 12 : 6);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000347 if (IS_ERR(cs))
348 return PTR_ERR(cs);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300349
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000350 flags = PIPE_CONTROL_CS_STALL;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700351
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100352 if (mode & EMIT_FLUSH) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700353 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
354 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800355 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100356 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700357 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100358 if (mode & EMIT_INVALIDATE) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700359 flags |= PIPE_CONTROL_TLB_INVALIDATE;
360 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
361 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
362 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
363 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
364 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
365 flags |= PIPE_CONTROL_QW_WRITE;
366 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800367
368 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000369 cs = gen8_emit_pipe_control(cs,
370 PIPE_CONTROL_CS_STALL |
371 PIPE_CONTROL_STALL_AT_SCOREBOARD,
372 0);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700373 }
374
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000375 cs = gen8_emit_pipe_control(cs, flags,
376 i915_ggtt_offset(req->engine->scratch) +
377 2 * CACHELINE_BYTES);
378
379 intel_ring_advance(req, cs);
380
381 return 0;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700382}
383
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000384static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200385{
Chris Wilsonc0336662016-05-06 15:40:21 +0100386 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200387 u32 addr;
388
389 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100390 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200391 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
392 I915_WRITE(HWS_PGA, addr);
393}
394
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000395static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000396{
Chris Wilsonc0336662016-05-06 15:40:21 +0100397 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200398 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000399
400 /* The ring status page addresses are no longer next to the rest of
401 * the ring registers as of gen7.
402 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100403 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000404 switch (engine->id) {
Michel Thierrya2d3d262017-08-30 11:01:15 -0700405 /*
406 * No more rings exist on Gen7. Default case is only to shut up
407 * gcc switch check warning.
408 */
409 default:
410 GEM_BUG_ON(engine->id);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000411 case RCS:
412 mmio = RENDER_HWS_PGA_GEN7;
413 break;
414 case BCS:
415 mmio = BLT_HWS_PGA_GEN7;
416 break;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000417 case VCS:
418 mmio = BSD_HWS_PGA_GEN7;
419 break;
420 case VECS:
421 mmio = VEBOX_HWS_PGA_GEN7;
422 break;
423 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100424 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000425 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000426 } else {
427 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000428 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000429 }
430
Ville Syrjäläc54980892017-08-18 21:37:01 +0300431 if (INTEL_GEN(dev_priv) >= 6)
432 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
433
Chris Wilson57e88532016-08-15 10:48:57 +0100434 I915_WRITE(mmio, engine->status_page.ggtt_offset);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000435 POSTING_READ(mmio);
436
437 /*
438 * Flush the TLB for this page
439 *
440 * FIXME: These two bits have disappeared on gen8, so a question
441 * arises: do we still need this and if so how should we go about
442 * invalidating the TLB?
443 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100444 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000445 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000446
447 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000448 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000449
450 I915_WRITE(reg,
451 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
452 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100453 if (intel_wait_for_register(dev_priv,
454 reg, INSTPM_SYNC_FLUSH, 0,
455 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000456 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000457 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000458 }
459}
460
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000461static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100462{
Chris Wilsonc0336662016-05-06 15:40:21 +0100463 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100464
Chris Wilson21a2c582016-08-15 10:49:11 +0100465 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000466 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100467 if (intel_wait_for_register(dev_priv,
468 RING_MI_MODE(engine->mmio_base),
469 MODE_IDLE,
470 MODE_IDLE,
471 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000472 DRM_ERROR("%s : timed out trying to stop ring\n",
473 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100474 /* Sometimes we observe that the idle flag is not
475 * set even though the ring is empty. So double
476 * check before giving up.
477 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000478 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100479 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100480 }
481 }
482
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000483 I915_WRITE_CTL(engine, 0);
484 I915_WRITE_HEAD(engine, 0);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100485 I915_WRITE_TAIL(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100486
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000487 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100488}
489
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000490static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800491{
Chris Wilsonc0336662016-05-06 15:40:21 +0100492 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +0100493 struct intel_ring *ring = engine->buffer;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200494 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800495
Mika Kuoppala59bad942015-01-16 11:34:40 +0200496 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200497
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000498 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100499 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000500 DRM_DEBUG_KMS("%s head not reset to zero "
501 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000502 engine->name,
503 I915_READ_CTL(engine),
504 I915_READ_HEAD(engine),
505 I915_READ_TAIL(engine),
506 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800507
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000508 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000509 DRM_ERROR("failed to set %s head to zero "
510 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000511 engine->name,
512 I915_READ_CTL(engine),
513 I915_READ_HEAD(engine),
514 I915_READ_TAIL(engine),
515 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100516 ret = -EIO;
517 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000518 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700519 }
520
Carlos Santa31776592016-08-17 12:30:56 -0700521 if (HWS_NEEDS_PHYSICAL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000522 ring_setup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -0700523 else
524 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100525
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100526 intel_engine_reset_breadcrumbs(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +0100527
Jiri Kosinaece4a172014-08-07 16:29:53 +0200528 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000529 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200530
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200531 /* Initialize the ring. This must happen _after_ we've cleared the ring
532 * registers with the above sequence (the readback of the HEAD registers
533 * also enforces ordering), otherwise the hw might lose the new ring
534 * register values. */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100535 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
Chris Wilson95468892014-08-07 15:39:54 +0100536
537 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000538 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100539 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000540 engine->name, I915_READ_HEAD(engine));
Chris Wilson821ed7d2016-09-09 14:11:53 +0100541
542 intel_ring_update_space(ring);
543 I915_WRITE_HEAD(engine, ring->head);
544 I915_WRITE_TAIL(engine, ring->tail);
545 (void)I915_READ_TAIL(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100546
Chris Wilson62ae14b2016-10-04 21:11:25 +0100547 I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800548
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800549 /* If the head is still not zero, the ring is dead */
Chris Wilsonf42bb652017-04-11 11:13:40 +0100550 if (intel_wait_for_register(dev_priv, RING_CTL(engine->mmio_base),
551 RING_VALID, RING_VALID,
552 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000553 DRM_ERROR("%s initialization failed "
Chris Wilson821ed7d2016-09-09 14:11:53 +0100554 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000555 engine->name,
556 I915_READ_CTL(engine),
557 I915_READ_CTL(engine) & RING_VALID,
Chris Wilson821ed7d2016-09-09 14:11:53 +0100558 I915_READ_HEAD(engine), ring->head,
559 I915_READ_TAIL(engine), ring->tail,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000560 I915_READ_START(engine),
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100561 i915_ggtt_offset(ring->vma));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200562 ret = -EIO;
563 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800564 }
565
Tomas Elffc0768c2016-03-21 16:26:59 +0000566 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100567
Chris Wilson7836cd02017-10-13 14:12:17 +0100568 if (INTEL_GEN(dev_priv) > 2)
569 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
570
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200571out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200572 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200573
574 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700575}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800576
Chris Wilson821ed7d2016-09-09 14:11:53 +0100577static void reset_ring_common(struct intel_engine_cs *engine,
578 struct drm_i915_gem_request *request)
579{
Chris Wilson67e64562017-10-09 12:03:01 +0100580 /*
581 * RC6 must be prevented until the reset is complete and the engine
582 * reinitialised. If it occurs in the middle of this sequence, the
583 * state written to/loaded from the power context is ill-defined (e.g.
584 * the PP_BASE_DIR may be lost).
585 */
586 assert_forcewakes_active(engine->i915, FORCEWAKE_ALL);
587
588 /*
589 * Try to restore the logical GPU state to match the continuation
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000590 * of the request queue. If we skip the context/PD restore, then
591 * the next request may try to execute assuming that its context
592 * is valid and loaded on the GPU and so may try to access invalid
593 * memory, prompting repeated GPU hangs.
594 *
595 * If the request was guilty, we still restore the logical state
596 * in case the next request requires it (e.g. the aliasing ppgtt),
597 * but skip over the hung batch.
598 *
599 * If the request was innocent, we try to replay the request with
600 * the restored context.
601 */
602 if (request) {
603 struct drm_i915_private *dev_priv = request->i915;
604 struct intel_context *ce = &request->ctx->engine[engine->id];
605 struct i915_hw_ppgtt *ppgtt;
Chris Wilson821ed7d2016-09-09 14:11:53 +0100606
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000607 /* FIXME consider gen8 reset */
608
609 if (ce->state) {
610 I915_WRITE(CCID,
611 i915_ggtt_offset(ce->state) |
612 BIT(8) /* must be set! */ |
613 CCID_EXTENDED_STATE_SAVE |
614 CCID_EXTENDED_STATE_RESTORE |
615 CCID_EN);
616 }
617
618 ppgtt = request->ctx->ppgtt ?: engine->i915->mm.aliasing_ppgtt;
619 if (ppgtt) {
620 u32 pd_offset = ppgtt->pd.base.ggtt_offset << 10;
621
622 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
623 I915_WRITE(RING_PP_DIR_BASE(engine), pd_offset);
624
625 /* Wait for the PD reload to complete */
626 if (intel_wait_for_register(dev_priv,
627 RING_PP_DIR_BASE(engine),
628 BIT(0), 0,
629 10))
630 DRM_ERROR("Wait for reload of ppgtt page-directory timed out\n");
631
632 ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
633 }
634
635 /* If the rq hung, jump to its breadcrumb and skip the batch */
Chris Wilsonfe085f12017-03-21 10:25:52 +0000636 if (request->fence.error == -EIO)
637 request->ring->head = request->postfix;
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000638 } else {
639 engine->legacy_active_context = NULL;
640 }
Chris Wilson821ed7d2016-09-09 14:11:53 +0100641}
642
John Harrison87531812015-05-29 17:43:44 +0100643static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100644{
645 int ret;
646
John Harrisone2be4fa2015-05-29 17:43:54 +0100647 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100648 if (ret != 0)
649 return ret;
650
Chris Wilson4e50f082016-10-28 13:58:31 +0100651 ret = i915_gem_render_state_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100652 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000653 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100654
Chris Wilsone26e1b92016-01-29 16:49:05 +0000655 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100656}
657
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000658static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800659{
Chris Wilsonc0336662016-05-06 15:40:21 +0100660 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000661 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +0200662 if (ret)
663 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800664
Akash Goel61a563a2014-03-25 18:01:50 +0530665 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100666 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200667 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000668
669 /* We need to disable the AsyncFlip performance optimisations in order
670 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
671 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +0100672 *
Ville Syrjälä2441f872015-06-02 15:37:37 +0300673 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000674 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100675 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000676 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
677
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000678 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +0530679 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +0100680 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000681 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +0000682 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000683
Akash Goel01fa0302014-03-24 23:00:04 +0530684 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +0100685 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000686 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +0530687 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000688 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +0100689
Chris Wilsonc0336662016-05-06 15:40:21 +0100690 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700691 /* From the Sandybridge PRM, volume 1 part 3, page 24:
692 * "If this bit is set, STCunit will have LRA as replacement
693 * policy. [...] This bit must be reset. LRA replacement
694 * policy is not supported."
695 */
696 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200697 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -0800698 }
699
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100700 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200701 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +0000702
Ville Syrjälä035ea402016-07-12 19:24:47 +0300703 if (INTEL_INFO(dev_priv)->gen >= 6)
704 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -0700705
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000706 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800707}
708
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000709static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000710{
Chris Wilsonc0336662016-05-06 15:40:21 +0100711 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700712
Chris Wilson19880c42016-08-15 10:49:05 +0100713 i915_vma_unpin_and_release(&dev_priv->semaphore);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000714}
715
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000716static u32 *gen8_rcs_signal(struct drm_i915_gem_request *req, u32 *cs)
Ben Widawsky3e789982014-06-30 09:53:37 -0700717{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100718 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700719 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +0000720 enum intel_engine_id id;
Ben Widawsky3e789982014-06-30 09:53:37 -0700721
Akash Goel3b3f1652016-10-13 22:44:48 +0530722 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100723 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -0700724 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
725 continue;
726
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000727 *cs++ = GFX_OP_PIPE_CONTROL(6);
728 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_QW_WRITE |
729 PIPE_CONTROL_CS_STALL;
730 *cs++ = lower_32_bits(gtt_offset);
731 *cs++ = upper_32_bits(gtt_offset);
732 *cs++ = req->global_seqno;
733 *cs++ = 0;
734 *cs++ = MI_SEMAPHORE_SIGNAL |
735 MI_SEMAPHORE_TARGET(waiter->hw_id);
736 *cs++ = 0;
Ben Widawsky3e789982014-06-30 09:53:37 -0700737 }
738
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000739 return cs;
Ben Widawsky3e789982014-06-30 09:53:37 -0700740}
741
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000742static u32 *gen8_xcs_signal(struct drm_i915_gem_request *req, u32 *cs)
Ben Widawsky3e789982014-06-30 09:53:37 -0700743{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100744 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700745 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +0000746 enum intel_engine_id id;
Ben Widawsky3e789982014-06-30 09:53:37 -0700747
Akash Goel3b3f1652016-10-13 22:44:48 +0530748 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100749 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -0700750 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
751 continue;
752
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000753 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
754 *cs++ = lower_32_bits(gtt_offset) | MI_FLUSH_DW_USE_GTT;
755 *cs++ = upper_32_bits(gtt_offset);
756 *cs++ = req->global_seqno;
757 *cs++ = MI_SEMAPHORE_SIGNAL |
758 MI_SEMAPHORE_TARGET(waiter->hw_id);
759 *cs++ = 0;
Ben Widawsky3e789982014-06-30 09:53:37 -0700760 }
761
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000762 return cs;
Ben Widawsky3e789982014-06-30 09:53:37 -0700763}
764
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000765static u32 *gen6_signal(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000766{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100767 struct drm_i915_private *dev_priv = req->i915;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100768 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530769 enum intel_engine_id id;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100770 int num_rings = 0;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700771
Akash Goel3b3f1652016-10-13 22:44:48 +0530772 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100773 i915_reg_t mbox_reg;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200774
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100775 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
776 continue;
777
778 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200779 if (i915_mmio_reg_valid(mbox_reg)) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000780 *cs++ = MI_LOAD_REGISTER_IMM(1);
781 *cs++ = i915_mmio_reg_offset(mbox_reg);
782 *cs++ = req->global_seqno;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100783 num_rings++;
Ben Widawsky78325f22014-04-29 14:52:29 -0700784 }
785 }
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100786 if (num_rings & 1)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000787 *cs++ = MI_NOOP;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700788
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000789 return cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000790}
791
Chris Wilson27a5f612017-09-15 18:31:00 +0100792static void cancel_requests(struct intel_engine_cs *engine)
793{
794 struct drm_i915_gem_request *request;
795 unsigned long flags;
796
797 spin_lock_irqsave(&engine->timeline->lock, flags);
798
799 /* Mark all submitted requests as skipped. */
800 list_for_each_entry(request, &engine->timeline->requests, link) {
801 GEM_BUG_ON(!request->global_seqno);
802 if (!i915_gem_request_completed(request))
803 dma_fence_set_error(&request->fence, -EIO);
804 }
805 /* Remaining _unready_ requests will be nop'ed when submitted */
806
807 spin_unlock_irqrestore(&engine->timeline->lock, flags);
808}
809
Chris Wilsonb0411e72016-08-02 22:50:34 +0100810static void i9xx_submit_request(struct drm_i915_gem_request *request)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000811{
Chris Wilsonb0411e72016-08-02 22:50:34 +0100812 struct drm_i915_private *dev_priv = request->i915;
813
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000814 i915_gem_request_submit(request);
815
Chris Wilsone6ba9992017-04-25 14:00:49 +0100816 I915_WRITE_TAIL(request->engine,
817 intel_ring_set_tail(request->ring, request->tail));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100818}
819
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000820static void i9xx_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100821{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000822 *cs++ = MI_STORE_DWORD_INDEX;
823 *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
824 *cs++ = req->global_seqno;
825 *cs++ = MI_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000826
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000827 req->tail = intel_ring_offset(req, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +0100828 assert_ring_tail_valid(req->ring, req->tail);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000829}
830
Chris Wilson98f29e82016-10-28 13:58:51 +0100831static const int i9xx_emit_breadcrumb_sz = 4;
832
Chris Wilsonb0411e72016-08-02 22:50:34 +0100833/**
Chris Wilson9b81d552016-10-28 13:58:50 +0100834 * gen6_sema_emit_breadcrumb - Update the semaphore mailbox registers
Chris Wilsonb0411e72016-08-02 22:50:34 +0100835 *
836 * @request - request to write to the ring
837 *
838 * Update the mailbox registers in the *other* rings with the current seqno.
839 * This acts like a signal in the canonical semaphore.
840 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000841static void gen6_sema_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100842{
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100843 return i9xx_emit_breadcrumb(req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000844 req->engine->semaphore.signal(req, cs));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100845}
846
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100847static void gen8_render_emit_breadcrumb(struct drm_i915_gem_request *req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000848 u32 *cs)
Chris Wilsona58c01a2016-04-29 13:18:21 +0100849{
850 struct intel_engine_cs *engine = req->engine;
Chris Wilsona58c01a2016-04-29 13:18:21 +0100851
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100852 if (engine->semaphore.signal)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000853 cs = engine->semaphore.signal(req, cs);
Chris Wilson9242f972016-08-02 22:50:33 +0100854
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000855 *cs++ = GFX_OP_PIPE_CONTROL(6);
856 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
857 PIPE_CONTROL_QW_WRITE;
858 *cs++ = intel_hws_seqno_address(engine);
859 *cs++ = 0;
860 *cs++ = req->global_seqno;
Chris Wilsona58c01a2016-04-29 13:18:21 +0100861 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000862 *cs++ = 0;
863 *cs++ = MI_USER_INTERRUPT;
864 *cs++ = MI_NOOP;
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100865
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000866 req->tail = intel_ring_offset(req, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +0100867 assert_ring_tail_valid(req->ring, req->tail);
Chris Wilsona58c01a2016-04-29 13:18:21 +0100868}
869
Chris Wilson98f29e82016-10-28 13:58:51 +0100870static const int gen8_render_emit_breadcrumb_sz = 8;
871
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700872/**
873 * intel_ring_sync - sync the waiter to the signaller on seqno
874 *
875 * @waiter - ring that is waiting
876 * @signaller - ring which has, or will signal
877 * @seqno - seqno which the waiter will block on
878 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700879
880static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100881gen8_ring_sync_to(struct drm_i915_gem_request *req,
882 struct drm_i915_gem_request *signal)
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700883{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100884 struct drm_i915_private *dev_priv = req->i915;
885 u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +0100886 struct i915_hw_ppgtt *ppgtt;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000887 u32 *cs;
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700888
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000889 cs = intel_ring_begin(req, 4);
890 if (IS_ERR(cs))
891 return PTR_ERR(cs);
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700892
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000893 *cs++ = MI_SEMAPHORE_WAIT | MI_SEMAPHORE_GLOBAL_GTT |
894 MI_SEMAPHORE_SAD_GTE_SDD;
895 *cs++ = signal->global_seqno;
896 *cs++ = lower_32_bits(offset);
897 *cs++ = upper_32_bits(offset);
898 intel_ring_advance(req, cs);
Chris Wilson6ef48d72016-04-29 13:18:25 +0100899
900 /* When the !RCS engines idle waiting upon a semaphore, they lose their
901 * pagetables and we must reload them before executing the batch.
902 * We do this on the i915_switch_context() following the wait and
903 * before the dispatch.
904 */
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100905 ppgtt = req->ctx->ppgtt;
906 if (ppgtt && req->engine->id != RCS)
907 ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700908 return 0;
909}
910
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700911static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100912gen6_ring_sync_to(struct drm_i915_gem_request *req,
913 struct drm_i915_gem_request *signal)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000914{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700915 u32 dw1 = MI_SEMAPHORE_MBOX |
916 MI_SEMAPHORE_COMPARE |
917 MI_SEMAPHORE_REGISTER;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100918 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000919 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000920
Chris Wilsonddf07be2016-08-02 22:50:39 +0100921 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
922
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000923 cs = intel_ring_begin(req, 4);
924 if (IS_ERR(cs))
925 return PTR_ERR(cs);
Chris Wilsonddf07be2016-08-02 22:50:39 +0100926
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000927 *cs++ = dw1 | wait_mbox;
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700928 /* Throughout all of the GEM code, seqno passed implies our current
929 * seqno is >= the last seqno executed. However for hardware the
930 * comparison is strictly greater than.
931 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000932 *cs++ = signal->global_seqno - 1;
933 *cs++ = 0;
934 *cs++ = MI_NOOP;
935 intel_ring_advance(req, cs);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000936
937 return 0;
938}
939
Chris Wilsonf8973c22016-07-01 17:23:21 +0100940static void
Dave Gordon38a0f2d2016-07-20 18:16:06 +0100941gen5_seqno_barrier(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000942{
Chris Wilsonf8973c22016-07-01 17:23:21 +0100943 /* MI_STORE are internally buffered by the GPU and not flushed
944 * either by MI_FLUSH or SyncFlush or any other combination of
945 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000946 *
Chris Wilsonf8973c22016-07-01 17:23:21 +0100947 * "Only the submission of the store operation is guaranteed.
948 * The write result will be complete (coherent) some time later
949 * (this is practically a finite period but there is no guaranteed
950 * latency)."
951 *
952 * Empirically, we observe that we need a delay of at least 75us to
953 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000954 */
Chris Wilsonf8973c22016-07-01 17:23:21 +0100955 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000956}
957
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100958static void
959gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100960{
Chris Wilsonc0336662016-05-06 15:40:21 +0100961 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100962
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100963 /* Workaround to force correct ordering between irq and seqno writes on
964 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +0100965 * ACTHD) before reading the status page.
966 *
967 * Note that this effectively stalls the read by the time it takes to
968 * do a memory transaction, which more or less ensures that the write
969 * from the GPU has sufficient time to invalidate the CPU cacheline.
970 * Alternatively we could delay the interrupt from the CS ring to give
971 * the write time to land, but that would incur a delay after every
972 * batch i.e. much more frequent than a delay when waiting for the
973 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100974 *
975 * Also note that to prevent whole machine hangs on gen7, we have to
976 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +0100977 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100978 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100979 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100980 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100981}
982
Chris Wilson31bb59c2016-07-01 17:23:27 +0100983static void
984gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200985{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100986 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +0200987}
988
989static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100990gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200991{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100992 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700993}
994
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800995static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100996i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700997{
Chris Wilsonc0336662016-05-06 15:40:21 +0100998 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700999
Chris Wilson31bb59c2016-07-01 17:23:27 +01001000 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1001 I915_WRITE(IMR, dev_priv->irq_mask);
1002 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +01001003}
1004
1005static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001006i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001007{
Chris Wilsonc0336662016-05-06 15:40:21 +01001008 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001009
Chris Wilson31bb59c2016-07-01 17:23:27 +01001010 dev_priv->irq_mask |= engine->irq_enable_mask;
1011 I915_WRITE(IMR, dev_priv->irq_mask);
1012}
1013
1014static void
1015i8xx_irq_enable(struct intel_engine_cs *engine)
1016{
1017 struct drm_i915_private *dev_priv = engine->i915;
1018
1019 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1020 I915_WRITE16(IMR, dev_priv->irq_mask);
1021 POSTING_READ16(RING_IMR(engine->mmio_base));
1022}
1023
1024static void
1025i8xx_irq_disable(struct intel_engine_cs *engine)
1026{
1027 struct drm_i915_private *dev_priv = engine->i915;
1028
1029 dev_priv->irq_mask |= engine->irq_enable_mask;
1030 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001031}
1032
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001033static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001034bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001035{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001036 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001037
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001038 cs = intel_ring_begin(req, 2);
1039 if (IS_ERR(cs))
1040 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001041
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001042 *cs++ = MI_FLUSH;
1043 *cs++ = MI_NOOP;
1044 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001045 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001046}
1047
Chris Wilson0f468322011-01-04 17:35:21 +00001048static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001049gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001050{
Chris Wilsonc0336662016-05-06 15:40:21 +01001051 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001052
Chris Wilson61ff75a2016-07-01 17:23:28 +01001053 I915_WRITE_IMR(engine,
1054 ~(engine->irq_enable_mask |
1055 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001056 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001057}
1058
1059static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001060gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001061{
Chris Wilsonc0336662016-05-06 15:40:21 +01001062 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001063
Chris Wilson61ff75a2016-07-01 17:23:28 +01001064 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001065 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001066}
1067
1068static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001069hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001070{
Chris Wilsonc0336662016-05-06 15:40:21 +01001071 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001072
Chris Wilson31bb59c2016-07-01 17:23:27 +01001073 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
Akash Goelf4e9af42016-10-12 21:54:30 +05301074 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001075}
1076
1077static void
1078hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1079{
1080 struct drm_i915_private *dev_priv = engine->i915;
1081
1082 I915_WRITE_IMR(engine, ~0);
Akash Goelf4e9af42016-10-12 21:54:30 +05301083 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001084}
1085
1086static void
1087gen8_irq_enable(struct intel_engine_cs *engine)
1088{
1089 struct drm_i915_private *dev_priv = engine->i915;
1090
Chris Wilson61ff75a2016-07-01 17:23:28 +01001091 I915_WRITE_IMR(engine,
1092 ~(engine->irq_enable_mask |
1093 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001094 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1095}
1096
1097static void
1098gen8_irq_disable(struct intel_engine_cs *engine)
1099{
1100 struct drm_i915_private *dev_priv = engine->i915;
1101
Chris Wilson61ff75a2016-07-01 17:23:28 +01001102 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001103}
1104
Zou Nan haid1b851f2010-05-21 09:08:57 +08001105static int
Chris Wilson803688b2016-08-02 22:50:27 +01001106i965_emit_bb_start(struct drm_i915_gem_request *req,
1107 u64 offset, u32 length,
1108 unsigned int dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001109{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001110 u32 *cs;
Chris Wilson78501ea2010-10-27 12:18:21 +01001111
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001112 cs = intel_ring_begin(req, 2);
1113 if (IS_ERR(cs))
1114 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001115
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001116 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
1117 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
1118 *cs++ = offset;
1119 intel_ring_advance(req, cs);
Chris Wilson78501ea2010-10-27 12:18:21 +01001120
Zou Nan haid1b851f2010-05-21 09:08:57 +08001121 return 0;
1122}
1123
Daniel Vetterb45305f2012-12-17 16:21:27 +01001124/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1125#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001126#define I830_TLB_ENTRIES (2)
1127#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001128static int
Chris Wilson803688b2016-08-02 22:50:27 +01001129i830_emit_bb_start(struct drm_i915_gem_request *req,
1130 u64 offset, u32 len,
1131 unsigned int dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001132{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001133 u32 *cs, cs_offset = i915_ggtt_offset(req->engine->scratch);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001134
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001135 cs = intel_ring_begin(req, 6);
1136 if (IS_ERR(cs))
1137 return PTR_ERR(cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001138
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001139 /* Evict the invalid PTE TLBs */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001140 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
1141 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
1142 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
1143 *cs++ = cs_offset;
1144 *cs++ = 0xdeadbeef;
1145 *cs++ = MI_NOOP;
1146 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001147
John Harrison8e004ef2015-02-13 11:48:10 +00001148 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001149 if (len > I830_BATCH_LIMIT)
1150 return -ENOSPC;
1151
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001152 cs = intel_ring_begin(req, 6 + 2);
1153 if (IS_ERR(cs))
1154 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001155
1156 /* Blit the batch (which has now all relocs applied) to the
1157 * stable batch scratch bo area (so that the CS never
1158 * stumbles over its tlb invalidation bug) ...
1159 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001160 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
1161 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
1162 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
1163 *cs++ = cs_offset;
1164 *cs++ = 4096;
1165 *cs++ = offset;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001166
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001167 *cs++ = MI_FLUSH;
1168 *cs++ = MI_NOOP;
1169 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001170
1171 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001172 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001173 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001174
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001175 cs = intel_ring_begin(req, 2);
1176 if (IS_ERR(cs))
1177 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001178
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001179 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1180 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1181 MI_BATCH_NON_SECURE);
1182 intel_ring_advance(req, cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001183
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001184 return 0;
1185}
1186
1187static int
Chris Wilson803688b2016-08-02 22:50:27 +01001188i915_emit_bb_start(struct drm_i915_gem_request *req,
1189 u64 offset, u32 len,
1190 unsigned int dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001191{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001192 u32 *cs;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001193
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001194 cs = intel_ring_begin(req, 2);
1195 if (IS_ERR(cs))
1196 return PTR_ERR(cs);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001197
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001198 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1199 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1200 MI_BATCH_NON_SECURE);
1201 intel_ring_advance(req, cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001202
Eric Anholt62fdfea2010-05-21 13:26:39 -07001203 return 0;
1204}
1205
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001206
Chris Wilson6b8294a2012-11-16 11:43:20 +00001207
Chris Wilsond822bb12017-04-03 12:34:25 +01001208int intel_ring_pin(struct intel_ring *ring,
1209 struct drm_i915_private *i915,
1210 unsigned int offset_bias)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001211{
Chris Wilsond822bb12017-04-03 12:34:25 +01001212 enum i915_map_type map = HAS_LLC(i915) ? I915_MAP_WB : I915_MAP_WC;
Chris Wilson57e88532016-08-15 10:48:57 +01001213 struct i915_vma *vma = ring->vma;
Chris Wilsond822bb12017-04-03 12:34:25 +01001214 unsigned int flags;
Dave Gordon83052162016-04-12 14:46:16 +01001215 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001216 int ret;
1217
Chris Wilson57e88532016-08-15 10:48:57 +01001218 GEM_BUG_ON(ring->vaddr);
1219
Chris Wilson9d808412016-08-18 17:16:56 +01001220
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001221 flags = PIN_GLOBAL;
1222 if (offset_bias)
1223 flags |= PIN_OFFSET_BIAS | offset_bias;
Chris Wilson9d808412016-08-18 17:16:56 +01001224 if (vma->obj->stolen)
Chris Wilson57e88532016-08-15 10:48:57 +01001225 flags |= PIN_MAPPABLE;
1226
1227 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilson9d808412016-08-18 17:16:56 +01001228 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
Chris Wilson57e88532016-08-15 10:48:57 +01001229 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1230 else
1231 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1232 if (unlikely(ret))
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001233 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001234 }
1235
Chris Wilson57e88532016-08-15 10:48:57 +01001236 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1237 if (unlikely(ret))
1238 return ret;
1239
Chris Wilson9d808412016-08-18 17:16:56 +01001240 if (i915_vma_is_map_and_fenceable(vma))
Chris Wilson57e88532016-08-15 10:48:57 +01001241 addr = (void __force *)i915_vma_pin_iomap(vma);
1242 else
Chris Wilson9d808412016-08-18 17:16:56 +01001243 addr = i915_gem_object_pin_map(vma->obj, map);
Chris Wilson57e88532016-08-15 10:48:57 +01001244 if (IS_ERR(addr))
1245 goto err;
1246
Chris Wilson3d574a62017-10-13 21:26:16 +01001247 vma->obj->pin_global++;
1248
Chris Wilson32c04f12016-08-02 22:50:22 +01001249 ring->vaddr = addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001250 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01001251
Chris Wilson57e88532016-08-15 10:48:57 +01001252err:
1253 i915_vma_unpin(vma);
1254 return PTR_ERR(addr);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001255}
1256
Chris Wilsone6ba9992017-04-25 14:00:49 +01001257void intel_ring_reset(struct intel_ring *ring, u32 tail)
1258{
1259 GEM_BUG_ON(!list_empty(&ring->request_list));
1260 ring->tail = tail;
1261 ring->head = tail;
1262 ring->emit = tail;
1263 intel_ring_update_space(ring);
1264}
1265
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001266void intel_ring_unpin(struct intel_ring *ring)
1267{
1268 GEM_BUG_ON(!ring->vma);
1269 GEM_BUG_ON(!ring->vaddr);
1270
Chris Wilsone6ba9992017-04-25 14:00:49 +01001271 /* Discard any unused bytes beyond that submitted to hw. */
1272 intel_ring_reset(ring, ring->tail);
1273
Chris Wilson9d808412016-08-18 17:16:56 +01001274 if (i915_vma_is_map_and_fenceable(ring->vma))
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001275 i915_vma_unpin_iomap(ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001276 else
1277 i915_gem_object_unpin_map(ring->vma->obj);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001278 ring->vaddr = NULL;
1279
Chris Wilson3d574a62017-10-13 21:26:16 +01001280 ring->vma->obj->pin_global--;
Chris Wilson57e88532016-08-15 10:48:57 +01001281 i915_vma_unpin(ring->vma);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001282}
1283
Chris Wilson57e88532016-08-15 10:48:57 +01001284static struct i915_vma *
1285intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
Oscar Mateo2919d292014-07-03 16:28:02 +01001286{
Chris Wilsone3efda42014-04-09 09:19:41 +01001287 struct drm_i915_gem_object *obj;
Chris Wilson57e88532016-08-15 10:48:57 +01001288 struct i915_vma *vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001289
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00001290 obj = i915_gem_object_create_stolen(dev_priv, size);
Chris Wilsonc58b7352016-08-18 17:16:57 +01001291 if (!obj)
Chris Wilson2d6c4c82017-04-20 11:17:09 +01001292 obj = i915_gem_object_create_internal(dev_priv, size);
Chris Wilson57e88532016-08-15 10:48:57 +01001293 if (IS_ERR(obj))
1294 return ERR_CAST(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001295
Akash Goel24f3a8c2014-06-17 10:59:42 +05301296 /* mark ring buffers as read-only from GPU side by default */
1297 obj->gt_ro = 1;
1298
Chris Wilsona01cb372017-01-16 15:21:30 +00001299 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson57e88532016-08-15 10:48:57 +01001300 if (IS_ERR(vma))
1301 goto err;
Chris Wilsone3efda42014-04-09 09:19:41 +01001302
Chris Wilson57e88532016-08-15 10:48:57 +01001303 return vma;
1304
1305err:
1306 i915_gem_object_put(obj);
1307 return vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001308}
1309
Chris Wilson7e37f882016-08-02 22:50:21 +01001310struct intel_ring *
1311intel_engine_create_ring(struct intel_engine_cs *engine, int size)
Chris Wilson01101fa2015-09-03 13:01:39 +01001312{
Chris Wilson7e37f882016-08-02 22:50:21 +01001313 struct intel_ring *ring;
Chris Wilson57e88532016-08-15 10:48:57 +01001314 struct i915_vma *vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001315
Chris Wilson8f942012016-08-02 22:50:30 +01001316 GEM_BUG_ON(!is_power_of_2(size));
Chris Wilson62ae14b2016-10-04 21:11:25 +01001317 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
Chris Wilson8f942012016-08-02 22:50:30 +01001318
Chris Wilson01101fa2015-09-03 13:01:39 +01001319 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson57e88532016-08-15 10:48:57 +01001320 if (!ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001321 return ERR_PTR(-ENOMEM);
1322
Chris Wilson675d9ad2016-08-04 07:52:36 +01001323 INIT_LIST_HEAD(&ring->request_list);
1324
Chris Wilson01101fa2015-09-03 13:01:39 +01001325 ring->size = size;
1326 /* Workaround an erratum on the i830 which causes a hang if
1327 * the TAIL pointer points to within the last 2 cachelines
1328 * of the buffer.
1329 */
1330 ring->effective_size = size;
Jani Nikula2a307c22016-11-30 17:43:04 +02001331 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01001332 ring->effective_size -= 2 * CACHELINE_BYTES;
1333
Chris Wilson01101fa2015-09-03 13:01:39 +01001334 intel_ring_update_space(ring);
1335
Chris Wilson57e88532016-08-15 10:48:57 +01001336 vma = intel_ring_create_vma(engine->i915, size);
1337 if (IS_ERR(vma)) {
Chris Wilson01101fa2015-09-03 13:01:39 +01001338 kfree(ring);
Chris Wilson57e88532016-08-15 10:48:57 +01001339 return ERR_CAST(vma);
Chris Wilson01101fa2015-09-03 13:01:39 +01001340 }
Chris Wilson57e88532016-08-15 10:48:57 +01001341 ring->vma = vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001342
1343 return ring;
1344}
1345
1346void
Chris Wilson7e37f882016-08-02 22:50:21 +01001347intel_ring_free(struct intel_ring *ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001348{
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001349 struct drm_i915_gem_object *obj = ring->vma->obj;
1350
1351 i915_vma_close(ring->vma);
1352 __i915_gem_object_release_unless_active(obj);
1353
Chris Wilson01101fa2015-09-03 13:01:39 +01001354 kfree(ring);
1355}
1356
Chris Wilson72b72ae2017-02-10 10:14:22 +00001357static int context_pin(struct i915_gem_context *ctx)
Chris Wilsone8a9c582016-12-18 15:37:20 +00001358{
1359 struct i915_vma *vma = ctx->engine[RCS].state;
1360 int ret;
1361
1362 /* Clear this page out of any CPU caches for coherent swap-in/out.
1363 * We only want to do this on the first bind so that we do not stall
1364 * on an active context (which by nature is already on the GPU).
1365 */
1366 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1367 ret = i915_gem_object_set_to_gtt_domain(vma->obj, false);
1368 if (ret)
1369 return ret;
1370 }
1371
Chris Wilsonafeddf52017-02-27 13:59:13 +00001372 return i915_vma_pin(vma, 0, I915_GTT_MIN_ALIGNMENT,
1373 PIN_GLOBAL | PIN_HIGH);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001374}
1375
Chris Wilson3204c342017-04-27 11:46:51 +01001376static struct i915_vma *
1377alloc_context_vma(struct intel_engine_cs *engine)
1378{
1379 struct drm_i915_private *i915 = engine->i915;
1380 struct drm_i915_gem_object *obj;
1381 struct i915_vma *vma;
1382
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001383 obj = i915_gem_object_create(i915, engine->context_size);
Chris Wilson3204c342017-04-27 11:46:51 +01001384 if (IS_ERR(obj))
1385 return ERR_CAST(obj);
1386
1387 /*
1388 * Try to make the context utilize L3 as well as LLC.
1389 *
1390 * On VLV we don't have L3 controls in the PTEs so we
1391 * shouldn't touch the cache level, especially as that
1392 * would make the object snooped which might have a
1393 * negative performance impact.
1394 *
1395 * Snooping is required on non-llc platforms in execlist
1396 * mode, but since all GGTT accesses use PAT entry 0 we
1397 * get snooping anyway regardless of cache_level.
1398 *
1399 * This is only applicable for Ivy Bridge devices since
1400 * later platforms don't have L3 control bits in the PTE.
1401 */
1402 if (IS_IVYBRIDGE(i915)) {
1403 /* Ignore any error, regard it as a simple optimisation */
1404 i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
1405 }
1406
1407 vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
1408 if (IS_ERR(vma))
1409 i915_gem_object_put(obj);
1410
1411 return vma;
1412}
1413
Chris Wilson266a2402017-05-04 10:33:08 +01001414static struct intel_ring *
1415intel_ring_context_pin(struct intel_engine_cs *engine,
1416 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001417{
1418 struct intel_context *ce = &ctx->engine[engine->id];
1419 int ret;
1420
Chris Wilson91c8a322016-07-05 10:40:23 +01001421 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001422
Chris Wilson266a2402017-05-04 10:33:08 +01001423 if (likely(ce->pin_count++))
1424 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001425 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson0cb26a82016-06-24 14:55:53 +01001426
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001427 if (!ce->state && engine->context_size) {
Chris Wilson3204c342017-04-27 11:46:51 +01001428 struct i915_vma *vma;
1429
1430 vma = alloc_context_vma(engine);
1431 if (IS_ERR(vma)) {
1432 ret = PTR_ERR(vma);
Chris Wilson266a2402017-05-04 10:33:08 +01001433 goto err;
Chris Wilson3204c342017-04-27 11:46:51 +01001434 }
1435
1436 ce->state = vma;
1437 }
1438
Chris Wilson0cb26a82016-06-24 14:55:53 +01001439 if (ce->state) {
Chris Wilson72b72ae2017-02-10 10:14:22 +00001440 ret = context_pin(ctx);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001441 if (ret)
Chris Wilson266a2402017-05-04 10:33:08 +01001442 goto err;
Chris Wilson5d4bac52017-03-22 20:59:30 +00001443
1444 ce->state->obj->mm.dirty = true;
Chris Wilson3d574a62017-10-13 21:26:16 +01001445 ce->state->obj->pin_global++;
Chris Wilson0cb26a82016-06-24 14:55:53 +01001446 }
1447
Chris Wilsonc7c3c072016-06-24 14:55:54 +01001448 /* The kernel context is only used as a placeholder for flushing the
1449 * active context. It is never used for submitting user rendering and
1450 * as such never requires the golden render context, and so we can skip
1451 * emitting it when we switch to the kernel context. This is required
1452 * as during eviction we cannot allocate and pin the renderstate in
1453 * order to initialise the context.
1454 */
Chris Wilson984ff29f2017-01-06 15:20:13 +00001455 if (i915_gem_context_is_kernel(ctx))
Chris Wilsonc7c3c072016-06-24 14:55:54 +01001456 ce->initialised = true;
1457
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001458 i915_gem_context_get(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001459
Chris Wilson266a2402017-05-04 10:33:08 +01001460out:
1461 /* One ringbuffer to rule them all */
1462 return engine->buffer;
1463
1464err:
Chris Wilson0cb26a82016-06-24 14:55:53 +01001465 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001466 return ERR_PTR(ret);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001467}
1468
Chris Wilsone8a9c582016-12-18 15:37:20 +00001469static void intel_ring_context_unpin(struct intel_engine_cs *engine,
1470 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001471{
1472 struct intel_context *ce = &ctx->engine[engine->id];
1473
Chris Wilson91c8a322016-07-05 10:40:23 +01001474 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001475 GEM_BUG_ON(ce->pin_count == 0);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001476
1477 if (--ce->pin_count)
1478 return;
1479
Chris Wilson3d574a62017-10-13 21:26:16 +01001480 if (ce->state) {
1481 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001482 i915_vma_unpin(ce->state);
Chris Wilson3d574a62017-10-13 21:26:16 +01001483 }
Chris Wilson0cb26a82016-06-24 14:55:53 +01001484
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001485 i915_gem_context_put(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001486}
1487
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001488static int intel_init_ring_buffer(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001489{
Chris Wilson32c04f12016-08-02 22:50:22 +01001490 struct intel_ring *ring;
Chris Wilson1a5788b2017-04-03 12:34:26 +01001491 int err;
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001492
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001493 intel_engine_setup_common(engine);
1494
Chris Wilson1a5788b2017-04-03 12:34:26 +01001495 err = intel_engine_init_common(engine);
1496 if (err)
1497 goto err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001498
Chris Wilsond822bb12017-04-03 12:34:25 +01001499 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
1500 if (IS_ERR(ring)) {
Chris Wilson1a5788b2017-04-03 12:34:26 +01001501 err = PTR_ERR(ring);
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001502 goto err;
Chris Wilsond822bb12017-04-03 12:34:25 +01001503 }
1504
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001505 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
Chris Wilson1a5788b2017-04-03 12:34:26 +01001506 err = intel_ring_pin(ring, engine->i915, I915_GTT_PAGE_SIZE);
1507 if (err)
1508 goto err_ring;
1509
1510 GEM_BUG_ON(engine->buffer);
Chris Wilson57e88532016-08-15 10:48:57 +01001511 engine->buffer = ring;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001512
Oscar Mateo8ee14972014-05-22 14:13:34 +01001513 return 0;
1514
Chris Wilson1a5788b2017-04-03 12:34:26 +01001515err_ring:
1516 intel_ring_free(ring);
Chris Wilson1a5788b2017-04-03 12:34:26 +01001517err:
1518 intel_engine_cleanup_common(engine);
1519 return err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001520}
1521
Chris Wilson7e37f882016-08-02 22:50:21 +01001522void intel_engine_cleanup(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001523{
Chris Wilson1a5788b2017-04-03 12:34:26 +01001524 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson33626e62010-10-29 16:18:36 +01001525
Chris Wilson1a5788b2017-04-03 12:34:26 +01001526 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
1527 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
John Harrison6402c332014-10-31 12:00:26 +00001528
Chris Wilson1a5788b2017-04-03 12:34:26 +01001529 intel_ring_unpin(engine->buffer);
1530 intel_ring_free(engine->buffer);
Chris Wilson78501ea2010-10-27 12:18:21 +01001531
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001532 if (engine->cleanup)
1533 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08001534
Chris Wilson96a945a2016-08-03 13:19:16 +01001535 intel_engine_cleanup_common(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001536
Akash Goel3b3f1652016-10-13 22:44:48 +05301537 dev_priv->engine[engine->id] = NULL;
1538 kfree(engine);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001539}
1540
Chris Wilson821ed7d2016-09-09 14:11:53 +01001541void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
1542{
1543 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301544 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001545
Chris Wilsone6ba9992017-04-25 14:00:49 +01001546 /* Restart from the beginning of the rings for convenience */
Chris Wilsonfe085f12017-03-21 10:25:52 +00001547 for_each_engine(engine, dev_priv, id)
Chris Wilsone6ba9992017-04-25 14:00:49 +01001548 intel_ring_reset(engine->buffer, 0);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001549}
1550
Chris Wilsonf73e7392016-12-18 15:37:24 +00001551static int ring_request_alloc(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00001552{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001553 u32 *cs;
Chris Wilson63103462016-04-28 09:56:49 +01001554
Chris Wilsone8a9c582016-12-18 15:37:20 +00001555 GEM_BUG_ON(!request->ctx->engine[request->engine->id].pin_count);
1556
Chris Wilson63103462016-04-28 09:56:49 +01001557 /* Flush enough space to reduce the likelihood of waiting after
1558 * we start building the request - in which case we will just
1559 * have to repeat work.
1560 */
Chris Wilsona0442462016-04-29 09:07:05 +01001561 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001562
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001563 cs = intel_ring_begin(request, 0);
1564 if (IS_ERR(cs))
1565 return PTR_ERR(cs);
Chris Wilson63103462016-04-28 09:56:49 +01001566
Chris Wilsona0442462016-04-29 09:07:05 +01001567 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001568 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00001569}
1570
Chris Wilson5e5655c2017-05-04 14:08:46 +01001571static noinline int wait_for_space(struct drm_i915_gem_request *req,
1572 unsigned int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001573{
Chris Wilson7e37f882016-08-02 22:50:21 +01001574 struct intel_ring *ring = req->ring;
Chris Wilson987046a2016-04-28 09:56:46 +01001575 struct drm_i915_gem_request *target;
Chris Wilsone95433c2016-10-28 13:58:27 +01001576 long timeout;
1577
1578 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson987046a2016-04-28 09:56:46 +01001579
Chris Wilson95aebcb2017-05-04 14:08:45 +01001580 if (intel_ring_update_space(ring) >= bytes)
Chris Wilson987046a2016-04-28 09:56:46 +01001581 return 0;
1582
1583 /*
1584 * Space is reserved in the ringbuffer for finalising the request,
1585 * as that cannot be allowed to fail. During request finalisation,
1586 * reserved_space is set to 0 to stop the overallocation and the
1587 * assumption is that then we never need to wait (which has the
1588 * risk of failing with EINTR).
1589 *
1590 * See also i915_gem_request_alloc() and i915_add_request().
1591 */
Chris Wilson0251a962016-04-28 09:56:47 +01001592 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01001593
Chris Wilson675d9ad2016-08-04 07:52:36 +01001594 list_for_each_entry(target, &ring->request_list, ring_link) {
Chris Wilson987046a2016-04-28 09:56:46 +01001595 /* Would completion of this request free enough space? */
Chris Wilson605d5b32017-05-04 14:08:44 +01001596 if (bytes <= __intel_ring_space(target->postfix,
1597 ring->emit, ring->size))
Chris Wilson987046a2016-04-28 09:56:46 +01001598 break;
1599 }
1600
Chris Wilson675d9ad2016-08-04 07:52:36 +01001601 if (WARN_ON(&target->ring_link == &ring->request_list))
Chris Wilson987046a2016-04-28 09:56:46 +01001602 return -ENOSPC;
1603
Chris Wilsone95433c2016-10-28 13:58:27 +01001604 timeout = i915_wait_request(target,
1605 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1606 MAX_SCHEDULE_TIMEOUT);
1607 if (timeout < 0)
1608 return timeout;
Chris Wilson7da844c2016-08-04 07:52:38 +01001609
Chris Wilson7da844c2016-08-04 07:52:38 +01001610 i915_gem_request_retire_upto(target);
1611
1612 intel_ring_update_space(ring);
1613 GEM_BUG_ON(ring->space < bytes);
1614 return 0;
Chris Wilson987046a2016-04-28 09:56:46 +01001615}
1616
Chris Wilson5e5655c2017-05-04 14:08:46 +01001617u32 *intel_ring_begin(struct drm_i915_gem_request *req,
1618 unsigned int num_dwords)
Chris Wilson987046a2016-04-28 09:56:46 +01001619{
Chris Wilson7e37f882016-08-02 22:50:21 +01001620 struct intel_ring *ring = req->ring;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001621 const unsigned int remain_usable = ring->effective_size - ring->emit;
1622 const unsigned int bytes = num_dwords * sizeof(u32);
1623 unsigned int need_wrap = 0;
1624 unsigned int total_bytes;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001625 u32 *cs;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001626
Chris Wilson6492ca72017-07-21 17:11:01 +01001627 /* Packets must be qword aligned. */
1628 GEM_BUG_ON(num_dwords & 1);
1629
Chris Wilson0251a962016-04-28 09:56:47 +01001630 total_bytes = bytes + req->reserved_space;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001631 GEM_BUG_ON(total_bytes > ring->effective_size);
John Harrison29b1b412015-06-18 13:10:09 +01001632
Chris Wilson5e5655c2017-05-04 14:08:46 +01001633 if (unlikely(total_bytes > remain_usable)) {
1634 const int remain_actual = ring->size - ring->emit;
1635
1636 if (bytes > remain_usable) {
1637 /*
1638 * Not enough space for the basic request. So need to
1639 * flush out the remainder and then wait for
1640 * base + reserved.
1641 */
1642 total_bytes += remain_actual;
1643 need_wrap = remain_actual | 1;
1644 } else {
1645 /*
1646 * The base request will fit but the reserved space
1647 * falls off the end. So we don't need an immediate
1648 * wrap and only need to effectively wait for the
1649 * reserved size from the start of ringbuffer.
1650 */
1651 total_bytes = req->reserved_space + remain_actual;
1652 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001653 }
1654
Chris Wilson5e5655c2017-05-04 14:08:46 +01001655 if (unlikely(total_bytes > ring->space)) {
1656 int ret = wait_for_space(req, total_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001657 if (unlikely(ret))
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001658 return ERR_PTR(ret);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001659 }
1660
Chris Wilson987046a2016-04-28 09:56:46 +01001661 if (unlikely(need_wrap)) {
Chris Wilson5e5655c2017-05-04 14:08:46 +01001662 need_wrap &= ~1;
1663 GEM_BUG_ON(need_wrap > ring->space);
1664 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001665
Chris Wilson987046a2016-04-28 09:56:46 +01001666 /* Fill the tail with MI_NOOP */
Chris Wilson5e5655c2017-05-04 14:08:46 +01001667 memset(ring->vaddr + ring->emit, 0, need_wrap);
Chris Wilsone6ba9992017-04-25 14:00:49 +01001668 ring->emit = 0;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001669 ring->space -= need_wrap;
Chris Wilson987046a2016-04-28 09:56:46 +01001670 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001671
Chris Wilsone6ba9992017-04-25 14:00:49 +01001672 GEM_BUG_ON(ring->emit > ring->size - bytes);
Chris Wilson605d5b32017-05-04 14:08:44 +01001673 GEM_BUG_ON(ring->space < bytes);
Chris Wilsone6ba9992017-04-25 14:00:49 +01001674 cs = ring->vaddr + ring->emit;
Chris Wilson01001862017-04-23 18:06:17 +01001675 GEM_DEBUG_EXEC(memset(cs, POISON_INUSE, bytes));
Chris Wilsone6ba9992017-04-25 14:00:49 +01001676 ring->emit += bytes;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001677 ring->space -= bytes;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001678
1679 return cs;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001680}
1681
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001682/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01001683int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001684{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001685 int num_dwords =
Chris Wilsone6ba9992017-04-25 14:00:49 +01001686 (req->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001687 u32 *cs;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001688
1689 if (num_dwords == 0)
1690 return 0;
1691
Chris Wilson18393f62014-04-09 09:19:40 +01001692 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001693 cs = intel_ring_begin(req, num_dwords);
1694 if (IS_ERR(cs))
1695 return PTR_ERR(cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001696
1697 while (num_dwords--)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001698 *cs++ = MI_NOOP;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001699
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001700 intel_ring_advance(req, cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001701
1702 return 0;
1703}
1704
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001705static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001706{
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001707 struct drm_i915_private *dev_priv = request->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001708
Chris Wilson76f84212016-06-30 15:33:45 +01001709 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
1710
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001711 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001712
Chris Wilson12f55812012-07-05 17:14:01 +01001713 /* Disable notification that the ring is IDLE. The GT
1714 * will then assume that it is busy and bring it out of rc6.
1715 */
Chris Wilson76f84212016-06-30 15:33:45 +01001716 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1717 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01001718
1719 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01001720 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01001721
1722 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson02b312d2017-04-11 11:13:37 +01001723 if (__intel_wait_for_register_fw(dev_priv,
1724 GEN6_BSD_SLEEP_PSMI_CONTROL,
1725 GEN6_BSD_SLEEP_INDICATOR,
1726 0,
1727 1000, 0, NULL))
Chris Wilson12f55812012-07-05 17:14:01 +01001728 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001729
Chris Wilson12f55812012-07-05 17:14:01 +01001730 /* Now that the ring is fully powered up, update the tail */
Chris Wilsonb0411e72016-08-02 22:50:34 +01001731 i9xx_submit_request(request);
Chris Wilson12f55812012-07-05 17:14:01 +01001732
1733 /* Let the ring send IDLE messages to the GT again,
1734 * and so let it sleep to conserve power when idle.
1735 */
Chris Wilson76f84212016-06-30 15:33:45 +01001736 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1737 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1738
1739 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001740}
1741
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001742static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001743{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001744 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001745
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001746 cs = intel_ring_begin(req, 4);
1747 if (IS_ERR(cs))
1748 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001749
Chris Wilson71a77e02011-02-02 12:13:49 +00001750 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01001751 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001752 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001753
1754 /* We always require a command barrier so that subsequent
1755 * commands, such as breadcrumb interrupts, are strictly ordered
1756 * wrt the contents of the write cache being flushed to memory
1757 * (and thus being coherent from the CPU).
1758 */
1759 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1760
Jesse Barnes9a289772012-10-26 09:42:42 -07001761 /*
1762 * Bspec vol 1c.5 - video engine command streamer:
1763 * "If ENABLED, all TLBs will be invalidated once the flush
1764 * operation is complete. This bit is only valid when the
1765 * Post-Sync Operation field is a value of 1h or 3h."
1766 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001767 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001768 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1769
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001770 *cs++ = cmd;
1771 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilsonc0336662016-05-06 15:40:21 +01001772 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001773 *cs++ = 0; /* upper addr */
1774 *cs++ = 0; /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001775 } else {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001776 *cs++ = 0;
1777 *cs++ = MI_NOOP;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001778 }
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001779 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001780 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001781}
1782
1783static int
Chris Wilson803688b2016-08-02 22:50:27 +01001784gen8_emit_bb_start(struct drm_i915_gem_request *req,
1785 u64 offset, u32 len,
1786 unsigned int dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001787{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001788 bool ppgtt = USES_PPGTT(req->i915) &&
John Harrison8e004ef2015-02-13 11:48:10 +00001789 !(dispatch_flags & I915_DISPATCH_SECURE);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001790 u32 *cs;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001791
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001792 cs = intel_ring_begin(req, 4);
1793 if (IS_ERR(cs))
1794 return PTR_ERR(cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001795
1796 /* FIXME(BDW): Address space and security selectors. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001797 *cs++ = MI_BATCH_BUFFER_START_GEN8 | (ppgtt << 8) | (dispatch_flags &
1798 I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
1799 *cs++ = lower_32_bits(offset);
1800 *cs++ = upper_32_bits(offset);
1801 *cs++ = MI_NOOP;
1802 intel_ring_advance(req, cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001803
1804 return 0;
1805}
1806
1807static int
Chris Wilson803688b2016-08-02 22:50:27 +01001808hsw_emit_bb_start(struct drm_i915_gem_request *req,
1809 u64 offset, u32 len,
1810 unsigned int dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001811{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001812 u32 *cs;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001813
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001814 cs = intel_ring_begin(req, 2);
1815 if (IS_ERR(cs))
1816 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001817
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001818 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1819 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
1820 (dispatch_flags & I915_DISPATCH_RS ?
1821 MI_BATCH_RESOURCE_STREAMER : 0);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001822 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001823 *cs++ = offset;
1824 intel_ring_advance(req, cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001825
1826 return 0;
1827}
1828
1829static int
Chris Wilson803688b2016-08-02 22:50:27 +01001830gen6_emit_bb_start(struct drm_i915_gem_request *req,
1831 u64 offset, u32 len,
1832 unsigned int dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001833{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001834 u32 *cs;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001835
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001836 cs = intel_ring_begin(req, 2);
1837 if (IS_ERR(cs))
1838 return PTR_ERR(cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001839
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001840 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1841 0 : MI_BATCH_NON_SECURE_I965);
Akshay Joshi0206e352011-08-16 15:34:10 -04001842 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001843 *cs++ = offset;
1844 intel_ring_advance(req, cs);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001845
Akshay Joshi0206e352011-08-16 15:34:10 -04001846 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001847}
1848
Chris Wilson549f7362010-10-19 11:19:32 +01001849/* Blitter support (SandyBridge+) */
1850
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001851static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan hai8d192152010-11-02 16:31:01 +08001852{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001853 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001854
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001855 cs = intel_ring_begin(req, 4);
1856 if (IS_ERR(cs))
1857 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001858
Chris Wilson71a77e02011-02-02 12:13:49 +00001859 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01001860 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001861 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001862
1863 /* We always require a command barrier so that subsequent
1864 * commands, such as breadcrumb interrupts, are strictly ordered
1865 * wrt the contents of the write cache being flushed to memory
1866 * (and thus being coherent from the CPU).
1867 */
1868 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1869
Jesse Barnes9a289772012-10-26 09:42:42 -07001870 /*
1871 * Bspec vol 1c.3 - blitter engine command streamer:
1872 * "If ENABLED, all TLBs will be invalidated once the flush
1873 * operation is complete. This bit is only valid when the
1874 * Post-Sync Operation field is a value of 1h or 3h."
1875 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001876 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001877 cmd |= MI_INVALIDATE_TLB;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001878 *cs++ = cmd;
1879 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilsonc0336662016-05-06 15:40:21 +01001880 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001881 *cs++ = 0; /* upper addr */
1882 *cs++ = 0; /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001883 } else {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001884 *cs++ = 0;
1885 *cs++ = MI_NOOP;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001886 }
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001887 intel_ring_advance(req, cs);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001888
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001889 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001890}
1891
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001892static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
1893 struct intel_engine_cs *engine)
1894{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001895 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001896 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001897
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001898 if (!i915_modparams.semaphores)
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001899 return;
1900
Chris Wilson51d545d2016-08-15 10:49:02 +01001901 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
1902 struct i915_vma *vma;
1903
Chris Wilsonf51455d2017-01-10 14:47:34 +00001904 obj = i915_gem_object_create(dev_priv, PAGE_SIZE);
Chris Wilson51d545d2016-08-15 10:49:02 +01001905 if (IS_ERR(obj))
1906 goto err;
1907
Chris Wilsona01cb372017-01-16 15:21:30 +00001908 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson51d545d2016-08-15 10:49:02 +01001909 if (IS_ERR(vma))
1910 goto err_obj;
1911
1912 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1913 if (ret)
1914 goto err_obj;
1915
1916 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
1917 if (ret)
1918 goto err_obj;
1919
1920 dev_priv->semaphore = vma;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001921 }
1922
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001923 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001924 u32 offset = i915_ggtt_offset(dev_priv->semaphore);
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001925
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001926 engine->semaphore.sync_to = gen8_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001927 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001928
1929 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001930 u32 ring_offset;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001931
1932 if (i != engine->id)
1933 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
1934 else
1935 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
1936
1937 engine->semaphore.signal_ggtt[i] = ring_offset;
1938 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001939 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001940 engine->semaphore.sync_to = gen6_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001941 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001942
1943 /*
1944 * The current semaphore is only applied on pre-gen8
1945 * platform. And there is no VCS2 ring on the pre-gen8
1946 * platform. So the semaphore between RCS and VCS2 is
1947 * initialized as INVALID. Gen8 will initialize the
1948 * sema between VCS2 and RCS later.
1949 */
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001950 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001951 static const struct {
1952 u32 wait_mbox;
1953 i915_reg_t mbox_reg;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001954 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
1955 [RCS_HW] = {
1956 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
1957 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
1958 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001959 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001960 [VCS_HW] = {
1961 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
1962 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
1963 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001964 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001965 [BCS_HW] = {
1966 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
1967 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
1968 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001969 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001970 [VECS_HW] = {
1971 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
1972 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
1973 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001974 },
1975 };
1976 u32 wait_mbox;
1977 i915_reg_t mbox_reg;
1978
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001979 if (i == engine->hw_id) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001980 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
1981 mbox_reg = GEN6_NOSYNC;
1982 } else {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001983 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
1984 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001985 }
1986
1987 engine->semaphore.mbox.wait[i] = wait_mbox;
1988 engine->semaphore.mbox.signal[i] = mbox_reg;
1989 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001990 }
Chris Wilson51d545d2016-08-15 10:49:02 +01001991
1992 return;
1993
1994err_obj:
1995 i915_gem_object_put(obj);
1996err:
1997 DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001998 i915_modparams.semaphores = 0;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001999}
2000
Chris Wilsoned003072016-07-01 09:18:13 +01002001static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2002 struct intel_engine_cs *engine)
2003{
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002004 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2005
Chris Wilsoned003072016-07-01 09:18:13 +01002006 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002007 engine->irq_enable = gen8_irq_enable;
2008 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002009 engine->irq_seqno_barrier = gen6_seqno_barrier;
2010 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002011 engine->irq_enable = gen6_irq_enable;
2012 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002013 engine->irq_seqno_barrier = gen6_seqno_barrier;
2014 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002015 engine->irq_enable = gen5_irq_enable;
2016 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002017 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002018 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002019 engine->irq_enable = i9xx_irq_enable;
2020 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002021 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002022 engine->irq_enable = i8xx_irq_enable;
2023 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002024 }
2025}
2026
Chris Wilsonff44ad52017-03-16 17:13:03 +00002027static void i9xx_set_default_submission(struct intel_engine_cs *engine)
2028{
2029 engine->submit_request = i9xx_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01002030 engine->cancel_requests = cancel_requests;
Chris Wilsonaba5e272017-10-25 15:39:41 +01002031
2032 engine->park = NULL;
2033 engine->unpark = NULL;
Chris Wilsonff44ad52017-03-16 17:13:03 +00002034}
2035
2036static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
2037{
Chris Wilsonaba5e272017-10-25 15:39:41 +01002038 i9xx_set_default_submission(engine);
Chris Wilsonff44ad52017-03-16 17:13:03 +00002039 engine->submit_request = gen6_bsd_submit_request;
2040}
2041
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002042static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2043 struct intel_engine_cs *engine)
2044{
Chris Wilson618e4ca2016-08-02 22:50:35 +01002045 intel_ring_init_irq(dev_priv, engine);
2046 intel_ring_init_semaphores(dev_priv, engine);
2047
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002048 engine->init_hw = init_ring_common;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002049 engine->reset_hw = reset_ring_common;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002050
Chris Wilsone8a9c582016-12-18 15:37:20 +00002051 engine->context_pin = intel_ring_context_pin;
2052 engine->context_unpin = intel_ring_context_unpin;
2053
Chris Wilsonf73e7392016-12-18 15:37:24 +00002054 engine->request_alloc = ring_request_alloc;
2055
Chris Wilson9b81d552016-10-28 13:58:50 +01002056 engine->emit_breadcrumb = i9xx_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002057 engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00002058 if (i915_modparams.semaphores) {
Chris Wilson98f29e82016-10-28 13:58:51 +01002059 int num_rings;
2060
Chris Wilson9b81d552016-10-28 13:58:50 +01002061 engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002062
Tvrtko Ursulinc58949f2017-06-19 11:59:17 +01002063 num_rings = INTEL_INFO(dev_priv)->num_rings - 1;
Chris Wilson98f29e82016-10-28 13:58:51 +01002064 if (INTEL_GEN(dev_priv) >= 8) {
2065 engine->emit_breadcrumb_sz += num_rings * 6;
2066 } else {
2067 engine->emit_breadcrumb_sz += num_rings * 3;
2068 if (num_rings & 1)
2069 engine->emit_breadcrumb_sz++;
2070 }
2071 }
Chris Wilsonff44ad52017-03-16 17:13:03 +00002072
2073 engine->set_default_submission = i9xx_set_default_submission;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002074
2075 if (INTEL_GEN(dev_priv) >= 8)
Chris Wilson803688b2016-08-02 22:50:27 +01002076 engine->emit_bb_start = gen8_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002077 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson803688b2016-08-02 22:50:27 +01002078 engine->emit_bb_start = gen6_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002079 else if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson803688b2016-08-02 22:50:27 +01002080 engine->emit_bb_start = i965_emit_bb_start;
Jani Nikula2a307c22016-11-30 17:43:04 +02002081 else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002082 engine->emit_bb_start = i830_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002083 else
Chris Wilson803688b2016-08-02 22:50:27 +01002084 engine->emit_bb_start = i915_emit_bb_start;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002085}
2086
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002087int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002088{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002089 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07002090 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002091
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002092 intel_ring_default_vfuncs(dev_priv, engine);
2093
Chris Wilson61ff75a2016-07-01 17:23:28 +01002094 if (HAS_L3_DPF(dev_priv))
2095 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002096
Chris Wilsonc0336662016-05-06 15:40:21 +01002097 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002098 engine->init_context = intel_rcs_ctx_init;
Chris Wilson9b81d552016-10-28 13:58:50 +01002099 engine->emit_breadcrumb = gen8_render_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002100 engine->emit_breadcrumb_sz = gen8_render_emit_breadcrumb_sz;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002101 engine->emit_flush = gen8_render_ring_flush;
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00002102 if (i915_modparams.semaphores) {
Chris Wilson98f29e82016-10-28 13:58:51 +01002103 int num_rings;
2104
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002105 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilson98f29e82016-10-28 13:58:51 +01002106
Tvrtko Ursulinc58949f2017-06-19 11:59:17 +01002107 num_rings = INTEL_INFO(dev_priv)->num_rings - 1;
Chris Wilson6f9b8502017-03-24 15:17:24 +00002108 engine->emit_breadcrumb_sz += num_rings * 8;
Chris Wilson98f29e82016-10-28 13:58:51 +01002109 }
Chris Wilsonc0336662016-05-06 15:40:21 +01002110 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002111 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002112 engine->emit_flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002113 if (IS_GEN6(dev_priv))
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002114 engine->emit_flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002115 } else if (IS_GEN5(dev_priv)) {
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002116 engine->emit_flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002117 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002118 if (INTEL_GEN(dev_priv) < 4)
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002119 engine->emit_flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002120 else
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002121 engine->emit_flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002122 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002123 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002124
Chris Wilsonc0336662016-05-06 15:40:21 +01002125 if (IS_HASWELL(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002126 engine->emit_bb_start = hsw_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002127
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002128 engine->init_hw = init_render_ring;
2129 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002130
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002131 ret = intel_init_ring_buffer(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002132 if (ret)
2133 return ret;
2134
Chris Wilsonf8973c22016-07-01 17:23:21 +01002135 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonf51455d2017-01-10 14:47:34 +00002136 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Chris Wilson7d5ea802016-07-01 17:23:20 +01002137 if (ret)
2138 return ret;
2139 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01002140 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002141 if (ret)
2142 return ret;
2143 }
2144
2145 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002146}
2147
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002148int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002149{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002150 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002151
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002152 intel_ring_default_vfuncs(dev_priv, engine);
2153
Chris Wilsonc0336662016-05-06 15:40:21 +01002154 if (INTEL_GEN(dev_priv) >= 6) {
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002155 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002156 if (IS_GEN6(dev_priv))
Chris Wilsonff44ad52017-03-16 17:13:03 +00002157 engine->set_default_submission = gen6_bsd_set_default_submission;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002158 engine->emit_flush = gen6_bsd_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002159 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002160 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002161 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002162 engine->mmio_base = BSD_RING_BASE;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002163 engine->emit_flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002164 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002165 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002166 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002167 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002168 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002169
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002170 return intel_init_ring_buffer(engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002171}
Chris Wilson549f7362010-10-19 11:19:32 +01002172
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002173int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
Chris Wilson549f7362010-10-19 11:19:32 +01002174{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002175 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002176
2177 intel_ring_default_vfuncs(dev_priv, engine);
2178
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002179 engine->emit_flush = gen6_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002180 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002181 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002182
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002183 return intel_init_ring_buffer(engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002184}
Chris Wilsona7b97612012-07-20 12:41:08 +01002185
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002186int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002187{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002188 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002189
2190 intel_ring_default_vfuncs(dev_priv, engine);
2191
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002192 engine->emit_flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002193
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002194 if (INTEL_GEN(dev_priv) < 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002195 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01002196 engine->irq_enable = hsw_vebox_irq_enable;
2197 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002198 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002199
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002200 return intel_init_ring_buffer(engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002201}