blob: dab5e7c790361255c400e4870e69ebbfb2a99511 [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
Chris Wilson18393f62014-04-09 09:19:40 +010036/* Early gen2 devices have a cacheline of just 32 bytes, using 64 is overkill,
37 * but keeps the logic simple. Indeed, the whole purpose of this macro is just
38 * to give some inclination as to some of the magic values used in the various
39 * workarounds!
40 */
41#define CACHELINE_BYTES 64
42
Oscar Mateo48d82382014-07-24 17:04:23 +010043bool
44intel_ring_initialized(struct intel_engine_cs *ring)
45{
46 struct drm_device *dev = ring->dev;
47
48 if (!dev)
49 return false;
50
51 if (i915.enable_execlists) {
52 struct intel_context *dctx = ring->default_context;
53 struct intel_ringbuffer *ringbuf = dctx->engine[ring->id].ringbuf;
54
55 return ringbuf->obj;
56 } else
57 return ring->buffer && ring->buffer->obj;
58}
59
Chris Wilson1cf0ba12014-05-05 09:07:33 +010060static inline int __ring_space(int head, int tail, int size)
61{
62 int space = head - (tail + I915_RING_FREE_SPACE);
63 if (space < 0)
64 space += size;
65 return space;
66}
67
Oscar Mateo64c58f22014-07-03 16:28:03 +010068static inline int ring_space(struct intel_ringbuffer *ringbuf)
Chris Wilsonc7dca472011-01-20 17:00:10 +000069{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010070 return __ring_space(ringbuf->head & HEAD_ADDR, ringbuf->tail, ringbuf->size);
Chris Wilsonc7dca472011-01-20 17:00:10 +000071}
72
Oscar Mateoa4872ba2014-05-22 14:13:33 +010073static bool intel_ring_stopped(struct intel_engine_cs *ring)
Chris Wilson09246732013-08-10 22:16:32 +010074{
75 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020076 return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
77}
Chris Wilson09246732013-08-10 22:16:32 +010078
Oscar Mateoa4872ba2014-05-22 14:13:33 +010079void __intel_ring_advance(struct intel_engine_cs *ring)
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020080{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010081 struct intel_ringbuffer *ringbuf = ring->buffer;
82 ringbuf->tail &= ringbuf->size - 1;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020083 if (intel_ring_stopped(ring))
Chris Wilson09246732013-08-10 22:16:32 +010084 return;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010085 ring->write_tail(ring, ringbuf->tail);
Chris Wilson09246732013-08-10 22:16:32 +010086}
87
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000088static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +010089gen2_render_ring_flush(struct intel_engine_cs *ring,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010090 u32 invalidate_domains,
91 u32 flush_domains)
92{
93 u32 cmd;
94 int ret;
95
96 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +020097 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010098 cmd |= MI_NO_WRITE_FLUSH;
99
100 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
101 cmd |= MI_READ_FLUSH;
102
103 ret = intel_ring_begin(ring, 2);
104 if (ret)
105 return ret;
106
107 intel_ring_emit(ring, cmd);
108 intel_ring_emit(ring, MI_NOOP);
109 intel_ring_advance(ring);
110
111 return 0;
112}
113
114static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100115gen4_render_ring_flush(struct intel_engine_cs *ring,
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100116 u32 invalidate_domains,
117 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700118{
Chris Wilson78501ea2010-10-27 12:18:21 +0100119 struct drm_device *dev = ring->dev;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100120 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000121 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100122
Chris Wilson36d527d2011-03-19 22:26:49 +0000123 /*
124 * read/write caches:
125 *
126 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
127 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
128 * also flushed at 2d versus 3d pipeline switches.
129 *
130 * read-only caches:
131 *
132 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
133 * MI_READ_FLUSH is set, and is always flushed on 965.
134 *
135 * I915_GEM_DOMAIN_COMMAND may not exist?
136 *
137 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
138 * invalidated when MI_EXE_FLUSH is set.
139 *
140 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
141 * invalidated with every MI_FLUSH.
142 *
143 * TLBs:
144 *
145 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
146 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
147 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
148 * are flushed at any MI_FLUSH.
149 */
150
151 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100152 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000153 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000154 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
155 cmd |= MI_EXE_FLUSH;
156
157 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
158 (IS_G4X(dev) || IS_GEN5(dev)))
159 cmd |= MI_INVALIDATE_ISP;
160
161 ret = intel_ring_begin(ring, 2);
162 if (ret)
163 return ret;
164
165 intel_ring_emit(ring, cmd);
166 intel_ring_emit(ring, MI_NOOP);
167 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000168
169 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800170}
171
Jesse Barnes8d315282011-10-16 10:23:31 +0200172/**
173 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
174 * implementing two workarounds on gen6. From section 1.4.7.1
175 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
176 *
177 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
178 * produced by non-pipelined state commands), software needs to first
179 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
180 * 0.
181 *
182 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
183 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
184 *
185 * And the workaround for these two requires this workaround first:
186 *
187 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
188 * BEFORE the pipe-control with a post-sync op and no write-cache
189 * flushes.
190 *
191 * And this last workaround is tricky because of the requirements on
192 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
193 * volume 2 part 1:
194 *
195 * "1 of the following must also be set:
196 * - Render Target Cache Flush Enable ([12] of DW1)
197 * - Depth Cache Flush Enable ([0] of DW1)
198 * - Stall at Pixel Scoreboard ([1] of DW1)
199 * - Depth Stall ([13] of DW1)
200 * - Post-Sync Operation ([13] of DW1)
201 * - Notify Enable ([8] of DW1)"
202 *
203 * The cache flushes require the workaround flush that triggered this
204 * one, so we can't use it. Depth stall would trigger the same.
205 * Post-sync nonzero is what triggered this second workaround, so we
206 * can't use that one either. Notify enable is IRQs, which aren't
207 * really our business. That leaves only stall at scoreboard.
208 */
209static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100210intel_emit_post_sync_nonzero_flush(struct intel_engine_cs *ring)
Jesse Barnes8d315282011-10-16 10:23:31 +0200211{
Chris Wilson18393f62014-04-09 09:19:40 +0100212 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200213 int ret;
214
215
216 ret = intel_ring_begin(ring, 6);
217 if (ret)
218 return ret;
219
220 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
221 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
222 PIPE_CONTROL_STALL_AT_SCOREBOARD);
223 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
224 intel_ring_emit(ring, 0); /* low dword */
225 intel_ring_emit(ring, 0); /* high dword */
226 intel_ring_emit(ring, MI_NOOP);
227 intel_ring_advance(ring);
228
229 ret = intel_ring_begin(ring, 6);
230 if (ret)
231 return ret;
232
233 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
234 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
235 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
236 intel_ring_emit(ring, 0);
237 intel_ring_emit(ring, 0);
238 intel_ring_emit(ring, MI_NOOP);
239 intel_ring_advance(ring);
240
241 return 0;
242}
243
244static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100245gen6_render_ring_flush(struct intel_engine_cs *ring,
Jesse Barnes8d315282011-10-16 10:23:31 +0200246 u32 invalidate_domains, u32 flush_domains)
247{
248 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100249 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200250 int ret;
251
Paulo Zanonib3111502012-08-17 18:35:42 -0300252 /* Force SNB workarounds for PIPE_CONTROL flushes */
253 ret = intel_emit_post_sync_nonzero_flush(ring);
254 if (ret)
255 return ret;
256
Jesse Barnes8d315282011-10-16 10:23:31 +0200257 /* Just flush everything. Experiments have shown that reducing the
258 * number of bits based on the write domains has little performance
259 * impact.
260 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100261 if (flush_domains) {
262 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
263 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
264 /*
265 * Ensure that any following seqno writes only happen
266 * when the render cache is indeed flushed.
267 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200268 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100269 }
270 if (invalidate_domains) {
271 flags |= PIPE_CONTROL_TLB_INVALIDATE;
272 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
273 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
274 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
275 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
276 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
277 /*
278 * TLB invalidate requires a post-sync write.
279 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700280 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100281 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200282
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100283 ret = intel_ring_begin(ring, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200284 if (ret)
285 return ret;
286
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100287 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
Jesse Barnes8d315282011-10-16 10:23:31 +0200288 intel_ring_emit(ring, flags);
289 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100290 intel_ring_emit(ring, 0);
Jesse Barnes8d315282011-10-16 10:23:31 +0200291 intel_ring_advance(ring);
292
293 return 0;
294}
295
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100296static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100297gen7_render_ring_cs_stall_wa(struct intel_engine_cs *ring)
Paulo Zanonif3987632012-08-17 18:35:43 -0300298{
299 int ret;
300
301 ret = intel_ring_begin(ring, 4);
302 if (ret)
303 return ret;
304
305 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
306 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
307 PIPE_CONTROL_STALL_AT_SCOREBOARD);
308 intel_ring_emit(ring, 0);
309 intel_ring_emit(ring, 0);
310 intel_ring_advance(ring);
311
312 return 0;
313}
314
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100315static int gen7_ring_fbc_flush(struct intel_engine_cs *ring, u32 value)
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300316{
317 int ret;
318
319 if (!ring->fbc_dirty)
320 return 0;
321
Ville Syrjälä37c1d942013-11-06 23:02:20 +0200322 ret = intel_ring_begin(ring, 6);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300323 if (ret)
324 return ret;
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300325 /* WaFbcNukeOn3DBlt:ivb/hsw */
326 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
327 intel_ring_emit(ring, MSG_FBC_REND_STATE);
328 intel_ring_emit(ring, value);
Ville Syrjälä37c1d942013-11-06 23:02:20 +0200329 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) | MI_SRM_LRM_GLOBAL_GTT);
330 intel_ring_emit(ring, MSG_FBC_REND_STATE);
331 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300332 intel_ring_advance(ring);
333
334 ring->fbc_dirty = false;
335 return 0;
336}
337
Paulo Zanonif3987632012-08-17 18:35:43 -0300338static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100339gen7_render_ring_flush(struct intel_engine_cs *ring,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300340 u32 invalidate_domains, u32 flush_domains)
341{
342 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100343 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300344 int ret;
345
Paulo Zanonif3987632012-08-17 18:35:43 -0300346 /*
347 * Ensure that any following seqno writes only happen when the render
348 * cache is indeed flushed.
349 *
350 * Workaround: 4th PIPE_CONTROL command (except the ones with only
351 * read-cache invalidate bits set) must have the CS_STALL bit set. We
352 * don't try to be clever and just set it unconditionally.
353 */
354 flags |= PIPE_CONTROL_CS_STALL;
355
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300356 /* Just flush everything. Experiments have shown that reducing the
357 * number of bits based on the write domains has little performance
358 * impact.
359 */
360 if (flush_domains) {
361 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
362 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300363 }
364 if (invalidate_domains) {
365 flags |= PIPE_CONTROL_TLB_INVALIDATE;
366 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
367 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
368 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
369 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
370 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
371 /*
372 * TLB invalidate requires a post-sync write.
373 */
374 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200375 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300376
377 /* Workaround: we must issue a pipe_control with CS-stall bit
378 * set before a pipe_control command that has the state cache
379 * invalidate bit set. */
380 gen7_render_ring_cs_stall_wa(ring);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300381 }
382
383 ret = intel_ring_begin(ring, 4);
384 if (ret)
385 return ret;
386
387 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
388 intel_ring_emit(ring, flags);
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200389 intel_ring_emit(ring, scratch_addr);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300390 intel_ring_emit(ring, 0);
391 intel_ring_advance(ring);
392
Ville Syrjälä9688eca2013-11-06 23:02:19 +0200393 if (!invalidate_domains && flush_domains)
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300394 return gen7_ring_fbc_flush(ring, FBC_REND_NUKE);
395
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300396 return 0;
397}
398
Ben Widawskya5f3d682013-11-02 21:07:27 -0700399static int
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300400gen8_emit_pipe_control(struct intel_engine_cs *ring,
401 u32 flags, u32 scratch_addr)
402{
403 int ret;
404
405 ret = intel_ring_begin(ring, 6);
406 if (ret)
407 return ret;
408
409 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
410 intel_ring_emit(ring, flags);
411 intel_ring_emit(ring, scratch_addr);
412 intel_ring_emit(ring, 0);
413 intel_ring_emit(ring, 0);
414 intel_ring_emit(ring, 0);
415 intel_ring_advance(ring);
416
417 return 0;
418}
419
420static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100421gen8_render_ring_flush(struct intel_engine_cs *ring,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700422 u32 invalidate_domains, u32 flush_domains)
423{
424 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100425 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800426 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700427
428 flags |= PIPE_CONTROL_CS_STALL;
429
430 if (flush_domains) {
431 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
432 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
433 }
434 if (invalidate_domains) {
435 flags |= PIPE_CONTROL_TLB_INVALIDATE;
436 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
437 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
438 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
439 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
440 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
441 flags |= PIPE_CONTROL_QW_WRITE;
442 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800443
444 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
445 ret = gen8_emit_pipe_control(ring,
446 PIPE_CONTROL_CS_STALL |
447 PIPE_CONTROL_STALL_AT_SCOREBOARD,
448 0);
449 if (ret)
450 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700451 }
452
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300453 return gen8_emit_pipe_control(ring, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700454}
455
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100456static void ring_write_tail(struct intel_engine_cs *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100457 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800458{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300459 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100460 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800461}
462
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100463u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800464{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300465 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson50877442014-03-21 12:41:53 +0000466 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800467
Chris Wilson50877442014-03-21 12:41:53 +0000468 if (INTEL_INFO(ring->dev)->gen >= 8)
469 acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
470 RING_ACTHD_UDW(ring->mmio_base));
471 else if (INTEL_INFO(ring->dev)->gen >= 4)
472 acthd = I915_READ(RING_ACTHD(ring->mmio_base));
473 else
474 acthd = I915_READ(ACTHD);
475
476 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800477}
478
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100479static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200480{
481 struct drm_i915_private *dev_priv = ring->dev->dev_private;
482 u32 addr;
483
484 addr = dev_priv->status_page_dmah->busaddr;
485 if (INTEL_INFO(ring->dev)->gen >= 4)
486 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
487 I915_WRITE(HWS_PGA, addr);
488}
489
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100490static bool stop_ring(struct intel_engine_cs *ring)
Chris Wilson9991ae72014-04-02 16:36:07 +0100491{
492 struct drm_i915_private *dev_priv = to_i915(ring->dev);
493
494 if (!IS_GEN2(ring->dev)) {
495 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
Daniel Vetter403bdd12014-08-07 16:05:39 +0200496 if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
497 DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100498 /* Sometimes we observe that the idle flag is not
499 * set even though the ring is empty. So double
500 * check before giving up.
501 */
502 if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
503 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100504 }
505 }
506
507 I915_WRITE_CTL(ring, 0);
508 I915_WRITE_HEAD(ring, 0);
509 ring->write_tail(ring, 0);
510
511 if (!IS_GEN2(ring->dev)) {
512 (void)I915_READ_CTL(ring);
513 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
514 }
515
516 return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
517}
518
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100519static int init_ring_common(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800520{
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200521 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300522 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100523 struct intel_ringbuffer *ringbuf = ring->buffer;
524 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200525 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800526
Deepak Sc8d9a592013-11-23 14:55:42 +0530527 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200528
Chris Wilson9991ae72014-04-02 16:36:07 +0100529 if (!stop_ring(ring)) {
530 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000531 DRM_DEBUG_KMS("%s head not reset to zero "
532 "ctl %08x head %08x tail %08x start %08x\n",
533 ring->name,
534 I915_READ_CTL(ring),
535 I915_READ_HEAD(ring),
536 I915_READ_TAIL(ring),
537 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800538
Chris Wilson9991ae72014-04-02 16:36:07 +0100539 if (!stop_ring(ring)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000540 DRM_ERROR("failed to set %s head to zero "
541 "ctl %08x head %08x tail %08x start %08x\n",
542 ring->name,
543 I915_READ_CTL(ring),
544 I915_READ_HEAD(ring),
545 I915_READ_TAIL(ring),
546 I915_READ_START(ring));
Chris Wilson9991ae72014-04-02 16:36:07 +0100547 ret = -EIO;
548 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000549 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700550 }
551
Chris Wilson9991ae72014-04-02 16:36:07 +0100552 if (I915_NEED_GFX_HWS(dev))
553 intel_ring_setup_status_page(ring);
554 else
555 ring_setup_phys_status_page(ring);
556
Jiri Kosinaece4a172014-08-07 16:29:53 +0200557 /* Enforce ordering by reading HEAD register back */
558 I915_READ_HEAD(ring);
559
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200560 /* Initialize the ring. This must happen _after_ we've cleared the ring
561 * registers with the above sequence (the readback of the HEAD registers
562 * also enforces ordering), otherwise the hw might lose the new ring
563 * register values. */
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700564 I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200565 I915_WRITE_CTL(ring,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100566 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000567 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800568
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800569 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400570 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700571 I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
Sean Paulf01db982012-03-16 12:43:22 -0400572 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000573 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100574 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
575 ring->name,
576 I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
577 I915_READ_HEAD(ring), I915_READ_TAIL(ring),
578 I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200579 ret = -EIO;
580 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800581 }
582
Chris Wilson78501ea2010-10-27 12:18:21 +0100583 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
584 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800585 else {
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100586 ringbuf->head = I915_READ_HEAD(ring);
587 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Oscar Mateo64c58f22014-07-03 16:28:03 +0100588 ringbuf->space = ring_space(ringbuf);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100589 ringbuf->last_retired_head = -1;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800590 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000591
Chris Wilson50f018d2013-06-10 11:20:19 +0100592 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
593
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200594out:
Deepak Sc8d9a592013-11-23 14:55:42 +0530595 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200596
597 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700598}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800599
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100600void
601intel_fini_pipe_control(struct intel_engine_cs *ring)
602{
603 struct drm_device *dev = ring->dev;
604
605 if (ring->scratch.obj == NULL)
606 return;
607
608 if (INTEL_INFO(dev)->gen >= 5) {
609 kunmap(sg_page(ring->scratch.obj->pages->sgl));
610 i915_gem_object_ggtt_unpin(ring->scratch.obj);
611 }
612
613 drm_gem_object_unreference(&ring->scratch.obj->base);
614 ring->scratch.obj = NULL;
615}
616
617int
618intel_init_pipe_control(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000619{
Chris Wilsonc6df5412010-12-15 09:56:50 +0000620 int ret;
621
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100622 if (ring->scratch.obj)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000623 return 0;
624
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100625 ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
626 if (ring->scratch.obj == NULL) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000627 DRM_ERROR("Failed to allocate seqno page\n");
628 ret = -ENOMEM;
629 goto err;
630 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100631
Daniel Vettera9cc7262014-02-14 14:01:13 +0100632 ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
633 if (ret)
634 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000635
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100636 ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000637 if (ret)
638 goto err_unref;
639
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100640 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
641 ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
642 if (ring->scratch.cpu_page == NULL) {
Wei Yongjun56b085a2013-05-28 17:51:44 +0800643 ret = -ENOMEM;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000644 goto err_unpin;
Wei Yongjun56b085a2013-05-28 17:51:44 +0800645 }
Chris Wilsonc6df5412010-12-15 09:56:50 +0000646
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200647 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100648 ring->name, ring->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000649 return 0;
650
651err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800652 i915_gem_object_ggtt_unpin(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000653err_unref:
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100654 drm_gem_object_unreference(&ring->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000655err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000656 return ret;
657}
658
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100659static int init_render_ring(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800660{
Chris Wilson78501ea2010-10-27 12:18:21 +0100661 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000662 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100663 int ret = init_ring_common(ring);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +0200664 if (ret)
665 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800666
Akash Goel61a563a2014-03-25 18:01:50 +0530667 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
668 if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
Daniel Vetter6b26c862012-04-24 14:04:12 +0200669 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000670
671 /* We need to disable the AsyncFlip performance optimisations in order
672 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
673 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +0100674 *
Ville Syrjäläb3f797a2014-04-28 14:31:09 +0300675 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000676 */
677 if (INTEL_INFO(dev)->gen >= 6)
678 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
679
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000680 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +0530681 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000682 if (INTEL_INFO(dev)->gen == 6)
683 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +0000684 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000685
Akash Goel01fa0302014-03-24 23:00:04 +0530686 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000687 if (IS_GEN7(dev))
688 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +0530689 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000690 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +0100691
Jesse Barnes8d315282011-10-16 10:23:31 +0200692 if (INTEL_INFO(dev)->gen >= 5) {
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100693 ret = intel_init_pipe_control(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000694 if (ret)
695 return ret;
696 }
697
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200698 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700699 /* From the Sandybridge PRM, volume 1 part 3, page 24:
700 * "If this bit is set, STCunit will have LRA as replacement
701 * policy. [...] This bit must be reset. LRA replacement
702 * policy is not supported."
703 */
704 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200705 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -0800706 }
707
Daniel Vetter6b26c862012-04-24 14:04:12 +0200708 if (INTEL_INFO(dev)->gen >= 6)
709 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +0000710
Ben Widawsky040d2ba2013-09-19 11:01:40 -0700711 if (HAS_L3_DPF(dev))
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700712 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -0700713
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800714 return ret;
715}
716
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100717static void render_ring_cleanup(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000718{
Daniel Vetterb45305f2012-12-17 16:21:27 +0100719 struct drm_device *dev = ring->dev;
Ben Widawsky3e789982014-06-30 09:53:37 -0700720 struct drm_i915_private *dev_priv = dev->dev_private;
721
722 if (dev_priv->semaphore_obj) {
723 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
724 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
725 dev_priv->semaphore_obj = NULL;
726 }
Daniel Vetterb45305f2012-12-17 16:21:27 +0100727
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100728 intel_fini_pipe_control(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000729}
730
Ben Widawsky3e789982014-06-30 09:53:37 -0700731static int gen8_rcs_signal(struct intel_engine_cs *signaller,
732 unsigned int num_dwords)
733{
734#define MBOX_UPDATE_DWORDS 8
735 struct drm_device *dev = signaller->dev;
736 struct drm_i915_private *dev_priv = dev->dev_private;
737 struct intel_engine_cs *waiter;
738 int i, ret, num_rings;
739
740 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
741 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
742#undef MBOX_UPDATE_DWORDS
743
744 ret = intel_ring_begin(signaller, num_dwords);
745 if (ret)
746 return ret;
747
748 for_each_ring(waiter, dev_priv, i) {
749 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
750 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
751 continue;
752
753 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
754 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
755 PIPE_CONTROL_QW_WRITE |
756 PIPE_CONTROL_FLUSH_ENABLE);
757 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
758 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
759 intel_ring_emit(signaller, signaller->outstanding_lazy_seqno);
760 intel_ring_emit(signaller, 0);
761 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
762 MI_SEMAPHORE_TARGET(waiter->id));
763 intel_ring_emit(signaller, 0);
764 }
765
766 return 0;
767}
768
769static int gen8_xcs_signal(struct intel_engine_cs *signaller,
770 unsigned int num_dwords)
771{
772#define MBOX_UPDATE_DWORDS 6
773 struct drm_device *dev = signaller->dev;
774 struct drm_i915_private *dev_priv = dev->dev_private;
775 struct intel_engine_cs *waiter;
776 int i, ret, num_rings;
777
778 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
779 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
780#undef MBOX_UPDATE_DWORDS
781
782 ret = intel_ring_begin(signaller, num_dwords);
783 if (ret)
784 return ret;
785
786 for_each_ring(waiter, dev_priv, i) {
787 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
788 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
789 continue;
790
791 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
792 MI_FLUSH_DW_OP_STOREDW);
793 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
794 MI_FLUSH_DW_USE_GTT);
795 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
796 intel_ring_emit(signaller, signaller->outstanding_lazy_seqno);
797 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
798 MI_SEMAPHORE_TARGET(waiter->id));
799 intel_ring_emit(signaller, 0);
800 }
801
802 return 0;
803}
804
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100805static int gen6_signal(struct intel_engine_cs *signaller,
Ben Widawsky024a43e2014-04-29 14:52:30 -0700806 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000807{
Ben Widawsky024a43e2014-04-29 14:52:30 -0700808 struct drm_device *dev = signaller->dev;
809 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100810 struct intel_engine_cs *useless;
Ben Widawskya1444b72014-06-30 09:53:35 -0700811 int i, ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -0700812
Ben Widawskya1444b72014-06-30 09:53:35 -0700813#define MBOX_UPDATE_DWORDS 3
814 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
815 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
816#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -0700817
818 ret = intel_ring_begin(signaller, num_dwords);
819 if (ret)
820 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700821
Ben Widawsky78325f22014-04-29 14:52:29 -0700822 for_each_ring(useless, dev_priv, i) {
823 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
824 if (mbox_reg != GEN6_NOSYNC) {
825 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
826 intel_ring_emit(signaller, mbox_reg);
827 intel_ring_emit(signaller, signaller->outstanding_lazy_seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -0700828 }
829 }
Ben Widawsky024a43e2014-04-29 14:52:30 -0700830
Ben Widawskya1444b72014-06-30 09:53:35 -0700831 /* If num_dwords was rounded, make sure the tail pointer is correct */
832 if (num_rings % 2 == 0)
833 intel_ring_emit(signaller, MI_NOOP);
834
Ben Widawsky024a43e2014-04-29 14:52:30 -0700835 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000836}
837
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700838/**
839 * gen6_add_request - Update the semaphore mailbox registers
840 *
841 * @ring - ring that is adding a request
842 * @seqno - return seqno stuck into the ring
843 *
844 * Update the mailbox registers in the *other* rings with the current seqno.
845 * This acts like a signal in the canonical semaphore.
846 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000847static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100848gen6_add_request(struct intel_engine_cs *ring)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000849{
Ben Widawsky024a43e2014-04-29 14:52:30 -0700850 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000851
Ben Widawsky707d9cf2014-06-30 09:53:36 -0700852 if (ring->semaphore.signal)
853 ret = ring->semaphore.signal(ring, 4);
854 else
855 ret = intel_ring_begin(ring, 4);
856
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000857 if (ret)
858 return ret;
859
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000860 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
861 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson18235212013-09-04 10:45:51 +0100862 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000863 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +0100864 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000865
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000866 return 0;
867}
868
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200869static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
870 u32 seqno)
871{
872 struct drm_i915_private *dev_priv = dev->dev_private;
873 return dev_priv->last_seqno < seqno;
874}
875
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700876/**
877 * intel_ring_sync - sync the waiter to the signaller on seqno
878 *
879 * @waiter - ring that is waiting
880 * @signaller - ring which has, or will signal
881 * @seqno - seqno which the waiter will block on
882 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700883
884static int
885gen8_ring_sync(struct intel_engine_cs *waiter,
886 struct intel_engine_cs *signaller,
887 u32 seqno)
888{
889 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
890 int ret;
891
892 ret = intel_ring_begin(waiter, 4);
893 if (ret)
894 return ret;
895
896 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
897 MI_SEMAPHORE_GLOBAL_GTT |
Ben Widawskybae4fcd2014-06-30 09:53:43 -0700898 MI_SEMAPHORE_POLL |
Ben Widawsky5ee426c2014-06-30 09:53:38 -0700899 MI_SEMAPHORE_SAD_GTE_SDD);
900 intel_ring_emit(waiter, seqno);
901 intel_ring_emit(waiter,
902 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
903 intel_ring_emit(waiter,
904 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
905 intel_ring_advance(waiter);
906 return 0;
907}
908
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700909static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100910gen6_ring_sync(struct intel_engine_cs *waiter,
911 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200912 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000913{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700914 u32 dw1 = MI_SEMAPHORE_MBOX |
915 MI_SEMAPHORE_COMPARE |
916 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -0700917 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
918 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000919
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700920 /* Throughout all of the GEM code, seqno passed implies our current
921 * seqno is >= the last seqno executed. However for hardware the
922 * comparison is strictly greater than.
923 */
924 seqno -= 1;
925
Ben Widawskyebc348b2014-04-29 14:52:28 -0700926 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200927
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700928 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000929 if (ret)
930 return ret;
931
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200932 /* If seqno wrap happened, omit the wait with no-ops */
933 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -0700934 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200935 intel_ring_emit(waiter, seqno);
936 intel_ring_emit(waiter, 0);
937 intel_ring_emit(waiter, MI_NOOP);
938 } else {
939 intel_ring_emit(waiter, MI_NOOP);
940 intel_ring_emit(waiter, MI_NOOP);
941 intel_ring_emit(waiter, MI_NOOP);
942 intel_ring_emit(waiter, MI_NOOP);
943 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700944 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000945
946 return 0;
947}
948
Chris Wilsonc6df5412010-12-15 09:56:50 +0000949#define PIPE_CONTROL_FLUSH(ring__, addr__) \
950do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200951 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
952 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000953 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
954 intel_ring_emit(ring__, 0); \
955 intel_ring_emit(ring__, 0); \
956} while (0)
957
958static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100959pc_render_add_request(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000960{
Chris Wilson18393f62014-04-09 09:19:40 +0100961 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000962 int ret;
963
964 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
965 * incoherent with writes to memory, i.e. completely fubar,
966 * so we need to use PIPE_NOTIFY instead.
967 *
968 * However, we also need to workaround the qword write
969 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
970 * memory before requesting an interrupt.
971 */
972 ret = intel_ring_begin(ring, 32);
973 if (ret)
974 return ret;
975
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200976 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200977 PIPE_CONTROL_WRITE_FLUSH |
978 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100979 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson18235212013-09-04 10:45:51 +0100980 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000981 intel_ring_emit(ring, 0);
982 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100983 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +0000984 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100985 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000986 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100987 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000988 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100989 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000990 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100991 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000992 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +0000993
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200994 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200995 PIPE_CONTROL_WRITE_FLUSH |
996 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000997 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100998 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson18235212013-09-04 10:45:51 +0100999 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001000 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +01001001 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001002
Chris Wilsonc6df5412010-12-15 09:56:50 +00001003 return 0;
1004}
1005
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001006static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001007gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001008{
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001009 /* Workaround to force correct ordering between irq and seqno writes on
1010 * ivb (and maybe also on snb) by reading from a CS register (like
1011 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +00001012 if (!lazy_coherency) {
1013 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1014 POSTING_READ(RING_ACTHD(ring->mmio_base));
1015 }
1016
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001017 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1018}
1019
1020static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001021ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001022{
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001023 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1024}
1025
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001026static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001027ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001028{
1029 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1030}
1031
Chris Wilsonc6df5412010-12-15 09:56:50 +00001032static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001033pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001034{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001035 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001036}
1037
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001038static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001039pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001040{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001041 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001042}
1043
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001044static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001045gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001046{
1047 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001048 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001049 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001050
1051 if (!dev->irq_enabled)
1052 return false;
1053
Chris Wilson7338aef2012-04-24 21:48:47 +01001054 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001055 if (ring->irq_refcount++ == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001056 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001057 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001058
1059 return true;
1060}
1061
1062static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001063gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001064{
1065 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001066 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001067 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001068
Chris Wilson7338aef2012-04-24 21:48:47 +01001069 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001070 if (--ring->irq_refcount == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001071 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001072 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001073}
1074
1075static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001076i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001077{
Chris Wilson78501ea2010-10-27 12:18:21 +01001078 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001079 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001080 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001081
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001082 if (!dev->irq_enabled)
1083 return false;
1084
Chris Wilson7338aef2012-04-24 21:48:47 +01001085 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001086 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001087 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1088 I915_WRITE(IMR, dev_priv->irq_mask);
1089 POSTING_READ(IMR);
1090 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001091 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001092
1093 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001094}
1095
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001096static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001097i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001098{
Chris Wilson78501ea2010-10-27 12:18:21 +01001099 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001100 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001101 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001102
Chris Wilson7338aef2012-04-24 21:48:47 +01001103 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001104 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001105 dev_priv->irq_mask |= ring->irq_enable_mask;
1106 I915_WRITE(IMR, dev_priv->irq_mask);
1107 POSTING_READ(IMR);
1108 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001109 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001110}
1111
Chris Wilsonc2798b12012-04-22 21:13:57 +01001112static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001113i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001114{
1115 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001116 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001117 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001118
1119 if (!dev->irq_enabled)
1120 return false;
1121
Chris Wilson7338aef2012-04-24 21:48:47 +01001122 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001123 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001124 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1125 I915_WRITE16(IMR, dev_priv->irq_mask);
1126 POSTING_READ16(IMR);
1127 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001128 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001129
1130 return true;
1131}
1132
1133static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001134i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001135{
1136 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001137 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001138 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001139
Chris Wilson7338aef2012-04-24 21:48:47 +01001140 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001141 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001142 dev_priv->irq_mask |= ring->irq_enable_mask;
1143 I915_WRITE16(IMR, dev_priv->irq_mask);
1144 POSTING_READ16(IMR);
1145 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001146 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001147}
1148
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001149void intel_ring_setup_status_page(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001150{
Eric Anholt45930102011-05-06 17:12:35 -07001151 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001152 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -07001153 u32 mmio = 0;
1154
1155 /* The ring status page addresses are no longer next to the rest of
1156 * the ring registers as of gen7.
1157 */
1158 if (IS_GEN7(dev)) {
1159 switch (ring->id) {
Daniel Vetter96154f22011-12-14 13:57:00 +01001160 case RCS:
Eric Anholt45930102011-05-06 17:12:35 -07001161 mmio = RENDER_HWS_PGA_GEN7;
1162 break;
Daniel Vetter96154f22011-12-14 13:57:00 +01001163 case BCS:
Eric Anholt45930102011-05-06 17:12:35 -07001164 mmio = BLT_HWS_PGA_GEN7;
1165 break;
Zhao Yakui77fe2ff2014-04-17 10:37:39 +08001166 /*
1167 * VCS2 actually doesn't exist on Gen7. Only shut up
1168 * gcc switch check warning
1169 */
1170 case VCS2:
Daniel Vetter96154f22011-12-14 13:57:00 +01001171 case VCS:
Eric Anholt45930102011-05-06 17:12:35 -07001172 mmio = BSD_HWS_PGA_GEN7;
1173 break;
Ben Widawsky4a3dd192013-05-28 19:22:19 -07001174 case VECS:
Ben Widawsky9a8a2212013-05-28 19:22:23 -07001175 mmio = VEBOX_HWS_PGA_GEN7;
1176 break;
Eric Anholt45930102011-05-06 17:12:35 -07001177 }
1178 } else if (IS_GEN6(ring->dev)) {
1179 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
1180 } else {
Ben Widawskyeb0d4b72013-11-07 21:40:50 -08001181 /* XXX: gen8 returns to sanity */
Eric Anholt45930102011-05-06 17:12:35 -07001182 mmio = RING_HWS_PGA(ring->mmio_base);
1183 }
1184
Chris Wilson78501ea2010-10-27 12:18:21 +01001185 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
1186 POSTING_READ(mmio);
Chris Wilson884020b2013-08-06 19:01:14 +01001187
Damien Lespiaudc616b82014-03-13 01:40:28 +00001188 /*
1189 * Flush the TLB for this page
1190 *
1191 * FIXME: These two bits have disappeared on gen8, so a question
1192 * arises: do we still need this and if so how should we go about
1193 * invalidating the TLB?
1194 */
1195 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
Chris Wilson884020b2013-08-06 19:01:14 +01001196 u32 reg = RING_INSTPM(ring->mmio_base);
Naresh Kumar Kachhi02f6a1e2014-03-12 16:39:42 +05301197
1198 /* ring should be idle before issuing a sync flush*/
1199 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
1200
Chris Wilson884020b2013-08-06 19:01:14 +01001201 I915_WRITE(reg,
1202 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
1203 INSTPM_SYNC_FLUSH));
1204 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
1205 1000))
1206 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
1207 ring->name);
1208 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001209}
1210
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001211static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001212bsd_ring_flush(struct intel_engine_cs *ring,
Chris Wilson78501ea2010-10-27 12:18:21 +01001213 u32 invalidate_domains,
1214 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001215{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001216 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001217
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001218 ret = intel_ring_begin(ring, 2);
1219 if (ret)
1220 return ret;
1221
1222 intel_ring_emit(ring, MI_FLUSH);
1223 intel_ring_emit(ring, MI_NOOP);
1224 intel_ring_advance(ring);
1225 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001226}
1227
Chris Wilson3cce4692010-10-27 16:11:02 +01001228static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001229i9xx_add_request(struct intel_engine_cs *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001230{
Chris Wilson3cce4692010-10-27 16:11:02 +01001231 int ret;
1232
1233 ret = intel_ring_begin(ring, 4);
1234 if (ret)
1235 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001236
Chris Wilson3cce4692010-10-27 16:11:02 +01001237 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1238 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson18235212013-09-04 10:45:51 +01001239 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +01001240 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001241 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001242
Chris Wilson3cce4692010-10-27 16:11:02 +01001243 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001244}
1245
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001246static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001247gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001248{
1249 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001250 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001251 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001252
1253 if (!dev->irq_enabled)
1254 return false;
1255
Chris Wilson7338aef2012-04-24 21:48:47 +01001256 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001257 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001258 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001259 I915_WRITE_IMR(ring,
1260 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001261 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001262 else
1263 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001264 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001265 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001266 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001267
1268 return true;
1269}
1270
1271static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001272gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001273{
1274 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001275 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001276 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001277
Chris Wilson7338aef2012-04-24 21:48:47 +01001278 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001279 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001280 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001281 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001282 else
1283 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001284 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001285 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001286 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001287}
1288
Ben Widawskya19d2932013-05-28 19:22:30 -07001289static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001290hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001291{
1292 struct drm_device *dev = ring->dev;
1293 struct drm_i915_private *dev_priv = dev->dev_private;
1294 unsigned long flags;
1295
1296 if (!dev->irq_enabled)
1297 return false;
1298
Daniel Vetter59cdb632013-07-04 23:35:28 +02001299 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001300 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001301 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001302 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001303 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001304 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001305
1306 return true;
1307}
1308
1309static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001310hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001311{
1312 struct drm_device *dev = ring->dev;
1313 struct drm_i915_private *dev_priv = dev->dev_private;
1314 unsigned long flags;
1315
1316 if (!dev->irq_enabled)
1317 return;
1318
Daniel Vetter59cdb632013-07-04 23:35:28 +02001319 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001320 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001321 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001322 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001323 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001324 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001325}
1326
Ben Widawskyabd58f02013-11-02 21:07:09 -07001327static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001328gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001329{
1330 struct drm_device *dev = ring->dev;
1331 struct drm_i915_private *dev_priv = dev->dev_private;
1332 unsigned long flags;
1333
1334 if (!dev->irq_enabled)
1335 return false;
1336
1337 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1338 if (ring->irq_refcount++ == 0) {
1339 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1340 I915_WRITE_IMR(ring,
1341 ~(ring->irq_enable_mask |
1342 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1343 } else {
1344 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1345 }
1346 POSTING_READ(RING_IMR(ring->mmio_base));
1347 }
1348 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1349
1350 return true;
1351}
1352
1353static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001354gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001355{
1356 struct drm_device *dev = ring->dev;
1357 struct drm_i915_private *dev_priv = dev->dev_private;
1358 unsigned long flags;
1359
1360 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1361 if (--ring->irq_refcount == 0) {
1362 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1363 I915_WRITE_IMR(ring,
1364 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1365 } else {
1366 I915_WRITE_IMR(ring, ~0);
1367 }
1368 POSTING_READ(RING_IMR(ring->mmio_base));
1369 }
1370 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1371}
1372
Zou Nan haid1b851f2010-05-21 09:08:57 +08001373static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001374i965_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001375 u64 offset, u32 length,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001376 unsigned flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001377{
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001378 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001379
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001380 ret = intel_ring_begin(ring, 2);
1381 if (ret)
1382 return ret;
1383
Chris Wilson78501ea2010-10-27 12:18:21 +01001384 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001385 MI_BATCH_BUFFER_START |
1386 MI_BATCH_GTT |
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001387 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001388 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001389 intel_ring_advance(ring);
1390
Zou Nan haid1b851f2010-05-21 09:08:57 +08001391 return 0;
1392}
1393
Daniel Vetterb45305f2012-12-17 16:21:27 +01001394/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1395#define I830_BATCH_LIMIT (256*1024)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001396static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001397i830_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001398 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001399 unsigned flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001400{
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001401 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001402
Daniel Vetterb45305f2012-12-17 16:21:27 +01001403 if (flags & I915_DISPATCH_PINNED) {
1404 ret = intel_ring_begin(ring, 4);
1405 if (ret)
1406 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001407
Daniel Vetterb45305f2012-12-17 16:21:27 +01001408 intel_ring_emit(ring, MI_BATCH_BUFFER);
1409 intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1410 intel_ring_emit(ring, offset + len - 8);
1411 intel_ring_emit(ring, MI_NOOP);
1412 intel_ring_advance(ring);
1413 } else {
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001414 u32 cs_offset = ring->scratch.gtt_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001415
1416 if (len > I830_BATCH_LIMIT)
1417 return -ENOSPC;
1418
1419 ret = intel_ring_begin(ring, 9+3);
1420 if (ret)
1421 return ret;
1422 /* Blit the batch (which has now all relocs applied) to the stable batch
1423 * scratch bo area (so that the CS never stumbles over its tlb
1424 * invalidation bug) ... */
1425 intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
1426 XY_SRC_COPY_BLT_WRITE_ALPHA |
1427 XY_SRC_COPY_BLT_WRITE_RGB);
1428 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
1429 intel_ring_emit(ring, 0);
1430 intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
1431 intel_ring_emit(ring, cs_offset);
1432 intel_ring_emit(ring, 0);
1433 intel_ring_emit(ring, 4096);
1434 intel_ring_emit(ring, offset);
1435 intel_ring_emit(ring, MI_FLUSH);
1436
1437 /* ... and execute it. */
1438 intel_ring_emit(ring, MI_BATCH_BUFFER);
1439 intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1440 intel_ring_emit(ring, cs_offset + len - 8);
1441 intel_ring_advance(ring);
1442 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001443
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001444 return 0;
1445}
1446
1447static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001448i915_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001449 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001450 unsigned flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001451{
1452 int ret;
1453
1454 ret = intel_ring_begin(ring, 2);
1455 if (ret)
1456 return ret;
1457
Chris Wilson65f56872012-04-17 16:38:12 +01001458 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001459 intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001460 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001461
Eric Anholt62fdfea2010-05-21 13:26:39 -07001462 return 0;
1463}
1464
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001465static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001466{
Chris Wilson05394f32010-11-08 19:18:58 +00001467 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001468
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001469 obj = ring->status_page.obj;
1470 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001471 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001472
Chris Wilson9da3da62012-06-01 15:20:22 +01001473 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001474 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001475 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001476 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001477}
1478
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001479static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001480{
Chris Wilson05394f32010-11-08 19:18:58 +00001481 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001482
Chris Wilsone3efda42014-04-09 09:19:41 +01001483 if ((obj = ring->status_page.obj) == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001484 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001485 int ret;
1486
1487 obj = i915_gem_alloc_object(ring->dev, 4096);
1488 if (obj == NULL) {
1489 DRM_ERROR("Failed to allocate status page\n");
1490 return -ENOMEM;
1491 }
1492
1493 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1494 if (ret)
1495 goto err_unref;
1496
Chris Wilson1f767e02014-07-03 17:33:03 -04001497 flags = 0;
1498 if (!HAS_LLC(ring->dev))
1499 /* On g33, we cannot place HWS above 256MiB, so
1500 * restrict its pinning to the low mappable arena.
1501 * Though this restriction is not documented for
1502 * gen4, gen5, or byt, they also behave similarly
1503 * and hang if the HWS is placed at the top of the
1504 * GTT. To generalise, it appears that all !llc
1505 * platforms have issues with us placing the HWS
1506 * above the mappable region (even though we never
1507 * actualy map it).
1508 */
1509 flags |= PIN_MAPPABLE;
1510 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001511 if (ret) {
1512err_unref:
1513 drm_gem_object_unreference(&obj->base);
1514 return ret;
1515 }
1516
1517 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001518 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001519
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001520 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001521 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001522 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001523
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001524 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1525 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001526
1527 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001528}
1529
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001530static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001531{
1532 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001533
1534 if (!dev_priv->status_page_dmah) {
1535 dev_priv->status_page_dmah =
1536 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1537 if (!dev_priv->status_page_dmah)
1538 return -ENOMEM;
1539 }
1540
Chris Wilson6b8294a2012-11-16 11:43:20 +00001541 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1542 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1543
1544 return 0;
1545}
1546
Oscar Mateo84c23772014-07-24 17:04:15 +01001547void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01001548{
Oscar Mateo2919d292014-07-03 16:28:02 +01001549 if (!ringbuf->obj)
1550 return;
1551
1552 iounmap(ringbuf->virtual_start);
1553 i915_gem_object_ggtt_unpin(ringbuf->obj);
1554 drm_gem_object_unreference(&ringbuf->obj->base);
1555 ringbuf->obj = NULL;
1556}
1557
Oscar Mateo84c23772014-07-24 17:04:15 +01001558int intel_alloc_ringbuffer_obj(struct drm_device *dev,
1559 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01001560{
Chris Wilsone3efda42014-04-09 09:19:41 +01001561 struct drm_i915_private *dev_priv = to_i915(dev);
1562 struct drm_i915_gem_object *obj;
1563 int ret;
1564
Oscar Mateo2919d292014-07-03 16:28:02 +01001565 if (ringbuf->obj)
Chris Wilsone3efda42014-04-09 09:19:41 +01001566 return 0;
1567
1568 obj = NULL;
1569 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001570 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01001571 if (obj == NULL)
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001572 obj = i915_gem_alloc_object(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01001573 if (obj == NULL)
1574 return -ENOMEM;
1575
Akash Goel24f3a8c2014-06-17 10:59:42 +05301576 /* mark ring buffers as read-only from GPU side by default */
1577 obj->gt_ro = 1;
1578
Chris Wilsone3efda42014-04-09 09:19:41 +01001579 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1580 if (ret)
1581 goto err_unref;
1582
1583 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1584 if (ret)
1585 goto err_unpin;
1586
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001587 ringbuf->virtual_start =
Chris Wilsone3efda42014-04-09 09:19:41 +01001588 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001589 ringbuf->size);
1590 if (ringbuf->virtual_start == NULL) {
Chris Wilsone3efda42014-04-09 09:19:41 +01001591 ret = -EINVAL;
1592 goto err_unpin;
1593 }
1594
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001595 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01001596 return 0;
1597
1598err_unpin:
1599 i915_gem_object_ggtt_unpin(obj);
1600err_unref:
1601 drm_gem_object_unreference(&obj->base);
1602 return ret;
1603}
1604
Ben Widawskyc43b5632012-04-16 14:07:40 -07001605static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001606 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001607{
Oscar Mateo8ee14972014-05-22 14:13:34 +01001608 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsondd785e32010-08-07 11:01:34 +01001609 int ret;
1610
Oscar Mateo8ee14972014-05-22 14:13:34 +01001611 if (ringbuf == NULL) {
1612 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1613 if (!ringbuf)
1614 return -ENOMEM;
1615 ring->buffer = ringbuf;
1616 }
1617
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001618 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01001619 INIT_LIST_HEAD(&ring->active_list);
1620 INIT_LIST_HEAD(&ring->request_list);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001621 ringbuf->size = 32 * PAGE_SIZE;
Daniel Vetter0c7dd532014-08-11 16:17:44 +02001622 ringbuf->ring = ring;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001623 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001624
Chris Wilsonb259f672011-03-29 13:19:09 +01001625 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001626
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001627 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001628 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001629 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001630 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001631 } else {
1632 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02001633 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001634 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001635 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001636 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001637
Oscar Mateo2919d292014-07-03 16:28:02 +01001638 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
Chris Wilsone3efda42014-04-09 09:19:41 +01001639 if (ret) {
1640 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n", ring->name, ret);
Oscar Mateo8ee14972014-05-22 14:13:34 +01001641 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001642 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001643
Chris Wilson55249ba2010-12-22 14:04:47 +00001644 /* Workaround an erratum on the i830 which causes a hang if
1645 * the TAIL pointer points to within the last 2 cachelines
1646 * of the buffer.
1647 */
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001648 ringbuf->effective_size = ringbuf->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01001649 if (IS_I830(dev) || IS_845G(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001650 ringbuf->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00001651
Brad Volkin44e895a2014-05-10 14:10:43 -07001652 ret = i915_cmd_parser_init_ring(ring);
1653 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001654 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08001655
Oscar Mateo8ee14972014-05-22 14:13:34 +01001656 ret = ring->init(ring);
1657 if (ret)
1658 goto error;
1659
1660 return 0;
1661
1662error:
1663 kfree(ringbuf);
1664 ring->buffer = NULL;
1665 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001666}
1667
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001668void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001669{
Chris Wilsone3efda42014-04-09 09:19:41 +01001670 struct drm_i915_private *dev_priv = to_i915(ring->dev);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001671 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilson33626e62010-10-29 16:18:36 +01001672
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001673 if (!intel_ring_initialized(ring))
Eric Anholt62fdfea2010-05-21 13:26:39 -07001674 return;
1675
Chris Wilsone3efda42014-04-09 09:19:41 +01001676 intel_stop_ring_buffer(ring);
Ville Syrjäläde8f0a52014-05-28 19:12:13 +03001677 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01001678
Oscar Mateo2919d292014-07-03 16:28:02 +01001679 intel_destroy_ringbuffer_obj(ringbuf);
Ben Widawsky3d57e5b2013-10-14 10:01:36 -07001680 ring->preallocated_lazy_request = NULL;
1681 ring->outstanding_lazy_seqno = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01001682
Zou Nan hai8d192152010-11-02 16:31:01 +08001683 if (ring->cleanup)
1684 ring->cleanup(ring);
1685
Chris Wilson78501ea2010-10-27 12:18:21 +01001686 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07001687
1688 i915_cmd_parser_fini_ring(ring);
Oscar Mateo8ee14972014-05-22 14:13:34 +01001689
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001690 kfree(ringbuf);
Oscar Mateo8ee14972014-05-22 14:13:34 +01001691 ring->buffer = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001692}
1693
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001694static int intel_ring_wait_request(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00001695{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001696 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001697 struct drm_i915_gem_request *request;
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001698 u32 seqno = 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001699 int ret;
1700
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001701 if (ringbuf->last_retired_head != -1) {
1702 ringbuf->head = ringbuf->last_retired_head;
1703 ringbuf->last_retired_head = -1;
Chris Wilson1f709992014-01-27 22:43:07 +00001704
Oscar Mateo64c58f22014-07-03 16:28:03 +01001705 ringbuf->space = ring_space(ringbuf);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001706 if (ringbuf->space >= n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00001707 return 0;
1708 }
1709
1710 list_for_each_entry(request, &ring->request_list, list) {
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001711 if (__ring_space(request->tail, ringbuf->tail, ringbuf->size) >= n) {
Chris Wilsona71d8d92012-02-15 11:25:36 +00001712 seqno = request->seqno;
1713 break;
1714 }
Chris Wilsona71d8d92012-02-15 11:25:36 +00001715 }
1716
1717 if (seqno == 0)
1718 return -ENOSPC;
1719
Chris Wilson1f709992014-01-27 22:43:07 +00001720 ret = i915_wait_seqno(ring, seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001721 if (ret)
1722 return ret;
1723
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001724 i915_gem_retire_requests_ring(ring);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001725 ringbuf->head = ringbuf->last_retired_head;
1726 ringbuf->last_retired_head = -1;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001727
Oscar Mateo64c58f22014-07-03 16:28:03 +01001728 ringbuf->space = ring_space(ringbuf);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001729 return 0;
1730}
1731
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001732static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001733{
Chris Wilson78501ea2010-10-27 12:18:21 +01001734 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001735 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001736 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilson78501ea2010-10-27 12:18:21 +01001737 unsigned long end;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001738 int ret;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001739
Chris Wilsona71d8d92012-02-15 11:25:36 +00001740 ret = intel_ring_wait_request(ring, n);
1741 if (ret != -ENOSPC)
1742 return ret;
1743
Chris Wilson09246732013-08-10 22:16:32 +01001744 /* force the tail write in case we have been skipping them */
1745 __intel_ring_advance(ring);
1746
Daniel Vetter63ed2cb2012-04-23 16:50:50 +02001747 /* With GEM the hangcheck timer should kick us out of the loop,
1748 * leaving it early runs the risk of corrupting GEM state (due
1749 * to running on almost untested codepaths). But on resume
1750 * timers don't work yet, so prevent a complete hang in that
1751 * case by choosing an insanely large timeout. */
1752 end = jiffies + 60 * HZ;
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001753
Chris Wilsondcfe0502014-05-05 09:07:32 +01001754 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001755 do {
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001756 ringbuf->head = I915_READ_HEAD(ring);
Oscar Mateo64c58f22014-07-03 16:28:03 +01001757 ringbuf->space = ring_space(ringbuf);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001758 if (ringbuf->space >= n) {
Chris Wilsondcfe0502014-05-05 09:07:32 +01001759 ret = 0;
1760 break;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001761 }
1762
Daniel Vetterfb19e2a2014-02-12 23:44:34 +01001763 if (!drm_core_check_feature(dev, DRIVER_MODESET) &&
1764 dev->primary->master) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001765 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1766 if (master_priv->sarea_priv)
1767 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1768 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001769
Chris Wilsone60a0b12010-10-13 10:09:14 +01001770 msleep(1);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001771
Chris Wilsondcfe0502014-05-05 09:07:32 +01001772 if (dev_priv->mm.interruptible && signal_pending(current)) {
1773 ret = -ERESTARTSYS;
1774 break;
1775 }
1776
Daniel Vetter33196de2012-11-14 17:14:05 +01001777 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1778 dev_priv->mm.interruptible);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001779 if (ret)
Chris Wilsondcfe0502014-05-05 09:07:32 +01001780 break;
1781
1782 if (time_after(jiffies, end)) {
1783 ret = -EBUSY;
1784 break;
1785 }
1786 } while (1);
Chris Wilsondb53a302011-02-03 11:57:46 +00001787 trace_i915_ring_wait_end(ring);
Chris Wilsondcfe0502014-05-05 09:07:32 +01001788 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001789}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001790
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001791static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00001792{
1793 uint32_t __iomem *virt;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001794 struct intel_ringbuffer *ringbuf = ring->buffer;
1795 int rem = ringbuf->size - ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00001796
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001797 if (ringbuf->space < rem) {
Chris Wilson3e960502012-11-27 16:22:54 +00001798 int ret = ring_wait_for_space(ring, rem);
1799 if (ret)
1800 return ret;
1801 }
1802
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001803 virt = ringbuf->virtual_start + ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00001804 rem /= 4;
1805 while (rem--)
1806 iowrite32(MI_NOOP, virt++);
1807
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001808 ringbuf->tail = 0;
Oscar Mateo64c58f22014-07-03 16:28:03 +01001809 ringbuf->space = ring_space(ringbuf);
Chris Wilson3e960502012-11-27 16:22:54 +00001810
1811 return 0;
1812}
1813
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001814int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00001815{
1816 u32 seqno;
1817 int ret;
1818
1819 /* We need to add any requests required to flush the objects and ring */
Chris Wilson18235212013-09-04 10:45:51 +01001820 if (ring->outstanding_lazy_seqno) {
Mika Kuoppala0025c072013-06-12 12:35:30 +03001821 ret = i915_add_request(ring, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00001822 if (ret)
1823 return ret;
1824 }
1825
1826 /* Wait upon the last request to be completed */
1827 if (list_empty(&ring->request_list))
1828 return 0;
1829
1830 seqno = list_entry(ring->request_list.prev,
1831 struct drm_i915_gem_request,
1832 list)->seqno;
1833
1834 return i915_wait_seqno(ring, seqno);
1835}
1836
Chris Wilson9d7730912012-11-27 16:22:52 +00001837static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001838intel_ring_alloc_seqno(struct intel_engine_cs *ring)
Chris Wilson9d7730912012-11-27 16:22:52 +00001839{
Chris Wilson18235212013-09-04 10:45:51 +01001840 if (ring->outstanding_lazy_seqno)
Chris Wilson9d7730912012-11-27 16:22:52 +00001841 return 0;
1842
Chris Wilson3c0e2342013-09-04 10:45:52 +01001843 if (ring->preallocated_lazy_request == NULL) {
1844 struct drm_i915_gem_request *request;
1845
1846 request = kmalloc(sizeof(*request), GFP_KERNEL);
1847 if (request == NULL)
1848 return -ENOMEM;
1849
1850 ring->preallocated_lazy_request = request;
1851 }
1852
Chris Wilson18235212013-09-04 10:45:51 +01001853 return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
Chris Wilson9d7730912012-11-27 16:22:52 +00001854}
1855
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001856static int __intel_ring_prepare(struct intel_engine_cs *ring,
Chris Wilson304d6952014-01-02 14:32:35 +00001857 int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001858{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001859 struct intel_ringbuffer *ringbuf = ring->buffer;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001860 int ret;
1861
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001862 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001863 ret = intel_wrap_ring_buffer(ring);
1864 if (unlikely(ret))
1865 return ret;
1866 }
1867
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001868 if (unlikely(ringbuf->space < bytes)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001869 ret = ring_wait_for_space(ring, bytes);
1870 if (unlikely(ret))
1871 return ret;
1872 }
1873
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001874 return 0;
1875}
1876
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001877int intel_ring_begin(struct intel_engine_cs *ring,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001878 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001879{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001880 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001881 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001882
Daniel Vetter33196de2012-11-14 17:14:05 +01001883 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1884 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02001885 if (ret)
1886 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00001887
Chris Wilson304d6952014-01-02 14:32:35 +00001888 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
1889 if (ret)
1890 return ret;
1891
Chris Wilson9d7730912012-11-27 16:22:52 +00001892 /* Preallocate the olr before touching the ring */
1893 ret = intel_ring_alloc_seqno(ring);
1894 if (ret)
1895 return ret;
1896
Oscar Mateoee1b1e52014-05-22 14:13:35 +01001897 ring->buffer->space -= num_dwords * sizeof(uint32_t);
Chris Wilson304d6952014-01-02 14:32:35 +00001898 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001899}
1900
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001901/* Align the ring tail to a cacheline boundary */
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001902int intel_ring_cacheline_align(struct intel_engine_cs *ring)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001903{
Oscar Mateoee1b1e52014-05-22 14:13:35 +01001904 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001905 int ret;
1906
1907 if (num_dwords == 0)
1908 return 0;
1909
Chris Wilson18393f62014-04-09 09:19:40 +01001910 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001911 ret = intel_ring_begin(ring, num_dwords);
1912 if (ret)
1913 return ret;
1914
1915 while (num_dwords--)
1916 intel_ring_emit(ring, MI_NOOP);
1917
1918 intel_ring_advance(ring);
1919
1920 return 0;
1921}
1922
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001923void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001924{
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01001925 struct drm_device *dev = ring->dev;
1926 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001927
Chris Wilson18235212013-09-04 10:45:51 +01001928 BUG_ON(ring->outstanding_lazy_seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001929
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01001930 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001931 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
1932 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01001933 if (HAS_VEBOX(dev))
Ben Widawsky50201502013-08-12 16:53:03 -07001934 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01001935 }
Chris Wilson297b0c52010-10-22 17:02:41 +01001936
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001937 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03001938 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01001939}
1940
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001941static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001942 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001943{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001944 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001945
1946 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001947
Chris Wilson12f55812012-07-05 17:14:01 +01001948 /* Disable notification that the ring is IDLE. The GT
1949 * will then assume that it is busy and bring it out of rc6.
1950 */
1951 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1952 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1953
1954 /* Clear the context id. Here be magic! */
1955 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1956
1957 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04001958 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01001959 GEN6_BSD_SLEEP_INDICATOR) == 0,
1960 50))
1961 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001962
Chris Wilson12f55812012-07-05 17:14:01 +01001963 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04001964 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01001965 POSTING_READ(RING_TAIL(ring->mmio_base));
1966
1967 /* Let the ring send IDLE messages to the GT again,
1968 * and so let it sleep to conserve power when idle.
1969 */
Akshay Joshi0206e352011-08-16 15:34:10 -04001970 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01001971 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001972}
1973
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001974static int gen6_bsd_ring_flush(struct intel_engine_cs *ring,
Ben Widawskyea251322013-05-28 19:22:21 -07001975 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001976{
Chris Wilson71a77e02011-02-02 12:13:49 +00001977 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001978 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001979
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001980 ret = intel_ring_begin(ring, 4);
1981 if (ret)
1982 return ret;
1983
Chris Wilson71a77e02011-02-02 12:13:49 +00001984 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001985 if (INTEL_INFO(ring->dev)->gen >= 8)
1986 cmd += 1;
Jesse Barnes9a289772012-10-26 09:42:42 -07001987 /*
1988 * Bspec vol 1c.5 - video engine command streamer:
1989 * "If ENABLED, all TLBs will be invalidated once the flush
1990 * operation is complete. This bit is only valid when the
1991 * Post-Sync Operation field is a value of 1h or 3h."
1992 */
Chris Wilson71a77e02011-02-02 12:13:49 +00001993 if (invalidate & I915_GEM_GPU_DOMAINS)
Jesse Barnes9a289772012-10-26 09:42:42 -07001994 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1995 MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
Chris Wilson71a77e02011-02-02 12:13:49 +00001996 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07001997 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001998 if (INTEL_INFO(ring->dev)->gen >= 8) {
1999 intel_ring_emit(ring, 0); /* upper addr */
2000 intel_ring_emit(ring, 0); /* value */
2001 } else {
2002 intel_ring_emit(ring, 0);
2003 intel_ring_emit(ring, MI_NOOP);
2004 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002005 intel_ring_advance(ring);
2006 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002007}
2008
2009static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002010gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002011 u64 offset, u32 len,
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002012 unsigned flags)
2013{
Ben Widawsky28cf5412013-11-02 21:07:26 -07002014 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2015 bool ppgtt = dev_priv->mm.aliasing_ppgtt != NULL &&
2016 !(flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002017 int ret;
2018
2019 ret = intel_ring_begin(ring, 4);
2020 if (ret)
2021 return ret;
2022
2023 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07002024 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002025 intel_ring_emit(ring, lower_32_bits(offset));
2026 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002027 intel_ring_emit(ring, MI_NOOP);
2028 intel_ring_advance(ring);
2029
2030 return 0;
2031}
2032
2033static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002034hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002035 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002036 unsigned flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002037{
Akshay Joshi0206e352011-08-16 15:34:10 -04002038 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002039
Akshay Joshi0206e352011-08-16 15:34:10 -04002040 ret = intel_ring_begin(ring, 2);
2041 if (ret)
2042 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002043
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002044 intel_ring_emit(ring,
2045 MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
2046 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
2047 /* bit0-7 is the length on GEN6+ */
2048 intel_ring_emit(ring, offset);
2049 intel_ring_advance(ring);
2050
2051 return 0;
2052}
2053
2054static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002055gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002056 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002057 unsigned flags)
2058{
2059 int ret;
2060
2061 ret = intel_ring_begin(ring, 2);
2062 if (ret)
2063 return ret;
2064
2065 intel_ring_emit(ring,
2066 MI_BATCH_BUFFER_START |
2067 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002068 /* bit0-7 is the length on GEN6+ */
2069 intel_ring_emit(ring, offset);
2070 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002071
Akshay Joshi0206e352011-08-16 15:34:10 -04002072 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002073}
2074
Chris Wilson549f7362010-10-19 11:19:32 +01002075/* Blitter support (SandyBridge+) */
2076
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002077static int gen6_ring_flush(struct intel_engine_cs *ring,
Ben Widawskyea251322013-05-28 19:22:21 -07002078 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002079{
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002080 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00002081 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002082 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002083
Daniel Vetter6a233c72011-12-14 13:57:07 +01002084 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002085 if (ret)
2086 return ret;
2087
Chris Wilson71a77e02011-02-02 12:13:49 +00002088 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002089 if (INTEL_INFO(ring->dev)->gen >= 8)
2090 cmd += 1;
Jesse Barnes9a289772012-10-26 09:42:42 -07002091 /*
2092 * Bspec vol 1c.3 - blitter engine command streamer:
2093 * "If ENABLED, all TLBs will be invalidated once the flush
2094 * operation is complete. This bit is only valid when the
2095 * Post-Sync Operation field is a value of 1h or 3h."
2096 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002097 if (invalidate & I915_GEM_DOMAIN_RENDER)
Jesse Barnes9a289772012-10-26 09:42:42 -07002098 cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
Daniel Vetterb3fcabb2012-11-04 12:24:47 +01002099 MI_FLUSH_DW_OP_STOREDW;
Chris Wilson71a77e02011-02-02 12:13:49 +00002100 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002101 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002102 if (INTEL_INFO(ring->dev)->gen >= 8) {
2103 intel_ring_emit(ring, 0); /* upper addr */
2104 intel_ring_emit(ring, 0); /* value */
2105 } else {
2106 intel_ring_emit(ring, 0);
2107 intel_ring_emit(ring, MI_NOOP);
2108 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002109 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002110
Ville Syrjälä9688eca2013-11-06 23:02:19 +02002111 if (IS_GEN7(dev) && !invalidate && flush)
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002112 return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
2113
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002114 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002115}
2116
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002117int intel_init_render_ring_buffer(struct drm_device *dev)
2118{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002119 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002120 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002121 struct drm_i915_gem_object *obj;
2122 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002123
Daniel Vetter59465b52012-04-11 22:12:48 +02002124 ring->name = "render ring";
2125 ring->id = RCS;
2126 ring->mmio_base = RENDER_RING_BASE;
2127
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002128 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002129 if (i915_semaphore_is_enabled(dev)) {
2130 obj = i915_gem_alloc_object(dev, 4096);
2131 if (obj == NULL) {
2132 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2133 i915.semaphores = 0;
2134 } else {
2135 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2136 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2137 if (ret != 0) {
2138 drm_gem_object_unreference(&obj->base);
2139 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2140 i915.semaphores = 0;
2141 } else
2142 dev_priv->semaphore_obj = obj;
2143 }
2144 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002145 ring->add_request = gen6_add_request;
2146 ring->flush = gen8_render_ring_flush;
2147 ring->irq_get = gen8_ring_get_irq;
2148 ring->irq_put = gen8_ring_put_irq;
2149 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2150 ring->get_seqno = gen6_ring_get_seqno;
2151 ring->set_seqno = ring_set_seqno;
2152 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002153 WARN_ON(!dev_priv->semaphore_obj);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002154 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002155 ring->semaphore.signal = gen8_rcs_signal;
2156 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002157 }
2158 } else if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002159 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03002160 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01002161 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03002162 ring->flush = gen6_render_ring_flush;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002163 ring->irq_get = gen6_ring_get_irq;
2164 ring->irq_put = gen6_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002165 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01002166 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002167 ring->set_seqno = ring_set_seqno;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002168 if (i915_semaphore_is_enabled(dev)) {
2169 ring->semaphore.sync_to = gen6_ring_sync;
2170 ring->semaphore.signal = gen6_signal;
2171 /*
2172 * The current semaphore is only applied on pre-gen8
2173 * platform. And there is no VCS2 ring on the pre-gen8
2174 * platform. So the semaphore between RCS and VCS2 is
2175 * initialized as INVALID. Gen8 will initialize the
2176 * sema between VCS2 and RCS later.
2177 */
2178 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2179 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2180 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2181 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2182 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2183 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2184 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2185 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2186 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2187 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2188 }
Chris Wilsonc6df5412010-12-15 09:56:50 +00002189 } else if (IS_GEN5(dev)) {
2190 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002191 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00002192 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002193 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002194 ring->irq_get = gen5_ring_get_irq;
2195 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002196 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2197 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002198 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002199 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002200 if (INTEL_INFO(dev)->gen < 4)
2201 ring->flush = gen2_render_ring_flush;
2202 else
2203 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002204 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002205 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002206 if (IS_GEN2(dev)) {
2207 ring->irq_get = i8xx_ring_get_irq;
2208 ring->irq_put = i8xx_ring_put_irq;
2209 } else {
2210 ring->irq_get = i9xx_ring_get_irq;
2211 ring->irq_put = i9xx_ring_put_irq;
2212 }
Daniel Vettere3670312012-04-11 22:12:53 +02002213 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002214 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002215 ring->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002216
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002217 if (IS_HASWELL(dev))
2218 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002219 else if (IS_GEN8(dev))
2220 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002221 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002222 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2223 else if (INTEL_INFO(dev)->gen >= 4)
2224 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2225 else if (IS_I830(dev) || IS_845G(dev))
2226 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2227 else
2228 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02002229 ring->init = init_render_ring;
2230 ring->cleanup = render_ring_cleanup;
2231
Daniel Vetterb45305f2012-12-17 16:21:27 +01002232 /* Workaround batchbuffer to combat CS tlb bug. */
2233 if (HAS_BROKEN_CS_TLB(dev)) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01002234 obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
2235 if (obj == NULL) {
2236 DRM_ERROR("Failed to allocate batch bo\n");
2237 return -ENOMEM;
2238 }
2239
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002240 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002241 if (ret != 0) {
2242 drm_gem_object_unreference(&obj->base);
2243 DRM_ERROR("Failed to ping batch bo\n");
2244 return ret;
2245 }
2246
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002247 ring->scratch.obj = obj;
2248 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002249 }
2250
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002251 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002252}
2253
Chris Wilsone8616b62011-01-20 09:57:11 +00002254int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
2255{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002256 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002257 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Oscar Mateo8ee14972014-05-22 14:13:34 +01002258 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002259 int ret;
Chris Wilsone8616b62011-01-20 09:57:11 +00002260
Oscar Mateo8ee14972014-05-22 14:13:34 +01002261 if (ringbuf == NULL) {
2262 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2263 if (!ringbuf)
2264 return -ENOMEM;
2265 ring->buffer = ringbuf;
2266 }
2267
Daniel Vetter59465b52012-04-11 22:12:48 +02002268 ring->name = "render ring";
2269 ring->id = RCS;
2270 ring->mmio_base = RENDER_RING_BASE;
2271
Chris Wilsone8616b62011-01-20 09:57:11 +00002272 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetterb4178f82012-04-11 22:12:51 +02002273 /* non-kms not supported on gen6+ */
Oscar Mateo8ee14972014-05-22 14:13:34 +01002274 ret = -ENODEV;
2275 goto err_ringbuf;
Chris Wilsone8616b62011-01-20 09:57:11 +00002276 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002277
2278 /* Note: gem is not supported on gen5/ilk without kms (the corresponding
2279 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
2280 * the special gen5 functions. */
2281 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002282 if (INTEL_INFO(dev)->gen < 4)
2283 ring->flush = gen2_render_ring_flush;
2284 else
2285 ring->flush = gen4_render_ring_flush;
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002286 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002287 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002288 if (IS_GEN2(dev)) {
2289 ring->irq_get = i8xx_ring_get_irq;
2290 ring->irq_put = i8xx_ring_put_irq;
2291 } else {
2292 ring->irq_get = i9xx_ring_get_irq;
2293 ring->irq_put = i9xx_ring_put_irq;
2294 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002295 ring->irq_enable_mask = I915_USER_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002296 ring->write_tail = ring_write_tail;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002297 if (INTEL_INFO(dev)->gen >= 4)
2298 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2299 else if (IS_I830(dev) || IS_845G(dev))
2300 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2301 else
2302 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02002303 ring->init = init_render_ring;
2304 ring->cleanup = render_ring_cleanup;
Chris Wilsone8616b62011-01-20 09:57:11 +00002305
2306 ring->dev = dev;
2307 INIT_LIST_HEAD(&ring->active_list);
2308 INIT_LIST_HEAD(&ring->request_list);
Chris Wilsone8616b62011-01-20 09:57:11 +00002309
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002310 ringbuf->size = size;
2311 ringbuf->effective_size = ringbuf->size;
Mika Kuoppala17f10fd2012-10-29 16:59:26 +02002312 if (IS_I830(ring->dev) || IS_845G(ring->dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002313 ringbuf->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilsone8616b62011-01-20 09:57:11 +00002314
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002315 ringbuf->virtual_start = ioremap_wc(start, size);
2316 if (ringbuf->virtual_start == NULL) {
Chris Wilsone8616b62011-01-20 09:57:11 +00002317 DRM_ERROR("can not ioremap virtual address for"
2318 " ring buffer\n");
Oscar Mateo8ee14972014-05-22 14:13:34 +01002319 ret = -ENOMEM;
2320 goto err_ringbuf;
Chris Wilsone8616b62011-01-20 09:57:11 +00002321 }
2322
Chris Wilson6b8294a2012-11-16 11:43:20 +00002323 if (!I915_NEED_GFX_HWS(dev)) {
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002324 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002325 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002326 goto err_vstart;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002327 }
2328
Chris Wilsone8616b62011-01-20 09:57:11 +00002329 return 0;
Oscar Mateo8ee14972014-05-22 14:13:34 +01002330
2331err_vstart:
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002332 iounmap(ringbuf->virtual_start);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002333err_ringbuf:
2334 kfree(ringbuf);
2335 ring->buffer = NULL;
2336 return ret;
Chris Wilsone8616b62011-01-20 09:57:11 +00002337}
2338
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002339int intel_init_bsd_ring_buffer(struct drm_device *dev)
2340{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002341 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002342 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002343
Daniel Vetter58fa3832012-04-11 22:12:49 +02002344 ring->name = "bsd ring";
2345 ring->id = VCS;
2346
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002347 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002348 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002349 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002350 /* gen6 bsd needs a special wa for tail updates */
2351 if (IS_GEN6(dev))
2352 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002353 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002354 ring->add_request = gen6_add_request;
2355 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002356 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002357 if (INTEL_INFO(dev)->gen >= 8) {
2358 ring->irq_enable_mask =
2359 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2360 ring->irq_get = gen8_ring_get_irq;
2361 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002362 ring->dispatch_execbuffer =
2363 gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002364 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002365 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002366 ring->semaphore.signal = gen8_xcs_signal;
2367 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002368 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002369 } else {
2370 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2371 ring->irq_get = gen6_ring_get_irq;
2372 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002373 ring->dispatch_execbuffer =
2374 gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002375 if (i915_semaphore_is_enabled(dev)) {
2376 ring->semaphore.sync_to = gen6_ring_sync;
2377 ring->semaphore.signal = gen6_signal;
2378 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2379 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2380 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2381 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2382 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2383 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2384 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2385 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2386 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2387 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2388 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002389 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002390 } else {
2391 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002392 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002393 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002394 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002395 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002396 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002397 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002398 ring->irq_get = gen5_ring_get_irq;
2399 ring->irq_put = gen5_ring_put_irq;
2400 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002401 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002402 ring->irq_get = i9xx_ring_get_irq;
2403 ring->irq_put = i9xx_ring_put_irq;
2404 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002405 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002406 }
2407 ring->init = init_ring_common;
2408
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002409 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002410}
Chris Wilson549f7362010-10-19 11:19:32 +01002411
Zhao Yakui845f74a2014-04-17 10:37:37 +08002412/**
2413 * Initialize the second BSD ring for Broadwell GT3.
2414 * It is noted that this only exists on Broadwell GT3.
2415 */
2416int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2417{
2418 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002419 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002420
2421 if ((INTEL_INFO(dev)->gen != 8)) {
2422 DRM_ERROR("No dual-BSD ring on non-BDW machine\n");
2423 return -EINVAL;
2424 }
2425
Rodrigo Vivif7b64232014-07-01 02:41:36 -07002426 ring->name = "bsd2 ring";
Zhao Yakui845f74a2014-04-17 10:37:37 +08002427 ring->id = VCS2;
2428
2429 ring->write_tail = ring_write_tail;
2430 ring->mmio_base = GEN8_BSD2_RING_BASE;
2431 ring->flush = gen6_bsd_ring_flush;
2432 ring->add_request = gen6_add_request;
2433 ring->get_seqno = gen6_ring_get_seqno;
2434 ring->set_seqno = ring_set_seqno;
2435 ring->irq_enable_mask =
2436 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2437 ring->irq_get = gen8_ring_get_irq;
2438 ring->irq_put = gen8_ring_put_irq;
2439 ring->dispatch_execbuffer =
2440 gen8_ring_dispatch_execbuffer;
Ben Widawsky3e789982014-06-30 09:53:37 -07002441 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002442 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002443 ring->semaphore.signal = gen8_xcs_signal;
2444 GEN8_RING_SEMAPHORE_INIT;
2445 }
Zhao Yakui845f74a2014-04-17 10:37:37 +08002446 ring->init = init_ring_common;
2447
2448 return intel_init_ring_buffer(dev, ring);
2449}
2450
Chris Wilson549f7362010-10-19 11:19:32 +01002451int intel_init_blt_ring_buffer(struct drm_device *dev)
2452{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002453 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002454 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002455
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002456 ring->name = "blitter ring";
2457 ring->id = BCS;
2458
2459 ring->mmio_base = BLT_RING_BASE;
2460 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002461 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002462 ring->add_request = gen6_add_request;
2463 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002464 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002465 if (INTEL_INFO(dev)->gen >= 8) {
2466 ring->irq_enable_mask =
2467 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2468 ring->irq_get = gen8_ring_get_irq;
2469 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002470 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002471 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002472 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002473 ring->semaphore.signal = gen8_xcs_signal;
2474 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002475 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002476 } else {
2477 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2478 ring->irq_get = gen6_ring_get_irq;
2479 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002480 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002481 if (i915_semaphore_is_enabled(dev)) {
2482 ring->semaphore.signal = gen6_signal;
2483 ring->semaphore.sync_to = gen6_ring_sync;
2484 /*
2485 * The current semaphore is only applied on pre-gen8
2486 * platform. And there is no VCS2 ring on the pre-gen8
2487 * platform. So the semaphore between BCS and VCS2 is
2488 * initialized as INVALID. Gen8 will initialize the
2489 * sema between BCS and VCS2 later.
2490 */
2491 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2492 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2493 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2494 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2495 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2496 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2497 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2498 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2499 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2500 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2501 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002502 }
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002503 ring->init = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002504
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002505 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002506}
Chris Wilsona7b97612012-07-20 12:41:08 +01002507
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002508int intel_init_vebox_ring_buffer(struct drm_device *dev)
2509{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002510 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002511 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002512
2513 ring->name = "video enhancement ring";
2514 ring->id = VECS;
2515
2516 ring->mmio_base = VEBOX_RING_BASE;
2517 ring->write_tail = ring_write_tail;
2518 ring->flush = gen6_ring_flush;
2519 ring->add_request = gen6_add_request;
2520 ring->get_seqno = gen6_ring_get_seqno;
2521 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002522
2523 if (INTEL_INFO(dev)->gen >= 8) {
2524 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002525 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002526 ring->irq_get = gen8_ring_get_irq;
2527 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002528 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002529 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002530 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002531 ring->semaphore.signal = gen8_xcs_signal;
2532 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002533 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002534 } else {
2535 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2536 ring->irq_get = hsw_vebox_get_irq;
2537 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002538 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002539 if (i915_semaphore_is_enabled(dev)) {
2540 ring->semaphore.sync_to = gen6_ring_sync;
2541 ring->semaphore.signal = gen6_signal;
2542 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2543 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2544 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2545 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2546 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2547 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2548 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2549 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2550 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2551 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2552 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002553 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002554 ring->init = init_ring_common;
2555
2556 return intel_init_ring_buffer(dev, ring);
2557}
2558
Chris Wilsona7b97612012-07-20 12:41:08 +01002559int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002560intel_ring_flush_all_caches(struct intel_engine_cs *ring)
Chris Wilsona7b97612012-07-20 12:41:08 +01002561{
2562 int ret;
2563
2564 if (!ring->gpu_caches_dirty)
2565 return 0;
2566
2567 ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2568 if (ret)
2569 return ret;
2570
2571 trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2572
2573 ring->gpu_caches_dirty = false;
2574 return 0;
2575}
2576
2577int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002578intel_ring_invalidate_all_caches(struct intel_engine_cs *ring)
Chris Wilsona7b97612012-07-20 12:41:08 +01002579{
2580 uint32_t flush_domains;
2581 int ret;
2582
2583 flush_domains = 0;
2584 if (ring->gpu_caches_dirty)
2585 flush_domains = I915_GEM_GPU_DOMAINS;
2586
2587 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2588 if (ret)
2589 return ret;
2590
2591 trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2592
2593 ring->gpu_caches_dirty = false;
2594 return 0;
2595}
Chris Wilsone3efda42014-04-09 09:19:41 +01002596
2597void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002598intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002599{
2600 int ret;
2601
2602 if (!intel_ring_initialized(ring))
2603 return;
2604
2605 ret = intel_ring_idle(ring);
2606 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2607 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2608 ring->name, ret);
2609
2610 stop_ring(ring);
2611}