xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision 0f3709757bcd92b057b45f4738aebb03d2063428)
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 
36ba55f2f5SFrançois Tigeot /* Early gen2 devices have a cacheline of just 32 bytes, using 64 is overkill,
37ba55f2f5SFrançois Tigeot  * but keeps the logic simple. Indeed, the whole purpose of this macro is just
38ba55f2f5SFrançois Tigeot  * to give some inclination as to some of the magic values used in the various
39ba55f2f5SFrançois Tigeot  * workarounds!
40ba55f2f5SFrançois Tigeot  */
41ba55f2f5SFrançois Tigeot #define CACHELINE_BYTES 64
42ba55f2f5SFrançois Tigeot 
43ba55f2f5SFrançois Tigeot static inline int __ring_space(int head, int tail, int size)
44e3adcf8fSFrançois Tigeot {
45ba55f2f5SFrançois Tigeot 	int space = head - (tail + I915_RING_FREE_SPACE);
46e3adcf8fSFrançois Tigeot 	if (space < 0)
47ba55f2f5SFrançois Tigeot 		space += size;
48e3adcf8fSFrançois Tigeot 	return space;
49e3adcf8fSFrançois Tigeot }
50e3adcf8fSFrançois Tigeot 
51ba55f2f5SFrançois Tigeot static inline int ring_space(struct intel_engine_cs *ring)
52ba55f2f5SFrançois Tigeot {
53ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
54ba55f2f5SFrançois Tigeot 	return __ring_space(ringbuf->head & HEAD_ADDR, ringbuf->tail, ringbuf->size);
55ba55f2f5SFrançois Tigeot }
56ba55f2f5SFrançois Tigeot 
57ba55f2f5SFrançois Tigeot static bool intel_ring_stopped(struct intel_engine_cs *ring)
589edbd4a0SFrançois Tigeot {
599edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
60ba55f2f5SFrançois Tigeot 	return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
61ba55f2f5SFrançois Tigeot }
629edbd4a0SFrançois Tigeot 
63ba55f2f5SFrançois Tigeot void __intel_ring_advance(struct intel_engine_cs *ring)
64ba55f2f5SFrançois Tigeot {
65ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
66ba55f2f5SFrançois Tigeot 	ringbuf->tail &= ringbuf->size - 1;
67ba55f2f5SFrançois Tigeot 	if (intel_ring_stopped(ring))
689edbd4a0SFrançois Tigeot 		return;
69ba55f2f5SFrançois Tigeot 	ring->write_tail(ring, ringbuf->tail);
709edbd4a0SFrançois Tigeot }
719edbd4a0SFrançois Tigeot 
72e3adcf8fSFrançois Tigeot static int
73ba55f2f5SFrançois Tigeot gen2_render_ring_flush(struct intel_engine_cs *ring,
74686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
75686a02f1SFrançois Tigeot 		       u32	flush_domains)
76686a02f1SFrançois Tigeot {
77686a02f1SFrançois Tigeot 	u32 cmd;
78686a02f1SFrançois Tigeot 	int ret;
79686a02f1SFrançois Tigeot 
80686a02f1SFrançois Tigeot 	cmd = MI_FLUSH;
81686a02f1SFrançois Tigeot 	if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
82686a02f1SFrançois Tigeot 		cmd |= MI_NO_WRITE_FLUSH;
83686a02f1SFrançois Tigeot 
84686a02f1SFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
85686a02f1SFrançois Tigeot 		cmd |= MI_READ_FLUSH;
86686a02f1SFrançois Tigeot 
87686a02f1SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
88686a02f1SFrançois Tigeot 	if (ret)
89686a02f1SFrançois Tigeot 		return ret;
90686a02f1SFrançois Tigeot 
91686a02f1SFrançois Tigeot 	intel_ring_emit(ring, cmd);
92686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
93686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
94686a02f1SFrançois Tigeot 
95686a02f1SFrançois Tigeot 	return 0;
96686a02f1SFrançois Tigeot }
97686a02f1SFrançois Tigeot 
98686a02f1SFrançois Tigeot static int
99ba55f2f5SFrançois Tigeot gen4_render_ring_flush(struct intel_engine_cs *ring,
100686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
101686a02f1SFrançois Tigeot 		       u32	flush_domains)
102e3adcf8fSFrançois Tigeot {
103e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
104686a02f1SFrançois Tigeot 	u32 cmd;
105e3adcf8fSFrançois Tigeot 	int ret;
106e3adcf8fSFrançois Tigeot 
107e3adcf8fSFrançois Tigeot 	/*
108e3adcf8fSFrançois Tigeot 	 * read/write caches:
109e3adcf8fSFrançois Tigeot 	 *
110e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
111e3adcf8fSFrançois Tigeot 	 * only flushed if MI_NO_WRITE_FLUSH is unset.  On 965, it is
112e3adcf8fSFrançois Tigeot 	 * also flushed at 2d versus 3d pipeline switches.
113e3adcf8fSFrançois Tigeot 	 *
114e3adcf8fSFrançois Tigeot 	 * read-only caches:
115e3adcf8fSFrançois Tigeot 	 *
116e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
117e3adcf8fSFrançois Tigeot 	 * MI_READ_FLUSH is set, and is always flushed on 965.
118e3adcf8fSFrançois Tigeot 	 *
119e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_COMMAND may not exist?
120e3adcf8fSFrançois Tigeot 	 *
121e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
122e3adcf8fSFrançois Tigeot 	 * invalidated when MI_EXE_FLUSH is set.
123e3adcf8fSFrançois Tigeot 	 *
124e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
125e3adcf8fSFrançois Tigeot 	 * invalidated with every MI_FLUSH.
126e3adcf8fSFrançois Tigeot 	 *
127e3adcf8fSFrançois Tigeot 	 * TLBs:
128e3adcf8fSFrançois Tigeot 	 *
129e3adcf8fSFrançois Tigeot 	 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
130e3adcf8fSFrançois Tigeot 	 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
131e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
132e3adcf8fSFrançois Tigeot 	 * are flushed at any MI_FLUSH.
133e3adcf8fSFrançois Tigeot 	 */
134e3adcf8fSFrançois Tigeot 
135e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
136686a02f1SFrançois Tigeot 	if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
137e3adcf8fSFrançois Tigeot 		cmd &= ~MI_NO_WRITE_FLUSH;
138e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
139e3adcf8fSFrançois Tigeot 		cmd |= MI_EXE_FLUSH;
140e3adcf8fSFrançois Tigeot 
141e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
142e3adcf8fSFrançois Tigeot 	    (IS_G4X(dev) || IS_GEN5(dev)))
143e3adcf8fSFrançois Tigeot 		cmd |= MI_INVALIDATE_ISP;
144e3adcf8fSFrançois Tigeot 
145e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
146e3adcf8fSFrançois Tigeot 	if (ret)
147e3adcf8fSFrançois Tigeot 		return ret;
148e3adcf8fSFrançois Tigeot 
149e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
150e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
151e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
152e3adcf8fSFrançois Tigeot 
153e3adcf8fSFrançois Tigeot 	return 0;
154e3adcf8fSFrançois Tigeot }
155e3adcf8fSFrançois Tigeot 
156e3adcf8fSFrançois Tigeot /**
157e3adcf8fSFrançois Tigeot  * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
158e3adcf8fSFrançois Tigeot  * implementing two workarounds on gen6.  From section 1.4.7.1
159e3adcf8fSFrançois Tigeot  * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
160e3adcf8fSFrançois Tigeot  *
161e3adcf8fSFrançois Tigeot  * [DevSNB-C+{W/A}] Before any depth stall flush (including those
162e3adcf8fSFrançois Tigeot  * produced by non-pipelined state commands), software needs to first
163e3adcf8fSFrançois Tigeot  * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
164e3adcf8fSFrançois Tigeot  * 0.
165e3adcf8fSFrançois Tigeot  *
166e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
167e3adcf8fSFrançois Tigeot  * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
168e3adcf8fSFrançois Tigeot  *
169e3adcf8fSFrançois Tigeot  * And the workaround for these two requires this workaround first:
170e3adcf8fSFrançois Tigeot  *
171e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
172e3adcf8fSFrançois Tigeot  * BEFORE the pipe-control with a post-sync op and no write-cache
173e3adcf8fSFrançois Tigeot  * flushes.
174e3adcf8fSFrançois Tigeot  *
175e3adcf8fSFrançois Tigeot  * And this last workaround is tricky because of the requirements on
176e3adcf8fSFrançois Tigeot  * that bit.  From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
177e3adcf8fSFrançois Tigeot  * volume 2 part 1:
178e3adcf8fSFrançois Tigeot  *
179e3adcf8fSFrançois Tigeot  *     "1 of the following must also be set:
180e3adcf8fSFrançois Tigeot  *      - Render Target Cache Flush Enable ([12] of DW1)
181e3adcf8fSFrançois Tigeot  *      - Depth Cache Flush Enable ([0] of DW1)
182e3adcf8fSFrançois Tigeot  *      - Stall at Pixel Scoreboard ([1] of DW1)
183e3adcf8fSFrançois Tigeot  *      - Depth Stall ([13] of DW1)
184e3adcf8fSFrançois Tigeot  *      - Post-Sync Operation ([13] of DW1)
185e3adcf8fSFrançois Tigeot  *      - Notify Enable ([8] of DW1)"
186e3adcf8fSFrançois Tigeot  *
187e3adcf8fSFrançois Tigeot  * The cache flushes require the workaround flush that triggered this
188e3adcf8fSFrançois Tigeot  * one, so we can't use it.  Depth stall would trigger the same.
189e3adcf8fSFrançois Tigeot  * Post-sync nonzero is what triggered this second workaround, so we
190e3adcf8fSFrançois Tigeot  * can't use that one either.  Notify enable is IRQs, which aren't
191e3adcf8fSFrançois Tigeot  * really our business.  That leaves only stall at scoreboard.
192e3adcf8fSFrançois Tigeot  */
193e3adcf8fSFrançois Tigeot static int
194ba55f2f5SFrançois Tigeot intel_emit_post_sync_nonzero_flush(struct intel_engine_cs *ring)
195e3adcf8fSFrançois Tigeot {
196ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
197e3adcf8fSFrançois Tigeot 	int ret;
198e3adcf8fSFrançois Tigeot 
199e3adcf8fSFrançois Tigeot 
200e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
201e3adcf8fSFrançois Tigeot 	if (ret)
202e3adcf8fSFrançois Tigeot 		return ret;
203e3adcf8fSFrançois Tigeot 
204e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
205e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
206e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_STALL_AT_SCOREBOARD);
207e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
208e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* low dword */
209e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* high dword */
210e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
211e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
212e3adcf8fSFrançois Tigeot 
213e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
214e3adcf8fSFrançois Tigeot 	if (ret)
215e3adcf8fSFrançois Tigeot 		return ret;
216e3adcf8fSFrançois Tigeot 
217e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
218e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
219e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
220e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
221e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
222e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
223e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
224e3adcf8fSFrançois Tigeot 
225e3adcf8fSFrançois Tigeot 	return 0;
226e3adcf8fSFrançois Tigeot }
227e3adcf8fSFrançois Tigeot 
228e3adcf8fSFrançois Tigeot static int
229ba55f2f5SFrançois Tigeot gen6_render_ring_flush(struct intel_engine_cs *ring,
230e3adcf8fSFrançois Tigeot                          u32 invalidate_domains, u32 flush_domains)
231e3adcf8fSFrançois Tigeot {
232e3adcf8fSFrançois Tigeot 	u32 flags = 0;
233ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
234e3adcf8fSFrançois Tigeot 	int ret;
235e3adcf8fSFrançois Tigeot 
236e3adcf8fSFrançois Tigeot 	/* Force SNB workarounds for PIPE_CONTROL flushes */
237686a02f1SFrançois Tigeot 	ret = intel_emit_post_sync_nonzero_flush(ring);
238686a02f1SFrançois Tigeot 	if (ret)
239686a02f1SFrançois Tigeot 		return ret;
240e3adcf8fSFrançois Tigeot 
241e3adcf8fSFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
242e3adcf8fSFrançois Tigeot 	 * number of bits based on the write domains has little performance
243e3adcf8fSFrançois Tigeot 	 * impact.
244e3adcf8fSFrançois Tigeot 	 */
245b5c29a34SFrançois Tigeot 	if (flush_domains) {
246e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
247b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
248b5c29a34SFrançois Tigeot 		/*
249b5c29a34SFrançois Tigeot 		 * Ensure that any following seqno writes only happen
250b5c29a34SFrançois Tigeot 		 * when the render cache is indeed flushed.
251b5c29a34SFrançois Tigeot 		 */
252b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CS_STALL;
253b5c29a34SFrançois Tigeot 	}
254b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
255686a02f1SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
256e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
257e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
258e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
259e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
260e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
261686a02f1SFrançois Tigeot 		/*
262b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
263686a02f1SFrançois Tigeot 		 */
264b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
265b5c29a34SFrançois Tigeot 	}
266e3adcf8fSFrançois Tigeot 
267b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
268e3adcf8fSFrançois Tigeot 	if (ret)
269e3adcf8fSFrançois Tigeot 		return ret;
270e3adcf8fSFrançois Tigeot 
271b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
272e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, flags);
273e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
274b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
275b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
276b5c29a34SFrançois Tigeot 
277b5c29a34SFrançois Tigeot 	return 0;
278b5c29a34SFrançois Tigeot }
279b5c29a34SFrançois Tigeot 
280b5c29a34SFrançois Tigeot static int
281ba55f2f5SFrançois Tigeot gen7_render_ring_cs_stall_wa(struct intel_engine_cs *ring)
282b5c29a34SFrançois Tigeot {
283b5c29a34SFrançois Tigeot 	int ret;
284b5c29a34SFrançois Tigeot 
285b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
286b5c29a34SFrançois Tigeot 	if (ret)
287b5c29a34SFrançois Tigeot 		return ret;
288b5c29a34SFrançois Tigeot 
289b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
290b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
291b5c29a34SFrançois Tigeot 			      PIPE_CONTROL_STALL_AT_SCOREBOARD);
292b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
293b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
294b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
295b5c29a34SFrançois Tigeot 
296b5c29a34SFrançois Tigeot 	return 0;
297b5c29a34SFrançois Tigeot }
298b5c29a34SFrançois Tigeot 
299ba55f2f5SFrançois Tigeot static int gen7_ring_fbc_flush(struct intel_engine_cs *ring, u32 value)
3005d0b1887SFrançois Tigeot {
3015d0b1887SFrançois Tigeot 	int ret;
3025d0b1887SFrançois Tigeot 
3035d0b1887SFrançois Tigeot 	if (!ring->fbc_dirty)
3045d0b1887SFrançois Tigeot 		return 0;
3055d0b1887SFrançois Tigeot 
3069edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
3075d0b1887SFrançois Tigeot 	if (ret)
3085d0b1887SFrançois Tigeot 		return ret;
3095d0b1887SFrançois Tigeot 	/* WaFbcNukeOn3DBlt:ivb/hsw */
3105d0b1887SFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
3115d0b1887SFrançois Tigeot 	intel_ring_emit(ring, MSG_FBC_REND_STATE);
3125d0b1887SFrançois Tigeot 	intel_ring_emit(ring, value);
3139edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) | MI_SRM_LRM_GLOBAL_GTT);
3149edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MSG_FBC_REND_STATE);
3159edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
3165d0b1887SFrançois Tigeot 	intel_ring_advance(ring);
3175d0b1887SFrançois Tigeot 
3185d0b1887SFrançois Tigeot 	ring->fbc_dirty = false;
3195d0b1887SFrançois Tigeot 	return 0;
3205d0b1887SFrançois Tigeot }
3215d0b1887SFrançois Tigeot 
322b5c29a34SFrançois Tigeot static int
323ba55f2f5SFrançois Tigeot gen7_render_ring_flush(struct intel_engine_cs *ring,
324b5c29a34SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
325b5c29a34SFrançois Tigeot {
326b5c29a34SFrançois Tigeot 	u32 flags = 0;
327ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
328b5c29a34SFrançois Tigeot 	int ret;
329b5c29a34SFrançois Tigeot 
330b5c29a34SFrançois Tigeot 	/*
331b5c29a34SFrançois Tigeot 	 * Ensure that any following seqno writes only happen when the render
332b5c29a34SFrançois Tigeot 	 * cache is indeed flushed.
333b5c29a34SFrançois Tigeot 	 *
334b5c29a34SFrançois Tigeot 	 * Workaround: 4th PIPE_CONTROL command (except the ones with only
335b5c29a34SFrançois Tigeot 	 * read-cache invalidate bits set) must have the CS_STALL bit set. We
336b5c29a34SFrançois Tigeot 	 * don't try to be clever and just set it unconditionally.
337b5c29a34SFrançois Tigeot 	 */
338b5c29a34SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
339b5c29a34SFrançois Tigeot 
340b5c29a34SFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
341b5c29a34SFrançois Tigeot 	 * number of bits based on the write domains has little performance
342b5c29a34SFrançois Tigeot 	 * impact.
343b5c29a34SFrançois Tigeot 	 */
344b5c29a34SFrançois Tigeot 	if (flush_domains) {
345b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
346b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
347b5c29a34SFrançois Tigeot 	}
348b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
349b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
350b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
351b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
352b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
353b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
354b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
355b5c29a34SFrançois Tigeot 		/*
356b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
357b5c29a34SFrançois Tigeot 		 */
358b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
359a2fdbec6SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
360b5c29a34SFrançois Tigeot 
361b5c29a34SFrançois Tigeot 		/* Workaround: we must issue a pipe_control with CS-stall bit
362b5c29a34SFrançois Tigeot 		 * set before a pipe_control command that has the state cache
363b5c29a34SFrançois Tigeot 		 * invalidate bit set. */
364b5c29a34SFrançois Tigeot 		gen7_render_ring_cs_stall_wa(ring);
365b5c29a34SFrançois Tigeot 	}
366b5c29a34SFrançois Tigeot 
367b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
368b5c29a34SFrançois Tigeot 	if (ret)
369b5c29a34SFrançois Tigeot 		return ret;
370b5c29a34SFrançois Tigeot 
371b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
372b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, flags);
373a2fdbec6SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
374b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
375e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
376e3adcf8fSFrançois Tigeot 
3779edbd4a0SFrançois Tigeot 	if (!invalidate_domains && flush_domains)
3785d0b1887SFrançois Tigeot 		return gen7_ring_fbc_flush(ring, FBC_REND_NUKE);
3795d0b1887SFrançois Tigeot 
380e3adcf8fSFrançois Tigeot 	return 0;
381e3adcf8fSFrançois Tigeot }
382e3adcf8fSFrançois Tigeot 
3839edbd4a0SFrançois Tigeot static int
384ba55f2f5SFrançois Tigeot gen8_render_ring_flush(struct intel_engine_cs *ring,
3859edbd4a0SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
3869edbd4a0SFrançois Tigeot {
3879edbd4a0SFrançois Tigeot 	u32 flags = 0;
388ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
3899edbd4a0SFrançois Tigeot 	int ret;
3909edbd4a0SFrançois Tigeot 
3919edbd4a0SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
3929edbd4a0SFrançois Tigeot 
3939edbd4a0SFrançois Tigeot 	if (flush_domains) {
3949edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
3959edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
3969edbd4a0SFrançois Tigeot 	}
3979edbd4a0SFrançois Tigeot 	if (invalidate_domains) {
3989edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
3999edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
4009edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
4019edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
4029edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
4039edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
4049edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
4059edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4069edbd4a0SFrançois Tigeot 	}
4079edbd4a0SFrançois Tigeot 
4089edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
4099edbd4a0SFrançois Tigeot 	if (ret)
4109edbd4a0SFrançois Tigeot 		return ret;
4119edbd4a0SFrançois Tigeot 
4129edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
4139edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, flags);
4149edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
4159edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, 0);
4169edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, 0);
4179edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, 0);
4189edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
4199edbd4a0SFrançois Tigeot 
4209edbd4a0SFrançois Tigeot 	return 0;
4219edbd4a0SFrançois Tigeot 
4229edbd4a0SFrançois Tigeot }
4239edbd4a0SFrançois Tigeot 
424ba55f2f5SFrançois Tigeot static void ring_write_tail(struct intel_engine_cs *ring,
425b5c29a34SFrançois Tigeot 			    u32 value)
426e3adcf8fSFrançois Tigeot {
427ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
428e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
429e3adcf8fSFrançois Tigeot }
430e3adcf8fSFrançois Tigeot 
431ba55f2f5SFrançois Tigeot u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
432e3adcf8fSFrançois Tigeot {
433ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
434ba55f2f5SFrançois Tigeot 	u64 acthd;
435e3adcf8fSFrançois Tigeot 
436ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
437ba55f2f5SFrançois Tigeot 		acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
438ba55f2f5SFrançois Tigeot 					 RING_ACTHD_UDW(ring->mmio_base));
439ba55f2f5SFrançois Tigeot 	else if (INTEL_INFO(ring->dev)->gen >= 4)
440ba55f2f5SFrançois Tigeot 		acthd = I915_READ(RING_ACTHD(ring->mmio_base));
441ba55f2f5SFrançois Tigeot 	else
442ba55f2f5SFrançois Tigeot 		acthd = I915_READ(ACTHD);
443ba55f2f5SFrançois Tigeot 
444ba55f2f5SFrançois Tigeot 	return acthd;
445e3adcf8fSFrançois Tigeot }
446e3adcf8fSFrançois Tigeot 
447ba55f2f5SFrançois Tigeot static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
4485d0b1887SFrançois Tigeot {
4495d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
4505d0b1887SFrançois Tigeot 	u32 addr;
4515d0b1887SFrançois Tigeot 
4525d0b1887SFrançois Tigeot 	addr = dev_priv->status_page_dmah->busaddr;
4535d0b1887SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 4)
4545d0b1887SFrançois Tigeot 		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
4555d0b1887SFrançois Tigeot 	I915_WRITE(HWS_PGA, addr);
4565d0b1887SFrançois Tigeot }
4575d0b1887SFrançois Tigeot 
458ba55f2f5SFrançois Tigeot static bool stop_ring(struct intel_engine_cs *ring)
459e3adcf8fSFrançois Tigeot {
460ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ring->dev);
461e3adcf8fSFrançois Tigeot 
462ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
463ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
464ba55f2f5SFrançois Tigeot 		if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
465ba55f2f5SFrançois Tigeot 			DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
466ba55f2f5SFrançois Tigeot 			return false;
467ba55f2f5SFrançois Tigeot 		}
468ba55f2f5SFrançois Tigeot 	}
469686a02f1SFrançois Tigeot 
470e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
471e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
472e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
473e3adcf8fSFrançois Tigeot 
474ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
475ba55f2f5SFrançois Tigeot 		(void)I915_READ_CTL(ring);
476ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
477ba55f2f5SFrançois Tigeot 	}
478e3adcf8fSFrançois Tigeot 
479ba55f2f5SFrançois Tigeot 	return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
480ba55f2f5SFrançois Tigeot }
481ba55f2f5SFrançois Tigeot 
482ba55f2f5SFrançois Tigeot static int init_ring_common(struct intel_engine_cs *ring)
483ba55f2f5SFrançois Tigeot {
484ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ring->dev;
485ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
486ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
487ba55f2f5SFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
488ba55f2f5SFrançois Tigeot 	int ret = 0;
489ba55f2f5SFrançois Tigeot 
490ba55f2f5SFrançois Tigeot 	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
491ba55f2f5SFrançois Tigeot 
492ba55f2f5SFrançois Tigeot 	if (!stop_ring(ring)) {
493ba55f2f5SFrançois Tigeot 		/* G45 ring initialization often fails to reset head to zero */
494b5c29a34SFrançois Tigeot 		DRM_DEBUG_KMS("%s head not reset to zero "
495e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
496e3adcf8fSFrançois Tigeot 			      ring->name,
497e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
498e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
499e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
500e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
501e3adcf8fSFrançois Tigeot 
502ba55f2f5SFrançois Tigeot 		if (!stop_ring(ring)) {
503e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
504e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
505e3adcf8fSFrançois Tigeot 				  ring->name,
506e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
507e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
508e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
509e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
510686a02f1SFrançois Tigeot 			ret = -EIO;
511686a02f1SFrançois Tigeot 			goto out;
512e3adcf8fSFrançois Tigeot 		}
513ba55f2f5SFrançois Tigeot 	}
514ba55f2f5SFrançois Tigeot 
515ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev))
516ba55f2f5SFrançois Tigeot 		intel_ring_setup_status_page(ring);
517ba55f2f5SFrançois Tigeot 	else
518ba55f2f5SFrançois Tigeot 		ring_setup_phys_status_page(ring);
519ba55f2f5SFrançois Tigeot 
520*0f370975SMatthew Dillon 	/* Enforce ordering by reading HEAD register back */
521*0f370975SMatthew Dillon 	I915_READ_HEAD(ring);
522*0f370975SMatthew Dillon 
523ba55f2f5SFrançois Tigeot 	/* Initialize the ring. This must happen _after_ we've cleared the ring
524ba55f2f5SFrançois Tigeot 	 * registers with the above sequence (the readback of the HEAD registers
525ba55f2f5SFrançois Tigeot 	 * also enforces ordering), otherwise the hw might lose the new ring
526ba55f2f5SFrançois Tigeot 	 * register values. */
527ba55f2f5SFrançois Tigeot 	I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
528ba55f2f5SFrançois Tigeot 	I915_WRITE_CTL(ring,
529ba55f2f5SFrançois Tigeot 			((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
530ba55f2f5SFrançois Tigeot 			| RING_VALID);
531ba55f2f5SFrançois Tigeot 
532ba55f2f5SFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
533ba55f2f5SFrançois Tigeot 	if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
534ba55f2f5SFrançois Tigeot 		     I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
535ba55f2f5SFrançois Tigeot 		     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
536ba55f2f5SFrançois Tigeot 		DRM_ERROR("%s initialization failed "
537ba55f2f5SFrançois Tigeot 			  "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
538ba55f2f5SFrançois Tigeot 			  ring->name,
539ba55f2f5SFrançois Tigeot 			  I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
540ba55f2f5SFrançois Tigeot 			  I915_READ_HEAD(ring), I915_READ_TAIL(ring),
541ba55f2f5SFrançois Tigeot 			  I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
542ba55f2f5SFrançois Tigeot 		ret = -EIO;
543ba55f2f5SFrançois Tigeot 		goto out;
544ba55f2f5SFrançois Tigeot 	}
545e3adcf8fSFrançois Tigeot 
546e3adcf8fSFrançois Tigeot 	if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
547e3adcf8fSFrançois Tigeot 		i915_kernel_lost_context(ring->dev);
548e3adcf8fSFrançois Tigeot 	else {
549ba55f2f5SFrançois Tigeot 		ringbuf->head = I915_READ_HEAD(ring);
550ba55f2f5SFrançois Tigeot 		ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
551ba55f2f5SFrançois Tigeot 		ringbuf->space = ring_space(ring);
552ba55f2f5SFrançois Tigeot 		ringbuf->last_retired_head = -1;
553e3adcf8fSFrançois Tigeot 	}
554e3adcf8fSFrançois Tigeot 
5555d0b1887SFrançois Tigeot 	memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
5565d0b1887SFrançois Tigeot 
557686a02f1SFrançois Tigeot out:
5589edbd4a0SFrançois Tigeot 	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
559686a02f1SFrançois Tigeot 
560686a02f1SFrançois Tigeot 	return ret;
561e3adcf8fSFrançois Tigeot }
562e3adcf8fSFrançois Tigeot 
563e3adcf8fSFrançois Tigeot static int
564ba55f2f5SFrançois Tigeot init_pipe_control(struct intel_engine_cs *ring)
565e3adcf8fSFrançois Tigeot {
566e3adcf8fSFrançois Tigeot 	int ret;
567e3adcf8fSFrançois Tigeot 
5689edbd4a0SFrançois Tigeot 	if (ring->scratch.obj)
569e3adcf8fSFrançois Tigeot 		return 0;
570e3adcf8fSFrançois Tigeot 
5719edbd4a0SFrançois Tigeot 	ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
5729edbd4a0SFrançois Tigeot 	if (ring->scratch.obj == NULL) {
573e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
574e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
575e3adcf8fSFrançois Tigeot 		goto err;
576e3adcf8fSFrançois Tigeot 	}
577e3adcf8fSFrançois Tigeot 
578ba55f2f5SFrançois Tigeot 	ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
579ba55f2f5SFrançois Tigeot 	if (ret)
580ba55f2f5SFrançois Tigeot 		goto err_unref;
581e3adcf8fSFrançois Tigeot 
582ba55f2f5SFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
583e3adcf8fSFrançois Tigeot 	if (ret)
584e3adcf8fSFrançois Tigeot 		goto err_unref;
585e3adcf8fSFrançois Tigeot 
5869edbd4a0SFrançois Tigeot 	ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
5879edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page = kmap(ring->scratch.obj->pages[0]);
5889edbd4a0SFrançois Tigeot 	if (ring->scratch.cpu_page == NULL) {
5895d0b1887SFrançois Tigeot 		ret = -ENOMEM;
590e3adcf8fSFrançois Tigeot 		goto err_unpin;
5915d0b1887SFrançois Tigeot 	}
592a2fdbec6SFrançois Tigeot 
593a2fdbec6SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
5949edbd4a0SFrançois Tigeot 			 ring->name, ring->scratch.gtt_offset);
595e3adcf8fSFrançois Tigeot 	return 0;
596e3adcf8fSFrançois Tigeot 
597e3adcf8fSFrançois Tigeot err_unpin:
598ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(ring->scratch.obj);
599e3adcf8fSFrançois Tigeot err_unref:
6009edbd4a0SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
601e3adcf8fSFrançois Tigeot err:
602e3adcf8fSFrançois Tigeot 	return ret;
603e3adcf8fSFrançois Tigeot }
604e3adcf8fSFrançois Tigeot 
605ba55f2f5SFrançois Tigeot static int init_render_ring(struct intel_engine_cs *ring)
606e3adcf8fSFrançois Tigeot {
607e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
608e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
609e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
610e3adcf8fSFrançois Tigeot 
611ba55f2f5SFrançois Tigeot 	/* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
612ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
613f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
614f4e1c372SFrançois Tigeot 
615f4e1c372SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
616f4e1c372SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
617f4e1c372SFrançois Tigeot 	 * programmed to '1' on all products.
6185d0b1887SFrançois Tigeot 	 *
619ba55f2f5SFrançois Tigeot 	 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
620f4e1c372SFrançois Tigeot 	 */
621f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
622f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
623f4e1c372SFrançois Tigeot 
624f4e1c372SFrançois Tigeot 	/* Required for the hardware to program scanline values for waiting */
625ba55f2f5SFrançois Tigeot 	/* WaEnableFlushTlbInvalidationMode:snb */
626f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6)
627f4e1c372SFrançois Tigeot 		I915_WRITE(GFX_MODE,
628ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
629f4e1c372SFrançois Tigeot 
630ba55f2f5SFrançois Tigeot 	/* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
631e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev))
632e3adcf8fSFrançois Tigeot 		I915_WRITE(GFX_MODE_GEN7,
633ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
634f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
635e3adcf8fSFrançois Tigeot 
636e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
637e3adcf8fSFrançois Tigeot 		ret = init_pipe_control(ring);
638e3adcf8fSFrançois Tigeot 		if (ret)
639e3adcf8fSFrançois Tigeot 			return ret;
640e3adcf8fSFrançois Tigeot 	}
641e3adcf8fSFrançois Tigeot 
642e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
643e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
644e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
645e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
646e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
647e3adcf8fSFrançois Tigeot 		 */
648e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
649f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
650e3adcf8fSFrançois Tigeot 	}
651e3adcf8fSFrançois Tigeot 
652f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
653f4e1c372SFrançois Tigeot 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
654f4e1c372SFrançois Tigeot 
6559edbd4a0SFrançois Tigeot 	if (HAS_L3_DPF(dev))
6569edbd4a0SFrançois Tigeot 		I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
657e3adcf8fSFrançois Tigeot 
658e3adcf8fSFrançois Tigeot 	return ret;
659e3adcf8fSFrançois Tigeot }
660e3adcf8fSFrançois Tigeot 
661ba55f2f5SFrançois Tigeot static void render_ring_cleanup(struct intel_engine_cs *ring)
662e3adcf8fSFrançois Tigeot {
663b5c29a34SFrançois Tigeot 	struct drm_device *dev = ring->dev;
664b5c29a34SFrançois Tigeot 
6659edbd4a0SFrançois Tigeot 	if (ring->scratch.obj == NULL)
666e3adcf8fSFrançois Tigeot 		return;
667e3adcf8fSFrançois Tigeot 
6689edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
6699edbd4a0SFrançois Tigeot 		kunmap(ring->scratch.obj->pages[0]);
670ba55f2f5SFrançois Tigeot 		i915_gem_object_ggtt_unpin(ring->scratch.obj);
6719edbd4a0SFrançois Tigeot 	}
672b5c29a34SFrançois Tigeot 
6739edbd4a0SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
6749edbd4a0SFrançois Tigeot 	ring->scratch.obj = NULL;
675e3adcf8fSFrançois Tigeot }
676e3adcf8fSFrançois Tigeot 
677ba55f2f5SFrançois Tigeot static int gen6_signal(struct intel_engine_cs *signaller,
678ba55f2f5SFrançois Tigeot 		       unsigned int num_dwords)
679e3adcf8fSFrançois Tigeot {
680ba55f2f5SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
681ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
682ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *useless;
683ba55f2f5SFrançois Tigeot 	int i, ret;
684ba55f2f5SFrançois Tigeot 
685ba55f2f5SFrançois Tigeot 	/* NB: In order to be able to do semaphore MBOX updates for varying
686ba55f2f5SFrançois Tigeot 	 * number of rings, it's easiest if we round up each individual update
687ba55f2f5SFrançois Tigeot 	 * to a multiple of 2 (since ring updates must always be a multiple of
688ba55f2f5SFrançois Tigeot 	 * 2) even though the actual update only requires 3 dwords.
6895d0b1887SFrançois Tigeot 	 */
6905d0b1887SFrançois Tigeot #define MBOX_UPDATE_DWORDS 4
691ba55f2f5SFrançois Tigeot 	if (i915_semaphore_is_enabled(dev))
692ba55f2f5SFrançois Tigeot 		num_dwords += ((I915_NUM_RINGS-1) * MBOX_UPDATE_DWORDS);
693ba55f2f5SFrançois Tigeot 	else
694ba55f2f5SFrançois Tigeot 		return intel_ring_begin(signaller, num_dwords);
695ba55f2f5SFrançois Tigeot 
696ba55f2f5SFrançois Tigeot 	ret = intel_ring_begin(signaller, num_dwords);
697ba55f2f5SFrançois Tigeot 	if (ret)
698ba55f2f5SFrançois Tigeot 		return ret;
699ba55f2f5SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
700ba55f2f5SFrançois Tigeot 
701ba55f2f5SFrançois Tigeot 	for_each_ring(useless, dev_priv, i) {
702ba55f2f5SFrançois Tigeot 		u32 mbox_reg = signaller->semaphore.mbox.signal[i];
703ba55f2f5SFrançois Tigeot 		if (mbox_reg != GEN6_NOSYNC) {
704ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
705ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, mbox_reg);
706ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, signaller->outstanding_lazy_seqno);
707ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
708ba55f2f5SFrançois Tigeot 		} else {
709ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
710ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
711ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
712ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
713ba55f2f5SFrançois Tigeot 		}
714ba55f2f5SFrançois Tigeot 	}
715ba55f2f5SFrançois Tigeot 
716ba55f2f5SFrançois Tigeot 	return 0;
717e3adcf8fSFrançois Tigeot }
718e3adcf8fSFrançois Tigeot 
719e3adcf8fSFrançois Tigeot /**
720e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
721e3adcf8fSFrançois Tigeot  *
722e3adcf8fSFrançois Tigeot  * @ring - ring that is adding a request
723e3adcf8fSFrançois Tigeot  * @seqno - return seqno stuck into the ring
724e3adcf8fSFrançois Tigeot  *
725e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
726e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
727e3adcf8fSFrançois Tigeot  */
728e3adcf8fSFrançois Tigeot static int
729ba55f2f5SFrançois Tigeot gen6_add_request(struct intel_engine_cs *ring)
730e3adcf8fSFrançois Tigeot {
731ba55f2f5SFrançois Tigeot 	int ret;
732e3adcf8fSFrançois Tigeot 
733ba55f2f5SFrançois Tigeot 	ret = ring->semaphore.signal(ring, 4);
7349edbd4a0SFrançois Tigeot 	if (ret)
7359edbd4a0SFrançois Tigeot 		return ret;
7369edbd4a0SFrançois Tigeot 
737e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
738e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
7399edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_seqno);
740e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
7419edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
742e3adcf8fSFrançois Tigeot 
743e3adcf8fSFrançois Tigeot 	return 0;
744e3adcf8fSFrançois Tigeot }
745e3adcf8fSFrançois Tigeot 
746a2fdbec6SFrançois Tigeot static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
747a2fdbec6SFrançois Tigeot 					      u32 seqno)
748a2fdbec6SFrançois Tigeot {
749a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
750a2fdbec6SFrançois Tigeot 	return dev_priv->last_seqno < seqno;
751a2fdbec6SFrançois Tigeot }
752a2fdbec6SFrançois Tigeot 
753e3adcf8fSFrançois Tigeot /**
754e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
755e3adcf8fSFrançois Tigeot  *
756e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
757e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
758e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
759e3adcf8fSFrançois Tigeot  */
760e3adcf8fSFrançois Tigeot static int
761ba55f2f5SFrançois Tigeot gen6_ring_sync(struct intel_engine_cs *waiter,
762ba55f2f5SFrançois Tigeot 	       struct intel_engine_cs *signaller,
763e3adcf8fSFrançois Tigeot 	       u32 seqno)
764e3adcf8fSFrançois Tigeot {
765e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
766e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
767e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
768ba55f2f5SFrançois Tigeot 	u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
769ba55f2f5SFrançois Tigeot 	int ret;
770e3adcf8fSFrançois Tigeot 
771686a02f1SFrançois Tigeot 	/* Throughout all of the GEM code, seqno passed implies our current
772686a02f1SFrançois Tigeot 	 * seqno is >= the last seqno executed. However for hardware the
773686a02f1SFrançois Tigeot 	 * comparison is strictly greater than.
774686a02f1SFrançois Tigeot 	 */
775686a02f1SFrançois Tigeot 	seqno -= 1;
776686a02f1SFrançois Tigeot 
777ba55f2f5SFrançois Tigeot 	WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
778686a02f1SFrançois Tigeot 
779e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
780e3adcf8fSFrançois Tigeot 	if (ret)
781e3adcf8fSFrançois Tigeot 		return ret;
782e3adcf8fSFrançois Tigeot 
783a2fdbec6SFrançois Tigeot 	/* If seqno wrap happened, omit the wait with no-ops */
784a2fdbec6SFrançois Tigeot 	if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
785ba55f2f5SFrançois Tigeot 		intel_ring_emit(waiter, dw1 | wait_mbox);
786e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, seqno);
787e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, 0);
788e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
789a2fdbec6SFrançois Tigeot 	} else {
790a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
791a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
792a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
793a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
794a2fdbec6SFrançois Tigeot 	}
795e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
796e3adcf8fSFrançois Tigeot 
797e3adcf8fSFrançois Tigeot 	return 0;
798e3adcf8fSFrançois Tigeot }
799e3adcf8fSFrançois Tigeot 
800e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
801e3adcf8fSFrançois Tigeot do {									\
802e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
803e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
804e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
805e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
806e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
807e3adcf8fSFrançois Tigeot } while (0)
808e3adcf8fSFrançois Tigeot 
809e3adcf8fSFrançois Tigeot static int
810ba55f2f5SFrançois Tigeot pc_render_add_request(struct intel_engine_cs *ring)
811e3adcf8fSFrançois Tigeot {
812ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
813e3adcf8fSFrançois Tigeot 	int ret;
814e3adcf8fSFrançois Tigeot 
815e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
816e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
817e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
818e3adcf8fSFrançois Tigeot 	 *
819e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
820e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
821e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
822e3adcf8fSFrançois Tigeot 	 */
823e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 32);
824e3adcf8fSFrançois Tigeot 	if (ret)
825e3adcf8fSFrançois Tigeot 		return ret;
826e3adcf8fSFrançois Tigeot 
827e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
828e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
829e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
8309edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
8319edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_seqno);
832e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
833e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
834ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
835e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
836ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
837e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
838ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
839e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
840ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
841e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
842ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
843e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
844b5c29a34SFrançois Tigeot 
845e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
846e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
847e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
848e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
8499edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
8509edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_seqno);
851e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
8529edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
853e3adcf8fSFrançois Tigeot 
854e3adcf8fSFrançois Tigeot 	return 0;
855e3adcf8fSFrançois Tigeot }
856e3adcf8fSFrançois Tigeot 
857e3adcf8fSFrançois Tigeot static u32
858ba55f2f5SFrançois Tigeot gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
859e3adcf8fSFrançois Tigeot {
860e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
861e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
862e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
863ba55f2f5SFrançois Tigeot 	if (!lazy_coherency) {
864ba55f2f5SFrançois Tigeot 		struct drm_i915_private *dev_priv = ring->dev->dev_private;
865ba55f2f5SFrançois Tigeot 		POSTING_READ(RING_ACTHD(ring->mmio_base));
866ba55f2f5SFrançois Tigeot 	}
867ba55f2f5SFrançois Tigeot 
868e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
869e3adcf8fSFrançois Tigeot }
870e3adcf8fSFrançois Tigeot 
871b030f26bSFrançois Tigeot static u32
872ba55f2f5SFrançois Tigeot ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
873e3adcf8fSFrançois Tigeot {
874e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
875e3adcf8fSFrançois Tigeot }
876e3adcf8fSFrançois Tigeot 
877a2fdbec6SFrançois Tigeot static void
878ba55f2f5SFrançois Tigeot ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
879a2fdbec6SFrançois Tigeot {
880a2fdbec6SFrançois Tigeot 	intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
881a2fdbec6SFrançois Tigeot }
882a2fdbec6SFrançois Tigeot 
883b030f26bSFrançois Tigeot static u32
884ba55f2f5SFrançois Tigeot pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
885e3adcf8fSFrançois Tigeot {
8869edbd4a0SFrançois Tigeot 	return ring->scratch.cpu_page[0];
887e3adcf8fSFrançois Tigeot }
888e3adcf8fSFrançois Tigeot 
889a2fdbec6SFrançois Tigeot static void
890ba55f2f5SFrançois Tigeot pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
891a2fdbec6SFrançois Tigeot {
8929edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page[0] = seqno;
893a2fdbec6SFrançois Tigeot }
894a2fdbec6SFrançois Tigeot 
895e3adcf8fSFrançois Tigeot static bool
896ba55f2f5SFrançois Tigeot gen5_ring_get_irq(struct intel_engine_cs *ring)
897e3adcf8fSFrançois Tigeot {
898e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
899ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
900e3adcf8fSFrançois Tigeot 
901e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
902e3adcf8fSFrançois Tigeot 		return false;
903e3adcf8fSFrançois Tigeot 
90402727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9059edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0)
9069edbd4a0SFrançois Tigeot 		ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
90702727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
908e3adcf8fSFrançois Tigeot 
909e3adcf8fSFrançois Tigeot 	return true;
910e3adcf8fSFrançois Tigeot }
911e3adcf8fSFrançois Tigeot 
912e3adcf8fSFrançois Tigeot static void
913ba55f2f5SFrançois Tigeot gen5_ring_put_irq(struct intel_engine_cs *ring)
914e3adcf8fSFrançois Tigeot {
915e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
916ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
917e3adcf8fSFrançois Tigeot 
91802727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9199edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0)
9209edbd4a0SFrançois Tigeot 		ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
921686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
922686a02f1SFrançois Tigeot }
923686a02f1SFrançois Tigeot 
924686a02f1SFrançois Tigeot static bool
925ba55f2f5SFrançois Tigeot i9xx_ring_get_irq(struct intel_engine_cs *ring)
926686a02f1SFrançois Tigeot {
927686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
928ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
929686a02f1SFrançois Tigeot 
930686a02f1SFrançois Tigeot 	if (!dev->irq_enabled)
931686a02f1SFrançois Tigeot 		return false;
932686a02f1SFrançois Tigeot 
933686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9349edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
935686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
936686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
937686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
938686a02f1SFrançois Tigeot 	}
939686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
940686a02f1SFrançois Tigeot 
941686a02f1SFrançois Tigeot 	return true;
942686a02f1SFrançois Tigeot }
943686a02f1SFrançois Tigeot 
944686a02f1SFrançois Tigeot static void
945ba55f2f5SFrançois Tigeot i9xx_ring_put_irq(struct intel_engine_cs *ring)
946686a02f1SFrançois Tigeot {
947686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
948ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
949686a02f1SFrançois Tigeot 
950686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9519edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
952686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
953686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
954686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
955686a02f1SFrançois Tigeot 	}
956686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
957686a02f1SFrançois Tigeot }
958686a02f1SFrançois Tigeot 
959686a02f1SFrançois Tigeot static bool
960ba55f2f5SFrançois Tigeot i8xx_ring_get_irq(struct intel_engine_cs *ring)
961686a02f1SFrançois Tigeot {
962686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
963ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
964686a02f1SFrançois Tigeot 
965686a02f1SFrançois Tigeot 	if (!dev->irq_enabled)
966686a02f1SFrançois Tigeot 		return false;
967686a02f1SFrançois Tigeot 
968686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9699edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
970686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
971686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
972686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
973686a02f1SFrançois Tigeot 	}
974686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
975686a02f1SFrançois Tigeot 
976686a02f1SFrançois Tigeot 	return true;
977686a02f1SFrançois Tigeot }
978686a02f1SFrançois Tigeot 
979686a02f1SFrançois Tigeot static void
980ba55f2f5SFrançois Tigeot i8xx_ring_put_irq(struct intel_engine_cs *ring)
981686a02f1SFrançois Tigeot {
982686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
983ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
984686a02f1SFrançois Tigeot 
985686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9869edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
987686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
988686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
989686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
990e3adcf8fSFrançois Tigeot 	}
99102727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
992e3adcf8fSFrançois Tigeot }
993e3adcf8fSFrançois Tigeot 
994ba55f2f5SFrançois Tigeot void intel_ring_setup_status_page(struct intel_engine_cs *ring)
995e3adcf8fSFrançois Tigeot {
996e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
997ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
998b5c29a34SFrançois Tigeot 	u32 mmio = 0;
999e3adcf8fSFrançois Tigeot 
1000e3adcf8fSFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
1001e3adcf8fSFrançois Tigeot 	 * the ring registers as of gen7.
1002e3adcf8fSFrançois Tigeot 	 */
1003e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev)) {
1004e3adcf8fSFrançois Tigeot 		switch (ring->id) {
1005e3adcf8fSFrançois Tigeot 		case RCS:
1006e3adcf8fSFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
1007e3adcf8fSFrançois Tigeot 			break;
1008e3adcf8fSFrançois Tigeot 		case BCS:
1009e3adcf8fSFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
1010e3adcf8fSFrançois Tigeot 			break;
1011ba55f2f5SFrançois Tigeot 		/*
1012ba55f2f5SFrançois Tigeot 		 * VCS2 actually doesn't exist on Gen7. Only shut up
1013ba55f2f5SFrançois Tigeot 		 * gcc switch check warning
1014ba55f2f5SFrançois Tigeot 		 */
1015ba55f2f5SFrançois Tigeot 		case VCS2:
1016e3adcf8fSFrançois Tigeot 		case VCS:
1017e3adcf8fSFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
1018e3adcf8fSFrançois Tigeot 			break;
10195d0b1887SFrançois Tigeot 		case VECS:
10205d0b1887SFrançois Tigeot 			mmio = VEBOX_HWS_PGA_GEN7;
10215d0b1887SFrançois Tigeot 			break;
1022e3adcf8fSFrançois Tigeot 		}
1023b5c29a34SFrançois Tigeot 	} else if (IS_GEN6(ring->dev)) {
1024e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
1025e3adcf8fSFrançois Tigeot 	} else {
10269edbd4a0SFrançois Tigeot 		/* XXX: gen8 returns to sanity */
1027e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
1028e3adcf8fSFrançois Tigeot 	}
1029e3adcf8fSFrançois Tigeot 
1030e3adcf8fSFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
1031e3adcf8fSFrançois Tigeot 	POSTING_READ(mmio);
10325d0b1887SFrançois Tigeot 
1033ba55f2f5SFrançois Tigeot 	/*
1034ba55f2f5SFrançois Tigeot 	 * Flush the TLB for this page
1035ba55f2f5SFrançois Tigeot 	 *
1036ba55f2f5SFrançois Tigeot 	 * FIXME: These two bits have disappeared on gen8, so a question
1037ba55f2f5SFrançois Tigeot 	 * arises: do we still need this and if so how should we go about
1038ba55f2f5SFrançois Tigeot 	 * invalidating the TLB?
1039ba55f2f5SFrançois Tigeot 	 */
1040ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
10415d0b1887SFrançois Tigeot 		u32 reg = RING_INSTPM(ring->mmio_base);
1042ba55f2f5SFrançois Tigeot 
1043ba55f2f5SFrançois Tigeot 		/* ring should be idle before issuing a sync flush*/
1044ba55f2f5SFrançois Tigeot 		WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
1045ba55f2f5SFrançois Tigeot 
10465d0b1887SFrançois Tigeot 		I915_WRITE(reg,
10475d0b1887SFrançois Tigeot 			   _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
10485d0b1887SFrançois Tigeot 					      INSTPM_SYNC_FLUSH));
10495d0b1887SFrançois Tigeot 		if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
10505d0b1887SFrançois Tigeot 			     1000))
10515d0b1887SFrançois Tigeot 			DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
10525d0b1887SFrançois Tigeot 				  ring->name);
10535d0b1887SFrançois Tigeot 	}
1054e3adcf8fSFrançois Tigeot }
1055e3adcf8fSFrançois Tigeot 
1056e3adcf8fSFrançois Tigeot static int
1057ba55f2f5SFrançois Tigeot bsd_ring_flush(struct intel_engine_cs *ring,
1058b5c29a34SFrançois Tigeot 	       u32     invalidate_domains,
1059b5c29a34SFrançois Tigeot 	       u32     flush_domains)
1060e3adcf8fSFrançois Tigeot {
1061e3adcf8fSFrançois Tigeot 	int ret;
1062e3adcf8fSFrançois Tigeot 
1063e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1064e3adcf8fSFrançois Tigeot 	if (ret)
1065e3adcf8fSFrançois Tigeot 		return ret;
1066e3adcf8fSFrançois Tigeot 
1067e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
1068e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1069e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1070e3adcf8fSFrançois Tigeot 	return 0;
1071e3adcf8fSFrançois Tigeot }
1072e3adcf8fSFrançois Tigeot 
1073e3adcf8fSFrançois Tigeot static int
1074ba55f2f5SFrançois Tigeot i9xx_add_request(struct intel_engine_cs *ring)
1075e3adcf8fSFrançois Tigeot {
1076e3adcf8fSFrançois Tigeot 	int ret;
1077e3adcf8fSFrançois Tigeot 
1078e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1079e3adcf8fSFrançois Tigeot 	if (ret)
1080e3adcf8fSFrançois Tigeot 		return ret;
1081e3adcf8fSFrançois Tigeot 
1082e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1083e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
10849edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_seqno);
1085e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
10869edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1087e3adcf8fSFrançois Tigeot 
1088e3adcf8fSFrançois Tigeot 	return 0;
1089e3adcf8fSFrançois Tigeot }
1090e3adcf8fSFrançois Tigeot 
1091e3adcf8fSFrançois Tigeot static bool
1092ba55f2f5SFrançois Tigeot gen6_ring_get_irq(struct intel_engine_cs *ring)
1093e3adcf8fSFrançois Tigeot {
1094e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1095ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1096e3adcf8fSFrançois Tigeot 
1097e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
1098e3adcf8fSFrançois Tigeot 	       return false;
1099e3adcf8fSFrançois Tigeot 
110002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11019edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
11029edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
11035d0b1887SFrançois Tigeot 			I915_WRITE_IMR(ring,
11045d0b1887SFrançois Tigeot 				       ~(ring->irq_enable_mask |
11059edbd4a0SFrançois Tigeot 					 GT_PARITY_ERROR(dev)));
1106686a02f1SFrançois Tigeot 		else
1107686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
11089edbd4a0SFrançois Tigeot 		ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1109e3adcf8fSFrançois Tigeot 	}
111002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1111e3adcf8fSFrançois Tigeot 
1112e3adcf8fSFrançois Tigeot 	return true;
1113e3adcf8fSFrançois Tigeot }
1114e3adcf8fSFrançois Tigeot 
1115e3adcf8fSFrançois Tigeot static void
1116ba55f2f5SFrançois Tigeot gen6_ring_put_irq(struct intel_engine_cs *ring)
1117e3adcf8fSFrançois Tigeot {
1118e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1119ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1120e3adcf8fSFrançois Tigeot 
112102727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11229edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
11239edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
11249edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1125686a02f1SFrançois Tigeot 		else
1126686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
11279edbd4a0SFrançois Tigeot 		ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1128e3adcf8fSFrançois Tigeot 	}
112902727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1130e3adcf8fSFrançois Tigeot }
1131e3adcf8fSFrançois Tigeot 
11325d0b1887SFrançois Tigeot static bool
1133ba55f2f5SFrançois Tigeot hsw_vebox_get_irq(struct intel_engine_cs *ring)
11345d0b1887SFrançois Tigeot {
11355d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11365d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11375d0b1887SFrançois Tigeot 
11385d0b1887SFrançois Tigeot 	if (!dev->irq_enabled)
11395d0b1887SFrançois Tigeot 		return false;
11405d0b1887SFrançois Tigeot 
11419edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11429edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
11435d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
11449edbd4a0SFrançois Tigeot 		snb_enable_pm_irq(dev_priv, ring->irq_enable_mask);
11455d0b1887SFrançois Tigeot 	}
11469edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
11475d0b1887SFrançois Tigeot 
11485d0b1887SFrançois Tigeot 	return true;
11495d0b1887SFrançois Tigeot }
11505d0b1887SFrançois Tigeot 
11515d0b1887SFrançois Tigeot static void
1152ba55f2f5SFrançois Tigeot hsw_vebox_put_irq(struct intel_engine_cs *ring)
11535d0b1887SFrançois Tigeot {
11545d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11555d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11565d0b1887SFrançois Tigeot 
11575d0b1887SFrançois Tigeot 	if (!dev->irq_enabled)
11585d0b1887SFrançois Tigeot 		return;
11595d0b1887SFrançois Tigeot 
11609edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11619edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
11625d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~0);
11639edbd4a0SFrançois Tigeot 		snb_disable_pm_irq(dev_priv, ring->irq_enable_mask);
11645d0b1887SFrançois Tigeot 	}
11659edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
11669edbd4a0SFrançois Tigeot }
11679edbd4a0SFrançois Tigeot 
11689edbd4a0SFrançois Tigeot static bool
1169ba55f2f5SFrançois Tigeot gen8_ring_get_irq(struct intel_engine_cs *ring)
11709edbd4a0SFrançois Tigeot {
11719edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11729edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11739edbd4a0SFrançois Tigeot 
11749edbd4a0SFrançois Tigeot 	if (!dev->irq_enabled)
11759edbd4a0SFrançois Tigeot 		return false;
11769edbd4a0SFrançois Tigeot 
11779edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11789edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
11799edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
11809edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
11819edbd4a0SFrançois Tigeot 				       ~(ring->irq_enable_mask |
11829edbd4a0SFrançois Tigeot 					 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
11839edbd4a0SFrançois Tigeot 		} else {
11849edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
11859edbd4a0SFrançois Tigeot 		}
11869edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
11879edbd4a0SFrançois Tigeot 	}
11889edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
11899edbd4a0SFrançois Tigeot 
11909edbd4a0SFrançois Tigeot 	return true;
11919edbd4a0SFrançois Tigeot }
11929edbd4a0SFrançois Tigeot 
11939edbd4a0SFrançois Tigeot static void
1194ba55f2f5SFrançois Tigeot gen8_ring_put_irq(struct intel_engine_cs *ring)
11959edbd4a0SFrançois Tigeot {
11969edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11979edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11989edbd4a0SFrançois Tigeot 
11999edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
12009edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
12019edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
12029edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
12039edbd4a0SFrançois Tigeot 				       ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
12049edbd4a0SFrançois Tigeot 		} else {
12059edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
12069edbd4a0SFrançois Tigeot 		}
12079edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
12089edbd4a0SFrançois Tigeot 	}
12099edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
12105d0b1887SFrançois Tigeot }
12115d0b1887SFrançois Tigeot 
1212e3adcf8fSFrançois Tigeot static int
1213ba55f2f5SFrançois Tigeot i965_dispatch_execbuffer(struct intel_engine_cs *ring,
1214ba55f2f5SFrançois Tigeot 			 u64 offset, u32 length,
1215b5c29a34SFrançois Tigeot 			 unsigned flags)
1216e3adcf8fSFrançois Tigeot {
1217e3adcf8fSFrançois Tigeot 	int ret;
1218e3adcf8fSFrançois Tigeot 
1219e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1220e3adcf8fSFrançois Tigeot 	if (ret)
1221e3adcf8fSFrançois Tigeot 		return ret;
1222e3adcf8fSFrançois Tigeot 
1223e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
1224686a02f1SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1225b5c29a34SFrançois Tigeot 			MI_BATCH_GTT |
1226b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
1227e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1228e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1229e3adcf8fSFrançois Tigeot 
1230e3adcf8fSFrançois Tigeot 	return 0;
1231e3adcf8fSFrançois Tigeot }
1232e3adcf8fSFrançois Tigeot 
1233b5c29a34SFrançois Tigeot /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1234b5c29a34SFrançois Tigeot #define I830_BATCH_LIMIT (256*1024)
1235e3adcf8fSFrançois Tigeot static int
1236ba55f2f5SFrançois Tigeot i830_dispatch_execbuffer(struct intel_engine_cs *ring,
1237ba55f2f5SFrançois Tigeot 				u64 offset, u32 len,
1238b5c29a34SFrançois Tigeot 				unsigned flags)
1239e3adcf8fSFrançois Tigeot {
1240e3adcf8fSFrançois Tigeot 	int ret;
1241e3adcf8fSFrançois Tigeot 
1242b5c29a34SFrançois Tigeot 	if (flags & I915_DISPATCH_PINNED) {
1243e3adcf8fSFrançois Tigeot 		ret = intel_ring_begin(ring, 4);
1244e3adcf8fSFrançois Tigeot 		if (ret)
1245e3adcf8fSFrançois Tigeot 			return ret;
1246e3adcf8fSFrançois Tigeot 
1247e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
1248b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1249e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, offset + len - 8);
1250b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
1251686a02f1SFrançois Tigeot 		intel_ring_advance(ring);
1252b5c29a34SFrançois Tigeot 	} else {
12539edbd4a0SFrançois Tigeot 		u32 cs_offset = ring->scratch.gtt_offset;
1254b5c29a34SFrançois Tigeot 
1255b5c29a34SFrançois Tigeot 		if (len > I830_BATCH_LIMIT)
1256b5c29a34SFrançois Tigeot 			return -ENOSPC;
1257b5c29a34SFrançois Tigeot 
1258b5c29a34SFrançois Tigeot 		ret = intel_ring_begin(ring, 9+3);
1259b5c29a34SFrançois Tigeot 		if (ret)
1260b5c29a34SFrançois Tigeot 			return ret;
1261b5c29a34SFrançois Tigeot 		/* Blit the batch (which has now all relocs applied) to the stable batch
1262b5c29a34SFrançois Tigeot 		 * scratch bo area (so that the CS never stumbles over its tlb
1263b5c29a34SFrançois Tigeot 		 * invalidation bug) ... */
1264b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
1265b5c29a34SFrançois Tigeot 				XY_SRC_COPY_BLT_WRITE_ALPHA |
1266b5c29a34SFrançois Tigeot 				XY_SRC_COPY_BLT_WRITE_RGB);
1267b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
1268b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 0);
1269b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
1270b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset);
1271b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 0);
1272b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 4096);
1273b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, offset);
1274b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_FLUSH);
1275b5c29a34SFrançois Tigeot 
1276b5c29a34SFrançois Tigeot 		/* ... and execute it. */
1277b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
1278b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1279b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset + len - 8);
1280b5c29a34SFrançois Tigeot 		intel_ring_advance(ring);
1281b5c29a34SFrançois Tigeot 	}
1282686a02f1SFrançois Tigeot 
1283686a02f1SFrançois Tigeot 	return 0;
1284686a02f1SFrançois Tigeot }
1285686a02f1SFrançois Tigeot 
1286686a02f1SFrançois Tigeot static int
1287ba55f2f5SFrançois Tigeot i915_dispatch_execbuffer(struct intel_engine_cs *ring,
1288ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1289b5c29a34SFrançois Tigeot 			 unsigned flags)
1290686a02f1SFrançois Tigeot {
1291686a02f1SFrançois Tigeot 	int ret;
1292686a02f1SFrançois Tigeot 
1293e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1294e3adcf8fSFrançois Tigeot 	if (ret)
1295e3adcf8fSFrançois Tigeot 		return ret;
1296e3adcf8fSFrançois Tigeot 
1297686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1298686a02f1SFrançois Tigeot 	intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1299e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1300e3adcf8fSFrançois Tigeot 
1301e3adcf8fSFrançois Tigeot 	return 0;
1302e3adcf8fSFrançois Tigeot }
1303e3adcf8fSFrançois Tigeot 
1304ba55f2f5SFrançois Tigeot static void cleanup_status_page(struct intel_engine_cs *ring)
1305e3adcf8fSFrançois Tigeot {
1306e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1307e3adcf8fSFrançois Tigeot 
1308e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
1309e3adcf8fSFrançois Tigeot 	if (obj == NULL)
1310e3adcf8fSFrançois Tigeot 		return;
1311e3adcf8fSFrançois Tigeot 
13129edbd4a0SFrançois Tigeot 	kunmap(obj->pages[0]);
1313ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(obj);
1314e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1315e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
1316e3adcf8fSFrançois Tigeot }
1317e3adcf8fSFrançois Tigeot 
1318ba55f2f5SFrançois Tigeot static int init_status_page(struct intel_engine_cs *ring)
1319e3adcf8fSFrançois Tigeot {
1320e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1321ba55f2f5SFrançois Tigeot 
1322ba55f2f5SFrançois Tigeot 	if ((obj = ring->status_page.obj) == NULL) {
1323e3adcf8fSFrançois Tigeot 		int ret;
1324e3adcf8fSFrançois Tigeot 
1325ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(ring->dev, 4096);
1326e3adcf8fSFrançois Tigeot 		if (obj == NULL) {
1327e3adcf8fSFrançois Tigeot 			DRM_ERROR("Failed to allocate status page\n");
1328ba55f2f5SFrançois Tigeot 			return -ENOMEM;
1329e3adcf8fSFrançois Tigeot 		}
1330e3adcf8fSFrançois Tigeot 
1331ba55f2f5SFrançois Tigeot 		ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1332ba55f2f5SFrançois Tigeot 		if (ret)
1333e3adcf8fSFrançois Tigeot 			goto err_unref;
1334ba55f2f5SFrançois Tigeot 
1335ba55f2f5SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 4096, 0);
1336ba55f2f5SFrançois Tigeot 		if (ret) {
1337ba55f2f5SFrançois Tigeot err_unref:
1338ba55f2f5SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
1339ba55f2f5SFrançois Tigeot 			return ret;
1340ba55f2f5SFrançois Tigeot 		}
1341ba55f2f5SFrançois Tigeot 
1342ba55f2f5SFrançois Tigeot 		ring->status_page.obj = obj;
1343e3adcf8fSFrançois Tigeot 	}
1344e3adcf8fSFrançois Tigeot 
13459edbd4a0SFrançois Tigeot 	ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1346f4f90b23SFrançois Tigeot 	ring->status_page.page_addr = kmap(obj->pages[0]);
1347e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1348e3adcf8fSFrançois Tigeot 
1349b5c29a34SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1350e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1351e3adcf8fSFrançois Tigeot 
1352e3adcf8fSFrançois Tigeot 	return 0;
1353e3adcf8fSFrançois Tigeot }
1354e3adcf8fSFrançois Tigeot 
1355ba55f2f5SFrançois Tigeot static int init_phys_status_page(struct intel_engine_cs *ring)
1356686a02f1SFrançois Tigeot {
1357686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1358686a02f1SFrançois Tigeot 
1359686a02f1SFrançois Tigeot 	if (!dev_priv->status_page_dmah) {
1360686a02f1SFrançois Tigeot 		dev_priv->status_page_dmah =
1361b31e9d59SFrançois Tigeot 			drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1362686a02f1SFrançois Tigeot 		if (!dev_priv->status_page_dmah)
1363686a02f1SFrançois Tigeot 			return -ENOMEM;
1364686a02f1SFrançois Tigeot 	}
1365686a02f1SFrançois Tigeot 
1366686a02f1SFrançois Tigeot 	ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1367686a02f1SFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1368686a02f1SFrançois Tigeot 
1369686a02f1SFrançois Tigeot 	return 0;
1370686a02f1SFrançois Tigeot }
1371686a02f1SFrançois Tigeot 
1372ba55f2f5SFrançois Tigeot static int allocate_ring_buffer(struct intel_engine_cs *ring)
1373e3adcf8fSFrançois Tigeot {
1374ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1375ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1376e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1377e3adcf8fSFrançois Tigeot 	int ret;
1378e3adcf8fSFrançois Tigeot 
1379ba55f2f5SFrançois Tigeot 	if (intel_ring_initialized(ring))
1380ba55f2f5SFrançois Tigeot 		return 0;
1381e3adcf8fSFrançois Tigeot 
1382a2fdbec6SFrançois Tigeot 	obj = NULL;
1383a2fdbec6SFrançois Tigeot 	if (!HAS_LLC(dev))
1384ba55f2f5SFrançois Tigeot 		obj = i915_gem_object_create_stolen(dev, ringbuf->size);
1385a2fdbec6SFrançois Tigeot 	if (obj == NULL)
1386ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, ringbuf->size);
1387ba55f2f5SFrançois Tigeot 	if (obj == NULL)
1388ba55f2f5SFrançois Tigeot 		return -ENOMEM;
1389e3adcf8fSFrançois Tigeot 
1390ba55f2f5SFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1391e3adcf8fSFrançois Tigeot 	if (ret)
1392e3adcf8fSFrançois Tigeot 		goto err_unref;
1393e3adcf8fSFrançois Tigeot 
1394686a02f1SFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
1395686a02f1SFrançois Tigeot 	if (ret)
1396686a02f1SFrançois Tigeot 		goto err_unpin;
1397e3adcf8fSFrançois Tigeot 
1398ba55f2f5SFrançois Tigeot 	ringbuf->virtual_start =
13999edbd4a0SFrançois Tigeot 		ioremap_wc(dev->agp->base + i915_gem_obj_ggtt_offset(obj),
1400ba55f2f5SFrançois Tigeot 				ringbuf->size);
1401ba55f2f5SFrançois Tigeot 	if (ringbuf->virtual_start == NULL) {
1402e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1403e3adcf8fSFrançois Tigeot 		goto err_unpin;
1404e3adcf8fSFrançois Tigeot 	}
1405e3adcf8fSFrançois Tigeot 
1406ba55f2f5SFrançois Tigeot 	ringbuf->obj = obj;
1407ba55f2f5SFrançois Tigeot 	return 0;
1408ba55f2f5SFrançois Tigeot 
1409ba55f2f5SFrançois Tigeot err_unpin:
1410ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(obj);
1411ba55f2f5SFrançois Tigeot err_unref:
1412ba55f2f5SFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1413ba55f2f5SFrançois Tigeot 	return ret;
1414ba55f2f5SFrançois Tigeot }
1415ba55f2f5SFrançois Tigeot 
1416ba55f2f5SFrançois Tigeot static int intel_init_ring_buffer(struct drm_device *dev,
1417ba55f2f5SFrançois Tigeot 				  struct intel_engine_cs *ring)
1418ba55f2f5SFrançois Tigeot {
1419ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1420ba55f2f5SFrançois Tigeot 	int ret;
1421ba55f2f5SFrançois Tigeot 
1422ba55f2f5SFrançois Tigeot 	if (ringbuf == NULL) {
1423ba55f2f5SFrançois Tigeot 		ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1424ba55f2f5SFrançois Tigeot 		if (!ringbuf)
1425ba55f2f5SFrançois Tigeot 			return -ENOMEM;
1426ba55f2f5SFrançois Tigeot 		ring->buffer = ringbuf;
1427ba55f2f5SFrançois Tigeot 	}
1428ba55f2f5SFrançois Tigeot 
1429ba55f2f5SFrançois Tigeot 	ring->dev = dev;
1430ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1431ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
1432ba55f2f5SFrançois Tigeot 	ringbuf->size = 32 * PAGE_SIZE;
1433ba55f2f5SFrançois Tigeot 	memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
1434ba55f2f5SFrançois Tigeot 
1435ba55f2f5SFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
1436ba55f2f5SFrançois Tigeot 
1437ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
1438ba55f2f5SFrançois Tigeot 		ret = init_status_page(ring);
1439e3adcf8fSFrançois Tigeot 		if (ret)
1440ba55f2f5SFrançois Tigeot 			goto error;
1441ba55f2f5SFrançois Tigeot 	} else {
1442ba55f2f5SFrançois Tigeot 		BUG_ON(ring->id != RCS);
1443ba55f2f5SFrançois Tigeot 		ret = init_phys_status_page(ring);
1444ba55f2f5SFrançois Tigeot 		if (ret)
1445ba55f2f5SFrançois Tigeot 			goto error;
1446ba55f2f5SFrançois Tigeot 	}
1447ba55f2f5SFrançois Tigeot 
1448ba55f2f5SFrançois Tigeot 	ret = allocate_ring_buffer(ring);
1449ba55f2f5SFrançois Tigeot 	if (ret) {
1450ba55f2f5SFrançois Tigeot 		DRM_ERROR("Failed to allocate ringbuffer %s: %d\n", ring->name, ret);
1451ba55f2f5SFrançois Tigeot 		goto error;
1452ba55f2f5SFrançois Tigeot 	}
1453e3adcf8fSFrançois Tigeot 
1454e3adcf8fSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
1455e3adcf8fSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
1456e3adcf8fSFrançois Tigeot 	 * of the buffer.
1457e3adcf8fSFrançois Tigeot 	 */
1458ba55f2f5SFrançois Tigeot 	ringbuf->effective_size = ringbuf->size;
1459ba55f2f5SFrançois Tigeot 	if (IS_I830(dev) || IS_845G(dev))
1460ba55f2f5SFrançois Tigeot 		ringbuf->effective_size -= 2 * CACHELINE_BYTES;
1461ba55f2f5SFrançois Tigeot 
1462ba55f2f5SFrançois Tigeot 	ret = i915_cmd_parser_init_ring(ring);
1463ba55f2f5SFrançois Tigeot 	if (ret)
1464ba55f2f5SFrançois Tigeot 		goto error;
1465ba55f2f5SFrançois Tigeot 
1466ba55f2f5SFrançois Tigeot 	ret = ring->init(ring);
1467ba55f2f5SFrançois Tigeot 	if (ret)
1468ba55f2f5SFrançois Tigeot 		goto error;
1469e3adcf8fSFrançois Tigeot 
1470e3adcf8fSFrançois Tigeot 	return 0;
1471e3adcf8fSFrançois Tigeot 
1472ba55f2f5SFrançois Tigeot error:
1473ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
1474ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
1475e3adcf8fSFrançois Tigeot 	return ret;
1476e3adcf8fSFrançois Tigeot }
1477e3adcf8fSFrançois Tigeot 
1478ba55f2f5SFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
1479e3adcf8fSFrançois Tigeot {
1480ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ring->dev);
1481ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1482e3adcf8fSFrançois Tigeot 
1483ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
1484e3adcf8fSFrançois Tigeot 		return;
1485e3adcf8fSFrançois Tigeot 
1486ba55f2f5SFrançois Tigeot 	intel_stop_ring_buffer(ring);
1487ba55f2f5SFrançois Tigeot 	WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
1488b030f26bSFrançois Tigeot 
1489ba55f2f5SFrançois Tigeot 	pmap_unmapdev((vm_offset_t)ringbuf->virtual_start, ringbuf->size);
1490e3adcf8fSFrançois Tigeot 
1491ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(ringbuf->obj);
1492ba55f2f5SFrançois Tigeot 	drm_gem_object_unreference(&ringbuf->obj->base);
1493ba55f2f5SFrançois Tigeot 	ringbuf->obj = NULL;
14949edbd4a0SFrançois Tigeot 	ring->preallocated_lazy_request = NULL;
14959edbd4a0SFrançois Tigeot 	ring->outstanding_lazy_seqno = 0;
1496e3adcf8fSFrançois Tigeot 
1497e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
1498e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
1499e3adcf8fSFrançois Tigeot 
1500e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1501ba55f2f5SFrançois Tigeot 
1502ba55f2f5SFrançois Tigeot 	i915_cmd_parser_fini_ring(ring);
1503ba55f2f5SFrançois Tigeot 
1504ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
1505ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
1506e3adcf8fSFrançois Tigeot }
1507e3adcf8fSFrançois Tigeot 
1508ba55f2f5SFrançois Tigeot static int intel_ring_wait_request(struct intel_engine_cs *ring, int n)
1509e3adcf8fSFrançois Tigeot {
1510ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1511e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
1512e3adcf8fSFrançois Tigeot 	u32 seqno = 0;
1513e3adcf8fSFrançois Tigeot 	int ret;
1514e3adcf8fSFrançois Tigeot 
1515ba55f2f5SFrançois Tigeot 	if (ringbuf->last_retired_head != -1) {
1516ba55f2f5SFrançois Tigeot 		ringbuf->head = ringbuf->last_retired_head;
1517ba55f2f5SFrançois Tigeot 		ringbuf->last_retired_head = -1;
1518e3adcf8fSFrançois Tigeot 
1519ba55f2f5SFrançois Tigeot 		ringbuf->space = ring_space(ring);
1520ba55f2f5SFrançois Tigeot 		if (ringbuf->space >= n)
1521e3adcf8fSFrançois Tigeot 			return 0;
1522e3adcf8fSFrançois Tigeot 	}
1523e3adcf8fSFrançois Tigeot 
1524e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
1525ba55f2f5SFrançois Tigeot 		if (__ring_space(request->tail, ringbuf->tail, ringbuf->size) >= n) {
1526e3adcf8fSFrançois Tigeot 			seqno = request->seqno;
1527e3adcf8fSFrançois Tigeot 			break;
1528e3adcf8fSFrançois Tigeot 		}
1529e3adcf8fSFrançois Tigeot 	}
1530e3adcf8fSFrançois Tigeot 
1531e3adcf8fSFrançois Tigeot 	if (seqno == 0)
1532e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1533e3adcf8fSFrançois Tigeot 
1534ba55f2f5SFrançois Tigeot 	ret = i915_wait_seqno(ring, seqno);
1535e3adcf8fSFrançois Tigeot 	if (ret)
1536e3adcf8fSFrançois Tigeot 		return ret;
1537e3adcf8fSFrançois Tigeot 
1538ba55f2f5SFrançois Tigeot 	i915_gem_retire_requests_ring(ring);
1539ba55f2f5SFrançois Tigeot 	ringbuf->head = ringbuf->last_retired_head;
1540ba55f2f5SFrançois Tigeot 	ringbuf->last_retired_head = -1;
1541e3adcf8fSFrançois Tigeot 
1542ba55f2f5SFrançois Tigeot 	ringbuf->space = ring_space(ring);
1543e3adcf8fSFrançois Tigeot 	return 0;
1544e3adcf8fSFrançois Tigeot }
1545e3adcf8fSFrançois Tigeot 
1546ba55f2f5SFrançois Tigeot static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
1547e3adcf8fSFrançois Tigeot {
1548e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1549e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1550ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1551245593daSFrançois Tigeot 	unsigned long end;
1552e3adcf8fSFrançois Tigeot 	int ret;
1553e3adcf8fSFrançois Tigeot 
1554e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_request(ring, n);
1555e3adcf8fSFrançois Tigeot 	if (ret != -ENOSPC)
1556e3adcf8fSFrançois Tigeot 		return ret;
1557e3adcf8fSFrançois Tigeot 
15589edbd4a0SFrançois Tigeot 	/* force the tail write in case we have been skipping them */
15599edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
15609edbd4a0SFrançois Tigeot 
1561e3adcf8fSFrançois Tigeot 	/* With GEM the hangcheck timer should kick us out of the loop,
1562e3adcf8fSFrançois Tigeot 	 * leaving it early runs the risk of corrupting GEM state (due
1563e3adcf8fSFrançois Tigeot 	 * to running on almost untested codepaths). But on resume
1564e3adcf8fSFrançois Tigeot 	 * timers don't work yet, so prevent a complete hang in that
1565e3adcf8fSFrançois Tigeot 	 * case by choosing an insanely large timeout. */
1566e3440f96SFrançois Tigeot 	end = jiffies + 60 * HZ;
1567245593daSFrançois Tigeot 
1568ba55f2f5SFrançois Tigeot 	trace_i915_ring_wait_begin(ring);
1569e3adcf8fSFrançois Tigeot 	do {
1570ba55f2f5SFrançois Tigeot 		ringbuf->head = I915_READ_HEAD(ring);
1571ba55f2f5SFrançois Tigeot 		ringbuf->space = ring_space(ring);
1572ba55f2f5SFrançois Tigeot 		if (ringbuf->space >= n) {
1573ba55f2f5SFrançois Tigeot 			ret = 0;
1574ba55f2f5SFrançois Tigeot 			break;
1575e3adcf8fSFrançois Tigeot 		}
1576e3adcf8fSFrançois Tigeot 
1577e3adcf8fSFrançois Tigeot #if 0
1578ba55f2f5SFrançois Tigeot 		if (!drm_core_check_feature(dev, DRIVER_MODESET) &&
1579ba55f2f5SFrançois Tigeot 		    dev->primary->master) {
1580e3adcf8fSFrançois Tigeot 			struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1581e3adcf8fSFrançois Tigeot 			if (master_priv->sarea_priv)
1582e3adcf8fSFrançois Tigeot 				master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1583e3adcf8fSFrançois Tigeot 		}
1584e3adcf8fSFrançois Tigeot #else
1585e3adcf8fSFrançois Tigeot 		if (dev_priv->sarea_priv)
1586e3adcf8fSFrançois Tigeot 			dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1587e3adcf8fSFrançois Tigeot #endif
1588e3adcf8fSFrançois Tigeot 
1589ba55f2f5SFrançois Tigeot 
1590e3440f96SFrançois Tigeot 		msleep(1);
1591245593daSFrançois Tigeot 
1592ba55f2f5SFrançois Tigeot #if 0
1593ba55f2f5SFrançois Tigeot 		if (dev_priv->mm.interruptible && signal_pending(current)) {
1594ba55f2f5SFrançois Tigeot 			ret = -ERESTARTSYS;
1595ba55f2f5SFrançois Tigeot 			break;
1596ba55f2f5SFrançois Tigeot 		}
1597ba55f2f5SFrançois Tigeot #endif
1598ba55f2f5SFrançois Tigeot 
1599a2fdbec6SFrançois Tigeot 		ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1600a2fdbec6SFrançois Tigeot 					   dev_priv->mm.interruptible);
1601245593daSFrançois Tigeot 		if (ret)
1602ba55f2f5SFrançois Tigeot 			break;
1603ba55f2f5SFrançois Tigeot 
1604ba55f2f5SFrançois Tigeot 		if (time_after(jiffies, end)) {
1605ba55f2f5SFrançois Tigeot 			ret = -EBUSY;
1606ba55f2f5SFrançois Tigeot 			break;
1607ba55f2f5SFrançois Tigeot 		}
1608ba55f2f5SFrançois Tigeot 	} while (1);
1609a2fdbec6SFrançois Tigeot 	trace_i915_ring_wait_end(ring);
1610ba55f2f5SFrançois Tigeot 	return ret;
1611e3adcf8fSFrançois Tigeot }
1612e3adcf8fSFrançois Tigeot 
1613ba55f2f5SFrançois Tigeot static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
1614b030f26bSFrançois Tigeot {
1615b030f26bSFrançois Tigeot 	uint32_t __iomem *virt;
1616ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1617ba55f2f5SFrançois Tigeot 	int rem = ringbuf->size - ringbuf->tail;
1618b030f26bSFrançois Tigeot 
1619ba55f2f5SFrançois Tigeot 	if (ringbuf->space < rem) {
1620b030f26bSFrançois Tigeot 		int ret = ring_wait_for_space(ring, rem);
1621b030f26bSFrançois Tigeot 		if (ret)
1622b030f26bSFrançois Tigeot 			return ret;
1623b030f26bSFrançois Tigeot 	}
1624b030f26bSFrançois Tigeot 
1625ba55f2f5SFrançois Tigeot 	virt = (unsigned int *)((char *)ringbuf->virtual_start + ringbuf->tail);
1626b030f26bSFrançois Tigeot 	rem /= 4;
1627b030f26bSFrançois Tigeot 	while (rem--)
1628686a02f1SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
1629b030f26bSFrançois Tigeot 
1630ba55f2f5SFrançois Tigeot 	ringbuf->tail = 0;
1631ba55f2f5SFrançois Tigeot 	ringbuf->space = ring_space(ring);
1632b030f26bSFrançois Tigeot 
1633b030f26bSFrançois Tigeot 	return 0;
1634b030f26bSFrançois Tigeot }
1635b030f26bSFrançois Tigeot 
1636ba55f2f5SFrançois Tigeot int intel_ring_idle(struct intel_engine_cs *ring)
1637b030f26bSFrançois Tigeot {
1638b5c29a34SFrançois Tigeot 	u32 seqno;
1639b5c29a34SFrançois Tigeot 	int ret;
1640b5c29a34SFrançois Tigeot 
1641b5c29a34SFrançois Tigeot 	/* We need to add any requests required to flush the objects and ring */
16429edbd4a0SFrançois Tigeot 	if (ring->outstanding_lazy_seqno) {
16435d0b1887SFrançois Tigeot 		ret = i915_add_request(ring, NULL);
1644b5c29a34SFrançois Tigeot 		if (ret)
1645b5c29a34SFrançois Tigeot 			return ret;
1646b5c29a34SFrançois Tigeot 	}
1647b5c29a34SFrançois Tigeot 
1648b5c29a34SFrançois Tigeot 	/* Wait upon the last request to be completed */
1649b5c29a34SFrançois Tigeot 	if (list_empty(&ring->request_list))
1650b5c29a34SFrançois Tigeot 		return 0;
1651b5c29a34SFrançois Tigeot 
1652b5c29a34SFrançois Tigeot 	seqno = list_entry(ring->request_list.prev,
1653b5c29a34SFrançois Tigeot 			   struct drm_i915_gem_request,
1654b5c29a34SFrançois Tigeot 			   list)->seqno;
1655b5c29a34SFrançois Tigeot 
1656b5c29a34SFrançois Tigeot 	return i915_wait_seqno(ring, seqno);
1657b5c29a34SFrançois Tigeot }
1658b5c29a34SFrançois Tigeot 
1659b5c29a34SFrançois Tigeot static int
1660ba55f2f5SFrançois Tigeot intel_ring_alloc_seqno(struct intel_engine_cs *ring)
1661b5c29a34SFrançois Tigeot {
16629edbd4a0SFrançois Tigeot 	if (ring->outstanding_lazy_seqno)
1663b5c29a34SFrançois Tigeot 		return 0;
1664b5c29a34SFrançois Tigeot 
16659edbd4a0SFrançois Tigeot 	if (ring->preallocated_lazy_request == NULL) {
16669edbd4a0SFrançois Tigeot 		struct drm_i915_gem_request *request;
16679edbd4a0SFrançois Tigeot 
16689edbd4a0SFrançois Tigeot 		request = kmalloc(sizeof(*request), M_DRM, M_WAITOK);
16699edbd4a0SFrançois Tigeot 		if (request == NULL)
16709edbd4a0SFrançois Tigeot 			return -ENOMEM;
16719edbd4a0SFrançois Tigeot 
16729edbd4a0SFrançois Tigeot 		ring->preallocated_lazy_request = request;
1673b030f26bSFrançois Tigeot 	}
1674b030f26bSFrançois Tigeot 
16759edbd4a0SFrançois Tigeot 	return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
16769edbd4a0SFrançois Tigeot }
16779edbd4a0SFrançois Tigeot 
1678ba55f2f5SFrançois Tigeot static int __intel_ring_prepare(struct intel_engine_cs *ring,
1679a2fdbec6SFrançois Tigeot 				int bytes)
1680a2fdbec6SFrançois Tigeot {
1681ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1682a2fdbec6SFrançois Tigeot 	int ret;
1683a2fdbec6SFrançois Tigeot 
1684ba55f2f5SFrançois Tigeot 	if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
1685a2fdbec6SFrançois Tigeot 		ret = intel_wrap_ring_buffer(ring);
1686a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
1687a2fdbec6SFrançois Tigeot 			return ret;
1688a2fdbec6SFrançois Tigeot 	}
1689a2fdbec6SFrançois Tigeot 
1690ba55f2f5SFrançois Tigeot 	if (unlikely(ringbuf->space < bytes)) {
1691a2fdbec6SFrançois Tigeot 		ret = ring_wait_for_space(ring, bytes);
1692a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
1693a2fdbec6SFrançois Tigeot 			return ret;
1694a2fdbec6SFrançois Tigeot 	}
1695a2fdbec6SFrançois Tigeot 
1696a2fdbec6SFrançois Tigeot 	return 0;
1697a2fdbec6SFrançois Tigeot }
1698a2fdbec6SFrançois Tigeot 
1699ba55f2f5SFrançois Tigeot int intel_ring_begin(struct intel_engine_cs *ring,
1700e3adcf8fSFrançois Tigeot 		     int num_dwords)
1701e3adcf8fSFrançois Tigeot {
1702ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1703e3adcf8fSFrançois Tigeot 	int ret;
1704e3adcf8fSFrançois Tigeot 
1705a2fdbec6SFrançois Tigeot 	ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1706a2fdbec6SFrançois Tigeot 				   dev_priv->mm.interruptible);
1707245593daSFrançois Tigeot 	if (ret)
1708245593daSFrançois Tigeot 		return ret;
1709e3adcf8fSFrançois Tigeot 
17109edbd4a0SFrançois Tigeot 	ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
17119edbd4a0SFrançois Tigeot 	if (ret)
17129edbd4a0SFrançois Tigeot 		return ret;
17139edbd4a0SFrançois Tigeot 
1714b5c29a34SFrançois Tigeot 	/* Preallocate the olr before touching the ring */
1715b5c29a34SFrançois Tigeot 	ret = intel_ring_alloc_seqno(ring);
1716b5c29a34SFrançois Tigeot 	if (ret)
1717b5c29a34SFrançois Tigeot 		return ret;
1718b5c29a34SFrançois Tigeot 
1719ba55f2f5SFrançois Tigeot 	ring->buffer->space -= num_dwords * sizeof(uint32_t);
17209edbd4a0SFrançois Tigeot 	return 0;
17219edbd4a0SFrançois Tigeot }
17229edbd4a0SFrançois Tigeot 
17239edbd4a0SFrançois Tigeot /* Align the ring tail to a cacheline boundary */
1724ba55f2f5SFrançois Tigeot int intel_ring_cacheline_align(struct intel_engine_cs *ring)
17259edbd4a0SFrançois Tigeot {
1726ba55f2f5SFrançois Tigeot 	int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
17279edbd4a0SFrançois Tigeot 	int ret;
17289edbd4a0SFrançois Tigeot 
17299edbd4a0SFrançois Tigeot 	if (num_dwords == 0)
17309edbd4a0SFrançois Tigeot 		return 0;
17319edbd4a0SFrançois Tigeot 
1732ba55f2f5SFrançois Tigeot 	num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
17339edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, num_dwords);
17349edbd4a0SFrançois Tigeot 	if (ret)
17359edbd4a0SFrançois Tigeot 		return ret;
17369edbd4a0SFrançois Tigeot 
17379edbd4a0SFrançois Tigeot 	while (num_dwords--)
17389edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
17399edbd4a0SFrançois Tigeot 
17409edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
17419edbd4a0SFrançois Tigeot 
17429edbd4a0SFrançois Tigeot 	return 0;
1743e3adcf8fSFrançois Tigeot }
1744e3adcf8fSFrançois Tigeot 
1745ba55f2f5SFrançois Tigeot void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
1746a2fdbec6SFrançois Tigeot {
1747a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1748a2fdbec6SFrançois Tigeot 
17499edbd4a0SFrançois Tigeot 	BUG_ON(ring->outstanding_lazy_seqno);
1750a2fdbec6SFrançois Tigeot 
1751a2fdbec6SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 6) {
1752a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
1753a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
17549edbd4a0SFrançois Tigeot 		if (HAS_VEBOX(ring->dev))
17559edbd4a0SFrançois Tigeot 			I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
1756e3adcf8fSFrançois Tigeot 	}
1757e3adcf8fSFrançois Tigeot 
1758a2fdbec6SFrançois Tigeot 	ring->set_seqno(ring, seqno);
17595d0b1887SFrançois Tigeot 	ring->hangcheck.seqno = seqno;
1760e3adcf8fSFrançois Tigeot }
1761e3adcf8fSFrançois Tigeot 
1762ba55f2f5SFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
1763f4e1c372SFrançois Tigeot 				     u32 value)
1764e3adcf8fSFrançois Tigeot {
1765ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1766e3adcf8fSFrançois Tigeot 
1767e3adcf8fSFrançois Tigeot        /* Every tail move must follow the sequence below */
1768f4e1c372SFrançois Tigeot 
1769f4e1c372SFrançois Tigeot 	/* Disable notification that the ring is IDLE. The GT
1770f4e1c372SFrançois Tigeot 	 * will then assume that it is busy and bring it out of rc6.
1771f4e1c372SFrançois Tigeot 	 */
1772e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1773f4e1c372SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1774e3adcf8fSFrançois Tigeot 
1775f4e1c372SFrançois Tigeot 	/* Clear the context id. Here be magic! */
1776f4e1c372SFrançois Tigeot 	I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1777e3adcf8fSFrançois Tigeot 
1778f4e1c372SFrançois Tigeot 	/* Wait for the ring not to be idle, i.e. for it to wake up. */
1779f4e1c372SFrançois Tigeot 	if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1780f4e1c372SFrançois Tigeot 		      GEN6_BSD_SLEEP_INDICATOR) == 0,
1781f4e1c372SFrançois Tigeot 		     50))
1782f4e1c372SFrançois Tigeot 		DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
1783f4e1c372SFrançois Tigeot 
1784f4e1c372SFrançois Tigeot 	/* Now that the ring is fully powered up, update the tail */
1785e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
1786f4e1c372SFrançois Tigeot 	POSTING_READ(RING_TAIL(ring->mmio_base));
1787f4e1c372SFrançois Tigeot 
1788f4e1c372SFrançois Tigeot 	/* Let the ring send IDLE messages to the GT again,
1789f4e1c372SFrançois Tigeot 	 * and so let it sleep to conserve power when idle.
1790f4e1c372SFrançois Tigeot 	 */
1791e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1792f4e1c372SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1793e3adcf8fSFrançois Tigeot }
1794e3adcf8fSFrançois Tigeot 
1795ba55f2f5SFrançois Tigeot static int gen6_bsd_ring_flush(struct intel_engine_cs *ring,
1796b5c29a34SFrançois Tigeot 			       u32 invalidate, u32 flush)
1797e3adcf8fSFrançois Tigeot {
1798e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1799e3adcf8fSFrançois Tigeot 	int ret;
1800e3adcf8fSFrançois Tigeot 
1801e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1802e3adcf8fSFrançois Tigeot 	if (ret)
1803e3adcf8fSFrançois Tigeot 		return ret;
1804e3adcf8fSFrançois Tigeot 
1805e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
18069edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
18079edbd4a0SFrançois Tigeot 		cmd += 1;
1808b5c29a34SFrançois Tigeot 	/*
1809b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.5 - video engine command streamer:
1810b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
1811b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
1812b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
1813b5c29a34SFrançois Tigeot 	 */
1814e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
1815b5c29a34SFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1816b5c29a34SFrançois Tigeot 			MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1817e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1818b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
18199edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
18209edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
18219edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
18229edbd4a0SFrançois Tigeot 	} else  {
18239edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0);
18249edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
18259edbd4a0SFrançois Tigeot 	}
18269edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
18279edbd4a0SFrançois Tigeot 	return 0;
18289edbd4a0SFrançois Tigeot }
18299edbd4a0SFrançois Tigeot 
18309edbd4a0SFrançois Tigeot static int
1831ba55f2f5SFrançois Tigeot gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
1832ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
18339edbd4a0SFrançois Tigeot 			      unsigned flags)
18349edbd4a0SFrançois Tigeot {
18359edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18369edbd4a0SFrançois Tigeot 	bool ppgtt = dev_priv->mm.aliasing_ppgtt != NULL &&
18379edbd4a0SFrançois Tigeot 		!(flags & I915_DISPATCH_SECURE);
18389edbd4a0SFrançois Tigeot 	int ret;
18399edbd4a0SFrançois Tigeot 
18409edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
18419edbd4a0SFrançois Tigeot 	if (ret)
18429edbd4a0SFrançois Tigeot 		return ret;
18439edbd4a0SFrançois Tigeot 
18449edbd4a0SFrançois Tigeot 	/* FIXME(BDW): Address space and security selectors. */
18459edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
1846ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, lower_32_bits(offset));
1847ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, upper_32_bits(offset));
1848e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1849e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
18509edbd4a0SFrançois Tigeot 
1851e3adcf8fSFrançois Tigeot 	return 0;
1852e3adcf8fSFrançois Tigeot }
1853e3adcf8fSFrançois Tigeot 
1854e3adcf8fSFrançois Tigeot static int
1855ba55f2f5SFrançois Tigeot hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
1856ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
1857b5c29a34SFrançois Tigeot 			      unsigned flags)
1858e3adcf8fSFrançois Tigeot {
1859e3adcf8fSFrançois Tigeot 	int ret;
1860e3adcf8fSFrançois Tigeot 
1861e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1862e3adcf8fSFrançois Tigeot 	if (ret)
1863e3adcf8fSFrançois Tigeot 		return ret;
1864e3adcf8fSFrançois Tigeot 
1865b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
1866b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
1867b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
1868b5c29a34SFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1869b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset);
1870b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
1871b5c29a34SFrançois Tigeot 
1872b5c29a34SFrançois Tigeot 	return 0;
1873b5c29a34SFrançois Tigeot }
1874b5c29a34SFrançois Tigeot 
1875b5c29a34SFrançois Tigeot static int
1876ba55f2f5SFrançois Tigeot gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
1877ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
1878b5c29a34SFrançois Tigeot 			      unsigned flags)
1879b5c29a34SFrançois Tigeot {
1880b5c29a34SFrançois Tigeot 	int ret;
1881b5c29a34SFrançois Tigeot 
1882b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1883b5c29a34SFrançois Tigeot 	if (ret)
1884b5c29a34SFrançois Tigeot 		return ret;
1885b5c29a34SFrançois Tigeot 
1886b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
1887b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1888b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
1889e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1890e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1891e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1892e3adcf8fSFrançois Tigeot 
1893e3adcf8fSFrançois Tigeot 	return 0;
1894e3adcf8fSFrançois Tigeot }
1895e3adcf8fSFrançois Tigeot 
1896e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
1897e3adcf8fSFrançois Tigeot 
1898ba55f2f5SFrançois Tigeot static int gen6_ring_flush(struct intel_engine_cs *ring,
1899b5c29a34SFrançois Tigeot 			   u32 invalidate, u32 flush)
1900e3adcf8fSFrançois Tigeot {
19015d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1902e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1903e3adcf8fSFrançois Tigeot 	int ret;
1904e3adcf8fSFrançois Tigeot 
1905e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1906e3adcf8fSFrançois Tigeot 	if (ret)
1907e3adcf8fSFrançois Tigeot 		return ret;
1908e3adcf8fSFrançois Tigeot 
1909e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
19109edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
19119edbd4a0SFrançois Tigeot 		cmd += 1;
1912b5c29a34SFrançois Tigeot 	/*
1913b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.3 - blitter engine command streamer:
1914b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
1915b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
1916b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
1917b5c29a34SFrançois Tigeot 	 */
1918e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
1919b5c29a34SFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
1920b5c29a34SFrançois Tigeot 			MI_FLUSH_DW_OP_STOREDW;
1921e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1922b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
19239edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
19249edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
19259edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
19269edbd4a0SFrançois Tigeot 	} else  {
1927e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, 0);
1928e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
19299edbd4a0SFrançois Tigeot 	}
1930e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
19315d0b1887SFrançois Tigeot 
19329edbd4a0SFrançois Tigeot 	if (IS_GEN7(dev) && !invalidate && flush)
19335d0b1887SFrançois Tigeot 		return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
19345d0b1887SFrançois Tigeot 
1935e3adcf8fSFrançois Tigeot 	return 0;
1936e3adcf8fSFrançois Tigeot }
1937e3adcf8fSFrançois Tigeot 
1938e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
1939e3adcf8fSFrançois Tigeot {
1940ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1941ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
1942e3adcf8fSFrançois Tigeot 
1943686a02f1SFrançois Tigeot 	ring->name = "render ring";
1944686a02f1SFrançois Tigeot 	ring->id = RCS;
1945686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
1946686a02f1SFrançois Tigeot 
1947e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
1948e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
1949b5c29a34SFrançois Tigeot 		ring->flush = gen7_render_ring_flush;
1950b5c29a34SFrançois Tigeot 		if (INTEL_INFO(dev)->gen == 6)
1951e3adcf8fSFrançois Tigeot 			ring->flush = gen6_render_ring_flush;
19529edbd4a0SFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 8) {
19539edbd4a0SFrançois Tigeot 			ring->flush = gen8_render_ring_flush;
19549edbd4a0SFrançois Tigeot 			ring->irq_get = gen8_ring_get_irq;
19559edbd4a0SFrançois Tigeot 			ring->irq_put = gen8_ring_put_irq;
19569edbd4a0SFrançois Tigeot 		} else {
1957686a02f1SFrançois Tigeot 			ring->irq_get = gen6_ring_get_irq;
1958686a02f1SFrançois Tigeot 			ring->irq_put = gen6_ring_put_irq;
19599edbd4a0SFrançois Tigeot 		}
19605d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
1961e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
1962a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
1963ba55f2f5SFrançois Tigeot 		ring->semaphore.sync_to = gen6_ring_sync;
1964ba55f2f5SFrançois Tigeot 		ring->semaphore.signal = gen6_signal;
1965ba55f2f5SFrançois Tigeot 		/*
1966ba55f2f5SFrançois Tigeot 		 * The current semaphore is only applied on pre-gen8 platform.
1967ba55f2f5SFrançois Tigeot 		 * And there is no VCS2 ring on the pre-gen8 platform. So the
1968ba55f2f5SFrançois Tigeot 		 * semaphore between RCS and VCS2 is initialized as INVALID.
1969ba55f2f5SFrançois Tigeot 		 * Gen8 will initialize the sema between VCS2 and RCS later.
1970ba55f2f5SFrançois Tigeot 		 */
1971ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
1972ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
1973ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
1974ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
1975ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
1976ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
1977ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
1978ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
1979ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
1980ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
1981e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
1982e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
1983686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
1984e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
1985a2fdbec6SFrançois Tigeot 		ring->set_seqno = pc_render_set_seqno;
1986686a02f1SFrançois Tigeot 		ring->irq_get = gen5_ring_get_irq;
1987686a02f1SFrançois Tigeot 		ring->irq_put = gen5_ring_put_irq;
19885d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
19895d0b1887SFrançois Tigeot 					GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
1990686a02f1SFrançois Tigeot 	} else {
1991686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
1992686a02f1SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 4)
1993686a02f1SFrançois Tigeot 			ring->flush = gen2_render_ring_flush;
1994686a02f1SFrançois Tigeot 		else
1995686a02f1SFrançois Tigeot 			ring->flush = gen4_render_ring_flush;
1996686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
1997a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
1998686a02f1SFrançois Tigeot 		if (IS_GEN2(dev)) {
1999686a02f1SFrançois Tigeot 			ring->irq_get = i8xx_ring_get_irq;
2000686a02f1SFrançois Tigeot 			ring->irq_put = i8xx_ring_put_irq;
2001686a02f1SFrançois Tigeot 		} else {
2002686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2003686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2004e3adcf8fSFrançois Tigeot 		}
2005686a02f1SFrançois Tigeot 		ring->irq_enable_mask = I915_USER_INTERRUPT;
2006686a02f1SFrançois Tigeot 	}
2007686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2008b5c29a34SFrançois Tigeot 	if (IS_HASWELL(dev))
2009b5c29a34SFrançois Tigeot 		ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
20109edbd4a0SFrançois Tigeot 	else if (IS_GEN8(dev))
20119edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2012b5c29a34SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 6)
2013686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2014686a02f1SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 4)
2015686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2016686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
2017686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2018686a02f1SFrançois Tigeot 	else
2019686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
2020686a02f1SFrançois Tigeot 	ring->init = init_render_ring;
2021686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
2022e3adcf8fSFrançois Tigeot 
2023b5c29a34SFrançois Tigeot 	/* Workaround batchbuffer to combat CS tlb bug. */
2024b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev)) {
2025b5c29a34SFrançois Tigeot 		struct drm_i915_gem_object *obj;
2026b5c29a34SFrançois Tigeot 		int ret;
2027b5c29a34SFrançois Tigeot 
2028b5c29a34SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
2029b5c29a34SFrançois Tigeot 		if (obj == NULL) {
2030b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to allocate batch bo\n");
2031b5c29a34SFrançois Tigeot 			return -ENOMEM;
2032b5c29a34SFrançois Tigeot 		}
2033b5c29a34SFrançois Tigeot 
2034ba55f2f5SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
2035b5c29a34SFrançois Tigeot 		if (ret != 0) {
2036b5c29a34SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
2037b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to ping batch bo\n");
2038b5c29a34SFrançois Tigeot 			return ret;
2039b5c29a34SFrançois Tigeot 		}
2040b5c29a34SFrançois Tigeot 
20419edbd4a0SFrançois Tigeot 		ring->scratch.obj = obj;
20429edbd4a0SFrançois Tigeot 		ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
2043e3adcf8fSFrançois Tigeot 	}
2044e3adcf8fSFrançois Tigeot 
2045e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2046e3adcf8fSFrançois Tigeot }
2047e3adcf8fSFrançois Tigeot 
2048686a02f1SFrançois Tigeot int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
2049e3adcf8fSFrançois Tigeot {
2050ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2051ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
2052ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2053b5c29a34SFrançois Tigeot 	int ret;
2054e3adcf8fSFrançois Tigeot 
2055ba55f2f5SFrançois Tigeot 	if (ringbuf == NULL) {
2056ba55f2f5SFrançois Tigeot 		ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2057ba55f2f5SFrançois Tigeot 		if (!ringbuf)
2058ba55f2f5SFrançois Tigeot 			return -ENOMEM;
2059ba55f2f5SFrançois Tigeot 		ring->buffer = ringbuf;
2060ba55f2f5SFrançois Tigeot 	}
2061ba55f2f5SFrançois Tigeot 
2062686a02f1SFrançois Tigeot 	ring->name = "render ring";
2063686a02f1SFrançois Tigeot 	ring->id = RCS;
2064686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
2065686a02f1SFrançois Tigeot 
2066e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
2067686a02f1SFrançois Tigeot 		/* non-kms not supported on gen6+ */
2068ba55f2f5SFrançois Tigeot 		ret = -ENODEV;
2069ba55f2f5SFrançois Tigeot 		goto err_ringbuf;
2070e3adcf8fSFrançois Tigeot 	}
2071e3adcf8fSFrançois Tigeot 
2072686a02f1SFrançois Tigeot 	/* Note: gem is not supported on gen5/ilk without kms (the corresponding
2073686a02f1SFrançois Tigeot 	 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
2074686a02f1SFrançois Tigeot 	 * the special gen5 functions. */
2075686a02f1SFrançois Tigeot 	ring->add_request = i9xx_add_request;
2076686a02f1SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 4)
2077686a02f1SFrançois Tigeot 		ring->flush = gen2_render_ring_flush;
2078686a02f1SFrançois Tigeot 	else
2079686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
2080686a02f1SFrançois Tigeot 	ring->get_seqno = ring_get_seqno;
2081a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
2082686a02f1SFrançois Tigeot 	if (IS_GEN2(dev)) {
2083686a02f1SFrançois Tigeot 		ring->irq_get = i8xx_ring_get_irq;
2084686a02f1SFrançois Tigeot 		ring->irq_put = i8xx_ring_put_irq;
2085686a02f1SFrançois Tigeot 	} else {
2086686a02f1SFrançois Tigeot 		ring->irq_get = i9xx_ring_get_irq;
2087686a02f1SFrançois Tigeot 		ring->irq_put = i9xx_ring_put_irq;
2088686a02f1SFrançois Tigeot 	}
2089686a02f1SFrançois Tigeot 	ring->irq_enable_mask = I915_USER_INTERRUPT;
2090686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2091686a02f1SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4)
2092686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2093686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
2094686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2095686a02f1SFrançois Tigeot 	else
2096686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
2097686a02f1SFrançois Tigeot 	ring->init = init_render_ring;
2098686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
2099686a02f1SFrançois Tigeot 
2100e3adcf8fSFrançois Tigeot 	ring->dev = dev;
2101e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
2102e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
2103e3adcf8fSFrançois Tigeot 
2104ba55f2f5SFrançois Tigeot 	ringbuf->size = size;
2105ba55f2f5SFrançois Tigeot 	ringbuf->effective_size = ringbuf->size;
2106b5c29a34SFrançois Tigeot 	if (IS_I830(ring->dev) || IS_845G(ring->dev))
2107ba55f2f5SFrançois Tigeot 		ringbuf->effective_size -= 2 * CACHELINE_BYTES;
2108e3adcf8fSFrançois Tigeot 
2109ba55f2f5SFrançois Tigeot 	ringbuf->virtual_start = ioremap_wc(start, size);
2110ba55f2f5SFrançois Tigeot 	if (ringbuf->virtual_start == NULL) {
2111e3adcf8fSFrançois Tigeot 		DRM_ERROR("can not ioremap virtual address for"
2112e3adcf8fSFrançois Tigeot 			  " ring buffer\n");
2113ba55f2f5SFrançois Tigeot 		ret = -ENOMEM;
2114ba55f2f5SFrançois Tigeot 		goto err_ringbuf;
2115e3adcf8fSFrançois Tigeot 	}
2116e3adcf8fSFrançois Tigeot 
2117b5c29a34SFrançois Tigeot 	if (!I915_NEED_GFX_HWS(dev)) {
21185d0b1887SFrançois Tigeot 		ret = init_phys_status_page(ring);
2119b5c29a34SFrançois Tigeot 		if (ret)
2120ba55f2f5SFrançois Tigeot 			goto err_vstart;
2121b5c29a34SFrançois Tigeot 	}
2122b5c29a34SFrançois Tigeot 
2123e3adcf8fSFrançois Tigeot 	return 0;
2124ba55f2f5SFrançois Tigeot 
2125ba55f2f5SFrançois Tigeot err_vstart:
2126ba55f2f5SFrançois Tigeot 	pmap_unmapdev((vm_offset_t)ring->buffer->virtual_start, size);
2127ba55f2f5SFrançois Tigeot err_ringbuf:
2128ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
2129ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
2130ba55f2f5SFrançois Tigeot 	return ret;
2131e3adcf8fSFrançois Tigeot }
2132e3adcf8fSFrançois Tigeot 
2133e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
2134e3adcf8fSFrançois Tigeot {
2135ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2136ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS];
2137e3adcf8fSFrançois Tigeot 
2138686a02f1SFrançois Tigeot 	ring->name = "bsd ring";
2139686a02f1SFrançois Tigeot 	ring->id = VCS;
2140686a02f1SFrançois Tigeot 
2141686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
21429edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
2143686a02f1SFrançois Tigeot 		ring->mmio_base = GEN6_BSD_RING_BASE;
2144686a02f1SFrançois Tigeot 		/* gen6 bsd needs a special wa for tail updates */
2145686a02f1SFrançois Tigeot 		if (IS_GEN6(dev))
2146686a02f1SFrançois Tigeot 			ring->write_tail = gen6_bsd_ring_write_tail;
21475d0b1887SFrançois Tigeot 		ring->flush = gen6_bsd_ring_flush;
2148686a02f1SFrançois Tigeot 		ring->add_request = gen6_add_request;
2149686a02f1SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2150a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
21519edbd4a0SFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 8) {
21529edbd4a0SFrançois Tigeot 			ring->irq_enable_mask =
21539edbd4a0SFrançois Tigeot 				GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
21549edbd4a0SFrançois Tigeot 			ring->irq_get = gen8_ring_get_irq;
21559edbd4a0SFrançois Tigeot 			ring->irq_put = gen8_ring_put_irq;
21569edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
21579edbd4a0SFrançois Tigeot 				gen8_ring_dispatch_execbuffer;
21589edbd4a0SFrançois Tigeot 		} else {
21595d0b1887SFrançois Tigeot 			ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2160686a02f1SFrançois Tigeot 			ring->irq_get = gen6_ring_get_irq;
2161686a02f1SFrançois Tigeot 			ring->irq_put = gen6_ring_put_irq;
21629edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
21639edbd4a0SFrançois Tigeot 				gen6_ring_dispatch_execbuffer;
21649edbd4a0SFrançois Tigeot 		}
2165ba55f2f5SFrançois Tigeot 		ring->semaphore.sync_to = gen6_ring_sync;
2166ba55f2f5SFrançois Tigeot 		ring->semaphore.signal = gen6_signal;
2167ba55f2f5SFrançois Tigeot 		/*
2168ba55f2f5SFrançois Tigeot 		 * The current semaphore is only applied on pre-gen8 platform.
2169ba55f2f5SFrançois Tigeot 		 * And there is no VCS2 ring on the pre-gen8 platform. So the
2170ba55f2f5SFrançois Tigeot 		 * semaphore between VCS and VCS2 is initialized as INVALID.
2171ba55f2f5SFrançois Tigeot 		 * Gen8 will initialize the sema between VCS2 and VCS later.
2172ba55f2f5SFrançois Tigeot 		 */
2173ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2174ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2175ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2176ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2177ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2178ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2179ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2180ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2181ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2182ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2183686a02f1SFrançois Tigeot 	} else {
2184686a02f1SFrançois Tigeot 		ring->mmio_base = BSD_RING_BASE;
2185686a02f1SFrançois Tigeot 		ring->flush = bsd_ring_flush;
2186686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2187686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2188a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2189686a02f1SFrançois Tigeot 		if (IS_GEN5(dev)) {
21905d0b1887SFrançois Tigeot 			ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2191686a02f1SFrançois Tigeot 			ring->irq_get = gen5_ring_get_irq;
2192686a02f1SFrançois Tigeot 			ring->irq_put = gen5_ring_put_irq;
2193686a02f1SFrançois Tigeot 		} else {
2194686a02f1SFrançois Tigeot 			ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2195686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2196686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2197686a02f1SFrançois Tigeot 		}
2198686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2199686a02f1SFrançois Tigeot 	}
2200686a02f1SFrançois Tigeot 	ring->init = init_ring_common;
2201e3adcf8fSFrançois Tigeot 
2202e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2203e3adcf8fSFrançois Tigeot }
2204e3adcf8fSFrançois Tigeot 
2205ba55f2f5SFrançois Tigeot /**
2206ba55f2f5SFrançois Tigeot  * Initialize the second BSD ring for Broadwell GT3.
2207ba55f2f5SFrançois Tigeot  * It is noted that this only exists on Broadwell GT3.
2208ba55f2f5SFrançois Tigeot  */
2209ba55f2f5SFrançois Tigeot int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2210ba55f2f5SFrançois Tigeot {
2211ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2212ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
2213ba55f2f5SFrançois Tigeot 
2214ba55f2f5SFrançois Tigeot 	if ((INTEL_INFO(dev)->gen != 8)) {
2215ba55f2f5SFrançois Tigeot 		DRM_ERROR("No dual-BSD ring on non-BDW machine\n");
2216ba55f2f5SFrançois Tigeot 		return -EINVAL;
2217ba55f2f5SFrançois Tigeot 	}
2218ba55f2f5SFrançois Tigeot 
2219ba55f2f5SFrançois Tigeot 	ring->name = "bds2_ring";
2220ba55f2f5SFrançois Tigeot 	ring->id = VCS2;
2221ba55f2f5SFrançois Tigeot 
2222ba55f2f5SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2223ba55f2f5SFrançois Tigeot 	ring->mmio_base = GEN8_BSD2_RING_BASE;
2224ba55f2f5SFrançois Tigeot 	ring->flush = gen6_bsd_ring_flush;
2225ba55f2f5SFrançois Tigeot 	ring->add_request = gen6_add_request;
2226ba55f2f5SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2227ba55f2f5SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
2228ba55f2f5SFrançois Tigeot 	ring->irq_enable_mask =
2229ba55f2f5SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2230ba55f2f5SFrançois Tigeot 	ring->irq_get = gen8_ring_get_irq;
2231ba55f2f5SFrançois Tigeot 	ring->irq_put = gen8_ring_put_irq;
2232ba55f2f5SFrançois Tigeot 	ring->dispatch_execbuffer =
2233ba55f2f5SFrançois Tigeot 			gen8_ring_dispatch_execbuffer;
2234ba55f2f5SFrançois Tigeot 	ring->semaphore.sync_to = gen6_ring_sync;
2235ba55f2f5SFrançois Tigeot 	ring->semaphore.signal = gen6_signal;
2236ba55f2f5SFrançois Tigeot 	/*
2237ba55f2f5SFrançois Tigeot 	 * The current semaphore is only applied on the pre-gen8. And there
2238ba55f2f5SFrançois Tigeot 	 * is no bsd2 ring on the pre-gen8. So now the semaphore_register
2239ba55f2f5SFrançois Tigeot 	 * between VCS2 and other ring is initialized as invalid.
2240ba55f2f5SFrançois Tigeot 	 * Gen8 will initialize the sema between VCS2 and other ring later.
2241ba55f2f5SFrançois Tigeot 	 */
2242ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2243ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2244ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2245ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2246ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2247ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2248ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2249ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2250ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2251ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2252ba55f2f5SFrançois Tigeot 
2253ba55f2f5SFrançois Tigeot 	ring->init = init_ring_common;
2254ba55f2f5SFrançois Tigeot 
2255ba55f2f5SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2256ba55f2f5SFrançois Tigeot }
2257ba55f2f5SFrançois Tigeot 
2258e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
2259e3adcf8fSFrançois Tigeot {
2260ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2261ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[BCS];
2262e3adcf8fSFrançois Tigeot 
2263686a02f1SFrançois Tigeot 	ring->name = "blitter ring";
2264686a02f1SFrançois Tigeot 	ring->id = BCS;
2265686a02f1SFrançois Tigeot 
2266686a02f1SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
2267686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
22685d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
2269686a02f1SFrançois Tigeot 	ring->add_request = gen6_add_request;
2270686a02f1SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2271a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
22729edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
22739edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
22749edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
22759edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
22769edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
22779edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
22789edbd4a0SFrançois Tigeot 	} else {
22795d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2280686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2281686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
2282686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
22839edbd4a0SFrançois Tigeot 	}
2284ba55f2f5SFrançois Tigeot 	ring->semaphore.sync_to = gen6_ring_sync;
2285ba55f2f5SFrançois Tigeot 	ring->semaphore.signal = gen6_signal;
2286ba55f2f5SFrançois Tigeot 	/*
2287ba55f2f5SFrançois Tigeot 	 * The current semaphore is only applied on pre-gen8 platform. And
2288ba55f2f5SFrançois Tigeot 	 * there is no VCS2 ring on the pre-gen8 platform. So the semaphore
2289ba55f2f5SFrançois Tigeot 	 * between BCS and VCS2 is initialized as INVALID.
2290ba55f2f5SFrançois Tigeot 	 * Gen8 will initialize the sema between BCS and VCS2 later.
2291ba55f2f5SFrançois Tigeot 	 */
2292ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2293ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2294ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2295ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2296ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2297ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2298ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2299ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2300ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2301ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
23025d0b1887SFrançois Tigeot 	ring->init = init_ring_common;
23035d0b1887SFrançois Tigeot 
23045d0b1887SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
23055d0b1887SFrançois Tigeot }
23065d0b1887SFrançois Tigeot 
23075d0b1887SFrançois Tigeot int intel_init_vebox_ring_buffer(struct drm_device *dev)
23085d0b1887SFrançois Tigeot {
2309ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2310ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VECS];
23115d0b1887SFrançois Tigeot 
23125d0b1887SFrançois Tigeot 	ring->name = "video enhancement ring";
23135d0b1887SFrançois Tigeot 	ring->id = VECS;
23145d0b1887SFrançois Tigeot 
23155d0b1887SFrançois Tigeot 	ring->mmio_base = VEBOX_RING_BASE;
23165d0b1887SFrançois Tigeot 	ring->write_tail = ring_write_tail;
23175d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
23185d0b1887SFrançois Tigeot 	ring->add_request = gen6_add_request;
23195d0b1887SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
23205d0b1887SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
23219edbd4a0SFrançois Tigeot 
23229edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
23239edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
23249edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
23259edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
23269edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
23279edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
23289edbd4a0SFrançois Tigeot 	} else {
23299edbd4a0SFrançois Tigeot 		ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
23305d0b1887SFrançois Tigeot 		ring->irq_get = hsw_vebox_get_irq;
23315d0b1887SFrançois Tigeot 		ring->irq_put = hsw_vebox_put_irq;
23325d0b1887SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
23339edbd4a0SFrançois Tigeot 	}
2334ba55f2f5SFrançois Tigeot 	ring->semaphore.sync_to = gen6_ring_sync;
2335ba55f2f5SFrançois Tigeot 	ring->semaphore.signal = gen6_signal;
2336ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2337ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2338ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2339ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2340ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2341ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2342ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2343ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2344ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2345ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2346686a02f1SFrançois Tigeot 	ring->init = init_ring_common;
2347e3adcf8fSFrançois Tigeot 
2348e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2349e3adcf8fSFrançois Tigeot }
2350b030f26bSFrançois Tigeot 
2351b030f26bSFrançois Tigeot int
2352ba55f2f5SFrançois Tigeot intel_ring_flush_all_caches(struct intel_engine_cs *ring)
2353b030f26bSFrançois Tigeot {
2354b030f26bSFrançois Tigeot 	int ret;
2355b030f26bSFrançois Tigeot 
2356b030f26bSFrançois Tigeot 	if (!ring->gpu_caches_dirty)
2357b030f26bSFrançois Tigeot 		return 0;
2358b030f26bSFrançois Tigeot 
2359b030f26bSFrançois Tigeot 	ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2360b030f26bSFrançois Tigeot 	if (ret)
2361b030f26bSFrançois Tigeot 		return ret;
2362b030f26bSFrançois Tigeot 
2363a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2364a2fdbec6SFrançois Tigeot 
2365b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2366b030f26bSFrançois Tigeot 	return 0;
2367b030f26bSFrançois Tigeot }
2368b030f26bSFrançois Tigeot 
2369b030f26bSFrançois Tigeot int
2370ba55f2f5SFrançois Tigeot intel_ring_invalidate_all_caches(struct intel_engine_cs *ring)
2371b030f26bSFrançois Tigeot {
2372b030f26bSFrançois Tigeot 	uint32_t flush_domains;
2373b030f26bSFrançois Tigeot 	int ret;
2374b030f26bSFrançois Tigeot 
2375b030f26bSFrançois Tigeot 	flush_domains = 0;
2376b030f26bSFrançois Tigeot 	if (ring->gpu_caches_dirty)
2377b030f26bSFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
2378b030f26bSFrançois Tigeot 
2379b030f26bSFrançois Tigeot 	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2380b030f26bSFrançois Tigeot 	if (ret)
2381b030f26bSFrançois Tigeot 		return ret;
2382b030f26bSFrançois Tigeot 
2383a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2384a2fdbec6SFrançois Tigeot 
2385b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2386b030f26bSFrançois Tigeot 	return 0;
2387b030f26bSFrançois Tigeot }
2388ba55f2f5SFrançois Tigeot 
2389ba55f2f5SFrançois Tigeot void
2390ba55f2f5SFrançois Tigeot intel_stop_ring_buffer(struct intel_engine_cs *ring)
2391ba55f2f5SFrançois Tigeot {
2392ba55f2f5SFrançois Tigeot 	int ret;
2393ba55f2f5SFrançois Tigeot 
2394ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
2395ba55f2f5SFrançois Tigeot 		return;
2396ba55f2f5SFrançois Tigeot 
2397ba55f2f5SFrançois Tigeot 	ret = intel_ring_idle(ring);
2398ba55f2f5SFrançois Tigeot 	if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2399ba55f2f5SFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2400ba55f2f5SFrançois Tigeot 			  ring->name, ret);
2401ba55f2f5SFrançois Tigeot 
2402ba55f2f5SFrançois Tigeot 	stop_ring(ring);
2403ba55f2f5SFrançois Tigeot }
2404