blob: 67c545204f19172b0f30b808188e743c0a49a266 [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) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000160 DRM_DEBUG_KMS("%s head not reset to zero "
161 "ctl %08x head %08x tail %08x start %08x\n",
162 ring->name,
163 I915_READ_CTL(ring),
164 I915_READ_HEAD(ring),
165 I915_READ_TAIL(ring),
166 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
Chris Wilson6fd0d562010-12-05 20:42:33 +0000170 if (I915_READ_HEAD(ring) & HEAD_ADDR) {
171 DRM_ERROR("failed to set %s head to zero "
172 "ctl %08x head %08x tail %08x start %08x\n",
173 ring->name,
174 I915_READ_CTL(ring),
175 I915_READ_HEAD(ring),
176 I915_READ_TAIL(ring),
177 I915_READ_START(ring));
178 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700179 }
180
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200181 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000182 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson6aa56062010-10-29 21:44:37 +0100183 | RING_REPORT_64K | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800184
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800185 /* If the head is still not zero, the ring is dead */
Chris Wilson176f28e2010-10-28 11:18:07 +0100186 if ((I915_READ_CTL(ring) & RING_VALID) == 0 ||
Chris Wilson05394f32010-11-08 19:18:58 +0000187 I915_READ_START(ring) != obj->gtt_offset ||
Chris Wilson176f28e2010-10-28 11:18:07 +0100188 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000189 DRM_ERROR("%s initialization failed "
190 "ctl %08x head %08x tail %08x start %08x\n",
191 ring->name,
192 I915_READ_CTL(ring),
193 I915_READ_HEAD(ring),
194 I915_READ_TAIL(ring),
195 I915_READ_START(ring));
196 return -EIO;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800197 }
198
Chris Wilson78501ea2010-10-27 12:18:21 +0100199 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
200 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800201 else {
Daniel Vetter570ef602010-08-02 17:06:23 +0200202 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Daniel Vetter870e86d2010-08-02 16:29:44 +0200203 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800204 ring->space = ring->head - (ring->tail + 8);
205 if (ring->space < 0)
206 ring->space += ring->size;
207 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000208
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800209 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700210}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800211
Chris Wilson78501ea2010-10-27 12:18:21 +0100212static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800213{
Chris Wilson78501ea2010-10-27 12:18:21 +0100214 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000215 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100216 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800217
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100218 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100219 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800220 if (IS_GEN6(dev))
221 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
222 I915_WRITE(MI_MODE, mode);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800223 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100224
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800225 return ret;
226}
227
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000228static void
229update_semaphore(struct intel_ring_buffer *ring, int i, u32 seqno)
230{
231 struct drm_device *dev = ring->dev;
232 struct drm_i915_private *dev_priv = dev->dev_private;
233 int id;
234
235 /*
236 * cs -> 1 = vcs, 0 = bcs
237 * vcs -> 1 = bcs, 0 = cs,
238 * bcs -> 1 = cs, 0 = vcs.
239 */
240 id = ring - dev_priv->ring;
241 id += 2 - i;
242 id %= 3;
243
244 intel_ring_emit(ring,
245 MI_SEMAPHORE_MBOX |
246 MI_SEMAPHORE_REGISTER |
247 MI_SEMAPHORE_UPDATE);
248 intel_ring_emit(ring, seqno);
249 intel_ring_emit(ring,
250 RING_SYNC_0(dev_priv->ring[id].mmio_base) + 4*i);
251}
252
253static int
254gen6_add_request(struct intel_ring_buffer *ring,
255 u32 *result)
256{
257 u32 seqno;
258 int ret;
259
260 ret = intel_ring_begin(ring, 10);
261 if (ret)
262 return ret;
263
264 seqno = i915_gem_get_seqno(ring->dev);
265 update_semaphore(ring, 0, seqno);
266 update_semaphore(ring, 1, seqno);
267
268 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
269 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
270 intel_ring_emit(ring, seqno);
271 intel_ring_emit(ring, MI_USER_INTERRUPT);
272 intel_ring_advance(ring);
273
274 *result = seqno;
275 return 0;
276}
277
278int
279intel_ring_sync(struct intel_ring_buffer *ring,
280 struct intel_ring_buffer *to,
281 u32 seqno)
282{
283 int ret;
284
285 ret = intel_ring_begin(ring, 4);
286 if (ret)
287 return ret;
288
289 intel_ring_emit(ring,
290 MI_SEMAPHORE_MBOX |
291 MI_SEMAPHORE_REGISTER |
292 intel_ring_sync_index(ring, to) << 17 |
293 MI_SEMAPHORE_COMPARE);
294 intel_ring_emit(ring, seqno);
295 intel_ring_emit(ring, 0);
296 intel_ring_emit(ring, MI_NOOP);
297 intel_ring_advance(ring);
298
299 return 0;
300}
301
Chris Wilson3cce4692010-10-27 16:11:02 +0100302static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100303render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100304 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700305{
Chris Wilson78501ea2010-10-27 12:18:21 +0100306 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100307 u32 seqno = i915_gem_get_seqno(dev);
308 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800309
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000310 ret = intel_ring_begin(ring, 4);
311 if (ret)
312 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100313
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000314 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
315 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
316 intel_ring_emit(ring, seqno);
317 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100318 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000319
Chris Wilson3cce4692010-10-27 16:11:02 +0100320 *result = seqno;
321 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700322}
323
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800324static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000325ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800326{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000327 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
328}
329
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800330static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000331render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700332{
Chris Wilson78501ea2010-10-27 12:18:21 +0100333 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700334
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000335 if (dev->irq_enabled && ++ring->irq_refcount == 1) {
336 drm_i915_private_t *dev_priv = dev->dev_private;
337 unsigned long irqflags;
338
339 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
340
Eric Anholt62fdfea2010-05-21 13:26:39 -0700341 if (HAS_PCH_SPLIT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000342 ironlake_enable_graphics_irq(dev_priv,
Chris Wilson88f23b82010-12-05 15:08:31 +0000343 GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700344 else
345 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000346
347 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700348 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700349}
350
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800351static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000352render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700353{
Chris Wilson78501ea2010-10-27 12:18:21 +0100354 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700355
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000356 BUG_ON(dev->irq_enabled && ring->irq_refcount == 0);
357 if (dev->irq_enabled && --ring->irq_refcount == 0) {
358 drm_i915_private_t *dev_priv = dev->dev_private;
359 unsigned long irqflags;
360
361 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700362 if (HAS_PCH_SPLIT(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000363 ironlake_disable_graphics_irq(dev_priv,
Chris Wilson88f23b82010-12-05 15:08:31 +0000364 GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700365 else
366 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000367 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700368 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700369}
370
Chris Wilson78501ea2010-10-27 12:18:21 +0100371void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800372{
Chris Wilson78501ea2010-10-27 12:18:21 +0100373 drm_i915_private_t *dev_priv = ring->dev->dev_private;
374 u32 mmio = IS_GEN6(ring->dev) ?
375 RING_HWS_PGA_GEN6(ring->mmio_base) :
376 RING_HWS_PGA(ring->mmio_base);
377 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
378 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800379}
380
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100381static void
Chris Wilson78501ea2010-10-27 12:18:21 +0100382bsd_ring_flush(struct intel_ring_buffer *ring,
383 u32 invalidate_domains,
384 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800385{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000386 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
387 return;
388
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100389 if (intel_ring_begin(ring, 2) == 0) {
390 intel_ring_emit(ring, MI_FLUSH);
391 intel_ring_emit(ring, MI_NOOP);
392 intel_ring_advance(ring);
393 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800394}
395
Chris Wilson3cce4692010-10-27 16:11:02 +0100396static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100397ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100398 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800399{
400 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100401 int ret;
402
403 ret = intel_ring_begin(ring, 4);
404 if (ret)
405 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100406
Chris Wilson78501ea2010-10-27 12:18:21 +0100407 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d52010-08-07 11:01:22 +0100408
Chris Wilson3cce4692010-10-27 16:11:02 +0100409 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
410 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
411 intel_ring_emit(ring, seqno);
412 intel_ring_emit(ring, MI_USER_INTERRUPT);
413 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800414
415 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
Chris Wilson3cce4692010-10-27 16:11:02 +0100416 *result = seqno;
417 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800418}
419
Zou Nan haid1b851f2010-05-21 09:08:57 +0800420static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000421ring_get_irq(struct intel_ring_buffer *ring, u32 flag)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800422{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000423 struct drm_device *dev = ring->dev;
424
425 if (dev->irq_enabled && ++ring->irq_refcount == 1) {
426 drm_i915_private_t *dev_priv = dev->dev_private;
427 unsigned long irqflags;
428
429 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
430 ironlake_enable_graphics_irq(dev_priv, flag);
431 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
432 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800433}
434
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000435static void
436ring_put_irq(struct intel_ring_buffer *ring, u32 flag)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800437{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000438 struct drm_device *dev = ring->dev;
439
440 if (dev->irq_enabled && --ring->irq_refcount == 0) {
441 drm_i915_private_t *dev_priv = dev->dev_private;
442 unsigned long irqflags;
443
444 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
445 ironlake_disable_graphics_irq(dev_priv, flag);
446 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
447 }
448}
449
450
451static void
452bsd_ring_get_irq(struct intel_ring_buffer *ring)
453{
454 ring_get_irq(ring, GT_BSD_USER_INTERRUPT);
455}
456static void
457bsd_ring_put_irq(struct intel_ring_buffer *ring)
458{
459 ring_put_irq(ring, GT_BSD_USER_INTERRUPT);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800460}
461
462static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000463ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800464{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100465 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100466
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100467 ret = intel_ring_begin(ring, 2);
468 if (ret)
469 return ret;
470
Chris Wilson78501ea2010-10-27 12:18:21 +0100471 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000472 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100473 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000474 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100475 intel_ring_advance(ring);
476
Zou Nan haid1b851f2010-05-21 09:08:57 +0800477 return 0;
478}
479
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800480static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100481render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000482 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700483{
Chris Wilson78501ea2010-10-27 12:18:21 +0100484 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700485 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000486 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700487
Chris Wilson6f392d52010-08-07 11:01:22 +0100488 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700489
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000490 if (IS_I830(dev) || IS_845G(dev)) {
491 ret = intel_ring_begin(ring, 4);
492 if (ret)
493 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700494
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000495 intel_ring_emit(ring, MI_BATCH_BUFFER);
496 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
497 intel_ring_emit(ring, offset + len - 8);
498 intel_ring_emit(ring, 0);
499 } else {
500 ret = intel_ring_begin(ring, 2);
501 if (ret)
502 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100503
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000504 if (INTEL_INFO(dev)->gen >= 4) {
505 intel_ring_emit(ring,
506 MI_BATCH_BUFFER_START | (2 << 6) |
507 MI_BATCH_NON_SECURE_I965);
508 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700509 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000510 intel_ring_emit(ring,
511 MI_BATCH_BUFFER_START | (2 << 6));
512 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700513 }
514 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000515 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700516
Eric Anholt62fdfea2010-05-21 13:26:39 -0700517 return 0;
518}
519
Chris Wilson78501ea2010-10-27 12:18:21 +0100520static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700521{
Chris Wilson78501ea2010-10-27 12:18:21 +0100522 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000523 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700524
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800525 obj = ring->status_page.obj;
526 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700527 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700528
Chris Wilson05394f32010-11-08 19:18:58 +0000529 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700530 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000531 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800532 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700533
534 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700535}
536
Chris Wilson78501ea2010-10-27 12:18:21 +0100537static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700538{
Chris Wilson78501ea2010-10-27 12:18:21 +0100539 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700540 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000541 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700542 int ret;
543
Eric Anholt62fdfea2010-05-21 13:26:39 -0700544 obj = i915_gem_alloc_object(dev, 4096);
545 if (obj == NULL) {
546 DRM_ERROR("Failed to allocate status page\n");
547 ret = -ENOMEM;
548 goto err;
549 }
Chris Wilson05394f32010-11-08 19:18:58 +0000550 obj->agp_type = AGP_USER_CACHED_MEMORY;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700551
Daniel Vetter75e9e912010-11-04 17:11:09 +0100552 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700553 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700554 goto err_unref;
555 }
556
Chris Wilson05394f32010-11-08 19:18:58 +0000557 ring->status_page.gfx_addr = obj->gtt_offset;
558 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800559 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700560 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700561 goto err_unpin;
562 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800563 ring->status_page.obj = obj;
564 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700565
Chris Wilson78501ea2010-10-27 12:18:21 +0100566 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800567 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
568 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700569
570 return 0;
571
572err_unpin:
573 i915_gem_object_unpin(obj);
574err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000575 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700576err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800577 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700578}
579
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800580int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100581 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700582{
Chris Wilson05394f32010-11-08 19:18:58 +0000583 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100584 int ret;
585
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800586 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100587 INIT_LIST_HEAD(&ring->active_list);
588 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100589 INIT_LIST_HEAD(&ring->gpu_write_list);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700590
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800591 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100592 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800593 if (ret)
594 return ret;
595 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700596
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800597 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700598 if (obj == NULL) {
599 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800600 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100601 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700602 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700603
Chris Wilson05394f32010-11-08 19:18:58 +0000604 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800605
Daniel Vetter75e9e912010-11-04 17:11:09 +0100606 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100607 if (ret)
608 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700609
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800610 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000611 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700612 ring->map.type = 0;
613 ring->map.flags = 0;
614 ring->map.mtrr = 0;
615
616 drm_core_ioremap_wc(&ring->map, dev);
617 if (ring->map.handle == NULL) {
618 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800619 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100620 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700621 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800622
Eric Anholt62fdfea2010-05-21 13:26:39 -0700623 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100624 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100625 if (ret)
626 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700627
Chris Wilsonc584fe42010-10-29 18:15:52 +0100628 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100629
630err_unmap:
631 drm_core_ioremapfree(&ring->map, dev);
632err_unpin:
633 i915_gem_object_unpin(obj);
634err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000635 drm_gem_object_unreference(&obj->base);
636 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100637err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100638 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800639 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700640}
641
Chris Wilson78501ea2010-10-27 12:18:21 +0100642void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700643{
Chris Wilson33626e62010-10-29 16:18:36 +0100644 struct drm_i915_private *dev_priv;
645 int ret;
646
Chris Wilson05394f32010-11-08 19:18:58 +0000647 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700648 return;
649
Chris Wilson33626e62010-10-29 16:18:36 +0100650 /* Disable the ring buffer. The ring must be idle at this point */
651 dev_priv = ring->dev->dev_private;
652 ret = intel_wait_ring_buffer(ring, ring->size - 8);
653 I915_WRITE_CTL(ring, 0);
654
Chris Wilson78501ea2010-10-27 12:18:21 +0100655 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700656
Chris Wilson05394f32010-11-08 19:18:58 +0000657 i915_gem_object_unpin(ring->obj);
658 drm_gem_object_unreference(&ring->obj->base);
659 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +0100660
Zou Nan hai8d192152010-11-02 16:31:01 +0800661 if (ring->cleanup)
662 ring->cleanup(ring);
663
Chris Wilson78501ea2010-10-27 12:18:21 +0100664 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700665}
666
Chris Wilson78501ea2010-10-27 12:18:21 +0100667static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700668{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800669 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700670 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800671 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700672
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800673 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100674 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700675 if (ret)
676 return ret;
677 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700678
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800679 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100680 rem /= 8;
681 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700682 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100683 *virt++ = MI_NOOP;
684 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700685
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800686 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100687 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700688
689 return 0;
690}
691
Chris Wilson78501ea2010-10-27 12:18:21 +0100692int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700693{
Chris Wilson78501ea2010-10-27 12:18:21 +0100694 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +0800695 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100696 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +0100697 u32 head;
698
699 head = intel_read_status_page(ring, 4);
700 if (head) {
701 ring->head = head & HEAD_ADDR;
702 ring->space = ring->head - (ring->tail + 8);
703 if (ring->space < 0)
704 ring->space += ring->size;
705 if (ring->space >= n)
706 return 0;
707 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700708
709 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800710 end = jiffies + 3 * HZ;
711 do {
Daniel Vetter570ef602010-08-02 17:06:23 +0200712 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700713 ring->space = ring->head - (ring->tail + 8);
714 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800715 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700716 if (ring->space >= n) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100717 trace_i915_ring_wait_end(dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700718 return 0;
719 }
720
721 if (dev->primary->master) {
722 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
723 if (master_priv->sarea_priv)
724 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
725 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800726
Chris Wilsone60a0b12010-10-13 10:09:14 +0100727 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +0100728 if (atomic_read(&dev_priv->mm.wedged))
729 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800730 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700731 trace_i915_ring_wait_end (dev);
732 return -EBUSY;
733}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800734
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100735int intel_ring_begin(struct intel_ring_buffer *ring,
736 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800737{
Zou Nan haibe26a102010-06-12 17:40:24 +0800738 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100739 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100740
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100741 if (unlikely(ring->tail + n > ring->size)) {
742 ret = intel_wrap_ring_buffer(ring);
743 if (unlikely(ret))
744 return ret;
745 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100746
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100747 if (unlikely(ring->space < n)) {
748 ret = intel_wait_ring_buffer(ring, n);
749 if (unlikely(ret))
750 return ret;
751 }
Chris Wilsond97ed332010-08-04 15:18:13 +0100752
753 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100754 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800755}
756
Chris Wilson78501ea2010-10-27 12:18:21 +0100757void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800758{
Chris Wilsond97ed332010-08-04 15:18:13 +0100759 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +0100760 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800761}
762
Chris Wilsone0708682010-09-19 14:46:27 +0100763static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800764 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +0100765 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200766 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800767 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800768 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100769 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800770 .flush = render_ring_flush,
771 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000772 .get_seqno = ring_get_seqno,
773 .irq_get = render_ring_get_irq,
774 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100775 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800776};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800777
778/* ring buffer for bit-stream decoder */
779
Chris Wilsone0708682010-09-19 14:46:27 +0100780static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +0800781 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +0100782 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +0200783 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800784 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +0100785 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +0100786 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800787 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +0100788 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000789 .get_seqno = ring_get_seqno,
790 .irq_get = bsd_ring_get_irq,
791 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100792 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800793};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +0800794
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100795
Chris Wilson78501ea2010-10-27 12:18:21 +0100796static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100797 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100798{
Chris Wilson78501ea2010-10-27 12:18:21 +0100799 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100800
801 /* Every tail move must follow the sequence below */
802 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
803 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
804 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
805 I915_WRITE(GEN6_BSD_RNCID, 0x0);
806
807 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
808 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
809 50))
810 DRM_ERROR("timed out waiting for IDLE Indicator\n");
811
Daniel Vetter870e86d2010-08-02 16:29:44 +0200812 I915_WRITE_TAIL(ring, value);
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100813 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
814 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
815 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
816}
817
Chris Wilson78501ea2010-10-27 12:18:21 +0100818static void gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson549f7362010-10-19 11:19:32 +0100819 u32 invalidate_domains,
820 u32 flush_domains)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100821{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000822 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
823 return;
824
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100825 if (intel_ring_begin(ring, 4) == 0) {
826 intel_ring_emit(ring, MI_FLUSH_DW);
827 intel_ring_emit(ring, 0);
828 intel_ring_emit(ring, 0);
829 intel_ring_emit(ring, 0);
830 intel_ring_advance(ring);
831 }
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100832}
833
834static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100835gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000836 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100837{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100838 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100839
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100840 ret = intel_ring_begin(ring, 2);
841 if (ret)
842 return ret;
843
Chris Wilson78501ea2010-10-27 12:18:21 +0100844 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100845 /* bit0-7 is the length on GEN6+ */
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000846 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100847 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100848
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100849 return 0;
850}
851
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000852static void
853gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
854{
855 ring_get_irq(ring, GT_GEN6_BSD_USER_INTERRUPT);
856}
857
858static void
859gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
860{
861 ring_put_irq(ring, GT_GEN6_BSD_USER_INTERRUPT);
862}
863
Xiang, Haihao881f47b2010-09-19 14:40:43 +0100864/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +0100865static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000866 .name = "gen6 bsd ring",
867 .id = RING_BSD,
868 .mmio_base = GEN6_BSD_RING_BASE,
869 .size = 32 * PAGE_SIZE,
870 .init = init_ring_common,
871 .write_tail = gen6_bsd_ring_write_tail,
872 .flush = gen6_ring_flush,
873 .add_request = gen6_add_request,
874 .get_seqno = ring_get_seqno,
875 .irq_get = gen6_bsd_ring_get_irq,
876 .irq_put = gen6_bsd_ring_put_irq,
877 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Chris Wilson549f7362010-10-19 11:19:32 +0100878};
879
880/* Blitter support (SandyBridge+) */
881
882static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000883blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100884{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000885 ring_get_irq(ring, GT_BLT_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +0100886}
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000887
Chris Wilson549f7362010-10-19 11:19:32 +0100888static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000889blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +0100890{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000891 ring_put_irq(ring, GT_BLT_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +0100892}
893
Zou Nan hai8d192152010-11-02 16:31:01 +0800894
895/* Workaround for some stepping of SNB,
896 * each time when BLT engine ring tail moved,
897 * the first command in the ring to be parsed
898 * should be MI_BATCH_BUFFER_START
899 */
900#define NEED_BLT_WORKAROUND(dev) \
901 (IS_GEN6(dev) && (dev->pdev->revision < 8))
902
903static inline struct drm_i915_gem_object *
904to_blt_workaround(struct intel_ring_buffer *ring)
905{
906 return ring->private;
907}
908
909static int blt_ring_init(struct intel_ring_buffer *ring)
910{
911 if (NEED_BLT_WORKAROUND(ring->dev)) {
912 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +0000913 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +0800914 int ret;
915
Chris Wilson05394f32010-11-08 19:18:58 +0000916 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +0800917 if (obj == NULL)
918 return -ENOMEM;
919
Chris Wilson05394f32010-11-08 19:18:58 +0000920 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +0800921 if (ret) {
922 drm_gem_object_unreference(&obj->base);
923 return ret;
924 }
925
926 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +0000927 *ptr++ = MI_BATCH_BUFFER_END;
928 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +0800929 kunmap(obj->pages[0]);
930
Chris Wilson05394f32010-11-08 19:18:58 +0000931 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +0800932 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000933 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +0800934 drm_gem_object_unreference(&obj->base);
935 return ret;
936 }
937
938 ring->private = obj;
939 }
940
941 return init_ring_common(ring);
942}
943
944static int blt_ring_begin(struct intel_ring_buffer *ring,
945 int num_dwords)
946{
947 if (ring->private) {
948 int ret = intel_ring_begin(ring, num_dwords+2);
949 if (ret)
950 return ret;
951
952 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
953 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
954
955 return 0;
956 } else
957 return intel_ring_begin(ring, 4);
958}
959
960static void blt_ring_flush(struct intel_ring_buffer *ring,
961 u32 invalidate_domains,
962 u32 flush_domains)
963{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000964 if ((flush_domains & I915_GEM_DOMAIN_RENDER) == 0)
965 return;
966
Zou Nan hai8d192152010-11-02 16:31:01 +0800967 if (blt_ring_begin(ring, 4) == 0) {
968 intel_ring_emit(ring, MI_FLUSH_DW);
969 intel_ring_emit(ring, 0);
970 intel_ring_emit(ring, 0);
971 intel_ring_emit(ring, 0);
972 intel_ring_advance(ring);
973 }
974}
975
Zou Nan hai8d192152010-11-02 16:31:01 +0800976static void blt_ring_cleanup(struct intel_ring_buffer *ring)
977{
978 if (!ring->private)
979 return;
980
981 i915_gem_object_unpin(ring->private);
982 drm_gem_object_unreference(ring->private);
983 ring->private = NULL;
984}
985
Chris Wilson549f7362010-10-19 11:19:32 +0100986static const struct intel_ring_buffer gen6_blt_ring = {
987 .name = "blt ring",
988 .id = RING_BLT,
989 .mmio_base = BLT_RING_BASE,
990 .size = 32 * PAGE_SIZE,
Zou Nan hai8d192152010-11-02 16:31:01 +0800991 .init = blt_ring_init,
Chris Wilson297b0c52010-10-22 17:02:41 +0100992 .write_tail = ring_write_tail,
Zou Nan hai8d192152010-11-02 16:31:01 +0800993 .flush = blt_ring_flush,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000994 .add_request = gen6_add_request,
995 .get_seqno = ring_get_seqno,
996 .irq_get = blt_ring_get_irq,
997 .irq_put = blt_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +0100998 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Zou Nan hai8d192152010-11-02 16:31:01 +0800999 .cleanup = blt_ring_cleanup,
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001000};
1001
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001002int intel_init_render_ring_buffer(struct drm_device *dev)
1003{
1004 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001005 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001006
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001007 *ring = render_ring;
1008 if (INTEL_INFO(dev)->gen >= 6) {
1009 ring->add_request = gen6_add_request;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001010 }
1011
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001012 if (!I915_NEED_GFX_HWS(dev)) {
1013 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1014 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1015 }
1016
1017 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001018}
1019
1020int intel_init_bsd_ring_buffer(struct drm_device *dev)
1021{
1022 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001023 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001024
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001025 if (IS_GEN6(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001026 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001027 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001028 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001029
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001030 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001031}
Chris Wilson549f7362010-10-19 11:19:32 +01001032
1033int intel_init_blt_ring_buffer(struct drm_device *dev)
1034{
1035 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001036 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001037
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001038 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001039
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001040 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001041}