blob: 7fc55a80be20d0044aa67de5fde0344eb3b8f822 [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 Wilson78501ea2010-10-27 12:18:21 +0100511ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
512 struct drm_i915_gem_execbuffer2 *exec,
513 struct drm_clip_rect *cliprects,
514 uint64_t exec_offset)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800515{
516 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100517 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100518
Zou Nan haid1b851f2010-05-21 09:08:57 +0800519 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilson78501ea2010-10-27 12:18:21 +0100520
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100521 ret = intel_ring_begin(ring, 2);
522 if (ret)
523 return ret;
524
Chris Wilson78501ea2010-10-27 12:18:21 +0100525 intel_ring_emit(ring,
526 MI_BATCH_BUFFER_START |
527 (2 << 6) |
528 MI_BATCH_NON_SECURE_I965);
529 intel_ring_emit(ring, exec_start);
530 intel_ring_advance(ring);
531
Zou Nan haid1b851f2010-05-21 09:08:57 +0800532 return 0;
533}
534
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800535static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100536render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
537 struct drm_i915_gem_execbuffer2 *exec,
538 struct drm_clip_rect *cliprects,
539 uint64_t exec_offset)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700540{
Chris Wilson78501ea2010-10-27 12:18:21 +0100541 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700542 drm_i915_private_t *dev_priv = dev->dev_private;
543 int nbox = exec->num_cliprects;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700544 uint32_t exec_start, exec_len;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100545 int i, count, ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100546
Eric Anholt62fdfea2010-05-21 13:26:39 -0700547 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
548 exec_len = (uint32_t) exec->batch_len;
549
Chris Wilson6f392d52010-08-07 11:01:22 +0100550 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700551
552 count = nbox ? nbox : 1;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700553 for (i = 0; i < count; i++) {
554 if (i < nbox) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100555 ret = i915_emit_box(dev, cliprects, i,
556 exec->DR1, exec->DR4);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700557 if (ret)
558 return ret;
559 }
560
561 if (IS_I830(dev) || IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100562 ret = intel_ring_begin(ring, 4);
563 if (ret)
564 return ret;
565
Chris Wilson78501ea2010-10-27 12:18:21 +0100566 intel_ring_emit(ring, MI_BATCH_BUFFER);
567 intel_ring_emit(ring, exec_start | MI_BATCH_NON_SECURE);
568 intel_ring_emit(ring, exec_start + exec_len - 4);
569 intel_ring_emit(ring, 0);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700570 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100571 ret = intel_ring_begin(ring, 2);
572 if (ret)
573 return ret;
574
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100575 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100576 intel_ring_emit(ring,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800577 MI_BATCH_BUFFER_START | (2 << 6)
578 | MI_BATCH_NON_SECURE_I965);
Chris Wilson78501ea2010-10-27 12:18:21 +0100579 intel_ring_emit(ring, exec_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700580 } else {
Chris Wilson78501ea2010-10-27 12:18:21 +0100581 intel_ring_emit(ring, MI_BATCH_BUFFER_START
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800582 | (2 << 6));
Chris Wilson78501ea2010-10-27 12:18:21 +0100583 intel_ring_emit(ring, exec_start |
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800584 MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700585 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700586 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100587 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700588 }
589
Eric Anholt62fdfea2010-05-21 13:26:39 -0700590 return 0;
591}
592
Chris Wilson78501ea2010-10-27 12:18:21 +0100593static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700594{
Chris Wilson78501ea2010-10-27 12:18:21 +0100595 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000596 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700597
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800598 obj = ring->status_page.obj;
599 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700600 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700601
Chris Wilson05394f32010-11-08 19:18:58 +0000602 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700603 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000604 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800605 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700606
607 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700608}
609
Chris Wilson78501ea2010-10-27 12:18:21 +0100610static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700611{
Chris Wilson78501ea2010-10-27 12:18:21 +0100612 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700613 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000614 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700615 int ret;
616
Eric Anholt62fdfea2010-05-21 13:26:39 -0700617 obj = i915_gem_alloc_object(dev, 4096);
618 if (obj == NULL) {
619 DRM_ERROR("Failed to allocate status page\n");
620 ret = -ENOMEM;
621 goto err;
622 }
Chris Wilson05394f32010-11-08 19:18:58 +0000623 obj->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700624
Daniel Vetter75e9e912010-11-04 17:11:09 +0100625 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700626 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700627 goto err_unref;
628 }
629
Chris Wilson05394f32010-11-08 19:18:58 +0000630 ring->status_page.gfx_addr = obj->gtt_offset;
631 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800632 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700633 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700634 goto err_unpin;
635 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800636 ring->status_page.obj = obj;
637 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700638
Chris Wilson78501ea2010-10-27 12:18:21 +0100639 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800640 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
641 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700642
643 return 0;
644
645err_unpin:
646 i915_gem_object_unpin(obj);
647err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000648 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700649err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800650 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700651}
652
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800653int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100654 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700655{
Chris Wilson05394f32010-11-08 19:18:58 +0000656 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100657 int ret;
658
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800659 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100660 INIT_LIST_HEAD(&ring->active_list);
661 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100662 INIT_LIST_HEAD(&ring->gpu_write_list);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700663
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800664 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100665 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800666 if (ret)
667 return ret;
668 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700669
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800670 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700671 if (obj == NULL) {
672 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800673 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100674 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700675 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700676
Chris Wilson05394f32010-11-08 19:18:58 +0000677 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800678
Daniel Vetter75e9e912010-11-04 17:11:09 +0100679 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100680 if (ret)
681 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700682
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800683 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000684 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700685 ring->map.type = 0;
686 ring->map.flags = 0;
687 ring->map.mtrr = 0;
688
689 drm_core_ioremap_wc(&ring->map, dev);
690 if (ring->map.handle == NULL) {
691 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800692 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100693 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700694 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800695
Eric Anholt62fdfea2010-05-21 13:26:39 -0700696 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100697 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100698 if (ret)
699 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700700
Chris Wilsonc584fe42010-10-29 18:15:52 +0100701 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100702
703err_unmap:
704 drm_core_ioremapfree(&ring->map, dev);
705err_unpin:
706 i915_gem_object_unpin(obj);
707err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000708 drm_gem_object_unreference(&obj->base);
709 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100710err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100711 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800712 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700713}
714
Chris Wilson78501ea2010-10-27 12:18:21 +0100715void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700716{
Chris Wilson33626e62010-10-29 16:18:36 +0100717 struct drm_i915_private *dev_priv;
718 int ret;
719
Chris Wilson05394f32010-11-08 19:18:58 +0000720 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700721 return;
722
Chris Wilson33626e62010-10-29 16:18:36 +0100723 /* Disable the ring buffer. The ring must be idle at this point */
724 dev_priv = ring->dev->dev_private;
725 ret = intel_wait_ring_buffer(ring, ring->size - 8);
726 I915_WRITE_CTL(ring, 0);
727
Chris Wilson78501ea2010-10-27 12:18:21 +0100728 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700729
Chris Wilson05394f32010-11-08 19:18:58 +0000730 i915_gem_object_unpin(ring->obj);
731 drm_gem_object_unreference(&ring->obj->base);
732 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100733
Zou Nan hai8d192152010-11-02 16:31:01 +0800734 if (ring->cleanup)
735 ring->cleanup(ring);
736
Chris Wilson78501ea2010-10-27 12:18:21 +0100737 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700738}
739
Chris Wilson78501ea2010-10-27 12:18:21 +0100740static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700741{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800742 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700743 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800744 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700745
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800746 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100747 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700748 if (ret)
749 return ret;
750 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700751
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800752 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100753 rem /= 8;
754 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700755 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100756 *virt++ = MI_NOOP;
757 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700758
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800759 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100760 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700761
762 return 0;
763}
764
Chris Wilson78501ea2010-10-27 12:18:21 +0100765int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700766{
Chris Wilson78501ea2010-10-27 12:18:21 +0100767 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800768 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100769 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100770 u32 head;
771
772 head = intel_read_status_page(ring, 4);
773 if (head) {
774 ring->head = head & HEAD_ADDR;
775 ring->space = ring->head - (ring->tail + 8);
776 if (ring->space < 0)
777 ring->space += ring->size;
778 if (ring->space >= n)
779 return 0;
780 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700781
782 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800783 end = jiffies + 3 * HZ;
784 do {
Daniel Vetter570ef602010-08-02 17:06:23 +0200785 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700786 ring->space = ring->head - (ring->tail + 8);
787 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800788 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700789 if (ring->space >= n) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100790 trace_i915_ring_wait_end(dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700791 return 0;
792 }
793
794 if (dev->primary->master) {
795 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
796 if (master_priv->sarea_priv)
797 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
798 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800799
Chris Wilsone60a0b12010-10-13 10:09:14 +0100800 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +0100801 if (atomic_read(&dev_priv->mm.wedged))
802 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800803 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700804 trace_i915_ring_wait_end (dev);
805 return -EBUSY;
806}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800807
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100808int intel_ring_begin(struct intel_ring_buffer *ring,
809 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800810{
Zou Nan haibe26a102010-06-12 17:40:24 +0800811 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100812 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100813
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100814 if (unlikely(ring->tail + n > ring->size)) {
815 ret = intel_wrap_ring_buffer(ring);
816 if (unlikely(ret))
817 return ret;
818 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100819
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100820 if (unlikely(ring->space < n)) {
821 ret = intel_wait_ring_buffer(ring, n);
822 if (unlikely(ret))
823 return ret;
824 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100825
826 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100827 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800828}
829
Chris Wilson78501ea2010-10-27 12:18:21 +0100830void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800831{
Chris Wilsond97ed332010-08-04 15:18:13 +0100832 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100833 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800834}
835
Chris Wilsone0708682010-09-19 14:46:27 +0100836static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800837 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100838 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200839 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800840 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800841 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100842 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800843 .flush = render_ring_flush,
844 .add_request = render_ring_add_request,
Chris Wilsonf787a5f2010-09-24 16:02:42 +0100845 .get_seqno = render_ring_get_seqno,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800846 .user_irq_get = render_ring_get_user_irq,
847 .user_irq_put = render_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100848 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Chris Wilsonb6913e42010-11-12 10:46:37 +0000849 .cleanup = render_ring_cleanup,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800850};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800851
852/* ring buffer for bit-stream decoder */
853
Chris Wilsone0708682010-09-19 14:46:27 +0100854static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +0800855 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +0100856 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200857 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800858 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100859 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100860 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800861 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +0100862 .add_request = ring_add_request,
863 .get_seqno = ring_status_page_get_seqno,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800864 .user_irq_get = bsd_ring_get_user_irq,
865 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100866 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800867};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800868
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100869
Chris Wilson78501ea2010-10-27 12:18:21 +0100870static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100871 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100872{
Chris Wilson78501ea2010-10-27 12:18:21 +0100873 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100874
875 /* Every tail move must follow the sequence below */
876 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
877 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
878 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
879 I915_WRITE(GEN6_BSD_RNCID, 0x0);
880
881 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
882 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
883 50))
884 DRM_ERROR("timed out waiting for IDLE Indicator\n");
885
Daniel Vetter870e86d2010-08-02 16:29:44 +0200886 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100887 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
888 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
889 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
890}
891
Chris Wilson78501ea2010-10-27 12:18:21 +0100892static void gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson549f7362010-10-19 11:19:32 +0100893 u32 invalidate_domains,
894 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100895{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100896 if (intel_ring_begin(ring, 4) == 0) {
897 intel_ring_emit(ring, MI_FLUSH_DW);
898 intel_ring_emit(ring, 0);
899 intel_ring_emit(ring, 0);
900 intel_ring_emit(ring, 0);
901 intel_ring_advance(ring);
902 }
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100903}
904
905static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100906gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
907 struct drm_i915_gem_execbuffer2 *exec,
908 struct drm_clip_rect *cliprects,
909 uint64_t exec_offset)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100910{
911 uint32_t exec_start;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100912 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100913
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100914 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100915
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100916 ret = intel_ring_begin(ring, 2);
917 if (ret)
918 return ret;
919
Chris Wilson78501ea2010-10-27 12:18:21 +0100920 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100921 /* bit0-7 is the length on GEN6+ */
Chris Wilson78501ea2010-10-27 12:18:21 +0100922 intel_ring_emit(ring, exec_start);
923 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100924
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100925 return 0;
926}
927
928/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +0100929static const struct intel_ring_buffer gen6_bsd_ring = {
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100930 .name = "gen6 bsd ring",
931 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200932 .mmio_base = GEN6_BSD_RING_BASE,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100933 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100934 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100935 .write_tail = gen6_bsd_ring_write_tail,
Chris Wilson549f7362010-10-19 11:19:32 +0100936 .flush = gen6_ring_flush,
937 .add_request = ring_add_request,
938 .get_seqno = ring_status_page_get_seqno,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100939 .user_irq_get = bsd_ring_get_user_irq,
940 .user_irq_put = bsd_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100941 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +0100942};
943
944/* Blitter support (SandyBridge+) */
945
946static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100947blt_ring_get_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100948{
949 /* do nothing */
950}
951static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100952blt_ring_put_user_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100953{
954 /* do nothing */
955}
956
Zou Nan hai8d192152010-11-02 16:31:01 +0800957
958/* Workaround for some stepping of SNB,
959 * each time when BLT engine ring tail moved,
960 * the first command in the ring to be parsed
961 * should be MI_BATCH_BUFFER_START
962 */
963#define NEED_BLT_WORKAROUND(dev) \
964 (IS_GEN6(dev) && (dev->pdev->revision < 8))
965
966static inline struct drm_i915_gem_object *
967to_blt_workaround(struct intel_ring_buffer *ring)
968{
969 return ring->private;
970}
971
972static int blt_ring_init(struct intel_ring_buffer *ring)
973{
974 if (NEED_BLT_WORKAROUND(ring->dev)) {
975 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +0000976 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +0800977 int ret;
978
Chris Wilson05394f32010-11-08 19:18:58 +0000979 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +0800980 if (obj == NULL)
981 return -ENOMEM;
982
Chris Wilson05394f32010-11-08 19:18:58 +0000983 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +0800984 if (ret) {
985 drm_gem_object_unreference(&obj->base);
986 return ret;
987 }
988
989 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +0000990 *ptr++ = MI_BATCH_BUFFER_END;
991 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +0800992 kunmap(obj->pages[0]);
993
Chris Wilson05394f32010-11-08 19:18:58 +0000994 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +0800995 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000996 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +0800997 drm_gem_object_unreference(&obj->base);
998 return ret;
999 }
1000
1001 ring->private = obj;
1002 }
1003
1004 return init_ring_common(ring);
1005}
1006
1007static int blt_ring_begin(struct intel_ring_buffer *ring,
1008 int num_dwords)
1009{
1010 if (ring->private) {
1011 int ret = intel_ring_begin(ring, num_dwords+2);
1012 if (ret)
1013 return ret;
1014
1015 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
1016 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
1017
1018 return 0;
1019 } else
1020 return intel_ring_begin(ring, 4);
1021}
1022
1023static void blt_ring_flush(struct intel_ring_buffer *ring,
1024 u32 invalidate_domains,
1025 u32 flush_domains)
1026{
1027 if (blt_ring_begin(ring, 4) == 0) {
1028 intel_ring_emit(ring, MI_FLUSH_DW);
1029 intel_ring_emit(ring, 0);
1030 intel_ring_emit(ring, 0);
1031 intel_ring_emit(ring, 0);
1032 intel_ring_advance(ring);
1033 }
1034}
1035
1036static int
1037blt_ring_add_request(struct intel_ring_buffer *ring,
1038 u32 *result)
1039{
1040 u32 seqno;
1041 int ret;
1042
1043 ret = blt_ring_begin(ring, 4);
1044 if (ret)
1045 return ret;
1046
1047 seqno = i915_gem_get_seqno(ring->dev);
1048
1049 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1050 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1051 intel_ring_emit(ring, seqno);
1052 intel_ring_emit(ring, MI_USER_INTERRUPT);
1053 intel_ring_advance(ring);
1054
1055 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
1056 *result = seqno;
1057 return 0;
1058}
1059
1060static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1061{
1062 if (!ring->private)
1063 return;
1064
1065 i915_gem_object_unpin(ring->private);
1066 drm_gem_object_unreference(ring->private);
1067 ring->private = NULL;
1068}
1069
Chris Wilson549f7362010-10-19 11:19:32 +01001070static const struct intel_ring_buffer gen6_blt_ring = {
1071 .name = "blt ring",
1072 .id = RING_BLT,
1073 .mmio_base = BLT_RING_BASE,
1074 .size = 32 * PAGE_SIZE,
Zou Nan hai8d192152010-11-02 16:31:01 +08001075 .init = blt_ring_init,
Chris Wilson297b0c52010-10-22 17:02:41 +01001076 .write_tail = ring_write_tail,
Zou Nan hai8d192152010-11-02 16:31:01 +08001077 .flush = blt_ring_flush,
1078 .add_request = blt_ring_add_request,
Chris Wilson549f7362010-10-19 11:19:32 +01001079 .get_seqno = ring_status_page_get_seqno,
1080 .user_irq_get = blt_ring_get_user_irq,
1081 .user_irq_put = blt_ring_put_user_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001082 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Zou Nan hai8d192152010-11-02 16:31:01 +08001083 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001084};
1085
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001086int intel_init_render_ring_buffer(struct drm_device *dev)
1087{
1088 drm_i915_private_t *dev_priv = dev->dev_private;
1089
1090 dev_priv->render_ring = render_ring;
1091
1092 if (!I915_NEED_GFX_HWS(dev)) {
1093 dev_priv->render_ring.status_page.page_addr
1094 = dev_priv->status_page_dmah->vaddr;
1095 memset(dev_priv->render_ring.status_page.page_addr,
1096 0, PAGE_SIZE);
1097 }
1098
1099 return intel_init_ring_buffer(dev, &dev_priv->render_ring);
1100}
1101
1102int intel_init_bsd_ring_buffer(struct drm_device *dev)
1103{
1104 drm_i915_private_t *dev_priv = dev->dev_private;
1105
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001106 if (IS_GEN6(dev))
1107 dev_priv->bsd_ring = gen6_bsd_ring;
1108 else
1109 dev_priv->bsd_ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001110
1111 return intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
1112}
Chris Wilson549f7362010-10-19 11:19:32 +01001113
1114int intel_init_blt_ring_buffer(struct drm_device *dev)
1115{
1116 drm_i915_private_t *dev_priv = dev->dev_private;
1117
1118 dev_priv->blt_ring = gen6_blt_ring;
1119
1120 return intel_init_ring_buffer(dev, &dev_priv->blt_ring);
1121}