xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision 2c9916cd50d5c4c4defa089bebed8c8865efa896)
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 {
55*2c9916cdSFrançois Tigeot 	int space = head - tail;
56*2c9916cdSFrançois Tigeot 	if (space <= 0)
57ba55f2f5SFrançois Tigeot 		space += size;
58*2c9916cdSFrançois Tigeot 	return space - I915_RING_FREE_SPACE;
59*2c9916cdSFrançois Tigeot }
60*2c9916cdSFrançois Tigeot 
61*2c9916cdSFrançois Tigeot void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
62*2c9916cdSFrançois Tigeot {
63*2c9916cdSFrançois Tigeot 	if (ringbuf->last_retired_head != -1) {
64*2c9916cdSFrançois Tigeot 		ringbuf->head = ringbuf->last_retired_head;
65*2c9916cdSFrançois Tigeot 		ringbuf->last_retired_head = -1;
66*2c9916cdSFrançois Tigeot 	}
67*2c9916cdSFrançois Tigeot 
68*2c9916cdSFrançois Tigeot 	ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
69*2c9916cdSFranç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 {
74*2c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
75*2c9916cdSFranç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 
320ba55f2f5SFrançois Tigeot static int gen7_ring_fbc_flush(struct intel_engine_cs *ring, u32 value)
3215d0b1887SFrançois Tigeot {
3225d0b1887SFrançois Tigeot 	int ret;
3235d0b1887SFrançois Tigeot 
3245d0b1887SFrançois Tigeot 	if (!ring->fbc_dirty)
3255d0b1887SFrançois Tigeot 		return 0;
3265d0b1887SFrançois Tigeot 
3279edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
3285d0b1887SFrançois Tigeot 	if (ret)
3295d0b1887SFrançois Tigeot 		return ret;
3305d0b1887SFrançois Tigeot 	/* WaFbcNukeOn3DBlt:ivb/hsw */
3315d0b1887SFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
3325d0b1887SFrançois Tigeot 	intel_ring_emit(ring, MSG_FBC_REND_STATE);
3335d0b1887SFrançois Tigeot 	intel_ring_emit(ring, value);
3349edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) | MI_SRM_LRM_GLOBAL_GTT);
3359edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MSG_FBC_REND_STATE);
3369edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
3375d0b1887SFrançois Tigeot 	intel_ring_advance(ring);
3385d0b1887SFrançois Tigeot 
3395d0b1887SFrançois Tigeot 	ring->fbc_dirty = false;
3405d0b1887SFrançois Tigeot 	return 0;
3415d0b1887SFrançois Tigeot }
3425d0b1887SFrançois Tigeot 
343b5c29a34SFrançois Tigeot static int
344ba55f2f5SFrançois Tigeot gen7_render_ring_flush(struct intel_engine_cs *ring,
345b5c29a34SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
346b5c29a34SFrançois Tigeot {
347b5c29a34SFrançois Tigeot 	u32 flags = 0;
348ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
349b5c29a34SFrançois Tigeot 	int ret;
350b5c29a34SFrançois Tigeot 
351b5c29a34SFrançois Tigeot 	/*
352b5c29a34SFrançois Tigeot 	 * Ensure that any following seqno writes only happen when the render
353b5c29a34SFrançois Tigeot 	 * cache is indeed flushed.
354b5c29a34SFrançois Tigeot 	 *
355b5c29a34SFrançois Tigeot 	 * Workaround: 4th PIPE_CONTROL command (except the ones with only
356b5c29a34SFrançois Tigeot 	 * read-cache invalidate bits set) must have the CS_STALL bit set. We
357b5c29a34SFrançois Tigeot 	 * don't try to be clever and just set it unconditionally.
358b5c29a34SFrançois Tigeot 	 */
359b5c29a34SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
360b5c29a34SFrançois Tigeot 
361b5c29a34SFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
362b5c29a34SFrançois Tigeot 	 * number of bits based on the write domains has little performance
363b5c29a34SFrançois Tigeot 	 * impact.
364b5c29a34SFrançois Tigeot 	 */
365b5c29a34SFrançois Tigeot 	if (flush_domains) {
366b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
367b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
368b5c29a34SFrançois Tigeot 	}
369b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
370b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
371b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
372b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
373b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
374b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
375b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
376*2c9916cdSFrançois Tigeot 		flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
377b5c29a34SFrançois Tigeot 		/*
378b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
379b5c29a34SFrançois Tigeot 		 */
380b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
381a2fdbec6SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
382b5c29a34SFrançois Tigeot 
3830dbf0ea8SMatthew Dillon 		flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3840dbf0ea8SMatthew Dillon 
385b5c29a34SFrançois Tigeot 		/* Workaround: we must issue a pipe_control with CS-stall bit
386b5c29a34SFrançois Tigeot 		 * set before a pipe_control command that has the state cache
387b5c29a34SFrançois Tigeot 		 * invalidate bit set. */
388b5c29a34SFrançois Tigeot 		gen7_render_ring_cs_stall_wa(ring);
389b5c29a34SFrançois Tigeot 	}
390b5c29a34SFrançois Tigeot 
391b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
392b5c29a34SFrançois Tigeot 	if (ret)
393b5c29a34SFrançois Tigeot 		return ret;
394b5c29a34SFrançois Tigeot 
395b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
396b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, flags);
397a2fdbec6SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
398b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
399e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
400e3adcf8fSFrançois Tigeot 
4019edbd4a0SFrançois Tigeot 	if (!invalidate_domains && flush_domains)
4025d0b1887SFrançois Tigeot 		return gen7_ring_fbc_flush(ring, FBC_REND_NUKE);
4035d0b1887SFrançois Tigeot 
404e3adcf8fSFrançois Tigeot 	return 0;
405e3adcf8fSFrançois Tigeot }
406e3adcf8fSFrançois Tigeot 
4079edbd4a0SFrançois Tigeot static int
40824edb884SFrançois Tigeot gen8_emit_pipe_control(struct intel_engine_cs *ring,
40924edb884SFrançois Tigeot 		       u32 flags, u32 scratch_addr)
41024edb884SFrançois Tigeot {
41124edb884SFrançois Tigeot 	int ret;
41224edb884SFrançois Tigeot 
41324edb884SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
41424edb884SFrançois Tigeot 	if (ret)
41524edb884SFrançois Tigeot 		return ret;
41624edb884SFrançois Tigeot 
41724edb884SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
41824edb884SFrançois Tigeot 	intel_ring_emit(ring, flags);
41924edb884SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
42024edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
42124edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
42224edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
42324edb884SFrançois Tigeot 	intel_ring_advance(ring);
42424edb884SFrançois Tigeot 
42524edb884SFrançois Tigeot 	return 0;
42624edb884SFrançois Tigeot }
42724edb884SFrançois Tigeot 
42824edb884SFrançois Tigeot static int
429ba55f2f5SFrançois Tigeot gen8_render_ring_flush(struct intel_engine_cs *ring,
4309edbd4a0SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
4319edbd4a0SFrançois Tigeot {
4329edbd4a0SFrançois Tigeot 	u32 flags = 0;
433ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
4349edbd4a0SFrançois Tigeot 	int ret;
4359edbd4a0SFrançois Tigeot 
4369edbd4a0SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
4379edbd4a0SFrançois Tigeot 
4389edbd4a0SFrançois Tigeot 	if (flush_domains) {
4399edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
4409edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
4419edbd4a0SFrançois Tigeot 	}
4429edbd4a0SFrançois Tigeot 	if (invalidate_domains) {
4439edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
4449edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
4459edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
4469edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
4479edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
4489edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
4499edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
4509edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4519edbd4a0SFrançois Tigeot 
45224edb884SFrançois Tigeot 		/* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
45324edb884SFrançois Tigeot 		ret = gen8_emit_pipe_control(ring,
45424edb884SFrançois Tigeot 					     PIPE_CONTROL_CS_STALL |
45524edb884SFrançois Tigeot 					     PIPE_CONTROL_STALL_AT_SCOREBOARD,
45624edb884SFrançois Tigeot 					     0);
4579edbd4a0SFrançois Tigeot 		if (ret)
4589edbd4a0SFrançois Tigeot 			return ret;
45924edb884SFrançois Tigeot 	}
4609edbd4a0SFrançois Tigeot 
4611b13d190SFrançois Tigeot 	ret = gen8_emit_pipe_control(ring, flags, scratch_addr);
4621b13d190SFrançois Tigeot 	if (ret)
4631b13d190SFrançois Tigeot 		return ret;
4641b13d190SFrançois Tigeot 
4651b13d190SFrançois Tigeot 	if (!invalidate_domains && flush_domains)
4661b13d190SFrançois Tigeot 		return gen7_ring_fbc_flush(ring, FBC_REND_NUKE);
4671b13d190SFrançois Tigeot 
4681b13d190SFrançois Tigeot 	return 0;
4699edbd4a0SFrançois Tigeot }
4709edbd4a0SFrançois Tigeot 
471ba55f2f5SFrançois Tigeot static void ring_write_tail(struct intel_engine_cs *ring,
472b5c29a34SFrançois Tigeot 			    u32 value)
473e3adcf8fSFrançois Tigeot {
474ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
475e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
476e3adcf8fSFrançois Tigeot }
477e3adcf8fSFrançois Tigeot 
478ba55f2f5SFrançois Tigeot u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
479e3adcf8fSFrançois Tigeot {
480ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
481ba55f2f5SFrançois Tigeot 	u64 acthd;
482e3adcf8fSFrançois Tigeot 
483ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
484ba55f2f5SFrançois Tigeot 		acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
485ba55f2f5SFrançois Tigeot 					 RING_ACTHD_UDW(ring->mmio_base));
486ba55f2f5SFrançois Tigeot 	else if (INTEL_INFO(ring->dev)->gen >= 4)
487ba55f2f5SFrançois Tigeot 		acthd = I915_READ(RING_ACTHD(ring->mmio_base));
488ba55f2f5SFrançois Tigeot 	else
489ba55f2f5SFrançois Tigeot 		acthd = I915_READ(ACTHD);
490ba55f2f5SFrançois Tigeot 
491ba55f2f5SFrançois Tigeot 	return acthd;
492e3adcf8fSFrançois Tigeot }
493e3adcf8fSFrançois Tigeot 
494ba55f2f5SFrançois Tigeot static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
4955d0b1887SFrançois Tigeot {
4965d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
4975d0b1887SFrançois Tigeot 	u32 addr;
4985d0b1887SFrançois Tigeot 
4995d0b1887SFrançois Tigeot 	addr = dev_priv->status_page_dmah->busaddr;
5005d0b1887SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 4)
5015d0b1887SFrançois Tigeot 		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
5025d0b1887SFrançois Tigeot 	I915_WRITE(HWS_PGA, addr);
5035d0b1887SFrançois Tigeot }
5045d0b1887SFrançois Tigeot 
505ba55f2f5SFrançois Tigeot static bool stop_ring(struct intel_engine_cs *ring)
506e3adcf8fSFrançois Tigeot {
507ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ring->dev);
508e3adcf8fSFrançois Tigeot 
509ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
510ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
5111b13d190SFrançois Tigeot 		if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
512ba55f2f5SFrançois Tigeot 			DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
5131b13d190SFrançois Tigeot 			/* Sometimes we observe that the idle flag is not
5141b13d190SFrançois Tigeot 			 * set even though the ring is empty. So double
5151b13d190SFrançois Tigeot 			 * check before giving up.
5161b13d190SFrançois Tigeot 			 */
5171b13d190SFrançois Tigeot 			if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
518ba55f2f5SFrançois Tigeot 				return false;
519ba55f2f5SFrançois Tigeot 		}
520ba55f2f5SFrançois Tigeot 	}
521686a02f1SFrançois Tigeot 
522e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
523e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
524e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
525e3adcf8fSFrançois Tigeot 
526ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
527ba55f2f5SFrançois Tigeot 		(void)I915_READ_CTL(ring);
528ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
529ba55f2f5SFrançois Tigeot 	}
530e3adcf8fSFrançois Tigeot 
531ba55f2f5SFrançois Tigeot 	return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
532ba55f2f5SFrançois Tigeot }
533ba55f2f5SFrançois Tigeot 
534ba55f2f5SFrançois Tigeot static int init_ring_common(struct intel_engine_cs *ring)
535ba55f2f5SFrançois Tigeot {
536ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ring->dev;
537ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
538ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
539ba55f2f5SFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
540ba55f2f5SFrançois Tigeot 	int ret = 0;
541ba55f2f5SFrançois Tigeot 
542*2c9916cdSFrançois Tigeot 	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
543ba55f2f5SFrançois Tigeot 
544ba55f2f5SFrançois Tigeot 	if (!stop_ring(ring)) {
545ba55f2f5SFrançois Tigeot 		/* G45 ring initialization often fails to reset head to zero */
546b5c29a34SFrançois Tigeot 		DRM_DEBUG_KMS("%s head not reset to zero "
547e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
548e3adcf8fSFrançois Tigeot 			      ring->name,
549e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
550e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
551e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
552e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
553e3adcf8fSFrançois Tigeot 
554ba55f2f5SFrançois Tigeot 		if (!stop_ring(ring)) {
555e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
556e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
557e3adcf8fSFrançois Tigeot 				  ring->name,
558e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
559e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
560e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
561e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
562686a02f1SFrançois Tigeot 			ret = -EIO;
563686a02f1SFrançois Tigeot 			goto out;
564e3adcf8fSFrançois Tigeot 		}
565ba55f2f5SFrançois Tigeot 	}
566ba55f2f5SFrançois Tigeot 
567ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev))
568ba55f2f5SFrançois Tigeot 		intel_ring_setup_status_page(ring);
569ba55f2f5SFrançois Tigeot 	else
570ba55f2f5SFrançois Tigeot 		ring_setup_phys_status_page(ring);
571ba55f2f5SFrançois Tigeot 
5720f370975SMatthew Dillon 	/* Enforce ordering by reading HEAD register back */
5730f370975SMatthew Dillon 	I915_READ_HEAD(ring);
5740f370975SMatthew Dillon 
575ba55f2f5SFrançois Tigeot 	/* Initialize the ring. This must happen _after_ we've cleared the ring
576ba55f2f5SFrançois Tigeot 	 * registers with the above sequence (the readback of the HEAD registers
577ba55f2f5SFrançois Tigeot 	 * also enforces ordering), otherwise the hw might lose the new ring
578ba55f2f5SFrançois Tigeot 	 * register values. */
579ba55f2f5SFrançois Tigeot 	I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
5801b13d190SFrançois Tigeot 
5811b13d190SFrançois Tigeot 	/* WaClearRingBufHeadRegAtInit:ctg,elk */
5821b13d190SFrançois Tigeot 	if (I915_READ_HEAD(ring))
5831b13d190SFrançois Tigeot 		DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
5841b13d190SFrançois Tigeot 			  ring->name, I915_READ_HEAD(ring));
5851b13d190SFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
5861b13d190SFrançois Tigeot 	(void)I915_READ_HEAD(ring);
5871b13d190SFrançois Tigeot 
588ba55f2f5SFrançois Tigeot 	I915_WRITE_CTL(ring,
589ba55f2f5SFrançois Tigeot 			((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
590ba55f2f5SFrançois Tigeot 			| RING_VALID);
591ba55f2f5SFrançois Tigeot 
592ba55f2f5SFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
593ba55f2f5SFrançois Tigeot 	if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
594ba55f2f5SFrançois Tigeot 		     I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
595ba55f2f5SFrançois Tigeot 		     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
596ba55f2f5SFrançois Tigeot 		DRM_ERROR("%s initialization failed "
597ba55f2f5SFrançois Tigeot 			  "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
598ba55f2f5SFrançois Tigeot 			  ring->name,
599ba55f2f5SFrançois Tigeot 			  I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
600ba55f2f5SFrançois Tigeot 			  I915_READ_HEAD(ring), I915_READ_TAIL(ring),
601ba55f2f5SFrançois Tigeot 			  I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
602ba55f2f5SFrançois Tigeot 		ret = -EIO;
603ba55f2f5SFrançois Tigeot 		goto out;
604ba55f2f5SFrançois Tigeot 	}
605e3adcf8fSFrançois Tigeot 
606*2c9916cdSFrançois Tigeot 	ringbuf->last_retired_head = -1;
607ba55f2f5SFrançois Tigeot 	ringbuf->head = I915_READ_HEAD(ring);
608ba55f2f5SFrançois Tigeot 	ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
609*2c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
610e3adcf8fSFrançois Tigeot 
6115d0b1887SFrançois Tigeot 	memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
6125d0b1887SFrançois Tigeot 
613686a02f1SFrançois Tigeot out:
614*2c9916cdSFrançois Tigeot 	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
615686a02f1SFrançois Tigeot 
616686a02f1SFrançois Tigeot 	return ret;
617e3adcf8fSFrançois Tigeot }
618e3adcf8fSFrançois Tigeot 
6191b13d190SFrançois Tigeot void
6201b13d190SFrançois Tigeot intel_fini_pipe_control(struct intel_engine_cs *ring)
6211b13d190SFrançois Tigeot {
6221b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
6231b13d190SFrançois Tigeot 
6241b13d190SFrançois Tigeot 	if (ring->scratch.obj == NULL)
6251b13d190SFrançois Tigeot 		return;
6261b13d190SFrançois Tigeot 
6271b13d190SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
6281b13d190SFrançois Tigeot 		kunmap(ring->scratch.obj->pages[0]);
6291b13d190SFrançois Tigeot 		i915_gem_object_ggtt_unpin(ring->scratch.obj);
6301b13d190SFrançois Tigeot 	}
6311b13d190SFrançois Tigeot 
6321b13d190SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
6331b13d190SFrançois Tigeot 	ring->scratch.obj = NULL;
6341b13d190SFrançois Tigeot }
6351b13d190SFrançois Tigeot 
6361b13d190SFrançois Tigeot int
6371b13d190SFrançois Tigeot intel_init_pipe_control(struct intel_engine_cs *ring)
638e3adcf8fSFrançois Tigeot {
639e3adcf8fSFrançois Tigeot 	int ret;
640e3adcf8fSFrançois Tigeot 
641*2c9916cdSFrançois Tigeot 	WARN_ON(ring->scratch.obj);
642e3adcf8fSFrançois Tigeot 
6439edbd4a0SFrançois Tigeot 	ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
6449edbd4a0SFrançois Tigeot 	if (ring->scratch.obj == NULL) {
645e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
646e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
647e3adcf8fSFrançois Tigeot 		goto err;
648e3adcf8fSFrançois Tigeot 	}
649e3adcf8fSFrançois Tigeot 
650ba55f2f5SFrançois Tigeot 	ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
651ba55f2f5SFrançois Tigeot 	if (ret)
652ba55f2f5SFrançois Tigeot 		goto err_unref;
653e3adcf8fSFrançois Tigeot 
654ba55f2f5SFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
655e3adcf8fSFrançois Tigeot 	if (ret)
656e3adcf8fSFrançois Tigeot 		goto err_unref;
657e3adcf8fSFrançois Tigeot 
6589edbd4a0SFrançois Tigeot 	ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
6599edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page = kmap(ring->scratch.obj->pages[0]);
6609edbd4a0SFrançois Tigeot 	if (ring->scratch.cpu_page == NULL) {
6615d0b1887SFrançois Tigeot 		ret = -ENOMEM;
662e3adcf8fSFrançois Tigeot 		goto err_unpin;
6635d0b1887SFrançois Tigeot 	}
664a2fdbec6SFrançois Tigeot 
665a2fdbec6SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
6669edbd4a0SFrançois Tigeot 			 ring->name, ring->scratch.gtt_offset);
667e3adcf8fSFrançois Tigeot 	return 0;
668e3adcf8fSFrançois Tigeot 
669e3adcf8fSFrançois Tigeot err_unpin:
670ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(ring->scratch.obj);
671e3adcf8fSFrançois Tigeot err_unref:
6729edbd4a0SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
673e3adcf8fSFrançois Tigeot err:
674e3adcf8fSFrançois Tigeot 	return ret;
675e3adcf8fSFrançois Tigeot }
676e3adcf8fSFrançois Tigeot 
677*2c9916cdSFrançois Tigeot static int intel_ring_workarounds_emit(struct intel_engine_cs *ring,
678*2c9916cdSFrançois Tigeot 				       struct intel_context *ctx)
6791b13d190SFrançois Tigeot {
680*2c9916cdSFrançois Tigeot 	int ret, i;
6811b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
6821b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
683*2c9916cdSFrançois Tigeot 	struct i915_workarounds *w = &dev_priv->workarounds;
6841b13d190SFrançois Tigeot 
685*2c9916cdSFrançois Tigeot 	if (WARN_ON_ONCE(w->count == 0))
686*2c9916cdSFrançois Tigeot 		return 0;
6871b13d190SFrançois Tigeot 
688*2c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
689*2c9916cdSFrançois Tigeot 	ret = intel_ring_flush_all_caches(ring);
6901b13d190SFrançois Tigeot 	if (ret)
6911b13d190SFrançois Tigeot 		return ret;
6921b13d190SFrançois Tigeot 
693*2c9916cdSFrançois Tigeot 	ret = intel_ring_begin(ring, (w->count * 2 + 2));
694*2c9916cdSFrançois Tigeot 	if (ret)
695*2c9916cdSFrançois Tigeot 		return ret;
696*2c9916cdSFrançois Tigeot 
697*2c9916cdSFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
698*2c9916cdSFrançois Tigeot 	for (i = 0; i < w->count; i++) {
699*2c9916cdSFrançois Tigeot 		intel_ring_emit(ring, w->reg[i].addr);
700*2c9916cdSFrançois Tigeot 		intel_ring_emit(ring, w->reg[i].value);
701*2c9916cdSFrançois Tigeot 	}
702*2c9916cdSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
703*2c9916cdSFrançois Tigeot 
704*2c9916cdSFrançois Tigeot 	intel_ring_advance(ring);
705*2c9916cdSFrançois Tigeot 
706*2c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
707*2c9916cdSFrançois Tigeot 	ret = intel_ring_flush_all_caches(ring);
708*2c9916cdSFrançois Tigeot 	if (ret)
709*2c9916cdSFrançois Tigeot 		return ret;
710*2c9916cdSFrançois Tigeot 
711*2c9916cdSFrançois Tigeot 	DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
712*2c9916cdSFrançois Tigeot 
713*2c9916cdSFrançois Tigeot 	return 0;
714*2c9916cdSFrançois Tigeot }
715*2c9916cdSFrançois Tigeot 
716*2c9916cdSFrançois Tigeot static int intel_rcs_ctx_init(struct intel_engine_cs *ring,
717*2c9916cdSFrançois Tigeot 			      struct intel_context *ctx)
718*2c9916cdSFrançois Tigeot {
719*2c9916cdSFrançois Tigeot 	int ret;
720*2c9916cdSFrançois Tigeot 
721*2c9916cdSFrançois Tigeot 	ret = intel_ring_workarounds_emit(ring, ctx);
722*2c9916cdSFrançois Tigeot 	if (ret != 0)
723*2c9916cdSFrançois Tigeot 		return ret;
724*2c9916cdSFrançois Tigeot 
725*2c9916cdSFrançois Tigeot 	ret = i915_gem_render_state_init(ring);
726*2c9916cdSFrançois Tigeot 	if (ret)
727*2c9916cdSFrançois Tigeot 		DRM_ERROR("init render state: %d\n", ret);
728*2c9916cdSFrançois Tigeot 
729*2c9916cdSFrançois Tigeot 	return ret;
730*2c9916cdSFrançois Tigeot }
731*2c9916cdSFrançois Tigeot 
732*2c9916cdSFrançois Tigeot static int wa_add(struct drm_i915_private *dev_priv,
733*2c9916cdSFrançois Tigeot 		  const u32 addr, const u32 mask, const u32 val)
734*2c9916cdSFrançois Tigeot {
735*2c9916cdSFrançois Tigeot 	const u32 idx = dev_priv->workarounds.count;
736*2c9916cdSFrançois Tigeot 
737*2c9916cdSFrançois Tigeot 	if (WARN_ON(idx >= I915_MAX_WA_REGS))
738*2c9916cdSFrançois Tigeot 		return -ENOSPC;
739*2c9916cdSFrançois Tigeot 
740*2c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].addr = addr;
741*2c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].value = val;
742*2c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].mask = mask;
743*2c9916cdSFrançois Tigeot 
744*2c9916cdSFrançois Tigeot 	dev_priv->workarounds.count++;
745*2c9916cdSFrançois Tigeot 
746*2c9916cdSFrançois Tigeot 	return 0;
747*2c9916cdSFrançois Tigeot }
748*2c9916cdSFrançois Tigeot 
749*2c9916cdSFrançois Tigeot #define WA_REG(addr, mask, val) { \
750*2c9916cdSFrançois Tigeot 		const int r = wa_add(dev_priv, (addr), (mask), (val)); \
751*2c9916cdSFrançois Tigeot 		if (r) \
752*2c9916cdSFrançois Tigeot 			return r; \
753*2c9916cdSFrançois Tigeot 	}
754*2c9916cdSFrançois Tigeot 
755*2c9916cdSFrançois Tigeot #define WA_SET_BIT_MASKED(addr, mask) \
756*2c9916cdSFrançois Tigeot 	WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
757*2c9916cdSFrançois Tigeot 
758*2c9916cdSFrançois Tigeot #define WA_CLR_BIT_MASKED(addr, mask) \
759*2c9916cdSFrançois Tigeot 	WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
760*2c9916cdSFrançois Tigeot 
761*2c9916cdSFrançois Tigeot #define WA_SET_FIELD_MASKED(addr, mask, value) \
762*2c9916cdSFrançois Tigeot 	WA_REG(addr, mask, _MASKED_FIELD(mask, value))
763*2c9916cdSFrançois Tigeot 
764*2c9916cdSFrançois Tigeot #define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
765*2c9916cdSFrançois Tigeot #define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
766*2c9916cdSFrançois Tigeot 
767*2c9916cdSFrançois Tigeot #define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
768*2c9916cdSFrançois Tigeot 
769*2c9916cdSFrançois Tigeot static int bdw_init_workarounds(struct intel_engine_cs *ring)
770*2c9916cdSFrançois Tigeot {
771*2c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
772*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
773*2c9916cdSFrançois Tigeot 
7741b13d190SFrançois Tigeot 	/* WaDisablePartialInstShootdown:bdw */
775*2c9916cdSFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:bdw (pre-production) */
776*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
777*2c9916cdSFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
778*2c9916cdSFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
7791b13d190SFrançois Tigeot 
780*2c9916cdSFrançois Tigeot 	/* WaDisableDopClockGating:bdw */
781*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
782*2c9916cdSFrançois Tigeot 			  DOP_CLOCK_GATING_DISABLE);
7831b13d190SFrançois Tigeot 
784*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
785*2c9916cdSFrançois Tigeot 			  GEN8_SAMPLER_POWER_BYPASS_DIS);
7861b13d190SFrançois Tigeot 
7871b13d190SFrançois Tigeot 	/* Use Force Non-Coherent whenever executing a 3D context. This is a
7881b13d190SFrançois Tigeot 	 * workaround for for a possible hang in the unlikely event a TLB
7891b13d190SFrançois Tigeot 	 * invalidation occurs during a PSD flush.
7901b13d190SFrançois Tigeot 	 */
791*2c9916cdSFrançois Tigeot 	/* WaForceEnableNonCoherent:bdw */
792*2c9916cdSFrançois Tigeot 	/* WaHdcDisableFetchWhenMasked:bdw */
793*2c9916cdSFrançois Tigeot 	/* WaDisableFenceDestinationToSLM:bdw (GT3 pre-production) */
794*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0,
795*2c9916cdSFrançois Tigeot 			  HDC_FORCE_NON_COHERENT |
796*2c9916cdSFrançois Tigeot 			  HDC_DONOT_FETCH_MEM_WHEN_MASKED |
797*2c9916cdSFrançois Tigeot 			  (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
798*2c9916cdSFrançois Tigeot 
799*2c9916cdSFrançois Tigeot 	/* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
800*2c9916cdSFrançois Tigeot 	 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
801*2c9916cdSFrançois Tigeot 	 *  polygons in the same 8x4 pixel/sample area to be processed without
802*2c9916cdSFrançois Tigeot 	 *  stalling waiting for the earlier ones to write to Hierarchical Z
803*2c9916cdSFrançois Tigeot 	 *  buffer."
804*2c9916cdSFrançois Tigeot 	 *
805*2c9916cdSFrançois Tigeot 	 * This optimization is off by default for Broadwell; turn it on.
806*2c9916cdSFrançois Tigeot 	 */
807*2c9916cdSFrançois Tigeot 	WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
8081b13d190SFrançois Tigeot 
8091b13d190SFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:bdw */
810*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1,
811*2c9916cdSFrançois Tigeot 			  GEN8_4x4_STC_OPTIMIZATION_DISABLE);
8121b13d190SFrançois Tigeot 
8131b13d190SFrançois Tigeot 	/*
8141b13d190SFrançois Tigeot 	 * BSpec recommends 8x4 when MSAA is used,
8151b13d190SFrançois Tigeot 	 * however in practice 16x4 seems fastest.
8161b13d190SFrançois Tigeot 	 *
8171b13d190SFrançois Tigeot 	 * Note that PS/WM thread counts depend on the WIZ hashing
8181b13d190SFrançois Tigeot 	 * disable bit, which we don't touch here, but it's good
8191b13d190SFrançois Tigeot 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8201b13d190SFrançois Tigeot 	 */
821*2c9916cdSFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
822*2c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_MASK,
823*2c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_16x4);
8241b13d190SFrançois Tigeot 
8251b13d190SFrançois Tigeot 	return 0;
8261b13d190SFrançois Tigeot }
8271b13d190SFrançois Tigeot 
8281b13d190SFrançois Tigeot static int chv_init_workarounds(struct intel_engine_cs *ring)
8291b13d190SFrançois Tigeot {
8301b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
8311b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
8321b13d190SFrançois Tigeot 
8331b13d190SFrançois Tigeot 	/* WaDisablePartialInstShootdown:chv */
8341b13d190SFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:chv */
835*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
836*2c9916cdSFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
837*2c9916cdSFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
8381b13d190SFrançois Tigeot 
839*2c9916cdSFrançois Tigeot 	/* Use Force Non-Coherent whenever executing a 3D context. This is a
840*2c9916cdSFrançois Tigeot 	 * workaround for a possible hang in the unlikely event a TLB
841*2c9916cdSFrançois Tigeot 	 * invalidation occurs during a PSD flush.
842*2c9916cdSFrançois Tigeot 	 */
843*2c9916cdSFrançois Tigeot 	/* WaForceEnableNonCoherent:chv */
844*2c9916cdSFrançois Tigeot 	/* WaHdcDisableFetchWhenMasked:chv */
845*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0,
846*2c9916cdSFrançois Tigeot 			  HDC_FORCE_NON_COHERENT |
847*2c9916cdSFrançois Tigeot 			  HDC_DONOT_FETCH_MEM_WHEN_MASKED);
8481b13d190SFrançois Tigeot 
849*2c9916cdSFrançois Tigeot 	/* According to the CACHE_MODE_0 default value documentation, some
850*2c9916cdSFrançois Tigeot 	 * CHV platforms disable this optimization by default.  Turn it on.
851*2c9916cdSFrançois Tigeot 	 */
852*2c9916cdSFrançois Tigeot 	WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
8531b13d190SFrançois Tigeot 
854*2c9916cdSFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:chv */
855*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1,
856*2c9916cdSFrançois Tigeot 			  GEN8_4x4_STC_OPTIMIZATION_DISABLE);
857*2c9916cdSFrançois Tigeot 
858*2c9916cdSFrançois Tigeot 	/* Improve HiZ throughput on CHV. */
859*2c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
860*2c9916cdSFrançois Tigeot 
861*2c9916cdSFrançois Tigeot 	/*
862*2c9916cdSFrançois Tigeot 	 * BSpec recommends 8x4 when MSAA is used,
863*2c9916cdSFrançois Tigeot 	 * however in practice 16x4 seems fastest.
864*2c9916cdSFrançois Tigeot 	 *
865*2c9916cdSFrançois Tigeot 	 * Note that PS/WM thread counts depend on the WIZ hashing
866*2c9916cdSFrançois Tigeot 	 * disable bit, which we don't touch here, but it's good
867*2c9916cdSFrançois Tigeot 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
868*2c9916cdSFrançois Tigeot 	 */
869*2c9916cdSFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
870*2c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_MASK,
871*2c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_16x4);
872*2c9916cdSFrançois Tigeot 
873*2c9916cdSFrançois Tigeot 	return 0;
874*2c9916cdSFrançois Tigeot }
875*2c9916cdSFrançois Tigeot 
876*2c9916cdSFrançois Tigeot int init_workarounds_ring(struct intel_engine_cs *ring)
877*2c9916cdSFrançois Tigeot {
878*2c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
879*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
880*2c9916cdSFrançois Tigeot 
881*2c9916cdSFrançois Tigeot 	WARN_ON(ring->id != RCS);
882*2c9916cdSFrançois Tigeot 
883*2c9916cdSFrançois Tigeot 	dev_priv->workarounds.count = 0;
884*2c9916cdSFrançois Tigeot 
885*2c9916cdSFrançois Tigeot 	if (IS_BROADWELL(dev))
886*2c9916cdSFrançois Tigeot 		return bdw_init_workarounds(ring);
887*2c9916cdSFrançois Tigeot 
888*2c9916cdSFrançois Tigeot 	if (IS_CHERRYVIEW(dev))
889*2c9916cdSFrançois Tigeot 		return chv_init_workarounds(ring);
8901b13d190SFrançois Tigeot 
8911b13d190SFrançois Tigeot 	return 0;
8921b13d190SFrançois Tigeot }
8931b13d190SFrançois Tigeot 
894ba55f2f5SFrançois Tigeot static int init_render_ring(struct intel_engine_cs *ring)
895e3adcf8fSFrançois Tigeot {
896e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
897e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
898e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
89924edb884SFrançois Tigeot 	if (ret)
90024edb884SFrançois Tigeot 		return ret;
901e3adcf8fSFrançois Tigeot 
902ba55f2f5SFrançois Tigeot 	/* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
903ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
904f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
905f4e1c372SFrançois Tigeot 
906f4e1c372SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
907f4e1c372SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
908f4e1c372SFrançois Tigeot 	 * programmed to '1' on all products.
9095d0b1887SFrançois Tigeot 	 *
910ba55f2f5SFrançois Tigeot 	 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
911f4e1c372SFrançois Tigeot 	 */
912*2c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 9)
913f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
914f4e1c372SFrançois Tigeot 
915f4e1c372SFrançois Tigeot 	/* Required for the hardware to program scanline values for waiting */
916ba55f2f5SFrançois Tigeot 	/* WaEnableFlushTlbInvalidationMode:snb */
917f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6)
918f4e1c372SFrançois Tigeot 		I915_WRITE(GFX_MODE,
919ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
920f4e1c372SFrançois Tigeot 
921ba55f2f5SFrançois Tigeot 	/* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
922e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev))
923e3adcf8fSFrançois Tigeot 		I915_WRITE(GFX_MODE_GEN7,
924ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
925f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
926e3adcf8fSFrançois Tigeot 
927e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
928e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
929e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
930e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
931e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
932e3adcf8fSFrançois Tigeot 		 */
933e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
934f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
935e3adcf8fSFrançois Tigeot 	}
936e3adcf8fSFrançois Tigeot 
937f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
938f4e1c372SFrançois Tigeot 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
939f4e1c372SFrançois Tigeot 
9409edbd4a0SFrançois Tigeot 	if (HAS_L3_DPF(dev))
9419edbd4a0SFrançois Tigeot 		I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
942e3adcf8fSFrançois Tigeot 
943*2c9916cdSFrançois Tigeot 	return init_workarounds_ring(ring);
944e3adcf8fSFrançois Tigeot }
945e3adcf8fSFrançois Tigeot 
946ba55f2f5SFrançois Tigeot static void render_ring_cleanup(struct intel_engine_cs *ring)
947e3adcf8fSFrançois Tigeot {
948b5c29a34SFrançois Tigeot 	struct drm_device *dev = ring->dev;
94924edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
95024edb884SFrançois Tigeot 
95124edb884SFrançois Tigeot 	if (dev_priv->semaphore_obj) {
95224edb884SFrançois Tigeot 		i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
95324edb884SFrançois Tigeot 		drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
95424edb884SFrançois Tigeot 		dev_priv->semaphore_obj = NULL;
95524edb884SFrançois Tigeot 	}
956b5c29a34SFrançois Tigeot 
9571b13d190SFrançois Tigeot 	intel_fini_pipe_control(ring);
958e3adcf8fSFrançois Tigeot }
959e3adcf8fSFrançois Tigeot 
96024edb884SFrançois Tigeot static int gen8_rcs_signal(struct intel_engine_cs *signaller,
96124edb884SFrançois Tigeot 			   unsigned int num_dwords)
96224edb884SFrançois Tigeot {
96324edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 8
96424edb884SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
96524edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
96624edb884SFrançois Tigeot 	struct intel_engine_cs *waiter;
96724edb884SFrançois Tigeot 	int i, ret, num_rings;
96824edb884SFrançois Tigeot 
96924edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
97024edb884SFrançois Tigeot 	num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
97124edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
97224edb884SFrançois Tigeot 
97324edb884SFrançois Tigeot 	ret = intel_ring_begin(signaller, num_dwords);
97424edb884SFrançois Tigeot 	if (ret)
97524edb884SFrançois Tigeot 		return ret;
97624edb884SFrançois Tigeot 
97724edb884SFrançois Tigeot 	for_each_ring(waiter, dev_priv, i) {
978*2c9916cdSFrançois Tigeot 		u32 seqno;
97924edb884SFrançois Tigeot 		u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
98024edb884SFrançois Tigeot 		if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
98124edb884SFrançois Tigeot 			continue;
98224edb884SFrançois Tigeot 
983*2c9916cdSFrançois Tigeot 		seqno = i915_gem_request_get_seqno(
984*2c9916cdSFrançois Tigeot 					   signaller->outstanding_lazy_request);
98524edb884SFrançois Tigeot 		intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
98624edb884SFrançois Tigeot 		intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
98724edb884SFrançois Tigeot 					   PIPE_CONTROL_QW_WRITE |
98824edb884SFrançois Tigeot 					   PIPE_CONTROL_FLUSH_ENABLE);
98924edb884SFrançois Tigeot 		intel_ring_emit(signaller, lower_32_bits(gtt_offset));
99024edb884SFrançois Tigeot 		intel_ring_emit(signaller, upper_32_bits(gtt_offset));
991*2c9916cdSFrançois Tigeot 		intel_ring_emit(signaller, seqno);
99224edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
99324edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
99424edb884SFrançois Tigeot 					   MI_SEMAPHORE_TARGET(waiter->id));
99524edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
99624edb884SFrançois Tigeot 	}
99724edb884SFrançois Tigeot 
99824edb884SFrançois Tigeot 	return 0;
99924edb884SFrançois Tigeot }
100024edb884SFrançois Tigeot 
100124edb884SFrançois Tigeot static int gen8_xcs_signal(struct intel_engine_cs *signaller,
100224edb884SFrançois Tigeot 			   unsigned int num_dwords)
100324edb884SFrançois Tigeot {
100424edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 6
100524edb884SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
100624edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
100724edb884SFrançois Tigeot 	struct intel_engine_cs *waiter;
100824edb884SFrançois Tigeot 	int i, ret, num_rings;
100924edb884SFrançois Tigeot 
101024edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
101124edb884SFrançois Tigeot 	num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
101224edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
101324edb884SFrançois Tigeot 
101424edb884SFrançois Tigeot 	ret = intel_ring_begin(signaller, num_dwords);
101524edb884SFrançois Tigeot 	if (ret)
101624edb884SFrançois Tigeot 		return ret;
101724edb884SFrançois Tigeot 
101824edb884SFrançois Tigeot 	for_each_ring(waiter, dev_priv, i) {
1019*2c9916cdSFrançois Tigeot 		u32 seqno;
102024edb884SFrançois Tigeot 		u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
102124edb884SFrançois Tigeot 		if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
102224edb884SFrançois Tigeot 			continue;
102324edb884SFrançois Tigeot 
1024*2c9916cdSFrançois Tigeot 		seqno = i915_gem_request_get_seqno(
1025*2c9916cdSFrançois Tigeot 					   signaller->outstanding_lazy_request);
102624edb884SFrançois Tigeot 		intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
102724edb884SFrançois Tigeot 					   MI_FLUSH_DW_OP_STOREDW);
102824edb884SFrançois Tigeot 		intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
102924edb884SFrançois Tigeot 					   MI_FLUSH_DW_USE_GTT);
103024edb884SFrançois Tigeot 		intel_ring_emit(signaller, upper_32_bits(gtt_offset));
1031*2c9916cdSFrançois Tigeot 		intel_ring_emit(signaller, seqno);
103224edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
103324edb884SFrançois Tigeot 					   MI_SEMAPHORE_TARGET(waiter->id));
103424edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
103524edb884SFrançois Tigeot 	}
103624edb884SFrançois Tigeot 
103724edb884SFrançois Tigeot 	return 0;
103824edb884SFrançois Tigeot }
103924edb884SFrançois Tigeot 
1040ba55f2f5SFrançois Tigeot static int gen6_signal(struct intel_engine_cs *signaller,
1041ba55f2f5SFrançois Tigeot 		       unsigned int num_dwords)
1042e3adcf8fSFrançois Tigeot {
1043ba55f2f5SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
1044ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1045ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *useless;
104624edb884SFrançois Tigeot 	int i, ret, num_rings;
1047ba55f2f5SFrançois Tigeot 
104824edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 3
104924edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
105024edb884SFrançois Tigeot 	num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
105124edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
1052ba55f2f5SFrançois Tigeot 
1053ba55f2f5SFrançois Tigeot 	ret = intel_ring_begin(signaller, num_dwords);
1054ba55f2f5SFrançois Tigeot 	if (ret)
1055ba55f2f5SFrançois Tigeot 		return ret;
1056ba55f2f5SFrançois Tigeot 
1057ba55f2f5SFrançois Tigeot 	for_each_ring(useless, dev_priv, i) {
1058ba55f2f5SFrançois Tigeot 		u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1059ba55f2f5SFrançois Tigeot 		if (mbox_reg != GEN6_NOSYNC) {
1060*2c9916cdSFrançois Tigeot 			u32 seqno = i915_gem_request_get_seqno(
1061*2c9916cdSFrançois Tigeot 					   signaller->outstanding_lazy_request);
1062ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1063ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, mbox_reg);
1064*2c9916cdSFrançois Tigeot 			intel_ring_emit(signaller, seqno);
1065ba55f2f5SFrançois Tigeot 		}
1066ba55f2f5SFrançois Tigeot 	}
1067ba55f2f5SFrançois Tigeot 
106824edb884SFrançois Tigeot 	/* If num_dwords was rounded, make sure the tail pointer is correct */
106924edb884SFrançois Tigeot 	if (num_rings % 2 == 0)
107024edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_NOOP);
107124edb884SFrançois Tigeot 
1072ba55f2f5SFrançois Tigeot 	return 0;
1073e3adcf8fSFrançois Tigeot }
1074e3adcf8fSFrançois Tigeot 
1075e3adcf8fSFrançois Tigeot /**
1076e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
1077e3adcf8fSFrançois Tigeot  *
1078e3adcf8fSFrançois Tigeot  * @ring - ring that is adding a request
1079e3adcf8fSFrançois Tigeot  * @seqno - return seqno stuck into the ring
1080e3adcf8fSFrançois Tigeot  *
1081e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
1082e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
1083e3adcf8fSFrançois Tigeot  */
1084e3adcf8fSFrançois Tigeot static int
1085ba55f2f5SFrançois Tigeot gen6_add_request(struct intel_engine_cs *ring)
1086e3adcf8fSFrançois Tigeot {
1087ba55f2f5SFrançois Tigeot 	int ret;
1088e3adcf8fSFrançois Tigeot 
108924edb884SFrançois Tigeot 	if (ring->semaphore.signal)
1090ba55f2f5SFrançois Tigeot 		ret = ring->semaphore.signal(ring, 4);
109124edb884SFrançois Tigeot 	else
109224edb884SFrançois Tigeot 		ret = intel_ring_begin(ring, 4);
109324edb884SFrançois Tigeot 
10949edbd4a0SFrançois Tigeot 	if (ret)
10959edbd4a0SFrançois Tigeot 		return ret;
10969edbd4a0SFrançois Tigeot 
1097e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1098e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1099*2c9916cdSFrançois Tigeot 	intel_ring_emit(ring,
1100*2c9916cdSFrançois Tigeot 		    i915_gem_request_get_seqno(ring->outstanding_lazy_request));
1101e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
11029edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1103e3adcf8fSFrançois Tigeot 
1104e3adcf8fSFrançois Tigeot 	return 0;
1105e3adcf8fSFrançois Tigeot }
1106e3adcf8fSFrançois Tigeot 
1107a2fdbec6SFrançois Tigeot static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1108a2fdbec6SFrançois Tigeot 					      u32 seqno)
1109a2fdbec6SFrançois Tigeot {
1110a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1111a2fdbec6SFrançois Tigeot 	return dev_priv->last_seqno < seqno;
1112a2fdbec6SFrançois Tigeot }
1113a2fdbec6SFrançois Tigeot 
1114e3adcf8fSFrançois Tigeot /**
1115e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
1116e3adcf8fSFrançois Tigeot  *
1117e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
1118e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
1119e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
1120e3adcf8fSFrançois Tigeot  */
112124edb884SFrançois Tigeot 
112224edb884SFrançois Tigeot static int
112324edb884SFrançois Tigeot gen8_ring_sync(struct intel_engine_cs *waiter,
112424edb884SFrançois Tigeot 	       struct intel_engine_cs *signaller,
112524edb884SFrançois Tigeot 	       u32 seqno)
112624edb884SFrançois Tigeot {
112724edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = waiter->dev->dev_private;
112824edb884SFrançois Tigeot 	int ret;
112924edb884SFrançois Tigeot 
113024edb884SFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
113124edb884SFrançois Tigeot 	if (ret)
113224edb884SFrançois Tigeot 		return ret;
113324edb884SFrançois Tigeot 
113424edb884SFrançois Tigeot 	intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
113524edb884SFrançois Tigeot 				MI_SEMAPHORE_GLOBAL_GTT |
113624edb884SFrançois Tigeot 				MI_SEMAPHORE_POLL |
113724edb884SFrançois Tigeot 				MI_SEMAPHORE_SAD_GTE_SDD);
113824edb884SFrançois Tigeot 	intel_ring_emit(waiter, seqno);
113924edb884SFrançois Tigeot 	intel_ring_emit(waiter,
114024edb884SFrançois Tigeot 			lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
114124edb884SFrançois Tigeot 	intel_ring_emit(waiter,
114224edb884SFrançois Tigeot 			upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
114324edb884SFrançois Tigeot 	intel_ring_advance(waiter);
114424edb884SFrançois Tigeot 	return 0;
114524edb884SFrançois Tigeot }
114624edb884SFrançois Tigeot 
1147e3adcf8fSFrançois Tigeot static int
1148ba55f2f5SFrançois Tigeot gen6_ring_sync(struct intel_engine_cs *waiter,
1149ba55f2f5SFrançois Tigeot 	       struct intel_engine_cs *signaller,
1150e3adcf8fSFrançois Tigeot 	       u32 seqno)
1151e3adcf8fSFrançois Tigeot {
1152e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
1153e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
1154e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
1155ba55f2f5SFrançois Tigeot 	u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1156ba55f2f5SFrançois Tigeot 	int ret;
1157e3adcf8fSFrançois Tigeot 
1158686a02f1SFrançois Tigeot 	/* Throughout all of the GEM code, seqno passed implies our current
1159686a02f1SFrançois Tigeot 	 * seqno is >= the last seqno executed. However for hardware the
1160686a02f1SFrançois Tigeot 	 * comparison is strictly greater than.
1161686a02f1SFrançois Tigeot 	 */
1162686a02f1SFrançois Tigeot 	seqno -= 1;
1163686a02f1SFrançois Tigeot 
1164ba55f2f5SFrançois Tigeot 	WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
1165686a02f1SFrançois Tigeot 
1166e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
1167e3adcf8fSFrançois Tigeot 	if (ret)
1168e3adcf8fSFrançois Tigeot 		return ret;
1169e3adcf8fSFrançois Tigeot 
1170a2fdbec6SFrançois Tigeot 	/* If seqno wrap happened, omit the wait with no-ops */
1171a2fdbec6SFrançois Tigeot 	if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
1172ba55f2f5SFrançois Tigeot 		intel_ring_emit(waiter, dw1 | wait_mbox);
1173e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, seqno);
1174e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, 0);
1175e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1176a2fdbec6SFrançois Tigeot 	} else {
1177a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1178a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1179a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1180a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1181a2fdbec6SFrançois Tigeot 	}
1182e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
1183e3adcf8fSFrançois Tigeot 
1184e3adcf8fSFrançois Tigeot 	return 0;
1185e3adcf8fSFrançois Tigeot }
1186e3adcf8fSFrançois Tigeot 
1187e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
1188e3adcf8fSFrançois Tigeot do {									\
1189e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
1190e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
1191e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
1192e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
1193e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
1194e3adcf8fSFrançois Tigeot } while (0)
1195e3adcf8fSFrançois Tigeot 
1196e3adcf8fSFrançois Tigeot static int
1197ba55f2f5SFrançois Tigeot pc_render_add_request(struct intel_engine_cs *ring)
1198e3adcf8fSFrançois Tigeot {
1199ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
1200e3adcf8fSFrançois Tigeot 	int ret;
1201e3adcf8fSFrançois Tigeot 
1202e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1203e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
1204e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
1205e3adcf8fSFrançois Tigeot 	 *
1206e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
1207e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1208e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
1209e3adcf8fSFrançois Tigeot 	 */
1210e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 32);
1211e3adcf8fSFrançois Tigeot 	if (ret)
1212e3adcf8fSFrançois Tigeot 		return ret;
1213e3adcf8fSFrançois Tigeot 
1214e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1215e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
1216e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
12179edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1218*2c9916cdSFrançois Tigeot 	intel_ring_emit(ring,
1219*2c9916cdSFrançois Tigeot 		    i915_gem_request_get_seqno(ring->outstanding_lazy_request));
1220e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1221e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1222ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
1223e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1224ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1225e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1226ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1227e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1228ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1229e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1230ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1231e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1232b5c29a34SFrançois Tigeot 
1233e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1234e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
1235e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
1236e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
12379edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1238*2c9916cdSFrançois Tigeot 	intel_ring_emit(ring,
1239*2c9916cdSFrançois Tigeot 		    i915_gem_request_get_seqno(ring->outstanding_lazy_request));
1240e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
12419edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1242e3adcf8fSFrançois Tigeot 
1243e3adcf8fSFrançois Tigeot 	return 0;
1244e3adcf8fSFrançois Tigeot }
1245e3adcf8fSFrançois Tigeot 
1246e3adcf8fSFrançois Tigeot static u32
1247ba55f2f5SFrançois Tigeot gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1248e3adcf8fSFrançois Tigeot {
1249e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
1250e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
1251e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
1252ba55f2f5SFrançois Tigeot 	if (!lazy_coherency) {
1253ba55f2f5SFrançois Tigeot 		struct drm_i915_private *dev_priv = ring->dev->dev_private;
1254ba55f2f5SFrançois Tigeot 		POSTING_READ(RING_ACTHD(ring->mmio_base));
1255ba55f2f5SFrançois Tigeot 	}
1256ba55f2f5SFrançois Tigeot 
1257e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1258e3adcf8fSFrançois Tigeot }
1259e3adcf8fSFrançois Tigeot 
1260b030f26bSFrançois Tigeot static u32
1261ba55f2f5SFrançois Tigeot ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1262e3adcf8fSFrançois Tigeot {
1263e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1264e3adcf8fSFrançois Tigeot }
1265e3adcf8fSFrançois Tigeot 
1266a2fdbec6SFrançois Tigeot static void
1267ba55f2f5SFrançois Tigeot ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1268a2fdbec6SFrançois Tigeot {
1269a2fdbec6SFrançois Tigeot 	intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1270a2fdbec6SFrançois Tigeot }
1271a2fdbec6SFrançois Tigeot 
1272b030f26bSFrançois Tigeot static u32
1273ba55f2f5SFrançois Tigeot pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1274e3adcf8fSFrançois Tigeot {
12759edbd4a0SFrançois Tigeot 	return ring->scratch.cpu_page[0];
1276e3adcf8fSFrançois Tigeot }
1277e3adcf8fSFrançois Tigeot 
1278a2fdbec6SFrançois Tigeot static void
1279ba55f2f5SFrançois Tigeot pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1280a2fdbec6SFrançois Tigeot {
12819edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page[0] = seqno;
1282a2fdbec6SFrançois Tigeot }
1283a2fdbec6SFrançois Tigeot 
1284e3adcf8fSFrançois Tigeot static bool
1285ba55f2f5SFrançois Tigeot gen5_ring_get_irq(struct intel_engine_cs *ring)
1286e3adcf8fSFrançois Tigeot {
1287e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1288ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1289e3adcf8fSFrançois Tigeot 
1290*2c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1291e3adcf8fSFrançois Tigeot 		return false;
1292e3adcf8fSFrançois Tigeot 
129302727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
12949edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0)
129524edb884SFrançois Tigeot 		gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
129602727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1297e3adcf8fSFrançois Tigeot 
1298e3adcf8fSFrançois Tigeot 	return true;
1299e3adcf8fSFrançois Tigeot }
1300e3adcf8fSFrançois Tigeot 
1301e3adcf8fSFrançois Tigeot static void
1302ba55f2f5SFrançois Tigeot gen5_ring_put_irq(struct intel_engine_cs *ring)
1303e3adcf8fSFrançois Tigeot {
1304e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1305ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1306e3adcf8fSFrançois Tigeot 
130702727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
13089edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0)
130924edb884SFrançois Tigeot 		gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1310686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1311686a02f1SFrançois Tigeot }
1312686a02f1SFrançois Tigeot 
1313686a02f1SFrançois Tigeot static bool
1314ba55f2f5SFrançois Tigeot i9xx_ring_get_irq(struct intel_engine_cs *ring)
1315686a02f1SFrançois Tigeot {
1316686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1317ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1318686a02f1SFrançois Tigeot 
1319*2c9916cdSFrançois Tigeot 	if (!intel_irqs_enabled(dev_priv))
1320686a02f1SFrançois Tigeot 		return false;
1321686a02f1SFrançois Tigeot 
1322686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
13239edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
1324686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
1325686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
1326686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
1327686a02f1SFrançois Tigeot 	}
1328686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1329686a02f1SFrançois Tigeot 
1330686a02f1SFrançois Tigeot 	return true;
1331686a02f1SFrançois Tigeot }
1332686a02f1SFrançois Tigeot 
1333686a02f1SFrançois Tigeot static void
1334ba55f2f5SFrançois Tigeot i9xx_ring_put_irq(struct intel_engine_cs *ring)
1335686a02f1SFrançois Tigeot {
1336686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1337ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1338686a02f1SFrançois Tigeot 
1339686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
13409edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
1341686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
1342686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
1343686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
1344686a02f1SFrançois Tigeot 	}
1345686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1346686a02f1SFrançois Tigeot }
1347686a02f1SFrançois Tigeot 
1348686a02f1SFrançois Tigeot static bool
1349ba55f2f5SFrançois Tigeot i8xx_ring_get_irq(struct intel_engine_cs *ring)
1350686a02f1SFrançois Tigeot {
1351686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1352ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1353686a02f1SFrançois Tigeot 
1354*2c9916cdSFrançois Tigeot 	if (!intel_irqs_enabled(dev_priv))
1355686a02f1SFrançois Tigeot 		return false;
1356686a02f1SFrançois Tigeot 
1357686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
13589edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
1359686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
1360686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
1361686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
1362686a02f1SFrançois Tigeot 	}
1363686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1364686a02f1SFrançois Tigeot 
1365686a02f1SFrançois Tigeot 	return true;
1366686a02f1SFrançois Tigeot }
1367686a02f1SFrançois Tigeot 
1368686a02f1SFrançois Tigeot static void
1369ba55f2f5SFrançois Tigeot i8xx_ring_put_irq(struct intel_engine_cs *ring)
1370686a02f1SFrançois Tigeot {
1371686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1372ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1373686a02f1SFrançois Tigeot 
1374686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
13759edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
1376686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
1377686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
1378686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
1379e3adcf8fSFrançois Tigeot 	}
138002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1381e3adcf8fSFrançois Tigeot }
1382e3adcf8fSFrançois Tigeot 
1383ba55f2f5SFrançois Tigeot void intel_ring_setup_status_page(struct intel_engine_cs *ring)
1384e3adcf8fSFrançois Tigeot {
1385e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1386ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1387b5c29a34SFrançois Tigeot 	u32 mmio = 0;
1388e3adcf8fSFrançois Tigeot 
1389e3adcf8fSFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
1390e3adcf8fSFrançois Tigeot 	 * the ring registers as of gen7.
1391e3adcf8fSFrançois Tigeot 	 */
1392e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev)) {
1393e3adcf8fSFrançois Tigeot 		switch (ring->id) {
1394e3adcf8fSFrançois Tigeot 		case RCS:
1395e3adcf8fSFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
1396e3adcf8fSFrançois Tigeot 			break;
1397e3adcf8fSFrançois Tigeot 		case BCS:
1398e3adcf8fSFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
1399e3adcf8fSFrançois Tigeot 			break;
1400ba55f2f5SFrançois Tigeot 		/*
1401ba55f2f5SFrançois Tigeot 		 * VCS2 actually doesn't exist on Gen7. Only shut up
1402ba55f2f5SFrançois Tigeot 		 * gcc switch check warning
1403ba55f2f5SFrançois Tigeot 		 */
1404ba55f2f5SFrançois Tigeot 		case VCS2:
1405e3adcf8fSFrançois Tigeot 		case VCS:
1406e3adcf8fSFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
1407e3adcf8fSFrançois Tigeot 			break;
14085d0b1887SFrançois Tigeot 		case VECS:
14095d0b1887SFrançois Tigeot 			mmio = VEBOX_HWS_PGA_GEN7;
14105d0b1887SFrançois Tigeot 			break;
1411e3adcf8fSFrançois Tigeot 		}
1412b5c29a34SFrançois Tigeot 	} else if (IS_GEN6(ring->dev)) {
1413e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
1414e3adcf8fSFrançois Tigeot 	} else {
14159edbd4a0SFrançois Tigeot 		/* XXX: gen8 returns to sanity */
1416e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
1417e3adcf8fSFrançois Tigeot 	}
1418e3adcf8fSFrançois Tigeot 
1419e3adcf8fSFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
1420e3adcf8fSFrançois Tigeot 	POSTING_READ(mmio);
14215d0b1887SFrançois Tigeot 
1422ba55f2f5SFrançois Tigeot 	/*
1423ba55f2f5SFrançois Tigeot 	 * Flush the TLB for this page
1424ba55f2f5SFrançois Tigeot 	 *
1425ba55f2f5SFrançois Tigeot 	 * FIXME: These two bits have disappeared on gen8, so a question
1426ba55f2f5SFrançois Tigeot 	 * arises: do we still need this and if so how should we go about
1427ba55f2f5SFrançois Tigeot 	 * invalidating the TLB?
1428ba55f2f5SFrançois Tigeot 	 */
1429ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
14305d0b1887SFrançois Tigeot 		u32 reg = RING_INSTPM(ring->mmio_base);
1431ba55f2f5SFrançois Tigeot 
1432ba55f2f5SFrançois Tigeot 		/* ring should be idle before issuing a sync flush*/
1433ba55f2f5SFrançois Tigeot 		WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
1434ba55f2f5SFrançois Tigeot 
14355d0b1887SFrançois Tigeot 		I915_WRITE(reg,
14365d0b1887SFrançois Tigeot 			   _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
14375d0b1887SFrançois Tigeot 					      INSTPM_SYNC_FLUSH));
14385d0b1887SFrançois Tigeot 		if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
14395d0b1887SFrançois Tigeot 			     1000))
14405d0b1887SFrançois Tigeot 			DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
14415d0b1887SFrançois Tigeot 				  ring->name);
14425d0b1887SFrançois Tigeot 	}
1443e3adcf8fSFrançois Tigeot }
1444e3adcf8fSFrançois Tigeot 
1445e3adcf8fSFrançois Tigeot static int
1446ba55f2f5SFrançois Tigeot bsd_ring_flush(struct intel_engine_cs *ring,
1447b5c29a34SFrançois Tigeot 	       u32     invalidate_domains,
1448b5c29a34SFrançois Tigeot 	       u32     flush_domains)
1449e3adcf8fSFrançois Tigeot {
1450e3adcf8fSFrançois Tigeot 	int ret;
1451e3adcf8fSFrançois Tigeot 
1452e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1453e3adcf8fSFrançois Tigeot 	if (ret)
1454e3adcf8fSFrançois Tigeot 		return ret;
1455e3adcf8fSFrançois Tigeot 
1456e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
1457e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1458e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1459e3adcf8fSFrançois Tigeot 	return 0;
1460e3adcf8fSFrançois Tigeot }
1461e3adcf8fSFrançois Tigeot 
1462e3adcf8fSFrançois Tigeot static int
1463ba55f2f5SFrançois Tigeot i9xx_add_request(struct intel_engine_cs *ring)
1464e3adcf8fSFrançois Tigeot {
1465e3adcf8fSFrançois Tigeot 	int ret;
1466e3adcf8fSFrançois Tigeot 
1467e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1468e3adcf8fSFrançois Tigeot 	if (ret)
1469e3adcf8fSFrançois Tigeot 		return ret;
1470e3adcf8fSFrançois Tigeot 
1471e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1472e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1473*2c9916cdSFrançois Tigeot 	intel_ring_emit(ring,
1474*2c9916cdSFrançois Tigeot 		    i915_gem_request_get_seqno(ring->outstanding_lazy_request));
1475e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
14769edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1477e3adcf8fSFrançois Tigeot 
1478e3adcf8fSFrançois Tigeot 	return 0;
1479e3adcf8fSFrançois Tigeot }
1480e3adcf8fSFrançois Tigeot 
1481e3adcf8fSFrançois Tigeot static bool
1482ba55f2f5SFrançois Tigeot gen6_ring_get_irq(struct intel_engine_cs *ring)
1483e3adcf8fSFrançois Tigeot {
1484e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1485ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1486e3adcf8fSFrançois Tigeot 
1487*2c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1488e3adcf8fSFrançois Tigeot 		return false;
1489e3adcf8fSFrançois Tigeot 
149002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
14919edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
14929edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
14935d0b1887SFrançois Tigeot 			I915_WRITE_IMR(ring,
14945d0b1887SFrançois Tigeot 				       ~(ring->irq_enable_mask |
14959edbd4a0SFrançois Tigeot 					 GT_PARITY_ERROR(dev)));
1496686a02f1SFrançois Tigeot 		else
1497686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
149824edb884SFrançois Tigeot 		gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1499e3adcf8fSFrançois Tigeot 	}
150002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1501e3adcf8fSFrançois Tigeot 
1502e3adcf8fSFrançois Tigeot 	return true;
1503e3adcf8fSFrançois Tigeot }
1504e3adcf8fSFrançois Tigeot 
1505e3adcf8fSFrançois Tigeot static void
1506ba55f2f5SFrançois Tigeot gen6_ring_put_irq(struct intel_engine_cs *ring)
1507e3adcf8fSFrançois Tigeot {
1508e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1509ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1510e3adcf8fSFrançois Tigeot 
151102727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15129edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
15139edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
15149edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1515686a02f1SFrançois Tigeot 		else
1516686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
151724edb884SFrançois Tigeot 		gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1518e3adcf8fSFrançois Tigeot 	}
151902727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1520e3adcf8fSFrançois Tigeot }
1521e3adcf8fSFrançois Tigeot 
15225d0b1887SFrançois Tigeot static bool
1523ba55f2f5SFrançois Tigeot hsw_vebox_get_irq(struct intel_engine_cs *ring)
15245d0b1887SFrançois Tigeot {
15255d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
15265d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15275d0b1887SFrançois Tigeot 
1528*2c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
15295d0b1887SFrançois Tigeot 		return false;
15305d0b1887SFrançois Tigeot 
15319edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15329edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
15335d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
153424edb884SFrançois Tigeot 		gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
15355d0b1887SFrançois Tigeot 	}
15369edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
15375d0b1887SFrançois Tigeot 
15385d0b1887SFrançois Tigeot 	return true;
15395d0b1887SFrançois Tigeot }
15405d0b1887SFrançois Tigeot 
15415d0b1887SFrançois Tigeot static void
1542ba55f2f5SFrançois Tigeot hsw_vebox_put_irq(struct intel_engine_cs *ring)
15435d0b1887SFrançois Tigeot {
15445d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
15455d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15465d0b1887SFrançois Tigeot 
15479edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15489edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
15495d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~0);
155024edb884SFrançois Tigeot 		gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
15515d0b1887SFrançois Tigeot 	}
15529edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
15539edbd4a0SFrançois Tigeot }
15549edbd4a0SFrançois Tigeot 
15559edbd4a0SFrançois Tigeot static bool
1556ba55f2f5SFrançois Tigeot gen8_ring_get_irq(struct intel_engine_cs *ring)
15579edbd4a0SFrançois Tigeot {
15589edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
15599edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15609edbd4a0SFrançois Tigeot 
1561*2c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
15629edbd4a0SFrançois Tigeot 		return false;
15639edbd4a0SFrançois Tigeot 
15649edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15659edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
15669edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
15679edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
15689edbd4a0SFrançois Tigeot 				       ~(ring->irq_enable_mask |
15699edbd4a0SFrançois Tigeot 					 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
15709edbd4a0SFrançois Tigeot 		} else {
15719edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
15729edbd4a0SFrançois Tigeot 		}
15739edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
15749edbd4a0SFrançois Tigeot 	}
15759edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
15769edbd4a0SFrançois Tigeot 
15779edbd4a0SFrançois Tigeot 	return true;
15789edbd4a0SFrançois Tigeot }
15799edbd4a0SFrançois Tigeot 
15809edbd4a0SFrançois Tigeot static void
1581ba55f2f5SFrançois Tigeot gen8_ring_put_irq(struct intel_engine_cs *ring)
15829edbd4a0SFrançois Tigeot {
15839edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
15849edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15859edbd4a0SFrançois Tigeot 
15869edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
15879edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
15889edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
15899edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
15909edbd4a0SFrançois Tigeot 				       ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
15919edbd4a0SFrançois Tigeot 		} else {
15929edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
15939edbd4a0SFrançois Tigeot 		}
15949edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
15959edbd4a0SFrançois Tigeot 	}
15969edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
15975d0b1887SFrançois Tigeot }
15985d0b1887SFrançois Tigeot 
1599e3adcf8fSFrançois Tigeot static int
1600ba55f2f5SFrançois Tigeot i965_dispatch_execbuffer(struct intel_engine_cs *ring,
1601ba55f2f5SFrançois Tigeot 			 u64 offset, u32 length,
1602b5c29a34SFrançois Tigeot 			 unsigned flags)
1603e3adcf8fSFrançois Tigeot {
1604e3adcf8fSFrançois Tigeot 	int ret;
1605e3adcf8fSFrançois Tigeot 
1606e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1607e3adcf8fSFrançois Tigeot 	if (ret)
1608e3adcf8fSFrançois Tigeot 		return ret;
1609e3adcf8fSFrançois Tigeot 
1610e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
1611686a02f1SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1612b5c29a34SFrançois Tigeot 			MI_BATCH_GTT |
1613b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
1614e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1615e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1616e3adcf8fSFrançois Tigeot 
1617e3adcf8fSFrançois Tigeot 	return 0;
1618e3adcf8fSFrançois Tigeot }
1619e3adcf8fSFrançois Tigeot 
1620b5c29a34SFrançois Tigeot /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1621b5c29a34SFrançois Tigeot #define I830_BATCH_LIMIT (256*1024)
162224edb884SFrançois Tigeot #define I830_TLB_ENTRIES (2)
162324edb884SFrançois Tigeot #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
1624e3adcf8fSFrançois Tigeot static int
1625ba55f2f5SFrançois Tigeot i830_dispatch_execbuffer(struct intel_engine_cs *ring,
1626ba55f2f5SFrançois Tigeot 				u64 offset, u32 len,
1627b5c29a34SFrançois Tigeot 				unsigned flags)
1628e3adcf8fSFrançois Tigeot {
162924edb884SFrançois Tigeot 	u32 cs_offset = ring->scratch.gtt_offset;
1630e3adcf8fSFrançois Tigeot 	int ret;
1631e3adcf8fSFrançois Tigeot 
163224edb884SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
163324edb884SFrançois Tigeot 	if (ret)
163424edb884SFrançois Tigeot 		return ret;
163524edb884SFrançois Tigeot 
163624edb884SFrançois Tigeot 	/* Evict the invalid PTE TLBs */
163724edb884SFrançois Tigeot 	intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
163824edb884SFrançois Tigeot 	intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
163924edb884SFrançois Tigeot 	intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
164024edb884SFrançois Tigeot 	intel_ring_emit(ring, cs_offset);
164124edb884SFrançois Tigeot 	intel_ring_emit(ring, 0xdeadbeef);
164224edb884SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
164324edb884SFrançois Tigeot 	intel_ring_advance(ring);
164424edb884SFrançois Tigeot 
164524edb884SFrançois Tigeot 	if ((flags & I915_DISPATCH_PINNED) == 0) {
164624edb884SFrançois Tigeot 		if (len > I830_BATCH_LIMIT)
164724edb884SFrançois Tigeot 			return -ENOSPC;
164824edb884SFrançois Tigeot 
164924edb884SFrançois Tigeot 		ret = intel_ring_begin(ring, 6 + 2);
165024edb884SFrançois Tigeot 		if (ret)
165124edb884SFrançois Tigeot 			return ret;
165224edb884SFrançois Tigeot 
165324edb884SFrançois Tigeot 		/* Blit the batch (which has now all relocs applied) to the
165424edb884SFrançois Tigeot 		 * stable batch scratch bo area (so that the CS never
165524edb884SFrançois Tigeot 		 * stumbles over its tlb invalidation bug) ...
165624edb884SFrançois Tigeot 		 */
165724edb884SFrançois Tigeot 		intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
165824edb884SFrançois Tigeot 		intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
165924edb884SFrançois Tigeot 		intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
166024edb884SFrançois Tigeot 		intel_ring_emit(ring, cs_offset);
166124edb884SFrançois Tigeot 		intel_ring_emit(ring, 4096);
166224edb884SFrançois Tigeot 		intel_ring_emit(ring, offset);
166324edb884SFrançois Tigeot 
166424edb884SFrançois Tigeot 		intel_ring_emit(ring, MI_FLUSH);
166524edb884SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
166624edb884SFrançois Tigeot 		intel_ring_advance(ring);
166724edb884SFrançois Tigeot 
166824edb884SFrançois Tigeot 		/* ... and execute it. */
166924edb884SFrançois Tigeot 		offset = cs_offset;
167024edb884SFrançois Tigeot 	}
167124edb884SFrançois Tigeot 
1672e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1673e3adcf8fSFrançois Tigeot 	if (ret)
1674e3adcf8fSFrançois Tigeot 		return ret;
1675e3adcf8fSFrançois Tigeot 
1676e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER);
1677b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1678e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset + len - 8);
1679b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1680686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
1681686a02f1SFrançois Tigeot 
1682686a02f1SFrançois Tigeot 	return 0;
1683686a02f1SFrançois Tigeot }
1684686a02f1SFrançois Tigeot 
1685686a02f1SFrançois Tigeot static int
1686ba55f2f5SFrançois Tigeot i915_dispatch_execbuffer(struct intel_engine_cs *ring,
1687ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1688b5c29a34SFrançois Tigeot 			 unsigned flags)
1689686a02f1SFrançois Tigeot {
1690686a02f1SFrançois Tigeot 	int ret;
1691686a02f1SFrançois Tigeot 
1692e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1693e3adcf8fSFrançois Tigeot 	if (ret)
1694e3adcf8fSFrançois Tigeot 		return ret;
1695e3adcf8fSFrançois Tigeot 
1696686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1697686a02f1SFrançois Tigeot 	intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1698e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1699e3adcf8fSFrançois Tigeot 
1700e3adcf8fSFrançois Tigeot 	return 0;
1701e3adcf8fSFrançois Tigeot }
1702e3adcf8fSFrançois Tigeot 
1703ba55f2f5SFrançois Tigeot static void cleanup_status_page(struct intel_engine_cs *ring)
1704e3adcf8fSFrançois Tigeot {
1705e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1706e3adcf8fSFrançois Tigeot 
1707e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
1708e3adcf8fSFrançois Tigeot 	if (obj == NULL)
1709e3adcf8fSFrançois Tigeot 		return;
1710e3adcf8fSFrançois Tigeot 
17119edbd4a0SFrançois Tigeot 	kunmap(obj->pages[0]);
1712ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(obj);
1713e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1714e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
1715e3adcf8fSFrançois Tigeot }
1716e3adcf8fSFrançois Tigeot 
1717ba55f2f5SFrançois Tigeot static int init_status_page(struct intel_engine_cs *ring)
1718e3adcf8fSFrançois Tigeot {
1719e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1720ba55f2f5SFrançois Tigeot 
1721ba55f2f5SFrançois Tigeot 	if ((obj = ring->status_page.obj) == NULL) {
172224edb884SFrançois Tigeot 		unsigned flags;
1723e3adcf8fSFrançois Tigeot 		int ret;
1724e3adcf8fSFrançois Tigeot 
1725ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(ring->dev, 4096);
1726e3adcf8fSFrançois Tigeot 		if (obj == NULL) {
1727e3adcf8fSFrançois Tigeot 			DRM_ERROR("Failed to allocate status page\n");
1728ba55f2f5SFrançois Tigeot 			return -ENOMEM;
1729e3adcf8fSFrançois Tigeot 		}
1730e3adcf8fSFrançois Tigeot 
1731ba55f2f5SFrançois Tigeot 		ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1732ba55f2f5SFrançois Tigeot 		if (ret)
1733e3adcf8fSFrançois Tigeot 			goto err_unref;
1734ba55f2f5SFrançois Tigeot 
173524edb884SFrançois Tigeot 		flags = 0;
173624edb884SFrançois Tigeot 		if (!HAS_LLC(ring->dev))
173724edb884SFrançois Tigeot 			/* On g33, we cannot place HWS above 256MiB, so
173824edb884SFrançois Tigeot 			 * restrict its pinning to the low mappable arena.
173924edb884SFrançois Tigeot 			 * Though this restriction is not documented for
174024edb884SFrançois Tigeot 			 * gen4, gen5, or byt, they also behave similarly
174124edb884SFrançois Tigeot 			 * and hang if the HWS is placed at the top of the
174224edb884SFrançois Tigeot 			 * GTT. To generalise, it appears that all !llc
174324edb884SFrançois Tigeot 			 * platforms have issues with us placing the HWS
174424edb884SFrançois Tigeot 			 * above the mappable region (even though we never
174524edb884SFrançois Tigeot 			 * actualy map it).
174624edb884SFrançois Tigeot 			 */
174724edb884SFrançois Tigeot 			flags |= PIN_MAPPABLE;
174824edb884SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
1749ba55f2f5SFrançois Tigeot 		if (ret) {
1750ba55f2f5SFrançois Tigeot err_unref:
1751ba55f2f5SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
1752ba55f2f5SFrançois Tigeot 			return ret;
1753ba55f2f5SFrançois Tigeot 		}
1754ba55f2f5SFrançois Tigeot 
1755ba55f2f5SFrançois Tigeot 		ring->status_page.obj = obj;
1756e3adcf8fSFrançois Tigeot 	}
1757e3adcf8fSFrançois Tigeot 
17589edbd4a0SFrançois Tigeot 	ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1759f4f90b23SFrançois Tigeot 	ring->status_page.page_addr = kmap(obj->pages[0]);
1760e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1761e3adcf8fSFrançois Tigeot 
1762b5c29a34SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1763e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1764e3adcf8fSFrançois Tigeot 
1765e3adcf8fSFrançois Tigeot 	return 0;
1766e3adcf8fSFrançois Tigeot }
1767e3adcf8fSFrançois Tigeot 
1768ba55f2f5SFrançois Tigeot static int init_phys_status_page(struct intel_engine_cs *ring)
1769686a02f1SFrançois Tigeot {
1770686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1771686a02f1SFrançois Tigeot 
1772686a02f1SFrançois Tigeot 	if (!dev_priv->status_page_dmah) {
1773686a02f1SFrançois Tigeot 		dev_priv->status_page_dmah =
1774b31e9d59SFrançois Tigeot 			drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1775686a02f1SFrançois Tigeot 		if (!dev_priv->status_page_dmah)
1776686a02f1SFrançois Tigeot 			return -ENOMEM;
1777686a02f1SFrançois Tigeot 	}
1778686a02f1SFrançois Tigeot 
1779686a02f1SFrançois Tigeot 	ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1780686a02f1SFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1781686a02f1SFrançois Tigeot 
1782686a02f1SFrançois Tigeot 	return 0;
1783686a02f1SFrançois Tigeot }
1784686a02f1SFrançois Tigeot 
1785*2c9916cdSFrançois Tigeot void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1786*2c9916cdSFrançois Tigeot {
1787*2c9916cdSFrançois Tigeot 	iounmap(ringbuf->virtual_start, ringbuf->size);
1788*2c9916cdSFrançois Tigeot 	ringbuf->virtual_start = NULL;
1789*2c9916cdSFrançois Tigeot 	i915_gem_object_ggtt_unpin(ringbuf->obj);
1790*2c9916cdSFrançois Tigeot }
1791*2c9916cdSFrançois Tigeot 
1792*2c9916cdSFrançois Tigeot int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
1793*2c9916cdSFrançois Tigeot 				     struct intel_ringbuffer *ringbuf)
1794*2c9916cdSFrançois Tigeot {
1795*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
1796*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
1797*2c9916cdSFrançois Tigeot 	int ret;
1798*2c9916cdSFrançois Tigeot 
1799*2c9916cdSFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1800*2c9916cdSFrançois Tigeot 	if (ret)
1801*2c9916cdSFrançois Tigeot 		return ret;
1802*2c9916cdSFrançois Tigeot 
1803*2c9916cdSFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
1804*2c9916cdSFrançois Tigeot 	if (ret) {
1805*2c9916cdSFrançois Tigeot 		i915_gem_object_ggtt_unpin(obj);
1806*2c9916cdSFrançois Tigeot 		return ret;
1807*2c9916cdSFrançois Tigeot 	}
1808*2c9916cdSFrançois Tigeot 
1809*2c9916cdSFrançois Tigeot 	ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
1810*2c9916cdSFrançois Tigeot 			i915_gem_obj_ggtt_offset(obj), ringbuf->size);
1811*2c9916cdSFrançois Tigeot 	if (ringbuf->virtual_start == NULL) {
1812*2c9916cdSFrançois Tigeot 		i915_gem_object_ggtt_unpin(obj);
1813*2c9916cdSFrançois Tigeot 		return -EINVAL;
1814*2c9916cdSFrançois Tigeot 	}
1815*2c9916cdSFrançois Tigeot 
1816*2c9916cdSFrançois Tigeot 	return 0;
1817*2c9916cdSFrançois Tigeot }
1818*2c9916cdSFrançois Tigeot 
18191b13d190SFrançois Tigeot void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1820e3adcf8fSFrançois Tigeot {
182124edb884SFrançois Tigeot 	drm_gem_object_unreference(&ringbuf->obj->base);
182224edb884SFrançois Tigeot 	ringbuf->obj = NULL;
182324edb884SFrançois Tigeot }
182424edb884SFrançois Tigeot 
18251b13d190SFrançois Tigeot int intel_alloc_ringbuffer_obj(struct drm_device *dev,
182624edb884SFrançois Tigeot 			       struct intel_ringbuffer *ringbuf)
182724edb884SFrançois Tigeot {
1828e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1829e3adcf8fSFrançois Tigeot 
1830a2fdbec6SFrançois Tigeot 	obj = NULL;
1831a2fdbec6SFrançois Tigeot 	if (!HAS_LLC(dev))
1832ba55f2f5SFrançois Tigeot 		obj = i915_gem_object_create_stolen(dev, ringbuf->size);
1833a2fdbec6SFrançois Tigeot 	if (obj == NULL)
1834ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, ringbuf->size);
1835ba55f2f5SFrançois Tigeot 	if (obj == NULL)
1836ba55f2f5SFrançois Tigeot 		return -ENOMEM;
1837e3adcf8fSFrançois Tigeot 
183824edb884SFrançois Tigeot 	/* mark ring buffers as read-only from GPU side by default */
183924edb884SFrançois Tigeot 	obj->gt_ro = 1;
184024edb884SFrançois Tigeot 
1841ba55f2f5SFrançois Tigeot 	ringbuf->obj = obj;
1842ba55f2f5SFrançois Tigeot 
1843*2c9916cdSFrançois Tigeot 	return 0;
1844ba55f2f5SFrançois Tigeot }
1845ba55f2f5SFrançois Tigeot 
1846ba55f2f5SFrançois Tigeot static int intel_init_ring_buffer(struct drm_device *dev,
1847ba55f2f5SFrançois Tigeot 				  struct intel_engine_cs *ring)
1848ba55f2f5SFrançois Tigeot {
1849*2c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf;
1850ba55f2f5SFrançois Tigeot 	int ret;
1851ba55f2f5SFrançois Tigeot 
1852*2c9916cdSFrançois Tigeot 	WARN_ON(ring->buffer);
1853*2c9916cdSFrançois Tigeot 
1854ba55f2f5SFrançois Tigeot 	ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1855ba55f2f5SFrançois Tigeot 	if (!ringbuf)
1856ba55f2f5SFrançois Tigeot 		return -ENOMEM;
1857ba55f2f5SFrançois Tigeot 	ring->buffer = ringbuf;
1858ba55f2f5SFrançois Tigeot 
1859ba55f2f5SFrançois Tigeot 	ring->dev = dev;
1860ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1861ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
18621b13d190SFrançois Tigeot 	INIT_LIST_HEAD(&ring->execlist_queue);
1863ba55f2f5SFrançois Tigeot 	ringbuf->size = 32 * PAGE_SIZE;
18641b13d190SFrançois Tigeot 	ringbuf->ring = ring;
1865ba55f2f5SFrançois Tigeot 	memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
1866ba55f2f5SFrançois Tigeot 
1867ba55f2f5SFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
1868ba55f2f5SFrançois Tigeot 
1869ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
1870ba55f2f5SFrançois Tigeot 		ret = init_status_page(ring);
1871e3adcf8fSFrançois Tigeot 		if (ret)
1872ba55f2f5SFrançois Tigeot 			goto error;
1873ba55f2f5SFrançois Tigeot 	} else {
1874ba55f2f5SFrançois Tigeot 		BUG_ON(ring->id != RCS);
1875ba55f2f5SFrançois Tigeot 		ret = init_phys_status_page(ring);
1876ba55f2f5SFrançois Tigeot 		if (ret)
1877ba55f2f5SFrançois Tigeot 			goto error;
1878ba55f2f5SFrançois Tigeot 	}
1879ba55f2f5SFrançois Tigeot 
1880*2c9916cdSFrançois Tigeot 	WARN_ON(ringbuf->obj);
1881*2c9916cdSFrançois Tigeot 
188224edb884SFrançois Tigeot 	ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
1883ba55f2f5SFrançois Tigeot 	if (ret) {
1884*2c9916cdSFrançois Tigeot 		DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
1885*2c9916cdSFrançois Tigeot 				ring->name, ret);
1886*2c9916cdSFrançois Tigeot 		goto error;
1887*2c9916cdSFrançois Tigeot 	}
1888*2c9916cdSFrançois Tigeot 
1889*2c9916cdSFrançois Tigeot 	ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
1890*2c9916cdSFrançois Tigeot 	if (ret) {
1891*2c9916cdSFrançois Tigeot 		DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
1892*2c9916cdSFrançois Tigeot 				ring->name, ret);
1893*2c9916cdSFrançois Tigeot 		intel_destroy_ringbuffer_obj(ringbuf);
1894ba55f2f5SFrançois Tigeot 		goto error;
1895ba55f2f5SFrançois Tigeot 	}
1896e3adcf8fSFrançois Tigeot 
1897e3adcf8fSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
1898e3adcf8fSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
1899e3adcf8fSFrançois Tigeot 	 * of the buffer.
1900e3adcf8fSFrançois Tigeot 	 */
1901ba55f2f5SFrançois Tigeot 	ringbuf->effective_size = ringbuf->size;
1902ba55f2f5SFrançois Tigeot 	if (IS_I830(dev) || IS_845G(dev))
1903ba55f2f5SFrançois Tigeot 		ringbuf->effective_size -= 2 * CACHELINE_BYTES;
1904ba55f2f5SFrançois Tigeot 
1905ba55f2f5SFrançois Tigeot 	ret = i915_cmd_parser_init_ring(ring);
1906ba55f2f5SFrançois Tigeot 	if (ret)
1907ba55f2f5SFrançois Tigeot 		goto error;
1908ba55f2f5SFrançois Tigeot 
1909e3adcf8fSFrançois Tigeot 	return 0;
1910e3adcf8fSFrançois Tigeot 
1911ba55f2f5SFrançois Tigeot error:
1912ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
1913ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
1914e3adcf8fSFrançois Tigeot 	return ret;
1915e3adcf8fSFrançois Tigeot }
1916e3adcf8fSFrançois Tigeot 
1917ba55f2f5SFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
1918e3adcf8fSFrançois Tigeot {
1919*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv;
1920*2c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf;
1921e3adcf8fSFrançois Tigeot 
1922ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
1923e3adcf8fSFrançois Tigeot 		return;
1924e3adcf8fSFrançois Tigeot 
1925*2c9916cdSFrançois Tigeot 	dev_priv = to_i915(ring->dev);
1926*2c9916cdSFrançois Tigeot 	ringbuf = ring->buffer;
1927*2c9916cdSFrançois Tigeot 
1928ba55f2f5SFrançois Tigeot 	intel_stop_ring_buffer(ring);
1929ba55f2f5SFrançois Tigeot 	WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
1930b030f26bSFrançois Tigeot 
1931*2c9916cdSFrançois Tigeot 	intel_unpin_ringbuffer_obj(ringbuf);
193224edb884SFrançois Tigeot 	intel_destroy_ringbuffer_obj(ringbuf);
1933*2c9916cdSFrançois Tigeot 	i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
1934e3adcf8fSFrançois Tigeot 
1935e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
1936e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
1937e3adcf8fSFrançois Tigeot 
1938e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1939ba55f2f5SFrançois Tigeot 
1940ba55f2f5SFrançois Tigeot 	i915_cmd_parser_fini_ring(ring);
1941ba55f2f5SFrançois Tigeot 
1942ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
1943ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
1944e3adcf8fSFrançois Tigeot }
1945e3adcf8fSFrançois Tigeot 
1946ba55f2f5SFrançois Tigeot static int intel_ring_wait_request(struct intel_engine_cs *ring, int n)
1947e3adcf8fSFrançois Tigeot {
1948ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1949e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
1950e3adcf8fSFrançois Tigeot 	int ret;
1951e3adcf8fSFrançois Tigeot 
1952*2c9916cdSFrançois Tigeot 	if (intel_ring_space(ringbuf) >= n)
1953e3adcf8fSFrançois Tigeot 		return 0;
1954e3adcf8fSFrançois Tigeot 
1955e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
1956*2c9916cdSFrançois Tigeot 		if (__intel_ring_space(request->postfix, ringbuf->tail,
19571b13d190SFrançois Tigeot 				       ringbuf->size) >= n) {
1958e3adcf8fSFrançois Tigeot 			break;
1959e3adcf8fSFrançois Tigeot 		}
1960e3adcf8fSFrançois Tigeot 	}
1961e3adcf8fSFrançois Tigeot 
1962*2c9916cdSFrançois Tigeot 	if (&request->list == &ring->request_list)
1963e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1964e3adcf8fSFrançois Tigeot 
1965*2c9916cdSFrançois Tigeot 	ret = i915_wait_request(request);
1966e3adcf8fSFrançois Tigeot 	if (ret)
1967e3adcf8fSFrançois Tigeot 		return ret;
1968e3adcf8fSFrançois Tigeot 
1969ba55f2f5SFrançois Tigeot 	i915_gem_retire_requests_ring(ring);
1970e3adcf8fSFrançois Tigeot 
1971e3adcf8fSFrançois Tigeot 	return 0;
1972e3adcf8fSFrançois Tigeot }
1973e3adcf8fSFrançois Tigeot 
1974ba55f2f5SFrançois Tigeot static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
1975e3adcf8fSFrançois Tigeot {
1976e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1977e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1978ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1979245593daSFrançois Tigeot 	unsigned long end;
1980e3adcf8fSFrançois Tigeot 	int ret;
1981e3adcf8fSFrançois Tigeot 
1982e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_request(ring, n);
1983e3adcf8fSFrançois Tigeot 	if (ret != -ENOSPC)
1984e3adcf8fSFrançois Tigeot 		return ret;
1985e3adcf8fSFrançois Tigeot 
19869edbd4a0SFrançois Tigeot 	/* force the tail write in case we have been skipping them */
19879edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
19889edbd4a0SFrançois Tigeot 
1989e3adcf8fSFrançois Tigeot 	/* With GEM the hangcheck timer should kick us out of the loop,
1990e3adcf8fSFrançois Tigeot 	 * leaving it early runs the risk of corrupting GEM state (due
1991e3adcf8fSFrançois Tigeot 	 * to running on almost untested codepaths). But on resume
1992e3adcf8fSFrançois Tigeot 	 * timers don't work yet, so prevent a complete hang in that
1993e3adcf8fSFrançois Tigeot 	 * case by choosing an insanely large timeout. */
1994e3440f96SFrançois Tigeot 	end = jiffies + 60 * HZ;
1995245593daSFrançois Tigeot 
1996*2c9916cdSFrançois Tigeot 	ret = 0;
1997ba55f2f5SFrançois Tigeot 	trace_i915_ring_wait_begin(ring);
1998e3adcf8fSFrançois Tigeot 	do {
1999*2c9916cdSFrançois Tigeot 		if (intel_ring_space(ringbuf) >= n)
2000ba55f2f5SFrançois Tigeot 			break;
2001*2c9916cdSFrançois Tigeot 		ringbuf->head = I915_READ_HEAD(ring);
2002*2c9916cdSFrançois Tigeot 		if (intel_ring_space(ringbuf) >= n)
2003*2c9916cdSFrançois Tigeot 			break;
2004ba55f2f5SFrançois Tigeot 
2005e3440f96SFrançois Tigeot 		msleep(1);
2006245593daSFrançois Tigeot 
2007b42320c2SFrançois Tigeot 		if (dev_priv->mm.interruptible && signal_pending(curthread->td_lwp)) {
2008ba55f2f5SFrançois Tigeot 			ret = -ERESTARTSYS;
2009ba55f2f5SFrançois Tigeot 			break;
2010ba55f2f5SFrançois Tigeot 		}
2011ba55f2f5SFrançois Tigeot 
2012a2fdbec6SFrançois Tigeot 		ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2013a2fdbec6SFrançois Tigeot 					   dev_priv->mm.interruptible);
2014245593daSFrançois Tigeot 		if (ret)
2015ba55f2f5SFrançois Tigeot 			break;
2016ba55f2f5SFrançois Tigeot 
2017ba55f2f5SFrançois Tigeot 		if (time_after(jiffies, end)) {
2018ba55f2f5SFrançois Tigeot 			ret = -EBUSY;
2019ba55f2f5SFrançois Tigeot 			break;
2020ba55f2f5SFrançois Tigeot 		}
2021ba55f2f5SFrançois Tigeot 	} while (1);
2022a2fdbec6SFrançois Tigeot 	trace_i915_ring_wait_end(ring);
2023ba55f2f5SFrançois Tigeot 	return ret;
2024e3adcf8fSFrançois Tigeot }
2025e3adcf8fSFrançois Tigeot 
2026ba55f2f5SFrançois Tigeot static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
2027b030f26bSFrançois Tigeot {
2028b030f26bSFrançois Tigeot 	uint32_t __iomem *virt;
2029ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2030ba55f2f5SFrançois Tigeot 	int rem = ringbuf->size - ringbuf->tail;
2031b030f26bSFrançois Tigeot 
2032ba55f2f5SFrançois Tigeot 	if (ringbuf->space < rem) {
2033b030f26bSFrançois Tigeot 		int ret = ring_wait_for_space(ring, rem);
2034b030f26bSFrançois Tigeot 		if (ret)
2035b030f26bSFrançois Tigeot 			return ret;
2036b030f26bSFrançois Tigeot 	}
2037b030f26bSFrançois Tigeot 
2038ba55f2f5SFrançois Tigeot 	virt = (unsigned int *)((char *)ringbuf->virtual_start + ringbuf->tail);
2039b030f26bSFrançois Tigeot 	rem /= 4;
2040b030f26bSFrançois Tigeot 	while (rem--)
2041686a02f1SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
2042b030f26bSFrançois Tigeot 
2043ba55f2f5SFrançois Tigeot 	ringbuf->tail = 0;
2044*2c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
2045b030f26bSFrançois Tigeot 
2046b030f26bSFrançois Tigeot 	return 0;
2047b030f26bSFrançois Tigeot }
2048b030f26bSFrançois Tigeot 
2049ba55f2f5SFrançois Tigeot int intel_ring_idle(struct intel_engine_cs *ring)
2050b030f26bSFrançois Tigeot {
2051*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *req;
2052b5c29a34SFrançois Tigeot 	int ret;
2053b5c29a34SFrançois Tigeot 
2054b5c29a34SFrançois Tigeot 	/* We need to add any requests required to flush the objects and ring */
2055*2c9916cdSFrançois Tigeot 	if (ring->outstanding_lazy_request) {
2056*2c9916cdSFrançois Tigeot 		ret = i915_add_request(ring);
2057b5c29a34SFrançois Tigeot 		if (ret)
2058b5c29a34SFrançois Tigeot 			return ret;
2059b5c29a34SFrançois Tigeot 	}
2060b5c29a34SFrançois Tigeot 
2061b5c29a34SFrançois Tigeot 	/* Wait upon the last request to be completed */
2062b5c29a34SFrançois Tigeot 	if (list_empty(&ring->request_list))
2063b5c29a34SFrançois Tigeot 		return 0;
2064b5c29a34SFrançois Tigeot 
2065*2c9916cdSFrançois Tigeot 	req = list_entry(ring->request_list.prev,
2066b5c29a34SFrançois Tigeot 			   struct drm_i915_gem_request,
2067*2c9916cdSFrançois Tigeot 			   list);
2068b5c29a34SFrançois Tigeot 
2069*2c9916cdSFrançois Tigeot 	return i915_wait_request(req);
2070b5c29a34SFrançois Tigeot }
2071b5c29a34SFrançois Tigeot 
2072b5c29a34SFrançois Tigeot static int
2073*2c9916cdSFrançois Tigeot intel_ring_alloc_request(struct intel_engine_cs *ring)
2074b5c29a34SFrançois Tigeot {
2075*2c9916cdSFrançois Tigeot 	int ret;
2076*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *request;
2077*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_private = ring->dev->dev_private;
2078*2c9916cdSFrançois Tigeot 
2079*2c9916cdSFrançois Tigeot 	if (ring->outstanding_lazy_request)
2080b5c29a34SFrançois Tigeot 		return 0;
2081b5c29a34SFrançois Tigeot 
2082*2c9916cdSFrançois Tigeot 	request = kzalloc(sizeof(*request), GFP_KERNEL);
20839edbd4a0SFrançois Tigeot 	if (request == NULL)
20849edbd4a0SFrançois Tigeot 		return -ENOMEM;
20859edbd4a0SFrançois Tigeot 
2086*2c9916cdSFrançois Tigeot 	kref_init(&request->ref);
2087*2c9916cdSFrançois Tigeot 	request->ring = ring;
2088*2c9916cdSFrançois Tigeot 	request->uniq = dev_private->request_uniq++;
2089*2c9916cdSFrançois Tigeot 
2090*2c9916cdSFrançois Tigeot 	ret = i915_gem_get_seqno(ring->dev, &request->seqno);
2091*2c9916cdSFrançois Tigeot 	if (ret) {
2092*2c9916cdSFrançois Tigeot 		kfree(request);
2093*2c9916cdSFrançois Tigeot 		return ret;
2094b030f26bSFrançois Tigeot 	}
2095b030f26bSFrançois Tigeot 
2096*2c9916cdSFrançois Tigeot 	ring->outstanding_lazy_request = request;
2097*2c9916cdSFrançois Tigeot 	return 0;
20989edbd4a0SFrançois Tigeot }
20999edbd4a0SFrançois Tigeot 
2100ba55f2f5SFrançois Tigeot static int __intel_ring_prepare(struct intel_engine_cs *ring,
2101a2fdbec6SFrançois Tigeot 				int bytes)
2102a2fdbec6SFrançois Tigeot {
2103ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2104a2fdbec6SFrançois Tigeot 	int ret;
2105a2fdbec6SFrançois Tigeot 
2106ba55f2f5SFrançois Tigeot 	if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
2107a2fdbec6SFrançois Tigeot 		ret = intel_wrap_ring_buffer(ring);
2108a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
2109a2fdbec6SFrançois Tigeot 			return ret;
2110a2fdbec6SFrançois Tigeot 	}
2111a2fdbec6SFrançois Tigeot 
2112ba55f2f5SFrançois Tigeot 	if (unlikely(ringbuf->space < bytes)) {
2113a2fdbec6SFrançois Tigeot 		ret = ring_wait_for_space(ring, bytes);
2114a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
2115a2fdbec6SFrançois Tigeot 			return ret;
2116a2fdbec6SFrançois Tigeot 	}
2117a2fdbec6SFrançois Tigeot 
2118a2fdbec6SFrançois Tigeot 	return 0;
2119a2fdbec6SFrançois Tigeot }
2120a2fdbec6SFrançois Tigeot 
2121ba55f2f5SFrançois Tigeot int intel_ring_begin(struct intel_engine_cs *ring,
2122e3adcf8fSFrançois Tigeot 		     int num_dwords)
2123e3adcf8fSFrançois Tigeot {
2124ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2125e3adcf8fSFrançois Tigeot 	int ret;
2126e3adcf8fSFrançois Tigeot 
2127a2fdbec6SFrançois Tigeot 	ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2128a2fdbec6SFrançois Tigeot 				   dev_priv->mm.interruptible);
2129245593daSFrançois Tigeot 	if (ret)
2130245593daSFrançois Tigeot 		return ret;
2131e3adcf8fSFrançois Tigeot 
21329edbd4a0SFrançois Tigeot 	ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
21339edbd4a0SFrançois Tigeot 	if (ret)
21349edbd4a0SFrançois Tigeot 		return ret;
21359edbd4a0SFrançois Tigeot 
2136b5c29a34SFrançois Tigeot 	/* Preallocate the olr before touching the ring */
2137*2c9916cdSFrançois Tigeot 	ret = intel_ring_alloc_request(ring);
2138b5c29a34SFrançois Tigeot 	if (ret)
2139b5c29a34SFrançois Tigeot 		return ret;
2140b5c29a34SFrançois Tigeot 
2141ba55f2f5SFrançois Tigeot 	ring->buffer->space -= num_dwords * sizeof(uint32_t);
21429edbd4a0SFrançois Tigeot 	return 0;
21439edbd4a0SFrançois Tigeot }
21449edbd4a0SFrançois Tigeot 
21459edbd4a0SFrançois Tigeot /* Align the ring tail to a cacheline boundary */
2146ba55f2f5SFrançois Tigeot int intel_ring_cacheline_align(struct intel_engine_cs *ring)
21479edbd4a0SFrançois Tigeot {
2148ba55f2f5SFrançois Tigeot 	int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
21499edbd4a0SFrançois Tigeot 	int ret;
21509edbd4a0SFrançois Tigeot 
21519edbd4a0SFrançois Tigeot 	if (num_dwords == 0)
21529edbd4a0SFrançois Tigeot 		return 0;
21539edbd4a0SFrançois Tigeot 
2154ba55f2f5SFrançois Tigeot 	num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
21559edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, num_dwords);
21569edbd4a0SFrançois Tigeot 	if (ret)
21579edbd4a0SFrançois Tigeot 		return ret;
21589edbd4a0SFrançois Tigeot 
21599edbd4a0SFrançois Tigeot 	while (num_dwords--)
21609edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
21619edbd4a0SFrançois Tigeot 
21629edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
21639edbd4a0SFrançois Tigeot 
21649edbd4a0SFrançois Tigeot 	return 0;
2165e3adcf8fSFrançois Tigeot }
2166e3adcf8fSFrançois Tigeot 
2167ba55f2f5SFrançois Tigeot void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
2168a2fdbec6SFrançois Tigeot {
216924edb884SFrançois Tigeot 	struct drm_device *dev = ring->dev;
217024edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2171a2fdbec6SFrançois Tigeot 
2172*2c9916cdSFrançois Tigeot 	BUG_ON(ring->outstanding_lazy_request);
2173a2fdbec6SFrançois Tigeot 
217424edb884SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
2175a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2176a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
217724edb884SFrançois Tigeot 		if (HAS_VEBOX(dev))
21789edbd4a0SFrançois Tigeot 			I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
2179e3adcf8fSFrançois Tigeot 	}
2180e3adcf8fSFrançois Tigeot 
2181a2fdbec6SFrançois Tigeot 	ring->set_seqno(ring, seqno);
21825d0b1887SFrançois Tigeot 	ring->hangcheck.seqno = seqno;
2183e3adcf8fSFrançois Tigeot }
2184e3adcf8fSFrançois Tigeot 
2185ba55f2f5SFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
2186f4e1c372SFrançois Tigeot 				     u32 value)
2187e3adcf8fSFrançois Tigeot {
2188ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2189e3adcf8fSFrançois Tigeot 
2190e3adcf8fSFrançois Tigeot        /* Every tail move must follow the sequence below */
2191f4e1c372SFrançois Tigeot 
2192f4e1c372SFrançois Tigeot 	/* Disable notification that the ring is IDLE. The GT
2193f4e1c372SFrançois Tigeot 	 * will then assume that it is busy and bring it out of rc6.
2194f4e1c372SFrançois Tigeot 	 */
2195e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2196f4e1c372SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2197e3adcf8fSFrançois Tigeot 
2198f4e1c372SFrançois Tigeot 	/* Clear the context id. Here be magic! */
2199f4e1c372SFrançois Tigeot 	I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2200e3adcf8fSFrançois Tigeot 
2201f4e1c372SFrançois Tigeot 	/* Wait for the ring not to be idle, i.e. for it to wake up. */
2202f4e1c372SFrançois Tigeot 	if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
2203f4e1c372SFrançois Tigeot 		      GEN6_BSD_SLEEP_INDICATOR) == 0,
2204f4e1c372SFrançois Tigeot 		     50))
2205f4e1c372SFrançois Tigeot 		DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
2206f4e1c372SFrançois Tigeot 
2207f4e1c372SFrançois Tigeot 	/* Now that the ring is fully powered up, update the tail */
2208e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
2209f4e1c372SFrançois Tigeot 	POSTING_READ(RING_TAIL(ring->mmio_base));
2210f4e1c372SFrançois Tigeot 
2211f4e1c372SFrançois Tigeot 	/* Let the ring send IDLE messages to the GT again,
2212f4e1c372SFrançois Tigeot 	 * and so let it sleep to conserve power when idle.
2213f4e1c372SFrançois Tigeot 	 */
2214e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2215f4e1c372SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2216e3adcf8fSFrançois Tigeot }
2217e3adcf8fSFrançois Tigeot 
2218ba55f2f5SFrançois Tigeot static int gen6_bsd_ring_flush(struct intel_engine_cs *ring,
2219b5c29a34SFrançois Tigeot 			       u32 invalidate, u32 flush)
2220e3adcf8fSFrançois Tigeot {
2221e3adcf8fSFrançois Tigeot 	uint32_t cmd;
2222e3adcf8fSFrançois Tigeot 	int ret;
2223e3adcf8fSFrançois Tigeot 
2224e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
2225e3adcf8fSFrançois Tigeot 	if (ret)
2226e3adcf8fSFrançois Tigeot 		return ret;
2227e3adcf8fSFrançois Tigeot 
2228e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
22299edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
22309edbd4a0SFrançois Tigeot 		cmd += 1;
2231*2c9916cdSFrançois Tigeot 
2232*2c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
2233*2c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
2234*2c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
2235*2c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
2236*2c9916cdSFrançois Tigeot 	 */
2237*2c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2238*2c9916cdSFrançois Tigeot 
2239b5c29a34SFrançois Tigeot 	/*
2240b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.5 - video engine command streamer:
2241b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
2242b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
2243b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
2244b5c29a34SFrançois Tigeot 	 */
2245e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
2246*2c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2247*2c9916cdSFrançois Tigeot 
2248e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
2249b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
22509edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
22519edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
22529edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
22539edbd4a0SFrançois Tigeot 	} else  {
22549edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0);
22559edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
22569edbd4a0SFrançois Tigeot 	}
22579edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
22589edbd4a0SFrançois Tigeot 	return 0;
22599edbd4a0SFrançois Tigeot }
22609edbd4a0SFrançois Tigeot 
22619edbd4a0SFrançois Tigeot static int
2262ba55f2f5SFrançois Tigeot gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
2263ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
22649edbd4a0SFrançois Tigeot 			      unsigned flags)
22659edbd4a0SFrançois Tigeot {
22661b13d190SFrançois Tigeot 	bool ppgtt = USES_PPGTT(ring->dev) && !(flags & I915_DISPATCH_SECURE);
22679edbd4a0SFrançois Tigeot 	int ret;
22689edbd4a0SFrançois Tigeot 
22699edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
22709edbd4a0SFrançois Tigeot 	if (ret)
22719edbd4a0SFrançois Tigeot 		return ret;
22729edbd4a0SFrançois Tigeot 
22739edbd4a0SFrançois Tigeot 	/* FIXME(BDW): Address space and security selectors. */
22749edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
2275ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, lower_32_bits(offset));
2276ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, upper_32_bits(offset));
2277e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
2278e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
22799edbd4a0SFrançois Tigeot 
2280e3adcf8fSFrançois Tigeot 	return 0;
2281e3adcf8fSFrançois Tigeot }
2282e3adcf8fSFrançois Tigeot 
2283e3adcf8fSFrançois Tigeot static int
2284ba55f2f5SFrançois Tigeot hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
2285ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
2286b5c29a34SFrançois Tigeot 			      unsigned flags)
2287e3adcf8fSFrançois Tigeot {
2288e3adcf8fSFrançois Tigeot 	int ret;
2289e3adcf8fSFrançois Tigeot 
2290e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
2291e3adcf8fSFrançois Tigeot 	if (ret)
2292e3adcf8fSFrançois Tigeot 		return ret;
2293e3adcf8fSFrançois Tigeot 
2294b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
22951b13d190SFrançois Tigeot 			MI_BATCH_BUFFER_START |
22961b13d190SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ?
22971b13d190SFrançois Tigeot 			 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW));
2298b5c29a34SFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
2299b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset);
2300b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
2301b5c29a34SFrançois Tigeot 
2302b5c29a34SFrançois Tigeot 	return 0;
2303b5c29a34SFrançois Tigeot }
2304b5c29a34SFrançois Tigeot 
2305b5c29a34SFrançois Tigeot static int
2306ba55f2f5SFrançois Tigeot gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
2307ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
2308b5c29a34SFrançois Tigeot 			      unsigned flags)
2309b5c29a34SFrançois Tigeot {
2310b5c29a34SFrançois Tigeot 	int ret;
2311b5c29a34SFrançois Tigeot 
2312b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
2313b5c29a34SFrançois Tigeot 	if (ret)
2314b5c29a34SFrançois Tigeot 		return ret;
2315b5c29a34SFrançois Tigeot 
2316b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
2317b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START |
2318b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
2319e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
2320e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
2321e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
2322e3adcf8fSFrançois Tigeot 
2323e3adcf8fSFrançois Tigeot 	return 0;
2324e3adcf8fSFrançois Tigeot }
2325e3adcf8fSFrançois Tigeot 
2326e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
2327e3adcf8fSFrançois Tigeot 
2328ba55f2f5SFrançois Tigeot static int gen6_ring_flush(struct intel_engine_cs *ring,
2329b5c29a34SFrançois Tigeot 			   u32 invalidate, u32 flush)
2330e3adcf8fSFrançois Tigeot {
23315d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
2332*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2333e3adcf8fSFrançois Tigeot 	uint32_t cmd;
2334e3adcf8fSFrançois Tigeot 	int ret;
2335e3adcf8fSFrançois Tigeot 
2336e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
2337e3adcf8fSFrançois Tigeot 	if (ret)
2338e3adcf8fSFrançois Tigeot 		return ret;
2339e3adcf8fSFrançois Tigeot 
2340e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
23419edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
23429edbd4a0SFrançois Tigeot 		cmd += 1;
2343*2c9916cdSFrançois Tigeot 
2344*2c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
2345*2c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
2346*2c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
2347*2c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
2348*2c9916cdSFrançois Tigeot 	 */
2349*2c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2350*2c9916cdSFrançois Tigeot 
2351b5c29a34SFrançois Tigeot 	/*
2352b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.3 - blitter engine command streamer:
2353b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
2354b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
2355b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
2356b5c29a34SFrançois Tigeot 	 */
2357e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
2358*2c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB;
2359e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
2360b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
23619edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
23629edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
23639edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
23649edbd4a0SFrançois Tigeot 	} else  {
2365e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, 0);
2366e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
23679edbd4a0SFrançois Tigeot 	}
2368e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
23695d0b1887SFrançois Tigeot 
2370*2c9916cdSFrançois Tigeot 	if (!invalidate && flush) {
2371*2c9916cdSFrançois Tigeot 		if (IS_GEN7(dev))
23725d0b1887SFrançois Tigeot 			return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
2373*2c9916cdSFrançois Tigeot 		else if (IS_BROADWELL(dev))
2374*2c9916cdSFrançois Tigeot 			dev_priv->fbc.need_sw_cache_clean = true;
2375*2c9916cdSFrançois Tigeot 	}
23765d0b1887SFrançois Tigeot 
2377e3adcf8fSFrançois Tigeot 	return 0;
2378e3adcf8fSFrançois Tigeot }
2379e3adcf8fSFrançois Tigeot 
2380e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
2381e3adcf8fSFrançois Tigeot {
2382ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2383ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
238424edb884SFrançois Tigeot 	struct drm_i915_gem_object *obj;
238524edb884SFrançois Tigeot 	int ret;
2386e3adcf8fSFrançois Tigeot 
2387686a02f1SFrançois Tigeot 	ring->name = "render ring";
2388686a02f1SFrançois Tigeot 	ring->id = RCS;
2389686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
2390686a02f1SFrançois Tigeot 
239124edb884SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
239224edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
239324edb884SFrançois Tigeot 			obj = i915_gem_alloc_object(dev, 4096);
239424edb884SFrançois Tigeot 			if (obj == NULL) {
239524edb884SFrançois Tigeot 				DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
239624edb884SFrançois Tigeot 				i915.semaphores = 0;
239724edb884SFrançois Tigeot 			} else {
239824edb884SFrançois Tigeot 				i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
239924edb884SFrançois Tigeot 				ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
240024edb884SFrançois Tigeot 				if (ret != 0) {
240124edb884SFrançois Tigeot 					drm_gem_object_unreference(&obj->base);
240224edb884SFrançois Tigeot 					DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
240324edb884SFrançois Tigeot 					i915.semaphores = 0;
240424edb884SFrançois Tigeot 				} else
240524edb884SFrançois Tigeot 					dev_priv->semaphore_obj = obj;
240624edb884SFrançois Tigeot 			}
240724edb884SFrançois Tigeot 		}
2408*2c9916cdSFrançois Tigeot 
2409*2c9916cdSFrançois Tigeot 		ring->init_context = intel_rcs_ctx_init;
241024edb884SFrançois Tigeot 		ring->add_request = gen6_add_request;
241124edb884SFrançois Tigeot 		ring->flush = gen8_render_ring_flush;
241224edb884SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
241324edb884SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
241424edb884SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
241524edb884SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
241624edb884SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
241724edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
241824edb884SFrançois Tigeot 			WARN_ON(!dev_priv->semaphore_obj);
241924edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
242024edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_rcs_signal;
242124edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
242224edb884SFrançois Tigeot 		}
242324edb884SFrançois Tigeot 	} else if (INTEL_INFO(dev)->gen >= 6) {
2424e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
2425b5c29a34SFrançois Tigeot 		ring->flush = gen7_render_ring_flush;
2426b5c29a34SFrançois Tigeot 		if (INTEL_INFO(dev)->gen == 6)
2427e3adcf8fSFrançois Tigeot 			ring->flush = gen6_render_ring_flush;
2428686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2429686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
24305d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2431e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2432a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
243324edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2434ba55f2f5SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2435ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
2436ba55f2f5SFrançois Tigeot 			/*
243724edb884SFrançois Tigeot 			 * The current semaphore is only applied on pre-gen8
243824edb884SFrançois Tigeot 			 * platform.  And there is no VCS2 ring on the pre-gen8
243924edb884SFrançois Tigeot 			 * platform. So the semaphore between RCS and VCS2 is
244024edb884SFrançois Tigeot 			 * initialized as INVALID.  Gen8 will initialize the
244124edb884SFrançois Tigeot 			 * sema between VCS2 and RCS later.
2442ba55f2f5SFrançois Tigeot 			 */
2443ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2444ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2445ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2446ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2447ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2448ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2449ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2450ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2451ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2452ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
245324edb884SFrançois Tigeot 		}
2454e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
2455e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
2456686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
2457e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
2458a2fdbec6SFrançois Tigeot 		ring->set_seqno = pc_render_set_seqno;
2459686a02f1SFrançois Tigeot 		ring->irq_get = gen5_ring_get_irq;
2460686a02f1SFrançois Tigeot 		ring->irq_put = gen5_ring_put_irq;
24615d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
24625d0b1887SFrançois Tigeot 					GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
2463686a02f1SFrançois Tigeot 	} else {
2464686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2465686a02f1SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 4)
2466686a02f1SFrançois Tigeot 			ring->flush = gen2_render_ring_flush;
2467686a02f1SFrançois Tigeot 		else
2468686a02f1SFrançois Tigeot 			ring->flush = gen4_render_ring_flush;
2469686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2470a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2471686a02f1SFrançois Tigeot 		if (IS_GEN2(dev)) {
2472686a02f1SFrançois Tigeot 			ring->irq_get = i8xx_ring_get_irq;
2473686a02f1SFrançois Tigeot 			ring->irq_put = i8xx_ring_put_irq;
2474686a02f1SFrançois Tigeot 		} else {
2475686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2476686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2477e3adcf8fSFrançois Tigeot 		}
2478686a02f1SFrançois Tigeot 		ring->irq_enable_mask = I915_USER_INTERRUPT;
2479686a02f1SFrançois Tigeot 	}
2480686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
248124edb884SFrançois Tigeot 
2482b5c29a34SFrançois Tigeot 	if (IS_HASWELL(dev))
2483b5c29a34SFrançois Tigeot 		ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
24849edbd4a0SFrançois Tigeot 	else if (IS_GEN8(dev))
24859edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2486b5c29a34SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 6)
2487686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2488686a02f1SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 4)
2489686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2490686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
2491686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2492686a02f1SFrançois Tigeot 	else
2493686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
2494*2c9916cdSFrançois Tigeot 	ring->init_hw = init_render_ring;
2495686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
2496e3adcf8fSFrançois Tigeot 
2497b5c29a34SFrançois Tigeot 	/* Workaround batchbuffer to combat CS tlb bug. */
2498b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev)) {
249924edb884SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
2500b5c29a34SFrançois Tigeot 		if (obj == NULL) {
2501b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to allocate batch bo\n");
2502b5c29a34SFrançois Tigeot 			return -ENOMEM;
2503b5c29a34SFrançois Tigeot 		}
2504b5c29a34SFrançois Tigeot 
2505ba55f2f5SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
2506b5c29a34SFrançois Tigeot 		if (ret != 0) {
2507b5c29a34SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
2508b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to ping batch bo\n");
2509b5c29a34SFrançois Tigeot 			return ret;
2510b5c29a34SFrançois Tigeot 		}
2511b5c29a34SFrançois Tigeot 
25129edbd4a0SFrançois Tigeot 		ring->scratch.obj = obj;
25139edbd4a0SFrançois Tigeot 		ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
2514e3adcf8fSFrançois Tigeot 	}
2515e3adcf8fSFrançois Tigeot 
2516*2c9916cdSFrançois Tigeot 	ret = intel_init_ring_buffer(dev, ring);
2517b5c29a34SFrançois Tigeot 	if (ret)
2518*2c9916cdSFrançois Tigeot 		return ret;
2519*2c9916cdSFrançois Tigeot 
2520*2c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
2521*2c9916cdSFrançois Tigeot 		ret = intel_init_pipe_control(ring);
2522*2c9916cdSFrançois Tigeot 		if (ret)
2523*2c9916cdSFrançois Tigeot 			return ret;
2524b5c29a34SFrançois Tigeot 	}
2525b5c29a34SFrançois Tigeot 
2526e3adcf8fSFrançois Tigeot 	return 0;
2527e3adcf8fSFrançois Tigeot }
2528e3adcf8fSFrançois Tigeot 
2529e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
2530e3adcf8fSFrançois Tigeot {
2531ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2532ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS];
2533e3adcf8fSFrançois Tigeot 
2534686a02f1SFrançois Tigeot 	ring->name = "bsd ring";
2535686a02f1SFrançois Tigeot 	ring->id = VCS;
2536686a02f1SFrançois Tigeot 
2537686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
25389edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
2539686a02f1SFrançois Tigeot 		ring->mmio_base = GEN6_BSD_RING_BASE;
2540686a02f1SFrançois Tigeot 		/* gen6 bsd needs a special wa for tail updates */
2541686a02f1SFrançois Tigeot 		if (IS_GEN6(dev))
2542686a02f1SFrançois Tigeot 			ring->write_tail = gen6_bsd_ring_write_tail;
25435d0b1887SFrançois Tigeot 		ring->flush = gen6_bsd_ring_flush;
2544686a02f1SFrançois Tigeot 		ring->add_request = gen6_add_request;
2545686a02f1SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2546a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
25479edbd4a0SFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 8) {
25489edbd4a0SFrançois Tigeot 			ring->irq_enable_mask =
25499edbd4a0SFrançois Tigeot 				GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
25509edbd4a0SFrançois Tigeot 			ring->irq_get = gen8_ring_get_irq;
25519edbd4a0SFrançois Tigeot 			ring->irq_put = gen8_ring_put_irq;
25529edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
25539edbd4a0SFrançois Tigeot 				gen8_ring_dispatch_execbuffer;
255424edb884SFrançois Tigeot 			if (i915_semaphore_is_enabled(dev)) {
255524edb884SFrançois Tigeot 				ring->semaphore.sync_to = gen8_ring_sync;
255624edb884SFrançois Tigeot 				ring->semaphore.signal = gen8_xcs_signal;
255724edb884SFrançois Tigeot 				GEN8_RING_SEMAPHORE_INIT;
255824edb884SFrançois Tigeot 			}
25599edbd4a0SFrançois Tigeot 		} else {
25605d0b1887SFrançois Tigeot 			ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2561686a02f1SFrançois Tigeot 			ring->irq_get = gen6_ring_get_irq;
2562686a02f1SFrançois Tigeot 			ring->irq_put = gen6_ring_put_irq;
25639edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
25649edbd4a0SFrançois Tigeot 				gen6_ring_dispatch_execbuffer;
256524edb884SFrançois Tigeot 			if (i915_semaphore_is_enabled(dev)) {
2566ba55f2f5SFrançois Tigeot 				ring->semaphore.sync_to = gen6_ring_sync;
2567ba55f2f5SFrançois Tigeot 				ring->semaphore.signal = gen6_signal;
2568ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2569ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2570ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2571ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2572ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2573ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2574ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2575ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2576ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2577ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
257824edb884SFrançois Tigeot 			}
257924edb884SFrançois Tigeot 		}
2580686a02f1SFrançois Tigeot 	} else {
2581686a02f1SFrançois Tigeot 		ring->mmio_base = BSD_RING_BASE;
2582686a02f1SFrançois Tigeot 		ring->flush = bsd_ring_flush;
2583686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2584686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2585a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2586686a02f1SFrançois Tigeot 		if (IS_GEN5(dev)) {
25875d0b1887SFrançois Tigeot 			ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2588686a02f1SFrançois Tigeot 			ring->irq_get = gen5_ring_get_irq;
2589686a02f1SFrançois Tigeot 			ring->irq_put = gen5_ring_put_irq;
2590686a02f1SFrançois Tigeot 		} else {
2591686a02f1SFrançois Tigeot 			ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2592686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2593686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2594686a02f1SFrançois Tigeot 		}
2595686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2596686a02f1SFrançois Tigeot 	}
2597*2c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2598e3adcf8fSFrançois Tigeot 
2599e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2600e3adcf8fSFrançois Tigeot }
2601e3adcf8fSFrançois Tigeot 
2602ba55f2f5SFrançois Tigeot /**
2603ba55f2f5SFrançois Tigeot  * Initialize the second BSD ring for Broadwell GT3.
2604ba55f2f5SFrançois Tigeot  * It is noted that this only exists on Broadwell GT3.
2605ba55f2f5SFrançois Tigeot  */
2606ba55f2f5SFrançois Tigeot int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2607ba55f2f5SFrançois Tigeot {
2608ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2609ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
2610ba55f2f5SFrançois Tigeot 
2611ba55f2f5SFrançois Tigeot 	if ((INTEL_INFO(dev)->gen != 8)) {
2612ba55f2f5SFrançois Tigeot 		DRM_ERROR("No dual-BSD ring on non-BDW machine\n");
2613ba55f2f5SFrançois Tigeot 		return -EINVAL;
2614ba55f2f5SFrançois Tigeot 	}
2615ba55f2f5SFrançois Tigeot 
261624edb884SFrançois Tigeot 	ring->name = "bsd2 ring";
2617ba55f2f5SFrançois Tigeot 	ring->id = VCS2;
2618ba55f2f5SFrançois Tigeot 
2619ba55f2f5SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2620ba55f2f5SFrançois Tigeot 	ring->mmio_base = GEN8_BSD2_RING_BASE;
2621ba55f2f5SFrançois Tigeot 	ring->flush = gen6_bsd_ring_flush;
2622ba55f2f5SFrançois Tigeot 	ring->add_request = gen6_add_request;
2623ba55f2f5SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2624ba55f2f5SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
2625ba55f2f5SFrançois Tigeot 	ring->irq_enable_mask =
2626ba55f2f5SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2627ba55f2f5SFrançois Tigeot 	ring->irq_get = gen8_ring_get_irq;
2628ba55f2f5SFrançois Tigeot 	ring->irq_put = gen8_ring_put_irq;
2629ba55f2f5SFrançois Tigeot 	ring->dispatch_execbuffer =
2630ba55f2f5SFrançois Tigeot 			gen8_ring_dispatch_execbuffer;
263124edb884SFrançois Tigeot 	if (i915_semaphore_is_enabled(dev)) {
263224edb884SFrançois Tigeot 		ring->semaphore.sync_to = gen8_ring_sync;
263324edb884SFrançois Tigeot 		ring->semaphore.signal = gen8_xcs_signal;
263424edb884SFrançois Tigeot 		GEN8_RING_SEMAPHORE_INIT;
263524edb884SFrançois Tigeot 	}
2636*2c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2637ba55f2f5SFrançois Tigeot 
2638ba55f2f5SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2639ba55f2f5SFrançois Tigeot }
2640ba55f2f5SFrançois Tigeot 
2641e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
2642e3adcf8fSFrançois Tigeot {
2643ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2644ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[BCS];
2645e3adcf8fSFrançois Tigeot 
2646686a02f1SFrançois Tigeot 	ring->name = "blitter ring";
2647686a02f1SFrançois Tigeot 	ring->id = BCS;
2648686a02f1SFrançois Tigeot 
2649686a02f1SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
2650686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
26515d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
2652686a02f1SFrançois Tigeot 	ring->add_request = gen6_add_request;
2653686a02f1SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2654a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
26559edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
26569edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
26579edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
26589edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
26599edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
26609edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
266124edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
266224edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
266324edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_xcs_signal;
266424edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
266524edb884SFrançois Tigeot 		}
26669edbd4a0SFrançois Tigeot 	} else {
26675d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2668686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2669686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
2670686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
267124edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2672ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
267324edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2674ba55f2f5SFrançois Tigeot 			/*
267524edb884SFrançois Tigeot 			 * The current semaphore is only applied on pre-gen8
267624edb884SFrançois Tigeot 			 * platform.  And there is no VCS2 ring on the pre-gen8
267724edb884SFrançois Tigeot 			 * platform. So the semaphore between BCS and VCS2 is
267824edb884SFrançois Tigeot 			 * initialized as INVALID.  Gen8 will initialize the
267924edb884SFrançois Tigeot 			 * sema between BCS and VCS2 later.
2680ba55f2f5SFrançois Tigeot 			 */
2681ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2682ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2683ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2684ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2685ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2686ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2687ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2688ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2689ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2690ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
269124edb884SFrançois Tigeot 		}
269224edb884SFrançois Tigeot 	}
2693*2c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
26945d0b1887SFrançois Tigeot 
26955d0b1887SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
26965d0b1887SFrançois Tigeot }
26975d0b1887SFrançois Tigeot 
26985d0b1887SFrançois Tigeot int intel_init_vebox_ring_buffer(struct drm_device *dev)
26995d0b1887SFrançois Tigeot {
2700ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2701ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VECS];
27025d0b1887SFrançois Tigeot 
27035d0b1887SFrançois Tigeot 	ring->name = "video enhancement ring";
27045d0b1887SFrançois Tigeot 	ring->id = VECS;
27055d0b1887SFrançois Tigeot 
27065d0b1887SFrançois Tigeot 	ring->mmio_base = VEBOX_RING_BASE;
27075d0b1887SFrançois Tigeot 	ring->write_tail = ring_write_tail;
27085d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
27095d0b1887SFrançois Tigeot 	ring->add_request = gen6_add_request;
27105d0b1887SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
27115d0b1887SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
27129edbd4a0SFrançois Tigeot 
27139edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
27149edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
27159edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
27169edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
27179edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
27189edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
271924edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
272024edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
272124edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_xcs_signal;
272224edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
272324edb884SFrançois Tigeot 		}
27249edbd4a0SFrançois Tigeot 	} else {
27259edbd4a0SFrançois Tigeot 		ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
27265d0b1887SFrançois Tigeot 		ring->irq_get = hsw_vebox_get_irq;
27275d0b1887SFrançois Tigeot 		ring->irq_put = hsw_vebox_put_irq;
27285d0b1887SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
272924edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2730ba55f2f5SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2731ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
2732ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2733ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2734ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2735ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2736ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2737ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2738ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2739ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2740ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2741ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
274224edb884SFrançois Tigeot 		}
274324edb884SFrançois Tigeot 	}
2744*2c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2745e3adcf8fSFrançois Tigeot 
2746e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2747e3adcf8fSFrançois Tigeot }
2748b030f26bSFrançois Tigeot 
2749b030f26bSFrançois Tigeot int
2750ba55f2f5SFrançois Tigeot intel_ring_flush_all_caches(struct intel_engine_cs *ring)
2751b030f26bSFrançois Tigeot {
2752b030f26bSFrançois Tigeot 	int ret;
2753b030f26bSFrançois Tigeot 
2754b030f26bSFrançois Tigeot 	if (!ring->gpu_caches_dirty)
2755b030f26bSFrançois Tigeot 		return 0;
2756b030f26bSFrançois Tigeot 
2757b030f26bSFrançois Tigeot 	ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2758b030f26bSFrançois Tigeot 	if (ret)
2759b030f26bSFrançois Tigeot 		return ret;
2760b030f26bSFrançois Tigeot 
2761a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2762a2fdbec6SFrançois Tigeot 
2763b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2764b030f26bSFrançois Tigeot 	return 0;
2765b030f26bSFrançois Tigeot }
2766b030f26bSFrançois Tigeot 
2767b030f26bSFrançois Tigeot int
2768ba55f2f5SFrançois Tigeot intel_ring_invalidate_all_caches(struct intel_engine_cs *ring)
2769b030f26bSFrançois Tigeot {
2770b030f26bSFrançois Tigeot 	uint32_t flush_domains;
2771b030f26bSFrançois Tigeot 	int ret;
2772b030f26bSFrançois Tigeot 
2773b030f26bSFrançois Tigeot 	flush_domains = 0;
2774b030f26bSFrançois Tigeot 	if (ring->gpu_caches_dirty)
2775b030f26bSFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
2776b030f26bSFrançois Tigeot 
2777b030f26bSFrançois Tigeot 	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2778b030f26bSFrançois Tigeot 	if (ret)
2779b030f26bSFrançois Tigeot 		return ret;
2780b030f26bSFrançois Tigeot 
2781a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2782a2fdbec6SFrançois Tigeot 
2783b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2784b030f26bSFrançois Tigeot 	return 0;
2785b030f26bSFrançois Tigeot }
2786ba55f2f5SFrançois Tigeot 
2787ba55f2f5SFrançois Tigeot void
2788ba55f2f5SFrançois Tigeot intel_stop_ring_buffer(struct intel_engine_cs *ring)
2789ba55f2f5SFrançois Tigeot {
2790ba55f2f5SFrançois Tigeot 	int ret;
2791ba55f2f5SFrançois Tigeot 
2792ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
2793ba55f2f5SFrançois Tigeot 		return;
2794ba55f2f5SFrançois Tigeot 
2795ba55f2f5SFrançois Tigeot 	ret = intel_ring_idle(ring);
2796ba55f2f5SFrançois Tigeot 	if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2797ba55f2f5SFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2798ba55f2f5SFrançois Tigeot 			  ring->name, ret);
2799ba55f2f5SFrançois Tigeot 
2800ba55f2f5SFrançois Tigeot 	stop_ring(ring);
2801ba55f2f5SFrançois Tigeot }
2802