blob: 5465e708545ff0f05663dd3f07c1e981458b05e2 [file] [log] [blame]
Eric Anholt62fdfea2010-05-21 13:26:39 -07001/*
2 * Copyright © 2008-2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 * Zou Nan hai <nanhai.zou@intel.com>
26 * Xiang Hai hao<haihao.xiang@intel.com>
27 *
28 */
29
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +080030#include <linux/log2.h>
Chris Wilson7c2fa7f2017-11-10 14:26:34 +000031
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/i915_drm.h>
Chris Wilson7c2fa7f2017-11-10 14:26:34 +000034
35#include "i915_drv.h"
36#include "i915_gem_render_state.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070037#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010038#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070039
Chris Wilsona0442462016-04-29 09:07:05 +010040/* Rough estimate of the typical request size, performing a flush,
41 * set-context and then emitting the batch.
42 */
43#define LEGACY_REQUEST_SIZE 200
44
Chris Wilson605d5b32017-05-04 14:08:44 +010045static unsigned int __intel_ring_space(unsigned int head,
46 unsigned int tail,
47 unsigned int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010048{
Chris Wilson605d5b32017-05-04 14:08:44 +010049 /*
50 * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
51 * same cacheline, the Head Pointer must not be greater than the Tail
52 * Pointer."
53 */
54 GEM_BUG_ON(!is_power_of_2(size));
55 return (head - tail - CACHELINE_BYTES) & (size - 1);
Chris Wilson1cf0ba12014-05-05 09:07:33 +010056}
57
Chris Wilson95aebcb2017-05-04 14:08:45 +010058unsigned int intel_ring_update_space(struct intel_ring *ring)
Dave Gordonebd0fd42014-11-27 11:22:49 +000059{
Chris Wilson95aebcb2017-05-04 14:08:45 +010060 unsigned int space;
61
62 space = __intel_ring_space(ring->head, ring->emit, ring->size);
63
64 ring->space = space;
65 return space;
Dave Gordonebd0fd42014-11-27 11:22:49 +000066}
67
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000068static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010069gen2_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010070{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000071 u32 cmd, *cs;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010072
73 cmd = MI_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010074
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010075 if (mode & EMIT_INVALIDATE)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010076 cmd |= MI_READ_FLUSH;
77
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000078 cs = intel_ring_begin(req, 2);
79 if (IS_ERR(cs))
80 return PTR_ERR(cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010081
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000082 *cs++ = cmd;
83 *cs++ = MI_NOOP;
84 intel_ring_advance(req, cs);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010085
86 return 0;
87}
88
89static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +010090gen4_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Eric Anholt62fdfea2010-05-21 13:26:39 -070091{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +000092 u32 cmd, *cs;
Chris Wilson6f392d52010-08-07 11:01:22 +010093
Chris Wilson36d527d2011-03-19 22:26:49 +000094 /*
95 * read/write caches:
96 *
97 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
98 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
99 * also flushed at 2d versus 3d pipeline switches.
100 *
101 * read-only caches:
102 *
103 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
104 * MI_READ_FLUSH is set, and is always flushed on 965.
105 *
106 * I915_GEM_DOMAIN_COMMAND may not exist?
107 *
108 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
109 * invalidated when MI_EXE_FLUSH is set.
110 *
111 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
112 * invalidated with every MI_FLUSH.
113 *
114 * TLBs:
115 *
116 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
117 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
118 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
119 * are flushed at any MI_FLUSH.
120 */
121
Chris Wilsonb5321f32016-08-02 22:50:18 +0100122 cmd = MI_FLUSH;
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100123 if (mode & EMIT_INVALIDATE) {
Chris Wilson36d527d2011-03-19 22:26:49 +0000124 cmd |= MI_EXE_FLUSH;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100125 if (IS_G4X(req->i915) || IS_GEN5(req->i915))
126 cmd |= MI_INVALIDATE_ISP;
127 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000128
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000129 cs = intel_ring_begin(req, 2);
130 if (IS_ERR(cs))
131 return PTR_ERR(cs);
Chris Wilson36d527d2011-03-19 22:26:49 +0000132
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000133 *cs++ = cmd;
134 *cs++ = MI_NOOP;
135 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000136
137 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800138}
139
Jesse Barnes8d315282011-10-16 10:23:31 +0200140/**
141 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
142 * implementing two workarounds on gen6. From section 1.4.7.1
143 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
144 *
145 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
146 * produced by non-pipelined state commands), software needs to first
147 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
148 * 0.
149 *
150 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
151 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
152 *
153 * And the workaround for these two requires this workaround first:
154 *
155 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
156 * BEFORE the pipe-control with a post-sync op and no write-cache
157 * flushes.
158 *
159 * And this last workaround is tricky because of the requirements on
160 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
161 * volume 2 part 1:
162 *
163 * "1 of the following must also be set:
164 * - Render Target Cache Flush Enable ([12] of DW1)
165 * - Depth Cache Flush Enable ([0] of DW1)
166 * - Stall at Pixel Scoreboard ([1] of DW1)
167 * - Depth Stall ([13] of DW1)
168 * - Post-Sync Operation ([13] of DW1)
169 * - Notify Enable ([8] of DW1)"
170 *
171 * The cache flushes require the workaround flush that triggered this
172 * one, so we can't use it. Depth stall would trigger the same.
173 * Post-sync nonzero is what triggered this second workaround, so we
174 * can't use that one either. Notify enable is IRQs, which aren't
175 * really our business. That leaves only stall at scoreboard.
176 */
177static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100178intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200179{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100180 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100181 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000182 u32 *cs;
Jesse Barnes8d315282011-10-16 10:23:31 +0200183
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000184 cs = intel_ring_begin(req, 6);
185 if (IS_ERR(cs))
186 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200187
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000188 *cs++ = GFX_OP_PIPE_CONTROL(5);
189 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
190 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
191 *cs++ = 0; /* low dword */
192 *cs++ = 0; /* high dword */
193 *cs++ = MI_NOOP;
194 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200195
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000196 cs = intel_ring_begin(req, 6);
197 if (IS_ERR(cs))
198 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200199
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000200 *cs++ = GFX_OP_PIPE_CONTROL(5);
201 *cs++ = PIPE_CONTROL_QW_WRITE;
202 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
203 *cs++ = 0;
204 *cs++ = 0;
205 *cs++ = MI_NOOP;
206 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200207
208 return 0;
209}
210
211static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100212gen6_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Jesse Barnes8d315282011-10-16 10:23:31 +0200213{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100214 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100215 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000216 u32 *cs, flags = 0;
Jesse Barnes8d315282011-10-16 10:23:31 +0200217 int ret;
218
Paulo Zanonib3111502012-08-17 18:35:42 -0300219 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100220 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300221 if (ret)
222 return ret;
223
Jesse Barnes8d315282011-10-16 10:23:31 +0200224 /* Just flush everything. Experiments have shown that reducing the
225 * number of bits based on the write domains has little performance
226 * impact.
227 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100228 if (mode & EMIT_FLUSH) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100229 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
230 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
231 /*
232 * Ensure that any following seqno writes only happen
233 * when the render cache is indeed flushed.
234 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200235 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100236 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100237 if (mode & EMIT_INVALIDATE) {
Chris Wilson7d54a902012-08-10 10:18:10 +0100238 flags |= PIPE_CONTROL_TLB_INVALIDATE;
239 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
240 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
241 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
242 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
243 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
244 /*
245 * TLB invalidate requires a post-sync write.
246 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700247 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100248 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200249
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000250 cs = intel_ring_begin(req, 4);
251 if (IS_ERR(cs))
252 return PTR_ERR(cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200253
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000254 *cs++ = GFX_OP_PIPE_CONTROL(4);
255 *cs++ = flags;
256 *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
257 *cs++ = 0;
258 intel_ring_advance(req, cs);
Jesse Barnes8d315282011-10-16 10:23:31 +0200259
260 return 0;
261}
262
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100263static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100264gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300265{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000266 u32 *cs;
Paulo Zanonif3987632012-08-17 18:35:43 -0300267
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000268 cs = intel_ring_begin(req, 4);
269 if (IS_ERR(cs))
270 return PTR_ERR(cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300271
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000272 *cs++ = GFX_OP_PIPE_CONTROL(4);
273 *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
274 *cs++ = 0;
275 *cs++ = 0;
276 intel_ring_advance(req, cs);
Paulo Zanonif3987632012-08-17 18:35:43 -0300277
278 return 0;
279}
280
281static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100282gen7_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300283{
Chris Wilsonb5321f32016-08-02 22:50:18 +0100284 u32 scratch_addr =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100285 i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000286 u32 *cs, flags = 0;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300287
Paulo Zanonif3987632012-08-17 18:35:43 -0300288 /*
289 * Ensure that any following seqno writes only happen when the render
290 * cache is indeed flushed.
291 *
292 * Workaround: 4th PIPE_CONTROL command (except the ones with only
293 * read-cache invalidate bits set) must have the CS_STALL bit set. We
294 * don't try to be clever and just set it unconditionally.
295 */
296 flags |= PIPE_CONTROL_CS_STALL;
297
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300298 /* Just flush everything. Experiments have shown that reducing the
299 * number of bits based on the write domains has little performance
300 * impact.
301 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100302 if (mode & EMIT_FLUSH) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300303 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
304 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800305 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100306 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300307 }
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100308 if (mode & EMIT_INVALIDATE) {
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300309 flags |= PIPE_CONTROL_TLB_INVALIDATE;
310 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
311 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
312 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
313 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
314 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000315 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300316 /*
317 * TLB invalidate requires a post-sync write.
318 */
319 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200320 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300321
Chris Wilsonadd284a2014-12-16 08:44:32 +0000322 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
323
Paulo Zanonif3987632012-08-17 18:35:43 -0300324 /* Workaround: we must issue a pipe_control with CS-stall bit
325 * set before a pipe_control command that has the state cache
326 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100327 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300328 }
329
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000330 cs = intel_ring_begin(req, 4);
331 if (IS_ERR(cs))
332 return PTR_ERR(cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300333
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000334 *cs++ = GFX_OP_PIPE_CONTROL(4);
335 *cs++ = flags;
336 *cs++ = scratch_addr;
337 *cs++ = 0;
338 intel_ring_advance(req, cs);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300339
340 return 0;
341}
342
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000343static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200344{
Chris Wilsonc0336662016-05-06 15:40:21 +0100345 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200346 u32 addr;
347
348 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100349 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200350 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
351 I915_WRITE(HWS_PGA, addr);
352}
353
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000354static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000355{
Chris Wilsonc0336662016-05-06 15:40:21 +0100356 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200357 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000358
359 /* The ring status page addresses are no longer next to the rest of
360 * the ring registers as of gen7.
361 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100362 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000363 switch (engine->id) {
Michel Thierrya2d3d262017-08-30 11:01:15 -0700364 /*
365 * No more rings exist on Gen7. Default case is only to shut up
366 * gcc switch check warning.
367 */
368 default:
369 GEM_BUG_ON(engine->id);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000370 case RCS:
371 mmio = RENDER_HWS_PGA_GEN7;
372 break;
373 case BCS:
374 mmio = BLT_HWS_PGA_GEN7;
375 break;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000376 case VCS:
377 mmio = BSD_HWS_PGA_GEN7;
378 break;
379 case VECS:
380 mmio = VEBOX_HWS_PGA_GEN7;
381 break;
382 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100383 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000384 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000385 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000386 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000387 }
388
Ville Syrjäläc54980892017-08-18 21:37:01 +0300389 if (INTEL_GEN(dev_priv) >= 6)
390 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
391
Chris Wilson57e88532016-08-15 10:48:57 +0100392 I915_WRITE(mmio, engine->status_page.ggtt_offset);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000393 POSTING_READ(mmio);
394
Chris Wilson79e67702017-11-20 20:55:01 +0000395 /* Flush the TLB for this page */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100396 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000397 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000398
399 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000400 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000401
402 I915_WRITE(reg,
403 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
404 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100405 if (intel_wait_for_register(dev_priv,
406 reg, INSTPM_SYNC_FLUSH, 0,
407 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000408 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000409 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000410 }
411}
412
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000413static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100414{
Chris Wilsonc0336662016-05-06 15:40:21 +0100415 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100416
Chris Wilson21a2c582016-08-15 10:49:11 +0100417 if (INTEL_GEN(dev_priv) > 2) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000418 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100419 if (intel_wait_for_register(dev_priv,
420 RING_MI_MODE(engine->mmio_base),
421 MODE_IDLE,
422 MODE_IDLE,
423 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000424 DRM_ERROR("%s : timed out trying to stop ring\n",
425 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100426 /* Sometimes we observe that the idle flag is not
427 * set even though the ring is empty. So double
428 * check before giving up.
429 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000430 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100431 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100432 }
433 }
434
Chris Wilson11caf552017-10-27 10:43:11 +0100435 I915_WRITE_HEAD(engine, I915_READ_TAIL(engine));
436
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000437 I915_WRITE_HEAD(engine, 0);
Chris Wilsonc5efa1a2016-08-02 22:50:29 +0100438 I915_WRITE_TAIL(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100439
Chris Wilson11caf552017-10-27 10:43:11 +0100440 /* The ring must be empty before it is disabled */
441 I915_WRITE_CTL(engine, 0);
442
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000443 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100444}
445
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000446static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800447{
Chris Wilsonc0336662016-05-06 15:40:21 +0100448 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson7e37f882016-08-02 22:50:21 +0100449 struct intel_ring *ring = engine->buffer;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200450 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800451
Mika Kuoppala59bad942015-01-16 11:34:40 +0200452 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200453
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000454 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100455 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000456 DRM_DEBUG_KMS("%s head not reset to zero "
457 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000458 engine->name,
459 I915_READ_CTL(engine),
460 I915_READ_HEAD(engine),
461 I915_READ_TAIL(engine),
462 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800463
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000464 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000465 DRM_ERROR("failed to set %s head to zero "
466 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000467 engine->name,
468 I915_READ_CTL(engine),
469 I915_READ_HEAD(engine),
470 I915_READ_TAIL(engine),
471 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100472 ret = -EIO;
473 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000474 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700475 }
476
Carlos Santa31776592016-08-17 12:30:56 -0700477 if (HWS_NEEDS_PHYSICAL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000478 ring_setup_phys_status_page(engine);
Carlos Santa31776592016-08-17 12:30:56 -0700479 else
480 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100481
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100482 intel_engine_reset_breadcrumbs(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +0100483
Jiri Kosinaece4a172014-08-07 16:29:53 +0200484 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000485 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200486
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200487 /* Initialize the ring. This must happen _after_ we've cleared the ring
488 * registers with the above sequence (the readback of the HEAD registers
489 * also enforces ordering), otherwise the hw might lose the new ring
490 * register values. */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100491 I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
Chris Wilson95468892014-08-07 15:39:54 +0100492
493 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000494 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100495 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000496 engine->name, I915_READ_HEAD(engine));
Chris Wilson821ed7d2016-09-09 14:11:53 +0100497
498 intel_ring_update_space(ring);
499 I915_WRITE_HEAD(engine, ring->head);
500 I915_WRITE_TAIL(engine, ring->tail);
501 (void)I915_READ_TAIL(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100502
Chris Wilson62ae14b2016-10-04 21:11:25 +0100503 I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800504
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800505 /* If the head is still not zero, the ring is dead */
Chris Wilsonf42bb652017-04-11 11:13:40 +0100506 if (intel_wait_for_register(dev_priv, RING_CTL(engine->mmio_base),
507 RING_VALID, RING_VALID,
508 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000509 DRM_ERROR("%s initialization failed "
Chris Wilson821ed7d2016-09-09 14:11:53 +0100510 "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000511 engine->name,
512 I915_READ_CTL(engine),
513 I915_READ_CTL(engine) & RING_VALID,
Chris Wilson821ed7d2016-09-09 14:11:53 +0100514 I915_READ_HEAD(engine), ring->head,
515 I915_READ_TAIL(engine), ring->tail,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000516 I915_READ_START(engine),
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100517 i915_ggtt_offset(ring->vma));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200518 ret = -EIO;
519 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800520 }
521
Tomas Elffc0768c2016-03-21 16:26:59 +0000522 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100523
Chris Wilson7836cd02017-10-13 14:12:17 +0100524 if (INTEL_GEN(dev_priv) > 2)
525 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
526
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200527out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200528 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200529
530 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700531}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800532
Chris Wilson821ed7d2016-09-09 14:11:53 +0100533static void reset_ring_common(struct intel_engine_cs *engine,
534 struct drm_i915_gem_request *request)
535{
Chris Wilson67e64562017-10-09 12:03:01 +0100536 /*
537 * RC6 must be prevented until the reset is complete and the engine
538 * reinitialised. If it occurs in the middle of this sequence, the
539 * state written to/loaded from the power context is ill-defined (e.g.
540 * the PP_BASE_DIR may be lost).
541 */
542 assert_forcewakes_active(engine->i915, FORCEWAKE_ALL);
543
544 /*
545 * Try to restore the logical GPU state to match the continuation
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000546 * of the request queue. If we skip the context/PD restore, then
547 * the next request may try to execute assuming that its context
548 * is valid and loaded on the GPU and so may try to access invalid
549 * memory, prompting repeated GPU hangs.
550 *
551 * If the request was guilty, we still restore the logical state
552 * in case the next request requires it (e.g. the aliasing ppgtt),
553 * but skip over the hung batch.
554 *
555 * If the request was innocent, we try to replay the request with
556 * the restored context.
557 */
558 if (request) {
559 struct drm_i915_private *dev_priv = request->i915;
560 struct intel_context *ce = &request->ctx->engine[engine->id];
561 struct i915_hw_ppgtt *ppgtt;
Chris Wilson821ed7d2016-09-09 14:11:53 +0100562
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000563 if (ce->state) {
564 I915_WRITE(CCID,
565 i915_ggtt_offset(ce->state) |
566 BIT(8) /* must be set! */ |
567 CCID_EXTENDED_STATE_SAVE |
568 CCID_EXTENDED_STATE_RESTORE |
569 CCID_EN);
570 }
571
572 ppgtt = request->ctx->ppgtt ?: engine->i915->mm.aliasing_ppgtt;
573 if (ppgtt) {
574 u32 pd_offset = ppgtt->pd.base.ggtt_offset << 10;
575
576 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
577 I915_WRITE(RING_PP_DIR_BASE(engine), pd_offset);
578
579 /* Wait for the PD reload to complete */
580 if (intel_wait_for_register(dev_priv,
581 RING_PP_DIR_BASE(engine),
582 BIT(0), 0,
583 10))
584 DRM_ERROR("Wait for reload of ppgtt page-directory timed out\n");
585
586 ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
587 }
588
589 /* If the rq hung, jump to its breadcrumb and skip the batch */
Chris Wilsonfe085f12017-03-21 10:25:52 +0000590 if (request->fence.error == -EIO)
591 request->ring->head = request->postfix;
Chris Wilsonc0dcb202017-02-07 15:24:37 +0000592 } else {
593 engine->legacy_active_context = NULL;
594 }
Chris Wilson821ed7d2016-09-09 14:11:53 +0100595}
596
John Harrison87531812015-05-29 17:43:44 +0100597static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100598{
599 int ret;
600
John Harrisone2be4fa2015-05-29 17:43:54 +0100601 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100602 if (ret != 0)
603 return ret;
604
Chris Wilson4e50f082016-10-28 13:58:31 +0100605 ret = i915_gem_render_state_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100606 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000607 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100608
Chris Wilsone26e1b92016-01-29 16:49:05 +0000609 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100610}
611
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000612static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800613{
Chris Wilsonc0336662016-05-06 15:40:21 +0100614 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000615 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +0200616 if (ret)
617 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800618
Akash Goel61a563a2014-03-25 18:01:50 +0530619 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100620 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200621 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000622
623 /* We need to disable the AsyncFlip performance optimisations in order
624 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
625 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +0100626 *
Ville Syrjälä2441f872015-06-02 15:37:37 +0300627 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000628 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100629 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000630 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
631
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000632 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +0530633 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +0100634 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000635 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +0000636 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +0000637
Akash Goel01fa0302014-03-24 23:00:04 +0530638 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +0100639 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000640 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +0530641 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +0000642 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +0100643
Chris Wilsonc0336662016-05-06 15:40:21 +0100644 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700645 /* From the Sandybridge PRM, volume 1 part 3, page 24:
646 * "If this bit is set, STCunit will have LRA as replacement
647 * policy. [...] This bit must be reset. LRA replacement
648 * policy is not supported."
649 */
650 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200651 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -0800652 }
653
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100654 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +0200655 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +0000656
Ville Syrjälä035ea402016-07-12 19:24:47 +0300657 if (INTEL_INFO(dev_priv)->gen >= 6)
658 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -0700659
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000660 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800661}
662
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000663static u32 *gen6_signal(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000664{
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100665 struct drm_i915_private *dev_priv = req->i915;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100666 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +0530667 enum intel_engine_id id;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100668 int num_rings = 0;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700669
Akash Goel3b3f1652016-10-13 22:44:48 +0530670 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100671 i915_reg_t mbox_reg;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200672
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100673 if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
674 continue;
675
676 mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200677 if (i915_mmio_reg_valid(mbox_reg)) {
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000678 *cs++ = MI_LOAD_REGISTER_IMM(1);
679 *cs++ = i915_mmio_reg_offset(mbox_reg);
680 *cs++ = req->global_seqno;
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100681 num_rings++;
Ben Widawsky78325f22014-04-29 14:52:29 -0700682 }
683 }
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100684 if (num_rings & 1)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000685 *cs++ = MI_NOOP;
Ben Widawsky024a43e2014-04-29 14:52:30 -0700686
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000687 return cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000688}
689
Chris Wilson27a5f612017-09-15 18:31:00 +0100690static void cancel_requests(struct intel_engine_cs *engine)
691{
692 struct drm_i915_gem_request *request;
693 unsigned long flags;
694
695 spin_lock_irqsave(&engine->timeline->lock, flags);
696
697 /* Mark all submitted requests as skipped. */
698 list_for_each_entry(request, &engine->timeline->requests, link) {
699 GEM_BUG_ON(!request->global_seqno);
700 if (!i915_gem_request_completed(request))
701 dma_fence_set_error(&request->fence, -EIO);
702 }
703 /* Remaining _unready_ requests will be nop'ed when submitted */
704
705 spin_unlock_irqrestore(&engine->timeline->lock, flags);
706}
707
Chris Wilsonb0411e72016-08-02 22:50:34 +0100708static void i9xx_submit_request(struct drm_i915_gem_request *request)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000709{
Chris Wilsonb0411e72016-08-02 22:50:34 +0100710 struct drm_i915_private *dev_priv = request->i915;
711
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000712 i915_gem_request_submit(request);
713
Chris Wilsone6ba9992017-04-25 14:00:49 +0100714 I915_WRITE_TAIL(request->engine,
715 intel_ring_set_tail(request->ring, request->tail));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100716}
717
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000718static void i9xx_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100719{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000720 *cs++ = MI_STORE_DWORD_INDEX;
721 *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
722 *cs++ = req->global_seqno;
723 *cs++ = MI_USER_INTERRUPT;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000724
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000725 req->tail = intel_ring_offset(req, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +0100726 assert_ring_tail_valid(req->ring, req->tail);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000727}
728
Chris Wilson98f29e82016-10-28 13:58:51 +0100729static const int i9xx_emit_breadcrumb_sz = 4;
730
Chris Wilsonb0411e72016-08-02 22:50:34 +0100731/**
Chris Wilson9b81d552016-10-28 13:58:50 +0100732 * gen6_sema_emit_breadcrumb - Update the semaphore mailbox registers
Chris Wilsonb0411e72016-08-02 22:50:34 +0100733 *
734 * @request - request to write to the ring
735 *
736 * Update the mailbox registers in the *other* rings with the current seqno.
737 * This acts like a signal in the canonical semaphore.
738 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000739static void gen6_sema_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
Chris Wilsonb0411e72016-08-02 22:50:34 +0100740{
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100741 return i9xx_emit_breadcrumb(req,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000742 req->engine->semaphore.signal(req, cs));
Chris Wilsonb0411e72016-08-02 22:50:34 +0100743}
744
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700745static int
Chris Wilsonad7bdb22016-08-02 22:50:40 +0100746gen6_ring_sync_to(struct drm_i915_gem_request *req,
747 struct drm_i915_gem_request *signal)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000748{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700749 u32 dw1 = MI_SEMAPHORE_MBOX |
750 MI_SEMAPHORE_COMPARE |
751 MI_SEMAPHORE_REGISTER;
Tvrtko Ursulin318f89c2016-08-16 17:04:21 +0100752 u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000753 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000754
Chris Wilsonddf07be2016-08-02 22:50:39 +0100755 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
756
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000757 cs = intel_ring_begin(req, 4);
758 if (IS_ERR(cs))
759 return PTR_ERR(cs);
Chris Wilsonddf07be2016-08-02 22:50:39 +0100760
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000761 *cs++ = dw1 | wait_mbox;
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700762 /* Throughout all of the GEM code, seqno passed implies our current
763 * seqno is >= the last seqno executed. However for hardware the
764 * comparison is strictly greater than.
765 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000766 *cs++ = signal->global_seqno - 1;
767 *cs++ = 0;
768 *cs++ = MI_NOOP;
769 intel_ring_advance(req, cs);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000770
771 return 0;
772}
773
Chris Wilsonf8973c22016-07-01 17:23:21 +0100774static void
Dave Gordon38a0f2d2016-07-20 18:16:06 +0100775gen5_seqno_barrier(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000776{
Chris Wilsonf8973c22016-07-01 17:23:21 +0100777 /* MI_STORE are internally buffered by the GPU and not flushed
778 * either by MI_FLUSH or SyncFlush or any other combination of
779 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000780 *
Chris Wilsonf8973c22016-07-01 17:23:21 +0100781 * "Only the submission of the store operation is guaranteed.
782 * The write result will be complete (coherent) some time later
783 * (this is practically a finite period but there is no guaranteed
784 * latency)."
785 *
786 * Empirically, we observe that we need a delay of at least 75us to
787 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +0000788 */
Chris Wilsonf8973c22016-07-01 17:23:21 +0100789 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000790}
791
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100792static void
793gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100794{
Chris Wilsonc0336662016-05-06 15:40:21 +0100795 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100796
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100797 /* Workaround to force correct ordering between irq and seqno writes on
798 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +0100799 * ACTHD) before reading the status page.
800 *
801 * Note that this effectively stalls the read by the time it takes to
802 * do a memory transaction, which more or less ensures that the write
803 * from the GPU has sufficient time to invalidate the CPU cacheline.
804 * Alternatively we could delay the interrupt from the CS ring to give
805 * the write time to land, but that would incur a delay after every
806 * batch i.e. much more frequent than a delay when waiting for the
807 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100808 *
809 * Also note that to prevent whole machine hangs on gen7, we have to
810 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +0100811 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100812 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +0100813 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +0100814 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100815}
816
Chris Wilson31bb59c2016-07-01 17:23:27 +0100817static void
818gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200819{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100820 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +0200821}
822
823static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100824gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +0200825{
Chris Wilson31bb59c2016-07-01 17:23:27 +0100826 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700827}
828
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800829static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100830i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700831{
Chris Wilsonc0336662016-05-06 15:40:21 +0100832 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700833
Chris Wilson31bb59c2016-07-01 17:23:27 +0100834 dev_priv->irq_mask &= ~engine->irq_enable_mask;
835 I915_WRITE(IMR, dev_priv->irq_mask);
836 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +0100837}
838
839static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100840i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +0100841{
Chris Wilsonc0336662016-05-06 15:40:21 +0100842 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100843
Chris Wilson31bb59c2016-07-01 17:23:27 +0100844 dev_priv->irq_mask |= engine->irq_enable_mask;
845 I915_WRITE(IMR, dev_priv->irq_mask);
846}
847
848static void
849i8xx_irq_enable(struct intel_engine_cs *engine)
850{
851 struct drm_i915_private *dev_priv = engine->i915;
852
853 dev_priv->irq_mask &= ~engine->irq_enable_mask;
854 I915_WRITE16(IMR, dev_priv->irq_mask);
855 POSTING_READ16(RING_IMR(engine->mmio_base));
856}
857
858static void
859i8xx_irq_disable(struct intel_engine_cs *engine)
860{
861 struct drm_i915_private *dev_priv = engine->i915;
862
863 dev_priv->irq_mask |= engine->irq_enable_mask;
864 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100865}
866
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000867static int
Chris Wilson7c9cf4e2016-08-02 22:50:25 +0100868bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800869{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000870 u32 *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000871
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000872 cs = intel_ring_begin(req, 2);
873 if (IS_ERR(cs))
874 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000875
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000876 *cs++ = MI_FLUSH;
877 *cs++ = MI_NOOP;
878 intel_ring_advance(req, cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000879 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800880}
881
Chris Wilson0f468322011-01-04 17:35:21 +0000882static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100883gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +0000884{
Chris Wilsonc0336662016-05-06 15:40:21 +0100885 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +0000886
Chris Wilson61ff75a2016-07-01 17:23:28 +0100887 I915_WRITE_IMR(engine,
888 ~(engine->irq_enable_mask |
889 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +0100890 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -0700891}
892
893static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100894gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -0700895{
Chris Wilsonc0336662016-05-06 15:40:21 +0100896 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -0700897
Chris Wilson61ff75a2016-07-01 17:23:28 +0100898 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100899 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -0700900}
901
902static void
Chris Wilson31bb59c2016-07-01 17:23:27 +0100903hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -0700904{
Chris Wilsonc0336662016-05-06 15:40:21 +0100905 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -0700906
Chris Wilson31bb59c2016-07-01 17:23:27 +0100907 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
Akash Goelf4e9af42016-10-12 21:54:30 +0530908 gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100909}
910
911static void
912hsw_vebox_irq_disable(struct intel_engine_cs *engine)
913{
914 struct drm_i915_private *dev_priv = engine->i915;
915
916 I915_WRITE_IMR(engine, ~0);
Akash Goelf4e9af42016-10-12 21:54:30 +0530917 gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100918}
919
Zou Nan haid1b851f2010-05-21 09:08:57 +0800920static int
Chris Wilson803688b2016-08-02 22:50:27 +0100921i965_emit_bb_start(struct drm_i915_gem_request *req,
922 u64 offset, u32 length,
923 unsigned int dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800924{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000925 u32 *cs;
Chris Wilson78501ea2010-10-27 12:18:21 +0100926
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000927 cs = intel_ring_begin(req, 2);
928 if (IS_ERR(cs))
929 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100930
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000931 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
932 I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
933 *cs++ = offset;
934 intel_ring_advance(req, cs);
Chris Wilson78501ea2010-10-27 12:18:21 +0100935
Zou Nan haid1b851f2010-05-21 09:08:57 +0800936 return 0;
937}
938
Daniel Vetterb45305f2012-12-17 16:21:27 +0100939/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
940#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +0100941#define I830_TLB_ENTRIES (2)
942#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800943static int
Chris Wilson803688b2016-08-02 22:50:27 +0100944i830_emit_bb_start(struct drm_i915_gem_request *req,
945 u64 offset, u32 len,
946 unsigned int dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700947{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000948 u32 *cs, cs_offset = i915_ggtt_offset(req->engine->scratch);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700949
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000950 cs = intel_ring_begin(req, 6);
951 if (IS_ERR(cs))
952 return PTR_ERR(cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700953
Chris Wilsonc4d69da2014-09-08 14:25:41 +0100954 /* Evict the invalid PTE TLBs */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000955 *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
956 *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
957 *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
958 *cs++ = cs_offset;
959 *cs++ = 0xdeadbeef;
960 *cs++ = MI_NOOP;
961 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +0100962
John Harrison8e004ef2015-02-13 11:48:10 +0000963 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +0100964 if (len > I830_BATCH_LIMIT)
965 return -ENOSPC;
966
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000967 cs = intel_ring_begin(req, 6 + 2);
968 if (IS_ERR(cs))
969 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +0100970
971 /* Blit the batch (which has now all relocs applied) to the
972 * stable batch scratch bo area (so that the CS never
973 * stumbles over its tlb invalidation bug) ...
974 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000975 *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
976 *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
977 *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
978 *cs++ = cs_offset;
979 *cs++ = 4096;
980 *cs++ = offset;
Chris Wilsonc4d69da2014-09-08 14:25:41 +0100981
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000982 *cs++ = MI_FLUSH;
983 *cs++ = MI_NOOP;
984 intel_ring_advance(req, cs);
Daniel Vetterb45305f2012-12-17 16:21:27 +0100985
986 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +0100987 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +0100988 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100989
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000990 cs = intel_ring_begin(req, 2);
991 if (IS_ERR(cs))
992 return PTR_ERR(cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +0100993
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000994 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
995 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
996 MI_BATCH_NON_SECURE);
997 intel_ring_advance(req, cs);
Chris Wilsonc4d69da2014-09-08 14:25:41 +0100998
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200999 return 0;
1000}
1001
1002static int
Chris Wilson803688b2016-08-02 22:50:27 +01001003i915_emit_bb_start(struct drm_i915_gem_request *req,
1004 u64 offset, u32 len,
1005 unsigned int dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001006{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001007 u32 *cs;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001008
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001009 cs = intel_ring_begin(req, 2);
1010 if (IS_ERR(cs))
1011 return PTR_ERR(cs);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001012
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001013 *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
1014 *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
1015 MI_BATCH_NON_SECURE);
1016 intel_ring_advance(req, cs);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001017
Eric Anholt62fdfea2010-05-21 13:26:39 -07001018 return 0;
1019}
1020
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001021
Chris Wilson6b8294a2012-11-16 11:43:20 +00001022
Chris Wilsond822bb12017-04-03 12:34:25 +01001023int intel_ring_pin(struct intel_ring *ring,
1024 struct drm_i915_private *i915,
1025 unsigned int offset_bias)
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001026{
Chris Wilsond822bb12017-04-03 12:34:25 +01001027 enum i915_map_type map = HAS_LLC(i915) ? I915_MAP_WB : I915_MAP_WC;
Chris Wilson57e88532016-08-15 10:48:57 +01001028 struct i915_vma *vma = ring->vma;
Chris Wilsond822bb12017-04-03 12:34:25 +01001029 unsigned int flags;
Dave Gordon83052162016-04-12 14:46:16 +01001030 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001031 int ret;
1032
Chris Wilson57e88532016-08-15 10:48:57 +01001033 GEM_BUG_ON(ring->vaddr);
1034
Chris Wilson9d808412016-08-18 17:16:56 +01001035
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001036 flags = PIN_GLOBAL;
1037 if (offset_bias)
1038 flags |= PIN_OFFSET_BIAS | offset_bias;
Chris Wilson9d808412016-08-18 17:16:56 +01001039 if (vma->obj->stolen)
Chris Wilson57e88532016-08-15 10:48:57 +01001040 flags |= PIN_MAPPABLE;
1041
1042 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilson9d808412016-08-18 17:16:56 +01001043 if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
Chris Wilson57e88532016-08-15 10:48:57 +01001044 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1045 else
1046 ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
1047 if (unlikely(ret))
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001048 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001049 }
1050
Chris Wilson57e88532016-08-15 10:48:57 +01001051 ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
1052 if (unlikely(ret))
1053 return ret;
1054
Chris Wilson9d808412016-08-18 17:16:56 +01001055 if (i915_vma_is_map_and_fenceable(vma))
Chris Wilson57e88532016-08-15 10:48:57 +01001056 addr = (void __force *)i915_vma_pin_iomap(vma);
1057 else
Chris Wilson9d808412016-08-18 17:16:56 +01001058 addr = i915_gem_object_pin_map(vma->obj, map);
Chris Wilson57e88532016-08-15 10:48:57 +01001059 if (IS_ERR(addr))
1060 goto err;
1061
Chris Wilson3d574a62017-10-13 21:26:16 +01001062 vma->obj->pin_global++;
1063
Chris Wilson32c04f12016-08-02 22:50:22 +01001064 ring->vaddr = addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001065 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01001066
Chris Wilson57e88532016-08-15 10:48:57 +01001067err:
1068 i915_vma_unpin(vma);
1069 return PTR_ERR(addr);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001070}
1071
Chris Wilsone6ba9992017-04-25 14:00:49 +01001072void intel_ring_reset(struct intel_ring *ring, u32 tail)
1073{
1074 GEM_BUG_ON(!list_empty(&ring->request_list));
1075 ring->tail = tail;
1076 ring->head = tail;
1077 ring->emit = tail;
1078 intel_ring_update_space(ring);
1079}
1080
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001081void intel_ring_unpin(struct intel_ring *ring)
1082{
1083 GEM_BUG_ON(!ring->vma);
1084 GEM_BUG_ON(!ring->vaddr);
1085
Chris Wilsone6ba9992017-04-25 14:00:49 +01001086 /* Discard any unused bytes beyond that submitted to hw. */
1087 intel_ring_reset(ring, ring->tail);
1088
Chris Wilson9d808412016-08-18 17:16:56 +01001089 if (i915_vma_is_map_and_fenceable(ring->vma))
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001090 i915_vma_unpin_iomap(ring->vma);
Chris Wilson57e88532016-08-15 10:48:57 +01001091 else
1092 i915_gem_object_unpin_map(ring->vma->obj);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001093 ring->vaddr = NULL;
1094
Chris Wilson3d574a62017-10-13 21:26:16 +01001095 ring->vma->obj->pin_global--;
Chris Wilson57e88532016-08-15 10:48:57 +01001096 i915_vma_unpin(ring->vma);
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001097}
1098
Chris Wilson57e88532016-08-15 10:48:57 +01001099static struct i915_vma *
1100intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
Oscar Mateo2919d292014-07-03 16:28:02 +01001101{
Chris Wilsone3efda42014-04-09 09:19:41 +01001102 struct drm_i915_gem_object *obj;
Chris Wilson57e88532016-08-15 10:48:57 +01001103 struct i915_vma *vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001104
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00001105 obj = i915_gem_object_create_stolen(dev_priv, size);
Chris Wilsonc58b7352016-08-18 17:16:57 +01001106 if (!obj)
Chris Wilson2d6c4c82017-04-20 11:17:09 +01001107 obj = i915_gem_object_create_internal(dev_priv, size);
Chris Wilson57e88532016-08-15 10:48:57 +01001108 if (IS_ERR(obj))
1109 return ERR_CAST(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001110
Akash Goel24f3a8c2014-06-17 10:59:42 +05301111 /* mark ring buffers as read-only from GPU side by default */
1112 obj->gt_ro = 1;
1113
Chris Wilsona01cb372017-01-16 15:21:30 +00001114 vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
Chris Wilson57e88532016-08-15 10:48:57 +01001115 if (IS_ERR(vma))
1116 goto err;
Chris Wilsone3efda42014-04-09 09:19:41 +01001117
Chris Wilson57e88532016-08-15 10:48:57 +01001118 return vma;
1119
1120err:
1121 i915_gem_object_put(obj);
1122 return vma;
Chris Wilsone3efda42014-04-09 09:19:41 +01001123}
1124
Chris Wilson7e37f882016-08-02 22:50:21 +01001125struct intel_ring *
1126intel_engine_create_ring(struct intel_engine_cs *engine, int size)
Chris Wilson01101fa2015-09-03 13:01:39 +01001127{
Chris Wilson7e37f882016-08-02 22:50:21 +01001128 struct intel_ring *ring;
Chris Wilson57e88532016-08-15 10:48:57 +01001129 struct i915_vma *vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001130
Chris Wilson8f942012016-08-02 22:50:30 +01001131 GEM_BUG_ON(!is_power_of_2(size));
Chris Wilson62ae14b2016-10-04 21:11:25 +01001132 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
Chris Wilson8f942012016-08-02 22:50:30 +01001133
Chris Wilson01101fa2015-09-03 13:01:39 +01001134 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson57e88532016-08-15 10:48:57 +01001135 if (!ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001136 return ERR_PTR(-ENOMEM);
1137
Chris Wilson675d9ad2016-08-04 07:52:36 +01001138 INIT_LIST_HEAD(&ring->request_list);
1139
Chris Wilson01101fa2015-09-03 13:01:39 +01001140 ring->size = size;
1141 /* Workaround an erratum on the i830 which causes a hang if
1142 * the TAIL pointer points to within the last 2 cachelines
1143 * of the buffer.
1144 */
1145 ring->effective_size = size;
Jani Nikula2a307c22016-11-30 17:43:04 +02001146 if (IS_I830(engine->i915) || IS_I845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01001147 ring->effective_size -= 2 * CACHELINE_BYTES;
1148
Chris Wilson01101fa2015-09-03 13:01:39 +01001149 intel_ring_update_space(ring);
1150
Chris Wilson57e88532016-08-15 10:48:57 +01001151 vma = intel_ring_create_vma(engine->i915, size);
1152 if (IS_ERR(vma)) {
Chris Wilson01101fa2015-09-03 13:01:39 +01001153 kfree(ring);
Chris Wilson57e88532016-08-15 10:48:57 +01001154 return ERR_CAST(vma);
Chris Wilson01101fa2015-09-03 13:01:39 +01001155 }
Chris Wilson57e88532016-08-15 10:48:57 +01001156 ring->vma = vma;
Chris Wilson01101fa2015-09-03 13:01:39 +01001157
1158 return ring;
1159}
1160
1161void
Chris Wilson7e37f882016-08-02 22:50:21 +01001162intel_ring_free(struct intel_ring *ring)
Chris Wilson01101fa2015-09-03 13:01:39 +01001163{
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01001164 struct drm_i915_gem_object *obj = ring->vma->obj;
1165
1166 i915_vma_close(ring->vma);
1167 __i915_gem_object_release_unless_active(obj);
1168
Chris Wilson01101fa2015-09-03 13:01:39 +01001169 kfree(ring);
1170}
1171
Chris Wilson72b72ae2017-02-10 10:14:22 +00001172static int context_pin(struct i915_gem_context *ctx)
Chris Wilsone8a9c582016-12-18 15:37:20 +00001173{
1174 struct i915_vma *vma = ctx->engine[RCS].state;
1175 int ret;
1176
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001177 /*
1178 * Clear this page out of any CPU caches for coherent swap-in/out.
Chris Wilsone8a9c582016-12-18 15:37:20 +00001179 * We only want to do this on the first bind so that we do not stall
1180 * on an active context (which by nature is already on the GPU).
1181 */
1182 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001183 ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001184 if (ret)
1185 return ret;
1186 }
1187
Chris Wilsonafeddf52017-02-27 13:59:13 +00001188 return i915_vma_pin(vma, 0, I915_GTT_MIN_ALIGNMENT,
1189 PIN_GLOBAL | PIN_HIGH);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001190}
1191
Chris Wilson3204c342017-04-27 11:46:51 +01001192static struct i915_vma *
1193alloc_context_vma(struct intel_engine_cs *engine)
1194{
1195 struct drm_i915_private *i915 = engine->i915;
1196 struct drm_i915_gem_object *obj;
1197 struct i915_vma *vma;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001198 int err;
Chris Wilson3204c342017-04-27 11:46:51 +01001199
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001200 obj = i915_gem_object_create(i915, engine->context_size);
Chris Wilson3204c342017-04-27 11:46:51 +01001201 if (IS_ERR(obj))
1202 return ERR_CAST(obj);
1203
Chris Wilsond2b4b972017-11-10 14:26:33 +00001204 if (engine->default_state) {
1205 void *defaults, *vaddr;
1206
1207 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1208 if (IS_ERR(vaddr)) {
1209 err = PTR_ERR(vaddr);
1210 goto err_obj;
1211 }
1212
1213 defaults = i915_gem_object_pin_map(engine->default_state,
1214 I915_MAP_WB);
1215 if (IS_ERR(defaults)) {
1216 err = PTR_ERR(defaults);
1217 goto err_map;
1218 }
1219
1220 memcpy(vaddr, defaults, engine->context_size);
1221
1222 i915_gem_object_unpin_map(engine->default_state);
1223 i915_gem_object_unpin_map(obj);
1224 }
1225
Chris Wilson3204c342017-04-27 11:46:51 +01001226 /*
1227 * Try to make the context utilize L3 as well as LLC.
1228 *
1229 * On VLV we don't have L3 controls in the PTEs so we
1230 * shouldn't touch the cache level, especially as that
1231 * would make the object snooped which might have a
1232 * negative performance impact.
1233 *
1234 * Snooping is required on non-llc platforms in execlist
1235 * mode, but since all GGTT accesses use PAT entry 0 we
1236 * get snooping anyway regardless of cache_level.
1237 *
1238 * This is only applicable for Ivy Bridge devices since
1239 * later platforms don't have L3 control bits in the PTE.
1240 */
1241 if (IS_IVYBRIDGE(i915)) {
1242 /* Ignore any error, regard it as a simple optimisation */
1243 i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
1244 }
1245
1246 vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
Chris Wilsond2b4b972017-11-10 14:26:33 +00001247 if (IS_ERR(vma)) {
1248 err = PTR_ERR(vma);
1249 goto err_obj;
1250 }
Chris Wilson3204c342017-04-27 11:46:51 +01001251
1252 return vma;
Chris Wilsond2b4b972017-11-10 14:26:33 +00001253
1254err_map:
1255 i915_gem_object_unpin_map(obj);
1256err_obj:
1257 i915_gem_object_put(obj);
1258 return ERR_PTR(err);
Chris Wilson3204c342017-04-27 11:46:51 +01001259}
1260
Chris Wilson266a2402017-05-04 10:33:08 +01001261static struct intel_ring *
1262intel_ring_context_pin(struct intel_engine_cs *engine,
1263 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001264{
1265 struct intel_context *ce = &ctx->engine[engine->id];
1266 int ret;
1267
Chris Wilson91c8a322016-07-05 10:40:23 +01001268 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001269
Chris Wilson266a2402017-05-04 10:33:08 +01001270 if (likely(ce->pin_count++))
1271 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001272 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson0cb26a82016-06-24 14:55:53 +01001273
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001274 if (!ce->state && engine->context_size) {
Chris Wilson3204c342017-04-27 11:46:51 +01001275 struct i915_vma *vma;
1276
1277 vma = alloc_context_vma(engine);
1278 if (IS_ERR(vma)) {
1279 ret = PTR_ERR(vma);
Chris Wilson266a2402017-05-04 10:33:08 +01001280 goto err;
Chris Wilson3204c342017-04-27 11:46:51 +01001281 }
1282
1283 ce->state = vma;
1284 }
1285
Chris Wilson0cb26a82016-06-24 14:55:53 +01001286 if (ce->state) {
Chris Wilson72b72ae2017-02-10 10:14:22 +00001287 ret = context_pin(ctx);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001288 if (ret)
Chris Wilson266a2402017-05-04 10:33:08 +01001289 goto err;
Chris Wilson5d4bac52017-03-22 20:59:30 +00001290
Chris Wilson3d574a62017-10-13 21:26:16 +01001291 ce->state->obj->pin_global++;
Chris Wilson0cb26a82016-06-24 14:55:53 +01001292 }
1293
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001294 i915_gem_context_get(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001295
Chris Wilson266a2402017-05-04 10:33:08 +01001296out:
1297 /* One ringbuffer to rule them all */
1298 return engine->buffer;
1299
1300err:
Chris Wilson0cb26a82016-06-24 14:55:53 +01001301 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001302 return ERR_PTR(ret);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001303}
1304
Chris Wilsone8a9c582016-12-18 15:37:20 +00001305static void intel_ring_context_unpin(struct intel_engine_cs *engine,
1306 struct i915_gem_context *ctx)
Chris Wilson0cb26a82016-06-24 14:55:53 +01001307{
1308 struct intel_context *ce = &ctx->engine[engine->id];
1309
Chris Wilson91c8a322016-07-05 10:40:23 +01001310 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001311 GEM_BUG_ON(ce->pin_count == 0);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001312
1313 if (--ce->pin_count)
1314 return;
1315
Chris Wilson3d574a62017-10-13 21:26:16 +01001316 if (ce->state) {
1317 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001318 i915_vma_unpin(ce->state);
Chris Wilson3d574a62017-10-13 21:26:16 +01001319 }
Chris Wilson0cb26a82016-06-24 14:55:53 +01001320
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001321 i915_gem_context_put(ctx);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001322}
1323
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001324static int intel_init_ring_buffer(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001325{
Chris Wilson32c04f12016-08-02 22:50:22 +01001326 struct intel_ring *ring;
Chris Wilson1a5788b2017-04-03 12:34:26 +01001327 int err;
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001328
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001329 intel_engine_setup_common(engine);
1330
Chris Wilson1a5788b2017-04-03 12:34:26 +01001331 err = intel_engine_init_common(engine);
1332 if (err)
1333 goto err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001334
Chris Wilsond822bb12017-04-03 12:34:25 +01001335 ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
1336 if (IS_ERR(ring)) {
Chris Wilson1a5788b2017-04-03 12:34:26 +01001337 err = PTR_ERR(ring);
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001338 goto err;
Chris Wilsond822bb12017-04-03 12:34:25 +01001339 }
1340
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -08001341 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
Chris Wilson1a5788b2017-04-03 12:34:26 +01001342 err = intel_ring_pin(ring, engine->i915, I915_GTT_PAGE_SIZE);
1343 if (err)
1344 goto err_ring;
1345
1346 GEM_BUG_ON(engine->buffer);
Chris Wilson57e88532016-08-15 10:48:57 +01001347 engine->buffer = ring;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001348
Oscar Mateo8ee14972014-05-22 14:13:34 +01001349 return 0;
1350
Chris Wilson1a5788b2017-04-03 12:34:26 +01001351err_ring:
1352 intel_ring_free(ring);
Chris Wilson1a5788b2017-04-03 12:34:26 +01001353err:
1354 intel_engine_cleanup_common(engine);
1355 return err;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001356}
1357
Chris Wilson7e37f882016-08-02 22:50:21 +01001358void intel_engine_cleanup(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001359{
Chris Wilson1a5788b2017-04-03 12:34:26 +01001360 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson33626e62010-10-29 16:18:36 +01001361
Chris Wilson1a5788b2017-04-03 12:34:26 +01001362 WARN_ON(INTEL_GEN(dev_priv) > 2 &&
1363 (I915_READ_MODE(engine) & MODE_IDLE) == 0);
John Harrison6402c332014-10-31 12:00:26 +00001364
Chris Wilson1a5788b2017-04-03 12:34:26 +01001365 intel_ring_unpin(engine->buffer);
1366 intel_ring_free(engine->buffer);
Chris Wilson78501ea2010-10-27 12:18:21 +01001367
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001368 if (engine->cleanup)
1369 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08001370
Chris Wilson96a945a2016-08-03 13:19:16 +01001371 intel_engine_cleanup_common(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01001372
Akash Goel3b3f1652016-10-13 22:44:48 +05301373 dev_priv->engine[engine->id] = NULL;
1374 kfree(engine);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001375}
1376
Chris Wilson821ed7d2016-09-09 14:11:53 +01001377void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
1378{
1379 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301380 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001381
Chris Wilsone6ba9992017-04-25 14:00:49 +01001382 /* Restart from the beginning of the rings for convenience */
Chris Wilsonfe085f12017-03-21 10:25:52 +00001383 for_each_engine(engine, dev_priv, id)
Chris Wilsone6ba9992017-04-25 14:00:49 +01001384 intel_ring_reset(engine->buffer, 0);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001385}
1386
Chris Wilsonf73e7392016-12-18 15:37:24 +00001387static int ring_request_alloc(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00001388{
Chris Wilsonfd138212017-11-15 15:12:04 +00001389 int ret;
Chris Wilson63103462016-04-28 09:56:49 +01001390
Chris Wilsone8a9c582016-12-18 15:37:20 +00001391 GEM_BUG_ON(!request->ctx->engine[request->engine->id].pin_count);
1392
Chris Wilson63103462016-04-28 09:56:49 +01001393 /* Flush enough space to reduce the likelihood of waiting after
1394 * we start building the request - in which case we will just
1395 * have to repeat work.
1396 */
Chris Wilsona0442462016-04-29 09:07:05 +01001397 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001398
Chris Wilsonfd138212017-11-15 15:12:04 +00001399 ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
1400 if (ret)
1401 return ret;
Chris Wilson63103462016-04-28 09:56:49 +01001402
Chris Wilson3fef5cd2017-11-20 10:20:02 +00001403 ret = i915_switch_context(request);
1404 if (ret)
1405 return ret;
1406
Chris Wilsona0442462016-04-29 09:07:05 +01001407 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01001408 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00001409}
1410
Chris Wilsonfd138212017-11-15 15:12:04 +00001411static noinline int wait_for_space(struct intel_ring *ring, unsigned int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001412{
Chris Wilson987046a2016-04-28 09:56:46 +01001413 struct drm_i915_gem_request *target;
Chris Wilsone95433c2016-10-28 13:58:27 +01001414 long timeout;
1415
Chris Wilsonfd138212017-11-15 15:12:04 +00001416 lockdep_assert_held(&ring->vma->vm->i915->drm.struct_mutex);
Chris Wilson987046a2016-04-28 09:56:46 +01001417
Chris Wilson95aebcb2017-05-04 14:08:45 +01001418 if (intel_ring_update_space(ring) >= bytes)
Chris Wilson987046a2016-04-28 09:56:46 +01001419 return 0;
1420
Chris Wilson675d9ad2016-08-04 07:52:36 +01001421 list_for_each_entry(target, &ring->request_list, ring_link) {
Chris Wilson987046a2016-04-28 09:56:46 +01001422 /* Would completion of this request free enough space? */
Chris Wilson605d5b32017-05-04 14:08:44 +01001423 if (bytes <= __intel_ring_space(target->postfix,
1424 ring->emit, ring->size))
Chris Wilson987046a2016-04-28 09:56:46 +01001425 break;
1426 }
1427
Chris Wilson675d9ad2016-08-04 07:52:36 +01001428 if (WARN_ON(&target->ring_link == &ring->request_list))
Chris Wilson987046a2016-04-28 09:56:46 +01001429 return -ENOSPC;
1430
Chris Wilsone95433c2016-10-28 13:58:27 +01001431 timeout = i915_wait_request(target,
1432 I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
1433 MAX_SCHEDULE_TIMEOUT);
1434 if (timeout < 0)
1435 return timeout;
Chris Wilson7da844c2016-08-04 07:52:38 +01001436
Chris Wilson7da844c2016-08-04 07:52:38 +01001437 i915_gem_request_retire_upto(target);
1438
1439 intel_ring_update_space(ring);
1440 GEM_BUG_ON(ring->space < bytes);
1441 return 0;
Chris Wilson987046a2016-04-28 09:56:46 +01001442}
1443
Chris Wilsonfd138212017-11-15 15:12:04 +00001444int intel_ring_wait_for_space(struct intel_ring *ring, unsigned int bytes)
1445{
1446 GEM_BUG_ON(bytes > ring->effective_size);
1447 if (unlikely(bytes > ring->effective_size - ring->emit))
1448 bytes += ring->size - ring->emit;
1449
1450 if (unlikely(bytes > ring->space)) {
1451 int ret = wait_for_space(ring, bytes);
1452 if (unlikely(ret))
1453 return ret;
1454 }
1455
1456 GEM_BUG_ON(ring->space < bytes);
1457 return 0;
1458}
1459
Chris Wilson5e5655c2017-05-04 14:08:46 +01001460u32 *intel_ring_begin(struct drm_i915_gem_request *req,
1461 unsigned int num_dwords)
Chris Wilson987046a2016-04-28 09:56:46 +01001462{
Chris Wilson7e37f882016-08-02 22:50:21 +01001463 struct intel_ring *ring = req->ring;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001464 const unsigned int remain_usable = ring->effective_size - ring->emit;
1465 const unsigned int bytes = num_dwords * sizeof(u32);
1466 unsigned int need_wrap = 0;
1467 unsigned int total_bytes;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001468 u32 *cs;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001469
Chris Wilson6492ca72017-07-21 17:11:01 +01001470 /* Packets must be qword aligned. */
1471 GEM_BUG_ON(num_dwords & 1);
1472
Chris Wilson0251a962016-04-28 09:56:47 +01001473 total_bytes = bytes + req->reserved_space;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001474 GEM_BUG_ON(total_bytes > ring->effective_size);
John Harrison29b1b412015-06-18 13:10:09 +01001475
Chris Wilson5e5655c2017-05-04 14:08:46 +01001476 if (unlikely(total_bytes > remain_usable)) {
1477 const int remain_actual = ring->size - ring->emit;
1478
1479 if (bytes > remain_usable) {
1480 /*
1481 * Not enough space for the basic request. So need to
1482 * flush out the remainder and then wait for
1483 * base + reserved.
1484 */
1485 total_bytes += remain_actual;
1486 need_wrap = remain_actual | 1;
1487 } else {
1488 /*
1489 * The base request will fit but the reserved space
1490 * falls off the end. So we don't need an immediate
1491 * wrap and only need to effectively wait for the
1492 * reserved size from the start of ringbuffer.
1493 */
1494 total_bytes = req->reserved_space + remain_actual;
1495 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001496 }
1497
Chris Wilson5e5655c2017-05-04 14:08:46 +01001498 if (unlikely(total_bytes > ring->space)) {
Chris Wilsonfd138212017-11-15 15:12:04 +00001499 int ret;
1500
1501 /*
1502 * Space is reserved in the ringbuffer for finalising the
1503 * request, as that cannot be allowed to fail. During request
1504 * finalisation, reserved_space is set to 0 to stop the
1505 * overallocation and the assumption is that then we never need
1506 * to wait (which has the risk of failing with EINTR).
1507 *
1508 * See also i915_gem_request_alloc() and i915_add_request().
1509 */
1510 GEM_BUG_ON(!req->reserved_space);
1511
1512 ret = wait_for_space(ring, total_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001513 if (unlikely(ret))
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001514 return ERR_PTR(ret);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001515 }
1516
Chris Wilson987046a2016-04-28 09:56:46 +01001517 if (unlikely(need_wrap)) {
Chris Wilson5e5655c2017-05-04 14:08:46 +01001518 need_wrap &= ~1;
1519 GEM_BUG_ON(need_wrap > ring->space);
1520 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02001521
Chris Wilson987046a2016-04-28 09:56:46 +01001522 /* Fill the tail with MI_NOOP */
Chris Wilson5e5655c2017-05-04 14:08:46 +01001523 memset(ring->vaddr + ring->emit, 0, need_wrap);
Chris Wilsone6ba9992017-04-25 14:00:49 +01001524 ring->emit = 0;
Chris Wilson5e5655c2017-05-04 14:08:46 +01001525 ring->space -= need_wrap;
Chris Wilson987046a2016-04-28 09:56:46 +01001526 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001527
Chris Wilsone6ba9992017-04-25 14:00:49 +01001528 GEM_BUG_ON(ring->emit > ring->size - bytes);
Chris Wilson605d5b32017-05-04 14:08:44 +01001529 GEM_BUG_ON(ring->space < bytes);
Chris Wilsone6ba9992017-04-25 14:00:49 +01001530 cs = ring->vaddr + ring->emit;
Chris Wilson01001862017-04-23 18:06:17 +01001531 GEM_DEBUG_EXEC(memset(cs, POISON_INUSE, bytes));
Chris Wilsone6ba9992017-04-25 14:00:49 +01001532 ring->emit += bytes;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001533 ring->space -= bytes;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001534
1535 return cs;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001536}
1537
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001538/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01001539int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001540{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001541 int num_dwords =
Chris Wilsone6ba9992017-04-25 14:00:49 +01001542 (req->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001543 u32 *cs;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001544
1545 if (num_dwords == 0)
1546 return 0;
1547
Chris Wilson18393f62014-04-09 09:19:40 +01001548 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001549 cs = intel_ring_begin(req, num_dwords);
1550 if (IS_ERR(cs))
1551 return PTR_ERR(cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001552
1553 while (num_dwords--)
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001554 *cs++ = MI_NOOP;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001555
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001556 intel_ring_advance(req, cs);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02001557
1558 return 0;
1559}
1560
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001561static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001562{
Chris Wilsonc5efa1a2016-08-02 22:50:29 +01001563 struct drm_i915_private *dev_priv = request->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001564
Chris Wilson76f84212016-06-30 15:33:45 +01001565 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
1566
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001567 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001568
Chris Wilson12f55812012-07-05 17:14:01 +01001569 /* Disable notification that the ring is IDLE. The GT
1570 * will then assume that it is busy and bring it out of rc6.
1571 */
Chris Wilson76f84212016-06-30 15:33:45 +01001572 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1573 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01001574
1575 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01001576 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01001577
1578 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson02b312d2017-04-11 11:13:37 +01001579 if (__intel_wait_for_register_fw(dev_priv,
1580 GEN6_BSD_SLEEP_PSMI_CONTROL,
1581 GEN6_BSD_SLEEP_INDICATOR,
1582 0,
1583 1000, 0, NULL))
Chris Wilson12f55812012-07-05 17:14:01 +01001584 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001585
Chris Wilson12f55812012-07-05 17:14:01 +01001586 /* Now that the ring is fully powered up, update the tail */
Chris Wilsonb0411e72016-08-02 22:50:34 +01001587 i9xx_submit_request(request);
Chris Wilson12f55812012-07-05 17:14:01 +01001588
1589 /* Let the ring send IDLE messages to the GT again,
1590 * and so let it sleep to conserve power when idle.
1591 */
Chris Wilson76f84212016-06-30 15:33:45 +01001592 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
1593 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1594
1595 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001596}
1597
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001598static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001599{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001600 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001601
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001602 cs = intel_ring_begin(req, 4);
1603 if (IS_ERR(cs))
1604 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001605
Chris Wilson71a77e02011-02-02 12:13:49 +00001606 cmd = MI_FLUSH_DW;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001607
1608 /* We always require a command barrier so that subsequent
1609 * commands, such as breadcrumb interrupts, are strictly ordered
1610 * wrt the contents of the write cache being flushed to memory
1611 * (and thus being coherent from the CPU).
1612 */
1613 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1614
Jesse Barnes9a289772012-10-26 09:42:42 -07001615 /*
1616 * Bspec vol 1c.5 - video engine command streamer:
1617 * "If ENABLED, all TLBs will be invalidated once the flush
1618 * operation is complete. This bit is only valid when the
1619 * Post-Sync Operation field is a value of 1h or 3h."
1620 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001621 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001622 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1623
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001624 *cs++ = cmd;
1625 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilson79e67702017-11-20 20:55:01 +00001626 *cs++ = 0;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001627 *cs++ = MI_NOOP;
1628 intel_ring_advance(req, cs);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07001629 return 0;
1630}
1631
1632static int
Chris Wilson803688b2016-08-02 22:50:27 +01001633hsw_emit_bb_start(struct drm_i915_gem_request *req,
1634 u64 offset, u32 len,
1635 unsigned int dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001636{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001637 u32 *cs;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001638
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001639 cs = intel_ring_begin(req, 2);
1640 if (IS_ERR(cs))
1641 return PTR_ERR(cs);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001642
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001643 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1644 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
1645 (dispatch_flags & I915_DISPATCH_RS ?
1646 MI_BATCH_RESOURCE_STREAMER : 0);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001647 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001648 *cs++ = offset;
1649 intel_ring_advance(req, cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001650
1651 return 0;
1652}
1653
1654static int
Chris Wilson803688b2016-08-02 22:50:27 +01001655gen6_emit_bb_start(struct drm_i915_gem_request *req,
1656 u64 offset, u32 len,
1657 unsigned int dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001658{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001659 u32 *cs;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001660
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001661 cs = intel_ring_begin(req, 2);
1662 if (IS_ERR(cs))
1663 return PTR_ERR(cs);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001664
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001665 *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
1666 0 : MI_BATCH_NON_SECURE_I965);
Akshay Joshi0206e352011-08-16 15:34:10 -04001667 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001668 *cs++ = offset;
1669 intel_ring_advance(req, cs);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001670
Akshay Joshi0206e352011-08-16 15:34:10 -04001671 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001672}
1673
Chris Wilson549f7362010-10-19 11:19:32 +01001674/* Blitter support (SandyBridge+) */
1675
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001676static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
Zou Nan hai8d192152010-11-02 16:31:01 +08001677{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001678 u32 cmd, *cs;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001679
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001680 cs = intel_ring_begin(req, 4);
1681 if (IS_ERR(cs))
1682 return PTR_ERR(cs);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001683
Chris Wilson71a77e02011-02-02 12:13:49 +00001684 cmd = MI_FLUSH_DW;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001685
1686 /* We always require a command barrier so that subsequent
1687 * commands, such as breadcrumb interrupts, are strictly ordered
1688 * wrt the contents of the write cache being flushed to memory
1689 * (and thus being coherent from the CPU).
1690 */
1691 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1692
Jesse Barnes9a289772012-10-26 09:42:42 -07001693 /*
1694 * Bspec vol 1c.3 - blitter engine command streamer:
1695 * "If ENABLED, all TLBs will be invalidated once the flush
1696 * operation is complete. This bit is only valid when the
1697 * Post-Sync Operation field is a value of 1h or 3h."
1698 */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001699 if (mode & EMIT_INVALIDATE)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001700 cmd |= MI_INVALIDATE_TLB;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001701 *cs++ = cmd;
1702 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
Chris Wilson79e67702017-11-20 20:55:01 +00001703 *cs++ = 0;
1704 *cs++ = MI_NOOP;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001705 intel_ring_advance(req, cs);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03001706
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001707 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001708}
1709
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001710static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
1711 struct intel_engine_cs *engine)
1712{
Chris Wilson79e67702017-11-20 20:55:01 +00001713 int i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001714
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001715 if (!i915_modparams.semaphores)
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01001716 return;
1717
Chris Wilson79e67702017-11-20 20:55:01 +00001718 GEM_BUG_ON(INTEL_GEN(dev_priv) < 6);
1719 engine->semaphore.sync_to = gen6_ring_sync_to;
1720 engine->semaphore.signal = gen6_signal;
Chris Wilson51d545d2016-08-15 10:49:02 +01001721
Chris Wilson79e67702017-11-20 20:55:01 +00001722 /*
1723 * The current semaphore is only applied on pre-gen8
1724 * platform. And there is no VCS2 ring on the pre-gen8
1725 * platform. So the semaphore between RCS and VCS2 is
1726 * initialized as INVALID.
1727 */
1728 for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
1729 static const struct {
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001730 u32 wait_mbox;
1731 i915_reg_t mbox_reg;
Chris Wilson79e67702017-11-20 20:55:01 +00001732 } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
1733 [RCS_HW] = {
1734 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
1735 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
1736 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
1737 },
1738 [VCS_HW] = {
1739 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
1740 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
1741 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
1742 },
1743 [BCS_HW] = {
1744 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
1745 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
1746 [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
1747 },
1748 [VECS_HW] = {
1749 [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
1750 [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
1751 [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
1752 },
1753 };
1754 u32 wait_mbox;
1755 i915_reg_t mbox_reg;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001756
Chris Wilson79e67702017-11-20 20:55:01 +00001757 if (i == engine->hw_id) {
1758 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
1759 mbox_reg = GEN6_NOSYNC;
1760 } else {
1761 wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
1762 mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01001763 }
Chris Wilson79e67702017-11-20 20:55:01 +00001764
1765 engine->semaphore.mbox.wait[i] = wait_mbox;
1766 engine->semaphore.mbox.signal[i] = mbox_reg;
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01001767 }
1768}
1769
Chris Wilsoned003072016-07-01 09:18:13 +01001770static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
1771 struct intel_engine_cs *engine)
1772{
Tvrtko Ursulinc78d6062016-07-13 16:03:38 +01001773 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
1774
Chris Wilson79e67702017-11-20 20:55:01 +00001775 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01001776 engine->irq_enable = gen6_irq_enable;
1777 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01001778 engine->irq_seqno_barrier = gen6_seqno_barrier;
1779 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01001780 engine->irq_enable = gen5_irq_enable;
1781 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01001782 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01001783 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01001784 engine->irq_enable = i9xx_irq_enable;
1785 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01001786 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01001787 engine->irq_enable = i8xx_irq_enable;
1788 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01001789 }
1790}
1791
Chris Wilsonff44ad52017-03-16 17:13:03 +00001792static void i9xx_set_default_submission(struct intel_engine_cs *engine)
1793{
1794 engine->submit_request = i9xx_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01001795 engine->cancel_requests = cancel_requests;
Chris Wilsonaba5e272017-10-25 15:39:41 +01001796
1797 engine->park = NULL;
1798 engine->unpark = NULL;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001799}
1800
1801static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
1802{
Chris Wilsonaba5e272017-10-25 15:39:41 +01001803 i9xx_set_default_submission(engine);
Chris Wilsonff44ad52017-03-16 17:13:03 +00001804 engine->submit_request = gen6_bsd_submit_request;
1805}
1806
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01001807static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
1808 struct intel_engine_cs *engine)
1809{
Chris Wilson79e67702017-11-20 20:55:01 +00001810 /* gen8+ are only supported with execlists */
1811 GEM_BUG_ON(INTEL_GEN(dev_priv) >= 8);
1812
Chris Wilson618e4ca2016-08-02 22:50:35 +01001813 intel_ring_init_irq(dev_priv, engine);
1814 intel_ring_init_semaphores(dev_priv, engine);
1815
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01001816 engine->init_hw = init_ring_common;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001817 engine->reset_hw = reset_ring_common;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01001818
Chris Wilsone8a9c582016-12-18 15:37:20 +00001819 engine->context_pin = intel_ring_context_pin;
1820 engine->context_unpin = intel_ring_context_unpin;
1821
Chris Wilsonf73e7392016-12-18 15:37:24 +00001822 engine->request_alloc = ring_request_alloc;
1823
Chris Wilson9b81d552016-10-28 13:58:50 +01001824 engine->emit_breadcrumb = i9xx_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001825 engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001826 if (i915_modparams.semaphores) {
Chris Wilson98f29e82016-10-28 13:58:51 +01001827 int num_rings;
1828
Chris Wilson9b81d552016-10-28 13:58:50 +01001829 engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001830
Tvrtko Ursulinc58949f2017-06-19 11:59:17 +01001831 num_rings = INTEL_INFO(dev_priv)->num_rings - 1;
Chris Wilson79e67702017-11-20 20:55:01 +00001832 engine->emit_breadcrumb_sz += num_rings * 3;
1833 if (num_rings & 1)
1834 engine->emit_breadcrumb_sz++;
Chris Wilson98f29e82016-10-28 13:58:51 +01001835 }
Chris Wilsonff44ad52017-03-16 17:13:03 +00001836
1837 engine->set_default_submission = i9xx_set_default_submission;
Chris Wilson6f7bef72016-07-01 09:18:12 +01001838
Chris Wilson79e67702017-11-20 20:55:01 +00001839 if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson803688b2016-08-02 22:50:27 +01001840 engine->emit_bb_start = gen6_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01001841 else if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson803688b2016-08-02 22:50:27 +01001842 engine->emit_bb_start = i965_emit_bb_start;
Jani Nikula2a307c22016-11-30 17:43:04 +02001843 else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01001844 engine->emit_bb_start = i830_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01001845 else
Chris Wilson803688b2016-08-02 22:50:27 +01001846 engine->emit_bb_start = i915_emit_bb_start;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01001847}
1848
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01001849int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001850{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01001851 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001852 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001853
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01001854 intel_ring_default_vfuncs(dev_priv, engine);
1855
Chris Wilson61ff75a2016-07-01 17:23:28 +01001856 if (HAS_L3_DPF(dev_priv))
1857 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01001858
Chris Wilson79e67702017-11-20 20:55:01 +00001859 if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001860 engine->init_context = intel_rcs_ctx_init;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001861 engine->emit_flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01001862 if (IS_GEN6(dev_priv))
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001863 engine->emit_flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01001864 } else if (IS_GEN5(dev_priv)) {
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001865 engine->emit_flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02001866 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01001867 if (INTEL_GEN(dev_priv) < 4)
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001868 engine->emit_flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001869 else
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001870 engine->emit_flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001871 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001872 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001873
Chris Wilsonc0336662016-05-06 15:40:21 +01001874 if (IS_HASWELL(dev_priv))
Chris Wilson803688b2016-08-02 22:50:27 +01001875 engine->emit_bb_start = hsw_emit_bb_start;
Chris Wilson6f7bef72016-07-01 09:18:12 +01001876
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001877 engine->init_hw = init_render_ring;
Daniel Vetter59465b52012-04-11 22:12:48 +02001878
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001879 ret = intel_init_ring_buffer(engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01001880 if (ret)
1881 return ret;
1882
Chris Wilsonf8973c22016-07-01 17:23:21 +01001883 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsonf51455d2017-01-10 14:47:34 +00001884 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Chris Wilson7d5ea802016-07-01 17:23:20 +01001885 if (ret)
1886 return ret;
1887 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
Chris Wilson56c0f1a2016-08-15 10:48:58 +01001888 ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01001889 if (ret)
1890 return ret;
1891 }
1892
1893 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001894}
1895
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01001896int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001897{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01001898 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001899
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01001900 intel_ring_default_vfuncs(dev_priv, engine);
1901
Chris Wilsonc0336662016-05-06 15:40:21 +01001902 if (INTEL_GEN(dev_priv) >= 6) {
Daniel Vetter0fd2c202012-04-11 22:12:55 +02001903 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01001904 if (IS_GEN6(dev_priv))
Chris Wilsonff44ad52017-03-16 17:13:03 +00001905 engine->set_default_submission = gen6_bsd_set_default_submission;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001906 engine->emit_flush = gen6_bsd_ring_flush;
Chris Wilson79e67702017-11-20 20:55:01 +00001907 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001908 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001909 engine->mmio_base = BSD_RING_BASE;
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001910 engine->emit_flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01001911 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001912 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01001913 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001914 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001915 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02001916
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001917 return intel_init_ring_buffer(engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001918}
Chris Wilson549f7362010-10-19 11:19:32 +01001919
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01001920int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
Chris Wilson549f7362010-10-19 11:19:32 +01001921{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01001922 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01001923
1924 intel_ring_default_vfuncs(dev_priv, engine);
1925
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001926 engine->emit_flush = gen6_ring_flush;
Chris Wilson79e67702017-11-20 20:55:01 +00001927 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01001928
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001929 return intel_init_ring_buffer(engine);
Chris Wilson549f7362010-10-19 11:19:32 +01001930}
Chris Wilsona7b97612012-07-20 12:41:08 +01001931
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01001932int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07001933{
Tvrtko Ursulin8b3e2d32016-07-13 16:03:37 +01001934 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01001935
1936 intel_ring_default_vfuncs(dev_priv, engine);
1937
Chris Wilsonc7fe7d22016-08-02 22:50:24 +01001938 engine->emit_flush = gen6_ring_flush;
Chris Wilson79e67702017-11-20 20:55:01 +00001939 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
1940 engine->irq_enable = hsw_vebox_irq_enable;
1941 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07001942
Tvrtko Ursulinacd27842016-07-13 16:03:39 +01001943 return intel_init_ring_buffer(engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07001944}