blob: 9907d66becddf41992b367ddaf7ebfbcab081d08 [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
Chris Wilsonc7dca472011-01-20 17:00:10 +000051static inline int ring_space(struct intel_ring_buffer *ring)
52{
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
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020056static bool intel_ring_stopped(struct intel_ring_buffer *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
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020062void __intel_ring_advance(struct intel_ring_buffer *ring)
63{
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
Chris Wilson46f0f8d2012-04-18 11:12:11 +010071gen2_render_ring_flush(struct intel_ring_buffer *ring,
72 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
97gen4_render_ring_flush(struct intel_ring_buffer *ring,
98 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
192intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
193{
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
227gen6_render_ring_flush(struct intel_ring_buffer *ring,
228 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
Paulo Zanonif3987632012-08-17 18:35:43 -0300279gen7_render_ring_cs_stall_wa(struct intel_ring_buffer *ring)
280{
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
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -0300297static int gen7_ring_fbc_flush(struct intel_ring_buffer *ring, u32 value)
298{
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
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300321gen7_render_ring_flush(struct intel_ring_buffer *ring,
322 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
382gen8_render_ring_flush(struct intel_ring_buffer *ring,
383 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
Chris Wilson78501ea2010-10-27 12:18:21 +0100422static void ring_write_tail(struct intel_ring_buffer *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
Chris Wilson50877442014-03-21 12:41:53 +0000429u64 intel_ring_get_active_head(struct intel_ring_buffer *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
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200445static void ring_setup_phys_status_page(struct intel_ring_buffer *ring)
446{
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
Chris Wilson9991ae72014-04-02 16:36:07 +0100456static bool stop_ring(struct intel_ring_buffer *ring)
457{
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
Chris Wilson78501ea2010-10-27 12:18:21 +0100480static int init_ring_common(struct intel_ring_buffer *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
558init_pipe_control(struct intel_ring_buffer *ring)
559{
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
Chris Wilson78501ea2010-10-27 12:18:21 +0100599static int init_render_ring(struct intel_ring_buffer *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ä82852222014-02-27 21:59:03 +0200613 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw
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
Chris Wilsonc6df5412010-12-15 09:56:50 +0000655static void render_ring_cleanup(struct intel_ring_buffer *ring)
656{
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
Ben Widawsky024a43e2014-04-29 14:52:30 -0700671static int gen6_signal(struct intel_ring_buffer *signaller,
672 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;
Ben Widawsky78325f22014-04-29 14:52:29 -0700676 struct intel_ring_buffer *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);
687
688 ret = intel_ring_begin(signaller, num_dwords);
689 if (ret)
690 return ret;
691#undef MBOX_UPDATE_DWORDS
692
Ben Widawsky78325f22014-04-29 14:52:29 -0700693 for_each_ring(useless, dev_priv, i) {
694 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
695 if (mbox_reg != GEN6_NOSYNC) {
696 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
697 intel_ring_emit(signaller, mbox_reg);
698 intel_ring_emit(signaller, signaller->outstanding_lazy_seqno);
699 intel_ring_emit(signaller, MI_NOOP);
700 } else {
701 intel_ring_emit(signaller, MI_NOOP);
702 intel_ring_emit(signaller, MI_NOOP);
703 intel_ring_emit(signaller, MI_NOOP);
704 intel_ring_emit(signaller, MI_NOOP);
705 }
706 }
Ben Widawsky024a43e2014-04-29 14:52:30 -0700707
708 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000709}
710
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700711/**
712 * gen6_add_request - Update the semaphore mailbox registers
713 *
714 * @ring - ring that is adding a request
715 * @seqno - return seqno stuck into the ring
716 *
717 * Update the mailbox registers in the *other* rings with the current seqno.
718 * This acts like a signal in the canonical semaphore.
719 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000720static int
Chris Wilson9d7730912012-11-27 16:22:52 +0000721gen6_add_request(struct intel_ring_buffer *ring)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000722{
Ben Widawsky024a43e2014-04-29 14:52:30 -0700723 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000724
Ben Widawsky024a43e2014-04-29 14:52:30 -0700725 ret = ring->semaphore.signal(ring, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000726 if (ret)
727 return ret;
728
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000729 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
730 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson18235212013-09-04 10:45:51 +0100731 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000732 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +0100733 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000734
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000735 return 0;
736}
737
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200738static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
739 u32 seqno)
740{
741 struct drm_i915_private *dev_priv = dev->dev_private;
742 return dev_priv->last_seqno < seqno;
743}
744
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700745/**
746 * intel_ring_sync - sync the waiter to the signaller on seqno
747 *
748 * @waiter - ring that is waiting
749 * @signaller - ring which has, or will signal
750 * @seqno - seqno which the waiter will block on
751 */
752static int
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200753gen6_ring_sync(struct intel_ring_buffer *waiter,
754 struct intel_ring_buffer *signaller,
755 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000756{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700757 u32 dw1 = MI_SEMAPHORE_MBOX |
758 MI_SEMAPHORE_COMPARE |
759 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -0700760 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
761 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000762
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700763 /* Throughout all of the GEM code, seqno passed implies our current
764 * seqno is >= the last seqno executed. However for hardware the
765 * comparison is strictly greater than.
766 */
767 seqno -= 1;
768
Ben Widawskyebc348b2014-04-29 14:52:28 -0700769 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200770
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700771 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000772 if (ret)
773 return ret;
774
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200775 /* If seqno wrap happened, omit the wait with no-ops */
776 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -0700777 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +0200778 intel_ring_emit(waiter, seqno);
779 intel_ring_emit(waiter, 0);
780 intel_ring_emit(waiter, MI_NOOP);
781 } else {
782 intel_ring_emit(waiter, MI_NOOP);
783 intel_ring_emit(waiter, MI_NOOP);
784 intel_ring_emit(waiter, MI_NOOP);
785 intel_ring_emit(waiter, MI_NOOP);
786 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700787 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000788
789 return 0;
790}
791
Chris Wilsonc6df5412010-12-15 09:56:50 +0000792#define PIPE_CONTROL_FLUSH(ring__, addr__) \
793do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200794 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
795 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000796 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
797 intel_ring_emit(ring__, 0); \
798 intel_ring_emit(ring__, 0); \
799} while (0)
800
801static int
Chris Wilson9d7730912012-11-27 16:22:52 +0000802pc_render_add_request(struct intel_ring_buffer *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000803{
Chris Wilson18393f62014-04-09 09:19:40 +0100804 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000805 int ret;
806
807 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
808 * incoherent with writes to memory, i.e. completely fubar,
809 * so we need to use PIPE_NOTIFY instead.
810 *
811 * However, we also need to workaround the qword write
812 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
813 * memory before requesting an interrupt.
814 */
815 ret = intel_ring_begin(ring, 32);
816 if (ret)
817 return ret;
818
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200819 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200820 PIPE_CONTROL_WRITE_FLUSH |
821 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100822 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson18235212013-09-04 10:45:51 +0100823 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000824 intel_ring_emit(ring, 0);
825 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100826 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +0000827 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +0100828 scratch_addr += 2 * CACHELINE_BYTES;
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 Wilsona71d8d92012-02-15 11:25:36 +0000836
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200837 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200838 PIPE_CONTROL_WRITE_FLUSH |
839 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000840 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100841 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson18235212013-09-04 10:45:51 +0100842 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000843 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +0100844 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000845
Chris Wilsonc6df5412010-12-15 09:56:50 +0000846 return 0;
847}
848
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800849static u32
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100850gen6_ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100851{
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100852 /* Workaround to force correct ordering between irq and seqno writes on
853 * ivb (and maybe also on snb) by reading from a CS register (like
854 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +0000855 if (!lazy_coherency) {
856 struct drm_i915_private *dev_priv = ring->dev->dev_private;
857 POSTING_READ(RING_ACTHD(ring->mmio_base));
858 }
859
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100860 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
861}
862
863static u32
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100864ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800865{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000866 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
867}
868
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +0200869static void
870ring_set_seqno(struct intel_ring_buffer *ring, u32 seqno)
871{
872 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
873}
874
Chris Wilsonc6df5412010-12-15 09:56:50 +0000875static u32
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100876pc_render_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000877{
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100878 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +0000879}
880
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +0200881static void
882pc_render_set_seqno(struct intel_ring_buffer *ring, u32 seqno)
883{
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100884 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +0200885}
886
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000887static bool
Daniel Vettere48d8632012-04-11 22:12:54 +0200888gen5_ring_get_irq(struct intel_ring_buffer *ring)
889{
890 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300891 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100892 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +0200893
894 if (!dev->irq_enabled)
895 return false;
896
Chris Wilson7338aef2012-04-24 21:48:47 +0100897 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300898 if (ring->irq_refcount++ == 0)
899 ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +0100900 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +0200901
902 return true;
903}
904
905static void
906gen5_ring_put_irq(struct intel_ring_buffer *ring)
907{
908 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300909 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100910 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +0200911
Chris Wilson7338aef2012-04-24 21:48:47 +0100912 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -0300913 if (--ring->irq_refcount == 0)
914 ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +0100915 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +0200916}
917
918static bool
Daniel Vettere3670312012-04-11 22:12:53 +0200919i9xx_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700920{
Chris Wilson78501ea2010-10-27 12:18:21 +0100921 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300922 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100923 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700924
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000925 if (!dev->irq_enabled)
926 return false;
927
Chris Wilson7338aef2012-04-24 21:48:47 +0100928 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +0200929 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +0200930 dev_priv->irq_mask &= ~ring->irq_enable_mask;
931 I915_WRITE(IMR, dev_priv->irq_mask);
932 POSTING_READ(IMR);
933 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100934 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000935
936 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700937}
938
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800939static void
Daniel Vettere3670312012-04-11 22:12:53 +0200940i9xx_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700941{
Chris Wilson78501ea2010-10-27 12:18:21 +0100942 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300943 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100944 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700945
Chris Wilson7338aef2012-04-24 21:48:47 +0100946 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +0200947 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +0200948 dev_priv->irq_mask |= ring->irq_enable_mask;
949 I915_WRITE(IMR, dev_priv->irq_mask);
950 POSTING_READ(IMR);
951 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100952 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700953}
954
Chris Wilsonc2798b12012-04-22 21:13:57 +0100955static bool
956i8xx_ring_get_irq(struct intel_ring_buffer *ring)
957{
958 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300959 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100960 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100961
962 if (!dev->irq_enabled)
963 return false;
964
Chris Wilson7338aef2012-04-24 21:48:47 +0100965 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +0200966 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +0100967 dev_priv->irq_mask &= ~ring->irq_enable_mask;
968 I915_WRITE16(IMR, dev_priv->irq_mask);
969 POSTING_READ16(IMR);
970 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100971 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100972
973 return true;
974}
975
976static void
977i8xx_ring_put_irq(struct intel_ring_buffer *ring)
978{
979 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300980 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100981 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100982
Chris Wilson7338aef2012-04-24 21:48:47 +0100983 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +0200984 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +0100985 dev_priv->irq_mask |= ring->irq_enable_mask;
986 I915_WRITE16(IMR, dev_priv->irq_mask);
987 POSTING_READ16(IMR);
988 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100989 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100990}
991
Chris Wilson78501ea2010-10-27 12:18:21 +0100992void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800993{
Eric Anholt45930102011-05-06 17:12:35 -0700994 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300995 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700996 u32 mmio = 0;
997
998 /* The ring status page addresses are no longer next to the rest of
999 * the ring registers as of gen7.
1000 */
1001 if (IS_GEN7(dev)) {
1002 switch (ring->id) {
Daniel Vetter96154f22011-12-14 13:57:00 +01001003 case RCS:
Eric Anholt45930102011-05-06 17:12:35 -07001004 mmio = RENDER_HWS_PGA_GEN7;
1005 break;
Daniel Vetter96154f22011-12-14 13:57:00 +01001006 case BCS:
Eric Anholt45930102011-05-06 17:12:35 -07001007 mmio = BLT_HWS_PGA_GEN7;
1008 break;
Zhao Yakui77fe2ff2014-04-17 10:37:39 +08001009 /*
1010 * VCS2 actually doesn't exist on Gen7. Only shut up
1011 * gcc switch check warning
1012 */
1013 case VCS2:
Daniel Vetter96154f22011-12-14 13:57:00 +01001014 case VCS:
Eric Anholt45930102011-05-06 17:12:35 -07001015 mmio = BSD_HWS_PGA_GEN7;
1016 break;
Ben Widawsky4a3dd192013-05-28 19:22:19 -07001017 case VECS:
Ben Widawsky9a8a2212013-05-28 19:22:23 -07001018 mmio = VEBOX_HWS_PGA_GEN7;
1019 break;
Eric Anholt45930102011-05-06 17:12:35 -07001020 }
1021 } else if (IS_GEN6(ring->dev)) {
1022 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
1023 } else {
Ben Widawskyeb0d4b72013-11-07 21:40:50 -08001024 /* XXX: gen8 returns to sanity */
Eric Anholt45930102011-05-06 17:12:35 -07001025 mmio = RING_HWS_PGA(ring->mmio_base);
1026 }
1027
Chris Wilson78501ea2010-10-27 12:18:21 +01001028 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
1029 POSTING_READ(mmio);
Chris Wilson884020b2013-08-06 19:01:14 +01001030
Damien Lespiaudc616b82014-03-13 01:40:28 +00001031 /*
1032 * Flush the TLB for this page
1033 *
1034 * FIXME: These two bits have disappeared on gen8, so a question
1035 * arises: do we still need this and if so how should we go about
1036 * invalidating the TLB?
1037 */
1038 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
Chris Wilson884020b2013-08-06 19:01:14 +01001039 u32 reg = RING_INSTPM(ring->mmio_base);
Naresh Kumar Kachhi02f6a1e2014-03-12 16:39:42 +05301040
1041 /* ring should be idle before issuing a sync flush*/
1042 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
1043
Chris Wilson884020b2013-08-06 19:01:14 +01001044 I915_WRITE(reg,
1045 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
1046 INSTPM_SYNC_FLUSH));
1047 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
1048 1000))
1049 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
1050 ring->name);
1051 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001052}
1053
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001054static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001055bsd_ring_flush(struct intel_ring_buffer *ring,
1056 u32 invalidate_domains,
1057 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001058{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001059 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001060
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001061 ret = intel_ring_begin(ring, 2);
1062 if (ret)
1063 return ret;
1064
1065 intel_ring_emit(ring, MI_FLUSH);
1066 intel_ring_emit(ring, MI_NOOP);
1067 intel_ring_advance(ring);
1068 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001069}
1070
Chris Wilson3cce4692010-10-27 16:11:02 +01001071static int
Chris Wilson9d7730912012-11-27 16:22:52 +00001072i9xx_add_request(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001073{
Chris Wilson3cce4692010-10-27 16:11:02 +01001074 int ret;
1075
1076 ret = intel_ring_begin(ring, 4);
1077 if (ret)
1078 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001079
Chris Wilson3cce4692010-10-27 16:11:02 +01001080 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1081 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson18235212013-09-04 10:45:51 +01001082 intel_ring_emit(ring, ring->outstanding_lazy_seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +01001083 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001084 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001085
Chris Wilson3cce4692010-10-27 16:11:02 +01001086 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001087}
1088
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001089static bool
Ben Widawsky25c06302012-03-29 19:11:27 -07001090gen6_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001091{
1092 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001093 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001094 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001095
1096 if (!dev->irq_enabled)
1097 return false;
1098
Chris Wilson7338aef2012-04-24 21:48:47 +01001099 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001100 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001101 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001102 I915_WRITE_IMR(ring,
1103 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001104 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001105 else
1106 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001107 ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001108 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001109 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001110
1111 return true;
1112}
1113
1114static void
Ben Widawsky25c06302012-03-29 19:11:27 -07001115gen6_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001116{
1117 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001118 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001119 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001120
Chris Wilson7338aef2012-04-24 21:48:47 +01001121 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001122 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001123 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001124 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001125 else
1126 I915_WRITE_IMR(ring, ~0);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001127 ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001128 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001129 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001130}
1131
Ben Widawskya19d2932013-05-28 19:22:30 -07001132static bool
1133hsw_vebox_get_irq(struct intel_ring_buffer *ring)
1134{
1135 struct drm_device *dev = ring->dev;
1136 struct drm_i915_private *dev_priv = dev->dev_private;
1137 unsigned long flags;
1138
1139 if (!dev->irq_enabled)
1140 return false;
1141
Daniel Vetter59cdb632013-07-04 23:35:28 +02001142 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001143 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001144 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03001145 snb_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001146 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001147 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001148
1149 return true;
1150}
1151
1152static void
1153hsw_vebox_put_irq(struct intel_ring_buffer *ring)
1154{
1155 struct drm_device *dev = ring->dev;
1156 struct drm_i915_private *dev_priv = dev->dev_private;
1157 unsigned long flags;
1158
1159 if (!dev->irq_enabled)
1160 return;
1161
Daniel Vetter59cdb632013-07-04 23:35:28 +02001162 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001163 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001164 I915_WRITE_IMR(ring, ~0);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03001165 snb_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001166 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001167 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001168}
1169
Ben Widawskyabd58f02013-11-02 21:07:09 -07001170static bool
1171gen8_ring_get_irq(struct intel_ring_buffer *ring)
1172{
1173 struct drm_device *dev = ring->dev;
1174 struct drm_i915_private *dev_priv = dev->dev_private;
1175 unsigned long flags;
1176
1177 if (!dev->irq_enabled)
1178 return false;
1179
1180 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1181 if (ring->irq_refcount++ == 0) {
1182 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1183 I915_WRITE_IMR(ring,
1184 ~(ring->irq_enable_mask |
1185 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1186 } else {
1187 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1188 }
1189 POSTING_READ(RING_IMR(ring->mmio_base));
1190 }
1191 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1192
1193 return true;
1194}
1195
1196static void
1197gen8_ring_put_irq(struct intel_ring_buffer *ring)
1198{
1199 struct drm_device *dev = ring->dev;
1200 struct drm_i915_private *dev_priv = dev->dev_private;
1201 unsigned long flags;
1202
1203 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1204 if (--ring->irq_refcount == 0) {
1205 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1206 I915_WRITE_IMR(ring,
1207 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1208 } else {
1209 I915_WRITE_IMR(ring, ~0);
1210 }
1211 POSTING_READ(RING_IMR(ring->mmio_base));
1212 }
1213 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1214}
1215
Zou Nan haid1b851f2010-05-21 09:08:57 +08001216static int
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001217i965_dispatch_execbuffer(struct intel_ring_buffer *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001218 u64 offset, u32 length,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001219 unsigned flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001220{
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001221 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001222
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001223 ret = intel_ring_begin(ring, 2);
1224 if (ret)
1225 return ret;
1226
Chris Wilson78501ea2010-10-27 12:18:21 +01001227 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001228 MI_BATCH_BUFFER_START |
1229 MI_BATCH_GTT |
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001230 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001231 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001232 intel_ring_advance(ring);
1233
Zou Nan haid1b851f2010-05-21 09:08:57 +08001234 return 0;
1235}
1236
Daniel Vetterb45305f2012-12-17 16:21:27 +01001237/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1238#define I830_BATCH_LIMIT (256*1024)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001239static int
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001240i830_dispatch_execbuffer(struct intel_ring_buffer *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001241 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001242 unsigned flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001243{
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001244 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001245
Daniel Vetterb45305f2012-12-17 16:21:27 +01001246 if (flags & I915_DISPATCH_PINNED) {
1247 ret = intel_ring_begin(ring, 4);
1248 if (ret)
1249 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001250
Daniel Vetterb45305f2012-12-17 16:21:27 +01001251 intel_ring_emit(ring, MI_BATCH_BUFFER);
1252 intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1253 intel_ring_emit(ring, offset + len - 8);
1254 intel_ring_emit(ring, MI_NOOP);
1255 intel_ring_advance(ring);
1256 } else {
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001257 u32 cs_offset = ring->scratch.gtt_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001258
1259 if (len > I830_BATCH_LIMIT)
1260 return -ENOSPC;
1261
1262 ret = intel_ring_begin(ring, 9+3);
1263 if (ret)
1264 return ret;
1265 /* Blit the batch (which has now all relocs applied) to the stable batch
1266 * scratch bo area (so that the CS never stumbles over its tlb
1267 * invalidation bug) ... */
1268 intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
1269 XY_SRC_COPY_BLT_WRITE_ALPHA |
1270 XY_SRC_COPY_BLT_WRITE_RGB);
1271 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
1272 intel_ring_emit(ring, 0);
1273 intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
1274 intel_ring_emit(ring, cs_offset);
1275 intel_ring_emit(ring, 0);
1276 intel_ring_emit(ring, 4096);
1277 intel_ring_emit(ring, offset);
1278 intel_ring_emit(ring, MI_FLUSH);
1279
1280 /* ... and execute it. */
1281 intel_ring_emit(ring, MI_BATCH_BUFFER);
1282 intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1283 intel_ring_emit(ring, cs_offset + len - 8);
1284 intel_ring_advance(ring);
1285 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001286
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001287 return 0;
1288}
1289
1290static int
1291i915_dispatch_execbuffer(struct intel_ring_buffer *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001292 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001293 unsigned flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001294{
1295 int ret;
1296
1297 ret = intel_ring_begin(ring, 2);
1298 if (ret)
1299 return ret;
1300
Chris Wilson65f56872012-04-17 16:38:12 +01001301 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001302 intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001303 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001304
Eric Anholt62fdfea2010-05-21 13:26:39 -07001305 return 0;
1306}
1307
Chris Wilson78501ea2010-10-27 12:18:21 +01001308static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001309{
Chris Wilson05394f32010-11-08 19:18:58 +00001310 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001311
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001312 obj = ring->status_page.obj;
1313 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001314 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001315
Chris Wilson9da3da62012-06-01 15:20:22 +01001316 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001317 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001318 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001319 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001320}
1321
Chris Wilson78501ea2010-10-27 12:18:21 +01001322static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001323{
Chris Wilson05394f32010-11-08 19:18:58 +00001324 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001325
Chris Wilsone3efda42014-04-09 09:19:41 +01001326 if ((obj = ring->status_page.obj) == NULL) {
1327 int ret;
1328
1329 obj = i915_gem_alloc_object(ring->dev, 4096);
1330 if (obj == NULL) {
1331 DRM_ERROR("Failed to allocate status page\n");
1332 return -ENOMEM;
1333 }
1334
1335 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1336 if (ret)
1337 goto err_unref;
1338
1339 ret = i915_gem_obj_ggtt_pin(obj, 4096, 0);
1340 if (ret) {
1341err_unref:
1342 drm_gem_object_unreference(&obj->base);
1343 return ret;
1344 }
1345
1346 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001347 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001348
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001349 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001350 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001351 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001352
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001353 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1354 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001355
1356 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001357}
1358
Daniel Vetter035dc1e2013-07-03 12:56:54 +02001359static int init_phys_status_page(struct intel_ring_buffer *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001360{
1361 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001362
1363 if (!dev_priv->status_page_dmah) {
1364 dev_priv->status_page_dmah =
1365 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1366 if (!dev_priv->status_page_dmah)
1367 return -ENOMEM;
1368 }
1369
Chris Wilson6b8294a2012-11-16 11:43:20 +00001370 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1371 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1372
1373 return 0;
1374}
1375
Chris Wilsone3efda42014-04-09 09:19:41 +01001376static int allocate_ring_buffer(struct intel_ring_buffer *ring)
1377{
1378 struct drm_device *dev = ring->dev;
1379 struct drm_i915_private *dev_priv = to_i915(dev);
1380 struct drm_i915_gem_object *obj;
1381 int ret;
1382
1383 if (ring->obj)
1384 return 0;
1385
1386 obj = NULL;
1387 if (!HAS_LLC(dev))
1388 obj = i915_gem_object_create_stolen(dev, ring->size);
1389 if (obj == NULL)
1390 obj = i915_gem_alloc_object(dev, ring->size);
1391 if (obj == NULL)
1392 return -ENOMEM;
1393
1394 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1395 if (ret)
1396 goto err_unref;
1397
1398 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1399 if (ret)
1400 goto err_unpin;
1401
1402 ring->virtual_start =
1403 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
1404 ring->size);
1405 if (ring->virtual_start == NULL) {
1406 ret = -EINVAL;
1407 goto err_unpin;
1408 }
1409
1410 ring->obj = obj;
1411 return 0;
1412
1413err_unpin:
1414 i915_gem_object_ggtt_unpin(obj);
1415err_unref:
1416 drm_gem_object_unreference(&obj->base);
1417 return ret;
1418}
1419
Ben Widawskyc43b5632012-04-16 14:07:40 -07001420static int intel_init_ring_buffer(struct drm_device *dev,
1421 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001422{
Chris Wilsondd785e32010-08-07 11:01:34 +01001423 int ret;
1424
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001425 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01001426 INIT_LIST_HEAD(&ring->active_list);
1427 INIT_LIST_HEAD(&ring->request_list);
Daniel Vetterdfc9ef22012-04-11 22:12:47 +02001428 ring->size = 32 * PAGE_SIZE;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001429 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001430
Chris Wilsonb259f672011-03-29 13:19:09 +01001431 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001432
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001433 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001434 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001435 if (ret)
1436 return ret;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001437 } else {
1438 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02001439 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001440 if (ret)
1441 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001442 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001443
Chris Wilsone3efda42014-04-09 09:19:41 +01001444 ret = allocate_ring_buffer(ring);
1445 if (ret) {
1446 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n", ring->name, ret);
1447 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001448 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001449
Chris Wilson55249ba2010-12-22 14:04:47 +00001450 /* Workaround an erratum on the i830 which causes a hang if
1451 * the TAIL pointer points to within the last 2 cachelines
1452 * of the buffer.
1453 */
1454 ring->effective_size = ring->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01001455 if (IS_I830(dev) || IS_845G(dev))
Chris Wilson18393f62014-04-09 09:19:40 +01001456 ring->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00001457
Brad Volkin351e3db2014-02-18 10:15:46 -08001458 i915_cmd_parser_init_ring(ring);
1459
Chris Wilsone3efda42014-04-09 09:19:41 +01001460 return ring->init(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001461}
1462
Chris Wilson78501ea2010-10-27 12:18:21 +01001463void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001464{
Chris Wilsone3efda42014-04-09 09:19:41 +01001465 struct drm_i915_private *dev_priv = to_i915(ring->dev);
Chris Wilson33626e62010-10-29 16:18:36 +01001466
Chris Wilson05394f32010-11-08 19:18:58 +00001467 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001468 return;
1469
Chris Wilsone3efda42014-04-09 09:19:41 +01001470 intel_stop_ring_buffer(ring);
1471 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01001472
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001473 iounmap(ring->virtual_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001474
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001475 i915_gem_object_ggtt_unpin(ring->obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001476 drm_gem_object_unreference(&ring->obj->base);
1477 ring->obj = NULL;
Ben Widawsky3d57e5b2013-10-14 10:01:36 -07001478 ring->preallocated_lazy_request = NULL;
1479 ring->outstanding_lazy_seqno = 0;
Chris Wilson78501ea2010-10-27 12:18:21 +01001480
Zou Nan hai8d192152010-11-02 16:31:01 +08001481 if (ring->cleanup)
1482 ring->cleanup(ring);
1483
Chris Wilson78501ea2010-10-27 12:18:21 +01001484 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001485}
1486
Chris Wilsona71d8d92012-02-15 11:25:36 +00001487static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
1488{
1489 struct drm_i915_gem_request *request;
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001490 u32 seqno = 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001491 int ret;
1492
Chris Wilsona71d8d92012-02-15 11:25:36 +00001493 if (ring->last_retired_head != -1) {
1494 ring->head = ring->last_retired_head;
1495 ring->last_retired_head = -1;
Chris Wilson1f709992014-01-27 22:43:07 +00001496
Chris Wilsona71d8d92012-02-15 11:25:36 +00001497 ring->space = ring_space(ring);
1498 if (ring->space >= n)
1499 return 0;
1500 }
1501
1502 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001503 if (__ring_space(request->tail, ring->tail, ring->size) >= n) {
Chris Wilsona71d8d92012-02-15 11:25:36 +00001504 seqno = request->seqno;
1505 break;
1506 }
Chris Wilsona71d8d92012-02-15 11:25:36 +00001507 }
1508
1509 if (seqno == 0)
1510 return -ENOSPC;
1511
Chris Wilson1f709992014-01-27 22:43:07 +00001512 ret = i915_wait_seqno(ring, seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001513 if (ret)
1514 return ret;
1515
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001516 i915_gem_retire_requests_ring(ring);
1517 ring->head = ring->last_retired_head;
1518 ring->last_retired_head = -1;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001519
Chris Wilson1cf0ba12014-05-05 09:07:33 +01001520 ring->space = ring_space(ring);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001521 return 0;
1522}
1523
Chris Wilson3e960502012-11-27 16:22:54 +00001524static int ring_wait_for_space(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001525{
Chris Wilson78501ea2010-10-27 12:18:21 +01001526 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001527 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001528 unsigned long end;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001529 int ret;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001530
Chris Wilsona71d8d92012-02-15 11:25:36 +00001531 ret = intel_ring_wait_request(ring, n);
1532 if (ret != -ENOSPC)
1533 return ret;
1534
Chris Wilson09246732013-08-10 22:16:32 +01001535 /* force the tail write in case we have been skipping them */
1536 __intel_ring_advance(ring);
1537
Daniel Vetter63ed2cb2012-04-23 16:50:50 +02001538 /* With GEM the hangcheck timer should kick us out of the loop,
1539 * leaving it early runs the risk of corrupting GEM state (due
1540 * to running on almost untested codepaths). But on resume
1541 * timers don't work yet, so prevent a complete hang in that
1542 * case by choosing an insanely large timeout. */
1543 end = jiffies + 60 * HZ;
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001544
Chris Wilsondcfe0502014-05-05 09:07:32 +01001545 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001546 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001547 ring->head = I915_READ_HEAD(ring);
1548 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001549 if (ring->space >= n) {
Chris Wilsondcfe0502014-05-05 09:07:32 +01001550 ret = 0;
1551 break;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001552 }
1553
Daniel Vetterfb19e2a2014-02-12 23:44:34 +01001554 if (!drm_core_check_feature(dev, DRIVER_MODESET) &&
1555 dev->primary->master) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001556 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1557 if (master_priv->sarea_priv)
1558 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1559 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001560
Chris Wilsone60a0b12010-10-13 10:09:14 +01001561 msleep(1);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001562
Chris Wilsondcfe0502014-05-05 09:07:32 +01001563 if (dev_priv->mm.interruptible && signal_pending(current)) {
1564 ret = -ERESTARTSYS;
1565 break;
1566 }
1567
Daniel Vetter33196de2012-11-14 17:14:05 +01001568 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1569 dev_priv->mm.interruptible);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001570 if (ret)
Chris Wilsondcfe0502014-05-05 09:07:32 +01001571 break;
1572
1573 if (time_after(jiffies, end)) {
1574 ret = -EBUSY;
1575 break;
1576 }
1577 } while (1);
Chris Wilsondb53a302011-02-03 11:57:46 +00001578 trace_i915_ring_wait_end(ring);
Chris Wilsondcfe0502014-05-05 09:07:32 +01001579 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001580}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001581
Chris Wilson3e960502012-11-27 16:22:54 +00001582static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
1583{
1584 uint32_t __iomem *virt;
1585 int rem = ring->size - ring->tail;
1586
1587 if (ring->space < rem) {
1588 int ret = ring_wait_for_space(ring, rem);
1589 if (ret)
1590 return ret;
1591 }
1592
1593 virt = ring->virtual_start + ring->tail;
1594 rem /= 4;
1595 while (rem--)
1596 iowrite32(MI_NOOP, virt++);
1597
1598 ring->tail = 0;
1599 ring->space = ring_space(ring);
1600
1601 return 0;
1602}
1603
1604int intel_ring_idle(struct intel_ring_buffer *ring)
1605{
1606 u32 seqno;
1607 int ret;
1608
1609 /* We need to add any requests required to flush the objects and ring */
Chris Wilson18235212013-09-04 10:45:51 +01001610 if (ring->outstanding_lazy_seqno) {
Mika Kuoppala0025c072013-06-12 12:35:30 +03001611 ret = i915_add_request(ring, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00001612 if (ret)
1613 return ret;
1614 }
1615
1616 /* Wait upon the last request to be completed */
1617 if (list_empty(&ring->request_list))
1618 return 0;
1619
1620 seqno = list_entry(ring->request_list.prev,
1621 struct drm_i915_gem_request,
1622 list)->seqno;
1623
1624 return i915_wait_seqno(ring, seqno);
1625}
1626
Chris Wilson9d7730912012-11-27 16:22:52 +00001627static int
1628intel_ring_alloc_seqno(struct intel_ring_buffer *ring)
1629{
Chris Wilson18235212013-09-04 10:45:51 +01001630 if (ring->outstanding_lazy_seqno)
Chris Wilson9d7730912012-11-27 16:22:52 +00001631 return 0;
1632
Chris Wilson3c0e2342013-09-04 10:45:52 +01001633 if (ring->preallocated_lazy_request == NULL) {
1634 struct drm_i915_gem_request *request;
1635
1636 request = kmalloc(sizeof(*request), GFP_KERNEL);
1637 if (request == NULL)
1638 return -ENOMEM;
1639
1640 ring->preallocated_lazy_request = request;
1641 }
1642
Chris Wilson18235212013-09-04 10:45:51 +01001643 return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
Chris Wilson9d7730912012-11-27 16:22:52 +00001644}
1645
Chris Wilson304d6952014-01-02 14:32:35 +00001646static int __intel_ring_prepare(struct intel_ring_buffer *ring,
1647 int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001648{
1649 int ret;
1650
1651 if (unlikely(ring->tail + bytes > ring->effective_size)) {
1652 ret = intel_wrap_ring_buffer(ring);
1653 if (unlikely(ret))
1654 return ret;
1655 }
1656
1657 if (unlikely(ring->space < bytes)) {
1658 ret = ring_wait_for_space(ring, bytes);
1659 if (unlikely(ret))
1660 return ret;
1661 }
1662
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001663 return 0;
1664}
1665
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001666int intel_ring_begin(struct intel_ring_buffer *ring,
1667 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001668{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001669 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001670 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001671
Daniel Vetter33196de2012-11-14 17:14:05 +01001672 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1673 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02001674 if (ret)
1675 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00001676
Chris Wilson304d6952014-01-02 14:32:35 +00001677 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
1678 if (ret)
1679 return ret;
1680
Chris Wilson9d7730912012-11-27 16:22:52 +00001681 /* Preallocate the olr before touching the ring */
1682 ret = intel_ring_alloc_seqno(ring);
1683 if (ret)
1684 return ret;
1685
Chris Wilson304d6952014-01-02 14:32:35 +00001686 ring->space -= num_dwords * sizeof(uint32_t);
1687 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001688}
1689
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001690/* Align the ring tail to a cacheline boundary */
1691int intel_ring_cacheline_align(struct intel_ring_buffer *ring)
1692{
Chris Wilson18393f62014-04-09 09:19:40 +01001693 int num_dwords = (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001694 int ret;
1695
1696 if (num_dwords == 0)
1697 return 0;
1698
Chris Wilson18393f62014-04-09 09:19:40 +01001699 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001700 ret = intel_ring_begin(ring, num_dwords);
1701 if (ret)
1702 return ret;
1703
1704 while (num_dwords--)
1705 intel_ring_emit(ring, MI_NOOP);
1706
1707 intel_ring_advance(ring);
1708
1709 return 0;
1710}
1711
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001712void intel_ring_init_seqno(struct intel_ring_buffer *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001713{
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001714 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001715
Chris Wilson18235212013-09-04 10:45:51 +01001716 BUG_ON(ring->outstanding_lazy_seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001717
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001718 if (INTEL_INFO(ring->dev)->gen >= 6) {
1719 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
1720 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Ben Widawsky50201502013-08-12 16:53:03 -07001721 if (HAS_VEBOX(ring->dev))
1722 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01001723 }
Chris Wilson297b0c52010-10-22 17:02:41 +01001724
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02001725 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03001726 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01001727}
1728
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001729static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
1730 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001731{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001732 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001733
1734 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001735
Chris Wilson12f55812012-07-05 17:14:01 +01001736 /* Disable notification that the ring is IDLE. The GT
1737 * will then assume that it is busy and bring it out of rc6.
1738 */
1739 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1740 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1741
1742 /* Clear the context id. Here be magic! */
1743 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1744
1745 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04001746 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01001747 GEN6_BSD_SLEEP_INDICATOR) == 0,
1748 50))
1749 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001750
Chris Wilson12f55812012-07-05 17:14:01 +01001751 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04001752 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01001753 POSTING_READ(RING_TAIL(ring->mmio_base));
1754
1755 /* Let the ring send IDLE messages to the GT again,
1756 * and so let it sleep to conserve power when idle.
1757 */
Akshay Joshi0206e352011-08-16 15:34:10 -04001758 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01001759 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001760}
1761
Ben Widawskyea251322013-05-28 19:22:21 -07001762static int gen6_bsd_ring_flush(struct intel_ring_buffer *ring,
1763 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001764{
Chris Wilson71a77e02011-02-02 12:13:49 +00001765 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001766 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001767
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001768 ret = intel_ring_begin(ring, 4);
1769 if (ret)
1770 return ret;
1771
Chris Wilson71a77e02011-02-02 12:13:49 +00001772 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001773 if (INTEL_INFO(ring->dev)->gen >= 8)
1774 cmd += 1;
Jesse Barnes9a289772012-10-26 09:42:42 -07001775 /*
1776 * Bspec vol 1c.5 - video engine command streamer:
1777 * "If ENABLED, all TLBs will be invalidated once the flush
1778 * operation is complete. This bit is only valid when the
1779 * Post-Sync Operation field is a value of 1h or 3h."
1780 */
Chris Wilson71a77e02011-02-02 12:13:49 +00001781 if (invalidate & I915_GEM_GPU_DOMAINS)
Jesse Barnes9a289772012-10-26 09:42:42 -07001782 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1783 MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
Chris Wilson71a77e02011-02-02 12:13:49 +00001784 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07001785 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001786 if (INTEL_INFO(ring->dev)->gen >= 8) {
1787 intel_ring_emit(ring, 0); /* upper addr */
1788 intel_ring_emit(ring, 0); /* value */
1789 } else {
1790 intel_ring_emit(ring, 0);
1791 intel_ring_emit(ring, MI_NOOP);
1792 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001793 intel_ring_advance(ring);
1794 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001795}
1796
1797static int
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001798gen8_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001799 u64 offset, u32 len,
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001800 unsigned flags)
1801{
Ben Widawsky28cf5412013-11-02 21:07:26 -07001802 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1803 bool ppgtt = dev_priv->mm.aliasing_ppgtt != NULL &&
1804 !(flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001805 int ret;
1806
1807 ret = intel_ring_begin(ring, 4);
1808 if (ret)
1809 return ret;
1810
1811 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07001812 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001813 intel_ring_emit(ring, lower_32_bits(offset));
1814 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001815 intel_ring_emit(ring, MI_NOOP);
1816 intel_ring_advance(ring);
1817
1818 return 0;
1819}
1820
1821static int
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001822hsw_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001823 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001824 unsigned flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001825{
Akshay Joshi0206e352011-08-16 15:34:10 -04001826 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001827
Akshay Joshi0206e352011-08-16 15:34:10 -04001828 ret = intel_ring_begin(ring, 2);
1829 if (ret)
1830 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001831
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001832 intel_ring_emit(ring,
1833 MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
1834 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
1835 /* bit0-7 is the length on GEN6+ */
1836 intel_ring_emit(ring, offset);
1837 intel_ring_advance(ring);
1838
1839 return 0;
1840}
1841
1842static int
1843gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001844 u64 offset, u32 len,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001845 unsigned flags)
1846{
1847 int ret;
1848
1849 ret = intel_ring_begin(ring, 2);
1850 if (ret)
1851 return ret;
1852
1853 intel_ring_emit(ring,
1854 MI_BATCH_BUFFER_START |
1855 (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04001856 /* bit0-7 is the length on GEN6+ */
1857 intel_ring_emit(ring, offset);
1858 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001859
Akshay Joshi0206e352011-08-16 15:34:10 -04001860 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001861}
1862
Chris Wilson549f7362010-10-19 11:19:32 +01001863/* Blitter support (SandyBridge+) */
1864
Ben Widawskyea251322013-05-28 19:22:21 -07001865static int gen6_ring_flush(struct intel_ring_buffer *ring,
1866 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001867{
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001868 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00001869 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001870 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001871
Daniel Vetter6a233c72011-12-14 13:57:07 +01001872 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001873 if (ret)
1874 return ret;
1875
Chris Wilson71a77e02011-02-02 12:13:49 +00001876 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001877 if (INTEL_INFO(ring->dev)->gen >= 8)
1878 cmd += 1;
Jesse Barnes9a289772012-10-26 09:42:42 -07001879 /*
1880 * Bspec vol 1c.3 - blitter engine command streamer:
1881 * "If ENABLED, all TLBs will be invalidated once the flush
1882 * operation is complete. This bit is only valid when the
1883 * Post-Sync Operation field is a value of 1h or 3h."
1884 */
Chris Wilson71a77e02011-02-02 12:13:49 +00001885 if (invalidate & I915_GEM_DOMAIN_RENDER)
Jesse Barnes9a289772012-10-26 09:42:42 -07001886 cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
Daniel Vetterb3fcabb2012-11-04 12:24:47 +01001887 MI_FLUSH_DW_OP_STOREDW;
Chris Wilson71a77e02011-02-02 12:13:49 +00001888 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07001889 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07001890 if (INTEL_INFO(ring->dev)->gen >= 8) {
1891 intel_ring_emit(ring, 0); /* upper addr */
1892 intel_ring_emit(ring, 0); /* value */
1893 } else {
1894 intel_ring_emit(ring, 0);
1895 intel_ring_emit(ring, MI_NOOP);
1896 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001897 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001898
Ville Syrjälä9688eca2013-11-06 23:02:19 +02001899 if (IS_GEN7(dev) && !invalidate && flush)
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001900 return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
1901
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001902 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001903}
1904
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001905int intel_init_render_ring_buffer(struct drm_device *dev)
1906{
Jani Nikula4640c4f2014-03-31 14:27:19 +03001907 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001908 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001909
Daniel Vetter59465b52012-04-11 22:12:48 +02001910 ring->name = "render ring";
1911 ring->id = RCS;
1912 ring->mmio_base = RENDER_RING_BASE;
1913
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001914 if (INTEL_INFO(dev)->gen >= 6) {
1915 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03001916 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01001917 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03001918 ring->flush = gen6_render_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001919 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawskya5f3d682013-11-02 21:07:27 -07001920 ring->flush = gen8_render_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001921 ring->irq_get = gen8_ring_get_irq;
1922 ring->irq_put = gen8_ring_put_irq;
1923 } else {
1924 ring->irq_get = gen6_ring_get_irq;
1925 ring->irq_put = gen6_ring_put_irq;
1926 }
Ben Widawskycc609d52013-05-28 19:22:29 -07001927 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001928 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001929 ring->set_seqno = ring_set_seqno;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001930 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07001931 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08001932 /*
1933 * The current semaphore is only applied on pre-gen8 platform.
1934 * And there is no VCS2 ring on the pre-gen8 platform. So the
1935 * semaphore between RCS and VCS2 is initialized as INVALID.
1936 * Gen8 will initialize the sema between VCS2 and RCS later.
1937 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07001938 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
1939 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
1940 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
1941 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
1942 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
1943 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
1944 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
1945 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
1946 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
1947 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001948 } else if (IS_GEN5(dev)) {
1949 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001950 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001951 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001952 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02001953 ring->irq_get = gen5_ring_get_irq;
1954 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07001955 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
1956 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02001957 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02001958 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001959 if (INTEL_INFO(dev)->gen < 4)
1960 ring->flush = gen2_render_ring_flush;
1961 else
1962 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02001963 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001964 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001965 if (IS_GEN2(dev)) {
1966 ring->irq_get = i8xx_ring_get_irq;
1967 ring->irq_put = i8xx_ring_put_irq;
1968 } else {
1969 ring->irq_get = i9xx_ring_get_irq;
1970 ring->irq_put = i9xx_ring_put_irq;
1971 }
Daniel Vettere3670312012-04-11 22:12:53 +02001972 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001973 }
Daniel Vetter59465b52012-04-11 22:12:48 +02001974 ring->write_tail = ring_write_tail;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001975 if (IS_HASWELL(dev))
1976 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001977 else if (IS_GEN8(dev))
1978 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001979 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001980 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1981 else if (INTEL_INFO(dev)->gen >= 4)
1982 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1983 else if (IS_I830(dev) || IS_845G(dev))
1984 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1985 else
1986 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02001987 ring->init = init_render_ring;
1988 ring->cleanup = render_ring_cleanup;
1989
Daniel Vetterb45305f2012-12-17 16:21:27 +01001990 /* Workaround batchbuffer to combat CS tlb bug. */
1991 if (HAS_BROKEN_CS_TLB(dev)) {
1992 struct drm_i915_gem_object *obj;
1993 int ret;
1994
1995 obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
1996 if (obj == NULL) {
1997 DRM_ERROR("Failed to allocate batch bo\n");
1998 return -ENOMEM;
1999 }
2000
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002001 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002002 if (ret != 0) {
2003 drm_gem_object_unreference(&obj->base);
2004 DRM_ERROR("Failed to ping batch bo\n");
2005 return ret;
2006 }
2007
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002008 ring->scratch.obj = obj;
2009 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002010 }
2011
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002012 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002013}
2014
Chris Wilsone8616b62011-01-20 09:57:11 +00002015int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
2016{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002017 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone8616b62011-01-20 09:57:11 +00002018 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson6b8294a2012-11-16 11:43:20 +00002019 int ret;
Chris Wilsone8616b62011-01-20 09:57:11 +00002020
Daniel Vetter59465b52012-04-11 22:12:48 +02002021 ring->name = "render ring";
2022 ring->id = RCS;
2023 ring->mmio_base = RENDER_RING_BASE;
2024
Chris Wilsone8616b62011-01-20 09:57:11 +00002025 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetterb4178f82012-04-11 22:12:51 +02002026 /* non-kms not supported on gen6+ */
2027 return -ENODEV;
Chris Wilsone8616b62011-01-20 09:57:11 +00002028 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002029
2030 /* Note: gem is not supported on gen5/ilk without kms (the corresponding
2031 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
2032 * the special gen5 functions. */
2033 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002034 if (INTEL_INFO(dev)->gen < 4)
2035 ring->flush = gen2_render_ring_flush;
2036 else
2037 ring->flush = gen4_render_ring_flush;
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002038 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002039 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002040 if (IS_GEN2(dev)) {
2041 ring->irq_get = i8xx_ring_get_irq;
2042 ring->irq_put = i8xx_ring_put_irq;
2043 } else {
2044 ring->irq_get = i9xx_ring_get_irq;
2045 ring->irq_put = i9xx_ring_put_irq;
2046 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02002047 ring->irq_enable_mask = I915_USER_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002048 ring->write_tail = ring_write_tail;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002049 if (INTEL_INFO(dev)->gen >= 4)
2050 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2051 else if (IS_I830(dev) || IS_845G(dev))
2052 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2053 else
2054 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02002055 ring->init = init_render_ring;
2056 ring->cleanup = render_ring_cleanup;
Chris Wilsone8616b62011-01-20 09:57:11 +00002057
2058 ring->dev = dev;
2059 INIT_LIST_HEAD(&ring->active_list);
2060 INIT_LIST_HEAD(&ring->request_list);
Chris Wilsone8616b62011-01-20 09:57:11 +00002061
2062 ring->size = size;
2063 ring->effective_size = ring->size;
Mika Kuoppala17f10fd2012-10-29 16:59:26 +02002064 if (IS_I830(ring->dev) || IS_845G(ring->dev))
Chris Wilson18393f62014-04-09 09:19:40 +01002065 ring->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilsone8616b62011-01-20 09:57:11 +00002066
Daniel Vetter4225d0f2012-04-26 23:28:16 +02002067 ring->virtual_start = ioremap_wc(start, size);
2068 if (ring->virtual_start == NULL) {
Chris Wilsone8616b62011-01-20 09:57:11 +00002069 DRM_ERROR("can not ioremap virtual address for"
2070 " ring buffer\n");
2071 return -ENOMEM;
2072 }
2073
Chris Wilson6b8294a2012-11-16 11:43:20 +00002074 if (!I915_NEED_GFX_HWS(dev)) {
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002075 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002076 if (ret)
2077 return ret;
2078 }
2079
Chris Wilsone8616b62011-01-20 09:57:11 +00002080 return 0;
2081}
2082
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002083int intel_init_bsd_ring_buffer(struct drm_device *dev)
2084{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002085 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002086 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002087
Daniel Vetter58fa3832012-04-11 22:12:49 +02002088 ring->name = "bsd ring";
2089 ring->id = VCS;
2090
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002091 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002092 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002093 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002094 /* gen6 bsd needs a special wa for tail updates */
2095 if (IS_GEN6(dev))
2096 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002097 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002098 ring->add_request = gen6_add_request;
2099 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002100 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002101 if (INTEL_INFO(dev)->gen >= 8) {
2102 ring->irq_enable_mask =
2103 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2104 ring->irq_get = gen8_ring_get_irq;
2105 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002106 ring->dispatch_execbuffer =
2107 gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002108 } else {
2109 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2110 ring->irq_get = gen6_ring_get_irq;
2111 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002112 ring->dispatch_execbuffer =
2113 gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002114 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002115 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002116 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002117 /*
2118 * The current semaphore is only applied on pre-gen8 platform.
2119 * And there is no VCS2 ring on the pre-gen8 platform. So the
2120 * semaphore between VCS and VCS2 is initialized as INVALID.
2121 * Gen8 will initialize the sema between VCS2 and VCS later.
2122 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002123 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2124 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2125 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2126 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2127 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2128 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2129 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2130 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2131 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2132 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002133 } else {
2134 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002135 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002136 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002137 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002138 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002139 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002140 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002141 ring->irq_get = gen5_ring_get_irq;
2142 ring->irq_put = gen5_ring_put_irq;
2143 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002144 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002145 ring->irq_get = i9xx_ring_get_irq;
2146 ring->irq_put = i9xx_ring_put_irq;
2147 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002148 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002149 }
2150 ring->init = init_ring_common;
2151
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002152 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002153}
Chris Wilson549f7362010-10-19 11:19:32 +01002154
Zhao Yakui845f74a2014-04-17 10:37:37 +08002155/**
2156 * Initialize the second BSD ring for Broadwell GT3.
2157 * It is noted that this only exists on Broadwell GT3.
2158 */
2159int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2160{
2161 struct drm_i915_private *dev_priv = dev->dev_private;
2162 struct intel_ring_buffer *ring = &dev_priv->ring[VCS2];
2163
2164 if ((INTEL_INFO(dev)->gen != 8)) {
2165 DRM_ERROR("No dual-BSD ring on non-BDW machine\n");
2166 return -EINVAL;
2167 }
2168
2169 ring->name = "bds2_ring";
2170 ring->id = VCS2;
2171
2172 ring->write_tail = ring_write_tail;
2173 ring->mmio_base = GEN8_BSD2_RING_BASE;
2174 ring->flush = gen6_bsd_ring_flush;
2175 ring->add_request = gen6_add_request;
2176 ring->get_seqno = gen6_ring_get_seqno;
2177 ring->set_seqno = ring_set_seqno;
2178 ring->irq_enable_mask =
2179 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2180 ring->irq_get = gen8_ring_get_irq;
2181 ring->irq_put = gen8_ring_put_irq;
2182 ring->dispatch_execbuffer =
2183 gen8_ring_dispatch_execbuffer;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002184 ring->semaphore.sync_to = gen6_ring_sync;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002185 /*
2186 * The current semaphore is only applied on the pre-gen8. And there
2187 * is no bsd2 ring on the pre-gen8. So now the semaphore_register
2188 * between VCS2 and other ring is initialized as invalid.
2189 * Gen8 will initialize the sema between VCS2 and other ring later.
2190 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002191 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2192 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2193 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2194 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2195 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2196 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2197 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2198 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2199 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2200 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002201
2202 ring->init = init_ring_common;
2203
2204 return intel_init_ring_buffer(dev, ring);
2205}
2206
Chris Wilson549f7362010-10-19 11:19:32 +01002207int intel_init_blt_ring_buffer(struct drm_device *dev)
2208{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002209 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002210 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002211
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002212 ring->name = "blitter ring";
2213 ring->id = BCS;
2214
2215 ring->mmio_base = BLT_RING_BASE;
2216 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002217 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002218 ring->add_request = gen6_add_request;
2219 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002220 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002221 if (INTEL_INFO(dev)->gen >= 8) {
2222 ring->irq_enable_mask =
2223 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2224 ring->irq_get = gen8_ring_get_irq;
2225 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002226 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002227 } else {
2228 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2229 ring->irq_get = gen6_ring_get_irq;
2230 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002231 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002232 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002233 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002234 ring->semaphore.signal = gen6_signal;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002235 /*
2236 * The current semaphore is only applied on pre-gen8 platform. And
2237 * there is no VCS2 ring on the pre-gen8 platform. So the semaphore
2238 * between BCS and VCS2 is initialized as INVALID.
2239 * Gen8 will initialize the sema between BCS and VCS2 later.
2240 */
Ben Widawskyebc348b2014-04-29 14:52:28 -07002241 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2242 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2243 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2244 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2245 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2246 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2247 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2248 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2249 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2250 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002251 ring->init = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002252
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002253 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002254}
Chris Wilsona7b97612012-07-20 12:41:08 +01002255
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002256int intel_init_vebox_ring_buffer(struct drm_device *dev)
2257{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002258 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002259 struct intel_ring_buffer *ring = &dev_priv->ring[VECS];
2260
2261 ring->name = "video enhancement ring";
2262 ring->id = VECS;
2263
2264 ring->mmio_base = VEBOX_RING_BASE;
2265 ring->write_tail = ring_write_tail;
2266 ring->flush = gen6_ring_flush;
2267 ring->add_request = gen6_add_request;
2268 ring->get_seqno = gen6_ring_get_seqno;
2269 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002270
2271 if (INTEL_INFO(dev)->gen >= 8) {
2272 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002273 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002274 ring->irq_get = gen8_ring_get_irq;
2275 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002276 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002277 } else {
2278 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2279 ring->irq_get = hsw_vebox_get_irq;
2280 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002281 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002282 }
Ben Widawskyebc348b2014-04-29 14:52:28 -07002283 ring->semaphore.sync_to = gen6_ring_sync;
Ben Widawsky78325f22014-04-29 14:52:29 -07002284 ring->semaphore.signal = gen6_signal;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002285 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2286 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2287 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2288 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2289 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2290 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2291 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2292 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2293 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2294 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002295 ring->init = init_ring_common;
2296
2297 return intel_init_ring_buffer(dev, ring);
2298}
2299
Chris Wilsona7b97612012-07-20 12:41:08 +01002300int
2301intel_ring_flush_all_caches(struct intel_ring_buffer *ring)
2302{
2303 int ret;
2304
2305 if (!ring->gpu_caches_dirty)
2306 return 0;
2307
2308 ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2309 if (ret)
2310 return ret;
2311
2312 trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2313
2314 ring->gpu_caches_dirty = false;
2315 return 0;
2316}
2317
2318int
2319intel_ring_invalidate_all_caches(struct intel_ring_buffer *ring)
2320{
2321 uint32_t flush_domains;
2322 int ret;
2323
2324 flush_domains = 0;
2325 if (ring->gpu_caches_dirty)
2326 flush_domains = I915_GEM_GPU_DOMAINS;
2327
2328 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2329 if (ret)
2330 return ret;
2331
2332 trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2333
2334 ring->gpu_caches_dirty = false;
2335 return 0;
2336}
Chris Wilsone3efda42014-04-09 09:19:41 +01002337
2338void
2339intel_stop_ring_buffer(struct intel_ring_buffer *ring)
2340{
2341 int ret;
2342
2343 if (!intel_ring_initialized(ring))
2344 return;
2345
2346 ret = intel_ring_idle(ring);
2347 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2348 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2349 ring->name, ret);
2350
2351 stop_ring(ring);
2352}