xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision 0dbf0ea8255afaf7dcbb8572fd681a7a45b3d3ce)
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 
361*0dbf0ea8SMatthew Dillon 		flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
362*0dbf0ea8SMatthew Dillon 
363b5c29a34SFrançois Tigeot 		/* Workaround: we must issue a pipe_control with CS-stall bit
364b5c29a34SFrançois Tigeot 		 * set before a pipe_control command that has the state cache
365b5c29a34SFrançois Tigeot 		 * invalidate bit set. */
366b5c29a34SFrançois Tigeot 		gen7_render_ring_cs_stall_wa(ring);
367b5c29a34SFrançois Tigeot 	}
368b5c29a34SFrançois Tigeot 
369b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
370b5c29a34SFrançois Tigeot 	if (ret)
371b5c29a34SFrançois Tigeot 		return ret;
372b5c29a34SFrançois Tigeot 
373b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
374b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, flags);
375a2fdbec6SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
376b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
377e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
378e3adcf8fSFrançois Tigeot 
3799edbd4a0SFrançois Tigeot 	if (!invalidate_domains && flush_domains)
3805d0b1887SFrançois Tigeot 		return gen7_ring_fbc_flush(ring, FBC_REND_NUKE);
3815d0b1887SFrançois Tigeot 
382e3adcf8fSFrançois Tigeot 	return 0;
383e3adcf8fSFrançois Tigeot }
384e3adcf8fSFrançois Tigeot 
3859edbd4a0SFrançois Tigeot static int
386ba55f2f5SFrançois Tigeot gen8_render_ring_flush(struct intel_engine_cs *ring,
3879edbd4a0SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
3889edbd4a0SFrançois Tigeot {
3899edbd4a0SFrançois Tigeot 	u32 flags = 0;
390ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
3919edbd4a0SFrançois Tigeot 	int ret;
3929edbd4a0SFrançois Tigeot 
3939edbd4a0SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
3949edbd4a0SFrançois Tigeot 
3959edbd4a0SFrançois Tigeot 	if (flush_domains) {
3969edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
3979edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
3989edbd4a0SFrançois Tigeot 	}
3999edbd4a0SFrançois Tigeot 	if (invalidate_domains) {
4009edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
4019edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
4029edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
4039edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
4049edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
4059edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
4069edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
4079edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4089edbd4a0SFrançois Tigeot 	}
4099edbd4a0SFrançois Tigeot 
4109edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
4119edbd4a0SFrançois Tigeot 	if (ret)
4129edbd4a0SFrançois Tigeot 		return ret;
4139edbd4a0SFrançois Tigeot 
4149edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
4159edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, flags);
4169edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
4179edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, 0);
4189edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, 0);
4199edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, 0);
4209edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
4219edbd4a0SFrançois Tigeot 
4229edbd4a0SFrançois Tigeot 	return 0;
4239edbd4a0SFrançois Tigeot 
4249edbd4a0SFrançois Tigeot }
4259edbd4a0SFrançois Tigeot 
426ba55f2f5SFrançois Tigeot static void ring_write_tail(struct intel_engine_cs *ring,
427b5c29a34SFrançois Tigeot 			    u32 value)
428e3adcf8fSFrançois Tigeot {
429ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
430e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
431e3adcf8fSFrançois Tigeot }
432e3adcf8fSFrançois Tigeot 
433ba55f2f5SFrançois Tigeot u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
434e3adcf8fSFrançois Tigeot {
435ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
436ba55f2f5SFrançois Tigeot 	u64 acthd;
437e3adcf8fSFrançois Tigeot 
438ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
439ba55f2f5SFrançois Tigeot 		acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
440ba55f2f5SFrançois Tigeot 					 RING_ACTHD_UDW(ring->mmio_base));
441ba55f2f5SFrançois Tigeot 	else if (INTEL_INFO(ring->dev)->gen >= 4)
442ba55f2f5SFrançois Tigeot 		acthd = I915_READ(RING_ACTHD(ring->mmio_base));
443ba55f2f5SFrançois Tigeot 	else
444ba55f2f5SFrançois Tigeot 		acthd = I915_READ(ACTHD);
445ba55f2f5SFrançois Tigeot 
446ba55f2f5SFrançois Tigeot 	return acthd;
447e3adcf8fSFrançois Tigeot }
448e3adcf8fSFrançois Tigeot 
449ba55f2f5SFrançois Tigeot static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
4505d0b1887SFrançois Tigeot {
4515d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
4525d0b1887SFrançois Tigeot 	u32 addr;
4535d0b1887SFrançois Tigeot 
4545d0b1887SFrançois Tigeot 	addr = dev_priv->status_page_dmah->busaddr;
4555d0b1887SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 4)
4565d0b1887SFrançois Tigeot 		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
4575d0b1887SFrançois Tigeot 	I915_WRITE(HWS_PGA, addr);
4585d0b1887SFrançois Tigeot }
4595d0b1887SFrançois Tigeot 
460ba55f2f5SFrançois Tigeot static bool stop_ring(struct intel_engine_cs *ring)
461e3adcf8fSFrançois Tigeot {
462ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ring->dev);
463e3adcf8fSFrançois Tigeot 
464ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
465ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
466ba55f2f5SFrançois Tigeot 		if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
467ba55f2f5SFrançois Tigeot 			DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
468ba55f2f5SFrançois Tigeot 			return false;
469ba55f2f5SFrançois Tigeot 		}
470ba55f2f5SFrançois Tigeot 	}
471686a02f1SFrançois Tigeot 
472e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
473e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
474e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
475e3adcf8fSFrançois Tigeot 
476ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
477ba55f2f5SFrançois Tigeot 		(void)I915_READ_CTL(ring);
478ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
479ba55f2f5SFrançois Tigeot 	}
480e3adcf8fSFrançois Tigeot 
481ba55f2f5SFrançois Tigeot 	return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
482ba55f2f5SFrançois Tigeot }
483ba55f2f5SFrançois Tigeot 
484ba55f2f5SFrançois Tigeot static int init_ring_common(struct intel_engine_cs *ring)
485ba55f2f5SFrançois Tigeot {
486ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ring->dev;
487ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
488ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
489ba55f2f5SFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
490ba55f2f5SFrançois Tigeot 	int ret = 0;
491ba55f2f5SFrançois Tigeot 
492ba55f2f5SFrançois Tigeot 	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
493ba55f2f5SFrançois Tigeot 
494ba55f2f5SFrançois Tigeot 	if (!stop_ring(ring)) {
495ba55f2f5SFrançois Tigeot 		/* G45 ring initialization often fails to reset head to zero */
496b5c29a34SFrançois Tigeot 		DRM_DEBUG_KMS("%s head not reset to zero "
497e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
498e3adcf8fSFrançois Tigeot 			      ring->name,
499e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
500e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
501e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
502e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
503e3adcf8fSFrançois Tigeot 
504ba55f2f5SFrançois Tigeot 		if (!stop_ring(ring)) {
505e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
506e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
507e3adcf8fSFrançois Tigeot 				  ring->name,
508e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
509e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
510e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
511e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
512686a02f1SFrançois Tigeot 			ret = -EIO;
513686a02f1SFrançois Tigeot 			goto out;
514e3adcf8fSFrançois Tigeot 		}
515ba55f2f5SFrançois Tigeot 	}
516ba55f2f5SFrançois Tigeot 
517ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev))
518ba55f2f5SFrançois Tigeot 		intel_ring_setup_status_page(ring);
519ba55f2f5SFrançois Tigeot 	else
520ba55f2f5SFrançois Tigeot 		ring_setup_phys_status_page(ring);
521ba55f2f5SFrançois Tigeot 
5220f370975SMatthew Dillon 	/* Enforce ordering by reading HEAD register back */
5230f370975SMatthew Dillon 	I915_READ_HEAD(ring);
5240f370975SMatthew Dillon 
525ba55f2f5SFrançois Tigeot 	/* Initialize the ring. This must happen _after_ we've cleared the ring
526ba55f2f5SFrançois Tigeot 	 * registers with the above sequence (the readback of the HEAD registers
527ba55f2f5SFrançois Tigeot 	 * also enforces ordering), otherwise the hw might lose the new ring
528ba55f2f5SFrançois Tigeot 	 * register values. */
529ba55f2f5SFrançois Tigeot 	I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
530ba55f2f5SFrançois Tigeot 	I915_WRITE_CTL(ring,
531ba55f2f5SFrançois Tigeot 			((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
532ba55f2f5SFrançois Tigeot 			| RING_VALID);
533ba55f2f5SFrançois Tigeot 
534ba55f2f5SFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
535ba55f2f5SFrançois Tigeot 	if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
536ba55f2f5SFrançois Tigeot 		     I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
537ba55f2f5SFrançois Tigeot 		     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
538ba55f2f5SFrançois Tigeot 		DRM_ERROR("%s initialization failed "
539ba55f2f5SFrançois Tigeot 			  "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
540ba55f2f5SFrançois Tigeot 			  ring->name,
541ba55f2f5SFrançois Tigeot 			  I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
542ba55f2f5SFrançois Tigeot 			  I915_READ_HEAD(ring), I915_READ_TAIL(ring),
543ba55f2f5SFrançois Tigeot 			  I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
544ba55f2f5SFrançois Tigeot 		ret = -EIO;
545ba55f2f5SFrançois Tigeot 		goto out;
546ba55f2f5SFrançois Tigeot 	}
547e3adcf8fSFrançois Tigeot 
548e3adcf8fSFrançois Tigeot 	if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
549e3adcf8fSFrançois Tigeot 		i915_kernel_lost_context(ring->dev);
550e3adcf8fSFrançois Tigeot 	else {
551ba55f2f5SFrançois Tigeot 		ringbuf->head = I915_READ_HEAD(ring);
552ba55f2f5SFrançois Tigeot 		ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
553ba55f2f5SFrançois Tigeot 		ringbuf->space = ring_space(ring);
554ba55f2f5SFrançois Tigeot 		ringbuf->last_retired_head = -1;
555e3adcf8fSFrançois Tigeot 	}
556e3adcf8fSFrançois Tigeot 
5575d0b1887SFrançois Tigeot 	memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
5585d0b1887SFrançois Tigeot 
559686a02f1SFrançois Tigeot out:
5609edbd4a0SFrançois Tigeot 	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
561686a02f1SFrançois Tigeot 
562686a02f1SFrançois Tigeot 	return ret;
563e3adcf8fSFrançois Tigeot }
564e3adcf8fSFrançois Tigeot 
565e3adcf8fSFrançois Tigeot static int
566ba55f2f5SFrançois Tigeot init_pipe_control(struct intel_engine_cs *ring)
567e3adcf8fSFrançois Tigeot {
568e3adcf8fSFrançois Tigeot 	int ret;
569e3adcf8fSFrançois Tigeot 
5709edbd4a0SFrançois Tigeot 	if (ring->scratch.obj)
571e3adcf8fSFrançois Tigeot 		return 0;
572e3adcf8fSFrançois Tigeot 
5739edbd4a0SFrançois Tigeot 	ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
5749edbd4a0SFrançois Tigeot 	if (ring->scratch.obj == NULL) {
575e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
576e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
577e3adcf8fSFrançois Tigeot 		goto err;
578e3adcf8fSFrançois Tigeot 	}
579e3adcf8fSFrançois Tigeot 
580ba55f2f5SFrançois Tigeot 	ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
581ba55f2f5SFrançois Tigeot 	if (ret)
582ba55f2f5SFrançois Tigeot 		goto err_unref;
583e3adcf8fSFrançois Tigeot 
584ba55f2f5SFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
585e3adcf8fSFrançois Tigeot 	if (ret)
586e3adcf8fSFrançois Tigeot 		goto err_unref;
587e3adcf8fSFrançois Tigeot 
5889edbd4a0SFrançois Tigeot 	ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
5899edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page = kmap(ring->scratch.obj->pages[0]);
5909edbd4a0SFrançois Tigeot 	if (ring->scratch.cpu_page == NULL) {
5915d0b1887SFrançois Tigeot 		ret = -ENOMEM;
592e3adcf8fSFrançois Tigeot 		goto err_unpin;
5935d0b1887SFrançois Tigeot 	}
594a2fdbec6SFrançois Tigeot 
595a2fdbec6SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
5969edbd4a0SFrançois Tigeot 			 ring->name, ring->scratch.gtt_offset);
597e3adcf8fSFrançois Tigeot 	return 0;
598e3adcf8fSFrançois Tigeot 
599e3adcf8fSFrançois Tigeot err_unpin:
600ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(ring->scratch.obj);
601e3adcf8fSFrançois Tigeot err_unref:
6029edbd4a0SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
603e3adcf8fSFrançois Tigeot err:
604e3adcf8fSFrançois Tigeot 	return ret;
605e3adcf8fSFrançois Tigeot }
606e3adcf8fSFrançois Tigeot 
607ba55f2f5SFrançois Tigeot static int init_render_ring(struct intel_engine_cs *ring)
608e3adcf8fSFrançois Tigeot {
609e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
610e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
611e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
612e3adcf8fSFrançois Tigeot 
613ba55f2f5SFrançois Tigeot 	/* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
614ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
615f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
616f4e1c372SFrançois Tigeot 
617f4e1c372SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
618f4e1c372SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
619f4e1c372SFrançois Tigeot 	 * programmed to '1' on all products.
6205d0b1887SFrançois Tigeot 	 *
621ba55f2f5SFrançois Tigeot 	 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
622f4e1c372SFrançois Tigeot 	 */
623f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
624f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
625f4e1c372SFrançois Tigeot 
626f4e1c372SFrançois Tigeot 	/* Required for the hardware to program scanline values for waiting */
627ba55f2f5SFrançois Tigeot 	/* WaEnableFlushTlbInvalidationMode:snb */
628f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6)
629f4e1c372SFrançois Tigeot 		I915_WRITE(GFX_MODE,
630ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
631f4e1c372SFrançois Tigeot 
632ba55f2f5SFrançois Tigeot 	/* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
633e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev))
634e3adcf8fSFrançois Tigeot 		I915_WRITE(GFX_MODE_GEN7,
635ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
636f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
637e3adcf8fSFrançois Tigeot 
638e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
639e3adcf8fSFrançois Tigeot 		ret = init_pipe_control(ring);
640e3adcf8fSFrançois Tigeot 		if (ret)
641e3adcf8fSFrançois Tigeot 			return ret;
642e3adcf8fSFrançois Tigeot 	}
643e3adcf8fSFrançois Tigeot 
644e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
645e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
646e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
647e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
648e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
649e3adcf8fSFrançois Tigeot 		 */
650e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
651f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
652e3adcf8fSFrançois Tigeot 	}
653e3adcf8fSFrançois Tigeot 
654f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
655f4e1c372SFrançois Tigeot 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
656f4e1c372SFrançois Tigeot 
6579edbd4a0SFrançois Tigeot 	if (HAS_L3_DPF(dev))
6589edbd4a0SFrançois Tigeot 		I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
659e3adcf8fSFrançois Tigeot 
660e3adcf8fSFrançois Tigeot 	return ret;
661e3adcf8fSFrançois Tigeot }
662e3adcf8fSFrançois Tigeot 
663ba55f2f5SFrançois Tigeot static void render_ring_cleanup(struct intel_engine_cs *ring)
664e3adcf8fSFrançois Tigeot {
665b5c29a34SFrançois Tigeot 	struct drm_device *dev = ring->dev;
666b5c29a34SFrançois Tigeot 
6679edbd4a0SFrançois Tigeot 	if (ring->scratch.obj == NULL)
668e3adcf8fSFrançois Tigeot 		return;
669e3adcf8fSFrançois Tigeot 
6709edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
6719edbd4a0SFrançois Tigeot 		kunmap(ring->scratch.obj->pages[0]);
672ba55f2f5SFrançois Tigeot 		i915_gem_object_ggtt_unpin(ring->scratch.obj);
6739edbd4a0SFrançois Tigeot 	}
674b5c29a34SFrançois Tigeot 
6759edbd4a0SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
6769edbd4a0SFrançois Tigeot 	ring->scratch.obj = NULL;
677e3adcf8fSFrançois Tigeot }
678e3adcf8fSFrançois Tigeot 
679ba55f2f5SFrançois Tigeot static int gen6_signal(struct intel_engine_cs *signaller,
680ba55f2f5SFrançois Tigeot 		       unsigned int num_dwords)
681e3adcf8fSFrançois Tigeot {
682ba55f2f5SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
683ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
684ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *useless;
685ba55f2f5SFrançois Tigeot 	int i, ret;
686ba55f2f5SFrançois Tigeot 
687ba55f2f5SFrançois Tigeot 	/* NB: In order to be able to do semaphore MBOX updates for varying
688ba55f2f5SFrançois Tigeot 	 * number of rings, it's easiest if we round up each individual update
689ba55f2f5SFrançois Tigeot 	 * to a multiple of 2 (since ring updates must always be a multiple of
690ba55f2f5SFrançois Tigeot 	 * 2) even though the actual update only requires 3 dwords.
6915d0b1887SFrançois Tigeot 	 */
6925d0b1887SFrançois Tigeot #define MBOX_UPDATE_DWORDS 4
693ba55f2f5SFrançois Tigeot 	if (i915_semaphore_is_enabled(dev))
694ba55f2f5SFrançois Tigeot 		num_dwords += ((I915_NUM_RINGS-1) * MBOX_UPDATE_DWORDS);
695ba55f2f5SFrançois Tigeot 	else
696ba55f2f5SFrançois Tigeot 		return intel_ring_begin(signaller, num_dwords);
697ba55f2f5SFrançois Tigeot 
698ba55f2f5SFrançois Tigeot 	ret = intel_ring_begin(signaller, num_dwords);
699ba55f2f5SFrançois Tigeot 	if (ret)
700ba55f2f5SFrançois Tigeot 		return ret;
701ba55f2f5SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
702ba55f2f5SFrançois Tigeot 
703ba55f2f5SFrançois Tigeot 	for_each_ring(useless, dev_priv, i) {
704ba55f2f5SFrançois Tigeot 		u32 mbox_reg = signaller->semaphore.mbox.signal[i];
705ba55f2f5SFrançois Tigeot 		if (mbox_reg != GEN6_NOSYNC) {
706ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
707ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, mbox_reg);
708ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, signaller->outstanding_lazy_seqno);
709ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
710ba55f2f5SFrançois Tigeot 		} else {
711ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
712ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
713ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
714ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_NOOP);
715ba55f2f5SFrançois Tigeot 		}
716ba55f2f5SFrançois Tigeot 	}
717ba55f2f5SFrançois Tigeot 
718ba55f2f5SFrançois Tigeot 	return 0;
719e3adcf8fSFrançois Tigeot }
720e3adcf8fSFrançois Tigeot 
721e3adcf8fSFrançois Tigeot /**
722e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
723e3adcf8fSFrançois Tigeot  *
724e3adcf8fSFrançois Tigeot  * @ring - ring that is adding a request
725e3adcf8fSFrançois Tigeot  * @seqno - return seqno stuck into the ring
726e3adcf8fSFrançois Tigeot  *
727e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
728e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
729e3adcf8fSFrançois Tigeot  */
730e3adcf8fSFrançois Tigeot static int
731ba55f2f5SFrançois Tigeot gen6_add_request(struct intel_engine_cs *ring)
732e3adcf8fSFrançois Tigeot {
733ba55f2f5SFrançois Tigeot 	int ret;
734e3adcf8fSFrançois Tigeot 
735ba55f2f5SFrançois Tigeot 	ret = ring->semaphore.signal(ring, 4);
7369edbd4a0SFrançois Tigeot 	if (ret)
7379edbd4a0SFrançois Tigeot 		return ret;
7389edbd4a0SFrançois Tigeot 
739e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
740e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
7419edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_seqno);
742e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
7439edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
744e3adcf8fSFrançois Tigeot 
745e3adcf8fSFrançois Tigeot 	return 0;
746e3adcf8fSFrançois Tigeot }
747e3adcf8fSFrançois Tigeot 
748a2fdbec6SFrançois Tigeot static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
749a2fdbec6SFrançois Tigeot 					      u32 seqno)
750a2fdbec6SFrançois Tigeot {
751a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
752a2fdbec6SFrançois Tigeot 	return dev_priv->last_seqno < seqno;
753a2fdbec6SFrançois Tigeot }
754a2fdbec6SFrançois Tigeot 
755e3adcf8fSFrançois Tigeot /**
756e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
757e3adcf8fSFrançois Tigeot  *
758e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
759e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
760e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
761e3adcf8fSFrançois Tigeot  */
762e3adcf8fSFrançois Tigeot static int
763ba55f2f5SFrançois Tigeot gen6_ring_sync(struct intel_engine_cs *waiter,
764ba55f2f5SFrançois Tigeot 	       struct intel_engine_cs *signaller,
765e3adcf8fSFrançois Tigeot 	       u32 seqno)
766e3adcf8fSFrançois Tigeot {
767e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
768e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
769e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
770ba55f2f5SFrançois Tigeot 	u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
771ba55f2f5SFrançois Tigeot 	int ret;
772e3adcf8fSFrançois Tigeot 
773686a02f1SFrançois Tigeot 	/* Throughout all of the GEM code, seqno passed implies our current
774686a02f1SFrançois Tigeot 	 * seqno is >= the last seqno executed. However for hardware the
775686a02f1SFrançois Tigeot 	 * comparison is strictly greater than.
776686a02f1SFrançois Tigeot 	 */
777686a02f1SFrançois Tigeot 	seqno -= 1;
778686a02f1SFrançois Tigeot 
779ba55f2f5SFrançois Tigeot 	WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
780686a02f1SFrançois Tigeot 
781e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
782e3adcf8fSFrançois Tigeot 	if (ret)
783e3adcf8fSFrançois Tigeot 		return ret;
784e3adcf8fSFrançois Tigeot 
785a2fdbec6SFrançois Tigeot 	/* If seqno wrap happened, omit the wait with no-ops */
786a2fdbec6SFrançois Tigeot 	if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
787ba55f2f5SFrançois Tigeot 		intel_ring_emit(waiter, dw1 | wait_mbox);
788e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, seqno);
789e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, 0);
790e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
791a2fdbec6SFrançois Tigeot 	} else {
792a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
793a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
794a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
795a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
796a2fdbec6SFrançois Tigeot 	}
797e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
798e3adcf8fSFrançois Tigeot 
799e3adcf8fSFrançois Tigeot 	return 0;
800e3adcf8fSFrançois Tigeot }
801e3adcf8fSFrançois Tigeot 
802e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
803e3adcf8fSFrançois Tigeot do {									\
804e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
805e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
806e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
807e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
808e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
809e3adcf8fSFrançois Tigeot } while (0)
810e3adcf8fSFrançois Tigeot 
811e3adcf8fSFrançois Tigeot static int
812ba55f2f5SFrançois Tigeot pc_render_add_request(struct intel_engine_cs *ring)
813e3adcf8fSFrançois Tigeot {
814ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
815e3adcf8fSFrançois Tigeot 	int ret;
816e3adcf8fSFrançois Tigeot 
817e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
818e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
819e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
820e3adcf8fSFrançois Tigeot 	 *
821e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
822e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
823e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
824e3adcf8fSFrançois Tigeot 	 */
825e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 32);
826e3adcf8fSFrançois Tigeot 	if (ret)
827e3adcf8fSFrançois Tigeot 		return ret;
828e3adcf8fSFrançois Tigeot 
829e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
830e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
831e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
8329edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
8339edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_seqno);
834e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
835e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
836ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
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);
844ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
845e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
846b5c29a34SFrançois Tigeot 
847e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
848e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
849e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
850e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
8519edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
8529edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_seqno);
853e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
8549edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
855e3adcf8fSFrançois Tigeot 
856e3adcf8fSFrançois Tigeot 	return 0;
857e3adcf8fSFrançois Tigeot }
858e3adcf8fSFrançois Tigeot 
859e3adcf8fSFrançois Tigeot static u32
860ba55f2f5SFrançois Tigeot gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
861e3adcf8fSFrançois Tigeot {
862e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
863e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
864e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
865ba55f2f5SFrançois Tigeot 	if (!lazy_coherency) {
866ba55f2f5SFrançois Tigeot 		struct drm_i915_private *dev_priv = ring->dev->dev_private;
867ba55f2f5SFrançois Tigeot 		POSTING_READ(RING_ACTHD(ring->mmio_base));
868ba55f2f5SFrançois Tigeot 	}
869ba55f2f5SFrançois Tigeot 
870e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
871e3adcf8fSFrançois Tigeot }
872e3adcf8fSFrançois Tigeot 
873b030f26bSFrançois Tigeot static u32
874ba55f2f5SFrançois Tigeot ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
875e3adcf8fSFrançois Tigeot {
876e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
877e3adcf8fSFrançois Tigeot }
878e3adcf8fSFrançois Tigeot 
879a2fdbec6SFrançois Tigeot static void
880ba55f2f5SFrançois Tigeot ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
881a2fdbec6SFrançois Tigeot {
882a2fdbec6SFrançois Tigeot 	intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
883a2fdbec6SFrançois Tigeot }
884a2fdbec6SFrançois Tigeot 
885b030f26bSFrançois Tigeot static u32
886ba55f2f5SFrançois Tigeot pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
887e3adcf8fSFrançois Tigeot {
8889edbd4a0SFrançois Tigeot 	return ring->scratch.cpu_page[0];
889e3adcf8fSFrançois Tigeot }
890e3adcf8fSFrançois Tigeot 
891a2fdbec6SFrançois Tigeot static void
892ba55f2f5SFrançois Tigeot pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
893a2fdbec6SFrançois Tigeot {
8949edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page[0] = seqno;
895a2fdbec6SFrançois Tigeot }
896a2fdbec6SFrançois Tigeot 
897e3adcf8fSFrançois Tigeot static bool
898ba55f2f5SFrançois Tigeot gen5_ring_get_irq(struct intel_engine_cs *ring)
899e3adcf8fSFrançois Tigeot {
900e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
901ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
902e3adcf8fSFrançois Tigeot 
903e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
904e3adcf8fSFrançois Tigeot 		return false;
905e3adcf8fSFrançois Tigeot 
90602727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9079edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0)
9089edbd4a0SFrançois Tigeot 		ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
90902727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
910e3adcf8fSFrançois Tigeot 
911e3adcf8fSFrançois Tigeot 	return true;
912e3adcf8fSFrançois Tigeot }
913e3adcf8fSFrançois Tigeot 
914e3adcf8fSFrançois Tigeot static void
915ba55f2f5SFrançois Tigeot gen5_ring_put_irq(struct intel_engine_cs *ring)
916e3adcf8fSFrançois Tigeot {
917e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
918ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
919e3adcf8fSFrançois Tigeot 
92002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9219edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0)
9229edbd4a0SFrançois Tigeot 		ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
923686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
924686a02f1SFrançois Tigeot }
925686a02f1SFrançois Tigeot 
926686a02f1SFrançois Tigeot static bool
927ba55f2f5SFrançois Tigeot i9xx_ring_get_irq(struct intel_engine_cs *ring)
928686a02f1SFrançois Tigeot {
929686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
930ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
931686a02f1SFrançois Tigeot 
932686a02f1SFrançois Tigeot 	if (!dev->irq_enabled)
933686a02f1SFrançois Tigeot 		return false;
934686a02f1SFrançois Tigeot 
935686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9369edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
937686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
938686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
939686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
940686a02f1SFrançois Tigeot 	}
941686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
942686a02f1SFrançois Tigeot 
943686a02f1SFrançois Tigeot 	return true;
944686a02f1SFrançois Tigeot }
945686a02f1SFrançois Tigeot 
946686a02f1SFrançois Tigeot static void
947ba55f2f5SFrançois Tigeot i9xx_ring_put_irq(struct intel_engine_cs *ring)
948686a02f1SFrançois Tigeot {
949686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
950ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
951686a02f1SFrançois Tigeot 
952686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9539edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
954686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
955686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
956686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
957686a02f1SFrançois Tigeot 	}
958686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
959686a02f1SFrançois Tigeot }
960686a02f1SFrançois Tigeot 
961686a02f1SFrançois Tigeot static bool
962ba55f2f5SFrançois Tigeot i8xx_ring_get_irq(struct intel_engine_cs *ring)
963686a02f1SFrançois Tigeot {
964686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
965ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
966686a02f1SFrançois Tigeot 
967686a02f1SFrançois Tigeot 	if (!dev->irq_enabled)
968686a02f1SFrançois Tigeot 		return false;
969686a02f1SFrançois Tigeot 
970686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9719edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
972686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
973686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
974686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
975686a02f1SFrançois Tigeot 	}
976686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
977686a02f1SFrançois Tigeot 
978686a02f1SFrançois Tigeot 	return true;
979686a02f1SFrançois Tigeot }
980686a02f1SFrançois Tigeot 
981686a02f1SFrançois Tigeot static void
982ba55f2f5SFrançois Tigeot i8xx_ring_put_irq(struct intel_engine_cs *ring)
983686a02f1SFrançois Tigeot {
984686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
985ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
986686a02f1SFrançois Tigeot 
987686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
9889edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
989686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
990686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
991686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
992e3adcf8fSFrançois Tigeot 	}
99302727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
994e3adcf8fSFrançois Tigeot }
995e3adcf8fSFrançois Tigeot 
996ba55f2f5SFrançois Tigeot void intel_ring_setup_status_page(struct intel_engine_cs *ring)
997e3adcf8fSFrançois Tigeot {
998e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
999ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1000b5c29a34SFrançois Tigeot 	u32 mmio = 0;
1001e3adcf8fSFrançois Tigeot 
1002e3adcf8fSFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
1003e3adcf8fSFrançois Tigeot 	 * the ring registers as of gen7.
1004e3adcf8fSFrançois Tigeot 	 */
1005e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev)) {
1006e3adcf8fSFrançois Tigeot 		switch (ring->id) {
1007e3adcf8fSFrançois Tigeot 		case RCS:
1008e3adcf8fSFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
1009e3adcf8fSFrançois Tigeot 			break;
1010e3adcf8fSFrançois Tigeot 		case BCS:
1011e3adcf8fSFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
1012e3adcf8fSFrançois Tigeot 			break;
1013ba55f2f5SFrançois Tigeot 		/*
1014ba55f2f5SFrançois Tigeot 		 * VCS2 actually doesn't exist on Gen7. Only shut up
1015ba55f2f5SFrançois Tigeot 		 * gcc switch check warning
1016ba55f2f5SFrançois Tigeot 		 */
1017ba55f2f5SFrançois Tigeot 		case VCS2:
1018e3adcf8fSFrançois Tigeot 		case VCS:
1019e3adcf8fSFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
1020e3adcf8fSFrançois Tigeot 			break;
10215d0b1887SFrançois Tigeot 		case VECS:
10225d0b1887SFrançois Tigeot 			mmio = VEBOX_HWS_PGA_GEN7;
10235d0b1887SFrançois Tigeot 			break;
1024e3adcf8fSFrançois Tigeot 		}
1025b5c29a34SFrançois Tigeot 	} else if (IS_GEN6(ring->dev)) {
1026e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
1027e3adcf8fSFrançois Tigeot 	} else {
10289edbd4a0SFrançois Tigeot 		/* XXX: gen8 returns to sanity */
1029e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
1030e3adcf8fSFrançois Tigeot 	}
1031e3adcf8fSFrançois Tigeot 
1032e3adcf8fSFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
1033e3adcf8fSFrançois Tigeot 	POSTING_READ(mmio);
10345d0b1887SFrançois Tigeot 
1035ba55f2f5SFrançois Tigeot 	/*
1036ba55f2f5SFrançois Tigeot 	 * Flush the TLB for this page
1037ba55f2f5SFrançois Tigeot 	 *
1038ba55f2f5SFrançois Tigeot 	 * FIXME: These two bits have disappeared on gen8, so a question
1039ba55f2f5SFrançois Tigeot 	 * arises: do we still need this and if so how should we go about
1040ba55f2f5SFrançois Tigeot 	 * invalidating the TLB?
1041ba55f2f5SFrançois Tigeot 	 */
1042ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
10435d0b1887SFrançois Tigeot 		u32 reg = RING_INSTPM(ring->mmio_base);
1044ba55f2f5SFrançois Tigeot 
1045ba55f2f5SFrançois Tigeot 		/* ring should be idle before issuing a sync flush*/
1046ba55f2f5SFrançois Tigeot 		WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
1047ba55f2f5SFrançois Tigeot 
10485d0b1887SFrançois Tigeot 		I915_WRITE(reg,
10495d0b1887SFrançois Tigeot 			   _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
10505d0b1887SFrançois Tigeot 					      INSTPM_SYNC_FLUSH));
10515d0b1887SFrançois Tigeot 		if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
10525d0b1887SFrançois Tigeot 			     1000))
10535d0b1887SFrançois Tigeot 			DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
10545d0b1887SFrançois Tigeot 				  ring->name);
10555d0b1887SFrançois Tigeot 	}
1056e3adcf8fSFrançois Tigeot }
1057e3adcf8fSFrançois Tigeot 
1058e3adcf8fSFrançois Tigeot static int
1059ba55f2f5SFrançois Tigeot bsd_ring_flush(struct intel_engine_cs *ring,
1060b5c29a34SFrançois Tigeot 	       u32     invalidate_domains,
1061b5c29a34SFrançois Tigeot 	       u32     flush_domains)
1062e3adcf8fSFrançois Tigeot {
1063e3adcf8fSFrançois Tigeot 	int ret;
1064e3adcf8fSFrançois Tigeot 
1065e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1066e3adcf8fSFrançois Tigeot 	if (ret)
1067e3adcf8fSFrançois Tigeot 		return ret;
1068e3adcf8fSFrançois Tigeot 
1069e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
1070e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1071e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1072e3adcf8fSFrançois Tigeot 	return 0;
1073e3adcf8fSFrançois Tigeot }
1074e3adcf8fSFrançois Tigeot 
1075e3adcf8fSFrançois Tigeot static int
1076ba55f2f5SFrançois Tigeot i9xx_add_request(struct intel_engine_cs *ring)
1077e3adcf8fSFrançois Tigeot {
1078e3adcf8fSFrançois Tigeot 	int ret;
1079e3adcf8fSFrançois Tigeot 
1080e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1081e3adcf8fSFrançois Tigeot 	if (ret)
1082e3adcf8fSFrançois Tigeot 		return ret;
1083e3adcf8fSFrançois Tigeot 
1084e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1085e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
10869edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_seqno);
1087e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
10889edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1089e3adcf8fSFrançois Tigeot 
1090e3adcf8fSFrançois Tigeot 	return 0;
1091e3adcf8fSFrançois Tigeot }
1092e3adcf8fSFrançois Tigeot 
1093e3adcf8fSFrançois Tigeot static bool
1094ba55f2f5SFrançois Tigeot gen6_ring_get_irq(struct intel_engine_cs *ring)
1095e3adcf8fSFrançois Tigeot {
1096e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1097ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1098e3adcf8fSFrançois Tigeot 
1099e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
1100e3adcf8fSFrançois Tigeot 	       return false;
1101e3adcf8fSFrançois Tigeot 
110202727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11039edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
11049edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
11055d0b1887SFrançois Tigeot 			I915_WRITE_IMR(ring,
11065d0b1887SFrançois Tigeot 				       ~(ring->irq_enable_mask |
11079edbd4a0SFrançois Tigeot 					 GT_PARITY_ERROR(dev)));
1108686a02f1SFrançois Tigeot 		else
1109686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
11109edbd4a0SFrançois Tigeot 		ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1111e3adcf8fSFrançois Tigeot 	}
111202727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1113e3adcf8fSFrançois Tigeot 
1114e3adcf8fSFrançois Tigeot 	return true;
1115e3adcf8fSFrançois Tigeot }
1116e3adcf8fSFrançois Tigeot 
1117e3adcf8fSFrançois Tigeot static void
1118ba55f2f5SFrançois Tigeot gen6_ring_put_irq(struct intel_engine_cs *ring)
1119e3adcf8fSFrançois Tigeot {
1120e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1121ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1122e3adcf8fSFrançois Tigeot 
112302727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11249edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
11259edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
11269edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1127686a02f1SFrançois Tigeot 		else
1128686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
11299edbd4a0SFrançois Tigeot 		ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1130e3adcf8fSFrançois Tigeot 	}
113102727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
1132e3adcf8fSFrançois Tigeot }
1133e3adcf8fSFrançois Tigeot 
11345d0b1887SFrançois Tigeot static bool
1135ba55f2f5SFrançois Tigeot hsw_vebox_get_irq(struct intel_engine_cs *ring)
11365d0b1887SFrançois Tigeot {
11375d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11385d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11395d0b1887SFrançois Tigeot 
11405d0b1887SFrançois Tigeot 	if (!dev->irq_enabled)
11415d0b1887SFrançois Tigeot 		return false;
11425d0b1887SFrançois Tigeot 
11439edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11449edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
11455d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
11469edbd4a0SFrançois Tigeot 		snb_enable_pm_irq(dev_priv, ring->irq_enable_mask);
11475d0b1887SFrançois Tigeot 	}
11489edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
11495d0b1887SFrançois Tigeot 
11505d0b1887SFrançois Tigeot 	return true;
11515d0b1887SFrançois Tigeot }
11525d0b1887SFrançois Tigeot 
11535d0b1887SFrançois Tigeot static void
1154ba55f2f5SFrançois Tigeot hsw_vebox_put_irq(struct intel_engine_cs *ring)
11555d0b1887SFrançois Tigeot {
11565d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11575d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11585d0b1887SFrançois Tigeot 
11595d0b1887SFrançois Tigeot 	if (!dev->irq_enabled)
11605d0b1887SFrançois Tigeot 		return;
11615d0b1887SFrançois Tigeot 
11629edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11639edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
11645d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~0);
11659edbd4a0SFrançois Tigeot 		snb_disable_pm_irq(dev_priv, ring->irq_enable_mask);
11665d0b1887SFrançois Tigeot 	}
11679edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
11689edbd4a0SFrançois Tigeot }
11699edbd4a0SFrançois Tigeot 
11709edbd4a0SFrançois Tigeot static bool
1171ba55f2f5SFrançois Tigeot gen8_ring_get_irq(struct intel_engine_cs *ring)
11729edbd4a0SFrançois Tigeot {
11739edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11749edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11759edbd4a0SFrançois Tigeot 
11769edbd4a0SFrançois Tigeot 	if (!dev->irq_enabled)
11779edbd4a0SFrançois Tigeot 		return false;
11789edbd4a0SFrançois Tigeot 
11799edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11809edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
11819edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
11829edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
11839edbd4a0SFrançois Tigeot 				       ~(ring->irq_enable_mask |
11849edbd4a0SFrançois Tigeot 					 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
11859edbd4a0SFrançois Tigeot 		} else {
11869edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
11879edbd4a0SFrançois Tigeot 		}
11889edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
11899edbd4a0SFrançois Tigeot 	}
11909edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
11919edbd4a0SFrançois Tigeot 
11929edbd4a0SFrançois Tigeot 	return true;
11939edbd4a0SFrançois Tigeot }
11949edbd4a0SFrançois Tigeot 
11959edbd4a0SFrançois Tigeot static void
1196ba55f2f5SFrançois Tigeot gen8_ring_put_irq(struct intel_engine_cs *ring)
11979edbd4a0SFrançois Tigeot {
11989edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11999edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
12009edbd4a0SFrançois Tigeot 
12019edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
12029edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
12039edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
12049edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
12059edbd4a0SFrançois Tigeot 				       ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
12069edbd4a0SFrançois Tigeot 		} else {
12079edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
12089edbd4a0SFrançois Tigeot 		}
12099edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
12109edbd4a0SFrançois Tigeot 	}
12119edbd4a0SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
12125d0b1887SFrançois Tigeot }
12135d0b1887SFrançois Tigeot 
1214e3adcf8fSFrançois Tigeot static int
1215ba55f2f5SFrançois Tigeot i965_dispatch_execbuffer(struct intel_engine_cs *ring,
1216ba55f2f5SFrançois Tigeot 			 u64 offset, u32 length,
1217b5c29a34SFrançois Tigeot 			 unsigned flags)
1218e3adcf8fSFrançois Tigeot {
1219e3adcf8fSFrançois Tigeot 	int ret;
1220e3adcf8fSFrançois Tigeot 
1221e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1222e3adcf8fSFrançois Tigeot 	if (ret)
1223e3adcf8fSFrançois Tigeot 		return ret;
1224e3adcf8fSFrançois Tigeot 
1225e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
1226686a02f1SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1227b5c29a34SFrançois Tigeot 			MI_BATCH_GTT |
1228b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
1229e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1230e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1231e3adcf8fSFrançois Tigeot 
1232e3adcf8fSFrançois Tigeot 	return 0;
1233e3adcf8fSFrançois Tigeot }
1234e3adcf8fSFrançois Tigeot 
1235b5c29a34SFrançois Tigeot /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1236b5c29a34SFrançois Tigeot #define I830_BATCH_LIMIT (256*1024)
1237e3adcf8fSFrançois Tigeot static int
1238ba55f2f5SFrançois Tigeot i830_dispatch_execbuffer(struct intel_engine_cs *ring,
1239ba55f2f5SFrançois Tigeot 				u64 offset, u32 len,
1240b5c29a34SFrançois Tigeot 				unsigned flags)
1241e3adcf8fSFrançois Tigeot {
1242e3adcf8fSFrançois Tigeot 	int ret;
1243e3adcf8fSFrançois Tigeot 
1244b5c29a34SFrançois Tigeot 	if (flags & I915_DISPATCH_PINNED) {
1245e3adcf8fSFrançois Tigeot 		ret = intel_ring_begin(ring, 4);
1246e3adcf8fSFrançois Tigeot 		if (ret)
1247e3adcf8fSFrançois Tigeot 			return ret;
1248e3adcf8fSFrançois Tigeot 
1249e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
1250b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1251e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, offset + len - 8);
1252b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
1253686a02f1SFrançois Tigeot 		intel_ring_advance(ring);
1254b5c29a34SFrançois Tigeot 	} else {
12559edbd4a0SFrançois Tigeot 		u32 cs_offset = ring->scratch.gtt_offset;
1256b5c29a34SFrançois Tigeot 
1257b5c29a34SFrançois Tigeot 		if (len > I830_BATCH_LIMIT)
1258b5c29a34SFrançois Tigeot 			return -ENOSPC;
1259b5c29a34SFrançois Tigeot 
1260b5c29a34SFrançois Tigeot 		ret = intel_ring_begin(ring, 9+3);
1261b5c29a34SFrançois Tigeot 		if (ret)
1262b5c29a34SFrançois Tigeot 			return ret;
1263b5c29a34SFrançois Tigeot 		/* Blit the batch (which has now all relocs applied) to the stable batch
1264b5c29a34SFrançois Tigeot 		 * scratch bo area (so that the CS never stumbles over its tlb
1265b5c29a34SFrançois Tigeot 		 * invalidation bug) ... */
1266b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
1267b5c29a34SFrançois Tigeot 				XY_SRC_COPY_BLT_WRITE_ALPHA |
1268b5c29a34SFrançois Tigeot 				XY_SRC_COPY_BLT_WRITE_RGB);
1269b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
1270b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 0);
1271b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
1272b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset);
1273b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 0);
1274b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 4096);
1275b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, offset);
1276b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_FLUSH);
1277b5c29a34SFrançois Tigeot 
1278b5c29a34SFrançois Tigeot 		/* ... and execute it. */
1279b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
1280b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1281b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset + len - 8);
1282b5c29a34SFrançois Tigeot 		intel_ring_advance(ring);
1283b5c29a34SFrançois Tigeot 	}
1284686a02f1SFrançois Tigeot 
1285686a02f1SFrançois Tigeot 	return 0;
1286686a02f1SFrançois Tigeot }
1287686a02f1SFrançois Tigeot 
1288686a02f1SFrançois Tigeot static int
1289ba55f2f5SFrançois Tigeot i915_dispatch_execbuffer(struct intel_engine_cs *ring,
1290ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1291b5c29a34SFrançois Tigeot 			 unsigned flags)
1292686a02f1SFrançois Tigeot {
1293686a02f1SFrançois Tigeot 	int ret;
1294686a02f1SFrançois Tigeot 
1295e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1296e3adcf8fSFrançois Tigeot 	if (ret)
1297e3adcf8fSFrançois Tigeot 		return ret;
1298e3adcf8fSFrançois Tigeot 
1299686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1300686a02f1SFrançois Tigeot 	intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1301e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1302e3adcf8fSFrançois Tigeot 
1303e3adcf8fSFrançois Tigeot 	return 0;
1304e3adcf8fSFrançois Tigeot }
1305e3adcf8fSFrançois Tigeot 
1306ba55f2f5SFrançois Tigeot static void cleanup_status_page(struct intel_engine_cs *ring)
1307e3adcf8fSFrançois Tigeot {
1308e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1309e3adcf8fSFrançois Tigeot 
1310e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
1311e3adcf8fSFrançois Tigeot 	if (obj == NULL)
1312e3adcf8fSFrançois Tigeot 		return;
1313e3adcf8fSFrançois Tigeot 
13149edbd4a0SFrançois Tigeot 	kunmap(obj->pages[0]);
1315ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(obj);
1316e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1317e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
1318e3adcf8fSFrançois Tigeot }
1319e3adcf8fSFrançois Tigeot 
1320ba55f2f5SFrançois Tigeot static int init_status_page(struct intel_engine_cs *ring)
1321e3adcf8fSFrançois Tigeot {
1322e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1323ba55f2f5SFrançois Tigeot 
1324ba55f2f5SFrançois Tigeot 	if ((obj = ring->status_page.obj) == NULL) {
1325e3adcf8fSFrançois Tigeot 		int ret;
1326e3adcf8fSFrançois Tigeot 
1327ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(ring->dev, 4096);
1328e3adcf8fSFrançois Tigeot 		if (obj == NULL) {
1329e3adcf8fSFrançois Tigeot 			DRM_ERROR("Failed to allocate status page\n");
1330ba55f2f5SFrançois Tigeot 			return -ENOMEM;
1331e3adcf8fSFrançois Tigeot 		}
1332e3adcf8fSFrançois Tigeot 
1333ba55f2f5SFrançois Tigeot 		ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1334ba55f2f5SFrançois Tigeot 		if (ret)
1335e3adcf8fSFrançois Tigeot 			goto err_unref;
1336ba55f2f5SFrançois Tigeot 
1337ba55f2f5SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 4096, 0);
1338ba55f2f5SFrançois Tigeot 		if (ret) {
1339ba55f2f5SFrançois Tigeot err_unref:
1340ba55f2f5SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
1341ba55f2f5SFrançois Tigeot 			return ret;
1342ba55f2f5SFrançois Tigeot 		}
1343ba55f2f5SFrançois Tigeot 
1344ba55f2f5SFrançois Tigeot 		ring->status_page.obj = obj;
1345e3adcf8fSFrançois Tigeot 	}
1346e3adcf8fSFrançois Tigeot 
13479edbd4a0SFrançois Tigeot 	ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1348f4f90b23SFrançois Tigeot 	ring->status_page.page_addr = kmap(obj->pages[0]);
1349e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1350e3adcf8fSFrançois Tigeot 
1351b5c29a34SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1352e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1353e3adcf8fSFrançois Tigeot 
1354e3adcf8fSFrançois Tigeot 	return 0;
1355e3adcf8fSFrançois Tigeot }
1356e3adcf8fSFrançois Tigeot 
1357ba55f2f5SFrançois Tigeot static int init_phys_status_page(struct intel_engine_cs *ring)
1358686a02f1SFrançois Tigeot {
1359686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1360686a02f1SFrançois Tigeot 
1361686a02f1SFrançois Tigeot 	if (!dev_priv->status_page_dmah) {
1362686a02f1SFrançois Tigeot 		dev_priv->status_page_dmah =
1363b31e9d59SFrançois Tigeot 			drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1364686a02f1SFrançois Tigeot 		if (!dev_priv->status_page_dmah)
1365686a02f1SFrançois Tigeot 			return -ENOMEM;
1366686a02f1SFrançois Tigeot 	}
1367686a02f1SFrançois Tigeot 
1368686a02f1SFrançois Tigeot 	ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1369686a02f1SFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1370686a02f1SFrançois Tigeot 
1371686a02f1SFrançois Tigeot 	return 0;
1372686a02f1SFrançois Tigeot }
1373686a02f1SFrançois Tigeot 
1374ba55f2f5SFrançois Tigeot static int allocate_ring_buffer(struct intel_engine_cs *ring)
1375e3adcf8fSFrançois Tigeot {
1376ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1377ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1378e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1379e3adcf8fSFrançois Tigeot 	int ret;
1380e3adcf8fSFrançois Tigeot 
1381ba55f2f5SFrançois Tigeot 	if (intel_ring_initialized(ring))
1382ba55f2f5SFrançois Tigeot 		return 0;
1383e3adcf8fSFrançois Tigeot 
1384a2fdbec6SFrançois Tigeot 	obj = NULL;
1385a2fdbec6SFrançois Tigeot 	if (!HAS_LLC(dev))
1386ba55f2f5SFrançois Tigeot 		obj = i915_gem_object_create_stolen(dev, ringbuf->size);
1387a2fdbec6SFrançois Tigeot 	if (obj == NULL)
1388ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, ringbuf->size);
1389ba55f2f5SFrançois Tigeot 	if (obj == NULL)
1390ba55f2f5SFrançois Tigeot 		return -ENOMEM;
1391e3adcf8fSFrançois Tigeot 
1392ba55f2f5SFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1393e3adcf8fSFrançois Tigeot 	if (ret)
1394e3adcf8fSFrançois Tigeot 		goto err_unref;
1395e3adcf8fSFrançois Tigeot 
1396686a02f1SFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
1397686a02f1SFrançois Tigeot 	if (ret)
1398686a02f1SFrançois Tigeot 		goto err_unpin;
1399e3adcf8fSFrançois Tigeot 
1400ba55f2f5SFrançois Tigeot 	ringbuf->virtual_start =
14019edbd4a0SFrançois Tigeot 		ioremap_wc(dev->agp->base + i915_gem_obj_ggtt_offset(obj),
1402ba55f2f5SFrançois Tigeot 				ringbuf->size);
1403ba55f2f5SFrançois Tigeot 	if (ringbuf->virtual_start == NULL) {
1404e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1405e3adcf8fSFrançois Tigeot 		goto err_unpin;
1406e3adcf8fSFrançois Tigeot 	}
1407e3adcf8fSFrançois Tigeot 
1408ba55f2f5SFrançois Tigeot 	ringbuf->obj = obj;
1409ba55f2f5SFrançois Tigeot 	return 0;
1410ba55f2f5SFrançois Tigeot 
1411ba55f2f5SFrançois Tigeot err_unpin:
1412ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(obj);
1413ba55f2f5SFrançois Tigeot err_unref:
1414ba55f2f5SFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1415ba55f2f5SFrançois Tigeot 	return ret;
1416ba55f2f5SFrançois Tigeot }
1417ba55f2f5SFrançois Tigeot 
1418ba55f2f5SFrançois Tigeot static int intel_init_ring_buffer(struct drm_device *dev,
1419ba55f2f5SFrançois Tigeot 				  struct intel_engine_cs *ring)
1420ba55f2f5SFrançois Tigeot {
1421ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1422ba55f2f5SFrançois Tigeot 	int ret;
1423ba55f2f5SFrançois Tigeot 
1424ba55f2f5SFrançois Tigeot 	if (ringbuf == NULL) {
1425ba55f2f5SFrançois Tigeot 		ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1426ba55f2f5SFrançois Tigeot 		if (!ringbuf)
1427ba55f2f5SFrançois Tigeot 			return -ENOMEM;
1428ba55f2f5SFrançois Tigeot 		ring->buffer = ringbuf;
1429ba55f2f5SFrançois Tigeot 	}
1430ba55f2f5SFrançois Tigeot 
1431ba55f2f5SFrançois Tigeot 	ring->dev = dev;
1432ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1433ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
1434ba55f2f5SFrançois Tigeot 	ringbuf->size = 32 * PAGE_SIZE;
1435ba55f2f5SFrançois Tigeot 	memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
1436ba55f2f5SFrançois Tigeot 
1437ba55f2f5SFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
1438ba55f2f5SFrançois Tigeot 
1439ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
1440ba55f2f5SFrançois Tigeot 		ret = init_status_page(ring);
1441e3adcf8fSFrançois Tigeot 		if (ret)
1442ba55f2f5SFrançois Tigeot 			goto error;
1443ba55f2f5SFrançois Tigeot 	} else {
1444ba55f2f5SFrançois Tigeot 		BUG_ON(ring->id != RCS);
1445ba55f2f5SFrançois Tigeot 		ret = init_phys_status_page(ring);
1446ba55f2f5SFrançois Tigeot 		if (ret)
1447ba55f2f5SFrançois Tigeot 			goto error;
1448ba55f2f5SFrançois Tigeot 	}
1449ba55f2f5SFrançois Tigeot 
1450ba55f2f5SFrançois Tigeot 	ret = allocate_ring_buffer(ring);
1451ba55f2f5SFrançois Tigeot 	if (ret) {
1452ba55f2f5SFrançois Tigeot 		DRM_ERROR("Failed to allocate ringbuffer %s: %d\n", ring->name, ret);
1453ba55f2f5SFrançois Tigeot 		goto error;
1454ba55f2f5SFrançois Tigeot 	}
1455e3adcf8fSFrançois Tigeot 
1456e3adcf8fSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
1457e3adcf8fSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
1458e3adcf8fSFrançois Tigeot 	 * of the buffer.
1459e3adcf8fSFrançois Tigeot 	 */
1460ba55f2f5SFrançois Tigeot 	ringbuf->effective_size = ringbuf->size;
1461ba55f2f5SFrançois Tigeot 	if (IS_I830(dev) || IS_845G(dev))
1462ba55f2f5SFrançois Tigeot 		ringbuf->effective_size -= 2 * CACHELINE_BYTES;
1463ba55f2f5SFrançois Tigeot 
1464ba55f2f5SFrançois Tigeot 	ret = i915_cmd_parser_init_ring(ring);
1465ba55f2f5SFrançois Tigeot 	if (ret)
1466ba55f2f5SFrançois Tigeot 		goto error;
1467ba55f2f5SFrançois Tigeot 
1468ba55f2f5SFrançois Tigeot 	ret = ring->init(ring);
1469ba55f2f5SFrançois Tigeot 	if (ret)
1470ba55f2f5SFrançois Tigeot 		goto error;
1471e3adcf8fSFrançois Tigeot 
1472e3adcf8fSFrançois Tigeot 	return 0;
1473e3adcf8fSFrançois Tigeot 
1474ba55f2f5SFrançois Tigeot error:
1475ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
1476ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
1477e3adcf8fSFrançois Tigeot 	return ret;
1478e3adcf8fSFrançois Tigeot }
1479e3adcf8fSFrançois Tigeot 
1480ba55f2f5SFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
1481e3adcf8fSFrançois Tigeot {
1482ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ring->dev);
1483ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1484e3adcf8fSFrançois Tigeot 
1485ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
1486e3adcf8fSFrançois Tigeot 		return;
1487e3adcf8fSFrançois Tigeot 
1488ba55f2f5SFrançois Tigeot 	intel_stop_ring_buffer(ring);
1489ba55f2f5SFrançois Tigeot 	WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
1490b030f26bSFrançois Tigeot 
1491ba55f2f5SFrançois Tigeot 	pmap_unmapdev((vm_offset_t)ringbuf->virtual_start, ringbuf->size);
1492e3adcf8fSFrançois Tigeot 
1493ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(ringbuf->obj);
1494ba55f2f5SFrançois Tigeot 	drm_gem_object_unreference(&ringbuf->obj->base);
1495ba55f2f5SFrançois Tigeot 	ringbuf->obj = NULL;
14969edbd4a0SFrançois Tigeot 	ring->preallocated_lazy_request = NULL;
14979edbd4a0SFrançois Tigeot 	ring->outstanding_lazy_seqno = 0;
1498e3adcf8fSFrançois Tigeot 
1499e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
1500e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
1501e3adcf8fSFrançois Tigeot 
1502e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1503ba55f2f5SFrançois Tigeot 
1504ba55f2f5SFrançois Tigeot 	i915_cmd_parser_fini_ring(ring);
1505ba55f2f5SFrançois Tigeot 
1506ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
1507ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
1508e3adcf8fSFrançois Tigeot }
1509e3adcf8fSFrançois Tigeot 
1510ba55f2f5SFrançois Tigeot static int intel_ring_wait_request(struct intel_engine_cs *ring, int n)
1511e3adcf8fSFrançois Tigeot {
1512ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1513e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
1514e3adcf8fSFrançois Tigeot 	u32 seqno = 0;
1515e3adcf8fSFrançois Tigeot 	int ret;
1516e3adcf8fSFrançois Tigeot 
1517ba55f2f5SFrançois Tigeot 	if (ringbuf->last_retired_head != -1) {
1518ba55f2f5SFrançois Tigeot 		ringbuf->head = ringbuf->last_retired_head;
1519ba55f2f5SFrançois Tigeot 		ringbuf->last_retired_head = -1;
1520e3adcf8fSFrançois Tigeot 
1521ba55f2f5SFrançois Tigeot 		ringbuf->space = ring_space(ring);
1522ba55f2f5SFrançois Tigeot 		if (ringbuf->space >= n)
1523e3adcf8fSFrançois Tigeot 			return 0;
1524e3adcf8fSFrançois Tigeot 	}
1525e3adcf8fSFrançois Tigeot 
1526e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
1527ba55f2f5SFrançois Tigeot 		if (__ring_space(request->tail, ringbuf->tail, ringbuf->size) >= n) {
1528e3adcf8fSFrançois Tigeot 			seqno = request->seqno;
1529e3adcf8fSFrançois Tigeot 			break;
1530e3adcf8fSFrançois Tigeot 		}
1531e3adcf8fSFrançois Tigeot 	}
1532e3adcf8fSFrançois Tigeot 
1533e3adcf8fSFrançois Tigeot 	if (seqno == 0)
1534e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1535e3adcf8fSFrançois Tigeot 
1536ba55f2f5SFrançois Tigeot 	ret = i915_wait_seqno(ring, seqno);
1537e3adcf8fSFrançois Tigeot 	if (ret)
1538e3adcf8fSFrançois Tigeot 		return ret;
1539e3adcf8fSFrançois Tigeot 
1540ba55f2f5SFrançois Tigeot 	i915_gem_retire_requests_ring(ring);
1541ba55f2f5SFrançois Tigeot 	ringbuf->head = ringbuf->last_retired_head;
1542ba55f2f5SFrançois Tigeot 	ringbuf->last_retired_head = -1;
1543e3adcf8fSFrançois Tigeot 
1544ba55f2f5SFrançois Tigeot 	ringbuf->space = ring_space(ring);
1545e3adcf8fSFrançois Tigeot 	return 0;
1546e3adcf8fSFrançois Tigeot }
1547e3adcf8fSFrançois Tigeot 
1548ba55f2f5SFrançois Tigeot static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
1549e3adcf8fSFrançois Tigeot {
1550e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1551e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1552ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1553245593daSFrançois Tigeot 	unsigned long end;
1554e3adcf8fSFrançois Tigeot 	int ret;
1555e3adcf8fSFrançois Tigeot 
1556e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_request(ring, n);
1557e3adcf8fSFrançois Tigeot 	if (ret != -ENOSPC)
1558e3adcf8fSFrançois Tigeot 		return ret;
1559e3adcf8fSFrançois Tigeot 
15609edbd4a0SFrançois Tigeot 	/* force the tail write in case we have been skipping them */
15619edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
15629edbd4a0SFrançois Tigeot 
1563e3adcf8fSFrançois Tigeot 	/* With GEM the hangcheck timer should kick us out of the loop,
1564e3adcf8fSFrançois Tigeot 	 * leaving it early runs the risk of corrupting GEM state (due
1565e3adcf8fSFrançois Tigeot 	 * to running on almost untested codepaths). But on resume
1566e3adcf8fSFrançois Tigeot 	 * timers don't work yet, so prevent a complete hang in that
1567e3adcf8fSFrançois Tigeot 	 * case by choosing an insanely large timeout. */
1568e3440f96SFrançois Tigeot 	end = jiffies + 60 * HZ;
1569245593daSFrançois Tigeot 
1570ba55f2f5SFrançois Tigeot 	trace_i915_ring_wait_begin(ring);
1571e3adcf8fSFrançois Tigeot 	do {
1572ba55f2f5SFrançois Tigeot 		ringbuf->head = I915_READ_HEAD(ring);
1573ba55f2f5SFrançois Tigeot 		ringbuf->space = ring_space(ring);
1574ba55f2f5SFrançois Tigeot 		if (ringbuf->space >= n) {
1575ba55f2f5SFrançois Tigeot 			ret = 0;
1576ba55f2f5SFrançois Tigeot 			break;
1577e3adcf8fSFrançois Tigeot 		}
1578e3adcf8fSFrançois Tigeot 
1579e3adcf8fSFrançois Tigeot #if 0
1580ba55f2f5SFrançois Tigeot 		if (!drm_core_check_feature(dev, DRIVER_MODESET) &&
1581ba55f2f5SFrançois Tigeot 		    dev->primary->master) {
1582e3adcf8fSFrançois Tigeot 			struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1583e3adcf8fSFrançois Tigeot 			if (master_priv->sarea_priv)
1584e3adcf8fSFrançois Tigeot 				master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1585e3adcf8fSFrançois Tigeot 		}
1586e3adcf8fSFrançois Tigeot #else
1587e3adcf8fSFrançois Tigeot 		if (dev_priv->sarea_priv)
1588e3adcf8fSFrançois Tigeot 			dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1589e3adcf8fSFrançois Tigeot #endif
1590e3adcf8fSFrançois Tigeot 
1591ba55f2f5SFrançois Tigeot 
1592e3440f96SFrançois Tigeot 		msleep(1);
1593245593daSFrançois Tigeot 
1594ba55f2f5SFrançois Tigeot #if 0
1595ba55f2f5SFrançois Tigeot 		if (dev_priv->mm.interruptible && signal_pending(current)) {
1596ba55f2f5SFrançois Tigeot 			ret = -ERESTARTSYS;
1597ba55f2f5SFrançois Tigeot 			break;
1598ba55f2f5SFrançois Tigeot 		}
1599ba55f2f5SFrançois Tigeot #endif
1600ba55f2f5SFrançois Tigeot 
1601a2fdbec6SFrançois Tigeot 		ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1602a2fdbec6SFrançois Tigeot 					   dev_priv->mm.interruptible);
1603245593daSFrançois Tigeot 		if (ret)
1604ba55f2f5SFrançois Tigeot 			break;
1605ba55f2f5SFrançois Tigeot 
1606ba55f2f5SFrançois Tigeot 		if (time_after(jiffies, end)) {
1607ba55f2f5SFrançois Tigeot 			ret = -EBUSY;
1608ba55f2f5SFrançois Tigeot 			break;
1609ba55f2f5SFrançois Tigeot 		}
1610ba55f2f5SFrançois Tigeot 	} while (1);
1611a2fdbec6SFrançois Tigeot 	trace_i915_ring_wait_end(ring);
1612ba55f2f5SFrançois Tigeot 	return ret;
1613e3adcf8fSFrançois Tigeot }
1614e3adcf8fSFrançois Tigeot 
1615ba55f2f5SFrançois Tigeot static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
1616b030f26bSFrançois Tigeot {
1617b030f26bSFrançois Tigeot 	uint32_t __iomem *virt;
1618ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1619ba55f2f5SFrançois Tigeot 	int rem = ringbuf->size - ringbuf->tail;
1620b030f26bSFrançois Tigeot 
1621ba55f2f5SFrançois Tigeot 	if (ringbuf->space < rem) {
1622b030f26bSFrançois Tigeot 		int ret = ring_wait_for_space(ring, rem);
1623b030f26bSFrançois Tigeot 		if (ret)
1624b030f26bSFrançois Tigeot 			return ret;
1625b030f26bSFrançois Tigeot 	}
1626b030f26bSFrançois Tigeot 
1627ba55f2f5SFrançois Tigeot 	virt = (unsigned int *)((char *)ringbuf->virtual_start + ringbuf->tail);
1628b030f26bSFrançois Tigeot 	rem /= 4;
1629b030f26bSFrançois Tigeot 	while (rem--)
1630686a02f1SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
1631b030f26bSFrançois Tigeot 
1632ba55f2f5SFrançois Tigeot 	ringbuf->tail = 0;
1633ba55f2f5SFrançois Tigeot 	ringbuf->space = ring_space(ring);
1634b030f26bSFrançois Tigeot 
1635b030f26bSFrançois Tigeot 	return 0;
1636b030f26bSFrançois Tigeot }
1637b030f26bSFrançois Tigeot 
1638ba55f2f5SFrançois Tigeot int intel_ring_idle(struct intel_engine_cs *ring)
1639b030f26bSFrançois Tigeot {
1640b5c29a34SFrançois Tigeot 	u32 seqno;
1641b5c29a34SFrançois Tigeot 	int ret;
1642b5c29a34SFrançois Tigeot 
1643b5c29a34SFrançois Tigeot 	/* We need to add any requests required to flush the objects and ring */
16449edbd4a0SFrançois Tigeot 	if (ring->outstanding_lazy_seqno) {
16455d0b1887SFrançois Tigeot 		ret = i915_add_request(ring, NULL);
1646b5c29a34SFrançois Tigeot 		if (ret)
1647b5c29a34SFrançois Tigeot 			return ret;
1648b5c29a34SFrançois Tigeot 	}
1649b5c29a34SFrançois Tigeot 
1650b5c29a34SFrançois Tigeot 	/* Wait upon the last request to be completed */
1651b5c29a34SFrançois Tigeot 	if (list_empty(&ring->request_list))
1652b5c29a34SFrançois Tigeot 		return 0;
1653b5c29a34SFrançois Tigeot 
1654b5c29a34SFrançois Tigeot 	seqno = list_entry(ring->request_list.prev,
1655b5c29a34SFrançois Tigeot 			   struct drm_i915_gem_request,
1656b5c29a34SFrançois Tigeot 			   list)->seqno;
1657b5c29a34SFrançois Tigeot 
1658b5c29a34SFrançois Tigeot 	return i915_wait_seqno(ring, seqno);
1659b5c29a34SFrançois Tigeot }
1660b5c29a34SFrançois Tigeot 
1661b5c29a34SFrançois Tigeot static int
1662ba55f2f5SFrançois Tigeot intel_ring_alloc_seqno(struct intel_engine_cs *ring)
1663b5c29a34SFrançois Tigeot {
16649edbd4a0SFrançois Tigeot 	if (ring->outstanding_lazy_seqno)
1665b5c29a34SFrançois Tigeot 		return 0;
1666b5c29a34SFrançois Tigeot 
16679edbd4a0SFrançois Tigeot 	if (ring->preallocated_lazy_request == NULL) {
16689edbd4a0SFrançois Tigeot 		struct drm_i915_gem_request *request;
16699edbd4a0SFrançois Tigeot 
16709edbd4a0SFrançois Tigeot 		request = kmalloc(sizeof(*request), M_DRM, M_WAITOK);
16719edbd4a0SFrançois Tigeot 		if (request == NULL)
16729edbd4a0SFrançois Tigeot 			return -ENOMEM;
16739edbd4a0SFrançois Tigeot 
16749edbd4a0SFrançois Tigeot 		ring->preallocated_lazy_request = request;
1675b030f26bSFrançois Tigeot 	}
1676b030f26bSFrançois Tigeot 
16779edbd4a0SFrançois Tigeot 	return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
16789edbd4a0SFrançois Tigeot }
16799edbd4a0SFrançois Tigeot 
1680ba55f2f5SFrançois Tigeot static int __intel_ring_prepare(struct intel_engine_cs *ring,
1681a2fdbec6SFrançois Tigeot 				int bytes)
1682a2fdbec6SFrançois Tigeot {
1683ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
1684a2fdbec6SFrançois Tigeot 	int ret;
1685a2fdbec6SFrançois Tigeot 
1686ba55f2f5SFrançois Tigeot 	if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
1687a2fdbec6SFrançois Tigeot 		ret = intel_wrap_ring_buffer(ring);
1688a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
1689a2fdbec6SFrançois Tigeot 			return ret;
1690a2fdbec6SFrançois Tigeot 	}
1691a2fdbec6SFrançois Tigeot 
1692ba55f2f5SFrançois Tigeot 	if (unlikely(ringbuf->space < bytes)) {
1693a2fdbec6SFrançois Tigeot 		ret = ring_wait_for_space(ring, bytes);
1694a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
1695a2fdbec6SFrançois Tigeot 			return ret;
1696a2fdbec6SFrançois Tigeot 	}
1697a2fdbec6SFrançois Tigeot 
1698a2fdbec6SFrançois Tigeot 	return 0;
1699a2fdbec6SFrançois Tigeot }
1700a2fdbec6SFrançois Tigeot 
1701ba55f2f5SFrançois Tigeot int intel_ring_begin(struct intel_engine_cs *ring,
1702e3adcf8fSFrançois Tigeot 		     int num_dwords)
1703e3adcf8fSFrançois Tigeot {
1704ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1705e3adcf8fSFrançois Tigeot 	int ret;
1706e3adcf8fSFrançois Tigeot 
1707a2fdbec6SFrançois Tigeot 	ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1708a2fdbec6SFrançois Tigeot 				   dev_priv->mm.interruptible);
1709245593daSFrançois Tigeot 	if (ret)
1710245593daSFrançois Tigeot 		return ret;
1711e3adcf8fSFrançois Tigeot 
17129edbd4a0SFrançois Tigeot 	ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
17139edbd4a0SFrançois Tigeot 	if (ret)
17149edbd4a0SFrançois Tigeot 		return ret;
17159edbd4a0SFrançois Tigeot 
1716b5c29a34SFrançois Tigeot 	/* Preallocate the olr before touching the ring */
1717b5c29a34SFrançois Tigeot 	ret = intel_ring_alloc_seqno(ring);
1718b5c29a34SFrançois Tigeot 	if (ret)
1719b5c29a34SFrançois Tigeot 		return ret;
1720b5c29a34SFrançois Tigeot 
1721ba55f2f5SFrançois Tigeot 	ring->buffer->space -= num_dwords * sizeof(uint32_t);
17229edbd4a0SFrançois Tigeot 	return 0;
17239edbd4a0SFrançois Tigeot }
17249edbd4a0SFrançois Tigeot 
17259edbd4a0SFrançois Tigeot /* Align the ring tail to a cacheline boundary */
1726ba55f2f5SFrançois Tigeot int intel_ring_cacheline_align(struct intel_engine_cs *ring)
17279edbd4a0SFrançois Tigeot {
1728ba55f2f5SFrançois Tigeot 	int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
17299edbd4a0SFrançois Tigeot 	int ret;
17309edbd4a0SFrançois Tigeot 
17319edbd4a0SFrançois Tigeot 	if (num_dwords == 0)
17329edbd4a0SFrançois Tigeot 		return 0;
17339edbd4a0SFrançois Tigeot 
1734ba55f2f5SFrançois Tigeot 	num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
17359edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, num_dwords);
17369edbd4a0SFrançois Tigeot 	if (ret)
17379edbd4a0SFrançois Tigeot 		return ret;
17389edbd4a0SFrançois Tigeot 
17399edbd4a0SFrançois Tigeot 	while (num_dwords--)
17409edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
17419edbd4a0SFrançois Tigeot 
17429edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
17439edbd4a0SFrançois Tigeot 
17449edbd4a0SFrançois Tigeot 	return 0;
1745e3adcf8fSFrançois Tigeot }
1746e3adcf8fSFrançois Tigeot 
1747ba55f2f5SFrançois Tigeot void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
1748a2fdbec6SFrançois Tigeot {
1749a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1750a2fdbec6SFrançois Tigeot 
17519edbd4a0SFrançois Tigeot 	BUG_ON(ring->outstanding_lazy_seqno);
1752a2fdbec6SFrançois Tigeot 
1753a2fdbec6SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 6) {
1754a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
1755a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
17569edbd4a0SFrançois Tigeot 		if (HAS_VEBOX(ring->dev))
17579edbd4a0SFrançois Tigeot 			I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
1758e3adcf8fSFrançois Tigeot 	}
1759e3adcf8fSFrançois Tigeot 
1760a2fdbec6SFrançois Tigeot 	ring->set_seqno(ring, seqno);
17615d0b1887SFrançois Tigeot 	ring->hangcheck.seqno = seqno;
1762e3adcf8fSFrançois Tigeot }
1763e3adcf8fSFrançois Tigeot 
1764ba55f2f5SFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
1765f4e1c372SFrançois Tigeot 				     u32 value)
1766e3adcf8fSFrançois Tigeot {
1767ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1768e3adcf8fSFrançois Tigeot 
1769e3adcf8fSFrançois Tigeot        /* Every tail move must follow the sequence below */
1770f4e1c372SFrançois Tigeot 
1771f4e1c372SFrançois Tigeot 	/* Disable notification that the ring is IDLE. The GT
1772f4e1c372SFrançois Tigeot 	 * will then assume that it is busy and bring it out of rc6.
1773f4e1c372SFrançois Tigeot 	 */
1774e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1775f4e1c372SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1776e3adcf8fSFrançois Tigeot 
1777f4e1c372SFrançois Tigeot 	/* Clear the context id. Here be magic! */
1778f4e1c372SFrançois Tigeot 	I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1779e3adcf8fSFrançois Tigeot 
1780f4e1c372SFrançois Tigeot 	/* Wait for the ring not to be idle, i.e. for it to wake up. */
1781f4e1c372SFrançois Tigeot 	if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1782f4e1c372SFrançois Tigeot 		      GEN6_BSD_SLEEP_INDICATOR) == 0,
1783f4e1c372SFrançois Tigeot 		     50))
1784f4e1c372SFrançois Tigeot 		DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
1785f4e1c372SFrançois Tigeot 
1786f4e1c372SFrançois Tigeot 	/* Now that the ring is fully powered up, update the tail */
1787e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
1788f4e1c372SFrançois Tigeot 	POSTING_READ(RING_TAIL(ring->mmio_base));
1789f4e1c372SFrançois Tigeot 
1790f4e1c372SFrançois Tigeot 	/* Let the ring send IDLE messages to the GT again,
1791f4e1c372SFrançois Tigeot 	 * and so let it sleep to conserve power when idle.
1792f4e1c372SFrançois Tigeot 	 */
1793e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1794f4e1c372SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1795e3adcf8fSFrançois Tigeot }
1796e3adcf8fSFrançois Tigeot 
1797ba55f2f5SFrançois Tigeot static int gen6_bsd_ring_flush(struct intel_engine_cs *ring,
1798b5c29a34SFrançois Tigeot 			       u32 invalidate, u32 flush)
1799e3adcf8fSFrançois Tigeot {
1800e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1801e3adcf8fSFrançois Tigeot 	int ret;
1802e3adcf8fSFrançois Tigeot 
1803e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1804e3adcf8fSFrançois Tigeot 	if (ret)
1805e3adcf8fSFrançois Tigeot 		return ret;
1806e3adcf8fSFrançois Tigeot 
1807e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
18089edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
18099edbd4a0SFrançois Tigeot 		cmd += 1;
1810b5c29a34SFrançois Tigeot 	/*
1811b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.5 - video engine command streamer:
1812b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
1813b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
1814b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
1815b5c29a34SFrançois Tigeot 	 */
1816e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
1817b5c29a34SFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1818b5c29a34SFrançois Tigeot 			MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1819e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1820b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
18219edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
18229edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
18239edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
18249edbd4a0SFrançois Tigeot 	} else  {
18259edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0);
18269edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
18279edbd4a0SFrançois Tigeot 	}
18289edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
18299edbd4a0SFrançois Tigeot 	return 0;
18309edbd4a0SFrançois Tigeot }
18319edbd4a0SFrançois Tigeot 
18329edbd4a0SFrançois Tigeot static int
1833ba55f2f5SFrançois Tigeot gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
1834ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
18359edbd4a0SFrançois Tigeot 			      unsigned flags)
18369edbd4a0SFrançois Tigeot {
18379edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
18389edbd4a0SFrançois Tigeot 	bool ppgtt = dev_priv->mm.aliasing_ppgtt != NULL &&
18399edbd4a0SFrançois Tigeot 		!(flags & I915_DISPATCH_SECURE);
18409edbd4a0SFrançois Tigeot 	int ret;
18419edbd4a0SFrançois Tigeot 
18429edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
18439edbd4a0SFrançois Tigeot 	if (ret)
18449edbd4a0SFrançois Tigeot 		return ret;
18459edbd4a0SFrançois Tigeot 
18469edbd4a0SFrançois Tigeot 	/* FIXME(BDW): Address space and security selectors. */
18479edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
1848ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, lower_32_bits(offset));
1849ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, upper_32_bits(offset));
1850e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1851e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
18529edbd4a0SFrançois Tigeot 
1853e3adcf8fSFrançois Tigeot 	return 0;
1854e3adcf8fSFrançois Tigeot }
1855e3adcf8fSFrançois Tigeot 
1856e3adcf8fSFrançois Tigeot static int
1857ba55f2f5SFrançois Tigeot hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
1858ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
1859b5c29a34SFrançois Tigeot 			      unsigned flags)
1860e3adcf8fSFrançois Tigeot {
1861e3adcf8fSFrançois Tigeot 	int ret;
1862e3adcf8fSFrançois Tigeot 
1863e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1864e3adcf8fSFrançois Tigeot 	if (ret)
1865e3adcf8fSFrançois Tigeot 		return ret;
1866e3adcf8fSFrançois Tigeot 
1867b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
1868b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
1869b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
1870b5c29a34SFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1871b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset);
1872b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
1873b5c29a34SFrançois Tigeot 
1874b5c29a34SFrançois Tigeot 	return 0;
1875b5c29a34SFrançois Tigeot }
1876b5c29a34SFrançois Tigeot 
1877b5c29a34SFrançois Tigeot static int
1878ba55f2f5SFrançois Tigeot gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
1879ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
1880b5c29a34SFrançois Tigeot 			      unsigned flags)
1881b5c29a34SFrançois Tigeot {
1882b5c29a34SFrançois Tigeot 	int ret;
1883b5c29a34SFrançois Tigeot 
1884b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1885b5c29a34SFrançois Tigeot 	if (ret)
1886b5c29a34SFrançois Tigeot 		return ret;
1887b5c29a34SFrançois Tigeot 
1888b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
1889b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1890b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
1891e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1892e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1893e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1894e3adcf8fSFrançois Tigeot 
1895e3adcf8fSFrançois Tigeot 	return 0;
1896e3adcf8fSFrançois Tigeot }
1897e3adcf8fSFrançois Tigeot 
1898e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
1899e3adcf8fSFrançois Tigeot 
1900ba55f2f5SFrançois Tigeot static int gen6_ring_flush(struct intel_engine_cs *ring,
1901b5c29a34SFrançois Tigeot 			   u32 invalidate, u32 flush)
1902e3adcf8fSFrançois Tigeot {
19035d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1904e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1905e3adcf8fSFrançois Tigeot 	int ret;
1906e3adcf8fSFrançois Tigeot 
1907e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1908e3adcf8fSFrançois Tigeot 	if (ret)
1909e3adcf8fSFrançois Tigeot 		return ret;
1910e3adcf8fSFrançois Tigeot 
1911e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
19129edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
19139edbd4a0SFrançois Tigeot 		cmd += 1;
1914b5c29a34SFrançois Tigeot 	/*
1915b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.3 - blitter engine command streamer:
1916b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
1917b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
1918b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
1919b5c29a34SFrançois Tigeot 	 */
1920e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
1921b5c29a34SFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
1922b5c29a34SFrançois Tigeot 			MI_FLUSH_DW_OP_STOREDW;
1923e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1924b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
19259edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
19269edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
19279edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
19289edbd4a0SFrançois Tigeot 	} else  {
1929e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, 0);
1930e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
19319edbd4a0SFrançois Tigeot 	}
1932e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
19335d0b1887SFrançois Tigeot 
19349edbd4a0SFrançois Tigeot 	if (IS_GEN7(dev) && !invalidate && flush)
19355d0b1887SFrançois Tigeot 		return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
19365d0b1887SFrançois Tigeot 
1937e3adcf8fSFrançois Tigeot 	return 0;
1938e3adcf8fSFrançois Tigeot }
1939e3adcf8fSFrançois Tigeot 
1940e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
1941e3adcf8fSFrançois Tigeot {
1942ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1943ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
1944e3adcf8fSFrançois Tigeot 
1945686a02f1SFrançois Tigeot 	ring->name = "render ring";
1946686a02f1SFrançois Tigeot 	ring->id = RCS;
1947686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
1948686a02f1SFrançois Tigeot 
1949e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
1950e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
1951b5c29a34SFrançois Tigeot 		ring->flush = gen7_render_ring_flush;
1952b5c29a34SFrançois Tigeot 		if (INTEL_INFO(dev)->gen == 6)
1953e3adcf8fSFrançois Tigeot 			ring->flush = gen6_render_ring_flush;
19549edbd4a0SFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 8) {
19559edbd4a0SFrançois Tigeot 			ring->flush = gen8_render_ring_flush;
19569edbd4a0SFrançois Tigeot 			ring->irq_get = gen8_ring_get_irq;
19579edbd4a0SFrançois Tigeot 			ring->irq_put = gen8_ring_put_irq;
19589edbd4a0SFrançois Tigeot 		} else {
1959686a02f1SFrançois Tigeot 			ring->irq_get = gen6_ring_get_irq;
1960686a02f1SFrançois Tigeot 			ring->irq_put = gen6_ring_put_irq;
19619edbd4a0SFrançois Tigeot 		}
19625d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
1963e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
1964a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
1965ba55f2f5SFrançois Tigeot 		ring->semaphore.sync_to = gen6_ring_sync;
1966ba55f2f5SFrançois Tigeot 		ring->semaphore.signal = gen6_signal;
1967ba55f2f5SFrançois Tigeot 		/*
1968ba55f2f5SFrançois Tigeot 		 * The current semaphore is only applied on pre-gen8 platform.
1969ba55f2f5SFrançois Tigeot 		 * And there is no VCS2 ring on the pre-gen8 platform. So the
1970ba55f2f5SFrançois Tigeot 		 * semaphore between RCS and VCS2 is initialized as INVALID.
1971ba55f2f5SFrançois Tigeot 		 * Gen8 will initialize the sema between VCS2 and RCS later.
1972ba55f2f5SFrançois Tigeot 		 */
1973ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
1974ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
1975ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
1976ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
1977ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
1978ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
1979ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
1980ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
1981ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
1982ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
1983e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
1984e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
1985686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
1986e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
1987a2fdbec6SFrançois Tigeot 		ring->set_seqno = pc_render_set_seqno;
1988686a02f1SFrançois Tigeot 		ring->irq_get = gen5_ring_get_irq;
1989686a02f1SFrançois Tigeot 		ring->irq_put = gen5_ring_put_irq;
19905d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
19915d0b1887SFrançois Tigeot 					GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
1992686a02f1SFrançois Tigeot 	} else {
1993686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
1994686a02f1SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 4)
1995686a02f1SFrançois Tigeot 			ring->flush = gen2_render_ring_flush;
1996686a02f1SFrançois Tigeot 		else
1997686a02f1SFrançois Tigeot 			ring->flush = gen4_render_ring_flush;
1998686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
1999a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2000686a02f1SFrançois Tigeot 		if (IS_GEN2(dev)) {
2001686a02f1SFrançois Tigeot 			ring->irq_get = i8xx_ring_get_irq;
2002686a02f1SFrançois Tigeot 			ring->irq_put = i8xx_ring_put_irq;
2003686a02f1SFrançois Tigeot 		} else {
2004686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2005686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2006e3adcf8fSFrançois Tigeot 		}
2007686a02f1SFrançois Tigeot 		ring->irq_enable_mask = I915_USER_INTERRUPT;
2008686a02f1SFrançois Tigeot 	}
2009686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2010b5c29a34SFrançois Tigeot 	if (IS_HASWELL(dev))
2011b5c29a34SFrançois Tigeot 		ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
20129edbd4a0SFrançois Tigeot 	else if (IS_GEN8(dev))
20139edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2014b5c29a34SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 6)
2015686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2016686a02f1SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 4)
2017686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2018686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
2019686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2020686a02f1SFrançois Tigeot 	else
2021686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
2022686a02f1SFrançois Tigeot 	ring->init = init_render_ring;
2023686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
2024e3adcf8fSFrançois Tigeot 
2025b5c29a34SFrançois Tigeot 	/* Workaround batchbuffer to combat CS tlb bug. */
2026b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev)) {
2027b5c29a34SFrançois Tigeot 		struct drm_i915_gem_object *obj;
2028b5c29a34SFrançois Tigeot 		int ret;
2029b5c29a34SFrançois Tigeot 
2030b5c29a34SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
2031b5c29a34SFrançois Tigeot 		if (obj == NULL) {
2032b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to allocate batch bo\n");
2033b5c29a34SFrançois Tigeot 			return -ENOMEM;
2034b5c29a34SFrançois Tigeot 		}
2035b5c29a34SFrançois Tigeot 
2036ba55f2f5SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
2037b5c29a34SFrançois Tigeot 		if (ret != 0) {
2038b5c29a34SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
2039b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to ping batch bo\n");
2040b5c29a34SFrançois Tigeot 			return ret;
2041b5c29a34SFrançois Tigeot 		}
2042b5c29a34SFrançois Tigeot 
20439edbd4a0SFrançois Tigeot 		ring->scratch.obj = obj;
20449edbd4a0SFrançois Tigeot 		ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
2045e3adcf8fSFrançois Tigeot 	}
2046e3adcf8fSFrançois Tigeot 
2047e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2048e3adcf8fSFrançois Tigeot }
2049e3adcf8fSFrançois Tigeot 
2050686a02f1SFrançois Tigeot int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
2051e3adcf8fSFrançois Tigeot {
2052ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2053ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
2054ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2055b5c29a34SFrançois Tigeot 	int ret;
2056e3adcf8fSFrançois Tigeot 
2057ba55f2f5SFrançois Tigeot 	if (ringbuf == NULL) {
2058ba55f2f5SFrançois Tigeot 		ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2059ba55f2f5SFrançois Tigeot 		if (!ringbuf)
2060ba55f2f5SFrançois Tigeot 			return -ENOMEM;
2061ba55f2f5SFrançois Tigeot 		ring->buffer = ringbuf;
2062ba55f2f5SFrançois Tigeot 	}
2063ba55f2f5SFrançois Tigeot 
2064686a02f1SFrançois Tigeot 	ring->name = "render ring";
2065686a02f1SFrançois Tigeot 	ring->id = RCS;
2066686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
2067686a02f1SFrançois Tigeot 
2068e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
2069686a02f1SFrançois Tigeot 		/* non-kms not supported on gen6+ */
2070ba55f2f5SFrançois Tigeot 		ret = -ENODEV;
2071ba55f2f5SFrançois Tigeot 		goto err_ringbuf;
2072e3adcf8fSFrançois Tigeot 	}
2073e3adcf8fSFrançois Tigeot 
2074686a02f1SFrançois Tigeot 	/* Note: gem is not supported on gen5/ilk without kms (the corresponding
2075686a02f1SFrançois Tigeot 	 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
2076686a02f1SFrançois Tigeot 	 * the special gen5 functions. */
2077686a02f1SFrançois Tigeot 	ring->add_request = i9xx_add_request;
2078686a02f1SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 4)
2079686a02f1SFrançois Tigeot 		ring->flush = gen2_render_ring_flush;
2080686a02f1SFrançois Tigeot 	else
2081686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
2082686a02f1SFrançois Tigeot 	ring->get_seqno = ring_get_seqno;
2083a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
2084686a02f1SFrançois Tigeot 	if (IS_GEN2(dev)) {
2085686a02f1SFrançois Tigeot 		ring->irq_get = i8xx_ring_get_irq;
2086686a02f1SFrançois Tigeot 		ring->irq_put = i8xx_ring_put_irq;
2087686a02f1SFrançois Tigeot 	} else {
2088686a02f1SFrançois Tigeot 		ring->irq_get = i9xx_ring_get_irq;
2089686a02f1SFrançois Tigeot 		ring->irq_put = i9xx_ring_put_irq;
2090686a02f1SFrançois Tigeot 	}
2091686a02f1SFrançois Tigeot 	ring->irq_enable_mask = I915_USER_INTERRUPT;
2092686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2093686a02f1SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4)
2094686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2095686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
2096686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2097686a02f1SFrançois Tigeot 	else
2098686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
2099686a02f1SFrançois Tigeot 	ring->init = init_render_ring;
2100686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
2101686a02f1SFrançois Tigeot 
2102e3adcf8fSFrançois Tigeot 	ring->dev = dev;
2103e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
2104e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
2105e3adcf8fSFrançois Tigeot 
2106ba55f2f5SFrançois Tigeot 	ringbuf->size = size;
2107ba55f2f5SFrançois Tigeot 	ringbuf->effective_size = ringbuf->size;
2108b5c29a34SFrançois Tigeot 	if (IS_I830(ring->dev) || IS_845G(ring->dev))
2109ba55f2f5SFrançois Tigeot 		ringbuf->effective_size -= 2 * CACHELINE_BYTES;
2110e3adcf8fSFrançois Tigeot 
2111ba55f2f5SFrançois Tigeot 	ringbuf->virtual_start = ioremap_wc(start, size);
2112ba55f2f5SFrançois Tigeot 	if (ringbuf->virtual_start == NULL) {
2113e3adcf8fSFrançois Tigeot 		DRM_ERROR("can not ioremap virtual address for"
2114e3adcf8fSFrançois Tigeot 			  " ring buffer\n");
2115ba55f2f5SFrançois Tigeot 		ret = -ENOMEM;
2116ba55f2f5SFrançois Tigeot 		goto err_ringbuf;
2117e3adcf8fSFrançois Tigeot 	}
2118e3adcf8fSFrançois Tigeot 
2119b5c29a34SFrançois Tigeot 	if (!I915_NEED_GFX_HWS(dev)) {
21205d0b1887SFrançois Tigeot 		ret = init_phys_status_page(ring);
2121b5c29a34SFrançois Tigeot 		if (ret)
2122ba55f2f5SFrançois Tigeot 			goto err_vstart;
2123b5c29a34SFrançois Tigeot 	}
2124b5c29a34SFrançois Tigeot 
2125e3adcf8fSFrançois Tigeot 	return 0;
2126ba55f2f5SFrançois Tigeot 
2127ba55f2f5SFrançois Tigeot err_vstart:
2128ba55f2f5SFrançois Tigeot 	pmap_unmapdev((vm_offset_t)ring->buffer->virtual_start, size);
2129ba55f2f5SFrançois Tigeot err_ringbuf:
2130ba55f2f5SFrançois Tigeot 	kfree(ringbuf);
2131ba55f2f5SFrançois Tigeot 	ring->buffer = NULL;
2132ba55f2f5SFrançois Tigeot 	return ret;
2133e3adcf8fSFrançois Tigeot }
2134e3adcf8fSFrançois Tigeot 
2135e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
2136e3adcf8fSFrançois Tigeot {
2137ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2138ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS];
2139e3adcf8fSFrançois Tigeot 
2140686a02f1SFrançois Tigeot 	ring->name = "bsd ring";
2141686a02f1SFrançois Tigeot 	ring->id = VCS;
2142686a02f1SFrançois Tigeot 
2143686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
21449edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
2145686a02f1SFrançois Tigeot 		ring->mmio_base = GEN6_BSD_RING_BASE;
2146686a02f1SFrançois Tigeot 		/* gen6 bsd needs a special wa for tail updates */
2147686a02f1SFrançois Tigeot 		if (IS_GEN6(dev))
2148686a02f1SFrançois Tigeot 			ring->write_tail = gen6_bsd_ring_write_tail;
21495d0b1887SFrançois Tigeot 		ring->flush = gen6_bsd_ring_flush;
2150686a02f1SFrançois Tigeot 		ring->add_request = gen6_add_request;
2151686a02f1SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2152a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
21539edbd4a0SFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 8) {
21549edbd4a0SFrançois Tigeot 			ring->irq_enable_mask =
21559edbd4a0SFrançois Tigeot 				GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
21569edbd4a0SFrançois Tigeot 			ring->irq_get = gen8_ring_get_irq;
21579edbd4a0SFrançois Tigeot 			ring->irq_put = gen8_ring_put_irq;
21589edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
21599edbd4a0SFrançois Tigeot 				gen8_ring_dispatch_execbuffer;
21609edbd4a0SFrançois Tigeot 		} else {
21615d0b1887SFrançois Tigeot 			ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2162686a02f1SFrançois Tigeot 			ring->irq_get = gen6_ring_get_irq;
2163686a02f1SFrançois Tigeot 			ring->irq_put = gen6_ring_put_irq;
21649edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
21659edbd4a0SFrançois Tigeot 				gen6_ring_dispatch_execbuffer;
21669edbd4a0SFrançois Tigeot 		}
2167ba55f2f5SFrançois Tigeot 		ring->semaphore.sync_to = gen6_ring_sync;
2168ba55f2f5SFrançois Tigeot 		ring->semaphore.signal = gen6_signal;
2169ba55f2f5SFrançois Tigeot 		/*
2170ba55f2f5SFrançois Tigeot 		 * The current semaphore is only applied on pre-gen8 platform.
2171ba55f2f5SFrançois Tigeot 		 * And there is no VCS2 ring on the pre-gen8 platform. So the
2172ba55f2f5SFrançois Tigeot 		 * semaphore between VCS and VCS2 is initialized as INVALID.
2173ba55f2f5SFrançois Tigeot 		 * Gen8 will initialize the sema between VCS2 and VCS later.
2174ba55f2f5SFrançois Tigeot 		 */
2175ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2176ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2177ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2178ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2179ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2180ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2181ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2182ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2183ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2184ba55f2f5SFrançois Tigeot 		ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2185686a02f1SFrançois Tigeot 	} else {
2186686a02f1SFrançois Tigeot 		ring->mmio_base = BSD_RING_BASE;
2187686a02f1SFrançois Tigeot 		ring->flush = bsd_ring_flush;
2188686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2189686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2190a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2191686a02f1SFrançois Tigeot 		if (IS_GEN5(dev)) {
21925d0b1887SFrançois Tigeot 			ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2193686a02f1SFrançois Tigeot 			ring->irq_get = gen5_ring_get_irq;
2194686a02f1SFrançois Tigeot 			ring->irq_put = gen5_ring_put_irq;
2195686a02f1SFrançois Tigeot 		} else {
2196686a02f1SFrançois Tigeot 			ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2197686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2198686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2199686a02f1SFrançois Tigeot 		}
2200686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2201686a02f1SFrançois Tigeot 	}
2202686a02f1SFrançois Tigeot 	ring->init = init_ring_common;
2203e3adcf8fSFrançois Tigeot 
2204e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2205e3adcf8fSFrançois Tigeot }
2206e3adcf8fSFrançois Tigeot 
2207ba55f2f5SFrançois Tigeot /**
2208ba55f2f5SFrançois Tigeot  * Initialize the second BSD ring for Broadwell GT3.
2209ba55f2f5SFrançois Tigeot  * It is noted that this only exists on Broadwell GT3.
2210ba55f2f5SFrançois Tigeot  */
2211ba55f2f5SFrançois Tigeot int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2212ba55f2f5SFrançois Tigeot {
2213ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2214ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
2215ba55f2f5SFrançois Tigeot 
2216ba55f2f5SFrançois Tigeot 	if ((INTEL_INFO(dev)->gen != 8)) {
2217ba55f2f5SFrançois Tigeot 		DRM_ERROR("No dual-BSD ring on non-BDW machine\n");
2218ba55f2f5SFrançois Tigeot 		return -EINVAL;
2219ba55f2f5SFrançois Tigeot 	}
2220ba55f2f5SFrançois Tigeot 
2221ba55f2f5SFrançois Tigeot 	ring->name = "bds2_ring";
2222ba55f2f5SFrançois Tigeot 	ring->id = VCS2;
2223ba55f2f5SFrançois Tigeot 
2224ba55f2f5SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2225ba55f2f5SFrançois Tigeot 	ring->mmio_base = GEN8_BSD2_RING_BASE;
2226ba55f2f5SFrançois Tigeot 	ring->flush = gen6_bsd_ring_flush;
2227ba55f2f5SFrançois Tigeot 	ring->add_request = gen6_add_request;
2228ba55f2f5SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2229ba55f2f5SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
2230ba55f2f5SFrançois Tigeot 	ring->irq_enable_mask =
2231ba55f2f5SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2232ba55f2f5SFrançois Tigeot 	ring->irq_get = gen8_ring_get_irq;
2233ba55f2f5SFrançois Tigeot 	ring->irq_put = gen8_ring_put_irq;
2234ba55f2f5SFrançois Tigeot 	ring->dispatch_execbuffer =
2235ba55f2f5SFrançois Tigeot 			gen8_ring_dispatch_execbuffer;
2236ba55f2f5SFrançois Tigeot 	ring->semaphore.sync_to = gen6_ring_sync;
2237ba55f2f5SFrançois Tigeot 	ring->semaphore.signal = gen6_signal;
2238ba55f2f5SFrançois Tigeot 	/*
2239ba55f2f5SFrançois Tigeot 	 * The current semaphore is only applied on the pre-gen8. And there
2240ba55f2f5SFrançois Tigeot 	 * is no bsd2 ring on the pre-gen8. So now the semaphore_register
2241ba55f2f5SFrançois Tigeot 	 * between VCS2 and other ring is initialized as invalid.
2242ba55f2f5SFrançois Tigeot 	 * Gen8 will initialize the sema between VCS2 and other ring later.
2243ba55f2f5SFrançois Tigeot 	 */
2244ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2245ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2246ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2247ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2248ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2249ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2250ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2251ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2252ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2253ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2254ba55f2f5SFrançois Tigeot 
2255ba55f2f5SFrançois Tigeot 	ring->init = init_ring_common;
2256ba55f2f5SFrançois Tigeot 
2257ba55f2f5SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2258ba55f2f5SFrançois Tigeot }
2259ba55f2f5SFrançois Tigeot 
2260e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
2261e3adcf8fSFrançois Tigeot {
2262ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2263ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[BCS];
2264e3adcf8fSFrançois Tigeot 
2265686a02f1SFrançois Tigeot 	ring->name = "blitter ring";
2266686a02f1SFrançois Tigeot 	ring->id = BCS;
2267686a02f1SFrançois Tigeot 
2268686a02f1SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
2269686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
22705d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
2271686a02f1SFrançois Tigeot 	ring->add_request = gen6_add_request;
2272686a02f1SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2273a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
22749edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
22759edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
22769edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
22779edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
22789edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
22799edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
22809edbd4a0SFrançois Tigeot 	} else {
22815d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2282686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2283686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
2284686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
22859edbd4a0SFrançois Tigeot 	}
2286ba55f2f5SFrançois Tigeot 	ring->semaphore.sync_to = gen6_ring_sync;
2287ba55f2f5SFrançois Tigeot 	ring->semaphore.signal = gen6_signal;
2288ba55f2f5SFrançois Tigeot 	/*
2289ba55f2f5SFrançois Tigeot 	 * The current semaphore is only applied on pre-gen8 platform. And
2290ba55f2f5SFrançois Tigeot 	 * there is no VCS2 ring on the pre-gen8 platform. So the semaphore
2291ba55f2f5SFrançois Tigeot 	 * between BCS and VCS2 is initialized as INVALID.
2292ba55f2f5SFrançois Tigeot 	 * Gen8 will initialize the sema between BCS and VCS2 later.
2293ba55f2f5SFrançois Tigeot 	 */
2294ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2295ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2296ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2297ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2298ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2299ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2300ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2301ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2302ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2303ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
23045d0b1887SFrançois Tigeot 	ring->init = init_ring_common;
23055d0b1887SFrançois Tigeot 
23065d0b1887SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
23075d0b1887SFrançois Tigeot }
23085d0b1887SFrançois Tigeot 
23095d0b1887SFrançois Tigeot int intel_init_vebox_ring_buffer(struct drm_device *dev)
23105d0b1887SFrançois Tigeot {
2311ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2312ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VECS];
23135d0b1887SFrançois Tigeot 
23145d0b1887SFrançois Tigeot 	ring->name = "video enhancement ring";
23155d0b1887SFrançois Tigeot 	ring->id = VECS;
23165d0b1887SFrançois Tigeot 
23175d0b1887SFrançois Tigeot 	ring->mmio_base = VEBOX_RING_BASE;
23185d0b1887SFrançois Tigeot 	ring->write_tail = ring_write_tail;
23195d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
23205d0b1887SFrançois Tigeot 	ring->add_request = gen6_add_request;
23215d0b1887SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
23225d0b1887SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
23239edbd4a0SFrançois Tigeot 
23249edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
23259edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
23269edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
23279edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
23289edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
23299edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
23309edbd4a0SFrançois Tigeot 	} else {
23319edbd4a0SFrançois Tigeot 		ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
23325d0b1887SFrançois Tigeot 		ring->irq_get = hsw_vebox_get_irq;
23335d0b1887SFrançois Tigeot 		ring->irq_put = hsw_vebox_put_irq;
23345d0b1887SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
23359edbd4a0SFrançois Tigeot 	}
2336ba55f2f5SFrançois Tigeot 	ring->semaphore.sync_to = gen6_ring_sync;
2337ba55f2f5SFrançois Tigeot 	ring->semaphore.signal = gen6_signal;
2338ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2339ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2340ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2341ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2342ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2343ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2344ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2345ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2346ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2347ba55f2f5SFrançois Tigeot 	ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2348686a02f1SFrançois Tigeot 	ring->init = init_ring_common;
2349e3adcf8fSFrançois Tigeot 
2350e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2351e3adcf8fSFrançois Tigeot }
2352b030f26bSFrançois Tigeot 
2353b030f26bSFrançois Tigeot int
2354ba55f2f5SFrançois Tigeot intel_ring_flush_all_caches(struct intel_engine_cs *ring)
2355b030f26bSFrançois Tigeot {
2356b030f26bSFrançois Tigeot 	int ret;
2357b030f26bSFrançois Tigeot 
2358b030f26bSFrançois Tigeot 	if (!ring->gpu_caches_dirty)
2359b030f26bSFrançois Tigeot 		return 0;
2360b030f26bSFrançois Tigeot 
2361b030f26bSFrançois Tigeot 	ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2362b030f26bSFrançois Tigeot 	if (ret)
2363b030f26bSFrançois Tigeot 		return ret;
2364b030f26bSFrançois Tigeot 
2365a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2366a2fdbec6SFrançois Tigeot 
2367b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2368b030f26bSFrançois Tigeot 	return 0;
2369b030f26bSFrançois Tigeot }
2370b030f26bSFrançois Tigeot 
2371b030f26bSFrançois Tigeot int
2372ba55f2f5SFrançois Tigeot intel_ring_invalidate_all_caches(struct intel_engine_cs *ring)
2373b030f26bSFrançois Tigeot {
2374b030f26bSFrançois Tigeot 	uint32_t flush_domains;
2375b030f26bSFrançois Tigeot 	int ret;
2376b030f26bSFrançois Tigeot 
2377b030f26bSFrançois Tigeot 	flush_domains = 0;
2378b030f26bSFrançois Tigeot 	if (ring->gpu_caches_dirty)
2379b030f26bSFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
2380b030f26bSFrançois Tigeot 
2381b030f26bSFrançois Tigeot 	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2382b030f26bSFrançois Tigeot 	if (ret)
2383b030f26bSFrançois Tigeot 		return ret;
2384b030f26bSFrançois Tigeot 
2385a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2386a2fdbec6SFrançois Tigeot 
2387b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
2388b030f26bSFrançois Tigeot 	return 0;
2389b030f26bSFrançois Tigeot }
2390ba55f2f5SFrançois Tigeot 
2391ba55f2f5SFrançois Tigeot void
2392ba55f2f5SFrançois Tigeot intel_stop_ring_buffer(struct intel_engine_cs *ring)
2393ba55f2f5SFrançois Tigeot {
2394ba55f2f5SFrançois Tigeot 	int ret;
2395ba55f2f5SFrançois Tigeot 
2396ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
2397ba55f2f5SFrançois Tigeot 		return;
2398ba55f2f5SFrançois Tigeot 
2399ba55f2f5SFrançois Tigeot 	ret = intel_ring_idle(ring);
2400ba55f2f5SFrançois Tigeot 	if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2401ba55f2f5SFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2402ba55f2f5SFrançois Tigeot 			  ring->name, ret);
2403ba55f2f5SFrançois Tigeot 
2404ba55f2f5SFrançois Tigeot 	stop_ring(ring);
2405ba55f2f5SFrançois Tigeot }
2406