blob: 21871b0766e28d4247ae8f6956a3461b57660aae [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
Chris Wilson70eac332010-11-30 14:07:47 +0000112 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
113 (IS_G4X(dev) || IS_GEN5(dev)))
114 cmd |= MI_INVALIDATE_ISP;
115
Eric Anholt62fdfea2010-05-21 13:26:39 -0700116#if WATCH_EXEC
117 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
118#endif
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100119 if (intel_ring_begin(ring, 2) == 0) {
120 intel_ring_emit(ring, cmd);
121 intel_ring_emit(ring, MI_NOOP);
122 intel_ring_advance(ring);
123 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800124 }
125}
126
Chris Wilson78501ea2010-10-27 12:18:21 +0100127static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100128 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800129{
Chris Wilson78501ea2010-10-27 12:18:21 +0100130 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100131 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800132}
133
Chris Wilson78501ea2010-10-27 12:18:21 +0100134u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800135{
Chris Wilson78501ea2010-10-27 12:18:21 +0100136 drm_i915_private_t *dev_priv = ring->dev->dev_private;
137 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200138 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800139
140 return I915_READ(acthd_reg);
141}
142
Chris Wilson78501ea2010-10-27 12:18:21 +0100143static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800144{
Chris Wilson78501ea2010-10-27 12:18:21 +0100145 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000146 struct drm_i915_gem_object *obj = ring->obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800147 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800148
149 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200150 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200151 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100152 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800153
154 /* Initialize the ring. */
Chris Wilson05394f32010-11-08 19:18:58 +0000155 I915_WRITE_START(ring, obj->gtt_offset);
Daniel Vetter570ef602010-08-02 17:06:23 +0200156 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800157
158 /* G45 ring initialization fails to reset head to zero */
159 if (head != 0) {
160 DRM_ERROR("%s head not reset to zero "
161 "ctl %08x head %08x tail %08x start %08x\n",
162 ring->name,
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200163 I915_READ_CTL(ring),
Daniel Vetter570ef602010-08-02 17:06:23 +0200164 I915_READ_HEAD(ring),
Daniel Vetter870e86d2010-08-02 16:29:44 +0200165 I915_READ_TAIL(ring),
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200166 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800167
Daniel Vetter570ef602010-08-02 17:06:23 +0200168 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800169
170 DRM_ERROR("%s head forced to zero "
171 "ctl %08x head %08x tail %08x start %08x\n",
172 ring->name,
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200173 I915_READ_CTL(ring),
Daniel Vetter570ef602010-08-02 17:06:23 +0200174 I915_READ_HEAD(ring),
Daniel Vetter870e86d2010-08-02 16:29:44 +0200175 I915_READ_TAIL(ring),
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200176 I915_READ_START(ring));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700177 }
178
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200179 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000180 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson6aa56062010-10-29 21:44:37 +0100181 | RING_REPORT_64K | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800182
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800183 /* If the head is still not zero, the ring is dead */
Chris Wilson176f28e2010-10-28 11:18:07 +0100184 if ((I915_READ_CTL(ring) & RING_VALID) == 0 ||
Chris Wilson05394f32010-11-08 19:18:58 +0000185 I915_READ_START(ring) != obj->gtt_offset ||
Chris Wilson176f28e2010-10-28 11:18:07 +0100186 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000187 DRM_ERROR("%s initialization failed "
188 "ctl %08x head %08x tail %08x start %08x\n",
189 ring->name,
190 I915_READ_CTL(ring),
191 I915_READ_HEAD(ring),
192 I915_READ_TAIL(ring),
193 I915_READ_START(ring));
194 return -EIO;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800195 }
196
Chris Wilson78501ea2010-10-27 12:18:21 +0100197 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
198 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800199 else {
Daniel Vetter570ef602010-08-02 17:06:23 +0200200 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Daniel Vetter870e86d2010-08-02 16:29:44 +0200201 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800202 ring->space = ring->head - (ring->tail + 8);
203 if (ring->space < 0)
204 ring->space += ring->size;
205 }
206 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700207}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800208
Chris Wilsonb6913e42010-11-12 10:46:37 +0000209/*
210 * 965+ support PIPE_CONTROL commands, which provide finer grained control
211 * over cache flushing.
212 */
213struct pipe_control {
214 struct drm_i915_gem_object *obj;
215 volatile u32 *cpu_page;
216 u32 gtt_offset;
217};
218
219static int
220init_pipe_control(struct intel_ring_buffer *ring)
221{
222 struct pipe_control *pc;
223 struct drm_i915_gem_object *obj;
224 int ret;
225
226 if (ring->private)
227 return 0;
228
229 pc = kmalloc(sizeof(*pc), GFP_KERNEL);
230 if (!pc)
231 return -ENOMEM;
232
233 obj = i915_gem_alloc_object(ring->dev, 4096);
234 if (obj == NULL) {
235 DRM_ERROR("Failed to allocate seqno page\n");
236 ret = -ENOMEM;
237 goto err;
238 }
239 obj->agp_type = AGP_USER_CACHED_MEMORY;
240
241 ret = i915_gem_object_pin(obj, 4096, true);
242 if (ret)
243 goto err_unref;
244
245 pc->gtt_offset = obj->gtt_offset;
246 pc->cpu_page = kmap(obj->pages[0]);
247 if (pc->cpu_page == NULL)
248 goto err_unpin;
249
250 pc->obj = obj;
251 ring->private = pc;
252 return 0;
253
254err_unpin:
255 i915_gem_object_unpin(obj);
256err_unref:
257 drm_gem_object_unreference(&obj->base);
258err:
259 kfree(pc);
260 return ret;
261}
262
263static void
264cleanup_pipe_control(struct intel_ring_buffer *ring)
265{
266 struct pipe_control *pc = ring->private;
267 struct drm_i915_gem_object *obj;
268
269 if (!ring->private)
270 return;
271
272 obj = pc->obj;
273 kunmap(obj->pages[0]);
274 i915_gem_object_unpin(obj);
275 drm_gem_object_unreference(&obj->base);
276
277 kfree(pc);
278 ring->private = NULL;
279}
280
Chris Wilson78501ea2010-10-27 12:18:21 +0100281static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800282{
Chris Wilson78501ea2010-10-27 12:18:21 +0100283 struct drm_device *dev = ring->dev;
284 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800285
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100286 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100287 drm_i915_private_t *dev_priv = dev->dev_private;
288 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800289 if (IS_GEN6(dev))
290 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
291 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800292 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100293
Chris Wilsonb6913e42010-11-12 10:46:37 +0000294 if (HAS_PIPE_CONTROL(dev)) {
295 ret = init_pipe_control(ring);
296 if (ret)
297 return ret;
298 }
299
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800300 return ret;
301}
302
Chris Wilsonb6913e42010-11-12 10:46:37 +0000303static void render_ring_cleanup(struct intel_ring_buffer *ring)
304{
305 if (!ring->private)
306 return;
307
308 cleanup_pipe_control(ring);
309}
310
Chris Wilson78501ea2010-10-27 12:18:21 +0100311#define PIPE_CONTROL_FLUSH(ring__, addr__) \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800312do { \
Chris Wilson78501ea2010-10-27 12:18:21 +0100313 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
Zhenyu Wangca764822010-05-27 10:26:42 +0800314 PIPE_CONTROL_DEPTH_STALL | 2); \
Chris Wilson78501ea2010-10-27 12:18:21 +0100315 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
316 intel_ring_emit(ring__, 0); \
317 intel_ring_emit(ring__, 0); \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800318} while (0)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700319
320/**
321 * Creates a new sequence number, emitting a write of it to the status page
322 * plus an interrupt, which will trigger i915_user_interrupt_handler.
323 *
324 * Must be called with struct_lock held.
325 *
326 * Returned sequence numbers are nonzero on success.
327 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100328static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100329render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100330 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700331{
Chris Wilson78501ea2010-10-27 12:18:21 +0100332 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100333 u32 seqno = i915_gem_get_seqno(dev);
Chris Wilsonb6913e42010-11-12 10:46:37 +0000334 struct pipe_control *pc = ring->private;
Chris Wilson3cce4692010-10-27 16:11:02 +0100335 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800336
337 if (IS_GEN6(dev)) {
Chris Wilson3cce4692010-10-27 16:11:02 +0100338 ret = intel_ring_begin(ring, 6);
339 if (ret)
340 return ret;
341
342 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | 3);
343 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE |
344 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_IS_FLUSH |
345 PIPE_CONTROL_NOTIFY);
Chris Wilsonb6913e42010-11-12 10:46:37 +0000346 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100347 intel_ring_emit(ring, seqno);
348 intel_ring_emit(ring, 0);
349 intel_ring_emit(ring, 0);
Zhenyu Wangca764822010-05-27 10:26:42 +0800350 } else if (HAS_PIPE_CONTROL(dev)) {
Chris Wilsonb6913e42010-11-12 10:46:37 +0000351 u32 scratch_addr = pc->gtt_offset + 128;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700352
353 /*
354 * Workaround qword write incoherence by flushing the
355 * PIPE_NOTIFY buffers out to memory before requesting
356 * an interrupt.
357 */
Chris Wilson3cce4692010-10-27 16:11:02 +0100358 ret = intel_ring_begin(ring, 32);
359 if (ret)
360 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700361
Chris Wilson3cce4692010-10-27 16:11:02 +0100362 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
363 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
Chris Wilsonb6913e42010-11-12 10:46:37 +0000364 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100365 intel_ring_emit(ring, seqno);
366 intel_ring_emit(ring, 0);
367 PIPE_CONTROL_FLUSH(ring, scratch_addr);
368 scratch_addr += 128; /* write to separate cachelines */
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 scratch_addr += 128;
375 PIPE_CONTROL_FLUSH(ring, scratch_addr);
376 scratch_addr += 128;
377 PIPE_CONTROL_FLUSH(ring, scratch_addr);
378 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
379 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
380 PIPE_CONTROL_NOTIFY);
Chris Wilsonb6913e42010-11-12 10:46:37 +0000381 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100382 intel_ring_emit(ring, seqno);
383 intel_ring_emit(ring, 0);
384 } else {
385 ret = intel_ring_begin(ring, 4);
386 if (ret)
387 return ret;
388
389 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
390 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
391 intel_ring_emit(ring, seqno);
392
393 intel_ring_emit(ring, MI_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700394 }
Chris Wilson3cce4692010-10-27 16:11:02 +0100395
396 intel_ring_advance(ring);
397 *result = seqno;
398 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700399}
400
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800401static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100402render_ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800403{
Chris Wilson78501ea2010-10-27 12:18:21 +0100404 struct drm_device *dev = ring->dev;
Chris Wilsonb6913e42010-11-12 10:46:37 +0000405 if (HAS_PIPE_CONTROL(dev)) {
406 struct pipe_control *pc = ring->private;
407 return pc->cpu_page[0];
408 } else
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800409 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
410}
411
412static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100413render_ring_get_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700414{
Chris Wilson78501ea2010-10-27 12:18:21 +0100415 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700416 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
417 unsigned long irqflags;
418
419 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800420 if (dev->irq_enabled && (++ring->user_irq_refcount == 1)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700421 if (HAS_PCH_SPLIT(dev))
422 ironlake_enable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
423 else
424 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
425 }
426 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
427}
428
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800429static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100430render_ring_put_user_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700431{
Chris Wilson78501ea2010-10-27 12:18:21 +0100432 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700433 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
434 unsigned long irqflags;
435
436 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800437 BUG_ON(dev->irq_enabled && ring->user_irq_refcount <= 0);
438 if (dev->irq_enabled && (--ring->user_irq_refcount == 0)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700439 if (HAS_PCH_SPLIT(dev))
440 ironlake_disable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
441 else
442 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
443 }
444 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
445}
446
Chris Wilson78501ea2010-10-27 12:18:21 +0100447void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800448{
Chris Wilson78501ea2010-10-27 12:18:21 +0100449 drm_i915_private_t *dev_priv = ring->dev->dev_private;
450 u32 mmio = IS_GEN6(ring->dev) ?
451 RING_HWS_PGA_GEN6(ring->mmio_base) :
452 RING_HWS_PGA(ring->mmio_base);
453 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
454 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800455}
456
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100457static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100458bsd_ring_flush(struct intel_ring_buffer *ring,
459 u32 invalidate_domains,
460 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800461{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100462 if (intel_ring_begin(ring, 2) == 0) {
463 intel_ring_emit(ring, MI_FLUSH);
464 intel_ring_emit(ring, MI_NOOP);
465 intel_ring_advance(ring);
466 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800467}
468
Chris Wilson3cce4692010-10-27 16:11:02 +0100469static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100470ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100471 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800472{
473 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100474 int ret;
475
476 ret = intel_ring_begin(ring, 4);
477 if (ret)
478 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100479
Chris Wilson78501ea2010-10-27 12:18:21 +0100480 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d52010-08-07 11:01:22 +0100481
Chris Wilson3cce4692010-10-27 16:11:02 +0100482 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
483 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
484 intel_ring_emit(ring, seqno);
485 intel_ring_emit(ring, MI_USER_INTERRUPT);
486 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800487
488 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +0100489 *result = seqno;
490 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800491}
492
Zou Nan haid1b851f2010-05-21 09:08:57 +0800493static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100494bsd_ring_get_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800495{
496 /* do nothing */
497}
498static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100499bsd_ring_put_user_irq(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800500{
501 /* do nothing */
502}
503
504static u32
Chris Wilson78501ea2010-10-27 12:18:21 +0100505ring_status_page_get_seqno(struct intel_ring_buffer *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800506{
507 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
508}
509
510static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000511ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800512{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100513 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100514
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100515 ret = intel_ring_begin(ring, 2);
516 if (ret)
517 return ret;
518
Chris Wilson78501ea2010-10-27 12:18:21 +0100519 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000520 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100521 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000522 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100523 intel_ring_advance(ring);
524
Zou Nan haid1b851f2010-05-21 09:08:57 +0800525 return 0;
526}
527
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800528static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100529render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000530 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700531{
Chris Wilson78501ea2010-10-27 12:18:21 +0100532 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700533 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000534 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700535
Chris Wilson6f392d52010-08-07 11:01:22 +0100536 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700537
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000538 if (IS_I830(dev) || IS_845G(dev)) {
539 ret = intel_ring_begin(ring, 4);
540 if (ret)
541 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700542
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000543 intel_ring_emit(ring, MI_BATCH_BUFFER);
544 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
545 intel_ring_emit(ring, offset + len - 8);
546 intel_ring_emit(ring, 0);
547 } else {
548 ret = intel_ring_begin(ring, 2);
549 if (ret)
550 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100551
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000552 if (INTEL_INFO(dev)->gen >= 4) {
553 intel_ring_emit(ring,
554 MI_BATCH_BUFFER_START | (2 << 6) |
555 MI_BATCH_NON_SECURE_I965);
556 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700557 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000558 intel_ring_emit(ring,
559 MI_BATCH_BUFFER_START | (2 << 6));
560 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700561 }
562 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000563 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700564
Eric Anholt62fdfea2010-05-21 13:26:39 -0700565 return 0;
566}
567
Chris Wilson78501ea2010-10-27 12:18:21 +0100568static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700569{
Chris Wilson78501ea2010-10-27 12:18:21 +0100570 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000571 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700572
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800573 obj = ring->status_page.obj;
574 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700575 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700576
Chris Wilson05394f32010-11-08 19:18:58 +0000577 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700578 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000579 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800580 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700581
582 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700583}
584
Chris Wilson78501ea2010-10-27 12:18:21 +0100585static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700586{
Chris Wilson78501ea2010-10-27 12:18:21 +0100587 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700588 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000589 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700590 int ret;
591
Eric Anholt62fdfea2010-05-21 13:26:39 -0700592 obj = i915_gem_alloc_object(dev, 4096);
593 if (obj == NULL) {
594 DRM_ERROR("Failed to allocate status page\n");
595 ret = -ENOMEM;
596 goto err;
597 }
Chris Wilson05394f32010-11-08 19:18:58 +0000598 obj->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700599
Daniel Vetter75e9e912010-11-04 17:11:09 +0100600 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700601 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700602 goto err_unref;
603 }
604
Chris Wilson05394f32010-11-08 19:18:58 +0000605 ring->status_page.gfx_addr = obj->gtt_offset;
606 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800607 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700608 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700609 goto err_unpin;
610 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800611 ring->status_page.obj = obj;
612 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700613
Chris Wilson78501ea2010-10-27 12:18:21 +0100614 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800615 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
616 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700617
618 return 0;
619
620err_unpin:
621 i915_gem_object_unpin(obj);
622err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000623 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700624err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800625 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700626}
627
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800628int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100629 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700630{
Chris Wilson05394f32010-11-08 19:18:58 +0000631 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100632 int ret;
633
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800634 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100635 INIT_LIST_HEAD(&ring->active_list);
636 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100637 INIT_LIST_HEAD(&ring->gpu_write_list);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700638
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800639 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100640 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800641 if (ret)
642 return ret;
643 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700644
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800645 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700646 if (obj == NULL) {
647 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800648 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100649 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700650 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700651
Chris Wilson05394f32010-11-08 19:18:58 +0000652 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800653
Daniel Vetter75e9e912010-11-04 17:11:09 +0100654 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100655 if (ret)
656 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700657
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800658 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000659 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700660 ring->map.type = 0;
661 ring->map.flags = 0;
662 ring->map.mtrr = 0;
663
664 drm_core_ioremap_wc(&ring->map, dev);
665 if (ring->map.handle == NULL) {
666 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800667 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100668 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700669 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800670
Eric Anholt62fdfea2010-05-21 13:26:39 -0700671 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100672 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100673 if (ret)
674 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700675
Chris Wilsonc584fe42010-10-29 18:15:52 +0100676 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100677
678err_unmap:
679 drm_core_ioremapfree(&ring->map, dev);
680err_unpin:
681 i915_gem_object_unpin(obj);
682err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000683 drm_gem_object_unreference(&obj->base);
684 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100685err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100686 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800687 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700688}
689
Chris Wilson78501ea2010-10-27 12:18:21 +0100690void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700691{
Chris Wilson33626e62010-10-29 16:18:36 +0100692 struct drm_i915_private *dev_priv;
693 int ret;
694
Chris Wilson05394f32010-11-08 19:18:58 +0000695 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700696 return;
697
Chris Wilson33626e62010-10-29 16:18:36 +0100698 /* Disable the ring buffer. The ring must be idle at this point */
699 dev_priv = ring->dev->dev_private;
700 ret = intel_wait_ring_buffer(ring, ring->size - 8);
701 I915_WRITE_CTL(ring, 0);
702
Chris Wilson78501ea2010-10-27 12:18:21 +0100703 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700704
Chris Wilson05394f32010-11-08 19:18:58 +0000705 i915_gem_object_unpin(ring->obj);
706 drm_gem_object_unreference(&ring->obj->base);
707 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100708
Zou Nan hai8d192152010-11-02 16:31:01 +0800709 if (ring->cleanup)
710 ring->cleanup(ring);
711
Chris Wilson78501ea2010-10-27 12:18:21 +0100712 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700713}
714
Chris Wilson78501ea2010-10-27 12:18:21 +0100715static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700716{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800717 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700718 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800719 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700720
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800721 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100722 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700723 if (ret)
724 return ret;
725 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700726
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800727 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100728 rem /= 8;
729 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700730 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100731 *virt++ = MI_NOOP;
732 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700733
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800734 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100735 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700736
737 return 0;
738}
739
Chris Wilson78501ea2010-10-27 12:18:21 +0100740int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700741{
Chris Wilson78501ea2010-10-27 12:18:21 +0100742 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800743 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100744 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100745 u32 head;
746
747 head = intel_read_status_page(ring, 4);
748 if (head) {
749 ring->head = head & HEAD_ADDR;
750 ring->space = ring->head - (ring->tail + 8);
751 if (ring->space < 0)
752 ring->space += ring->size;
753 if (ring->space >= n)
754 return 0;
755 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700756
757 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800758 end = jiffies + 3 * HZ;
759 do {
Daniel Vetter570ef602010-08-02 17:06:23 +0200760 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700761 ring->space = ring->head - (ring->tail + 8);
762 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800763 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700764 if (ring->space >= n) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100765 trace_i915_ring_wait_end(dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700766 return 0;
767 }
768
769 if (dev->primary->master) {
770 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
771 if (master_priv->sarea_priv)
772 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
773 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800774
Chris Wilsone60a0b12010-10-13 10:09:14 +0100775 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +0100776 if (atomic_read(&dev_priv->mm.wedged))
777 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800778 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700779 trace_i915_ring_wait_end (dev);
780 return -EBUSY;
781}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800782
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100783int intel_ring_begin(struct intel_ring_buffer *ring,
784 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800785{
Zou Nan haibe26a102010-06-12 17:40:24 +0800786 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100787 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100788
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100789 if (unlikely(ring->tail + n > ring->size)) {
790 ret = intel_wrap_ring_buffer(ring);
791 if (unlikely(ret))
792 return ret;
793 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100794
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100795 if (unlikely(ring->space < n)) {
796 ret = intel_wait_ring_buffer(ring, n);
797 if (unlikely(ret))
798 return ret;
799 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100800
801 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100802 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800803}
804
Chris Wilson78501ea2010-10-27 12:18:21 +0100805void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800806{
Chris Wilsond97ed332010-08-04 15:18:13 +0100807 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100808 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800809}
810
Chris Wilsone0708682010-09-19 14:46:27 +0100811static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800812 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100813 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200814 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800815 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800816 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100817 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800818 .flush = render_ring_flush,
819 .add_request = render_ring_add_request,
Chris Wilsonf787a5f2010-09-24 16:02:42 +0100820 .get_seqno = render_ring_get_seqno,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800821 .user_irq_get = render_ring_get_user_irq,
822 .user_irq_put = render_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100823 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Chris Wilsonb6913e42010-11-12 10:46:37 +0000824 .cleanup = render_ring_cleanup,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800825};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800826
827/* ring buffer for bit-stream decoder */
828
Chris Wilsone0708682010-09-19 14:46:27 +0100829static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +0800830 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +0100831 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200832 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800833 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100834 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100835 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800836 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +0100837 .add_request = ring_add_request,
838 .get_seqno = ring_status_page_get_seqno,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800839 .user_irq_get = bsd_ring_get_user_irq,
840 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100841 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800842};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800843
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100844
Chris Wilson78501ea2010-10-27 12:18:21 +0100845static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100846 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100847{
Chris Wilson78501ea2010-10-27 12:18:21 +0100848 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100849
850 /* Every tail move must follow the sequence below */
851 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
852 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
853 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
854 I915_WRITE(GEN6_BSD_RNCID, 0x0);
855
856 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
857 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
858 50))
859 DRM_ERROR("timed out waiting for IDLE Indicator\n");
860
Daniel Vetter870e86d2010-08-02 16:29:44 +0200861 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100862 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
863 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
864 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
865}
866
Chris Wilson78501ea2010-10-27 12:18:21 +0100867static void gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson549f7362010-10-19 11:19:32 +0100868 u32 invalidate_domains,
869 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100870{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100871 if (intel_ring_begin(ring, 4) == 0) {
872 intel_ring_emit(ring, MI_FLUSH_DW);
873 intel_ring_emit(ring, 0);
874 intel_ring_emit(ring, 0);
875 intel_ring_emit(ring, 0);
876 intel_ring_advance(ring);
877 }
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100878}
879
880static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100881gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000882 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100883{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100884 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100885
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100886 ret = intel_ring_begin(ring, 2);
887 if (ret)
888 return ret;
889
Chris Wilson78501ea2010-10-27 12:18:21 +0100890 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100891 /* bit0-7 is the length on GEN6+ */
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000892 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100893 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100894
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100895 return 0;
896}
897
898/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +0100899static const struct intel_ring_buffer gen6_bsd_ring = {
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100900 .name = "gen6 bsd ring",
901 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200902 .mmio_base = GEN6_BSD_RING_BASE,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100903 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100904 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100905 .write_tail = gen6_bsd_ring_write_tail,
Chris Wilson549f7362010-10-19 11:19:32 +0100906 .flush = gen6_ring_flush,
907 .add_request = ring_add_request,
908 .get_seqno = ring_status_page_get_seqno,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100909 .user_irq_get = bsd_ring_get_user_irq,
910 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100911 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +0100912};
913
914/* Blitter support (SandyBridge+) */
915
916static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100917blt_ring_get_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100918{
919 /* do nothing */
920}
921static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100922blt_ring_put_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100923{
924 /* do nothing */
925}
926
Zou Nan hai8d192152010-11-02 16:31:01 +0800927
928/* Workaround for some stepping of SNB,
929 * each time when BLT engine ring tail moved,
930 * the first command in the ring to be parsed
931 * should be MI_BATCH_BUFFER_START
932 */
933#define NEED_BLT_WORKAROUND(dev) \
934 (IS_GEN6(dev) && (dev->pdev->revision < 8))
935
936static inline struct drm_i915_gem_object *
937to_blt_workaround(struct intel_ring_buffer *ring)
938{
939 return ring->private;
940}
941
942static int blt_ring_init(struct intel_ring_buffer *ring)
943{
944 if (NEED_BLT_WORKAROUND(ring->dev)) {
945 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +0000946 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +0800947 int ret;
948
Chris Wilson05394f32010-11-08 19:18:58 +0000949 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +0800950 if (obj == NULL)
951 return -ENOMEM;
952
Chris Wilson05394f32010-11-08 19:18:58 +0000953 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +0800954 if (ret) {
955 drm_gem_object_unreference(&obj->base);
956 return ret;
957 }
958
959 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +0000960 *ptr++ = MI_BATCH_BUFFER_END;
961 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +0800962 kunmap(obj->pages[0]);
963
Chris Wilson05394f32010-11-08 19:18:58 +0000964 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +0800965 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000966 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +0800967 drm_gem_object_unreference(&obj->base);
968 return ret;
969 }
970
971 ring->private = obj;
972 }
973
974 return init_ring_common(ring);
975}
976
977static int blt_ring_begin(struct intel_ring_buffer *ring,
978 int num_dwords)
979{
980 if (ring->private) {
981 int ret = intel_ring_begin(ring, num_dwords+2);
982 if (ret)
983 return ret;
984
985 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
986 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
987
988 return 0;
989 } else
990 return intel_ring_begin(ring, 4);
991}
992
993static void blt_ring_flush(struct intel_ring_buffer *ring,
994 u32 invalidate_domains,
995 u32 flush_domains)
996{
997 if (blt_ring_begin(ring, 4) == 0) {
998 intel_ring_emit(ring, MI_FLUSH_DW);
999 intel_ring_emit(ring, 0);
1000 intel_ring_emit(ring, 0);
1001 intel_ring_emit(ring, 0);
1002 intel_ring_advance(ring);
1003 }
1004}
1005
1006static int
1007blt_ring_add_request(struct intel_ring_buffer *ring,
1008 u32 *result)
1009{
1010 u32 seqno;
1011 int ret;
1012
1013 ret = blt_ring_begin(ring, 4);
1014 if (ret)
1015 return ret;
1016
1017 seqno = i915_gem_get_seqno(ring->dev);
1018
1019 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1020 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1021 intel_ring_emit(ring, seqno);
1022 intel_ring_emit(ring, MI_USER_INTERRUPT);
1023 intel_ring_advance(ring);
1024
1025 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
1026 *result = seqno;
1027 return 0;
1028}
1029
1030static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1031{
1032 if (!ring->private)
1033 return;
1034
1035 i915_gem_object_unpin(ring->private);
1036 drm_gem_object_unreference(ring->private);
1037 ring->private = NULL;
1038}
1039
Chris Wilson549f7362010-10-19 11:19:32 +01001040static const struct intel_ring_buffer gen6_blt_ring = {
1041 .name = "blt ring",
1042 .id = RING_BLT,
1043 .mmio_base = BLT_RING_BASE,
1044 .size = 32 * PAGE_SIZE,
Zou Nan hai8d192152010-11-02 16:31:01 +08001045 .init = blt_ring_init,
Chris Wilson297b0c52010-10-22 17:02:41 +01001046 .write_tail = ring_write_tail,
Zou Nan hai8d192152010-11-02 16:31:01 +08001047 .flush = blt_ring_flush,
1048 .add_request = blt_ring_add_request,
Chris Wilson549f7362010-10-19 11:19:32 +01001049 .get_seqno = ring_status_page_get_seqno,
1050 .user_irq_get = blt_ring_get_user_irq,
1051 .user_irq_put = blt_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001052 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Zou Nan hai8d192152010-11-02 16:31:01 +08001053 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001054};
1055
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001056int intel_init_render_ring_buffer(struct drm_device *dev)
1057{
1058 drm_i915_private_t *dev_priv = dev->dev_private;
1059
1060 dev_priv->render_ring = render_ring;
1061
1062 if (!I915_NEED_GFX_HWS(dev)) {
1063 dev_priv->render_ring.status_page.page_addr
1064 = dev_priv->status_page_dmah->vaddr;
1065 memset(dev_priv->render_ring.status_page.page_addr,
1066 0, PAGE_SIZE);
1067 }
1068
1069 return intel_init_ring_buffer(dev, &dev_priv->render_ring);
1070}
1071
1072int intel_init_bsd_ring_buffer(struct drm_device *dev)
1073{
1074 drm_i915_private_t *dev_priv = dev->dev_private;
1075
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001076 if (IS_GEN6(dev))
1077 dev_priv->bsd_ring = gen6_bsd_ring;
1078 else
1079 dev_priv->bsd_ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001080
1081 return intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
1082}
Chris Wilson549f7362010-10-19 11:19:32 +01001083
1084int intel_init_blt_ring_buffer(struct drm_device *dev)
1085{
1086 drm_i915_private_t *dev_priv = dev->dev_private;
1087
1088 dev_priv->blt_ring = gen6_blt_ring;
1089
1090 return intel_init_ring_buffer(dev, &dev_priv->blt_ring);
1091}