blob: a40f3456448d4f3b8a24afac20c64561338d6714 [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 Wilson6f392d52010-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 Wilson6f392d52010-08-07 11:01:22 +010065 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000066 int ret;
Chris Wilson6f392d52010-08-07 11:01:22 +010067
Chris Wilson36d527d2011-03-19 22:26:49 +000068 /*
69 * read/write caches:
70 *
71 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
72 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
73 * also flushed at 2d versus 3d pipeline switches.
74 *
75 * read-only caches:
76 *
77 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
78 * MI_READ_FLUSH is set, and is always flushed on 965.
79 *
80 * I915_GEM_DOMAIN_COMMAND may not exist?
81 *
82 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
83 * invalidated when MI_EXE_FLUSH is set.
84 *
85 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
86 * invalidated with every MI_FLUSH.
87 *
88 * TLBs:
89 *
90 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
91 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
92 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
93 * are flushed at any MI_FLUSH.
94 */
95
96 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
97 if ((invalidate_domains|flush_domains) &
98 I915_GEM_DOMAIN_RENDER)
99 cmd &= ~MI_NO_WRITE_FLUSH;
100 if (INTEL_INFO(dev)->gen < 4) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700101 /*
Chris Wilson36d527d2011-03-19 22:26:49 +0000102 * On the 965, the sampler cache always gets flushed
103 * and this bit is reserved.
Eric Anholt62fdfea2010-05-21 13:26:39 -0700104 */
Chris Wilson36d527d2011-03-19 22:26:49 +0000105 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
106 cmd |= MI_READ_FLUSH;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800107 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000108 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
109 cmd |= MI_EXE_FLUSH;
110
111 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
112 (IS_G4X(dev) || IS_GEN5(dev)))
113 cmd |= MI_INVALIDATE_ISP;
114
115 ret = intel_ring_begin(ring, 2);
116 if (ret)
117 return ret;
118
119 intel_ring_emit(ring, cmd);
120 intel_ring_emit(ring, MI_NOOP);
121 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000122
123 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800124}
125
Chris Wilson78501ea2010-10-27 12:18:21 +0100126static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100127 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800128{
Chris Wilson78501ea2010-10-27 12:18:21 +0100129 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100130 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800131}
132
Chris Wilson78501ea2010-10-27 12:18:21 +0100133u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800134{
Chris Wilson78501ea2010-10-27 12:18:21 +0100135 drm_i915_private_t *dev_priv = ring->dev->dev_private;
136 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200137 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800138
139 return I915_READ(acthd_reg);
140}
141
Chris Wilson78501ea2010-10-27 12:18:21 +0100142static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800143{
Chris Wilson78501ea2010-10-27 12:18:21 +0100144 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000145 struct drm_i915_gem_object *obj = ring->obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800146 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800147
148 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200149 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200150 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100151 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800152
153 /* Initialize the ring. */
Chris Wilson05394f32010-11-08 19:18:58 +0000154 I915_WRITE_START(ring, obj->gtt_offset);
Daniel Vetter570ef602010-08-02 17:06:23 +0200155 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800156
157 /* G45 ring initialization fails to reset head to zero */
158 if (head != 0) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000159 DRM_DEBUG_KMS("%s head not reset to zero "
160 "ctl %08x head %08x tail %08x start %08x\n",
161 ring->name,
162 I915_READ_CTL(ring),
163 I915_READ_HEAD(ring),
164 I915_READ_TAIL(ring),
165 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800166
Daniel Vetter570ef602010-08-02 17:06:23 +0200167 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800168
Chris Wilson6fd0d562010-12-05 20:42:33 +0000169 if (I915_READ_HEAD(ring) & HEAD_ADDR) {
170 DRM_ERROR("failed to set %s head to zero "
171 "ctl %08x head %08x tail %08x start %08x\n",
172 ring->name,
173 I915_READ_CTL(ring),
174 I915_READ_HEAD(ring),
175 I915_READ_TAIL(ring),
176 I915_READ_START(ring));
177 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700178 }
179
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200180 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000181 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson6aa56062010-10-29 21:44:37 +0100182 | RING_REPORT_64K | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800183
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800184 /* If the head is still not zero, the ring is dead */
Chris Wilson176f28e2010-10-28 11:18:07 +0100185 if ((I915_READ_CTL(ring) & RING_VALID) == 0 ||
Chris Wilson05394f32010-11-08 19:18:58 +0000186 I915_READ_START(ring) != obj->gtt_offset ||
Chris Wilson176f28e2010-10-28 11:18:07 +0100187 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000188 DRM_ERROR("%s initialization failed "
189 "ctl %08x head %08x tail %08x start %08x\n",
190 ring->name,
191 I915_READ_CTL(ring),
192 I915_READ_HEAD(ring),
193 I915_READ_TAIL(ring),
194 I915_READ_START(ring));
195 return -EIO;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800196 }
197
Chris Wilson78501ea2010-10-27 12:18:21 +0100198 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
199 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800200 else {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000201 ring->head = I915_READ_HEAD(ring);
Daniel Vetter870e86d2010-08-02 16:29:44 +0200202 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000203 ring->space = ring_space(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800204 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000205
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800206 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700207}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800208
Chris Wilsonc6df5412010-12-15 09:56:50 +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 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100239
240 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000241
242 ret = i915_gem_object_pin(obj, 4096, true);
243 if (ret)
244 goto err_unref;
245
246 pc->gtt_offset = obj->gtt_offset;
247 pc->cpu_page = kmap(obj->pages[0]);
248 if (pc->cpu_page == NULL)
249 goto err_unpin;
250
251 pc->obj = obj;
252 ring->private = pc;
253 return 0;
254
255err_unpin:
256 i915_gem_object_unpin(obj);
257err_unref:
258 drm_gem_object_unreference(&obj->base);
259err:
260 kfree(pc);
261 return ret;
262}
263
264static void
265cleanup_pipe_control(struct intel_ring_buffer *ring)
266{
267 struct pipe_control *pc = ring->private;
268 struct drm_i915_gem_object *obj;
269
270 if (!ring->private)
271 return;
272
273 obj = pc->obj;
274 kunmap(obj->pages[0]);
275 i915_gem_object_unpin(obj);
276 drm_gem_object_unreference(&obj->base);
277
278 kfree(pc);
279 ring->private = NULL;
280}
281
Chris Wilson78501ea2010-10-27 12:18:21 +0100282static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800283{
Chris Wilson78501ea2010-10-27 12:18:21 +0100284 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000285 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100286 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800287
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100288 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100289 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Jesse Barnes65d3eb12011-04-06 14:54:44 -0700290 if (IS_GEN6(dev) || IS_GEN7(dev))
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800291 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
292 I915_WRITE(MI_MODE, mode);
Jesse Barnesb095cd02011-08-12 15:28:32 -0700293 if (IS_GEN7(dev))
294 I915_WRITE(GFX_MODE_GEN7,
295 GFX_MODE_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
296 GFX_MODE_ENABLE(GFX_REPLAY_MODE));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800297 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100298
Chris Wilsonc6df5412010-12-15 09:56:50 +0000299 if (INTEL_INFO(dev)->gen >= 6) {
300 } else if (IS_GEN5(dev)) {
301 ret = init_pipe_control(ring);
302 if (ret)
303 return ret;
304 }
305
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800306 return ret;
307}
308
Chris Wilsonc6df5412010-12-15 09:56:50 +0000309static void render_ring_cleanup(struct intel_ring_buffer *ring)
310{
311 if (!ring->private)
312 return;
313
314 cleanup_pipe_control(ring);
315}
316
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000317static void
318update_semaphore(struct intel_ring_buffer *ring, int i, u32 seqno)
319{
320 struct drm_device *dev = ring->dev;
321 struct drm_i915_private *dev_priv = dev->dev_private;
322 int id;
323
324 /*
325 * cs -> 1 = vcs, 0 = bcs
326 * vcs -> 1 = bcs, 0 = cs,
327 * bcs -> 1 = cs, 0 = vcs.
328 */
329 id = ring - dev_priv->ring;
330 id += 2 - i;
331 id %= 3;
332
333 intel_ring_emit(ring,
334 MI_SEMAPHORE_MBOX |
335 MI_SEMAPHORE_REGISTER |
336 MI_SEMAPHORE_UPDATE);
337 intel_ring_emit(ring, seqno);
338 intel_ring_emit(ring,
339 RING_SYNC_0(dev_priv->ring[id].mmio_base) + 4*i);
340}
341
342static int
343gen6_add_request(struct intel_ring_buffer *ring,
344 u32 *result)
345{
346 u32 seqno;
347 int ret;
348
349 ret = intel_ring_begin(ring, 10);
350 if (ret)
351 return ret;
352
353 seqno = i915_gem_get_seqno(ring->dev);
354 update_semaphore(ring, 0, seqno);
355 update_semaphore(ring, 1, seqno);
356
357 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
358 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
359 intel_ring_emit(ring, seqno);
360 intel_ring_emit(ring, MI_USER_INTERRUPT);
361 intel_ring_advance(ring);
362
363 *result = seqno;
364 return 0;
365}
366
367int
368intel_ring_sync(struct intel_ring_buffer *ring,
369 struct intel_ring_buffer *to,
370 u32 seqno)
371{
372 int ret;
373
374 ret = intel_ring_begin(ring, 4);
375 if (ret)
376 return ret;
377
378 intel_ring_emit(ring,
379 MI_SEMAPHORE_MBOX |
380 MI_SEMAPHORE_REGISTER |
381 intel_ring_sync_index(ring, to) << 17 |
382 MI_SEMAPHORE_COMPARE);
383 intel_ring_emit(ring, seqno);
384 intel_ring_emit(ring, 0);
385 intel_ring_emit(ring, MI_NOOP);
386 intel_ring_advance(ring);
387
388 return 0;
389}
390
Chris Wilsonc6df5412010-12-15 09:56:50 +0000391#define PIPE_CONTROL_FLUSH(ring__, addr__) \
392do { \
393 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
394 PIPE_CONTROL_DEPTH_STALL | 2); \
395 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
396 intel_ring_emit(ring__, 0); \
397 intel_ring_emit(ring__, 0); \
398} while (0)
399
400static int
401pc_render_add_request(struct intel_ring_buffer *ring,
402 u32 *result)
403{
404 struct drm_device *dev = ring->dev;
405 u32 seqno = i915_gem_get_seqno(dev);
406 struct pipe_control *pc = ring->private;
407 u32 scratch_addr = pc->gtt_offset + 128;
408 int ret;
409
410 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
411 * incoherent with writes to memory, i.e. completely fubar,
412 * so we need to use PIPE_NOTIFY instead.
413 *
414 * However, we also need to workaround the qword write
415 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
416 * memory before requesting an interrupt.
417 */
418 ret = intel_ring_begin(ring, 32);
419 if (ret)
420 return ret;
421
422 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
423 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
424 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
425 intel_ring_emit(ring, seqno);
426 intel_ring_emit(ring, 0);
427 PIPE_CONTROL_FLUSH(ring, scratch_addr);
428 scratch_addr += 128; /* write to separate cachelines */
429 PIPE_CONTROL_FLUSH(ring, scratch_addr);
430 scratch_addr += 128;
431 PIPE_CONTROL_FLUSH(ring, scratch_addr);
432 scratch_addr += 128;
433 PIPE_CONTROL_FLUSH(ring, scratch_addr);
434 scratch_addr += 128;
435 PIPE_CONTROL_FLUSH(ring, scratch_addr);
436 scratch_addr += 128;
437 PIPE_CONTROL_FLUSH(ring, scratch_addr);
438 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
439 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
440 PIPE_CONTROL_NOTIFY);
441 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
442 intel_ring_emit(ring, seqno);
443 intel_ring_emit(ring, 0);
444 intel_ring_advance(ring);
445
446 *result = seqno;
447 return 0;
448}
449
Chris Wilson3cce4692010-10-27 16:11:02 +0100450static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100451render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100452 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700453{
Chris Wilson78501ea2010-10-27 12:18:21 +0100454 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100455 u32 seqno = i915_gem_get_seqno(dev);
456 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800457
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000458 ret = intel_ring_begin(ring, 4);
459 if (ret)
460 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100461
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000462 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
463 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
464 intel_ring_emit(ring, seqno);
465 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100466 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000467
Chris Wilson3cce4692010-10-27 16:11:02 +0100468 *result = seqno;
469 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700470}
471
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800472static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000473ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800474{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000475 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
476}
477
Chris Wilsonc6df5412010-12-15 09:56:50 +0000478static u32
479pc_render_get_seqno(struct intel_ring_buffer *ring)
480{
481 struct pipe_control *pc = ring->private;
482 return pc->cpu_page[0];
483}
484
Chris Wilson0f468322011-01-04 17:35:21 +0000485static void
486ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
487{
488 dev_priv->gt_irq_mask &= ~mask;
489 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
490 POSTING_READ(GTIMR);
491}
492
493static void
494ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
495{
496 dev_priv->gt_irq_mask |= mask;
497 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
498 POSTING_READ(GTIMR);
499}
500
501static void
502i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
503{
504 dev_priv->irq_mask &= ~mask;
505 I915_WRITE(IMR, dev_priv->irq_mask);
506 POSTING_READ(IMR);
507}
508
509static void
510i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
511{
512 dev_priv->irq_mask |= mask;
513 I915_WRITE(IMR, dev_priv->irq_mask);
514 POSTING_READ(IMR);
515}
516
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000517static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000518render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700519{
Chris Wilson78501ea2010-10-27 12:18:21 +0100520 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000521 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700522
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000523 if (!dev->irq_enabled)
524 return false;
525
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000526 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000527 if (ring->irq_refcount++ == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700528 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000529 ironlake_enable_irq(dev_priv,
530 GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700531 else
532 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
533 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000534 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000535
536 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700537}
538
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800539static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000540render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700541{
Chris Wilson78501ea2010-10-27 12:18:21 +0100542 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000543 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700544
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000545 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000546 if (--ring->irq_refcount == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700547 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000548 ironlake_disable_irq(dev_priv,
549 GT_USER_INTERRUPT |
550 GT_PIPE_NOTIFY);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700551 else
552 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
553 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000554 spin_unlock(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700555}
556
Chris Wilson78501ea2010-10-27 12:18:21 +0100557void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800558{
Eric Anholt45930102011-05-06 17:12:35 -0700559 struct drm_device *dev = ring->dev;
Chris Wilson78501ea2010-10-27 12:18:21 +0100560 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700561 u32 mmio = 0;
562
563 /* The ring status page addresses are no longer next to the rest of
564 * the ring registers as of gen7.
565 */
566 if (IS_GEN7(dev)) {
567 switch (ring->id) {
568 case RING_RENDER:
569 mmio = RENDER_HWS_PGA_GEN7;
570 break;
571 case RING_BLT:
572 mmio = BLT_HWS_PGA_GEN7;
573 break;
574 case RING_BSD:
575 mmio = BSD_HWS_PGA_GEN7;
576 break;
577 }
578 } else if (IS_GEN6(ring->dev)) {
579 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
580 } else {
581 mmio = RING_HWS_PGA(ring->mmio_base);
582 }
583
Chris Wilson78501ea2010-10-27 12:18:21 +0100584 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
585 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800586}
587
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000588static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100589bsd_ring_flush(struct intel_ring_buffer *ring,
590 u32 invalidate_domains,
591 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800592{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000593 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000594
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000595 ret = intel_ring_begin(ring, 2);
596 if (ret)
597 return ret;
598
599 intel_ring_emit(ring, MI_FLUSH);
600 intel_ring_emit(ring, MI_NOOP);
601 intel_ring_advance(ring);
602 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800603}
604
Chris Wilson3cce4692010-10-27 16:11:02 +0100605static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100606ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100607 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800608{
609 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100610 int ret;
611
612 ret = intel_ring_begin(ring, 4);
613 if (ret)
614 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100615
Chris Wilson78501ea2010-10-27 12:18:21 +0100616 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d52010-08-07 11:01:22 +0100617
Chris Wilson3cce4692010-10-27 16:11:02 +0100618 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
619 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
620 intel_ring_emit(ring, seqno);
621 intel_ring_emit(ring, MI_USER_INTERRUPT);
622 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800623
Chris Wilson3cce4692010-10-27 16:11:02 +0100624 *result = seqno;
625 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800626}
627
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000628static bool
Chris Wilson0f468322011-01-04 17:35:21 +0000629gen6_ring_get_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
630{
631 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000632 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000633
634 if (!dev->irq_enabled)
635 return false;
636
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000637 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000638 if (ring->irq_refcount++ == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000639 ring->irq_mask &= ~rflag;
640 I915_WRITE_IMR(ring, ring->irq_mask);
641 ironlake_enable_irq(dev_priv, gflag);
Chris Wilson0f468322011-01-04 17:35:21 +0000642 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000643 spin_unlock(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000644
645 return true;
646}
647
648static void
649gen6_ring_put_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
650{
651 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000652 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000653
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000654 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000655 if (--ring->irq_refcount == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000656 ring->irq_mask |= rflag;
657 I915_WRITE_IMR(ring, ring->irq_mask);
658 ironlake_disable_irq(dev_priv, gflag);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000659 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000660 spin_unlock(&ring->irq_lock);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000661}
662
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000663static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000664bsd_ring_get_irq(struct intel_ring_buffer *ring)
665{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800666 struct drm_device *dev = ring->dev;
667 drm_i915_private_t *dev_priv = dev->dev_private;
668
669 if (!dev->irq_enabled)
670 return false;
671
672 spin_lock(&ring->irq_lock);
673 if (ring->irq_refcount++ == 0) {
674 if (IS_G4X(dev))
675 i915_enable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
676 else
677 ironlake_enable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
678 }
679 spin_unlock(&ring->irq_lock);
680
681 return true;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000682}
683static void
684bsd_ring_put_irq(struct intel_ring_buffer *ring)
685{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800686 struct drm_device *dev = ring->dev;
687 drm_i915_private_t *dev_priv = dev->dev_private;
688
689 spin_lock(&ring->irq_lock);
690 if (--ring->irq_refcount == 0) {
691 if (IS_G4X(dev))
692 i915_disable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
693 else
694 ironlake_disable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
695 }
696 spin_unlock(&ring->irq_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800697}
698
699static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000700ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800701{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100702 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100703
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100704 ret = intel_ring_begin(ring, 2);
705 if (ret)
706 return ret;
707
Chris Wilson78501ea2010-10-27 12:18:21 +0100708 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000709 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100710 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000711 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100712 intel_ring_advance(ring);
713
Zou Nan haid1b851f2010-05-21 09:08:57 +0800714 return 0;
715}
716
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800717static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100718render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000719 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700720{
Chris Wilson78501ea2010-10-27 12:18:21 +0100721 struct drm_device *dev = ring->dev;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000722 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700723
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000724 if (IS_I830(dev) || IS_845G(dev)) {
725 ret = intel_ring_begin(ring, 4);
726 if (ret)
727 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700728
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000729 intel_ring_emit(ring, MI_BATCH_BUFFER);
730 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
731 intel_ring_emit(ring, offset + len - 8);
732 intel_ring_emit(ring, 0);
733 } else {
734 ret = intel_ring_begin(ring, 2);
735 if (ret)
736 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100737
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000738 if (INTEL_INFO(dev)->gen >= 4) {
739 intel_ring_emit(ring,
740 MI_BATCH_BUFFER_START | (2 << 6) |
741 MI_BATCH_NON_SECURE_I965);
742 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700743 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000744 intel_ring_emit(ring,
745 MI_BATCH_BUFFER_START | (2 << 6));
746 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700747 }
748 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000749 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700750
Eric Anholt62fdfea2010-05-21 13:26:39 -0700751 return 0;
752}
753
Chris Wilson78501ea2010-10-27 12:18:21 +0100754static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700755{
Chris Wilson78501ea2010-10-27 12:18:21 +0100756 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000757 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700758
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800759 obj = ring->status_page.obj;
760 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700761 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700762
Chris Wilson05394f32010-11-08 19:18:58 +0000763 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700764 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000765 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800766 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700767
768 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700769}
770
Chris Wilson78501ea2010-10-27 12:18:21 +0100771static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700772{
Chris Wilson78501ea2010-10-27 12:18:21 +0100773 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700774 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000775 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700776 int ret;
777
Eric Anholt62fdfea2010-05-21 13:26:39 -0700778 obj = i915_gem_alloc_object(dev, 4096);
779 if (obj == NULL) {
780 DRM_ERROR("Failed to allocate status page\n");
781 ret = -ENOMEM;
782 goto err;
783 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100784
785 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700786
Daniel Vetter75e9e912010-11-04 17:11:09 +0100787 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700788 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700789 goto err_unref;
790 }
791
Chris Wilson05394f32010-11-08 19:18:58 +0000792 ring->status_page.gfx_addr = obj->gtt_offset;
793 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800794 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700795 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700796 goto err_unpin;
797 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800798 ring->status_page.obj = obj;
799 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700800
Chris Wilson78501ea2010-10-27 12:18:21 +0100801 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800802 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
803 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700804
805 return 0;
806
807err_unpin:
808 i915_gem_object_unpin(obj);
809err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000810 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700811err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800812 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700813}
814
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800815int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100816 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700817{
Chris Wilson05394f32010-11-08 19:18:58 +0000818 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100819 int ret;
820
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800821 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100822 INIT_LIST_HEAD(&ring->active_list);
823 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100824 INIT_LIST_HEAD(&ring->gpu_write_list);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000825
Chris Wilsonb259f672011-03-29 13:19:09 +0100826 init_waitqueue_head(&ring->irq_queue);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000827 spin_lock_init(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000828 ring->irq_mask = ~0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700829
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800830 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100831 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800832 if (ret)
833 return ret;
834 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700835
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800836 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700837 if (obj == NULL) {
838 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800839 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100840 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700841 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700842
Chris Wilson05394f32010-11-08 19:18:58 +0000843 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800844
Daniel Vetter75e9e912010-11-04 17:11:09 +0100845 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100846 if (ret)
847 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700848
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800849 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000850 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700851 ring->map.type = 0;
852 ring->map.flags = 0;
853 ring->map.mtrr = 0;
854
855 drm_core_ioremap_wc(&ring->map, dev);
856 if (ring->map.handle == NULL) {
857 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800858 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100859 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700860 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800861
Eric Anholt62fdfea2010-05-21 13:26:39 -0700862 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100863 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100864 if (ret)
865 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700866
Chris Wilson55249ba2010-12-22 14:04:47 +0000867 /* Workaround an erratum on the i830 which causes a hang if
868 * the TAIL pointer points to within the last 2 cachelines
869 * of the buffer.
870 */
871 ring->effective_size = ring->size;
872 if (IS_I830(ring->dev))
873 ring->effective_size -= 128;
874
Chris Wilsonc584fe42010-10-29 18:15:52 +0100875 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100876
877err_unmap:
878 drm_core_ioremapfree(&ring->map, dev);
879err_unpin:
880 i915_gem_object_unpin(obj);
881err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000882 drm_gem_object_unreference(&obj->base);
883 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100884err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100885 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800886 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700887}
888
Chris Wilson78501ea2010-10-27 12:18:21 +0100889void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700890{
Chris Wilson33626e62010-10-29 16:18:36 +0100891 struct drm_i915_private *dev_priv;
892 int ret;
893
Chris Wilson05394f32010-11-08 19:18:58 +0000894 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700895 return;
896
Chris Wilson33626e62010-10-29 16:18:36 +0100897 /* Disable the ring buffer. The ring must be idle at this point */
898 dev_priv = ring->dev->dev_private;
Ben Widawsky96f298a2011-03-19 18:14:27 -0700899 ret = intel_wait_ring_idle(ring);
Chris Wilson29ee3992011-01-24 16:35:42 +0000900 if (ret)
901 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
902 ring->name, ret);
903
Chris Wilson33626e62010-10-29 16:18:36 +0100904 I915_WRITE_CTL(ring, 0);
905
Chris Wilson78501ea2010-10-27 12:18:21 +0100906 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700907
Chris Wilson05394f32010-11-08 19:18:58 +0000908 i915_gem_object_unpin(ring->obj);
909 drm_gem_object_unreference(&ring->obj->base);
910 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100911
Zou Nan hai8d192152010-11-02 16:31:01 +0800912 if (ring->cleanup)
913 ring->cleanup(ring);
914
Chris Wilson78501ea2010-10-27 12:18:21 +0100915 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700916}
917
Chris Wilson78501ea2010-10-27 12:18:21 +0100918static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700919{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800920 unsigned int *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +0000921 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700922
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800923 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100924 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700925 if (ret)
926 return ret;
927 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700928
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800929 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100930 rem /= 8;
931 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700932 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100933 *virt++ = MI_NOOP;
934 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700935
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800936 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000937 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700938
939 return 0;
940}
941
Chris Wilson78501ea2010-10-27 12:18:21 +0100942int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700943{
Chris Wilson78501ea2010-10-27 12:18:21 +0100944 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800945 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100946 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100947 u32 head;
948
Chris Wilsonc7dca472011-01-20 17:00:10 +0000949 /* If the reported head position has wrapped or hasn't advanced,
950 * fallback to the slow and accurate path.
951 */
952 head = intel_read_status_page(ring, 4);
953 if (head > ring->head) {
954 ring->head = head;
955 ring->space = ring_space(ring);
956 if (ring->space >= n)
957 return 0;
958 }
959
Chris Wilsondb53a302011-02-03 11:57:46 +0000960 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800961 end = jiffies + 3 * HZ;
962 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000963 ring->head = I915_READ_HEAD(ring);
964 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700965 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000966 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700967 return 0;
968 }
969
970 if (dev->primary->master) {
971 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
972 if (master_priv->sarea_priv)
973 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
974 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800975
Chris Wilsone60a0b12010-10-13 10:09:14 +0100976 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +0100977 if (atomic_read(&dev_priv->mm.wedged))
978 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800979 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +0000980 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700981 return -EBUSY;
982}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800983
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100984int intel_ring_begin(struct intel_ring_buffer *ring,
985 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800986{
Chris Wilson21dd3732011-01-26 15:55:56 +0000987 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +0800988 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100989 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100990
Chris Wilson21dd3732011-01-26 15:55:56 +0000991 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
992 return -EIO;
993
Chris Wilson55249ba2010-12-22 14:04:47 +0000994 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100995 ret = intel_wrap_ring_buffer(ring);
996 if (unlikely(ret))
997 return ret;
998 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100999
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001000 if (unlikely(ring->space < n)) {
1001 ret = intel_wait_ring_buffer(ring, n);
1002 if (unlikely(ret))
1003 return ret;
1004 }
Chris Wilsond97ed332010-08-04 15:18:13 +01001005
1006 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001007 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001008}
1009
Chris Wilson78501ea2010-10-27 12:18:21 +01001010void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001011{
Chris Wilsond97ed332010-08-04 15:18:13 +01001012 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +01001013 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001014}
1015
Chris Wilsone0708682010-09-19 14:46:27 +01001016static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001017 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +01001018 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001019 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001020 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001021 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001022 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001023 .flush = render_ring_flush,
1024 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001025 .get_seqno = ring_get_seqno,
1026 .irq_get = render_ring_get_irq,
1027 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001028 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Akshay Joshi0206e352011-08-16 15:34:10 -04001029 .cleanup = render_ring_cleanup,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001030};
Zou Nan haid1b851f2010-05-21 09:08:57 +08001031
1032/* ring buffer for bit-stream decoder */
1033
Chris Wilsone0708682010-09-19 14:46:27 +01001034static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +08001035 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +01001036 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001037 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001038 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +01001039 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +01001040 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001041 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +01001042 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001043 .get_seqno = ring_get_seqno,
1044 .irq_get = bsd_ring_get_irq,
1045 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001046 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001047};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001048
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001049
Chris Wilson78501ea2010-10-27 12:18:21 +01001050static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001051 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001052{
Akshay Joshi0206e352011-08-16 15:34:10 -04001053 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001054
1055 /* Every tail move must follow the sequence below */
Akshay Joshi0206e352011-08-16 15:34:10 -04001056 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1057 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1058 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1059 I915_WRITE(GEN6_BSD_RNCID, 0x0);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001060
Akshay Joshi0206e352011-08-16 15:34:10 -04001061 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1062 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1063 50))
1064 DRM_ERROR("timed out waiting for IDLE Indicator\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001065
Akshay Joshi0206e352011-08-16 15:34:10 -04001066 I915_WRITE_TAIL(ring, value);
1067 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1068 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1069 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001070}
1071
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001072static int gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001073 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001074{
Chris Wilson71a77e02011-02-02 12:13:49 +00001075 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001076 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001077
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001078 ret = intel_ring_begin(ring, 4);
1079 if (ret)
1080 return ret;
1081
Chris Wilson71a77e02011-02-02 12:13:49 +00001082 cmd = MI_FLUSH_DW;
1083 if (invalidate & I915_GEM_GPU_DOMAINS)
1084 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1085 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001086 intel_ring_emit(ring, 0);
1087 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001088 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001089 intel_ring_advance(ring);
1090 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001091}
1092
1093static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001094gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001095 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001096{
Akshay Joshi0206e352011-08-16 15:34:10 -04001097 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001098
Akshay Joshi0206e352011-08-16 15:34:10 -04001099 ret = intel_ring_begin(ring, 2);
1100 if (ret)
1101 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001102
Akshay Joshi0206e352011-08-16 15:34:10 -04001103 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1104 /* bit0-7 is the length on GEN6+ */
1105 intel_ring_emit(ring, offset);
1106 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001107
Akshay Joshi0206e352011-08-16 15:34:10 -04001108 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001109}
1110
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001111static bool
Chris Wilson0f468322011-01-04 17:35:21 +00001112gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
1113{
1114 return gen6_ring_get_irq(ring,
1115 GT_USER_INTERRUPT,
1116 GEN6_RENDER_USER_INTERRUPT);
1117}
1118
1119static void
1120gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
1121{
1122 return gen6_ring_put_irq(ring,
1123 GT_USER_INTERRUPT,
1124 GEN6_RENDER_USER_INTERRUPT);
1125}
1126
1127static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001128gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
1129{
Chris Wilson0f468322011-01-04 17:35:21 +00001130 return gen6_ring_get_irq(ring,
1131 GT_GEN6_BSD_USER_INTERRUPT,
1132 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001133}
1134
1135static void
1136gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
1137{
Chris Wilson0f468322011-01-04 17:35:21 +00001138 return gen6_ring_put_irq(ring,
1139 GT_GEN6_BSD_USER_INTERRUPT,
1140 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001141}
1142
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001143/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +01001144static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001145 .name = "gen6 bsd ring",
1146 .id = RING_BSD,
1147 .mmio_base = GEN6_BSD_RING_BASE,
1148 .size = 32 * PAGE_SIZE,
1149 .init = init_ring_common,
1150 .write_tail = gen6_bsd_ring_write_tail,
1151 .flush = gen6_ring_flush,
1152 .add_request = gen6_add_request,
1153 .get_seqno = ring_get_seqno,
1154 .irq_get = gen6_bsd_ring_get_irq,
1155 .irq_put = gen6_bsd_ring_put_irq,
1156 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +01001157};
1158
1159/* Blitter support (SandyBridge+) */
1160
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001161static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001162blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001163{
Chris Wilson0f468322011-01-04 17:35:21 +00001164 return gen6_ring_get_irq(ring,
1165 GT_BLT_USER_INTERRUPT,
1166 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001167}
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001168
Chris Wilson549f7362010-10-19 11:19:32 +01001169static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001170blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001171{
Chris Wilson0f468322011-01-04 17:35:21 +00001172 gen6_ring_put_irq(ring,
1173 GT_BLT_USER_INTERRUPT,
1174 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001175}
1176
Zou Nan hai8d192152010-11-02 16:31:01 +08001177
1178/* Workaround for some stepping of SNB,
1179 * each time when BLT engine ring tail moved,
1180 * the first command in the ring to be parsed
1181 * should be MI_BATCH_BUFFER_START
1182 */
1183#define NEED_BLT_WORKAROUND(dev) \
1184 (IS_GEN6(dev) && (dev->pdev->revision < 8))
1185
1186static inline struct drm_i915_gem_object *
1187to_blt_workaround(struct intel_ring_buffer *ring)
1188{
1189 return ring->private;
1190}
1191
1192static int blt_ring_init(struct intel_ring_buffer *ring)
1193{
1194 if (NEED_BLT_WORKAROUND(ring->dev)) {
1195 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +00001196 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +08001197 int ret;
1198
Chris Wilson05394f32010-11-08 19:18:58 +00001199 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +08001200 if (obj == NULL)
1201 return -ENOMEM;
1202
Chris Wilson05394f32010-11-08 19:18:58 +00001203 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +08001204 if (ret) {
1205 drm_gem_object_unreference(&obj->base);
1206 return ret;
1207 }
1208
1209 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +00001210 *ptr++ = MI_BATCH_BUFFER_END;
1211 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +08001212 kunmap(obj->pages[0]);
1213
Chris Wilson05394f32010-11-08 19:18:58 +00001214 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +08001215 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00001216 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +08001217 drm_gem_object_unreference(&obj->base);
1218 return ret;
1219 }
1220
1221 ring->private = obj;
1222 }
1223
1224 return init_ring_common(ring);
1225}
1226
1227static int blt_ring_begin(struct intel_ring_buffer *ring,
1228 int num_dwords)
1229{
1230 if (ring->private) {
1231 int ret = intel_ring_begin(ring, num_dwords+2);
1232 if (ret)
1233 return ret;
1234
1235 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
1236 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
1237
1238 return 0;
1239 } else
1240 return intel_ring_begin(ring, 4);
1241}
1242
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001243static int blt_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001244 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001245{
Chris Wilson71a77e02011-02-02 12:13:49 +00001246 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001247 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001248
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001249 ret = blt_ring_begin(ring, 4);
1250 if (ret)
1251 return ret;
1252
Chris Wilson71a77e02011-02-02 12:13:49 +00001253 cmd = MI_FLUSH_DW;
1254 if (invalidate & I915_GEM_DOMAIN_RENDER)
1255 cmd |= MI_INVALIDATE_TLB;
1256 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001257 intel_ring_emit(ring, 0);
1258 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001259 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001260 intel_ring_advance(ring);
1261 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001262}
1263
Zou Nan hai8d192152010-11-02 16:31:01 +08001264static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1265{
1266 if (!ring->private)
1267 return;
1268
1269 i915_gem_object_unpin(ring->private);
1270 drm_gem_object_unreference(ring->private);
1271 ring->private = NULL;
1272}
1273
Chris Wilson549f7362010-10-19 11:19:32 +01001274static const struct intel_ring_buffer gen6_blt_ring = {
Akshay Joshi0206e352011-08-16 15:34:10 -04001275 .name = "blt ring",
1276 .id = RING_BLT,
1277 .mmio_base = BLT_RING_BASE,
1278 .size = 32 * PAGE_SIZE,
1279 .init = blt_ring_init,
1280 .write_tail = ring_write_tail,
1281 .flush = blt_ring_flush,
1282 .add_request = gen6_add_request,
1283 .get_seqno = ring_get_seqno,
1284 .irq_get = blt_ring_get_irq,
1285 .irq_put = blt_ring_put_irq,
1286 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
1287 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001288};
1289
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001290int intel_init_render_ring_buffer(struct drm_device *dev)
1291{
1292 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001293 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001294
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001295 *ring = render_ring;
1296 if (INTEL_INFO(dev)->gen >= 6) {
1297 ring->add_request = gen6_add_request;
Chris Wilson0f468322011-01-04 17:35:21 +00001298 ring->irq_get = gen6_render_ring_get_irq;
1299 ring->irq_put = gen6_render_ring_put_irq;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001300 } else if (IS_GEN5(dev)) {
1301 ring->add_request = pc_render_add_request;
1302 ring->get_seqno = pc_render_get_seqno;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001303 }
1304
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001305 if (!I915_NEED_GFX_HWS(dev)) {
1306 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1307 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1308 }
1309
1310 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001311}
1312
Chris Wilsone8616b62011-01-20 09:57:11 +00001313int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1314{
1315 drm_i915_private_t *dev_priv = dev->dev_private;
1316 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1317
1318 *ring = render_ring;
1319 if (INTEL_INFO(dev)->gen >= 6) {
1320 ring->add_request = gen6_add_request;
1321 ring->irq_get = gen6_render_ring_get_irq;
1322 ring->irq_put = gen6_render_ring_put_irq;
1323 } else if (IS_GEN5(dev)) {
1324 ring->add_request = pc_render_add_request;
1325 ring->get_seqno = pc_render_get_seqno;
1326 }
1327
Keith Packardf3234702011-07-22 10:44:39 -07001328 if (!I915_NEED_GFX_HWS(dev))
1329 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1330
Chris Wilsone8616b62011-01-20 09:57:11 +00001331 ring->dev = dev;
1332 INIT_LIST_HEAD(&ring->active_list);
1333 INIT_LIST_HEAD(&ring->request_list);
1334 INIT_LIST_HEAD(&ring->gpu_write_list);
1335
1336 ring->size = size;
1337 ring->effective_size = ring->size;
1338 if (IS_I830(ring->dev))
1339 ring->effective_size -= 128;
1340
1341 ring->map.offset = start;
1342 ring->map.size = size;
1343 ring->map.type = 0;
1344 ring->map.flags = 0;
1345 ring->map.mtrr = 0;
1346
1347 drm_core_ioremap_wc(&ring->map, dev);
1348 if (ring->map.handle == NULL) {
1349 DRM_ERROR("can not ioremap virtual address for"
1350 " ring buffer\n");
1351 return -ENOMEM;
1352 }
1353
1354 ring->virtual_start = (void __force __iomem *)ring->map.handle;
1355 return 0;
1356}
1357
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001358int intel_init_bsd_ring_buffer(struct drm_device *dev)
1359{
1360 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001361 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001362
Jesse Barnes65d3eb12011-04-06 14:54:44 -07001363 if (IS_GEN6(dev) || IS_GEN7(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001364 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001365 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001366 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001367
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001368 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001369}
Chris Wilson549f7362010-10-19 11:19:32 +01001370
1371int intel_init_blt_ring_buffer(struct drm_device *dev)
1372{
1373 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001374 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001375
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001376 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001377
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001378 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001379}