blob: 9b406e424a6f97c5d06e121b204bd82d4d7e728c [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{
Oscar Mateo8ee14972014-05-22 14:13:34 +01001425 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsondd785e32010-08-07 11:01:34 +01001426 int ret;
1427
Oscar Mateo8ee14972014-05-22 14:13:34 +01001428 if (ringbuf == NULL) {
1429 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1430 if (!ringbuf)
1431 return -ENOMEM;
1432 ring->buffer = ringbuf;
1433 }
1434
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001435 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01001436 INIT_LIST_HEAD(&ring->active_list);
1437 INIT_LIST_HEAD(&ring->request_list);
Daniel Vetterdfc9ef22012-04-11 22:12:47 +02001438 ring->size = 32 * PAGE_SIZE;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001439 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001440
Chris Wilsonb259f672011-03-29 13:19:09 +01001441 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001442
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001443 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001444 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001445 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001446 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001447 } else {
1448 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02001449 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001450 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001451 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001452 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001453
Chris Wilsone3efda42014-04-09 09:19:41 +01001454 ret = allocate_ring_buffer(ring);
1455 if (ret) {
1456 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n", ring->name, ret);
Oscar Mateo8ee14972014-05-22 14:13:34 +01001457 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001458 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001459
Chris Wilson55249ba2010-12-22 14:04:47 +00001460 /* Workaround an erratum on the i830 which causes a hang if
1461 * the TAIL pointer points to within the last 2 cachelines
1462 * of the buffer.
1463 */
1464 ring->effective_size = ring->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01001465 if (IS_I830(dev) || IS_845G(dev))
Chris Wilson18393f62014-04-09 09:19:40 +01001466 ring->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00001467
Brad Volkin44e895a2014-05-10 14:10:43 -07001468 ret = i915_cmd_parser_init_ring(ring);
1469 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001470 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08001471
Oscar Mateo8ee14972014-05-22 14:13:34 +01001472 ret = ring->init(ring);
1473 if (ret)
1474 goto error;
1475
1476 return 0;
1477
1478error:
1479 kfree(ringbuf);
1480 ring->buffer = NULL;
1481 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001482}
1483
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001484void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001485{
Chris Wilsone3efda42014-04-09 09:19:41 +01001486 struct drm_i915_private *dev_priv = to_i915(ring->dev);
Chris Wilson33626e62010-10-29 16:18:36 +01001487
Chris Wilson05394f32010-11-08 19:18:58 +00001488 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001489 return;
1490
Chris Wilsone3efda42014-04-09 09:19:41 +01001491 intel_stop_ring_buffer(ring);
1492 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01001493
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001494 iounmap(ring->virtual_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001495
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001496 i915_gem_object_ggtt_unpin(ring->obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001497 drm_gem_object_unreference(&ring->obj->base);
1498 ring->obj = NULL;
Ben Widawsky3d57e5b2013-10-14 10:01:36 -07001499 ring->preallocated_lazy_request = NULL;
1500 ring->outstanding_lazy_seqno = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01001501
Zou Nan hai8d192152010-11-02 16:31:01 +08001502 if (ring->cleanup)
1503 ring->cleanup(ring);
1504
Chris Wilson78501ea2010-10-27 12:18:21 +01001505 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07001506
1507 i915_cmd_parser_fini_ring(ring);
Oscar Mateo8ee14972014-05-22 14:13:34 +01001508
1509 kfree(ring->buffer);
1510 ring->buffer = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001511}
1512
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001513static int intel_ring_wait_request(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00001514{
1515 struct drm_i915_gem_request *request;
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001516 u32 seqno = 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001517 int ret;
1518
Chris Wilsona71d8d92012-02-15 11:25:36 +00001519 if (ring->last_retired_head != -1) {
1520 ring->head = ring->last_retired_head;
1521 ring->last_retired_head = -1;
Chris Wilson1f709992014-01-27 22:43:07 +00001522
Chris Wilsona71d8d92012-02-15 11:25:36 +00001523 ring->space = ring_space(ring);
1524 if (ring->space >= n)
1525 return 0;
1526 }
1527
1528 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001529 if (__ring_space(request->tail, ring->tail, ring->size) >= n) {
Chris Wilsona71d8d92012-02-15 11:25:36 +00001530 seqno = request->seqno;
1531 break;
1532 }
Chris Wilsona71d8d92012-02-15 11:25:36 +00001533 }
1534
1535 if (seqno == 0)
1536 return -ENOSPC;
1537
Chris Wilson1f709992014-01-27 22:43:07 +00001538 ret = i915_wait_seqno(ring, seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001539 if (ret)
1540 return ret;
1541
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001542 i915_gem_retire_requests_ring(ring);
1543 ring->head = ring->last_retired_head;
1544 ring->last_retired_head = -1;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001545
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001546 ring->space = ring_space(ring);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001547 return 0;
1548}
1549
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001550static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001551{
Chris Wilson78501ea2010-10-27 12:18:21 +01001552 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001553 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001554 unsigned long end;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001555 int ret;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001556
Chris Wilsona71d8d92012-02-15 11:25:36 +00001557 ret = intel_ring_wait_request(ring, n);
1558 if (ret != -ENOSPC)
1559 return ret;
1560
Chris Wilson09246732013-08-10 22:16:32 +01001561 /* force the tail write in case we have been skipping them */
1562 __intel_ring_advance(ring);
1563
Daniel Vetter63ed2cb2012-04-23 16:50:50 +02001564 /* With GEM the hangcheck timer should kick us out of the loop,
1565 * leaving it early runs the risk of corrupting GEM state (due
1566 * to running on almost untested codepaths). But on resume
1567 * timers don't work yet, so prevent a complete hang in that
1568 * case by choosing an insanely large timeout. */
1569 end = jiffies + 60 * HZ;
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001570
Chris Wilsondcfe0502014-05-05 09:07:32 +01001571 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001572 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001573 ring->head = I915_READ_HEAD(ring);
1574 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001575 if (ring->space >= n) {
Chris Wilsondcfe0502014-05-05 09:07:32 +01001576 ret = 0;
1577 break;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001578 }
1579
Daniel Vetterfb19e2a2014-02-12 23:44:34 +01001580 if (!drm_core_check_feature(dev, DRIVER_MODESET) &&
1581 dev->primary->master) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001582 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1583 if (master_priv->sarea_priv)
1584 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1585 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001586
Chris Wilsone60a0b12010-10-13 10:09:14 +01001587 msleep(1);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001588
Chris Wilsondcfe0502014-05-05 09:07:32 +01001589 if (dev_priv->mm.interruptible && signal_pending(current)) {
1590 ret = -ERESTARTSYS;
1591 break;
1592 }
1593
Daniel Vetter33196de2012-11-14 17:14:05 +01001594 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1595 dev_priv->mm.interruptible);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001596 if (ret)
Chris Wilsondcfe0502014-05-05 09:07:32 +01001597 break;
1598
1599 if (time_after(jiffies, end)) {
1600 ret = -EBUSY;
1601 break;
1602 }
1603 } while (1);
Chris Wilsondb53a302011-02-03 11:57:46 +00001604 trace_i915_ring_wait_end(ring);
Chris Wilsondcfe0502014-05-05 09:07:32 +01001605 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001606}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001607
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001608static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00001609{
1610 uint32_t __iomem *virt;
1611 int rem = ring->size - ring->tail;
1612
1613 if (ring->space < rem) {
1614 int ret = ring_wait_for_space(ring, rem);
1615 if (ret)
1616 return ret;
1617 }
1618
1619 virt = ring->virtual_start + ring->tail;
1620 rem /= 4;
1621 while (rem--)
1622 iowrite32(MI_NOOP, virt++);
1623
1624 ring->tail = 0;
1625 ring->space = ring_space(ring);
1626
1627 return 0;
1628}
1629
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001630int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00001631{
1632 u32 seqno;
1633 int ret;
1634
1635 /* We need to add any requests required to flush the objects and ring */
Chris Wilson18235212013-09-04 10:45:51 +01001636 if (ring->outstanding_lazy_seqno) {
Mika Kuoppala0025c072013-06-12 12:35:30 +03001637 ret = i915_add_request(ring, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00001638 if (ret)
1639 return ret;
1640 }
1641
1642 /* Wait upon the last request to be completed */
1643 if (list_empty(&ring->request_list))
1644 return 0;
1645
1646 seqno = list_entry(ring->request_list.prev,
1647 struct drm_i915_gem_request,
1648 list)->seqno;
1649
1650 return i915_wait_seqno(ring, seqno);
1651}
1652
Chris Wilson9d7730912012-11-27 16:22:52 +00001653static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001654intel_ring_alloc_seqno(struct intel_engine_cs *ring)
Chris Wilson9d7730912012-11-27 16:22:52 +00001655{
Chris Wilson18235212013-09-04 10:45:51 +01001656 if (ring->outstanding_lazy_seqno)
Chris Wilson9d7730912012-11-27 16:22:52 +00001657 return 0;
1658
Chris Wilson3c0e2342013-09-04 10:45:52 +01001659 if (ring->preallocated_lazy_request == NULL) {
1660 struct drm_i915_gem_request *request;
1661
1662 request = kmalloc(sizeof(*request), GFP_KERNEL);
1663 if (request == NULL)
1664 return -ENOMEM;
1665
1666 ring->preallocated_lazy_request = request;
1667 }
1668
Chris Wilson18235212013-09-04 10:45:51 +01001669 return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
Chris Wilson9d7730912012-11-27 16:22:52 +00001670}
1671
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001672static int __intel_ring_prepare(struct intel_engine_cs *ring,
Chris Wilson304d6952014-01-02 14:32:35 +00001673 int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001674{
1675 int ret;
1676
1677 if (unlikely(ring->tail + bytes > ring->effective_size)) {
1678 ret = intel_wrap_ring_buffer(ring);
1679 if (unlikely(ret))
1680 return ret;
1681 }
1682
1683 if (unlikely(ring->space < bytes)) {
1684 ret = ring_wait_for_space(ring, bytes);
1685 if (unlikely(ret))
1686 return ret;
1687 }
1688
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001689 return 0;
1690}
1691
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001692int intel_ring_begin(struct intel_engine_cs *ring,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001693 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001694{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001695 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001696 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001697
Daniel Vetter33196de2012-11-14 17:14:05 +01001698 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1699 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02001700 if (ret)
1701 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00001702
Chris Wilson304d6952014-01-02 14:32:35 +00001703 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
1704 if (ret)
1705 return ret;
1706
Chris Wilson9d7730912012-11-27 16:22:52 +00001707 /* Preallocate the olr before touching the ring */
1708 ret = intel_ring_alloc_seqno(ring);
1709 if (ret)
1710 return ret;
1711
Chris Wilson304d6952014-01-02 14:32:35 +00001712 ring->space -= num_dwords * sizeof(uint32_t);
1713 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001714}
1715
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001716/* Align the ring tail to a cacheline boundary */
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001717int intel_ring_cacheline_align(struct intel_engine_cs *ring)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001718{
Chris Wilson18393f62014-04-09 09:19:40 +01001719 int num_dwords = (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001720 int ret;
1721
1722 if (num_dwords == 0)
1723 return 0;
1724
Chris Wilson18393f62014-04-09 09:19:40 +01001725 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001726 ret = intel_ring_begin(ring, num_dwords);
1727 if (ret)
1728 return ret;
1729
1730 while (num_dwords--)
1731 intel_ring_emit(ring, MI_NOOP);
1732
1733 intel_ring_advance(ring);
1734
1735 return 0;
1736}
1737
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001738void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001739{
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001740 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001741
Chris Wilson18235212013-09-04 10:45:51 +01001742 BUG_ON(ring->outstanding_lazy_seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001743
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001744 if (INTEL_INFO(ring->dev)->gen >= 6) {
1745 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
1746 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Ben Widawsky50201502013-08-12 16:53:03 -07001747 if (HAS_VEBOX(ring->dev))
1748 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01001749 }
Chris Wilson297b0c52010-10-22 17:02:41 +01001750
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001751 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03001752 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01001753}
1754
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001755static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001756 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001757{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001758 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001759
1760 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001761
Chris Wilson12f55812012-07-05 17:14:01 +01001762 /* Disable notification that the ring is IDLE. The GT
1763 * will then assume that it is busy and bring it out of rc6.
1764 */
1765 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1766 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1767
1768 /* Clear the context id. Here be magic! */
1769 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1770
1771 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04001772 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01001773 GEN6_BSD_SLEEP_INDICATOR) == 0,
1774 50))
1775 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001776
Chris Wilson12f55812012-07-05 17:14:01 +01001777 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04001778 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01001779 POSTING_READ(RING_TAIL(ring->mmio_base));
1780
1781 /* Let the ring send IDLE messages to the GT again,
1782 * and so let it sleep to conserve power when idle.
1783 */
Akshay Joshi0206e352011-08-16 15:34:10 -04001784 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01001785 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001786}
1787
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001788static int gen6_bsd_ring_flush(struct intel_engine_cs *ring,
Ben Widawskyea251322013-05-28 19:22:21 -07001789 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001790{
Chris Wilson71a77e02011-02-02 12:13:49 +00001791 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001792 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001793
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001794 ret = intel_ring_begin(ring, 4);
1795 if (ret)
1796 return ret;
1797
Chris Wilson71a77e02011-02-02 12:13:49 +00001798 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001799 if (INTEL_INFO(ring->dev)->gen >= 8)
1800 cmd += 1;
Jesse Barnes9a289772012-10-26 09:42:42 -07001801 /*
1802 * Bspec vol 1c.5 - video engine command streamer:
1803 * "If ENABLED, all TLBs will be invalidated once the flush
1804 * operation is complete. This bit is only valid when the
1805 * Post-Sync Operation field is a value of 1h or 3h."
1806 */
Chris Wilson71a77e02011-02-02 12:13:49 +00001807 if (invalidate & I915_GEM_GPU_DOMAINS)
Jesse Barnes9a289772012-10-26 09:42:42 -07001808 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1809 MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
Chris Wilson71a77e02011-02-02 12:13:49 +00001810 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07001811 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001812 if (INTEL_INFO(ring->dev)->gen >= 8) {
1813 intel_ring_emit(ring, 0); /* upper addr */
1814 intel_ring_emit(ring, 0); /* value */
1815 } else {
1816 intel_ring_emit(ring, 0);
1817 intel_ring_emit(ring, MI_NOOP);
1818 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001819 intel_ring_advance(ring);
1820 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001821}
1822
1823static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001824gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001825 u64 offset, u32 len,
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001826 unsigned flags)
1827{
Ben Widawsky28cf5412013-11-02 21:07:26 -07001828 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1829 bool ppgtt = dev_priv->mm.aliasing_ppgtt != NULL &&
1830 !(flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001831 int ret;
1832
1833 ret = intel_ring_begin(ring, 4);
1834 if (ret)
1835 return ret;
1836
1837 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07001838 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001839 intel_ring_emit(ring, lower_32_bits(offset));
1840 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001841 intel_ring_emit(ring, MI_NOOP);
1842 intel_ring_advance(ring);
1843
1844 return 0;
1845}
1846
1847static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001848hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001849 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001850 unsigned flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001851{
Akshay Joshi0206e352011-08-16 15:34:10 -04001852 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001853
Akshay Joshi0206e352011-08-16 15:34:10 -04001854 ret = intel_ring_begin(ring, 2);
1855 if (ret)
1856 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001857
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001858 intel_ring_emit(ring,
1859 MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
1860 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
1861 /* bit0-7 is the length on GEN6+ */
1862 intel_ring_emit(ring, offset);
1863 intel_ring_advance(ring);
1864
1865 return 0;
1866}
1867
1868static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001869gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001870 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001871 unsigned flags)
1872{
1873 int ret;
1874
1875 ret = intel_ring_begin(ring, 2);
1876 if (ret)
1877 return ret;
1878
1879 intel_ring_emit(ring,
1880 MI_BATCH_BUFFER_START |
1881 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04001882 /* bit0-7 is the length on GEN6+ */
1883 intel_ring_emit(ring, offset);
1884 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001885
Akshay Joshi0206e352011-08-16 15:34:10 -04001886 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001887}
1888
Chris Wilson549f7362010-10-19 11:19:32 +01001889/* Blitter support (SandyBridge+) */
1890
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001891static int gen6_ring_flush(struct intel_engine_cs *ring,
Ben Widawskyea251322013-05-28 19:22:21 -07001892 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001893{
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001894 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00001895 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001896 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001897
Daniel Vetter6a233c72011-12-14 13:57:07 +01001898 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001899 if (ret)
1900 return ret;
1901
Chris Wilson71a77e02011-02-02 12:13:49 +00001902 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001903 if (INTEL_INFO(ring->dev)->gen >= 8)
1904 cmd += 1;
Jesse Barnes9a289772012-10-26 09:42:42 -07001905 /*
1906 * Bspec vol 1c.3 - blitter engine command streamer:
1907 * "If ENABLED, all TLBs will be invalidated once the flush
1908 * operation is complete. This bit is only valid when the
1909 * Post-Sync Operation field is a value of 1h or 3h."
1910 */
Chris Wilson71a77e02011-02-02 12:13:49 +00001911 if (invalidate & I915_GEM_DOMAIN_RENDER)
Jesse Barnes9a289772012-10-26 09:42:42 -07001912 cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
Daniel Vetterb3fcabb2012-11-04 12:24:47 +01001913 MI_FLUSH_DW_OP_STOREDW;
Chris Wilson71a77e02011-02-02 12:13:49 +00001914 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07001915 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001916 if (INTEL_INFO(ring->dev)->gen >= 8) {
1917 intel_ring_emit(ring, 0); /* upper addr */
1918 intel_ring_emit(ring, 0); /* value */
1919 } else {
1920 intel_ring_emit(ring, 0);
1921 intel_ring_emit(ring, MI_NOOP);
1922 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001923 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001924
Ville Syrjälä9688eca2013-11-06 23:02:19 +02001925 if (IS_GEN7(dev) && !invalidate && flush)
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001926 return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
1927
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001928 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001929}
1930
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001931int intel_init_render_ring_buffer(struct drm_device *dev)
1932{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001933 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001934 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001935
Daniel Vetter59465b52012-04-11 22:12:48 +02001936 ring->name = "render ring";
1937 ring->id = RCS;
1938 ring->mmio_base = RENDER_RING_BASE;
1939
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001940 if (INTEL_INFO(dev)->gen >= 6) {
1941 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03001942 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01001943 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03001944 ring->flush = gen6_render_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001945 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawskya5f3d682013-11-02 21:07:27 -07001946 ring->flush = gen8_render_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001947 ring->irq_get = gen8_ring_get_irq;
1948 ring->irq_put = gen8_ring_put_irq;
1949 } else {
1950 ring->irq_get = gen6_ring_get_irq;
1951 ring->irq_put = gen6_ring_put_irq;
1952 }
Ben Widawskycc609d52013-05-28 19:22:29 -07001953 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001954 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001955 ring->set_seqno = ring_set_seqno;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001956 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07001957 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08001958 /*
1959 * The current semaphore is only applied on pre-gen8 platform.
1960 * And there is no VCS2 ring on the pre-gen8 platform. So the
1961 * semaphore between RCS and VCS2 is initialized as INVALID.
1962 * Gen8 will initialize the sema between VCS2 and RCS later.
1963 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07001964 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
1965 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
1966 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
1967 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
1968 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
1969 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
1970 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
1971 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
1972 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
1973 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001974 } else if (IS_GEN5(dev)) {
1975 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001976 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001977 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001978 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02001979 ring->irq_get = gen5_ring_get_irq;
1980 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07001981 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
1982 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02001983 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02001984 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001985 if (INTEL_INFO(dev)->gen < 4)
1986 ring->flush = gen2_render_ring_flush;
1987 else
1988 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02001989 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001990 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001991 if (IS_GEN2(dev)) {
1992 ring->irq_get = i8xx_ring_get_irq;
1993 ring->irq_put = i8xx_ring_put_irq;
1994 } else {
1995 ring->irq_get = i9xx_ring_get_irq;
1996 ring->irq_put = i9xx_ring_put_irq;
1997 }
Daniel Vettere3670312012-04-11 22:12:53 +02001998 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001999 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002000 ring->write_tail = ring_write_tail;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002001 if (IS_HASWELL(dev))
2002 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002003 else if (IS_GEN8(dev))
2004 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002005 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002006 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2007 else if (INTEL_INFO(dev)->gen >= 4)
2008 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2009 else if (IS_I830(dev) || IS_845G(dev))
2010 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2011 else
2012 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02002013 ring->init = init_render_ring;
2014 ring->cleanup = render_ring_cleanup;
2015
Daniel Vetterb45305f2012-12-17 16:21:27 +01002016 /* Workaround batchbuffer to combat CS tlb bug. */
2017 if (HAS_BROKEN_CS_TLB(dev)) {
2018 struct drm_i915_gem_object *obj;
2019 int ret;
2020
2021 obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
2022 if (obj == NULL) {
2023 DRM_ERROR("Failed to allocate batch bo\n");
2024 return -ENOMEM;
2025 }
2026
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002027 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002028 if (ret != 0) {
2029 drm_gem_object_unreference(&obj->base);
2030 DRM_ERROR("Failed to ping batch bo\n");
2031 return ret;
2032 }
2033
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002034 ring->scratch.obj = obj;
2035 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002036 }
2037
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002038 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002039}
2040
Chris Wilsone8616b62011-01-20 09:57:11 +00002041int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
2042{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002043 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002044 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Oscar Mateo8ee14972014-05-22 14:13:34 +01002045 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002046 int ret;
Chris Wilsone8616b62011-01-20 09:57:11 +00002047
Oscar Mateo8ee14972014-05-22 14:13:34 +01002048 if (ringbuf == NULL) {
2049 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2050 if (!ringbuf)
2051 return -ENOMEM;
2052 ring->buffer = ringbuf;
2053 }
2054
Daniel Vetter59465b52012-04-11 22:12:48 +02002055 ring->name = "render ring";
2056 ring->id = RCS;
2057 ring->mmio_base = RENDER_RING_BASE;
2058
Chris Wilsone8616b62011-01-20 09:57:11 +00002059 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetterb4178f82012-04-11 22:12:51 +02002060 /* non-kms not supported on gen6+ */
Oscar Mateo8ee14972014-05-22 14:13:34 +01002061 ret = -ENODEV;
2062 goto err_ringbuf;
Chris Wilsone8616b62011-01-20 09:57:11 +00002063 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002064
2065 /* Note: gem is not supported on gen5/ilk without kms (the corresponding
2066 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
2067 * the special gen5 functions. */
2068 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002069 if (INTEL_INFO(dev)->gen < 4)
2070 ring->flush = gen2_render_ring_flush;
2071 else
2072 ring->flush = gen4_render_ring_flush;
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002073 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002074 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002075 if (IS_GEN2(dev)) {
2076 ring->irq_get = i8xx_ring_get_irq;
2077 ring->irq_put = i8xx_ring_put_irq;
2078 } else {
2079 ring->irq_get = i9xx_ring_get_irq;
2080 ring->irq_put = i9xx_ring_put_irq;
2081 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002082 ring->irq_enable_mask = I915_USER_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002083 ring->write_tail = ring_write_tail;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002084 if (INTEL_INFO(dev)->gen >= 4)
2085 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2086 else if (IS_I830(dev) || IS_845G(dev))
2087 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2088 else
2089 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02002090 ring->init = init_render_ring;
2091 ring->cleanup = render_ring_cleanup;
Chris Wilsone8616b62011-01-20 09:57:11 +00002092
2093 ring->dev = dev;
2094 INIT_LIST_HEAD(&ring->active_list);
2095 INIT_LIST_HEAD(&ring->request_list);
Chris Wilsone8616b62011-01-20 09:57:11 +00002096
2097 ring->size = size;
2098 ring->effective_size = ring->size;
Mika Kuoppala17f10fd2012-10-29 16:59:26 +02002099 if (IS_I830(ring->dev) || IS_845G(ring->dev))
Chris Wilson18393f62014-04-09 09:19:40 +01002100 ring->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilsone8616b62011-01-20 09:57:11 +00002101
Daniel Vetter4225d0f2012-04-26 23:28:16 +02002102 ring->virtual_start = ioremap_wc(start, size);
2103 if (ring->virtual_start == NULL) {
Chris Wilsone8616b62011-01-20 09:57:11 +00002104 DRM_ERROR("can not ioremap virtual address for"
2105 " ring buffer\n");
Oscar Mateo8ee14972014-05-22 14:13:34 +01002106 ret = -ENOMEM;
2107 goto err_ringbuf;
Chris Wilsone8616b62011-01-20 09:57:11 +00002108 }
2109
Chris Wilson6b8294a2012-11-16 11:43:20 +00002110 if (!I915_NEED_GFX_HWS(dev)) {
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002111 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002112 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002113 goto err_vstart;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002114 }
2115
Chris Wilsone8616b62011-01-20 09:57:11 +00002116 return 0;
Oscar Mateo8ee14972014-05-22 14:13:34 +01002117
2118err_vstart:
2119 iounmap(ring->virtual_start);
2120err_ringbuf:
2121 kfree(ringbuf);
2122 ring->buffer = NULL;
2123 return ret;
Chris Wilsone8616b62011-01-20 09:57:11 +00002124}
2125
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002126int intel_init_bsd_ring_buffer(struct drm_device *dev)
2127{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002128 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002129 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002130
Daniel Vetter58fa3832012-04-11 22:12:49 +02002131 ring->name = "bsd ring";
2132 ring->id = VCS;
2133
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002134 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002135 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002136 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002137 /* gen6 bsd needs a special wa for tail updates */
2138 if (IS_GEN6(dev))
2139 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002140 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002141 ring->add_request = gen6_add_request;
2142 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002143 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002144 if (INTEL_INFO(dev)->gen >= 8) {
2145 ring->irq_enable_mask =
2146 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2147 ring->irq_get = gen8_ring_get_irq;
2148 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002149 ring->dispatch_execbuffer =
2150 gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002151 } else {
2152 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2153 ring->irq_get = gen6_ring_get_irq;
2154 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002155 ring->dispatch_execbuffer =
2156 gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002157 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002158 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002159 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002160 /*
2161 * The current semaphore is only applied on pre-gen8 platform.
2162 * And there is no VCS2 ring on the pre-gen8 platform. So the
2163 * semaphore between VCS and VCS2 is initialized as INVALID.
2164 * Gen8 will initialize the sema between VCS2 and VCS later.
2165 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002166 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2167 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2168 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2169 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2170 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2171 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2172 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2173 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2174 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2175 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002176 } else {
2177 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002178 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002179 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002180 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002181 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002182 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002183 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002184 ring->irq_get = gen5_ring_get_irq;
2185 ring->irq_put = gen5_ring_put_irq;
2186 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002187 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002188 ring->irq_get = i9xx_ring_get_irq;
2189 ring->irq_put = i9xx_ring_put_irq;
2190 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002191 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002192 }
2193 ring->init = init_ring_common;
2194
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002195 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002196}
Chris Wilson549f7362010-10-19 11:19:32 +01002197
Zhao Yakui845f74a2014-04-17 10:37:37 +08002198/**
2199 * Initialize the second BSD ring for Broadwell GT3.
2200 * It is noted that this only exists on Broadwell GT3.
2201 */
2202int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2203{
2204 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002205 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002206
2207 if ((INTEL_INFO(dev)->gen != 8)) {
2208 DRM_ERROR("No dual-BSD ring on non-BDW machine\n");
2209 return -EINVAL;
2210 }
2211
2212 ring->name = "bds2_ring";
2213 ring->id = VCS2;
2214
2215 ring->write_tail = ring_write_tail;
2216 ring->mmio_base = GEN8_BSD2_RING_BASE;
2217 ring->flush = gen6_bsd_ring_flush;
2218 ring->add_request = gen6_add_request;
2219 ring->get_seqno = gen6_ring_get_seqno;
2220 ring->set_seqno = ring_set_seqno;
2221 ring->irq_enable_mask =
2222 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2223 ring->irq_get = gen8_ring_get_irq;
2224 ring->irq_put = gen8_ring_put_irq;
2225 ring->dispatch_execbuffer =
2226 gen8_ring_dispatch_execbuffer;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002227 ring->semaphore.sync_to = gen6_ring_sync;
Oscar Mateod1533372014-05-09 13:44:59 +01002228 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002229 /*
2230 * The current semaphore is only applied on the pre-gen8. And there
2231 * is no bsd2 ring on the pre-gen8. So now the semaphore_register
2232 * between VCS2 and other ring is initialized as invalid.
2233 * Gen8 will initialize the sema between VCS2 and other ring later.
2234 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002235 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2236 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2237 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2238 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2239 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2240 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2241 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2242 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2243 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2244 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002245
2246 ring->init = init_ring_common;
2247
2248 return intel_init_ring_buffer(dev, ring);
2249}
2250
Chris Wilson549f7362010-10-19 11:19:32 +01002251int intel_init_blt_ring_buffer(struct drm_device *dev)
2252{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002253 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002254 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002255
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002256 ring->name = "blitter ring";
2257 ring->id = BCS;
2258
2259 ring->mmio_base = BLT_RING_BASE;
2260 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002261 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002262 ring->add_request = gen6_add_request;
2263 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002264 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002265 if (INTEL_INFO(dev)->gen >= 8) {
2266 ring->irq_enable_mask =
2267 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2268 ring->irq_get = gen8_ring_get_irq;
2269 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002270 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002271 } else {
2272 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2273 ring->irq_get = gen6_ring_get_irq;
2274 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002275 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002276 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002277 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002278 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002279 /*
2280 * The current semaphore is only applied on pre-gen8 platform. And
2281 * there is no VCS2 ring on the pre-gen8 platform. So the semaphore
2282 * between BCS and VCS2 is initialized as INVALID.
2283 * Gen8 will initialize the sema between BCS and VCS2 later.
2284 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002285 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2286 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2287 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2288 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2289 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2290 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2291 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2292 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2293 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2294 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002295 ring->init = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002296
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002297 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002298}
Chris Wilsona7b97612012-07-20 12:41:08 +01002299
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002300int intel_init_vebox_ring_buffer(struct drm_device *dev)
2301{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002302 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002303 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002304
2305 ring->name = "video enhancement ring";
2306 ring->id = VECS;
2307
2308 ring->mmio_base = VEBOX_RING_BASE;
2309 ring->write_tail = ring_write_tail;
2310 ring->flush = gen6_ring_flush;
2311 ring->add_request = gen6_add_request;
2312 ring->get_seqno = gen6_ring_get_seqno;
2313 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002314
2315 if (INTEL_INFO(dev)->gen >= 8) {
2316 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002317 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002318 ring->irq_get = gen8_ring_get_irq;
2319 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002320 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002321 } else {
2322 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2323 ring->irq_get = hsw_vebox_get_irq;
2324 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002325 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002326 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002327 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002328 ring->semaphore.signal = gen6_signal;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002329 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2330 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2331 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2332 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2333 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2334 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2335 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2336 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2337 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2338 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002339 ring->init = init_ring_common;
2340
2341 return intel_init_ring_buffer(dev, ring);
2342}
2343
Chris Wilsona7b97612012-07-20 12:41:08 +01002344int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002345intel_ring_flush_all_caches(struct intel_engine_cs *ring)
Chris Wilsona7b97612012-07-20 12:41:08 +01002346{
2347 int ret;
2348
2349 if (!ring->gpu_caches_dirty)
2350 return 0;
2351
2352 ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2353 if (ret)
2354 return ret;
2355
2356 trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2357
2358 ring->gpu_caches_dirty = false;
2359 return 0;
2360}
2361
2362int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002363intel_ring_invalidate_all_caches(struct intel_engine_cs *ring)
Chris Wilsona7b97612012-07-20 12:41:08 +01002364{
2365 uint32_t flush_domains;
2366 int ret;
2367
2368 flush_domains = 0;
2369 if (ring->gpu_caches_dirty)
2370 flush_domains = I915_GEM_GPU_DOMAINS;
2371
2372 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2373 if (ret)
2374 return ret;
2375
2376 trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2377
2378 ring->gpu_caches_dirty = false;
2379 return 0;
2380}
Chris Wilsone3efda42014-04-09 09:19:41 +01002381
2382void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002383intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002384{
2385 int ret;
2386
2387 if (!intel_ring_initialized(ring))
2388 return;
2389
2390 ret = intel_ring_idle(ring);
2391 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2392 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2393 ring->name, ret);
2394
2395 stop_ring(ring);
2396}