blob: 2425dc2db42c0fdfd767b83907fc2163664c4c2d [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
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/drmP.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070031#include "i915_drv.h"
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/i915_drm.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070033#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010034#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070035
Oscar Mateo48d82382014-07-24 17:04:23 +010036bool
37intel_ring_initialized(struct intel_engine_cs *ring)
38{
39 struct drm_device *dev = ring->dev;
Chris Wilson18393f62014-04-09 09:19:40 +010040
Oscar Mateo48d82382014-07-24 17:04:23 +010041 if (!dev)
42 return false;
43
44 if (i915.enable_execlists) {
45 struct intel_context *dctx = ring->default_context;
46 struct intel_ringbuffer *ringbuf = dctx->engine[ring->id].ringbuf;
47
48 return ringbuf->obj;
49 } else
50 return ring->buffer && ring->buffer->obj;
51}
52
Oscar Mateo82e104c2014-07-24 17:04:26 +010053int __intel_ring_space(int head, int tail, int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010054{
Dave Gordon4f547412014-11-27 11:22:48 +000055 int space = head - tail;
56 if (space <= 0)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010057 space += size;
Dave Gordon4f547412014-11-27 11:22:48 +000058 return space - I915_RING_FREE_SPACE;
Chris Wilson1cf0ba12014-05-05 09:07:33 +010059}
60
Dave Gordonebd0fd42014-11-27 11:22:49 +000061void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
62{
63 if (ringbuf->last_retired_head != -1) {
64 ringbuf->head = ringbuf->last_retired_head;
65 ringbuf->last_retired_head = -1;
66 }
67
68 ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
69 ringbuf->tail, ringbuf->size);
70}
71
Oscar Mateo82e104c2014-07-24 17:04:26 +010072int intel_ring_space(struct intel_ringbuffer *ringbuf)
Chris Wilsonc7dca472011-01-20 17:00:10 +000073{
Dave Gordonebd0fd42014-11-27 11:22:49 +000074 intel_ring_update_space(ringbuf);
75 return ringbuf->space;
Chris Wilsonc7dca472011-01-20 17:00:10 +000076}
77
Oscar Mateo82e104c2014-07-24 17:04:26 +010078bool intel_ring_stopped(struct intel_engine_cs *ring)
Chris Wilson09246732013-08-10 22:16:32 +010079{
80 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020081 return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
82}
Chris Wilson09246732013-08-10 22:16:32 +010083
John Harrison6258fbe2015-05-29 17:43:48 +010084static void __intel_ring_advance(struct intel_engine_cs *ring)
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020085{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010086 struct intel_ringbuffer *ringbuf = ring->buffer;
87 ringbuf->tail &= ringbuf->size - 1;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020088 if (intel_ring_stopped(ring))
Chris Wilson09246732013-08-10 22:16:32 +010089 return;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010090 ring->write_tail(ring, ringbuf->tail);
Chris Wilson09246732013-08-10 22:16:32 +010091}
92
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000093static int
John Harrisona84c3ae2015-05-29 17:43:57 +010094gen2_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010095 u32 invalidate_domains,
96 u32 flush_domains)
97{
John Harrisona84c3ae2015-05-29 17:43:57 +010098 struct intel_engine_cs *ring = req->ring;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010099 u32 cmd;
100 int ret;
101
102 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +0200103 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100104 cmd |= MI_NO_WRITE_FLUSH;
105
106 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
107 cmd |= MI_READ_FLUSH;
108
109 ret = intel_ring_begin(ring, 2);
110 if (ret)
111 return ret;
112
113 intel_ring_emit(ring, cmd);
114 intel_ring_emit(ring, MI_NOOP);
115 intel_ring_advance(ring);
116
117 return 0;
118}
119
120static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100121gen4_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100122 u32 invalidate_domains,
123 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700124{
John Harrisona84c3ae2015-05-29 17:43:57 +0100125 struct intel_engine_cs *ring = req->ring;
Chris Wilson78501ea2010-10-27 12:18:21 +0100126 struct drm_device *dev = ring->dev;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100127 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000128 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100129
Chris Wilson36d527d2011-03-19 22:26:49 +0000130 /*
131 * read/write caches:
132 *
133 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
134 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
135 * also flushed at 2d versus 3d pipeline switches.
136 *
137 * read-only caches:
138 *
139 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
140 * MI_READ_FLUSH is set, and is always flushed on 965.
141 *
142 * I915_GEM_DOMAIN_COMMAND may not exist?
143 *
144 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
145 * invalidated when MI_EXE_FLUSH is set.
146 *
147 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
148 * invalidated with every MI_FLUSH.
149 *
150 * TLBs:
151 *
152 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
153 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
154 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
155 * are flushed at any MI_FLUSH.
156 */
157
158 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100159 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000160 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000161 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
162 cmd |= MI_EXE_FLUSH;
163
164 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
165 (IS_G4X(dev) || IS_GEN5(dev)))
166 cmd |= MI_INVALIDATE_ISP;
167
168 ret = intel_ring_begin(ring, 2);
169 if (ret)
170 return ret;
171
172 intel_ring_emit(ring, cmd);
173 intel_ring_emit(ring, MI_NOOP);
174 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000175
176 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800177}
178
Jesse Barnes8d315282011-10-16 10:23:31 +0200179/**
180 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
181 * implementing two workarounds on gen6. From section 1.4.7.1
182 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
183 *
184 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
185 * produced by non-pipelined state commands), software needs to first
186 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
187 * 0.
188 *
189 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
190 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
191 *
192 * And the workaround for these two requires this workaround first:
193 *
194 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
195 * BEFORE the pipe-control with a post-sync op and no write-cache
196 * flushes.
197 *
198 * And this last workaround is tricky because of the requirements on
199 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
200 * volume 2 part 1:
201 *
202 * "1 of the following must also be set:
203 * - Render Target Cache Flush Enable ([12] of DW1)
204 * - Depth Cache Flush Enable ([0] of DW1)
205 * - Stall at Pixel Scoreboard ([1] of DW1)
206 * - Depth Stall ([13] of DW1)
207 * - Post-Sync Operation ([13] of DW1)
208 * - Notify Enable ([8] of DW1)"
209 *
210 * The cache flushes require the workaround flush that triggered this
211 * one, so we can't use it. Depth stall would trigger the same.
212 * Post-sync nonzero is what triggered this second workaround, so we
213 * can't use that one either. Notify enable is IRQs, which aren't
214 * really our business. That leaves only stall at scoreboard.
215 */
216static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100217intel_emit_post_sync_nonzero_flush(struct intel_engine_cs *ring)
Jesse Barnes8d315282011-10-16 10:23:31 +0200218{
Chris Wilson18393f62014-04-09 09:19:40 +0100219 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200220 int ret;
221
222
223 ret = intel_ring_begin(ring, 6);
224 if (ret)
225 return ret;
226
227 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
228 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
229 PIPE_CONTROL_STALL_AT_SCOREBOARD);
230 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
231 intel_ring_emit(ring, 0); /* low dword */
232 intel_ring_emit(ring, 0); /* high dword */
233 intel_ring_emit(ring, MI_NOOP);
234 intel_ring_advance(ring);
235
236 ret = intel_ring_begin(ring, 6);
237 if (ret)
238 return ret;
239
240 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
241 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
242 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
243 intel_ring_emit(ring, 0);
244 intel_ring_emit(ring, 0);
245 intel_ring_emit(ring, MI_NOOP);
246 intel_ring_advance(ring);
247
248 return 0;
249}
250
251static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100252gen6_render_ring_flush(struct drm_i915_gem_request *req,
253 u32 invalidate_domains, u32 flush_domains)
Jesse Barnes8d315282011-10-16 10:23:31 +0200254{
John Harrisona84c3ae2015-05-29 17:43:57 +0100255 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8d315282011-10-16 10:23:31 +0200256 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100257 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200258 int ret;
259
Paulo Zanonib3111502012-08-17 18:35:42 -0300260 /* Force SNB workarounds for PIPE_CONTROL flushes */
261 ret = intel_emit_post_sync_nonzero_flush(ring);
262 if (ret)
263 return ret;
264
Jesse Barnes8d315282011-10-16 10:23:31 +0200265 /* Just flush everything. Experiments have shown that reducing the
266 * number of bits based on the write domains has little performance
267 * impact.
268 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100269 if (flush_domains) {
270 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
271 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
272 /*
273 * Ensure that any following seqno writes only happen
274 * when the render cache is indeed flushed.
275 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200276 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100277 }
278 if (invalidate_domains) {
279 flags |= PIPE_CONTROL_TLB_INVALIDATE;
280 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
281 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
282 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
283 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
284 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
285 /*
286 * TLB invalidate requires a post-sync write.
287 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700288 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100289 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200290
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100291 ret = intel_ring_begin(ring, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200292 if (ret)
293 return ret;
294
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100295 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
Jesse Barnes8d315282011-10-16 10:23:31 +0200296 intel_ring_emit(ring, flags);
297 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100298 intel_ring_emit(ring, 0);
Jesse Barnes8d315282011-10-16 10:23:31 +0200299 intel_ring_advance(ring);
300
301 return 0;
302}
303
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100304static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100305gen7_render_ring_cs_stall_wa(struct intel_engine_cs *ring)
Paulo Zanonif3987632012-08-17 18:35:43 -0300306{
307 int ret;
308
309 ret = intel_ring_begin(ring, 4);
310 if (ret)
311 return ret;
312
313 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
314 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
315 PIPE_CONTROL_STALL_AT_SCOREBOARD);
316 intel_ring_emit(ring, 0);
317 intel_ring_emit(ring, 0);
318 intel_ring_advance(ring);
319
320 return 0;
321}
322
323static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100324gen7_render_ring_flush(struct drm_i915_gem_request *req,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300325 u32 invalidate_domains, u32 flush_domains)
326{
John Harrisona84c3ae2015-05-29 17:43:57 +0100327 struct intel_engine_cs *ring = req->ring;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300328 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100329 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300330 int ret;
331
Paulo Zanonif3987632012-08-17 18:35:43 -0300332 /*
333 * Ensure that any following seqno writes only happen when the render
334 * cache is indeed flushed.
335 *
336 * Workaround: 4th PIPE_CONTROL command (except the ones with only
337 * read-cache invalidate bits set) must have the CS_STALL bit set. We
338 * don't try to be clever and just set it unconditionally.
339 */
340 flags |= PIPE_CONTROL_CS_STALL;
341
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300342 /* Just flush everything. Experiments have shown that reducing the
343 * number of bits based on the write domains has little performance
344 * impact.
345 */
346 if (flush_domains) {
347 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
348 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300349 }
350 if (invalidate_domains) {
351 flags |= PIPE_CONTROL_TLB_INVALIDATE;
352 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
353 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
354 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
355 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
356 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000357 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300358 /*
359 * TLB invalidate requires a post-sync write.
360 */
361 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200362 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300363
Chris Wilsonadd284a2014-12-16 08:44:32 +0000364 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
365
Paulo Zanonif3987632012-08-17 18:35:43 -0300366 /* Workaround: we must issue a pipe_control with CS-stall bit
367 * set before a pipe_control command that has the state cache
368 * invalidate bit set. */
369 gen7_render_ring_cs_stall_wa(ring);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300370 }
371
372 ret = intel_ring_begin(ring, 4);
373 if (ret)
374 return ret;
375
376 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
377 intel_ring_emit(ring, flags);
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200378 intel_ring_emit(ring, scratch_addr);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300379 intel_ring_emit(ring, 0);
380 intel_ring_advance(ring);
381
382 return 0;
383}
384
Ben Widawskya5f3d682013-11-02 21:07:27 -0700385static int
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300386gen8_emit_pipe_control(struct intel_engine_cs *ring,
387 u32 flags, u32 scratch_addr)
388{
389 int ret;
390
391 ret = intel_ring_begin(ring, 6);
392 if (ret)
393 return ret;
394
395 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
396 intel_ring_emit(ring, flags);
397 intel_ring_emit(ring, scratch_addr);
398 intel_ring_emit(ring, 0);
399 intel_ring_emit(ring, 0);
400 intel_ring_emit(ring, 0);
401 intel_ring_advance(ring);
402
403 return 0;
404}
405
406static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100407gen8_render_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700408 u32 invalidate_domains, u32 flush_domains)
409{
John Harrisona84c3ae2015-05-29 17:43:57 +0100410 struct intel_engine_cs *ring = req->ring;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700411 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100412 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800413 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700414
415 flags |= PIPE_CONTROL_CS_STALL;
416
417 if (flush_domains) {
418 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
419 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
420 }
421 if (invalidate_domains) {
422 flags |= PIPE_CONTROL_TLB_INVALIDATE;
423 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
424 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
425 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
426 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
427 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
428 flags |= PIPE_CONTROL_QW_WRITE;
429 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800430
431 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
432 ret = gen8_emit_pipe_control(ring,
433 PIPE_CONTROL_CS_STALL |
434 PIPE_CONTROL_STALL_AT_SCOREBOARD,
435 0);
436 if (ret)
437 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700438 }
439
kbuild test robot6e0b3f82015-03-05 22:03:08 +0800440 return gen8_emit_pipe_control(ring, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700441}
442
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100443static void ring_write_tail(struct intel_engine_cs *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100444 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800445{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300446 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100447 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800448}
449
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100450u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800451{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300452 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson50877442014-03-21 12:41:53 +0000453 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800454
Chris Wilson50877442014-03-21 12:41:53 +0000455 if (INTEL_INFO(ring->dev)->gen >= 8)
456 acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
457 RING_ACTHD_UDW(ring->mmio_base));
458 else if (INTEL_INFO(ring->dev)->gen >= 4)
459 acthd = I915_READ(RING_ACTHD(ring->mmio_base));
460 else
461 acthd = I915_READ(ACTHD);
462
463 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800464}
465
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100466static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200467{
468 struct drm_i915_private *dev_priv = ring->dev->dev_private;
469 u32 addr;
470
471 addr = dev_priv->status_page_dmah->busaddr;
472 if (INTEL_INFO(ring->dev)->gen >= 4)
473 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
474 I915_WRITE(HWS_PGA, addr);
475}
476
Damien Lespiauaf75f262015-02-10 19:32:17 +0000477static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
478{
479 struct drm_device *dev = ring->dev;
480 struct drm_i915_private *dev_priv = ring->dev->dev_private;
481 u32 mmio = 0;
482
483 /* The ring status page addresses are no longer next to the rest of
484 * the ring registers as of gen7.
485 */
486 if (IS_GEN7(dev)) {
487 switch (ring->id) {
488 case RCS:
489 mmio = RENDER_HWS_PGA_GEN7;
490 break;
491 case BCS:
492 mmio = BLT_HWS_PGA_GEN7;
493 break;
494 /*
495 * VCS2 actually doesn't exist on Gen7. Only shut up
496 * gcc switch check warning
497 */
498 case VCS2:
499 case VCS:
500 mmio = BSD_HWS_PGA_GEN7;
501 break;
502 case VECS:
503 mmio = VEBOX_HWS_PGA_GEN7;
504 break;
505 }
506 } else if (IS_GEN6(ring->dev)) {
507 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
508 } else {
509 /* XXX: gen8 returns to sanity */
510 mmio = RING_HWS_PGA(ring->mmio_base);
511 }
512
513 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
514 POSTING_READ(mmio);
515
516 /*
517 * Flush the TLB for this page
518 *
519 * FIXME: These two bits have disappeared on gen8, so a question
520 * arises: do we still need this and if so how should we go about
521 * invalidating the TLB?
522 */
523 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
524 u32 reg = RING_INSTPM(ring->mmio_base);
525
526 /* ring should be idle before issuing a sync flush*/
527 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
528
529 I915_WRITE(reg,
530 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
531 INSTPM_SYNC_FLUSH));
532 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
533 1000))
534 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
535 ring->name);
536 }
537}
538
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100539static bool stop_ring(struct intel_engine_cs *ring)
Chris Wilson9991ae72014-04-02 16:36:07 +0100540{
541 struct drm_i915_private *dev_priv = to_i915(ring->dev);
542
543 if (!IS_GEN2(ring->dev)) {
544 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
Daniel Vetter403bdd12014-08-07 16:05:39 +0200545 if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
546 DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100547 /* Sometimes we observe that the idle flag is not
548 * set even though the ring is empty. So double
549 * check before giving up.
550 */
551 if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
552 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100553 }
554 }
555
556 I915_WRITE_CTL(ring, 0);
557 I915_WRITE_HEAD(ring, 0);
558 ring->write_tail(ring, 0);
559
560 if (!IS_GEN2(ring->dev)) {
561 (void)I915_READ_CTL(ring);
562 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
563 }
564
565 return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
566}
567
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100568static int init_ring_common(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800569{
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200570 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300571 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100572 struct intel_ringbuffer *ringbuf = ring->buffer;
573 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200574 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800575
Mika Kuoppala59bad942015-01-16 11:34:40 +0200576 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200577
Chris Wilson9991ae72014-04-02 16:36:07 +0100578 if (!stop_ring(ring)) {
579 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000580 DRM_DEBUG_KMS("%s head not reset to zero "
581 "ctl %08x head %08x tail %08x start %08x\n",
582 ring->name,
583 I915_READ_CTL(ring),
584 I915_READ_HEAD(ring),
585 I915_READ_TAIL(ring),
586 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800587
Chris Wilson9991ae72014-04-02 16:36:07 +0100588 if (!stop_ring(ring)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000589 DRM_ERROR("failed to set %s head to zero "
590 "ctl %08x head %08x tail %08x start %08x\n",
591 ring->name,
592 I915_READ_CTL(ring),
593 I915_READ_HEAD(ring),
594 I915_READ_TAIL(ring),
595 I915_READ_START(ring));
Chris Wilson9991ae72014-04-02 16:36:07 +0100596 ret = -EIO;
597 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000598 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700599 }
600
Chris Wilson9991ae72014-04-02 16:36:07 +0100601 if (I915_NEED_GFX_HWS(dev))
602 intel_ring_setup_status_page(ring);
603 else
604 ring_setup_phys_status_page(ring);
605
Jiri Kosinaece4a172014-08-07 16:29:53 +0200606 /* Enforce ordering by reading HEAD register back */
607 I915_READ_HEAD(ring);
608
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200609 /* Initialize the ring. This must happen _after_ we've cleared the ring
610 * registers with the above sequence (the readback of the HEAD registers
611 * also enforces ordering), otherwise the hw might lose the new ring
612 * register values. */
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700613 I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100614
615 /* WaClearRingBufHeadRegAtInit:ctg,elk */
616 if (I915_READ_HEAD(ring))
617 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
618 ring->name, I915_READ_HEAD(ring));
619 I915_WRITE_HEAD(ring, 0);
620 (void)I915_READ_HEAD(ring);
621
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200622 I915_WRITE_CTL(ring,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100623 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000624 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800625
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800626 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400627 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700628 I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
Sean Paulf01db982012-03-16 12:43:22 -0400629 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000630 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100631 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
632 ring->name,
633 I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
634 I915_READ_HEAD(ring), I915_READ_TAIL(ring),
635 I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200636 ret = -EIO;
637 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800638 }
639
Dave Gordonebd0fd42014-11-27 11:22:49 +0000640 ringbuf->last_retired_head = -1;
Chris Wilson5c6c6002014-09-06 10:28:27 +0100641 ringbuf->head = I915_READ_HEAD(ring);
642 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Dave Gordonebd0fd42014-11-27 11:22:49 +0000643 intel_ring_update_space(ringbuf);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000644
Chris Wilson50f018d2013-06-10 11:20:19 +0100645 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
646
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200647out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200648 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200649
650 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700651}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800652
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100653void
654intel_fini_pipe_control(struct intel_engine_cs *ring)
655{
656 struct drm_device *dev = ring->dev;
657
658 if (ring->scratch.obj == NULL)
659 return;
660
661 if (INTEL_INFO(dev)->gen >= 5) {
662 kunmap(sg_page(ring->scratch.obj->pages->sgl));
663 i915_gem_object_ggtt_unpin(ring->scratch.obj);
664 }
665
666 drm_gem_object_unreference(&ring->scratch.obj->base);
667 ring->scratch.obj = NULL;
668}
669
670int
671intel_init_pipe_control(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000672{
Chris Wilsonc6df5412010-12-15 09:56:50 +0000673 int ret;
674
Daniel Vetterbfc882b2014-11-20 00:33:08 +0100675 WARN_ON(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000676
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100677 ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
678 if (ring->scratch.obj == NULL) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000679 DRM_ERROR("Failed to allocate seqno page\n");
680 ret = -ENOMEM;
681 goto err;
682 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100683
Daniel Vettera9cc7262014-02-14 14:01:13 +0100684 ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
685 if (ret)
686 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000687
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100688 ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000689 if (ret)
690 goto err_unref;
691
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100692 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
693 ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
694 if (ring->scratch.cpu_page == NULL) {
Wei Yongjun56b085a2013-05-28 17:51:44 +0800695 ret = -ENOMEM;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000696 goto err_unpin;
Wei Yongjun56b085a2013-05-28 17:51:44 +0800697 }
Chris Wilsonc6df5412010-12-15 09:56:50 +0000698
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200699 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100700 ring->name, ring->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000701 return 0;
702
703err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800704 i915_gem_object_ggtt_unpin(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000705err_unref:
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100706 drm_gem_object_unreference(&ring->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000707err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000708 return ret;
709}
710
John Harrisone2be4fa2015-05-29 17:43:54 +0100711static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100712{
Mika Kuoppala72253422014-10-07 17:21:26 +0300713 int ret, i;
John Harrisone2be4fa2015-05-29 17:43:54 +0100714 struct intel_engine_cs *ring = req->ring;
Arun Siluvery888b5992014-08-26 14:44:51 +0100715 struct drm_device *dev = ring->dev;
716 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala72253422014-10-07 17:21:26 +0300717 struct i915_workarounds *w = &dev_priv->workarounds;
Arun Siluvery888b5992014-08-26 14:44:51 +0100718
Michel Thierrye6c1abb2014-11-26 14:21:02 +0000719 if (WARN_ON_ONCE(w->count == 0))
Mika Kuoppala72253422014-10-07 17:21:26 +0300720 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100721
Mika Kuoppala72253422014-10-07 17:21:26 +0300722 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100723 ret = intel_ring_flush_all_caches(req);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100724 if (ret)
725 return ret;
726
Arun Siluvery22a916a2014-10-22 18:59:52 +0100727 ret = intel_ring_begin(ring, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300728 if (ret)
729 return ret;
730
Arun Siluvery22a916a2014-10-22 18:59:52 +0100731 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300732 for (i = 0; i < w->count; i++) {
Mika Kuoppala72253422014-10-07 17:21:26 +0300733 intel_ring_emit(ring, w->reg[i].addr);
734 intel_ring_emit(ring, w->reg[i].value);
735 }
Arun Siluvery22a916a2014-10-22 18:59:52 +0100736 intel_ring_emit(ring, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300737
738 intel_ring_advance(ring);
739
740 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100741 ret = intel_ring_flush_all_caches(req);
Mika Kuoppala72253422014-10-07 17:21:26 +0300742 if (ret)
743 return ret;
744
745 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
746
747 return 0;
748}
749
John Harrison87531812015-05-29 17:43:44 +0100750static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100751{
752 int ret;
753
John Harrisone2be4fa2015-05-29 17:43:54 +0100754 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100755 if (ret != 0)
756 return ret;
757
John Harrisonbe013632015-05-29 17:43:45 +0100758 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100759 if (ret)
760 DRM_ERROR("init render state: %d\n", ret);
761
762 return ret;
763}
764
Mika Kuoppala72253422014-10-07 17:21:26 +0300765static int wa_add(struct drm_i915_private *dev_priv,
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000766 const u32 addr, const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300767{
768 const u32 idx = dev_priv->workarounds.count;
769
770 if (WARN_ON(idx >= I915_MAX_WA_REGS))
771 return -ENOSPC;
772
773 dev_priv->workarounds.reg[idx].addr = addr;
774 dev_priv->workarounds.reg[idx].value = val;
775 dev_priv->workarounds.reg[idx].mask = mask;
776
777 dev_priv->workarounds.count++;
778
779 return 0;
780}
781
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000782#define WA_REG(addr, mask, val) { \
783 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300784 if (r) \
785 return r; \
786 }
787
788#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000789 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300790
791#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000792 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300793
Damien Lespiau98533252014-12-08 17:33:51 +0000794#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000795 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300796
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000797#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
798#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300799
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000800#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300801
802static int bdw_init_workarounds(struct intel_engine_cs *ring)
803{
804 struct drm_device *dev = ring->dev;
805 struct drm_i915_private *dev_priv = dev->dev_private;
806
Ville Syrjälä9cc83022015-06-02 15:37:36 +0300807 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
808
Ville Syrjälä2441f872015-06-02 15:37:37 +0300809 /* WaDisableAsyncFlipPerfMode:bdw */
810 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
811
Arun Siluvery86d7f232014-08-26 14:44:50 +0100812 /* WaDisablePartialInstShootdown:bdw */
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700813 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Mika Kuoppala72253422014-10-07 17:21:26 +0300814 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
815 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
816 STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100817
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700818 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300819 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
820 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100821
Mika Kuoppala72253422014-10-07 17:21:26 +0300822 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
823 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100824
825 /* Use Force Non-Coherent whenever executing a 3D context. This is a
826 * workaround for for a possible hang in the unlikely event a TLB
827 * invalidation occurs during a PSD flush.
828 */
Mika Kuoppala72253422014-10-07 17:21:26 +0300829 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000830 /* WaForceEnableNonCoherent:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300831 HDC_FORCE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000832 /* WaForceContextSaveRestoreNonCoherent:bdw */
833 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
834 /* WaHdcDisableFetchWhenMasked:bdw */
Michel Thierryf3f32362014-12-04 15:07:52 +0000835 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000836 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Mika Kuoppala72253422014-10-07 17:21:26 +0300837 (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100838
Kenneth Graunke2701fc42015-01-13 12:46:52 -0800839 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
840 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
841 * polygons in the same 8x4 pixel/sample area to be processed without
842 * stalling waiting for the earlier ones to write to Hierarchical Z
843 * buffer."
844 *
845 * This optimization is off by default for Broadwell; turn it on.
846 */
847 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
848
Arun Siluvery86d7f232014-08-26 14:44:50 +0100849 /* Wa4x4STCOptimizationDisable:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300850 WA_SET_BIT_MASKED(CACHE_MODE_1,
851 GEN8_4x4_STC_OPTIMIZATION_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100852
853 /*
854 * BSpec recommends 8x4 when MSAA is used,
855 * however in practice 16x4 seems fastest.
856 *
857 * Note that PS/WM thread counts depend on the WIZ hashing
858 * disable bit, which we don't touch here, but it's good
859 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
860 */
Damien Lespiau98533252014-12-08 17:33:51 +0000861 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
862 GEN6_WIZ_HASHING_MASK,
863 GEN6_WIZ_HASHING_16x4);
Arun Siluvery888b5992014-08-26 14:44:51 +0100864
Arun Siluvery86d7f232014-08-26 14:44:50 +0100865 return 0;
866}
867
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300868static int chv_init_workarounds(struct intel_engine_cs *ring)
869{
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300870 struct drm_device *dev = ring->dev;
871 struct drm_i915_private *dev_priv = dev->dev_private;
872
Ville Syrjälä9cc83022015-06-02 15:37:36 +0300873 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
874
Ville Syrjälä2441f872015-06-02 15:37:37 +0300875 /* WaDisableAsyncFlipPerfMode:chv */
876 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
877
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300878 /* WaDisablePartialInstShootdown:chv */
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300879 /* WaDisableThreadStallDopClockGating:chv */
Mika Kuoppala72253422014-10-07 17:21:26 +0300880 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Arun Siluvery605f1432014-10-28 18:33:13 +0000881 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
882 STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300883
Arun Siluvery952890092014-10-28 18:33:14 +0000884 /* Use Force Non-Coherent whenever executing a 3D context. This is a
885 * workaround for a possible hang in the unlikely event a TLB
886 * invalidation occurs during a PSD flush.
887 */
888 /* WaForceEnableNonCoherent:chv */
889 /* WaHdcDisableFetchWhenMasked:chv */
890 WA_SET_BIT_MASKED(HDC_CHICKEN0,
891 HDC_FORCE_NON_COHERENT |
892 HDC_DONOT_FETCH_MEM_WHEN_MASKED);
893
Kenneth Graunke973a5b02015-01-13 12:46:53 -0800894 /* According to the CACHE_MODE_0 default value documentation, some
895 * CHV platforms disable this optimization by default. Turn it on.
896 */
897 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
898
Ville Syrjälä14bc16e2015-01-21 19:37:58 +0200899 /* Wa4x4STCOptimizationDisable:chv */
900 WA_SET_BIT_MASKED(CACHE_MODE_1,
901 GEN8_4x4_STC_OPTIMIZATION_DISABLE);
902
Kenneth Graunked60de812015-01-10 18:02:22 -0800903 /* Improve HiZ throughput on CHV. */
904 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
905
Ville Syrjäläe7fc2432015-01-21 19:38:00 +0200906 /*
907 * BSpec recommends 8x4 when MSAA is used,
908 * however in practice 16x4 seems fastest.
909 *
910 * Note that PS/WM thread counts depend on the WIZ hashing
911 * disable bit, which we don't touch here, but it's good
912 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
913 */
914 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
915 GEN6_WIZ_HASHING_MASK,
916 GEN6_WIZ_HASHING_16x4);
917
Mika Kuoppala72253422014-10-07 17:21:26 +0300918 return 0;
919}
920
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000921static int gen9_init_workarounds(struct intel_engine_cs *ring)
922{
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000923 struct drm_device *dev = ring->dev;
924 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak8ea6f892015-05-19 17:05:42 +0300925 uint32_t tmp;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000926
Nick Hoathb0e6f6d2015-05-07 14:15:29 +0100927 /* WaDisablePartialInstShootdown:skl,bxt */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000928 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
929 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
930
Nick Hoatha119a6e2015-05-07 14:15:30 +0100931 /* Syncing dependencies between camera and graphics:skl,bxt */
Nick Hoath84241712015-02-05 10:47:20 +0000932 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
933 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
934
Nick Hoathd2a31db2015-05-07 14:15:31 +0100935 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) == SKL_REVID_A0 ||
936 INTEL_REVID(dev) == SKL_REVID_B0)) ||
937 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
938 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
Damien Lespiaua86eb582015-02-11 18:21:44 +0000939 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
940 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000941 }
942
Nick Hoatha13d2152015-05-07 14:15:32 +0100943 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0) ||
944 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
945 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000946 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
947 GEN9_RHWO_OPTIMIZATION_DISABLE);
948 WA_SET_BIT_MASKED(GEN9_SLICE_COMMON_ECO_CHICKEN0,
949 DISABLE_PIXEL_MASK_CAMMING);
950 }
951
Nick Hoath27a1b682015-05-07 14:15:33 +0100952 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) >= SKL_REVID_C0) ||
953 IS_BROXTON(dev)) {
954 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt */
Nick Hoathcac23df2015-02-05 10:47:22 +0000955 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
956 GEN9_ENABLE_YV12_BUGFIX);
957 }
958
Nick Hoath50683682015-05-07 14:15:35 +0100959 /* Wa4x4STCOptimizationDisable:skl,bxt */
Hoath, Nicholas18404812015-02-05 10:47:23 +0000960 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
961
Nick Hoath27160c92015-05-07 14:15:36 +0100962 /* WaDisablePartialResolveInVc:skl,bxt */
Damien Lespiau9370cd92015-02-09 19:33:17 +0000963 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE);
964
Nick Hoath16be17a2015-05-07 14:15:37 +0100965 /* WaCcsTlbPrefetchDisable:skl,bxt */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000966 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
967 GEN9_CCS_TLB_PREFETCH_ENABLE);
968
Imre Deak5a2ae952015-05-19 15:04:59 +0300969 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
970 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_C0) ||
971 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200972 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
973 PIXEL_MASK_CAMMING_DISABLE);
974
Imre Deak8ea6f892015-05-19 17:05:42 +0300975 /* WaForceContextSaveRestoreNonCoherent:skl,bxt */
976 tmp = HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT;
977 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_F0) ||
978 (IS_BROXTON(dev) && INTEL_REVID(dev) >= BXT_REVID_B0))
979 tmp |= HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE;
980 WA_SET_BIT_MASKED(HDC_CHICKEN0, tmp);
981
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000982 return 0;
983}
984
Damien Lespiaub7668792015-02-14 18:30:29 +0000985static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
Damien Lespiau8d205492015-02-09 19:33:15 +0000986{
Damien Lespiaub7668792015-02-14 18:30:29 +0000987 struct drm_device *dev = ring->dev;
988 struct drm_i915_private *dev_priv = dev->dev_private;
989 u8 vals[3] = { 0, 0, 0 };
990 unsigned int i;
991
992 for (i = 0; i < 3; i++) {
993 u8 ss;
994
995 /*
996 * Only consider slices where one, and only one, subslice has 7
997 * EUs
998 */
999 if (hweight8(dev_priv->info.subslice_7eu[i]) != 1)
1000 continue;
1001
1002 /*
1003 * subslice_7eu[i] != 0 (because of the check above) and
1004 * ss_max == 4 (maximum number of subslices possible per slice)
1005 *
1006 * -> 0 <= ss <= 3;
1007 */
1008 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1009 vals[i] = 3 - ss;
1010 }
1011
1012 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1013 return 0;
1014
1015 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1016 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1017 GEN9_IZ_HASHING_MASK(2) |
1018 GEN9_IZ_HASHING_MASK(1) |
1019 GEN9_IZ_HASHING_MASK(0),
1020 GEN9_IZ_HASHING(2, vals[2]) |
1021 GEN9_IZ_HASHING(1, vals[1]) |
1022 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001023
Mika Kuoppala72253422014-10-07 17:21:26 +03001024 return 0;
1025}
1026
Damien Lespiaub7668792015-02-14 18:30:29 +00001027
Damien Lespiau8d205492015-02-09 19:33:15 +00001028static int skl_init_workarounds(struct intel_engine_cs *ring)
1029{
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001030 struct drm_device *dev = ring->dev;
1031 struct drm_i915_private *dev_priv = dev->dev_private;
1032
Damien Lespiau8d205492015-02-09 19:33:15 +00001033 gen9_init_workarounds(ring);
1034
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001035 /* WaDisablePowerCompilerClockGating:skl */
1036 if (INTEL_REVID(dev) == SKL_REVID_B0)
1037 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1038 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1039
Nick Hoathb62adbd2015-05-07 14:15:34 +01001040 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
1041 /*
1042 *Use Force Non-Coherent whenever executing a 3D context. This
1043 * is a workaround for a possible hang in the unlikely event
1044 * a TLB invalidation occurs during a PSD flush.
1045 */
1046 /* WaForceEnableNonCoherent:skl */
1047 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1048 HDC_FORCE_NON_COHERENT);
1049 }
1050
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001051 if (INTEL_REVID(dev) == SKL_REVID_C0 ||
1052 INTEL_REVID(dev) == SKL_REVID_D0)
1053 /* WaBarrierPerformanceFixDisable:skl */
1054 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1055 HDC_FENCE_DEST_SLM_DISABLE |
1056 HDC_BARRIER_PERFORMANCE_DISABLE);
1057
Damien Lespiaub7668792015-02-14 18:30:29 +00001058 return skl_tune_iz_hashing(ring);
Damien Lespiau8d205492015-02-09 19:33:15 +00001059}
1060
Nick Hoathcae04372015-03-17 11:39:38 +02001061static int bxt_init_workarounds(struct intel_engine_cs *ring)
1062{
Nick Hoathdfb601e2015-04-10 13:12:24 +01001063 struct drm_device *dev = ring->dev;
1064 struct drm_i915_private *dev_priv = dev->dev_private;
1065
Nick Hoathcae04372015-03-17 11:39:38 +02001066 gen9_init_workarounds(ring);
1067
Nick Hoathdfb601e2015-04-10 13:12:24 +01001068 /* WaDisableThreadStallDopClockGating:bxt */
1069 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1070 STALL_DOP_GATING_DISABLE);
1071
Nick Hoath983b4b92015-04-10 13:12:25 +01001072 /* WaDisableSbeCacheDispatchPortSharing:bxt */
1073 if (INTEL_REVID(dev) <= BXT_REVID_B0) {
1074 WA_SET_BIT_MASKED(
1075 GEN7_HALF_SLICE_CHICKEN1,
1076 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1077 }
1078
Nick Hoathcae04372015-03-17 11:39:38 +02001079 return 0;
1080}
1081
Michel Thierry771b9a52014-11-11 16:47:33 +00001082int init_workarounds_ring(struct intel_engine_cs *ring)
Mika Kuoppala72253422014-10-07 17:21:26 +03001083{
1084 struct drm_device *dev = ring->dev;
1085 struct drm_i915_private *dev_priv = dev->dev_private;
1086
1087 WARN_ON(ring->id != RCS);
1088
1089 dev_priv->workarounds.count = 0;
1090
1091 if (IS_BROADWELL(dev))
1092 return bdw_init_workarounds(ring);
1093
1094 if (IS_CHERRYVIEW(dev))
1095 return chv_init_workarounds(ring);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001096
Damien Lespiau8d205492015-02-09 19:33:15 +00001097 if (IS_SKYLAKE(dev))
1098 return skl_init_workarounds(ring);
Nick Hoathcae04372015-03-17 11:39:38 +02001099
1100 if (IS_BROXTON(dev))
1101 return bxt_init_workarounds(ring);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001102
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001103 return 0;
1104}
1105
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001106static int init_render_ring(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001107{
Chris Wilson78501ea2010-10-27 12:18:21 +01001108 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001109 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001110 int ret = init_ring_common(ring);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001111 if (ret)
1112 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001113
Akash Goel61a563a2014-03-25 18:01:50 +05301114 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1115 if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001116 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001117
1118 /* We need to disable the AsyncFlip performance optimisations in order
1119 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1120 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001121 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001122 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001123 */
Ville Syrjälä2441f872015-06-02 15:37:37 +03001124 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001125 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1126
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001127 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301128 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001129 if (INTEL_INFO(dev)->gen == 6)
1130 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001131 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001132
Akash Goel01fa0302014-03-24 23:00:04 +05301133 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001134 if (IS_GEN7(dev))
1135 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301136 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001137 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001138
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001139 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001140 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1141 * "If this bit is set, STCunit will have LRA as replacement
1142 * policy. [...] This bit must be reset. LRA replacement
1143 * policy is not supported."
1144 */
1145 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001146 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001147 }
1148
Ville Syrjälä9cc83022015-06-02 15:37:36 +03001149 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001150 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001151
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001152 if (HAS_L3_DPF(dev))
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001153 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001154
Mika Kuoppala72253422014-10-07 17:21:26 +03001155 return init_workarounds_ring(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001156}
1157
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001158static void render_ring_cleanup(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001159{
Daniel Vetterb45305f2012-12-17 16:21:27 +01001160 struct drm_device *dev = ring->dev;
Ben Widawsky3e789982014-06-30 09:53:37 -07001161 struct drm_i915_private *dev_priv = dev->dev_private;
1162
1163 if (dev_priv->semaphore_obj) {
1164 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1165 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1166 dev_priv->semaphore_obj = NULL;
1167 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001168
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001169 intel_fini_pipe_control(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001170}
1171
Ben Widawsky3e789982014-06-30 09:53:37 -07001172static int gen8_rcs_signal(struct intel_engine_cs *signaller,
1173 unsigned int num_dwords)
1174{
1175#define MBOX_UPDATE_DWORDS 8
1176 struct drm_device *dev = signaller->dev;
1177 struct drm_i915_private *dev_priv = dev->dev_private;
1178 struct intel_engine_cs *waiter;
1179 int i, ret, num_rings;
1180
1181 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1182 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1183#undef MBOX_UPDATE_DWORDS
1184
1185 ret = intel_ring_begin(signaller, num_dwords);
1186 if (ret)
1187 return ret;
1188
1189 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001190 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001191 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1192 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1193 continue;
1194
John Harrison6259cea2014-11-24 18:49:29 +00001195 seqno = i915_gem_request_get_seqno(
1196 signaller->outstanding_lazy_request);
Ben Widawsky3e789982014-06-30 09:53:37 -07001197 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1198 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1199 PIPE_CONTROL_QW_WRITE |
1200 PIPE_CONTROL_FLUSH_ENABLE);
1201 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1202 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001203 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001204 intel_ring_emit(signaller, 0);
1205 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1206 MI_SEMAPHORE_TARGET(waiter->id));
1207 intel_ring_emit(signaller, 0);
1208 }
1209
1210 return 0;
1211}
1212
1213static int gen8_xcs_signal(struct intel_engine_cs *signaller,
1214 unsigned int num_dwords)
1215{
1216#define MBOX_UPDATE_DWORDS 6
1217 struct drm_device *dev = signaller->dev;
1218 struct drm_i915_private *dev_priv = dev->dev_private;
1219 struct intel_engine_cs *waiter;
1220 int i, ret, num_rings;
1221
1222 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1223 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1224#undef MBOX_UPDATE_DWORDS
1225
1226 ret = intel_ring_begin(signaller, num_dwords);
1227 if (ret)
1228 return ret;
1229
1230 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001231 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001232 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1233 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1234 continue;
1235
John Harrison6259cea2014-11-24 18:49:29 +00001236 seqno = i915_gem_request_get_seqno(
1237 signaller->outstanding_lazy_request);
Ben Widawsky3e789982014-06-30 09:53:37 -07001238 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1239 MI_FLUSH_DW_OP_STOREDW);
1240 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1241 MI_FLUSH_DW_USE_GTT);
1242 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001243 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001244 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1245 MI_SEMAPHORE_TARGET(waiter->id));
1246 intel_ring_emit(signaller, 0);
1247 }
1248
1249 return 0;
1250}
1251
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001252static int gen6_signal(struct intel_engine_cs *signaller,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001253 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001254{
Ben Widawsky024a43e2014-04-29 14:52:30 -07001255 struct drm_device *dev = signaller->dev;
1256 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001257 struct intel_engine_cs *useless;
Ben Widawskya1444b72014-06-30 09:53:35 -07001258 int i, ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001259
Ben Widawskya1444b72014-06-30 09:53:35 -07001260#define MBOX_UPDATE_DWORDS 3
1261 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1262 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1263#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001264
1265 ret = intel_ring_begin(signaller, num_dwords);
1266 if (ret)
1267 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001268
Ben Widawsky78325f22014-04-29 14:52:29 -07001269 for_each_ring(useless, dev_priv, i) {
1270 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1271 if (mbox_reg != GEN6_NOSYNC) {
John Harrison6259cea2014-11-24 18:49:29 +00001272 u32 seqno = i915_gem_request_get_seqno(
1273 signaller->outstanding_lazy_request);
Ben Widawsky78325f22014-04-29 14:52:29 -07001274 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1275 intel_ring_emit(signaller, mbox_reg);
John Harrison6259cea2014-11-24 18:49:29 +00001276 intel_ring_emit(signaller, seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001277 }
1278 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001279
Ben Widawskya1444b72014-06-30 09:53:35 -07001280 /* If num_dwords was rounded, make sure the tail pointer is correct */
1281 if (num_rings % 2 == 0)
1282 intel_ring_emit(signaller, MI_NOOP);
1283
Ben Widawsky024a43e2014-04-29 14:52:30 -07001284 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001285}
1286
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001287/**
1288 * gen6_add_request - Update the semaphore mailbox registers
1289 *
1290 * @ring - ring that is adding a request
1291 * @seqno - return seqno stuck into the ring
1292 *
1293 * Update the mailbox registers in the *other* rings with the current seqno.
1294 * This acts like a signal in the canonical semaphore.
1295 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001296static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001297gen6_add_request(struct intel_engine_cs *ring)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001298{
Ben Widawsky024a43e2014-04-29 14:52:30 -07001299 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001300
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001301 if (ring->semaphore.signal)
1302 ret = ring->semaphore.signal(ring, 4);
1303 else
1304 ret = intel_ring_begin(ring, 4);
1305
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001306 if (ret)
1307 return ret;
1308
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001309 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1310 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrison6259cea2014-11-24 18:49:29 +00001311 intel_ring_emit(ring,
1312 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001313 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001314 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001315
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001316 return 0;
1317}
1318
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001319static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1320 u32 seqno)
1321{
1322 struct drm_i915_private *dev_priv = dev->dev_private;
1323 return dev_priv->last_seqno < seqno;
1324}
1325
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001326/**
1327 * intel_ring_sync - sync the waiter to the signaller on seqno
1328 *
1329 * @waiter - ring that is waiting
1330 * @signaller - ring which has, or will signal
1331 * @seqno - seqno which the waiter will block on
1332 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001333
1334static int
1335gen8_ring_sync(struct intel_engine_cs *waiter,
1336 struct intel_engine_cs *signaller,
1337 u32 seqno)
1338{
1339 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
1340 int ret;
1341
1342 ret = intel_ring_begin(waiter, 4);
1343 if (ret)
1344 return ret;
1345
1346 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1347 MI_SEMAPHORE_GLOBAL_GTT |
Ben Widawskybae4fcd2014-06-30 09:53:43 -07001348 MI_SEMAPHORE_POLL |
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001349 MI_SEMAPHORE_SAD_GTE_SDD);
1350 intel_ring_emit(waiter, seqno);
1351 intel_ring_emit(waiter,
1352 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1353 intel_ring_emit(waiter,
1354 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1355 intel_ring_advance(waiter);
1356 return 0;
1357}
1358
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001359static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001360gen6_ring_sync(struct intel_engine_cs *waiter,
1361 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001362 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001363{
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001364 u32 dw1 = MI_SEMAPHORE_MBOX |
1365 MI_SEMAPHORE_COMPARE |
1366 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001367 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1368 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001369
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001370 /* Throughout all of the GEM code, seqno passed implies our current
1371 * seqno is >= the last seqno executed. However for hardware the
1372 * comparison is strictly greater than.
1373 */
1374 seqno -= 1;
1375
Ben Widawskyebc348b2014-04-29 14:52:28 -07001376 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001377
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001378 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001379 if (ret)
1380 return ret;
1381
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001382 /* If seqno wrap happened, omit the wait with no-ops */
1383 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001384 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001385 intel_ring_emit(waiter, seqno);
1386 intel_ring_emit(waiter, 0);
1387 intel_ring_emit(waiter, MI_NOOP);
1388 } else {
1389 intel_ring_emit(waiter, MI_NOOP);
1390 intel_ring_emit(waiter, MI_NOOP);
1391 intel_ring_emit(waiter, MI_NOOP);
1392 intel_ring_emit(waiter, MI_NOOP);
1393 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001394 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001395
1396 return 0;
1397}
1398
Chris Wilsonc6df5412010-12-15 09:56:50 +00001399#define PIPE_CONTROL_FLUSH(ring__, addr__) \
1400do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001401 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1402 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +00001403 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1404 intel_ring_emit(ring__, 0); \
1405 intel_ring_emit(ring__, 0); \
1406} while (0)
1407
1408static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001409pc_render_add_request(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001410{
Chris Wilson18393f62014-04-09 09:19:40 +01001411 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001412 int ret;
1413
1414 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1415 * incoherent with writes to memory, i.e. completely fubar,
1416 * so we need to use PIPE_NOTIFY instead.
1417 *
1418 * However, we also need to workaround the qword write
1419 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1420 * memory before requesting an interrupt.
1421 */
1422 ret = intel_ring_begin(ring, 32);
1423 if (ret)
1424 return ret;
1425
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001426 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001427 PIPE_CONTROL_WRITE_FLUSH |
1428 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001429 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrison6259cea2014-11-24 18:49:29 +00001430 intel_ring_emit(ring,
1431 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001432 intel_ring_emit(ring, 0);
1433 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001434 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +00001435 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001436 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001437 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001438 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001439 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001440 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001441 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001442 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001443 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001444
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001445 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001446 PIPE_CONTROL_WRITE_FLUSH |
1447 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001448 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001449 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrison6259cea2014-11-24 18:49:29 +00001450 intel_ring_emit(ring,
1451 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001452 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +01001453 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001454
Chris Wilsonc6df5412010-12-15 09:56:50 +00001455 return 0;
1456}
1457
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001458static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001459gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001460{
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001461 /* Workaround to force correct ordering between irq and seqno writes on
1462 * ivb (and maybe also on snb) by reading from a CS register (like
1463 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +00001464 if (!lazy_coherency) {
1465 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1466 POSTING_READ(RING_ACTHD(ring->mmio_base));
1467 }
1468
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001469 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1470}
1471
1472static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001473ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001474{
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001475 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1476}
1477
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001478static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001479ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001480{
1481 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1482}
1483
Chris Wilsonc6df5412010-12-15 09:56:50 +00001484static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001485pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001486{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001487 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001488}
1489
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001490static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001491pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001492{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001493 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001494}
1495
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001496static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001497gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001498{
1499 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001500 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001501 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001502
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001503 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Daniel Vettere48d8632012-04-11 22:12:54 +02001504 return false;
1505
Chris Wilson7338aef2012-04-24 21:48:47 +01001506 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001507 if (ring->irq_refcount++ == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001508 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001509 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001510
1511 return true;
1512}
1513
1514static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001515gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001516{
1517 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001518 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001519 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001520
Chris Wilson7338aef2012-04-24 21:48:47 +01001521 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001522 if (--ring->irq_refcount == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001523 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001524 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001525}
1526
1527static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001528i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001529{
Chris Wilson78501ea2010-10-27 12:18:21 +01001530 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001531 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001532 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001533
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001534 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001535 return false;
1536
Chris Wilson7338aef2012-04-24 21:48:47 +01001537 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001538 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001539 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1540 I915_WRITE(IMR, dev_priv->irq_mask);
1541 POSTING_READ(IMR);
1542 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001543 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001544
1545 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001546}
1547
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001548static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001549i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001550{
Chris Wilson78501ea2010-10-27 12:18:21 +01001551 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001552 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001553 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001554
Chris Wilson7338aef2012-04-24 21:48:47 +01001555 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001556 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001557 dev_priv->irq_mask |= ring->irq_enable_mask;
1558 I915_WRITE(IMR, dev_priv->irq_mask);
1559 POSTING_READ(IMR);
1560 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001561 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001562}
1563
Chris Wilsonc2798b12012-04-22 21:13:57 +01001564static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001565i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001566{
1567 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001568 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001569 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001570
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001571 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonc2798b12012-04-22 21:13:57 +01001572 return false;
1573
Chris Wilson7338aef2012-04-24 21:48:47 +01001574 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001575 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001576 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1577 I915_WRITE16(IMR, dev_priv->irq_mask);
1578 POSTING_READ16(IMR);
1579 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001580 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001581
1582 return true;
1583}
1584
1585static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001586i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001587{
1588 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001589 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001590 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001591
Chris Wilson7338aef2012-04-24 21:48:47 +01001592 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001593 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001594 dev_priv->irq_mask |= ring->irq_enable_mask;
1595 I915_WRITE16(IMR, dev_priv->irq_mask);
1596 POSTING_READ16(IMR);
1597 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001598 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001599}
1600
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001601static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001602bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001603 u32 invalidate_domains,
1604 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001605{
John Harrisona84c3ae2015-05-29 17:43:57 +01001606 struct intel_engine_cs *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001607 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001608
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001609 ret = intel_ring_begin(ring, 2);
1610 if (ret)
1611 return ret;
1612
1613 intel_ring_emit(ring, MI_FLUSH);
1614 intel_ring_emit(ring, MI_NOOP);
1615 intel_ring_advance(ring);
1616 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001617}
1618
Chris Wilson3cce4692010-10-27 16:11:02 +01001619static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001620i9xx_add_request(struct intel_engine_cs *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001621{
Chris Wilson3cce4692010-10-27 16:11:02 +01001622 int ret;
1623
1624 ret = intel_ring_begin(ring, 4);
1625 if (ret)
1626 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001627
Chris Wilson3cce4692010-10-27 16:11:02 +01001628 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1629 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrison6259cea2014-11-24 18:49:29 +00001630 intel_ring_emit(ring,
1631 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilson3cce4692010-10-27 16:11:02 +01001632 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001633 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001634
Chris Wilson3cce4692010-10-27 16:11:02 +01001635 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001636}
1637
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001638static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001639gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001640{
1641 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001642 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001643 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001644
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001645 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1646 return false;
Chris Wilson0f468322011-01-04 17:35:21 +00001647
Chris Wilson7338aef2012-04-24 21:48:47 +01001648 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001649 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001650 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001651 I915_WRITE_IMR(ring,
1652 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001653 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001654 else
1655 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001656 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001657 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001658 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001659
1660 return true;
1661}
1662
1663static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001664gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001665{
1666 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001667 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001668 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001669
Chris Wilson7338aef2012-04-24 21:48:47 +01001670 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001671 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001672 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001673 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001674 else
1675 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001676 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001677 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001678 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001679}
1680
Ben Widawskya19d2932013-05-28 19:22:30 -07001681static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001682hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001683{
1684 struct drm_device *dev = ring->dev;
1685 struct drm_i915_private *dev_priv = dev->dev_private;
1686 unsigned long flags;
1687
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001688 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskya19d2932013-05-28 19:22:30 -07001689 return false;
1690
Daniel Vetter59cdb632013-07-04 23:35:28 +02001691 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001692 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001693 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001694 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001695 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001696 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001697
1698 return true;
1699}
1700
1701static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001702hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001703{
1704 struct drm_device *dev = ring->dev;
1705 struct drm_i915_private *dev_priv = dev->dev_private;
1706 unsigned long flags;
1707
Daniel Vetter59cdb632013-07-04 23:35:28 +02001708 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001709 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001710 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001711 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001712 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001713 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001714}
1715
Ben Widawskyabd58f02013-11-02 21:07:09 -07001716static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001717gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001718{
1719 struct drm_device *dev = ring->dev;
1720 struct drm_i915_private *dev_priv = dev->dev_private;
1721 unsigned long flags;
1722
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001723 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskyabd58f02013-11-02 21:07:09 -07001724 return false;
1725
1726 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1727 if (ring->irq_refcount++ == 0) {
1728 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1729 I915_WRITE_IMR(ring,
1730 ~(ring->irq_enable_mask |
1731 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1732 } else {
1733 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1734 }
1735 POSTING_READ(RING_IMR(ring->mmio_base));
1736 }
1737 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1738
1739 return true;
1740}
1741
1742static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001743gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001744{
1745 struct drm_device *dev = ring->dev;
1746 struct drm_i915_private *dev_priv = dev->dev_private;
1747 unsigned long flags;
1748
1749 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1750 if (--ring->irq_refcount == 0) {
1751 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1752 I915_WRITE_IMR(ring,
1753 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1754 } else {
1755 I915_WRITE_IMR(ring, ~0);
1756 }
1757 POSTING_READ(RING_IMR(ring->mmio_base));
1758 }
1759 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1760}
1761
Zou Nan haid1b851f2010-05-21 09:08:57 +08001762static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001763i965_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001764 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001765 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001766{
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001767 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001768
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001769 ret = intel_ring_begin(ring, 2);
1770 if (ret)
1771 return ret;
1772
Chris Wilson78501ea2010-10-27 12:18:21 +01001773 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001774 MI_BATCH_BUFFER_START |
1775 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001776 (dispatch_flags & I915_DISPATCH_SECURE ?
1777 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001778 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001779 intel_ring_advance(ring);
1780
Zou Nan haid1b851f2010-05-21 09:08:57 +08001781 return 0;
1782}
1783
Daniel Vetterb45305f2012-12-17 16:21:27 +01001784/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1785#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001786#define I830_TLB_ENTRIES (2)
1787#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001788static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001789i830_dispatch_execbuffer(struct intel_engine_cs *ring,
John Harrison8e004ef2015-02-13 11:48:10 +00001790 u64 offset, u32 len,
1791 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001792{
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001793 u32 cs_offset = ring->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001794 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001795
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001796 ret = intel_ring_begin(ring, 6);
1797 if (ret)
1798 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001799
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001800 /* Evict the invalid PTE TLBs */
1801 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1802 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1803 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1804 intel_ring_emit(ring, cs_offset);
1805 intel_ring_emit(ring, 0xdeadbeef);
1806 intel_ring_emit(ring, MI_NOOP);
1807 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001808
John Harrison8e004ef2015-02-13 11:48:10 +00001809 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001810 if (len > I830_BATCH_LIMIT)
1811 return -ENOSPC;
1812
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001813 ret = intel_ring_begin(ring, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001814 if (ret)
1815 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001816
1817 /* Blit the batch (which has now all relocs applied) to the
1818 * stable batch scratch bo area (so that the CS never
1819 * stumbles over its tlb invalidation bug) ...
1820 */
1821 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1822 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilson611a7a42014-09-12 07:37:42 +01001823 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001824 intel_ring_emit(ring, cs_offset);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001825 intel_ring_emit(ring, 4096);
1826 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001827
Daniel Vetterb45305f2012-12-17 16:21:27 +01001828 intel_ring_emit(ring, MI_FLUSH);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001829 intel_ring_emit(ring, MI_NOOP);
1830 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001831
1832 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001833 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001834 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001835
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001836 ret = intel_ring_begin(ring, 4);
1837 if (ret)
1838 return ret;
1839
1840 intel_ring_emit(ring, MI_BATCH_BUFFER);
John Harrison8e004ef2015-02-13 11:48:10 +00001841 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1842 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001843 intel_ring_emit(ring, offset + len - 8);
1844 intel_ring_emit(ring, MI_NOOP);
1845 intel_ring_advance(ring);
1846
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001847 return 0;
1848}
1849
1850static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001851i915_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001852 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001853 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001854{
1855 int ret;
1856
1857 ret = intel_ring_begin(ring, 2);
1858 if (ret)
1859 return ret;
1860
Chris Wilson65f56872012-04-17 16:38:12 +01001861 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
John Harrison8e004ef2015-02-13 11:48:10 +00001862 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1863 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001864 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001865
Eric Anholt62fdfea2010-05-21 13:26:39 -07001866 return 0;
1867}
1868
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001869static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001870{
Chris Wilson05394f32010-11-08 19:18:58 +00001871 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001872
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001873 obj = ring->status_page.obj;
1874 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001875 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001876
Chris Wilson9da3da62012-06-01 15:20:22 +01001877 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001878 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001879 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001880 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001881}
1882
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001883static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001884{
Chris Wilson05394f32010-11-08 19:18:58 +00001885 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001886
Chris Wilsone3efda42014-04-09 09:19:41 +01001887 if ((obj = ring->status_page.obj) == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001888 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001889 int ret;
1890
1891 obj = i915_gem_alloc_object(ring->dev, 4096);
1892 if (obj == NULL) {
1893 DRM_ERROR("Failed to allocate status page\n");
1894 return -ENOMEM;
1895 }
1896
1897 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1898 if (ret)
1899 goto err_unref;
1900
Chris Wilson1f767e02014-07-03 17:33:03 -04001901 flags = 0;
1902 if (!HAS_LLC(ring->dev))
1903 /* On g33, we cannot place HWS above 256MiB, so
1904 * restrict its pinning to the low mappable arena.
1905 * Though this restriction is not documented for
1906 * gen4, gen5, or byt, they also behave similarly
1907 * and hang if the HWS is placed at the top of the
1908 * GTT. To generalise, it appears that all !llc
1909 * platforms have issues with us placing the HWS
1910 * above the mappable region (even though we never
1911 * actualy map it).
1912 */
1913 flags |= PIN_MAPPABLE;
1914 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001915 if (ret) {
1916err_unref:
1917 drm_gem_object_unreference(&obj->base);
1918 return ret;
1919 }
1920
1921 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001922 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001923
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001924 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001925 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001926 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001927
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001928 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1929 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001930
1931 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001932}
1933
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001934static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001935{
1936 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001937
1938 if (!dev_priv->status_page_dmah) {
1939 dev_priv->status_page_dmah =
1940 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1941 if (!dev_priv->status_page_dmah)
1942 return -ENOMEM;
1943 }
1944
Chris Wilson6b8294a2012-11-16 11:43:20 +00001945 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1946 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1947
1948 return 0;
1949}
1950
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001951void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1952{
1953 iounmap(ringbuf->virtual_start);
1954 ringbuf->virtual_start = NULL;
1955 i915_gem_object_ggtt_unpin(ringbuf->obj);
1956}
1957
1958int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
1959 struct intel_ringbuffer *ringbuf)
1960{
1961 struct drm_i915_private *dev_priv = to_i915(dev);
1962 struct drm_i915_gem_object *obj = ringbuf->obj;
1963 int ret;
1964
1965 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1966 if (ret)
1967 return ret;
1968
1969 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1970 if (ret) {
1971 i915_gem_object_ggtt_unpin(obj);
1972 return ret;
1973 }
1974
1975 ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
1976 i915_gem_obj_ggtt_offset(obj), ringbuf->size);
1977 if (ringbuf->virtual_start == NULL) {
1978 i915_gem_object_ggtt_unpin(obj);
1979 return -EINVAL;
1980 }
1981
1982 return 0;
1983}
1984
Oscar Mateo84c23772014-07-24 17:04:15 +01001985void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01001986{
Oscar Mateo2919d292014-07-03 16:28:02 +01001987 drm_gem_object_unreference(&ringbuf->obj->base);
1988 ringbuf->obj = NULL;
1989}
1990
Oscar Mateo84c23772014-07-24 17:04:15 +01001991int intel_alloc_ringbuffer_obj(struct drm_device *dev,
1992 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01001993{
Chris Wilsone3efda42014-04-09 09:19:41 +01001994 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01001995
1996 obj = NULL;
1997 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001998 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01001999 if (obj == NULL)
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002000 obj = i915_gem_alloc_object(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002001 if (obj == NULL)
2002 return -ENOMEM;
2003
Akash Goel24f3a8c2014-06-17 10:59:42 +05302004 /* mark ring buffers as read-only from GPU side by default */
2005 obj->gt_ro = 1;
2006
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002007 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002008
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002009 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002010}
2011
Ben Widawskyc43b5632012-04-16 14:07:40 -07002012static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002013 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002014{
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002015 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002016 int ret;
2017
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002018 WARN_ON(ring->buffer);
2019
2020 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2021 if (!ringbuf)
2022 return -ENOMEM;
2023 ring->buffer = ringbuf;
Oscar Mateo8ee14972014-05-22 14:13:34 +01002024
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002025 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01002026 INIT_LIST_HEAD(&ring->active_list);
2027 INIT_LIST_HEAD(&ring->request_list);
Oscar Mateocc9130b2014-07-24 17:04:42 +01002028 INIT_LIST_HEAD(&ring->execlist_queue);
Chris Wilson06fbca72015-04-07 16:20:36 +01002029 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002030 ringbuf->size = 32 * PAGE_SIZE;
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002031 ringbuf->ring = ring;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002032 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002033
Chris Wilsonb259f672011-03-29 13:19:09 +01002034 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002035
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002036 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002037 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002038 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002039 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002040 } else {
2041 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002042 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002043 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002044 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002045 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002046
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002047 WARN_ON(ringbuf->obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002048
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002049 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2050 if (ret) {
2051 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
2052 ring->name, ret);
2053 goto error;
2054 }
2055
2056 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2057 if (ret) {
2058 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2059 ring->name, ret);
2060 intel_destroy_ringbuffer_obj(ringbuf);
2061 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002062 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002063
Chris Wilson55249ba2010-12-22 14:04:47 +00002064 /* Workaround an erratum on the i830 which causes a hang if
2065 * the TAIL pointer points to within the last 2 cachelines
2066 * of the buffer.
2067 */
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002068 ringbuf->effective_size = ringbuf->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01002069 if (IS_I830(dev) || IS_845G(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002070 ringbuf->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00002071
Brad Volkin44e895a2014-05-10 14:10:43 -07002072 ret = i915_cmd_parser_init_ring(ring);
2073 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002074 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002075
Oscar Mateo8ee14972014-05-22 14:13:34 +01002076 return 0;
2077
2078error:
2079 kfree(ringbuf);
2080 ring->buffer = NULL;
2081 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002082}
2083
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002084void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002085{
John Harrison6402c332014-10-31 12:00:26 +00002086 struct drm_i915_private *dev_priv;
2087 struct intel_ringbuffer *ringbuf;
Chris Wilson33626e62010-10-29 16:18:36 +01002088
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002089 if (!intel_ring_initialized(ring))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002090 return;
2091
John Harrison6402c332014-10-31 12:00:26 +00002092 dev_priv = to_i915(ring->dev);
2093 ringbuf = ring->buffer;
2094
Chris Wilsone3efda42014-04-09 09:19:41 +01002095 intel_stop_ring_buffer(ring);
Ville Syrjäläde8f0a52014-05-28 19:12:13 +03002096 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002097
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002098 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateo2919d292014-07-03 16:28:02 +01002099 intel_destroy_ringbuffer_obj(ringbuf);
John Harrison6259cea2014-11-24 18:49:29 +00002100 i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
Chris Wilson78501ea2010-10-27 12:18:21 +01002101
Zou Nan hai8d192152010-11-02 16:31:01 +08002102 if (ring->cleanup)
2103 ring->cleanup(ring);
2104
Chris Wilson78501ea2010-10-27 12:18:21 +01002105 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07002106
2107 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01002108 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002109
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002110 kfree(ringbuf);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002111 ring->buffer = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002112}
2113
Chris Wilson595e1ee2015-04-07 16:20:51 +01002114static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002115{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002116 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002117 struct drm_i915_gem_request *request;
Chris Wilsonb4716182015-04-27 13:41:17 +01002118 unsigned space;
2119 int ret;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002120
John Harrison29b1b412015-06-18 13:10:09 +01002121 /* The whole point of reserving space is to not wait! */
2122 WARN_ON(ringbuf->reserved_in_use);
2123
Dave Gordonebd0fd42014-11-27 11:22:49 +00002124 if (intel_ring_space(ringbuf) >= n)
2125 return 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002126
2127 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002128 space = __intel_ring_space(request->postfix, ringbuf->tail,
2129 ringbuf->size);
2130 if (space >= n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002131 break;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002132 }
2133
Chris Wilson595e1ee2015-04-07 16:20:51 +01002134 if (WARN_ON(&request->list == &ring->request_list))
Chris Wilsona71d8d92012-02-15 11:25:36 +00002135 return -ENOSPC;
2136
Daniel Vettera4b3a572014-11-26 14:17:05 +01002137 ret = i915_wait_request(request);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002138 if (ret)
2139 return ret;
2140
Chris Wilsonb4716182015-04-27 13:41:17 +01002141 ringbuf->space = space;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002142 return 0;
2143}
2144
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002145static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002146{
2147 uint32_t __iomem *virt;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002148 struct intel_ringbuffer *ringbuf = ring->buffer;
2149 int rem = ringbuf->size - ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002150
John Harrison29b1b412015-06-18 13:10:09 +01002151 /* Can't wrap if space has already been reserved! */
2152 WARN_ON(ringbuf->reserved_in_use);
2153
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002154 if (ringbuf->space < rem) {
Chris Wilson3e960502012-11-27 16:22:54 +00002155 int ret = ring_wait_for_space(ring, rem);
2156 if (ret)
2157 return ret;
2158 }
2159
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002160 virt = ringbuf->virtual_start + ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002161 rem /= 4;
2162 while (rem--)
2163 iowrite32(MI_NOOP, virt++);
2164
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002165 ringbuf->tail = 0;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002166 intel_ring_update_space(ringbuf);
Chris Wilson3e960502012-11-27 16:22:54 +00002167
2168 return 0;
2169}
2170
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002171int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002172{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002173 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002174
2175 /* We need to add any requests required to flush the objects and ring */
John Harrison75289872015-05-29 17:43:49 +01002176 WARN_ON(ring->outstanding_lazy_request);
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002177 if (ring->outstanding_lazy_request)
John Harrison75289872015-05-29 17:43:49 +01002178 i915_add_request(ring->outstanding_lazy_request);
Chris Wilson3e960502012-11-27 16:22:54 +00002179
2180 /* Wait upon the last request to be completed */
2181 if (list_empty(&ring->request_list))
2182 return 0;
2183
Daniel Vettera4b3a572014-11-26 14:17:05 +01002184 req = list_entry(ring->request_list.prev,
Chris Wilsonb4716182015-04-27 13:41:17 +01002185 struct drm_i915_gem_request,
2186 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002187
Chris Wilsonb4716182015-04-27 13:41:17 +01002188 /* Make sure we do not trigger any retires */
2189 return __i915_wait_request(req,
2190 atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
2191 to_i915(ring->dev)->mm.interruptible,
2192 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002193}
2194
John Harrison6689cb22015-03-19 12:30:08 +00002195int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002196{
John Harrison6689cb22015-03-19 12:30:08 +00002197 request->ringbuf = request->ring->buffer;
John Harrison9eba5d42014-11-24 18:49:23 +00002198 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002199}
2200
John Harrison29b1b412015-06-18 13:10:09 +01002201void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2202{
2203 /* NB: Until request management is fully tidied up and the OLR is
2204 * removed, there are too many ways for get false hits on this
2205 * anti-recursion check! */
2206 /*WARN_ON(ringbuf->reserved_size);*/
2207 WARN_ON(ringbuf->reserved_in_use);
2208
2209 ringbuf->reserved_size = size;
2210
2211 /*
2212 * Really need to call _begin() here but that currently leads to
2213 * recursion problems! This will be fixed later but for now just
2214 * return and hope for the best. Note that there is only a real
2215 * problem if the create of the request never actually calls _begin()
2216 * but if they are not submitting any work then why did they create
2217 * the request in the first place?
2218 */
2219}
2220
2221void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2222{
2223 WARN_ON(ringbuf->reserved_in_use);
2224
2225 ringbuf->reserved_size = 0;
2226 ringbuf->reserved_in_use = false;
2227}
2228
2229void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2230{
2231 WARN_ON(ringbuf->reserved_in_use);
2232
2233 ringbuf->reserved_in_use = true;
2234 ringbuf->reserved_tail = ringbuf->tail;
2235}
2236
2237void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2238{
2239 WARN_ON(!ringbuf->reserved_in_use);
2240 WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2241 "request reserved size too small: %d vs %d!\n",
2242 ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2243
2244 ringbuf->reserved_size = 0;
2245 ringbuf->reserved_in_use = false;
2246}
2247
2248static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002249{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002250 struct intel_ringbuffer *ringbuf = ring->buffer;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002251 int ret;
2252
John Harrison29b1b412015-06-18 13:10:09 +01002253 /*
2254 * Add on the reserved size to the request to make sure that after
2255 * the intended commands have been emitted, there is guaranteed to
2256 * still be enough free space to send them to the hardware.
2257 */
2258 if (!ringbuf->reserved_in_use)
2259 bytes += ringbuf->reserved_size;
2260
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002261 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002262 ret = intel_wrap_ring_buffer(ring);
2263 if (unlikely(ret))
2264 return ret;
John Harrison29b1b412015-06-18 13:10:09 +01002265
2266 if(ringbuf->reserved_size) {
2267 uint32_t size = ringbuf->reserved_size;
2268
2269 intel_ring_reserved_space_cancel(ringbuf);
2270 intel_ring_reserved_space_reserve(ringbuf, size);
2271 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002272 }
2273
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002274 if (unlikely(ringbuf->space < bytes)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002275 ret = ring_wait_for_space(ring, bytes);
2276 if (unlikely(ret))
2277 return ret;
2278 }
2279
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002280 return 0;
2281}
2282
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002283int intel_ring_begin(struct intel_engine_cs *ring,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002284 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002285{
John Harrison217e46b2015-05-29 17:43:29 +01002286 struct drm_i915_gem_request *req;
Jani Nikula4640c4f2014-03-31 14:27:19 +03002287 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002288 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01002289
Daniel Vetter33196de2012-11-14 17:14:05 +01002290 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2291 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02002292 if (ret)
2293 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00002294
Chris Wilson304d6952014-01-02 14:32:35 +00002295 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
2296 if (ret)
2297 return ret;
2298
Chris Wilson9d7730912012-11-27 16:22:52 +00002299 /* Preallocate the olr before touching the ring */
John Harrison217e46b2015-05-29 17:43:29 +01002300 ret = i915_gem_request_alloc(ring, ring->default_context, &req);
Chris Wilson9d7730912012-11-27 16:22:52 +00002301 if (ret)
2302 return ret;
2303
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002304 ring->buffer->space -= num_dwords * sizeof(uint32_t);
Chris Wilson304d6952014-01-02 14:32:35 +00002305 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002306}
2307
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002308/* Align the ring tail to a cacheline boundary */
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002309int intel_ring_cacheline_align(struct intel_engine_cs *ring)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002310{
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002311 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002312 int ret;
2313
2314 if (num_dwords == 0)
2315 return 0;
2316
Chris Wilson18393f62014-04-09 09:19:40 +01002317 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002318 ret = intel_ring_begin(ring, num_dwords);
2319 if (ret)
2320 return ret;
2321
2322 while (num_dwords--)
2323 intel_ring_emit(ring, MI_NOOP);
2324
2325 intel_ring_advance(ring);
2326
2327 return 0;
2328}
2329
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002330void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002331{
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002332 struct drm_device *dev = ring->dev;
2333 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002334
John Harrison6259cea2014-11-24 18:49:29 +00002335 BUG_ON(ring->outstanding_lazy_request);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002336
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002337 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002338 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2339 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002340 if (HAS_VEBOX(dev))
Ben Widawsky50201502013-08-12 16:53:03 -07002341 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002342 }
Chris Wilson297b0c52010-10-22 17:02:41 +01002343
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002344 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03002345 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01002346}
2347
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002348static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002349 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002350{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002351 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002352
2353 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002354
Chris Wilson12f55812012-07-05 17:14:01 +01002355 /* Disable notification that the ring is IDLE. The GT
2356 * will then assume that it is busy and bring it out of rc6.
2357 */
2358 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2359 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2360
2361 /* Clear the context id. Here be magic! */
2362 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2363
2364 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04002365 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01002366 GEN6_BSD_SLEEP_INDICATOR) == 0,
2367 50))
2368 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002369
Chris Wilson12f55812012-07-05 17:14:01 +01002370 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04002371 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01002372 POSTING_READ(RING_TAIL(ring->mmio_base));
2373
2374 /* Let the ring send IDLE messages to the GT again,
2375 * and so let it sleep to conserve power when idle.
2376 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002377 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01002378 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002379}
2380
John Harrisona84c3ae2015-05-29 17:43:57 +01002381static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002382 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002383{
John Harrisona84c3ae2015-05-29 17:43:57 +01002384 struct intel_engine_cs *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002385 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002386 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002387
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002388 ret = intel_ring_begin(ring, 4);
2389 if (ret)
2390 return ret;
2391
Chris Wilson71a77e02011-02-02 12:13:49 +00002392 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002393 if (INTEL_INFO(ring->dev)->gen >= 8)
2394 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002395
2396 /* We always require a command barrier so that subsequent
2397 * commands, such as breadcrumb interrupts, are strictly ordered
2398 * wrt the contents of the write cache being flushed to memory
2399 * (and thus being coherent from the CPU).
2400 */
2401 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2402
Jesse Barnes9a289772012-10-26 09:42:42 -07002403 /*
2404 * Bspec vol 1c.5 - video engine command streamer:
2405 * "If ENABLED, all TLBs will be invalidated once the flush
2406 * operation is complete. This bit is only valid when the
2407 * Post-Sync Operation field is a value of 1h or 3h."
2408 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002409 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002410 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2411
Chris Wilson71a77e02011-02-02 12:13:49 +00002412 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002413 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002414 if (INTEL_INFO(ring->dev)->gen >= 8) {
2415 intel_ring_emit(ring, 0); /* upper addr */
2416 intel_ring_emit(ring, 0); /* value */
2417 } else {
2418 intel_ring_emit(ring, 0);
2419 intel_ring_emit(ring, MI_NOOP);
2420 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002421 intel_ring_advance(ring);
2422 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002423}
2424
2425static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002426gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002427 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002428 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002429{
John Harrison8e004ef2015-02-13 11:48:10 +00002430 bool ppgtt = USES_PPGTT(ring->dev) &&
2431 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002432 int ret;
2433
2434 ret = intel_ring_begin(ring, 4);
2435 if (ret)
2436 return ret;
2437
2438 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07002439 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002440 intel_ring_emit(ring, lower_32_bits(offset));
2441 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002442 intel_ring_emit(ring, MI_NOOP);
2443 intel_ring_advance(ring);
2444
2445 return 0;
2446}
2447
2448static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002449hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
John Harrison8e004ef2015-02-13 11:48:10 +00002450 u64 offset, u32 len,
2451 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002452{
Akshay Joshi0206e352011-08-16 15:34:10 -04002453 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002454
Akshay Joshi0206e352011-08-16 15:34:10 -04002455 ret = intel_ring_begin(ring, 2);
2456 if (ret)
2457 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002458
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002459 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002460 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002461 (dispatch_flags & I915_DISPATCH_SECURE ?
Chris Wilson77072252014-09-10 12:18:27 +01002462 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002463 /* bit0-7 is the length on GEN6+ */
2464 intel_ring_emit(ring, offset);
2465 intel_ring_advance(ring);
2466
2467 return 0;
2468}
2469
2470static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002471gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002472 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002473 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002474{
2475 int ret;
2476
2477 ret = intel_ring_begin(ring, 2);
2478 if (ret)
2479 return ret;
2480
2481 intel_ring_emit(ring,
2482 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002483 (dispatch_flags & I915_DISPATCH_SECURE ?
2484 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002485 /* bit0-7 is the length on GEN6+ */
2486 intel_ring_emit(ring, offset);
2487 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002488
Akshay Joshi0206e352011-08-16 15:34:10 -04002489 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002490}
2491
Chris Wilson549f7362010-10-19 11:19:32 +01002492/* Blitter support (SandyBridge+) */
2493
John Harrisona84c3ae2015-05-29 17:43:57 +01002494static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002495 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002496{
John Harrisona84c3ae2015-05-29 17:43:57 +01002497 struct intel_engine_cs *ring = req->ring;
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002498 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00002499 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002500 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002501
Daniel Vetter6a233c72011-12-14 13:57:07 +01002502 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002503 if (ret)
2504 return ret;
2505
Chris Wilson71a77e02011-02-02 12:13:49 +00002506 cmd = MI_FLUSH_DW;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002507 if (INTEL_INFO(dev)->gen >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002508 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002509
2510 /* We always require a command barrier so that subsequent
2511 * commands, such as breadcrumb interrupts, are strictly ordered
2512 * wrt the contents of the write cache being flushed to memory
2513 * (and thus being coherent from the CPU).
2514 */
2515 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2516
Jesse Barnes9a289772012-10-26 09:42:42 -07002517 /*
2518 * Bspec vol 1c.3 - blitter engine command streamer:
2519 * "If ENABLED, all TLBs will be invalidated once the flush
2520 * operation is complete. This bit is only valid when the
2521 * Post-Sync Operation field is a value of 1h or 3h."
2522 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002523 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002524 cmd |= MI_INVALIDATE_TLB;
Chris Wilson71a77e02011-02-02 12:13:49 +00002525 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002526 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002527 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002528 intel_ring_emit(ring, 0); /* upper addr */
2529 intel_ring_emit(ring, 0); /* value */
2530 } else {
2531 intel_ring_emit(ring, 0);
2532 intel_ring_emit(ring, MI_NOOP);
2533 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002534 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002535
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002536 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002537}
2538
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002539int intel_init_render_ring_buffer(struct drm_device *dev)
2540{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002541 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002542 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002543 struct drm_i915_gem_object *obj;
2544 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002545
Daniel Vetter59465b52012-04-11 22:12:48 +02002546 ring->name = "render ring";
2547 ring->id = RCS;
2548 ring->mmio_base = RENDER_RING_BASE;
2549
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002550 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002551 if (i915_semaphore_is_enabled(dev)) {
2552 obj = i915_gem_alloc_object(dev, 4096);
2553 if (obj == NULL) {
2554 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2555 i915.semaphores = 0;
2556 } else {
2557 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2558 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2559 if (ret != 0) {
2560 drm_gem_object_unreference(&obj->base);
2561 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2562 i915.semaphores = 0;
2563 } else
2564 dev_priv->semaphore_obj = obj;
2565 }
2566 }
Mika Kuoppala72253422014-10-07 17:21:26 +03002567
Daniel Vetter8f0e2b92014-12-02 16:19:07 +01002568 ring->init_context = intel_rcs_ctx_init;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002569 ring->add_request = gen6_add_request;
2570 ring->flush = gen8_render_ring_flush;
2571 ring->irq_get = gen8_ring_get_irq;
2572 ring->irq_put = gen8_ring_put_irq;
2573 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2574 ring->get_seqno = gen6_ring_get_seqno;
2575 ring->set_seqno = ring_set_seqno;
2576 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002577 WARN_ON(!dev_priv->semaphore_obj);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002578 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002579 ring->semaphore.signal = gen8_rcs_signal;
2580 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002581 }
2582 } else if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002583 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03002584 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01002585 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03002586 ring->flush = gen6_render_ring_flush;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002587 ring->irq_get = gen6_ring_get_irq;
2588 ring->irq_put = gen6_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002589 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01002590 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002591 ring->set_seqno = ring_set_seqno;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002592 if (i915_semaphore_is_enabled(dev)) {
2593 ring->semaphore.sync_to = gen6_ring_sync;
2594 ring->semaphore.signal = gen6_signal;
2595 /*
2596 * The current semaphore is only applied on pre-gen8
2597 * platform. And there is no VCS2 ring on the pre-gen8
2598 * platform. So the semaphore between RCS and VCS2 is
2599 * initialized as INVALID. Gen8 will initialize the
2600 * sema between VCS2 and RCS later.
2601 */
2602 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2603 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2604 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2605 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2606 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2607 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2608 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2609 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2610 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2611 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2612 }
Chris Wilsonc6df5412010-12-15 09:56:50 +00002613 } else if (IS_GEN5(dev)) {
2614 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002615 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00002616 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002617 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002618 ring->irq_get = gen5_ring_get_irq;
2619 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002620 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2621 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002622 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002623 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002624 if (INTEL_INFO(dev)->gen < 4)
2625 ring->flush = gen2_render_ring_flush;
2626 else
2627 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002628 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002629 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002630 if (IS_GEN2(dev)) {
2631 ring->irq_get = i8xx_ring_get_irq;
2632 ring->irq_put = i8xx_ring_put_irq;
2633 } else {
2634 ring->irq_get = i9xx_ring_get_irq;
2635 ring->irq_put = i9xx_ring_put_irq;
2636 }
Daniel Vettere3670312012-04-11 22:12:53 +02002637 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002638 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002639 ring->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002640
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002641 if (IS_HASWELL(dev))
2642 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002643 else if (IS_GEN8(dev))
2644 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002645 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002646 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2647 else if (INTEL_INFO(dev)->gen >= 4)
2648 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2649 else if (IS_I830(dev) || IS_845G(dev))
2650 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2651 else
2652 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002653 ring->init_hw = init_render_ring;
Daniel Vetter59465b52012-04-11 22:12:48 +02002654 ring->cleanup = render_ring_cleanup;
2655
Daniel Vetterb45305f2012-12-17 16:21:27 +01002656 /* Workaround batchbuffer to combat CS tlb bug. */
2657 if (HAS_BROKEN_CS_TLB(dev)) {
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002658 obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002659 if (obj == NULL) {
2660 DRM_ERROR("Failed to allocate batch bo\n");
2661 return -ENOMEM;
2662 }
2663
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002664 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002665 if (ret != 0) {
2666 drm_gem_object_unreference(&obj->base);
2667 DRM_ERROR("Failed to ping batch bo\n");
2668 return ret;
2669 }
2670
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002671 ring->scratch.obj = obj;
2672 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002673 }
2674
Daniel Vetter99be1df2014-11-20 00:33:06 +01002675 ret = intel_init_ring_buffer(dev, ring);
2676 if (ret)
2677 return ret;
2678
2679 if (INTEL_INFO(dev)->gen >= 5) {
2680 ret = intel_init_pipe_control(ring);
2681 if (ret)
2682 return ret;
2683 }
2684
2685 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002686}
2687
2688int intel_init_bsd_ring_buffer(struct drm_device *dev)
2689{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002690 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002691 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002692
Daniel Vetter58fa3832012-04-11 22:12:49 +02002693 ring->name = "bsd ring";
2694 ring->id = VCS;
2695
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002696 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002697 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002698 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002699 /* gen6 bsd needs a special wa for tail updates */
2700 if (IS_GEN6(dev))
2701 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002702 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002703 ring->add_request = gen6_add_request;
2704 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002705 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002706 if (INTEL_INFO(dev)->gen >= 8) {
2707 ring->irq_enable_mask =
2708 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2709 ring->irq_get = gen8_ring_get_irq;
2710 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002711 ring->dispatch_execbuffer =
2712 gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002713 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002714 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002715 ring->semaphore.signal = gen8_xcs_signal;
2716 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002717 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002718 } else {
2719 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2720 ring->irq_get = gen6_ring_get_irq;
2721 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002722 ring->dispatch_execbuffer =
2723 gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002724 if (i915_semaphore_is_enabled(dev)) {
2725 ring->semaphore.sync_to = gen6_ring_sync;
2726 ring->semaphore.signal = gen6_signal;
2727 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2728 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2729 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2730 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2731 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2732 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2733 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2734 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2735 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2736 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2737 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002738 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002739 } else {
2740 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002741 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002742 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002743 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002744 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002745 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002746 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002747 ring->irq_get = gen5_ring_get_irq;
2748 ring->irq_put = gen5_ring_put_irq;
2749 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002750 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002751 ring->irq_get = i9xx_ring_get_irq;
2752 ring->irq_put = i9xx_ring_put_irq;
2753 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002754 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002755 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002756 ring->init_hw = init_ring_common;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002757
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002758 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002759}
Chris Wilson549f7362010-10-19 11:19:32 +01002760
Zhao Yakui845f74a2014-04-17 10:37:37 +08002761/**
Damien Lespiau62659922015-01-29 14:13:40 +00002762 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002763 */
2764int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2765{
2766 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002767 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002768
Rodrigo Vivif7b64232014-07-01 02:41:36 -07002769 ring->name = "bsd2 ring";
Zhao Yakui845f74a2014-04-17 10:37:37 +08002770 ring->id = VCS2;
2771
2772 ring->write_tail = ring_write_tail;
2773 ring->mmio_base = GEN8_BSD2_RING_BASE;
2774 ring->flush = gen6_bsd_ring_flush;
2775 ring->add_request = gen6_add_request;
2776 ring->get_seqno = gen6_ring_get_seqno;
2777 ring->set_seqno = ring_set_seqno;
2778 ring->irq_enable_mask =
2779 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2780 ring->irq_get = gen8_ring_get_irq;
2781 ring->irq_put = gen8_ring_put_irq;
2782 ring->dispatch_execbuffer =
2783 gen8_ring_dispatch_execbuffer;
Ben Widawsky3e789982014-06-30 09:53:37 -07002784 if (i915_semaphore_is_enabled(dev)) {
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_xcs_signal;
2787 GEN8_RING_SEMAPHORE_INIT;
2788 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002789 ring->init_hw = init_ring_common;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002790
2791 return intel_init_ring_buffer(dev, ring);
2792}
2793
Chris Wilson549f7362010-10-19 11:19:32 +01002794int intel_init_blt_ring_buffer(struct drm_device *dev)
2795{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002796 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002797 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002798
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002799 ring->name = "blitter ring";
2800 ring->id = BCS;
2801
2802 ring->mmio_base = BLT_RING_BASE;
2803 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002804 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002805 ring->add_request = gen6_add_request;
2806 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002807 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002808 if (INTEL_INFO(dev)->gen >= 8) {
2809 ring->irq_enable_mask =
2810 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2811 ring->irq_get = gen8_ring_get_irq;
2812 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002813 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002814 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002815 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002816 ring->semaphore.signal = gen8_xcs_signal;
2817 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002818 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002819 } else {
2820 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2821 ring->irq_get = gen6_ring_get_irq;
2822 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002823 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002824 if (i915_semaphore_is_enabled(dev)) {
2825 ring->semaphore.signal = gen6_signal;
2826 ring->semaphore.sync_to = gen6_ring_sync;
2827 /*
2828 * The current semaphore is only applied on pre-gen8
2829 * platform. And there is no VCS2 ring on the pre-gen8
2830 * platform. So the semaphore between BCS and VCS2 is
2831 * initialized as INVALID. Gen8 will initialize the
2832 * sema between BCS and VCS2 later.
2833 */
2834 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2835 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2836 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2837 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2838 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2839 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2840 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2841 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2842 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2843 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2844 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002845 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002846 ring->init_hw = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002847
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002848 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002849}
Chris Wilsona7b97612012-07-20 12:41:08 +01002850
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002851int intel_init_vebox_ring_buffer(struct drm_device *dev)
2852{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002853 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002854 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002855
2856 ring->name = "video enhancement ring";
2857 ring->id = VECS;
2858
2859 ring->mmio_base = VEBOX_RING_BASE;
2860 ring->write_tail = ring_write_tail;
2861 ring->flush = gen6_ring_flush;
2862 ring->add_request = gen6_add_request;
2863 ring->get_seqno = gen6_ring_get_seqno;
2864 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002865
2866 if (INTEL_INFO(dev)->gen >= 8) {
2867 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002868 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002869 ring->irq_get = gen8_ring_get_irq;
2870 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002871 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002872 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002873 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002874 ring->semaphore.signal = gen8_xcs_signal;
2875 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002876 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002877 } else {
2878 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2879 ring->irq_get = hsw_vebox_get_irq;
2880 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002881 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002882 if (i915_semaphore_is_enabled(dev)) {
2883 ring->semaphore.sync_to = gen6_ring_sync;
2884 ring->semaphore.signal = gen6_signal;
2885 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2886 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2887 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2888 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2889 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2890 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2891 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2892 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2893 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2894 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2895 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002896 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002897 ring->init_hw = init_ring_common;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002898
2899 return intel_init_ring_buffer(dev, ring);
2900}
2901
Chris Wilsona7b97612012-07-20 12:41:08 +01002902int
John Harrison4866d722015-05-29 17:43:55 +01002903intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002904{
John Harrison4866d722015-05-29 17:43:55 +01002905 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002906 int ret;
2907
2908 if (!ring->gpu_caches_dirty)
2909 return 0;
2910
John Harrisona84c3ae2015-05-29 17:43:57 +01002911 ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002912 if (ret)
2913 return ret;
2914
John Harrisona84c3ae2015-05-29 17:43:57 +01002915 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002916
2917 ring->gpu_caches_dirty = false;
2918 return 0;
2919}
2920
2921int
John Harrison2f200552015-05-29 17:43:53 +01002922intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002923{
John Harrison2f200552015-05-29 17:43:53 +01002924 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002925 uint32_t flush_domains;
2926 int ret;
2927
2928 flush_domains = 0;
2929 if (ring->gpu_caches_dirty)
2930 flush_domains = I915_GEM_GPU_DOMAINS;
2931
John Harrisona84c3ae2015-05-29 17:43:57 +01002932 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002933 if (ret)
2934 return ret;
2935
John Harrisona84c3ae2015-05-29 17:43:57 +01002936 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002937
2938 ring->gpu_caches_dirty = false;
2939 return 0;
2940}
Chris Wilsone3efda42014-04-09 09:19:41 +01002941
2942void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002943intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002944{
2945 int ret;
2946
2947 if (!intel_ring_initialized(ring))
2948 return;
2949
2950 ret = intel_ring_idle(ring);
2951 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2952 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2953 ring->name, ret);
2954
2955 stop_ring(ring);
2956}