blob: 69cdbcd6e5b7c75247d5037f1f8a769c2a5d557b [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 Wilson93dfb402011-03-29 16:59:50 -0700239 obj->cache_level = I915_CACHE_LLC;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000240
241 ret = i915_gem_object_pin(obj, 4096, true);
242 if (ret)
243 goto err_unref;
244
245 pc->gtt_offset = obj->gtt_offset;
246 pc->cpu_page = kmap(obj->pages[0]);
247 if (pc->cpu_page == NULL)
248 goto err_unpin;
249
250 pc->obj = obj;
251 ring->private = pc;
252 return 0;
253
254err_unpin:
255 i915_gem_object_unpin(obj);
256err_unref:
257 drm_gem_object_unreference(&obj->base);
258err:
259 kfree(pc);
260 return ret;
261}
262
263static void
264cleanup_pipe_control(struct intel_ring_buffer *ring)
265{
266 struct pipe_control *pc = ring->private;
267 struct drm_i915_gem_object *obj;
268
269 if (!ring->private)
270 return;
271
272 obj = pc->obj;
273 kunmap(obj->pages[0]);
274 i915_gem_object_unpin(obj);
275 drm_gem_object_unreference(&obj->base);
276
277 kfree(pc);
278 ring->private = NULL;
279}
280
Chris Wilson78501ea2010-10-27 12:18:21 +0100281static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800282{
Chris Wilson78501ea2010-10-27 12:18:21 +0100283 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000284 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100285 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800286
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100287 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100288 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Jesse Barnes65d3eb12011-04-06 14:54:44 -0700289 if (IS_GEN6(dev) || IS_GEN7(dev))
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800290 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
291 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800292 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100293
Chris Wilsonc6df5412010-12-15 09:56:50 +0000294 if (INTEL_INFO(dev)->gen >= 6) {
295 } else if (IS_GEN5(dev)) {
296 ret = init_pipe_control(ring);
297 if (ret)
298 return ret;
299 }
300
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800301 return ret;
302}
303
Chris Wilsonc6df5412010-12-15 09:56:50 +0000304static void render_ring_cleanup(struct intel_ring_buffer *ring)
305{
306 if (!ring->private)
307 return;
308
309 cleanup_pipe_control(ring);
310}
311
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000312static void
313update_semaphore(struct intel_ring_buffer *ring, int i, u32 seqno)
314{
315 struct drm_device *dev = ring->dev;
316 struct drm_i915_private *dev_priv = dev->dev_private;
317 int id;
318
319 /*
320 * cs -> 1 = vcs, 0 = bcs
321 * vcs -> 1 = bcs, 0 = cs,
322 * bcs -> 1 = cs, 0 = vcs.
323 */
324 id = ring - dev_priv->ring;
325 id += 2 - i;
326 id %= 3;
327
328 intel_ring_emit(ring,
329 MI_SEMAPHORE_MBOX |
330 MI_SEMAPHORE_REGISTER |
331 MI_SEMAPHORE_UPDATE);
332 intel_ring_emit(ring, seqno);
333 intel_ring_emit(ring,
334 RING_SYNC_0(dev_priv->ring[id].mmio_base) + 4*i);
335}
336
337static int
338gen6_add_request(struct intel_ring_buffer *ring,
339 u32 *result)
340{
341 u32 seqno;
342 int ret;
343
344 ret = intel_ring_begin(ring, 10);
345 if (ret)
346 return ret;
347
348 seqno = i915_gem_get_seqno(ring->dev);
349 update_semaphore(ring, 0, seqno);
350 update_semaphore(ring, 1, seqno);
351
352 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
353 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
354 intel_ring_emit(ring, seqno);
355 intel_ring_emit(ring, MI_USER_INTERRUPT);
356 intel_ring_advance(ring);
357
358 *result = seqno;
359 return 0;
360}
361
362int
363intel_ring_sync(struct intel_ring_buffer *ring,
364 struct intel_ring_buffer *to,
365 u32 seqno)
366{
367 int ret;
368
369 ret = intel_ring_begin(ring, 4);
370 if (ret)
371 return ret;
372
373 intel_ring_emit(ring,
374 MI_SEMAPHORE_MBOX |
375 MI_SEMAPHORE_REGISTER |
376 intel_ring_sync_index(ring, to) << 17 |
377 MI_SEMAPHORE_COMPARE);
378 intel_ring_emit(ring, seqno);
379 intel_ring_emit(ring, 0);
380 intel_ring_emit(ring, MI_NOOP);
381 intel_ring_advance(ring);
382
383 return 0;
384}
385
Chris Wilsonc6df5412010-12-15 09:56:50 +0000386#define PIPE_CONTROL_FLUSH(ring__, addr__) \
387do { \
388 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
389 PIPE_CONTROL_DEPTH_STALL | 2); \
390 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
391 intel_ring_emit(ring__, 0); \
392 intel_ring_emit(ring__, 0); \
393} while (0)
394
395static int
396pc_render_add_request(struct intel_ring_buffer *ring,
397 u32 *result)
398{
399 struct drm_device *dev = ring->dev;
400 u32 seqno = i915_gem_get_seqno(dev);
401 struct pipe_control *pc = ring->private;
402 u32 scratch_addr = pc->gtt_offset + 128;
403 int ret;
404
405 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
406 * incoherent with writes to memory, i.e. completely fubar,
407 * so we need to use PIPE_NOTIFY instead.
408 *
409 * However, we also need to workaround the qword write
410 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
411 * memory before requesting an interrupt.
412 */
413 ret = intel_ring_begin(ring, 32);
414 if (ret)
415 return ret;
416
417 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
418 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
419 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
420 intel_ring_emit(ring, seqno);
421 intel_ring_emit(ring, 0);
422 PIPE_CONTROL_FLUSH(ring, scratch_addr);
423 scratch_addr += 128; /* write to separate cachelines */
424 PIPE_CONTROL_FLUSH(ring, scratch_addr);
425 scratch_addr += 128;
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 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
434 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
435 PIPE_CONTROL_NOTIFY);
436 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
437 intel_ring_emit(ring, seqno);
438 intel_ring_emit(ring, 0);
439 intel_ring_advance(ring);
440
441 *result = seqno;
442 return 0;
443}
444
Chris Wilson3cce4692010-10-27 16:11:02 +0100445static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100446render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100447 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700448{
Chris Wilson78501ea2010-10-27 12:18:21 +0100449 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100450 u32 seqno = i915_gem_get_seqno(dev);
451 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800452
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000453 ret = intel_ring_begin(ring, 4);
454 if (ret)
455 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100456
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000457 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
458 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
459 intel_ring_emit(ring, seqno);
460 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100461 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000462
Chris Wilson3cce4692010-10-27 16:11:02 +0100463 *result = seqno;
464 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700465}
466
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800467static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000468ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800469{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000470 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
471}
472
Chris Wilsonc6df5412010-12-15 09:56:50 +0000473static u32
474pc_render_get_seqno(struct intel_ring_buffer *ring)
475{
476 struct pipe_control *pc = ring->private;
477 return pc->cpu_page[0];
478}
479
Chris Wilson0f46832f2011-01-04 17:35:21 +0000480static void
481ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
482{
483 dev_priv->gt_irq_mask &= ~mask;
484 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
485 POSTING_READ(GTIMR);
486}
487
488static void
489ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
490{
491 dev_priv->gt_irq_mask |= mask;
492 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
493 POSTING_READ(GTIMR);
494}
495
496static void
497i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
498{
499 dev_priv->irq_mask &= ~mask;
500 I915_WRITE(IMR, dev_priv->irq_mask);
501 POSTING_READ(IMR);
502}
503
504static void
505i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
506{
507 dev_priv->irq_mask |= mask;
508 I915_WRITE(IMR, dev_priv->irq_mask);
509 POSTING_READ(IMR);
510}
511
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000512static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000513render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700514{
Chris Wilson78501ea2010-10-27 12:18:21 +0100515 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000516 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700517
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000518 if (!dev->irq_enabled)
519 return false;
520
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000521 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000522 if (ring->irq_refcount++ == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700523 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f46832f2011-01-04 17:35:21 +0000524 ironlake_enable_irq(dev_priv,
525 GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700526 else
527 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
528 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000529 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000530
531 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700532}
533
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800534static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000535render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700536{
Chris Wilson78501ea2010-10-27 12:18:21 +0100537 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000538 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700539
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000540 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000541 if (--ring->irq_refcount == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700542 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f46832f2011-01-04 17:35:21 +0000543 ironlake_disable_irq(dev_priv,
544 GT_USER_INTERRUPT |
545 GT_PIPE_NOTIFY);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700546 else
547 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
548 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000549 spin_unlock(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700550}
551
Chris Wilson78501ea2010-10-27 12:18:21 +0100552void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800553{
Eric Anholt45930102011-05-06 17:12:35 -0700554 struct drm_device *dev = ring->dev;
Chris Wilson78501ea2010-10-27 12:18:21 +0100555 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700556 u32 mmio = 0;
557
558 /* The ring status page addresses are no longer next to the rest of
559 * the ring registers as of gen7.
560 */
561 if (IS_GEN7(dev)) {
562 switch (ring->id) {
563 case RING_RENDER:
564 mmio = RENDER_HWS_PGA_GEN7;
565 break;
566 case RING_BLT:
567 mmio = BLT_HWS_PGA_GEN7;
568 break;
569 case RING_BSD:
570 mmio = BSD_HWS_PGA_GEN7;
571 break;
572 }
573 } else if (IS_GEN6(ring->dev)) {
574 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
575 } else {
576 mmio = RING_HWS_PGA(ring->mmio_base);
577 }
578
Chris Wilson78501ea2010-10-27 12:18:21 +0100579 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
580 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800581}
582
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000583static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100584bsd_ring_flush(struct intel_ring_buffer *ring,
585 u32 invalidate_domains,
586 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800587{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000588 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000589
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000590 ret = intel_ring_begin(ring, 2);
591 if (ret)
592 return ret;
593
594 intel_ring_emit(ring, MI_FLUSH);
595 intel_ring_emit(ring, MI_NOOP);
596 intel_ring_advance(ring);
597 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800598}
599
Chris Wilson3cce4692010-10-27 16:11:02 +0100600static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100601ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100602 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800603{
604 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100605 int ret;
606
607 ret = intel_ring_begin(ring, 4);
608 if (ret)
609 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100610
Chris Wilson78501ea2010-10-27 12:18:21 +0100611 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d52010-08-07 11:01:22 +0100612
Chris Wilson3cce4692010-10-27 16:11:02 +0100613 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
614 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
615 intel_ring_emit(ring, seqno);
616 intel_ring_emit(ring, MI_USER_INTERRUPT);
617 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800618
Chris Wilson3cce4692010-10-27 16:11:02 +0100619 *result = seqno;
620 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800621}
622
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000623static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000624ring_get_irq(struct intel_ring_buffer *ring, u32 flag)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800625{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000626 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000627 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000628
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000629 if (!dev->irq_enabled)
630 return false;
631
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000632 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000633 if (ring->irq_refcount++ == 0)
Chris Wilson0f46832f2011-01-04 17:35:21 +0000634 ironlake_enable_irq(dev_priv, flag);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000635 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000636
637 return true;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800638}
639
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000640static void
641ring_put_irq(struct intel_ring_buffer *ring, u32 flag)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800642{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000643 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000644 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000645
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000646 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000647 if (--ring->irq_refcount == 0)
Chris Wilson0f46832f2011-01-04 17:35:21 +0000648 ironlake_disable_irq(dev_priv, flag);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000649 spin_unlock(&ring->irq_lock);
Chris Wilson0f46832f2011-01-04 17:35:21 +0000650}
651
652static bool
653gen6_ring_get_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
654{
655 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000656 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f46832f2011-01-04 17:35:21 +0000657
658 if (!dev->irq_enabled)
659 return false;
660
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000661 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000662 if (ring->irq_refcount++ == 0) {
Chris Wilson0f46832f2011-01-04 17:35:21 +0000663 ring->irq_mask &= ~rflag;
664 I915_WRITE_IMR(ring, ring->irq_mask);
665 ironlake_enable_irq(dev_priv, gflag);
Chris Wilson0f46832f2011-01-04 17:35:21 +0000666 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000667 spin_unlock(&ring->irq_lock);
Chris Wilson0f46832f2011-01-04 17:35:21 +0000668
669 return true;
670}
671
672static void
673gen6_ring_put_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
674{
675 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000676 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f46832f2011-01-04 17:35:21 +0000677
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000678 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000679 if (--ring->irq_refcount == 0) {
Chris Wilson0f46832f2011-01-04 17:35:21 +0000680 ring->irq_mask |= rflag;
681 I915_WRITE_IMR(ring, ring->irq_mask);
682 ironlake_disable_irq(dev_priv, gflag);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000683 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000684 spin_unlock(&ring->irq_lock);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000685}
686
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000687static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000688bsd_ring_get_irq(struct intel_ring_buffer *ring)
689{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800690 struct drm_device *dev = ring->dev;
691 drm_i915_private_t *dev_priv = dev->dev_private;
692
693 if (!dev->irq_enabled)
694 return false;
695
696 spin_lock(&ring->irq_lock);
697 if (ring->irq_refcount++ == 0) {
698 if (IS_G4X(dev))
699 i915_enable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
700 else
701 ironlake_enable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
702 }
703 spin_unlock(&ring->irq_lock);
704
705 return true;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000706}
707static void
708bsd_ring_put_irq(struct intel_ring_buffer *ring)
709{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800710 struct drm_device *dev = ring->dev;
711 drm_i915_private_t *dev_priv = dev->dev_private;
712
713 spin_lock(&ring->irq_lock);
714 if (--ring->irq_refcount == 0) {
715 if (IS_G4X(dev))
716 i915_disable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
717 else
718 ironlake_disable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
719 }
720 spin_unlock(&ring->irq_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800721}
722
723static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000724ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800725{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100726 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100727
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100728 ret = intel_ring_begin(ring, 2);
729 if (ret)
730 return ret;
731
Chris Wilson78501ea2010-10-27 12:18:21 +0100732 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000733 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100734 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000735 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100736 intel_ring_advance(ring);
737
Zou Nan haid1b851f2010-05-21 09:08:57 +0800738 return 0;
739}
740
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800741static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100742render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000743 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700744{
Chris Wilson78501ea2010-10-27 12:18:21 +0100745 struct drm_device *dev = ring->dev;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000746 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700747
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000748 if (IS_I830(dev) || IS_845G(dev)) {
749 ret = intel_ring_begin(ring, 4);
750 if (ret)
751 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700752
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000753 intel_ring_emit(ring, MI_BATCH_BUFFER);
754 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
755 intel_ring_emit(ring, offset + len - 8);
756 intel_ring_emit(ring, 0);
757 } else {
758 ret = intel_ring_begin(ring, 2);
759 if (ret)
760 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100761
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000762 if (INTEL_INFO(dev)->gen >= 4) {
763 intel_ring_emit(ring,
764 MI_BATCH_BUFFER_START | (2 << 6) |
765 MI_BATCH_NON_SECURE_I965);
766 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700767 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000768 intel_ring_emit(ring,
769 MI_BATCH_BUFFER_START | (2 << 6));
770 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700771 }
772 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000773 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700774
Eric Anholt62fdfea2010-05-21 13:26:39 -0700775 return 0;
776}
777
Chris Wilson78501ea2010-10-27 12:18:21 +0100778static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700779{
Chris Wilson78501ea2010-10-27 12:18:21 +0100780 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000781 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700782
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800783 obj = ring->status_page.obj;
784 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700785 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700786
Chris Wilson05394f32010-11-08 19:18:58 +0000787 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700788 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000789 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800790 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700791
792 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700793}
794
Chris Wilson78501ea2010-10-27 12:18:21 +0100795static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700796{
Chris Wilson78501ea2010-10-27 12:18:21 +0100797 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700798 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000799 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700800 int ret;
801
Eric Anholt62fdfea2010-05-21 13:26:39 -0700802 obj = i915_gem_alloc_object(dev, 4096);
803 if (obj == NULL) {
804 DRM_ERROR("Failed to allocate status page\n");
805 ret = -ENOMEM;
806 goto err;
807 }
Chris Wilson93dfb402011-03-29 16:59:50 -0700808 obj->cache_level = I915_CACHE_LLC;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700809
Daniel Vetter75e9e912010-11-04 17:11:09 +0100810 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700811 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700812 goto err_unref;
813 }
814
Chris Wilson05394f32010-11-08 19:18:58 +0000815 ring->status_page.gfx_addr = obj->gtt_offset;
816 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800817 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700818 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700819 goto err_unpin;
820 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800821 ring->status_page.obj = obj;
822 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700823
Chris Wilson78501ea2010-10-27 12:18:21 +0100824 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800825 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
826 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700827
828 return 0;
829
830err_unpin:
831 i915_gem_object_unpin(obj);
832err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000833 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700834err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800835 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700836}
837
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800838int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100839 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700840{
Chris Wilson05394f32010-11-08 19:18:58 +0000841 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100842 int ret;
843
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800844 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100845 INIT_LIST_HEAD(&ring->active_list);
846 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100847 INIT_LIST_HEAD(&ring->gpu_write_list);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000848
Chris Wilsonb259f672011-03-29 13:19:09 +0100849 init_waitqueue_head(&ring->irq_queue);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000850 spin_lock_init(&ring->irq_lock);
Chris Wilson0f46832f2011-01-04 17:35:21 +0000851 ring->irq_mask = ~0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700852
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800853 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100854 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800855 if (ret)
856 return ret;
857 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700858
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800859 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700860 if (obj == NULL) {
861 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800862 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100863 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700864 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700865
Chris Wilson05394f32010-11-08 19:18:58 +0000866 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800867
Daniel Vetter75e9e912010-11-04 17:11:09 +0100868 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100869 if (ret)
870 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700871
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800872 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000873 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700874 ring->map.type = 0;
875 ring->map.flags = 0;
876 ring->map.mtrr = 0;
877
878 drm_core_ioremap_wc(&ring->map, dev);
879 if (ring->map.handle == NULL) {
880 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800881 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100882 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700883 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800884
Eric Anholt62fdfea2010-05-21 13:26:39 -0700885 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100886 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100887 if (ret)
888 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700889
Chris Wilson55249ba2010-12-22 14:04:47 +0000890 /* Workaround an erratum on the i830 which causes a hang if
891 * the TAIL pointer points to within the last 2 cachelines
892 * of the buffer.
893 */
894 ring->effective_size = ring->size;
895 if (IS_I830(ring->dev))
896 ring->effective_size -= 128;
897
Chris Wilsonc584fe42010-10-29 18:15:52 +0100898 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100899
900err_unmap:
901 drm_core_ioremapfree(&ring->map, dev);
902err_unpin:
903 i915_gem_object_unpin(obj);
904err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000905 drm_gem_object_unreference(&obj->base);
906 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100907err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100908 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800909 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700910}
911
Chris Wilson78501ea2010-10-27 12:18:21 +0100912void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700913{
Chris Wilson33626e62010-10-29 16:18:36 +0100914 struct drm_i915_private *dev_priv;
915 int ret;
916
Chris Wilson05394f32010-11-08 19:18:58 +0000917 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700918 return;
919
Chris Wilson33626e62010-10-29 16:18:36 +0100920 /* Disable the ring buffer. The ring must be idle at this point */
921 dev_priv = ring->dev->dev_private;
Ben Widawsky96f298a2011-03-19 18:14:27 -0700922 ret = intel_wait_ring_idle(ring);
Chris Wilson29ee3992011-01-24 16:35:42 +0000923 if (ret)
924 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
925 ring->name, ret);
926
Chris Wilson33626e62010-10-29 16:18:36 +0100927 I915_WRITE_CTL(ring, 0);
928
Chris Wilson78501ea2010-10-27 12:18:21 +0100929 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700930
Chris Wilson05394f32010-11-08 19:18:58 +0000931 i915_gem_object_unpin(ring->obj);
932 drm_gem_object_unreference(&ring->obj->base);
933 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100934
Zou Nan hai8d192152010-11-02 16:31:01 +0800935 if (ring->cleanup)
936 ring->cleanup(ring);
937
Chris Wilson78501ea2010-10-27 12:18:21 +0100938 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700939}
940
Chris Wilson78501ea2010-10-27 12:18:21 +0100941static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700942{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800943 unsigned int *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +0000944 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700945
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800946 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100947 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700948 if (ret)
949 return ret;
950 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700951
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800952 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100953 rem /= 8;
954 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700955 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100956 *virt++ = MI_NOOP;
957 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700958
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800959 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000960 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700961
962 return 0;
963}
964
Chris Wilson78501ea2010-10-27 12:18:21 +0100965int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700966{
Chris Wilson78501ea2010-10-27 12:18:21 +0100967 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800968 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100969 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100970 u32 head;
971
Chris Wilsonc7dca472011-01-20 17:00:10 +0000972 /* If the reported head position has wrapped or hasn't advanced,
973 * fallback to the slow and accurate path.
974 */
975 head = intel_read_status_page(ring, 4);
976 if (head > ring->head) {
977 ring->head = head;
978 ring->space = ring_space(ring);
979 if (ring->space >= n)
980 return 0;
981 }
982
Chris Wilsondb53a302011-02-03 11:57:46 +0000983 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800984 end = jiffies + 3 * HZ;
985 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000986 ring->head = I915_READ_HEAD(ring);
987 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700988 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +0000989 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700990 return 0;
991 }
992
993 if (dev->primary->master) {
994 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
995 if (master_priv->sarea_priv)
996 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
997 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800998
Chris Wilsone60a0b12010-10-13 10:09:14 +0100999 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +01001000 if (atomic_read(&dev_priv->mm.wedged))
1001 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001002 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +00001003 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001004 return -EBUSY;
1005}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001006
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001007int intel_ring_begin(struct intel_ring_buffer *ring,
1008 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001009{
Chris Wilson21dd3732011-01-26 15:55:56 +00001010 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +08001011 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001012 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001013
Chris Wilson21dd3732011-01-26 15:55:56 +00001014 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
1015 return -EIO;
1016
Chris Wilson55249ba2010-12-22 14:04:47 +00001017 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001018 ret = intel_wrap_ring_buffer(ring);
1019 if (unlikely(ret))
1020 return ret;
1021 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001022
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001023 if (unlikely(ring->space < n)) {
1024 ret = intel_wait_ring_buffer(ring, n);
1025 if (unlikely(ret))
1026 return ret;
1027 }
Chris Wilsond97ed332010-08-04 15:18:13 +01001028
1029 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001030 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001031}
1032
Chris Wilson78501ea2010-10-27 12:18:21 +01001033void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001034{
Chris Wilsond97ed332010-08-04 15:18:13 +01001035 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +01001036 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001037}
1038
Chris Wilsone0708682010-09-19 14:46:27 +01001039static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001040 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +01001041 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001042 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001043 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001044 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001045 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001046 .flush = render_ring_flush,
1047 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001048 .get_seqno = ring_get_seqno,
1049 .irq_get = render_ring_get_irq,
1050 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001051 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Chris Wilsonc6df5412010-12-15 09:56:50 +00001052 .cleanup = render_ring_cleanup,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001053};
Zou Nan haid1b851f2010-05-21 09:08:57 +08001054
1055/* ring buffer for bit-stream decoder */
1056
Chris Wilsone0708682010-09-19 14:46:27 +01001057static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +08001058 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +01001059 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001060 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001061 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +01001062 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +01001063 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001064 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +01001065 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001066 .get_seqno = ring_get_seqno,
1067 .irq_get = bsd_ring_get_irq,
1068 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001069 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001070};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001071
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001072
Chris Wilson78501ea2010-10-27 12:18:21 +01001073static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001074 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001075{
Chris Wilson78501ea2010-10-27 12:18:21 +01001076 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001077
1078 /* Every tail move must follow the sequence below */
1079 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1080 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1081 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1082 I915_WRITE(GEN6_BSD_RNCID, 0x0);
1083
1084 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1085 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1086 50))
1087 DRM_ERROR("timed out waiting for IDLE Indicator\n");
1088
Daniel Vetter870e86d2010-08-02 16:29:44 +02001089 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001090 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1091 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1092 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
1093}
1094
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001095static int gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001096 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001097{
Chris Wilson71a77e02011-02-02 12:13:49 +00001098 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001099 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001100
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001101 ret = intel_ring_begin(ring, 4);
1102 if (ret)
1103 return ret;
1104
Chris Wilson71a77e02011-02-02 12:13:49 +00001105 cmd = MI_FLUSH_DW;
1106 if (invalidate & I915_GEM_GPU_DOMAINS)
1107 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1108 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001109 intel_ring_emit(ring, 0);
1110 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001111 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001112 intel_ring_advance(ring);
1113 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001114}
1115
1116static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001117gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001118 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001119{
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001120 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001121
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001122 ret = intel_ring_begin(ring, 2);
1123 if (ret)
1124 return ret;
1125
Chris Wilson78501ea2010-10-27 12:18:21 +01001126 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001127 /* bit0-7 is the length on GEN6+ */
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001128 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001129 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001130
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001131 return 0;
1132}
1133
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001134static bool
Chris Wilson0f46832f2011-01-04 17:35:21 +00001135gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
1136{
1137 return gen6_ring_get_irq(ring,
1138 GT_USER_INTERRUPT,
1139 GEN6_RENDER_USER_INTERRUPT);
1140}
1141
1142static void
1143gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
1144{
1145 return gen6_ring_put_irq(ring,
1146 GT_USER_INTERRUPT,
1147 GEN6_RENDER_USER_INTERRUPT);
1148}
1149
1150static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001151gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
1152{
Chris Wilson0f46832f2011-01-04 17:35:21 +00001153 return gen6_ring_get_irq(ring,
1154 GT_GEN6_BSD_USER_INTERRUPT,
1155 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001156}
1157
1158static void
1159gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
1160{
Chris Wilson0f46832f2011-01-04 17:35:21 +00001161 return gen6_ring_put_irq(ring,
1162 GT_GEN6_BSD_USER_INTERRUPT,
1163 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001164}
1165
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001166/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +01001167static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001168 .name = "gen6 bsd ring",
1169 .id = RING_BSD,
1170 .mmio_base = GEN6_BSD_RING_BASE,
1171 .size = 32 * PAGE_SIZE,
1172 .init = init_ring_common,
1173 .write_tail = gen6_bsd_ring_write_tail,
1174 .flush = gen6_ring_flush,
1175 .add_request = gen6_add_request,
1176 .get_seqno = ring_get_seqno,
1177 .irq_get = gen6_bsd_ring_get_irq,
1178 .irq_put = gen6_bsd_ring_put_irq,
1179 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +01001180};
1181
1182/* Blitter support (SandyBridge+) */
1183
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001184static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001185blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001186{
Chris Wilson0f46832f2011-01-04 17:35:21 +00001187 return gen6_ring_get_irq(ring,
1188 GT_BLT_USER_INTERRUPT,
1189 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001190}
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001191
Chris Wilson549f7362010-10-19 11:19:32 +01001192static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001193blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001194{
Chris Wilson0f46832f2011-01-04 17:35:21 +00001195 gen6_ring_put_irq(ring,
1196 GT_BLT_USER_INTERRUPT,
1197 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001198}
1199
Zou Nan hai8d192152010-11-02 16:31:01 +08001200
1201/* Workaround for some stepping of SNB,
1202 * each time when BLT engine ring tail moved,
1203 * the first command in the ring to be parsed
1204 * should be MI_BATCH_BUFFER_START
1205 */
1206#define NEED_BLT_WORKAROUND(dev) \
1207 (IS_GEN6(dev) && (dev->pdev->revision < 8))
1208
1209static inline struct drm_i915_gem_object *
1210to_blt_workaround(struct intel_ring_buffer *ring)
1211{
1212 return ring->private;
1213}
1214
1215static int blt_ring_init(struct intel_ring_buffer *ring)
1216{
1217 if (NEED_BLT_WORKAROUND(ring->dev)) {
1218 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +00001219 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +08001220 int ret;
1221
Chris Wilson05394f32010-11-08 19:18:58 +00001222 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +08001223 if (obj == NULL)
1224 return -ENOMEM;
1225
Chris Wilson05394f32010-11-08 19:18:58 +00001226 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +08001227 if (ret) {
1228 drm_gem_object_unreference(&obj->base);
1229 return ret;
1230 }
1231
1232 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +00001233 *ptr++ = MI_BATCH_BUFFER_END;
1234 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +08001235 kunmap(obj->pages[0]);
1236
Chris Wilson05394f32010-11-08 19:18:58 +00001237 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +08001238 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00001239 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +08001240 drm_gem_object_unreference(&obj->base);
1241 return ret;
1242 }
1243
1244 ring->private = obj;
1245 }
1246
1247 return init_ring_common(ring);
1248}
1249
1250static int blt_ring_begin(struct intel_ring_buffer *ring,
1251 int num_dwords)
1252{
1253 if (ring->private) {
1254 int ret = intel_ring_begin(ring, num_dwords+2);
1255 if (ret)
1256 return ret;
1257
1258 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
1259 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
1260
1261 return 0;
1262 } else
1263 return intel_ring_begin(ring, 4);
1264}
1265
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001266static int blt_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001267 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001268{
Chris Wilson71a77e02011-02-02 12:13:49 +00001269 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001270 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001271
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001272 ret = blt_ring_begin(ring, 4);
1273 if (ret)
1274 return ret;
1275
Chris Wilson71a77e02011-02-02 12:13:49 +00001276 cmd = MI_FLUSH_DW;
1277 if (invalidate & I915_GEM_DOMAIN_RENDER)
1278 cmd |= MI_INVALIDATE_TLB;
1279 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001280 intel_ring_emit(ring, 0);
1281 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001282 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001283 intel_ring_advance(ring);
1284 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001285}
1286
Zou Nan hai8d192152010-11-02 16:31:01 +08001287static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1288{
1289 if (!ring->private)
1290 return;
1291
1292 i915_gem_object_unpin(ring->private);
1293 drm_gem_object_unreference(ring->private);
1294 ring->private = NULL;
1295}
1296
Chris Wilson549f7362010-10-19 11:19:32 +01001297static const struct intel_ring_buffer gen6_blt_ring = {
1298 .name = "blt ring",
1299 .id = RING_BLT,
1300 .mmio_base = BLT_RING_BASE,
1301 .size = 32 * PAGE_SIZE,
Zou Nan hai8d192152010-11-02 16:31:01 +08001302 .init = blt_ring_init,
Chris Wilson297b0c52010-10-22 17:02:41 +01001303 .write_tail = ring_write_tail,
Zou Nan hai8d192152010-11-02 16:31:01 +08001304 .flush = blt_ring_flush,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001305 .add_request = gen6_add_request,
1306 .get_seqno = ring_get_seqno,
1307 .irq_get = blt_ring_get_irq,
1308 .irq_put = blt_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001309 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Zou Nan hai8d192152010-11-02 16:31:01 +08001310 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001311};
1312
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001313int intel_init_render_ring_buffer(struct drm_device *dev)
1314{
1315 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001316 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001317
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001318 *ring = render_ring;
1319 if (INTEL_INFO(dev)->gen >= 6) {
1320 ring->add_request = gen6_add_request;
Chris Wilson0f46832f2011-01-04 17:35:21 +00001321 ring->irq_get = gen6_render_ring_get_irq;
1322 ring->irq_put = gen6_render_ring_put_irq;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001323 } else if (IS_GEN5(dev)) {
1324 ring->add_request = pc_render_add_request;
1325 ring->get_seqno = pc_render_get_seqno;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001326 }
1327
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001328 if (!I915_NEED_GFX_HWS(dev)) {
1329 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1330 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1331 }
1332
1333 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001334}
1335
Chris Wilsone8616b62011-01-20 09:57:11 +00001336int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1337{
1338 drm_i915_private_t *dev_priv = dev->dev_private;
1339 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1340
1341 *ring = render_ring;
1342 if (INTEL_INFO(dev)->gen >= 6) {
1343 ring->add_request = gen6_add_request;
1344 ring->irq_get = gen6_render_ring_get_irq;
1345 ring->irq_put = gen6_render_ring_put_irq;
1346 } else if (IS_GEN5(dev)) {
1347 ring->add_request = pc_render_add_request;
1348 ring->get_seqno = pc_render_get_seqno;
1349 }
1350
1351 ring->dev = dev;
1352 INIT_LIST_HEAD(&ring->active_list);
1353 INIT_LIST_HEAD(&ring->request_list);
1354 INIT_LIST_HEAD(&ring->gpu_write_list);
1355
1356 ring->size = size;
1357 ring->effective_size = ring->size;
1358 if (IS_I830(ring->dev))
1359 ring->effective_size -= 128;
1360
1361 ring->map.offset = start;
1362 ring->map.size = size;
1363 ring->map.type = 0;
1364 ring->map.flags = 0;
1365 ring->map.mtrr = 0;
1366
1367 drm_core_ioremap_wc(&ring->map, dev);
1368 if (ring->map.handle == NULL) {
1369 DRM_ERROR("can not ioremap virtual address for"
1370 " ring buffer\n");
1371 return -ENOMEM;
1372 }
1373
1374 ring->virtual_start = (void __force __iomem *)ring->map.handle;
1375 return 0;
1376}
1377
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001378int intel_init_bsd_ring_buffer(struct drm_device *dev)
1379{
1380 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001381 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001382
Jesse Barnes65d3eb12011-04-06 14:54:44 -07001383 if (IS_GEN6(dev) || IS_GEN7(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001384 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001385 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001386 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001387
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001388 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001389}
Chris Wilson549f7362010-10-19 11:19:32 +01001390
1391int intel_init_blt_ring_buffer(struct drm_device *dev)
1392{
1393 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001394 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001395
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001396 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001397
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001398 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001399}