blob: 4d00da97905fd68c815cccb9ba3edf90ce6101eb [file] [log] [blame]
Eric Anholt62fdfea2010-05-21 13:26:39 -07001/*
2 * Copyright © 2008-2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
27 *
28 */
29
30#include "drmP.h"
31#include "drm.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070032#include "i915_drv.h"
Zou Nan hai8187a2b2010-05-21 09:08:55 +080033#include "i915_drm.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070034#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010035#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070036
Chris Wilson6f392d5482010-08-07 11:01:22 +010037static u32 i915_gem_get_seqno(struct drm_device *dev)
38{
39 drm_i915_private_t *dev_priv = dev->dev_private;
40 u32 seqno;
41
42 seqno = dev_priv->next_seqno;
43
44 /* reserve 0 for non-seqno */
45 if (++dev_priv->next_seqno == 0)
46 dev_priv->next_seqno = 1;
47
48 return seqno;
49}
50
Zou Nan hai8187a2b2010-05-21 09:08:55 +080051static void
Chris Wilson78501ea2010-10-27 12:18:21 +010052render_ring_flush(struct intel_ring_buffer *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +010053 u32 invalidate_domains,
54 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070055{
Chris Wilson78501ea2010-10-27 12:18:21 +010056 struct drm_device *dev = ring->dev;
Chris Wilson6f392d5482010-08-07 11:01:22 +010057 drm_i915_private_t *dev_priv = dev->dev_private;
58 u32 cmd;
59
Eric Anholt62fdfea2010-05-21 13:26:39 -070060#if WATCH_EXEC
61 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
62 invalidate_domains, flush_domains);
63#endif
Chris Wilson6f392d5482010-08-07 11:01:22 +010064
65 trace_i915_gem_request_flush(dev, dev_priv->next_seqno,
Eric Anholt62fdfea2010-05-21 13:26:39 -070066 invalidate_domains, flush_domains);
67
Eric Anholt62fdfea2010-05-21 13:26:39 -070068 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
69 /*
70 * read/write caches:
71 *
72 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
73 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
74 * also flushed at 2d versus 3d pipeline switches.
75 *
76 * read-only caches:
77 *
78 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
79 * MI_READ_FLUSH is set, and is always flushed on 965.
80 *
81 * I915_GEM_DOMAIN_COMMAND may not exist?
82 *
83 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
84 * invalidated when MI_EXE_FLUSH is set.
85 *
86 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
87 * invalidated with every MI_FLUSH.
88 *
89 * TLBs:
90 *
91 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
92 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
93 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
94 * are flushed at any MI_FLUSH.
95 */
96
97 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
98 if ((invalidate_domains|flush_domains) &
99 I915_GEM_DOMAIN_RENDER)
100 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100101 if (INTEL_INFO(dev)->gen < 4) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700102 /*
103 * On the 965, the sampler cache always gets flushed
104 * and this bit is reserved.
105 */
106 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
107 cmd |= MI_READ_FLUSH;
108 }
109 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
110 cmd |= MI_EXE_FLUSH;
111
112#if WATCH_EXEC
113 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
114#endif
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100115 if (intel_ring_begin(ring, 2) == 0) {
116 intel_ring_emit(ring, cmd);
117 intel_ring_emit(ring, MI_NOOP);
118 intel_ring_advance(ring);
119 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800120 }
121}
122
Chris Wilson78501ea2010-10-27 12:18:21 +0100123static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100124 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800125{
Chris Wilson78501ea2010-10-27 12:18:21 +0100126 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100127 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800128}
129
Chris Wilson78501ea2010-10-27 12:18:21 +0100130u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800131{
Chris Wilson78501ea2010-10-27 12:18:21 +0100132 drm_i915_private_t *dev_priv = ring->dev->dev_private;
133 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200134 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800135
136 return I915_READ(acthd_reg);
137}
138
Chris Wilson78501ea2010-10-27 12:18:21 +0100139static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800140{
Chris Wilson78501ea2010-10-27 12:18:21 +0100141 drm_i915_private_t *dev_priv = ring->dev->dev_private;
142 struct drm_i915_gem_object *obj_priv = to_intel_bo(ring->gem_object);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800143 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800144
145 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200146 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200147 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100148 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800149
150 /* Initialize the ring. */
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200151 I915_WRITE_START(ring, obj_priv->gtt_offset);
Daniel Vetter570ef602010-08-02 17:06:23 +0200152 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800153
154 /* G45 ring initialization fails to reset head to zero */
155 if (head != 0) {
156 DRM_ERROR("%s head not reset to zero "
157 "ctl %08x head %08x tail %08x start %08x\n",
158 ring->name,
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200159 I915_READ_CTL(ring),
Daniel Vetter570ef602010-08-02 17:06:23 +0200160 I915_READ_HEAD(ring),
Daniel Vetter870e86d2010-08-02 16:29:44 +0200161 I915_READ_TAIL(ring),
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200162 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800163
Daniel Vetter570ef602010-08-02 17:06:23 +0200164 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800165
166 DRM_ERROR("%s head forced to zero "
167 "ctl %08x head %08x tail %08x start %08x\n",
168 ring->name,
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200169 I915_READ_CTL(ring),
Daniel Vetter570ef602010-08-02 17:06:23 +0200170 I915_READ_HEAD(ring),
Daniel Vetter870e86d2010-08-02 16:29:44 +0200171 I915_READ_TAIL(ring),
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200172 I915_READ_START(ring));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700173 }
174
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200175 I915_WRITE_CTL(ring,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800176 ((ring->gem_object->size - PAGE_SIZE) & RING_NR_PAGES)
177 | RING_NO_REPORT | RING_VALID);
178
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800179 /* If the head is still not zero, the ring is dead */
Chris Wilson176f28e2010-10-28 11:18:07 +0100180 if ((I915_READ_CTL(ring) & RING_VALID) == 0 ||
181 I915_READ_START(ring) != obj_priv->gtt_offset ||
182 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800183 DRM_ERROR("%s initialization failed "
184 "ctl %08x head %08x tail %08x start %08x\n",
185 ring->name,
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200186 I915_READ_CTL(ring),
Daniel Vetter570ef602010-08-02 17:06:23 +0200187 I915_READ_HEAD(ring),
Daniel Vetter870e86d2010-08-02 16:29:44 +0200188 I915_READ_TAIL(ring),
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200189 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800190 return -EIO;
191 }
192
Chris Wilson78501ea2010-10-27 12:18:21 +0100193 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
194 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800195 else {
Daniel Vetter570ef602010-08-02 17:06:23 +0200196 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Daniel Vetter870e86d2010-08-02 16:29:44 +0200197 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800198 ring->space = ring->head - (ring->tail + 8);
199 if (ring->space < 0)
200 ring->space += ring->size;
201 }
202 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700203}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800204
Chris Wilson78501ea2010-10-27 12:18:21 +0100205static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800206{
Chris Wilson78501ea2010-10-27 12:18:21 +0100207 struct drm_device *dev = ring->dev;
208 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800209
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100210 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100211 drm_i915_private_t *dev_priv = dev->dev_private;
212 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800213 if (IS_GEN6(dev))
214 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
215 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800216 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100217
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800218 return ret;
219}
220
Chris Wilson78501ea2010-10-27 12:18:21 +0100221#define PIPE_CONTROL_FLUSH(ring__, addr__) \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800222do { \
Chris Wilson78501ea2010-10-27 12:18:21 +0100223 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
Zhenyu Wangca764822010-05-27 10:26:42 +0800224 PIPE_CONTROL_DEPTH_STALL | 2); \
Chris Wilson78501ea2010-10-27 12:18:21 +0100225 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
226 intel_ring_emit(ring__, 0); \
227 intel_ring_emit(ring__, 0); \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800228} while (0)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700229
230/**
231 * Creates a new sequence number, emitting a write of it to the status page
232 * plus an interrupt, which will trigger i915_user_interrupt_handler.
233 *
234 * Must be called with struct_lock held.
235 *
236 * Returned sequence numbers are nonzero on success.
237 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100238static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100239render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100240 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700241{
Chris Wilson78501ea2010-10-27 12:18:21 +0100242 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700243 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson3cce4692010-10-27 16:11:02 +0100244 u32 seqno = i915_gem_get_seqno(dev);
245 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800246
247 if (IS_GEN6(dev)) {
Chris Wilson3cce4692010-10-27 16:11:02 +0100248 ret = intel_ring_begin(ring, 6);
249 if (ret)
250 return ret;
251
252 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | 3);
253 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE |
254 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_IS_FLUSH |
255 PIPE_CONTROL_NOTIFY);
256 intel_ring_emit(ring, dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
257 intel_ring_emit(ring, seqno);
258 intel_ring_emit(ring, 0);
259 intel_ring_emit(ring, 0);
Zhenyu Wangca764822010-05-27 10:26:42 +0800260 } else if (HAS_PIPE_CONTROL(dev)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700261 u32 scratch_addr = dev_priv->seqno_gfx_addr + 128;
262
263 /*
264 * Workaround qword write incoherence by flushing the
265 * PIPE_NOTIFY buffers out to memory before requesting
266 * an interrupt.
267 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100268 ret = intel_ring_begin(ring, 32);
269 if (ret)
270 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700271
Chris Wilson3cce4692010-10-27 16:11:02 +0100272 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
273 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
274 intel_ring_emit(ring, dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
275 intel_ring_emit(ring, seqno);
276 intel_ring_emit(ring, 0);
277 PIPE_CONTROL_FLUSH(ring, scratch_addr);
278 scratch_addr += 128; /* write to separate cachelines */
279 PIPE_CONTROL_FLUSH(ring, scratch_addr);
280 scratch_addr += 128;
281 PIPE_CONTROL_FLUSH(ring, scratch_addr);
282 scratch_addr += 128;
283 PIPE_CONTROL_FLUSH(ring, scratch_addr);
284 scratch_addr += 128;
285 PIPE_CONTROL_FLUSH(ring, scratch_addr);
286 scratch_addr += 128;
287 PIPE_CONTROL_FLUSH(ring, scratch_addr);
288 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
289 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
290 PIPE_CONTROL_NOTIFY);
291 intel_ring_emit(ring, dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
292 intel_ring_emit(ring, seqno);
293 intel_ring_emit(ring, 0);
294 } else {
295 ret = intel_ring_begin(ring, 4);
296 if (ret)
297 return ret;
298
299 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
300 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
301 intel_ring_emit(ring, seqno);
302
303 intel_ring_emit(ring, MI_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700304 }
Chris Wilson3cce4692010-10-27 16:11:02 +0100305
306 intel_ring_advance(ring);
307 *result = seqno;
308 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700309}
310
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800311static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100312render_ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800313{
Chris Wilson78501ea2010-10-27 12:18:21 +0100314 struct drm_device *dev = ring->dev;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800315 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
316 if (HAS_PIPE_CONTROL(dev))
317 return ((volatile u32 *)(dev_priv->seqno_page))[0];
318 else
319 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
320}
321
322static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100323render_ring_get_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700324{
Chris Wilson78501ea2010-10-27 12:18:21 +0100325 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700326 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
327 unsigned long irqflags;
328
329 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800330 if (dev->irq_enabled && (++ring->user_irq_refcount == 1)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700331 if (HAS_PCH_SPLIT(dev))
332 ironlake_enable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
333 else
334 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
335 }
336 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
337}
338
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800339static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100340render_ring_put_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700341{
Chris Wilson78501ea2010-10-27 12:18:21 +0100342 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700343 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
344 unsigned long irqflags;
345
346 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800347 BUG_ON(dev->irq_enabled && ring->user_irq_refcount <= 0);
348 if (dev->irq_enabled && (--ring->user_irq_refcount == 0)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700349 if (HAS_PCH_SPLIT(dev))
350 ironlake_disable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
351 else
352 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
353 }
354 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
355}
356
Chris Wilson78501ea2010-10-27 12:18:21 +0100357void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800358{
Chris Wilson78501ea2010-10-27 12:18:21 +0100359 drm_i915_private_t *dev_priv = ring->dev->dev_private;
360 u32 mmio = IS_GEN6(ring->dev) ?
361 RING_HWS_PGA_GEN6(ring->mmio_base) :
362 RING_HWS_PGA(ring->mmio_base);
363 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
364 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800365}
366
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100367static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100368bsd_ring_flush(struct intel_ring_buffer *ring,
369 u32 invalidate_domains,
370 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800371{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100372 if (intel_ring_begin(ring, 2) == 0) {
373 intel_ring_emit(ring, MI_FLUSH);
374 intel_ring_emit(ring, MI_NOOP);
375 intel_ring_advance(ring);
376 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800377}
378
Chris Wilson3cce4692010-10-27 16:11:02 +0100379static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100380ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100381 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800382{
383 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100384 int ret;
385
386 ret = intel_ring_begin(ring, 4);
387 if (ret)
388 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100389
Chris Wilson78501ea2010-10-27 12:18:21 +0100390 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d5482010-08-07 11:01:22 +0100391
Chris Wilson3cce4692010-10-27 16:11:02 +0100392 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
393 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
394 intel_ring_emit(ring, seqno);
395 intel_ring_emit(ring, MI_USER_INTERRUPT);
396 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800397
398 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +0100399 *result = seqno;
400 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800401}
402
Zou Nan haid1b851f2010-05-21 09:08:57 +0800403static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100404bsd_ring_get_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800405{
406 /* do nothing */
407}
408static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100409bsd_ring_put_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800410{
411 /* do nothing */
412}
413
414static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100415ring_status_page_get_seqno(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800416{
417 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
418}
419
420static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100421ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
422 struct drm_i915_gem_execbuffer2 *exec,
423 struct drm_clip_rect *cliprects,
424 uint64_t exec_offset)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800425{
426 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100427 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100428
Zou Nan haid1b851f2010-05-21 09:08:57 +0800429 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilson78501ea2010-10-27 12:18:21 +0100430
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100431 ret = intel_ring_begin(ring, 2);
432 if (ret)
433 return ret;
434
Chris Wilson78501ea2010-10-27 12:18:21 +0100435 intel_ring_emit(ring,
436 MI_BATCH_BUFFER_START |
437 (2 << 6) |
438 MI_BATCH_NON_SECURE_I965);
439 intel_ring_emit(ring, exec_start);
440 intel_ring_advance(ring);
441
Zou Nan haid1b851f2010-05-21 09:08:57 +0800442 return 0;
443}
444
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800445static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100446render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
447 struct drm_i915_gem_execbuffer2 *exec,
448 struct drm_clip_rect *cliprects,
449 uint64_t exec_offset)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700450{
Chris Wilson78501ea2010-10-27 12:18:21 +0100451 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700452 drm_i915_private_t *dev_priv = dev->dev_private;
453 int nbox = exec->num_cliprects;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700454 uint32_t exec_start, exec_len;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100455 int i, count, ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100456
Eric Anholt62fdfea2010-05-21 13:26:39 -0700457 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
458 exec_len = (uint32_t) exec->batch_len;
459
Chris Wilson6f392d5482010-08-07 11:01:22 +0100460 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700461
462 count = nbox ? nbox : 1;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700463 for (i = 0; i < count; i++) {
464 if (i < nbox) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100465 ret = i915_emit_box(dev, cliprects, i,
466 exec->DR1, exec->DR4);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700467 if (ret)
468 return ret;
469 }
470
471 if (IS_I830(dev) || IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100472 ret = intel_ring_begin(ring, 4);
473 if (ret)
474 return ret;
475
Chris Wilson78501ea2010-10-27 12:18:21 +0100476 intel_ring_emit(ring, MI_BATCH_BUFFER);
477 intel_ring_emit(ring, exec_start | MI_BATCH_NON_SECURE);
478 intel_ring_emit(ring, exec_start + exec_len - 4);
479 intel_ring_emit(ring, 0);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700480 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100481 ret = intel_ring_begin(ring, 2);
482 if (ret)
483 return ret;
484
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100485 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100486 intel_ring_emit(ring,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800487 MI_BATCH_BUFFER_START | (2 << 6)
488 | MI_BATCH_NON_SECURE_I965);
Chris Wilson78501ea2010-10-27 12:18:21 +0100489 intel_ring_emit(ring, exec_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700490 } else {
Chris Wilson78501ea2010-10-27 12:18:21 +0100491 intel_ring_emit(ring, MI_BATCH_BUFFER_START
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800492 | (2 << 6));
Chris Wilson78501ea2010-10-27 12:18:21 +0100493 intel_ring_emit(ring, exec_start |
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800494 MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700495 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700496 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100497 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700498 }
499
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100500 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100501 if (intel_ring_begin(ring, 2) == 0) {
502 intel_ring_emit(ring, MI_FLUSH |
503 MI_NO_WRITE_FLUSH |
504 MI_INVALIDATE_ISP );
505 intel_ring_emit(ring, MI_NOOP);
506 intel_ring_advance(ring);
507 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800508 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700509 /* XXX breadcrumb */
Zou Nan hai1cafd342010-06-25 13:40:24 +0800510
Eric Anholt62fdfea2010-05-21 13:26:39 -0700511 return 0;
512}
513
Chris Wilson78501ea2010-10-27 12:18:21 +0100514static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700515{
Chris Wilson78501ea2010-10-27 12:18:21 +0100516 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700517 struct drm_gem_object *obj;
518 struct drm_i915_gem_object *obj_priv;
519
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800520 obj = ring->status_page.obj;
521 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700522 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700523 obj_priv = to_intel_bo(obj);
524
525 kunmap(obj_priv->pages[0]);
526 i915_gem_object_unpin(obj);
527 drm_gem_object_unreference(obj);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800528 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700529
530 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700531}
532
Chris Wilson78501ea2010-10-27 12:18:21 +0100533static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700534{
Chris Wilson78501ea2010-10-27 12:18:21 +0100535 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700536 drm_i915_private_t *dev_priv = dev->dev_private;
537 struct drm_gem_object *obj;
538 struct drm_i915_gem_object *obj_priv;
539 int ret;
540
Eric Anholt62fdfea2010-05-21 13:26:39 -0700541 obj = i915_gem_alloc_object(dev, 4096);
542 if (obj == NULL) {
543 DRM_ERROR("Failed to allocate status page\n");
544 ret = -ENOMEM;
545 goto err;
546 }
547 obj_priv = to_intel_bo(obj);
548 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
549
Chris Wilsona00b10c2010-09-24 21:15:47 +0100550 ret = i915_gem_object_pin(obj, 4096, true, false);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700551 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700552 goto err_unref;
553 }
554
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800555 ring->status_page.gfx_addr = obj_priv->gtt_offset;
556 ring->status_page.page_addr = kmap(obj_priv->pages[0]);
557 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700558 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700559 goto err_unpin;
560 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800561 ring->status_page.obj = obj;
562 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700563
Chris Wilson78501ea2010-10-27 12:18:21 +0100564 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800565 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
566 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700567
568 return 0;
569
570err_unpin:
571 i915_gem_object_unpin(obj);
572err_unref:
573 drm_gem_object_unreference(obj);
574err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800575 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700576}
577
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800578int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100579 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700580{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800581 struct drm_i915_gem_object *obj_priv;
582 struct drm_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100583 int ret;
584
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800585 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100586 INIT_LIST_HEAD(&ring->active_list);
587 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100588 INIT_LIST_HEAD(&ring->gpu_write_list);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700589
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800590 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100591 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800592 if (ret)
593 return ret;
594 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700595
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800596 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700597 if (obj == NULL) {
598 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800599 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100600 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700601 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700602
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800603 ring->gem_object = obj;
604
Chris Wilsona00b10c2010-09-24 21:15:47 +0100605 ret = i915_gem_object_pin(obj, PAGE_SIZE, true, false);
Chris Wilsondd785e32010-08-07 11:01:34 +0100606 if (ret)
607 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700608
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800609 obj_priv = to_intel_bo(obj);
610 ring->map.size = ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700611 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700612 ring->map.type = 0;
613 ring->map.flags = 0;
614 ring->map.mtrr = 0;
615
616 drm_core_ioremap_wc(&ring->map, dev);
617 if (ring->map.handle == NULL) {
618 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800619 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100620 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700621 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800622
Eric Anholt62fdfea2010-05-21 13:26:39 -0700623 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100624 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100625 if (ret)
626 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700627
Chris Wilsonc584fe42010-10-29 18:15:52 +0100628 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100629
630err_unmap:
631 drm_core_ioremapfree(&ring->map, dev);
632err_unpin:
633 i915_gem_object_unpin(obj);
634err_unref:
635 drm_gem_object_unreference(obj);
636 ring->gem_object = NULL;
637err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100638 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800639 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700640}
641
Chris Wilson78501ea2010-10-27 12:18:21 +0100642void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700643{
Chris Wilson33626e62010-10-29 16:18:36 +0100644 struct drm_i915_private *dev_priv;
645 int ret;
646
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800647 if (ring->gem_object == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700648 return;
649
Chris Wilson33626e62010-10-29 16:18:36 +0100650 /* Disable the ring buffer. The ring must be idle at this point */
651 dev_priv = ring->dev->dev_private;
652 ret = intel_wait_ring_buffer(ring, ring->size - 8);
653 I915_WRITE_CTL(ring, 0);
654
Chris Wilson78501ea2010-10-27 12:18:21 +0100655 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700656
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800657 i915_gem_object_unpin(ring->gem_object);
658 drm_gem_object_unreference(ring->gem_object);
659 ring->gem_object = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100660
661 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700662}
663
Chris Wilson78501ea2010-10-27 12:18:21 +0100664static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700665{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800666 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700667 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800668 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700669
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800670 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100671 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700672 if (ret)
673 return ret;
674 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700675
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800676 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100677 rem /= 8;
678 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700679 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100680 *virt++ = MI_NOOP;
681 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700682
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800683 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100684 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700685
686 return 0;
687}
688
Chris Wilson78501ea2010-10-27 12:18:21 +0100689int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700690{
Chris Wilson78501ea2010-10-27 12:18:21 +0100691 struct drm_device *dev = ring->dev;
Daniel Vetter570ef602010-08-02 17:06:23 +0200692 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100693 unsigned long end;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700694
695 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800696 end = jiffies + 3 * HZ;
697 do {
Daniel Vetter570ef602010-08-02 17:06:23 +0200698 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700699 ring->space = ring->head - (ring->tail + 8);
700 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800701 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700702 if (ring->space >= n) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100703 trace_i915_ring_wait_end(dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700704 return 0;
705 }
706
707 if (dev->primary->master) {
708 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
709 if (master_priv->sarea_priv)
710 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
711 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800712
Chris Wilsone60a0b12010-10-13 10:09:14 +0100713 msleep(1);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800714 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700715 trace_i915_ring_wait_end (dev);
716 return -EBUSY;
717}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800718
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100719int intel_ring_begin(struct intel_ring_buffer *ring,
720 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800721{
Zou Nan haibe26a102010-06-12 17:40:24 +0800722 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100723 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100724
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100725 if (unlikely(ring->tail + n > ring->size)) {
726 ret = intel_wrap_ring_buffer(ring);
727 if (unlikely(ret))
728 return ret;
729 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100730
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100731 if (unlikely(ring->space < n)) {
732 ret = intel_wait_ring_buffer(ring, n);
733 if (unlikely(ret))
734 return ret;
735 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100736
737 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100738 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800739}
740
Chris Wilson78501ea2010-10-27 12:18:21 +0100741void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800742{
Chris Wilsond97ed332010-08-04 15:18:13 +0100743 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100744 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800745}
746
Chris Wilsone0708682010-09-19 14:46:27 +0100747static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800748 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100749 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200750 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800751 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800752 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100753 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800754 .flush = render_ring_flush,
755 .add_request = render_ring_add_request,
Chris Wilsonf787a5f2010-09-24 16:02:42 +0100756 .get_seqno = render_ring_get_seqno,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800757 .user_irq_get = render_ring_get_user_irq,
758 .user_irq_put = render_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100759 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800760};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800761
762/* ring buffer for bit-stream decoder */
763
Chris Wilsone0708682010-09-19 14:46:27 +0100764static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +0800765 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +0100766 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200767 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800768 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100769 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100770 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800771 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +0100772 .add_request = ring_add_request,
773 .get_seqno = ring_status_page_get_seqno,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800774 .user_irq_get = bsd_ring_get_user_irq,
775 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100776 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800777};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800778
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100779
Chris Wilson78501ea2010-10-27 12:18:21 +0100780static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100781 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100782{
Chris Wilson78501ea2010-10-27 12:18:21 +0100783 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100784
785 /* Every tail move must follow the sequence below */
786 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
787 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
788 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
789 I915_WRITE(GEN6_BSD_RNCID, 0x0);
790
791 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
792 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
793 50))
794 DRM_ERROR("timed out waiting for IDLE Indicator\n");
795
Daniel Vetter870e86d2010-08-02 16:29:44 +0200796 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100797 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
798 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
799 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
800}
801
Chris Wilson78501ea2010-10-27 12:18:21 +0100802static void gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson549f7362010-10-19 11:19:32 +0100803 u32 invalidate_domains,
804 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100805{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100806 if (intel_ring_begin(ring, 4) == 0) {
807 intel_ring_emit(ring, MI_FLUSH_DW);
808 intel_ring_emit(ring, 0);
809 intel_ring_emit(ring, 0);
810 intel_ring_emit(ring, 0);
811 intel_ring_advance(ring);
812 }
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100813}
814
815static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100816gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
817 struct drm_i915_gem_execbuffer2 *exec,
818 struct drm_clip_rect *cliprects,
819 uint64_t exec_offset)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100820{
821 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100822 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100823
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100824 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100825
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100826 ret = intel_ring_begin(ring, 2);
827 if (ret)
828 return ret;
829
Chris Wilson78501ea2010-10-27 12:18:21 +0100830 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100831 /* bit0-7 is the length on GEN6+ */
Chris Wilson78501ea2010-10-27 12:18:21 +0100832 intel_ring_emit(ring, exec_start);
833 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100834
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100835 return 0;
836}
837
838/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +0100839static const struct intel_ring_buffer gen6_bsd_ring = {
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100840 .name = "gen6 bsd ring",
841 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200842 .mmio_base = GEN6_BSD_RING_BASE,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100843 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100844 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100845 .write_tail = gen6_bsd_ring_write_tail,
Chris Wilson549f7362010-10-19 11:19:32 +0100846 .flush = gen6_ring_flush,
847 .add_request = ring_add_request,
848 .get_seqno = ring_status_page_get_seqno,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100849 .user_irq_get = bsd_ring_get_user_irq,
850 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100851 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +0100852};
853
854/* Blitter support (SandyBridge+) */
855
856static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100857blt_ring_get_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100858{
859 /* do nothing */
860}
861static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100862blt_ring_put_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100863{
864 /* do nothing */
865}
866
867static const struct intel_ring_buffer gen6_blt_ring = {
868 .name = "blt ring",
869 .id = RING_BLT,
870 .mmio_base = BLT_RING_BASE,
871 .size = 32 * PAGE_SIZE,
872 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100873 .write_tail = ring_write_tail,
Chris Wilson549f7362010-10-19 11:19:32 +0100874 .flush = gen6_ring_flush,
875 .add_request = ring_add_request,
876 .get_seqno = ring_status_page_get_seqno,
877 .user_irq_get = blt_ring_get_user_irq,
878 .user_irq_put = blt_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100879 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100880};
881
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800882int intel_init_render_ring_buffer(struct drm_device *dev)
883{
884 drm_i915_private_t *dev_priv = dev->dev_private;
885
886 dev_priv->render_ring = render_ring;
887
888 if (!I915_NEED_GFX_HWS(dev)) {
889 dev_priv->render_ring.status_page.page_addr
890 = dev_priv->status_page_dmah->vaddr;
891 memset(dev_priv->render_ring.status_page.page_addr,
892 0, PAGE_SIZE);
893 }
894
895 return intel_init_ring_buffer(dev, &dev_priv->render_ring);
896}
897
898int intel_init_bsd_ring_buffer(struct drm_device *dev)
899{
900 drm_i915_private_t *dev_priv = dev->dev_private;
901
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100902 if (IS_GEN6(dev))
903 dev_priv->bsd_ring = gen6_bsd_ring;
904 else
905 dev_priv->bsd_ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800906
907 return intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
908}
Chris Wilson549f7362010-10-19 11:19:32 +0100909
910int intel_init_blt_ring_buffer(struct drm_device *dev)
911{
912 drm_i915_private_t *dev_priv = dev->dev_private;
913
914 dev_priv->blt_ring = gen6_blt_ring;
915
916 return intel_init_ring_buffer(dev, &dev_priv->blt_ring);
917}