blob: 0ee78525959a31ab279ca46f07b344b69ea1213b [file] [log] [blame]
Eric Anholt62fdfea2010-05-21 13:26:39 -07001/*
2 * Copyright © 2008-2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
27 *
28 */
29
30#include "drmP.h"
31#include "drm.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070032#include "i915_drv.h"
Zou Nan hai8187a2b2010-05-21 09:08:55 +080033#include "i915_drm.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070034#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010035#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070036
Chris Wilson6f392d52010-08-07 11:01:22 +010037static u32 i915_gem_get_seqno(struct drm_device *dev)
38{
39 drm_i915_private_t *dev_priv = dev->dev_private;
40 u32 seqno;
41
42 seqno = dev_priv->next_seqno;
43
44 /* reserve 0 for non-seqno */
45 if (++dev_priv->next_seqno == 0)
46 dev_priv->next_seqno = 1;
47
48 return seqno;
49}
50
Zou Nan hai8187a2b2010-05-21 09:08:55 +080051static void
Chris Wilson78501ea2010-10-27 12:18:21 +010052render_ring_flush(struct intel_ring_buffer *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +010053 u32 invalidate_domains,
54 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070055{
Chris Wilson78501ea2010-10-27 12:18:21 +010056 struct drm_device *dev = ring->dev;
Chris Wilson6f392d52010-08-07 11:01:22 +010057 drm_i915_private_t *dev_priv = dev->dev_private;
58 u32 cmd;
59
Eric Anholt62fdfea2010-05-21 13:26:39 -070060#if WATCH_EXEC
61 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
62 invalidate_domains, flush_domains);
63#endif
Chris Wilson6f392d52010-08-07 11:01:22 +010064
65 trace_i915_gem_request_flush(dev, dev_priv->next_seqno,
Eric Anholt62fdfea2010-05-21 13:26:39 -070066 invalidate_domains, flush_domains);
67
Eric Anholt62fdfea2010-05-21 13:26:39 -070068 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
69 /*
70 * read/write caches:
71 *
72 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
73 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
74 * also flushed at 2d versus 3d pipeline switches.
75 *
76 * read-only caches:
77 *
78 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
79 * MI_READ_FLUSH is set, and is always flushed on 965.
80 *
81 * I915_GEM_DOMAIN_COMMAND may not exist?
82 *
83 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
84 * invalidated when MI_EXE_FLUSH is set.
85 *
86 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
87 * invalidated with every MI_FLUSH.
88 *
89 * TLBs:
90 *
91 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
92 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
93 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
94 * are flushed at any MI_FLUSH.
95 */
96
97 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
98 if ((invalidate_domains|flush_domains) &
99 I915_GEM_DOMAIN_RENDER)
100 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100101 if (INTEL_INFO(dev)->gen < 4) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700102 /*
103 * On the 965, the sampler cache always gets flushed
104 * and this bit is reserved.
105 */
106 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
107 cmd |= MI_READ_FLUSH;
108 }
109 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
110 cmd |= MI_EXE_FLUSH;
111
Chris Wilson70eac332010-11-30 14:07:47 +0000112 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
113 (IS_G4X(dev) || IS_GEN5(dev)))
114 cmd |= MI_INVALIDATE_ISP;
115
Eric Anholt62fdfea2010-05-21 13:26:39 -0700116#if WATCH_EXEC
117 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
118#endif
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100119 if (intel_ring_begin(ring, 2) == 0) {
120 intel_ring_emit(ring, cmd);
121 intel_ring_emit(ring, MI_NOOP);
122 intel_ring_advance(ring);
123 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800124 }
125}
126
Chris Wilson78501ea2010-10-27 12:18:21 +0100127static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100128 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800129{
Chris Wilson78501ea2010-10-27 12:18:21 +0100130 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100131 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800132}
133
Chris Wilson78501ea2010-10-27 12:18:21 +0100134u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800135{
Chris Wilson78501ea2010-10-27 12:18:21 +0100136 drm_i915_private_t *dev_priv = ring->dev->dev_private;
137 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200138 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800139
140 return I915_READ(acthd_reg);
141}
142
Chris Wilson78501ea2010-10-27 12:18:21 +0100143static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800144{
Chris Wilson78501ea2010-10-27 12:18:21 +0100145 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000146 struct drm_i915_gem_object *obj = ring->obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800147 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800148
149 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200150 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200151 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100152 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800153
154 /* Initialize the ring. */
Chris Wilson05394f32010-11-08 19:18:58 +0000155 I915_WRITE_START(ring, obj->gtt_offset);
Daniel Vetter570ef602010-08-02 17:06:23 +0200156 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800157
158 /* G45 ring initialization fails to reset head to zero */
159 if (head != 0) {
160 DRM_ERROR("%s head not reset to zero "
161 "ctl %08x head %08x tail %08x start %08x\n",
162 ring->name,
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200163 I915_READ_CTL(ring),
Daniel Vetter570ef602010-08-02 17:06:23 +0200164 I915_READ_HEAD(ring),
Daniel Vetter870e86d2010-08-02 16:29:44 +0200165 I915_READ_TAIL(ring),
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200166 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800167
Daniel Vetter570ef602010-08-02 17:06:23 +0200168 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800169
170 DRM_ERROR("%s head forced to zero "
171 "ctl %08x head %08x tail %08x start %08x\n",
172 ring->name,
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200173 I915_READ_CTL(ring),
Daniel Vetter570ef602010-08-02 17:06:23 +0200174 I915_READ_HEAD(ring),
Daniel Vetter870e86d2010-08-02 16:29:44 +0200175 I915_READ_TAIL(ring),
Daniel Vetter6c0e1c52010-08-02 16:33:33 +0200176 I915_READ_START(ring));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700177 }
178
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200179 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000180 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson6aa56062010-10-29 21:44:37 +0100181 | RING_REPORT_64K | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800182
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800183 /* If the head is still not zero, the ring is dead */
Chris Wilson176f28e2010-10-28 11:18:07 +0100184 if ((I915_READ_CTL(ring) & RING_VALID) == 0 ||
Chris Wilson05394f32010-11-08 19:18:58 +0000185 I915_READ_START(ring) != obj->gtt_offset ||
Chris Wilson176f28e2010-10-28 11:18:07 +0100186 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000187 DRM_ERROR("%s initialization failed "
188 "ctl %08x head %08x tail %08x start %08x\n",
189 ring->name,
190 I915_READ_CTL(ring),
191 I915_READ_HEAD(ring),
192 I915_READ_TAIL(ring),
193 I915_READ_START(ring));
194 return -EIO;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800195 }
196
Chris Wilson78501ea2010-10-27 12:18:21 +0100197 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
198 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800199 else {
Daniel Vetter570ef602010-08-02 17:06:23 +0200200 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Daniel Vetter870e86d2010-08-02 16:29:44 +0200201 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800202 ring->space = ring->head - (ring->tail + 8);
203 if (ring->space < 0)
204 ring->space += ring->size;
205 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000206
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800207 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700208}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800209
Chris Wilson78501ea2010-10-27 12:18:21 +0100210static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800211{
Chris Wilson78501ea2010-10-27 12:18:21 +0100212 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000213 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100214 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800215
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100216 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100217 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800218 if (IS_GEN6(dev))
219 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
220 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800221 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100222
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800223 return ret;
224}
225
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000226static void
227update_semaphore(struct intel_ring_buffer *ring, int i, u32 seqno)
228{
229 struct drm_device *dev = ring->dev;
230 struct drm_i915_private *dev_priv = dev->dev_private;
231 int id;
232
233 /*
234 * cs -> 1 = vcs, 0 = bcs
235 * vcs -> 1 = bcs, 0 = cs,
236 * bcs -> 1 = cs, 0 = vcs.
237 */
238 id = ring - dev_priv->ring;
239 id += 2 - i;
240 id %= 3;
241
242 intel_ring_emit(ring,
243 MI_SEMAPHORE_MBOX |
244 MI_SEMAPHORE_REGISTER |
245 MI_SEMAPHORE_UPDATE);
246 intel_ring_emit(ring, seqno);
247 intel_ring_emit(ring,
248 RING_SYNC_0(dev_priv->ring[id].mmio_base) + 4*i);
249}
250
251static int
252gen6_add_request(struct intel_ring_buffer *ring,
253 u32 *result)
254{
255 u32 seqno;
256 int ret;
257
258 ret = intel_ring_begin(ring, 10);
259 if (ret)
260 return ret;
261
262 seqno = i915_gem_get_seqno(ring->dev);
263 update_semaphore(ring, 0, seqno);
264 update_semaphore(ring, 1, seqno);
265
266 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
267 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
268 intel_ring_emit(ring, seqno);
269 intel_ring_emit(ring, MI_USER_INTERRUPT);
270 intel_ring_advance(ring);
271
272 *result = seqno;
273 return 0;
274}
275
276int
277intel_ring_sync(struct intel_ring_buffer *ring,
278 struct intel_ring_buffer *to,
279 u32 seqno)
280{
281 int ret;
282
283 ret = intel_ring_begin(ring, 4);
284 if (ret)
285 return ret;
286
287 intel_ring_emit(ring,
288 MI_SEMAPHORE_MBOX |
289 MI_SEMAPHORE_REGISTER |
290 intel_ring_sync_index(ring, to) << 17 |
291 MI_SEMAPHORE_COMPARE);
292 intel_ring_emit(ring, seqno);
293 intel_ring_emit(ring, 0);
294 intel_ring_emit(ring, MI_NOOP);
295 intel_ring_advance(ring);
296
297 return 0;
298}
299
Chris Wilson3cce4692010-10-27 16:11:02 +0100300static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100301render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100302 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700303{
Chris Wilson78501ea2010-10-27 12:18:21 +0100304 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100305 u32 seqno = i915_gem_get_seqno(dev);
306 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800307
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000308 ret = intel_ring_begin(ring, 4);
309 if (ret)
310 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100311
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000312 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
313 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
314 intel_ring_emit(ring, seqno);
315 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100316 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000317
Chris Wilson3cce4692010-10-27 16:11:02 +0100318 *result = seqno;
319 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700320}
321
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800322static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000323ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800324{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000325 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
326}
327
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800328static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000329render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700330{
Chris Wilson78501ea2010-10-27 12:18:21 +0100331 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700332
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000333 if (dev->irq_enabled && ++ring->irq_refcount == 1) {
334 drm_i915_private_t *dev_priv = dev->dev_private;
335 unsigned long irqflags;
336
337 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
338
Eric Anholt62fdfea2010-05-21 13:26:39 -0700339 if (HAS_PCH_SPLIT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000340 ironlake_enable_graphics_irq(dev_priv,
Chris Wilson88f23b82010-12-05 15:08:31 +0000341 GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700342 else
343 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000344
345 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700346 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700347}
348
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800349static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000350render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700351{
Chris Wilson78501ea2010-10-27 12:18:21 +0100352 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700353
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000354 BUG_ON(dev->irq_enabled && ring->irq_refcount == 0);
355 if (dev->irq_enabled && --ring->irq_refcount == 0) {
356 drm_i915_private_t *dev_priv = dev->dev_private;
357 unsigned long irqflags;
358
359 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700360 if (HAS_PCH_SPLIT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000361 ironlake_disable_graphics_irq(dev_priv,
Chris Wilson88f23b82010-12-05 15:08:31 +0000362 GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700363 else
364 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000365 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700366 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700367}
368
Chris Wilson78501ea2010-10-27 12:18:21 +0100369void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800370{
Chris Wilson78501ea2010-10-27 12:18:21 +0100371 drm_i915_private_t *dev_priv = ring->dev->dev_private;
372 u32 mmio = IS_GEN6(ring->dev) ?
373 RING_HWS_PGA_GEN6(ring->mmio_base) :
374 RING_HWS_PGA(ring->mmio_base);
375 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
376 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800377}
378
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100379static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100380bsd_ring_flush(struct intel_ring_buffer *ring,
381 u32 invalidate_domains,
382 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800383{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000384 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
385 return;
386
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100387 if (intel_ring_begin(ring, 2) == 0) {
388 intel_ring_emit(ring, MI_FLUSH);
389 intel_ring_emit(ring, MI_NOOP);
390 intel_ring_advance(ring);
391 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800392}
393
Chris Wilson3cce4692010-10-27 16:11:02 +0100394static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100395ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100396 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800397{
398 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100399 int ret;
400
401 ret = intel_ring_begin(ring, 4);
402 if (ret)
403 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100404
Chris Wilson78501ea2010-10-27 12:18:21 +0100405 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d52010-08-07 11:01:22 +0100406
Chris Wilson3cce4692010-10-27 16:11:02 +0100407 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
408 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
409 intel_ring_emit(ring, seqno);
410 intel_ring_emit(ring, MI_USER_INTERRUPT);
411 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800412
413 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +0100414 *result = seqno;
415 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800416}
417
Zou Nan haid1b851f2010-05-21 09:08:57 +0800418static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000419ring_get_irq(struct intel_ring_buffer *ring, u32 flag)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800420{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000421 struct drm_device *dev = ring->dev;
422
423 if (dev->irq_enabled && ++ring->irq_refcount == 1) {
424 drm_i915_private_t *dev_priv = dev->dev_private;
425 unsigned long irqflags;
426
427 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
428 ironlake_enable_graphics_irq(dev_priv, flag);
429 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
430 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800431}
432
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000433static void
434ring_put_irq(struct intel_ring_buffer *ring, u32 flag)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800435{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000436 struct drm_device *dev = ring->dev;
437
438 if (dev->irq_enabled && --ring->irq_refcount == 0) {
439 drm_i915_private_t *dev_priv = dev->dev_private;
440 unsigned long irqflags;
441
442 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
443 ironlake_disable_graphics_irq(dev_priv, flag);
444 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
445 }
446}
447
448
449static void
450bsd_ring_get_irq(struct intel_ring_buffer *ring)
451{
452 ring_get_irq(ring, GT_BSD_USER_INTERRUPT);
453}
454static void
455bsd_ring_put_irq(struct intel_ring_buffer *ring)
456{
457 ring_put_irq(ring, GT_BSD_USER_INTERRUPT);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800458}
459
460static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000461ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800462{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100463 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100464
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100465 ret = intel_ring_begin(ring, 2);
466 if (ret)
467 return ret;
468
Chris Wilson78501ea2010-10-27 12:18:21 +0100469 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000470 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100471 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000472 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100473 intel_ring_advance(ring);
474
Zou Nan haid1b851f2010-05-21 09:08:57 +0800475 return 0;
476}
477
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800478static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100479render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000480 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700481{
Chris Wilson78501ea2010-10-27 12:18:21 +0100482 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700483 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000484 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700485
Chris Wilson6f392d52010-08-07 11:01:22 +0100486 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700487
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000488 if (IS_I830(dev) || IS_845G(dev)) {
489 ret = intel_ring_begin(ring, 4);
490 if (ret)
491 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700492
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000493 intel_ring_emit(ring, MI_BATCH_BUFFER);
494 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
495 intel_ring_emit(ring, offset + len - 8);
496 intel_ring_emit(ring, 0);
497 } else {
498 ret = intel_ring_begin(ring, 2);
499 if (ret)
500 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100501
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000502 if (INTEL_INFO(dev)->gen >= 4) {
503 intel_ring_emit(ring,
504 MI_BATCH_BUFFER_START | (2 << 6) |
505 MI_BATCH_NON_SECURE_I965);
506 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700507 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000508 intel_ring_emit(ring,
509 MI_BATCH_BUFFER_START | (2 << 6));
510 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700511 }
512 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000513 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700514
Eric Anholt62fdfea2010-05-21 13:26:39 -0700515 return 0;
516}
517
Chris Wilson78501ea2010-10-27 12:18:21 +0100518static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700519{
Chris Wilson78501ea2010-10-27 12:18:21 +0100520 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000521 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700522
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800523 obj = ring->status_page.obj;
524 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700525 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700526
Chris Wilson05394f32010-11-08 19:18:58 +0000527 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700528 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000529 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800530 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700531
532 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700533}
534
Chris Wilson78501ea2010-10-27 12:18:21 +0100535static int init_status_page(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;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700538 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000539 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700540 int ret;
541
Eric Anholt62fdfea2010-05-21 13:26:39 -0700542 obj = i915_gem_alloc_object(dev, 4096);
543 if (obj == NULL) {
544 DRM_ERROR("Failed to allocate status page\n");
545 ret = -ENOMEM;
546 goto err;
547 }
Chris Wilson05394f32010-11-08 19:18:58 +0000548 obj->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700549
Daniel Vetter75e9e912010-11-04 17:11:09 +0100550 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700551 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700552 goto err_unref;
553 }
554
Chris Wilson05394f32010-11-08 19:18:58 +0000555 ring->status_page.gfx_addr = obj->gtt_offset;
556 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800557 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700558 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700559 goto err_unpin;
560 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800561 ring->status_page.obj = obj;
562 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700563
Chris Wilson78501ea2010-10-27 12:18:21 +0100564 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800565 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
566 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700567
568 return 0;
569
570err_unpin:
571 i915_gem_object_unpin(obj);
572err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000573 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700574err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800575 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700576}
577
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800578int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100579 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700580{
Chris Wilson05394f32010-11-08 19:18:58 +0000581 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100582 int ret;
583
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800584 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100585 INIT_LIST_HEAD(&ring->active_list);
586 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100587 INIT_LIST_HEAD(&ring->gpu_write_list);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700588
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800589 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100590 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800591 if (ret)
592 return ret;
593 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700594
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800595 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700596 if (obj == NULL) {
597 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800598 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100599 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700600 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700601
Chris Wilson05394f32010-11-08 19:18:58 +0000602 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800603
Daniel Vetter75e9e912010-11-04 17:11:09 +0100604 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100605 if (ret)
606 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700607
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800608 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000609 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700610 ring->map.type = 0;
611 ring->map.flags = 0;
612 ring->map.mtrr = 0;
613
614 drm_core_ioremap_wc(&ring->map, dev);
615 if (ring->map.handle == NULL) {
616 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800617 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100618 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700619 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800620
Eric Anholt62fdfea2010-05-21 13:26:39 -0700621 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100622 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100623 if (ret)
624 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700625
Chris Wilsonc584fe42010-10-29 18:15:52 +0100626 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100627
628err_unmap:
629 drm_core_ioremapfree(&ring->map, dev);
630err_unpin:
631 i915_gem_object_unpin(obj);
632err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000633 drm_gem_object_unreference(&obj->base);
634 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100635err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100636 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800637 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700638}
639
Chris Wilson78501ea2010-10-27 12:18:21 +0100640void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700641{
Chris Wilson33626e62010-10-29 16:18:36 +0100642 struct drm_i915_private *dev_priv;
643 int ret;
644
Chris Wilson05394f32010-11-08 19:18:58 +0000645 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700646 return;
647
Chris Wilson33626e62010-10-29 16:18:36 +0100648 /* Disable the ring buffer. The ring must be idle at this point */
649 dev_priv = ring->dev->dev_private;
650 ret = intel_wait_ring_buffer(ring, ring->size - 8);
651 I915_WRITE_CTL(ring, 0);
652
Chris Wilson78501ea2010-10-27 12:18:21 +0100653 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700654
Chris Wilson05394f32010-11-08 19:18:58 +0000655 i915_gem_object_unpin(ring->obj);
656 drm_gem_object_unreference(&ring->obj->base);
657 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100658
Zou Nan hai8d192152010-11-02 16:31:01 +0800659 if (ring->cleanup)
660 ring->cleanup(ring);
661
Chris Wilson78501ea2010-10-27 12:18:21 +0100662 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700663}
664
Chris Wilson78501ea2010-10-27 12:18:21 +0100665static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700666{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800667 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700668 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800669 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700670
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800671 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100672 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700673 if (ret)
674 return ret;
675 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700676
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800677 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100678 rem /= 8;
679 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700680 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100681 *virt++ = MI_NOOP;
682 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700683
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800684 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100685 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700686
687 return 0;
688}
689
Chris Wilson78501ea2010-10-27 12:18:21 +0100690int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700691{
Chris Wilson78501ea2010-10-27 12:18:21 +0100692 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800693 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100694 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100695 u32 head;
696
697 head = intel_read_status_page(ring, 4);
698 if (head) {
699 ring->head = head & HEAD_ADDR;
700 ring->space = ring->head - (ring->tail + 8);
701 if (ring->space < 0)
702 ring->space += ring->size;
703 if (ring->space >= n)
704 return 0;
705 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700706
707 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800708 end = jiffies + 3 * HZ;
709 do {
Daniel Vetter570ef602010-08-02 17:06:23 +0200710 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700711 ring->space = ring->head - (ring->tail + 8);
712 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800713 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700714 if (ring->space >= n) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100715 trace_i915_ring_wait_end(dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700716 return 0;
717 }
718
719 if (dev->primary->master) {
720 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
721 if (master_priv->sarea_priv)
722 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
723 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800724
Chris Wilsone60a0b12010-10-13 10:09:14 +0100725 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +0100726 if (atomic_read(&dev_priv->mm.wedged))
727 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800728 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700729 trace_i915_ring_wait_end (dev);
730 return -EBUSY;
731}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800732
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100733int intel_ring_begin(struct intel_ring_buffer *ring,
734 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800735{
Zou Nan haibe26a102010-06-12 17:40:24 +0800736 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100737 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100738
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100739 if (unlikely(ring->tail + n > ring->size)) {
740 ret = intel_wrap_ring_buffer(ring);
741 if (unlikely(ret))
742 return ret;
743 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100744
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100745 if (unlikely(ring->space < n)) {
746 ret = intel_wait_ring_buffer(ring, n);
747 if (unlikely(ret))
748 return ret;
749 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100750
751 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100752 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800753}
754
Chris Wilson78501ea2010-10-27 12:18:21 +0100755void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800756{
Chris Wilsond97ed332010-08-04 15:18:13 +0100757 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100758 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800759}
760
Chris Wilsone0708682010-09-19 14:46:27 +0100761static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800762 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100763 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200764 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800765 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800766 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100767 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800768 .flush = render_ring_flush,
769 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000770 .get_seqno = ring_get_seqno,
771 .irq_get = render_ring_get_irq,
772 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100773 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800774};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800775
776/* ring buffer for bit-stream decoder */
777
Chris Wilsone0708682010-09-19 14:46:27 +0100778static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +0800779 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +0100780 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200781 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800782 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100783 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100784 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800785 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +0100786 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000787 .get_seqno = ring_get_seqno,
788 .irq_get = bsd_ring_get_irq,
789 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100790 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800791};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800792
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100793
Chris Wilson78501ea2010-10-27 12:18:21 +0100794static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100795 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100796{
Chris Wilson78501ea2010-10-27 12:18:21 +0100797 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100798
799 /* Every tail move must follow the sequence below */
800 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
801 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
802 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
803 I915_WRITE(GEN6_BSD_RNCID, 0x0);
804
805 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
806 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
807 50))
808 DRM_ERROR("timed out waiting for IDLE Indicator\n");
809
Daniel Vetter870e86d2010-08-02 16:29:44 +0200810 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100811 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
812 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
813 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
814}
815
Chris Wilson78501ea2010-10-27 12:18:21 +0100816static void gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson549f7362010-10-19 11:19:32 +0100817 u32 invalidate_domains,
818 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100819{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000820 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
821 return;
822
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100823 if (intel_ring_begin(ring, 4) == 0) {
824 intel_ring_emit(ring, MI_FLUSH_DW);
825 intel_ring_emit(ring, 0);
826 intel_ring_emit(ring, 0);
827 intel_ring_emit(ring, 0);
828 intel_ring_advance(ring);
829 }
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100830}
831
832static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100833gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000834 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100835{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100836 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100837
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100838 ret = intel_ring_begin(ring, 2);
839 if (ret)
840 return ret;
841
Chris Wilson78501ea2010-10-27 12:18:21 +0100842 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100843 /* bit0-7 is the length on GEN6+ */
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000844 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100845 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100846
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100847 return 0;
848}
849
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000850static void
851gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
852{
853 ring_get_irq(ring, GT_GEN6_BSD_USER_INTERRUPT);
854}
855
856static void
857gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
858{
859 ring_put_irq(ring, GT_GEN6_BSD_USER_INTERRUPT);
860}
861
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100862/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +0100863static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000864 .name = "gen6 bsd ring",
865 .id = RING_BSD,
866 .mmio_base = GEN6_BSD_RING_BASE,
867 .size = 32 * PAGE_SIZE,
868 .init = init_ring_common,
869 .write_tail = gen6_bsd_ring_write_tail,
870 .flush = gen6_ring_flush,
871 .add_request = gen6_add_request,
872 .get_seqno = ring_get_seqno,
873 .irq_get = gen6_bsd_ring_get_irq,
874 .irq_put = gen6_bsd_ring_put_irq,
875 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +0100876};
877
878/* Blitter support (SandyBridge+) */
879
880static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000881blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100882{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000883 ring_get_irq(ring, GT_BLT_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +0100884}
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000885
Chris Wilson549f7362010-10-19 11:19:32 +0100886static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000887blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100888{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000889 ring_put_irq(ring, GT_BLT_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +0100890}
891
Zou Nan hai8d192152010-11-02 16:31:01 +0800892
893/* Workaround for some stepping of SNB,
894 * each time when BLT engine ring tail moved,
895 * the first command in the ring to be parsed
896 * should be MI_BATCH_BUFFER_START
897 */
898#define NEED_BLT_WORKAROUND(dev) \
899 (IS_GEN6(dev) && (dev->pdev->revision < 8))
900
901static inline struct drm_i915_gem_object *
902to_blt_workaround(struct intel_ring_buffer *ring)
903{
904 return ring->private;
905}
906
907static int blt_ring_init(struct intel_ring_buffer *ring)
908{
909 if (NEED_BLT_WORKAROUND(ring->dev)) {
910 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +0000911 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +0800912 int ret;
913
Chris Wilson05394f32010-11-08 19:18:58 +0000914 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +0800915 if (obj == NULL)
916 return -ENOMEM;
917
Chris Wilson05394f32010-11-08 19:18:58 +0000918 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +0800919 if (ret) {
920 drm_gem_object_unreference(&obj->base);
921 return ret;
922 }
923
924 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +0000925 *ptr++ = MI_BATCH_BUFFER_END;
926 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +0800927 kunmap(obj->pages[0]);
928
Chris Wilson05394f32010-11-08 19:18:58 +0000929 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +0800930 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000931 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +0800932 drm_gem_object_unreference(&obj->base);
933 return ret;
934 }
935
936 ring->private = obj;
937 }
938
939 return init_ring_common(ring);
940}
941
942static int blt_ring_begin(struct intel_ring_buffer *ring,
943 int num_dwords)
944{
945 if (ring->private) {
946 int ret = intel_ring_begin(ring, num_dwords+2);
947 if (ret)
948 return ret;
949
950 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
951 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
952
953 return 0;
954 } else
955 return intel_ring_begin(ring, 4);
956}
957
958static void blt_ring_flush(struct intel_ring_buffer *ring,
959 u32 invalidate_domains,
960 u32 flush_domains)
961{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000962 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
963 return;
964
Zou Nan hai8d192152010-11-02 16:31:01 +0800965 if (blt_ring_begin(ring, 4) == 0) {
966 intel_ring_emit(ring, MI_FLUSH_DW);
967 intel_ring_emit(ring, 0);
968 intel_ring_emit(ring, 0);
969 intel_ring_emit(ring, 0);
970 intel_ring_advance(ring);
971 }
972}
973
Zou Nan hai8d192152010-11-02 16:31:01 +0800974static void blt_ring_cleanup(struct intel_ring_buffer *ring)
975{
976 if (!ring->private)
977 return;
978
979 i915_gem_object_unpin(ring->private);
980 drm_gem_object_unreference(ring->private);
981 ring->private = NULL;
982}
983
Chris Wilson549f7362010-10-19 11:19:32 +0100984static const struct intel_ring_buffer gen6_blt_ring = {
985 .name = "blt ring",
986 .id = RING_BLT,
987 .mmio_base = BLT_RING_BASE,
988 .size = 32 * PAGE_SIZE,
Zou Nan hai8d192152010-11-02 16:31:01 +0800989 .init = blt_ring_init,
Chris Wilson297b0c52010-10-22 17:02:41 +0100990 .write_tail = ring_write_tail,
Zou Nan hai8d192152010-11-02 16:31:01 +0800991 .flush = blt_ring_flush,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000992 .add_request = gen6_add_request,
993 .get_seqno = ring_get_seqno,
994 .irq_get = blt_ring_get_irq,
995 .irq_put = blt_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100996 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Zou Nan hai8d192152010-11-02 16:31:01 +0800997 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100998};
999
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001000int intel_init_render_ring_buffer(struct drm_device *dev)
1001{
1002 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001003 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001004
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001005 *ring = render_ring;
1006 if (INTEL_INFO(dev)->gen >= 6) {
1007 ring->add_request = gen6_add_request;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001008 }
1009
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001010 if (!I915_NEED_GFX_HWS(dev)) {
1011 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1012 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1013 }
1014
1015 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001016}
1017
1018int intel_init_bsd_ring_buffer(struct drm_device *dev)
1019{
1020 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001021 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001022
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001023 if (IS_GEN6(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001024 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001025 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001026 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001027
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001028 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001029}
Chris Wilson549f7362010-10-19 11:19:32 +01001030
1031int intel_init_blt_ring_buffer(struct drm_device *dev)
1032{
1033 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001034 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001035
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001036 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001037
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001038 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001039}