drm/i915: Rename residual ringbuf parameters
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / intel_ringbuffer.c
blob5dd720e7feaab6dfa1735aea6944787d18ab0f08
1 /*
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
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
30 #include <linux/log2.h>
31 #include <drm/drmP.h>
32 #include "i915_drv.h"
33 #include <drm/i915_drm.h>
34 #include "i915_trace.h"
35 #include "intel_drv.h"
37 /* Rough estimate of the typical request size, performing a flush,
38 * set-context and then emitting the batch.
40 #define LEGACY_REQUEST_SIZE 200
42 int __intel_ring_space(int head, int tail, int size)
44 int space = head - tail;
45 if (space <= 0)
46 space += size;
47 return space - I915_RING_FREE_SPACE;
50 void intel_ring_update_space(struct intel_ring *ring)
52 if (ring->last_retired_head != -1) {
53 ring->head = ring->last_retired_head;
54 ring->last_retired_head = -1;
57 ring->space = __intel_ring_space(ring->head & HEAD_ADDR,
58 ring->tail, ring->size);
61 static void __intel_engine_submit(struct intel_engine_cs *engine)
63 struct intel_ring *ring = engine->buffer;
65 ring->tail &= ring->size - 1;
66 engine->write_tail(engine, ring->tail);
69 static int
70 gen2_render_ring_flush(struct drm_i915_gem_request *req,
71 u32 invalidate_domains,
72 u32 flush_domains)
74 struct intel_ring *ring = req->ring;
75 u32 cmd;
76 int ret;
78 cmd = MI_FLUSH;
79 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
80 cmd |= MI_NO_WRITE_FLUSH;
82 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
83 cmd |= MI_READ_FLUSH;
85 ret = intel_ring_begin(req, 2);
86 if (ret)
87 return ret;
89 intel_ring_emit(ring, cmd);
90 intel_ring_emit(ring, MI_NOOP);
91 intel_ring_advance(ring);
93 return 0;
96 static int
97 gen4_render_ring_flush(struct drm_i915_gem_request *req,
98 u32 invalidate_domains,
99 u32 flush_domains)
101 struct intel_ring *ring = req->ring;
102 u32 cmd;
103 int ret;
106 * read/write caches:
108 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
109 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
110 * also flushed at 2d versus 3d pipeline switches.
112 * read-only caches:
114 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
115 * MI_READ_FLUSH is set, and is always flushed on 965.
117 * I915_GEM_DOMAIN_COMMAND may not exist?
119 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
120 * invalidated when MI_EXE_FLUSH is set.
122 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
123 * invalidated with every MI_FLUSH.
125 * TLBs:
127 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
128 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
129 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
130 * are flushed at any MI_FLUSH.
133 cmd = MI_FLUSH;
134 if (invalidate_domains) {
135 cmd |= MI_EXE_FLUSH;
136 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
137 cmd |= MI_INVALIDATE_ISP;
140 ret = intel_ring_begin(req, 2);
141 if (ret)
142 return ret;
144 intel_ring_emit(ring, cmd);
145 intel_ring_emit(ring, MI_NOOP);
146 intel_ring_advance(ring);
148 return 0;
152 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
153 * implementing two workarounds on gen6. From section 1.4.7.1
154 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
156 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
157 * produced by non-pipelined state commands), software needs to first
158 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
159 * 0.
161 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
162 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
164 * And the workaround for these two requires this workaround first:
166 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
167 * BEFORE the pipe-control with a post-sync op and no write-cache
168 * flushes.
170 * And this last workaround is tricky because of the requirements on
171 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
172 * volume 2 part 1:
174 * "1 of the following must also be set:
175 * - Render Target Cache Flush Enable ([12] of DW1)
176 * - Depth Cache Flush Enable ([0] of DW1)
177 * - Stall at Pixel Scoreboard ([1] of DW1)
178 * - Depth Stall ([13] of DW1)
179 * - Post-Sync Operation ([13] of DW1)
180 * - Notify Enable ([8] of DW1)"
182 * The cache flushes require the workaround flush that triggered this
183 * one, so we can't use it. Depth stall would trigger the same.
184 * Post-sync nonzero is what triggered this second workaround, so we
185 * can't use that one either. Notify enable is IRQs, which aren't
186 * really our business. That leaves only stall at scoreboard.
188 static int
189 intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
191 struct intel_ring *ring = req->ring;
192 u32 scratch_addr =
193 req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
194 int ret;
196 ret = intel_ring_begin(req, 6);
197 if (ret)
198 return ret;
200 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
201 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
202 PIPE_CONTROL_STALL_AT_SCOREBOARD);
203 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
204 intel_ring_emit(ring, 0); /* low dword */
205 intel_ring_emit(ring, 0); /* high dword */
206 intel_ring_emit(ring, MI_NOOP);
207 intel_ring_advance(ring);
209 ret = intel_ring_begin(req, 6);
210 if (ret)
211 return ret;
213 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
214 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
215 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
216 intel_ring_emit(ring, 0);
217 intel_ring_emit(ring, 0);
218 intel_ring_emit(ring, MI_NOOP);
219 intel_ring_advance(ring);
221 return 0;
224 static int
225 gen6_render_ring_flush(struct drm_i915_gem_request *req,
226 u32 invalidate_domains, u32 flush_domains)
228 struct intel_ring *ring = req->ring;
229 u32 scratch_addr =
230 req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
231 u32 flags = 0;
232 int ret;
234 /* Force SNB workarounds for PIPE_CONTROL flushes */
235 ret = intel_emit_post_sync_nonzero_flush(req);
236 if (ret)
237 return ret;
239 /* Just flush everything. Experiments have shown that reducing the
240 * number of bits based on the write domains has little performance
241 * impact.
243 if (flush_domains) {
244 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
245 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
247 * Ensure that any following seqno writes only happen
248 * when the render cache is indeed flushed.
250 flags |= PIPE_CONTROL_CS_STALL;
252 if (invalidate_domains) {
253 flags |= PIPE_CONTROL_TLB_INVALIDATE;
254 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
255 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
256 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
257 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
258 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
260 * TLB invalidate requires a post-sync write.
262 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
265 ret = intel_ring_begin(req, 4);
266 if (ret)
267 return ret;
269 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
270 intel_ring_emit(ring, flags);
271 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
272 intel_ring_emit(ring, 0);
273 intel_ring_advance(ring);
275 return 0;
278 static int
279 gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
281 struct intel_ring *ring = req->ring;
282 int ret;
284 ret = intel_ring_begin(req, 4);
285 if (ret)
286 return ret;
288 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
289 intel_ring_emit(ring,
290 PIPE_CONTROL_CS_STALL |
291 PIPE_CONTROL_STALL_AT_SCOREBOARD);
292 intel_ring_emit(ring, 0);
293 intel_ring_emit(ring, 0);
294 intel_ring_advance(ring);
296 return 0;
299 static int
300 gen7_render_ring_flush(struct drm_i915_gem_request *req,
301 u32 invalidate_domains, u32 flush_domains)
303 struct intel_ring *ring = req->ring;
304 u32 scratch_addr =
305 req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
306 u32 flags = 0;
307 int ret;
310 * Ensure that any following seqno writes only happen when the render
311 * cache is indeed flushed.
313 * Workaround: 4th PIPE_CONTROL command (except the ones with only
314 * read-cache invalidate bits set) must have the CS_STALL bit set. We
315 * don't try to be clever and just set it unconditionally.
317 flags |= PIPE_CONTROL_CS_STALL;
319 /* Just flush everything. Experiments have shown that reducing the
320 * number of bits based on the write domains has little performance
321 * impact.
323 if (flush_domains) {
324 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
325 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
326 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
327 flags |= PIPE_CONTROL_FLUSH_ENABLE;
329 if (invalidate_domains) {
330 flags |= PIPE_CONTROL_TLB_INVALIDATE;
331 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
332 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
333 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
334 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
335 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
336 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
338 * TLB invalidate requires a post-sync write.
340 flags |= PIPE_CONTROL_QW_WRITE;
341 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
343 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
345 /* Workaround: we must issue a pipe_control with CS-stall bit
346 * set before a pipe_control command that has the state cache
347 * invalidate bit set. */
348 gen7_render_ring_cs_stall_wa(req);
351 ret = intel_ring_begin(req, 4);
352 if (ret)
353 return ret;
355 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
356 intel_ring_emit(ring, flags);
357 intel_ring_emit(ring, scratch_addr);
358 intel_ring_emit(ring, 0);
359 intel_ring_advance(ring);
361 return 0;
364 static int
365 gen8_emit_pipe_control(struct drm_i915_gem_request *req,
366 u32 flags, u32 scratch_addr)
368 struct intel_ring *ring = req->ring;
369 int ret;
371 ret = intel_ring_begin(req, 6);
372 if (ret)
373 return ret;
375 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
376 intel_ring_emit(ring, flags);
377 intel_ring_emit(ring, scratch_addr);
378 intel_ring_emit(ring, 0);
379 intel_ring_emit(ring, 0);
380 intel_ring_emit(ring, 0);
381 intel_ring_advance(ring);
383 return 0;
386 static int
387 gen8_render_ring_flush(struct drm_i915_gem_request *req,
388 u32 invalidate_domains, u32 flush_domains)
390 u32 scratch_addr = req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
391 u32 flags = 0;
392 int ret;
394 flags |= PIPE_CONTROL_CS_STALL;
396 if (flush_domains) {
397 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
398 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
399 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
400 flags |= PIPE_CONTROL_FLUSH_ENABLE;
402 if (invalidate_domains) {
403 flags |= PIPE_CONTROL_TLB_INVALIDATE;
404 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
405 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
406 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
407 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
408 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
409 flags |= PIPE_CONTROL_QW_WRITE;
410 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
412 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
413 ret = gen8_emit_pipe_control(req,
414 PIPE_CONTROL_CS_STALL |
415 PIPE_CONTROL_STALL_AT_SCOREBOARD,
417 if (ret)
418 return ret;
421 return gen8_emit_pipe_control(req, flags, scratch_addr);
424 static void ring_write_tail(struct intel_engine_cs *engine,
425 u32 value)
427 struct drm_i915_private *dev_priv = engine->i915;
428 I915_WRITE_TAIL(engine, value);
431 u64 intel_engine_get_active_head(struct intel_engine_cs *engine)
433 struct drm_i915_private *dev_priv = engine->i915;
434 u64 acthd;
436 if (INTEL_GEN(dev_priv) >= 8)
437 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
438 RING_ACTHD_UDW(engine->mmio_base));
439 else if (INTEL_GEN(dev_priv) >= 4)
440 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
441 else
442 acthd = I915_READ(ACTHD);
444 return acthd;
447 static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
449 struct drm_i915_private *dev_priv = engine->i915;
450 u32 addr;
452 addr = dev_priv->status_page_dmah->busaddr;
453 if (INTEL_GEN(dev_priv) >= 4)
454 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
455 I915_WRITE(HWS_PGA, addr);
458 static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
460 struct drm_i915_private *dev_priv = engine->i915;
461 i915_reg_t mmio;
463 /* The ring status page addresses are no longer next to the rest of
464 * the ring registers as of gen7.
466 if (IS_GEN7(dev_priv)) {
467 switch (engine->id) {
468 case RCS:
469 mmio = RENDER_HWS_PGA_GEN7;
470 break;
471 case BCS:
472 mmio = BLT_HWS_PGA_GEN7;
473 break;
475 * VCS2 actually doesn't exist on Gen7. Only shut up
476 * gcc switch check warning
478 case VCS2:
479 case VCS:
480 mmio = BSD_HWS_PGA_GEN7;
481 break;
482 case VECS:
483 mmio = VEBOX_HWS_PGA_GEN7;
484 break;
486 } else if (IS_GEN6(dev_priv)) {
487 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
488 } else {
489 /* XXX: gen8 returns to sanity */
490 mmio = RING_HWS_PGA(engine->mmio_base);
493 I915_WRITE(mmio, (u32)engine->status_page.gfx_addr);
494 POSTING_READ(mmio);
497 * Flush the TLB for this page
499 * FIXME: These two bits have disappeared on gen8, so a question
500 * arises: do we still need this and if so how should we go about
501 * invalidating the TLB?
503 if (IS_GEN(dev_priv, 6, 7)) {
504 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
506 /* ring should be idle before issuing a sync flush*/
507 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
509 I915_WRITE(reg,
510 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
511 INSTPM_SYNC_FLUSH));
512 if (intel_wait_for_register(dev_priv,
513 reg, INSTPM_SYNC_FLUSH, 0,
514 1000))
515 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
516 engine->name);
520 static bool stop_ring(struct intel_engine_cs *engine)
522 struct drm_i915_private *dev_priv = engine->i915;
524 if (!IS_GEN2(dev_priv)) {
525 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
526 if (intel_wait_for_register(dev_priv,
527 RING_MI_MODE(engine->mmio_base),
528 MODE_IDLE,
529 MODE_IDLE,
530 1000)) {
531 DRM_ERROR("%s : timed out trying to stop ring\n",
532 engine->name);
533 /* Sometimes we observe that the idle flag is not
534 * set even though the ring is empty. So double
535 * check before giving up.
537 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
538 return false;
542 I915_WRITE_CTL(engine, 0);
543 I915_WRITE_HEAD(engine, 0);
544 engine->write_tail(engine, 0);
546 if (!IS_GEN2(dev_priv)) {
547 (void)I915_READ_CTL(engine);
548 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
551 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
554 static int init_ring_common(struct intel_engine_cs *engine)
556 struct drm_i915_private *dev_priv = engine->i915;
557 struct intel_ring *ring = engine->buffer;
558 struct drm_i915_gem_object *obj = ring->obj;
559 int ret = 0;
561 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
563 if (!stop_ring(engine)) {
564 /* G45 ring initialization often fails to reset head to zero */
565 DRM_DEBUG_KMS("%s head not reset to zero "
566 "ctl %08x head %08x tail %08x start %08x\n",
567 engine->name,
568 I915_READ_CTL(engine),
569 I915_READ_HEAD(engine),
570 I915_READ_TAIL(engine),
571 I915_READ_START(engine));
573 if (!stop_ring(engine)) {
574 DRM_ERROR("failed to set %s head to zero "
575 "ctl %08x head %08x tail %08x start %08x\n",
576 engine->name,
577 I915_READ_CTL(engine),
578 I915_READ_HEAD(engine),
579 I915_READ_TAIL(engine),
580 I915_READ_START(engine));
581 ret = -EIO;
582 goto out;
586 if (I915_NEED_GFX_HWS(dev_priv))
587 intel_ring_setup_status_page(engine);
588 else
589 ring_setup_phys_status_page(engine);
591 /* Enforce ordering by reading HEAD register back */
592 I915_READ_HEAD(engine);
594 /* Initialize the ring. This must happen _after_ we've cleared the ring
595 * registers with the above sequence (the readback of the HEAD registers
596 * also enforces ordering), otherwise the hw might lose the new ring
597 * register values. */
598 I915_WRITE_START(engine, i915_gem_obj_ggtt_offset(obj));
600 /* WaClearRingBufHeadRegAtInit:ctg,elk */
601 if (I915_READ_HEAD(engine))
602 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
603 engine->name, I915_READ_HEAD(engine));
604 I915_WRITE_HEAD(engine, 0);
605 (void)I915_READ_HEAD(engine);
607 I915_WRITE_CTL(engine,
608 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
609 | RING_VALID);
611 /* If the head is still not zero, the ring is dead */
612 if (wait_for((I915_READ_CTL(engine) & RING_VALID) != 0 &&
613 I915_READ_START(engine) == i915_gem_obj_ggtt_offset(obj) &&
614 (I915_READ_HEAD(engine) & HEAD_ADDR) == 0, 50)) {
615 DRM_ERROR("%s initialization failed "
616 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
617 engine->name,
618 I915_READ_CTL(engine),
619 I915_READ_CTL(engine) & RING_VALID,
620 I915_READ_HEAD(engine), I915_READ_TAIL(engine),
621 I915_READ_START(engine),
622 (unsigned long)i915_gem_obj_ggtt_offset(obj));
623 ret = -EIO;
624 goto out;
627 ring->last_retired_head = -1;
628 ring->head = I915_READ_HEAD(engine);
629 ring->tail = I915_READ_TAIL(engine) & TAIL_ADDR;
630 intel_ring_update_space(ring);
632 intel_engine_init_hangcheck(engine);
634 out:
635 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
637 return ret;
640 void intel_fini_pipe_control(struct intel_engine_cs *engine)
642 if (engine->scratch.obj == NULL)
643 return;
645 i915_gem_object_ggtt_unpin(engine->scratch.obj);
646 i915_gem_object_put(engine->scratch.obj);
647 engine->scratch.obj = NULL;
650 int intel_init_pipe_control(struct intel_engine_cs *engine, int size)
652 struct drm_i915_gem_object *obj;
653 int ret;
655 WARN_ON(engine->scratch.obj);
657 obj = i915_gem_object_create_stolen(&engine->i915->drm, size);
658 if (!obj)
659 obj = i915_gem_object_create(&engine->i915->drm, size);
660 if (IS_ERR(obj)) {
661 DRM_ERROR("Failed to allocate scratch page\n");
662 ret = PTR_ERR(obj);
663 goto err;
666 ret = i915_gem_obj_ggtt_pin(obj, 4096, PIN_HIGH);
667 if (ret)
668 goto err_unref;
670 engine->scratch.obj = obj;
671 engine->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
672 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
673 engine->name, engine->scratch.gtt_offset);
674 return 0;
676 err_unref:
677 i915_gem_object_put(engine->scratch.obj);
678 err:
679 return ret;
682 static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
684 struct intel_ring *ring = req->ring;
685 struct i915_workarounds *w = &req->i915->workarounds;
686 int ret, i;
688 if (w->count == 0)
689 return 0;
691 req->engine->gpu_caches_dirty = true;
692 ret = intel_engine_flush_all_caches(req);
693 if (ret)
694 return ret;
696 ret = intel_ring_begin(req, (w->count * 2 + 2));
697 if (ret)
698 return ret;
700 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
701 for (i = 0; i < w->count; i++) {
702 intel_ring_emit_reg(ring, w->reg[i].addr);
703 intel_ring_emit(ring, w->reg[i].value);
705 intel_ring_emit(ring, MI_NOOP);
707 intel_ring_advance(ring);
709 req->engine->gpu_caches_dirty = true;
710 ret = intel_engine_flush_all_caches(req);
711 if (ret)
712 return ret;
714 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
716 return 0;
719 static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
721 int ret;
723 ret = intel_ring_workarounds_emit(req);
724 if (ret != 0)
725 return ret;
727 ret = i915_gem_render_state_init(req);
728 if (ret)
729 return ret;
731 return 0;
734 static int wa_add(struct drm_i915_private *dev_priv,
735 i915_reg_t addr,
736 const u32 mask, const u32 val)
738 const u32 idx = dev_priv->workarounds.count;
740 if (WARN_ON(idx >= I915_MAX_WA_REGS))
741 return -ENOSPC;
743 dev_priv->workarounds.reg[idx].addr = addr;
744 dev_priv->workarounds.reg[idx].value = val;
745 dev_priv->workarounds.reg[idx].mask = mask;
747 dev_priv->workarounds.count++;
749 return 0;
752 #define WA_REG(addr, mask, val) do { \
753 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
754 if (r) \
755 return r; \
756 } while (0)
758 #define WA_SET_BIT_MASKED(addr, mask) \
759 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
761 #define WA_CLR_BIT_MASKED(addr, mask) \
762 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
764 #define WA_SET_FIELD_MASKED(addr, mask, value) \
765 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
767 #define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
768 #define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
770 #define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
772 static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
773 i915_reg_t reg)
775 struct drm_i915_private *dev_priv = engine->i915;
776 struct i915_workarounds *wa = &dev_priv->workarounds;
777 const uint32_t index = wa->hw_whitelist_count[engine->id];
779 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
780 return -EINVAL;
782 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
783 i915_mmio_reg_offset(reg));
784 wa->hw_whitelist_count[engine->id]++;
786 return 0;
789 static int gen8_init_workarounds(struct intel_engine_cs *engine)
791 struct drm_i915_private *dev_priv = engine->i915;
793 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
795 /* WaDisableAsyncFlipPerfMode:bdw,chv */
796 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
798 /* WaDisablePartialInstShootdown:bdw,chv */
799 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
800 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
802 /* Use Force Non-Coherent whenever executing a 3D context. This is a
803 * workaround for for a possible hang in the unlikely event a TLB
804 * invalidation occurs during a PSD flush.
806 /* WaForceEnableNonCoherent:bdw,chv */
807 /* WaHdcDisableFetchWhenMasked:bdw,chv */
808 WA_SET_BIT_MASKED(HDC_CHICKEN0,
809 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
810 HDC_FORCE_NON_COHERENT);
812 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
813 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
814 * polygons in the same 8x4 pixel/sample area to be processed without
815 * stalling waiting for the earlier ones to write to Hierarchical Z
816 * buffer."
818 * This optimization is off by default for BDW and CHV; turn it on.
820 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
822 /* Wa4x4STCOptimizationDisable:bdw,chv */
823 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
826 * BSpec recommends 8x4 when MSAA is used,
827 * however in practice 16x4 seems fastest.
829 * Note that PS/WM thread counts depend on the WIZ hashing
830 * disable bit, which we don't touch here, but it's good
831 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
833 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
834 GEN6_WIZ_HASHING_MASK,
835 GEN6_WIZ_HASHING_16x4);
837 return 0;
840 static int bdw_init_workarounds(struct intel_engine_cs *engine)
842 struct drm_i915_private *dev_priv = engine->i915;
843 int ret;
845 ret = gen8_init_workarounds(engine);
846 if (ret)
847 return ret;
849 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
850 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
852 /* WaDisableDopClockGating:bdw */
853 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
854 DOP_CLOCK_GATING_DISABLE);
856 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
857 GEN8_SAMPLER_POWER_BYPASS_DIS);
859 WA_SET_BIT_MASKED(HDC_CHICKEN0,
860 /* WaForceContextSaveRestoreNonCoherent:bdw */
861 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
862 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
863 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
865 return 0;
868 static int chv_init_workarounds(struct intel_engine_cs *engine)
870 struct drm_i915_private *dev_priv = engine->i915;
871 int ret;
873 ret = gen8_init_workarounds(engine);
874 if (ret)
875 return ret;
877 /* WaDisableThreadStallDopClockGating:chv */
878 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
880 /* Improve HiZ throughput on CHV. */
881 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
883 return 0;
886 static int gen9_init_workarounds(struct intel_engine_cs *engine)
888 struct drm_i915_private *dev_priv = engine->i915;
889 int ret;
891 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
892 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
894 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
895 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
896 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
898 /* WaDisableKillLogic:bxt,skl,kbl */
899 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
900 ECOCHK_DIS_TLB);
902 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
903 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
904 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
905 FLOW_CONTROL_ENABLE |
906 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
908 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
909 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
910 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
912 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
913 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
914 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
915 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
916 GEN9_DG_MIRROR_FIX_ENABLE);
918 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
919 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
920 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
921 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
922 GEN9_RHWO_OPTIMIZATION_DISABLE);
924 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
925 * but we do that in per ctx batchbuffer as there is an issue
926 * with this register not getting restored on ctx restore
930 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl */
931 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
932 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
933 GEN9_ENABLE_YV12_BUGFIX |
934 GEN9_ENABLE_GPGPU_PREEMPTION);
936 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
937 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
938 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
939 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
941 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
942 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
943 GEN9_CCS_TLB_PREFETCH_ENABLE);
945 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
946 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_C0) ||
947 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
948 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
949 PIXEL_MASK_CAMMING_DISABLE);
951 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
952 WA_SET_BIT_MASKED(HDC_CHICKEN0,
953 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
954 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
956 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
957 * both tied to WaForceContextSaveRestoreNonCoherent
958 * in some hsds for skl. We keep the tie for all gen9. The
959 * documentation is a bit hazy and so we want to get common behaviour,
960 * even though there is no clear evidence we would need both on kbl/bxt.
961 * This area has been source of system hangs so we play it safe
962 * and mimic the skl regardless of what bspec says.
964 * Use Force Non-Coherent whenever executing a 3D context. This
965 * is a workaround for a possible hang in the unlikely event
966 * a TLB invalidation occurs during a PSD flush.
969 /* WaForceEnableNonCoherent:skl,bxt,kbl */
970 WA_SET_BIT_MASKED(HDC_CHICKEN0,
971 HDC_FORCE_NON_COHERENT);
973 /* WaDisableHDCInvalidation:skl,bxt,kbl */
974 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
975 BDW_DISABLE_HDC_INVALIDATION);
977 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
978 if (IS_SKYLAKE(dev_priv) ||
979 IS_KABYLAKE(dev_priv) ||
980 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
981 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
982 GEN8_SAMPLER_POWER_BYPASS_DIS);
984 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
985 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
987 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
988 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
989 GEN8_LQSC_FLUSH_COHERENT_LINES));
991 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
992 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
993 if (ret)
994 return ret;
996 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
997 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
998 if (ret)
999 return ret;
1001 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
1002 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
1003 if (ret)
1004 return ret;
1006 return 0;
1009 static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
1011 struct drm_i915_private *dev_priv = engine->i915;
1012 u8 vals[3] = { 0, 0, 0 };
1013 unsigned int i;
1015 for (i = 0; i < 3; i++) {
1016 u8 ss;
1019 * Only consider slices where one, and only one, subslice has 7
1020 * EUs
1022 if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
1023 continue;
1026 * subslice_7eu[i] != 0 (because of the check above) and
1027 * ss_max == 4 (maximum number of subslices possible per slice)
1029 * -> 0 <= ss <= 3;
1031 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1032 vals[i] = 3 - ss;
1035 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1036 return 0;
1038 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1039 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1040 GEN9_IZ_HASHING_MASK(2) |
1041 GEN9_IZ_HASHING_MASK(1) |
1042 GEN9_IZ_HASHING_MASK(0),
1043 GEN9_IZ_HASHING(2, vals[2]) |
1044 GEN9_IZ_HASHING(1, vals[1]) |
1045 GEN9_IZ_HASHING(0, vals[0]));
1047 return 0;
1050 static int skl_init_workarounds(struct intel_engine_cs *engine)
1052 struct drm_i915_private *dev_priv = engine->i915;
1053 int ret;
1055 ret = gen9_init_workarounds(engine);
1056 if (ret)
1057 return ret;
1060 * Actual WA is to disable percontext preemption granularity control
1061 * until D0 which is the default case so this is equivalent to
1062 * !WaDisablePerCtxtPreemptionGranularityControl:skl
1064 if (IS_SKL_REVID(dev_priv, SKL_REVID_E0, REVID_FOREVER)) {
1065 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1066 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1069 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0)) {
1070 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1071 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1072 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1075 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1076 * involving this register should also be added to WA batch as required.
1078 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0))
1079 /* WaDisableLSQCROPERFforOCL:skl */
1080 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1081 GEN8_LQSC_RO_PERF_DIS);
1083 /* WaEnableGapsTsvCreditFix:skl */
1084 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, REVID_FOREVER)) {
1085 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1086 GEN9_GAPS_TSV_CREDIT_DISABLE));
1089 /* WaDisablePowerCompilerClockGating:skl */
1090 if (IS_SKL_REVID(dev_priv, SKL_REVID_B0, SKL_REVID_B0))
1091 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1092 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1094 /* WaBarrierPerformanceFixDisable:skl */
1095 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_D0))
1096 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1097 HDC_FENCE_DEST_SLM_DISABLE |
1098 HDC_BARRIER_PERFORMANCE_DISABLE);
1100 /* WaDisableSbeCacheDispatchPortSharing:skl */
1101 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0))
1102 WA_SET_BIT_MASKED(
1103 GEN7_HALF_SLICE_CHICKEN1,
1104 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1106 /* WaDisableGafsUnitClkGating:skl */
1107 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1109 /* WaInPlaceDecompressionHang:skl */
1110 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
1111 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1112 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1114 /* WaDisableLSQCROPERFforOCL:skl */
1115 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1116 if (ret)
1117 return ret;
1119 return skl_tune_iz_hashing(engine);
1122 static int bxt_init_workarounds(struct intel_engine_cs *engine)
1124 struct drm_i915_private *dev_priv = engine->i915;
1125 int ret;
1127 ret = gen9_init_workarounds(engine);
1128 if (ret)
1129 return ret;
1131 /* WaStoreMultiplePTEenable:bxt */
1132 /* This is a requirement according to Hardware specification */
1133 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
1134 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1136 /* WaSetClckGatingDisableMedia:bxt */
1137 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
1138 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1139 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1142 /* WaDisableThreadStallDopClockGating:bxt */
1143 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1144 STALL_DOP_GATING_DISABLE);
1146 /* WaDisablePooledEuLoadBalancingFix:bxt */
1147 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1148 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1149 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1152 /* WaDisableSbeCacheDispatchPortSharing:bxt */
1153 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
1154 WA_SET_BIT_MASKED(
1155 GEN7_HALF_SLICE_CHICKEN1,
1156 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1159 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1160 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1161 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
1162 /* WaDisableLSQCROPERFforOCL:bxt */
1163 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
1164 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
1165 if (ret)
1166 return ret;
1168 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1169 if (ret)
1170 return ret;
1173 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
1174 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
1175 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1176 L3_HIGH_PRIO_CREDITS(2));
1178 /* WaInsertDummyPushConstPs:bxt */
1179 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
1180 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1181 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1183 /* WaInPlaceDecompressionHang:bxt */
1184 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
1185 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1186 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1188 return 0;
1191 static int kbl_init_workarounds(struct intel_engine_cs *engine)
1193 struct drm_i915_private *dev_priv = engine->i915;
1194 int ret;
1196 ret = gen9_init_workarounds(engine);
1197 if (ret)
1198 return ret;
1200 /* WaEnableGapsTsvCreditFix:kbl */
1201 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1202 GEN9_GAPS_TSV_CREDIT_DISABLE));
1204 /* WaDisableDynamicCreditSharing:kbl */
1205 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1206 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1207 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1209 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1210 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1211 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1212 HDC_FENCE_DEST_SLM_DISABLE);
1214 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1215 * involving this register should also be added to WA batch as required.
1217 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1218 /* WaDisableLSQCROPERFforOCL:kbl */
1219 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1220 GEN8_LQSC_RO_PERF_DIS);
1222 /* WaInsertDummyPushConstPs:kbl */
1223 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1224 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1225 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1227 /* WaDisableGafsUnitClkGating:kbl */
1228 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1230 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1231 WA_SET_BIT_MASKED(
1232 GEN7_HALF_SLICE_CHICKEN1,
1233 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1235 /* WaInPlaceDecompressionHang:kbl */
1236 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1237 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1239 /* WaDisableLSQCROPERFforOCL:kbl */
1240 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1241 if (ret)
1242 return ret;
1244 return 0;
1247 int init_workarounds_ring(struct intel_engine_cs *engine)
1249 struct drm_i915_private *dev_priv = engine->i915;
1251 WARN_ON(engine->id != RCS);
1253 dev_priv->workarounds.count = 0;
1254 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
1256 if (IS_BROADWELL(dev_priv))
1257 return bdw_init_workarounds(engine);
1259 if (IS_CHERRYVIEW(dev_priv))
1260 return chv_init_workarounds(engine);
1262 if (IS_SKYLAKE(dev_priv))
1263 return skl_init_workarounds(engine);
1265 if (IS_BROXTON(dev_priv))
1266 return bxt_init_workarounds(engine);
1268 if (IS_KABYLAKE(dev_priv))
1269 return kbl_init_workarounds(engine);
1271 return 0;
1274 static int init_render_ring(struct intel_engine_cs *engine)
1276 struct drm_i915_private *dev_priv = engine->i915;
1277 int ret = init_ring_common(engine);
1278 if (ret)
1279 return ret;
1281 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1282 if (IS_GEN(dev_priv, 4, 6))
1283 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
1285 /* We need to disable the AsyncFlip performance optimisations in order
1286 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1287 * programmed to '1' on all products.
1289 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
1291 if (IS_GEN(dev_priv, 6, 7))
1292 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1294 /* Required for the hardware to program scanline values for waiting */
1295 /* WaEnableFlushTlbInvalidationMode:snb */
1296 if (IS_GEN6(dev_priv))
1297 I915_WRITE(GFX_MODE,
1298 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
1300 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
1301 if (IS_GEN7(dev_priv))
1302 I915_WRITE(GFX_MODE_GEN7,
1303 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
1304 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
1306 if (IS_GEN6(dev_priv)) {
1307 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1308 * "If this bit is set, STCunit will have LRA as replacement
1309 * policy. [...] This bit must be reset. LRA replacement
1310 * policy is not supported."
1312 I915_WRITE(CACHE_MODE_0,
1313 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
1316 if (IS_GEN(dev_priv, 6, 7))
1317 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1319 if (INTEL_INFO(dev_priv)->gen >= 6)
1320 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
1322 return init_workarounds_ring(engine);
1325 static void render_ring_cleanup(struct intel_engine_cs *engine)
1327 struct drm_i915_private *dev_priv = engine->i915;
1329 if (dev_priv->semaphore_obj) {
1330 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1331 i915_gem_object_put(dev_priv->semaphore_obj);
1332 dev_priv->semaphore_obj = NULL;
1335 intel_fini_pipe_control(engine);
1338 static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
1339 unsigned int num_dwords)
1341 #define MBOX_UPDATE_DWORDS 8
1342 struct intel_ring *signaller = signaller_req->ring;
1343 struct drm_i915_private *dev_priv = signaller_req->i915;
1344 struct intel_engine_cs *waiter;
1345 enum intel_engine_id id;
1346 int ret, num_rings;
1348 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
1349 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1350 #undef MBOX_UPDATE_DWORDS
1352 ret = intel_ring_begin(signaller_req, num_dwords);
1353 if (ret)
1354 return ret;
1356 for_each_engine_id(waiter, dev_priv, id) {
1357 u64 gtt_offset =
1358 signaller_req->engine->semaphore.signal_ggtt[id];
1359 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1360 continue;
1362 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1363 intel_ring_emit(signaller,
1364 PIPE_CONTROL_GLOBAL_GTT_IVB |
1365 PIPE_CONTROL_QW_WRITE |
1366 PIPE_CONTROL_CS_STALL);
1367 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1368 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
1369 intel_ring_emit(signaller, signaller_req->fence.seqno);
1370 intel_ring_emit(signaller, 0);
1371 intel_ring_emit(signaller,
1372 MI_SEMAPHORE_SIGNAL |
1373 MI_SEMAPHORE_TARGET(waiter->hw_id));
1374 intel_ring_emit(signaller, 0);
1377 return 0;
1380 static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
1381 unsigned int num_dwords)
1383 #define MBOX_UPDATE_DWORDS 6
1384 struct intel_ring *signaller = signaller_req->ring;
1385 struct drm_i915_private *dev_priv = signaller_req->i915;
1386 struct intel_engine_cs *waiter;
1387 enum intel_engine_id id;
1388 int ret, num_rings;
1390 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
1391 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1392 #undef MBOX_UPDATE_DWORDS
1394 ret = intel_ring_begin(signaller_req, num_dwords);
1395 if (ret)
1396 return ret;
1398 for_each_engine_id(waiter, dev_priv, id) {
1399 u64 gtt_offset =
1400 signaller_req->engine->semaphore.signal_ggtt[id];
1401 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1402 continue;
1404 intel_ring_emit(signaller,
1405 (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW);
1406 intel_ring_emit(signaller,
1407 lower_32_bits(gtt_offset) |
1408 MI_FLUSH_DW_USE_GTT);
1409 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
1410 intel_ring_emit(signaller, signaller_req->fence.seqno);
1411 intel_ring_emit(signaller,
1412 MI_SEMAPHORE_SIGNAL |
1413 MI_SEMAPHORE_TARGET(waiter->hw_id));
1414 intel_ring_emit(signaller, 0);
1417 return 0;
1420 static int gen6_signal(struct drm_i915_gem_request *signaller_req,
1421 unsigned int num_dwords)
1423 struct intel_ring *signaller = signaller_req->ring;
1424 struct drm_i915_private *dev_priv = signaller_req->i915;
1425 struct intel_engine_cs *useless;
1426 enum intel_engine_id id;
1427 int ret, num_rings;
1429 #define MBOX_UPDATE_DWORDS 3
1430 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
1431 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1432 #undef MBOX_UPDATE_DWORDS
1434 ret = intel_ring_begin(signaller_req, num_dwords);
1435 if (ret)
1436 return ret;
1438 for_each_engine_id(useless, dev_priv, id) {
1439 i915_reg_t mbox_reg =
1440 signaller_req->engine->semaphore.mbox.signal[id];
1442 if (i915_mmio_reg_valid(mbox_reg)) {
1443 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1444 intel_ring_emit_reg(signaller, mbox_reg);
1445 intel_ring_emit(signaller, signaller_req->fence.seqno);
1449 /* If num_dwords was rounded, make sure the tail pointer is correct */
1450 if (num_rings % 2 == 0)
1451 intel_ring_emit(signaller, MI_NOOP);
1453 return 0;
1457 * gen6_add_request - Update the semaphore mailbox registers
1459 * @request - request to write to the ring
1461 * Update the mailbox registers in the *other* rings with the current seqno.
1462 * This acts like a signal in the canonical semaphore.
1464 static int
1465 gen6_add_request(struct drm_i915_gem_request *req)
1467 struct intel_engine_cs *engine = req->engine;
1468 struct intel_ring *ring = req->ring;
1469 int ret;
1471 if (engine->semaphore.signal)
1472 ret = engine->semaphore.signal(req, 4);
1473 else
1474 ret = intel_ring_begin(req, 4);
1476 if (ret)
1477 return ret;
1479 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1480 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1481 intel_ring_emit(ring, req->fence.seqno);
1482 intel_ring_emit(ring, MI_USER_INTERRUPT);
1483 __intel_engine_submit(engine);
1485 return 0;
1488 static int
1489 gen8_render_add_request(struct drm_i915_gem_request *req)
1491 struct intel_engine_cs *engine = req->engine;
1492 struct intel_ring *ring = req->ring;
1493 int ret;
1495 if (engine->semaphore.signal)
1496 ret = engine->semaphore.signal(req, 8);
1497 else
1498 ret = intel_ring_begin(req, 8);
1499 if (ret)
1500 return ret;
1502 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
1503 intel_ring_emit(ring, (PIPE_CONTROL_GLOBAL_GTT_IVB |
1504 PIPE_CONTROL_CS_STALL |
1505 PIPE_CONTROL_QW_WRITE));
1506 intel_ring_emit(ring, intel_hws_seqno_address(engine));
1507 intel_ring_emit(ring, 0);
1508 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
1509 /* We're thrashing one dword of HWS. */
1510 intel_ring_emit(ring, 0);
1511 intel_ring_emit(ring, MI_USER_INTERRUPT);
1512 intel_ring_emit(ring, MI_NOOP);
1513 __intel_engine_submit(engine);
1515 return 0;
1518 static inline bool i915_gem_has_seqno_wrapped(struct drm_i915_private *dev_priv,
1519 u32 seqno)
1521 return dev_priv->last_seqno < seqno;
1525 * intel_ring_sync - sync the waiter to the signaller on seqno
1527 * @waiter - ring that is waiting
1528 * @signaller - ring which has, or will signal
1529 * @seqno - seqno which the waiter will block on
1532 static int
1533 gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
1534 struct intel_engine_cs *signaller,
1535 u32 seqno)
1537 struct intel_ring *waiter = waiter_req->ring;
1538 struct drm_i915_private *dev_priv = waiter_req->i915;
1539 u64 offset = GEN8_WAIT_OFFSET(waiter_req->engine, signaller->id);
1540 struct i915_hw_ppgtt *ppgtt;
1541 int ret;
1543 ret = intel_ring_begin(waiter_req, 4);
1544 if (ret)
1545 return ret;
1547 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1548 MI_SEMAPHORE_GLOBAL_GTT |
1549 MI_SEMAPHORE_SAD_GTE_SDD);
1550 intel_ring_emit(waiter, seqno);
1551 intel_ring_emit(waiter, lower_32_bits(offset));
1552 intel_ring_emit(waiter, upper_32_bits(offset));
1553 intel_ring_advance(waiter);
1555 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1556 * pagetables and we must reload them before executing the batch.
1557 * We do this on the i915_switch_context() following the wait and
1558 * before the dispatch.
1560 ppgtt = waiter_req->ctx->ppgtt;
1561 if (ppgtt && waiter_req->engine->id != RCS)
1562 ppgtt->pd_dirty_rings |= intel_engine_flag(waiter_req->engine);
1563 return 0;
1566 static int
1567 gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
1568 struct intel_engine_cs *signaller,
1569 u32 seqno)
1571 struct intel_ring *waiter = waiter_req->ring;
1572 u32 dw1 = MI_SEMAPHORE_MBOX |
1573 MI_SEMAPHORE_COMPARE |
1574 MI_SEMAPHORE_REGISTER;
1575 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter_req->engine->id];
1576 int ret;
1578 /* Throughout all of the GEM code, seqno passed implies our current
1579 * seqno is >= the last seqno executed. However for hardware the
1580 * comparison is strictly greater than.
1582 seqno -= 1;
1584 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
1586 ret = intel_ring_begin(waiter_req, 4);
1587 if (ret)
1588 return ret;
1590 /* If seqno wrap happened, omit the wait with no-ops */
1591 if (likely(!i915_gem_has_seqno_wrapped(waiter_req->i915, seqno))) {
1592 intel_ring_emit(waiter, dw1 | wait_mbox);
1593 intel_ring_emit(waiter, seqno);
1594 intel_ring_emit(waiter, 0);
1595 intel_ring_emit(waiter, MI_NOOP);
1596 } else {
1597 intel_ring_emit(waiter, MI_NOOP);
1598 intel_ring_emit(waiter, MI_NOOP);
1599 intel_ring_emit(waiter, MI_NOOP);
1600 intel_ring_emit(waiter, MI_NOOP);
1602 intel_ring_advance(waiter);
1604 return 0;
1607 static void
1608 gen5_seqno_barrier(struct intel_engine_cs *engine)
1610 /* MI_STORE are internally buffered by the GPU and not flushed
1611 * either by MI_FLUSH or SyncFlush or any other combination of
1612 * MI commands.
1614 * "Only the submission of the store operation is guaranteed.
1615 * The write result will be complete (coherent) some time later
1616 * (this is practically a finite period but there is no guaranteed
1617 * latency)."
1619 * Empirically, we observe that we need a delay of at least 75us to
1620 * be sure that the seqno write is visible by the CPU.
1622 usleep_range(125, 250);
1625 static void
1626 gen6_seqno_barrier(struct intel_engine_cs *engine)
1628 struct drm_i915_private *dev_priv = engine->i915;
1630 /* Workaround to force correct ordering between irq and seqno writes on
1631 * ivb (and maybe also on snb) by reading from a CS register (like
1632 * ACTHD) before reading the status page.
1634 * Note that this effectively stalls the read by the time it takes to
1635 * do a memory transaction, which more or less ensures that the write
1636 * from the GPU has sufficient time to invalidate the CPU cacheline.
1637 * Alternatively we could delay the interrupt from the CS ring to give
1638 * the write time to land, but that would incur a delay after every
1639 * batch i.e. much more frequent than a delay when waiting for the
1640 * interrupt (with the same net latency).
1642 * Also note that to prevent whole machine hangs on gen7, we have to
1643 * take the spinlock to guard against concurrent cacheline access.
1645 spin_lock_irq(&dev_priv->uncore.lock);
1646 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
1647 spin_unlock_irq(&dev_priv->uncore.lock);
1650 static void
1651 gen5_irq_enable(struct intel_engine_cs *engine)
1653 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
1656 static void
1657 gen5_irq_disable(struct intel_engine_cs *engine)
1659 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
1662 static void
1663 i9xx_irq_enable(struct intel_engine_cs *engine)
1665 struct drm_i915_private *dev_priv = engine->i915;
1667 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1668 I915_WRITE(IMR, dev_priv->irq_mask);
1669 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1672 static void
1673 i9xx_irq_disable(struct intel_engine_cs *engine)
1675 struct drm_i915_private *dev_priv = engine->i915;
1677 dev_priv->irq_mask |= engine->irq_enable_mask;
1678 I915_WRITE(IMR, dev_priv->irq_mask);
1681 static void
1682 i8xx_irq_enable(struct intel_engine_cs *engine)
1684 struct drm_i915_private *dev_priv = engine->i915;
1686 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1687 I915_WRITE16(IMR, dev_priv->irq_mask);
1688 POSTING_READ16(RING_IMR(engine->mmio_base));
1691 static void
1692 i8xx_irq_disable(struct intel_engine_cs *engine)
1694 struct drm_i915_private *dev_priv = engine->i915;
1696 dev_priv->irq_mask |= engine->irq_enable_mask;
1697 I915_WRITE16(IMR, dev_priv->irq_mask);
1700 static int
1701 bsd_ring_flush(struct drm_i915_gem_request *req,
1702 u32 invalidate_domains,
1703 u32 flush_domains)
1705 struct intel_ring *ring = req->ring;
1706 int ret;
1708 ret = intel_ring_begin(req, 2);
1709 if (ret)
1710 return ret;
1712 intel_ring_emit(ring, MI_FLUSH);
1713 intel_ring_emit(ring, MI_NOOP);
1714 intel_ring_advance(ring);
1715 return 0;
1718 static int
1719 i9xx_add_request(struct drm_i915_gem_request *req)
1721 struct intel_ring *ring = req->ring;
1722 int ret;
1724 ret = intel_ring_begin(req, 4);
1725 if (ret)
1726 return ret;
1728 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1729 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1730 intel_ring_emit(ring, req->fence.seqno);
1731 intel_ring_emit(ring, MI_USER_INTERRUPT);
1732 __intel_engine_submit(req->engine);
1734 return 0;
1737 static void
1738 gen6_irq_enable(struct intel_engine_cs *engine)
1740 struct drm_i915_private *dev_priv = engine->i915;
1742 I915_WRITE_IMR(engine,
1743 ~(engine->irq_enable_mask |
1744 engine->irq_keep_mask));
1745 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
1748 static void
1749 gen6_irq_disable(struct intel_engine_cs *engine)
1751 struct drm_i915_private *dev_priv = engine->i915;
1753 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
1754 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
1757 static void
1758 hsw_vebox_irq_enable(struct intel_engine_cs *engine)
1760 struct drm_i915_private *dev_priv = engine->i915;
1762 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
1763 gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
1766 static void
1767 hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1769 struct drm_i915_private *dev_priv = engine->i915;
1771 I915_WRITE_IMR(engine, ~0);
1772 gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
1775 static void
1776 gen8_irq_enable(struct intel_engine_cs *engine)
1778 struct drm_i915_private *dev_priv = engine->i915;
1780 I915_WRITE_IMR(engine,
1781 ~(engine->irq_enable_mask |
1782 engine->irq_keep_mask));
1783 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1786 static void
1787 gen8_irq_disable(struct intel_engine_cs *engine)
1789 struct drm_i915_private *dev_priv = engine->i915;
1791 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
1794 static int
1795 i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
1796 u64 offset, u32 length,
1797 unsigned dispatch_flags)
1799 struct intel_ring *ring = req->ring;
1800 int ret;
1802 ret = intel_ring_begin(req, 2);
1803 if (ret)
1804 return ret;
1806 intel_ring_emit(ring,
1807 MI_BATCH_BUFFER_START |
1808 MI_BATCH_GTT |
1809 (dispatch_flags & I915_DISPATCH_SECURE ?
1810 0 : MI_BATCH_NON_SECURE_I965));
1811 intel_ring_emit(ring, offset);
1812 intel_ring_advance(ring);
1814 return 0;
1817 /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1818 #define I830_BATCH_LIMIT (256*1024)
1819 #define I830_TLB_ENTRIES (2)
1820 #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
1821 static int
1822 i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
1823 u64 offset, u32 len,
1824 unsigned dispatch_flags)
1826 struct intel_ring *ring = req->ring;
1827 u32 cs_offset = req->engine->scratch.gtt_offset;
1828 int ret;
1830 ret = intel_ring_begin(req, 6);
1831 if (ret)
1832 return ret;
1834 /* Evict the invalid PTE TLBs */
1835 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1836 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1837 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1838 intel_ring_emit(ring, cs_offset);
1839 intel_ring_emit(ring, 0xdeadbeef);
1840 intel_ring_emit(ring, MI_NOOP);
1841 intel_ring_advance(ring);
1843 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
1844 if (len > I830_BATCH_LIMIT)
1845 return -ENOSPC;
1847 ret = intel_ring_begin(req, 6 + 2);
1848 if (ret)
1849 return ret;
1851 /* Blit the batch (which has now all relocs applied) to the
1852 * stable batch scratch bo area (so that the CS never
1853 * stumbles over its tlb invalidation bug) ...
1855 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1856 intel_ring_emit(ring,
1857 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
1858 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1859 intel_ring_emit(ring, cs_offset);
1860 intel_ring_emit(ring, 4096);
1861 intel_ring_emit(ring, offset);
1863 intel_ring_emit(ring, MI_FLUSH);
1864 intel_ring_emit(ring, MI_NOOP);
1865 intel_ring_advance(ring);
1867 /* ... and execute it. */
1868 offset = cs_offset;
1871 ret = intel_ring_begin(req, 2);
1872 if (ret)
1873 return ret;
1875 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1876 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1877 0 : MI_BATCH_NON_SECURE));
1878 intel_ring_advance(ring);
1880 return 0;
1883 static int
1884 i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
1885 u64 offset, u32 len,
1886 unsigned dispatch_flags)
1888 struct intel_ring *ring = req->ring;
1889 int ret;
1891 ret = intel_ring_begin(req, 2);
1892 if (ret)
1893 return ret;
1895 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1896 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1897 0 : MI_BATCH_NON_SECURE));
1898 intel_ring_advance(ring);
1900 return 0;
1903 static void cleanup_phys_status_page(struct intel_engine_cs *engine)
1905 struct drm_i915_private *dev_priv = engine->i915;
1907 if (!dev_priv->status_page_dmah)
1908 return;
1910 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
1911 engine->status_page.page_addr = NULL;
1914 static void cleanup_status_page(struct intel_engine_cs *engine)
1916 struct drm_i915_gem_object *obj;
1918 obj = engine->status_page.obj;
1919 if (obj == NULL)
1920 return;
1922 kunmap(sg_page(obj->pages->sgl));
1923 i915_gem_object_ggtt_unpin(obj);
1924 i915_gem_object_put(obj);
1925 engine->status_page.obj = NULL;
1928 static int init_status_page(struct intel_engine_cs *engine)
1930 struct drm_i915_gem_object *obj = engine->status_page.obj;
1932 if (obj == NULL) {
1933 unsigned flags;
1934 int ret;
1936 obj = i915_gem_object_create(&engine->i915->drm, 4096);
1937 if (IS_ERR(obj)) {
1938 DRM_ERROR("Failed to allocate status page\n");
1939 return PTR_ERR(obj);
1942 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1943 if (ret)
1944 goto err_unref;
1946 flags = 0;
1947 if (!HAS_LLC(engine->i915))
1948 /* On g33, we cannot place HWS above 256MiB, so
1949 * restrict its pinning to the low mappable arena.
1950 * Though this restriction is not documented for
1951 * gen4, gen5, or byt, they also behave similarly
1952 * and hang if the HWS is placed at the top of the
1953 * GTT. To generalise, it appears that all !llc
1954 * platforms have issues with us placing the HWS
1955 * above the mappable region (even though we never
1956 * actualy map it).
1958 flags |= PIN_MAPPABLE;
1959 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
1960 if (ret) {
1961 err_unref:
1962 i915_gem_object_put(obj);
1963 return ret;
1966 engine->status_page.obj = obj;
1969 engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1970 engine->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
1971 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
1973 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1974 engine->name, engine->status_page.gfx_addr);
1976 return 0;
1979 static int init_phys_status_page(struct intel_engine_cs *engine)
1981 struct drm_i915_private *dev_priv = engine->i915;
1983 if (!dev_priv->status_page_dmah) {
1984 dev_priv->status_page_dmah =
1985 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
1986 if (!dev_priv->status_page_dmah)
1987 return -ENOMEM;
1990 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1991 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
1993 return 0;
1996 void intel_unpin_ring(struct intel_ring *ring)
1998 GEM_BUG_ON(!ring->vma);
1999 GEM_BUG_ON(!ring->vaddr);
2001 if (HAS_LLC(ring->obj->base.dev) && !ring->obj->stolen)
2002 i915_gem_object_unpin_map(ring->obj);
2003 else
2004 i915_vma_unpin_iomap(ring->vma);
2005 ring->vaddr = NULL;
2007 i915_gem_object_ggtt_unpin(ring->obj);
2008 ring->vma = NULL;
2011 int intel_pin_and_map_ring(struct drm_i915_private *dev_priv,
2012 struct intel_ring *ring)
2014 struct drm_i915_gem_object *obj = ring->obj;
2015 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
2016 unsigned flags = PIN_OFFSET_BIAS | 4096;
2017 void *addr;
2018 int ret;
2020 if (HAS_LLC(dev_priv) && !obj->stolen) {
2021 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
2022 if (ret)
2023 return ret;
2025 ret = i915_gem_object_set_to_cpu_domain(obj, true);
2026 if (ret)
2027 goto err_unpin;
2029 addr = i915_gem_object_pin_map(obj);
2030 if (IS_ERR(addr)) {
2031 ret = PTR_ERR(addr);
2032 goto err_unpin;
2034 } else {
2035 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
2036 flags | PIN_MAPPABLE);
2037 if (ret)
2038 return ret;
2040 ret = i915_gem_object_set_to_gtt_domain(obj, true);
2041 if (ret)
2042 goto err_unpin;
2044 /* Access through the GTT requires the device to be awake. */
2045 assert_rpm_wakelock_held(dev_priv);
2047 addr = (void __force *)
2048 i915_vma_pin_iomap(i915_gem_obj_to_ggtt(obj));
2049 if (IS_ERR(addr)) {
2050 ret = PTR_ERR(addr);
2051 goto err_unpin;
2055 ring->vaddr = addr;
2056 ring->vma = i915_gem_obj_to_ggtt(obj);
2057 return 0;
2059 err_unpin:
2060 i915_gem_object_ggtt_unpin(obj);
2061 return ret;
2064 static void intel_destroy_ringbuffer_obj(struct intel_ring *ring)
2066 i915_gem_object_put(ring->obj);
2067 ring->obj = NULL;
2070 static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
2071 struct intel_ring *ring)
2073 struct drm_i915_gem_object *obj;
2075 obj = NULL;
2076 if (!HAS_LLC(dev))
2077 obj = i915_gem_object_create_stolen(dev, ring->size);
2078 if (obj == NULL)
2079 obj = i915_gem_object_create(dev, ring->size);
2080 if (IS_ERR(obj))
2081 return PTR_ERR(obj);
2083 /* mark ring buffers as read-only from GPU side by default */
2084 obj->gt_ro = 1;
2086 ring->obj = obj;
2088 return 0;
2091 struct intel_ring *
2092 intel_engine_create_ring(struct intel_engine_cs *engine, int size)
2094 struct intel_ring *ring;
2095 int ret;
2097 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2098 if (ring == NULL) {
2099 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2100 engine->name);
2101 return ERR_PTR(-ENOMEM);
2104 ring->engine = engine;
2105 list_add(&ring->link, &engine->buffers);
2107 ring->size = size;
2108 /* Workaround an erratum on the i830 which causes a hang if
2109 * the TAIL pointer points to within the last 2 cachelines
2110 * of the buffer.
2112 ring->effective_size = size;
2113 if (IS_I830(engine->i915) || IS_845G(engine->i915))
2114 ring->effective_size -= 2 * CACHELINE_BYTES;
2116 ring->last_retired_head = -1;
2117 intel_ring_update_space(ring);
2119 ret = intel_alloc_ringbuffer_obj(&engine->i915->drm, ring);
2120 if (ret) {
2121 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
2122 engine->name, ret);
2123 list_del(&ring->link);
2124 kfree(ring);
2125 return ERR_PTR(ret);
2128 return ring;
2131 void
2132 intel_ring_free(struct intel_ring *ring)
2134 intel_destroy_ringbuffer_obj(ring);
2135 list_del(&ring->link);
2136 kfree(ring);
2139 static int intel_ring_context_pin(struct i915_gem_context *ctx,
2140 struct intel_engine_cs *engine)
2142 struct intel_context *ce = &ctx->engine[engine->id];
2143 int ret;
2145 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
2147 if (ce->pin_count++)
2148 return 0;
2150 if (ce->state) {
2151 ret = i915_gem_obj_ggtt_pin(ce->state, ctx->ggtt_alignment, 0);
2152 if (ret)
2153 goto error;
2156 /* The kernel context is only used as a placeholder for flushing the
2157 * active context. It is never used for submitting user rendering and
2158 * as such never requires the golden render context, and so we can skip
2159 * emitting it when we switch to the kernel context. This is required
2160 * as during eviction we cannot allocate and pin the renderstate in
2161 * order to initialise the context.
2163 if (ctx == ctx->i915->kernel_context)
2164 ce->initialised = true;
2166 i915_gem_context_get(ctx);
2167 return 0;
2169 error:
2170 ce->pin_count = 0;
2171 return ret;
2174 static void intel_ring_context_unpin(struct i915_gem_context *ctx,
2175 struct intel_engine_cs *engine)
2177 struct intel_context *ce = &ctx->engine[engine->id];
2179 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
2181 if (--ce->pin_count)
2182 return;
2184 if (ce->state)
2185 i915_gem_object_ggtt_unpin(ce->state);
2187 i915_gem_context_put(ctx);
2190 static int intel_init_ring_buffer(struct intel_engine_cs *engine)
2192 struct drm_i915_private *dev_priv = engine->i915;
2193 struct intel_ring *ring;
2194 int ret;
2196 WARN_ON(engine->buffer);
2198 intel_engine_setup_common(engine);
2200 memset(engine->semaphore.sync_seqno, 0,
2201 sizeof(engine->semaphore.sync_seqno));
2203 ret = intel_engine_init_common(engine);
2204 if (ret)
2205 goto error;
2207 /* We may need to do things with the shrinker which
2208 * require us to immediately switch back to the default
2209 * context. This can cause a problem as pinning the
2210 * default context also requires GTT space which may not
2211 * be available. To avoid this we always pin the default
2212 * context.
2214 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2215 if (ret)
2216 goto error;
2218 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
2219 if (IS_ERR(ring)) {
2220 ret = PTR_ERR(ring);
2221 goto error;
2223 engine->buffer = ring;
2225 if (I915_NEED_GFX_HWS(dev_priv)) {
2226 ret = init_status_page(engine);
2227 if (ret)
2228 goto error;
2229 } else {
2230 WARN_ON(engine->id != RCS);
2231 ret = init_phys_status_page(engine);
2232 if (ret)
2233 goto error;
2236 ret = intel_pin_and_map_ring(dev_priv, ring);
2237 if (ret) {
2238 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2239 engine->name, ret);
2240 intel_destroy_ringbuffer_obj(ring);
2241 goto error;
2244 return 0;
2246 error:
2247 intel_engine_cleanup(engine);
2248 return ret;
2251 void intel_engine_cleanup(struct intel_engine_cs *engine)
2253 struct drm_i915_private *dev_priv;
2255 if (!intel_engine_initialized(engine))
2256 return;
2258 dev_priv = engine->i915;
2260 if (engine->buffer) {
2261 intel_engine_stop(engine);
2262 WARN_ON(!IS_GEN2(dev_priv) && (I915_READ_MODE(engine) & MODE_IDLE) == 0);
2264 intel_unpin_ring(engine->buffer);
2265 intel_ring_free(engine->buffer);
2266 engine->buffer = NULL;
2269 if (engine->cleanup)
2270 engine->cleanup(engine);
2272 if (I915_NEED_GFX_HWS(dev_priv)) {
2273 cleanup_status_page(engine);
2274 } else {
2275 WARN_ON(engine->id != RCS);
2276 cleanup_phys_status_page(engine);
2279 intel_engine_cleanup_cmd_parser(engine);
2280 i915_gem_batch_pool_fini(&engine->batch_pool);
2281 intel_engine_fini_breadcrumbs(engine);
2283 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2285 engine->i915 = NULL;
2288 int intel_engine_idle(struct intel_engine_cs *engine)
2290 struct drm_i915_gem_request *req;
2292 /* Wait upon the last request to be completed */
2293 if (list_empty(&engine->request_list))
2294 return 0;
2296 req = list_entry(engine->request_list.prev,
2297 struct drm_i915_gem_request,
2298 list);
2300 /* Make sure we do not trigger any retires */
2301 return __i915_wait_request(req,
2302 req->i915->mm.interruptible,
2303 NULL, NULL);
2306 int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
2308 int ret;
2310 /* Flush enough space to reduce the likelihood of waiting after
2311 * we start building the request - in which case we will just
2312 * have to repeat work.
2314 request->reserved_space += LEGACY_REQUEST_SIZE;
2316 request->ring = request->engine->buffer;
2318 ret = intel_ring_begin(request, 0);
2319 if (ret)
2320 return ret;
2322 request->reserved_space -= LEGACY_REQUEST_SIZE;
2323 return 0;
2326 static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
2328 struct intel_ring *ring = req->ring;
2329 struct intel_engine_cs *engine = req->engine;
2330 struct drm_i915_gem_request *target;
2332 intel_ring_update_space(ring);
2333 if (ring->space >= bytes)
2334 return 0;
2337 * Space is reserved in the ringbuffer for finalising the request,
2338 * as that cannot be allowed to fail. During request finalisation,
2339 * reserved_space is set to 0 to stop the overallocation and the
2340 * assumption is that then we never need to wait (which has the
2341 * risk of failing with EINTR).
2343 * See also i915_gem_request_alloc() and i915_add_request().
2345 GEM_BUG_ON(!req->reserved_space);
2347 list_for_each_entry(target, &engine->request_list, list) {
2348 unsigned space;
2351 * The request queue is per-engine, so can contain requests
2352 * from multiple ringbuffers. Here, we must ignore any that
2353 * aren't from the ringbuffer we're considering.
2355 if (target->ring != ring)
2356 continue;
2358 /* Would completion of this request free enough space? */
2359 space = __intel_ring_space(target->postfix, ring->tail,
2360 ring->size);
2361 if (space >= bytes)
2362 break;
2365 if (WARN_ON(&target->list == &engine->request_list))
2366 return -ENOSPC;
2368 return i915_wait_request(target);
2371 int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
2373 struct intel_ring *ring = req->ring;
2374 int remain_actual = ring->size - ring->tail;
2375 int remain_usable = ring->effective_size - ring->tail;
2376 int bytes = num_dwords * sizeof(u32);
2377 int total_bytes, wait_bytes;
2378 bool need_wrap = false;
2380 total_bytes = bytes + req->reserved_space;
2382 if (unlikely(bytes > remain_usable)) {
2384 * Not enough space for the basic request. So need to flush
2385 * out the remainder and then wait for base + reserved.
2387 wait_bytes = remain_actual + total_bytes;
2388 need_wrap = true;
2389 } else if (unlikely(total_bytes > remain_usable)) {
2391 * The base request will fit but the reserved space
2392 * falls off the end. So we don't need an immediate wrap
2393 * and only need to effectively wait for the reserved
2394 * size space from the start of ringbuffer.
2396 wait_bytes = remain_actual + req->reserved_space;
2397 } else {
2398 /* No wrapping required, just waiting. */
2399 wait_bytes = total_bytes;
2402 if (wait_bytes > ring->space) {
2403 int ret = wait_for_space(req, wait_bytes);
2404 if (unlikely(ret))
2405 return ret;
2407 intel_ring_update_space(ring);
2408 if (unlikely(ring->space < wait_bytes))
2409 return -EAGAIN;
2412 if (unlikely(need_wrap)) {
2413 GEM_BUG_ON(remain_actual > ring->space);
2414 GEM_BUG_ON(ring->tail + remain_actual > ring->size);
2416 /* Fill the tail with MI_NOOP */
2417 memset(ring->vaddr + ring->tail, 0, remain_actual);
2418 ring->tail = 0;
2419 ring->space -= remain_actual;
2422 ring->space -= bytes;
2423 GEM_BUG_ON(ring->space < 0);
2424 return 0;
2427 /* Align the ring tail to a cacheline boundary */
2428 int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
2430 struct intel_ring *ring = req->ring;
2431 int num_dwords =
2432 (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
2433 int ret;
2435 if (num_dwords == 0)
2436 return 0;
2438 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
2439 ret = intel_ring_begin(req, num_dwords);
2440 if (ret)
2441 return ret;
2443 while (num_dwords--)
2444 intel_ring_emit(ring, MI_NOOP);
2446 intel_ring_advance(ring);
2448 return 0;
2451 void intel_engine_init_seqno(struct intel_engine_cs *engine, u32 seqno)
2453 struct drm_i915_private *dev_priv = engine->i915;
2455 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
2456 * so long as the semaphore value in the register/page is greater
2457 * than the sync value), so whenever we reset the seqno,
2458 * so long as we reset the tracking semaphore value to 0, it will
2459 * always be before the next request's seqno. If we don't reset
2460 * the semaphore value, then when the seqno moves backwards all
2461 * future waits will complete instantly (causing rendering corruption).
2463 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
2464 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
2465 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
2466 if (HAS_VEBOX(dev_priv))
2467 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
2469 if (dev_priv->semaphore_obj) {
2470 struct drm_i915_gem_object *obj = dev_priv->semaphore_obj;
2471 struct page *page = i915_gem_object_get_dirty_page(obj, 0);
2472 void *semaphores = kmap(page);
2473 memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
2474 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
2475 kunmap(page);
2477 memset(engine->semaphore.sync_seqno, 0,
2478 sizeof(engine->semaphore.sync_seqno));
2480 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
2481 if (engine->irq_seqno_barrier)
2482 engine->irq_seqno_barrier(engine);
2483 engine->last_submitted_seqno = seqno;
2485 engine->hangcheck.seqno = seqno;
2487 /* After manually advancing the seqno, fake the interrupt in case
2488 * there are any waiters for that seqno.
2490 rcu_read_lock();
2491 intel_engine_wakeup(engine);
2492 rcu_read_unlock();
2495 static void gen6_bsd_ring_write_tail(struct intel_engine_cs *engine,
2496 u32 value)
2498 struct drm_i915_private *dev_priv = engine->i915;
2500 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2502 /* Every tail move must follow the sequence below */
2504 /* Disable notification that the ring is IDLE. The GT
2505 * will then assume that it is busy and bring it out of rc6.
2507 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2508 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2510 /* Clear the context id. Here be magic! */
2511 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
2513 /* Wait for the ring not to be idle, i.e. for it to wake up. */
2514 if (intel_wait_for_register_fw(dev_priv,
2515 GEN6_BSD_SLEEP_PSMI_CONTROL,
2516 GEN6_BSD_SLEEP_INDICATOR,
2518 50))
2519 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
2521 /* Now that the ring is fully powered up, update the tail */
2522 I915_WRITE_FW(RING_TAIL(engine->mmio_base), value);
2523 POSTING_READ_FW(RING_TAIL(engine->mmio_base));
2525 /* Let the ring send IDLE messages to the GT again,
2526 * and so let it sleep to conserve power when idle.
2528 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2529 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2531 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
2534 static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
2535 u32 invalidate, u32 flush)
2537 struct intel_ring *ring = req->ring;
2538 uint32_t cmd;
2539 int ret;
2541 ret = intel_ring_begin(req, 4);
2542 if (ret)
2543 return ret;
2545 cmd = MI_FLUSH_DW;
2546 if (INTEL_GEN(req->i915) >= 8)
2547 cmd += 1;
2549 /* We always require a command barrier so that subsequent
2550 * commands, such as breadcrumb interrupts, are strictly ordered
2551 * wrt the contents of the write cache being flushed to memory
2552 * (and thus being coherent from the CPU).
2554 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2557 * Bspec vol 1c.5 - video engine command streamer:
2558 * "If ENABLED, all TLBs will be invalidated once the flush
2559 * operation is complete. This bit is only valid when the
2560 * Post-Sync Operation field is a value of 1h or 3h."
2562 if (invalidate & I915_GEM_GPU_DOMAINS)
2563 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2565 intel_ring_emit(ring, cmd);
2566 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
2567 if (INTEL_GEN(req->i915) >= 8) {
2568 intel_ring_emit(ring, 0); /* upper addr */
2569 intel_ring_emit(ring, 0); /* value */
2570 } else {
2571 intel_ring_emit(ring, 0);
2572 intel_ring_emit(ring, MI_NOOP);
2574 intel_ring_advance(ring);
2575 return 0;
2578 static int
2579 gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2580 u64 offset, u32 len,
2581 unsigned dispatch_flags)
2583 struct intel_ring *ring = req->ring;
2584 bool ppgtt = USES_PPGTT(req->i915) &&
2585 !(dispatch_flags & I915_DISPATCH_SECURE);
2586 int ret;
2588 ret = intel_ring_begin(req, 4);
2589 if (ret)
2590 return ret;
2592 /* FIXME(BDW): Address space and security selectors. */
2593 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
2594 (dispatch_flags & I915_DISPATCH_RS ?
2595 MI_BATCH_RESOURCE_STREAMER : 0));
2596 intel_ring_emit(ring, lower_32_bits(offset));
2597 intel_ring_emit(ring, upper_32_bits(offset));
2598 intel_ring_emit(ring, MI_NOOP);
2599 intel_ring_advance(ring);
2601 return 0;
2604 static int
2605 hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2606 u64 offset, u32 len,
2607 unsigned dispatch_flags)
2609 struct intel_ring *ring = req->ring;
2610 int ret;
2612 ret = intel_ring_begin(req, 2);
2613 if (ret)
2614 return ret;
2616 intel_ring_emit(ring,
2617 MI_BATCH_BUFFER_START |
2618 (dispatch_flags & I915_DISPATCH_SECURE ?
2619 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2620 (dispatch_flags & I915_DISPATCH_RS ?
2621 MI_BATCH_RESOURCE_STREAMER : 0));
2622 /* bit0-7 is the length on GEN6+ */
2623 intel_ring_emit(ring, offset);
2624 intel_ring_advance(ring);
2626 return 0;
2629 static int
2630 gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
2631 u64 offset, u32 len,
2632 unsigned dispatch_flags)
2634 struct intel_ring *ring = req->ring;
2635 int ret;
2637 ret = intel_ring_begin(req, 2);
2638 if (ret)
2639 return ret;
2641 intel_ring_emit(ring,
2642 MI_BATCH_BUFFER_START |
2643 (dispatch_flags & I915_DISPATCH_SECURE ?
2644 0 : MI_BATCH_NON_SECURE_I965));
2645 /* bit0-7 is the length on GEN6+ */
2646 intel_ring_emit(ring, offset);
2647 intel_ring_advance(ring);
2649 return 0;
2652 /* Blitter support (SandyBridge+) */
2654 static int gen6_ring_flush(struct drm_i915_gem_request *req,
2655 u32 invalidate, u32 flush)
2657 struct intel_ring *ring = req->ring;
2658 uint32_t cmd;
2659 int ret;
2661 ret = intel_ring_begin(req, 4);
2662 if (ret)
2663 return ret;
2665 cmd = MI_FLUSH_DW;
2666 if (INTEL_GEN(req->i915) >= 8)
2667 cmd += 1;
2669 /* We always require a command barrier so that subsequent
2670 * commands, such as breadcrumb interrupts, are strictly ordered
2671 * wrt the contents of the write cache being flushed to memory
2672 * (and thus being coherent from the CPU).
2674 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2677 * Bspec vol 1c.3 - blitter engine command streamer:
2678 * "If ENABLED, all TLBs will be invalidated once the flush
2679 * operation is complete. This bit is only valid when the
2680 * Post-Sync Operation field is a value of 1h or 3h."
2682 if (invalidate & I915_GEM_DOMAIN_RENDER)
2683 cmd |= MI_INVALIDATE_TLB;
2684 intel_ring_emit(ring, cmd);
2685 intel_ring_emit(ring,
2686 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
2687 if (INTEL_GEN(req->i915) >= 8) {
2688 intel_ring_emit(ring, 0); /* upper addr */
2689 intel_ring_emit(ring, 0); /* value */
2690 } else {
2691 intel_ring_emit(ring, 0);
2692 intel_ring_emit(ring, MI_NOOP);
2694 intel_ring_advance(ring);
2696 return 0;
2699 static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2700 struct intel_engine_cs *engine)
2702 struct drm_i915_gem_object *obj;
2703 int ret, i;
2705 if (!i915.semaphores)
2706 return;
2708 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore_obj) {
2709 obj = i915_gem_object_create(&dev_priv->drm, 4096);
2710 if (IS_ERR(obj)) {
2711 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2712 i915.semaphores = 0;
2713 } else {
2714 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2715 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2716 if (ret != 0) {
2717 i915_gem_object_put(obj);
2718 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2719 i915.semaphores = 0;
2720 } else {
2721 dev_priv->semaphore_obj = obj;
2726 if (!i915.semaphores)
2727 return;
2729 if (INTEL_GEN(dev_priv) >= 8) {
2730 u64 offset = i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj);
2732 engine->semaphore.sync_to = gen8_ring_sync;
2733 engine->semaphore.signal = gen8_xcs_signal;
2735 for (i = 0; i < I915_NUM_ENGINES; i++) {
2736 u64 ring_offset;
2738 if (i != engine->id)
2739 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2740 else
2741 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2743 engine->semaphore.signal_ggtt[i] = ring_offset;
2745 } else if (INTEL_GEN(dev_priv) >= 6) {
2746 engine->semaphore.sync_to = gen6_ring_sync;
2747 engine->semaphore.signal = gen6_signal;
2750 * The current semaphore is only applied on pre-gen8
2751 * platform. And there is no VCS2 ring on the pre-gen8
2752 * platform. So the semaphore between RCS and VCS2 is
2753 * initialized as INVALID. Gen8 will initialize the
2754 * sema between VCS2 and RCS later.
2756 for (i = 0; i < I915_NUM_ENGINES; i++) {
2757 static const struct {
2758 u32 wait_mbox;
2759 i915_reg_t mbox_reg;
2760 } sem_data[I915_NUM_ENGINES][I915_NUM_ENGINES] = {
2761 [RCS] = {
2762 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2763 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2764 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
2766 [VCS] = {
2767 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2768 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2769 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
2771 [BCS] = {
2772 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2773 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2774 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
2776 [VECS] = {
2777 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2778 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2779 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
2782 u32 wait_mbox;
2783 i915_reg_t mbox_reg;
2785 if (i == engine->id || i == VCS2) {
2786 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2787 mbox_reg = GEN6_NOSYNC;
2788 } else {
2789 wait_mbox = sem_data[engine->id][i].wait_mbox;
2790 mbox_reg = sem_data[engine->id][i].mbox_reg;
2793 engine->semaphore.mbox.wait[i] = wait_mbox;
2794 engine->semaphore.mbox.signal[i] = mbox_reg;
2799 static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2800 struct intel_engine_cs *engine)
2802 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
2804 if (INTEL_GEN(dev_priv) >= 8) {
2805 engine->irq_enable = gen8_irq_enable;
2806 engine->irq_disable = gen8_irq_disable;
2807 engine->irq_seqno_barrier = gen6_seqno_barrier;
2808 } else if (INTEL_GEN(dev_priv) >= 6) {
2809 engine->irq_enable = gen6_irq_enable;
2810 engine->irq_disable = gen6_irq_disable;
2811 engine->irq_seqno_barrier = gen6_seqno_barrier;
2812 } else if (INTEL_GEN(dev_priv) >= 5) {
2813 engine->irq_enable = gen5_irq_enable;
2814 engine->irq_disable = gen5_irq_disable;
2815 engine->irq_seqno_barrier = gen5_seqno_barrier;
2816 } else if (INTEL_GEN(dev_priv) >= 3) {
2817 engine->irq_enable = i9xx_irq_enable;
2818 engine->irq_disable = i9xx_irq_disable;
2819 } else {
2820 engine->irq_enable = i8xx_irq_enable;
2821 engine->irq_disable = i8xx_irq_disable;
2825 static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2826 struct intel_engine_cs *engine)
2828 engine->init_hw = init_ring_common;
2829 engine->write_tail = ring_write_tail;
2831 engine->add_request = i9xx_add_request;
2832 if (INTEL_GEN(dev_priv) >= 6)
2833 engine->add_request = gen6_add_request;
2835 if (INTEL_GEN(dev_priv) >= 8)
2836 engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
2837 else if (INTEL_GEN(dev_priv) >= 6)
2838 engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2839 else if (INTEL_GEN(dev_priv) >= 4)
2840 engine->dispatch_execbuffer = i965_dispatch_execbuffer;
2841 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
2842 engine->dispatch_execbuffer = i830_dispatch_execbuffer;
2843 else
2844 engine->dispatch_execbuffer = i915_dispatch_execbuffer;
2846 intel_ring_init_irq(dev_priv, engine);
2847 intel_ring_init_semaphores(dev_priv, engine);
2850 int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
2852 struct drm_i915_private *dev_priv = engine->i915;
2853 int ret;
2855 intel_ring_default_vfuncs(dev_priv, engine);
2857 if (HAS_L3_DPF(dev_priv))
2858 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2860 if (INTEL_GEN(dev_priv) >= 8) {
2861 engine->init_context = intel_rcs_ctx_init;
2862 engine->add_request = gen8_render_add_request;
2863 engine->flush = gen8_render_ring_flush;
2864 if (i915.semaphores)
2865 engine->semaphore.signal = gen8_rcs_signal;
2866 } else if (INTEL_GEN(dev_priv) >= 6) {
2867 engine->init_context = intel_rcs_ctx_init;
2868 engine->flush = gen7_render_ring_flush;
2869 if (IS_GEN6(dev_priv))
2870 engine->flush = gen6_render_ring_flush;
2871 } else if (IS_GEN5(dev_priv)) {
2872 engine->flush = gen4_render_ring_flush;
2873 } else {
2874 if (INTEL_GEN(dev_priv) < 4)
2875 engine->flush = gen2_render_ring_flush;
2876 else
2877 engine->flush = gen4_render_ring_flush;
2878 engine->irq_enable_mask = I915_USER_INTERRUPT;
2881 if (IS_HASWELL(dev_priv))
2882 engine->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
2884 engine->init_hw = init_render_ring;
2885 engine->cleanup = render_ring_cleanup;
2887 ret = intel_init_ring_buffer(engine);
2888 if (ret)
2889 return ret;
2891 if (INTEL_GEN(dev_priv) >= 6) {
2892 ret = intel_init_pipe_control(engine, 4096);
2893 if (ret)
2894 return ret;
2895 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
2896 ret = intel_init_pipe_control(engine, I830_WA_SIZE);
2897 if (ret)
2898 return ret;
2901 return 0;
2904 int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
2906 struct drm_i915_private *dev_priv = engine->i915;
2908 intel_ring_default_vfuncs(dev_priv, engine);
2910 if (INTEL_GEN(dev_priv) >= 6) {
2911 /* gen6 bsd needs a special wa for tail updates */
2912 if (IS_GEN6(dev_priv))
2913 engine->write_tail = gen6_bsd_ring_write_tail;
2914 engine->flush = gen6_bsd_ring_flush;
2915 if (INTEL_GEN(dev_priv) < 8)
2916 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2917 } else {
2918 engine->mmio_base = BSD_RING_BASE;
2919 engine->flush = bsd_ring_flush;
2920 if (IS_GEN5(dev_priv))
2921 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
2922 else
2923 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
2926 return intel_init_ring_buffer(engine);
2930 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
2932 int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
2934 struct drm_i915_private *dev_priv = engine->i915;
2936 intel_ring_default_vfuncs(dev_priv, engine);
2938 engine->flush = gen6_bsd_ring_flush;
2940 return intel_init_ring_buffer(engine);
2943 int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
2945 struct drm_i915_private *dev_priv = engine->i915;
2947 intel_ring_default_vfuncs(dev_priv, engine);
2949 engine->flush = gen6_ring_flush;
2950 if (INTEL_GEN(dev_priv) < 8)
2951 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2953 return intel_init_ring_buffer(engine);
2956 int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
2958 struct drm_i915_private *dev_priv = engine->i915;
2960 intel_ring_default_vfuncs(dev_priv, engine);
2962 engine->flush = gen6_ring_flush;
2964 if (INTEL_GEN(dev_priv) < 8) {
2965 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2966 engine->irq_enable = hsw_vebox_irq_enable;
2967 engine->irq_disable = hsw_vebox_irq_disable;
2970 return intel_init_ring_buffer(engine);
2974 intel_engine_flush_all_caches(struct drm_i915_gem_request *req)
2976 struct intel_engine_cs *engine = req->engine;
2977 int ret;
2979 if (!engine->gpu_caches_dirty)
2980 return 0;
2982 ret = engine->flush(req, 0, I915_GEM_GPU_DOMAINS);
2983 if (ret)
2984 return ret;
2986 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
2988 engine->gpu_caches_dirty = false;
2989 return 0;
2993 intel_engine_invalidate_all_caches(struct drm_i915_gem_request *req)
2995 struct intel_engine_cs *engine = req->engine;
2996 uint32_t flush_domains;
2997 int ret;
2999 flush_domains = 0;
3000 if (engine->gpu_caches_dirty)
3001 flush_domains = I915_GEM_GPU_DOMAINS;
3003 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
3004 if (ret)
3005 return ret;
3007 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
3009 engine->gpu_caches_dirty = false;
3010 return 0;
3013 void intel_engine_stop(struct intel_engine_cs *engine)
3015 int ret;
3017 if (!intel_engine_initialized(engine))
3018 return;
3020 ret = intel_engine_idle(engine);
3021 if (ret)
3022 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
3023 engine->name, ret);
3025 stop_ring(engine);