blob: 99f2c96a9c70caf8715aa5ae06e151a54b419ff6 [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,
Chris Wilsonae69b422010-11-07 11:45:52 +0000176 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson6aa56062010-10-29 21:44:37 +0100177 | RING_REPORT_64K | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800178
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) {
Chris Wilson629e8942010-11-07 11:50:02 +0000183 if (IS_GEN6(ring->dev) && ring->dev->pdev->revision <= 8) {
184 /* Early revisions of Sandybridge do not like
185 * revealing the contents of the ring buffer
186 * registers whilst idle. Fortunately, the
187 * auto-reporting mechanism prevents most hangs,
188 * but this will bite us eventually...
189 */
190 DRM_DEBUG("%s initialization failed "
191 "ctl %08x head %08x tail %08x start %08x. Ignoring, hope for the best!\n",
192 ring->name,
193 I915_READ_CTL(ring),
194 I915_READ_HEAD(ring),
195 I915_READ_TAIL(ring),
196 I915_READ_START(ring));
197 } else {
198 DRM_ERROR("%s initialization failed "
199 "ctl %08x head %08x tail %08x start %08x\n",
200 ring->name,
201 I915_READ_CTL(ring),
202 I915_READ_HEAD(ring),
203 I915_READ_TAIL(ring),
204 I915_READ_START(ring));
205 return -EIO;
206 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800207 }
208
Chris Wilson78501ea2010-10-27 12:18:21 +0100209 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
210 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800211 else {
Daniel Vetter570ef602010-08-02 17:06:23 +0200212 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Daniel Vetter870e86d2010-08-02 16:29:44 +0200213 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800214 ring->space = ring->head - (ring->tail + 8);
215 if (ring->space < 0)
216 ring->space += ring->size;
217 }
218 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700219}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800220
Chris Wilson78501ea2010-10-27 12:18:21 +0100221static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800222{
Chris Wilson78501ea2010-10-27 12:18:21 +0100223 struct drm_device *dev = ring->dev;
224 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800225
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100226 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100227 drm_i915_private_t *dev_priv = dev->dev_private;
228 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800229 if (IS_GEN6(dev))
230 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
231 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800232 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100233
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800234 return ret;
235}
236
Chris Wilson78501ea2010-10-27 12:18:21 +0100237#define PIPE_CONTROL_FLUSH(ring__, addr__) \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800238do { \
Chris Wilson78501ea2010-10-27 12:18:21 +0100239 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
Zhenyu Wangca764822010-05-27 10:26:42 +0800240 PIPE_CONTROL_DEPTH_STALL | 2); \
Chris Wilson78501ea2010-10-27 12:18:21 +0100241 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
242 intel_ring_emit(ring__, 0); \
243 intel_ring_emit(ring__, 0); \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800244} while (0)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700245
246/**
247 * Creates a new sequence number, emitting a write of it to the status page
248 * plus an interrupt, which will trigger i915_user_interrupt_handler.
249 *
250 * Must be called with struct_lock held.
251 *
252 * Returned sequence numbers are nonzero on success.
253 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100254static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100255render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100256 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700257{
Chris Wilson78501ea2010-10-27 12:18:21 +0100258 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700259 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson3cce4692010-10-27 16:11:02 +0100260 u32 seqno = i915_gem_get_seqno(dev);
261 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800262
263 if (IS_GEN6(dev)) {
Chris Wilson3cce4692010-10-27 16:11:02 +0100264 ret = intel_ring_begin(ring, 6);
265 if (ret)
266 return ret;
267
268 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | 3);
269 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE |
270 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_IS_FLUSH |
271 PIPE_CONTROL_NOTIFY);
272 intel_ring_emit(ring, dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
273 intel_ring_emit(ring, seqno);
274 intel_ring_emit(ring, 0);
275 intel_ring_emit(ring, 0);
Zhenyu Wangca764822010-05-27 10:26:42 +0800276 } else if (HAS_PIPE_CONTROL(dev)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700277 u32 scratch_addr = dev_priv->seqno_gfx_addr + 128;
278
279 /*
280 * Workaround qword write incoherence by flushing the
281 * PIPE_NOTIFY buffers out to memory before requesting
282 * an interrupt.
283 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100284 ret = intel_ring_begin(ring, 32);
285 if (ret)
286 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700287
Chris Wilson3cce4692010-10-27 16:11:02 +0100288 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
289 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
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 PIPE_CONTROL_FLUSH(ring, scratch_addr);
294 scratch_addr += 128; /* write to separate cachelines */
295 PIPE_CONTROL_FLUSH(ring, scratch_addr);
296 scratch_addr += 128;
297 PIPE_CONTROL_FLUSH(ring, scratch_addr);
298 scratch_addr += 128;
299 PIPE_CONTROL_FLUSH(ring, scratch_addr);
300 scratch_addr += 128;
301 PIPE_CONTROL_FLUSH(ring, scratch_addr);
302 scratch_addr += 128;
303 PIPE_CONTROL_FLUSH(ring, scratch_addr);
304 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
305 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
306 PIPE_CONTROL_NOTIFY);
307 intel_ring_emit(ring, dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
308 intel_ring_emit(ring, seqno);
309 intel_ring_emit(ring, 0);
310 } else {
311 ret = intel_ring_begin(ring, 4);
312 if (ret)
313 return ret;
314
315 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
316 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
317 intel_ring_emit(ring, seqno);
318
319 intel_ring_emit(ring, MI_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700320 }
Chris Wilson3cce4692010-10-27 16:11:02 +0100321
322 intel_ring_advance(ring);
323 *result = seqno;
324 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700325}
326
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800327static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100328render_ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800329{
Chris Wilson78501ea2010-10-27 12:18:21 +0100330 struct drm_device *dev = ring->dev;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800331 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
332 if (HAS_PIPE_CONTROL(dev))
333 return ((volatile u32 *)(dev_priv->seqno_page))[0];
334 else
335 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
336}
337
338static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100339render_ring_get_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 if (dev->irq_enabled && (++ring->user_irq_refcount == 1)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700347 if (HAS_PCH_SPLIT(dev))
348 ironlake_enable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
349 else
350 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
351 }
352 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
353}
354
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800355static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100356render_ring_put_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700357{
Chris Wilson78501ea2010-10-27 12:18:21 +0100358 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700359 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
360 unsigned long irqflags;
361
362 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800363 BUG_ON(dev->irq_enabled && ring->user_irq_refcount <= 0);
364 if (dev->irq_enabled && (--ring->user_irq_refcount == 0)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700365 if (HAS_PCH_SPLIT(dev))
366 ironlake_disable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
367 else
368 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
369 }
370 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
371}
372
Chris Wilson78501ea2010-10-27 12:18:21 +0100373void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800374{
Chris Wilson78501ea2010-10-27 12:18:21 +0100375 drm_i915_private_t *dev_priv = ring->dev->dev_private;
376 u32 mmio = IS_GEN6(ring->dev) ?
377 RING_HWS_PGA_GEN6(ring->mmio_base) :
378 RING_HWS_PGA(ring->mmio_base);
379 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
380 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800381}
382
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100383static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100384bsd_ring_flush(struct intel_ring_buffer *ring,
385 u32 invalidate_domains,
386 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800387{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100388 if (intel_ring_begin(ring, 2) == 0) {
389 intel_ring_emit(ring, MI_FLUSH);
390 intel_ring_emit(ring, MI_NOOP);
391 intel_ring_advance(ring);
392 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800393}
394
Chris Wilson3cce4692010-10-27 16:11:02 +0100395static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100396ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100397 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800398{
399 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100400 int ret;
401
402 ret = intel_ring_begin(ring, 4);
403 if (ret)
404 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100405
Chris Wilson78501ea2010-10-27 12:18:21 +0100406 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d5482010-08-07 11:01:22 +0100407
Chris Wilson3cce4692010-10-27 16:11:02 +0100408 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
409 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
410 intel_ring_emit(ring, seqno);
411 intel_ring_emit(ring, MI_USER_INTERRUPT);
412 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800413
414 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +0100415 *result = seqno;
416 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800417}
418
Zou Nan haid1b851f2010-05-21 09:08:57 +0800419static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100420bsd_ring_get_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800421{
422 /* do nothing */
423}
424static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100425bsd_ring_put_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800426{
427 /* do nothing */
428}
429
430static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100431ring_status_page_get_seqno(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800432{
433 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
434}
435
436static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100437ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
438 struct drm_i915_gem_execbuffer2 *exec,
439 struct drm_clip_rect *cliprects,
440 uint64_t exec_offset)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800441{
442 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100443 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100444
Zou Nan haid1b851f2010-05-21 09:08:57 +0800445 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilson78501ea2010-10-27 12:18:21 +0100446
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100447 ret = intel_ring_begin(ring, 2);
448 if (ret)
449 return ret;
450
Chris Wilson78501ea2010-10-27 12:18:21 +0100451 intel_ring_emit(ring,
452 MI_BATCH_BUFFER_START |
453 (2 << 6) |
454 MI_BATCH_NON_SECURE_I965);
455 intel_ring_emit(ring, exec_start);
456 intel_ring_advance(ring);
457
Zou Nan haid1b851f2010-05-21 09:08:57 +0800458 return 0;
459}
460
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800461static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100462render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
463 struct drm_i915_gem_execbuffer2 *exec,
464 struct drm_clip_rect *cliprects,
465 uint64_t exec_offset)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700466{
Chris Wilson78501ea2010-10-27 12:18:21 +0100467 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700468 drm_i915_private_t *dev_priv = dev->dev_private;
469 int nbox = exec->num_cliprects;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700470 uint32_t exec_start, exec_len;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100471 int i, count, ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100472
Eric Anholt62fdfea2010-05-21 13:26:39 -0700473 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
474 exec_len = (uint32_t) exec->batch_len;
475
Chris Wilson6f392d5482010-08-07 11:01:22 +0100476 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700477
478 count = nbox ? nbox : 1;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700479 for (i = 0; i < count; i++) {
480 if (i < nbox) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100481 ret = i915_emit_box(dev, cliprects, i,
482 exec->DR1, exec->DR4);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700483 if (ret)
484 return ret;
485 }
486
487 if (IS_I830(dev) || IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100488 ret = intel_ring_begin(ring, 4);
489 if (ret)
490 return ret;
491
Chris Wilson78501ea2010-10-27 12:18:21 +0100492 intel_ring_emit(ring, MI_BATCH_BUFFER);
493 intel_ring_emit(ring, exec_start | MI_BATCH_NON_SECURE);
494 intel_ring_emit(ring, exec_start + exec_len - 4);
495 intel_ring_emit(ring, 0);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700496 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100497 ret = intel_ring_begin(ring, 2);
498 if (ret)
499 return ret;
500
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100501 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100502 intel_ring_emit(ring,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800503 MI_BATCH_BUFFER_START | (2 << 6)
504 | MI_BATCH_NON_SECURE_I965);
Chris Wilson78501ea2010-10-27 12:18:21 +0100505 intel_ring_emit(ring, exec_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700506 } else {
Chris Wilson78501ea2010-10-27 12:18:21 +0100507 intel_ring_emit(ring, MI_BATCH_BUFFER_START
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800508 | (2 << 6));
Chris Wilson78501ea2010-10-27 12:18:21 +0100509 intel_ring_emit(ring, exec_start |
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800510 MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700511 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700512 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100513 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700514 }
515
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100516 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100517 if (intel_ring_begin(ring, 2) == 0) {
518 intel_ring_emit(ring, MI_FLUSH |
519 MI_NO_WRITE_FLUSH |
520 MI_INVALIDATE_ISP );
521 intel_ring_emit(ring, MI_NOOP);
522 intel_ring_advance(ring);
523 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800524 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700525 /* XXX breadcrumb */
Zou Nan hai1cafd342010-06-25 13:40:24 +0800526
Eric Anholt62fdfea2010-05-21 13:26:39 -0700527 return 0;
528}
529
Chris Wilson78501ea2010-10-27 12:18:21 +0100530static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700531{
Chris Wilson78501ea2010-10-27 12:18:21 +0100532 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700533 struct drm_gem_object *obj;
534 struct drm_i915_gem_object *obj_priv;
535
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800536 obj = ring->status_page.obj;
537 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700538 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700539 obj_priv = to_intel_bo(obj);
540
541 kunmap(obj_priv->pages[0]);
542 i915_gem_object_unpin(obj);
543 drm_gem_object_unreference(obj);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800544 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700545
546 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700547}
548
Chris Wilson78501ea2010-10-27 12:18:21 +0100549static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700550{
Chris Wilson78501ea2010-10-27 12:18:21 +0100551 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700552 drm_i915_private_t *dev_priv = dev->dev_private;
553 struct drm_gem_object *obj;
554 struct drm_i915_gem_object *obj_priv;
555 int ret;
556
Eric Anholt62fdfea2010-05-21 13:26:39 -0700557 obj = i915_gem_alloc_object(dev, 4096);
558 if (obj == NULL) {
559 DRM_ERROR("Failed to allocate status page\n");
560 ret = -ENOMEM;
561 goto err;
562 }
563 obj_priv = to_intel_bo(obj);
564 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
565
Daniel Vetter75e9e912010-11-04 17:11:09 +0100566 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700567 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700568 goto err_unref;
569 }
570
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800571 ring->status_page.gfx_addr = obj_priv->gtt_offset;
572 ring->status_page.page_addr = kmap(obj_priv->pages[0]);
573 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700574 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700575 goto err_unpin;
576 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800577 ring->status_page.obj = obj;
578 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700579
Chris Wilson78501ea2010-10-27 12:18:21 +0100580 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800581 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
582 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700583
584 return 0;
585
586err_unpin:
587 i915_gem_object_unpin(obj);
588err_unref:
589 drm_gem_object_unreference(obj);
590err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800591 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700592}
593
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800594int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100595 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700596{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800597 struct drm_i915_gem_object *obj_priv;
598 struct drm_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100599 int ret;
600
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800601 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100602 INIT_LIST_HEAD(&ring->active_list);
603 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100604 INIT_LIST_HEAD(&ring->gpu_write_list);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700605
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800606 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100607 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800608 if (ret)
609 return ret;
610 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700611
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800612 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700613 if (obj == NULL) {
614 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800615 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100616 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700617 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700618
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800619 ring->gem_object = obj;
620
Daniel Vetter75e9e912010-11-04 17:11:09 +0100621 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100622 if (ret)
623 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700624
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800625 obj_priv = to_intel_bo(obj);
626 ring->map.size = ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700627 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700628 ring->map.type = 0;
629 ring->map.flags = 0;
630 ring->map.mtrr = 0;
631
632 drm_core_ioremap_wc(&ring->map, dev);
633 if (ring->map.handle == NULL) {
634 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800635 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100636 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700637 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800638
Eric Anholt62fdfea2010-05-21 13:26:39 -0700639 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100640 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100641 if (ret)
642 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700643
Chris Wilsonc584fe42010-10-29 18:15:52 +0100644 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100645
646err_unmap:
647 drm_core_ioremapfree(&ring->map, dev);
648err_unpin:
649 i915_gem_object_unpin(obj);
650err_unref:
651 drm_gem_object_unreference(obj);
652 ring->gem_object = NULL;
653err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100654 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800655 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700656}
657
Chris Wilson78501ea2010-10-27 12:18:21 +0100658void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700659{
Chris Wilson33626e62010-10-29 16:18:36 +0100660 struct drm_i915_private *dev_priv;
661 int ret;
662
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800663 if (ring->gem_object == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700664 return;
665
Chris Wilson33626e62010-10-29 16:18:36 +0100666 /* Disable the ring buffer. The ring must be idle at this point */
667 dev_priv = ring->dev->dev_private;
668 ret = intel_wait_ring_buffer(ring, ring->size - 8);
669 I915_WRITE_CTL(ring, 0);
670
Chris Wilson78501ea2010-10-27 12:18:21 +0100671 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700672
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800673 i915_gem_object_unpin(ring->gem_object);
674 drm_gem_object_unreference(ring->gem_object);
675 ring->gem_object = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100676
Zou Nan hai8d192152010-11-02 16:31:01 +0800677 if (ring->cleanup)
678 ring->cleanup(ring);
679
Chris Wilson78501ea2010-10-27 12:18:21 +0100680 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700681}
682
Chris Wilson78501ea2010-10-27 12:18:21 +0100683static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700684{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800685 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700686 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800687 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700688
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800689 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100690 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700691 if (ret)
692 return ret;
693 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700694
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800695 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100696 rem /= 8;
697 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700698 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100699 *virt++ = MI_NOOP;
700 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700701
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800702 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100703 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700704
705 return 0;
706}
707
Chris Wilson78501ea2010-10-27 12:18:21 +0100708int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700709{
Chris Wilson78501ea2010-10-27 12:18:21 +0100710 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800711 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100712 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100713 u32 head;
714
715 head = intel_read_status_page(ring, 4);
716 if (head) {
717 ring->head = head & HEAD_ADDR;
718 ring->space = ring->head - (ring->tail + 8);
719 if (ring->space < 0)
720 ring->space += ring->size;
721 if (ring->space >= n)
722 return 0;
723 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700724
725 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800726 end = jiffies + 3 * HZ;
727 do {
Daniel Vetter570ef602010-08-02 17:06:23 +0200728 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700729 ring->space = ring->head - (ring->tail + 8);
730 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800731 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700732 if (ring->space >= n) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100733 trace_i915_ring_wait_end(dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700734 return 0;
735 }
736
737 if (dev->primary->master) {
738 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
739 if (master_priv->sarea_priv)
740 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
741 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800742
Chris Wilsone60a0b12010-10-13 10:09:14 +0100743 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +0100744 if (atomic_read(&dev_priv->mm.wedged))
745 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800746 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700747 trace_i915_ring_wait_end (dev);
748 return -EBUSY;
749}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800750
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100751int intel_ring_begin(struct intel_ring_buffer *ring,
752 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800753{
Zou Nan haibe26a102010-06-12 17:40:24 +0800754 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100755 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100756
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100757 if (unlikely(ring->tail + n > ring->size)) {
758 ret = intel_wrap_ring_buffer(ring);
759 if (unlikely(ret))
760 return ret;
761 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100762
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100763 if (unlikely(ring->space < n)) {
764 ret = intel_wait_ring_buffer(ring, n);
765 if (unlikely(ret))
766 return ret;
767 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100768
769 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100770 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800771}
772
Chris Wilson78501ea2010-10-27 12:18:21 +0100773void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800774{
Chris Wilsond97ed332010-08-04 15:18:13 +0100775 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100776 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800777}
778
Chris Wilsone0708682010-09-19 14:46:27 +0100779static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800780 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100781 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200782 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800783 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800784 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100785 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800786 .flush = render_ring_flush,
787 .add_request = render_ring_add_request,
Chris Wilsonf787a5f2010-09-24 16:02:42 +0100788 .get_seqno = render_ring_get_seqno,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800789 .user_irq_get = render_ring_get_user_irq,
790 .user_irq_put = render_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100791 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800792};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800793
794/* ring buffer for bit-stream decoder */
795
Chris Wilsone0708682010-09-19 14:46:27 +0100796static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +0800797 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +0100798 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200799 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800800 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100801 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100802 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800803 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +0100804 .add_request = ring_add_request,
805 .get_seqno = ring_status_page_get_seqno,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800806 .user_irq_get = bsd_ring_get_user_irq,
807 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100808 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800809};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800810
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100811
Chris Wilson78501ea2010-10-27 12:18:21 +0100812static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100813 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100814{
Chris Wilson78501ea2010-10-27 12:18:21 +0100815 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100816
817 /* Every tail move must follow the sequence below */
818 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
819 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
820 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
821 I915_WRITE(GEN6_BSD_RNCID, 0x0);
822
823 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
824 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
825 50))
826 DRM_ERROR("timed out waiting for IDLE Indicator\n");
827
Daniel Vetter870e86d2010-08-02 16:29:44 +0200828 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100829 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
830 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
831 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
832}
833
Chris Wilson78501ea2010-10-27 12:18:21 +0100834static void gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson549f7362010-10-19 11:19:32 +0100835 u32 invalidate_domains,
836 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100837{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100838 if (intel_ring_begin(ring, 4) == 0) {
839 intel_ring_emit(ring, MI_FLUSH_DW);
840 intel_ring_emit(ring, 0);
841 intel_ring_emit(ring, 0);
842 intel_ring_emit(ring, 0);
843 intel_ring_advance(ring);
844 }
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100845}
846
847static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100848gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
849 struct drm_i915_gem_execbuffer2 *exec,
850 struct drm_clip_rect *cliprects,
851 uint64_t exec_offset)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100852{
853 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100854 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100855
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100856 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100857
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100858 ret = intel_ring_begin(ring, 2);
859 if (ret)
860 return ret;
861
Chris Wilson78501ea2010-10-27 12:18:21 +0100862 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100863 /* bit0-7 is the length on GEN6+ */
Chris Wilson78501ea2010-10-27 12:18:21 +0100864 intel_ring_emit(ring, exec_start);
865 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100866
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100867 return 0;
868}
869
870/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +0100871static const struct intel_ring_buffer gen6_bsd_ring = {
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100872 .name = "gen6 bsd ring",
873 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200874 .mmio_base = GEN6_BSD_RING_BASE,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100875 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100876 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100877 .write_tail = gen6_bsd_ring_write_tail,
Chris Wilson549f7362010-10-19 11:19:32 +0100878 .flush = gen6_ring_flush,
879 .add_request = ring_add_request,
880 .get_seqno = ring_status_page_get_seqno,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100881 .user_irq_get = bsd_ring_get_user_irq,
882 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100883 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +0100884};
885
886/* Blitter support (SandyBridge+) */
887
888static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100889blt_ring_get_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100890{
891 /* do nothing */
892}
893static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100894blt_ring_put_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100895{
896 /* do nothing */
897}
898
Zou Nan hai8d192152010-11-02 16:31:01 +0800899
900/* Workaround for some stepping of SNB,
901 * each time when BLT engine ring tail moved,
902 * the first command in the ring to be parsed
903 * should be MI_BATCH_BUFFER_START
904 */
905#define NEED_BLT_WORKAROUND(dev) \
906 (IS_GEN6(dev) && (dev->pdev->revision < 8))
907
908static inline struct drm_i915_gem_object *
909to_blt_workaround(struct intel_ring_buffer *ring)
910{
911 return ring->private;
912}
913
914static int blt_ring_init(struct intel_ring_buffer *ring)
915{
916 if (NEED_BLT_WORKAROUND(ring->dev)) {
917 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +0000918 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +0800919 int ret;
920
921 obj = to_intel_bo(i915_gem_alloc_object(ring->dev, 4096));
922 if (obj == NULL)
923 return -ENOMEM;
924
Daniel Vetter75e9e912010-11-04 17:11:09 +0100925 ret = i915_gem_object_pin(&obj->base, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +0800926 if (ret) {
927 drm_gem_object_unreference(&obj->base);
928 return ret;
929 }
930
931 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +0000932 *ptr++ = MI_BATCH_BUFFER_END;
933 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +0800934 kunmap(obj->pages[0]);
935
936 ret = i915_gem_object_set_to_gtt_domain(&obj->base, false);
937 if (ret) {
938 i915_gem_object_unpin(&obj->base);
939 drm_gem_object_unreference(&obj->base);
940 return ret;
941 }
942
943 ring->private = obj;
944 }
945
946 return init_ring_common(ring);
947}
948
949static int blt_ring_begin(struct intel_ring_buffer *ring,
950 int num_dwords)
951{
952 if (ring->private) {
953 int ret = intel_ring_begin(ring, num_dwords+2);
954 if (ret)
955 return ret;
956
957 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
958 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
959
960 return 0;
961 } else
962 return intel_ring_begin(ring, 4);
963}
964
965static void blt_ring_flush(struct intel_ring_buffer *ring,
966 u32 invalidate_domains,
967 u32 flush_domains)
968{
969 if (blt_ring_begin(ring, 4) == 0) {
970 intel_ring_emit(ring, MI_FLUSH_DW);
971 intel_ring_emit(ring, 0);
972 intel_ring_emit(ring, 0);
973 intel_ring_emit(ring, 0);
974 intel_ring_advance(ring);
975 }
976}
977
978static int
979blt_ring_add_request(struct intel_ring_buffer *ring,
980 u32 *result)
981{
982 u32 seqno;
983 int ret;
984
985 ret = blt_ring_begin(ring, 4);
986 if (ret)
987 return ret;
988
989 seqno = i915_gem_get_seqno(ring->dev);
990
991 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
992 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
993 intel_ring_emit(ring, seqno);
994 intel_ring_emit(ring, MI_USER_INTERRUPT);
995 intel_ring_advance(ring);
996
997 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
998 *result = seqno;
999 return 0;
1000}
1001
1002static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1003{
1004 if (!ring->private)
1005 return;
1006
1007 i915_gem_object_unpin(ring->private);
1008 drm_gem_object_unreference(ring->private);
1009 ring->private = NULL;
1010}
1011
Chris Wilson549f7362010-10-19 11:19:32 +01001012static const struct intel_ring_buffer gen6_blt_ring = {
1013 .name = "blt ring",
1014 .id = RING_BLT,
1015 .mmio_base = BLT_RING_BASE,
1016 .size = 32 * PAGE_SIZE,
Zou Nan hai8d192152010-11-02 16:31:01 +08001017 .init = blt_ring_init,
Chris Wilson297b0c52010-10-22 17:02:41 +01001018 .write_tail = ring_write_tail,
Zou Nan hai8d192152010-11-02 16:31:01 +08001019 .flush = blt_ring_flush,
1020 .add_request = blt_ring_add_request,
Chris Wilson549f7362010-10-19 11:19:32 +01001021 .get_seqno = ring_status_page_get_seqno,
1022 .user_irq_get = blt_ring_get_user_irq,
1023 .user_irq_put = blt_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001024 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Zou Nan hai8d192152010-11-02 16:31:01 +08001025 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001026};
1027
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001028int intel_init_render_ring_buffer(struct drm_device *dev)
1029{
1030 drm_i915_private_t *dev_priv = dev->dev_private;
1031
1032 dev_priv->render_ring = render_ring;
1033
1034 if (!I915_NEED_GFX_HWS(dev)) {
1035 dev_priv->render_ring.status_page.page_addr
1036 = dev_priv->status_page_dmah->vaddr;
1037 memset(dev_priv->render_ring.status_page.page_addr,
1038 0, PAGE_SIZE);
1039 }
1040
1041 return intel_init_ring_buffer(dev, &dev_priv->render_ring);
1042}
1043
1044int intel_init_bsd_ring_buffer(struct drm_device *dev)
1045{
1046 drm_i915_private_t *dev_priv = dev->dev_private;
1047
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001048 if (IS_GEN6(dev))
1049 dev_priv->bsd_ring = gen6_bsd_ring;
1050 else
1051 dev_priv->bsd_ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001052
1053 return intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
1054}
Chris Wilson549f7362010-10-19 11:19:32 +01001055
1056int intel_init_blt_ring_buffer(struct drm_device *dev)
1057{
1058 drm_i915_private_t *dev_priv = dev->dev_private;
1059
1060 dev_priv->blt_ring = gen6_blt_ring;
1061
1062 return intel_init_ring_buffer(dev, &dev_priv->blt_ring);
1063}