blob: d9b8d17c3fc679b54bfef38542d2f1e2723f9dbe [file] [log] [blame]
Eric Anholt62fdfea2010-05-21 13:26:39 -07001/*
2 * Copyright © 2008-2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
27 *
28 */
29
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +080030#include <linux/log2.h>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070032#include "i915_drv.h"
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/i915_drm.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070034#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010035#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070036
Chris Wilsona0442462016-04-29 09:07:05 +010037/* Rough estimate of the typical request size, performing a flush,
38 * set-context and then emitting the batch.
39 */
40#define LEGACY_REQUEST_SIZE 200
41
Tvrtko Ursulin2f35afe2017-02-16 12:23:21 +000042static int __intel_ring_space(int head, int tail, int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010043{
Dave Gordon4f547412014-11-27 11:22:48 +000044 int space = head - tail;
45 if (space <= 0)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010046 space += size;
Dave Gordon4f547412014-11-27 11:22:48 +000047 return space - I915_RING_FREE_SPACE;
Chris Wilson1cf0ba12014-05-05 09:07:33 +010048}
49
Chris Wilson32c04f12016-08-02 22:50:22 +010050void intel_ring_update_space(struct intel_ring *ring)
Dave Gordonebd0fd42014-11-27 11:22:49 +000051{
Chris Wilson32c04f12016-08-02 22:50:22 +010052 if (ring->last_retired_head != -1) {
53 ring->head = ring->last_retired_head;
54 ring->last_retired_head = -1;
Dave Gordonebd0fd42014-11-27 11:22:49 +000055 }
56
Chris Wilson32c04f12016-08-02 22:50:22 +010057 ring->space = __intel_ring_space(ring->head & HEAD_ADDR,
58 ring->tail, ring->size);
Dave Gordonebd0fd42014-11-27 11:22:49 +000059}
60
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000061static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010062gen2_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010063{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000064 u32 cmd, *cs;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010065
66 cmd = MI_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010067
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010068 if (mode & EMIT_INVALIDATE)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010069 cmd |= MI_READ_FLUSH;
70
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000071 cs = intel_ring_begin(req, 2);
72 if (IS_ERR(cs))
73 return PTR_ERR(cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010074
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000075 *cs++ = cmd;
76 *cs++ = MI_NOOP;
77 intel_ring_advance(req, cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010078
79 return 0;
80}
81
82static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010083gen4_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Eric Anholt62fdfea2010-05-21 13:26:39 -070084{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000085 u32 cmd, *cs;
Chris Wilson6f392d52010-08-07 11:01:22 +010086
Chris Wilson36d527d2011-03-19 22:26:49 +000087 /*
88 * read/write caches:
89 *
90 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
91 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
92 * also flushed at 2d versus 3d pipeline switches.
93 *
94 * read-only caches:
95 *
96 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
97 * MI_READ_FLUSH is set, and is always flushed on 965.
98 *
99 * I915_GEM_DOMAIN_COMMAND may not exist?
100 *
101 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
102 * invalidated when MI_EXE_FLUSH is set.
103 *
104 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
105 * invalidated with every MI_FLUSH.
106 *
107 * TLBs:
108 *
109 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
110 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
111 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
112 * are flushed at any MI_FLUSH.
113 */
114
Chris Wilsonb5321f32016-08-02 22:50:18 +0100115 cmd = MI_FLUSH;
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100116 if (mode & EMIT_INVALIDATE) {
Chris Wilson36d527d2011-03-19 22:26:49 +0000117 cmd |= MI_EXE_FLUSH;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100118 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
119 cmd |= MI_INVALIDATE_ISP;
120 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000121
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000122 cs = intel_ring_begin(req, 2);
123 if (IS_ERR(cs))
124 return PTR_ERR(cs);
Chris Wilson36d527d2011-03-19 22:26:49 +0000125
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000126 *cs++ = cmd;
127 *cs++ = MI_NOOP;
128 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000129
130 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800131}
132
Jesse Barnes8d315282011-10-16 10:23:31 +0200133/**
134 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
135 * implementing two workarounds on gen6. From section 1.4.7.1
136 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
137 *
138 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
139 * produced by non-pipelined state commands), software needs to first
140 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
141 * 0.
142 *
143 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
144 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
145 *
146 * And the workaround for these two requires this workaround first:
147 *
148 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
149 * BEFORE the pipe-control with a post-sync op and no write-cache
150 * flushes.
151 *
152 * And this last workaround is tricky because of the requirements on
153 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
154 * volume 2 part 1:
155 *
156 * "1 of the following must also be set:
157 * - Render Target Cache Flush Enable ([12] of DW1)
158 * - Depth Cache Flush Enable ([0] of DW1)
159 * - Stall at Pixel Scoreboard ([1] of DW1)
160 * - Depth Stall ([13] of DW1)
161 * - Post-Sync Operation ([13] of DW1)
162 * - Notify Enable ([8] of DW1)"
163 *
164 * The cache flushes require the workaround flush that triggered this
165 * one, so we can't use it. Depth stall would trigger the same.
166 * Post-sync nonzero is what triggered this second workaround, so we
167 * can't use that one either. Notify enable is IRQs, which aren't
168 * really our business. That leaves only stall at scoreboard.
169 */
170static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100171intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200172{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100173 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100174 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000175 u32 *cs;
Jesse Barnes8d315282011-10-16 10:23:31 +0200176
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000177 cs = intel_ring_begin(req, 6);
178 if (IS_ERR(cs))
179 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200180
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000181 *cs++ = GFX_OP_PIPE_CONTROL(5);
182 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
183 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
184 *cs++ = 0; /* low dword */
185 *cs++ = 0; /* high dword */
186 *cs++ = MI_NOOP;
187 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200188
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000189 cs = intel_ring_begin(req, 6);
190 if (IS_ERR(cs))
191 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200192
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000193 *cs++ = GFX_OP_PIPE_CONTROL(5);
194 *cs++ = PIPE_CONTROL_QW_WRITE;
195 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
196 *cs++ = 0;
197 *cs++ = 0;
198 *cs++ = MI_NOOP;
199 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200200
201 return 0;
202}
203
204static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100205gen6_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Jesse Barnes8d315282011-10-16 10:23:31 +0200206{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100207 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100208 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000209 u32 *cs, flags = 0;
Jesse Barnes8d315282011-10-16 10:23:31 +0200210 int ret;
211
Paulo Zanonib3111502012-08-17 18:35:42 -0300212 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100213 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300214 if (ret)
215 return ret;
216
Jesse Barnes8d315282011-10-16 10:23:31 +0200217 /* Just flush everything. Experiments have shown that reducing the
218 * number of bits based on the write domains has little performance
219 * impact.
220 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100221 if (mode & EMIT_FLUSH) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100222 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
223 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
224 /*
225 * Ensure that any following seqno writes only happen
226 * when the render cache is indeed flushed.
227 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200228 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100229 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100230 if (mode & EMIT_INVALIDATE) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100231 flags |= PIPE_CONTROL_TLB_INVALIDATE;
232 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
233 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
234 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
235 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
236 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
237 /*
238 * TLB invalidate requires a post-sync write.
239 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700240 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100241 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200242
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000243 cs = intel_ring_begin(req, 4);
244 if (IS_ERR(cs))
245 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200246
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000247 *cs++ = GFX_OP_PIPE_CONTROL(4);
248 *cs++ = flags;
249 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
250 *cs++ = 0;
251 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200252
253 return 0;
254}
255
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100256static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100257gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300258{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000259 u32 *cs;
Paulo Zanonif3987632012-08-17 18:35:43 -0300260
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000261 cs = intel_ring_begin(req, 4);
262 if (IS_ERR(cs))
263 return PTR_ERR(cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300264
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000265 *cs++ = GFX_OP_PIPE_CONTROL(4);
266 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
267 *cs++ = 0;
268 *cs++ = 0;
269 intel_ring_advance(req, cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300270
271 return 0;
272}
273
274static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100275gen7_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300276{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100277 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100278 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000279 u32 *cs, flags = 0;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300280
Paulo Zanonif3987632012-08-17 18:35:43 -0300281 /*
282 * Ensure that any following seqno writes only happen when the render
283 * cache is indeed flushed.
284 *
285 * Workaround: 4th PIPE_CONTROL command (except the ones with only
286 * read-cache invalidate bits set) must have the CS_STALL bit set. We
287 * don't try to be clever and just set it unconditionally.
288 */
289 flags |= PIPE_CONTROL_CS_STALL;
290
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300291 /* Just flush everything. Experiments have shown that reducing the
292 * number of bits based on the write domains has little performance
293 * impact.
294 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100295 if (mode & EMIT_FLUSH) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300296 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
297 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800298 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100299 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300300 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100301 if (mode & EMIT_INVALIDATE) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300302 flags |= PIPE_CONTROL_TLB_INVALIDATE;
303 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
304 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
305 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
306 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
307 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000308 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300309 /*
310 * TLB invalidate requires a post-sync write.
311 */
312 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200313 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300314
Chris Wilsonadd284a2014-12-16 08:44:32 +0000315 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
316
Paulo Zanonif3987632012-08-17 18:35:43 -0300317 /* Workaround: we must issue a pipe_control with CS-stall bit
318 * set before a pipe_control command that has the state cache
319 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100320 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300321 }
322
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000323 cs = intel_ring_begin(req, 4);
324 if (IS_ERR(cs))
325 return PTR_ERR(cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300326
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000327 *cs++ = GFX_OP_PIPE_CONTROL(4);
328 *cs++ = flags;
329 *cs++ = scratch_addr;
330 *cs++ = 0;
331 intel_ring_advance(req, cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300332
333 return 0;
334}
335
Ben Widawskya5f3d682013-11-02 21:07:27 -0700336static int
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000337gen8_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300338{
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000339 u32 flags;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000340 u32 *cs;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300341
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000342 cs = intel_ring_begin(req, mode & EMIT_INVALIDATE ? 12 : 6);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000343 if (IS_ERR(cs))
344 return PTR_ERR(cs);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300345
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000346 flags = PIPE_CONTROL_CS_STALL;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700347
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100348 if (mode & EMIT_FLUSH) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700349 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
350 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800351 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100352 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700353 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100354 if (mode & EMIT_INVALIDATE) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700355 flags |= PIPE_CONTROL_TLB_INVALIDATE;
356 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
357 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
358 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
359 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
360 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
361 flags |= PIPE_CONTROL_QW_WRITE;
362 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800363
364 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000365 cs = gen8_emit_pipe_control(cs,
366 PIPE_CONTROL_CS_STALL |
367 PIPE_CONTROL_STALL_AT_SCOREBOARD,
368 0);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700369 }
370
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000371 cs = gen8_emit_pipe_control(cs, flags,
372 i915_ggtt_offset(req->engine->scratch) +
373 2 * CACHELINE_BYTES);
374
375 intel_ring_advance(req, cs);
376
377 return 0;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700378}
379
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000380static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200381{
Chris Wilsonc0336662016-05-06 15:40:21 +0100382 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200383 u32 addr;
384
385 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100386 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200387 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
388 I915_WRITE(HWS_PGA, addr);
389}
390
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000391static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000392{
Chris Wilsonc0336662016-05-06 15:40:21 +0100393 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200394 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000395
396 /* The ring status page addresses are no longer next to the rest of
397 * the ring registers as of gen7.
398 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100399 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000400 switch (engine->id) {
Damien Lespiauaf75f262015-02-10 19:32:17 +0000401 case RCS:
402 mmio = RENDER_HWS_PGA_GEN7;
403 break;
404 case BCS:
405 mmio = BLT_HWS_PGA_GEN7;
406 break;
407 /*
408 * VCS2 actually doesn't exist on Gen7. Only shut up
409 * gcc switch check warning
410 */
411 case VCS2:
412 case VCS:
413 mmio = BSD_HWS_PGA_GEN7;
414 break;
415 case VECS:
416 mmio = VEBOX_HWS_PGA_GEN7;
417 break;
418 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100419 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000420 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000421 } else {
422 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000423 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000424 }
425
Chris Wilson57e88532016-08-15 10:48:57 +0100426 I915_WRITE(mmio, engine->status_page.ggtt_offset);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000427 POSTING_READ(mmio);
428
429 /*
430 * Flush the TLB for this page
431 *
432 * FIXME: These two bits have disappeared on gen8, so a question
433 * arises: do we still need this and if so how should we go about
434 * invalidating the TLB?
435 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100436 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000437 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000438
439 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000440 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000441
442 I915_WRITE(reg,
443 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
444 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100445 if (intel_wait_for_register(dev_priv,
446 reg, INSTPM_SYNC_FLUSH, 0,
447 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000448 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000449 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000450 }
451}
452
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000453static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100454{
Chris Wilsonc0336662016-05-06 15:40:21 +0100455 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100456
Chris Wilson21a2c582016-08-15 10:49:11 +0100457 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000458 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100459 if (intel_wait_for_register(dev_priv,
460 RING_MI_MODE(engine->mmio_base),
461 MODE_IDLE,
462 MODE_IDLE,
463 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000464 DRM_ERROR("%s : timed out trying to stop ring\n",
465 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100466 /* Sometimes we observe that the idle flag is not
467 * set even though the ring is empty. So double
468 * check before giving up.
469 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000470 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100471 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100472 }
473 }
474
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000475 I915_WRITE_CTL(engine, 0);
476 I915_WRITE_HEAD(engine, 0);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100477 I915_WRITE_TAIL(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100478
Chris Wilson21a2c582016-08-15 10:49:11 +0100479 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000480 (void)I915_READ_CTL(engine);
481 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
Chris Wilson9991ae72014-04-02 16:36:07 +0100482 }
483
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000484 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100485}
486
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000487static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800488{
Chris Wilsonc0336662016-05-06 15:40:21 +0100489 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +0100490 struct intel_ring *ring = engine->buffer;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200491 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800492
Mika Kuoppala59bad942015-01-16 11:34:40 +0200493 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200494
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000495 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100496 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000497 DRM_DEBUG_KMS("%s head not reset to zero "
498 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000499 engine->name,
500 I915_READ_CTL(engine),
501 I915_READ_HEAD(engine),
502 I915_READ_TAIL(engine),
503 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800504
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000505 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000506 DRM_ERROR("failed to set %s head to zero "
507 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000508 engine->name,
509 I915_READ_CTL(engine),
510 I915_READ_HEAD(engine),
511 I915_READ_TAIL(engine),
512 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100513 ret = -EIO;
514 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000515 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700516 }
517
Carlos Santa31776592016-08-17 12:30:56 -0700518 if (HWS_NEEDS_PHYSICAL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000519 ring_setup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -0700520 else
521 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100522
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100523 intel_engine_reset_breadcrumbs(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +0100524
Jiri Kosinaece4a172014-08-07 16:29:53 +0200525 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000526 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200527
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200528 /* Initialize the ring. This must happen _after_ we've cleared the ring
529 * registers with the above sequence (the readback of the HEAD registers
530 * also enforces ordering), otherwise the hw might lose the new ring
531 * register values. */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100532 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
Chris Wilson95468892014-08-07 15:39:54 +0100533
534 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000535 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100536 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000537 engine->name, I915_READ_HEAD(engine));
Chris Wilson821ed7d2016-09-09 14:11:53 +0100538
539 intel_ring_update_space(ring);
540 I915_WRITE_HEAD(engine, ring->head);
541 I915_WRITE_TAIL(engine, ring->tail);
542 (void)I915_READ_TAIL(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100543
Chris Wilson62ae14b2016-10-04 21:11:25 +0100544 I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800545
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800546 /* If the head is still not zero, the ring is dead */
Chris Wilson821ed7d2016-09-09 14:11:53 +0100547 if (intel_wait_for_register_fw(dev_priv, RING_CTL(engine->mmio_base),
548 RING_VALID, RING_VALID,
549 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000550 DRM_ERROR("%s initialization failed "
Chris Wilson821ed7d2016-09-09 14:11:53 +0100551 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000552 engine->name,
553 I915_READ_CTL(engine),
554 I915_READ_CTL(engine) & RING_VALID,
Chris Wilson821ed7d2016-09-09 14:11:53 +0100555 I915_READ_HEAD(engine), ring->head,
556 I915_READ_TAIL(engine), ring->tail,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000557 I915_READ_START(engine),
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100558 i915_ggtt_offset(ring->vma));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200559 ret = -EIO;
560 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800561 }
562
Tomas Elffc0768c2016-03-21 16:26:59 +0000563 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100564
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200565out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200566 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200567
568 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700569}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800570
Chris Wilson821ed7d2016-09-09 14:11:53 +0100571static void reset_ring_common(struct intel_engine_cs *engine,
572 struct drm_i915_gem_request *request)
573{
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000574 /* Try to restore the logical GPU state to match the continuation
575 * of the request queue. If we skip the context/PD restore, then
576 * the next request may try to execute assuming that its context
577 * is valid and loaded on the GPU and so may try to access invalid
578 * memory, prompting repeated GPU hangs.
579 *
580 * If the request was guilty, we still restore the logical state
581 * in case the next request requires it (e.g. the aliasing ppgtt),
582 * but skip over the hung batch.
583 *
584 * If the request was innocent, we try to replay the request with
585 * the restored context.
586 */
587 if (request) {
588 struct drm_i915_private *dev_priv = request->i915;
589 struct intel_context *ce = &request->ctx->engine[engine->id];
590 struct i915_hw_ppgtt *ppgtt;
Chris Wilson821ed7d2016-09-09 14:11:53 +0100591
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000592 /* FIXME consider gen8 reset */
593
594 if (ce->state) {
595 I915_WRITE(CCID,
596 i915_ggtt_offset(ce->state) |
597 BIT(8) /* must be set! */ |
598 CCID_EXTENDED_STATE_SAVE |
599 CCID_EXTENDED_STATE_RESTORE |
600 CCID_EN);
601 }
602
603 ppgtt = request->ctx->ppgtt ?: engine->i915->mm.aliasing_ppgtt;
604 if (ppgtt) {
605 u32 pd_offset = ppgtt->pd.base.ggtt_offset << 10;
606
607 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
608 I915_WRITE(RING_PP_DIR_BASE(engine), pd_offset);
609
610 /* Wait for the PD reload to complete */
611 if (intel_wait_for_register(dev_priv,
612 RING_PP_DIR_BASE(engine),
613 BIT(0), 0,
614 10))
615 DRM_ERROR("Wait for reload of ppgtt page-directory timed out\n");
616
617 ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
618 }
619
620 /* If the rq hung, jump to its breadcrumb and skip the batch */
621 if (request->fence.error == -EIO) {
622 struct intel_ring *ring = request->ring;
623
624 ring->head = request->postfix;
625 ring->last_retired_head = -1;
626 }
627 } else {
628 engine->legacy_active_context = NULL;
629 }
Chris Wilson821ed7d2016-09-09 14:11:53 +0100630}
631
John Harrison87531812015-05-29 17:43:44 +0100632static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100633{
634 int ret;
635
John Harrisone2be4fa2015-05-29 17:43:54 +0100636 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100637 if (ret != 0)
638 return ret;
639
Chris Wilson4e50f082016-10-28 13:58:31 +0100640 ret = i915_gem_render_state_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100641 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000642 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100643
Chris Wilsone26e1b92016-01-29 16:49:05 +0000644 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100645}
646
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000647static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800648{
Chris Wilsonc0336662016-05-06 15:40:21 +0100649 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000650 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +0200651 if (ret)
652 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800653
Akash Goel61a563a2014-03-25 18:01:50 +0530654 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100655 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200656 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000657
658 /* We need to disable the AsyncFlip performance optimisations in order
659 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
660 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +0100661 *
Ville Syrjälä2441f872015-06-02 15:37:37 +0300662 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000663 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100664 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000665 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
666
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000667 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +0530668 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +0100669 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000670 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +0000671 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000672
Akash Goel01fa0302014-03-24 23:00:04 +0530673 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +0100674 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000675 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +0530676 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000677 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +0100678
Chris Wilsonc0336662016-05-06 15:40:21 +0100679 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700680 /* From the Sandybridge PRM, volume 1 part 3, page 24:
681 * "If this bit is set, STCunit will have LRA as replacement
682 * policy. [...] This bit must be reset. LRA replacement
683 * policy is not supported."
684 */
685 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200686 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -0800687 }
688
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100689 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200690 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +0000691
Ville Syrjälä035ea402016-07-12 19:24:47 +0300692 if (INTEL_INFO(dev_priv)->gen >= 6)
693 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -0700694
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000695 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800696}
697
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000698static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000699{
Chris Wilsonc0336662016-05-06 15:40:21 +0100700 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700701
Chris Wilson19880c42016-08-15 10:49:05 +0100702 i915_vma_unpin_and_release(&dev_priv->semaphore);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000703}
704
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000705static u32 *gen8_rcs_signal(struct drm_i915_gem_request *req, u32 *cs)
Ben Widawsky3e789982014-06-30 09:53:37 -0700706{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100707 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700708 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +0000709 enum intel_engine_id id;
Ben Widawsky3e789982014-06-30 09:53:37 -0700710
Akash Goel3b3f1652016-10-13 22:44:48 +0530711 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100712 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -0700713 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
714 continue;
715
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000716 *cs++ = GFX_OP_PIPE_CONTROL(6);
717 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_QW_WRITE |
718 PIPE_CONTROL_CS_STALL;
719 *cs++ = lower_32_bits(gtt_offset);
720 *cs++ = upper_32_bits(gtt_offset);
721 *cs++ = req->global_seqno;
722 *cs++ = 0;
723 *cs++ = MI_SEMAPHORE_SIGNAL |
724 MI_SEMAPHORE_TARGET(waiter->hw_id);
725 *cs++ = 0;
Ben Widawsky3e789982014-06-30 09:53:37 -0700726 }
727
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000728 return cs;
Ben Widawsky3e789982014-06-30 09:53:37 -0700729}
730
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000731static u32 *gen8_xcs_signal(struct drm_i915_gem_request *req, u32 *cs)
Ben Widawsky3e789982014-06-30 09:53:37 -0700732{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100733 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -0700734 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +0000735 enum intel_engine_id id;
Ben Widawsky3e789982014-06-30 09:53:37 -0700736
Akash Goel3b3f1652016-10-13 22:44:48 +0530737 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100738 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -0700739 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
740 continue;
741
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000742 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
743 *cs++ = lower_32_bits(gtt_offset) | MI_FLUSH_DW_USE_GTT;
744 *cs++ = upper_32_bits(gtt_offset);
745 *cs++ = req->global_seqno;
746 *cs++ = MI_SEMAPHORE_SIGNAL |
747 MI_SEMAPHORE_TARGET(waiter->hw_id);
748 *cs++ = 0;
Ben Widawsky3e789982014-06-30 09:53:37 -0700749 }
750
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000751 return cs;
Ben Widawsky3e789982014-06-30 09:53:37 -0700752}
753
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000754static u32 *gen6_signal(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000755{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100756 struct drm_i915_private *dev_priv = req->i915;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100757 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530758 enum intel_engine_id id;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100759 int num_rings = 0;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700760
Akash Goel3b3f1652016-10-13 22:44:48 +0530761 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100762 i915_reg_t mbox_reg;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200763
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100764 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
765 continue;
766
767 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200768 if (i915_mmio_reg_valid(mbox_reg)) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000769 *cs++ = MI_LOAD_REGISTER_IMM(1);
770 *cs++ = i915_mmio_reg_offset(mbox_reg);
771 *cs++ = req->global_seqno;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100772 num_rings++;
Ben Widawsky78325f22014-04-29 14:52:29 -0700773 }
774 }
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100775 if (num_rings & 1)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000776 *cs++ = MI_NOOP;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700777
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000778 return cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000779}
780
Chris Wilsonb0411e72016-08-02 22:50:34 +0100781static void i9xx_submit_request(struct drm_i915_gem_request *request)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000782{
Chris Wilsonb0411e72016-08-02 22:50:34 +0100783 struct drm_i915_private *dev_priv = request->i915;
784
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000785 i915_gem_request_submit(request);
786
Chris Wilson944a36d2017-02-17 16:38:33 +0000787 GEM_BUG_ON(!IS_ALIGNED(request->tail, 8));
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100788 I915_WRITE_TAIL(request->engine, request->tail);
Chris Wilsonb0411e72016-08-02 22:50:34 +0100789}
790
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000791static void i9xx_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100792{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000793 *cs++ = MI_STORE_DWORD_INDEX;
794 *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
795 *cs++ = req->global_seqno;
796 *cs++ = MI_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000797
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000798 req->tail = intel_ring_offset(req, cs);
Chris Wilson944a36d2017-02-17 16:38:33 +0000799 GEM_BUG_ON(!IS_ALIGNED(req->tail, 8));
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000800}
801
Chris Wilson98f29e82016-10-28 13:58:51 +0100802static const int i9xx_emit_breadcrumb_sz = 4;
803
Chris Wilsonb0411e72016-08-02 22:50:34 +0100804/**
Chris Wilson9b81d552016-10-28 13:58:50 +0100805 * gen6_sema_emit_breadcrumb - Update the semaphore mailbox registers
Chris Wilsonb0411e72016-08-02 22:50:34 +0100806 *
807 * @request - request to write to the ring
808 *
809 * Update the mailbox registers in the *other* rings with the current seqno.
810 * This acts like a signal in the canonical semaphore.
811 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000812static void gen6_sema_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100813{
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100814 return i9xx_emit_breadcrumb(req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000815 req->engine->semaphore.signal(req, cs));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100816}
817
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100818static void gen8_render_emit_breadcrumb(struct drm_i915_gem_request *req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000819 u32 *cs)
Chris Wilsona58c01a2016-04-29 13:18:21 +0100820{
821 struct intel_engine_cs *engine = req->engine;
Chris Wilsona58c01a2016-04-29 13:18:21 +0100822
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100823 if (engine->semaphore.signal)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000824 cs = engine->semaphore.signal(req, cs);
Chris Wilson9242f972016-08-02 22:50:33 +0100825
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000826 *cs++ = GFX_OP_PIPE_CONTROL(6);
827 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
828 PIPE_CONTROL_QW_WRITE;
829 *cs++ = intel_hws_seqno_address(engine);
830 *cs++ = 0;
831 *cs++ = req->global_seqno;
Chris Wilsona58c01a2016-04-29 13:18:21 +0100832 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000833 *cs++ = 0;
834 *cs++ = MI_USER_INTERRUPT;
835 *cs++ = MI_NOOP;
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100836
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000837 req->tail = intel_ring_offset(req, cs);
Chris Wilson944a36d2017-02-17 16:38:33 +0000838 GEM_BUG_ON(!IS_ALIGNED(req->tail, 8));
Chris Wilsona58c01a2016-04-29 13:18:21 +0100839}
840
Chris Wilson98f29e82016-10-28 13:58:51 +0100841static const int gen8_render_emit_breadcrumb_sz = 8;
842
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700843/**
844 * intel_ring_sync - sync the waiter to the signaller on seqno
845 *
846 * @waiter - ring that is waiting
847 * @signaller - ring which has, or will signal
848 * @seqno - seqno which the waiter will block on
849 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700850
851static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100852gen8_ring_sync_to(struct drm_i915_gem_request *req,
853 struct drm_i915_gem_request *signal)
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700854{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100855 struct drm_i915_private *dev_priv = req->i915;
856 u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +0100857 struct i915_hw_ppgtt *ppgtt;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000858 u32 *cs;
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700859
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000860 cs = intel_ring_begin(req, 4);
861 if (IS_ERR(cs))
862 return PTR_ERR(cs);
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700863
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000864 *cs++ = MI_SEMAPHORE_WAIT | MI_SEMAPHORE_GLOBAL_GTT |
865 MI_SEMAPHORE_SAD_GTE_SDD;
866 *cs++ = signal->global_seqno;
867 *cs++ = lower_32_bits(offset);
868 *cs++ = upper_32_bits(offset);
869 intel_ring_advance(req, cs);
Chris Wilson6ef48d72016-04-29 13:18:25 +0100870
871 /* When the !RCS engines idle waiting upon a semaphore, they lose their
872 * pagetables and we must reload them before executing the batch.
873 * We do this on the i915_switch_context() following the wait and
874 * before the dispatch.
875 */
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100876 ppgtt = req->ctx->ppgtt;
877 if (ppgtt && req->engine->id != RCS)
878 ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700879 return 0;
880}
881
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700882static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100883gen6_ring_sync_to(struct drm_i915_gem_request *req,
884 struct drm_i915_gem_request *signal)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000885{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700886 u32 dw1 = MI_SEMAPHORE_MBOX |
887 MI_SEMAPHORE_COMPARE |
888 MI_SEMAPHORE_REGISTER;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100889 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000890 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000891
Chris Wilsonddf07be2016-08-02 22:50:39 +0100892 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
893
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000894 cs = intel_ring_begin(req, 4);
895 if (IS_ERR(cs))
896 return PTR_ERR(cs);
Chris Wilsonddf07be2016-08-02 22:50:39 +0100897
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000898 *cs++ = dw1 | wait_mbox;
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700899 /* Throughout all of the GEM code, seqno passed implies our current
900 * seqno is >= the last seqno executed. However for hardware the
901 * comparison is strictly greater than.
902 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000903 *cs++ = signal->global_seqno - 1;
904 *cs++ = 0;
905 *cs++ = MI_NOOP;
906 intel_ring_advance(req, cs);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000907
908 return 0;
909}
910
Chris Wilsonf8973c22016-07-01 17:23:21 +0100911static void
Dave Gordon38a0f2d2016-07-20 18:16:06 +0100912gen5_seqno_barrier(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000913{
Chris Wilsonf8973c22016-07-01 17:23:21 +0100914 /* MI_STORE are internally buffered by the GPU and not flushed
915 * either by MI_FLUSH or SyncFlush or any other combination of
916 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000917 *
Chris Wilsonf8973c22016-07-01 17:23:21 +0100918 * "Only the submission of the store operation is guaranteed.
919 * The write result will be complete (coherent) some time later
920 * (this is practically a finite period but there is no guaranteed
921 * latency)."
922 *
923 * Empirically, we observe that we need a delay of at least 75us to
924 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000925 */
Chris Wilsonf8973c22016-07-01 17:23:21 +0100926 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000927}
928
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100929static void
930gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100931{
Chris Wilsonc0336662016-05-06 15:40:21 +0100932 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100933
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100934 /* Workaround to force correct ordering between irq and seqno writes on
935 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +0100936 * ACTHD) before reading the status page.
937 *
938 * Note that this effectively stalls the read by the time it takes to
939 * do a memory transaction, which more or less ensures that the write
940 * from the GPU has sufficient time to invalidate the CPU cacheline.
941 * Alternatively we could delay the interrupt from the CS ring to give
942 * the write time to land, but that would incur a delay after every
943 * batch i.e. much more frequent than a delay when waiting for the
944 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100945 *
946 * Also note that to prevent whole machine hangs on gen7, we have to
947 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +0100948 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100949 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100950 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100951 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100952}
953
Chris Wilson31bb59c2016-07-01 17:23:27 +0100954static void
955gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200956{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100957 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +0200958}
959
960static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100961gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200962{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100963 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700964}
965
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800966static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100967i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700968{
Chris Wilsonc0336662016-05-06 15:40:21 +0100969 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700970
Chris Wilson31bb59c2016-07-01 17:23:27 +0100971 dev_priv->irq_mask &= ~engine->irq_enable_mask;
972 I915_WRITE(IMR, dev_priv->irq_mask);
973 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +0100974}
975
976static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100977i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +0100978{
Chris Wilsonc0336662016-05-06 15:40:21 +0100979 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100980
Chris Wilson31bb59c2016-07-01 17:23:27 +0100981 dev_priv->irq_mask |= engine->irq_enable_mask;
982 I915_WRITE(IMR, dev_priv->irq_mask);
983}
984
985static void
986i8xx_irq_enable(struct intel_engine_cs *engine)
987{
988 struct drm_i915_private *dev_priv = engine->i915;
989
990 dev_priv->irq_mask &= ~engine->irq_enable_mask;
991 I915_WRITE16(IMR, dev_priv->irq_mask);
992 POSTING_READ16(RING_IMR(engine->mmio_base));
993}
994
995static void
996i8xx_irq_disable(struct intel_engine_cs *engine)
997{
998 struct drm_i915_private *dev_priv = engine->i915;
999
1000 dev_priv->irq_mask |= engine->irq_enable_mask;
1001 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001002}
1003
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001004static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001005bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001006{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001007 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001008
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001009 cs = intel_ring_begin(req, 2);
1010 if (IS_ERR(cs))
1011 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001012
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001013 *cs++ = MI_FLUSH;
1014 *cs++ = MI_NOOP;
1015 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001016 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001017}
1018
Chris Wilson0f468322011-01-04 17:35:21 +00001019static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001020gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001021{
Chris Wilsonc0336662016-05-06 15:40:21 +01001022 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001023
Chris Wilson61ff75a2016-07-01 17:23:28 +01001024 I915_WRITE_IMR(engine,
1025 ~(engine->irq_enable_mask |
1026 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001027 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001028}
1029
1030static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001031gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001032{
Chris Wilsonc0336662016-05-06 15:40:21 +01001033 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001034
Chris Wilson61ff75a2016-07-01 17:23:28 +01001035 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001036 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001037}
1038
1039static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001040hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001041{
Chris Wilsonc0336662016-05-06 15:40:21 +01001042 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001043
Chris Wilson31bb59c2016-07-01 17:23:27 +01001044 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
Akash Goelf4e9af42016-10-12 21:54:30 +05301045 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001046}
1047
1048static void
1049hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1050{
1051 struct drm_i915_private *dev_priv = engine->i915;
1052
1053 I915_WRITE_IMR(engine, ~0);
Akash Goelf4e9af42016-10-12 21:54:30 +05301054 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001055}
1056
1057static void
1058gen8_irq_enable(struct intel_engine_cs *engine)
1059{
1060 struct drm_i915_private *dev_priv = engine->i915;
1061
Chris Wilson61ff75a2016-07-01 17:23:28 +01001062 I915_WRITE_IMR(engine,
1063 ~(engine->irq_enable_mask |
1064 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001065 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1066}
1067
1068static void
1069gen8_irq_disable(struct intel_engine_cs *engine)
1070{
1071 struct drm_i915_private *dev_priv = engine->i915;
1072
Chris Wilson61ff75a2016-07-01 17:23:28 +01001073 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001074}
1075
Zou Nan haid1b851f2010-05-21 09:08:57 +08001076static int
Chris Wilson803688b2016-08-02 22:50:27 +01001077i965_emit_bb_start(struct drm_i915_gem_request *req,
1078 u64 offset, u32 length,
1079 unsigned int dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001080{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001081 u32 *cs;
Chris Wilson78501ea2010-10-27 12:18:21 +01001082
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001083 cs = intel_ring_begin(req, 2);
1084 if (IS_ERR(cs))
1085 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001086
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001087 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
1088 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
1089 *cs++ = offset;
1090 intel_ring_advance(req, cs);
Chris Wilson78501ea2010-10-27 12:18:21 +01001091
Zou Nan haid1b851f2010-05-21 09:08:57 +08001092 return 0;
1093}
1094
Daniel Vetterb45305f2012-12-17 16:21:27 +01001095/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1096#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001097#define I830_TLB_ENTRIES (2)
1098#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001099static int
Chris Wilson803688b2016-08-02 22:50:27 +01001100i830_emit_bb_start(struct drm_i915_gem_request *req,
1101 u64 offset, u32 len,
1102 unsigned int dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001103{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001104 u32 *cs, cs_offset = i915_ggtt_offset(req->engine->scratch);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001105
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001106 cs = intel_ring_begin(req, 6);
1107 if (IS_ERR(cs))
1108 return PTR_ERR(cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001109
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001110 /* Evict the invalid PTE TLBs */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001111 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
1112 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
1113 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
1114 *cs++ = cs_offset;
1115 *cs++ = 0xdeadbeef;
1116 *cs++ = MI_NOOP;
1117 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001118
John Harrison8e004ef2015-02-13 11:48:10 +00001119 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001120 if (len > I830_BATCH_LIMIT)
1121 return -ENOSPC;
1122
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001123 cs = intel_ring_begin(req, 6 + 2);
1124 if (IS_ERR(cs))
1125 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001126
1127 /* Blit the batch (which has now all relocs applied) to the
1128 * stable batch scratch bo area (so that the CS never
1129 * stumbles over its tlb invalidation bug) ...
1130 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001131 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
1132 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
1133 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
1134 *cs++ = cs_offset;
1135 *cs++ = 4096;
1136 *cs++ = offset;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001137
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001138 *cs++ = MI_FLUSH;
1139 *cs++ = MI_NOOP;
1140 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001141
1142 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001143 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001144 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001145
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001146 cs = intel_ring_begin(req, 2);
1147 if (IS_ERR(cs))
1148 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001149
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001150 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1151 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1152 MI_BATCH_NON_SECURE);
1153 intel_ring_advance(req, cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001154
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001155 return 0;
1156}
1157
1158static int
Chris Wilson803688b2016-08-02 22:50:27 +01001159i915_emit_bb_start(struct drm_i915_gem_request *req,
1160 u64 offset, u32 len,
1161 unsigned int dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001162{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001163 u32 *cs;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001164
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001165 cs = intel_ring_begin(req, 2);
1166 if (IS_ERR(cs))
1167 return PTR_ERR(cs);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001168
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001169 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1170 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1171 MI_BATCH_NON_SECURE);
1172 intel_ring_advance(req, cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001173
Eric Anholt62fdfea2010-05-21 13:26:39 -07001174 return 0;
1175}
1176
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001177static void cleanup_phys_status_page(struct intel_engine_cs *engine)
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001178{
Chris Wilsonc0336662016-05-06 15:40:21 +01001179 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001180
1181 if (!dev_priv->status_page_dmah)
1182 return;
1183
Chris Wilson91c8a322016-07-05 10:40:23 +01001184 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001185 engine->status_page.page_addr = NULL;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001186}
1187
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001188static void cleanup_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001189{
Chris Wilson57e88532016-08-15 10:48:57 +01001190 struct i915_vma *vma;
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001191 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001192
Chris Wilson57e88532016-08-15 10:48:57 +01001193 vma = fetch_and_zero(&engine->status_page.vma);
1194 if (!vma)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001195 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001196
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001197 obj = vma->obj;
1198
Chris Wilson57e88532016-08-15 10:48:57 +01001199 i915_vma_unpin(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001200 i915_vma_close(vma);
1201
1202 i915_gem_object_unpin_map(obj);
1203 __i915_gem_object_release_unless_active(obj);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001204}
1205
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001206static int init_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001207{
Chris Wilson57e88532016-08-15 10:48:57 +01001208 struct drm_i915_gem_object *obj;
1209 struct i915_vma *vma;
1210 unsigned int flags;
Chris Wilson920cf412016-10-28 13:58:30 +01001211 void *vaddr;
Chris Wilson57e88532016-08-15 10:48:57 +01001212 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001213
Chris Wilsonf51455d2017-01-10 14:47:34 +00001214 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
Chris Wilson57e88532016-08-15 10:48:57 +01001215 if (IS_ERR(obj)) {
1216 DRM_ERROR("Failed to allocate status page\n");
1217 return PTR_ERR(obj);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001218 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001219
Chris Wilson57e88532016-08-15 10:48:57 +01001220 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1221 if (ret)
1222 goto err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001223
Chris Wilsona01cb372017-01-16 15:21:30 +00001224 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson57e88532016-08-15 10:48:57 +01001225 if (IS_ERR(vma)) {
1226 ret = PTR_ERR(vma);
1227 goto err;
1228 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001229
Chris Wilson57e88532016-08-15 10:48:57 +01001230 flags = PIN_GLOBAL;
1231 if (!HAS_LLC(engine->i915))
1232 /* On g33, we cannot place HWS above 256MiB, so
1233 * restrict its pinning to the low mappable arena.
1234 * Though this restriction is not documented for
1235 * gen4, gen5, or byt, they also behave similarly
1236 * and hang if the HWS is placed at the top of the
1237 * GTT. To generalise, it appears that all !llc
1238 * platforms have issues with us placing the HWS
1239 * above the mappable region (even though we never
1240 * actualy map it).
1241 */
1242 flags |= PIN_MAPPABLE;
1243 ret = i915_vma_pin(vma, 0, 4096, flags);
1244 if (ret)
1245 goto err;
1246
Chris Wilson920cf412016-10-28 13:58:30 +01001247 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1248 if (IS_ERR(vaddr)) {
1249 ret = PTR_ERR(vaddr);
1250 goto err_unpin;
1251 }
1252
Chris Wilson57e88532016-08-15 10:48:57 +01001253 engine->status_page.vma = vma;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001254 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
Chris Wilsonf51455d2017-01-10 14:47:34 +00001255 engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
Chris Wilson57e88532016-08-15 10:48:57 +01001256
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001257 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1258 engine->name, i915_ggtt_offset(vma));
Eric Anholt62fdfea2010-05-21 13:26:39 -07001259 return 0;
Chris Wilson57e88532016-08-15 10:48:57 +01001260
Chris Wilson920cf412016-10-28 13:58:30 +01001261err_unpin:
1262 i915_vma_unpin(vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001263err:
1264 i915_gem_object_put(obj);
1265 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001266}
1267
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001268static int init_phys_status_page(struct intel_engine_cs *engine)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001269{
Chris Wilsonc0336662016-05-06 15:40:21 +01001270 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001271
Chris Wilson57e88532016-08-15 10:48:57 +01001272 dev_priv->status_page_dmah =
1273 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
1274 if (!dev_priv->status_page_dmah)
1275 return -ENOMEM;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001276
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001277 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1278 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001279
1280 return 0;
1281}
1282
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001283int intel_ring_pin(struct intel_ring *ring, unsigned int offset_bias)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001284{
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001285 unsigned int flags;
Chris Wilson9d808412016-08-18 17:16:56 +01001286 enum i915_map_type map;
Chris Wilson57e88532016-08-15 10:48:57 +01001287 struct i915_vma *vma = ring->vma;
Dave Gordon83052162016-04-12 14:46:16 +01001288 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001289 int ret;
1290
Chris Wilson57e88532016-08-15 10:48:57 +01001291 GEM_BUG_ON(ring->vaddr);
1292
Chris Wilson9d808412016-08-18 17:16:56 +01001293 map = HAS_LLC(ring->engine->i915) ? I915_MAP_WB : I915_MAP_WC;
1294
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001295 flags = PIN_GLOBAL;
1296 if (offset_bias)
1297 flags |= PIN_OFFSET_BIAS | offset_bias;
Chris Wilson9d808412016-08-18 17:16:56 +01001298 if (vma->obj->stolen)
Chris Wilson57e88532016-08-15 10:48:57 +01001299 flags |= PIN_MAPPABLE;
1300
1301 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilson9d808412016-08-18 17:16:56 +01001302 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
Chris Wilson57e88532016-08-15 10:48:57 +01001303 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1304 else
1305 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1306 if (unlikely(ret))
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001307 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001308 }
1309
Chris Wilson57e88532016-08-15 10:48:57 +01001310 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1311 if (unlikely(ret))
1312 return ret;
1313
Chris Wilson9d808412016-08-18 17:16:56 +01001314 if (i915_vma_is_map_and_fenceable(vma))
Chris Wilson57e88532016-08-15 10:48:57 +01001315 addr = (void __force *)i915_vma_pin_iomap(vma);
1316 else
Chris Wilson9d808412016-08-18 17:16:56 +01001317 addr = i915_gem_object_pin_map(vma->obj, map);
Chris Wilson57e88532016-08-15 10:48:57 +01001318 if (IS_ERR(addr))
1319 goto err;
1320
Chris Wilson32c04f12016-08-02 22:50:22 +01001321 ring->vaddr = addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001322 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01001323
Chris Wilson57e88532016-08-15 10:48:57 +01001324err:
1325 i915_vma_unpin(vma);
1326 return PTR_ERR(addr);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001327}
1328
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001329void intel_ring_unpin(struct intel_ring *ring)
1330{
1331 GEM_BUG_ON(!ring->vma);
1332 GEM_BUG_ON(!ring->vaddr);
1333
Chris Wilson9d808412016-08-18 17:16:56 +01001334 if (i915_vma_is_map_and_fenceable(ring->vma))
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001335 i915_vma_unpin_iomap(ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001336 else
1337 i915_gem_object_unpin_map(ring->vma->obj);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001338 ring->vaddr = NULL;
1339
Chris Wilson57e88532016-08-15 10:48:57 +01001340 i915_vma_unpin(ring->vma);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001341}
1342
Chris Wilson57e88532016-08-15 10:48:57 +01001343static struct i915_vma *
1344intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
Oscar Mateo2919d292014-07-03 16:28:02 +01001345{
Chris Wilsone3efda42014-04-09 09:19:41 +01001346 struct drm_i915_gem_object *obj;
Chris Wilson57e88532016-08-15 10:48:57 +01001347 struct i915_vma *vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001348
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00001349 obj = i915_gem_object_create_stolen(dev_priv, size);
Chris Wilsonc58b7352016-08-18 17:16:57 +01001350 if (!obj)
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00001351 obj = i915_gem_object_create(dev_priv, size);
Chris Wilson57e88532016-08-15 10:48:57 +01001352 if (IS_ERR(obj))
1353 return ERR_CAST(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001354
Akash Goel24f3a8c2014-06-17 10:59:42 +05301355 /* mark ring buffers as read-only from GPU side by default */
1356 obj->gt_ro = 1;
1357
Chris Wilsona01cb372017-01-16 15:21:30 +00001358 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson57e88532016-08-15 10:48:57 +01001359 if (IS_ERR(vma))
1360 goto err;
Chris Wilsone3efda42014-04-09 09:19:41 +01001361
Chris Wilson57e88532016-08-15 10:48:57 +01001362 return vma;
1363
1364err:
1365 i915_gem_object_put(obj);
1366 return vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001367}
1368
Chris Wilson7e37f882016-08-02 22:50:21 +01001369struct intel_ring *
1370intel_engine_create_ring(struct intel_engine_cs *engine, int size)
Chris Wilson01101fa2015-09-03 13:01:39 +01001371{
Chris Wilson7e37f882016-08-02 22:50:21 +01001372 struct intel_ring *ring;
Chris Wilson57e88532016-08-15 10:48:57 +01001373 struct i915_vma *vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001374
Chris Wilson8f942012016-08-02 22:50:30 +01001375 GEM_BUG_ON(!is_power_of_2(size));
Chris Wilson62ae14b2016-10-04 21:11:25 +01001376 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
Chris Wilson8f942012016-08-02 22:50:30 +01001377
Chris Wilson01101fa2015-09-03 13:01:39 +01001378 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson57e88532016-08-15 10:48:57 +01001379 if (!ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001380 return ERR_PTR(-ENOMEM);
1381
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001382 ring->engine = engine;
Chris Wilson01101fa2015-09-03 13:01:39 +01001383
Chris Wilson675d9ad2016-08-04 07:52:36 +01001384 INIT_LIST_HEAD(&ring->request_list);
1385
Chris Wilson01101fa2015-09-03 13:01:39 +01001386 ring->size = size;
1387 /* Workaround an erratum on the i830 which causes a hang if
1388 * the TAIL pointer points to within the last 2 cachelines
1389 * of the buffer.
1390 */
1391 ring->effective_size = size;
Jani Nikula2a307c22016-11-30 17:43:04 +02001392 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01001393 ring->effective_size -= 2 * CACHELINE_BYTES;
1394
1395 ring->last_retired_head = -1;
1396 intel_ring_update_space(ring);
1397
Chris Wilson57e88532016-08-15 10:48:57 +01001398 vma = intel_ring_create_vma(engine->i915, size);
1399 if (IS_ERR(vma)) {
Chris Wilson01101fa2015-09-03 13:01:39 +01001400 kfree(ring);
Chris Wilson57e88532016-08-15 10:48:57 +01001401 return ERR_CAST(vma);
Chris Wilson01101fa2015-09-03 13:01:39 +01001402 }
Chris Wilson57e88532016-08-15 10:48:57 +01001403 ring->vma = vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001404
1405 return ring;
1406}
1407
1408void
Chris Wilson7e37f882016-08-02 22:50:21 +01001409intel_ring_free(struct intel_ring *ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001410{
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001411 struct drm_i915_gem_object *obj = ring->vma->obj;
1412
1413 i915_vma_close(ring->vma);
1414 __i915_gem_object_release_unless_active(obj);
1415
Chris Wilson01101fa2015-09-03 13:01:39 +01001416 kfree(ring);
1417}
1418
Chris Wilson72b72ae2017-02-10 10:14:22 +00001419static int context_pin(struct i915_gem_context *ctx)
Chris Wilsone8a9c582016-12-18 15:37:20 +00001420{
1421 struct i915_vma *vma = ctx->engine[RCS].state;
1422 int ret;
1423
1424 /* Clear this page out of any CPU caches for coherent swap-in/out.
1425 * We only want to do this on the first bind so that we do not stall
1426 * on an active context (which by nature is already on the GPU).
1427 */
1428 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1429 ret = i915_gem_object_set_to_gtt_domain(vma->obj, false);
1430 if (ret)
1431 return ret;
1432 }
1433
Chris Wilsonafeddf52017-02-27 13:59:13 +00001434 return i915_vma_pin(vma, 0, I915_GTT_MIN_ALIGNMENT,
1435 PIN_GLOBAL | PIN_HIGH);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001436}
1437
1438static int intel_ring_context_pin(struct intel_engine_cs *engine,
1439 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001440{
1441 struct intel_context *ce = &ctx->engine[engine->id];
1442 int ret;
1443
Chris Wilson91c8a322016-07-05 10:40:23 +01001444 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001445
1446 if (ce->pin_count++)
1447 return 0;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001448 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson0cb26a82016-06-24 14:55:53 +01001449
1450 if (ce->state) {
Chris Wilson72b72ae2017-02-10 10:14:22 +00001451 ret = context_pin(ctx);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001452 if (ret)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001453 goto error;
1454 }
1455
Chris Wilsonc7c3c072016-06-24 14:55:54 +01001456 /* The kernel context is only used as a placeholder for flushing the
1457 * active context. It is never used for submitting user rendering and
1458 * as such never requires the golden render context, and so we can skip
1459 * emitting it when we switch to the kernel context. This is required
1460 * as during eviction we cannot allocate and pin the renderstate in
1461 * order to initialise the context.
1462 */
Chris Wilson984ff29f2017-01-06 15:20:13 +00001463 if (i915_gem_context_is_kernel(ctx))
Chris Wilsonc7c3c072016-06-24 14:55:54 +01001464 ce->initialised = true;
1465
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001466 i915_gem_context_get(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001467 return 0;
1468
1469error:
1470 ce->pin_count = 0;
1471 return ret;
1472}
1473
Chris Wilsone8a9c582016-12-18 15:37:20 +00001474static void intel_ring_context_unpin(struct intel_engine_cs *engine,
1475 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001476{
1477 struct intel_context *ce = &ctx->engine[engine->id];
1478
Chris Wilson91c8a322016-07-05 10:40:23 +01001479 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001480 GEM_BUG_ON(ce->pin_count == 0);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001481
1482 if (--ce->pin_count)
1483 return;
1484
1485 if (ce->state)
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001486 i915_vma_unpin(ce->state);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001487
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001488 i915_gem_context_put(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001489}
1490
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001491static int intel_init_ring_buffer(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001492{
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001493 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson32c04f12016-08-02 22:50:22 +01001494 struct intel_ring *ring;
Chris Wilsondd785e32010-08-07 11:01:34 +01001495 int ret;
1496
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001497 WARN_ON(engine->buffer);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001498
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001499 intel_engine_setup_common(engine);
1500
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001501 ret = intel_engine_init_common(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +01001502 if (ret)
1503 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001504
Chris Wilson32c04f12016-08-02 22:50:22 +01001505 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
1506 if (IS_ERR(ring)) {
1507 ret = PTR_ERR(ring);
Dave Gordonb0366a52015-12-08 15:02:36 +00001508 goto error;
1509 }
Chris Wilson01101fa2015-09-03 13:01:39 +01001510
Carlos Santa31776592016-08-17 12:30:56 -07001511 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
1512 WARN_ON(engine->id != RCS);
1513 ret = init_phys_status_page(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001514 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001515 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001516 } else {
Carlos Santa31776592016-08-17 12:30:56 -07001517 ret = init_status_page(engine);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001518 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001519 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001520 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001521
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001522 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
Chris Wilsonf51455d2017-01-10 14:47:34 +00001523 ret = intel_ring_pin(ring, I915_GTT_PAGE_SIZE);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001524 if (ret) {
Chris Wilson57e88532016-08-15 10:48:57 +01001525 intel_ring_free(ring);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001526 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001527 }
Chris Wilson57e88532016-08-15 10:48:57 +01001528 engine->buffer = ring;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001529
Oscar Mateo8ee14972014-05-22 14:13:34 +01001530 return 0;
1531
1532error:
Chris Wilson7e37f882016-08-02 22:50:21 +01001533 intel_engine_cleanup(engine);
Oscar Mateo8ee14972014-05-22 14:13:34 +01001534 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001535}
1536
Chris Wilson7e37f882016-08-02 22:50:21 +01001537void intel_engine_cleanup(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001538{
John Harrison6402c332014-10-31 12:00:26 +00001539 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01001540
Chris Wilsonc0336662016-05-06 15:40:21 +01001541 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001542
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001543 if (engine->buffer) {
Chris Wilson21a2c582016-08-15 10:49:11 +01001544 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
1545 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01001546
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001547 intel_ring_unpin(engine->buffer);
Chris Wilson7e37f882016-08-02 22:50:21 +01001548 intel_ring_free(engine->buffer);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001549 engine->buffer = NULL;
Dave Gordonb0366a52015-12-08 15:02:36 +00001550 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001551
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001552 if (engine->cleanup)
1553 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08001554
Carlos Santa31776592016-08-17 12:30:56 -07001555 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001556 WARN_ON(engine->id != RCS);
1557 cleanup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -07001558 } else {
1559 cleanup_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001560 }
Brad Volkin44e895a2014-05-10 14:10:43 -07001561
Chris Wilson96a945a2016-08-03 13:19:16 +01001562 intel_engine_cleanup_common(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001563
Chris Wilsonc0336662016-05-06 15:40:21 +01001564 engine->i915 = NULL;
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
Akash Goel3b3f1652016-10-13 22:44:48 +05301574 for_each_engine(engine, dev_priv, id) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01001575 engine->buffer->head = engine->buffer->tail;
1576 engine->buffer->last_retired_head = -1;
1577 }
1578}
1579
Chris Wilsonf73e7392016-12-18 15:37:24 +00001580static int ring_request_alloc(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00001581{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001582 u32 *cs;
Chris Wilson63103462016-04-28 09:56:49 +01001583
Chris Wilsone8a9c582016-12-18 15:37:20 +00001584 GEM_BUG_ON(!request->ctx->engine[request->engine->id].pin_count);
1585
Chris Wilson63103462016-04-28 09:56:49 +01001586 /* Flush enough space to reduce the likelihood of waiting after
1587 * we start building the request - in which case we will just
1588 * have to repeat work.
1589 */
Chris Wilsona0442462016-04-29 09:07:05 +01001590 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001591
Chris Wilsone8a9c582016-12-18 15:37:20 +00001592 GEM_BUG_ON(!request->engine->buffer);
Chris Wilson1dae2df2016-08-02 22:50:19 +01001593 request->ring = request->engine->buffer;
Chris Wilson63103462016-04-28 09:56:49 +01001594
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001595 cs = intel_ring_begin(request, 0);
1596 if (IS_ERR(cs))
1597 return PTR_ERR(cs);
Chris Wilson63103462016-04-28 09:56:49 +01001598
Chris Wilsona0442462016-04-29 09:07:05 +01001599 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001600 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00001601}
1602
Chris Wilson987046a2016-04-28 09:56:46 +01001603static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001604{
Chris Wilson7e37f882016-08-02 22:50:21 +01001605 struct intel_ring *ring = req->ring;
Chris Wilson987046a2016-04-28 09:56:46 +01001606 struct drm_i915_gem_request *target;
Chris Wilsone95433c2016-10-28 13:58:27 +01001607 long timeout;
1608
1609 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson987046a2016-04-28 09:56:46 +01001610
Chris Wilson1dae2df2016-08-02 22:50:19 +01001611 intel_ring_update_space(ring);
1612 if (ring->space >= bytes)
Chris Wilson987046a2016-04-28 09:56:46 +01001613 return 0;
1614
1615 /*
1616 * Space is reserved in the ringbuffer for finalising the request,
1617 * as that cannot be allowed to fail. During request finalisation,
1618 * reserved_space is set to 0 to stop the overallocation and the
1619 * assumption is that then we never need to wait (which has the
1620 * risk of failing with EINTR).
1621 *
1622 * See also i915_gem_request_alloc() and i915_add_request().
1623 */
Chris Wilson0251a962016-04-28 09:56:47 +01001624 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01001625
Chris Wilson675d9ad2016-08-04 07:52:36 +01001626 list_for_each_entry(target, &ring->request_list, ring_link) {
Chris Wilson987046a2016-04-28 09:56:46 +01001627 unsigned space;
1628
Chris Wilson987046a2016-04-28 09:56:46 +01001629 /* Would completion of this request free enough space? */
Chris Wilson1dae2df2016-08-02 22:50:19 +01001630 space = __intel_ring_space(target->postfix, ring->tail,
1631 ring->size);
Chris Wilson987046a2016-04-28 09:56:46 +01001632 if (space >= bytes)
1633 break;
1634 }
1635
Chris Wilson675d9ad2016-08-04 07:52:36 +01001636 if (WARN_ON(&target->ring_link == &ring->request_list))
Chris Wilson987046a2016-04-28 09:56:46 +01001637 return -ENOSPC;
1638
Chris Wilsone95433c2016-10-28 13:58:27 +01001639 timeout = i915_wait_request(target,
1640 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1641 MAX_SCHEDULE_TIMEOUT);
1642 if (timeout < 0)
1643 return timeout;
Chris Wilson7da844c2016-08-04 07:52:38 +01001644
Chris Wilson7da844c2016-08-04 07:52:38 +01001645 i915_gem_request_retire_upto(target);
1646
1647 intel_ring_update_space(ring);
1648 GEM_BUG_ON(ring->space < bytes);
1649 return 0;
Chris Wilson987046a2016-04-28 09:56:46 +01001650}
1651
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001652u32 *intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
Chris Wilson987046a2016-04-28 09:56:46 +01001653{
Chris Wilson7e37f882016-08-02 22:50:21 +01001654 struct intel_ring *ring = req->ring;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001655 int remain_actual = ring->size - ring->tail;
1656 int remain_usable = ring->effective_size - ring->tail;
Chris Wilson987046a2016-04-28 09:56:46 +01001657 int bytes = num_dwords * sizeof(u32);
1658 int total_bytes, wait_bytes;
John Harrison79bbcc22015-06-30 12:40:55 +01001659 bool need_wrap = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001660 u32 *cs;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001661
Chris Wilson0251a962016-04-28 09:56:47 +01001662 total_bytes = bytes + req->reserved_space;
John Harrison29b1b412015-06-18 13:10:09 +01001663
John Harrison79bbcc22015-06-30 12:40:55 +01001664 if (unlikely(bytes > remain_usable)) {
1665 /*
1666 * Not enough space for the basic request. So need to flush
1667 * out the remainder and then wait for base + reserved.
1668 */
1669 wait_bytes = remain_actual + total_bytes;
1670 need_wrap = true;
Chris Wilson987046a2016-04-28 09:56:46 +01001671 } else if (unlikely(total_bytes > remain_usable)) {
1672 /*
1673 * The base request will fit but the reserved space
1674 * falls off the end. So we don't need an immediate wrap
1675 * and only need to effectively wait for the reserved
1676 * size space from the start of ringbuffer.
1677 */
Chris Wilson0251a962016-04-28 09:56:47 +01001678 wait_bytes = remain_actual + req->reserved_space;
John Harrison79bbcc22015-06-30 12:40:55 +01001679 } else {
Chris Wilson987046a2016-04-28 09:56:46 +01001680 /* No wrapping required, just waiting. */
1681 wait_bytes = total_bytes;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001682 }
1683
Chris Wilson1dae2df2016-08-02 22:50:19 +01001684 if (wait_bytes > ring->space) {
Chris Wilson987046a2016-04-28 09:56:46 +01001685 int ret = wait_for_space(req, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001686 if (unlikely(ret))
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001687 return ERR_PTR(ret);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001688 }
1689
Chris Wilson987046a2016-04-28 09:56:46 +01001690 if (unlikely(need_wrap)) {
Chris Wilson1dae2df2016-08-02 22:50:19 +01001691 GEM_BUG_ON(remain_actual > ring->space);
1692 GEM_BUG_ON(ring->tail + remain_actual > 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 Wilson1dae2df2016-08-02 22:50:19 +01001695 memset(ring->vaddr + ring->tail, 0, remain_actual);
1696 ring->tail = 0;
1697 ring->space -= remain_actual;
Chris Wilson987046a2016-04-28 09:56:46 +01001698 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001699
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001700 GEM_BUG_ON(ring->tail > ring->size - bytes);
1701 cs = ring->vaddr + ring->tail;
1702 ring->tail += bytes;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001703 ring->space -= bytes;
1704 GEM_BUG_ON(ring->space < 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001705
1706 return cs;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001707}
1708
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001709/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01001710int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001711{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001712 int num_dwords =
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001713 (req->ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
1714 u32 *cs;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001715
1716 if (num_dwords == 0)
1717 return 0;
1718
Chris Wilson18393f62014-04-09 09:19:40 +01001719 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001720 cs = intel_ring_begin(req, num_dwords);
1721 if (IS_ERR(cs))
1722 return PTR_ERR(cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001723
1724 while (num_dwords--)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001725 *cs++ = MI_NOOP;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001726
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001727 intel_ring_advance(req, cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001728
1729 return 0;
1730}
1731
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001732static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001733{
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001734 struct drm_i915_private *dev_priv = request->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001735
Chris Wilson76f84212016-06-30 15:33:45 +01001736 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
1737
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001738 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001739
Chris Wilson12f55812012-07-05 17:14:01 +01001740 /* Disable notification that the ring is IDLE. The GT
1741 * will then assume that it is busy and bring it out of rc6.
1742 */
Chris Wilson76f84212016-06-30 15:33:45 +01001743 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1744 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01001745
1746 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01001747 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01001748
1749 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson76f84212016-06-30 15:33:45 +01001750 if (intel_wait_for_register_fw(dev_priv,
1751 GEN6_BSD_SLEEP_PSMI_CONTROL,
1752 GEN6_BSD_SLEEP_INDICATOR,
1753 0,
1754 50))
Chris Wilson12f55812012-07-05 17:14:01 +01001755 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001756
Chris Wilson12f55812012-07-05 17:14:01 +01001757 /* Now that the ring is fully powered up, update the tail */
Chris Wilsonb0411e72016-08-02 22:50:34 +01001758 i9xx_submit_request(request);
Chris Wilson12f55812012-07-05 17:14:01 +01001759
1760 /* Let the ring send IDLE messages to the GT again,
1761 * and so let it sleep to conserve power when idle.
1762 */
Chris Wilson76f84212016-06-30 15:33:45 +01001763 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1764 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1765
1766 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001767}
1768
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001769static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001770{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001771 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001772
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001773 cs = intel_ring_begin(req, 4);
1774 if (IS_ERR(cs))
1775 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001776
Chris Wilson71a77e02011-02-02 12:13:49 +00001777 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01001778 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001779 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001780
1781 /* We always require a command barrier so that subsequent
1782 * commands, such as breadcrumb interrupts, are strictly ordered
1783 * wrt the contents of the write cache being flushed to memory
1784 * (and thus being coherent from the CPU).
1785 */
1786 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1787
Jesse Barnes9a289772012-10-26 09:42:42 -07001788 /*
1789 * Bspec vol 1c.5 - video engine command streamer:
1790 * "If ENABLED, all TLBs will be invalidated once the flush
1791 * operation is complete. This bit is only valid when the
1792 * Post-Sync Operation field is a value of 1h or 3h."
1793 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001794 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001795 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1796
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001797 *cs++ = cmd;
1798 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilsonc0336662016-05-06 15:40:21 +01001799 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001800 *cs++ = 0; /* upper addr */
1801 *cs++ = 0; /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001802 } else {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001803 *cs++ = 0;
1804 *cs++ = MI_NOOP;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001805 }
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001806 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001807 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001808}
1809
1810static int
Chris Wilson803688b2016-08-02 22:50:27 +01001811gen8_emit_bb_start(struct drm_i915_gem_request *req,
1812 u64 offset, u32 len,
1813 unsigned int dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001814{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001815 bool ppgtt = USES_PPGTT(req->i915) &&
John Harrison8e004ef2015-02-13 11:48:10 +00001816 !(dispatch_flags & I915_DISPATCH_SECURE);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001817 u32 *cs;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001818
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001819 cs = intel_ring_begin(req, 4);
1820 if (IS_ERR(cs))
1821 return PTR_ERR(cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001822
1823 /* FIXME(BDW): Address space and security selectors. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001824 *cs++ = MI_BATCH_BUFFER_START_GEN8 | (ppgtt << 8) | (dispatch_flags &
1825 I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
1826 *cs++ = lower_32_bits(offset);
1827 *cs++ = upper_32_bits(offset);
1828 *cs++ = MI_NOOP;
1829 intel_ring_advance(req, cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001830
1831 return 0;
1832}
1833
1834static int
Chris Wilson803688b2016-08-02 22:50:27 +01001835hsw_emit_bb_start(struct drm_i915_gem_request *req,
1836 u64 offset, u32 len,
1837 unsigned int dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001838{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001839 u32 *cs;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001840
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001841 cs = intel_ring_begin(req, 2);
1842 if (IS_ERR(cs))
1843 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001844
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001845 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1846 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
1847 (dispatch_flags & I915_DISPATCH_RS ?
1848 MI_BATCH_RESOURCE_STREAMER : 0);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001849 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001850 *cs++ = offset;
1851 intel_ring_advance(req, cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001852
1853 return 0;
1854}
1855
1856static int
Chris Wilson803688b2016-08-02 22:50:27 +01001857gen6_emit_bb_start(struct drm_i915_gem_request *req,
1858 u64 offset, u32 len,
1859 unsigned int dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001860{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001861 u32 *cs;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001862
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001863 cs = intel_ring_begin(req, 2);
1864 if (IS_ERR(cs))
1865 return PTR_ERR(cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001866
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001867 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1868 0 : MI_BATCH_NON_SECURE_I965);
Akshay Joshi0206e352011-08-16 15:34:10 -04001869 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001870 *cs++ = offset;
1871 intel_ring_advance(req, cs);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001872
Akshay Joshi0206e352011-08-16 15:34:10 -04001873 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001874}
1875
Chris Wilson549f7362010-10-19 11:19:32 +01001876/* Blitter support (SandyBridge+) */
1877
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001878static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan hai8d192152010-11-02 16:31:01 +08001879{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001880 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001881
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001882 cs = intel_ring_begin(req, 4);
1883 if (IS_ERR(cs))
1884 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001885
Chris Wilson71a77e02011-02-02 12:13:49 +00001886 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01001887 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001888 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001889
1890 /* We always require a command barrier so that subsequent
1891 * commands, such as breadcrumb interrupts, are strictly ordered
1892 * wrt the contents of the write cache being flushed to memory
1893 * (and thus being coherent from the CPU).
1894 */
1895 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1896
Jesse Barnes9a289772012-10-26 09:42:42 -07001897 /*
1898 * Bspec vol 1c.3 - blitter engine command streamer:
1899 * "If ENABLED, all TLBs will be invalidated once the flush
1900 * operation is complete. This bit is only valid when the
1901 * Post-Sync Operation field is a value of 1h or 3h."
1902 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001903 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001904 cmd |= MI_INVALIDATE_TLB;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001905 *cs++ = cmd;
1906 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilsonc0336662016-05-06 15:40:21 +01001907 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001908 *cs++ = 0; /* upper addr */
1909 *cs++ = 0; /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001910 } else {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001911 *cs++ = 0;
1912 *cs++ = MI_NOOP;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001913 }
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001914 intel_ring_advance(req, cs);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001915
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001916 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001917}
1918
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001919static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
1920 struct intel_engine_cs *engine)
1921{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001922 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001923 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001924
Chris Wilson39df9192016-07-20 13:31:57 +01001925 if (!i915.semaphores)
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001926 return;
1927
Chris Wilson51d545d2016-08-15 10:49:02 +01001928 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
1929 struct i915_vma *vma;
1930
Chris Wilsonf51455d2017-01-10 14:47:34 +00001931 obj = i915_gem_object_create(dev_priv, PAGE_SIZE);
Chris Wilson51d545d2016-08-15 10:49:02 +01001932 if (IS_ERR(obj))
1933 goto err;
1934
Chris Wilsona01cb372017-01-16 15:21:30 +00001935 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson51d545d2016-08-15 10:49:02 +01001936 if (IS_ERR(vma))
1937 goto err_obj;
1938
1939 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1940 if (ret)
1941 goto err_obj;
1942
1943 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
1944 if (ret)
1945 goto err_obj;
1946
1947 dev_priv->semaphore = vma;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001948 }
1949
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001950 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001951 u32 offset = i915_ggtt_offset(dev_priv->semaphore);
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001952
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001953 engine->semaphore.sync_to = gen8_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001954 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001955
1956 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001957 u32 ring_offset;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01001958
1959 if (i != engine->id)
1960 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
1961 else
1962 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
1963
1964 engine->semaphore.signal_ggtt[i] = ring_offset;
1965 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001966 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001967 engine->semaphore.sync_to = gen6_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001968 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001969
1970 /*
1971 * The current semaphore is only applied on pre-gen8
1972 * platform. And there is no VCS2 ring on the pre-gen8
1973 * platform. So the semaphore between RCS and VCS2 is
1974 * initialized as INVALID. Gen8 will initialize the
1975 * sema between VCS2 and RCS later.
1976 */
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001977 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001978 static const struct {
1979 u32 wait_mbox;
1980 i915_reg_t mbox_reg;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001981 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
1982 [RCS_HW] = {
1983 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
1984 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
1985 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001986 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001987 [VCS_HW] = {
1988 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
1989 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
1990 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001991 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001992 [BCS_HW] = {
1993 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
1994 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
1995 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001996 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001997 [VECS_HW] = {
1998 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
1999 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2000 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002001 },
2002 };
2003 u32 wait_mbox;
2004 i915_reg_t mbox_reg;
2005
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002006 if (i == engine->hw_id) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002007 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2008 mbox_reg = GEN6_NOSYNC;
2009 } else {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002010 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
2011 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002012 }
2013
2014 engine->semaphore.mbox.wait[i] = wait_mbox;
2015 engine->semaphore.mbox.signal[i] = mbox_reg;
2016 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002017 }
Chris Wilson51d545d2016-08-15 10:49:02 +01002018
2019 return;
2020
2021err_obj:
2022 i915_gem_object_put(obj);
2023err:
2024 DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
2025 i915.semaphores = 0;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002026}
2027
Chris Wilsoned003072016-07-01 09:18:13 +01002028static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2029 struct intel_engine_cs *engine)
2030{
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002031 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2032
Chris Wilsoned003072016-07-01 09:18:13 +01002033 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002034 engine->irq_enable = gen8_irq_enable;
2035 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002036 engine->irq_seqno_barrier = gen6_seqno_barrier;
2037 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002038 engine->irq_enable = gen6_irq_enable;
2039 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002040 engine->irq_seqno_barrier = gen6_seqno_barrier;
2041 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002042 engine->irq_enable = gen5_irq_enable;
2043 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002044 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002045 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002046 engine->irq_enable = i9xx_irq_enable;
2047 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002048 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002049 engine->irq_enable = i8xx_irq_enable;
2050 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002051 }
2052}
2053
Chris Wilsonff44ad52017-03-16 17:13:03 +00002054static void i9xx_set_default_submission(struct intel_engine_cs *engine)
2055{
2056 engine->submit_request = i9xx_submit_request;
2057}
2058
2059static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
2060{
2061 engine->submit_request = gen6_bsd_submit_request;
2062}
2063
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002064static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2065 struct intel_engine_cs *engine)
2066{
Chris Wilson618e4ca2016-08-02 22:50:35 +01002067 intel_ring_init_irq(dev_priv, engine);
2068 intel_ring_init_semaphores(dev_priv, engine);
2069
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002070 engine->init_hw = init_ring_common;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002071 engine->reset_hw = reset_ring_common;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002072
Chris Wilsone8a9c582016-12-18 15:37:20 +00002073 engine->context_pin = intel_ring_context_pin;
2074 engine->context_unpin = intel_ring_context_unpin;
2075
Chris Wilsonf73e7392016-12-18 15:37:24 +00002076 engine->request_alloc = ring_request_alloc;
2077
Chris Wilson9b81d552016-10-28 13:58:50 +01002078 engine->emit_breadcrumb = i9xx_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002079 engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
2080 if (i915.semaphores) {
2081 int num_rings;
2082
Chris Wilson9b81d552016-10-28 13:58:50 +01002083 engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002084
2085 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
2086 if (INTEL_GEN(dev_priv) >= 8) {
2087 engine->emit_breadcrumb_sz += num_rings * 6;
2088 } else {
2089 engine->emit_breadcrumb_sz += num_rings * 3;
2090 if (num_rings & 1)
2091 engine->emit_breadcrumb_sz++;
2092 }
2093 }
Chris Wilsonff44ad52017-03-16 17:13:03 +00002094
2095 engine->set_default_submission = i9xx_set_default_submission;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002096
2097 if (INTEL_GEN(dev_priv) >= 8)
Chris Wilson803688b2016-08-02 22:50:27 +01002098 engine->emit_bb_start = gen8_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002099 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson803688b2016-08-02 22:50:27 +01002100 engine->emit_bb_start = gen6_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002101 else if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson803688b2016-08-02 22:50:27 +01002102 engine->emit_bb_start = i965_emit_bb_start;
Jani Nikula2a307c22016-11-30 17:43:04 +02002103 else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002104 engine->emit_bb_start = i830_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002105 else
Chris Wilson803688b2016-08-02 22:50:27 +01002106 engine->emit_bb_start = i915_emit_bb_start;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002107}
2108
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002109int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002110{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002111 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07002112 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002113
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002114 intel_ring_default_vfuncs(dev_priv, engine);
2115
Chris Wilson61ff75a2016-07-01 17:23:28 +01002116 if (HAS_L3_DPF(dev_priv))
2117 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002118
Chris Wilsonc0336662016-05-06 15:40:21 +01002119 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002120 engine->init_context = intel_rcs_ctx_init;
Chris Wilson9b81d552016-10-28 13:58:50 +01002121 engine->emit_breadcrumb = gen8_render_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002122 engine->emit_breadcrumb_sz = gen8_render_emit_breadcrumb_sz;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002123 engine->emit_flush = gen8_render_ring_flush;
Chris Wilson98f29e82016-10-28 13:58:51 +01002124 if (i915.semaphores) {
2125 int num_rings;
2126
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002127 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilson98f29e82016-10-28 13:58:51 +01002128
2129 num_rings =
2130 hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
2131 engine->emit_breadcrumb_sz += num_rings * 6;
2132 }
Chris Wilsonc0336662016-05-06 15:40:21 +01002133 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002134 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002135 engine->emit_flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002136 if (IS_GEN6(dev_priv))
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002137 engine->emit_flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002138 } else if (IS_GEN5(dev_priv)) {
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002139 engine->emit_flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002140 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002141 if (INTEL_GEN(dev_priv) < 4)
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002142 engine->emit_flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002143 else
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002144 engine->emit_flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002145 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002146 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002147
Chris Wilsonc0336662016-05-06 15:40:21 +01002148 if (IS_HASWELL(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002149 engine->emit_bb_start = hsw_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002150
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002151 engine->init_hw = init_render_ring;
2152 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002153
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002154 ret = intel_init_ring_buffer(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002155 if (ret)
2156 return ret;
2157
Chris Wilsonf8973c22016-07-01 17:23:21 +01002158 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonf51455d2017-01-10 14:47:34 +00002159 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Chris Wilson7d5ea802016-07-01 17:23:20 +01002160 if (ret)
2161 return ret;
2162 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01002163 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002164 if (ret)
2165 return ret;
2166 }
2167
2168 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002169}
2170
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002171int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002172{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002173 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002174
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002175 intel_ring_default_vfuncs(dev_priv, engine);
2176
Chris Wilsonc0336662016-05-06 15:40:21 +01002177 if (INTEL_GEN(dev_priv) >= 6) {
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002178 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002179 if (IS_GEN6(dev_priv))
Chris Wilsonff44ad52017-03-16 17:13:03 +00002180 engine->set_default_submission = gen6_bsd_set_default_submission;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002181 engine->emit_flush = gen6_bsd_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002182 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002183 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002184 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002185 engine->mmio_base = BSD_RING_BASE;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002186 engine->emit_flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002187 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002188 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002189 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002190 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002191 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002192
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002193 return intel_init_ring_buffer(engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002194}
Chris Wilson549f7362010-10-19 11:19:32 +01002195
Zhao Yakui845f74a2014-04-17 10:37:37 +08002196/**
Damien Lespiau62659922015-01-29 14:13:40 +00002197 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002198 */
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002199int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002200{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002201 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002202
2203 intel_ring_default_vfuncs(dev_priv, engine);
2204
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002205 engine->emit_flush = gen6_bsd_ring_flush;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002206
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002207 return intel_init_ring_buffer(engine);
Zhao Yakui845f74a2014-04-17 10:37:37 +08002208}
2209
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002210int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
Chris Wilson549f7362010-10-19 11:19:32 +01002211{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002212 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002213
2214 intel_ring_default_vfuncs(dev_priv, engine);
2215
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002216 engine->emit_flush = gen6_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002217 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002218 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002219
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002220 return intel_init_ring_buffer(engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002221}
Chris Wilsona7b97612012-07-20 12:41:08 +01002222
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002223int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002224{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002225 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002226
2227 intel_ring_default_vfuncs(dev_priv, engine);
2228
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002229 engine->emit_flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002230
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002231 if (INTEL_GEN(dev_priv) < 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002232 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01002233 engine->irq_enable = hsw_vebox_irq_enable;
2234 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002235 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002236
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002237 return intel_init_ring_buffer(engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002238}