blob: 7f188c9caf0d31ac434861e0a4afd9854f0cd654 [file] [log] [blame]
Eric Anholt62fdfea2010-05-21 13:26:39 -07001/*
2 * Copyright © 2008-2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
27 *
28 */
29
30#include "drmP.h"
31#include "drm.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070032#include "i915_drv.h"
Zou Nan hai8187a2b2010-05-21 09:08:55 +080033#include "i915_drm.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070034#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010035#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070036
Chris Wilsonc7dca472011-01-20 17:00:10 +000037static inline int ring_space(struct intel_ring_buffer *ring)
38{
39 int space = (ring->head & HEAD_ADDR) - (ring->tail + 8);
40 if (space < 0)
41 space += ring->size;
42 return space;
43}
44
Chris Wilson6f392d5482010-08-07 11:01:22 +010045static u32 i915_gem_get_seqno(struct drm_device *dev)
46{
47 drm_i915_private_t *dev_priv = dev->dev_private;
48 u32 seqno;
49
50 seqno = dev_priv->next_seqno;
51
52 /* reserve 0 for non-seqno */
53 if (++dev_priv->next_seqno == 0)
54 dev_priv->next_seqno = 1;
55
56 return seqno;
57}
58
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000059static int
Chris Wilson78501ea2010-10-27 12:18:21 +010060render_ring_flush(struct intel_ring_buffer *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +010061 u32 invalidate_domains,
62 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070063{
Chris Wilson78501ea2010-10-27 12:18:21 +010064 struct drm_device *dev = ring->dev;
Chris Wilson6f392d5482010-08-07 11:01:22 +010065 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000066 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +010067
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
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700318update_mboxes(struct intel_ring_buffer *ring,
319 u32 seqno,
320 u32 mmio_offset)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000321{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700322 intel_ring_emit(ring, MI_SEMAPHORE_MBOX |
323 MI_SEMAPHORE_GLOBAL_GTT |
324 MI_SEMAPHORE_REGISTER |
325 MI_SEMAPHORE_UPDATE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000326 intel_ring_emit(ring, seqno);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700327 intel_ring_emit(ring, mmio_offset);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000328}
329
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700330/**
331 * gen6_add_request - Update the semaphore mailbox registers
332 *
333 * @ring - ring that is adding a request
334 * @seqno - return seqno stuck into the ring
335 *
336 * Update the mailbox registers in the *other* rings with the current seqno.
337 * This acts like a signal in the canonical semaphore.
338 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000339static int
340gen6_add_request(struct intel_ring_buffer *ring,
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700341 u32 *seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000342{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700343 u32 mbox1_reg;
344 u32 mbox2_reg;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000345 int ret;
346
347 ret = intel_ring_begin(ring, 10);
348 if (ret)
349 return ret;
350
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700351 mbox1_reg = ring->signal_mbox[0];
352 mbox2_reg = ring->signal_mbox[1];
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000353
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700354 *seqno = i915_gem_get_seqno(ring->dev);
355
356 update_mboxes(ring, *seqno, mbox1_reg);
357 update_mboxes(ring, *seqno, mbox2_reg);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000358 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
359 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700360 intel_ring_emit(ring, *seqno);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000361 intel_ring_emit(ring, MI_USER_INTERRUPT);
362 intel_ring_advance(ring);
363
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000364 return 0;
365}
366
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700367/**
368 * intel_ring_sync - sync the waiter to the signaller on seqno
369 *
370 * @waiter - ring that is waiting
371 * @signaller - ring which has, or will signal
372 * @seqno - seqno which the waiter will block on
373 */
374static int
375intel_ring_sync(struct intel_ring_buffer *waiter,
376 struct intel_ring_buffer *signaller,
377 int ring,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000378 u32 seqno)
379{
380 int ret;
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700381 u32 dw1 = MI_SEMAPHORE_MBOX |
382 MI_SEMAPHORE_COMPARE |
383 MI_SEMAPHORE_REGISTER;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000384
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700385 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000386 if (ret)
387 return ret;
388
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700389 intel_ring_emit(waiter, dw1 | signaller->semaphore_register[ring]);
390 intel_ring_emit(waiter, seqno);
391 intel_ring_emit(waiter, 0);
392 intel_ring_emit(waiter, MI_NOOP);
393 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000394
395 return 0;
396}
397
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700398/* VCS->RCS (RVSYNC) or BCS->RCS (RBSYNC) */
399int
400render_ring_sync_to(struct intel_ring_buffer *waiter,
401 struct intel_ring_buffer *signaller,
402 u32 seqno)
403{
404 WARN_ON(signaller->semaphore_register[RCS] == MI_SEMAPHORE_SYNC_INVALID);
405 return intel_ring_sync(waiter,
406 signaller,
407 RCS,
408 seqno);
409}
410
411/* RCS->VCS (VRSYNC) or BCS->VCS (VBSYNC) */
412int
413gen6_bsd_ring_sync_to(struct intel_ring_buffer *waiter,
414 struct intel_ring_buffer *signaller,
415 u32 seqno)
416{
417 WARN_ON(signaller->semaphore_register[VCS] == MI_SEMAPHORE_SYNC_INVALID);
418 return intel_ring_sync(waiter,
419 signaller,
420 VCS,
421 seqno);
422}
423
424/* RCS->BCS (BRSYNC) or VCS->BCS (BVSYNC) */
425int
426gen6_blt_ring_sync_to(struct intel_ring_buffer *waiter,
427 struct intel_ring_buffer *signaller,
428 u32 seqno)
429{
430 WARN_ON(signaller->semaphore_register[BCS] == MI_SEMAPHORE_SYNC_INVALID);
431 return intel_ring_sync(waiter,
432 signaller,
433 BCS,
434 seqno);
435}
436
437
438
Chris Wilsonc6df5412010-12-15 09:56:50 +0000439#define PIPE_CONTROL_FLUSH(ring__, addr__) \
440do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200441 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
442 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000443 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
444 intel_ring_emit(ring__, 0); \
445 intel_ring_emit(ring__, 0); \
446} while (0)
447
448static int
449pc_render_add_request(struct intel_ring_buffer *ring,
450 u32 *result)
451{
452 struct drm_device *dev = ring->dev;
453 u32 seqno = i915_gem_get_seqno(dev);
454 struct pipe_control *pc = ring->private;
455 u32 scratch_addr = pc->gtt_offset + 128;
456 int ret;
457
458 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
459 * incoherent with writes to memory, i.e. completely fubar,
460 * so we need to use PIPE_NOTIFY instead.
461 *
462 * However, we also need to workaround the qword write
463 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
464 * memory before requesting an interrupt.
465 */
466 ret = intel_ring_begin(ring, 32);
467 if (ret)
468 return ret;
469
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200470 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000471 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
472 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
473 intel_ring_emit(ring, seqno);
474 intel_ring_emit(ring, 0);
475 PIPE_CONTROL_FLUSH(ring, scratch_addr);
476 scratch_addr += 128; /* write to separate cachelines */
477 PIPE_CONTROL_FLUSH(ring, scratch_addr);
478 scratch_addr += 128;
479 PIPE_CONTROL_FLUSH(ring, scratch_addr);
480 scratch_addr += 128;
481 PIPE_CONTROL_FLUSH(ring, scratch_addr);
482 scratch_addr += 128;
483 PIPE_CONTROL_FLUSH(ring, scratch_addr);
484 scratch_addr += 128;
485 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200486 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000487 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
488 PIPE_CONTROL_NOTIFY);
489 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
490 intel_ring_emit(ring, seqno);
491 intel_ring_emit(ring, 0);
492 intel_ring_advance(ring);
493
494 *result = seqno;
495 return 0;
496}
497
Chris Wilson3cce4692010-10-27 16:11:02 +0100498static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100499render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100500 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700501{
Chris Wilson78501ea2010-10-27 12:18:21 +0100502 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100503 u32 seqno = i915_gem_get_seqno(dev);
504 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800505
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000506 ret = intel_ring_begin(ring, 4);
507 if (ret)
508 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100509
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000510 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
511 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
512 intel_ring_emit(ring, seqno);
513 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100514 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000515
Chris Wilson3cce4692010-10-27 16:11:02 +0100516 *result = seqno;
517 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700518}
519
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800520static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000521ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800522{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000523 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
524}
525
Chris Wilsonc6df5412010-12-15 09:56:50 +0000526static u32
527pc_render_get_seqno(struct intel_ring_buffer *ring)
528{
529 struct pipe_control *pc = ring->private;
530 return pc->cpu_page[0];
531}
532
Chris Wilson0f468322011-01-04 17:35:21 +0000533static void
534ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
535{
536 dev_priv->gt_irq_mask &= ~mask;
537 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
538 POSTING_READ(GTIMR);
539}
540
541static void
542ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
543{
544 dev_priv->gt_irq_mask |= mask;
545 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
546 POSTING_READ(GTIMR);
547}
548
549static void
550i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
551{
552 dev_priv->irq_mask &= ~mask;
553 I915_WRITE(IMR, dev_priv->irq_mask);
554 POSTING_READ(IMR);
555}
556
557static void
558i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
559{
560 dev_priv->irq_mask |= mask;
561 I915_WRITE(IMR, dev_priv->irq_mask);
562 POSTING_READ(IMR);
563}
564
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000565static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000566render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700567{
Chris Wilson78501ea2010-10-27 12:18:21 +0100568 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000569 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700570
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000571 if (!dev->irq_enabled)
572 return false;
573
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000574 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000575 if (ring->irq_refcount++ == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700576 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000577 ironlake_enable_irq(dev_priv,
578 GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700579 else
580 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
581 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000582 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000583
584 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700585}
586
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800587static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000588render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700589{
Chris Wilson78501ea2010-10-27 12:18:21 +0100590 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000591 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700592
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000593 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000594 if (--ring->irq_refcount == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700595 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000596 ironlake_disable_irq(dev_priv,
597 GT_USER_INTERRUPT |
598 GT_PIPE_NOTIFY);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700599 else
600 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
601 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000602 spin_unlock(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700603}
604
Chris Wilson78501ea2010-10-27 12:18:21 +0100605void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800606{
Eric Anholt45930102011-05-06 17:12:35 -0700607 struct drm_device *dev = ring->dev;
Chris Wilson78501ea2010-10-27 12:18:21 +0100608 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700609 u32 mmio = 0;
610
611 /* The ring status page addresses are no longer next to the rest of
612 * the ring registers as of gen7.
613 */
614 if (IS_GEN7(dev)) {
615 switch (ring->id) {
616 case RING_RENDER:
617 mmio = RENDER_HWS_PGA_GEN7;
618 break;
619 case RING_BLT:
620 mmio = BLT_HWS_PGA_GEN7;
621 break;
622 case RING_BSD:
623 mmio = BSD_HWS_PGA_GEN7;
624 break;
625 }
626 } else if (IS_GEN6(ring->dev)) {
627 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
628 } else {
629 mmio = RING_HWS_PGA(ring->mmio_base);
630 }
631
Chris Wilson78501ea2010-10-27 12:18:21 +0100632 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
633 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800634}
635
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000636static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100637bsd_ring_flush(struct intel_ring_buffer *ring,
638 u32 invalidate_domains,
639 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800640{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000641 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000642
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000643 ret = intel_ring_begin(ring, 2);
644 if (ret)
645 return ret;
646
647 intel_ring_emit(ring, MI_FLUSH);
648 intel_ring_emit(ring, MI_NOOP);
649 intel_ring_advance(ring);
650 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800651}
652
Chris Wilson3cce4692010-10-27 16:11:02 +0100653static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100654ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100655 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800656{
657 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100658 int ret;
659
660 ret = intel_ring_begin(ring, 4);
661 if (ret)
662 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100663
Chris Wilson78501ea2010-10-27 12:18:21 +0100664 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d5482010-08-07 11:01:22 +0100665
Chris Wilson3cce4692010-10-27 16:11:02 +0100666 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
667 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
668 intel_ring_emit(ring, seqno);
669 intel_ring_emit(ring, MI_USER_INTERRUPT);
670 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800671
Chris Wilson3cce4692010-10-27 16:11:02 +0100672 *result = seqno;
673 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800674}
675
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000676static bool
Chris Wilson0f468322011-01-04 17:35:21 +0000677gen6_ring_get_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
678{
679 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000680 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000681
682 if (!dev->irq_enabled)
683 return false;
684
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000685 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000686 if (ring->irq_refcount++ == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000687 ring->irq_mask &= ~rflag;
688 I915_WRITE_IMR(ring, ring->irq_mask);
689 ironlake_enable_irq(dev_priv, gflag);
Chris Wilson0f468322011-01-04 17:35:21 +0000690 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000691 spin_unlock(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000692
693 return true;
694}
695
696static void
697gen6_ring_put_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
698{
699 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000700 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000701
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000702 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000703 if (--ring->irq_refcount == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000704 ring->irq_mask |= rflag;
705 I915_WRITE_IMR(ring, ring->irq_mask);
706 ironlake_disable_irq(dev_priv, gflag);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000707 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000708 spin_unlock(&ring->irq_lock);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000709}
710
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000711static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000712bsd_ring_get_irq(struct intel_ring_buffer *ring)
713{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800714 struct drm_device *dev = ring->dev;
715 drm_i915_private_t *dev_priv = dev->dev_private;
716
717 if (!dev->irq_enabled)
718 return false;
719
720 spin_lock(&ring->irq_lock);
721 if (ring->irq_refcount++ == 0) {
722 if (IS_G4X(dev))
723 i915_enable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
724 else
725 ironlake_enable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
726 }
727 spin_unlock(&ring->irq_lock);
728
729 return true;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000730}
731static void
732bsd_ring_put_irq(struct intel_ring_buffer *ring)
733{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800734 struct drm_device *dev = ring->dev;
735 drm_i915_private_t *dev_priv = dev->dev_private;
736
737 spin_lock(&ring->irq_lock);
738 if (--ring->irq_refcount == 0) {
739 if (IS_G4X(dev))
740 i915_disable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
741 else
742 ironlake_disable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
743 }
744 spin_unlock(&ring->irq_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800745}
746
747static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000748ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800749{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100750 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100751
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100752 ret = intel_ring_begin(ring, 2);
753 if (ret)
754 return ret;
755
Chris Wilson78501ea2010-10-27 12:18:21 +0100756 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000757 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100758 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000759 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100760 intel_ring_advance(ring);
761
Zou Nan haid1b851f2010-05-21 09:08:57 +0800762 return 0;
763}
764
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800765static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100766render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000767 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700768{
Chris Wilson78501ea2010-10-27 12:18:21 +0100769 struct drm_device *dev = ring->dev;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000770 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700771
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000772 if (IS_I830(dev) || IS_845G(dev)) {
773 ret = intel_ring_begin(ring, 4);
774 if (ret)
775 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700776
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000777 intel_ring_emit(ring, MI_BATCH_BUFFER);
778 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
779 intel_ring_emit(ring, offset + len - 8);
780 intel_ring_emit(ring, 0);
781 } else {
782 ret = intel_ring_begin(ring, 2);
783 if (ret)
784 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100785
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000786 if (INTEL_INFO(dev)->gen >= 4) {
787 intel_ring_emit(ring,
788 MI_BATCH_BUFFER_START | (2 << 6) |
789 MI_BATCH_NON_SECURE_I965);
790 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700791 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000792 intel_ring_emit(ring,
793 MI_BATCH_BUFFER_START | (2 << 6));
794 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700795 }
796 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000797 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700798
Eric Anholt62fdfea2010-05-21 13:26:39 -0700799 return 0;
800}
801
Chris Wilson78501ea2010-10-27 12:18:21 +0100802static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700803{
Chris Wilson78501ea2010-10-27 12:18:21 +0100804 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000805 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700806
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800807 obj = ring->status_page.obj;
808 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700809 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700810
Chris Wilson05394f32010-11-08 19:18:58 +0000811 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700812 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000813 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800814 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700815
816 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700817}
818
Chris Wilson78501ea2010-10-27 12:18:21 +0100819static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700820{
Chris Wilson78501ea2010-10-27 12:18:21 +0100821 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700822 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000823 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700824 int ret;
825
Eric Anholt62fdfea2010-05-21 13:26:39 -0700826 obj = i915_gem_alloc_object(dev, 4096);
827 if (obj == NULL) {
828 DRM_ERROR("Failed to allocate status page\n");
829 ret = -ENOMEM;
830 goto err;
831 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100832
833 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700834
Daniel Vetter75e9e912010-11-04 17:11:09 +0100835 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700836 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700837 goto err_unref;
838 }
839
Chris Wilson05394f32010-11-08 19:18:58 +0000840 ring->status_page.gfx_addr = obj->gtt_offset;
841 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800842 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700843 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700844 goto err_unpin;
845 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800846 ring->status_page.obj = obj;
847 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700848
Chris Wilson78501ea2010-10-27 12:18:21 +0100849 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800850 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
851 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700852
853 return 0;
854
855err_unpin:
856 i915_gem_object_unpin(obj);
857err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000858 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700859err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800860 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700861}
862
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800863int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100864 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700865{
Chris Wilson05394f32010-11-08 19:18:58 +0000866 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100867 int ret;
868
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800869 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100870 INIT_LIST_HEAD(&ring->active_list);
871 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100872 INIT_LIST_HEAD(&ring->gpu_write_list);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000873
Chris Wilsonb259f672011-03-29 13:19:09 +0100874 init_waitqueue_head(&ring->irq_queue);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000875 spin_lock_init(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000876 ring->irq_mask = ~0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700877
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800878 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100879 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800880 if (ret)
881 return ret;
882 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700883
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800884 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700885 if (obj == NULL) {
886 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800887 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100888 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700889 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700890
Chris Wilson05394f32010-11-08 19:18:58 +0000891 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800892
Daniel Vetter75e9e912010-11-04 17:11:09 +0100893 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100894 if (ret)
895 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700896
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800897 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000898 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700899 ring->map.type = 0;
900 ring->map.flags = 0;
901 ring->map.mtrr = 0;
902
903 drm_core_ioremap_wc(&ring->map, dev);
904 if (ring->map.handle == NULL) {
905 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800906 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100907 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700908 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800909
Eric Anholt62fdfea2010-05-21 13:26:39 -0700910 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100911 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100912 if (ret)
913 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700914
Chris Wilson55249ba2010-12-22 14:04:47 +0000915 /* Workaround an erratum on the i830 which causes a hang if
916 * the TAIL pointer points to within the last 2 cachelines
917 * of the buffer.
918 */
919 ring->effective_size = ring->size;
920 if (IS_I830(ring->dev))
921 ring->effective_size -= 128;
922
Chris Wilsonc584fe42010-10-29 18:15:52 +0100923 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100924
925err_unmap:
926 drm_core_ioremapfree(&ring->map, dev);
927err_unpin:
928 i915_gem_object_unpin(obj);
929err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000930 drm_gem_object_unreference(&obj->base);
931 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100932err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100933 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800934 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700935}
936
Chris Wilson78501ea2010-10-27 12:18:21 +0100937void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700938{
Chris Wilson33626e62010-10-29 16:18:36 +0100939 struct drm_i915_private *dev_priv;
940 int ret;
941
Chris Wilson05394f32010-11-08 19:18:58 +0000942 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700943 return;
944
Chris Wilson33626e62010-10-29 16:18:36 +0100945 /* Disable the ring buffer. The ring must be idle at this point */
946 dev_priv = ring->dev->dev_private;
Ben Widawsky96f298a2011-03-19 18:14:27 -0700947 ret = intel_wait_ring_idle(ring);
Chris Wilson29ee3992011-01-24 16:35:42 +0000948 if (ret)
949 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
950 ring->name, ret);
951
Chris Wilson33626e62010-10-29 16:18:36 +0100952 I915_WRITE_CTL(ring, 0);
953
Chris Wilson78501ea2010-10-27 12:18:21 +0100954 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700955
Chris Wilson05394f32010-11-08 19:18:58 +0000956 i915_gem_object_unpin(ring->obj);
957 drm_gem_object_unreference(&ring->obj->base);
958 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100959
Zou Nan hai8d192152010-11-02 16:31:01 +0800960 if (ring->cleanup)
961 ring->cleanup(ring);
962
Chris Wilson78501ea2010-10-27 12:18:21 +0100963 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700964}
965
Chris Wilson78501ea2010-10-27 12:18:21 +0100966static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700967{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800968 unsigned int *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +0000969 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700970
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800971 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100972 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700973 if (ret)
974 return ret;
975 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700976
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800977 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100978 rem /= 8;
979 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700980 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100981 *virt++ = MI_NOOP;
982 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700983
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800984 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000985 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700986
987 return 0;
988}
989
Chris Wilson78501ea2010-10-27 12:18:21 +0100990int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700991{
Chris Wilson78501ea2010-10-27 12:18:21 +0100992 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800993 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100994 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100995 u32 head;
996
Chris Wilsonc7dca472011-01-20 17:00:10 +0000997 /* If the reported head position has wrapped or hasn't advanced,
998 * fallback to the slow and accurate path.
999 */
1000 head = intel_read_status_page(ring, 4);
1001 if (head > ring->head) {
1002 ring->head = head;
1003 ring->space = ring_space(ring);
1004 if (ring->space >= n)
1005 return 0;
1006 }
1007
Chris Wilsondb53a302011-02-03 11:57:46 +00001008 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001009 end = jiffies + 3 * HZ;
1010 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001011 ring->head = I915_READ_HEAD(ring);
1012 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001013 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001014 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001015 return 0;
1016 }
1017
1018 if (dev->primary->master) {
1019 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1020 if (master_priv->sarea_priv)
1021 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1022 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001023
Chris Wilsone60a0b12010-10-13 10:09:14 +01001024 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +01001025 if (atomic_read(&dev_priv->mm.wedged))
1026 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001027 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +00001028 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001029 return -EBUSY;
1030}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001031
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001032int intel_ring_begin(struct intel_ring_buffer *ring,
1033 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001034{
Chris Wilson21dd3732011-01-26 15:55:56 +00001035 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +08001036 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001037 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001038
Chris Wilson21dd3732011-01-26 15:55:56 +00001039 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
1040 return -EIO;
1041
Chris Wilson55249ba2010-12-22 14:04:47 +00001042 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001043 ret = intel_wrap_ring_buffer(ring);
1044 if (unlikely(ret))
1045 return ret;
1046 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001047
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001048 if (unlikely(ring->space < n)) {
1049 ret = intel_wait_ring_buffer(ring, n);
1050 if (unlikely(ret))
1051 return ret;
1052 }
Chris Wilsond97ed332010-08-04 15:18:13 +01001053
1054 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001055 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001056}
1057
Chris Wilson78501ea2010-10-27 12:18:21 +01001058void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001059{
Chris Wilsond97ed332010-08-04 15:18:13 +01001060 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +01001061 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001062}
1063
Chris Wilsone0708682010-09-19 14:46:27 +01001064static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001065 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +01001066 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001067 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001068 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001069 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001070 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001071 .flush = render_ring_flush,
1072 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001073 .get_seqno = ring_get_seqno,
1074 .irq_get = render_ring_get_irq,
1075 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001076 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Akshay Joshi0206e352011-08-16 15:34:10 -04001077 .cleanup = render_ring_cleanup,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001078 .sync_to = render_ring_sync_to,
1079 .semaphore_register = {MI_SEMAPHORE_SYNC_INVALID,
1080 MI_SEMAPHORE_SYNC_RV,
1081 MI_SEMAPHORE_SYNC_RB},
1082 .signal_mbox = {GEN6_VRSYNC, GEN6_BRSYNC},
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001083};
Zou Nan haid1b851f2010-05-21 09:08:57 +08001084
1085/* ring buffer for bit-stream decoder */
1086
Chris Wilsone0708682010-09-19 14:46:27 +01001087static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +08001088 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +01001089 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001090 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001091 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +01001092 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +01001093 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001094 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +01001095 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001096 .get_seqno = ring_get_seqno,
1097 .irq_get = bsd_ring_get_irq,
1098 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001099 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001100};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001101
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001102
Chris Wilson78501ea2010-10-27 12:18:21 +01001103static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001104 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001105{
Akshay Joshi0206e352011-08-16 15:34:10 -04001106 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001107
1108 /* Every tail move must follow the sequence below */
Akshay Joshi0206e352011-08-16 15:34:10 -04001109 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1110 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1111 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1112 I915_WRITE(GEN6_BSD_RNCID, 0x0);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001113
Akshay Joshi0206e352011-08-16 15:34:10 -04001114 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1115 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1116 50))
1117 DRM_ERROR("timed out waiting for IDLE Indicator\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001118
Akshay Joshi0206e352011-08-16 15:34:10 -04001119 I915_WRITE_TAIL(ring, value);
1120 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1121 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1122 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001123}
1124
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001125static int gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001126 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001127{
Chris Wilson71a77e02011-02-02 12:13:49 +00001128 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001129 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001130
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001131 ret = intel_ring_begin(ring, 4);
1132 if (ret)
1133 return ret;
1134
Chris Wilson71a77e02011-02-02 12:13:49 +00001135 cmd = MI_FLUSH_DW;
1136 if (invalidate & I915_GEM_GPU_DOMAINS)
1137 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1138 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001139 intel_ring_emit(ring, 0);
1140 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001141 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001142 intel_ring_advance(ring);
1143 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001144}
1145
1146static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001147gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001148 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001149{
Akshay Joshi0206e352011-08-16 15:34:10 -04001150 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001151
Akshay Joshi0206e352011-08-16 15:34:10 -04001152 ret = intel_ring_begin(ring, 2);
1153 if (ret)
1154 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001155
Akshay Joshi0206e352011-08-16 15:34:10 -04001156 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1157 /* bit0-7 is the length on GEN6+ */
1158 intel_ring_emit(ring, offset);
1159 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001160
Akshay Joshi0206e352011-08-16 15:34:10 -04001161 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001162}
1163
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001164static bool
Chris Wilson0f468322011-01-04 17:35:21 +00001165gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
1166{
1167 return gen6_ring_get_irq(ring,
1168 GT_USER_INTERRUPT,
1169 GEN6_RENDER_USER_INTERRUPT);
1170}
1171
1172static void
1173gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
1174{
1175 return gen6_ring_put_irq(ring,
1176 GT_USER_INTERRUPT,
1177 GEN6_RENDER_USER_INTERRUPT);
1178}
1179
1180static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001181gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
1182{
Chris Wilson0f468322011-01-04 17:35:21 +00001183 return gen6_ring_get_irq(ring,
1184 GT_GEN6_BSD_USER_INTERRUPT,
1185 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001186}
1187
1188static void
1189gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
1190{
Chris Wilson0f468322011-01-04 17:35:21 +00001191 return gen6_ring_put_irq(ring,
1192 GT_GEN6_BSD_USER_INTERRUPT,
1193 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001194}
1195
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001196/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +01001197static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001198 .name = "gen6 bsd ring",
1199 .id = RING_BSD,
1200 .mmio_base = GEN6_BSD_RING_BASE,
1201 .size = 32 * PAGE_SIZE,
1202 .init = init_ring_common,
1203 .write_tail = gen6_bsd_ring_write_tail,
1204 .flush = gen6_ring_flush,
1205 .add_request = gen6_add_request,
1206 .get_seqno = ring_get_seqno,
1207 .irq_get = gen6_bsd_ring_get_irq,
1208 .irq_put = gen6_bsd_ring_put_irq,
1209 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001210 .sync_to = gen6_bsd_ring_sync_to,
1211 .semaphore_register = {MI_SEMAPHORE_SYNC_VR,
1212 MI_SEMAPHORE_SYNC_INVALID,
1213 MI_SEMAPHORE_SYNC_VB},
1214 .signal_mbox = {GEN6_RVSYNC, GEN6_BVSYNC},
Chris Wilson549f7362010-10-19 11:19:32 +01001215};
1216
1217/* Blitter support (SandyBridge+) */
1218
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001219static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001220blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001221{
Chris Wilson0f468322011-01-04 17:35:21 +00001222 return gen6_ring_get_irq(ring,
1223 GT_BLT_USER_INTERRUPT,
1224 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001225}
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001226
Chris Wilson549f7362010-10-19 11:19:32 +01001227static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001228blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001229{
Chris Wilson0f468322011-01-04 17:35:21 +00001230 gen6_ring_put_irq(ring,
1231 GT_BLT_USER_INTERRUPT,
1232 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001233}
1234
Zou Nan hai8d192152010-11-02 16:31:01 +08001235
1236/* Workaround for some stepping of SNB,
1237 * each time when BLT engine ring tail moved,
1238 * the first command in the ring to be parsed
1239 * should be MI_BATCH_BUFFER_START
1240 */
1241#define NEED_BLT_WORKAROUND(dev) \
1242 (IS_GEN6(dev) && (dev->pdev->revision < 8))
1243
1244static inline struct drm_i915_gem_object *
1245to_blt_workaround(struct intel_ring_buffer *ring)
1246{
1247 return ring->private;
1248}
1249
1250static int blt_ring_init(struct intel_ring_buffer *ring)
1251{
1252 if (NEED_BLT_WORKAROUND(ring->dev)) {
1253 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +00001254 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +08001255 int ret;
1256
Chris Wilson05394f32010-11-08 19:18:58 +00001257 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +08001258 if (obj == NULL)
1259 return -ENOMEM;
1260
Chris Wilson05394f32010-11-08 19:18:58 +00001261 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +08001262 if (ret) {
1263 drm_gem_object_unreference(&obj->base);
1264 return ret;
1265 }
1266
1267 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +00001268 *ptr++ = MI_BATCH_BUFFER_END;
1269 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +08001270 kunmap(obj->pages[0]);
1271
Chris Wilson05394f32010-11-08 19:18:58 +00001272 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +08001273 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00001274 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +08001275 drm_gem_object_unreference(&obj->base);
1276 return ret;
1277 }
1278
1279 ring->private = obj;
1280 }
1281
1282 return init_ring_common(ring);
1283}
1284
1285static int blt_ring_begin(struct intel_ring_buffer *ring,
1286 int num_dwords)
1287{
1288 if (ring->private) {
1289 int ret = intel_ring_begin(ring, num_dwords+2);
1290 if (ret)
1291 return ret;
1292
1293 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
1294 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
1295
1296 return 0;
1297 } else
1298 return intel_ring_begin(ring, 4);
1299}
1300
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001301static int blt_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001302 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001303{
Chris Wilson71a77e02011-02-02 12:13:49 +00001304 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001305 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001306
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001307 ret = blt_ring_begin(ring, 4);
1308 if (ret)
1309 return ret;
1310
Chris Wilson71a77e02011-02-02 12:13:49 +00001311 cmd = MI_FLUSH_DW;
1312 if (invalidate & I915_GEM_DOMAIN_RENDER)
1313 cmd |= MI_INVALIDATE_TLB;
1314 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001315 intel_ring_emit(ring, 0);
1316 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001317 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001318 intel_ring_advance(ring);
1319 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001320}
1321
Zou Nan hai8d192152010-11-02 16:31:01 +08001322static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1323{
1324 if (!ring->private)
1325 return;
1326
1327 i915_gem_object_unpin(ring->private);
1328 drm_gem_object_unreference(ring->private);
1329 ring->private = NULL;
1330}
1331
Chris Wilson549f7362010-10-19 11:19:32 +01001332static const struct intel_ring_buffer gen6_blt_ring = {
Akshay Joshi0206e352011-08-16 15:34:10 -04001333 .name = "blt ring",
1334 .id = RING_BLT,
1335 .mmio_base = BLT_RING_BASE,
1336 .size = 32 * PAGE_SIZE,
1337 .init = blt_ring_init,
1338 .write_tail = ring_write_tail,
1339 .flush = blt_ring_flush,
1340 .add_request = gen6_add_request,
1341 .get_seqno = ring_get_seqno,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001342 .irq_get = blt_ring_get_irq,
1343 .irq_put = blt_ring_put_irq,
Akshay Joshi0206e352011-08-16 15:34:10 -04001344 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001345 .cleanup = blt_ring_cleanup,
1346 .sync_to = gen6_blt_ring_sync_to,
1347 .semaphore_register = {MI_SEMAPHORE_SYNC_BR,
1348 MI_SEMAPHORE_SYNC_BV,
1349 MI_SEMAPHORE_SYNC_INVALID},
1350 .signal_mbox = {GEN6_RBSYNC, GEN6_VBSYNC},
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001351};
1352
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001353int intel_init_render_ring_buffer(struct drm_device *dev)
1354{
1355 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001356 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001357
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001358 *ring = render_ring;
1359 if (INTEL_INFO(dev)->gen >= 6) {
1360 ring->add_request = gen6_add_request;
Chris Wilson0f468322011-01-04 17:35:21 +00001361 ring->irq_get = gen6_render_ring_get_irq;
1362 ring->irq_put = gen6_render_ring_put_irq;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001363 } else if (IS_GEN5(dev)) {
1364 ring->add_request = pc_render_add_request;
1365 ring->get_seqno = pc_render_get_seqno;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001366 }
1367
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001368 if (!I915_NEED_GFX_HWS(dev)) {
1369 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1370 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1371 }
1372
1373 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001374}
1375
Chris Wilsone8616b62011-01-20 09:57:11 +00001376int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1377{
1378 drm_i915_private_t *dev_priv = dev->dev_private;
1379 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1380
1381 *ring = render_ring;
1382 if (INTEL_INFO(dev)->gen >= 6) {
1383 ring->add_request = gen6_add_request;
1384 ring->irq_get = gen6_render_ring_get_irq;
1385 ring->irq_put = gen6_render_ring_put_irq;
1386 } else if (IS_GEN5(dev)) {
1387 ring->add_request = pc_render_add_request;
1388 ring->get_seqno = pc_render_get_seqno;
1389 }
1390
Keith Packardf3234702011-07-22 10:44:39 -07001391 if (!I915_NEED_GFX_HWS(dev))
1392 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1393
Chris Wilsone8616b62011-01-20 09:57:11 +00001394 ring->dev = dev;
1395 INIT_LIST_HEAD(&ring->active_list);
1396 INIT_LIST_HEAD(&ring->request_list);
1397 INIT_LIST_HEAD(&ring->gpu_write_list);
1398
1399 ring->size = size;
1400 ring->effective_size = ring->size;
1401 if (IS_I830(ring->dev))
1402 ring->effective_size -= 128;
1403
1404 ring->map.offset = start;
1405 ring->map.size = size;
1406 ring->map.type = 0;
1407 ring->map.flags = 0;
1408 ring->map.mtrr = 0;
1409
1410 drm_core_ioremap_wc(&ring->map, dev);
1411 if (ring->map.handle == NULL) {
1412 DRM_ERROR("can not ioremap virtual address for"
1413 " ring buffer\n");
1414 return -ENOMEM;
1415 }
1416
1417 ring->virtual_start = (void __force __iomem *)ring->map.handle;
1418 return 0;
1419}
1420
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001421int intel_init_bsd_ring_buffer(struct drm_device *dev)
1422{
1423 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001424 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001425
Jesse Barnes65d3eb12011-04-06 14:54:44 -07001426 if (IS_GEN6(dev) || IS_GEN7(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001427 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001428 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001429 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001430
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001431 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001432}
Chris Wilson549f7362010-10-19 11:19:32 +01001433
1434int intel_init_blt_ring_buffer(struct drm_device *dev)
1435{
1436 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001437 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001438
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001439 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001440
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001441 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001442}