xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision e3440f963d168fa3ccb25daabd2beebd240133f5)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2008-2010 Intel Corporation
3e3adcf8fSFrançois Tigeot  *
4e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10e3adcf8fSFrançois Tigeot  *
11e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13e3adcf8fSFrançois Tigeot  * Software.
14e3adcf8fSFrançois Tigeot  *
15e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e3adcf8fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e3adcf8fSFrançois Tigeot  * IN THE SOFTWARE.
22e3adcf8fSFrançois Tigeot  *
23e3adcf8fSFrançois Tigeot  * Authors:
24e3adcf8fSFrançois Tigeot  *    Eric Anholt <eric@anholt.net>
25e3adcf8fSFrançois Tigeot  *    Zou Nan hai <nanhai.zou@intel.com>
26e3adcf8fSFrançois Tigeot  *    Xiang Hai hao<haihao.xiang@intel.com>
27e3adcf8fSFrançois Tigeot  *
28e3adcf8fSFrançois Tigeot  */
29e3adcf8fSFrançois Tigeot 
3018e26a6dSFrançois Tigeot #include <drm/drmP.h>
315c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
32e3adcf8fSFrançois Tigeot #include "i915_drv.h"
33e3adcf8fSFrançois Tigeot #include "intel_drv.h"
34e3adcf8fSFrançois Tigeot #include "intel_ringbuffer.h"
35e3adcf8fSFrançois Tigeot 
36e3adcf8fSFrançois Tigeot /*
37e3adcf8fSFrançois Tigeot  * 965+ support PIPE_CONTROL commands, which provide finer grained control
38e3adcf8fSFrançois Tigeot  * over cache flushing.
39e3adcf8fSFrançois Tigeot  */
40e3adcf8fSFrançois Tigeot struct pipe_control {
41e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
42e3adcf8fSFrançois Tigeot 	volatile u32 *cpu_page;
43e3adcf8fSFrançois Tigeot 	u32 gtt_offset;
44e3adcf8fSFrançois Tigeot };
45e3adcf8fSFrançois Tigeot 
46e3adcf8fSFrançois Tigeot static inline int ring_space(struct intel_ring_buffer *ring)
47e3adcf8fSFrançois Tigeot {
487cbd1a46SFrançois Tigeot 	int space = (ring->head & HEAD_ADDR) - (ring->tail + I915_RING_FREE_SPACE);
49e3adcf8fSFrançois Tigeot 	if (space < 0)
50e3adcf8fSFrançois Tigeot 		space += ring->size;
51e3adcf8fSFrançois Tigeot 	return space;
52e3adcf8fSFrançois Tigeot }
53e3adcf8fSFrançois Tigeot 
54e3adcf8fSFrançois Tigeot static int
55686a02f1SFrançois Tigeot gen2_render_ring_flush(struct intel_ring_buffer *ring,
56686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
57686a02f1SFrançois Tigeot 		       u32	flush_domains)
58686a02f1SFrançois Tigeot {
59686a02f1SFrançois Tigeot 	u32 cmd;
60686a02f1SFrançois Tigeot 	int ret;
61686a02f1SFrançois Tigeot 
62686a02f1SFrançois Tigeot 	cmd = MI_FLUSH;
63686a02f1SFrançois Tigeot 	if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
64686a02f1SFrançois Tigeot 		cmd |= MI_NO_WRITE_FLUSH;
65686a02f1SFrançois Tigeot 
66686a02f1SFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
67686a02f1SFrançois Tigeot 		cmd |= MI_READ_FLUSH;
68686a02f1SFrançois Tigeot 
69686a02f1SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
70686a02f1SFrançois Tigeot 	if (ret)
71686a02f1SFrançois Tigeot 		return ret;
72686a02f1SFrançois Tigeot 
73686a02f1SFrançois Tigeot 	intel_ring_emit(ring, cmd);
74686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
75686a02f1SFrançois Tigeot 	intel_ring_advance(ring);
76686a02f1SFrançois Tigeot 
77686a02f1SFrançois Tigeot 	return 0;
78686a02f1SFrançois Tigeot }
79686a02f1SFrançois Tigeot 
80686a02f1SFrançois Tigeot static int
81686a02f1SFrançois Tigeot gen4_render_ring_flush(struct intel_ring_buffer *ring,
82686a02f1SFrançois Tigeot 		       u32	invalidate_domains,
83686a02f1SFrançois Tigeot 		       u32	flush_domains)
84e3adcf8fSFrançois Tigeot {
85e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
86686a02f1SFrançois Tigeot 	u32 cmd;
87e3adcf8fSFrançois Tigeot 	int ret;
88e3adcf8fSFrançois Tigeot 
89e3adcf8fSFrançois Tigeot 	/*
90e3adcf8fSFrançois Tigeot 	 * read/write caches:
91e3adcf8fSFrançois Tigeot 	 *
92e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
93e3adcf8fSFrançois Tigeot 	 * only flushed if MI_NO_WRITE_FLUSH is unset.  On 965, it is
94e3adcf8fSFrançois Tigeot 	 * also flushed at 2d versus 3d pipeline switches.
95e3adcf8fSFrançois Tigeot 	 *
96e3adcf8fSFrançois Tigeot 	 * read-only caches:
97e3adcf8fSFrançois Tigeot 	 *
98e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
99e3adcf8fSFrançois Tigeot 	 * MI_READ_FLUSH is set, and is always flushed on 965.
100e3adcf8fSFrançois Tigeot 	 *
101e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_COMMAND may not exist?
102e3adcf8fSFrançois Tigeot 	 *
103e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
104e3adcf8fSFrançois Tigeot 	 * invalidated when MI_EXE_FLUSH is set.
105e3adcf8fSFrançois Tigeot 	 *
106e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
107e3adcf8fSFrançois Tigeot 	 * invalidated with every MI_FLUSH.
108e3adcf8fSFrançois Tigeot 	 *
109e3adcf8fSFrançois Tigeot 	 * TLBs:
110e3adcf8fSFrançois Tigeot 	 *
111e3adcf8fSFrançois Tigeot 	 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
112e3adcf8fSFrançois Tigeot 	 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
113e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
114e3adcf8fSFrançois Tigeot 	 * are flushed at any MI_FLUSH.
115e3adcf8fSFrançois Tigeot 	 */
116e3adcf8fSFrançois Tigeot 
117e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
118686a02f1SFrançois Tigeot 	if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
119e3adcf8fSFrançois Tigeot 		cmd &= ~MI_NO_WRITE_FLUSH;
120e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
121e3adcf8fSFrançois Tigeot 		cmd |= MI_EXE_FLUSH;
122e3adcf8fSFrançois Tigeot 
123e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
124e3adcf8fSFrançois Tigeot 	    (IS_G4X(dev) || IS_GEN5(dev)))
125e3adcf8fSFrançois Tigeot 		cmd |= MI_INVALIDATE_ISP;
126e3adcf8fSFrançois Tigeot 
127e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
128e3adcf8fSFrançois Tigeot 	if (ret)
129e3adcf8fSFrançois Tigeot 		return ret;
130e3adcf8fSFrançois Tigeot 
131e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
132e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
133e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
134e3adcf8fSFrançois Tigeot 
135e3adcf8fSFrançois Tigeot 	return 0;
136e3adcf8fSFrançois Tigeot }
137e3adcf8fSFrançois Tigeot 
138e3adcf8fSFrançois Tigeot /**
139e3adcf8fSFrançois Tigeot  * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
140e3adcf8fSFrançois Tigeot  * implementing two workarounds on gen6.  From section 1.4.7.1
141e3adcf8fSFrançois Tigeot  * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
142e3adcf8fSFrançois Tigeot  *
143e3adcf8fSFrançois Tigeot  * [DevSNB-C+{W/A}] Before any depth stall flush (including those
144e3adcf8fSFrançois Tigeot  * produced by non-pipelined state commands), software needs to first
145e3adcf8fSFrançois Tigeot  * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
146e3adcf8fSFrançois Tigeot  * 0.
147e3adcf8fSFrançois Tigeot  *
148e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
149e3adcf8fSFrançois Tigeot  * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
150e3adcf8fSFrançois Tigeot  *
151e3adcf8fSFrançois Tigeot  * And the workaround for these two requires this workaround first:
152e3adcf8fSFrançois Tigeot  *
153e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
154e3adcf8fSFrançois Tigeot  * BEFORE the pipe-control with a post-sync op and no write-cache
155e3adcf8fSFrançois Tigeot  * flushes.
156e3adcf8fSFrançois Tigeot  *
157e3adcf8fSFrançois Tigeot  * And this last workaround is tricky because of the requirements on
158e3adcf8fSFrançois Tigeot  * that bit.  From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
159e3adcf8fSFrançois Tigeot  * volume 2 part 1:
160e3adcf8fSFrançois Tigeot  *
161e3adcf8fSFrançois Tigeot  *     "1 of the following must also be set:
162e3adcf8fSFrançois Tigeot  *      - Render Target Cache Flush Enable ([12] of DW1)
163e3adcf8fSFrançois Tigeot  *      - Depth Cache Flush Enable ([0] of DW1)
164e3adcf8fSFrançois Tigeot  *      - Stall at Pixel Scoreboard ([1] of DW1)
165e3adcf8fSFrançois Tigeot  *      - Depth Stall ([13] of DW1)
166e3adcf8fSFrançois Tigeot  *      - Post-Sync Operation ([13] of DW1)
167e3adcf8fSFrançois Tigeot  *      - Notify Enable ([8] of DW1)"
168e3adcf8fSFrançois Tigeot  *
169e3adcf8fSFrançois Tigeot  * The cache flushes require the workaround flush that triggered this
170e3adcf8fSFrançois Tigeot  * one, so we can't use it.  Depth stall would trigger the same.
171e3adcf8fSFrançois Tigeot  * Post-sync nonzero is what triggered this second workaround, so we
172e3adcf8fSFrançois Tigeot  * can't use that one either.  Notify enable is IRQs, which aren't
173e3adcf8fSFrançois Tigeot  * really our business.  That leaves only stall at scoreboard.
174e3adcf8fSFrançois Tigeot  */
175e3adcf8fSFrançois Tigeot static int
176e3adcf8fSFrançois Tigeot intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
177e3adcf8fSFrançois Tigeot {
178e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
179e3adcf8fSFrançois Tigeot 	u32 scratch_addr = pc->gtt_offset + 128;
180e3adcf8fSFrançois Tigeot 	int ret;
181e3adcf8fSFrançois Tigeot 
182e3adcf8fSFrançois Tigeot 
183e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
184e3adcf8fSFrançois Tigeot 	if (ret)
185e3adcf8fSFrançois Tigeot 		return ret;
186e3adcf8fSFrançois Tigeot 
187e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
188e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
189e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_STALL_AT_SCOREBOARD);
190e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
191e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* low dword */
192e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* high dword */
193e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
194e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
195e3adcf8fSFrançois Tigeot 
196e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
197e3adcf8fSFrançois Tigeot 	if (ret)
198e3adcf8fSFrançois Tigeot 		return ret;
199e3adcf8fSFrançois Tigeot 
200e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
201e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
202e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
203e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
204e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
205e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
206e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
207e3adcf8fSFrançois Tigeot 
208e3adcf8fSFrançois Tigeot 	return 0;
209e3adcf8fSFrançois Tigeot }
210e3adcf8fSFrançois Tigeot 
211e3adcf8fSFrançois Tigeot static int
212e3adcf8fSFrançois Tigeot gen6_render_ring_flush(struct intel_ring_buffer *ring,
213e3adcf8fSFrançois Tigeot                          u32 invalidate_domains, u32 flush_domains)
214e3adcf8fSFrançois Tigeot {
215e3adcf8fSFrançois Tigeot 	u32 flags = 0;
216e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
217e3adcf8fSFrançois Tigeot 	u32 scratch_addr = pc->gtt_offset + 128;
218e3adcf8fSFrançois Tigeot 	int ret;
219e3adcf8fSFrançois Tigeot 
220e3adcf8fSFrançois Tigeot 	/* Force SNB workarounds for PIPE_CONTROL flushes */
221686a02f1SFrançois Tigeot 	ret = intel_emit_post_sync_nonzero_flush(ring);
222686a02f1SFrançois Tigeot 	if (ret)
223686a02f1SFrançois Tigeot 		return ret;
224e3adcf8fSFrançois Tigeot 
225e3adcf8fSFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
226e3adcf8fSFrançois Tigeot 	 * number of bits based on the write domains has little performance
227e3adcf8fSFrançois Tigeot 	 * impact.
228e3adcf8fSFrançois Tigeot 	 */
229b5c29a34SFrançois Tigeot 	if (flush_domains) {
230e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
231b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
232b5c29a34SFrançois Tigeot 		/*
233b5c29a34SFrançois Tigeot 		 * Ensure that any following seqno writes only happen
234b5c29a34SFrançois Tigeot 		 * when the render cache is indeed flushed.
235b5c29a34SFrançois Tigeot 		 */
236b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CS_STALL;
237b5c29a34SFrançois Tigeot 	}
238b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
239686a02f1SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
240e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
241e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
242e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
243e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
244e3adcf8fSFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
245686a02f1SFrançois Tigeot 		/*
246b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
247686a02f1SFrançois Tigeot 		 */
248b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
249b5c29a34SFrançois Tigeot 	}
250e3adcf8fSFrançois Tigeot 
251b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
252e3adcf8fSFrançois Tigeot 	if (ret)
253e3adcf8fSFrançois Tigeot 		return ret;
254e3adcf8fSFrançois Tigeot 
255b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
256e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, flags);
257e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
258b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
259b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
260b5c29a34SFrançois Tigeot 
261b5c29a34SFrançois Tigeot 	return 0;
262b5c29a34SFrançois Tigeot }
263b5c29a34SFrançois Tigeot 
264b5c29a34SFrançois Tigeot static int
265b5c29a34SFrançois Tigeot gen7_render_ring_cs_stall_wa(struct intel_ring_buffer *ring)
266b5c29a34SFrançois Tigeot {
267b5c29a34SFrançois Tigeot 	int ret;
268b5c29a34SFrançois Tigeot 
269b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
270b5c29a34SFrançois Tigeot 	if (ret)
271b5c29a34SFrançois Tigeot 		return ret;
272b5c29a34SFrançois Tigeot 
273b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
274b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
275b5c29a34SFrançois Tigeot 			      PIPE_CONTROL_STALL_AT_SCOREBOARD);
276b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
277b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
278b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
279b5c29a34SFrançois Tigeot 
280b5c29a34SFrançois Tigeot 	return 0;
281b5c29a34SFrançois Tigeot }
282b5c29a34SFrançois Tigeot 
283b5c29a34SFrançois Tigeot static int
284b5c29a34SFrançois Tigeot gen7_render_ring_flush(struct intel_ring_buffer *ring,
285b5c29a34SFrançois Tigeot 		       u32 invalidate_domains, u32 flush_domains)
286b5c29a34SFrançois Tigeot {
287b5c29a34SFrançois Tigeot 	u32 flags = 0;
288b5c29a34SFrançois Tigeot 	struct pipe_control *pc = ring->private;
289b5c29a34SFrançois Tigeot 	u32 scratch_addr = pc->gtt_offset + 128;
290b5c29a34SFrançois Tigeot 	int ret;
291b5c29a34SFrançois Tigeot 
292b5c29a34SFrançois Tigeot 	/*
293b5c29a34SFrançois Tigeot 	 * Ensure that any following seqno writes only happen when the render
294b5c29a34SFrançois Tigeot 	 * cache is indeed flushed.
295b5c29a34SFrançois Tigeot 	 *
296b5c29a34SFrançois Tigeot 	 * Workaround: 4th PIPE_CONTROL command (except the ones with only
297b5c29a34SFrançois Tigeot 	 * read-cache invalidate bits set) must have the CS_STALL bit set. We
298b5c29a34SFrançois Tigeot 	 * don't try to be clever and just set it unconditionally.
299b5c29a34SFrançois Tigeot 	 */
300b5c29a34SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
301b5c29a34SFrançois Tigeot 
302b5c29a34SFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
303b5c29a34SFrançois Tigeot 	 * number of bits based on the write domains has little performance
304b5c29a34SFrançois Tigeot 	 * impact.
305b5c29a34SFrançois Tigeot 	 */
306b5c29a34SFrançois Tigeot 	if (flush_domains) {
307b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
308b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
309b5c29a34SFrançois Tigeot 	}
310b5c29a34SFrançois Tigeot 	if (invalidate_domains) {
311b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
312b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
313b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
314b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
315b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
316b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
317b5c29a34SFrançois Tigeot 		/*
318b5c29a34SFrançois Tigeot 		 * TLB invalidate requires a post-sync write.
319b5c29a34SFrançois Tigeot 		 */
320b5c29a34SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
321b5c29a34SFrançois Tigeot 
322b5c29a34SFrançois Tigeot 		/* Workaround: we must issue a pipe_control with CS-stall bit
323b5c29a34SFrançois Tigeot 		 * set before a pipe_control command that has the state cache
324b5c29a34SFrançois Tigeot 		 * invalidate bit set. */
325b5c29a34SFrançois Tigeot 		gen7_render_ring_cs_stall_wa(ring);
326b5c29a34SFrançois Tigeot 	}
327b5c29a34SFrançois Tigeot 
328b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
329b5c29a34SFrançois Tigeot 	if (ret)
330b5c29a34SFrançois Tigeot 		return ret;
331b5c29a34SFrançois Tigeot 
332b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
333b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, flags);
334b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
335b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
336e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
337e3adcf8fSFrançois Tigeot 
338e3adcf8fSFrançois Tigeot 	return 0;
339e3adcf8fSFrançois Tigeot }
340e3adcf8fSFrançois Tigeot 
341e3adcf8fSFrançois Tigeot static void ring_write_tail(struct intel_ring_buffer *ring,
342b5c29a34SFrançois Tigeot 			    u32 value)
343e3adcf8fSFrançois Tigeot {
344e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
345e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
346e3adcf8fSFrançois Tigeot }
347e3adcf8fSFrançois Tigeot 
348e3adcf8fSFrançois Tigeot u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
349e3adcf8fSFrançois Tigeot {
350e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
351b5c29a34SFrançois Tigeot 	u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
352e3adcf8fSFrançois Tigeot 			RING_ACTHD(ring->mmio_base) : ACTHD;
353e3adcf8fSFrançois Tigeot 
354e3adcf8fSFrançois Tigeot 	return I915_READ(acthd_reg);
355e3adcf8fSFrançois Tigeot }
356e3adcf8fSFrançois Tigeot 
357e3adcf8fSFrançois Tigeot static int init_ring_common(struct intel_ring_buffer *ring)
358e3adcf8fSFrançois Tigeot {
359686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
360686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
361e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj = ring->obj;
362686a02f1SFrançois Tigeot 	int ret = 0;
363b5c29a34SFrançois Tigeot 	u32 head;
364e3adcf8fSFrançois Tigeot 
365686a02f1SFrançois Tigeot 	if (HAS_FORCE_WAKE(dev))
366686a02f1SFrançois Tigeot 		gen6_gt_force_wake_get(dev_priv);
367686a02f1SFrançois Tigeot 
368e3adcf8fSFrançois Tigeot 	/* Stop the ring if it's running. */
369e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
370e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
371e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
372e3adcf8fSFrançois Tigeot 
373e3adcf8fSFrançois Tigeot 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
374e3adcf8fSFrançois Tigeot 
375e3adcf8fSFrançois Tigeot 	/* G45 ring initialization fails to reset head to zero */
376e3adcf8fSFrançois Tigeot 	if (head != 0) {
377b5c29a34SFrançois Tigeot 		DRM_DEBUG_KMS("%s head not reset to zero "
378e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
379e3adcf8fSFrançois Tigeot 			      ring->name,
380e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
381e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
382e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
383e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
384e3adcf8fSFrançois Tigeot 
385e3adcf8fSFrançois Tigeot 		I915_WRITE_HEAD(ring, 0);
386e3adcf8fSFrançois Tigeot 
387e3adcf8fSFrançois Tigeot 		if (I915_READ_HEAD(ring) & HEAD_ADDR) {
388e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
389e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
390e3adcf8fSFrançois Tigeot 				  ring->name,
391e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
392e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
393e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
394e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
395e3adcf8fSFrançois Tigeot 		}
396e3adcf8fSFrançois Tigeot 	}
397e3adcf8fSFrançois Tigeot 
398b5c29a34SFrançois Tigeot 	/* Initialize the ring. This must happen _after_ we've cleared the ring
399b5c29a34SFrançois Tigeot 	 * registers with the above sequence (the readback of the HEAD registers
400b5c29a34SFrançois Tigeot 	 * also enforces ordering), otherwise the hw might lose the new ring
401b5c29a34SFrançois Tigeot 	 * register values. */
402b5c29a34SFrançois Tigeot 	I915_WRITE_START(ring, obj->gtt_offset);
403e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring,
404e3adcf8fSFrançois Tigeot 			((ring->size - PAGE_SIZE) & RING_NR_PAGES)
405e3adcf8fSFrançois Tigeot 			| RING_VALID);
406e3adcf8fSFrançois Tigeot 
407e3adcf8fSFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
408b5c29a34SFrançois Tigeot 	if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
409e3adcf8fSFrançois Tigeot 		     I915_READ_START(ring) == obj->gtt_offset &&
410b5c29a34SFrançois Tigeot 		     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
411e3adcf8fSFrançois Tigeot 		DRM_ERROR("%s initialization failed "
412e3adcf8fSFrançois Tigeot 				"ctl %08x head %08x tail %08x start %08x\n",
413e3adcf8fSFrançois Tigeot 				ring->name,
414e3adcf8fSFrançois Tigeot 				I915_READ_CTL(ring),
415e3adcf8fSFrançois Tigeot 				I915_READ_HEAD(ring),
416e3adcf8fSFrançois Tigeot 				I915_READ_TAIL(ring),
417e3adcf8fSFrançois Tigeot 				I915_READ_START(ring));
418686a02f1SFrançois Tigeot 		ret = -EIO;
419686a02f1SFrançois Tigeot 		goto out;
420e3adcf8fSFrançois Tigeot 	}
421e3adcf8fSFrançois Tigeot 
422e3adcf8fSFrançois Tigeot 	if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
423e3adcf8fSFrançois Tigeot 		i915_kernel_lost_context(ring->dev);
424e3adcf8fSFrançois Tigeot 	else {
425e3adcf8fSFrançois Tigeot 		ring->head = I915_READ_HEAD(ring);
426e3adcf8fSFrançois Tigeot 		ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
427e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
428686a02f1SFrançois Tigeot 		ring->last_retired_head = -1;
429e3adcf8fSFrançois Tigeot 	}
430e3adcf8fSFrançois Tigeot 
431686a02f1SFrançois Tigeot out:
432686a02f1SFrançois Tigeot 	if (HAS_FORCE_WAKE(dev))
433686a02f1SFrançois Tigeot 		gen6_gt_force_wake_put(dev_priv);
434686a02f1SFrançois Tigeot 
435686a02f1SFrançois Tigeot 	return ret;
436e3adcf8fSFrançois Tigeot }
437e3adcf8fSFrançois Tigeot 
438e3adcf8fSFrançois Tigeot static int
439e3adcf8fSFrançois Tigeot init_pipe_control(struct intel_ring_buffer *ring)
440e3adcf8fSFrançois Tigeot {
441e3adcf8fSFrançois Tigeot 	struct pipe_control *pc;
442e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
443e3adcf8fSFrançois Tigeot 	int ret;
444e3adcf8fSFrançois Tigeot 
445e3adcf8fSFrançois Tigeot 	if (ring->private)
446e3adcf8fSFrançois Tigeot 		return 0;
447e3adcf8fSFrançois Tigeot 
4485a3b77d5SFrançois Tigeot 	pc = kmalloc(sizeof(*pc), M_DRM, M_WAITOK);
449e3adcf8fSFrançois Tigeot 	if (!pc)
450e3adcf8fSFrançois Tigeot 		return -ENOMEM;
451e3adcf8fSFrançois Tigeot 
452e3adcf8fSFrançois Tigeot 	obj = i915_gem_alloc_object(ring->dev, 4096);
453e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
454e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
455e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
456e3adcf8fSFrançois Tigeot 		goto err;
457e3adcf8fSFrançois Tigeot 	}
458e3adcf8fSFrançois Tigeot 
459e3adcf8fSFrançois Tigeot 	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
460e3adcf8fSFrançois Tigeot 
461b00bc81cSFrançois Tigeot 	ret = i915_gem_object_pin(obj, 4096, true, false);
462e3adcf8fSFrançois Tigeot 	if (ret)
463e3adcf8fSFrançois Tigeot 		goto err_unref;
464e3adcf8fSFrançois Tigeot 
465e3adcf8fSFrançois Tigeot 	pc->gtt_offset = obj->gtt_offset;
466e3adcf8fSFrançois Tigeot 	pc->cpu_page = (uint32_t *)kmem_alloc_nofault(&kernel_map, PAGE_SIZE, PAGE_SIZE);
467e3adcf8fSFrançois Tigeot 	if (pc->cpu_page == NULL)
468e3adcf8fSFrançois Tigeot 		goto err_unpin;
469e3adcf8fSFrançois Tigeot 	pmap_qenter((uintptr_t)pc->cpu_page, &obj->pages[0], 1);
470e3adcf8fSFrançois Tigeot 	pmap_invalidate_cache_range((vm_offset_t)pc->cpu_page,
471e3adcf8fSFrançois Tigeot 	    (vm_offset_t)pc->cpu_page + PAGE_SIZE);
472e3adcf8fSFrançois Tigeot 
473e3adcf8fSFrançois Tigeot 	pc->obj = obj;
474e3adcf8fSFrançois Tigeot 	ring->private = pc;
475e3adcf8fSFrançois Tigeot 	return 0;
476e3adcf8fSFrançois Tigeot 
477e3adcf8fSFrançois Tigeot err_unpin:
478e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
479e3adcf8fSFrançois Tigeot err_unref:
480e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
481e3adcf8fSFrançois Tigeot err:
482*e3440f96SFrançois Tigeot 	kfree(pc, M_DRM);
483e3adcf8fSFrançois Tigeot 	return ret;
484e3adcf8fSFrançois Tigeot }
485e3adcf8fSFrançois Tigeot 
486e3adcf8fSFrançois Tigeot static void
487e3adcf8fSFrançois Tigeot cleanup_pipe_control(struct intel_ring_buffer *ring)
488e3adcf8fSFrançois Tigeot {
489e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
490e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
491e3adcf8fSFrançois Tigeot 
492e3adcf8fSFrançois Tigeot 	if (!ring->private)
493e3adcf8fSFrançois Tigeot 		return;
494e3adcf8fSFrançois Tigeot 
495e3adcf8fSFrançois Tigeot 	obj = pc->obj;
496e3adcf8fSFrançois Tigeot 	pmap_qremove((vm_offset_t)pc->cpu_page, 1);
497e3adcf8fSFrançois Tigeot 	kmem_free(&kernel_map, (uintptr_t)pc->cpu_page, PAGE_SIZE);
498e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
499e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
500e3adcf8fSFrançois Tigeot 
501*e3440f96SFrançois Tigeot 	kfree(pc, M_DRM);
502e3adcf8fSFrançois Tigeot 	ring->private = NULL;
503e3adcf8fSFrançois Tigeot }
504e3adcf8fSFrançois Tigeot 
505e3adcf8fSFrançois Tigeot static int init_render_ring(struct intel_ring_buffer *ring)
506e3adcf8fSFrançois Tigeot {
507e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
508e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
509e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
510e3adcf8fSFrançois Tigeot 
511f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen > 3)
512f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
513f4e1c372SFrançois Tigeot 
514f4e1c372SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
515f4e1c372SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
516f4e1c372SFrançois Tigeot 	 * programmed to '1' on all products.
517f4e1c372SFrançois Tigeot 	 */
518f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
519f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
520f4e1c372SFrançois Tigeot 
521f4e1c372SFrançois Tigeot 	/* Required for the hardware to program scanline values for waiting */
522f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6)
523f4e1c372SFrançois Tigeot 		I915_WRITE(GFX_MODE,
524f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_ALWAYS));
525f4e1c372SFrançois Tigeot 
526e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev))
527e3adcf8fSFrançois Tigeot 		I915_WRITE(GFX_MODE_GEN7,
528f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
529f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
530e3adcf8fSFrançois Tigeot 
531e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
532e3adcf8fSFrançois Tigeot 		ret = init_pipe_control(ring);
533e3adcf8fSFrançois Tigeot 		if (ret)
534e3adcf8fSFrançois Tigeot 			return ret;
535e3adcf8fSFrançois Tigeot 	}
536e3adcf8fSFrançois Tigeot 
537e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
538e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
539e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
540e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
541e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
542e3adcf8fSFrançois Tigeot 		 */
543e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
544f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
545686a02f1SFrançois Tigeot 
546686a02f1SFrançois Tigeot 		/* This is not explicitly set for GEN6, so read the register.
547686a02f1SFrançois Tigeot 		 * see intel_ring_mi_set_context() for why we care.
548686a02f1SFrançois Tigeot 		 * TODO: consider explicitly setting the bit for GEN5
549686a02f1SFrançois Tigeot 		 */
550686a02f1SFrançois Tigeot 		ring->itlb_before_ctx_switch =
551686a02f1SFrançois Tigeot 			!!(I915_READ(GFX_MODE) & GFX_TLB_INVALIDATE_ALWAYS);
552e3adcf8fSFrançois Tigeot 	}
553e3adcf8fSFrançois Tigeot 
554f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
555f4e1c372SFrançois Tigeot 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
556f4e1c372SFrançois Tigeot 
557f4e1c372SFrançois Tigeot 	if (HAS_L3_GPU_CACHE(dev))
558f4e1c372SFrançois Tigeot 		I915_WRITE_IMR(ring, ~GEN6_RENDER_L3_PARITY_ERROR);
559e3adcf8fSFrançois Tigeot 
560e3adcf8fSFrançois Tigeot 	return ret;
561e3adcf8fSFrançois Tigeot }
562e3adcf8fSFrançois Tigeot 
563e3adcf8fSFrançois Tigeot static void render_ring_cleanup(struct intel_ring_buffer *ring)
564e3adcf8fSFrançois Tigeot {
565b5c29a34SFrançois Tigeot 	struct drm_device *dev = ring->dev;
566b5c29a34SFrançois Tigeot 
567e3adcf8fSFrançois Tigeot 	if (!ring->private)
568e3adcf8fSFrançois Tigeot 		return;
569e3adcf8fSFrançois Tigeot 
570b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev))
571b5c29a34SFrançois Tigeot 		drm_gem_object_unreference(to_gem_object(ring->private));
572b5c29a34SFrançois Tigeot 
573e3adcf8fSFrançois Tigeot 	cleanup_pipe_control(ring);
574e3adcf8fSFrançois Tigeot }
575e3adcf8fSFrançois Tigeot 
576e3adcf8fSFrançois Tigeot static void
577e3adcf8fSFrançois Tigeot update_mboxes(struct intel_ring_buffer *ring,
578e3adcf8fSFrançois Tigeot 	      u32 mmio_offset)
579e3adcf8fSFrançois Tigeot {
580b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
581e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, mmio_offset);
582b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
583e3adcf8fSFrançois Tigeot }
584e3adcf8fSFrançois Tigeot 
585e3adcf8fSFrançois Tigeot /**
586e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
587e3adcf8fSFrançois Tigeot  *
588e3adcf8fSFrançois Tigeot  * @ring - ring that is adding a request
589e3adcf8fSFrançois Tigeot  * @seqno - return seqno stuck into the ring
590e3adcf8fSFrançois Tigeot  *
591e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
592e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
593e3adcf8fSFrançois Tigeot  */
594e3adcf8fSFrançois Tigeot static int
595b5c29a34SFrançois Tigeot gen6_add_request(struct intel_ring_buffer *ring)
596e3adcf8fSFrançois Tigeot {
597e3adcf8fSFrançois Tigeot 	u32 mbox1_reg;
598e3adcf8fSFrançois Tigeot 	u32 mbox2_reg;
599e3adcf8fSFrançois Tigeot 	int ret;
600e3adcf8fSFrançois Tigeot 
601e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 10);
602e3adcf8fSFrançois Tigeot 	if (ret)
603e3adcf8fSFrançois Tigeot 		return ret;
604e3adcf8fSFrançois Tigeot 
605e3adcf8fSFrançois Tigeot 	mbox1_reg = ring->signal_mbox[0];
606e3adcf8fSFrançois Tigeot 	mbox2_reg = ring->signal_mbox[1];
607e3adcf8fSFrançois Tigeot 
608b5c29a34SFrançois Tigeot 	update_mboxes(ring, mbox1_reg);
609b5c29a34SFrançois Tigeot 	update_mboxes(ring, mbox2_reg);
610e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
611e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
612b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
613e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
614e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
615e3adcf8fSFrançois Tigeot 
616e3adcf8fSFrançois Tigeot 	return 0;
617e3adcf8fSFrançois Tigeot }
618e3adcf8fSFrançois Tigeot 
619e3adcf8fSFrançois Tigeot /**
620e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
621e3adcf8fSFrançois Tigeot  *
622e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
623e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
624e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
625e3adcf8fSFrançois Tigeot  */
626e3adcf8fSFrançois Tigeot static int
627686a02f1SFrançois Tigeot gen6_ring_sync(struct intel_ring_buffer *waiter,
628e3adcf8fSFrançois Tigeot 	       struct intel_ring_buffer *signaller,
629e3adcf8fSFrançois Tigeot 	       u32 seqno)
630e3adcf8fSFrançois Tigeot {
631e3adcf8fSFrançois Tigeot 	int ret;
632e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
633e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
634e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
635e3adcf8fSFrançois Tigeot 
636686a02f1SFrançois Tigeot 	/* Throughout all of the GEM code, seqno passed implies our current
637686a02f1SFrançois Tigeot 	 * seqno is >= the last seqno executed. However for hardware the
638686a02f1SFrançois Tigeot 	 * comparison is strictly greater than.
639686a02f1SFrançois Tigeot 	 */
640686a02f1SFrançois Tigeot 	seqno -= 1;
641686a02f1SFrançois Tigeot 
642686a02f1SFrançois Tigeot 	WARN_ON(signaller->semaphore_register[waiter->id] ==
643686a02f1SFrançois Tigeot 		MI_SEMAPHORE_SYNC_INVALID);
644686a02f1SFrançois Tigeot 
645e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
646e3adcf8fSFrançois Tigeot 	if (ret)
647e3adcf8fSFrançois Tigeot 		return ret;
648e3adcf8fSFrançois Tigeot 
649686a02f1SFrançois Tigeot 	intel_ring_emit(waiter,
650686a02f1SFrançois Tigeot 			dw1 | signaller->semaphore_register[waiter->id]);
651e3adcf8fSFrançois Tigeot 	intel_ring_emit(waiter, seqno);
652e3adcf8fSFrançois Tigeot 	intel_ring_emit(waiter, 0);
653e3adcf8fSFrançois Tigeot 	intel_ring_emit(waiter, MI_NOOP);
654e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
655e3adcf8fSFrançois Tigeot 
656e3adcf8fSFrançois Tigeot 	return 0;
657e3adcf8fSFrançois Tigeot }
658e3adcf8fSFrançois Tigeot 
659e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
660e3adcf8fSFrançois Tigeot do {									\
661e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
662e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
663e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
664e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
665e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
666e3adcf8fSFrançois Tigeot } while (0)
667e3adcf8fSFrançois Tigeot 
668e3adcf8fSFrançois Tigeot static int
669b5c29a34SFrançois Tigeot pc_render_add_request(struct intel_ring_buffer *ring)
670e3adcf8fSFrançois Tigeot {
671e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
672e3adcf8fSFrançois Tigeot 	u32 scratch_addr = pc->gtt_offset + 128;
673e3adcf8fSFrançois Tigeot 	int ret;
674e3adcf8fSFrançois Tigeot 
675e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
676e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
677e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
678e3adcf8fSFrançois Tigeot 	 *
679e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
680e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
681e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
682e3adcf8fSFrançois Tigeot 	 */
683e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 32);
684e3adcf8fSFrançois Tigeot 	if (ret)
685e3adcf8fSFrançois Tigeot 		return ret;
686e3adcf8fSFrançois Tigeot 
687e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
688e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
689e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
690e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
691b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
692e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
693e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
694e3adcf8fSFrançois Tigeot 	scratch_addr += 128; /* write to separate cachelines */
695e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
696e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
697e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
698e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
699e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
700e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
701e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
702e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
703e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
704b5c29a34SFrançois Tigeot 
705e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
706e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
707e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
708e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
709e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
710b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
711e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
712e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
713e3adcf8fSFrançois Tigeot 
714e3adcf8fSFrançois Tigeot 	return 0;
715e3adcf8fSFrançois Tigeot }
716e3adcf8fSFrançois Tigeot 
717e3adcf8fSFrançois Tigeot static u32
718b030f26bSFrançois Tigeot gen6_ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
719e3adcf8fSFrançois Tigeot {
720e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
721e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
722e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
723b030f26bSFrançois Tigeot 	if (!lazy_coherency)
724e3adcf8fSFrançois Tigeot 		intel_ring_get_active_head(ring);
725e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
726e3adcf8fSFrançois Tigeot }
727e3adcf8fSFrançois Tigeot 
728b030f26bSFrançois Tigeot static u32
729b030f26bSFrançois Tigeot ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
730e3adcf8fSFrançois Tigeot {
731e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
732e3adcf8fSFrançois Tigeot }
733e3adcf8fSFrançois Tigeot 
734b030f26bSFrançois Tigeot static u32
735b030f26bSFrançois Tigeot pc_render_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
736e3adcf8fSFrançois Tigeot {
737e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
738e3adcf8fSFrançois Tigeot 	return pc->cpu_page[0];
739e3adcf8fSFrançois Tigeot }
740e3adcf8fSFrançois Tigeot 
741e3adcf8fSFrançois Tigeot static bool
742686a02f1SFrançois Tigeot gen5_ring_get_irq(struct intel_ring_buffer *ring)
743e3adcf8fSFrançois Tigeot {
744e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
745e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
746e3adcf8fSFrançois Tigeot 
747e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
748e3adcf8fSFrançois Tigeot 		return false;
749e3adcf8fSFrançois Tigeot 
75002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
751e3adcf8fSFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
752686a02f1SFrançois Tigeot 		dev_priv->gt_irq_mask &= ~ring->irq_enable_mask;
753686a02f1SFrançois Tigeot 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
754686a02f1SFrançois Tigeot 		POSTING_READ(GTIMR);
755e3adcf8fSFrançois Tigeot 	}
75602727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
757e3adcf8fSFrançois Tigeot 
758e3adcf8fSFrançois Tigeot 	return true;
759e3adcf8fSFrançois Tigeot }
760e3adcf8fSFrançois Tigeot 
761e3adcf8fSFrançois Tigeot static void
762686a02f1SFrançois Tigeot gen5_ring_put_irq(struct intel_ring_buffer *ring)
763e3adcf8fSFrançois Tigeot {
764e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
765e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
766e3adcf8fSFrançois Tigeot 
76702727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
768e3adcf8fSFrançois Tigeot 	if (--ring->irq_refcount == 0) {
769686a02f1SFrançois Tigeot 		dev_priv->gt_irq_mask |= ring->irq_enable_mask;
770686a02f1SFrançois Tigeot 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
771686a02f1SFrançois Tigeot 		POSTING_READ(GTIMR);
772686a02f1SFrançois Tigeot 	}
773686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
774686a02f1SFrançois Tigeot }
775686a02f1SFrançois Tigeot 
776686a02f1SFrançois Tigeot static bool
777686a02f1SFrançois Tigeot i9xx_ring_get_irq(struct intel_ring_buffer *ring)
778686a02f1SFrançois Tigeot {
779686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
780686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
781686a02f1SFrançois Tigeot 
782686a02f1SFrançois Tigeot 	if (!dev->irq_enabled)
783686a02f1SFrançois Tigeot 		return false;
784686a02f1SFrançois Tigeot 
785686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
786686a02f1SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
787686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
788686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
789686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
790686a02f1SFrançois Tigeot 	}
791686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
792686a02f1SFrançois Tigeot 
793686a02f1SFrançois Tigeot 	return true;
794686a02f1SFrançois Tigeot }
795686a02f1SFrançois Tigeot 
796686a02f1SFrançois Tigeot static void
797686a02f1SFrançois Tigeot i9xx_ring_put_irq(struct intel_ring_buffer *ring)
798686a02f1SFrançois Tigeot {
799686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
800686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
801686a02f1SFrançois Tigeot 
802686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
803686a02f1SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
804686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
805686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
806686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
807686a02f1SFrançois Tigeot 	}
808686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
809686a02f1SFrançois Tigeot }
810686a02f1SFrançois Tigeot 
811686a02f1SFrançois Tigeot static bool
812686a02f1SFrançois Tigeot i8xx_ring_get_irq(struct intel_ring_buffer *ring)
813686a02f1SFrançois Tigeot {
814686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
815686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
816686a02f1SFrançois Tigeot 
817686a02f1SFrançois Tigeot 	if (!dev->irq_enabled)
818686a02f1SFrançois Tigeot 		return false;
819686a02f1SFrançois Tigeot 
820686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
821686a02f1SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
822686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
823686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
824686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
825686a02f1SFrançois Tigeot 	}
826686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
827686a02f1SFrançois Tigeot 
828686a02f1SFrançois Tigeot 	return true;
829686a02f1SFrançois Tigeot }
830686a02f1SFrançois Tigeot 
831686a02f1SFrançois Tigeot static void
832686a02f1SFrançois Tigeot i8xx_ring_put_irq(struct intel_ring_buffer *ring)
833686a02f1SFrançois Tigeot {
834686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
835686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
836686a02f1SFrançois Tigeot 
837686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
838686a02f1SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
839686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
840686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
841686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
842e3adcf8fSFrançois Tigeot 	}
84302727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
844e3adcf8fSFrançois Tigeot }
845e3adcf8fSFrançois Tigeot 
846e3adcf8fSFrançois Tigeot void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
847e3adcf8fSFrançois Tigeot {
848e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
849b5c29a34SFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
850b5c29a34SFrançois Tigeot 	u32 mmio = 0;
851e3adcf8fSFrançois Tigeot 
852e3adcf8fSFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
853e3adcf8fSFrançois Tigeot 	 * the ring registers as of gen7.
854e3adcf8fSFrançois Tigeot 	 */
855e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev)) {
856e3adcf8fSFrançois Tigeot 		switch (ring->id) {
857e3adcf8fSFrançois Tigeot 		case RCS:
858e3adcf8fSFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
859e3adcf8fSFrançois Tigeot 			break;
860e3adcf8fSFrançois Tigeot 		case BCS:
861e3adcf8fSFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
862e3adcf8fSFrançois Tigeot 			break;
863e3adcf8fSFrançois Tigeot 		case VCS:
864e3adcf8fSFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
865e3adcf8fSFrançois Tigeot 			break;
866e3adcf8fSFrançois Tigeot 		}
867b5c29a34SFrançois Tigeot 	} else if (IS_GEN6(ring->dev)) {
868e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
869e3adcf8fSFrançois Tigeot 	} else {
870e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
871e3adcf8fSFrançois Tigeot 	}
872e3adcf8fSFrançois Tigeot 
873e3adcf8fSFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
874e3adcf8fSFrançois Tigeot 	POSTING_READ(mmio);
875e3adcf8fSFrançois Tigeot }
876e3adcf8fSFrançois Tigeot 
877e3adcf8fSFrançois Tigeot static int
878e3adcf8fSFrançois Tigeot bsd_ring_flush(struct intel_ring_buffer *ring,
879b5c29a34SFrançois Tigeot 	       u32     invalidate_domains,
880b5c29a34SFrançois Tigeot 	       u32     flush_domains)
881e3adcf8fSFrançois Tigeot {
882e3adcf8fSFrançois Tigeot 	int ret;
883e3adcf8fSFrançois Tigeot 
884e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
885e3adcf8fSFrançois Tigeot 	if (ret)
886e3adcf8fSFrançois Tigeot 		return ret;
887e3adcf8fSFrançois Tigeot 
888e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
889e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
890e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
891e3adcf8fSFrançois Tigeot 	return 0;
892e3adcf8fSFrançois Tigeot }
893e3adcf8fSFrançois Tigeot 
894e3adcf8fSFrançois Tigeot static int
895b5c29a34SFrançois Tigeot i9xx_add_request(struct intel_ring_buffer *ring)
896e3adcf8fSFrançois Tigeot {
897e3adcf8fSFrançois Tigeot 	int ret;
898e3adcf8fSFrançois Tigeot 
899e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
900e3adcf8fSFrançois Tigeot 	if (ret)
901e3adcf8fSFrançois Tigeot 		return ret;
902e3adcf8fSFrançois Tigeot 
903e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
904e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
905b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
906e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
907e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
908e3adcf8fSFrançois Tigeot 
909e3adcf8fSFrançois Tigeot 	return 0;
910e3adcf8fSFrançois Tigeot }
911e3adcf8fSFrançois Tigeot 
912e3adcf8fSFrançois Tigeot static bool
913686a02f1SFrançois Tigeot gen6_ring_get_irq(struct intel_ring_buffer *ring)
914e3adcf8fSFrançois Tigeot {
915e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
916e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
917e3adcf8fSFrançois Tigeot 
918e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
919e3adcf8fSFrançois Tigeot 	       return false;
920e3adcf8fSFrançois Tigeot 
921686a02f1SFrançois Tigeot 	/* It looks like we need to prevent the gt from suspending while waiting
922686a02f1SFrançois Tigeot 	 * for an notifiy irq, otherwise irqs seem to get lost on at least the
923686a02f1SFrançois Tigeot 	 * blt/bsd rings on ivb. */
924e3adcf8fSFrançois Tigeot 	gen6_gt_force_wake_get(dev_priv);
925e3adcf8fSFrançois Tigeot 
92602727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
927e3adcf8fSFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
928686a02f1SFrançois Tigeot 		if (HAS_L3_GPU_CACHE(dev) && ring->id == RCS)
929686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~(ring->irq_enable_mask |
930686a02f1SFrançois Tigeot 						GEN6_RENDER_L3_PARITY_ERROR));
931686a02f1SFrançois Tigeot 		else
932686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
933686a02f1SFrançois Tigeot 		dev_priv->gt_irq_mask &= ~ring->irq_enable_mask;
934686a02f1SFrançois Tigeot 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
935686a02f1SFrançois Tigeot 		POSTING_READ(GTIMR);
936e3adcf8fSFrançois Tigeot 	}
93702727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
938e3adcf8fSFrançois Tigeot 
939e3adcf8fSFrançois Tigeot 	return true;
940e3adcf8fSFrançois Tigeot }
941e3adcf8fSFrançois Tigeot 
942e3adcf8fSFrançois Tigeot static void
943686a02f1SFrançois Tigeot gen6_ring_put_irq(struct intel_ring_buffer *ring)
944e3adcf8fSFrançois Tigeot {
945e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
946e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
947e3adcf8fSFrançois Tigeot 
94802727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
949e3adcf8fSFrançois Tigeot 	if (--ring->irq_refcount == 0) {
950686a02f1SFrançois Tigeot 		if (HAS_L3_GPU_CACHE(dev) && ring->id == RCS)
951686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~GEN6_RENDER_L3_PARITY_ERROR);
952686a02f1SFrançois Tigeot 		else
953686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
954686a02f1SFrançois Tigeot 		dev_priv->gt_irq_mask |= ring->irq_enable_mask;
955686a02f1SFrançois Tigeot 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
956686a02f1SFrançois Tigeot 		POSTING_READ(GTIMR);
957e3adcf8fSFrançois Tigeot 	}
95802727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
959e3adcf8fSFrançois Tigeot 
960e3adcf8fSFrançois Tigeot 	gen6_gt_force_wake_put(dev_priv);
961e3adcf8fSFrançois Tigeot }
962e3adcf8fSFrançois Tigeot 
963e3adcf8fSFrançois Tigeot static int
964b5c29a34SFrançois Tigeot i965_dispatch_execbuffer(struct intel_ring_buffer *ring,
965b5c29a34SFrançois Tigeot 			 u32 offset, u32 length,
966b5c29a34SFrançois Tigeot 			 unsigned flags)
967e3adcf8fSFrançois Tigeot {
968e3adcf8fSFrançois Tigeot 	int ret;
969e3adcf8fSFrançois Tigeot 
970e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
971e3adcf8fSFrançois Tigeot 	if (ret)
972e3adcf8fSFrançois Tigeot 		return ret;
973e3adcf8fSFrançois Tigeot 
974e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
975686a02f1SFrançois Tigeot 			MI_BATCH_BUFFER_START |
976b5c29a34SFrançois Tigeot 			MI_BATCH_GTT |
977b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
978e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
979e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
980e3adcf8fSFrançois Tigeot 
981e3adcf8fSFrançois Tigeot 	return 0;
982e3adcf8fSFrançois Tigeot }
983e3adcf8fSFrançois Tigeot 
984b5c29a34SFrançois Tigeot /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
985b5c29a34SFrançois Tigeot #define I830_BATCH_LIMIT (256*1024)
986e3adcf8fSFrançois Tigeot static int
987686a02f1SFrançois Tigeot i830_dispatch_execbuffer(struct intel_ring_buffer *ring,
988b5c29a34SFrançois Tigeot 				u32 offset, u32 len,
989b5c29a34SFrançois Tigeot 				unsigned flags)
990e3adcf8fSFrançois Tigeot {
991e3adcf8fSFrançois Tigeot 	int ret;
992e3adcf8fSFrançois Tigeot 
993b5c29a34SFrançois Tigeot 	if (flags & I915_DISPATCH_PINNED) {
994e3adcf8fSFrançois Tigeot 		ret = intel_ring_begin(ring, 4);
995e3adcf8fSFrançois Tigeot 		if (ret)
996e3adcf8fSFrançois Tigeot 			return ret;
997e3adcf8fSFrançois Tigeot 
998e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
999b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1000e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, offset + len - 8);
1001b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
1002686a02f1SFrançois Tigeot 		intel_ring_advance(ring);
1003b5c29a34SFrançois Tigeot 	} else {
1004b5c29a34SFrançois Tigeot 		struct drm_i915_gem_object *obj = ring->private;
1005b5c29a34SFrançois Tigeot 		u32 cs_offset = obj->gtt_offset;
1006b5c29a34SFrançois Tigeot 
1007b5c29a34SFrançois Tigeot 		if (len > I830_BATCH_LIMIT)
1008b5c29a34SFrançois Tigeot 			return -ENOSPC;
1009b5c29a34SFrançois Tigeot 
1010b5c29a34SFrançois Tigeot 		ret = intel_ring_begin(ring, 9+3);
1011b5c29a34SFrançois Tigeot 		if (ret)
1012b5c29a34SFrançois Tigeot 			return ret;
1013b5c29a34SFrançois Tigeot 		/* Blit the batch (which has now all relocs applied) to the stable batch
1014b5c29a34SFrançois Tigeot 		 * scratch bo area (so that the CS never stumbles over its tlb
1015b5c29a34SFrançois Tigeot 		 * invalidation bug) ... */
1016b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
1017b5c29a34SFrançois Tigeot 				XY_SRC_COPY_BLT_WRITE_ALPHA |
1018b5c29a34SFrançois Tigeot 				XY_SRC_COPY_BLT_WRITE_RGB);
1019b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
1020b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 0);
1021b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
1022b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset);
1023b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 0);
1024b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 4096);
1025b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, offset);
1026b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_FLUSH);
1027b5c29a34SFrançois Tigeot 
1028b5c29a34SFrançois Tigeot 		/* ... and execute it. */
1029b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
1030b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1031b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset + len - 8);
1032b5c29a34SFrançois Tigeot 		intel_ring_advance(ring);
1033b5c29a34SFrançois Tigeot 	}
1034686a02f1SFrançois Tigeot 
1035686a02f1SFrançois Tigeot 	return 0;
1036686a02f1SFrançois Tigeot }
1037686a02f1SFrançois Tigeot 
1038686a02f1SFrançois Tigeot static int
1039686a02f1SFrançois Tigeot i915_dispatch_execbuffer(struct intel_ring_buffer *ring,
1040b5c29a34SFrançois Tigeot 			 u32 offset, u32 len,
1041b5c29a34SFrançois Tigeot 			 unsigned flags)
1042686a02f1SFrançois Tigeot {
1043686a02f1SFrançois Tigeot 	int ret;
1044686a02f1SFrançois Tigeot 
1045e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1046e3adcf8fSFrançois Tigeot 	if (ret)
1047e3adcf8fSFrançois Tigeot 		return ret;
1048e3adcf8fSFrançois Tigeot 
1049686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1050686a02f1SFrançois Tigeot 	intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1051e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1052e3adcf8fSFrançois Tigeot 
1053e3adcf8fSFrançois Tigeot 	return 0;
1054e3adcf8fSFrançois Tigeot }
1055e3adcf8fSFrançois Tigeot 
1056e3adcf8fSFrançois Tigeot static void cleanup_status_page(struct intel_ring_buffer *ring)
1057e3adcf8fSFrançois Tigeot {
1058e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1059e3adcf8fSFrançois Tigeot 
1060e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
1061e3adcf8fSFrançois Tigeot 	if (obj == NULL)
1062e3adcf8fSFrançois Tigeot 		return;
1063e3adcf8fSFrançois Tigeot 
1064e3adcf8fSFrançois Tigeot 	pmap_qremove((vm_offset_t)ring->status_page.page_addr, 1);
1065e3adcf8fSFrançois Tigeot 	kmem_free(&kernel_map, (vm_offset_t)ring->status_page.page_addr,
1066e3adcf8fSFrançois Tigeot 	    PAGE_SIZE);
1067e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
1068e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1069e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
1070e3adcf8fSFrançois Tigeot }
1071e3adcf8fSFrançois Tigeot 
1072e3adcf8fSFrançois Tigeot static int init_status_page(struct intel_ring_buffer *ring)
1073e3adcf8fSFrançois Tigeot {
1074e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1075e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1076e3adcf8fSFrançois Tigeot 	int ret;
1077e3adcf8fSFrançois Tigeot 
1078e3adcf8fSFrançois Tigeot 	obj = i915_gem_alloc_object(dev, 4096);
1079e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
1080e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate status page\n");
1081e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
1082e3adcf8fSFrançois Tigeot 		goto err;
1083e3adcf8fSFrançois Tigeot 	}
1084e3adcf8fSFrançois Tigeot 
1085e3adcf8fSFrançois Tigeot 	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1086e3adcf8fSFrançois Tigeot 
1087b00bc81cSFrançois Tigeot 	ret = i915_gem_object_pin(obj, 4096, true, false);
1088e3adcf8fSFrançois Tigeot 	if (ret != 0) {
1089e3adcf8fSFrançois Tigeot 		goto err_unref;
1090e3adcf8fSFrançois Tigeot 	}
1091e3adcf8fSFrançois Tigeot 
1092e3adcf8fSFrançois Tigeot 	ring->status_page.gfx_addr = obj->gtt_offset;
1093e3adcf8fSFrançois Tigeot 	ring->status_page.page_addr = (void *)kmem_alloc_nofault(&kernel_map,
1094e3adcf8fSFrançois Tigeot 	    PAGE_SIZE, PAGE_SIZE);
1095e3adcf8fSFrançois Tigeot 	if (ring->status_page.page_addr == NULL) {
1096686a02f1SFrançois Tigeot 		ret = -ENOMEM;
1097e3adcf8fSFrançois Tigeot 		goto err_unpin;
1098e3adcf8fSFrançois Tigeot 	}
1099e3adcf8fSFrançois Tigeot 	pmap_qenter((vm_offset_t)ring->status_page.page_addr, &obj->pages[0],
1100e3adcf8fSFrançois Tigeot 	    1);
1101e3adcf8fSFrançois Tigeot 	pmap_invalidate_cache_range((vm_offset_t)ring->status_page.page_addr,
1102e3adcf8fSFrançois Tigeot 	    (vm_offset_t)ring->status_page.page_addr + PAGE_SIZE);
1103e3adcf8fSFrançois Tigeot 	ring->status_page.obj = obj;
1104e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1105e3adcf8fSFrançois Tigeot 
1106e3adcf8fSFrançois Tigeot 	intel_ring_setup_status_page(ring);
1107b5c29a34SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1108e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1109e3adcf8fSFrançois Tigeot 
1110e3adcf8fSFrançois Tigeot 	return 0;
1111e3adcf8fSFrançois Tigeot 
1112e3adcf8fSFrançois Tigeot err_unpin:
1113e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
1114e3adcf8fSFrançois Tigeot err_unref:
1115e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1116e3adcf8fSFrançois Tigeot err:
1117e3adcf8fSFrançois Tigeot 	return ret;
1118e3adcf8fSFrançois Tigeot }
1119e3adcf8fSFrançois Tigeot 
1120686a02f1SFrançois Tigeot static int init_phys_hws_pga(struct intel_ring_buffer *ring)
1121686a02f1SFrançois Tigeot {
1122686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1123686a02f1SFrançois Tigeot 	u32 addr;
1124686a02f1SFrançois Tigeot 
1125686a02f1SFrançois Tigeot 	if (!dev_priv->status_page_dmah) {
1126686a02f1SFrançois Tigeot 		dev_priv->status_page_dmah =
1127686a02f1SFrançois Tigeot 			drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE, ~0);
1128686a02f1SFrançois Tigeot 		if (!dev_priv->status_page_dmah)
1129686a02f1SFrançois Tigeot 			return -ENOMEM;
1130686a02f1SFrançois Tigeot 	}
1131686a02f1SFrançois Tigeot 
1132686a02f1SFrançois Tigeot 	addr = dev_priv->status_page_dmah->busaddr;
1133686a02f1SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 4)
1134686a02f1SFrançois Tigeot 		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
1135686a02f1SFrançois Tigeot 	I915_WRITE(HWS_PGA, addr);
1136686a02f1SFrançois Tigeot 
1137686a02f1SFrançois Tigeot 	ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1138686a02f1SFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1139686a02f1SFrançois Tigeot 
1140686a02f1SFrançois Tigeot 	return 0;
1141686a02f1SFrançois Tigeot }
1142686a02f1SFrançois Tigeot 
1143686a02f1SFrançois Tigeot static inline void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
1144686a02f1SFrançois Tigeot {
1145686a02f1SFrançois Tigeot 	return pmap_mapdev_attr(phys_addr, size, VM_MEMATTR_WRITE_COMBINING);
1146686a02f1SFrançois Tigeot }
1147686a02f1SFrançois Tigeot 
1148b030f26bSFrançois Tigeot static int intel_init_ring_buffer(struct drm_device *dev,
1149e3adcf8fSFrançois Tigeot 				  struct intel_ring_buffer *ring)
1150e3adcf8fSFrançois Tigeot {
1151e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1152e3adcf8fSFrançois Tigeot 	int ret;
1153e3adcf8fSFrançois Tigeot 
1154e3adcf8fSFrançois Tigeot 	ring->dev = dev;
1155e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1156e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
1157686a02f1SFrançois Tigeot 	ring->size = 32 * PAGE_SIZE;
1158686a02f1SFrançois Tigeot 	memset(ring->sync_seqno, 0, sizeof(ring->sync_seqno));
1159e3adcf8fSFrançois Tigeot 
1160b030f26bSFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
1161b030f26bSFrançois Tigeot 
1162e3adcf8fSFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
1163e3adcf8fSFrançois Tigeot 		ret = init_status_page(ring);
1164e3adcf8fSFrançois Tigeot 		if (ret)
1165e3adcf8fSFrançois Tigeot 			return ret;
1166686a02f1SFrançois Tigeot 	} else {
1167686a02f1SFrançois Tigeot 		BUG_ON(ring->id != RCS);
1168686a02f1SFrançois Tigeot 		ret = init_phys_hws_pga(ring);
1169686a02f1SFrançois Tigeot 		if (ret)
1170686a02f1SFrançois Tigeot 			return ret;
1171e3adcf8fSFrançois Tigeot 	}
1172e3adcf8fSFrançois Tigeot 
1173e3adcf8fSFrançois Tigeot 	obj = i915_gem_alloc_object(dev, ring->size);
1174e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
1175e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate ringbuffer\n");
1176e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
1177e3adcf8fSFrançois Tigeot 		goto err_hws;
1178e3adcf8fSFrançois Tigeot 	}
1179e3adcf8fSFrançois Tigeot 
1180e3adcf8fSFrançois Tigeot 	ring->obj = obj;
1181e3adcf8fSFrançois Tigeot 
1182b00bc81cSFrançois Tigeot 	ret = i915_gem_object_pin(obj, PAGE_SIZE, true, false);
1183e3adcf8fSFrançois Tigeot 	if (ret)
1184e3adcf8fSFrançois Tigeot 		goto err_unref;
1185e3adcf8fSFrançois Tigeot 
1186686a02f1SFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
1187686a02f1SFrançois Tigeot 	if (ret)
1188686a02f1SFrançois Tigeot 		goto err_unpin;
1189e3adcf8fSFrançois Tigeot 
1190686a02f1SFrançois Tigeot 	ring->virtual_start = ioremap_wc(dev->agp->base + obj->gtt_offset,
1191686a02f1SFrançois Tigeot 					 ring->size);
1192686a02f1SFrançois Tigeot 	if (ring->virtual_start == NULL) {
1193e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to map ringbuffer.\n");
1194e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1195e3adcf8fSFrançois Tigeot 		goto err_unpin;
1196e3adcf8fSFrançois Tigeot 	}
1197e3adcf8fSFrançois Tigeot 
1198e3adcf8fSFrançois Tigeot 	ret = ring->init(ring);
1199e3adcf8fSFrançois Tigeot 	if (ret)
1200e3adcf8fSFrançois Tigeot 		goto err_unmap;
1201e3adcf8fSFrançois Tigeot 
1202e3adcf8fSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
1203e3adcf8fSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
1204e3adcf8fSFrançois Tigeot 	 * of the buffer.
1205e3adcf8fSFrançois Tigeot 	 */
1206e3adcf8fSFrançois Tigeot 	ring->effective_size = ring->size;
1207e3adcf8fSFrançois Tigeot 	if (IS_I830(ring->dev) || IS_845G(ring->dev))
1208e3adcf8fSFrançois Tigeot 		ring->effective_size -= 128;
1209e3adcf8fSFrançois Tigeot 
1210e3adcf8fSFrançois Tigeot 	return 0;
1211e3adcf8fSFrançois Tigeot 
1212e3adcf8fSFrançois Tigeot err_unmap:
1213686a02f1SFrançois Tigeot 	pmap_unmapdev((vm_offset_t)ring->virtual_start, ring->size);
1214e3adcf8fSFrançois Tigeot err_unpin:
1215e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
1216e3adcf8fSFrançois Tigeot err_unref:
1217e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1218e3adcf8fSFrançois Tigeot 	ring->obj = NULL;
1219e3adcf8fSFrançois Tigeot err_hws:
1220e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1221e3adcf8fSFrançois Tigeot 	return ret;
1222e3adcf8fSFrançois Tigeot }
1223e3adcf8fSFrançois Tigeot 
1224e3adcf8fSFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
1225e3adcf8fSFrançois Tigeot {
1226e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv;
1227e3adcf8fSFrançois Tigeot 	int ret;
1228e3adcf8fSFrançois Tigeot 
1229e3adcf8fSFrançois Tigeot 	if (ring->obj == NULL)
1230e3adcf8fSFrançois Tigeot 		return;
1231e3adcf8fSFrançois Tigeot 
1232e3adcf8fSFrançois Tigeot 	/* Disable the ring buffer. The ring must be idle at this point */
1233e3adcf8fSFrançois Tigeot 	dev_priv = ring->dev->dev_private;
1234b030f26bSFrançois Tigeot 	ret = intel_ring_idle(ring);
1235b030f26bSFrançois Tigeot 	if (ret)
1236b030f26bSFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
1237b030f26bSFrançois Tigeot 			  ring->name, ret);
1238b030f26bSFrançois Tigeot 
1239e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
1240e3adcf8fSFrançois Tigeot 
1241686a02f1SFrançois Tigeot 	pmap_unmapdev((vm_offset_t)ring->virtual_start, ring->size);
1242e3adcf8fSFrançois Tigeot 
1243e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(ring->obj);
1244e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&ring->obj->base);
1245e3adcf8fSFrançois Tigeot 	ring->obj = NULL;
1246e3adcf8fSFrançois Tigeot 
1247e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
1248e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
1249e3adcf8fSFrançois Tigeot 
1250e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1251e3adcf8fSFrançois Tigeot }
1252e3adcf8fSFrançois Tigeot 
1253e3adcf8fSFrançois Tigeot static int intel_ring_wait_seqno(struct intel_ring_buffer *ring, u32 seqno)
1254e3adcf8fSFrançois Tigeot {
1255e3adcf8fSFrançois Tigeot 	int ret;
1256e3adcf8fSFrançois Tigeot 
1257b030f26bSFrançois Tigeot 	ret = i915_wait_seqno(ring, seqno);
1258b030f26bSFrançois Tigeot 	if (!ret)
1259b030f26bSFrançois Tigeot 		i915_gem_retire_requests_ring(ring);
1260e3adcf8fSFrançois Tigeot 
1261e3adcf8fSFrançois Tigeot 	return ret;
1262e3adcf8fSFrançois Tigeot }
1263e3adcf8fSFrançois Tigeot 
1264e3adcf8fSFrançois Tigeot static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
1265e3adcf8fSFrançois Tigeot {
1266e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
1267e3adcf8fSFrançois Tigeot 	u32 seqno = 0;
1268e3adcf8fSFrançois Tigeot 	int ret;
1269e3adcf8fSFrançois Tigeot 
1270e3adcf8fSFrançois Tigeot 	i915_gem_retire_requests_ring(ring);
1271e3adcf8fSFrançois Tigeot 
1272e3adcf8fSFrançois Tigeot 	if (ring->last_retired_head != -1) {
1273e3adcf8fSFrançois Tigeot 		ring->head = ring->last_retired_head;
1274e3adcf8fSFrançois Tigeot 		ring->last_retired_head = -1;
1275e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
1276e3adcf8fSFrançois Tigeot 		if (ring->space >= n)
1277e3adcf8fSFrançois Tigeot 			return 0;
1278e3adcf8fSFrançois Tigeot 	}
1279e3adcf8fSFrançois Tigeot 
1280e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
1281e3adcf8fSFrançois Tigeot 		int space;
1282e3adcf8fSFrançois Tigeot 
1283e3adcf8fSFrançois Tigeot 		if (request->tail == -1)
1284e3adcf8fSFrançois Tigeot 			continue;
1285e3adcf8fSFrançois Tigeot 
1286b5c29a34SFrançois Tigeot 		space = request->tail - (ring->tail + I915_RING_FREE_SPACE);
1287e3adcf8fSFrançois Tigeot 		if (space < 0)
1288e3adcf8fSFrançois Tigeot 			space += ring->size;
1289e3adcf8fSFrançois Tigeot 		if (space >= n) {
1290e3adcf8fSFrançois Tigeot 			seqno = request->seqno;
1291e3adcf8fSFrançois Tigeot 			break;
1292e3adcf8fSFrançois Tigeot 		}
1293e3adcf8fSFrançois Tigeot 
1294e3adcf8fSFrançois Tigeot 		/* Consume this request in case we need more space than
1295e3adcf8fSFrançois Tigeot 		 * is available and so need to prevent a race between
1296e3adcf8fSFrançois Tigeot 		 * updating last_retired_head and direct reads of
1297e3adcf8fSFrançois Tigeot 		 * I915_RING_HEAD. It also provides a nice sanity check.
1298e3adcf8fSFrançois Tigeot 		 */
1299e3adcf8fSFrançois Tigeot 		request->tail = -1;
1300e3adcf8fSFrançois Tigeot 	}
1301e3adcf8fSFrançois Tigeot 
1302e3adcf8fSFrançois Tigeot 	if (seqno == 0)
1303e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1304e3adcf8fSFrançois Tigeot 
1305e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_seqno(ring, seqno);
1306e3adcf8fSFrançois Tigeot 	if (ret)
1307e3adcf8fSFrançois Tigeot 		return ret;
1308e3adcf8fSFrançois Tigeot 
1309b5c29a34SFrançois Tigeot 	if (WARN_ON(ring->last_retired_head == -1))
1310e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1311e3adcf8fSFrançois Tigeot 
1312e3adcf8fSFrançois Tigeot 	ring->head = ring->last_retired_head;
1313e3adcf8fSFrançois Tigeot 	ring->last_retired_head = -1;
1314e3adcf8fSFrançois Tigeot 	ring->space = ring_space(ring);
1315b5c29a34SFrançois Tigeot 	if (WARN_ON(ring->space < n))
1316e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1317e3adcf8fSFrançois Tigeot 
1318e3adcf8fSFrançois Tigeot 	return 0;
1319e3adcf8fSFrançois Tigeot }
1320e3adcf8fSFrançois Tigeot 
1321b030f26bSFrançois Tigeot static int ring_wait_for_space(struct intel_ring_buffer *ring, int n)
1322e3adcf8fSFrançois Tigeot {
1323e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1324e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1325245593daSFrançois Tigeot 	unsigned long end;
1326e3adcf8fSFrançois Tigeot 	int ret;
1327e3adcf8fSFrançois Tigeot 
1328e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_request(ring, n);
1329e3adcf8fSFrançois Tigeot 	if (ret != -ENOSPC)
1330e3adcf8fSFrançois Tigeot 		return ret;
1331e3adcf8fSFrançois Tigeot 
1332e3adcf8fSFrançois Tigeot 	/* With GEM the hangcheck timer should kick us out of the loop,
1333e3adcf8fSFrançois Tigeot 	 * leaving it early runs the risk of corrupting GEM state (due
1334e3adcf8fSFrançois Tigeot 	 * to running on almost untested codepaths). But on resume
1335e3adcf8fSFrançois Tigeot 	 * timers don't work yet, so prevent a complete hang in that
1336e3adcf8fSFrançois Tigeot 	 * case by choosing an insanely large timeout. */
1337*e3440f96SFrançois Tigeot 	end = jiffies + 60 * HZ;
1338245593daSFrançois Tigeot 
1339e3adcf8fSFrançois Tigeot 	do {
1340e3adcf8fSFrançois Tigeot 		ring->head = I915_READ_HEAD(ring);
1341e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
1342e3adcf8fSFrançois Tigeot 		if (ring->space >= n) {
1343e3adcf8fSFrançois Tigeot 			return 0;
1344e3adcf8fSFrançois Tigeot 		}
1345e3adcf8fSFrançois Tigeot 
1346e3adcf8fSFrançois Tigeot #if 0
1347e3adcf8fSFrançois Tigeot 		if (dev->primary->master) {
1348e3adcf8fSFrançois Tigeot 			struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1349e3adcf8fSFrançois Tigeot 			if (master_priv->sarea_priv)
1350e3adcf8fSFrançois Tigeot 				master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1351e3adcf8fSFrançois Tigeot 		}
1352e3adcf8fSFrançois Tigeot #else
1353e3adcf8fSFrançois Tigeot 		if (dev_priv->sarea_priv)
1354e3adcf8fSFrançois Tigeot 			dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1355e3adcf8fSFrançois Tigeot #endif
1356e3adcf8fSFrançois Tigeot 
1357*e3440f96SFrançois Tigeot 		msleep(1);
1358245593daSFrançois Tigeot 
1359245593daSFrançois Tigeot 		ret = i915_gem_check_wedge(dev_priv, dev_priv->mm.interruptible);
1360245593daSFrançois Tigeot 		if (ret)
1361245593daSFrançois Tigeot 			return ret;
1362*e3440f96SFrançois Tigeot 	} while (!time_after(jiffies, end));
1363e3adcf8fSFrançois Tigeot 	return -EBUSY;
1364e3adcf8fSFrançois Tigeot }
1365e3adcf8fSFrançois Tigeot 
1366b030f26bSFrançois Tigeot static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
1367b030f26bSFrançois Tigeot {
1368b030f26bSFrançois Tigeot 	uint32_t __iomem *virt;
1369b030f26bSFrançois Tigeot 	int rem = ring->size - ring->tail;
1370b030f26bSFrançois Tigeot 
1371b030f26bSFrançois Tigeot 	if (ring->space < rem) {
1372b030f26bSFrançois Tigeot 		int ret = ring_wait_for_space(ring, rem);
1373b030f26bSFrançois Tigeot 		if (ret)
1374b030f26bSFrançois Tigeot 			return ret;
1375b030f26bSFrançois Tigeot 	}
1376b030f26bSFrançois Tigeot 
1377b030f26bSFrançois Tigeot 	virt = (unsigned int *)((char *)ring->virtual_start + ring->tail);
1378b030f26bSFrançois Tigeot 	rem /= 4;
1379b030f26bSFrançois Tigeot 	while (rem--)
1380686a02f1SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
1381b030f26bSFrançois Tigeot 
1382b030f26bSFrançois Tigeot 	ring->tail = 0;
1383b030f26bSFrançois Tigeot 	ring->space = ring_space(ring);
1384b030f26bSFrançois Tigeot 
1385b030f26bSFrançois Tigeot 	return 0;
1386b030f26bSFrançois Tigeot }
1387b030f26bSFrançois Tigeot 
1388b030f26bSFrançois Tigeot int intel_ring_idle(struct intel_ring_buffer *ring)
1389b030f26bSFrançois Tigeot {
1390b5c29a34SFrançois Tigeot 	u32 seqno;
1391b5c29a34SFrançois Tigeot 	int ret;
1392b5c29a34SFrançois Tigeot 
1393b5c29a34SFrançois Tigeot 	/* We need to add any requests required to flush the objects and ring */
1394b5c29a34SFrançois Tigeot 	if (ring->outstanding_lazy_request) {
1395b5c29a34SFrançois Tigeot 		ret = i915_add_request(ring, NULL, NULL);
1396b5c29a34SFrançois Tigeot 		if (ret)
1397b5c29a34SFrançois Tigeot 			return ret;
1398b5c29a34SFrançois Tigeot 	}
1399b5c29a34SFrançois Tigeot 
1400b5c29a34SFrançois Tigeot 	/* Wait upon the last request to be completed */
1401b5c29a34SFrançois Tigeot 	if (list_empty(&ring->request_list))
1402b5c29a34SFrançois Tigeot 		return 0;
1403b5c29a34SFrançois Tigeot 
1404b5c29a34SFrançois Tigeot 	seqno = list_entry(ring->request_list.prev,
1405b5c29a34SFrançois Tigeot 			   struct drm_i915_gem_request,
1406b5c29a34SFrançois Tigeot 			   list)->seqno;
1407b5c29a34SFrançois Tigeot 
1408b5c29a34SFrançois Tigeot 	return i915_wait_seqno(ring, seqno);
1409b5c29a34SFrançois Tigeot }
1410b5c29a34SFrançois Tigeot 
1411b5c29a34SFrançois Tigeot static int
1412b5c29a34SFrançois Tigeot intel_ring_alloc_seqno(struct intel_ring_buffer *ring)
1413b5c29a34SFrançois Tigeot {
1414b5c29a34SFrançois Tigeot 	if (ring->outstanding_lazy_request)
1415b5c29a34SFrançois Tigeot 		return 0;
1416b5c29a34SFrançois Tigeot 
1417b5c29a34SFrançois Tigeot 	return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_request);
1418b030f26bSFrançois Tigeot }
1419b030f26bSFrançois Tigeot 
1420e3adcf8fSFrançois Tigeot int intel_ring_begin(struct intel_ring_buffer *ring,
1421e3adcf8fSFrançois Tigeot 		     int num_dwords)
1422e3adcf8fSFrançois Tigeot {
1423b5c29a34SFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1424e3adcf8fSFrançois Tigeot 	int n = 4*num_dwords;
1425e3adcf8fSFrançois Tigeot 	int ret;
1426e3adcf8fSFrançois Tigeot 
1427245593daSFrançois Tigeot 	ret = i915_gem_check_wedge(dev_priv, dev_priv->mm.interruptible);
1428245593daSFrançois Tigeot 	if (ret)
1429245593daSFrançois Tigeot 		return ret;
1430e3adcf8fSFrançois Tigeot 
1431b5c29a34SFrançois Tigeot 	/* Preallocate the olr before touching the ring */
1432b5c29a34SFrançois Tigeot 	ret = intel_ring_alloc_seqno(ring);
1433b5c29a34SFrançois Tigeot 	if (ret)
1434b5c29a34SFrançois Tigeot 		return ret;
1435b5c29a34SFrançois Tigeot 
1436245593daSFrançois Tigeot 	if (unlikely(ring->tail + n > ring->effective_size)) {
1437e3adcf8fSFrançois Tigeot 		ret = intel_wrap_ring_buffer(ring);
1438245593daSFrançois Tigeot 		if (unlikely(ret))
1439e3adcf8fSFrançois Tigeot 			return ret;
1440e3adcf8fSFrançois Tigeot 	}
1441e3adcf8fSFrançois Tigeot 
1442b030f26bSFrançois Tigeot 	if (unlikely(ring->space < n)) {
1443b030f26bSFrançois Tigeot 		ret = ring_wait_for_space(ring, n);
1444b030f26bSFrançois Tigeot 		if (unlikely(ret))
1445e3adcf8fSFrançois Tigeot 			return ret;
1446e3adcf8fSFrançois Tigeot 	}
1447e3adcf8fSFrançois Tigeot 
1448e3adcf8fSFrançois Tigeot 	ring->space -= n;
1449e3adcf8fSFrançois Tigeot 	return 0;
1450e3adcf8fSFrançois Tigeot }
1451e3adcf8fSFrançois Tigeot 
1452e3adcf8fSFrançois Tigeot void intel_ring_advance(struct intel_ring_buffer *ring)
1453e3adcf8fSFrançois Tigeot {
1454686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1455686a02f1SFrançois Tigeot 
1456e3adcf8fSFrançois Tigeot 	ring->tail &= ring->size - 1;
1457686a02f1SFrançois Tigeot 	if (dev_priv->stop_rings & intel_ring_flag(ring))
1458686a02f1SFrançois Tigeot 		return;
1459e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, ring->tail);
1460e3adcf8fSFrançois Tigeot }
1461e3adcf8fSFrançois Tigeot 
1462b5c29a34SFrançois Tigeot 
1463e3adcf8fSFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
1464f4e1c372SFrançois Tigeot 				     u32 value)
1465e3adcf8fSFrançois Tigeot {
1466e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1467e3adcf8fSFrançois Tigeot 
1468e3adcf8fSFrançois Tigeot        /* Every tail move must follow the sequence below */
1469f4e1c372SFrançois Tigeot 
1470f4e1c372SFrançois Tigeot 	/* Disable notification that the ring is IDLE. The GT
1471f4e1c372SFrançois Tigeot 	 * will then assume that it is busy and bring it out of rc6.
1472f4e1c372SFrançois Tigeot 	 */
1473e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1474f4e1c372SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1475e3adcf8fSFrançois Tigeot 
1476f4e1c372SFrançois Tigeot 	/* Clear the context id. Here be magic! */
1477f4e1c372SFrançois Tigeot 	I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1478e3adcf8fSFrançois Tigeot 
1479f4e1c372SFrançois Tigeot 	/* Wait for the ring not to be idle, i.e. for it to wake up. */
1480f4e1c372SFrançois Tigeot 	if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1481f4e1c372SFrançois Tigeot 		      GEN6_BSD_SLEEP_INDICATOR) == 0,
1482f4e1c372SFrançois Tigeot 		     50))
1483f4e1c372SFrançois Tigeot 		DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
1484f4e1c372SFrançois Tigeot 
1485f4e1c372SFrançois Tigeot 	/* Now that the ring is fully powered up, update the tail */
1486e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
1487f4e1c372SFrançois Tigeot 	POSTING_READ(RING_TAIL(ring->mmio_base));
1488f4e1c372SFrançois Tigeot 
1489f4e1c372SFrançois Tigeot 	/* Let the ring send IDLE messages to the GT again,
1490f4e1c372SFrançois Tigeot 	 * and so let it sleep to conserve power when idle.
1491f4e1c372SFrançois Tigeot 	 */
1492e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1493f4e1c372SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1494e3adcf8fSFrançois Tigeot }
1495e3adcf8fSFrançois Tigeot 
1496e3adcf8fSFrançois Tigeot static int gen6_ring_flush(struct intel_ring_buffer *ring,
1497b5c29a34SFrançois Tigeot 			   u32 invalidate, u32 flush)
1498e3adcf8fSFrançois Tigeot {
1499e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1500e3adcf8fSFrançois Tigeot 	int ret;
1501e3adcf8fSFrançois Tigeot 
1502e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1503e3adcf8fSFrançois Tigeot 	if (ret)
1504e3adcf8fSFrançois Tigeot 		return ret;
1505e3adcf8fSFrançois Tigeot 
1506e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
1507b5c29a34SFrançois Tigeot 	/*
1508b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.5 - video engine command streamer:
1509b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
1510b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
1511b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
1512b5c29a34SFrançois Tigeot 	 */
1513e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
1514b5c29a34SFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1515b5c29a34SFrançois Tigeot 			MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1516e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1517b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
1518e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1519e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1520e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1521e3adcf8fSFrançois Tigeot 	return 0;
1522e3adcf8fSFrançois Tigeot }
1523e3adcf8fSFrançois Tigeot 
1524e3adcf8fSFrançois Tigeot static int
1525b5c29a34SFrançois Tigeot hsw_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
1526b5c29a34SFrançois Tigeot 			      u32 offset, u32 len,
1527b5c29a34SFrançois Tigeot 			      unsigned flags)
1528e3adcf8fSFrançois Tigeot {
1529e3adcf8fSFrançois Tigeot 	int ret;
1530e3adcf8fSFrançois Tigeot 
1531e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1532e3adcf8fSFrançois Tigeot 	if (ret)
1533e3adcf8fSFrançois Tigeot 		return ret;
1534e3adcf8fSFrançois Tigeot 
1535b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
1536b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
1537b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
1538b5c29a34SFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1539b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset);
1540b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
1541b5c29a34SFrançois Tigeot 
1542b5c29a34SFrançois Tigeot 	return 0;
1543b5c29a34SFrançois Tigeot }
1544b5c29a34SFrançois Tigeot 
1545b5c29a34SFrançois Tigeot static int
1546b5c29a34SFrançois Tigeot gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
1547b5c29a34SFrançois Tigeot 			      u32 offset, u32 len,
1548b5c29a34SFrançois Tigeot 			      unsigned flags)
1549b5c29a34SFrançois Tigeot {
1550b5c29a34SFrançois Tigeot 	int ret;
1551b5c29a34SFrançois Tigeot 
1552b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1553b5c29a34SFrançois Tigeot 	if (ret)
1554b5c29a34SFrançois Tigeot 		return ret;
1555b5c29a34SFrançois Tigeot 
1556b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
1557b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1558b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
1559e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1560e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1561e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1562e3adcf8fSFrançois Tigeot 
1563e3adcf8fSFrançois Tigeot 	return 0;
1564e3adcf8fSFrançois Tigeot }
1565e3adcf8fSFrançois Tigeot 
1566e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
1567e3adcf8fSFrançois Tigeot 
1568e3adcf8fSFrançois Tigeot static int blt_ring_flush(struct intel_ring_buffer *ring,
1569b5c29a34SFrançois Tigeot 			  u32 invalidate, u32 flush)
1570e3adcf8fSFrançois Tigeot {
1571e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1572e3adcf8fSFrançois Tigeot 	int ret;
1573e3adcf8fSFrançois Tigeot 
1574e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1575e3adcf8fSFrançois Tigeot 	if (ret)
1576e3adcf8fSFrançois Tigeot 		return ret;
1577e3adcf8fSFrançois Tigeot 
1578e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
1579b5c29a34SFrançois Tigeot 	/*
1580b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.3 - blitter engine command streamer:
1581b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
1582b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
1583b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
1584b5c29a34SFrançois Tigeot 	 */
1585e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
1586b5c29a34SFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
1587b5c29a34SFrançois Tigeot 			MI_FLUSH_DW_OP_STOREDW;
1588e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1589b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
1590e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1591e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1592e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1593e3adcf8fSFrançois Tigeot 	return 0;
1594e3adcf8fSFrançois Tigeot }
1595e3adcf8fSFrançois Tigeot 
1596e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
1597e3adcf8fSFrançois Tigeot {
1598e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1599ad50ea93SFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1600e3adcf8fSFrançois Tigeot 
1601686a02f1SFrançois Tigeot 	ring->name = "render ring";
1602686a02f1SFrançois Tigeot 	ring->id = RCS;
1603686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
1604686a02f1SFrançois Tigeot 
1605e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
1606e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
1607b5c29a34SFrançois Tigeot 		ring->flush = gen7_render_ring_flush;
1608b5c29a34SFrançois Tigeot 		if (INTEL_INFO(dev)->gen == 6)
1609e3adcf8fSFrançois Tigeot 			ring->flush = gen6_render_ring_flush;
1610686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
1611686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
1612686a02f1SFrançois Tigeot 		ring->irq_enable_mask = GT_USER_INTERRUPT;
1613e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
1614686a02f1SFrançois Tigeot 		ring->sync_to = gen6_ring_sync;
1615686a02f1SFrançois Tigeot 		ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_INVALID;
1616686a02f1SFrançois Tigeot 		ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_RV;
1617686a02f1SFrançois Tigeot 		ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_RB;
1618686a02f1SFrançois Tigeot 		ring->signal_mbox[0] = GEN6_VRSYNC;
1619686a02f1SFrançois Tigeot 		ring->signal_mbox[1] = GEN6_BRSYNC;
1620e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
1621e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
1622686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
1623e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
1624686a02f1SFrançois Tigeot 		ring->irq_get = gen5_ring_get_irq;
1625686a02f1SFrançois Tigeot 		ring->irq_put = gen5_ring_put_irq;
1626686a02f1SFrançois Tigeot 		ring->irq_enable_mask = GT_USER_INTERRUPT | GT_PIPE_NOTIFY;
1627686a02f1SFrançois Tigeot 	} else {
1628686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
1629686a02f1SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 4)
1630686a02f1SFrançois Tigeot 			ring->flush = gen2_render_ring_flush;
1631686a02f1SFrançois Tigeot 		else
1632686a02f1SFrançois Tigeot 			ring->flush = gen4_render_ring_flush;
1633686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
1634686a02f1SFrançois Tigeot 		if (IS_GEN2(dev)) {
1635686a02f1SFrançois Tigeot 			ring->irq_get = i8xx_ring_get_irq;
1636686a02f1SFrançois Tigeot 			ring->irq_put = i8xx_ring_put_irq;
1637686a02f1SFrançois Tigeot 		} else {
1638686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
1639686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
1640e3adcf8fSFrançois Tigeot 		}
1641686a02f1SFrançois Tigeot 		ring->irq_enable_mask = I915_USER_INTERRUPT;
1642686a02f1SFrançois Tigeot 	}
1643686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
1644b5c29a34SFrançois Tigeot 	if (IS_HASWELL(dev))
1645b5c29a34SFrançois Tigeot 		ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
1646b5c29a34SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 6)
1647686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1648686a02f1SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 4)
1649686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1650686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
1651686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1652686a02f1SFrançois Tigeot 	else
1653686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
1654686a02f1SFrançois Tigeot 	ring->init = init_render_ring;
1655686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
1656e3adcf8fSFrançois Tigeot 
1657b5c29a34SFrançois Tigeot 	/* Workaround batchbuffer to combat CS tlb bug. */
1658b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev)) {
1659b5c29a34SFrançois Tigeot 		struct drm_i915_gem_object *obj;
1660b5c29a34SFrançois Tigeot 		int ret;
1661b5c29a34SFrançois Tigeot 
1662b5c29a34SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
1663b5c29a34SFrançois Tigeot 		if (obj == NULL) {
1664b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to allocate batch bo\n");
1665b5c29a34SFrançois Tigeot 			return -ENOMEM;
1666b5c29a34SFrançois Tigeot 		}
1667b5c29a34SFrançois Tigeot 
1668b00bc81cSFrançois Tigeot 		ret = i915_gem_object_pin(obj, 0, true, false);
1669b5c29a34SFrançois Tigeot 		if (ret != 0) {
1670b5c29a34SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
1671b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to ping batch bo\n");
1672b5c29a34SFrançois Tigeot 			return ret;
1673b5c29a34SFrançois Tigeot 		}
1674b5c29a34SFrançois Tigeot 
1675b5c29a34SFrançois Tigeot 		ring->private = obj;
1676e3adcf8fSFrançois Tigeot 	}
1677e3adcf8fSFrançois Tigeot 
1678e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1679e3adcf8fSFrançois Tigeot }
1680e3adcf8fSFrançois Tigeot 
1681686a02f1SFrançois Tigeot int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1682e3adcf8fSFrançois Tigeot {
1683e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1684ad50ea93SFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1685b5c29a34SFrançois Tigeot 	int ret;
1686e3adcf8fSFrançois Tigeot 
1687686a02f1SFrançois Tigeot 	ring->name = "render ring";
1688686a02f1SFrançois Tigeot 	ring->id = RCS;
1689686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
1690686a02f1SFrançois Tigeot 
1691e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
1692686a02f1SFrançois Tigeot 		/* non-kms not supported on gen6+ */
1693686a02f1SFrançois Tigeot 		return -ENODEV;
1694e3adcf8fSFrançois Tigeot 	}
1695e3adcf8fSFrançois Tigeot 
1696686a02f1SFrançois Tigeot 	/* Note: gem is not supported on gen5/ilk without kms (the corresponding
1697686a02f1SFrançois Tigeot 	 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
1698686a02f1SFrançois Tigeot 	 * the special gen5 functions. */
1699686a02f1SFrançois Tigeot 	ring->add_request = i9xx_add_request;
1700686a02f1SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 4)
1701686a02f1SFrançois Tigeot 		ring->flush = gen2_render_ring_flush;
1702686a02f1SFrançois Tigeot 	else
1703686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
1704686a02f1SFrançois Tigeot 	ring->get_seqno = ring_get_seqno;
1705686a02f1SFrançois Tigeot 	if (IS_GEN2(dev)) {
1706686a02f1SFrançois Tigeot 		ring->irq_get = i8xx_ring_get_irq;
1707686a02f1SFrançois Tigeot 		ring->irq_put = i8xx_ring_put_irq;
1708686a02f1SFrançois Tigeot 	} else {
1709686a02f1SFrançois Tigeot 		ring->irq_get = i9xx_ring_get_irq;
1710686a02f1SFrançois Tigeot 		ring->irq_put = i9xx_ring_put_irq;
1711686a02f1SFrançois Tigeot 	}
1712686a02f1SFrançois Tigeot 	ring->irq_enable_mask = I915_USER_INTERRUPT;
1713686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
1714686a02f1SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4)
1715686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1716686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
1717686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1718686a02f1SFrançois Tigeot 	else
1719686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
1720686a02f1SFrançois Tigeot 	ring->init = init_render_ring;
1721686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
1722686a02f1SFrançois Tigeot 
1723e3adcf8fSFrançois Tigeot 	ring->dev = dev;
1724e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1725e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
1726e3adcf8fSFrançois Tigeot 
1727e3adcf8fSFrançois Tigeot 	ring->size = size;
1728e3adcf8fSFrançois Tigeot 	ring->effective_size = ring->size;
1729b5c29a34SFrançois Tigeot 	if (IS_I830(ring->dev) || IS_845G(ring->dev))
1730e3adcf8fSFrançois Tigeot 		ring->effective_size -= 128;
1731e3adcf8fSFrançois Tigeot 
1732686a02f1SFrançois Tigeot 	ring->virtual_start = ioremap_wc(start, size);
1733686a02f1SFrançois Tigeot 	if (ring->virtual_start == NULL) {
1734e3adcf8fSFrançois Tigeot 		DRM_ERROR("can not ioremap virtual address for"
1735e3adcf8fSFrançois Tigeot 			  " ring buffer\n");
1736e3adcf8fSFrançois Tigeot 		return -ENOMEM;
1737e3adcf8fSFrançois Tigeot 	}
1738e3adcf8fSFrançois Tigeot 
1739b5c29a34SFrançois Tigeot 	if (!I915_NEED_GFX_HWS(dev)) {
1740b5c29a34SFrançois Tigeot 		ret = init_phys_hws_pga(ring);
1741b5c29a34SFrançois Tigeot 		if (ret)
1742b5c29a34SFrançois Tigeot 			return ret;
1743b5c29a34SFrançois Tigeot 	}
1744b5c29a34SFrançois Tigeot 
1745e3adcf8fSFrançois Tigeot 	return 0;
1746e3adcf8fSFrançois Tigeot }
1747e3adcf8fSFrançois Tigeot 
1748e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
1749e3adcf8fSFrançois Tigeot {
1750e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1751ad50ea93SFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
1752e3adcf8fSFrançois Tigeot 
1753686a02f1SFrançois Tigeot 	ring->name = "bsd ring";
1754686a02f1SFrançois Tigeot 	ring->id = VCS;
1755686a02f1SFrançois Tigeot 
1756686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
1757686a02f1SFrançois Tigeot 	if (IS_GEN6(dev) || IS_GEN7(dev)) {
1758686a02f1SFrançois Tigeot 		ring->mmio_base = GEN6_BSD_RING_BASE;
1759686a02f1SFrançois Tigeot 		/* gen6 bsd needs a special wa for tail updates */
1760686a02f1SFrançois Tigeot 		if (IS_GEN6(dev))
1761686a02f1SFrançois Tigeot 			ring->write_tail = gen6_bsd_ring_write_tail;
1762686a02f1SFrançois Tigeot 		ring->flush = gen6_ring_flush;
1763686a02f1SFrançois Tigeot 		ring->add_request = gen6_add_request;
1764686a02f1SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
1765686a02f1SFrançois Tigeot 		ring->irq_enable_mask = GEN6_BSD_USER_INTERRUPT;
1766686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
1767686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
1768686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1769686a02f1SFrançois Tigeot 		ring->sync_to = gen6_ring_sync;
1770686a02f1SFrançois Tigeot 		ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_VR;
1771686a02f1SFrançois Tigeot 		ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_INVALID;
1772686a02f1SFrançois Tigeot 		ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_VB;
1773686a02f1SFrançois Tigeot 		ring->signal_mbox[0] = GEN6_RVSYNC;
1774686a02f1SFrançois Tigeot 		ring->signal_mbox[1] = GEN6_BVSYNC;
1775686a02f1SFrançois Tigeot 	} else {
1776686a02f1SFrançois Tigeot 		ring->mmio_base = BSD_RING_BASE;
1777686a02f1SFrançois Tigeot 		ring->flush = bsd_ring_flush;
1778686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
1779686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
1780686a02f1SFrançois Tigeot 		if (IS_GEN5(dev)) {
1781686a02f1SFrançois Tigeot 			ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
1782686a02f1SFrançois Tigeot 			ring->irq_get = gen5_ring_get_irq;
1783686a02f1SFrançois Tigeot 			ring->irq_put = gen5_ring_put_irq;
1784686a02f1SFrançois Tigeot 		} else {
1785686a02f1SFrançois Tigeot 			ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
1786686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
1787686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
1788686a02f1SFrançois Tigeot 		}
1789686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1790686a02f1SFrançois Tigeot 	}
1791686a02f1SFrançois Tigeot 	ring->init = init_ring_common;
1792e3adcf8fSFrançois Tigeot 
1793e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1794e3adcf8fSFrançois Tigeot }
1795e3adcf8fSFrançois Tigeot 
1796e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
1797e3adcf8fSFrançois Tigeot {
1798e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1799ad50ea93SFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
1800e3adcf8fSFrançois Tigeot 
1801686a02f1SFrançois Tigeot 	ring->name = "blitter ring";
1802686a02f1SFrançois Tigeot 	ring->id = BCS;
1803686a02f1SFrançois Tigeot 
1804686a02f1SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
1805686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
1806686a02f1SFrançois Tigeot 	ring->flush = blt_ring_flush;
1807686a02f1SFrançois Tigeot 	ring->add_request = gen6_add_request;
1808686a02f1SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
1809686a02f1SFrançois Tigeot 	ring->irq_enable_mask = GEN6_BLITTER_USER_INTERRUPT;
1810686a02f1SFrançois Tigeot 	ring->irq_get = gen6_ring_get_irq;
1811686a02f1SFrançois Tigeot 	ring->irq_put = gen6_ring_put_irq;
1812686a02f1SFrançois Tigeot 	ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1813686a02f1SFrançois Tigeot 	ring->sync_to = gen6_ring_sync;
1814686a02f1SFrançois Tigeot 	ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_BR;
1815686a02f1SFrançois Tigeot 	ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_BV;
1816686a02f1SFrançois Tigeot 	ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_INVALID;
1817686a02f1SFrançois Tigeot 	ring->signal_mbox[0] = GEN6_RBSYNC;
1818686a02f1SFrançois Tigeot 	ring->signal_mbox[1] = GEN6_VBSYNC;
1819686a02f1SFrançois Tigeot 	ring->init = init_ring_common;
1820e3adcf8fSFrançois Tigeot 
1821e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1822e3adcf8fSFrançois Tigeot }
1823b030f26bSFrançois Tigeot 
1824b030f26bSFrançois Tigeot int
1825b030f26bSFrançois Tigeot intel_ring_flush_all_caches(struct intel_ring_buffer *ring)
1826b030f26bSFrançois Tigeot {
1827b030f26bSFrançois Tigeot 	int ret;
1828b030f26bSFrançois Tigeot 
1829b030f26bSFrançois Tigeot 	if (!ring->gpu_caches_dirty)
1830b030f26bSFrançois Tigeot 		return 0;
1831b030f26bSFrançois Tigeot 
1832b030f26bSFrançois Tigeot 	ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
1833b030f26bSFrançois Tigeot 	if (ret)
1834b030f26bSFrançois Tigeot 		return ret;
1835b030f26bSFrançois Tigeot 
1836b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
1837b030f26bSFrançois Tigeot 	return 0;
1838b030f26bSFrançois Tigeot }
1839b030f26bSFrançois Tigeot 
1840b030f26bSFrançois Tigeot int
1841b030f26bSFrançois Tigeot intel_ring_invalidate_all_caches(struct intel_ring_buffer *ring)
1842b030f26bSFrançois Tigeot {
1843b030f26bSFrançois Tigeot 	uint32_t flush_domains;
1844b030f26bSFrançois Tigeot 	int ret;
1845b030f26bSFrançois Tigeot 
1846b030f26bSFrançois Tigeot 	flush_domains = 0;
1847b030f26bSFrançois Tigeot 	if (ring->gpu_caches_dirty)
1848b030f26bSFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
1849b030f26bSFrançois Tigeot 
1850b030f26bSFrançois Tigeot 	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
1851b030f26bSFrançois Tigeot 	if (ret)
1852b030f26bSFrançois Tigeot 		return ret;
1853b030f26bSFrançois Tigeot 
1854b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
1855b030f26bSFrançois Tigeot 	return 0;
1856b030f26bSFrançois Tigeot }
1857