blob: ec7175e0dcd8708d1632941c863d3ab1aacc1676 [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 Wilsonc7dca472011-01-20 17:00:10 +000037static inline int ring_space(struct intel_ring_buffer *ring)
38{
39 int space = (ring->head & HEAD_ADDR) - (ring->tail + 8);
40 if (space < 0)
41 space += ring->size;
42 return space;
43}
44
Chris Wilson6f392d5482010-08-07 11:01:22 +010045static u32 i915_gem_get_seqno(struct drm_device *dev)
46{
47 drm_i915_private_t *dev_priv = dev->dev_private;
48 u32 seqno;
49
50 seqno = dev_priv->next_seqno;
51
52 /* reserve 0 for non-seqno */
53 if (++dev_priv->next_seqno == 0)
54 dev_priv->next_seqno = 1;
55
56 return seqno;
57}
58
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000059static int
Chris Wilson78501ea2010-10-27 12:18:21 +010060render_ring_flush(struct intel_ring_buffer *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +010061 u32 invalidate_domains,
62 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070063{
Chris Wilson78501ea2010-10-27 12:18:21 +010064 struct drm_device *dev = ring->dev;
Chris Wilson6f392d5482010-08-07 11:01:22 +010065 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000066 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +010067
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
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000116 ret = intel_ring_begin(ring, 2);
117 if (ret)
118 return ret;
119
120 intel_ring_emit(ring, cmd);
121 intel_ring_emit(ring, MI_NOOP);
122 intel_ring_advance(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800123 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000124
125 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800126}
127
Chris Wilson78501ea2010-10-27 12:18:21 +0100128static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100129 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800130{
Chris Wilson78501ea2010-10-27 12:18:21 +0100131 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100132 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800133}
134
Chris Wilson78501ea2010-10-27 12:18:21 +0100135u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800136{
Chris Wilson78501ea2010-10-27 12:18:21 +0100137 drm_i915_private_t *dev_priv = ring->dev->dev_private;
138 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200139 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800140
141 return I915_READ(acthd_reg);
142}
143
Chris Wilson78501ea2010-10-27 12:18:21 +0100144static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800145{
Chris Wilson78501ea2010-10-27 12:18:21 +0100146 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000147 struct drm_i915_gem_object *obj = ring->obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800148 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800149
150 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200151 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200152 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100153 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800154
155 /* Initialize the ring. */
Chris Wilson05394f32010-11-08 19:18:58 +0000156 I915_WRITE_START(ring, obj->gtt_offset);
Daniel Vetter570ef602010-08-02 17:06:23 +0200157 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800158
159 /* G45 ring initialization fails to reset head to zero */
160 if (head != 0) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000161 DRM_DEBUG_KMS("%s head not reset to zero "
162 "ctl %08x head %08x tail %08x start %08x\n",
163 ring->name,
164 I915_READ_CTL(ring),
165 I915_READ_HEAD(ring),
166 I915_READ_TAIL(ring),
167 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800168
Daniel Vetter570ef602010-08-02 17:06:23 +0200169 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800170
Chris Wilson6fd0d562010-12-05 20:42:33 +0000171 if (I915_READ_HEAD(ring) & HEAD_ADDR) {
172 DRM_ERROR("failed to set %s head to zero "
173 "ctl %08x head %08x tail %08x start %08x\n",
174 ring->name,
175 I915_READ_CTL(ring),
176 I915_READ_HEAD(ring),
177 I915_READ_TAIL(ring),
178 I915_READ_START(ring));
179 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700180 }
181
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200182 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000183 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson6aa56062010-10-29 21:44:37 +0100184 | RING_REPORT_64K | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800185
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800186 /* If the head is still not zero, the ring is dead */
Chris Wilson176f28e2010-10-28 11:18:07 +0100187 if ((I915_READ_CTL(ring) & RING_VALID) == 0 ||
Chris Wilson05394f32010-11-08 19:18:58 +0000188 I915_READ_START(ring) != obj->gtt_offset ||
Chris Wilson176f28e2010-10-28 11:18:07 +0100189 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000190 DRM_ERROR("%s initialization failed "
191 "ctl %08x head %08x tail %08x start %08x\n",
192 ring->name,
193 I915_READ_CTL(ring),
194 I915_READ_HEAD(ring),
195 I915_READ_TAIL(ring),
196 I915_READ_START(ring));
197 return -EIO;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800198 }
199
Chris Wilson78501ea2010-10-27 12:18:21 +0100200 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
201 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800202 else {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000203 ring->head = I915_READ_HEAD(ring);
Daniel Vetter870e86d2010-08-02 16:29:44 +0200204 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000205 ring->space = ring_space(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800206 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000207
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800208 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700209}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800210
Chris Wilsonc6df5412010-12-15 09:56:50 +0000211/*
212 * 965+ support PIPE_CONTROL commands, which provide finer grained control
213 * over cache flushing.
214 */
215struct pipe_control {
216 struct drm_i915_gem_object *obj;
217 volatile u32 *cpu_page;
218 u32 gtt_offset;
219};
220
221static int
222init_pipe_control(struct intel_ring_buffer *ring)
223{
224 struct pipe_control *pc;
225 struct drm_i915_gem_object *obj;
226 int ret;
227
228 if (ring->private)
229 return 0;
230
231 pc = kmalloc(sizeof(*pc), GFP_KERNEL);
232 if (!pc)
233 return -ENOMEM;
234
235 obj = i915_gem_alloc_object(ring->dev, 4096);
236 if (obj == NULL) {
237 DRM_ERROR("Failed to allocate seqno page\n");
238 ret = -ENOMEM;
239 goto err;
240 }
241 obj->agp_type = AGP_USER_CACHED_MEMORY;
242
243 ret = i915_gem_object_pin(obj, 4096, true);
244 if (ret)
245 goto err_unref;
246
247 pc->gtt_offset = obj->gtt_offset;
248 pc->cpu_page = kmap(obj->pages[0]);
249 if (pc->cpu_page == NULL)
250 goto err_unpin;
251
252 pc->obj = obj;
253 ring->private = pc;
254 return 0;
255
256err_unpin:
257 i915_gem_object_unpin(obj);
258err_unref:
259 drm_gem_object_unreference(&obj->base);
260err:
261 kfree(pc);
262 return ret;
263}
264
265static void
266cleanup_pipe_control(struct intel_ring_buffer *ring)
267{
268 struct pipe_control *pc = ring->private;
269 struct drm_i915_gem_object *obj;
270
271 if (!ring->private)
272 return;
273
274 obj = pc->obj;
275 kunmap(obj->pages[0]);
276 i915_gem_object_unpin(obj);
277 drm_gem_object_unreference(&obj->base);
278
279 kfree(pc);
280 ring->private = NULL;
281}
282
Chris Wilson78501ea2010-10-27 12:18:21 +0100283static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800284{
Chris Wilson78501ea2010-10-27 12:18:21 +0100285 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000286 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100287 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800288
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100289 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100290 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800291 if (IS_GEN6(dev))
292 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
293 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800294 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100295
Chris Wilsonc6df5412010-12-15 09:56:50 +0000296 if (INTEL_INFO(dev)->gen >= 6) {
297 } else if (IS_GEN5(dev)) {
298 ret = init_pipe_control(ring);
299 if (ret)
300 return ret;
301 }
302
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800303 return ret;
304}
305
Chris Wilsonc6df5412010-12-15 09:56:50 +0000306static void render_ring_cleanup(struct intel_ring_buffer *ring)
307{
308 if (!ring->private)
309 return;
310
311 cleanup_pipe_control(ring);
312}
313
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000314static void
315update_semaphore(struct intel_ring_buffer *ring, int i, u32 seqno)
316{
317 struct drm_device *dev = ring->dev;
318 struct drm_i915_private *dev_priv = dev->dev_private;
319 int id;
320
321 /*
322 * cs -> 1 = vcs, 0 = bcs
323 * vcs -> 1 = bcs, 0 = cs,
324 * bcs -> 1 = cs, 0 = vcs.
325 */
326 id = ring - dev_priv->ring;
327 id += 2 - i;
328 id %= 3;
329
330 intel_ring_emit(ring,
331 MI_SEMAPHORE_MBOX |
332 MI_SEMAPHORE_REGISTER |
333 MI_SEMAPHORE_UPDATE);
334 intel_ring_emit(ring, seqno);
335 intel_ring_emit(ring,
336 RING_SYNC_0(dev_priv->ring[id].mmio_base) + 4*i);
337}
338
339static int
340gen6_add_request(struct intel_ring_buffer *ring,
341 u32 *result)
342{
343 u32 seqno;
344 int ret;
345
346 ret = intel_ring_begin(ring, 10);
347 if (ret)
348 return ret;
349
350 seqno = i915_gem_get_seqno(ring->dev);
351 update_semaphore(ring, 0, seqno);
352 update_semaphore(ring, 1, seqno);
353
354 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
355 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
356 intel_ring_emit(ring, seqno);
357 intel_ring_emit(ring, MI_USER_INTERRUPT);
358 intel_ring_advance(ring);
359
360 *result = seqno;
361 return 0;
362}
363
364int
365intel_ring_sync(struct intel_ring_buffer *ring,
366 struct intel_ring_buffer *to,
367 u32 seqno)
368{
369 int ret;
370
371 ret = intel_ring_begin(ring, 4);
372 if (ret)
373 return ret;
374
375 intel_ring_emit(ring,
376 MI_SEMAPHORE_MBOX |
377 MI_SEMAPHORE_REGISTER |
378 intel_ring_sync_index(ring, to) << 17 |
379 MI_SEMAPHORE_COMPARE);
380 intel_ring_emit(ring, seqno);
381 intel_ring_emit(ring, 0);
382 intel_ring_emit(ring, MI_NOOP);
383 intel_ring_advance(ring);
384
385 return 0;
386}
387
Chris Wilsonc6df5412010-12-15 09:56:50 +0000388#define PIPE_CONTROL_FLUSH(ring__, addr__) \
389do { \
390 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
391 PIPE_CONTROL_DEPTH_STALL | 2); \
392 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
393 intel_ring_emit(ring__, 0); \
394 intel_ring_emit(ring__, 0); \
395} while (0)
396
397static int
398pc_render_add_request(struct intel_ring_buffer *ring,
399 u32 *result)
400{
401 struct drm_device *dev = ring->dev;
402 u32 seqno = i915_gem_get_seqno(dev);
403 struct pipe_control *pc = ring->private;
404 u32 scratch_addr = pc->gtt_offset + 128;
405 int ret;
406
407 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
408 * incoherent with writes to memory, i.e. completely fubar,
409 * so we need to use PIPE_NOTIFY instead.
410 *
411 * However, we also need to workaround the qword write
412 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
413 * memory before requesting an interrupt.
414 */
415 ret = intel_ring_begin(ring, 32);
416 if (ret)
417 return ret;
418
419 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
420 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
421 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
422 intel_ring_emit(ring, seqno);
423 intel_ring_emit(ring, 0);
424 PIPE_CONTROL_FLUSH(ring, scratch_addr);
425 scratch_addr += 128; /* write to separate cachelines */
426 PIPE_CONTROL_FLUSH(ring, scratch_addr);
427 scratch_addr += 128;
428 PIPE_CONTROL_FLUSH(ring, scratch_addr);
429 scratch_addr += 128;
430 PIPE_CONTROL_FLUSH(ring, scratch_addr);
431 scratch_addr += 128;
432 PIPE_CONTROL_FLUSH(ring, scratch_addr);
433 scratch_addr += 128;
434 PIPE_CONTROL_FLUSH(ring, scratch_addr);
435 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
436 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
437 PIPE_CONTROL_NOTIFY);
438 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
439 intel_ring_emit(ring, seqno);
440 intel_ring_emit(ring, 0);
441 intel_ring_advance(ring);
442
443 *result = seqno;
444 return 0;
445}
446
Chris Wilson3cce4692010-10-27 16:11:02 +0100447static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100448render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100449 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700450{
Chris Wilson78501ea2010-10-27 12:18:21 +0100451 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100452 u32 seqno = i915_gem_get_seqno(dev);
453 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800454
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000455 ret = intel_ring_begin(ring, 4);
456 if (ret)
457 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100458
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000459 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
460 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
461 intel_ring_emit(ring, seqno);
462 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100463 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000464
Chris Wilson3cce4692010-10-27 16:11:02 +0100465 *result = seqno;
466 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700467}
468
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800469static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000470ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800471{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000472 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
473}
474
Chris Wilsonc6df5412010-12-15 09:56:50 +0000475static u32
476pc_render_get_seqno(struct intel_ring_buffer *ring)
477{
478 struct pipe_control *pc = ring->private;
479 return pc->cpu_page[0];
480}
481
Chris Wilson0f468322011-01-04 17:35:21 +0000482static void
483ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
484{
485 dev_priv->gt_irq_mask &= ~mask;
486 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
487 POSTING_READ(GTIMR);
488}
489
490static void
491ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
492{
493 dev_priv->gt_irq_mask |= mask;
494 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
495 POSTING_READ(GTIMR);
496}
497
498static void
499i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
500{
501 dev_priv->irq_mask &= ~mask;
502 I915_WRITE(IMR, dev_priv->irq_mask);
503 POSTING_READ(IMR);
504}
505
506static void
507i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
508{
509 dev_priv->irq_mask |= mask;
510 I915_WRITE(IMR, dev_priv->irq_mask);
511 POSTING_READ(IMR);
512}
513
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000514static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000515render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700516{
Chris Wilson78501ea2010-10-27 12:18:21 +0100517 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000518 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700519
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000520 if (!dev->irq_enabled)
521 return false;
522
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000523 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000524 if (ring->irq_refcount++ == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700525 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000526 ironlake_enable_irq(dev_priv,
527 GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700528 else
529 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
530 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000531 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000532
533 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700534}
535
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800536static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000537render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700538{
Chris Wilson78501ea2010-10-27 12:18:21 +0100539 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000540 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700541
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000542 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000543 if (--ring->irq_refcount == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700544 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000545 ironlake_disable_irq(dev_priv,
546 GT_USER_INTERRUPT |
547 GT_PIPE_NOTIFY);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700548 else
549 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
550 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000551 spin_unlock(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700552}
553
Chris Wilson78501ea2010-10-27 12:18:21 +0100554void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800555{
Chris Wilson78501ea2010-10-27 12:18:21 +0100556 drm_i915_private_t *dev_priv = ring->dev->dev_private;
557 u32 mmio = IS_GEN6(ring->dev) ?
558 RING_HWS_PGA_GEN6(ring->mmio_base) :
559 RING_HWS_PGA(ring->mmio_base);
560 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
561 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800562}
563
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000564static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100565bsd_ring_flush(struct intel_ring_buffer *ring,
566 u32 invalidate_domains,
567 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800568{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000569 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000570
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000571 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
572 return 0;
573
574 ret = intel_ring_begin(ring, 2);
575 if (ret)
576 return ret;
577
578 intel_ring_emit(ring, MI_FLUSH);
579 intel_ring_emit(ring, MI_NOOP);
580 intel_ring_advance(ring);
581 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800582}
583
Chris Wilson3cce4692010-10-27 16:11:02 +0100584static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100585ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100586 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800587{
588 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100589 int ret;
590
591 ret = intel_ring_begin(ring, 4);
592 if (ret)
593 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100594
Chris Wilson78501ea2010-10-27 12:18:21 +0100595 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d5482010-08-07 11:01:22 +0100596
Chris Wilson3cce4692010-10-27 16:11:02 +0100597 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
598 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
599 intel_ring_emit(ring, seqno);
600 intel_ring_emit(ring, MI_USER_INTERRUPT);
601 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800602
Chris Wilson3cce4692010-10-27 16:11:02 +0100603 *result = seqno;
604 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800605}
606
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000607static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000608ring_get_irq(struct intel_ring_buffer *ring, u32 flag)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800609{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000610 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000611 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000612
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000613 if (!dev->irq_enabled)
614 return false;
615
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000616 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000617 if (ring->irq_refcount++ == 0)
Chris Wilson0f468322011-01-04 17:35:21 +0000618 ironlake_enable_irq(dev_priv, flag);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000619 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000620
621 return true;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800622}
623
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000624static void
625ring_put_irq(struct intel_ring_buffer *ring, u32 flag)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800626{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000627 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000628 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000629
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000630 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000631 if (--ring->irq_refcount == 0)
Chris Wilson0f468322011-01-04 17:35:21 +0000632 ironlake_disable_irq(dev_priv, flag);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000633 spin_unlock(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000634}
635
636static bool
637gen6_ring_get_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
638{
639 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000640 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000641
642 if (!dev->irq_enabled)
643 return false;
644
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000645 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000646 if (ring->irq_refcount++ == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000647 ring->irq_mask &= ~rflag;
648 I915_WRITE_IMR(ring, ring->irq_mask);
649 ironlake_enable_irq(dev_priv, gflag);
Chris Wilson0f468322011-01-04 17:35:21 +0000650 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000651 spin_unlock(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000652
653 return true;
654}
655
656static void
657gen6_ring_put_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
658{
659 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000660 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000661
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000662 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000663 if (--ring->irq_refcount == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000664 ring->irq_mask |= rflag;
665 I915_WRITE_IMR(ring, ring->irq_mask);
666 ironlake_disable_irq(dev_priv, gflag);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000667 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000668 spin_unlock(&ring->irq_lock);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000669}
670
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000671static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000672bsd_ring_get_irq(struct intel_ring_buffer *ring)
673{
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000674 return ring_get_irq(ring, GT_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000675}
676static void
677bsd_ring_put_irq(struct intel_ring_buffer *ring)
678{
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000679 ring_put_irq(ring, GT_BSD_USER_INTERRUPT);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800680}
681
682static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000683ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800684{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100685 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100686
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100687 ret = intel_ring_begin(ring, 2);
688 if (ret)
689 return ret;
690
Chris Wilson78501ea2010-10-27 12:18:21 +0100691 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000692 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100693 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000694 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100695 intel_ring_advance(ring);
696
Zou Nan haid1b851f2010-05-21 09:08:57 +0800697 return 0;
698}
699
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800700static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100701render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000702 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700703{
Chris Wilson78501ea2010-10-27 12:18:21 +0100704 struct drm_device *dev = ring->dev;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000705 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700706
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000707 if (IS_I830(dev) || IS_845G(dev)) {
708 ret = intel_ring_begin(ring, 4);
709 if (ret)
710 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700711
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000712 intel_ring_emit(ring, MI_BATCH_BUFFER);
713 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
714 intel_ring_emit(ring, offset + len - 8);
715 intel_ring_emit(ring, 0);
716 } else {
717 ret = intel_ring_begin(ring, 2);
718 if (ret)
719 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100720
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000721 if (INTEL_INFO(dev)->gen >= 4) {
722 intel_ring_emit(ring,
723 MI_BATCH_BUFFER_START | (2 << 6) |
724 MI_BATCH_NON_SECURE_I965);
725 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700726 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000727 intel_ring_emit(ring,
728 MI_BATCH_BUFFER_START | (2 << 6));
729 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700730 }
731 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000732 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700733
Eric Anholt62fdfea2010-05-21 13:26:39 -0700734 return 0;
735}
736
Chris Wilson78501ea2010-10-27 12:18:21 +0100737static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700738{
Chris Wilson78501ea2010-10-27 12:18:21 +0100739 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000740 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700741
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800742 obj = ring->status_page.obj;
743 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700744 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700745
Chris Wilson05394f32010-11-08 19:18:58 +0000746 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700747 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000748 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800749 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700750
751 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700752}
753
Chris Wilson78501ea2010-10-27 12:18:21 +0100754static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700755{
Chris Wilson78501ea2010-10-27 12:18:21 +0100756 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700757 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000758 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700759 int ret;
760
Eric Anholt62fdfea2010-05-21 13:26:39 -0700761 obj = i915_gem_alloc_object(dev, 4096);
762 if (obj == NULL) {
763 DRM_ERROR("Failed to allocate status page\n");
764 ret = -ENOMEM;
765 goto err;
766 }
Chris Wilson05394f32010-11-08 19:18:58 +0000767 obj->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700768
Daniel Vetter75e9e912010-11-04 17:11:09 +0100769 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700770 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700771 goto err_unref;
772 }
773
Chris Wilson05394f32010-11-08 19:18:58 +0000774 ring->status_page.gfx_addr = obj->gtt_offset;
775 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800776 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700777 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700778 goto err_unpin;
779 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800780 ring->status_page.obj = obj;
781 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700782
Chris Wilson78501ea2010-10-27 12:18:21 +0100783 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800784 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
785 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700786
787 return 0;
788
789err_unpin:
790 i915_gem_object_unpin(obj);
791err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000792 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700793err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800794 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700795}
796
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800797int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100798 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700799{
Chris Wilson05394f32010-11-08 19:18:58 +0000800 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100801 int ret;
802
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800803 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100804 INIT_LIST_HEAD(&ring->active_list);
805 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100806 INIT_LIST_HEAD(&ring->gpu_write_list);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000807
808 spin_lock_init(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000809 ring->irq_mask = ~0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700810
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800811 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100812 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800813 if (ret)
814 return ret;
815 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700816
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800817 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700818 if (obj == NULL) {
819 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800820 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100821 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700822 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700823
Chris Wilson05394f32010-11-08 19:18:58 +0000824 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800825
Daniel Vetter75e9e912010-11-04 17:11:09 +0100826 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100827 if (ret)
828 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700829
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800830 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000831 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700832 ring->map.type = 0;
833 ring->map.flags = 0;
834 ring->map.mtrr = 0;
835
836 drm_core_ioremap_wc(&ring->map, dev);
837 if (ring->map.handle == NULL) {
838 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800839 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100840 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700841 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800842
Eric Anholt62fdfea2010-05-21 13:26:39 -0700843 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100844 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100845 if (ret)
846 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700847
Chris Wilson55249ba2010-12-22 14:04:47 +0000848 /* Workaround an erratum on the i830 which causes a hang if
849 * the TAIL pointer points to within the last 2 cachelines
850 * of the buffer.
851 */
852 ring->effective_size = ring->size;
853 if (IS_I830(ring->dev))
854 ring->effective_size -= 128;
855
Chris Wilsonc584fe42010-10-29 18:15:52 +0100856 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100857
858err_unmap:
859 drm_core_ioremapfree(&ring->map, dev);
860err_unpin:
861 i915_gem_object_unpin(obj);
862err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000863 drm_gem_object_unreference(&obj->base);
864 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100865err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100866 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800867 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700868}
869
Chris Wilson78501ea2010-10-27 12:18:21 +0100870void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700871{
Chris Wilson33626e62010-10-29 16:18:36 +0100872 struct drm_i915_private *dev_priv;
873 int ret;
874
Chris Wilson05394f32010-11-08 19:18:58 +0000875 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700876 return;
877
Chris Wilson33626e62010-10-29 16:18:36 +0100878 /* Disable the ring buffer. The ring must be idle at this point */
879 dev_priv = ring->dev->dev_private;
880 ret = intel_wait_ring_buffer(ring, ring->size - 8);
Chris Wilson29ee3992011-01-24 16:35:42 +0000881 if (ret)
882 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
883 ring->name, ret);
884
Chris Wilson33626e62010-10-29 16:18:36 +0100885 I915_WRITE_CTL(ring, 0);
886
Chris Wilson78501ea2010-10-27 12:18:21 +0100887 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700888
Chris Wilson05394f32010-11-08 19:18:58 +0000889 i915_gem_object_unpin(ring->obj);
890 drm_gem_object_unreference(&ring->obj->base);
891 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100892
Zou Nan hai8d192152010-11-02 16:31:01 +0800893 if (ring->cleanup)
894 ring->cleanup(ring);
895
Chris Wilson78501ea2010-10-27 12:18:21 +0100896 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700897}
898
Chris Wilson78501ea2010-10-27 12:18:21 +0100899static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700900{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800901 unsigned int *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +0000902 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700903
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800904 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100905 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700906 if (ret)
907 return ret;
908 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700909
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800910 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100911 rem /= 8;
912 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700913 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100914 *virt++ = MI_NOOP;
915 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700916
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800917 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000918 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700919
920 return 0;
921}
922
Chris Wilson78501ea2010-10-27 12:18:21 +0100923int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700924{
Chris Wilson78501ea2010-10-27 12:18:21 +0100925 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800926 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100927 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100928 u32 head;
929
Chris Wilsonc7dca472011-01-20 17:00:10 +0000930 /* If the reported head position has wrapped or hasn't advanced,
931 * fallback to the slow and accurate path.
932 */
933 head = intel_read_status_page(ring, 4);
934 if (head > ring->head) {
935 ring->head = head;
936 ring->space = ring_space(ring);
937 if (ring->space >= n)
938 return 0;
939 }
940
Chris Wilsondb53a302011-02-03 11:57:46 +0000941 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800942 end = jiffies + 3 * HZ;
943 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000944 ring->head = I915_READ_HEAD(ring);
945 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700946 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000947 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700948 return 0;
949 }
950
951 if (dev->primary->master) {
952 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
953 if (master_priv->sarea_priv)
954 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
955 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800956
Chris Wilsone60a0b12010-10-13 10:09:14 +0100957 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +0100958 if (atomic_read(&dev_priv->mm.wedged))
959 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800960 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +0000961 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700962 return -EBUSY;
963}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800964
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100965int intel_ring_begin(struct intel_ring_buffer *ring,
966 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800967{
Chris Wilson21dd3732011-01-26 15:55:56 +0000968 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +0800969 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100970 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100971
Chris Wilson21dd3732011-01-26 15:55:56 +0000972 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
973 return -EIO;
974
Chris Wilson55249ba2010-12-22 14:04:47 +0000975 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100976 ret = intel_wrap_ring_buffer(ring);
977 if (unlikely(ret))
978 return ret;
979 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100980
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100981 if (unlikely(ring->space < n)) {
982 ret = intel_wait_ring_buffer(ring, n);
983 if (unlikely(ret))
984 return ret;
985 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100986
987 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100988 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800989}
990
Chris Wilson78501ea2010-10-27 12:18:21 +0100991void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800992{
Chris Wilsond97ed332010-08-04 15:18:13 +0100993 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100994 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800995}
996
Chris Wilsone0708682010-09-19 14:46:27 +0100997static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800998 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100999 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001000 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001001 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001002 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001003 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001004 .flush = render_ring_flush,
1005 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001006 .get_seqno = ring_get_seqno,
1007 .irq_get = render_ring_get_irq,
1008 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001009 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Chris Wilsonc6df5412010-12-15 09:56:50 +00001010 .cleanup = render_ring_cleanup,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001011};
Zou Nan haid1b851f2010-05-21 09:08:57 +08001012
1013/* ring buffer for bit-stream decoder */
1014
Chris Wilsone0708682010-09-19 14:46:27 +01001015static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +08001016 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +01001017 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001018 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001019 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +01001020 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +01001021 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001022 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +01001023 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001024 .get_seqno = ring_get_seqno,
1025 .irq_get = bsd_ring_get_irq,
1026 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001027 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001028};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001029
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001030
Chris Wilson78501ea2010-10-27 12:18:21 +01001031static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001032 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001033{
Chris Wilson78501ea2010-10-27 12:18:21 +01001034 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001035
1036 /* Every tail move must follow the sequence below */
1037 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1038 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1039 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1040 I915_WRITE(GEN6_BSD_RNCID, 0x0);
1041
1042 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1043 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1044 50))
1045 DRM_ERROR("timed out waiting for IDLE Indicator\n");
1046
Daniel Vetter870e86d2010-08-02 16:29:44 +02001047 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001048 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1049 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1050 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
1051}
1052
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001053static int gen6_ring_flush(struct intel_ring_buffer *ring,
1054 u32 invalidate_domains,
1055 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001056{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001057 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001058
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001059 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
1060 return 0;
1061
1062 ret = intel_ring_begin(ring, 4);
1063 if (ret)
1064 return ret;
1065
1066 intel_ring_emit(ring, MI_FLUSH_DW);
1067 intel_ring_emit(ring, 0);
1068 intel_ring_emit(ring, 0);
1069 intel_ring_emit(ring, 0);
1070 intel_ring_advance(ring);
1071 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001072}
1073
1074static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001075gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001076 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001077{
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001078 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001079
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001080 ret = intel_ring_begin(ring, 2);
1081 if (ret)
1082 return ret;
1083
Chris Wilson78501ea2010-10-27 12:18:21 +01001084 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001085 /* bit0-7 is the length on GEN6+ */
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001086 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001087 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001088
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001089 return 0;
1090}
1091
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001092static bool
Chris Wilson0f468322011-01-04 17:35:21 +00001093gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
1094{
1095 return gen6_ring_get_irq(ring,
1096 GT_USER_INTERRUPT,
1097 GEN6_RENDER_USER_INTERRUPT);
1098}
1099
1100static void
1101gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
1102{
1103 return gen6_ring_put_irq(ring,
1104 GT_USER_INTERRUPT,
1105 GEN6_RENDER_USER_INTERRUPT);
1106}
1107
1108static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001109gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
1110{
Chris Wilson0f468322011-01-04 17:35:21 +00001111 return gen6_ring_get_irq(ring,
1112 GT_GEN6_BSD_USER_INTERRUPT,
1113 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001114}
1115
1116static void
1117gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
1118{
Chris Wilson0f468322011-01-04 17:35:21 +00001119 return gen6_ring_put_irq(ring,
1120 GT_GEN6_BSD_USER_INTERRUPT,
1121 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001122}
1123
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001124/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +01001125static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001126 .name = "gen6 bsd ring",
1127 .id = RING_BSD,
1128 .mmio_base = GEN6_BSD_RING_BASE,
1129 .size = 32 * PAGE_SIZE,
1130 .init = init_ring_common,
1131 .write_tail = gen6_bsd_ring_write_tail,
1132 .flush = gen6_ring_flush,
1133 .add_request = gen6_add_request,
1134 .get_seqno = ring_get_seqno,
1135 .irq_get = gen6_bsd_ring_get_irq,
1136 .irq_put = gen6_bsd_ring_put_irq,
1137 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +01001138};
1139
1140/* Blitter support (SandyBridge+) */
1141
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001142static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001143blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001144{
Chris Wilson0f468322011-01-04 17:35:21 +00001145 return gen6_ring_get_irq(ring,
1146 GT_BLT_USER_INTERRUPT,
1147 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001148}
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001149
Chris Wilson549f7362010-10-19 11:19:32 +01001150static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001151blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001152{
Chris Wilson0f468322011-01-04 17:35:21 +00001153 gen6_ring_put_irq(ring,
1154 GT_BLT_USER_INTERRUPT,
1155 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001156}
1157
Zou Nan hai8d192152010-11-02 16:31:01 +08001158
1159/* Workaround for some stepping of SNB,
1160 * each time when BLT engine ring tail moved,
1161 * the first command in the ring to be parsed
1162 * should be MI_BATCH_BUFFER_START
1163 */
1164#define NEED_BLT_WORKAROUND(dev) \
1165 (IS_GEN6(dev) && (dev->pdev->revision < 8))
1166
1167static inline struct drm_i915_gem_object *
1168to_blt_workaround(struct intel_ring_buffer *ring)
1169{
1170 return ring->private;
1171}
1172
1173static int blt_ring_init(struct intel_ring_buffer *ring)
1174{
1175 if (NEED_BLT_WORKAROUND(ring->dev)) {
1176 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +00001177 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +08001178 int ret;
1179
Chris Wilson05394f32010-11-08 19:18:58 +00001180 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +08001181 if (obj == NULL)
1182 return -ENOMEM;
1183
Chris Wilson05394f32010-11-08 19:18:58 +00001184 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +08001185 if (ret) {
1186 drm_gem_object_unreference(&obj->base);
1187 return ret;
1188 }
1189
1190 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +00001191 *ptr++ = MI_BATCH_BUFFER_END;
1192 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +08001193 kunmap(obj->pages[0]);
1194
Chris Wilson05394f32010-11-08 19:18:58 +00001195 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +08001196 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00001197 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +08001198 drm_gem_object_unreference(&obj->base);
1199 return ret;
1200 }
1201
1202 ring->private = obj;
1203 }
1204
1205 return init_ring_common(ring);
1206}
1207
1208static int blt_ring_begin(struct intel_ring_buffer *ring,
1209 int num_dwords)
1210{
1211 if (ring->private) {
1212 int ret = intel_ring_begin(ring, num_dwords+2);
1213 if (ret)
1214 return ret;
1215
1216 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
1217 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
1218
1219 return 0;
1220 } else
1221 return intel_ring_begin(ring, 4);
1222}
1223
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001224static int blt_ring_flush(struct intel_ring_buffer *ring,
Zou Nan hai8d192152010-11-02 16:31:01 +08001225 u32 invalidate_domains,
1226 u32 flush_domains)
1227{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001228 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001229
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001230 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
1231 return 0;
1232
1233 ret = blt_ring_begin(ring, 4);
1234 if (ret)
1235 return ret;
1236
1237 intel_ring_emit(ring, MI_FLUSH_DW);
1238 intel_ring_emit(ring, 0);
1239 intel_ring_emit(ring, 0);
1240 intel_ring_emit(ring, 0);
1241 intel_ring_advance(ring);
1242 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001243}
1244
Zou Nan hai8d192152010-11-02 16:31:01 +08001245static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1246{
1247 if (!ring->private)
1248 return;
1249
1250 i915_gem_object_unpin(ring->private);
1251 drm_gem_object_unreference(ring->private);
1252 ring->private = NULL;
1253}
1254
Chris Wilson549f7362010-10-19 11:19:32 +01001255static const struct intel_ring_buffer gen6_blt_ring = {
1256 .name = "blt ring",
1257 .id = RING_BLT,
1258 .mmio_base = BLT_RING_BASE,
1259 .size = 32 * PAGE_SIZE,
Zou Nan hai8d192152010-11-02 16:31:01 +08001260 .init = blt_ring_init,
Chris Wilson297b0c52010-10-22 17:02:41 +01001261 .write_tail = ring_write_tail,
Zou Nan hai8d192152010-11-02 16:31:01 +08001262 .flush = blt_ring_flush,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001263 .add_request = gen6_add_request,
1264 .get_seqno = ring_get_seqno,
1265 .irq_get = blt_ring_get_irq,
1266 .irq_put = blt_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001267 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Zou Nan hai8d192152010-11-02 16:31:01 +08001268 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001269};
1270
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001271int intel_init_render_ring_buffer(struct drm_device *dev)
1272{
1273 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001274 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001275
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001276 *ring = render_ring;
1277 if (INTEL_INFO(dev)->gen >= 6) {
1278 ring->add_request = gen6_add_request;
Chris Wilson0f468322011-01-04 17:35:21 +00001279 ring->irq_get = gen6_render_ring_get_irq;
1280 ring->irq_put = gen6_render_ring_put_irq;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001281 } else if (IS_GEN5(dev)) {
1282 ring->add_request = pc_render_add_request;
1283 ring->get_seqno = pc_render_get_seqno;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001284 }
1285
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001286 if (!I915_NEED_GFX_HWS(dev)) {
1287 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1288 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1289 }
1290
1291 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001292}
1293
Chris Wilsone8616b62011-01-20 09:57:11 +00001294int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1295{
1296 drm_i915_private_t *dev_priv = dev->dev_private;
1297 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1298
1299 *ring = render_ring;
1300 if (INTEL_INFO(dev)->gen >= 6) {
1301 ring->add_request = gen6_add_request;
1302 ring->irq_get = gen6_render_ring_get_irq;
1303 ring->irq_put = gen6_render_ring_put_irq;
1304 } else if (IS_GEN5(dev)) {
1305 ring->add_request = pc_render_add_request;
1306 ring->get_seqno = pc_render_get_seqno;
1307 }
1308
1309 ring->dev = dev;
1310 INIT_LIST_HEAD(&ring->active_list);
1311 INIT_LIST_HEAD(&ring->request_list);
1312 INIT_LIST_HEAD(&ring->gpu_write_list);
1313
1314 ring->size = size;
1315 ring->effective_size = ring->size;
1316 if (IS_I830(ring->dev))
1317 ring->effective_size -= 128;
1318
1319 ring->map.offset = start;
1320 ring->map.size = size;
1321 ring->map.type = 0;
1322 ring->map.flags = 0;
1323 ring->map.mtrr = 0;
1324
1325 drm_core_ioremap_wc(&ring->map, dev);
1326 if (ring->map.handle == NULL) {
1327 DRM_ERROR("can not ioremap virtual address for"
1328 " ring buffer\n");
1329 return -ENOMEM;
1330 }
1331
1332 ring->virtual_start = (void __force __iomem *)ring->map.handle;
1333 return 0;
1334}
1335
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001336int intel_init_bsd_ring_buffer(struct drm_device *dev)
1337{
1338 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001339 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001340
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001341 if (IS_GEN6(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001342 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001343 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001344 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001345
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001346 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001347}
Chris Wilson549f7362010-10-19 11:19:32 +01001348
1349int intel_init_blt_ring_buffer(struct drm_device *dev)
1350{
1351 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001352 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001353
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001354 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001355
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001356 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001357}