blob: 513a0f4b469b32c9d0ac2e87c089bb6f2e4907ba [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
Tvrtko Ursulin2f35afe2017-02-16 12:23:21 +000042static int __intel_ring_space(int head, int tail, int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010043{
Dave Gordon4f547412014-11-27 11:22:48 +000044 int space = head - tail;
45 if (space <= 0)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010046 space += size;
Dave Gordon4f547412014-11-27 11:22:48 +000047 return space - I915_RING_FREE_SPACE;
Chris Wilson1cf0ba12014-05-05 09:07:33 +010048}
49
Chris Wilson32c04f12016-08-02 22:50:22 +010050void intel_ring_update_space(struct intel_ring *ring)
Dave Gordonebd0fd42014-11-27 11:22:49 +000051{
Chris Wilsona21ef712017-06-15 14:11:29 +010052 ring->space = __intel_ring_space(ring->head, ring->emit, ring->size);
Dave Gordonebd0fd42014-11-27 11:22:49 +000053}
54
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000055static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010056gen2_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010057{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000058 u32 cmd, *cs;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010059
60 cmd = MI_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010061
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010062 if (mode & EMIT_INVALIDATE)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010063 cmd |= MI_READ_FLUSH;
64
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000065 cs = intel_ring_begin(req, 2);
66 if (IS_ERR(cs))
67 return PTR_ERR(cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010068
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000069 *cs++ = cmd;
70 *cs++ = MI_NOOP;
71 intel_ring_advance(req, cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010072
73 return 0;
74}
75
76static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010077gen4_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Eric Anholt62fdfea2010-05-21 13:26:39 -070078{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000079 u32 cmd, *cs;
Chris Wilson6f392d52010-08-07 11:01:22 +010080
Chris Wilson36d527d2011-03-19 22:26:49 +000081 /*
82 * read/write caches:
83 *
84 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
85 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
86 * also flushed at 2d versus 3d pipeline switches.
87 *
88 * read-only caches:
89 *
90 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
91 * MI_READ_FLUSH is set, and is always flushed on 965.
92 *
93 * I915_GEM_DOMAIN_COMMAND may not exist?
94 *
95 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
96 * invalidated when MI_EXE_FLUSH is set.
97 *
98 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
99 * invalidated with every MI_FLUSH.
100 *
101 * TLBs:
102 *
103 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
104 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
105 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
106 * are flushed at any MI_FLUSH.
107 */
108
Chris Wilsonb5321f32016-08-02 22:50:18 +0100109 cmd = MI_FLUSH;
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100110 if (mode & EMIT_INVALIDATE) {
Chris Wilson36d527d2011-03-19 22:26:49 +0000111 cmd |= MI_EXE_FLUSH;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100112 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
113 cmd |= MI_INVALIDATE_ISP;
114 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000115
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000116 cs = intel_ring_begin(req, 2);
117 if (IS_ERR(cs))
118 return PTR_ERR(cs);
Chris Wilson36d527d2011-03-19 22:26:49 +0000119
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000120 *cs++ = cmd;
121 *cs++ = MI_NOOP;
122 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000123
124 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800125}
126
Jesse Barnes8d315282011-10-16 10:23:31 +0200127/**
128 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
129 * implementing two workarounds on gen6. From section 1.4.7.1
130 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
131 *
132 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
133 * produced by non-pipelined state commands), software needs to first
134 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
135 * 0.
136 *
137 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
138 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
139 *
140 * And the workaround for these two requires this workaround first:
141 *
142 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
143 * BEFORE the pipe-control with a post-sync op and no write-cache
144 * flushes.
145 *
146 * And this last workaround is tricky because of the requirements on
147 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
148 * volume 2 part 1:
149 *
150 * "1 of the following must also be set:
151 * - Render Target Cache Flush Enable ([12] of DW1)
152 * - Depth Cache Flush Enable ([0] of DW1)
153 * - Stall at Pixel Scoreboard ([1] of DW1)
154 * - Depth Stall ([13] of DW1)
155 * - Post-Sync Operation ([13] of DW1)
156 * - Notify Enable ([8] of DW1)"
157 *
158 * The cache flushes require the workaround flush that triggered this
159 * one, so we can't use it. Depth stall would trigger the same.
160 * Post-sync nonzero is what triggered this second workaround, so we
161 * can't use that one either. Notify enable is IRQs, which aren't
162 * really our business. That leaves only stall at scoreboard.
163 */
164static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100165intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200166{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100167 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100168 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000169 u32 *cs;
Jesse Barnes8d315282011-10-16 10:23:31 +0200170
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000171 cs = intel_ring_begin(req, 6);
172 if (IS_ERR(cs))
173 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200174
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000175 *cs++ = GFX_OP_PIPE_CONTROL(5);
176 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
177 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
178 *cs++ = 0; /* low dword */
179 *cs++ = 0; /* high dword */
180 *cs++ = MI_NOOP;
181 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200182
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000183 cs = intel_ring_begin(req, 6);
184 if (IS_ERR(cs))
185 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200186
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000187 *cs++ = GFX_OP_PIPE_CONTROL(5);
188 *cs++ = PIPE_CONTROL_QW_WRITE;
189 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
190 *cs++ = 0;
191 *cs++ = 0;
192 *cs++ = MI_NOOP;
193 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200194
195 return 0;
196}
197
198static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100199gen6_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Jesse Barnes8d315282011-10-16 10:23:31 +0200200{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100201 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100202 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000203 u32 *cs, flags = 0;
Jesse Barnes8d315282011-10-16 10:23:31 +0200204 int ret;
205
Paulo Zanonib3111502012-08-17 18:35:42 -0300206 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100207 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300208 if (ret)
209 return ret;
210
Jesse Barnes8d315282011-10-16 10:23:31 +0200211 /* Just flush everything. Experiments have shown that reducing the
212 * number of bits based on the write domains has little performance
213 * impact.
214 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100215 if (mode & EMIT_FLUSH) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100216 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
217 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
218 /*
219 * Ensure that any following seqno writes only happen
220 * when the render cache is indeed flushed.
221 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200222 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100223 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100224 if (mode & EMIT_INVALIDATE) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100225 flags |= PIPE_CONTROL_TLB_INVALIDATE;
226 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
227 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
228 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
229 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
230 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
231 /*
232 * TLB invalidate requires a post-sync write.
233 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700234 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100235 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200236
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000237 cs = intel_ring_begin(req, 4);
238 if (IS_ERR(cs))
239 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200240
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000241 *cs++ = GFX_OP_PIPE_CONTROL(4);
242 *cs++ = flags;
243 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
244 *cs++ = 0;
245 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200246
247 return 0;
248}
249
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100250static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100251gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300252{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000253 u32 *cs;
Paulo Zanonif3987632012-08-17 18:35:43 -0300254
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000255 cs = intel_ring_begin(req, 4);
256 if (IS_ERR(cs))
257 return PTR_ERR(cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300258
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000259 *cs++ = GFX_OP_PIPE_CONTROL(4);
260 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
261 *cs++ = 0;
262 *cs++ = 0;
263 intel_ring_advance(req, cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300264
265 return 0;
266}
267
268static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100269gen7_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300270{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100271 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100272 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000273 u32 *cs, flags = 0;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300274
Paulo Zanonif3987632012-08-17 18:35:43 -0300275 /*
276 * Ensure that any following seqno writes only happen when the render
277 * cache is indeed flushed.
278 *
279 * Workaround: 4th PIPE_CONTROL command (except the ones with only
280 * read-cache invalidate bits set) must have the CS_STALL bit set. We
281 * don't try to be clever and just set it unconditionally.
282 */
283 flags |= PIPE_CONTROL_CS_STALL;
284
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300285 /* Just flush everything. Experiments have shown that reducing the
286 * number of bits based on the write domains has little performance
287 * impact.
288 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100289 if (mode & EMIT_FLUSH) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300290 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
291 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800292 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100293 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300294 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100295 if (mode & EMIT_INVALIDATE) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300296 flags |= PIPE_CONTROL_TLB_INVALIDATE;
297 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
298 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
299 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
300 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
301 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000302 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300303 /*
304 * TLB invalidate requires a post-sync write.
305 */
306 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200307 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300308
Chris Wilsonadd284a2014-12-16 08:44:32 +0000309 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
310
Paulo Zanonif3987632012-08-17 18:35:43 -0300311 /* Workaround: we must issue a pipe_control with CS-stall bit
312 * set before a pipe_control command that has the state cache
313 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100314 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300315 }
316
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000317 cs = intel_ring_begin(req, 4);
318 if (IS_ERR(cs))
319 return PTR_ERR(cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300320
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000321 *cs++ = GFX_OP_PIPE_CONTROL(4);
322 *cs++ = flags;
323 *cs++ = scratch_addr;
324 *cs++ = 0;
325 intel_ring_advance(req, cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300326
327 return 0;
328}
329
Ben Widawskya5f3d682013-11-02 21:07:27 -0700330static int
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000331gen8_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300332{
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000333 u32 flags;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000334 u32 *cs;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300335
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000336 cs = intel_ring_begin(req, mode & EMIT_INVALIDATE ? 12 : 6);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000337 if (IS_ERR(cs))
338 return PTR_ERR(cs);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300339
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000340 flags = PIPE_CONTROL_CS_STALL;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700341
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100342 if (mode & EMIT_FLUSH) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700343 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
344 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800345 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100346 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700347 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100348 if (mode & EMIT_INVALIDATE) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700349 flags |= PIPE_CONTROL_TLB_INVALIDATE;
350 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
351 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
352 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
353 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
354 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
355 flags |= PIPE_CONTROL_QW_WRITE;
356 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800357
358 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000359 cs = gen8_emit_pipe_control(cs,
360 PIPE_CONTROL_CS_STALL |
361 PIPE_CONTROL_STALL_AT_SCOREBOARD,
362 0);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700363 }
364
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000365 cs = gen8_emit_pipe_control(cs, flags,
366 i915_ggtt_offset(req->engine->scratch) +
367 2 * CACHELINE_BYTES);
368
369 intel_ring_advance(req, cs);
370
371 return 0;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700372}
373
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000374static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200375{
Chris Wilsonc0336662016-05-06 15:40:21 +0100376 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200377 u32 addr;
378
379 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100380 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200381 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
382 I915_WRITE(HWS_PGA, addr);
383}
384
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000385static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000386{
Chris Wilsonc0336662016-05-06 15:40:21 +0100387 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200388 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000389
390 /* The ring status page addresses are no longer next to the rest of
391 * the ring registers as of gen7.
392 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100393 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000394 switch (engine->id) {
Damien Lespiauaf75f262015-02-10 19:32:17 +0000395 case RCS:
396 mmio = RENDER_HWS_PGA_GEN7;
397 break;
398 case BCS:
399 mmio = BLT_HWS_PGA_GEN7;
400 break;
401 /*
402 * VCS2 actually doesn't exist on Gen7. Only shut up
403 * gcc switch check warning
404 */
405 case VCS2:
406 case VCS:
407 mmio = BSD_HWS_PGA_GEN7;
408 break;
409 case VECS:
410 mmio = VEBOX_HWS_PGA_GEN7;
411 break;
412 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100413 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000414 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000415 } else {
416 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000417 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000418 }
419
Chris Wilson57e88532016-08-15 10:48:57 +0100420 I915_WRITE(mmio, engine->status_page.ggtt_offset);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000421 POSTING_READ(mmio);
422
423 /*
424 * Flush the TLB for this page
425 *
426 * FIXME: These two bits have disappeared on gen8, so a question
427 * arises: do we still need this and if so how should we go about
428 * invalidating the TLB?
429 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100430 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000431 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000432
433 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000434 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000435
436 I915_WRITE(reg,
437 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
438 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100439 if (intel_wait_for_register(dev_priv,
440 reg, INSTPM_SYNC_FLUSH, 0,
441 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000442 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000443 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000444 }
445}
446
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000447static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100448{
Chris Wilsonc0336662016-05-06 15:40:21 +0100449 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100450
Chris Wilson21a2c582016-08-15 10:49:11 +0100451 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000452 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100453 if (intel_wait_for_register(dev_priv,
454 RING_MI_MODE(engine->mmio_base),
455 MODE_IDLE,
456 MODE_IDLE,
457 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000458 DRM_ERROR("%s : timed out trying to stop ring\n",
459 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100460 /* Sometimes we observe that the idle flag is not
461 * set even though the ring is empty. So double
462 * check before giving up.
463 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000464 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100465 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100466 }
467 }
468
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000469 I915_WRITE_CTL(engine, 0);
470 I915_WRITE_HEAD(engine, 0);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100471 I915_WRITE_TAIL(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100472
Chris Wilson21a2c582016-08-15 10:49:11 +0100473 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000474 (void)I915_READ_CTL(engine);
475 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
Chris Wilson9991ae72014-04-02 16:36:07 +0100476 }
477
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000478 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100479}
480
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000481static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800482{
Chris Wilsonc0336662016-05-06 15:40:21 +0100483 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +0100484 struct intel_ring *ring = engine->buffer;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200485 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800486
Mika Kuoppala59bad942015-01-16 11:34:40 +0200487 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200488
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000489 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100490 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000491 DRM_DEBUG_KMS("%s head not reset to zero "
492 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000493 engine->name,
494 I915_READ_CTL(engine),
495 I915_READ_HEAD(engine),
496 I915_READ_TAIL(engine),
497 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800498
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000499 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000500 DRM_ERROR("failed to set %s head 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));
Chris Wilson9991ae72014-04-02 16:36:07 +0100507 ret = -EIO;
508 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000509 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700510 }
511
Carlos Santa31776592016-08-17 12:30:56 -0700512 if (HWS_NEEDS_PHYSICAL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000513 ring_setup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -0700514 else
515 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100516
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100517 intel_engine_reset_breadcrumbs(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +0100518
Jiri Kosinaece4a172014-08-07 16:29:53 +0200519 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000520 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200521
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200522 /* Initialize the ring. This must happen _after_ we've cleared the ring
523 * registers with the above sequence (the readback of the HEAD registers
524 * also enforces ordering), otherwise the hw might lose the new ring
525 * register values. */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100526 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
Chris Wilson95468892014-08-07 15:39:54 +0100527
528 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000529 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100530 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000531 engine->name, I915_READ_HEAD(engine));
Chris Wilson821ed7d2016-09-09 14:11:53 +0100532
533 intel_ring_update_space(ring);
534 I915_WRITE_HEAD(engine, ring->head);
535 I915_WRITE_TAIL(engine, ring->tail);
536 (void)I915_READ_TAIL(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100537
Chris Wilson62ae14b2016-10-04 21:11:25 +0100538 I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800539
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800540 /* If the head is still not zero, the ring is dead */
Chris Wilson821ed7d2016-09-09 14:11:53 +0100541 if (intel_wait_for_register_fw(dev_priv, RING_CTL(engine->mmio_base),
542 RING_VALID, RING_VALID,
543 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000544 DRM_ERROR("%s initialization failed "
Chris Wilson821ed7d2016-09-09 14:11:53 +0100545 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000546 engine->name,
547 I915_READ_CTL(engine),
548 I915_READ_CTL(engine) & RING_VALID,
Chris Wilson821ed7d2016-09-09 14:11:53 +0100549 I915_READ_HEAD(engine), ring->head,
550 I915_READ_TAIL(engine), ring->tail,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000551 I915_READ_START(engine),
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100552 i915_ggtt_offset(ring->vma));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200553 ret = -EIO;
554 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800555 }
556
Tomas Elffc0768c2016-03-21 16:26:59 +0000557 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100558
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200559out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200560 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200561
562 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700563}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800564
Chris Wilson821ed7d2016-09-09 14:11:53 +0100565static void reset_ring_common(struct intel_engine_cs *engine,
566 struct drm_i915_gem_request *request)
567{
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000568 /* Try to restore the logical GPU state to match the continuation
569 * of the request queue. If we skip the context/PD restore, then
570 * the next request may try to execute assuming that its context
571 * is valid and loaded on the GPU and so may try to access invalid
572 * memory, prompting repeated GPU hangs.
573 *
574 * If the request was guilty, we still restore the logical state
575 * in case the next request requires it (e.g. the aliasing ppgtt),
576 * but skip over the hung batch.
577 *
578 * If the request was innocent, we try to replay the request with
579 * the restored context.
580 */
581 if (request) {
582 struct drm_i915_private *dev_priv = request->i915;
583 struct intel_context *ce = &request->ctx->engine[engine->id];
584 struct i915_hw_ppgtt *ppgtt;
Chris Wilson821ed7d2016-09-09 14:11:53 +0100585
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000586 /* FIXME consider gen8 reset */
587
588 if (ce->state) {
589 I915_WRITE(CCID,
590 i915_ggtt_offset(ce->state) |
591 BIT(8) /* must be set! */ |
592 CCID_EXTENDED_STATE_SAVE |
593 CCID_EXTENDED_STATE_RESTORE |
594 CCID_EN);
595 }
596
597 ppgtt = request->ctx->ppgtt ?: engine->i915->mm.aliasing_ppgtt;
598 if (ppgtt) {
599 u32 pd_offset = ppgtt->pd.base.ggtt_offset << 10;
600
601 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
602 I915_WRITE(RING_PP_DIR_BASE(engine), pd_offset);
603
604 /* Wait for the PD reload to complete */
605 if (intel_wait_for_register(dev_priv,
606 RING_PP_DIR_BASE(engine),
607 BIT(0), 0,
608 10))
609 DRM_ERROR("Wait for reload of ppgtt page-directory timed out\n");
610
611 ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
612 }
613
614 /* If the rq hung, jump to its breadcrumb and skip the batch */
Chris Wilsonfe085f12017-03-21 10:25:52 +0000615 if (request->fence.error == -EIO)
616 request->ring->head = request->postfix;
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000617 } else {
618 engine->legacy_active_context = NULL;
619 }
Chris Wilson821ed7d2016-09-09 14:11:53 +0100620}
621
John Harrison87531812015-05-29 17:43:44 +0100622static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100623{
624 int ret;
625
John Harrisone2be4fa2015-05-29 17:43:54 +0100626 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100627 if (ret != 0)
628 return ret;
629
Chris Wilson4e50f082016-10-28 13:58:31 +0100630 ret = i915_gem_render_state_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100631 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000632 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100633
Chris Wilsone26e1b92016-01-29 16:49:05 +0000634 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100635}
636
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000637static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800638{
Chris Wilsonc0336662016-05-06 15:40:21 +0100639 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000640 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +0200641 if (ret)
642 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800643
Akash Goel61a563a2014-03-25 18:01:50 +0530644 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100645 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200646 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000647
648 /* We need to disable the AsyncFlip performance optimisations in order
649 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
650 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +0100651 *
Ville Syrjälä2441f872015-06-02 15:37:37 +0300652 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000653 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100654 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000655 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
656
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000657 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +0530658 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +0100659 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000660 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +0000661 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000662
Akash Goel01fa0302014-03-24 23:00:04 +0530663 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +0100664 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000665 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +0530666 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000667 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +0100668
Chris Wilsonc0336662016-05-06 15:40:21 +0100669 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700670 /* From the Sandybridge PRM, volume 1 part 3, page 24:
671 * "If this bit is set, STCunit will have LRA as replacement
672 * policy. [...] This bit must be reset. LRA replacement
673 * policy is not supported."
674 */
675 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200676 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -0800677 }
678
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100679 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200680 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +0000681
Ville Syrjälä035ea402016-07-12 19:24:47 +0300682 if (INTEL_INFO(dev_priv)->gen >= 6)
683 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -0700684
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000685 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800686}
687
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000688static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000689{
Chris Wilsonc0336662016-05-06 15:40:21 +0100690 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700691
Chris Wilson19880c42016-08-15 10:49:05 +0100692 i915_vma_unpin_and_release(&dev_priv->semaphore);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000693}
694
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000695static u32 *gen8_rcs_signal(struct drm_i915_gem_request *req, u32 *cs)
Ben Widawsky3e789982014-06-30 09:53:37 -0700696{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100697 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700698 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +0000699 enum intel_engine_id id;
Ben Widawsky3e789982014-06-30 09:53:37 -0700700
Akash Goel3b3f1652016-10-13 22:44:48 +0530701 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100702 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -0700703 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
704 continue;
705
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000706 *cs++ = GFX_OP_PIPE_CONTROL(6);
707 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_QW_WRITE |
708 PIPE_CONTROL_CS_STALL;
709 *cs++ = lower_32_bits(gtt_offset);
710 *cs++ = upper_32_bits(gtt_offset);
711 *cs++ = req->global_seqno;
712 *cs++ = 0;
713 *cs++ = MI_SEMAPHORE_SIGNAL |
714 MI_SEMAPHORE_TARGET(waiter->hw_id);
715 *cs++ = 0;
Ben Widawsky3e789982014-06-30 09:53:37 -0700716 }
717
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000718 return cs;
Ben Widawsky3e789982014-06-30 09:53:37 -0700719}
720
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000721static u32 *gen8_xcs_signal(struct drm_i915_gem_request *req, u32 *cs)
Ben Widawsky3e789982014-06-30 09:53:37 -0700722{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100723 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700724 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +0000725 enum intel_engine_id id;
Ben Widawsky3e789982014-06-30 09:53:37 -0700726
Akash Goel3b3f1652016-10-13 22:44:48 +0530727 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100728 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -0700729 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
730 continue;
731
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000732 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
733 *cs++ = lower_32_bits(gtt_offset) | MI_FLUSH_DW_USE_GTT;
734 *cs++ = upper_32_bits(gtt_offset);
735 *cs++ = req->global_seqno;
736 *cs++ = MI_SEMAPHORE_SIGNAL |
737 MI_SEMAPHORE_TARGET(waiter->hw_id);
738 *cs++ = 0;
Ben Widawsky3e789982014-06-30 09:53:37 -0700739 }
740
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000741 return cs;
Ben Widawsky3e789982014-06-30 09:53:37 -0700742}
743
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000744static u32 *gen6_signal(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000745{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100746 struct drm_i915_private *dev_priv = req->i915;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100747 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530748 enum intel_engine_id id;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100749 int num_rings = 0;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700750
Akash Goel3b3f1652016-10-13 22:44:48 +0530751 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100752 i915_reg_t mbox_reg;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200753
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100754 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
755 continue;
756
757 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200758 if (i915_mmio_reg_valid(mbox_reg)) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000759 *cs++ = MI_LOAD_REGISTER_IMM(1);
760 *cs++ = i915_mmio_reg_offset(mbox_reg);
761 *cs++ = req->global_seqno;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100762 num_rings++;
Ben Widawsky78325f22014-04-29 14:52:29 -0700763 }
764 }
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100765 if (num_rings & 1)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000766 *cs++ = MI_NOOP;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700767
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000768 return cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000769}
770
Chris Wilsonb0411e72016-08-02 22:50:34 +0100771static void i9xx_submit_request(struct drm_i915_gem_request *request)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000772{
Chris Wilsonb0411e72016-08-02 22:50:34 +0100773 struct drm_i915_private *dev_priv = request->i915;
774
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000775 i915_gem_request_submit(request);
776
Chris Wilsona21ef712017-06-15 14:11:29 +0100777 I915_WRITE_TAIL(request->engine,
778 intel_ring_set_tail(request->ring, request->tail));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100779}
780
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000781static void i9xx_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100782{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000783 *cs++ = MI_STORE_DWORD_INDEX;
784 *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
785 *cs++ = req->global_seqno;
786 *cs++ = MI_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000787
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000788 req->tail = intel_ring_offset(req, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +0100789 assert_ring_tail_valid(req->ring, req->tail);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000790}
791
Chris Wilson98f29e82016-10-28 13:58:51 +0100792static const int i9xx_emit_breadcrumb_sz = 4;
793
Chris Wilsonb0411e72016-08-02 22:50:34 +0100794/**
Chris Wilson9b81d552016-10-28 13:58:50 +0100795 * gen6_sema_emit_breadcrumb - Update the semaphore mailbox registers
Chris Wilsonb0411e72016-08-02 22:50:34 +0100796 *
797 * @request - request to write to the ring
798 *
799 * Update the mailbox registers in the *other* rings with the current seqno.
800 * This acts like a signal in the canonical semaphore.
801 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000802static void gen6_sema_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100803{
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100804 return i9xx_emit_breadcrumb(req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000805 req->engine->semaphore.signal(req, cs));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100806}
807
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100808static void gen8_render_emit_breadcrumb(struct drm_i915_gem_request *req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000809 u32 *cs)
Chris Wilsona58c01a2016-04-29 13:18:21 +0100810{
811 struct intel_engine_cs *engine = req->engine;
Chris Wilsona58c01a2016-04-29 13:18:21 +0100812
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100813 if (engine->semaphore.signal)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000814 cs = engine->semaphore.signal(req, cs);
Chris Wilson9242f972016-08-02 22:50:33 +0100815
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000816 *cs++ = GFX_OP_PIPE_CONTROL(6);
817 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
818 PIPE_CONTROL_QW_WRITE;
819 *cs++ = intel_hws_seqno_address(engine);
820 *cs++ = 0;
821 *cs++ = req->global_seqno;
Chris Wilsona58c01a2016-04-29 13:18:21 +0100822 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000823 *cs++ = 0;
824 *cs++ = MI_USER_INTERRUPT;
825 *cs++ = MI_NOOP;
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100826
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 Wilsona58c01a2016-04-29 13:18:21 +0100829}
830
Chris Wilson98f29e82016-10-28 13:58:51 +0100831static const int gen8_render_emit_breadcrumb_sz = 8;
832
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700833/**
834 * intel_ring_sync - sync the waiter to the signaller on seqno
835 *
836 * @waiter - ring that is waiting
837 * @signaller - ring which has, or will signal
838 * @seqno - seqno which the waiter will block on
839 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700840
841static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100842gen8_ring_sync_to(struct drm_i915_gem_request *req,
843 struct drm_i915_gem_request *signal)
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700844{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100845 struct drm_i915_private *dev_priv = req->i915;
846 u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +0100847 struct i915_hw_ppgtt *ppgtt;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000848 u32 *cs;
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700849
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000850 cs = intel_ring_begin(req, 4);
851 if (IS_ERR(cs))
852 return PTR_ERR(cs);
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700853
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000854 *cs++ = MI_SEMAPHORE_WAIT | MI_SEMAPHORE_GLOBAL_GTT |
855 MI_SEMAPHORE_SAD_GTE_SDD;
856 *cs++ = signal->global_seqno;
857 *cs++ = lower_32_bits(offset);
858 *cs++ = upper_32_bits(offset);
859 intel_ring_advance(req, cs);
Chris Wilson6ef48d72016-04-29 13:18:25 +0100860
861 /* When the !RCS engines idle waiting upon a semaphore, they lose their
862 * pagetables and we must reload them before executing the batch.
863 * We do this on the i915_switch_context() following the wait and
864 * before the dispatch.
865 */
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100866 ppgtt = req->ctx->ppgtt;
867 if (ppgtt && req->engine->id != RCS)
868 ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700869 return 0;
870}
871
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700872static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100873gen6_ring_sync_to(struct drm_i915_gem_request *req,
874 struct drm_i915_gem_request *signal)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000875{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700876 u32 dw1 = MI_SEMAPHORE_MBOX |
877 MI_SEMAPHORE_COMPARE |
878 MI_SEMAPHORE_REGISTER;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100879 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000880 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000881
Chris Wilsonddf07be2016-08-02 22:50:39 +0100882 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
883
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000884 cs = intel_ring_begin(req, 4);
885 if (IS_ERR(cs))
886 return PTR_ERR(cs);
Chris Wilsonddf07be2016-08-02 22:50:39 +0100887
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000888 *cs++ = dw1 | wait_mbox;
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700889 /* Throughout all of the GEM code, seqno passed implies our current
890 * seqno is >= the last seqno executed. However for hardware the
891 * comparison is strictly greater than.
892 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000893 *cs++ = signal->global_seqno - 1;
894 *cs++ = 0;
895 *cs++ = MI_NOOP;
896 intel_ring_advance(req, cs);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000897
898 return 0;
899}
900
Chris Wilsonf8973c22016-07-01 17:23:21 +0100901static void
Dave Gordon38a0f2d2016-07-20 18:16:06 +0100902gen5_seqno_barrier(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000903{
Chris Wilsonf8973c22016-07-01 17:23:21 +0100904 /* MI_STORE are internally buffered by the GPU and not flushed
905 * either by MI_FLUSH or SyncFlush or any other combination of
906 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000907 *
Chris Wilsonf8973c22016-07-01 17:23:21 +0100908 * "Only the submission of the store operation is guaranteed.
909 * The write result will be complete (coherent) some time later
910 * (this is practically a finite period but there is no guaranteed
911 * latency)."
912 *
913 * Empirically, we observe that we need a delay of at least 75us to
914 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000915 */
Chris Wilsonf8973c22016-07-01 17:23:21 +0100916 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000917}
918
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100919static void
920gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100921{
Chris Wilsonc0336662016-05-06 15:40:21 +0100922 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100923
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100924 /* Workaround to force correct ordering between irq and seqno writes on
925 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +0100926 * ACTHD) before reading the status page.
927 *
928 * Note that this effectively stalls the read by the time it takes to
929 * do a memory transaction, which more or less ensures that the write
930 * from the GPU has sufficient time to invalidate the CPU cacheline.
931 * Alternatively we could delay the interrupt from the CS ring to give
932 * the write time to land, but that would incur a delay after every
933 * batch i.e. much more frequent than a delay when waiting for the
934 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100935 *
936 * Also note that to prevent whole machine hangs on gen7, we have to
937 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +0100938 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100939 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100940 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100941 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100942}
943
Chris Wilson31bb59c2016-07-01 17:23:27 +0100944static void
945gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200946{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100947 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +0200948}
949
950static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100951gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200952{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100953 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700954}
955
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800956static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100957i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700958{
Chris Wilsonc0336662016-05-06 15:40:21 +0100959 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700960
Chris Wilson31bb59c2016-07-01 17:23:27 +0100961 dev_priv->irq_mask &= ~engine->irq_enable_mask;
962 I915_WRITE(IMR, dev_priv->irq_mask);
963 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +0100964}
965
966static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100967i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +0100968{
Chris Wilsonc0336662016-05-06 15:40:21 +0100969 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100970
Chris Wilson31bb59c2016-07-01 17:23:27 +0100971 dev_priv->irq_mask |= engine->irq_enable_mask;
972 I915_WRITE(IMR, dev_priv->irq_mask);
973}
974
975static void
976i8xx_irq_enable(struct intel_engine_cs *engine)
977{
978 struct drm_i915_private *dev_priv = engine->i915;
979
980 dev_priv->irq_mask &= ~engine->irq_enable_mask;
981 I915_WRITE16(IMR, dev_priv->irq_mask);
982 POSTING_READ16(RING_IMR(engine->mmio_base));
983}
984
985static void
986i8xx_irq_disable(struct intel_engine_cs *engine)
987{
988 struct drm_i915_private *dev_priv = engine->i915;
989
990 dev_priv->irq_mask |= engine->irq_enable_mask;
991 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100992}
993
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000994static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100995bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800996{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000997 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000998
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000999 cs = intel_ring_begin(req, 2);
1000 if (IS_ERR(cs))
1001 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001002
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001003 *cs++ = MI_FLUSH;
1004 *cs++ = MI_NOOP;
1005 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001006 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001007}
1008
Chris Wilson0f468322011-01-04 17:35:21 +00001009static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001010gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001011{
Chris Wilsonc0336662016-05-06 15:40:21 +01001012 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001013
Chris Wilson61ff75a2016-07-01 17:23:28 +01001014 I915_WRITE_IMR(engine,
1015 ~(engine->irq_enable_mask |
1016 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001017 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001018}
1019
1020static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001021gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001022{
Chris Wilsonc0336662016-05-06 15:40:21 +01001023 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001024
Chris Wilson61ff75a2016-07-01 17:23:28 +01001025 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001026 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001027}
1028
1029static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001030hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001031{
Chris Wilsonc0336662016-05-06 15:40:21 +01001032 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001033
Chris Wilson31bb59c2016-07-01 17:23:27 +01001034 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
Akash Goelf4e9af42016-10-12 21:54:30 +05301035 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001036}
1037
1038static void
1039hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1040{
1041 struct drm_i915_private *dev_priv = engine->i915;
1042
1043 I915_WRITE_IMR(engine, ~0);
Akash Goelf4e9af42016-10-12 21:54:30 +05301044 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001045}
1046
1047static void
1048gen8_irq_enable(struct intel_engine_cs *engine)
1049{
1050 struct drm_i915_private *dev_priv = engine->i915;
1051
Chris Wilson61ff75a2016-07-01 17:23:28 +01001052 I915_WRITE_IMR(engine,
1053 ~(engine->irq_enable_mask |
1054 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001055 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1056}
1057
1058static void
1059gen8_irq_disable(struct intel_engine_cs *engine)
1060{
1061 struct drm_i915_private *dev_priv = engine->i915;
1062
Chris Wilson61ff75a2016-07-01 17:23:28 +01001063 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001064}
1065
Zou Nan haid1b851f2010-05-21 09:08:57 +08001066static int
Chris Wilson803688b2016-08-02 22:50:27 +01001067i965_emit_bb_start(struct drm_i915_gem_request *req,
1068 u64 offset, u32 length,
1069 unsigned int dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001070{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001071 u32 *cs;
Chris Wilson78501ea2010-10-27 12:18:21 +01001072
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001073 cs = intel_ring_begin(req, 2);
1074 if (IS_ERR(cs))
1075 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001076
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001077 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
1078 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
1079 *cs++ = offset;
1080 intel_ring_advance(req, cs);
Chris Wilson78501ea2010-10-27 12:18:21 +01001081
Zou Nan haid1b851f2010-05-21 09:08:57 +08001082 return 0;
1083}
1084
Daniel Vetterb45305f2012-12-17 16:21:27 +01001085/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1086#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001087#define I830_TLB_ENTRIES (2)
1088#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001089static int
Chris Wilson803688b2016-08-02 22:50:27 +01001090i830_emit_bb_start(struct drm_i915_gem_request *req,
1091 u64 offset, u32 len,
1092 unsigned int dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001093{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001094 u32 *cs, cs_offset = i915_ggtt_offset(req->engine->scratch);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001095
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001096 cs = intel_ring_begin(req, 6);
1097 if (IS_ERR(cs))
1098 return PTR_ERR(cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001099
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001100 /* Evict the invalid PTE TLBs */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001101 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
1102 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
1103 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
1104 *cs++ = cs_offset;
1105 *cs++ = 0xdeadbeef;
1106 *cs++ = MI_NOOP;
1107 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001108
John Harrison8e004ef2015-02-13 11:48:10 +00001109 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001110 if (len > I830_BATCH_LIMIT)
1111 return -ENOSPC;
1112
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001113 cs = intel_ring_begin(req, 6 + 2);
1114 if (IS_ERR(cs))
1115 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001116
1117 /* Blit the batch (which has now all relocs applied) to the
1118 * stable batch scratch bo area (so that the CS never
1119 * stumbles over its tlb invalidation bug) ...
1120 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001121 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
1122 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
1123 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
1124 *cs++ = cs_offset;
1125 *cs++ = 4096;
1126 *cs++ = offset;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001127
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001128 *cs++ = MI_FLUSH;
1129 *cs++ = MI_NOOP;
1130 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001131
1132 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001133 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001134 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001135
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001136 cs = intel_ring_begin(req, 2);
1137 if (IS_ERR(cs))
1138 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001139
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001140 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1141 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1142 MI_BATCH_NON_SECURE);
1143 intel_ring_advance(req, cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001144
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001145 return 0;
1146}
1147
1148static int
Chris Wilson803688b2016-08-02 22:50:27 +01001149i915_emit_bb_start(struct drm_i915_gem_request *req,
1150 u64 offset, u32 len,
1151 unsigned int dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001152{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001153 u32 *cs;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001154
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001155 cs = intel_ring_begin(req, 2);
1156 if (IS_ERR(cs))
1157 return PTR_ERR(cs);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001158
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001159 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1160 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1161 MI_BATCH_NON_SECURE);
1162 intel_ring_advance(req, cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001163
Eric Anholt62fdfea2010-05-21 13:26:39 -07001164 return 0;
1165}
1166
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001167static void cleanup_phys_status_page(struct intel_engine_cs *engine)
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001168{
Chris Wilsonc0336662016-05-06 15:40:21 +01001169 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001170
1171 if (!dev_priv->status_page_dmah)
1172 return;
1173
Chris Wilson91c8a322016-07-05 10:40:23 +01001174 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001175 engine->status_page.page_addr = NULL;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001176}
1177
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001178static void cleanup_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001179{
Chris Wilson57e88532016-08-15 10:48:57 +01001180 struct i915_vma *vma;
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001181 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001182
Chris Wilson57e88532016-08-15 10:48:57 +01001183 vma = fetch_and_zero(&engine->status_page.vma);
1184 if (!vma)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001185 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001186
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001187 obj = vma->obj;
1188
Chris Wilson57e88532016-08-15 10:48:57 +01001189 i915_vma_unpin(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001190 i915_vma_close(vma);
1191
1192 i915_gem_object_unpin_map(obj);
1193 __i915_gem_object_release_unless_active(obj);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001194}
1195
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001196static int init_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001197{
Chris Wilson57e88532016-08-15 10:48:57 +01001198 struct drm_i915_gem_object *obj;
1199 struct i915_vma *vma;
1200 unsigned int flags;
Chris Wilson920cf412016-10-28 13:58:30 +01001201 void *vaddr;
Chris Wilson57e88532016-08-15 10:48:57 +01001202 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001203
Chris Wilsonf51455d2017-01-10 14:47:34 +00001204 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
Chris Wilson57e88532016-08-15 10:48:57 +01001205 if (IS_ERR(obj)) {
1206 DRM_ERROR("Failed to allocate status page\n");
1207 return PTR_ERR(obj);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001208 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001209
Chris Wilson57e88532016-08-15 10:48:57 +01001210 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1211 if (ret)
1212 goto err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001213
Chris Wilsona01cb372017-01-16 15:21:30 +00001214 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson57e88532016-08-15 10:48:57 +01001215 if (IS_ERR(vma)) {
1216 ret = PTR_ERR(vma);
1217 goto err;
1218 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001219
Chris Wilson57e88532016-08-15 10:48:57 +01001220 flags = PIN_GLOBAL;
1221 if (!HAS_LLC(engine->i915))
1222 /* On g33, we cannot place HWS above 256MiB, so
1223 * restrict its pinning to the low mappable arena.
1224 * Though this restriction is not documented for
1225 * gen4, gen5, or byt, they also behave similarly
1226 * and hang if the HWS is placed at the top of the
1227 * GTT. To generalise, it appears that all !llc
1228 * platforms have issues with us placing the HWS
1229 * above the mappable region (even though we never
1230 * actualy map it).
1231 */
1232 flags |= PIN_MAPPABLE;
1233 ret = i915_vma_pin(vma, 0, 4096, flags);
1234 if (ret)
1235 goto err;
1236
Chris Wilson920cf412016-10-28 13:58:30 +01001237 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1238 if (IS_ERR(vaddr)) {
1239 ret = PTR_ERR(vaddr);
1240 goto err_unpin;
1241 }
1242
Chris Wilson57e88532016-08-15 10:48:57 +01001243 engine->status_page.vma = vma;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001244 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
Chris Wilsonf51455d2017-01-10 14:47:34 +00001245 engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
Chris Wilson57e88532016-08-15 10:48:57 +01001246
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001247 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1248 engine->name, i915_ggtt_offset(vma));
Eric Anholt62fdfea2010-05-21 13:26:39 -07001249 return 0;
Chris Wilson57e88532016-08-15 10:48:57 +01001250
Chris Wilson920cf412016-10-28 13:58:30 +01001251err_unpin:
1252 i915_vma_unpin(vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001253err:
1254 i915_gem_object_put(obj);
1255 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001256}
1257
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001258static int init_phys_status_page(struct intel_engine_cs *engine)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001259{
Chris Wilsonc0336662016-05-06 15:40:21 +01001260 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001261
Chris Wilson57e88532016-08-15 10:48:57 +01001262 dev_priv->status_page_dmah =
1263 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
1264 if (!dev_priv->status_page_dmah)
1265 return -ENOMEM;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001266
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001267 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1268 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001269
1270 return 0;
1271}
1272
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001273int intel_ring_pin(struct intel_ring *ring, unsigned int offset_bias)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001274{
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001275 unsigned int flags;
Chris Wilson9d808412016-08-18 17:16:56 +01001276 enum i915_map_type map;
Chris Wilson57e88532016-08-15 10:48:57 +01001277 struct i915_vma *vma = ring->vma;
Dave Gordon83052162016-04-12 14:46:16 +01001278 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001279 int ret;
1280
Chris Wilson57e88532016-08-15 10:48:57 +01001281 GEM_BUG_ON(ring->vaddr);
1282
Chris Wilson9d808412016-08-18 17:16:56 +01001283 map = HAS_LLC(ring->engine->i915) ? I915_MAP_WB : I915_MAP_WC;
1284
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001285 flags = PIN_GLOBAL;
1286 if (offset_bias)
1287 flags |= PIN_OFFSET_BIAS | offset_bias;
Chris Wilson9d808412016-08-18 17:16:56 +01001288 if (vma->obj->stolen)
Chris Wilson57e88532016-08-15 10:48:57 +01001289 flags |= PIN_MAPPABLE;
1290
1291 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilson9d808412016-08-18 17:16:56 +01001292 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
Chris Wilson57e88532016-08-15 10:48:57 +01001293 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1294 else
1295 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1296 if (unlikely(ret))
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001297 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001298 }
1299
Chris Wilson57e88532016-08-15 10:48:57 +01001300 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1301 if (unlikely(ret))
1302 return ret;
1303
Chris Wilson9d808412016-08-18 17:16:56 +01001304 if (i915_vma_is_map_and_fenceable(vma))
Chris Wilson57e88532016-08-15 10:48:57 +01001305 addr = (void __force *)i915_vma_pin_iomap(vma);
1306 else
Chris Wilson9d808412016-08-18 17:16:56 +01001307 addr = i915_gem_object_pin_map(vma->obj, map);
Chris Wilson57e88532016-08-15 10:48:57 +01001308 if (IS_ERR(addr))
1309 goto err;
1310
Chris Wilson32c04f12016-08-02 22:50:22 +01001311 ring->vaddr = addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001312 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01001313
Chris Wilson57e88532016-08-15 10:48:57 +01001314err:
1315 i915_vma_unpin(vma);
1316 return PTR_ERR(addr);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001317}
1318
Chris Wilsona21ef712017-06-15 14:11:29 +01001319void intel_ring_reset(struct intel_ring *ring, u32 tail)
1320{
1321 GEM_BUG_ON(!list_empty(&ring->request_list));
1322 ring->tail = tail;
1323 ring->head = tail;
1324 ring->emit = tail;
1325 intel_ring_update_space(ring);
1326}
1327
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001328void intel_ring_unpin(struct intel_ring *ring)
1329{
1330 GEM_BUG_ON(!ring->vma);
1331 GEM_BUG_ON(!ring->vaddr);
1332
Chris Wilsona21ef712017-06-15 14:11:29 +01001333 /* Discard any unused bytes beyond that submitted to hw. */
1334 intel_ring_reset(ring, ring->tail);
1335
Chris Wilson9d808412016-08-18 17:16:56 +01001336 if (i915_vma_is_map_and_fenceable(ring->vma))
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001337 i915_vma_unpin_iomap(ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001338 else
1339 i915_gem_object_unpin_map(ring->vma->obj);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001340 ring->vaddr = NULL;
1341
Chris Wilson57e88532016-08-15 10:48:57 +01001342 i915_vma_unpin(ring->vma);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001343}
1344
Chris Wilson57e88532016-08-15 10:48:57 +01001345static struct i915_vma *
1346intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
Oscar Mateo2919d292014-07-03 16:28:02 +01001347{
Chris Wilsone3efda42014-04-09 09:19:41 +01001348 struct drm_i915_gem_object *obj;
Chris Wilson57e88532016-08-15 10:48:57 +01001349 struct i915_vma *vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001350
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00001351 obj = i915_gem_object_create_stolen(dev_priv, size);
Chris Wilsonc58b7352016-08-18 17:16:57 +01001352 if (!obj)
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00001353 obj = i915_gem_object_create(dev_priv, size);
Chris Wilson57e88532016-08-15 10:48:57 +01001354 if (IS_ERR(obj))
1355 return ERR_CAST(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001356
Akash Goel24f3a8c2014-06-17 10:59:42 +05301357 /* mark ring buffers as read-only from GPU side by default */
1358 obj->gt_ro = 1;
1359
Chris Wilsona01cb372017-01-16 15:21:30 +00001360 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson57e88532016-08-15 10:48:57 +01001361 if (IS_ERR(vma))
1362 goto err;
Chris Wilsone3efda42014-04-09 09:19:41 +01001363
Chris Wilson57e88532016-08-15 10:48:57 +01001364 return vma;
1365
1366err:
1367 i915_gem_object_put(obj);
1368 return vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001369}
1370
Chris Wilson7e37f882016-08-02 22:50:21 +01001371struct intel_ring *
1372intel_engine_create_ring(struct intel_engine_cs *engine, int size)
Chris Wilson01101fa2015-09-03 13:01:39 +01001373{
Chris Wilson7e37f882016-08-02 22:50:21 +01001374 struct intel_ring *ring;
Chris Wilson57e88532016-08-15 10:48:57 +01001375 struct i915_vma *vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001376
Chris Wilson8f942012016-08-02 22:50:30 +01001377 GEM_BUG_ON(!is_power_of_2(size));
Chris Wilson62ae14b2016-10-04 21:11:25 +01001378 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
Chris Wilson8f942012016-08-02 22:50:30 +01001379
Chris Wilson01101fa2015-09-03 13:01:39 +01001380 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson57e88532016-08-15 10:48:57 +01001381 if (!ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001382 return ERR_PTR(-ENOMEM);
1383
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001384 ring->engine = engine;
Chris Wilson01101fa2015-09-03 13:01:39 +01001385
Chris Wilson675d9ad2016-08-04 07:52:36 +01001386 INIT_LIST_HEAD(&ring->request_list);
1387
Chris Wilson01101fa2015-09-03 13:01:39 +01001388 ring->size = size;
1389 /* Workaround an erratum on the i830 which causes a hang if
1390 * the TAIL pointer points to within the last 2 cachelines
1391 * of the buffer.
1392 */
1393 ring->effective_size = size;
Jani Nikula2a307c22016-11-30 17:43:04 +02001394 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01001395 ring->effective_size -= 2 * CACHELINE_BYTES;
1396
Chris Wilson01101fa2015-09-03 13:01:39 +01001397 intel_ring_update_space(ring);
1398
Chris Wilson57e88532016-08-15 10:48:57 +01001399 vma = intel_ring_create_vma(engine->i915, size);
1400 if (IS_ERR(vma)) {
Chris Wilson01101fa2015-09-03 13:01:39 +01001401 kfree(ring);
Chris Wilson57e88532016-08-15 10:48:57 +01001402 return ERR_CAST(vma);
Chris Wilson01101fa2015-09-03 13:01:39 +01001403 }
Chris Wilson57e88532016-08-15 10:48:57 +01001404 ring->vma = vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001405
1406 return ring;
1407}
1408
1409void
Chris Wilson7e37f882016-08-02 22:50:21 +01001410intel_ring_free(struct intel_ring *ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001411{
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001412 struct drm_i915_gem_object *obj = ring->vma->obj;
1413
1414 i915_vma_close(ring->vma);
1415 __i915_gem_object_release_unless_active(obj);
1416
Chris Wilson01101fa2015-09-03 13:01:39 +01001417 kfree(ring);
1418}
1419
Chris Wilson72b72ae2017-02-10 10:14:22 +00001420static int context_pin(struct i915_gem_context *ctx)
Chris Wilsone8a9c582016-12-18 15:37:20 +00001421{
1422 struct i915_vma *vma = ctx->engine[RCS].state;
1423 int ret;
1424
1425 /* Clear this page out of any CPU caches for coherent swap-in/out.
1426 * We only want to do this on the first bind so that we do not stall
1427 * on an active context (which by nature is already on the GPU).
1428 */
1429 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1430 ret = i915_gem_object_set_to_gtt_domain(vma->obj, false);
1431 if (ret)
1432 return ret;
1433 }
1434
Chris Wilsonafeddf52017-02-27 13:59:13 +00001435 return i915_vma_pin(vma, 0, I915_GTT_MIN_ALIGNMENT,
1436 PIN_GLOBAL | PIN_HIGH);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001437}
1438
1439static int intel_ring_context_pin(struct intel_engine_cs *engine,
1440 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001441{
1442 struct intel_context *ce = &ctx->engine[engine->id];
1443 int ret;
1444
Chris Wilson91c8a322016-07-05 10:40:23 +01001445 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001446
1447 if (ce->pin_count++)
1448 return 0;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001449 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson0cb26a82016-06-24 14:55:53 +01001450
1451 if (ce->state) {
Chris Wilson72b72ae2017-02-10 10:14:22 +00001452 ret = context_pin(ctx);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001453 if (ret)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001454 goto error;
Chris Wilson5d4bac52017-03-22 20:59:30 +00001455
1456 ce->state->obj->mm.dirty = true;
Chris Wilson0cb26a82016-06-24 14:55:53 +01001457 }
1458
Chris Wilsonc7c3c072016-06-24 14:55:54 +01001459 /* The kernel context is only used as a placeholder for flushing the
1460 * active context. It is never used for submitting user rendering and
1461 * as such never requires the golden render context, and so we can skip
1462 * emitting it when we switch to the kernel context. This is required
1463 * as during eviction we cannot allocate and pin the renderstate in
1464 * order to initialise the context.
1465 */
Chris Wilson984ff29f2017-01-06 15:20:13 +00001466 if (i915_gem_context_is_kernel(ctx))
Chris Wilsonc7c3c072016-06-24 14:55:54 +01001467 ce->initialised = true;
1468
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001469 i915_gem_context_get(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001470 return 0;
1471
1472error:
1473 ce->pin_count = 0;
1474 return ret;
1475}
1476
Chris Wilsone8a9c582016-12-18 15:37:20 +00001477static void intel_ring_context_unpin(struct intel_engine_cs *engine,
1478 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001479{
1480 struct intel_context *ce = &ctx->engine[engine->id];
1481
Chris Wilson91c8a322016-07-05 10:40:23 +01001482 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001483 GEM_BUG_ON(ce->pin_count == 0);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001484
1485 if (--ce->pin_count)
1486 return;
1487
1488 if (ce->state)
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001489 i915_vma_unpin(ce->state);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001490
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001491 i915_gem_context_put(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001492}
1493
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001494static int intel_init_ring_buffer(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001495{
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001496 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson32c04f12016-08-02 22:50:22 +01001497 struct intel_ring *ring;
Chris Wilsondd785e32010-08-07 11:01:34 +01001498 int ret;
1499
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001500 WARN_ON(engine->buffer);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001501
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001502 intel_engine_setup_common(engine);
1503
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001504 ret = intel_engine_init_common(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +01001505 if (ret)
1506 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001507
Chris Wilson32c04f12016-08-02 22:50:22 +01001508 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
1509 if (IS_ERR(ring)) {
1510 ret = PTR_ERR(ring);
Dave Gordonb0366a52015-12-08 15:02:36 +00001511 goto error;
1512 }
Chris Wilson01101fa2015-09-03 13:01:39 +01001513
Carlos Santa31776592016-08-17 12:30:56 -07001514 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
1515 WARN_ON(engine->id != RCS);
1516 ret = init_phys_status_page(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001517 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001518 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001519 } else {
Carlos Santa31776592016-08-17 12:30:56 -07001520 ret = init_status_page(engine);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001521 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001522 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001523 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001524
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001525 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
Chris Wilsonf51455d2017-01-10 14:47:34 +00001526 ret = intel_ring_pin(ring, I915_GTT_PAGE_SIZE);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001527 if (ret) {
Chris Wilson57e88532016-08-15 10:48:57 +01001528 intel_ring_free(ring);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001529 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001530 }
Chris Wilson57e88532016-08-15 10:48:57 +01001531 engine->buffer = ring;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001532
Oscar Mateo8ee14972014-05-22 14:13:34 +01001533 return 0;
1534
1535error:
Chris Wilson7e37f882016-08-02 22:50:21 +01001536 intel_engine_cleanup(engine);
Oscar Mateo8ee14972014-05-22 14:13:34 +01001537 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001538}
1539
Chris Wilson7e37f882016-08-02 22:50:21 +01001540void intel_engine_cleanup(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001541{
John Harrison6402c332014-10-31 12:00:26 +00001542 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01001543
Chris Wilsonc0336662016-05-06 15:40:21 +01001544 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001545
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001546 if (engine->buffer) {
Chris Wilson21a2c582016-08-15 10:49:11 +01001547 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
1548 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01001549
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001550 intel_ring_unpin(engine->buffer);
Chris Wilson7e37f882016-08-02 22:50:21 +01001551 intel_ring_free(engine->buffer);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001552 engine->buffer = NULL;
Dave Gordonb0366a52015-12-08 15:02:36 +00001553 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001554
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001555 if (engine->cleanup)
1556 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08001557
Carlos Santa31776592016-08-17 12:30:56 -07001558 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001559 WARN_ON(engine->id != RCS);
1560 cleanup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -07001561 } else {
1562 cleanup_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001563 }
Brad Volkin44e895a2014-05-10 14:10:43 -07001564
Chris Wilson96a945a2016-08-03 13:19:16 +01001565 intel_engine_cleanup_common(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001566
Chris Wilsonc0336662016-05-06 15:40:21 +01001567 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301568 dev_priv->engine[engine->id] = NULL;
1569 kfree(engine);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001570}
1571
Chris Wilson821ed7d2016-09-09 14:11:53 +01001572void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
1573{
1574 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301575 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001576
Chris Wilsona21ef712017-06-15 14:11:29 +01001577 /* Restart from the beginning of the rings for convenience */
Chris Wilsonfe085f12017-03-21 10:25:52 +00001578 for_each_engine(engine, dev_priv, id)
Chris Wilsona21ef712017-06-15 14:11:29 +01001579 intel_ring_reset(engine->buffer, 0);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001580}
1581
Chris Wilsonf73e7392016-12-18 15:37:24 +00001582static int ring_request_alloc(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00001583{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001584 u32 *cs;
Chris Wilson63103462016-04-28 09:56:49 +01001585
Chris Wilsone8a9c582016-12-18 15:37:20 +00001586 GEM_BUG_ON(!request->ctx->engine[request->engine->id].pin_count);
1587
Chris Wilson63103462016-04-28 09:56:49 +01001588 /* Flush enough space to reduce the likelihood of waiting after
1589 * we start building the request - in which case we will just
1590 * have to repeat work.
1591 */
Chris Wilsona0442462016-04-29 09:07:05 +01001592 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001593
Chris Wilsone8a9c582016-12-18 15:37:20 +00001594 GEM_BUG_ON(!request->engine->buffer);
Chris Wilson1dae2df2016-08-02 22:50:19 +01001595 request->ring = request->engine->buffer;
Chris Wilson63103462016-04-28 09:56:49 +01001596
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001597 cs = intel_ring_begin(request, 0);
1598 if (IS_ERR(cs))
1599 return PTR_ERR(cs);
Chris Wilson63103462016-04-28 09:56:49 +01001600
Chris Wilsona0442462016-04-29 09:07:05 +01001601 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001602 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00001603}
1604
Chris Wilson987046a2016-04-28 09:56:46 +01001605static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001606{
Chris Wilson7e37f882016-08-02 22:50:21 +01001607 struct intel_ring *ring = req->ring;
Chris Wilson987046a2016-04-28 09:56:46 +01001608 struct drm_i915_gem_request *target;
Chris Wilsone95433c2016-10-28 13:58:27 +01001609 long timeout;
1610
1611 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson987046a2016-04-28 09:56:46 +01001612
Chris Wilson1dae2df2016-08-02 22:50:19 +01001613 intel_ring_update_space(ring);
1614 if (ring->space >= bytes)
Chris Wilson987046a2016-04-28 09:56:46 +01001615 return 0;
1616
1617 /*
1618 * Space is reserved in the ringbuffer for finalising the request,
1619 * as that cannot be allowed to fail. During request finalisation,
1620 * reserved_space is set to 0 to stop the overallocation and the
1621 * assumption is that then we never need to wait (which has the
1622 * risk of failing with EINTR).
1623 *
1624 * See also i915_gem_request_alloc() and i915_add_request().
1625 */
Chris Wilson0251a962016-04-28 09:56:47 +01001626 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01001627
Chris Wilson675d9ad2016-08-04 07:52:36 +01001628 list_for_each_entry(target, &ring->request_list, ring_link) {
Chris Wilson987046a2016-04-28 09:56:46 +01001629 unsigned space;
1630
Chris Wilson987046a2016-04-28 09:56:46 +01001631 /* Would completion of this request free enough space? */
Chris Wilsona21ef712017-06-15 14:11:29 +01001632 space = __intel_ring_space(target->postfix, ring->emit,
Chris Wilson1dae2df2016-08-02 22:50:19 +01001633 ring->size);
Chris Wilson987046a2016-04-28 09:56:46 +01001634 if (space >= bytes)
1635 break;
1636 }
1637
Chris Wilson675d9ad2016-08-04 07:52:36 +01001638 if (WARN_ON(&target->ring_link == &ring->request_list))
Chris Wilson987046a2016-04-28 09:56:46 +01001639 return -ENOSPC;
1640
Chris Wilsone95433c2016-10-28 13:58:27 +01001641 timeout = i915_wait_request(target,
1642 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1643 MAX_SCHEDULE_TIMEOUT);
1644 if (timeout < 0)
1645 return timeout;
Chris Wilson7da844c2016-08-04 07:52:38 +01001646
Chris Wilson7da844c2016-08-04 07:52:38 +01001647 i915_gem_request_retire_upto(target);
1648
1649 intel_ring_update_space(ring);
1650 GEM_BUG_ON(ring->space < bytes);
1651 return 0;
Chris Wilson987046a2016-04-28 09:56:46 +01001652}
1653
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001654u32 *intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
Chris Wilson987046a2016-04-28 09:56:46 +01001655{
Chris Wilson7e37f882016-08-02 22:50:21 +01001656 struct intel_ring *ring = req->ring;
Chris Wilsona21ef712017-06-15 14:11:29 +01001657 int remain_actual = ring->size - ring->emit;
1658 int remain_usable = ring->effective_size - ring->emit;
Chris Wilson987046a2016-04-28 09:56:46 +01001659 int bytes = num_dwords * sizeof(u32);
1660 int total_bytes, wait_bytes;
John Harrison79bbcc22015-06-30 12:40:55 +01001661 bool need_wrap = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001662 u32 *cs;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001663
Chris Wilson0251a962016-04-28 09:56:47 +01001664 total_bytes = bytes + req->reserved_space;
John Harrison29b1b412015-06-18 13:10:09 +01001665
John Harrison79bbcc22015-06-30 12:40:55 +01001666 if (unlikely(bytes > remain_usable)) {
1667 /*
1668 * Not enough space for the basic request. So need to flush
1669 * out the remainder and then wait for base + reserved.
1670 */
1671 wait_bytes = remain_actual + total_bytes;
1672 need_wrap = true;
Chris Wilson987046a2016-04-28 09:56:46 +01001673 } else if (unlikely(total_bytes > remain_usable)) {
1674 /*
1675 * The base request will fit but the reserved space
1676 * falls off the end. So we don't need an immediate wrap
1677 * and only need to effectively wait for the reserved
1678 * size space from the start of ringbuffer.
1679 */
Chris Wilson0251a962016-04-28 09:56:47 +01001680 wait_bytes = remain_actual + req->reserved_space;
John Harrison79bbcc22015-06-30 12:40:55 +01001681 } else {
Chris Wilson987046a2016-04-28 09:56:46 +01001682 /* No wrapping required, just waiting. */
1683 wait_bytes = total_bytes;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001684 }
1685
Chris Wilson1dae2df2016-08-02 22:50:19 +01001686 if (wait_bytes > ring->space) {
Chris Wilson987046a2016-04-28 09:56:46 +01001687 int ret = wait_for_space(req, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001688 if (unlikely(ret))
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001689 return ERR_PTR(ret);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001690 }
1691
Chris Wilson987046a2016-04-28 09:56:46 +01001692 if (unlikely(need_wrap)) {
Chris Wilson1dae2df2016-08-02 22:50:19 +01001693 GEM_BUG_ON(remain_actual > ring->space);
Chris Wilsona21ef712017-06-15 14:11:29 +01001694 GEM_BUG_ON(ring->emit + remain_actual > ring->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001695
Chris Wilson987046a2016-04-28 09:56:46 +01001696 /* Fill the tail with MI_NOOP */
Chris Wilsona21ef712017-06-15 14:11:29 +01001697 memset(ring->vaddr + ring->emit, 0, remain_actual);
1698 ring->emit = 0;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001699 ring->space -= remain_actual;
Chris Wilson987046a2016-04-28 09:56:46 +01001700 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001701
Chris Wilsona21ef712017-06-15 14:11:29 +01001702 GEM_BUG_ON(ring->emit > ring->size - bytes);
1703 cs = ring->vaddr + ring->emit;
1704 ring->emit += bytes;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001705 ring->space -= bytes;
1706 GEM_BUG_ON(ring->space < 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001707
1708 return cs;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001709}
1710
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001711/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01001712int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001713{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001714 int num_dwords =
Chris Wilsona21ef712017-06-15 14:11:29 +01001715 (req->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001716 u32 *cs;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001717
1718 if (num_dwords == 0)
1719 return 0;
1720
Chris Wilson18393f62014-04-09 09:19:40 +01001721 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001722 cs = intel_ring_begin(req, num_dwords);
1723 if (IS_ERR(cs))
1724 return PTR_ERR(cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001725
1726 while (num_dwords--)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001727 *cs++ = MI_NOOP;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001728
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001729 intel_ring_advance(req, cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001730
1731 return 0;
1732}
1733
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001734static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001735{
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001736 struct drm_i915_private *dev_priv = request->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001737
Chris Wilson76f84212016-06-30 15:33:45 +01001738 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
1739
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001740 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001741
Chris Wilson12f55812012-07-05 17:14:01 +01001742 /* Disable notification that the ring is IDLE. The GT
1743 * will then assume that it is busy and bring it out of rc6.
1744 */
Chris Wilson76f84212016-06-30 15:33:45 +01001745 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1746 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01001747
1748 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01001749 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01001750
1751 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson76f84212016-06-30 15:33:45 +01001752 if (intel_wait_for_register_fw(dev_priv,
1753 GEN6_BSD_SLEEP_PSMI_CONTROL,
1754 GEN6_BSD_SLEEP_INDICATOR,
1755 0,
1756 50))
Chris Wilson12f55812012-07-05 17:14:01 +01001757 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001758
Chris Wilson12f55812012-07-05 17:14:01 +01001759 /* Now that the ring is fully powered up, update the tail */
Chris Wilsonb0411e72016-08-02 22:50:34 +01001760 i9xx_submit_request(request);
Chris Wilson12f55812012-07-05 17:14:01 +01001761
1762 /* Let the ring send IDLE messages to the GT again,
1763 * and so let it sleep to conserve power when idle.
1764 */
Chris Wilson76f84212016-06-30 15:33:45 +01001765 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1766 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1767
1768 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001769}
1770
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001771static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001772{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001773 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001774
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001775 cs = intel_ring_begin(req, 4);
1776 if (IS_ERR(cs))
1777 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001778
Chris Wilson71a77e02011-02-02 12:13:49 +00001779 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01001780 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001781 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001782
1783 /* We always require a command barrier so that subsequent
1784 * commands, such as breadcrumb interrupts, are strictly ordered
1785 * wrt the contents of the write cache being flushed to memory
1786 * (and thus being coherent from the CPU).
1787 */
1788 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1789
Jesse Barnes9a289772012-10-26 09:42:42 -07001790 /*
1791 * Bspec vol 1c.5 - video engine command streamer:
1792 * "If ENABLED, all TLBs will be invalidated once the flush
1793 * operation is complete. This bit is only valid when the
1794 * Post-Sync Operation field is a value of 1h or 3h."
1795 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001796 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001797 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1798
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001799 *cs++ = cmd;
1800 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilsonc0336662016-05-06 15:40:21 +01001801 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001802 *cs++ = 0; /* upper addr */
1803 *cs++ = 0; /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001804 } else {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001805 *cs++ = 0;
1806 *cs++ = MI_NOOP;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001807 }
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001808 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001809 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001810}
1811
1812static int
Chris Wilson803688b2016-08-02 22:50:27 +01001813gen8_emit_bb_start(struct drm_i915_gem_request *req,
1814 u64 offset, u32 len,
1815 unsigned int dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001816{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001817 bool ppgtt = USES_PPGTT(req->i915) &&
John Harrison8e004ef2015-02-13 11:48:10 +00001818 !(dispatch_flags & I915_DISPATCH_SECURE);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001819 u32 *cs;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001820
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001821 cs = intel_ring_begin(req, 4);
1822 if (IS_ERR(cs))
1823 return PTR_ERR(cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001824
1825 /* FIXME(BDW): Address space and security selectors. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001826 *cs++ = MI_BATCH_BUFFER_START_GEN8 | (ppgtt << 8) | (dispatch_flags &
1827 I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
1828 *cs++ = lower_32_bits(offset);
1829 *cs++ = upper_32_bits(offset);
1830 *cs++ = MI_NOOP;
1831 intel_ring_advance(req, cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001832
1833 return 0;
1834}
1835
1836static int
Chris Wilson803688b2016-08-02 22:50:27 +01001837hsw_emit_bb_start(struct drm_i915_gem_request *req,
1838 u64 offset, u32 len,
1839 unsigned int dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001840{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001841 u32 *cs;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001842
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001843 cs = intel_ring_begin(req, 2);
1844 if (IS_ERR(cs))
1845 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001846
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001847 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1848 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
1849 (dispatch_flags & I915_DISPATCH_RS ?
1850 MI_BATCH_RESOURCE_STREAMER : 0);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001851 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001852 *cs++ = offset;
1853 intel_ring_advance(req, cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001854
1855 return 0;
1856}
1857
1858static int
Chris Wilson803688b2016-08-02 22:50:27 +01001859gen6_emit_bb_start(struct drm_i915_gem_request *req,
1860 u64 offset, u32 len,
1861 unsigned int dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001862{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001863 u32 *cs;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001864
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001865 cs = intel_ring_begin(req, 2);
1866 if (IS_ERR(cs))
1867 return PTR_ERR(cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001868
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001869 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1870 0 : MI_BATCH_NON_SECURE_I965);
Akshay Joshi0206e352011-08-16 15:34:10 -04001871 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001872 *cs++ = offset;
1873 intel_ring_advance(req, cs);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001874
Akshay Joshi0206e352011-08-16 15:34:10 -04001875 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001876}
1877
Chris Wilson549f7362010-10-19 11:19:32 +01001878/* Blitter support (SandyBridge+) */
1879
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001880static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan hai8d192152010-11-02 16:31:01 +08001881{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001882 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001883
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001884 cs = intel_ring_begin(req, 4);
1885 if (IS_ERR(cs))
1886 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001887
Chris Wilson71a77e02011-02-02 12:13:49 +00001888 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01001889 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001890 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001891
1892 /* We always require a command barrier so that subsequent
1893 * commands, such as breadcrumb interrupts, are strictly ordered
1894 * wrt the contents of the write cache being flushed to memory
1895 * (and thus being coherent from the CPU).
1896 */
1897 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1898
Jesse Barnes9a289772012-10-26 09:42:42 -07001899 /*
1900 * Bspec vol 1c.3 - blitter engine command streamer:
1901 * "If ENABLED, all TLBs will be invalidated once the flush
1902 * operation is complete. This bit is only valid when the
1903 * Post-Sync Operation field is a value of 1h or 3h."
1904 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001905 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001906 cmd |= MI_INVALIDATE_TLB;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001907 *cs++ = cmd;
1908 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilsonc0336662016-05-06 15:40:21 +01001909 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001910 *cs++ = 0; /* upper addr */
1911 *cs++ = 0; /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001912 } else {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001913 *cs++ = 0;
1914 *cs++ = MI_NOOP;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001915 }
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001916 intel_ring_advance(req, cs);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001917
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001918 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001919}
1920
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001921static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
1922 struct intel_engine_cs *engine)
1923{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001924 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001925 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001926
Chris Wilson39df9192016-07-20 13:31:57 +01001927 if (!i915.semaphores)
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001928 return;
1929
Chris Wilson51d545d2016-08-15 10:49:02 +01001930 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
1931 struct i915_vma *vma;
1932
Chris Wilsonf51455d2017-01-10 14:47:34 +00001933 obj = i915_gem_object_create(dev_priv, PAGE_SIZE);
Chris Wilson51d545d2016-08-15 10:49:02 +01001934 if (IS_ERR(obj))
1935 goto err;
1936
Chris Wilsona01cb372017-01-16 15:21:30 +00001937 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson51d545d2016-08-15 10:49:02 +01001938 if (IS_ERR(vma))
1939 goto err_obj;
1940
1941 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1942 if (ret)
1943 goto err_obj;
1944
1945 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
1946 if (ret)
1947 goto err_obj;
1948
1949 dev_priv->semaphore = vma;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001950 }
1951
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001952 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001953 u32 offset = i915_ggtt_offset(dev_priv->semaphore);
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001954
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001955 engine->semaphore.sync_to = gen8_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001956 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001957
1958 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001959 u32 ring_offset;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001960
1961 if (i != engine->id)
1962 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
1963 else
1964 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
1965
1966 engine->semaphore.signal_ggtt[i] = ring_offset;
1967 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001968 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001969 engine->semaphore.sync_to = gen6_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001970 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001971
1972 /*
1973 * The current semaphore is only applied on pre-gen8
1974 * platform. And there is no VCS2 ring on the pre-gen8
1975 * platform. So the semaphore between RCS and VCS2 is
1976 * initialized as INVALID. Gen8 will initialize the
1977 * sema between VCS2 and RCS later.
1978 */
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001979 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001980 static const struct {
1981 u32 wait_mbox;
1982 i915_reg_t mbox_reg;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001983 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
1984 [RCS_HW] = {
1985 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
1986 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
1987 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001988 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001989 [VCS_HW] = {
1990 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
1991 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
1992 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001993 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001994 [BCS_HW] = {
1995 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
1996 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
1997 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001998 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001999 [VECS_HW] = {
2000 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2001 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2002 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002003 },
2004 };
2005 u32 wait_mbox;
2006 i915_reg_t mbox_reg;
2007
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002008 if (i == engine->hw_id) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002009 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2010 mbox_reg = GEN6_NOSYNC;
2011 } else {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002012 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
2013 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002014 }
2015
2016 engine->semaphore.mbox.wait[i] = wait_mbox;
2017 engine->semaphore.mbox.signal[i] = mbox_reg;
2018 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002019 }
Chris Wilson51d545d2016-08-15 10:49:02 +01002020
2021 return;
2022
2023err_obj:
2024 i915_gem_object_put(obj);
2025err:
2026 DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
2027 i915.semaphores = 0;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002028}
2029
Chris Wilsoned003072016-07-01 09:18:13 +01002030static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2031 struct intel_engine_cs *engine)
2032{
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002033 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2034
Chris Wilsoned003072016-07-01 09:18:13 +01002035 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002036 engine->irq_enable = gen8_irq_enable;
2037 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002038 engine->irq_seqno_barrier = gen6_seqno_barrier;
2039 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002040 engine->irq_enable = gen6_irq_enable;
2041 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002042 engine->irq_seqno_barrier = gen6_seqno_barrier;
2043 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002044 engine->irq_enable = gen5_irq_enable;
2045 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002046 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002047 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002048 engine->irq_enable = i9xx_irq_enable;
2049 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002050 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002051 engine->irq_enable = i8xx_irq_enable;
2052 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002053 }
2054}
2055
Chris Wilsonff44ad52017-03-16 17:13:03 +00002056static void i9xx_set_default_submission(struct intel_engine_cs *engine)
2057{
2058 engine->submit_request = i9xx_submit_request;
2059}
2060
2061static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
2062{
2063 engine->submit_request = gen6_bsd_submit_request;
2064}
2065
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002066static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2067 struct intel_engine_cs *engine)
2068{
Chris Wilson618e4ca2016-08-02 22:50:35 +01002069 intel_ring_init_irq(dev_priv, engine);
2070 intel_ring_init_semaphores(dev_priv, engine);
2071
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002072 engine->init_hw = init_ring_common;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002073 engine->reset_hw = reset_ring_common;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002074
Chris Wilsone8a9c582016-12-18 15:37:20 +00002075 engine->context_pin = intel_ring_context_pin;
2076 engine->context_unpin = intel_ring_context_unpin;
2077
Chris Wilsonf73e7392016-12-18 15:37:24 +00002078 engine->request_alloc = ring_request_alloc;
2079
Chris Wilson9b81d552016-10-28 13:58:50 +01002080 engine->emit_breadcrumb = i9xx_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002081 engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
2082 if (i915.semaphores) {
2083 int num_rings;
2084
Chris Wilson9b81d552016-10-28 13:58:50 +01002085 engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002086
2087 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
2088 if (INTEL_GEN(dev_priv) >= 8) {
2089 engine->emit_breadcrumb_sz += num_rings * 6;
2090 } else {
2091 engine->emit_breadcrumb_sz += num_rings * 3;
2092 if (num_rings & 1)
2093 engine->emit_breadcrumb_sz++;
2094 }
2095 }
Chris Wilsonff44ad52017-03-16 17:13:03 +00002096
2097 engine->set_default_submission = i9xx_set_default_submission;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002098
2099 if (INTEL_GEN(dev_priv) >= 8)
Chris Wilson803688b2016-08-02 22:50:27 +01002100 engine->emit_bb_start = gen8_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002101 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson803688b2016-08-02 22:50:27 +01002102 engine->emit_bb_start = gen6_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002103 else if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson803688b2016-08-02 22:50:27 +01002104 engine->emit_bb_start = i965_emit_bb_start;
Jani Nikula2a307c22016-11-30 17:43:04 +02002105 else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002106 engine->emit_bb_start = i830_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002107 else
Chris Wilson803688b2016-08-02 22:50:27 +01002108 engine->emit_bb_start = i915_emit_bb_start;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002109}
2110
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002111int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002112{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002113 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07002114 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002115
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002116 intel_ring_default_vfuncs(dev_priv, engine);
2117
Chris Wilson61ff75a2016-07-01 17:23:28 +01002118 if (HAS_L3_DPF(dev_priv))
2119 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002120
Chris Wilsonc0336662016-05-06 15:40:21 +01002121 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002122 engine->init_context = intel_rcs_ctx_init;
Chris Wilson9b81d552016-10-28 13:58:50 +01002123 engine->emit_breadcrumb = gen8_render_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002124 engine->emit_breadcrumb_sz = gen8_render_emit_breadcrumb_sz;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002125 engine->emit_flush = gen8_render_ring_flush;
Chris Wilson98f29e82016-10-28 13:58:51 +01002126 if (i915.semaphores) {
2127 int num_rings;
2128
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002129 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilson98f29e82016-10-28 13:58:51 +01002130
2131 num_rings =
2132 hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
Chris Wilson6f9b8502017-03-24 15:17:24 +00002133 engine->emit_breadcrumb_sz += num_rings * 8;
Chris Wilson98f29e82016-10-28 13:58:51 +01002134 }
Chris Wilsonc0336662016-05-06 15:40:21 +01002135 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002136 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002137 engine->emit_flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002138 if (IS_GEN6(dev_priv))
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002139 engine->emit_flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002140 } else if (IS_GEN5(dev_priv)) {
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002141 engine->emit_flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002142 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002143 if (INTEL_GEN(dev_priv) < 4)
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002144 engine->emit_flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002145 else
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002146 engine->emit_flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002147 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002148 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002149
Chris Wilsonc0336662016-05-06 15:40:21 +01002150 if (IS_HASWELL(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002151 engine->emit_bb_start = hsw_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002152
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002153 engine->init_hw = init_render_ring;
2154 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002155
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002156 ret = intel_init_ring_buffer(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002157 if (ret)
2158 return ret;
2159
Chris Wilsonf8973c22016-07-01 17:23:21 +01002160 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonf51455d2017-01-10 14:47:34 +00002161 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Chris Wilson7d5ea802016-07-01 17:23:20 +01002162 if (ret)
2163 return ret;
2164 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01002165 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002166 if (ret)
2167 return ret;
2168 }
2169
2170 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002171}
2172
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002173int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002174{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002175 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002176
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002177 intel_ring_default_vfuncs(dev_priv, engine);
2178
Chris Wilsonc0336662016-05-06 15:40:21 +01002179 if (INTEL_GEN(dev_priv) >= 6) {
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002180 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002181 if (IS_GEN6(dev_priv))
Chris Wilsonff44ad52017-03-16 17:13:03 +00002182 engine->set_default_submission = gen6_bsd_set_default_submission;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002183 engine->emit_flush = gen6_bsd_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002184 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002185 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002186 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002187 engine->mmio_base = BSD_RING_BASE;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002188 engine->emit_flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002189 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002190 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002191 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002192 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002193 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002194
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002195 return intel_init_ring_buffer(engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002196}
Chris Wilson549f7362010-10-19 11:19:32 +01002197
Zhao Yakui845f74a2014-04-17 10:37:37 +08002198/**
Damien Lespiau62659922015-01-29 14:13:40 +00002199 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002200 */
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002201int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002202{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002203 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002204
2205 intel_ring_default_vfuncs(dev_priv, engine);
2206
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002207 engine->emit_flush = gen6_bsd_ring_flush;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002208
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002209 return intel_init_ring_buffer(engine);
Zhao Yakui845f74a2014-04-17 10:37:37 +08002210}
2211
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002212int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
Chris Wilson549f7362010-10-19 11:19:32 +01002213{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002214 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002215
2216 intel_ring_default_vfuncs(dev_priv, engine);
2217
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002218 engine->emit_flush = gen6_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002219 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002220 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002221
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002222 return intel_init_ring_buffer(engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002223}
Chris Wilsona7b97612012-07-20 12:41:08 +01002224
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002225int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002226{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002227 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002228
2229 intel_ring_default_vfuncs(dev_priv, engine);
2230
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002231 engine->emit_flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002232
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002233 if (INTEL_GEN(dev_priv) < 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002234 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01002235 engine->irq_enable = hsw_vebox_irq_enable;
2236 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002237 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002238
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002239 return intel_init_ring_buffer(engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002240}