xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision 24409b39a157c9c103a0e5debcf368727f712f34)
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 
84ba55f2f5SFrançois Tigeot 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
94ba55f2f5SFrançois Tigeot gen2_render_ring_flush(struct intel_engine_cs *ring,
95686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
96686a02f1SFrançois Tigeot 		       u32	flush_domains)
97686a02f1SFrançois Tigeot {
98686a02f1SFrançois Tigeot 	u32 cmd;
99686a02f1SFrançois Tigeot 	int ret;
100686a02f1SFrançois Tigeot 
101686a02f1SFrançois Tigeot 	cmd = MI_FLUSH;
102686a02f1SFrançois Tigeot 	if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
103686a02f1SFrançois Tigeot 		cmd |= MI_NO_WRITE_FLUSH;
104686a02f1SFrançois Tigeot 
105686a02f1SFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
106686a02f1SFrançois Tigeot 		cmd |= MI_READ_FLUSH;
107686a02f1SFrançois Tigeot 
108686a02f1SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
109686a02f1SFrançois Tigeot 	if (ret)
110686a02f1SFrançois Tigeot 		return ret;
111686a02f1SFrançois Tigeot 
112686a02f1SFrançois Tigeot 	intel_ring_emit(ring, cmd);
113686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
114686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
115686a02f1SFrançois Tigeot 
116686a02f1SFrançois Tigeot 	return 0;
117686a02f1SFrançois Tigeot }
118686a02f1SFrançois Tigeot 
119686a02f1SFrançois Tigeot static int
120ba55f2f5SFrançois Tigeot gen4_render_ring_flush(struct intel_engine_cs *ring,
121686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
122686a02f1SFrançois Tigeot 		       u32	flush_domains)
123e3adcf8fSFrançois Tigeot {
124e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
125686a02f1SFrançois Tigeot 	u32 cmd;
126e3adcf8fSFrançois Tigeot 	int ret;
127e3adcf8fSFrançois Tigeot 
128e3adcf8fSFrançois Tigeot 	/*
129e3adcf8fSFrançois Tigeot 	 * read/write caches:
130e3adcf8fSFrançois Tigeot 	 *
131e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
132e3adcf8fSFrançois Tigeot 	 * only flushed if MI_NO_WRITE_FLUSH is unset.  On 965, it is
133e3adcf8fSFrançois Tigeot 	 * also flushed at 2d versus 3d pipeline switches.
134e3adcf8fSFrançois Tigeot 	 *
135e3adcf8fSFrançois Tigeot 	 * read-only caches:
136e3adcf8fSFrançois Tigeot 	 *
137e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
138e3adcf8fSFrançois Tigeot 	 * MI_READ_FLUSH is set, and is always flushed on 965.
139e3adcf8fSFrançois Tigeot 	 *
140e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_COMMAND may not exist?
141e3adcf8fSFrançois Tigeot 	 *
142e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
143e3adcf8fSFrançois Tigeot 	 * invalidated when MI_EXE_FLUSH is set.
144e3adcf8fSFrançois Tigeot 	 *
145e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
146e3adcf8fSFrançois Tigeot 	 * invalidated with every MI_FLUSH.
147e3adcf8fSFrançois Tigeot 	 *
148e3adcf8fSFrançois Tigeot 	 * TLBs:
149e3adcf8fSFrançois Tigeot 	 *
150e3adcf8fSFrançois Tigeot 	 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
151e3adcf8fSFrançois Tigeot 	 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
152e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
153e3adcf8fSFrançois Tigeot 	 * are flushed at any MI_FLUSH.
154e3adcf8fSFrançois Tigeot 	 */
155e3adcf8fSFrançois Tigeot 
156e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
157686a02f1SFrançois Tigeot 	if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
158e3adcf8fSFrançois Tigeot 		cmd &= ~MI_NO_WRITE_FLUSH;
159e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
160e3adcf8fSFrançois Tigeot 		cmd |= MI_EXE_FLUSH;
161e3adcf8fSFrançois Tigeot 
162e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
163e3adcf8fSFrançois Tigeot 	    (IS_G4X(dev) || IS_GEN5(dev)))
164e3adcf8fSFrançois Tigeot 		cmd |= MI_INVALIDATE_ISP;
165e3adcf8fSFrançois Tigeot 
166e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
167e3adcf8fSFrançois Tigeot 	if (ret)
168e3adcf8fSFrançois Tigeot 		return ret;
169e3adcf8fSFrançois Tigeot 
170e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
171e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
172e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
173e3adcf8fSFrançois Tigeot 
174e3adcf8fSFrançois Tigeot 	return 0;
175e3adcf8fSFrançois Tigeot }
176e3adcf8fSFrançois Tigeot 
177e3adcf8fSFrançois Tigeot /**
178e3adcf8fSFrançois Tigeot  * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
179e3adcf8fSFrançois Tigeot  * implementing two workarounds on gen6.  From section 1.4.7.1
180e3adcf8fSFrançois Tigeot  * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
181e3adcf8fSFrançois Tigeot  *
182e3adcf8fSFrançois Tigeot  * [DevSNB-C+{W/A}] Before any depth stall flush (including those
183e3adcf8fSFrançois Tigeot  * produced by non-pipelined state commands), software needs to first
184e3adcf8fSFrançois Tigeot  * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
185e3adcf8fSFrançois Tigeot  * 0.
186e3adcf8fSFrançois Tigeot  *
187e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
188e3adcf8fSFrançois Tigeot  * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
189e3adcf8fSFrançois Tigeot  *
190e3adcf8fSFrançois Tigeot  * And the workaround for these two requires this workaround first:
191e3adcf8fSFrançois Tigeot  *
192e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
193e3adcf8fSFrançois Tigeot  * BEFORE the pipe-control with a post-sync op and no write-cache
194e3adcf8fSFrançois Tigeot  * flushes.
195e3adcf8fSFrançois Tigeot  *
196e3adcf8fSFrançois Tigeot  * And this last workaround is tricky because of the requirements on
197e3adcf8fSFrançois Tigeot  * that bit.  From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
198e3adcf8fSFrançois Tigeot  * volume 2 part 1:
199e3adcf8fSFrançois Tigeot  *
200e3adcf8fSFrançois Tigeot  *     "1 of the following must also be set:
201e3adcf8fSFrançois Tigeot  *      - Render Target Cache Flush Enable ([12] of DW1)
202e3adcf8fSFrançois Tigeot  *      - Depth Cache Flush Enable ([0] of DW1)
203e3adcf8fSFrançois Tigeot  *      - Stall at Pixel Scoreboard ([1] of DW1)
204e3adcf8fSFrançois Tigeot  *      - Depth Stall ([13] of DW1)
205e3adcf8fSFrançois Tigeot  *      - Post-Sync Operation ([13] of DW1)
206e3adcf8fSFrançois Tigeot  *      - Notify Enable ([8] of DW1)"
207e3adcf8fSFrançois Tigeot  *
208e3adcf8fSFrançois Tigeot  * The cache flushes require the workaround flush that triggered this
209e3adcf8fSFrançois Tigeot  * one, so we can't use it.  Depth stall would trigger the same.
210e3adcf8fSFrançois Tigeot  * Post-sync nonzero is what triggered this second workaround, so we
211e3adcf8fSFrançois Tigeot  * can't use that one either.  Notify enable is IRQs, which aren't
212e3adcf8fSFrançois Tigeot  * really our business.  That leaves only stall at scoreboard.
213e3adcf8fSFrançois Tigeot  */
214e3adcf8fSFrançois Tigeot static int
215ba55f2f5SFrançois Tigeot intel_emit_post_sync_nonzero_flush(struct intel_engine_cs *ring)
216e3adcf8fSFrançois Tigeot {
217ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
218e3adcf8fSFrançois Tigeot 	int ret;
219e3adcf8fSFrançois Tigeot 
220e3adcf8fSFrançois Tigeot 
221e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
222e3adcf8fSFrançois Tigeot 	if (ret)
223e3adcf8fSFrançois Tigeot 		return ret;
224e3adcf8fSFrançois Tigeot 
225e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
226e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
227e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_STALL_AT_SCOREBOARD);
228e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
229e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* low dword */
230e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* high dword */
231e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
232e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
233e3adcf8fSFrançois Tigeot 
234e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
235e3adcf8fSFrançois Tigeot 	if (ret)
236e3adcf8fSFrançois Tigeot 		return ret;
237e3adcf8fSFrançois Tigeot 
238e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
239e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
240e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
241e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
242e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
243e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
244e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
245e3adcf8fSFrançois Tigeot 
246e3adcf8fSFrançois Tigeot 	return 0;
247e3adcf8fSFrançois Tigeot }
248e3adcf8fSFrançois Tigeot 
249e3adcf8fSFrançois Tigeot static int
250ba55f2f5SFrançois Tigeot gen6_render_ring_flush(struct intel_engine_cs *ring,
251e3adcf8fSFrançois Tigeot                          u32 invalidate_domains, u32 flush_domains)
252e3adcf8fSFrançois Tigeot {
253e3adcf8fSFrançois Tigeot 	u32 flags = 0;
254ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
255e3adcf8fSFrançois Tigeot 	int ret;
256e3adcf8fSFrançois Tigeot 
257e3adcf8fSFrançois Tigeot 	/* Force SNB workarounds for PIPE_CONTROL flushes */
258686a02f1SFrançois Tigeot 	ret = intel_emit_post_sync_nonzero_flush(ring);
259686a02f1SFrançois Tigeot 	if (ret)
260686a02f1SFrançois Tigeot 		return ret;
261e3adcf8fSFrançois Tigeot 
262e3adcf8fSFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
263e3adcf8fSFrançois Tigeot 	 * number of bits based on the write domains has little performance
264e3adcf8fSFrançois Tigeot 	 * impact.
265e3adcf8fSFrançois Tigeot 	 */
266b5c29a34SFrançois Tigeot 	if (flush_domains) {
267e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
268b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
269b5c29a34SFrançois Tigeot 		/*
270b5c29a34SFrançois Tigeot 		 * Ensure that any following seqno writes only happen
271b5c29a34SFrançois Tigeot 		 * when the render cache is indeed flushed.
272b5c29a34SFrançois Tigeot 		 */
273b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CS_STALL;
274b5c29a34SFrançois Tigeot 	}
275b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
276686a02f1SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
277e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
278e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
279e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
280e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
281e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
282686a02f1SFrançois Tigeot 		/*
283b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
284686a02f1SFrançois Tigeot 		 */
285b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
286b5c29a34SFrançois Tigeot 	}
287e3adcf8fSFrançois Tigeot 
288b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
289e3adcf8fSFrançois Tigeot 	if (ret)
290e3adcf8fSFrançois Tigeot 		return ret;
291e3adcf8fSFrançois Tigeot 
292b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
293e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, flags);
294e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
295b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
296b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
297b5c29a34SFrançois Tigeot 
298b5c29a34SFrançois Tigeot 	return 0;
299b5c29a34SFrançois Tigeot }
300b5c29a34SFrançois Tigeot 
301b5c29a34SFrançois Tigeot static int
302ba55f2f5SFrançois Tigeot gen7_render_ring_cs_stall_wa(struct intel_engine_cs *ring)
303b5c29a34SFrançois Tigeot {
304b5c29a34SFrançois Tigeot 	int ret;
305b5c29a34SFrançois Tigeot 
306b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
307b5c29a34SFrançois Tigeot 	if (ret)
308b5c29a34SFrançois Tigeot 		return ret;
309b5c29a34SFrançois Tigeot 
310b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
311b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
312b5c29a34SFrançois Tigeot 			      PIPE_CONTROL_STALL_AT_SCOREBOARD);
313b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
314b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
315b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
316b5c29a34SFrançois Tigeot 
317b5c29a34SFrançois Tigeot 	return 0;
318b5c29a34SFrançois Tigeot }
319b5c29a34SFrançois Tigeot 
320b5c29a34SFrançois Tigeot static int
321ba55f2f5SFrançois Tigeot gen7_render_ring_flush(struct intel_engine_cs *ring,
322b5c29a34SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
323b5c29a34SFrançois Tigeot {
324b5c29a34SFrançois Tigeot 	u32 flags = 0;
325ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
326b5c29a34SFrançois Tigeot 	int ret;
327b5c29a34SFrançois Tigeot 
328b5c29a34SFrançois Tigeot 	/*
329b5c29a34SFrançois Tigeot 	 * Ensure that any following seqno writes only happen when the render
330b5c29a34SFrançois Tigeot 	 * cache is indeed flushed.
331b5c29a34SFrançois Tigeot 	 *
332b5c29a34SFrançois Tigeot 	 * Workaround: 4th PIPE_CONTROL command (except the ones with only
333b5c29a34SFrançois Tigeot 	 * read-cache invalidate bits set) must have the CS_STALL bit set. We
334b5c29a34SFrançois Tigeot 	 * don't try to be clever and just set it unconditionally.
335b5c29a34SFrançois Tigeot 	 */
336b5c29a34SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
337b5c29a34SFrançois Tigeot 
338b5c29a34SFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
339b5c29a34SFrançois Tigeot 	 * number of bits based on the write domains has little performance
340b5c29a34SFrançois Tigeot 	 * impact.
341b5c29a34SFrançois Tigeot 	 */
342b5c29a34SFrançois Tigeot 	if (flush_domains) {
343b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
344b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
345b5c29a34SFrançois Tigeot 	}
346b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
347b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
348b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
349b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
350b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
351b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
352b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
3532c9916cdSFrançois Tigeot 		flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
354b5c29a34SFrançois Tigeot 		/*
355b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
356b5c29a34SFrançois Tigeot 		 */
357b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
358a2fdbec6SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
359b5c29a34SFrançois Tigeot 
3600dbf0ea8SMatthew Dillon 		flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3610dbf0ea8SMatthew Dillon 
362b5c29a34SFrançois Tigeot 		/* Workaround: we must issue a pipe_control with CS-stall bit
363b5c29a34SFrançois Tigeot 		 * set before a pipe_control command that has the state cache
364b5c29a34SFrançois Tigeot 		 * invalidate bit set. */
365b5c29a34SFrançois Tigeot 		gen7_render_ring_cs_stall_wa(ring);
366b5c29a34SFrançois Tigeot 	}
367b5c29a34SFrançois Tigeot 
368b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
369b5c29a34SFrançois Tigeot 	if (ret)
370b5c29a34SFrançois Tigeot 		return ret;
371b5c29a34SFrançois Tigeot 
372b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
373b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, flags);
374a2fdbec6SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
375b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
376e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
377e3adcf8fSFrançois Tigeot 
378e3adcf8fSFrançois Tigeot 	return 0;
379e3adcf8fSFrançois Tigeot }
380e3adcf8fSFrançois Tigeot 
3819edbd4a0SFrançois Tigeot static int
38224edb884SFrançois Tigeot gen8_emit_pipe_control(struct intel_engine_cs *ring,
38324edb884SFrançois Tigeot 		       u32 flags, u32 scratch_addr)
38424edb884SFrançois Tigeot {
38524edb884SFrançois Tigeot 	int ret;
38624edb884SFrançois Tigeot 
38724edb884SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
38824edb884SFrançois Tigeot 	if (ret)
38924edb884SFrançois Tigeot 		return ret;
39024edb884SFrançois Tigeot 
39124edb884SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
39224edb884SFrançois Tigeot 	intel_ring_emit(ring, flags);
39324edb884SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
39424edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
39524edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
39624edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
39724edb884SFrançois Tigeot 	intel_ring_advance(ring);
39824edb884SFrançois Tigeot 
39924edb884SFrançois Tigeot 	return 0;
40024edb884SFrançois Tigeot }
40124edb884SFrançois Tigeot 
40224edb884SFrançois Tigeot static int
403ba55f2f5SFrançois Tigeot gen8_render_ring_flush(struct intel_engine_cs *ring,
4049edbd4a0SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
4059edbd4a0SFrançois Tigeot {
4069edbd4a0SFrançois Tigeot 	u32 flags = 0;
407ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
4089edbd4a0SFrançois Tigeot 	int ret;
4099edbd4a0SFrançois Tigeot 
4109edbd4a0SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
4119edbd4a0SFrançois Tigeot 
4129edbd4a0SFrançois Tigeot 	if (flush_domains) {
4139edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
4149edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
4159edbd4a0SFrançois Tigeot 	}
4169edbd4a0SFrançois Tigeot 	if (invalidate_domains) {
4179edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
4189edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
4199edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
4209edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
4219edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
4229edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
4239edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
4249edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4259edbd4a0SFrançois Tigeot 
42624edb884SFrançois Tigeot 		/* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
42724edb884SFrançois Tigeot 		ret = gen8_emit_pipe_control(ring,
42824edb884SFrançois Tigeot 					     PIPE_CONTROL_CS_STALL |
42924edb884SFrançois Tigeot 					     PIPE_CONTROL_STALL_AT_SCOREBOARD,
43024edb884SFrançois Tigeot 					     0);
4319edbd4a0SFrançois Tigeot 		if (ret)
4329edbd4a0SFrançois Tigeot 			return ret;
43324edb884SFrançois Tigeot 	}
4349edbd4a0SFrançois Tigeot 
435477eb7f9SFrançois Tigeot 	return gen8_emit_pipe_control(ring, flags, scratch_addr);
4369edbd4a0SFrançois Tigeot }
4379edbd4a0SFrançois Tigeot 
438ba55f2f5SFrançois Tigeot static void ring_write_tail(struct intel_engine_cs *ring,
439b5c29a34SFrançois Tigeot 			    u32 value)
440e3adcf8fSFrançois Tigeot {
441ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
442e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
443e3adcf8fSFrançois Tigeot }
444e3adcf8fSFrançois Tigeot 
445ba55f2f5SFrançois Tigeot u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
446e3adcf8fSFrançois Tigeot {
447ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
448ba55f2f5SFrançois Tigeot 	u64 acthd;
449e3adcf8fSFrançois Tigeot 
450ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
451ba55f2f5SFrançois Tigeot 		acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
452ba55f2f5SFrançois Tigeot 					 RING_ACTHD_UDW(ring->mmio_base));
453ba55f2f5SFrançois Tigeot 	else if (INTEL_INFO(ring->dev)->gen >= 4)
454ba55f2f5SFrançois Tigeot 		acthd = I915_READ(RING_ACTHD(ring->mmio_base));
455ba55f2f5SFrançois Tigeot 	else
456ba55f2f5SFrançois Tigeot 		acthd = I915_READ(ACTHD);
457ba55f2f5SFrançois Tigeot 
458ba55f2f5SFrançois Tigeot 	return acthd;
459e3adcf8fSFrançois Tigeot }
460e3adcf8fSFrançois Tigeot 
461ba55f2f5SFrançois Tigeot static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
4625d0b1887SFrançois Tigeot {
4635d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
4645d0b1887SFrançois Tigeot 	u32 addr;
4655d0b1887SFrançois Tigeot 
4665d0b1887SFrançois Tigeot 	addr = dev_priv->status_page_dmah->busaddr;
4675d0b1887SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 4)
4685d0b1887SFrançois Tigeot 		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
4695d0b1887SFrançois Tigeot 	I915_WRITE(HWS_PGA, addr);
4705d0b1887SFrançois Tigeot }
4715d0b1887SFrançois Tigeot 
472477eb7f9SFrançois Tigeot static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
473477eb7f9SFrançois Tigeot {
474477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
475477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
476477eb7f9SFrançois Tigeot 	u32 mmio = 0;
477477eb7f9SFrançois Tigeot 
478477eb7f9SFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
479477eb7f9SFrançois Tigeot 	 * the ring registers as of gen7.
480477eb7f9SFrançois Tigeot 	 */
481477eb7f9SFrançois Tigeot 	if (IS_GEN7(dev)) {
482477eb7f9SFrançois Tigeot 		switch (ring->id) {
483477eb7f9SFrançois Tigeot 		case RCS:
484477eb7f9SFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
485477eb7f9SFrançois Tigeot 			break;
486477eb7f9SFrançois Tigeot 		case BCS:
487477eb7f9SFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
488477eb7f9SFrançois Tigeot 			break;
489477eb7f9SFrançois Tigeot 		/*
490477eb7f9SFrançois Tigeot 		 * VCS2 actually doesn't exist on Gen7. Only shut up
491477eb7f9SFrançois Tigeot 		 * gcc switch check warning
492477eb7f9SFrançois Tigeot 		 */
493477eb7f9SFrançois Tigeot 		case VCS2:
494477eb7f9SFrançois Tigeot 		case VCS:
495477eb7f9SFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
496477eb7f9SFrançois Tigeot 			break;
497477eb7f9SFrançois Tigeot 		case VECS:
498477eb7f9SFrançois Tigeot 			mmio = VEBOX_HWS_PGA_GEN7;
499477eb7f9SFrançois Tigeot 			break;
500477eb7f9SFrançois Tigeot 		}
501477eb7f9SFrançois Tigeot 	} else if (IS_GEN6(ring->dev)) {
502477eb7f9SFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
503477eb7f9SFrançois Tigeot 	} else {
504477eb7f9SFrançois Tigeot 		/* XXX: gen8 returns to sanity */
505477eb7f9SFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
506477eb7f9SFrançois Tigeot 	}
507477eb7f9SFrançois Tigeot 
508477eb7f9SFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
509477eb7f9SFrançois Tigeot 	POSTING_READ(mmio);
510477eb7f9SFrançois Tigeot 
511477eb7f9SFrançois Tigeot 	/*
512477eb7f9SFrançois Tigeot 	 * Flush the TLB for this page
513477eb7f9SFrançois Tigeot 	 *
514477eb7f9SFrançois Tigeot 	 * FIXME: These two bits have disappeared on gen8, so a question
515477eb7f9SFrançois Tigeot 	 * arises: do we still need this and if so how should we go about
516477eb7f9SFrançois Tigeot 	 * invalidating the TLB?
517477eb7f9SFrançois Tigeot 	 */
518477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
519477eb7f9SFrançois Tigeot 		u32 reg = RING_INSTPM(ring->mmio_base);
520477eb7f9SFrançois Tigeot 
521477eb7f9SFrançois Tigeot 		/* ring should be idle before issuing a sync flush*/
522477eb7f9SFrançois Tigeot 		WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
523477eb7f9SFrançois Tigeot 
524477eb7f9SFrançois Tigeot 		I915_WRITE(reg,
525477eb7f9SFrançois Tigeot 			   _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
526477eb7f9SFrançois Tigeot 					      INSTPM_SYNC_FLUSH));
527477eb7f9SFrançois Tigeot 		if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
528477eb7f9SFrançois Tigeot 			     1000))
529477eb7f9SFrançois Tigeot 			DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
530477eb7f9SFrançois Tigeot 				  ring->name);
531477eb7f9SFrançois Tigeot 	}
532477eb7f9SFrançois Tigeot }
533477eb7f9SFrançois Tigeot 
534ba55f2f5SFrançois Tigeot static bool stop_ring(struct intel_engine_cs *ring)
535e3adcf8fSFrançois Tigeot {
536ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ring->dev);
537e3adcf8fSFrançois Tigeot 
538ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
539ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
5401b13d190SFrançois Tigeot 		if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
541ba55f2f5SFrançois Tigeot 			DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
5421b13d190SFrançois Tigeot 			/* Sometimes we observe that the idle flag is not
5431b13d190SFrançois Tigeot 			 * set even though the ring is empty. So double
5441b13d190SFrançois Tigeot 			 * check before giving up.
5451b13d190SFrançois Tigeot 			 */
5461b13d190SFrançois Tigeot 			if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
547ba55f2f5SFrançois Tigeot 				return false;
548ba55f2f5SFrançois Tigeot 		}
549ba55f2f5SFrançois Tigeot 	}
550686a02f1SFrançois Tigeot 
551e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
552e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
553e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
554e3adcf8fSFrançois Tigeot 
555ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
556ba55f2f5SFrançois Tigeot 		(void)I915_READ_CTL(ring);
557ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
558ba55f2f5SFrançois Tigeot 	}
559e3adcf8fSFrançois Tigeot 
560ba55f2f5SFrançois Tigeot 	return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
561ba55f2f5SFrançois Tigeot }
562ba55f2f5SFrançois Tigeot 
563ba55f2f5SFrançois Tigeot static int init_ring_common(struct intel_engine_cs *ring)
564ba55f2f5SFrançois Tigeot {
565ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ring->dev;
566ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
567ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
568ba55f2f5SFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
569ba55f2f5SFrançois Tigeot 	int ret = 0;
570ba55f2f5SFrançois Tigeot 
5712c9916cdSFrançois Tigeot 	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
572ba55f2f5SFrançois Tigeot 
573ba55f2f5SFrançois Tigeot 	if (!stop_ring(ring)) {
574ba55f2f5SFrançois Tigeot 		/* G45 ring initialization often fails to reset head to zero */
575b5c29a34SFrançois Tigeot 		DRM_DEBUG_KMS("%s head not reset to zero "
576e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
577e3adcf8fSFrançois Tigeot 			      ring->name,
578e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
579e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
580e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
581e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
582e3adcf8fSFrançois Tigeot 
583ba55f2f5SFrançois Tigeot 		if (!stop_ring(ring)) {
584e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
585e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
586e3adcf8fSFrançois Tigeot 				  ring->name,
587e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
588e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
589e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
590e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
591686a02f1SFrançois Tigeot 			ret = -EIO;
592686a02f1SFrançois Tigeot 			goto out;
593e3adcf8fSFrançois Tigeot 		}
594ba55f2f5SFrançois Tigeot 	}
595ba55f2f5SFrançois Tigeot 
596ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev))
597ba55f2f5SFrançois Tigeot 		intel_ring_setup_status_page(ring);
598ba55f2f5SFrançois Tigeot 	else
599ba55f2f5SFrançois Tigeot 		ring_setup_phys_status_page(ring);
600ba55f2f5SFrançois Tigeot 
6010f370975SMatthew Dillon 	/* Enforce ordering by reading HEAD register back */
6020f370975SMatthew Dillon 	I915_READ_HEAD(ring);
6030f370975SMatthew Dillon 
604ba55f2f5SFrançois Tigeot 	/* Initialize the ring. This must happen _after_ we've cleared the ring
605ba55f2f5SFrançois Tigeot 	 * registers with the above sequence (the readback of the HEAD registers
606ba55f2f5SFrançois Tigeot 	 * also enforces ordering), otherwise the hw might lose the new ring
607ba55f2f5SFrançois Tigeot 	 * register values. */
608ba55f2f5SFrançois Tigeot 	I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
6091b13d190SFrançois Tigeot 
6101b13d190SFrançois Tigeot 	/* WaClearRingBufHeadRegAtInit:ctg,elk */
6111b13d190SFrançois Tigeot 	if (I915_READ_HEAD(ring))
6121b13d190SFrançois Tigeot 		DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
6131b13d190SFrançois Tigeot 			  ring->name, I915_READ_HEAD(ring));
6141b13d190SFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
6151b13d190SFrançois Tigeot 	(void)I915_READ_HEAD(ring);
6161b13d190SFrançois Tigeot 
617ba55f2f5SFrançois Tigeot 	I915_WRITE_CTL(ring,
618ba55f2f5SFrançois Tigeot 			((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
619ba55f2f5SFrançois Tigeot 			| RING_VALID);
620ba55f2f5SFrançois Tigeot 
621ba55f2f5SFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
622ba55f2f5SFrançois Tigeot 	if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
623ba55f2f5SFrançois Tigeot 		     I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
624ba55f2f5SFrançois Tigeot 		     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
625ba55f2f5SFrançois Tigeot 		DRM_ERROR("%s initialization failed "
626ba55f2f5SFrançois Tigeot 			  "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
627ba55f2f5SFrançois Tigeot 			  ring->name,
628ba55f2f5SFrançois Tigeot 			  I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
629ba55f2f5SFrançois Tigeot 			  I915_READ_HEAD(ring), I915_READ_TAIL(ring),
630ba55f2f5SFrançois Tigeot 			  I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
631ba55f2f5SFrançois Tigeot 		ret = -EIO;
632ba55f2f5SFrançois Tigeot 		goto out;
633ba55f2f5SFrançois Tigeot 	}
634e3adcf8fSFrançois Tigeot 
6352c9916cdSFrançois Tigeot 	ringbuf->last_retired_head = -1;
636ba55f2f5SFrançois Tigeot 	ringbuf->head = I915_READ_HEAD(ring);
637ba55f2f5SFrançois Tigeot 	ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
6382c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
639e3adcf8fSFrançois Tigeot 
6405d0b1887SFrançois Tigeot 	memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
6415d0b1887SFrançois Tigeot 
642686a02f1SFrançois Tigeot out:
6432c9916cdSFrançois Tigeot 	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
644686a02f1SFrançois Tigeot 
645686a02f1SFrançois Tigeot 	return ret;
646e3adcf8fSFrançois Tigeot }
647e3adcf8fSFrançois Tigeot 
6481b13d190SFrançois Tigeot void
6491b13d190SFrançois Tigeot intel_fini_pipe_control(struct intel_engine_cs *ring)
6501b13d190SFrançois Tigeot {
6511b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
6521b13d190SFrançois Tigeot 
6531b13d190SFrançois Tigeot 	if (ring->scratch.obj == NULL)
6541b13d190SFrançois Tigeot 		return;
6551b13d190SFrançois Tigeot 
6561b13d190SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
6571b13d190SFrançois Tigeot 		kunmap(ring->scratch.obj->pages[0]);
6581b13d190SFrançois Tigeot 		i915_gem_object_ggtt_unpin(ring->scratch.obj);
6591b13d190SFrançois Tigeot 	}
6601b13d190SFrançois Tigeot 
6611b13d190SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
6621b13d190SFrançois Tigeot 	ring->scratch.obj = NULL;
6631b13d190SFrançois Tigeot }
6641b13d190SFrançois Tigeot 
6651b13d190SFrançois Tigeot int
6661b13d190SFrançois Tigeot intel_init_pipe_control(struct intel_engine_cs *ring)
667e3adcf8fSFrançois Tigeot {
668e3adcf8fSFrançois Tigeot 	int ret;
669e3adcf8fSFrançois Tigeot 
6702c9916cdSFrançois Tigeot 	WARN_ON(ring->scratch.obj);
671e3adcf8fSFrançois Tigeot 
6729edbd4a0SFrançois Tigeot 	ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
6739edbd4a0SFrançois Tigeot 	if (ring->scratch.obj == NULL) {
674e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
675e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
676e3adcf8fSFrançois Tigeot 		goto err;
677e3adcf8fSFrançois Tigeot 	}
678e3adcf8fSFrançois Tigeot 
679ba55f2f5SFrançois Tigeot 	ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
680ba55f2f5SFrançois Tigeot 	if (ret)
681ba55f2f5SFrançois Tigeot 		goto err_unref;
682e3adcf8fSFrançois Tigeot 
683ba55f2f5SFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
684e3adcf8fSFrançois Tigeot 	if (ret)
685e3adcf8fSFrançois Tigeot 		goto err_unref;
686e3adcf8fSFrançois Tigeot 
6879edbd4a0SFrançois Tigeot 	ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
6889edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page = kmap(ring->scratch.obj->pages[0]);
6899edbd4a0SFrançois Tigeot 	if (ring->scratch.cpu_page == NULL) {
6905d0b1887SFrançois Tigeot 		ret = -ENOMEM;
691e3adcf8fSFrançois Tigeot 		goto err_unpin;
6925d0b1887SFrançois Tigeot 	}
693a2fdbec6SFrançois Tigeot 
694a2fdbec6SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
6959edbd4a0SFrançois Tigeot 			 ring->name, ring->scratch.gtt_offset);
696e3adcf8fSFrançois Tigeot 	return 0;
697e3adcf8fSFrançois Tigeot 
698e3adcf8fSFrançois Tigeot err_unpin:
699ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(ring->scratch.obj);
700e3adcf8fSFrançois Tigeot err_unref:
7019edbd4a0SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
702e3adcf8fSFrançois Tigeot err:
703e3adcf8fSFrançois Tigeot 	return ret;
704e3adcf8fSFrançois Tigeot }
705e3adcf8fSFrançois Tigeot 
7062c9916cdSFrançois Tigeot static int intel_ring_workarounds_emit(struct intel_engine_cs *ring,
7072c9916cdSFrançois Tigeot 				       struct intel_context *ctx)
7081b13d190SFrançois Tigeot {
7092c9916cdSFrançois Tigeot 	int ret, i;
7101b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
7111b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
7122c9916cdSFrançois Tigeot 	struct i915_workarounds *w = &dev_priv->workarounds;
7131b13d190SFrançois Tigeot 
7142c9916cdSFrançois Tigeot 	if (WARN_ON_ONCE(w->count == 0))
7152c9916cdSFrançois Tigeot 		return 0;
7161b13d190SFrançois Tigeot 
7172c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
7182c9916cdSFrançois Tigeot 	ret = intel_ring_flush_all_caches(ring);
7191b13d190SFrançois Tigeot 	if (ret)
7201b13d190SFrançois Tigeot 		return ret;
7211b13d190SFrançois Tigeot 
7222c9916cdSFrançois Tigeot 	ret = intel_ring_begin(ring, (w->count * 2 + 2));
7232c9916cdSFrançois Tigeot 	if (ret)
7242c9916cdSFrançois Tigeot 		return ret;
7252c9916cdSFrançois Tigeot 
7262c9916cdSFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
7272c9916cdSFrançois Tigeot 	for (i = 0; i < w->count; i++) {
7282c9916cdSFrançois Tigeot 		intel_ring_emit(ring, w->reg[i].addr);
7292c9916cdSFrançois Tigeot 		intel_ring_emit(ring, w->reg[i].value);
7302c9916cdSFrançois Tigeot 	}
7312c9916cdSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
7322c9916cdSFrançois Tigeot 
7332c9916cdSFrançois Tigeot 	intel_ring_advance(ring);
7342c9916cdSFrançois Tigeot 
7352c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
7362c9916cdSFrançois Tigeot 	ret = intel_ring_flush_all_caches(ring);
7372c9916cdSFrançois Tigeot 	if (ret)
7382c9916cdSFrançois Tigeot 		return ret;
7392c9916cdSFrançois Tigeot 
7402c9916cdSFrançois Tigeot 	DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
7412c9916cdSFrançois Tigeot 
7422c9916cdSFrançois Tigeot 	return 0;
7432c9916cdSFrançois Tigeot }
7442c9916cdSFrançois Tigeot 
7452c9916cdSFrançois Tigeot static int intel_rcs_ctx_init(struct intel_engine_cs *ring,
7462c9916cdSFrançois Tigeot 			      struct intel_context *ctx)
7472c9916cdSFrançois Tigeot {
7482c9916cdSFrançois Tigeot 	int ret;
7492c9916cdSFrançois Tigeot 
7502c9916cdSFrançois Tigeot 	ret = intel_ring_workarounds_emit(ring, ctx);
7512c9916cdSFrançois Tigeot 	if (ret != 0)
7522c9916cdSFrançois Tigeot 		return ret;
7532c9916cdSFrançois Tigeot 
7542c9916cdSFrançois Tigeot 	ret = i915_gem_render_state_init(ring);
7552c9916cdSFrançois Tigeot 	if (ret)
7562c9916cdSFrançois Tigeot 		DRM_ERROR("init render state: %d\n", ret);
7572c9916cdSFrançois Tigeot 
7582c9916cdSFrançois Tigeot 	return ret;
7592c9916cdSFrançois Tigeot }
7602c9916cdSFrançois Tigeot 
7612c9916cdSFrançois Tigeot static int wa_add(struct drm_i915_private *dev_priv,
7622c9916cdSFrançois Tigeot 		  const u32 addr, const u32 mask, const u32 val)
7632c9916cdSFrançois Tigeot {
7642c9916cdSFrançois Tigeot 	const u32 idx = dev_priv->workarounds.count;
7652c9916cdSFrançois Tigeot 
7662c9916cdSFrançois Tigeot 	if (WARN_ON(idx >= I915_MAX_WA_REGS))
7672c9916cdSFrançois Tigeot 		return -ENOSPC;
7682c9916cdSFrançois Tigeot 
7692c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].addr = addr;
7702c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].value = val;
7712c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].mask = mask;
7722c9916cdSFrançois Tigeot 
7732c9916cdSFrançois Tigeot 	dev_priv->workarounds.count++;
7742c9916cdSFrançois Tigeot 
7752c9916cdSFrançois Tigeot 	return 0;
7762c9916cdSFrançois Tigeot }
7772c9916cdSFrançois Tigeot 
7782c9916cdSFrançois Tigeot #define WA_REG(addr, mask, val) { \
7792c9916cdSFrançois Tigeot 		const int r = wa_add(dev_priv, (addr), (mask), (val)); \
7802c9916cdSFrançois Tigeot 		if (r) \
7812c9916cdSFrançois Tigeot 			return r; \
7822c9916cdSFrançois Tigeot 	}
7832c9916cdSFrançois Tigeot 
7842c9916cdSFrançois Tigeot #define WA_SET_BIT_MASKED(addr, mask) \
7852c9916cdSFrançois Tigeot 	WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
7862c9916cdSFrançois Tigeot 
7872c9916cdSFrançois Tigeot #define WA_CLR_BIT_MASKED(addr, mask) \
7882c9916cdSFrançois Tigeot 	WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
7892c9916cdSFrançois Tigeot 
7902c9916cdSFrançois Tigeot #define WA_SET_FIELD_MASKED(addr, mask, value) \
7912c9916cdSFrançois Tigeot 	WA_REG(addr, mask, _MASKED_FIELD(mask, value))
7922c9916cdSFrançois Tigeot 
7932c9916cdSFrançois Tigeot #define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
7942c9916cdSFrançois Tigeot #define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
7952c9916cdSFrançois Tigeot 
7962c9916cdSFrançois Tigeot #define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
7972c9916cdSFrançois Tigeot 
7982c9916cdSFrançois Tigeot static int bdw_init_workarounds(struct intel_engine_cs *ring)
7992c9916cdSFrançois Tigeot {
8002c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
8012c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
8022c9916cdSFrançois Tigeot 
8031b13d190SFrançois Tigeot 	/* WaDisablePartialInstShootdown:bdw */
8042c9916cdSFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:bdw (pre-production) */
8052c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
8062c9916cdSFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
8072c9916cdSFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
8081b13d190SFrançois Tigeot 
8092c9916cdSFrançois Tigeot 	/* WaDisableDopClockGating:bdw */
8102c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
8112c9916cdSFrançois Tigeot 			  DOP_CLOCK_GATING_DISABLE);
8121b13d190SFrançois Tigeot 
8132c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
8142c9916cdSFrançois Tigeot 			  GEN8_SAMPLER_POWER_BYPASS_DIS);
8151b13d190SFrançois Tigeot 
8161b13d190SFrançois Tigeot 	/* Use Force Non-Coherent whenever executing a 3D context. This is a
8171b13d190SFrançois Tigeot 	 * workaround for for a possible hang in the unlikely event a TLB
8181b13d190SFrançois Tigeot 	 * invalidation occurs during a PSD flush.
8191b13d190SFrançois Tigeot 	 */
8202c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0,
821477eb7f9SFrançois Tigeot 			  /* WaForceEnableNonCoherent:bdw */
8222c9916cdSFrançois Tigeot 			  HDC_FORCE_NON_COHERENT |
823477eb7f9SFrançois Tigeot 			  /* WaForceContextSaveRestoreNonCoherent:bdw */
824477eb7f9SFrançois Tigeot 			  HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
825477eb7f9SFrançois Tigeot 			  /* WaHdcDisableFetchWhenMasked:bdw */
8262c9916cdSFrançois Tigeot 			  HDC_DONOT_FETCH_MEM_WHEN_MASKED |
827477eb7f9SFrançois Tigeot 			  /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
8282c9916cdSFrançois Tigeot 			  (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
8292c9916cdSFrançois Tigeot 
8302c9916cdSFrançois Tigeot 	/* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
8312c9916cdSFrançois Tigeot 	 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
8322c9916cdSFrançois Tigeot 	 *  polygons in the same 8x4 pixel/sample area to be processed without
8332c9916cdSFrançois Tigeot 	 *  stalling waiting for the earlier ones to write to Hierarchical Z
8342c9916cdSFrançois Tigeot 	 *  buffer."
8352c9916cdSFrançois Tigeot 	 *
8362c9916cdSFrançois Tigeot 	 * This optimization is off by default for Broadwell; turn it on.
8372c9916cdSFrançois Tigeot 	 */
8382c9916cdSFrançois Tigeot 	WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
8391b13d190SFrançois Tigeot 
8401b13d190SFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:bdw */
8412c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1,
8422c9916cdSFrançois Tigeot 			  GEN8_4x4_STC_OPTIMIZATION_DISABLE);
8431b13d190SFrançois Tigeot 
8441b13d190SFrançois Tigeot 	/*
8451b13d190SFrançois Tigeot 	 * BSpec recommends 8x4 when MSAA is used,
8461b13d190SFrançois Tigeot 	 * however in practice 16x4 seems fastest.
8471b13d190SFrançois Tigeot 	 *
8481b13d190SFrançois Tigeot 	 * Note that PS/WM thread counts depend on the WIZ hashing
8491b13d190SFrançois Tigeot 	 * disable bit, which we don't touch here, but it's good
8501b13d190SFrançois Tigeot 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8511b13d190SFrançois Tigeot 	 */
8522c9916cdSFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
8532c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_MASK,
8542c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_16x4);
8551b13d190SFrançois Tigeot 
8561b13d190SFrançois Tigeot 	return 0;
8571b13d190SFrançois Tigeot }
8581b13d190SFrançois Tigeot 
8591b13d190SFrançois Tigeot static int chv_init_workarounds(struct intel_engine_cs *ring)
8601b13d190SFrançois Tigeot {
8611b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
8621b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
8631b13d190SFrançois Tigeot 
8641b13d190SFrançois Tigeot 	/* WaDisablePartialInstShootdown:chv */
8651b13d190SFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:chv */
8662c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
8672c9916cdSFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
8682c9916cdSFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
8691b13d190SFrançois Tigeot 
8702c9916cdSFrançois Tigeot 	/* Use Force Non-Coherent whenever executing a 3D context. This is a
8712c9916cdSFrançois Tigeot 	 * workaround for a possible hang in the unlikely event a TLB
8722c9916cdSFrançois Tigeot 	 * invalidation occurs during a PSD flush.
8732c9916cdSFrançois Tigeot 	 */
8742c9916cdSFrançois Tigeot 	/* WaForceEnableNonCoherent:chv */
8752c9916cdSFrançois Tigeot 	/* WaHdcDisableFetchWhenMasked:chv */
8762c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0,
8772c9916cdSFrançois Tigeot 			  HDC_FORCE_NON_COHERENT |
8782c9916cdSFrançois Tigeot 			  HDC_DONOT_FETCH_MEM_WHEN_MASKED);
8791b13d190SFrançois Tigeot 
8802c9916cdSFrançois Tigeot 	/* According to the CACHE_MODE_0 default value documentation, some
8812c9916cdSFrançois Tigeot 	 * CHV platforms disable this optimization by default.  Turn it on.
8822c9916cdSFrançois Tigeot 	 */
8832c9916cdSFrançois Tigeot 	WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
8841b13d190SFrançois Tigeot 
8852c9916cdSFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:chv */
8862c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1,
8872c9916cdSFrançois Tigeot 			  GEN8_4x4_STC_OPTIMIZATION_DISABLE);
8882c9916cdSFrançois Tigeot 
8892c9916cdSFrançois Tigeot 	/* Improve HiZ throughput on CHV. */
8902c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
8912c9916cdSFrançois Tigeot 
8922c9916cdSFrançois Tigeot 	/*
8932c9916cdSFrançois Tigeot 	 * BSpec recommends 8x4 when MSAA is used,
8942c9916cdSFrançois Tigeot 	 * however in practice 16x4 seems fastest.
8952c9916cdSFrançois Tigeot 	 *
8962c9916cdSFrançois Tigeot 	 * Note that PS/WM thread counts depend on the WIZ hashing
8972c9916cdSFrançois Tigeot 	 * disable bit, which we don't touch here, but it's good
8982c9916cdSFrançois Tigeot 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8992c9916cdSFrançois Tigeot 	 */
9002c9916cdSFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
9012c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_MASK,
9022c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_16x4);
9032c9916cdSFrançois Tigeot 
9042c9916cdSFrançois Tigeot 	return 0;
9052c9916cdSFrançois Tigeot }
9062c9916cdSFrançois Tigeot 
907477eb7f9SFrançois Tigeot static int gen9_init_workarounds(struct intel_engine_cs *ring)
908477eb7f9SFrançois Tigeot {
909477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
910477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
91119c468b4SFrançois Tigeot 	uint32_t tmp;
912477eb7f9SFrançois Tigeot 
91319c468b4SFrançois Tigeot 	/* WaDisablePartialInstShootdown:skl,bxt */
914477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
915477eb7f9SFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
916477eb7f9SFrançois Tigeot 
91719c468b4SFrançois Tigeot 	/* Syncing dependencies between camera and graphics:skl,bxt */
918477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
919477eb7f9SFrançois Tigeot 			  GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
920477eb7f9SFrançois Tigeot 
92119c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) == SKL_REVID_A0 ||
92219c468b4SFrançois Tigeot 	    INTEL_REVID(dev) == SKL_REVID_B0)) ||
92319c468b4SFrançois Tigeot 	    (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
92419c468b4SFrançois Tigeot 		/* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
925477eb7f9SFrançois Tigeot 		WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
926477eb7f9SFrançois Tigeot 				  GEN9_DG_MIRROR_FIX_ENABLE);
927477eb7f9SFrançois Tigeot 	}
928477eb7f9SFrançois Tigeot 
92919c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0) ||
93019c468b4SFrançois Tigeot 	    (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
93119c468b4SFrançois Tigeot 		/* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
932477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
933477eb7f9SFrançois Tigeot 				  GEN9_RHWO_OPTIMIZATION_DISABLE);
934477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(GEN9_SLICE_COMMON_ECO_CHICKEN0,
935477eb7f9SFrançois Tigeot 				  DISABLE_PIXEL_MASK_CAMMING);
936477eb7f9SFrançois Tigeot 	}
937477eb7f9SFrançois Tigeot 
93819c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) >= SKL_REVID_C0) ||
93919c468b4SFrançois Tigeot 	    IS_BROXTON(dev)) {
94019c468b4SFrançois Tigeot 		/* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt */
941477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
942477eb7f9SFrançois Tigeot 				  GEN9_ENABLE_YV12_BUGFIX);
943477eb7f9SFrançois Tigeot 	}
944477eb7f9SFrançois Tigeot 
94519c468b4SFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:skl,bxt */
946477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
947477eb7f9SFrançois Tigeot 
94819c468b4SFrançois Tigeot 	/* WaDisablePartialResolveInVc:skl,bxt */
949477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1, GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE);
950477eb7f9SFrançois Tigeot 
95119c468b4SFrançois Tigeot 	/* WaCcsTlbPrefetchDisable:skl,bxt */
952477eb7f9SFrançois Tigeot 	WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
953477eb7f9SFrançois Tigeot 			  GEN9_CCS_TLB_PREFETCH_ENABLE);
954477eb7f9SFrançois Tigeot 
95519c468b4SFrançois Tigeot 	/* WaDisableMaskBasedCammingInRCC:skl,bxt */
95619c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_C0) ||
95719c468b4SFrançois Tigeot 	    (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0))
95819c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
95919c468b4SFrançois Tigeot 				  PIXEL_MASK_CAMMING_DISABLE);
96019c468b4SFrançois Tigeot 
96119c468b4SFrançois Tigeot 	/* WaForceContextSaveRestoreNonCoherent:skl,bxt */
96219c468b4SFrançois Tigeot 	tmp = HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT;
96319c468b4SFrançois Tigeot 	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_F0) ||
96419c468b4SFrançois Tigeot 	    (IS_BROXTON(dev) && INTEL_REVID(dev) >= BXT_REVID_B0))
96519c468b4SFrançois Tigeot 		tmp |= HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE;
96619c468b4SFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0, tmp);
96719c468b4SFrançois Tigeot 
968477eb7f9SFrançois Tigeot 	return 0;
969477eb7f9SFrançois Tigeot }
970477eb7f9SFrançois Tigeot 
971477eb7f9SFrançois Tigeot static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
972477eb7f9SFrançois Tigeot {
973477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
974477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
975477eb7f9SFrançois Tigeot 	u8 vals[3] = { 0, 0, 0 };
976477eb7f9SFrançois Tigeot 	unsigned int i;
977477eb7f9SFrançois Tigeot 
978477eb7f9SFrançois Tigeot 	for (i = 0; i < 3; i++) {
979477eb7f9SFrançois Tigeot 		u8 ss;
980477eb7f9SFrançois Tigeot 
981477eb7f9SFrançois Tigeot 		/*
982477eb7f9SFrançois Tigeot 		 * Only consider slices where one, and only one, subslice has 7
983477eb7f9SFrançois Tigeot 		 * EUs
984477eb7f9SFrançois Tigeot 		 */
985477eb7f9SFrançois Tigeot 		if (hweight8(dev_priv->info.subslice_7eu[i]) != 1)
986477eb7f9SFrançois Tigeot 			continue;
987477eb7f9SFrançois Tigeot 
988477eb7f9SFrançois Tigeot 		/*
989477eb7f9SFrançois Tigeot 		 * subslice_7eu[i] != 0 (because of the check above) and
990477eb7f9SFrançois Tigeot 		 * ss_max == 4 (maximum number of subslices possible per slice)
991477eb7f9SFrançois Tigeot 		 *
992477eb7f9SFrançois Tigeot 		 * ->    0 <= ss <= 3;
993477eb7f9SFrançois Tigeot 		 */
994477eb7f9SFrançois Tigeot 		ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
995477eb7f9SFrançois Tigeot 		vals[i] = 3 - ss;
996477eb7f9SFrançois Tigeot 	}
997477eb7f9SFrançois Tigeot 
998477eb7f9SFrançois Tigeot 	if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
999477eb7f9SFrançois Tigeot 		return 0;
1000477eb7f9SFrançois Tigeot 
1001477eb7f9SFrançois Tigeot 	/* Tune IZ hashing. See intel_device_info_runtime_init() */
1002477eb7f9SFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1003477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(2) |
1004477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(1) |
1005477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(0),
1006477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(2, vals[2]) |
1007477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(1, vals[1]) |
1008477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(0, vals[0]));
1009477eb7f9SFrançois Tigeot 
1010477eb7f9SFrançois Tigeot 	return 0;
1011477eb7f9SFrançois Tigeot }
1012477eb7f9SFrançois Tigeot 
1013477eb7f9SFrançois Tigeot 
1014477eb7f9SFrançois Tigeot static int skl_init_workarounds(struct intel_engine_cs *ring)
1015477eb7f9SFrançois Tigeot {
1016477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1017477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1018477eb7f9SFrançois Tigeot 
1019477eb7f9SFrançois Tigeot 	gen9_init_workarounds(ring);
1020477eb7f9SFrançois Tigeot 
1021477eb7f9SFrançois Tigeot 	/* WaDisablePowerCompilerClockGating:skl */
1022477eb7f9SFrançois Tigeot 	if (INTEL_REVID(dev) == SKL_REVID_B0)
1023477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(HIZ_CHICKEN,
1024477eb7f9SFrançois Tigeot 				  BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1025477eb7f9SFrançois Tigeot 
1026477eb7f9SFrançois Tigeot 	if (INTEL_REVID(dev) == SKL_REVID_C0 ||
1027477eb7f9SFrançois Tigeot 	    INTEL_REVID(dev) == SKL_REVID_D0)
1028477eb7f9SFrançois Tigeot 		/* WaBarrierPerformanceFixDisable:skl */
1029477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(HDC_CHICKEN0,
1030477eb7f9SFrançois Tigeot 				  HDC_FENCE_DEST_SLM_DISABLE |
1031477eb7f9SFrançois Tigeot 				  HDC_BARRIER_PERFORMANCE_DISABLE);
1032477eb7f9SFrançois Tigeot 
103319c468b4SFrançois Tigeot 	if (INTEL_REVID(dev) <= SKL_REVID_D0) {
103419c468b4SFrançois Tigeot 		/*
103519c468b4SFrançois Tigeot 		 *Use Force Non-Coherent whenever executing a 3D context. This
103619c468b4SFrançois Tigeot 		 * is a workaround for a possible hang in the unlikely event
103719c468b4SFrançois Tigeot 		 * a TLB invalidation occurs during a PSD flush.
103819c468b4SFrançois Tigeot 		 */
103919c468b4SFrançois Tigeot 		/* WaForceEnableNonCoherent:skl */
104019c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(HDC_CHICKEN0,
104119c468b4SFrançois Tigeot 				  HDC_FORCE_NON_COHERENT);
104219c468b4SFrançois Tigeot 	}
104319c468b4SFrançois Tigeot 
1044477eb7f9SFrançois Tigeot 	return skl_tune_iz_hashing(ring);
1045477eb7f9SFrançois Tigeot }
1046477eb7f9SFrançois Tigeot 
104719c468b4SFrançois Tigeot static int bxt_init_workarounds(struct intel_engine_cs *ring)
104819c468b4SFrançois Tigeot {
104919c468b4SFrançois Tigeot 	struct drm_device *dev = ring->dev;
105019c468b4SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
105119c468b4SFrançois Tigeot 
105219c468b4SFrançois Tigeot 	gen9_init_workarounds(ring);
105319c468b4SFrançois Tigeot 
105419c468b4SFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:bxt */
105519c468b4SFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
105619c468b4SFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
105719c468b4SFrançois Tigeot 
105819c468b4SFrançois Tigeot 	/* WaDisableSbeCacheDispatchPortSharing:bxt */
105919c468b4SFrançois Tigeot 	if (INTEL_REVID(dev) <= BXT_REVID_B0) {
106019c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(
106119c468b4SFrançois Tigeot 			GEN7_HALF_SLICE_CHICKEN1,
106219c468b4SFrançois Tigeot 			GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
106319c468b4SFrançois Tigeot 	}
106419c468b4SFrançois Tigeot 
106519c468b4SFrançois Tigeot 	return 0;
106619c468b4SFrançois Tigeot }
106719c468b4SFrançois Tigeot 
10682c9916cdSFrançois Tigeot int init_workarounds_ring(struct intel_engine_cs *ring)
10692c9916cdSFrançois Tigeot {
10702c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
10712c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
10722c9916cdSFrançois Tigeot 
10732c9916cdSFrançois Tigeot 	WARN_ON(ring->id != RCS);
10742c9916cdSFrançois Tigeot 
10752c9916cdSFrançois Tigeot 	dev_priv->workarounds.count = 0;
10762c9916cdSFrançois Tigeot 
10772c9916cdSFrançois Tigeot 	if (IS_BROADWELL(dev))
10782c9916cdSFrançois Tigeot 		return bdw_init_workarounds(ring);
10792c9916cdSFrançois Tigeot 
10802c9916cdSFrançois Tigeot 	if (IS_CHERRYVIEW(dev))
10812c9916cdSFrançois Tigeot 		return chv_init_workarounds(ring);
10821b13d190SFrançois Tigeot 
1083477eb7f9SFrançois Tigeot 	if (IS_SKYLAKE(dev))
1084477eb7f9SFrançois Tigeot 		return skl_init_workarounds(ring);
108519c468b4SFrançois Tigeot 
108619c468b4SFrançois Tigeot 	if (IS_BROXTON(dev))
108719c468b4SFrançois Tigeot 		return bxt_init_workarounds(ring);
1088477eb7f9SFrançois Tigeot 
10891b13d190SFrançois Tigeot 	return 0;
10901b13d190SFrançois Tigeot }
10911b13d190SFrançois Tigeot 
1092ba55f2f5SFrançois Tigeot static int init_render_ring(struct intel_engine_cs *ring)
1093e3adcf8fSFrançois Tigeot {
1094e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1095e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1096e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
109724edb884SFrançois Tigeot 	if (ret)
109824edb884SFrançois Tigeot 		return ret;
1099e3adcf8fSFrançois Tigeot 
1100ba55f2f5SFrançois Tigeot 	/* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1101ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
1102f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1103f4e1c372SFrançois Tigeot 
1104f4e1c372SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
1105f4e1c372SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1106f4e1c372SFrançois Tigeot 	 * programmed to '1' on all products.
11075d0b1887SFrançois Tigeot 	 *
1108ba55f2f5SFrançois Tigeot 	 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1109f4e1c372SFrançois Tigeot 	 */
11102c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 9)
1111f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1112f4e1c372SFrançois Tigeot 
1113f4e1c372SFrançois Tigeot 	/* Required for the hardware to program scanline values for waiting */
1114ba55f2f5SFrançois Tigeot 	/* WaEnableFlushTlbInvalidationMode:snb */
1115f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6)
1116f4e1c372SFrançois Tigeot 		I915_WRITE(GFX_MODE,
1117ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
1118f4e1c372SFrançois Tigeot 
1119ba55f2f5SFrançois Tigeot 	/* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
1120e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev))
1121e3adcf8fSFrançois Tigeot 		I915_WRITE(GFX_MODE_GEN7,
1122ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1123f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
1124e3adcf8fSFrançois Tigeot 
1125e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
1126e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
1127e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
1128e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
1129e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
1130e3adcf8fSFrançois Tigeot 		 */
1131e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
1132f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
1133e3adcf8fSFrançois Tigeot 	}
1134e3adcf8fSFrançois Tigeot 
1135f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
1136f4e1c372SFrançois Tigeot 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1137f4e1c372SFrançois Tigeot 
11389edbd4a0SFrançois Tigeot 	if (HAS_L3_DPF(dev))
11399edbd4a0SFrançois Tigeot 		I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1140e3adcf8fSFrançois Tigeot 
11412c9916cdSFrançois Tigeot 	return init_workarounds_ring(ring);
1142e3adcf8fSFrançois Tigeot }
1143e3adcf8fSFrançois Tigeot 
1144ba55f2f5SFrançois Tigeot static void render_ring_cleanup(struct intel_engine_cs *ring)
1145e3adcf8fSFrançois Tigeot {
1146b5c29a34SFrançois Tigeot 	struct drm_device *dev = ring->dev;
114724edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
114824edb884SFrançois Tigeot 
114924edb884SFrançois Tigeot 	if (dev_priv->semaphore_obj) {
115024edb884SFrançois Tigeot 		i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
115124edb884SFrançois Tigeot 		drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
115224edb884SFrançois Tigeot 		dev_priv->semaphore_obj = NULL;
115324edb884SFrançois Tigeot 	}
1154b5c29a34SFrançois Tigeot 
11551b13d190SFrançois Tigeot 	intel_fini_pipe_control(ring);
1156e3adcf8fSFrançois Tigeot }
1157e3adcf8fSFrançois Tigeot 
115824edb884SFrançois Tigeot static int gen8_rcs_signal(struct intel_engine_cs *signaller,
115924edb884SFrançois Tigeot 			   unsigned int num_dwords)
116024edb884SFrançois Tigeot {
116124edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 8
116224edb884SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
116324edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
116424edb884SFrançois Tigeot 	struct intel_engine_cs *waiter;
116524edb884SFrançois Tigeot 	int i, ret, num_rings;
116624edb884SFrançois Tigeot 
116724edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
116824edb884SFrançois Tigeot 	num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
116924edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
117024edb884SFrançois Tigeot 
117124edb884SFrançois Tigeot 	ret = intel_ring_begin(signaller, num_dwords);
117224edb884SFrançois Tigeot 	if (ret)
117324edb884SFrançois Tigeot 		return ret;
117424edb884SFrançois Tigeot 
117524edb884SFrançois Tigeot 	for_each_ring(waiter, dev_priv, i) {
11762c9916cdSFrançois Tigeot 		u32 seqno;
117724edb884SFrançois Tigeot 		u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
117824edb884SFrançois Tigeot 		if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
117924edb884SFrançois Tigeot 			continue;
118024edb884SFrançois Tigeot 
11812c9916cdSFrançois Tigeot 		seqno = i915_gem_request_get_seqno(
11822c9916cdSFrançois Tigeot 					   signaller->outstanding_lazy_request);
118324edb884SFrançois Tigeot 		intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
118424edb884SFrançois Tigeot 		intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
118524edb884SFrançois Tigeot 					   PIPE_CONTROL_QW_WRITE |
118624edb884SFrançois Tigeot 					   PIPE_CONTROL_FLUSH_ENABLE);
118724edb884SFrançois Tigeot 		intel_ring_emit(signaller, lower_32_bits(gtt_offset));
118824edb884SFrançois Tigeot 		intel_ring_emit(signaller, upper_32_bits(gtt_offset));
11892c9916cdSFrançois Tigeot 		intel_ring_emit(signaller, seqno);
119024edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
119124edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
119224edb884SFrançois Tigeot 					   MI_SEMAPHORE_TARGET(waiter->id));
119324edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
119424edb884SFrançois Tigeot 	}
119524edb884SFrançois Tigeot 
119624edb884SFrançois Tigeot 	return 0;
119724edb884SFrançois Tigeot }
119824edb884SFrançois Tigeot 
119924edb884SFrançois Tigeot static int gen8_xcs_signal(struct intel_engine_cs *signaller,
120024edb884SFrançois Tigeot 			   unsigned int num_dwords)
120124edb884SFrançois Tigeot {
120224edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 6
120324edb884SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
120424edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
120524edb884SFrançois Tigeot 	struct intel_engine_cs *waiter;
120624edb884SFrançois Tigeot 	int i, ret, num_rings;
120724edb884SFrançois Tigeot 
120824edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
120924edb884SFrançois Tigeot 	num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
121024edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
121124edb884SFrançois Tigeot 
121224edb884SFrançois Tigeot 	ret = intel_ring_begin(signaller, num_dwords);
121324edb884SFrançois Tigeot 	if (ret)
121424edb884SFrançois Tigeot 		return ret;
121524edb884SFrançois Tigeot 
121624edb884SFrançois Tigeot 	for_each_ring(waiter, dev_priv, i) {
12172c9916cdSFrançois Tigeot 		u32 seqno;
121824edb884SFrançois Tigeot 		u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
121924edb884SFrançois Tigeot 		if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
122024edb884SFrançois Tigeot 			continue;
122124edb884SFrançois Tigeot 
12222c9916cdSFrançois Tigeot 		seqno = i915_gem_request_get_seqno(
12232c9916cdSFrançois Tigeot 					   signaller->outstanding_lazy_request);
122424edb884SFrançois Tigeot 		intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
122524edb884SFrançois Tigeot 					   MI_FLUSH_DW_OP_STOREDW);
122624edb884SFrançois Tigeot 		intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
122724edb884SFrançois Tigeot 					   MI_FLUSH_DW_USE_GTT);
122824edb884SFrançois Tigeot 		intel_ring_emit(signaller, upper_32_bits(gtt_offset));
12292c9916cdSFrançois Tigeot 		intel_ring_emit(signaller, seqno);
123024edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
123124edb884SFrançois Tigeot 					   MI_SEMAPHORE_TARGET(waiter->id));
123224edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
123324edb884SFrançois Tigeot 	}
123424edb884SFrançois Tigeot 
123524edb884SFrançois Tigeot 	return 0;
123624edb884SFrançois Tigeot }
123724edb884SFrançois Tigeot 
1238ba55f2f5SFrançois Tigeot static int gen6_signal(struct intel_engine_cs *signaller,
1239ba55f2f5SFrançois Tigeot 		       unsigned int num_dwords)
1240e3adcf8fSFrançois Tigeot {
1241ba55f2f5SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
1242ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1243ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *useless;
124424edb884SFrançois Tigeot 	int i, ret, num_rings;
1245ba55f2f5SFrançois Tigeot 
124624edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 3
124724edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
124824edb884SFrançois Tigeot 	num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
124924edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
1250ba55f2f5SFrançois Tigeot 
1251ba55f2f5SFrançois Tigeot 	ret = intel_ring_begin(signaller, num_dwords);
1252ba55f2f5SFrançois Tigeot 	if (ret)
1253ba55f2f5SFrançois Tigeot 		return ret;
1254ba55f2f5SFrançois Tigeot 
1255ba55f2f5SFrançois Tigeot 	for_each_ring(useless, dev_priv, i) {
1256ba55f2f5SFrançois Tigeot 		u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1257ba55f2f5SFrançois Tigeot 		if (mbox_reg != GEN6_NOSYNC) {
12582c9916cdSFrançois Tigeot 			u32 seqno = i915_gem_request_get_seqno(
12592c9916cdSFrançois Tigeot 					   signaller->outstanding_lazy_request);
1260ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1261ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, mbox_reg);
12622c9916cdSFrançois Tigeot 			intel_ring_emit(signaller, seqno);
1263ba55f2f5SFrançois Tigeot 		}
1264ba55f2f5SFrançois Tigeot 	}
1265ba55f2f5SFrançois Tigeot 
126624edb884SFrançois Tigeot 	/* If num_dwords was rounded, make sure the tail pointer is correct */
126724edb884SFrançois Tigeot 	if (num_rings % 2 == 0)
126824edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_NOOP);
126924edb884SFrançois Tigeot 
1270ba55f2f5SFrançois Tigeot 	return 0;
1271e3adcf8fSFrançois Tigeot }
1272e3adcf8fSFrançois Tigeot 
1273e3adcf8fSFrançois Tigeot /**
1274e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
1275e3adcf8fSFrançois Tigeot  *
1276e3adcf8fSFrançois Tigeot  * @ring - ring that is adding a request
1277e3adcf8fSFrançois Tigeot  * @seqno - return seqno stuck into the ring
1278e3adcf8fSFrançois Tigeot  *
1279e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
1280e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
1281e3adcf8fSFrançois Tigeot  */
1282e3adcf8fSFrançois Tigeot static int
1283ba55f2f5SFrançois Tigeot gen6_add_request(struct intel_engine_cs *ring)
1284e3adcf8fSFrançois Tigeot {
1285ba55f2f5SFrançois Tigeot 	int ret;
1286e3adcf8fSFrançois Tigeot 
128724edb884SFrançois Tigeot 	if (ring->semaphore.signal)
1288ba55f2f5SFrançois Tigeot 		ret = ring->semaphore.signal(ring, 4);
128924edb884SFrançois Tigeot 	else
129024edb884SFrançois Tigeot 		ret = intel_ring_begin(ring, 4);
129124edb884SFrançois Tigeot 
12929edbd4a0SFrançois Tigeot 	if (ret)
12939edbd4a0SFrançois Tigeot 		return ret;
12949edbd4a0SFrançois Tigeot 
1295e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1296e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
12972c9916cdSFrançois Tigeot 	intel_ring_emit(ring,
12982c9916cdSFrançois Tigeot 		    i915_gem_request_get_seqno(ring->outstanding_lazy_request));
1299e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
13009edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1301e3adcf8fSFrançois Tigeot 
1302e3adcf8fSFrançois Tigeot 	return 0;
1303e3adcf8fSFrançois Tigeot }
1304e3adcf8fSFrançois Tigeot 
1305a2fdbec6SFrançois Tigeot static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1306a2fdbec6SFrançois Tigeot 					      u32 seqno)
1307a2fdbec6SFrançois Tigeot {
1308a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1309a2fdbec6SFrançois Tigeot 	return dev_priv->last_seqno < seqno;
1310a2fdbec6SFrançois Tigeot }
1311a2fdbec6SFrançois Tigeot 
1312e3adcf8fSFrançois Tigeot /**
1313e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
1314e3adcf8fSFrançois Tigeot  *
1315e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
1316e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
1317e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
1318e3adcf8fSFrançois Tigeot  */
131924edb884SFrançois Tigeot 
132024edb884SFrançois Tigeot static int
132124edb884SFrançois Tigeot gen8_ring_sync(struct intel_engine_cs *waiter,
132224edb884SFrançois Tigeot 	       struct intel_engine_cs *signaller,
132324edb884SFrançois Tigeot 	       u32 seqno)
132424edb884SFrançois Tigeot {
132524edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = waiter->dev->dev_private;
132624edb884SFrançois Tigeot 	int ret;
132724edb884SFrançois Tigeot 
132824edb884SFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
132924edb884SFrançois Tigeot 	if (ret)
133024edb884SFrançois Tigeot 		return ret;
133124edb884SFrançois Tigeot 
133224edb884SFrançois Tigeot 	intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
133324edb884SFrançois Tigeot 				MI_SEMAPHORE_GLOBAL_GTT |
133424edb884SFrançois Tigeot 				MI_SEMAPHORE_POLL |
133524edb884SFrançois Tigeot 				MI_SEMAPHORE_SAD_GTE_SDD);
133624edb884SFrançois Tigeot 	intel_ring_emit(waiter, seqno);
133724edb884SFrançois Tigeot 	intel_ring_emit(waiter,
133824edb884SFrançois Tigeot 			lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
133924edb884SFrançois Tigeot 	intel_ring_emit(waiter,
134024edb884SFrançois Tigeot 			upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
134124edb884SFrançois Tigeot 	intel_ring_advance(waiter);
134224edb884SFrançois Tigeot 	return 0;
134324edb884SFrançois Tigeot }
134424edb884SFrançois Tigeot 
1345e3adcf8fSFrançois Tigeot static int
1346ba55f2f5SFrançois Tigeot gen6_ring_sync(struct intel_engine_cs *waiter,
1347ba55f2f5SFrançois Tigeot 	       struct intel_engine_cs *signaller,
1348e3adcf8fSFrançois Tigeot 	       u32 seqno)
1349e3adcf8fSFrançois Tigeot {
1350e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
1351e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
1352e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
1353ba55f2f5SFrançois Tigeot 	u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1354ba55f2f5SFrançois Tigeot 	int ret;
1355e3adcf8fSFrançois Tigeot 
1356686a02f1SFrançois Tigeot 	/* Throughout all of the GEM code, seqno passed implies our current
1357686a02f1SFrançois Tigeot 	 * seqno is >= the last seqno executed. However for hardware the
1358686a02f1SFrançois Tigeot 	 * comparison is strictly greater than.
1359686a02f1SFrançois Tigeot 	 */
1360686a02f1SFrançois Tigeot 	seqno -= 1;
1361686a02f1SFrançois Tigeot 
1362ba55f2f5SFrançois Tigeot 	WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
1363686a02f1SFrançois Tigeot 
1364e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
1365e3adcf8fSFrançois Tigeot 	if (ret)
1366e3adcf8fSFrançois Tigeot 		return ret;
1367e3adcf8fSFrançois Tigeot 
1368a2fdbec6SFrançois Tigeot 	/* If seqno wrap happened, omit the wait with no-ops */
1369a2fdbec6SFrançois Tigeot 	if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
1370ba55f2f5SFrançois Tigeot 		intel_ring_emit(waiter, dw1 | wait_mbox);
1371e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, seqno);
1372e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, 0);
1373e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1374a2fdbec6SFrançois Tigeot 	} else {
1375a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1376a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1377a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1378a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1379a2fdbec6SFrançois Tigeot 	}
1380e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
1381e3adcf8fSFrançois Tigeot 
1382e3adcf8fSFrançois Tigeot 	return 0;
1383e3adcf8fSFrançois Tigeot }
1384e3adcf8fSFrançois Tigeot 
1385e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
1386e3adcf8fSFrançois Tigeot do {									\
1387e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
1388e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
1389e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
1390e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
1391e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
1392e3adcf8fSFrançois Tigeot } while (0)
1393e3adcf8fSFrançois Tigeot 
1394e3adcf8fSFrançois Tigeot static int
1395ba55f2f5SFrançois Tigeot pc_render_add_request(struct intel_engine_cs *ring)
1396e3adcf8fSFrançois Tigeot {
1397ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
1398e3adcf8fSFrançois Tigeot 	int ret;
1399e3adcf8fSFrançois Tigeot 
1400e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1401e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
1402e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
1403e3adcf8fSFrançois Tigeot 	 *
1404e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
1405e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1406e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
1407e3adcf8fSFrançois Tigeot 	 */
1408e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 32);
1409e3adcf8fSFrançois Tigeot 	if (ret)
1410e3adcf8fSFrançois Tigeot 		return ret;
1411e3adcf8fSFrançois Tigeot 
1412e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1413e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
1414e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
14159edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
14162c9916cdSFrançois Tigeot 	intel_ring_emit(ring,
14172c9916cdSFrançois Tigeot 		    i915_gem_request_get_seqno(ring->outstanding_lazy_request));
1418e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1419e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1420ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
1421e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1422ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1423e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1424ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1425e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1426ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1427e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1428ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1429e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1430b5c29a34SFrançois Tigeot 
1431e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1432e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
1433e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
1434e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
14359edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
14362c9916cdSFrançois Tigeot 	intel_ring_emit(ring,
14372c9916cdSFrançois Tigeot 		    i915_gem_request_get_seqno(ring->outstanding_lazy_request));
1438e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
14399edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1440e3adcf8fSFrançois Tigeot 
1441e3adcf8fSFrançois Tigeot 	return 0;
1442e3adcf8fSFrançois Tigeot }
1443e3adcf8fSFrançois Tigeot 
1444e3adcf8fSFrançois Tigeot static u32
1445ba55f2f5SFrançois Tigeot gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1446e3adcf8fSFrançois Tigeot {
1447e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
1448e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
1449e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
1450ba55f2f5SFrançois Tigeot 	if (!lazy_coherency) {
1451ba55f2f5SFrançois Tigeot 		struct drm_i915_private *dev_priv = ring->dev->dev_private;
1452ba55f2f5SFrançois Tigeot 		POSTING_READ(RING_ACTHD(ring->mmio_base));
1453ba55f2f5SFrançois Tigeot 	}
1454ba55f2f5SFrançois Tigeot 
1455e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1456e3adcf8fSFrançois Tigeot }
1457e3adcf8fSFrançois Tigeot 
1458b030f26bSFrançois Tigeot static u32
1459ba55f2f5SFrançois Tigeot ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1460e3adcf8fSFrançois Tigeot {
1461e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1462e3adcf8fSFrançois Tigeot }
1463e3adcf8fSFrançois Tigeot 
1464a2fdbec6SFrançois Tigeot static void
1465ba55f2f5SFrançois Tigeot ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1466a2fdbec6SFrançois Tigeot {
1467a2fdbec6SFrançois Tigeot 	intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1468a2fdbec6SFrançois Tigeot }
1469a2fdbec6SFrançois Tigeot 
1470b030f26bSFrançois Tigeot static u32
1471ba55f2f5SFrançois Tigeot pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1472e3adcf8fSFrançois Tigeot {
14739edbd4a0SFrançois Tigeot 	return ring->scratch.cpu_page[0];
1474e3adcf8fSFrançois Tigeot }
1475e3adcf8fSFrançois Tigeot 
1476a2fdbec6SFrançois Tigeot static void
1477ba55f2f5SFrançois Tigeot pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1478a2fdbec6SFrançois Tigeot {
14799edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page[0] = seqno;
1480a2fdbec6SFrançois Tigeot }
1481a2fdbec6SFrançois Tigeot 
1482e3adcf8fSFrançois Tigeot static bool
1483ba55f2f5SFrançois Tigeot gen5_ring_get_irq(struct intel_engine_cs *ring)
1484e3adcf8fSFrançois Tigeot {
1485e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1486ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1487e3adcf8fSFrançois Tigeot 
14882c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1489e3adcf8fSFrançois Tigeot 		return false;
1490e3adcf8fSFrançois Tigeot 
149102727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
14929edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0)
149324edb884SFrançois Tigeot 		gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
149402727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1495e3adcf8fSFrançois Tigeot 
1496e3adcf8fSFrançois Tigeot 	return true;
1497e3adcf8fSFrançois Tigeot }
1498e3adcf8fSFrançois Tigeot 
1499e3adcf8fSFrançois Tigeot static void
1500ba55f2f5SFrançois Tigeot gen5_ring_put_irq(struct intel_engine_cs *ring)
1501e3adcf8fSFrançois Tigeot {
1502e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1503ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1504e3adcf8fSFrançois Tigeot 
150502727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15069edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0)
150724edb884SFrançois Tigeot 		gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1508686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1509686a02f1SFrançois Tigeot }
1510686a02f1SFrançois Tigeot 
1511686a02f1SFrançois Tigeot static bool
1512ba55f2f5SFrançois Tigeot i9xx_ring_get_irq(struct intel_engine_cs *ring)
1513686a02f1SFrançois Tigeot {
1514686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1515ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1516686a02f1SFrançois Tigeot 
15172c9916cdSFrançois Tigeot 	if (!intel_irqs_enabled(dev_priv))
1518686a02f1SFrançois Tigeot 		return false;
1519686a02f1SFrançois Tigeot 
1520686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15219edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
1522686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
1523686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
1524686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
1525686a02f1SFrançois Tigeot 	}
1526686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1527686a02f1SFrançois Tigeot 
1528686a02f1SFrançois Tigeot 	return true;
1529686a02f1SFrançois Tigeot }
1530686a02f1SFrançois Tigeot 
1531686a02f1SFrançois Tigeot static void
1532ba55f2f5SFrançois Tigeot i9xx_ring_put_irq(struct intel_engine_cs *ring)
1533686a02f1SFrançois Tigeot {
1534686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1535ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1536686a02f1SFrançois Tigeot 
1537686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15389edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
1539686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
1540686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
1541686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
1542686a02f1SFrançois Tigeot 	}
1543686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1544686a02f1SFrançois Tigeot }
1545686a02f1SFrançois Tigeot 
1546686a02f1SFrançois Tigeot static bool
1547ba55f2f5SFrançois Tigeot i8xx_ring_get_irq(struct intel_engine_cs *ring)
1548686a02f1SFrançois Tigeot {
1549686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1550ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1551686a02f1SFrançois Tigeot 
15522c9916cdSFrançois Tigeot 	if (!intel_irqs_enabled(dev_priv))
1553686a02f1SFrançois Tigeot 		return false;
1554686a02f1SFrançois Tigeot 
1555686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15569edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
1557686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
1558686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
1559686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
1560686a02f1SFrançois Tigeot 	}
1561686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1562686a02f1SFrançois Tigeot 
1563686a02f1SFrançois Tigeot 	return true;
1564686a02f1SFrançois Tigeot }
1565686a02f1SFrançois Tigeot 
1566686a02f1SFrançois Tigeot static void
1567ba55f2f5SFrançois Tigeot i8xx_ring_put_irq(struct intel_engine_cs *ring)
1568686a02f1SFrançois Tigeot {
1569686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1570ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1571686a02f1SFrançois Tigeot 
1572686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15739edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
1574686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
1575686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
1576686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
1577e3adcf8fSFrançois Tigeot 	}
157802727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1579e3adcf8fSFrançois Tigeot }
1580e3adcf8fSFrançois Tigeot 
1581e3adcf8fSFrançois Tigeot static int
1582ba55f2f5SFrançois Tigeot bsd_ring_flush(struct intel_engine_cs *ring,
1583b5c29a34SFrançois Tigeot 	       u32     invalidate_domains,
1584b5c29a34SFrançois Tigeot 	       u32     flush_domains)
1585e3adcf8fSFrançois Tigeot {
1586e3adcf8fSFrançois Tigeot 	int ret;
1587e3adcf8fSFrançois Tigeot 
1588e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1589e3adcf8fSFrançois Tigeot 	if (ret)
1590e3adcf8fSFrançois Tigeot 		return ret;
1591e3adcf8fSFrançois Tigeot 
1592e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
1593e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1594e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1595e3adcf8fSFrançois Tigeot 	return 0;
1596e3adcf8fSFrançois Tigeot }
1597e3adcf8fSFrançois Tigeot 
1598e3adcf8fSFrançois Tigeot static int
1599ba55f2f5SFrançois Tigeot i9xx_add_request(struct intel_engine_cs *ring)
1600e3adcf8fSFrançois Tigeot {
1601e3adcf8fSFrançois Tigeot 	int ret;
1602e3adcf8fSFrançois Tigeot 
1603e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1604e3adcf8fSFrançois Tigeot 	if (ret)
1605e3adcf8fSFrançois Tigeot 		return ret;
1606e3adcf8fSFrançois Tigeot 
1607e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1608e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
16092c9916cdSFrançois Tigeot 	intel_ring_emit(ring,
16102c9916cdSFrançois Tigeot 		    i915_gem_request_get_seqno(ring->outstanding_lazy_request));
1611e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
16129edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1613e3adcf8fSFrançois Tigeot 
1614e3adcf8fSFrançois Tigeot 	return 0;
1615e3adcf8fSFrançois Tigeot }
1616e3adcf8fSFrançois Tigeot 
1617e3adcf8fSFrançois Tigeot static bool
1618ba55f2f5SFrançois Tigeot gen6_ring_get_irq(struct intel_engine_cs *ring)
1619e3adcf8fSFrançois Tigeot {
1620e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1621ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1622e3adcf8fSFrançois Tigeot 
16232c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1624e3adcf8fSFrançois Tigeot 		return false;
1625e3adcf8fSFrançois Tigeot 
162602727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
16279edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
16289edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
16295d0b1887SFrançois Tigeot 			I915_WRITE_IMR(ring,
16305d0b1887SFrançois Tigeot 				       ~(ring->irq_enable_mask |
16319edbd4a0SFrançois Tigeot 					 GT_PARITY_ERROR(dev)));
1632686a02f1SFrançois Tigeot 		else
1633686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
163424edb884SFrançois Tigeot 		gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1635e3adcf8fSFrançois Tigeot 	}
163602727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1637e3adcf8fSFrançois Tigeot 
1638e3adcf8fSFrançois Tigeot 	return true;
1639e3adcf8fSFrançois Tigeot }
1640e3adcf8fSFrançois Tigeot 
1641e3adcf8fSFrançois Tigeot static void
1642ba55f2f5SFrançois Tigeot gen6_ring_put_irq(struct intel_engine_cs *ring)
1643e3adcf8fSFrançois Tigeot {
1644e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1645ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1646e3adcf8fSFrançois Tigeot 
164702727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
16489edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
16499edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
16509edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1651686a02f1SFrançois Tigeot 		else
1652686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
165324edb884SFrançois Tigeot 		gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1654e3adcf8fSFrançois Tigeot 	}
165502727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1656e3adcf8fSFrançois Tigeot }
1657e3adcf8fSFrançois Tigeot 
16585d0b1887SFrançois Tigeot static bool
1659ba55f2f5SFrançois Tigeot hsw_vebox_get_irq(struct intel_engine_cs *ring)
16605d0b1887SFrançois Tigeot {
16615d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
16625d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16635d0b1887SFrançois Tigeot 
16642c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
16655d0b1887SFrançois Tigeot 		return false;
16665d0b1887SFrançois Tigeot 
16679edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
16689edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
16695d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
167024edb884SFrançois Tigeot 		gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
16715d0b1887SFrançois Tigeot 	}
16729edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
16735d0b1887SFrançois Tigeot 
16745d0b1887SFrançois Tigeot 	return true;
16755d0b1887SFrançois Tigeot }
16765d0b1887SFrançois Tigeot 
16775d0b1887SFrançois Tigeot static void
1678ba55f2f5SFrançois Tigeot hsw_vebox_put_irq(struct intel_engine_cs *ring)
16795d0b1887SFrançois Tigeot {
16805d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
16815d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16825d0b1887SFrançois Tigeot 
16839edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
16849edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
16855d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~0);
168624edb884SFrançois Tigeot 		gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
16875d0b1887SFrançois Tigeot 	}
16889edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
16899edbd4a0SFrançois Tigeot }
16909edbd4a0SFrançois Tigeot 
16919edbd4a0SFrançois Tigeot static bool
1692ba55f2f5SFrançois Tigeot gen8_ring_get_irq(struct intel_engine_cs *ring)
16939edbd4a0SFrançois Tigeot {
16949edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
16959edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16969edbd4a0SFrançois Tigeot 
16972c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
16989edbd4a0SFrançois Tigeot 		return false;
16999edbd4a0SFrançois Tigeot 
17009edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
17019edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
17029edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
17039edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
17049edbd4a0SFrançois Tigeot 				       ~(ring->irq_enable_mask |
17059edbd4a0SFrançois Tigeot 					 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
17069edbd4a0SFrançois Tigeot 		} else {
17079edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
17089edbd4a0SFrançois Tigeot 		}
17099edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
17109edbd4a0SFrançois Tigeot 	}
17119edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
17129edbd4a0SFrançois Tigeot 
17139edbd4a0SFrançois Tigeot 	return true;
17149edbd4a0SFrançois Tigeot }
17159edbd4a0SFrançois Tigeot 
17169edbd4a0SFrançois Tigeot static void
1717ba55f2f5SFrançois Tigeot gen8_ring_put_irq(struct intel_engine_cs *ring)
17189edbd4a0SFrançois Tigeot {
17199edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
17209edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
17219edbd4a0SFrançois Tigeot 
17229edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
17239edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
17249edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
17259edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
17269edbd4a0SFrançois Tigeot 				       ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
17279edbd4a0SFrançois Tigeot 		} else {
17289edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
17299edbd4a0SFrançois Tigeot 		}
17309edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
17319edbd4a0SFrançois Tigeot 	}
17329edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
17335d0b1887SFrançois Tigeot }
17345d0b1887SFrançois Tigeot 
1735e3adcf8fSFrançois Tigeot static int
1736ba55f2f5SFrançois Tigeot i965_dispatch_execbuffer(struct intel_engine_cs *ring,
1737ba55f2f5SFrançois Tigeot 			 u64 offset, u32 length,
1738477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1739e3adcf8fSFrançois Tigeot {
1740e3adcf8fSFrançois Tigeot 	int ret;
1741e3adcf8fSFrançois Tigeot 
1742e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1743e3adcf8fSFrançois Tigeot 	if (ret)
1744e3adcf8fSFrançois Tigeot 		return ret;
1745e3adcf8fSFrançois Tigeot 
1746e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
1747686a02f1SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1748b5c29a34SFrançois Tigeot 			MI_BATCH_GTT |
1749477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
1750477eb7f9SFrançois Tigeot 			 0 : MI_BATCH_NON_SECURE_I965));
1751e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1752e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1753e3adcf8fSFrançois Tigeot 
1754e3adcf8fSFrançois Tigeot 	return 0;
1755e3adcf8fSFrançois Tigeot }
1756e3adcf8fSFrançois Tigeot 
1757b5c29a34SFrançois Tigeot /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1758b5c29a34SFrançois Tigeot #define I830_BATCH_LIMIT (256*1024)
175924edb884SFrançois Tigeot #define I830_TLB_ENTRIES (2)
176024edb884SFrançois Tigeot #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
1761e3adcf8fSFrançois Tigeot static int
1762ba55f2f5SFrançois Tigeot i830_dispatch_execbuffer(struct intel_engine_cs *ring,
1763ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1764477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1765e3adcf8fSFrançois Tigeot {
176624edb884SFrançois Tigeot 	u32 cs_offset = ring->scratch.gtt_offset;
1767e3adcf8fSFrançois Tigeot 	int ret;
1768e3adcf8fSFrançois Tigeot 
176924edb884SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
177024edb884SFrançois Tigeot 	if (ret)
177124edb884SFrançois Tigeot 		return ret;
177224edb884SFrançois Tigeot 
177324edb884SFrançois Tigeot 	/* Evict the invalid PTE TLBs */
177424edb884SFrançois Tigeot 	intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
177524edb884SFrançois Tigeot 	intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
177624edb884SFrançois Tigeot 	intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
177724edb884SFrançois Tigeot 	intel_ring_emit(ring, cs_offset);
177824edb884SFrançois Tigeot 	intel_ring_emit(ring, 0xdeadbeef);
177924edb884SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
178024edb884SFrançois Tigeot 	intel_ring_advance(ring);
178124edb884SFrançois Tigeot 
1782477eb7f9SFrançois Tigeot 	if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
178324edb884SFrançois Tigeot 		if (len > I830_BATCH_LIMIT)
178424edb884SFrançois Tigeot 			return -ENOSPC;
178524edb884SFrançois Tigeot 
178624edb884SFrançois Tigeot 		ret = intel_ring_begin(ring, 6 + 2);
178724edb884SFrançois Tigeot 		if (ret)
178824edb884SFrançois Tigeot 			return ret;
178924edb884SFrançois Tigeot 
179024edb884SFrançois Tigeot 		/* Blit the batch (which has now all relocs applied) to the
179124edb884SFrançois Tigeot 		 * stable batch scratch bo area (so that the CS never
179224edb884SFrançois Tigeot 		 * stumbles over its tlb invalidation bug) ...
179324edb884SFrançois Tigeot 		 */
179424edb884SFrançois Tigeot 		intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
179524edb884SFrançois Tigeot 		intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
179624edb884SFrançois Tigeot 		intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
179724edb884SFrançois Tigeot 		intel_ring_emit(ring, cs_offset);
179824edb884SFrançois Tigeot 		intel_ring_emit(ring, 4096);
179924edb884SFrançois Tigeot 		intel_ring_emit(ring, offset);
180024edb884SFrançois Tigeot 
180124edb884SFrançois Tigeot 		intel_ring_emit(ring, MI_FLUSH);
180224edb884SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
180324edb884SFrançois Tigeot 		intel_ring_advance(ring);
180424edb884SFrançois Tigeot 
180524edb884SFrançois Tigeot 		/* ... and execute it. */
180624edb884SFrançois Tigeot 		offset = cs_offset;
180724edb884SFrançois Tigeot 	}
180824edb884SFrançois Tigeot 
1809e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1810e3adcf8fSFrançois Tigeot 	if (ret)
1811e3adcf8fSFrançois Tigeot 		return ret;
1812e3adcf8fSFrançois Tigeot 
1813e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER);
1814477eb7f9SFrançois Tigeot 	intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1815477eb7f9SFrançois Tigeot 					0 : MI_BATCH_NON_SECURE));
1816e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset + len - 8);
1817b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1818686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
1819686a02f1SFrançois Tigeot 
1820686a02f1SFrançois Tigeot 	return 0;
1821686a02f1SFrançois Tigeot }
1822686a02f1SFrançois Tigeot 
1823686a02f1SFrançois Tigeot static int
1824ba55f2f5SFrançois Tigeot i915_dispatch_execbuffer(struct intel_engine_cs *ring,
1825ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1826477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1827686a02f1SFrançois Tigeot {
1828686a02f1SFrançois Tigeot 	int ret;
1829686a02f1SFrançois Tigeot 
1830e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1831e3adcf8fSFrançois Tigeot 	if (ret)
1832e3adcf8fSFrançois Tigeot 		return ret;
1833e3adcf8fSFrançois Tigeot 
1834686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1835477eb7f9SFrançois Tigeot 	intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1836477eb7f9SFrançois Tigeot 					0 : MI_BATCH_NON_SECURE));
1837e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1838e3adcf8fSFrançois Tigeot 
1839e3adcf8fSFrançois Tigeot 	return 0;
1840e3adcf8fSFrançois Tigeot }
1841e3adcf8fSFrançois Tigeot 
1842ba55f2f5SFrançois Tigeot static void cleanup_status_page(struct intel_engine_cs *ring)
1843e3adcf8fSFrançois Tigeot {
1844e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1845e3adcf8fSFrançois Tigeot 
1846e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
1847e3adcf8fSFrançois Tigeot 	if (obj == NULL)
1848e3adcf8fSFrançois Tigeot 		return;
1849e3adcf8fSFrançois Tigeot 
18509edbd4a0SFrançois Tigeot 	kunmap(obj->pages[0]);
1851ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(obj);
1852e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1853e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
1854e3adcf8fSFrançois Tigeot }
1855e3adcf8fSFrançois Tigeot 
1856ba55f2f5SFrançois Tigeot static int init_status_page(struct intel_engine_cs *ring)
1857e3adcf8fSFrançois Tigeot {
1858e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1859ba55f2f5SFrançois Tigeot 
1860ba55f2f5SFrançois Tigeot 	if ((obj = ring->status_page.obj) == NULL) {
186124edb884SFrançois Tigeot 		unsigned flags;
1862e3adcf8fSFrançois Tigeot 		int ret;
1863e3adcf8fSFrançois Tigeot 
1864ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(ring->dev, 4096);
1865e3adcf8fSFrançois Tigeot 		if (obj == NULL) {
1866e3adcf8fSFrançois Tigeot 			DRM_ERROR("Failed to allocate status page\n");
1867ba55f2f5SFrançois Tigeot 			return -ENOMEM;
1868e3adcf8fSFrançois Tigeot 		}
1869e3adcf8fSFrançois Tigeot 
1870ba55f2f5SFrançois Tigeot 		ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1871ba55f2f5SFrançois Tigeot 		if (ret)
1872e3adcf8fSFrançois Tigeot 			goto err_unref;
1873ba55f2f5SFrançois Tigeot 
187424edb884SFrançois Tigeot 		flags = 0;
187524edb884SFrançois Tigeot 		if (!HAS_LLC(ring->dev))
187624edb884SFrançois Tigeot 			/* On g33, we cannot place HWS above 256MiB, so
187724edb884SFrançois Tigeot 			 * restrict its pinning to the low mappable arena.
187824edb884SFrançois Tigeot 			 * Though this restriction is not documented for
187924edb884SFrançois Tigeot 			 * gen4, gen5, or byt, they also behave similarly
188024edb884SFrançois Tigeot 			 * and hang if the HWS is placed at the top of the
188124edb884SFrançois Tigeot 			 * GTT. To generalise, it appears that all !llc
188224edb884SFrançois Tigeot 			 * platforms have issues with us placing the HWS
188324edb884SFrançois Tigeot 			 * above the mappable region (even though we never
188424edb884SFrançois Tigeot 			 * actualy map it).
188524edb884SFrançois Tigeot 			 */
188624edb884SFrançois Tigeot 			flags |= PIN_MAPPABLE;
188724edb884SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
1888ba55f2f5SFrançois Tigeot 		if (ret) {
1889ba55f2f5SFrançois Tigeot err_unref:
1890ba55f2f5SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
1891ba55f2f5SFrançois Tigeot 			return ret;
1892ba55f2f5SFrançois Tigeot 		}
1893ba55f2f5SFrançois Tigeot 
1894ba55f2f5SFrançois Tigeot 		ring->status_page.obj = obj;
1895e3adcf8fSFrançois Tigeot 	}
1896e3adcf8fSFrançois Tigeot 
18979edbd4a0SFrançois Tigeot 	ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1898f4f90b23SFrançois Tigeot 	ring->status_page.page_addr = kmap(obj->pages[0]);
1899e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1900e3adcf8fSFrançois Tigeot 
1901b5c29a34SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1902e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1903e3adcf8fSFrançois Tigeot 
1904e3adcf8fSFrançois Tigeot 	return 0;
1905e3adcf8fSFrançois Tigeot }
1906e3adcf8fSFrançois Tigeot 
1907ba55f2f5SFrançois Tigeot static int init_phys_status_page(struct intel_engine_cs *ring)
1908686a02f1SFrançois Tigeot {
1909686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1910686a02f1SFrançois Tigeot 
1911686a02f1SFrançois Tigeot 	if (!dev_priv->status_page_dmah) {
1912686a02f1SFrançois Tigeot 		dev_priv->status_page_dmah =
1913b31e9d59SFrançois Tigeot 			drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1914686a02f1SFrançois Tigeot 		if (!dev_priv->status_page_dmah)
1915686a02f1SFrançois Tigeot 			return -ENOMEM;
1916686a02f1SFrançois Tigeot 	}
1917686a02f1SFrançois Tigeot 
1918686a02f1SFrançois Tigeot 	ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1919686a02f1SFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1920686a02f1SFrançois Tigeot 
1921686a02f1SFrançois Tigeot 	return 0;
1922686a02f1SFrançois Tigeot }
1923686a02f1SFrançois Tigeot 
19242c9916cdSFrançois Tigeot void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
19252c9916cdSFrançois Tigeot {
1926*24409b39SFrançois Tigeot 	iounmap(ringbuf->virtual_start);
19272c9916cdSFrançois Tigeot 	ringbuf->virtual_start = NULL;
19282c9916cdSFrançois Tigeot 	i915_gem_object_ggtt_unpin(ringbuf->obj);
19292c9916cdSFrançois Tigeot }
19302c9916cdSFrançois Tigeot 
19312c9916cdSFrançois Tigeot int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
19322c9916cdSFrançois Tigeot 				     struct intel_ringbuffer *ringbuf)
19332c9916cdSFrançois Tigeot {
19342c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
19352c9916cdSFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
19362c9916cdSFrançois Tigeot 	int ret;
19372c9916cdSFrançois Tigeot 
19382c9916cdSFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
19392c9916cdSFrançois Tigeot 	if (ret)
19402c9916cdSFrançois Tigeot 		return ret;
19412c9916cdSFrançois Tigeot 
19422c9916cdSFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
19432c9916cdSFrançois Tigeot 	if (ret) {
19442c9916cdSFrançois Tigeot 		i915_gem_object_ggtt_unpin(obj);
19452c9916cdSFrançois Tigeot 		return ret;
19462c9916cdSFrançois Tigeot 	}
19472c9916cdSFrançois Tigeot 
19482c9916cdSFrançois Tigeot 	ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
19492c9916cdSFrançois Tigeot 			i915_gem_obj_ggtt_offset(obj), ringbuf->size);
19502c9916cdSFrançois Tigeot 	if (ringbuf->virtual_start == NULL) {
19512c9916cdSFrançois Tigeot 		i915_gem_object_ggtt_unpin(obj);
19522c9916cdSFrançois Tigeot 		return -EINVAL;
19532c9916cdSFrançois Tigeot 	}
19542c9916cdSFrançois Tigeot 
19552c9916cdSFrançois Tigeot 	return 0;
19562c9916cdSFrançois Tigeot }
19572c9916cdSFrançois Tigeot 
19581b13d190SFrançois Tigeot void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1959e3adcf8fSFrançois Tigeot {
196024edb884SFrançois Tigeot 	drm_gem_object_unreference(&ringbuf->obj->base);
196124edb884SFrançois Tigeot 	ringbuf->obj = NULL;
196224edb884SFrançois Tigeot }
196324edb884SFrançois Tigeot 
19641b13d190SFrançois Tigeot int intel_alloc_ringbuffer_obj(struct drm_device *dev,
196524edb884SFrançois Tigeot 			       struct intel_ringbuffer *ringbuf)
196624edb884SFrançois Tigeot {
1967e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1968e3adcf8fSFrançois Tigeot 
1969a2fdbec6SFrançois Tigeot 	obj = NULL;
1970a2fdbec6SFrançois Tigeot 	if (!HAS_LLC(dev))
1971ba55f2f5SFrançois Tigeot 		obj = i915_gem_object_create_stolen(dev, ringbuf->size);
1972a2fdbec6SFrançois Tigeot 	if (obj == NULL)
1973ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, ringbuf->size);
1974ba55f2f5SFrançois Tigeot 	if (obj == NULL)
1975ba55f2f5SFrançois Tigeot 		return -ENOMEM;
1976e3adcf8fSFrançois Tigeot 
197724edb884SFrançois Tigeot 	/* mark ring buffers as read-only from GPU side by default */
197824edb884SFrançois Tigeot 	obj->gt_ro = 1;
197924edb884SFrançois Tigeot 
1980ba55f2f5SFrançois Tigeot 	ringbuf->obj = obj;
1981ba55f2f5SFrançois Tigeot 
19822c9916cdSFrançois Tigeot 	return 0;
1983ba55f2f5SFrançois Tigeot }
1984ba55f2f5SFrançois Tigeot 
1985ba55f2f5SFrançois Tigeot static int intel_init_ring_buffer(struct drm_device *dev,
1986ba55f2f5SFrançois Tigeot 				  struct intel_engine_cs *ring)
1987ba55f2f5SFrançois Tigeot {
19882c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf;
1989ba55f2f5SFrançois Tigeot 	int ret;
1990ba55f2f5SFrançois Tigeot 
19912c9916cdSFrançois Tigeot 	WARN_ON(ring->buffer);
19922c9916cdSFrançois Tigeot 
1993ba55f2f5SFrançois Tigeot 	ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1994ba55f2f5SFrançois Tigeot 	if (!ringbuf)
1995ba55f2f5SFrançois Tigeot 		return -ENOMEM;
1996ba55f2f5SFrançois Tigeot 	ring->buffer = ringbuf;
1997ba55f2f5SFrançois Tigeot 
1998ba55f2f5SFrançois Tigeot 	ring->dev = dev;
1999ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
2000ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
20011b13d190SFrançois Tigeot 	INIT_LIST_HEAD(&ring->execlist_queue);
200219c468b4SFrançois Tigeot 	i915_gem_batch_pool_init(dev, &ring->batch_pool);
2003ba55f2f5SFrançois Tigeot 	ringbuf->size = 32 * PAGE_SIZE;
20041b13d190SFrançois Tigeot 	ringbuf->ring = ring;
2005ba55f2f5SFrançois Tigeot 	memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
2006ba55f2f5SFrançois Tigeot 
2007ba55f2f5SFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
2008ba55f2f5SFrançois Tigeot 
2009ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
2010ba55f2f5SFrançois Tigeot 		ret = init_status_page(ring);
2011e3adcf8fSFrançois Tigeot 		if (ret)
2012ba55f2f5SFrançois Tigeot 			goto error;
2013ba55f2f5SFrançois Tigeot 	} else {
2014ba55f2f5SFrançois Tigeot 		BUG_ON(ring->id != RCS);
2015ba55f2f5SFrançois Tigeot 		ret = init_phys_status_page(ring);
2016ba55f2f5SFrançois Tigeot 		if (ret)
2017ba55f2f5SFrançois Tigeot 			goto error;
2018ba55f2f5SFrançois Tigeot 	}
2019ba55f2f5SFrançois Tigeot 
20202c9916cdSFrançois Tigeot 	WARN_ON(ringbuf->obj);
20212c9916cdSFrançois Tigeot 
202224edb884SFrançois Tigeot 	ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2023ba55f2f5SFrançois Tigeot 	if (ret) {
20242c9916cdSFrançois Tigeot 		DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
20252c9916cdSFrançois Tigeot 				ring->name, ret);
20262c9916cdSFrançois Tigeot 		goto error;
20272c9916cdSFrançois Tigeot 	}
20282c9916cdSFrançois Tigeot 
20292c9916cdSFrançois Tigeot 	ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
20302c9916cdSFrançois Tigeot 	if (ret) {
20312c9916cdSFrançois Tigeot 		DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
20322c9916cdSFrançois Tigeot 				ring->name, ret);
20332c9916cdSFrançois Tigeot 		intel_destroy_ringbuffer_obj(ringbuf);
2034ba55f2f5SFrançois Tigeot 		goto error;
2035ba55f2f5SFrançois Tigeot 	}
2036e3adcf8fSFrançois Tigeot 
2037e3adcf8fSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
2038e3adcf8fSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
2039e3adcf8fSFrançois Tigeot 	 * of the buffer.
2040e3adcf8fSFrançois Tigeot 	 */
2041ba55f2f5SFrançois Tigeot 	ringbuf->effective_size = ringbuf->size;
2042ba55f2f5SFrançois Tigeot 	if (IS_I830(dev) || IS_845G(dev))
2043ba55f2f5SFrançois Tigeot 		ringbuf->effective_size -= 2 * CACHELINE_BYTES;
2044ba55f2f5SFrançois Tigeot 
2045ba55f2f5SFrançois Tigeot 	ret = i915_cmd_parser_init_ring(ring);
2046ba55f2f5SFrançois Tigeot 	if (ret)
2047ba55f2f5SFrançois Tigeot 		goto error;
2048ba55f2f5SFrançois Tigeot 
2049e3adcf8fSFrançois Tigeot 	return 0;
2050e3adcf8fSFrançois Tigeot 
2051ba55f2f5SFrançois Tigeot error:
2052ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
2053ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
2054e3adcf8fSFrançois Tigeot 	return ret;
2055e3adcf8fSFrançois Tigeot }
2056e3adcf8fSFrançois Tigeot 
2057ba55f2f5SFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
2058e3adcf8fSFrançois Tigeot {
20592c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv;
20602c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf;
2061e3adcf8fSFrançois Tigeot 
2062ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
2063e3adcf8fSFrançois Tigeot 		return;
2064e3adcf8fSFrançois Tigeot 
20652c9916cdSFrançois Tigeot 	dev_priv = to_i915(ring->dev);
20662c9916cdSFrançois Tigeot 	ringbuf = ring->buffer;
20672c9916cdSFrançois Tigeot 
2068ba55f2f5SFrançois Tigeot 	intel_stop_ring_buffer(ring);
2069ba55f2f5SFrançois Tigeot 	WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
2070b030f26bSFrançois Tigeot 
20712c9916cdSFrançois Tigeot 	intel_unpin_ringbuffer_obj(ringbuf);
207224edb884SFrançois Tigeot 	intel_destroy_ringbuffer_obj(ringbuf);
20732c9916cdSFrançois Tigeot 	i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
2074e3adcf8fSFrançois Tigeot 
2075e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
2076e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
2077e3adcf8fSFrançois Tigeot 
2078e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
2079ba55f2f5SFrançois Tigeot 
2080ba55f2f5SFrançois Tigeot 	i915_cmd_parser_fini_ring(ring);
208119c468b4SFrançois Tigeot 	i915_gem_batch_pool_fini(&ring->batch_pool);
2082ba55f2f5SFrançois Tigeot 
2083ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
2084ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
2085e3adcf8fSFrançois Tigeot }
2086e3adcf8fSFrançois Tigeot 
208719c468b4SFrançois Tigeot static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
2088e3adcf8fSFrançois Tigeot {
2089ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2090e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
209119c468b4SFrançois Tigeot 	unsigned space;
2092e3adcf8fSFrançois Tigeot 	int ret;
2093e3adcf8fSFrançois Tigeot 
20942c9916cdSFrançois Tigeot 	if (intel_ring_space(ringbuf) >= n)
2095e3adcf8fSFrançois Tigeot 		return 0;
2096e3adcf8fSFrançois Tigeot 
2097e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
209819c468b4SFrançois Tigeot 		space = __intel_ring_space(request->postfix, ringbuf->tail,
209919c468b4SFrançois Tigeot 					   ringbuf->size);
210019c468b4SFrançois Tigeot 		if (space >= n)
2101e3adcf8fSFrançois Tigeot 			break;
2102e3adcf8fSFrançois Tigeot 	}
2103e3adcf8fSFrançois Tigeot 
210419c468b4SFrançois Tigeot 	if (WARN_ON(&request->list == &ring->request_list))
2105e3adcf8fSFrançois Tigeot 		return -ENOSPC;
2106e3adcf8fSFrançois Tigeot 
21072c9916cdSFrançois Tigeot 	ret = i915_wait_request(request);
2108e3adcf8fSFrançois Tigeot 	if (ret)
2109e3adcf8fSFrançois Tigeot 		return ret;
2110e3adcf8fSFrançois Tigeot 
211119c468b4SFrançois Tigeot 	ringbuf->space = space;
2112e3adcf8fSFrançois Tigeot 	return 0;
2113e3adcf8fSFrançois Tigeot }
2114e3adcf8fSFrançois Tigeot 
2115ba55f2f5SFrançois Tigeot static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
2116b030f26bSFrançois Tigeot {
2117b030f26bSFrançois Tigeot 	uint32_t __iomem *virt;
2118ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2119ba55f2f5SFrançois Tigeot 	int rem = ringbuf->size - ringbuf->tail;
2120b030f26bSFrançois Tigeot 
2121ba55f2f5SFrançois Tigeot 	if (ringbuf->space < rem) {
2122b030f26bSFrançois Tigeot 		int ret = ring_wait_for_space(ring, rem);
2123b030f26bSFrançois Tigeot 		if (ret)
2124b030f26bSFrançois Tigeot 			return ret;
2125b030f26bSFrançois Tigeot 	}
2126b030f26bSFrançois Tigeot 
2127ba55f2f5SFrançois Tigeot 	virt = (unsigned int *)((char *)ringbuf->virtual_start + ringbuf->tail);
2128b030f26bSFrançois Tigeot 	rem /= 4;
2129b030f26bSFrançois Tigeot 	while (rem--)
2130686a02f1SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
2131b030f26bSFrançois Tigeot 
2132ba55f2f5SFrançois Tigeot 	ringbuf->tail = 0;
21332c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
2134b030f26bSFrançois Tigeot 
2135b030f26bSFrançois Tigeot 	return 0;
2136b030f26bSFrançois Tigeot }
2137b030f26bSFrançois Tigeot 
2138ba55f2f5SFrançois Tigeot int intel_ring_idle(struct intel_engine_cs *ring)
2139b030f26bSFrançois Tigeot {
21402c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *req;
2141b5c29a34SFrançois Tigeot 	int ret;
2142b5c29a34SFrançois Tigeot 
2143b5c29a34SFrançois Tigeot 	/* We need to add any requests required to flush the objects and ring */
21442c9916cdSFrançois Tigeot 	if (ring->outstanding_lazy_request) {
21452c9916cdSFrançois Tigeot 		ret = i915_add_request(ring);
2146b5c29a34SFrançois Tigeot 		if (ret)
2147b5c29a34SFrançois Tigeot 			return ret;
2148b5c29a34SFrançois Tigeot 	}
2149b5c29a34SFrançois Tigeot 
2150b5c29a34SFrançois Tigeot 	/* Wait upon the last request to be completed */
2151b5c29a34SFrançois Tigeot 	if (list_empty(&ring->request_list))
2152b5c29a34SFrançois Tigeot 		return 0;
2153b5c29a34SFrançois Tigeot 
21542c9916cdSFrançois Tigeot 	req = list_entry(ring->request_list.prev,
2155b5c29a34SFrançois Tigeot 			struct drm_i915_gem_request,
21562c9916cdSFrançois Tigeot 			list);
2157b5c29a34SFrançois Tigeot 
215819c468b4SFrançois Tigeot 	/* Make sure we do not trigger any retires */
215919c468b4SFrançois Tigeot 	return __i915_wait_request(req,
216019c468b4SFrançois Tigeot 				   atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
216119c468b4SFrançois Tigeot 				   to_i915(ring->dev)->mm.interruptible,
216219c468b4SFrançois Tigeot 				   NULL, NULL);
2163b5c29a34SFrançois Tigeot }
2164b5c29a34SFrançois Tigeot 
216519c468b4SFrançois Tigeot int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
2166b5c29a34SFrançois Tigeot {
216719c468b4SFrançois Tigeot 	request->ringbuf = request->ring->buffer;
21682c9916cdSFrançois Tigeot 	return 0;
21699edbd4a0SFrançois Tigeot }
21709edbd4a0SFrançois Tigeot 
2171ba55f2f5SFrançois Tigeot static int __intel_ring_prepare(struct intel_engine_cs *ring,
2172a2fdbec6SFrançois Tigeot 				int bytes)
2173a2fdbec6SFrançois Tigeot {
2174ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2175a2fdbec6SFrançois Tigeot 	int ret;
2176a2fdbec6SFrançois Tigeot 
2177ba55f2f5SFrançois Tigeot 	if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
2178a2fdbec6SFrançois Tigeot 		ret = intel_wrap_ring_buffer(ring);
2179a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
2180a2fdbec6SFrançois Tigeot 			return ret;
2181a2fdbec6SFrançois Tigeot 	}
2182a2fdbec6SFrançois Tigeot 
2183ba55f2f5SFrançois Tigeot 	if (unlikely(ringbuf->space < bytes)) {
2184a2fdbec6SFrançois Tigeot 		ret = ring_wait_for_space(ring, bytes);
2185a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
2186a2fdbec6SFrançois Tigeot 			return ret;
2187a2fdbec6SFrançois Tigeot 	}
2188a2fdbec6SFrançois Tigeot 
2189a2fdbec6SFrançois Tigeot 	return 0;
2190a2fdbec6SFrançois Tigeot }
2191a2fdbec6SFrançois Tigeot 
2192ba55f2f5SFrançois Tigeot int intel_ring_begin(struct intel_engine_cs *ring,
2193e3adcf8fSFrançois Tigeot 		     int num_dwords)
2194e3adcf8fSFrançois Tigeot {
2195ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2196e3adcf8fSFrançois Tigeot 	int ret;
2197e3adcf8fSFrançois Tigeot 
2198a2fdbec6SFrançois Tigeot 	ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2199a2fdbec6SFrançois Tigeot 				   dev_priv->mm.interruptible);
2200245593daSFrançois Tigeot 	if (ret)
2201245593daSFrançois Tigeot 		return ret;
2202e3adcf8fSFrançois Tigeot 
22039edbd4a0SFrançois Tigeot 	ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
22049edbd4a0SFrançois Tigeot 	if (ret)
22059edbd4a0SFrançois Tigeot 		return ret;
22069edbd4a0SFrançois Tigeot 
2207b5c29a34SFrançois Tigeot 	/* Preallocate the olr before touching the ring */
220819c468b4SFrançois Tigeot 	ret = i915_gem_request_alloc(ring, ring->default_context);
2209b5c29a34SFrançois Tigeot 	if (ret)
2210b5c29a34SFrançois Tigeot 		return ret;
2211b5c29a34SFrançois Tigeot 
2212ba55f2f5SFrançois Tigeot 	ring->buffer->space -= num_dwords * sizeof(uint32_t);
22139edbd4a0SFrançois Tigeot 	return 0;
22149edbd4a0SFrançois Tigeot }
22159edbd4a0SFrançois Tigeot 
22169edbd4a0SFrançois Tigeot /* Align the ring tail to a cacheline boundary */
2217ba55f2f5SFrançois Tigeot int intel_ring_cacheline_align(struct intel_engine_cs *ring)
22189edbd4a0SFrançois Tigeot {
2219ba55f2f5SFrançois Tigeot 	int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
22209edbd4a0SFrançois Tigeot 	int ret;
22219edbd4a0SFrançois Tigeot 
22229edbd4a0SFrançois Tigeot 	if (num_dwords == 0)
22239edbd4a0SFrançois Tigeot 		return 0;
22249edbd4a0SFrançois Tigeot 
2225ba55f2f5SFrançois Tigeot 	num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
22269edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, num_dwords);
22279edbd4a0SFrançois Tigeot 	if (ret)
22289edbd4a0SFrançois Tigeot 		return ret;
22299edbd4a0SFrançois Tigeot 
22309edbd4a0SFrançois Tigeot 	while (num_dwords--)
22319edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
22329edbd4a0SFrançois Tigeot 
22339edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
22349edbd4a0SFrançois Tigeot 
22359edbd4a0SFrançois Tigeot 	return 0;
2236e3adcf8fSFrançois Tigeot }
2237e3adcf8fSFrançois Tigeot 
2238ba55f2f5SFrançois Tigeot void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
2239a2fdbec6SFrançois Tigeot {
224024edb884SFrançois Tigeot 	struct drm_device *dev = ring->dev;
224124edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2242a2fdbec6SFrançois Tigeot 
22432c9916cdSFrançois Tigeot 	BUG_ON(ring->outstanding_lazy_request);
2244a2fdbec6SFrançois Tigeot 
224524edb884SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
2246a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2247a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
224824edb884SFrançois Tigeot 		if (HAS_VEBOX(dev))
22499edbd4a0SFrançois Tigeot 			I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
2250e3adcf8fSFrançois Tigeot 	}
2251e3adcf8fSFrançois Tigeot 
2252a2fdbec6SFrançois Tigeot 	ring->set_seqno(ring, seqno);
22535d0b1887SFrançois Tigeot 	ring->hangcheck.seqno = seqno;
2254e3adcf8fSFrançois Tigeot }
2255e3adcf8fSFrançois Tigeot 
2256ba55f2f5SFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
2257f4e1c372SFrançois Tigeot 				     u32 value)
2258e3adcf8fSFrançois Tigeot {
2259ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2260e3adcf8fSFrançois Tigeot 
2261e3adcf8fSFrançois Tigeot        /* Every tail move must follow the sequence below */
2262f4e1c372SFrançois Tigeot 
2263f4e1c372SFrançois Tigeot 	/* Disable notification that the ring is IDLE. The GT
2264f4e1c372SFrançois Tigeot 	 * will then assume that it is busy and bring it out of rc6.
2265f4e1c372SFrançois Tigeot 	 */
2266e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2267f4e1c372SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2268e3adcf8fSFrançois Tigeot 
2269f4e1c372SFrançois Tigeot 	/* Clear the context id. Here be magic! */
2270f4e1c372SFrançois Tigeot 	I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2271e3adcf8fSFrançois Tigeot 
2272f4e1c372SFrançois Tigeot 	/* Wait for the ring not to be idle, i.e. for it to wake up. */
2273f4e1c372SFrançois Tigeot 	if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
2274f4e1c372SFrançois Tigeot 		      GEN6_BSD_SLEEP_INDICATOR) == 0,
2275f4e1c372SFrançois Tigeot 		     50))
2276f4e1c372SFrançois Tigeot 		DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
2277f4e1c372SFrançois Tigeot 
2278f4e1c372SFrançois Tigeot 	/* Now that the ring is fully powered up, update the tail */
2279e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
2280f4e1c372SFrançois Tigeot 	POSTING_READ(RING_TAIL(ring->mmio_base));
2281f4e1c372SFrançois Tigeot 
2282f4e1c372SFrançois Tigeot 	/* Let the ring send IDLE messages to the GT again,
2283f4e1c372SFrançois Tigeot 	 * and so let it sleep to conserve power when idle.
2284f4e1c372SFrançois Tigeot 	 */
2285e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2286f4e1c372SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2287e3adcf8fSFrançois Tigeot }
2288e3adcf8fSFrançois Tigeot 
2289ba55f2f5SFrançois Tigeot static int gen6_bsd_ring_flush(struct intel_engine_cs *ring,
2290b5c29a34SFrançois Tigeot 			       u32 invalidate, u32 flush)
2291e3adcf8fSFrançois Tigeot {
2292e3adcf8fSFrançois Tigeot 	uint32_t cmd;
2293e3adcf8fSFrançois Tigeot 	int ret;
2294e3adcf8fSFrançois Tigeot 
2295e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
2296e3adcf8fSFrançois Tigeot 	if (ret)
2297e3adcf8fSFrançois Tigeot 		return ret;
2298e3adcf8fSFrançois Tigeot 
2299e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
23009edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
23019edbd4a0SFrançois Tigeot 		cmd += 1;
23022c9916cdSFrançois Tigeot 
23032c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
23042c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
23052c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
23062c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
23072c9916cdSFrançois Tigeot 	 */
23082c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
23092c9916cdSFrançois Tigeot 
2310b5c29a34SFrançois Tigeot 	/*
2311b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.5 - video engine command streamer:
2312b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
2313b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
2314b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
2315b5c29a34SFrançois Tigeot 	 */
2316e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
23172c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
23182c9916cdSFrançois Tigeot 
2319e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
2320b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
23219edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
23229edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
23239edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
23249edbd4a0SFrançois Tigeot 	} else  {
23259edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0);
23269edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
23279edbd4a0SFrançois Tigeot 	}
23289edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
23299edbd4a0SFrançois Tigeot 	return 0;
23309edbd4a0SFrançois Tigeot }
23319edbd4a0SFrançois Tigeot 
23329edbd4a0SFrançois Tigeot static int
2333ba55f2f5SFrançois Tigeot gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
2334ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
2335477eb7f9SFrançois Tigeot 			      unsigned dispatch_flags)
23369edbd4a0SFrançois Tigeot {
2337477eb7f9SFrançois Tigeot 	bool ppgtt = USES_PPGTT(ring->dev) &&
2338477eb7f9SFrançois Tigeot 			!(dispatch_flags & I915_DISPATCH_SECURE);
23399edbd4a0SFrançois Tigeot 	int ret;
23409edbd4a0SFrançois Tigeot 
23419edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
23429edbd4a0SFrançois Tigeot 	if (ret)
23439edbd4a0SFrançois Tigeot 		return ret;
23449edbd4a0SFrançois Tigeot 
23459edbd4a0SFrançois Tigeot 	/* FIXME(BDW): Address space and security selectors. */
23469edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
2347ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, lower_32_bits(offset));
2348ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, upper_32_bits(offset));
2349e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
2350e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
23519edbd4a0SFrançois Tigeot 
2352e3adcf8fSFrançois Tigeot 	return 0;
2353e3adcf8fSFrançois Tigeot }
2354e3adcf8fSFrançois Tigeot 
2355e3adcf8fSFrançois Tigeot static int
2356ba55f2f5SFrançois Tigeot hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
2357ba55f2f5SFrançois Tigeot 			     u64 offset, u32 len,
2358477eb7f9SFrançois Tigeot 			     unsigned dispatch_flags)
2359e3adcf8fSFrançois Tigeot {
2360e3adcf8fSFrançois Tigeot 	int ret;
2361e3adcf8fSFrançois Tigeot 
2362e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
2363e3adcf8fSFrançois Tigeot 	if (ret)
2364e3adcf8fSFrançois Tigeot 		return ret;
2365e3adcf8fSFrançois Tigeot 
2366b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
23671b13d190SFrançois Tigeot 			MI_BATCH_BUFFER_START |
2368477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
23691b13d190SFrançois Tigeot 			 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW));
2370b5c29a34SFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
2371b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset);
2372b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
2373b5c29a34SFrançois Tigeot 
2374b5c29a34SFrançois Tigeot 	return 0;
2375b5c29a34SFrançois Tigeot }
2376b5c29a34SFrançois Tigeot 
2377b5c29a34SFrançois Tigeot static int
2378ba55f2f5SFrançois Tigeot gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
2379ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
2380477eb7f9SFrançois Tigeot 			      unsigned dispatch_flags)
2381b5c29a34SFrançois Tigeot {
2382b5c29a34SFrançois Tigeot 	int ret;
2383b5c29a34SFrançois Tigeot 
2384b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
2385b5c29a34SFrançois Tigeot 	if (ret)
2386b5c29a34SFrançois Tigeot 		return ret;
2387b5c29a34SFrançois Tigeot 
2388b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
2389b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START |
2390477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
2391477eb7f9SFrançois Tigeot 			 0 : MI_BATCH_NON_SECURE_I965));
2392e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
2393e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
2394e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
2395e3adcf8fSFrançois Tigeot 
2396e3adcf8fSFrançois Tigeot 	return 0;
2397e3adcf8fSFrançois Tigeot }
2398e3adcf8fSFrançois Tigeot 
2399e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
2400e3adcf8fSFrançois Tigeot 
2401ba55f2f5SFrançois Tigeot static int gen6_ring_flush(struct intel_engine_cs *ring,
2402b5c29a34SFrançois Tigeot 			   u32 invalidate, u32 flush)
2403e3adcf8fSFrançois Tigeot {
24045d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
2405e3adcf8fSFrançois Tigeot 	uint32_t cmd;
2406e3adcf8fSFrançois Tigeot 	int ret;
2407e3adcf8fSFrançois Tigeot 
2408e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
2409e3adcf8fSFrançois Tigeot 	if (ret)
2410e3adcf8fSFrançois Tigeot 		return ret;
2411e3adcf8fSFrançois Tigeot 
2412e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
2413477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8)
24149edbd4a0SFrançois Tigeot 		cmd += 1;
24152c9916cdSFrançois Tigeot 
24162c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
24172c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
24182c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
24192c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
24202c9916cdSFrançois Tigeot 	 */
24212c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
24222c9916cdSFrançois Tigeot 
2423b5c29a34SFrançois Tigeot 	/*
2424b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.3 - blitter engine command streamer:
2425b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
2426b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
2427b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
2428b5c29a34SFrançois Tigeot 	 */
2429e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
24302c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB;
2431e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
2432b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
2433477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
24349edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
24359edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
24369edbd4a0SFrançois Tigeot 	} else  {
2437e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, 0);
2438e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
24399edbd4a0SFrançois Tigeot 	}
2440e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
24415d0b1887SFrançois Tigeot 
2442e3adcf8fSFrançois Tigeot 	return 0;
2443e3adcf8fSFrançois Tigeot }
2444e3adcf8fSFrançois Tigeot 
2445e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
2446e3adcf8fSFrançois Tigeot {
2447ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2448ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
244924edb884SFrançois Tigeot 	struct drm_i915_gem_object *obj;
245024edb884SFrançois Tigeot 	int ret;
2451e3adcf8fSFrançois Tigeot 
2452686a02f1SFrançois Tigeot 	ring->name = "render ring";
2453686a02f1SFrançois Tigeot 	ring->id = RCS;
2454686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
2455686a02f1SFrançois Tigeot 
245624edb884SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
245724edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
245824edb884SFrançois Tigeot 			obj = i915_gem_alloc_object(dev, 4096);
245924edb884SFrançois Tigeot 			if (obj == NULL) {
246024edb884SFrançois Tigeot 				DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
246124edb884SFrançois Tigeot 				i915.semaphores = 0;
246224edb884SFrançois Tigeot 			} else {
246324edb884SFrançois Tigeot 				i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
246424edb884SFrançois Tigeot 				ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
246524edb884SFrançois Tigeot 				if (ret != 0) {
246624edb884SFrançois Tigeot 					drm_gem_object_unreference(&obj->base);
246724edb884SFrançois Tigeot 					DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
246824edb884SFrançois Tigeot 					i915.semaphores = 0;
246924edb884SFrançois Tigeot 				} else
247024edb884SFrançois Tigeot 					dev_priv->semaphore_obj = obj;
247124edb884SFrançois Tigeot 			}
247224edb884SFrançois Tigeot 		}
24732c9916cdSFrançois Tigeot 
24742c9916cdSFrançois Tigeot 		ring->init_context = intel_rcs_ctx_init;
247524edb884SFrançois Tigeot 		ring->add_request = gen6_add_request;
247624edb884SFrançois Tigeot 		ring->flush = gen8_render_ring_flush;
247724edb884SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
247824edb884SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
247924edb884SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
248024edb884SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
248124edb884SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
248224edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
248324edb884SFrançois Tigeot 			WARN_ON(!dev_priv->semaphore_obj);
248424edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
248524edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_rcs_signal;
248624edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
248724edb884SFrançois Tigeot 		}
248824edb884SFrançois Tigeot 	} else if (INTEL_INFO(dev)->gen >= 6) {
2489e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
2490b5c29a34SFrançois Tigeot 		ring->flush = gen7_render_ring_flush;
2491b5c29a34SFrançois Tigeot 		if (INTEL_INFO(dev)->gen == 6)
2492e3adcf8fSFrançois Tigeot 			ring->flush = gen6_render_ring_flush;
2493686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2494686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
24955d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2496e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2497a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
249824edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2499ba55f2f5SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2500ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
2501ba55f2f5SFrançois Tigeot 			/*
250224edb884SFrançois Tigeot 			 * The current semaphore is only applied on pre-gen8
250324edb884SFrançois Tigeot 			 * platform.  And there is no VCS2 ring on the pre-gen8
250424edb884SFrançois Tigeot 			 * platform. So the semaphore between RCS and VCS2 is
250524edb884SFrançois Tigeot 			 * initialized as INVALID.  Gen8 will initialize the
250624edb884SFrançois Tigeot 			 * sema between VCS2 and RCS later.
2507ba55f2f5SFrançois Tigeot 			 */
2508ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2509ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2510ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2511ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2512ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2513ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2514ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2515ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2516ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2517ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
251824edb884SFrançois Tigeot 		}
2519e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
2520e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
2521686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
2522e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
2523a2fdbec6SFrançois Tigeot 		ring->set_seqno = pc_render_set_seqno;
2524686a02f1SFrançois Tigeot 		ring->irq_get = gen5_ring_get_irq;
2525686a02f1SFrançois Tigeot 		ring->irq_put = gen5_ring_put_irq;
25265d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
25275d0b1887SFrançois Tigeot 					GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
2528686a02f1SFrançois Tigeot 	} else {
2529686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2530686a02f1SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 4)
2531686a02f1SFrançois Tigeot 			ring->flush = gen2_render_ring_flush;
2532686a02f1SFrançois Tigeot 		else
2533686a02f1SFrançois Tigeot 			ring->flush = gen4_render_ring_flush;
2534686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2535a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2536686a02f1SFrançois Tigeot 		if (IS_GEN2(dev)) {
2537686a02f1SFrançois Tigeot 			ring->irq_get = i8xx_ring_get_irq;
2538686a02f1SFrançois Tigeot 			ring->irq_put = i8xx_ring_put_irq;
2539686a02f1SFrançois Tigeot 		} else {
2540686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2541686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2542e3adcf8fSFrançois Tigeot 		}
2543686a02f1SFrançois Tigeot 		ring->irq_enable_mask = I915_USER_INTERRUPT;
2544686a02f1SFrançois Tigeot 	}
2545686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
254624edb884SFrançois Tigeot 
2547b5c29a34SFrançois Tigeot 	if (IS_HASWELL(dev))
2548b5c29a34SFrançois Tigeot 		ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
25499edbd4a0SFrançois Tigeot 	else if (IS_GEN8(dev))
25509edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2551b5c29a34SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 6)
2552686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2553686a02f1SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 4)
2554686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2555686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
2556686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2557686a02f1SFrançois Tigeot 	else
2558686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
25592c9916cdSFrançois Tigeot 	ring->init_hw = init_render_ring;
2560686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
2561e3adcf8fSFrançois Tigeot 
2562b5c29a34SFrançois Tigeot 	/* Workaround batchbuffer to combat CS tlb bug. */
2563b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev)) {
256424edb884SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
2565b5c29a34SFrançois Tigeot 		if (obj == NULL) {
2566b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to allocate batch bo\n");
2567b5c29a34SFrançois Tigeot 			return -ENOMEM;
2568b5c29a34SFrançois Tigeot 		}
2569b5c29a34SFrançois Tigeot 
2570ba55f2f5SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
2571b5c29a34SFrançois Tigeot 		if (ret != 0) {
2572b5c29a34SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
2573b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to ping batch bo\n");
2574b5c29a34SFrançois Tigeot 			return ret;
2575b5c29a34SFrançois Tigeot 		}
2576b5c29a34SFrançois Tigeot 
25779edbd4a0SFrançois Tigeot 		ring->scratch.obj = obj;
25789edbd4a0SFrançois Tigeot 		ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
2579e3adcf8fSFrançois Tigeot 	}
2580e3adcf8fSFrançois Tigeot 
25812c9916cdSFrançois Tigeot 	ret = intel_init_ring_buffer(dev, ring);
2582b5c29a34SFrançois Tigeot 	if (ret)
25832c9916cdSFrançois Tigeot 		return ret;
25842c9916cdSFrançois Tigeot 
25852c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
25862c9916cdSFrançois Tigeot 		ret = intel_init_pipe_control(ring);
25872c9916cdSFrançois Tigeot 		if (ret)
25882c9916cdSFrançois Tigeot 			return ret;
2589b5c29a34SFrançois Tigeot 	}
2590b5c29a34SFrançois Tigeot 
2591e3adcf8fSFrançois Tigeot 	return 0;
2592e3adcf8fSFrançois Tigeot }
2593e3adcf8fSFrançois Tigeot 
2594e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
2595e3adcf8fSFrançois Tigeot {
2596ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2597ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS];
2598e3adcf8fSFrançois Tigeot 
2599686a02f1SFrançois Tigeot 	ring->name = "bsd ring";
2600686a02f1SFrançois Tigeot 	ring->id = VCS;
2601686a02f1SFrançois Tigeot 
2602686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
26039edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
2604686a02f1SFrançois Tigeot 		ring->mmio_base = GEN6_BSD_RING_BASE;
2605686a02f1SFrançois Tigeot 		/* gen6 bsd needs a special wa for tail updates */
2606686a02f1SFrançois Tigeot 		if (IS_GEN6(dev))
2607686a02f1SFrançois Tigeot 			ring->write_tail = gen6_bsd_ring_write_tail;
26085d0b1887SFrançois Tigeot 		ring->flush = gen6_bsd_ring_flush;
2609686a02f1SFrançois Tigeot 		ring->add_request = gen6_add_request;
2610686a02f1SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2611a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
26129edbd4a0SFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 8) {
26139edbd4a0SFrançois Tigeot 			ring->irq_enable_mask =
26149edbd4a0SFrançois Tigeot 				GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
26159edbd4a0SFrançois Tigeot 			ring->irq_get = gen8_ring_get_irq;
26169edbd4a0SFrançois Tigeot 			ring->irq_put = gen8_ring_put_irq;
26179edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
26189edbd4a0SFrançois Tigeot 				gen8_ring_dispatch_execbuffer;
261924edb884SFrançois Tigeot 			if (i915_semaphore_is_enabled(dev)) {
262024edb884SFrançois Tigeot 				ring->semaphore.sync_to = gen8_ring_sync;
262124edb884SFrançois Tigeot 				ring->semaphore.signal = gen8_xcs_signal;
262224edb884SFrançois Tigeot 				GEN8_RING_SEMAPHORE_INIT;
262324edb884SFrançois Tigeot 			}
26249edbd4a0SFrançois Tigeot 		} else {
26255d0b1887SFrançois Tigeot 			ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2626686a02f1SFrançois Tigeot 			ring->irq_get = gen6_ring_get_irq;
2627686a02f1SFrançois Tigeot 			ring->irq_put = gen6_ring_put_irq;
26289edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
26299edbd4a0SFrançois Tigeot 				gen6_ring_dispatch_execbuffer;
263024edb884SFrançois Tigeot 			if (i915_semaphore_is_enabled(dev)) {
2631ba55f2f5SFrançois Tigeot 				ring->semaphore.sync_to = gen6_ring_sync;
2632ba55f2f5SFrançois Tigeot 				ring->semaphore.signal = gen6_signal;
2633ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2634ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2635ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2636ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2637ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2638ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2639ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2640ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2641ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2642ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
264324edb884SFrançois Tigeot 			}
264424edb884SFrançois Tigeot 		}
2645686a02f1SFrançois Tigeot 	} else {
2646686a02f1SFrançois Tigeot 		ring->mmio_base = BSD_RING_BASE;
2647686a02f1SFrançois Tigeot 		ring->flush = bsd_ring_flush;
2648686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2649686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2650a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2651686a02f1SFrançois Tigeot 		if (IS_GEN5(dev)) {
26525d0b1887SFrançois Tigeot 			ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2653686a02f1SFrançois Tigeot 			ring->irq_get = gen5_ring_get_irq;
2654686a02f1SFrançois Tigeot 			ring->irq_put = gen5_ring_put_irq;
2655686a02f1SFrançois Tigeot 		} else {
2656686a02f1SFrançois Tigeot 			ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2657686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2658686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2659686a02f1SFrançois Tigeot 		}
2660686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2661686a02f1SFrançois Tigeot 	}
26622c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2663e3adcf8fSFrançois Tigeot 
2664e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2665e3adcf8fSFrançois Tigeot }
2666e3adcf8fSFrançois Tigeot 
2667ba55f2f5SFrançois Tigeot /**
2668477eb7f9SFrançois Tigeot  * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
2669ba55f2f5SFrançois Tigeot  */
2670ba55f2f5SFrançois Tigeot int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2671ba55f2f5SFrançois Tigeot {
2672ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2673ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
2674ba55f2f5SFrançois Tigeot 
267524edb884SFrançois Tigeot 	ring->name = "bsd2 ring";
2676ba55f2f5SFrançois Tigeot 	ring->id = VCS2;
2677ba55f2f5SFrançois Tigeot 
2678ba55f2f5SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2679ba55f2f5SFrançois Tigeot 	ring->mmio_base = GEN8_BSD2_RING_BASE;
2680ba55f2f5SFrançois Tigeot 	ring->flush = gen6_bsd_ring_flush;
2681ba55f2f5SFrançois Tigeot 	ring->add_request = gen6_add_request;
2682ba55f2f5SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2683ba55f2f5SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
2684ba55f2f5SFrançois Tigeot 	ring->irq_enable_mask =
2685ba55f2f5SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2686ba55f2f5SFrançois Tigeot 	ring->irq_get = gen8_ring_get_irq;
2687ba55f2f5SFrançois Tigeot 	ring->irq_put = gen8_ring_put_irq;
2688ba55f2f5SFrançois Tigeot 	ring->dispatch_execbuffer =
2689ba55f2f5SFrançois Tigeot 			gen8_ring_dispatch_execbuffer;
269024edb884SFrançois Tigeot 	if (i915_semaphore_is_enabled(dev)) {
269124edb884SFrançois Tigeot 		ring->semaphore.sync_to = gen8_ring_sync;
269224edb884SFrançois Tigeot 		ring->semaphore.signal = gen8_xcs_signal;
269324edb884SFrançois Tigeot 		GEN8_RING_SEMAPHORE_INIT;
269424edb884SFrançois Tigeot 	}
26952c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2696ba55f2f5SFrançois Tigeot 
2697ba55f2f5SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2698ba55f2f5SFrançois Tigeot }
2699ba55f2f5SFrançois Tigeot 
2700e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
2701e3adcf8fSFrançois Tigeot {
2702ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2703ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[BCS];
2704e3adcf8fSFrançois Tigeot 
2705686a02f1SFrançois Tigeot 	ring->name = "blitter ring";
2706686a02f1SFrançois Tigeot 	ring->id = BCS;
2707686a02f1SFrançois Tigeot 
2708686a02f1SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
2709686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
27105d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
2711686a02f1SFrançois Tigeot 	ring->add_request = gen6_add_request;
2712686a02f1SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2713a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
27149edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
27159edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
27169edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
27179edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
27189edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
27199edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
272024edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
272124edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
272224edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_xcs_signal;
272324edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
272424edb884SFrançois Tigeot 		}
27259edbd4a0SFrançois Tigeot 	} else {
27265d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2727686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2728686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
2729686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
273024edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2731ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
273224edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2733ba55f2f5SFrançois Tigeot 			/*
273424edb884SFrançois Tigeot 			 * The current semaphore is only applied on pre-gen8
273524edb884SFrançois Tigeot 			 * platform.  And there is no VCS2 ring on the pre-gen8
273624edb884SFrançois Tigeot 			 * platform. So the semaphore between BCS and VCS2 is
273724edb884SFrançois Tigeot 			 * initialized as INVALID.  Gen8 will initialize the
273824edb884SFrançois Tigeot 			 * sema between BCS and VCS2 later.
2739ba55f2f5SFrançois Tigeot 			 */
2740ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2741ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2742ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2743ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2744ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2745ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2746ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2747ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2748ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2749ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
275024edb884SFrançois Tigeot 		}
275124edb884SFrançois Tigeot 	}
27522c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
27535d0b1887SFrançois Tigeot 
27545d0b1887SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
27555d0b1887SFrançois Tigeot }
27565d0b1887SFrançois Tigeot 
27575d0b1887SFrançois Tigeot int intel_init_vebox_ring_buffer(struct drm_device *dev)
27585d0b1887SFrançois Tigeot {
2759ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2760ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VECS];
27615d0b1887SFrançois Tigeot 
27625d0b1887SFrançois Tigeot 	ring->name = "video enhancement ring";
27635d0b1887SFrançois Tigeot 	ring->id = VECS;
27645d0b1887SFrançois Tigeot 
27655d0b1887SFrançois Tigeot 	ring->mmio_base = VEBOX_RING_BASE;
27665d0b1887SFrançois Tigeot 	ring->write_tail = ring_write_tail;
27675d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
27685d0b1887SFrançois Tigeot 	ring->add_request = gen6_add_request;
27695d0b1887SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
27705d0b1887SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
27719edbd4a0SFrançois Tigeot 
27729edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
27739edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
27749edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
27759edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
27769edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
27779edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
277824edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
277924edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
278024edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_xcs_signal;
278124edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
278224edb884SFrançois Tigeot 		}
27839edbd4a0SFrançois Tigeot 	} else {
27849edbd4a0SFrançois Tigeot 		ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
27855d0b1887SFrançois Tigeot 		ring->irq_get = hsw_vebox_get_irq;
27865d0b1887SFrançois Tigeot 		ring->irq_put = hsw_vebox_put_irq;
27875d0b1887SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
278824edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2789ba55f2f5SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2790ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
2791ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2792ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2793ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2794ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2795ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2796ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2797ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2798ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2799ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2800ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
280124edb884SFrançois Tigeot 		}
280224edb884SFrançois Tigeot 	}
28032c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2804e3adcf8fSFrançois Tigeot 
2805e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2806e3adcf8fSFrançois Tigeot }
2807b030f26bSFrançois Tigeot 
2808b030f26bSFrançois Tigeot int
2809ba55f2f5SFrançois Tigeot intel_ring_flush_all_caches(struct intel_engine_cs *ring)
2810b030f26bSFrançois Tigeot {
2811b030f26bSFrançois Tigeot 	int ret;
2812b030f26bSFrançois Tigeot 
2813b030f26bSFrançois Tigeot 	if (!ring->gpu_caches_dirty)
2814b030f26bSFrançois Tigeot 		return 0;
2815b030f26bSFrançois Tigeot 
2816b030f26bSFrançois Tigeot 	ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2817b030f26bSFrançois Tigeot 	if (ret)
2818b030f26bSFrançois Tigeot 		return ret;
2819b030f26bSFrançois Tigeot 
2820a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2821a2fdbec6SFrançois Tigeot 
2822b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2823b030f26bSFrançois Tigeot 	return 0;
2824b030f26bSFrançois Tigeot }
2825b030f26bSFrançois Tigeot 
2826b030f26bSFrançois Tigeot int
2827ba55f2f5SFrançois Tigeot intel_ring_invalidate_all_caches(struct intel_engine_cs *ring)
2828b030f26bSFrançois Tigeot {
2829b030f26bSFrançois Tigeot 	uint32_t flush_domains;
2830b030f26bSFrançois Tigeot 	int ret;
2831b030f26bSFrançois Tigeot 
2832b030f26bSFrançois Tigeot 	flush_domains = 0;
2833b030f26bSFrançois Tigeot 	if (ring->gpu_caches_dirty)
2834b030f26bSFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
2835b030f26bSFrançois Tigeot 
2836b030f26bSFrançois Tigeot 	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2837b030f26bSFrançois Tigeot 	if (ret)
2838b030f26bSFrançois Tigeot 		return ret;
2839b030f26bSFrançois Tigeot 
2840a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2841a2fdbec6SFrançois Tigeot 
2842b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2843b030f26bSFrançois Tigeot 	return 0;
2844b030f26bSFrançois Tigeot }
2845ba55f2f5SFrançois Tigeot 
2846ba55f2f5SFrançois Tigeot void
2847ba55f2f5SFrançois Tigeot intel_stop_ring_buffer(struct intel_engine_cs *ring)
2848ba55f2f5SFrançois Tigeot {
2849ba55f2f5SFrançois Tigeot 	int ret;
2850ba55f2f5SFrançois Tigeot 
2851ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
2852ba55f2f5SFrançois Tigeot 		return;
2853ba55f2f5SFrançois Tigeot 
2854ba55f2f5SFrançois Tigeot 	ret = intel_ring_idle(ring);
2855ba55f2f5SFrançois Tigeot 	if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2856ba55f2f5SFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2857ba55f2f5SFrançois Tigeot 			  ring->name, ret);
2858ba55f2f5SFrançois Tigeot 
2859ba55f2f5SFrançois Tigeot 	stop_ring(ring);
2860ba55f2f5SFrançois Tigeot }
2861