xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision 5e269720fc70c6433960b2ce4ba8dde782a120d8)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2008-2010 Intel Corporation
3e3adcf8fSFrançois Tigeot  *
4e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10e3adcf8fSFrançois Tigeot  *
11e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13e3adcf8fSFrançois Tigeot  * Software.
14e3adcf8fSFrançois Tigeot  *
15e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e3adcf8fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e3adcf8fSFrançois Tigeot  * IN THE SOFTWARE.
22e3adcf8fSFrançois Tigeot  *
23e3adcf8fSFrançois Tigeot  * Authors:
24e3adcf8fSFrançois Tigeot  *    Eric Anholt <eric@anholt.net>
25e3adcf8fSFrançois Tigeot  *    Zou Nan hai <nanhai.zou@intel.com>
26e3adcf8fSFrançois Tigeot  *    Xiang Hai hao<haihao.xiang@intel.com>
27e3adcf8fSFrançois Tigeot  *
28e3adcf8fSFrançois Tigeot  */
29e3adcf8fSFrançois Tigeot 
3018e26a6dSFrançois Tigeot #include <drm/drmP.h>
31e3adcf8fSFrançois Tigeot #include "i915_drv.h"
32a2fdbec6SFrançois Tigeot #include <drm/i915_drm.h>
33a2fdbec6SFrançois Tigeot #include "i915_trace.h"
34e3adcf8fSFrançois Tigeot #include "intel_drv.h"
35e3adcf8fSFrançois Tigeot 
361b13d190SFrançois Tigeot bool
371b13d190SFrançois Tigeot intel_ring_initialized(struct intel_engine_cs *ring)
381b13d190SFrançois Tigeot {
391b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
40ba55f2f5SFrançois Tigeot 
411b13d190SFrançois Tigeot 	if (!dev)
421b13d190SFrançois Tigeot 		return false;
431b13d190SFrançois Tigeot 
441b13d190SFrançois Tigeot 	if (i915.enable_execlists) {
451b13d190SFrançois Tigeot 		struct intel_context *dctx = ring->default_context;
461b13d190SFrançois Tigeot 		struct intel_ringbuffer *ringbuf = dctx->engine[ring->id].ringbuf;
471b13d190SFrançois Tigeot 
481b13d190SFrançois Tigeot 		return ringbuf->obj;
491b13d190SFrançois Tigeot 	} else
501b13d190SFrançois Tigeot 		return ring->buffer && ring->buffer->obj;
511b13d190SFrançois Tigeot }
521b13d190SFrançois Tigeot 
531b13d190SFrançois Tigeot int __intel_ring_space(int head, int tail, int size)
54e3adcf8fSFrançois Tigeot {
552c9916cdSFrançois Tigeot 	int space = head - tail;
562c9916cdSFrançois Tigeot 	if (space <= 0)
57ba55f2f5SFrançois Tigeot 		space += size;
582c9916cdSFrançois Tigeot 	return space - I915_RING_FREE_SPACE;
592c9916cdSFrançois Tigeot }
602c9916cdSFrançois Tigeot 
612c9916cdSFrançois Tigeot void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
622c9916cdSFrançois Tigeot {
632c9916cdSFrançois Tigeot 	if (ringbuf->last_retired_head != -1) {
642c9916cdSFrançois Tigeot 		ringbuf->head = ringbuf->last_retired_head;
652c9916cdSFrançois Tigeot 		ringbuf->last_retired_head = -1;
662c9916cdSFrançois Tigeot 	}
672c9916cdSFrançois Tigeot 
682c9916cdSFrançois Tigeot 	ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
692c9916cdSFrançois Tigeot 					    ringbuf->tail, ringbuf->size);
70e3adcf8fSFrançois Tigeot }
71e3adcf8fSFrançois Tigeot 
721b13d190SFrançois Tigeot int intel_ring_space(struct intel_ringbuffer *ringbuf)
73ba55f2f5SFrançois Tigeot {
742c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
752c9916cdSFrançois Tigeot 	return ringbuf->space;
76ba55f2f5SFrançois Tigeot }
77ba55f2f5SFrançois Tigeot 
781b13d190SFrançois Tigeot bool intel_ring_stopped(struct intel_engine_cs *ring)
799edbd4a0SFrançois Tigeot {
809edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
81ba55f2f5SFrançois Tigeot 	return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
82ba55f2f5SFrançois Tigeot }
839edbd4a0SFrançois Tigeot 
84a05eeebfSFrançois Tigeot static void __intel_ring_advance(struct intel_engine_cs *ring)
85ba55f2f5SFrançois Tigeot {
86ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
87ba55f2f5SFrançois Tigeot 	ringbuf->tail &= ringbuf->size - 1;
88ba55f2f5SFrançois Tigeot 	if (intel_ring_stopped(ring))
899edbd4a0SFrançois Tigeot 		return;
90ba55f2f5SFrançois Tigeot 	ring->write_tail(ring, ringbuf->tail);
919edbd4a0SFrançois Tigeot }
929edbd4a0SFrançois Tigeot 
93e3adcf8fSFrançois Tigeot static int
94a05eeebfSFrançois Tigeot gen2_render_ring_flush(struct drm_i915_gem_request *req,
95686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
96686a02f1SFrançois Tigeot 		       u32	flush_domains)
97686a02f1SFrançois Tigeot {
98a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
99686a02f1SFrançois Tigeot 	u32 cmd;
100686a02f1SFrançois Tigeot 	int ret;
101686a02f1SFrançois Tigeot 
102686a02f1SFrançois Tigeot 	cmd = MI_FLUSH;
103686a02f1SFrançois Tigeot 	if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
104686a02f1SFrançois Tigeot 		cmd |= MI_NO_WRITE_FLUSH;
105686a02f1SFrançois Tigeot 
106686a02f1SFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
107686a02f1SFrançois Tigeot 		cmd |= MI_READ_FLUSH;
108686a02f1SFrançois Tigeot 
109a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
110686a02f1SFrançois Tigeot 	if (ret)
111686a02f1SFrançois Tigeot 		return ret;
112686a02f1SFrançois Tigeot 
113686a02f1SFrançois Tigeot 	intel_ring_emit(ring, cmd);
114686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
115686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
116686a02f1SFrançois Tigeot 
117686a02f1SFrançois Tigeot 	return 0;
118686a02f1SFrançois Tigeot }
119686a02f1SFrançois Tigeot 
120686a02f1SFrançois Tigeot static int
121a05eeebfSFrançois Tigeot gen4_render_ring_flush(struct drm_i915_gem_request *req,
122686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
123686a02f1SFrançois Tigeot 		       u32	flush_domains)
124e3adcf8fSFrançois Tigeot {
125a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
126e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
127686a02f1SFrançois Tigeot 	u32 cmd;
128e3adcf8fSFrançois Tigeot 	int ret;
129e3adcf8fSFrançois Tigeot 
130e3adcf8fSFrançois Tigeot 	/*
131e3adcf8fSFrançois Tigeot 	 * read/write caches:
132e3adcf8fSFrançois Tigeot 	 *
133e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
134e3adcf8fSFrançois Tigeot 	 * only flushed if MI_NO_WRITE_FLUSH is unset.  On 965, it is
135e3adcf8fSFrançois Tigeot 	 * also flushed at 2d versus 3d pipeline switches.
136e3adcf8fSFrançois Tigeot 	 *
137e3adcf8fSFrançois Tigeot 	 * read-only caches:
138e3adcf8fSFrançois Tigeot 	 *
139e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
140e3adcf8fSFrançois Tigeot 	 * MI_READ_FLUSH is set, and is always flushed on 965.
141e3adcf8fSFrançois Tigeot 	 *
142e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_COMMAND may not exist?
143e3adcf8fSFrançois Tigeot 	 *
144e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
145e3adcf8fSFrançois Tigeot 	 * invalidated when MI_EXE_FLUSH is set.
146e3adcf8fSFrançois Tigeot 	 *
147e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
148e3adcf8fSFrançois Tigeot 	 * invalidated with every MI_FLUSH.
149e3adcf8fSFrançois Tigeot 	 *
150e3adcf8fSFrançois Tigeot 	 * TLBs:
151e3adcf8fSFrançois Tigeot 	 *
152e3adcf8fSFrançois Tigeot 	 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
153e3adcf8fSFrançois Tigeot 	 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
154e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
155e3adcf8fSFrançois Tigeot 	 * are flushed at any MI_FLUSH.
156e3adcf8fSFrançois Tigeot 	 */
157e3adcf8fSFrançois Tigeot 
158e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
159686a02f1SFrançois Tigeot 	if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
160e3adcf8fSFrançois Tigeot 		cmd &= ~MI_NO_WRITE_FLUSH;
161e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
162e3adcf8fSFrançois Tigeot 		cmd |= MI_EXE_FLUSH;
163e3adcf8fSFrançois Tigeot 
164e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
165e3adcf8fSFrançois Tigeot 	    (IS_G4X(dev) || IS_GEN5(dev)))
166e3adcf8fSFrançois Tigeot 		cmd |= MI_INVALIDATE_ISP;
167e3adcf8fSFrançois Tigeot 
168a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
169e3adcf8fSFrançois Tigeot 	if (ret)
170e3adcf8fSFrançois Tigeot 		return ret;
171e3adcf8fSFrançois Tigeot 
172e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
173e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
174e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
175e3adcf8fSFrançois Tigeot 
176e3adcf8fSFrançois Tigeot 	return 0;
177e3adcf8fSFrançois Tigeot }
178e3adcf8fSFrançois Tigeot 
179e3adcf8fSFrançois Tigeot /**
180e3adcf8fSFrançois Tigeot  * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
181e3adcf8fSFrançois Tigeot  * implementing two workarounds on gen6.  From section 1.4.7.1
182e3adcf8fSFrançois Tigeot  * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
183e3adcf8fSFrançois Tigeot  *
184e3adcf8fSFrançois Tigeot  * [DevSNB-C+{W/A}] Before any depth stall flush (including those
185e3adcf8fSFrançois Tigeot  * produced by non-pipelined state commands), software needs to first
186e3adcf8fSFrançois Tigeot  * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
187e3adcf8fSFrançois Tigeot  * 0.
188e3adcf8fSFrançois Tigeot  *
189e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
190e3adcf8fSFrançois Tigeot  * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
191e3adcf8fSFrançois Tigeot  *
192e3adcf8fSFrançois Tigeot  * And the workaround for these two requires this workaround first:
193e3adcf8fSFrançois Tigeot  *
194e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
195e3adcf8fSFrançois Tigeot  * BEFORE the pipe-control with a post-sync op and no write-cache
196e3adcf8fSFrançois Tigeot  * flushes.
197e3adcf8fSFrançois Tigeot  *
198e3adcf8fSFrançois Tigeot  * And this last workaround is tricky because of the requirements on
199e3adcf8fSFrançois Tigeot  * that bit.  From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
200e3adcf8fSFrançois Tigeot  * volume 2 part 1:
201e3adcf8fSFrançois Tigeot  *
202e3adcf8fSFrançois Tigeot  *     "1 of the following must also be set:
203e3adcf8fSFrançois Tigeot  *      - Render Target Cache Flush Enable ([12] of DW1)
204e3adcf8fSFrançois Tigeot  *      - Depth Cache Flush Enable ([0] of DW1)
205e3adcf8fSFrançois Tigeot  *      - Stall at Pixel Scoreboard ([1] of DW1)
206e3adcf8fSFrançois Tigeot  *      - Depth Stall ([13] of DW1)
207e3adcf8fSFrançois Tigeot  *      - Post-Sync Operation ([13] of DW1)
208e3adcf8fSFrançois Tigeot  *      - Notify Enable ([8] of DW1)"
209e3adcf8fSFrançois Tigeot  *
210e3adcf8fSFrançois Tigeot  * The cache flushes require the workaround flush that triggered this
211e3adcf8fSFrançois Tigeot  * one, so we can't use it.  Depth stall would trigger the same.
212e3adcf8fSFrançois Tigeot  * Post-sync nonzero is what triggered this second workaround, so we
213e3adcf8fSFrançois Tigeot  * can't use that one either.  Notify enable is IRQs, which aren't
214e3adcf8fSFrançois Tigeot  * really our business.  That leaves only stall at scoreboard.
215e3adcf8fSFrançois Tigeot  */
216e3adcf8fSFrançois Tigeot static int
217a05eeebfSFrançois Tigeot intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
218e3adcf8fSFrançois Tigeot {
219a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
220ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
221e3adcf8fSFrançois Tigeot 	int ret;
222e3adcf8fSFrançois Tigeot 
223a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 6);
224e3adcf8fSFrançois Tigeot 	if (ret)
225e3adcf8fSFrançois Tigeot 		return ret;
226e3adcf8fSFrançois Tigeot 
227e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
228e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
229e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_STALL_AT_SCOREBOARD);
230e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
231e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* low dword */
232e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* high dword */
233e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
234e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
235e3adcf8fSFrançois Tigeot 
236a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 6);
237e3adcf8fSFrançois Tigeot 	if (ret)
238e3adcf8fSFrançois Tigeot 		return ret;
239e3adcf8fSFrançois Tigeot 
240e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
241e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
242e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
243e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
244e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
245e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
246e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
247e3adcf8fSFrançois Tigeot 
248e3adcf8fSFrançois Tigeot 	return 0;
249e3adcf8fSFrançois Tigeot }
250e3adcf8fSFrançois Tigeot 
251e3adcf8fSFrançois Tigeot static int
252a05eeebfSFrançois Tigeot gen6_render_ring_flush(struct drm_i915_gem_request *req,
253e3adcf8fSFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
254e3adcf8fSFrançois Tigeot {
255a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
256e3adcf8fSFrançois Tigeot 	u32 flags = 0;
257ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
258e3adcf8fSFrançois Tigeot 	int ret;
259e3adcf8fSFrançois Tigeot 
260e3adcf8fSFrançois Tigeot 	/* Force SNB workarounds for PIPE_CONTROL flushes */
261a05eeebfSFrançois Tigeot 	ret = intel_emit_post_sync_nonzero_flush(req);
262686a02f1SFrançois Tigeot 	if (ret)
263686a02f1SFrançois Tigeot 		return ret;
264e3adcf8fSFrançois Tigeot 
265e3adcf8fSFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
266e3adcf8fSFrançois Tigeot 	 * number of bits based on the write domains has little performance
267e3adcf8fSFrançois Tigeot 	 * impact.
268e3adcf8fSFrançois Tigeot 	 */
269b5c29a34SFrançois Tigeot 	if (flush_domains) {
270e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
271b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
272b5c29a34SFrançois Tigeot 		/*
273b5c29a34SFrançois Tigeot 		 * Ensure that any following seqno writes only happen
274b5c29a34SFrançois Tigeot 		 * when the render cache is indeed flushed.
275b5c29a34SFrançois Tigeot 		 */
276b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CS_STALL;
277b5c29a34SFrançois Tigeot 	}
278b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
279686a02f1SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
280e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
281e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
282e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
283e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
284e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
285686a02f1SFrançois Tigeot 		/*
286b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
287686a02f1SFrançois Tigeot 		 */
288b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
289b5c29a34SFrançois Tigeot 	}
290e3adcf8fSFrançois Tigeot 
291a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
292e3adcf8fSFrançois Tigeot 	if (ret)
293e3adcf8fSFrançois Tigeot 		return ret;
294e3adcf8fSFrançois Tigeot 
295b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
296e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, flags);
297e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
298b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
299b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
300b5c29a34SFrançois Tigeot 
301b5c29a34SFrançois Tigeot 	return 0;
302b5c29a34SFrançois Tigeot }
303b5c29a34SFrançois Tigeot 
304b5c29a34SFrançois Tigeot static int
305a05eeebfSFrançois Tigeot gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
306b5c29a34SFrançois Tigeot {
307a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
308b5c29a34SFrançois Tigeot 	int ret;
309b5c29a34SFrançois Tigeot 
310a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
311b5c29a34SFrançois Tigeot 	if (ret)
312b5c29a34SFrançois Tigeot 		return ret;
313b5c29a34SFrançois Tigeot 
314b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
315b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
316b5c29a34SFrançois Tigeot 			      PIPE_CONTROL_STALL_AT_SCOREBOARD);
317b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
318b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
319b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
320b5c29a34SFrançois Tigeot 
321b5c29a34SFrançois Tigeot 	return 0;
322b5c29a34SFrançois Tigeot }
323b5c29a34SFrançois Tigeot 
324b5c29a34SFrançois Tigeot static int
325a05eeebfSFrançois Tigeot gen7_render_ring_flush(struct drm_i915_gem_request *req,
326b5c29a34SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
327b5c29a34SFrançois Tigeot {
328a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
329b5c29a34SFrançois Tigeot 	u32 flags = 0;
330ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
331b5c29a34SFrançois Tigeot 	int ret;
332b5c29a34SFrançois Tigeot 
333b5c29a34SFrançois Tigeot 	/*
334b5c29a34SFrançois Tigeot 	 * Ensure that any following seqno writes only happen when the render
335b5c29a34SFrançois Tigeot 	 * cache is indeed flushed.
336b5c29a34SFrançois Tigeot 	 *
337b5c29a34SFrançois Tigeot 	 * Workaround: 4th PIPE_CONTROL command (except the ones with only
338b5c29a34SFrançois Tigeot 	 * read-cache invalidate bits set) must have the CS_STALL bit set. We
339b5c29a34SFrançois Tigeot 	 * don't try to be clever and just set it unconditionally.
340b5c29a34SFrançois Tigeot 	 */
341b5c29a34SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
342b5c29a34SFrançois Tigeot 
343b5c29a34SFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
344b5c29a34SFrançois Tigeot 	 * number of bits based on the write domains has little performance
345b5c29a34SFrançois Tigeot 	 * impact.
346b5c29a34SFrançois Tigeot 	 */
347b5c29a34SFrançois Tigeot 	if (flush_domains) {
348b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
349b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
350b49c8cf9SFrançois Tigeot 		flags |= PIPE_CONTROL_FLUSH_ENABLE;
351b5c29a34SFrançois Tigeot 	}
352b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
353b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
354b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
355b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
356b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
357b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
358b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
3592c9916cdSFrançois Tigeot 		flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
360b5c29a34SFrançois Tigeot 		/*
361b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
362b5c29a34SFrançois Tigeot 		 */
363b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
364a2fdbec6SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
365b5c29a34SFrançois Tigeot 
3660dbf0ea8SMatthew Dillon 		flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3670dbf0ea8SMatthew Dillon 
368b5c29a34SFrançois Tigeot 		/* Workaround: we must issue a pipe_control with CS-stall bit
369b5c29a34SFrançois Tigeot 		 * set before a pipe_control command that has the state cache
370b5c29a34SFrançois Tigeot 		 * invalidate bit set. */
371a05eeebfSFrançois Tigeot 		gen7_render_ring_cs_stall_wa(req);
372b5c29a34SFrançois Tigeot 	}
373b5c29a34SFrançois Tigeot 
374a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
375b5c29a34SFrançois Tigeot 	if (ret)
376b5c29a34SFrançois Tigeot 		return ret;
377b5c29a34SFrançois Tigeot 
378b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
379b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, flags);
380a2fdbec6SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
381b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
382e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
383e3adcf8fSFrançois Tigeot 
384e3adcf8fSFrançois Tigeot 	return 0;
385e3adcf8fSFrançois Tigeot }
386e3adcf8fSFrançois Tigeot 
3879edbd4a0SFrançois Tigeot static int
388a05eeebfSFrançois Tigeot gen8_emit_pipe_control(struct drm_i915_gem_request *req,
38924edb884SFrançois Tigeot 		       u32 flags, u32 scratch_addr)
39024edb884SFrançois Tigeot {
391a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
39224edb884SFrançois Tigeot 	int ret;
39324edb884SFrançois Tigeot 
394a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 6);
39524edb884SFrançois Tigeot 	if (ret)
39624edb884SFrançois Tigeot 		return ret;
39724edb884SFrançois Tigeot 
39824edb884SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
39924edb884SFrançois Tigeot 	intel_ring_emit(ring, flags);
40024edb884SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
40124edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
40224edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
40324edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
40424edb884SFrançois Tigeot 	intel_ring_advance(ring);
40524edb884SFrançois Tigeot 
40624edb884SFrançois Tigeot 	return 0;
40724edb884SFrançois Tigeot }
40824edb884SFrançois Tigeot 
40924edb884SFrançois Tigeot static int
410a05eeebfSFrançois Tigeot gen8_render_ring_flush(struct drm_i915_gem_request *req,
4119edbd4a0SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
4129edbd4a0SFrançois Tigeot {
4139edbd4a0SFrançois Tigeot 	u32 flags = 0;
414a05eeebfSFrançois Tigeot 	u32 scratch_addr = req->ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
4159edbd4a0SFrançois Tigeot 	int ret;
4169edbd4a0SFrançois Tigeot 
4179edbd4a0SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
4189edbd4a0SFrançois Tigeot 
4199edbd4a0SFrançois Tigeot 	if (flush_domains) {
4209edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
4219edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
422b49c8cf9SFrançois Tigeot 		flags |= PIPE_CONTROL_FLUSH_ENABLE;
4239edbd4a0SFrançois Tigeot 	}
4249edbd4a0SFrançois Tigeot 	if (invalidate_domains) {
4259edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
4269edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
4279edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
4289edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
4299edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
4309edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
4319edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
4329edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4339edbd4a0SFrançois Tigeot 
43424edb884SFrançois Tigeot 		/* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
435a05eeebfSFrançois Tigeot 		ret = gen8_emit_pipe_control(req,
43624edb884SFrançois Tigeot 					     PIPE_CONTROL_CS_STALL |
43724edb884SFrançois Tigeot 					     PIPE_CONTROL_STALL_AT_SCOREBOARD,
43824edb884SFrançois Tigeot 					     0);
4399edbd4a0SFrançois Tigeot 		if (ret)
4409edbd4a0SFrançois Tigeot 			return ret;
44124edb884SFrançois Tigeot 	}
4429edbd4a0SFrançois Tigeot 
443a05eeebfSFrançois Tigeot 	return gen8_emit_pipe_control(req, flags, scratch_addr);
4449edbd4a0SFrançois Tigeot }
4459edbd4a0SFrançois Tigeot 
446ba55f2f5SFrançois Tigeot static void ring_write_tail(struct intel_engine_cs *ring,
447b5c29a34SFrançois Tigeot 			    u32 value)
448e3adcf8fSFrançois Tigeot {
449ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
450e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
451e3adcf8fSFrançois Tigeot }
452e3adcf8fSFrançois Tigeot 
453ba55f2f5SFrançois Tigeot u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
454e3adcf8fSFrançois Tigeot {
455ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
456ba55f2f5SFrançois Tigeot 	u64 acthd;
457e3adcf8fSFrançois Tigeot 
458ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
459ba55f2f5SFrançois Tigeot 		acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
460ba55f2f5SFrançois Tigeot 					 RING_ACTHD_UDW(ring->mmio_base));
461ba55f2f5SFrançois Tigeot 	else if (INTEL_INFO(ring->dev)->gen >= 4)
462ba55f2f5SFrançois Tigeot 		acthd = I915_READ(RING_ACTHD(ring->mmio_base));
463ba55f2f5SFrançois Tigeot 	else
464ba55f2f5SFrançois Tigeot 		acthd = I915_READ(ACTHD);
465ba55f2f5SFrançois Tigeot 
466ba55f2f5SFrançois Tigeot 	return acthd;
467e3adcf8fSFrançois Tigeot }
468e3adcf8fSFrançois Tigeot 
469ba55f2f5SFrançois Tigeot static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
4705d0b1887SFrançois Tigeot {
4715d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
4725d0b1887SFrançois Tigeot 	u32 addr;
4735d0b1887SFrançois Tigeot 
4745d0b1887SFrançois Tigeot 	addr = dev_priv->status_page_dmah->busaddr;
4755d0b1887SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 4)
4765d0b1887SFrançois Tigeot 		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
4775d0b1887SFrançois Tigeot 	I915_WRITE(HWS_PGA, addr);
4785d0b1887SFrançois Tigeot }
4795d0b1887SFrançois Tigeot 
480477eb7f9SFrançois Tigeot static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
481477eb7f9SFrançois Tigeot {
482477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
483477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
484477eb7f9SFrançois Tigeot 	u32 mmio = 0;
485477eb7f9SFrançois Tigeot 
486477eb7f9SFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
487477eb7f9SFrançois Tigeot 	 * the ring registers as of gen7.
488477eb7f9SFrançois Tigeot 	 */
489477eb7f9SFrançois Tigeot 	if (IS_GEN7(dev)) {
490477eb7f9SFrançois Tigeot 		switch (ring->id) {
491477eb7f9SFrançois Tigeot 		case RCS:
492477eb7f9SFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
493477eb7f9SFrançois Tigeot 			break;
494477eb7f9SFrançois Tigeot 		case BCS:
495477eb7f9SFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
496477eb7f9SFrançois Tigeot 			break;
497477eb7f9SFrançois Tigeot 		/*
498477eb7f9SFrançois Tigeot 		 * VCS2 actually doesn't exist on Gen7. Only shut up
499477eb7f9SFrançois Tigeot 		 * gcc switch check warning
500477eb7f9SFrançois Tigeot 		 */
501477eb7f9SFrançois Tigeot 		case VCS2:
502477eb7f9SFrançois Tigeot 		case VCS:
503477eb7f9SFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
504477eb7f9SFrançois Tigeot 			break;
505477eb7f9SFrançois Tigeot 		case VECS:
506477eb7f9SFrançois Tigeot 			mmio = VEBOX_HWS_PGA_GEN7;
507477eb7f9SFrançois Tigeot 			break;
508477eb7f9SFrançois Tigeot 		}
509477eb7f9SFrançois Tigeot 	} else if (IS_GEN6(ring->dev)) {
510477eb7f9SFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
511477eb7f9SFrançois Tigeot 	} else {
512477eb7f9SFrançois Tigeot 		/* XXX: gen8 returns to sanity */
513477eb7f9SFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
514477eb7f9SFrançois Tigeot 	}
515477eb7f9SFrançois Tigeot 
516477eb7f9SFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
517477eb7f9SFrançois Tigeot 	POSTING_READ(mmio);
518477eb7f9SFrançois Tigeot 
519477eb7f9SFrançois Tigeot 	/*
520477eb7f9SFrançois Tigeot 	 * Flush the TLB for this page
521477eb7f9SFrançois Tigeot 	 *
522477eb7f9SFrançois Tigeot 	 * FIXME: These two bits have disappeared on gen8, so a question
523477eb7f9SFrançois Tigeot 	 * arises: do we still need this and if so how should we go about
524477eb7f9SFrançois Tigeot 	 * invalidating the TLB?
525477eb7f9SFrançois Tigeot 	 */
526477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
527477eb7f9SFrançois Tigeot 		u32 reg = RING_INSTPM(ring->mmio_base);
528477eb7f9SFrançois Tigeot 
529477eb7f9SFrançois Tigeot 		/* ring should be idle before issuing a sync flush*/
530477eb7f9SFrançois Tigeot 		WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
531477eb7f9SFrançois Tigeot 
532477eb7f9SFrançois Tigeot 		I915_WRITE(reg,
533477eb7f9SFrançois Tigeot 			   _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
534477eb7f9SFrançois Tigeot 					      INSTPM_SYNC_FLUSH));
535477eb7f9SFrançois Tigeot 		if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
536477eb7f9SFrançois Tigeot 			     1000))
537477eb7f9SFrançois Tigeot 			DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
538477eb7f9SFrançois Tigeot 				  ring->name);
539477eb7f9SFrançois Tigeot 	}
540477eb7f9SFrançois Tigeot }
541477eb7f9SFrançois Tigeot 
542ba55f2f5SFrançois Tigeot static bool stop_ring(struct intel_engine_cs *ring)
543e3adcf8fSFrançois Tigeot {
544ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ring->dev);
545e3adcf8fSFrançois Tigeot 
546ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
547ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
5481b13d190SFrançois Tigeot 		if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
549ba55f2f5SFrançois Tigeot 			DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
5501b13d190SFrançois Tigeot 			/* Sometimes we observe that the idle flag is not
5511b13d190SFrançois Tigeot 			 * set even though the ring is empty. So double
5521b13d190SFrançois Tigeot 			 * check before giving up.
5531b13d190SFrançois Tigeot 			 */
5541b13d190SFrançois Tigeot 			if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
555ba55f2f5SFrançois Tigeot 				return false;
556ba55f2f5SFrançois Tigeot 		}
557ba55f2f5SFrançois Tigeot 	}
558686a02f1SFrançois Tigeot 
559e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
560e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
561e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
562e3adcf8fSFrançois Tigeot 
563ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
564ba55f2f5SFrançois Tigeot 		(void)I915_READ_CTL(ring);
565ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
566ba55f2f5SFrançois Tigeot 	}
567e3adcf8fSFrançois Tigeot 
568ba55f2f5SFrançois Tigeot 	return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
569ba55f2f5SFrançois Tigeot }
570ba55f2f5SFrançois Tigeot 
571ba55f2f5SFrançois Tigeot static int init_ring_common(struct intel_engine_cs *ring)
572ba55f2f5SFrançois Tigeot {
573ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ring->dev;
574ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
575ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
576ba55f2f5SFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
577ba55f2f5SFrançois Tigeot 	int ret = 0;
578ba55f2f5SFrançois Tigeot 
5792c9916cdSFrançois Tigeot 	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
580ba55f2f5SFrançois Tigeot 
581ba55f2f5SFrançois Tigeot 	if (!stop_ring(ring)) {
582ba55f2f5SFrançois Tigeot 		/* G45 ring initialization often fails to reset head to zero */
583b5c29a34SFrançois Tigeot 		DRM_DEBUG_KMS("%s head not reset to zero "
584e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
585e3adcf8fSFrançois Tigeot 			      ring->name,
586e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
587e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
588e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
589e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
590e3adcf8fSFrançois Tigeot 
591ba55f2f5SFrançois Tigeot 		if (!stop_ring(ring)) {
592e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
593e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
594e3adcf8fSFrançois Tigeot 				  ring->name,
595e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
596e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
597e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
598e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
599686a02f1SFrançois Tigeot 			ret = -EIO;
600686a02f1SFrançois Tigeot 			goto out;
601e3adcf8fSFrançois Tigeot 		}
602ba55f2f5SFrançois Tigeot 	}
603ba55f2f5SFrançois Tigeot 
604ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev))
605ba55f2f5SFrançois Tigeot 		intel_ring_setup_status_page(ring);
606ba55f2f5SFrançois Tigeot 	else
607ba55f2f5SFrançois Tigeot 		ring_setup_phys_status_page(ring);
608ba55f2f5SFrançois Tigeot 
6090f370975SMatthew Dillon 	/* Enforce ordering by reading HEAD register back */
6100f370975SMatthew Dillon 	I915_READ_HEAD(ring);
6110f370975SMatthew Dillon 
612ba55f2f5SFrançois Tigeot 	/* Initialize the ring. This must happen _after_ we've cleared the ring
613ba55f2f5SFrançois Tigeot 	 * registers with the above sequence (the readback of the HEAD registers
614ba55f2f5SFrançois Tigeot 	 * also enforces ordering), otherwise the hw might lose the new ring
615ba55f2f5SFrançois Tigeot 	 * register values. */
616ba55f2f5SFrançois Tigeot 	I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
6171b13d190SFrançois Tigeot 
6181b13d190SFrançois Tigeot 	/* WaClearRingBufHeadRegAtInit:ctg,elk */
6191b13d190SFrançois Tigeot 	if (I915_READ_HEAD(ring))
6201b13d190SFrançois Tigeot 		DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
6211b13d190SFrançois Tigeot 			  ring->name, I915_READ_HEAD(ring));
6221b13d190SFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
6231b13d190SFrançois Tigeot 	(void)I915_READ_HEAD(ring);
6241b13d190SFrançois Tigeot 
625ba55f2f5SFrançois Tigeot 	I915_WRITE_CTL(ring,
626ba55f2f5SFrançois Tigeot 			((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
627ba55f2f5SFrançois Tigeot 			| RING_VALID);
628ba55f2f5SFrançois Tigeot 
629ba55f2f5SFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
630ba55f2f5SFrançois Tigeot 	if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
631ba55f2f5SFrançois Tigeot 		     I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
632ba55f2f5SFrançois Tigeot 		     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
633ba55f2f5SFrançois Tigeot 		DRM_ERROR("%s initialization failed "
634ba55f2f5SFrançois Tigeot 			  "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
635ba55f2f5SFrançois Tigeot 			  ring->name,
636ba55f2f5SFrançois Tigeot 			  I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
637ba55f2f5SFrançois Tigeot 			  I915_READ_HEAD(ring), I915_READ_TAIL(ring),
638ba55f2f5SFrançois Tigeot 			  I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
639ba55f2f5SFrançois Tigeot 		ret = -EIO;
640ba55f2f5SFrançois Tigeot 		goto out;
641ba55f2f5SFrançois Tigeot 	}
642e3adcf8fSFrançois Tigeot 
6432c9916cdSFrançois Tigeot 	ringbuf->last_retired_head = -1;
644ba55f2f5SFrançois Tigeot 	ringbuf->head = I915_READ_HEAD(ring);
645ba55f2f5SFrançois Tigeot 	ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
6462c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
647e3adcf8fSFrançois Tigeot 
6485d0b1887SFrançois Tigeot 	memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
6495d0b1887SFrançois Tigeot 
650686a02f1SFrançois Tigeot out:
6512c9916cdSFrançois Tigeot 	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
652686a02f1SFrançois Tigeot 
653686a02f1SFrançois Tigeot 	return ret;
654e3adcf8fSFrançois Tigeot }
655e3adcf8fSFrançois Tigeot 
6561b13d190SFrançois Tigeot void
6571b13d190SFrançois Tigeot intel_fini_pipe_control(struct intel_engine_cs *ring)
6581b13d190SFrançois Tigeot {
6591b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
6601b13d190SFrançois Tigeot 
6611b13d190SFrançois Tigeot 	if (ring->scratch.obj == NULL)
6621b13d190SFrançois Tigeot 		return;
6631b13d190SFrançois Tigeot 
6641b13d190SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
6657ec9f8e5SFrançois Tigeot 		kunmap(sg_page(ring->scratch.obj->pages->sgl));
6661b13d190SFrançois Tigeot 		i915_gem_object_ggtt_unpin(ring->scratch.obj);
6671b13d190SFrançois Tigeot 	}
6681b13d190SFrançois Tigeot 
6691b13d190SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
6701b13d190SFrançois Tigeot 	ring->scratch.obj = NULL;
6711b13d190SFrançois Tigeot }
6721b13d190SFrançois Tigeot 
6731b13d190SFrançois Tigeot int
6741b13d190SFrançois Tigeot intel_init_pipe_control(struct intel_engine_cs *ring)
675e3adcf8fSFrançois Tigeot {
676e3adcf8fSFrançois Tigeot 	int ret;
677e3adcf8fSFrançois Tigeot 
6782c9916cdSFrançois Tigeot 	WARN_ON(ring->scratch.obj);
679e3adcf8fSFrançois Tigeot 
6809edbd4a0SFrançois Tigeot 	ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
6819edbd4a0SFrançois Tigeot 	if (ring->scratch.obj == NULL) {
682e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
683e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
684e3adcf8fSFrançois Tigeot 		goto err;
685e3adcf8fSFrançois Tigeot 	}
686e3adcf8fSFrançois Tigeot 
687ba55f2f5SFrançois Tigeot 	ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
688ba55f2f5SFrançois Tigeot 	if (ret)
689ba55f2f5SFrançois Tigeot 		goto err_unref;
690e3adcf8fSFrançois Tigeot 
691ba55f2f5SFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
692e3adcf8fSFrançois Tigeot 	if (ret)
693e3adcf8fSFrançois Tigeot 		goto err_unref;
694e3adcf8fSFrançois Tigeot 
6959edbd4a0SFrançois Tigeot 	ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
6967ec9f8e5SFrançois Tigeot 	ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
6979edbd4a0SFrançois Tigeot 	if (ring->scratch.cpu_page == NULL) {
6985d0b1887SFrançois Tigeot 		ret = -ENOMEM;
699e3adcf8fSFrançois Tigeot 		goto err_unpin;
7005d0b1887SFrançois Tigeot 	}
701a2fdbec6SFrançois Tigeot 
702a2fdbec6SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
7039edbd4a0SFrançois Tigeot 			 ring->name, ring->scratch.gtt_offset);
704e3adcf8fSFrançois Tigeot 	return 0;
705e3adcf8fSFrançois Tigeot 
706e3adcf8fSFrançois Tigeot err_unpin:
707ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(ring->scratch.obj);
708e3adcf8fSFrançois Tigeot err_unref:
7099edbd4a0SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
710e3adcf8fSFrançois Tigeot err:
711e3adcf8fSFrançois Tigeot 	return ret;
712e3adcf8fSFrançois Tigeot }
713e3adcf8fSFrançois Tigeot 
714a05eeebfSFrançois Tigeot static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
7151b13d190SFrançois Tigeot {
7162c9916cdSFrançois Tigeot 	int ret, i;
717a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
7181b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
7191b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
7202c9916cdSFrançois Tigeot 	struct i915_workarounds *w = &dev_priv->workarounds;
7211b13d190SFrançois Tigeot 
7222c9916cdSFrançois Tigeot 	if (WARN_ON_ONCE(w->count == 0))
7232c9916cdSFrançois Tigeot 		return 0;
7241b13d190SFrançois Tigeot 
7252c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
726a05eeebfSFrançois Tigeot 	ret = intel_ring_flush_all_caches(req);
7271b13d190SFrançois Tigeot 	if (ret)
7281b13d190SFrançois Tigeot 		return ret;
7291b13d190SFrançois Tigeot 
730a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, (w->count * 2 + 2));
7312c9916cdSFrançois Tigeot 	if (ret)
7322c9916cdSFrançois Tigeot 		return ret;
7332c9916cdSFrançois Tigeot 
7342c9916cdSFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
7352c9916cdSFrançois Tigeot 	for (i = 0; i < w->count; i++) {
7362c9916cdSFrançois Tigeot 		intel_ring_emit(ring, w->reg[i].addr);
7372c9916cdSFrançois Tigeot 		intel_ring_emit(ring, w->reg[i].value);
7382c9916cdSFrançois Tigeot 	}
7392c9916cdSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
7402c9916cdSFrançois Tigeot 
7412c9916cdSFrançois Tigeot 	intel_ring_advance(ring);
7422c9916cdSFrançois Tigeot 
7432c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
744a05eeebfSFrançois Tigeot 	ret = intel_ring_flush_all_caches(req);
7452c9916cdSFrançois Tigeot 	if (ret)
7462c9916cdSFrançois Tigeot 		return ret;
7472c9916cdSFrançois Tigeot 
7482c9916cdSFrançois Tigeot 	DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
7492c9916cdSFrançois Tigeot 
7502c9916cdSFrançois Tigeot 	return 0;
7512c9916cdSFrançois Tigeot }
7522c9916cdSFrançois Tigeot 
753a05eeebfSFrançois Tigeot static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
7542c9916cdSFrançois Tigeot {
7552c9916cdSFrançois Tigeot 	int ret;
7562c9916cdSFrançois Tigeot 
757a05eeebfSFrançois Tigeot 	ret = intel_ring_workarounds_emit(req);
7582c9916cdSFrançois Tigeot 	if (ret != 0)
7592c9916cdSFrançois Tigeot 		return ret;
7602c9916cdSFrançois Tigeot 
761a05eeebfSFrançois Tigeot 	ret = i915_gem_render_state_init(req);
7622c9916cdSFrançois Tigeot 	if (ret)
7632c9916cdSFrançois Tigeot 		DRM_ERROR("init render state: %d\n", ret);
7642c9916cdSFrançois Tigeot 
7652c9916cdSFrançois Tigeot 	return ret;
7662c9916cdSFrançois Tigeot }
7672c9916cdSFrançois Tigeot 
7682c9916cdSFrançois Tigeot static int wa_add(struct drm_i915_private *dev_priv,
7692c9916cdSFrançois Tigeot 		  const u32 addr, const u32 mask, const u32 val)
7702c9916cdSFrançois Tigeot {
7712c9916cdSFrançois Tigeot 	const u32 idx = dev_priv->workarounds.count;
7722c9916cdSFrançois Tigeot 
7732c9916cdSFrançois Tigeot 	if (WARN_ON(idx >= I915_MAX_WA_REGS))
7742c9916cdSFrançois Tigeot 		return -ENOSPC;
7752c9916cdSFrançois Tigeot 
7762c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].addr = addr;
7772c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].value = val;
7782c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].mask = mask;
7792c9916cdSFrançois Tigeot 
7802c9916cdSFrançois Tigeot 	dev_priv->workarounds.count++;
7812c9916cdSFrançois Tigeot 
7822c9916cdSFrançois Tigeot 	return 0;
7832c9916cdSFrançois Tigeot }
7842c9916cdSFrançois Tigeot 
785a05eeebfSFrançois Tigeot #define WA_REG(addr, mask, val) do { \
7862c9916cdSFrançois Tigeot 		const int r = wa_add(dev_priv, (addr), (mask), (val)); \
7872c9916cdSFrançois Tigeot 		if (r) \
7882c9916cdSFrançois Tigeot 			return r; \
789a05eeebfSFrançois Tigeot 	} while (0)
7902c9916cdSFrançois Tigeot 
7912c9916cdSFrançois Tigeot #define WA_SET_BIT_MASKED(addr, mask) \
7922c9916cdSFrançois Tigeot 	WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
7932c9916cdSFrançois Tigeot 
7942c9916cdSFrançois Tigeot #define WA_CLR_BIT_MASKED(addr, mask) \
7952c9916cdSFrançois Tigeot 	WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
7962c9916cdSFrançois Tigeot 
7972c9916cdSFrançois Tigeot #define WA_SET_FIELD_MASKED(addr, mask, value) \
7982c9916cdSFrançois Tigeot 	WA_REG(addr, mask, _MASKED_FIELD(mask, value))
7992c9916cdSFrançois Tigeot 
8002c9916cdSFrançois Tigeot #define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
8012c9916cdSFrançois Tigeot #define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
8022c9916cdSFrançois Tigeot 
8032c9916cdSFrançois Tigeot #define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
8042c9916cdSFrançois Tigeot 
8052c9916cdSFrançois Tigeot static int bdw_init_workarounds(struct intel_engine_cs *ring)
8062c9916cdSFrançois Tigeot {
8072c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
8082c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
8092c9916cdSFrançois Tigeot 
810a05eeebfSFrançois Tigeot 	WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
811a05eeebfSFrançois Tigeot 
812a05eeebfSFrançois Tigeot 	/* WaDisableAsyncFlipPerfMode:bdw */
813a05eeebfSFrançois Tigeot 	WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
814a05eeebfSFrançois Tigeot 
8151b13d190SFrançois Tigeot 	/* WaDisablePartialInstShootdown:bdw */
8162c9916cdSFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:bdw (pre-production) */
8172c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
8182c9916cdSFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
8192c9916cdSFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
8201b13d190SFrançois Tigeot 
8212c9916cdSFrançois Tigeot 	/* WaDisableDopClockGating:bdw */
8222c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
8232c9916cdSFrançois Tigeot 			  DOP_CLOCK_GATING_DISABLE);
8241b13d190SFrançois Tigeot 
8252c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
8262c9916cdSFrançois Tigeot 			  GEN8_SAMPLER_POWER_BYPASS_DIS);
8271b13d190SFrançois Tigeot 
8281b13d190SFrançois Tigeot 	/* Use Force Non-Coherent whenever executing a 3D context. This is a
8291b13d190SFrançois Tigeot 	 * workaround for for a possible hang in the unlikely event a TLB
8301b13d190SFrançois Tigeot 	 * invalidation occurs during a PSD flush.
8311b13d190SFrançois Tigeot 	 */
8322c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0,
833477eb7f9SFrançois Tigeot 			  /* WaForceEnableNonCoherent:bdw */
8342c9916cdSFrançois Tigeot 			  HDC_FORCE_NON_COHERENT |
835477eb7f9SFrançois Tigeot 			  /* WaForceContextSaveRestoreNonCoherent:bdw */
836477eb7f9SFrançois Tigeot 			  HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
837477eb7f9SFrançois Tigeot 			  /* WaHdcDisableFetchWhenMasked:bdw */
8382c9916cdSFrançois Tigeot 			  HDC_DONOT_FETCH_MEM_WHEN_MASKED |
839477eb7f9SFrançois Tigeot 			  /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
8402c9916cdSFrançois Tigeot 			  (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
8412c9916cdSFrançois Tigeot 
8422c9916cdSFrançois Tigeot 	/* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
8432c9916cdSFrançois Tigeot 	 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
8442c9916cdSFrançois Tigeot 	 *  polygons in the same 8x4 pixel/sample area to be processed without
8452c9916cdSFrançois Tigeot 	 *  stalling waiting for the earlier ones to write to Hierarchical Z
8462c9916cdSFrançois Tigeot 	 *  buffer."
8472c9916cdSFrançois Tigeot 	 *
8482c9916cdSFrançois Tigeot 	 * This optimization is off by default for Broadwell; turn it on.
8492c9916cdSFrançois Tigeot 	 */
8502c9916cdSFrançois Tigeot 	WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
8511b13d190SFrançois Tigeot 
8521b13d190SFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:bdw */
8532c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1,
8542c9916cdSFrançois Tigeot 			  GEN8_4x4_STC_OPTIMIZATION_DISABLE);
8551b13d190SFrançois Tigeot 
8561b13d190SFrançois Tigeot 	/*
8571b13d190SFrançois Tigeot 	 * BSpec recommends 8x4 when MSAA is used,
8581b13d190SFrançois Tigeot 	 * however in practice 16x4 seems fastest.
8591b13d190SFrançois Tigeot 	 *
8601b13d190SFrançois Tigeot 	 * Note that PS/WM thread counts depend on the WIZ hashing
8611b13d190SFrançois Tigeot 	 * disable bit, which we don't touch here, but it's good
8621b13d190SFrançois Tigeot 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8631b13d190SFrançois Tigeot 	 */
8642c9916cdSFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
8652c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_MASK,
8662c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_16x4);
8671b13d190SFrançois Tigeot 
8681b13d190SFrançois Tigeot 	return 0;
8691b13d190SFrançois Tigeot }
8701b13d190SFrançois Tigeot 
8711b13d190SFrançois Tigeot static int chv_init_workarounds(struct intel_engine_cs *ring)
8721b13d190SFrançois Tigeot {
8731b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
8741b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
8751b13d190SFrançois Tigeot 
876a05eeebfSFrançois Tigeot 	WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
877a05eeebfSFrançois Tigeot 
878a05eeebfSFrançois Tigeot 	/* WaDisableAsyncFlipPerfMode:chv */
879a05eeebfSFrançois Tigeot 	WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
880a05eeebfSFrançois Tigeot 
8811b13d190SFrançois Tigeot 	/* WaDisablePartialInstShootdown:chv */
8821b13d190SFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:chv */
8832c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
8842c9916cdSFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
8852c9916cdSFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
8861b13d190SFrançois Tigeot 
8872c9916cdSFrançois Tigeot 	/* Use Force Non-Coherent whenever executing a 3D context. This is a
8882c9916cdSFrançois Tigeot 	 * workaround for a possible hang in the unlikely event a TLB
8892c9916cdSFrançois Tigeot 	 * invalidation occurs during a PSD flush.
8902c9916cdSFrançois Tigeot 	 */
8912c9916cdSFrançois Tigeot 	/* WaForceEnableNonCoherent:chv */
8922c9916cdSFrançois Tigeot 	/* WaHdcDisableFetchWhenMasked:chv */
8932c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0,
8942c9916cdSFrançois Tigeot 			  HDC_FORCE_NON_COHERENT |
8952c9916cdSFrançois Tigeot 			  HDC_DONOT_FETCH_MEM_WHEN_MASKED);
8961b13d190SFrançois Tigeot 
8972c9916cdSFrançois Tigeot 	/* According to the CACHE_MODE_0 default value documentation, some
8982c9916cdSFrançois Tigeot 	 * CHV platforms disable this optimization by default.  Turn it on.
8992c9916cdSFrançois Tigeot 	 */
9002c9916cdSFrançois Tigeot 	WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
9011b13d190SFrançois Tigeot 
9022c9916cdSFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:chv */
9032c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1,
9042c9916cdSFrançois Tigeot 			  GEN8_4x4_STC_OPTIMIZATION_DISABLE);
9052c9916cdSFrançois Tigeot 
9062c9916cdSFrançois Tigeot 	/* Improve HiZ throughput on CHV. */
9072c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
9082c9916cdSFrançois Tigeot 
9092c9916cdSFrançois Tigeot 	/*
9102c9916cdSFrançois Tigeot 	 * BSpec recommends 8x4 when MSAA is used,
9112c9916cdSFrançois Tigeot 	 * however in practice 16x4 seems fastest.
9122c9916cdSFrançois Tigeot 	 *
9132c9916cdSFrançois Tigeot 	 * Note that PS/WM thread counts depend on the WIZ hashing
9142c9916cdSFrançois Tigeot 	 * disable bit, which we don't touch here, but it's good
9152c9916cdSFrançois Tigeot 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
9162c9916cdSFrançois Tigeot 	 */
9172c9916cdSFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
9182c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_MASK,
9192c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_16x4);
9202c9916cdSFrançois Tigeot 
9212c9916cdSFrançois Tigeot 	return 0;
9222c9916cdSFrançois Tigeot }
9232c9916cdSFrançois Tigeot 
924477eb7f9SFrançois Tigeot static int gen9_init_workarounds(struct intel_engine_cs *ring)
925477eb7f9SFrançois Tigeot {
926477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
927477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
92819c468b4SFrançois Tigeot 	uint32_t tmp;
929477eb7f9SFrançois Tigeot 
93019c468b4SFrançois Tigeot 	/* WaDisablePartialInstShootdown:skl,bxt */
931477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
932477eb7f9SFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
933477eb7f9SFrançois Tigeot 
93419c468b4SFrançois Tigeot 	/* Syncing dependencies between camera and graphics:skl,bxt */
935477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
936477eb7f9SFrançois Tigeot 			  GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
937477eb7f9SFrançois Tigeot 
93819c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) == SKL_REVID_A0 ||
93919c468b4SFrançois Tigeot 	    INTEL_REVID(dev) == SKL_REVID_B0)) ||
94019c468b4SFrançois Tigeot 	    (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
94119c468b4SFrançois Tigeot 		/* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
942477eb7f9SFrançois Tigeot 		WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
943477eb7f9SFrançois Tigeot 				  GEN9_DG_MIRROR_FIX_ENABLE);
944477eb7f9SFrançois Tigeot 	}
945477eb7f9SFrançois Tigeot 
94619c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0) ||
94719c468b4SFrançois Tigeot 	    (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
94819c468b4SFrançois Tigeot 		/* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
949477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
950477eb7f9SFrançois Tigeot 				  GEN9_RHWO_OPTIMIZATION_DISABLE);
951a05eeebfSFrançois Tigeot 		/*
952a05eeebfSFrançois Tigeot 		 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
953a05eeebfSFrançois Tigeot 		 * but we do that in per ctx batchbuffer as there is an issue
954a05eeebfSFrançois Tigeot 		 * with this register not getting restored on ctx restore
955a05eeebfSFrançois Tigeot 		 */
956477eb7f9SFrançois Tigeot 	}
957477eb7f9SFrançois Tigeot 
95819c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) >= SKL_REVID_C0) ||
95919c468b4SFrançois Tigeot 	    IS_BROXTON(dev)) {
96019c468b4SFrançois Tigeot 		/* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt */
961477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
962477eb7f9SFrançois Tigeot 				  GEN9_ENABLE_YV12_BUGFIX);
963477eb7f9SFrançois Tigeot 	}
964477eb7f9SFrançois Tigeot 
96519c468b4SFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:skl,bxt */
966477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
967477eb7f9SFrançois Tigeot 
96819c468b4SFrançois Tigeot 	/* WaDisablePartialResolveInVc:skl,bxt */
969477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1, GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE);
970477eb7f9SFrançois Tigeot 
97119c468b4SFrançois Tigeot 	/* WaCcsTlbPrefetchDisable:skl,bxt */
972477eb7f9SFrançois Tigeot 	WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
973477eb7f9SFrançois Tigeot 			  GEN9_CCS_TLB_PREFETCH_ENABLE);
974477eb7f9SFrançois Tigeot 
97519c468b4SFrançois Tigeot 	/* WaDisableMaskBasedCammingInRCC:skl,bxt */
97619c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_C0) ||
97719c468b4SFrançois Tigeot 	    (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0))
97819c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
97919c468b4SFrançois Tigeot 				  PIXEL_MASK_CAMMING_DISABLE);
98019c468b4SFrançois Tigeot 
98119c468b4SFrançois Tigeot 	/* WaForceContextSaveRestoreNonCoherent:skl,bxt */
98219c468b4SFrançois Tigeot 	tmp = HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT;
98319c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_F0) ||
98419c468b4SFrançois Tigeot 	    (IS_BROXTON(dev) && INTEL_REVID(dev) >= BXT_REVID_B0))
98519c468b4SFrançois Tigeot 		tmp |= HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE;
98619c468b4SFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0, tmp);
98719c468b4SFrançois Tigeot 
988477eb7f9SFrançois Tigeot 	return 0;
989477eb7f9SFrançois Tigeot }
990477eb7f9SFrançois Tigeot 
991477eb7f9SFrançois Tigeot static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
992477eb7f9SFrançois Tigeot {
993477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
994477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
995477eb7f9SFrançois Tigeot 	u8 vals[3] = { 0, 0, 0 };
996477eb7f9SFrançois Tigeot 	unsigned int i;
997477eb7f9SFrançois Tigeot 
998477eb7f9SFrançois Tigeot 	for (i = 0; i < 3; i++) {
999477eb7f9SFrançois Tigeot 		u8 ss;
1000477eb7f9SFrançois Tigeot 
1001477eb7f9SFrançois Tigeot 		/*
1002477eb7f9SFrançois Tigeot 		 * Only consider slices where one, and only one, subslice has 7
1003477eb7f9SFrançois Tigeot 		 * EUs
1004477eb7f9SFrançois Tigeot 		 */
1005477eb7f9SFrançois Tigeot 		if (hweight8(dev_priv->info.subslice_7eu[i]) != 1)
1006477eb7f9SFrançois Tigeot 			continue;
1007477eb7f9SFrançois Tigeot 
1008477eb7f9SFrançois Tigeot 		/*
1009477eb7f9SFrançois Tigeot 		 * subslice_7eu[i] != 0 (because of the check above) and
1010477eb7f9SFrançois Tigeot 		 * ss_max == 4 (maximum number of subslices possible per slice)
1011477eb7f9SFrançois Tigeot 		 *
1012477eb7f9SFrançois Tigeot 		 * ->    0 <= ss <= 3;
1013477eb7f9SFrançois Tigeot 		 */
1014477eb7f9SFrançois Tigeot 		ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1015477eb7f9SFrançois Tigeot 		vals[i] = 3 - ss;
1016477eb7f9SFrançois Tigeot 	}
1017477eb7f9SFrançois Tigeot 
1018477eb7f9SFrançois Tigeot 	if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1019477eb7f9SFrançois Tigeot 		return 0;
1020477eb7f9SFrançois Tigeot 
1021477eb7f9SFrançois Tigeot 	/* Tune IZ hashing. See intel_device_info_runtime_init() */
1022477eb7f9SFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1023477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(2) |
1024477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(1) |
1025477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(0),
1026477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(2, vals[2]) |
1027477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(1, vals[1]) |
1028477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(0, vals[0]));
1029477eb7f9SFrançois Tigeot 
1030477eb7f9SFrançois Tigeot 	return 0;
1031477eb7f9SFrançois Tigeot }
1032477eb7f9SFrançois Tigeot 
1033477eb7f9SFrançois Tigeot 
1034477eb7f9SFrançois Tigeot static int skl_init_workarounds(struct intel_engine_cs *ring)
1035477eb7f9SFrançois Tigeot {
1036477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1037477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1038477eb7f9SFrançois Tigeot 
1039477eb7f9SFrançois Tigeot 	gen9_init_workarounds(ring);
1040477eb7f9SFrançois Tigeot 
1041477eb7f9SFrançois Tigeot 	/* WaDisablePowerCompilerClockGating:skl */
1042477eb7f9SFrançois Tigeot 	if (INTEL_REVID(dev) == SKL_REVID_B0)
1043477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(HIZ_CHICKEN,
1044477eb7f9SFrançois Tigeot 				  BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1045477eb7f9SFrançois Tigeot 
104619c468b4SFrançois Tigeot 	if (INTEL_REVID(dev) <= SKL_REVID_D0) {
104719c468b4SFrançois Tigeot 		/*
104819c468b4SFrançois Tigeot 		 *Use Force Non-Coherent whenever executing a 3D context. This
104919c468b4SFrançois Tigeot 		 * is a workaround for a possible hang in the unlikely event
105019c468b4SFrançois Tigeot 		 * a TLB invalidation occurs during a PSD flush.
105119c468b4SFrançois Tigeot 		 */
105219c468b4SFrançois Tigeot 		/* WaForceEnableNonCoherent:skl */
105319c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(HDC_CHICKEN0,
105419c468b4SFrançois Tigeot 				  HDC_FORCE_NON_COHERENT);
105519c468b4SFrançois Tigeot 	}
105619c468b4SFrançois Tigeot 
1057a05eeebfSFrançois Tigeot 	if (INTEL_REVID(dev) == SKL_REVID_C0 ||
1058a05eeebfSFrançois Tigeot 	    INTEL_REVID(dev) == SKL_REVID_D0)
1059a05eeebfSFrançois Tigeot 		/* WaBarrierPerformanceFixDisable:skl */
1060a05eeebfSFrançois Tigeot 		WA_SET_BIT_MASKED(HDC_CHICKEN0,
1061a05eeebfSFrançois Tigeot 				  HDC_FENCE_DEST_SLM_DISABLE |
1062a05eeebfSFrançois Tigeot 				  HDC_BARRIER_PERFORMANCE_DISABLE);
1063a05eeebfSFrançois Tigeot 
1064a05eeebfSFrançois Tigeot 	/* WaDisableSbeCacheDispatchPortSharing:skl */
1065a05eeebfSFrançois Tigeot 	if (INTEL_REVID(dev) <= SKL_REVID_F0) {
1066a05eeebfSFrançois Tigeot 		WA_SET_BIT_MASKED(
1067a05eeebfSFrançois Tigeot 			GEN7_HALF_SLICE_CHICKEN1,
1068a05eeebfSFrançois Tigeot 			GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1069a05eeebfSFrançois Tigeot 	}
1070a05eeebfSFrançois Tigeot 
1071477eb7f9SFrançois Tigeot 	return skl_tune_iz_hashing(ring);
1072477eb7f9SFrançois Tigeot }
1073477eb7f9SFrançois Tigeot 
107419c468b4SFrançois Tigeot static int bxt_init_workarounds(struct intel_engine_cs *ring)
107519c468b4SFrançois Tigeot {
107619c468b4SFrançois Tigeot 	struct drm_device *dev = ring->dev;
107719c468b4SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
107819c468b4SFrançois Tigeot 
107919c468b4SFrançois Tigeot 	gen9_init_workarounds(ring);
108019c468b4SFrançois Tigeot 
108119c468b4SFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:bxt */
108219c468b4SFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
108319c468b4SFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
108419c468b4SFrançois Tigeot 
108519c468b4SFrançois Tigeot 	/* WaDisableSbeCacheDispatchPortSharing:bxt */
108619c468b4SFrançois Tigeot 	if (INTEL_REVID(dev) <= BXT_REVID_B0) {
108719c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(
108819c468b4SFrançois Tigeot 			GEN7_HALF_SLICE_CHICKEN1,
108919c468b4SFrançois Tigeot 			GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
109019c468b4SFrançois Tigeot 	}
109119c468b4SFrançois Tigeot 
109219c468b4SFrançois Tigeot 	return 0;
109319c468b4SFrançois Tigeot }
109419c468b4SFrançois Tigeot 
10952c9916cdSFrançois Tigeot int init_workarounds_ring(struct intel_engine_cs *ring)
10962c9916cdSFrançois Tigeot {
10972c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
10982c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
10992c9916cdSFrançois Tigeot 
11002c9916cdSFrançois Tigeot 	WARN_ON(ring->id != RCS);
11012c9916cdSFrançois Tigeot 
11022c9916cdSFrançois Tigeot 	dev_priv->workarounds.count = 0;
11032c9916cdSFrançois Tigeot 
11042c9916cdSFrançois Tigeot 	if (IS_BROADWELL(dev))
11052c9916cdSFrançois Tigeot 		return bdw_init_workarounds(ring);
11062c9916cdSFrançois Tigeot 
11072c9916cdSFrançois Tigeot 	if (IS_CHERRYVIEW(dev))
11082c9916cdSFrançois Tigeot 		return chv_init_workarounds(ring);
11091b13d190SFrançois Tigeot 
1110477eb7f9SFrançois Tigeot 	if (IS_SKYLAKE(dev))
1111477eb7f9SFrançois Tigeot 		return skl_init_workarounds(ring);
111219c468b4SFrançois Tigeot 
111319c468b4SFrançois Tigeot 	if (IS_BROXTON(dev))
111419c468b4SFrançois Tigeot 		return bxt_init_workarounds(ring);
1115477eb7f9SFrançois Tigeot 
11161b13d190SFrançois Tigeot 	return 0;
11171b13d190SFrançois Tigeot }
11181b13d190SFrançois Tigeot 
1119ba55f2f5SFrançois Tigeot static int init_render_ring(struct intel_engine_cs *ring)
1120e3adcf8fSFrançois Tigeot {
1121e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1122e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1123e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
112424edb884SFrançois Tigeot 	if (ret)
112524edb884SFrançois Tigeot 		return ret;
1126e3adcf8fSFrançois Tigeot 
1127ba55f2f5SFrançois Tigeot 	/* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1128ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
1129f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1130f4e1c372SFrançois Tigeot 
1131f4e1c372SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
1132f4e1c372SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1133f4e1c372SFrançois Tigeot 	 * programmed to '1' on all products.
11345d0b1887SFrançois Tigeot 	 *
1135a05eeebfSFrançois Tigeot 	 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1136f4e1c372SFrançois Tigeot 	 */
1137a05eeebfSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
1138f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1139f4e1c372SFrançois Tigeot 
1140f4e1c372SFrançois Tigeot 	/* Required for the hardware to program scanline values for waiting */
1141ba55f2f5SFrançois Tigeot 	/* WaEnableFlushTlbInvalidationMode:snb */
1142f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6)
1143f4e1c372SFrançois Tigeot 		I915_WRITE(GFX_MODE,
1144ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
1145f4e1c372SFrançois Tigeot 
1146ba55f2f5SFrançois Tigeot 	/* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
1147e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev))
1148e3adcf8fSFrançois Tigeot 		I915_WRITE(GFX_MODE_GEN7,
1149ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1150f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
1151e3adcf8fSFrançois Tigeot 
1152e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
1153e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
1154e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
1155e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
1156e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
1157e3adcf8fSFrançois Tigeot 		 */
1158e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
1159f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
1160e3adcf8fSFrançois Tigeot 	}
1161e3adcf8fSFrançois Tigeot 
1162a05eeebfSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
1163f4e1c372SFrançois Tigeot 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1164f4e1c372SFrançois Tigeot 
11659edbd4a0SFrançois Tigeot 	if (HAS_L3_DPF(dev))
11669edbd4a0SFrançois Tigeot 		I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1167e3adcf8fSFrançois Tigeot 
11682c9916cdSFrançois Tigeot 	return init_workarounds_ring(ring);
1169e3adcf8fSFrançois Tigeot }
1170e3adcf8fSFrançois Tigeot 
1171ba55f2f5SFrançois Tigeot static void render_ring_cleanup(struct intel_engine_cs *ring)
1172e3adcf8fSFrançois Tigeot {
1173b5c29a34SFrançois Tigeot 	struct drm_device *dev = ring->dev;
117424edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
117524edb884SFrançois Tigeot 
117624edb884SFrançois Tigeot 	if (dev_priv->semaphore_obj) {
117724edb884SFrançois Tigeot 		i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
117824edb884SFrançois Tigeot 		drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
117924edb884SFrançois Tigeot 		dev_priv->semaphore_obj = NULL;
118024edb884SFrançois Tigeot 	}
1181b5c29a34SFrançois Tigeot 
11821b13d190SFrançois Tigeot 	intel_fini_pipe_control(ring);
1183e3adcf8fSFrançois Tigeot }
1184e3adcf8fSFrançois Tigeot 
1185a05eeebfSFrançois Tigeot static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
118624edb884SFrançois Tigeot 			   unsigned int num_dwords)
118724edb884SFrançois Tigeot {
118824edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 8
1189a05eeebfSFrançois Tigeot 	struct intel_engine_cs *signaller = signaller_req->ring;
119024edb884SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
119124edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
119224edb884SFrançois Tigeot 	struct intel_engine_cs *waiter;
119324edb884SFrançois Tigeot 	int i, ret, num_rings;
119424edb884SFrançois Tigeot 
119524edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
119624edb884SFrançois Tigeot 	num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
119724edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
119824edb884SFrançois Tigeot 
1199a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(signaller_req, num_dwords);
120024edb884SFrançois Tigeot 	if (ret)
120124edb884SFrançois Tigeot 		return ret;
120224edb884SFrançois Tigeot 
120324edb884SFrançois Tigeot 	for_each_ring(waiter, dev_priv, i) {
12042c9916cdSFrançois Tigeot 		u32 seqno;
120524edb884SFrançois Tigeot 		u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
120624edb884SFrançois Tigeot 		if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
120724edb884SFrançois Tigeot 			continue;
120824edb884SFrançois Tigeot 
1209a05eeebfSFrançois Tigeot 		seqno = i915_gem_request_get_seqno(signaller_req);
121024edb884SFrançois Tigeot 		intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
121124edb884SFrançois Tigeot 		intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
121224edb884SFrançois Tigeot 					   PIPE_CONTROL_QW_WRITE |
121324edb884SFrançois Tigeot 					   PIPE_CONTROL_FLUSH_ENABLE);
121424edb884SFrançois Tigeot 		intel_ring_emit(signaller, lower_32_bits(gtt_offset));
121524edb884SFrançois Tigeot 		intel_ring_emit(signaller, upper_32_bits(gtt_offset));
12162c9916cdSFrançois Tigeot 		intel_ring_emit(signaller, seqno);
121724edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
121824edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
121924edb884SFrançois Tigeot 					   MI_SEMAPHORE_TARGET(waiter->id));
122024edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
122124edb884SFrançois Tigeot 	}
122224edb884SFrançois Tigeot 
122324edb884SFrançois Tigeot 	return 0;
122424edb884SFrançois Tigeot }
122524edb884SFrançois Tigeot 
1226a05eeebfSFrançois Tigeot static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
122724edb884SFrançois Tigeot 			   unsigned int num_dwords)
122824edb884SFrançois Tigeot {
122924edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 6
1230a05eeebfSFrançois Tigeot 	struct intel_engine_cs *signaller = signaller_req->ring;
123124edb884SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
123224edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
123324edb884SFrançois Tigeot 	struct intel_engine_cs *waiter;
123424edb884SFrançois Tigeot 	int i, ret, num_rings;
123524edb884SFrançois Tigeot 
123624edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
123724edb884SFrançois Tigeot 	num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
123824edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
123924edb884SFrançois Tigeot 
1240a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(signaller_req, num_dwords);
124124edb884SFrançois Tigeot 	if (ret)
124224edb884SFrançois Tigeot 		return ret;
124324edb884SFrançois Tigeot 
124424edb884SFrançois Tigeot 	for_each_ring(waiter, dev_priv, i) {
12452c9916cdSFrançois Tigeot 		u32 seqno;
124624edb884SFrançois Tigeot 		u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
124724edb884SFrançois Tigeot 		if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
124824edb884SFrançois Tigeot 			continue;
124924edb884SFrançois Tigeot 
1250a05eeebfSFrançois Tigeot 		seqno = i915_gem_request_get_seqno(signaller_req);
125124edb884SFrançois Tigeot 		intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
125224edb884SFrançois Tigeot 					   MI_FLUSH_DW_OP_STOREDW);
125324edb884SFrançois Tigeot 		intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
125424edb884SFrançois Tigeot 					   MI_FLUSH_DW_USE_GTT);
125524edb884SFrançois Tigeot 		intel_ring_emit(signaller, upper_32_bits(gtt_offset));
12562c9916cdSFrançois Tigeot 		intel_ring_emit(signaller, seqno);
125724edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
125824edb884SFrançois Tigeot 					   MI_SEMAPHORE_TARGET(waiter->id));
125924edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
126024edb884SFrançois Tigeot 	}
126124edb884SFrançois Tigeot 
126224edb884SFrançois Tigeot 	return 0;
126324edb884SFrançois Tigeot }
126424edb884SFrançois Tigeot 
1265a05eeebfSFrançois Tigeot static int gen6_signal(struct drm_i915_gem_request *signaller_req,
1266ba55f2f5SFrançois Tigeot 		       unsigned int num_dwords)
1267e3adcf8fSFrançois Tigeot {
1268a05eeebfSFrançois Tigeot 	struct intel_engine_cs *signaller = signaller_req->ring;
1269ba55f2f5SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
1270ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1271ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *useless;
127224edb884SFrançois Tigeot 	int i, ret, num_rings;
1273ba55f2f5SFrançois Tigeot 
127424edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 3
127524edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
127624edb884SFrançois Tigeot 	num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
127724edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
1278ba55f2f5SFrançois Tigeot 
1279a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(signaller_req, num_dwords);
1280ba55f2f5SFrançois Tigeot 	if (ret)
1281ba55f2f5SFrançois Tigeot 		return ret;
1282ba55f2f5SFrançois Tigeot 
1283ba55f2f5SFrançois Tigeot 	for_each_ring(useless, dev_priv, i) {
1284ba55f2f5SFrançois Tigeot 		u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1285ba55f2f5SFrançois Tigeot 		if (mbox_reg != GEN6_NOSYNC) {
1286a05eeebfSFrançois Tigeot 			u32 seqno = i915_gem_request_get_seqno(signaller_req);
1287ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1288ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, mbox_reg);
12892c9916cdSFrançois Tigeot 			intel_ring_emit(signaller, seqno);
1290ba55f2f5SFrançois Tigeot 		}
1291ba55f2f5SFrançois Tigeot 	}
1292ba55f2f5SFrançois Tigeot 
129324edb884SFrançois Tigeot 	/* If num_dwords was rounded, make sure the tail pointer is correct */
129424edb884SFrançois Tigeot 	if (num_rings % 2 == 0)
129524edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_NOOP);
129624edb884SFrançois Tigeot 
1297ba55f2f5SFrançois Tigeot 	return 0;
1298e3adcf8fSFrançois Tigeot }
1299e3adcf8fSFrançois Tigeot 
1300e3adcf8fSFrançois Tigeot /**
1301e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
1302e3adcf8fSFrançois Tigeot  *
1303a05eeebfSFrançois Tigeot  * @request - request to write to the ring
1304e3adcf8fSFrançois Tigeot  *
1305e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
1306e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
1307e3adcf8fSFrançois Tigeot  */
1308e3adcf8fSFrançois Tigeot static int
1309a05eeebfSFrançois Tigeot gen6_add_request(struct drm_i915_gem_request *req)
1310e3adcf8fSFrançois Tigeot {
1311a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1312ba55f2f5SFrançois Tigeot 	int ret;
1313e3adcf8fSFrançois Tigeot 
131424edb884SFrançois Tigeot 	if (ring->semaphore.signal)
1315a05eeebfSFrançois Tigeot 		ret = ring->semaphore.signal(req, 4);
131624edb884SFrançois Tigeot 	else
1317a05eeebfSFrançois Tigeot 		ret = intel_ring_begin(req, 4);
131824edb884SFrançois Tigeot 
13199edbd4a0SFrançois Tigeot 	if (ret)
13209edbd4a0SFrançois Tigeot 		return ret;
13219edbd4a0SFrançois Tigeot 
1322e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1323e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1324a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1325e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
13269edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1327e3adcf8fSFrançois Tigeot 
1328e3adcf8fSFrançois Tigeot 	return 0;
1329e3adcf8fSFrançois Tigeot }
1330e3adcf8fSFrançois Tigeot 
1331a2fdbec6SFrançois Tigeot static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1332a2fdbec6SFrançois Tigeot 					      u32 seqno)
1333a2fdbec6SFrançois Tigeot {
1334a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1335a2fdbec6SFrançois Tigeot 	return dev_priv->last_seqno < seqno;
1336a2fdbec6SFrançois Tigeot }
1337a2fdbec6SFrançois Tigeot 
1338e3adcf8fSFrançois Tigeot /**
1339e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
1340e3adcf8fSFrançois Tigeot  *
1341e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
1342e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
1343e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
1344e3adcf8fSFrançois Tigeot  */
134524edb884SFrançois Tigeot 
134624edb884SFrançois Tigeot static int
1347a05eeebfSFrançois Tigeot gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
134824edb884SFrançois Tigeot 	       struct intel_engine_cs *signaller,
134924edb884SFrançois Tigeot 	       u32 seqno)
135024edb884SFrançois Tigeot {
1351a05eeebfSFrançois Tigeot 	struct intel_engine_cs *waiter = waiter_req->ring;
135224edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = waiter->dev->dev_private;
135324edb884SFrançois Tigeot 	int ret;
135424edb884SFrançois Tigeot 
1355a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(waiter_req, 4);
135624edb884SFrançois Tigeot 	if (ret)
135724edb884SFrançois Tigeot 		return ret;
135824edb884SFrançois Tigeot 
135924edb884SFrançois Tigeot 	intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
136024edb884SFrançois Tigeot 				MI_SEMAPHORE_GLOBAL_GTT |
136124edb884SFrançois Tigeot 				MI_SEMAPHORE_POLL |
136224edb884SFrançois Tigeot 				MI_SEMAPHORE_SAD_GTE_SDD);
136324edb884SFrançois Tigeot 	intel_ring_emit(waiter, seqno);
136424edb884SFrançois Tigeot 	intel_ring_emit(waiter,
136524edb884SFrançois Tigeot 			lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
136624edb884SFrançois Tigeot 	intel_ring_emit(waiter,
136724edb884SFrançois Tigeot 			upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
136824edb884SFrançois Tigeot 	intel_ring_advance(waiter);
136924edb884SFrançois Tigeot 	return 0;
137024edb884SFrançois Tigeot }
137124edb884SFrançois Tigeot 
1372e3adcf8fSFrançois Tigeot static int
1373a05eeebfSFrançois Tigeot gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
1374ba55f2f5SFrançois Tigeot 	       struct intel_engine_cs *signaller,
1375e3adcf8fSFrançois Tigeot 	       u32 seqno)
1376e3adcf8fSFrançois Tigeot {
1377a05eeebfSFrançois Tigeot 	struct intel_engine_cs *waiter = waiter_req->ring;
1378e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
1379e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
1380e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
1381ba55f2f5SFrançois Tigeot 	u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1382ba55f2f5SFrançois Tigeot 	int ret;
1383e3adcf8fSFrançois Tigeot 
1384686a02f1SFrançois Tigeot 	/* Throughout all of the GEM code, seqno passed implies our current
1385686a02f1SFrançois Tigeot 	 * seqno is >= the last seqno executed. However for hardware the
1386686a02f1SFrançois Tigeot 	 * comparison is strictly greater than.
1387686a02f1SFrançois Tigeot 	 */
1388686a02f1SFrançois Tigeot 	seqno -= 1;
1389686a02f1SFrançois Tigeot 
1390ba55f2f5SFrançois Tigeot 	WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
1391686a02f1SFrançois Tigeot 
1392a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(waiter_req, 4);
1393e3adcf8fSFrançois Tigeot 	if (ret)
1394e3adcf8fSFrançois Tigeot 		return ret;
1395e3adcf8fSFrançois Tigeot 
1396a2fdbec6SFrançois Tigeot 	/* If seqno wrap happened, omit the wait with no-ops */
1397a2fdbec6SFrançois Tigeot 	if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
1398ba55f2f5SFrançois Tigeot 		intel_ring_emit(waiter, dw1 | wait_mbox);
1399e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, seqno);
1400e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, 0);
1401e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1402a2fdbec6SFrançois Tigeot 	} else {
1403a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1404a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1405a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1406a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1407a2fdbec6SFrançois Tigeot 	}
1408e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
1409e3adcf8fSFrançois Tigeot 
1410e3adcf8fSFrançois Tigeot 	return 0;
1411e3adcf8fSFrançois Tigeot }
1412e3adcf8fSFrançois Tigeot 
1413e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
1414e3adcf8fSFrançois Tigeot do {									\
1415e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
1416e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
1417e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
1418e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
1419e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
1420e3adcf8fSFrançois Tigeot } while (0)
1421e3adcf8fSFrançois Tigeot 
1422e3adcf8fSFrançois Tigeot static int
1423a05eeebfSFrançois Tigeot pc_render_add_request(struct drm_i915_gem_request *req)
1424e3adcf8fSFrançois Tigeot {
1425a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1426ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
1427e3adcf8fSFrançois Tigeot 	int ret;
1428e3adcf8fSFrançois Tigeot 
1429e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1430e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
1431e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
1432e3adcf8fSFrançois Tigeot 	 *
1433e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
1434e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1435e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
1436e3adcf8fSFrançois Tigeot 	 */
1437a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 32);
1438e3adcf8fSFrançois Tigeot 	if (ret)
1439e3adcf8fSFrançois Tigeot 		return ret;
1440e3adcf8fSFrançois Tigeot 
1441e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1442e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
1443e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
14449edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1445a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1446e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1447e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1448ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
1449e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1450ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1451e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1452ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1453e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1454ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1455e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1456ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1457e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1458b5c29a34SFrançois Tigeot 
1459e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1460e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
1461e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
1462e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
14639edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1464a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1465e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
14669edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1467e3adcf8fSFrançois Tigeot 
1468e3adcf8fSFrançois Tigeot 	return 0;
1469e3adcf8fSFrançois Tigeot }
1470e3adcf8fSFrançois Tigeot 
1471e3adcf8fSFrançois Tigeot static u32
1472ba55f2f5SFrançois Tigeot gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1473e3adcf8fSFrançois Tigeot {
1474e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
1475e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
1476e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
1477ba55f2f5SFrançois Tigeot 	if (!lazy_coherency) {
1478ba55f2f5SFrançois Tigeot 		struct drm_i915_private *dev_priv = ring->dev->dev_private;
1479ba55f2f5SFrançois Tigeot 		POSTING_READ(RING_ACTHD(ring->mmio_base));
1480ba55f2f5SFrançois Tigeot 	}
1481ba55f2f5SFrançois Tigeot 
1482e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1483e3adcf8fSFrançois Tigeot }
1484e3adcf8fSFrançois Tigeot 
1485b030f26bSFrançois Tigeot static u32
1486ba55f2f5SFrançois Tigeot ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1487e3adcf8fSFrançois Tigeot {
1488e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1489e3adcf8fSFrançois Tigeot }
1490e3adcf8fSFrançois Tigeot 
1491a2fdbec6SFrançois Tigeot static void
1492ba55f2f5SFrançois Tigeot ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1493a2fdbec6SFrançois Tigeot {
1494a2fdbec6SFrançois Tigeot 	intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1495a2fdbec6SFrançois Tigeot }
1496a2fdbec6SFrançois Tigeot 
1497b030f26bSFrançois Tigeot static u32
1498ba55f2f5SFrançois Tigeot pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1499e3adcf8fSFrançois Tigeot {
15009edbd4a0SFrançois Tigeot 	return ring->scratch.cpu_page[0];
1501e3adcf8fSFrançois Tigeot }
1502e3adcf8fSFrançois Tigeot 
1503a2fdbec6SFrançois Tigeot static void
1504ba55f2f5SFrançois Tigeot pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1505a2fdbec6SFrançois Tigeot {
15069edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page[0] = seqno;
1507a2fdbec6SFrançois Tigeot }
1508a2fdbec6SFrançois Tigeot 
1509e3adcf8fSFrançois Tigeot static bool
1510ba55f2f5SFrançois Tigeot gen5_ring_get_irq(struct intel_engine_cs *ring)
1511e3adcf8fSFrançois Tigeot {
1512e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1513ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1514*5e269720SFrançois Tigeot 	unsigned long flags;
1515e3adcf8fSFrançois Tigeot 
15162c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1517e3adcf8fSFrançois Tigeot 		return false;
1518e3adcf8fSFrançois Tigeot 
1519*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15209edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0)
152124edb884SFrançois Tigeot 		gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1522*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1523e3adcf8fSFrançois Tigeot 
1524e3adcf8fSFrançois Tigeot 	return true;
1525e3adcf8fSFrançois Tigeot }
1526e3adcf8fSFrançois Tigeot 
1527e3adcf8fSFrançois Tigeot static void
1528ba55f2f5SFrançois Tigeot gen5_ring_put_irq(struct intel_engine_cs *ring)
1529e3adcf8fSFrançois Tigeot {
1530e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1531ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1532*5e269720SFrançois Tigeot 	unsigned long flags;
1533e3adcf8fSFrançois Tigeot 
1534*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15359edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0)
153624edb884SFrançois Tigeot 		gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1537*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1538686a02f1SFrançois Tigeot }
1539686a02f1SFrançois Tigeot 
1540686a02f1SFrançois Tigeot static bool
1541ba55f2f5SFrançois Tigeot i9xx_ring_get_irq(struct intel_engine_cs *ring)
1542686a02f1SFrançois Tigeot {
1543686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1544ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1545*5e269720SFrançois Tigeot 	unsigned long flags;
1546686a02f1SFrançois Tigeot 
15472c9916cdSFrançois Tigeot 	if (!intel_irqs_enabled(dev_priv))
1548686a02f1SFrançois Tigeot 		return false;
1549686a02f1SFrançois Tigeot 
1550*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15519edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
1552686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
1553686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
1554686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
1555686a02f1SFrançois Tigeot 	}
1556*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1557686a02f1SFrançois Tigeot 
1558686a02f1SFrançois Tigeot 	return true;
1559686a02f1SFrançois Tigeot }
1560686a02f1SFrançois Tigeot 
1561686a02f1SFrançois Tigeot static void
1562ba55f2f5SFrançois Tigeot i9xx_ring_put_irq(struct intel_engine_cs *ring)
1563686a02f1SFrançois Tigeot {
1564686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1565ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1566*5e269720SFrançois Tigeot 	unsigned long flags;
1567686a02f1SFrançois Tigeot 
1568*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15699edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
1570686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
1571686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
1572686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
1573686a02f1SFrançois Tigeot 	}
1574*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1575686a02f1SFrançois Tigeot }
1576686a02f1SFrançois Tigeot 
1577686a02f1SFrançois Tigeot static bool
1578ba55f2f5SFrançois Tigeot i8xx_ring_get_irq(struct intel_engine_cs *ring)
1579686a02f1SFrançois Tigeot {
1580686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1581ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1582*5e269720SFrançois Tigeot 	unsigned long flags;
1583686a02f1SFrançois Tigeot 
15842c9916cdSFrançois Tigeot 	if (!intel_irqs_enabled(dev_priv))
1585686a02f1SFrançois Tigeot 		return false;
1586686a02f1SFrançois Tigeot 
1587*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15889edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
1589686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
1590686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
1591686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
1592686a02f1SFrançois Tigeot 	}
1593*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1594686a02f1SFrançois Tigeot 
1595686a02f1SFrançois Tigeot 	return true;
1596686a02f1SFrançois Tigeot }
1597686a02f1SFrançois Tigeot 
1598686a02f1SFrançois Tigeot static void
1599ba55f2f5SFrançois Tigeot i8xx_ring_put_irq(struct intel_engine_cs *ring)
1600686a02f1SFrançois Tigeot {
1601686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1602ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1603*5e269720SFrançois Tigeot 	unsigned long flags;
1604686a02f1SFrançois Tigeot 
1605*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
16069edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
1607686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
1608686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
1609686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
1610e3adcf8fSFrançois Tigeot 	}
1611*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1612e3adcf8fSFrançois Tigeot }
1613e3adcf8fSFrançois Tigeot 
1614e3adcf8fSFrançois Tigeot static int
1615a05eeebfSFrançois Tigeot bsd_ring_flush(struct drm_i915_gem_request *req,
1616b5c29a34SFrançois Tigeot 	       u32     invalidate_domains,
1617b5c29a34SFrançois Tigeot 	       u32     flush_domains)
1618e3adcf8fSFrançois Tigeot {
1619a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1620e3adcf8fSFrançois Tigeot 	int ret;
1621e3adcf8fSFrançois Tigeot 
1622a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
1623e3adcf8fSFrançois Tigeot 	if (ret)
1624e3adcf8fSFrançois Tigeot 		return ret;
1625e3adcf8fSFrançois Tigeot 
1626e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
1627e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1628e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1629e3adcf8fSFrançois Tigeot 	return 0;
1630e3adcf8fSFrançois Tigeot }
1631e3adcf8fSFrançois Tigeot 
1632e3adcf8fSFrançois Tigeot static int
1633a05eeebfSFrançois Tigeot i9xx_add_request(struct drm_i915_gem_request *req)
1634e3adcf8fSFrançois Tigeot {
1635a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1636e3adcf8fSFrançois Tigeot 	int ret;
1637e3adcf8fSFrançois Tigeot 
1638a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
1639e3adcf8fSFrançois Tigeot 	if (ret)
1640e3adcf8fSFrançois Tigeot 		return ret;
1641e3adcf8fSFrançois Tigeot 
1642e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1643e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1644a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1645e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
16469edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1647e3adcf8fSFrançois Tigeot 
1648e3adcf8fSFrançois Tigeot 	return 0;
1649e3adcf8fSFrançois Tigeot }
1650e3adcf8fSFrançois Tigeot 
1651e3adcf8fSFrançois Tigeot static bool
1652ba55f2f5SFrançois Tigeot gen6_ring_get_irq(struct intel_engine_cs *ring)
1653e3adcf8fSFrançois Tigeot {
1654e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1655ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1656*5e269720SFrançois Tigeot 	unsigned long flags;
1657e3adcf8fSFrançois Tigeot 
16582c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1659e3adcf8fSFrançois Tigeot 		return false;
1660e3adcf8fSFrançois Tigeot 
1661*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
16629edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
16639edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
16645d0b1887SFrançois Tigeot 			I915_WRITE_IMR(ring,
16655d0b1887SFrançois Tigeot 				       ~(ring->irq_enable_mask |
16669edbd4a0SFrançois Tigeot 					 GT_PARITY_ERROR(dev)));
1667686a02f1SFrançois Tigeot 		else
1668686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
166924edb884SFrançois Tigeot 		gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1670e3adcf8fSFrançois Tigeot 	}
1671*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1672e3adcf8fSFrançois Tigeot 
1673e3adcf8fSFrançois Tigeot 	return true;
1674e3adcf8fSFrançois Tigeot }
1675e3adcf8fSFrançois Tigeot 
1676e3adcf8fSFrançois Tigeot static void
1677ba55f2f5SFrançois Tigeot gen6_ring_put_irq(struct intel_engine_cs *ring)
1678e3adcf8fSFrançois Tigeot {
1679e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1680ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1681*5e269720SFrançois Tigeot 	unsigned long flags;
1682e3adcf8fSFrançois Tigeot 
1683*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
16849edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
16859edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
16869edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1687686a02f1SFrançois Tigeot 		else
1688686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
168924edb884SFrançois Tigeot 		gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1690e3adcf8fSFrançois Tigeot 	}
1691*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1692e3adcf8fSFrançois Tigeot }
1693e3adcf8fSFrançois Tigeot 
16945d0b1887SFrançois Tigeot static bool
1695ba55f2f5SFrançois Tigeot hsw_vebox_get_irq(struct intel_engine_cs *ring)
16965d0b1887SFrançois Tigeot {
16975d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
16985d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1699*5e269720SFrançois Tigeot 	unsigned long flags;
17005d0b1887SFrançois Tigeot 
17012c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
17025d0b1887SFrançois Tigeot 		return false;
17035d0b1887SFrançois Tigeot 
1704*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17059edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
17065d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
170724edb884SFrançois Tigeot 		gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
17085d0b1887SFrançois Tigeot 	}
1709*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
17105d0b1887SFrançois Tigeot 
17115d0b1887SFrançois Tigeot 	return true;
17125d0b1887SFrançois Tigeot }
17135d0b1887SFrançois Tigeot 
17145d0b1887SFrançois Tigeot static void
1715ba55f2f5SFrançois Tigeot hsw_vebox_put_irq(struct intel_engine_cs *ring)
17165d0b1887SFrançois Tigeot {
17175d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
17185d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1719*5e269720SFrançois Tigeot 	unsigned long flags;
17205d0b1887SFrançois Tigeot 
1721*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17229edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
17235d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~0);
172424edb884SFrançois Tigeot 		gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
17255d0b1887SFrançois Tigeot 	}
1726*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
17279edbd4a0SFrançois Tigeot }
17289edbd4a0SFrançois Tigeot 
17299edbd4a0SFrançois Tigeot static bool
1730ba55f2f5SFrançois Tigeot gen8_ring_get_irq(struct intel_engine_cs *ring)
17319edbd4a0SFrançois Tigeot {
17329edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
17339edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1734*5e269720SFrançois Tigeot 	unsigned long flags;
17359edbd4a0SFrançois Tigeot 
17362c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
17379edbd4a0SFrançois Tigeot 		return false;
17389edbd4a0SFrançois Tigeot 
1739*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17409edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
17419edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
17429edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
17439edbd4a0SFrançois Tigeot 				       ~(ring->irq_enable_mask |
17449edbd4a0SFrançois Tigeot 					 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
17459edbd4a0SFrançois Tigeot 		} else {
17469edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
17479edbd4a0SFrançois Tigeot 		}
17489edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
17499edbd4a0SFrançois Tigeot 	}
1750*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
17519edbd4a0SFrançois Tigeot 
17529edbd4a0SFrançois Tigeot 	return true;
17539edbd4a0SFrançois Tigeot }
17549edbd4a0SFrançois Tigeot 
17559edbd4a0SFrançois Tigeot static void
1756ba55f2f5SFrançois Tigeot gen8_ring_put_irq(struct intel_engine_cs *ring)
17579edbd4a0SFrançois Tigeot {
17589edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
17599edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1760*5e269720SFrançois Tigeot 	unsigned long flags;
17619edbd4a0SFrançois Tigeot 
1762*5e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17639edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
17649edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
17659edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
17669edbd4a0SFrançois Tigeot 				       ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
17679edbd4a0SFrançois Tigeot 		} else {
17689edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
17699edbd4a0SFrançois Tigeot 		}
17709edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
17719edbd4a0SFrançois Tigeot 	}
1772*5e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
17735d0b1887SFrançois Tigeot }
17745d0b1887SFrançois Tigeot 
1775e3adcf8fSFrançois Tigeot static int
1776a05eeebfSFrançois Tigeot i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
1777ba55f2f5SFrançois Tigeot 			 u64 offset, u32 length,
1778477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1779e3adcf8fSFrançois Tigeot {
1780a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1781e3adcf8fSFrançois Tigeot 	int ret;
1782e3adcf8fSFrançois Tigeot 
1783a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
1784e3adcf8fSFrançois Tigeot 	if (ret)
1785e3adcf8fSFrançois Tigeot 		return ret;
1786e3adcf8fSFrançois Tigeot 
1787e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
1788686a02f1SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1789b5c29a34SFrançois Tigeot 			MI_BATCH_GTT |
1790477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
1791477eb7f9SFrançois Tigeot 			 0 : MI_BATCH_NON_SECURE_I965));
1792e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1793e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1794e3adcf8fSFrançois Tigeot 
1795e3adcf8fSFrançois Tigeot 	return 0;
1796e3adcf8fSFrançois Tigeot }
1797e3adcf8fSFrançois Tigeot 
1798b5c29a34SFrançois Tigeot /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1799b5c29a34SFrançois Tigeot #define I830_BATCH_LIMIT (256*1024)
180024edb884SFrançois Tigeot #define I830_TLB_ENTRIES (2)
180124edb884SFrançois Tigeot #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
1802e3adcf8fSFrançois Tigeot static int
1803a05eeebfSFrançois Tigeot i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
1804ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1805477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1806e3adcf8fSFrançois Tigeot {
1807a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
180824edb884SFrançois Tigeot 	u32 cs_offset = ring->scratch.gtt_offset;
1809e3adcf8fSFrançois Tigeot 	int ret;
1810e3adcf8fSFrançois Tigeot 
1811a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 6);
181224edb884SFrançois Tigeot 	if (ret)
181324edb884SFrançois Tigeot 		return ret;
181424edb884SFrançois Tigeot 
181524edb884SFrançois Tigeot 	/* Evict the invalid PTE TLBs */
181624edb884SFrançois Tigeot 	intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
181724edb884SFrançois Tigeot 	intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
181824edb884SFrançois Tigeot 	intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
181924edb884SFrançois Tigeot 	intel_ring_emit(ring, cs_offset);
182024edb884SFrançois Tigeot 	intel_ring_emit(ring, 0xdeadbeef);
182124edb884SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
182224edb884SFrançois Tigeot 	intel_ring_advance(ring);
182324edb884SFrançois Tigeot 
1824477eb7f9SFrançois Tigeot 	if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
182524edb884SFrançois Tigeot 		if (len > I830_BATCH_LIMIT)
182624edb884SFrançois Tigeot 			return -ENOSPC;
182724edb884SFrançois Tigeot 
1828a05eeebfSFrançois Tigeot 		ret = intel_ring_begin(req, 6 + 2);
182924edb884SFrançois Tigeot 		if (ret)
183024edb884SFrançois Tigeot 			return ret;
183124edb884SFrançois Tigeot 
183224edb884SFrançois Tigeot 		/* Blit the batch (which has now all relocs applied) to the
183324edb884SFrançois Tigeot 		 * stable batch scratch bo area (so that the CS never
183424edb884SFrançois Tigeot 		 * stumbles over its tlb invalidation bug) ...
183524edb884SFrançois Tigeot 		 */
183624edb884SFrançois Tigeot 		intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
183724edb884SFrançois Tigeot 		intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
183824edb884SFrançois Tigeot 		intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
183924edb884SFrançois Tigeot 		intel_ring_emit(ring, cs_offset);
184024edb884SFrançois Tigeot 		intel_ring_emit(ring, 4096);
184124edb884SFrançois Tigeot 		intel_ring_emit(ring, offset);
184224edb884SFrançois Tigeot 
184324edb884SFrançois Tigeot 		intel_ring_emit(ring, MI_FLUSH);
184424edb884SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
184524edb884SFrançois Tigeot 		intel_ring_advance(ring);
184624edb884SFrançois Tigeot 
184724edb884SFrançois Tigeot 		/* ... and execute it. */
184824edb884SFrançois Tigeot 		offset = cs_offset;
184924edb884SFrançois Tigeot 	}
185024edb884SFrançois Tigeot 
1851a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
1852e3adcf8fSFrançois Tigeot 	if (ret)
1853e3adcf8fSFrançois Tigeot 		return ret;
1854e3adcf8fSFrançois Tigeot 
1855e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER);
1856477eb7f9SFrançois Tigeot 	intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1857477eb7f9SFrançois Tigeot 					0 : MI_BATCH_NON_SECURE));
1858e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset + len - 8);
1859b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1860686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
1861686a02f1SFrançois Tigeot 
1862686a02f1SFrançois Tigeot 	return 0;
1863686a02f1SFrançois Tigeot }
1864686a02f1SFrançois Tigeot 
1865686a02f1SFrançois Tigeot static int
1866a05eeebfSFrançois Tigeot i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
1867ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1868477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1869686a02f1SFrançois Tigeot {
1870a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1871686a02f1SFrançois Tigeot 	int ret;
1872686a02f1SFrançois Tigeot 
1873a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
1874e3adcf8fSFrançois Tigeot 	if (ret)
1875e3adcf8fSFrançois Tigeot 		return ret;
1876e3adcf8fSFrançois Tigeot 
1877686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1878477eb7f9SFrançois Tigeot 	intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1879477eb7f9SFrançois Tigeot 					0 : MI_BATCH_NON_SECURE));
1880e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1881e3adcf8fSFrançois Tigeot 
1882e3adcf8fSFrançois Tigeot 	return 0;
1883e3adcf8fSFrançois Tigeot }
1884e3adcf8fSFrançois Tigeot 
1885ba55f2f5SFrançois Tigeot static void cleanup_status_page(struct intel_engine_cs *ring)
1886e3adcf8fSFrançois Tigeot {
1887e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1888e3adcf8fSFrançois Tigeot 
1889e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
1890e3adcf8fSFrançois Tigeot 	if (obj == NULL)
1891e3adcf8fSFrançois Tigeot 		return;
1892e3adcf8fSFrançois Tigeot 
18937ec9f8e5SFrançois Tigeot 	kunmap(sg_page(obj->pages->sgl));
1894ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(obj);
1895e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1896e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
1897e3adcf8fSFrançois Tigeot }
1898e3adcf8fSFrançois Tigeot 
1899ba55f2f5SFrançois Tigeot static int init_status_page(struct intel_engine_cs *ring)
1900e3adcf8fSFrançois Tigeot {
1901e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1902ba55f2f5SFrançois Tigeot 
1903ba55f2f5SFrançois Tigeot 	if ((obj = ring->status_page.obj) == NULL) {
190424edb884SFrançois Tigeot 		unsigned flags;
1905e3adcf8fSFrançois Tigeot 		int ret;
1906e3adcf8fSFrançois Tigeot 
1907ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(ring->dev, 4096);
1908e3adcf8fSFrançois Tigeot 		if (obj == NULL) {
1909e3adcf8fSFrançois Tigeot 			DRM_ERROR("Failed to allocate status page\n");
1910ba55f2f5SFrançois Tigeot 			return -ENOMEM;
1911e3adcf8fSFrançois Tigeot 		}
1912e3adcf8fSFrançois Tigeot 
1913ba55f2f5SFrançois Tigeot 		ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1914ba55f2f5SFrançois Tigeot 		if (ret)
1915e3adcf8fSFrançois Tigeot 			goto err_unref;
1916ba55f2f5SFrançois Tigeot 
191724edb884SFrançois Tigeot 		flags = 0;
191824edb884SFrançois Tigeot 		if (!HAS_LLC(ring->dev))
191924edb884SFrançois Tigeot 			/* On g33, we cannot place HWS above 256MiB, so
192024edb884SFrançois Tigeot 			 * restrict its pinning to the low mappable arena.
192124edb884SFrançois Tigeot 			 * Though this restriction is not documented for
192224edb884SFrançois Tigeot 			 * gen4, gen5, or byt, they also behave similarly
192324edb884SFrançois Tigeot 			 * and hang if the HWS is placed at the top of the
192424edb884SFrançois Tigeot 			 * GTT. To generalise, it appears that all !llc
192524edb884SFrançois Tigeot 			 * platforms have issues with us placing the HWS
192624edb884SFrançois Tigeot 			 * above the mappable region (even though we never
192724edb884SFrançois Tigeot 			 * actualy map it).
192824edb884SFrançois Tigeot 			 */
192924edb884SFrançois Tigeot 			flags |= PIN_MAPPABLE;
193024edb884SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
1931ba55f2f5SFrançois Tigeot 		if (ret) {
1932ba55f2f5SFrançois Tigeot err_unref:
1933ba55f2f5SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
1934ba55f2f5SFrançois Tigeot 			return ret;
1935ba55f2f5SFrançois Tigeot 		}
1936ba55f2f5SFrançois Tigeot 
1937ba55f2f5SFrançois Tigeot 		ring->status_page.obj = obj;
1938e3adcf8fSFrançois Tigeot 	}
1939e3adcf8fSFrançois Tigeot 
19409edbd4a0SFrançois Tigeot 	ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
19417ec9f8e5SFrançois Tigeot 	ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
1942e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1943e3adcf8fSFrançois Tigeot 
1944b5c29a34SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1945e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1946e3adcf8fSFrançois Tigeot 
1947e3adcf8fSFrançois Tigeot 	return 0;
1948e3adcf8fSFrançois Tigeot }
1949e3adcf8fSFrançois Tigeot 
1950ba55f2f5SFrançois Tigeot static int init_phys_status_page(struct intel_engine_cs *ring)
1951686a02f1SFrançois Tigeot {
1952686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1953686a02f1SFrançois Tigeot 
1954686a02f1SFrançois Tigeot 	if (!dev_priv->status_page_dmah) {
1955686a02f1SFrançois Tigeot 		dev_priv->status_page_dmah =
1956b31e9d59SFrançois Tigeot 			drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1957686a02f1SFrançois Tigeot 		if (!dev_priv->status_page_dmah)
1958686a02f1SFrançois Tigeot 			return -ENOMEM;
1959686a02f1SFrançois Tigeot 	}
1960686a02f1SFrançois Tigeot 
1961686a02f1SFrançois Tigeot 	ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1962686a02f1SFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1963686a02f1SFrançois Tigeot 
1964686a02f1SFrançois Tigeot 	return 0;
1965686a02f1SFrançois Tigeot }
1966686a02f1SFrançois Tigeot 
19672c9916cdSFrançois Tigeot void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
19682c9916cdSFrançois Tigeot {
196924409b39SFrançois Tigeot 	iounmap(ringbuf->virtual_start);
19702c9916cdSFrançois Tigeot 	ringbuf->virtual_start = NULL;
19712c9916cdSFrançois Tigeot 	i915_gem_object_ggtt_unpin(ringbuf->obj);
19722c9916cdSFrançois Tigeot }
19732c9916cdSFrançois Tigeot 
19742c9916cdSFrançois Tigeot int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
19752c9916cdSFrançois Tigeot 				     struct intel_ringbuffer *ringbuf)
19762c9916cdSFrançois Tigeot {
19772c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
19782c9916cdSFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
19792c9916cdSFrançois Tigeot 	int ret;
19802c9916cdSFrançois Tigeot 
19812c9916cdSFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
19822c9916cdSFrançois Tigeot 	if (ret)
19832c9916cdSFrançois Tigeot 		return ret;
19842c9916cdSFrançois Tigeot 
19852c9916cdSFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
19862c9916cdSFrançois Tigeot 	if (ret) {
19872c9916cdSFrançois Tigeot 		i915_gem_object_ggtt_unpin(obj);
19882c9916cdSFrançois Tigeot 		return ret;
19892c9916cdSFrançois Tigeot 	}
19902c9916cdSFrançois Tigeot 
19912c9916cdSFrançois Tigeot 	ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
19922c9916cdSFrançois Tigeot 			i915_gem_obj_ggtt_offset(obj), ringbuf->size);
19932c9916cdSFrançois Tigeot 	if (ringbuf->virtual_start == NULL) {
19942c9916cdSFrançois Tigeot 		i915_gem_object_ggtt_unpin(obj);
19952c9916cdSFrançois Tigeot 		return -EINVAL;
19962c9916cdSFrançois Tigeot 	}
19972c9916cdSFrançois Tigeot 
19982c9916cdSFrançois Tigeot 	return 0;
19992c9916cdSFrançois Tigeot }
20002c9916cdSFrançois Tigeot 
20011b13d190SFrançois Tigeot void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
2002e3adcf8fSFrançois Tigeot {
200324edb884SFrançois Tigeot 	drm_gem_object_unreference(&ringbuf->obj->base);
200424edb884SFrançois Tigeot 	ringbuf->obj = NULL;
200524edb884SFrançois Tigeot }
200624edb884SFrançois Tigeot 
20071b13d190SFrançois Tigeot int intel_alloc_ringbuffer_obj(struct drm_device *dev,
200824edb884SFrançois Tigeot 			       struct intel_ringbuffer *ringbuf)
200924edb884SFrançois Tigeot {
2010e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
2011e3adcf8fSFrançois Tigeot 
2012a2fdbec6SFrançois Tigeot 	obj = NULL;
2013a2fdbec6SFrançois Tigeot 	if (!HAS_LLC(dev))
2014ba55f2f5SFrançois Tigeot 		obj = i915_gem_object_create_stolen(dev, ringbuf->size);
2015a2fdbec6SFrançois Tigeot 	if (obj == NULL)
2016ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, ringbuf->size);
2017ba55f2f5SFrançois Tigeot 	if (obj == NULL)
2018ba55f2f5SFrançois Tigeot 		return -ENOMEM;
2019e3adcf8fSFrançois Tigeot 
202024edb884SFrançois Tigeot 	/* mark ring buffers as read-only from GPU side by default */
202124edb884SFrançois Tigeot 	obj->gt_ro = 1;
202224edb884SFrançois Tigeot 
2023ba55f2f5SFrançois Tigeot 	ringbuf->obj = obj;
2024ba55f2f5SFrançois Tigeot 
20252c9916cdSFrançois Tigeot 	return 0;
2026ba55f2f5SFrançois Tigeot }
2027ba55f2f5SFrançois Tigeot 
2028ba55f2f5SFrançois Tigeot static int intel_init_ring_buffer(struct drm_device *dev,
2029ba55f2f5SFrançois Tigeot 				  struct intel_engine_cs *ring)
2030ba55f2f5SFrançois Tigeot {
20312c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf;
2032ba55f2f5SFrançois Tigeot 	int ret;
2033ba55f2f5SFrançois Tigeot 
20342c9916cdSFrançois Tigeot 	WARN_ON(ring->buffer);
20352c9916cdSFrançois Tigeot 
2036ba55f2f5SFrançois Tigeot 	ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2037ba55f2f5SFrançois Tigeot 	if (!ringbuf)
2038ba55f2f5SFrançois Tigeot 		return -ENOMEM;
2039ba55f2f5SFrançois Tigeot 	ring->buffer = ringbuf;
2040ba55f2f5SFrançois Tigeot 
2041ba55f2f5SFrançois Tigeot 	ring->dev = dev;
2042ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
2043ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
20441b13d190SFrançois Tigeot 	INIT_LIST_HEAD(&ring->execlist_queue);
204519c468b4SFrançois Tigeot 	i915_gem_batch_pool_init(dev, &ring->batch_pool);
2046ba55f2f5SFrançois Tigeot 	ringbuf->size = 32 * PAGE_SIZE;
20471b13d190SFrançois Tigeot 	ringbuf->ring = ring;
2048ba55f2f5SFrançois Tigeot 	memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
2049ba55f2f5SFrançois Tigeot 
2050ba55f2f5SFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
2051ba55f2f5SFrançois Tigeot 
2052ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
2053ba55f2f5SFrançois Tigeot 		ret = init_status_page(ring);
2054e3adcf8fSFrançois Tigeot 		if (ret)
2055ba55f2f5SFrançois Tigeot 			goto error;
2056ba55f2f5SFrançois Tigeot 	} else {
2057ba55f2f5SFrançois Tigeot 		BUG_ON(ring->id != RCS);
2058ba55f2f5SFrançois Tigeot 		ret = init_phys_status_page(ring);
2059ba55f2f5SFrançois Tigeot 		if (ret)
2060ba55f2f5SFrançois Tigeot 			goto error;
2061ba55f2f5SFrançois Tigeot 	}
2062ba55f2f5SFrançois Tigeot 
20632c9916cdSFrançois Tigeot 	WARN_ON(ringbuf->obj);
20642c9916cdSFrançois Tigeot 
206524edb884SFrançois Tigeot 	ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2066ba55f2f5SFrançois Tigeot 	if (ret) {
20672c9916cdSFrançois Tigeot 		DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
20682c9916cdSFrançois Tigeot 				ring->name, ret);
20692c9916cdSFrançois Tigeot 		goto error;
20702c9916cdSFrançois Tigeot 	}
20712c9916cdSFrançois Tigeot 
20722c9916cdSFrançois Tigeot 	ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
20732c9916cdSFrançois Tigeot 	if (ret) {
20742c9916cdSFrançois Tigeot 		DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
20752c9916cdSFrançois Tigeot 				ring->name, ret);
20762c9916cdSFrançois Tigeot 		intel_destroy_ringbuffer_obj(ringbuf);
2077ba55f2f5SFrançois Tigeot 		goto error;
2078ba55f2f5SFrançois Tigeot 	}
2079e3adcf8fSFrançois Tigeot 
2080e3adcf8fSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
2081e3adcf8fSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
2082e3adcf8fSFrançois Tigeot 	 * of the buffer.
2083e3adcf8fSFrançois Tigeot 	 */
2084ba55f2f5SFrançois Tigeot 	ringbuf->effective_size = ringbuf->size;
2085ba55f2f5SFrançois Tigeot 	if (IS_I830(dev) || IS_845G(dev))
2086ba55f2f5SFrançois Tigeot 		ringbuf->effective_size -= 2 * CACHELINE_BYTES;
2087ba55f2f5SFrançois Tigeot 
2088ba55f2f5SFrançois Tigeot 	ret = i915_cmd_parser_init_ring(ring);
2089ba55f2f5SFrançois Tigeot 	if (ret)
2090ba55f2f5SFrançois Tigeot 		goto error;
2091ba55f2f5SFrançois Tigeot 
2092e3adcf8fSFrançois Tigeot 	return 0;
2093e3adcf8fSFrançois Tigeot 
2094ba55f2f5SFrançois Tigeot error:
2095ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
2096ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
2097e3adcf8fSFrançois Tigeot 	return ret;
2098e3adcf8fSFrançois Tigeot }
2099e3adcf8fSFrançois Tigeot 
2100ba55f2f5SFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
2101e3adcf8fSFrançois Tigeot {
21022c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv;
21032c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf;
2104e3adcf8fSFrançois Tigeot 
2105ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
2106e3adcf8fSFrançois Tigeot 		return;
2107e3adcf8fSFrançois Tigeot 
21082c9916cdSFrançois Tigeot 	dev_priv = to_i915(ring->dev);
21092c9916cdSFrançois Tigeot 	ringbuf = ring->buffer;
21102c9916cdSFrançois Tigeot 
2111ba55f2f5SFrançois Tigeot 	intel_stop_ring_buffer(ring);
2112ba55f2f5SFrançois Tigeot 	WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
2113b030f26bSFrançois Tigeot 
21142c9916cdSFrançois Tigeot 	intel_unpin_ringbuffer_obj(ringbuf);
211524edb884SFrançois Tigeot 	intel_destroy_ringbuffer_obj(ringbuf);
2116e3adcf8fSFrançois Tigeot 
2117e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
2118e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
2119e3adcf8fSFrançois Tigeot 
2120e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
2121ba55f2f5SFrançois Tigeot 
2122ba55f2f5SFrançois Tigeot 	i915_cmd_parser_fini_ring(ring);
212319c468b4SFrançois Tigeot 	i915_gem_batch_pool_fini(&ring->batch_pool);
2124ba55f2f5SFrançois Tigeot 
2125ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
2126ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
2127e3adcf8fSFrançois Tigeot }
2128e3adcf8fSFrançois Tigeot 
212919c468b4SFrançois Tigeot static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
2130e3adcf8fSFrançois Tigeot {
2131ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2132e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
213319c468b4SFrançois Tigeot 	unsigned space;
2134e3adcf8fSFrançois Tigeot 	int ret;
2135e3adcf8fSFrançois Tigeot 
21362c9916cdSFrançois Tigeot 	if (intel_ring_space(ringbuf) >= n)
2137e3adcf8fSFrançois Tigeot 		return 0;
2138e3adcf8fSFrançois Tigeot 
2139a05eeebfSFrançois Tigeot 	/* The whole point of reserving space is to not wait! */
2140a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_in_use);
2141a05eeebfSFrançois Tigeot 
2142e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
214319c468b4SFrançois Tigeot 		space = __intel_ring_space(request->postfix, ringbuf->tail,
214419c468b4SFrançois Tigeot 					   ringbuf->size);
214519c468b4SFrançois Tigeot 		if (space >= n)
2146e3adcf8fSFrançois Tigeot 			break;
2147e3adcf8fSFrançois Tigeot 	}
2148e3adcf8fSFrançois Tigeot 
214919c468b4SFrançois Tigeot 	if (WARN_ON(&request->list == &ring->request_list))
2150e3adcf8fSFrançois Tigeot 		return -ENOSPC;
2151e3adcf8fSFrançois Tigeot 
21522c9916cdSFrançois Tigeot 	ret = i915_wait_request(request);
2153e3adcf8fSFrançois Tigeot 	if (ret)
2154e3adcf8fSFrançois Tigeot 		return ret;
2155e3adcf8fSFrançois Tigeot 
215619c468b4SFrançois Tigeot 	ringbuf->space = space;
2157e3adcf8fSFrançois Tigeot 	return 0;
2158e3adcf8fSFrançois Tigeot }
2159e3adcf8fSFrançois Tigeot 
2160a05eeebfSFrançois Tigeot static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
2161b030f26bSFrançois Tigeot {
2162b030f26bSFrançois Tigeot 	uint32_t __iomem *virt;
2163ba55f2f5SFrançois Tigeot 	int rem = ringbuf->size - ringbuf->tail;
2164b030f26bSFrançois Tigeot 
2165ba55f2f5SFrançois Tigeot 	virt = (unsigned int *)((char *)ringbuf->virtual_start + ringbuf->tail);
2166b030f26bSFrançois Tigeot 	rem /= 4;
2167b030f26bSFrançois Tigeot 	while (rem--)
2168686a02f1SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
2169b030f26bSFrançois Tigeot 
2170ba55f2f5SFrançois Tigeot 	ringbuf->tail = 0;
21712c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
2172b030f26bSFrançois Tigeot }
2173b030f26bSFrançois Tigeot 
2174ba55f2f5SFrançois Tigeot int intel_ring_idle(struct intel_engine_cs *ring)
2175b030f26bSFrançois Tigeot {
21762c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *req;
2177b5c29a34SFrançois Tigeot 
2178b5c29a34SFrançois Tigeot 	/* Wait upon the last request to be completed */
2179b5c29a34SFrançois Tigeot 	if (list_empty(&ring->request_list))
2180b5c29a34SFrançois Tigeot 		return 0;
2181b5c29a34SFrançois Tigeot 
21822c9916cdSFrançois Tigeot 	req = list_entry(ring->request_list.prev,
2183b5c29a34SFrançois Tigeot 			struct drm_i915_gem_request,
21842c9916cdSFrançois Tigeot 			list);
2185b5c29a34SFrançois Tigeot 
218619c468b4SFrançois Tigeot 	/* Make sure we do not trigger any retires */
218719c468b4SFrançois Tigeot 	return __i915_wait_request(req,
218819c468b4SFrançois Tigeot 				   atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
218919c468b4SFrançois Tigeot 				   to_i915(ring->dev)->mm.interruptible,
219019c468b4SFrançois Tigeot 				   NULL, NULL);
2191b5c29a34SFrançois Tigeot }
2192b5c29a34SFrançois Tigeot 
219319c468b4SFrançois Tigeot int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
2194b5c29a34SFrançois Tigeot {
219519c468b4SFrançois Tigeot 	request->ringbuf = request->ring->buffer;
21962c9916cdSFrançois Tigeot 	return 0;
21979edbd4a0SFrançois Tigeot }
21989edbd4a0SFrançois Tigeot 
2199a05eeebfSFrançois Tigeot int intel_ring_reserve_space(struct drm_i915_gem_request *request)
2200a2fdbec6SFrançois Tigeot {
2201a05eeebfSFrançois Tigeot 	/*
2202a05eeebfSFrançois Tigeot 	 * The first call merely notes the reserve request and is common for
2203a05eeebfSFrançois Tigeot 	 * all back ends. The subsequent localised _begin() call actually
2204a05eeebfSFrançois Tigeot 	 * ensures that the reservation is available. Without the begin, if
2205a05eeebfSFrançois Tigeot 	 * the request creator immediately submitted the request without
2206a05eeebfSFrançois Tigeot 	 * adding any commands to it then there might not actually be
2207a05eeebfSFrançois Tigeot 	 * sufficient room for the submission commands.
2208a05eeebfSFrançois Tigeot 	 */
2209a05eeebfSFrançois Tigeot 	intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
2210a2fdbec6SFrançois Tigeot 
2211a05eeebfSFrançois Tigeot 	return intel_ring_begin(request, 0);
2212a2fdbec6SFrançois Tigeot }
2213a2fdbec6SFrançois Tigeot 
2214a05eeebfSFrançois Tigeot void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2215a05eeebfSFrançois Tigeot {
2216a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_size);
2217a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_in_use);
2218a05eeebfSFrançois Tigeot 
2219a05eeebfSFrançois Tigeot 	ringbuf->reserved_size = size;
2220a05eeebfSFrançois Tigeot }
2221a05eeebfSFrançois Tigeot 
2222a05eeebfSFrançois Tigeot void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2223a05eeebfSFrançois Tigeot {
2224a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_in_use);
2225a05eeebfSFrançois Tigeot 
2226a05eeebfSFrançois Tigeot 	ringbuf->reserved_size   = 0;
2227a05eeebfSFrançois Tigeot 	ringbuf->reserved_in_use = false;
2228a05eeebfSFrançois Tigeot }
2229a05eeebfSFrançois Tigeot 
2230a05eeebfSFrançois Tigeot void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2231a05eeebfSFrançois Tigeot {
2232a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_in_use);
2233a05eeebfSFrançois Tigeot 
2234a05eeebfSFrançois Tigeot 	ringbuf->reserved_in_use = true;
2235a05eeebfSFrançois Tigeot 	ringbuf->reserved_tail   = ringbuf->tail;
2236a05eeebfSFrançois Tigeot }
2237a05eeebfSFrançois Tigeot 
2238a05eeebfSFrançois Tigeot void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2239a05eeebfSFrançois Tigeot {
2240a05eeebfSFrançois Tigeot 	WARN_ON(!ringbuf->reserved_in_use);
2241a05eeebfSFrançois Tigeot 	if (ringbuf->tail > ringbuf->reserved_tail) {
2242a05eeebfSFrançois Tigeot 		WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2243a05eeebfSFrançois Tigeot 		     "request reserved size too small: %d vs %d!\n",
2244a05eeebfSFrançois Tigeot 		     ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2245a05eeebfSFrançois Tigeot 	} else {
2246a05eeebfSFrançois Tigeot 		/*
2247a05eeebfSFrançois Tigeot 		 * The ring was wrapped while the reserved space was in use.
2248a05eeebfSFrançois Tigeot 		 * That means that some unknown amount of the ring tail was
2249a05eeebfSFrançois Tigeot 		 * no-op filled and skipped. Thus simply adding the ring size
2250a05eeebfSFrançois Tigeot 		 * to the tail and doing the above space check will not work.
2251a05eeebfSFrançois Tigeot 		 * Rather than attempt to track how much tail was skipped,
2252a05eeebfSFrançois Tigeot 		 * it is much simpler to say that also skipping the sanity
2253a05eeebfSFrançois Tigeot 		 * check every once in a while is not a big issue.
2254a05eeebfSFrançois Tigeot 		 */
2255a05eeebfSFrançois Tigeot 	}
2256a05eeebfSFrançois Tigeot 
2257a05eeebfSFrançois Tigeot 	ringbuf->reserved_size   = 0;
2258a05eeebfSFrançois Tigeot 	ringbuf->reserved_in_use = false;
2259a05eeebfSFrançois Tigeot }
2260a05eeebfSFrançois Tigeot 
2261a05eeebfSFrançois Tigeot static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
2262a05eeebfSFrançois Tigeot {
2263a05eeebfSFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2264a05eeebfSFrançois Tigeot 	int remain_usable = ringbuf->effective_size - ringbuf->tail;
2265a05eeebfSFrançois Tigeot 	int remain_actual = ringbuf->size - ringbuf->tail;
2266a05eeebfSFrançois Tigeot 	int ret, total_bytes, wait_bytes = 0;
2267a05eeebfSFrançois Tigeot 	bool need_wrap = false;
2268a05eeebfSFrançois Tigeot 
2269a05eeebfSFrançois Tigeot 	if (ringbuf->reserved_in_use)
2270a05eeebfSFrançois Tigeot 		total_bytes = bytes;
2271a05eeebfSFrançois Tigeot 	else
2272a05eeebfSFrançois Tigeot 		total_bytes = bytes + ringbuf->reserved_size;
2273a05eeebfSFrançois Tigeot 
2274a05eeebfSFrançois Tigeot 	if (unlikely(bytes > remain_usable)) {
2275a05eeebfSFrançois Tigeot 		/*
2276a05eeebfSFrançois Tigeot 		 * Not enough space for the basic request. So need to flush
2277a05eeebfSFrançois Tigeot 		 * out the remainder and then wait for base + reserved.
2278a05eeebfSFrançois Tigeot 		 */
2279a05eeebfSFrançois Tigeot 		wait_bytes = remain_actual + total_bytes;
2280a05eeebfSFrançois Tigeot 		need_wrap = true;
2281a05eeebfSFrançois Tigeot 	} else {
2282a05eeebfSFrançois Tigeot 		if (unlikely(total_bytes > remain_usable)) {
2283a05eeebfSFrançois Tigeot 			/*
2284a05eeebfSFrançois Tigeot 			 * The base request will fit but the reserved space
2285a05eeebfSFrançois Tigeot 			 * falls off the end. So only need to to wait for the
2286a05eeebfSFrançois Tigeot 			 * reserved size after flushing out the remainder.
2287a05eeebfSFrançois Tigeot 			 */
2288a05eeebfSFrançois Tigeot 			wait_bytes = remain_actual + ringbuf->reserved_size;
2289a05eeebfSFrançois Tigeot 			need_wrap = true;
2290a05eeebfSFrançois Tigeot 		} else if (total_bytes > ringbuf->space) {
2291a05eeebfSFrançois Tigeot 			/* No wrapping required, just waiting. */
2292a05eeebfSFrançois Tigeot 			wait_bytes = total_bytes;
2293a05eeebfSFrançois Tigeot 		}
2294a05eeebfSFrançois Tigeot 	}
2295a05eeebfSFrançois Tigeot 
2296a05eeebfSFrançois Tigeot 	if (wait_bytes) {
2297a05eeebfSFrançois Tigeot 		ret = ring_wait_for_space(ring, wait_bytes);
2298a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
2299a2fdbec6SFrançois Tigeot 			return ret;
2300a05eeebfSFrançois Tigeot 
2301a05eeebfSFrançois Tigeot 		if (need_wrap)
2302a05eeebfSFrançois Tigeot 			__wrap_ring_buffer(ringbuf);
2303a2fdbec6SFrançois Tigeot 	}
2304a2fdbec6SFrançois Tigeot 
2305a2fdbec6SFrançois Tigeot 	return 0;
2306a2fdbec6SFrançois Tigeot }
2307a2fdbec6SFrançois Tigeot 
2308a05eeebfSFrançois Tigeot int intel_ring_begin(struct drm_i915_gem_request *req,
2309e3adcf8fSFrançois Tigeot 		     int num_dwords)
2310e3adcf8fSFrançois Tigeot {
2311a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring;
2312a05eeebfSFrançois Tigeot 	struct drm_i915_private *dev_priv;
2313e3adcf8fSFrançois Tigeot 	int ret;
2314e3adcf8fSFrançois Tigeot 
2315a05eeebfSFrançois Tigeot 	WARN_ON(req == NULL);
2316a05eeebfSFrançois Tigeot 	ring = req->ring;
2317a05eeebfSFrançois Tigeot 	dev_priv = ring->dev->dev_private;
2318a05eeebfSFrançois Tigeot 
2319a2fdbec6SFrançois Tigeot 	ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2320a2fdbec6SFrançois Tigeot 				   dev_priv->mm.interruptible);
2321245593daSFrançois Tigeot 	if (ret)
2322245593daSFrançois Tigeot 		return ret;
2323e3adcf8fSFrançois Tigeot 
23249edbd4a0SFrançois Tigeot 	ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
23259edbd4a0SFrançois Tigeot 	if (ret)
23269edbd4a0SFrançois Tigeot 		return ret;
23279edbd4a0SFrançois Tigeot 
2328ba55f2f5SFrançois Tigeot 	ring->buffer->space -= num_dwords * sizeof(uint32_t);
23299edbd4a0SFrançois Tigeot 	return 0;
23309edbd4a0SFrançois Tigeot }
23319edbd4a0SFrançois Tigeot 
23329edbd4a0SFrançois Tigeot /* Align the ring tail to a cacheline boundary */
2333a05eeebfSFrançois Tigeot int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
23349edbd4a0SFrançois Tigeot {
2335a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2336ba55f2f5SFrançois Tigeot 	int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
23379edbd4a0SFrançois Tigeot 	int ret;
23389edbd4a0SFrançois Tigeot 
23399edbd4a0SFrançois Tigeot 	if (num_dwords == 0)
23409edbd4a0SFrançois Tigeot 		return 0;
23419edbd4a0SFrançois Tigeot 
2342ba55f2f5SFrançois Tigeot 	num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
2343a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, num_dwords);
23449edbd4a0SFrançois Tigeot 	if (ret)
23459edbd4a0SFrançois Tigeot 		return ret;
23469edbd4a0SFrançois Tigeot 
23479edbd4a0SFrançois Tigeot 	while (num_dwords--)
23489edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
23499edbd4a0SFrançois Tigeot 
23509edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
23519edbd4a0SFrançois Tigeot 
23529edbd4a0SFrançois Tigeot 	return 0;
2353e3adcf8fSFrançois Tigeot }
2354e3adcf8fSFrançois Tigeot 
2355ba55f2f5SFrançois Tigeot void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
2356a2fdbec6SFrançois Tigeot {
235724edb884SFrançois Tigeot 	struct drm_device *dev = ring->dev;
235824edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2359a2fdbec6SFrançois Tigeot 
236024edb884SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
2361a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2362a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
236324edb884SFrançois Tigeot 		if (HAS_VEBOX(dev))
23649edbd4a0SFrançois Tigeot 			I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
2365e3adcf8fSFrançois Tigeot 	}
2366e3adcf8fSFrançois Tigeot 
2367a2fdbec6SFrançois Tigeot 	ring->set_seqno(ring, seqno);
23685d0b1887SFrançois Tigeot 	ring->hangcheck.seqno = seqno;
2369e3adcf8fSFrançois Tigeot }
2370e3adcf8fSFrançois Tigeot 
2371ba55f2f5SFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
2372f4e1c372SFrançois Tigeot 				     u32 value)
2373e3adcf8fSFrançois Tigeot {
2374ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2375e3adcf8fSFrançois Tigeot 
2376e3adcf8fSFrançois Tigeot        /* Every tail move must follow the sequence below */
2377f4e1c372SFrançois Tigeot 
2378f4e1c372SFrançois Tigeot 	/* Disable notification that the ring is IDLE. The GT
2379f4e1c372SFrançois Tigeot 	 * will then assume that it is busy and bring it out of rc6.
2380f4e1c372SFrançois Tigeot 	 */
2381e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2382f4e1c372SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2383e3adcf8fSFrançois Tigeot 
2384f4e1c372SFrançois Tigeot 	/* Clear the context id. Here be magic! */
2385f4e1c372SFrançois Tigeot 	I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2386e3adcf8fSFrançois Tigeot 
2387f4e1c372SFrançois Tigeot 	/* Wait for the ring not to be idle, i.e. for it to wake up. */
2388f4e1c372SFrançois Tigeot 	if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
2389f4e1c372SFrançois Tigeot 		      GEN6_BSD_SLEEP_INDICATOR) == 0,
2390f4e1c372SFrançois Tigeot 		     50))
2391f4e1c372SFrançois Tigeot 		DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
2392f4e1c372SFrançois Tigeot 
2393f4e1c372SFrançois Tigeot 	/* Now that the ring is fully powered up, update the tail */
2394e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
2395f4e1c372SFrançois Tigeot 	POSTING_READ(RING_TAIL(ring->mmio_base));
2396f4e1c372SFrançois Tigeot 
2397f4e1c372SFrançois Tigeot 	/* Let the ring send IDLE messages to the GT again,
2398f4e1c372SFrançois Tigeot 	 * and so let it sleep to conserve power when idle.
2399f4e1c372SFrançois Tigeot 	 */
2400e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2401f4e1c372SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2402e3adcf8fSFrançois Tigeot }
2403e3adcf8fSFrançois Tigeot 
2404a05eeebfSFrançois Tigeot static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
2405b5c29a34SFrançois Tigeot 			       u32 invalidate, u32 flush)
2406e3adcf8fSFrançois Tigeot {
2407a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2408e3adcf8fSFrançois Tigeot 	uint32_t cmd;
2409e3adcf8fSFrançois Tigeot 	int ret;
2410e3adcf8fSFrançois Tigeot 
2411a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
2412e3adcf8fSFrançois Tigeot 	if (ret)
2413e3adcf8fSFrançois Tigeot 		return ret;
2414e3adcf8fSFrançois Tigeot 
2415e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
24169edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
24179edbd4a0SFrançois Tigeot 		cmd += 1;
24182c9916cdSFrançois Tigeot 
24192c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
24202c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
24212c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
24222c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
24232c9916cdSFrançois Tigeot 	 */
24242c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
24252c9916cdSFrançois Tigeot 
2426b5c29a34SFrançois Tigeot 	/*
2427b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.5 - video engine command streamer:
2428b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
2429b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
2430b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
2431b5c29a34SFrançois Tigeot 	 */
2432e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
24332c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
24342c9916cdSFrançois Tigeot 
2435e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
2436b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
24379edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
24389edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
24399edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
24409edbd4a0SFrançois Tigeot 	} else  {
24419edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0);
24429edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
24439edbd4a0SFrançois Tigeot 	}
24449edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
24459edbd4a0SFrançois Tigeot 	return 0;
24469edbd4a0SFrançois Tigeot }
24479edbd4a0SFrançois Tigeot 
24489edbd4a0SFrançois Tigeot static int
2449a05eeebfSFrançois Tigeot gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2450ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
2451477eb7f9SFrançois Tigeot 			      unsigned dispatch_flags)
24529edbd4a0SFrançois Tigeot {
2453a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2454477eb7f9SFrançois Tigeot 	bool ppgtt = USES_PPGTT(ring->dev) &&
2455477eb7f9SFrançois Tigeot 			!(dispatch_flags & I915_DISPATCH_SECURE);
24569edbd4a0SFrançois Tigeot 	int ret;
24579edbd4a0SFrançois Tigeot 
2458a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
24599edbd4a0SFrançois Tigeot 	if (ret)
24609edbd4a0SFrançois Tigeot 		return ret;
24619edbd4a0SFrançois Tigeot 
24629edbd4a0SFrançois Tigeot 	/* FIXME(BDW): Address space and security selectors. */
2463a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
2464a05eeebfSFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_RS ?
2465a05eeebfSFrançois Tigeot 			 MI_BATCH_RESOURCE_STREAMER : 0));
2466ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, lower_32_bits(offset));
2467ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, upper_32_bits(offset));
2468e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
2469e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
24709edbd4a0SFrançois Tigeot 
2471e3adcf8fSFrançois Tigeot 	return 0;
2472e3adcf8fSFrançois Tigeot }
2473e3adcf8fSFrançois Tigeot 
2474e3adcf8fSFrançois Tigeot static int
2475a05eeebfSFrançois Tigeot hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2476ba55f2f5SFrançois Tigeot 			     u64 offset, u32 len,
2477477eb7f9SFrançois Tigeot 			     unsigned dispatch_flags)
2478e3adcf8fSFrançois Tigeot {
2479a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2480e3adcf8fSFrançois Tigeot 	int ret;
2481e3adcf8fSFrançois Tigeot 
2482a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
2483e3adcf8fSFrançois Tigeot 	if (ret)
2484e3adcf8fSFrançois Tigeot 		return ret;
2485e3adcf8fSFrançois Tigeot 
2486b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
24871b13d190SFrançois Tigeot 			MI_BATCH_BUFFER_START |
2488477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
2489a05eeebfSFrançois Tigeot 			 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2490a05eeebfSFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_RS ?
2491a05eeebfSFrançois Tigeot 			 MI_BATCH_RESOURCE_STREAMER : 0));
2492b5c29a34SFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
2493b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset);
2494b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
2495b5c29a34SFrançois Tigeot 
2496b5c29a34SFrançois Tigeot 	return 0;
2497b5c29a34SFrançois Tigeot }
2498b5c29a34SFrançois Tigeot 
2499b5c29a34SFrançois Tigeot static int
2500a05eeebfSFrançois Tigeot gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2501ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
2502477eb7f9SFrançois Tigeot 			      unsigned dispatch_flags)
2503b5c29a34SFrançois Tigeot {
2504a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2505b5c29a34SFrançois Tigeot 	int ret;
2506b5c29a34SFrançois Tigeot 
2507a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
2508b5c29a34SFrançois Tigeot 	if (ret)
2509b5c29a34SFrançois Tigeot 		return ret;
2510b5c29a34SFrançois Tigeot 
2511b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
2512b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START |
2513477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
2514477eb7f9SFrançois Tigeot 			 0 : MI_BATCH_NON_SECURE_I965));
2515e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
2516e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
2517e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
2518e3adcf8fSFrançois Tigeot 
2519e3adcf8fSFrançois Tigeot 	return 0;
2520e3adcf8fSFrançois Tigeot }
2521e3adcf8fSFrançois Tigeot 
2522e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
2523e3adcf8fSFrançois Tigeot 
2524a05eeebfSFrançois Tigeot static int gen6_ring_flush(struct drm_i915_gem_request *req,
2525b5c29a34SFrançois Tigeot 			   u32 invalidate, u32 flush)
2526e3adcf8fSFrançois Tigeot {
2527a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
25285d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
2529e3adcf8fSFrançois Tigeot 	uint32_t cmd;
2530e3adcf8fSFrançois Tigeot 	int ret;
2531e3adcf8fSFrançois Tigeot 
2532a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
2533e3adcf8fSFrançois Tigeot 	if (ret)
2534e3adcf8fSFrançois Tigeot 		return ret;
2535e3adcf8fSFrançois Tigeot 
2536e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
2537477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8)
25389edbd4a0SFrançois Tigeot 		cmd += 1;
25392c9916cdSFrançois Tigeot 
25402c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
25412c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
25422c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
25432c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
25442c9916cdSFrançois Tigeot 	 */
25452c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
25462c9916cdSFrançois Tigeot 
2547b5c29a34SFrançois Tigeot 	/*
2548b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.3 - blitter engine command streamer:
2549b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
2550b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
2551b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
2552b5c29a34SFrançois Tigeot 	 */
2553e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
25542c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB;
2555e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
2556b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
2557477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
25589edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
25599edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
25609edbd4a0SFrançois Tigeot 	} else  {
2561e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, 0);
2562e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
25639edbd4a0SFrançois Tigeot 	}
2564e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
25655d0b1887SFrançois Tigeot 
2566e3adcf8fSFrançois Tigeot 	return 0;
2567e3adcf8fSFrançois Tigeot }
2568e3adcf8fSFrançois Tigeot 
2569e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
2570e3adcf8fSFrançois Tigeot {
2571ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2572ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
257324edb884SFrançois Tigeot 	struct drm_i915_gem_object *obj;
257424edb884SFrançois Tigeot 	int ret;
2575e3adcf8fSFrançois Tigeot 
2576686a02f1SFrançois Tigeot 	ring->name = "render ring";
2577686a02f1SFrançois Tigeot 	ring->id = RCS;
2578686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
2579686a02f1SFrançois Tigeot 
258024edb884SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
258124edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
258224edb884SFrançois Tigeot 			obj = i915_gem_alloc_object(dev, 4096);
258324edb884SFrançois Tigeot 			if (obj == NULL) {
258424edb884SFrançois Tigeot 				DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
258524edb884SFrançois Tigeot 				i915.semaphores = 0;
258624edb884SFrançois Tigeot 			} else {
258724edb884SFrançois Tigeot 				i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
258824edb884SFrançois Tigeot 				ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
258924edb884SFrançois Tigeot 				if (ret != 0) {
259024edb884SFrançois Tigeot 					drm_gem_object_unreference(&obj->base);
259124edb884SFrançois Tigeot 					DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
259224edb884SFrançois Tigeot 					i915.semaphores = 0;
259324edb884SFrançois Tigeot 				} else
259424edb884SFrançois Tigeot 					dev_priv->semaphore_obj = obj;
259524edb884SFrançois Tigeot 			}
259624edb884SFrançois Tigeot 		}
25972c9916cdSFrançois Tigeot 
25982c9916cdSFrançois Tigeot 		ring->init_context = intel_rcs_ctx_init;
259924edb884SFrançois Tigeot 		ring->add_request = gen6_add_request;
260024edb884SFrançois Tigeot 		ring->flush = gen8_render_ring_flush;
260124edb884SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
260224edb884SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
260324edb884SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
260424edb884SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
260524edb884SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
260624edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
260724edb884SFrançois Tigeot 			WARN_ON(!dev_priv->semaphore_obj);
260824edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
260924edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_rcs_signal;
261024edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
261124edb884SFrançois Tigeot 		}
261224edb884SFrançois Tigeot 	} else if (INTEL_INFO(dev)->gen >= 6) {
2613e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
2614b5c29a34SFrançois Tigeot 		ring->flush = gen7_render_ring_flush;
2615b5c29a34SFrançois Tigeot 		if (INTEL_INFO(dev)->gen == 6)
2616e3adcf8fSFrançois Tigeot 			ring->flush = gen6_render_ring_flush;
2617686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2618686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
26195d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2620e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2621a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
262224edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2623ba55f2f5SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2624ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
2625ba55f2f5SFrançois Tigeot 			/*
262624edb884SFrançois Tigeot 			 * The current semaphore is only applied on pre-gen8
262724edb884SFrançois Tigeot 			 * platform.  And there is no VCS2 ring on the pre-gen8
262824edb884SFrançois Tigeot 			 * platform. So the semaphore between RCS and VCS2 is
262924edb884SFrançois Tigeot 			 * initialized as INVALID.  Gen8 will initialize the
263024edb884SFrançois Tigeot 			 * sema between VCS2 and RCS later.
2631ba55f2f5SFrançois Tigeot 			 */
2632ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2633ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2634ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2635ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2636ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2637ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2638ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2639ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2640ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2641ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
264224edb884SFrançois Tigeot 		}
2643e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
2644e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
2645686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
2646e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
2647a2fdbec6SFrançois Tigeot 		ring->set_seqno = pc_render_set_seqno;
2648686a02f1SFrançois Tigeot 		ring->irq_get = gen5_ring_get_irq;
2649686a02f1SFrançois Tigeot 		ring->irq_put = gen5_ring_put_irq;
26505d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
26515d0b1887SFrançois Tigeot 					GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
2652686a02f1SFrançois Tigeot 	} else {
2653686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2654686a02f1SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 4)
2655686a02f1SFrançois Tigeot 			ring->flush = gen2_render_ring_flush;
2656686a02f1SFrançois Tigeot 		else
2657686a02f1SFrançois Tigeot 			ring->flush = gen4_render_ring_flush;
2658686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2659a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2660686a02f1SFrançois Tigeot 		if (IS_GEN2(dev)) {
2661686a02f1SFrançois Tigeot 			ring->irq_get = i8xx_ring_get_irq;
2662686a02f1SFrançois Tigeot 			ring->irq_put = i8xx_ring_put_irq;
2663686a02f1SFrançois Tigeot 		} else {
2664686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2665686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2666e3adcf8fSFrançois Tigeot 		}
2667686a02f1SFrançois Tigeot 		ring->irq_enable_mask = I915_USER_INTERRUPT;
2668686a02f1SFrançois Tigeot 	}
2669686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
267024edb884SFrançois Tigeot 
2671b5c29a34SFrançois Tigeot 	if (IS_HASWELL(dev))
2672b5c29a34SFrançois Tigeot 		ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
26739edbd4a0SFrançois Tigeot 	else if (IS_GEN8(dev))
26749edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2675b5c29a34SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 6)
2676686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2677686a02f1SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 4)
2678686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2679686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
2680686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2681686a02f1SFrançois Tigeot 	else
2682686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
26832c9916cdSFrançois Tigeot 	ring->init_hw = init_render_ring;
2684686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
2685e3adcf8fSFrançois Tigeot 
2686b5c29a34SFrançois Tigeot 	/* Workaround batchbuffer to combat CS tlb bug. */
2687b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev)) {
268824edb884SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
2689b5c29a34SFrançois Tigeot 		if (obj == NULL) {
2690b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to allocate batch bo\n");
2691b5c29a34SFrançois Tigeot 			return -ENOMEM;
2692b5c29a34SFrançois Tigeot 		}
2693b5c29a34SFrançois Tigeot 
2694ba55f2f5SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
2695b5c29a34SFrançois Tigeot 		if (ret != 0) {
2696b5c29a34SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
2697b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to ping batch bo\n");
2698b5c29a34SFrançois Tigeot 			return ret;
2699b5c29a34SFrançois Tigeot 		}
2700b5c29a34SFrançois Tigeot 
27019edbd4a0SFrançois Tigeot 		ring->scratch.obj = obj;
27029edbd4a0SFrançois Tigeot 		ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
2703e3adcf8fSFrançois Tigeot 	}
2704e3adcf8fSFrançois Tigeot 
27052c9916cdSFrançois Tigeot 	ret = intel_init_ring_buffer(dev, ring);
2706b5c29a34SFrançois Tigeot 	if (ret)
27072c9916cdSFrançois Tigeot 		return ret;
27082c9916cdSFrançois Tigeot 
27092c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
27102c9916cdSFrançois Tigeot 		ret = intel_init_pipe_control(ring);
27112c9916cdSFrançois Tigeot 		if (ret)
27122c9916cdSFrançois Tigeot 			return ret;
2713b5c29a34SFrançois Tigeot 	}
2714b5c29a34SFrançois Tigeot 
2715e3adcf8fSFrançois Tigeot 	return 0;
2716e3adcf8fSFrançois Tigeot }
2717e3adcf8fSFrançois Tigeot 
2718e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
2719e3adcf8fSFrançois Tigeot {
2720ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2721ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS];
2722e3adcf8fSFrançois Tigeot 
2723686a02f1SFrançois Tigeot 	ring->name = "bsd ring";
2724686a02f1SFrançois Tigeot 	ring->id = VCS;
2725686a02f1SFrançois Tigeot 
2726686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
27279edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
2728686a02f1SFrançois Tigeot 		ring->mmio_base = GEN6_BSD_RING_BASE;
2729686a02f1SFrançois Tigeot 		/* gen6 bsd needs a special wa for tail updates */
2730686a02f1SFrançois Tigeot 		if (IS_GEN6(dev))
2731686a02f1SFrançois Tigeot 			ring->write_tail = gen6_bsd_ring_write_tail;
27325d0b1887SFrançois Tigeot 		ring->flush = gen6_bsd_ring_flush;
2733686a02f1SFrançois Tigeot 		ring->add_request = gen6_add_request;
2734686a02f1SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2735a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
27369edbd4a0SFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 8) {
27379edbd4a0SFrançois Tigeot 			ring->irq_enable_mask =
27389edbd4a0SFrançois Tigeot 				GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
27399edbd4a0SFrançois Tigeot 			ring->irq_get = gen8_ring_get_irq;
27409edbd4a0SFrançois Tigeot 			ring->irq_put = gen8_ring_put_irq;
27419edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
27429edbd4a0SFrançois Tigeot 				gen8_ring_dispatch_execbuffer;
274324edb884SFrançois Tigeot 			if (i915_semaphore_is_enabled(dev)) {
274424edb884SFrançois Tigeot 				ring->semaphore.sync_to = gen8_ring_sync;
274524edb884SFrançois Tigeot 				ring->semaphore.signal = gen8_xcs_signal;
274624edb884SFrançois Tigeot 				GEN8_RING_SEMAPHORE_INIT;
274724edb884SFrançois Tigeot 			}
27489edbd4a0SFrançois Tigeot 		} else {
27495d0b1887SFrançois Tigeot 			ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2750686a02f1SFrançois Tigeot 			ring->irq_get = gen6_ring_get_irq;
2751686a02f1SFrançois Tigeot 			ring->irq_put = gen6_ring_put_irq;
27529edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
27539edbd4a0SFrançois Tigeot 				gen6_ring_dispatch_execbuffer;
275424edb884SFrançois Tigeot 			if (i915_semaphore_is_enabled(dev)) {
2755ba55f2f5SFrançois Tigeot 				ring->semaphore.sync_to = gen6_ring_sync;
2756ba55f2f5SFrançois Tigeot 				ring->semaphore.signal = gen6_signal;
2757ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2758ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2759ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2760ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2761ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2762ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2763ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2764ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2765ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2766ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
276724edb884SFrançois Tigeot 			}
276824edb884SFrançois Tigeot 		}
2769686a02f1SFrançois Tigeot 	} else {
2770686a02f1SFrançois Tigeot 		ring->mmio_base = BSD_RING_BASE;
2771686a02f1SFrançois Tigeot 		ring->flush = bsd_ring_flush;
2772686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2773686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2774a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2775686a02f1SFrançois Tigeot 		if (IS_GEN5(dev)) {
27765d0b1887SFrançois Tigeot 			ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2777686a02f1SFrançois Tigeot 			ring->irq_get = gen5_ring_get_irq;
2778686a02f1SFrançois Tigeot 			ring->irq_put = gen5_ring_put_irq;
2779686a02f1SFrançois Tigeot 		} else {
2780686a02f1SFrançois Tigeot 			ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2781686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2782686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2783686a02f1SFrançois Tigeot 		}
2784686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2785686a02f1SFrançois Tigeot 	}
27862c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2787e3adcf8fSFrançois Tigeot 
2788e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2789e3adcf8fSFrançois Tigeot }
2790e3adcf8fSFrançois Tigeot 
2791ba55f2f5SFrançois Tigeot /**
2792477eb7f9SFrançois Tigeot  * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
2793ba55f2f5SFrançois Tigeot  */
2794ba55f2f5SFrançois Tigeot int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2795ba55f2f5SFrançois Tigeot {
2796ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2797ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
2798ba55f2f5SFrançois Tigeot 
279924edb884SFrançois Tigeot 	ring->name = "bsd2 ring";
2800ba55f2f5SFrançois Tigeot 	ring->id = VCS2;
2801ba55f2f5SFrançois Tigeot 
2802ba55f2f5SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2803ba55f2f5SFrançois Tigeot 	ring->mmio_base = GEN8_BSD2_RING_BASE;
2804ba55f2f5SFrançois Tigeot 	ring->flush = gen6_bsd_ring_flush;
2805ba55f2f5SFrançois Tigeot 	ring->add_request = gen6_add_request;
2806ba55f2f5SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2807ba55f2f5SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
2808ba55f2f5SFrançois Tigeot 	ring->irq_enable_mask =
2809ba55f2f5SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2810ba55f2f5SFrançois Tigeot 	ring->irq_get = gen8_ring_get_irq;
2811ba55f2f5SFrançois Tigeot 	ring->irq_put = gen8_ring_put_irq;
2812ba55f2f5SFrançois Tigeot 	ring->dispatch_execbuffer =
2813ba55f2f5SFrançois Tigeot 			gen8_ring_dispatch_execbuffer;
281424edb884SFrançois Tigeot 	if (i915_semaphore_is_enabled(dev)) {
281524edb884SFrançois Tigeot 		ring->semaphore.sync_to = gen8_ring_sync;
281624edb884SFrançois Tigeot 		ring->semaphore.signal = gen8_xcs_signal;
281724edb884SFrançois Tigeot 		GEN8_RING_SEMAPHORE_INIT;
281824edb884SFrançois Tigeot 	}
28192c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2820ba55f2f5SFrançois Tigeot 
2821ba55f2f5SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2822ba55f2f5SFrançois Tigeot }
2823ba55f2f5SFrançois Tigeot 
2824e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
2825e3adcf8fSFrançois Tigeot {
2826ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2827ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[BCS];
2828e3adcf8fSFrançois Tigeot 
2829686a02f1SFrançois Tigeot 	ring->name = "blitter ring";
2830686a02f1SFrançois Tigeot 	ring->id = BCS;
2831686a02f1SFrançois Tigeot 
2832686a02f1SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
2833686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
28345d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
2835686a02f1SFrançois Tigeot 	ring->add_request = gen6_add_request;
2836686a02f1SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2837a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
28389edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
28399edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
28409edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
28419edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
28429edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
28439edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
284424edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
284524edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
284624edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_xcs_signal;
284724edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
284824edb884SFrançois Tigeot 		}
28499edbd4a0SFrançois Tigeot 	} else {
28505d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2851686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2852686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
2853686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
285424edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2855ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
285624edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2857ba55f2f5SFrançois Tigeot 			/*
285824edb884SFrançois Tigeot 			 * The current semaphore is only applied on pre-gen8
285924edb884SFrançois Tigeot 			 * platform.  And there is no VCS2 ring on the pre-gen8
286024edb884SFrançois Tigeot 			 * platform. So the semaphore between BCS and VCS2 is
286124edb884SFrançois Tigeot 			 * initialized as INVALID.  Gen8 will initialize the
286224edb884SFrançois Tigeot 			 * sema between BCS and VCS2 later.
2863ba55f2f5SFrançois Tigeot 			 */
2864ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2865ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2866ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2867ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2868ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2869ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2870ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2871ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2872ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2873ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
287424edb884SFrançois Tigeot 		}
287524edb884SFrançois Tigeot 	}
28762c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
28775d0b1887SFrançois Tigeot 
28785d0b1887SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
28795d0b1887SFrançois Tigeot }
28805d0b1887SFrançois Tigeot 
28815d0b1887SFrançois Tigeot int intel_init_vebox_ring_buffer(struct drm_device *dev)
28825d0b1887SFrançois Tigeot {
2883ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2884ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VECS];
28855d0b1887SFrançois Tigeot 
28865d0b1887SFrançois Tigeot 	ring->name = "video enhancement ring";
28875d0b1887SFrançois Tigeot 	ring->id = VECS;
28885d0b1887SFrançois Tigeot 
28895d0b1887SFrançois Tigeot 	ring->mmio_base = VEBOX_RING_BASE;
28905d0b1887SFrançois Tigeot 	ring->write_tail = ring_write_tail;
28915d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
28925d0b1887SFrançois Tigeot 	ring->add_request = gen6_add_request;
28935d0b1887SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
28945d0b1887SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
28959edbd4a0SFrançois Tigeot 
28969edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
28979edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
28989edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
28999edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
29009edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
29019edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
290224edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
290324edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
290424edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_xcs_signal;
290524edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
290624edb884SFrançois Tigeot 		}
29079edbd4a0SFrançois Tigeot 	} else {
29089edbd4a0SFrançois Tigeot 		ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
29095d0b1887SFrançois Tigeot 		ring->irq_get = hsw_vebox_get_irq;
29105d0b1887SFrançois Tigeot 		ring->irq_put = hsw_vebox_put_irq;
29115d0b1887SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
291224edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2913ba55f2f5SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2914ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
2915ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2916ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2917ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2918ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2919ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2920ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2921ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2922ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2923ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2924ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
292524edb884SFrançois Tigeot 		}
292624edb884SFrançois Tigeot 	}
29272c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2928e3adcf8fSFrançois Tigeot 
2929e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2930e3adcf8fSFrançois Tigeot }
2931b030f26bSFrançois Tigeot 
2932b030f26bSFrançois Tigeot int
2933a05eeebfSFrançois Tigeot intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
2934b030f26bSFrançois Tigeot {
2935a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2936b030f26bSFrançois Tigeot 	int ret;
2937b030f26bSFrançois Tigeot 
2938b030f26bSFrançois Tigeot 	if (!ring->gpu_caches_dirty)
2939b030f26bSFrançois Tigeot 		return 0;
2940b030f26bSFrançois Tigeot 
2941a05eeebfSFrançois Tigeot 	ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
2942b030f26bSFrançois Tigeot 	if (ret)
2943b030f26bSFrançois Tigeot 		return ret;
2944b030f26bSFrançois Tigeot 
2945a05eeebfSFrançois Tigeot 	trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
2946a2fdbec6SFrançois Tigeot 
2947b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2948b030f26bSFrançois Tigeot 	return 0;
2949b030f26bSFrançois Tigeot }
2950b030f26bSFrançois Tigeot 
2951b030f26bSFrançois Tigeot int
2952a05eeebfSFrançois Tigeot intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
2953b030f26bSFrançois Tigeot {
2954a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2955b030f26bSFrançois Tigeot 	uint32_t flush_domains;
2956b030f26bSFrançois Tigeot 	int ret;
2957b030f26bSFrançois Tigeot 
2958b030f26bSFrançois Tigeot 	flush_domains = 0;
2959b030f26bSFrançois Tigeot 	if (ring->gpu_caches_dirty)
2960b030f26bSFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
2961b030f26bSFrançois Tigeot 
2962a05eeebfSFrançois Tigeot 	ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
2963b030f26bSFrançois Tigeot 	if (ret)
2964b030f26bSFrançois Tigeot 		return ret;
2965b030f26bSFrançois Tigeot 
2966a05eeebfSFrançois Tigeot 	trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
2967a2fdbec6SFrançois Tigeot 
2968b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2969b030f26bSFrançois Tigeot 	return 0;
2970b030f26bSFrançois Tigeot }
2971ba55f2f5SFrançois Tigeot 
2972ba55f2f5SFrançois Tigeot void
2973ba55f2f5SFrançois Tigeot intel_stop_ring_buffer(struct intel_engine_cs *ring)
2974ba55f2f5SFrançois Tigeot {
2975ba55f2f5SFrançois Tigeot 	int ret;
2976ba55f2f5SFrançois Tigeot 
2977ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
2978ba55f2f5SFrançois Tigeot 		return;
2979ba55f2f5SFrançois Tigeot 
2980ba55f2f5SFrançois Tigeot 	ret = intel_ring_idle(ring);
2981ba55f2f5SFrançois Tigeot 	if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2982ba55f2f5SFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2983ba55f2f5SFrançois Tigeot 			  ring->name, ret);
2984ba55f2f5SFrançois Tigeot 
2985ba55f2f5SFrançois Tigeot 	stop_ring(ring);
2986ba55f2f5SFrançois Tigeot }
2987