blob: 4472752812cbf373d38dc307b5f65d53e99ec9f0 [file] [log] [blame]
Eric Anholt62fdfea2010-05-21 13:26:39 -07001/*
2 * Copyright © 2008-2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
27 *
28 */
29
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +080030#include <linux/log2.h>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070032#include "i915_drv.h"
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/i915_drm.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070034#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010035#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070036
Chris Wilsona0442462016-04-29 09:07:05 +010037/* Rough estimate of the typical request size, performing a flush,
38 * set-context and then emitting the batch.
39 */
40#define LEGACY_REQUEST_SIZE 200
41
Oscar Mateo82e104c2014-07-24 17:04:26 +010042int __intel_ring_space(int head, int tail, int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010043{
Dave Gordon4f547412014-11-27 11:22:48 +000044 int space = head - tail;
45 if (space <= 0)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010046 space += size;
Dave Gordon4f547412014-11-27 11:22:48 +000047 return space - I915_RING_FREE_SPACE;
Chris Wilson1cf0ba12014-05-05 09:07:33 +010048}
49
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{
Chris Wilson7e37f882016-08-02 22:50:21 +010064 struct intel_ring *ring = req->ring;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010065 u32 cmd;
66 int ret;
67
68 cmd = MI_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010069
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010070 if (mode & EMIT_INVALIDATE)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010071 cmd |= MI_READ_FLUSH;
72
John Harrison5fb9de12015-05-29 17:44:07 +010073 ret = intel_ring_begin(req, 2);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010074 if (ret)
75 return ret;
76
Chris Wilsonb5321f32016-08-02 22:50:18 +010077 intel_ring_emit(ring, cmd);
78 intel_ring_emit(ring, MI_NOOP);
79 intel_ring_advance(ring);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010080
81 return 0;
82}
83
84static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010085gen4_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Eric Anholt62fdfea2010-05-21 13:26:39 -070086{
Chris Wilson7e37f882016-08-02 22:50:21 +010087 struct intel_ring *ring = req->ring;
Chris Wilson6f392d5482010-08-07 11:01:22 +010088 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000089 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +010090
Chris Wilson36d527d2011-03-19 22:26:49 +000091 /*
92 * read/write caches:
93 *
94 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
95 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
96 * also flushed at 2d versus 3d pipeline switches.
97 *
98 * read-only caches:
99 *
100 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
101 * MI_READ_FLUSH is set, and is always flushed on 965.
102 *
103 * I915_GEM_DOMAIN_COMMAND may not exist?
104 *
105 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
106 * invalidated when MI_EXE_FLUSH is set.
107 *
108 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
109 * invalidated with every MI_FLUSH.
110 *
111 * TLBs:
112 *
113 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
114 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
115 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
116 * are flushed at any MI_FLUSH.
117 */
118
Chris Wilsonb5321f32016-08-02 22:50:18 +0100119 cmd = MI_FLUSH;
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100120 if (mode & EMIT_INVALIDATE) {
Chris Wilson36d527d2011-03-19 22:26:49 +0000121 cmd |= MI_EXE_FLUSH;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100122 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
123 cmd |= MI_INVALIDATE_ISP;
124 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000125
John Harrison5fb9de12015-05-29 17:44:07 +0100126 ret = intel_ring_begin(req, 2);
Chris Wilson36d527d2011-03-19 22:26:49 +0000127 if (ret)
128 return ret;
129
Chris Wilsonb5321f32016-08-02 22:50:18 +0100130 intel_ring_emit(ring, cmd);
131 intel_ring_emit(ring, MI_NOOP);
132 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000133
134 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800135}
136
Jesse Barnes8d315282011-10-16 10:23:31 +0200137/**
138 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
139 * implementing two workarounds on gen6. From section 1.4.7.1
140 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
141 *
142 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
143 * produced by non-pipelined state commands), software needs to first
144 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
145 * 0.
146 *
147 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
148 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
149 *
150 * And the workaround for these two requires this workaround first:
151 *
152 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
153 * BEFORE the pipe-control with a post-sync op and no write-cache
154 * flushes.
155 *
156 * And this last workaround is tricky because of the requirements on
157 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
158 * volume 2 part 1:
159 *
160 * "1 of the following must also be set:
161 * - Render Target Cache Flush Enable ([12] of DW1)
162 * - Depth Cache Flush Enable ([0] of DW1)
163 * - Stall at Pixel Scoreboard ([1] of DW1)
164 * - Depth Stall ([13] of DW1)
165 * - Post-Sync Operation ([13] of DW1)
166 * - Notify Enable ([8] of DW1)"
167 *
168 * The cache flushes require the workaround flush that triggered this
169 * one, so we can't use it. Depth stall would trigger the same.
170 * Post-sync nonzero is what triggered this second workaround, so we
171 * can't use that one either. Notify enable is IRQs, which aren't
172 * really our business. That leaves only stall at scoreboard.
173 */
174static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100175intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200176{
Chris Wilson7e37f882016-08-02 22:50:21 +0100177 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100178 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100179 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200180 int ret;
181
John Harrison5fb9de12015-05-29 17:44:07 +0100182 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200183 if (ret)
184 return ret;
185
Chris Wilsonb5321f32016-08-02 22:50:18 +0100186 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
187 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
Jesse Barnes8d315282011-10-16 10:23:31 +0200188 PIPE_CONTROL_STALL_AT_SCOREBOARD);
Chris Wilsonb5321f32016-08-02 22:50:18 +0100189 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
190 intel_ring_emit(ring, 0); /* low dword */
191 intel_ring_emit(ring, 0); /* high dword */
192 intel_ring_emit(ring, MI_NOOP);
193 intel_ring_advance(ring);
Jesse Barnes8d315282011-10-16 10:23:31 +0200194
John Harrison5fb9de12015-05-29 17:44:07 +0100195 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200196 if (ret)
197 return ret;
198
Chris Wilsonb5321f32016-08-02 22:50:18 +0100199 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
200 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
201 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
202 intel_ring_emit(ring, 0);
203 intel_ring_emit(ring, 0);
204 intel_ring_emit(ring, MI_NOOP);
205 intel_ring_advance(ring);
Jesse Barnes8d315282011-10-16 10:23:31 +0200206
207 return 0;
208}
209
210static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100211gen6_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Jesse Barnes8d315282011-10-16 10:23:31 +0200212{
Chris Wilson7e37f882016-08-02 22:50:21 +0100213 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100214 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100215 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200216 u32 flags = 0;
Jesse Barnes8d315282011-10-16 10:23:31 +0200217 int ret;
218
Paulo Zanonib3111502012-08-17 18:35:42 -0300219 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100220 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300221 if (ret)
222 return ret;
223
Jesse Barnes8d315282011-10-16 10:23:31 +0200224 /* Just flush everything. Experiments have shown that reducing the
225 * number of bits based on the write domains has little performance
226 * impact.
227 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100228 if (mode & EMIT_FLUSH) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100229 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
230 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
231 /*
232 * Ensure that any following seqno writes only happen
233 * when the render cache is indeed flushed.
234 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200235 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100236 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100237 if (mode & EMIT_INVALIDATE) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100238 flags |= PIPE_CONTROL_TLB_INVALIDATE;
239 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
240 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
241 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
242 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
243 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
244 /*
245 * TLB invalidate requires a post-sync write.
246 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700247 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100248 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200249
John Harrison5fb9de12015-05-29 17:44:07 +0100250 ret = intel_ring_begin(req, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200251 if (ret)
252 return ret;
253
Chris Wilsonb5321f32016-08-02 22:50:18 +0100254 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
255 intel_ring_emit(ring, flags);
256 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
257 intel_ring_emit(ring, 0);
258 intel_ring_advance(ring);
Jesse Barnes8d315282011-10-16 10:23:31 +0200259
260 return 0;
261}
262
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100263static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100264gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300265{
Chris Wilson7e37f882016-08-02 22:50:21 +0100266 struct intel_ring *ring = req->ring;
Paulo Zanonif3987632012-08-17 18:35:43 -0300267 int ret;
268
John Harrison5fb9de12015-05-29 17:44:07 +0100269 ret = intel_ring_begin(req, 4);
Paulo Zanonif3987632012-08-17 18:35:43 -0300270 if (ret)
271 return ret;
272
Chris Wilsonb5321f32016-08-02 22:50:18 +0100273 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
274 intel_ring_emit(ring,
275 PIPE_CONTROL_CS_STALL |
276 PIPE_CONTROL_STALL_AT_SCOREBOARD);
277 intel_ring_emit(ring, 0);
278 intel_ring_emit(ring, 0);
279 intel_ring_advance(ring);
Paulo Zanonif3987632012-08-17 18:35:43 -0300280
281 return 0;
282}
283
284static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100285gen7_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300286{
Chris Wilson7e37f882016-08-02 22:50:21 +0100287 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100288 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100289 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300290 u32 flags = 0;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300291 int ret;
292
Paulo Zanonif3987632012-08-17 18:35:43 -0300293 /*
294 * Ensure that any following seqno writes only happen when the render
295 * cache is indeed flushed.
296 *
297 * Workaround: 4th PIPE_CONTROL command (except the ones with only
298 * read-cache invalidate bits set) must have the CS_STALL bit set. We
299 * don't try to be clever and just set it unconditionally.
300 */
301 flags |= PIPE_CONTROL_CS_STALL;
302
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300303 /* Just flush everything. Experiments have shown that reducing the
304 * number of bits based on the write domains has little performance
305 * impact.
306 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100307 if (mode & EMIT_FLUSH) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300308 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
309 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800310 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100311 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300312 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100313 if (mode & EMIT_INVALIDATE) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300314 flags |= PIPE_CONTROL_TLB_INVALIDATE;
315 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
316 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
317 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
318 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
319 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000320 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300321 /*
322 * TLB invalidate requires a post-sync write.
323 */
324 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200325 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300326
Chris Wilsonadd284a2014-12-16 08:44:32 +0000327 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
328
Paulo Zanonif3987632012-08-17 18:35:43 -0300329 /* Workaround: we must issue a pipe_control with CS-stall bit
330 * set before a pipe_control command that has the state cache
331 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100332 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300333 }
334
John Harrison5fb9de12015-05-29 17:44:07 +0100335 ret = intel_ring_begin(req, 4);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300336 if (ret)
337 return ret;
338
Chris Wilsonb5321f32016-08-02 22:50:18 +0100339 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
340 intel_ring_emit(ring, flags);
341 intel_ring_emit(ring, scratch_addr);
342 intel_ring_emit(ring, 0);
343 intel_ring_advance(ring);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300344
345 return 0;
346}
347
Ben Widawskya5f3d682013-11-02 21:07:27 -0700348static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100349gen8_emit_pipe_control(struct drm_i915_gem_request *req,
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300350 u32 flags, u32 scratch_addr)
351{
Chris Wilson7e37f882016-08-02 22:50:21 +0100352 struct intel_ring *ring = req->ring;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300353 int ret;
354
John Harrison5fb9de12015-05-29 17:44:07 +0100355 ret = intel_ring_begin(req, 6);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300356 if (ret)
357 return ret;
358
Chris Wilsonb5321f32016-08-02 22:50:18 +0100359 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
360 intel_ring_emit(ring, flags);
361 intel_ring_emit(ring, scratch_addr);
362 intel_ring_emit(ring, 0);
363 intel_ring_emit(ring, 0);
364 intel_ring_emit(ring, 0);
365 intel_ring_advance(ring);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300366
367 return 0;
368}
369
370static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100371gen8_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Ben Widawskya5f3d682013-11-02 21:07:27 -0700372{
Chris Wilson56c0f1a2016-08-15 10:48:58 +0100373 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100374 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100375 u32 flags = 0;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800376 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700377
378 flags |= PIPE_CONTROL_CS_STALL;
379
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100380 if (mode & EMIT_FLUSH) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700381 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
382 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800383 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100384 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700385 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100386 if (mode & EMIT_INVALIDATE) {
Ben Widawskya5f3d682013-11-02 21:07:27 -0700387 flags |= PIPE_CONTROL_TLB_INVALIDATE;
388 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
389 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
390 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
391 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
392 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
393 flags |= PIPE_CONTROL_QW_WRITE;
394 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800395
396 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100397 ret = gen8_emit_pipe_control(req,
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800398 PIPE_CONTROL_CS_STALL |
399 PIPE_CONTROL_STALL_AT_SCOREBOARD,
400 0);
401 if (ret)
402 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700403 }
404
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100405 return gen8_emit_pipe_control(req, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700406}
407
Chris Wilson7e37f882016-08-02 22:50:21 +0100408u64 intel_engine_get_active_head(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800409{
Chris Wilsonc0336662016-05-06 15:40:21 +0100410 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson50877442014-03-21 12:41:53 +0000411 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800412
Chris Wilsonc0336662016-05-06 15:40:21 +0100413 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000414 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
415 RING_ACTHD_UDW(engine->mmio_base));
Chris Wilsonc0336662016-05-06 15:40:21 +0100416 else if (INTEL_GEN(dev_priv) >= 4)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000417 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
Chris Wilson50877442014-03-21 12:41:53 +0000418 else
419 acthd = I915_READ(ACTHD);
420
421 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800422}
423
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000424static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200425{
Chris Wilsonc0336662016-05-06 15:40:21 +0100426 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200427 u32 addr;
428
429 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100430 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200431 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
432 I915_WRITE(HWS_PGA, addr);
433}
434
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000435static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000436{
Chris Wilsonc0336662016-05-06 15:40:21 +0100437 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200438 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000439
440 /* The ring status page addresses are no longer next to the rest of
441 * the ring registers as of gen7.
442 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100443 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000444 switch (engine->id) {
Damien Lespiauaf75f262015-02-10 19:32:17 +0000445 case RCS:
446 mmio = RENDER_HWS_PGA_GEN7;
447 break;
448 case BCS:
449 mmio = BLT_HWS_PGA_GEN7;
450 break;
451 /*
452 * VCS2 actually doesn't exist on Gen7. Only shut up
453 * gcc switch check warning
454 */
455 case VCS2:
456 case VCS:
457 mmio = BSD_HWS_PGA_GEN7;
458 break;
459 case VECS:
460 mmio = VEBOX_HWS_PGA_GEN7;
461 break;
462 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100463 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000464 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000465 } else {
466 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000467 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000468 }
469
Chris Wilson57e88532016-08-15 10:48:57 +0100470 I915_WRITE(mmio, engine->status_page.ggtt_offset);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000471 POSTING_READ(mmio);
472
473 /*
474 * Flush the TLB for this page
475 *
476 * FIXME: These two bits have disappeared on gen8, so a question
477 * arises: do we still need this and if so how should we go about
478 * invalidating the TLB?
479 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100480 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000481 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000482
483 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000484 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000485
486 I915_WRITE(reg,
487 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
488 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100489 if (intel_wait_for_register(dev_priv,
490 reg, INSTPM_SYNC_FLUSH, 0,
491 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000492 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000493 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000494 }
495}
496
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000497static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100498{
Chris Wilsonc0336662016-05-06 15:40:21 +0100499 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100500
Chris Wilson21a2c582016-08-15 10:49:11 +0100501 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000502 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100503 if (intel_wait_for_register(dev_priv,
504 RING_MI_MODE(engine->mmio_base),
505 MODE_IDLE,
506 MODE_IDLE,
507 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000508 DRM_ERROR("%s : timed out trying to stop ring\n",
509 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100510 /* Sometimes we observe that the idle flag is not
511 * set even though the ring is empty. So double
512 * check before giving up.
513 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000514 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100515 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100516 }
517 }
518
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000519 I915_WRITE_CTL(engine, 0);
520 I915_WRITE_HEAD(engine, 0);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100521 I915_WRITE_TAIL(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100522
Chris Wilson21a2c582016-08-15 10:49:11 +0100523 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000524 (void)I915_READ_CTL(engine);
525 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
Chris Wilson9991ae72014-04-02 16:36:07 +0100526 }
527
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000528 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100529}
530
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000531static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800532{
Chris Wilsonc0336662016-05-06 15:40:21 +0100533 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +0100534 struct intel_ring *ring = engine->buffer;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200535 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800536
Mika Kuoppala59bad942015-01-16 11:34:40 +0200537 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200538
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000539 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100540 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000541 DRM_DEBUG_KMS("%s head not reset to zero "
542 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000543 engine->name,
544 I915_READ_CTL(engine),
545 I915_READ_HEAD(engine),
546 I915_READ_TAIL(engine),
547 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800548
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000549 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000550 DRM_ERROR("failed to set %s head to zero "
551 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000552 engine->name,
553 I915_READ_CTL(engine),
554 I915_READ_HEAD(engine),
555 I915_READ_TAIL(engine),
556 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100557 ret = -EIO;
558 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000559 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700560 }
561
Chris Wilsonc0336662016-05-06 15:40:21 +0100562 if (I915_NEED_GFX_HWS(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000563 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100564 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000565 ring_setup_phys_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100566
Jiri Kosinaece4a172014-08-07 16:29:53 +0200567 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000568 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200569
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200570 /* Initialize the ring. This must happen _after_ we've cleared the ring
571 * registers with the above sequence (the readback of the HEAD registers
572 * also enforces ordering), otherwise the hw might lose the new ring
573 * register values. */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100574 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
Chris Wilson95468892014-08-07 15:39:54 +0100575
576 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000577 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100578 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000579 engine->name, I915_READ_HEAD(engine));
580 I915_WRITE_HEAD(engine, 0);
581 (void)I915_READ_HEAD(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100582
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000583 I915_WRITE_CTL(engine,
Chris Wilson7e37f882016-08-02 22:50:21 +0100584 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000585 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800586
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800587 /* If the head is still not zero, the ring is dead */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000588 if (wait_for((I915_READ_CTL(engine) & RING_VALID) != 0 &&
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100589 I915_READ_START(engine) == i915_ggtt_offset(ring->vma) &&
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000590 (I915_READ_HEAD(engine) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000591 DRM_ERROR("%s initialization failed "
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100592 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08x]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000593 engine->name,
594 I915_READ_CTL(engine),
595 I915_READ_CTL(engine) & RING_VALID,
596 I915_READ_HEAD(engine), I915_READ_TAIL(engine),
597 I915_READ_START(engine),
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100598 i915_ggtt_offset(ring->vma));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200599 ret = -EIO;
600 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800601 }
602
Chris Wilson7e37f882016-08-02 22:50:21 +0100603 ring->last_retired_head = -1;
604 ring->head = I915_READ_HEAD(engine);
605 ring->tail = I915_READ_TAIL(engine) & TAIL_ADDR;
606 intel_ring_update_space(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000607
Tomas Elffc0768c2016-03-21 16:26:59 +0000608 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100609
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200610out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200611 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200612
613 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700614}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800615
John Harrisone2be4fa2015-05-29 17:43:54 +0100616static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100617{
Chris Wilson7e37f882016-08-02 22:50:21 +0100618 struct intel_ring *ring = req->ring;
Chris Wilsonc0336662016-05-06 15:40:21 +0100619 struct i915_workarounds *w = &req->i915->workarounds;
620 int ret, i;
Arun Siluvery888b5992014-08-26 14:44:51 +0100621
Francisco Jerez02235802015-10-07 14:44:01 +0300622 if (w->count == 0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300623 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100624
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100625 ret = req->engine->emit_flush(req, EMIT_BARRIER);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100626 if (ret)
627 return ret;
628
John Harrison5fb9de12015-05-29 17:44:07 +0100629 ret = intel_ring_begin(req, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300630 if (ret)
631 return ret;
632
Chris Wilsonb5321f32016-08-02 22:50:18 +0100633 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300634 for (i = 0; i < w->count; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +0100635 intel_ring_emit_reg(ring, w->reg[i].addr);
636 intel_ring_emit(ring, w->reg[i].value);
Mika Kuoppala72253422014-10-07 17:21:26 +0300637 }
Chris Wilsonb5321f32016-08-02 22:50:18 +0100638 intel_ring_emit(ring, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300639
Chris Wilsonb5321f32016-08-02 22:50:18 +0100640 intel_ring_advance(ring);
Mika Kuoppala72253422014-10-07 17:21:26 +0300641
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100642 ret = req->engine->emit_flush(req, EMIT_BARRIER);
Mika Kuoppala72253422014-10-07 17:21:26 +0300643 if (ret)
644 return ret;
645
646 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
647
648 return 0;
649}
650
John Harrison87531812015-05-29 17:43:44 +0100651static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100652{
653 int ret;
654
John Harrisone2be4fa2015-05-29 17:43:54 +0100655 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100656 if (ret != 0)
657 return ret;
658
John Harrisonbe013632015-05-29 17:43:45 +0100659 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100660 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000661 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100662
Chris Wilsone26e1b92016-01-29 16:49:05 +0000663 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100664}
665
Mika Kuoppala72253422014-10-07 17:21:26 +0300666static int wa_add(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200667 i915_reg_t addr,
668 const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300669{
670 const u32 idx = dev_priv->workarounds.count;
671
672 if (WARN_ON(idx >= I915_MAX_WA_REGS))
673 return -ENOSPC;
674
675 dev_priv->workarounds.reg[idx].addr = addr;
676 dev_priv->workarounds.reg[idx].value = val;
677 dev_priv->workarounds.reg[idx].mask = mask;
678
679 dev_priv->workarounds.count++;
680
681 return 0;
682}
683
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100684#define WA_REG(addr, mask, val) do { \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000685 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300686 if (r) \
687 return r; \
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100688 } while (0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300689
690#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000691 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300692
693#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000694 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300695
Damien Lespiau98533252014-12-08 17:33:51 +0000696#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000697 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300698
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000699#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
700#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300701
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000702#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300703
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000704static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
705 i915_reg_t reg)
Arun Siluvery33136b02016-01-21 21:43:47 +0000706{
Chris Wilsonc0336662016-05-06 15:40:21 +0100707 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery33136b02016-01-21 21:43:47 +0000708 struct i915_workarounds *wa = &dev_priv->workarounds;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000709 const uint32_t index = wa->hw_whitelist_count[engine->id];
Arun Siluvery33136b02016-01-21 21:43:47 +0000710
711 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
712 return -EINVAL;
713
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000714 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
Arun Siluvery33136b02016-01-21 21:43:47 +0000715 i915_mmio_reg_offset(reg));
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000716 wa->hw_whitelist_count[engine->id]++;
Arun Siluvery33136b02016-01-21 21:43:47 +0000717
718 return 0;
719}
720
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000721static int gen8_init_workarounds(struct intel_engine_cs *engine)
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100722{
Chris Wilsonc0336662016-05-06 15:40:21 +0100723 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery68c61982015-09-25 17:40:38 +0100724
725 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100726
Arun Siluvery717d84d2015-09-25 17:40:39 +0100727 /* WaDisableAsyncFlipPerfMode:bdw,chv */
728 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
729
Arun Siluveryd0581192015-09-25 17:40:40 +0100730 /* WaDisablePartialInstShootdown:bdw,chv */
731 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
732 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
733
Arun Siluverya340af52015-09-25 17:40:45 +0100734 /* Use Force Non-Coherent whenever executing a 3D context. This is a
735 * workaround for for a possible hang in the unlikely event a TLB
736 * invalidation occurs during a PSD flush.
737 */
738 /* WaForceEnableNonCoherent:bdw,chv */
Arun Siluvery120f5d22015-09-25 17:40:46 +0100739 /* WaHdcDisableFetchWhenMasked:bdw,chv */
Arun Siluverya340af52015-09-25 17:40:45 +0100740 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Arun Siluvery120f5d22015-09-25 17:40:46 +0100741 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Arun Siluverya340af52015-09-25 17:40:45 +0100742 HDC_FORCE_NON_COHERENT);
743
Arun Siluvery6def8fd2015-09-25 17:40:42 +0100744 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
745 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
746 * polygons in the same 8x4 pixel/sample area to be processed without
747 * stalling waiting for the earlier ones to write to Hierarchical Z
748 * buffer."
749 *
750 * This optimization is off by default for BDW and CHV; turn it on.
751 */
752 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
753
Arun Siluvery48404632015-09-25 17:40:43 +0100754 /* Wa4x4STCOptimizationDisable:bdw,chv */
755 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
756
Arun Siluvery7eebcde2015-09-25 17:40:44 +0100757 /*
758 * BSpec recommends 8x4 when MSAA is used,
759 * however in practice 16x4 seems fastest.
760 *
761 * Note that PS/WM thread counts depend on the WIZ hashing
762 * disable bit, which we don't touch here, but it's good
763 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
764 */
765 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
766 GEN6_WIZ_HASHING_MASK,
767 GEN6_WIZ_HASHING_16x4);
768
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100769 return 0;
770}
771
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000772static int bdw_init_workarounds(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +0300773{
Chris Wilsonc0336662016-05-06 15:40:21 +0100774 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100775 int ret;
Mika Kuoppala72253422014-10-07 17:21:26 +0300776
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000777 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100778 if (ret)
779 return ret;
780
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700781 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Arun Siluveryd0581192015-09-25 17:40:40 +0100782 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100783
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700784 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300785 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
786 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100787
Mika Kuoppala72253422014-10-07 17:21:26 +0300788 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
789 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100790
Mika Kuoppala72253422014-10-07 17:21:26 +0300791 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000792 /* WaForceContextSaveRestoreNonCoherent:bdw */
793 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000794 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Chris Wilsonc0336662016-05-06 15:40:21 +0100795 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100796
Arun Siluvery86d7f232014-08-26 14:44:50 +0100797 return 0;
798}
799
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000800static int chv_init_workarounds(struct intel_engine_cs *engine)
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300801{
Chris Wilsonc0336662016-05-06 15:40:21 +0100802 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100803 int ret;
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300804
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000805 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100806 if (ret)
807 return ret;
808
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300809 /* WaDisableThreadStallDopClockGating:chv */
Arun Siluveryd0581192015-09-25 17:40:40 +0100810 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300811
Kenneth Graunked60de812015-01-10 18:02:22 -0800812 /* Improve HiZ throughput on CHV. */
813 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
814
Mika Kuoppala72253422014-10-07 17:21:26 +0300815 return 0;
816}
817
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000818static int gen9_init_workarounds(struct intel_engine_cs *engine)
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000819{
Chris Wilsonc0336662016-05-06 15:40:21 +0100820 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000821 int ret;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000822
Tim Gorea8ab5ed2016-06-13 12:15:01 +0100823 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
824 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
825
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300826 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300827 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
828 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
829
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300830 /* WaDisableKillLogic:bxt,skl,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300831 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
832 ECOCHK_DIS_TLB);
833
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300834 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
835 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000836 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Tim Gore950b2aa2016-03-16 16:13:46 +0000837 FLOW_CONTROL_ENABLE |
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000838 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
839
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300840 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Nick Hoath84241712015-02-05 10:47:20 +0000841 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
842 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
843
Jani Nikulae87a0052015-10-20 15:22:02 +0300844 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100845 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
846 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Damien Lespiaua86eb582015-02-11 18:21:44 +0000847 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
848 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000849
Jani Nikulae87a0052015-10-20 15:22:02 +0300850 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100851 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
852 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Damien Lespiau183c6da2015-02-09 19:33:11 +0000853 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
854 GEN9_RHWO_OPTIMIZATION_DISABLE);
Arun Siluvery9b014352015-07-14 15:01:30 +0100855 /*
856 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
857 * but we do that in per ctx batchbuffer as there is an issue
858 * with this register not getting restored on ctx restore
859 */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000860 }
861
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300862 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl */
863 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
Tim Gorebfd8ad42016-04-19 15:45:52 +0100864 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
865 GEN9_ENABLE_YV12_BUGFIX |
866 GEN9_ENABLE_GPGPU_PREEMPTION);
Nick Hoathcac23df2015-02-05 10:47:22 +0000867
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300868 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
869 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
Arun Siluvery60294682015-09-25 14:33:37 +0100870 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
871 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
Damien Lespiau9370cd92015-02-09 19:33:17 +0000872
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300873 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000874 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
875 GEN9_CCS_TLB_PREFETCH_ENABLE);
876
Imre Deak5a2ae952015-05-19 15:04:59 +0300877 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100878 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_C0) ||
879 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200880 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
881 PIXEL_MASK_CAMMING_DISABLE);
882
Mika Kuoppala5b0e3652016-06-07 17:18:57 +0300883 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
884 WA_SET_BIT_MASKED(HDC_CHICKEN0,
885 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
886 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
Imre Deak8ea6f892015-05-19 17:05:42 +0300887
Mika Kuoppalabbaefe72016-06-07 17:18:58 +0300888 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
889 * both tied to WaForceContextSaveRestoreNonCoherent
890 * in some hsds for skl. We keep the tie for all gen9. The
891 * documentation is a bit hazy and so we want to get common behaviour,
892 * even though there is no clear evidence we would need both on kbl/bxt.
893 * This area has been source of system hangs so we play it safe
894 * and mimic the skl regardless of what bspec says.
895 *
896 * Use Force Non-Coherent whenever executing a 3D context. This
897 * is a workaround for a possible hang in the unlikely event
898 * a TLB invalidation occurs during a PSD flush.
899 */
900
901 /* WaForceEnableNonCoherent:skl,bxt,kbl */
902 WA_SET_BIT_MASKED(HDC_CHICKEN0,
903 HDC_FORCE_NON_COHERENT);
904
905 /* WaDisableHDCInvalidation:skl,bxt,kbl */
906 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
907 BDW_DISABLE_HDC_INVALIDATION);
908
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300909 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
910 if (IS_SKYLAKE(dev_priv) ||
911 IS_KABYLAKE(dev_priv) ||
912 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
Arun Siluvery8c761602015-09-08 10:31:48 +0100913 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
914 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery8c761602015-09-08 10:31:48 +0100915
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300916 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
Robert Beckett6b6d5622015-09-08 10:31:52 +0100917 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
918
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300919 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
Arun Siluvery6ecf56a2016-01-21 21:43:54 +0000920 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
921 GEN8_LQSC_FLUSH_COHERENT_LINES));
922
arun.siluvery@linux.intel.com6bb628552016-06-06 09:52:49 +0100923 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
924 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
925 if (ret)
926 return ret;
927
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300928 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000929 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000930 if (ret)
931 return ret;
932
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300933 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000934 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
Arun Siluvery3669ab62016-01-21 21:43:49 +0000935 if (ret)
936 return ret;
937
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000938 return 0;
939}
940
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000941static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +0000942{
Chris Wilsonc0336662016-05-06 15:40:21 +0100943 struct drm_i915_private *dev_priv = engine->i915;
Damien Lespiaub7668792015-02-14 18:30:29 +0000944 u8 vals[3] = { 0, 0, 0 };
945 unsigned int i;
946
947 for (i = 0; i < 3; i++) {
948 u8 ss;
949
950 /*
951 * Only consider slices where one, and only one, subslice has 7
952 * EUs
953 */
Imre Deak43b67992016-08-31 19:13:02 +0300954 if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
Damien Lespiaub7668792015-02-14 18:30:29 +0000955 continue;
956
957 /*
958 * subslice_7eu[i] != 0 (because of the check above) and
959 * ss_max == 4 (maximum number of subslices possible per slice)
960 *
961 * -> 0 <= ss <= 3;
962 */
Imre Deak43b67992016-08-31 19:13:02 +0300963 ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
Damien Lespiaub7668792015-02-14 18:30:29 +0000964 vals[i] = 3 - ss;
965 }
966
967 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
968 return 0;
969
970 /* Tune IZ hashing. See intel_device_info_runtime_init() */
971 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
972 GEN9_IZ_HASHING_MASK(2) |
973 GEN9_IZ_HASHING_MASK(1) |
974 GEN9_IZ_HASHING_MASK(0),
975 GEN9_IZ_HASHING(2, vals[2]) |
976 GEN9_IZ_HASHING(1, vals[1]) |
977 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +0000978
Mika Kuoppala72253422014-10-07 17:21:26 +0300979 return 0;
980}
981
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000982static int skl_init_workarounds(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +0000983{
Chris Wilsonc0336662016-05-06 15:40:21 +0100984 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +0100985 int ret;
Damien Lespiaud0bbbc42015-02-09 19:33:16 +0000986
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000987 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +0100988 if (ret)
989 return ret;
Damien Lespiau8d205492015-02-09 19:33:15 +0000990
Arun Siluverya78536e2016-01-21 21:43:53 +0000991 /*
992 * Actual WA is to disable percontext preemption granularity control
993 * until D0 which is the default case so this is equivalent to
994 * !WaDisablePerCtxtPreemptionGranularityControl:skl
995 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100996 if (IS_SKL_REVID(dev_priv, SKL_REVID_E0, REVID_FOREVER)) {
Arun Siluverya78536e2016-01-21 21:43:53 +0000997 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
998 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
999 }
1000
Mika Kuoppala71dce582016-06-07 17:19:14 +03001001 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001002 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1003 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1004 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1005 }
1006
1007 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1008 * involving this register should also be added to WA batch as required.
1009 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001010 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001011 /* WaDisableLSQCROPERFforOCL:skl */
1012 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1013 GEN8_LQSC_RO_PERF_DIS);
1014
1015 /* WaEnableGapsTsvCreditFix:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001016 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, REVID_FOREVER)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001017 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1018 GEN9_GAPS_TSV_CREDIT_DISABLE));
1019 }
1020
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001021 /* WaDisablePowerCompilerClockGating:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001022 if (IS_SKL_REVID(dev_priv, SKL_REVID_B0, SKL_REVID_B0))
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001023 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1024 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1025
Jani Nikulae87a0052015-10-20 15:22:02 +03001026 /* WaBarrierPerformanceFixDisable:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001027 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_D0))
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001028 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1029 HDC_FENCE_DEST_SLM_DISABLE |
1030 HDC_BARRIER_PERFORMANCE_DISABLE);
1031
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001032 /* WaDisableSbeCacheDispatchPortSharing:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001033 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0))
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001034 WA_SET_BIT_MASKED(
1035 GEN7_HALF_SLICE_CHICKEN1,
1036 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001037
Mika Kuoppalaeee8efb2016-06-07 17:18:53 +03001038 /* WaDisableGafsUnitClkGating:skl */
1039 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1040
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +03001041 /* WaInPlaceDecompressionHang:skl */
1042 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
1043 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1044 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1045
Arun Siluvery61074972016-01-21 21:43:52 +00001046 /* WaDisableLSQCROPERFforOCL:skl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001047 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluvery61074972016-01-21 21:43:52 +00001048 if (ret)
1049 return ret;
1050
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001051 return skl_tune_iz_hashing(engine);
Damien Lespiau8d205492015-02-09 19:33:15 +00001052}
1053
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001054static int bxt_init_workarounds(struct intel_engine_cs *engine)
Nick Hoathcae04372015-03-17 11:39:38 +02001055{
Chris Wilsonc0336662016-05-06 15:40:21 +01001056 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001057 int ret;
Nick Hoathdfb601e2015-04-10 13:12:24 +01001058
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001059 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001060 if (ret)
1061 return ret;
Nick Hoathcae04372015-03-17 11:39:38 +02001062
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001063 /* WaStoreMultiplePTEenable:bxt */
1064 /* This is a requirement according to Hardware specification */
Chris Wilsonc0336662016-05-06 15:40:21 +01001065 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001066 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1067
1068 /* WaSetClckGatingDisableMedia:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001069 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001070 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1071 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1072 }
1073
Nick Hoathdfb601e2015-04-10 13:12:24 +01001074 /* WaDisableThreadStallDopClockGating:bxt */
1075 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1076 STALL_DOP_GATING_DISABLE);
1077
arun.siluvery@linux.intel.com780f0ae2016-06-03 11:16:10 +01001078 /* WaDisablePooledEuLoadBalancingFix:bxt */
1079 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1080 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1081 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1082 }
1083
Nick Hoath983b4b92015-04-10 13:12:25 +01001084 /* WaDisableSbeCacheDispatchPortSharing:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001085 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
Nick Hoath983b4b92015-04-10 13:12:25 +01001086 WA_SET_BIT_MASKED(
1087 GEN7_HALF_SLICE_CHICKEN1,
1088 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1089 }
1090
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001091 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1092 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1093 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
Arun Siluverya786d532016-01-21 21:43:51 +00001094 /* WaDisableLSQCROPERFforOCL:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001095 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001096 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001097 if (ret)
1098 return ret;
Arun Siluverya786d532016-01-21 21:43:51 +00001099
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001100 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluverya786d532016-01-21 21:43:51 +00001101 if (ret)
1102 return ret;
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001103 }
1104
Tim Gore050fc462016-04-22 09:46:01 +01001105 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001106 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
Imre Deak36579cb2016-05-03 15:54:20 +03001107 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1108 L3_HIGH_PRIO_CREDITS(2));
Tim Gore050fc462016-04-22 09:46:01 +01001109
Matthew Auld575e3cc2016-08-02 09:36:53 +01001110 /* WaToEnableHwFixForPushConstHWBug:bxt */
1111 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001112 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1113 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1114
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +03001115 /* WaInPlaceDecompressionHang:bxt */
1116 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
1117 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1118 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1119
Nick Hoathcae04372015-03-17 11:39:38 +02001120 return 0;
1121}
1122
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001123static int kbl_init_workarounds(struct intel_engine_cs *engine)
1124{
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001125 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001126 int ret;
1127
1128 ret = gen9_init_workarounds(engine);
1129 if (ret)
1130 return ret;
1131
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001132 /* WaEnableGapsTsvCreditFix:kbl */
1133 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1134 GEN9_GAPS_TSV_CREDIT_DISABLE));
1135
Mika Kuoppalac0b730d2016-06-07 17:19:06 +03001136 /* WaDisableDynamicCreditSharing:kbl */
1137 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1138 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1139 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1140
Mika Kuoppala8401d422016-06-07 17:19:00 +03001141 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1142 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1143 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1144 HDC_FENCE_DEST_SLM_DISABLE);
1145
Mika Kuoppalafe905812016-06-07 17:19:03 +03001146 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1147 * involving this register should also be added to WA batch as required.
1148 */
1149 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1150 /* WaDisableLSQCROPERFforOCL:kbl */
1151 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1152 GEN8_LQSC_RO_PERF_DIS);
1153
Matthew Auld575e3cc2016-08-02 09:36:53 +01001154 /* WaToEnableHwFixForPushConstHWBug:kbl */
1155 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001156 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1157 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1158
Mika Kuoppala4de5d7c2016-06-07 17:19:11 +03001159 /* WaDisableGafsUnitClkGating:kbl */
1160 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1161
Mika Kuoppala954337a2016-06-07 17:19:12 +03001162 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1163 WA_SET_BIT_MASKED(
1164 GEN7_HALF_SLICE_CHICKEN1,
1165 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1166
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +03001167 /* WaInPlaceDecompressionHang:kbl */
1168 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1169 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1170
Mika Kuoppalafe905812016-06-07 17:19:03 +03001171 /* WaDisableLSQCROPERFforOCL:kbl */
1172 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1173 if (ret)
1174 return ret;
1175
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001176 return 0;
1177}
1178
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001179int init_workarounds_ring(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +03001180{
Chris Wilsonc0336662016-05-06 15:40:21 +01001181 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala72253422014-10-07 17:21:26 +03001182
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001183 WARN_ON(engine->id != RCS);
Mika Kuoppala72253422014-10-07 17:21:26 +03001184
1185 dev_priv->workarounds.count = 0;
Arun Siluvery33136b02016-01-21 21:43:47 +00001186 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
Mika Kuoppala72253422014-10-07 17:21:26 +03001187
Chris Wilsonc0336662016-05-06 15:40:21 +01001188 if (IS_BROADWELL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001189 return bdw_init_workarounds(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +03001190
Chris Wilsonc0336662016-05-06 15:40:21 +01001191 if (IS_CHERRYVIEW(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001192 return chv_init_workarounds(engine);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001193
Chris Wilsonc0336662016-05-06 15:40:21 +01001194 if (IS_SKYLAKE(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001195 return skl_init_workarounds(engine);
Nick Hoathcae04372015-03-17 11:39:38 +02001196
Chris Wilsonc0336662016-05-06 15:40:21 +01001197 if (IS_BROXTON(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001198 return bxt_init_workarounds(engine);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001199
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001200 if (IS_KABYLAKE(dev_priv))
1201 return kbl_init_workarounds(engine);
1202
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001203 return 0;
1204}
1205
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001206static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001207{
Chris Wilsonc0336662016-05-06 15:40:21 +01001208 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001209 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001210 if (ret)
1211 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001212
Akash Goel61a563a2014-03-25 18:01:50 +05301213 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001214 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001215 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001216
1217 /* We need to disable the AsyncFlip performance optimisations in order
1218 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1219 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001220 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001221 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001222 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001223 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001224 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1225
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001226 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301227 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +01001228 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001229 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001230 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001231
Akash Goel01fa0302014-03-24 23:00:04 +05301232 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +01001233 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001234 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301235 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001236 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001237
Chris Wilsonc0336662016-05-06 15:40:21 +01001238 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001239 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1240 * "If this bit is set, STCunit will have LRA as replacement
1241 * policy. [...] This bit must be reset. LRA replacement
1242 * policy is not supported."
1243 */
1244 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001245 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001246 }
1247
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001248 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001249 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001250
Ville Syrjälä035ea402016-07-12 19:24:47 +03001251 if (INTEL_INFO(dev_priv)->gen >= 6)
1252 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -07001253
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001254 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001255}
1256
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001257static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001258{
Chris Wilsonc0336662016-05-06 15:40:21 +01001259 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001260
Chris Wilson19880c42016-08-15 10:49:05 +01001261 i915_vma_unpin_and_release(&dev_priv->semaphore);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001262}
1263
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001264static int gen8_rcs_signal(struct drm_i915_gem_request *req)
Ben Widawsky3e789982014-06-30 09:53:37 -07001265{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001266 struct intel_ring *ring = req->ring;
1267 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001268 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001269 enum intel_engine_id id;
1270 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001271
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +01001272 num_rings = INTEL_INFO(dev_priv)->num_rings;
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001273 ret = intel_ring_begin(req, (num_rings-1) * 8);
Ben Widawsky3e789982014-06-30 09:53:37 -07001274 if (ret)
1275 return ret;
1276
Dave Gordonc3232b12016-03-23 18:19:53 +00001277 for_each_engine_id(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001278 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001279 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1280 continue;
1281
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001282 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
1283 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001284 PIPE_CONTROL_GLOBAL_GTT_IVB |
1285 PIPE_CONTROL_QW_WRITE |
1286 PIPE_CONTROL_CS_STALL);
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001287 intel_ring_emit(ring, lower_32_bits(gtt_offset));
1288 intel_ring_emit(ring, upper_32_bits(gtt_offset));
1289 intel_ring_emit(ring, req->fence.seqno);
1290 intel_ring_emit(ring, 0);
1291 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001292 MI_SEMAPHORE_SIGNAL |
1293 MI_SEMAPHORE_TARGET(waiter->hw_id));
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001294 intel_ring_emit(ring, 0);
Ben Widawsky3e789982014-06-30 09:53:37 -07001295 }
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001296 intel_ring_advance(ring);
Ben Widawsky3e789982014-06-30 09:53:37 -07001297
1298 return 0;
1299}
1300
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001301static int gen8_xcs_signal(struct drm_i915_gem_request *req)
Ben Widawsky3e789982014-06-30 09:53:37 -07001302{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001303 struct intel_ring *ring = req->ring;
1304 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001305 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001306 enum intel_engine_id id;
1307 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001308
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +01001309 num_rings = INTEL_INFO(dev_priv)->num_rings;
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001310 ret = intel_ring_begin(req, (num_rings-1) * 6);
Ben Widawsky3e789982014-06-30 09:53:37 -07001311 if (ret)
1312 return ret;
1313
Dave Gordonc3232b12016-03-23 18:19:53 +00001314 for_each_engine_id(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001315 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001316 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1317 continue;
1318
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001319 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001320 (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW);
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001321 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001322 lower_32_bits(gtt_offset) |
1323 MI_FLUSH_DW_USE_GTT);
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001324 intel_ring_emit(ring, upper_32_bits(gtt_offset));
1325 intel_ring_emit(ring, req->fence.seqno);
1326 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001327 MI_SEMAPHORE_SIGNAL |
1328 MI_SEMAPHORE_TARGET(waiter->hw_id));
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001329 intel_ring_emit(ring, 0);
Ben Widawsky3e789982014-06-30 09:53:37 -07001330 }
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001331 intel_ring_advance(ring);
Ben Widawsky3e789982014-06-30 09:53:37 -07001332
1333 return 0;
1334}
1335
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001336static int gen6_signal(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001337{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001338 struct intel_ring *ring = req->ring;
1339 struct drm_i915_private *dev_priv = req->i915;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001340 struct intel_engine_cs *engine;
Dave Gordonc3232b12016-03-23 18:19:53 +00001341 int ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001342
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +01001343 num_rings = INTEL_INFO(dev_priv)->num_rings;
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001344 ret = intel_ring_begin(req, round_up((num_rings-1) * 3, 2));
Ben Widawsky024a43e2014-04-29 14:52:30 -07001345 if (ret)
1346 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001347
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001348 for_each_engine(engine, dev_priv) {
1349 i915_reg_t mbox_reg;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001350
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001351 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
1352 continue;
1353
1354 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001355 if (i915_mmio_reg_valid(mbox_reg)) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001356 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1357 intel_ring_emit_reg(ring, mbox_reg);
1358 intel_ring_emit(ring, req->fence.seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001359 }
1360 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001361
Ben Widawskya1444b72014-06-30 09:53:35 -07001362 /* If num_dwords was rounded, make sure the tail pointer is correct */
1363 if (num_rings % 2 == 0)
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001364 intel_ring_emit(ring, MI_NOOP);
1365 intel_ring_advance(ring);
Ben Widawskya1444b72014-06-30 09:53:35 -07001366
Ben Widawsky024a43e2014-04-29 14:52:30 -07001367 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001368}
1369
Chris Wilsonb0411e72016-08-02 22:50:34 +01001370static void i9xx_submit_request(struct drm_i915_gem_request *request)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001371{
Chris Wilsonb0411e72016-08-02 22:50:34 +01001372 struct drm_i915_private *dev_priv = request->i915;
1373
1374 I915_WRITE_TAIL(request->engine,
1375 intel_ring_offset(request->ring, request->tail));
1376}
1377
1378static int i9xx_emit_request(struct drm_i915_gem_request *req)
1379{
Chris Wilson7e37f882016-08-02 22:50:21 +01001380 struct intel_ring *ring = req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001381 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001382
Chris Wilson9242f972016-08-02 22:50:33 +01001383 ret = intel_ring_begin(req, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001384 if (ret)
1385 return ret;
1386
Chris Wilsonb5321f32016-08-02 22:50:18 +01001387 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1388 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1389 intel_ring_emit(ring, req->fence.seqno);
1390 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001391 intel_ring_advance(ring);
1392
1393 req->tail = ring->tail;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001394
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001395 return 0;
1396}
1397
Chris Wilsonb0411e72016-08-02 22:50:34 +01001398/**
Chris Wilson618e4ca2016-08-02 22:50:35 +01001399 * gen6_sema_emit_request - Update the semaphore mailbox registers
Chris Wilsonb0411e72016-08-02 22:50:34 +01001400 *
1401 * @request - request to write to the ring
1402 *
1403 * Update the mailbox registers in the *other* rings with the current seqno.
1404 * This acts like a signal in the canonical semaphore.
1405 */
Chris Wilson618e4ca2016-08-02 22:50:35 +01001406static int gen6_sema_emit_request(struct drm_i915_gem_request *req)
Chris Wilsonb0411e72016-08-02 22:50:34 +01001407{
Chris Wilson618e4ca2016-08-02 22:50:35 +01001408 int ret;
Chris Wilsonb0411e72016-08-02 22:50:34 +01001409
Chris Wilson618e4ca2016-08-02 22:50:35 +01001410 ret = req->engine->semaphore.signal(req);
1411 if (ret)
1412 return ret;
Chris Wilsonb0411e72016-08-02 22:50:34 +01001413
1414 return i9xx_emit_request(req);
1415}
1416
Chris Wilsonddd66c52016-08-02 22:50:31 +01001417static int gen8_render_emit_request(struct drm_i915_gem_request *req)
Chris Wilsona58c01a2016-04-29 13:18:21 +01001418{
1419 struct intel_engine_cs *engine = req->engine;
Chris Wilson7e37f882016-08-02 22:50:21 +01001420 struct intel_ring *ring = req->ring;
Chris Wilsona58c01a2016-04-29 13:18:21 +01001421 int ret;
1422
Chris Wilson9242f972016-08-02 22:50:33 +01001423 if (engine->semaphore.signal) {
1424 ret = engine->semaphore.signal(req);
1425 if (ret)
1426 return ret;
1427 }
1428
1429 ret = intel_ring_begin(req, 8);
Chris Wilsona58c01a2016-04-29 13:18:21 +01001430 if (ret)
1431 return ret;
1432
Chris Wilsonb5321f32016-08-02 22:50:18 +01001433 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
1434 intel_ring_emit(ring, (PIPE_CONTROL_GLOBAL_GTT_IVB |
1435 PIPE_CONTROL_CS_STALL |
1436 PIPE_CONTROL_QW_WRITE));
1437 intel_ring_emit(ring, intel_hws_seqno_address(engine));
1438 intel_ring_emit(ring, 0);
1439 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsona58c01a2016-04-29 13:18:21 +01001440 /* We're thrashing one dword of HWS. */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001441 intel_ring_emit(ring, 0);
1442 intel_ring_emit(ring, MI_USER_INTERRUPT);
1443 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonddd66c52016-08-02 22:50:31 +01001444 intel_ring_advance(ring);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001445
1446 req->tail = ring->tail;
Chris Wilsona58c01a2016-04-29 13:18:21 +01001447
1448 return 0;
1449}
1450
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001451/**
1452 * intel_ring_sync - sync the waiter to the signaller on seqno
1453 *
1454 * @waiter - ring that is waiting
1455 * @signaller - ring which has, or will signal
1456 * @seqno - seqno which the waiter will block on
1457 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001458
1459static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001460gen8_ring_sync_to(struct drm_i915_gem_request *req,
1461 struct drm_i915_gem_request *signal)
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001462{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001463 struct intel_ring *ring = req->ring;
1464 struct drm_i915_private *dev_priv = req->i915;
1465 u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001466 struct i915_hw_ppgtt *ppgtt;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001467 int ret;
1468
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001469 ret = intel_ring_begin(req, 4);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001470 if (ret)
1471 return ret;
1472
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001473 intel_ring_emit(ring,
1474 MI_SEMAPHORE_WAIT |
1475 MI_SEMAPHORE_GLOBAL_GTT |
1476 MI_SEMAPHORE_SAD_GTE_SDD);
1477 intel_ring_emit(ring, signal->fence.seqno);
1478 intel_ring_emit(ring, lower_32_bits(offset));
1479 intel_ring_emit(ring, upper_32_bits(offset));
1480 intel_ring_advance(ring);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001481
1482 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1483 * pagetables and we must reload them before executing the batch.
1484 * We do this on the i915_switch_context() following the wait and
1485 * before the dispatch.
1486 */
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001487 ppgtt = req->ctx->ppgtt;
1488 if (ppgtt && req->engine->id != RCS)
1489 ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001490 return 0;
1491}
1492
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001493static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001494gen6_ring_sync_to(struct drm_i915_gem_request *req,
1495 struct drm_i915_gem_request *signal)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001496{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001497 struct intel_ring *ring = req->ring;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001498 u32 dw1 = MI_SEMAPHORE_MBOX |
1499 MI_SEMAPHORE_COMPARE |
1500 MI_SEMAPHORE_REGISTER;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001501 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
Ben Widawskyebc348b2014-04-29 14:52:28 -07001502 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001503
Chris Wilsonddf07be2016-08-02 22:50:39 +01001504 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
1505
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001506 ret = intel_ring_begin(req, 4);
Chris Wilsonddf07be2016-08-02 22:50:39 +01001507 if (ret)
1508 return ret;
1509
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001510 intel_ring_emit(ring, dw1 | wait_mbox);
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001511 /* Throughout all of the GEM code, seqno passed implies our current
1512 * seqno is >= the last seqno executed. However for hardware the
1513 * comparison is strictly greater than.
1514 */
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001515 intel_ring_emit(ring, signal->fence.seqno - 1);
1516 intel_ring_emit(ring, 0);
1517 intel_ring_emit(ring, MI_NOOP);
1518 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001519
1520 return 0;
1521}
1522
Chris Wilsonf8973c22016-07-01 17:23:21 +01001523static void
Dave Gordon38a0f2d2016-07-20 18:16:06 +01001524gen5_seqno_barrier(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001525{
Chris Wilsonf8973c22016-07-01 17:23:21 +01001526 /* MI_STORE are internally buffered by the GPU and not flushed
1527 * either by MI_FLUSH or SyncFlush or any other combination of
1528 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001529 *
Chris Wilsonf8973c22016-07-01 17:23:21 +01001530 * "Only the submission of the store operation is guaranteed.
1531 * The write result will be complete (coherent) some time later
1532 * (this is practically a finite period but there is no guaranteed
1533 * latency)."
1534 *
1535 * Empirically, we observe that we need a delay of at least 75us to
1536 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001537 */
Chris Wilsonf8973c22016-07-01 17:23:21 +01001538 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001539}
1540
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001541static void
1542gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001543{
Chris Wilsonc0336662016-05-06 15:40:21 +01001544 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001545
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001546 /* Workaround to force correct ordering between irq and seqno writes on
1547 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +01001548 * ACTHD) before reading the status page.
1549 *
1550 * Note that this effectively stalls the read by the time it takes to
1551 * do a memory transaction, which more or less ensures that the write
1552 * from the GPU has sufficient time to invalidate the CPU cacheline.
1553 * Alternatively we could delay the interrupt from the CS ring to give
1554 * the write time to land, but that would incur a delay after every
1555 * batch i.e. much more frequent than a delay when waiting for the
1556 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001557 *
1558 * Also note that to prevent whole machine hangs on gen7, we have to
1559 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +01001560 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001561 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001562 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001563 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001564}
1565
Chris Wilson31bb59c2016-07-01 17:23:27 +01001566static void
1567gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001568{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001569 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +02001570}
1571
1572static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001573gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001574{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001575 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001576}
1577
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001578static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001579i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001580{
Chris Wilsonc0336662016-05-06 15:40:21 +01001581 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001582
Chris Wilson31bb59c2016-07-01 17:23:27 +01001583 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1584 I915_WRITE(IMR, dev_priv->irq_mask);
1585 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +01001586}
1587
1588static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001589i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001590{
Chris Wilsonc0336662016-05-06 15:40:21 +01001591 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001592
Chris Wilson31bb59c2016-07-01 17:23:27 +01001593 dev_priv->irq_mask |= engine->irq_enable_mask;
1594 I915_WRITE(IMR, dev_priv->irq_mask);
1595}
1596
1597static void
1598i8xx_irq_enable(struct intel_engine_cs *engine)
1599{
1600 struct drm_i915_private *dev_priv = engine->i915;
1601
1602 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1603 I915_WRITE16(IMR, dev_priv->irq_mask);
1604 POSTING_READ16(RING_IMR(engine->mmio_base));
1605}
1606
1607static void
1608i8xx_irq_disable(struct intel_engine_cs *engine)
1609{
1610 struct drm_i915_private *dev_priv = engine->i915;
1611
1612 dev_priv->irq_mask |= engine->irq_enable_mask;
1613 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001614}
1615
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001616static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001617bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001618{
Chris Wilson7e37f882016-08-02 22:50:21 +01001619 struct intel_ring *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001620 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001621
John Harrison5fb9de12015-05-29 17:44:07 +01001622 ret = intel_ring_begin(req, 2);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001623 if (ret)
1624 return ret;
1625
Chris Wilsonb5321f32016-08-02 22:50:18 +01001626 intel_ring_emit(ring, MI_FLUSH);
1627 intel_ring_emit(ring, MI_NOOP);
1628 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001629 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001630}
1631
Chris Wilson0f468322011-01-04 17:35:21 +00001632static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001633gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001634{
Chris Wilsonc0336662016-05-06 15:40:21 +01001635 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001636
Chris Wilson61ff75a2016-07-01 17:23:28 +01001637 I915_WRITE_IMR(engine,
1638 ~(engine->irq_enable_mask |
1639 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001640 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001641}
1642
1643static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001644gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001645{
Chris Wilsonc0336662016-05-06 15:40:21 +01001646 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001647
Chris Wilson61ff75a2016-07-01 17:23:28 +01001648 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001649 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001650}
1651
1652static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001653hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001654{
Chris Wilsonc0336662016-05-06 15:40:21 +01001655 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001656
Chris Wilson31bb59c2016-07-01 17:23:27 +01001657 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
1658 gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
1659}
1660
1661static void
1662hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1663{
1664 struct drm_i915_private *dev_priv = engine->i915;
1665
1666 I915_WRITE_IMR(engine, ~0);
1667 gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
1668}
1669
1670static void
1671gen8_irq_enable(struct intel_engine_cs *engine)
1672{
1673 struct drm_i915_private *dev_priv = engine->i915;
1674
Chris Wilson61ff75a2016-07-01 17:23:28 +01001675 I915_WRITE_IMR(engine,
1676 ~(engine->irq_enable_mask |
1677 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001678 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1679}
1680
1681static void
1682gen8_irq_disable(struct intel_engine_cs *engine)
1683{
1684 struct drm_i915_private *dev_priv = engine->i915;
1685
Chris Wilson61ff75a2016-07-01 17:23:28 +01001686 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001687}
1688
Zou Nan haid1b851f2010-05-21 09:08:57 +08001689static int
Chris Wilson803688b2016-08-02 22:50:27 +01001690i965_emit_bb_start(struct drm_i915_gem_request *req,
1691 u64 offset, u32 length,
1692 unsigned int dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001693{
Chris Wilson7e37f882016-08-02 22:50:21 +01001694 struct intel_ring *ring = req->ring;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001695 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001696
John Harrison5fb9de12015-05-29 17:44:07 +01001697 ret = intel_ring_begin(req, 2);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001698 if (ret)
1699 return ret;
1700
Chris Wilsonb5321f32016-08-02 22:50:18 +01001701 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001702 MI_BATCH_BUFFER_START |
1703 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001704 (dispatch_flags & I915_DISPATCH_SECURE ?
1705 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonb5321f32016-08-02 22:50:18 +01001706 intel_ring_emit(ring, offset);
1707 intel_ring_advance(ring);
Chris Wilson78501ea2010-10-27 12:18:21 +01001708
Zou Nan haid1b851f2010-05-21 09:08:57 +08001709 return 0;
1710}
1711
Daniel Vetterb45305f2012-12-17 16:21:27 +01001712/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1713#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001714#define I830_TLB_ENTRIES (2)
1715#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001716static int
Chris Wilson803688b2016-08-02 22:50:27 +01001717i830_emit_bb_start(struct drm_i915_gem_request *req,
1718 u64 offset, u32 len,
1719 unsigned int dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001720{
Chris Wilson7e37f882016-08-02 22:50:21 +01001721 struct intel_ring *ring = req->ring;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001722 u32 cs_offset = i915_ggtt_offset(req->engine->scratch);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001723 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001724
John Harrison5fb9de12015-05-29 17:44:07 +01001725 ret = intel_ring_begin(req, 6);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001726 if (ret)
1727 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001728
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001729 /* Evict the invalid PTE TLBs */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001730 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1731 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1732 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1733 intel_ring_emit(ring, cs_offset);
1734 intel_ring_emit(ring, 0xdeadbeef);
1735 intel_ring_emit(ring, MI_NOOP);
1736 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001737
John Harrison8e004ef2015-02-13 11:48:10 +00001738 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001739 if (len > I830_BATCH_LIMIT)
1740 return -ENOSPC;
1741
John Harrison5fb9de12015-05-29 17:44:07 +01001742 ret = intel_ring_begin(req, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001743 if (ret)
1744 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001745
1746 /* Blit the batch (which has now all relocs applied) to the
1747 * stable batch scratch bo area (so that the CS never
1748 * stumbles over its tlb invalidation bug) ...
1749 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001750 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1751 intel_ring_emit(ring,
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001752 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilsonb5321f32016-08-02 22:50:18 +01001753 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1754 intel_ring_emit(ring, cs_offset);
1755 intel_ring_emit(ring, 4096);
1756 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001757
Chris Wilsonb5321f32016-08-02 22:50:18 +01001758 intel_ring_emit(ring, MI_FLUSH);
1759 intel_ring_emit(ring, MI_NOOP);
1760 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001761
1762 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001763 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001764 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001765
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001766 ret = intel_ring_begin(req, 2);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001767 if (ret)
1768 return ret;
1769
Chris Wilsonb5321f32016-08-02 22:50:18 +01001770 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1771 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1772 0 : MI_BATCH_NON_SECURE));
1773 intel_ring_advance(ring);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001774
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001775 return 0;
1776}
1777
1778static int
Chris Wilson803688b2016-08-02 22:50:27 +01001779i915_emit_bb_start(struct drm_i915_gem_request *req,
1780 u64 offset, u32 len,
1781 unsigned int dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001782{
Chris Wilson7e37f882016-08-02 22:50:21 +01001783 struct intel_ring *ring = req->ring;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001784 int ret;
1785
John Harrison5fb9de12015-05-29 17:44:07 +01001786 ret = intel_ring_begin(req, 2);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001787 if (ret)
1788 return ret;
1789
Chris Wilsonb5321f32016-08-02 22:50:18 +01001790 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1791 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1792 0 : MI_BATCH_NON_SECURE));
1793 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001794
Eric Anholt62fdfea2010-05-21 13:26:39 -07001795 return 0;
1796}
1797
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001798static void cleanup_phys_status_page(struct intel_engine_cs *engine)
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001799{
Chris Wilsonc0336662016-05-06 15:40:21 +01001800 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001801
1802 if (!dev_priv->status_page_dmah)
1803 return;
1804
Chris Wilson91c8a322016-07-05 10:40:23 +01001805 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001806 engine->status_page.page_addr = NULL;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001807}
1808
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001809static void cleanup_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001810{
Chris Wilson57e88532016-08-15 10:48:57 +01001811 struct i915_vma *vma;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001812
Chris Wilson57e88532016-08-15 10:48:57 +01001813 vma = fetch_and_zero(&engine->status_page.vma);
1814 if (!vma)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001815 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001816
Chris Wilson57e88532016-08-15 10:48:57 +01001817 i915_vma_unpin(vma);
1818 i915_gem_object_unpin_map(vma->obj);
1819 i915_vma_put(vma);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001820}
1821
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001822static int init_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001823{
Chris Wilson57e88532016-08-15 10:48:57 +01001824 struct drm_i915_gem_object *obj;
1825 struct i915_vma *vma;
1826 unsigned int flags;
1827 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001828
Chris Wilson57e88532016-08-15 10:48:57 +01001829 obj = i915_gem_object_create(&engine->i915->drm, 4096);
1830 if (IS_ERR(obj)) {
1831 DRM_ERROR("Failed to allocate status page\n");
1832 return PTR_ERR(obj);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001833 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001834
Chris Wilson57e88532016-08-15 10:48:57 +01001835 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1836 if (ret)
1837 goto err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001838
Chris Wilson57e88532016-08-15 10:48:57 +01001839 vma = i915_vma_create(obj, &engine->i915->ggtt.base, NULL);
1840 if (IS_ERR(vma)) {
1841 ret = PTR_ERR(vma);
1842 goto err;
1843 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001844
Chris Wilson57e88532016-08-15 10:48:57 +01001845 flags = PIN_GLOBAL;
1846 if (!HAS_LLC(engine->i915))
1847 /* On g33, we cannot place HWS above 256MiB, so
1848 * restrict its pinning to the low mappable arena.
1849 * Though this restriction is not documented for
1850 * gen4, gen5, or byt, they also behave similarly
1851 * and hang if the HWS is placed at the top of the
1852 * GTT. To generalise, it appears that all !llc
1853 * platforms have issues with us placing the HWS
1854 * above the mappable region (even though we never
1855 * actualy map it).
1856 */
1857 flags |= PIN_MAPPABLE;
1858 ret = i915_vma_pin(vma, 0, 4096, flags);
1859 if (ret)
1860 goto err;
1861
1862 engine->status_page.vma = vma;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001863 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001864 engine->status_page.page_addr =
1865 i915_gem_object_pin_map(obj, I915_MAP_WB);
1866
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001867 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1868 engine->name, i915_ggtt_offset(vma));
Eric Anholt62fdfea2010-05-21 13:26:39 -07001869 return 0;
Chris Wilson57e88532016-08-15 10:48:57 +01001870
1871err:
1872 i915_gem_object_put(obj);
1873 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001874}
1875
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001876static int init_phys_status_page(struct intel_engine_cs *engine)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001877{
Chris Wilsonc0336662016-05-06 15:40:21 +01001878 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001879
Chris Wilson57e88532016-08-15 10:48:57 +01001880 dev_priv->status_page_dmah =
1881 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
1882 if (!dev_priv->status_page_dmah)
1883 return -ENOMEM;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001884
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001885 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1886 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001887
1888 return 0;
1889}
1890
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001891int intel_ring_pin(struct intel_ring *ring)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001892{
Chris Wilsona687a432016-04-13 17:35:11 +01001893 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
Chris Wilson57e88532016-08-15 10:48:57 +01001894 unsigned int flags = PIN_GLOBAL | PIN_OFFSET_BIAS | 4096;
Chris Wilson9d808412016-08-18 17:16:56 +01001895 enum i915_map_type map;
Chris Wilson57e88532016-08-15 10:48:57 +01001896 struct i915_vma *vma = ring->vma;
Dave Gordon83052162016-04-12 14:46:16 +01001897 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001898 int ret;
1899
Chris Wilson57e88532016-08-15 10:48:57 +01001900 GEM_BUG_ON(ring->vaddr);
1901
Chris Wilson9d808412016-08-18 17:16:56 +01001902 map = HAS_LLC(ring->engine->i915) ? I915_MAP_WB : I915_MAP_WC;
1903
1904 if (vma->obj->stolen)
Chris Wilson57e88532016-08-15 10:48:57 +01001905 flags |= PIN_MAPPABLE;
1906
1907 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilson9d808412016-08-18 17:16:56 +01001908 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
Chris Wilson57e88532016-08-15 10:48:57 +01001909 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1910 else
1911 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1912 if (unlikely(ret))
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001913 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001914 }
1915
Chris Wilson57e88532016-08-15 10:48:57 +01001916 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1917 if (unlikely(ret))
1918 return ret;
1919
Chris Wilson9d808412016-08-18 17:16:56 +01001920 if (i915_vma_is_map_and_fenceable(vma))
Chris Wilson57e88532016-08-15 10:48:57 +01001921 addr = (void __force *)i915_vma_pin_iomap(vma);
1922 else
Chris Wilson9d808412016-08-18 17:16:56 +01001923 addr = i915_gem_object_pin_map(vma->obj, map);
Chris Wilson57e88532016-08-15 10:48:57 +01001924 if (IS_ERR(addr))
1925 goto err;
1926
Chris Wilson32c04f12016-08-02 22:50:22 +01001927 ring->vaddr = addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001928 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01001929
Chris Wilson57e88532016-08-15 10:48:57 +01001930err:
1931 i915_vma_unpin(vma);
1932 return PTR_ERR(addr);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001933}
1934
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001935void intel_ring_unpin(struct intel_ring *ring)
1936{
1937 GEM_BUG_ON(!ring->vma);
1938 GEM_BUG_ON(!ring->vaddr);
1939
Chris Wilson9d808412016-08-18 17:16:56 +01001940 if (i915_vma_is_map_and_fenceable(ring->vma))
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001941 i915_vma_unpin_iomap(ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001942 else
1943 i915_gem_object_unpin_map(ring->vma->obj);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001944 ring->vaddr = NULL;
1945
Chris Wilson57e88532016-08-15 10:48:57 +01001946 i915_vma_unpin(ring->vma);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001947}
1948
Chris Wilson57e88532016-08-15 10:48:57 +01001949static struct i915_vma *
1950intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
Oscar Mateo2919d292014-07-03 16:28:02 +01001951{
Chris Wilsone3efda42014-04-09 09:19:41 +01001952 struct drm_i915_gem_object *obj;
Chris Wilson57e88532016-08-15 10:48:57 +01001953 struct i915_vma *vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001954
Chris Wilsonc58b7352016-08-18 17:16:57 +01001955 obj = i915_gem_object_create_stolen(&dev_priv->drm, size);
1956 if (!obj)
Chris Wilson57e88532016-08-15 10:48:57 +01001957 obj = i915_gem_object_create(&dev_priv->drm, size);
1958 if (IS_ERR(obj))
1959 return ERR_CAST(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001960
Akash Goel24f3a8c2014-06-17 10:59:42 +05301961 /* mark ring buffers as read-only from GPU side by default */
1962 obj->gt_ro = 1;
1963
Chris Wilson57e88532016-08-15 10:48:57 +01001964 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
1965 if (IS_ERR(vma))
1966 goto err;
Chris Wilsone3efda42014-04-09 09:19:41 +01001967
Chris Wilson57e88532016-08-15 10:48:57 +01001968 return vma;
1969
1970err:
1971 i915_gem_object_put(obj);
1972 return vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001973}
1974
Chris Wilson7e37f882016-08-02 22:50:21 +01001975struct intel_ring *
1976intel_engine_create_ring(struct intel_engine_cs *engine, int size)
Chris Wilson01101fa2015-09-03 13:01:39 +01001977{
Chris Wilson7e37f882016-08-02 22:50:21 +01001978 struct intel_ring *ring;
Chris Wilson57e88532016-08-15 10:48:57 +01001979 struct i915_vma *vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001980
Chris Wilson8f942012016-08-02 22:50:30 +01001981 GEM_BUG_ON(!is_power_of_2(size));
1982
Chris Wilson01101fa2015-09-03 13:01:39 +01001983 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson57e88532016-08-15 10:48:57 +01001984 if (!ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001985 return ERR_PTR(-ENOMEM);
1986
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001987 ring->engine = engine;
Chris Wilson01101fa2015-09-03 13:01:39 +01001988
Chris Wilson675d9ad2016-08-04 07:52:36 +01001989 INIT_LIST_HEAD(&ring->request_list);
1990
Chris Wilson01101fa2015-09-03 13:01:39 +01001991 ring->size = size;
1992 /* Workaround an erratum on the i830 which causes a hang if
1993 * the TAIL pointer points to within the last 2 cachelines
1994 * of the buffer.
1995 */
1996 ring->effective_size = size;
Chris Wilsonc0336662016-05-06 15:40:21 +01001997 if (IS_I830(engine->i915) || IS_845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01001998 ring->effective_size -= 2 * CACHELINE_BYTES;
1999
2000 ring->last_retired_head = -1;
2001 intel_ring_update_space(ring);
2002
Chris Wilson57e88532016-08-15 10:48:57 +01002003 vma = intel_ring_create_vma(engine->i915, size);
2004 if (IS_ERR(vma)) {
Chris Wilson01101fa2015-09-03 13:01:39 +01002005 kfree(ring);
Chris Wilson57e88532016-08-15 10:48:57 +01002006 return ERR_CAST(vma);
Chris Wilson01101fa2015-09-03 13:01:39 +01002007 }
Chris Wilson57e88532016-08-15 10:48:57 +01002008 ring->vma = vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01002009
Chris Wilson57e88532016-08-15 10:48:57 +01002010 list_add(&ring->link, &engine->buffers);
Chris Wilson01101fa2015-09-03 13:01:39 +01002011 return ring;
2012}
2013
2014void
Chris Wilson7e37f882016-08-02 22:50:21 +01002015intel_ring_free(struct intel_ring *ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01002016{
Chris Wilson57e88532016-08-15 10:48:57 +01002017 i915_vma_put(ring->vma);
Chris Wilson608c1a52015-09-03 13:01:40 +01002018 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002019 kfree(ring);
2020}
2021
Chris Wilson0cb26a82016-06-24 14:55:53 +01002022static int intel_ring_context_pin(struct i915_gem_context *ctx,
2023 struct intel_engine_cs *engine)
2024{
2025 struct intel_context *ce = &ctx->engine[engine->id];
2026 int ret;
2027
Chris Wilson91c8a322016-07-05 10:40:23 +01002028 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002029
2030 if (ce->pin_count++)
2031 return 0;
2032
2033 if (ce->state) {
Chris Wilson7abc98f2016-08-15 10:48:55 +01002034 ret = i915_gem_object_set_to_gtt_domain(ce->state->obj, false);
2035 if (ret)
2036 goto error;
2037
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002038 ret = i915_vma_pin(ce->state, 0, ctx->ggtt_alignment,
2039 PIN_GLOBAL | PIN_HIGH);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002040 if (ret)
2041 goto error;
2042 }
2043
Chris Wilsonc7c3c072016-06-24 14:55:54 +01002044 /* The kernel context is only used as a placeholder for flushing the
2045 * active context. It is never used for submitting user rendering and
2046 * as such never requires the golden render context, and so we can skip
2047 * emitting it when we switch to the kernel context. This is required
2048 * as during eviction we cannot allocate and pin the renderstate in
2049 * order to initialise the context.
2050 */
2051 if (ctx == ctx->i915->kernel_context)
2052 ce->initialised = true;
2053
Chris Wilson9a6feaf2016-07-20 13:31:50 +01002054 i915_gem_context_get(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002055 return 0;
2056
2057error:
2058 ce->pin_count = 0;
2059 return ret;
2060}
2061
2062static void intel_ring_context_unpin(struct i915_gem_context *ctx,
2063 struct intel_engine_cs *engine)
2064{
2065 struct intel_context *ce = &ctx->engine[engine->id];
2066
Chris Wilson91c8a322016-07-05 10:40:23 +01002067 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002068
2069 if (--ce->pin_count)
2070 return;
2071
2072 if (ce->state)
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002073 i915_vma_unpin(ce->state);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002074
Chris Wilson9a6feaf2016-07-20 13:31:50 +01002075 i915_gem_context_put(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002076}
2077
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002078static int intel_init_ring_buffer(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002079{
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002080 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson32c04f12016-08-02 22:50:22 +01002081 struct intel_ring *ring;
Chris Wilsondd785e32010-08-07 11:01:34 +01002082 int ret;
2083
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002084 WARN_ON(engine->buffer);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002085
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002086 intel_engine_setup_common(engine);
2087
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002088 memset(engine->semaphore.sync_seqno, 0,
2089 sizeof(engine->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002090
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002091 ret = intel_engine_init_common(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +01002092 if (ret)
2093 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002094
Chris Wilson0cb26a82016-06-24 14:55:53 +01002095 /* We may need to do things with the shrinker which
2096 * require us to immediately switch back to the default
2097 * context. This can cause a problem as pinning the
2098 * default context also requires GTT space which may not
2099 * be available. To avoid this we always pin the default
2100 * context.
2101 */
2102 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2103 if (ret)
2104 goto error;
2105
Chris Wilson32c04f12016-08-02 22:50:22 +01002106 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
2107 if (IS_ERR(ring)) {
2108 ret = PTR_ERR(ring);
Dave Gordonb0366a52015-12-08 15:02:36 +00002109 goto error;
2110 }
Chris Wilson01101fa2015-09-03 13:01:39 +01002111
Chris Wilsonc0336662016-05-06 15:40:21 +01002112 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002113 ret = init_status_page(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002114 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002115 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002116 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002117 WARN_ON(engine->id != RCS);
2118 ret = init_phys_status_page(engine);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002119 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002120 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002121 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002122
Chris Wilsonaad29fb2016-08-02 22:50:23 +01002123 ret = intel_ring_pin(ring);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002124 if (ret) {
Chris Wilson57e88532016-08-15 10:48:57 +01002125 intel_ring_free(ring);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002126 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002127 }
Chris Wilson57e88532016-08-15 10:48:57 +01002128 engine->buffer = ring;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002129
Oscar Mateo8ee14972014-05-22 14:13:34 +01002130 return 0;
2131
2132error:
Chris Wilson7e37f882016-08-02 22:50:21 +01002133 intel_engine_cleanup(engine);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002134 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002135}
2136
Chris Wilson7e37f882016-08-02 22:50:21 +01002137void intel_engine_cleanup(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002138{
John Harrison6402c332014-10-31 12:00:26 +00002139 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01002140
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002141 if (!intel_engine_initialized(engine))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002142 return;
2143
Chris Wilsonc0336662016-05-06 15:40:21 +01002144 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00002145
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002146 if (engine->buffer) {
Chris Wilson21a2c582016-08-15 10:49:11 +01002147 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
2148 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002149
Chris Wilsonaad29fb2016-08-02 22:50:23 +01002150 intel_ring_unpin(engine->buffer);
Chris Wilson7e37f882016-08-02 22:50:21 +01002151 intel_ring_free(engine->buffer);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002152 engine->buffer = NULL;
Dave Gordonb0366a52015-12-08 15:02:36 +00002153 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002154
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002155 if (engine->cleanup)
2156 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08002157
Chris Wilsonc0336662016-05-06 15:40:21 +01002158 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002159 cleanup_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002160 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002161 WARN_ON(engine->id != RCS);
2162 cleanup_phys_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002163 }
Brad Volkin44e895a2014-05-10 14:10:43 -07002164
Chris Wilson96a945a2016-08-03 13:19:16 +01002165 intel_engine_cleanup_common(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002166
2167 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2168
Chris Wilsonc0336662016-05-06 15:40:21 +01002169 engine->i915 = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002170}
2171
John Harrison6689cb22015-03-19 12:30:08 +00002172int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002173{
Chris Wilson63103462016-04-28 09:56:49 +01002174 int ret;
2175
2176 /* Flush enough space to reduce the likelihood of waiting after
2177 * we start building the request - in which case we will just
2178 * have to repeat work.
2179 */
Chris Wilsona0442462016-04-29 09:07:05 +01002180 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002181
Chris Wilson1dae2df2016-08-02 22:50:19 +01002182 request->ring = request->engine->buffer;
Chris Wilson63103462016-04-28 09:56:49 +01002183
2184 ret = intel_ring_begin(request, 0);
2185 if (ret)
2186 return ret;
2187
Chris Wilsona0442462016-04-29 09:07:05 +01002188 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002189 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002190}
2191
Chris Wilson987046a2016-04-28 09:56:46 +01002192static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002193{
Chris Wilson7e37f882016-08-02 22:50:21 +01002194 struct intel_ring *ring = req->ring;
Chris Wilson987046a2016-04-28 09:56:46 +01002195 struct drm_i915_gem_request *target;
Chris Wilson7da844c2016-08-04 07:52:38 +01002196 int ret;
Chris Wilson987046a2016-04-28 09:56:46 +01002197
Chris Wilson1dae2df2016-08-02 22:50:19 +01002198 intel_ring_update_space(ring);
2199 if (ring->space >= bytes)
Chris Wilson987046a2016-04-28 09:56:46 +01002200 return 0;
2201
2202 /*
2203 * Space is reserved in the ringbuffer for finalising the request,
2204 * as that cannot be allowed to fail. During request finalisation,
2205 * reserved_space is set to 0 to stop the overallocation and the
2206 * assumption is that then we never need to wait (which has the
2207 * risk of failing with EINTR).
2208 *
2209 * See also i915_gem_request_alloc() and i915_add_request().
2210 */
Chris Wilson0251a962016-04-28 09:56:47 +01002211 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01002212
Chris Wilson675d9ad2016-08-04 07:52:36 +01002213 list_for_each_entry(target, &ring->request_list, ring_link) {
Chris Wilson987046a2016-04-28 09:56:46 +01002214 unsigned space;
2215
Chris Wilson987046a2016-04-28 09:56:46 +01002216 /* Would completion of this request free enough space? */
Chris Wilson1dae2df2016-08-02 22:50:19 +01002217 space = __intel_ring_space(target->postfix, ring->tail,
2218 ring->size);
Chris Wilson987046a2016-04-28 09:56:46 +01002219 if (space >= bytes)
2220 break;
2221 }
2222
Chris Wilson675d9ad2016-08-04 07:52:36 +01002223 if (WARN_ON(&target->ring_link == &ring->request_list))
Chris Wilson987046a2016-04-28 09:56:46 +01002224 return -ENOSPC;
2225
Chris Wilson776f3232016-08-04 07:52:40 +01002226 ret = i915_wait_request(target, true, NULL, NO_WAITBOOST);
Chris Wilson7da844c2016-08-04 07:52:38 +01002227 if (ret)
2228 return ret;
2229
2230 if (i915_reset_in_progress(&target->i915->gpu_error))
2231 return -EAGAIN;
2232
2233 i915_gem_request_retire_upto(target);
2234
2235 intel_ring_update_space(ring);
2236 GEM_BUG_ON(ring->space < bytes);
2237 return 0;
Chris Wilson987046a2016-04-28 09:56:46 +01002238}
2239
2240int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
2241{
Chris Wilson7e37f882016-08-02 22:50:21 +01002242 struct intel_ring *ring = req->ring;
Chris Wilson1dae2df2016-08-02 22:50:19 +01002243 int remain_actual = ring->size - ring->tail;
2244 int remain_usable = ring->effective_size - ring->tail;
Chris Wilson987046a2016-04-28 09:56:46 +01002245 int bytes = num_dwords * sizeof(u32);
2246 int total_bytes, wait_bytes;
John Harrison79bbcc22015-06-30 12:40:55 +01002247 bool need_wrap = false;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002248
Chris Wilson0251a962016-04-28 09:56:47 +01002249 total_bytes = bytes + req->reserved_space;
John Harrison29b1b412015-06-18 13:10:09 +01002250
John Harrison79bbcc22015-06-30 12:40:55 +01002251 if (unlikely(bytes > remain_usable)) {
2252 /*
2253 * Not enough space for the basic request. So need to flush
2254 * out the remainder and then wait for base + reserved.
2255 */
2256 wait_bytes = remain_actual + total_bytes;
2257 need_wrap = true;
Chris Wilson987046a2016-04-28 09:56:46 +01002258 } else if (unlikely(total_bytes > remain_usable)) {
2259 /*
2260 * The base request will fit but the reserved space
2261 * falls off the end. So we don't need an immediate wrap
2262 * and only need to effectively wait for the reserved
2263 * size space from the start of ringbuffer.
2264 */
Chris Wilson0251a962016-04-28 09:56:47 +01002265 wait_bytes = remain_actual + req->reserved_space;
John Harrison79bbcc22015-06-30 12:40:55 +01002266 } else {
Chris Wilson987046a2016-04-28 09:56:46 +01002267 /* No wrapping required, just waiting. */
2268 wait_bytes = total_bytes;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002269 }
2270
Chris Wilson1dae2df2016-08-02 22:50:19 +01002271 if (wait_bytes > ring->space) {
Chris Wilson987046a2016-04-28 09:56:46 +01002272 int ret = wait_for_space(req, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002273 if (unlikely(ret))
2274 return ret;
2275 }
2276
Chris Wilson987046a2016-04-28 09:56:46 +01002277 if (unlikely(need_wrap)) {
Chris Wilson1dae2df2016-08-02 22:50:19 +01002278 GEM_BUG_ON(remain_actual > ring->space);
2279 GEM_BUG_ON(ring->tail + remain_actual > ring->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002280
Chris Wilson987046a2016-04-28 09:56:46 +01002281 /* Fill the tail with MI_NOOP */
Chris Wilson1dae2df2016-08-02 22:50:19 +01002282 memset(ring->vaddr + ring->tail, 0, remain_actual);
2283 ring->tail = 0;
2284 ring->space -= remain_actual;
Chris Wilson987046a2016-04-28 09:56:46 +01002285 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002286
Chris Wilson1dae2df2016-08-02 22:50:19 +01002287 ring->space -= bytes;
2288 GEM_BUG_ON(ring->space < 0);
Chris Wilson304d6952014-01-02 14:32:35 +00002289 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002290}
2291
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002292/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01002293int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002294{
Chris Wilson7e37f882016-08-02 22:50:21 +01002295 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002296 int num_dwords =
2297 (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002298 int ret;
2299
2300 if (num_dwords == 0)
2301 return 0;
2302
Chris Wilson18393f62014-04-09 09:19:40 +01002303 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
John Harrison5fb9de12015-05-29 17:44:07 +01002304 ret = intel_ring_begin(req, num_dwords);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002305 if (ret)
2306 return ret;
2307
2308 while (num_dwords--)
Chris Wilsonb5321f32016-08-02 22:50:18 +01002309 intel_ring_emit(ring, MI_NOOP);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002310
Chris Wilsonb5321f32016-08-02 22:50:18 +01002311 intel_ring_advance(ring);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002312
2313 return 0;
2314}
2315
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01002316static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002317{
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01002318 struct drm_i915_private *dev_priv = request->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002319
Chris Wilson76f84212016-06-30 15:33:45 +01002320 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2321
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002322 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002323
Chris Wilson12f55812012-07-05 17:14:01 +01002324 /* Disable notification that the ring is IDLE. The GT
2325 * will then assume that it is busy and bring it out of rc6.
2326 */
Chris Wilson76f84212016-06-30 15:33:45 +01002327 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2328 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01002329
2330 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01002331 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01002332
2333 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson76f84212016-06-30 15:33:45 +01002334 if (intel_wait_for_register_fw(dev_priv,
2335 GEN6_BSD_SLEEP_PSMI_CONTROL,
2336 GEN6_BSD_SLEEP_INDICATOR,
2337 0,
2338 50))
Chris Wilson12f55812012-07-05 17:14:01 +01002339 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002340
Chris Wilson12f55812012-07-05 17:14:01 +01002341 /* Now that the ring is fully powered up, update the tail */
Chris Wilsonb0411e72016-08-02 22:50:34 +01002342 i9xx_submit_request(request);
Chris Wilson12f55812012-07-05 17:14:01 +01002343
2344 /* Let the ring send IDLE messages to the GT again,
2345 * and so let it sleep to conserve power when idle.
2346 */
Chris Wilson76f84212016-06-30 15:33:45 +01002347 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2348 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2349
2350 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002351}
2352
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01002353static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002354{
Chris Wilson7e37f882016-08-02 22:50:21 +01002355 struct intel_ring *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002356 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002357 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002358
John Harrison5fb9de12015-05-29 17:44:07 +01002359 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002360 if (ret)
2361 return ret;
2362
Chris Wilson71a77e02011-02-02 12:13:49 +00002363 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002364 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002365 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002366
2367 /* We always require a command barrier so that subsequent
2368 * commands, such as breadcrumb interrupts, are strictly ordered
2369 * wrt the contents of the write cache being flushed to memory
2370 * (and thus being coherent from the CPU).
2371 */
2372 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2373
Jesse Barnes9a289772012-10-26 09:42:42 -07002374 /*
2375 * Bspec vol 1c.5 - video engine command streamer:
2376 * "If ENABLED, all TLBs will be invalidated once the flush
2377 * operation is complete. This bit is only valid when the
2378 * Post-Sync Operation field is a value of 1h or 3h."
2379 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01002380 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002381 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2382
Chris Wilsonb5321f32016-08-02 22:50:18 +01002383 intel_ring_emit(ring, cmd);
2384 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002385 if (INTEL_GEN(req->i915) >= 8) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002386 intel_ring_emit(ring, 0); /* upper addr */
2387 intel_ring_emit(ring, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002388 } else {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002389 intel_ring_emit(ring, 0);
2390 intel_ring_emit(ring, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002391 }
Chris Wilsonb5321f32016-08-02 22:50:18 +01002392 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002393 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002394}
2395
2396static int
Chris Wilson803688b2016-08-02 22:50:27 +01002397gen8_emit_bb_start(struct drm_i915_gem_request *req,
2398 u64 offset, u32 len,
2399 unsigned int dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002400{
Chris Wilson7e37f882016-08-02 22:50:21 +01002401 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002402 bool ppgtt = USES_PPGTT(req->i915) &&
John Harrison8e004ef2015-02-13 11:48:10 +00002403 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002404 int ret;
2405
John Harrison5fb9de12015-05-29 17:44:07 +01002406 ret = intel_ring_begin(req, 4);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002407 if (ret)
2408 return ret;
2409
2410 /* FIXME(BDW): Address space and security selectors. */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002411 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002412 (dispatch_flags & I915_DISPATCH_RS ?
2413 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsonb5321f32016-08-02 22:50:18 +01002414 intel_ring_emit(ring, lower_32_bits(offset));
2415 intel_ring_emit(ring, upper_32_bits(offset));
2416 intel_ring_emit(ring, MI_NOOP);
2417 intel_ring_advance(ring);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002418
2419 return 0;
2420}
2421
2422static int
Chris Wilson803688b2016-08-02 22:50:27 +01002423hsw_emit_bb_start(struct drm_i915_gem_request *req,
2424 u64 offset, u32 len,
2425 unsigned int dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002426{
Chris Wilson7e37f882016-08-02 22:50:21 +01002427 struct intel_ring *ring = req->ring;
Akshay Joshi0206e352011-08-16 15:34:10 -04002428 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002429
John Harrison5fb9de12015-05-29 17:44:07 +01002430 ret = intel_ring_begin(req, 2);
Akshay Joshi0206e352011-08-16 15:34:10 -04002431 if (ret)
2432 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002433
Chris Wilsonb5321f32016-08-02 22:50:18 +01002434 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002435 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002436 (dispatch_flags & I915_DISPATCH_SECURE ?
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002437 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2438 (dispatch_flags & I915_DISPATCH_RS ?
2439 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002440 /* bit0-7 is the length on GEN6+ */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002441 intel_ring_emit(ring, offset);
2442 intel_ring_advance(ring);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002443
2444 return 0;
2445}
2446
2447static int
Chris Wilson803688b2016-08-02 22:50:27 +01002448gen6_emit_bb_start(struct drm_i915_gem_request *req,
2449 u64 offset, u32 len,
2450 unsigned int dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002451{
Chris Wilson7e37f882016-08-02 22:50:21 +01002452 struct intel_ring *ring = req->ring;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002453 int ret;
2454
John Harrison5fb9de12015-05-29 17:44:07 +01002455 ret = intel_ring_begin(req, 2);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002456 if (ret)
2457 return ret;
2458
Chris Wilsonb5321f32016-08-02 22:50:18 +01002459 intel_ring_emit(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002460 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002461 (dispatch_flags & I915_DISPATCH_SECURE ?
2462 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002463 /* bit0-7 is the length on GEN6+ */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002464 intel_ring_emit(ring, offset);
2465 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002466
Akshay Joshi0206e352011-08-16 15:34:10 -04002467 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002468}
2469
Chris Wilson549f7362010-10-19 11:19:32 +01002470/* Blitter support (SandyBridge+) */
2471
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01002472static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan hai8d192152010-11-02 16:31:01 +08002473{
Chris Wilson7e37f882016-08-02 22:50:21 +01002474 struct intel_ring *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002475 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002476 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002477
John Harrison5fb9de12015-05-29 17:44:07 +01002478 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002479 if (ret)
2480 return ret;
2481
Chris Wilson71a77e02011-02-02 12:13:49 +00002482 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002483 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002484 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002485
2486 /* We always require a command barrier so that subsequent
2487 * commands, such as breadcrumb interrupts, are strictly ordered
2488 * wrt the contents of the write cache being flushed to memory
2489 * (and thus being coherent from the CPU).
2490 */
2491 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2492
Jesse Barnes9a289772012-10-26 09:42:42 -07002493 /*
2494 * Bspec vol 1c.3 - blitter engine command streamer:
2495 * "If ENABLED, all TLBs will be invalidated once the flush
2496 * operation is complete. This bit is only valid when the
2497 * Post-Sync Operation field is a value of 1h or 3h."
2498 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01002499 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002500 cmd |= MI_INVALIDATE_TLB;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002501 intel_ring_emit(ring, cmd);
2502 intel_ring_emit(ring,
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002503 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002504 if (INTEL_GEN(req->i915) >= 8) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002505 intel_ring_emit(ring, 0); /* upper addr */
2506 intel_ring_emit(ring, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002507 } else {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002508 intel_ring_emit(ring, 0);
2509 intel_ring_emit(ring, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002510 }
Chris Wilsonb5321f32016-08-02 22:50:18 +01002511 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002512
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002513 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002514}
2515
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002516static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2517 struct intel_engine_cs *engine)
2518{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002519 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002520 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002521
Chris Wilson39df9192016-07-20 13:31:57 +01002522 if (!i915.semaphores)
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002523 return;
2524
Chris Wilson51d545d2016-08-15 10:49:02 +01002525 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
2526 struct i915_vma *vma;
2527
Chris Wilson91c8a322016-07-05 10:40:23 +01002528 obj = i915_gem_object_create(&dev_priv->drm, 4096);
Chris Wilson51d545d2016-08-15 10:49:02 +01002529 if (IS_ERR(obj))
2530 goto err;
2531
2532 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
2533 if (IS_ERR(vma))
2534 goto err_obj;
2535
2536 ret = i915_gem_object_set_to_gtt_domain(obj, false);
2537 if (ret)
2538 goto err_obj;
2539
2540 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
2541 if (ret)
2542 goto err_obj;
2543
2544 dev_priv->semaphore = vma;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002545 }
2546
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002547 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002548 u32 offset = i915_ggtt_offset(dev_priv->semaphore);
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002549
Chris Wilsonad7bdb22016-08-02 22:50:40 +01002550 engine->semaphore.sync_to = gen8_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002551 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002552
2553 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002554 u32 ring_offset;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002555
2556 if (i != engine->id)
2557 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2558 else
2559 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2560
2561 engine->semaphore.signal_ggtt[i] = ring_offset;
2562 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002563 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01002564 engine->semaphore.sync_to = gen6_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002565 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002566
2567 /*
2568 * The current semaphore is only applied on pre-gen8
2569 * platform. And there is no VCS2 ring on the pre-gen8
2570 * platform. So the semaphore between RCS and VCS2 is
2571 * initialized as INVALID. Gen8 will initialize the
2572 * sema between VCS2 and RCS later.
2573 */
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002574 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002575 static const struct {
2576 u32 wait_mbox;
2577 i915_reg_t mbox_reg;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002578 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
2579 [RCS_HW] = {
2580 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2581 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2582 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002583 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002584 [VCS_HW] = {
2585 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2586 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2587 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002588 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002589 [BCS_HW] = {
2590 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2591 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2592 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002593 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002594 [VECS_HW] = {
2595 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2596 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2597 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002598 },
2599 };
2600 u32 wait_mbox;
2601 i915_reg_t mbox_reg;
2602
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002603 if (i == engine->hw_id) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002604 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2605 mbox_reg = GEN6_NOSYNC;
2606 } else {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002607 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
2608 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002609 }
2610
2611 engine->semaphore.mbox.wait[i] = wait_mbox;
2612 engine->semaphore.mbox.signal[i] = mbox_reg;
2613 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002614 }
Chris Wilson51d545d2016-08-15 10:49:02 +01002615
2616 return;
2617
2618err_obj:
2619 i915_gem_object_put(obj);
2620err:
2621 DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
2622 i915.semaphores = 0;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002623}
2624
Chris Wilsoned003072016-07-01 09:18:13 +01002625static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2626 struct intel_engine_cs *engine)
2627{
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002628 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2629
Chris Wilsoned003072016-07-01 09:18:13 +01002630 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002631 engine->irq_enable = gen8_irq_enable;
2632 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002633 engine->irq_seqno_barrier = gen6_seqno_barrier;
2634 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002635 engine->irq_enable = gen6_irq_enable;
2636 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002637 engine->irq_seqno_barrier = gen6_seqno_barrier;
2638 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002639 engine->irq_enable = gen5_irq_enable;
2640 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002641 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002642 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002643 engine->irq_enable = i9xx_irq_enable;
2644 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002645 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002646 engine->irq_enable = i8xx_irq_enable;
2647 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002648 }
2649}
2650
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002651static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2652 struct intel_engine_cs *engine)
2653{
Chris Wilson618e4ca2016-08-02 22:50:35 +01002654 intel_ring_init_irq(dev_priv, engine);
2655 intel_ring_init_semaphores(dev_priv, engine);
2656
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002657 engine->init_hw = init_ring_common;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002658
Chris Wilsonddd66c52016-08-02 22:50:31 +01002659 engine->emit_request = i9xx_emit_request;
Chris Wilson618e4ca2016-08-02 22:50:35 +01002660 if (i915.semaphores)
2661 engine->emit_request = gen6_sema_emit_request;
Chris Wilsonddd66c52016-08-02 22:50:31 +01002662 engine->submit_request = i9xx_submit_request;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002663
2664 if (INTEL_GEN(dev_priv) >= 8)
Chris Wilson803688b2016-08-02 22:50:27 +01002665 engine->emit_bb_start = gen8_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002666 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson803688b2016-08-02 22:50:27 +01002667 engine->emit_bb_start = gen6_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002668 else if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson803688b2016-08-02 22:50:27 +01002669 engine->emit_bb_start = i965_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002670 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002671 engine->emit_bb_start = i830_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002672 else
Chris Wilson803688b2016-08-02 22:50:27 +01002673 engine->emit_bb_start = i915_emit_bb_start;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002674}
2675
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002676int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002677{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002678 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07002679 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002680
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002681 intel_ring_default_vfuncs(dev_priv, engine);
2682
Chris Wilson61ff75a2016-07-01 17:23:28 +01002683 if (HAS_L3_DPF(dev_priv))
2684 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002685
Chris Wilsonc0336662016-05-06 15:40:21 +01002686 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002687 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonddd66c52016-08-02 22:50:31 +01002688 engine->emit_request = gen8_render_emit_request;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002689 engine->emit_flush = gen8_render_ring_flush;
Chris Wilson39df9192016-07-20 13:31:57 +01002690 if (i915.semaphores)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002691 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilsonc0336662016-05-06 15:40:21 +01002692 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002693 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002694 engine->emit_flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002695 if (IS_GEN6(dev_priv))
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002696 engine->emit_flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002697 } else if (IS_GEN5(dev_priv)) {
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002698 engine->emit_flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002699 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002700 if (INTEL_GEN(dev_priv) < 4)
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002701 engine->emit_flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002702 else
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002703 engine->emit_flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002704 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002705 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002706
Chris Wilsonc0336662016-05-06 15:40:21 +01002707 if (IS_HASWELL(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002708 engine->emit_bb_start = hsw_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002709
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002710 engine->init_hw = init_render_ring;
2711 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002712
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002713 ret = intel_init_ring_buffer(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002714 if (ret)
2715 return ret;
2716
Chris Wilsonf8973c22016-07-01 17:23:21 +01002717 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01002718 ret = intel_engine_create_scratch(engine, 4096);
Chris Wilson7d5ea802016-07-01 17:23:20 +01002719 if (ret)
2720 return ret;
2721 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01002722 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002723 if (ret)
2724 return ret;
2725 }
2726
2727 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002728}
2729
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002730int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002731{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002732 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002733
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002734 intel_ring_default_vfuncs(dev_priv, engine);
2735
Chris Wilsonc0336662016-05-06 15:40:21 +01002736 if (INTEL_GEN(dev_priv) >= 6) {
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002737 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002738 if (IS_GEN6(dev_priv))
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01002739 engine->submit_request = gen6_bsd_submit_request;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002740 engine->emit_flush = gen6_bsd_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002741 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002742 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002743 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002744 engine->mmio_base = BSD_RING_BASE;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002745 engine->emit_flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002746 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002747 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002748 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002749 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002750 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002751
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002752 return intel_init_ring_buffer(engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002753}
Chris Wilson549f7362010-10-19 11:19:32 +01002754
Zhao Yakui845f74a2014-04-17 10:37:37 +08002755/**
Damien Lespiau62659922015-01-29 14:13:40 +00002756 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002757 */
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002758int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002759{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002760 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002761
2762 intel_ring_default_vfuncs(dev_priv, engine);
2763
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002764 engine->emit_flush = gen6_bsd_ring_flush;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002765
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002766 return intel_init_ring_buffer(engine);
Zhao Yakui845f74a2014-04-17 10:37:37 +08002767}
2768
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002769int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
Chris Wilson549f7362010-10-19 11:19:32 +01002770{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002771 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002772
2773 intel_ring_default_vfuncs(dev_priv, engine);
2774
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002775 engine->emit_flush = gen6_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002776 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002777 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002778
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002779 return intel_init_ring_buffer(engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002780}
Chris Wilsona7b97612012-07-20 12:41:08 +01002781
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002782int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002783{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002784 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002785
2786 intel_ring_default_vfuncs(dev_priv, engine);
2787
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002788 engine->emit_flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002789
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002790 if (INTEL_GEN(dev_priv) < 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002791 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01002792 engine->irq_enable = hsw_vebox_irq_enable;
2793 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002794 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002795
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002796 return intel_init_ring_buffer(engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002797}