blob: 748041aa84df5cee6b33f512ec179576833d0fdf [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
Chris Wilson1cf0ba12014-05-05 09:07:33 +010043static inline int __ring_space(int head, int tail, int size)
44{
45 int space = head - (tail + I915_RING_FREE_SPACE);
46 if (space < 0)
47 space += size;
48 return space;
49}
50
Oscar Mateoa4872ba2014-05-22 14:13:33 +010051static inline int ring_space(struct intel_engine_cs *ring)
Chris Wilsonc7dca472011-01-20 17:00:10 +000052{
Chris Wilson1cf0ba12014-05-05 09:07:33 +010053 return __ring_space(ring->head & HEAD_ADDR, ring->tail, ring->size);
Chris Wilsonc7dca472011-01-20 17:00:10 +000054}
55
Oscar Mateoa4872ba2014-05-22 14:13:33 +010056static bool intel_ring_stopped(struct intel_engine_cs *ring)
Chris Wilson09246732013-08-10 22:16:32 +010057{
58 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020059 return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
60}
Chris Wilson09246732013-08-10 22:16:32 +010061
Oscar Mateoa4872ba2014-05-22 14:13:33 +010062void __intel_ring_advance(struct intel_engine_cs *ring)
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020063{
Chris Wilson09246732013-08-10 22:16:32 +010064 ring->tail &= ring->size - 1;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020065 if (intel_ring_stopped(ring))
Chris Wilson09246732013-08-10 22:16:32 +010066 return;
67 ring->write_tail(ring, ring->tail);
68}
69
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000070static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +010071gen2_render_ring_flush(struct intel_engine_cs *ring,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010072 u32 invalidate_domains,
73 u32 flush_domains)
74{
75 u32 cmd;
76 int ret;
77
78 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +020079 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010080 cmd |= MI_NO_WRITE_FLUSH;
81
82 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
83 cmd |= MI_READ_FLUSH;
84
85 ret = intel_ring_begin(ring, 2);
86 if (ret)
87 return ret;
88
89 intel_ring_emit(ring, cmd);
90 intel_ring_emit(ring, MI_NOOP);
91 intel_ring_advance(ring);
92
93 return 0;
94}
95
96static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +010097gen4_render_ring_flush(struct intel_engine_cs *ring,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010098 u32 invalidate_domains,
99 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700100{
Chris Wilson78501ea2010-10-27 12:18:21 +0100101 struct drm_device *dev = ring->dev;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100102 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000103 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100104
Chris Wilson36d527d2011-03-19 22:26:49 +0000105 /*
106 * read/write caches:
107 *
108 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
109 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
110 * also flushed at 2d versus 3d pipeline switches.
111 *
112 * read-only caches:
113 *
114 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
115 * MI_READ_FLUSH is set, and is always flushed on 965.
116 *
117 * I915_GEM_DOMAIN_COMMAND may not exist?
118 *
119 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
120 * invalidated when MI_EXE_FLUSH is set.
121 *
122 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
123 * invalidated with every MI_FLUSH.
124 *
125 * TLBs:
126 *
127 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
128 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
129 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
130 * are flushed at any MI_FLUSH.
131 */
132
133 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100134 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000135 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000136 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
137 cmd |= MI_EXE_FLUSH;
138
139 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
140 (IS_G4X(dev) || IS_GEN5(dev)))
141 cmd |= MI_INVALIDATE_ISP;
142
143 ret = intel_ring_begin(ring, 2);
144 if (ret)
145 return ret;
146
147 intel_ring_emit(ring, cmd);
148 intel_ring_emit(ring, MI_NOOP);
149 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000150
151 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800152}
153
Jesse Barnes8d315282011-10-16 10:23:31 +0200154/**
155 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
156 * implementing two workarounds on gen6. From section 1.4.7.1
157 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
158 *
159 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
160 * produced by non-pipelined state commands), software needs to first
161 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
162 * 0.
163 *
164 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
165 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
166 *
167 * And the workaround for these two requires this workaround first:
168 *
169 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
170 * BEFORE the pipe-control with a post-sync op and no write-cache
171 * flushes.
172 *
173 * And this last workaround is tricky because of the requirements on
174 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
175 * volume 2 part 1:
176 *
177 * "1 of the following must also be set:
178 * - Render Target Cache Flush Enable ([12] of DW1)
179 * - Depth Cache Flush Enable ([0] of DW1)
180 * - Stall at Pixel Scoreboard ([1] of DW1)
181 * - Depth Stall ([13] of DW1)
182 * - Post-Sync Operation ([13] of DW1)
183 * - Notify Enable ([8] of DW1)"
184 *
185 * The cache flushes require the workaround flush that triggered this
186 * one, so we can't use it. Depth stall would trigger the same.
187 * Post-sync nonzero is what triggered this second workaround, so we
188 * can't use that one either. Notify enable is IRQs, which aren't
189 * really our business. That leaves only stall at scoreboard.
190 */
191static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100192intel_emit_post_sync_nonzero_flush(struct intel_engine_cs *ring)
Jesse Barnes8d315282011-10-16 10:23:31 +0200193{
Chris Wilson18393f62014-04-09 09:19:40 +0100194 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200195 int ret;
196
197
198 ret = intel_ring_begin(ring, 6);
199 if (ret)
200 return ret;
201
202 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
203 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
204 PIPE_CONTROL_STALL_AT_SCOREBOARD);
205 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
206 intel_ring_emit(ring, 0); /* low dword */
207 intel_ring_emit(ring, 0); /* high dword */
208 intel_ring_emit(ring, MI_NOOP);
209 intel_ring_advance(ring);
210
211 ret = intel_ring_begin(ring, 6);
212 if (ret)
213 return ret;
214
215 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
216 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
217 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
218 intel_ring_emit(ring, 0);
219 intel_ring_emit(ring, 0);
220 intel_ring_emit(ring, MI_NOOP);
221 intel_ring_advance(ring);
222
223 return 0;
224}
225
226static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100227gen6_render_ring_flush(struct intel_engine_cs *ring,
Jesse Barnes8d315282011-10-16 10:23:31 +0200228 u32 invalidate_domains, u32 flush_domains)
229{
230 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100231 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200232 int ret;
233
Paulo Zanonib3111502012-08-17 18:35:42 -0300234 /* Force SNB workarounds for PIPE_CONTROL flushes */
235 ret = intel_emit_post_sync_nonzero_flush(ring);
236 if (ret)
237 return ret;
238
Jesse Barnes8d315282011-10-16 10:23:31 +0200239 /* Just flush everything. Experiments have shown that reducing the
240 * number of bits based on the write domains has little performance
241 * impact.
242 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100243 if (flush_domains) {
244 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
245 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
246 /*
247 * Ensure that any following seqno writes only happen
248 * when the render cache is indeed flushed.
249 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200250 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100251 }
252 if (invalidate_domains) {
253 flags |= PIPE_CONTROL_TLB_INVALIDATE;
254 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
255 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
256 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
257 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
258 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
259 /*
260 * TLB invalidate requires a post-sync write.
261 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700262 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100263 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200264
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100265 ret = intel_ring_begin(ring, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200266 if (ret)
267 return ret;
268
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100269 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
Jesse Barnes8d315282011-10-16 10:23:31 +0200270 intel_ring_emit(ring, flags);
271 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100272 intel_ring_emit(ring, 0);
Jesse Barnes8d315282011-10-16 10:23:31 +0200273 intel_ring_advance(ring);
274
275 return 0;
276}
277
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100278static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100279gen7_render_ring_cs_stall_wa(struct intel_engine_cs *ring)
Paulo Zanonif3987632012-08-17 18:35:43 -0300280{
281 int ret;
282
283 ret = intel_ring_begin(ring, 4);
284 if (ret)
285 return ret;
286
287 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
288 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
289 PIPE_CONTROL_STALL_AT_SCOREBOARD);
290 intel_ring_emit(ring, 0);
291 intel_ring_emit(ring, 0);
292 intel_ring_advance(ring);
293
294 return 0;
295}
296
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100297static int gen7_ring_fbc_flush(struct intel_engine_cs *ring, u32 value)
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300298{
299 int ret;
300
301 if (!ring->fbc_dirty)
302 return 0;
303
Ville Syrjälä37c1d942013-11-06 23:02:20 +0200304 ret = intel_ring_begin(ring, 6);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300305 if (ret)
306 return ret;
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300307 /* WaFbcNukeOn3DBlt:ivb/hsw */
308 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
309 intel_ring_emit(ring, MSG_FBC_REND_STATE);
310 intel_ring_emit(ring, value);
Ville Syrjälä37c1d942013-11-06 23:02:20 +0200311 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) | MI_SRM_LRM_GLOBAL_GTT);
312 intel_ring_emit(ring, MSG_FBC_REND_STATE);
313 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300314 intel_ring_advance(ring);
315
316 ring->fbc_dirty = false;
317 return 0;
318}
319
Paulo Zanonif3987632012-08-17 18:35:43 -0300320static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100321gen7_render_ring_flush(struct intel_engine_cs *ring,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300322 u32 invalidate_domains, u32 flush_domains)
323{
324 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100325 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300326 int ret;
327
Paulo Zanonif3987632012-08-17 18:35:43 -0300328 /*
329 * Ensure that any following seqno writes only happen when the render
330 * cache is indeed flushed.
331 *
332 * Workaround: 4th PIPE_CONTROL command (except the ones with only
333 * read-cache invalidate bits set) must have the CS_STALL bit set. We
334 * don't try to be clever and just set it unconditionally.
335 */
336 flags |= PIPE_CONTROL_CS_STALL;
337
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300338 /* Just flush everything. Experiments have shown that reducing the
339 * number of bits based on the write domains has little performance
340 * impact.
341 */
342 if (flush_domains) {
343 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
344 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300345 }
346 if (invalidate_domains) {
347 flags |= PIPE_CONTROL_TLB_INVALIDATE;
348 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
349 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
350 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
351 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
352 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
353 /*
354 * TLB invalidate requires a post-sync write.
355 */
356 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200357 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300358
359 /* Workaround: we must issue a pipe_control with CS-stall bit
360 * set before a pipe_control command that has the state cache
361 * invalidate bit set. */
362 gen7_render_ring_cs_stall_wa(ring);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300363 }
364
365 ret = intel_ring_begin(ring, 4);
366 if (ret)
367 return ret;
368
369 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
370 intel_ring_emit(ring, flags);
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200371 intel_ring_emit(ring, scratch_addr);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300372 intel_ring_emit(ring, 0);
373 intel_ring_advance(ring);
374
Ville Syrjälä9688eca2013-11-06 23:02:19 +0200375 if (!invalidate_domains && flush_domains)
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300376 return gen7_ring_fbc_flush(ring, FBC_REND_NUKE);
377
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300378 return 0;
379}
380
Ben Widawskya5f3d682013-11-02 21:07:27 -0700381static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100382gen8_render_ring_flush(struct intel_engine_cs *ring,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700383 u32 invalidate_domains, u32 flush_domains)
384{
385 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100386 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700387 int ret;
388
389 flags |= PIPE_CONTROL_CS_STALL;
390
391 if (flush_domains) {
392 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
393 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
394 }
395 if (invalidate_domains) {
396 flags |= PIPE_CONTROL_TLB_INVALIDATE;
397 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
398 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
399 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
400 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
401 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
402 flags |= PIPE_CONTROL_QW_WRITE;
403 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
404 }
405
406 ret = intel_ring_begin(ring, 6);
407 if (ret)
408 return ret;
409
410 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
411 intel_ring_emit(ring, flags);
412 intel_ring_emit(ring, scratch_addr);
413 intel_ring_emit(ring, 0);
414 intel_ring_emit(ring, 0);
415 intel_ring_emit(ring, 0);
416 intel_ring_advance(ring);
417
418 return 0;
419
420}
421
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100422static void ring_write_tail(struct intel_engine_cs *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100423 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800424{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300425 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100426 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800427}
428
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100429u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800430{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300431 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson50877442014-03-21 12:41:53 +0000432 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800433
Chris Wilson50877442014-03-21 12:41:53 +0000434 if (INTEL_INFO(ring->dev)->gen >= 8)
435 acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
436 RING_ACTHD_UDW(ring->mmio_base));
437 else if (INTEL_INFO(ring->dev)->gen >= 4)
438 acthd = I915_READ(RING_ACTHD(ring->mmio_base));
439 else
440 acthd = I915_READ(ACTHD);
441
442 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800443}
444
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100445static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200446{
447 struct drm_i915_private *dev_priv = ring->dev->dev_private;
448 u32 addr;
449
450 addr = dev_priv->status_page_dmah->busaddr;
451 if (INTEL_INFO(ring->dev)->gen >= 4)
452 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
453 I915_WRITE(HWS_PGA, addr);
454}
455
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100456static bool stop_ring(struct intel_engine_cs *ring)
Chris Wilson9991ae72014-04-02 16:36:07 +0100457{
458 struct drm_i915_private *dev_priv = to_i915(ring->dev);
459
460 if (!IS_GEN2(ring->dev)) {
461 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
462 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
463 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
464 return false;
465 }
466 }
467
468 I915_WRITE_CTL(ring, 0);
469 I915_WRITE_HEAD(ring, 0);
470 ring->write_tail(ring, 0);
471
472 if (!IS_GEN2(ring->dev)) {
473 (void)I915_READ_CTL(ring);
474 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
475 }
476
477 return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
478}
479
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100480static int init_ring_common(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800481{
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200482 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300483 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000484 struct drm_i915_gem_object *obj = ring->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200485 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800486
Deepak Sc8d9a592013-11-23 14:55:42 +0530487 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200488
Chris Wilson9991ae72014-04-02 16:36:07 +0100489 if (!stop_ring(ring)) {
490 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000491 DRM_DEBUG_KMS("%s head not reset to zero "
492 "ctl %08x head %08x tail %08x start %08x\n",
493 ring->name,
494 I915_READ_CTL(ring),
495 I915_READ_HEAD(ring),
496 I915_READ_TAIL(ring),
497 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800498
Chris Wilson9991ae72014-04-02 16:36:07 +0100499 if (!stop_ring(ring)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000500 DRM_ERROR("failed to set %s head to zero "
501 "ctl %08x head %08x tail %08x start %08x\n",
502 ring->name,
503 I915_READ_CTL(ring),
504 I915_READ_HEAD(ring),
505 I915_READ_TAIL(ring),
506 I915_READ_START(ring));
Chris Wilson9991ae72014-04-02 16:36:07 +0100507 ret = -EIO;
508 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000509 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700510 }
511
Chris Wilson9991ae72014-04-02 16:36:07 +0100512 if (I915_NEED_GFX_HWS(dev))
513 intel_ring_setup_status_page(ring);
514 else
515 ring_setup_phys_status_page(ring);
516
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200517 /* Initialize the ring. This must happen _after_ we've cleared the ring
518 * registers with the above sequence (the readback of the HEAD registers
519 * also enforces ordering), otherwise the hw might lose the new ring
520 * register values. */
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700521 I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200522 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000523 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000524 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800525
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800526 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400527 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700528 I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
Sean Paulf01db982012-03-16 12:43:22 -0400529 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000530 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100531 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
532 ring->name,
533 I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
534 I915_READ_HEAD(ring), I915_READ_TAIL(ring),
535 I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200536 ret = -EIO;
537 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800538 }
539
Chris Wilson78501ea2010-10-27 12:18:21 +0100540 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
541 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800542 else {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000543 ring->head = I915_READ_HEAD(ring);
Daniel Vetter870e86d2010-08-02 16:29:44 +0200544 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000545 ring->space = ring_space(ring);
Chris Wilsonc3b20032012-05-28 22:33:02 +0100546 ring->last_retired_head = -1;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800547 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000548
Chris Wilson50f018d2013-06-10 11:20:19 +0100549 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
550
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200551out:
Deepak Sc8d9a592013-11-23 14:55:42 +0530552 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200553
554 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700555}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800556
Chris Wilsonc6df5412010-12-15 09:56:50 +0000557static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100558init_pipe_control(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000559{
Chris Wilsonc6df5412010-12-15 09:56:50 +0000560 int ret;
561
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100562 if (ring->scratch.obj)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000563 return 0;
564
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100565 ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
566 if (ring->scratch.obj == NULL) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000567 DRM_ERROR("Failed to allocate seqno page\n");
568 ret = -ENOMEM;
569 goto err;
570 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100571
Daniel Vettera9cc7262014-02-14 14:01:13 +0100572 ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
573 if (ret)
574 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000575
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100576 ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000577 if (ret)
578 goto err_unref;
579
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100580 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
581 ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
582 if (ring->scratch.cpu_page == NULL) {
Wei Yongjun56b085a2013-05-28 17:51:44 +0800583 ret = -ENOMEM;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000584 goto err_unpin;
Wei Yongjun56b085a2013-05-28 17:51:44 +0800585 }
Chris Wilsonc6df5412010-12-15 09:56:50 +0000586
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200587 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100588 ring->name, ring->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000589 return 0;
590
591err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800592 i915_gem_object_ggtt_unpin(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000593err_unref:
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100594 drm_gem_object_unreference(&ring->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000595err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000596 return ret;
597}
598
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100599static int init_render_ring(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800600{
Chris Wilson78501ea2010-10-27 12:18:21 +0100601 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000602 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100603 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800604
Akash Goel61a563a2014-03-25 18:01:50 +0530605 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
606 if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
Daniel Vetter6b26c862012-04-24 14:04:12 +0200607 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000608
609 /* We need to disable the AsyncFlip performance optimisations in order
610 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
611 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +0100612 *
Ville Syrjäläb3f797a2014-04-28 14:31:09 +0300613 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000614 */
615 if (INTEL_INFO(dev)->gen >= 6)
616 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
617
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000618 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +0530619 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000620 if (INTEL_INFO(dev)->gen == 6)
621 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +0000622 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000623
Akash Goel01fa0302014-03-24 23:00:04 +0530624 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000625 if (IS_GEN7(dev))
626 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +0530627 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000628 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +0100629
Jesse Barnes8d315282011-10-16 10:23:31 +0200630 if (INTEL_INFO(dev)->gen >= 5) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000631 ret = init_pipe_control(ring);
632 if (ret)
633 return ret;
634 }
635
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200636 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700637 /* From the Sandybridge PRM, volume 1 part 3, page 24:
638 * "If this bit is set, STCunit will have LRA as replacement
639 * policy. [...] This bit must be reset. LRA replacement
640 * policy is not supported."
641 */
642 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200643 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -0800644 }
645
Daniel Vetter6b26c862012-04-24 14:04:12 +0200646 if (INTEL_INFO(dev)->gen >= 6)
647 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +0000648
Ben Widawsky040d2ba2013-09-19 11:01:40 -0700649 if (HAS_L3_DPF(dev))
Ben Widawsky35a85ac2013-09-19 11:13:41 -0700650 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -0700651
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800652 return ret;
653}
654
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100655static void render_ring_cleanup(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000656{
Daniel Vetterb45305f2012-12-17 16:21:27 +0100657 struct drm_device *dev = ring->dev;
658
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100659 if (ring->scratch.obj == NULL)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000660 return;
661
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100662 if (INTEL_INFO(dev)->gen >= 5) {
663 kunmap(sg_page(ring->scratch.obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800664 i915_gem_object_ggtt_unpin(ring->scratch.obj);
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100665 }
Daniel Vetterb45305f2012-12-17 16:21:27 +0100666
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100667 drm_gem_object_unreference(&ring->scratch.obj->base);
668 ring->scratch.obj = NULL;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000669}
670
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100671static int gen6_signal(struct intel_engine_cs *signaller,
Ben Widawsky024a43e2014-04-29 14:52:30 -0700672 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000673{
Ben Widawsky024a43e2014-04-29 14:52:30 -0700674 struct drm_device *dev = signaller->dev;
675 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100676 struct intel_engine_cs *useless;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700677 int i, ret;
Ben Widawsky78325f22014-04-29 14:52:29 -0700678
Ben Widawsky024a43e2014-04-29 14:52:30 -0700679 /* NB: In order to be able to do semaphore MBOX updates for varying
680 * number of rings, it's easiest if we round up each individual update
681 * to a multiple of 2 (since ring updates must always be a multiple of
682 * 2) even though the actual update only requires 3 dwords.
683 */
Ben Widawskyad776f82013-05-28 19:22:18 -0700684#define MBOX_UPDATE_DWORDS 4
Ben Widawsky024a43e2014-04-29 14:52:30 -0700685 if (i915_semaphore_is_enabled(dev))
686 num_dwords += ((I915_NUM_RINGS-1) * MBOX_UPDATE_DWORDS);
Mika Kuoppala6e450ab2014-05-15 20:58:07 +0300687 else
688 return intel_ring_begin(signaller, num_dwords);
Ben Widawsky024a43e2014-04-29 14:52:30 -0700689
690 ret = intel_ring_begin(signaller, num_dwords);
691 if (ret)
692 return ret;
693#undef MBOX_UPDATE_DWORDS
694
Ben Widawsky78325f22014-04-29 14:52:29 -0700695 for_each_ring(useless, dev_priv, i) {
696 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
697 if (mbox_reg != GEN6_NOSYNC) {
698 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
699 intel_ring_emit(signaller, mbox_reg);
700 intel_ring_emit(signaller, signaller->outstanding_lazy_seqno);
701 intel_ring_emit(signaller, MI_NOOP);
702 } else {
703 intel_ring_emit(signaller, MI_NOOP);
704 intel_ring_emit(signaller, MI_NOOP);
705 intel_ring_emit(signaller, MI_NOOP);
706 intel_ring_emit(signaller, MI_NOOP);
707 }
708 }
Ben Widawsky024a43e2014-04-29 14:52:30 -0700709
710 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000711}
712
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700713/**
714 * gen6_add_request - Update the semaphore mailbox registers
715 *
716 * @ring - ring that is adding a request
717 * @seqno - return seqno stuck into the ring
718 *
719 * Update the mailbox registers in the *other* rings with the current seqno.
720 * This acts like a signal in the canonical semaphore.
721 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000722static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100723gen6_add_request(struct intel_engine_cs *ring)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000724{
Ben Widawsky024a43e2014-04-29 14:52:30 -0700725 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000726
Ben Widawsky024a43e2014-04-29 14:52:30 -0700727 ret = ring->semaphore.signal(ring, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000728 if (ret)
729 return ret;
730
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000731 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
732 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson18235212013-09-04 10:45:51 +0100733 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000734 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +0100735 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000736
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000737 return 0;
738}
739
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200740static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
741 u32 seqno)
742{
743 struct drm_i915_private *dev_priv = dev->dev_private;
744 return dev_priv->last_seqno < seqno;
745}
746
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700747/**
748 * intel_ring_sync - sync the waiter to the signaller on seqno
749 *
750 * @waiter - ring that is waiting
751 * @signaller - ring which has, or will signal
752 * @seqno - seqno which the waiter will block on
753 */
754static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100755gen6_ring_sync(struct intel_engine_cs *waiter,
756 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200757 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000758{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700759 u32 dw1 = MI_SEMAPHORE_MBOX |
760 MI_SEMAPHORE_COMPARE |
761 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -0700762 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
763 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000764
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700765 /* Throughout all of the GEM code, seqno passed implies our current
766 * seqno is >= the last seqno executed. However for hardware the
767 * comparison is strictly greater than.
768 */
769 seqno -= 1;
770
Ben Widawskyebc348b2014-04-29 14:52:28 -0700771 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200772
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700773 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000774 if (ret)
775 return ret;
776
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200777 /* If seqno wrap happened, omit the wait with no-ops */
778 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -0700779 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200780 intel_ring_emit(waiter, seqno);
781 intel_ring_emit(waiter, 0);
782 intel_ring_emit(waiter, MI_NOOP);
783 } else {
784 intel_ring_emit(waiter, MI_NOOP);
785 intel_ring_emit(waiter, MI_NOOP);
786 intel_ring_emit(waiter, MI_NOOP);
787 intel_ring_emit(waiter, MI_NOOP);
788 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700789 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000790
791 return 0;
792}
793
Chris Wilsonc6df5412010-12-15 09:56:50 +0000794#define PIPE_CONTROL_FLUSH(ring__, addr__) \
795do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200796 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
797 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000798 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
799 intel_ring_emit(ring__, 0); \
800 intel_ring_emit(ring__, 0); \
801} while (0)
802
803static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100804pc_render_add_request(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000805{
Chris Wilson18393f62014-04-09 09:19:40 +0100806 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000807 int ret;
808
809 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
810 * incoherent with writes to memory, i.e. completely fubar,
811 * so we need to use PIPE_NOTIFY instead.
812 *
813 * However, we also need to workaround the qword write
814 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
815 * memory before requesting an interrupt.
816 */
817 ret = intel_ring_begin(ring, 32);
818 if (ret)
819 return ret;
820
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200821 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200822 PIPE_CONTROL_WRITE_FLUSH |
823 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100824 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson18235212013-09-04 10:45:51 +0100825 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000826 intel_ring_emit(ring, 0);
827 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100828 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +0000829 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100830 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000831 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100832 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000833 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100834 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000835 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100836 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000837 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +0000838
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200839 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200840 PIPE_CONTROL_WRITE_FLUSH |
841 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000842 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100843 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson18235212013-09-04 10:45:51 +0100844 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000845 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +0100846 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000847
Chris Wilsonc6df5412010-12-15 09:56:50 +0000848 return 0;
849}
850
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800851static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100852gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100853{
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100854 /* Workaround to force correct ordering between irq and seqno writes on
855 * ivb (and maybe also on snb) by reading from a CS register (like
856 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +0000857 if (!lazy_coherency) {
858 struct drm_i915_private *dev_priv = ring->dev->dev_private;
859 POSTING_READ(RING_ACTHD(ring->mmio_base));
860 }
861
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100862 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
863}
864
865static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100866ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800867{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000868 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
869}
870
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +0200871static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100872ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +0200873{
874 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
875}
876
Chris Wilsonc6df5412010-12-15 09:56:50 +0000877static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100878pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000879{
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100880 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +0000881}
882
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +0200883static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100884pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +0200885{
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100886 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +0200887}
888
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000889static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100890gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +0200891{
892 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300893 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100894 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +0200895
896 if (!dev->irq_enabled)
897 return false;
898
Chris Wilson7338aef2012-04-24 21:48:47 +0100899 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300900 if (ring->irq_refcount++ == 0)
901 ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +0100902 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +0200903
904 return true;
905}
906
907static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100908gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +0200909{
910 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300911 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100912 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +0200913
Chris Wilson7338aef2012-04-24 21:48:47 +0100914 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300915 if (--ring->irq_refcount == 0)
916 ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +0100917 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +0200918}
919
920static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100921i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700922{
Chris Wilson78501ea2010-10-27 12:18:21 +0100923 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300924 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100925 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700926
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000927 if (!dev->irq_enabled)
928 return false;
929
Chris Wilson7338aef2012-04-24 21:48:47 +0100930 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +0200931 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +0200932 dev_priv->irq_mask &= ~ring->irq_enable_mask;
933 I915_WRITE(IMR, dev_priv->irq_mask);
934 POSTING_READ(IMR);
935 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100936 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000937
938 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700939}
940
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800941static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100942i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700943{
Chris Wilson78501ea2010-10-27 12:18:21 +0100944 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300945 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100946 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700947
Chris Wilson7338aef2012-04-24 21:48:47 +0100948 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +0200949 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +0200950 dev_priv->irq_mask |= ring->irq_enable_mask;
951 I915_WRITE(IMR, dev_priv->irq_mask);
952 POSTING_READ(IMR);
953 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100954 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700955}
956
Chris Wilsonc2798b12012-04-22 21:13:57 +0100957static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100958i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +0100959{
960 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300961 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100962 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100963
964 if (!dev->irq_enabled)
965 return false;
966
Chris Wilson7338aef2012-04-24 21:48:47 +0100967 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +0200968 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +0100969 dev_priv->irq_mask &= ~ring->irq_enable_mask;
970 I915_WRITE16(IMR, dev_priv->irq_mask);
971 POSTING_READ16(IMR);
972 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100973 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100974
975 return true;
976}
977
978static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100979i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +0100980{
981 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300982 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100983 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100984
Chris Wilson7338aef2012-04-24 21:48:47 +0100985 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +0200986 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +0100987 dev_priv->irq_mask |= ring->irq_enable_mask;
988 I915_WRITE16(IMR, dev_priv->irq_mask);
989 POSTING_READ16(IMR);
990 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100991 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100992}
993
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100994void intel_ring_setup_status_page(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800995{
Eric Anholt45930102011-05-06 17:12:35 -0700996 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300997 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700998 u32 mmio = 0;
999
1000 /* The ring status page addresses are no longer next to the rest of
1001 * the ring registers as of gen7.
1002 */
1003 if (IS_GEN7(dev)) {
1004 switch (ring->id) {
Daniel Vetter96154f22011-12-14 13:57:00 +01001005 case RCS:
Eric Anholt45930102011-05-06 17:12:35 -07001006 mmio = RENDER_HWS_PGA_GEN7;
1007 break;
Daniel Vetter96154f22011-12-14 13:57:00 +01001008 case BCS:
Eric Anholt45930102011-05-06 17:12:35 -07001009 mmio = BLT_HWS_PGA_GEN7;
1010 break;
Zhao Yakui77fe2ff2014-04-17 10:37:39 +08001011 /*
1012 * VCS2 actually doesn't exist on Gen7. Only shut up
1013 * gcc switch check warning
1014 */
1015 case VCS2:
Daniel Vetter96154f22011-12-14 13:57:00 +01001016 case VCS:
Eric Anholt45930102011-05-06 17:12:35 -07001017 mmio = BSD_HWS_PGA_GEN7;
1018 break;
Ben Widawsky4a3dd192013-05-28 19:22:19 -07001019 case VECS:
Ben Widawsky9a8a2212013-05-28 19:22:23 -07001020 mmio = VEBOX_HWS_PGA_GEN7;
1021 break;
Eric Anholt45930102011-05-06 17:12:35 -07001022 }
1023 } else if (IS_GEN6(ring->dev)) {
1024 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
1025 } else {
Ben Widawskyeb0d4b72013-11-07 21:40:50 -08001026 /* XXX: gen8 returns to sanity */
Eric Anholt45930102011-05-06 17:12:35 -07001027 mmio = RING_HWS_PGA(ring->mmio_base);
1028 }
1029
Chris Wilson78501ea2010-10-27 12:18:21 +01001030 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
1031 POSTING_READ(mmio);
Chris Wilson884020b2013-08-06 19:01:14 +01001032
Damien Lespiaudc616b82014-03-13 01:40:28 +00001033 /*
1034 * Flush the TLB for this page
1035 *
1036 * FIXME: These two bits have disappeared on gen8, so a question
1037 * arises: do we still need this and if so how should we go about
1038 * invalidating the TLB?
1039 */
1040 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
Chris Wilson884020b2013-08-06 19:01:14 +01001041 u32 reg = RING_INSTPM(ring->mmio_base);
Naresh Kumar Kachhi02f6a1e2014-03-12 16:39:42 +05301042
1043 /* ring should be idle before issuing a sync flush*/
1044 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
1045
Chris Wilson884020b2013-08-06 19:01:14 +01001046 I915_WRITE(reg,
1047 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
1048 INSTPM_SYNC_FLUSH));
1049 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
1050 1000))
1051 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
1052 ring->name);
1053 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001054}
1055
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001056static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001057bsd_ring_flush(struct intel_engine_cs *ring,
Chris Wilson78501ea2010-10-27 12:18:21 +01001058 u32 invalidate_domains,
1059 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001060{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001061 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001062
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001063 ret = intel_ring_begin(ring, 2);
1064 if (ret)
1065 return ret;
1066
1067 intel_ring_emit(ring, MI_FLUSH);
1068 intel_ring_emit(ring, MI_NOOP);
1069 intel_ring_advance(ring);
1070 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001071}
1072
Chris Wilson3cce4692010-10-27 16:11:02 +01001073static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001074i9xx_add_request(struct intel_engine_cs *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001075{
Chris Wilson3cce4692010-10-27 16:11:02 +01001076 int ret;
1077
1078 ret = intel_ring_begin(ring, 4);
1079 if (ret)
1080 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001081
Chris Wilson3cce4692010-10-27 16:11:02 +01001082 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1083 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson18235212013-09-04 10:45:51 +01001084 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +01001085 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001086 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001087
Chris Wilson3cce4692010-10-27 16:11:02 +01001088 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001089}
1090
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001091static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001092gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001093{
1094 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001095 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001096 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001097
1098 if (!dev->irq_enabled)
1099 return false;
1100
Chris Wilson7338aef2012-04-24 21:48:47 +01001101 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001102 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001103 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001104 I915_WRITE_IMR(ring,
1105 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001106 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001107 else
1108 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001109 ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001110 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001111 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001112
1113 return true;
1114}
1115
1116static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001117gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001118{
1119 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001120 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001121 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001122
Chris Wilson7338aef2012-04-24 21:48:47 +01001123 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001124 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001125 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001126 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001127 else
1128 I915_WRITE_IMR(ring, ~0);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001129 ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001130 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001131 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001132}
1133
Ben Widawskya19d2932013-05-28 19:22:30 -07001134static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001135hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001136{
1137 struct drm_device *dev = ring->dev;
1138 struct drm_i915_private *dev_priv = dev->dev_private;
1139 unsigned long flags;
1140
1141 if (!dev->irq_enabled)
1142 return false;
1143
Daniel Vetter59cdb632013-07-04 23:35:28 +02001144 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001145 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001146 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03001147 snb_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001148 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001149 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001150
1151 return true;
1152}
1153
1154static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001155hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001156{
1157 struct drm_device *dev = ring->dev;
1158 struct drm_i915_private *dev_priv = dev->dev_private;
1159 unsigned long flags;
1160
1161 if (!dev->irq_enabled)
1162 return;
1163
Daniel Vetter59cdb632013-07-04 23:35:28 +02001164 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001165 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001166 I915_WRITE_IMR(ring, ~0);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03001167 snb_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001168 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001169 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001170}
1171
Ben Widawskyabd58f02013-11-02 21:07:09 -07001172static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001173gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001174{
1175 struct drm_device *dev = ring->dev;
1176 struct drm_i915_private *dev_priv = dev->dev_private;
1177 unsigned long flags;
1178
1179 if (!dev->irq_enabled)
1180 return false;
1181
1182 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1183 if (ring->irq_refcount++ == 0) {
1184 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1185 I915_WRITE_IMR(ring,
1186 ~(ring->irq_enable_mask |
1187 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1188 } else {
1189 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1190 }
1191 POSTING_READ(RING_IMR(ring->mmio_base));
1192 }
1193 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1194
1195 return true;
1196}
1197
1198static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001199gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001200{
1201 struct drm_device *dev = ring->dev;
1202 struct drm_i915_private *dev_priv = dev->dev_private;
1203 unsigned long flags;
1204
1205 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1206 if (--ring->irq_refcount == 0) {
1207 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1208 I915_WRITE_IMR(ring,
1209 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1210 } else {
1211 I915_WRITE_IMR(ring, ~0);
1212 }
1213 POSTING_READ(RING_IMR(ring->mmio_base));
1214 }
1215 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1216}
1217
Zou Nan haid1b851f2010-05-21 09:08:57 +08001218static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001219i965_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001220 u64 offset, u32 length,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001221 unsigned flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001222{
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001223 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001224
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001225 ret = intel_ring_begin(ring, 2);
1226 if (ret)
1227 return ret;
1228
Chris Wilson78501ea2010-10-27 12:18:21 +01001229 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001230 MI_BATCH_BUFFER_START |
1231 MI_BATCH_GTT |
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001232 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001233 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001234 intel_ring_advance(ring);
1235
Zou Nan haid1b851f2010-05-21 09:08:57 +08001236 return 0;
1237}
1238
Daniel Vetterb45305f2012-12-17 16:21:27 +01001239/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1240#define I830_BATCH_LIMIT (256*1024)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001241static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001242i830_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001243 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001244 unsigned flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001245{
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001246 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001247
Daniel Vetterb45305f2012-12-17 16:21:27 +01001248 if (flags & I915_DISPATCH_PINNED) {
1249 ret = intel_ring_begin(ring, 4);
1250 if (ret)
1251 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001252
Daniel Vetterb45305f2012-12-17 16:21:27 +01001253 intel_ring_emit(ring, MI_BATCH_BUFFER);
1254 intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1255 intel_ring_emit(ring, offset + len - 8);
1256 intel_ring_emit(ring, MI_NOOP);
1257 intel_ring_advance(ring);
1258 } else {
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001259 u32 cs_offset = ring->scratch.gtt_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001260
1261 if (len > I830_BATCH_LIMIT)
1262 return -ENOSPC;
1263
1264 ret = intel_ring_begin(ring, 9+3);
1265 if (ret)
1266 return ret;
1267 /* Blit the batch (which has now all relocs applied) to the stable batch
1268 * scratch bo area (so that the CS never stumbles over its tlb
1269 * invalidation bug) ... */
1270 intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
1271 XY_SRC_COPY_BLT_WRITE_ALPHA |
1272 XY_SRC_COPY_BLT_WRITE_RGB);
1273 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
1274 intel_ring_emit(ring, 0);
1275 intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
1276 intel_ring_emit(ring, cs_offset);
1277 intel_ring_emit(ring, 0);
1278 intel_ring_emit(ring, 4096);
1279 intel_ring_emit(ring, offset);
1280 intel_ring_emit(ring, MI_FLUSH);
1281
1282 /* ... and execute it. */
1283 intel_ring_emit(ring, MI_BATCH_BUFFER);
1284 intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1285 intel_ring_emit(ring, cs_offset + len - 8);
1286 intel_ring_advance(ring);
1287 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001288
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001289 return 0;
1290}
1291
1292static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001293i915_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001294 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001295 unsigned flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001296{
1297 int ret;
1298
1299 ret = intel_ring_begin(ring, 2);
1300 if (ret)
1301 return ret;
1302
Chris Wilson65f56872012-04-17 16:38:12 +01001303 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001304 intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001305 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001306
Eric Anholt62fdfea2010-05-21 13:26:39 -07001307 return 0;
1308}
1309
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001310static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001311{
Chris Wilson05394f32010-11-08 19:18:58 +00001312 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001313
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001314 obj = ring->status_page.obj;
1315 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001316 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001317
Chris Wilson9da3da62012-06-01 15:20:22 +01001318 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001319 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001320 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001321 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001322}
1323
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001324static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001325{
Chris Wilson05394f32010-11-08 19:18:58 +00001326 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001327
Chris Wilsone3efda42014-04-09 09:19:41 +01001328 if ((obj = ring->status_page.obj) == NULL) {
1329 int ret;
1330
1331 obj = i915_gem_alloc_object(ring->dev, 4096);
1332 if (obj == NULL) {
1333 DRM_ERROR("Failed to allocate status page\n");
1334 return -ENOMEM;
1335 }
1336
1337 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1338 if (ret)
1339 goto err_unref;
1340
1341 ret = i915_gem_obj_ggtt_pin(obj, 4096, 0);
1342 if (ret) {
1343err_unref:
1344 drm_gem_object_unreference(&obj->base);
1345 return ret;
1346 }
1347
1348 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001349 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001350
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001351 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001352 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001353 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001354
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001355 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1356 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001357
1358 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001359}
1360
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001361static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001362{
1363 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001364
1365 if (!dev_priv->status_page_dmah) {
1366 dev_priv->status_page_dmah =
1367 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1368 if (!dev_priv->status_page_dmah)
1369 return -ENOMEM;
1370 }
1371
Chris Wilson6b8294a2012-11-16 11:43:20 +00001372 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1373 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1374
1375 return 0;
1376}
1377
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001378static int allocate_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01001379{
1380 struct drm_device *dev = ring->dev;
1381 struct drm_i915_private *dev_priv = to_i915(dev);
1382 struct drm_i915_gem_object *obj;
1383 int ret;
1384
1385 if (ring->obj)
1386 return 0;
1387
1388 obj = NULL;
1389 if (!HAS_LLC(dev))
1390 obj = i915_gem_object_create_stolen(dev, ring->size);
1391 if (obj == NULL)
1392 obj = i915_gem_alloc_object(dev, ring->size);
1393 if (obj == NULL)
1394 return -ENOMEM;
1395
1396 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1397 if (ret)
1398 goto err_unref;
1399
1400 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1401 if (ret)
1402 goto err_unpin;
1403
1404 ring->virtual_start =
1405 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
1406 ring->size);
1407 if (ring->virtual_start == NULL) {
1408 ret = -EINVAL;
1409 goto err_unpin;
1410 }
1411
1412 ring->obj = obj;
1413 return 0;
1414
1415err_unpin:
1416 i915_gem_object_ggtt_unpin(obj);
1417err_unref:
1418 drm_gem_object_unreference(&obj->base);
1419 return ret;
1420}
1421
Ben Widawskyc43b5632012-04-16 14:07:40 -07001422static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001423 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001424{
Chris Wilsondd785e32010-08-07 11:01:34 +01001425 int ret;
1426
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001427 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01001428 INIT_LIST_HEAD(&ring->active_list);
1429 INIT_LIST_HEAD(&ring->request_list);
Daniel Vetterdfc9ef22012-04-11 22:12:47 +02001430 ring->size = 32 * PAGE_SIZE;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001431 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001432
Chris Wilsonb259f672011-03-29 13:19:09 +01001433 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001434
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001435 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001436 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001437 if (ret)
1438 return ret;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001439 } else {
1440 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02001441 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001442 if (ret)
1443 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001444 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001445
Chris Wilsone3efda42014-04-09 09:19:41 +01001446 ret = allocate_ring_buffer(ring);
1447 if (ret) {
1448 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n", ring->name, ret);
1449 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001450 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001451
Chris Wilson55249ba2010-12-22 14:04:47 +00001452 /* Workaround an erratum on the i830 which causes a hang if
1453 * the TAIL pointer points to within the last 2 cachelines
1454 * of the buffer.
1455 */
1456 ring->effective_size = ring->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01001457 if (IS_I830(dev) || IS_845G(dev))
Chris Wilson18393f62014-04-09 09:19:40 +01001458 ring->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00001459
Brad Volkin44e895a2014-05-10 14:10:43 -07001460 ret = i915_cmd_parser_init_ring(ring);
1461 if (ret)
1462 return ret;
Brad Volkin351e3db2014-02-18 10:15:46 -08001463
Chris Wilsone3efda42014-04-09 09:19:41 +01001464 return ring->init(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001465}
1466
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001467void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001468{
Chris Wilsone3efda42014-04-09 09:19:41 +01001469 struct drm_i915_private *dev_priv = to_i915(ring->dev);
Chris Wilson33626e62010-10-29 16:18:36 +01001470
Chris Wilson05394f32010-11-08 19:18:58 +00001471 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001472 return;
1473
Chris Wilsone3efda42014-04-09 09:19:41 +01001474 intel_stop_ring_buffer(ring);
1475 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01001476
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001477 iounmap(ring->virtual_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001478
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001479 i915_gem_object_ggtt_unpin(ring->obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001480 drm_gem_object_unreference(&ring->obj->base);
1481 ring->obj = NULL;
Ben Widawsky3d57e5b2013-10-14 10:01:36 -07001482 ring->preallocated_lazy_request = NULL;
1483 ring->outstanding_lazy_seqno = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01001484
Zou Nan hai8d192152010-11-02 16:31:01 +08001485 if (ring->cleanup)
1486 ring->cleanup(ring);
1487
Chris Wilson78501ea2010-10-27 12:18:21 +01001488 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07001489
1490 i915_cmd_parser_fini_ring(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001491}
1492
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001493static int intel_ring_wait_request(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00001494{
1495 struct drm_i915_gem_request *request;
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001496 u32 seqno = 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001497 int ret;
1498
Chris Wilsona71d8d92012-02-15 11:25:36 +00001499 if (ring->last_retired_head != -1) {
1500 ring->head = ring->last_retired_head;
1501 ring->last_retired_head = -1;
Chris Wilson1f709992014-01-27 22:43:07 +00001502
Chris Wilsona71d8d92012-02-15 11:25:36 +00001503 ring->space = ring_space(ring);
1504 if (ring->space >= n)
1505 return 0;
1506 }
1507
1508 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001509 if (__ring_space(request->tail, ring->tail, ring->size) >= n) {
Chris Wilsona71d8d92012-02-15 11:25:36 +00001510 seqno = request->seqno;
1511 break;
1512 }
Chris Wilsona71d8d92012-02-15 11:25:36 +00001513 }
1514
1515 if (seqno == 0)
1516 return -ENOSPC;
1517
Chris Wilson1f709992014-01-27 22:43:07 +00001518 ret = i915_wait_seqno(ring, seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001519 if (ret)
1520 return ret;
1521
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001522 i915_gem_retire_requests_ring(ring);
1523 ring->head = ring->last_retired_head;
1524 ring->last_retired_head = -1;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001525
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001526 ring->space = ring_space(ring);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001527 return 0;
1528}
1529
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001530static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001531{
Chris Wilson78501ea2010-10-27 12:18:21 +01001532 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001533 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001534 unsigned long end;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001535 int ret;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001536
Chris Wilsona71d8d92012-02-15 11:25:36 +00001537 ret = intel_ring_wait_request(ring, n);
1538 if (ret != -ENOSPC)
1539 return ret;
1540
Chris Wilson09246732013-08-10 22:16:32 +01001541 /* force the tail write in case we have been skipping them */
1542 __intel_ring_advance(ring);
1543
Daniel Vetter63ed2cb2012-04-23 16:50:50 +02001544 /* With GEM the hangcheck timer should kick us out of the loop,
1545 * leaving it early runs the risk of corrupting GEM state (due
1546 * to running on almost untested codepaths). But on resume
1547 * timers don't work yet, so prevent a complete hang in that
1548 * case by choosing an insanely large timeout. */
1549 end = jiffies + 60 * HZ;
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001550
Chris Wilsondcfe0502014-05-05 09:07:32 +01001551 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001552 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001553 ring->head = I915_READ_HEAD(ring);
1554 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001555 if (ring->space >= n) {
Chris Wilsondcfe0502014-05-05 09:07:32 +01001556 ret = 0;
1557 break;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001558 }
1559
Daniel Vetterfb19e2a2014-02-12 23:44:34 +01001560 if (!drm_core_check_feature(dev, DRIVER_MODESET) &&
1561 dev->primary->master) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001562 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1563 if (master_priv->sarea_priv)
1564 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1565 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001566
Chris Wilsone60a0b12010-10-13 10:09:14 +01001567 msleep(1);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001568
Chris Wilsondcfe0502014-05-05 09:07:32 +01001569 if (dev_priv->mm.interruptible && signal_pending(current)) {
1570 ret = -ERESTARTSYS;
1571 break;
1572 }
1573
Daniel Vetter33196de2012-11-14 17:14:05 +01001574 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1575 dev_priv->mm.interruptible);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001576 if (ret)
Chris Wilsondcfe0502014-05-05 09:07:32 +01001577 break;
1578
1579 if (time_after(jiffies, end)) {
1580 ret = -EBUSY;
1581 break;
1582 }
1583 } while (1);
Chris Wilsondb53a302011-02-03 11:57:46 +00001584 trace_i915_ring_wait_end(ring);
Chris Wilsondcfe0502014-05-05 09:07:32 +01001585 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001586}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001587
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001588static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00001589{
1590 uint32_t __iomem *virt;
1591 int rem = ring->size - ring->tail;
1592
1593 if (ring->space < rem) {
1594 int ret = ring_wait_for_space(ring, rem);
1595 if (ret)
1596 return ret;
1597 }
1598
1599 virt = ring->virtual_start + ring->tail;
1600 rem /= 4;
1601 while (rem--)
1602 iowrite32(MI_NOOP, virt++);
1603
1604 ring->tail = 0;
1605 ring->space = ring_space(ring);
1606
1607 return 0;
1608}
1609
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001610int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00001611{
1612 u32 seqno;
1613 int ret;
1614
1615 /* We need to add any requests required to flush the objects and ring */
Chris Wilson18235212013-09-04 10:45:51 +01001616 if (ring->outstanding_lazy_seqno) {
Mika Kuoppala0025c072013-06-12 12:35:30 +03001617 ret = i915_add_request(ring, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00001618 if (ret)
1619 return ret;
1620 }
1621
1622 /* Wait upon the last request to be completed */
1623 if (list_empty(&ring->request_list))
1624 return 0;
1625
1626 seqno = list_entry(ring->request_list.prev,
1627 struct drm_i915_gem_request,
1628 list)->seqno;
1629
1630 return i915_wait_seqno(ring, seqno);
1631}
1632
Chris Wilson9d7730912012-11-27 16:22:52 +00001633static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001634intel_ring_alloc_seqno(struct intel_engine_cs *ring)
Chris Wilson9d7730912012-11-27 16:22:52 +00001635{
Chris Wilson18235212013-09-04 10:45:51 +01001636 if (ring->outstanding_lazy_seqno)
Chris Wilson9d7730912012-11-27 16:22:52 +00001637 return 0;
1638
Chris Wilson3c0e2342013-09-04 10:45:52 +01001639 if (ring->preallocated_lazy_request == NULL) {
1640 struct drm_i915_gem_request *request;
1641
1642 request = kmalloc(sizeof(*request), GFP_KERNEL);
1643 if (request == NULL)
1644 return -ENOMEM;
1645
1646 ring->preallocated_lazy_request = request;
1647 }
1648
Chris Wilson18235212013-09-04 10:45:51 +01001649 return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
Chris Wilson9d7730912012-11-27 16:22:52 +00001650}
1651
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001652static int __intel_ring_prepare(struct intel_engine_cs *ring,
Chris Wilson304d6952014-01-02 14:32:35 +00001653 int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001654{
1655 int ret;
1656
1657 if (unlikely(ring->tail + bytes > ring->effective_size)) {
1658 ret = intel_wrap_ring_buffer(ring);
1659 if (unlikely(ret))
1660 return ret;
1661 }
1662
1663 if (unlikely(ring->space < bytes)) {
1664 ret = ring_wait_for_space(ring, bytes);
1665 if (unlikely(ret))
1666 return ret;
1667 }
1668
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001669 return 0;
1670}
1671
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001672int intel_ring_begin(struct intel_engine_cs *ring,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001673 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001674{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001675 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001676 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001677
Daniel Vetter33196de2012-11-14 17:14:05 +01001678 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1679 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02001680 if (ret)
1681 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00001682
Chris Wilson304d6952014-01-02 14:32:35 +00001683 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
1684 if (ret)
1685 return ret;
1686
Chris Wilson9d7730912012-11-27 16:22:52 +00001687 /* Preallocate the olr before touching the ring */
1688 ret = intel_ring_alloc_seqno(ring);
1689 if (ret)
1690 return ret;
1691
Chris Wilson304d6952014-01-02 14:32:35 +00001692 ring->space -= num_dwords * sizeof(uint32_t);
1693 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001694}
1695
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001696/* Align the ring tail to a cacheline boundary */
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001697int intel_ring_cacheline_align(struct intel_engine_cs *ring)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001698{
Chris Wilson18393f62014-04-09 09:19:40 +01001699 int num_dwords = (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001700 int ret;
1701
1702 if (num_dwords == 0)
1703 return 0;
1704
Chris Wilson18393f62014-04-09 09:19:40 +01001705 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001706 ret = intel_ring_begin(ring, num_dwords);
1707 if (ret)
1708 return ret;
1709
1710 while (num_dwords--)
1711 intel_ring_emit(ring, MI_NOOP);
1712
1713 intel_ring_advance(ring);
1714
1715 return 0;
1716}
1717
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001718void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001719{
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001720 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001721
Chris Wilson18235212013-09-04 10:45:51 +01001722 BUG_ON(ring->outstanding_lazy_seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001723
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001724 if (INTEL_INFO(ring->dev)->gen >= 6) {
1725 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
1726 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Ben Widawsky50201502013-08-12 16:53:03 -07001727 if (HAS_VEBOX(ring->dev))
1728 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01001729 }
Chris Wilson297b0c52010-10-22 17:02:41 +01001730
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001731 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03001732 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01001733}
1734
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001735static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001736 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001737{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001738 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001739
1740 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001741
Chris Wilson12f55812012-07-05 17:14:01 +01001742 /* Disable notification that the ring is IDLE. The GT
1743 * will then assume that it is busy and bring it out of rc6.
1744 */
1745 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1746 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1747
1748 /* Clear the context id. Here be magic! */
1749 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1750
1751 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04001752 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01001753 GEN6_BSD_SLEEP_INDICATOR) == 0,
1754 50))
1755 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001756
Chris Wilson12f55812012-07-05 17:14:01 +01001757 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04001758 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01001759 POSTING_READ(RING_TAIL(ring->mmio_base));
1760
1761 /* Let the ring send IDLE messages to the GT again,
1762 * and so let it sleep to conserve power when idle.
1763 */
Akshay Joshi0206e352011-08-16 15:34:10 -04001764 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01001765 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001766}
1767
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001768static int gen6_bsd_ring_flush(struct intel_engine_cs *ring,
Ben Widawskyea251322013-05-28 19:22:21 -07001769 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001770{
Chris Wilson71a77e02011-02-02 12:13:49 +00001771 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001772 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001773
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001774 ret = intel_ring_begin(ring, 4);
1775 if (ret)
1776 return ret;
1777
Chris Wilson71a77e02011-02-02 12:13:49 +00001778 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001779 if (INTEL_INFO(ring->dev)->gen >= 8)
1780 cmd += 1;
Jesse Barnes9a289772012-10-26 09:42:42 -07001781 /*
1782 * Bspec vol 1c.5 - video engine command streamer:
1783 * "If ENABLED, all TLBs will be invalidated once the flush
1784 * operation is complete. This bit is only valid when the
1785 * Post-Sync Operation field is a value of 1h or 3h."
1786 */
Chris Wilson71a77e02011-02-02 12:13:49 +00001787 if (invalidate & I915_GEM_GPU_DOMAINS)
Jesse Barnes9a289772012-10-26 09:42:42 -07001788 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1789 MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
Chris Wilson71a77e02011-02-02 12:13:49 +00001790 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07001791 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001792 if (INTEL_INFO(ring->dev)->gen >= 8) {
1793 intel_ring_emit(ring, 0); /* upper addr */
1794 intel_ring_emit(ring, 0); /* value */
1795 } else {
1796 intel_ring_emit(ring, 0);
1797 intel_ring_emit(ring, MI_NOOP);
1798 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001799 intel_ring_advance(ring);
1800 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001801}
1802
1803static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001804gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001805 u64 offset, u32 len,
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001806 unsigned flags)
1807{
Ben Widawsky28cf5412013-11-02 21:07:26 -07001808 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1809 bool ppgtt = dev_priv->mm.aliasing_ppgtt != NULL &&
1810 !(flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001811 int ret;
1812
1813 ret = intel_ring_begin(ring, 4);
1814 if (ret)
1815 return ret;
1816
1817 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07001818 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001819 intel_ring_emit(ring, lower_32_bits(offset));
1820 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001821 intel_ring_emit(ring, MI_NOOP);
1822 intel_ring_advance(ring);
1823
1824 return 0;
1825}
1826
1827static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001828hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001829 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001830 unsigned flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001831{
Akshay Joshi0206e352011-08-16 15:34:10 -04001832 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001833
Akshay Joshi0206e352011-08-16 15:34:10 -04001834 ret = intel_ring_begin(ring, 2);
1835 if (ret)
1836 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001837
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001838 intel_ring_emit(ring,
1839 MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
1840 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
1841 /* bit0-7 is the length on GEN6+ */
1842 intel_ring_emit(ring, offset);
1843 intel_ring_advance(ring);
1844
1845 return 0;
1846}
1847
1848static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001849gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001850 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001851 unsigned flags)
1852{
1853 int ret;
1854
1855 ret = intel_ring_begin(ring, 2);
1856 if (ret)
1857 return ret;
1858
1859 intel_ring_emit(ring,
1860 MI_BATCH_BUFFER_START |
1861 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04001862 /* bit0-7 is the length on GEN6+ */
1863 intel_ring_emit(ring, offset);
1864 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001865
Akshay Joshi0206e352011-08-16 15:34:10 -04001866 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001867}
1868
Chris Wilson549f7362010-10-19 11:19:32 +01001869/* Blitter support (SandyBridge+) */
1870
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001871static int gen6_ring_flush(struct intel_engine_cs *ring,
Ben Widawskyea251322013-05-28 19:22:21 -07001872 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001873{
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001874 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00001875 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001876 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001877
Daniel Vetter6a233c72011-12-14 13:57:07 +01001878 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001879 if (ret)
1880 return ret;
1881
Chris Wilson71a77e02011-02-02 12:13:49 +00001882 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001883 if (INTEL_INFO(ring->dev)->gen >= 8)
1884 cmd += 1;
Jesse Barnes9a289772012-10-26 09:42:42 -07001885 /*
1886 * Bspec vol 1c.3 - blitter engine command streamer:
1887 * "If ENABLED, all TLBs will be invalidated once the flush
1888 * operation is complete. This bit is only valid when the
1889 * Post-Sync Operation field is a value of 1h or 3h."
1890 */
Chris Wilson71a77e02011-02-02 12:13:49 +00001891 if (invalidate & I915_GEM_DOMAIN_RENDER)
Jesse Barnes9a289772012-10-26 09:42:42 -07001892 cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
Daniel Vetterb3fcabb2012-11-04 12:24:47 +01001893 MI_FLUSH_DW_OP_STOREDW;
Chris Wilson71a77e02011-02-02 12:13:49 +00001894 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07001895 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001896 if (INTEL_INFO(ring->dev)->gen >= 8) {
1897 intel_ring_emit(ring, 0); /* upper addr */
1898 intel_ring_emit(ring, 0); /* value */
1899 } else {
1900 intel_ring_emit(ring, 0);
1901 intel_ring_emit(ring, MI_NOOP);
1902 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001903 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001904
Ville Syrjälä9688eca2013-11-06 23:02:19 +02001905 if (IS_GEN7(dev) && !invalidate && flush)
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001906 return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
1907
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001908 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001909}
1910
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001911int intel_init_render_ring_buffer(struct drm_device *dev)
1912{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001913 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001914 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001915
Daniel Vetter59465b52012-04-11 22:12:48 +02001916 ring->name = "render ring";
1917 ring->id = RCS;
1918 ring->mmio_base = RENDER_RING_BASE;
1919
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001920 if (INTEL_INFO(dev)->gen >= 6) {
1921 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03001922 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01001923 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03001924 ring->flush = gen6_render_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001925 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawskya5f3d682013-11-02 21:07:27 -07001926 ring->flush = gen8_render_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001927 ring->irq_get = gen8_ring_get_irq;
1928 ring->irq_put = gen8_ring_put_irq;
1929 } else {
1930 ring->irq_get = gen6_ring_get_irq;
1931 ring->irq_put = gen6_ring_put_irq;
1932 }
Ben Widawskycc609d52013-05-28 19:22:29 -07001933 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001934 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001935 ring->set_seqno = ring_set_seqno;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001936 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07001937 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08001938 /*
1939 * The current semaphore is only applied on pre-gen8 platform.
1940 * And there is no VCS2 ring on the pre-gen8 platform. So the
1941 * semaphore between RCS and VCS2 is initialized as INVALID.
1942 * Gen8 will initialize the sema between VCS2 and RCS later.
1943 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07001944 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
1945 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
1946 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
1947 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
1948 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
1949 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
1950 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
1951 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
1952 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
1953 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001954 } else if (IS_GEN5(dev)) {
1955 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001956 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001957 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001958 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02001959 ring->irq_get = gen5_ring_get_irq;
1960 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07001961 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
1962 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02001963 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02001964 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001965 if (INTEL_INFO(dev)->gen < 4)
1966 ring->flush = gen2_render_ring_flush;
1967 else
1968 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02001969 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001970 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001971 if (IS_GEN2(dev)) {
1972 ring->irq_get = i8xx_ring_get_irq;
1973 ring->irq_put = i8xx_ring_put_irq;
1974 } else {
1975 ring->irq_get = i9xx_ring_get_irq;
1976 ring->irq_put = i9xx_ring_put_irq;
1977 }
Daniel Vettere3670312012-04-11 22:12:53 +02001978 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001979 }
Daniel Vetter59465b52012-04-11 22:12:48 +02001980 ring->write_tail = ring_write_tail;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001981 if (IS_HASWELL(dev))
1982 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001983 else if (IS_GEN8(dev))
1984 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001985 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001986 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1987 else if (INTEL_INFO(dev)->gen >= 4)
1988 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1989 else if (IS_I830(dev) || IS_845G(dev))
1990 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1991 else
1992 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02001993 ring->init = init_render_ring;
1994 ring->cleanup = render_ring_cleanup;
1995
Daniel Vetterb45305f2012-12-17 16:21:27 +01001996 /* Workaround batchbuffer to combat CS tlb bug. */
1997 if (HAS_BROKEN_CS_TLB(dev)) {
1998 struct drm_i915_gem_object *obj;
1999 int ret;
2000
2001 obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
2002 if (obj == NULL) {
2003 DRM_ERROR("Failed to allocate batch bo\n");
2004 return -ENOMEM;
2005 }
2006
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002007 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002008 if (ret != 0) {
2009 drm_gem_object_unreference(&obj->base);
2010 DRM_ERROR("Failed to ping batch bo\n");
2011 return ret;
2012 }
2013
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002014 ring->scratch.obj = obj;
2015 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002016 }
2017
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002018 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002019}
2020
Chris Wilsone8616b62011-01-20 09:57:11 +00002021int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
2022{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002023 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002024 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson6b8294a2012-11-16 11:43:20 +00002025 int ret;
Chris Wilsone8616b62011-01-20 09:57:11 +00002026
Daniel Vetter59465b52012-04-11 22:12:48 +02002027 ring->name = "render ring";
2028 ring->id = RCS;
2029 ring->mmio_base = RENDER_RING_BASE;
2030
Chris Wilsone8616b62011-01-20 09:57:11 +00002031 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetterb4178f82012-04-11 22:12:51 +02002032 /* non-kms not supported on gen6+ */
2033 return -ENODEV;
Chris Wilsone8616b62011-01-20 09:57:11 +00002034 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002035
2036 /* Note: gem is not supported on gen5/ilk without kms (the corresponding
2037 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
2038 * the special gen5 functions. */
2039 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002040 if (INTEL_INFO(dev)->gen < 4)
2041 ring->flush = gen2_render_ring_flush;
2042 else
2043 ring->flush = gen4_render_ring_flush;
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002044 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002045 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002046 if (IS_GEN2(dev)) {
2047 ring->irq_get = i8xx_ring_get_irq;
2048 ring->irq_put = i8xx_ring_put_irq;
2049 } else {
2050 ring->irq_get = i9xx_ring_get_irq;
2051 ring->irq_put = i9xx_ring_put_irq;
2052 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002053 ring->irq_enable_mask = I915_USER_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002054 ring->write_tail = ring_write_tail;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002055 if (INTEL_INFO(dev)->gen >= 4)
2056 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2057 else if (IS_I830(dev) || IS_845G(dev))
2058 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2059 else
2060 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02002061 ring->init = init_render_ring;
2062 ring->cleanup = render_ring_cleanup;
Chris Wilsone8616b62011-01-20 09:57:11 +00002063
2064 ring->dev = dev;
2065 INIT_LIST_HEAD(&ring->active_list);
2066 INIT_LIST_HEAD(&ring->request_list);
Chris Wilsone8616b62011-01-20 09:57:11 +00002067
2068 ring->size = size;
2069 ring->effective_size = ring->size;
Mika Kuoppala17f10fd2012-10-29 16:59:26 +02002070 if (IS_I830(ring->dev) || IS_845G(ring->dev))
Chris Wilson18393f62014-04-09 09:19:40 +01002071 ring->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilsone8616b62011-01-20 09:57:11 +00002072
Daniel Vetter4225d0f2012-04-26 23:28:16 +02002073 ring->virtual_start = ioremap_wc(start, size);
2074 if (ring->virtual_start == NULL) {
Chris Wilsone8616b62011-01-20 09:57:11 +00002075 DRM_ERROR("can not ioremap virtual address for"
2076 " ring buffer\n");
2077 return -ENOMEM;
2078 }
2079
Chris Wilson6b8294a2012-11-16 11:43:20 +00002080 if (!I915_NEED_GFX_HWS(dev)) {
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002081 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002082 if (ret)
2083 return ret;
2084 }
2085
Chris Wilsone8616b62011-01-20 09:57:11 +00002086 return 0;
2087}
2088
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002089int intel_init_bsd_ring_buffer(struct drm_device *dev)
2090{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002091 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002092 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002093
Daniel Vetter58fa3832012-04-11 22:12:49 +02002094 ring->name = "bsd ring";
2095 ring->id = VCS;
2096
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002097 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002098 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002099 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002100 /* gen6 bsd needs a special wa for tail updates */
2101 if (IS_GEN6(dev))
2102 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002103 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002104 ring->add_request = gen6_add_request;
2105 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002106 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002107 if (INTEL_INFO(dev)->gen >= 8) {
2108 ring->irq_enable_mask =
2109 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2110 ring->irq_get = gen8_ring_get_irq;
2111 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002112 ring->dispatch_execbuffer =
2113 gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002114 } else {
2115 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2116 ring->irq_get = gen6_ring_get_irq;
2117 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002118 ring->dispatch_execbuffer =
2119 gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002120 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002121 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002122 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002123 /*
2124 * The current semaphore is only applied on pre-gen8 platform.
2125 * And there is no VCS2 ring on the pre-gen8 platform. So the
2126 * semaphore between VCS and VCS2 is initialized as INVALID.
2127 * Gen8 will initialize the sema between VCS2 and VCS later.
2128 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002129 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2130 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2131 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2132 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2133 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2134 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2135 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2136 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2137 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2138 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002139 } else {
2140 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002141 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002142 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002143 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002144 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002145 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002146 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002147 ring->irq_get = gen5_ring_get_irq;
2148 ring->irq_put = gen5_ring_put_irq;
2149 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002150 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002151 ring->irq_get = i9xx_ring_get_irq;
2152 ring->irq_put = i9xx_ring_put_irq;
2153 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002154 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002155 }
2156 ring->init = init_ring_common;
2157
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002158 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002159}
Chris Wilson549f7362010-10-19 11:19:32 +01002160
Zhao Yakui845f74a2014-04-17 10:37:37 +08002161/**
2162 * Initialize the second BSD ring for Broadwell GT3.
2163 * It is noted that this only exists on Broadwell GT3.
2164 */
2165int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2166{
2167 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002168 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002169
2170 if ((INTEL_INFO(dev)->gen != 8)) {
2171 DRM_ERROR("No dual-BSD ring on non-BDW machine\n");
2172 return -EINVAL;
2173 }
2174
2175 ring->name = "bds2_ring";
2176 ring->id = VCS2;
2177
2178 ring->write_tail = ring_write_tail;
2179 ring->mmio_base = GEN8_BSD2_RING_BASE;
2180 ring->flush = gen6_bsd_ring_flush;
2181 ring->add_request = gen6_add_request;
2182 ring->get_seqno = gen6_ring_get_seqno;
2183 ring->set_seqno = ring_set_seqno;
2184 ring->irq_enable_mask =
2185 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2186 ring->irq_get = gen8_ring_get_irq;
2187 ring->irq_put = gen8_ring_put_irq;
2188 ring->dispatch_execbuffer =
2189 gen8_ring_dispatch_execbuffer;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002190 ring->semaphore.sync_to = gen6_ring_sync;
Oscar Mateod1533372014-05-09 13:44:59 +01002191 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002192 /*
2193 * The current semaphore is only applied on the pre-gen8. And there
2194 * is no bsd2 ring on the pre-gen8. So now the semaphore_register
2195 * between VCS2 and other ring is initialized as invalid.
2196 * Gen8 will initialize the sema between VCS2 and other ring later.
2197 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002198 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2199 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2200 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2201 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2202 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2203 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2204 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2205 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2206 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2207 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002208
2209 ring->init = init_ring_common;
2210
2211 return intel_init_ring_buffer(dev, ring);
2212}
2213
Chris Wilson549f7362010-10-19 11:19:32 +01002214int intel_init_blt_ring_buffer(struct drm_device *dev)
2215{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002216 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002217 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002218
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002219 ring->name = "blitter ring";
2220 ring->id = BCS;
2221
2222 ring->mmio_base = BLT_RING_BASE;
2223 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002224 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002225 ring->add_request = gen6_add_request;
2226 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002227 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002228 if (INTEL_INFO(dev)->gen >= 8) {
2229 ring->irq_enable_mask =
2230 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2231 ring->irq_get = gen8_ring_get_irq;
2232 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002233 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002234 } else {
2235 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2236 ring->irq_get = gen6_ring_get_irq;
2237 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002238 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002239 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002240 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002241 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002242 /*
2243 * The current semaphore is only applied on pre-gen8 platform. And
2244 * there is no VCS2 ring on the pre-gen8 platform. So the semaphore
2245 * between BCS and VCS2 is initialized as INVALID.
2246 * Gen8 will initialize the sema between BCS and VCS2 later.
2247 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002248 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2249 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2250 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2251 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2252 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2253 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2254 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2255 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2256 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2257 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002258 ring->init = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002259
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002260 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002261}
Chris Wilsona7b97612012-07-20 12:41:08 +01002262
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002263int intel_init_vebox_ring_buffer(struct drm_device *dev)
2264{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002265 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002266 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002267
2268 ring->name = "video enhancement ring";
2269 ring->id = VECS;
2270
2271 ring->mmio_base = VEBOX_RING_BASE;
2272 ring->write_tail = ring_write_tail;
2273 ring->flush = gen6_ring_flush;
2274 ring->add_request = gen6_add_request;
2275 ring->get_seqno = gen6_ring_get_seqno;
2276 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002277
2278 if (INTEL_INFO(dev)->gen >= 8) {
2279 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002280 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002281 ring->irq_get = gen8_ring_get_irq;
2282 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002283 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002284 } else {
2285 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2286 ring->irq_get = hsw_vebox_get_irq;
2287 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002288 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002289 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002290 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002291 ring->semaphore.signal = gen6_signal;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002292 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2293 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2294 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2295 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2296 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2297 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2298 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2299 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2300 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2301 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002302 ring->init = init_ring_common;
2303
2304 return intel_init_ring_buffer(dev, ring);
2305}
2306
Chris Wilsona7b97612012-07-20 12:41:08 +01002307int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002308intel_ring_flush_all_caches(struct intel_engine_cs *ring)
Chris Wilsona7b97612012-07-20 12:41:08 +01002309{
2310 int ret;
2311
2312 if (!ring->gpu_caches_dirty)
2313 return 0;
2314
2315 ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2316 if (ret)
2317 return ret;
2318
2319 trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2320
2321 ring->gpu_caches_dirty = false;
2322 return 0;
2323}
2324
2325int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002326intel_ring_invalidate_all_caches(struct intel_engine_cs *ring)
Chris Wilsona7b97612012-07-20 12:41:08 +01002327{
2328 uint32_t flush_domains;
2329 int ret;
2330
2331 flush_domains = 0;
2332 if (ring->gpu_caches_dirty)
2333 flush_domains = I915_GEM_GPU_DOMAINS;
2334
2335 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2336 if (ret)
2337 return ret;
2338
2339 trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2340
2341 ring->gpu_caches_dirty = false;
2342 return 0;
2343}
Chris Wilsone3efda42014-04-09 09:19:41 +01002344
2345void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002346intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002347{
2348 int ret;
2349
2350 if (!intel_ring_initialized(ring))
2351 return;
2352
2353 ret = intel_ring_idle(ring);
2354 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2355 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2356 ring->name, ret);
2357
2358 stop_ring(ring);
2359}