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>
32 #include <drm/i915_drm.h>
33 #include "i915_trace.h"
34 #include "intel_drv.h"
37 intel_ring_initialized(struct intel_engine_cs *ring)
39 struct drm_device *dev = ring->dev;
44 if (i915.enable_execlists) {
45 struct intel_context *dctx = ring->default_context;
46 struct intel_ringbuffer *ringbuf = dctx->engine[ring->id].ringbuf;
50 return ring->buffer && ring->buffer->obj;
53 int __intel_ring_space(int head, int tail, int size)
55 int space = head - tail;
58 return space - I915_RING_FREE_SPACE;
61 void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
63 if (ringbuf->last_retired_head != -1) {
64 ringbuf->head = ringbuf->last_retired_head;
65 ringbuf->last_retired_head = -1;
68 ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
69 ringbuf->tail, ringbuf->size);
72 int intel_ring_space(struct intel_ringbuffer *ringbuf)
74 intel_ring_update_space(ringbuf);
75 return ringbuf->space;
78 bool intel_ring_stopped(struct intel_engine_cs *ring)
80 struct drm_i915_private *dev_priv = ring->dev->dev_private;
81 return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
84 static void __intel_ring_advance(struct intel_engine_cs *ring)
86 struct intel_ringbuffer *ringbuf = ring->buffer;
87 ringbuf->tail &= ringbuf->size - 1;
88 if (intel_ring_stopped(ring))
90 ring->write_tail(ring, ringbuf->tail);
94 gen2_render_ring_flush(struct drm_i915_gem_request *req,
95 u32 invalidate_domains,
98 struct intel_engine_cs *ring = req->ring;
103 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
104 cmd |= MI_NO_WRITE_FLUSH;
106 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
107 cmd |= MI_READ_FLUSH;
109 ret = intel_ring_begin(req, 2);
113 intel_ring_emit(ring, cmd);
114 intel_ring_emit(ring, MI_NOOP);
115 intel_ring_advance(ring);
121 gen4_render_ring_flush(struct drm_i915_gem_request *req,
122 u32 invalidate_domains,
125 struct intel_engine_cs *ring = req->ring;
126 struct drm_device *dev = ring->dev;
133 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
134 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
135 * also flushed at 2d versus 3d pipeline switches.
139 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
140 * MI_READ_FLUSH is set, and is always flushed on 965.
142 * I915_GEM_DOMAIN_COMMAND may not exist?
144 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
145 * invalidated when MI_EXE_FLUSH is set.
147 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
148 * invalidated with every MI_FLUSH.
152 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
153 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
154 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
155 * are flushed at any MI_FLUSH.
158 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
159 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
160 cmd &= ~MI_NO_WRITE_FLUSH;
161 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
164 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
165 (IS_G4X(dev) || IS_GEN5(dev)))
166 cmd |= MI_INVALIDATE_ISP;
168 ret = intel_ring_begin(req, 2);
172 intel_ring_emit(ring, cmd);
173 intel_ring_emit(ring, MI_NOOP);
174 intel_ring_advance(ring);
180 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
181 * implementing two workarounds on gen6. From section 1.4.7.1
182 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
184 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
185 * produced by non-pipelined state commands), software needs to first
186 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
189 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
190 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
192 * And the workaround for these two requires this workaround first:
194 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
195 * BEFORE the pipe-control with a post-sync op and no write-cache
198 * And this last workaround is tricky because of the requirements on
199 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
202 * "1 of the following must also be set:
203 * - Render Target Cache Flush Enable ([12] of DW1)
204 * - Depth Cache Flush Enable ([0] of DW1)
205 * - Stall at Pixel Scoreboard ([1] of DW1)
206 * - Depth Stall ([13] of DW1)
207 * - Post-Sync Operation ([13] of DW1)
208 * - Notify Enable ([8] of DW1)"
210 * The cache flushes require the workaround flush that triggered this
211 * one, so we can't use it. Depth stall would trigger the same.
212 * Post-sync nonzero is what triggered this second workaround, so we
213 * can't use that one either. Notify enable is IRQs, which aren't
214 * really our business. That leaves only stall at scoreboard.
217 intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
219 struct intel_engine_cs *ring = req->ring;
220 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
223 ret = intel_ring_begin(req, 6);
227 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
228 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
229 PIPE_CONTROL_STALL_AT_SCOREBOARD);
230 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
231 intel_ring_emit(ring, 0); /* low dword */
232 intel_ring_emit(ring, 0); /* high dword */
233 intel_ring_emit(ring, MI_NOOP);
234 intel_ring_advance(ring);
236 ret = intel_ring_begin(req, 6);
240 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
241 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
242 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
243 intel_ring_emit(ring, 0);
244 intel_ring_emit(ring, 0);
245 intel_ring_emit(ring, MI_NOOP);
246 intel_ring_advance(ring);
252 gen6_render_ring_flush(struct drm_i915_gem_request *req,
253 u32 invalidate_domains, u32 flush_domains)
255 struct intel_engine_cs *ring = req->ring;
257 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
260 /* Force SNB workarounds for PIPE_CONTROL flushes */
261 ret = intel_emit_post_sync_nonzero_flush(req);
265 /* Just flush everything. Experiments have shown that reducing the
266 * number of bits based on the write domains has little performance
270 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
271 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
273 * Ensure that any following seqno writes only happen
274 * when the render cache is indeed flushed.
276 flags |= PIPE_CONTROL_CS_STALL;
278 if (invalidate_domains) {
279 flags |= PIPE_CONTROL_TLB_INVALIDATE;
280 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
281 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
282 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
283 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
284 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
286 * TLB invalidate requires a post-sync write.
288 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
291 ret = intel_ring_begin(req, 4);
295 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
296 intel_ring_emit(ring, flags);
297 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
298 intel_ring_emit(ring, 0);
299 intel_ring_advance(ring);
305 gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
307 struct intel_engine_cs *ring = req->ring;
310 ret = intel_ring_begin(req, 4);
314 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
315 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
316 PIPE_CONTROL_STALL_AT_SCOREBOARD);
317 intel_ring_emit(ring, 0);
318 intel_ring_emit(ring, 0);
319 intel_ring_advance(ring);
325 gen7_render_ring_flush(struct drm_i915_gem_request *req,
326 u32 invalidate_domains, u32 flush_domains)
328 struct intel_engine_cs *ring = req->ring;
330 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
334 * Ensure that any following seqno writes only happen when the render
335 * cache is indeed flushed.
337 * Workaround: 4th PIPE_CONTROL command (except the ones with only
338 * read-cache invalidate bits set) must have the CS_STALL bit set. We
339 * don't try to be clever and just set it unconditionally.
341 flags |= PIPE_CONTROL_CS_STALL;
343 /* Just flush everything. Experiments have shown that reducing the
344 * number of bits based on the write domains has little performance
348 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
349 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
350 flags |= PIPE_CONTROL_FLUSH_ENABLE;
352 if (invalidate_domains) {
353 flags |= PIPE_CONTROL_TLB_INVALIDATE;
354 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
355 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
356 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
357 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
358 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
359 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
361 * TLB invalidate requires a post-sync write.
363 flags |= PIPE_CONTROL_QW_WRITE;
364 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
366 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
368 /* Workaround: we must issue a pipe_control with CS-stall bit
369 * set before a pipe_control command that has the state cache
370 * invalidate bit set. */
371 gen7_render_ring_cs_stall_wa(req);
374 ret = intel_ring_begin(req, 4);
378 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
379 intel_ring_emit(ring, flags);
380 intel_ring_emit(ring, scratch_addr);
381 intel_ring_emit(ring, 0);
382 intel_ring_advance(ring);
388 gen8_emit_pipe_control(struct drm_i915_gem_request *req,
389 u32 flags, u32 scratch_addr)
391 struct intel_engine_cs *ring = req->ring;
394 ret = intel_ring_begin(req, 6);
398 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
399 intel_ring_emit(ring, flags);
400 intel_ring_emit(ring, scratch_addr);
401 intel_ring_emit(ring, 0);
402 intel_ring_emit(ring, 0);
403 intel_ring_emit(ring, 0);
404 intel_ring_advance(ring);
410 gen8_render_ring_flush(struct drm_i915_gem_request *req,
411 u32 invalidate_domains, u32 flush_domains)
414 u32 scratch_addr = req->ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
417 flags |= PIPE_CONTROL_CS_STALL;
420 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
421 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
422 flags |= PIPE_CONTROL_FLUSH_ENABLE;
424 if (invalidate_domains) {
425 flags |= PIPE_CONTROL_TLB_INVALIDATE;
426 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
427 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
428 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
429 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
430 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
431 flags |= PIPE_CONTROL_QW_WRITE;
432 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
434 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
435 ret = gen8_emit_pipe_control(req,
436 PIPE_CONTROL_CS_STALL |
437 PIPE_CONTROL_STALL_AT_SCOREBOARD,
443 return gen8_emit_pipe_control(req, flags, scratch_addr);
446 static void ring_write_tail(struct intel_engine_cs *ring,
449 struct drm_i915_private *dev_priv = ring->dev->dev_private;
450 I915_WRITE_TAIL(ring, value);
453 u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
455 struct drm_i915_private *dev_priv = ring->dev->dev_private;
458 if (INTEL_INFO(ring->dev)->gen >= 8)
459 acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
460 RING_ACTHD_UDW(ring->mmio_base));
461 else if (INTEL_INFO(ring->dev)->gen >= 4)
462 acthd = I915_READ(RING_ACTHD(ring->mmio_base));
464 acthd = I915_READ(ACTHD);
469 static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
471 struct drm_i915_private *dev_priv = ring->dev->dev_private;
474 addr = dev_priv->status_page_dmah->busaddr;
475 if (INTEL_INFO(ring->dev)->gen >= 4)
476 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
477 I915_WRITE(HWS_PGA, addr);
480 static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
482 struct drm_device *dev = ring->dev;
483 struct drm_i915_private *dev_priv = ring->dev->dev_private;
486 /* The ring status page addresses are no longer next to the rest of
487 * the ring registers as of gen7.
492 mmio = RENDER_HWS_PGA_GEN7;
495 mmio = BLT_HWS_PGA_GEN7;
498 * VCS2 actually doesn't exist on Gen7. Only shut up
499 * gcc switch check warning
503 mmio = BSD_HWS_PGA_GEN7;
506 mmio = VEBOX_HWS_PGA_GEN7;
509 } else if (IS_GEN6(ring->dev)) {
510 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
512 /* XXX: gen8 returns to sanity */
513 mmio = RING_HWS_PGA(ring->mmio_base);
516 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
520 * Flush the TLB for this page
522 * FIXME: These two bits have disappeared on gen8, so a question
523 * arises: do we still need this and if so how should we go about
524 * invalidating the TLB?
526 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
527 u32 reg = RING_INSTPM(ring->mmio_base);
529 /* ring should be idle before issuing a sync flush*/
530 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
533 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
535 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
537 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
542 static bool stop_ring(struct intel_engine_cs *ring)
544 struct drm_i915_private *dev_priv = to_i915(ring->dev);
546 if (!IS_GEN2(ring->dev)) {
547 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
548 if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
549 DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
550 /* Sometimes we observe that the idle flag is not
551 * set even though the ring is empty. So double
552 * check before giving up.
554 if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
559 I915_WRITE_CTL(ring, 0);
560 I915_WRITE_HEAD(ring, 0);
561 ring->write_tail(ring, 0);
563 if (!IS_GEN2(ring->dev)) {
564 (void)I915_READ_CTL(ring);
565 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
568 return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
571 static int init_ring_common(struct intel_engine_cs *ring)
573 struct drm_device *dev = ring->dev;
574 struct drm_i915_private *dev_priv = dev->dev_private;
575 struct intel_ringbuffer *ringbuf = ring->buffer;
576 struct drm_i915_gem_object *obj = ringbuf->obj;
579 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
581 if (!stop_ring(ring)) {
582 /* G45 ring initialization often fails to reset head to zero */
583 DRM_DEBUG_KMS("%s head not reset to zero "
584 "ctl %08x head %08x tail %08x start %08x\n",
587 I915_READ_HEAD(ring),
588 I915_READ_TAIL(ring),
589 I915_READ_START(ring));
591 if (!stop_ring(ring)) {
592 DRM_ERROR("failed to set %s head to zero "
593 "ctl %08x head %08x tail %08x start %08x\n",
596 I915_READ_HEAD(ring),
597 I915_READ_TAIL(ring),
598 I915_READ_START(ring));
604 if (I915_NEED_GFX_HWS(dev))
605 intel_ring_setup_status_page(ring);
607 ring_setup_phys_status_page(ring);
609 /* Enforce ordering by reading HEAD register back */
610 I915_READ_HEAD(ring);
612 /* Initialize the ring. This must happen _after_ we've cleared the ring
613 * registers with the above sequence (the readback of the HEAD registers
614 * also enforces ordering), otherwise the hw might lose the new ring
615 * register values. */
616 I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
618 /* WaClearRingBufHeadRegAtInit:ctg,elk */
619 if (I915_READ_HEAD(ring))
620 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
621 ring->name, I915_READ_HEAD(ring));
622 I915_WRITE_HEAD(ring, 0);
623 (void)I915_READ_HEAD(ring);
626 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
629 /* If the head is still not zero, the ring is dead */
630 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
631 I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
632 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
633 DRM_ERROR("%s initialization failed "
634 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
636 I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
637 I915_READ_HEAD(ring), I915_READ_TAIL(ring),
638 I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
643 ringbuf->last_retired_head = -1;
644 ringbuf->head = I915_READ_HEAD(ring);
645 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
646 intel_ring_update_space(ringbuf);
648 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
651 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
657 intel_fini_pipe_control(struct intel_engine_cs *ring)
659 struct drm_device *dev = ring->dev;
661 if (ring->scratch.obj == NULL)
664 if (INTEL_INFO(dev)->gen >= 5) {
665 kunmap(sg_page(ring->scratch.obj->pages->sgl));
666 i915_gem_object_ggtt_unpin(ring->scratch.obj);
669 drm_gem_object_unreference(&ring->scratch.obj->base);
670 ring->scratch.obj = NULL;
674 intel_init_pipe_control(struct intel_engine_cs *ring)
678 WARN_ON(ring->scratch.obj);
680 ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
681 if (ring->scratch.obj == NULL) {
682 DRM_ERROR("Failed to allocate seqno page\n");
687 ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
691 ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
695 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
696 ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
697 if (ring->scratch.cpu_page == NULL) {
702 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
703 ring->name, ring->scratch.gtt_offset);
707 i915_gem_object_ggtt_unpin(ring->scratch.obj);
709 drm_gem_object_unreference(&ring->scratch.obj->base);
714 static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
717 struct intel_engine_cs *ring = req->ring;
718 struct drm_device *dev = ring->dev;
719 struct drm_i915_private *dev_priv = dev->dev_private;
720 struct i915_workarounds *w = &dev_priv->workarounds;
725 ring->gpu_caches_dirty = true;
726 ret = intel_ring_flush_all_caches(req);
730 ret = intel_ring_begin(req, (w->count * 2 + 2));
734 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
735 for (i = 0; i < w->count; i++) {
736 intel_ring_emit(ring, w->reg[i].addr);
737 intel_ring_emit(ring, w->reg[i].value);
739 intel_ring_emit(ring, MI_NOOP);
741 intel_ring_advance(ring);
743 ring->gpu_caches_dirty = true;
744 ret = intel_ring_flush_all_caches(req);
748 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
753 static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
757 ret = intel_ring_workarounds_emit(req);
761 ret = i915_gem_render_state_init(req);
763 DRM_ERROR("init render state: %d\n", ret);
768 static int wa_add(struct drm_i915_private *dev_priv,
769 const u32 addr, const u32 mask, const u32 val)
771 const u32 idx = dev_priv->workarounds.count;
773 if (WARN_ON(idx >= I915_MAX_WA_REGS))
776 dev_priv->workarounds.reg[idx].addr = addr;
777 dev_priv->workarounds.reg[idx].value = val;
778 dev_priv->workarounds.reg[idx].mask = mask;
780 dev_priv->workarounds.count++;
785 #define WA_REG(addr, mask, val) do { \
786 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
791 #define WA_SET_BIT_MASKED(addr, mask) \
792 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
794 #define WA_CLR_BIT_MASKED(addr, mask) \
795 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
797 #define WA_SET_FIELD_MASKED(addr, mask, value) \
798 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
800 #define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
801 #define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
803 #define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
805 static int gen8_init_workarounds(struct intel_engine_cs *ring)
807 struct drm_device *dev = ring->dev;
808 struct drm_i915_private *dev_priv = dev->dev_private;
810 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
812 /* WaDisableAsyncFlipPerfMode:bdw,chv */
813 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
815 /* WaDisablePartialInstShootdown:bdw,chv */
816 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
817 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
819 /* Use Force Non-Coherent whenever executing a 3D context. This is a
820 * workaround for for a possible hang in the unlikely event a TLB
821 * invalidation occurs during a PSD flush.
823 /* WaForceEnableNonCoherent:bdw,chv */
824 /* WaHdcDisableFetchWhenMasked:bdw,chv */
825 WA_SET_BIT_MASKED(HDC_CHICKEN0,
826 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
827 HDC_FORCE_NON_COHERENT);
829 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
830 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
831 * polygons in the same 8x4 pixel/sample area to be processed without
832 * stalling waiting for the earlier ones to write to Hierarchical Z
835 * This optimization is off by default for BDW and CHV; turn it on.
837 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
839 /* Wa4x4STCOptimizationDisable:bdw,chv */
840 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
843 * BSpec recommends 8x4 when MSAA is used,
844 * however in practice 16x4 seems fastest.
846 * Note that PS/WM thread counts depend on the WIZ hashing
847 * disable bit, which we don't touch here, but it's good
848 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
850 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
851 GEN6_WIZ_HASHING_MASK,
852 GEN6_WIZ_HASHING_16x4);
857 static int bdw_init_workarounds(struct intel_engine_cs *ring)
860 struct drm_device *dev = ring->dev;
861 struct drm_i915_private *dev_priv = dev->dev_private;
863 ret = gen8_init_workarounds(ring);
867 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
868 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
870 /* WaDisableDopClockGating:bdw */
871 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
872 DOP_CLOCK_GATING_DISABLE);
874 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
875 GEN8_SAMPLER_POWER_BYPASS_DIS);
877 WA_SET_BIT_MASKED(HDC_CHICKEN0,
878 /* WaForceContextSaveRestoreNonCoherent:bdw */
879 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
880 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
881 (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
886 static int chv_init_workarounds(struct intel_engine_cs *ring)
889 struct drm_device *dev = ring->dev;
890 struct drm_i915_private *dev_priv = dev->dev_private;
892 ret = gen8_init_workarounds(ring);
896 /* WaDisableThreadStallDopClockGating:chv */
897 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
899 /* Improve HiZ throughput on CHV. */
900 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
905 static int gen9_init_workarounds(struct intel_engine_cs *ring)
907 struct drm_device *dev = ring->dev;
908 struct drm_i915_private *dev_priv = dev->dev_private;
911 /* WaEnableLbsSlaRetryTimerDecrement:skl */
912 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
913 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
915 /* WaDisableKillLogic:bxt,skl */
916 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
919 /* WaDisablePartialInstShootdown:skl,bxt */
920 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
921 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
923 /* Syncing dependencies between camera and graphics:skl,bxt */
924 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
925 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
927 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) == SKL_REVID_A0 ||
928 INTEL_REVID(dev) == SKL_REVID_B0)) ||
929 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
930 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
931 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
932 GEN9_DG_MIRROR_FIX_ENABLE);
935 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0) ||
936 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
937 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
938 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
939 GEN9_RHWO_OPTIMIZATION_DISABLE);
941 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
942 * but we do that in per ctx batchbuffer as there is an issue
943 * with this register not getting restored on ctx restore
947 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) >= SKL_REVID_C0) ||
949 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt */
950 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
951 GEN9_ENABLE_YV12_BUGFIX);
954 /* Wa4x4STCOptimizationDisable:skl,bxt */
955 /* WaDisablePartialResolveInVc:skl,bxt */
956 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
957 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
959 /* WaCcsTlbPrefetchDisable:skl,bxt */
960 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
961 GEN9_CCS_TLB_PREFETCH_ENABLE);
963 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
964 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_C0) ||
965 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0))
966 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
967 PIXEL_MASK_CAMMING_DISABLE);
969 /* WaForceContextSaveRestoreNonCoherent:skl,bxt */
970 tmp = HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT;
971 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_F0) ||
972 (IS_BROXTON(dev) && INTEL_REVID(dev) >= BXT_REVID_B0))
973 tmp |= HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE;
974 WA_SET_BIT_MASKED(HDC_CHICKEN0, tmp);
976 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt */
977 if (IS_SKYLAKE(dev) ||
978 (IS_BROXTON(dev) && INTEL_REVID(dev) <= BXT_REVID_B0)) {
979 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
980 GEN8_SAMPLER_POWER_BYPASS_DIS);
983 /* WaDisableSTUnitPowerOptimization:skl,bxt */
984 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
989 static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
991 struct drm_device *dev = ring->dev;
992 struct drm_i915_private *dev_priv = dev->dev_private;
993 u8 vals[3] = { 0, 0, 0 };
996 for (i = 0; i < 3; i++) {
1000 * Only consider slices where one, and only one, subslice has 7
1003 if (hweight8(dev_priv->info.subslice_7eu[i]) != 1)
1007 * subslice_7eu[i] != 0 (because of the check above) and
1008 * ss_max == 4 (maximum number of subslices possible per slice)
1012 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1016 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1019 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1020 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1021 GEN9_IZ_HASHING_MASK(2) |
1022 GEN9_IZ_HASHING_MASK(1) |
1023 GEN9_IZ_HASHING_MASK(0),
1024 GEN9_IZ_HASHING(2, vals[2]) |
1025 GEN9_IZ_HASHING(1, vals[1]) |
1026 GEN9_IZ_HASHING(0, vals[0]));
1031 static int skl_init_workarounds(struct intel_engine_cs *ring)
1034 struct drm_device *dev = ring->dev;
1035 struct drm_i915_private *dev_priv = dev->dev_private;
1037 ret = gen9_init_workarounds(ring);
1041 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
1042 /* WaDisableHDCInvalidation:skl */
1043 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
1044 BDW_DISABLE_HDC_INVALIDATION);
1046 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1047 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1048 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1051 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1052 * involving this register should also be added to WA batch as required.
1054 if (INTEL_REVID(dev) <= SKL_REVID_E0)
1055 /* WaDisableLSQCROPERFforOCL:skl */
1056 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1057 GEN8_LQSC_RO_PERF_DIS);
1059 /* WaEnableGapsTsvCreditFix:skl */
1060 if (IS_SKYLAKE(dev) && (INTEL_REVID(dev) >= SKL_REVID_C0)) {
1061 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1062 GEN9_GAPS_TSV_CREDIT_DISABLE));
1065 /* WaDisablePowerCompilerClockGating:skl */
1066 if (INTEL_REVID(dev) == SKL_REVID_B0)
1067 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1068 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1070 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
1072 *Use Force Non-Coherent whenever executing a 3D context. This
1073 * is a workaround for a possible hang in the unlikely event
1074 * a TLB invalidation occurs during a PSD flush.
1076 /* WaForceEnableNonCoherent:skl */
1077 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1078 HDC_FORCE_NON_COHERENT);
1081 if (INTEL_REVID(dev) == SKL_REVID_C0 ||
1082 INTEL_REVID(dev) == SKL_REVID_D0)
1083 /* WaBarrierPerformanceFixDisable:skl */
1084 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1085 HDC_FENCE_DEST_SLM_DISABLE |
1086 HDC_BARRIER_PERFORMANCE_DISABLE);
1088 /* WaDisableSbeCacheDispatchPortSharing:skl */
1089 if (INTEL_REVID(dev) <= SKL_REVID_F0) {
1091 GEN7_HALF_SLICE_CHICKEN1,
1092 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1095 return skl_tune_iz_hashing(ring);
1098 static int bxt_init_workarounds(struct intel_engine_cs *ring)
1101 struct drm_device *dev = ring->dev;
1102 struct drm_i915_private *dev_priv = dev->dev_private;
1104 ret = gen9_init_workarounds(ring);
1108 /* WaStoreMultiplePTEenable:bxt */
1109 /* This is a requirement according to Hardware specification */
1110 if (INTEL_REVID(dev) == BXT_REVID_A0)
1111 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1113 /* WaSetClckGatingDisableMedia:bxt */
1114 if (INTEL_REVID(dev) == BXT_REVID_A0) {
1115 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1116 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1119 /* WaDisableThreadStallDopClockGating:bxt */
1120 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1121 STALL_DOP_GATING_DISABLE);
1123 /* WaDisableSbeCacheDispatchPortSharing:bxt */
1124 if (INTEL_REVID(dev) <= BXT_REVID_B0) {
1126 GEN7_HALF_SLICE_CHICKEN1,
1127 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1133 int init_workarounds_ring(struct intel_engine_cs *ring)
1135 struct drm_device *dev = ring->dev;
1136 struct drm_i915_private *dev_priv = dev->dev_private;
1138 WARN_ON(ring->id != RCS);
1140 dev_priv->workarounds.count = 0;
1142 if (IS_BROADWELL(dev))
1143 return bdw_init_workarounds(ring);
1145 if (IS_CHERRYVIEW(dev))
1146 return chv_init_workarounds(ring);
1148 if (IS_SKYLAKE(dev))
1149 return skl_init_workarounds(ring);
1151 if (IS_BROXTON(dev))
1152 return bxt_init_workarounds(ring);
1157 static int init_render_ring(struct intel_engine_cs *ring)
1159 struct drm_device *dev = ring->dev;
1160 struct drm_i915_private *dev_priv = dev->dev_private;
1161 int ret = init_ring_common(ring);
1165 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1166 if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
1167 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1169 /* We need to disable the AsyncFlip performance optimisations in order
1170 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1171 * programmed to '1' on all products.
1173 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1175 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
1176 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1178 /* Required for the hardware to program scanline values for waiting */
1179 /* WaEnableFlushTlbInvalidationMode:snb */
1180 if (INTEL_INFO(dev)->gen == 6)
1181 I915_WRITE(GFX_MODE,
1182 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
1184 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
1186 I915_WRITE(GFX_MODE_GEN7,
1187 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1188 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
1191 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1192 * "If this bit is set, STCunit will have LRA as replacement
1193 * policy. [...] This bit must be reset. LRA replacement
1194 * policy is not supported."
1196 I915_WRITE(CACHE_MODE_0,
1197 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
1200 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
1201 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1203 if (HAS_L3_DPF(dev))
1204 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1206 return init_workarounds_ring(ring);
1209 static void render_ring_cleanup(struct intel_engine_cs *ring)
1211 struct drm_device *dev = ring->dev;
1212 struct drm_i915_private *dev_priv = dev->dev_private;
1214 if (dev_priv->semaphore_obj) {
1215 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1216 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1217 dev_priv->semaphore_obj = NULL;
1220 intel_fini_pipe_control(ring);
1223 static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
1224 unsigned int num_dwords)
1226 #define MBOX_UPDATE_DWORDS 8
1227 struct intel_engine_cs *signaller = signaller_req->ring;
1228 struct drm_device *dev = signaller->dev;
1229 struct drm_i915_private *dev_priv = dev->dev_private;
1230 struct intel_engine_cs *waiter;
1231 int i, ret, num_rings;
1233 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1234 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1235 #undef MBOX_UPDATE_DWORDS
1237 ret = intel_ring_begin(signaller_req, num_dwords);
1241 for_each_ring(waiter, dev_priv, i) {
1243 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1244 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1247 seqno = i915_gem_request_get_seqno(signaller_req);
1248 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1249 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1250 PIPE_CONTROL_QW_WRITE |
1251 PIPE_CONTROL_FLUSH_ENABLE);
1252 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1253 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
1254 intel_ring_emit(signaller, seqno);
1255 intel_ring_emit(signaller, 0);
1256 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1257 MI_SEMAPHORE_TARGET(waiter->id));
1258 intel_ring_emit(signaller, 0);
1264 static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
1265 unsigned int num_dwords)
1267 #define MBOX_UPDATE_DWORDS 6
1268 struct intel_engine_cs *signaller = signaller_req->ring;
1269 struct drm_device *dev = signaller->dev;
1270 struct drm_i915_private *dev_priv = dev->dev_private;
1271 struct intel_engine_cs *waiter;
1272 int i, ret, num_rings;
1274 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1275 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1276 #undef MBOX_UPDATE_DWORDS
1278 ret = intel_ring_begin(signaller_req, num_dwords);
1282 for_each_ring(waiter, dev_priv, i) {
1284 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1285 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1288 seqno = i915_gem_request_get_seqno(signaller_req);
1289 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1290 MI_FLUSH_DW_OP_STOREDW);
1291 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1292 MI_FLUSH_DW_USE_GTT);
1293 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
1294 intel_ring_emit(signaller, seqno);
1295 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1296 MI_SEMAPHORE_TARGET(waiter->id));
1297 intel_ring_emit(signaller, 0);
1303 static int gen6_signal(struct drm_i915_gem_request *signaller_req,
1304 unsigned int num_dwords)
1306 struct intel_engine_cs *signaller = signaller_req->ring;
1307 struct drm_device *dev = signaller->dev;
1308 struct drm_i915_private *dev_priv = dev->dev_private;
1309 struct intel_engine_cs *useless;
1310 int i, ret, num_rings;
1312 #define MBOX_UPDATE_DWORDS 3
1313 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1314 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1315 #undef MBOX_UPDATE_DWORDS
1317 ret = intel_ring_begin(signaller_req, num_dwords);
1321 for_each_ring(useless, dev_priv, i) {
1322 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1323 if (mbox_reg != GEN6_NOSYNC) {
1324 u32 seqno = i915_gem_request_get_seqno(signaller_req);
1325 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1326 intel_ring_emit(signaller, mbox_reg);
1327 intel_ring_emit(signaller, seqno);
1331 /* If num_dwords was rounded, make sure the tail pointer is correct */
1332 if (num_rings % 2 == 0)
1333 intel_ring_emit(signaller, MI_NOOP);
1339 * gen6_add_request - Update the semaphore mailbox registers
1341 * @request - request to write to the ring
1343 * Update the mailbox registers in the *other* rings with the current seqno.
1344 * This acts like a signal in the canonical semaphore.
1347 gen6_add_request(struct drm_i915_gem_request *req)
1349 struct intel_engine_cs *ring = req->ring;
1352 if (ring->semaphore.signal)
1353 ret = ring->semaphore.signal(req, 4);
1355 ret = intel_ring_begin(req, 4);
1360 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1361 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1362 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1363 intel_ring_emit(ring, MI_USER_INTERRUPT);
1364 __intel_ring_advance(ring);
1369 static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1372 struct drm_i915_private *dev_priv = dev->dev_private;
1373 return dev_priv->last_seqno < seqno;
1377 * intel_ring_sync - sync the waiter to the signaller on seqno
1379 * @waiter - ring that is waiting
1380 * @signaller - ring which has, or will signal
1381 * @seqno - seqno which the waiter will block on
1385 gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
1386 struct intel_engine_cs *signaller,
1389 struct intel_engine_cs *waiter = waiter_req->ring;
1390 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
1393 ret = intel_ring_begin(waiter_req, 4);
1397 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1398 MI_SEMAPHORE_GLOBAL_GTT |
1400 MI_SEMAPHORE_SAD_GTE_SDD);
1401 intel_ring_emit(waiter, seqno);
1402 intel_ring_emit(waiter,
1403 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1404 intel_ring_emit(waiter,
1405 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1406 intel_ring_advance(waiter);
1411 gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
1412 struct intel_engine_cs *signaller,
1415 struct intel_engine_cs *waiter = waiter_req->ring;
1416 u32 dw1 = MI_SEMAPHORE_MBOX |
1417 MI_SEMAPHORE_COMPARE |
1418 MI_SEMAPHORE_REGISTER;
1419 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1422 /* Throughout all of the GEM code, seqno passed implies our current
1423 * seqno is >= the last seqno executed. However for hardware the
1424 * comparison is strictly greater than.
1428 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
1430 ret = intel_ring_begin(waiter_req, 4);
1434 /* If seqno wrap happened, omit the wait with no-ops */
1435 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
1436 intel_ring_emit(waiter, dw1 | wait_mbox);
1437 intel_ring_emit(waiter, seqno);
1438 intel_ring_emit(waiter, 0);
1439 intel_ring_emit(waiter, MI_NOOP);
1441 intel_ring_emit(waiter, MI_NOOP);
1442 intel_ring_emit(waiter, MI_NOOP);
1443 intel_ring_emit(waiter, MI_NOOP);
1444 intel_ring_emit(waiter, MI_NOOP);
1446 intel_ring_advance(waiter);
1451 #define PIPE_CONTROL_FLUSH(ring__, addr__) \
1453 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1454 PIPE_CONTROL_DEPTH_STALL); \
1455 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1456 intel_ring_emit(ring__, 0); \
1457 intel_ring_emit(ring__, 0); \
1461 pc_render_add_request(struct drm_i915_gem_request *req)
1463 struct intel_engine_cs *ring = req->ring;
1464 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
1467 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1468 * incoherent with writes to memory, i.e. completely fubar,
1469 * so we need to use PIPE_NOTIFY instead.
1471 * However, we also need to workaround the qword write
1472 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1473 * memory before requesting an interrupt.
1475 ret = intel_ring_begin(req, 32);
1479 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1480 PIPE_CONTROL_WRITE_FLUSH |
1481 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
1482 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1483 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1484 intel_ring_emit(ring, 0);
1485 PIPE_CONTROL_FLUSH(ring, scratch_addr);
1486 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
1487 PIPE_CONTROL_FLUSH(ring, scratch_addr);
1488 scratch_addr += 2 * CACHELINE_BYTES;
1489 PIPE_CONTROL_FLUSH(ring, scratch_addr);
1490 scratch_addr += 2 * CACHELINE_BYTES;
1491 PIPE_CONTROL_FLUSH(ring, scratch_addr);
1492 scratch_addr += 2 * CACHELINE_BYTES;
1493 PIPE_CONTROL_FLUSH(ring, scratch_addr);
1494 scratch_addr += 2 * CACHELINE_BYTES;
1495 PIPE_CONTROL_FLUSH(ring, scratch_addr);
1497 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
1498 PIPE_CONTROL_WRITE_FLUSH |
1499 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
1500 PIPE_CONTROL_NOTIFY);
1501 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
1502 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1503 intel_ring_emit(ring, 0);
1504 __intel_ring_advance(ring);
1510 gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1512 /* Workaround to force correct ordering between irq and seqno writes on
1513 * ivb (and maybe also on snb) by reading from a CS register (like
1514 * ACTHD) before reading the status page. */
1515 if (!lazy_coherency) {
1516 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1517 POSTING_READ(RING_ACTHD(ring->mmio_base));
1520 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1524 ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1526 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1530 ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1532 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1536 pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1538 return ring->scratch.cpu_page[0];
1542 pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1544 ring->scratch.cpu_page[0] = seqno;
1548 gen5_ring_get_irq(struct intel_engine_cs *ring)
1550 struct drm_device *dev = ring->dev;
1551 struct drm_i915_private *dev_priv = dev->dev_private;
1552 unsigned long flags;
1554 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1557 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1558 if (ring->irq_refcount++ == 0)
1559 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1560 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1566 gen5_ring_put_irq(struct intel_engine_cs *ring)
1568 struct drm_device *dev = ring->dev;
1569 struct drm_i915_private *dev_priv = dev->dev_private;
1570 unsigned long flags;
1572 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1573 if (--ring->irq_refcount == 0)
1574 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1575 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1579 i9xx_ring_get_irq(struct intel_engine_cs *ring)
1581 struct drm_device *dev = ring->dev;
1582 struct drm_i915_private *dev_priv = dev->dev_private;
1583 unsigned long flags;
1585 if (!intel_irqs_enabled(dev_priv))
1588 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1589 if (ring->irq_refcount++ == 0) {
1590 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1591 I915_WRITE(IMR, dev_priv->irq_mask);
1594 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1600 i9xx_ring_put_irq(struct intel_engine_cs *ring)
1602 struct drm_device *dev = ring->dev;
1603 struct drm_i915_private *dev_priv = dev->dev_private;
1604 unsigned long flags;
1606 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1607 if (--ring->irq_refcount == 0) {
1608 dev_priv->irq_mask |= ring->irq_enable_mask;
1609 I915_WRITE(IMR, dev_priv->irq_mask);
1612 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1616 i8xx_ring_get_irq(struct intel_engine_cs *ring)
1618 struct drm_device *dev = ring->dev;
1619 struct drm_i915_private *dev_priv = dev->dev_private;
1620 unsigned long flags;
1622 if (!intel_irqs_enabled(dev_priv))
1625 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1626 if (ring->irq_refcount++ == 0) {
1627 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1628 I915_WRITE16(IMR, dev_priv->irq_mask);
1629 POSTING_READ16(IMR);
1631 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1637 i8xx_ring_put_irq(struct intel_engine_cs *ring)
1639 struct drm_device *dev = ring->dev;
1640 struct drm_i915_private *dev_priv = dev->dev_private;
1641 unsigned long flags;
1643 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1644 if (--ring->irq_refcount == 0) {
1645 dev_priv->irq_mask |= ring->irq_enable_mask;
1646 I915_WRITE16(IMR, dev_priv->irq_mask);
1647 POSTING_READ16(IMR);
1649 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1653 bsd_ring_flush(struct drm_i915_gem_request *req,
1654 u32 invalidate_domains,
1657 struct intel_engine_cs *ring = req->ring;
1660 ret = intel_ring_begin(req, 2);
1664 intel_ring_emit(ring, MI_FLUSH);
1665 intel_ring_emit(ring, MI_NOOP);
1666 intel_ring_advance(ring);
1671 i9xx_add_request(struct drm_i915_gem_request *req)
1673 struct intel_engine_cs *ring = req->ring;
1676 ret = intel_ring_begin(req, 4);
1680 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1681 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1682 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1683 intel_ring_emit(ring, MI_USER_INTERRUPT);
1684 __intel_ring_advance(ring);
1690 gen6_ring_get_irq(struct intel_engine_cs *ring)
1692 struct drm_device *dev = ring->dev;
1693 struct drm_i915_private *dev_priv = dev->dev_private;
1694 unsigned long flags;
1696 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1699 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1700 if (ring->irq_refcount++ == 0) {
1701 if (HAS_L3_DPF(dev) && ring->id == RCS)
1702 I915_WRITE_IMR(ring,
1703 ~(ring->irq_enable_mask |
1704 GT_PARITY_ERROR(dev)));
1706 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1707 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
1709 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1715 gen6_ring_put_irq(struct intel_engine_cs *ring)
1717 struct drm_device *dev = ring->dev;
1718 struct drm_i915_private *dev_priv = dev->dev_private;
1719 unsigned long flags;
1721 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1722 if (--ring->irq_refcount == 0) {
1723 if (HAS_L3_DPF(dev) && ring->id == RCS)
1724 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
1726 I915_WRITE_IMR(ring, ~0);
1727 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
1729 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1733 hsw_vebox_get_irq(struct intel_engine_cs *ring)
1735 struct drm_device *dev = ring->dev;
1736 struct drm_i915_private *dev_priv = dev->dev_private;
1737 unsigned long flags;
1739 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1742 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1743 if (ring->irq_refcount++ == 0) {
1744 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1745 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
1747 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1753 hsw_vebox_put_irq(struct intel_engine_cs *ring)
1755 struct drm_device *dev = ring->dev;
1756 struct drm_i915_private *dev_priv = dev->dev_private;
1757 unsigned long flags;
1759 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1760 if (--ring->irq_refcount == 0) {
1761 I915_WRITE_IMR(ring, ~0);
1762 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
1764 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1768 gen8_ring_get_irq(struct intel_engine_cs *ring)
1770 struct drm_device *dev = ring->dev;
1771 struct drm_i915_private *dev_priv = dev->dev_private;
1772 unsigned long flags;
1774 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1777 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1778 if (ring->irq_refcount++ == 0) {
1779 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1780 I915_WRITE_IMR(ring,
1781 ~(ring->irq_enable_mask |
1782 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1784 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1786 POSTING_READ(RING_IMR(ring->mmio_base));
1788 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1794 gen8_ring_put_irq(struct intel_engine_cs *ring)
1796 struct drm_device *dev = ring->dev;
1797 struct drm_i915_private *dev_priv = dev->dev_private;
1798 unsigned long flags;
1800 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1801 if (--ring->irq_refcount == 0) {
1802 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1803 I915_WRITE_IMR(ring,
1804 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1806 I915_WRITE_IMR(ring, ~0);
1808 POSTING_READ(RING_IMR(ring->mmio_base));
1810 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1814 i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
1815 u64 offset, u32 length,
1816 unsigned dispatch_flags)
1818 struct intel_engine_cs *ring = req->ring;
1821 ret = intel_ring_begin(req, 2);
1825 intel_ring_emit(ring,
1826 MI_BATCH_BUFFER_START |
1828 (dispatch_flags & I915_DISPATCH_SECURE ?
1829 0 : MI_BATCH_NON_SECURE_I965));
1830 intel_ring_emit(ring, offset);
1831 intel_ring_advance(ring);
1836 /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1837 #define I830_BATCH_LIMIT (256*1024)
1838 #define I830_TLB_ENTRIES (2)
1839 #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
1841 i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
1842 u64 offset, u32 len,
1843 unsigned dispatch_flags)
1845 struct intel_engine_cs *ring = req->ring;
1846 u32 cs_offset = ring->scratch.gtt_offset;
1849 ret = intel_ring_begin(req, 6);
1853 /* Evict the invalid PTE TLBs */
1854 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1855 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1856 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1857 intel_ring_emit(ring, cs_offset);
1858 intel_ring_emit(ring, 0xdeadbeef);
1859 intel_ring_emit(ring, MI_NOOP);
1860 intel_ring_advance(ring);
1862 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
1863 if (len > I830_BATCH_LIMIT)
1866 ret = intel_ring_begin(req, 6 + 2);
1870 /* Blit the batch (which has now all relocs applied) to the
1871 * stable batch scratch bo area (so that the CS never
1872 * stumbles over its tlb invalidation bug) ...
1874 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1875 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
1876 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1877 intel_ring_emit(ring, cs_offset);
1878 intel_ring_emit(ring, 4096);
1879 intel_ring_emit(ring, offset);
1881 intel_ring_emit(ring, MI_FLUSH);
1882 intel_ring_emit(ring, MI_NOOP);
1883 intel_ring_advance(ring);
1885 /* ... and execute it. */
1889 ret = intel_ring_begin(req, 4);
1893 intel_ring_emit(ring, MI_BATCH_BUFFER);
1894 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1895 0 : MI_BATCH_NON_SECURE));
1896 intel_ring_emit(ring, offset + len - 8);
1897 intel_ring_emit(ring, MI_NOOP);
1898 intel_ring_advance(ring);
1904 i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
1905 u64 offset, u32 len,
1906 unsigned dispatch_flags)
1908 struct intel_engine_cs *ring = req->ring;
1911 ret = intel_ring_begin(req, 2);
1915 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1916 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1917 0 : MI_BATCH_NON_SECURE));
1918 intel_ring_advance(ring);
1923 static void cleanup_status_page(struct intel_engine_cs *ring)
1925 struct drm_i915_gem_object *obj;
1927 obj = ring->status_page.obj;
1931 kunmap(sg_page(obj->pages->sgl));
1932 i915_gem_object_ggtt_unpin(obj);
1933 drm_gem_object_unreference(&obj->base);
1934 ring->status_page.obj = NULL;
1937 static int init_status_page(struct intel_engine_cs *ring)
1939 struct drm_i915_gem_object *obj;
1941 if ((obj = ring->status_page.obj) == NULL) {
1945 obj = i915_gem_alloc_object(ring->dev, 4096);
1947 DRM_ERROR("Failed to allocate status page\n");
1951 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1956 if (!HAS_LLC(ring->dev))
1957 /* On g33, we cannot place HWS above 256MiB, so
1958 * restrict its pinning to the low mappable arena.
1959 * Though this restriction is not documented for
1960 * gen4, gen5, or byt, they also behave similarly
1961 * and hang if the HWS is placed at the top of the
1962 * GTT. To generalise, it appears that all !llc
1963 * platforms have issues with us placing the HWS
1964 * above the mappable region (even though we never
1967 flags |= PIN_MAPPABLE;
1968 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
1971 drm_gem_object_unreference(&obj->base);
1975 ring->status_page.obj = obj;
1978 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1979 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
1980 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1982 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1983 ring->name, ring->status_page.gfx_addr);
1988 static int init_phys_status_page(struct intel_engine_cs *ring)
1990 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1992 if (!dev_priv->status_page_dmah) {
1993 dev_priv->status_page_dmah =
1994 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1995 if (!dev_priv->status_page_dmah)
1999 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
2000 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
2005 void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
2007 iounmap(ringbuf->virtual_start);
2008 ringbuf->virtual_start = NULL;
2009 i915_gem_object_ggtt_unpin(ringbuf->obj);
2012 int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
2013 struct intel_ringbuffer *ringbuf)
2015 struct drm_i915_private *dev_priv = to_i915(dev);
2016 struct drm_i915_gem_object *obj = ringbuf->obj;
2019 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
2023 ret = i915_gem_object_set_to_gtt_domain(obj, true);
2025 i915_gem_object_ggtt_unpin(obj);
2029 ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
2030 i915_gem_obj_ggtt_offset(obj), ringbuf->size);
2031 if (ringbuf->virtual_start == NULL) {
2032 i915_gem_object_ggtt_unpin(obj);
2039 static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
2041 drm_gem_object_unreference(&ringbuf->obj->base);
2042 ringbuf->obj = NULL;
2045 static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
2046 struct intel_ringbuffer *ringbuf)
2048 struct drm_i915_gem_object *obj;
2052 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
2054 obj = i915_gem_alloc_object(dev, ringbuf->size);
2058 /* mark ring buffers as read-only from GPU side by default */
2066 struct intel_ringbuffer *
2067 intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
2069 struct intel_ringbuffer *ring;
2072 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2074 return ERR_PTR(-ENOMEM);
2076 ring->ring = engine;
2079 /* Workaround an erratum on the i830 which causes a hang if
2080 * the TAIL pointer points to within the last 2 cachelines
2083 ring->effective_size = size;
2084 if (IS_I830(engine->dev) || IS_845G(engine->dev))
2085 ring->effective_size -= 2 * CACHELINE_BYTES;
2087 ring->last_retired_head = -1;
2088 intel_ring_update_space(ring);
2090 ret = intel_alloc_ringbuffer_obj(engine->dev, ring);
2092 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
2095 return ERR_PTR(ret);
2102 intel_ringbuffer_free(struct intel_ringbuffer *ring)
2104 intel_destroy_ringbuffer_obj(ring);
2108 static int intel_init_ring_buffer(struct drm_device *dev,
2109 struct intel_engine_cs *ring)
2111 struct intel_ringbuffer *ringbuf;
2114 WARN_ON(ring->buffer);
2117 INIT_LIST_HEAD(&ring->active_list);
2118 INIT_LIST_HEAD(&ring->request_list);
2119 INIT_LIST_HEAD(&ring->execlist_queue);
2120 i915_gem_batch_pool_init(dev, &ring->batch_pool);
2121 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
2123 init_waitqueue_head(&ring->irq_queue);
2125 ringbuf = intel_engine_create_ringbuffer(ring, 32 * PAGE_SIZE);
2126 if (IS_ERR(ringbuf))
2127 return PTR_ERR(ringbuf);
2128 ring->buffer = ringbuf;
2130 if (I915_NEED_GFX_HWS(dev)) {
2131 ret = init_status_page(ring);
2135 BUG_ON(ring->id != RCS);
2136 ret = init_phys_status_page(ring);
2141 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2143 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2145 intel_destroy_ringbuffer_obj(ringbuf);
2149 ret = i915_cmd_parser_init_ring(ring);
2156 intel_ringbuffer_free(ringbuf);
2157 ring->buffer = NULL;
2161 void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
2163 struct drm_i915_private *dev_priv;
2165 if (!intel_ring_initialized(ring))
2168 dev_priv = to_i915(ring->dev);
2170 intel_stop_ring_buffer(ring);
2171 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
2173 intel_unpin_ringbuffer_obj(ring->buffer);
2174 intel_ringbuffer_free(ring->buffer);
2175 ring->buffer = NULL;
2178 ring->cleanup(ring);
2180 cleanup_status_page(ring);
2182 i915_cmd_parser_fini_ring(ring);
2183 i915_gem_batch_pool_fini(&ring->batch_pool);
2186 static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
2188 struct intel_ringbuffer *ringbuf = ring->buffer;
2189 struct drm_i915_gem_request *request;
2193 if (intel_ring_space(ringbuf) >= n)
2196 /* The whole point of reserving space is to not wait! */
2197 WARN_ON(ringbuf->reserved_in_use);
2199 list_for_each_entry(request, &ring->request_list, list) {
2200 space = __intel_ring_space(request->postfix, ringbuf->tail,
2206 if (WARN_ON(&request->list == &ring->request_list))
2209 ret = i915_wait_request(request);
2213 ringbuf->space = space;
2217 static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
2219 uint32_t __iomem *virt;
2220 int rem = ringbuf->size - ringbuf->tail;
2222 virt = ringbuf->virtual_start + ringbuf->tail;
2225 iowrite32(MI_NOOP, virt++);
2228 intel_ring_update_space(ringbuf);
2231 int intel_ring_idle(struct intel_engine_cs *ring)
2233 struct drm_i915_gem_request *req;
2235 /* Wait upon the last request to be completed */
2236 if (list_empty(&ring->request_list))
2239 req = list_entry(ring->request_list.prev,
2240 struct drm_i915_gem_request,
2243 /* Make sure we do not trigger any retires */
2244 return __i915_wait_request(req,
2245 atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
2246 to_i915(ring->dev)->mm.interruptible,
2250 int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
2252 request->ringbuf = request->ring->buffer;
2256 int intel_ring_reserve_space(struct drm_i915_gem_request *request)
2259 * The first call merely notes the reserve request and is common for
2260 * all back ends. The subsequent localised _begin() call actually
2261 * ensures that the reservation is available. Without the begin, if
2262 * the request creator immediately submitted the request without
2263 * adding any commands to it then there might not actually be
2264 * sufficient room for the submission commands.
2266 intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
2268 return intel_ring_begin(request, 0);
2271 void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2273 WARN_ON(ringbuf->reserved_size);
2274 WARN_ON(ringbuf->reserved_in_use);
2276 ringbuf->reserved_size = size;
2279 void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2281 WARN_ON(ringbuf->reserved_in_use);
2283 ringbuf->reserved_size = 0;
2284 ringbuf->reserved_in_use = false;
2287 void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2289 WARN_ON(ringbuf->reserved_in_use);
2291 ringbuf->reserved_in_use = true;
2292 ringbuf->reserved_tail = ringbuf->tail;
2295 void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2297 WARN_ON(!ringbuf->reserved_in_use);
2298 if (ringbuf->tail > ringbuf->reserved_tail) {
2299 WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2300 "request reserved size too small: %d vs %d!\n",
2301 ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2304 * The ring was wrapped while the reserved space was in use.
2305 * That means that some unknown amount of the ring tail was
2306 * no-op filled and skipped. Thus simply adding the ring size
2307 * to the tail and doing the above space check will not work.
2308 * Rather than attempt to track how much tail was skipped,
2309 * it is much simpler to say that also skipping the sanity
2310 * check every once in a while is not a big issue.
2314 ringbuf->reserved_size = 0;
2315 ringbuf->reserved_in_use = false;
2318 static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
2320 struct intel_ringbuffer *ringbuf = ring->buffer;
2321 int remain_usable = ringbuf->effective_size - ringbuf->tail;
2322 int remain_actual = ringbuf->size - ringbuf->tail;
2323 int ret, total_bytes, wait_bytes = 0;
2324 bool need_wrap = false;
2326 if (ringbuf->reserved_in_use)
2327 total_bytes = bytes;
2329 total_bytes = bytes + ringbuf->reserved_size;
2331 if (unlikely(bytes > remain_usable)) {
2333 * Not enough space for the basic request. So need to flush
2334 * out the remainder and then wait for base + reserved.
2336 wait_bytes = remain_actual + total_bytes;
2339 if (unlikely(total_bytes > remain_usable)) {
2341 * The base request will fit but the reserved space
2342 * falls off the end. So only need to to wait for the
2343 * reserved size after flushing out the remainder.
2345 wait_bytes = remain_actual + ringbuf->reserved_size;
2347 } else if (total_bytes > ringbuf->space) {
2348 /* No wrapping required, just waiting. */
2349 wait_bytes = total_bytes;
2354 ret = ring_wait_for_space(ring, wait_bytes);
2359 __wrap_ring_buffer(ringbuf);
2365 int intel_ring_begin(struct drm_i915_gem_request *req,
2368 struct intel_engine_cs *ring;
2369 struct drm_i915_private *dev_priv;
2372 WARN_ON(req == NULL);
2374 dev_priv = ring->dev->dev_private;
2376 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2377 dev_priv->mm.interruptible);
2381 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
2385 ring->buffer->space -= num_dwords * sizeof(uint32_t);
2389 /* Align the ring tail to a cacheline boundary */
2390 int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
2392 struct intel_engine_cs *ring = req->ring;
2393 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
2396 if (num_dwords == 0)
2399 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
2400 ret = intel_ring_begin(req, num_dwords);
2404 while (num_dwords--)
2405 intel_ring_emit(ring, MI_NOOP);
2407 intel_ring_advance(ring);
2412 void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
2414 struct drm_device *dev = ring->dev;
2415 struct drm_i915_private *dev_priv = dev->dev_private;
2417 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
2418 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2419 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
2421 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
2424 ring->set_seqno(ring, seqno);
2425 ring->hangcheck.seqno = seqno;
2428 static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
2431 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2433 /* Every tail move must follow the sequence below */
2435 /* Disable notification that the ring is IDLE. The GT
2436 * will then assume that it is busy and bring it out of rc6.
2438 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2439 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2441 /* Clear the context id. Here be magic! */
2442 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2444 /* Wait for the ring not to be idle, i.e. for it to wake up. */
2445 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
2446 GEN6_BSD_SLEEP_INDICATOR) == 0,
2448 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
2450 /* Now that the ring is fully powered up, update the tail */
2451 I915_WRITE_TAIL(ring, value);
2452 POSTING_READ(RING_TAIL(ring->mmio_base));
2454 /* Let the ring send IDLE messages to the GT again,
2455 * and so let it sleep to conserve power when idle.
2457 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2458 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2461 static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
2462 u32 invalidate, u32 flush)
2464 struct intel_engine_cs *ring = req->ring;
2468 ret = intel_ring_begin(req, 4);
2473 if (INTEL_INFO(ring->dev)->gen >= 8)
2476 /* We always require a command barrier so that subsequent
2477 * commands, such as breadcrumb interrupts, are strictly ordered
2478 * wrt the contents of the write cache being flushed to memory
2479 * (and thus being coherent from the CPU).
2481 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2484 * Bspec vol 1c.5 - video engine command streamer:
2485 * "If ENABLED, all TLBs will be invalidated once the flush
2486 * operation is complete. This bit is only valid when the
2487 * Post-Sync Operation field is a value of 1h or 3h."
2489 if (invalidate & I915_GEM_GPU_DOMAINS)
2490 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2492 intel_ring_emit(ring, cmd);
2493 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
2494 if (INTEL_INFO(ring->dev)->gen >= 8) {
2495 intel_ring_emit(ring, 0); /* upper addr */
2496 intel_ring_emit(ring, 0); /* value */
2498 intel_ring_emit(ring, 0);
2499 intel_ring_emit(ring, MI_NOOP);
2501 intel_ring_advance(ring);
2506 gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2507 u64 offset, u32 len,
2508 unsigned dispatch_flags)
2510 struct intel_engine_cs *ring = req->ring;
2511 bool ppgtt = USES_PPGTT(ring->dev) &&
2512 !(dispatch_flags & I915_DISPATCH_SECURE);
2515 ret = intel_ring_begin(req, 4);
2519 /* FIXME(BDW): Address space and security selectors. */
2520 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
2521 (dispatch_flags & I915_DISPATCH_RS ?
2522 MI_BATCH_RESOURCE_STREAMER : 0));
2523 intel_ring_emit(ring, lower_32_bits(offset));
2524 intel_ring_emit(ring, upper_32_bits(offset));
2525 intel_ring_emit(ring, MI_NOOP);
2526 intel_ring_advance(ring);
2532 hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2533 u64 offset, u32 len,
2534 unsigned dispatch_flags)
2536 struct intel_engine_cs *ring = req->ring;
2539 ret = intel_ring_begin(req, 2);
2543 intel_ring_emit(ring,
2544 MI_BATCH_BUFFER_START |
2545 (dispatch_flags & I915_DISPATCH_SECURE ?
2546 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2547 (dispatch_flags & I915_DISPATCH_RS ?
2548 MI_BATCH_RESOURCE_STREAMER : 0));
2549 /* bit0-7 is the length on GEN6+ */
2550 intel_ring_emit(ring, offset);
2551 intel_ring_advance(ring);
2557 gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2558 u64 offset, u32 len,
2559 unsigned dispatch_flags)
2561 struct intel_engine_cs *ring = req->ring;
2564 ret = intel_ring_begin(req, 2);
2568 intel_ring_emit(ring,
2569 MI_BATCH_BUFFER_START |
2570 (dispatch_flags & I915_DISPATCH_SECURE ?
2571 0 : MI_BATCH_NON_SECURE_I965));
2572 /* bit0-7 is the length on GEN6+ */
2573 intel_ring_emit(ring, offset);
2574 intel_ring_advance(ring);
2579 /* Blitter support (SandyBridge+) */
2581 static int gen6_ring_flush(struct drm_i915_gem_request *req,
2582 u32 invalidate, u32 flush)
2584 struct intel_engine_cs *ring = req->ring;
2585 struct drm_device *dev = ring->dev;
2589 ret = intel_ring_begin(req, 4);
2594 if (INTEL_INFO(dev)->gen >= 8)
2597 /* We always require a command barrier so that subsequent
2598 * commands, such as breadcrumb interrupts, are strictly ordered
2599 * wrt the contents of the write cache being flushed to memory
2600 * (and thus being coherent from the CPU).
2602 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2605 * Bspec vol 1c.3 - blitter engine command streamer:
2606 * "If ENABLED, all TLBs will be invalidated once the flush
2607 * operation is complete. This bit is only valid when the
2608 * Post-Sync Operation field is a value of 1h or 3h."
2610 if (invalidate & I915_GEM_DOMAIN_RENDER)
2611 cmd |= MI_INVALIDATE_TLB;
2612 intel_ring_emit(ring, cmd);
2613 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
2614 if (INTEL_INFO(dev)->gen >= 8) {
2615 intel_ring_emit(ring, 0); /* upper addr */
2616 intel_ring_emit(ring, 0); /* value */
2618 intel_ring_emit(ring, 0);
2619 intel_ring_emit(ring, MI_NOOP);
2621 intel_ring_advance(ring);
2626 int intel_init_render_ring_buffer(struct drm_device *dev)
2628 struct drm_i915_private *dev_priv = dev->dev_private;
2629 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
2630 struct drm_i915_gem_object *obj;
2633 ring->name = "render ring";
2635 ring->mmio_base = RENDER_RING_BASE;
2637 if (INTEL_INFO(dev)->gen >= 8) {
2638 if (i915_semaphore_is_enabled(dev)) {
2639 obj = i915_gem_alloc_object(dev, 4096);
2641 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2642 i915.semaphores = 0;
2644 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2645 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2647 drm_gem_object_unreference(&obj->base);
2648 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2649 i915.semaphores = 0;
2651 dev_priv->semaphore_obj = obj;
2655 ring->init_context = intel_rcs_ctx_init;
2656 ring->add_request = gen6_add_request;
2657 ring->flush = gen8_render_ring_flush;
2658 ring->irq_get = gen8_ring_get_irq;
2659 ring->irq_put = gen8_ring_put_irq;
2660 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2661 ring->get_seqno = gen6_ring_get_seqno;
2662 ring->set_seqno = ring_set_seqno;
2663 if (i915_semaphore_is_enabled(dev)) {
2664 WARN_ON(!dev_priv->semaphore_obj);
2665 ring->semaphore.sync_to = gen8_ring_sync;
2666 ring->semaphore.signal = gen8_rcs_signal;
2667 GEN8_RING_SEMAPHORE_INIT;
2669 } else if (INTEL_INFO(dev)->gen >= 6) {
2670 ring->init_context = intel_rcs_ctx_init;
2671 ring->add_request = gen6_add_request;
2672 ring->flush = gen7_render_ring_flush;
2673 if (INTEL_INFO(dev)->gen == 6)
2674 ring->flush = gen6_render_ring_flush;
2675 ring->irq_get = gen6_ring_get_irq;
2676 ring->irq_put = gen6_ring_put_irq;
2677 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2678 ring->get_seqno = gen6_ring_get_seqno;
2679 ring->set_seqno = ring_set_seqno;
2680 if (i915_semaphore_is_enabled(dev)) {
2681 ring->semaphore.sync_to = gen6_ring_sync;
2682 ring->semaphore.signal = gen6_signal;
2684 * The current semaphore is only applied on pre-gen8
2685 * platform. And there is no VCS2 ring on the pre-gen8
2686 * platform. So the semaphore between RCS and VCS2 is
2687 * initialized as INVALID. Gen8 will initialize the
2688 * sema between VCS2 and RCS later.
2690 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2691 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2692 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2693 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2694 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2695 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2696 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2697 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2698 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2699 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2701 } else if (IS_GEN5(dev)) {
2702 ring->add_request = pc_render_add_request;
2703 ring->flush = gen4_render_ring_flush;
2704 ring->get_seqno = pc_render_get_seqno;
2705 ring->set_seqno = pc_render_set_seqno;
2706 ring->irq_get = gen5_ring_get_irq;
2707 ring->irq_put = gen5_ring_put_irq;
2708 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2709 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
2711 ring->add_request = i9xx_add_request;
2712 if (INTEL_INFO(dev)->gen < 4)
2713 ring->flush = gen2_render_ring_flush;
2715 ring->flush = gen4_render_ring_flush;
2716 ring->get_seqno = ring_get_seqno;
2717 ring->set_seqno = ring_set_seqno;
2719 ring->irq_get = i8xx_ring_get_irq;
2720 ring->irq_put = i8xx_ring_put_irq;
2722 ring->irq_get = i9xx_ring_get_irq;
2723 ring->irq_put = i9xx_ring_put_irq;
2725 ring->irq_enable_mask = I915_USER_INTERRUPT;
2727 ring->write_tail = ring_write_tail;
2729 if (IS_HASWELL(dev))
2730 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
2731 else if (IS_GEN8(dev))
2732 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2733 else if (INTEL_INFO(dev)->gen >= 6)
2734 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2735 else if (INTEL_INFO(dev)->gen >= 4)
2736 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2737 else if (IS_I830(dev) || IS_845G(dev))
2738 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2740 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
2741 ring->init_hw = init_render_ring;
2742 ring->cleanup = render_ring_cleanup;
2744 /* Workaround batchbuffer to combat CS tlb bug. */
2745 if (HAS_BROKEN_CS_TLB(dev)) {
2746 obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
2748 DRM_ERROR("Failed to allocate batch bo\n");
2752 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
2754 drm_gem_object_unreference(&obj->base);
2755 DRM_ERROR("Failed to ping batch bo\n");
2759 ring->scratch.obj = obj;
2760 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
2763 ret = intel_init_ring_buffer(dev, ring);
2767 if (INTEL_INFO(dev)->gen >= 5) {
2768 ret = intel_init_pipe_control(ring);
2776 int intel_init_bsd_ring_buffer(struct drm_device *dev)
2778 struct drm_i915_private *dev_priv = dev->dev_private;
2779 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
2781 ring->name = "bsd ring";
2784 ring->write_tail = ring_write_tail;
2785 if (INTEL_INFO(dev)->gen >= 6) {
2786 ring->mmio_base = GEN6_BSD_RING_BASE;
2787 /* gen6 bsd needs a special wa for tail updates */
2789 ring->write_tail = gen6_bsd_ring_write_tail;
2790 ring->flush = gen6_bsd_ring_flush;
2791 ring->add_request = gen6_add_request;
2792 ring->get_seqno = gen6_ring_get_seqno;
2793 ring->set_seqno = ring_set_seqno;
2794 if (INTEL_INFO(dev)->gen >= 8) {
2795 ring->irq_enable_mask =
2796 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2797 ring->irq_get = gen8_ring_get_irq;
2798 ring->irq_put = gen8_ring_put_irq;
2799 ring->dispatch_execbuffer =
2800 gen8_ring_dispatch_execbuffer;
2801 if (i915_semaphore_is_enabled(dev)) {
2802 ring->semaphore.sync_to = gen8_ring_sync;
2803 ring->semaphore.signal = gen8_xcs_signal;
2804 GEN8_RING_SEMAPHORE_INIT;
2807 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2808 ring->irq_get = gen6_ring_get_irq;
2809 ring->irq_put = gen6_ring_put_irq;
2810 ring->dispatch_execbuffer =
2811 gen6_ring_dispatch_execbuffer;
2812 if (i915_semaphore_is_enabled(dev)) {
2813 ring->semaphore.sync_to = gen6_ring_sync;
2814 ring->semaphore.signal = gen6_signal;
2815 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2816 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2817 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2818 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2819 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2820 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2821 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2822 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2823 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2824 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2828 ring->mmio_base = BSD_RING_BASE;
2829 ring->flush = bsd_ring_flush;
2830 ring->add_request = i9xx_add_request;
2831 ring->get_seqno = ring_get_seqno;
2832 ring->set_seqno = ring_set_seqno;
2834 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2835 ring->irq_get = gen5_ring_get_irq;
2836 ring->irq_put = gen5_ring_put_irq;
2838 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2839 ring->irq_get = i9xx_ring_get_irq;
2840 ring->irq_put = i9xx_ring_put_irq;
2842 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2844 ring->init_hw = init_ring_common;
2846 return intel_init_ring_buffer(dev, ring);
2850 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
2852 int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2854 struct drm_i915_private *dev_priv = dev->dev_private;
2855 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
2857 ring->name = "bsd2 ring";
2860 ring->write_tail = ring_write_tail;
2861 ring->mmio_base = GEN8_BSD2_RING_BASE;
2862 ring->flush = gen6_bsd_ring_flush;
2863 ring->add_request = gen6_add_request;
2864 ring->get_seqno = gen6_ring_get_seqno;
2865 ring->set_seqno = ring_set_seqno;
2866 ring->irq_enable_mask =
2867 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2868 ring->irq_get = gen8_ring_get_irq;
2869 ring->irq_put = gen8_ring_put_irq;
2870 ring->dispatch_execbuffer =
2871 gen8_ring_dispatch_execbuffer;
2872 if (i915_semaphore_is_enabled(dev)) {
2873 ring->semaphore.sync_to = gen8_ring_sync;
2874 ring->semaphore.signal = gen8_xcs_signal;
2875 GEN8_RING_SEMAPHORE_INIT;
2877 ring->init_hw = init_ring_common;
2879 return intel_init_ring_buffer(dev, ring);
2882 int intel_init_blt_ring_buffer(struct drm_device *dev)
2884 struct drm_i915_private *dev_priv = dev->dev_private;
2885 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
2887 ring->name = "blitter ring";
2890 ring->mmio_base = BLT_RING_BASE;
2891 ring->write_tail = ring_write_tail;
2892 ring->flush = gen6_ring_flush;
2893 ring->add_request = gen6_add_request;
2894 ring->get_seqno = gen6_ring_get_seqno;
2895 ring->set_seqno = ring_set_seqno;
2896 if (INTEL_INFO(dev)->gen >= 8) {
2897 ring->irq_enable_mask =
2898 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2899 ring->irq_get = gen8_ring_get_irq;
2900 ring->irq_put = gen8_ring_put_irq;
2901 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2902 if (i915_semaphore_is_enabled(dev)) {
2903 ring->semaphore.sync_to = gen8_ring_sync;
2904 ring->semaphore.signal = gen8_xcs_signal;
2905 GEN8_RING_SEMAPHORE_INIT;
2908 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2909 ring->irq_get = gen6_ring_get_irq;
2910 ring->irq_put = gen6_ring_put_irq;
2911 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2912 if (i915_semaphore_is_enabled(dev)) {
2913 ring->semaphore.signal = gen6_signal;
2914 ring->semaphore.sync_to = gen6_ring_sync;
2916 * The current semaphore is only applied on pre-gen8
2917 * platform. And there is no VCS2 ring on the pre-gen8
2918 * platform. So the semaphore between BCS and VCS2 is
2919 * initialized as INVALID. Gen8 will initialize the
2920 * sema between BCS and VCS2 later.
2922 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2923 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2924 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2925 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2926 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2927 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2928 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2929 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2930 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2931 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2934 ring->init_hw = init_ring_common;
2936 return intel_init_ring_buffer(dev, ring);
2939 int intel_init_vebox_ring_buffer(struct drm_device *dev)
2941 struct drm_i915_private *dev_priv = dev->dev_private;
2942 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
2944 ring->name = "video enhancement ring";
2947 ring->mmio_base = VEBOX_RING_BASE;
2948 ring->write_tail = ring_write_tail;
2949 ring->flush = gen6_ring_flush;
2950 ring->add_request = gen6_add_request;
2951 ring->get_seqno = gen6_ring_get_seqno;
2952 ring->set_seqno = ring_set_seqno;
2954 if (INTEL_INFO(dev)->gen >= 8) {
2955 ring->irq_enable_mask =
2956 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
2957 ring->irq_get = gen8_ring_get_irq;
2958 ring->irq_put = gen8_ring_put_irq;
2959 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2960 if (i915_semaphore_is_enabled(dev)) {
2961 ring->semaphore.sync_to = gen8_ring_sync;
2962 ring->semaphore.signal = gen8_xcs_signal;
2963 GEN8_RING_SEMAPHORE_INIT;
2966 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2967 ring->irq_get = hsw_vebox_get_irq;
2968 ring->irq_put = hsw_vebox_put_irq;
2969 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2970 if (i915_semaphore_is_enabled(dev)) {
2971 ring->semaphore.sync_to = gen6_ring_sync;
2972 ring->semaphore.signal = gen6_signal;
2973 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2974 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2975 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2976 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2977 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2978 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2979 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2980 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2981 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2982 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2985 ring->init_hw = init_ring_common;
2987 return intel_init_ring_buffer(dev, ring);
2991 intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
2993 struct intel_engine_cs *ring = req->ring;
2996 if (!ring->gpu_caches_dirty)
2999 ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
3003 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
3005 ring->gpu_caches_dirty = false;
3010 intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
3012 struct intel_engine_cs *ring = req->ring;
3013 uint32_t flush_domains;
3017 if (ring->gpu_caches_dirty)
3018 flush_domains = I915_GEM_GPU_DOMAINS;
3020 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
3024 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
3026 ring->gpu_caches_dirty = false;
3031 intel_stop_ring_buffer(struct intel_engine_cs *ring)
3035 if (!intel_ring_initialized(ring))
3038 ret = intel_ring_idle(ring);
3039 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
3040 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",