blob: 3321b801e77d7f2cfd6664972429a70e99925dfc [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>
Chris Wilson7c2fa7f2017-11-10 14:26:34 +000031
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/i915_drm.h>
Chris Wilson7c2fa7f2017-11-10 14:26:34 +000034
35#include "i915_drv.h"
36#include "i915_gem_render_state.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070037#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010038#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070039
Chris Wilsona0442462016-04-29 09:07:05 +010040/* Rough estimate of the typical request size, performing a flush,
41 * set-context and then emitting the batch.
42 */
43#define LEGACY_REQUEST_SIZE 200
44
Chris Wilson605d5b32017-05-04 14:08:44 +010045static unsigned int __intel_ring_space(unsigned int head,
46 unsigned int tail,
47 unsigned int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010048{
Chris Wilson605d5b32017-05-04 14:08:44 +010049 /*
50 * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
51 * same cacheline, the Head Pointer must not be greater than the Tail
52 * Pointer."
53 */
54 GEM_BUG_ON(!is_power_of_2(size));
55 return (head - tail - CACHELINE_BYTES) & (size - 1);
Chris Wilson1cf0ba12014-05-05 09:07:33 +010056}
57
Chris Wilson95aebcb2017-05-04 14:08:45 +010058unsigned int intel_ring_update_space(struct intel_ring *ring)
Dave Gordonebd0fd42014-11-27 11:22:49 +000059{
Chris Wilson95aebcb2017-05-04 14:08:45 +010060 unsigned int space;
61
62 space = __intel_ring_space(ring->head, ring->emit, ring->size);
63
64 ring->space = space;
65 return space;
Dave Gordonebd0fd42014-11-27 11:22:49 +000066}
67
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000068static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010069gen2_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010070{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000071 u32 cmd, *cs;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010072
73 cmd = MI_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010074
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010075 if (mode & EMIT_INVALIDATE)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010076 cmd |= MI_READ_FLUSH;
77
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000078 cs = intel_ring_begin(req, 2);
79 if (IS_ERR(cs))
80 return PTR_ERR(cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010081
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000082 *cs++ = cmd;
83 *cs++ = MI_NOOP;
84 intel_ring_advance(req, cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010085
86 return 0;
87}
88
89static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010090gen4_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Eric Anholt62fdfea2010-05-21 13:26:39 -070091{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000092 u32 cmd, *cs;
Chris Wilson6f392d52010-08-07 11:01:22 +010093
Chris Wilson36d527d2011-03-19 22:26:49 +000094 /*
95 * read/write caches:
96 *
97 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
98 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
99 * also flushed at 2d versus 3d pipeline switches.
100 *
101 * read-only caches:
102 *
103 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
104 * MI_READ_FLUSH is set, and is always flushed on 965.
105 *
106 * I915_GEM_DOMAIN_COMMAND may not exist?
107 *
108 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
109 * invalidated when MI_EXE_FLUSH is set.
110 *
111 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
112 * invalidated with every MI_FLUSH.
113 *
114 * TLBs:
115 *
116 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
117 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
118 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
119 * are flushed at any MI_FLUSH.
120 */
121
Chris Wilsonb5321f32016-08-02 22:50:18 +0100122 cmd = MI_FLUSH;
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100123 if (mode & EMIT_INVALIDATE) {
Chris Wilson36d527d2011-03-19 22:26:49 +0000124 cmd |= MI_EXE_FLUSH;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100125 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
126 cmd |= MI_INVALIDATE_ISP;
127 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000128
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000129 cs = intel_ring_begin(req, 2);
130 if (IS_ERR(cs))
131 return PTR_ERR(cs);
Chris Wilson36d527d2011-03-19 22:26:49 +0000132
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000133 *cs++ = cmd;
134 *cs++ = MI_NOOP;
135 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000136
137 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800138}
139
Jesse Barnes8d315282011-10-16 10:23:31 +0200140/**
141 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
142 * implementing two workarounds on gen6. From section 1.4.7.1
143 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
144 *
145 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
146 * produced by non-pipelined state commands), software needs to first
147 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
148 * 0.
149 *
150 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
151 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
152 *
153 * And the workaround for these two requires this workaround first:
154 *
155 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
156 * BEFORE the pipe-control with a post-sync op and no write-cache
157 * flushes.
158 *
159 * And this last workaround is tricky because of the requirements on
160 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
161 * volume 2 part 1:
162 *
163 * "1 of the following must also be set:
164 * - Render Target Cache Flush Enable ([12] of DW1)
165 * - Depth Cache Flush Enable ([0] of DW1)
166 * - Stall at Pixel Scoreboard ([1] of DW1)
167 * - Depth Stall ([13] of DW1)
168 * - Post-Sync Operation ([13] of DW1)
169 * - Notify Enable ([8] of DW1)"
170 *
171 * The cache flushes require the workaround flush that triggered this
172 * one, so we can't use it. Depth stall would trigger the same.
173 * Post-sync nonzero is what triggered this second workaround, so we
174 * can't use that one either. Notify enable is IRQs, which aren't
175 * really our business. That leaves only stall at scoreboard.
176 */
177static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100178intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200179{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100180 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100181 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000182 u32 *cs;
Jesse Barnes8d315282011-10-16 10:23:31 +0200183
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000184 cs = intel_ring_begin(req, 6);
185 if (IS_ERR(cs))
186 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200187
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000188 *cs++ = GFX_OP_PIPE_CONTROL(5);
189 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
190 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
191 *cs++ = 0; /* low dword */
192 *cs++ = 0; /* high dword */
193 *cs++ = MI_NOOP;
194 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200195
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000196 cs = intel_ring_begin(req, 6);
197 if (IS_ERR(cs))
198 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200199
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000200 *cs++ = GFX_OP_PIPE_CONTROL(5);
201 *cs++ = PIPE_CONTROL_QW_WRITE;
202 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
203 *cs++ = 0;
204 *cs++ = 0;
205 *cs++ = MI_NOOP;
206 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200207
208 return 0;
209}
210
211static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100212gen6_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Jesse Barnes8d315282011-10-16 10:23:31 +0200213{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100214 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100215 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000216 u32 *cs, flags = 0;
Jesse Barnes8d315282011-10-16 10:23:31 +0200217 int ret;
218
Paulo Zanonib3111502012-08-17 18:35:42 -0300219 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100220 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300221 if (ret)
222 return ret;
223
Jesse Barnes8d315282011-10-16 10:23:31 +0200224 /* Just flush everything. Experiments have shown that reducing the
225 * number of bits based on the write domains has little performance
226 * impact.
227 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100228 if (mode & EMIT_FLUSH) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100229 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
230 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
231 /*
232 * Ensure that any following seqno writes only happen
233 * when the render cache is indeed flushed.
234 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200235 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100236 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100237 if (mode & EMIT_INVALIDATE) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100238 flags |= PIPE_CONTROL_TLB_INVALIDATE;
239 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
240 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
241 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
242 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
243 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
244 /*
245 * TLB invalidate requires a post-sync write.
246 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700247 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100248 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200249
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000250 cs = intel_ring_begin(req, 4);
251 if (IS_ERR(cs))
252 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200253
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000254 *cs++ = GFX_OP_PIPE_CONTROL(4);
255 *cs++ = flags;
256 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
257 *cs++ = 0;
258 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200259
260 return 0;
261}
262
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100263static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100264gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300265{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000266 u32 *cs;
Paulo Zanonif3987632012-08-17 18:35:43 -0300267
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000268 cs = intel_ring_begin(req, 4);
269 if (IS_ERR(cs))
270 return PTR_ERR(cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300271
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000272 *cs++ = GFX_OP_PIPE_CONTROL(4);
273 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
274 *cs++ = 0;
275 *cs++ = 0;
276 intel_ring_advance(req, cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300277
278 return 0;
279}
280
281static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100282gen7_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300283{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100284 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100285 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000286 u32 *cs, flags = 0;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300287
Paulo Zanonif3987632012-08-17 18:35:43 -0300288 /*
289 * Ensure that any following seqno writes only happen when the render
290 * cache is indeed flushed.
291 *
292 * Workaround: 4th PIPE_CONTROL command (except the ones with only
293 * read-cache invalidate bits set) must have the CS_STALL bit set. We
294 * don't try to be clever and just set it unconditionally.
295 */
296 flags |= PIPE_CONTROL_CS_STALL;
297
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300298 /* Just flush everything. Experiments have shown that reducing the
299 * number of bits based on the write domains has little performance
300 * impact.
301 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100302 if (mode & EMIT_FLUSH) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300303 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
304 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800305 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100306 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300307 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100308 if (mode & EMIT_INVALIDATE) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300309 flags |= PIPE_CONTROL_TLB_INVALIDATE;
310 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
311 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
312 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
313 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
314 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000315 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300316 /*
317 * TLB invalidate requires a post-sync write.
318 */
319 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200320 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300321
Chris Wilsonadd284a2014-12-16 08:44:32 +0000322 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
323
Paulo Zanonif3987632012-08-17 18:35:43 -0300324 /* Workaround: we must issue a pipe_control with CS-stall bit
325 * set before a pipe_control command that has the state cache
326 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100327 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300328 }
329
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000330 cs = intel_ring_begin(req, 4);
331 if (IS_ERR(cs))
332 return PTR_ERR(cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300333
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000334 *cs++ = GFX_OP_PIPE_CONTROL(4);
335 *cs++ = flags;
336 *cs++ = scratch_addr;
337 *cs++ = 0;
338 intel_ring_advance(req, cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300339
340 return 0;
341}
342
Ben Widawskya5f3d682013-11-02 21:07:27 -0700343static int
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000344gen8_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300345{
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000346 u32 flags;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000347 u32 *cs;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300348
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000349 cs = intel_ring_begin(req, mode & EMIT_INVALIDATE ? 12 : 6);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000350 if (IS_ERR(cs))
351 return PTR_ERR(cs);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300352
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000353 flags = PIPE_CONTROL_CS_STALL;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700354
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100355 if (mode & EMIT_FLUSH) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700356 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
357 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800358 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100359 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700360 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100361 if (mode & EMIT_INVALIDATE) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700362 flags |= PIPE_CONTROL_TLB_INVALIDATE;
363 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
364 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
365 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
366 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
367 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
368 flags |= PIPE_CONTROL_QW_WRITE;
369 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800370
371 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000372 cs = gen8_emit_pipe_control(cs,
373 PIPE_CONTROL_CS_STALL |
374 PIPE_CONTROL_STALL_AT_SCOREBOARD,
375 0);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700376 }
377
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000378 cs = gen8_emit_pipe_control(cs, flags,
379 i915_ggtt_offset(req->engine->scratch) +
380 2 * CACHELINE_BYTES);
381
382 intel_ring_advance(req, cs);
383
384 return 0;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700385}
386
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000387static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200388{
Chris Wilsonc0336662016-05-06 15:40:21 +0100389 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200390 u32 addr;
391
392 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100393 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200394 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
395 I915_WRITE(HWS_PGA, addr);
396}
397
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000398static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000399{
Chris Wilsonc0336662016-05-06 15:40:21 +0100400 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200401 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000402
403 /* The ring status page addresses are no longer next to the rest of
404 * the ring registers as of gen7.
405 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100406 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000407 switch (engine->id) {
Michel Thierrya2d3d262017-08-30 11:01:15 -0700408 /*
409 * No more rings exist on Gen7. Default case is only to shut up
410 * gcc switch check warning.
411 */
412 default:
413 GEM_BUG_ON(engine->id);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000414 case RCS:
415 mmio = RENDER_HWS_PGA_GEN7;
416 break;
417 case BCS:
418 mmio = BLT_HWS_PGA_GEN7;
419 break;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000420 case VCS:
421 mmio = BSD_HWS_PGA_GEN7;
422 break;
423 case VECS:
424 mmio = VEBOX_HWS_PGA_GEN7;
425 break;
426 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100427 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000428 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000429 } else {
430 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000431 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000432 }
433
Ville Syrjäläc54980892017-08-18 21:37:01 +0300434 if (INTEL_GEN(dev_priv) >= 6)
435 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
436
Chris Wilson57e88532016-08-15 10:48:57 +0100437 I915_WRITE(mmio, engine->status_page.ggtt_offset);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000438 POSTING_READ(mmio);
439
440 /*
441 * Flush the TLB for this page
442 *
443 * FIXME: These two bits have disappeared on gen8, so a question
444 * arises: do we still need this and if so how should we go about
445 * invalidating the TLB?
446 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100447 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000448 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000449
450 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000451 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000452
453 I915_WRITE(reg,
454 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
455 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100456 if (intel_wait_for_register(dev_priv,
457 reg, INSTPM_SYNC_FLUSH, 0,
458 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000459 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000460 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000461 }
462}
463
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000464static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100465{
Chris Wilsonc0336662016-05-06 15:40:21 +0100466 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100467
Chris Wilson21a2c582016-08-15 10:49:11 +0100468 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000469 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100470 if (intel_wait_for_register(dev_priv,
471 RING_MI_MODE(engine->mmio_base),
472 MODE_IDLE,
473 MODE_IDLE,
474 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000475 DRM_ERROR("%s : timed out trying to stop ring\n",
476 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100477 /* Sometimes we observe that the idle flag is not
478 * set even though the ring is empty. So double
479 * check before giving up.
480 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000481 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100482 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100483 }
484 }
485
Chris Wilson11caf552017-10-27 10:43:11 +0100486 I915_WRITE_HEAD(engine, I915_READ_TAIL(engine));
487
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000488 I915_WRITE_HEAD(engine, 0);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100489 I915_WRITE_TAIL(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100490
Chris Wilson11caf552017-10-27 10:43:11 +0100491 /* The ring must be empty before it is disabled */
492 I915_WRITE_CTL(engine, 0);
493
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000494 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100495}
496
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000497static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800498{
Chris Wilsonc0336662016-05-06 15:40:21 +0100499 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +0100500 struct intel_ring *ring = engine->buffer;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200501 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800502
Mika Kuoppala59bad942015-01-16 11:34:40 +0200503 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200504
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000505 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100506 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000507 DRM_DEBUG_KMS("%s head not reset to zero "
508 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000509 engine->name,
510 I915_READ_CTL(engine),
511 I915_READ_HEAD(engine),
512 I915_READ_TAIL(engine),
513 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800514
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000515 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000516 DRM_ERROR("failed to set %s head to zero "
517 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000518 engine->name,
519 I915_READ_CTL(engine),
520 I915_READ_HEAD(engine),
521 I915_READ_TAIL(engine),
522 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100523 ret = -EIO;
524 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000525 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700526 }
527
Carlos Santa31776592016-08-17 12:30:56 -0700528 if (HWS_NEEDS_PHYSICAL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000529 ring_setup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -0700530 else
531 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100532
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100533 intel_engine_reset_breadcrumbs(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +0100534
Jiri Kosinaece4a172014-08-07 16:29:53 +0200535 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000536 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200537
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200538 /* Initialize the ring. This must happen _after_ we've cleared the ring
539 * registers with the above sequence (the readback of the HEAD registers
540 * also enforces ordering), otherwise the hw might lose the new ring
541 * register values. */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100542 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
Chris Wilson95468892014-08-07 15:39:54 +0100543
544 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000545 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100546 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000547 engine->name, I915_READ_HEAD(engine));
Chris Wilson821ed7d2016-09-09 14:11:53 +0100548
549 intel_ring_update_space(ring);
550 I915_WRITE_HEAD(engine, ring->head);
551 I915_WRITE_TAIL(engine, ring->tail);
552 (void)I915_READ_TAIL(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100553
Chris Wilson62ae14b2016-10-04 21:11:25 +0100554 I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800555
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800556 /* If the head is still not zero, the ring is dead */
Chris Wilsonf42bb652017-04-11 11:13:40 +0100557 if (intel_wait_for_register(dev_priv, RING_CTL(engine->mmio_base),
558 RING_VALID, RING_VALID,
559 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000560 DRM_ERROR("%s initialization failed "
Chris Wilson821ed7d2016-09-09 14:11:53 +0100561 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000562 engine->name,
563 I915_READ_CTL(engine),
564 I915_READ_CTL(engine) & RING_VALID,
Chris Wilson821ed7d2016-09-09 14:11:53 +0100565 I915_READ_HEAD(engine), ring->head,
566 I915_READ_TAIL(engine), ring->tail,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000567 I915_READ_START(engine),
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100568 i915_ggtt_offset(ring->vma));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200569 ret = -EIO;
570 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800571 }
572
Tomas Elffc0768c2016-03-21 16:26:59 +0000573 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100574
Chris Wilson7836cd02017-10-13 14:12:17 +0100575 if (INTEL_GEN(dev_priv) > 2)
576 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
577
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200578out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200579 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200580
581 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700582}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800583
Chris Wilson821ed7d2016-09-09 14:11:53 +0100584static void reset_ring_common(struct intel_engine_cs *engine,
585 struct drm_i915_gem_request *request)
586{
Chris Wilson67e64562017-10-09 12:03:01 +0100587 /*
588 * RC6 must be prevented until the reset is complete and the engine
589 * reinitialised. If it occurs in the middle of this sequence, the
590 * state written to/loaded from the power context is ill-defined (e.g.
591 * the PP_BASE_DIR may be lost).
592 */
593 assert_forcewakes_active(engine->i915, FORCEWAKE_ALL);
594
595 /*
596 * Try to restore the logical GPU state to match the continuation
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000597 * of the request queue. If we skip the context/PD restore, then
598 * the next request may try to execute assuming that its context
599 * is valid and loaded on the GPU and so may try to access invalid
600 * memory, prompting repeated GPU hangs.
601 *
602 * If the request was guilty, we still restore the logical state
603 * in case the next request requires it (e.g. the aliasing ppgtt),
604 * but skip over the hung batch.
605 *
606 * If the request was innocent, we try to replay the request with
607 * the restored context.
608 */
609 if (request) {
610 struct drm_i915_private *dev_priv = request->i915;
611 struct intel_context *ce = &request->ctx->engine[engine->id];
612 struct i915_hw_ppgtt *ppgtt;
Chris Wilson821ed7d2016-09-09 14:11:53 +0100613
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000614 /* FIXME consider gen8 reset */
615
616 if (ce->state) {
617 I915_WRITE(CCID,
618 i915_ggtt_offset(ce->state) |
619 BIT(8) /* must be set! */ |
620 CCID_EXTENDED_STATE_SAVE |
621 CCID_EXTENDED_STATE_RESTORE |
622 CCID_EN);
623 }
624
625 ppgtt = request->ctx->ppgtt ?: engine->i915->mm.aliasing_ppgtt;
626 if (ppgtt) {
627 u32 pd_offset = ppgtt->pd.base.ggtt_offset << 10;
628
629 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
630 I915_WRITE(RING_PP_DIR_BASE(engine), pd_offset);
631
632 /* Wait for the PD reload to complete */
633 if (intel_wait_for_register(dev_priv,
634 RING_PP_DIR_BASE(engine),
635 BIT(0), 0,
636 10))
637 DRM_ERROR("Wait for reload of ppgtt page-directory timed out\n");
638
639 ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
640 }
641
642 /* If the rq hung, jump to its breadcrumb and skip the batch */
Chris Wilsonfe085f12017-03-21 10:25:52 +0000643 if (request->fence.error == -EIO)
644 request->ring->head = request->postfix;
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000645 } else {
646 engine->legacy_active_context = NULL;
647 }
Chris Wilson821ed7d2016-09-09 14:11:53 +0100648}
649
John Harrison87531812015-05-29 17:43:44 +0100650static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100651{
652 int ret;
653
John Harrisone2be4fa2015-05-29 17:43:54 +0100654 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100655 if (ret != 0)
656 return ret;
657
Chris Wilson4e50f082016-10-28 13:58:31 +0100658 ret = i915_gem_render_state_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100659 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000660 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100661
Chris Wilsone26e1b92016-01-29 16:49:05 +0000662 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100663}
664
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000665static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800666{
Chris Wilsonc0336662016-05-06 15:40:21 +0100667 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000668 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +0200669 if (ret)
670 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800671
Akash Goel61a563a2014-03-25 18:01:50 +0530672 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100673 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200674 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000675
676 /* We need to disable the AsyncFlip performance optimisations in order
677 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
678 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +0100679 *
Ville Syrjälä2441f872015-06-02 15:37:37 +0300680 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000681 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100682 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000683 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
684
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000685 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +0530686 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +0100687 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000688 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +0000689 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000690
Akash Goel01fa0302014-03-24 23:00:04 +0530691 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +0100692 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000693 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +0530694 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000695 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +0100696
Chris Wilsonc0336662016-05-06 15:40:21 +0100697 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700698 /* From the Sandybridge PRM, volume 1 part 3, page 24:
699 * "If this bit is set, STCunit will have LRA as replacement
700 * policy. [...] This bit must be reset. LRA replacement
701 * policy is not supported."
702 */
703 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200704 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -0800705 }
706
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100707 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200708 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +0000709
Ville Syrjälä035ea402016-07-12 19:24:47 +0300710 if (INTEL_INFO(dev_priv)->gen >= 6)
711 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -0700712
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000713 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800714}
715
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000716static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000717{
Chris Wilsonc0336662016-05-06 15:40:21 +0100718 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700719
Chris Wilson19880c42016-08-15 10:49:05 +0100720 i915_vma_unpin_and_release(&dev_priv->semaphore);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000721}
722
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000723static u32 *gen8_rcs_signal(struct drm_i915_gem_request *req, u32 *cs)
Ben Widawsky3e789982014-06-30 09:53:37 -0700724{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100725 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700726 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +0000727 enum intel_engine_id id;
Ben Widawsky3e789982014-06-30 09:53:37 -0700728
Akash Goel3b3f1652016-10-13 22:44:48 +0530729 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100730 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -0700731 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
732 continue;
733
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000734 *cs++ = GFX_OP_PIPE_CONTROL(6);
735 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_QW_WRITE |
736 PIPE_CONTROL_CS_STALL;
737 *cs++ = lower_32_bits(gtt_offset);
738 *cs++ = upper_32_bits(gtt_offset);
739 *cs++ = req->global_seqno;
740 *cs++ = 0;
741 *cs++ = MI_SEMAPHORE_SIGNAL |
742 MI_SEMAPHORE_TARGET(waiter->hw_id);
743 *cs++ = 0;
Ben Widawsky3e789982014-06-30 09:53:37 -0700744 }
745
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000746 return cs;
Ben Widawsky3e789982014-06-30 09:53:37 -0700747}
748
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000749static u32 *gen8_xcs_signal(struct drm_i915_gem_request *req, u32 *cs)
Ben Widawsky3e789982014-06-30 09:53:37 -0700750{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100751 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700752 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +0000753 enum intel_engine_id id;
Ben Widawsky3e789982014-06-30 09:53:37 -0700754
Akash Goel3b3f1652016-10-13 22:44:48 +0530755 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100756 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -0700757 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
758 continue;
759
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000760 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
761 *cs++ = lower_32_bits(gtt_offset) | MI_FLUSH_DW_USE_GTT;
762 *cs++ = upper_32_bits(gtt_offset);
763 *cs++ = req->global_seqno;
764 *cs++ = MI_SEMAPHORE_SIGNAL |
765 MI_SEMAPHORE_TARGET(waiter->hw_id);
766 *cs++ = 0;
Ben Widawsky3e789982014-06-30 09:53:37 -0700767 }
768
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000769 return cs;
Ben Widawsky3e789982014-06-30 09:53:37 -0700770}
771
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000772static u32 *gen6_signal(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000773{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100774 struct drm_i915_private *dev_priv = req->i915;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100775 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530776 enum intel_engine_id id;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100777 int num_rings = 0;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700778
Akash Goel3b3f1652016-10-13 22:44:48 +0530779 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100780 i915_reg_t mbox_reg;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200781
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100782 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
783 continue;
784
785 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200786 if (i915_mmio_reg_valid(mbox_reg)) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000787 *cs++ = MI_LOAD_REGISTER_IMM(1);
788 *cs++ = i915_mmio_reg_offset(mbox_reg);
789 *cs++ = req->global_seqno;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100790 num_rings++;
Ben Widawsky78325f22014-04-29 14:52:29 -0700791 }
792 }
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100793 if (num_rings & 1)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000794 *cs++ = MI_NOOP;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700795
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000796 return cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000797}
798
Chris Wilson27a5f612017-09-15 18:31:00 +0100799static void cancel_requests(struct intel_engine_cs *engine)
800{
801 struct drm_i915_gem_request *request;
802 unsigned long flags;
803
804 spin_lock_irqsave(&engine->timeline->lock, flags);
805
806 /* Mark all submitted requests as skipped. */
807 list_for_each_entry(request, &engine->timeline->requests, link) {
808 GEM_BUG_ON(!request->global_seqno);
809 if (!i915_gem_request_completed(request))
810 dma_fence_set_error(&request->fence, -EIO);
811 }
812 /* Remaining _unready_ requests will be nop'ed when submitted */
813
814 spin_unlock_irqrestore(&engine->timeline->lock, flags);
815}
816
Chris Wilsonb0411e72016-08-02 22:50:34 +0100817static void i9xx_submit_request(struct drm_i915_gem_request *request)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000818{
Chris Wilsonb0411e72016-08-02 22:50:34 +0100819 struct drm_i915_private *dev_priv = request->i915;
820
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000821 i915_gem_request_submit(request);
822
Chris Wilsone6ba9992017-04-25 14:00:49 +0100823 I915_WRITE_TAIL(request->engine,
824 intel_ring_set_tail(request->ring, request->tail));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100825}
826
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000827static void i9xx_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100828{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000829 *cs++ = MI_STORE_DWORD_INDEX;
830 *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
831 *cs++ = req->global_seqno;
832 *cs++ = MI_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000833
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000834 req->tail = intel_ring_offset(req, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +0100835 assert_ring_tail_valid(req->ring, req->tail);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000836}
837
Chris Wilson98f29e82016-10-28 13:58:51 +0100838static const int i9xx_emit_breadcrumb_sz = 4;
839
Chris Wilsonb0411e72016-08-02 22:50:34 +0100840/**
Chris Wilson9b81d552016-10-28 13:58:50 +0100841 * gen6_sema_emit_breadcrumb - Update the semaphore mailbox registers
Chris Wilsonb0411e72016-08-02 22:50:34 +0100842 *
843 * @request - request to write to the ring
844 *
845 * Update the mailbox registers in the *other* rings with the current seqno.
846 * This acts like a signal in the canonical semaphore.
847 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000848static void gen6_sema_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100849{
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100850 return i9xx_emit_breadcrumb(req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000851 req->engine->semaphore.signal(req, cs));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100852}
853
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100854static void gen8_render_emit_breadcrumb(struct drm_i915_gem_request *req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000855 u32 *cs)
Chris Wilsona58c01a2016-04-29 13:18:21 +0100856{
857 struct intel_engine_cs *engine = req->engine;
Chris Wilsona58c01a2016-04-29 13:18:21 +0100858
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100859 if (engine->semaphore.signal)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000860 cs = engine->semaphore.signal(req, cs);
Chris Wilson9242f972016-08-02 22:50:33 +0100861
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000862 *cs++ = GFX_OP_PIPE_CONTROL(6);
863 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
864 PIPE_CONTROL_QW_WRITE;
865 *cs++ = intel_hws_seqno_address(engine);
866 *cs++ = 0;
867 *cs++ = req->global_seqno;
Chris Wilsona58c01a2016-04-29 13:18:21 +0100868 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000869 *cs++ = 0;
870 *cs++ = MI_USER_INTERRUPT;
871 *cs++ = MI_NOOP;
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100872
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000873 req->tail = intel_ring_offset(req, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +0100874 assert_ring_tail_valid(req->ring, req->tail);
Chris Wilsona58c01a2016-04-29 13:18:21 +0100875}
876
Chris Wilson98f29e82016-10-28 13:58:51 +0100877static const int gen8_render_emit_breadcrumb_sz = 8;
878
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700879/**
880 * intel_ring_sync - sync the waiter to the signaller on seqno
881 *
882 * @waiter - ring that is waiting
883 * @signaller - ring which has, or will signal
884 * @seqno - seqno which the waiter will block on
885 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700886
887static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100888gen8_ring_sync_to(struct drm_i915_gem_request *req,
889 struct drm_i915_gem_request *signal)
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700890{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100891 struct drm_i915_private *dev_priv = req->i915;
892 u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +0100893 struct i915_hw_ppgtt *ppgtt;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000894 u32 *cs;
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700895
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000896 cs = intel_ring_begin(req, 4);
897 if (IS_ERR(cs))
898 return PTR_ERR(cs);
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700899
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000900 *cs++ = MI_SEMAPHORE_WAIT | MI_SEMAPHORE_GLOBAL_GTT |
901 MI_SEMAPHORE_SAD_GTE_SDD;
902 *cs++ = signal->global_seqno;
903 *cs++ = lower_32_bits(offset);
904 *cs++ = upper_32_bits(offset);
905 intel_ring_advance(req, cs);
Chris Wilson6ef48d72016-04-29 13:18:25 +0100906
907 /* When the !RCS engines idle waiting upon a semaphore, they lose their
908 * pagetables and we must reload them before executing the batch.
909 * We do this on the i915_switch_context() following the wait and
910 * before the dispatch.
911 */
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100912 ppgtt = req->ctx->ppgtt;
913 if (ppgtt && req->engine->id != RCS)
914 ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700915 return 0;
916}
917
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700918static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100919gen6_ring_sync_to(struct drm_i915_gem_request *req,
920 struct drm_i915_gem_request *signal)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000921{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700922 u32 dw1 = MI_SEMAPHORE_MBOX |
923 MI_SEMAPHORE_COMPARE |
924 MI_SEMAPHORE_REGISTER;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100925 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000926 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000927
Chris Wilsonddf07be2016-08-02 22:50:39 +0100928 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
929
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000930 cs = intel_ring_begin(req, 4);
931 if (IS_ERR(cs))
932 return PTR_ERR(cs);
Chris Wilsonddf07be2016-08-02 22:50:39 +0100933
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000934 *cs++ = dw1 | wait_mbox;
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700935 /* Throughout all of the GEM code, seqno passed implies our current
936 * seqno is >= the last seqno executed. However for hardware the
937 * comparison is strictly greater than.
938 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000939 *cs++ = signal->global_seqno - 1;
940 *cs++ = 0;
941 *cs++ = MI_NOOP;
942 intel_ring_advance(req, cs);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000943
944 return 0;
945}
946
Chris Wilsonf8973c22016-07-01 17:23:21 +0100947static void
Dave Gordon38a0f2d2016-07-20 18:16:06 +0100948gen5_seqno_barrier(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000949{
Chris Wilsonf8973c22016-07-01 17:23:21 +0100950 /* MI_STORE are internally buffered by the GPU and not flushed
951 * either by MI_FLUSH or SyncFlush or any other combination of
952 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000953 *
Chris Wilsonf8973c22016-07-01 17:23:21 +0100954 * "Only the submission of the store operation is guaranteed.
955 * The write result will be complete (coherent) some time later
956 * (this is practically a finite period but there is no guaranteed
957 * latency)."
958 *
959 * Empirically, we observe that we need a delay of at least 75us to
960 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000961 */
Chris Wilsonf8973c22016-07-01 17:23:21 +0100962 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000963}
964
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100965static void
966gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100967{
Chris Wilsonc0336662016-05-06 15:40:21 +0100968 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100969
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100970 /* Workaround to force correct ordering between irq and seqno writes on
971 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +0100972 * ACTHD) before reading the status page.
973 *
974 * Note that this effectively stalls the read by the time it takes to
975 * do a memory transaction, which more or less ensures that the write
976 * from the GPU has sufficient time to invalidate the CPU cacheline.
977 * Alternatively we could delay the interrupt from the CS ring to give
978 * the write time to land, but that would incur a delay after every
979 * batch i.e. much more frequent than a delay when waiting for the
980 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100981 *
982 * Also note that to prevent whole machine hangs on gen7, we have to
983 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +0100984 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100985 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100986 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100987 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100988}
989
Chris Wilson31bb59c2016-07-01 17:23:27 +0100990static void
991gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200992{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100993 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +0200994}
995
996static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100997gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200998{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100999 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001000}
1001
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001002static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001003i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001004{
Chris Wilsonc0336662016-05-06 15:40:21 +01001005 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001006
Chris Wilson31bb59c2016-07-01 17:23:27 +01001007 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1008 I915_WRITE(IMR, dev_priv->irq_mask);
1009 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +01001010}
1011
1012static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001013i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001014{
Chris Wilsonc0336662016-05-06 15:40:21 +01001015 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001016
Chris Wilson31bb59c2016-07-01 17:23:27 +01001017 dev_priv->irq_mask |= engine->irq_enable_mask;
1018 I915_WRITE(IMR, dev_priv->irq_mask);
1019}
1020
1021static void
1022i8xx_irq_enable(struct intel_engine_cs *engine)
1023{
1024 struct drm_i915_private *dev_priv = engine->i915;
1025
1026 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1027 I915_WRITE16(IMR, dev_priv->irq_mask);
1028 POSTING_READ16(RING_IMR(engine->mmio_base));
1029}
1030
1031static void
1032i8xx_irq_disable(struct intel_engine_cs *engine)
1033{
1034 struct drm_i915_private *dev_priv = engine->i915;
1035
1036 dev_priv->irq_mask |= engine->irq_enable_mask;
1037 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001038}
1039
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001040static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001041bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001042{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001043 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001044
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001045 cs = intel_ring_begin(req, 2);
1046 if (IS_ERR(cs))
1047 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001048
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001049 *cs++ = MI_FLUSH;
1050 *cs++ = MI_NOOP;
1051 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001052 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001053}
1054
Chris Wilson0f468322011-01-04 17:35:21 +00001055static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001056gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001057{
Chris Wilsonc0336662016-05-06 15:40:21 +01001058 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001059
Chris Wilson61ff75a2016-07-01 17:23:28 +01001060 I915_WRITE_IMR(engine,
1061 ~(engine->irq_enable_mask |
1062 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001063 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001064}
1065
1066static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001067gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001068{
Chris Wilsonc0336662016-05-06 15:40:21 +01001069 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001070
Chris Wilson61ff75a2016-07-01 17:23:28 +01001071 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001072 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001073}
1074
1075static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001076hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001077{
Chris Wilsonc0336662016-05-06 15:40:21 +01001078 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001079
Chris Wilson31bb59c2016-07-01 17:23:27 +01001080 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
Akash Goelf4e9af42016-10-12 21:54:30 +05301081 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001082}
1083
1084static void
1085hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1086{
1087 struct drm_i915_private *dev_priv = engine->i915;
1088
1089 I915_WRITE_IMR(engine, ~0);
Akash Goelf4e9af42016-10-12 21:54:30 +05301090 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001091}
1092
1093static void
1094gen8_irq_enable(struct intel_engine_cs *engine)
1095{
1096 struct drm_i915_private *dev_priv = engine->i915;
1097
Chris Wilson61ff75a2016-07-01 17:23:28 +01001098 I915_WRITE_IMR(engine,
1099 ~(engine->irq_enable_mask |
1100 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001101 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1102}
1103
1104static void
1105gen8_irq_disable(struct intel_engine_cs *engine)
1106{
1107 struct drm_i915_private *dev_priv = engine->i915;
1108
Chris Wilson61ff75a2016-07-01 17:23:28 +01001109 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001110}
1111
Zou Nan haid1b851f2010-05-21 09:08:57 +08001112static int
Chris Wilson803688b2016-08-02 22:50:27 +01001113i965_emit_bb_start(struct drm_i915_gem_request *req,
1114 u64 offset, u32 length,
1115 unsigned int dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001116{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001117 u32 *cs;
Chris Wilson78501ea2010-10-27 12:18:21 +01001118
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001119 cs = intel_ring_begin(req, 2);
1120 if (IS_ERR(cs))
1121 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001122
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001123 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
1124 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
1125 *cs++ = offset;
1126 intel_ring_advance(req, cs);
Chris Wilson78501ea2010-10-27 12:18:21 +01001127
Zou Nan haid1b851f2010-05-21 09:08:57 +08001128 return 0;
1129}
1130
Daniel Vetterb45305f2012-12-17 16:21:27 +01001131/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1132#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001133#define I830_TLB_ENTRIES (2)
1134#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001135static int
Chris Wilson803688b2016-08-02 22:50:27 +01001136i830_emit_bb_start(struct drm_i915_gem_request *req,
1137 u64 offset, u32 len,
1138 unsigned int dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001139{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001140 u32 *cs, cs_offset = i915_ggtt_offset(req->engine->scratch);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001141
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001142 cs = intel_ring_begin(req, 6);
1143 if (IS_ERR(cs))
1144 return PTR_ERR(cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001145
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001146 /* Evict the invalid PTE TLBs */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001147 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
1148 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
1149 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
1150 *cs++ = cs_offset;
1151 *cs++ = 0xdeadbeef;
1152 *cs++ = MI_NOOP;
1153 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001154
John Harrison8e004ef2015-02-13 11:48:10 +00001155 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001156 if (len > I830_BATCH_LIMIT)
1157 return -ENOSPC;
1158
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001159 cs = intel_ring_begin(req, 6 + 2);
1160 if (IS_ERR(cs))
1161 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001162
1163 /* Blit the batch (which has now all relocs applied) to the
1164 * stable batch scratch bo area (so that the CS never
1165 * stumbles over its tlb invalidation bug) ...
1166 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001167 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
1168 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
1169 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
1170 *cs++ = cs_offset;
1171 *cs++ = 4096;
1172 *cs++ = offset;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001173
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001174 *cs++ = MI_FLUSH;
1175 *cs++ = MI_NOOP;
1176 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001177
1178 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001179 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001180 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001181
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001182 cs = intel_ring_begin(req, 2);
1183 if (IS_ERR(cs))
1184 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001185
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001186 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1187 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1188 MI_BATCH_NON_SECURE);
1189 intel_ring_advance(req, cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001190
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001191 return 0;
1192}
1193
1194static int
Chris Wilson803688b2016-08-02 22:50:27 +01001195i915_emit_bb_start(struct drm_i915_gem_request *req,
1196 u64 offset, u32 len,
1197 unsigned int dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001198{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001199 u32 *cs;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001200
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001201 cs = intel_ring_begin(req, 2);
1202 if (IS_ERR(cs))
1203 return PTR_ERR(cs);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001204
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001205 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1206 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1207 MI_BATCH_NON_SECURE);
1208 intel_ring_advance(req, cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001209
Eric Anholt62fdfea2010-05-21 13:26:39 -07001210 return 0;
1211}
1212
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001213
Chris Wilson6b8294a2012-11-16 11:43:20 +00001214
Chris Wilsond822bb12017-04-03 12:34:25 +01001215int intel_ring_pin(struct intel_ring *ring,
1216 struct drm_i915_private *i915,
1217 unsigned int offset_bias)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001218{
Chris Wilsond822bb12017-04-03 12:34:25 +01001219 enum i915_map_type map = HAS_LLC(i915) ? I915_MAP_WB : I915_MAP_WC;
Chris Wilson57e88532016-08-15 10:48:57 +01001220 struct i915_vma *vma = ring->vma;
Chris Wilsond822bb12017-04-03 12:34:25 +01001221 unsigned int flags;
Dave Gordon83052162016-04-12 14:46:16 +01001222 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001223 int ret;
1224
Chris Wilson57e88532016-08-15 10:48:57 +01001225 GEM_BUG_ON(ring->vaddr);
1226
Chris Wilson9d808412016-08-18 17:16:56 +01001227
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001228 flags = PIN_GLOBAL;
1229 if (offset_bias)
1230 flags |= PIN_OFFSET_BIAS | offset_bias;
Chris Wilson9d808412016-08-18 17:16:56 +01001231 if (vma->obj->stolen)
Chris Wilson57e88532016-08-15 10:48:57 +01001232 flags |= PIN_MAPPABLE;
1233
1234 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilson9d808412016-08-18 17:16:56 +01001235 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
Chris Wilson57e88532016-08-15 10:48:57 +01001236 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1237 else
1238 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1239 if (unlikely(ret))
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001240 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001241 }
1242
Chris Wilson57e88532016-08-15 10:48:57 +01001243 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1244 if (unlikely(ret))
1245 return ret;
1246
Chris Wilson9d808412016-08-18 17:16:56 +01001247 if (i915_vma_is_map_and_fenceable(vma))
Chris Wilson57e88532016-08-15 10:48:57 +01001248 addr = (void __force *)i915_vma_pin_iomap(vma);
1249 else
Chris Wilson9d808412016-08-18 17:16:56 +01001250 addr = i915_gem_object_pin_map(vma->obj, map);
Chris Wilson57e88532016-08-15 10:48:57 +01001251 if (IS_ERR(addr))
1252 goto err;
1253
Chris Wilson3d574a62017-10-13 21:26:16 +01001254 vma->obj->pin_global++;
1255
Chris Wilson32c04f12016-08-02 22:50:22 +01001256 ring->vaddr = addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001257 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01001258
Chris Wilson57e88532016-08-15 10:48:57 +01001259err:
1260 i915_vma_unpin(vma);
1261 return PTR_ERR(addr);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001262}
1263
Chris Wilsone6ba9992017-04-25 14:00:49 +01001264void intel_ring_reset(struct intel_ring *ring, u32 tail)
1265{
1266 GEM_BUG_ON(!list_empty(&ring->request_list));
1267 ring->tail = tail;
1268 ring->head = tail;
1269 ring->emit = tail;
1270 intel_ring_update_space(ring);
1271}
1272
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001273void intel_ring_unpin(struct intel_ring *ring)
1274{
1275 GEM_BUG_ON(!ring->vma);
1276 GEM_BUG_ON(!ring->vaddr);
1277
Chris Wilsone6ba9992017-04-25 14:00:49 +01001278 /* Discard any unused bytes beyond that submitted to hw. */
1279 intel_ring_reset(ring, ring->tail);
1280
Chris Wilson9d808412016-08-18 17:16:56 +01001281 if (i915_vma_is_map_and_fenceable(ring->vma))
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001282 i915_vma_unpin_iomap(ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001283 else
1284 i915_gem_object_unpin_map(ring->vma->obj);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001285 ring->vaddr = NULL;
1286
Chris Wilson3d574a62017-10-13 21:26:16 +01001287 ring->vma->obj->pin_global--;
Chris Wilson57e88532016-08-15 10:48:57 +01001288 i915_vma_unpin(ring->vma);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001289}
1290
Chris Wilson57e88532016-08-15 10:48:57 +01001291static struct i915_vma *
1292intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
Oscar Mateo2919d292014-07-03 16:28:02 +01001293{
Chris Wilsone3efda42014-04-09 09:19:41 +01001294 struct drm_i915_gem_object *obj;
Chris Wilson57e88532016-08-15 10:48:57 +01001295 struct i915_vma *vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001296
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00001297 obj = i915_gem_object_create_stolen(dev_priv, size);
Chris Wilsonc58b7352016-08-18 17:16:57 +01001298 if (!obj)
Chris Wilson2d6c4c82017-04-20 11:17:09 +01001299 obj = i915_gem_object_create_internal(dev_priv, size);
Chris Wilson57e88532016-08-15 10:48:57 +01001300 if (IS_ERR(obj))
1301 return ERR_CAST(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001302
Akash Goel24f3a8c2014-06-17 10:59:42 +05301303 /* mark ring buffers as read-only from GPU side by default */
1304 obj->gt_ro = 1;
1305
Chris Wilsona01cb372017-01-16 15:21:30 +00001306 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson57e88532016-08-15 10:48:57 +01001307 if (IS_ERR(vma))
1308 goto err;
Chris Wilsone3efda42014-04-09 09:19:41 +01001309
Chris Wilson57e88532016-08-15 10:48:57 +01001310 return vma;
1311
1312err:
1313 i915_gem_object_put(obj);
1314 return vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001315}
1316
Chris Wilson7e37f882016-08-02 22:50:21 +01001317struct intel_ring *
1318intel_engine_create_ring(struct intel_engine_cs *engine, int size)
Chris Wilson01101fa2015-09-03 13:01:39 +01001319{
Chris Wilson7e37f882016-08-02 22:50:21 +01001320 struct intel_ring *ring;
Chris Wilson57e88532016-08-15 10:48:57 +01001321 struct i915_vma *vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001322
Chris Wilson8f942012016-08-02 22:50:30 +01001323 GEM_BUG_ON(!is_power_of_2(size));
Chris Wilson62ae14b2016-10-04 21:11:25 +01001324 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
Chris Wilson8f942012016-08-02 22:50:30 +01001325
Chris Wilson01101fa2015-09-03 13:01:39 +01001326 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson57e88532016-08-15 10:48:57 +01001327 if (!ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001328 return ERR_PTR(-ENOMEM);
1329
Chris Wilson675d9ad2016-08-04 07:52:36 +01001330 INIT_LIST_HEAD(&ring->request_list);
1331
Chris Wilson01101fa2015-09-03 13:01:39 +01001332 ring->size = size;
1333 /* Workaround an erratum on the i830 which causes a hang if
1334 * the TAIL pointer points to within the last 2 cachelines
1335 * of the buffer.
1336 */
1337 ring->effective_size = size;
Jani Nikula2a307c22016-11-30 17:43:04 +02001338 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01001339 ring->effective_size -= 2 * CACHELINE_BYTES;
1340
Chris Wilson01101fa2015-09-03 13:01:39 +01001341 intel_ring_update_space(ring);
1342
Chris Wilson57e88532016-08-15 10:48:57 +01001343 vma = intel_ring_create_vma(engine->i915, size);
1344 if (IS_ERR(vma)) {
Chris Wilson01101fa2015-09-03 13:01:39 +01001345 kfree(ring);
Chris Wilson57e88532016-08-15 10:48:57 +01001346 return ERR_CAST(vma);
Chris Wilson01101fa2015-09-03 13:01:39 +01001347 }
Chris Wilson57e88532016-08-15 10:48:57 +01001348 ring->vma = vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001349
1350 return ring;
1351}
1352
1353void
Chris Wilson7e37f882016-08-02 22:50:21 +01001354intel_ring_free(struct intel_ring *ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001355{
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001356 struct drm_i915_gem_object *obj = ring->vma->obj;
1357
1358 i915_vma_close(ring->vma);
1359 __i915_gem_object_release_unless_active(obj);
1360
Chris Wilson01101fa2015-09-03 13:01:39 +01001361 kfree(ring);
1362}
1363
Chris Wilson72b72ae2017-02-10 10:14:22 +00001364static int context_pin(struct i915_gem_context *ctx)
Chris Wilsone8a9c582016-12-18 15:37:20 +00001365{
1366 struct i915_vma *vma = ctx->engine[RCS].state;
1367 int ret;
1368
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001369 /*
1370 * Clear this page out of any CPU caches for coherent swap-in/out.
Chris Wilsone8a9c582016-12-18 15:37:20 +00001371 * We only want to do this on the first bind so that we do not stall
1372 * on an active context (which by nature is already on the GPU).
1373 */
1374 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001375 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001376 if (ret)
1377 return ret;
1378 }
1379
Chris Wilsonafeddf52017-02-27 13:59:13 +00001380 return i915_vma_pin(vma, 0, I915_GTT_MIN_ALIGNMENT,
1381 PIN_GLOBAL | PIN_HIGH);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001382}
1383
Chris Wilson3204c342017-04-27 11:46:51 +01001384static struct i915_vma *
1385alloc_context_vma(struct intel_engine_cs *engine)
1386{
1387 struct drm_i915_private *i915 = engine->i915;
1388 struct drm_i915_gem_object *obj;
1389 struct i915_vma *vma;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001390 int err;
Chris Wilson3204c342017-04-27 11:46:51 +01001391
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001392 obj = i915_gem_object_create(i915, engine->context_size);
Chris Wilson3204c342017-04-27 11:46:51 +01001393 if (IS_ERR(obj))
1394 return ERR_CAST(obj);
1395
Chris Wilsond2b4b972017-11-10 14:26:33 +00001396 if (engine->default_state) {
1397 void *defaults, *vaddr;
1398
1399 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1400 if (IS_ERR(vaddr)) {
1401 err = PTR_ERR(vaddr);
1402 goto err_obj;
1403 }
1404
1405 defaults = i915_gem_object_pin_map(engine->default_state,
1406 I915_MAP_WB);
1407 if (IS_ERR(defaults)) {
1408 err = PTR_ERR(defaults);
1409 goto err_map;
1410 }
1411
1412 memcpy(vaddr, defaults, engine->context_size);
1413
1414 i915_gem_object_unpin_map(engine->default_state);
1415 i915_gem_object_unpin_map(obj);
1416 }
1417
Chris Wilson3204c342017-04-27 11:46:51 +01001418 /*
1419 * Try to make the context utilize L3 as well as LLC.
1420 *
1421 * On VLV we don't have L3 controls in the PTEs so we
1422 * shouldn't touch the cache level, especially as that
1423 * would make the object snooped which might have a
1424 * negative performance impact.
1425 *
1426 * Snooping is required on non-llc platforms in execlist
1427 * mode, but since all GGTT accesses use PAT entry 0 we
1428 * get snooping anyway regardless of cache_level.
1429 *
1430 * This is only applicable for Ivy Bridge devices since
1431 * later platforms don't have L3 control bits in the PTE.
1432 */
1433 if (IS_IVYBRIDGE(i915)) {
1434 /* Ignore any error, regard it as a simple optimisation */
1435 i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
1436 }
1437
1438 vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001439 if (IS_ERR(vma)) {
1440 err = PTR_ERR(vma);
1441 goto err_obj;
1442 }
Chris Wilson3204c342017-04-27 11:46:51 +01001443
1444 return vma;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001445
1446err_map:
1447 i915_gem_object_unpin_map(obj);
1448err_obj:
1449 i915_gem_object_put(obj);
1450 return ERR_PTR(err);
Chris Wilson3204c342017-04-27 11:46:51 +01001451}
1452
Chris Wilson266a2402017-05-04 10:33:08 +01001453static struct intel_ring *
1454intel_ring_context_pin(struct intel_engine_cs *engine,
1455 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001456{
1457 struct intel_context *ce = &ctx->engine[engine->id];
1458 int ret;
1459
Chris Wilson91c8a322016-07-05 10:40:23 +01001460 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001461
Chris Wilson266a2402017-05-04 10:33:08 +01001462 if (likely(ce->pin_count++))
1463 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001464 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson0cb26a82016-06-24 14:55:53 +01001465
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001466 if (!ce->state && engine->context_size) {
Chris Wilson3204c342017-04-27 11:46:51 +01001467 struct i915_vma *vma;
1468
1469 vma = alloc_context_vma(engine);
1470 if (IS_ERR(vma)) {
1471 ret = PTR_ERR(vma);
Chris Wilson266a2402017-05-04 10:33:08 +01001472 goto err;
Chris Wilson3204c342017-04-27 11:46:51 +01001473 }
1474
1475 ce->state = vma;
1476 }
1477
Chris Wilson0cb26a82016-06-24 14:55:53 +01001478 if (ce->state) {
Chris Wilson72b72ae2017-02-10 10:14:22 +00001479 ret = context_pin(ctx);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001480 if (ret)
Chris Wilson266a2402017-05-04 10:33:08 +01001481 goto err;
Chris Wilson5d4bac52017-03-22 20:59:30 +00001482
Chris Wilson3d574a62017-10-13 21:26:16 +01001483 ce->state->obj->pin_global++;
Chris Wilson0cb26a82016-06-24 14:55:53 +01001484 }
1485
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001486 i915_gem_context_get(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001487
Chris Wilson266a2402017-05-04 10:33:08 +01001488out:
1489 /* One ringbuffer to rule them all */
1490 return engine->buffer;
1491
1492err:
Chris Wilson0cb26a82016-06-24 14:55:53 +01001493 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001494 return ERR_PTR(ret);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001495}
1496
Chris Wilsone8a9c582016-12-18 15:37:20 +00001497static void intel_ring_context_unpin(struct intel_engine_cs *engine,
1498 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001499{
1500 struct intel_context *ce = &ctx->engine[engine->id];
1501
Chris Wilson91c8a322016-07-05 10:40:23 +01001502 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001503 GEM_BUG_ON(ce->pin_count == 0);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001504
1505 if (--ce->pin_count)
1506 return;
1507
Chris Wilson3d574a62017-10-13 21:26:16 +01001508 if (ce->state) {
1509 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001510 i915_vma_unpin(ce->state);
Chris Wilson3d574a62017-10-13 21:26:16 +01001511 }
Chris Wilson0cb26a82016-06-24 14:55:53 +01001512
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001513 i915_gem_context_put(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001514}
1515
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001516static int intel_init_ring_buffer(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001517{
Chris Wilson32c04f12016-08-02 22:50:22 +01001518 struct intel_ring *ring;
Chris Wilson1a5788b2017-04-03 12:34:26 +01001519 int err;
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001520
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001521 intel_engine_setup_common(engine);
1522
Chris Wilson1a5788b2017-04-03 12:34:26 +01001523 err = intel_engine_init_common(engine);
1524 if (err)
1525 goto err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001526
Chris Wilsond822bb12017-04-03 12:34:25 +01001527 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
1528 if (IS_ERR(ring)) {
Chris Wilson1a5788b2017-04-03 12:34:26 +01001529 err = PTR_ERR(ring);
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001530 goto err;
Chris Wilsond822bb12017-04-03 12:34:25 +01001531 }
1532
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001533 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
Chris Wilson1a5788b2017-04-03 12:34:26 +01001534 err = intel_ring_pin(ring, engine->i915, I915_GTT_PAGE_SIZE);
1535 if (err)
1536 goto err_ring;
1537
1538 GEM_BUG_ON(engine->buffer);
Chris Wilson57e88532016-08-15 10:48:57 +01001539 engine->buffer = ring;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001540
Oscar Mateo8ee14972014-05-22 14:13:34 +01001541 return 0;
1542
Chris Wilson1a5788b2017-04-03 12:34:26 +01001543err_ring:
1544 intel_ring_free(ring);
Chris Wilson1a5788b2017-04-03 12:34:26 +01001545err:
1546 intel_engine_cleanup_common(engine);
1547 return err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001548}
1549
Chris Wilson7e37f882016-08-02 22:50:21 +01001550void intel_engine_cleanup(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001551{
Chris Wilson1a5788b2017-04-03 12:34:26 +01001552 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson33626e62010-10-29 16:18:36 +01001553
Chris Wilson1a5788b2017-04-03 12:34:26 +01001554 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
1555 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
John Harrison6402c332014-10-31 12:00:26 +00001556
Chris Wilson1a5788b2017-04-03 12:34:26 +01001557 intel_ring_unpin(engine->buffer);
1558 intel_ring_free(engine->buffer);
Chris Wilson78501ea2010-10-27 12:18:21 +01001559
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001560 if (engine->cleanup)
1561 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08001562
Chris Wilson96a945a2016-08-03 13:19:16 +01001563 intel_engine_cleanup_common(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001564
Akash Goel3b3f1652016-10-13 22:44:48 +05301565 dev_priv->engine[engine->id] = NULL;
1566 kfree(engine);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001567}
1568
Chris Wilson821ed7d2016-09-09 14:11:53 +01001569void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
1570{
1571 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301572 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001573
Chris Wilsone6ba9992017-04-25 14:00:49 +01001574 /* Restart from the beginning of the rings for convenience */
Chris Wilsonfe085f12017-03-21 10:25:52 +00001575 for_each_engine(engine, dev_priv, id)
Chris Wilsone6ba9992017-04-25 14:00:49 +01001576 intel_ring_reset(engine->buffer, 0);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001577}
1578
Chris Wilsonf73e7392016-12-18 15:37:24 +00001579static int ring_request_alloc(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00001580{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001581 u32 *cs;
Chris Wilson63103462016-04-28 09:56:49 +01001582
Chris Wilsone8a9c582016-12-18 15:37:20 +00001583 GEM_BUG_ON(!request->ctx->engine[request->engine->id].pin_count);
1584
Chris Wilson63103462016-04-28 09:56:49 +01001585 /* Flush enough space to reduce the likelihood of waiting after
1586 * we start building the request - in which case we will just
1587 * have to repeat work.
1588 */
Chris Wilsona0442462016-04-29 09:07:05 +01001589 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001590
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001591 cs = intel_ring_begin(request, 0);
1592 if (IS_ERR(cs))
1593 return PTR_ERR(cs);
Chris Wilson63103462016-04-28 09:56:49 +01001594
Chris Wilsona0442462016-04-29 09:07:05 +01001595 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001596 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00001597}
1598
Chris Wilson5e5655c2017-05-04 14:08:46 +01001599static noinline int wait_for_space(struct drm_i915_gem_request *req,
1600 unsigned int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001601{
Chris Wilson7e37f882016-08-02 22:50:21 +01001602 struct intel_ring *ring = req->ring;
Chris Wilson987046a2016-04-28 09:56:46 +01001603 struct drm_i915_gem_request *target;
Chris Wilsone95433c2016-10-28 13:58:27 +01001604 long timeout;
1605
1606 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson987046a2016-04-28 09:56:46 +01001607
Chris Wilson95aebcb2017-05-04 14:08:45 +01001608 if (intel_ring_update_space(ring) >= bytes)
Chris Wilson987046a2016-04-28 09:56:46 +01001609 return 0;
1610
1611 /*
1612 * Space is reserved in the ringbuffer for finalising the request,
1613 * as that cannot be allowed to fail. During request finalisation,
1614 * reserved_space is set to 0 to stop the overallocation and the
1615 * assumption is that then we never need to wait (which has the
1616 * risk of failing with EINTR).
1617 *
1618 * See also i915_gem_request_alloc() and i915_add_request().
1619 */
Chris Wilson0251a962016-04-28 09:56:47 +01001620 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01001621
Chris Wilson675d9ad2016-08-04 07:52:36 +01001622 list_for_each_entry(target, &ring->request_list, ring_link) {
Chris Wilson987046a2016-04-28 09:56:46 +01001623 /* Would completion of this request free enough space? */
Chris Wilson605d5b32017-05-04 14:08:44 +01001624 if (bytes <= __intel_ring_space(target->postfix,
1625 ring->emit, ring->size))
Chris Wilson987046a2016-04-28 09:56:46 +01001626 break;
1627 }
1628
Chris Wilson675d9ad2016-08-04 07:52:36 +01001629 if (WARN_ON(&target->ring_link == &ring->request_list))
Chris Wilson987046a2016-04-28 09:56:46 +01001630 return -ENOSPC;
1631
Chris Wilsone95433c2016-10-28 13:58:27 +01001632 timeout = i915_wait_request(target,
1633 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1634 MAX_SCHEDULE_TIMEOUT);
1635 if (timeout < 0)
1636 return timeout;
Chris Wilson7da844c2016-08-04 07:52:38 +01001637
Chris Wilson7da844c2016-08-04 07:52:38 +01001638 i915_gem_request_retire_upto(target);
1639
1640 intel_ring_update_space(ring);
1641 GEM_BUG_ON(ring->space < bytes);
1642 return 0;
Chris Wilson987046a2016-04-28 09:56:46 +01001643}
1644
Chris Wilson5e5655c2017-05-04 14:08:46 +01001645u32 *intel_ring_begin(struct drm_i915_gem_request *req,
1646 unsigned int num_dwords)
Chris Wilson987046a2016-04-28 09:56:46 +01001647{
Chris Wilson7e37f882016-08-02 22:50:21 +01001648 struct intel_ring *ring = req->ring;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001649 const unsigned int remain_usable = ring->effective_size - ring->emit;
1650 const unsigned int bytes = num_dwords * sizeof(u32);
1651 unsigned int need_wrap = 0;
1652 unsigned int total_bytes;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001653 u32 *cs;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001654
Chris Wilson6492ca72017-07-21 17:11:01 +01001655 /* Packets must be qword aligned. */
1656 GEM_BUG_ON(num_dwords & 1);
1657
Chris Wilson0251a962016-04-28 09:56:47 +01001658 total_bytes = bytes + req->reserved_space;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001659 GEM_BUG_ON(total_bytes > ring->effective_size);
John Harrison29b1b412015-06-18 13:10:09 +01001660
Chris Wilson5e5655c2017-05-04 14:08:46 +01001661 if (unlikely(total_bytes > remain_usable)) {
1662 const int remain_actual = ring->size - ring->emit;
1663
1664 if (bytes > remain_usable) {
1665 /*
1666 * Not enough space for the basic request. So need to
1667 * flush out the remainder and then wait for
1668 * base + reserved.
1669 */
1670 total_bytes += remain_actual;
1671 need_wrap = remain_actual | 1;
1672 } else {
1673 /*
1674 * The base request will fit but the reserved space
1675 * falls off the end. So we don't need an immediate
1676 * wrap and only need to effectively wait for the
1677 * reserved size from the start of ringbuffer.
1678 */
1679 total_bytes = req->reserved_space + remain_actual;
1680 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001681 }
1682
Chris Wilson5e5655c2017-05-04 14:08:46 +01001683 if (unlikely(total_bytes > ring->space)) {
1684 int ret = wait_for_space(req, total_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001685 if (unlikely(ret))
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001686 return ERR_PTR(ret);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001687 }
1688
Chris Wilson987046a2016-04-28 09:56:46 +01001689 if (unlikely(need_wrap)) {
Chris Wilson5e5655c2017-05-04 14:08:46 +01001690 need_wrap &= ~1;
1691 GEM_BUG_ON(need_wrap > ring->space);
1692 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001693
Chris Wilson987046a2016-04-28 09:56:46 +01001694 /* Fill the tail with MI_NOOP */
Chris Wilson5e5655c2017-05-04 14:08:46 +01001695 memset(ring->vaddr + ring->emit, 0, need_wrap);
Chris Wilsone6ba9992017-04-25 14:00:49 +01001696 ring->emit = 0;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001697 ring->space -= need_wrap;
Chris Wilson987046a2016-04-28 09:56:46 +01001698 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001699
Chris Wilsone6ba9992017-04-25 14:00:49 +01001700 GEM_BUG_ON(ring->emit > ring->size - bytes);
Chris Wilson605d5b32017-05-04 14:08:44 +01001701 GEM_BUG_ON(ring->space < bytes);
Chris Wilsone6ba9992017-04-25 14:00:49 +01001702 cs = ring->vaddr + ring->emit;
Chris Wilson01001862017-04-23 18:06:17 +01001703 GEM_DEBUG_EXEC(memset(cs, POISON_INUSE, bytes));
Chris Wilsone6ba9992017-04-25 14:00:49 +01001704 ring->emit += bytes;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001705 ring->space -= bytes;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001706
1707 return cs;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001708}
1709
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001710/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01001711int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001712{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001713 int num_dwords =
Chris Wilsone6ba9992017-04-25 14:00:49 +01001714 (req->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001715 u32 *cs;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001716
1717 if (num_dwords == 0)
1718 return 0;
1719
Chris Wilson18393f62014-04-09 09:19:40 +01001720 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001721 cs = intel_ring_begin(req, num_dwords);
1722 if (IS_ERR(cs))
1723 return PTR_ERR(cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001724
1725 while (num_dwords--)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001726 *cs++ = MI_NOOP;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001727
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001728 intel_ring_advance(req, cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001729
1730 return 0;
1731}
1732
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001733static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001734{
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001735 struct drm_i915_private *dev_priv = request->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001736
Chris Wilson76f84212016-06-30 15:33:45 +01001737 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
1738
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001739 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001740
Chris Wilson12f55812012-07-05 17:14:01 +01001741 /* Disable notification that the ring is IDLE. The GT
1742 * will then assume that it is busy and bring it out of rc6.
1743 */
Chris Wilson76f84212016-06-30 15:33:45 +01001744 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1745 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01001746
1747 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01001748 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01001749
1750 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson02b312d2017-04-11 11:13:37 +01001751 if (__intel_wait_for_register_fw(dev_priv,
1752 GEN6_BSD_SLEEP_PSMI_CONTROL,
1753 GEN6_BSD_SLEEP_INDICATOR,
1754 0,
1755 1000, 0, NULL))
Chris Wilson12f55812012-07-05 17:14:01 +01001756 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001757
Chris Wilson12f55812012-07-05 17:14:01 +01001758 /* Now that the ring is fully powered up, update the tail */
Chris Wilsonb0411e72016-08-02 22:50:34 +01001759 i9xx_submit_request(request);
Chris Wilson12f55812012-07-05 17:14:01 +01001760
1761 /* Let the ring send IDLE messages to the GT again,
1762 * and so let it sleep to conserve power when idle.
1763 */
Chris Wilson76f84212016-06-30 15:33:45 +01001764 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1765 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1766
1767 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001768}
1769
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001770static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001771{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001772 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001773
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001774 cs = intel_ring_begin(req, 4);
1775 if (IS_ERR(cs))
1776 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001777
Chris Wilson71a77e02011-02-02 12:13:49 +00001778 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01001779 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001780 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001781
1782 /* We always require a command barrier so that subsequent
1783 * commands, such as breadcrumb interrupts, are strictly ordered
1784 * wrt the contents of the write cache being flushed to memory
1785 * (and thus being coherent from the CPU).
1786 */
1787 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1788
Jesse Barnes9a289772012-10-26 09:42:42 -07001789 /*
1790 * Bspec vol 1c.5 - video engine command streamer:
1791 * "If ENABLED, all TLBs will be invalidated once the flush
1792 * operation is complete. This bit is only valid when the
1793 * Post-Sync Operation field is a value of 1h or 3h."
1794 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001795 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001796 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1797
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001798 *cs++ = cmd;
1799 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilsonc0336662016-05-06 15:40:21 +01001800 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001801 *cs++ = 0; /* upper addr */
1802 *cs++ = 0; /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001803 } else {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001804 *cs++ = 0;
1805 *cs++ = MI_NOOP;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001806 }
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001807 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001808 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001809}
1810
1811static int
Chris Wilson803688b2016-08-02 22:50:27 +01001812gen8_emit_bb_start(struct drm_i915_gem_request *req,
1813 u64 offset, u32 len,
1814 unsigned int dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001815{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001816 bool ppgtt = USES_PPGTT(req->i915) &&
John Harrison8e004ef2015-02-13 11:48:10 +00001817 !(dispatch_flags & I915_DISPATCH_SECURE);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001818 u32 *cs;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001819
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001820 cs = intel_ring_begin(req, 4);
1821 if (IS_ERR(cs))
1822 return PTR_ERR(cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001823
1824 /* FIXME(BDW): Address space and security selectors. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001825 *cs++ = MI_BATCH_BUFFER_START_GEN8 | (ppgtt << 8) | (dispatch_flags &
1826 I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
1827 *cs++ = lower_32_bits(offset);
1828 *cs++ = upper_32_bits(offset);
1829 *cs++ = MI_NOOP;
1830 intel_ring_advance(req, cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001831
1832 return 0;
1833}
1834
1835static int
Chris Wilson803688b2016-08-02 22:50:27 +01001836hsw_emit_bb_start(struct drm_i915_gem_request *req,
1837 u64 offset, u32 len,
1838 unsigned int dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001839{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001840 u32 *cs;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001841
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001842 cs = intel_ring_begin(req, 2);
1843 if (IS_ERR(cs))
1844 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001845
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001846 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1847 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
1848 (dispatch_flags & I915_DISPATCH_RS ?
1849 MI_BATCH_RESOURCE_STREAMER : 0);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001850 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001851 *cs++ = offset;
1852 intel_ring_advance(req, cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001853
1854 return 0;
1855}
1856
1857static int
Chris Wilson803688b2016-08-02 22:50:27 +01001858gen6_emit_bb_start(struct drm_i915_gem_request *req,
1859 u64 offset, u32 len,
1860 unsigned int dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001861{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001862 u32 *cs;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001863
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001864 cs = intel_ring_begin(req, 2);
1865 if (IS_ERR(cs))
1866 return PTR_ERR(cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001867
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001868 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1869 0 : MI_BATCH_NON_SECURE_I965);
Akshay Joshi0206e352011-08-16 15:34:10 -04001870 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001871 *cs++ = offset;
1872 intel_ring_advance(req, cs);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001873
Akshay Joshi0206e352011-08-16 15:34:10 -04001874 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001875}
1876
Chris Wilson549f7362010-10-19 11:19:32 +01001877/* Blitter support (SandyBridge+) */
1878
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001879static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan hai8d192152010-11-02 16:31:01 +08001880{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001881 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001882
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001883 cs = intel_ring_begin(req, 4);
1884 if (IS_ERR(cs))
1885 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001886
Chris Wilson71a77e02011-02-02 12:13:49 +00001887 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01001888 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001889 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001890
1891 /* We always require a command barrier so that subsequent
1892 * commands, such as breadcrumb interrupts, are strictly ordered
1893 * wrt the contents of the write cache being flushed to memory
1894 * (and thus being coherent from the CPU).
1895 */
1896 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1897
Jesse Barnes9a289772012-10-26 09:42:42 -07001898 /*
1899 * Bspec vol 1c.3 - blitter engine command streamer:
1900 * "If ENABLED, all TLBs will be invalidated once the flush
1901 * operation is complete. This bit is only valid when the
1902 * Post-Sync Operation field is a value of 1h or 3h."
1903 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001904 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001905 cmd |= MI_INVALIDATE_TLB;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001906 *cs++ = cmd;
1907 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilsonc0336662016-05-06 15:40:21 +01001908 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001909 *cs++ = 0; /* upper addr */
1910 *cs++ = 0; /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001911 } else {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001912 *cs++ = 0;
1913 *cs++ = MI_NOOP;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001914 }
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001915 intel_ring_advance(req, cs);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001916
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001917 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001918}
1919
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001920static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
1921 struct intel_engine_cs *engine)
1922{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001923 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001924 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001925
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001926 if (!i915_modparams.semaphores)
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001927 return;
1928
Chris Wilson51d545d2016-08-15 10:49:02 +01001929 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
1930 struct i915_vma *vma;
1931
Chris Wilsonf51455d2017-01-10 14:47:34 +00001932 obj = i915_gem_object_create(dev_priv, PAGE_SIZE);
Chris Wilson51d545d2016-08-15 10:49:02 +01001933 if (IS_ERR(obj))
1934 goto err;
1935
Chris Wilsona01cb372017-01-16 15:21:30 +00001936 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson51d545d2016-08-15 10:49:02 +01001937 if (IS_ERR(vma))
1938 goto err_obj;
1939
1940 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1941 if (ret)
1942 goto err_obj;
1943
1944 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
1945 if (ret)
1946 goto err_obj;
1947
1948 dev_priv->semaphore = vma;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001949 }
1950
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001951 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001952 u32 offset = i915_ggtt_offset(dev_priv->semaphore);
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001953
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001954 engine->semaphore.sync_to = gen8_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001955 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001956
1957 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001958 u32 ring_offset;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001959
1960 if (i != engine->id)
1961 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
1962 else
1963 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
1964
1965 engine->semaphore.signal_ggtt[i] = ring_offset;
1966 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001967 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001968 engine->semaphore.sync_to = gen6_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001969 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001970
1971 /*
1972 * The current semaphore is only applied on pre-gen8
1973 * platform. And there is no VCS2 ring on the pre-gen8
1974 * platform. So the semaphore between RCS and VCS2 is
1975 * initialized as INVALID. Gen8 will initialize the
1976 * sema between VCS2 and RCS later.
1977 */
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001978 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001979 static const struct {
1980 u32 wait_mbox;
1981 i915_reg_t mbox_reg;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001982 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
1983 [RCS_HW] = {
1984 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
1985 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
1986 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001987 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001988 [VCS_HW] = {
1989 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
1990 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
1991 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001992 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001993 [BCS_HW] = {
1994 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
1995 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
1996 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001997 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001998 [VECS_HW] = {
1999 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2000 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2001 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002002 },
2003 };
2004 u32 wait_mbox;
2005 i915_reg_t mbox_reg;
2006
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002007 if (i == engine->hw_id) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002008 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2009 mbox_reg = GEN6_NOSYNC;
2010 } else {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002011 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
2012 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002013 }
2014
2015 engine->semaphore.mbox.wait[i] = wait_mbox;
2016 engine->semaphore.mbox.signal[i] = mbox_reg;
2017 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002018 }
Chris Wilson51d545d2016-08-15 10:49:02 +01002019
2020 return;
2021
2022err_obj:
2023 i915_gem_object_put(obj);
2024err:
2025 DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00002026 i915_modparams.semaphores = 0;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002027}
2028
Chris Wilsoned003072016-07-01 09:18:13 +01002029static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2030 struct intel_engine_cs *engine)
2031{
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002032 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2033
Chris Wilsoned003072016-07-01 09:18:13 +01002034 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002035 engine->irq_enable = gen8_irq_enable;
2036 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002037 engine->irq_seqno_barrier = gen6_seqno_barrier;
2038 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002039 engine->irq_enable = gen6_irq_enable;
2040 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002041 engine->irq_seqno_barrier = gen6_seqno_barrier;
2042 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002043 engine->irq_enable = gen5_irq_enable;
2044 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002045 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002046 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002047 engine->irq_enable = i9xx_irq_enable;
2048 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002049 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002050 engine->irq_enable = i8xx_irq_enable;
2051 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002052 }
2053}
2054
Chris Wilsonff44ad52017-03-16 17:13:03 +00002055static void i9xx_set_default_submission(struct intel_engine_cs *engine)
2056{
2057 engine->submit_request = i9xx_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01002058 engine->cancel_requests = cancel_requests;
Chris Wilsonaba5e272017-10-25 15:39:41 +01002059
2060 engine->park = NULL;
2061 engine->unpark = NULL;
Chris Wilsonff44ad52017-03-16 17:13:03 +00002062}
2063
2064static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
2065{
Chris Wilsonaba5e272017-10-25 15:39:41 +01002066 i9xx_set_default_submission(engine);
Chris Wilsonff44ad52017-03-16 17:13:03 +00002067 engine->submit_request = gen6_bsd_submit_request;
2068}
2069
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002070static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2071 struct intel_engine_cs *engine)
2072{
Chris Wilson618e4ca2016-08-02 22:50:35 +01002073 intel_ring_init_irq(dev_priv, engine);
2074 intel_ring_init_semaphores(dev_priv, engine);
2075
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002076 engine->init_hw = init_ring_common;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002077 engine->reset_hw = reset_ring_common;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002078
Chris Wilsone8a9c582016-12-18 15:37:20 +00002079 engine->context_pin = intel_ring_context_pin;
2080 engine->context_unpin = intel_ring_context_unpin;
2081
Chris Wilsonf73e7392016-12-18 15:37:24 +00002082 engine->request_alloc = ring_request_alloc;
2083
Chris Wilson9b81d552016-10-28 13:58:50 +01002084 engine->emit_breadcrumb = i9xx_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002085 engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00002086 if (i915_modparams.semaphores) {
Chris Wilson98f29e82016-10-28 13:58:51 +01002087 int num_rings;
2088
Chris Wilson9b81d552016-10-28 13:58:50 +01002089 engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002090
Tvrtko Ursulinc58949f2017-06-19 11:59:17 +01002091 num_rings = INTEL_INFO(dev_priv)->num_rings - 1;
Chris Wilson98f29e82016-10-28 13:58:51 +01002092 if (INTEL_GEN(dev_priv) >= 8) {
2093 engine->emit_breadcrumb_sz += num_rings * 6;
2094 } else {
2095 engine->emit_breadcrumb_sz += num_rings * 3;
2096 if (num_rings & 1)
2097 engine->emit_breadcrumb_sz++;
2098 }
2099 }
Chris Wilsonff44ad52017-03-16 17:13:03 +00002100
2101 engine->set_default_submission = i9xx_set_default_submission;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002102
2103 if (INTEL_GEN(dev_priv) >= 8)
Chris Wilson803688b2016-08-02 22:50:27 +01002104 engine->emit_bb_start = gen8_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002105 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson803688b2016-08-02 22:50:27 +01002106 engine->emit_bb_start = gen6_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002107 else if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson803688b2016-08-02 22:50:27 +01002108 engine->emit_bb_start = i965_emit_bb_start;
Jani Nikula2a307c22016-11-30 17:43:04 +02002109 else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002110 engine->emit_bb_start = i830_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002111 else
Chris Wilson803688b2016-08-02 22:50:27 +01002112 engine->emit_bb_start = i915_emit_bb_start;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002113}
2114
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002115int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002116{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002117 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07002118 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002119
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002120 intel_ring_default_vfuncs(dev_priv, engine);
2121
Chris Wilson61ff75a2016-07-01 17:23:28 +01002122 if (HAS_L3_DPF(dev_priv))
2123 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002124
Chris Wilsonc0336662016-05-06 15:40:21 +01002125 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002126 engine->init_context = intel_rcs_ctx_init;
Chris Wilson9b81d552016-10-28 13:58:50 +01002127 engine->emit_breadcrumb = gen8_render_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002128 engine->emit_breadcrumb_sz = gen8_render_emit_breadcrumb_sz;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002129 engine->emit_flush = gen8_render_ring_flush;
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00002130 if (i915_modparams.semaphores) {
Chris Wilson98f29e82016-10-28 13:58:51 +01002131 int num_rings;
2132
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002133 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilson98f29e82016-10-28 13:58:51 +01002134
Tvrtko Ursulinc58949f2017-06-19 11:59:17 +01002135 num_rings = INTEL_INFO(dev_priv)->num_rings - 1;
Chris Wilson6f9b8502017-03-24 15:17:24 +00002136 engine->emit_breadcrumb_sz += num_rings * 8;
Chris Wilson98f29e82016-10-28 13:58:51 +01002137 }
Chris Wilsonc0336662016-05-06 15:40:21 +01002138 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002139 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002140 engine->emit_flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002141 if (IS_GEN6(dev_priv))
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002142 engine->emit_flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002143 } else if (IS_GEN5(dev_priv)) {
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002144 engine->emit_flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002145 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002146 if (INTEL_GEN(dev_priv) < 4)
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002147 engine->emit_flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002148 else
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002149 engine->emit_flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002150 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002151 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002152
Chris Wilsonc0336662016-05-06 15:40:21 +01002153 if (IS_HASWELL(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002154 engine->emit_bb_start = hsw_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002155
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002156 engine->init_hw = init_render_ring;
2157 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002158
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002159 ret = intel_init_ring_buffer(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002160 if (ret)
2161 return ret;
2162
Chris Wilsonf8973c22016-07-01 17:23:21 +01002163 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonf51455d2017-01-10 14:47:34 +00002164 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Chris Wilson7d5ea802016-07-01 17:23:20 +01002165 if (ret)
2166 return ret;
2167 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01002168 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002169 if (ret)
2170 return ret;
2171 }
2172
2173 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002174}
2175
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002176int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002177{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002178 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002179
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002180 intel_ring_default_vfuncs(dev_priv, engine);
2181
Chris Wilsonc0336662016-05-06 15:40:21 +01002182 if (INTEL_GEN(dev_priv) >= 6) {
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002183 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002184 if (IS_GEN6(dev_priv))
Chris Wilsonff44ad52017-03-16 17:13:03 +00002185 engine->set_default_submission = gen6_bsd_set_default_submission;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002186 engine->emit_flush = gen6_bsd_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002187 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002188 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002189 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002190 engine->mmio_base = BSD_RING_BASE;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002191 engine->emit_flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002192 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002193 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002194 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002195 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002196 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002197
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002198 return intel_init_ring_buffer(engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002199}
Chris Wilson549f7362010-10-19 11:19:32 +01002200
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002201int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
Chris Wilson549f7362010-10-19 11:19:32 +01002202{
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_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002208 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002209 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002210
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002211 return intel_init_ring_buffer(engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002212}
Chris Wilsona7b97612012-07-20 12:41:08 +01002213
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002214int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002215{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002216 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002217
2218 intel_ring_default_vfuncs(dev_priv, engine);
2219
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002220 engine->emit_flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002221
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002222 if (INTEL_GEN(dev_priv) < 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002223 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01002224 engine->irq_enable = hsw_vebox_irq_enable;
2225 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002226 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002227
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002228 return intel_init_ring_buffer(engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002229}