blob: aaa46d9ffbc13560e4f5ace4e0b84128af899bfb [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 Wilson6f392d52010-08-07 11:01:22 +010088 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000089 int ret;
Chris Wilson6f392d52010-08-07 11:01:22 +010090
Chris Wilson36d527d2011-03-19 22:26:49 +000091 /*
92 * read/write caches:
93 *
94 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
95 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
96 * also flushed at 2d versus 3d pipeline switches.
97 *
98 * read-only caches:
99 *
100 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
101 * MI_READ_FLUSH is set, and is always flushed on 965.
102 *
103 * I915_GEM_DOMAIN_COMMAND may not exist?
104 *
105 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
106 * invalidated when MI_EXE_FLUSH is set.
107 *
108 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
109 * invalidated with every MI_FLUSH.
110 *
111 * TLBs:
112 *
113 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
114 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
115 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
116 * are flushed at any MI_FLUSH.
117 */
118
Chris Wilsonb5321f32016-08-02 22:50:18 +0100119 cmd = MI_FLUSH;
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100120 if (mode & EMIT_INVALIDATE) {
Chris Wilson36d527d2011-03-19 22:26:49 +0000121 cmd |= MI_EXE_FLUSH;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100122 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
123 cmd |= MI_INVALIDATE_ISP;
124 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000125
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
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000408static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200409{
Chris Wilsonc0336662016-05-06 15:40:21 +0100410 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200411 u32 addr;
412
413 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100414 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200415 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
416 I915_WRITE(HWS_PGA, addr);
417}
418
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000419static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000420{
Chris Wilsonc0336662016-05-06 15:40:21 +0100421 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200422 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000423
424 /* The ring status page addresses are no longer next to the rest of
425 * the ring registers as of gen7.
426 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100427 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000428 switch (engine->id) {
Damien Lespiauaf75f262015-02-10 19:32:17 +0000429 case RCS:
430 mmio = RENDER_HWS_PGA_GEN7;
431 break;
432 case BCS:
433 mmio = BLT_HWS_PGA_GEN7;
434 break;
435 /*
436 * VCS2 actually doesn't exist on Gen7. Only shut up
437 * gcc switch check warning
438 */
439 case VCS2:
440 case VCS:
441 mmio = BSD_HWS_PGA_GEN7;
442 break;
443 case VECS:
444 mmio = VEBOX_HWS_PGA_GEN7;
445 break;
446 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100447 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000448 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000449 } else {
450 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000451 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000452 }
453
Chris Wilson57e88532016-08-15 10:48:57 +0100454 I915_WRITE(mmio, engine->status_page.ggtt_offset);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000455 POSTING_READ(mmio);
456
457 /*
458 * Flush the TLB for this page
459 *
460 * FIXME: These two bits have disappeared on gen8, so a question
461 * arises: do we still need this and if so how should we go about
462 * invalidating the TLB?
463 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100464 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000465 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000466
467 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000468 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000469
470 I915_WRITE(reg,
471 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
472 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100473 if (intel_wait_for_register(dev_priv,
474 reg, INSTPM_SYNC_FLUSH, 0,
475 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000476 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000477 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000478 }
479}
480
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000481static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100482{
Chris Wilsonc0336662016-05-06 15:40:21 +0100483 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100484
Chris Wilson21a2c582016-08-15 10:49:11 +0100485 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000486 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100487 if (intel_wait_for_register(dev_priv,
488 RING_MI_MODE(engine->mmio_base),
489 MODE_IDLE,
490 MODE_IDLE,
491 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000492 DRM_ERROR("%s : timed out trying to stop ring\n",
493 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100494 /* Sometimes we observe that the idle flag is not
495 * set even though the ring is empty. So double
496 * check before giving up.
497 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000498 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100499 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100500 }
501 }
502
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000503 I915_WRITE_CTL(engine, 0);
504 I915_WRITE_HEAD(engine, 0);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100505 I915_WRITE_TAIL(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100506
Chris Wilson21a2c582016-08-15 10:49:11 +0100507 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000508 (void)I915_READ_CTL(engine);
509 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
Chris Wilson9991ae72014-04-02 16:36:07 +0100510 }
511
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000512 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100513}
514
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000515static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800516{
Chris Wilsonc0336662016-05-06 15:40:21 +0100517 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +0100518 struct intel_ring *ring = engine->buffer;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200519 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800520
Mika Kuoppala59bad942015-01-16 11:34:40 +0200521 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200522
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000523 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100524 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000525 DRM_DEBUG_KMS("%s head not reset to zero "
526 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000527 engine->name,
528 I915_READ_CTL(engine),
529 I915_READ_HEAD(engine),
530 I915_READ_TAIL(engine),
531 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800532
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000533 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000534 DRM_ERROR("failed to set %s head to zero "
535 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000536 engine->name,
537 I915_READ_CTL(engine),
538 I915_READ_HEAD(engine),
539 I915_READ_TAIL(engine),
540 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100541 ret = -EIO;
542 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000543 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700544 }
545
Carlos Santa31776592016-08-17 12:30:56 -0700546 if (HWS_NEEDS_PHYSICAL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000547 ring_setup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -0700548 else
549 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100550
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100551 intel_engine_reset_breadcrumbs(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +0100552
Jiri Kosinaece4a172014-08-07 16:29:53 +0200553 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000554 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200555
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200556 /* Initialize the ring. This must happen _after_ we've cleared the ring
557 * registers with the above sequence (the readback of the HEAD registers
558 * also enforces ordering), otherwise the hw might lose the new ring
559 * register values. */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100560 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
Chris Wilson95468892014-08-07 15:39:54 +0100561
562 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000563 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100564 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000565 engine->name, I915_READ_HEAD(engine));
Chris Wilson821ed7d2016-09-09 14:11:53 +0100566
567 intel_ring_update_space(ring);
568 I915_WRITE_HEAD(engine, ring->head);
569 I915_WRITE_TAIL(engine, ring->tail);
570 (void)I915_READ_TAIL(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100571
Chris Wilson62ae14b2016-10-04 21:11:25 +0100572 I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800573
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800574 /* If the head is still not zero, the ring is dead */
Chris Wilson821ed7d2016-09-09 14:11:53 +0100575 if (intel_wait_for_register_fw(dev_priv, RING_CTL(engine->mmio_base),
576 RING_VALID, RING_VALID,
577 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000578 DRM_ERROR("%s initialization failed "
Chris Wilson821ed7d2016-09-09 14:11:53 +0100579 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000580 engine->name,
581 I915_READ_CTL(engine),
582 I915_READ_CTL(engine) & RING_VALID,
Chris Wilson821ed7d2016-09-09 14:11:53 +0100583 I915_READ_HEAD(engine), ring->head,
584 I915_READ_TAIL(engine), ring->tail,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000585 I915_READ_START(engine),
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100586 i915_ggtt_offset(ring->vma));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200587 ret = -EIO;
588 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800589 }
590
Tomas Elffc0768c2016-03-21 16:26:59 +0000591 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100592
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200593out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200594 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200595
596 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700597}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800598
Chris Wilson821ed7d2016-09-09 14:11:53 +0100599static void reset_ring_common(struct intel_engine_cs *engine,
600 struct drm_i915_gem_request *request)
601{
602 struct intel_ring *ring = request->ring;
603
604 ring->head = request->postfix;
605 ring->last_retired_head = -1;
606}
607
John Harrisone2be4fa2015-05-29 17:43:54 +0100608static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100609{
Chris Wilson7e37f882016-08-02 22:50:21 +0100610 struct intel_ring *ring = req->ring;
Chris Wilsonc0336662016-05-06 15:40:21 +0100611 struct i915_workarounds *w = &req->i915->workarounds;
612 int ret, i;
Arun Siluvery888b5992014-08-26 14:44:51 +0100613
Francisco Jerez02235802015-10-07 14:44:01 +0300614 if (w->count == 0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300615 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100616
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100617 ret = req->engine->emit_flush(req, EMIT_BARRIER);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100618 if (ret)
619 return ret;
620
John Harrison5fb9de12015-05-29 17:44:07 +0100621 ret = intel_ring_begin(req, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300622 if (ret)
623 return ret;
624
Chris Wilsonb5321f32016-08-02 22:50:18 +0100625 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300626 for (i = 0; i < w->count; i++) {
Chris Wilsonb5321f32016-08-02 22:50:18 +0100627 intel_ring_emit_reg(ring, w->reg[i].addr);
628 intel_ring_emit(ring, w->reg[i].value);
Mika Kuoppala72253422014-10-07 17:21:26 +0300629 }
Chris Wilsonb5321f32016-08-02 22:50:18 +0100630 intel_ring_emit(ring, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300631
Chris Wilsonb5321f32016-08-02 22:50:18 +0100632 intel_ring_advance(ring);
Mika Kuoppala72253422014-10-07 17:21:26 +0300633
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100634 ret = req->engine->emit_flush(req, EMIT_BARRIER);
Mika Kuoppala72253422014-10-07 17:21:26 +0300635 if (ret)
636 return ret;
637
638 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
639
640 return 0;
641}
642
John Harrison87531812015-05-29 17:43:44 +0100643static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100644{
645 int ret;
646
John Harrisone2be4fa2015-05-29 17:43:54 +0100647 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100648 if (ret != 0)
649 return ret;
650
Chris Wilson4e50f082016-10-28 13:58:31 +0100651 ret = i915_gem_render_state_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100652 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000653 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100654
Chris Wilsone26e1b92016-01-29 16:49:05 +0000655 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100656}
657
Mika Kuoppala72253422014-10-07 17:21:26 +0300658static int wa_add(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200659 i915_reg_t addr,
660 const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300661{
662 const u32 idx = dev_priv->workarounds.count;
663
664 if (WARN_ON(idx >= I915_MAX_WA_REGS))
665 return -ENOSPC;
666
667 dev_priv->workarounds.reg[idx].addr = addr;
668 dev_priv->workarounds.reg[idx].value = val;
669 dev_priv->workarounds.reg[idx].mask = mask;
670
671 dev_priv->workarounds.count++;
672
673 return 0;
674}
675
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100676#define WA_REG(addr, mask, val) do { \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000677 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300678 if (r) \
679 return r; \
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100680 } while (0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300681
682#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000683 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300684
685#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000686 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300687
Damien Lespiau98533252014-12-08 17:33:51 +0000688#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000689 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300690
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000691#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
692#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300693
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000694#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300695
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000696static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
697 i915_reg_t reg)
Arun Siluvery33136b02016-01-21 21:43:47 +0000698{
Chris Wilsonc0336662016-05-06 15:40:21 +0100699 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery33136b02016-01-21 21:43:47 +0000700 struct i915_workarounds *wa = &dev_priv->workarounds;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000701 const uint32_t index = wa->hw_whitelist_count[engine->id];
Arun Siluvery33136b02016-01-21 21:43:47 +0000702
703 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
704 return -EINVAL;
705
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000706 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
Arun Siluvery33136b02016-01-21 21:43:47 +0000707 i915_mmio_reg_offset(reg));
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000708 wa->hw_whitelist_count[engine->id]++;
Arun Siluvery33136b02016-01-21 21:43:47 +0000709
710 return 0;
711}
712
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000713static int gen8_init_workarounds(struct intel_engine_cs *engine)
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100714{
Chris Wilsonc0336662016-05-06 15:40:21 +0100715 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery68c61982015-09-25 17:40:38 +0100716
717 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100718
Arun Siluvery717d84d2015-09-25 17:40:39 +0100719 /* WaDisableAsyncFlipPerfMode:bdw,chv */
720 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
721
Arun Siluveryd0581192015-09-25 17:40:40 +0100722 /* WaDisablePartialInstShootdown:bdw,chv */
723 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
724 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
725
Arun Siluverya340af52015-09-25 17:40:45 +0100726 /* Use Force Non-Coherent whenever executing a 3D context. This is a
727 * workaround for for a possible hang in the unlikely event a TLB
728 * invalidation occurs during a PSD flush.
729 */
730 /* WaForceEnableNonCoherent:bdw,chv */
Arun Siluvery120f5d22015-09-25 17:40:46 +0100731 /* WaHdcDisableFetchWhenMasked:bdw,chv */
Arun Siluverya340af52015-09-25 17:40:45 +0100732 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Arun Siluvery120f5d22015-09-25 17:40:46 +0100733 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Arun Siluverya340af52015-09-25 17:40:45 +0100734 HDC_FORCE_NON_COHERENT);
735
Arun Siluvery6def8fd2015-09-25 17:40:42 +0100736 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
737 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
738 * polygons in the same 8x4 pixel/sample area to be processed without
739 * stalling waiting for the earlier ones to write to Hierarchical Z
740 * buffer."
741 *
742 * This optimization is off by default for BDW and CHV; turn it on.
743 */
744 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
745
Arun Siluvery48404632015-09-25 17:40:43 +0100746 /* Wa4x4STCOptimizationDisable:bdw,chv */
747 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
748
Arun Siluvery7eebcde2015-09-25 17:40:44 +0100749 /*
750 * BSpec recommends 8x4 when MSAA is used,
751 * however in practice 16x4 seems fastest.
752 *
753 * Note that PS/WM thread counts depend on the WIZ hashing
754 * disable bit, which we don't touch here, but it's good
755 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
756 */
757 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
758 GEN6_WIZ_HASHING_MASK,
759 GEN6_WIZ_HASHING_16x4);
760
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100761 return 0;
762}
763
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000764static int bdw_init_workarounds(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +0300765{
Chris Wilsonc0336662016-05-06 15:40:21 +0100766 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100767 int ret;
Mika Kuoppala72253422014-10-07 17:21:26 +0300768
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000769 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100770 if (ret)
771 return ret;
772
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700773 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Arun Siluveryd0581192015-09-25 17:40:40 +0100774 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100775
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700776 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300777 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
778 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100779
Mika Kuoppala72253422014-10-07 17:21:26 +0300780 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
781 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100782
Mika Kuoppala72253422014-10-07 17:21:26 +0300783 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000784 /* WaForceContextSaveRestoreNonCoherent:bdw */
785 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000786 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Chris Wilsonc0336662016-05-06 15:40:21 +0100787 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100788
Arun Siluvery86d7f232014-08-26 14:44:50 +0100789 return 0;
790}
791
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000792static int chv_init_workarounds(struct intel_engine_cs *engine)
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300793{
Chris Wilsonc0336662016-05-06 15:40:21 +0100794 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100795 int ret;
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300796
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000797 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100798 if (ret)
799 return ret;
800
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300801 /* WaDisableThreadStallDopClockGating:chv */
Arun Siluveryd0581192015-09-25 17:40:40 +0100802 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300803
Kenneth Graunked60de812015-01-10 18:02:22 -0800804 /* Improve HiZ throughput on CHV. */
805 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
806
Mika Kuoppala72253422014-10-07 17:21:26 +0300807 return 0;
808}
809
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000810static int gen9_init_workarounds(struct intel_engine_cs *engine)
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000811{
Chris Wilsonc0336662016-05-06 15:40:21 +0100812 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000813 int ret;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000814
Tim Gorea8ab5ed2016-06-13 12:15:01 +0100815 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
816 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
817
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300818 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300819 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
820 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
821
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300822 /* WaDisableKillLogic:bxt,skl,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300823 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
824 ECOCHK_DIS_TLB);
825
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300826 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
827 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000828 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Tim Gore950b2aa2016-03-16 16:13:46 +0000829 FLOW_CONTROL_ENABLE |
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000830 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
831
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300832 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Nick Hoath84241712015-02-05 10:47:20 +0000833 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
834 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
835
Jani Nikulaa117f372016-09-16 16:59:44 +0300836 /* WaDisableDgMirrorFixInHalfSliceChicken5:bxt */
837 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Damien Lespiaua86eb582015-02-11 18:21:44 +0000838 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
839 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000840
Jani Nikulaa117f372016-09-16 16:59:44 +0300841 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:bxt */
842 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Damien Lespiau183c6da2015-02-09 19:33:11 +0000843 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
844 GEN9_RHWO_OPTIMIZATION_DISABLE);
Arun Siluvery9b014352015-07-14 15:01:30 +0100845 /*
846 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
847 * but we do that in per ctx batchbuffer as there is an issue
848 * with this register not getting restored on ctx restore
849 */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000850 }
851
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300852 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
Tim Gorebfd8ad42016-04-19 15:45:52 +0100853 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
Tim Gorebfd8ad42016-04-19 15:45:52 +0100854 GEN9_ENABLE_GPGPU_PREEMPTION);
Nick Hoathcac23df2015-02-05 10:47:22 +0000855
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300856 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
857 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
Arun Siluvery60294682015-09-25 14:33:37 +0100858 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
859 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
Damien Lespiau9370cd92015-02-09 19:33:17 +0000860
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300861 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000862 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
863 GEN9_CCS_TLB_PREFETCH_ENABLE);
864
Jani Nikula0d0b8dc2016-09-16 16:59:45 +0300865 /* WaDisableMaskBasedCammingInRCC:bxt */
866 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200867 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
868 PIXEL_MASK_CAMMING_DISABLE);
869
Mika Kuoppala5b0e3652016-06-07 17:18:57 +0300870 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
871 WA_SET_BIT_MASKED(HDC_CHICKEN0,
872 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
873 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
Imre Deak8ea6f892015-05-19 17:05:42 +0300874
Mika Kuoppalabbaefe72016-06-07 17:18:58 +0300875 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
876 * both tied to WaForceContextSaveRestoreNonCoherent
877 * in some hsds for skl. We keep the tie for all gen9. The
878 * documentation is a bit hazy and so we want to get common behaviour,
879 * even though there is no clear evidence we would need both on kbl/bxt.
880 * This area has been source of system hangs so we play it safe
881 * and mimic the skl regardless of what bspec says.
882 *
883 * Use Force Non-Coherent whenever executing a 3D context. This
884 * is a workaround for a possible hang in the unlikely event
885 * a TLB invalidation occurs during a PSD flush.
886 */
887
888 /* WaForceEnableNonCoherent:skl,bxt,kbl */
889 WA_SET_BIT_MASKED(HDC_CHICKEN0,
890 HDC_FORCE_NON_COHERENT);
891
892 /* WaDisableHDCInvalidation:skl,bxt,kbl */
893 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
894 BDW_DISABLE_HDC_INVALIDATION);
895
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300896 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
897 if (IS_SKYLAKE(dev_priv) ||
898 IS_KABYLAKE(dev_priv) ||
899 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
Arun Siluvery8c761602015-09-08 10:31:48 +0100900 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
901 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery8c761602015-09-08 10:31:48 +0100902
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300903 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
Robert Beckett6b6d5622015-09-08 10:31:52 +0100904 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
905
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300906 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
Arun Siluvery6ecf56a2016-01-21 21:43:54 +0000907 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
908 GEN8_LQSC_FLUSH_COHERENT_LINES));
909
arun.siluvery@linux.intel.com6bb628552016-06-06 09:52:49 +0100910 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
911 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
912 if (ret)
913 return ret;
914
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300915 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000916 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000917 if (ret)
918 return ret;
919
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300920 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000921 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
Arun Siluvery3669ab62016-01-21 21:43:49 +0000922 if (ret)
923 return ret;
924
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000925 return 0;
926}
927
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000928static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +0000929{
Chris Wilsonc0336662016-05-06 15:40:21 +0100930 struct drm_i915_private *dev_priv = engine->i915;
Damien Lespiaub7668792015-02-14 18:30:29 +0000931 u8 vals[3] = { 0, 0, 0 };
932 unsigned int i;
933
934 for (i = 0; i < 3; i++) {
935 u8 ss;
936
937 /*
938 * Only consider slices where one, and only one, subslice has 7
939 * EUs
940 */
Imre Deak43b67992016-08-31 19:13:02 +0300941 if (!is_power_of_2(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]))
Damien Lespiaub7668792015-02-14 18:30:29 +0000942 continue;
943
944 /*
945 * subslice_7eu[i] != 0 (because of the check above) and
946 * ss_max == 4 (maximum number of subslices possible per slice)
947 *
948 * -> 0 <= ss <= 3;
949 */
Imre Deak43b67992016-08-31 19:13:02 +0300950 ss = ffs(INTEL_INFO(dev_priv)->sseu.subslice_7eu[i]) - 1;
Damien Lespiaub7668792015-02-14 18:30:29 +0000951 vals[i] = 3 - ss;
952 }
953
954 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
955 return 0;
956
957 /* Tune IZ hashing. See intel_device_info_runtime_init() */
958 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
959 GEN9_IZ_HASHING_MASK(2) |
960 GEN9_IZ_HASHING_MASK(1) |
961 GEN9_IZ_HASHING_MASK(0),
962 GEN9_IZ_HASHING(2, vals[2]) |
963 GEN9_IZ_HASHING(1, vals[1]) |
964 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +0000965
Mika Kuoppala72253422014-10-07 17:21:26 +0300966 return 0;
967}
968
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000969static int skl_init_workarounds(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +0000970{
Chris Wilsonc0336662016-05-06 15:40:21 +0100971 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +0100972 int ret;
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +0000973
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000974 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +0100975 if (ret)
976 return ret;
Damien Lespiau8d205492015-02-09 19:33:15 +0000977
Arun Siluverya78536e2016-01-21 21:43:53 +0000978 /*
979 * Actual WA is to disable percontext preemption granularity control
980 * until D0 which is the default case so this is equivalent to
981 * !WaDisablePerCtxtPreemptionGranularityControl:skl
982 */
Jani Nikula9fc736e2016-09-16 16:59:46 +0300983 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
984 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
Arun Siluverya78536e2016-01-21 21:43:53 +0000985
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300986 /* WaEnableGapsTsvCreditFix:skl */
Jani Nikulaa117f372016-09-16 16:59:44 +0300987 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
988 GEN9_GAPS_TSV_CREDIT_DISABLE));
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +0000989
Mika Kuoppalaeee8efb2016-06-07 17:18:53 +0300990 /* WaDisableGafsUnitClkGating:skl */
991 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
992
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +0300993 /* WaInPlaceDecompressionHang:skl */
994 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
995 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
996 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
997
Arun Siluvery61074972016-01-21 21:43:52 +0000998 /* WaDisableLSQCROPERFforOCL:skl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000999 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluvery61074972016-01-21 21:43:52 +00001000 if (ret)
1001 return ret;
1002
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001003 return skl_tune_iz_hashing(engine);
Damien Lespiau8d205492015-02-09 19:33:15 +00001004}
1005
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001006static int bxt_init_workarounds(struct intel_engine_cs *engine)
Nick Hoathcae04372015-03-17 11:39:38 +02001007{
Chris Wilsonc0336662016-05-06 15:40:21 +01001008 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001009 int ret;
Nick Hoathdfb601e2015-04-10 13:12:24 +01001010
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001011 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001012 if (ret)
1013 return ret;
Nick Hoathcae04372015-03-17 11:39:38 +02001014
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001015 /* WaStoreMultiplePTEenable:bxt */
1016 /* This is a requirement according to Hardware specification */
Chris Wilsonc0336662016-05-06 15:40:21 +01001017 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001018 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1019
1020 /* WaSetClckGatingDisableMedia:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001021 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001022 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1023 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1024 }
1025
Nick Hoathdfb601e2015-04-10 13:12:24 +01001026 /* WaDisableThreadStallDopClockGating:bxt */
1027 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1028 STALL_DOP_GATING_DISABLE);
1029
arun.siluvery@linux.intel.com780f0ae2016-06-03 11:16:10 +01001030 /* WaDisablePooledEuLoadBalancingFix:bxt */
1031 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1032 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1033 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1034 }
1035
Nick Hoath983b4b92015-04-10 13:12:25 +01001036 /* WaDisableSbeCacheDispatchPortSharing:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001037 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
Nick Hoath983b4b92015-04-10 13:12:25 +01001038 WA_SET_BIT_MASKED(
1039 GEN7_HALF_SLICE_CHICKEN1,
1040 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1041 }
1042
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001043 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1044 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1045 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
Arun Siluverya786d532016-01-21 21:43:51 +00001046 /* WaDisableLSQCROPERFforOCL:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001047 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001048 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001049 if (ret)
1050 return ret;
Arun Siluverya786d532016-01-21 21:43:51 +00001051
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001052 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluverya786d532016-01-21 21:43:51 +00001053 if (ret)
1054 return ret;
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001055 }
1056
Tim Gore050fc462016-04-22 09:46:01 +01001057 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001058 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
Imre Deak36579cb2016-05-03 15:54:20 +03001059 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1060 L3_HIGH_PRIO_CREDITS(2));
Tim Gore050fc462016-04-22 09:46:01 +01001061
Matthew Auld575e3cc2016-08-02 09:36:53 +01001062 /* WaToEnableHwFixForPushConstHWBug:bxt */
1063 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001064 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1065 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1066
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +03001067 /* WaInPlaceDecompressionHang:bxt */
1068 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
1069 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1070 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1071
Nick Hoathcae04372015-03-17 11:39:38 +02001072 return 0;
1073}
1074
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001075static int kbl_init_workarounds(struct intel_engine_cs *engine)
1076{
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001077 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001078 int ret;
1079
1080 ret = gen9_init_workarounds(engine);
1081 if (ret)
1082 return ret;
1083
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001084 /* WaEnableGapsTsvCreditFix:kbl */
1085 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1086 GEN9_GAPS_TSV_CREDIT_DISABLE));
1087
Mika Kuoppalac0b730d2016-06-07 17:19:06 +03001088 /* WaDisableDynamicCreditSharing:kbl */
1089 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1090 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1091 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1092
Mika Kuoppala8401d422016-06-07 17:19:00 +03001093 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1094 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1095 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1096 HDC_FENCE_DEST_SLM_DISABLE);
1097
Mika Kuoppalafe905812016-06-07 17:19:03 +03001098 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1099 * involving this register should also be added to WA batch as required.
1100 */
1101 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1102 /* WaDisableLSQCROPERFforOCL:kbl */
1103 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1104 GEN8_LQSC_RO_PERF_DIS);
1105
Matthew Auld575e3cc2016-08-02 09:36:53 +01001106 /* WaToEnableHwFixForPushConstHWBug:kbl */
1107 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001108 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1109 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1110
Mika Kuoppala4de5d7c2016-06-07 17:19:11 +03001111 /* WaDisableGafsUnitClkGating:kbl */
1112 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1113
Mika Kuoppala954337a2016-06-07 17:19:12 +03001114 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1115 WA_SET_BIT_MASKED(
1116 GEN7_HALF_SLICE_CHICKEN1,
1117 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1118
Mika Kuoppala4ba9c1f2016-07-20 14:26:12 +03001119 /* WaInPlaceDecompressionHang:kbl */
1120 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1121 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1122
Mika Kuoppalafe905812016-06-07 17:19:03 +03001123 /* WaDisableLSQCROPERFforOCL:kbl */
1124 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1125 if (ret)
1126 return ret;
1127
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001128 return 0;
1129}
1130
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001131int init_workarounds_ring(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +03001132{
Chris Wilsonc0336662016-05-06 15:40:21 +01001133 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala72253422014-10-07 17:21:26 +03001134
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001135 WARN_ON(engine->id != RCS);
Mika Kuoppala72253422014-10-07 17:21:26 +03001136
1137 dev_priv->workarounds.count = 0;
Arun Siluvery33136b02016-01-21 21:43:47 +00001138 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
Mika Kuoppala72253422014-10-07 17:21:26 +03001139
Chris Wilsonc0336662016-05-06 15:40:21 +01001140 if (IS_BROADWELL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001141 return bdw_init_workarounds(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +03001142
Chris Wilsonc0336662016-05-06 15:40:21 +01001143 if (IS_CHERRYVIEW(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001144 return chv_init_workarounds(engine);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001145
Chris Wilsonc0336662016-05-06 15:40:21 +01001146 if (IS_SKYLAKE(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001147 return skl_init_workarounds(engine);
Nick Hoathcae04372015-03-17 11:39:38 +02001148
Chris Wilsonc0336662016-05-06 15:40:21 +01001149 if (IS_BROXTON(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001150 return bxt_init_workarounds(engine);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001151
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001152 if (IS_KABYLAKE(dev_priv))
1153 return kbl_init_workarounds(engine);
1154
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001155 return 0;
1156}
1157
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001158static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001159{
Chris Wilsonc0336662016-05-06 15:40:21 +01001160 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001161 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001162 if (ret)
1163 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001164
Akash Goel61a563a2014-03-25 18:01:50 +05301165 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001166 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001167 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001168
1169 /* We need to disable the AsyncFlip performance optimisations in order
1170 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1171 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001172 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001173 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001174 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001175 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001176 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1177
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001178 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301179 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +01001180 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001181 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001182 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001183
Akash Goel01fa0302014-03-24 23:00:04 +05301184 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +01001185 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001186 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301187 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001188 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001189
Chris Wilsonc0336662016-05-06 15:40:21 +01001190 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001191 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1192 * "If this bit is set, STCunit will have LRA as replacement
1193 * policy. [...] This bit must be reset. LRA replacement
1194 * policy is not supported."
1195 */
1196 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001197 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001198 }
1199
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001200 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001201 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001202
Ville Syrjälä035ea402016-07-12 19:24:47 +03001203 if (INTEL_INFO(dev_priv)->gen >= 6)
1204 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -07001205
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001206 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001207}
1208
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001209static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001210{
Chris Wilsonc0336662016-05-06 15:40:21 +01001211 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001212
Chris Wilson19880c42016-08-15 10:49:05 +01001213 i915_vma_unpin_and_release(&dev_priv->semaphore);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001214}
1215
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001216static int gen8_rcs_signal(struct drm_i915_gem_request *req)
Ben Widawsky3e789982014-06-30 09:53:37 -07001217{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001218 struct intel_ring *ring = req->ring;
1219 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001220 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001221 enum intel_engine_id id;
1222 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001223
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +01001224 num_rings = INTEL_INFO(dev_priv)->num_rings;
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001225 ret = intel_ring_begin(req, (num_rings-1) * 8);
Ben Widawsky3e789982014-06-30 09:53:37 -07001226 if (ret)
1227 return ret;
1228
Akash Goel3b3f1652016-10-13 22:44:48 +05301229 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001230 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001231 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1232 continue;
1233
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001234 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
1235 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001236 PIPE_CONTROL_GLOBAL_GTT_IVB |
1237 PIPE_CONTROL_QW_WRITE |
1238 PIPE_CONTROL_CS_STALL);
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001239 intel_ring_emit(ring, lower_32_bits(gtt_offset));
1240 intel_ring_emit(ring, upper_32_bits(gtt_offset));
1241 intel_ring_emit(ring, req->fence.seqno);
1242 intel_ring_emit(ring, 0);
1243 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001244 MI_SEMAPHORE_SIGNAL |
1245 MI_SEMAPHORE_TARGET(waiter->hw_id));
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001246 intel_ring_emit(ring, 0);
Ben Widawsky3e789982014-06-30 09:53:37 -07001247 }
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001248 intel_ring_advance(ring);
Ben Widawsky3e789982014-06-30 09:53:37 -07001249
1250 return 0;
1251}
1252
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001253static int gen8_xcs_signal(struct drm_i915_gem_request *req)
Ben Widawsky3e789982014-06-30 09:53:37 -07001254{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001255 struct intel_ring *ring = req->ring;
1256 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001257 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001258 enum intel_engine_id id;
1259 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001260
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +01001261 num_rings = INTEL_INFO(dev_priv)->num_rings;
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001262 ret = intel_ring_begin(req, (num_rings-1) * 6);
Ben Widawsky3e789982014-06-30 09:53:37 -07001263 if (ret)
1264 return ret;
1265
Akash Goel3b3f1652016-10-13 22:44:48 +05301266 for_each_engine(waiter, dev_priv, id) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001267 u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001268 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1269 continue;
1270
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001271 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001272 (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW);
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001273 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001274 lower_32_bits(gtt_offset) |
1275 MI_FLUSH_DW_USE_GTT);
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001276 intel_ring_emit(ring, upper_32_bits(gtt_offset));
1277 intel_ring_emit(ring, req->fence.seqno);
1278 intel_ring_emit(ring,
Chris Wilsonb5321f32016-08-02 22:50:18 +01001279 MI_SEMAPHORE_SIGNAL |
1280 MI_SEMAPHORE_TARGET(waiter->hw_id));
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001281 intel_ring_emit(ring, 0);
Ben Widawsky3e789982014-06-30 09:53:37 -07001282 }
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001283 intel_ring_advance(ring);
Ben Widawsky3e789982014-06-30 09:53:37 -07001284
1285 return 0;
1286}
1287
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001288static int gen6_signal(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001289{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001290 struct intel_ring *ring = req->ring;
1291 struct drm_i915_private *dev_priv = req->i915;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001292 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301293 enum intel_engine_id id;
Dave Gordonc3232b12016-03-23 18:19:53 +00001294 int ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001295
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +01001296 num_rings = INTEL_INFO(dev_priv)->num_rings;
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001297 ret = intel_ring_begin(req, round_up((num_rings-1) * 3, 2));
Ben Widawsky024a43e2014-04-29 14:52:30 -07001298 if (ret)
1299 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001300
Akash Goel3b3f1652016-10-13 22:44:48 +05301301 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001302 i915_reg_t mbox_reg;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001303
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001304 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
1305 continue;
1306
1307 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001308 if (i915_mmio_reg_valid(mbox_reg)) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001309 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1310 intel_ring_emit_reg(ring, mbox_reg);
1311 intel_ring_emit(ring, req->fence.seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001312 }
1313 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001314
Ben Widawskya1444b72014-06-30 09:53:35 -07001315 /* If num_dwords was rounded, make sure the tail pointer is correct */
1316 if (num_rings % 2 == 0)
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001317 intel_ring_emit(ring, MI_NOOP);
1318 intel_ring_advance(ring);
Ben Widawskya1444b72014-06-30 09:53:35 -07001319
Ben Widawsky024a43e2014-04-29 14:52:30 -07001320 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001321}
1322
Chris Wilsonb0411e72016-08-02 22:50:34 +01001323static void i9xx_submit_request(struct drm_i915_gem_request *request)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001324{
Chris Wilsonb0411e72016-08-02 22:50:34 +01001325 struct drm_i915_private *dev_priv = request->i915;
1326
1327 I915_WRITE_TAIL(request->engine,
1328 intel_ring_offset(request->ring, request->tail));
1329}
1330
1331static int i9xx_emit_request(struct drm_i915_gem_request *req)
1332{
Chris Wilson7e37f882016-08-02 22:50:21 +01001333 struct intel_ring *ring = req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001334 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001335
Chris Wilson9242f972016-08-02 22:50:33 +01001336 ret = intel_ring_begin(req, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001337 if (ret)
1338 return ret;
1339
Chris Wilsonb5321f32016-08-02 22:50:18 +01001340 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1341 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1342 intel_ring_emit(ring, req->fence.seqno);
1343 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001344 intel_ring_advance(ring);
1345
1346 req->tail = ring->tail;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001347
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001348 return 0;
1349}
1350
Chris Wilsonb0411e72016-08-02 22:50:34 +01001351/**
Chris Wilson618e4ca2016-08-02 22:50:35 +01001352 * gen6_sema_emit_request - Update the semaphore mailbox registers
Chris Wilsonb0411e72016-08-02 22:50:34 +01001353 *
1354 * @request - request to write to the ring
1355 *
1356 * Update the mailbox registers in the *other* rings with the current seqno.
1357 * This acts like a signal in the canonical semaphore.
1358 */
Chris Wilson618e4ca2016-08-02 22:50:35 +01001359static int gen6_sema_emit_request(struct drm_i915_gem_request *req)
Chris Wilsonb0411e72016-08-02 22:50:34 +01001360{
Chris Wilson618e4ca2016-08-02 22:50:35 +01001361 int ret;
Chris Wilsonb0411e72016-08-02 22:50:34 +01001362
Chris Wilson618e4ca2016-08-02 22:50:35 +01001363 ret = req->engine->semaphore.signal(req);
1364 if (ret)
1365 return ret;
Chris Wilsonb0411e72016-08-02 22:50:34 +01001366
1367 return i9xx_emit_request(req);
1368}
1369
Chris Wilsonddd66c52016-08-02 22:50:31 +01001370static int gen8_render_emit_request(struct drm_i915_gem_request *req)
Chris Wilsona58c01a2016-04-29 13:18:21 +01001371{
1372 struct intel_engine_cs *engine = req->engine;
Chris Wilson7e37f882016-08-02 22:50:21 +01001373 struct intel_ring *ring = req->ring;
Chris Wilsona58c01a2016-04-29 13:18:21 +01001374 int ret;
1375
Chris Wilson9242f972016-08-02 22:50:33 +01001376 if (engine->semaphore.signal) {
1377 ret = engine->semaphore.signal(req);
1378 if (ret)
1379 return ret;
1380 }
1381
1382 ret = intel_ring_begin(req, 8);
Chris Wilsona58c01a2016-04-29 13:18:21 +01001383 if (ret)
1384 return ret;
1385
Chris Wilsonb5321f32016-08-02 22:50:18 +01001386 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
1387 intel_ring_emit(ring, (PIPE_CONTROL_GLOBAL_GTT_IVB |
1388 PIPE_CONTROL_CS_STALL |
1389 PIPE_CONTROL_QW_WRITE));
1390 intel_ring_emit(ring, intel_hws_seqno_address(engine));
1391 intel_ring_emit(ring, 0);
1392 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsona58c01a2016-04-29 13:18:21 +01001393 /* We're thrashing one dword of HWS. */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001394 intel_ring_emit(ring, 0);
1395 intel_ring_emit(ring, MI_USER_INTERRUPT);
1396 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonddd66c52016-08-02 22:50:31 +01001397 intel_ring_advance(ring);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001398
1399 req->tail = ring->tail;
Chris Wilsona58c01a2016-04-29 13:18:21 +01001400
1401 return 0;
1402}
1403
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001404/**
1405 * intel_ring_sync - sync the waiter to the signaller on seqno
1406 *
1407 * @waiter - ring that is waiting
1408 * @signaller - ring which has, or will signal
1409 * @seqno - seqno which the waiter will block on
1410 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001411
1412static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001413gen8_ring_sync_to(struct drm_i915_gem_request *req,
1414 struct drm_i915_gem_request *signal)
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001415{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001416 struct intel_ring *ring = req->ring;
1417 struct drm_i915_private *dev_priv = req->i915;
1418 u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001419 struct i915_hw_ppgtt *ppgtt;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001420 int ret;
1421
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001422 ret = intel_ring_begin(req, 4);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001423 if (ret)
1424 return ret;
1425
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001426 intel_ring_emit(ring,
1427 MI_SEMAPHORE_WAIT |
1428 MI_SEMAPHORE_GLOBAL_GTT |
1429 MI_SEMAPHORE_SAD_GTE_SDD);
1430 intel_ring_emit(ring, signal->fence.seqno);
1431 intel_ring_emit(ring, lower_32_bits(offset));
1432 intel_ring_emit(ring, upper_32_bits(offset));
1433 intel_ring_advance(ring);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001434
1435 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1436 * pagetables and we must reload them before executing the batch.
1437 * We do this on the i915_switch_context() following the wait and
1438 * before the dispatch.
1439 */
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001440 ppgtt = req->ctx->ppgtt;
1441 if (ppgtt && req->engine->id != RCS)
1442 ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001443 return 0;
1444}
1445
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001446static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001447gen6_ring_sync_to(struct drm_i915_gem_request *req,
1448 struct drm_i915_gem_request *signal)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001449{
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001450 struct intel_ring *ring = req->ring;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001451 u32 dw1 = MI_SEMAPHORE_MBOX |
1452 MI_SEMAPHORE_COMPARE |
1453 MI_SEMAPHORE_REGISTER;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01001454 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
Ben Widawskyebc348b2014-04-29 14:52:28 -07001455 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001456
Chris Wilsonddf07be2016-08-02 22:50:39 +01001457 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
1458
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001459 ret = intel_ring_begin(req, 4);
Chris Wilsonddf07be2016-08-02 22:50:39 +01001460 if (ret)
1461 return ret;
1462
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001463 intel_ring_emit(ring, dw1 | wait_mbox);
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001464 /* Throughout all of the GEM code, seqno passed implies our current
1465 * seqno is >= the last seqno executed. However for hardware the
1466 * comparison is strictly greater than.
1467 */
Chris Wilsonad7bdb22016-08-02 22:50:40 +01001468 intel_ring_emit(ring, signal->fence.seqno - 1);
1469 intel_ring_emit(ring, 0);
1470 intel_ring_emit(ring, MI_NOOP);
1471 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001472
1473 return 0;
1474}
1475
Chris Wilsonf8973c22016-07-01 17:23:21 +01001476static void
Dave Gordon38a0f2d2016-07-20 18:16:06 +01001477gen5_seqno_barrier(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001478{
Chris Wilsonf8973c22016-07-01 17:23:21 +01001479 /* MI_STORE are internally buffered by the GPU and not flushed
1480 * either by MI_FLUSH or SyncFlush or any other combination of
1481 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001482 *
Chris Wilsonf8973c22016-07-01 17:23:21 +01001483 * "Only the submission of the store operation is guaranteed.
1484 * The write result will be complete (coherent) some time later
1485 * (this is practically a finite period but there is no guaranteed
1486 * latency)."
1487 *
1488 * Empirically, we observe that we need a delay of at least 75us to
1489 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001490 */
Chris Wilsonf8973c22016-07-01 17:23:21 +01001491 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001492}
1493
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001494static void
1495gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001496{
Chris Wilsonc0336662016-05-06 15:40:21 +01001497 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001498
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001499 /* Workaround to force correct ordering between irq and seqno writes on
1500 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +01001501 * ACTHD) before reading the status page.
1502 *
1503 * Note that this effectively stalls the read by the time it takes to
1504 * do a memory transaction, which more or less ensures that the write
1505 * from the GPU has sufficient time to invalidate the CPU cacheline.
1506 * Alternatively we could delay the interrupt from the CS ring to give
1507 * the write time to land, but that would incur a delay after every
1508 * batch i.e. much more frequent than a delay when waiting for the
1509 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001510 *
1511 * Also note that to prevent whole machine hangs on gen7, we have to
1512 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +01001513 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001514 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001515 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001516 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001517}
1518
Chris Wilson31bb59c2016-07-01 17:23:27 +01001519static void
1520gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001521{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001522 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +02001523}
1524
1525static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001526gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001527{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001528 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001529}
1530
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001531static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001532i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001533{
Chris Wilsonc0336662016-05-06 15:40:21 +01001534 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001535
Chris Wilson31bb59c2016-07-01 17:23:27 +01001536 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1537 I915_WRITE(IMR, dev_priv->irq_mask);
1538 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +01001539}
1540
1541static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001542i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001543{
Chris Wilsonc0336662016-05-06 15:40:21 +01001544 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001545
Chris Wilson31bb59c2016-07-01 17:23:27 +01001546 dev_priv->irq_mask |= engine->irq_enable_mask;
1547 I915_WRITE(IMR, dev_priv->irq_mask);
1548}
1549
1550static void
1551i8xx_irq_enable(struct intel_engine_cs *engine)
1552{
1553 struct drm_i915_private *dev_priv = engine->i915;
1554
1555 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1556 I915_WRITE16(IMR, dev_priv->irq_mask);
1557 POSTING_READ16(RING_IMR(engine->mmio_base));
1558}
1559
1560static void
1561i8xx_irq_disable(struct intel_engine_cs *engine)
1562{
1563 struct drm_i915_private *dev_priv = engine->i915;
1564
1565 dev_priv->irq_mask |= engine->irq_enable_mask;
1566 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001567}
1568
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001569static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001570bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001571{
Chris Wilson7e37f882016-08-02 22:50:21 +01001572 struct intel_ring *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001573 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001574
John Harrison5fb9de12015-05-29 17:44:07 +01001575 ret = intel_ring_begin(req, 2);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001576 if (ret)
1577 return ret;
1578
Chris Wilsonb5321f32016-08-02 22:50:18 +01001579 intel_ring_emit(ring, MI_FLUSH);
1580 intel_ring_emit(ring, MI_NOOP);
1581 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001582 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001583}
1584
Chris Wilson0f468322011-01-04 17:35:21 +00001585static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001586gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001587{
Chris Wilsonc0336662016-05-06 15:40:21 +01001588 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001589
Chris Wilson61ff75a2016-07-01 17:23:28 +01001590 I915_WRITE_IMR(engine,
1591 ~(engine->irq_enable_mask |
1592 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001593 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001594}
1595
1596static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001597gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001598{
Chris Wilsonc0336662016-05-06 15:40:21 +01001599 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001600
Chris Wilson61ff75a2016-07-01 17:23:28 +01001601 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001602 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001603}
1604
1605static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001606hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001607{
Chris Wilsonc0336662016-05-06 15:40:21 +01001608 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001609
Chris Wilson31bb59c2016-07-01 17:23:27 +01001610 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
Akash Goelf4e9af42016-10-12 21:54:30 +05301611 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001612}
1613
1614static void
1615hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1616{
1617 struct drm_i915_private *dev_priv = engine->i915;
1618
1619 I915_WRITE_IMR(engine, ~0);
Akash Goelf4e9af42016-10-12 21:54:30 +05301620 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001621}
1622
1623static void
1624gen8_irq_enable(struct intel_engine_cs *engine)
1625{
1626 struct drm_i915_private *dev_priv = engine->i915;
1627
Chris Wilson61ff75a2016-07-01 17:23:28 +01001628 I915_WRITE_IMR(engine,
1629 ~(engine->irq_enable_mask |
1630 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001631 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1632}
1633
1634static void
1635gen8_irq_disable(struct intel_engine_cs *engine)
1636{
1637 struct drm_i915_private *dev_priv = engine->i915;
1638
Chris Wilson61ff75a2016-07-01 17:23:28 +01001639 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001640}
1641
Zou Nan haid1b851f2010-05-21 09:08:57 +08001642static int
Chris Wilson803688b2016-08-02 22:50:27 +01001643i965_emit_bb_start(struct drm_i915_gem_request *req,
1644 u64 offset, u32 length,
1645 unsigned int dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001646{
Chris Wilson7e37f882016-08-02 22:50:21 +01001647 struct intel_ring *ring = req->ring;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001648 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001649
John Harrison5fb9de12015-05-29 17:44:07 +01001650 ret = intel_ring_begin(req, 2);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001651 if (ret)
1652 return ret;
1653
Chris Wilsonb5321f32016-08-02 22:50:18 +01001654 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001655 MI_BATCH_BUFFER_START |
1656 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001657 (dispatch_flags & I915_DISPATCH_SECURE ?
1658 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonb5321f32016-08-02 22:50:18 +01001659 intel_ring_emit(ring, offset);
1660 intel_ring_advance(ring);
Chris Wilson78501ea2010-10-27 12:18:21 +01001661
Zou Nan haid1b851f2010-05-21 09:08:57 +08001662 return 0;
1663}
1664
Daniel Vetterb45305f2012-12-17 16:21:27 +01001665/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1666#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001667#define I830_TLB_ENTRIES (2)
1668#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001669static int
Chris Wilson803688b2016-08-02 22:50:27 +01001670i830_emit_bb_start(struct drm_i915_gem_request *req,
1671 u64 offset, u32 len,
1672 unsigned int dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001673{
Chris Wilson7e37f882016-08-02 22:50:21 +01001674 struct intel_ring *ring = req->ring;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001675 u32 cs_offset = i915_ggtt_offset(req->engine->scratch);
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001676 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001677
John Harrison5fb9de12015-05-29 17:44:07 +01001678 ret = intel_ring_begin(req, 6);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001679 if (ret)
1680 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001681
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001682 /* Evict the invalid PTE TLBs */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001683 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1684 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1685 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1686 intel_ring_emit(ring, cs_offset);
1687 intel_ring_emit(ring, 0xdeadbeef);
1688 intel_ring_emit(ring, MI_NOOP);
1689 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001690
John Harrison8e004ef2015-02-13 11:48:10 +00001691 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001692 if (len > I830_BATCH_LIMIT)
1693 return -ENOSPC;
1694
John Harrison5fb9de12015-05-29 17:44:07 +01001695 ret = intel_ring_begin(req, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001696 if (ret)
1697 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001698
1699 /* Blit the batch (which has now all relocs applied) to the
1700 * stable batch scratch bo area (so that the CS never
1701 * stumbles over its tlb invalidation bug) ...
1702 */
Chris Wilsonb5321f32016-08-02 22:50:18 +01001703 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1704 intel_ring_emit(ring,
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001705 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilsonb5321f32016-08-02 22:50:18 +01001706 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1707 intel_ring_emit(ring, cs_offset);
1708 intel_ring_emit(ring, 4096);
1709 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001710
Chris Wilsonb5321f32016-08-02 22:50:18 +01001711 intel_ring_emit(ring, MI_FLUSH);
1712 intel_ring_emit(ring, MI_NOOP);
1713 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001714
1715 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001716 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001717 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001718
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001719 ret = intel_ring_begin(req, 2);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001720 if (ret)
1721 return ret;
1722
Chris Wilsonb5321f32016-08-02 22:50:18 +01001723 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1724 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1725 0 : MI_BATCH_NON_SECURE));
1726 intel_ring_advance(ring);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001727
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001728 return 0;
1729}
1730
1731static int
Chris Wilson803688b2016-08-02 22:50:27 +01001732i915_emit_bb_start(struct drm_i915_gem_request *req,
1733 u64 offset, u32 len,
1734 unsigned int dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001735{
Chris Wilson7e37f882016-08-02 22:50:21 +01001736 struct intel_ring *ring = req->ring;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001737 int ret;
1738
John Harrison5fb9de12015-05-29 17:44:07 +01001739 ret = intel_ring_begin(req, 2);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001740 if (ret)
1741 return ret;
1742
Chris Wilsonb5321f32016-08-02 22:50:18 +01001743 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1744 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1745 0 : MI_BATCH_NON_SECURE));
1746 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001747
Eric Anholt62fdfea2010-05-21 13:26:39 -07001748 return 0;
1749}
1750
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001751static void cleanup_phys_status_page(struct intel_engine_cs *engine)
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001752{
Chris Wilsonc0336662016-05-06 15:40:21 +01001753 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001754
1755 if (!dev_priv->status_page_dmah)
1756 return;
1757
Chris Wilson91c8a322016-07-05 10:40:23 +01001758 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001759 engine->status_page.page_addr = NULL;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001760}
1761
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001762static void cleanup_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001763{
Chris Wilson57e88532016-08-15 10:48:57 +01001764 struct i915_vma *vma;
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001765 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001766
Chris Wilson57e88532016-08-15 10:48:57 +01001767 vma = fetch_and_zero(&engine->status_page.vma);
1768 if (!vma)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001769 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001770
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001771 obj = vma->obj;
1772
Chris Wilson57e88532016-08-15 10:48:57 +01001773 i915_vma_unpin(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001774 i915_vma_close(vma);
1775
1776 i915_gem_object_unpin_map(obj);
1777 __i915_gem_object_release_unless_active(obj);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001778}
1779
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001780static int init_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001781{
Chris Wilson57e88532016-08-15 10:48:57 +01001782 struct drm_i915_gem_object *obj;
1783 struct i915_vma *vma;
1784 unsigned int flags;
Chris Wilson920cf412016-10-28 13:58:30 +01001785 void *vaddr;
Chris Wilson57e88532016-08-15 10:48:57 +01001786 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001787
Chris Wilson920cf412016-10-28 13:58:30 +01001788 obj = i915_gem_object_create_internal(engine->i915, 4096);
Chris Wilson57e88532016-08-15 10:48:57 +01001789 if (IS_ERR(obj)) {
1790 DRM_ERROR("Failed to allocate status page\n");
1791 return PTR_ERR(obj);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001792 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001793
Chris Wilson57e88532016-08-15 10:48:57 +01001794 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1795 if (ret)
1796 goto err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001797
Chris Wilson57e88532016-08-15 10:48:57 +01001798 vma = i915_vma_create(obj, &engine->i915->ggtt.base, NULL);
1799 if (IS_ERR(vma)) {
1800 ret = PTR_ERR(vma);
1801 goto err;
1802 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001803
Chris Wilson57e88532016-08-15 10:48:57 +01001804 flags = PIN_GLOBAL;
1805 if (!HAS_LLC(engine->i915))
1806 /* On g33, we cannot place HWS above 256MiB, so
1807 * restrict its pinning to the low mappable arena.
1808 * Though this restriction is not documented for
1809 * gen4, gen5, or byt, they also behave similarly
1810 * and hang if the HWS is placed at the top of the
1811 * GTT. To generalise, it appears that all !llc
1812 * platforms have issues with us placing the HWS
1813 * above the mappable region (even though we never
1814 * actualy map it).
1815 */
1816 flags |= PIN_MAPPABLE;
1817 ret = i915_vma_pin(vma, 0, 4096, flags);
1818 if (ret)
1819 goto err;
1820
Chris Wilson920cf412016-10-28 13:58:30 +01001821 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1822 if (IS_ERR(vaddr)) {
1823 ret = PTR_ERR(vaddr);
1824 goto err_unpin;
1825 }
1826
Chris Wilson57e88532016-08-15 10:48:57 +01001827 engine->status_page.vma = vma;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001828 engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
Chris Wilson920cf412016-10-28 13:58:30 +01001829 engine->status_page.page_addr = memset(vaddr, 0, 4096);
Chris Wilson57e88532016-08-15 10:48:57 +01001830
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001831 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1832 engine->name, i915_ggtt_offset(vma));
Eric Anholt62fdfea2010-05-21 13:26:39 -07001833 return 0;
Chris Wilson57e88532016-08-15 10:48:57 +01001834
Chris Wilson920cf412016-10-28 13:58:30 +01001835err_unpin:
1836 i915_vma_unpin(vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001837err:
1838 i915_gem_object_put(obj);
1839 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001840}
1841
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001842static int init_phys_status_page(struct intel_engine_cs *engine)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001843{
Chris Wilsonc0336662016-05-06 15:40:21 +01001844 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001845
Chris Wilson57e88532016-08-15 10:48:57 +01001846 dev_priv->status_page_dmah =
1847 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
1848 if (!dev_priv->status_page_dmah)
1849 return -ENOMEM;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001850
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001851 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1852 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001853
1854 return 0;
1855}
1856
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001857int intel_ring_pin(struct intel_ring *ring)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001858{
Chris Wilsona687a432016-04-13 17:35:11 +01001859 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
Chris Wilson57e88532016-08-15 10:48:57 +01001860 unsigned int flags = PIN_GLOBAL | PIN_OFFSET_BIAS | 4096;
Chris Wilson9d808412016-08-18 17:16:56 +01001861 enum i915_map_type map;
Chris Wilson57e88532016-08-15 10:48:57 +01001862 struct i915_vma *vma = ring->vma;
Dave Gordon83052162016-04-12 14:46:16 +01001863 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001864 int ret;
1865
Chris Wilson57e88532016-08-15 10:48:57 +01001866 GEM_BUG_ON(ring->vaddr);
1867
Chris Wilson9d808412016-08-18 17:16:56 +01001868 map = HAS_LLC(ring->engine->i915) ? I915_MAP_WB : I915_MAP_WC;
1869
1870 if (vma->obj->stolen)
Chris Wilson57e88532016-08-15 10:48:57 +01001871 flags |= PIN_MAPPABLE;
1872
1873 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilson9d808412016-08-18 17:16:56 +01001874 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
Chris Wilson57e88532016-08-15 10:48:57 +01001875 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1876 else
1877 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1878 if (unlikely(ret))
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001879 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001880 }
1881
Chris Wilson57e88532016-08-15 10:48:57 +01001882 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1883 if (unlikely(ret))
1884 return ret;
1885
Chris Wilson9d808412016-08-18 17:16:56 +01001886 if (i915_vma_is_map_and_fenceable(vma))
Chris Wilson57e88532016-08-15 10:48:57 +01001887 addr = (void __force *)i915_vma_pin_iomap(vma);
1888 else
Chris Wilson9d808412016-08-18 17:16:56 +01001889 addr = i915_gem_object_pin_map(vma->obj, map);
Chris Wilson57e88532016-08-15 10:48:57 +01001890 if (IS_ERR(addr))
1891 goto err;
1892
Chris Wilson32c04f12016-08-02 22:50:22 +01001893 ring->vaddr = addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001894 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01001895
Chris Wilson57e88532016-08-15 10:48:57 +01001896err:
1897 i915_vma_unpin(vma);
1898 return PTR_ERR(addr);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001899}
1900
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001901void intel_ring_unpin(struct intel_ring *ring)
1902{
1903 GEM_BUG_ON(!ring->vma);
1904 GEM_BUG_ON(!ring->vaddr);
1905
Chris Wilson9d808412016-08-18 17:16:56 +01001906 if (i915_vma_is_map_and_fenceable(ring->vma))
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001907 i915_vma_unpin_iomap(ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001908 else
1909 i915_gem_object_unpin_map(ring->vma->obj);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001910 ring->vaddr = NULL;
1911
Chris Wilson57e88532016-08-15 10:48:57 +01001912 i915_vma_unpin(ring->vma);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001913}
1914
Chris Wilson57e88532016-08-15 10:48:57 +01001915static struct i915_vma *
1916intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
Oscar Mateo2919d292014-07-03 16:28:02 +01001917{
Chris Wilsone3efda42014-04-09 09:19:41 +01001918 struct drm_i915_gem_object *obj;
Chris Wilson57e88532016-08-15 10:48:57 +01001919 struct i915_vma *vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001920
Chris Wilsonc58b7352016-08-18 17:16:57 +01001921 obj = i915_gem_object_create_stolen(&dev_priv->drm, size);
1922 if (!obj)
Chris Wilson57e88532016-08-15 10:48:57 +01001923 obj = i915_gem_object_create(&dev_priv->drm, size);
1924 if (IS_ERR(obj))
1925 return ERR_CAST(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001926
Akash Goel24f3a8c2014-06-17 10:59:42 +05301927 /* mark ring buffers as read-only from GPU side by default */
1928 obj->gt_ro = 1;
1929
Chris Wilson57e88532016-08-15 10:48:57 +01001930 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
1931 if (IS_ERR(vma))
1932 goto err;
Chris Wilsone3efda42014-04-09 09:19:41 +01001933
Chris Wilson57e88532016-08-15 10:48:57 +01001934 return vma;
1935
1936err:
1937 i915_gem_object_put(obj);
1938 return vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001939}
1940
Chris Wilson7e37f882016-08-02 22:50:21 +01001941struct intel_ring *
1942intel_engine_create_ring(struct intel_engine_cs *engine, int size)
Chris Wilson01101fa2015-09-03 13:01:39 +01001943{
Chris Wilson7e37f882016-08-02 22:50:21 +01001944 struct intel_ring *ring;
Chris Wilson57e88532016-08-15 10:48:57 +01001945 struct i915_vma *vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001946
Chris Wilson8f942012016-08-02 22:50:30 +01001947 GEM_BUG_ON(!is_power_of_2(size));
Chris Wilson62ae14b2016-10-04 21:11:25 +01001948 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
Chris Wilson8f942012016-08-02 22:50:30 +01001949
Chris Wilson01101fa2015-09-03 13:01:39 +01001950 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson57e88532016-08-15 10:48:57 +01001951 if (!ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001952 return ERR_PTR(-ENOMEM);
1953
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001954 ring->engine = engine;
Chris Wilson01101fa2015-09-03 13:01:39 +01001955
Chris Wilson675d9ad2016-08-04 07:52:36 +01001956 INIT_LIST_HEAD(&ring->request_list);
1957
Chris Wilson01101fa2015-09-03 13:01:39 +01001958 ring->size = size;
1959 /* Workaround an erratum on the i830 which causes a hang if
1960 * the TAIL pointer points to within the last 2 cachelines
1961 * of the buffer.
1962 */
1963 ring->effective_size = size;
Chris Wilsonc0336662016-05-06 15:40:21 +01001964 if (IS_I830(engine->i915) || IS_845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01001965 ring->effective_size -= 2 * CACHELINE_BYTES;
1966
1967 ring->last_retired_head = -1;
1968 intel_ring_update_space(ring);
1969
Chris Wilson57e88532016-08-15 10:48:57 +01001970 vma = intel_ring_create_vma(engine->i915, size);
1971 if (IS_ERR(vma)) {
Chris Wilson01101fa2015-09-03 13:01:39 +01001972 kfree(ring);
Chris Wilson57e88532016-08-15 10:48:57 +01001973 return ERR_CAST(vma);
Chris Wilson01101fa2015-09-03 13:01:39 +01001974 }
Chris Wilson57e88532016-08-15 10:48:57 +01001975 ring->vma = vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001976
1977 return ring;
1978}
1979
1980void
Chris Wilson7e37f882016-08-02 22:50:21 +01001981intel_ring_free(struct intel_ring *ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001982{
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001983 struct drm_i915_gem_object *obj = ring->vma->obj;
1984
1985 i915_vma_close(ring->vma);
1986 __i915_gem_object_release_unless_active(obj);
1987
Chris Wilson01101fa2015-09-03 13:01:39 +01001988 kfree(ring);
1989}
1990
Chris Wilson0cb26a82016-06-24 14:55:53 +01001991static int intel_ring_context_pin(struct i915_gem_context *ctx,
1992 struct intel_engine_cs *engine)
1993{
1994 struct intel_context *ce = &ctx->engine[engine->id];
1995 int ret;
1996
Chris Wilson91c8a322016-07-05 10:40:23 +01001997 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001998
1999 if (ce->pin_count++)
2000 return 0;
2001
2002 if (ce->state) {
Chris Wilson7abc98f2016-08-15 10:48:55 +01002003 ret = i915_gem_object_set_to_gtt_domain(ce->state->obj, false);
2004 if (ret)
2005 goto error;
2006
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002007 ret = i915_vma_pin(ce->state, 0, ctx->ggtt_alignment,
2008 PIN_GLOBAL | PIN_HIGH);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002009 if (ret)
2010 goto error;
2011 }
2012
Chris Wilsonc7c3c072016-06-24 14:55:54 +01002013 /* The kernel context is only used as a placeholder for flushing the
2014 * active context. It is never used for submitting user rendering and
2015 * as such never requires the golden render context, and so we can skip
2016 * emitting it when we switch to the kernel context. This is required
2017 * as during eviction we cannot allocate and pin the renderstate in
2018 * order to initialise the context.
2019 */
2020 if (ctx == ctx->i915->kernel_context)
2021 ce->initialised = true;
2022
Chris Wilson9a6feaf2016-07-20 13:31:50 +01002023 i915_gem_context_get(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002024 return 0;
2025
2026error:
2027 ce->pin_count = 0;
2028 return ret;
2029}
2030
2031static void intel_ring_context_unpin(struct i915_gem_context *ctx,
2032 struct intel_engine_cs *engine)
2033{
2034 struct intel_context *ce = &ctx->engine[engine->id];
2035
Chris Wilson91c8a322016-07-05 10:40:23 +01002036 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002037
2038 if (--ce->pin_count)
2039 return;
2040
2041 if (ce->state)
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002042 i915_vma_unpin(ce->state);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002043
Chris Wilson9a6feaf2016-07-20 13:31:50 +01002044 i915_gem_context_put(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002045}
2046
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002047static int intel_init_ring_buffer(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002048{
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002049 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson32c04f12016-08-02 22:50:22 +01002050 struct intel_ring *ring;
Chris Wilsondd785e32010-08-07 11:01:34 +01002051 int ret;
2052
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002053 WARN_ON(engine->buffer);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002054
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002055 intel_engine_setup_common(engine);
2056
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002057 memset(engine->semaphore.sync_seqno, 0,
2058 sizeof(engine->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002059
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002060 ret = intel_engine_init_common(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +01002061 if (ret)
2062 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002063
Chris Wilson0cb26a82016-06-24 14:55:53 +01002064 /* We may need to do things with the shrinker which
2065 * require us to immediately switch back to the default
2066 * context. This can cause a problem as pinning the
2067 * default context also requires GTT space which may not
2068 * be available. To avoid this we always pin the default
2069 * context.
2070 */
2071 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2072 if (ret)
2073 goto error;
2074
Chris Wilson32c04f12016-08-02 22:50:22 +01002075 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
2076 if (IS_ERR(ring)) {
2077 ret = PTR_ERR(ring);
Dave Gordonb0366a52015-12-08 15:02:36 +00002078 goto error;
2079 }
Chris Wilson01101fa2015-09-03 13:01:39 +01002080
Carlos Santa31776592016-08-17 12:30:56 -07002081 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
2082 WARN_ON(engine->id != RCS);
2083 ret = init_phys_status_page(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002084 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002085 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002086 } else {
Carlos Santa31776592016-08-17 12:30:56 -07002087 ret = init_status_page(engine);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002088 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002089 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002090 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002091
Chris Wilsonaad29fb2016-08-02 22:50:23 +01002092 ret = intel_ring_pin(ring);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002093 if (ret) {
Chris Wilson57e88532016-08-15 10:48:57 +01002094 intel_ring_free(ring);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002095 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002096 }
Chris Wilson57e88532016-08-15 10:48:57 +01002097 engine->buffer = ring;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002098
Oscar Mateo8ee14972014-05-22 14:13:34 +01002099 return 0;
2100
2101error:
Chris Wilson7e37f882016-08-02 22:50:21 +01002102 intel_engine_cleanup(engine);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002103 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002104}
2105
Chris Wilson7e37f882016-08-02 22:50:21 +01002106void intel_engine_cleanup(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002107{
John Harrison6402c332014-10-31 12:00:26 +00002108 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01002109
Chris Wilsonc0336662016-05-06 15:40:21 +01002110 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00002111
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002112 if (engine->buffer) {
Chris Wilson21a2c582016-08-15 10:49:11 +01002113 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
2114 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002115
Chris Wilsonaad29fb2016-08-02 22:50:23 +01002116 intel_ring_unpin(engine->buffer);
Chris Wilson7e37f882016-08-02 22:50:21 +01002117 intel_ring_free(engine->buffer);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002118 engine->buffer = NULL;
Dave Gordonb0366a52015-12-08 15:02:36 +00002119 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002120
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002121 if (engine->cleanup)
2122 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08002123
Carlos Santa31776592016-08-17 12:30:56 -07002124 if (HWS_NEEDS_PHYSICAL(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002125 WARN_ON(engine->id != RCS);
2126 cleanup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -07002127 } else {
2128 cleanup_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002129 }
Brad Volkin44e895a2014-05-10 14:10:43 -07002130
Chris Wilson96a945a2016-08-03 13:19:16 +01002131 intel_engine_cleanup_common(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002132
2133 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2134
Chris Wilsonc0336662016-05-06 15:40:21 +01002135 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05302136 dev_priv->engine[engine->id] = NULL;
2137 kfree(engine);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002138}
2139
Chris Wilson821ed7d2016-09-09 14:11:53 +01002140void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
2141{
2142 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302143 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002144
Akash Goel3b3f1652016-10-13 22:44:48 +05302145 for_each_engine(engine, dev_priv, id) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01002146 engine->buffer->head = engine->buffer->tail;
2147 engine->buffer->last_retired_head = -1;
2148 }
2149}
2150
John Harrison6689cb22015-03-19 12:30:08 +00002151int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002152{
Chris Wilson63103462016-04-28 09:56:49 +01002153 int ret;
2154
2155 /* Flush enough space to reduce the likelihood of waiting after
2156 * we start building the request - in which case we will just
2157 * have to repeat work.
2158 */
Chris Wilsona0442462016-04-29 09:07:05 +01002159 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002160
Chris Wilson1dae2df2016-08-02 22:50:19 +01002161 request->ring = request->engine->buffer;
Chris Wilson63103462016-04-28 09:56:49 +01002162
2163 ret = intel_ring_begin(request, 0);
2164 if (ret)
2165 return ret;
2166
Chris Wilsona0442462016-04-29 09:07:05 +01002167 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002168 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002169}
2170
Chris Wilson987046a2016-04-28 09:56:46 +01002171static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002172{
Chris Wilson7e37f882016-08-02 22:50:21 +01002173 struct intel_ring *ring = req->ring;
Chris Wilson987046a2016-04-28 09:56:46 +01002174 struct drm_i915_gem_request *target;
Chris Wilsone95433c2016-10-28 13:58:27 +01002175 long timeout;
2176
2177 lockdep_assert_held(&req->i915->drm.struct_mutex);
Chris Wilson987046a2016-04-28 09:56:46 +01002178
Chris Wilson1dae2df2016-08-02 22:50:19 +01002179 intel_ring_update_space(ring);
2180 if (ring->space >= bytes)
Chris Wilson987046a2016-04-28 09:56:46 +01002181 return 0;
2182
2183 /*
2184 * Space is reserved in the ringbuffer for finalising the request,
2185 * as that cannot be allowed to fail. During request finalisation,
2186 * reserved_space is set to 0 to stop the overallocation and the
2187 * assumption is that then we never need to wait (which has the
2188 * risk of failing with EINTR).
2189 *
2190 * See also i915_gem_request_alloc() and i915_add_request().
2191 */
Chris Wilson0251a962016-04-28 09:56:47 +01002192 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01002193
Chris Wilson675d9ad2016-08-04 07:52:36 +01002194 list_for_each_entry(target, &ring->request_list, ring_link) {
Chris Wilson987046a2016-04-28 09:56:46 +01002195 unsigned space;
2196
Chris Wilson987046a2016-04-28 09:56:46 +01002197 /* Would completion of this request free enough space? */
Chris Wilson1dae2df2016-08-02 22:50:19 +01002198 space = __intel_ring_space(target->postfix, ring->tail,
2199 ring->size);
Chris Wilson987046a2016-04-28 09:56:46 +01002200 if (space >= bytes)
2201 break;
2202 }
2203
Chris Wilson675d9ad2016-08-04 07:52:36 +01002204 if (WARN_ON(&target->ring_link == &ring->request_list))
Chris Wilson987046a2016-04-28 09:56:46 +01002205 return -ENOSPC;
2206
Chris Wilsone95433c2016-10-28 13:58:27 +01002207 timeout = i915_wait_request(target,
2208 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
2209 MAX_SCHEDULE_TIMEOUT);
2210 if (timeout < 0)
2211 return timeout;
Chris Wilson7da844c2016-08-04 07:52:38 +01002212
Chris Wilson7da844c2016-08-04 07:52:38 +01002213 i915_gem_request_retire_upto(target);
2214
2215 intel_ring_update_space(ring);
2216 GEM_BUG_ON(ring->space < bytes);
2217 return 0;
Chris Wilson987046a2016-04-28 09:56:46 +01002218}
2219
2220int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
2221{
Chris Wilson7e37f882016-08-02 22:50:21 +01002222 struct intel_ring *ring = req->ring;
Chris Wilson1dae2df2016-08-02 22:50:19 +01002223 int remain_actual = ring->size - ring->tail;
2224 int remain_usable = ring->effective_size - ring->tail;
Chris Wilson987046a2016-04-28 09:56:46 +01002225 int bytes = num_dwords * sizeof(u32);
2226 int total_bytes, wait_bytes;
John Harrison79bbcc22015-06-30 12:40:55 +01002227 bool need_wrap = false;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002228
Chris Wilson0251a962016-04-28 09:56:47 +01002229 total_bytes = bytes + req->reserved_space;
John Harrison29b1b412015-06-18 13:10:09 +01002230
John Harrison79bbcc22015-06-30 12:40:55 +01002231 if (unlikely(bytes > remain_usable)) {
2232 /*
2233 * Not enough space for the basic request. So need to flush
2234 * out the remainder and then wait for base + reserved.
2235 */
2236 wait_bytes = remain_actual + total_bytes;
2237 need_wrap = true;
Chris Wilson987046a2016-04-28 09:56:46 +01002238 } else if (unlikely(total_bytes > remain_usable)) {
2239 /*
2240 * The base request will fit but the reserved space
2241 * falls off the end. So we don't need an immediate wrap
2242 * and only need to effectively wait for the reserved
2243 * size space from the start of ringbuffer.
2244 */
Chris Wilson0251a962016-04-28 09:56:47 +01002245 wait_bytes = remain_actual + req->reserved_space;
John Harrison79bbcc22015-06-30 12:40:55 +01002246 } else {
Chris Wilson987046a2016-04-28 09:56:46 +01002247 /* No wrapping required, just waiting. */
2248 wait_bytes = total_bytes;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002249 }
2250
Chris Wilson1dae2df2016-08-02 22:50:19 +01002251 if (wait_bytes > ring->space) {
Chris Wilson987046a2016-04-28 09:56:46 +01002252 int ret = wait_for_space(req, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002253 if (unlikely(ret))
2254 return ret;
2255 }
2256
Chris Wilson987046a2016-04-28 09:56:46 +01002257 if (unlikely(need_wrap)) {
Chris Wilson1dae2df2016-08-02 22:50:19 +01002258 GEM_BUG_ON(remain_actual > ring->space);
2259 GEM_BUG_ON(ring->tail + remain_actual > ring->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002260
Chris Wilson987046a2016-04-28 09:56:46 +01002261 /* Fill the tail with MI_NOOP */
Chris Wilson1dae2df2016-08-02 22:50:19 +01002262 memset(ring->vaddr + ring->tail, 0, remain_actual);
2263 ring->tail = 0;
2264 ring->space -= remain_actual;
Chris Wilson987046a2016-04-28 09:56:46 +01002265 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002266
Chris Wilson1dae2df2016-08-02 22:50:19 +01002267 ring->space -= bytes;
2268 GEM_BUG_ON(ring->space < 0);
Chris Wilson304d6952014-01-02 14:32:35 +00002269 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002270}
2271
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002272/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01002273int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002274{
Chris Wilson7e37f882016-08-02 22:50:21 +01002275 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002276 int num_dwords =
2277 (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002278 int ret;
2279
2280 if (num_dwords == 0)
2281 return 0;
2282
Chris Wilson18393f62014-04-09 09:19:40 +01002283 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
John Harrison5fb9de12015-05-29 17:44:07 +01002284 ret = intel_ring_begin(req, num_dwords);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002285 if (ret)
2286 return ret;
2287
2288 while (num_dwords--)
Chris Wilsonb5321f32016-08-02 22:50:18 +01002289 intel_ring_emit(ring, MI_NOOP);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002290
Chris Wilsonb5321f32016-08-02 22:50:18 +01002291 intel_ring_advance(ring);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002292
2293 return 0;
2294}
2295
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01002296static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002297{
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01002298 struct drm_i915_private *dev_priv = request->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002299
Chris Wilson76f84212016-06-30 15:33:45 +01002300 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2301
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002302 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002303
Chris Wilson12f55812012-07-05 17:14:01 +01002304 /* Disable notification that the ring is IDLE. The GT
2305 * will then assume that it is busy and bring it out of rc6.
2306 */
Chris Wilson76f84212016-06-30 15:33:45 +01002307 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2308 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01002309
2310 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01002311 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01002312
2313 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson76f84212016-06-30 15:33:45 +01002314 if (intel_wait_for_register_fw(dev_priv,
2315 GEN6_BSD_SLEEP_PSMI_CONTROL,
2316 GEN6_BSD_SLEEP_INDICATOR,
2317 0,
2318 50))
Chris Wilson12f55812012-07-05 17:14:01 +01002319 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002320
Chris Wilson12f55812012-07-05 17:14:01 +01002321 /* Now that the ring is fully powered up, update the tail */
Chris Wilsonb0411e72016-08-02 22:50:34 +01002322 i9xx_submit_request(request);
Chris Wilson12f55812012-07-05 17:14:01 +01002323
2324 /* Let the ring send IDLE messages to the GT again,
2325 * and so let it sleep to conserve power when idle.
2326 */
Chris Wilson76f84212016-06-30 15:33:45 +01002327 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2328 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2329
2330 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002331}
2332
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01002333static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002334{
Chris Wilson7e37f882016-08-02 22:50:21 +01002335 struct intel_ring *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002336 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002337 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002338
John Harrison5fb9de12015-05-29 17:44:07 +01002339 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002340 if (ret)
2341 return ret;
2342
Chris Wilson71a77e02011-02-02 12:13:49 +00002343 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002344 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002345 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002346
2347 /* We always require a command barrier so that subsequent
2348 * commands, such as breadcrumb interrupts, are strictly ordered
2349 * wrt the contents of the write cache being flushed to memory
2350 * (and thus being coherent from the CPU).
2351 */
2352 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2353
Jesse Barnes9a289772012-10-26 09:42:42 -07002354 /*
2355 * Bspec vol 1c.5 - video engine command streamer:
2356 * "If ENABLED, all TLBs will be invalidated once the flush
2357 * operation is complete. This bit is only valid when the
2358 * Post-Sync Operation field is a value of 1h or 3h."
2359 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01002360 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002361 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2362
Chris Wilsonb5321f32016-08-02 22:50:18 +01002363 intel_ring_emit(ring, cmd);
2364 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002365 if (INTEL_GEN(req->i915) >= 8) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002366 intel_ring_emit(ring, 0); /* upper addr */
2367 intel_ring_emit(ring, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002368 } else {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002369 intel_ring_emit(ring, 0);
2370 intel_ring_emit(ring, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002371 }
Chris Wilsonb5321f32016-08-02 22:50:18 +01002372 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002373 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002374}
2375
2376static int
Chris Wilson803688b2016-08-02 22:50:27 +01002377gen8_emit_bb_start(struct drm_i915_gem_request *req,
2378 u64 offset, u32 len,
2379 unsigned int dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002380{
Chris Wilson7e37f882016-08-02 22:50:21 +01002381 struct intel_ring *ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002382 bool ppgtt = USES_PPGTT(req->i915) &&
John Harrison8e004ef2015-02-13 11:48:10 +00002383 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002384 int ret;
2385
John Harrison5fb9de12015-05-29 17:44:07 +01002386 ret = intel_ring_begin(req, 4);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002387 if (ret)
2388 return ret;
2389
2390 /* FIXME(BDW): Address space and security selectors. */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002391 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002392 (dispatch_flags & I915_DISPATCH_RS ?
2393 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsonb5321f32016-08-02 22:50:18 +01002394 intel_ring_emit(ring, lower_32_bits(offset));
2395 intel_ring_emit(ring, upper_32_bits(offset));
2396 intel_ring_emit(ring, MI_NOOP);
2397 intel_ring_advance(ring);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002398
2399 return 0;
2400}
2401
2402static int
Chris Wilson803688b2016-08-02 22:50:27 +01002403hsw_emit_bb_start(struct drm_i915_gem_request *req,
2404 u64 offset, u32 len,
2405 unsigned int dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002406{
Chris Wilson7e37f882016-08-02 22:50:21 +01002407 struct intel_ring *ring = req->ring;
Akshay Joshi0206e352011-08-16 15:34:10 -04002408 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002409
John Harrison5fb9de12015-05-29 17:44:07 +01002410 ret = intel_ring_begin(req, 2);
Akshay Joshi0206e352011-08-16 15:34:10 -04002411 if (ret)
2412 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002413
Chris Wilsonb5321f32016-08-02 22:50:18 +01002414 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002415 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002416 (dispatch_flags & I915_DISPATCH_SECURE ?
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002417 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2418 (dispatch_flags & I915_DISPATCH_RS ?
2419 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002420 /* bit0-7 is the length on GEN6+ */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002421 intel_ring_emit(ring, offset);
2422 intel_ring_advance(ring);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002423
2424 return 0;
2425}
2426
2427static int
Chris Wilson803688b2016-08-02 22:50:27 +01002428gen6_emit_bb_start(struct drm_i915_gem_request *req,
2429 u64 offset, u32 len,
2430 unsigned int dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002431{
Chris Wilson7e37f882016-08-02 22:50:21 +01002432 struct intel_ring *ring = req->ring;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002433 int ret;
2434
John Harrison5fb9de12015-05-29 17:44:07 +01002435 ret = intel_ring_begin(req, 2);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002436 if (ret)
2437 return ret;
2438
Chris Wilsonb5321f32016-08-02 22:50:18 +01002439 intel_ring_emit(ring,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002440 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002441 (dispatch_flags & I915_DISPATCH_SECURE ?
2442 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002443 /* bit0-7 is the length on GEN6+ */
Chris Wilsonb5321f32016-08-02 22:50:18 +01002444 intel_ring_emit(ring, offset);
2445 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002446
Akshay Joshi0206e352011-08-16 15:34:10 -04002447 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002448}
2449
Chris Wilson549f7362010-10-19 11:19:32 +01002450/* Blitter support (SandyBridge+) */
2451
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01002452static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan hai8d192152010-11-02 16:31:01 +08002453{
Chris Wilson7e37f882016-08-02 22:50:21 +01002454 struct intel_ring *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002455 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002456 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002457
John Harrison5fb9de12015-05-29 17:44:07 +01002458 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002459 if (ret)
2460 return ret;
2461
Chris Wilson71a77e02011-02-02 12:13:49 +00002462 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002463 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002464 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002465
2466 /* We always require a command barrier so that subsequent
2467 * commands, such as breadcrumb interrupts, are strictly ordered
2468 * wrt the contents of the write cache being flushed to memory
2469 * (and thus being coherent from the CPU).
2470 */
2471 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2472
Jesse Barnes9a289772012-10-26 09:42:42 -07002473 /*
2474 * Bspec vol 1c.3 - blitter engine command streamer:
2475 * "If ENABLED, all TLBs will be invalidated once the flush
2476 * operation is complete. This bit is only valid when the
2477 * Post-Sync Operation field is a value of 1h or 3h."
2478 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01002479 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002480 cmd |= MI_INVALIDATE_TLB;
Chris Wilsonb5321f32016-08-02 22:50:18 +01002481 intel_ring_emit(ring, cmd);
2482 intel_ring_emit(ring,
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002483 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002484 if (INTEL_GEN(req->i915) >= 8) {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002485 intel_ring_emit(ring, 0); /* upper addr */
2486 intel_ring_emit(ring, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002487 } else {
Chris Wilsonb5321f32016-08-02 22:50:18 +01002488 intel_ring_emit(ring, 0);
2489 intel_ring_emit(ring, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002490 }
Chris Wilsonb5321f32016-08-02 22:50:18 +01002491 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002492
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002493 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002494}
2495
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002496static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2497 struct intel_engine_cs *engine)
2498{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002499 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002500 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002501
Chris Wilson39df9192016-07-20 13:31:57 +01002502 if (!i915.semaphores)
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002503 return;
2504
Chris Wilson51d545d2016-08-15 10:49:02 +01002505 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
2506 struct i915_vma *vma;
2507
Chris Wilson91c8a322016-07-05 10:40:23 +01002508 obj = i915_gem_object_create(&dev_priv->drm, 4096);
Chris Wilson51d545d2016-08-15 10:49:02 +01002509 if (IS_ERR(obj))
2510 goto err;
2511
2512 vma = i915_vma_create(obj, &dev_priv->ggtt.base, NULL);
2513 if (IS_ERR(vma))
2514 goto err_obj;
2515
2516 ret = i915_gem_object_set_to_gtt_domain(obj, false);
2517 if (ret)
2518 goto err_obj;
2519
2520 ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
2521 if (ret)
2522 goto err_obj;
2523
2524 dev_priv->semaphore = vma;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002525 }
2526
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002527 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002528 u32 offset = i915_ggtt_offset(dev_priv->semaphore);
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002529
Chris Wilsonad7bdb22016-08-02 22:50:40 +01002530 engine->semaphore.sync_to = gen8_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002531 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002532
2533 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002534 u32 ring_offset;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002535
2536 if (i != engine->id)
2537 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2538 else
2539 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2540
2541 engine->semaphore.signal_ggtt[i] = ring_offset;
2542 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002543 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonad7bdb22016-08-02 22:50:40 +01002544 engine->semaphore.sync_to = gen6_ring_sync_to;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002545 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002546
2547 /*
2548 * The current semaphore is only applied on pre-gen8
2549 * platform. And there is no VCS2 ring on the pre-gen8
2550 * platform. So the semaphore between RCS and VCS2 is
2551 * initialized as INVALID. Gen8 will initialize the
2552 * sema between VCS2 and RCS later.
2553 */
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002554 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002555 static const struct {
2556 u32 wait_mbox;
2557 i915_reg_t mbox_reg;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002558 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
2559 [RCS_HW] = {
2560 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2561 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2562 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002563 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002564 [VCS_HW] = {
2565 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2566 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2567 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002568 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002569 [BCS_HW] = {
2570 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2571 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2572 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002573 },
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002574 [VECS_HW] = {
2575 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2576 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2577 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002578 },
2579 };
2580 u32 wait_mbox;
2581 i915_reg_t mbox_reg;
2582
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002583 if (i == engine->hw_id) {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002584 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2585 mbox_reg = GEN6_NOSYNC;
2586 } else {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +01002587 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
2588 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002589 }
2590
2591 engine->semaphore.mbox.wait[i] = wait_mbox;
2592 engine->semaphore.mbox.signal[i] = mbox_reg;
2593 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002594 }
Chris Wilson51d545d2016-08-15 10:49:02 +01002595
2596 return;
2597
2598err_obj:
2599 i915_gem_object_put(obj);
2600err:
2601 DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
2602 i915.semaphores = 0;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002603}
2604
Chris Wilsoned003072016-07-01 09:18:13 +01002605static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2606 struct intel_engine_cs *engine)
2607{
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002608 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2609
Chris Wilsoned003072016-07-01 09:18:13 +01002610 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002611 engine->irq_enable = gen8_irq_enable;
2612 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002613 engine->irq_seqno_barrier = gen6_seqno_barrier;
2614 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002615 engine->irq_enable = gen6_irq_enable;
2616 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002617 engine->irq_seqno_barrier = gen6_seqno_barrier;
2618 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002619 engine->irq_enable = gen5_irq_enable;
2620 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002621 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002622 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002623 engine->irq_enable = i9xx_irq_enable;
2624 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002625 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002626 engine->irq_enable = i8xx_irq_enable;
2627 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002628 }
2629}
2630
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002631static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2632 struct intel_engine_cs *engine)
2633{
Chris Wilson618e4ca2016-08-02 22:50:35 +01002634 intel_ring_init_irq(dev_priv, engine);
2635 intel_ring_init_semaphores(dev_priv, engine);
2636
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002637 engine->init_hw = init_ring_common;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002638 engine->reset_hw = reset_ring_common;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002639
Chris Wilsonddd66c52016-08-02 22:50:31 +01002640 engine->emit_request = i9xx_emit_request;
Chris Wilson618e4ca2016-08-02 22:50:35 +01002641 if (i915.semaphores)
2642 engine->emit_request = gen6_sema_emit_request;
Chris Wilsonddd66c52016-08-02 22:50:31 +01002643 engine->submit_request = i9xx_submit_request;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002644
2645 if (INTEL_GEN(dev_priv) >= 8)
Chris Wilson803688b2016-08-02 22:50:27 +01002646 engine->emit_bb_start = gen8_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002647 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson803688b2016-08-02 22:50:27 +01002648 engine->emit_bb_start = gen6_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002649 else if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson803688b2016-08-02 22:50:27 +01002650 engine->emit_bb_start = i965_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002651 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002652 engine->emit_bb_start = i830_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002653 else
Chris Wilson803688b2016-08-02 22:50:27 +01002654 engine->emit_bb_start = i915_emit_bb_start;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002655}
2656
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002657int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002658{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002659 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07002660 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002661
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002662 intel_ring_default_vfuncs(dev_priv, engine);
2663
Chris Wilson61ff75a2016-07-01 17:23:28 +01002664 if (HAS_L3_DPF(dev_priv))
2665 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002666
Chris Wilsonc0336662016-05-06 15:40:21 +01002667 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002668 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonddd66c52016-08-02 22:50:31 +01002669 engine->emit_request = gen8_render_emit_request;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002670 engine->emit_flush = gen8_render_ring_flush;
Chris Wilson39df9192016-07-20 13:31:57 +01002671 if (i915.semaphores)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002672 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilsonc0336662016-05-06 15:40:21 +01002673 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002674 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002675 engine->emit_flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002676 if (IS_GEN6(dev_priv))
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002677 engine->emit_flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002678 } else if (IS_GEN5(dev_priv)) {
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002679 engine->emit_flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002680 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002681 if (INTEL_GEN(dev_priv) < 4)
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002682 engine->emit_flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002683 else
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002684 engine->emit_flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002685 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002686 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002687
Chris Wilsonc0336662016-05-06 15:40:21 +01002688 if (IS_HASWELL(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01002689 engine->emit_bb_start = hsw_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002690
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002691 engine->init_hw = init_render_ring;
2692 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002693
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002694 ret = intel_init_ring_buffer(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002695 if (ret)
2696 return ret;
2697
Chris Wilsonf8973c22016-07-01 17:23:21 +01002698 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01002699 ret = intel_engine_create_scratch(engine, 4096);
Chris Wilson7d5ea802016-07-01 17:23:20 +01002700 if (ret)
2701 return ret;
2702 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01002703 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002704 if (ret)
2705 return ret;
2706 }
2707
2708 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002709}
2710
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002711int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002712{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002713 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002714
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002715 intel_ring_default_vfuncs(dev_priv, engine);
2716
Chris Wilsonc0336662016-05-06 15:40:21 +01002717 if (INTEL_GEN(dev_priv) >= 6) {
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002718 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002719 if (IS_GEN6(dev_priv))
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01002720 engine->submit_request = gen6_bsd_submit_request;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002721 engine->emit_flush = gen6_bsd_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002722 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002723 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002724 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002725 engine->mmio_base = BSD_RING_BASE;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002726 engine->emit_flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002727 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002728 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002729 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002730 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002731 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002732
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002733 return intel_init_ring_buffer(engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002734}
Chris Wilson549f7362010-10-19 11:19:32 +01002735
Zhao Yakui845f74a2014-04-17 10:37:37 +08002736/**
Damien Lespiau62659922015-01-29 14:13:40 +00002737 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002738 */
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002739int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002740{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002741 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002742
2743 intel_ring_default_vfuncs(dev_priv, engine);
2744
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002745 engine->emit_flush = gen6_bsd_ring_flush;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002746
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002747 return intel_init_ring_buffer(engine);
Zhao Yakui845f74a2014-04-17 10:37:37 +08002748}
2749
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002750int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
Chris Wilson549f7362010-10-19 11:19:32 +01002751{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002752 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002753
2754 intel_ring_default_vfuncs(dev_priv, engine);
2755
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002756 engine->emit_flush = gen6_ring_flush;
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002757 if (INTEL_GEN(dev_priv) < 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002758 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002759
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002760 return intel_init_ring_buffer(engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002761}
Chris Wilsona7b97612012-07-20 12:41:08 +01002762
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002763int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002764{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01002765 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002766
2767 intel_ring_default_vfuncs(dev_priv, engine);
2768
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01002769 engine->emit_flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002770
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01002771 if (INTEL_GEN(dev_priv) < 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002772 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01002773 engine->irq_enable = hsw_vebox_irq_enable;
2774 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002775 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002776
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01002777 return intel_init_ring_buffer(engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002778}