xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision a2fdbec6742a86e62144b3d67a14a72c57c4d985)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2008-2010 Intel Corporation
3e3adcf8fSFrançois Tigeot  *
4e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10e3adcf8fSFrançois Tigeot  *
11e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13e3adcf8fSFrançois Tigeot  * Software.
14e3adcf8fSFrançois Tigeot  *
15e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e3adcf8fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e3adcf8fSFrançois Tigeot  * IN THE SOFTWARE.
22e3adcf8fSFrançois Tigeot  *
23e3adcf8fSFrançois Tigeot  * Authors:
24e3adcf8fSFrançois Tigeot  *    Eric Anholt <eric@anholt.net>
25e3adcf8fSFrançois Tigeot  *    Zou Nan hai <nanhai.zou@intel.com>
26e3adcf8fSFrançois Tigeot  *    Xiang Hai hao<haihao.xiang@intel.com>
27e3adcf8fSFrançois Tigeot  *
28e3adcf8fSFrançois Tigeot  */
29e3adcf8fSFrançois Tigeot 
3018e26a6dSFrançois Tigeot #include <drm/drmP.h>
31e3adcf8fSFrançois Tigeot #include "i915_drv.h"
32*a2fdbec6SFrançois Tigeot #include <drm/i915_drm.h>
33*a2fdbec6SFrançois Tigeot #include "i915_trace.h"
34e3adcf8fSFrançois Tigeot #include "intel_drv.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;
321*a2fdbec6SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
322b5c29a34SFrançois Tigeot 
323b5c29a34SFrançois Tigeot 		/* Workaround: we must issue a pipe_control with CS-stall bit
324b5c29a34SFrançois Tigeot 		 * set before a pipe_control command that has the state cache
325b5c29a34SFrançois Tigeot 		 * invalidate bit set. */
326b5c29a34SFrançois Tigeot 		gen7_render_ring_cs_stall_wa(ring);
327b5c29a34SFrançois Tigeot 	}
328b5c29a34SFrançois Tigeot 
329b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
330b5c29a34SFrançois Tigeot 	if (ret)
331b5c29a34SFrançois Tigeot 		return ret;
332b5c29a34SFrançois Tigeot 
333b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
334b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, flags);
335*a2fdbec6SFrançois Tigeot 	intel_ring_emit(ring, scratch_addr);
336b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, 0);
337e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
338e3adcf8fSFrançois Tigeot 
339e3adcf8fSFrançois Tigeot 	return 0;
340e3adcf8fSFrançois Tigeot }
341e3adcf8fSFrançois Tigeot 
342e3adcf8fSFrançois Tigeot static void ring_write_tail(struct intel_ring_buffer *ring,
343b5c29a34SFrançois Tigeot 			    u32 value)
344e3adcf8fSFrançois Tigeot {
345e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
346e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
347e3adcf8fSFrançois Tigeot }
348e3adcf8fSFrançois Tigeot 
349e3adcf8fSFrançois Tigeot u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
350e3adcf8fSFrançois Tigeot {
351e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
352b5c29a34SFrançois Tigeot 	u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
353e3adcf8fSFrançois Tigeot 			RING_ACTHD(ring->mmio_base) : ACTHD;
354e3adcf8fSFrançois Tigeot 
355e3adcf8fSFrançois Tigeot 	return I915_READ(acthd_reg);
356e3adcf8fSFrançois Tigeot }
357e3adcf8fSFrançois Tigeot 
358e3adcf8fSFrançois Tigeot static int init_ring_common(struct intel_ring_buffer *ring)
359e3adcf8fSFrançois Tigeot {
360686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
361686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
362e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj = ring->obj;
363686a02f1SFrançois Tigeot 	int ret = 0;
364b5c29a34SFrançois Tigeot 	u32 head;
365e3adcf8fSFrançois Tigeot 
366686a02f1SFrançois Tigeot 	if (HAS_FORCE_WAKE(dev))
367686a02f1SFrançois Tigeot 		gen6_gt_force_wake_get(dev_priv);
368686a02f1SFrançois Tigeot 
369e3adcf8fSFrançois Tigeot 	/* Stop the ring if it's running. */
370e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
371e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
372e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
373e3adcf8fSFrançois Tigeot 
374e3adcf8fSFrançois Tigeot 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
375e3adcf8fSFrançois Tigeot 
376e3adcf8fSFrançois Tigeot 	/* G45 ring initialization fails to reset head to zero */
377e3adcf8fSFrançois Tigeot 	if (head != 0) {
378b5c29a34SFrançois Tigeot 		DRM_DEBUG_KMS("%s head not reset to zero "
379e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
380e3adcf8fSFrançois Tigeot 			      ring->name,
381e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
382e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
383e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
384e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
385e3adcf8fSFrançois Tigeot 
386e3adcf8fSFrançois Tigeot 		I915_WRITE_HEAD(ring, 0);
387e3adcf8fSFrançois Tigeot 
388e3adcf8fSFrançois Tigeot 		if (I915_READ_HEAD(ring) & HEAD_ADDR) {
389e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
390e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
391e3adcf8fSFrançois Tigeot 				  ring->name,
392e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
393e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
394e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
395e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
396e3adcf8fSFrançois Tigeot 		}
397e3adcf8fSFrançois Tigeot 	}
398e3adcf8fSFrançois Tigeot 
399b5c29a34SFrançois Tigeot 	/* Initialize the ring. This must happen _after_ we've cleared the ring
400b5c29a34SFrançois Tigeot 	 * registers with the above sequence (the readback of the HEAD registers
401b5c29a34SFrançois Tigeot 	 * also enforces ordering), otherwise the hw might lose the new ring
402b5c29a34SFrançois Tigeot 	 * register values. */
403b5c29a34SFrançois Tigeot 	I915_WRITE_START(ring, obj->gtt_offset);
404e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring,
405e3adcf8fSFrançois Tigeot 			((ring->size - PAGE_SIZE) & RING_NR_PAGES)
406e3adcf8fSFrançois Tigeot 			| RING_VALID);
407e3adcf8fSFrançois Tigeot 
408e3adcf8fSFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
409b5c29a34SFrançois Tigeot 	if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
410e3adcf8fSFrançois Tigeot 		     I915_READ_START(ring) == obj->gtt_offset &&
411b5c29a34SFrançois Tigeot 		     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
412e3adcf8fSFrançois Tigeot 		DRM_ERROR("%s initialization failed "
413e3adcf8fSFrançois Tigeot 				"ctl %08x head %08x tail %08x start %08x\n",
414e3adcf8fSFrançois Tigeot 				ring->name,
415e3adcf8fSFrançois Tigeot 				I915_READ_CTL(ring),
416e3adcf8fSFrançois Tigeot 				I915_READ_HEAD(ring),
417e3adcf8fSFrançois Tigeot 				I915_READ_TAIL(ring),
418e3adcf8fSFrançois Tigeot 				I915_READ_START(ring));
419686a02f1SFrançois Tigeot 		ret = -EIO;
420686a02f1SFrançois Tigeot 		goto out;
421e3adcf8fSFrançois Tigeot 	}
422e3adcf8fSFrançois Tigeot 
423e3adcf8fSFrançois Tigeot 	if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
424e3adcf8fSFrançois Tigeot 		i915_kernel_lost_context(ring->dev);
425e3adcf8fSFrançois Tigeot 	else {
426e3adcf8fSFrançois Tigeot 		ring->head = I915_READ_HEAD(ring);
427e3adcf8fSFrançois Tigeot 		ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
428e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
429686a02f1SFrançois Tigeot 		ring->last_retired_head = -1;
430e3adcf8fSFrançois Tigeot 	}
431e3adcf8fSFrançois Tigeot 
432686a02f1SFrançois Tigeot out:
433686a02f1SFrançois Tigeot 	if (HAS_FORCE_WAKE(dev))
434686a02f1SFrançois Tigeot 		gen6_gt_force_wake_put(dev_priv);
435686a02f1SFrançois Tigeot 
436686a02f1SFrançois Tigeot 	return ret;
437e3adcf8fSFrançois Tigeot }
438e3adcf8fSFrançois Tigeot 
439e3adcf8fSFrançois Tigeot static int
440e3adcf8fSFrançois Tigeot init_pipe_control(struct intel_ring_buffer *ring)
441e3adcf8fSFrançois Tigeot {
442e3adcf8fSFrançois Tigeot 	struct pipe_control *pc;
443e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
444e3adcf8fSFrançois Tigeot 	int ret;
445e3adcf8fSFrançois Tigeot 
446e3adcf8fSFrançois Tigeot 	if (ring->private)
447e3adcf8fSFrançois Tigeot 		return 0;
448e3adcf8fSFrançois Tigeot 
4495a3b77d5SFrançois Tigeot 	pc = kmalloc(sizeof(*pc), M_DRM, M_WAITOK);
450e3adcf8fSFrançois Tigeot 	if (!pc)
451e3adcf8fSFrançois Tigeot 		return -ENOMEM;
452e3adcf8fSFrançois Tigeot 
453e3adcf8fSFrançois Tigeot 	obj = i915_gem_alloc_object(ring->dev, 4096);
454e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
455e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
456e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
457e3adcf8fSFrançois Tigeot 		goto err;
458e3adcf8fSFrançois Tigeot 	}
459e3adcf8fSFrançois Tigeot 
460e3adcf8fSFrançois Tigeot 	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
461e3adcf8fSFrançois Tigeot 
462b00bc81cSFrançois Tigeot 	ret = i915_gem_object_pin(obj, 4096, true, false);
463e3adcf8fSFrançois Tigeot 	if (ret)
464e3adcf8fSFrançois Tigeot 		goto err_unref;
465e3adcf8fSFrançois Tigeot 
466e3adcf8fSFrançois Tigeot 	pc->gtt_offset = obj->gtt_offset;
467e3adcf8fSFrançois Tigeot 	pc->cpu_page = (uint32_t *)kmem_alloc_nofault(&kernel_map, PAGE_SIZE, PAGE_SIZE);
468e3adcf8fSFrançois Tigeot 	if (pc->cpu_page == NULL)
469e3adcf8fSFrançois Tigeot 		goto err_unpin;
470*a2fdbec6SFrançois Tigeot 
471e3adcf8fSFrançois Tigeot 	pmap_qenter((uintptr_t)pc->cpu_page, &obj->pages[0], 1);
472e3adcf8fSFrançois Tigeot 	pmap_invalidate_cache_range((vm_offset_t)pc->cpu_page,
473e3adcf8fSFrançois Tigeot 	    (vm_offset_t)pc->cpu_page + PAGE_SIZE);
474*a2fdbec6SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
475*a2fdbec6SFrançois Tigeot 			 ring->name, pc->gtt_offset);
476e3adcf8fSFrançois Tigeot 
477e3adcf8fSFrançois Tigeot 	pc->obj = obj;
478e3adcf8fSFrançois Tigeot 	ring->private = pc;
479e3adcf8fSFrançois Tigeot 	return 0;
480e3adcf8fSFrançois Tigeot 
481e3adcf8fSFrançois Tigeot err_unpin:
482e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
483e3adcf8fSFrançois Tigeot err_unref:
484e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
485e3adcf8fSFrançois Tigeot err:
486e3440f96SFrançois Tigeot 	kfree(pc, M_DRM);
487e3adcf8fSFrançois Tigeot 	return ret;
488e3adcf8fSFrançois Tigeot }
489e3adcf8fSFrançois Tigeot 
490e3adcf8fSFrançois Tigeot static void
491e3adcf8fSFrançois Tigeot cleanup_pipe_control(struct intel_ring_buffer *ring)
492e3adcf8fSFrançois Tigeot {
493e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
494e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
495e3adcf8fSFrançois Tigeot 
496e3adcf8fSFrançois Tigeot 	if (!ring->private)
497e3adcf8fSFrançois Tigeot 		return;
498e3adcf8fSFrançois Tigeot 
499e3adcf8fSFrançois Tigeot 	obj = pc->obj;
500*a2fdbec6SFrançois Tigeot 
501e3adcf8fSFrançois Tigeot 	pmap_qremove((vm_offset_t)pc->cpu_page, 1);
502e3adcf8fSFrançois Tigeot 	kmem_free(&kernel_map, (uintptr_t)pc->cpu_page, PAGE_SIZE);
503e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
504e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
505e3adcf8fSFrançois Tigeot 
506e3440f96SFrançois Tigeot 	kfree(pc, M_DRM);
507e3adcf8fSFrançois Tigeot 	ring->private = NULL;
508e3adcf8fSFrançois Tigeot }
509e3adcf8fSFrançois Tigeot 
510e3adcf8fSFrançois Tigeot static int init_render_ring(struct intel_ring_buffer *ring)
511e3adcf8fSFrançois Tigeot {
512e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
513e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
514e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
515e3adcf8fSFrançois Tigeot 
516f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen > 3)
517f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
518f4e1c372SFrançois Tigeot 
519f4e1c372SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
520f4e1c372SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
521f4e1c372SFrançois Tigeot 	 * programmed to '1' on all products.
522f4e1c372SFrançois Tigeot 	 */
523f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
524f4e1c372SFrançois Tigeot 		I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
525f4e1c372SFrançois Tigeot 
526f4e1c372SFrançois Tigeot 	/* Required for the hardware to program scanline values for waiting */
527f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6)
528f4e1c372SFrançois Tigeot 		I915_WRITE(GFX_MODE,
529f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_ALWAYS));
530f4e1c372SFrançois Tigeot 
531e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev))
532e3adcf8fSFrançois Tigeot 		I915_WRITE(GFX_MODE_GEN7,
533f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
534f4e1c372SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
535e3adcf8fSFrançois Tigeot 
536e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
537e3adcf8fSFrançois Tigeot 		ret = init_pipe_control(ring);
538e3adcf8fSFrançois Tigeot 		if (ret)
539e3adcf8fSFrançois Tigeot 			return ret;
540e3adcf8fSFrançois Tigeot 	}
541e3adcf8fSFrançois Tigeot 
542e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
543e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
544e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
545e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
546e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
547e3adcf8fSFrançois Tigeot 		 */
548e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
549f4e1c372SFrançois Tigeot 			   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
550686a02f1SFrançois Tigeot 
551686a02f1SFrançois Tigeot 		/* This is not explicitly set for GEN6, so read the register.
552686a02f1SFrançois Tigeot 		 * see intel_ring_mi_set_context() for why we care.
553686a02f1SFrançois Tigeot 		 * TODO: consider explicitly setting the bit for GEN5
554686a02f1SFrançois Tigeot 		 */
555686a02f1SFrançois Tigeot 		ring->itlb_before_ctx_switch =
556686a02f1SFrançois Tigeot 			!!(I915_READ(GFX_MODE) & GFX_TLB_INVALIDATE_ALWAYS);
557e3adcf8fSFrançois Tigeot 	}
558e3adcf8fSFrançois Tigeot 
559f4e1c372SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6)
560f4e1c372SFrançois Tigeot 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
561f4e1c372SFrançois Tigeot 
562f4e1c372SFrançois Tigeot 	if (HAS_L3_GPU_CACHE(dev))
563f4e1c372SFrançois Tigeot 		I915_WRITE_IMR(ring, ~GEN6_RENDER_L3_PARITY_ERROR);
564e3adcf8fSFrançois Tigeot 
565e3adcf8fSFrançois Tigeot 	return ret;
566e3adcf8fSFrançois Tigeot }
567e3adcf8fSFrançois Tigeot 
568e3adcf8fSFrançois Tigeot static void render_ring_cleanup(struct intel_ring_buffer *ring)
569e3adcf8fSFrançois Tigeot {
570b5c29a34SFrançois Tigeot 	struct drm_device *dev = ring->dev;
571b5c29a34SFrançois Tigeot 
572e3adcf8fSFrançois Tigeot 	if (!ring->private)
573e3adcf8fSFrançois Tigeot 		return;
574e3adcf8fSFrançois Tigeot 
575b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev))
576b5c29a34SFrançois Tigeot 		drm_gem_object_unreference(to_gem_object(ring->private));
577b5c29a34SFrançois Tigeot 
578e3adcf8fSFrançois Tigeot 	cleanup_pipe_control(ring);
579e3adcf8fSFrançois Tigeot }
580e3adcf8fSFrançois Tigeot 
581e3adcf8fSFrançois Tigeot static void
582e3adcf8fSFrançois Tigeot update_mboxes(struct intel_ring_buffer *ring,
583e3adcf8fSFrançois Tigeot 	      u32 mmio_offset)
584e3adcf8fSFrançois Tigeot {
585b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
586e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, mmio_offset);
587b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
588e3adcf8fSFrançois Tigeot }
589e3adcf8fSFrançois Tigeot 
590e3adcf8fSFrançois Tigeot /**
591e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
592e3adcf8fSFrançois Tigeot  *
593e3adcf8fSFrançois Tigeot  * @ring - ring that is adding a request
594e3adcf8fSFrançois Tigeot  * @seqno - return seqno stuck into the ring
595e3adcf8fSFrançois Tigeot  *
596e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
597e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
598e3adcf8fSFrançois Tigeot  */
599e3adcf8fSFrançois Tigeot static int
600b5c29a34SFrançois Tigeot gen6_add_request(struct intel_ring_buffer *ring)
601e3adcf8fSFrançois Tigeot {
602e3adcf8fSFrançois Tigeot 	u32 mbox1_reg;
603e3adcf8fSFrançois Tigeot 	u32 mbox2_reg;
604e3adcf8fSFrançois Tigeot 	int ret;
605e3adcf8fSFrançois Tigeot 
606e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 10);
607e3adcf8fSFrançois Tigeot 	if (ret)
608e3adcf8fSFrançois Tigeot 		return ret;
609e3adcf8fSFrançois Tigeot 
610e3adcf8fSFrançois Tigeot 	mbox1_reg = ring->signal_mbox[0];
611e3adcf8fSFrançois Tigeot 	mbox2_reg = ring->signal_mbox[1];
612e3adcf8fSFrançois Tigeot 
613b5c29a34SFrançois Tigeot 	update_mboxes(ring, mbox1_reg);
614b5c29a34SFrançois Tigeot 	update_mboxes(ring, mbox2_reg);
615e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
616e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
617b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
618e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
619e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
620e3adcf8fSFrançois Tigeot 
621e3adcf8fSFrançois Tigeot 	return 0;
622e3adcf8fSFrançois Tigeot }
623e3adcf8fSFrançois Tigeot 
624*a2fdbec6SFrançois Tigeot static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
625*a2fdbec6SFrançois Tigeot 					      u32 seqno)
626*a2fdbec6SFrançois Tigeot {
627*a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
628*a2fdbec6SFrançois Tigeot 	return dev_priv->last_seqno < seqno;
629*a2fdbec6SFrançois Tigeot }
630*a2fdbec6SFrançois Tigeot 
631e3adcf8fSFrançois Tigeot /**
632e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
633e3adcf8fSFrançois Tigeot  *
634e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
635e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
636e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
637e3adcf8fSFrançois Tigeot  */
638e3adcf8fSFrançois Tigeot static int
639686a02f1SFrançois Tigeot gen6_ring_sync(struct intel_ring_buffer *waiter,
640e3adcf8fSFrançois Tigeot 	       struct intel_ring_buffer *signaller,
641e3adcf8fSFrançois Tigeot 	       u32 seqno)
642e3adcf8fSFrançois Tigeot {
643e3adcf8fSFrançois Tigeot 	int ret;
644e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
645e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
646e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
647e3adcf8fSFrançois Tigeot 
648686a02f1SFrançois Tigeot 	/* Throughout all of the GEM code, seqno passed implies our current
649686a02f1SFrançois Tigeot 	 * seqno is >= the last seqno executed. However for hardware the
650686a02f1SFrançois Tigeot 	 * comparison is strictly greater than.
651686a02f1SFrançois Tigeot 	 */
652686a02f1SFrançois Tigeot 	seqno -= 1;
653686a02f1SFrançois Tigeot 
654686a02f1SFrançois Tigeot 	WARN_ON(signaller->semaphore_register[waiter->id] ==
655686a02f1SFrançois Tigeot 		MI_SEMAPHORE_SYNC_INVALID);
656686a02f1SFrançois Tigeot 
657e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
658e3adcf8fSFrançois Tigeot 	if (ret)
659e3adcf8fSFrançois Tigeot 		return ret;
660e3adcf8fSFrançois Tigeot 
661*a2fdbec6SFrançois Tigeot 	/* If seqno wrap happened, omit the wait with no-ops */
662*a2fdbec6SFrançois Tigeot 	if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
663686a02f1SFrançois Tigeot 		intel_ring_emit(waiter,
664*a2fdbec6SFrançois Tigeot 				dw1 |
665*a2fdbec6SFrançois Tigeot 				signaller->semaphore_register[waiter->id]);
666e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, seqno);
667e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, 0);
668e3adcf8fSFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
669*a2fdbec6SFrançois Tigeot 	} else {
670*a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
671*a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
672*a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
673*a2fdbec6SFrançois Tigeot 		intel_ring_emit(waiter, MI_NOOP);
674*a2fdbec6SFrançois Tigeot 	}
675e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
676e3adcf8fSFrançois Tigeot 
677e3adcf8fSFrançois Tigeot 	return 0;
678e3adcf8fSFrançois Tigeot }
679e3adcf8fSFrançois Tigeot 
680e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
681e3adcf8fSFrançois Tigeot do {									\
682e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
683e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
684e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
685e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
686e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
687e3adcf8fSFrançois Tigeot } while (0)
688e3adcf8fSFrançois Tigeot 
689e3adcf8fSFrançois Tigeot static int
690b5c29a34SFrançois Tigeot pc_render_add_request(struct intel_ring_buffer *ring)
691e3adcf8fSFrançois Tigeot {
692e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
693e3adcf8fSFrançois Tigeot 	u32 scratch_addr = pc->gtt_offset + 128;
694e3adcf8fSFrançois Tigeot 	int ret;
695e3adcf8fSFrançois Tigeot 
696e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
697e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
698e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
699e3adcf8fSFrançois Tigeot 	 *
700e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
701e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
702e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
703e3adcf8fSFrançois Tigeot 	 */
704e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 32);
705e3adcf8fSFrançois Tigeot 	if (ret)
706e3adcf8fSFrançois Tigeot 		return ret;
707e3adcf8fSFrançois Tigeot 
708e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
709e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
710e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
711e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
712b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
713e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
714e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
715e3adcf8fSFrançois Tigeot 	scratch_addr += 128; /* write to separate cachelines */
716e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
717e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
718e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
719e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
720e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
721e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
722e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
723e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
724e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
725b5c29a34SFrançois Tigeot 
726e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
727e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
728e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
729e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
730e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
731b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
732e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
733e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
734e3adcf8fSFrançois Tigeot 
735e3adcf8fSFrançois Tigeot 	return 0;
736e3adcf8fSFrançois Tigeot }
737e3adcf8fSFrançois Tigeot 
738e3adcf8fSFrançois Tigeot static u32
739b030f26bSFrançois Tigeot gen6_ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
740e3adcf8fSFrançois Tigeot {
741e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
742e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
743e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
744b030f26bSFrançois Tigeot 	if (!lazy_coherency)
745e3adcf8fSFrançois Tigeot 		intel_ring_get_active_head(ring);
746e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
747e3adcf8fSFrançois Tigeot }
748e3adcf8fSFrançois Tigeot 
749b030f26bSFrançois Tigeot static u32
750b030f26bSFrançois Tigeot ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
751e3adcf8fSFrançois Tigeot {
752e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
753e3adcf8fSFrançois Tigeot }
754e3adcf8fSFrançois Tigeot 
755*a2fdbec6SFrançois Tigeot static void
756*a2fdbec6SFrançois Tigeot ring_set_seqno(struct intel_ring_buffer *ring, u32 seqno)
757*a2fdbec6SFrançois Tigeot {
758*a2fdbec6SFrançois Tigeot 	intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
759*a2fdbec6SFrançois Tigeot }
760*a2fdbec6SFrançois Tigeot 
761b030f26bSFrançois Tigeot static u32
762b030f26bSFrançois Tigeot pc_render_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
763e3adcf8fSFrançois Tigeot {
764e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
765e3adcf8fSFrançois Tigeot 	return pc->cpu_page[0];
766e3adcf8fSFrançois Tigeot }
767e3adcf8fSFrançois Tigeot 
768*a2fdbec6SFrançois Tigeot static void
769*a2fdbec6SFrançois Tigeot pc_render_set_seqno(struct intel_ring_buffer *ring, u32 seqno)
770*a2fdbec6SFrançois Tigeot {
771*a2fdbec6SFrançois Tigeot 	struct pipe_control *pc = ring->private;
772*a2fdbec6SFrançois Tigeot 	pc->cpu_page[0] = seqno;
773*a2fdbec6SFrançois Tigeot }
774*a2fdbec6SFrançois Tigeot 
775e3adcf8fSFrançois Tigeot static bool
776686a02f1SFrançois Tigeot gen5_ring_get_irq(struct intel_ring_buffer *ring)
777e3adcf8fSFrançois Tigeot {
778e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
779e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
780e3adcf8fSFrançois Tigeot 
781e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
782e3adcf8fSFrançois Tigeot 		return false;
783e3adcf8fSFrançois Tigeot 
78402727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
785e3adcf8fSFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
786686a02f1SFrançois Tigeot 		dev_priv->gt_irq_mask &= ~ring->irq_enable_mask;
787686a02f1SFrançois Tigeot 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
788686a02f1SFrançois Tigeot 		POSTING_READ(GTIMR);
789e3adcf8fSFrançois Tigeot 	}
79002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
791e3adcf8fSFrançois Tigeot 
792e3adcf8fSFrançois Tigeot 	return true;
793e3adcf8fSFrançois Tigeot }
794e3adcf8fSFrançois Tigeot 
795e3adcf8fSFrançois Tigeot static void
796686a02f1SFrançois Tigeot gen5_ring_put_irq(struct intel_ring_buffer *ring)
797e3adcf8fSFrançois Tigeot {
798e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
799e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
800e3adcf8fSFrançois Tigeot 
80102727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
802e3adcf8fSFrançois Tigeot 	if (--ring->irq_refcount == 0) {
803686a02f1SFrançois Tigeot 		dev_priv->gt_irq_mask |= ring->irq_enable_mask;
804686a02f1SFrançois Tigeot 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
805686a02f1SFrançois Tigeot 		POSTING_READ(GTIMR);
806686a02f1SFrançois Tigeot 	}
807686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
808686a02f1SFrançois Tigeot }
809686a02f1SFrançois Tigeot 
810686a02f1SFrançois Tigeot static bool
811686a02f1SFrançois Tigeot i9xx_ring_get_irq(struct intel_ring_buffer *ring)
812686a02f1SFrançois Tigeot {
813686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
814686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
815686a02f1SFrançois Tigeot 
816686a02f1SFrançois Tigeot 	if (!dev->irq_enabled)
817686a02f1SFrançois Tigeot 		return false;
818686a02f1SFrançois Tigeot 
819686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
820686a02f1SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
821686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
822686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
823686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
824686a02f1SFrançois Tigeot 	}
825686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
826686a02f1SFrançois Tigeot 
827686a02f1SFrançois Tigeot 	return true;
828686a02f1SFrançois Tigeot }
829686a02f1SFrançois Tigeot 
830686a02f1SFrançois Tigeot static void
831686a02f1SFrançois Tigeot i9xx_ring_put_irq(struct intel_ring_buffer *ring)
832686a02f1SFrançois Tigeot {
833686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
834686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
835686a02f1SFrançois Tigeot 
836686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
837686a02f1SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
838686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
839686a02f1SFrançois Tigeot 		I915_WRITE(IMR, dev_priv->irq_mask);
840686a02f1SFrançois Tigeot 		POSTING_READ(IMR);
841686a02f1SFrançois Tigeot 	}
842686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
843686a02f1SFrançois Tigeot }
844686a02f1SFrançois Tigeot 
845686a02f1SFrançois Tigeot static bool
846686a02f1SFrançois Tigeot i8xx_ring_get_irq(struct intel_ring_buffer *ring)
847686a02f1SFrançois Tigeot {
848686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
849686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
850686a02f1SFrançois Tigeot 
851686a02f1SFrançois Tigeot 	if (!dev->irq_enabled)
852686a02f1SFrançois Tigeot 		return false;
853686a02f1SFrançois Tigeot 
854686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
855686a02f1SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
856686a02f1SFrançois Tigeot 		dev_priv->irq_mask &= ~ring->irq_enable_mask;
857686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
858686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
859686a02f1SFrançois Tigeot 	}
860686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
861686a02f1SFrançois Tigeot 
862686a02f1SFrançois Tigeot 	return true;
863686a02f1SFrançois Tigeot }
864686a02f1SFrançois Tigeot 
865686a02f1SFrançois Tigeot static void
866686a02f1SFrançois Tigeot i8xx_ring_put_irq(struct intel_ring_buffer *ring)
867686a02f1SFrançois Tigeot {
868686a02f1SFrançois Tigeot 	struct drm_device *dev = ring->dev;
869686a02f1SFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
870686a02f1SFrançois Tigeot 
871686a02f1SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
872686a02f1SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
873686a02f1SFrançois Tigeot 		dev_priv->irq_mask |= ring->irq_enable_mask;
874686a02f1SFrançois Tigeot 		I915_WRITE16(IMR, dev_priv->irq_mask);
875686a02f1SFrançois Tigeot 		POSTING_READ16(IMR);
876e3adcf8fSFrançois Tigeot 	}
87702727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
878e3adcf8fSFrançois Tigeot }
879e3adcf8fSFrançois Tigeot 
880e3adcf8fSFrançois Tigeot void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
881e3adcf8fSFrançois Tigeot {
882e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
883b5c29a34SFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
884b5c29a34SFrançois Tigeot 	u32 mmio = 0;
885e3adcf8fSFrançois Tigeot 
886e3adcf8fSFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
887e3adcf8fSFrançois Tigeot 	 * the ring registers as of gen7.
888e3adcf8fSFrançois Tigeot 	 */
889e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev)) {
890e3adcf8fSFrançois Tigeot 		switch (ring->id) {
891e3adcf8fSFrançois Tigeot 		case RCS:
892e3adcf8fSFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
893e3adcf8fSFrançois Tigeot 			break;
894e3adcf8fSFrançois Tigeot 		case BCS:
895e3adcf8fSFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
896e3adcf8fSFrançois Tigeot 			break;
897e3adcf8fSFrançois Tigeot 		case VCS:
898e3adcf8fSFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
899e3adcf8fSFrançois Tigeot 			break;
900e3adcf8fSFrançois Tigeot 		}
901b5c29a34SFrançois Tigeot 	} else if (IS_GEN6(ring->dev)) {
902e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
903e3adcf8fSFrançois Tigeot 	} else {
904e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
905e3adcf8fSFrançois Tigeot 	}
906e3adcf8fSFrançois Tigeot 
907e3adcf8fSFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
908e3adcf8fSFrançois Tigeot 	POSTING_READ(mmio);
909e3adcf8fSFrançois Tigeot }
910e3adcf8fSFrançois Tigeot 
911e3adcf8fSFrançois Tigeot static int
912e3adcf8fSFrançois Tigeot bsd_ring_flush(struct intel_ring_buffer *ring,
913b5c29a34SFrançois Tigeot 	       u32     invalidate_domains,
914b5c29a34SFrançois Tigeot 	       u32     flush_domains)
915e3adcf8fSFrançois Tigeot {
916e3adcf8fSFrançois Tigeot 	int ret;
917e3adcf8fSFrançois Tigeot 
918e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
919e3adcf8fSFrançois Tigeot 	if (ret)
920e3adcf8fSFrançois Tigeot 		return ret;
921e3adcf8fSFrançois Tigeot 
922e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
923e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
924e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
925e3adcf8fSFrançois Tigeot 	return 0;
926e3adcf8fSFrançois Tigeot }
927e3adcf8fSFrançois Tigeot 
928e3adcf8fSFrançois Tigeot static int
929b5c29a34SFrançois Tigeot i9xx_add_request(struct intel_ring_buffer *ring)
930e3adcf8fSFrançois Tigeot {
931e3adcf8fSFrançois Tigeot 	int ret;
932e3adcf8fSFrançois Tigeot 
933e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
934e3adcf8fSFrançois Tigeot 	if (ret)
935e3adcf8fSFrançois Tigeot 		return ret;
936e3adcf8fSFrançois Tigeot 
937e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
938e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
939b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, ring->outstanding_lazy_request);
940e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
941e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
942e3adcf8fSFrançois Tigeot 
943e3adcf8fSFrançois Tigeot 	return 0;
944e3adcf8fSFrançois Tigeot }
945e3adcf8fSFrançois Tigeot 
946e3adcf8fSFrançois Tigeot static bool
947686a02f1SFrançois Tigeot gen6_ring_get_irq(struct intel_ring_buffer *ring)
948e3adcf8fSFrançois Tigeot {
949e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
950e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
951e3adcf8fSFrançois Tigeot 
952e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
953e3adcf8fSFrançois Tigeot 	       return false;
954e3adcf8fSFrançois Tigeot 
955686a02f1SFrançois Tigeot 	/* It looks like we need to prevent the gt from suspending while waiting
956686a02f1SFrançois Tigeot 	 * for an notifiy irq, otherwise irqs seem to get lost on at least the
957686a02f1SFrançois Tigeot 	 * blt/bsd rings on ivb. */
958e3adcf8fSFrançois Tigeot 	gen6_gt_force_wake_get(dev_priv);
959e3adcf8fSFrançois Tigeot 
96002727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
961e3adcf8fSFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
962686a02f1SFrançois Tigeot 		if (HAS_L3_GPU_CACHE(dev) && ring->id == RCS)
963686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~(ring->irq_enable_mask |
964686a02f1SFrançois Tigeot 						GEN6_RENDER_L3_PARITY_ERROR));
965686a02f1SFrançois Tigeot 		else
966686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
967686a02f1SFrançois Tigeot 		dev_priv->gt_irq_mask &= ~ring->irq_enable_mask;
968686a02f1SFrançois Tigeot 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
969686a02f1SFrançois Tigeot 		POSTING_READ(GTIMR);
970e3adcf8fSFrançois Tigeot 	}
97102727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
972e3adcf8fSFrançois Tigeot 
973e3adcf8fSFrançois Tigeot 	return true;
974e3adcf8fSFrançois Tigeot }
975e3adcf8fSFrançois Tigeot 
976e3adcf8fSFrançois Tigeot static void
977686a02f1SFrançois Tigeot gen6_ring_put_irq(struct intel_ring_buffer *ring)
978e3adcf8fSFrançois Tigeot {
979e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
980e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
981e3adcf8fSFrançois Tigeot 
98202727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
983e3adcf8fSFrançois Tigeot 	if (--ring->irq_refcount == 0) {
984686a02f1SFrançois Tigeot 		if (HAS_L3_GPU_CACHE(dev) && ring->id == RCS)
985686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~GEN6_RENDER_L3_PARITY_ERROR);
986686a02f1SFrançois Tigeot 		else
987686a02f1SFrançois Tigeot 			I915_WRITE_IMR(ring, ~0);
988686a02f1SFrançois Tigeot 		dev_priv->gt_irq_mask |= ring->irq_enable_mask;
989686a02f1SFrançois Tigeot 		I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
990686a02f1SFrançois Tigeot 		POSTING_READ(GTIMR);
991e3adcf8fSFrançois Tigeot 	}
99202727ecdSFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
993e3adcf8fSFrançois Tigeot 
994e3adcf8fSFrançois Tigeot 	gen6_gt_force_wake_put(dev_priv);
995e3adcf8fSFrançois Tigeot }
996e3adcf8fSFrançois Tigeot 
997e3adcf8fSFrançois Tigeot static int
998b5c29a34SFrançois Tigeot i965_dispatch_execbuffer(struct intel_ring_buffer *ring,
999b5c29a34SFrançois Tigeot 			 u32 offset, u32 length,
1000b5c29a34SFrançois Tigeot 			 unsigned flags)
1001e3adcf8fSFrançois Tigeot {
1002e3adcf8fSFrançois Tigeot 	int ret;
1003e3adcf8fSFrançois Tigeot 
1004e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1005e3adcf8fSFrançois Tigeot 	if (ret)
1006e3adcf8fSFrançois Tigeot 		return ret;
1007e3adcf8fSFrançois Tigeot 
1008e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
1009686a02f1SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1010b5c29a34SFrançois Tigeot 			MI_BATCH_GTT |
1011b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
1012e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1013e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1014e3adcf8fSFrançois Tigeot 
1015e3adcf8fSFrançois Tigeot 	return 0;
1016e3adcf8fSFrançois Tigeot }
1017e3adcf8fSFrançois Tigeot 
1018b5c29a34SFrançois Tigeot /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1019b5c29a34SFrançois Tigeot #define I830_BATCH_LIMIT (256*1024)
1020e3adcf8fSFrançois Tigeot static int
1021686a02f1SFrançois Tigeot i830_dispatch_execbuffer(struct intel_ring_buffer *ring,
1022b5c29a34SFrançois Tigeot 				u32 offset, u32 len,
1023b5c29a34SFrançois Tigeot 				unsigned flags)
1024e3adcf8fSFrançois Tigeot {
1025e3adcf8fSFrançois Tigeot 	int ret;
1026e3adcf8fSFrançois Tigeot 
1027b5c29a34SFrançois Tigeot 	if (flags & I915_DISPATCH_PINNED) {
1028e3adcf8fSFrançois Tigeot 		ret = intel_ring_begin(ring, 4);
1029e3adcf8fSFrançois Tigeot 		if (ret)
1030e3adcf8fSFrançois Tigeot 			return ret;
1031e3adcf8fSFrançois Tigeot 
1032e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
1033b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1034e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, offset + len - 8);
1035b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_NOOP);
1036686a02f1SFrançois Tigeot 		intel_ring_advance(ring);
1037b5c29a34SFrançois Tigeot 	} else {
1038b5c29a34SFrançois Tigeot 		struct drm_i915_gem_object *obj = ring->private;
1039b5c29a34SFrançois Tigeot 		u32 cs_offset = obj->gtt_offset;
1040b5c29a34SFrançois Tigeot 
1041b5c29a34SFrançois Tigeot 		if (len > I830_BATCH_LIMIT)
1042b5c29a34SFrançois Tigeot 			return -ENOSPC;
1043b5c29a34SFrançois Tigeot 
1044b5c29a34SFrançois Tigeot 		ret = intel_ring_begin(ring, 9+3);
1045b5c29a34SFrançois Tigeot 		if (ret)
1046b5c29a34SFrançois Tigeot 			return ret;
1047b5c29a34SFrançois Tigeot 		/* Blit the batch (which has now all relocs applied) to the stable batch
1048b5c29a34SFrançois Tigeot 		 * scratch bo area (so that the CS never stumbles over its tlb
1049b5c29a34SFrançois Tigeot 		 * invalidation bug) ... */
1050b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
1051b5c29a34SFrançois Tigeot 				XY_SRC_COPY_BLT_WRITE_ALPHA |
1052b5c29a34SFrançois Tigeot 				XY_SRC_COPY_BLT_WRITE_RGB);
1053b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
1054b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 0);
1055b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
1056b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset);
1057b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 0);
1058b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, 4096);
1059b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, offset);
1060b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_FLUSH);
1061b5c29a34SFrançois Tigeot 
1062b5c29a34SFrançois Tigeot 		/* ... and execute it. */
1063b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
1064b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1065b5c29a34SFrançois Tigeot 		intel_ring_emit(ring, cs_offset + len - 8);
1066b5c29a34SFrançois Tigeot 		intel_ring_advance(ring);
1067b5c29a34SFrançois Tigeot 	}
1068686a02f1SFrançois Tigeot 
1069686a02f1SFrançois Tigeot 	return 0;
1070686a02f1SFrançois Tigeot }
1071686a02f1SFrançois Tigeot 
1072686a02f1SFrançois Tigeot static int
1073686a02f1SFrançois Tigeot i915_dispatch_execbuffer(struct intel_ring_buffer *ring,
1074b5c29a34SFrançois Tigeot 			 u32 offset, u32 len,
1075b5c29a34SFrançois Tigeot 			 unsigned flags)
1076686a02f1SFrançois Tigeot {
1077686a02f1SFrançois Tigeot 	int ret;
1078686a02f1SFrançois Tigeot 
1079e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1080e3adcf8fSFrançois Tigeot 	if (ret)
1081e3adcf8fSFrançois Tigeot 		return ret;
1082e3adcf8fSFrançois Tigeot 
1083686a02f1SFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1084686a02f1SFrançois Tigeot 	intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
1085e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1086e3adcf8fSFrançois Tigeot 
1087e3adcf8fSFrançois Tigeot 	return 0;
1088e3adcf8fSFrançois Tigeot }
1089e3adcf8fSFrançois Tigeot 
1090e3adcf8fSFrançois Tigeot static void cleanup_status_page(struct intel_ring_buffer *ring)
1091e3adcf8fSFrançois Tigeot {
1092e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1093e3adcf8fSFrançois Tigeot 
1094e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
1095e3adcf8fSFrançois Tigeot 	if (obj == NULL)
1096e3adcf8fSFrançois Tigeot 		return;
1097e3adcf8fSFrançois Tigeot 
1098e3adcf8fSFrançois Tigeot 	pmap_qremove((vm_offset_t)ring->status_page.page_addr, 1);
1099e3adcf8fSFrançois Tigeot 	kmem_free(&kernel_map, (vm_offset_t)ring->status_page.page_addr,
1100e3adcf8fSFrançois Tigeot 	    PAGE_SIZE);
1101e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
1102e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1103e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
1104e3adcf8fSFrançois Tigeot }
1105e3adcf8fSFrançois Tigeot 
1106e3adcf8fSFrançois Tigeot static int init_status_page(struct intel_ring_buffer *ring)
1107e3adcf8fSFrançois Tigeot {
1108e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1109e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1110e3adcf8fSFrançois Tigeot 	int ret;
1111e3adcf8fSFrançois Tigeot 
1112e3adcf8fSFrançois Tigeot 	obj = i915_gem_alloc_object(dev, 4096);
1113e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
1114e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate status page\n");
1115e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
1116e3adcf8fSFrançois Tigeot 		goto err;
1117e3adcf8fSFrançois Tigeot 	}
1118e3adcf8fSFrançois Tigeot 
1119e3adcf8fSFrançois Tigeot 	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1120e3adcf8fSFrançois Tigeot 
1121b00bc81cSFrançois Tigeot 	ret = i915_gem_object_pin(obj, 4096, true, false);
1122e3adcf8fSFrançois Tigeot 	if (ret != 0) {
1123e3adcf8fSFrançois Tigeot 		goto err_unref;
1124e3adcf8fSFrançois Tigeot 	}
1125e3adcf8fSFrançois Tigeot 
1126e3adcf8fSFrançois Tigeot 	ring->status_page.gfx_addr = obj->gtt_offset;
1127e3adcf8fSFrançois Tigeot 	ring->status_page.page_addr = (void *)kmem_alloc_nofault(&kernel_map,
1128e3adcf8fSFrançois Tigeot 	    PAGE_SIZE, PAGE_SIZE);
1129e3adcf8fSFrançois Tigeot 	if (ring->status_page.page_addr == NULL) {
1130686a02f1SFrançois Tigeot 		ret = -ENOMEM;
1131e3adcf8fSFrançois Tigeot 		goto err_unpin;
1132e3adcf8fSFrançois Tigeot 	}
1133*a2fdbec6SFrançois Tigeot 	pmap_qenter((vm_offset_t)ring->status_page.page_addr, &obj->pages[0], 1);
1134e3adcf8fSFrançois Tigeot 	pmap_invalidate_cache_range((vm_offset_t)ring->status_page.page_addr,
1135e3adcf8fSFrançois Tigeot 	    (vm_offset_t)ring->status_page.page_addr + PAGE_SIZE);
1136e3adcf8fSFrançois Tigeot 	ring->status_page.obj = obj;
1137e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1138e3adcf8fSFrançois Tigeot 
1139e3adcf8fSFrançois Tigeot 	intel_ring_setup_status_page(ring);
1140b5c29a34SFrançois Tigeot 	DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1141e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1142e3adcf8fSFrançois Tigeot 
1143e3adcf8fSFrançois Tigeot 	return 0;
1144e3adcf8fSFrançois Tigeot 
1145e3adcf8fSFrançois Tigeot err_unpin:
1146e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
1147e3adcf8fSFrançois Tigeot err_unref:
1148e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1149e3adcf8fSFrançois Tigeot err:
1150e3adcf8fSFrançois Tigeot 	return ret;
1151e3adcf8fSFrançois Tigeot }
1152e3adcf8fSFrançois Tigeot 
1153686a02f1SFrançois Tigeot static int init_phys_hws_pga(struct intel_ring_buffer *ring)
1154686a02f1SFrançois Tigeot {
1155686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1156686a02f1SFrançois Tigeot 	u32 addr;
1157686a02f1SFrançois Tigeot 
1158686a02f1SFrançois Tigeot 	if (!dev_priv->status_page_dmah) {
1159686a02f1SFrançois Tigeot 		dev_priv->status_page_dmah =
1160b31e9d59SFrançois Tigeot 			drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1161686a02f1SFrançois Tigeot 		if (!dev_priv->status_page_dmah)
1162686a02f1SFrançois Tigeot 			return -ENOMEM;
1163686a02f1SFrançois Tigeot 	}
1164686a02f1SFrançois Tigeot 
1165686a02f1SFrançois Tigeot 	addr = dev_priv->status_page_dmah->busaddr;
1166686a02f1SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 4)
1167686a02f1SFrançois Tigeot 		addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
1168686a02f1SFrançois Tigeot 	I915_WRITE(HWS_PGA, addr);
1169686a02f1SFrançois Tigeot 
1170686a02f1SFrançois Tigeot 	ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1171686a02f1SFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1172686a02f1SFrançois Tigeot 
1173686a02f1SFrançois Tigeot 	return 0;
1174686a02f1SFrançois Tigeot }
1175686a02f1SFrançois Tigeot 
1176b030f26bSFrançois Tigeot static int intel_init_ring_buffer(struct drm_device *dev,
1177e3adcf8fSFrançois Tigeot 				  struct intel_ring_buffer *ring)
1178e3adcf8fSFrançois Tigeot {
1179e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1180e3adcf8fSFrançois Tigeot 	int ret;
1181e3adcf8fSFrançois Tigeot 
1182e3adcf8fSFrançois Tigeot 	ring->dev = dev;
1183e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1184e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
1185686a02f1SFrançois Tigeot 	ring->size = 32 * PAGE_SIZE;
1186686a02f1SFrançois Tigeot 	memset(ring->sync_seqno, 0, sizeof(ring->sync_seqno));
1187e3adcf8fSFrançois Tigeot 
1188b030f26bSFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
1189b030f26bSFrançois Tigeot 
1190e3adcf8fSFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
1191e3adcf8fSFrançois Tigeot 		ret = init_status_page(ring);
1192e3adcf8fSFrançois Tigeot 		if (ret)
1193e3adcf8fSFrançois Tigeot 			return ret;
1194686a02f1SFrançois Tigeot 	} else {
1195686a02f1SFrançois Tigeot 		BUG_ON(ring->id != RCS);
1196686a02f1SFrançois Tigeot 		ret = init_phys_hws_pga(ring);
1197686a02f1SFrançois Tigeot 		if (ret)
1198686a02f1SFrançois Tigeot 			return ret;
1199e3adcf8fSFrançois Tigeot 	}
1200e3adcf8fSFrançois Tigeot 
1201*a2fdbec6SFrançois Tigeot 	obj = NULL;
1202*a2fdbec6SFrançois Tigeot 	if (!HAS_LLC(dev))
1203*a2fdbec6SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, ring->size);
1204*a2fdbec6SFrançois Tigeot 	if (obj == NULL)
1205e3adcf8fSFrançois Tigeot 		obj = i915_gem_alloc_object(dev, ring->size);
1206e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
1207e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate ringbuffer\n");
1208e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
1209e3adcf8fSFrançois Tigeot 		goto err_hws;
1210e3adcf8fSFrançois Tigeot 	}
1211e3adcf8fSFrançois Tigeot 
1212e3adcf8fSFrançois Tigeot 	ring->obj = obj;
1213e3adcf8fSFrançois Tigeot 
1214b00bc81cSFrançois Tigeot 	ret = i915_gem_object_pin(obj, PAGE_SIZE, true, false);
1215e3adcf8fSFrançois Tigeot 	if (ret)
1216e3adcf8fSFrançois Tigeot 		goto err_unref;
1217e3adcf8fSFrançois Tigeot 
1218686a02f1SFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
1219686a02f1SFrançois Tigeot 	if (ret)
1220686a02f1SFrançois Tigeot 		goto err_unpin;
1221e3adcf8fSFrançois Tigeot 
1222*a2fdbec6SFrançois Tigeot 	ring->virtual_start =
1223*a2fdbec6SFrançois Tigeot 		ioremap_wc(dev->agp->base + obj->gtt_offset,
1224686a02f1SFrançois Tigeot 			   ring->size);
1225686a02f1SFrançois Tigeot 	if (ring->virtual_start == NULL) {
1226e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to map ringbuffer.\n");
1227e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1228e3adcf8fSFrançois Tigeot 		goto err_unpin;
1229e3adcf8fSFrançois Tigeot 	}
1230e3adcf8fSFrançois Tigeot 
1231e3adcf8fSFrançois Tigeot 	ret = ring->init(ring);
1232e3adcf8fSFrançois Tigeot 	if (ret)
1233e3adcf8fSFrançois Tigeot 		goto err_unmap;
1234e3adcf8fSFrançois Tigeot 
1235e3adcf8fSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
1236e3adcf8fSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
1237e3adcf8fSFrançois Tigeot 	 * of the buffer.
1238e3adcf8fSFrançois Tigeot 	 */
1239e3adcf8fSFrançois Tigeot 	ring->effective_size = ring->size;
1240e3adcf8fSFrançois Tigeot 	if (IS_I830(ring->dev) || IS_845G(ring->dev))
1241e3adcf8fSFrançois Tigeot 		ring->effective_size -= 128;
1242e3adcf8fSFrançois Tigeot 
1243e3adcf8fSFrançois Tigeot 	return 0;
1244e3adcf8fSFrançois Tigeot 
1245e3adcf8fSFrançois Tigeot err_unmap:
1246686a02f1SFrançois Tigeot 	pmap_unmapdev((vm_offset_t)ring->virtual_start, ring->size);
1247e3adcf8fSFrançois Tigeot err_unpin:
1248e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
1249e3adcf8fSFrançois Tigeot err_unref:
1250e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1251e3adcf8fSFrançois Tigeot 	ring->obj = NULL;
1252e3adcf8fSFrançois Tigeot err_hws:
1253e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1254e3adcf8fSFrançois Tigeot 	return ret;
1255e3adcf8fSFrançois Tigeot }
1256e3adcf8fSFrançois Tigeot 
1257e3adcf8fSFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
1258e3adcf8fSFrançois Tigeot {
1259e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv;
1260e3adcf8fSFrançois Tigeot 	int ret;
1261e3adcf8fSFrançois Tigeot 
1262e3adcf8fSFrançois Tigeot 	if (ring->obj == NULL)
1263e3adcf8fSFrançois Tigeot 		return;
1264e3adcf8fSFrançois Tigeot 
1265e3adcf8fSFrançois Tigeot 	/* Disable the ring buffer. The ring must be idle at this point */
1266e3adcf8fSFrançois Tigeot 	dev_priv = ring->dev->dev_private;
1267b030f26bSFrançois Tigeot 	ret = intel_ring_idle(ring);
1268b030f26bSFrançois Tigeot 	if (ret)
1269b030f26bSFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
1270b030f26bSFrançois Tigeot 			  ring->name, ret);
1271b030f26bSFrançois Tigeot 
1272e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
1273e3adcf8fSFrançois Tigeot 
1274686a02f1SFrançois Tigeot 	pmap_unmapdev((vm_offset_t)ring->virtual_start, ring->size);
1275e3adcf8fSFrançois Tigeot 
1276e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(ring->obj);
1277e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&ring->obj->base);
1278e3adcf8fSFrançois Tigeot 	ring->obj = NULL;
1279e3adcf8fSFrançois Tigeot 
1280e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
1281e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
1282e3adcf8fSFrançois Tigeot 
1283e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1284e3adcf8fSFrançois Tigeot }
1285e3adcf8fSFrançois Tigeot 
1286e3adcf8fSFrançois Tigeot static int intel_ring_wait_seqno(struct intel_ring_buffer *ring, u32 seqno)
1287e3adcf8fSFrançois Tigeot {
1288e3adcf8fSFrançois Tigeot 	int ret;
1289e3adcf8fSFrançois Tigeot 
1290b030f26bSFrançois Tigeot 	ret = i915_wait_seqno(ring, seqno);
1291b030f26bSFrançois Tigeot 	if (!ret)
1292b030f26bSFrançois Tigeot 		i915_gem_retire_requests_ring(ring);
1293e3adcf8fSFrançois Tigeot 
1294e3adcf8fSFrançois Tigeot 	return ret;
1295e3adcf8fSFrançois Tigeot }
1296e3adcf8fSFrançois Tigeot 
1297e3adcf8fSFrançois Tigeot static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
1298e3adcf8fSFrançois Tigeot {
1299e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
1300e3adcf8fSFrançois Tigeot 	u32 seqno = 0;
1301e3adcf8fSFrançois Tigeot 	int ret;
1302e3adcf8fSFrançois Tigeot 
1303e3adcf8fSFrançois Tigeot 	i915_gem_retire_requests_ring(ring);
1304e3adcf8fSFrançois Tigeot 
1305e3adcf8fSFrançois Tigeot 	if (ring->last_retired_head != -1) {
1306e3adcf8fSFrançois Tigeot 		ring->head = ring->last_retired_head;
1307e3adcf8fSFrançois Tigeot 		ring->last_retired_head = -1;
1308e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
1309e3adcf8fSFrançois Tigeot 		if (ring->space >= n)
1310e3adcf8fSFrançois Tigeot 			return 0;
1311e3adcf8fSFrançois Tigeot 	}
1312e3adcf8fSFrançois Tigeot 
1313e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
1314e3adcf8fSFrançois Tigeot 		int space;
1315e3adcf8fSFrançois Tigeot 
1316e3adcf8fSFrançois Tigeot 		if (request->tail == -1)
1317e3adcf8fSFrançois Tigeot 			continue;
1318e3adcf8fSFrançois Tigeot 
1319b5c29a34SFrançois Tigeot 		space = request->tail - (ring->tail + I915_RING_FREE_SPACE);
1320e3adcf8fSFrançois Tigeot 		if (space < 0)
1321e3adcf8fSFrançois Tigeot 			space += ring->size;
1322e3adcf8fSFrançois Tigeot 		if (space >= n) {
1323e3adcf8fSFrançois Tigeot 			seqno = request->seqno;
1324e3adcf8fSFrançois Tigeot 			break;
1325e3adcf8fSFrançois Tigeot 		}
1326e3adcf8fSFrançois Tigeot 
1327e3adcf8fSFrançois Tigeot 		/* Consume this request in case we need more space than
1328e3adcf8fSFrançois Tigeot 		 * is available and so need to prevent a race between
1329e3adcf8fSFrançois Tigeot 		 * updating last_retired_head and direct reads of
1330e3adcf8fSFrançois Tigeot 		 * I915_RING_HEAD. It also provides a nice sanity check.
1331e3adcf8fSFrançois Tigeot 		 */
1332e3adcf8fSFrançois Tigeot 		request->tail = -1;
1333e3adcf8fSFrançois Tigeot 	}
1334e3adcf8fSFrançois Tigeot 
1335e3adcf8fSFrançois Tigeot 	if (seqno == 0)
1336e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1337e3adcf8fSFrançois Tigeot 
1338e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_seqno(ring, seqno);
1339e3adcf8fSFrançois Tigeot 	if (ret)
1340e3adcf8fSFrançois Tigeot 		return ret;
1341e3adcf8fSFrançois Tigeot 
1342b5c29a34SFrançois Tigeot 	if (WARN_ON(ring->last_retired_head == -1))
1343e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1344e3adcf8fSFrançois Tigeot 
1345e3adcf8fSFrançois Tigeot 	ring->head = ring->last_retired_head;
1346e3adcf8fSFrançois Tigeot 	ring->last_retired_head = -1;
1347e3adcf8fSFrançois Tigeot 	ring->space = ring_space(ring);
1348b5c29a34SFrançois Tigeot 	if (WARN_ON(ring->space < n))
1349e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1350e3adcf8fSFrançois Tigeot 
1351e3adcf8fSFrançois Tigeot 	return 0;
1352e3adcf8fSFrançois Tigeot }
1353e3adcf8fSFrançois Tigeot 
1354b030f26bSFrançois Tigeot static int ring_wait_for_space(struct intel_ring_buffer *ring, int n)
1355e3adcf8fSFrançois Tigeot {
1356e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1357e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1358245593daSFrançois Tigeot 	unsigned long end;
1359e3adcf8fSFrançois Tigeot 	int ret;
1360e3adcf8fSFrançois Tigeot 
1361e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_request(ring, n);
1362e3adcf8fSFrançois Tigeot 	if (ret != -ENOSPC)
1363e3adcf8fSFrançois Tigeot 		return ret;
1364e3adcf8fSFrançois Tigeot 
1365*a2fdbec6SFrançois Tigeot 	trace_i915_ring_wait_begin(ring);
1366e3adcf8fSFrançois Tigeot 	/* With GEM the hangcheck timer should kick us out of the loop,
1367e3adcf8fSFrançois Tigeot 	 * leaving it early runs the risk of corrupting GEM state (due
1368e3adcf8fSFrançois Tigeot 	 * to running on almost untested codepaths). But on resume
1369e3adcf8fSFrançois Tigeot 	 * timers don't work yet, so prevent a complete hang in that
1370e3adcf8fSFrançois Tigeot 	 * case by choosing an insanely large timeout. */
1371e3440f96SFrançois Tigeot 	end = jiffies + 60 * HZ;
1372245593daSFrançois Tigeot 
1373e3adcf8fSFrançois Tigeot 	do {
1374e3adcf8fSFrançois Tigeot 		ring->head = I915_READ_HEAD(ring);
1375e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
1376e3adcf8fSFrançois Tigeot 		if (ring->space >= n) {
1377*a2fdbec6SFrançois Tigeot 			trace_i915_ring_wait_end(ring);
1378e3adcf8fSFrançois Tigeot 			return 0;
1379e3adcf8fSFrançois Tigeot 		}
1380e3adcf8fSFrançois Tigeot 
1381e3adcf8fSFrançois Tigeot #if 0
1382e3adcf8fSFrançois Tigeot 		if (dev->primary->master) {
1383e3adcf8fSFrançois Tigeot 			struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1384e3adcf8fSFrançois Tigeot 			if (master_priv->sarea_priv)
1385e3adcf8fSFrançois Tigeot 				master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1386e3adcf8fSFrançois Tigeot 		}
1387e3adcf8fSFrançois Tigeot #else
1388e3adcf8fSFrançois Tigeot 		if (dev_priv->sarea_priv)
1389e3adcf8fSFrançois Tigeot 			dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1390e3adcf8fSFrançois Tigeot #endif
1391e3adcf8fSFrançois Tigeot 
1392e3440f96SFrançois Tigeot 		msleep(1);
1393245593daSFrançois Tigeot 
1394*a2fdbec6SFrançois Tigeot 		ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1395*a2fdbec6SFrançois Tigeot 					   dev_priv->mm.interruptible);
1396245593daSFrançois Tigeot 		if (ret)
1397245593daSFrançois Tigeot 			return ret;
1398e3440f96SFrançois Tigeot 	} while (!time_after(jiffies, end));
1399*a2fdbec6SFrançois Tigeot 	trace_i915_ring_wait_end(ring);
1400e3adcf8fSFrançois Tigeot 	return -EBUSY;
1401e3adcf8fSFrançois Tigeot }
1402e3adcf8fSFrançois Tigeot 
1403b030f26bSFrançois Tigeot static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
1404b030f26bSFrançois Tigeot {
1405b030f26bSFrançois Tigeot 	uint32_t __iomem *virt;
1406b030f26bSFrançois Tigeot 	int rem = ring->size - ring->tail;
1407b030f26bSFrançois Tigeot 
1408b030f26bSFrançois Tigeot 	if (ring->space < rem) {
1409b030f26bSFrançois Tigeot 		int ret = ring_wait_for_space(ring, rem);
1410b030f26bSFrançois Tigeot 		if (ret)
1411b030f26bSFrançois Tigeot 			return ret;
1412b030f26bSFrançois Tigeot 	}
1413b030f26bSFrançois Tigeot 
1414b030f26bSFrançois Tigeot 	virt = (unsigned int *)((char *)ring->virtual_start + ring->tail);
1415b030f26bSFrançois Tigeot 	rem /= 4;
1416b030f26bSFrançois Tigeot 	while (rem--)
1417686a02f1SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
1418b030f26bSFrançois Tigeot 
1419b030f26bSFrançois Tigeot 	ring->tail = 0;
1420b030f26bSFrançois Tigeot 	ring->space = ring_space(ring);
1421b030f26bSFrançois Tigeot 
1422b030f26bSFrançois Tigeot 	return 0;
1423b030f26bSFrançois Tigeot }
1424b030f26bSFrançois Tigeot 
1425b030f26bSFrançois Tigeot int intel_ring_idle(struct intel_ring_buffer *ring)
1426b030f26bSFrançois Tigeot {
1427b5c29a34SFrançois Tigeot 	u32 seqno;
1428b5c29a34SFrançois Tigeot 	int ret;
1429b5c29a34SFrançois Tigeot 
1430b5c29a34SFrançois Tigeot 	/* We need to add any requests required to flush the objects and ring */
1431b5c29a34SFrançois Tigeot 	if (ring->outstanding_lazy_request) {
1432b5c29a34SFrançois Tigeot 		ret = i915_add_request(ring, NULL, NULL);
1433b5c29a34SFrançois Tigeot 		if (ret)
1434b5c29a34SFrançois Tigeot 			return ret;
1435b5c29a34SFrançois Tigeot 	}
1436b5c29a34SFrançois Tigeot 
1437b5c29a34SFrançois Tigeot 	/* Wait upon the last request to be completed */
1438b5c29a34SFrançois Tigeot 	if (list_empty(&ring->request_list))
1439b5c29a34SFrançois Tigeot 		return 0;
1440b5c29a34SFrançois Tigeot 
1441b5c29a34SFrançois Tigeot 	seqno = list_entry(ring->request_list.prev,
1442b5c29a34SFrançois Tigeot 			   struct drm_i915_gem_request,
1443b5c29a34SFrançois Tigeot 			   list)->seqno;
1444b5c29a34SFrançois Tigeot 
1445b5c29a34SFrançois Tigeot 	return i915_wait_seqno(ring, seqno);
1446b5c29a34SFrançois Tigeot }
1447b5c29a34SFrançois Tigeot 
1448b5c29a34SFrançois Tigeot static int
1449b5c29a34SFrançois Tigeot intel_ring_alloc_seqno(struct intel_ring_buffer *ring)
1450b5c29a34SFrançois Tigeot {
1451b5c29a34SFrançois Tigeot 	if (ring->outstanding_lazy_request)
1452b5c29a34SFrançois Tigeot 		return 0;
1453b5c29a34SFrançois Tigeot 
1454b5c29a34SFrançois Tigeot 	return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_request);
1455b030f26bSFrançois Tigeot }
1456b030f26bSFrançois Tigeot 
1457*a2fdbec6SFrançois Tigeot static int __intel_ring_begin(struct intel_ring_buffer *ring,
1458*a2fdbec6SFrançois Tigeot 			      int bytes)
1459*a2fdbec6SFrançois Tigeot {
1460*a2fdbec6SFrançois Tigeot 	int ret;
1461*a2fdbec6SFrançois Tigeot 
1462*a2fdbec6SFrançois Tigeot 	if (unlikely(ring->tail + bytes > ring->effective_size)) {
1463*a2fdbec6SFrançois Tigeot 		ret = intel_wrap_ring_buffer(ring);
1464*a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
1465*a2fdbec6SFrançois Tigeot 			return ret;
1466*a2fdbec6SFrançois Tigeot 	}
1467*a2fdbec6SFrançois Tigeot 
1468*a2fdbec6SFrançois Tigeot 	if (unlikely(ring->space < bytes)) {
1469*a2fdbec6SFrançois Tigeot 		ret = ring_wait_for_space(ring, bytes);
1470*a2fdbec6SFrançois Tigeot 		if (unlikely(ret))
1471*a2fdbec6SFrançois Tigeot 			return ret;
1472*a2fdbec6SFrançois Tigeot 	}
1473*a2fdbec6SFrançois Tigeot 
1474*a2fdbec6SFrançois Tigeot 	ring->space -= bytes;
1475*a2fdbec6SFrançois Tigeot 	return 0;
1476*a2fdbec6SFrançois Tigeot }
1477*a2fdbec6SFrançois Tigeot 
1478e3adcf8fSFrançois Tigeot int intel_ring_begin(struct intel_ring_buffer *ring,
1479e3adcf8fSFrançois Tigeot 		     int num_dwords)
1480e3adcf8fSFrançois Tigeot {
1481b5c29a34SFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1482e3adcf8fSFrançois Tigeot 	int ret;
1483e3adcf8fSFrançois Tigeot 
1484*a2fdbec6SFrançois Tigeot 	ret = i915_gem_check_wedge(&dev_priv->gpu_error,
1485*a2fdbec6SFrançois Tigeot 				   dev_priv->mm.interruptible);
1486245593daSFrançois Tigeot 	if (ret)
1487245593daSFrançois Tigeot 		return ret;
1488e3adcf8fSFrançois Tigeot 
1489b5c29a34SFrançois Tigeot 	/* Preallocate the olr before touching the ring */
1490b5c29a34SFrançois Tigeot 	ret = intel_ring_alloc_seqno(ring);
1491b5c29a34SFrançois Tigeot 	if (ret)
1492b5c29a34SFrançois Tigeot 		return ret;
1493b5c29a34SFrançois Tigeot 
1494*a2fdbec6SFrançois Tigeot 	return __intel_ring_begin(ring, num_dwords * sizeof(uint32_t));
1495e3adcf8fSFrançois Tigeot }
1496e3adcf8fSFrançois Tigeot 
1497*a2fdbec6SFrançois Tigeot void intel_ring_init_seqno(struct intel_ring_buffer *ring, u32 seqno)
1498*a2fdbec6SFrançois Tigeot {
1499*a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1500*a2fdbec6SFrançois Tigeot 
1501*a2fdbec6SFrançois Tigeot 	BUG_ON(ring->outstanding_lazy_request);
1502*a2fdbec6SFrançois Tigeot 
1503*a2fdbec6SFrançois Tigeot 	if (INTEL_INFO(ring->dev)->gen >= 6) {
1504*a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
1505*a2fdbec6SFrançois Tigeot 		I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
1506e3adcf8fSFrançois Tigeot 	}
1507e3adcf8fSFrançois Tigeot 
1508*a2fdbec6SFrançois Tigeot 	ring->set_seqno(ring, seqno);
1509e3adcf8fSFrançois Tigeot }
1510e3adcf8fSFrançois Tigeot 
1511e3adcf8fSFrançois Tigeot void intel_ring_advance(struct intel_ring_buffer *ring)
1512e3adcf8fSFrançois Tigeot {
1513686a02f1SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1514686a02f1SFrançois Tigeot 
1515e3adcf8fSFrançois Tigeot 	ring->tail &= ring->size - 1;
1516*a2fdbec6SFrançois Tigeot 	if (dev_priv->gpu_error.stop_rings & intel_ring_flag(ring))
1517686a02f1SFrançois Tigeot 		return;
1518e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, ring->tail);
1519e3adcf8fSFrançois Tigeot }
1520e3adcf8fSFrançois Tigeot 
1521b5c29a34SFrançois Tigeot 
1522e3adcf8fSFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
1523f4e1c372SFrançois Tigeot 				     u32 value)
1524e3adcf8fSFrançois Tigeot {
1525e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1526e3adcf8fSFrançois Tigeot 
1527e3adcf8fSFrançois Tigeot        /* Every tail move must follow the sequence below */
1528f4e1c372SFrançois Tigeot 
1529f4e1c372SFrançois Tigeot 	/* Disable notification that the ring is IDLE. The GT
1530f4e1c372SFrançois Tigeot 	 * will then assume that it is busy and bring it out of rc6.
1531f4e1c372SFrançois Tigeot 	 */
1532e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1533f4e1c372SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1534e3adcf8fSFrançois Tigeot 
1535f4e1c372SFrançois Tigeot 	/* Clear the context id. Here be magic! */
1536f4e1c372SFrançois Tigeot 	I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1537e3adcf8fSFrançois Tigeot 
1538f4e1c372SFrançois Tigeot 	/* Wait for the ring not to be idle, i.e. for it to wake up. */
1539f4e1c372SFrançois Tigeot 	if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1540f4e1c372SFrançois Tigeot 		      GEN6_BSD_SLEEP_INDICATOR) == 0,
1541f4e1c372SFrançois Tigeot 		     50))
1542f4e1c372SFrançois Tigeot 		DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
1543f4e1c372SFrançois Tigeot 
1544f4e1c372SFrançois Tigeot 	/* Now that the ring is fully powered up, update the tail */
1545e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
1546f4e1c372SFrançois Tigeot 	POSTING_READ(RING_TAIL(ring->mmio_base));
1547f4e1c372SFrançois Tigeot 
1548f4e1c372SFrançois Tigeot 	/* Let the ring send IDLE messages to the GT again,
1549f4e1c372SFrançois Tigeot 	 * and so let it sleep to conserve power when idle.
1550f4e1c372SFrançois Tigeot 	 */
1551e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1552f4e1c372SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1553e3adcf8fSFrançois Tigeot }
1554e3adcf8fSFrançois Tigeot 
1555e3adcf8fSFrançois Tigeot static int gen6_ring_flush(struct intel_ring_buffer *ring,
1556b5c29a34SFrançois Tigeot 			   u32 invalidate, u32 flush)
1557e3adcf8fSFrançois Tigeot {
1558e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1559e3adcf8fSFrançois Tigeot 	int ret;
1560e3adcf8fSFrançois Tigeot 
1561e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1562e3adcf8fSFrançois Tigeot 	if (ret)
1563e3adcf8fSFrançois Tigeot 		return ret;
1564e3adcf8fSFrançois Tigeot 
1565e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
1566b5c29a34SFrançois Tigeot 	/*
1567b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.5 - video engine command streamer:
1568b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
1569b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
1570b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
1571b5c29a34SFrançois Tigeot 	 */
1572e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
1573b5c29a34SFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1574b5c29a34SFrançois Tigeot 			MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1575e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1576b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
1577e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1578e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1579e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1580e3adcf8fSFrançois Tigeot 	return 0;
1581e3adcf8fSFrançois Tigeot }
1582e3adcf8fSFrançois Tigeot 
1583e3adcf8fSFrançois Tigeot static int
1584b5c29a34SFrançois Tigeot hsw_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
1585b5c29a34SFrançois Tigeot 			      u32 offset, u32 len,
1586b5c29a34SFrançois Tigeot 			      unsigned flags)
1587e3adcf8fSFrançois Tigeot {
1588e3adcf8fSFrançois Tigeot 	int ret;
1589e3adcf8fSFrançois Tigeot 
1590e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1591e3adcf8fSFrançois Tigeot 	if (ret)
1592e3adcf8fSFrançois Tigeot 		return ret;
1593e3adcf8fSFrançois Tigeot 
1594b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
1595b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
1596b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
1597b5c29a34SFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1598b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, offset);
1599b5c29a34SFrançois Tigeot 	intel_ring_advance(ring);
1600b5c29a34SFrançois Tigeot 
1601b5c29a34SFrançois Tigeot 	return 0;
1602b5c29a34SFrançois Tigeot }
1603b5c29a34SFrançois Tigeot 
1604b5c29a34SFrançois Tigeot static int
1605b5c29a34SFrançois Tigeot gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
1606b5c29a34SFrançois Tigeot 			      u32 offset, u32 len,
1607b5c29a34SFrançois Tigeot 			      unsigned flags)
1608b5c29a34SFrançois Tigeot {
1609b5c29a34SFrançois Tigeot 	int ret;
1610b5c29a34SFrançois Tigeot 
1611b5c29a34SFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1612b5c29a34SFrançois Tigeot 	if (ret)
1613b5c29a34SFrançois Tigeot 		return ret;
1614b5c29a34SFrançois Tigeot 
1615b5c29a34SFrançois Tigeot 	intel_ring_emit(ring,
1616b5c29a34SFrançois Tigeot 			MI_BATCH_BUFFER_START |
1617b5c29a34SFrançois Tigeot 			(flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
1618e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1619e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1620e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1621e3adcf8fSFrançois Tigeot 
1622e3adcf8fSFrançois Tigeot 	return 0;
1623e3adcf8fSFrançois Tigeot }
1624e3adcf8fSFrançois Tigeot 
1625e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
1626e3adcf8fSFrançois Tigeot 
1627e3adcf8fSFrançois Tigeot static int blt_ring_flush(struct intel_ring_buffer *ring,
1628b5c29a34SFrançois Tigeot 			  u32 invalidate, u32 flush)
1629e3adcf8fSFrançois Tigeot {
1630e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1631e3adcf8fSFrançois Tigeot 	int ret;
1632e3adcf8fSFrançois Tigeot 
1633e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1634e3adcf8fSFrançois Tigeot 	if (ret)
1635e3adcf8fSFrançois Tigeot 		return ret;
1636e3adcf8fSFrançois Tigeot 
1637e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
1638b5c29a34SFrançois Tigeot 	/*
1639b5c29a34SFrançois Tigeot 	 * Bspec vol 1c.3 - blitter engine command streamer:
1640b5c29a34SFrançois Tigeot 	 * "If ENABLED, all TLBs will be invalidated once the flush
1641b5c29a34SFrançois Tigeot 	 * operation is complete. This bit is only valid when the
1642b5c29a34SFrançois Tigeot 	 * Post-Sync Operation field is a value of 1h or 3h."
1643b5c29a34SFrançois Tigeot 	 */
1644e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
1645b5c29a34SFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
1646b5c29a34SFrançois Tigeot 			MI_FLUSH_DW_OP_STOREDW;
1647e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1648b5c29a34SFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
1649e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1650e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1651e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1652e3adcf8fSFrançois Tigeot 	return 0;
1653e3adcf8fSFrançois Tigeot }
1654e3adcf8fSFrançois Tigeot 
1655e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
1656e3adcf8fSFrançois Tigeot {
1657e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1658ad50ea93SFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1659e3adcf8fSFrançois Tigeot 
1660686a02f1SFrançois Tigeot 	ring->name = "render ring";
1661686a02f1SFrançois Tigeot 	ring->id = RCS;
1662686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
1663686a02f1SFrançois Tigeot 
1664e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
1665e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
1666b5c29a34SFrançois Tigeot 		ring->flush = gen7_render_ring_flush;
1667b5c29a34SFrançois Tigeot 		if (INTEL_INFO(dev)->gen == 6)
1668e3adcf8fSFrançois Tigeot 			ring->flush = gen6_render_ring_flush;
1669686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
1670686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
1671686a02f1SFrançois Tigeot 		ring->irq_enable_mask = GT_USER_INTERRUPT;
1672e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
1673*a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
1674686a02f1SFrançois Tigeot 		ring->sync_to = gen6_ring_sync;
1675686a02f1SFrançois Tigeot 		ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_INVALID;
1676686a02f1SFrançois Tigeot 		ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_RV;
1677686a02f1SFrançois Tigeot 		ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_RB;
1678686a02f1SFrançois Tigeot 		ring->signal_mbox[0] = GEN6_VRSYNC;
1679686a02f1SFrançois Tigeot 		ring->signal_mbox[1] = GEN6_BRSYNC;
1680e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
1681e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
1682686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
1683e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
1684*a2fdbec6SFrançois Tigeot 		ring->set_seqno = pc_render_set_seqno;
1685686a02f1SFrançois Tigeot 		ring->irq_get = gen5_ring_get_irq;
1686686a02f1SFrançois Tigeot 		ring->irq_put = gen5_ring_put_irq;
1687686a02f1SFrançois Tigeot 		ring->irq_enable_mask = GT_USER_INTERRUPT | GT_PIPE_NOTIFY;
1688686a02f1SFrançois Tigeot 	} else {
1689686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
1690686a02f1SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 4)
1691686a02f1SFrançois Tigeot 			ring->flush = gen2_render_ring_flush;
1692686a02f1SFrançois Tigeot 		else
1693686a02f1SFrançois Tigeot 			ring->flush = gen4_render_ring_flush;
1694686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
1695*a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
1696686a02f1SFrançois Tigeot 		if (IS_GEN2(dev)) {
1697686a02f1SFrançois Tigeot 			ring->irq_get = i8xx_ring_get_irq;
1698686a02f1SFrançois Tigeot 			ring->irq_put = i8xx_ring_put_irq;
1699686a02f1SFrançois Tigeot 		} else {
1700686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
1701686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
1702e3adcf8fSFrançois Tigeot 		}
1703686a02f1SFrançois Tigeot 		ring->irq_enable_mask = I915_USER_INTERRUPT;
1704686a02f1SFrançois Tigeot 	}
1705686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
1706b5c29a34SFrançois Tigeot 	if (IS_HASWELL(dev))
1707b5c29a34SFrançois Tigeot 		ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
1708b5c29a34SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 6)
1709686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1710686a02f1SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 4)
1711686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1712686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
1713686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1714686a02f1SFrançois Tigeot 	else
1715686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
1716686a02f1SFrançois Tigeot 	ring->init = init_render_ring;
1717686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
1718e3adcf8fSFrançois Tigeot 
1719b5c29a34SFrançois Tigeot 	/* Workaround batchbuffer to combat CS tlb bug. */
1720b5c29a34SFrançois Tigeot 	if (HAS_BROKEN_CS_TLB(dev)) {
1721b5c29a34SFrançois Tigeot 		struct drm_i915_gem_object *obj;
1722b5c29a34SFrançois Tigeot 		int ret;
1723b5c29a34SFrançois Tigeot 
1724b5c29a34SFrançois Tigeot 		obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
1725b5c29a34SFrançois Tigeot 		if (obj == NULL) {
1726b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to allocate batch bo\n");
1727b5c29a34SFrançois Tigeot 			return -ENOMEM;
1728b5c29a34SFrançois Tigeot 		}
1729b5c29a34SFrançois Tigeot 
1730b00bc81cSFrançois Tigeot 		ret = i915_gem_object_pin(obj, 0, true, false);
1731b5c29a34SFrançois Tigeot 		if (ret != 0) {
1732b5c29a34SFrançois Tigeot 			drm_gem_object_unreference(&obj->base);
1733b5c29a34SFrançois Tigeot 			DRM_ERROR("Failed to ping batch bo\n");
1734b5c29a34SFrançois Tigeot 			return ret;
1735b5c29a34SFrançois Tigeot 		}
1736b5c29a34SFrançois Tigeot 
1737b5c29a34SFrançois Tigeot 		ring->private = obj;
1738e3adcf8fSFrançois Tigeot 	}
1739e3adcf8fSFrançois Tigeot 
1740e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1741e3adcf8fSFrançois Tigeot }
1742e3adcf8fSFrançois Tigeot 
1743686a02f1SFrançois Tigeot int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1744e3adcf8fSFrançois Tigeot {
1745e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1746ad50ea93SFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1747b5c29a34SFrançois Tigeot 	int ret;
1748e3adcf8fSFrançois Tigeot 
1749686a02f1SFrançois Tigeot 	ring->name = "render ring";
1750686a02f1SFrançois Tigeot 	ring->id = RCS;
1751686a02f1SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
1752686a02f1SFrançois Tigeot 
1753e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
1754686a02f1SFrançois Tigeot 		/* non-kms not supported on gen6+ */
1755686a02f1SFrançois Tigeot 		return -ENODEV;
1756e3adcf8fSFrançois Tigeot 	}
1757e3adcf8fSFrançois Tigeot 
1758686a02f1SFrançois Tigeot 	/* Note: gem is not supported on gen5/ilk without kms (the corresponding
1759686a02f1SFrançois Tigeot 	 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
1760686a02f1SFrançois Tigeot 	 * the special gen5 functions. */
1761686a02f1SFrançois Tigeot 	ring->add_request = i9xx_add_request;
1762686a02f1SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 4)
1763686a02f1SFrançois Tigeot 		ring->flush = gen2_render_ring_flush;
1764686a02f1SFrançois Tigeot 	else
1765686a02f1SFrançois Tigeot 		ring->flush = gen4_render_ring_flush;
1766686a02f1SFrançois Tigeot 	ring->get_seqno = ring_get_seqno;
1767*a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
1768686a02f1SFrançois Tigeot 	if (IS_GEN2(dev)) {
1769686a02f1SFrançois Tigeot 		ring->irq_get = i8xx_ring_get_irq;
1770686a02f1SFrançois Tigeot 		ring->irq_put = i8xx_ring_put_irq;
1771686a02f1SFrançois Tigeot 	} else {
1772686a02f1SFrançois Tigeot 		ring->irq_get = i9xx_ring_get_irq;
1773686a02f1SFrançois Tigeot 		ring->irq_put = i9xx_ring_put_irq;
1774686a02f1SFrançois Tigeot 	}
1775686a02f1SFrançois Tigeot 	ring->irq_enable_mask = I915_USER_INTERRUPT;
1776686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
1777686a02f1SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 4)
1778686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1779686a02f1SFrançois Tigeot 	else if (IS_I830(dev) || IS_845G(dev))
1780686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1781686a02f1SFrançois Tigeot 	else
1782686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i915_dispatch_execbuffer;
1783686a02f1SFrançois Tigeot 	ring->init = init_render_ring;
1784686a02f1SFrançois Tigeot 	ring->cleanup = render_ring_cleanup;
1785686a02f1SFrançois Tigeot 
1786e3adcf8fSFrançois Tigeot 	ring->dev = dev;
1787e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1788e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
1789e3adcf8fSFrançois Tigeot 
1790e3adcf8fSFrançois Tigeot 	ring->size = size;
1791e3adcf8fSFrançois Tigeot 	ring->effective_size = ring->size;
1792b5c29a34SFrançois Tigeot 	if (IS_I830(ring->dev) || IS_845G(ring->dev))
1793e3adcf8fSFrançois Tigeot 		ring->effective_size -= 128;
1794e3adcf8fSFrançois Tigeot 
1795686a02f1SFrançois Tigeot 	ring->virtual_start = ioremap_wc(start, size);
1796686a02f1SFrançois Tigeot 	if (ring->virtual_start == NULL) {
1797e3adcf8fSFrançois Tigeot 		DRM_ERROR("can not ioremap virtual address for"
1798e3adcf8fSFrançois Tigeot 			  " ring buffer\n");
1799e3adcf8fSFrançois Tigeot 		return -ENOMEM;
1800e3adcf8fSFrançois Tigeot 	}
1801e3adcf8fSFrançois Tigeot 
1802b5c29a34SFrançois Tigeot 	if (!I915_NEED_GFX_HWS(dev)) {
1803b5c29a34SFrançois Tigeot 		ret = init_phys_hws_pga(ring);
1804b5c29a34SFrançois Tigeot 		if (ret)
1805b5c29a34SFrançois Tigeot 			return ret;
1806b5c29a34SFrançois Tigeot 	}
1807b5c29a34SFrançois Tigeot 
1808e3adcf8fSFrançois Tigeot 	return 0;
1809e3adcf8fSFrançois Tigeot }
1810e3adcf8fSFrançois Tigeot 
1811e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
1812e3adcf8fSFrançois Tigeot {
1813e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1814ad50ea93SFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
1815e3adcf8fSFrançois Tigeot 
1816686a02f1SFrançois Tigeot 	ring->name = "bsd ring";
1817686a02f1SFrançois Tigeot 	ring->id = VCS;
1818686a02f1SFrançois Tigeot 
1819686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
1820686a02f1SFrançois Tigeot 	if (IS_GEN6(dev) || IS_GEN7(dev)) {
1821686a02f1SFrançois Tigeot 		ring->mmio_base = GEN6_BSD_RING_BASE;
1822686a02f1SFrançois Tigeot 		/* gen6 bsd needs a special wa for tail updates */
1823686a02f1SFrançois Tigeot 		if (IS_GEN6(dev))
1824686a02f1SFrançois Tigeot 			ring->write_tail = gen6_bsd_ring_write_tail;
1825686a02f1SFrançois Tigeot 		ring->flush = gen6_ring_flush;
1826686a02f1SFrançois Tigeot 		ring->add_request = gen6_add_request;
1827686a02f1SFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
1828*a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
1829686a02f1SFrançois Tigeot 		ring->irq_enable_mask = GEN6_BSD_USER_INTERRUPT;
1830686a02f1SFrançois Tigeot 		ring->irq_get = gen6_ring_get_irq;
1831686a02f1SFrançois Tigeot 		ring->irq_put = gen6_ring_put_irq;
1832686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1833686a02f1SFrançois Tigeot 		ring->sync_to = gen6_ring_sync;
1834686a02f1SFrançois Tigeot 		ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_VR;
1835686a02f1SFrançois Tigeot 		ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_INVALID;
1836686a02f1SFrançois Tigeot 		ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_VB;
1837686a02f1SFrançois Tigeot 		ring->signal_mbox[0] = GEN6_RVSYNC;
1838686a02f1SFrançois Tigeot 		ring->signal_mbox[1] = GEN6_BVSYNC;
1839686a02f1SFrançois Tigeot 	} else {
1840686a02f1SFrançois Tigeot 		ring->mmio_base = BSD_RING_BASE;
1841686a02f1SFrançois Tigeot 		ring->flush = bsd_ring_flush;
1842686a02f1SFrançois Tigeot 		ring->add_request = i9xx_add_request;
1843686a02f1SFrançois Tigeot 		ring->get_seqno = ring_get_seqno;
1844*a2fdbec6SFrançois Tigeot 		ring->set_seqno = ring_set_seqno;
1845686a02f1SFrançois Tigeot 		if (IS_GEN5(dev)) {
1846686a02f1SFrançois Tigeot 			ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
1847686a02f1SFrançois Tigeot 			ring->irq_get = gen5_ring_get_irq;
1848686a02f1SFrançois Tigeot 			ring->irq_put = gen5_ring_put_irq;
1849686a02f1SFrançois Tigeot 		} else {
1850686a02f1SFrançois Tigeot 			ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
1851686a02f1SFrançois Tigeot 			ring->irq_get = i9xx_ring_get_irq;
1852686a02f1SFrançois Tigeot 			ring->irq_put = i9xx_ring_put_irq;
1853686a02f1SFrançois Tigeot 		}
1854686a02f1SFrançois Tigeot 		ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1855686a02f1SFrançois Tigeot 	}
1856686a02f1SFrançois Tigeot 	ring->init = init_ring_common;
1857e3adcf8fSFrançois Tigeot 
1858e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1859e3adcf8fSFrançois Tigeot }
1860e3adcf8fSFrançois Tigeot 
1861e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
1862e3adcf8fSFrançois Tigeot {
1863e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1864ad50ea93SFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
1865e3adcf8fSFrançois Tigeot 
1866686a02f1SFrançois Tigeot 	ring->name = "blitter ring";
1867686a02f1SFrançois Tigeot 	ring->id = BCS;
1868686a02f1SFrançois Tigeot 
1869686a02f1SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
1870686a02f1SFrançois Tigeot 	ring->write_tail = ring_write_tail;
1871686a02f1SFrançois Tigeot 	ring->flush = blt_ring_flush;
1872686a02f1SFrançois Tigeot 	ring->add_request = gen6_add_request;
1873686a02f1SFrançois Tigeot 	ring->get_seqno = gen6_ring_get_seqno;
1874*a2fdbec6SFrançois Tigeot 	ring->set_seqno = ring_set_seqno;
1875686a02f1SFrançois Tigeot 	ring->irq_enable_mask = GEN6_BLITTER_USER_INTERRUPT;
1876686a02f1SFrançois Tigeot 	ring->irq_get = gen6_ring_get_irq;
1877686a02f1SFrançois Tigeot 	ring->irq_put = gen6_ring_put_irq;
1878686a02f1SFrançois Tigeot 	ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1879686a02f1SFrançois Tigeot 	ring->sync_to = gen6_ring_sync;
1880686a02f1SFrançois Tigeot 	ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_BR;
1881686a02f1SFrançois Tigeot 	ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_BV;
1882686a02f1SFrançois Tigeot 	ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_INVALID;
1883686a02f1SFrançois Tigeot 	ring->signal_mbox[0] = GEN6_RBSYNC;
1884686a02f1SFrançois Tigeot 	ring->signal_mbox[1] = GEN6_VBSYNC;
1885686a02f1SFrançois Tigeot 	ring->init = init_ring_common;
1886e3adcf8fSFrançois Tigeot 
1887e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1888e3adcf8fSFrançois Tigeot }
1889b030f26bSFrançois Tigeot 
1890b030f26bSFrançois Tigeot int
1891b030f26bSFrançois Tigeot intel_ring_flush_all_caches(struct intel_ring_buffer *ring)
1892b030f26bSFrançois Tigeot {
1893b030f26bSFrançois Tigeot 	int ret;
1894b030f26bSFrançois Tigeot 
1895b030f26bSFrançois Tigeot 	if (!ring->gpu_caches_dirty)
1896b030f26bSFrançois Tigeot 		return 0;
1897b030f26bSFrançois Tigeot 
1898b030f26bSFrançois Tigeot 	ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
1899b030f26bSFrançois Tigeot 	if (ret)
1900b030f26bSFrançois Tigeot 		return ret;
1901b030f26bSFrançois Tigeot 
1902*a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
1903*a2fdbec6SFrançois Tigeot 
1904b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
1905b030f26bSFrançois Tigeot 	return 0;
1906b030f26bSFrançois Tigeot }
1907b030f26bSFrançois Tigeot 
1908b030f26bSFrançois Tigeot int
1909b030f26bSFrançois Tigeot intel_ring_invalidate_all_caches(struct intel_ring_buffer *ring)
1910b030f26bSFrançois Tigeot {
1911b030f26bSFrançois Tigeot 	uint32_t flush_domains;
1912b030f26bSFrançois Tigeot 	int ret;
1913b030f26bSFrançois Tigeot 
1914b030f26bSFrançois Tigeot 	flush_domains = 0;
1915b030f26bSFrançois Tigeot 	if (ring->gpu_caches_dirty)
1916b030f26bSFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
1917b030f26bSFrançois Tigeot 
1918b030f26bSFrançois Tigeot 	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
1919b030f26bSFrançois Tigeot 	if (ret)
1920b030f26bSFrançois Tigeot 		return ret;
1921b030f26bSFrançois Tigeot 
1922*a2fdbec6SFrançois Tigeot 	trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
1923*a2fdbec6SFrançois Tigeot 
1924b030f26bSFrançois Tigeot 	ring->gpu_caches_dirty = false;
1925b030f26bSFrançois Tigeot 	return 0;
1926b030f26bSFrançois Tigeot }
1927