xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision aee94f86171368465eaa15d649743f13cea3363a)
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 
30*aee94f86SFrançois Tigeot #include <linux/log2.h>
3118e26a6dSFrançois Tigeot #include <drm/drmP.h>
32e3adcf8fSFrançois Tigeot #include "i915_drv.h"
33a2fdbec6SFrançois Tigeot #include <drm/i915_drm.h>
34a2fdbec6SFrançois Tigeot #include "i915_trace.h"
35e3adcf8fSFrançois Tigeot #include "intel_drv.h"
36e3adcf8fSFrançois Tigeot 
371b13d190SFrançois Tigeot int __intel_ring_space(int head, int tail, int size)
38e3adcf8fSFrançois Tigeot {
392c9916cdSFrançois Tigeot 	int space = head - tail;
402c9916cdSFrançois Tigeot 	if (space <= 0)
41ba55f2f5SFrançois Tigeot 		space += size;
422c9916cdSFrançois Tigeot 	return space - I915_RING_FREE_SPACE;
432c9916cdSFrançois Tigeot }
442c9916cdSFrançois Tigeot 
452c9916cdSFrançois Tigeot void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
462c9916cdSFrançois Tigeot {
472c9916cdSFrançois Tigeot 	if (ringbuf->last_retired_head != -1) {
482c9916cdSFrançois Tigeot 		ringbuf->head = ringbuf->last_retired_head;
492c9916cdSFrançois Tigeot 		ringbuf->last_retired_head = -1;
502c9916cdSFrançois Tigeot 	}
512c9916cdSFrançois Tigeot 
522c9916cdSFrançois Tigeot 	ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
532c9916cdSFrançois Tigeot 					    ringbuf->tail, ringbuf->size);
54e3adcf8fSFrançois Tigeot }
55e3adcf8fSFrançois Tigeot 
561b13d190SFrançois Tigeot int intel_ring_space(struct intel_ringbuffer *ringbuf)
57ba55f2f5SFrançois Tigeot {
582c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
592c9916cdSFrançois Tigeot 	return ringbuf->space;
60ba55f2f5SFrançois Tigeot }
61ba55f2f5SFrançois Tigeot 
621b13d190SFrançois Tigeot bool intel_ring_stopped(struct intel_engine_cs *ring)
639edbd4a0SFrançois Tigeot {
649edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
65ba55f2f5SFrançois Tigeot 	return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
66ba55f2f5SFrançois Tigeot }
679edbd4a0SFrançois Tigeot 
68a05eeebfSFrançois Tigeot static void __intel_ring_advance(struct intel_engine_cs *ring)
69ba55f2f5SFrançois Tigeot {
70ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
71ba55f2f5SFrançois Tigeot 	ringbuf->tail &= ringbuf->size - 1;
72ba55f2f5SFrançois Tigeot 	if (intel_ring_stopped(ring))
739edbd4a0SFrançois Tigeot 		return;
74ba55f2f5SFrançois Tigeot 	ring->write_tail(ring, ringbuf->tail);
759edbd4a0SFrançois Tigeot }
769edbd4a0SFrançois Tigeot 
77e3adcf8fSFrançois Tigeot static int
78a05eeebfSFrançois Tigeot gen2_render_ring_flush(struct drm_i915_gem_request *req,
79686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
80686a02f1SFrançois Tigeot 		       u32	flush_domains)
81686a02f1SFrançois Tigeot {
82a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
83686a02f1SFrançois Tigeot 	u32 cmd;
84686a02f1SFrançois Tigeot 	int ret;
85686a02f1SFrançois Tigeot 
86686a02f1SFrançois Tigeot 	cmd = MI_FLUSH;
87686a02f1SFrançois Tigeot 	if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
88686a02f1SFrançois Tigeot 		cmd |= MI_NO_WRITE_FLUSH;
89686a02f1SFrançois Tigeot 
90686a02f1SFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
91686a02f1SFrançois Tigeot 		cmd |= MI_READ_FLUSH;
92686a02f1SFrançois Tigeot 
93a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
94686a02f1SFrançois Tigeot 	if (ret)
95686a02f1SFrançois Tigeot 		return ret;
96686a02f1SFrançois Tigeot 
97686a02f1SFrançois Tigeot 	intel_ring_emit(ring, cmd);
98686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
99686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
100686a02f1SFrançois Tigeot 
101686a02f1SFrançois Tigeot 	return 0;
102686a02f1SFrançois Tigeot }
103686a02f1SFrançois Tigeot 
104686a02f1SFrançois Tigeot static int
105a05eeebfSFrançois Tigeot gen4_render_ring_flush(struct drm_i915_gem_request *req,
106686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
107686a02f1SFrançois Tigeot 		       u32	flush_domains)
108e3adcf8fSFrançois Tigeot {
109a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
110e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
111686a02f1SFrançois Tigeot 	u32 cmd;
112e3adcf8fSFrançois Tigeot 	int ret;
113e3adcf8fSFrançois Tigeot 
114e3adcf8fSFrançois Tigeot 	/*
115e3adcf8fSFrançois Tigeot 	 * read/write caches:
116e3adcf8fSFrançois Tigeot 	 *
117e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
118e3adcf8fSFrançois Tigeot 	 * only flushed if MI_NO_WRITE_FLUSH is unset.  On 965, it is
119e3adcf8fSFrançois Tigeot 	 * also flushed at 2d versus 3d pipeline switches.
120e3adcf8fSFrançois Tigeot 	 *
121e3adcf8fSFrançois Tigeot 	 * read-only caches:
122e3adcf8fSFrançois Tigeot 	 *
123e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
124e3adcf8fSFrançois Tigeot 	 * MI_READ_FLUSH is set, and is always flushed on 965.
125e3adcf8fSFrançois Tigeot 	 *
126e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_COMMAND may not exist?
127e3adcf8fSFrançois Tigeot 	 *
128e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
129e3adcf8fSFrançois Tigeot 	 * invalidated when MI_EXE_FLUSH is set.
130e3adcf8fSFrançois Tigeot 	 *
131e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
132e3adcf8fSFrançois Tigeot 	 * invalidated with every MI_FLUSH.
133e3adcf8fSFrançois Tigeot 	 *
134e3adcf8fSFrançois Tigeot 	 * TLBs:
135e3adcf8fSFrançois Tigeot 	 *
136e3adcf8fSFrançois Tigeot 	 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
137e3adcf8fSFrançois Tigeot 	 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
138e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
139e3adcf8fSFrançois Tigeot 	 * are flushed at any MI_FLUSH.
140e3adcf8fSFrançois Tigeot 	 */
141e3adcf8fSFrançois Tigeot 
142e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
143686a02f1SFrançois Tigeot 	if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
144e3adcf8fSFrançois Tigeot 		cmd &= ~MI_NO_WRITE_FLUSH;
145e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
146e3adcf8fSFrançois Tigeot 		cmd |= MI_EXE_FLUSH;
147e3adcf8fSFrançois Tigeot 
148e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
149e3adcf8fSFrançois Tigeot 	    (IS_G4X(dev) || IS_GEN5(dev)))
150e3adcf8fSFrançois Tigeot 		cmd |= MI_INVALIDATE_ISP;
151e3adcf8fSFrançois Tigeot 
152a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
153e3adcf8fSFrançois Tigeot 	if (ret)
154e3adcf8fSFrançois Tigeot 		return ret;
155e3adcf8fSFrançois Tigeot 
156e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
157e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
158e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
159e3adcf8fSFrançois Tigeot 
160e3adcf8fSFrançois Tigeot 	return 0;
161e3adcf8fSFrançois Tigeot }
162e3adcf8fSFrançois Tigeot 
163e3adcf8fSFrançois Tigeot /**
164e3adcf8fSFrançois Tigeot  * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
165e3adcf8fSFrançois Tigeot  * implementing two workarounds on gen6.  From section 1.4.7.1
166e3adcf8fSFrançois Tigeot  * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
167e3adcf8fSFrançois Tigeot  *
168e3adcf8fSFrançois Tigeot  * [DevSNB-C+{W/A}] Before any depth stall flush (including those
169e3adcf8fSFrançois Tigeot  * produced by non-pipelined state commands), software needs to first
170e3adcf8fSFrançois Tigeot  * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
171e3adcf8fSFrançois Tigeot  * 0.
172e3adcf8fSFrançois Tigeot  *
173e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
174e3adcf8fSFrançois Tigeot  * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
175e3adcf8fSFrançois Tigeot  *
176e3adcf8fSFrançois Tigeot  * And the workaround for these two requires this workaround first:
177e3adcf8fSFrançois Tigeot  *
178e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
179e3adcf8fSFrançois Tigeot  * BEFORE the pipe-control with a post-sync op and no write-cache
180e3adcf8fSFrançois Tigeot  * flushes.
181e3adcf8fSFrançois Tigeot  *
182e3adcf8fSFrançois Tigeot  * And this last workaround is tricky because of the requirements on
183e3adcf8fSFrançois Tigeot  * that bit.  From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
184e3adcf8fSFrançois Tigeot  * volume 2 part 1:
185e3adcf8fSFrançois Tigeot  *
186e3adcf8fSFrançois Tigeot  *     "1 of the following must also be set:
187e3adcf8fSFrançois Tigeot  *      - Render Target Cache Flush Enable ([12] of DW1)
188e3adcf8fSFrançois Tigeot  *      - Depth Cache Flush Enable ([0] of DW1)
189e3adcf8fSFrançois Tigeot  *      - Stall at Pixel Scoreboard ([1] of DW1)
190e3adcf8fSFrançois Tigeot  *      - Depth Stall ([13] of DW1)
191e3adcf8fSFrançois Tigeot  *      - Post-Sync Operation ([13] of DW1)
192e3adcf8fSFrançois Tigeot  *      - Notify Enable ([8] of DW1)"
193e3adcf8fSFrançois Tigeot  *
194e3adcf8fSFrançois Tigeot  * The cache flushes require the workaround flush that triggered this
195e3adcf8fSFrançois Tigeot  * one, so we can't use it.  Depth stall would trigger the same.
196e3adcf8fSFrançois Tigeot  * Post-sync nonzero is what triggered this second workaround, so we
197e3adcf8fSFrançois Tigeot  * can't use that one either.  Notify enable is IRQs, which aren't
198e3adcf8fSFrançois Tigeot  * really our business.  That leaves only stall at scoreboard.
199e3adcf8fSFrançois Tigeot  */
200e3adcf8fSFrançois Tigeot static int
201a05eeebfSFrançois Tigeot intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
202e3adcf8fSFrançois Tigeot {
203a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
204ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
205e3adcf8fSFrançois Tigeot 	int ret;
206e3adcf8fSFrançois Tigeot 
207a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 6);
208e3adcf8fSFrançois Tigeot 	if (ret)
209e3adcf8fSFrançois Tigeot 		return ret;
210e3adcf8fSFrançois Tigeot 
211e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
212e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
213e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_STALL_AT_SCOREBOARD);
214e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
215e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* low dword */
216e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* high dword */
217e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
218e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
219e3adcf8fSFrançois Tigeot 
220a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 6);
221e3adcf8fSFrançois Tigeot 	if (ret)
222e3adcf8fSFrançois Tigeot 		return ret;
223e3adcf8fSFrançois Tigeot 
224e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
225e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
226e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
227e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
228e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
229e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
230e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
231e3adcf8fSFrançois Tigeot 
232e3adcf8fSFrançois Tigeot 	return 0;
233e3adcf8fSFrançois Tigeot }
234e3adcf8fSFrançois Tigeot 
235e3adcf8fSFrançois Tigeot static int
236a05eeebfSFrançois Tigeot gen6_render_ring_flush(struct drm_i915_gem_request *req,
237e3adcf8fSFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
238e3adcf8fSFrançois Tigeot {
239a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
240e3adcf8fSFrançois Tigeot 	u32 flags = 0;
241ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
242e3adcf8fSFrançois Tigeot 	int ret;
243e3adcf8fSFrançois Tigeot 
244e3adcf8fSFrançois Tigeot 	/* Force SNB workarounds for PIPE_CONTROL flushes */
245a05eeebfSFrançois Tigeot 	ret = intel_emit_post_sync_nonzero_flush(req);
246686a02f1SFrançois Tigeot 	if (ret)
247686a02f1SFrançois Tigeot 		return ret;
248e3adcf8fSFrançois Tigeot 
249e3adcf8fSFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
250e3adcf8fSFrançois Tigeot 	 * number of bits based on the write domains has little performance
251e3adcf8fSFrançois Tigeot 	 * impact.
252e3adcf8fSFrançois Tigeot 	 */
253b5c29a34SFrançois Tigeot 	if (flush_domains) {
254e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
255b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
256b5c29a34SFrançois Tigeot 		/*
257b5c29a34SFrançois Tigeot 		 * Ensure that any following seqno writes only happen
258b5c29a34SFrançois Tigeot 		 * when the render cache is indeed flushed.
259b5c29a34SFrançois Tigeot 		 */
260b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CS_STALL;
261b5c29a34SFrançois Tigeot 	}
262b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
263686a02f1SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
264e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
265e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
266e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
267e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
268e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
269686a02f1SFrançois Tigeot 		/*
270b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
271686a02f1SFrançois Tigeot 		 */
272b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
273b5c29a34SFrançois Tigeot 	}
274e3adcf8fSFrançois Tigeot 
275a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
276e3adcf8fSFrançois Tigeot 	if (ret)
277e3adcf8fSFrançois Tigeot 		return ret;
278e3adcf8fSFrançois Tigeot 
279b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
280e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, flags);
281e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
282b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
283b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
284b5c29a34SFrançois Tigeot 
285b5c29a34SFrançois Tigeot 	return 0;
286b5c29a34SFrançois Tigeot }
287b5c29a34SFrançois Tigeot 
288b5c29a34SFrançois Tigeot static int
289a05eeebfSFrançois Tigeot gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
290b5c29a34SFrançois Tigeot {
291a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
292b5c29a34SFrançois Tigeot 	int ret;
293b5c29a34SFrançois Tigeot 
294a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
295b5c29a34SFrançois Tigeot 	if (ret)
296b5c29a34SFrançois Tigeot 		return ret;
297b5c29a34SFrançois Tigeot 
298b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
299b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
300b5c29a34SFrançois Tigeot 			      PIPE_CONTROL_STALL_AT_SCOREBOARD);
301b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
302b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
303b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
304b5c29a34SFrançois Tigeot 
305b5c29a34SFrançois Tigeot 	return 0;
306b5c29a34SFrançois Tigeot }
307b5c29a34SFrançois Tigeot 
308b5c29a34SFrançois Tigeot static int
309a05eeebfSFrançois Tigeot gen7_render_ring_flush(struct drm_i915_gem_request *req,
310b5c29a34SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
311b5c29a34SFrançois Tigeot {
312a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
313b5c29a34SFrançois Tigeot 	u32 flags = 0;
314ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
315b5c29a34SFrançois Tigeot 	int ret;
316b5c29a34SFrançois Tigeot 
317b5c29a34SFrançois Tigeot 	/*
318b5c29a34SFrançois Tigeot 	 * Ensure that any following seqno writes only happen when the render
319b5c29a34SFrançois Tigeot 	 * cache is indeed flushed.
320b5c29a34SFrançois Tigeot 	 *
321b5c29a34SFrançois Tigeot 	 * Workaround: 4th PIPE_CONTROL command (except the ones with only
322b5c29a34SFrançois Tigeot 	 * read-cache invalidate bits set) must have the CS_STALL bit set. We
323b5c29a34SFrançois Tigeot 	 * don't try to be clever and just set it unconditionally.
324b5c29a34SFrançois Tigeot 	 */
325b5c29a34SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
326b5c29a34SFrançois Tigeot 
327b5c29a34SFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
328b5c29a34SFrançois Tigeot 	 * number of bits based on the write domains has little performance
329b5c29a34SFrançois Tigeot 	 * impact.
330b5c29a34SFrançois Tigeot 	 */
331b5c29a34SFrançois Tigeot 	if (flush_domains) {
332b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
333b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
334*aee94f86SFrançois Tigeot 		flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
335b49c8cf9SFrançois Tigeot 		flags |= PIPE_CONTROL_FLUSH_ENABLE;
336b5c29a34SFrançois Tigeot 	}
337b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
338b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
339b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
340b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
341b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
342b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
343b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
3442c9916cdSFrançois Tigeot 		flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
345b5c29a34SFrançois Tigeot 		/*
346b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
347b5c29a34SFrançois Tigeot 		 */
348b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
349a2fdbec6SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
350b5c29a34SFrançois Tigeot 
3510dbf0ea8SMatthew Dillon 		flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3520dbf0ea8SMatthew Dillon 
353b5c29a34SFrançois Tigeot 		/* Workaround: we must issue a pipe_control with CS-stall bit
354b5c29a34SFrançois Tigeot 		 * set before a pipe_control command that has the state cache
355b5c29a34SFrançois Tigeot 		 * invalidate bit set. */
356a05eeebfSFrançois Tigeot 		gen7_render_ring_cs_stall_wa(req);
357b5c29a34SFrançois Tigeot 	}
358b5c29a34SFrançois Tigeot 
359a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
360b5c29a34SFrançois Tigeot 	if (ret)
361b5c29a34SFrançois Tigeot 		return ret;
362b5c29a34SFrançois Tigeot 
363b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
364b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, flags);
365a2fdbec6SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
366b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
367e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
368e3adcf8fSFrançois Tigeot 
369e3adcf8fSFrançois Tigeot 	return 0;
370e3adcf8fSFrançois Tigeot }
371e3adcf8fSFrançois Tigeot 
3729edbd4a0SFrançois Tigeot static int
373a05eeebfSFrançois Tigeot gen8_emit_pipe_control(struct drm_i915_gem_request *req,
37424edb884SFrançois Tigeot 		       u32 flags, u32 scratch_addr)
37524edb884SFrançois Tigeot {
376a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
37724edb884SFrançois Tigeot 	int ret;
37824edb884SFrançois Tigeot 
379a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 6);
38024edb884SFrançois Tigeot 	if (ret)
38124edb884SFrançois Tigeot 		return ret;
38224edb884SFrançois Tigeot 
38324edb884SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
38424edb884SFrançois Tigeot 	intel_ring_emit(ring, flags);
38524edb884SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
38624edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
38724edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
38824edb884SFrançois Tigeot 	intel_ring_emit(ring, 0);
38924edb884SFrançois Tigeot 	intel_ring_advance(ring);
39024edb884SFrançois Tigeot 
39124edb884SFrançois Tigeot 	return 0;
39224edb884SFrançois Tigeot }
39324edb884SFrançois Tigeot 
39424edb884SFrançois Tigeot static int
395a05eeebfSFrançois Tigeot gen8_render_ring_flush(struct drm_i915_gem_request *req,
3969edbd4a0SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
3979edbd4a0SFrançois Tigeot {
3989edbd4a0SFrançois Tigeot 	u32 flags = 0;
399a05eeebfSFrançois Tigeot 	u32 scratch_addr = req->ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
4009edbd4a0SFrançois Tigeot 	int ret;
4019edbd4a0SFrançois Tigeot 
4029edbd4a0SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
4039edbd4a0SFrançois Tigeot 
4049edbd4a0SFrançois Tigeot 	if (flush_domains) {
4059edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
4069edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
407*aee94f86SFrançois Tigeot 		flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
408b49c8cf9SFrançois Tigeot 		flags |= PIPE_CONTROL_FLUSH_ENABLE;
4099edbd4a0SFrançois Tigeot 	}
4109edbd4a0SFrançois Tigeot 	if (invalidate_domains) {
4119edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
4129edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
4139edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
4149edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
4159edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
4169edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
4179edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
4189edbd4a0SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4199edbd4a0SFrançois Tigeot 
42024edb884SFrançois Tigeot 		/* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
421a05eeebfSFrançois Tigeot 		ret = gen8_emit_pipe_control(req,
42224edb884SFrançois Tigeot 					     PIPE_CONTROL_CS_STALL |
42324edb884SFrançois Tigeot 					     PIPE_CONTROL_STALL_AT_SCOREBOARD,
42424edb884SFrançois Tigeot 					     0);
4259edbd4a0SFrançois Tigeot 		if (ret)
4269edbd4a0SFrançois Tigeot 			return ret;
42724edb884SFrançois Tigeot 	}
4289edbd4a0SFrançois Tigeot 
429a05eeebfSFrançois Tigeot 	return gen8_emit_pipe_control(req, flags, scratch_addr);
4309edbd4a0SFrançois Tigeot }
4319edbd4a0SFrançois Tigeot 
432ba55f2f5SFrançois Tigeot static void ring_write_tail(struct intel_engine_cs *ring,
433b5c29a34SFrançois Tigeot 			    u32 value)
434e3adcf8fSFrançois Tigeot {
435ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
436e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
437e3adcf8fSFrançois Tigeot }
438e3adcf8fSFrançois Tigeot 
439ba55f2f5SFrançois Tigeot u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
440e3adcf8fSFrançois Tigeot {
441ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
442ba55f2f5SFrançois Tigeot 	u64 acthd;
443e3adcf8fSFrançois Tigeot 
444ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
445ba55f2f5SFrançois Tigeot 		acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
446ba55f2f5SFrançois Tigeot 					 RING_ACTHD_UDW(ring->mmio_base));
447ba55f2f5SFrançois Tigeot 	else if (INTEL_INFO(ring->dev)->gen >= 4)
448ba55f2f5SFrançois Tigeot 		acthd = I915_READ(RING_ACTHD(ring->mmio_base));
449ba55f2f5SFrançois Tigeot 	else
450ba55f2f5SFrançois Tigeot 		acthd = I915_READ(ACTHD);
451ba55f2f5SFrançois Tigeot 
452ba55f2f5SFrançois Tigeot 	return acthd;
453e3adcf8fSFrançois Tigeot }
454e3adcf8fSFrançois Tigeot 
455ba55f2f5SFrançois Tigeot static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
4565d0b1887SFrançois Tigeot {
4575d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
4585d0b1887SFrançois Tigeot 	u32 addr;
4595d0b1887SFrançois Tigeot 
4605d0b1887SFrançois Tigeot 	addr = dev_priv->status_page_dmah->busaddr;
4615d0b1887SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 4)
4625d0b1887SFrançois Tigeot 		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
4635d0b1887SFrançois Tigeot 	I915_WRITE(HWS_PGA, addr);
4645d0b1887SFrançois Tigeot }
4655d0b1887SFrançois Tigeot 
466477eb7f9SFrançois Tigeot static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
467477eb7f9SFrançois Tigeot {
468477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
469477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
470*aee94f86SFrançois Tigeot 	i915_reg_t mmio;
471477eb7f9SFrançois Tigeot 
472477eb7f9SFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
473477eb7f9SFrançois Tigeot 	 * the ring registers as of gen7.
474477eb7f9SFrançois Tigeot 	 */
475477eb7f9SFrançois Tigeot 	if (IS_GEN7(dev)) {
476477eb7f9SFrançois Tigeot 		switch (ring->id) {
477477eb7f9SFrançois Tigeot 		case RCS:
478477eb7f9SFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
479477eb7f9SFrançois Tigeot 			break;
480477eb7f9SFrançois Tigeot 		case BCS:
481477eb7f9SFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
482477eb7f9SFrançois Tigeot 			break;
483477eb7f9SFrançois Tigeot 		/*
484477eb7f9SFrançois Tigeot 		 * VCS2 actually doesn't exist on Gen7. Only shut up
485477eb7f9SFrançois Tigeot 		 * gcc switch check warning
486477eb7f9SFrançois Tigeot 		 */
487477eb7f9SFrançois Tigeot 		case VCS2:
488477eb7f9SFrançois Tigeot 		case VCS:
489477eb7f9SFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
490477eb7f9SFrançois Tigeot 			break;
491477eb7f9SFrançois Tigeot 		case VECS:
492477eb7f9SFrançois Tigeot 			mmio = VEBOX_HWS_PGA_GEN7;
493477eb7f9SFrançois Tigeot 			break;
494477eb7f9SFrançois Tigeot 		}
495477eb7f9SFrançois Tigeot 	} else if (IS_GEN6(ring->dev)) {
496477eb7f9SFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
497477eb7f9SFrançois Tigeot 	} else {
498477eb7f9SFrançois Tigeot 		/* XXX: gen8 returns to sanity */
499477eb7f9SFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
500477eb7f9SFrançois Tigeot 	}
501477eb7f9SFrançois Tigeot 
502477eb7f9SFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
503477eb7f9SFrançois Tigeot 	POSTING_READ(mmio);
504477eb7f9SFrançois Tigeot 
505477eb7f9SFrançois Tigeot 	/*
506477eb7f9SFrançois Tigeot 	 * Flush the TLB for this page
507477eb7f9SFrançois Tigeot 	 *
508477eb7f9SFrançois Tigeot 	 * FIXME: These two bits have disappeared on gen8, so a question
509477eb7f9SFrançois Tigeot 	 * arises: do we still need this and if so how should we go about
510477eb7f9SFrançois Tigeot 	 * invalidating the TLB?
511477eb7f9SFrançois Tigeot 	 */
512477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
513*aee94f86SFrançois Tigeot 		i915_reg_t reg = RING_INSTPM(ring->mmio_base);
514477eb7f9SFrançois Tigeot 
515477eb7f9SFrançois Tigeot 		/* ring should be idle before issuing a sync flush*/
516477eb7f9SFrançois Tigeot 		WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
517477eb7f9SFrançois Tigeot 
518477eb7f9SFrançois Tigeot 		I915_WRITE(reg,
519477eb7f9SFrançois Tigeot 			   _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
520477eb7f9SFrançois Tigeot 					      INSTPM_SYNC_FLUSH));
521477eb7f9SFrançois Tigeot 		if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
522477eb7f9SFrançois Tigeot 			     1000))
523477eb7f9SFrançois Tigeot 			DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
524477eb7f9SFrançois Tigeot 				  ring->name);
525477eb7f9SFrançois Tigeot 	}
526477eb7f9SFrançois Tigeot }
527477eb7f9SFrançois Tigeot 
528ba55f2f5SFrançois Tigeot static bool stop_ring(struct intel_engine_cs *ring)
529e3adcf8fSFrançois Tigeot {
530ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ring->dev);
531e3adcf8fSFrançois Tigeot 
532ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
533ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
5341b13d190SFrançois Tigeot 		if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
535ba55f2f5SFrançois Tigeot 			DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
5361b13d190SFrançois Tigeot 			/* Sometimes we observe that the idle flag is not
5371b13d190SFrançois Tigeot 			 * set even though the ring is empty. So double
5381b13d190SFrançois Tigeot 			 * check before giving up.
5391b13d190SFrançois Tigeot 			 */
5401b13d190SFrançois Tigeot 			if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
541ba55f2f5SFrançois Tigeot 				return false;
542ba55f2f5SFrançois Tigeot 		}
543ba55f2f5SFrançois Tigeot 	}
544686a02f1SFrançois Tigeot 
545e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
546e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
547e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
548e3adcf8fSFrançois Tigeot 
549ba55f2f5SFrançois Tigeot 	if (!IS_GEN2(ring->dev)) {
550ba55f2f5SFrançois Tigeot 		(void)I915_READ_CTL(ring);
551ba55f2f5SFrançois Tigeot 		I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
552ba55f2f5SFrançois Tigeot 	}
553e3adcf8fSFrançois Tigeot 
554ba55f2f5SFrançois Tigeot 	return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
555ba55f2f5SFrançois Tigeot }
556ba55f2f5SFrançois Tigeot 
557ba55f2f5SFrançois Tigeot static int init_ring_common(struct intel_engine_cs *ring)
558ba55f2f5SFrançois Tigeot {
559ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ring->dev;
560ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
561ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
562ba55f2f5SFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
563ba55f2f5SFrançois Tigeot 	int ret = 0;
564ba55f2f5SFrançois Tigeot 
5652c9916cdSFrançois Tigeot 	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
566ba55f2f5SFrançois Tigeot 
567ba55f2f5SFrançois Tigeot 	if (!stop_ring(ring)) {
568ba55f2f5SFrançois Tigeot 		/* G45 ring initialization often fails to reset head to zero */
569b5c29a34SFrançois Tigeot 		DRM_DEBUG_KMS("%s head not reset to zero "
570e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
571e3adcf8fSFrançois Tigeot 			      ring->name,
572e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
573e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
574e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
575e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
576e3adcf8fSFrançois Tigeot 
577ba55f2f5SFrançois Tigeot 		if (!stop_ring(ring)) {
578e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
579e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
580e3adcf8fSFrançois Tigeot 				  ring->name,
581e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
582e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
583e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
584e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
585686a02f1SFrançois Tigeot 			ret = -EIO;
586686a02f1SFrançois Tigeot 			goto out;
587e3adcf8fSFrançois Tigeot 		}
588ba55f2f5SFrançois Tigeot 	}
589ba55f2f5SFrançois Tigeot 
590ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev))
591ba55f2f5SFrançois Tigeot 		intel_ring_setup_status_page(ring);
592ba55f2f5SFrançois Tigeot 	else
593ba55f2f5SFrançois Tigeot 		ring_setup_phys_status_page(ring);
594ba55f2f5SFrançois Tigeot 
5950f370975SMatthew Dillon 	/* Enforce ordering by reading HEAD register back */
5960f370975SMatthew Dillon 	I915_READ_HEAD(ring);
5970f370975SMatthew Dillon 
598ba55f2f5SFrançois Tigeot 	/* Initialize the ring. This must happen _after_ we've cleared the ring
599ba55f2f5SFrançois Tigeot 	 * registers with the above sequence (the readback of the HEAD registers
600ba55f2f5SFrançois Tigeot 	 * also enforces ordering), otherwise the hw might lose the new ring
601ba55f2f5SFrançois Tigeot 	 * register values. */
602ba55f2f5SFrançois Tigeot 	I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
6031b13d190SFrançois Tigeot 
6041b13d190SFrançois Tigeot 	/* WaClearRingBufHeadRegAtInit:ctg,elk */
6051b13d190SFrançois Tigeot 	if (I915_READ_HEAD(ring))
6061b13d190SFrançois Tigeot 		DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
6071b13d190SFrançois Tigeot 			  ring->name, I915_READ_HEAD(ring));
6081b13d190SFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
6091b13d190SFrançois Tigeot 	(void)I915_READ_HEAD(ring);
6101b13d190SFrançois Tigeot 
611ba55f2f5SFrançois Tigeot 	I915_WRITE_CTL(ring,
612ba55f2f5SFrançois Tigeot 			((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
613ba55f2f5SFrançois Tigeot 			| RING_VALID);
614ba55f2f5SFrançois Tigeot 
615ba55f2f5SFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
616ba55f2f5SFrançois Tigeot 	if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
617ba55f2f5SFrançois Tigeot 		     I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
618ba55f2f5SFrançois Tigeot 		     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
619ba55f2f5SFrançois Tigeot 		DRM_ERROR("%s initialization failed "
620ba55f2f5SFrançois Tigeot 			  "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
621ba55f2f5SFrançois Tigeot 			  ring->name,
622ba55f2f5SFrançois Tigeot 			  I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
623ba55f2f5SFrançois Tigeot 			  I915_READ_HEAD(ring), I915_READ_TAIL(ring),
624ba55f2f5SFrançois Tigeot 			  I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
625ba55f2f5SFrançois Tigeot 		ret = -EIO;
626ba55f2f5SFrançois Tigeot 		goto out;
627ba55f2f5SFrançois Tigeot 	}
628e3adcf8fSFrançois Tigeot 
6292c9916cdSFrançois Tigeot 	ringbuf->last_retired_head = -1;
630ba55f2f5SFrançois Tigeot 	ringbuf->head = I915_READ_HEAD(ring);
631ba55f2f5SFrançois Tigeot 	ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
6322c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
633e3adcf8fSFrançois Tigeot 
6345d0b1887SFrançois Tigeot 	memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
6355d0b1887SFrançois Tigeot 
636686a02f1SFrançois Tigeot out:
6372c9916cdSFrançois Tigeot 	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
638686a02f1SFrançois Tigeot 
639686a02f1SFrançois Tigeot 	return ret;
640e3adcf8fSFrançois Tigeot }
641e3adcf8fSFrançois Tigeot 
6421b13d190SFrançois Tigeot void
6431b13d190SFrançois Tigeot intel_fini_pipe_control(struct intel_engine_cs *ring)
6441b13d190SFrançois Tigeot {
6451b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
6461b13d190SFrançois Tigeot 
6471b13d190SFrançois Tigeot 	if (ring->scratch.obj == NULL)
6481b13d190SFrançois Tigeot 		return;
6491b13d190SFrançois Tigeot 
6501b13d190SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
6517ec9f8e5SFrançois Tigeot 		kunmap(sg_page(ring->scratch.obj->pages->sgl));
6521b13d190SFrançois Tigeot 		i915_gem_object_ggtt_unpin(ring->scratch.obj);
6531b13d190SFrançois Tigeot 	}
6541b13d190SFrançois Tigeot 
6551b13d190SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
6561b13d190SFrançois Tigeot 	ring->scratch.obj = NULL;
6571b13d190SFrançois Tigeot }
6581b13d190SFrançois Tigeot 
6591b13d190SFrançois Tigeot int
6601b13d190SFrançois Tigeot intel_init_pipe_control(struct intel_engine_cs *ring)
661e3adcf8fSFrançois Tigeot {
662e3adcf8fSFrançois Tigeot 	int ret;
663e3adcf8fSFrançois Tigeot 
6642c9916cdSFrançois Tigeot 	WARN_ON(ring->scratch.obj);
665e3adcf8fSFrançois Tigeot 
6669edbd4a0SFrançois Tigeot 	ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
6679edbd4a0SFrançois Tigeot 	if (ring->scratch.obj == NULL) {
668e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
669e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
670e3adcf8fSFrançois Tigeot 		goto err;
671e3adcf8fSFrançois Tigeot 	}
672e3adcf8fSFrançois Tigeot 
673ba55f2f5SFrançois Tigeot 	ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
674ba55f2f5SFrançois Tigeot 	if (ret)
675ba55f2f5SFrançois Tigeot 		goto err_unref;
676e3adcf8fSFrançois Tigeot 
677ba55f2f5SFrançois Tigeot 	ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
678e3adcf8fSFrançois Tigeot 	if (ret)
679e3adcf8fSFrançois Tigeot 		goto err_unref;
680e3adcf8fSFrançois Tigeot 
6819edbd4a0SFrançois Tigeot 	ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
6827ec9f8e5SFrançois Tigeot 	ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
6839edbd4a0SFrançois Tigeot 	if (ring->scratch.cpu_page == NULL) {
6845d0b1887SFrançois Tigeot 		ret = -ENOMEM;
685e3adcf8fSFrançois Tigeot 		goto err_unpin;
6865d0b1887SFrançois Tigeot 	}
687a2fdbec6SFrançois Tigeot 
688a2fdbec6SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
6899edbd4a0SFrançois Tigeot 			 ring->name, ring->scratch.gtt_offset);
690e3adcf8fSFrançois Tigeot 	return 0;
691e3adcf8fSFrançois Tigeot 
692e3adcf8fSFrançois Tigeot err_unpin:
693ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(ring->scratch.obj);
694e3adcf8fSFrançois Tigeot err_unref:
6959edbd4a0SFrançois Tigeot 	drm_gem_object_unreference(&ring->scratch.obj->base);
696e3adcf8fSFrançois Tigeot err:
697e3adcf8fSFrançois Tigeot 	return ret;
698e3adcf8fSFrançois Tigeot }
699e3adcf8fSFrançois Tigeot 
700a05eeebfSFrançois Tigeot static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
7011b13d190SFrançois Tigeot {
7022c9916cdSFrançois Tigeot 	int ret, i;
703a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
7041b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
7051b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
7062c9916cdSFrançois Tigeot 	struct i915_workarounds *w = &dev_priv->workarounds;
7071b13d190SFrançois Tigeot 
708352ff8bdSFrançois Tigeot 	if (w->count == 0)
7092c9916cdSFrançois Tigeot 		return 0;
7101b13d190SFrançois Tigeot 
7112c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
712a05eeebfSFrançois Tigeot 	ret = intel_ring_flush_all_caches(req);
7131b13d190SFrançois Tigeot 	if (ret)
7141b13d190SFrançois Tigeot 		return ret;
7151b13d190SFrançois Tigeot 
716a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, (w->count * 2 + 2));
7172c9916cdSFrançois Tigeot 	if (ret)
7182c9916cdSFrançois Tigeot 		return ret;
7192c9916cdSFrançois Tigeot 
7202c9916cdSFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
7212c9916cdSFrançois Tigeot 	for (i = 0; i < w->count; i++) {
722*aee94f86SFrançois Tigeot 		intel_ring_emit_reg(ring, w->reg[i].addr);
7232c9916cdSFrançois Tigeot 		intel_ring_emit(ring, w->reg[i].value);
7242c9916cdSFrançois Tigeot 	}
7252c9916cdSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
7262c9916cdSFrançois Tigeot 
7272c9916cdSFrançois Tigeot 	intel_ring_advance(ring);
7282c9916cdSFrançois Tigeot 
7292c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
730a05eeebfSFrançois Tigeot 	ret = intel_ring_flush_all_caches(req);
7312c9916cdSFrançois Tigeot 	if (ret)
7322c9916cdSFrançois Tigeot 		return ret;
7332c9916cdSFrançois Tigeot 
7342c9916cdSFrançois Tigeot 	DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
7352c9916cdSFrançois Tigeot 
7362c9916cdSFrançois Tigeot 	return 0;
7372c9916cdSFrançois Tigeot }
7382c9916cdSFrançois Tigeot 
739a05eeebfSFrançois Tigeot static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
7402c9916cdSFrançois Tigeot {
7412c9916cdSFrançois Tigeot 	int ret;
7422c9916cdSFrançois Tigeot 
743a05eeebfSFrançois Tigeot 	ret = intel_ring_workarounds_emit(req);
7442c9916cdSFrançois Tigeot 	if (ret != 0)
7452c9916cdSFrançois Tigeot 		return ret;
7462c9916cdSFrançois Tigeot 
747a05eeebfSFrançois Tigeot 	ret = i915_gem_render_state_init(req);
7482c9916cdSFrançois Tigeot 	if (ret)
7492c9916cdSFrançois Tigeot 		DRM_ERROR("init render state: %d\n", ret);
7502c9916cdSFrançois Tigeot 
7512c9916cdSFrançois Tigeot 	return ret;
7522c9916cdSFrançois Tigeot }
7532c9916cdSFrançois Tigeot 
7542c9916cdSFrançois Tigeot static int wa_add(struct drm_i915_private *dev_priv,
755*aee94f86SFrançois Tigeot 		  i915_reg_t addr,
756*aee94f86SFrançois Tigeot 		  const u32 mask, const u32 val)
7572c9916cdSFrançois Tigeot {
7582c9916cdSFrançois Tigeot 	const u32 idx = dev_priv->workarounds.count;
7592c9916cdSFrançois Tigeot 
7602c9916cdSFrançois Tigeot 	if (WARN_ON(idx >= I915_MAX_WA_REGS))
7612c9916cdSFrançois Tigeot 		return -ENOSPC;
7622c9916cdSFrançois Tigeot 
7632c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].addr = addr;
7642c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].value = val;
7652c9916cdSFrançois Tigeot 	dev_priv->workarounds.reg[idx].mask = mask;
7662c9916cdSFrançois Tigeot 
7672c9916cdSFrançois Tigeot 	dev_priv->workarounds.count++;
7682c9916cdSFrançois Tigeot 
7692c9916cdSFrançois Tigeot 	return 0;
7702c9916cdSFrançois Tigeot }
7712c9916cdSFrançois Tigeot 
772a05eeebfSFrançois Tigeot #define WA_REG(addr, mask, val) do { \
7732c9916cdSFrançois Tigeot 		const int r = wa_add(dev_priv, (addr), (mask), (val)); \
7742c9916cdSFrançois Tigeot 		if (r) \
7752c9916cdSFrançois Tigeot 			return r; \
776a05eeebfSFrançois Tigeot 	} while (0)
7772c9916cdSFrançois Tigeot 
7782c9916cdSFrançois Tigeot #define WA_SET_BIT_MASKED(addr, mask) \
7792c9916cdSFrançois Tigeot 	WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
7802c9916cdSFrançois Tigeot 
7812c9916cdSFrançois Tigeot #define WA_CLR_BIT_MASKED(addr, mask) \
7822c9916cdSFrançois Tigeot 	WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
7832c9916cdSFrançois Tigeot 
7842c9916cdSFrançois Tigeot #define WA_SET_FIELD_MASKED(addr, mask, value) \
7852c9916cdSFrançois Tigeot 	WA_REG(addr, mask, _MASKED_FIELD(mask, value))
7862c9916cdSFrançois Tigeot 
7872c9916cdSFrançois Tigeot #define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
7882c9916cdSFrançois Tigeot #define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
7892c9916cdSFrançois Tigeot 
7902c9916cdSFrançois Tigeot #define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
7912c9916cdSFrançois Tigeot 
792352ff8bdSFrançois Tigeot static int gen8_init_workarounds(struct intel_engine_cs *ring)
7932c9916cdSFrançois Tigeot {
7942c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
7952c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
7962c9916cdSFrançois Tigeot 
797a05eeebfSFrançois Tigeot 	WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
798a05eeebfSFrançois Tigeot 
799352ff8bdSFrançois Tigeot 	/* WaDisableAsyncFlipPerfMode:bdw,chv */
800a05eeebfSFrançois Tigeot 	WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
801a05eeebfSFrançois Tigeot 
802352ff8bdSFrançois Tigeot 	/* WaDisablePartialInstShootdown:bdw,chv */
8032c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
804352ff8bdSFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
8051b13d190SFrançois Tigeot 
8061b13d190SFrançois Tigeot 	/* Use Force Non-Coherent whenever executing a 3D context. This is a
8071b13d190SFrançois Tigeot 	 * workaround for for a possible hang in the unlikely event a TLB
8081b13d190SFrançois Tigeot 	 * invalidation occurs during a PSD flush.
8091b13d190SFrançois Tigeot 	 */
810352ff8bdSFrançois Tigeot 	/* WaForceEnableNonCoherent:bdw,chv */
811352ff8bdSFrançois Tigeot 	/* WaHdcDisableFetchWhenMasked:bdw,chv */
8122c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0,
8132c9916cdSFrançois Tigeot 			  HDC_DONOT_FETCH_MEM_WHEN_MASKED |
814352ff8bdSFrançois Tigeot 			  HDC_FORCE_NON_COHERENT);
8152c9916cdSFrançois Tigeot 
8162c9916cdSFrançois Tigeot 	/* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
8172c9916cdSFrançois Tigeot 	 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
8182c9916cdSFrançois Tigeot 	 *  polygons in the same 8x4 pixel/sample area to be processed without
8192c9916cdSFrançois Tigeot 	 *  stalling waiting for the earlier ones to write to Hierarchical Z
8202c9916cdSFrançois Tigeot 	 *  buffer."
8212c9916cdSFrançois Tigeot 	 *
822352ff8bdSFrançois Tigeot 	 * This optimization is off by default for BDW and CHV; turn it on.
8232c9916cdSFrançois Tigeot 	 */
8242c9916cdSFrançois Tigeot 	WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
8251b13d190SFrançois Tigeot 
826352ff8bdSFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:bdw,chv */
827352ff8bdSFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
8281b13d190SFrançois Tigeot 
8291b13d190SFrançois Tigeot 	/*
8301b13d190SFrançois Tigeot 	 * BSpec recommends 8x4 when MSAA is used,
8311b13d190SFrançois Tigeot 	 * however in practice 16x4 seems fastest.
8321b13d190SFrançois Tigeot 	 *
8331b13d190SFrançois Tigeot 	 * Note that PS/WM thread counts depend on the WIZ hashing
8341b13d190SFrançois Tigeot 	 * disable bit, which we don't touch here, but it's good
8351b13d190SFrançois Tigeot 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8361b13d190SFrançois Tigeot 	 */
8372c9916cdSFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
8382c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_MASK,
8392c9916cdSFrançois Tigeot 			    GEN6_WIZ_HASHING_16x4);
8401b13d190SFrançois Tigeot 
8411b13d190SFrançois Tigeot 	return 0;
8421b13d190SFrançois Tigeot }
8431b13d190SFrançois Tigeot 
844352ff8bdSFrançois Tigeot static int bdw_init_workarounds(struct intel_engine_cs *ring)
8451b13d190SFrançois Tigeot {
846352ff8bdSFrançois Tigeot 	int ret;
8471b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
8481b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
8491b13d190SFrançois Tigeot 
850352ff8bdSFrançois Tigeot 	ret = gen8_init_workarounds(ring);
851352ff8bdSFrançois Tigeot 	if (ret)
852352ff8bdSFrançois Tigeot 		return ret;
853a05eeebfSFrançois Tigeot 
854352ff8bdSFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:bdw (pre-production) */
855352ff8bdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
856a05eeebfSFrançois Tigeot 
857352ff8bdSFrançois Tigeot 	/* WaDisableDopClockGating:bdw */
858352ff8bdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
859352ff8bdSFrançois Tigeot 			  DOP_CLOCK_GATING_DISABLE);
8601b13d190SFrançois Tigeot 
861352ff8bdSFrançois Tigeot 	WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
862352ff8bdSFrançois Tigeot 			  GEN8_SAMPLER_POWER_BYPASS_DIS);
863352ff8bdSFrançois Tigeot 
8642c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0,
865352ff8bdSFrançois Tigeot 			  /* WaForceContextSaveRestoreNonCoherent:bdw */
866352ff8bdSFrançois Tigeot 			  HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
867352ff8bdSFrançois Tigeot 			  /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
868352ff8bdSFrançois Tigeot 			  (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
8691b13d190SFrançois Tigeot 
870352ff8bdSFrançois Tigeot 	return 0;
871352ff8bdSFrançois Tigeot }
8721b13d190SFrançois Tigeot 
873352ff8bdSFrançois Tigeot static int chv_init_workarounds(struct intel_engine_cs *ring)
874352ff8bdSFrançois Tigeot {
875352ff8bdSFrançois Tigeot 	int ret;
876352ff8bdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
877352ff8bdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
878352ff8bdSFrançois Tigeot 
879352ff8bdSFrançois Tigeot 	ret = gen8_init_workarounds(ring);
880352ff8bdSFrançois Tigeot 	if (ret)
881352ff8bdSFrançois Tigeot 		return ret;
882352ff8bdSFrançois Tigeot 
883352ff8bdSFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:chv */
884352ff8bdSFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
8852c9916cdSFrançois Tigeot 
8862c9916cdSFrançois Tigeot 	/* Improve HiZ throughput on CHV. */
8872c9916cdSFrançois Tigeot 	WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
8882c9916cdSFrançois Tigeot 
8892c9916cdSFrançois Tigeot 	return 0;
8902c9916cdSFrançois Tigeot }
8912c9916cdSFrançois Tigeot 
892477eb7f9SFrançois Tigeot static int gen9_init_workarounds(struct intel_engine_cs *ring)
893477eb7f9SFrançois Tigeot {
894477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
895477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
89619c468b4SFrançois Tigeot 	uint32_t tmp;
897477eb7f9SFrançois Tigeot 
898352ff8bdSFrançois Tigeot 	/* WaEnableLbsSlaRetryTimerDecrement:skl */
899352ff8bdSFrançois Tigeot 	I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
900352ff8bdSFrançois Tigeot 		   GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
901352ff8bdSFrançois Tigeot 
902352ff8bdSFrançois Tigeot 	/* WaDisableKillLogic:bxt,skl */
903352ff8bdSFrançois Tigeot 	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
904352ff8bdSFrançois Tigeot 		   ECOCHK_DIS_TLB);
905352ff8bdSFrançois Tigeot 
90619c468b4SFrançois Tigeot 	/* WaDisablePartialInstShootdown:skl,bxt */
907477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
908477eb7f9SFrançois Tigeot 			  PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
909477eb7f9SFrançois Tigeot 
91019c468b4SFrançois Tigeot 	/* Syncing dependencies between camera and graphics:skl,bxt */
911477eb7f9SFrançois Tigeot 	WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
912477eb7f9SFrançois Tigeot 			  GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
913477eb7f9SFrançois Tigeot 
91419c468b4SFrançois Tigeot 	/* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
915*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, 0, SKL_REVID_B0) ||
916*aee94f86SFrançois Tigeot 	    IS_BXT_REVID(dev, 0, BXT_REVID_A1))
917477eb7f9SFrançois Tigeot 		WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
918477eb7f9SFrançois Tigeot 				  GEN9_DG_MIRROR_FIX_ENABLE);
919477eb7f9SFrançois Tigeot 
92019c468b4SFrançois Tigeot 	/* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
921*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, 0, SKL_REVID_B0) ||
922*aee94f86SFrançois Tigeot 	    IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
923477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
924477eb7f9SFrançois Tigeot 				  GEN9_RHWO_OPTIMIZATION_DISABLE);
925a05eeebfSFrançois Tigeot 		/*
926a05eeebfSFrançois Tigeot 		 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
927a05eeebfSFrançois Tigeot 		 * but we do that in per ctx batchbuffer as there is an issue
928a05eeebfSFrançois Tigeot 		 * with this register not getting restored on ctx restore
929a05eeebfSFrançois Tigeot 		 */
930477eb7f9SFrançois Tigeot 	}
931477eb7f9SFrançois Tigeot 
93219c468b4SFrançois Tigeot 	/* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt */
933*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, SKL_REVID_C0, REVID_FOREVER) || IS_BROXTON(dev))
934477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
935477eb7f9SFrançois Tigeot 				  GEN9_ENABLE_YV12_BUGFIX);
936477eb7f9SFrançois Tigeot 
93719c468b4SFrançois Tigeot 	/* Wa4x4STCOptimizationDisable:skl,bxt */
93819c468b4SFrançois Tigeot 	/* WaDisablePartialResolveInVc:skl,bxt */
939352ff8bdSFrançois Tigeot 	WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
940352ff8bdSFrançois Tigeot 					 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
941477eb7f9SFrançois Tigeot 
94219c468b4SFrançois Tigeot 	/* WaCcsTlbPrefetchDisable:skl,bxt */
943477eb7f9SFrançois Tigeot 	WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
944477eb7f9SFrançois Tigeot 			  GEN9_CCS_TLB_PREFETCH_ENABLE);
945477eb7f9SFrançois Tigeot 
94619c468b4SFrançois Tigeot 	/* WaDisableMaskBasedCammingInRCC:skl,bxt */
947*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, SKL_REVID_C0, SKL_REVID_C0) ||
948*aee94f86SFrançois Tigeot 	    IS_BXT_REVID(dev, 0, BXT_REVID_A1))
94919c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
95019c468b4SFrançois Tigeot 				  PIXEL_MASK_CAMMING_DISABLE);
95119c468b4SFrançois Tigeot 
95219c468b4SFrançois Tigeot 	/* WaForceContextSaveRestoreNonCoherent:skl,bxt */
95319c468b4SFrançois Tigeot 	tmp = HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT;
954*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, SKL_REVID_F0, SKL_REVID_F0) ||
955*aee94f86SFrançois Tigeot 	    IS_BXT_REVID(dev, BXT_REVID_B0, REVID_FOREVER))
95619c468b4SFrançois Tigeot 		tmp |= HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE;
95719c468b4SFrançois Tigeot 	WA_SET_BIT_MASKED(HDC_CHICKEN0, tmp);
95819c468b4SFrançois Tigeot 
959352ff8bdSFrançois Tigeot 	/* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt */
960*aee94f86SFrançois Tigeot 	if (IS_SKYLAKE(dev) || IS_BXT_REVID(dev, 0, BXT_REVID_B0))
961352ff8bdSFrançois Tigeot 		WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
962352ff8bdSFrançois Tigeot 				  GEN8_SAMPLER_POWER_BYPASS_DIS);
963352ff8bdSFrançois Tigeot 
964352ff8bdSFrançois Tigeot 	/* WaDisableSTUnitPowerOptimization:skl,bxt */
965352ff8bdSFrançois Tigeot 	WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
966352ff8bdSFrançois Tigeot 
967477eb7f9SFrançois Tigeot 	return 0;
968477eb7f9SFrançois Tigeot }
969477eb7f9SFrançois Tigeot 
970477eb7f9SFrançois Tigeot static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
971477eb7f9SFrançois Tigeot {
972477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
973477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
974477eb7f9SFrançois Tigeot 	u8 vals[3] = { 0, 0, 0 };
975477eb7f9SFrançois Tigeot 	unsigned int i;
976477eb7f9SFrançois Tigeot 
977477eb7f9SFrançois Tigeot 	for (i = 0; i < 3; i++) {
978477eb7f9SFrançois Tigeot 		u8 ss;
979477eb7f9SFrançois Tigeot 
980477eb7f9SFrançois Tigeot 		/*
981477eb7f9SFrançois Tigeot 		 * Only consider slices where one, and only one, subslice has 7
982477eb7f9SFrançois Tigeot 		 * EUs
983477eb7f9SFrançois Tigeot 		 */
984*aee94f86SFrançois Tigeot 		if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
985477eb7f9SFrançois Tigeot 			continue;
986477eb7f9SFrançois Tigeot 
987477eb7f9SFrançois Tigeot 		/*
988477eb7f9SFrançois Tigeot 		 * subslice_7eu[i] != 0 (because of the check above) and
989477eb7f9SFrançois Tigeot 		 * ss_max == 4 (maximum number of subslices possible per slice)
990477eb7f9SFrançois Tigeot 		 *
991477eb7f9SFrançois Tigeot 		 * ->    0 <= ss <= 3;
992477eb7f9SFrançois Tigeot 		 */
993477eb7f9SFrançois Tigeot 		ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
994477eb7f9SFrançois Tigeot 		vals[i] = 3 - ss;
995477eb7f9SFrançois Tigeot 	}
996477eb7f9SFrançois Tigeot 
997477eb7f9SFrançois Tigeot 	if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
998477eb7f9SFrançois Tigeot 		return 0;
999477eb7f9SFrançois Tigeot 
1000477eb7f9SFrançois Tigeot 	/* Tune IZ hashing. See intel_device_info_runtime_init() */
1001477eb7f9SFrançois Tigeot 	WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1002477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(2) |
1003477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(1) |
1004477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING_MASK(0),
1005477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(2, vals[2]) |
1006477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(1, vals[1]) |
1007477eb7f9SFrançois Tigeot 			    GEN9_IZ_HASHING(0, vals[0]));
1008477eb7f9SFrançois Tigeot 
1009477eb7f9SFrançois Tigeot 	return 0;
1010477eb7f9SFrançois Tigeot }
1011477eb7f9SFrançois Tigeot 
1012477eb7f9SFrançois Tigeot static int skl_init_workarounds(struct intel_engine_cs *ring)
1013477eb7f9SFrançois Tigeot {
1014352ff8bdSFrançois Tigeot 	int ret;
1015477eb7f9SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1016477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1017477eb7f9SFrançois Tigeot 
1018352ff8bdSFrançois Tigeot 	ret = gen9_init_workarounds(ring);
1019352ff8bdSFrançois Tigeot 	if (ret)
1020352ff8bdSFrançois Tigeot 		return ret;
1021352ff8bdSFrançois Tigeot 
1022*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, 0, SKL_REVID_D0)) {
1023352ff8bdSFrançois Tigeot 		/* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1024352ff8bdSFrançois Tigeot 		I915_WRITE(FF_SLICE_CS_CHICKEN2,
1025352ff8bdSFrançois Tigeot 			   _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1026352ff8bdSFrançois Tigeot 	}
1027352ff8bdSFrançois Tigeot 
1028352ff8bdSFrançois Tigeot 	/* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1029352ff8bdSFrançois Tigeot 	 * involving this register should also be added to WA batch as required.
1030352ff8bdSFrançois Tigeot 	 */
1031*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, 0, SKL_REVID_E0))
1032352ff8bdSFrançois Tigeot 		/* WaDisableLSQCROPERFforOCL:skl */
1033352ff8bdSFrançois Tigeot 		I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1034352ff8bdSFrançois Tigeot 			   GEN8_LQSC_RO_PERF_DIS);
1035352ff8bdSFrançois Tigeot 
1036352ff8bdSFrançois Tigeot 	/* WaEnableGapsTsvCreditFix:skl */
1037*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, SKL_REVID_C0, REVID_FOREVER)) {
1038352ff8bdSFrançois Tigeot 		I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1039352ff8bdSFrançois Tigeot 					   GEN9_GAPS_TSV_CREDIT_DISABLE));
1040352ff8bdSFrançois Tigeot 	}
1041477eb7f9SFrançois Tigeot 
1042477eb7f9SFrançois Tigeot 	/* WaDisablePowerCompilerClockGating:skl */
1043*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, SKL_REVID_B0, SKL_REVID_B0))
1044477eb7f9SFrançois Tigeot 		WA_SET_BIT_MASKED(HIZ_CHICKEN,
1045477eb7f9SFrançois Tigeot 				  BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1046477eb7f9SFrançois Tigeot 
1047*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, 0, SKL_REVID_F0)) {
104819c468b4SFrançois Tigeot 		/*
104919c468b4SFrançois Tigeot 		 *Use Force Non-Coherent whenever executing a 3D context. This
105019c468b4SFrançois Tigeot 		 * is a workaround for a possible hang in the unlikely event
105119c468b4SFrançois Tigeot 		 * a TLB invalidation occurs during a PSD flush.
105219c468b4SFrançois Tigeot 		 */
105319c468b4SFrançois Tigeot 		/* WaForceEnableNonCoherent:skl */
105419c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(HDC_CHICKEN0,
105519c468b4SFrançois Tigeot 				  HDC_FORCE_NON_COHERENT);
1056*aee94f86SFrançois Tigeot 
1057*aee94f86SFrançois Tigeot 		/* WaDisableHDCInvalidation:skl */
1058*aee94f86SFrançois Tigeot 		I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
1059*aee94f86SFrançois Tigeot 			   BDW_DISABLE_HDC_INVALIDATION);
106019c468b4SFrançois Tigeot 	}
106119c468b4SFrançois Tigeot 
1062a05eeebfSFrançois Tigeot 	/* WaBarrierPerformanceFixDisable:skl */
1063*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, SKL_REVID_C0, SKL_REVID_D0))
1064a05eeebfSFrançois Tigeot 		WA_SET_BIT_MASKED(HDC_CHICKEN0,
1065a05eeebfSFrançois Tigeot 				  HDC_FENCE_DEST_SLM_DISABLE |
1066a05eeebfSFrançois Tigeot 				  HDC_BARRIER_PERFORMANCE_DISABLE);
1067a05eeebfSFrançois Tigeot 
1068a05eeebfSFrançois Tigeot 	/* WaDisableSbeCacheDispatchPortSharing:skl */
1069*aee94f86SFrançois Tigeot 	if (IS_SKL_REVID(dev, 0, SKL_REVID_F0))
1070a05eeebfSFrançois Tigeot 		WA_SET_BIT_MASKED(
1071a05eeebfSFrançois Tigeot 			GEN7_HALF_SLICE_CHICKEN1,
1072a05eeebfSFrançois Tigeot 			GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1073a05eeebfSFrançois Tigeot 
1074477eb7f9SFrançois Tigeot 	return skl_tune_iz_hashing(ring);
1075477eb7f9SFrançois Tigeot }
1076477eb7f9SFrançois Tigeot 
107719c468b4SFrançois Tigeot static int bxt_init_workarounds(struct intel_engine_cs *ring)
107819c468b4SFrançois Tigeot {
1079352ff8bdSFrançois Tigeot 	int ret;
108019c468b4SFrançois Tigeot 	struct drm_device *dev = ring->dev;
108119c468b4SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
108219c468b4SFrançois Tigeot 
1083352ff8bdSFrançois Tigeot 	ret = gen9_init_workarounds(ring);
1084352ff8bdSFrançois Tigeot 	if (ret)
1085352ff8bdSFrançois Tigeot 		return ret;
1086352ff8bdSFrançois Tigeot 
1087352ff8bdSFrançois Tigeot 	/* WaStoreMultiplePTEenable:bxt */
1088352ff8bdSFrançois Tigeot 	/* This is a requirement according to Hardware specification */
1089*aee94f86SFrançois Tigeot 	if (IS_BXT_REVID(dev, 0, BXT_REVID_A1))
1090352ff8bdSFrançois Tigeot 		I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1091352ff8bdSFrançois Tigeot 
1092352ff8bdSFrançois Tigeot 	/* WaSetClckGatingDisableMedia:bxt */
1093*aee94f86SFrançois Tigeot 	if (IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
1094352ff8bdSFrançois Tigeot 		I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1095352ff8bdSFrançois Tigeot 					    ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1096352ff8bdSFrançois Tigeot 	}
109719c468b4SFrançois Tigeot 
109819c468b4SFrançois Tigeot 	/* WaDisableThreadStallDopClockGating:bxt */
109919c468b4SFrançois Tigeot 	WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
110019c468b4SFrançois Tigeot 			  STALL_DOP_GATING_DISABLE);
110119c468b4SFrançois Tigeot 
110219c468b4SFrançois Tigeot 	/* WaDisableSbeCacheDispatchPortSharing:bxt */
1103*aee94f86SFrançois Tigeot 	if (IS_BXT_REVID(dev, 0, BXT_REVID_B0)) {
110419c468b4SFrançois Tigeot 		WA_SET_BIT_MASKED(
110519c468b4SFrançois Tigeot 			GEN7_HALF_SLICE_CHICKEN1,
110619c468b4SFrançois Tigeot 			GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
110719c468b4SFrançois Tigeot 	}
110819c468b4SFrançois Tigeot 
110919c468b4SFrançois Tigeot 	return 0;
111019c468b4SFrançois Tigeot }
111119c468b4SFrançois Tigeot 
11122c9916cdSFrançois Tigeot int init_workarounds_ring(struct intel_engine_cs *ring)
11132c9916cdSFrançois Tigeot {
11142c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
11152c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11162c9916cdSFrançois Tigeot 
11172c9916cdSFrançois Tigeot 	WARN_ON(ring->id != RCS);
11182c9916cdSFrançois Tigeot 
11192c9916cdSFrançois Tigeot 	dev_priv->workarounds.count = 0;
11202c9916cdSFrançois Tigeot 
11212c9916cdSFrançois Tigeot 	if (IS_BROADWELL(dev))
11222c9916cdSFrançois Tigeot 		return bdw_init_workarounds(ring);
11232c9916cdSFrançois Tigeot 
11242c9916cdSFrançois Tigeot 	if (IS_CHERRYVIEW(dev))
11252c9916cdSFrançois Tigeot 		return chv_init_workarounds(ring);
11261b13d190SFrançois Tigeot 
1127477eb7f9SFrançois Tigeot 	if (IS_SKYLAKE(dev))
1128477eb7f9SFrançois Tigeot 		return skl_init_workarounds(ring);
112919c468b4SFrançois Tigeot 
113019c468b4SFrançois Tigeot 	if (IS_BROXTON(dev))
113119c468b4SFrançois Tigeot 		return bxt_init_workarounds(ring);
1132477eb7f9SFrançois Tigeot 
11331b13d190SFrançois Tigeot 	return 0;
11341b13d190SFrançois Tigeot }
11351b13d190SFrançois Tigeot 
1136ba55f2f5SFrançois Tigeot static int init_render_ring(struct intel_engine_cs *ring)
1137e3adcf8fSFrançois Tigeot {
1138e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1139e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1140e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
114124edb884SFrançois Tigeot 	if (ret)
114224edb884SFrançois Tigeot 		return ret;
1143e3adcf8fSFrançois Tigeot 
1144ba55f2f5SFrançois Tigeot 	/* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1145ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
1146f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1147f4e1c372SFrançois Tigeot 
1148f4e1c372SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
1149f4e1c372SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1150f4e1c372SFrançois Tigeot 	 * programmed to '1' on all products.
11515d0b1887SFrançois Tigeot 	 *
1152a05eeebfSFrançois Tigeot 	 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1153f4e1c372SFrançois Tigeot 	 */
1154a05eeebfSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
1155f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1156f4e1c372SFrançois Tigeot 
1157f4e1c372SFrançois Tigeot 	/* Required for the hardware to program scanline values for waiting */
1158ba55f2f5SFrançois Tigeot 	/* WaEnableFlushTlbInvalidationMode:snb */
1159f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6)
1160f4e1c372SFrançois Tigeot 		I915_WRITE(GFX_MODE,
1161ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
1162f4e1c372SFrançois Tigeot 
1163ba55f2f5SFrançois Tigeot 	/* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
1164e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev))
1165e3adcf8fSFrançois Tigeot 		I915_WRITE(GFX_MODE_GEN7,
1166ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1167f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
1168e3adcf8fSFrançois Tigeot 
1169e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
1170e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
1171e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
1172e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
1173e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
1174e3adcf8fSFrançois Tigeot 		 */
1175e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
1176f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
1177e3adcf8fSFrançois Tigeot 	}
1178e3adcf8fSFrançois Tigeot 
1179a05eeebfSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
1180f4e1c372SFrançois Tigeot 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1181f4e1c372SFrançois Tigeot 
11829edbd4a0SFrançois Tigeot 	if (HAS_L3_DPF(dev))
11839edbd4a0SFrançois Tigeot 		I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1184e3adcf8fSFrançois Tigeot 
11852c9916cdSFrançois Tigeot 	return init_workarounds_ring(ring);
1186e3adcf8fSFrançois Tigeot }
1187e3adcf8fSFrançois Tigeot 
1188ba55f2f5SFrançois Tigeot static void render_ring_cleanup(struct intel_engine_cs *ring)
1189e3adcf8fSFrançois Tigeot {
1190b5c29a34SFrançois Tigeot 	struct drm_device *dev = ring->dev;
119124edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
119224edb884SFrançois Tigeot 
119324edb884SFrançois Tigeot 	if (dev_priv->semaphore_obj) {
119424edb884SFrançois Tigeot 		i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
119524edb884SFrançois Tigeot 		drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
119624edb884SFrançois Tigeot 		dev_priv->semaphore_obj = NULL;
119724edb884SFrançois Tigeot 	}
1198b5c29a34SFrançois Tigeot 
11991b13d190SFrançois Tigeot 	intel_fini_pipe_control(ring);
1200e3adcf8fSFrançois Tigeot }
1201e3adcf8fSFrançois Tigeot 
1202a05eeebfSFrançois Tigeot static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
120324edb884SFrançois Tigeot 			   unsigned int num_dwords)
120424edb884SFrançois Tigeot {
120524edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 8
1206a05eeebfSFrançois Tigeot 	struct intel_engine_cs *signaller = signaller_req->ring;
120724edb884SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
120824edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
120924edb884SFrançois Tigeot 	struct intel_engine_cs *waiter;
121024edb884SFrançois Tigeot 	int i, ret, num_rings;
121124edb884SFrançois Tigeot 
121224edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
121324edb884SFrançois Tigeot 	num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
121424edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
121524edb884SFrançois Tigeot 
1216a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(signaller_req, num_dwords);
121724edb884SFrançois Tigeot 	if (ret)
121824edb884SFrançois Tigeot 		return ret;
121924edb884SFrançois Tigeot 
122024edb884SFrançois Tigeot 	for_each_ring(waiter, dev_priv, i) {
12212c9916cdSFrançois Tigeot 		u32 seqno;
122224edb884SFrançois Tigeot 		u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
122324edb884SFrançois Tigeot 		if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
122424edb884SFrançois Tigeot 			continue;
122524edb884SFrançois Tigeot 
1226a05eeebfSFrançois Tigeot 		seqno = i915_gem_request_get_seqno(signaller_req);
122724edb884SFrançois Tigeot 		intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
122824edb884SFrançois Tigeot 		intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
122924edb884SFrançois Tigeot 					   PIPE_CONTROL_QW_WRITE |
123024edb884SFrançois Tigeot 					   PIPE_CONTROL_FLUSH_ENABLE);
123124edb884SFrançois Tigeot 		intel_ring_emit(signaller, lower_32_bits(gtt_offset));
123224edb884SFrançois Tigeot 		intel_ring_emit(signaller, upper_32_bits(gtt_offset));
12332c9916cdSFrançois Tigeot 		intel_ring_emit(signaller, seqno);
123424edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
123524edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
123624edb884SFrançois Tigeot 					   MI_SEMAPHORE_TARGET(waiter->id));
123724edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
123824edb884SFrançois Tigeot 	}
123924edb884SFrançois Tigeot 
124024edb884SFrançois Tigeot 	return 0;
124124edb884SFrançois Tigeot }
124224edb884SFrançois Tigeot 
1243a05eeebfSFrançois Tigeot static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
124424edb884SFrançois Tigeot 			   unsigned int num_dwords)
124524edb884SFrançois Tigeot {
124624edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 6
1247a05eeebfSFrançois Tigeot 	struct intel_engine_cs *signaller = signaller_req->ring;
124824edb884SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
124924edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
125024edb884SFrançois Tigeot 	struct intel_engine_cs *waiter;
125124edb884SFrançois Tigeot 	int i, ret, num_rings;
125224edb884SFrançois Tigeot 
125324edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
125424edb884SFrançois Tigeot 	num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
125524edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
125624edb884SFrançois Tigeot 
1257a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(signaller_req, num_dwords);
125824edb884SFrançois Tigeot 	if (ret)
125924edb884SFrançois Tigeot 		return ret;
126024edb884SFrançois Tigeot 
126124edb884SFrançois Tigeot 	for_each_ring(waiter, dev_priv, i) {
12622c9916cdSFrançois Tigeot 		u32 seqno;
126324edb884SFrançois Tigeot 		u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
126424edb884SFrançois Tigeot 		if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
126524edb884SFrançois Tigeot 			continue;
126624edb884SFrançois Tigeot 
1267a05eeebfSFrançois Tigeot 		seqno = i915_gem_request_get_seqno(signaller_req);
126824edb884SFrançois Tigeot 		intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
126924edb884SFrançois Tigeot 					   MI_FLUSH_DW_OP_STOREDW);
127024edb884SFrançois Tigeot 		intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
127124edb884SFrançois Tigeot 					   MI_FLUSH_DW_USE_GTT);
127224edb884SFrançois Tigeot 		intel_ring_emit(signaller, upper_32_bits(gtt_offset));
12732c9916cdSFrançois Tigeot 		intel_ring_emit(signaller, seqno);
127424edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
127524edb884SFrançois Tigeot 					   MI_SEMAPHORE_TARGET(waiter->id));
127624edb884SFrançois Tigeot 		intel_ring_emit(signaller, 0);
127724edb884SFrançois Tigeot 	}
127824edb884SFrançois Tigeot 
127924edb884SFrançois Tigeot 	return 0;
128024edb884SFrançois Tigeot }
128124edb884SFrançois Tigeot 
1282a05eeebfSFrançois Tigeot static int gen6_signal(struct drm_i915_gem_request *signaller_req,
1283ba55f2f5SFrançois Tigeot 		       unsigned int num_dwords)
1284e3adcf8fSFrançois Tigeot {
1285a05eeebfSFrançois Tigeot 	struct intel_engine_cs *signaller = signaller_req->ring;
1286ba55f2f5SFrançois Tigeot 	struct drm_device *dev = signaller->dev;
1287ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1288ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *useless;
128924edb884SFrançois Tigeot 	int i, ret, num_rings;
1290ba55f2f5SFrançois Tigeot 
129124edb884SFrançois Tigeot #define MBOX_UPDATE_DWORDS 3
129224edb884SFrançois Tigeot 	num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
129324edb884SFrançois Tigeot 	num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
129424edb884SFrançois Tigeot #undef MBOX_UPDATE_DWORDS
1295ba55f2f5SFrançois Tigeot 
1296a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(signaller_req, num_dwords);
1297ba55f2f5SFrançois Tigeot 	if (ret)
1298ba55f2f5SFrançois Tigeot 		return ret;
1299ba55f2f5SFrançois Tigeot 
1300ba55f2f5SFrançois Tigeot 	for_each_ring(useless, dev_priv, i) {
1301*aee94f86SFrançois Tigeot 		i915_reg_t mbox_reg = signaller->semaphore.mbox.signal[i];
1302*aee94f86SFrançois Tigeot 
1303*aee94f86SFrançois Tigeot 		if (i915_mmio_reg_valid(mbox_reg)) {
1304a05eeebfSFrançois Tigeot 			u32 seqno = i915_gem_request_get_seqno(signaller_req);
1305*aee94f86SFrançois Tigeot 
1306ba55f2f5SFrançois Tigeot 			intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1307*aee94f86SFrançois Tigeot 			intel_ring_emit_reg(signaller, mbox_reg);
13082c9916cdSFrançois Tigeot 			intel_ring_emit(signaller, seqno);
1309ba55f2f5SFrançois Tigeot 		}
1310ba55f2f5SFrançois Tigeot 	}
1311ba55f2f5SFrançois Tigeot 
131224edb884SFrançois Tigeot 	/* If num_dwords was rounded, make sure the tail pointer is correct */
131324edb884SFrançois Tigeot 	if (num_rings % 2 == 0)
131424edb884SFrançois Tigeot 		intel_ring_emit(signaller, MI_NOOP);
131524edb884SFrançois Tigeot 
1316ba55f2f5SFrançois Tigeot 	return 0;
1317e3adcf8fSFrançois Tigeot }
1318e3adcf8fSFrançois Tigeot 
1319e3adcf8fSFrançois Tigeot /**
1320e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
1321e3adcf8fSFrançois Tigeot  *
1322a05eeebfSFrançois Tigeot  * @request - request to write to the ring
1323e3adcf8fSFrançois Tigeot  *
1324e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
1325e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
1326e3adcf8fSFrançois Tigeot  */
1327e3adcf8fSFrançois Tigeot static int
1328a05eeebfSFrançois Tigeot gen6_add_request(struct drm_i915_gem_request *req)
1329e3adcf8fSFrançois Tigeot {
1330a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1331ba55f2f5SFrançois Tigeot 	int ret;
1332e3adcf8fSFrançois Tigeot 
133324edb884SFrançois Tigeot 	if (ring->semaphore.signal)
1334a05eeebfSFrançois Tigeot 		ret = ring->semaphore.signal(req, 4);
133524edb884SFrançois Tigeot 	else
1336a05eeebfSFrançois Tigeot 		ret = intel_ring_begin(req, 4);
133724edb884SFrançois Tigeot 
13389edbd4a0SFrançois Tigeot 	if (ret)
13399edbd4a0SFrançois Tigeot 		return ret;
13409edbd4a0SFrançois Tigeot 
1341e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1342e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1343a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1344e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
13459edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1346e3adcf8fSFrançois Tigeot 
1347e3adcf8fSFrançois Tigeot 	return 0;
1348e3adcf8fSFrançois Tigeot }
1349e3adcf8fSFrançois Tigeot 
1350a2fdbec6SFrançois Tigeot static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1351a2fdbec6SFrançois Tigeot 					      u32 seqno)
1352a2fdbec6SFrançois Tigeot {
1353a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1354a2fdbec6SFrançois Tigeot 	return dev_priv->last_seqno < seqno;
1355a2fdbec6SFrançois Tigeot }
1356a2fdbec6SFrançois Tigeot 
1357e3adcf8fSFrançois Tigeot /**
1358e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
1359e3adcf8fSFrançois Tigeot  *
1360e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
1361e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
1362e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
1363e3adcf8fSFrançois Tigeot  */
136424edb884SFrançois Tigeot 
136524edb884SFrançois Tigeot static int
1366a05eeebfSFrançois Tigeot gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
136724edb884SFrançois Tigeot 	       struct intel_engine_cs *signaller,
136824edb884SFrançois Tigeot 	       u32 seqno)
136924edb884SFrançois Tigeot {
1370a05eeebfSFrançois Tigeot 	struct intel_engine_cs *waiter = waiter_req->ring;
137124edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = waiter->dev->dev_private;
137224edb884SFrançois Tigeot 	int ret;
137324edb884SFrançois Tigeot 
1374a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(waiter_req, 4);
137524edb884SFrançois Tigeot 	if (ret)
137624edb884SFrançois Tigeot 		return ret;
137724edb884SFrançois Tigeot 
137824edb884SFrançois Tigeot 	intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
137924edb884SFrançois Tigeot 				MI_SEMAPHORE_GLOBAL_GTT |
138024edb884SFrançois Tigeot 				MI_SEMAPHORE_POLL |
138124edb884SFrançois Tigeot 				MI_SEMAPHORE_SAD_GTE_SDD);
138224edb884SFrançois Tigeot 	intel_ring_emit(waiter, seqno);
138324edb884SFrançois Tigeot 	intel_ring_emit(waiter,
138424edb884SFrançois Tigeot 			lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
138524edb884SFrançois Tigeot 	intel_ring_emit(waiter,
138624edb884SFrançois Tigeot 			upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
138724edb884SFrançois Tigeot 	intel_ring_advance(waiter);
138824edb884SFrançois Tigeot 	return 0;
138924edb884SFrançois Tigeot }
139024edb884SFrançois Tigeot 
1391e3adcf8fSFrançois Tigeot static int
1392a05eeebfSFrançois Tigeot gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
1393ba55f2f5SFrançois Tigeot 	       struct intel_engine_cs *signaller,
1394e3adcf8fSFrançois Tigeot 	       u32 seqno)
1395e3adcf8fSFrançois Tigeot {
1396a05eeebfSFrançois Tigeot 	struct intel_engine_cs *waiter = waiter_req->ring;
1397e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
1398e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
1399e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
1400ba55f2f5SFrançois Tigeot 	u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1401ba55f2f5SFrançois Tigeot 	int ret;
1402e3adcf8fSFrançois Tigeot 
1403686a02f1SFrançois Tigeot 	/* Throughout all of the GEM code, seqno passed implies our current
1404686a02f1SFrançois Tigeot 	 * seqno is >= the last seqno executed. However for hardware the
1405686a02f1SFrançois Tigeot 	 * comparison is strictly greater than.
1406686a02f1SFrançois Tigeot 	 */
1407686a02f1SFrançois Tigeot 	seqno -= 1;
1408686a02f1SFrançois Tigeot 
1409ba55f2f5SFrançois Tigeot 	WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
1410686a02f1SFrançois Tigeot 
1411a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(waiter_req, 4);
1412e3adcf8fSFrançois Tigeot 	if (ret)
1413e3adcf8fSFrançois Tigeot 		return ret;
1414e3adcf8fSFrançois Tigeot 
1415a2fdbec6SFrançois Tigeot 	/* If seqno wrap happened, omit the wait with no-ops */
1416a2fdbec6SFrançois Tigeot 	if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
1417ba55f2f5SFrançois Tigeot 		intel_ring_emit(waiter, dw1 | wait_mbox);
1418e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, seqno);
1419e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, 0);
1420e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1421a2fdbec6SFrançois Tigeot 	} else {
1422a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1423a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1424a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1425a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
1426a2fdbec6SFrançois Tigeot 	}
1427e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
1428e3adcf8fSFrançois Tigeot 
1429e3adcf8fSFrançois Tigeot 	return 0;
1430e3adcf8fSFrançois Tigeot }
1431e3adcf8fSFrançois Tigeot 
1432e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
1433e3adcf8fSFrançois Tigeot do {									\
1434e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
1435e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
1436e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
1437e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
1438e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
1439e3adcf8fSFrançois Tigeot } while (0)
1440e3adcf8fSFrançois Tigeot 
1441e3adcf8fSFrançois Tigeot static int
1442a05eeebfSFrançois Tigeot pc_render_add_request(struct drm_i915_gem_request *req)
1443e3adcf8fSFrançois Tigeot {
1444a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1445ba55f2f5SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
1446e3adcf8fSFrançois Tigeot 	int ret;
1447e3adcf8fSFrançois Tigeot 
1448e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1449e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
1450e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
1451e3adcf8fSFrançois Tigeot 	 *
1452e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
1453e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1454e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
1455e3adcf8fSFrançois Tigeot 	 */
1456a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 32);
1457e3adcf8fSFrançois Tigeot 	if (ret)
1458e3adcf8fSFrançois Tigeot 		return ret;
1459e3adcf8fSFrançois Tigeot 
1460e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1461e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
1462e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
14639edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1464a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1465e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1466e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1467ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
1468e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1469ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1470e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1471ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1472e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1473ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1474e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1475ba55f2f5SFrançois Tigeot 	scratch_addr += 2 * CACHELINE_BYTES;
1476e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
1477b5c29a34SFrançois Tigeot 
1478e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1479e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
1480e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
1481e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
14829edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1483a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1484e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
14859edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1486e3adcf8fSFrançois Tigeot 
1487e3adcf8fSFrançois Tigeot 	return 0;
1488e3adcf8fSFrançois Tigeot }
1489e3adcf8fSFrançois Tigeot 
1490e3adcf8fSFrançois Tigeot static u32
1491ba55f2f5SFrançois Tigeot gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1492e3adcf8fSFrançois Tigeot {
1493e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
1494e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
1495e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
1496ba55f2f5SFrançois Tigeot 	if (!lazy_coherency) {
1497ba55f2f5SFrançois Tigeot 		struct drm_i915_private *dev_priv = ring->dev->dev_private;
1498ba55f2f5SFrançois Tigeot 		POSTING_READ(RING_ACTHD(ring->mmio_base));
1499ba55f2f5SFrançois Tigeot 	}
1500ba55f2f5SFrançois Tigeot 
1501e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1502e3adcf8fSFrançois Tigeot }
1503e3adcf8fSFrançois Tigeot 
1504b030f26bSFrançois Tigeot static u32
1505ba55f2f5SFrançois Tigeot ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1506e3adcf8fSFrançois Tigeot {
1507e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1508e3adcf8fSFrançois Tigeot }
1509e3adcf8fSFrançois Tigeot 
1510a2fdbec6SFrançois Tigeot static void
1511ba55f2f5SFrançois Tigeot ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1512a2fdbec6SFrançois Tigeot {
1513a2fdbec6SFrançois Tigeot 	intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1514a2fdbec6SFrançois Tigeot }
1515a2fdbec6SFrançois Tigeot 
1516b030f26bSFrançois Tigeot static u32
1517ba55f2f5SFrançois Tigeot pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1518e3adcf8fSFrançois Tigeot {
15199edbd4a0SFrançois Tigeot 	return ring->scratch.cpu_page[0];
1520e3adcf8fSFrançois Tigeot }
1521e3adcf8fSFrançois Tigeot 
1522a2fdbec6SFrançois Tigeot static void
1523ba55f2f5SFrançois Tigeot pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1524a2fdbec6SFrançois Tigeot {
15259edbd4a0SFrançois Tigeot 	ring->scratch.cpu_page[0] = seqno;
1526a2fdbec6SFrançois Tigeot }
1527a2fdbec6SFrançois Tigeot 
1528e3adcf8fSFrançois Tigeot static bool
1529ba55f2f5SFrançois Tigeot gen5_ring_get_irq(struct intel_engine_cs *ring)
1530e3adcf8fSFrançois Tigeot {
1531e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1532ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15335e269720SFrançois Tigeot 	unsigned long flags;
1534e3adcf8fSFrançois Tigeot 
15352c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1536e3adcf8fSFrançois Tigeot 		return false;
1537e3adcf8fSFrançois Tigeot 
15385e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15399edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0)
154024edb884SFrançois Tigeot 		gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
15415e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1542e3adcf8fSFrançois Tigeot 
1543e3adcf8fSFrançois Tigeot 	return true;
1544e3adcf8fSFrançois Tigeot }
1545e3adcf8fSFrançois Tigeot 
1546e3adcf8fSFrançois Tigeot static void
1547ba55f2f5SFrançois Tigeot gen5_ring_put_irq(struct intel_engine_cs *ring)
1548e3adcf8fSFrançois Tigeot {
1549e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1550ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15515e269720SFrançois Tigeot 	unsigned long flags;
1552e3adcf8fSFrançois Tigeot 
15535e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15549edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0)
155524edb884SFrançois Tigeot 		gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
15565e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1557686a02f1SFrançois Tigeot }
1558686a02f1SFrançois Tigeot 
1559686a02f1SFrançois Tigeot static bool
1560ba55f2f5SFrançois Tigeot i9xx_ring_get_irq(struct intel_engine_cs *ring)
1561686a02f1SFrançois Tigeot {
1562686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1563ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15645e269720SFrançois Tigeot 	unsigned long flags;
1565686a02f1SFrançois Tigeot 
15662c9916cdSFrançois Tigeot 	if (!intel_irqs_enabled(dev_priv))
1567686a02f1SFrançois Tigeot 		return false;
1568686a02f1SFrançois Tigeot 
15695e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15709edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
1571686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
1572686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
1573686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
1574686a02f1SFrançois Tigeot 	}
15755e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1576686a02f1SFrançois Tigeot 
1577686a02f1SFrançois Tigeot 	return true;
1578686a02f1SFrançois Tigeot }
1579686a02f1SFrançois Tigeot 
1580686a02f1SFrançois Tigeot static void
1581ba55f2f5SFrançois Tigeot i9xx_ring_put_irq(struct intel_engine_cs *ring)
1582686a02f1SFrançois Tigeot {
1583686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1584ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15855e269720SFrançois Tigeot 	unsigned long flags;
1586686a02f1SFrançois Tigeot 
15875e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
15889edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
1589686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
1590686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
1591686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
1592686a02f1SFrançois Tigeot 	}
15935e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1594686a02f1SFrançois Tigeot }
1595686a02f1SFrançois Tigeot 
1596686a02f1SFrançois Tigeot static bool
1597ba55f2f5SFrançois Tigeot i8xx_ring_get_irq(struct intel_engine_cs *ring)
1598686a02f1SFrançois Tigeot {
1599686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1600ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16015e269720SFrançois Tigeot 	unsigned long flags;
1602686a02f1SFrançois Tigeot 
16032c9916cdSFrançois Tigeot 	if (!intel_irqs_enabled(dev_priv))
1604686a02f1SFrançois Tigeot 		return false;
1605686a02f1SFrançois Tigeot 
16065e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
16079edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
1608686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
1609686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
1610686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
1611686a02f1SFrançois Tigeot 	}
16125e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1613686a02f1SFrançois Tigeot 
1614686a02f1SFrançois Tigeot 	return true;
1615686a02f1SFrançois Tigeot }
1616686a02f1SFrançois Tigeot 
1617686a02f1SFrançois Tigeot static void
1618ba55f2f5SFrançois Tigeot i8xx_ring_put_irq(struct intel_engine_cs *ring)
1619686a02f1SFrançois Tigeot {
1620686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
1621ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16225e269720SFrançois Tigeot 	unsigned long flags;
1623686a02f1SFrançois Tigeot 
16245e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
16259edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
1626686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
1627686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
1628686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
1629e3adcf8fSFrançois Tigeot 	}
16305e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1631e3adcf8fSFrançois Tigeot }
1632e3adcf8fSFrançois Tigeot 
1633e3adcf8fSFrançois Tigeot static int
1634a05eeebfSFrançois Tigeot bsd_ring_flush(struct drm_i915_gem_request *req,
1635b5c29a34SFrançois Tigeot 	       u32     invalidate_domains,
1636b5c29a34SFrançois Tigeot 	       u32     flush_domains)
1637e3adcf8fSFrançois Tigeot {
1638a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1639e3adcf8fSFrançois Tigeot 	int ret;
1640e3adcf8fSFrançois Tigeot 
1641a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
1642e3adcf8fSFrançois Tigeot 	if (ret)
1643e3adcf8fSFrançois Tigeot 		return ret;
1644e3adcf8fSFrançois Tigeot 
1645e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
1646e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1647e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1648e3adcf8fSFrançois Tigeot 	return 0;
1649e3adcf8fSFrançois Tigeot }
1650e3adcf8fSFrançois Tigeot 
1651e3adcf8fSFrançois Tigeot static int
1652a05eeebfSFrançois Tigeot i9xx_add_request(struct drm_i915_gem_request *req)
1653e3adcf8fSFrançois Tigeot {
1654a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1655e3adcf8fSFrançois Tigeot 	int ret;
1656e3adcf8fSFrançois Tigeot 
1657a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
1658e3adcf8fSFrançois Tigeot 	if (ret)
1659e3adcf8fSFrançois Tigeot 		return ret;
1660e3adcf8fSFrançois Tigeot 
1661e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1662e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1663a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1664e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
16659edbd4a0SFrançois Tigeot 	__intel_ring_advance(ring);
1666e3adcf8fSFrançois Tigeot 
1667e3adcf8fSFrançois Tigeot 	return 0;
1668e3adcf8fSFrançois Tigeot }
1669e3adcf8fSFrançois Tigeot 
1670e3adcf8fSFrançois Tigeot static bool
1671ba55f2f5SFrançois Tigeot gen6_ring_get_irq(struct intel_engine_cs *ring)
1672e3adcf8fSFrançois Tigeot {
1673e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1674ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16755e269720SFrançois Tigeot 	unsigned long flags;
1676e3adcf8fSFrançois Tigeot 
16772c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1678e3adcf8fSFrançois Tigeot 		return false;
1679e3adcf8fSFrançois Tigeot 
16805e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
16819edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
16829edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
16835d0b1887SFrançois Tigeot 			I915_WRITE_IMR(ring,
16845d0b1887SFrançois Tigeot 				       ~(ring->irq_enable_mask |
16859edbd4a0SFrançois Tigeot 					 GT_PARITY_ERROR(dev)));
1686686a02f1SFrançois Tigeot 		else
1687686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
168824edb884SFrançois Tigeot 		gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1689e3adcf8fSFrançois Tigeot 	}
16905e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1691e3adcf8fSFrançois Tigeot 
1692e3adcf8fSFrançois Tigeot 	return true;
1693e3adcf8fSFrançois Tigeot }
1694e3adcf8fSFrançois Tigeot 
1695e3adcf8fSFrançois Tigeot static void
1696ba55f2f5SFrançois Tigeot gen6_ring_put_irq(struct intel_engine_cs *ring)
1697e3adcf8fSFrançois Tigeot {
1698e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1699ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
17005e269720SFrançois Tigeot 	unsigned long flags;
1701e3adcf8fSFrançois Tigeot 
17025e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17039edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
17049edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS)
17059edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1706686a02f1SFrançois Tigeot 		else
1707686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
170824edb884SFrançois Tigeot 		gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1709e3adcf8fSFrançois Tigeot 	}
17105e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1711e3adcf8fSFrançois Tigeot }
1712e3adcf8fSFrançois Tigeot 
17135d0b1887SFrançois Tigeot static bool
1714ba55f2f5SFrançois Tigeot hsw_vebox_get_irq(struct intel_engine_cs *ring)
17155d0b1887SFrançois Tigeot {
17165d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
17175d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
17185e269720SFrançois Tigeot 	unsigned long flags;
17195d0b1887SFrançois Tigeot 
17202c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
17215d0b1887SFrançois Tigeot 		return false;
17225d0b1887SFrançois Tigeot 
17235e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17249edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
17255d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
172624edb884SFrançois Tigeot 		gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
17275d0b1887SFrançois Tigeot 	}
17285e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
17295d0b1887SFrançois Tigeot 
17305d0b1887SFrançois Tigeot 	return true;
17315d0b1887SFrançois Tigeot }
17325d0b1887SFrançois Tigeot 
17335d0b1887SFrançois Tigeot static void
1734ba55f2f5SFrançois Tigeot hsw_vebox_put_irq(struct intel_engine_cs *ring)
17355d0b1887SFrançois Tigeot {
17365d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
17375d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
17385e269720SFrançois Tigeot 	unsigned long flags;
17395d0b1887SFrançois Tigeot 
17405e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17419edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
17425d0b1887SFrançois Tigeot 		I915_WRITE_IMR(ring, ~0);
174324edb884SFrançois Tigeot 		gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
17445d0b1887SFrançois Tigeot 	}
17455e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
17469edbd4a0SFrançois Tigeot }
17479edbd4a0SFrançois Tigeot 
17489edbd4a0SFrançois Tigeot static bool
1749ba55f2f5SFrançois Tigeot gen8_ring_get_irq(struct intel_engine_cs *ring)
17509edbd4a0SFrançois Tigeot {
17519edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
17529edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
17535e269720SFrançois Tigeot 	unsigned long flags;
17549edbd4a0SFrançois Tigeot 
17552c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
17569edbd4a0SFrançois Tigeot 		return false;
17579edbd4a0SFrançois Tigeot 
17585e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17599edbd4a0SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
17609edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
17619edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
17629edbd4a0SFrançois Tigeot 				       ~(ring->irq_enable_mask |
17639edbd4a0SFrançois Tigeot 					 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
17649edbd4a0SFrançois Tigeot 		} else {
17659edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
17669edbd4a0SFrançois Tigeot 		}
17679edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
17689edbd4a0SFrançois Tigeot 	}
17695e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
17709edbd4a0SFrançois Tigeot 
17719edbd4a0SFrançois Tigeot 	return true;
17729edbd4a0SFrançois Tigeot }
17739edbd4a0SFrançois Tigeot 
17749edbd4a0SFrançois Tigeot static void
1775ba55f2f5SFrançois Tigeot gen8_ring_put_irq(struct intel_engine_cs *ring)
17769edbd4a0SFrançois Tigeot {
17779edbd4a0SFrançois Tigeot 	struct drm_device *dev = ring->dev;
17789edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
17795e269720SFrançois Tigeot 	unsigned long flags;
17809edbd4a0SFrançois Tigeot 
17815e269720SFrançois Tigeot 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
17829edbd4a0SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
17839edbd4a0SFrançois Tigeot 		if (HAS_L3_DPF(dev) && ring->id == RCS) {
17849edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring,
17859edbd4a0SFrançois Tigeot 				       ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
17869edbd4a0SFrançois Tigeot 		} else {
17879edbd4a0SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
17889edbd4a0SFrançois Tigeot 		}
17899edbd4a0SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
17909edbd4a0SFrançois Tigeot 	}
17915e269720SFrançois Tigeot 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
17925d0b1887SFrançois Tigeot }
17935d0b1887SFrançois Tigeot 
1794e3adcf8fSFrançois Tigeot static int
1795a05eeebfSFrançois Tigeot i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
1796ba55f2f5SFrançois Tigeot 			 u64 offset, u32 length,
1797477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1798e3adcf8fSFrançois Tigeot {
1799a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1800e3adcf8fSFrançois Tigeot 	int ret;
1801e3adcf8fSFrançois Tigeot 
1802a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
1803e3adcf8fSFrançois Tigeot 	if (ret)
1804e3adcf8fSFrançois Tigeot 		return ret;
1805e3adcf8fSFrançois Tigeot 
1806e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
1807686a02f1SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1808b5c29a34SFrançois Tigeot 			MI_BATCH_GTT |
1809477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
1810477eb7f9SFrançois Tigeot 			 0 : MI_BATCH_NON_SECURE_I965));
1811e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1812e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1813e3adcf8fSFrançois Tigeot 
1814e3adcf8fSFrançois Tigeot 	return 0;
1815e3adcf8fSFrançois Tigeot }
1816e3adcf8fSFrançois Tigeot 
1817b5c29a34SFrançois Tigeot /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1818b5c29a34SFrançois Tigeot #define I830_BATCH_LIMIT (256*1024)
181924edb884SFrançois Tigeot #define I830_TLB_ENTRIES (2)
182024edb884SFrançois Tigeot #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
1821e3adcf8fSFrançois Tigeot static int
1822a05eeebfSFrançois Tigeot i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
1823ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1824477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1825e3adcf8fSFrançois Tigeot {
1826a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
182724edb884SFrançois Tigeot 	u32 cs_offset = ring->scratch.gtt_offset;
1828e3adcf8fSFrançois Tigeot 	int ret;
1829e3adcf8fSFrançois Tigeot 
1830a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 6);
183124edb884SFrançois Tigeot 	if (ret)
183224edb884SFrançois Tigeot 		return ret;
183324edb884SFrançois Tigeot 
183424edb884SFrançois Tigeot 	/* Evict the invalid PTE TLBs */
183524edb884SFrançois Tigeot 	intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
183624edb884SFrançois Tigeot 	intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
183724edb884SFrançois Tigeot 	intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
183824edb884SFrançois Tigeot 	intel_ring_emit(ring, cs_offset);
183924edb884SFrançois Tigeot 	intel_ring_emit(ring, 0xdeadbeef);
184024edb884SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
184124edb884SFrançois Tigeot 	intel_ring_advance(ring);
184224edb884SFrançois Tigeot 
1843477eb7f9SFrançois Tigeot 	if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
184424edb884SFrançois Tigeot 		if (len > I830_BATCH_LIMIT)
184524edb884SFrançois Tigeot 			return -ENOSPC;
184624edb884SFrançois Tigeot 
1847a05eeebfSFrançois Tigeot 		ret = intel_ring_begin(req, 6 + 2);
184824edb884SFrançois Tigeot 		if (ret)
184924edb884SFrançois Tigeot 			return ret;
185024edb884SFrançois Tigeot 
185124edb884SFrançois Tigeot 		/* Blit the batch (which has now all relocs applied) to the
185224edb884SFrançois Tigeot 		 * stable batch scratch bo area (so that the CS never
185324edb884SFrançois Tigeot 		 * stumbles over its tlb invalidation bug) ...
185424edb884SFrançois Tigeot 		 */
185524edb884SFrançois Tigeot 		intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
185624edb884SFrançois Tigeot 		intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
185724edb884SFrançois Tigeot 		intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
185824edb884SFrançois Tigeot 		intel_ring_emit(ring, cs_offset);
185924edb884SFrançois Tigeot 		intel_ring_emit(ring, 4096);
186024edb884SFrançois Tigeot 		intel_ring_emit(ring, offset);
186124edb884SFrançois Tigeot 
186224edb884SFrançois Tigeot 		intel_ring_emit(ring, MI_FLUSH);
186324edb884SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
186424edb884SFrançois Tigeot 		intel_ring_advance(ring);
186524edb884SFrançois Tigeot 
186624edb884SFrançois Tigeot 		/* ... and execute it. */
186724edb884SFrançois Tigeot 		offset = cs_offset;
186824edb884SFrançois Tigeot 	}
186924edb884SFrançois Tigeot 
1870a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
1871e3adcf8fSFrançois Tigeot 	if (ret)
1872e3adcf8fSFrançois Tigeot 		return ret;
1873e3adcf8fSFrançois Tigeot 
1874e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER);
1875477eb7f9SFrançois Tigeot 	intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1876477eb7f9SFrançois Tigeot 					0 : MI_BATCH_NON_SECURE));
1877e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset + len - 8);
1878b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1879686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
1880686a02f1SFrançois Tigeot 
1881686a02f1SFrançois Tigeot 	return 0;
1882686a02f1SFrançois Tigeot }
1883686a02f1SFrançois Tigeot 
1884686a02f1SFrançois Tigeot static int
1885a05eeebfSFrançois Tigeot i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
1886ba55f2f5SFrançois Tigeot 			 u64 offset, u32 len,
1887477eb7f9SFrançois Tigeot 			 unsigned dispatch_flags)
1888686a02f1SFrançois Tigeot {
1889a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
1890686a02f1SFrançois Tigeot 	int ret;
1891686a02f1SFrançois Tigeot 
1892a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
1893e3adcf8fSFrançois Tigeot 	if (ret)
1894e3adcf8fSFrançois Tigeot 		return ret;
1895e3adcf8fSFrançois Tigeot 
1896686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1897477eb7f9SFrançois Tigeot 	intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1898477eb7f9SFrançois Tigeot 					0 : MI_BATCH_NON_SECURE));
1899e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1900e3adcf8fSFrançois Tigeot 
1901e3adcf8fSFrançois Tigeot 	return 0;
1902e3adcf8fSFrançois Tigeot }
1903e3adcf8fSFrançois Tigeot 
1904ba55f2f5SFrançois Tigeot static void cleanup_status_page(struct intel_engine_cs *ring)
1905e3adcf8fSFrançois Tigeot {
1906e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1907e3adcf8fSFrançois Tigeot 
1908e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
1909e3adcf8fSFrançois Tigeot 	if (obj == NULL)
1910e3adcf8fSFrançois Tigeot 		return;
1911e3adcf8fSFrançois Tigeot 
19127ec9f8e5SFrançois Tigeot 	kunmap(sg_page(obj->pages->sgl));
1913ba55f2f5SFrançois Tigeot 	i915_gem_object_ggtt_unpin(obj);
1914e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1915e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
1916e3adcf8fSFrançois Tigeot }
1917e3adcf8fSFrançois Tigeot 
1918ba55f2f5SFrançois Tigeot static int init_status_page(struct intel_engine_cs *ring)
1919e3adcf8fSFrançois Tigeot {
1920e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1921ba55f2f5SFrançois Tigeot 
1922ba55f2f5SFrançois Tigeot 	if ((obj = ring->status_page.obj) == NULL) {
192324edb884SFrançois Tigeot 		unsigned flags;
1924e3adcf8fSFrançois Tigeot 		int ret;
1925e3adcf8fSFrançois Tigeot 
1926ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(ring->dev, 4096);
1927e3adcf8fSFrançois Tigeot 		if (obj == NULL) {
1928e3adcf8fSFrançois Tigeot 			DRM_ERROR("Failed to allocate status page\n");
1929ba55f2f5SFrançois Tigeot 			return -ENOMEM;
1930e3adcf8fSFrançois Tigeot 		}
1931e3adcf8fSFrançois Tigeot 
1932ba55f2f5SFrançois Tigeot 		ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1933ba55f2f5SFrançois Tigeot 		if (ret)
1934e3adcf8fSFrançois Tigeot 			goto err_unref;
1935ba55f2f5SFrançois Tigeot 
193624edb884SFrançois Tigeot 		flags = 0;
193724edb884SFrançois Tigeot 		if (!HAS_LLC(ring->dev))
193824edb884SFrançois Tigeot 			/* On g33, we cannot place HWS above 256MiB, so
193924edb884SFrançois Tigeot 			 * restrict its pinning to the low mappable arena.
194024edb884SFrançois Tigeot 			 * Though this restriction is not documented for
194124edb884SFrançois Tigeot 			 * gen4, gen5, or byt, they also behave similarly
194224edb884SFrançois Tigeot 			 * and hang if the HWS is placed at the top of the
194324edb884SFrançois Tigeot 			 * GTT. To generalise, it appears that all !llc
194424edb884SFrançois Tigeot 			 * platforms have issues with us placing the HWS
194524edb884SFrançois Tigeot 			 * above the mappable region (even though we never
194624edb884SFrançois Tigeot 			 * actualy map it).
194724edb884SFrançois Tigeot 			 */
194824edb884SFrançois Tigeot 			flags |= PIN_MAPPABLE;
194924edb884SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
1950ba55f2f5SFrançois Tigeot 		if (ret) {
1951ba55f2f5SFrançois Tigeot err_unref:
1952ba55f2f5SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
1953ba55f2f5SFrançois Tigeot 			return ret;
1954ba55f2f5SFrançois Tigeot 		}
1955ba55f2f5SFrançois Tigeot 
1956ba55f2f5SFrançois Tigeot 		ring->status_page.obj = obj;
1957e3adcf8fSFrançois Tigeot 	}
1958e3adcf8fSFrançois Tigeot 
19599edbd4a0SFrançois Tigeot 	ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
19607ec9f8e5SFrançois Tigeot 	ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
1961e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1962e3adcf8fSFrançois Tigeot 
1963b5c29a34SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1964e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1965e3adcf8fSFrançois Tigeot 
1966e3adcf8fSFrançois Tigeot 	return 0;
1967e3adcf8fSFrançois Tigeot }
1968e3adcf8fSFrançois Tigeot 
1969ba55f2f5SFrançois Tigeot static int init_phys_status_page(struct intel_engine_cs *ring)
1970686a02f1SFrançois Tigeot {
1971686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1972686a02f1SFrançois Tigeot 
1973686a02f1SFrançois Tigeot 	if (!dev_priv->status_page_dmah) {
1974686a02f1SFrançois Tigeot 		dev_priv->status_page_dmah =
1975b31e9d59SFrançois Tigeot 			drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1976686a02f1SFrançois Tigeot 		if (!dev_priv->status_page_dmah)
1977686a02f1SFrançois Tigeot 			return -ENOMEM;
1978686a02f1SFrançois Tigeot 	}
1979686a02f1SFrançois Tigeot 
1980686a02f1SFrançois Tigeot 	ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1981686a02f1SFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1982686a02f1SFrançois Tigeot 
1983686a02f1SFrançois Tigeot 	return 0;
1984686a02f1SFrançois Tigeot }
1985686a02f1SFrançois Tigeot 
19862c9916cdSFrançois Tigeot void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
19872c9916cdSFrançois Tigeot {
1988*aee94f86SFrançois Tigeot 	if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
1989*aee94f86SFrançois Tigeot 		vunmap(ringbuf->virtual_start);
1990*aee94f86SFrançois Tigeot 	else
199124409b39SFrançois Tigeot 		iounmap(ringbuf->virtual_start);
19922c9916cdSFrançois Tigeot 	ringbuf->virtual_start = NULL;
19932c9916cdSFrançois Tigeot 	i915_gem_object_ggtt_unpin(ringbuf->obj);
19942c9916cdSFrançois Tigeot }
19952c9916cdSFrançois Tigeot 
1996*aee94f86SFrançois Tigeot static u32 *vmap_obj(struct drm_i915_gem_object *obj)
1997*aee94f86SFrançois Tigeot {
1998*aee94f86SFrançois Tigeot 	struct sg_page_iter sg_iter;
1999*aee94f86SFrançois Tigeot 	struct vm_page **pages;
2000*aee94f86SFrançois Tigeot 	void *addr;
2001*aee94f86SFrançois Tigeot 	int i;
2002*aee94f86SFrançois Tigeot 
2003*aee94f86SFrançois Tigeot 	pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
2004*aee94f86SFrançois Tigeot 	if (pages == NULL)
2005*aee94f86SFrançois Tigeot 		return NULL;
2006*aee94f86SFrançois Tigeot 
2007*aee94f86SFrançois Tigeot 	i = 0;
2008*aee94f86SFrançois Tigeot 	for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0)
2009*aee94f86SFrançois Tigeot 		pages[i++] = sg_page_iter_page(&sg_iter);
2010*aee94f86SFrançois Tigeot 
2011*aee94f86SFrançois Tigeot 	addr = vmap(pages, i, 0, PAGE_KERNEL);
2012*aee94f86SFrançois Tigeot 	drm_free_large(pages);
2013*aee94f86SFrançois Tigeot 
2014*aee94f86SFrançois Tigeot 	return addr;
2015*aee94f86SFrançois Tigeot }
2016*aee94f86SFrançois Tigeot 
20172c9916cdSFrançois Tigeot int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
20182c9916cdSFrançois Tigeot 				     struct intel_ringbuffer *ringbuf)
20192c9916cdSFrançois Tigeot {
20202c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
20212c9916cdSFrançois Tigeot 	struct drm_i915_gem_object *obj = ringbuf->obj;
20222c9916cdSFrançois Tigeot 	int ret;
20232c9916cdSFrançois Tigeot 
2024*aee94f86SFrançois Tigeot 	if (HAS_LLC(dev_priv) && !obj->stolen) {
2025*aee94f86SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, 0);
2026*aee94f86SFrançois Tigeot 		if (ret)
2027*aee94f86SFrançois Tigeot 			return ret;
2028*aee94f86SFrançois Tigeot 
2029*aee94f86SFrançois Tigeot 		ret = i915_gem_object_set_to_cpu_domain(obj, true);
2030*aee94f86SFrançois Tigeot 		if (ret) {
2031*aee94f86SFrançois Tigeot 			i915_gem_object_ggtt_unpin(obj);
2032*aee94f86SFrançois Tigeot 			return ret;
2033*aee94f86SFrançois Tigeot 		}
2034*aee94f86SFrançois Tigeot 
2035*aee94f86SFrançois Tigeot 		ringbuf->virtual_start = (char *)vmap_obj(obj);
2036*aee94f86SFrançois Tigeot 		if (ringbuf->virtual_start == NULL) {
2037*aee94f86SFrançois Tigeot 			i915_gem_object_ggtt_unpin(obj);
2038*aee94f86SFrançois Tigeot 			return -ENOMEM;
2039*aee94f86SFrançois Tigeot 		}
2040*aee94f86SFrançois Tigeot 	} else {
20412c9916cdSFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
20422c9916cdSFrançois Tigeot 		if (ret)
20432c9916cdSFrançois Tigeot 			return ret;
20442c9916cdSFrançois Tigeot 
20452c9916cdSFrançois Tigeot 		ret = i915_gem_object_set_to_gtt_domain(obj, true);
20462c9916cdSFrançois Tigeot 		if (ret) {
20472c9916cdSFrançois Tigeot 			i915_gem_object_ggtt_unpin(obj);
20482c9916cdSFrançois Tigeot 			return ret;
20492c9916cdSFrançois Tigeot 		}
20502c9916cdSFrançois Tigeot 
20512c9916cdSFrançois Tigeot 		ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
20522c9916cdSFrançois Tigeot 						    i915_gem_obj_ggtt_offset(obj), ringbuf->size);
20532c9916cdSFrançois Tigeot 		if (ringbuf->virtual_start == NULL) {
20542c9916cdSFrançois Tigeot 			i915_gem_object_ggtt_unpin(obj);
20552c9916cdSFrançois Tigeot 			return -EINVAL;
20562c9916cdSFrançois Tigeot 		}
2057*aee94f86SFrançois Tigeot 	}
20582c9916cdSFrançois Tigeot 
20592c9916cdSFrançois Tigeot 	return 0;
20602c9916cdSFrançois Tigeot }
20612c9916cdSFrançois Tigeot 
2062352ff8bdSFrançois Tigeot static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
2063e3adcf8fSFrançois Tigeot {
206424edb884SFrançois Tigeot 	drm_gem_object_unreference(&ringbuf->obj->base);
206524edb884SFrançois Tigeot 	ringbuf->obj = NULL;
206624edb884SFrançois Tigeot }
206724edb884SFrançois Tigeot 
2068352ff8bdSFrançois Tigeot static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
206924edb884SFrançois Tigeot 				      struct intel_ringbuffer *ringbuf)
207024edb884SFrançois Tigeot {
2071e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
2072e3adcf8fSFrançois Tigeot 
2073a2fdbec6SFrançois Tigeot 	obj = NULL;
2074a2fdbec6SFrançois Tigeot 	if (!HAS_LLC(dev))
2075ba55f2f5SFrançois Tigeot 		obj = i915_gem_object_create_stolen(dev, ringbuf->size);
2076a2fdbec6SFrançois Tigeot 	if (obj == NULL)
2077ba55f2f5SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, ringbuf->size);
2078ba55f2f5SFrançois Tigeot 	if (obj == NULL)
2079ba55f2f5SFrançois Tigeot 		return -ENOMEM;
2080e3adcf8fSFrançois Tigeot 
208124edb884SFrançois Tigeot 	/* mark ring buffers as read-only from GPU side by default */
208224edb884SFrançois Tigeot 	obj->gt_ro = 1;
208324edb884SFrançois Tigeot 
2084ba55f2f5SFrançois Tigeot 	ringbuf->obj = obj;
2085ba55f2f5SFrançois Tigeot 
20862c9916cdSFrançois Tigeot 	return 0;
2087ba55f2f5SFrançois Tigeot }
2088ba55f2f5SFrançois Tigeot 
2089352ff8bdSFrançois Tigeot struct intel_ringbuffer *
2090352ff8bdSFrançois Tigeot intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
2091352ff8bdSFrançois Tigeot {
2092352ff8bdSFrançois Tigeot 	struct intel_ringbuffer *ring;
2093352ff8bdSFrançois Tigeot 	int ret;
2094352ff8bdSFrançois Tigeot 
2095352ff8bdSFrançois Tigeot 	ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2096*aee94f86SFrançois Tigeot 	if (ring == NULL) {
2097*aee94f86SFrançois Tigeot 		DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2098*aee94f86SFrançois Tigeot 				 engine->name);
2099352ff8bdSFrançois Tigeot 		return ERR_PTR(-ENOMEM);
2100*aee94f86SFrançois Tigeot 	}
2101352ff8bdSFrançois Tigeot 
2102352ff8bdSFrançois Tigeot 	ring->ring = engine;
2103*aee94f86SFrançois Tigeot 	list_add(&ring->link, &engine->buffers);
2104352ff8bdSFrançois Tigeot 
2105352ff8bdSFrançois Tigeot 	ring->size = size;
2106352ff8bdSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
2107352ff8bdSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
2108352ff8bdSFrançois Tigeot 	 * of the buffer.
2109352ff8bdSFrançois Tigeot 	 */
2110352ff8bdSFrançois Tigeot 	ring->effective_size = size;
2111352ff8bdSFrançois Tigeot 	if (IS_I830(engine->dev) || IS_845G(engine->dev))
2112352ff8bdSFrançois Tigeot 		ring->effective_size -= 2 * CACHELINE_BYTES;
2113352ff8bdSFrançois Tigeot 
2114352ff8bdSFrançois Tigeot 	ring->last_retired_head = -1;
2115352ff8bdSFrançois Tigeot 	intel_ring_update_space(ring);
2116352ff8bdSFrançois Tigeot 
2117352ff8bdSFrançois Tigeot 	ret = intel_alloc_ringbuffer_obj(engine->dev, ring);
2118352ff8bdSFrançois Tigeot 	if (ret) {
2119*aee94f86SFrançois Tigeot 		DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
2120352ff8bdSFrançois Tigeot 				 engine->name, ret);
2121*aee94f86SFrançois Tigeot 		list_del(&ring->link);
2122352ff8bdSFrançois Tigeot 		kfree(ring);
2123352ff8bdSFrançois Tigeot 		return ERR_PTR(ret);
2124352ff8bdSFrançois Tigeot 	}
2125352ff8bdSFrançois Tigeot 
2126352ff8bdSFrançois Tigeot 	return ring;
2127352ff8bdSFrançois Tigeot }
2128352ff8bdSFrançois Tigeot 
2129352ff8bdSFrançois Tigeot void
2130352ff8bdSFrançois Tigeot intel_ringbuffer_free(struct intel_ringbuffer *ring)
2131352ff8bdSFrançois Tigeot {
2132352ff8bdSFrançois Tigeot 	intel_destroy_ringbuffer_obj(ring);
2133*aee94f86SFrançois Tigeot 	list_del(&ring->link);
2134352ff8bdSFrançois Tigeot 	kfree(ring);
2135352ff8bdSFrançois Tigeot }
2136352ff8bdSFrançois Tigeot 
2137ba55f2f5SFrançois Tigeot static int intel_init_ring_buffer(struct drm_device *dev,
2138ba55f2f5SFrançois Tigeot 				  struct intel_engine_cs *ring)
2139ba55f2f5SFrançois Tigeot {
21402c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf;
2141ba55f2f5SFrançois Tigeot 	int ret;
2142ba55f2f5SFrançois Tigeot 
21432c9916cdSFrançois Tigeot 	WARN_ON(ring->buffer);
21442c9916cdSFrançois Tigeot 
2145ba55f2f5SFrançois Tigeot 	ring->dev = dev;
2146ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
2147ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
21481b13d190SFrançois Tigeot 	INIT_LIST_HEAD(&ring->execlist_queue);
2149*aee94f86SFrançois Tigeot 	INIT_LIST_HEAD(&ring->buffers);
215019c468b4SFrançois Tigeot 	i915_gem_batch_pool_init(dev, &ring->batch_pool);
2151ba55f2f5SFrançois Tigeot 	memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
2152ba55f2f5SFrançois Tigeot 
2153ba55f2f5SFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
2154ba55f2f5SFrançois Tigeot 
2155352ff8bdSFrançois Tigeot 	ringbuf = intel_engine_create_ringbuffer(ring, 32 * PAGE_SIZE);
2156*aee94f86SFrançois Tigeot 	if (IS_ERR(ringbuf)) {
2157*aee94f86SFrançois Tigeot 		ret = PTR_ERR(ringbuf);
2158*aee94f86SFrançois Tigeot 		goto error;
2159*aee94f86SFrançois Tigeot 	}
2160352ff8bdSFrançois Tigeot 	ring->buffer = ringbuf;
2161352ff8bdSFrançois Tigeot 
2162ba55f2f5SFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
2163ba55f2f5SFrançois Tigeot 		ret = init_status_page(ring);
2164e3adcf8fSFrançois Tigeot 		if (ret)
2165ba55f2f5SFrançois Tigeot 			goto error;
2166ba55f2f5SFrançois Tigeot 	} else {
2167ba55f2f5SFrançois Tigeot 		BUG_ON(ring->id != RCS);
2168ba55f2f5SFrançois Tigeot 		ret = init_phys_status_page(ring);
2169ba55f2f5SFrançois Tigeot 		if (ret)
2170ba55f2f5SFrançois Tigeot 			goto error;
2171ba55f2f5SFrançois Tigeot 	}
2172ba55f2f5SFrançois Tigeot 
21732c9916cdSFrançois Tigeot 	ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
21742c9916cdSFrançois Tigeot 	if (ret) {
21752c9916cdSFrançois Tigeot 		DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
21762c9916cdSFrançois Tigeot 				ring->name, ret);
21772c9916cdSFrançois Tigeot 		intel_destroy_ringbuffer_obj(ringbuf);
2178ba55f2f5SFrançois Tigeot 		goto error;
2179ba55f2f5SFrançois Tigeot 	}
2180e3adcf8fSFrançois Tigeot 
2181ba55f2f5SFrançois Tigeot 	ret = i915_cmd_parser_init_ring(ring);
2182ba55f2f5SFrançois Tigeot 	if (ret)
2183ba55f2f5SFrançois Tigeot 		goto error;
2184ba55f2f5SFrançois Tigeot 
2185e3adcf8fSFrançois Tigeot 	return 0;
2186e3adcf8fSFrançois Tigeot 
2187ba55f2f5SFrançois Tigeot error:
2188*aee94f86SFrançois Tigeot 	intel_cleanup_ring_buffer(ring);
2189e3adcf8fSFrançois Tigeot 	return ret;
2190e3adcf8fSFrançois Tigeot }
2191e3adcf8fSFrançois Tigeot 
2192ba55f2f5SFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
2193e3adcf8fSFrançois Tigeot {
21942c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv;
2195e3adcf8fSFrançois Tigeot 
2196ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
2197e3adcf8fSFrançois Tigeot 		return;
2198e3adcf8fSFrançois Tigeot 
21992c9916cdSFrançois Tigeot 	dev_priv = to_i915(ring->dev);
22002c9916cdSFrançois Tigeot 
2201*aee94f86SFrançois Tigeot 	if (ring->buffer) {
2202ba55f2f5SFrançois Tigeot 		intel_stop_ring_buffer(ring);
2203ba55f2f5SFrançois Tigeot 		WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
2204b030f26bSFrançois Tigeot 
2205352ff8bdSFrançois Tigeot 		intel_unpin_ringbuffer_obj(ring->buffer);
2206352ff8bdSFrançois Tigeot 		intel_ringbuffer_free(ring->buffer);
2207352ff8bdSFrançois Tigeot 		ring->buffer = NULL;
2208*aee94f86SFrançois Tigeot 	}
2209e3adcf8fSFrançois Tigeot 
2210e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
2211e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
2212e3adcf8fSFrançois Tigeot 
2213e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
2214ba55f2f5SFrançois Tigeot 
2215ba55f2f5SFrançois Tigeot 	i915_cmd_parser_fini_ring(ring);
221619c468b4SFrançois Tigeot 	i915_gem_batch_pool_fini(&ring->batch_pool);
2217*aee94f86SFrançois Tigeot 	ring->dev = NULL;
2218e3adcf8fSFrançois Tigeot }
2219e3adcf8fSFrançois Tigeot 
222019c468b4SFrançois Tigeot static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
2221e3adcf8fSFrançois Tigeot {
2222ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2223e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
222419c468b4SFrançois Tigeot 	unsigned space;
2225e3adcf8fSFrançois Tigeot 	int ret;
2226e3adcf8fSFrançois Tigeot 
22272c9916cdSFrançois Tigeot 	if (intel_ring_space(ringbuf) >= n)
2228e3adcf8fSFrançois Tigeot 		return 0;
2229e3adcf8fSFrançois Tigeot 
2230a05eeebfSFrançois Tigeot 	/* The whole point of reserving space is to not wait! */
2231a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_in_use);
2232a05eeebfSFrançois Tigeot 
2233e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
223419c468b4SFrançois Tigeot 		space = __intel_ring_space(request->postfix, ringbuf->tail,
223519c468b4SFrançois Tigeot 					   ringbuf->size);
223619c468b4SFrançois Tigeot 		if (space >= n)
2237e3adcf8fSFrançois Tigeot 			break;
2238e3adcf8fSFrançois Tigeot 	}
2239e3adcf8fSFrançois Tigeot 
224019c468b4SFrançois Tigeot 	if (WARN_ON(&request->list == &ring->request_list))
2241e3adcf8fSFrançois Tigeot 		return -ENOSPC;
2242e3adcf8fSFrançois Tigeot 
22432c9916cdSFrançois Tigeot 	ret = i915_wait_request(request);
2244e3adcf8fSFrançois Tigeot 	if (ret)
2245e3adcf8fSFrançois Tigeot 		return ret;
2246e3adcf8fSFrançois Tigeot 
224719c468b4SFrançois Tigeot 	ringbuf->space = space;
2248e3adcf8fSFrançois Tigeot 	return 0;
2249e3adcf8fSFrançois Tigeot }
2250e3adcf8fSFrançois Tigeot 
2251a05eeebfSFrançois Tigeot static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
2252b030f26bSFrançois Tigeot {
2253b030f26bSFrançois Tigeot 	uint32_t __iomem *virt;
2254ba55f2f5SFrançois Tigeot 	int rem = ringbuf->size - ringbuf->tail;
2255b030f26bSFrançois Tigeot 
2256ba55f2f5SFrançois Tigeot 	virt = (unsigned int *)((char *)ringbuf->virtual_start + ringbuf->tail);
2257b030f26bSFrançois Tigeot 	rem /= 4;
2258b030f26bSFrançois Tigeot 	while (rem--)
2259686a02f1SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
2260b030f26bSFrançois Tigeot 
2261ba55f2f5SFrançois Tigeot 	ringbuf->tail = 0;
22622c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
2263b030f26bSFrançois Tigeot }
2264b030f26bSFrançois Tigeot 
2265ba55f2f5SFrançois Tigeot int intel_ring_idle(struct intel_engine_cs *ring)
2266b030f26bSFrançois Tigeot {
22672c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *req;
2268b5c29a34SFrançois Tigeot 
2269b5c29a34SFrançois Tigeot 	/* Wait upon the last request to be completed */
2270b5c29a34SFrançois Tigeot 	if (list_empty(&ring->request_list))
2271b5c29a34SFrançois Tigeot 		return 0;
2272b5c29a34SFrançois Tigeot 
22732c9916cdSFrançois Tigeot 	req = list_entry(ring->request_list.prev,
2274b5c29a34SFrançois Tigeot 			struct drm_i915_gem_request,
22752c9916cdSFrançois Tigeot 			list);
2276b5c29a34SFrançois Tigeot 
227719c468b4SFrançois Tigeot 	/* Make sure we do not trigger any retires */
227819c468b4SFrançois Tigeot 	return __i915_wait_request(req,
227919c468b4SFrançois Tigeot 				   atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
228019c468b4SFrançois Tigeot 				   to_i915(ring->dev)->mm.interruptible,
228119c468b4SFrançois Tigeot 				   NULL, NULL);
2282b5c29a34SFrançois Tigeot }
2283b5c29a34SFrançois Tigeot 
228419c468b4SFrançois Tigeot int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
2285b5c29a34SFrançois Tigeot {
228619c468b4SFrançois Tigeot 	request->ringbuf = request->ring->buffer;
22872c9916cdSFrançois Tigeot 	return 0;
22889edbd4a0SFrançois Tigeot }
22899edbd4a0SFrançois Tigeot 
2290a05eeebfSFrançois Tigeot int intel_ring_reserve_space(struct drm_i915_gem_request *request)
2291a2fdbec6SFrançois Tigeot {
2292a05eeebfSFrançois Tigeot 	/*
2293a05eeebfSFrançois Tigeot 	 * The first call merely notes the reserve request and is common for
2294a05eeebfSFrançois Tigeot 	 * all back ends. The subsequent localised _begin() call actually
2295a05eeebfSFrançois Tigeot 	 * ensures that the reservation is available. Without the begin, if
2296a05eeebfSFrançois Tigeot 	 * the request creator immediately submitted the request without
2297a05eeebfSFrançois Tigeot 	 * adding any commands to it then there might not actually be
2298a05eeebfSFrançois Tigeot 	 * sufficient room for the submission commands.
2299a05eeebfSFrançois Tigeot 	 */
2300a05eeebfSFrançois Tigeot 	intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
2301a2fdbec6SFrançois Tigeot 
2302a05eeebfSFrançois Tigeot 	return intel_ring_begin(request, 0);
2303a2fdbec6SFrançois Tigeot }
2304a2fdbec6SFrançois Tigeot 
2305a05eeebfSFrançois Tigeot void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2306a05eeebfSFrançois Tigeot {
2307a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_size);
2308a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_in_use);
2309a05eeebfSFrançois Tigeot 
2310a05eeebfSFrançois Tigeot 	ringbuf->reserved_size = size;
2311a05eeebfSFrançois Tigeot }
2312a05eeebfSFrançois Tigeot 
2313a05eeebfSFrançois Tigeot void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2314a05eeebfSFrançois Tigeot {
2315a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_in_use);
2316a05eeebfSFrançois Tigeot 
2317a05eeebfSFrançois Tigeot 	ringbuf->reserved_size   = 0;
2318a05eeebfSFrançois Tigeot 	ringbuf->reserved_in_use = false;
2319a05eeebfSFrançois Tigeot }
2320a05eeebfSFrançois Tigeot 
2321a05eeebfSFrançois Tigeot void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2322a05eeebfSFrançois Tigeot {
2323a05eeebfSFrançois Tigeot 	WARN_ON(ringbuf->reserved_in_use);
2324a05eeebfSFrançois Tigeot 
2325a05eeebfSFrançois Tigeot 	ringbuf->reserved_in_use = true;
2326a05eeebfSFrançois Tigeot 	ringbuf->reserved_tail   = ringbuf->tail;
2327a05eeebfSFrançois Tigeot }
2328a05eeebfSFrançois Tigeot 
2329a05eeebfSFrançois Tigeot void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2330a05eeebfSFrançois Tigeot {
2331a05eeebfSFrançois Tigeot 	WARN_ON(!ringbuf->reserved_in_use);
2332a05eeebfSFrançois Tigeot 	if (ringbuf->tail > ringbuf->reserved_tail) {
2333a05eeebfSFrançois Tigeot 		WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2334a05eeebfSFrançois Tigeot 		     "request reserved size too small: %d vs %d!\n",
2335a05eeebfSFrançois Tigeot 		     ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2336a05eeebfSFrançois Tigeot 	} else {
2337a05eeebfSFrançois Tigeot 		/*
2338a05eeebfSFrançois Tigeot 		 * The ring was wrapped while the reserved space was in use.
2339a05eeebfSFrançois Tigeot 		 * That means that some unknown amount of the ring tail was
2340a05eeebfSFrançois Tigeot 		 * no-op filled and skipped. Thus simply adding the ring size
2341a05eeebfSFrançois Tigeot 		 * to the tail and doing the above space check will not work.
2342a05eeebfSFrançois Tigeot 		 * Rather than attempt to track how much tail was skipped,
2343a05eeebfSFrançois Tigeot 		 * it is much simpler to say that also skipping the sanity
2344a05eeebfSFrançois Tigeot 		 * check every once in a while is not a big issue.
2345a05eeebfSFrançois Tigeot 		 */
2346a05eeebfSFrançois Tigeot 	}
2347a05eeebfSFrançois Tigeot 
2348a05eeebfSFrançois Tigeot 	ringbuf->reserved_size   = 0;
2349a05eeebfSFrançois Tigeot 	ringbuf->reserved_in_use = false;
2350a05eeebfSFrançois Tigeot }
2351a05eeebfSFrançois Tigeot 
2352a05eeebfSFrançois Tigeot static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
2353a05eeebfSFrançois Tigeot {
2354a05eeebfSFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
2355a05eeebfSFrançois Tigeot 	int remain_usable = ringbuf->effective_size - ringbuf->tail;
2356a05eeebfSFrançois Tigeot 	int remain_actual = ringbuf->size - ringbuf->tail;
2357a05eeebfSFrançois Tigeot 	int ret, total_bytes, wait_bytes = 0;
2358a05eeebfSFrançois Tigeot 	bool need_wrap = false;
2359a05eeebfSFrançois Tigeot 
2360a05eeebfSFrançois Tigeot 	if (ringbuf->reserved_in_use)
2361a05eeebfSFrançois Tigeot 		total_bytes = bytes;
2362a05eeebfSFrançois Tigeot 	else
2363a05eeebfSFrançois Tigeot 		total_bytes = bytes + ringbuf->reserved_size;
2364a05eeebfSFrançois Tigeot 
2365a05eeebfSFrançois Tigeot 	if (unlikely(bytes > remain_usable)) {
2366a05eeebfSFrançois Tigeot 		/*
2367a05eeebfSFrançois Tigeot 		 * Not enough space for the basic request. So need to flush
2368a05eeebfSFrançois Tigeot 		 * out the remainder and then wait for base + reserved.
2369a05eeebfSFrançois Tigeot 		 */
2370a05eeebfSFrançois Tigeot 		wait_bytes = remain_actual + total_bytes;
2371a05eeebfSFrançois Tigeot 		need_wrap = true;
2372a05eeebfSFrançois Tigeot 	} else {
2373a05eeebfSFrançois Tigeot 		if (unlikely(total_bytes > remain_usable)) {
2374a05eeebfSFrançois Tigeot 			/*
2375a05eeebfSFrançois Tigeot 			 * The base request will fit but the reserved space
2376a05eeebfSFrançois Tigeot 			 * falls off the end. So only need to to wait for the
2377a05eeebfSFrançois Tigeot 			 * reserved size after flushing out the remainder.
2378a05eeebfSFrançois Tigeot 			 */
2379a05eeebfSFrançois Tigeot 			wait_bytes = remain_actual + ringbuf->reserved_size;
2380a05eeebfSFrançois Tigeot 			need_wrap = true;
2381a05eeebfSFrançois Tigeot 		} else if (total_bytes > ringbuf->space) {
2382a05eeebfSFrançois Tigeot 			/* No wrapping required, just waiting. */
2383a05eeebfSFrançois Tigeot 			wait_bytes = total_bytes;
2384a05eeebfSFrançois Tigeot 		}
2385a05eeebfSFrançois Tigeot 	}
2386a05eeebfSFrançois Tigeot 
2387a05eeebfSFrançois Tigeot 	if (wait_bytes) {
2388a05eeebfSFrançois Tigeot 		ret = ring_wait_for_space(ring, wait_bytes);
2389a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
2390a2fdbec6SFrançois Tigeot 			return ret;
2391a05eeebfSFrançois Tigeot 
2392a05eeebfSFrançois Tigeot 		if (need_wrap)
2393a05eeebfSFrançois Tigeot 			__wrap_ring_buffer(ringbuf);
2394a2fdbec6SFrançois Tigeot 	}
2395a2fdbec6SFrançois Tigeot 
2396a2fdbec6SFrançois Tigeot 	return 0;
2397a2fdbec6SFrançois Tigeot }
2398a2fdbec6SFrançois Tigeot 
2399a05eeebfSFrançois Tigeot int intel_ring_begin(struct drm_i915_gem_request *req,
2400e3adcf8fSFrançois Tigeot 		     int num_dwords)
2401e3adcf8fSFrançois Tigeot {
2402a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring;
2403a05eeebfSFrançois Tigeot 	struct drm_i915_private *dev_priv;
2404e3adcf8fSFrançois Tigeot 	int ret;
2405e3adcf8fSFrançois Tigeot 
2406a05eeebfSFrançois Tigeot 	WARN_ON(req == NULL);
2407a05eeebfSFrançois Tigeot 	ring = req->ring;
2408a05eeebfSFrançois Tigeot 	dev_priv = ring->dev->dev_private;
2409a05eeebfSFrançois Tigeot 
2410a2fdbec6SFrançois Tigeot 	ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2411a2fdbec6SFrançois Tigeot 				   dev_priv->mm.interruptible);
2412245593daSFrançois Tigeot 	if (ret)
2413245593daSFrançois Tigeot 		return ret;
2414e3adcf8fSFrançois Tigeot 
24159edbd4a0SFrançois Tigeot 	ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
24169edbd4a0SFrançois Tigeot 	if (ret)
24179edbd4a0SFrançois Tigeot 		return ret;
24189edbd4a0SFrançois Tigeot 
2419ba55f2f5SFrançois Tigeot 	ring->buffer->space -= num_dwords * sizeof(uint32_t);
24209edbd4a0SFrançois Tigeot 	return 0;
24219edbd4a0SFrançois Tigeot }
24229edbd4a0SFrançois Tigeot 
24239edbd4a0SFrançois Tigeot /* Align the ring tail to a cacheline boundary */
2424a05eeebfSFrançois Tigeot int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
24259edbd4a0SFrançois Tigeot {
2426a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2427ba55f2f5SFrançois Tigeot 	int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
24289edbd4a0SFrançois Tigeot 	int ret;
24299edbd4a0SFrançois Tigeot 
24309edbd4a0SFrançois Tigeot 	if (num_dwords == 0)
24319edbd4a0SFrançois Tigeot 		return 0;
24329edbd4a0SFrançois Tigeot 
2433ba55f2f5SFrançois Tigeot 	num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
2434a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, num_dwords);
24359edbd4a0SFrançois Tigeot 	if (ret)
24369edbd4a0SFrançois Tigeot 		return ret;
24379edbd4a0SFrançois Tigeot 
24389edbd4a0SFrançois Tigeot 	while (num_dwords--)
24399edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
24409edbd4a0SFrançois Tigeot 
24419edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
24429edbd4a0SFrançois Tigeot 
24439edbd4a0SFrançois Tigeot 	return 0;
2444e3adcf8fSFrançois Tigeot }
2445e3adcf8fSFrançois Tigeot 
2446ba55f2f5SFrançois Tigeot void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
2447a2fdbec6SFrançois Tigeot {
244824edb884SFrançois Tigeot 	struct drm_device *dev = ring->dev;
244924edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2450a2fdbec6SFrançois Tigeot 
245124edb884SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
2452a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2453a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
245424edb884SFrançois Tigeot 		if (HAS_VEBOX(dev))
24559edbd4a0SFrançois Tigeot 			I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
2456e3adcf8fSFrançois Tigeot 	}
2457e3adcf8fSFrançois Tigeot 
2458a2fdbec6SFrançois Tigeot 	ring->set_seqno(ring, seqno);
24595d0b1887SFrançois Tigeot 	ring->hangcheck.seqno = seqno;
2460e3adcf8fSFrançois Tigeot }
2461e3adcf8fSFrançois Tigeot 
2462ba55f2f5SFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
2463f4e1c372SFrançois Tigeot 				     u32 value)
2464e3adcf8fSFrançois Tigeot {
2465ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2466e3adcf8fSFrançois Tigeot 
2467e3adcf8fSFrançois Tigeot        /* Every tail move must follow the sequence below */
2468f4e1c372SFrançois Tigeot 
2469f4e1c372SFrançois Tigeot 	/* Disable notification that the ring is IDLE. The GT
2470f4e1c372SFrançois Tigeot 	 * will then assume that it is busy and bring it out of rc6.
2471f4e1c372SFrançois Tigeot 	 */
2472e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2473f4e1c372SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2474e3adcf8fSFrançois Tigeot 
2475f4e1c372SFrançois Tigeot 	/* Clear the context id. Here be magic! */
2476f4e1c372SFrançois Tigeot 	I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2477e3adcf8fSFrançois Tigeot 
2478f4e1c372SFrançois Tigeot 	/* Wait for the ring not to be idle, i.e. for it to wake up. */
2479f4e1c372SFrançois Tigeot 	if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
2480f4e1c372SFrançois Tigeot 		      GEN6_BSD_SLEEP_INDICATOR) == 0,
2481f4e1c372SFrançois Tigeot 		     50))
2482f4e1c372SFrançois Tigeot 		DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
2483f4e1c372SFrançois Tigeot 
2484f4e1c372SFrançois Tigeot 	/* Now that the ring is fully powered up, update the tail */
2485e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
2486f4e1c372SFrançois Tigeot 	POSTING_READ(RING_TAIL(ring->mmio_base));
2487f4e1c372SFrançois Tigeot 
2488f4e1c372SFrançois Tigeot 	/* Let the ring send IDLE messages to the GT again,
2489f4e1c372SFrançois Tigeot 	 * and so let it sleep to conserve power when idle.
2490f4e1c372SFrançois Tigeot 	 */
2491e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2492f4e1c372SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2493e3adcf8fSFrançois Tigeot }
2494e3adcf8fSFrançois Tigeot 
2495a05eeebfSFrançois Tigeot static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
2496b5c29a34SFrançois Tigeot 			       u32 invalidate, u32 flush)
2497e3adcf8fSFrançois Tigeot {
2498a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2499e3adcf8fSFrançois Tigeot 	uint32_t cmd;
2500e3adcf8fSFrançois Tigeot 	int ret;
2501e3adcf8fSFrançois Tigeot 
2502a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
2503e3adcf8fSFrançois Tigeot 	if (ret)
2504e3adcf8fSFrançois Tigeot 		return ret;
2505e3adcf8fSFrançois Tigeot 
2506e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
25079edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8)
25089edbd4a0SFrançois Tigeot 		cmd += 1;
25092c9916cdSFrançois Tigeot 
25102c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
25112c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
25122c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
25132c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
25142c9916cdSFrançois Tigeot 	 */
25152c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
25162c9916cdSFrançois Tigeot 
2517b5c29a34SFrançois Tigeot 	/*
2518b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.5 - video engine command streamer:
2519b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
2520b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
2521b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
2522b5c29a34SFrançois Tigeot 	 */
2523e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
25242c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
25252c9916cdSFrançois Tigeot 
2526e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
2527b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
25289edbd4a0SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 8) {
25299edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
25309edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
25319edbd4a0SFrançois Tigeot 	} else  {
25329edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0);
25339edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
25349edbd4a0SFrançois Tigeot 	}
25359edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
25369edbd4a0SFrançois Tigeot 	return 0;
25379edbd4a0SFrançois Tigeot }
25389edbd4a0SFrançois Tigeot 
25399edbd4a0SFrançois Tigeot static int
2540a05eeebfSFrançois Tigeot gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2541ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
2542477eb7f9SFrançois Tigeot 			      unsigned dispatch_flags)
25439edbd4a0SFrançois Tigeot {
2544a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2545477eb7f9SFrançois Tigeot 	bool ppgtt = USES_PPGTT(ring->dev) &&
2546477eb7f9SFrançois Tigeot 			!(dispatch_flags & I915_DISPATCH_SECURE);
25479edbd4a0SFrançois Tigeot 	int ret;
25489edbd4a0SFrançois Tigeot 
2549a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
25509edbd4a0SFrançois Tigeot 	if (ret)
25519edbd4a0SFrançois Tigeot 		return ret;
25529edbd4a0SFrançois Tigeot 
25539edbd4a0SFrançois Tigeot 	/* FIXME(BDW): Address space and security selectors. */
2554a05eeebfSFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
2555a05eeebfSFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_RS ?
2556a05eeebfSFrançois Tigeot 			 MI_BATCH_RESOURCE_STREAMER : 0));
2557ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, lower_32_bits(offset));
2558ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, upper_32_bits(offset));
2559e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
2560e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
25619edbd4a0SFrançois Tigeot 
2562e3adcf8fSFrançois Tigeot 	return 0;
2563e3adcf8fSFrançois Tigeot }
2564e3adcf8fSFrançois Tigeot 
2565e3adcf8fSFrançois Tigeot static int
2566a05eeebfSFrançois Tigeot hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2567ba55f2f5SFrançois Tigeot 			     u64 offset, u32 len,
2568477eb7f9SFrançois Tigeot 			     unsigned dispatch_flags)
2569e3adcf8fSFrançois Tigeot {
2570a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2571e3adcf8fSFrançois Tigeot 	int ret;
2572e3adcf8fSFrançois Tigeot 
2573a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
2574e3adcf8fSFrançois Tigeot 	if (ret)
2575e3adcf8fSFrançois Tigeot 		return ret;
2576e3adcf8fSFrançois Tigeot 
2577b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
25781b13d190SFrançois Tigeot 			MI_BATCH_BUFFER_START |
2579477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
2580a05eeebfSFrançois Tigeot 			 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2581a05eeebfSFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_RS ?
2582a05eeebfSFrançois Tigeot 			 MI_BATCH_RESOURCE_STREAMER : 0));
2583b5c29a34SFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
2584b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset);
2585b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
2586b5c29a34SFrançois Tigeot 
2587b5c29a34SFrançois Tigeot 	return 0;
2588b5c29a34SFrançois Tigeot }
2589b5c29a34SFrançois Tigeot 
2590b5c29a34SFrançois Tigeot static int
2591a05eeebfSFrançois Tigeot gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2592ba55f2f5SFrançois Tigeot 			      u64 offset, u32 len,
2593477eb7f9SFrançois Tigeot 			      unsigned dispatch_flags)
2594b5c29a34SFrançois Tigeot {
2595a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
2596b5c29a34SFrançois Tigeot 	int ret;
2597b5c29a34SFrançois Tigeot 
2598a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 2);
2599b5c29a34SFrançois Tigeot 	if (ret)
2600b5c29a34SFrançois Tigeot 		return ret;
2601b5c29a34SFrançois Tigeot 
2602b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
2603b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START |
2604477eb7f9SFrançois Tigeot 			(dispatch_flags & I915_DISPATCH_SECURE ?
2605477eb7f9SFrançois Tigeot 			 0 : MI_BATCH_NON_SECURE_I965));
2606e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
2607e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
2608e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
2609e3adcf8fSFrançois Tigeot 
2610e3adcf8fSFrançois Tigeot 	return 0;
2611e3adcf8fSFrançois Tigeot }
2612e3adcf8fSFrançois Tigeot 
2613e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
2614e3adcf8fSFrançois Tigeot 
2615a05eeebfSFrançois Tigeot static int gen6_ring_flush(struct drm_i915_gem_request *req,
2616b5c29a34SFrançois Tigeot 			   u32 invalidate, u32 flush)
2617e3adcf8fSFrançois Tigeot {
2618a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
26195d0b1887SFrançois Tigeot 	struct drm_device *dev = ring->dev;
2620e3adcf8fSFrançois Tigeot 	uint32_t cmd;
2621e3adcf8fSFrançois Tigeot 	int ret;
2622e3adcf8fSFrançois Tigeot 
2623a05eeebfSFrançois Tigeot 	ret = intel_ring_begin(req, 4);
2624e3adcf8fSFrançois Tigeot 	if (ret)
2625e3adcf8fSFrançois Tigeot 		return ret;
2626e3adcf8fSFrançois Tigeot 
2627e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
2628477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8)
26299edbd4a0SFrançois Tigeot 		cmd += 1;
26302c9916cdSFrançois Tigeot 
26312c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
26322c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
26332c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
26342c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
26352c9916cdSFrançois Tigeot 	 */
26362c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
26372c9916cdSFrançois Tigeot 
2638b5c29a34SFrançois Tigeot 	/*
2639b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.3 - blitter engine command streamer:
2640b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
2641b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
2642b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
2643b5c29a34SFrançois Tigeot 	 */
2644e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
26452c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB;
2646e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
2647b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
2648477eb7f9SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
26499edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* upper addr */
26509edbd4a0SFrançois Tigeot 		intel_ring_emit(ring, 0); /* value */
26519edbd4a0SFrançois Tigeot 	} else  {
2652e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, 0);
2653e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
26549edbd4a0SFrançois Tigeot 	}
2655e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
26565d0b1887SFrançois Tigeot 
2657e3adcf8fSFrançois Tigeot 	return 0;
2658e3adcf8fSFrançois Tigeot }
2659e3adcf8fSFrançois Tigeot 
2660e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
2661e3adcf8fSFrançois Tigeot {
2662ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2663ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
266424edb884SFrançois Tigeot 	struct drm_i915_gem_object *obj;
266524edb884SFrançois Tigeot 	int ret;
2666e3adcf8fSFrançois Tigeot 
2667686a02f1SFrançois Tigeot 	ring->name = "render ring";
2668686a02f1SFrançois Tigeot 	ring->id = RCS;
2669686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
2670686a02f1SFrançois Tigeot 
267124edb884SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
267224edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
267324edb884SFrançois Tigeot 			obj = i915_gem_alloc_object(dev, 4096);
267424edb884SFrançois Tigeot 			if (obj == NULL) {
267524edb884SFrançois Tigeot 				DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
267624edb884SFrançois Tigeot 				i915.semaphores = 0;
267724edb884SFrançois Tigeot 			} else {
267824edb884SFrançois Tigeot 				i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
267924edb884SFrançois Tigeot 				ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
268024edb884SFrançois Tigeot 				if (ret != 0) {
268124edb884SFrançois Tigeot 					drm_gem_object_unreference(&obj->base);
268224edb884SFrançois Tigeot 					DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
268324edb884SFrançois Tigeot 					i915.semaphores = 0;
268424edb884SFrançois Tigeot 				} else
268524edb884SFrançois Tigeot 					dev_priv->semaphore_obj = obj;
268624edb884SFrançois Tigeot 			}
268724edb884SFrançois Tigeot 		}
26882c9916cdSFrançois Tigeot 
26892c9916cdSFrançois Tigeot 		ring->init_context = intel_rcs_ctx_init;
269024edb884SFrançois Tigeot 		ring->add_request = gen6_add_request;
269124edb884SFrançois Tigeot 		ring->flush = gen8_render_ring_flush;
269224edb884SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
269324edb884SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
269424edb884SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
269524edb884SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
269624edb884SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
269724edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
269824edb884SFrançois Tigeot 			WARN_ON(!dev_priv->semaphore_obj);
269924edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
270024edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_rcs_signal;
270124edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
270224edb884SFrançois Tigeot 		}
270324edb884SFrançois Tigeot 	} else if (INTEL_INFO(dev)->gen >= 6) {
2704352ff8bdSFrançois Tigeot 		ring->init_context = intel_rcs_ctx_init;
2705e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
2706b5c29a34SFrançois Tigeot 		ring->flush = gen7_render_ring_flush;
2707b5c29a34SFrançois Tigeot 		if (INTEL_INFO(dev)->gen == 6)
2708e3adcf8fSFrançois Tigeot 			ring->flush = gen6_render_ring_flush;
2709686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2710686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
27115d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2712e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2713a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
271424edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2715ba55f2f5SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2716ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
2717ba55f2f5SFrançois Tigeot 			/*
271824edb884SFrançois Tigeot 			 * The current semaphore is only applied on pre-gen8
271924edb884SFrançois Tigeot 			 * platform.  And there is no VCS2 ring on the pre-gen8
272024edb884SFrançois Tigeot 			 * platform. So the semaphore between RCS and VCS2 is
272124edb884SFrançois Tigeot 			 * initialized as INVALID.  Gen8 will initialize the
272224edb884SFrançois Tigeot 			 * sema between VCS2 and RCS later.
2723ba55f2f5SFrançois Tigeot 			 */
2724ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2725ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2726ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2727ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2728ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2729ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2730ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2731ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2732ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2733ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
273424edb884SFrançois Tigeot 		}
2735e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
2736e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
2737686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
2738e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
2739a2fdbec6SFrançois Tigeot 		ring->set_seqno = pc_render_set_seqno;
2740686a02f1SFrançois Tigeot 		ring->irq_get = gen5_ring_get_irq;
2741686a02f1SFrançois Tigeot 		ring->irq_put = gen5_ring_put_irq;
27425d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
27435d0b1887SFrançois Tigeot 					GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
2744686a02f1SFrançois Tigeot 	} else {
2745686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2746686a02f1SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 4)
2747686a02f1SFrançois Tigeot 			ring->flush = gen2_render_ring_flush;
2748686a02f1SFrançois Tigeot 		else
2749686a02f1SFrançois Tigeot 			ring->flush = gen4_render_ring_flush;
2750686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2751a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2752686a02f1SFrançois Tigeot 		if (IS_GEN2(dev)) {
2753686a02f1SFrançois Tigeot 			ring->irq_get = i8xx_ring_get_irq;
2754686a02f1SFrançois Tigeot 			ring->irq_put = i8xx_ring_put_irq;
2755686a02f1SFrançois Tigeot 		} else {
2756686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2757686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2758e3adcf8fSFrançois Tigeot 		}
2759686a02f1SFrançois Tigeot 		ring->irq_enable_mask = I915_USER_INTERRUPT;
2760686a02f1SFrançois Tigeot 	}
2761686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
276224edb884SFrançois Tigeot 
2763b5c29a34SFrançois Tigeot 	if (IS_HASWELL(dev))
2764b5c29a34SFrançois Tigeot 		ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
27659edbd4a0SFrançois Tigeot 	else if (IS_GEN8(dev))
27669edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2767b5c29a34SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 6)
2768686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2769686a02f1SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 4)
2770686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2771686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
2772686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2773686a02f1SFrançois Tigeot 	else
2774686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
27752c9916cdSFrançois Tigeot 	ring->init_hw = init_render_ring;
2776686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
2777e3adcf8fSFrançois Tigeot 
2778b5c29a34SFrançois Tigeot 	/* Workaround batchbuffer to combat CS tlb bug. */
2779b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev)) {
278024edb884SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
2781b5c29a34SFrançois Tigeot 		if (obj == NULL) {
2782b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to allocate batch bo\n");
2783b5c29a34SFrançois Tigeot 			return -ENOMEM;
2784b5c29a34SFrançois Tigeot 		}
2785b5c29a34SFrançois Tigeot 
2786ba55f2f5SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
2787b5c29a34SFrançois Tigeot 		if (ret != 0) {
2788b5c29a34SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
2789b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to ping batch bo\n");
2790b5c29a34SFrançois Tigeot 			return ret;
2791b5c29a34SFrançois Tigeot 		}
2792b5c29a34SFrançois Tigeot 
27939edbd4a0SFrançois Tigeot 		ring->scratch.obj = obj;
27949edbd4a0SFrançois Tigeot 		ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
2795e3adcf8fSFrançois Tigeot 	}
2796e3adcf8fSFrançois Tigeot 
27972c9916cdSFrançois Tigeot 	ret = intel_init_ring_buffer(dev, ring);
2798b5c29a34SFrançois Tigeot 	if (ret)
27992c9916cdSFrançois Tigeot 		return ret;
28002c9916cdSFrançois Tigeot 
28012c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
28022c9916cdSFrançois Tigeot 		ret = intel_init_pipe_control(ring);
28032c9916cdSFrançois Tigeot 		if (ret)
28042c9916cdSFrançois Tigeot 			return ret;
2805b5c29a34SFrançois Tigeot 	}
2806b5c29a34SFrançois Tigeot 
2807e3adcf8fSFrançois Tigeot 	return 0;
2808e3adcf8fSFrançois Tigeot }
2809e3adcf8fSFrançois Tigeot 
2810e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
2811e3adcf8fSFrançois Tigeot {
2812ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2813ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS];
2814e3adcf8fSFrançois Tigeot 
2815686a02f1SFrançois Tigeot 	ring->name = "bsd ring";
2816686a02f1SFrançois Tigeot 	ring->id = VCS;
2817686a02f1SFrançois Tigeot 
2818686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
28199edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
2820686a02f1SFrançois Tigeot 		ring->mmio_base = GEN6_BSD_RING_BASE;
2821686a02f1SFrançois Tigeot 		/* gen6 bsd needs a special wa for tail updates */
2822686a02f1SFrançois Tigeot 		if (IS_GEN6(dev))
2823686a02f1SFrançois Tigeot 			ring->write_tail = gen6_bsd_ring_write_tail;
28245d0b1887SFrançois Tigeot 		ring->flush = gen6_bsd_ring_flush;
2825686a02f1SFrançois Tigeot 		ring->add_request = gen6_add_request;
2826686a02f1SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
2827a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
28289edbd4a0SFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 8) {
28299edbd4a0SFrançois Tigeot 			ring->irq_enable_mask =
28309edbd4a0SFrançois Tigeot 				GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
28319edbd4a0SFrançois Tigeot 			ring->irq_get = gen8_ring_get_irq;
28329edbd4a0SFrançois Tigeot 			ring->irq_put = gen8_ring_put_irq;
28339edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
28349edbd4a0SFrançois Tigeot 				gen8_ring_dispatch_execbuffer;
283524edb884SFrançois Tigeot 			if (i915_semaphore_is_enabled(dev)) {
283624edb884SFrançois Tigeot 				ring->semaphore.sync_to = gen8_ring_sync;
283724edb884SFrançois Tigeot 				ring->semaphore.signal = gen8_xcs_signal;
283824edb884SFrançois Tigeot 				GEN8_RING_SEMAPHORE_INIT;
283924edb884SFrançois Tigeot 			}
28409edbd4a0SFrançois Tigeot 		} else {
28415d0b1887SFrançois Tigeot 			ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2842686a02f1SFrançois Tigeot 			ring->irq_get = gen6_ring_get_irq;
2843686a02f1SFrançois Tigeot 			ring->irq_put = gen6_ring_put_irq;
28449edbd4a0SFrançois Tigeot 			ring->dispatch_execbuffer =
28459edbd4a0SFrançois Tigeot 				gen6_ring_dispatch_execbuffer;
284624edb884SFrançois Tigeot 			if (i915_semaphore_is_enabled(dev)) {
2847ba55f2f5SFrançois Tigeot 				ring->semaphore.sync_to = gen6_ring_sync;
2848ba55f2f5SFrançois Tigeot 				ring->semaphore.signal = gen6_signal;
2849ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2850ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2851ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2852ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2853ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2854ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2855ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2856ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2857ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2858ba55f2f5SFrançois Tigeot 				ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
285924edb884SFrançois Tigeot 			}
286024edb884SFrançois Tigeot 		}
2861686a02f1SFrançois Tigeot 	} else {
2862686a02f1SFrançois Tigeot 		ring->mmio_base = BSD_RING_BASE;
2863686a02f1SFrançois Tigeot 		ring->flush = bsd_ring_flush;
2864686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
2865686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
2866a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
2867686a02f1SFrançois Tigeot 		if (IS_GEN5(dev)) {
28685d0b1887SFrançois Tigeot 			ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2869686a02f1SFrançois Tigeot 			ring->irq_get = gen5_ring_get_irq;
2870686a02f1SFrançois Tigeot 			ring->irq_put = gen5_ring_put_irq;
2871686a02f1SFrançois Tigeot 		} else {
2872686a02f1SFrançois Tigeot 			ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2873686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
2874686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
2875686a02f1SFrançois Tigeot 		}
2876686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2877686a02f1SFrançois Tigeot 	}
28782c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2879e3adcf8fSFrançois Tigeot 
2880e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2881e3adcf8fSFrançois Tigeot }
2882e3adcf8fSFrançois Tigeot 
2883ba55f2f5SFrançois Tigeot /**
2884477eb7f9SFrançois Tigeot  * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
2885ba55f2f5SFrançois Tigeot  */
2886ba55f2f5SFrançois Tigeot int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2887ba55f2f5SFrançois Tigeot {
2888ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2889ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
2890ba55f2f5SFrançois Tigeot 
289124edb884SFrançois Tigeot 	ring->name = "bsd2 ring";
2892ba55f2f5SFrançois Tigeot 	ring->id = VCS2;
2893ba55f2f5SFrançois Tigeot 
2894ba55f2f5SFrançois Tigeot 	ring->write_tail = ring_write_tail;
2895ba55f2f5SFrançois Tigeot 	ring->mmio_base = GEN8_BSD2_RING_BASE;
2896ba55f2f5SFrançois Tigeot 	ring->flush = gen6_bsd_ring_flush;
2897ba55f2f5SFrançois Tigeot 	ring->add_request = gen6_add_request;
2898ba55f2f5SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2899ba55f2f5SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
2900ba55f2f5SFrançois Tigeot 	ring->irq_enable_mask =
2901ba55f2f5SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2902ba55f2f5SFrançois Tigeot 	ring->irq_get = gen8_ring_get_irq;
2903ba55f2f5SFrançois Tigeot 	ring->irq_put = gen8_ring_put_irq;
2904ba55f2f5SFrançois Tigeot 	ring->dispatch_execbuffer =
2905ba55f2f5SFrançois Tigeot 			gen8_ring_dispatch_execbuffer;
290624edb884SFrançois Tigeot 	if (i915_semaphore_is_enabled(dev)) {
290724edb884SFrançois Tigeot 		ring->semaphore.sync_to = gen8_ring_sync;
290824edb884SFrançois Tigeot 		ring->semaphore.signal = gen8_xcs_signal;
290924edb884SFrançois Tigeot 		GEN8_RING_SEMAPHORE_INIT;
291024edb884SFrançois Tigeot 	}
29112c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
2912ba55f2f5SFrançois Tigeot 
2913ba55f2f5SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
2914ba55f2f5SFrançois Tigeot }
2915ba55f2f5SFrançois Tigeot 
2916e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
2917e3adcf8fSFrançois Tigeot {
2918ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2919ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[BCS];
2920e3adcf8fSFrançois Tigeot 
2921686a02f1SFrançois Tigeot 	ring->name = "blitter ring";
2922686a02f1SFrançois Tigeot 	ring->id = BCS;
2923686a02f1SFrançois Tigeot 
2924686a02f1SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
2925686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
29265d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
2927686a02f1SFrançois Tigeot 	ring->add_request = gen6_add_request;
2928686a02f1SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
2929a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
29309edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
29319edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
29329edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
29339edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
29349edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
29359edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
293624edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
293724edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
293824edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_xcs_signal;
293924edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
294024edb884SFrançois Tigeot 		}
29419edbd4a0SFrançois Tigeot 	} else {
29425d0b1887SFrançois Tigeot 		ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2943686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
2944686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
2945686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
294624edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
2947ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
294824edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
2949ba55f2f5SFrançois Tigeot 			/*
295024edb884SFrançois Tigeot 			 * The current semaphore is only applied on pre-gen8
295124edb884SFrançois Tigeot 			 * platform.  And there is no VCS2 ring on the pre-gen8
295224edb884SFrançois Tigeot 			 * platform. So the semaphore between BCS and VCS2 is
295324edb884SFrançois Tigeot 			 * initialized as INVALID.  Gen8 will initialize the
295424edb884SFrançois Tigeot 			 * sema between BCS and VCS2 later.
2955ba55f2f5SFrançois Tigeot 			 */
2956ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2957ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2958ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2959ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2960ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2961ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2962ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2963ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2964ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2965ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
296624edb884SFrançois Tigeot 		}
296724edb884SFrançois Tigeot 	}
29682c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
29695d0b1887SFrançois Tigeot 
29705d0b1887SFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
29715d0b1887SFrançois Tigeot }
29725d0b1887SFrançois Tigeot 
29735d0b1887SFrançois Tigeot int intel_init_vebox_ring_buffer(struct drm_device *dev)
29745d0b1887SFrançois Tigeot {
2975ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2976ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VECS];
29775d0b1887SFrançois Tigeot 
29785d0b1887SFrançois Tigeot 	ring->name = "video enhancement ring";
29795d0b1887SFrançois Tigeot 	ring->id = VECS;
29805d0b1887SFrançois Tigeot 
29815d0b1887SFrançois Tigeot 	ring->mmio_base = VEBOX_RING_BASE;
29825d0b1887SFrançois Tigeot 	ring->write_tail = ring_write_tail;
29835d0b1887SFrançois Tigeot 	ring->flush = gen6_ring_flush;
29845d0b1887SFrançois Tigeot 	ring->add_request = gen6_add_request;
29855d0b1887SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
29865d0b1887SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
29879edbd4a0SFrançois Tigeot 
29889edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
29899edbd4a0SFrançois Tigeot 		ring->irq_enable_mask =
29909edbd4a0SFrançois Tigeot 			GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
29919edbd4a0SFrançois Tigeot 		ring->irq_get = gen8_ring_get_irq;
29929edbd4a0SFrançois Tigeot 		ring->irq_put = gen8_ring_put_irq;
29939edbd4a0SFrançois Tigeot 		ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
299424edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
299524edb884SFrançois Tigeot 			ring->semaphore.sync_to = gen8_ring_sync;
299624edb884SFrançois Tigeot 			ring->semaphore.signal = gen8_xcs_signal;
299724edb884SFrançois Tigeot 			GEN8_RING_SEMAPHORE_INIT;
299824edb884SFrançois Tigeot 		}
29999edbd4a0SFrançois Tigeot 	} else {
30009edbd4a0SFrançois Tigeot 		ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
30015d0b1887SFrançois Tigeot 		ring->irq_get = hsw_vebox_get_irq;
30025d0b1887SFrançois Tigeot 		ring->irq_put = hsw_vebox_put_irq;
30035d0b1887SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
300424edb884SFrançois Tigeot 		if (i915_semaphore_is_enabled(dev)) {
3005ba55f2f5SFrançois Tigeot 			ring->semaphore.sync_to = gen6_ring_sync;
3006ba55f2f5SFrançois Tigeot 			ring->semaphore.signal = gen6_signal;
3007ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
3008ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
3009ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
3010ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
3011ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
3012ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
3013ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
3014ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
3015ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
3016ba55f2f5SFrançois Tigeot 			ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
301724edb884SFrançois Tigeot 		}
301824edb884SFrançois Tigeot 	}
30192c9916cdSFrançois Tigeot 	ring->init_hw = init_ring_common;
3020e3adcf8fSFrançois Tigeot 
3021e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
3022e3adcf8fSFrançois Tigeot }
3023b030f26bSFrançois Tigeot 
3024b030f26bSFrançois Tigeot int
3025a05eeebfSFrançois Tigeot intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
3026b030f26bSFrançois Tigeot {
3027a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
3028b030f26bSFrançois Tigeot 	int ret;
3029b030f26bSFrançois Tigeot 
3030b030f26bSFrançois Tigeot 	if (!ring->gpu_caches_dirty)
3031b030f26bSFrançois Tigeot 		return 0;
3032b030f26bSFrançois Tigeot 
3033a05eeebfSFrançois Tigeot 	ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
3034b030f26bSFrançois Tigeot 	if (ret)
3035b030f26bSFrançois Tigeot 		return ret;
3036b030f26bSFrançois Tigeot 
3037a05eeebfSFrançois Tigeot 	trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
3038a2fdbec6SFrançois Tigeot 
3039b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
3040b030f26bSFrançois Tigeot 	return 0;
3041b030f26bSFrançois Tigeot }
3042b030f26bSFrançois Tigeot 
3043b030f26bSFrançois Tigeot int
3044a05eeebfSFrançois Tigeot intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
3045b030f26bSFrançois Tigeot {
3046a05eeebfSFrançois Tigeot 	struct intel_engine_cs *ring = req->ring;
3047b030f26bSFrançois Tigeot 	uint32_t flush_domains;
3048b030f26bSFrançois Tigeot 	int ret;
3049b030f26bSFrançois Tigeot 
3050b030f26bSFrançois Tigeot 	flush_domains = 0;
3051b030f26bSFrançois Tigeot 	if (ring->gpu_caches_dirty)
3052b030f26bSFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
3053b030f26bSFrançois Tigeot 
3054a05eeebfSFrançois Tigeot 	ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
3055b030f26bSFrançois Tigeot 	if (ret)
3056b030f26bSFrançois Tigeot 		return ret;
3057b030f26bSFrançois Tigeot 
3058a05eeebfSFrançois Tigeot 	trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
3059a2fdbec6SFrançois Tigeot 
3060b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
3061b030f26bSFrançois Tigeot 	return 0;
3062b030f26bSFrançois Tigeot }
3063ba55f2f5SFrançois Tigeot 
3064ba55f2f5SFrançois Tigeot void
3065ba55f2f5SFrançois Tigeot intel_stop_ring_buffer(struct intel_engine_cs *ring)
3066ba55f2f5SFrançois Tigeot {
3067ba55f2f5SFrançois Tigeot 	int ret;
3068ba55f2f5SFrançois Tigeot 
3069ba55f2f5SFrançois Tigeot 	if (!intel_ring_initialized(ring))
3070ba55f2f5SFrançois Tigeot 		return;
3071ba55f2f5SFrançois Tigeot 
3072ba55f2f5SFrançois Tigeot 	ret = intel_ring_idle(ring);
3073ba55f2f5SFrançois Tigeot 	if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
3074ba55f2f5SFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
3075ba55f2f5SFrançois Tigeot 			  ring->name, ret);
3076ba55f2f5SFrançois Tigeot 
3077ba55f2f5SFrançois Tigeot 	stop_ring(ring);
3078ba55f2f5SFrançois Tigeot }
3079