blob: 3a02425572204cd8bdc0172c70570e0dabe9ae0c [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"
Eric Anholt62fdfea2010-05-21 13:26:39 -070035
Chris Wilson6f392d52010-08-07 11:01:22 +010036static u32 i915_gem_get_seqno(struct drm_device *dev)
37{
38 drm_i915_private_t *dev_priv = dev->dev_private;
39 u32 seqno;
40
41 seqno = dev_priv->next_seqno;
42
43 /* reserve 0 for non-seqno */
44 if (++dev_priv->next_seqno == 0)
45 dev_priv->next_seqno = 1;
46
47 return seqno;
48}
49
Zou Nan hai8187a2b2010-05-21 09:08:55 +080050static void
51render_ring_flush(struct drm_device *dev,
52 struct intel_ring_buffer *ring,
53 u32 invalidate_domains,
54 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070055{
Chris Wilson6f392d52010-08-07 11:01:22 +010056 drm_i915_private_t *dev_priv = dev->dev_private;
57 u32 cmd;
58
Eric Anholt62fdfea2010-05-21 13:26:39 -070059#if WATCH_EXEC
60 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
61 invalidate_domains, flush_domains);
62#endif
Chris Wilson6f392d52010-08-07 11:01:22 +010063
64 trace_i915_gem_request_flush(dev, dev_priv->next_seqno,
Eric Anholt62fdfea2010-05-21 13:26:39 -070065 invalidate_domains, flush_domains);
66
Eric Anholt62fdfea2010-05-21 13:26:39 -070067 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
68 /*
69 * read/write caches:
70 *
71 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
72 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
73 * also flushed at 2d versus 3d pipeline switches.
74 *
75 * read-only caches:
76 *
77 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
78 * MI_READ_FLUSH is set, and is always flushed on 965.
79 *
80 * I915_GEM_DOMAIN_COMMAND may not exist?
81 *
82 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
83 * invalidated when MI_EXE_FLUSH is set.
84 *
85 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
86 * invalidated with every MI_FLUSH.
87 *
88 * TLBs:
89 *
90 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
91 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
92 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
93 * are flushed at any MI_FLUSH.
94 */
95
96 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
97 if ((invalidate_domains|flush_domains) &
98 I915_GEM_DOMAIN_RENDER)
99 cmd &= ~MI_NO_WRITE_FLUSH;
100 if (!IS_I965G(dev)) {
101 /*
102 * On the 965, the sampler cache always gets flushed
103 * and this bit is reserved.
104 */
105 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
106 cmd |= MI_READ_FLUSH;
107 }
108 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
109 cmd |= MI_EXE_FLUSH;
110
111#if WATCH_EXEC
112 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
113#endif
Zou Nan haibe26a102010-06-12 17:40:24 +0800114 intel_ring_begin(dev, ring, 2);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800115 intel_ring_emit(dev, ring, cmd);
116 intel_ring_emit(dev, ring, MI_NOOP);
117 intel_ring_advance(dev, ring);
118 }
119}
120
121static unsigned int render_ring_get_head(struct drm_device *dev,
122 struct intel_ring_buffer *ring)
123{
124 drm_i915_private_t *dev_priv = dev->dev_private;
125 return I915_READ(PRB0_HEAD) & HEAD_ADDR;
126}
127
128static unsigned int render_ring_get_tail(struct drm_device *dev,
129 struct intel_ring_buffer *ring)
130{
131 drm_i915_private_t *dev_priv = dev->dev_private;
132 return I915_READ(PRB0_TAIL) & TAIL_ADDR;
133}
134
135static unsigned int render_ring_get_active_head(struct drm_device *dev,
136 struct intel_ring_buffer *ring)
137{
138 drm_i915_private_t *dev_priv = dev->dev_private;
139 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
140
141 return I915_READ(acthd_reg);
142}
143
144static void render_ring_advance_ring(struct drm_device *dev,
145 struct intel_ring_buffer *ring)
146{
147 drm_i915_private_t *dev_priv = dev->dev_private;
148 I915_WRITE(PRB0_TAIL, ring->tail);
149}
150
151static int init_ring_common(struct drm_device *dev,
152 struct intel_ring_buffer *ring)
153{
154 u32 head;
155 drm_i915_private_t *dev_priv = dev->dev_private;
156 struct drm_i915_gem_object *obj_priv;
157 obj_priv = to_intel_bo(ring->gem_object);
158
159 /* Stop the ring if it's running. */
160 I915_WRITE(ring->regs.ctl, 0);
161 I915_WRITE(ring->regs.head, 0);
162 I915_WRITE(ring->regs.tail, 0);
163
164 /* Initialize the ring. */
165 I915_WRITE(ring->regs.start, obj_priv->gtt_offset);
166 head = ring->get_head(dev, ring);
167
168 /* G45 ring initialization fails to reset head to zero */
169 if (head != 0) {
170 DRM_ERROR("%s head not reset to zero "
171 "ctl %08x head %08x tail %08x start %08x\n",
172 ring->name,
173 I915_READ(ring->regs.ctl),
174 I915_READ(ring->regs.head),
175 I915_READ(ring->regs.tail),
176 I915_READ(ring->regs.start));
177
178 I915_WRITE(ring->regs.head, 0);
179
180 DRM_ERROR("%s head forced to zero "
181 "ctl %08x head %08x tail %08x start %08x\n",
182 ring->name,
183 I915_READ(ring->regs.ctl),
184 I915_READ(ring->regs.head),
185 I915_READ(ring->regs.tail),
186 I915_READ(ring->regs.start));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700187 }
188
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800189 I915_WRITE(ring->regs.ctl,
190 ((ring->gem_object->size - PAGE_SIZE) & RING_NR_PAGES)
191 | RING_NO_REPORT | RING_VALID);
192
193 head = I915_READ(ring->regs.head) & HEAD_ADDR;
194 /* If the head is still not zero, the ring is dead */
195 if (head != 0) {
196 DRM_ERROR("%s initialization failed "
197 "ctl %08x head %08x tail %08x start %08x\n",
198 ring->name,
199 I915_READ(ring->regs.ctl),
200 I915_READ(ring->regs.head),
201 I915_READ(ring->regs.tail),
202 I915_READ(ring->regs.start));
203 return -EIO;
204 }
205
206 if (!drm_core_check_feature(dev, DRIVER_MODESET))
207 i915_kernel_lost_context(dev);
208 else {
209 ring->head = ring->get_head(dev, ring);
210 ring->tail = ring->get_tail(dev, ring);
211 ring->space = ring->head - (ring->tail + 8);
212 if (ring->space < 0)
213 ring->space += ring->size;
214 }
215 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700216}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800217
218static int init_render_ring(struct drm_device *dev,
219 struct intel_ring_buffer *ring)
220{
221 drm_i915_private_t *dev_priv = dev->dev_private;
222 int ret = init_ring_common(dev, ring);
223 if (IS_I9XX(dev) && !IS_GEN3(dev)) {
224 I915_WRITE(MI_MODE,
225 (VS_TIMER_DISPATCH) << 16 | VS_TIMER_DISPATCH);
226 }
227 return ret;
228}
229
Eric Anholt62fdfea2010-05-21 13:26:39 -0700230#define PIPE_CONTROL_FLUSH(addr) \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800231do { \
Eric Anholt62fdfea2010-05-21 13:26:39 -0700232 OUT_RING(GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \
Zhenyu Wangca764822010-05-27 10:26:42 +0800233 PIPE_CONTROL_DEPTH_STALL | 2); \
Eric Anholt62fdfea2010-05-21 13:26:39 -0700234 OUT_RING(addr | PIPE_CONTROL_GLOBAL_GTT); \
235 OUT_RING(0); \
236 OUT_RING(0); \
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800237} while (0)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700238
239/**
240 * Creates a new sequence number, emitting a write of it to the status page
241 * plus an interrupt, which will trigger i915_user_interrupt_handler.
242 *
243 * Must be called with struct_lock held.
244 *
245 * Returned sequence numbers are nonzero on success.
246 */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800247static u32
248render_ring_add_request(struct drm_device *dev,
249 struct intel_ring_buffer *ring,
250 struct drm_file *file_priv,
251 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700252{
253 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson6f392d52010-08-07 11:01:22 +0100254 u32 seqno;
255
256 seqno = i915_gem_get_seqno(dev);
Zhenyu Wangca764822010-05-27 10:26:42 +0800257
258 if (IS_GEN6(dev)) {
259 BEGIN_LP_RING(6);
260 OUT_RING(GFX_OP_PIPE_CONTROL | 3);
261 OUT_RING(PIPE_CONTROL_QW_WRITE |
262 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_IS_FLUSH |
263 PIPE_CONTROL_NOTIFY);
264 OUT_RING(dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
265 OUT_RING(seqno);
266 OUT_RING(0);
267 OUT_RING(0);
268 ADVANCE_LP_RING();
269 } else if (HAS_PIPE_CONTROL(dev)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700270 u32 scratch_addr = dev_priv->seqno_gfx_addr + 128;
271
272 /*
273 * Workaround qword write incoherence by flushing the
274 * PIPE_NOTIFY buffers out to memory before requesting
275 * an interrupt.
276 */
277 BEGIN_LP_RING(32);
278 OUT_RING(GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
279 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH);
280 OUT_RING(dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
281 OUT_RING(seqno);
282 OUT_RING(0);
283 PIPE_CONTROL_FLUSH(scratch_addr);
284 scratch_addr += 128; /* write to separate cachelines */
285 PIPE_CONTROL_FLUSH(scratch_addr);
286 scratch_addr += 128;
287 PIPE_CONTROL_FLUSH(scratch_addr);
288 scratch_addr += 128;
289 PIPE_CONTROL_FLUSH(scratch_addr);
290 scratch_addr += 128;
291 PIPE_CONTROL_FLUSH(scratch_addr);
292 scratch_addr += 128;
293 PIPE_CONTROL_FLUSH(scratch_addr);
294 OUT_RING(GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE |
295 PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH |
296 PIPE_CONTROL_NOTIFY);
297 OUT_RING(dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT);
298 OUT_RING(seqno);
299 OUT_RING(0);
300 ADVANCE_LP_RING();
301 } else {
302 BEGIN_LP_RING(4);
303 OUT_RING(MI_STORE_DWORD_INDEX);
304 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
305 OUT_RING(seqno);
306
307 OUT_RING(MI_USER_INTERRUPT);
308 ADVANCE_LP_RING();
309 }
310 return seqno;
311}
312
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800313static u32
314render_ring_get_gem_seqno(struct drm_device *dev,
315 struct intel_ring_buffer *ring)
316{
317 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
318 if (HAS_PIPE_CONTROL(dev))
319 return ((volatile u32 *)(dev_priv->seqno_page))[0];
320 else
321 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
322}
323
324static void
325render_ring_get_user_irq(struct drm_device *dev,
326 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700327{
328 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
329 unsigned long irqflags;
330
331 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800332 if (dev->irq_enabled && (++ring->user_irq_refcount == 1)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700333 if (HAS_PCH_SPLIT(dev))
334 ironlake_enable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
335 else
336 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
337 }
338 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
339}
340
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800341static void
342render_ring_put_user_irq(struct drm_device *dev,
343 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700344{
345 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
346 unsigned long irqflags;
347
348 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800349 BUG_ON(dev->irq_enabled && ring->user_irq_refcount <= 0);
350 if (dev->irq_enabled && (--ring->user_irq_refcount == 0)) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700351 if (HAS_PCH_SPLIT(dev))
352 ironlake_disable_graphics_irq(dev_priv, GT_PIPE_NOTIFY);
353 else
354 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
355 }
356 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
357}
358
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800359static void render_setup_status_page(struct drm_device *dev,
360 struct intel_ring_buffer *ring)
361{
362 drm_i915_private_t *dev_priv = dev->dev_private;
363 if (IS_GEN6(dev)) {
364 I915_WRITE(HWS_PGA_GEN6, ring->status_page.gfx_addr);
365 I915_READ(HWS_PGA_GEN6); /* posting read */
366 } else {
367 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
368 I915_READ(HWS_PGA); /* posting read */
369 }
370
371}
372
Zou Nan haid1b851f2010-05-21 09:08:57 +0800373void
374bsd_ring_flush(struct drm_device *dev,
375 struct intel_ring_buffer *ring,
376 u32 invalidate_domains,
377 u32 flush_domains)
378{
Zou Nan haibe26a102010-06-12 17:40:24 +0800379 intel_ring_begin(dev, ring, 2);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800380 intel_ring_emit(dev, ring, MI_FLUSH);
381 intel_ring_emit(dev, ring, MI_NOOP);
382 intel_ring_advance(dev, ring);
383}
384
385static inline unsigned int bsd_ring_get_head(struct drm_device *dev,
386 struct intel_ring_buffer *ring)
387{
388 drm_i915_private_t *dev_priv = dev->dev_private;
389 return I915_READ(BSD_RING_HEAD) & HEAD_ADDR;
390}
391
392static inline unsigned int bsd_ring_get_tail(struct drm_device *dev,
393 struct intel_ring_buffer *ring)
394{
395 drm_i915_private_t *dev_priv = dev->dev_private;
396 return I915_READ(BSD_RING_TAIL) & TAIL_ADDR;
397}
398
399static inline unsigned int bsd_ring_get_active_head(struct drm_device *dev,
400 struct intel_ring_buffer *ring)
401{
402 drm_i915_private_t *dev_priv = dev->dev_private;
403 return I915_READ(BSD_RING_ACTHD);
404}
405
406static inline void bsd_ring_advance_ring(struct drm_device *dev,
407 struct intel_ring_buffer *ring)
408{
409 drm_i915_private_t *dev_priv = dev->dev_private;
410 I915_WRITE(BSD_RING_TAIL, ring->tail);
411}
412
413static int init_bsd_ring(struct drm_device *dev,
414 struct intel_ring_buffer *ring)
415{
416 return init_ring_common(dev, ring);
417}
418
419static u32
420bsd_ring_add_request(struct drm_device *dev,
421 struct intel_ring_buffer *ring,
422 struct drm_file *file_priv,
423 u32 flush_domains)
424{
425 u32 seqno;
Chris Wilson6f392d52010-08-07 11:01:22 +0100426
427 seqno = i915_gem_get_seqno(dev);
428
Zou Nan haid1b851f2010-05-21 09:08:57 +0800429 intel_ring_begin(dev, ring, 4);
430 intel_ring_emit(dev, ring, MI_STORE_DWORD_INDEX);
431 intel_ring_emit(dev, ring,
432 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
433 intel_ring_emit(dev, ring, seqno);
434 intel_ring_emit(dev, ring, MI_USER_INTERRUPT);
435 intel_ring_advance(dev, ring);
436
437 DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno);
438
439 return seqno;
440}
441
442static void bsd_setup_status_page(struct drm_device *dev,
443 struct intel_ring_buffer *ring)
444{
445 drm_i915_private_t *dev_priv = dev->dev_private;
446 I915_WRITE(BSD_HWS_PGA, ring->status_page.gfx_addr);
447 I915_READ(BSD_HWS_PGA);
448}
449
450static void
451bsd_ring_get_user_irq(struct drm_device *dev,
452 struct intel_ring_buffer *ring)
453{
454 /* do nothing */
455}
456static void
457bsd_ring_put_user_irq(struct drm_device *dev,
458 struct intel_ring_buffer *ring)
459{
460 /* do nothing */
461}
462
463static u32
464bsd_ring_get_gem_seqno(struct drm_device *dev,
465 struct intel_ring_buffer *ring)
466{
467 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
468}
469
470static int
471bsd_ring_dispatch_gem_execbuffer(struct drm_device *dev,
472 struct intel_ring_buffer *ring,
473 struct drm_i915_gem_execbuffer2 *exec,
474 struct drm_clip_rect *cliprects,
475 uint64_t exec_offset)
476{
477 uint32_t exec_start;
478 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
479 intel_ring_begin(dev, ring, 2);
480 intel_ring_emit(dev, ring, MI_BATCH_BUFFER_START |
481 (2 << 6) | MI_BATCH_NON_SECURE_I965);
482 intel_ring_emit(dev, ring, exec_start);
483 intel_ring_advance(dev, ring);
484 return 0;
485}
486
487
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800488static int
489render_ring_dispatch_gem_execbuffer(struct drm_device *dev,
490 struct intel_ring_buffer *ring,
491 struct drm_i915_gem_execbuffer2 *exec,
492 struct drm_clip_rect *cliprects,
493 uint64_t exec_offset)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700494{
495 drm_i915_private_t *dev_priv = dev->dev_private;
496 int nbox = exec->num_cliprects;
497 int i = 0, count;
498 uint32_t exec_start, exec_len;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700499 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
500 exec_len = (uint32_t) exec->batch_len;
501
Chris Wilson6f392d52010-08-07 11:01:22 +0100502 trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700503
504 count = nbox ? nbox : 1;
505
506 for (i = 0; i < count; i++) {
507 if (i < nbox) {
508 int ret = i915_emit_box(dev, cliprects, i,
509 exec->DR1, exec->DR4);
510 if (ret)
511 return ret;
512 }
513
514 if (IS_I830(dev) || IS_845G(dev)) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800515 intel_ring_begin(dev, ring, 4);
516 intel_ring_emit(dev, ring, MI_BATCH_BUFFER);
517 intel_ring_emit(dev, ring,
518 exec_start | MI_BATCH_NON_SECURE);
519 intel_ring_emit(dev, ring, exec_start + exec_len - 4);
520 intel_ring_emit(dev, ring, 0);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700521 } else {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800522 intel_ring_begin(dev, ring, 4);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700523 if (IS_I965G(dev)) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800524 intel_ring_emit(dev, ring,
525 MI_BATCH_BUFFER_START | (2 << 6)
526 | MI_BATCH_NON_SECURE_I965);
527 intel_ring_emit(dev, ring, exec_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700528 } else {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800529 intel_ring_emit(dev, ring, MI_BATCH_BUFFER_START
530 | (2 << 6));
531 intel_ring_emit(dev, ring, exec_start |
532 MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700533 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700534 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800535 intel_ring_advance(dev, ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700536 }
537
538 /* XXX breadcrumb */
539 return 0;
540}
541
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800542static void cleanup_status_page(struct drm_device *dev,
543 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700544{
545 drm_i915_private_t *dev_priv = dev->dev_private;
546 struct drm_gem_object *obj;
547 struct drm_i915_gem_object *obj_priv;
548
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800549 obj = ring->status_page.obj;
550 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700551 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700552 obj_priv = to_intel_bo(obj);
553
554 kunmap(obj_priv->pages[0]);
555 i915_gem_object_unpin(obj);
556 drm_gem_object_unreference(obj);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800557 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700558
559 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700560}
561
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800562static int init_status_page(struct drm_device *dev,
563 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700564{
565 drm_i915_private_t *dev_priv = dev->dev_private;
566 struct drm_gem_object *obj;
567 struct drm_i915_gem_object *obj_priv;
568 int ret;
569
Eric Anholt62fdfea2010-05-21 13:26:39 -0700570 obj = i915_gem_alloc_object(dev, 4096);
571 if (obj == NULL) {
572 DRM_ERROR("Failed to allocate status page\n");
573 ret = -ENOMEM;
574 goto err;
575 }
576 obj_priv = to_intel_bo(obj);
577 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
578
579 ret = i915_gem_object_pin(obj, 4096);
580 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700581 goto err_unref;
582 }
583
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800584 ring->status_page.gfx_addr = obj_priv->gtt_offset;
585 ring->status_page.page_addr = kmap(obj_priv->pages[0]);
586 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700587 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700588 goto err_unpin;
589 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800590 ring->status_page.obj = obj;
591 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700592
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800593 ring->setup_status_page(dev, ring);
594 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
595 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700596
597 return 0;
598
599err_unpin:
600 i915_gem_object_unpin(obj);
601err_unref:
602 drm_gem_object_unreference(obj);
603err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800604 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700605}
606
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800607
608int intel_init_ring_buffer(struct drm_device *dev,
609 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700610{
Eric Anholt62fdfea2010-05-21 13:26:39 -0700611 int ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800612 struct drm_i915_gem_object *obj_priv;
613 struct drm_gem_object *obj;
614 ring->dev = dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700615
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800616 if (I915_NEED_GFX_HWS(dev)) {
617 ret = init_status_page(dev, ring);
618 if (ret)
619 return ret;
620 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700621
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800622 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700623 if (obj == NULL) {
624 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800625 ret = -ENOMEM;
626 goto cleanup;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700627 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700628
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800629 ring->gem_object = obj;
630
631 ret = i915_gem_object_pin(obj, ring->alignment);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700632 if (ret != 0) {
633 drm_gem_object_unreference(obj);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800634 goto cleanup;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700635 }
636
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800637 obj_priv = to_intel_bo(obj);
638 ring->map.size = ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700639 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700640 ring->map.type = 0;
641 ring->map.flags = 0;
642 ring->map.mtrr = 0;
643
644 drm_core_ioremap_wc(&ring->map, dev);
645 if (ring->map.handle == NULL) {
646 DRM_ERROR("Failed to map ringbuffer.\n");
Eric Anholt62fdfea2010-05-21 13:26:39 -0700647 i915_gem_object_unpin(obj);
648 drm_gem_object_unreference(obj);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800649 ret = -EINVAL;
650 goto cleanup;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700651 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800652
Eric Anholt62fdfea2010-05-21 13:26:39 -0700653 ring->virtual_start = ring->map.handle;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800654 ret = ring->init(dev, ring);
655 if (ret != 0) {
656 intel_cleanup_ring_buffer(dev, ring);
657 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700658 }
659
Eric Anholt62fdfea2010-05-21 13:26:39 -0700660 if (!drm_core_check_feature(dev, DRIVER_MODESET))
661 i915_kernel_lost_context(dev);
662 else {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800663 ring->head = ring->get_head(dev, ring);
664 ring->tail = ring->get_tail(dev, ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700665 ring->space = ring->head - (ring->tail + 8);
666 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800667 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700668 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800669 INIT_LIST_HEAD(&ring->active_list);
670 INIT_LIST_HEAD(&ring->request_list);
671 return ret;
672cleanup:
673 cleanup_status_page(dev, ring);
674 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700675}
676
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800677void intel_cleanup_ring_buffer(struct drm_device *dev,
678 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700679{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800680 if (ring->gem_object == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700681 return;
682
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800683 drm_core_ioremapfree(&ring->map, dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700684
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800685 i915_gem_object_unpin(ring->gem_object);
686 drm_gem_object_unreference(ring->gem_object);
687 ring->gem_object = NULL;
688 cleanup_status_page(dev, ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700689}
690
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800691int intel_wrap_ring_buffer(struct drm_device *dev,
692 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700693{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800694 unsigned int *virt;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700695 int rem;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800696 rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700697
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800698 if (ring->space < rem) {
699 int ret = intel_wait_ring_buffer(dev, ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700700 if (ret)
701 return ret;
702 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700703
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800704 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +0100705 rem /= 8;
706 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700707 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +0100708 *virt++ = MI_NOOP;
709 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700710
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800711 ring->tail = 0;
Chris Wilson43ed3402010-07-01 17:53:00 +0100712 ring->space = ring->head - 8;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700713
714 return 0;
715}
716
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800717int intel_wait_ring_buffer(struct drm_device *dev,
718 struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700719{
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800720 unsigned long end;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700721
722 trace_i915_ring_wait_begin (dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800723 end = jiffies + 3 * HZ;
724 do {
725 ring->head = ring->get_head(dev, ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700726 ring->space = ring->head - (ring->tail + 8);
727 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800728 ring->space += ring->size;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700729 if (ring->space >= n) {
730 trace_i915_ring_wait_end (dev);
731 return 0;
732 }
733
734 if (dev->primary->master) {
735 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
736 if (master_priv->sarea_priv)
737 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
738 }
Zou Nan haid1b851f2010-05-21 09:08:57 +0800739
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800740 yield();
741 } while (!time_after(jiffies, end));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700742 trace_i915_ring_wait_end (dev);
743 return -EBUSY;
744}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800745
746void intel_ring_begin(struct drm_device *dev,
Zou Nan haibe26a102010-06-12 17:40:24 +0800747 struct intel_ring_buffer *ring, int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800748{
Zou Nan haibe26a102010-06-12 17:40:24 +0800749 int n = 4*num_dwords;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800750 if (unlikely(ring->tail + n > ring->size))
751 intel_wrap_ring_buffer(dev, ring);
752 if (unlikely(ring->space < n))
753 intel_wait_ring_buffer(dev, ring, n);
Chris Wilsond97ed332010-08-04 15:18:13 +0100754
755 ring->space -= n;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800756}
757
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800758void intel_ring_advance(struct drm_device *dev,
759 struct intel_ring_buffer *ring)
760{
Chris Wilsond97ed332010-08-04 15:18:13 +0100761 ring->tail &= ring->size - 1;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800762 ring->advance_ring(dev, ring);
763}
764
765void intel_fill_struct(struct drm_device *dev,
766 struct intel_ring_buffer *ring,
767 void *data,
768 unsigned int len)
769{
770 unsigned int *virt = ring->virtual_start + ring->tail;
771 BUG_ON((len&~(4-1)) != 0);
Zou Nan haibe26a102010-06-12 17:40:24 +0800772 intel_ring_begin(dev, ring, len/4);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800773 memcpy(virt, data, len);
774 ring->tail += len;
775 ring->tail &= ring->size - 1;
776 ring->space -= len;
777 intel_ring_advance(dev, ring);
778}
779
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800780struct intel_ring_buffer render_ring = {
781 .name = "render ring",
782 .regs = {
783 .ctl = PRB0_CTL,
784 .head = PRB0_HEAD,
785 .tail = PRB0_TAIL,
786 .start = PRB0_START
787 },
788 .ring_flag = I915_EXEC_RENDER,
789 .size = 32 * PAGE_SIZE,
790 .alignment = PAGE_SIZE,
791 .virtual_start = NULL,
792 .dev = NULL,
793 .gem_object = NULL,
794 .head = 0,
795 .tail = 0,
796 .space = 0,
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800797 .user_irq_refcount = 0,
798 .irq_gem_seqno = 0,
799 .waiting_gem_seqno = 0,
800 .setup_status_page = render_setup_status_page,
801 .init = init_render_ring,
802 .get_head = render_ring_get_head,
803 .get_tail = render_ring_get_tail,
804 .get_active_head = render_ring_get_active_head,
805 .advance_ring = render_ring_advance_ring,
806 .flush = render_ring_flush,
807 .add_request = render_ring_add_request,
808 .get_gem_seqno = render_ring_get_gem_seqno,
809 .user_irq_get = render_ring_get_user_irq,
810 .user_irq_put = render_ring_put_user_irq,
811 .dispatch_gem_execbuffer = render_ring_dispatch_gem_execbuffer,
812 .status_page = {NULL, 0, NULL},
813 .map = {0,}
814};
Zou Nan haid1b851f2010-05-21 09:08:57 +0800815
816/* ring buffer for bit-stream decoder */
817
818struct intel_ring_buffer bsd_ring = {
819 .name = "bsd ring",
820 .regs = {
821 .ctl = BSD_RING_CTL,
822 .head = BSD_RING_HEAD,
823 .tail = BSD_RING_TAIL,
824 .start = BSD_RING_START
825 },
826 .ring_flag = I915_EXEC_BSD,
827 .size = 32 * PAGE_SIZE,
828 .alignment = PAGE_SIZE,
829 .virtual_start = NULL,
830 .dev = NULL,
831 .gem_object = NULL,
832 .head = 0,
833 .tail = 0,
834 .space = 0,
Zou Nan haid1b851f2010-05-21 09:08:57 +0800835 .user_irq_refcount = 0,
836 .irq_gem_seqno = 0,
837 .waiting_gem_seqno = 0,
838 .setup_status_page = bsd_setup_status_page,
839 .init = init_bsd_ring,
840 .get_head = bsd_ring_get_head,
841 .get_tail = bsd_ring_get_tail,
842 .get_active_head = bsd_ring_get_active_head,
843 .advance_ring = bsd_ring_advance_ring,
844 .flush = bsd_ring_flush,
845 .add_request = bsd_ring_add_request,
846 .get_gem_seqno = bsd_ring_get_gem_seqno,
847 .user_irq_get = bsd_ring_get_user_irq,
848 .user_irq_put = bsd_ring_put_user_irq,
849 .dispatch_gem_execbuffer = bsd_ring_dispatch_gem_execbuffer,
850 .status_page = {NULL, 0, NULL},
851 .map = {0,}
852};