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