blob: 9121646d7c4dd98c4c32ed74343948e662059d06 [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
Oscar Mateo82e104c2014-07-24 17:04:26 +010037int __intel_ring_space(int head, int tail, int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010038{
Dave Gordon4f547412014-11-27 11:22:48 +000039 int space = head - tail;
40 if (space <= 0)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010041 space += size;
Dave Gordon4f547412014-11-27 11:22:48 +000042 return space - I915_RING_FREE_SPACE;
Chris Wilson1cf0ba12014-05-05 09:07:33 +010043}
44
Dave Gordonebd0fd42014-11-27 11:22:49 +000045void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
46{
47 if (ringbuf->last_retired_head != -1) {
48 ringbuf->head = ringbuf->last_retired_head;
49 ringbuf->last_retired_head = -1;
50 }
51
52 ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
53 ringbuf->tail, ringbuf->size);
54}
55
Oscar Mateo82e104c2014-07-24 17:04:26 +010056int intel_ring_space(struct intel_ringbuffer *ringbuf)
Chris Wilsonc7dca472011-01-20 17:00:10 +000057{
Dave Gordonebd0fd42014-11-27 11:22:49 +000058 intel_ring_update_space(ringbuf);
59 return ringbuf->space;
Chris Wilsonc7dca472011-01-20 17:00:10 +000060}
61
Oscar Mateo82e104c2014-07-24 17:04:26 +010062bool intel_ring_stopped(struct intel_engine_cs *ring)
Chris Wilson09246732013-08-10 22:16:32 +010063{
64 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020065 return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
66}
Chris Wilson09246732013-08-10 22:16:32 +010067
John Harrison6258fbe2015-05-29 17:43:48 +010068static void __intel_ring_advance(struct intel_engine_cs *ring)
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020069{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010070 struct intel_ringbuffer *ringbuf = ring->buffer;
71 ringbuf->tail &= ringbuf->size - 1;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020072 if (intel_ring_stopped(ring))
Chris Wilson09246732013-08-10 22:16:32 +010073 return;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010074 ring->write_tail(ring, ringbuf->tail);
Chris Wilson09246732013-08-10 22:16:32 +010075}
76
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000077static int
John Harrisona84c3ae2015-05-29 17:43:57 +010078gen2_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010079 u32 invalidate_domains,
80 u32 flush_domains)
81{
John Harrisona84c3ae2015-05-29 17:43:57 +010082 struct intel_engine_cs *ring = req->ring;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010083 u32 cmd;
84 int ret;
85
86 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +020087 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010088 cmd |= MI_NO_WRITE_FLUSH;
89
90 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
91 cmd |= MI_READ_FLUSH;
92
John Harrison5fb9de12015-05-29 17:44:07 +010093 ret = intel_ring_begin(req, 2);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010094 if (ret)
95 return ret;
96
97 intel_ring_emit(ring, cmd);
98 intel_ring_emit(ring, MI_NOOP);
99 intel_ring_advance(ring);
100
101 return 0;
102}
103
104static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100105gen4_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100106 u32 invalidate_domains,
107 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700108{
John Harrisona84c3ae2015-05-29 17:43:57 +0100109 struct intel_engine_cs *ring = req->ring;
Chris Wilson78501ea2010-10-27 12:18:21 +0100110 struct drm_device *dev = ring->dev;
Chris Wilson6f392d52010-08-07 11:01:22 +0100111 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000112 int ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100113
Chris Wilson36d527d2011-03-19 22:26:49 +0000114 /*
115 * read/write caches:
116 *
117 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
118 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
119 * also flushed at 2d versus 3d pipeline switches.
120 *
121 * read-only caches:
122 *
123 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
124 * MI_READ_FLUSH is set, and is always flushed on 965.
125 *
126 * I915_GEM_DOMAIN_COMMAND may not exist?
127 *
128 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
129 * invalidated when MI_EXE_FLUSH is set.
130 *
131 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
132 * invalidated with every MI_FLUSH.
133 *
134 * TLBs:
135 *
136 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
137 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
138 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
139 * are flushed at any MI_FLUSH.
140 */
141
142 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100143 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000144 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000145 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
146 cmd |= MI_EXE_FLUSH;
147
148 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
149 (IS_G4X(dev) || IS_GEN5(dev)))
150 cmd |= MI_INVALIDATE_ISP;
151
John Harrison5fb9de12015-05-29 17:44:07 +0100152 ret = intel_ring_begin(req, 2);
Chris Wilson36d527d2011-03-19 22:26:49 +0000153 if (ret)
154 return ret;
155
156 intel_ring_emit(ring, cmd);
157 intel_ring_emit(ring, MI_NOOP);
158 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000159
160 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800161}
162
Jesse Barnes8d315282011-10-16 10:23:31 +0200163/**
164 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
165 * implementing two workarounds on gen6. From section 1.4.7.1
166 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
167 *
168 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
169 * produced by non-pipelined state commands), software needs to first
170 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
171 * 0.
172 *
173 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
174 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
175 *
176 * And the workaround for these two requires this workaround first:
177 *
178 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
179 * BEFORE the pipe-control with a post-sync op and no write-cache
180 * flushes.
181 *
182 * And this last workaround is tricky because of the requirements on
183 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
184 * volume 2 part 1:
185 *
186 * "1 of the following must also be set:
187 * - Render Target Cache Flush Enable ([12] of DW1)
188 * - Depth Cache Flush Enable ([0] of DW1)
189 * - Stall at Pixel Scoreboard ([1] of DW1)
190 * - Depth Stall ([13] of DW1)
191 * - Post-Sync Operation ([13] of DW1)
192 * - Notify Enable ([8] of DW1)"
193 *
194 * The cache flushes require the workaround flush that triggered this
195 * one, so we can't use it. Depth stall would trigger the same.
196 * Post-sync nonzero is what triggered this second workaround, so we
197 * can't use that one either. Notify enable is IRQs, which aren't
198 * really our business. That leaves only stall at scoreboard.
199 */
200static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100201intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200202{
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100203 struct intel_engine_cs *ring = req->ring;
Chris Wilson18393f62014-04-09 09:19:40 +0100204 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200205 int ret;
206
John Harrison5fb9de12015-05-29 17:44:07 +0100207 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200208 if (ret)
209 return ret;
210
211 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
212 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
213 PIPE_CONTROL_STALL_AT_SCOREBOARD);
214 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
215 intel_ring_emit(ring, 0); /* low dword */
216 intel_ring_emit(ring, 0); /* high dword */
217 intel_ring_emit(ring, MI_NOOP);
218 intel_ring_advance(ring);
219
John Harrison5fb9de12015-05-29 17:44:07 +0100220 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200221 if (ret)
222 return ret;
223
224 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
225 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
226 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
227 intel_ring_emit(ring, 0);
228 intel_ring_emit(ring, 0);
229 intel_ring_emit(ring, MI_NOOP);
230 intel_ring_advance(ring);
231
232 return 0;
233}
234
235static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100236gen6_render_ring_flush(struct drm_i915_gem_request *req,
237 u32 invalidate_domains, u32 flush_domains)
Jesse Barnes8d315282011-10-16 10:23:31 +0200238{
John Harrisona84c3ae2015-05-29 17:43:57 +0100239 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8d315282011-10-16 10:23:31 +0200240 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100241 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200242 int ret;
243
Paulo Zanonib3111502012-08-17 18:35:42 -0300244 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100245 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300246 if (ret)
247 return ret;
248
Jesse Barnes8d315282011-10-16 10:23:31 +0200249 /* Just flush everything. Experiments have shown that reducing the
250 * number of bits based on the write domains has little performance
251 * impact.
252 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100253 if (flush_domains) {
254 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
255 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
256 /*
257 * Ensure that any following seqno writes only happen
258 * when the render cache is indeed flushed.
259 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200260 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100261 }
262 if (invalidate_domains) {
263 flags |= PIPE_CONTROL_TLB_INVALIDATE;
264 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
265 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
266 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
267 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
268 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
269 /*
270 * TLB invalidate requires a post-sync write.
271 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700272 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100273 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200274
John Harrison5fb9de12015-05-29 17:44:07 +0100275 ret = intel_ring_begin(req, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200276 if (ret)
277 return ret;
278
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100279 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
Jesse Barnes8d315282011-10-16 10:23:31 +0200280 intel_ring_emit(ring, flags);
281 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100282 intel_ring_emit(ring, 0);
Jesse Barnes8d315282011-10-16 10:23:31 +0200283 intel_ring_advance(ring);
284
285 return 0;
286}
287
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100288static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100289gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300290{
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100291 struct intel_engine_cs *ring = req->ring;
Paulo Zanonif3987632012-08-17 18:35:43 -0300292 int ret;
293
John Harrison5fb9de12015-05-29 17:44:07 +0100294 ret = intel_ring_begin(req, 4);
Paulo Zanonif3987632012-08-17 18:35:43 -0300295 if (ret)
296 return ret;
297
298 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
299 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
300 PIPE_CONTROL_STALL_AT_SCOREBOARD);
301 intel_ring_emit(ring, 0);
302 intel_ring_emit(ring, 0);
303 intel_ring_advance(ring);
304
305 return 0;
306}
307
308static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100309gen7_render_ring_flush(struct drm_i915_gem_request *req,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300310 u32 invalidate_domains, u32 flush_domains)
311{
John Harrisona84c3ae2015-05-29 17:43:57 +0100312 struct intel_engine_cs *ring = req->ring;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300313 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100314 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300315 int ret;
316
Paulo Zanonif3987632012-08-17 18:35:43 -0300317 /*
318 * Ensure that any following seqno writes only happen when the render
319 * cache is indeed flushed.
320 *
321 * Workaround: 4th PIPE_CONTROL command (except the ones with only
322 * read-cache invalidate bits set) must have the CS_STALL bit set. We
323 * don't try to be clever and just set it unconditionally.
324 */
325 flags |= PIPE_CONTROL_CS_STALL;
326
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300327 /* Just flush everything. Experiments have shown that reducing the
328 * number of bits based on the write domains has little performance
329 * impact.
330 */
331 if (flush_domains) {
332 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
333 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800334 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100335 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300336 }
337 if (invalidate_domains) {
338 flags |= PIPE_CONTROL_TLB_INVALIDATE;
339 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
340 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
341 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
342 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
343 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000344 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300345 /*
346 * TLB invalidate requires a post-sync write.
347 */
348 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200349 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300350
Chris Wilsonadd284a2014-12-16 08:44:32 +0000351 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
352
Paulo Zanonif3987632012-08-17 18:35:43 -0300353 /* Workaround: we must issue a pipe_control with CS-stall bit
354 * set before a pipe_control command that has the state cache
355 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100356 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300357 }
358
John Harrison5fb9de12015-05-29 17:44:07 +0100359 ret = intel_ring_begin(req, 4);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300360 if (ret)
361 return ret;
362
363 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
364 intel_ring_emit(ring, flags);
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200365 intel_ring_emit(ring, scratch_addr);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300366 intel_ring_emit(ring, 0);
367 intel_ring_advance(ring);
368
369 return 0;
370}
371
Ben Widawskya5f3d682013-11-02 21:07:27 -0700372static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100373gen8_emit_pipe_control(struct drm_i915_gem_request *req,
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300374 u32 flags, u32 scratch_addr)
375{
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100376 struct intel_engine_cs *ring = req->ring;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300377 int ret;
378
John Harrison5fb9de12015-05-29 17:44:07 +0100379 ret = intel_ring_begin(req, 6);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300380 if (ret)
381 return ret;
382
383 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
384 intel_ring_emit(ring, flags);
385 intel_ring_emit(ring, scratch_addr);
386 intel_ring_emit(ring, 0);
387 intel_ring_emit(ring, 0);
388 intel_ring_emit(ring, 0);
389 intel_ring_advance(ring);
390
391 return 0;
392}
393
394static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100395gen8_render_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700396 u32 invalidate_domains, u32 flush_domains)
397{
398 u32 flags = 0;
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100399 u32 scratch_addr = req->ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800400 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700401
402 flags |= PIPE_CONTROL_CS_STALL;
403
404 if (flush_domains) {
405 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
406 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800407 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100408 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700409 }
410 if (invalidate_domains) {
411 flags |= PIPE_CONTROL_TLB_INVALIDATE;
412 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
413 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
414 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
415 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
416 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
417 flags |= PIPE_CONTROL_QW_WRITE;
418 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800419
420 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100421 ret = gen8_emit_pipe_control(req,
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800422 PIPE_CONTROL_CS_STALL |
423 PIPE_CONTROL_STALL_AT_SCOREBOARD,
424 0);
425 if (ret)
426 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700427 }
428
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100429 return gen8_emit_pipe_control(req, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700430}
431
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100432static void ring_write_tail(struct intel_engine_cs *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100433 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800434{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300435 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100436 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800437}
438
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100439u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800440{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300441 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson50877442014-03-21 12:41:53 +0000442 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800443
Chris Wilson50877442014-03-21 12:41:53 +0000444 if (INTEL_INFO(ring->dev)->gen >= 8)
445 acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
446 RING_ACTHD_UDW(ring->mmio_base));
447 else if (INTEL_INFO(ring->dev)->gen >= 4)
448 acthd = I915_READ(RING_ACTHD(ring->mmio_base));
449 else
450 acthd = I915_READ(ACTHD);
451
452 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800453}
454
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100455static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200456{
457 struct drm_i915_private *dev_priv = ring->dev->dev_private;
458 u32 addr;
459
460 addr = dev_priv->status_page_dmah->busaddr;
461 if (INTEL_INFO(ring->dev)->gen >= 4)
462 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
463 I915_WRITE(HWS_PGA, addr);
464}
465
Damien Lespiauaf75f262015-02-10 19:32:17 +0000466static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
467{
468 struct drm_device *dev = ring->dev;
469 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200470 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000471
472 /* The ring status page addresses are no longer next to the rest of
473 * the ring registers as of gen7.
474 */
475 if (IS_GEN7(dev)) {
476 switch (ring->id) {
477 case RCS:
478 mmio = RENDER_HWS_PGA_GEN7;
479 break;
480 case BCS:
481 mmio = BLT_HWS_PGA_GEN7;
482 break;
483 /*
484 * VCS2 actually doesn't exist on Gen7. Only shut up
485 * gcc switch check warning
486 */
487 case VCS2:
488 case VCS:
489 mmio = BSD_HWS_PGA_GEN7;
490 break;
491 case VECS:
492 mmio = VEBOX_HWS_PGA_GEN7;
493 break;
494 }
495 } else if (IS_GEN6(ring->dev)) {
496 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
497 } else {
498 /* XXX: gen8 returns to sanity */
499 mmio = RING_HWS_PGA(ring->mmio_base);
500 }
501
502 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
503 POSTING_READ(mmio);
504
505 /*
506 * Flush the TLB for this page
507 *
508 * FIXME: These two bits have disappeared on gen8, so a question
509 * arises: do we still need this and if so how should we go about
510 * invalidating the TLB?
511 */
512 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200513 i915_reg_t reg = RING_INSTPM(ring->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000514
515 /* ring should be idle before issuing a sync flush*/
516 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
517
518 I915_WRITE(reg,
519 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
520 INSTPM_SYNC_FLUSH));
521 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
522 1000))
523 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
524 ring->name);
525 }
526}
527
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100528static bool stop_ring(struct intel_engine_cs *ring)
Chris Wilson9991ae72014-04-02 16:36:07 +0100529{
530 struct drm_i915_private *dev_priv = to_i915(ring->dev);
531
532 if (!IS_GEN2(ring->dev)) {
533 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
Daniel Vetter403bdd12014-08-07 16:05:39 +0200534 if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
535 DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100536 /* Sometimes we observe that the idle flag is not
537 * set even though the ring is empty. So double
538 * check before giving up.
539 */
540 if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
541 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100542 }
543 }
544
545 I915_WRITE_CTL(ring, 0);
546 I915_WRITE_HEAD(ring, 0);
547 ring->write_tail(ring, 0);
548
549 if (!IS_GEN2(ring->dev)) {
550 (void)I915_READ_CTL(ring);
551 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
552 }
553
554 return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
555}
556
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100557static int init_ring_common(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800558{
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200559 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300560 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100561 struct intel_ringbuffer *ringbuf = ring->buffer;
562 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200563 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800564
Mika Kuoppala59bad942015-01-16 11:34:40 +0200565 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200566
Chris Wilson9991ae72014-04-02 16:36:07 +0100567 if (!stop_ring(ring)) {
568 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000569 DRM_DEBUG_KMS("%s head not reset to zero "
570 "ctl %08x head %08x tail %08x start %08x\n",
571 ring->name,
572 I915_READ_CTL(ring),
573 I915_READ_HEAD(ring),
574 I915_READ_TAIL(ring),
575 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800576
Chris Wilson9991ae72014-04-02 16:36:07 +0100577 if (!stop_ring(ring)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000578 DRM_ERROR("failed to set %s head to zero "
579 "ctl %08x head %08x tail %08x start %08x\n",
580 ring->name,
581 I915_READ_CTL(ring),
582 I915_READ_HEAD(ring),
583 I915_READ_TAIL(ring),
584 I915_READ_START(ring));
Chris Wilson9991ae72014-04-02 16:36:07 +0100585 ret = -EIO;
586 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000587 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700588 }
589
Chris Wilson9991ae72014-04-02 16:36:07 +0100590 if (I915_NEED_GFX_HWS(dev))
591 intel_ring_setup_status_page(ring);
592 else
593 ring_setup_phys_status_page(ring);
594
Jiri Kosinaece4a172014-08-07 16:29:53 +0200595 /* Enforce ordering by reading HEAD register back */
596 I915_READ_HEAD(ring);
597
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200598 /* Initialize the ring. This must happen _after_ we've cleared the ring
599 * registers with the above sequence (the readback of the HEAD registers
600 * also enforces ordering), otherwise the hw might lose the new ring
601 * register values. */
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700602 I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100603
604 /* WaClearRingBufHeadRegAtInit:ctg,elk */
605 if (I915_READ_HEAD(ring))
606 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
607 ring->name, I915_READ_HEAD(ring));
608 I915_WRITE_HEAD(ring, 0);
609 (void)I915_READ_HEAD(ring);
610
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200611 I915_WRITE_CTL(ring,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100612 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000613 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800614
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800615 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400616 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700617 I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
Sean Paulf01db982012-03-16 12:43:22 -0400618 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000619 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100620 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
621 ring->name,
622 I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
623 I915_READ_HEAD(ring), I915_READ_TAIL(ring),
624 I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200625 ret = -EIO;
626 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800627 }
628
Dave Gordonebd0fd42014-11-27 11:22:49 +0000629 ringbuf->last_retired_head = -1;
Chris Wilson5c6c6002014-09-06 10:28:27 +0100630 ringbuf->head = I915_READ_HEAD(ring);
631 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Dave Gordonebd0fd42014-11-27 11:22:49 +0000632 intel_ring_update_space(ringbuf);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000633
Chris Wilson50f018d2013-06-10 11:20:19 +0100634 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
635
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200636out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200637 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200638
639 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700640}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800641
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100642void
643intel_fini_pipe_control(struct intel_engine_cs *ring)
644{
645 struct drm_device *dev = ring->dev;
646
647 if (ring->scratch.obj == NULL)
648 return;
649
650 if (INTEL_INFO(dev)->gen >= 5) {
651 kunmap(sg_page(ring->scratch.obj->pages->sgl));
652 i915_gem_object_ggtt_unpin(ring->scratch.obj);
653 }
654
655 drm_gem_object_unreference(&ring->scratch.obj->base);
656 ring->scratch.obj = NULL;
657}
658
659int
660intel_init_pipe_control(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000661{
Chris Wilsonc6df5412010-12-15 09:56:50 +0000662 int ret;
663
Daniel Vetterbfc882b2014-11-20 00:33:08 +0100664 WARN_ON(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000665
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100666 ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
667 if (ring->scratch.obj == NULL) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000668 DRM_ERROR("Failed to allocate seqno page\n");
669 ret = -ENOMEM;
670 goto err;
671 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100672
Daniel Vettera9cc7262014-02-14 14:01:13 +0100673 ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
674 if (ret)
675 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000676
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100677 ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000678 if (ret)
679 goto err_unref;
680
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100681 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
682 ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
683 if (ring->scratch.cpu_page == NULL) {
Wei Yongjun56b085a2013-05-28 17:51:44 +0800684 ret = -ENOMEM;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000685 goto err_unpin;
Wei Yongjun56b085a2013-05-28 17:51:44 +0800686 }
Chris Wilsonc6df5412010-12-15 09:56:50 +0000687
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200688 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100689 ring->name, ring->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000690 return 0;
691
692err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800693 i915_gem_object_ggtt_unpin(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000694err_unref:
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100695 drm_gem_object_unreference(&ring->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000696err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000697 return ret;
698}
699
John Harrisone2be4fa2015-05-29 17:43:54 +0100700static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100701{
Mika Kuoppala72253422014-10-07 17:21:26 +0300702 int ret, i;
John Harrisone2be4fa2015-05-29 17:43:54 +0100703 struct intel_engine_cs *ring = req->ring;
Arun Siluvery888b5992014-08-26 14:44:51 +0100704 struct drm_device *dev = ring->dev;
705 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala72253422014-10-07 17:21:26 +0300706 struct i915_workarounds *w = &dev_priv->workarounds;
Arun Siluvery888b5992014-08-26 14:44:51 +0100707
Francisco Jerez02235802015-10-07 14:44:01 +0300708 if (w->count == 0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300709 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100710
Mika Kuoppala72253422014-10-07 17:21:26 +0300711 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100712 ret = intel_ring_flush_all_caches(req);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100713 if (ret)
714 return ret;
715
John Harrison5fb9de12015-05-29 17:44:07 +0100716 ret = intel_ring_begin(req, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300717 if (ret)
718 return ret;
719
Arun Siluvery22a916a2014-10-22 18:59:52 +0100720 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300721 for (i = 0; i < w->count; i++) {
Ville Syrjäläf92a9162015-11-04 23:20:07 +0200722 intel_ring_emit_reg(ring, w->reg[i].addr);
Mika Kuoppala72253422014-10-07 17:21:26 +0300723 intel_ring_emit(ring, w->reg[i].value);
724 }
Arun Siluvery22a916a2014-10-22 18:59:52 +0100725 intel_ring_emit(ring, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300726
727 intel_ring_advance(ring);
728
729 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100730 ret = intel_ring_flush_all_caches(req);
Mika Kuoppala72253422014-10-07 17:21:26 +0300731 if (ret)
732 return ret;
733
734 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
735
736 return 0;
737}
738
John Harrison87531812015-05-29 17:43:44 +0100739static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100740{
741 int ret;
742
John Harrisone2be4fa2015-05-29 17:43:54 +0100743 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100744 if (ret != 0)
745 return ret;
746
John Harrisonbe013632015-05-29 17:43:45 +0100747 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100748 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000749 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100750
Chris Wilsone26e1b92016-01-29 16:49:05 +0000751 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100752}
753
Mika Kuoppala72253422014-10-07 17:21:26 +0300754static int wa_add(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200755 i915_reg_t addr,
756 const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300757{
758 const u32 idx = dev_priv->workarounds.count;
759
760 if (WARN_ON(idx >= I915_MAX_WA_REGS))
761 return -ENOSPC;
762
763 dev_priv->workarounds.reg[idx].addr = addr;
764 dev_priv->workarounds.reg[idx].value = val;
765 dev_priv->workarounds.reg[idx].mask = mask;
766
767 dev_priv->workarounds.count++;
768
769 return 0;
770}
771
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100772#define WA_REG(addr, mask, val) do { \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000773 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300774 if (r) \
775 return r; \
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100776 } while (0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300777
778#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000779 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300780
781#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000782 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300783
Damien Lespiau98533252014-12-08 17:33:51 +0000784#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000785 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300786
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000787#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
788#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300789
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000790#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300791
Arun Siluvery33136b02016-01-21 21:43:47 +0000792static int wa_ring_whitelist_reg(struct intel_engine_cs *ring, i915_reg_t reg)
793{
794 struct drm_i915_private *dev_priv = ring->dev->dev_private;
795 struct i915_workarounds *wa = &dev_priv->workarounds;
796 const uint32_t index = wa->hw_whitelist_count[ring->id];
797
798 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
799 return -EINVAL;
800
801 WA_WRITE(RING_FORCE_TO_NONPRIV(ring->mmio_base, index),
802 i915_mmio_reg_offset(reg));
803 wa->hw_whitelist_count[ring->id]++;
804
805 return 0;
806}
807
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100808static int gen8_init_workarounds(struct intel_engine_cs *ring)
809{
Arun Siluvery68c61982015-09-25 17:40:38 +0100810 struct drm_device *dev = ring->dev;
811 struct drm_i915_private *dev_priv = dev->dev_private;
812
813 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100814
Arun Siluvery717d84d2015-09-25 17:40:39 +0100815 /* WaDisableAsyncFlipPerfMode:bdw,chv */
816 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
817
Arun Siluveryd0581192015-09-25 17:40:40 +0100818 /* WaDisablePartialInstShootdown:bdw,chv */
819 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
820 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
821
Arun Siluverya340af52015-09-25 17:40:45 +0100822 /* Use Force Non-Coherent whenever executing a 3D context. This is a
823 * workaround for for a possible hang in the unlikely event a TLB
824 * invalidation occurs during a PSD flush.
825 */
826 /* WaForceEnableNonCoherent:bdw,chv */
Arun Siluvery120f5d22015-09-25 17:40:46 +0100827 /* WaHdcDisableFetchWhenMasked:bdw,chv */
Arun Siluverya340af52015-09-25 17:40:45 +0100828 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Arun Siluvery120f5d22015-09-25 17:40:46 +0100829 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Arun Siluverya340af52015-09-25 17:40:45 +0100830 HDC_FORCE_NON_COHERENT);
831
Arun Siluvery6def8fd2015-09-25 17:40:42 +0100832 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
833 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
834 * polygons in the same 8x4 pixel/sample area to be processed without
835 * stalling waiting for the earlier ones to write to Hierarchical Z
836 * buffer."
837 *
838 * This optimization is off by default for BDW and CHV; turn it on.
839 */
840 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
841
Arun Siluvery48404632015-09-25 17:40:43 +0100842 /* Wa4x4STCOptimizationDisable:bdw,chv */
843 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
844
Arun Siluvery7eebcde2015-09-25 17:40:44 +0100845 /*
846 * BSpec recommends 8x4 when MSAA is used,
847 * however in practice 16x4 seems fastest.
848 *
849 * Note that PS/WM thread counts depend on the WIZ hashing
850 * disable bit, which we don't touch here, but it's good
851 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
852 */
853 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
854 GEN6_WIZ_HASHING_MASK,
855 GEN6_WIZ_HASHING_16x4);
856
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100857 return 0;
858}
859
Mika Kuoppala72253422014-10-07 17:21:26 +0300860static int bdw_init_workarounds(struct intel_engine_cs *ring)
861{
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100862 int ret;
Mika Kuoppala72253422014-10-07 17:21:26 +0300863 struct drm_device *dev = ring->dev;
864 struct drm_i915_private *dev_priv = dev->dev_private;
865
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100866 ret = gen8_init_workarounds(ring);
867 if (ret)
868 return ret;
869
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700870 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Arun Siluveryd0581192015-09-25 17:40:40 +0100871 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100872
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700873 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300874 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
875 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100876
Mika Kuoppala72253422014-10-07 17:21:26 +0300877 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
878 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100879
Mika Kuoppala72253422014-10-07 17:21:26 +0300880 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000881 /* WaForceContextSaveRestoreNonCoherent:bdw */
882 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000883 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Mika Kuoppala72253422014-10-07 17:21:26 +0300884 (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100885
Arun Siluvery86d7f232014-08-26 14:44:50 +0100886 return 0;
887}
888
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300889static int chv_init_workarounds(struct intel_engine_cs *ring)
890{
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100891 int ret;
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300892 struct drm_device *dev = ring->dev;
893 struct drm_i915_private *dev_priv = dev->dev_private;
894
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100895 ret = gen8_init_workarounds(ring);
896 if (ret)
897 return ret;
898
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300899 /* WaDisableThreadStallDopClockGating:chv */
Arun Siluveryd0581192015-09-25 17:40:40 +0100900 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300901
Kenneth Graunked60de812015-01-10 18:02:22 -0800902 /* Improve HiZ throughput on CHV. */
903 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
904
Mika Kuoppala72253422014-10-07 17:21:26 +0300905 return 0;
906}
907
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000908static int gen9_init_workarounds(struct intel_engine_cs *ring)
909{
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000910 struct drm_device *dev = ring->dev;
911 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak8ea6f892015-05-19 17:05:42 +0300912 uint32_t tmp;
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000913 int ret;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000914
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300915 /* WaEnableLbsSlaRetryTimerDecrement:skl */
916 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
917 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
918
919 /* WaDisableKillLogic:bxt,skl */
920 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
921 ECOCHK_DIS_TLB);
922
Nick Hoathb0e6f6d2015-05-07 14:15:29 +0100923 /* WaDisablePartialInstShootdown:skl,bxt */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000924 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
925 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
926
Nick Hoatha119a6e2015-05-07 14:15:30 +0100927 /* Syncing dependencies between camera and graphics:skl,bxt */
Nick Hoath84241712015-02-05 10:47:20 +0000928 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
929 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
930
Jani Nikulae87a0052015-10-20 15:22:02 +0300931 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
932 if (IS_SKL_REVID(dev, 0, SKL_REVID_B0) ||
933 IS_BXT_REVID(dev, 0, BXT_REVID_A1))
Damien Lespiaua86eb582015-02-11 18:21:44 +0000934 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
935 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000936
Jani Nikulae87a0052015-10-20 15:22:02 +0300937 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
938 if (IS_SKL_REVID(dev, 0, SKL_REVID_B0) ||
939 IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
Damien Lespiau183c6da2015-02-09 19:33:11 +0000940 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
941 GEN9_RHWO_OPTIMIZATION_DISABLE);
Arun Siluvery9b014352015-07-14 15:01:30 +0100942 /*
943 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
944 * but we do that in per ctx batchbuffer as there is an issue
945 * with this register not getting restored on ctx restore
946 */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000947 }
948
Jani Nikulae87a0052015-10-20 15:22:02 +0300949 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt */
950 if (IS_SKL_REVID(dev, SKL_REVID_C0, REVID_FOREVER) || IS_BROXTON(dev))
Nick Hoathcac23df2015-02-05 10:47:22 +0000951 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
952 GEN9_ENABLE_YV12_BUGFIX);
Nick Hoathcac23df2015-02-05 10:47:22 +0000953
Nick Hoath50683682015-05-07 14:15:35 +0100954 /* Wa4x4STCOptimizationDisable:skl,bxt */
Nick Hoath27160c92015-05-07 14:15:36 +0100955 /* WaDisablePartialResolveInVc:skl,bxt */
Arun Siluvery60294682015-09-25 14:33:37 +0100956 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
957 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
Damien Lespiau9370cd92015-02-09 19:33:17 +0000958
Nick Hoath16be17a2015-05-07 14:15:37 +0100959 /* WaCcsTlbPrefetchDisable:skl,bxt */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000960 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
961 GEN9_CCS_TLB_PREFETCH_ENABLE);
962
Imre Deak5a2ae952015-05-19 15:04:59 +0300963 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
Jani Nikulae87a0052015-10-20 15:22:02 +0300964 if (IS_SKL_REVID(dev, SKL_REVID_C0, SKL_REVID_C0) ||
965 IS_BXT_REVID(dev, 0, BXT_REVID_A1))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200966 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
967 PIXEL_MASK_CAMMING_DISABLE);
968
Imre Deak8ea6f892015-05-19 17:05:42 +0300969 /* WaForceContextSaveRestoreNonCoherent:skl,bxt */
970 tmp = HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT;
Mika Kuoppala510650e2016-04-05 15:56:17 +0300971 if (IS_SKL_REVID(dev, SKL_REVID_F0, REVID_FOREVER) ||
Jani Nikulae87a0052015-10-20 15:22:02 +0300972 IS_BXT_REVID(dev, BXT_REVID_B0, REVID_FOREVER))
Imre Deak8ea6f892015-05-19 17:05:42 +0300973 tmp |= HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE;
974 WA_SET_BIT_MASKED(HDC_CHICKEN0, tmp);
975
Arun Siluvery8c761602015-09-08 10:31:48 +0100976 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt */
Jani Nikulae87a0052015-10-20 15:22:02 +0300977 if (IS_SKYLAKE(dev) || IS_BXT_REVID(dev, 0, BXT_REVID_B0))
Arun Siluvery8c761602015-09-08 10:31:48 +0100978 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
979 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery8c761602015-09-08 10:31:48 +0100980
Robert Beckett6b6d5622015-09-08 10:31:52 +0100981 /* WaDisableSTUnitPowerOptimization:skl,bxt */
982 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
983
Arun Siluvery6ecf56a2016-01-21 21:43:54 +0000984 /* WaOCLCoherentLineFlush:skl,bxt */
985 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
986 GEN8_LQSC_FLUSH_COHERENT_LINES));
987
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000988 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt */
989 ret= wa_ring_whitelist_reg(ring, GEN8_CS_CHICKEN1);
990 if (ret)
991 return ret;
992
Arun Siluvery3669ab62016-01-21 21:43:49 +0000993 /* WaAllowUMDToModifyHDCChicken1:skl,bxt */
994 ret = wa_ring_whitelist_reg(ring, GEN8_HDC_CHICKEN1);
995 if (ret)
996 return ret;
997
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000998 return 0;
999}
1000
Damien Lespiaub7668792015-02-14 18:30:29 +00001001static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
Damien Lespiau8d205492015-02-09 19:33:15 +00001002{
Damien Lespiaub7668792015-02-14 18:30:29 +00001003 struct drm_device *dev = ring->dev;
1004 struct drm_i915_private *dev_priv = dev->dev_private;
1005 u8 vals[3] = { 0, 0, 0 };
1006 unsigned int i;
1007
1008 for (i = 0; i < 3; i++) {
1009 u8 ss;
1010
1011 /*
1012 * Only consider slices where one, and only one, subslice has 7
1013 * EUs
1014 */
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +08001015 if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
Damien Lespiaub7668792015-02-14 18:30:29 +00001016 continue;
1017
1018 /*
1019 * subslice_7eu[i] != 0 (because of the check above) and
1020 * ss_max == 4 (maximum number of subslices possible per slice)
1021 *
1022 * -> 0 <= ss <= 3;
1023 */
1024 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1025 vals[i] = 3 - ss;
1026 }
1027
1028 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1029 return 0;
1030
1031 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1032 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1033 GEN9_IZ_HASHING_MASK(2) |
1034 GEN9_IZ_HASHING_MASK(1) |
1035 GEN9_IZ_HASHING_MASK(0),
1036 GEN9_IZ_HASHING(2, vals[2]) |
1037 GEN9_IZ_HASHING(1, vals[1]) |
1038 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001039
Mika Kuoppala72253422014-10-07 17:21:26 +03001040 return 0;
1041}
1042
Damien Lespiau8d205492015-02-09 19:33:15 +00001043static int skl_init_workarounds(struct intel_engine_cs *ring)
1044{
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001045 int ret;
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001046 struct drm_device *dev = ring->dev;
1047 struct drm_i915_private *dev_priv = dev->dev_private;
1048
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001049 ret = gen9_init_workarounds(ring);
1050 if (ret)
1051 return ret;
Damien Lespiau8d205492015-02-09 19:33:15 +00001052
Arun Siluverya78536e2016-01-21 21:43:53 +00001053 /*
1054 * Actual WA is to disable percontext preemption granularity control
1055 * until D0 which is the default case so this is equivalent to
1056 * !WaDisablePerCtxtPreemptionGranularityControl:skl
1057 */
1058 if (IS_SKL_REVID(dev, SKL_REVID_E0, REVID_FOREVER)) {
1059 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1060 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1061 }
1062
Jani Nikulae87a0052015-10-20 15:22:02 +03001063 if (IS_SKL_REVID(dev, 0, SKL_REVID_D0)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001064 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1065 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1066 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1067 }
1068
1069 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1070 * involving this register should also be added to WA batch as required.
1071 */
Jani Nikulae87a0052015-10-20 15:22:02 +03001072 if (IS_SKL_REVID(dev, 0, SKL_REVID_E0))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001073 /* WaDisableLSQCROPERFforOCL:skl */
1074 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1075 GEN8_LQSC_RO_PERF_DIS);
1076
1077 /* WaEnableGapsTsvCreditFix:skl */
Jani Nikulae87a0052015-10-20 15:22:02 +03001078 if (IS_SKL_REVID(dev, SKL_REVID_C0, REVID_FOREVER)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001079 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1080 GEN9_GAPS_TSV_CREDIT_DISABLE));
1081 }
1082
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001083 /* WaDisablePowerCompilerClockGating:skl */
Jani Nikulae87a0052015-10-20 15:22:02 +03001084 if (IS_SKL_REVID(dev, SKL_REVID_B0, SKL_REVID_B0))
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001085 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1086 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1087
Mika Kuoppala510650e2016-04-05 15:56:17 +03001088 /* This is tied to WaForceContextSaveRestoreNonCoherent */
1089 if (IS_SKL_REVID(dev, 0, REVID_FOREVER)) {
Nick Hoathb62adbd2015-05-07 14:15:34 +01001090 /*
1091 *Use Force Non-Coherent whenever executing a 3D context. This
1092 * is a workaround for a possible hang in the unlikely event
1093 * a TLB invalidation occurs during a PSD flush.
1094 */
1095 /* WaForceEnableNonCoherent:skl */
1096 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1097 HDC_FORCE_NON_COHERENT);
Mika Kuoppalae2386592015-12-18 16:14:53 +02001098
1099 /* WaDisableHDCInvalidation:skl */
1100 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
1101 BDW_DISABLE_HDC_INVALIDATION);
Nick Hoathb62adbd2015-05-07 14:15:34 +01001102 }
1103
Jani Nikulae87a0052015-10-20 15:22:02 +03001104 /* WaBarrierPerformanceFixDisable:skl */
1105 if (IS_SKL_REVID(dev, SKL_REVID_C0, SKL_REVID_D0))
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001106 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1107 HDC_FENCE_DEST_SLM_DISABLE |
1108 HDC_BARRIER_PERFORMANCE_DISABLE);
1109
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001110 /* WaDisableSbeCacheDispatchPortSharing:skl */
Jani Nikulae87a0052015-10-20 15:22:02 +03001111 if (IS_SKL_REVID(dev, 0, SKL_REVID_F0))
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001112 WA_SET_BIT_MASKED(
1113 GEN7_HALF_SLICE_CHICKEN1,
1114 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001115
Arun Siluvery61074972016-01-21 21:43:52 +00001116 /* WaDisableLSQCROPERFforOCL:skl */
1117 ret = wa_ring_whitelist_reg(ring, GEN8_L3SQCREG4);
1118 if (ret)
1119 return ret;
1120
Damien Lespiaub7668792015-02-14 18:30:29 +00001121 return skl_tune_iz_hashing(ring);
Damien Lespiau8d205492015-02-09 19:33:15 +00001122}
1123
Nick Hoathcae04372015-03-17 11:39:38 +02001124static int bxt_init_workarounds(struct intel_engine_cs *ring)
1125{
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001126 int ret;
Nick Hoathdfb601e2015-04-10 13:12:24 +01001127 struct drm_device *dev = ring->dev;
1128 struct drm_i915_private *dev_priv = dev->dev_private;
1129
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001130 ret = gen9_init_workarounds(ring);
1131 if (ret)
1132 return ret;
Nick Hoathcae04372015-03-17 11:39:38 +02001133
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001134 /* WaStoreMultiplePTEenable:bxt */
1135 /* This is a requirement according to Hardware specification */
Tim Gorecbdc12a2015-10-26 10:48:58 +00001136 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001137 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1138
1139 /* WaSetClckGatingDisableMedia:bxt */
Tim Gorecbdc12a2015-10-26 10:48:58 +00001140 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001141 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1142 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1143 }
1144
Nick Hoathdfb601e2015-04-10 13:12:24 +01001145 /* WaDisableThreadStallDopClockGating:bxt */
1146 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1147 STALL_DOP_GATING_DISABLE);
1148
Nick Hoath983b4b92015-04-10 13:12:25 +01001149 /* WaDisableSbeCacheDispatchPortSharing:bxt */
Jani Nikulae87a0052015-10-20 15:22:02 +03001150 if (IS_BXT_REVID(dev, 0, BXT_REVID_B0)) {
Nick Hoath983b4b92015-04-10 13:12:25 +01001151 WA_SET_BIT_MASKED(
1152 GEN7_HALF_SLICE_CHICKEN1,
1153 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1154 }
1155
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001156 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1157 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1158 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
Arun Siluverya786d532016-01-21 21:43:51 +00001159 /* WaDisableLSQCROPERFforOCL:bxt */
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001160 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
1161 ret = wa_ring_whitelist_reg(ring, GEN9_CS_DEBUG_MODE1);
1162 if (ret)
1163 return ret;
Arun Siluverya786d532016-01-21 21:43:51 +00001164
1165 ret = wa_ring_whitelist_reg(ring, GEN8_L3SQCREG4);
1166 if (ret)
1167 return ret;
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001168 }
1169
Nick Hoathcae04372015-03-17 11:39:38 +02001170 return 0;
1171}
1172
Michel Thierry771b9a52014-11-11 16:47:33 +00001173int init_workarounds_ring(struct intel_engine_cs *ring)
Mika Kuoppala72253422014-10-07 17:21:26 +03001174{
1175 struct drm_device *dev = ring->dev;
1176 struct drm_i915_private *dev_priv = dev->dev_private;
1177
1178 WARN_ON(ring->id != RCS);
1179
1180 dev_priv->workarounds.count = 0;
Arun Siluvery33136b02016-01-21 21:43:47 +00001181 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
Mika Kuoppala72253422014-10-07 17:21:26 +03001182
1183 if (IS_BROADWELL(dev))
1184 return bdw_init_workarounds(ring);
1185
1186 if (IS_CHERRYVIEW(dev))
1187 return chv_init_workarounds(ring);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001188
Damien Lespiau8d205492015-02-09 19:33:15 +00001189 if (IS_SKYLAKE(dev))
1190 return skl_init_workarounds(ring);
Nick Hoathcae04372015-03-17 11:39:38 +02001191
1192 if (IS_BROXTON(dev))
1193 return bxt_init_workarounds(ring);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001194
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001195 return 0;
1196}
1197
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001198static int init_render_ring(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001199{
Chris Wilson78501ea2010-10-27 12:18:21 +01001200 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001201 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001202 int ret = init_ring_common(ring);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001203 if (ret)
1204 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001205
Akash Goel61a563a2014-03-25 18:01:50 +05301206 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1207 if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001208 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001209
1210 /* We need to disable the AsyncFlip performance optimisations in order
1211 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1212 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001213 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001214 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001215 */
Ville Syrjälä2441f872015-06-02 15:37:37 +03001216 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001217 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1218
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001219 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301220 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001221 if (INTEL_INFO(dev)->gen == 6)
1222 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001223 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001224
Akash Goel01fa0302014-03-24 23:00:04 +05301225 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001226 if (IS_GEN7(dev))
1227 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301228 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001229 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001230
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001231 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001232 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1233 * "If this bit is set, STCunit will have LRA as replacement
1234 * policy. [...] This bit must be reset. LRA replacement
1235 * policy is not supported."
1236 */
1237 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001238 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001239 }
1240
Ville Syrjälä9cc83022015-06-02 15:37:36 +03001241 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001242 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001243
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001244 if (HAS_L3_DPF(dev))
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001245 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001246
Mika Kuoppala72253422014-10-07 17:21:26 +03001247 return init_workarounds_ring(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001248}
1249
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001250static void render_ring_cleanup(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001251{
Daniel Vetterb45305f2012-12-17 16:21:27 +01001252 struct drm_device *dev = ring->dev;
Ben Widawsky3e789982014-06-30 09:53:37 -07001253 struct drm_i915_private *dev_priv = dev->dev_private;
1254
1255 if (dev_priv->semaphore_obj) {
1256 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1257 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1258 dev_priv->semaphore_obj = NULL;
1259 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001260
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001261 intel_fini_pipe_control(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001262}
1263
John Harrisonf7169682015-05-29 17:44:05 +01001264static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001265 unsigned int num_dwords)
1266{
1267#define MBOX_UPDATE_DWORDS 8
John Harrisonf7169682015-05-29 17:44:05 +01001268 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky3e789982014-06-30 09:53:37 -07001269 struct drm_device *dev = signaller->dev;
1270 struct drm_i915_private *dev_priv = dev->dev_private;
1271 struct intel_engine_cs *waiter;
1272 int i, ret, num_rings;
1273
1274 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1275 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1276#undef MBOX_UPDATE_DWORDS
1277
John Harrison5fb9de12015-05-29 17:44:07 +01001278 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001279 if (ret)
1280 return ret;
1281
1282 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001283 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001284 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1285 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1286 continue;
1287
John Harrisonf7169682015-05-29 17:44:05 +01001288 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky3e789982014-06-30 09:53:37 -07001289 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1290 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1291 PIPE_CONTROL_QW_WRITE |
1292 PIPE_CONTROL_FLUSH_ENABLE);
1293 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1294 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001295 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001296 intel_ring_emit(signaller, 0);
1297 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1298 MI_SEMAPHORE_TARGET(waiter->id));
1299 intel_ring_emit(signaller, 0);
1300 }
1301
1302 return 0;
1303}
1304
John Harrisonf7169682015-05-29 17:44:05 +01001305static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001306 unsigned int num_dwords)
1307{
1308#define MBOX_UPDATE_DWORDS 6
John Harrisonf7169682015-05-29 17:44:05 +01001309 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky3e789982014-06-30 09:53:37 -07001310 struct drm_device *dev = signaller->dev;
1311 struct drm_i915_private *dev_priv = dev->dev_private;
1312 struct intel_engine_cs *waiter;
1313 int i, ret, num_rings;
1314
1315 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1316 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1317#undef MBOX_UPDATE_DWORDS
1318
John Harrison5fb9de12015-05-29 17:44:07 +01001319 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001320 if (ret)
1321 return ret;
1322
1323 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001324 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001325 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1326 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1327 continue;
1328
John Harrisonf7169682015-05-29 17:44:05 +01001329 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky3e789982014-06-30 09:53:37 -07001330 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1331 MI_FLUSH_DW_OP_STOREDW);
1332 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1333 MI_FLUSH_DW_USE_GTT);
1334 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001335 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001336 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1337 MI_SEMAPHORE_TARGET(waiter->id));
1338 intel_ring_emit(signaller, 0);
1339 }
1340
1341 return 0;
1342}
1343
John Harrisonf7169682015-05-29 17:44:05 +01001344static int gen6_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001345 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001346{
John Harrisonf7169682015-05-29 17:44:05 +01001347 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001348 struct drm_device *dev = signaller->dev;
1349 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001350 struct intel_engine_cs *useless;
Ben Widawskya1444b72014-06-30 09:53:35 -07001351 int i, ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001352
Ben Widawskya1444b72014-06-30 09:53:35 -07001353#define MBOX_UPDATE_DWORDS 3
1354 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1355 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1356#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001357
John Harrison5fb9de12015-05-29 17:44:07 +01001358 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky024a43e2014-04-29 14:52:30 -07001359 if (ret)
1360 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001361
Ben Widawsky78325f22014-04-29 14:52:29 -07001362 for_each_ring(useless, dev_priv, i) {
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001363 i915_reg_t mbox_reg = signaller->semaphore.mbox.signal[i];
1364
1365 if (i915_mmio_reg_valid(mbox_reg)) {
John Harrisonf7169682015-05-29 17:44:05 +01001366 u32 seqno = i915_gem_request_get_seqno(signaller_req);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001367
Ben Widawsky78325f22014-04-29 14:52:29 -07001368 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001369 intel_ring_emit_reg(signaller, mbox_reg);
John Harrison6259cea2014-11-24 18:49:29 +00001370 intel_ring_emit(signaller, seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001371 }
1372 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001373
Ben Widawskya1444b72014-06-30 09:53:35 -07001374 /* If num_dwords was rounded, make sure the tail pointer is correct */
1375 if (num_rings % 2 == 0)
1376 intel_ring_emit(signaller, MI_NOOP);
1377
Ben Widawsky024a43e2014-04-29 14:52:30 -07001378 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001379}
1380
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001381/**
1382 * gen6_add_request - Update the semaphore mailbox registers
John Harrisonee044a82015-05-29 17:44:00 +01001383 *
1384 * @request - request to write to the ring
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001385 *
1386 * Update the mailbox registers in the *other* rings with the current seqno.
1387 * This acts like a signal in the canonical semaphore.
1388 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001389static int
John Harrisonee044a82015-05-29 17:44:00 +01001390gen6_add_request(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001391{
John Harrisonee044a82015-05-29 17:44:00 +01001392 struct intel_engine_cs *ring = req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001393 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001394
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001395 if (ring->semaphore.signal)
John Harrisonf7169682015-05-29 17:44:05 +01001396 ret = ring->semaphore.signal(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001397 else
John Harrison5fb9de12015-05-29 17:44:07 +01001398 ret = intel_ring_begin(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001399
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001400 if (ret)
1401 return ret;
1402
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001403 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1404 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrisonee044a82015-05-29 17:44:00 +01001405 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001406 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001407 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001408
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001409 return 0;
1410}
1411
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001412static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1413 u32 seqno)
1414{
1415 struct drm_i915_private *dev_priv = dev->dev_private;
1416 return dev_priv->last_seqno < seqno;
1417}
1418
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001419/**
1420 * intel_ring_sync - sync the waiter to the signaller on seqno
1421 *
1422 * @waiter - ring that is waiting
1423 * @signaller - ring which has, or will signal
1424 * @seqno - seqno which the waiter will block on
1425 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001426
1427static int
John Harrison599d9242015-05-29 17:44:04 +01001428gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001429 struct intel_engine_cs *signaller,
1430 u32 seqno)
1431{
John Harrison599d9242015-05-29 17:44:04 +01001432 struct intel_engine_cs *waiter = waiter_req->ring;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001433 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
1434 int ret;
1435
John Harrison5fb9de12015-05-29 17:44:07 +01001436 ret = intel_ring_begin(waiter_req, 4);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001437 if (ret)
1438 return ret;
1439
1440 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1441 MI_SEMAPHORE_GLOBAL_GTT |
Ben Widawskybae4fcd2014-06-30 09:53:43 -07001442 MI_SEMAPHORE_POLL |
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001443 MI_SEMAPHORE_SAD_GTE_SDD);
1444 intel_ring_emit(waiter, seqno);
1445 intel_ring_emit(waiter,
1446 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1447 intel_ring_emit(waiter,
1448 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1449 intel_ring_advance(waiter);
1450 return 0;
1451}
1452
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001453static int
John Harrison599d9242015-05-29 17:44:04 +01001454gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001455 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001456 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001457{
John Harrison599d9242015-05-29 17:44:04 +01001458 struct intel_engine_cs *waiter = waiter_req->ring;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001459 u32 dw1 = MI_SEMAPHORE_MBOX |
1460 MI_SEMAPHORE_COMPARE |
1461 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001462 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1463 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001464
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001465 /* Throughout all of the GEM code, seqno passed implies our current
1466 * seqno is >= the last seqno executed. However for hardware the
1467 * comparison is strictly greater than.
1468 */
1469 seqno -= 1;
1470
Ben Widawskyebc348b2014-04-29 14:52:28 -07001471 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001472
John Harrison5fb9de12015-05-29 17:44:07 +01001473 ret = intel_ring_begin(waiter_req, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001474 if (ret)
1475 return ret;
1476
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001477 /* If seqno wrap happened, omit the wait with no-ops */
1478 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001479 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001480 intel_ring_emit(waiter, seqno);
1481 intel_ring_emit(waiter, 0);
1482 intel_ring_emit(waiter, MI_NOOP);
1483 } else {
1484 intel_ring_emit(waiter, MI_NOOP);
1485 intel_ring_emit(waiter, MI_NOOP);
1486 intel_ring_emit(waiter, MI_NOOP);
1487 intel_ring_emit(waiter, MI_NOOP);
1488 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001489 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001490
1491 return 0;
1492}
1493
Chris Wilsonc6df5412010-12-15 09:56:50 +00001494#define PIPE_CONTROL_FLUSH(ring__, addr__) \
1495do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001496 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1497 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +00001498 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1499 intel_ring_emit(ring__, 0); \
1500 intel_ring_emit(ring__, 0); \
1501} while (0)
1502
1503static int
John Harrisonee044a82015-05-29 17:44:00 +01001504pc_render_add_request(struct drm_i915_gem_request *req)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001505{
John Harrisonee044a82015-05-29 17:44:00 +01001506 struct intel_engine_cs *ring = req->ring;
Chris Wilson18393f62014-04-09 09:19:40 +01001507 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001508 int ret;
1509
1510 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1511 * incoherent with writes to memory, i.e. completely fubar,
1512 * so we need to use PIPE_NOTIFY instead.
1513 *
1514 * However, we also need to workaround the qword write
1515 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1516 * memory before requesting an interrupt.
1517 */
John Harrison5fb9de12015-05-29 17:44:07 +01001518 ret = intel_ring_begin(req, 32);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001519 if (ret)
1520 return ret;
1521
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001522 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001523 PIPE_CONTROL_WRITE_FLUSH |
1524 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001525 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrisonee044a82015-05-29 17:44:00 +01001526 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001527 intel_ring_emit(ring, 0);
1528 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001529 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +00001530 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001531 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001532 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001533 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001534 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001535 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001536 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001537 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001538 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001539
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001540 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001541 PIPE_CONTROL_WRITE_FLUSH |
1542 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001543 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001544 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrisonee044a82015-05-29 17:44:00 +01001545 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001546 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +01001547 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001548
Chris Wilsonc6df5412010-12-15 09:56:50 +00001549 return 0;
1550}
1551
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001552static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001553gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001554{
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001555 /* Workaround to force correct ordering between irq and seqno writes on
1556 * ivb (and maybe also on snb) by reading from a CS register (like
1557 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +00001558 if (!lazy_coherency) {
1559 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1560 POSTING_READ(RING_ACTHD(ring->mmio_base));
1561 }
1562
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001563 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1564}
1565
1566static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001567ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001568{
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001569 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1570}
1571
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001572static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001573ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001574{
1575 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1576}
1577
Chris Wilsonc6df5412010-12-15 09:56:50 +00001578static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001579pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001580{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001581 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001582}
1583
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001584static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001585pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001586{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001587 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001588}
1589
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001590static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001591gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001592{
1593 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001594 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001595 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001596
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001597 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Daniel Vettere48d8632012-04-11 22:12:54 +02001598 return false;
1599
Chris Wilson7338aef2012-04-24 21:48:47 +01001600 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001601 if (ring->irq_refcount++ == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001602 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001603 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001604
1605 return true;
1606}
1607
1608static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001609gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001610{
1611 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001612 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001613 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001614
Chris Wilson7338aef2012-04-24 21:48:47 +01001615 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001616 if (--ring->irq_refcount == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001617 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001618 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001619}
1620
1621static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001622i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001623{
Chris Wilson78501ea2010-10-27 12:18:21 +01001624 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001625 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001626 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001627
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001628 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001629 return false;
1630
Chris Wilson7338aef2012-04-24 21:48:47 +01001631 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001632 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001633 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1634 I915_WRITE(IMR, dev_priv->irq_mask);
1635 POSTING_READ(IMR);
1636 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001637 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001638
1639 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001640}
1641
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001642static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001643i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001644{
Chris Wilson78501ea2010-10-27 12:18:21 +01001645 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001646 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001647 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001648
Chris Wilson7338aef2012-04-24 21:48:47 +01001649 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001650 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001651 dev_priv->irq_mask |= ring->irq_enable_mask;
1652 I915_WRITE(IMR, dev_priv->irq_mask);
1653 POSTING_READ(IMR);
1654 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001655 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001656}
1657
Chris Wilsonc2798b12012-04-22 21:13:57 +01001658static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001659i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001660{
1661 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001662 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001663 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001664
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001665 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonc2798b12012-04-22 21:13:57 +01001666 return false;
1667
Chris Wilson7338aef2012-04-24 21:48:47 +01001668 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001669 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001670 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1671 I915_WRITE16(IMR, dev_priv->irq_mask);
1672 POSTING_READ16(IMR);
1673 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001674 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001675
1676 return true;
1677}
1678
1679static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001680i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001681{
1682 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001683 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001684 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001685
Chris Wilson7338aef2012-04-24 21:48:47 +01001686 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001687 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001688 dev_priv->irq_mask |= ring->irq_enable_mask;
1689 I915_WRITE16(IMR, dev_priv->irq_mask);
1690 POSTING_READ16(IMR);
1691 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001692 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001693}
1694
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001695static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001696bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001697 u32 invalidate_domains,
1698 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001699{
John Harrisona84c3ae2015-05-29 17:43:57 +01001700 struct intel_engine_cs *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001701 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001702
John Harrison5fb9de12015-05-29 17:44:07 +01001703 ret = intel_ring_begin(req, 2);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001704 if (ret)
1705 return ret;
1706
1707 intel_ring_emit(ring, MI_FLUSH);
1708 intel_ring_emit(ring, MI_NOOP);
1709 intel_ring_advance(ring);
1710 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001711}
1712
Chris Wilson3cce4692010-10-27 16:11:02 +01001713static int
John Harrisonee044a82015-05-29 17:44:00 +01001714i9xx_add_request(struct drm_i915_gem_request *req)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001715{
John Harrisonee044a82015-05-29 17:44:00 +01001716 struct intel_engine_cs *ring = req->ring;
Chris Wilson3cce4692010-10-27 16:11:02 +01001717 int ret;
1718
John Harrison5fb9de12015-05-29 17:44:07 +01001719 ret = intel_ring_begin(req, 4);
Chris Wilson3cce4692010-10-27 16:11:02 +01001720 if (ret)
1721 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +01001722
Chris Wilson3cce4692010-10-27 16:11:02 +01001723 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1724 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrisonee044a82015-05-29 17:44:00 +01001725 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilson3cce4692010-10-27 16:11:02 +01001726 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001727 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001728
Chris Wilson3cce4692010-10-27 16:11:02 +01001729 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001730}
1731
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001732static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001733gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001734{
1735 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001736 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001737 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001738
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001739 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1740 return false;
Chris Wilson0f468322011-01-04 17:35:21 +00001741
Chris Wilson7338aef2012-04-24 21:48:47 +01001742 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001743 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001744 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001745 I915_WRITE_IMR(ring,
1746 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001747 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001748 else
1749 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001750 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001751 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001752 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001753
1754 return true;
1755}
1756
1757static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001758gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001759{
1760 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001761 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001762 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001763
Chris Wilson7338aef2012-04-24 21:48:47 +01001764 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001765 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001766 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001767 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001768 else
1769 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001770 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001771 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001772 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001773}
1774
Ben Widawskya19d2932013-05-28 19:22:30 -07001775static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001776hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001777{
1778 struct drm_device *dev = ring->dev;
1779 struct drm_i915_private *dev_priv = dev->dev_private;
1780 unsigned long flags;
1781
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001782 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskya19d2932013-05-28 19:22:30 -07001783 return false;
1784
Daniel Vetter59cdb632013-07-04 23:35:28 +02001785 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001786 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001787 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001788 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001789 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001790 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001791
1792 return true;
1793}
1794
1795static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001796hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001797{
1798 struct drm_device *dev = ring->dev;
1799 struct drm_i915_private *dev_priv = dev->dev_private;
1800 unsigned long flags;
1801
Daniel Vetter59cdb632013-07-04 23:35:28 +02001802 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001803 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001804 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001805 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001806 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001807 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001808}
1809
Ben Widawskyabd58f02013-11-02 21:07:09 -07001810static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001811gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001812{
1813 struct drm_device *dev = ring->dev;
1814 struct drm_i915_private *dev_priv = dev->dev_private;
1815 unsigned long flags;
1816
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001817 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskyabd58f02013-11-02 21:07:09 -07001818 return false;
1819
1820 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1821 if (ring->irq_refcount++ == 0) {
1822 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1823 I915_WRITE_IMR(ring,
1824 ~(ring->irq_enable_mask |
1825 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1826 } else {
1827 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1828 }
1829 POSTING_READ(RING_IMR(ring->mmio_base));
1830 }
1831 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1832
1833 return true;
1834}
1835
1836static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001837gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001838{
1839 struct drm_device *dev = ring->dev;
1840 struct drm_i915_private *dev_priv = dev->dev_private;
1841 unsigned long flags;
1842
1843 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1844 if (--ring->irq_refcount == 0) {
1845 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1846 I915_WRITE_IMR(ring,
1847 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1848 } else {
1849 I915_WRITE_IMR(ring, ~0);
1850 }
1851 POSTING_READ(RING_IMR(ring->mmio_base));
1852 }
1853 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1854}
1855
Zou Nan haid1b851f2010-05-21 09:08:57 +08001856static int
John Harrison53fddaf2015-05-29 17:44:02 +01001857i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001858 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001859 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001860{
John Harrison53fddaf2015-05-29 17:44:02 +01001861 struct intel_engine_cs *ring = req->ring;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001862 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001863
John Harrison5fb9de12015-05-29 17:44:07 +01001864 ret = intel_ring_begin(req, 2);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001865 if (ret)
1866 return ret;
1867
Chris Wilson78501ea2010-10-27 12:18:21 +01001868 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001869 MI_BATCH_BUFFER_START |
1870 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001871 (dispatch_flags & I915_DISPATCH_SECURE ?
1872 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001873 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001874 intel_ring_advance(ring);
1875
Zou Nan haid1b851f2010-05-21 09:08:57 +08001876 return 0;
1877}
1878
Daniel Vetterb45305f2012-12-17 16:21:27 +01001879/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1880#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001881#define I830_TLB_ENTRIES (2)
1882#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001883static int
John Harrison53fddaf2015-05-29 17:44:02 +01001884i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001885 u64 offset, u32 len,
1886 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001887{
John Harrison53fddaf2015-05-29 17:44:02 +01001888 struct intel_engine_cs *ring = req->ring;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001889 u32 cs_offset = ring->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001890 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001891
John Harrison5fb9de12015-05-29 17:44:07 +01001892 ret = intel_ring_begin(req, 6);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001893 if (ret)
1894 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001895
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001896 /* Evict the invalid PTE TLBs */
1897 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1898 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1899 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1900 intel_ring_emit(ring, cs_offset);
1901 intel_ring_emit(ring, 0xdeadbeef);
1902 intel_ring_emit(ring, MI_NOOP);
1903 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001904
John Harrison8e004ef2015-02-13 11:48:10 +00001905 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001906 if (len > I830_BATCH_LIMIT)
1907 return -ENOSPC;
1908
John Harrison5fb9de12015-05-29 17:44:07 +01001909 ret = intel_ring_begin(req, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001910 if (ret)
1911 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001912
1913 /* Blit the batch (which has now all relocs applied) to the
1914 * stable batch scratch bo area (so that the CS never
1915 * stumbles over its tlb invalidation bug) ...
1916 */
1917 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1918 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilson611a7a42014-09-12 07:37:42 +01001919 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001920 intel_ring_emit(ring, cs_offset);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001921 intel_ring_emit(ring, 4096);
1922 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001923
Daniel Vetterb45305f2012-12-17 16:21:27 +01001924 intel_ring_emit(ring, MI_FLUSH);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001925 intel_ring_emit(ring, MI_NOOP);
1926 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001927
1928 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001929 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001930 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001931
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001932 ret = intel_ring_begin(req, 2);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001933 if (ret)
1934 return ret;
1935
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001936 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
John Harrison8e004ef2015-02-13 11:48:10 +00001937 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1938 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001939 intel_ring_advance(ring);
1940
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001941 return 0;
1942}
1943
1944static int
John Harrison53fddaf2015-05-29 17:44:02 +01001945i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001946 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001947 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001948{
John Harrison53fddaf2015-05-29 17:44:02 +01001949 struct intel_engine_cs *ring = req->ring;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001950 int ret;
1951
John Harrison5fb9de12015-05-29 17:44:07 +01001952 ret = intel_ring_begin(req, 2);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001953 if (ret)
1954 return ret;
1955
Chris Wilson65f56872012-04-17 16:38:12 +01001956 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
John Harrison8e004ef2015-02-13 11:48:10 +00001957 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1958 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001959 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001960
Eric Anholt62fdfea2010-05-21 13:26:39 -07001961 return 0;
1962}
1963
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001964static void cleanup_phys_status_page(struct intel_engine_cs *ring)
1965{
1966 struct drm_i915_private *dev_priv = to_i915(ring->dev);
1967
1968 if (!dev_priv->status_page_dmah)
1969 return;
1970
1971 drm_pci_free(ring->dev, dev_priv->status_page_dmah);
1972 ring->status_page.page_addr = NULL;
1973}
1974
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001975static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001976{
Chris Wilson05394f32010-11-08 19:18:58 +00001977 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001978
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001979 obj = ring->status_page.obj;
1980 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001981 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001982
Chris Wilson9da3da62012-06-01 15:20:22 +01001983 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001984 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001985 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001986 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001987}
1988
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001989static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001990{
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001991 struct drm_i915_gem_object *obj = ring->status_page.obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001992
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001993 if (obj == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001994 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001995 int ret;
1996
1997 obj = i915_gem_alloc_object(ring->dev, 4096);
1998 if (obj == NULL) {
1999 DRM_ERROR("Failed to allocate status page\n");
2000 return -ENOMEM;
2001 }
2002
2003 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2004 if (ret)
2005 goto err_unref;
2006
Chris Wilson1f767e02014-07-03 17:33:03 -04002007 flags = 0;
2008 if (!HAS_LLC(ring->dev))
2009 /* On g33, we cannot place HWS above 256MiB, so
2010 * restrict its pinning to the low mappable arena.
2011 * Though this restriction is not documented for
2012 * gen4, gen5, or byt, they also behave similarly
2013 * and hang if the HWS is placed at the top of the
2014 * GTT. To generalise, it appears that all !llc
2015 * platforms have issues with us placing the HWS
2016 * above the mappable region (even though we never
2017 * actualy map it).
2018 */
2019 flags |= PIN_MAPPABLE;
2020 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01002021 if (ret) {
2022err_unref:
2023 drm_gem_object_unreference(&obj->base);
2024 return ret;
2025 }
2026
2027 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002028 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01002029
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002030 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01002031 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002032 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002033
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002034 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
2035 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002036
2037 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002038}
2039
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002040static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00002041{
2042 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002043
2044 if (!dev_priv->status_page_dmah) {
2045 dev_priv->status_page_dmah =
2046 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
2047 if (!dev_priv->status_page_dmah)
2048 return -ENOMEM;
2049 }
2050
Chris Wilson6b8294a2012-11-16 11:43:20 +00002051 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
2052 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
2053
2054 return 0;
2055}
2056
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002057void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
2058{
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002059 if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
2060 vunmap(ringbuf->virtual_start);
2061 else
2062 iounmap(ringbuf->virtual_start);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002063 ringbuf->virtual_start = NULL;
Tvrtko Ursulin0eb973d2016-01-15 15:10:28 +00002064 ringbuf->vma = NULL;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002065 i915_gem_object_ggtt_unpin(ringbuf->obj);
2066}
2067
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002068static u32 *vmap_obj(struct drm_i915_gem_object *obj)
2069{
2070 struct sg_page_iter sg_iter;
2071 struct page **pages;
2072 void *addr;
2073 int i;
2074
2075 pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
2076 if (pages == NULL)
2077 return NULL;
2078
2079 i = 0;
2080 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0)
2081 pages[i++] = sg_page_iter_page(&sg_iter);
2082
2083 addr = vmap(pages, i, 0, PAGE_KERNEL);
2084 drm_free_large(pages);
2085
2086 return addr;
2087}
2088
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002089int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
2090 struct intel_ringbuffer *ringbuf)
2091{
2092 struct drm_i915_private *dev_priv = to_i915(dev);
2093 struct drm_i915_gem_object *obj = ringbuf->obj;
Chris Wilson1b3e8852016-04-13 17:35:11 +01002094 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
2095 unsigned flags = PIN_OFFSET_BIAS | 4096;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002096 int ret;
2097
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002098 if (HAS_LLC(dev_priv) && !obj->stolen) {
Chris Wilson1b3e8852016-04-13 17:35:11 +01002099 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002100 if (ret)
2101 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002102
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002103 ret = i915_gem_object_set_to_cpu_domain(obj, true);
2104 if (ret) {
2105 i915_gem_object_ggtt_unpin(obj);
2106 return ret;
2107 }
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002108
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002109 ringbuf->virtual_start = vmap_obj(obj);
2110 if (ringbuf->virtual_start == NULL) {
2111 i915_gem_object_ggtt_unpin(obj);
2112 return -ENOMEM;
2113 }
2114 } else {
Chris Wilson1b3e8852016-04-13 17:35:11 +01002115 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
2116 flags | PIN_MAPPABLE);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002117 if (ret)
2118 return ret;
2119
2120 ret = i915_gem_object_set_to_gtt_domain(obj, true);
2121 if (ret) {
2122 i915_gem_object_ggtt_unpin(obj);
2123 return ret;
2124 }
2125
Daniele Ceraolo Spurioff3dc082016-01-27 15:43:49 +00002126 /* Access through the GTT requires the device to be awake. */
2127 assert_rpm_wakelock_held(dev_priv);
2128
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002129 ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
2130 i915_gem_obj_ggtt_offset(obj), ringbuf->size);
2131 if (ringbuf->virtual_start == NULL) {
2132 i915_gem_object_ggtt_unpin(obj);
2133 return -EINVAL;
2134 }
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002135 }
2136
Tvrtko Ursulin0eb973d2016-01-15 15:10:28 +00002137 ringbuf->vma = i915_gem_obj_to_ggtt(obj);
2138
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002139 return 0;
2140}
2141
Chris Wilson01101fa2015-09-03 13:01:39 +01002142static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01002143{
Oscar Mateo2919d292014-07-03 16:28:02 +01002144 drm_gem_object_unreference(&ringbuf->obj->base);
2145 ringbuf->obj = NULL;
2146}
2147
Chris Wilson01101fa2015-09-03 13:01:39 +01002148static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
2149 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01002150{
Chris Wilsone3efda42014-04-09 09:19:41 +01002151 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002152
2153 obj = NULL;
2154 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002155 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002156 if (obj == NULL)
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002157 obj = i915_gem_alloc_object(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002158 if (obj == NULL)
2159 return -ENOMEM;
2160
Akash Goel24f3a8c2014-06-17 10:59:42 +05302161 /* mark ring buffers as read-only from GPU side by default */
2162 obj->gt_ro = 1;
2163
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002164 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002165
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002166 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002167}
2168
Chris Wilson01101fa2015-09-03 13:01:39 +01002169struct intel_ringbuffer *
2170intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
2171{
2172 struct intel_ringbuffer *ring;
2173 int ret;
2174
2175 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson608c1a52015-09-03 13:01:40 +01002176 if (ring == NULL) {
2177 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2178 engine->name);
Chris Wilson01101fa2015-09-03 13:01:39 +01002179 return ERR_PTR(-ENOMEM);
Chris Wilson608c1a52015-09-03 13:01:40 +01002180 }
Chris Wilson01101fa2015-09-03 13:01:39 +01002181
2182 ring->ring = engine;
Chris Wilson608c1a52015-09-03 13:01:40 +01002183 list_add(&ring->link, &engine->buffers);
Chris Wilson01101fa2015-09-03 13:01:39 +01002184
2185 ring->size = size;
2186 /* Workaround an erratum on the i830 which causes a hang if
2187 * the TAIL pointer points to within the last 2 cachelines
2188 * of the buffer.
2189 */
2190 ring->effective_size = size;
2191 if (IS_I830(engine->dev) || IS_845G(engine->dev))
2192 ring->effective_size -= 2 * CACHELINE_BYTES;
2193
2194 ring->last_retired_head = -1;
2195 intel_ring_update_space(ring);
2196
2197 ret = intel_alloc_ringbuffer_obj(engine->dev, ring);
2198 if (ret) {
Chris Wilson608c1a52015-09-03 13:01:40 +01002199 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
2200 engine->name, ret);
2201 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002202 kfree(ring);
2203 return ERR_PTR(ret);
2204 }
2205
2206 return ring;
2207}
2208
2209void
2210intel_ringbuffer_free(struct intel_ringbuffer *ring)
2211{
2212 intel_destroy_ringbuffer_obj(ring);
Chris Wilson608c1a52015-09-03 13:01:40 +01002213 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002214 kfree(ring);
2215}
2216
Ben Widawskyc43b5632012-04-16 14:07:40 -07002217static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002218 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002219{
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002220 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002221 int ret;
2222
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002223 WARN_ON(ring->buffer);
2224
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002225 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01002226 INIT_LIST_HEAD(&ring->active_list);
2227 INIT_LIST_HEAD(&ring->request_list);
Oscar Mateocc9130b2014-07-24 17:04:42 +01002228 INIT_LIST_HEAD(&ring->execlist_queue);
Chris Wilson608c1a52015-09-03 13:01:40 +01002229 INIT_LIST_HEAD(&ring->buffers);
Chris Wilson06fbca72015-04-07 16:20:36 +01002230 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Ben Widawskyebc348b2014-04-29 14:52:28 -07002231 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002232
Chris Wilsonb259f672011-03-29 13:19:09 +01002233 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002234
Chris Wilson01101fa2015-09-03 13:01:39 +01002235 ringbuf = intel_engine_create_ringbuffer(ring, 32 * PAGE_SIZE);
Dave Gordonb0366a52015-12-08 15:02:36 +00002236 if (IS_ERR(ringbuf)) {
2237 ret = PTR_ERR(ringbuf);
2238 goto error;
2239 }
Chris Wilson01101fa2015-09-03 13:01:39 +01002240 ring->buffer = ringbuf;
2241
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002242 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002243 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002244 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002245 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002246 } else {
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002247 WARN_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002248 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002249 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002250 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002251 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002252
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002253 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2254 if (ret) {
2255 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2256 ring->name, ret);
2257 intel_destroy_ringbuffer_obj(ringbuf);
2258 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002259 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002260
Brad Volkin44e895a2014-05-10 14:10:43 -07002261 ret = i915_cmd_parser_init_ring(ring);
2262 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002263 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002264
Oscar Mateo8ee14972014-05-22 14:13:34 +01002265 return 0;
2266
2267error:
Dave Gordonb0366a52015-12-08 15:02:36 +00002268 intel_cleanup_ring_buffer(ring);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002269 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002270}
2271
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002272void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002273{
John Harrison6402c332014-10-31 12:00:26 +00002274 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01002275
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002276 if (!intel_ring_initialized(ring))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002277 return;
2278
John Harrison6402c332014-10-31 12:00:26 +00002279 dev_priv = to_i915(ring->dev);
John Harrison6402c332014-10-31 12:00:26 +00002280
Dave Gordonb0366a52015-12-08 15:02:36 +00002281 if (ring->buffer) {
2282 intel_stop_ring_buffer(ring);
2283 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002284
Dave Gordonb0366a52015-12-08 15:02:36 +00002285 intel_unpin_ringbuffer_obj(ring->buffer);
2286 intel_ringbuffer_free(ring->buffer);
2287 ring->buffer = NULL;
2288 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002289
Zou Nan hai8d192152010-11-02 16:31:01 +08002290 if (ring->cleanup)
2291 ring->cleanup(ring);
2292
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002293 if (I915_NEED_GFX_HWS(ring->dev)) {
2294 cleanup_status_page(ring);
2295 } else {
2296 WARN_ON(ring->id != RCS);
2297 cleanup_phys_status_page(ring);
2298 }
Brad Volkin44e895a2014-05-10 14:10:43 -07002299
2300 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01002301 i915_gem_batch_pool_fini(&ring->batch_pool);
Dave Gordonb0366a52015-12-08 15:02:36 +00002302 ring->dev = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002303}
2304
Chris Wilson595e1ee2015-04-07 16:20:51 +01002305static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002306{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002307 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002308 struct drm_i915_gem_request *request;
Chris Wilsonb4716182015-04-27 13:41:17 +01002309 unsigned space;
2310 int ret;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002311
Dave Gordonebd0fd42014-11-27 11:22:49 +00002312 if (intel_ring_space(ringbuf) >= n)
2313 return 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002314
John Harrison79bbcc22015-06-30 12:40:55 +01002315 /* The whole point of reserving space is to not wait! */
2316 WARN_ON(ringbuf->reserved_in_use);
2317
Chris Wilsona71d8d92012-02-15 11:25:36 +00002318 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002319 space = __intel_ring_space(request->postfix, ringbuf->tail,
2320 ringbuf->size);
2321 if (space >= n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002322 break;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002323 }
2324
Chris Wilson595e1ee2015-04-07 16:20:51 +01002325 if (WARN_ON(&request->list == &ring->request_list))
Chris Wilsona71d8d92012-02-15 11:25:36 +00002326 return -ENOSPC;
2327
Daniel Vettera4b3a572014-11-26 14:17:05 +01002328 ret = i915_wait_request(request);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002329 if (ret)
2330 return ret;
2331
Chris Wilsonb4716182015-04-27 13:41:17 +01002332 ringbuf->space = space;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002333 return 0;
2334}
2335
John Harrison79bbcc22015-06-30 12:40:55 +01002336static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
Chris Wilson3e960502012-11-27 16:22:54 +00002337{
2338 uint32_t __iomem *virt;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002339 int rem = ringbuf->size - ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002340
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002341 virt = ringbuf->virtual_start + ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002342 rem /= 4;
2343 while (rem--)
2344 iowrite32(MI_NOOP, virt++);
2345
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002346 ringbuf->tail = 0;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002347 intel_ring_update_space(ringbuf);
Chris Wilson3e960502012-11-27 16:22:54 +00002348}
2349
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002350int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002351{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002352 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002353
Chris Wilson3e960502012-11-27 16:22:54 +00002354 /* Wait upon the last request to be completed */
2355 if (list_empty(&ring->request_list))
2356 return 0;
2357
Daniel Vettera4b3a572014-11-26 14:17:05 +01002358 req = list_entry(ring->request_list.prev,
Chris Wilsonb4716182015-04-27 13:41:17 +01002359 struct drm_i915_gem_request,
2360 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002361
Chris Wilsonb4716182015-04-27 13:41:17 +01002362 /* Make sure we do not trigger any retires */
2363 return __i915_wait_request(req,
2364 atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
2365 to_i915(ring->dev)->mm.interruptible,
2366 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002367}
2368
John Harrison6689cb22015-03-19 12:30:08 +00002369int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002370{
John Harrison6689cb22015-03-19 12:30:08 +00002371 request->ringbuf = request->ring->buffer;
John Harrison9eba5d42014-11-24 18:49:23 +00002372 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002373}
2374
John Harrisonccd98fe2015-05-29 17:44:09 +01002375int intel_ring_reserve_space(struct drm_i915_gem_request *request)
2376{
2377 /*
2378 * The first call merely notes the reserve request and is common for
2379 * all back ends. The subsequent localised _begin() call actually
2380 * ensures that the reservation is available. Without the begin, if
2381 * the request creator immediately submitted the request without
2382 * adding any commands to it then there might not actually be
2383 * sufficient room for the submission commands.
2384 */
2385 intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
2386
2387 return intel_ring_begin(request, 0);
2388}
2389
John Harrison29b1b412015-06-18 13:10:09 +01002390void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2391{
John Harrisonccd98fe2015-05-29 17:44:09 +01002392 WARN_ON(ringbuf->reserved_size);
John Harrison29b1b412015-06-18 13:10:09 +01002393 WARN_ON(ringbuf->reserved_in_use);
2394
2395 ringbuf->reserved_size = size;
John Harrison29b1b412015-06-18 13:10:09 +01002396}
2397
2398void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2399{
2400 WARN_ON(ringbuf->reserved_in_use);
2401
2402 ringbuf->reserved_size = 0;
2403 ringbuf->reserved_in_use = false;
2404}
2405
2406void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2407{
2408 WARN_ON(ringbuf->reserved_in_use);
2409
2410 ringbuf->reserved_in_use = true;
2411 ringbuf->reserved_tail = ringbuf->tail;
2412}
2413
2414void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2415{
2416 WARN_ON(!ringbuf->reserved_in_use);
John Harrison79bbcc22015-06-30 12:40:55 +01002417 if (ringbuf->tail > ringbuf->reserved_tail) {
2418 WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2419 "request reserved size too small: %d vs %d!\n",
2420 ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2421 } else {
2422 /*
2423 * The ring was wrapped while the reserved space was in use.
2424 * That means that some unknown amount of the ring tail was
2425 * no-op filled and skipped. Thus simply adding the ring size
2426 * to the tail and doing the above space check will not work.
2427 * Rather than attempt to track how much tail was skipped,
2428 * it is much simpler to say that also skipping the sanity
2429 * check every once in a while is not a big issue.
2430 */
2431 }
John Harrison29b1b412015-06-18 13:10:09 +01002432
2433 ringbuf->reserved_size = 0;
2434 ringbuf->reserved_in_use = false;
2435}
2436
2437static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002438{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002439 struct intel_ringbuffer *ringbuf = ring->buffer;
John Harrison79bbcc22015-06-30 12:40:55 +01002440 int remain_usable = ringbuf->effective_size - ringbuf->tail;
2441 int remain_actual = ringbuf->size - ringbuf->tail;
2442 int ret, total_bytes, wait_bytes = 0;
2443 bool need_wrap = false;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002444
John Harrison79bbcc22015-06-30 12:40:55 +01002445 if (ringbuf->reserved_in_use)
2446 total_bytes = bytes;
2447 else
2448 total_bytes = bytes + ringbuf->reserved_size;
John Harrison29b1b412015-06-18 13:10:09 +01002449
John Harrison79bbcc22015-06-30 12:40:55 +01002450 if (unlikely(bytes > remain_usable)) {
2451 /*
2452 * Not enough space for the basic request. So need to flush
2453 * out the remainder and then wait for base + reserved.
2454 */
2455 wait_bytes = remain_actual + total_bytes;
2456 need_wrap = true;
2457 } else {
2458 if (unlikely(total_bytes > remain_usable)) {
2459 /*
2460 * The base request will fit but the reserved space
Akash Goeld43f3eb2016-03-11 14:56:42 +05302461 * falls off the end. So don't need an immediate wrap
2462 * and only need to effectively wait for the reserved
2463 * size space from the start of ringbuffer.
John Harrison79bbcc22015-06-30 12:40:55 +01002464 */
2465 wait_bytes = remain_actual + ringbuf->reserved_size;
John Harrison79bbcc22015-06-30 12:40:55 +01002466 } else if (total_bytes > ringbuf->space) {
2467 /* No wrapping required, just waiting. */
2468 wait_bytes = total_bytes;
John Harrison29b1b412015-06-18 13:10:09 +01002469 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002470 }
2471
John Harrison79bbcc22015-06-30 12:40:55 +01002472 if (wait_bytes) {
2473 ret = ring_wait_for_space(ring, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002474 if (unlikely(ret))
2475 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +01002476
2477 if (need_wrap)
2478 __wrap_ring_buffer(ringbuf);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002479 }
2480
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002481 return 0;
2482}
2483
John Harrison5fb9de12015-05-29 17:44:07 +01002484int intel_ring_begin(struct drm_i915_gem_request *req,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002485 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002486{
John Harrison5fb9de12015-05-29 17:44:07 +01002487 struct intel_engine_cs *ring;
2488 struct drm_i915_private *dev_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002489 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01002490
John Harrison5fb9de12015-05-29 17:44:07 +01002491 WARN_ON(req == NULL);
2492 ring = req->ring;
2493 dev_priv = ring->dev->dev_private;
2494
Daniel Vetter33196de2012-11-14 17:14:05 +01002495 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2496 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02002497 if (ret)
2498 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00002499
Chris Wilson304d6952014-01-02 14:32:35 +00002500 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
2501 if (ret)
2502 return ret;
2503
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002504 ring->buffer->space -= num_dwords * sizeof(uint32_t);
Chris Wilson304d6952014-01-02 14:32:35 +00002505 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002506}
2507
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002508/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01002509int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002510{
John Harrisonbba09b12015-05-29 17:44:06 +01002511 struct intel_engine_cs *ring = req->ring;
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002512 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002513 int ret;
2514
2515 if (num_dwords == 0)
2516 return 0;
2517
Chris Wilson18393f62014-04-09 09:19:40 +01002518 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
John Harrison5fb9de12015-05-29 17:44:07 +01002519 ret = intel_ring_begin(req, num_dwords);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002520 if (ret)
2521 return ret;
2522
2523 while (num_dwords--)
2524 intel_ring_emit(ring, MI_NOOP);
2525
2526 intel_ring_advance(ring);
2527
2528 return 0;
2529}
2530
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002531void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002532{
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002533 struct drm_device *dev = ring->dev;
2534 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002535
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002536 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002537 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2538 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002539 if (HAS_VEBOX(dev))
Ben Widawsky50201502013-08-12 16:53:03 -07002540 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002541 }
Chris Wilson297b0c52010-10-22 17:02:41 +01002542
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002543 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03002544 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01002545}
2546
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002547static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002548 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002549{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002550 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002551
2552 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002553
Chris Wilson12f55812012-07-05 17:14:01 +01002554 /* Disable notification that the ring is IDLE. The GT
2555 * will then assume that it is busy and bring it out of rc6.
2556 */
2557 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2558 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2559
2560 /* Clear the context id. Here be magic! */
2561 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2562
2563 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04002564 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01002565 GEN6_BSD_SLEEP_INDICATOR) == 0,
2566 50))
2567 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002568
Chris Wilson12f55812012-07-05 17:14:01 +01002569 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04002570 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01002571 POSTING_READ(RING_TAIL(ring->mmio_base));
2572
2573 /* Let the ring send IDLE messages to the GT again,
2574 * and so let it sleep to conserve power when idle.
2575 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002576 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01002577 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002578}
2579
John Harrisona84c3ae2015-05-29 17:43:57 +01002580static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002581 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002582{
John Harrisona84c3ae2015-05-29 17:43:57 +01002583 struct intel_engine_cs *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002584 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002585 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002586
John Harrison5fb9de12015-05-29 17:44:07 +01002587 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002588 if (ret)
2589 return ret;
2590
Chris Wilson71a77e02011-02-02 12:13:49 +00002591 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002592 if (INTEL_INFO(ring->dev)->gen >= 8)
2593 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002594
2595 /* We always require a command barrier so that subsequent
2596 * commands, such as breadcrumb interrupts, are strictly ordered
2597 * wrt the contents of the write cache being flushed to memory
2598 * (and thus being coherent from the CPU).
2599 */
2600 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2601
Jesse Barnes9a289772012-10-26 09:42:42 -07002602 /*
2603 * Bspec vol 1c.5 - video engine command streamer:
2604 * "If ENABLED, all TLBs will be invalidated once the flush
2605 * operation is complete. This bit is only valid when the
2606 * Post-Sync Operation field is a value of 1h or 3h."
2607 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002608 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002609 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2610
Chris Wilson71a77e02011-02-02 12:13:49 +00002611 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002612 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002613 if (INTEL_INFO(ring->dev)->gen >= 8) {
2614 intel_ring_emit(ring, 0); /* upper addr */
2615 intel_ring_emit(ring, 0); /* value */
2616 } else {
2617 intel_ring_emit(ring, 0);
2618 intel_ring_emit(ring, MI_NOOP);
2619 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002620 intel_ring_advance(ring);
2621 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002622}
2623
2624static int
John Harrison53fddaf2015-05-29 17:44:02 +01002625gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002626 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002627 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002628{
John Harrison53fddaf2015-05-29 17:44:02 +01002629 struct intel_engine_cs *ring = req->ring;
John Harrison8e004ef2015-02-13 11:48:10 +00002630 bool ppgtt = USES_PPGTT(ring->dev) &&
2631 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002632 int ret;
2633
John Harrison5fb9de12015-05-29 17:44:07 +01002634 ret = intel_ring_begin(req, 4);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002635 if (ret)
2636 return ret;
2637
2638 /* FIXME(BDW): Address space and security selectors. */
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002639 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
2640 (dispatch_flags & I915_DISPATCH_RS ?
2641 MI_BATCH_RESOURCE_STREAMER : 0));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002642 intel_ring_emit(ring, lower_32_bits(offset));
2643 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002644 intel_ring_emit(ring, MI_NOOP);
2645 intel_ring_advance(ring);
2646
2647 return 0;
2648}
2649
2650static int
John Harrison53fddaf2015-05-29 17:44:02 +01002651hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002652 u64 offset, u32 len,
2653 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002654{
John Harrison53fddaf2015-05-29 17:44:02 +01002655 struct intel_engine_cs *ring = req->ring;
Akshay Joshi0206e352011-08-16 15:34:10 -04002656 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002657
John Harrison5fb9de12015-05-29 17:44:07 +01002658 ret = intel_ring_begin(req, 2);
Akshay Joshi0206e352011-08-16 15:34:10 -04002659 if (ret)
2660 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002661
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002662 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002663 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002664 (dispatch_flags & I915_DISPATCH_SECURE ?
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002665 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2666 (dispatch_flags & I915_DISPATCH_RS ?
2667 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002668 /* bit0-7 is the length on GEN6+ */
2669 intel_ring_emit(ring, offset);
2670 intel_ring_advance(ring);
2671
2672 return 0;
2673}
2674
2675static int
John Harrison53fddaf2015-05-29 17:44:02 +01002676gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002677 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002678 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002679{
John Harrison53fddaf2015-05-29 17:44:02 +01002680 struct intel_engine_cs *ring = req->ring;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002681 int ret;
2682
John Harrison5fb9de12015-05-29 17:44:07 +01002683 ret = intel_ring_begin(req, 2);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002684 if (ret)
2685 return ret;
2686
2687 intel_ring_emit(ring,
2688 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002689 (dispatch_flags & I915_DISPATCH_SECURE ?
2690 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002691 /* bit0-7 is the length on GEN6+ */
2692 intel_ring_emit(ring, offset);
2693 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002694
Akshay Joshi0206e352011-08-16 15:34:10 -04002695 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002696}
2697
Chris Wilson549f7362010-10-19 11:19:32 +01002698/* Blitter support (SandyBridge+) */
2699
John Harrisona84c3ae2015-05-29 17:43:57 +01002700static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002701 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002702{
John Harrisona84c3ae2015-05-29 17:43:57 +01002703 struct intel_engine_cs *ring = req->ring;
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002704 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00002705 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002706 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002707
John Harrison5fb9de12015-05-29 17:44:07 +01002708 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002709 if (ret)
2710 return ret;
2711
Chris Wilson71a77e02011-02-02 12:13:49 +00002712 cmd = MI_FLUSH_DW;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002713 if (INTEL_INFO(dev)->gen >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002714 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002715
2716 /* We always require a command barrier so that subsequent
2717 * commands, such as breadcrumb interrupts, are strictly ordered
2718 * wrt the contents of the write cache being flushed to memory
2719 * (and thus being coherent from the CPU).
2720 */
2721 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2722
Jesse Barnes9a289772012-10-26 09:42:42 -07002723 /*
2724 * Bspec vol 1c.3 - blitter engine command streamer:
2725 * "If ENABLED, all TLBs will be invalidated once the flush
2726 * operation is complete. This bit is only valid when the
2727 * Post-Sync Operation field is a value of 1h or 3h."
2728 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002729 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002730 cmd |= MI_INVALIDATE_TLB;
Chris Wilson71a77e02011-02-02 12:13:49 +00002731 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002732 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002733 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002734 intel_ring_emit(ring, 0); /* upper addr */
2735 intel_ring_emit(ring, 0); /* value */
2736 } else {
2737 intel_ring_emit(ring, 0);
2738 intel_ring_emit(ring, MI_NOOP);
2739 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002740 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002741
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002742 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002743}
2744
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002745int intel_init_render_ring_buffer(struct drm_device *dev)
2746{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002747 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002748 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002749 struct drm_i915_gem_object *obj;
2750 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002751
Daniel Vetter59465b52012-04-11 22:12:48 +02002752 ring->name = "render ring";
2753 ring->id = RCS;
Chris Wilson426960b2016-01-15 16:51:46 +00002754 ring->exec_id = I915_EXEC_RENDER;
Daniel Vetter59465b52012-04-11 22:12:48 +02002755 ring->mmio_base = RENDER_RING_BASE;
2756
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002757 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002758 if (i915_semaphore_is_enabled(dev)) {
2759 obj = i915_gem_alloc_object(dev, 4096);
2760 if (obj == NULL) {
2761 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2762 i915.semaphores = 0;
2763 } else {
2764 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2765 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2766 if (ret != 0) {
2767 drm_gem_object_unreference(&obj->base);
2768 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2769 i915.semaphores = 0;
2770 } else
2771 dev_priv->semaphore_obj = obj;
2772 }
2773 }
Mika Kuoppala72253422014-10-07 17:21:26 +03002774
Daniel Vetter8f0e2b92014-12-02 16:19:07 +01002775 ring->init_context = intel_rcs_ctx_init;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002776 ring->add_request = gen6_add_request;
2777 ring->flush = gen8_render_ring_flush;
2778 ring->irq_get = gen8_ring_get_irq;
2779 ring->irq_put = gen8_ring_put_irq;
2780 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2781 ring->get_seqno = gen6_ring_get_seqno;
2782 ring->set_seqno = ring_set_seqno;
2783 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002784 WARN_ON(!dev_priv->semaphore_obj);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002785 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002786 ring->semaphore.signal = gen8_rcs_signal;
2787 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002788 }
2789 } else if (INTEL_INFO(dev)->gen >= 6) {
Francisco Jerez4f91fc62015-10-07 14:44:02 +03002790 ring->init_context = intel_rcs_ctx_init;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002791 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03002792 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01002793 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03002794 ring->flush = gen6_render_ring_flush;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002795 ring->irq_get = gen6_ring_get_irq;
2796 ring->irq_put = gen6_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002797 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01002798 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002799 ring->set_seqno = ring_set_seqno;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002800 if (i915_semaphore_is_enabled(dev)) {
2801 ring->semaphore.sync_to = gen6_ring_sync;
2802 ring->semaphore.signal = gen6_signal;
2803 /*
2804 * The current semaphore is only applied on pre-gen8
2805 * platform. And there is no VCS2 ring on the pre-gen8
2806 * platform. So the semaphore between RCS and VCS2 is
2807 * initialized as INVALID. Gen8 will initialize the
2808 * sema between VCS2 and RCS later.
2809 */
2810 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2811 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2812 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2813 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2814 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2815 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2816 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2817 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2818 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2819 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2820 }
Chris Wilsonc6df5412010-12-15 09:56:50 +00002821 } else if (IS_GEN5(dev)) {
2822 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002823 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00002824 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002825 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002826 ring->irq_get = gen5_ring_get_irq;
2827 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002828 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2829 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002830 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002831 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002832 if (INTEL_INFO(dev)->gen < 4)
2833 ring->flush = gen2_render_ring_flush;
2834 else
2835 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002836 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002837 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002838 if (IS_GEN2(dev)) {
2839 ring->irq_get = i8xx_ring_get_irq;
2840 ring->irq_put = i8xx_ring_put_irq;
2841 } else {
2842 ring->irq_get = i9xx_ring_get_irq;
2843 ring->irq_put = i9xx_ring_put_irq;
2844 }
Daniel Vettere3670312012-04-11 22:12:53 +02002845 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002846 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002847 ring->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002848
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002849 if (IS_HASWELL(dev))
2850 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002851 else if (IS_GEN8(dev))
2852 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002853 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002854 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2855 else if (INTEL_INFO(dev)->gen >= 4)
2856 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2857 else if (IS_I830(dev) || IS_845G(dev))
2858 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2859 else
2860 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002861 ring->init_hw = init_render_ring;
Daniel Vetter59465b52012-04-11 22:12:48 +02002862 ring->cleanup = render_ring_cleanup;
2863
Daniel Vetterb45305f2012-12-17 16:21:27 +01002864 /* Workaround batchbuffer to combat CS tlb bug. */
2865 if (HAS_BROKEN_CS_TLB(dev)) {
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002866 obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002867 if (obj == NULL) {
2868 DRM_ERROR("Failed to allocate batch bo\n");
2869 return -ENOMEM;
2870 }
2871
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002872 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002873 if (ret != 0) {
2874 drm_gem_object_unreference(&obj->base);
2875 DRM_ERROR("Failed to ping batch bo\n");
2876 return ret;
2877 }
2878
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002879 ring->scratch.obj = obj;
2880 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002881 }
2882
Daniel Vetter99be1df2014-11-20 00:33:06 +01002883 ret = intel_init_ring_buffer(dev, ring);
2884 if (ret)
2885 return ret;
2886
2887 if (INTEL_INFO(dev)->gen >= 5) {
2888 ret = intel_init_pipe_control(ring);
2889 if (ret)
2890 return ret;
2891 }
2892
2893 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002894}
2895
2896int intel_init_bsd_ring_buffer(struct drm_device *dev)
2897{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002898 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002899 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002900
Daniel Vetter58fa3832012-04-11 22:12:49 +02002901 ring->name = "bsd ring";
2902 ring->id = VCS;
Chris Wilson426960b2016-01-15 16:51:46 +00002903 ring->exec_id = I915_EXEC_BSD;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002904
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002905 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002906 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002907 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002908 /* gen6 bsd needs a special wa for tail updates */
2909 if (IS_GEN6(dev))
2910 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002911 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002912 ring->add_request = gen6_add_request;
2913 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002914 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002915 if (INTEL_INFO(dev)->gen >= 8) {
2916 ring->irq_enable_mask =
2917 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2918 ring->irq_get = gen8_ring_get_irq;
2919 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002920 ring->dispatch_execbuffer =
2921 gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002922 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002923 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002924 ring->semaphore.signal = gen8_xcs_signal;
2925 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002926 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002927 } else {
2928 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2929 ring->irq_get = gen6_ring_get_irq;
2930 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002931 ring->dispatch_execbuffer =
2932 gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002933 if (i915_semaphore_is_enabled(dev)) {
2934 ring->semaphore.sync_to = gen6_ring_sync;
2935 ring->semaphore.signal = gen6_signal;
2936 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2937 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2938 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2939 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2940 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2941 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2942 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2943 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2944 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2945 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2946 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002947 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002948 } else {
2949 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002950 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002951 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002952 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002953 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002954 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002955 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002956 ring->irq_get = gen5_ring_get_irq;
2957 ring->irq_put = gen5_ring_put_irq;
2958 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002959 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002960 ring->irq_get = i9xx_ring_get_irq;
2961 ring->irq_put = i9xx_ring_put_irq;
2962 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002963 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002964 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002965 ring->init_hw = init_ring_common;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002966
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002967 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002968}
Chris Wilson549f7362010-10-19 11:19:32 +01002969
Zhao Yakui845f74a2014-04-17 10:37:37 +08002970/**
Damien Lespiau62659922015-01-29 14:13:40 +00002971 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002972 */
2973int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2974{
2975 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002976 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002977
Rodrigo Vivif7b64232014-07-01 02:41:36 -07002978 ring->name = "bsd2 ring";
Zhao Yakui845f74a2014-04-17 10:37:37 +08002979 ring->id = VCS2;
Chris Wilson426960b2016-01-15 16:51:46 +00002980 ring->exec_id = I915_EXEC_BSD;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002981
2982 ring->write_tail = ring_write_tail;
2983 ring->mmio_base = GEN8_BSD2_RING_BASE;
2984 ring->flush = gen6_bsd_ring_flush;
2985 ring->add_request = gen6_add_request;
2986 ring->get_seqno = gen6_ring_get_seqno;
2987 ring->set_seqno = ring_set_seqno;
2988 ring->irq_enable_mask =
2989 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2990 ring->irq_get = gen8_ring_get_irq;
2991 ring->irq_put = gen8_ring_put_irq;
2992 ring->dispatch_execbuffer =
2993 gen8_ring_dispatch_execbuffer;
Ben Widawsky3e789982014-06-30 09:53:37 -07002994 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002995 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002996 ring->semaphore.signal = gen8_xcs_signal;
2997 GEN8_RING_SEMAPHORE_INIT;
2998 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002999 ring->init_hw = init_ring_common;
Zhao Yakui845f74a2014-04-17 10:37:37 +08003000
3001 return intel_init_ring_buffer(dev, ring);
3002}
3003
Chris Wilson549f7362010-10-19 11:19:32 +01003004int intel_init_blt_ring_buffer(struct drm_device *dev)
3005{
Jani Nikula4640c4f2014-03-31 14:27:19 +03003006 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003007 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01003008
Daniel Vetter3535d9d2012-04-11 22:12:50 +02003009 ring->name = "blitter ring";
3010 ring->id = BCS;
Chris Wilson426960b2016-01-15 16:51:46 +00003011 ring->exec_id = I915_EXEC_BLT;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02003012
3013 ring->mmio_base = BLT_RING_BASE;
3014 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07003015 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02003016 ring->add_request = gen6_add_request;
3017 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02003018 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003019 if (INTEL_INFO(dev)->gen >= 8) {
3020 ring->irq_enable_mask =
3021 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
3022 ring->irq_get = gen8_ring_get_irq;
3023 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07003024 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003025 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07003026 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07003027 ring->semaphore.signal = gen8_xcs_signal;
3028 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003029 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003030 } else {
3031 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
3032 ring->irq_get = gen6_ring_get_irq;
3033 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07003034 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003035 if (i915_semaphore_is_enabled(dev)) {
3036 ring->semaphore.signal = gen6_signal;
3037 ring->semaphore.sync_to = gen6_ring_sync;
3038 /*
3039 * The current semaphore is only applied on pre-gen8
3040 * platform. And there is no VCS2 ring on the pre-gen8
3041 * platform. So the semaphore between BCS and VCS2 is
3042 * initialized as INVALID. Gen8 will initialize the
3043 * sema between BCS and VCS2 later.
3044 */
3045 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
3046 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
3047 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
3048 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
3049 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
3050 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
3051 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
3052 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
3053 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
3054 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
3055 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003056 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01003057 ring->init_hw = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01003058
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003059 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01003060}
Chris Wilsona7b97612012-07-20 12:41:08 +01003061
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003062int intel_init_vebox_ring_buffer(struct drm_device *dev)
3063{
Jani Nikula4640c4f2014-03-31 14:27:19 +03003064 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003065 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003066
3067 ring->name = "video enhancement ring";
3068 ring->id = VECS;
Chris Wilson426960b2016-01-15 16:51:46 +00003069 ring->exec_id = I915_EXEC_VEBOX;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003070
3071 ring->mmio_base = VEBOX_RING_BASE;
3072 ring->write_tail = ring_write_tail;
3073 ring->flush = gen6_ring_flush;
3074 ring->add_request = gen6_add_request;
3075 ring->get_seqno = gen6_ring_get_seqno;
3076 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003077
3078 if (INTEL_INFO(dev)->gen >= 8) {
3079 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08003080 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003081 ring->irq_get = gen8_ring_get_irq;
3082 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07003083 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003084 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07003085 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07003086 ring->semaphore.signal = gen8_xcs_signal;
3087 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003088 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003089 } else {
3090 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
3091 ring->irq_get = hsw_vebox_get_irq;
3092 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07003093 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07003094 if (i915_semaphore_is_enabled(dev)) {
3095 ring->semaphore.sync_to = gen6_ring_sync;
3096 ring->semaphore.signal = gen6_signal;
3097 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
3098 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
3099 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
3100 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
3101 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
3102 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
3103 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
3104 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
3105 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
3106 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
3107 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07003108 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01003109 ring->init_hw = init_ring_common;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003110
3111 return intel_init_ring_buffer(dev, ring);
3112}
3113
Chris Wilsona7b97612012-07-20 12:41:08 +01003114int
John Harrison4866d722015-05-29 17:43:55 +01003115intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01003116{
John Harrison4866d722015-05-29 17:43:55 +01003117 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01003118 int ret;
3119
3120 if (!ring->gpu_caches_dirty)
3121 return 0;
3122
John Harrisona84c3ae2015-05-29 17:43:57 +01003123 ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01003124 if (ret)
3125 return ret;
3126
John Harrisona84c3ae2015-05-29 17:43:57 +01003127 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01003128
3129 ring->gpu_caches_dirty = false;
3130 return 0;
3131}
3132
3133int
John Harrison2f200552015-05-29 17:43:53 +01003134intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01003135{
John Harrison2f200552015-05-29 17:43:53 +01003136 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01003137 uint32_t flush_domains;
3138 int ret;
3139
3140 flush_domains = 0;
3141 if (ring->gpu_caches_dirty)
3142 flush_domains = I915_GEM_GPU_DOMAINS;
3143
John Harrisona84c3ae2015-05-29 17:43:57 +01003144 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003145 if (ret)
3146 return ret;
3147
John Harrisona84c3ae2015-05-29 17:43:57 +01003148 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003149
3150 ring->gpu_caches_dirty = false;
3151 return 0;
3152}
Chris Wilsone3efda42014-04-09 09:19:41 +01003153
3154void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003155intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01003156{
3157 int ret;
3158
3159 if (!intel_ring_initialized(ring))
3160 return;
3161
3162 ret = intel_ring_idle(ring);
3163 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
3164 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
3165 ring->name, ret);
3166
3167 stop_ring(ring);
3168}