blob: 4803b32f308fcf293426eb2d129e639091d8a8cf [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
Daniel Vetter570ef602010-08-02 17:06:23 +0200179 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800180 /* If the head is still not zero, the ring is dead */
181 if (head != 0) {
182 DRM_ERROR("%s initialization failed "
183 "ctl %08x head %08x tail %08x start %08x\n",
184 ring->name,
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200185 I915_READ_CTL(ring),
Daniel Vetter570ef602010-08-02 17:06:23 +0200186 I915_READ_HEAD(ring),
Daniel Vetter870e86d2010-08-02 16:29:44 +0200187 I915_READ_TAIL(ring),
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200188 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800189 return -EIO;
190 }
191
Chris Wilson78501ea2010-10-27 12:18:21 +0100192 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
193 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800194 else {
Daniel Vetter570ef602010-08-02 17:06:23 +0200195 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Daniel Vetter870e86d2010-08-02 16:29:44 +0200196 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800197 ring->space = ring->head - (ring->tail + 8);
198 if (ring->space < 0)
199 ring->space += ring->size;
200 }
201 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700202}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800203
Chris Wilson78501ea2010-10-27 12:18:21 +0100204static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800205{
Chris Wilson78501ea2010-10-27 12:18:21 +0100206 struct drm_device *dev = ring->dev;
207 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800208
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100209 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100210 drm_i915_private_t *dev_priv = dev->dev_private;
211 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800212 if (IS_GEN6(dev))
213 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
214 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800215 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100216
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800217 return ret;
218}
219
Chris Wilson78501ea2010-10-27 12:18:21 +0100220#define PIPE_CONTROL_FLUSH(ring__, addr__) \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800221do { \
Chris Wilson78501ea2010-10-27 12:18:21 +0100222 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
Zhenyu Wangca764822010-05-27 10:26:42 +0800223 PIPE_CONTROL_DEPTH_STALL | 2); \
Chris Wilson78501ea2010-10-27 12:18:21 +0100224 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
225 intel_ring_emit(ring__, 0); \
226 intel_ring_emit(ring__, 0); \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800227} while (0)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700228
229/**
230 * Creates a new sequence number, emitting a write of it to the status page
231 * plus an interrupt, which will trigger i915_user_interrupt_handler.
232 *
233 * Must be called with struct_lock held.
234 *
235 * Returned sequence numbers are nonzero on success.
236 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100237static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100238render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100239 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700240{
Chris Wilson78501ea2010-10-27 12:18:21 +0100241 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700242 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson3cce4692010-10-27 16:11:02 +0100243 u32 seqno = i915_gem_get_seqno(dev);
244 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800245
246 if (IS_GEN6(dev)) {
Chris Wilson3cce4692010-10-27 16:11:02 +0100247 ret = intel_ring_begin(ring, 6);
248 if (ret)
249 return ret;
250
251 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | 3);
252 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE |
253 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_IS_FLUSH |
254 PIPE_CONTROL_NOTIFY);
255 intel_ring_emit(ring, dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
256 intel_ring_emit(ring, seqno);
257 intel_ring_emit(ring, 0);
258 intel_ring_emit(ring, 0);
Zhenyu Wangca764822010-05-27 10:26:42 +0800259 } else if (HAS_PIPE_CONTROL(dev)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700260 u32 scratch_addr = dev_priv->seqno_gfx_addr + 128;
261
262 /*
263 * Workaround qword write incoherence by flushing the
264 * PIPE_NOTIFY buffers out to memory before requesting
265 * an interrupt.
266 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100267 ret = intel_ring_begin(ring, 32);
268 if (ret)
269 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700270
Chris Wilson3cce4692010-10-27 16:11:02 +0100271 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
272 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
273 intel_ring_emit(ring, dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
274 intel_ring_emit(ring, seqno);
275 intel_ring_emit(ring, 0);
276 PIPE_CONTROL_FLUSH(ring, scratch_addr);
277 scratch_addr += 128; /* write to separate cachelines */
278 PIPE_CONTROL_FLUSH(ring, scratch_addr);
279 scratch_addr += 128;
280 PIPE_CONTROL_FLUSH(ring, scratch_addr);
281 scratch_addr += 128;
282 PIPE_CONTROL_FLUSH(ring, scratch_addr);
283 scratch_addr += 128;
284 PIPE_CONTROL_FLUSH(ring, scratch_addr);
285 scratch_addr += 128;
286 PIPE_CONTROL_FLUSH(ring, scratch_addr);
287 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
288 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
289 PIPE_CONTROL_NOTIFY);
290 intel_ring_emit(ring, dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
291 intel_ring_emit(ring, seqno);
292 intel_ring_emit(ring, 0);
293 } else {
294 ret = intel_ring_begin(ring, 4);
295 if (ret)
296 return ret;
297
298 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
299 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
300 intel_ring_emit(ring, seqno);
301
302 intel_ring_emit(ring, MI_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700303 }
Chris Wilson3cce4692010-10-27 16:11:02 +0100304
305 intel_ring_advance(ring);
306 *result = seqno;
307 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700308}
309
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800310static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100311render_ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800312{
Chris Wilson78501ea2010-10-27 12:18:21 +0100313 struct drm_device *dev = ring->dev;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800314 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
315 if (HAS_PIPE_CONTROL(dev))
316 return ((volatile u32 *)(dev_priv->seqno_page))[0];
317 else
318 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
319}
320
321static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100322render_ring_get_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700323{
Chris Wilson78501ea2010-10-27 12:18:21 +0100324 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700325 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
326 unsigned long irqflags;
327
328 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800329 if (dev->irq_enabled && (++ring->user_irq_refcount == 1)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700330 if (HAS_PCH_SPLIT(dev))
331 ironlake_enable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
332 else
333 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
334 }
335 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
336}
337
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800338static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100339render_ring_put_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700340{
Chris Wilson78501ea2010-10-27 12:18:21 +0100341 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700342 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
343 unsigned long irqflags;
344
345 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800346 BUG_ON(dev->irq_enabled && ring->user_irq_refcount <= 0);
347 if (dev->irq_enabled && (--ring->user_irq_refcount == 0)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700348 if (HAS_PCH_SPLIT(dev))
349 ironlake_disable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
350 else
351 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
352 }
353 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
354}
355
Chris Wilson78501ea2010-10-27 12:18:21 +0100356void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800357{
Chris Wilson78501ea2010-10-27 12:18:21 +0100358 drm_i915_private_t *dev_priv = ring->dev->dev_private;
359 u32 mmio = IS_GEN6(ring->dev) ?
360 RING_HWS_PGA_GEN6(ring->mmio_base) :
361 RING_HWS_PGA(ring->mmio_base);
362 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
363 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800364}
365
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100366static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100367bsd_ring_flush(struct intel_ring_buffer *ring,
368 u32 invalidate_domains,
369 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800370{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100371 if (intel_ring_begin(ring, 2) == 0) {
372 intel_ring_emit(ring, MI_FLUSH);
373 intel_ring_emit(ring, MI_NOOP);
374 intel_ring_advance(ring);
375 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800376}
377
Chris Wilson3cce4692010-10-27 16:11:02 +0100378static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100379ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100380 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800381{
382 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100383 int ret;
384
385 ret = intel_ring_begin(ring, 4);
386 if (ret)
387 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100388
Chris Wilson78501ea2010-10-27 12:18:21 +0100389 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d5482010-08-07 11:01:22 +0100390
Chris Wilson3cce4692010-10-27 16:11:02 +0100391 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
392 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
393 intel_ring_emit(ring, seqno);
394 intel_ring_emit(ring, MI_USER_INTERRUPT);
395 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800396
397 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +0100398 *result = seqno;
399 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800400}
401
Zou Nan haid1b851f2010-05-21 09:08:57 +0800402static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100403bsd_ring_get_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800404{
405 /* do nothing */
406}
407static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100408bsd_ring_put_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800409{
410 /* do nothing */
411}
412
413static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100414ring_status_page_get_seqno(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800415{
416 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
417}
418
419static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100420ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
421 struct drm_i915_gem_execbuffer2 *exec,
422 struct drm_clip_rect *cliprects,
423 uint64_t exec_offset)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800424{
425 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100426 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100427
Zou Nan haid1b851f2010-05-21 09:08:57 +0800428 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilson78501ea2010-10-27 12:18:21 +0100429
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100430 ret = intel_ring_begin(ring, 2);
431 if (ret)
432 return ret;
433
Chris Wilson78501ea2010-10-27 12:18:21 +0100434 intel_ring_emit(ring,
435 MI_BATCH_BUFFER_START |
436 (2 << 6) |
437 MI_BATCH_NON_SECURE_I965);
438 intel_ring_emit(ring, exec_start);
439 intel_ring_advance(ring);
440
Zou Nan haid1b851f2010-05-21 09:08:57 +0800441 return 0;
442}
443
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800444static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100445render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
446 struct drm_i915_gem_execbuffer2 *exec,
447 struct drm_clip_rect *cliprects,
448 uint64_t exec_offset)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700449{
Chris Wilson78501ea2010-10-27 12:18:21 +0100450 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700451 drm_i915_private_t *dev_priv = dev->dev_private;
452 int nbox = exec->num_cliprects;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700453 uint32_t exec_start, exec_len;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100454 int i, count, ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100455
Eric Anholt62fdfea2010-05-21 13:26:39 -0700456 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
457 exec_len = (uint32_t) exec->batch_len;
458
Chris Wilson6f392d5482010-08-07 11:01:22 +0100459 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700460
461 count = nbox ? nbox : 1;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700462 for (i = 0; i < count; i++) {
463 if (i < nbox) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100464 ret = i915_emit_box(dev, cliprects, i,
465 exec->DR1, exec->DR4);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700466 if (ret)
467 return ret;
468 }
469
470 if (IS_I830(dev) || IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100471 ret = intel_ring_begin(ring, 4);
472 if (ret)
473 return ret;
474
Chris Wilson78501ea2010-10-27 12:18:21 +0100475 intel_ring_emit(ring, MI_BATCH_BUFFER);
476 intel_ring_emit(ring, exec_start | MI_BATCH_NON_SECURE);
477 intel_ring_emit(ring, exec_start + exec_len - 4);
478 intel_ring_emit(ring, 0);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700479 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100480 ret = intel_ring_begin(ring, 2);
481 if (ret)
482 return ret;
483
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100484 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100485 intel_ring_emit(ring,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800486 MI_BATCH_BUFFER_START | (2 << 6)
487 | MI_BATCH_NON_SECURE_I965);
Chris Wilson78501ea2010-10-27 12:18:21 +0100488 intel_ring_emit(ring, exec_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700489 } else {
Chris Wilson78501ea2010-10-27 12:18:21 +0100490 intel_ring_emit(ring, MI_BATCH_BUFFER_START
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800491 | (2 << 6));
Chris Wilson78501ea2010-10-27 12:18:21 +0100492 intel_ring_emit(ring, exec_start |
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800493 MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700494 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700495 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100496 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700497 }
498
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100499 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100500 if (intel_ring_begin(ring, 2) == 0) {
501 intel_ring_emit(ring, MI_FLUSH |
502 MI_NO_WRITE_FLUSH |
503 MI_INVALIDATE_ISP );
504 intel_ring_emit(ring, MI_NOOP);
505 intel_ring_advance(ring);
506 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800507 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700508 /* XXX breadcrumb */
Zou Nan hai1cafd342010-06-25 13:40:24 +0800509
Eric Anholt62fdfea2010-05-21 13:26:39 -0700510 return 0;
511}
512
Chris Wilson78501ea2010-10-27 12:18:21 +0100513static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700514{
Chris Wilson78501ea2010-10-27 12:18:21 +0100515 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700516 struct drm_gem_object *obj;
517 struct drm_i915_gem_object *obj_priv;
518
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800519 obj = ring->status_page.obj;
520 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700521 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700522 obj_priv = to_intel_bo(obj);
523
524 kunmap(obj_priv->pages[0]);
525 i915_gem_object_unpin(obj);
526 drm_gem_object_unreference(obj);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800527 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700528
529 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700530}
531
Chris Wilson78501ea2010-10-27 12:18:21 +0100532static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700533{
Chris Wilson78501ea2010-10-27 12:18:21 +0100534 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700535 drm_i915_private_t *dev_priv = dev->dev_private;
536 struct drm_gem_object *obj;
537 struct drm_i915_gem_object *obj_priv;
538 int ret;
539
Eric Anholt62fdfea2010-05-21 13:26:39 -0700540 obj = i915_gem_alloc_object(dev, 4096);
541 if (obj == NULL) {
542 DRM_ERROR("Failed to allocate status page\n");
543 ret = -ENOMEM;
544 goto err;
545 }
546 obj_priv = to_intel_bo(obj);
547 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
548
549 ret = i915_gem_object_pin(obj, 4096);
550 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700551 goto err_unref;
552 }
553
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800554 ring->status_page.gfx_addr = obj_priv->gtt_offset;
555 ring->status_page.page_addr = kmap(obj_priv->pages[0]);
556 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700557 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700558 goto err_unpin;
559 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800560 ring->status_page.obj = obj;
561 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700562
Chris Wilson78501ea2010-10-27 12:18:21 +0100563 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800564 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
565 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700566
567 return 0;
568
569err_unpin:
570 i915_gem_object_unpin(obj);
571err_unref:
572 drm_gem_object_unreference(obj);
573err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800574 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700575}
576
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800577int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100578 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700579{
Daniel Vetter870e86d2010-08-02 16:29:44 +0200580 struct drm_i915_private *dev_priv = dev->dev_private;
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
Daniel Vettera9db5c82010-08-02 17:22:48 +0200605 ret = i915_gem_object_pin(obj, PAGE_SIZE);
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
Eric Anholt62fdfea2010-05-21 13:26:39 -0700628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
629 i915_kernel_lost_context(dev);
630 else {
Daniel Vetter570ef602010-08-02 17:06:23 +0200631 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Daniel Vetter870e86d2010-08-02 16:29:44 +0200632 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700633 ring->space = ring->head - (ring->tail + 8);
634 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800635 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700636 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800637 return ret;
Chris Wilsondd785e32010-08-07 11:01:34 +0100638
639err_unmap:
640 drm_core_ioremapfree(&ring->map, dev);
641err_unpin:
642 i915_gem_object_unpin(obj);
643err_unref:
644 drm_gem_object_unreference(obj);
645 ring->gem_object = NULL;
646err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100647 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800648 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700649}
650
Chris Wilson78501ea2010-10-27 12:18:21 +0100651void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700652{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800653 if (ring->gem_object == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700654 return;
655
Chris Wilson78501ea2010-10-27 12:18:21 +0100656 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700657
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800658 i915_gem_object_unpin(ring->gem_object);
659 drm_gem_object_unreference(ring->gem_object);
660 ring->gem_object = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100661
662 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700663}
664
Chris Wilson78501ea2010-10-27 12:18:21 +0100665static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700666{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800667 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700668 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800669 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700670
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800671 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100672 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700673 if (ret)
674 return ret;
675 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700676
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800677 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100678 rem /= 8;
679 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700680 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100681 *virt++ = MI_NOOP;
682 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700683
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800684 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100685 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700686
687 return 0;
688}
689
Chris Wilson78501ea2010-10-27 12:18:21 +0100690int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700691{
Chris Wilson78501ea2010-10-27 12:18:21 +0100692 struct drm_device *dev = ring->dev;
Daniel Vetter570ef602010-08-02 17:06:23 +0200693 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100694 unsigned long end;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700695
696 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800697 end = jiffies + 3 * HZ;
698 do {
Daniel Vetter570ef602010-08-02 17:06:23 +0200699 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700700 ring->space = ring->head - (ring->tail + 8);
701 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800702 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700703 if (ring->space >= n) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100704 trace_i915_ring_wait_end(dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700705 return 0;
706 }
707
708 if (dev->primary->master) {
709 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
710 if (master_priv->sarea_priv)
711 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
712 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800713
Chris Wilsone60a0b12010-10-13 10:09:14 +0100714 msleep(1);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800715 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700716 trace_i915_ring_wait_end (dev);
717 return -EBUSY;
718}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800719
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100720int intel_ring_begin(struct intel_ring_buffer *ring,
721 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800722{
Zou Nan haibe26a102010-06-12 17:40:24 +0800723 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100724 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100725
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100726 if (unlikely(ring->tail + n > ring->size)) {
727 ret = intel_wrap_ring_buffer(ring);
728 if (unlikely(ret))
729 return ret;
730 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100731
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100732 if (unlikely(ring->space < n)) {
733 ret = intel_wait_ring_buffer(ring, n);
734 if (unlikely(ret))
735 return ret;
736 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100737
738 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100739 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800740}
741
Chris Wilson78501ea2010-10-27 12:18:21 +0100742void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800743{
Chris Wilsond97ed332010-08-04 15:18:13 +0100744 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100745 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800746}
747
Chris Wilsone0708682010-09-19 14:46:27 +0100748static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800749 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100750 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200751 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800752 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800753 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100754 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800755 .flush = render_ring_flush,
756 .add_request = render_ring_add_request,
Chris Wilsonf787a5f2010-09-24 16:02:42 +0100757 .get_seqno = render_ring_get_seqno,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800758 .user_irq_get = render_ring_get_user_irq,
759 .user_irq_put = render_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100760 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800761};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800762
763/* ring buffer for bit-stream decoder */
764
Chris Wilsone0708682010-09-19 14:46:27 +0100765static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +0800766 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +0100767 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200768 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800769 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100770 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100771 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800772 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +0100773 .add_request = ring_add_request,
774 .get_seqno = ring_status_page_get_seqno,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800775 .user_irq_get = bsd_ring_get_user_irq,
776 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100777 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800778};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800779
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100780
Chris Wilson78501ea2010-10-27 12:18:21 +0100781static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100782 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100783{
Chris Wilson78501ea2010-10-27 12:18:21 +0100784 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100785
786 /* Every tail move must follow the sequence below */
787 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
788 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
789 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
790 I915_WRITE(GEN6_BSD_RNCID, 0x0);
791
792 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
793 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
794 50))
795 DRM_ERROR("timed out waiting for IDLE Indicator\n");
796
Daniel Vetter870e86d2010-08-02 16:29:44 +0200797 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100798 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
799 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
800 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
801}
802
Chris Wilson78501ea2010-10-27 12:18:21 +0100803static void gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson549f7362010-10-19 11:19:32 +0100804 u32 invalidate_domains,
805 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100806{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100807 if (intel_ring_begin(ring, 4) == 0) {
808 intel_ring_emit(ring, MI_FLUSH_DW);
809 intel_ring_emit(ring, 0);
810 intel_ring_emit(ring, 0);
811 intel_ring_emit(ring, 0);
812 intel_ring_advance(ring);
813 }
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100814}
815
816static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100817gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
818 struct drm_i915_gem_execbuffer2 *exec,
819 struct drm_clip_rect *cliprects,
820 uint64_t exec_offset)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100821{
822 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100823 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100824
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100825 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100826
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100827 ret = intel_ring_begin(ring, 2);
828 if (ret)
829 return ret;
830
Chris Wilson78501ea2010-10-27 12:18:21 +0100831 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100832 /* bit0-7 is the length on GEN6+ */
Chris Wilson78501ea2010-10-27 12:18:21 +0100833 intel_ring_emit(ring, exec_start);
834 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100835
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100836 return 0;
837}
838
839/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +0100840static const struct intel_ring_buffer gen6_bsd_ring = {
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100841 .name = "gen6 bsd ring",
842 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200843 .mmio_base = GEN6_BSD_RING_BASE,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100844 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100845 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100846 .write_tail = gen6_bsd_ring_write_tail,
Chris Wilson549f7362010-10-19 11:19:32 +0100847 .flush = gen6_ring_flush,
848 .add_request = ring_add_request,
849 .get_seqno = ring_status_page_get_seqno,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100850 .user_irq_get = bsd_ring_get_user_irq,
851 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100852 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +0100853};
854
855/* Blitter support (SandyBridge+) */
856
857static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100858blt_ring_get_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100859{
860 /* do nothing */
861}
862static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100863blt_ring_put_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100864{
865 /* do nothing */
866}
867
868static const struct intel_ring_buffer gen6_blt_ring = {
869 .name = "blt ring",
870 .id = RING_BLT,
871 .mmio_base = BLT_RING_BASE,
872 .size = 32 * PAGE_SIZE,
873 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100874 .write_tail = ring_write_tail,
Chris Wilson549f7362010-10-19 11:19:32 +0100875 .flush = gen6_ring_flush,
876 .add_request = ring_add_request,
877 .get_seqno = ring_status_page_get_seqno,
878 .user_irq_get = blt_ring_get_user_irq,
879 .user_irq_put = blt_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100880 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100881};
882
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800883int intel_init_render_ring_buffer(struct drm_device *dev)
884{
885 drm_i915_private_t *dev_priv = dev->dev_private;
886
887 dev_priv->render_ring = render_ring;
888
889 if (!I915_NEED_GFX_HWS(dev)) {
890 dev_priv->render_ring.status_page.page_addr
891 = dev_priv->status_page_dmah->vaddr;
892 memset(dev_priv->render_ring.status_page.page_addr,
893 0, PAGE_SIZE);
894 }
895
896 return intel_init_ring_buffer(dev, &dev_priv->render_ring);
897}
898
899int intel_init_bsd_ring_buffer(struct drm_device *dev)
900{
901 drm_i915_private_t *dev_priv = dev->dev_private;
902
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100903 if (IS_GEN6(dev))
904 dev_priv->bsd_ring = gen6_bsd_ring;
905 else
906 dev_priv->bsd_ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800907
908 return intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
909}
Chris Wilson549f7362010-10-19 11:19:32 +0100910
911int intel_init_blt_ring_buffer(struct drm_device *dev)
912{
913 drm_i915_private_t *dev_priv = dev->dev_private;
914
915 dev_priv->blt_ring = gen6_blt_ring;
916
917 return intel_init_ring_buffer(dev, &dev_priv->blt_ring);
918}