blob: 1d3161bbea24ecc865c82287daa331adb6516f99 [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>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070032#include "i915_drv.h"
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/i915_drm.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070034#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010035#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070036
Chris Wilsona0442462016-04-29 09:07:05 +010037/* Rough estimate of the typical request size, performing a flush,
38 * set-context and then emitting the batch.
39 */
40#define LEGACY_REQUEST_SIZE 200
41
Oscar Mateo82e104c2014-07-24 17:04:26 +010042int __intel_ring_space(int head, int tail, int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010043{
Dave Gordon4f547412014-11-27 11:22:48 +000044 int space = head - tail;
45 if (space <= 0)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010046 space += size;
Dave Gordon4f547412014-11-27 11:22:48 +000047 return space - I915_RING_FREE_SPACE;
Chris Wilson1cf0ba12014-05-05 09:07:33 +010048}
49
Dave Gordonebd0fd42014-11-27 11:22:49 +000050void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
51{
52 if (ringbuf->last_retired_head != -1) {
53 ringbuf->head = ringbuf->last_retired_head;
54 ringbuf->last_retired_head = -1;
55 }
56
57 ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
58 ringbuf->tail, ringbuf->size);
59}
60
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000061static void __intel_ring_advance(struct intel_engine_cs *engine)
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020062{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000063 struct intel_ringbuffer *ringbuf = engine->buffer;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010064 ringbuf->tail &= ringbuf->size - 1;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +000065 engine->write_tail(engine, ringbuf->tail);
Chris Wilson09246732013-08-10 22:16:32 +010066}
67
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000068static int
John Harrisona84c3ae2015-05-29 17:43:57 +010069gen2_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010070 u32 invalidate_domains,
71 u32 flush_domains)
72{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +000073 struct intel_engine_cs *engine = req->engine;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010074 u32 cmd;
75 int ret;
76
77 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +020078 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010079 cmd |= MI_NO_WRITE_FLUSH;
80
81 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
82 cmd |= MI_READ_FLUSH;
83
John Harrison5fb9de12015-05-29 17:44:07 +010084 ret = intel_ring_begin(req, 2);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010085 if (ret)
86 return ret;
87
Tvrtko Ursuline2f80392016-03-16 11:00:36 +000088 intel_ring_emit(engine, cmd);
89 intel_ring_emit(engine, MI_NOOP);
90 intel_ring_advance(engine);
Chris Wilson46f0f8d2012-04-18 11:12:11 +010091
92 return 0;
93}
94
95static int
John Harrisona84c3ae2015-05-29 17:43:57 +010096gen4_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010097 u32 invalidate_domains,
98 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070099{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000100 struct intel_engine_cs *engine = req->engine;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100101 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000102 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100103
Chris Wilson36d527d2011-03-19 22:26:49 +0000104 /*
105 * read/write caches:
106 *
107 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
108 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
109 * also flushed at 2d versus 3d pipeline switches.
110 *
111 * read-only caches:
112 *
113 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
114 * MI_READ_FLUSH is set, and is always flushed on 965.
115 *
116 * I915_GEM_DOMAIN_COMMAND may not exist?
117 *
118 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
119 * invalidated when MI_EXE_FLUSH is set.
120 *
121 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
122 * invalidated with every MI_FLUSH.
123 *
124 * TLBs:
125 *
126 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
127 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
128 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
129 * are flushed at any MI_FLUSH.
130 */
131
132 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100133 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000134 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000135 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
136 cmd |= MI_EXE_FLUSH;
137
138 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
Chris Wilsonc0336662016-05-06 15:40:21 +0100139 (IS_G4X(req->i915) || IS_GEN5(req->i915)))
Chris Wilson36d527d2011-03-19 22:26:49 +0000140 cmd |= MI_INVALIDATE_ISP;
141
John Harrison5fb9de12015-05-29 17:44:07 +0100142 ret = intel_ring_begin(req, 2);
Chris Wilson36d527d2011-03-19 22:26:49 +0000143 if (ret)
144 return ret;
145
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000146 intel_ring_emit(engine, cmd);
147 intel_ring_emit(engine, MI_NOOP);
148 intel_ring_advance(engine);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000149
150 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800151}
152
Jesse Barnes8d315282011-10-16 10:23:31 +0200153/**
154 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
155 * implementing two workarounds on gen6. From section 1.4.7.1
156 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
157 *
158 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
159 * produced by non-pipelined state commands), software needs to first
160 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
161 * 0.
162 *
163 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
164 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
165 *
166 * And the workaround for these two requires this workaround first:
167 *
168 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
169 * BEFORE the pipe-control with a post-sync op and no write-cache
170 * flushes.
171 *
172 * And this last workaround is tricky because of the requirements on
173 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
174 * volume 2 part 1:
175 *
176 * "1 of the following must also be set:
177 * - Render Target Cache Flush Enable ([12] of DW1)
178 * - Depth Cache Flush Enable ([0] of DW1)
179 * - Stall at Pixel Scoreboard ([1] of DW1)
180 * - Depth Stall ([13] of DW1)
181 * - Post-Sync Operation ([13] of DW1)
182 * - Notify Enable ([8] of DW1)"
183 *
184 * The cache flushes require the workaround flush that triggered this
185 * one, so we can't use it. Depth stall would trigger the same.
186 * Post-sync nonzero is what triggered this second workaround, so we
187 * can't use that one either. Notify enable is IRQs, which aren't
188 * really our business. That leaves only stall at scoreboard.
189 */
190static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100191intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200192{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000193 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000194 u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200195 int ret;
196
John Harrison5fb9de12015-05-29 17:44:07 +0100197 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200198 if (ret)
199 return ret;
200
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000201 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(5));
202 intel_ring_emit(engine, PIPE_CONTROL_CS_STALL |
Jesse Barnes8d315282011-10-16 10:23:31 +0200203 PIPE_CONTROL_STALL_AT_SCOREBOARD);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000204 intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
205 intel_ring_emit(engine, 0); /* low dword */
206 intel_ring_emit(engine, 0); /* high dword */
207 intel_ring_emit(engine, MI_NOOP);
208 intel_ring_advance(engine);
Jesse Barnes8d315282011-10-16 10:23:31 +0200209
John Harrison5fb9de12015-05-29 17:44:07 +0100210 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200211 if (ret)
212 return ret;
213
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000214 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(5));
215 intel_ring_emit(engine, PIPE_CONTROL_QW_WRITE);
216 intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
217 intel_ring_emit(engine, 0);
218 intel_ring_emit(engine, 0);
219 intel_ring_emit(engine, MI_NOOP);
220 intel_ring_advance(engine);
Jesse Barnes8d315282011-10-16 10:23:31 +0200221
222 return 0;
223}
224
225static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100226gen6_render_ring_flush(struct drm_i915_gem_request *req,
227 u32 invalidate_domains, u32 flush_domains)
Jesse Barnes8d315282011-10-16 10:23:31 +0200228{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000229 struct intel_engine_cs *engine = req->engine;
Jesse Barnes8d315282011-10-16 10:23:31 +0200230 u32 flags = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000231 u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200232 int ret;
233
Paulo Zanonib3111502012-08-17 18:35:42 -0300234 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100235 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300236 if (ret)
237 return ret;
238
Jesse Barnes8d315282011-10-16 10:23:31 +0200239 /* Just flush everything. Experiments have shown that reducing the
240 * number of bits based on the write domains has little performance
241 * impact.
242 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100243 if (flush_domains) {
244 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
245 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
246 /*
247 * Ensure that any following seqno writes only happen
248 * when the render cache is indeed flushed.
249 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200250 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100251 }
252 if (invalidate_domains) {
253 flags |= PIPE_CONTROL_TLB_INVALIDATE;
254 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
255 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
256 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
257 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
258 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
259 /*
260 * TLB invalidate requires a post-sync write.
261 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700262 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100263 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200264
John Harrison5fb9de12015-05-29 17:44:07 +0100265 ret = intel_ring_begin(req, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200266 if (ret)
267 return ret;
268
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000269 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
270 intel_ring_emit(engine, flags);
271 intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
272 intel_ring_emit(engine, 0);
273 intel_ring_advance(engine);
Jesse Barnes8d315282011-10-16 10:23:31 +0200274
275 return 0;
276}
277
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100278static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100279gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300280{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000281 struct intel_engine_cs *engine = req->engine;
Paulo Zanonif3987632012-08-17 18:35:43 -0300282 int ret;
283
John Harrison5fb9de12015-05-29 17:44:07 +0100284 ret = intel_ring_begin(req, 4);
Paulo Zanonif3987632012-08-17 18:35:43 -0300285 if (ret)
286 return ret;
287
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000288 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
289 intel_ring_emit(engine, PIPE_CONTROL_CS_STALL |
Paulo Zanonif3987632012-08-17 18:35:43 -0300290 PIPE_CONTROL_STALL_AT_SCOREBOARD);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000291 intel_ring_emit(engine, 0);
292 intel_ring_emit(engine, 0);
293 intel_ring_advance(engine);
Paulo Zanonif3987632012-08-17 18:35:43 -0300294
295 return 0;
296}
297
298static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100299gen7_render_ring_flush(struct drm_i915_gem_request *req,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300300 u32 invalidate_domains, u32 flush_domains)
301{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000302 struct intel_engine_cs *engine = req->engine;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300303 u32 flags = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000304 u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300305 int ret;
306
Paulo Zanonif3987632012-08-17 18:35:43 -0300307 /*
308 * Ensure that any following seqno writes only happen when the render
309 * cache is indeed flushed.
310 *
311 * Workaround: 4th PIPE_CONTROL command (except the ones with only
312 * read-cache invalidate bits set) must have the CS_STALL bit set. We
313 * don't try to be clever and just set it unconditionally.
314 */
315 flags |= PIPE_CONTROL_CS_STALL;
316
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300317 /* Just flush everything. Experiments have shown that reducing the
318 * number of bits based on the write domains has little performance
319 * impact.
320 */
321 if (flush_domains) {
322 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
323 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800324 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100325 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300326 }
327 if (invalidate_domains) {
328 flags |= PIPE_CONTROL_TLB_INVALIDATE;
329 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
330 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
331 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
332 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
333 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000334 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300335 /*
336 * TLB invalidate requires a post-sync write.
337 */
338 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200339 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300340
Chris Wilsonadd284a2014-12-16 08:44:32 +0000341 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
342
Paulo Zanonif3987632012-08-17 18:35:43 -0300343 /* Workaround: we must issue a pipe_control with CS-stall bit
344 * set before a pipe_control command that has the state cache
345 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100346 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300347 }
348
John Harrison5fb9de12015-05-29 17:44:07 +0100349 ret = intel_ring_begin(req, 4);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300350 if (ret)
351 return ret;
352
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000353 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
354 intel_ring_emit(engine, flags);
355 intel_ring_emit(engine, scratch_addr);
356 intel_ring_emit(engine, 0);
357 intel_ring_advance(engine);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300358
359 return 0;
360}
361
Ben Widawskya5f3d682013-11-02 21:07:27 -0700362static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100363gen8_emit_pipe_control(struct drm_i915_gem_request *req,
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300364 u32 flags, u32 scratch_addr)
365{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000366 struct intel_engine_cs *engine = req->engine;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300367 int ret;
368
John Harrison5fb9de12015-05-29 17:44:07 +0100369 ret = intel_ring_begin(req, 6);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300370 if (ret)
371 return ret;
372
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000373 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
374 intel_ring_emit(engine, flags);
375 intel_ring_emit(engine, scratch_addr);
376 intel_ring_emit(engine, 0);
377 intel_ring_emit(engine, 0);
378 intel_ring_emit(engine, 0);
379 intel_ring_advance(engine);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300380
381 return 0;
382}
383
384static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100385gen8_render_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700386 u32 invalidate_domains, u32 flush_domains)
387{
388 u32 flags = 0;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000389 u32 scratch_addr = req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800390 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700391
392 flags |= PIPE_CONTROL_CS_STALL;
393
394 if (flush_domains) {
395 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
396 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -0800397 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +0100398 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700399 }
400 if (invalidate_domains) {
401 flags |= PIPE_CONTROL_TLB_INVALIDATE;
402 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
403 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
404 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
405 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
406 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
407 flags |= PIPE_CONTROL_QW_WRITE;
408 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800409
410 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100411 ret = gen8_emit_pipe_control(req,
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800412 PIPE_CONTROL_CS_STALL |
413 PIPE_CONTROL_STALL_AT_SCOREBOARD,
414 0);
415 if (ret)
416 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700417 }
418
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100419 return gen8_emit_pipe_control(req, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700420}
421
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000422static void ring_write_tail(struct intel_engine_cs *engine,
Chris Wilson297b0c52010-10-22 17:02:41 +0100423 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800424{
Chris Wilsonc0336662016-05-06 15:40:21 +0100425 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000426 I915_WRITE_TAIL(engine, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800427}
428
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000429u64 intel_ring_get_active_head(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800430{
Chris Wilsonc0336662016-05-06 15:40:21 +0100431 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson50877442014-03-21 12:41:53 +0000432 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800433
Chris Wilsonc0336662016-05-06 15:40:21 +0100434 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000435 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
436 RING_ACTHD_UDW(engine->mmio_base));
Chris Wilsonc0336662016-05-06 15:40:21 +0100437 else if (INTEL_GEN(dev_priv) >= 4)
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000438 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
Chris Wilson50877442014-03-21 12:41:53 +0000439 else
440 acthd = I915_READ(ACTHD);
441
442 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800443}
444
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000445static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200446{
Chris Wilsonc0336662016-05-06 15:40:21 +0100447 struct drm_i915_private *dev_priv = engine->i915;
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200448 u32 addr;
449
450 addr = dev_priv->status_page_dmah->busaddr;
Chris Wilsonc0336662016-05-06 15:40:21 +0100451 if (INTEL_GEN(dev_priv) >= 4)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200452 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
453 I915_WRITE(HWS_PGA, addr);
454}
455
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000456static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
Damien Lespiauaf75f262015-02-10 19:32:17 +0000457{
Chris Wilsonc0336662016-05-06 15:40:21 +0100458 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200459 i915_reg_t mmio;
Damien Lespiauaf75f262015-02-10 19:32:17 +0000460
461 /* The ring status page addresses are no longer next to the rest of
462 * the ring registers as of gen7.
463 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100464 if (IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000465 switch (engine->id) {
Damien Lespiauaf75f262015-02-10 19:32:17 +0000466 case RCS:
467 mmio = RENDER_HWS_PGA_GEN7;
468 break;
469 case BCS:
470 mmio = BLT_HWS_PGA_GEN7;
471 break;
472 /*
473 * VCS2 actually doesn't exist on Gen7. Only shut up
474 * gcc switch check warning
475 */
476 case VCS2:
477 case VCS:
478 mmio = BSD_HWS_PGA_GEN7;
479 break;
480 case VECS:
481 mmio = VEBOX_HWS_PGA_GEN7;
482 break;
483 }
Chris Wilsonc0336662016-05-06 15:40:21 +0100484 } else if (IS_GEN6(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000485 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000486 } else {
487 /* XXX: gen8 returns to sanity */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000488 mmio = RING_HWS_PGA(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000489 }
490
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000491 I915_WRITE(mmio, (u32)engine->status_page.gfx_addr);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000492 POSTING_READ(mmio);
493
494 /*
495 * Flush the TLB for this page
496 *
497 * FIXME: These two bits have disappeared on gen8, so a question
498 * arises: do we still need this and if so how should we go about
499 * invalidating the TLB?
500 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +0100501 if (IS_GEN(dev_priv, 6, 7)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000502 i915_reg_t reg = RING_INSTPM(engine->mmio_base);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000503
504 /* ring should be idle before issuing a sync flush*/
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000505 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000506
507 I915_WRITE(reg,
508 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
509 INSTPM_SYNC_FLUSH));
Chris Wilson25ab57f2016-06-30 15:33:29 +0100510 if (intel_wait_for_register(dev_priv,
511 reg, INSTPM_SYNC_FLUSH, 0,
512 1000))
Damien Lespiauaf75f262015-02-10 19:32:17 +0000513 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000514 engine->name);
Damien Lespiauaf75f262015-02-10 19:32:17 +0000515 }
516}
517
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000518static bool stop_ring(struct intel_engine_cs *engine)
Chris Wilson9991ae72014-04-02 16:36:07 +0100519{
Chris Wilsonc0336662016-05-06 15:40:21 +0100520 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson9991ae72014-04-02 16:36:07 +0100521
Chris Wilsonc0336662016-05-06 15:40:21 +0100522 if (!IS_GEN2(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000523 I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
Chris Wilson3d808eb2016-06-30 15:33:30 +0100524 if (intel_wait_for_register(dev_priv,
525 RING_MI_MODE(engine->mmio_base),
526 MODE_IDLE,
527 MODE_IDLE,
528 1000)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000529 DRM_ERROR("%s : timed out trying to stop ring\n",
530 engine->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100531 /* Sometimes we observe that the idle flag is not
532 * set even though the ring is empty. So double
533 * check before giving up.
534 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000535 if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
Chris Wilson9bec9b12014-08-11 09:21:35 +0100536 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100537 }
538 }
539
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000540 I915_WRITE_CTL(engine, 0);
541 I915_WRITE_HEAD(engine, 0);
542 engine->write_tail(engine, 0);
Chris Wilson9991ae72014-04-02 16:36:07 +0100543
Chris Wilsonc0336662016-05-06 15:40:21 +0100544 if (!IS_GEN2(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000545 (void)I915_READ_CTL(engine);
546 I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
Chris Wilson9991ae72014-04-02 16:36:07 +0100547 }
548
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000549 return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
Chris Wilson9991ae72014-04-02 16:36:07 +0100550}
551
Tomas Elffc0768c2016-03-21 16:26:59 +0000552void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
553{
554 memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
555}
556
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000557static int init_ring_common(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800558{
Chris Wilsonc0336662016-05-06 15:40:21 +0100559 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000560 struct intel_ringbuffer *ringbuf = engine->buffer;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100561 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200562 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800563
Mika Kuoppala59bad942015-01-16 11:34:40 +0200564 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200565
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000566 if (!stop_ring(engine)) {
Chris Wilson9991ae72014-04-02 16:36:07 +0100567 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000568 DRM_DEBUG_KMS("%s head not reset to zero "
569 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000570 engine->name,
571 I915_READ_CTL(engine),
572 I915_READ_HEAD(engine),
573 I915_READ_TAIL(engine),
574 I915_READ_START(engine));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800575
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000576 if (!stop_ring(engine)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000577 DRM_ERROR("failed to set %s head to zero "
578 "ctl %08x head %08x tail %08x start %08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000579 engine->name,
580 I915_READ_CTL(engine),
581 I915_READ_HEAD(engine),
582 I915_READ_TAIL(engine),
583 I915_READ_START(engine));
Chris Wilson9991ae72014-04-02 16:36:07 +0100584 ret = -EIO;
585 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000586 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700587 }
588
Chris Wilsonc0336662016-05-06 15:40:21 +0100589 if (I915_NEED_GFX_HWS(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000590 intel_ring_setup_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100591 else
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000592 ring_setup_phys_status_page(engine);
Chris Wilson9991ae72014-04-02 16:36:07 +0100593
Jiri Kosinaece4a172014-08-07 16:29:53 +0200594 /* Enforce ordering by reading HEAD register back */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000595 I915_READ_HEAD(engine);
Jiri Kosinaece4a172014-08-07 16:29:53 +0200596
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200597 /* Initialize the ring. This must happen _after_ we've cleared the ring
598 * registers with the above sequence (the readback of the HEAD registers
599 * also enforces ordering), otherwise the hw might lose the new ring
600 * register values. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000601 I915_WRITE_START(engine, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100602
603 /* WaClearRingBufHeadRegAtInit:ctg,elk */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000604 if (I915_READ_HEAD(engine))
Chris Wilson95468892014-08-07 15:39:54 +0100605 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000606 engine->name, I915_READ_HEAD(engine));
607 I915_WRITE_HEAD(engine, 0);
608 (void)I915_READ_HEAD(engine);
Chris Wilson95468892014-08-07 15:39:54 +0100609
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000610 I915_WRITE_CTL(engine,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100611 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000612 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800613
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800614 /* If the head is still not zero, the ring is dead */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000615 if (wait_for((I915_READ_CTL(engine) & RING_VALID) != 0 &&
616 I915_READ_START(engine) == i915_gem_obj_ggtt_offset(obj) &&
617 (I915_READ_HEAD(engine) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000618 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100619 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000620 engine->name,
621 I915_READ_CTL(engine),
622 I915_READ_CTL(engine) & RING_VALID,
623 I915_READ_HEAD(engine), I915_READ_TAIL(engine),
624 I915_READ_START(engine),
625 (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200626 ret = -EIO;
627 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800628 }
629
Dave Gordonebd0fd42014-11-27 11:22:49 +0000630 ringbuf->last_retired_head = -1;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000631 ringbuf->head = I915_READ_HEAD(engine);
632 ringbuf->tail = I915_READ_TAIL(engine) & TAIL_ADDR;
Dave Gordonebd0fd42014-11-27 11:22:49 +0000633 intel_ring_update_space(ringbuf);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000634
Tomas Elffc0768c2016-03-21 16:26:59 +0000635 intel_engine_init_hangcheck(engine);
Chris Wilson50f018d2013-06-10 11:20:19 +0100636
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200637out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200638 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200639
640 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700641}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800642
Chris Wilsonf8291952016-07-01 17:23:18 +0100643void intel_fini_pipe_control(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100644{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000645 if (engine->scratch.obj == NULL)
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100646 return;
647
Chris Wilsonf8291952016-07-01 17:23:18 +0100648 i915_gem_object_ggtt_unpin(engine->scratch.obj);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000649 drm_gem_object_unreference(&engine->scratch.obj->base);
650 engine->scratch.obj = NULL;
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100651}
652
Chris Wilson7d5ea802016-07-01 17:23:20 +0100653int intel_init_pipe_control(struct intel_engine_cs *engine, int size)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000654{
Chris Wilsonf8291952016-07-01 17:23:18 +0100655 struct drm_i915_gem_object *obj;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000656 int ret;
657
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000658 WARN_ON(engine->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000659
Chris Wilson91c8a322016-07-05 10:40:23 +0100660 obj = i915_gem_object_create_stolen(&engine->i915->drm, size);
Chris Wilsonde8fe162016-07-01 17:23:19 +0100661 if (!obj)
Chris Wilson91c8a322016-07-05 10:40:23 +0100662 obj = i915_gem_object_create(&engine->i915->drm, size);
Chris Wilsonf8291952016-07-01 17:23:18 +0100663 if (IS_ERR(obj)) {
664 DRM_ERROR("Failed to allocate scratch page\n");
665 ret = PTR_ERR(obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000666 goto err;
667 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100668
Chris Wilsonf8291952016-07-01 17:23:18 +0100669 ret = i915_gem_obj_ggtt_pin(obj, 4096, PIN_HIGH);
Daniel Vettera9cc7262014-02-14 14:01:13 +0100670 if (ret)
671 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000672
Chris Wilsonf8291952016-07-01 17:23:18 +0100673 engine->scratch.obj = obj;
674 engine->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200675 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000676 engine->name, engine->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000677 return 0;
678
Chris Wilsonc6df5412010-12-15 09:56:50 +0000679err_unref:
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000680 drm_gem_object_unreference(&engine->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000681err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000682 return ret;
683}
684
John Harrisone2be4fa2015-05-29 17:43:54 +0100685static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100686{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000687 struct intel_engine_cs *engine = req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +0100688 struct i915_workarounds *w = &req->i915->workarounds;
689 int ret, i;
Arun Siluvery888b5992014-08-26 14:44:51 +0100690
Francisco Jerez02235802015-10-07 14:44:01 +0300691 if (w->count == 0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300692 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100693
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000694 engine->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100695 ret = intel_ring_flush_all_caches(req);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100696 if (ret)
697 return ret;
698
John Harrison5fb9de12015-05-29 17:44:07 +0100699 ret = intel_ring_begin(req, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300700 if (ret)
701 return ret;
702
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000703 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300704 for (i = 0; i < w->count; i++) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000705 intel_ring_emit_reg(engine, w->reg[i].addr);
706 intel_ring_emit(engine, w->reg[i].value);
Mika Kuoppala72253422014-10-07 17:21:26 +0300707 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000708 intel_ring_emit(engine, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300709
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000710 intel_ring_advance(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +0300711
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000712 engine->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100713 ret = intel_ring_flush_all_caches(req);
Mika Kuoppala72253422014-10-07 17:21:26 +0300714 if (ret)
715 return ret;
716
717 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
718
719 return 0;
720}
721
John Harrison87531812015-05-29 17:43:44 +0100722static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100723{
724 int ret;
725
John Harrisone2be4fa2015-05-29 17:43:54 +0100726 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100727 if (ret != 0)
728 return ret;
729
John Harrisonbe013632015-05-29 17:43:45 +0100730 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100731 if (ret)
Chris Wilsone26e1b92016-01-29 16:49:05 +0000732 return ret;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100733
Chris Wilsone26e1b92016-01-29 16:49:05 +0000734 return 0;
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100735}
736
Mika Kuoppala72253422014-10-07 17:21:26 +0300737static int wa_add(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200738 i915_reg_t addr,
739 const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300740{
741 const u32 idx = dev_priv->workarounds.count;
742
743 if (WARN_ON(idx >= I915_MAX_WA_REGS))
744 return -ENOSPC;
745
746 dev_priv->workarounds.reg[idx].addr = addr;
747 dev_priv->workarounds.reg[idx].value = val;
748 dev_priv->workarounds.reg[idx].mask = mask;
749
750 dev_priv->workarounds.count++;
751
752 return 0;
753}
754
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100755#define WA_REG(addr, mask, val) do { \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000756 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300757 if (r) \
758 return r; \
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100759 } while (0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300760
761#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000762 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300763
764#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000765 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300766
Damien Lespiau98533252014-12-08 17:33:51 +0000767#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000768 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300769
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000770#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
771#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300772
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000773#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300774
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000775static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
776 i915_reg_t reg)
Arun Siluvery33136b02016-01-21 21:43:47 +0000777{
Chris Wilsonc0336662016-05-06 15:40:21 +0100778 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery33136b02016-01-21 21:43:47 +0000779 struct i915_workarounds *wa = &dev_priv->workarounds;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000780 const uint32_t index = wa->hw_whitelist_count[engine->id];
Arun Siluvery33136b02016-01-21 21:43:47 +0000781
782 if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
783 return -EINVAL;
784
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000785 WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
Arun Siluvery33136b02016-01-21 21:43:47 +0000786 i915_mmio_reg_offset(reg));
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000787 wa->hw_whitelist_count[engine->id]++;
Arun Siluvery33136b02016-01-21 21:43:47 +0000788
789 return 0;
790}
791
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000792static int gen8_init_workarounds(struct intel_engine_cs *engine)
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100793{
Chris Wilsonc0336662016-05-06 15:40:21 +0100794 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluvery68c61982015-09-25 17:40:38 +0100795
796 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100797
Arun Siluvery717d84d2015-09-25 17:40:39 +0100798 /* WaDisableAsyncFlipPerfMode:bdw,chv */
799 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
800
Arun Siluveryd0581192015-09-25 17:40:40 +0100801 /* WaDisablePartialInstShootdown:bdw,chv */
802 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
803 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
804
Arun Siluverya340af52015-09-25 17:40:45 +0100805 /* Use Force Non-Coherent whenever executing a 3D context. This is a
806 * workaround for for a possible hang in the unlikely event a TLB
807 * invalidation occurs during a PSD flush.
808 */
809 /* WaForceEnableNonCoherent:bdw,chv */
Arun Siluvery120f5d22015-09-25 17:40:46 +0100810 /* WaHdcDisableFetchWhenMasked:bdw,chv */
Arun Siluverya340af52015-09-25 17:40:45 +0100811 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Arun Siluvery120f5d22015-09-25 17:40:46 +0100812 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Arun Siluverya340af52015-09-25 17:40:45 +0100813 HDC_FORCE_NON_COHERENT);
814
Arun Siluvery6def8fd2015-09-25 17:40:42 +0100815 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
816 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
817 * polygons in the same 8x4 pixel/sample area to be processed without
818 * stalling waiting for the earlier ones to write to Hierarchical Z
819 * buffer."
820 *
821 * This optimization is off by default for BDW and CHV; turn it on.
822 */
823 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
824
Arun Siluvery48404632015-09-25 17:40:43 +0100825 /* Wa4x4STCOptimizationDisable:bdw,chv */
826 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
827
Arun Siluvery7eebcde2015-09-25 17:40:44 +0100828 /*
829 * BSpec recommends 8x4 when MSAA is used,
830 * however in practice 16x4 seems fastest.
831 *
832 * Note that PS/WM thread counts depend on the WIZ hashing
833 * disable bit, which we don't touch here, but it's good
834 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
835 */
836 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
837 GEN6_WIZ_HASHING_MASK,
838 GEN6_WIZ_HASHING_16x4);
839
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100840 return 0;
841}
842
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000843static int bdw_init_workarounds(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +0300844{
Chris Wilsonc0336662016-05-06 15:40:21 +0100845 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100846 int ret;
Mika Kuoppala72253422014-10-07 17:21:26 +0300847
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000848 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100849 if (ret)
850 return ret;
851
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700852 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Arun Siluveryd0581192015-09-25 17:40:40 +0100853 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100854
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700855 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300856 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
857 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100858
Mika Kuoppala72253422014-10-07 17:21:26 +0300859 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
860 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100861
Mika Kuoppala72253422014-10-07 17:21:26 +0300862 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000863 /* WaForceContextSaveRestoreNonCoherent:bdw */
864 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000865 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Chris Wilsonc0336662016-05-06 15:40:21 +0100866 (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100867
Arun Siluvery86d7f232014-08-26 14:44:50 +0100868 return 0;
869}
870
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000871static int chv_init_workarounds(struct intel_engine_cs *engine)
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300872{
Chris Wilsonc0336662016-05-06 15:40:21 +0100873 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100874 int ret;
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300875
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000876 ret = gen8_init_workarounds(engine);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100877 if (ret)
878 return ret;
879
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300880 /* WaDisableThreadStallDopClockGating:chv */
Arun Siluveryd0581192015-09-25 17:40:40 +0100881 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300882
Kenneth Graunked60de812015-01-10 18:02:22 -0800883 /* Improve HiZ throughput on CHV. */
884 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
885
Mika Kuoppala72253422014-10-07 17:21:26 +0300886 return 0;
887}
888
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000889static int gen9_init_workarounds(struct intel_engine_cs *engine)
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000890{
Chris Wilsonc0336662016-05-06 15:40:21 +0100891 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluverye0f3fa02016-01-21 21:43:48 +0000892 int ret;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000893
Tim Gorea8ab5ed2016-06-13 12:15:01 +0100894 /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
895 I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
896
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300897 /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300898 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
899 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
900
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300901 /* WaDisableKillLogic:bxt,skl,kbl */
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +0300902 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
903 ECOCHK_DIS_TLB);
904
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300905 /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
906 /* WaDisablePartialInstShootdown:skl,bxt,kbl */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000907 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Tim Gore950b2aa2016-03-16 16:13:46 +0000908 FLOW_CONTROL_ENABLE |
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000909 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
910
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300911 /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
Nick Hoath84241712015-02-05 10:47:20 +0000912 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
913 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
914
Jani Nikulae87a0052015-10-20 15:22:02 +0300915 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100916 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
917 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Damien Lespiaua86eb582015-02-11 18:21:44 +0000918 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
919 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000920
Jani Nikulae87a0052015-10-20 15:22:02 +0300921 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100922 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
923 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Damien Lespiau183c6da2015-02-09 19:33:11 +0000924 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
925 GEN9_RHWO_OPTIMIZATION_DISABLE);
Arun Siluvery9b014352015-07-14 15:01:30 +0100926 /*
927 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
928 * but we do that in per ctx batchbuffer as there is an issue
929 * with this register not getting restored on ctx restore
930 */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000931 }
932
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300933 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl */
934 /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
Tim Gorebfd8ad42016-04-19 15:45:52 +0100935 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
936 GEN9_ENABLE_YV12_BUGFIX |
937 GEN9_ENABLE_GPGPU_PREEMPTION);
Nick Hoathcac23df2015-02-05 10:47:22 +0000938
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300939 /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
940 /* WaDisablePartialResolveInVc:skl,bxt,kbl */
Arun Siluvery60294682015-09-25 14:33:37 +0100941 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
942 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
Damien Lespiau9370cd92015-02-09 19:33:17 +0000943
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300944 /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000945 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
946 GEN9_CCS_TLB_PREFETCH_ENABLE);
947
Imre Deak5a2ae952015-05-19 15:04:59 +0300948 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +0100949 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_C0) ||
950 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200951 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
952 PIXEL_MASK_CAMMING_DISABLE);
953
Mika Kuoppala5b0e3652016-06-07 17:18:57 +0300954 /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
955 WA_SET_BIT_MASKED(HDC_CHICKEN0,
956 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
957 HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
Imre Deak8ea6f892015-05-19 17:05:42 +0300958
Mika Kuoppalabbaefe72016-06-07 17:18:58 +0300959 /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
960 * both tied to WaForceContextSaveRestoreNonCoherent
961 * in some hsds for skl. We keep the tie for all gen9. The
962 * documentation is a bit hazy and so we want to get common behaviour,
963 * even though there is no clear evidence we would need both on kbl/bxt.
964 * This area has been source of system hangs so we play it safe
965 * and mimic the skl regardless of what bspec says.
966 *
967 * Use Force Non-Coherent whenever executing a 3D context. This
968 * is a workaround for a possible hang in the unlikely event
969 * a TLB invalidation occurs during a PSD flush.
970 */
971
972 /* WaForceEnableNonCoherent:skl,bxt,kbl */
973 WA_SET_BIT_MASKED(HDC_CHICKEN0,
974 HDC_FORCE_NON_COHERENT);
975
976 /* WaDisableHDCInvalidation:skl,bxt,kbl */
977 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
978 BDW_DISABLE_HDC_INVALIDATION);
979
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300980 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
981 if (IS_SKYLAKE(dev_priv) ||
982 IS_KABYLAKE(dev_priv) ||
983 IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
Arun Siluvery8c761602015-09-08 10:31:48 +0100984 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
985 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery8c761602015-09-08 10:31:48 +0100986
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300987 /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
Robert Beckett6b6d5622015-09-08 10:31:52 +0100988 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
989
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300990 /* WaOCLCoherentLineFlush:skl,bxt,kbl */
Arun Siluvery6ecf56a2016-01-21 21:43:54 +0000991 I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
992 GEN8_LQSC_FLUSH_COHERENT_LINES));
993
arun.siluvery@linux.intel.com6bb628552016-06-06 09:52:49 +0100994 /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
995 ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
996 if (ret)
997 return ret;
998
Mika Kuoppalae5f81d62016-06-07 17:18:54 +0300999 /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001000 ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
Arun Siluverye0f3fa02016-01-21 21:43:48 +00001001 if (ret)
1002 return ret;
1003
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001004 /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001005 ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
Arun Siluvery3669ab62016-01-21 21:43:49 +00001006 if (ret)
1007 return ret;
1008
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001009 return 0;
1010}
1011
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001012static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +00001013{
Chris Wilsonc0336662016-05-06 15:40:21 +01001014 struct drm_i915_private *dev_priv = engine->i915;
Damien Lespiaub7668792015-02-14 18:30:29 +00001015 u8 vals[3] = { 0, 0, 0 };
1016 unsigned int i;
1017
1018 for (i = 0; i < 3; i++) {
1019 u8 ss;
1020
1021 /*
1022 * Only consider slices where one, and only one, subslice has 7
1023 * EUs
1024 */
Zeng Zhaoxiua4d8a0f2015-12-06 18:26:30 +08001025 if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
Damien Lespiaub7668792015-02-14 18:30:29 +00001026 continue;
1027
1028 /*
1029 * subslice_7eu[i] != 0 (because of the check above) and
1030 * ss_max == 4 (maximum number of subslices possible per slice)
1031 *
1032 * -> 0 <= ss <= 3;
1033 */
1034 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1035 vals[i] = 3 - ss;
1036 }
1037
1038 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1039 return 0;
1040
1041 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1042 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1043 GEN9_IZ_HASHING_MASK(2) |
1044 GEN9_IZ_HASHING_MASK(1) |
1045 GEN9_IZ_HASHING_MASK(0),
1046 GEN9_IZ_HASHING(2, vals[2]) |
1047 GEN9_IZ_HASHING(1, vals[1]) |
1048 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001049
Mika Kuoppala72253422014-10-07 17:21:26 +03001050 return 0;
1051}
1052
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001053static int skl_init_workarounds(struct intel_engine_cs *engine)
Damien Lespiau8d205492015-02-09 19:33:15 +00001054{
Chris Wilsonc0336662016-05-06 15:40:21 +01001055 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001056 int ret;
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001057
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001058 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001059 if (ret)
1060 return ret;
Damien Lespiau8d205492015-02-09 19:33:15 +00001061
Arun Siluverya78536e2016-01-21 21:43:53 +00001062 /*
1063 * Actual WA is to disable percontext preemption granularity control
1064 * until D0 which is the default case so this is equivalent to
1065 * !WaDisablePerCtxtPreemptionGranularityControl:skl
1066 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001067 if (IS_SKL_REVID(dev_priv, SKL_REVID_E0, REVID_FOREVER)) {
Arun Siluverya78536e2016-01-21 21:43:53 +00001068 I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
1069 _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
1070 }
1071
Mika Kuoppala71dce582016-06-07 17:19:14 +03001072 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001073 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
1074 I915_WRITE(FF_SLICE_CS_CHICKEN2,
1075 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
1076 }
1077
1078 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1079 * involving this register should also be added to WA batch as required.
1080 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001081 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001082 /* WaDisableLSQCROPERFforOCL:skl */
1083 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1084 GEN8_LQSC_RO_PERF_DIS);
1085
1086 /* WaEnableGapsTsvCreditFix:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001087 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, REVID_FOREVER)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001088 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1089 GEN9_GAPS_TSV_CREDIT_DISABLE));
1090 }
1091
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001092 /* WaDisablePowerCompilerClockGating:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001093 if (IS_SKL_REVID(dev_priv, SKL_REVID_B0, SKL_REVID_B0))
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001094 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1095 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1096
Jani Nikulae87a0052015-10-20 15:22:02 +03001097 /* WaBarrierPerformanceFixDisable:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001098 if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_D0))
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001099 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1100 HDC_FENCE_DEST_SLM_DISABLE |
1101 HDC_BARRIER_PERFORMANCE_DISABLE);
1102
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001103 /* WaDisableSbeCacheDispatchPortSharing:skl */
Chris Wilsonc0336662016-05-06 15:40:21 +01001104 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0))
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001105 WA_SET_BIT_MASKED(
1106 GEN7_HALF_SLICE_CHICKEN1,
1107 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001108
Mika Kuoppalaeee8efb2016-06-07 17:18:53 +03001109 /* WaDisableGafsUnitClkGating:skl */
1110 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1111
Mika Kuoppalaf15f6ca2016-07-20 14:26:12 +03001112 /* WaInPlaceDecompressionHang:skl */
1113 if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
1114 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1115 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1116
Arun Siluvery61074972016-01-21 21:43:52 +00001117 /* WaDisableLSQCROPERFforOCL:skl */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001118 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluvery61074972016-01-21 21:43:52 +00001119 if (ret)
1120 return ret;
1121
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001122 return skl_tune_iz_hashing(engine);
Damien Lespiau8d205492015-02-09 19:33:15 +00001123}
1124
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001125static int bxt_init_workarounds(struct intel_engine_cs *engine)
Nick Hoathcae04372015-03-17 11:39:38 +02001126{
Chris Wilsonc0336662016-05-06 15:40:21 +01001127 struct drm_i915_private *dev_priv = engine->i915;
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001128 int ret;
Nick Hoathdfb601e2015-04-10 13:12:24 +01001129
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001130 ret = gen9_init_workarounds(engine);
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001131 if (ret)
1132 return ret;
Nick Hoathcae04372015-03-17 11:39:38 +02001133
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001134 /* WaStoreMultiplePTEenable:bxt */
1135 /* This is a requirement according to Hardware specification */
Chris Wilsonc0336662016-05-06 15:40:21 +01001136 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001137 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
1138
1139 /* WaSetClckGatingDisableMedia:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001140 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Mika Kuoppala9c4cbf82015-10-12 13:20:59 +03001141 I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
1142 ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
1143 }
1144
Nick Hoathdfb601e2015-04-10 13:12:24 +01001145 /* WaDisableThreadStallDopClockGating:bxt */
1146 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1147 STALL_DOP_GATING_DISABLE);
1148
arun.siluvery@linux.intel.com780f0ae2016-06-03 11:16:10 +01001149 /* WaDisablePooledEuLoadBalancingFix:bxt */
1150 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
1151 WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
1152 GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
1153 }
1154
Nick Hoath983b4b92015-04-10 13:12:25 +01001155 /* WaDisableSbeCacheDispatchPortSharing:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001156 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
Nick Hoath983b4b92015-04-10 13:12:25 +01001157 WA_SET_BIT_MASKED(
1158 GEN7_HALF_SLICE_CHICKEN1,
1159 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1160 }
1161
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001162 /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
1163 /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
1164 /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
Arun Siluverya786d532016-01-21 21:43:51 +00001165 /* WaDisableLSQCROPERFforOCL:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001166 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001167 ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001168 if (ret)
1169 return ret;
Arun Siluverya786d532016-01-21 21:43:51 +00001170
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001171 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
Arun Siluverya786d532016-01-21 21:43:51 +00001172 if (ret)
1173 return ret;
Arun Siluvery2c8580e2016-01-21 21:43:50 +00001174 }
1175
Tim Gore050fc462016-04-22 09:46:01 +01001176 /* WaProgramL3SqcReg1DefaultForPerf:bxt */
Chris Wilsonc0336662016-05-06 15:40:21 +01001177 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
Imre Deak36579cb2016-05-03 15:54:20 +03001178 I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
1179 L3_HIGH_PRIO_CREDITS(2));
Tim Gore050fc462016-04-22 09:46:01 +01001180
Matthew Auldbf74c932016-08-02 09:36:53 +01001181 /* WaToEnableHwFixForPushConstHWBug:bxt */
1182 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001183 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1184 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1185
Mika Kuoppalaf15f6ca2016-07-20 14:26:12 +03001186 /* WaInPlaceDecompressionHang:bxt */
1187 if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
1188 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1189 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1190
Nick Hoathcae04372015-03-17 11:39:38 +02001191 return 0;
1192}
1193
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001194static int kbl_init_workarounds(struct intel_engine_cs *engine)
1195{
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001196 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001197 int ret;
1198
1199 ret = gen9_init_workarounds(engine);
1200 if (ret)
1201 return ret;
1202
Mika Kuoppalae587f6c2016-06-07 17:18:59 +03001203 /* WaEnableGapsTsvCreditFix:kbl */
1204 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
1205 GEN9_GAPS_TSV_CREDIT_DISABLE));
1206
Mika Kuoppalac0b730d2016-06-07 17:19:06 +03001207 /* WaDisableDynamicCreditSharing:kbl */
1208 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
1209 WA_SET_BIT(GAMT_CHKN_BIT_REG,
1210 GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
1211
Mika Kuoppala8401d422016-06-07 17:19:00 +03001212 /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
1213 if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
1214 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1215 HDC_FENCE_DEST_SLM_DISABLE);
1216
Mika Kuoppalafe905812016-06-07 17:19:03 +03001217 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
1218 * involving this register should also be added to WA batch as required.
1219 */
1220 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
1221 /* WaDisableLSQCROPERFforOCL:kbl */
1222 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
1223 GEN8_LQSC_RO_PERF_DIS);
1224
Matthew Auldbf74c932016-08-02 09:36:53 +01001225 /* WaToEnableHwFixForPushConstHWBug:kbl */
1226 if (IS_KBL_REVID(dev_priv, KBL_REVID_C0, REVID_FOREVER))
Mika Kuoppalaad2bdb42016-06-07 17:19:07 +03001227 WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
1228 GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
1229
Mika Kuoppala4de5d7c2016-06-07 17:19:11 +03001230 /* WaDisableGafsUnitClkGating:kbl */
1231 WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
1232
Mika Kuoppala954337a2016-06-07 17:19:12 +03001233 /* WaDisableSbeCacheDispatchPortSharing:kbl */
1234 WA_SET_BIT_MASKED(
1235 GEN7_HALF_SLICE_CHICKEN1,
1236 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1237
Mika Kuoppalaf15f6ca2016-07-20 14:26:12 +03001238 /* WaInPlaceDecompressionHang:kbl */
1239 WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
1240 GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
1241
Mika Kuoppalafe905812016-06-07 17:19:03 +03001242 /* WaDisableLSQCROPERFforOCL:kbl */
1243 ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
1244 if (ret)
1245 return ret;
1246
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001247 return 0;
1248}
1249
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001250int init_workarounds_ring(struct intel_engine_cs *engine)
Mika Kuoppala72253422014-10-07 17:21:26 +03001251{
Chris Wilsonc0336662016-05-06 15:40:21 +01001252 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala72253422014-10-07 17:21:26 +03001253
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001254 WARN_ON(engine->id != RCS);
Mika Kuoppala72253422014-10-07 17:21:26 +03001255
1256 dev_priv->workarounds.count = 0;
Arun Siluvery33136b02016-01-21 21:43:47 +00001257 dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
Mika Kuoppala72253422014-10-07 17:21:26 +03001258
Chris Wilsonc0336662016-05-06 15:40:21 +01001259 if (IS_BROADWELL(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001260 return bdw_init_workarounds(engine);
Mika Kuoppala72253422014-10-07 17:21:26 +03001261
Chris Wilsonc0336662016-05-06 15:40:21 +01001262 if (IS_CHERRYVIEW(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001263 return chv_init_workarounds(engine);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001264
Chris Wilsonc0336662016-05-06 15:40:21 +01001265 if (IS_SKYLAKE(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001266 return skl_init_workarounds(engine);
Nick Hoathcae04372015-03-17 11:39:38 +02001267
Chris Wilsonc0336662016-05-06 15:40:21 +01001268 if (IS_BROXTON(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001269 return bxt_init_workarounds(engine);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001270
Mika Kuoppalae5f81d62016-06-07 17:18:54 +03001271 if (IS_KABYLAKE(dev_priv))
1272 return kbl_init_workarounds(engine);
1273
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001274 return 0;
1275}
1276
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001277static int init_render_ring(struct intel_engine_cs *engine)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001278{
Chris Wilsonc0336662016-05-06 15:40:21 +01001279 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001280 int ret = init_ring_common(engine);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001281 if (ret)
1282 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001283
Akash Goel61a563a2014-03-25 18:01:50 +05301284 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001285 if (IS_GEN(dev_priv, 4, 6))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001286 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001287
1288 /* We need to disable the AsyncFlip performance optimisations in order
1289 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1290 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001291 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001292 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001293 */
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001294 if (IS_GEN(dev_priv, 6, 7))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001295 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1296
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001297 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301298 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonc0336662016-05-06 15:40:21 +01001299 if (IS_GEN6(dev_priv))
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001300 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001301 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001302
Akash Goel01fa0302014-03-24 23:00:04 +05301303 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilsonc0336662016-05-06 15:40:21 +01001304 if (IS_GEN7(dev_priv))
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001305 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301306 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001307 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001308
Chris Wilsonc0336662016-05-06 15:40:21 +01001309 if (IS_GEN6(dev_priv)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001310 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1311 * "If this bit is set, STCunit will have LRA as replacement
1312 * policy. [...] This bit must be reset. LRA replacement
1313 * policy is not supported."
1314 */
1315 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001316 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001317 }
1318
Tvrtko Ursulinac657f62016-05-10 10:57:08 +01001319 if (IS_GEN(dev_priv, 6, 7))
Daniel Vetter6b26c862012-04-24 14:04:12 +02001320 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001321
Ville Syrjäläb224c4d2016-07-12 19:24:47 +03001322 if (INTEL_INFO(dev_priv)->gen >= 6)
1323 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawsky15b9f802012-05-25 16:56:23 -07001324
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001325 return init_workarounds_ring(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001326}
1327
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001328static void render_ring_cleanup(struct intel_engine_cs *engine)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001329{
Chris Wilsonc0336662016-05-06 15:40:21 +01001330 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001331
1332 if (dev_priv->semaphore_obj) {
1333 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1334 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1335 dev_priv->semaphore_obj = NULL;
1336 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001337
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001338 intel_fini_pipe_control(engine);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001339}
1340
John Harrisonf7169682015-05-29 17:44:05 +01001341static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001342 unsigned int num_dwords)
1343{
1344#define MBOX_UPDATE_DWORDS 8
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001345 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001346 struct drm_i915_private *dev_priv = signaller_req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001347 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001348 enum intel_engine_id id;
1349 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001350
Chris Wilsonc0336662016-05-06 15:40:21 +01001351 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawsky3e789982014-06-30 09:53:37 -07001352 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1353#undef MBOX_UPDATE_DWORDS
1354
John Harrison5fb9de12015-05-29 17:44:07 +01001355 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001356 if (ret)
1357 return ret;
1358
Dave Gordonc3232b12016-03-23 18:19:53 +00001359 for_each_engine_id(waiter, dev_priv, id) {
Dave Gordonc3232b12016-03-23 18:19:53 +00001360 u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001361 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1362 continue;
1363
1364 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1365 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1366 PIPE_CONTROL_QW_WRITE |
Chris Wilsonf9a4ea32016-04-29 13:18:24 +01001367 PIPE_CONTROL_CS_STALL);
Ben Widawsky3e789982014-06-30 09:53:37 -07001368 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1369 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
Chris Wilson1b7744e2016-07-01 17:23:17 +01001370 intel_ring_emit(signaller, signaller_req->seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001371 intel_ring_emit(signaller, 0);
1372 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
Chris Wilson215a7e32016-04-29 13:18:23 +01001373 MI_SEMAPHORE_TARGET(waiter->hw_id));
Ben Widawsky3e789982014-06-30 09:53:37 -07001374 intel_ring_emit(signaller, 0);
1375 }
1376
1377 return 0;
1378}
1379
John Harrisonf7169682015-05-29 17:44:05 +01001380static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001381 unsigned int num_dwords)
1382{
1383#define MBOX_UPDATE_DWORDS 6
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001384 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001385 struct drm_i915_private *dev_priv = signaller_req->i915;
Ben Widawsky3e789982014-06-30 09:53:37 -07001386 struct intel_engine_cs *waiter;
Dave Gordonc3232b12016-03-23 18:19:53 +00001387 enum intel_engine_id id;
1388 int ret, num_rings;
Ben Widawsky3e789982014-06-30 09:53:37 -07001389
Chris Wilsonc0336662016-05-06 15:40:21 +01001390 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawsky3e789982014-06-30 09:53:37 -07001391 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1392#undef MBOX_UPDATE_DWORDS
1393
John Harrison5fb9de12015-05-29 17:44:07 +01001394 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001395 if (ret)
1396 return ret;
1397
Dave Gordonc3232b12016-03-23 18:19:53 +00001398 for_each_engine_id(waiter, dev_priv, id) {
Dave Gordonc3232b12016-03-23 18:19:53 +00001399 u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
Ben Widawsky3e789982014-06-30 09:53:37 -07001400 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1401 continue;
1402
1403 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1404 MI_FLUSH_DW_OP_STOREDW);
1405 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1406 MI_FLUSH_DW_USE_GTT);
1407 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
Chris Wilson1b7744e2016-07-01 17:23:17 +01001408 intel_ring_emit(signaller, signaller_req->seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001409 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
Chris Wilson215a7e32016-04-29 13:18:23 +01001410 MI_SEMAPHORE_TARGET(waiter->hw_id));
Ben Widawsky3e789982014-06-30 09:53:37 -07001411 intel_ring_emit(signaller, 0);
1412 }
1413
1414 return 0;
1415}
1416
John Harrisonf7169682015-05-29 17:44:05 +01001417static int gen6_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001418 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001419{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001420 struct intel_engine_cs *signaller = signaller_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001421 struct drm_i915_private *dev_priv = signaller_req->i915;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001422 struct intel_engine_cs *useless;
Dave Gordonc3232b12016-03-23 18:19:53 +00001423 enum intel_engine_id id;
1424 int ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001425
Ben Widawskya1444b72014-06-30 09:53:35 -07001426#define MBOX_UPDATE_DWORDS 3
Chris Wilsonc0336662016-05-06 15:40:21 +01001427 num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
Ben Widawskya1444b72014-06-30 09:53:35 -07001428 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1429#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001430
John Harrison5fb9de12015-05-29 17:44:07 +01001431 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky024a43e2014-04-29 14:52:30 -07001432 if (ret)
1433 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001434
Dave Gordonc3232b12016-03-23 18:19:53 +00001435 for_each_engine_id(useless, dev_priv, id) {
1436 i915_reg_t mbox_reg = signaller->semaphore.mbox.signal[id];
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02001437
1438 if (i915_mmio_reg_valid(mbox_reg)) {
Ben Widawsky78325f22014-04-29 14:52:29 -07001439 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
Ville Syrjäläf92a9162015-11-04 23:20:07 +02001440 intel_ring_emit_reg(signaller, mbox_reg);
Chris Wilson1b7744e2016-07-01 17:23:17 +01001441 intel_ring_emit(signaller, signaller_req->seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001442 }
1443 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001444
Ben Widawskya1444b72014-06-30 09:53:35 -07001445 /* If num_dwords was rounded, make sure the tail pointer is correct */
1446 if (num_rings % 2 == 0)
1447 intel_ring_emit(signaller, MI_NOOP);
1448
Ben Widawsky024a43e2014-04-29 14:52:30 -07001449 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001450}
1451
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001452/**
1453 * gen6_add_request - Update the semaphore mailbox registers
John Harrisonee044a82015-05-29 17:44:00 +01001454 *
1455 * @request - request to write to the ring
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001456 *
1457 * Update the mailbox registers in the *other* rings with the current seqno.
1458 * This acts like a signal in the canonical semaphore.
1459 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001460static int
John Harrisonee044a82015-05-29 17:44:00 +01001461gen6_add_request(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001462{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001463 struct intel_engine_cs *engine = req->engine;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001464 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001465
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001466 if (engine->semaphore.signal)
1467 ret = engine->semaphore.signal(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001468 else
John Harrison5fb9de12015-05-29 17:44:07 +01001469 ret = intel_ring_begin(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001470
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001471 if (ret)
1472 return ret;
1473
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001474 intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
1475 intel_ring_emit(engine,
1476 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson1b7744e2016-07-01 17:23:17 +01001477 intel_ring_emit(engine, req->seqno);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001478 intel_ring_emit(engine, MI_USER_INTERRUPT);
1479 __intel_ring_advance(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001480
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001481 return 0;
1482}
1483
Chris Wilsona58c01a2016-04-29 13:18:21 +01001484static int
1485gen8_render_add_request(struct drm_i915_gem_request *req)
1486{
1487 struct intel_engine_cs *engine = req->engine;
1488 int ret;
1489
1490 if (engine->semaphore.signal)
1491 ret = engine->semaphore.signal(req, 8);
1492 else
1493 ret = intel_ring_begin(req, 8);
1494 if (ret)
1495 return ret;
1496
1497 intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
1498 intel_ring_emit(engine, (PIPE_CONTROL_GLOBAL_GTT_IVB |
1499 PIPE_CONTROL_CS_STALL |
1500 PIPE_CONTROL_QW_WRITE));
1501 intel_ring_emit(engine, intel_hws_seqno_address(req->engine));
1502 intel_ring_emit(engine, 0);
1503 intel_ring_emit(engine, i915_gem_request_get_seqno(req));
1504 /* We're thrashing one dword of HWS. */
1505 intel_ring_emit(engine, 0);
1506 intel_ring_emit(engine, MI_USER_INTERRUPT);
1507 intel_ring_emit(engine, MI_NOOP);
1508 __intel_ring_advance(engine);
1509
1510 return 0;
1511}
1512
Chris Wilsonc0336662016-05-06 15:40:21 +01001513static inline bool i915_gem_has_seqno_wrapped(struct drm_i915_private *dev_priv,
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001514 u32 seqno)
1515{
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001516 return dev_priv->last_seqno < seqno;
1517}
1518
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001519/**
1520 * intel_ring_sync - sync the waiter to the signaller on seqno
1521 *
1522 * @waiter - ring that is waiting
1523 * @signaller - ring which has, or will signal
1524 * @seqno - seqno which the waiter will block on
1525 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001526
1527static int
John Harrison599d9242015-05-29 17:44:04 +01001528gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001529 struct intel_engine_cs *signaller,
1530 u32 seqno)
1531{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001532 struct intel_engine_cs *waiter = waiter_req->engine;
Chris Wilsonc0336662016-05-06 15:40:21 +01001533 struct drm_i915_private *dev_priv = waiter_req->i915;
Tvrtko Ursulinc38c6512016-06-29 16:09:30 +01001534 u64 offset = GEN8_WAIT_OFFSET(waiter, signaller->id);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001535 struct i915_hw_ppgtt *ppgtt;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001536 int ret;
1537
John Harrison5fb9de12015-05-29 17:44:07 +01001538 ret = intel_ring_begin(waiter_req, 4);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001539 if (ret)
1540 return ret;
1541
1542 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1543 MI_SEMAPHORE_GLOBAL_GTT |
1544 MI_SEMAPHORE_SAD_GTE_SDD);
1545 intel_ring_emit(waiter, seqno);
Tvrtko Ursulinc38c6512016-06-29 16:09:30 +01001546 intel_ring_emit(waiter, lower_32_bits(offset));
1547 intel_ring_emit(waiter, upper_32_bits(offset));
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001548 intel_ring_advance(waiter);
Chris Wilson6ef48d72016-04-29 13:18:25 +01001549
1550 /* When the !RCS engines idle waiting upon a semaphore, they lose their
1551 * pagetables and we must reload them before executing the batch.
1552 * We do this on the i915_switch_context() following the wait and
1553 * before the dispatch.
1554 */
1555 ppgtt = waiter_req->ctx->ppgtt;
1556 if (ppgtt && waiter_req->engine->id != RCS)
1557 ppgtt->pd_dirty_rings |= intel_engine_flag(waiter_req->engine);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001558 return 0;
1559}
1560
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001561static int
John Harrison599d9242015-05-29 17:44:04 +01001562gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001563 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001564 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001565{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001566 struct intel_engine_cs *waiter = waiter_req->engine;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001567 u32 dw1 = MI_SEMAPHORE_MBOX |
1568 MI_SEMAPHORE_COMPARE |
1569 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001570 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1571 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001572
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001573 /* Throughout all of the GEM code, seqno passed implies our current
1574 * seqno is >= the last seqno executed. However for hardware the
1575 * comparison is strictly greater than.
1576 */
1577 seqno -= 1;
1578
Ben Widawskyebc348b2014-04-29 14:52:28 -07001579 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001580
John Harrison5fb9de12015-05-29 17:44:07 +01001581 ret = intel_ring_begin(waiter_req, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001582 if (ret)
1583 return ret;
1584
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001585 /* If seqno wrap happened, omit the wait with no-ops */
Chris Wilsonc0336662016-05-06 15:40:21 +01001586 if (likely(!i915_gem_has_seqno_wrapped(waiter_req->i915, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001587 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001588 intel_ring_emit(waiter, seqno);
1589 intel_ring_emit(waiter, 0);
1590 intel_ring_emit(waiter, MI_NOOP);
1591 } else {
1592 intel_ring_emit(waiter, MI_NOOP);
1593 intel_ring_emit(waiter, MI_NOOP);
1594 intel_ring_emit(waiter, MI_NOOP);
1595 intel_ring_emit(waiter, MI_NOOP);
1596 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001597 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001598
1599 return 0;
1600}
1601
Chris Wilsonf8973c22016-07-01 17:23:21 +01001602static void
1603gen5_seqno_barrier(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001604{
Chris Wilsonf8973c22016-07-01 17:23:21 +01001605 /* MI_STORE are internally buffered by the GPU and not flushed
1606 * either by MI_FLUSH or SyncFlush or any other combination of
1607 * MI commands.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001608 *
Chris Wilsonf8973c22016-07-01 17:23:21 +01001609 * "Only the submission of the store operation is guaranteed.
1610 * The write result will be complete (coherent) some time later
1611 * (this is practically a finite period but there is no guaranteed
1612 * latency)."
1613 *
1614 * Empirically, we observe that we need a delay of at least 75us to
1615 * be sure that the seqno write is visible by the CPU.
Chris Wilsonc6df5412010-12-15 09:56:50 +00001616 */
Chris Wilsonf8973c22016-07-01 17:23:21 +01001617 usleep_range(125, 250);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001618}
1619
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001620static void
1621gen6_seqno_barrier(struct intel_engine_cs *engine)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001622{
Chris Wilsonc0336662016-05-06 15:40:21 +01001623 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001624
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001625 /* Workaround to force correct ordering between irq and seqno writes on
1626 * ivb (and maybe also on snb) by reading from a CS register (like
Chris Wilson9b9ed302016-04-09 10:57:53 +01001627 * ACTHD) before reading the status page.
1628 *
1629 * Note that this effectively stalls the read by the time it takes to
1630 * do a memory transaction, which more or less ensures that the write
1631 * from the GPU has sufficient time to invalidate the CPU cacheline.
1632 * Alternatively we could delay the interrupt from the CS ring to give
1633 * the write time to land, but that would incur a delay after every
1634 * batch i.e. much more frequent than a delay when waiting for the
1635 * interrupt (with the same net latency).
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001636 *
1637 * Also note that to prevent whole machine hangs on gen7, we have to
1638 * take the spinlock to guard against concurrent cacheline access.
Chris Wilson9b9ed302016-04-09 10:57:53 +01001639 */
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001640 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc04e0f32016-04-09 10:57:54 +01001641 POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
Chris Wilsonbcbdb6d2016-04-27 09:02:01 +01001642 spin_unlock_irq(&dev_priv->uncore.lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001643}
1644
Chris Wilson31bb59c2016-07-01 17:23:27 +01001645static void
1646gen5_irq_enable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001647{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001648 gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
Daniel Vettere48d8632012-04-11 22:12:54 +02001649}
1650
1651static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001652gen5_irq_disable(struct intel_engine_cs *engine)
Daniel Vettere48d8632012-04-11 22:12:54 +02001653{
Chris Wilson31bb59c2016-07-01 17:23:27 +01001654 gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001655}
1656
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001657static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001658i9xx_irq_enable(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001659{
Chris Wilsonc0336662016-05-06 15:40:21 +01001660 struct drm_i915_private *dev_priv = engine->i915;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001661
Chris Wilson31bb59c2016-07-01 17:23:27 +01001662 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1663 I915_WRITE(IMR, dev_priv->irq_mask);
1664 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Chris Wilsonc2798b12012-04-22 21:13:57 +01001665}
1666
1667static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001668i9xx_irq_disable(struct intel_engine_cs *engine)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001669{
Chris Wilsonc0336662016-05-06 15:40:21 +01001670 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001671
Chris Wilson31bb59c2016-07-01 17:23:27 +01001672 dev_priv->irq_mask |= engine->irq_enable_mask;
1673 I915_WRITE(IMR, dev_priv->irq_mask);
1674}
1675
1676static void
1677i8xx_irq_enable(struct intel_engine_cs *engine)
1678{
1679 struct drm_i915_private *dev_priv = engine->i915;
1680
1681 dev_priv->irq_mask &= ~engine->irq_enable_mask;
1682 I915_WRITE16(IMR, dev_priv->irq_mask);
1683 POSTING_READ16(RING_IMR(engine->mmio_base));
1684}
1685
1686static void
1687i8xx_irq_disable(struct intel_engine_cs *engine)
1688{
1689 struct drm_i915_private *dev_priv = engine->i915;
1690
1691 dev_priv->irq_mask |= engine->irq_enable_mask;
1692 I915_WRITE16(IMR, dev_priv->irq_mask);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001693}
1694
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001695static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001696bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001697 u32 invalidate_domains,
1698 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001699{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001700 struct intel_engine_cs *engine = req->engine;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001701 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001702
John Harrison5fb9de12015-05-29 17:44:07 +01001703 ret = intel_ring_begin(req, 2);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001704 if (ret)
1705 return ret;
1706
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001707 intel_ring_emit(engine, MI_FLUSH);
1708 intel_ring_emit(engine, MI_NOOP);
1709 intel_ring_advance(engine);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001710 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001711}
1712
Chris Wilson3cce4692010-10-27 16:11:02 +01001713static int
John Harrisonee044a82015-05-29 17:44:00 +01001714i9xx_add_request(struct drm_i915_gem_request *req)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001715{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001716 struct intel_engine_cs *engine = req->engine;
Chris Wilson3cce4692010-10-27 16:11:02 +01001717 int ret;
1718
John Harrison5fb9de12015-05-29 17:44:07 +01001719 ret = intel_ring_begin(req, 4);
Chris Wilson3cce4692010-10-27 16:11:02 +01001720 if (ret)
1721 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001722
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001723 intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
1724 intel_ring_emit(engine,
1725 I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Chris Wilson1b7744e2016-07-01 17:23:17 +01001726 intel_ring_emit(engine, req->seqno);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001727 intel_ring_emit(engine, MI_USER_INTERRUPT);
1728 __intel_ring_advance(engine);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001729
Chris Wilson3cce4692010-10-27 16:11:02 +01001730 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001731}
1732
Chris Wilson0f468322011-01-04 17:35:21 +00001733static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001734gen6_irq_enable(struct intel_engine_cs *engine)
Chris Wilson0f468322011-01-04 17:35:21 +00001735{
Chris Wilsonc0336662016-05-06 15:40:21 +01001736 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson0f468322011-01-04 17:35:21 +00001737
Chris Wilson61ff75a2016-07-01 17:23:28 +01001738 I915_WRITE_IMR(engine,
1739 ~(engine->irq_enable_mask |
1740 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001741 gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001742}
1743
1744static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001745gen6_irq_disable(struct intel_engine_cs *engine)
Ben Widawskya19d2932013-05-28 19:22:30 -07001746{
Chris Wilsonc0336662016-05-06 15:40:21 +01001747 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskya19d2932013-05-28 19:22:30 -07001748
Chris Wilson61ff75a2016-07-01 17:23:28 +01001749 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Chris Wilson31bb59c2016-07-01 17:23:27 +01001750 gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001751}
1752
1753static void
Chris Wilson31bb59c2016-07-01 17:23:27 +01001754hsw_vebox_irq_enable(struct intel_engine_cs *engine)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001755{
Chris Wilsonc0336662016-05-06 15:40:21 +01001756 struct drm_i915_private *dev_priv = engine->i915;
Ben Widawskyabd58f02013-11-02 21:07:09 -07001757
Chris Wilson31bb59c2016-07-01 17:23:27 +01001758 I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
1759 gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
1760}
1761
1762static void
1763hsw_vebox_irq_disable(struct intel_engine_cs *engine)
1764{
1765 struct drm_i915_private *dev_priv = engine->i915;
1766
1767 I915_WRITE_IMR(engine, ~0);
1768 gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
1769}
1770
1771static void
1772gen8_irq_enable(struct intel_engine_cs *engine)
1773{
1774 struct drm_i915_private *dev_priv = engine->i915;
1775
Chris Wilson61ff75a2016-07-01 17:23:28 +01001776 I915_WRITE_IMR(engine,
1777 ~(engine->irq_enable_mask |
1778 engine->irq_keep_mask));
Chris Wilson31bb59c2016-07-01 17:23:27 +01001779 POSTING_READ_FW(RING_IMR(engine->mmio_base));
1780}
1781
1782static void
1783gen8_irq_disable(struct intel_engine_cs *engine)
1784{
1785 struct drm_i915_private *dev_priv = engine->i915;
1786
Chris Wilson61ff75a2016-07-01 17:23:28 +01001787 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Ben Widawskyabd58f02013-11-02 21:07:09 -07001788}
1789
Zou Nan haid1b851f2010-05-21 09:08:57 +08001790static int
John Harrison53fddaf2015-05-29 17:44:02 +01001791i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001792 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001793 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001794{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001795 struct intel_engine_cs *engine = req->engine;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001796 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001797
John Harrison5fb9de12015-05-29 17:44:07 +01001798 ret = intel_ring_begin(req, 2);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001799 if (ret)
1800 return ret;
1801
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001802 intel_ring_emit(engine,
Chris Wilson65f56872012-04-17 16:38:12 +01001803 MI_BATCH_BUFFER_START |
1804 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001805 (dispatch_flags & I915_DISPATCH_SECURE ?
1806 0 : MI_BATCH_NON_SECURE_I965));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001807 intel_ring_emit(engine, offset);
1808 intel_ring_advance(engine);
Chris Wilson78501ea2010-10-27 12:18:21 +01001809
Zou Nan haid1b851f2010-05-21 09:08:57 +08001810 return 0;
1811}
1812
Daniel Vetterb45305f2012-12-17 16:21:27 +01001813/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1814#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001815#define I830_TLB_ENTRIES (2)
1816#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001817static int
John Harrison53fddaf2015-05-29 17:44:02 +01001818i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001819 u64 offset, u32 len,
1820 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001821{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001822 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001823 u32 cs_offset = engine->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001824 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001825
John Harrison5fb9de12015-05-29 17:44:07 +01001826 ret = intel_ring_begin(req, 6);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001827 if (ret)
1828 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001829
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001830 /* Evict the invalid PTE TLBs */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001831 intel_ring_emit(engine, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1832 intel_ring_emit(engine, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1833 intel_ring_emit(engine, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1834 intel_ring_emit(engine, cs_offset);
1835 intel_ring_emit(engine, 0xdeadbeef);
1836 intel_ring_emit(engine, MI_NOOP);
1837 intel_ring_advance(engine);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001838
John Harrison8e004ef2015-02-13 11:48:10 +00001839 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001840 if (len > I830_BATCH_LIMIT)
1841 return -ENOSPC;
1842
John Harrison5fb9de12015-05-29 17:44:07 +01001843 ret = intel_ring_begin(req, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001844 if (ret)
1845 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001846
1847 /* Blit the batch (which has now all relocs applied) to the
1848 * stable batch scratch bo area (so that the CS never
1849 * stumbles over its tlb invalidation bug) ...
1850 */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001851 intel_ring_emit(engine, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1852 intel_ring_emit(engine,
1853 BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
1854 intel_ring_emit(engine, DIV_ROUND_UP(len, 4096) << 16 | 4096);
1855 intel_ring_emit(engine, cs_offset);
1856 intel_ring_emit(engine, 4096);
1857 intel_ring_emit(engine, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001858
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001859 intel_ring_emit(engine, MI_FLUSH);
1860 intel_ring_emit(engine, MI_NOOP);
1861 intel_ring_advance(engine);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001862
1863 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001864 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001865 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001866
Ville Syrjälä9d611c02015-12-14 18:23:49 +02001867 ret = intel_ring_begin(req, 2);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001868 if (ret)
1869 return ret;
1870
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001871 intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1872 intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1873 0 : MI_BATCH_NON_SECURE));
1874 intel_ring_advance(engine);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001875
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001876 return 0;
1877}
1878
1879static int
John Harrison53fddaf2015-05-29 17:44:02 +01001880i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001881 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001882 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001883{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001884 struct intel_engine_cs *engine = req->engine;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001885 int ret;
1886
John Harrison5fb9de12015-05-29 17:44:07 +01001887 ret = intel_ring_begin(req, 2);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001888 if (ret)
1889 return ret;
1890
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001891 intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
1892 intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1893 0 : MI_BATCH_NON_SECURE));
1894 intel_ring_advance(engine);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001895
Eric Anholt62fdfea2010-05-21 13:26:39 -07001896 return 0;
1897}
1898
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001899static void cleanup_phys_status_page(struct intel_engine_cs *engine)
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001900{
Chris Wilsonc0336662016-05-06 15:40:21 +01001901 struct drm_i915_private *dev_priv = engine->i915;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001902
1903 if (!dev_priv->status_page_dmah)
1904 return;
1905
Chris Wilson91c8a322016-07-05 10:40:23 +01001906 drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001907 engine->status_page.page_addr = NULL;
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001908}
1909
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001910static void cleanup_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001911{
Chris Wilson05394f32010-11-08 19:18:58 +00001912 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001913
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001914 obj = engine->status_page.obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001915 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001916 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001917
Chris Wilson9da3da62012-06-01 15:20:22 +01001918 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001919 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001920 drm_gem_object_unreference(&obj->base);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001921 engine->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001922}
1923
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001924static int init_status_page(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001925{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001926 struct drm_i915_gem_object *obj = engine->status_page.obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001927
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02001928 if (obj == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001929 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001930 int ret;
1931
Chris Wilson91c8a322016-07-05 10:40:23 +01001932 obj = i915_gem_object_create(&engine->i915->drm, 4096);
Chris Wilsonfe3db792016-04-25 13:32:13 +01001933 if (IS_ERR(obj)) {
Chris Wilsone3efda42014-04-09 09:19:41 +01001934 DRM_ERROR("Failed to allocate status page\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01001935 return PTR_ERR(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01001936 }
1937
1938 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1939 if (ret)
1940 goto err_unref;
1941
Chris Wilson1f767e02014-07-03 17:33:03 -04001942 flags = 0;
Chris Wilsonc0336662016-05-06 15:40:21 +01001943 if (!HAS_LLC(engine->i915))
Chris Wilson1f767e02014-07-03 17:33:03 -04001944 /* On g33, we cannot place HWS above 256MiB, so
1945 * restrict its pinning to the low mappable arena.
1946 * Though this restriction is not documented for
1947 * gen4, gen5, or byt, they also behave similarly
1948 * and hang if the HWS is placed at the top of the
1949 * GTT. To generalise, it appears that all !llc
1950 * platforms have issues with us placing the HWS
1951 * above the mappable region (even though we never
1952 * actualy map it).
1953 */
1954 flags |= PIN_MAPPABLE;
1955 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001956 if (ret) {
1957err_unref:
1958 drm_gem_object_unreference(&obj->base);
1959 return ret;
1960 }
1961
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001962 engine->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001963 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001964
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001965 engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
1966 engine->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
1967 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001968
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001969 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001970 engine->name, engine->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001971
1972 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001973}
1974
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001975static int init_phys_status_page(struct intel_engine_cs *engine)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001976{
Chris Wilsonc0336662016-05-06 15:40:21 +01001977 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001978
1979 if (!dev_priv->status_page_dmah) {
1980 dev_priv->status_page_dmah =
Chris Wilson91c8a322016-07-05 10:40:23 +01001981 drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001982 if (!dev_priv->status_page_dmah)
1983 return -ENOMEM;
1984 }
1985
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001986 engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1987 memset(engine->status_page.page_addr, 0, PAGE_SIZE);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001988
1989 return 0;
1990}
1991
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001992void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1993{
Chris Wilson3d77e9b2016-04-28 09:56:40 +01001994 GEM_BUG_ON(ringbuf->vma == NULL);
1995 GEM_BUG_ON(ringbuf->virtual_start == NULL);
1996
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001997 if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
Chris Wilson0a798eb2016-04-08 12:11:11 +01001998 i915_gem_object_unpin_map(ringbuf->obj);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01001999 else
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002000 i915_vma_unpin_iomap(ringbuf->vma);
Dave Gordon83052162016-04-12 14:46:16 +01002001 ringbuf->virtual_start = NULL;
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002002
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002003 i915_gem_object_ggtt_unpin(ringbuf->obj);
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002004 ringbuf->vma = NULL;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002005}
2006
Chris Wilsonc0336662016-05-06 15:40:21 +01002007int intel_pin_and_map_ringbuffer_obj(struct drm_i915_private *dev_priv,
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002008 struct intel_ringbuffer *ringbuf)
2009{
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002010 struct drm_i915_gem_object *obj = ringbuf->obj;
Chris Wilsona687a432016-04-13 17:35:11 +01002011 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
2012 unsigned flags = PIN_OFFSET_BIAS | 4096;
Dave Gordon83052162016-04-12 14:46:16 +01002013 void *addr;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002014 int ret;
2015
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002016 if (HAS_LLC(dev_priv) && !obj->stolen) {
Chris Wilsona687a432016-04-13 17:35:11 +01002017 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002018 if (ret)
2019 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002020
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002021 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsond2cad532016-04-08 12:11:10 +01002022 if (ret)
2023 goto err_unpin;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002024
Dave Gordon83052162016-04-12 14:46:16 +01002025 addr = i915_gem_object_pin_map(obj);
2026 if (IS_ERR(addr)) {
2027 ret = PTR_ERR(addr);
Chris Wilsond2cad532016-04-08 12:11:10 +01002028 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002029 }
2030 } else {
Chris Wilsona687a432016-04-13 17:35:11 +01002031 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
2032 flags | PIN_MAPPABLE);
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002033 if (ret)
2034 return ret;
2035
2036 ret = i915_gem_object_set_to_gtt_domain(obj, true);
Chris Wilsond2cad532016-04-08 12:11:10 +01002037 if (ret)
2038 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002039
Daniele Ceraolo Spurioff3dc082016-01-27 15:43:49 +00002040 /* Access through the GTT requires the device to be awake. */
2041 assert_rpm_wakelock_held(dev_priv);
2042
Chris Wilson3d77e9b2016-04-28 09:56:40 +01002043 addr = i915_vma_pin_iomap(i915_gem_obj_to_ggtt(obj));
2044 if (IS_ERR(addr)) {
2045 ret = PTR_ERR(addr);
Chris Wilsond2cad532016-04-08 12:11:10 +01002046 goto err_unpin;
Chris Wilsondef0c5f2015-10-08 13:39:54 +01002047 }
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002048 }
2049
Dave Gordon83052162016-04-12 14:46:16 +01002050 ringbuf->virtual_start = addr;
Tvrtko Ursulin0eb973d2016-01-15 15:10:28 +00002051 ringbuf->vma = i915_gem_obj_to_ggtt(obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002052 return 0;
Chris Wilsond2cad532016-04-08 12:11:10 +01002053
2054err_unpin:
2055 i915_gem_object_ggtt_unpin(obj);
2056 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002057}
2058
Chris Wilson01101fa2015-09-03 13:01:39 +01002059static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01002060{
Oscar Mateo2919d292014-07-03 16:28:02 +01002061 drm_gem_object_unreference(&ringbuf->obj->base);
2062 ringbuf->obj = NULL;
2063}
2064
Chris Wilson01101fa2015-09-03 13:01:39 +01002065static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
2066 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01002067{
Chris Wilsone3efda42014-04-09 09:19:41 +01002068 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002069
2070 obj = NULL;
2071 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002072 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002073 if (obj == NULL)
Dave Gordond37cd8a2016-04-22 19:14:32 +01002074 obj = i915_gem_object_create(dev, ringbuf->size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002075 if (IS_ERR(obj))
2076 return PTR_ERR(obj);
Chris Wilsone3efda42014-04-09 09:19:41 +01002077
Akash Goel24f3a8c2014-06-17 10:59:42 +05302078 /* mark ring buffers as read-only from GPU side by default */
2079 obj->gt_ro = 1;
2080
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002081 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002082
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002083 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002084}
2085
Chris Wilson01101fa2015-09-03 13:01:39 +01002086struct intel_ringbuffer *
2087intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
2088{
2089 struct intel_ringbuffer *ring;
2090 int ret;
2091
2092 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
Chris Wilson608c1a52015-09-03 13:01:40 +01002093 if (ring == NULL) {
2094 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2095 engine->name);
Chris Wilson01101fa2015-09-03 13:01:39 +01002096 return ERR_PTR(-ENOMEM);
Chris Wilson608c1a52015-09-03 13:01:40 +01002097 }
Chris Wilson01101fa2015-09-03 13:01:39 +01002098
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002099 ring->engine = engine;
Chris Wilson608c1a52015-09-03 13:01:40 +01002100 list_add(&ring->link, &engine->buffers);
Chris Wilson01101fa2015-09-03 13:01:39 +01002101
2102 ring->size = size;
2103 /* Workaround an erratum on the i830 which causes a hang if
2104 * the TAIL pointer points to within the last 2 cachelines
2105 * of the buffer.
2106 */
2107 ring->effective_size = size;
Chris Wilsonc0336662016-05-06 15:40:21 +01002108 if (IS_I830(engine->i915) || IS_845G(engine->i915))
Chris Wilson01101fa2015-09-03 13:01:39 +01002109 ring->effective_size -= 2 * CACHELINE_BYTES;
2110
2111 ring->last_retired_head = -1;
2112 intel_ring_update_space(ring);
2113
Chris Wilson91c8a322016-07-05 10:40:23 +01002114 ret = intel_alloc_ringbuffer_obj(&engine->i915->drm, ring);
Chris Wilson01101fa2015-09-03 13:01:39 +01002115 if (ret) {
Chris Wilson608c1a52015-09-03 13:01:40 +01002116 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
2117 engine->name, ret);
2118 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002119 kfree(ring);
2120 return ERR_PTR(ret);
2121 }
2122
2123 return ring;
2124}
2125
2126void
2127intel_ringbuffer_free(struct intel_ringbuffer *ring)
2128{
2129 intel_destroy_ringbuffer_obj(ring);
Chris Wilson608c1a52015-09-03 13:01:40 +01002130 list_del(&ring->link);
Chris Wilson01101fa2015-09-03 13:01:39 +01002131 kfree(ring);
2132}
2133
Chris Wilson0cb26a82016-06-24 14:55:53 +01002134static int intel_ring_context_pin(struct i915_gem_context *ctx,
2135 struct intel_engine_cs *engine)
2136{
2137 struct intel_context *ce = &ctx->engine[engine->id];
2138 int ret;
2139
Chris Wilson91c8a322016-07-05 10:40:23 +01002140 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002141
2142 if (ce->pin_count++)
2143 return 0;
2144
2145 if (ce->state) {
2146 ret = i915_gem_obj_ggtt_pin(ce->state, ctx->ggtt_alignment, 0);
2147 if (ret)
2148 goto error;
2149 }
2150
Chris Wilsonc7c3c072016-06-24 14:55:54 +01002151 /* The kernel context is only used as a placeholder for flushing the
2152 * active context. It is never used for submitting user rendering and
2153 * as such never requires the golden render context, and so we can skip
2154 * emitting it when we switch to the kernel context. This is required
2155 * as during eviction we cannot allocate and pin the renderstate in
2156 * order to initialise the context.
2157 */
2158 if (ctx == ctx->i915->kernel_context)
2159 ce->initialised = true;
2160
Chris Wilson0cb26a82016-06-24 14:55:53 +01002161 i915_gem_context_reference(ctx);
2162 return 0;
2163
2164error:
2165 ce->pin_count = 0;
2166 return ret;
2167}
2168
2169static void intel_ring_context_unpin(struct i915_gem_context *ctx,
2170 struct intel_engine_cs *engine)
2171{
2172 struct intel_context *ce = &ctx->engine[engine->id];
2173
Chris Wilson91c8a322016-07-05 10:40:23 +01002174 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002175
2176 if (--ce->pin_count)
2177 return;
2178
2179 if (ce->state)
2180 i915_gem_object_ggtt_unpin(ce->state);
2181
2182 i915_gem_context_unreference(ctx);
2183}
2184
Ben Widawskyc43b5632012-04-16 14:07:40 -07002185static int intel_init_ring_buffer(struct drm_device *dev,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002186 struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002187{
Chris Wilsonc0336662016-05-06 15:40:21 +01002188 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002189 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002190 int ret;
2191
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002192 WARN_ON(engine->buffer);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002193
Chris Wilsonc0336662016-05-06 15:40:21 +01002194 engine->i915 = dev_priv;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002195 INIT_LIST_HEAD(&engine->active_list);
2196 INIT_LIST_HEAD(&engine->request_list);
2197 INIT_LIST_HEAD(&engine->execlist_queue);
2198 INIT_LIST_HEAD(&engine->buffers);
2199 i915_gem_batch_pool_init(dev, &engine->batch_pool);
2200 memset(engine->semaphore.sync_seqno, 0,
2201 sizeof(engine->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002202
Chris Wilson688e6c72016-07-01 17:23:15 +01002203 ret = intel_engine_init_breadcrumbs(engine);
2204 if (ret)
2205 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002206
Chris Wilson0cb26a82016-06-24 14:55:53 +01002207 /* We may need to do things with the shrinker which
2208 * require us to immediately switch back to the default
2209 * context. This can cause a problem as pinning the
2210 * default context also requires GTT space which may not
2211 * be available. To avoid this we always pin the default
2212 * context.
2213 */
2214 ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
2215 if (ret)
2216 goto error;
2217
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002218 ringbuf = intel_engine_create_ringbuffer(engine, 32 * PAGE_SIZE);
Dave Gordonb0366a52015-12-08 15:02:36 +00002219 if (IS_ERR(ringbuf)) {
2220 ret = PTR_ERR(ringbuf);
2221 goto error;
2222 }
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002223 engine->buffer = ringbuf;
Chris Wilson01101fa2015-09-03 13:01:39 +01002224
Chris Wilsonc0336662016-05-06 15:40:21 +01002225 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002226 ret = init_status_page(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002227 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002228 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002229 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002230 WARN_ON(engine->id != RCS);
2231 ret = init_phys_status_page(engine);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002232 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002233 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002234 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002235
Chris Wilsonc0336662016-05-06 15:40:21 +01002236 ret = intel_pin_and_map_ringbuffer_obj(dev_priv, ringbuf);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002237 if (ret) {
2238 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002239 engine->name, ret);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002240 intel_destroy_ringbuffer_obj(ringbuf);
2241 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002242 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002243
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002244 ret = i915_cmd_parser_init_ring(engine);
Brad Volkin44e895a2014-05-10 14:10:43 -07002245 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002246 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002247
Oscar Mateo8ee14972014-05-22 14:13:34 +01002248 return 0;
2249
2250error:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002251 intel_cleanup_engine(engine);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002252 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002253}
2254
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002255void intel_cleanup_engine(struct intel_engine_cs *engine)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002256{
John Harrison6402c332014-10-31 12:00:26 +00002257 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01002258
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002259 if (!intel_engine_initialized(engine))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002260 return;
2261
Chris Wilsonc0336662016-05-06 15:40:21 +01002262 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00002263
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002264 if (engine->buffer) {
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002265 intel_stop_engine(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01002266 WARN_ON(!IS_GEN2(dev_priv) && (I915_READ_MODE(engine) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002267
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002268 intel_unpin_ringbuffer_obj(engine->buffer);
2269 intel_ringbuffer_free(engine->buffer);
2270 engine->buffer = NULL;
Dave Gordonb0366a52015-12-08 15:02:36 +00002271 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002272
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002273 if (engine->cleanup)
2274 engine->cleanup(engine);
Zou Nan hai8d192152010-11-02 16:31:01 +08002275
Chris Wilsonc0336662016-05-06 15:40:21 +01002276 if (I915_NEED_GFX_HWS(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002277 cleanup_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002278 } else {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002279 WARN_ON(engine->id != RCS);
2280 cleanup_phys_status_page(engine);
Ville Syrjälä7d3fdff2016-01-11 20:48:32 +02002281 }
Brad Volkin44e895a2014-05-10 14:10:43 -07002282
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002283 i915_cmd_parser_fini_ring(engine);
2284 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson688e6c72016-07-01 17:23:15 +01002285 intel_engine_fini_breadcrumbs(engine);
Chris Wilson0cb26a82016-06-24 14:55:53 +01002286
2287 intel_ring_context_unpin(dev_priv->kernel_context, engine);
2288
Chris Wilsonc0336662016-05-06 15:40:21 +01002289 engine->i915 = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002290}
2291
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002292int intel_engine_idle(struct intel_engine_cs *engine)
Chris Wilson3e960502012-11-27 16:22:54 +00002293{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002294 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002295
Chris Wilson3e960502012-11-27 16:22:54 +00002296 /* Wait upon the last request to be completed */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002297 if (list_empty(&engine->request_list))
Chris Wilson3e960502012-11-27 16:22:54 +00002298 return 0;
2299
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002300 req = list_entry(engine->request_list.prev,
2301 struct drm_i915_gem_request,
2302 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002303
Chris Wilsonb4716182015-04-27 13:41:17 +01002304 /* Make sure we do not trigger any retires */
2305 return __i915_wait_request(req,
Chris Wilsonc19ae982016-04-13 17:35:03 +01002306 req->i915->mm.interruptible,
Chris Wilsonb4716182015-04-27 13:41:17 +01002307 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002308}
2309
John Harrison6689cb22015-03-19 12:30:08 +00002310int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002311{
Chris Wilson63103462016-04-28 09:56:49 +01002312 int ret;
2313
2314 /* Flush enough space to reduce the likelihood of waiting after
2315 * we start building the request - in which case we will just
2316 * have to repeat work.
2317 */
Chris Wilsona0442462016-04-29 09:07:05 +01002318 request->reserved_space += LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002319
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002320 request->ringbuf = request->engine->buffer;
Chris Wilson63103462016-04-28 09:56:49 +01002321
2322 ret = intel_ring_begin(request, 0);
2323 if (ret)
2324 return ret;
2325
Chris Wilsona0442462016-04-29 09:07:05 +01002326 request->reserved_space -= LEGACY_REQUEST_SIZE;
Chris Wilson63103462016-04-28 09:56:49 +01002327 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002328}
2329
Chris Wilson987046a2016-04-28 09:56:46 +01002330static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002331{
Chris Wilson987046a2016-04-28 09:56:46 +01002332 struct intel_ringbuffer *ringbuf = req->ringbuf;
2333 struct intel_engine_cs *engine = req->engine;
2334 struct drm_i915_gem_request *target;
2335
2336 intel_ring_update_space(ringbuf);
2337 if (ringbuf->space >= bytes)
2338 return 0;
2339
2340 /*
2341 * Space is reserved in the ringbuffer for finalising the request,
2342 * as that cannot be allowed to fail. During request finalisation,
2343 * reserved_space is set to 0 to stop the overallocation and the
2344 * assumption is that then we never need to wait (which has the
2345 * risk of failing with EINTR).
2346 *
2347 * See also i915_gem_request_alloc() and i915_add_request().
2348 */
Chris Wilson0251a962016-04-28 09:56:47 +01002349 GEM_BUG_ON(!req->reserved_space);
Chris Wilson987046a2016-04-28 09:56:46 +01002350
2351 list_for_each_entry(target, &engine->request_list, list) {
2352 unsigned space;
2353
2354 /*
2355 * The request queue is per-engine, so can contain requests
2356 * from multiple ringbuffers. Here, we must ignore any that
2357 * aren't from the ringbuffer we're considering.
2358 */
2359 if (target->ringbuf != ringbuf)
2360 continue;
2361
2362 /* Would completion of this request free enough space? */
2363 space = __intel_ring_space(target->postfix, ringbuf->tail,
2364 ringbuf->size);
2365 if (space >= bytes)
2366 break;
2367 }
2368
2369 if (WARN_ON(&target->list == &engine->request_list))
2370 return -ENOSPC;
2371
2372 return i915_wait_request(target);
2373}
2374
2375int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
2376{
2377 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison79bbcc22015-06-30 12:40:55 +01002378 int remain_actual = ringbuf->size - ringbuf->tail;
Chris Wilson987046a2016-04-28 09:56:46 +01002379 int remain_usable = ringbuf->effective_size - ringbuf->tail;
2380 int bytes = num_dwords * sizeof(u32);
2381 int total_bytes, wait_bytes;
John Harrison79bbcc22015-06-30 12:40:55 +01002382 bool need_wrap = false;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002383
Chris Wilson0251a962016-04-28 09:56:47 +01002384 total_bytes = bytes + req->reserved_space;
John Harrison29b1b412015-06-18 13:10:09 +01002385
John Harrison79bbcc22015-06-30 12:40:55 +01002386 if (unlikely(bytes > remain_usable)) {
2387 /*
2388 * Not enough space for the basic request. So need to flush
2389 * out the remainder and then wait for base + reserved.
2390 */
2391 wait_bytes = remain_actual + total_bytes;
2392 need_wrap = true;
Chris Wilson987046a2016-04-28 09:56:46 +01002393 } else if (unlikely(total_bytes > remain_usable)) {
2394 /*
2395 * The base request will fit but the reserved space
2396 * falls off the end. So we don't need an immediate wrap
2397 * and only need to effectively wait for the reserved
2398 * size space from the start of ringbuffer.
2399 */
Chris Wilson0251a962016-04-28 09:56:47 +01002400 wait_bytes = remain_actual + req->reserved_space;
John Harrison79bbcc22015-06-30 12:40:55 +01002401 } else {
Chris Wilson987046a2016-04-28 09:56:46 +01002402 /* No wrapping required, just waiting. */
2403 wait_bytes = total_bytes;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002404 }
2405
Chris Wilson987046a2016-04-28 09:56:46 +01002406 if (wait_bytes > ringbuf->space) {
2407 int ret = wait_for_space(req, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002408 if (unlikely(ret))
2409 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +01002410
Chris Wilson987046a2016-04-28 09:56:46 +01002411 intel_ring_update_space(ringbuf);
Chris Wilsone075a322016-05-13 11:57:22 +01002412 if (unlikely(ringbuf->space < wait_bytes))
2413 return -EAGAIN;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002414 }
2415
Chris Wilson987046a2016-04-28 09:56:46 +01002416 if (unlikely(need_wrap)) {
2417 GEM_BUG_ON(remain_actual > ringbuf->space);
2418 GEM_BUG_ON(ringbuf->tail + remain_actual > ringbuf->size);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002419
Chris Wilson987046a2016-04-28 09:56:46 +01002420 /* Fill the tail with MI_NOOP */
2421 memset(ringbuf->virtual_start + ringbuf->tail,
2422 0, remain_actual);
2423 ringbuf->tail = 0;
2424 ringbuf->space -= remain_actual;
2425 }
Chris Wilson78501ea2010-10-27 12:18:21 +01002426
Chris Wilson987046a2016-04-28 09:56:46 +01002427 ringbuf->space -= bytes;
2428 GEM_BUG_ON(ringbuf->space < 0);
Chris Wilson304d6952014-01-02 14:32:35 +00002429 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002430}
2431
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002432/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01002433int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002434{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002435 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002436 int num_dwords = (engine->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002437 int ret;
2438
2439 if (num_dwords == 0)
2440 return 0;
2441
Chris Wilson18393f62014-04-09 09:19:40 +01002442 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
John Harrison5fb9de12015-05-29 17:44:07 +01002443 ret = intel_ring_begin(req, num_dwords);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002444 if (ret)
2445 return ret;
2446
2447 while (num_dwords--)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002448 intel_ring_emit(engine, MI_NOOP);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002449
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002450 intel_ring_advance(engine);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002451
2452 return 0;
2453}
2454
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002455void intel_ring_init_seqno(struct intel_engine_cs *engine, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002456{
Chris Wilsonc0336662016-05-06 15:40:21 +01002457 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002458
Chris Wilson29dcb572016-04-07 07:29:13 +01002459 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
2460 * so long as the semaphore value in the register/page is greater
2461 * than the sync value), so whenever we reset the seqno,
2462 * so long as we reset the tracking semaphore value to 0, it will
2463 * always be before the next request's seqno. If we don't reset
2464 * the semaphore value, then when the seqno moves backwards all
2465 * future waits will complete instantly (causing rendering corruption).
2466 */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01002467 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002468 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
2469 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
Chris Wilsond04bce42016-04-07 07:29:12 +01002470 if (HAS_VEBOX(dev_priv))
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002471 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002472 }
Chris Wilsona058d932016-04-07 07:29:15 +01002473 if (dev_priv->semaphore_obj) {
2474 struct drm_i915_gem_object *obj = dev_priv->semaphore_obj;
2475 struct page *page = i915_gem_object_get_dirty_page(obj, 0);
2476 void *semaphores = kmap(page);
2477 memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
2478 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
2479 kunmap(page);
2480 }
Chris Wilson29dcb572016-04-07 07:29:13 +01002481 memset(engine->semaphore.sync_seqno, 0,
2482 sizeof(engine->semaphore.sync_seqno));
Chris Wilson297b0c52010-10-22 17:02:41 +01002483
Chris Wilson1b7744e2016-07-01 17:23:17 +01002484 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
2485 if (engine->irq_seqno_barrier)
2486 engine->irq_seqno_barrier(engine);
Chris Wilson01347122016-04-07 07:29:16 +01002487 engine->last_submitted_seqno = seqno;
Chris Wilson29dcb572016-04-07 07:29:13 +01002488
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002489 engine->hangcheck.seqno = seqno;
Chris Wilson688e6c72016-07-01 17:23:15 +01002490
2491 /* After manually advancing the seqno, fake the interrupt in case
2492 * there are any waiters for that seqno.
2493 */
2494 rcu_read_lock();
2495 intel_engine_wakeup(engine);
2496 rcu_read_unlock();
Chris Wilson549f7362010-10-19 11:19:32 +01002497}
2498
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002499static void gen6_bsd_ring_write_tail(struct intel_engine_cs *engine,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002500 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002501{
Chris Wilsonc0336662016-05-06 15:40:21 +01002502 struct drm_i915_private *dev_priv = engine->i915;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002503
Chris Wilson76f84212016-06-30 15:33:45 +01002504 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2505
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002506 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002507
Chris Wilson12f55812012-07-05 17:14:01 +01002508 /* Disable notification that the ring is IDLE. The GT
2509 * will then assume that it is busy and bring it out of rc6.
2510 */
Chris Wilson76f84212016-06-30 15:33:45 +01002511 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2512 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Chris Wilson12f55812012-07-05 17:14:01 +01002513
2514 /* Clear the context id. Here be magic! */
Chris Wilson76f84212016-06-30 15:33:45 +01002515 I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
Chris Wilson12f55812012-07-05 17:14:01 +01002516
2517 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Chris Wilson76f84212016-06-30 15:33:45 +01002518 if (intel_wait_for_register_fw(dev_priv,
2519 GEN6_BSD_SLEEP_PSMI_CONTROL,
2520 GEN6_BSD_SLEEP_INDICATOR,
2521 0,
2522 50))
Chris Wilson12f55812012-07-05 17:14:01 +01002523 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002524
Chris Wilson12f55812012-07-05 17:14:01 +01002525 /* Now that the ring is fully powered up, update the tail */
Chris Wilson76f84212016-06-30 15:33:45 +01002526 I915_WRITE_FW(RING_TAIL(engine->mmio_base), value);
2527 POSTING_READ_FW(RING_TAIL(engine->mmio_base));
Chris Wilson12f55812012-07-05 17:14:01 +01002528
2529 /* Let the ring send IDLE messages to the GT again,
2530 * and so let it sleep to conserve power when idle.
2531 */
Chris Wilson76f84212016-06-30 15:33:45 +01002532 I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
2533 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2534
2535 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002536}
2537
John Harrisona84c3ae2015-05-29 17:43:57 +01002538static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002539 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002540{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002541 struct intel_engine_cs *engine = req->engine;
Chris Wilson71a77e02011-02-02 12:13:49 +00002542 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002543 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002544
John Harrison5fb9de12015-05-29 17:44:07 +01002545 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002546 if (ret)
2547 return ret;
2548
Chris Wilson71a77e02011-02-02 12:13:49 +00002549 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002550 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002551 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002552
2553 /* We always require a command barrier so that subsequent
2554 * commands, such as breadcrumb interrupts, are strictly ordered
2555 * wrt the contents of the write cache being flushed to memory
2556 * (and thus being coherent from the CPU).
2557 */
2558 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2559
Jesse Barnes9a289772012-10-26 09:42:42 -07002560 /*
2561 * Bspec vol 1c.5 - video engine command streamer:
2562 * "If ENABLED, all TLBs will be invalidated once the flush
2563 * operation is complete. This bit is only valid when the
2564 * Post-Sync Operation field is a value of 1h or 3h."
2565 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002566 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002567 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2568
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002569 intel_ring_emit(engine, cmd);
2570 intel_ring_emit(engine,
2571 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002572 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002573 intel_ring_emit(engine, 0); /* upper addr */
2574 intel_ring_emit(engine, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002575 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002576 intel_ring_emit(engine, 0);
2577 intel_ring_emit(engine, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002578 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002579 intel_ring_advance(engine);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002580 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002581}
2582
2583static int
John Harrison53fddaf2015-05-29 17:44:02 +01002584gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002585 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002586 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002587{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002588 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002589 bool ppgtt = USES_PPGTT(engine->dev) &&
John Harrison8e004ef2015-02-13 11:48:10 +00002590 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002591 int ret;
2592
John Harrison5fb9de12015-05-29 17:44:07 +01002593 ret = intel_ring_begin(req, 4);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002594 if (ret)
2595 return ret;
2596
2597 /* FIXME(BDW): Address space and security selectors. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002598 intel_ring_emit(engine, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002599 (dispatch_flags & I915_DISPATCH_RS ?
2600 MI_BATCH_RESOURCE_STREAMER : 0));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002601 intel_ring_emit(engine, lower_32_bits(offset));
2602 intel_ring_emit(engine, upper_32_bits(offset));
2603 intel_ring_emit(engine, MI_NOOP);
2604 intel_ring_advance(engine);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002605
2606 return 0;
2607}
2608
2609static int
John Harrison53fddaf2015-05-29 17:44:02 +01002610hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002611 u64 offset, u32 len,
2612 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002613{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002614 struct intel_engine_cs *engine = req->engine;
Akshay Joshi0206e352011-08-16 15:34:10 -04002615 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002616
John Harrison5fb9de12015-05-29 17:44:07 +01002617 ret = intel_ring_begin(req, 2);
Akshay Joshi0206e352011-08-16 15:34:10 -04002618 if (ret)
2619 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002620
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002621 intel_ring_emit(engine,
Chris Wilson77072252014-09-10 12:18:27 +01002622 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002623 (dispatch_flags & I915_DISPATCH_SECURE ?
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002624 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2625 (dispatch_flags & I915_DISPATCH_RS ?
2626 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002627 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002628 intel_ring_emit(engine, offset);
2629 intel_ring_advance(engine);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002630
2631 return 0;
2632}
2633
2634static int
John Harrison53fddaf2015-05-29 17:44:02 +01002635gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002636 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002637 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002638{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002639 struct intel_engine_cs *engine = req->engine;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002640 int ret;
2641
John Harrison5fb9de12015-05-29 17:44:07 +01002642 ret = intel_ring_begin(req, 2);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002643 if (ret)
2644 return ret;
2645
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002646 intel_ring_emit(engine,
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002647 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002648 (dispatch_flags & I915_DISPATCH_SECURE ?
2649 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002650 /* bit0-7 is the length on GEN6+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002651 intel_ring_emit(engine, offset);
2652 intel_ring_advance(engine);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002653
Akshay Joshi0206e352011-08-16 15:34:10 -04002654 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002655}
2656
Chris Wilson549f7362010-10-19 11:19:32 +01002657/* Blitter support (SandyBridge+) */
2658
John Harrisona84c3ae2015-05-29 17:43:57 +01002659static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002660 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002661{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002662 struct intel_engine_cs *engine = req->engine;
Chris Wilson71a77e02011-02-02 12:13:49 +00002663 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002664 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002665
John Harrison5fb9de12015-05-29 17:44:07 +01002666 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002667 if (ret)
2668 return ret;
2669
Chris Wilson71a77e02011-02-02 12:13:49 +00002670 cmd = MI_FLUSH_DW;
Chris Wilsonc0336662016-05-06 15:40:21 +01002671 if (INTEL_GEN(req->i915) >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002672 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002673
2674 /* We always require a command barrier so that subsequent
2675 * commands, such as breadcrumb interrupts, are strictly ordered
2676 * wrt the contents of the write cache being flushed to memory
2677 * (and thus being coherent from the CPU).
2678 */
2679 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2680
Jesse Barnes9a289772012-10-26 09:42:42 -07002681 /*
2682 * Bspec vol 1c.3 - blitter engine command streamer:
2683 * "If ENABLED, all TLBs will be invalidated once the flush
2684 * operation is complete. This bit is only valid when the
2685 * Post-Sync Operation field is a value of 1h or 3h."
2686 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002687 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002688 cmd |= MI_INVALIDATE_TLB;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002689 intel_ring_emit(engine, cmd);
2690 intel_ring_emit(engine,
2691 I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Chris Wilsonc0336662016-05-06 15:40:21 +01002692 if (INTEL_GEN(req->i915) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002693 intel_ring_emit(engine, 0); /* upper addr */
2694 intel_ring_emit(engine, 0); /* value */
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002695 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002696 intel_ring_emit(engine, 0);
2697 intel_ring_emit(engine, MI_NOOP);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002698 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002699 intel_ring_advance(engine);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002700
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002701 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002702}
2703
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002704static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
2705 struct intel_engine_cs *engine)
2706{
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002707 struct drm_i915_gem_object *obj;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002708 int ret, i;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002709
2710 if (!i915_semaphore_is_enabled(dev_priv))
2711 return;
2712
2713 if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore_obj) {
Chris Wilson91c8a322016-07-05 10:40:23 +01002714 obj = i915_gem_object_create(&dev_priv->drm, 4096);
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002715 if (IS_ERR(obj)) {
2716 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2717 i915.semaphores = 0;
2718 } else {
2719 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2720 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2721 if (ret != 0) {
2722 drm_gem_object_unreference(&obj->base);
2723 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2724 i915.semaphores = 0;
2725 } else {
2726 dev_priv->semaphore_obj = obj;
2727 }
2728 }
2729 }
2730
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002731 if (!i915_semaphore_is_enabled(dev_priv))
2732 return;
2733
2734 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002735 u64 offset = i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj);
2736
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002737 engine->semaphore.sync_to = gen8_ring_sync;
2738 engine->semaphore.signal = gen8_xcs_signal;
Tvrtko Ursulin1b9e6652016-06-29 16:09:29 +01002739
2740 for (i = 0; i < I915_NUM_ENGINES; i++) {
2741 u64 ring_offset;
2742
2743 if (i != engine->id)
2744 ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
2745 else
2746 ring_offset = MI_SEMAPHORE_SYNC_INVALID;
2747
2748 engine->semaphore.signal_ggtt[i] = ring_offset;
2749 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002750 } else if (INTEL_GEN(dev_priv) >= 6) {
2751 engine->semaphore.sync_to = gen6_ring_sync;
2752 engine->semaphore.signal = gen6_signal;
Tvrtko Ursulin4b8e38a2016-06-29 16:09:31 +01002753
2754 /*
2755 * The current semaphore is only applied on pre-gen8
2756 * platform. And there is no VCS2 ring on the pre-gen8
2757 * platform. So the semaphore between RCS and VCS2 is
2758 * initialized as INVALID. Gen8 will initialize the
2759 * sema between VCS2 and RCS later.
2760 */
2761 for (i = 0; i < I915_NUM_ENGINES; i++) {
2762 static const struct {
2763 u32 wait_mbox;
2764 i915_reg_t mbox_reg;
2765 } sem_data[I915_NUM_ENGINES][I915_NUM_ENGINES] = {
2766 [RCS] = {
2767 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
2768 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
2769 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
2770 },
2771 [VCS] = {
2772 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
2773 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
2774 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
2775 },
2776 [BCS] = {
2777 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
2778 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
2779 [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
2780 },
2781 [VECS] = {
2782 [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
2783 [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
2784 [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
2785 },
2786 };
2787 u32 wait_mbox;
2788 i915_reg_t mbox_reg;
2789
2790 if (i == engine->id || i == VCS2) {
2791 wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
2792 mbox_reg = GEN6_NOSYNC;
2793 } else {
2794 wait_mbox = sem_data[engine->id][i].wait_mbox;
2795 mbox_reg = sem_data[engine->id][i].mbox_reg;
2796 }
2797
2798 engine->semaphore.mbox.wait[i] = wait_mbox;
2799 engine->semaphore.mbox.signal[i] = mbox_reg;
2800 }
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002801 }
2802}
2803
Chris Wilsoned003072016-07-01 09:18:13 +01002804static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
2805 struct intel_engine_cs *engine)
2806{
2807 if (INTEL_GEN(dev_priv) >= 8) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002808 engine->irq_enable = gen8_irq_enable;
2809 engine->irq_disable = gen8_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002810 engine->irq_seqno_barrier = gen6_seqno_barrier;
2811 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002812 engine->irq_enable = gen6_irq_enable;
2813 engine->irq_disable = gen6_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002814 engine->irq_seqno_barrier = gen6_seqno_barrier;
2815 } else if (INTEL_GEN(dev_priv) >= 5) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002816 engine->irq_enable = gen5_irq_enable;
2817 engine->irq_disable = gen5_irq_disable;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002818 engine->irq_seqno_barrier = gen5_seqno_barrier;
Chris Wilsoned003072016-07-01 09:18:13 +01002819 } else if (INTEL_GEN(dev_priv) >= 3) {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002820 engine->irq_enable = i9xx_irq_enable;
2821 engine->irq_disable = i9xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002822 } else {
Chris Wilson31bb59c2016-07-01 17:23:27 +01002823 engine->irq_enable = i8xx_irq_enable;
2824 engine->irq_disable = i8xx_irq_disable;
Chris Wilsoned003072016-07-01 09:18:13 +01002825 }
2826}
2827
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002828static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
2829 struct intel_engine_cs *engine)
2830{
Tvrtko Ursulin1d8a1332016-06-29 16:09:25 +01002831 engine->init_hw = init_ring_common;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002832 engine->write_tail = ring_write_tail;
Tvrtko Ursulin7445a2a2016-06-29 16:09:21 +01002833
Chris Wilson6f7bef72016-07-01 09:18:12 +01002834 engine->add_request = i9xx_add_request;
2835 if (INTEL_GEN(dev_priv) >= 6)
2836 engine->add_request = gen6_add_request;
2837
2838 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002839 engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002840 else if (INTEL_GEN(dev_priv) >= 6)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002841 engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002842 else if (INTEL_GEN(dev_priv) >= 4)
Tvrtko Ursulin960ecaa2016-06-29 17:40:26 +01002843 engine->dispatch_execbuffer = i965_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002844 else if (IS_I830(dev_priv) || IS_845G(dev_priv))
2845 engine->dispatch_execbuffer = i830_dispatch_execbuffer;
2846 else
2847 engine->dispatch_execbuffer = i915_dispatch_execbuffer;
Tvrtko Ursulinb9700322016-06-29 16:09:23 +01002848
Chris Wilsoned003072016-07-01 09:18:13 +01002849 intel_ring_init_irq(dev_priv, engine);
Tvrtko Ursulind9a64612016-06-29 16:09:27 +01002850 intel_ring_init_semaphores(dev_priv, engine);
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002851}
2852
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002853int intel_init_render_ring_buffer(struct drm_device *dev)
2854{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002855 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002856 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002857 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002858
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002859 engine->name = "render ring";
2860 engine->id = RCS;
2861 engine->exec_id = I915_EXEC_RENDER;
Chris Wilson215a7e32016-04-29 13:18:23 +01002862 engine->hw_id = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002863 engine->mmio_base = RENDER_RING_BASE;
Daniel Vetter59465b52012-04-11 22:12:48 +02002864
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002865 intel_ring_default_vfuncs(dev_priv, engine);
2866
Chris Wilsonf8973c22016-07-01 17:23:21 +01002867 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Chris Wilson61ff75a2016-07-01 17:23:28 +01002868 if (HAS_L3_DPF(dev_priv))
2869 engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Chris Wilsonf8973c22016-07-01 17:23:21 +01002870
Chris Wilsonc0336662016-05-06 15:40:21 +01002871 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002872 engine->init_context = intel_rcs_ctx_init;
Chris Wilsona58c01a2016-04-29 13:18:21 +01002873 engine->add_request = gen8_render_add_request;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002874 engine->flush = gen8_render_ring_flush;
Tvrtko Ursulindb3d4012016-06-29 16:09:28 +01002875 if (i915_semaphore_is_enabled(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002876 engine->semaphore.signal = gen8_rcs_signal;
Chris Wilsonc0336662016-05-06 15:40:21 +01002877 } else if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002878 engine->init_context = intel_rcs_ctx_init;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002879 engine->flush = gen7_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002880 if (IS_GEN6(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002881 engine->flush = gen6_render_ring_flush;
Chris Wilsonc0336662016-05-06 15:40:21 +01002882 } else if (IS_GEN5(dev_priv)) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002883 engine->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002884 } else {
Chris Wilsonc0336662016-05-06 15:40:21 +01002885 if (INTEL_GEN(dev_priv) < 4)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002886 engine->flush = gen2_render_ring_flush;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002887 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002888 engine->flush = gen4_render_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002889 engine->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002890 }
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002891
Chris Wilsonc0336662016-05-06 15:40:21 +01002892 if (IS_HASWELL(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002893 engine->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Chris Wilson6f7bef72016-07-01 09:18:12 +01002894
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002895 engine->init_hw = init_render_ring;
2896 engine->cleanup = render_ring_cleanup;
Daniel Vetter59465b52012-04-11 22:12:48 +02002897
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002898 ret = intel_init_ring_buffer(dev, engine);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002899 if (ret)
2900 return ret;
2901
Chris Wilsonf8973c22016-07-01 17:23:21 +01002902 if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilson7d5ea802016-07-01 17:23:20 +01002903 ret = intel_init_pipe_control(engine, 4096);
2904 if (ret)
2905 return ret;
2906 } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
2907 ret = intel_init_pipe_control(engine, I830_WA_SIZE);
Daniel Vetter99be1df2014-11-20 00:33:06 +01002908 if (ret)
2909 return ret;
2910 }
2911
2912 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002913}
2914
2915int intel_init_bsd_ring_buffer(struct drm_device *dev)
2916{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002917 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002918 struct intel_engine_cs *engine = &dev_priv->engine[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002919
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002920 engine->name = "bsd ring";
2921 engine->id = VCS;
2922 engine->exec_id = I915_EXEC_BSD;
Chris Wilson215a7e32016-04-29 13:18:23 +01002923 engine->hw_id = 1;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002924
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002925 intel_ring_default_vfuncs(dev_priv, engine);
2926
Chris Wilsonc0336662016-05-06 15:40:21 +01002927 if (INTEL_GEN(dev_priv) >= 6) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002928 engine->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002929 /* gen6 bsd needs a special wa for tail updates */
Chris Wilsonc0336662016-05-06 15:40:21 +01002930 if (IS_GEN6(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002931 engine->write_tail = gen6_bsd_ring_write_tail;
2932 engine->flush = gen6_bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002933 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002934 engine->irq_enable_mask =
Ben Widawskyabd58f02013-11-02 21:07:09 -07002935 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002936 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002937 engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002938 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002939 engine->mmio_base = BSD_RING_BASE;
2940 engine->flush = bsd_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002941 if (IS_GEN5(dev_priv))
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002942 engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002943 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002944 engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002945 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002946
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002947 return intel_init_ring_buffer(dev, engine);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002948}
Chris Wilson549f7362010-10-19 11:19:32 +01002949
Zhao Yakui845f74a2014-04-17 10:37:37 +08002950/**
Damien Lespiau62659922015-01-29 14:13:40 +00002951 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002952 */
2953int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2954{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002955 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002956 struct intel_engine_cs *engine = &dev_priv->engine[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002957
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002958 engine->name = "bsd2 ring";
2959 engine->id = VCS2;
2960 engine->exec_id = I915_EXEC_BSD;
Chris Wilson215a7e32016-04-29 13:18:23 +01002961 engine->hw_id = 4;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002962 engine->mmio_base = GEN8_BSD2_RING_BASE;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002963
2964 intel_ring_default_vfuncs(dev_priv, engine);
2965
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002966 engine->flush = gen6_bsd_ring_flush;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002967 engine->irq_enable_mask =
Zhao Yakui845f74a2014-04-17 10:37:37 +08002968 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002969
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002970 return intel_init_ring_buffer(dev, engine);
Zhao Yakui845f74a2014-04-17 10:37:37 +08002971}
2972
Chris Wilson549f7362010-10-19 11:19:32 +01002973int intel_init_blt_ring_buffer(struct drm_device *dev)
2974{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002975 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002976 struct intel_engine_cs *engine = &dev_priv->engine[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002977
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002978 engine->name = "blitter ring";
2979 engine->id = BCS;
2980 engine->exec_id = I915_EXEC_BLT;
Chris Wilson215a7e32016-04-29 13:18:23 +01002981 engine->hw_id = 2;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002982 engine->mmio_base = BLT_RING_BASE;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01002983
2984 intel_ring_default_vfuncs(dev_priv, engine);
2985
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002986 engine->flush = gen6_ring_flush;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002987 if (INTEL_GEN(dev_priv) >= 8)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002988 engine->irq_enable_mask =
Ben Widawskyabd58f02013-11-02 21:07:09 -07002989 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Tvrtko Ursulin8d228912016-06-29 16:09:32 +01002990 else
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002991 engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
Chris Wilson549f7362010-10-19 11:19:32 +01002992
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002993 return intel_init_ring_buffer(dev, engine);
Chris Wilson549f7362010-10-19 11:19:32 +01002994}
Chris Wilsona7b97612012-07-20 12:41:08 +01002995
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002996int intel_init_vebox_ring_buffer(struct drm_device *dev)
2997{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002998 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002999 struct intel_engine_cs *engine = &dev_priv->engine[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003000
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003001 engine->name = "video enhancement ring";
3002 engine->id = VECS;
3003 engine->exec_id = I915_EXEC_VEBOX;
Chris Wilson215a7e32016-04-29 13:18:23 +01003004 engine->hw_id = 3;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003005 engine->mmio_base = VEBOX_RING_BASE;
Tvrtko Ursulin06a2fe22016-06-29 16:09:20 +01003006
3007 intel_ring_default_vfuncs(dev_priv, engine);
3008
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003009 engine->flush = gen6_ring_flush;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003010
Chris Wilsonc0336662016-05-06 15:40:21 +01003011 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003012 engine->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08003013 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003014 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003015 engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
Chris Wilson31bb59c2016-07-01 17:23:27 +01003016 engine->irq_enable = hsw_vebox_irq_enable;
3017 engine->irq_disable = hsw_vebox_irq_disable;
Ben Widawskyabd58f02013-11-02 21:07:09 -07003018 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003019
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003020 return intel_init_ring_buffer(dev, engine);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07003021}
3022
Chris Wilsona7b97612012-07-20 12:41:08 +01003023int
John Harrison4866d722015-05-29 17:43:55 +01003024intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01003025{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003026 struct intel_engine_cs *engine = req->engine;
Chris Wilsona7b97612012-07-20 12:41:08 +01003027 int ret;
3028
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003029 if (!engine->gpu_caches_dirty)
Chris Wilsona7b97612012-07-20 12:41:08 +01003030 return 0;
3031
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003032 ret = engine->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01003033 if (ret)
3034 return ret;
3035
John Harrisona84c3ae2015-05-29 17:43:57 +01003036 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01003037
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003038 engine->gpu_caches_dirty = false;
Chris Wilsona7b97612012-07-20 12:41:08 +01003039 return 0;
3040}
3041
3042int
John Harrison2f200552015-05-29 17:43:53 +01003043intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01003044{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00003045 struct intel_engine_cs *engine = req->engine;
Chris Wilsona7b97612012-07-20 12:41:08 +01003046 uint32_t flush_domains;
3047 int ret;
3048
3049 flush_domains = 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003050 if (engine->gpu_caches_dirty)
Chris Wilsona7b97612012-07-20 12:41:08 +01003051 flush_domains = I915_GEM_GPU_DOMAINS;
3052
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003053 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003054 if (ret)
3055 return ret;
3056
John Harrisona84c3ae2015-05-29 17:43:57 +01003057 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01003058
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003059 engine->gpu_caches_dirty = false;
Chris Wilsona7b97612012-07-20 12:41:08 +01003060 return 0;
3061}
Chris Wilsone3efda42014-04-09 09:19:41 +01003062
3063void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00003064intel_stop_engine(struct intel_engine_cs *engine)
Chris Wilsone3efda42014-04-09 09:19:41 +01003065{
3066 int ret;
3067
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00003068 if (!intel_engine_initialized(engine))
Chris Wilsone3efda42014-04-09 09:19:41 +01003069 return;
3070
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003071 ret = intel_engine_idle(engine);
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003072 if (ret)
Chris Wilsone3efda42014-04-09 09:19:41 +01003073 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003074 engine->name, ret);
Chris Wilsone3efda42014-04-09 09:19:41 +01003075
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003076 stop_ring(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01003077}