xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.c (revision 5c6c6f233992d9a8b1765176aeec0a54588e65c7)
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  * $FreeBSD: head/sys/dev/drm2/i915/intel_ringbuffer.c 253709 2013-07-27 16:42:29Z kib $
29e3adcf8fSFrançois Tigeot  */
30e3adcf8fSFrançois Tigeot 
3118e26a6dSFrançois Tigeot #include <drm/drmP.h>
32*5c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
33e3adcf8fSFrançois Tigeot #include "i915_drv.h"
34e3adcf8fSFrançois Tigeot #include "intel_drv.h"
35e3adcf8fSFrançois Tigeot #include "intel_ringbuffer.h"
36e3adcf8fSFrançois Tigeot #include <sys/sched.h>
37e3adcf8fSFrançois Tigeot 
38e3adcf8fSFrançois Tigeot /*
39e3adcf8fSFrançois Tigeot  * 965+ support PIPE_CONTROL commands, which provide finer grained control
40e3adcf8fSFrançois Tigeot  * over cache flushing.
41e3adcf8fSFrançois Tigeot  */
42e3adcf8fSFrançois Tigeot struct pipe_control {
43e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
44e3adcf8fSFrançois Tigeot 	volatile u32 *cpu_page;
45e3adcf8fSFrançois Tigeot 	u32 gtt_offset;
46e3adcf8fSFrançois Tigeot };
47e3adcf8fSFrançois Tigeot 
48e3adcf8fSFrançois Tigeot void
49e3adcf8fSFrançois Tigeot i915_trace_irq_get(struct intel_ring_buffer *ring, uint32_t seqno)
50e3adcf8fSFrançois Tigeot {
51e3adcf8fSFrançois Tigeot 
52e3adcf8fSFrançois Tigeot 	if (ring->trace_irq_seqno == 0) {
53e3adcf8fSFrançois Tigeot 		lockmgr(&ring->irq_lock, LK_EXCLUSIVE);
54e3adcf8fSFrançois Tigeot 		if (ring->irq_get(ring))
55e3adcf8fSFrançois Tigeot 			ring->trace_irq_seqno = seqno;
56e3adcf8fSFrançois Tigeot 		lockmgr(&ring->irq_lock, LK_RELEASE);
57e3adcf8fSFrançois Tigeot 	}
58e3adcf8fSFrançois Tigeot }
59e3adcf8fSFrançois Tigeot 
60e3adcf8fSFrançois Tigeot static inline int ring_space(struct intel_ring_buffer *ring)
61e3adcf8fSFrançois Tigeot {
62e3adcf8fSFrançois Tigeot 	int space = (ring->head & HEAD_ADDR) - (ring->tail + 8);
63e3adcf8fSFrançois Tigeot 	if (space < 0)
64e3adcf8fSFrançois Tigeot 		space += ring->size;
65e3adcf8fSFrançois Tigeot 	return space;
66e3adcf8fSFrançois Tigeot }
67e3adcf8fSFrançois Tigeot 
68e3adcf8fSFrançois Tigeot static int
69e3adcf8fSFrançois Tigeot render_ring_flush(struct intel_ring_buffer *ring,
70e3adcf8fSFrançois Tigeot 		  uint32_t	invalidate_domains,
71e3adcf8fSFrançois Tigeot 		  uint32_t	flush_domains)
72e3adcf8fSFrançois Tigeot {
73e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
74e3adcf8fSFrançois Tigeot 	uint32_t cmd;
75e3adcf8fSFrançois Tigeot 	int ret;
76e3adcf8fSFrançois Tigeot 
77e3adcf8fSFrançois Tigeot 	/*
78e3adcf8fSFrançois Tigeot 	 * read/write caches:
79e3adcf8fSFrançois Tigeot 	 *
80e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
81e3adcf8fSFrançois Tigeot 	 * only flushed if MI_NO_WRITE_FLUSH is unset.  On 965, it is
82e3adcf8fSFrançois Tigeot 	 * also flushed at 2d versus 3d pipeline switches.
83e3adcf8fSFrançois Tigeot 	 *
84e3adcf8fSFrançois Tigeot 	 * read-only caches:
85e3adcf8fSFrançois Tigeot 	 *
86e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
87e3adcf8fSFrançois Tigeot 	 * MI_READ_FLUSH is set, and is always flushed on 965.
88e3adcf8fSFrançois Tigeot 	 *
89e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_COMMAND may not exist?
90e3adcf8fSFrançois Tigeot 	 *
91e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
92e3adcf8fSFrançois Tigeot 	 * invalidated when MI_EXE_FLUSH is set.
93e3adcf8fSFrançois Tigeot 	 *
94e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
95e3adcf8fSFrançois Tigeot 	 * invalidated with every MI_FLUSH.
96e3adcf8fSFrançois Tigeot 	 *
97e3adcf8fSFrançois Tigeot 	 * TLBs:
98e3adcf8fSFrançois Tigeot 	 *
99e3adcf8fSFrançois Tigeot 	 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
100e3adcf8fSFrançois Tigeot 	 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
101e3adcf8fSFrançois Tigeot 	 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
102e3adcf8fSFrançois Tigeot 	 * are flushed at any MI_FLUSH.
103e3adcf8fSFrançois Tigeot 	 */
104e3adcf8fSFrançois Tigeot 
105e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
106e3adcf8fSFrançois Tigeot 	if ((invalidate_domains|flush_domains) &
107e3adcf8fSFrançois Tigeot 	    I915_GEM_DOMAIN_RENDER)
108e3adcf8fSFrançois Tigeot 		cmd &= ~MI_NO_WRITE_FLUSH;
109e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 4) {
110e3adcf8fSFrançois Tigeot 		/*
111e3adcf8fSFrançois Tigeot 		 * On the 965, the sampler cache always gets flushed
112e3adcf8fSFrançois Tigeot 		 * and this bit is reserved.
113e3adcf8fSFrançois Tigeot 		 */
114e3adcf8fSFrançois Tigeot 		if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
115e3adcf8fSFrançois Tigeot 			cmd |= MI_READ_FLUSH;
116e3adcf8fSFrançois Tigeot 	}
117e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
118e3adcf8fSFrançois Tigeot 		cmd |= MI_EXE_FLUSH;
119e3adcf8fSFrançois Tigeot 
120e3adcf8fSFrançois Tigeot 	if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
121e3adcf8fSFrançois Tigeot 	    (IS_G4X(dev) || IS_GEN5(dev)))
122e3adcf8fSFrançois Tigeot 		cmd |= MI_INVALIDATE_ISP;
123e3adcf8fSFrançois Tigeot 
124e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
125e3adcf8fSFrançois Tigeot 	if (ret)
126e3adcf8fSFrançois Tigeot 		return ret;
127e3adcf8fSFrançois Tigeot 
128e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
129e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
130e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
131e3adcf8fSFrançois Tigeot 
132e3adcf8fSFrançois Tigeot 	return 0;
133e3adcf8fSFrançois Tigeot }
134e3adcf8fSFrançois Tigeot 
135e3adcf8fSFrançois Tigeot /**
136e3adcf8fSFrançois Tigeot  * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
137e3adcf8fSFrançois Tigeot  * implementing two workarounds on gen6.  From section 1.4.7.1
138e3adcf8fSFrançois Tigeot  * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
139e3adcf8fSFrançois Tigeot  *
140e3adcf8fSFrançois Tigeot  * [DevSNB-C+{W/A}] Before any depth stall flush (including those
141e3adcf8fSFrançois Tigeot  * produced by non-pipelined state commands), software needs to first
142e3adcf8fSFrançois Tigeot  * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
143e3adcf8fSFrançois Tigeot  * 0.
144e3adcf8fSFrançois Tigeot  *
145e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
146e3adcf8fSFrançois Tigeot  * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
147e3adcf8fSFrançois Tigeot  *
148e3adcf8fSFrançois Tigeot  * And the workaround for these two requires this workaround first:
149e3adcf8fSFrançois Tigeot  *
150e3adcf8fSFrançois Tigeot  * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
151e3adcf8fSFrançois Tigeot  * BEFORE the pipe-control with a post-sync op and no write-cache
152e3adcf8fSFrançois Tigeot  * flushes.
153e3adcf8fSFrançois Tigeot  *
154e3adcf8fSFrançois Tigeot  * And this last workaround is tricky because of the requirements on
155e3adcf8fSFrançois Tigeot  * that bit.  From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
156e3adcf8fSFrançois Tigeot  * volume 2 part 1:
157e3adcf8fSFrançois Tigeot  *
158e3adcf8fSFrançois Tigeot  *     "1 of the following must also be set:
159e3adcf8fSFrançois Tigeot  *      - Render Target Cache Flush Enable ([12] of DW1)
160e3adcf8fSFrançois Tigeot  *      - Depth Cache Flush Enable ([0] of DW1)
161e3adcf8fSFrançois Tigeot  *      - Stall at Pixel Scoreboard ([1] of DW1)
162e3adcf8fSFrançois Tigeot  *      - Depth Stall ([13] of DW1)
163e3adcf8fSFrançois Tigeot  *      - Post-Sync Operation ([13] of DW1)
164e3adcf8fSFrançois Tigeot  *      - Notify Enable ([8] of DW1)"
165e3adcf8fSFrançois Tigeot  *
166e3adcf8fSFrançois Tigeot  * The cache flushes require the workaround flush that triggered this
167e3adcf8fSFrançois Tigeot  * one, so we can't use it.  Depth stall would trigger the same.
168e3adcf8fSFrançois Tigeot  * Post-sync nonzero is what triggered this second workaround, so we
169e3adcf8fSFrançois Tigeot  * can't use that one either.  Notify enable is IRQs, which aren't
170e3adcf8fSFrançois Tigeot  * really our business.  That leaves only stall at scoreboard.
171e3adcf8fSFrançois Tigeot  */
172e3adcf8fSFrançois Tigeot static int
173e3adcf8fSFrançois Tigeot intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
174e3adcf8fSFrançois Tigeot {
175e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
176e3adcf8fSFrançois Tigeot 	u32 scratch_addr = pc->gtt_offset + 128;
177e3adcf8fSFrançois Tigeot 	int ret;
178e3adcf8fSFrançois Tigeot 
179e3adcf8fSFrançois Tigeot 
180e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
181e3adcf8fSFrançois Tigeot 	if (ret)
182e3adcf8fSFrançois Tigeot 		return ret;
183e3adcf8fSFrançois Tigeot 
184e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
185e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
186e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_STALL_AT_SCOREBOARD);
187e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
188e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* low dword */
189e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* high dword */
190e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
191e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
192e3adcf8fSFrançois Tigeot 
193e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
194e3adcf8fSFrançois Tigeot 	if (ret)
195e3adcf8fSFrançois Tigeot 		return ret;
196e3adcf8fSFrançois Tigeot 
197e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
198e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
199e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
200e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
201e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
202e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
203e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
204e3adcf8fSFrançois Tigeot 
205e3adcf8fSFrançois Tigeot 	return 0;
206e3adcf8fSFrançois Tigeot }
207e3adcf8fSFrançois Tigeot 
208e3adcf8fSFrançois Tigeot static int
209e3adcf8fSFrançois Tigeot gen6_render_ring_flush(struct intel_ring_buffer *ring,
210e3adcf8fSFrançois Tigeot                          u32 invalidate_domains, u32 flush_domains)
211e3adcf8fSFrançois Tigeot {
212e3adcf8fSFrançois Tigeot 	u32 flags = 0;
213e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
214e3adcf8fSFrançois Tigeot 	u32 scratch_addr = pc->gtt_offset + 128;
215e3adcf8fSFrançois Tigeot 	int ret;
216e3adcf8fSFrançois Tigeot 
217e3adcf8fSFrançois Tigeot 	/* Force SNB workarounds for PIPE_CONTROL flushes */
218e3adcf8fSFrançois Tigeot 	intel_emit_post_sync_nonzero_flush(ring);
219e3adcf8fSFrançois Tigeot 
220e3adcf8fSFrançois Tigeot 	/* Just flush everything.  Experiments have shown that reducing the
221e3adcf8fSFrançois Tigeot 	 * number of bits based on the write domains has little performance
222e3adcf8fSFrançois Tigeot 	 * impact.
223e3adcf8fSFrançois Tigeot 	 */
224e3adcf8fSFrançois Tigeot 	flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
225e3adcf8fSFrançois Tigeot 	flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
226e3adcf8fSFrançois Tigeot 	flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
227e3adcf8fSFrançois Tigeot 	flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
228e3adcf8fSFrançois Tigeot 	flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
229e3adcf8fSFrançois Tigeot 	flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
230e3adcf8fSFrançois Tigeot 	flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
231e3adcf8fSFrançois Tigeot 
232e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
233e3adcf8fSFrançois Tigeot 	if (ret)
234e3adcf8fSFrançois Tigeot 		return ret;
235e3adcf8fSFrançois Tigeot 
236e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
237e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, flags);
238e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
239e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* lower dword */
240e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0); /* uppwer dword */
241e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
242e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
243e3adcf8fSFrançois Tigeot 
244e3adcf8fSFrançois Tigeot 	return 0;
245e3adcf8fSFrançois Tigeot }
246e3adcf8fSFrançois Tigeot 
247e3adcf8fSFrançois Tigeot static void ring_write_tail(struct intel_ring_buffer *ring,
248e3adcf8fSFrançois Tigeot 			    uint32_t value)
249e3adcf8fSFrançois Tigeot {
250e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
251e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
252e3adcf8fSFrançois Tigeot }
253e3adcf8fSFrançois Tigeot 
254e3adcf8fSFrançois Tigeot u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
255e3adcf8fSFrançois Tigeot {
256e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
257e3adcf8fSFrançois Tigeot 	uint32_t acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
258e3adcf8fSFrançois Tigeot 			RING_ACTHD(ring->mmio_base) : ACTHD;
259e3adcf8fSFrançois Tigeot 
260e3adcf8fSFrançois Tigeot 	return I915_READ(acthd_reg);
261e3adcf8fSFrançois Tigeot }
262e3adcf8fSFrançois Tigeot 
263e3adcf8fSFrançois Tigeot static int init_ring_common(struct intel_ring_buffer *ring)
264e3adcf8fSFrançois Tigeot {
265e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
266e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj = ring->obj;
267e3adcf8fSFrançois Tigeot 	uint32_t head;
268e3adcf8fSFrançois Tigeot 
269e3adcf8fSFrançois Tigeot 	/* Stop the ring if it's running. */
270e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
271e3adcf8fSFrançois Tigeot 	I915_WRITE_HEAD(ring, 0);
272e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, 0);
273e3adcf8fSFrançois Tigeot 
274e3adcf8fSFrançois Tigeot 	/* Initialize the ring. */
275e3adcf8fSFrançois Tigeot 	I915_WRITE_START(ring, obj->gtt_offset);
276e3adcf8fSFrançois Tigeot 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
277e3adcf8fSFrançois Tigeot 
278e3adcf8fSFrançois Tigeot 	/* G45 ring initialization fails to reset head to zero */
279e3adcf8fSFrançois Tigeot 	if (head != 0) {
280e3adcf8fSFrançois Tigeot 		DRM_DEBUG("%s head not reset to zero "
281e3adcf8fSFrançois Tigeot 			      "ctl %08x head %08x tail %08x start %08x\n",
282e3adcf8fSFrançois Tigeot 			      ring->name,
283e3adcf8fSFrançois Tigeot 			      I915_READ_CTL(ring),
284e3adcf8fSFrançois Tigeot 			      I915_READ_HEAD(ring),
285e3adcf8fSFrançois Tigeot 			      I915_READ_TAIL(ring),
286e3adcf8fSFrançois Tigeot 			      I915_READ_START(ring));
287e3adcf8fSFrançois Tigeot 
288e3adcf8fSFrançois Tigeot 		I915_WRITE_HEAD(ring, 0);
289e3adcf8fSFrançois Tigeot 
290e3adcf8fSFrançois Tigeot 		if (I915_READ_HEAD(ring) & HEAD_ADDR) {
291e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to set %s head to zero "
292e3adcf8fSFrançois Tigeot 				  "ctl %08x head %08x tail %08x start %08x\n",
293e3adcf8fSFrançois Tigeot 				  ring->name,
294e3adcf8fSFrançois Tigeot 				  I915_READ_CTL(ring),
295e3adcf8fSFrançois Tigeot 				  I915_READ_HEAD(ring),
296e3adcf8fSFrançois Tigeot 				  I915_READ_TAIL(ring),
297e3adcf8fSFrançois Tigeot 				  I915_READ_START(ring));
298e3adcf8fSFrançois Tigeot 		}
299e3adcf8fSFrançois Tigeot 	}
300e3adcf8fSFrançois Tigeot 
301e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring,
302e3adcf8fSFrançois Tigeot 			((ring->size - PAGE_SIZE) & RING_NR_PAGES)
303e3adcf8fSFrançois Tigeot 			| RING_VALID);
304e3adcf8fSFrançois Tigeot 
305e3adcf8fSFrançois Tigeot 	/* If the head is still not zero, the ring is dead */
306e3adcf8fSFrançois Tigeot 	if (_intel_wait_for(ring->dev,
307e3adcf8fSFrançois Tigeot 	    (I915_READ_CTL(ring) & RING_VALID) != 0 &&
308e3adcf8fSFrançois Tigeot 	     I915_READ_START(ring) == obj->gtt_offset &&
309e3adcf8fSFrançois Tigeot 	     (I915_READ_HEAD(ring) & HEAD_ADDR) == 0,
310e3adcf8fSFrançois Tigeot 	    50, 1, "915rii")) {
311e3adcf8fSFrançois Tigeot 		DRM_ERROR("%s initialization failed "
312e3adcf8fSFrançois Tigeot 				"ctl %08x head %08x tail %08x start %08x\n",
313e3adcf8fSFrançois Tigeot 				ring->name,
314e3adcf8fSFrançois Tigeot 				I915_READ_CTL(ring),
315e3adcf8fSFrançois Tigeot 				I915_READ_HEAD(ring),
316e3adcf8fSFrançois Tigeot 				I915_READ_TAIL(ring),
317e3adcf8fSFrançois Tigeot 				I915_READ_START(ring));
318e3adcf8fSFrançois Tigeot 		return -EIO;
319e3adcf8fSFrançois Tigeot 	}
320e3adcf8fSFrançois Tigeot 
321e3adcf8fSFrançois Tigeot 	if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
322e3adcf8fSFrançois Tigeot 		i915_kernel_lost_context(ring->dev);
323e3adcf8fSFrançois Tigeot 	else {
324e3adcf8fSFrançois Tigeot 		ring->head = I915_READ_HEAD(ring);
325e3adcf8fSFrançois Tigeot 		ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
326e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
327e3adcf8fSFrançois Tigeot 	}
328e3adcf8fSFrançois Tigeot 
329e3adcf8fSFrançois Tigeot 	return 0;
330e3adcf8fSFrançois Tigeot }
331e3adcf8fSFrançois Tigeot 
332e3adcf8fSFrançois Tigeot static int
333e3adcf8fSFrançois Tigeot init_pipe_control(struct intel_ring_buffer *ring)
334e3adcf8fSFrançois Tigeot {
335e3adcf8fSFrançois Tigeot 	struct pipe_control *pc;
336e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
337e3adcf8fSFrançois Tigeot 	int ret;
338e3adcf8fSFrançois Tigeot 
339e3adcf8fSFrançois Tigeot 	if (ring->private)
340e3adcf8fSFrançois Tigeot 		return 0;
341e3adcf8fSFrançois Tigeot 
342e3adcf8fSFrançois Tigeot 	pc = kmalloc(sizeof(*pc), DRM_I915_GEM, M_WAITOK);
343e3adcf8fSFrançois Tigeot 	if (!pc)
344e3adcf8fSFrançois Tigeot 		return -ENOMEM;
345e3adcf8fSFrançois Tigeot 
346e3adcf8fSFrançois Tigeot 	obj = i915_gem_alloc_object(ring->dev, 4096);
347e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
348e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate seqno page\n");
349e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
350e3adcf8fSFrançois Tigeot 		goto err;
351e3adcf8fSFrançois Tigeot 	}
352e3adcf8fSFrançois Tigeot 
353e3adcf8fSFrançois Tigeot 	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
354e3adcf8fSFrançois Tigeot 
355e3adcf8fSFrançois Tigeot 	ret = i915_gem_object_pin(obj, 4096, true);
356e3adcf8fSFrançois Tigeot 	if (ret)
357e3adcf8fSFrançois Tigeot 		goto err_unref;
358e3adcf8fSFrançois Tigeot 
359e3adcf8fSFrançois Tigeot 	pc->gtt_offset = obj->gtt_offset;
360e3adcf8fSFrançois Tigeot 	pc->cpu_page = (uint32_t *)kmem_alloc_nofault(&kernel_map, PAGE_SIZE, PAGE_SIZE);
361e3adcf8fSFrançois Tigeot 	if (pc->cpu_page == NULL)
362e3adcf8fSFrançois Tigeot 		goto err_unpin;
363e3adcf8fSFrançois Tigeot 	pmap_qenter((uintptr_t)pc->cpu_page, &obj->pages[0], 1);
364e3adcf8fSFrançois Tigeot 	pmap_invalidate_cache_range((vm_offset_t)pc->cpu_page,
365e3adcf8fSFrançois Tigeot 	    (vm_offset_t)pc->cpu_page + PAGE_SIZE);
366e3adcf8fSFrançois Tigeot 
367e3adcf8fSFrançois Tigeot 	pc->obj = obj;
368e3adcf8fSFrançois Tigeot 	ring->private = pc;
369e3adcf8fSFrançois Tigeot 	return 0;
370e3adcf8fSFrançois Tigeot 
371e3adcf8fSFrançois Tigeot err_unpin:
372e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
373e3adcf8fSFrançois Tigeot err_unref:
374e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
375e3adcf8fSFrançois Tigeot err:
376e3adcf8fSFrançois Tigeot 	drm_free(pc, DRM_I915_GEM);
377e3adcf8fSFrançois Tigeot 	return ret;
378e3adcf8fSFrançois Tigeot }
379e3adcf8fSFrançois Tigeot 
380e3adcf8fSFrançois Tigeot static void
381e3adcf8fSFrançois Tigeot cleanup_pipe_control(struct intel_ring_buffer *ring)
382e3adcf8fSFrançois Tigeot {
383e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
384e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
385e3adcf8fSFrançois Tigeot 
386e3adcf8fSFrançois Tigeot 	if (!ring->private)
387e3adcf8fSFrançois Tigeot 		return;
388e3adcf8fSFrançois Tigeot 
389e3adcf8fSFrançois Tigeot 	obj = pc->obj;
390e3adcf8fSFrançois Tigeot 	pmap_qremove((vm_offset_t)pc->cpu_page, 1);
391e3adcf8fSFrançois Tigeot 	kmem_free(&kernel_map, (uintptr_t)pc->cpu_page, PAGE_SIZE);
392e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
393e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
394e3adcf8fSFrançois Tigeot 
395e3adcf8fSFrançois Tigeot 	drm_free(pc, DRM_I915_GEM);
396e3adcf8fSFrançois Tigeot 	ring->private = NULL;
397e3adcf8fSFrançois Tigeot }
398e3adcf8fSFrançois Tigeot 
399e3adcf8fSFrançois Tigeot static int init_render_ring(struct intel_ring_buffer *ring)
400e3adcf8fSFrançois Tigeot {
401e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
402e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
403e3adcf8fSFrançois Tigeot 	int ret = init_ring_common(ring);
404e3adcf8fSFrançois Tigeot 
405e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen > 3) {
406e3adcf8fSFrançois Tigeot 		int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
407e3adcf8fSFrançois Tigeot 		I915_WRITE(MI_MODE, mode);
408e3adcf8fSFrançois Tigeot 		if (IS_GEN7(dev))
409e3adcf8fSFrançois Tigeot 			I915_WRITE(GFX_MODE_GEN7,
410e3adcf8fSFrançois Tigeot 				   GFX_MODE_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
411e3adcf8fSFrançois Tigeot 				   GFX_MODE_ENABLE(GFX_REPLAY_MODE));
412e3adcf8fSFrançois Tigeot 	}
413e3adcf8fSFrançois Tigeot 
414e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 5) {
415e3adcf8fSFrançois Tigeot 		ret = init_pipe_control(ring);
416e3adcf8fSFrançois Tigeot 		if (ret)
417e3adcf8fSFrançois Tigeot 			return ret;
418e3adcf8fSFrançois Tigeot 	}
419e3adcf8fSFrançois Tigeot 
420e3adcf8fSFrançois Tigeot 
421e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev)) {
422e3adcf8fSFrançois Tigeot 		/* From the Sandybridge PRM, volume 1 part 3, page 24:
423e3adcf8fSFrançois Tigeot 		 * "If this bit is set, STCunit will have LRA as replacement
424e3adcf8fSFrançois Tigeot 		 *  policy. [...] This bit must be reset.  LRA replacement
425e3adcf8fSFrançois Tigeot 		 *  policy is not supported."
426e3adcf8fSFrançois Tigeot 		 */
427e3adcf8fSFrançois Tigeot 		I915_WRITE(CACHE_MODE_0,
428e3adcf8fSFrançois Tigeot 			   CM0_STC_EVICT_DISABLE_LRA_SNB << CM0_MASK_SHIFT);
429e3adcf8fSFrançois Tigeot 	}
430e3adcf8fSFrançois Tigeot 
431e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
432e3adcf8fSFrançois Tigeot 		I915_WRITE(INSTPM,
433e3adcf8fSFrançois Tigeot 			   INSTPM_FORCE_ORDERING << 16 | INSTPM_FORCE_ORDERING);
434e3adcf8fSFrançois Tigeot 	}
435e3adcf8fSFrançois Tigeot 
436e3adcf8fSFrançois Tigeot 	return ret;
437e3adcf8fSFrançois Tigeot }
438e3adcf8fSFrançois Tigeot 
439e3adcf8fSFrançois Tigeot static void render_ring_cleanup(struct intel_ring_buffer *ring)
440e3adcf8fSFrançois Tigeot {
441e3adcf8fSFrançois Tigeot 	if (!ring->private)
442e3adcf8fSFrançois Tigeot 		return;
443e3adcf8fSFrançois Tigeot 
444e3adcf8fSFrançois Tigeot 	cleanup_pipe_control(ring);
445e3adcf8fSFrançois Tigeot }
446e3adcf8fSFrançois Tigeot 
447e3adcf8fSFrançois Tigeot static void
448e3adcf8fSFrançois Tigeot update_mboxes(struct intel_ring_buffer *ring,
449e3adcf8fSFrançois Tigeot 	    u32 seqno,
450e3adcf8fSFrançois Tigeot 	    u32 mmio_offset)
451e3adcf8fSFrançois Tigeot {
452e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_SEMAPHORE_MBOX |
453e3adcf8fSFrançois Tigeot 			      MI_SEMAPHORE_GLOBAL_GTT |
454e3adcf8fSFrançois Tigeot 			      MI_SEMAPHORE_REGISTER |
455e3adcf8fSFrançois Tigeot 			      MI_SEMAPHORE_UPDATE);
456e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, seqno);
457e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, mmio_offset);
458e3adcf8fSFrançois Tigeot }
459e3adcf8fSFrançois Tigeot 
460e3adcf8fSFrançois Tigeot /**
461e3adcf8fSFrançois Tigeot  * gen6_add_request - Update the semaphore mailbox registers
462e3adcf8fSFrançois Tigeot  *
463e3adcf8fSFrançois Tigeot  * @ring - ring that is adding a request
464e3adcf8fSFrançois Tigeot  * @seqno - return seqno stuck into the ring
465e3adcf8fSFrançois Tigeot  *
466e3adcf8fSFrançois Tigeot  * Update the mailbox registers in the *other* rings with the current seqno.
467e3adcf8fSFrançois Tigeot  * This acts like a signal in the canonical semaphore.
468e3adcf8fSFrançois Tigeot  */
469e3adcf8fSFrançois Tigeot static int
470e3adcf8fSFrançois Tigeot gen6_add_request(struct intel_ring_buffer *ring,
471e3adcf8fSFrançois Tigeot 		 u32 *seqno)
472e3adcf8fSFrançois Tigeot {
473e3adcf8fSFrançois Tigeot 	u32 mbox1_reg;
474e3adcf8fSFrançois Tigeot 	u32 mbox2_reg;
475e3adcf8fSFrançois Tigeot 	int ret;
476e3adcf8fSFrançois Tigeot 
477e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 10);
478e3adcf8fSFrançois Tigeot 	if (ret)
479e3adcf8fSFrançois Tigeot 		return ret;
480e3adcf8fSFrançois Tigeot 
481e3adcf8fSFrançois Tigeot 	mbox1_reg = ring->signal_mbox[0];
482e3adcf8fSFrançois Tigeot 	mbox2_reg = ring->signal_mbox[1];
483e3adcf8fSFrançois Tigeot 
484e3adcf8fSFrançois Tigeot 	*seqno = i915_gem_next_request_seqno(ring);
485e3adcf8fSFrançois Tigeot 
486e3adcf8fSFrançois Tigeot 	update_mboxes(ring, *seqno, mbox1_reg);
487e3adcf8fSFrançois Tigeot 	update_mboxes(ring, *seqno, mbox2_reg);
488e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
489e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
490e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, *seqno);
491e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
492e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
493e3adcf8fSFrançois Tigeot 
494e3adcf8fSFrançois Tigeot 	return 0;
495e3adcf8fSFrançois Tigeot }
496e3adcf8fSFrançois Tigeot 
497e3adcf8fSFrançois Tigeot /**
498e3adcf8fSFrançois Tigeot  * intel_ring_sync - sync the waiter to the signaller on seqno
499e3adcf8fSFrançois Tigeot  *
500e3adcf8fSFrançois Tigeot  * @waiter - ring that is waiting
501e3adcf8fSFrançois Tigeot  * @signaller - ring which has, or will signal
502e3adcf8fSFrançois Tigeot  * @seqno - seqno which the waiter will block on
503e3adcf8fSFrançois Tigeot  */
504e3adcf8fSFrançois Tigeot static int
505e3adcf8fSFrançois Tigeot intel_ring_sync(struct intel_ring_buffer *waiter,
506e3adcf8fSFrançois Tigeot 		struct intel_ring_buffer *signaller,
507e3adcf8fSFrançois Tigeot 		int ring,
508e3adcf8fSFrançois Tigeot 		u32 seqno)
509e3adcf8fSFrançois Tigeot {
510e3adcf8fSFrançois Tigeot 	int ret;
511e3adcf8fSFrançois Tigeot 	u32 dw1 = MI_SEMAPHORE_MBOX |
512e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_COMPARE |
513e3adcf8fSFrançois Tigeot 		  MI_SEMAPHORE_REGISTER;
514e3adcf8fSFrançois Tigeot 
515e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(waiter, 4);
516e3adcf8fSFrançois Tigeot 	if (ret)
517e3adcf8fSFrançois Tigeot 		return ret;
518e3adcf8fSFrançois Tigeot 
519e3adcf8fSFrançois Tigeot 	intel_ring_emit(waiter, dw1 | signaller->semaphore_register[ring]);
520e3adcf8fSFrançois Tigeot 	intel_ring_emit(waiter, seqno);
521e3adcf8fSFrançois Tigeot 	intel_ring_emit(waiter, 0);
522e3adcf8fSFrançois Tigeot 	intel_ring_emit(waiter, MI_NOOP);
523e3adcf8fSFrançois Tigeot 	intel_ring_advance(waiter);
524e3adcf8fSFrançois Tigeot 
525e3adcf8fSFrançois Tigeot 	return 0;
526e3adcf8fSFrançois Tigeot }
527e3adcf8fSFrançois Tigeot 
528e3adcf8fSFrançois Tigeot int render_ring_sync_to(struct intel_ring_buffer *waiter,
529e3adcf8fSFrançois Tigeot     struct intel_ring_buffer *signaller, u32 seqno);
530e3adcf8fSFrançois Tigeot int gen6_bsd_ring_sync_to(struct intel_ring_buffer *waiter,
531e3adcf8fSFrançois Tigeot     struct intel_ring_buffer *signaller, u32 seqno);
532e3adcf8fSFrançois Tigeot int gen6_blt_ring_sync_to(struct intel_ring_buffer *waiter,
533e3adcf8fSFrançois Tigeot     struct intel_ring_buffer *signaller, u32 seqno);
534e3adcf8fSFrançois Tigeot 
535e3adcf8fSFrançois Tigeot /* VCS->RCS (RVSYNC) or BCS->RCS (RBSYNC) */
536e3adcf8fSFrançois Tigeot int
537e3adcf8fSFrançois Tigeot render_ring_sync_to(struct intel_ring_buffer *waiter,
538e3adcf8fSFrançois Tigeot 		    struct intel_ring_buffer *signaller,
539e3adcf8fSFrançois Tigeot 		    u32 seqno)
540e3adcf8fSFrançois Tigeot {
541e3adcf8fSFrançois Tigeot 	KASSERT(signaller->semaphore_register[RCS] != MI_SEMAPHORE_SYNC_INVALID,
542e3adcf8fSFrançois Tigeot 	    ("valid RCS semaphore"));
543e3adcf8fSFrançois Tigeot 	return intel_ring_sync(waiter,
544e3adcf8fSFrançois Tigeot 			       signaller,
545e3adcf8fSFrançois Tigeot 			       RCS,
546e3adcf8fSFrançois Tigeot 			       seqno);
547e3adcf8fSFrançois Tigeot }
548e3adcf8fSFrançois Tigeot 
549e3adcf8fSFrançois Tigeot /* RCS->VCS (VRSYNC) or BCS->VCS (VBSYNC) */
550e3adcf8fSFrançois Tigeot int
551e3adcf8fSFrançois Tigeot gen6_bsd_ring_sync_to(struct intel_ring_buffer *waiter,
552e3adcf8fSFrançois Tigeot 		      struct intel_ring_buffer *signaller,
553e3adcf8fSFrançois Tigeot 		      u32 seqno)
554e3adcf8fSFrançois Tigeot {
555e3adcf8fSFrançois Tigeot 	KASSERT(signaller->semaphore_register[VCS] != MI_SEMAPHORE_SYNC_INVALID,
556e3adcf8fSFrançois Tigeot 	    ("Valid VCS semaphore"));
557e3adcf8fSFrançois Tigeot 	return intel_ring_sync(waiter,
558e3adcf8fSFrançois Tigeot 			       signaller,
559e3adcf8fSFrançois Tigeot 			       VCS,
560e3adcf8fSFrançois Tigeot 			       seqno);
561e3adcf8fSFrançois Tigeot }
562e3adcf8fSFrançois Tigeot 
563e3adcf8fSFrançois Tigeot /* RCS->BCS (BRSYNC) or VCS->BCS (BVSYNC) */
564e3adcf8fSFrançois Tigeot int
565e3adcf8fSFrançois Tigeot gen6_blt_ring_sync_to(struct intel_ring_buffer *waiter,
566e3adcf8fSFrançois Tigeot 		      struct intel_ring_buffer *signaller,
567e3adcf8fSFrançois Tigeot 		      u32 seqno)
568e3adcf8fSFrançois Tigeot {
569e3adcf8fSFrançois Tigeot 	KASSERT(signaller->semaphore_register[BCS] != MI_SEMAPHORE_SYNC_INVALID,
570e3adcf8fSFrançois Tigeot 	    ("Valid BCS semaphore"));
571e3adcf8fSFrançois Tigeot 	return intel_ring_sync(waiter,
572e3adcf8fSFrançois Tigeot 			       signaller,
573e3adcf8fSFrançois Tigeot 			       BCS,
574e3adcf8fSFrançois Tigeot 			       seqno);
575e3adcf8fSFrançois Tigeot }
576e3adcf8fSFrançois Tigeot 
577e3adcf8fSFrançois Tigeot #define PIPE_CONTROL_FLUSH(ring__, addr__)					\
578e3adcf8fSFrançois Tigeot do {									\
579e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |		\
580e3adcf8fSFrançois Tigeot 		 PIPE_CONTROL_DEPTH_STALL);				\
581e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT);			\
582e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
583e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring__, 0);							\
584e3adcf8fSFrançois Tigeot } while (0)
585e3adcf8fSFrançois Tigeot 
586e3adcf8fSFrançois Tigeot static int
587e3adcf8fSFrançois Tigeot pc_render_add_request(struct intel_ring_buffer *ring,
588e3adcf8fSFrançois Tigeot 		      uint32_t *result)
589e3adcf8fSFrançois Tigeot {
590e3adcf8fSFrançois Tigeot 	u32 seqno = i915_gem_next_request_seqno(ring);
591e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
592e3adcf8fSFrançois Tigeot 	u32 scratch_addr = pc->gtt_offset + 128;
593e3adcf8fSFrançois Tigeot 	int ret;
594e3adcf8fSFrançois Tigeot 
595e3adcf8fSFrançois Tigeot 	/* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
596e3adcf8fSFrançois Tigeot 	 * incoherent with writes to memory, i.e. completely fubar,
597e3adcf8fSFrançois Tigeot 	 * so we need to use PIPE_NOTIFY instead.
598e3adcf8fSFrançois Tigeot 	 *
599e3adcf8fSFrançois Tigeot 	 * However, we also need to workaround the qword write
600e3adcf8fSFrançois Tigeot 	 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
601e3adcf8fSFrançois Tigeot 	 * memory before requesting an interrupt.
602e3adcf8fSFrançois Tigeot 	 */
603e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 32);
604e3adcf8fSFrançois Tigeot 	if (ret)
605e3adcf8fSFrançois Tigeot 		return ret;
606e3adcf8fSFrançois Tigeot 
607e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
608e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
609e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
610e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
611e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, seqno);
612e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
613e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
614e3adcf8fSFrançois Tigeot 	scratch_addr += 128; /* write to separate cachelines */
615e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
616e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
617e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
618e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
619e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
620e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
621e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
622e3adcf8fSFrançois Tigeot 	scratch_addr += 128;
623e3adcf8fSFrançois Tigeot 	PIPE_CONTROL_FLUSH(ring, scratch_addr);
624e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
625e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_WRITE_FLUSH |
626e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
627e3adcf8fSFrançois Tigeot 			PIPE_CONTROL_NOTIFY);
628e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
629e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, seqno);
630e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
631e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
632e3adcf8fSFrançois Tigeot 
633e3adcf8fSFrançois Tigeot 	*result = seqno;
634e3adcf8fSFrançois Tigeot 	return 0;
635e3adcf8fSFrançois Tigeot }
636e3adcf8fSFrançois Tigeot 
637e3adcf8fSFrançois Tigeot static int
638e3adcf8fSFrançois Tigeot render_ring_add_request(struct intel_ring_buffer *ring,
639e3adcf8fSFrançois Tigeot 			uint32_t *result)
640e3adcf8fSFrançois Tigeot {
641e3adcf8fSFrançois Tigeot 	u32 seqno = i915_gem_next_request_seqno(ring);
642e3adcf8fSFrançois Tigeot 	int ret;
643e3adcf8fSFrançois Tigeot 
644e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
645e3adcf8fSFrançois Tigeot 	if (ret)
646e3adcf8fSFrançois Tigeot 		return ret;
647e3adcf8fSFrançois Tigeot 
648e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
649e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
650e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, seqno);
651e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
652e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
653e3adcf8fSFrançois Tigeot 
654e3adcf8fSFrançois Tigeot 	*result = seqno;
655e3adcf8fSFrançois Tigeot 	return 0;
656e3adcf8fSFrançois Tigeot }
657e3adcf8fSFrançois Tigeot 
658e3adcf8fSFrançois Tigeot  static u32
659e3adcf8fSFrançois Tigeot gen6_ring_get_seqno(struct intel_ring_buffer *ring)
660e3adcf8fSFrançois Tigeot {
661e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
662e3adcf8fSFrançois Tigeot 
663e3adcf8fSFrançois Tigeot 	/* Workaround to force correct ordering between irq and seqno writes on
664e3adcf8fSFrançois Tigeot 	 * ivb (and maybe also on snb) by reading from a CS register (like
665e3adcf8fSFrançois Tigeot 	 * ACTHD) before reading the status page. */
666e3adcf8fSFrançois Tigeot 	if (/* IS_GEN6(dev) || */IS_GEN7(dev))
667e3adcf8fSFrançois Tigeot 		intel_ring_get_active_head(ring);
668e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
669e3adcf8fSFrançois Tigeot }
670e3adcf8fSFrançois Tigeot 
671e3adcf8fSFrançois Tigeot static uint32_t
672e3adcf8fSFrançois Tigeot ring_get_seqno(struct intel_ring_buffer *ring)
673e3adcf8fSFrançois Tigeot {
674e3adcf8fSFrançois Tigeot 	if (ring->status_page.page_addr == NULL)
675e3adcf8fSFrançois Tigeot 		return (-1);
676e3adcf8fSFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
677e3adcf8fSFrançois Tigeot }
678e3adcf8fSFrançois Tigeot 
679e3adcf8fSFrançois Tigeot static uint32_t
680e3adcf8fSFrançois Tigeot pc_render_get_seqno(struct intel_ring_buffer *ring)
681e3adcf8fSFrançois Tigeot {
682e3adcf8fSFrançois Tigeot 	struct pipe_control *pc = ring->private;
683e3adcf8fSFrançois Tigeot 	if (pc != NULL)
684e3adcf8fSFrançois Tigeot 		return pc->cpu_page[0];
685e3adcf8fSFrançois Tigeot 	else
686e3adcf8fSFrançois Tigeot 		return (-1);
687e3adcf8fSFrançois Tigeot }
688e3adcf8fSFrançois Tigeot 
689e3adcf8fSFrançois Tigeot static void
690e3adcf8fSFrançois Tigeot ironlake_enable_irq(drm_i915_private_t *dev_priv, uint32_t mask)
691e3adcf8fSFrançois Tigeot {
692e3adcf8fSFrançois Tigeot 	dev_priv->gt_irq_mask &= ~mask;
693e3adcf8fSFrançois Tigeot 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
694e3adcf8fSFrançois Tigeot 	POSTING_READ(GTIMR);
695e3adcf8fSFrançois Tigeot }
696e3adcf8fSFrançois Tigeot 
697e3adcf8fSFrançois Tigeot static void
698e3adcf8fSFrançois Tigeot ironlake_disable_irq(drm_i915_private_t *dev_priv, uint32_t mask)
699e3adcf8fSFrançois Tigeot {
700e3adcf8fSFrançois Tigeot 	dev_priv->gt_irq_mask |= mask;
701e3adcf8fSFrançois Tigeot 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
702e3adcf8fSFrançois Tigeot 	POSTING_READ(GTIMR);
703e3adcf8fSFrançois Tigeot }
704e3adcf8fSFrançois Tigeot 
705e3adcf8fSFrançois Tigeot static void
706e3adcf8fSFrançois Tigeot i915_enable_irq(drm_i915_private_t *dev_priv, uint32_t mask)
707e3adcf8fSFrançois Tigeot {
708e3adcf8fSFrançois Tigeot 	dev_priv->irq_mask &= ~mask;
709e3adcf8fSFrançois Tigeot 	I915_WRITE(IMR, dev_priv->irq_mask);
710e3adcf8fSFrançois Tigeot 	POSTING_READ(IMR);
711e3adcf8fSFrançois Tigeot }
712e3adcf8fSFrançois Tigeot 
713e3adcf8fSFrançois Tigeot static void
714e3adcf8fSFrançois Tigeot i915_disable_irq(drm_i915_private_t *dev_priv, uint32_t mask)
715e3adcf8fSFrançois Tigeot {
716e3adcf8fSFrançois Tigeot 	dev_priv->irq_mask |= mask;
717e3adcf8fSFrançois Tigeot 	I915_WRITE(IMR, dev_priv->irq_mask);
718e3adcf8fSFrançois Tigeot 	POSTING_READ(IMR);
719e3adcf8fSFrançois Tigeot }
720e3adcf8fSFrançois Tigeot 
721e3adcf8fSFrançois Tigeot static bool
722e3adcf8fSFrançois Tigeot render_ring_get_irq(struct intel_ring_buffer *ring)
723e3adcf8fSFrançois Tigeot {
724e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
725e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
726e3adcf8fSFrançois Tigeot 
727e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
728e3adcf8fSFrançois Tigeot 		return false;
729e3adcf8fSFrançois Tigeot 
730e3adcf8fSFrançois Tigeot 	KKASSERT(lockstatus(&ring->irq_lock, curthread) != 0);
731e3adcf8fSFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
732e3adcf8fSFrançois Tigeot 		if (HAS_PCH_SPLIT(dev))
733e3adcf8fSFrançois Tigeot 			ironlake_enable_irq(dev_priv,
734e3adcf8fSFrançois Tigeot 					    GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
735e3adcf8fSFrançois Tigeot 		else
736e3adcf8fSFrançois Tigeot 			i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
737e3adcf8fSFrançois Tigeot 	}
738e3adcf8fSFrançois Tigeot 
739e3adcf8fSFrançois Tigeot 	return true;
740e3adcf8fSFrançois Tigeot }
741e3adcf8fSFrançois Tigeot 
742e3adcf8fSFrançois Tigeot static void
743e3adcf8fSFrançois Tigeot render_ring_put_irq(struct intel_ring_buffer *ring)
744e3adcf8fSFrançois Tigeot {
745e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
746e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
747e3adcf8fSFrançois Tigeot 
748e3adcf8fSFrançois Tigeot 	KKASSERT(lockstatus(&ring->irq_lock, curthread) != 0);
749e3adcf8fSFrançois Tigeot 	if (--ring->irq_refcount == 0) {
750e3adcf8fSFrançois Tigeot 		if (HAS_PCH_SPLIT(dev))
751e3adcf8fSFrançois Tigeot 			ironlake_disable_irq(dev_priv,
752e3adcf8fSFrançois Tigeot 					     GT_USER_INTERRUPT |
753e3adcf8fSFrançois Tigeot 					     GT_PIPE_NOTIFY);
754e3adcf8fSFrançois Tigeot 		else
755e3adcf8fSFrançois Tigeot 			i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
756e3adcf8fSFrançois Tigeot 	}
757e3adcf8fSFrançois Tigeot }
758e3adcf8fSFrançois Tigeot 
759e3adcf8fSFrançois Tigeot void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
760e3adcf8fSFrançois Tigeot {
761e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
762e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
763e3adcf8fSFrançois Tigeot 	uint32_t mmio = 0;
764e3adcf8fSFrançois Tigeot 
765e3adcf8fSFrançois Tigeot 	/* The ring status page addresses are no longer next to the rest of
766e3adcf8fSFrançois Tigeot 	 * the ring registers as of gen7.
767e3adcf8fSFrançois Tigeot 	 */
768e3adcf8fSFrançois Tigeot 	if (IS_GEN7(dev)) {
769e3adcf8fSFrançois Tigeot 		switch (ring->id) {
770e3adcf8fSFrançois Tigeot 		case RCS:
771e3adcf8fSFrançois Tigeot 			mmio = RENDER_HWS_PGA_GEN7;
772e3adcf8fSFrançois Tigeot 			break;
773e3adcf8fSFrançois Tigeot 		case BCS:
774e3adcf8fSFrançois Tigeot 			mmio = BLT_HWS_PGA_GEN7;
775e3adcf8fSFrançois Tigeot 			break;
776e3adcf8fSFrançois Tigeot 		case VCS:
777e3adcf8fSFrançois Tigeot 			mmio = BSD_HWS_PGA_GEN7;
778e3adcf8fSFrançois Tigeot 			break;
779e3adcf8fSFrançois Tigeot 		}
780e3adcf8fSFrançois Tigeot 	} else if (IS_GEN6(dev)) {
781e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
782e3adcf8fSFrançois Tigeot 	} else {
783e3adcf8fSFrançois Tigeot 		mmio = RING_HWS_PGA(ring->mmio_base);
784e3adcf8fSFrançois Tigeot 	}
785e3adcf8fSFrançois Tigeot 
786e3adcf8fSFrançois Tigeot 	I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
787e3adcf8fSFrançois Tigeot 	POSTING_READ(mmio);
788e3adcf8fSFrançois Tigeot }
789e3adcf8fSFrançois Tigeot 
790e3adcf8fSFrançois Tigeot static int
791e3adcf8fSFrançois Tigeot bsd_ring_flush(struct intel_ring_buffer *ring,
792e3adcf8fSFrançois Tigeot 	       uint32_t     invalidate_domains,
793e3adcf8fSFrançois Tigeot 	       uint32_t     flush_domains)
794e3adcf8fSFrançois Tigeot {
795e3adcf8fSFrançois Tigeot 	int ret;
796e3adcf8fSFrançois Tigeot 
797e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
798e3adcf8fSFrançois Tigeot 	if (ret)
799e3adcf8fSFrançois Tigeot 		return ret;
800e3adcf8fSFrançois Tigeot 
801e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_FLUSH);
802e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
803e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
804e3adcf8fSFrançois Tigeot 	return 0;
805e3adcf8fSFrançois Tigeot }
806e3adcf8fSFrançois Tigeot 
807e3adcf8fSFrançois Tigeot static int
808e3adcf8fSFrançois Tigeot ring_add_request(struct intel_ring_buffer *ring,
809e3adcf8fSFrançois Tigeot 		 uint32_t *result)
810e3adcf8fSFrançois Tigeot {
811e3adcf8fSFrançois Tigeot 	uint32_t seqno;
812e3adcf8fSFrançois Tigeot 	int ret;
813e3adcf8fSFrançois Tigeot 
814e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
815e3adcf8fSFrançois Tigeot 	if (ret)
816e3adcf8fSFrançois Tigeot 		return ret;
817e3adcf8fSFrançois Tigeot 
818e3adcf8fSFrançois Tigeot 	seqno = i915_gem_next_request_seqno(ring);
819e3adcf8fSFrançois Tigeot 
820e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
821e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
822e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, seqno);
823e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_USER_INTERRUPT);
824e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
825e3adcf8fSFrançois Tigeot 
826e3adcf8fSFrançois Tigeot 	*result = seqno;
827e3adcf8fSFrançois Tigeot 	return 0;
828e3adcf8fSFrançois Tigeot }
829e3adcf8fSFrançois Tigeot 
830e3adcf8fSFrançois Tigeot static bool
831e3adcf8fSFrançois Tigeot gen6_ring_get_irq(struct intel_ring_buffer *ring, uint32_t gflag, uint32_t rflag)
832e3adcf8fSFrançois Tigeot {
833e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
834e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
835e3adcf8fSFrançois Tigeot 
836e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
837e3adcf8fSFrançois Tigeot 	       return false;
838e3adcf8fSFrançois Tigeot 
839e3adcf8fSFrançois Tigeot 	gen6_gt_force_wake_get(dev_priv);
840e3adcf8fSFrançois Tigeot 
841e3adcf8fSFrançois Tigeot 	KKASSERT(lockstatus(&ring->irq_lock, curthread) != 0);
842e3adcf8fSFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
843e3adcf8fSFrançois Tigeot 		ring->irq_mask &= ~rflag;
844e3adcf8fSFrançois Tigeot 		I915_WRITE_IMR(ring, ring->irq_mask);
845e3adcf8fSFrançois Tigeot 		ironlake_enable_irq(dev_priv, gflag);
846e3adcf8fSFrançois Tigeot 	}
847e3adcf8fSFrançois Tigeot 
848e3adcf8fSFrançois Tigeot 	return true;
849e3adcf8fSFrançois Tigeot }
850e3adcf8fSFrançois Tigeot 
851e3adcf8fSFrançois Tigeot static void
852e3adcf8fSFrançois Tigeot gen6_ring_put_irq(struct intel_ring_buffer *ring, uint32_t gflag, uint32_t rflag)
853e3adcf8fSFrançois Tigeot {
854e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
855e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
856e3adcf8fSFrançois Tigeot 
857e3adcf8fSFrançois Tigeot 	KKASSERT(lockstatus(&ring->irq_lock, curthread) != 0);
858e3adcf8fSFrançois Tigeot 	if (--ring->irq_refcount == 0) {
859e3adcf8fSFrançois Tigeot 		ring->irq_mask |= rflag;
860e3adcf8fSFrançois Tigeot 		I915_WRITE_IMR(ring, ring->irq_mask);
861e3adcf8fSFrançois Tigeot 		ironlake_disable_irq(dev_priv, gflag);
862e3adcf8fSFrançois Tigeot 	}
863e3adcf8fSFrançois Tigeot 
864e3adcf8fSFrançois Tigeot 	gen6_gt_force_wake_put(dev_priv);
865e3adcf8fSFrançois Tigeot }
866e3adcf8fSFrançois Tigeot 
867e3adcf8fSFrançois Tigeot static bool
868e3adcf8fSFrançois Tigeot bsd_ring_get_irq(struct intel_ring_buffer *ring)
869e3adcf8fSFrançois Tigeot {
870e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
871e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
872e3adcf8fSFrançois Tigeot 
873e3adcf8fSFrançois Tigeot 	if (!dev->irq_enabled)
874e3adcf8fSFrançois Tigeot 		return false;
875e3adcf8fSFrançois Tigeot 
876e3adcf8fSFrançois Tigeot 	KKASSERT(lockstatus(&ring->irq_lock, curthread) != 0);
877e3adcf8fSFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
878e3adcf8fSFrançois Tigeot 		if (IS_G4X(dev))
879e3adcf8fSFrançois Tigeot 			i915_enable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
880e3adcf8fSFrançois Tigeot 		else
881e3adcf8fSFrançois Tigeot 			ironlake_enable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
882e3adcf8fSFrançois Tigeot 	}
883e3adcf8fSFrançois Tigeot 
884e3adcf8fSFrançois Tigeot 	return true;
885e3adcf8fSFrançois Tigeot }
886e3adcf8fSFrançois Tigeot static void
887e3adcf8fSFrançois Tigeot bsd_ring_put_irq(struct intel_ring_buffer *ring)
888e3adcf8fSFrançois Tigeot {
889e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
890e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
891e3adcf8fSFrançois Tigeot 
892e3adcf8fSFrançois Tigeot 	KKASSERT(lockstatus(&ring->irq_lock, curthread) != 0);
893e3adcf8fSFrançois Tigeot 	if (--ring->irq_refcount == 0) {
894e3adcf8fSFrançois Tigeot 		if (IS_G4X(dev))
895e3adcf8fSFrançois Tigeot 			i915_disable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
896e3adcf8fSFrançois Tigeot 		else
897e3adcf8fSFrançois Tigeot 			ironlake_disable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
898e3adcf8fSFrançois Tigeot 	}
899e3adcf8fSFrançois Tigeot }
900e3adcf8fSFrançois Tigeot 
901e3adcf8fSFrançois Tigeot static int
902e3adcf8fSFrançois Tigeot ring_dispatch_execbuffer(struct intel_ring_buffer *ring, uint32_t offset,
903e3adcf8fSFrançois Tigeot     uint32_t length)
904e3adcf8fSFrançois Tigeot {
905e3adcf8fSFrançois Tigeot 	int ret;
906e3adcf8fSFrançois Tigeot 
907e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
908e3adcf8fSFrançois Tigeot 	if (ret)
909e3adcf8fSFrançois Tigeot 		return ret;
910e3adcf8fSFrançois Tigeot 
911e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring,
912e3adcf8fSFrançois Tigeot 			MI_BATCH_BUFFER_START | (2 << 6) |
913e3adcf8fSFrançois Tigeot 			MI_BATCH_NON_SECURE_I965);
914e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
915e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
916e3adcf8fSFrançois Tigeot 
917e3adcf8fSFrançois Tigeot 	return 0;
918e3adcf8fSFrançois Tigeot }
919e3adcf8fSFrançois Tigeot 
920e3adcf8fSFrançois Tigeot static int
921e3adcf8fSFrançois Tigeot render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
922e3adcf8fSFrançois Tigeot 				uint32_t offset, uint32_t len)
923e3adcf8fSFrançois Tigeot {
924e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
925e3adcf8fSFrançois Tigeot 	int ret;
926e3adcf8fSFrançois Tigeot 
927e3adcf8fSFrançois Tigeot 	if (IS_I830(dev) || IS_845G(dev)) {
928e3adcf8fSFrançois Tigeot 		ret = intel_ring_begin(ring, 4);
929e3adcf8fSFrançois Tigeot 		if (ret)
930e3adcf8fSFrançois Tigeot 			return ret;
931e3adcf8fSFrançois Tigeot 
932e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, MI_BATCH_BUFFER);
933e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
934e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, offset + len - 8);
935e3adcf8fSFrançois Tigeot 		intel_ring_emit(ring, 0);
936e3adcf8fSFrançois Tigeot 	} else {
937e3adcf8fSFrançois Tigeot 		ret = intel_ring_begin(ring, 2);
938e3adcf8fSFrançois Tigeot 		if (ret)
939e3adcf8fSFrançois Tigeot 			return ret;
940e3adcf8fSFrançois Tigeot 
941e3adcf8fSFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 4) {
942e3adcf8fSFrançois Tigeot 			intel_ring_emit(ring,
943e3adcf8fSFrançois Tigeot 					MI_BATCH_BUFFER_START | (2 << 6) |
944e3adcf8fSFrançois Tigeot 					MI_BATCH_NON_SECURE_I965);
945e3adcf8fSFrançois Tigeot 			intel_ring_emit(ring, offset);
946e3adcf8fSFrançois Tigeot 		} else {
947e3adcf8fSFrançois Tigeot 			intel_ring_emit(ring,
948e3adcf8fSFrançois Tigeot 					MI_BATCH_BUFFER_START | (2 << 6));
949e3adcf8fSFrançois Tigeot 			intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
950e3adcf8fSFrançois Tigeot 		}
951e3adcf8fSFrançois Tigeot 	}
952e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
953e3adcf8fSFrançois Tigeot 
954e3adcf8fSFrançois Tigeot 	return 0;
955e3adcf8fSFrançois Tigeot }
956e3adcf8fSFrançois Tigeot 
957e3adcf8fSFrançois Tigeot static void cleanup_status_page(struct intel_ring_buffer *ring)
958e3adcf8fSFrançois Tigeot {
959e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
960e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
961e3adcf8fSFrançois Tigeot 
962e3adcf8fSFrançois Tigeot 	obj = ring->status_page.obj;
963e3adcf8fSFrançois Tigeot 	if (obj == NULL)
964e3adcf8fSFrançois Tigeot 		return;
965e3adcf8fSFrançois Tigeot 
966e3adcf8fSFrançois Tigeot 	pmap_qremove((vm_offset_t)ring->status_page.page_addr, 1);
967e3adcf8fSFrançois Tigeot 	kmem_free(&kernel_map, (vm_offset_t)ring->status_page.page_addr,
968e3adcf8fSFrançois Tigeot 	    PAGE_SIZE);
969e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
970e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
971e3adcf8fSFrançois Tigeot 	ring->status_page.obj = NULL;
972e3adcf8fSFrançois Tigeot 
973e3adcf8fSFrançois Tigeot 	memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
974e3adcf8fSFrançois Tigeot }
975e3adcf8fSFrançois Tigeot 
976e3adcf8fSFrançois Tigeot static int init_status_page(struct intel_ring_buffer *ring)
977e3adcf8fSFrançois Tigeot {
978e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
979e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
980e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
981e3adcf8fSFrançois Tigeot 	int ret;
982e3adcf8fSFrançois Tigeot 
983e3adcf8fSFrançois Tigeot 	obj = i915_gem_alloc_object(dev, 4096);
984e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
985e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate status page\n");
986e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
987e3adcf8fSFrançois Tigeot 		goto err;
988e3adcf8fSFrançois Tigeot 	}
989e3adcf8fSFrançois Tigeot 
990e3adcf8fSFrançois Tigeot 	i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
991e3adcf8fSFrançois Tigeot 
992e3adcf8fSFrançois Tigeot 	ret = i915_gem_object_pin(obj, 4096, true);
993e3adcf8fSFrançois Tigeot 	if (ret != 0) {
994e3adcf8fSFrançois Tigeot 		goto err_unref;
995e3adcf8fSFrançois Tigeot 	}
996e3adcf8fSFrançois Tigeot 
997e3adcf8fSFrançois Tigeot 	ring->status_page.gfx_addr = obj->gtt_offset;
998e3adcf8fSFrançois Tigeot 	ring->status_page.page_addr = (void *)kmem_alloc_nofault(&kernel_map,
999e3adcf8fSFrançois Tigeot 	    PAGE_SIZE, PAGE_SIZE);
1000e3adcf8fSFrançois Tigeot 	if (ring->status_page.page_addr == NULL) {
1001e3adcf8fSFrançois Tigeot 		memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
1002e3adcf8fSFrançois Tigeot 		goto err_unpin;
1003e3adcf8fSFrançois Tigeot 	}
1004e3adcf8fSFrançois Tigeot 	pmap_qenter((vm_offset_t)ring->status_page.page_addr, &obj->pages[0],
1005e3adcf8fSFrançois Tigeot 	    1);
1006e3adcf8fSFrançois Tigeot 	pmap_invalidate_cache_range((vm_offset_t)ring->status_page.page_addr,
1007e3adcf8fSFrançois Tigeot 	    (vm_offset_t)ring->status_page.page_addr + PAGE_SIZE);
1008e3adcf8fSFrançois Tigeot 	ring->status_page.obj = obj;
1009e3adcf8fSFrançois Tigeot 	memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1010e3adcf8fSFrançois Tigeot 
1011e3adcf8fSFrançois Tigeot 	intel_ring_setup_status_page(ring);
1012e3adcf8fSFrançois Tigeot 	DRM_DEBUG("i915: init_status_page %s hws offset: 0x%08x\n",
1013e3adcf8fSFrançois Tigeot 			ring->name, ring->status_page.gfx_addr);
1014e3adcf8fSFrançois Tigeot 
1015e3adcf8fSFrançois Tigeot 	return 0;
1016e3adcf8fSFrançois Tigeot 
1017e3adcf8fSFrançois Tigeot err_unpin:
1018e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
1019e3adcf8fSFrançois Tigeot err_unref:
1020e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1021e3adcf8fSFrançois Tigeot err:
1022e3adcf8fSFrançois Tigeot 	return ret;
1023e3adcf8fSFrançois Tigeot }
1024e3adcf8fSFrançois Tigeot 
1025e3adcf8fSFrançois Tigeot static
1026e3adcf8fSFrançois Tigeot int intel_init_ring_buffer(struct drm_device *dev,
1027e3adcf8fSFrançois Tigeot 			   struct intel_ring_buffer *ring)
1028e3adcf8fSFrançois Tigeot {
1029e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
1030e3adcf8fSFrançois Tigeot 	int ret;
1031e3adcf8fSFrançois Tigeot 
1032e3adcf8fSFrançois Tigeot 	ring->dev = dev;
1033e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1034e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
1035e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->gpu_write_list);
1036e3adcf8fSFrançois Tigeot 
1037e3adcf8fSFrançois Tigeot 	lockinit(&ring->irq_lock, "ringb", 0, LK_CANRECURSE);
1038e3adcf8fSFrançois Tigeot 	ring->irq_mask = ~0;
1039e3adcf8fSFrançois Tigeot 
1040e3adcf8fSFrançois Tigeot 	if (I915_NEED_GFX_HWS(dev)) {
1041e3adcf8fSFrançois Tigeot 		ret = init_status_page(ring);
1042e3adcf8fSFrançois Tigeot 		if (ret)
1043e3adcf8fSFrançois Tigeot 			return ret;
1044e3adcf8fSFrançois Tigeot 	}
1045e3adcf8fSFrançois Tigeot 
1046e3adcf8fSFrançois Tigeot 	obj = i915_gem_alloc_object(dev, ring->size);
1047e3adcf8fSFrançois Tigeot 	if (obj == NULL) {
1048e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to allocate ringbuffer\n");
1049e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
1050e3adcf8fSFrançois Tigeot 		goto err_hws;
1051e3adcf8fSFrançois Tigeot 	}
1052e3adcf8fSFrançois Tigeot 
1053e3adcf8fSFrançois Tigeot 	ring->obj = obj;
1054e3adcf8fSFrançois Tigeot 
1055e3adcf8fSFrançois Tigeot 	ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
1056e3adcf8fSFrançois Tigeot 	if (ret)
1057e3adcf8fSFrançois Tigeot 		goto err_unref;
1058e3adcf8fSFrançois Tigeot 
1059e3adcf8fSFrançois Tigeot 	ring->map.size = ring->size;
1060e3adcf8fSFrançois Tigeot 	ring->map.offset = dev->agp->base + obj->gtt_offset;
1061e3adcf8fSFrançois Tigeot 	ring->map.type = 0;
1062e3adcf8fSFrançois Tigeot 	ring->map.flags = 0;
1063e3adcf8fSFrançois Tigeot 	ring->map.mtrr = 0;
1064e3adcf8fSFrançois Tigeot 
1065e3adcf8fSFrançois Tigeot 	drm_core_ioremap_wc(&ring->map, dev);
1066e3adcf8fSFrançois Tigeot 	if (ring->map.virtual == NULL) {
1067e3adcf8fSFrançois Tigeot 		DRM_ERROR("Failed to map ringbuffer.\n");
1068e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1069e3adcf8fSFrançois Tigeot 		goto err_unpin;
1070e3adcf8fSFrançois Tigeot 	}
1071e3adcf8fSFrançois Tigeot 
1072e3adcf8fSFrançois Tigeot 	ring->virtual_start = ring->map.virtual;
1073e3adcf8fSFrançois Tigeot 	ret = ring->init(ring);
1074e3adcf8fSFrançois Tigeot 	if (ret)
1075e3adcf8fSFrançois Tigeot 		goto err_unmap;
1076e3adcf8fSFrançois Tigeot 
1077e3adcf8fSFrançois Tigeot 	/* Workaround an erratum on the i830 which causes a hang if
1078e3adcf8fSFrançois Tigeot 	 * the TAIL pointer points to within the last 2 cachelines
1079e3adcf8fSFrançois Tigeot 	 * of the buffer.
1080e3adcf8fSFrançois Tigeot 	 */
1081e3adcf8fSFrançois Tigeot 	ring->effective_size = ring->size;
1082e3adcf8fSFrançois Tigeot 	if (IS_I830(ring->dev) || IS_845G(ring->dev))
1083e3adcf8fSFrançois Tigeot 		ring->effective_size -= 128;
1084e3adcf8fSFrançois Tigeot 
1085e3adcf8fSFrançois Tigeot 	return 0;
1086e3adcf8fSFrançois Tigeot 
1087e3adcf8fSFrançois Tigeot err_unmap:
1088e3adcf8fSFrançois Tigeot 	drm_core_ioremapfree(&ring->map, dev);
1089e3adcf8fSFrançois Tigeot err_unpin:
1090e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(obj);
1091e3adcf8fSFrançois Tigeot err_unref:
1092e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&obj->base);
1093e3adcf8fSFrançois Tigeot 	ring->obj = NULL;
1094e3adcf8fSFrançois Tigeot err_hws:
1095e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1096e3adcf8fSFrançois Tigeot 	return ret;
1097e3adcf8fSFrançois Tigeot }
1098e3adcf8fSFrançois Tigeot 
1099e3adcf8fSFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
1100e3adcf8fSFrançois Tigeot {
1101e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv;
1102e3adcf8fSFrançois Tigeot 	int ret;
1103e3adcf8fSFrançois Tigeot 
1104e3adcf8fSFrançois Tigeot 	if (ring->obj == NULL)
1105e3adcf8fSFrançois Tigeot 		return;
1106e3adcf8fSFrançois Tigeot 
1107e3adcf8fSFrançois Tigeot 	/* Disable the ring buffer. The ring must be idle at this point */
1108e3adcf8fSFrançois Tigeot 	dev_priv = ring->dev->dev_private;
1109e3adcf8fSFrançois Tigeot 	ret = intel_wait_ring_idle(ring);
1110e3adcf8fSFrançois Tigeot 	I915_WRITE_CTL(ring, 0);
1111e3adcf8fSFrançois Tigeot 
1112e3adcf8fSFrançois Tigeot 	drm_core_ioremapfree(&ring->map, ring->dev);
1113e3adcf8fSFrançois Tigeot 
1114e3adcf8fSFrançois Tigeot 	i915_gem_object_unpin(ring->obj);
1115e3adcf8fSFrançois Tigeot 	drm_gem_object_unreference(&ring->obj->base);
1116e3adcf8fSFrançois Tigeot 	ring->obj = NULL;
1117e3adcf8fSFrançois Tigeot 
1118e3adcf8fSFrançois Tigeot 	if (ring->cleanup)
1119e3adcf8fSFrançois Tigeot 		ring->cleanup(ring);
1120e3adcf8fSFrançois Tigeot 
1121e3adcf8fSFrançois Tigeot 	cleanup_status_page(ring);
1122e3adcf8fSFrançois Tigeot }
1123e3adcf8fSFrançois Tigeot 
1124e3adcf8fSFrançois Tigeot static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
1125e3adcf8fSFrançois Tigeot {
1126e3adcf8fSFrançois Tigeot 	unsigned int *virt;
1127e3adcf8fSFrançois Tigeot 	int rem = ring->size - ring->tail;
1128e3adcf8fSFrançois Tigeot 
1129e3adcf8fSFrançois Tigeot 	if (ring->space < rem) {
1130e3adcf8fSFrançois Tigeot 		int ret = intel_wait_ring_buffer(ring, rem);
1131e3adcf8fSFrançois Tigeot 		if (ret)
1132e3adcf8fSFrançois Tigeot 			return ret;
1133e3adcf8fSFrançois Tigeot 	}
1134e3adcf8fSFrançois Tigeot 
1135e3adcf8fSFrançois Tigeot 	virt = (unsigned int *)((char *)ring->virtual_start + ring->tail);
1136e3adcf8fSFrançois Tigeot 	rem /= 8;
1137e3adcf8fSFrançois Tigeot 	while (rem--) {
1138e3adcf8fSFrançois Tigeot 		*virt++ = MI_NOOP;
1139e3adcf8fSFrançois Tigeot 		*virt++ = MI_NOOP;
1140e3adcf8fSFrançois Tigeot 	}
1141e3adcf8fSFrançois Tigeot 
1142e3adcf8fSFrançois Tigeot 	ring->tail = 0;
1143e3adcf8fSFrançois Tigeot 	ring->space = ring_space(ring);
1144e3adcf8fSFrançois Tigeot 
1145e3adcf8fSFrançois Tigeot 	return 0;
1146e3adcf8fSFrançois Tigeot }
1147e3adcf8fSFrançois Tigeot 
1148e3adcf8fSFrançois Tigeot static int intel_ring_wait_seqno(struct intel_ring_buffer *ring, u32 seqno)
1149e3adcf8fSFrançois Tigeot {
1150e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1151e3adcf8fSFrançois Tigeot 	bool was_interruptible;
1152e3adcf8fSFrançois Tigeot 	int ret;
1153e3adcf8fSFrançois Tigeot 
1154e3adcf8fSFrançois Tigeot 	/* XXX As we have not yet audited all the paths to check that
1155e3adcf8fSFrançois Tigeot 	 * they are ready for ERESTARTSYS from intel_ring_begin, do not
1156e3adcf8fSFrançois Tigeot 	 * allow us to be interruptible by a signal.
1157e3adcf8fSFrançois Tigeot 	 */
1158e3adcf8fSFrançois Tigeot 	was_interruptible = dev_priv->mm.interruptible;
1159e3adcf8fSFrançois Tigeot 	dev_priv->mm.interruptible = false;
1160e3adcf8fSFrançois Tigeot 
1161e3adcf8fSFrançois Tigeot 	ret = i915_wait_request(ring, seqno, true);
1162e3adcf8fSFrançois Tigeot 
1163e3adcf8fSFrançois Tigeot 	dev_priv->mm.interruptible = was_interruptible;
1164e3adcf8fSFrançois Tigeot 
1165e3adcf8fSFrançois Tigeot 	return ret;
1166e3adcf8fSFrançois Tigeot }
1167e3adcf8fSFrançois Tigeot 
1168e3adcf8fSFrançois Tigeot static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
1169e3adcf8fSFrançois Tigeot {
1170e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_request *request;
1171e3adcf8fSFrançois Tigeot 	u32 seqno = 0;
1172e3adcf8fSFrançois Tigeot 	int ret;
1173e3adcf8fSFrançois Tigeot 
1174e3adcf8fSFrançois Tigeot 	i915_gem_retire_requests_ring(ring);
1175e3adcf8fSFrançois Tigeot 
1176e3adcf8fSFrançois Tigeot 	if (ring->last_retired_head != -1) {
1177e3adcf8fSFrançois Tigeot 		ring->head = ring->last_retired_head;
1178e3adcf8fSFrançois Tigeot 		ring->last_retired_head = -1;
1179e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
1180e3adcf8fSFrançois Tigeot 		if (ring->space >= n)
1181e3adcf8fSFrançois Tigeot 			return 0;
1182e3adcf8fSFrançois Tigeot 	}
1183e3adcf8fSFrançois Tigeot 
1184e3adcf8fSFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
1185e3adcf8fSFrançois Tigeot 		int space;
1186e3adcf8fSFrançois Tigeot 
1187e3adcf8fSFrançois Tigeot 		if (request->tail == -1)
1188e3adcf8fSFrançois Tigeot 			continue;
1189e3adcf8fSFrançois Tigeot 
1190e3adcf8fSFrançois Tigeot 		space = request->tail - (ring->tail + 8);
1191e3adcf8fSFrançois Tigeot 		if (space < 0)
1192e3adcf8fSFrançois Tigeot 			space += ring->size;
1193e3adcf8fSFrançois Tigeot 		if (space >= n) {
1194e3adcf8fSFrançois Tigeot 			seqno = request->seqno;
1195e3adcf8fSFrançois Tigeot 			break;
1196e3adcf8fSFrançois Tigeot 		}
1197e3adcf8fSFrançois Tigeot 
1198e3adcf8fSFrançois Tigeot 		/* Consume this request in case we need more space than
1199e3adcf8fSFrançois Tigeot 		 * is available and so need to prevent a race between
1200e3adcf8fSFrançois Tigeot 		 * updating last_retired_head and direct reads of
1201e3adcf8fSFrançois Tigeot 		 * I915_RING_HEAD. It also provides a nice sanity check.
1202e3adcf8fSFrançois Tigeot 		 */
1203e3adcf8fSFrançois Tigeot 		request->tail = -1;
1204e3adcf8fSFrançois Tigeot 	}
1205e3adcf8fSFrançois Tigeot 
1206e3adcf8fSFrançois Tigeot 	if (seqno == 0)
1207e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1208e3adcf8fSFrançois Tigeot 
1209e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_seqno(ring, seqno);
1210e3adcf8fSFrançois Tigeot 	if (ret)
1211e3adcf8fSFrançois Tigeot 		return ret;
1212e3adcf8fSFrançois Tigeot 
1213e3adcf8fSFrançois Tigeot 	if (ring->last_retired_head == -1)
1214e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1215e3adcf8fSFrançois Tigeot 
1216e3adcf8fSFrançois Tigeot 	ring->head = ring->last_retired_head;
1217e3adcf8fSFrançois Tigeot 	ring->last_retired_head = -1;
1218e3adcf8fSFrançois Tigeot 	ring->space = ring_space(ring);
1219e3adcf8fSFrançois Tigeot 	if (ring->space < n)
1220e3adcf8fSFrançois Tigeot 		return -ENOSPC;
1221e3adcf8fSFrançois Tigeot 
1222e3adcf8fSFrançois Tigeot 	return 0;
1223e3adcf8fSFrançois Tigeot }
1224e3adcf8fSFrançois Tigeot 
1225e3adcf8fSFrançois Tigeot int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
1226e3adcf8fSFrançois Tigeot {
1227e3adcf8fSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1228e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1229e3adcf8fSFrançois Tigeot 	int end;
1230e3adcf8fSFrançois Tigeot 	int ret;
1231e3adcf8fSFrançois Tigeot 
1232e3adcf8fSFrançois Tigeot 	ret = intel_ring_wait_request(ring, n);
1233e3adcf8fSFrançois Tigeot 	if (ret != -ENOSPC)
1234e3adcf8fSFrançois Tigeot 		return ret;
1235e3adcf8fSFrançois Tigeot 
1236e3adcf8fSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_GEM))
1237e3adcf8fSFrançois Tigeot 		/* With GEM the hangcheck timer should kick us out of the loop,
1238e3adcf8fSFrançois Tigeot 		 * leaving it early runs the risk of corrupting GEM state (due
1239e3adcf8fSFrançois Tigeot 		 * to running on almost untested codepaths). But on resume
1240e3adcf8fSFrançois Tigeot 		 * timers don't work yet, so prevent a complete hang in that
1241e3adcf8fSFrançois Tigeot 		 * case by choosing an insanely large timeout. */
1242e3adcf8fSFrançois Tigeot 		end = ticks + hz * 60;
1243e3adcf8fSFrançois Tigeot 	else
1244e3adcf8fSFrançois Tigeot 		end = ticks + hz * 3;
1245e3adcf8fSFrançois Tigeot 	do {
1246e3adcf8fSFrançois Tigeot 		ring->head = I915_READ_HEAD(ring);
1247e3adcf8fSFrançois Tigeot 		ring->space = ring_space(ring);
1248e3adcf8fSFrançois Tigeot 		if (ring->space >= n) {
1249e3adcf8fSFrançois Tigeot 			return 0;
1250e3adcf8fSFrançois Tigeot 		}
1251e3adcf8fSFrançois Tigeot 
1252e3adcf8fSFrançois Tigeot #if 0
1253e3adcf8fSFrançois Tigeot 		if (dev->primary->master) {
1254e3adcf8fSFrançois Tigeot 			struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1255e3adcf8fSFrançois Tigeot 			if (master_priv->sarea_priv)
1256e3adcf8fSFrançois Tigeot 				master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1257e3adcf8fSFrançois Tigeot 		}
1258e3adcf8fSFrançois Tigeot #else
1259e3adcf8fSFrançois Tigeot 		if (dev_priv->sarea_priv)
1260e3adcf8fSFrançois Tigeot 			dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1261e3adcf8fSFrançois Tigeot #endif
1262e3adcf8fSFrançois Tigeot 
1263e3adcf8fSFrançois Tigeot 		DELAY(1000);
1264e3adcf8fSFrançois Tigeot 		if (atomic_load_acq_32(&dev_priv->mm.wedged) != 0) {
1265e3adcf8fSFrançois Tigeot 			return -EAGAIN;
1266e3adcf8fSFrançois Tigeot 		}
1267e3adcf8fSFrançois Tigeot 	} while (!time_after(ticks, end));
1268e3adcf8fSFrançois Tigeot 	return -EBUSY;
1269e3adcf8fSFrançois Tigeot }
1270e3adcf8fSFrançois Tigeot 
1271e3adcf8fSFrançois Tigeot int intel_ring_begin(struct intel_ring_buffer *ring,
1272e3adcf8fSFrançois Tigeot 		     int num_dwords)
1273e3adcf8fSFrançois Tigeot {
1274e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1275e3adcf8fSFrançois Tigeot 	int n = 4*num_dwords;
1276e3adcf8fSFrançois Tigeot 	int ret;
1277e3adcf8fSFrançois Tigeot 
1278e3adcf8fSFrançois Tigeot 	if (atomic_load_acq_int(&dev_priv->mm.wedged))
1279e3adcf8fSFrançois Tigeot 		return -EIO;
1280e3adcf8fSFrançois Tigeot 
1281e3adcf8fSFrançois Tigeot 	if (ring->tail + n > ring->effective_size) {
1282e3adcf8fSFrançois Tigeot 		ret = intel_wrap_ring_buffer(ring);
1283e3adcf8fSFrançois Tigeot 		if (ret != 0)
1284e3adcf8fSFrançois Tigeot 			return ret;
1285e3adcf8fSFrançois Tigeot 	}
1286e3adcf8fSFrançois Tigeot 
1287e3adcf8fSFrançois Tigeot 	if (ring->space < n) {
1288e3adcf8fSFrançois Tigeot 		ret = intel_wait_ring_buffer(ring, n);
1289e3adcf8fSFrançois Tigeot 		if (ret != 0)
1290e3adcf8fSFrançois Tigeot 			return ret;
1291e3adcf8fSFrançois Tigeot 	}
1292e3adcf8fSFrançois Tigeot 
1293e3adcf8fSFrançois Tigeot 	ring->space -= n;
1294e3adcf8fSFrançois Tigeot 	return 0;
1295e3adcf8fSFrançois Tigeot }
1296e3adcf8fSFrançois Tigeot 
1297e3adcf8fSFrançois Tigeot void intel_ring_advance(struct intel_ring_buffer *ring)
1298e3adcf8fSFrançois Tigeot {
1299e3adcf8fSFrançois Tigeot 	ring->tail &= ring->size - 1;
1300e3adcf8fSFrançois Tigeot 	ring->write_tail(ring, ring->tail);
1301e3adcf8fSFrançois Tigeot }
1302e3adcf8fSFrançois Tigeot 
1303e3adcf8fSFrançois Tigeot static const struct intel_ring_buffer render_ring = {
1304e3adcf8fSFrançois Tigeot 	.name			= "render ring",
1305e3adcf8fSFrançois Tigeot 	.id			= RCS,
1306e3adcf8fSFrançois Tigeot 	.mmio_base		= RENDER_RING_BASE,
1307e3adcf8fSFrançois Tigeot 	.size			= 32 * PAGE_SIZE,
1308e3adcf8fSFrançois Tigeot 	.init			= init_render_ring,
1309e3adcf8fSFrançois Tigeot 	.write_tail		= ring_write_tail,
1310e3adcf8fSFrançois Tigeot 	.flush			= render_ring_flush,
1311e3adcf8fSFrançois Tigeot 	.add_request		= render_ring_add_request,
1312e3adcf8fSFrançois Tigeot 	.get_seqno		= ring_get_seqno,
1313e3adcf8fSFrançois Tigeot 	.irq_get		= render_ring_get_irq,
1314e3adcf8fSFrançois Tigeot 	.irq_put		= render_ring_put_irq,
1315e3adcf8fSFrançois Tigeot 	.dispatch_execbuffer	= render_ring_dispatch_execbuffer,
1316e3adcf8fSFrançois Tigeot 	.cleanup		= render_ring_cleanup,
1317e3adcf8fSFrançois Tigeot 	.sync_to		= render_ring_sync_to,
1318e3adcf8fSFrançois Tigeot 	.semaphore_register	= {MI_SEMAPHORE_SYNC_INVALID,
1319e3adcf8fSFrançois Tigeot 				   MI_SEMAPHORE_SYNC_RV,
1320e3adcf8fSFrançois Tigeot 				   MI_SEMAPHORE_SYNC_RB},
1321e3adcf8fSFrançois Tigeot 	.signal_mbox		= {GEN6_VRSYNC, GEN6_BRSYNC},
1322e3adcf8fSFrançois Tigeot };
1323e3adcf8fSFrançois Tigeot 
1324e3adcf8fSFrançois Tigeot /* ring buffer for bit-stream decoder */
1325e3adcf8fSFrançois Tigeot 
1326e3adcf8fSFrançois Tigeot static const struct intel_ring_buffer bsd_ring = {
1327e3adcf8fSFrançois Tigeot 	.name                   = "bsd ring",
1328e3adcf8fSFrançois Tigeot 	.id			= VCS,
1329e3adcf8fSFrançois Tigeot 	.mmio_base		= BSD_RING_BASE,
1330e3adcf8fSFrançois Tigeot 	.size			= 32 * PAGE_SIZE,
1331e3adcf8fSFrançois Tigeot 	.init			= init_ring_common,
1332e3adcf8fSFrançois Tigeot 	.write_tail		= ring_write_tail,
1333e3adcf8fSFrançois Tigeot 	.flush			= bsd_ring_flush,
1334e3adcf8fSFrançois Tigeot 	.add_request		= ring_add_request,
1335e3adcf8fSFrançois Tigeot 	.get_seqno		= ring_get_seqno,
1336e3adcf8fSFrançois Tigeot 	.irq_get		= bsd_ring_get_irq,
1337e3adcf8fSFrançois Tigeot 	.irq_put		= bsd_ring_put_irq,
1338e3adcf8fSFrançois Tigeot 	.dispatch_execbuffer	= ring_dispatch_execbuffer,
1339e3adcf8fSFrançois Tigeot };
1340e3adcf8fSFrançois Tigeot 
1341e3adcf8fSFrançois Tigeot 
1342e3adcf8fSFrançois Tigeot static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
1343e3adcf8fSFrançois Tigeot 				     uint32_t value)
1344e3adcf8fSFrançois Tigeot {
1345e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1346e3adcf8fSFrançois Tigeot 
1347e3adcf8fSFrançois Tigeot 	/* Every tail move must follow the sequence below */
1348e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1349e3adcf8fSFrançois Tigeot 	    GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1350e3adcf8fSFrançois Tigeot 	    GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1351e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_RNCID, 0x0);
1352e3adcf8fSFrançois Tigeot 
1353e3adcf8fSFrançois Tigeot 	if (_intel_wait_for(ring->dev,
1354e3adcf8fSFrançois Tigeot 	    (I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1355e3adcf8fSFrançois Tigeot 	     GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0, 50,
1356e3adcf8fSFrançois Tigeot 	    true, "915g6i") != 0)
1357e3adcf8fSFrançois Tigeot 		DRM_ERROR("timed out waiting for IDLE Indicator\n");
1358e3adcf8fSFrançois Tigeot 
1359e3adcf8fSFrançois Tigeot 	I915_WRITE_TAIL(ring, value);
1360e3adcf8fSFrançois Tigeot 	I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1361e3adcf8fSFrançois Tigeot 	    GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1362e3adcf8fSFrançois Tigeot 	    GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
1363e3adcf8fSFrançois Tigeot }
1364e3adcf8fSFrançois Tigeot 
1365e3adcf8fSFrançois Tigeot static int gen6_ring_flush(struct intel_ring_buffer *ring,
1366e3adcf8fSFrançois Tigeot 			   uint32_t invalidate, uint32_t flush)
1367e3adcf8fSFrançois Tigeot {
1368e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1369e3adcf8fSFrançois Tigeot 	int ret;
1370e3adcf8fSFrançois Tigeot 
1371e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1372e3adcf8fSFrançois Tigeot 	if (ret)
1373e3adcf8fSFrançois Tigeot 		return ret;
1374e3adcf8fSFrançois Tigeot 
1375e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
1376e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_GPU_DOMAINS)
1377e3adcf8fSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1378e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1379e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1380e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1381e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1382e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1383e3adcf8fSFrançois Tigeot 	return 0;
1384e3adcf8fSFrançois Tigeot }
1385e3adcf8fSFrançois Tigeot 
1386e3adcf8fSFrançois Tigeot static int
1387e3adcf8fSFrançois Tigeot gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
1388e3adcf8fSFrançois Tigeot 			      uint32_t offset, uint32_t len)
1389e3adcf8fSFrançois Tigeot {
1390e3adcf8fSFrançois Tigeot 	int ret;
1391e3adcf8fSFrançois Tigeot 
1392e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 2);
1393e3adcf8fSFrançois Tigeot 	if (ret)
1394e3adcf8fSFrançois Tigeot 		return ret;
1395e3adcf8fSFrançois Tigeot 
1396e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1397e3adcf8fSFrançois Tigeot 	/* bit0-7 is the length on GEN6+ */
1398e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, offset);
1399e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1400e3adcf8fSFrançois Tigeot 
1401e3adcf8fSFrançois Tigeot 	return 0;
1402e3adcf8fSFrançois Tigeot }
1403e3adcf8fSFrançois Tigeot 
1404e3adcf8fSFrançois Tigeot static bool
1405e3adcf8fSFrançois Tigeot gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
1406e3adcf8fSFrançois Tigeot {
1407e3adcf8fSFrançois Tigeot 	return gen6_ring_get_irq(ring,
1408e3adcf8fSFrançois Tigeot 				 GT_USER_INTERRUPT,
1409e3adcf8fSFrançois Tigeot 				 GEN6_RENDER_USER_INTERRUPT);
1410e3adcf8fSFrançois Tigeot }
1411e3adcf8fSFrançois Tigeot 
1412e3adcf8fSFrançois Tigeot static void
1413e3adcf8fSFrançois Tigeot gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
1414e3adcf8fSFrançois Tigeot {
1415e3adcf8fSFrançois Tigeot 	return gen6_ring_put_irq(ring,
1416e3adcf8fSFrançois Tigeot 				 GT_USER_INTERRUPT,
1417e3adcf8fSFrançois Tigeot 				 GEN6_RENDER_USER_INTERRUPT);
1418e3adcf8fSFrançois Tigeot }
1419e3adcf8fSFrançois Tigeot 
1420e3adcf8fSFrançois Tigeot static bool
1421e3adcf8fSFrançois Tigeot gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
1422e3adcf8fSFrançois Tigeot {
1423e3adcf8fSFrançois Tigeot 	return gen6_ring_get_irq(ring,
1424e3adcf8fSFrançois Tigeot 				 GT_GEN6_BSD_USER_INTERRUPT,
1425e3adcf8fSFrançois Tigeot 				 GEN6_BSD_USER_INTERRUPT);
1426e3adcf8fSFrançois Tigeot }
1427e3adcf8fSFrançois Tigeot 
1428e3adcf8fSFrançois Tigeot static void
1429e3adcf8fSFrançois Tigeot gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
1430e3adcf8fSFrançois Tigeot {
1431e3adcf8fSFrançois Tigeot 	return gen6_ring_put_irq(ring,
1432e3adcf8fSFrançois Tigeot 				 GT_GEN6_BSD_USER_INTERRUPT,
1433e3adcf8fSFrançois Tigeot 				 GEN6_BSD_USER_INTERRUPT);
1434e3adcf8fSFrançois Tigeot }
1435e3adcf8fSFrançois Tigeot 
1436e3adcf8fSFrançois Tigeot /* ring buffer for Video Codec for Gen6+ */
1437e3adcf8fSFrançois Tigeot static const struct intel_ring_buffer gen6_bsd_ring = {
1438e3adcf8fSFrançois Tigeot 	.name			= "gen6 bsd ring",
1439e3adcf8fSFrançois Tigeot 	.id			= VCS,
1440e3adcf8fSFrançois Tigeot 	.mmio_base		= GEN6_BSD_RING_BASE,
1441e3adcf8fSFrançois Tigeot 	.size			= 32 * PAGE_SIZE,
1442e3adcf8fSFrançois Tigeot 	.init			= init_ring_common,
1443e3adcf8fSFrançois Tigeot 	.write_tail		= gen6_bsd_ring_write_tail,
1444e3adcf8fSFrançois Tigeot 	.flush			= gen6_ring_flush,
1445e3adcf8fSFrançois Tigeot 	.add_request		= gen6_add_request,
1446e3adcf8fSFrançois Tigeot 	.get_seqno		= gen6_ring_get_seqno,
1447e3adcf8fSFrançois Tigeot 	.irq_get		= gen6_bsd_ring_get_irq,
1448e3adcf8fSFrançois Tigeot 	.irq_put		= gen6_bsd_ring_put_irq,
1449e3adcf8fSFrançois Tigeot 	.dispatch_execbuffer	= gen6_ring_dispatch_execbuffer,
1450e3adcf8fSFrançois Tigeot 	.sync_to		= gen6_bsd_ring_sync_to,
1451e3adcf8fSFrançois Tigeot 	.semaphore_register	= {MI_SEMAPHORE_SYNC_VR,
1452e3adcf8fSFrançois Tigeot 				   MI_SEMAPHORE_SYNC_INVALID,
1453e3adcf8fSFrançois Tigeot 				   MI_SEMAPHORE_SYNC_VB},
1454e3adcf8fSFrançois Tigeot 	.signal_mbox		= {GEN6_RVSYNC, GEN6_BVSYNC},
1455e3adcf8fSFrançois Tigeot };
1456e3adcf8fSFrançois Tigeot 
1457e3adcf8fSFrançois Tigeot /* Blitter support (SandyBridge+) */
1458e3adcf8fSFrançois Tigeot 
1459e3adcf8fSFrançois Tigeot static bool
1460e3adcf8fSFrançois Tigeot blt_ring_get_irq(struct intel_ring_buffer *ring)
1461e3adcf8fSFrançois Tigeot {
1462e3adcf8fSFrançois Tigeot 	return gen6_ring_get_irq(ring,
1463e3adcf8fSFrançois Tigeot 				 GT_BLT_USER_INTERRUPT,
1464e3adcf8fSFrançois Tigeot 				 GEN6_BLITTER_USER_INTERRUPT);
1465e3adcf8fSFrançois Tigeot }
1466e3adcf8fSFrançois Tigeot 
1467e3adcf8fSFrançois Tigeot static void
1468e3adcf8fSFrançois Tigeot blt_ring_put_irq(struct intel_ring_buffer *ring)
1469e3adcf8fSFrançois Tigeot {
1470e3adcf8fSFrançois Tigeot 	gen6_ring_put_irq(ring,
1471e3adcf8fSFrançois Tigeot 			  GT_BLT_USER_INTERRUPT,
1472e3adcf8fSFrançois Tigeot 			  GEN6_BLITTER_USER_INTERRUPT);
1473e3adcf8fSFrançois Tigeot }
1474e3adcf8fSFrançois Tigeot 
1475e3adcf8fSFrançois Tigeot static int blt_ring_flush(struct intel_ring_buffer *ring,
1476e3adcf8fSFrançois Tigeot 			  uint32_t invalidate, uint32_t flush)
1477e3adcf8fSFrançois Tigeot {
1478e3adcf8fSFrançois Tigeot 	uint32_t cmd;
1479e3adcf8fSFrançois Tigeot 	int ret;
1480e3adcf8fSFrançois Tigeot 
1481e3adcf8fSFrançois Tigeot 	ret = intel_ring_begin(ring, 4);
1482e3adcf8fSFrançois Tigeot 	if (ret)
1483e3adcf8fSFrançois Tigeot 		return ret;
1484e3adcf8fSFrançois Tigeot 
1485e3adcf8fSFrançois Tigeot 	cmd = MI_FLUSH_DW;
1486e3adcf8fSFrançois Tigeot 	if (invalidate & I915_GEM_DOMAIN_RENDER)
1487e3adcf8fSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB;
1488e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, cmd);
1489e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1490e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, 0);
1491e3adcf8fSFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1492e3adcf8fSFrançois Tigeot 	intel_ring_advance(ring);
1493e3adcf8fSFrançois Tigeot 	return 0;
1494e3adcf8fSFrançois Tigeot }
1495e3adcf8fSFrançois Tigeot 
1496e3adcf8fSFrançois Tigeot static const struct intel_ring_buffer gen6_blt_ring = {
1497e3adcf8fSFrançois Tigeot 	.name			= "blt ring",
1498e3adcf8fSFrançois Tigeot 	.id			= BCS,
1499e3adcf8fSFrançois Tigeot 	.mmio_base		= BLT_RING_BASE,
1500e3adcf8fSFrançois Tigeot 	.size			= 32 * PAGE_SIZE,
1501e3adcf8fSFrançois Tigeot 	.init			= init_ring_common,
1502e3adcf8fSFrançois Tigeot 	.write_tail		= ring_write_tail,
1503e3adcf8fSFrançois Tigeot 	.flush			= blt_ring_flush,
1504e3adcf8fSFrançois Tigeot 	.add_request		= gen6_add_request,
1505e3adcf8fSFrançois Tigeot 	.get_seqno		= gen6_ring_get_seqno,
1506e3adcf8fSFrançois Tigeot 	.irq_get		= blt_ring_get_irq,
1507e3adcf8fSFrançois Tigeot 	.irq_put		= blt_ring_put_irq,
1508e3adcf8fSFrançois Tigeot 	.dispatch_execbuffer	= gen6_ring_dispatch_execbuffer,
1509e3adcf8fSFrançois Tigeot 	.sync_to		= gen6_blt_ring_sync_to,
1510e3adcf8fSFrançois Tigeot 	.semaphore_register	= {MI_SEMAPHORE_SYNC_BR,
1511e3adcf8fSFrançois Tigeot 				   MI_SEMAPHORE_SYNC_BV,
1512e3adcf8fSFrançois Tigeot 				   MI_SEMAPHORE_SYNC_INVALID},
1513e3adcf8fSFrançois Tigeot 	.signal_mbox		= {GEN6_RBSYNC, GEN6_VBSYNC},
1514e3adcf8fSFrançois Tigeot };
1515e3adcf8fSFrançois Tigeot 
1516e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev)
1517e3adcf8fSFrançois Tigeot {
1518e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1519e3adcf8fSFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->rings[RCS];
1520e3adcf8fSFrançois Tigeot 
1521e3adcf8fSFrançois Tigeot 	*ring = render_ring;
1522e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
1523e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
1524e3adcf8fSFrançois Tigeot 		ring->flush = gen6_render_ring_flush;
1525e3adcf8fSFrançois Tigeot 		ring->irq_get = gen6_render_ring_get_irq;
1526e3adcf8fSFrançois Tigeot 		ring->irq_put = gen6_render_ring_put_irq;
1527e3adcf8fSFrançois Tigeot 		ring->get_seqno = gen6_ring_get_seqno;
1528e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
1529e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
1530e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
1531e3adcf8fSFrançois Tigeot 	}
1532e3adcf8fSFrançois Tigeot 
1533e3adcf8fSFrançois Tigeot 	if (!I915_NEED_GFX_HWS(dev)) {
1534e3adcf8fSFrançois Tigeot 		ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1535e3adcf8fSFrançois Tigeot 		memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1536e3adcf8fSFrançois Tigeot 	}
1537e3adcf8fSFrançois Tigeot 
1538e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1539e3adcf8fSFrançois Tigeot }
1540e3adcf8fSFrançois Tigeot 
1541e3adcf8fSFrançois Tigeot int intel_render_ring_init_dri(struct drm_device *dev, uint64_t start,
1542e3adcf8fSFrançois Tigeot     uint32_t size)
1543e3adcf8fSFrançois Tigeot {
1544e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1545e3adcf8fSFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->rings[RCS];
1546e3adcf8fSFrançois Tigeot 
1547e3adcf8fSFrançois Tigeot 	*ring = render_ring;
1548e3adcf8fSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 6) {
1549e3adcf8fSFrançois Tigeot 		ring->add_request = gen6_add_request;
1550e3adcf8fSFrançois Tigeot 		ring->irq_get = gen6_render_ring_get_irq;
1551e3adcf8fSFrançois Tigeot 		ring->irq_put = gen6_render_ring_put_irq;
1552e3adcf8fSFrançois Tigeot 	} else if (IS_GEN5(dev)) {
1553e3adcf8fSFrançois Tigeot 		ring->add_request = pc_render_add_request;
1554e3adcf8fSFrançois Tigeot 		ring->get_seqno = pc_render_get_seqno;
1555e3adcf8fSFrançois Tigeot 	}
1556e3adcf8fSFrançois Tigeot 
1557e3adcf8fSFrançois Tigeot 	ring->dev = dev;
1558e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
1559e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
1560e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&ring->gpu_write_list);
1561e3adcf8fSFrançois Tigeot 
1562e3adcf8fSFrançois Tigeot 	ring->size = size;
1563e3adcf8fSFrançois Tigeot 	ring->effective_size = ring->size;
1564e3adcf8fSFrançois Tigeot 	if (IS_I830(ring->dev))
1565e3adcf8fSFrançois Tigeot 		ring->effective_size -= 128;
1566e3adcf8fSFrançois Tigeot 
1567e3adcf8fSFrançois Tigeot 	ring->map.offset = start;
1568e3adcf8fSFrançois Tigeot 	ring->map.size = size;
1569e3adcf8fSFrançois Tigeot 	ring->map.type = 0;
1570e3adcf8fSFrançois Tigeot 	ring->map.flags = 0;
1571e3adcf8fSFrançois Tigeot 	ring->map.mtrr = 0;
1572e3adcf8fSFrançois Tigeot 
1573e3adcf8fSFrançois Tigeot 	drm_core_ioremap_wc(&ring->map, dev);
1574e3adcf8fSFrançois Tigeot 	if (ring->map.virtual == NULL) {
1575e3adcf8fSFrançois Tigeot 		DRM_ERROR("can not ioremap virtual address for"
1576e3adcf8fSFrançois Tigeot 			  " ring buffer\n");
1577e3adcf8fSFrançois Tigeot 		return -ENOMEM;
1578e3adcf8fSFrançois Tigeot 	}
1579e3adcf8fSFrançois Tigeot 
1580e3adcf8fSFrançois Tigeot 	ring->virtual_start = (void *)ring->map.virtual;
1581e3adcf8fSFrançois Tigeot 	return 0;
1582e3adcf8fSFrançois Tigeot }
1583e3adcf8fSFrançois Tigeot 
1584e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev)
1585e3adcf8fSFrançois Tigeot {
1586e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1587e3adcf8fSFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->rings[VCS];
1588e3adcf8fSFrançois Tigeot 
1589e3adcf8fSFrançois Tigeot 	if (IS_GEN6(dev) || IS_GEN7(dev))
1590e3adcf8fSFrançois Tigeot 		*ring = gen6_bsd_ring;
1591e3adcf8fSFrançois Tigeot 	else
1592e3adcf8fSFrançois Tigeot 		*ring = bsd_ring;
1593e3adcf8fSFrançois Tigeot 
1594e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1595e3adcf8fSFrançois Tigeot }
1596e3adcf8fSFrançois Tigeot 
1597e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev)
1598e3adcf8fSFrançois Tigeot {
1599e3adcf8fSFrançois Tigeot 	drm_i915_private_t *dev_priv = dev->dev_private;
1600e3adcf8fSFrançois Tigeot 	struct intel_ring_buffer *ring = &dev_priv->rings[BCS];
1601e3adcf8fSFrançois Tigeot 
1602e3adcf8fSFrançois Tigeot 	*ring = gen6_blt_ring;
1603e3adcf8fSFrançois Tigeot 
1604e3adcf8fSFrançois Tigeot 	return intel_init_ring_buffer(dev, ring);
1605e3adcf8fSFrançois Tigeot }
1606