blob: b12578558268702a94bc445f8747d25e5b3f9bd9 [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 Wilson6f392d52010-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 Wilson6f392d52010-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 Wilson6f392d52010-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;
Chris Wilson05394f32010-11-08 19:18:58 +0000142 struct drm_i915_gem_object *obj = ring->obj;
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. */
Chris Wilson05394f32010-11-08 19:18:58 +0000151 I915_WRITE_START(ring, obj->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 ||
Chris Wilson05394f32010-11-08 19:18:58 +0000181 I915_READ_START(ring) != obj->gtt_offset ||
Chris Wilson176f28e2010-10-28 11:18:07 +0100182 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000183 DRM_ERROR("%s initialization failed "
184 "ctl %08x head %08x tail %08x start %08x\n",
185 ring->name,
186 I915_READ_CTL(ring),
187 I915_READ_HEAD(ring),
188 I915_READ_TAIL(ring),
189 I915_READ_START(ring));
190 return -EIO;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800191 }
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 Wilsonb6913e42010-11-12 10:46:37 +0000205/*
206 * 965+ support PIPE_CONTROL commands, which provide finer grained control
207 * over cache flushing.
208 */
209struct pipe_control {
210 struct drm_i915_gem_object *obj;
211 volatile u32 *cpu_page;
212 u32 gtt_offset;
213};
214
215static int
216init_pipe_control(struct intel_ring_buffer *ring)
217{
218 struct pipe_control *pc;
219 struct drm_i915_gem_object *obj;
220 int ret;
221
222 if (ring->private)
223 return 0;
224
225 pc = kmalloc(sizeof(*pc), GFP_KERNEL);
226 if (!pc)
227 return -ENOMEM;
228
229 obj = i915_gem_alloc_object(ring->dev, 4096);
230 if (obj == NULL) {
231 DRM_ERROR("Failed to allocate seqno page\n");
232 ret = -ENOMEM;
233 goto err;
234 }
235 obj->agp_type = AGP_USER_CACHED_MEMORY;
236
237 ret = i915_gem_object_pin(obj, 4096, true);
238 if (ret)
239 goto err_unref;
240
241 pc->gtt_offset = obj->gtt_offset;
242 pc->cpu_page = kmap(obj->pages[0]);
243 if (pc->cpu_page == NULL)
244 goto err_unpin;
245
246 pc->obj = obj;
247 ring->private = pc;
248 return 0;
249
250err_unpin:
251 i915_gem_object_unpin(obj);
252err_unref:
253 drm_gem_object_unreference(&obj->base);
254err:
255 kfree(pc);
256 return ret;
257}
258
259static void
260cleanup_pipe_control(struct intel_ring_buffer *ring)
261{
262 struct pipe_control *pc = ring->private;
263 struct drm_i915_gem_object *obj;
264
265 if (!ring->private)
266 return;
267
268 obj = pc->obj;
269 kunmap(obj->pages[0]);
270 i915_gem_object_unpin(obj);
271 drm_gem_object_unreference(&obj->base);
272
273 kfree(pc);
274 ring->private = NULL;
275}
276
Chris Wilson78501ea2010-10-27 12:18:21 +0100277static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800278{
Chris Wilson78501ea2010-10-27 12:18:21 +0100279 struct drm_device *dev = ring->dev;
280 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800281
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100282 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100283 drm_i915_private_t *dev_priv = dev->dev_private;
284 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800285 if (IS_GEN6(dev))
286 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
287 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800288 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100289
Chris Wilsonb6913e42010-11-12 10:46:37 +0000290 if (HAS_PIPE_CONTROL(dev)) {
291 ret = init_pipe_control(ring);
292 if (ret)
293 return ret;
294 }
295
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800296 return ret;
297}
298
Chris Wilsonb6913e42010-11-12 10:46:37 +0000299static void render_ring_cleanup(struct intel_ring_buffer *ring)
300{
301 if (!ring->private)
302 return;
303
304 cleanup_pipe_control(ring);
305}
306
Chris Wilson78501ea2010-10-27 12:18:21 +0100307#define PIPE_CONTROL_FLUSH(ring__, addr__) \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800308do { \
Chris Wilson78501ea2010-10-27 12:18:21 +0100309 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
Zhenyu Wangca764822010-05-27 10:26:42 +0800310 PIPE_CONTROL_DEPTH_STALL | 2); \
Chris Wilson78501ea2010-10-27 12:18:21 +0100311 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
312 intel_ring_emit(ring__, 0); \
313 intel_ring_emit(ring__, 0); \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800314} while (0)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700315
316/**
317 * Creates a new sequence number, emitting a write of it to the status page
318 * plus an interrupt, which will trigger i915_user_interrupt_handler.
319 *
320 * Must be called with struct_lock held.
321 *
322 * Returned sequence numbers are nonzero on success.
323 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100324static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100325render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100326 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700327{
Chris Wilson78501ea2010-10-27 12:18:21 +0100328 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100329 u32 seqno = i915_gem_get_seqno(dev);
Chris Wilsonb6913e42010-11-12 10:46:37 +0000330 struct pipe_control *pc = ring->private;
Chris Wilson3cce4692010-10-27 16:11:02 +0100331 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800332
333 if (IS_GEN6(dev)) {
Chris Wilson3cce4692010-10-27 16:11:02 +0100334 ret = intel_ring_begin(ring, 6);
335 if (ret)
336 return ret;
337
338 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | 3);
339 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE |
340 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_IS_FLUSH |
341 PIPE_CONTROL_NOTIFY);
Chris Wilsonb6913e42010-11-12 10:46:37 +0000342 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100343 intel_ring_emit(ring, seqno);
344 intel_ring_emit(ring, 0);
345 intel_ring_emit(ring, 0);
Zhenyu Wangca764822010-05-27 10:26:42 +0800346 } else if (HAS_PIPE_CONTROL(dev)) {
Chris Wilsonb6913e42010-11-12 10:46:37 +0000347 u32 scratch_addr = pc->gtt_offset + 128;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700348
349 /*
350 * Workaround qword write incoherence by flushing the
351 * PIPE_NOTIFY buffers out to memory before requesting
352 * an interrupt.
353 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100354 ret = intel_ring_begin(ring, 32);
355 if (ret)
356 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700357
Chris Wilson3cce4692010-10-27 16:11:02 +0100358 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
359 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
Chris Wilsonb6913e42010-11-12 10:46:37 +0000360 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100361 intel_ring_emit(ring, seqno);
362 intel_ring_emit(ring, 0);
363 PIPE_CONTROL_FLUSH(ring, scratch_addr);
364 scratch_addr += 128; /* write to separate cachelines */
365 PIPE_CONTROL_FLUSH(ring, scratch_addr);
366 scratch_addr += 128;
367 PIPE_CONTROL_FLUSH(ring, scratch_addr);
368 scratch_addr += 128;
369 PIPE_CONTROL_FLUSH(ring, scratch_addr);
370 scratch_addr += 128;
371 PIPE_CONTROL_FLUSH(ring, scratch_addr);
372 scratch_addr += 128;
373 PIPE_CONTROL_FLUSH(ring, scratch_addr);
374 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
375 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
376 PIPE_CONTROL_NOTIFY);
Chris Wilsonb6913e42010-11-12 10:46:37 +0000377 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100378 intel_ring_emit(ring, seqno);
379 intel_ring_emit(ring, 0);
380 } else {
381 ret = intel_ring_begin(ring, 4);
382 if (ret)
383 return ret;
384
385 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
386 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
387 intel_ring_emit(ring, seqno);
388
389 intel_ring_emit(ring, MI_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700390 }
Chris Wilson3cce4692010-10-27 16:11:02 +0100391
392 intel_ring_advance(ring);
393 *result = seqno;
394 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700395}
396
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800397static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100398render_ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800399{
Chris Wilson78501ea2010-10-27 12:18:21 +0100400 struct drm_device *dev = ring->dev;
Chris Wilsonb6913e42010-11-12 10:46:37 +0000401 if (HAS_PIPE_CONTROL(dev)) {
402 struct pipe_control *pc = ring->private;
403 return pc->cpu_page[0];
404 } else
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800405 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
406}
407
408static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100409render_ring_get_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700410{
Chris Wilson78501ea2010-10-27 12:18:21 +0100411 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700412 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
413 unsigned long irqflags;
414
415 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800416 if (dev->irq_enabled && (++ring->user_irq_refcount == 1)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700417 if (HAS_PCH_SPLIT(dev))
418 ironlake_enable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
419 else
420 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
421 }
422 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
423}
424
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800425static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100426render_ring_put_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700427{
Chris Wilson78501ea2010-10-27 12:18:21 +0100428 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700429 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
430 unsigned long irqflags;
431
432 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800433 BUG_ON(dev->irq_enabled && ring->user_irq_refcount <= 0);
434 if (dev->irq_enabled && (--ring->user_irq_refcount == 0)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700435 if (HAS_PCH_SPLIT(dev))
436 ironlake_disable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
437 else
438 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
439 }
440 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
441}
442
Chris Wilson78501ea2010-10-27 12:18:21 +0100443void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800444{
Chris Wilson78501ea2010-10-27 12:18:21 +0100445 drm_i915_private_t *dev_priv = ring->dev->dev_private;
446 u32 mmio = IS_GEN6(ring->dev) ?
447 RING_HWS_PGA_GEN6(ring->mmio_base) :
448 RING_HWS_PGA(ring->mmio_base);
449 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
450 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800451}
452
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100453static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100454bsd_ring_flush(struct intel_ring_buffer *ring,
455 u32 invalidate_domains,
456 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800457{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100458 if (intel_ring_begin(ring, 2) == 0) {
459 intel_ring_emit(ring, MI_FLUSH);
460 intel_ring_emit(ring, MI_NOOP);
461 intel_ring_advance(ring);
462 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800463}
464
Chris Wilson3cce4692010-10-27 16:11:02 +0100465static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100466ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100467 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800468{
469 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100470 int ret;
471
472 ret = intel_ring_begin(ring, 4);
473 if (ret)
474 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100475
Chris Wilson78501ea2010-10-27 12:18:21 +0100476 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d52010-08-07 11:01:22 +0100477
Chris Wilson3cce4692010-10-27 16:11:02 +0100478 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
479 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
480 intel_ring_emit(ring, seqno);
481 intel_ring_emit(ring, MI_USER_INTERRUPT);
482 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800483
484 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +0100485 *result = seqno;
486 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800487}
488
Zou Nan haid1b851f2010-05-21 09:08:57 +0800489static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100490bsd_ring_get_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800491{
492 /* do nothing */
493}
494static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100495bsd_ring_put_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800496{
497 /* do nothing */
498}
499
500static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100501ring_status_page_get_seqno(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800502{
503 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
504}
505
506static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100507ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
508 struct drm_i915_gem_execbuffer2 *exec,
509 struct drm_clip_rect *cliprects,
510 uint64_t exec_offset)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800511{
512 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100513 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100514
Zou Nan haid1b851f2010-05-21 09:08:57 +0800515 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilson78501ea2010-10-27 12:18:21 +0100516
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100517 ret = intel_ring_begin(ring, 2);
518 if (ret)
519 return ret;
520
Chris Wilson78501ea2010-10-27 12:18:21 +0100521 intel_ring_emit(ring,
522 MI_BATCH_BUFFER_START |
523 (2 << 6) |
524 MI_BATCH_NON_SECURE_I965);
525 intel_ring_emit(ring, exec_start);
526 intel_ring_advance(ring);
527
Zou Nan haid1b851f2010-05-21 09:08:57 +0800528 return 0;
529}
530
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800531static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100532render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
533 struct drm_i915_gem_execbuffer2 *exec,
534 struct drm_clip_rect *cliprects,
535 uint64_t exec_offset)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700536{
Chris Wilson78501ea2010-10-27 12:18:21 +0100537 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700538 drm_i915_private_t *dev_priv = dev->dev_private;
539 int nbox = exec->num_cliprects;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700540 uint32_t exec_start, exec_len;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100541 int i, count, ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100542
Eric Anholt62fdfea2010-05-21 13:26:39 -0700543 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
544 exec_len = (uint32_t) exec->batch_len;
545
Chris Wilson6f392d52010-08-07 11:01:22 +0100546 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700547
548 count = nbox ? nbox : 1;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700549 for (i = 0; i < count; i++) {
550 if (i < nbox) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100551 ret = i915_emit_box(dev, cliprects, i,
552 exec->DR1, exec->DR4);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700553 if (ret)
554 return ret;
555 }
556
557 if (IS_I830(dev) || IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100558 ret = intel_ring_begin(ring, 4);
559 if (ret)
560 return ret;
561
Chris Wilson78501ea2010-10-27 12:18:21 +0100562 intel_ring_emit(ring, MI_BATCH_BUFFER);
563 intel_ring_emit(ring, exec_start | MI_BATCH_NON_SECURE);
564 intel_ring_emit(ring, exec_start + exec_len - 4);
565 intel_ring_emit(ring, 0);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700566 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100567 ret = intel_ring_begin(ring, 2);
568 if (ret)
569 return ret;
570
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100571 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100572 intel_ring_emit(ring,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800573 MI_BATCH_BUFFER_START | (2 << 6)
574 | MI_BATCH_NON_SECURE_I965);
Chris Wilson78501ea2010-10-27 12:18:21 +0100575 intel_ring_emit(ring, exec_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700576 } else {
Chris Wilson78501ea2010-10-27 12:18:21 +0100577 intel_ring_emit(ring, MI_BATCH_BUFFER_START
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800578 | (2 << 6));
Chris Wilson78501ea2010-10-27 12:18:21 +0100579 intel_ring_emit(ring, exec_start |
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800580 MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700581 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700582 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100583 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700584 }
585
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100586 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100587 if (intel_ring_begin(ring, 2) == 0) {
588 intel_ring_emit(ring, MI_FLUSH |
589 MI_NO_WRITE_FLUSH |
590 MI_INVALIDATE_ISP );
591 intel_ring_emit(ring, MI_NOOP);
592 intel_ring_advance(ring);
593 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800594 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700595 /* XXX breadcrumb */
Zou Nan hai1cafd342010-06-25 13:40:24 +0800596
Eric Anholt62fdfea2010-05-21 13:26:39 -0700597 return 0;
598}
599
Chris Wilson78501ea2010-10-27 12:18:21 +0100600static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700601{
Chris Wilson78501ea2010-10-27 12:18:21 +0100602 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000603 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700604
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800605 obj = ring->status_page.obj;
606 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700607 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700608
Chris Wilson05394f32010-11-08 19:18:58 +0000609 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700610 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000611 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800612 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700613
614 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700615}
616
Chris Wilson78501ea2010-10-27 12:18:21 +0100617static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700618{
Chris Wilson78501ea2010-10-27 12:18:21 +0100619 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700620 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000621 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700622 int ret;
623
Eric Anholt62fdfea2010-05-21 13:26:39 -0700624 obj = i915_gem_alloc_object(dev, 4096);
625 if (obj == NULL) {
626 DRM_ERROR("Failed to allocate status page\n");
627 ret = -ENOMEM;
628 goto err;
629 }
Chris Wilson05394f32010-11-08 19:18:58 +0000630 obj->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700631
Daniel Vetter75e9e912010-11-04 17:11:09 +0100632 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700633 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700634 goto err_unref;
635 }
636
Chris Wilson05394f32010-11-08 19:18:58 +0000637 ring->status_page.gfx_addr = obj->gtt_offset;
638 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800639 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700640 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700641 goto err_unpin;
642 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800643 ring->status_page.obj = obj;
644 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700645
Chris Wilson78501ea2010-10-27 12:18:21 +0100646 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800647 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
648 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700649
650 return 0;
651
652err_unpin:
653 i915_gem_object_unpin(obj);
654err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000655 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700656err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800657 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700658}
659
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800660int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100661 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700662{
Chris Wilson05394f32010-11-08 19:18:58 +0000663 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100664 int ret;
665
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800666 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100667 INIT_LIST_HEAD(&ring->active_list);
668 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100669 INIT_LIST_HEAD(&ring->gpu_write_list);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700670
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800671 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100672 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800673 if (ret)
674 return ret;
675 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700676
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800677 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700678 if (obj == NULL) {
679 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800680 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100681 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700682 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700683
Chris Wilson05394f32010-11-08 19:18:58 +0000684 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800685
Daniel Vetter75e9e912010-11-04 17:11:09 +0100686 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100687 if (ret)
688 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700689
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800690 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000691 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700692 ring->map.type = 0;
693 ring->map.flags = 0;
694 ring->map.mtrr = 0;
695
696 drm_core_ioremap_wc(&ring->map, dev);
697 if (ring->map.handle == NULL) {
698 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800699 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100700 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700701 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800702
Eric Anholt62fdfea2010-05-21 13:26:39 -0700703 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100704 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100705 if (ret)
706 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700707
Chris Wilsonc584fe42010-10-29 18:15:52 +0100708 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100709
710err_unmap:
711 drm_core_ioremapfree(&ring->map, dev);
712err_unpin:
713 i915_gem_object_unpin(obj);
714err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000715 drm_gem_object_unreference(&obj->base);
716 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100717err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100718 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800719 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700720}
721
Chris Wilson78501ea2010-10-27 12:18:21 +0100722void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700723{
Chris Wilson33626e62010-10-29 16:18:36 +0100724 struct drm_i915_private *dev_priv;
725 int ret;
726
Chris Wilson05394f32010-11-08 19:18:58 +0000727 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700728 return;
729
Chris Wilson33626e62010-10-29 16:18:36 +0100730 /* Disable the ring buffer. The ring must be idle at this point */
731 dev_priv = ring->dev->dev_private;
732 ret = intel_wait_ring_buffer(ring, ring->size - 8);
733 I915_WRITE_CTL(ring, 0);
734
Chris Wilson78501ea2010-10-27 12:18:21 +0100735 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700736
Chris Wilson05394f32010-11-08 19:18:58 +0000737 i915_gem_object_unpin(ring->obj);
738 drm_gem_object_unreference(&ring->obj->base);
739 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100740
Zou Nan hai8d192152010-11-02 16:31:01 +0800741 if (ring->cleanup)
742 ring->cleanup(ring);
743
Chris Wilson78501ea2010-10-27 12:18:21 +0100744 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700745}
746
Chris Wilson78501ea2010-10-27 12:18:21 +0100747static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700748{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800749 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700750 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800751 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700752
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800753 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100754 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700755 if (ret)
756 return ret;
757 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700758
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800759 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100760 rem /= 8;
761 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700762 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100763 *virt++ = MI_NOOP;
764 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700765
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800766 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100767 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700768
769 return 0;
770}
771
Chris Wilson78501ea2010-10-27 12:18:21 +0100772int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700773{
Chris Wilson78501ea2010-10-27 12:18:21 +0100774 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800775 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100776 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100777 u32 head;
778
779 head = intel_read_status_page(ring, 4);
780 if (head) {
781 ring->head = head & HEAD_ADDR;
782 ring->space = ring->head - (ring->tail + 8);
783 if (ring->space < 0)
784 ring->space += ring->size;
785 if (ring->space >= n)
786 return 0;
787 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700788
789 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800790 end = jiffies + 3 * HZ;
791 do {
Daniel Vetter570ef602010-08-02 17:06:23 +0200792 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700793 ring->space = ring->head - (ring->tail + 8);
794 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800795 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700796 if (ring->space >= n) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100797 trace_i915_ring_wait_end(dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700798 return 0;
799 }
800
801 if (dev->primary->master) {
802 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
803 if (master_priv->sarea_priv)
804 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
805 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800806
Chris Wilsone60a0b12010-10-13 10:09:14 +0100807 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +0100808 if (atomic_read(&dev_priv->mm.wedged))
809 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800810 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700811 trace_i915_ring_wait_end (dev);
812 return -EBUSY;
813}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800814
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100815int intel_ring_begin(struct intel_ring_buffer *ring,
816 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800817{
Zou Nan haibe26a102010-06-12 17:40:24 +0800818 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100819 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100820
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100821 if (unlikely(ring->tail + n > ring->size)) {
822 ret = intel_wrap_ring_buffer(ring);
823 if (unlikely(ret))
824 return ret;
825 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100826
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100827 if (unlikely(ring->space < n)) {
828 ret = intel_wait_ring_buffer(ring, n);
829 if (unlikely(ret))
830 return ret;
831 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100832
833 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100834 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800835}
836
Chris Wilson78501ea2010-10-27 12:18:21 +0100837void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800838{
Chris Wilsond97ed332010-08-04 15:18:13 +0100839 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100840 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800841}
842
Chris Wilsone0708682010-09-19 14:46:27 +0100843static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800844 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100845 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200846 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800847 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800848 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100849 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800850 .flush = render_ring_flush,
851 .add_request = render_ring_add_request,
Chris Wilsonf787a5f2010-09-24 16:02:42 +0100852 .get_seqno = render_ring_get_seqno,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800853 .user_irq_get = render_ring_get_user_irq,
854 .user_irq_put = render_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100855 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Chris Wilsonb6913e42010-11-12 10:46:37 +0000856 .cleanup = render_ring_cleanup,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800857};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800858
859/* ring buffer for bit-stream decoder */
860
Chris Wilsone0708682010-09-19 14:46:27 +0100861static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +0800862 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +0100863 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200864 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800865 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100866 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100867 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800868 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +0100869 .add_request = ring_add_request,
870 .get_seqno = ring_status_page_get_seqno,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800871 .user_irq_get = bsd_ring_get_user_irq,
872 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100873 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800874};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800875
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100876
Chris Wilson78501ea2010-10-27 12:18:21 +0100877static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100878 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100879{
Chris Wilson78501ea2010-10-27 12:18:21 +0100880 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100881
882 /* Every tail move must follow the sequence below */
883 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
884 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
885 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
886 I915_WRITE(GEN6_BSD_RNCID, 0x0);
887
888 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
889 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
890 50))
891 DRM_ERROR("timed out waiting for IDLE Indicator\n");
892
Daniel Vetter870e86d2010-08-02 16:29:44 +0200893 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100894 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
895 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
896 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
897}
898
Chris Wilson78501ea2010-10-27 12:18:21 +0100899static void gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson549f7362010-10-19 11:19:32 +0100900 u32 invalidate_domains,
901 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100902{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100903 if (intel_ring_begin(ring, 4) == 0) {
904 intel_ring_emit(ring, MI_FLUSH_DW);
905 intel_ring_emit(ring, 0);
906 intel_ring_emit(ring, 0);
907 intel_ring_emit(ring, 0);
908 intel_ring_advance(ring);
909 }
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100910}
911
912static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100913gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
914 struct drm_i915_gem_execbuffer2 *exec,
915 struct drm_clip_rect *cliprects,
916 uint64_t exec_offset)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100917{
918 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100919 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100920
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100921 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100922
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100923 ret = intel_ring_begin(ring, 2);
924 if (ret)
925 return ret;
926
Chris Wilson78501ea2010-10-27 12:18:21 +0100927 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100928 /* bit0-7 is the length on GEN6+ */
Chris Wilson78501ea2010-10-27 12:18:21 +0100929 intel_ring_emit(ring, exec_start);
930 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100931
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100932 return 0;
933}
934
935/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +0100936static const struct intel_ring_buffer gen6_bsd_ring = {
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100937 .name = "gen6 bsd ring",
938 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200939 .mmio_base = GEN6_BSD_RING_BASE,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100940 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100941 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100942 .write_tail = gen6_bsd_ring_write_tail,
Chris Wilson549f7362010-10-19 11:19:32 +0100943 .flush = gen6_ring_flush,
944 .add_request = ring_add_request,
945 .get_seqno = ring_status_page_get_seqno,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100946 .user_irq_get = bsd_ring_get_user_irq,
947 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100948 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +0100949};
950
951/* Blitter support (SandyBridge+) */
952
953static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100954blt_ring_get_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100955{
956 /* do nothing */
957}
958static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100959blt_ring_put_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100960{
961 /* do nothing */
962}
963
Zou Nan hai8d192152010-11-02 16:31:01 +0800964
965/* Workaround for some stepping of SNB,
966 * each time when BLT engine ring tail moved,
967 * the first command in the ring to be parsed
968 * should be MI_BATCH_BUFFER_START
969 */
970#define NEED_BLT_WORKAROUND(dev) \
971 (IS_GEN6(dev) && (dev->pdev->revision < 8))
972
973static inline struct drm_i915_gem_object *
974to_blt_workaround(struct intel_ring_buffer *ring)
975{
976 return ring->private;
977}
978
979static int blt_ring_init(struct intel_ring_buffer *ring)
980{
981 if (NEED_BLT_WORKAROUND(ring->dev)) {
982 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +0000983 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +0800984 int ret;
985
Chris Wilson05394f32010-11-08 19:18:58 +0000986 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +0800987 if (obj == NULL)
988 return -ENOMEM;
989
Chris Wilson05394f32010-11-08 19:18:58 +0000990 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +0800991 if (ret) {
992 drm_gem_object_unreference(&obj->base);
993 return ret;
994 }
995
996 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +0000997 *ptr++ = MI_BATCH_BUFFER_END;
998 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +0800999 kunmap(obj->pages[0]);
1000
Chris Wilson05394f32010-11-08 19:18:58 +00001001 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +08001002 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00001003 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +08001004 drm_gem_object_unreference(&obj->base);
1005 return ret;
1006 }
1007
1008 ring->private = obj;
1009 }
1010
1011 return init_ring_common(ring);
1012}
1013
1014static int blt_ring_begin(struct intel_ring_buffer *ring,
1015 int num_dwords)
1016{
1017 if (ring->private) {
1018 int ret = intel_ring_begin(ring, num_dwords+2);
1019 if (ret)
1020 return ret;
1021
1022 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
1023 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
1024
1025 return 0;
1026 } else
1027 return intel_ring_begin(ring, 4);
1028}
1029
1030static void blt_ring_flush(struct intel_ring_buffer *ring,
1031 u32 invalidate_domains,
1032 u32 flush_domains)
1033{
1034 if (blt_ring_begin(ring, 4) == 0) {
1035 intel_ring_emit(ring, MI_FLUSH_DW);
1036 intel_ring_emit(ring, 0);
1037 intel_ring_emit(ring, 0);
1038 intel_ring_emit(ring, 0);
1039 intel_ring_advance(ring);
1040 }
1041}
1042
1043static int
1044blt_ring_add_request(struct intel_ring_buffer *ring,
1045 u32 *result)
1046{
1047 u32 seqno;
1048 int ret;
1049
1050 ret = blt_ring_begin(ring, 4);
1051 if (ret)
1052 return ret;
1053
1054 seqno = i915_gem_get_seqno(ring->dev);
1055
1056 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1057 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1058 intel_ring_emit(ring, seqno);
1059 intel_ring_emit(ring, MI_USER_INTERRUPT);
1060 intel_ring_advance(ring);
1061
1062 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
1063 *result = seqno;
1064 return 0;
1065}
1066
1067static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1068{
1069 if (!ring->private)
1070 return;
1071
1072 i915_gem_object_unpin(ring->private);
1073 drm_gem_object_unreference(ring->private);
1074 ring->private = NULL;
1075}
1076
Chris Wilson549f7362010-10-19 11:19:32 +01001077static const struct intel_ring_buffer gen6_blt_ring = {
1078 .name = "blt ring",
1079 .id = RING_BLT,
1080 .mmio_base = BLT_RING_BASE,
1081 .size = 32 * PAGE_SIZE,
Zou Nan hai8d192152010-11-02 16:31:01 +08001082 .init = blt_ring_init,
Chris Wilson297b0c52010-10-22 17:02:41 +01001083 .write_tail = ring_write_tail,
Zou Nan hai8d192152010-11-02 16:31:01 +08001084 .flush = blt_ring_flush,
1085 .add_request = blt_ring_add_request,
Chris Wilson549f7362010-10-19 11:19:32 +01001086 .get_seqno = ring_status_page_get_seqno,
1087 .user_irq_get = blt_ring_get_user_irq,
1088 .user_irq_put = blt_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001089 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Zou Nan hai8d192152010-11-02 16:31:01 +08001090 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001091};
1092
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001093int intel_init_render_ring_buffer(struct drm_device *dev)
1094{
1095 drm_i915_private_t *dev_priv = dev->dev_private;
1096
1097 dev_priv->render_ring = render_ring;
1098
1099 if (!I915_NEED_GFX_HWS(dev)) {
1100 dev_priv->render_ring.status_page.page_addr
1101 = dev_priv->status_page_dmah->vaddr;
1102 memset(dev_priv->render_ring.status_page.page_addr,
1103 0, PAGE_SIZE);
1104 }
1105
1106 return intel_init_ring_buffer(dev, &dev_priv->render_ring);
1107}
1108
1109int intel_init_bsd_ring_buffer(struct drm_device *dev)
1110{
1111 drm_i915_private_t *dev_priv = dev->dev_private;
1112
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001113 if (IS_GEN6(dev))
1114 dev_priv->bsd_ring = gen6_bsd_ring;
1115 else
1116 dev_priv->bsd_ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001117
1118 return intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
1119}
Chris Wilson549f7362010-10-19 11:19:32 +01001120
1121int intel_init_blt_ring_buffer(struct drm_device *dev)
1122{
1123 drm_i915_private_t *dev_priv = dev->dev_private;
1124
1125 dev_priv->blt_ring = gen6_blt_ring;
1126
1127 return intel_init_ring_buffer(dev, &dev_priv->blt_ring);
1128}