2 * Copyright © 2008-2010 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
34 #include "i915_trace.h"
35 #include "intel_drv.h"
38 * 965+ support PIPE_CONTROL commands, which provide finer grained control
39 * over cache flushing.
42 struct drm_i915_gem_object *obj;
43 volatile u32 *cpu_page;
47 static inline int ring_space(struct intel_ring_buffer *ring)
49 int space = (ring->head & HEAD_ADDR) - (ring->tail + 8);
56 gen2_render_ring_flush(struct intel_ring_buffer *ring,
57 u32 invalidate_domains,
64 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
65 cmd |= MI_NO_WRITE_FLUSH;
67 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
70 ret = intel_ring_begin(ring, 2);
74 intel_ring_emit(ring, cmd);
75 intel_ring_emit(ring, MI_NOOP);
76 intel_ring_advance(ring);
82 gen4_render_ring_flush(struct intel_ring_buffer *ring,
83 u32 invalidate_domains,
86 struct drm_device *dev = ring->dev;
93 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
94 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
95 * also flushed at 2d versus 3d pipeline switches.
99 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
100 * MI_READ_FLUSH is set, and is always flushed on 965.
102 * I915_GEM_DOMAIN_COMMAND may not exist?
104 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
105 * invalidated when MI_EXE_FLUSH is set.
107 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
108 * invalidated with every MI_FLUSH.
112 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
113 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
114 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
115 * are flushed at any MI_FLUSH.
118 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
119 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
120 cmd &= ~MI_NO_WRITE_FLUSH;
121 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
124 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
125 (IS_G4X(dev) || IS_GEN5(dev)))
126 cmd |= MI_INVALIDATE_ISP;
128 ret = intel_ring_begin(ring, 2);
132 intel_ring_emit(ring, cmd);
133 intel_ring_emit(ring, MI_NOOP);
134 intel_ring_advance(ring);
140 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
141 * implementing two workarounds on gen6. From section 1.4.7.1
142 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
144 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
145 * produced by non-pipelined state commands), software needs to first
146 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
149 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
150 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
152 * And the workaround for these two requires this workaround first:
154 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
155 * BEFORE the pipe-control with a post-sync op and no write-cache
158 * And this last workaround is tricky because of the requirements on
159 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
162 * "1 of the following must also be set:
163 * - Render Target Cache Flush Enable ([12] of DW1)
164 * - Depth Cache Flush Enable ([0] of DW1)
165 * - Stall at Pixel Scoreboard ([1] of DW1)
166 * - Depth Stall ([13] of DW1)
167 * - Post-Sync Operation ([13] of DW1)
168 * - Notify Enable ([8] of DW1)"
170 * The cache flushes require the workaround flush that triggered this
171 * one, so we can't use it. Depth stall would trigger the same.
172 * Post-sync nonzero is what triggered this second workaround, so we
173 * can't use that one either. Notify enable is IRQs, which aren't
174 * really our business. That leaves only stall at scoreboard.
177 intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
179 struct pipe_control *pc = ring->private;
180 u32 scratch_addr = pc->gtt_offset + 128;
184 ret = intel_ring_begin(ring, 6);
188 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
189 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
190 PIPE_CONTROL_STALL_AT_SCOREBOARD);
191 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
192 intel_ring_emit(ring, 0); /* low dword */
193 intel_ring_emit(ring, 0); /* high dword */
194 intel_ring_emit(ring, MI_NOOP);
195 intel_ring_advance(ring);
197 ret = intel_ring_begin(ring, 6);
201 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
202 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
203 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
204 intel_ring_emit(ring, 0);
205 intel_ring_emit(ring, 0);
206 intel_ring_emit(ring, MI_NOOP);
207 intel_ring_advance(ring);
213 gen6_render_ring_flush(struct intel_ring_buffer *ring,
214 u32 invalidate_domains, u32 flush_domains)
217 struct pipe_control *pc = ring->private;
218 u32 scratch_addr = pc->gtt_offset + 128;
221 /* Force SNB workarounds for PIPE_CONTROL flushes */
222 intel_emit_post_sync_nonzero_flush(ring);
224 /* Just flush everything. Experiments have shown that reducing the
225 * number of bits based on the write domains has little performance
228 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
229 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
230 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
231 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
232 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
233 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
234 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
236 ret = intel_ring_begin(ring, 6);
240 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
241 intel_ring_emit(ring, flags);
242 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
243 intel_ring_emit(ring, 0); /* lower dword */
244 intel_ring_emit(ring, 0); /* uppwer dword */
245 intel_ring_emit(ring, MI_NOOP);
246 intel_ring_advance(ring);
251 static void ring_write_tail(struct intel_ring_buffer *ring,
254 drm_i915_private_t *dev_priv = ring->dev->dev_private;
255 I915_WRITE_TAIL(ring, value);
258 u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
260 drm_i915_private_t *dev_priv = ring->dev->dev_private;
261 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
262 RING_ACTHD(ring->mmio_base) : ACTHD;
264 return I915_READ(acthd_reg);
267 static int init_ring_common(struct intel_ring_buffer *ring)
269 drm_i915_private_t *dev_priv = ring->dev->dev_private;
270 struct drm_i915_gem_object *obj = ring->obj;
273 /* Stop the ring if it's running. */
274 I915_WRITE_CTL(ring, 0);
275 I915_WRITE_HEAD(ring, 0);
276 ring->write_tail(ring, 0);
278 /* Initialize the ring. */
279 I915_WRITE_START(ring, obj->gtt_offset);
280 head = I915_READ_HEAD(ring) & HEAD_ADDR;
282 /* G45 ring initialization fails to reset head to zero */
284 DRM_DEBUG_KMS("%s head not reset to zero "
285 "ctl %08x head %08x tail %08x start %08x\n",
288 I915_READ_HEAD(ring),
289 I915_READ_TAIL(ring),
290 I915_READ_START(ring));
292 I915_WRITE_HEAD(ring, 0);
294 if (I915_READ_HEAD(ring) & HEAD_ADDR) {
295 DRM_ERROR("failed to set %s head to zero "
296 "ctl %08x head %08x tail %08x start %08x\n",
299 I915_READ_HEAD(ring),
300 I915_READ_TAIL(ring),
301 I915_READ_START(ring));
306 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
309 /* If the head is still not zero, the ring is dead */
310 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
311 I915_READ_START(ring) == obj->gtt_offset &&
312 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
313 DRM_ERROR("%s initialization failed "
314 "ctl %08x head %08x tail %08x start %08x\n",
317 I915_READ_HEAD(ring),
318 I915_READ_TAIL(ring),
319 I915_READ_START(ring));
323 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
324 i915_kernel_lost_context(ring->dev);
326 ring->head = I915_READ_HEAD(ring);
327 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
328 ring->space = ring_space(ring);
329 ring->last_retired_head = -1;
336 init_pipe_control(struct intel_ring_buffer *ring)
338 struct pipe_control *pc;
339 struct drm_i915_gem_object *obj;
345 pc = kmalloc(sizeof(*pc), GFP_KERNEL);
349 obj = i915_gem_alloc_object(ring->dev, 4096);
351 DRM_ERROR("Failed to allocate seqno page\n");
356 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
358 ret = i915_gem_object_pin(obj, 4096, true);
362 pc->gtt_offset = obj->gtt_offset;
363 pc->cpu_page = kmap(obj->pages[0]);
364 if (pc->cpu_page == NULL)
372 i915_gem_object_unpin(obj);
374 drm_gem_object_unreference(&obj->base);
381 cleanup_pipe_control(struct intel_ring_buffer *ring)
383 struct pipe_control *pc = ring->private;
384 struct drm_i915_gem_object *obj;
390 kunmap(obj->pages[0]);
391 i915_gem_object_unpin(obj);
392 drm_gem_object_unreference(&obj->base);
395 ring->private = NULL;
398 static int init_render_ring(struct intel_ring_buffer *ring)
400 struct drm_device *dev = ring->dev;
401 struct drm_i915_private *dev_priv = dev->dev_private;
402 int ret = init_ring_common(ring);
404 if (INTEL_INFO(dev)->gen > 3) {
405 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
407 I915_WRITE(GFX_MODE_GEN7,
408 _MASKED_BIT_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
409 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
412 if (INTEL_INFO(dev)->gen >= 5) {
413 ret = init_pipe_control(ring);
419 /* From the Sandybridge PRM, volume 1 part 3, page 24:
420 * "If this bit is set, STCunit will have LRA as replacement
421 * policy. [...] This bit must be reset. LRA replacement
422 * policy is not supported."
424 I915_WRITE(CACHE_MODE_0,
425 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
428 if (INTEL_INFO(dev)->gen >= 6)
429 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
434 static void render_ring_cleanup(struct intel_ring_buffer *ring)
439 cleanup_pipe_control(ring);
443 update_mboxes(struct intel_ring_buffer *ring,
447 intel_ring_emit(ring, MI_SEMAPHORE_MBOX |
448 MI_SEMAPHORE_GLOBAL_GTT |
449 MI_SEMAPHORE_REGISTER |
450 MI_SEMAPHORE_UPDATE);
451 intel_ring_emit(ring, seqno);
452 intel_ring_emit(ring, mmio_offset);
456 * gen6_add_request - Update the semaphore mailbox registers
458 * @ring - ring that is adding a request
459 * @seqno - return seqno stuck into the ring
461 * Update the mailbox registers in the *other* rings with the current seqno.
462 * This acts like a signal in the canonical semaphore.
465 gen6_add_request(struct intel_ring_buffer *ring,
472 ret = intel_ring_begin(ring, 10);
476 mbox1_reg = ring->signal_mbox[0];
477 mbox2_reg = ring->signal_mbox[1];
479 *seqno = i915_gem_next_request_seqno(ring);
481 update_mboxes(ring, *seqno, mbox1_reg);
482 update_mboxes(ring, *seqno, mbox2_reg);
483 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
484 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
485 intel_ring_emit(ring, *seqno);
486 intel_ring_emit(ring, MI_USER_INTERRUPT);
487 intel_ring_advance(ring);
493 * intel_ring_sync - sync the waiter to the signaller on seqno
495 * @waiter - ring that is waiting
496 * @signaller - ring which has, or will signal
497 * @seqno - seqno which the waiter will block on
500 gen6_ring_sync(struct intel_ring_buffer *waiter,
501 struct intel_ring_buffer *signaller,
505 u32 dw1 = MI_SEMAPHORE_MBOX |
506 MI_SEMAPHORE_COMPARE |
507 MI_SEMAPHORE_REGISTER;
509 /* Throughout all of the GEM code, seqno passed implies our current
510 * seqno is >= the last seqno executed. However for hardware the
511 * comparison is strictly greater than.
515 WARN_ON(signaller->semaphore_register[waiter->id] ==
516 MI_SEMAPHORE_SYNC_INVALID);
518 ret = intel_ring_begin(waiter, 4);
522 intel_ring_emit(waiter,
523 dw1 | signaller->semaphore_register[waiter->id]);
524 intel_ring_emit(waiter, seqno);
525 intel_ring_emit(waiter, 0);
526 intel_ring_emit(waiter, MI_NOOP);
527 intel_ring_advance(waiter);
532 #define PIPE_CONTROL_FLUSH(ring__, addr__) \
534 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
535 PIPE_CONTROL_DEPTH_STALL); \
536 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
537 intel_ring_emit(ring__, 0); \
538 intel_ring_emit(ring__, 0); \
542 pc_render_add_request(struct intel_ring_buffer *ring,
545 u32 seqno = i915_gem_next_request_seqno(ring);
546 struct pipe_control *pc = ring->private;
547 u32 scratch_addr = pc->gtt_offset + 128;
550 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
551 * incoherent with writes to memory, i.e. completely fubar,
552 * so we need to use PIPE_NOTIFY instead.
554 * However, we also need to workaround the qword write
555 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
556 * memory before requesting an interrupt.
558 ret = intel_ring_begin(ring, 32);
562 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
563 PIPE_CONTROL_WRITE_FLUSH |
564 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
565 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
566 intel_ring_emit(ring, seqno);
567 intel_ring_emit(ring, 0);
568 PIPE_CONTROL_FLUSH(ring, scratch_addr);
569 scratch_addr += 128; /* write to separate cachelines */
570 PIPE_CONTROL_FLUSH(ring, scratch_addr);
572 PIPE_CONTROL_FLUSH(ring, scratch_addr);
574 PIPE_CONTROL_FLUSH(ring, scratch_addr);
576 PIPE_CONTROL_FLUSH(ring, scratch_addr);
578 PIPE_CONTROL_FLUSH(ring, scratch_addr);
580 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
581 PIPE_CONTROL_WRITE_FLUSH |
582 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
583 PIPE_CONTROL_NOTIFY);
584 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
585 intel_ring_emit(ring, seqno);
586 intel_ring_emit(ring, 0);
587 intel_ring_advance(ring);
594 gen6_ring_get_seqno(struct intel_ring_buffer *ring)
596 struct drm_device *dev = ring->dev;
598 /* Workaround to force correct ordering between irq and seqno writes on
599 * ivb (and maybe also on snb) by reading from a CS register (like
600 * ACTHD) before reading the status page. */
601 if (IS_GEN6(dev) || IS_GEN7(dev))
602 intel_ring_get_active_head(ring);
603 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
607 ring_get_seqno(struct intel_ring_buffer *ring)
609 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
613 pc_render_get_seqno(struct intel_ring_buffer *ring)
615 struct pipe_control *pc = ring->private;
616 return pc->cpu_page[0];
620 gen5_ring_get_irq(struct intel_ring_buffer *ring)
622 struct drm_device *dev = ring->dev;
623 drm_i915_private_t *dev_priv = dev->dev_private;
626 if (!dev->irq_enabled)
629 spin_lock_irqsave(&dev_priv->irq_lock, flags);
630 if (ring->irq_refcount++ == 0) {
631 dev_priv->gt_irq_mask &= ~ring->irq_enable_mask;
632 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
635 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
641 gen5_ring_put_irq(struct intel_ring_buffer *ring)
643 struct drm_device *dev = ring->dev;
644 drm_i915_private_t *dev_priv = dev->dev_private;
647 spin_lock_irqsave(&dev_priv->irq_lock, flags);
648 if (--ring->irq_refcount == 0) {
649 dev_priv->gt_irq_mask |= ring->irq_enable_mask;
650 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
653 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
657 i9xx_ring_get_irq(struct intel_ring_buffer *ring)
659 struct drm_device *dev = ring->dev;
660 drm_i915_private_t *dev_priv = dev->dev_private;
663 if (!dev->irq_enabled)
666 spin_lock_irqsave(&dev_priv->irq_lock, flags);
667 if (ring->irq_refcount++ == 0) {
668 dev_priv->irq_mask &= ~ring->irq_enable_mask;
669 I915_WRITE(IMR, dev_priv->irq_mask);
672 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
678 i9xx_ring_put_irq(struct intel_ring_buffer *ring)
680 struct drm_device *dev = ring->dev;
681 drm_i915_private_t *dev_priv = dev->dev_private;
684 spin_lock_irqsave(&dev_priv->irq_lock, flags);
685 if (--ring->irq_refcount == 0) {
686 dev_priv->irq_mask |= ring->irq_enable_mask;
687 I915_WRITE(IMR, dev_priv->irq_mask);
690 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
694 i8xx_ring_get_irq(struct intel_ring_buffer *ring)
696 struct drm_device *dev = ring->dev;
697 drm_i915_private_t *dev_priv = dev->dev_private;
700 if (!dev->irq_enabled)
703 spin_lock_irqsave(&dev_priv->irq_lock, flags);
704 if (ring->irq_refcount++ == 0) {
705 dev_priv->irq_mask &= ~ring->irq_enable_mask;
706 I915_WRITE16(IMR, dev_priv->irq_mask);
709 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
715 i8xx_ring_put_irq(struct intel_ring_buffer *ring)
717 struct drm_device *dev = ring->dev;
718 drm_i915_private_t *dev_priv = dev->dev_private;
721 spin_lock_irqsave(&dev_priv->irq_lock, flags);
722 if (--ring->irq_refcount == 0) {
723 dev_priv->irq_mask |= ring->irq_enable_mask;
724 I915_WRITE16(IMR, dev_priv->irq_mask);
727 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
730 void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
732 struct drm_device *dev = ring->dev;
733 drm_i915_private_t *dev_priv = ring->dev->dev_private;
736 /* The ring status page addresses are no longer next to the rest of
737 * the ring registers as of gen7.
742 mmio = RENDER_HWS_PGA_GEN7;
745 mmio = BLT_HWS_PGA_GEN7;
748 mmio = BSD_HWS_PGA_GEN7;
751 } else if (IS_GEN6(ring->dev)) {
752 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
754 mmio = RING_HWS_PGA(ring->mmio_base);
757 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
762 bsd_ring_flush(struct intel_ring_buffer *ring,
763 u32 invalidate_domains,
768 ret = intel_ring_begin(ring, 2);
772 intel_ring_emit(ring, MI_FLUSH);
773 intel_ring_emit(ring, MI_NOOP);
774 intel_ring_advance(ring);
779 i9xx_add_request(struct intel_ring_buffer *ring,
785 ret = intel_ring_begin(ring, 4);
789 seqno = i915_gem_next_request_seqno(ring);
791 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
792 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
793 intel_ring_emit(ring, seqno);
794 intel_ring_emit(ring, MI_USER_INTERRUPT);
795 intel_ring_advance(ring);
802 gen6_ring_get_irq(struct intel_ring_buffer *ring)
804 struct drm_device *dev = ring->dev;
805 drm_i915_private_t *dev_priv = dev->dev_private;
808 if (!dev->irq_enabled)
811 /* It looks like we need to prevent the gt from suspending while waiting
812 * for an notifiy irq, otherwise irqs seem to get lost on at least the
813 * blt/bsd rings on ivb. */
814 gen6_gt_force_wake_get(dev_priv);
816 spin_lock_irqsave(&dev_priv->irq_lock, flags);
817 if (ring->irq_refcount++ == 0) {
818 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
819 dev_priv->gt_irq_mask &= ~ring->irq_enable_mask;
820 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
823 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
829 gen6_ring_put_irq(struct intel_ring_buffer *ring)
831 struct drm_device *dev = ring->dev;
832 drm_i915_private_t *dev_priv = dev->dev_private;
835 spin_lock_irqsave(&dev_priv->irq_lock, flags);
836 if (--ring->irq_refcount == 0) {
837 I915_WRITE_IMR(ring, ~0);
838 dev_priv->gt_irq_mask |= ring->irq_enable_mask;
839 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
842 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
844 gen6_gt_force_wake_put(dev_priv);
848 i965_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
852 ret = intel_ring_begin(ring, 2);
856 intel_ring_emit(ring,
857 MI_BATCH_BUFFER_START |
859 MI_BATCH_NON_SECURE_I965);
860 intel_ring_emit(ring, offset);
861 intel_ring_advance(ring);
867 i830_dispatch_execbuffer(struct intel_ring_buffer *ring,
872 ret = intel_ring_begin(ring, 4);
876 intel_ring_emit(ring, MI_BATCH_BUFFER);
877 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
878 intel_ring_emit(ring, offset + len - 8);
879 intel_ring_emit(ring, 0);
880 intel_ring_advance(ring);
886 i915_dispatch_execbuffer(struct intel_ring_buffer *ring,
891 ret = intel_ring_begin(ring, 2);
895 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
896 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
897 intel_ring_advance(ring);
902 static void cleanup_status_page(struct intel_ring_buffer *ring)
904 struct drm_i915_gem_object *obj;
906 obj = ring->status_page.obj;
910 kunmap(obj->pages[0]);
911 i915_gem_object_unpin(obj);
912 drm_gem_object_unreference(&obj->base);
913 ring->status_page.obj = NULL;
916 static int init_status_page(struct intel_ring_buffer *ring)
918 struct drm_device *dev = ring->dev;
919 struct drm_i915_gem_object *obj;
922 obj = i915_gem_alloc_object(dev, 4096);
924 DRM_ERROR("Failed to allocate status page\n");
929 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
931 ret = i915_gem_object_pin(obj, 4096, true);
936 ring->status_page.gfx_addr = obj->gtt_offset;
937 ring->status_page.page_addr = kmap(obj->pages[0]);
938 if (ring->status_page.page_addr == NULL) {
941 ring->status_page.obj = obj;
942 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
944 intel_ring_setup_status_page(ring);
945 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
946 ring->name, ring->status_page.gfx_addr);
951 i915_gem_object_unpin(obj);
953 drm_gem_object_unreference(&obj->base);
958 static int intel_init_ring_buffer(struct drm_device *dev,
959 struct intel_ring_buffer *ring)
961 struct drm_i915_gem_object *obj;
965 INIT_LIST_HEAD(&ring->active_list);
966 INIT_LIST_HEAD(&ring->request_list);
967 INIT_LIST_HEAD(&ring->gpu_write_list);
968 ring->size = 32 * PAGE_SIZE;
970 init_waitqueue_head(&ring->irq_queue);
972 if (I915_NEED_GFX_HWS(dev)) {
973 ret = init_status_page(ring);
978 obj = i915_gem_alloc_object(dev, ring->size);
980 DRM_ERROR("Failed to allocate ringbuffer\n");
987 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
991 ret = i915_gem_object_set_to_gtt_domain(obj, true);
995 ring->virtual_start = ioremap_wc(dev->agp->base + obj->gtt_offset,
997 if (ring->virtual_start == NULL) {
998 DRM_ERROR("Failed to map ringbuffer.\n");
1003 ret = ring->init(ring);
1007 /* Workaround an erratum on the i830 which causes a hang if
1008 * the TAIL pointer points to within the last 2 cachelines
1011 ring->effective_size = ring->size;
1012 if (IS_I830(ring->dev) || IS_845G(ring->dev))
1013 ring->effective_size -= 128;
1018 iounmap(ring->virtual_start);
1020 i915_gem_object_unpin(obj);
1022 drm_gem_object_unreference(&obj->base);
1025 cleanup_status_page(ring);
1029 void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
1031 struct drm_i915_private *dev_priv;
1034 if (ring->obj == NULL)
1037 /* Disable the ring buffer. The ring must be idle at this point */
1038 dev_priv = ring->dev->dev_private;
1039 ret = intel_wait_ring_idle(ring);
1041 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
1044 I915_WRITE_CTL(ring, 0);
1046 iounmap(ring->virtual_start);
1048 i915_gem_object_unpin(ring->obj);
1049 drm_gem_object_unreference(&ring->obj->base);
1053 ring->cleanup(ring);
1055 cleanup_status_page(ring);
1058 static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
1060 uint32_t __iomem *virt;
1061 int rem = ring->size - ring->tail;
1063 if (ring->space < rem) {
1064 int ret = intel_wait_ring_buffer(ring, rem);
1069 virt = ring->virtual_start + ring->tail;
1072 iowrite32(MI_NOOP, virt++);
1075 ring->space = ring_space(ring);
1080 static int intel_ring_wait_seqno(struct intel_ring_buffer *ring, u32 seqno)
1082 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1083 bool was_interruptible;
1086 /* XXX As we have not yet audited all the paths to check that
1087 * they are ready for ERESTARTSYS from intel_ring_begin, do not
1088 * allow us to be interruptible by a signal.
1090 was_interruptible = dev_priv->mm.interruptible;
1091 dev_priv->mm.interruptible = false;
1093 ret = i915_wait_request(ring, seqno);
1095 dev_priv->mm.interruptible = was_interruptible;
1097 i915_gem_retire_requests_ring(ring);
1102 static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
1104 struct drm_i915_gem_request *request;
1108 i915_gem_retire_requests_ring(ring);
1110 if (ring->last_retired_head != -1) {
1111 ring->head = ring->last_retired_head;
1112 ring->last_retired_head = -1;
1113 ring->space = ring_space(ring);
1114 if (ring->space >= n)
1118 list_for_each_entry(request, &ring->request_list, list) {
1121 if (request->tail == -1)
1124 space = request->tail - (ring->tail + 8);
1126 space += ring->size;
1128 seqno = request->seqno;
1132 /* Consume this request in case we need more space than
1133 * is available and so need to prevent a race between
1134 * updating last_retired_head and direct reads of
1135 * I915_RING_HEAD. It also provides a nice sanity check.
1143 ret = intel_ring_wait_seqno(ring, seqno);
1147 if (WARN_ON(ring->last_retired_head == -1))
1150 ring->head = ring->last_retired_head;
1151 ring->last_retired_head = -1;
1152 ring->space = ring_space(ring);
1153 if (WARN_ON(ring->space < n))
1159 int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
1161 struct drm_device *dev = ring->dev;
1162 struct drm_i915_private *dev_priv = dev->dev_private;
1166 ret = intel_ring_wait_request(ring, n);
1170 trace_i915_ring_wait_begin(ring);
1171 /* With GEM the hangcheck timer should kick us out of the loop,
1172 * leaving it early runs the risk of corrupting GEM state (due
1173 * to running on almost untested codepaths). But on resume
1174 * timers don't work yet, so prevent a complete hang in that
1175 * case by choosing an insanely large timeout. */
1176 end = jiffies + 60 * HZ;
1179 ring->head = I915_READ_HEAD(ring);
1180 ring->space = ring_space(ring);
1181 if (ring->space >= n) {
1182 trace_i915_ring_wait_end(ring);
1186 if (dev->primary->master) {
1187 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1188 if (master_priv->sarea_priv)
1189 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1193 if (atomic_read(&dev_priv->mm.wedged))
1195 } while (!time_after(jiffies, end));
1196 trace_i915_ring_wait_end(ring);
1200 int intel_ring_begin(struct intel_ring_buffer *ring,
1203 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1204 int n = 4*num_dwords;
1207 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
1210 if (unlikely(ring->tail + n > ring->effective_size)) {
1211 ret = intel_wrap_ring_buffer(ring);
1216 if (unlikely(ring->space < n)) {
1217 ret = intel_wait_ring_buffer(ring, n);
1226 void intel_ring_advance(struct intel_ring_buffer *ring)
1228 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1230 ring->tail &= ring->size - 1;
1231 if (dev_priv->stop_rings & intel_ring_flag(ring))
1233 ring->write_tail(ring, ring->tail);
1237 static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
1240 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1242 /* Every tail move must follow the sequence below */
1243 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1244 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1245 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1246 I915_WRITE(GEN6_BSD_RNCID, 0x0);
1248 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1249 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1251 DRM_ERROR("timed out waiting for IDLE Indicator\n");
1253 I915_WRITE_TAIL(ring, value);
1254 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1255 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1256 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
1259 static int gen6_ring_flush(struct intel_ring_buffer *ring,
1260 u32 invalidate, u32 flush)
1265 ret = intel_ring_begin(ring, 4);
1270 if (invalidate & I915_GEM_GPU_DOMAINS)
1271 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1272 intel_ring_emit(ring, cmd);
1273 intel_ring_emit(ring, 0);
1274 intel_ring_emit(ring, 0);
1275 intel_ring_emit(ring, MI_NOOP);
1276 intel_ring_advance(ring);
1281 gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
1282 u32 offset, u32 len)
1286 ret = intel_ring_begin(ring, 2);
1290 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1291 /* bit0-7 is the length on GEN6+ */
1292 intel_ring_emit(ring, offset);
1293 intel_ring_advance(ring);
1298 /* Blitter support (SandyBridge+) */
1300 static int blt_ring_flush(struct intel_ring_buffer *ring,
1301 u32 invalidate, u32 flush)
1306 ret = intel_ring_begin(ring, 4);
1311 if (invalidate & I915_GEM_DOMAIN_RENDER)
1312 cmd |= MI_INVALIDATE_TLB;
1313 intel_ring_emit(ring, cmd);
1314 intel_ring_emit(ring, 0);
1315 intel_ring_emit(ring, 0);
1316 intel_ring_emit(ring, MI_NOOP);
1317 intel_ring_advance(ring);
1321 int intel_init_render_ring_buffer(struct drm_device *dev)
1323 drm_i915_private_t *dev_priv = dev->dev_private;
1324 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1326 ring->name = "render ring";
1328 ring->mmio_base = RENDER_RING_BASE;
1330 if (INTEL_INFO(dev)->gen >= 6) {
1331 ring->add_request = gen6_add_request;
1332 ring->flush = gen6_render_ring_flush;
1333 ring->irq_get = gen6_ring_get_irq;
1334 ring->irq_put = gen6_ring_put_irq;
1335 ring->irq_enable_mask = GT_USER_INTERRUPT;
1336 ring->get_seqno = gen6_ring_get_seqno;
1337 ring->sync_to = gen6_ring_sync;
1338 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_INVALID;
1339 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_RV;
1340 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_RB;
1341 ring->signal_mbox[0] = GEN6_VRSYNC;
1342 ring->signal_mbox[1] = GEN6_BRSYNC;
1343 } else if (IS_GEN5(dev)) {
1344 ring->add_request = pc_render_add_request;
1345 ring->flush = gen4_render_ring_flush;
1346 ring->get_seqno = pc_render_get_seqno;
1347 ring->irq_get = gen5_ring_get_irq;
1348 ring->irq_put = gen5_ring_put_irq;
1349 ring->irq_enable_mask = GT_USER_INTERRUPT | GT_PIPE_NOTIFY;
1351 ring->add_request = i9xx_add_request;
1352 if (INTEL_INFO(dev)->gen < 4)
1353 ring->flush = gen2_render_ring_flush;
1355 ring->flush = gen4_render_ring_flush;
1356 ring->get_seqno = ring_get_seqno;
1358 ring->irq_get = i8xx_ring_get_irq;
1359 ring->irq_put = i8xx_ring_put_irq;
1361 ring->irq_get = i9xx_ring_get_irq;
1362 ring->irq_put = i9xx_ring_put_irq;
1364 ring->irq_enable_mask = I915_USER_INTERRUPT;
1366 ring->write_tail = ring_write_tail;
1367 if (INTEL_INFO(dev)->gen >= 6)
1368 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1369 else if (INTEL_INFO(dev)->gen >= 4)
1370 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1371 else if (IS_I830(dev) || IS_845G(dev))
1372 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1374 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
1375 ring->init = init_render_ring;
1376 ring->cleanup = render_ring_cleanup;
1379 if (!I915_NEED_GFX_HWS(dev)) {
1380 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1381 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1384 return intel_init_ring_buffer(dev, ring);
1387 int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1389 drm_i915_private_t *dev_priv = dev->dev_private;
1390 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1392 ring->name = "render ring";
1394 ring->mmio_base = RENDER_RING_BASE;
1396 if (INTEL_INFO(dev)->gen >= 6) {
1397 /* non-kms not supported on gen6+ */
1401 /* Note: gem is not supported on gen5/ilk without kms (the corresponding
1402 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
1403 * the special gen5 functions. */
1404 ring->add_request = i9xx_add_request;
1405 if (INTEL_INFO(dev)->gen < 4)
1406 ring->flush = gen2_render_ring_flush;
1408 ring->flush = gen4_render_ring_flush;
1409 ring->get_seqno = ring_get_seqno;
1411 ring->irq_get = i8xx_ring_get_irq;
1412 ring->irq_put = i8xx_ring_put_irq;
1414 ring->irq_get = i9xx_ring_get_irq;
1415 ring->irq_put = i9xx_ring_put_irq;
1417 ring->irq_enable_mask = I915_USER_INTERRUPT;
1418 ring->write_tail = ring_write_tail;
1419 if (INTEL_INFO(dev)->gen >= 4)
1420 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1421 else if (IS_I830(dev) || IS_845G(dev))
1422 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1424 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
1425 ring->init = init_render_ring;
1426 ring->cleanup = render_ring_cleanup;
1428 if (!I915_NEED_GFX_HWS(dev))
1429 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1432 INIT_LIST_HEAD(&ring->active_list);
1433 INIT_LIST_HEAD(&ring->request_list);
1434 INIT_LIST_HEAD(&ring->gpu_write_list);
1437 ring->effective_size = ring->size;
1438 if (IS_I830(ring->dev))
1439 ring->effective_size -= 128;
1441 ring->virtual_start = ioremap_wc(start, size);
1442 if (ring->virtual_start == NULL) {
1443 DRM_ERROR("can not ioremap virtual address for"
1451 int intel_init_bsd_ring_buffer(struct drm_device *dev)
1453 drm_i915_private_t *dev_priv = dev->dev_private;
1454 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
1456 ring->name = "bsd ring";
1459 ring->write_tail = ring_write_tail;
1460 if (IS_GEN6(dev) || IS_GEN7(dev)) {
1461 ring->mmio_base = GEN6_BSD_RING_BASE;
1462 /* gen6 bsd needs a special wa for tail updates */
1464 ring->write_tail = gen6_bsd_ring_write_tail;
1465 ring->flush = gen6_ring_flush;
1466 ring->add_request = gen6_add_request;
1467 ring->get_seqno = gen6_ring_get_seqno;
1468 ring->irq_enable_mask = GEN6_BSD_USER_INTERRUPT;
1469 ring->irq_get = gen6_ring_get_irq;
1470 ring->irq_put = gen6_ring_put_irq;
1471 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1472 ring->sync_to = gen6_ring_sync;
1473 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_VR;
1474 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_INVALID;
1475 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_VB;
1476 ring->signal_mbox[0] = GEN6_RVSYNC;
1477 ring->signal_mbox[1] = GEN6_BVSYNC;
1479 ring->mmio_base = BSD_RING_BASE;
1480 ring->flush = bsd_ring_flush;
1481 ring->add_request = i9xx_add_request;
1482 ring->get_seqno = ring_get_seqno;
1484 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
1485 ring->irq_get = gen5_ring_get_irq;
1486 ring->irq_put = gen5_ring_put_irq;
1488 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
1489 ring->irq_get = i9xx_ring_get_irq;
1490 ring->irq_put = i9xx_ring_put_irq;
1492 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1494 ring->init = init_ring_common;
1497 return intel_init_ring_buffer(dev, ring);
1500 int intel_init_blt_ring_buffer(struct drm_device *dev)
1502 drm_i915_private_t *dev_priv = dev->dev_private;
1503 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
1505 ring->name = "blitter ring";
1508 ring->mmio_base = BLT_RING_BASE;
1509 ring->write_tail = ring_write_tail;
1510 ring->flush = blt_ring_flush;
1511 ring->add_request = gen6_add_request;
1512 ring->get_seqno = gen6_ring_get_seqno;
1513 ring->irq_enable_mask = GEN6_BLITTER_USER_INTERRUPT;
1514 ring->irq_get = gen6_ring_get_irq;
1515 ring->irq_put = gen6_ring_put_irq;
1516 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1517 ring->sync_to = gen6_ring_sync;
1518 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_BR;
1519 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_BV;
1520 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_INVALID;
1521 ring->signal_mbox[0] = GEN6_RBSYNC;
1522 ring->signal_mbox[1] = GEN6_VBSYNC;
1523 ring->init = init_ring_common;
1525 return intel_init_ring_buffer(dev, ring);