blob: 654ae991ea13c6208696b2a0834b475af0aa29cb [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
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/drmP.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070031#include "i915_drv.h"
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/i915_drm.h>
Eric Anholt62fdfea2010-05-21 13:26:39 -070033#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010034#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070035
Oscar Mateo48d82382014-07-24 17:04:23 +010036bool
37intel_ring_initialized(struct intel_engine_cs *ring)
38{
39 struct drm_device *dev = ring->dev;
Chris Wilson18393f62014-04-09 09:19:40 +010040
Oscar Mateo48d82382014-07-24 17:04:23 +010041 if (!dev)
42 return false;
43
44 if (i915.enable_execlists) {
45 struct intel_context *dctx = ring->default_context;
46 struct intel_ringbuffer *ringbuf = dctx->engine[ring->id].ringbuf;
47
48 return ringbuf->obj;
49 } else
50 return ring->buffer && ring->buffer->obj;
51}
52
Oscar Mateo82e104c2014-07-24 17:04:26 +010053int __intel_ring_space(int head, int tail, int size)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010054{
Dave Gordon4f547412014-11-27 11:22:48 +000055 int space = head - tail;
56 if (space <= 0)
Chris Wilson1cf0ba12014-05-05 09:07:33 +010057 space += size;
Dave Gordon4f547412014-11-27 11:22:48 +000058 return space - I915_RING_FREE_SPACE;
Chris Wilson1cf0ba12014-05-05 09:07:33 +010059}
60
Dave Gordonebd0fd42014-11-27 11:22:49 +000061void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
62{
63 if (ringbuf->last_retired_head != -1) {
64 ringbuf->head = ringbuf->last_retired_head;
65 ringbuf->last_retired_head = -1;
66 }
67
68 ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
69 ringbuf->tail, ringbuf->size);
70}
71
Oscar Mateo82e104c2014-07-24 17:04:26 +010072int intel_ring_space(struct intel_ringbuffer *ringbuf)
Chris Wilsonc7dca472011-01-20 17:00:10 +000073{
Dave Gordonebd0fd42014-11-27 11:22:49 +000074 intel_ring_update_space(ringbuf);
75 return ringbuf->space;
Chris Wilsonc7dca472011-01-20 17:00:10 +000076}
77
Oscar Mateo82e104c2014-07-24 17:04:26 +010078bool intel_ring_stopped(struct intel_engine_cs *ring)
Chris Wilson09246732013-08-10 22:16:32 +010079{
80 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020081 return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
82}
Chris Wilson09246732013-08-10 22:16:32 +010083
John Harrison6258fbe2015-05-29 17:43:48 +010084static void __intel_ring_advance(struct intel_engine_cs *ring)
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020085{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010086 struct intel_ringbuffer *ringbuf = ring->buffer;
87 ringbuf->tail &= ringbuf->size - 1;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +020088 if (intel_ring_stopped(ring))
Chris Wilson09246732013-08-10 22:16:32 +010089 return;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +010090 ring->write_tail(ring, ringbuf->tail);
Chris Wilson09246732013-08-10 22:16:32 +010091}
92
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000093static int
John Harrisona84c3ae2015-05-29 17:43:57 +010094gen2_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010095 u32 invalidate_domains,
96 u32 flush_domains)
97{
John Harrisona84c3ae2015-05-29 17:43:57 +010098 struct intel_engine_cs *ring = req->ring;
Chris Wilson46f0f8d2012-04-18 11:12:11 +010099 u32 cmd;
100 int ret;
101
102 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +0200103 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100104 cmd |= MI_NO_WRITE_FLUSH;
105
106 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
107 cmd |= MI_READ_FLUSH;
108
John Harrison5fb9de12015-05-29 17:44:07 +0100109 ret = intel_ring_begin(req, 2);
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100110 if (ret)
111 return ret;
112
113 intel_ring_emit(ring, cmd);
114 intel_ring_emit(ring, MI_NOOP);
115 intel_ring_advance(ring);
116
117 return 0;
118}
119
120static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100121gen4_render_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100122 u32 invalidate_domains,
123 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700124{
John Harrisona84c3ae2015-05-29 17:43:57 +0100125 struct intel_engine_cs *ring = req->ring;
Chris Wilson78501ea2010-10-27 12:18:21 +0100126 struct drm_device *dev = ring->dev;
Chris Wilson6f392d52010-08-07 11:01:22 +0100127 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000128 int ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100129
Chris Wilson36d527d2011-03-19 22:26:49 +0000130 /*
131 * read/write caches:
132 *
133 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
134 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
135 * also flushed at 2d versus 3d pipeline switches.
136 *
137 * read-only caches:
138 *
139 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
140 * MI_READ_FLUSH is set, and is always flushed on 965.
141 *
142 * I915_GEM_DOMAIN_COMMAND may not exist?
143 *
144 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
145 * invalidated when MI_EXE_FLUSH is set.
146 *
147 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
148 * invalidated with every MI_FLUSH.
149 *
150 * TLBs:
151 *
152 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
153 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
154 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
155 * are flushed at any MI_FLUSH.
156 */
157
158 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100159 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000160 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000161 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
162 cmd |= MI_EXE_FLUSH;
163
164 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
165 (IS_G4X(dev) || IS_GEN5(dev)))
166 cmd |= MI_INVALIDATE_ISP;
167
John Harrison5fb9de12015-05-29 17:44:07 +0100168 ret = intel_ring_begin(req, 2);
Chris Wilson36d527d2011-03-19 22:26:49 +0000169 if (ret)
170 return ret;
171
172 intel_ring_emit(ring, cmd);
173 intel_ring_emit(ring, MI_NOOP);
174 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000175
176 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800177}
178
Jesse Barnes8d315282011-10-16 10:23:31 +0200179/**
180 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
181 * implementing two workarounds on gen6. From section 1.4.7.1
182 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
183 *
184 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
185 * produced by non-pipelined state commands), software needs to first
186 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
187 * 0.
188 *
189 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
190 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
191 *
192 * And the workaround for these two requires this workaround first:
193 *
194 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
195 * BEFORE the pipe-control with a post-sync op and no write-cache
196 * flushes.
197 *
198 * And this last workaround is tricky because of the requirements on
199 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
200 * volume 2 part 1:
201 *
202 * "1 of the following must also be set:
203 * - Render Target Cache Flush Enable ([12] of DW1)
204 * - Depth Cache Flush Enable ([0] of DW1)
205 * - Stall at Pixel Scoreboard ([1] of DW1)
206 * - Depth Stall ([13] of DW1)
207 * - Post-Sync Operation ([13] of DW1)
208 * - Notify Enable ([8] of DW1)"
209 *
210 * The cache flushes require the workaround flush that triggered this
211 * one, so we can't use it. Depth stall would trigger the same.
212 * Post-sync nonzero is what triggered this second workaround, so we
213 * can't use that one either. Notify enable is IRQs, which aren't
214 * really our business. That leaves only stall at scoreboard.
215 */
216static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100217intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
Jesse Barnes8d315282011-10-16 10:23:31 +0200218{
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100219 struct intel_engine_cs *ring = req->ring;
Chris Wilson18393f62014-04-09 09:19:40 +0100220 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200221 int ret;
222
John Harrison5fb9de12015-05-29 17:44:07 +0100223 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200224 if (ret)
225 return ret;
226
227 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
228 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
229 PIPE_CONTROL_STALL_AT_SCOREBOARD);
230 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
231 intel_ring_emit(ring, 0); /* low dword */
232 intel_ring_emit(ring, 0); /* high dword */
233 intel_ring_emit(ring, MI_NOOP);
234 intel_ring_advance(ring);
235
John Harrison5fb9de12015-05-29 17:44:07 +0100236 ret = intel_ring_begin(req, 6);
Jesse Barnes8d315282011-10-16 10:23:31 +0200237 if (ret)
238 return ret;
239
240 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
241 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
242 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
243 intel_ring_emit(ring, 0);
244 intel_ring_emit(ring, 0);
245 intel_ring_emit(ring, MI_NOOP);
246 intel_ring_advance(ring);
247
248 return 0;
249}
250
251static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100252gen6_render_ring_flush(struct drm_i915_gem_request *req,
253 u32 invalidate_domains, u32 flush_domains)
Jesse Barnes8d315282011-10-16 10:23:31 +0200254{
John Harrisona84c3ae2015-05-29 17:43:57 +0100255 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8d315282011-10-16 10:23:31 +0200256 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100257 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200258 int ret;
259
Paulo Zanonib3111502012-08-17 18:35:42 -0300260 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100261 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300262 if (ret)
263 return ret;
264
Jesse Barnes8d315282011-10-16 10:23:31 +0200265 /* Just flush everything. Experiments have shown that reducing the
266 * number of bits based on the write domains has little performance
267 * impact.
268 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100269 if (flush_domains) {
270 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
271 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
272 /*
273 * Ensure that any following seqno writes only happen
274 * when the render cache is indeed flushed.
275 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200276 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100277 }
278 if (invalidate_domains) {
279 flags |= PIPE_CONTROL_TLB_INVALIDATE;
280 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
281 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
282 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
283 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
284 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
285 /*
286 * TLB invalidate requires a post-sync write.
287 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700288 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100289 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200290
John Harrison5fb9de12015-05-29 17:44:07 +0100291 ret = intel_ring_begin(req, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200292 if (ret)
293 return ret;
294
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100295 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
Jesse Barnes8d315282011-10-16 10:23:31 +0200296 intel_ring_emit(ring, flags);
297 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100298 intel_ring_emit(ring, 0);
Jesse Barnes8d315282011-10-16 10:23:31 +0200299 intel_ring_advance(ring);
300
301 return 0;
302}
303
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100304static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100305gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300306{
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100307 struct intel_engine_cs *ring = req->ring;
Paulo Zanonif3987632012-08-17 18:35:43 -0300308 int ret;
309
John Harrison5fb9de12015-05-29 17:44:07 +0100310 ret = intel_ring_begin(req, 4);
Paulo Zanonif3987632012-08-17 18:35:43 -0300311 if (ret)
312 return ret;
313
314 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
315 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
316 PIPE_CONTROL_STALL_AT_SCOREBOARD);
317 intel_ring_emit(ring, 0);
318 intel_ring_emit(ring, 0);
319 intel_ring_advance(ring);
320
321 return 0;
322}
323
324static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100325gen7_render_ring_flush(struct drm_i915_gem_request *req,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300326 u32 invalidate_domains, u32 flush_domains)
327{
John Harrisona84c3ae2015-05-29 17:43:57 +0100328 struct intel_engine_cs *ring = req->ring;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300329 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100330 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300331 int ret;
332
Paulo Zanonif3987632012-08-17 18:35:43 -0300333 /*
334 * Ensure that any following seqno writes only happen when the render
335 * cache is indeed flushed.
336 *
337 * Workaround: 4th PIPE_CONTROL command (except the ones with only
338 * read-cache invalidate bits set) must have the CS_STALL bit set. We
339 * don't try to be clever and just set it unconditionally.
340 */
341 flags |= PIPE_CONTROL_CS_STALL;
342
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300343 /* Just flush everything. Experiments have shown that reducing the
344 * number of bits based on the write domains has little performance
345 * impact.
346 */
347 if (flush_domains) {
348 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
349 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300350 }
351 if (invalidate_domains) {
352 flags |= PIPE_CONTROL_TLB_INVALIDATE;
353 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
354 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
355 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
356 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
357 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000358 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300359 /*
360 * TLB invalidate requires a post-sync write.
361 */
362 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200363 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300364
Chris Wilsonadd284a2014-12-16 08:44:32 +0000365 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
366
Paulo Zanonif3987632012-08-17 18:35:43 -0300367 /* Workaround: we must issue a pipe_control with CS-stall bit
368 * set before a pipe_control command that has the state cache
369 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100370 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300371 }
372
John Harrison5fb9de12015-05-29 17:44:07 +0100373 ret = intel_ring_begin(req, 4);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300374 if (ret)
375 return ret;
376
377 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
378 intel_ring_emit(ring, flags);
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200379 intel_ring_emit(ring, scratch_addr);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300380 intel_ring_emit(ring, 0);
381 intel_ring_advance(ring);
382
383 return 0;
384}
385
Ben Widawskya5f3d682013-11-02 21:07:27 -0700386static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100387gen8_emit_pipe_control(struct drm_i915_gem_request *req,
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300388 u32 flags, u32 scratch_addr)
389{
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100390 struct intel_engine_cs *ring = req->ring;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300391 int ret;
392
John Harrison5fb9de12015-05-29 17:44:07 +0100393 ret = intel_ring_begin(req, 6);
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300394 if (ret)
395 return ret;
396
397 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
398 intel_ring_emit(ring, flags);
399 intel_ring_emit(ring, scratch_addr);
400 intel_ring_emit(ring, 0);
401 intel_ring_emit(ring, 0);
402 intel_ring_emit(ring, 0);
403 intel_ring_advance(ring);
404
405 return 0;
406}
407
408static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100409gen8_render_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700410 u32 invalidate_domains, u32 flush_domains)
411{
412 u32 flags = 0;
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100413 u32 scratch_addr = req->ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800414 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700415
416 flags |= PIPE_CONTROL_CS_STALL;
417
418 if (flush_domains) {
419 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
420 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
421 }
422 if (invalidate_domains) {
423 flags |= PIPE_CONTROL_TLB_INVALIDATE;
424 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
425 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
426 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
427 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
428 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
429 flags |= PIPE_CONTROL_QW_WRITE;
430 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800431
432 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100433 ret = gen8_emit_pipe_control(req,
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800434 PIPE_CONTROL_CS_STALL |
435 PIPE_CONTROL_STALL_AT_SCOREBOARD,
436 0);
437 if (ret)
438 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700439 }
440
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100441 return gen8_emit_pipe_control(req, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700442}
443
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100444static void ring_write_tail(struct intel_engine_cs *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100445 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800446{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300447 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100448 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800449}
450
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100451u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800452{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300453 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson50877442014-03-21 12:41:53 +0000454 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800455
Chris Wilson50877442014-03-21 12:41:53 +0000456 if (INTEL_INFO(ring->dev)->gen >= 8)
457 acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
458 RING_ACTHD_UDW(ring->mmio_base));
459 else if (INTEL_INFO(ring->dev)->gen >= 4)
460 acthd = I915_READ(RING_ACTHD(ring->mmio_base));
461 else
462 acthd = I915_READ(ACTHD);
463
464 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800465}
466
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100467static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200468{
469 struct drm_i915_private *dev_priv = ring->dev->dev_private;
470 u32 addr;
471
472 addr = dev_priv->status_page_dmah->busaddr;
473 if (INTEL_INFO(ring->dev)->gen >= 4)
474 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
475 I915_WRITE(HWS_PGA, addr);
476}
477
Damien Lespiauaf75f262015-02-10 19:32:17 +0000478static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
479{
480 struct drm_device *dev = ring->dev;
481 struct drm_i915_private *dev_priv = ring->dev->dev_private;
482 u32 mmio = 0;
483
484 /* The ring status page addresses are no longer next to the rest of
485 * the ring registers as of gen7.
486 */
487 if (IS_GEN7(dev)) {
488 switch (ring->id) {
489 case RCS:
490 mmio = RENDER_HWS_PGA_GEN7;
491 break;
492 case BCS:
493 mmio = BLT_HWS_PGA_GEN7;
494 break;
495 /*
496 * VCS2 actually doesn't exist on Gen7. Only shut up
497 * gcc switch check warning
498 */
499 case VCS2:
500 case VCS:
501 mmio = BSD_HWS_PGA_GEN7;
502 break;
503 case VECS:
504 mmio = VEBOX_HWS_PGA_GEN7;
505 break;
506 }
507 } else if (IS_GEN6(ring->dev)) {
508 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
509 } else {
510 /* XXX: gen8 returns to sanity */
511 mmio = RING_HWS_PGA(ring->mmio_base);
512 }
513
514 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
515 POSTING_READ(mmio);
516
517 /*
518 * Flush the TLB for this page
519 *
520 * FIXME: These two bits have disappeared on gen8, so a question
521 * arises: do we still need this and if so how should we go about
522 * invalidating the TLB?
523 */
524 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
525 u32 reg = RING_INSTPM(ring->mmio_base);
526
527 /* ring should be idle before issuing a sync flush*/
528 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
529
530 I915_WRITE(reg,
531 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
532 INSTPM_SYNC_FLUSH));
533 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
534 1000))
535 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
536 ring->name);
537 }
538}
539
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100540static bool stop_ring(struct intel_engine_cs *ring)
Chris Wilson9991ae72014-04-02 16:36:07 +0100541{
542 struct drm_i915_private *dev_priv = to_i915(ring->dev);
543
544 if (!IS_GEN2(ring->dev)) {
545 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
Daniel Vetter403bdd12014-08-07 16:05:39 +0200546 if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
547 DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100548 /* Sometimes we observe that the idle flag is not
549 * set even though the ring is empty. So double
550 * check before giving up.
551 */
552 if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
553 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100554 }
555 }
556
557 I915_WRITE_CTL(ring, 0);
558 I915_WRITE_HEAD(ring, 0);
559 ring->write_tail(ring, 0);
560
561 if (!IS_GEN2(ring->dev)) {
562 (void)I915_READ_CTL(ring);
563 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
564 }
565
566 return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
567}
568
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100569static int init_ring_common(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800570{
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200571 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300572 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100573 struct intel_ringbuffer *ringbuf = ring->buffer;
574 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200575 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800576
Mika Kuoppala59bad942015-01-16 11:34:40 +0200577 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200578
Chris Wilson9991ae72014-04-02 16:36:07 +0100579 if (!stop_ring(ring)) {
580 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000581 DRM_DEBUG_KMS("%s head not reset to zero "
582 "ctl %08x head %08x tail %08x start %08x\n",
583 ring->name,
584 I915_READ_CTL(ring),
585 I915_READ_HEAD(ring),
586 I915_READ_TAIL(ring),
587 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800588
Chris Wilson9991ae72014-04-02 16:36:07 +0100589 if (!stop_ring(ring)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000590 DRM_ERROR("failed to set %s head to zero "
591 "ctl %08x head %08x tail %08x start %08x\n",
592 ring->name,
593 I915_READ_CTL(ring),
594 I915_READ_HEAD(ring),
595 I915_READ_TAIL(ring),
596 I915_READ_START(ring));
Chris Wilson9991ae72014-04-02 16:36:07 +0100597 ret = -EIO;
598 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000599 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700600 }
601
Chris Wilson9991ae72014-04-02 16:36:07 +0100602 if (I915_NEED_GFX_HWS(dev))
603 intel_ring_setup_status_page(ring);
604 else
605 ring_setup_phys_status_page(ring);
606
Jiri Kosinaece4a172014-08-07 16:29:53 +0200607 /* Enforce ordering by reading HEAD register back */
608 I915_READ_HEAD(ring);
609
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200610 /* Initialize the ring. This must happen _after_ we've cleared the ring
611 * registers with the above sequence (the readback of the HEAD registers
612 * also enforces ordering), otherwise the hw might lose the new ring
613 * register values. */
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700614 I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100615
616 /* WaClearRingBufHeadRegAtInit:ctg,elk */
617 if (I915_READ_HEAD(ring))
618 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
619 ring->name, I915_READ_HEAD(ring));
620 I915_WRITE_HEAD(ring, 0);
621 (void)I915_READ_HEAD(ring);
622
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200623 I915_WRITE_CTL(ring,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100624 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000625 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800626
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800627 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400628 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700629 I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
Sean Paulf01db982012-03-16 12:43:22 -0400630 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000631 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100632 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
633 ring->name,
634 I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
635 I915_READ_HEAD(ring), I915_READ_TAIL(ring),
636 I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200637 ret = -EIO;
638 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800639 }
640
Dave Gordonebd0fd42014-11-27 11:22:49 +0000641 ringbuf->last_retired_head = -1;
Chris Wilson5c6c6002014-09-06 10:28:27 +0100642 ringbuf->head = I915_READ_HEAD(ring);
643 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Dave Gordonebd0fd42014-11-27 11:22:49 +0000644 intel_ring_update_space(ringbuf);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000645
Chris Wilson50f018d2013-06-10 11:20:19 +0100646 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
647
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200648out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200649 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200650
651 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700652}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800653
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100654void
655intel_fini_pipe_control(struct intel_engine_cs *ring)
656{
657 struct drm_device *dev = ring->dev;
658
659 if (ring->scratch.obj == NULL)
660 return;
661
662 if (INTEL_INFO(dev)->gen >= 5) {
663 kunmap(sg_page(ring->scratch.obj->pages->sgl));
664 i915_gem_object_ggtt_unpin(ring->scratch.obj);
665 }
666
667 drm_gem_object_unreference(&ring->scratch.obj->base);
668 ring->scratch.obj = NULL;
669}
670
671int
672intel_init_pipe_control(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000673{
Chris Wilsonc6df5412010-12-15 09:56:50 +0000674 int ret;
675
Daniel Vetterbfc882b2014-11-20 00:33:08 +0100676 WARN_ON(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000677
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100678 ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
679 if (ring->scratch.obj == NULL) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000680 DRM_ERROR("Failed to allocate seqno page\n");
681 ret = -ENOMEM;
682 goto err;
683 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100684
Daniel Vettera9cc7262014-02-14 14:01:13 +0100685 ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
686 if (ret)
687 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000688
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100689 ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000690 if (ret)
691 goto err_unref;
692
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100693 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
694 ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
695 if (ring->scratch.cpu_page == NULL) {
Wei Yongjun56b085a2013-05-28 17:51:44 +0800696 ret = -ENOMEM;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000697 goto err_unpin;
Wei Yongjun56b085a2013-05-28 17:51:44 +0800698 }
Chris Wilsonc6df5412010-12-15 09:56:50 +0000699
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200700 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100701 ring->name, ring->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000702 return 0;
703
704err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800705 i915_gem_object_ggtt_unpin(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000706err_unref:
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100707 drm_gem_object_unreference(&ring->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000708err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000709 return ret;
710}
711
John Harrisone2be4fa2015-05-29 17:43:54 +0100712static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100713{
Mika Kuoppala72253422014-10-07 17:21:26 +0300714 int ret, i;
John Harrisone2be4fa2015-05-29 17:43:54 +0100715 struct intel_engine_cs *ring = req->ring;
Arun Siluvery888b5992014-08-26 14:44:51 +0100716 struct drm_device *dev = ring->dev;
717 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala72253422014-10-07 17:21:26 +0300718 struct i915_workarounds *w = &dev_priv->workarounds;
Arun Siluvery888b5992014-08-26 14:44:51 +0100719
Francisco Jerez02235802015-10-07 14:44:01 +0300720 if (w->count == 0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300721 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100722
Mika Kuoppala72253422014-10-07 17:21:26 +0300723 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100724 ret = intel_ring_flush_all_caches(req);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100725 if (ret)
726 return ret;
727
John Harrison5fb9de12015-05-29 17:44:07 +0100728 ret = intel_ring_begin(req, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300729 if (ret)
730 return ret;
731
Arun Siluvery22a916a2014-10-22 18:59:52 +0100732 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300733 for (i = 0; i < w->count; i++) {
Mika Kuoppala72253422014-10-07 17:21:26 +0300734 intel_ring_emit(ring, w->reg[i].addr);
735 intel_ring_emit(ring, w->reg[i].value);
736 }
Arun Siluvery22a916a2014-10-22 18:59:52 +0100737 intel_ring_emit(ring, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300738
739 intel_ring_advance(ring);
740
741 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100742 ret = intel_ring_flush_all_caches(req);
Mika Kuoppala72253422014-10-07 17:21:26 +0300743 if (ret)
744 return ret;
745
746 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
747
748 return 0;
749}
750
John Harrison87531812015-05-29 17:43:44 +0100751static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100752{
753 int ret;
754
John Harrisone2be4fa2015-05-29 17:43:54 +0100755 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100756 if (ret != 0)
757 return ret;
758
John Harrisonbe013632015-05-29 17:43:45 +0100759 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100760 if (ret)
761 DRM_ERROR("init render state: %d\n", ret);
762
763 return ret;
764}
765
Mika Kuoppala72253422014-10-07 17:21:26 +0300766static int wa_add(struct drm_i915_private *dev_priv,
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000767 const u32 addr, const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300768{
769 const u32 idx = dev_priv->workarounds.count;
770
771 if (WARN_ON(idx >= I915_MAX_WA_REGS))
772 return -ENOSPC;
773
774 dev_priv->workarounds.reg[idx].addr = addr;
775 dev_priv->workarounds.reg[idx].value = val;
776 dev_priv->workarounds.reg[idx].mask = mask;
777
778 dev_priv->workarounds.count++;
779
780 return 0;
781}
782
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100783#define WA_REG(addr, mask, val) do { \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000784 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300785 if (r) \
786 return r; \
Mika Kuoppalaca5a0fb2015-08-11 15:44:31 +0100787 } while (0)
Mika Kuoppala72253422014-10-07 17:21:26 +0300788
789#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000790 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300791
792#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000793 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300794
Damien Lespiau98533252014-12-08 17:33:51 +0000795#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000796 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300797
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000798#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
799#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300800
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000801#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300802
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100803static int gen8_init_workarounds(struct intel_engine_cs *ring)
804{
Arun Siluvery68c61982015-09-25 17:40:38 +0100805 struct drm_device *dev = ring->dev;
806 struct drm_i915_private *dev_priv = dev->dev_private;
807
808 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100809
Arun Siluvery717d84d2015-09-25 17:40:39 +0100810 /* WaDisableAsyncFlipPerfMode:bdw,chv */
811 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
812
Arun Siluveryd0581192015-09-25 17:40:40 +0100813 /* WaDisablePartialInstShootdown:bdw,chv */
814 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
815 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
816
Arun Siluverya340af52015-09-25 17:40:45 +0100817 /* Use Force Non-Coherent whenever executing a 3D context. This is a
818 * workaround for for a possible hang in the unlikely event a TLB
819 * invalidation occurs during a PSD flush.
820 */
821 /* WaForceEnableNonCoherent:bdw,chv */
Arun Siluvery120f5d22015-09-25 17:40:46 +0100822 /* WaHdcDisableFetchWhenMasked:bdw,chv */
Arun Siluverya340af52015-09-25 17:40:45 +0100823 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Arun Siluvery120f5d22015-09-25 17:40:46 +0100824 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Arun Siluverya340af52015-09-25 17:40:45 +0100825 HDC_FORCE_NON_COHERENT);
826
Arun Siluvery6def8fd2015-09-25 17:40:42 +0100827 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
828 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
829 * polygons in the same 8x4 pixel/sample area to be processed without
830 * stalling waiting for the earlier ones to write to Hierarchical Z
831 * buffer."
832 *
833 * This optimization is off by default for BDW and CHV; turn it on.
834 */
835 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
836
Arun Siluvery48404632015-09-25 17:40:43 +0100837 /* Wa4x4STCOptimizationDisable:bdw,chv */
838 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
839
Arun Siluvery7eebcde2015-09-25 17:40:44 +0100840 /*
841 * BSpec recommends 8x4 when MSAA is used,
842 * however in practice 16x4 seems fastest.
843 *
844 * Note that PS/WM thread counts depend on the WIZ hashing
845 * disable bit, which we don't touch here, but it's good
846 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
847 */
848 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
849 GEN6_WIZ_HASHING_MASK,
850 GEN6_WIZ_HASHING_16x4);
851
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100852 return 0;
853}
854
Mika Kuoppala72253422014-10-07 17:21:26 +0300855static int bdw_init_workarounds(struct intel_engine_cs *ring)
856{
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100857 int ret;
Mika Kuoppala72253422014-10-07 17:21:26 +0300858 struct drm_device *dev = ring->dev;
859 struct drm_i915_private *dev_priv = dev->dev_private;
860
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100861 ret = gen8_init_workarounds(ring);
862 if (ret)
863 return ret;
864
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700865 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Arun Siluveryd0581192015-09-25 17:40:40 +0100866 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100867
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700868 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300869 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
870 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100871
Mika Kuoppala72253422014-10-07 17:21:26 +0300872 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
873 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100874
Mika Kuoppala72253422014-10-07 17:21:26 +0300875 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000876 /* WaForceContextSaveRestoreNonCoherent:bdw */
877 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000878 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Mika Kuoppala72253422014-10-07 17:21:26 +0300879 (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100880
Arun Siluvery86d7f232014-08-26 14:44:50 +0100881 return 0;
882}
883
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300884static int chv_init_workarounds(struct intel_engine_cs *ring)
885{
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100886 int ret;
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300887 struct drm_device *dev = ring->dev;
888 struct drm_i915_private *dev_priv = dev->dev_private;
889
Arun Siluverye9a64ad2015-09-25 17:40:37 +0100890 ret = gen8_init_workarounds(ring);
891 if (ret)
892 return ret;
893
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300894 /* WaDisableThreadStallDopClockGating:chv */
Arun Siluveryd0581192015-09-25 17:40:40 +0100895 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300896
Kenneth Graunked60de812015-01-10 18:02:22 -0800897 /* Improve HiZ throughput on CHV. */
898 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
899
Mika Kuoppala72253422014-10-07 17:21:26 +0300900 return 0;
901}
902
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000903static int gen9_init_workarounds(struct intel_engine_cs *ring)
904{
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000905 struct drm_device *dev = ring->dev;
906 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak8ea6f892015-05-19 17:05:42 +0300907 uint32_t tmp;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000908
Nick Hoathb0e6f6d2015-05-07 14:15:29 +0100909 /* WaDisablePartialInstShootdown:skl,bxt */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000910 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
911 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
912
Nick Hoatha119a6e2015-05-07 14:15:30 +0100913 /* Syncing dependencies between camera and graphics:skl,bxt */
Nick Hoath84241712015-02-05 10:47:20 +0000914 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
915 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
916
Nick Hoathd2a31db2015-05-07 14:15:31 +0100917 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) == SKL_REVID_A0 ||
918 INTEL_REVID(dev) == SKL_REVID_B0)) ||
919 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
920 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
Damien Lespiaua86eb582015-02-11 18:21:44 +0000921 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
922 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000923 }
924
Nick Hoatha13d2152015-05-07 14:15:32 +0100925 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0) ||
926 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
927 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000928 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
929 GEN9_RHWO_OPTIMIZATION_DISABLE);
Arun Siluvery9b014352015-07-14 15:01:30 +0100930 /*
931 * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
932 * but we do that in per ctx batchbuffer as there is an issue
933 * with this register not getting restored on ctx restore
934 */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000935 }
936
Nick Hoath27a1b682015-05-07 14:15:33 +0100937 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) >= SKL_REVID_C0) ||
938 IS_BROXTON(dev)) {
939 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt */
Nick Hoathcac23df2015-02-05 10:47:22 +0000940 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
941 GEN9_ENABLE_YV12_BUGFIX);
942 }
943
Nick Hoath50683682015-05-07 14:15:35 +0100944 /* Wa4x4STCOptimizationDisable:skl,bxt */
Nick Hoath27160c92015-05-07 14:15:36 +0100945 /* WaDisablePartialResolveInVc:skl,bxt */
Arun Siluvery60294682015-09-25 14:33:37 +0100946 WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
947 GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
Damien Lespiau9370cd92015-02-09 19:33:17 +0000948
Nick Hoath16be17a2015-05-07 14:15:37 +0100949 /* WaCcsTlbPrefetchDisable:skl,bxt */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000950 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
951 GEN9_CCS_TLB_PREFETCH_ENABLE);
952
Imre Deak5a2ae952015-05-19 15:04:59 +0300953 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
954 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_C0) ||
955 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200956 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
957 PIXEL_MASK_CAMMING_DISABLE);
958
Imre Deak8ea6f892015-05-19 17:05:42 +0300959 /* WaForceContextSaveRestoreNonCoherent:skl,bxt */
960 tmp = HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT;
961 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_F0) ||
962 (IS_BROXTON(dev) && INTEL_REVID(dev) >= BXT_REVID_B0))
963 tmp |= HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE;
964 WA_SET_BIT_MASKED(HDC_CHICKEN0, tmp);
965
Arun Siluvery8c761602015-09-08 10:31:48 +0100966 /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt */
967 if (IS_SKYLAKE(dev) ||
968 (IS_BROXTON(dev) && INTEL_REVID(dev) <= BXT_REVID_B0)) {
969 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
970 GEN8_SAMPLER_POWER_BYPASS_DIS);
971 }
972
Robert Beckett6b6d5622015-09-08 10:31:52 +0100973 /* WaDisableSTUnitPowerOptimization:skl,bxt */
974 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
975
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000976 return 0;
977}
978
Damien Lespiaub7668792015-02-14 18:30:29 +0000979static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
Damien Lespiau8d205492015-02-09 19:33:15 +0000980{
Damien Lespiaub7668792015-02-14 18:30:29 +0000981 struct drm_device *dev = ring->dev;
982 struct drm_i915_private *dev_priv = dev->dev_private;
983 u8 vals[3] = { 0, 0, 0 };
984 unsigned int i;
985
986 for (i = 0; i < 3; i++) {
987 u8 ss;
988
989 /*
990 * Only consider slices where one, and only one, subslice has 7
991 * EUs
992 */
993 if (hweight8(dev_priv->info.subslice_7eu[i]) != 1)
994 continue;
995
996 /*
997 * subslice_7eu[i] != 0 (because of the check above) and
998 * ss_max == 4 (maximum number of subslices possible per slice)
999 *
1000 * -> 0 <= ss <= 3;
1001 */
1002 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1003 vals[i] = 3 - ss;
1004 }
1005
1006 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1007 return 0;
1008
1009 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1010 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1011 GEN9_IZ_HASHING_MASK(2) |
1012 GEN9_IZ_HASHING_MASK(1) |
1013 GEN9_IZ_HASHING_MASK(0),
1014 GEN9_IZ_HASHING(2, vals[2]) |
1015 GEN9_IZ_HASHING(1, vals[1]) |
1016 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001017
Mika Kuoppala72253422014-10-07 17:21:26 +03001018 return 0;
1019}
1020
Damien Lespiaub7668792015-02-14 18:30:29 +00001021
Damien Lespiau8d205492015-02-09 19:33:15 +00001022static int skl_init_workarounds(struct intel_engine_cs *ring)
1023{
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001024 int ret;
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001025 struct drm_device *dev = ring->dev;
1026 struct drm_i915_private *dev_priv = dev->dev_private;
1027
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001028 ret = gen9_init_workarounds(ring);
1029 if (ret)
1030 return ret;
Damien Lespiau8d205492015-02-09 19:33:15 +00001031
Damien Lespiaud0bbbc4f2015-02-09 19:33:16 +00001032 /* WaDisablePowerCompilerClockGating:skl */
1033 if (INTEL_REVID(dev) == SKL_REVID_B0)
1034 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1035 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1036
Nick Hoathb62adbd2015-05-07 14:15:34 +01001037 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
1038 /*
1039 *Use Force Non-Coherent whenever executing a 3D context. This
1040 * is a workaround for a possible hang in the unlikely event
1041 * a TLB invalidation occurs during a PSD flush.
1042 */
1043 /* WaForceEnableNonCoherent:skl */
1044 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1045 HDC_FORCE_NON_COHERENT);
1046 }
1047
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001048 if (INTEL_REVID(dev) == SKL_REVID_C0 ||
1049 INTEL_REVID(dev) == SKL_REVID_D0)
1050 /* WaBarrierPerformanceFixDisable:skl */
1051 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1052 HDC_FENCE_DEST_SLM_DISABLE |
1053 HDC_BARRIER_PERFORMANCE_DISABLE);
1054
Mika Kuoppala9bd9dfb2015-08-06 16:51:00 +03001055 /* WaDisableSbeCacheDispatchPortSharing:skl */
1056 if (INTEL_REVID(dev) <= SKL_REVID_F0) {
1057 WA_SET_BIT_MASKED(
1058 GEN7_HALF_SLICE_CHICKEN1,
1059 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1060 }
1061
Damien Lespiaub7668792015-02-14 18:30:29 +00001062 return skl_tune_iz_hashing(ring);
Damien Lespiau8d205492015-02-09 19:33:15 +00001063}
1064
Nick Hoathcae04372015-03-17 11:39:38 +02001065static int bxt_init_workarounds(struct intel_engine_cs *ring)
1066{
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001067 int ret;
Nick Hoathdfb601e2015-04-10 13:12:24 +01001068 struct drm_device *dev = ring->dev;
1069 struct drm_i915_private *dev_priv = dev->dev_private;
1070
Arun Siluveryaa0011a2015-09-25 14:33:35 +01001071 ret = gen9_init_workarounds(ring);
1072 if (ret)
1073 return ret;
Nick Hoathcae04372015-03-17 11:39:38 +02001074
Nick Hoathdfb601e2015-04-10 13:12:24 +01001075 /* WaDisableThreadStallDopClockGating:bxt */
1076 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1077 STALL_DOP_GATING_DISABLE);
1078
Nick Hoath983b4b92015-04-10 13:12:25 +01001079 /* WaDisableSbeCacheDispatchPortSharing:bxt */
1080 if (INTEL_REVID(dev) <= BXT_REVID_B0) {
1081 WA_SET_BIT_MASKED(
1082 GEN7_HALF_SLICE_CHICKEN1,
1083 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1084 }
1085
Nick Hoathcae04372015-03-17 11:39:38 +02001086 return 0;
1087}
1088
Michel Thierry771b9a52014-11-11 16:47:33 +00001089int init_workarounds_ring(struct intel_engine_cs *ring)
Mika Kuoppala72253422014-10-07 17:21:26 +03001090{
1091 struct drm_device *dev = ring->dev;
1092 struct drm_i915_private *dev_priv = dev->dev_private;
1093
1094 WARN_ON(ring->id != RCS);
1095
1096 dev_priv->workarounds.count = 0;
1097
1098 if (IS_BROADWELL(dev))
1099 return bdw_init_workarounds(ring);
1100
1101 if (IS_CHERRYVIEW(dev))
1102 return chv_init_workarounds(ring);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001103
Damien Lespiau8d205492015-02-09 19:33:15 +00001104 if (IS_SKYLAKE(dev))
1105 return skl_init_workarounds(ring);
Nick Hoathcae04372015-03-17 11:39:38 +02001106
1107 if (IS_BROXTON(dev))
1108 return bxt_init_workarounds(ring);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001109
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001110 return 0;
1111}
1112
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001113static int init_render_ring(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001114{
Chris Wilson78501ea2010-10-27 12:18:21 +01001115 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001116 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001117 int ret = init_ring_common(ring);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001118 if (ret)
1119 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001120
Akash Goel61a563a2014-03-25 18:01:50 +05301121 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1122 if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001123 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001124
1125 /* We need to disable the AsyncFlip performance optimisations in order
1126 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1127 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001128 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001129 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001130 */
Ville Syrjälä2441f872015-06-02 15:37:37 +03001131 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001132 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1133
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001134 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301135 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001136 if (INTEL_INFO(dev)->gen == 6)
1137 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001138 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001139
Akash Goel01fa0302014-03-24 23:00:04 +05301140 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001141 if (IS_GEN7(dev))
1142 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301143 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001144 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001145
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001146 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001147 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1148 * "If this bit is set, STCunit will have LRA as replacement
1149 * policy. [...] This bit must be reset. LRA replacement
1150 * policy is not supported."
1151 */
1152 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001153 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001154 }
1155
Ville Syrjälä9cc83022015-06-02 15:37:36 +03001156 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001157 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001158
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001159 if (HAS_L3_DPF(dev))
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001160 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001161
Mika Kuoppala72253422014-10-07 17:21:26 +03001162 return init_workarounds_ring(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001163}
1164
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001165static void render_ring_cleanup(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001166{
Daniel Vetterb45305f2012-12-17 16:21:27 +01001167 struct drm_device *dev = ring->dev;
Ben Widawsky3e789982014-06-30 09:53:37 -07001168 struct drm_i915_private *dev_priv = dev->dev_private;
1169
1170 if (dev_priv->semaphore_obj) {
1171 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1172 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1173 dev_priv->semaphore_obj = NULL;
1174 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001175
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001176 intel_fini_pipe_control(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001177}
1178
John Harrisonf7169682015-05-29 17:44:05 +01001179static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001180 unsigned int num_dwords)
1181{
1182#define MBOX_UPDATE_DWORDS 8
John Harrisonf7169682015-05-29 17:44:05 +01001183 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky3e789982014-06-30 09:53:37 -07001184 struct drm_device *dev = signaller->dev;
1185 struct drm_i915_private *dev_priv = dev->dev_private;
1186 struct intel_engine_cs *waiter;
1187 int i, ret, num_rings;
1188
1189 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1190 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1191#undef MBOX_UPDATE_DWORDS
1192
John Harrison5fb9de12015-05-29 17:44:07 +01001193 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001194 if (ret)
1195 return ret;
1196
1197 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001198 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001199 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1200 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1201 continue;
1202
John Harrisonf7169682015-05-29 17:44:05 +01001203 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky3e789982014-06-30 09:53:37 -07001204 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1205 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1206 PIPE_CONTROL_QW_WRITE |
1207 PIPE_CONTROL_FLUSH_ENABLE);
1208 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1209 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001210 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001211 intel_ring_emit(signaller, 0);
1212 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1213 MI_SEMAPHORE_TARGET(waiter->id));
1214 intel_ring_emit(signaller, 0);
1215 }
1216
1217 return 0;
1218}
1219
John Harrisonf7169682015-05-29 17:44:05 +01001220static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001221 unsigned int num_dwords)
1222{
1223#define MBOX_UPDATE_DWORDS 6
John Harrisonf7169682015-05-29 17:44:05 +01001224 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky3e789982014-06-30 09:53:37 -07001225 struct drm_device *dev = signaller->dev;
1226 struct drm_i915_private *dev_priv = dev->dev_private;
1227 struct intel_engine_cs *waiter;
1228 int i, ret, num_rings;
1229
1230 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1231 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1232#undef MBOX_UPDATE_DWORDS
1233
John Harrison5fb9de12015-05-29 17:44:07 +01001234 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky3e789982014-06-30 09:53:37 -07001235 if (ret)
1236 return ret;
1237
1238 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001239 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001240 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1241 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1242 continue;
1243
John Harrisonf7169682015-05-29 17:44:05 +01001244 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky3e789982014-06-30 09:53:37 -07001245 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1246 MI_FLUSH_DW_OP_STOREDW);
1247 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1248 MI_FLUSH_DW_USE_GTT);
1249 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001250 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001251 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1252 MI_SEMAPHORE_TARGET(waiter->id));
1253 intel_ring_emit(signaller, 0);
1254 }
1255
1256 return 0;
1257}
1258
John Harrisonf7169682015-05-29 17:44:05 +01001259static int gen6_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001260 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001261{
John Harrisonf7169682015-05-29 17:44:05 +01001262 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001263 struct drm_device *dev = signaller->dev;
1264 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001265 struct intel_engine_cs *useless;
Ben Widawskya1444b72014-06-30 09:53:35 -07001266 int i, ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001267
Ben Widawskya1444b72014-06-30 09:53:35 -07001268#define MBOX_UPDATE_DWORDS 3
1269 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1270 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1271#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001272
John Harrison5fb9de12015-05-29 17:44:07 +01001273 ret = intel_ring_begin(signaller_req, num_dwords);
Ben Widawsky024a43e2014-04-29 14:52:30 -07001274 if (ret)
1275 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001276
Ben Widawsky78325f22014-04-29 14:52:29 -07001277 for_each_ring(useless, dev_priv, i) {
1278 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1279 if (mbox_reg != GEN6_NOSYNC) {
John Harrisonf7169682015-05-29 17:44:05 +01001280 u32 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky78325f22014-04-29 14:52:29 -07001281 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1282 intel_ring_emit(signaller, mbox_reg);
John Harrison6259cea2014-11-24 18:49:29 +00001283 intel_ring_emit(signaller, seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001284 }
1285 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001286
Ben Widawskya1444b72014-06-30 09:53:35 -07001287 /* If num_dwords was rounded, make sure the tail pointer is correct */
1288 if (num_rings % 2 == 0)
1289 intel_ring_emit(signaller, MI_NOOP);
1290
Ben Widawsky024a43e2014-04-29 14:52:30 -07001291 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001292}
1293
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001294/**
1295 * gen6_add_request - Update the semaphore mailbox registers
John Harrisonee044a82015-05-29 17:44:00 +01001296 *
1297 * @request - request to write to the ring
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001298 *
1299 * Update the mailbox registers in the *other* rings with the current seqno.
1300 * This acts like a signal in the canonical semaphore.
1301 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001302static int
John Harrisonee044a82015-05-29 17:44:00 +01001303gen6_add_request(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001304{
John Harrisonee044a82015-05-29 17:44:00 +01001305 struct intel_engine_cs *ring = req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001306 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001307
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001308 if (ring->semaphore.signal)
John Harrisonf7169682015-05-29 17:44:05 +01001309 ret = ring->semaphore.signal(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001310 else
John Harrison5fb9de12015-05-29 17:44:07 +01001311 ret = intel_ring_begin(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001312
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001313 if (ret)
1314 return ret;
1315
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001316 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1317 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrisonee044a82015-05-29 17:44:00 +01001318 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001319 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001320 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001321
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001322 return 0;
1323}
1324
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001325static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1326 u32 seqno)
1327{
1328 struct drm_i915_private *dev_priv = dev->dev_private;
1329 return dev_priv->last_seqno < seqno;
1330}
1331
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001332/**
1333 * intel_ring_sync - sync the waiter to the signaller on seqno
1334 *
1335 * @waiter - ring that is waiting
1336 * @signaller - ring which has, or will signal
1337 * @seqno - seqno which the waiter will block on
1338 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001339
1340static int
John Harrison599d9242015-05-29 17:44:04 +01001341gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001342 struct intel_engine_cs *signaller,
1343 u32 seqno)
1344{
John Harrison599d9242015-05-29 17:44:04 +01001345 struct intel_engine_cs *waiter = waiter_req->ring;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001346 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
1347 int ret;
1348
John Harrison5fb9de12015-05-29 17:44:07 +01001349 ret = intel_ring_begin(waiter_req, 4);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001350 if (ret)
1351 return ret;
1352
1353 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1354 MI_SEMAPHORE_GLOBAL_GTT |
Ben Widawskybae4fcd2014-06-30 09:53:43 -07001355 MI_SEMAPHORE_POLL |
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001356 MI_SEMAPHORE_SAD_GTE_SDD);
1357 intel_ring_emit(waiter, seqno);
1358 intel_ring_emit(waiter,
1359 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1360 intel_ring_emit(waiter,
1361 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1362 intel_ring_advance(waiter);
1363 return 0;
1364}
1365
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001366static int
John Harrison599d9242015-05-29 17:44:04 +01001367gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001368 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001369 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001370{
John Harrison599d9242015-05-29 17:44:04 +01001371 struct intel_engine_cs *waiter = waiter_req->ring;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001372 u32 dw1 = MI_SEMAPHORE_MBOX |
1373 MI_SEMAPHORE_COMPARE |
1374 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001375 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1376 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001377
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001378 /* Throughout all of the GEM code, seqno passed implies our current
1379 * seqno is >= the last seqno executed. However for hardware the
1380 * comparison is strictly greater than.
1381 */
1382 seqno -= 1;
1383
Ben Widawskyebc348b2014-04-29 14:52:28 -07001384 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001385
John Harrison5fb9de12015-05-29 17:44:07 +01001386 ret = intel_ring_begin(waiter_req, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001387 if (ret)
1388 return ret;
1389
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001390 /* If seqno wrap happened, omit the wait with no-ops */
1391 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001392 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001393 intel_ring_emit(waiter, seqno);
1394 intel_ring_emit(waiter, 0);
1395 intel_ring_emit(waiter, MI_NOOP);
1396 } else {
1397 intel_ring_emit(waiter, MI_NOOP);
1398 intel_ring_emit(waiter, MI_NOOP);
1399 intel_ring_emit(waiter, MI_NOOP);
1400 intel_ring_emit(waiter, MI_NOOP);
1401 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001402 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001403
1404 return 0;
1405}
1406
Chris Wilsonc6df5412010-12-15 09:56:50 +00001407#define PIPE_CONTROL_FLUSH(ring__, addr__) \
1408do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001409 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1410 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +00001411 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1412 intel_ring_emit(ring__, 0); \
1413 intel_ring_emit(ring__, 0); \
1414} while (0)
1415
1416static int
John Harrisonee044a82015-05-29 17:44:00 +01001417pc_render_add_request(struct drm_i915_gem_request *req)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001418{
John Harrisonee044a82015-05-29 17:44:00 +01001419 struct intel_engine_cs *ring = req->ring;
Chris Wilson18393f62014-04-09 09:19:40 +01001420 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001421 int ret;
1422
1423 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1424 * incoherent with writes to memory, i.e. completely fubar,
1425 * so we need to use PIPE_NOTIFY instead.
1426 *
1427 * However, we also need to workaround the qword write
1428 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1429 * memory before requesting an interrupt.
1430 */
John Harrison5fb9de12015-05-29 17:44:07 +01001431 ret = intel_ring_begin(req, 32);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001432 if (ret)
1433 return ret;
1434
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001435 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001436 PIPE_CONTROL_WRITE_FLUSH |
1437 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001438 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrisonee044a82015-05-29 17:44:00 +01001439 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001440 intel_ring_emit(ring, 0);
1441 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001442 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +00001443 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001444 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001445 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001446 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001447 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001448 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001449 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001450 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001451 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001452
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001453 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001454 PIPE_CONTROL_WRITE_FLUSH |
1455 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001456 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001457 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrisonee044a82015-05-29 17:44:00 +01001458 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001459 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +01001460 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001461
Chris Wilsonc6df5412010-12-15 09:56:50 +00001462 return 0;
1463}
1464
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001465static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001466gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001467{
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001468 /* Workaround to force correct ordering between irq and seqno writes on
1469 * ivb (and maybe also on snb) by reading from a CS register (like
1470 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +00001471 if (!lazy_coherency) {
1472 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1473 POSTING_READ(RING_ACTHD(ring->mmio_base));
1474 }
1475
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001476 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1477}
1478
1479static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001480ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001481{
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001482 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1483}
1484
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001485static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001486ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001487{
1488 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1489}
1490
Chris Wilsonc6df5412010-12-15 09:56:50 +00001491static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001492pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001493{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001494 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001495}
1496
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001497static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001498pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001499{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001500 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001501}
1502
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001503static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001504gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001505{
1506 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001507 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001508 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001509
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001510 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Daniel Vettere48d8632012-04-11 22:12:54 +02001511 return false;
1512
Chris Wilson7338aef2012-04-24 21:48:47 +01001513 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001514 if (ring->irq_refcount++ == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001515 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001516 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001517
1518 return true;
1519}
1520
1521static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001522gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001523{
1524 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001525 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001526 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001527
Chris Wilson7338aef2012-04-24 21:48:47 +01001528 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001529 if (--ring->irq_refcount == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001530 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001531 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001532}
1533
1534static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001535i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001536{
Chris Wilson78501ea2010-10-27 12:18:21 +01001537 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001538 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001539 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001540
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001541 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001542 return false;
1543
Chris Wilson7338aef2012-04-24 21:48:47 +01001544 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001545 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001546 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1547 I915_WRITE(IMR, dev_priv->irq_mask);
1548 POSTING_READ(IMR);
1549 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001550 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001551
1552 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001553}
1554
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001555static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001556i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001557{
Chris Wilson78501ea2010-10-27 12:18:21 +01001558 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001559 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001560 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001561
Chris Wilson7338aef2012-04-24 21:48:47 +01001562 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001563 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001564 dev_priv->irq_mask |= ring->irq_enable_mask;
1565 I915_WRITE(IMR, dev_priv->irq_mask);
1566 POSTING_READ(IMR);
1567 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001568 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001569}
1570
Chris Wilsonc2798b12012-04-22 21:13:57 +01001571static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001572i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001573{
1574 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001575 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001576 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001577
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001578 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonc2798b12012-04-22 21:13:57 +01001579 return false;
1580
Chris Wilson7338aef2012-04-24 21:48:47 +01001581 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001582 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001583 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1584 I915_WRITE16(IMR, dev_priv->irq_mask);
1585 POSTING_READ16(IMR);
1586 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001587 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001588
1589 return true;
1590}
1591
1592static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001593i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001594{
1595 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001596 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001597 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001598
Chris Wilson7338aef2012-04-24 21:48:47 +01001599 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001600 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001601 dev_priv->irq_mask |= ring->irq_enable_mask;
1602 I915_WRITE16(IMR, dev_priv->irq_mask);
1603 POSTING_READ16(IMR);
1604 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001605 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001606}
1607
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001608static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001609bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001610 u32 invalidate_domains,
1611 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001612{
John Harrisona84c3ae2015-05-29 17:43:57 +01001613 struct intel_engine_cs *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001614 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001615
John Harrison5fb9de12015-05-29 17:44:07 +01001616 ret = intel_ring_begin(req, 2);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001617 if (ret)
1618 return ret;
1619
1620 intel_ring_emit(ring, MI_FLUSH);
1621 intel_ring_emit(ring, MI_NOOP);
1622 intel_ring_advance(ring);
1623 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001624}
1625
Chris Wilson3cce4692010-10-27 16:11:02 +01001626static int
John Harrisonee044a82015-05-29 17:44:00 +01001627i9xx_add_request(struct drm_i915_gem_request *req)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001628{
John Harrisonee044a82015-05-29 17:44:00 +01001629 struct intel_engine_cs *ring = req->ring;
Chris Wilson3cce4692010-10-27 16:11:02 +01001630 int ret;
1631
John Harrison5fb9de12015-05-29 17:44:07 +01001632 ret = intel_ring_begin(req, 4);
Chris Wilson3cce4692010-10-27 16:11:02 +01001633 if (ret)
1634 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +01001635
Chris Wilson3cce4692010-10-27 16:11:02 +01001636 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1637 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrisonee044a82015-05-29 17:44:00 +01001638 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilson3cce4692010-10-27 16:11:02 +01001639 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001640 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001641
Chris Wilson3cce4692010-10-27 16:11:02 +01001642 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001643}
1644
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001645static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001646gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001647{
1648 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001649 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001650 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001651
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001652 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1653 return false;
Chris Wilson0f468322011-01-04 17:35:21 +00001654
Chris Wilson7338aef2012-04-24 21:48:47 +01001655 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001656 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001657 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001658 I915_WRITE_IMR(ring,
1659 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001660 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001661 else
1662 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001663 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001664 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001665 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001666
1667 return true;
1668}
1669
1670static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001671gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001672{
1673 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001674 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001675 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001676
Chris Wilson7338aef2012-04-24 21:48:47 +01001677 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001678 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001679 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001680 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001681 else
1682 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001683 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001684 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001685 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001686}
1687
Ben Widawskya19d2932013-05-28 19:22:30 -07001688static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001689hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001690{
1691 struct drm_device *dev = ring->dev;
1692 struct drm_i915_private *dev_priv = dev->dev_private;
1693 unsigned long flags;
1694
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001695 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskya19d2932013-05-28 19:22:30 -07001696 return false;
1697
Daniel Vetter59cdb632013-07-04 23:35:28 +02001698 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001699 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001700 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001701 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001702 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001703 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001704
1705 return true;
1706}
1707
1708static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001709hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001710{
1711 struct drm_device *dev = ring->dev;
1712 struct drm_i915_private *dev_priv = dev->dev_private;
1713 unsigned long flags;
1714
Daniel Vetter59cdb632013-07-04 23:35:28 +02001715 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001716 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001717 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001718 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001719 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001720 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001721}
1722
Ben Widawskyabd58f02013-11-02 21:07:09 -07001723static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001724gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001725{
1726 struct drm_device *dev = ring->dev;
1727 struct drm_i915_private *dev_priv = dev->dev_private;
1728 unsigned long flags;
1729
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001730 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskyabd58f02013-11-02 21:07:09 -07001731 return false;
1732
1733 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1734 if (ring->irq_refcount++ == 0) {
1735 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1736 I915_WRITE_IMR(ring,
1737 ~(ring->irq_enable_mask |
1738 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1739 } else {
1740 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1741 }
1742 POSTING_READ(RING_IMR(ring->mmio_base));
1743 }
1744 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1745
1746 return true;
1747}
1748
1749static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001750gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001751{
1752 struct drm_device *dev = ring->dev;
1753 struct drm_i915_private *dev_priv = dev->dev_private;
1754 unsigned long flags;
1755
1756 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1757 if (--ring->irq_refcount == 0) {
1758 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1759 I915_WRITE_IMR(ring,
1760 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1761 } else {
1762 I915_WRITE_IMR(ring, ~0);
1763 }
1764 POSTING_READ(RING_IMR(ring->mmio_base));
1765 }
1766 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1767}
1768
Zou Nan haid1b851f2010-05-21 09:08:57 +08001769static int
John Harrison53fddaf2015-05-29 17:44:02 +01001770i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001771 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001772 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001773{
John Harrison53fddaf2015-05-29 17:44:02 +01001774 struct intel_engine_cs *ring = req->ring;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001775 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001776
John Harrison5fb9de12015-05-29 17:44:07 +01001777 ret = intel_ring_begin(req, 2);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001778 if (ret)
1779 return ret;
1780
Chris Wilson78501ea2010-10-27 12:18:21 +01001781 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001782 MI_BATCH_BUFFER_START |
1783 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001784 (dispatch_flags & I915_DISPATCH_SECURE ?
1785 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001786 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001787 intel_ring_advance(ring);
1788
Zou Nan haid1b851f2010-05-21 09:08:57 +08001789 return 0;
1790}
1791
Daniel Vetterb45305f2012-12-17 16:21:27 +01001792/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1793#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001794#define I830_TLB_ENTRIES (2)
1795#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001796static int
John Harrison53fddaf2015-05-29 17:44:02 +01001797i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001798 u64 offset, u32 len,
1799 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001800{
John Harrison53fddaf2015-05-29 17:44:02 +01001801 struct intel_engine_cs *ring = req->ring;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001802 u32 cs_offset = ring->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001803 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001804
John Harrison5fb9de12015-05-29 17:44:07 +01001805 ret = intel_ring_begin(req, 6);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001806 if (ret)
1807 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001808
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001809 /* Evict the invalid PTE TLBs */
1810 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1811 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1812 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1813 intel_ring_emit(ring, cs_offset);
1814 intel_ring_emit(ring, 0xdeadbeef);
1815 intel_ring_emit(ring, MI_NOOP);
1816 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001817
John Harrison8e004ef2015-02-13 11:48:10 +00001818 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001819 if (len > I830_BATCH_LIMIT)
1820 return -ENOSPC;
1821
John Harrison5fb9de12015-05-29 17:44:07 +01001822 ret = intel_ring_begin(req, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001823 if (ret)
1824 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001825
1826 /* Blit the batch (which has now all relocs applied) to the
1827 * stable batch scratch bo area (so that the CS never
1828 * stumbles over its tlb invalidation bug) ...
1829 */
1830 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1831 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilson611a7a42014-09-12 07:37:42 +01001832 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001833 intel_ring_emit(ring, cs_offset);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001834 intel_ring_emit(ring, 4096);
1835 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001836
Daniel Vetterb45305f2012-12-17 16:21:27 +01001837 intel_ring_emit(ring, MI_FLUSH);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001838 intel_ring_emit(ring, MI_NOOP);
1839 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001840
1841 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001842 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001843 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001844
John Harrison5fb9de12015-05-29 17:44:07 +01001845 ret = intel_ring_begin(req, 4);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001846 if (ret)
1847 return ret;
1848
1849 intel_ring_emit(ring, MI_BATCH_BUFFER);
John Harrison8e004ef2015-02-13 11:48:10 +00001850 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1851 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001852 intel_ring_emit(ring, offset + len - 8);
1853 intel_ring_emit(ring, MI_NOOP);
1854 intel_ring_advance(ring);
1855
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001856 return 0;
1857}
1858
1859static int
John Harrison53fddaf2015-05-29 17:44:02 +01001860i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001861 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001862 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001863{
John Harrison53fddaf2015-05-29 17:44:02 +01001864 struct intel_engine_cs *ring = req->ring;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001865 int ret;
1866
John Harrison5fb9de12015-05-29 17:44:07 +01001867 ret = intel_ring_begin(req, 2);
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001868 if (ret)
1869 return ret;
1870
Chris Wilson65f56872012-04-17 16:38:12 +01001871 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
John Harrison8e004ef2015-02-13 11:48:10 +00001872 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1873 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001874 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001875
Eric Anholt62fdfea2010-05-21 13:26:39 -07001876 return 0;
1877}
1878
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001879static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001880{
Chris Wilson05394f32010-11-08 19:18:58 +00001881 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001882
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001883 obj = ring->status_page.obj;
1884 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001885 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001886
Chris Wilson9da3da62012-06-01 15:20:22 +01001887 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001888 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001889 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001890 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001891}
1892
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001893static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001894{
Chris Wilson05394f32010-11-08 19:18:58 +00001895 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001896
Chris Wilsone3efda42014-04-09 09:19:41 +01001897 if ((obj = ring->status_page.obj) == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001898 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001899 int ret;
1900
1901 obj = i915_gem_alloc_object(ring->dev, 4096);
1902 if (obj == NULL) {
1903 DRM_ERROR("Failed to allocate status page\n");
1904 return -ENOMEM;
1905 }
1906
1907 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1908 if (ret)
1909 goto err_unref;
1910
Chris Wilson1f767e02014-07-03 17:33:03 -04001911 flags = 0;
1912 if (!HAS_LLC(ring->dev))
1913 /* On g33, we cannot place HWS above 256MiB, so
1914 * restrict its pinning to the low mappable arena.
1915 * Though this restriction is not documented for
1916 * gen4, gen5, or byt, they also behave similarly
1917 * and hang if the HWS is placed at the top of the
1918 * GTT. To generalise, it appears that all !llc
1919 * platforms have issues with us placing the HWS
1920 * above the mappable region (even though we never
1921 * actualy map it).
1922 */
1923 flags |= PIN_MAPPABLE;
1924 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001925 if (ret) {
1926err_unref:
1927 drm_gem_object_unreference(&obj->base);
1928 return ret;
1929 }
1930
1931 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001932 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001933
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001934 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001935 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001936 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001937
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001938 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1939 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001940
1941 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001942}
1943
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001944static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001945{
1946 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001947
1948 if (!dev_priv->status_page_dmah) {
1949 dev_priv->status_page_dmah =
1950 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1951 if (!dev_priv->status_page_dmah)
1952 return -ENOMEM;
1953 }
1954
Chris Wilson6b8294a2012-11-16 11:43:20 +00001955 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1956 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1957
1958 return 0;
1959}
1960
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001961void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1962{
1963 iounmap(ringbuf->virtual_start);
1964 ringbuf->virtual_start = NULL;
1965 i915_gem_object_ggtt_unpin(ringbuf->obj);
1966}
1967
1968int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
1969 struct intel_ringbuffer *ringbuf)
1970{
1971 struct drm_i915_private *dev_priv = to_i915(dev);
1972 struct drm_i915_gem_object *obj = ringbuf->obj;
1973 int ret;
1974
1975 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1976 if (ret)
1977 return ret;
1978
1979 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1980 if (ret) {
1981 i915_gem_object_ggtt_unpin(obj);
1982 return ret;
1983 }
1984
1985 ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
1986 i915_gem_obj_ggtt_offset(obj), ringbuf->size);
1987 if (ringbuf->virtual_start == NULL) {
1988 i915_gem_object_ggtt_unpin(obj);
1989 return -EINVAL;
1990 }
1991
1992 return 0;
1993}
1994
Chris Wilson01101fa2015-09-03 13:01:39 +01001995static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01001996{
Oscar Mateo2919d292014-07-03 16:28:02 +01001997 drm_gem_object_unreference(&ringbuf->obj->base);
1998 ringbuf->obj = NULL;
1999}
2000
Chris Wilson01101fa2015-09-03 13:01:39 +01002001static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
2002 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01002003{
Chris Wilsone3efda42014-04-09 09:19:41 +01002004 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002005
2006 obj = NULL;
2007 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002008 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002009 if (obj == NULL)
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002010 obj = i915_gem_alloc_object(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002011 if (obj == NULL)
2012 return -ENOMEM;
2013
Akash Goel24f3a8c2014-06-17 10:59:42 +05302014 /* mark ring buffers as read-only from GPU side by default */
2015 obj->gt_ro = 1;
2016
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002017 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002018
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002019 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002020}
2021
Chris Wilson01101fa2015-09-03 13:01:39 +01002022struct intel_ringbuffer *
2023intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
2024{
2025 struct intel_ringbuffer *ring;
2026 int ret;
2027
2028 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2029 if (ring == NULL)
2030 return ERR_PTR(-ENOMEM);
2031
2032 ring->ring = engine;
2033
2034 ring->size = size;
2035 /* Workaround an erratum on the i830 which causes a hang if
2036 * the TAIL pointer points to within the last 2 cachelines
2037 * of the buffer.
2038 */
2039 ring->effective_size = size;
2040 if (IS_I830(engine->dev) || IS_845G(engine->dev))
2041 ring->effective_size -= 2 * CACHELINE_BYTES;
2042
2043 ring->last_retired_head = -1;
2044 intel_ring_update_space(ring);
2045
2046 ret = intel_alloc_ringbuffer_obj(engine->dev, ring);
2047 if (ret) {
2048 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
2049 engine->name, ret);
2050 kfree(ring);
2051 return ERR_PTR(ret);
2052 }
2053
2054 return ring;
2055}
2056
2057void
2058intel_ringbuffer_free(struct intel_ringbuffer *ring)
2059{
2060 intel_destroy_ringbuffer_obj(ring);
2061 kfree(ring);
2062}
2063
Ben Widawskyc43b5632012-04-16 14:07:40 -07002064static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002065 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002066{
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002067 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002068 int ret;
2069
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002070 WARN_ON(ring->buffer);
2071
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002072 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01002073 INIT_LIST_HEAD(&ring->active_list);
2074 INIT_LIST_HEAD(&ring->request_list);
Oscar Mateocc9130b2014-07-24 17:04:42 +01002075 INIT_LIST_HEAD(&ring->execlist_queue);
Chris Wilson06fbca72015-04-07 16:20:36 +01002076 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Ben Widawskyebc348b2014-04-29 14:52:28 -07002077 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002078
Chris Wilsonb259f672011-03-29 13:19:09 +01002079 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002080
Chris Wilson01101fa2015-09-03 13:01:39 +01002081 ringbuf = intel_engine_create_ringbuffer(ring, 32 * PAGE_SIZE);
2082 if (IS_ERR(ringbuf))
2083 return PTR_ERR(ringbuf);
2084 ring->buffer = ringbuf;
2085
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002086 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002087 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002088 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002089 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002090 } else {
2091 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002092 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002093 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002094 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002095 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002096
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002097 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2098 if (ret) {
2099 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2100 ring->name, ret);
2101 intel_destroy_ringbuffer_obj(ringbuf);
2102 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002103 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002104
Brad Volkin44e895a2014-05-10 14:10:43 -07002105 ret = i915_cmd_parser_init_ring(ring);
2106 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002107 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002108
Oscar Mateo8ee14972014-05-22 14:13:34 +01002109 return 0;
2110
2111error:
Chris Wilson01101fa2015-09-03 13:01:39 +01002112 intel_ringbuffer_free(ringbuf);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002113 ring->buffer = NULL;
2114 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002115}
2116
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002117void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002118{
John Harrison6402c332014-10-31 12:00:26 +00002119 struct drm_i915_private *dev_priv;
Chris Wilson33626e62010-10-29 16:18:36 +01002120
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002121 if (!intel_ring_initialized(ring))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002122 return;
2123
John Harrison6402c332014-10-31 12:00:26 +00002124 dev_priv = to_i915(ring->dev);
John Harrison6402c332014-10-31 12:00:26 +00002125
Chris Wilsone3efda42014-04-09 09:19:41 +01002126 intel_stop_ring_buffer(ring);
Ville Syrjäläde8f0a52014-05-28 19:12:13 +03002127 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002128
Chris Wilson01101fa2015-09-03 13:01:39 +01002129 intel_unpin_ringbuffer_obj(ring->buffer);
2130 intel_ringbuffer_free(ring->buffer);
2131 ring->buffer = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +01002132
Zou Nan hai8d192152010-11-02 16:31:01 +08002133 if (ring->cleanup)
2134 ring->cleanup(ring);
2135
Chris Wilson78501ea2010-10-27 12:18:21 +01002136 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07002137
2138 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01002139 i915_gem_batch_pool_fini(&ring->batch_pool);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002140}
2141
Chris Wilson595e1ee2015-04-07 16:20:51 +01002142static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002143{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002144 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002145 struct drm_i915_gem_request *request;
Chris Wilsonb4716182015-04-27 13:41:17 +01002146 unsigned space;
2147 int ret;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002148
Dave Gordonebd0fd42014-11-27 11:22:49 +00002149 if (intel_ring_space(ringbuf) >= n)
2150 return 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002151
John Harrison79bbcc22015-06-30 12:40:55 +01002152 /* The whole point of reserving space is to not wait! */
2153 WARN_ON(ringbuf->reserved_in_use);
2154
Chris Wilsona71d8d92012-02-15 11:25:36 +00002155 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002156 space = __intel_ring_space(request->postfix, ringbuf->tail,
2157 ringbuf->size);
2158 if (space >= n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002159 break;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002160 }
2161
Chris Wilson595e1ee2015-04-07 16:20:51 +01002162 if (WARN_ON(&request->list == &ring->request_list))
Chris Wilsona71d8d92012-02-15 11:25:36 +00002163 return -ENOSPC;
2164
Daniel Vettera4b3a572014-11-26 14:17:05 +01002165 ret = i915_wait_request(request);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002166 if (ret)
2167 return ret;
2168
Chris Wilsonb4716182015-04-27 13:41:17 +01002169 ringbuf->space = space;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002170 return 0;
2171}
2172
John Harrison79bbcc22015-06-30 12:40:55 +01002173static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
Chris Wilson3e960502012-11-27 16:22:54 +00002174{
2175 uint32_t __iomem *virt;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002176 int rem = ringbuf->size - ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002177
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002178 virt = ringbuf->virtual_start + ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002179 rem /= 4;
2180 while (rem--)
2181 iowrite32(MI_NOOP, virt++);
2182
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002183 ringbuf->tail = 0;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002184 intel_ring_update_space(ringbuf);
Chris Wilson3e960502012-11-27 16:22:54 +00002185}
2186
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002187int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002188{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002189 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002190
Chris Wilson3e960502012-11-27 16:22:54 +00002191 /* Wait upon the last request to be completed */
2192 if (list_empty(&ring->request_list))
2193 return 0;
2194
Daniel Vettera4b3a572014-11-26 14:17:05 +01002195 req = list_entry(ring->request_list.prev,
Chris Wilsonb4716182015-04-27 13:41:17 +01002196 struct drm_i915_gem_request,
2197 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002198
Chris Wilsonb4716182015-04-27 13:41:17 +01002199 /* Make sure we do not trigger any retires */
2200 return __i915_wait_request(req,
2201 atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
2202 to_i915(ring->dev)->mm.interruptible,
2203 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002204}
2205
John Harrison6689cb22015-03-19 12:30:08 +00002206int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002207{
John Harrison6689cb22015-03-19 12:30:08 +00002208 request->ringbuf = request->ring->buffer;
John Harrison9eba5d42014-11-24 18:49:23 +00002209 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002210}
2211
John Harrisonccd98fe2015-05-29 17:44:09 +01002212int intel_ring_reserve_space(struct drm_i915_gem_request *request)
2213{
2214 /*
2215 * The first call merely notes the reserve request and is common for
2216 * all back ends. The subsequent localised _begin() call actually
2217 * ensures that the reservation is available. Without the begin, if
2218 * the request creator immediately submitted the request without
2219 * adding any commands to it then there might not actually be
2220 * sufficient room for the submission commands.
2221 */
2222 intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
2223
2224 return intel_ring_begin(request, 0);
2225}
2226
John Harrison29b1b412015-06-18 13:10:09 +01002227void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2228{
John Harrisonccd98fe2015-05-29 17:44:09 +01002229 WARN_ON(ringbuf->reserved_size);
John Harrison29b1b412015-06-18 13:10:09 +01002230 WARN_ON(ringbuf->reserved_in_use);
2231
2232 ringbuf->reserved_size = size;
John Harrison29b1b412015-06-18 13:10:09 +01002233}
2234
2235void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2236{
2237 WARN_ON(ringbuf->reserved_in_use);
2238
2239 ringbuf->reserved_size = 0;
2240 ringbuf->reserved_in_use = false;
2241}
2242
2243void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2244{
2245 WARN_ON(ringbuf->reserved_in_use);
2246
2247 ringbuf->reserved_in_use = true;
2248 ringbuf->reserved_tail = ringbuf->tail;
2249}
2250
2251void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2252{
2253 WARN_ON(!ringbuf->reserved_in_use);
John Harrison79bbcc22015-06-30 12:40:55 +01002254 if (ringbuf->tail > ringbuf->reserved_tail) {
2255 WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2256 "request reserved size too small: %d vs %d!\n",
2257 ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2258 } else {
2259 /*
2260 * The ring was wrapped while the reserved space was in use.
2261 * That means that some unknown amount of the ring tail was
2262 * no-op filled and skipped. Thus simply adding the ring size
2263 * to the tail and doing the above space check will not work.
2264 * Rather than attempt to track how much tail was skipped,
2265 * it is much simpler to say that also skipping the sanity
2266 * check every once in a while is not a big issue.
2267 */
2268 }
John Harrison29b1b412015-06-18 13:10:09 +01002269
2270 ringbuf->reserved_size = 0;
2271 ringbuf->reserved_in_use = false;
2272}
2273
2274static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002275{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002276 struct intel_ringbuffer *ringbuf = ring->buffer;
John Harrison79bbcc22015-06-30 12:40:55 +01002277 int remain_usable = ringbuf->effective_size - ringbuf->tail;
2278 int remain_actual = ringbuf->size - ringbuf->tail;
2279 int ret, total_bytes, wait_bytes = 0;
2280 bool need_wrap = false;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002281
John Harrison79bbcc22015-06-30 12:40:55 +01002282 if (ringbuf->reserved_in_use)
2283 total_bytes = bytes;
2284 else
2285 total_bytes = bytes + ringbuf->reserved_size;
John Harrison29b1b412015-06-18 13:10:09 +01002286
John Harrison79bbcc22015-06-30 12:40:55 +01002287 if (unlikely(bytes > remain_usable)) {
2288 /*
2289 * Not enough space for the basic request. So need to flush
2290 * out the remainder and then wait for base + reserved.
2291 */
2292 wait_bytes = remain_actual + total_bytes;
2293 need_wrap = true;
2294 } else {
2295 if (unlikely(total_bytes > remain_usable)) {
2296 /*
2297 * The base request will fit but the reserved space
2298 * falls off the end. So only need to to wait for the
2299 * reserved size after flushing out the remainder.
2300 */
2301 wait_bytes = remain_actual + ringbuf->reserved_size;
2302 need_wrap = true;
2303 } else if (total_bytes > ringbuf->space) {
2304 /* No wrapping required, just waiting. */
2305 wait_bytes = total_bytes;
John Harrison29b1b412015-06-18 13:10:09 +01002306 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002307 }
2308
John Harrison79bbcc22015-06-30 12:40:55 +01002309 if (wait_bytes) {
2310 ret = ring_wait_for_space(ring, wait_bytes);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002311 if (unlikely(ret))
2312 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +01002313
2314 if (need_wrap)
2315 __wrap_ring_buffer(ringbuf);
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002316 }
2317
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002318 return 0;
2319}
2320
John Harrison5fb9de12015-05-29 17:44:07 +01002321int intel_ring_begin(struct drm_i915_gem_request *req,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002322 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002323{
John Harrison5fb9de12015-05-29 17:44:07 +01002324 struct intel_engine_cs *ring;
2325 struct drm_i915_private *dev_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002326 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01002327
John Harrison5fb9de12015-05-29 17:44:07 +01002328 WARN_ON(req == NULL);
2329 ring = req->ring;
2330 dev_priv = ring->dev->dev_private;
2331
Daniel Vetter33196de2012-11-14 17:14:05 +01002332 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2333 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02002334 if (ret)
2335 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00002336
Chris Wilson304d6952014-01-02 14:32:35 +00002337 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
2338 if (ret)
2339 return ret;
2340
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002341 ring->buffer->space -= num_dwords * sizeof(uint32_t);
Chris Wilson304d6952014-01-02 14:32:35 +00002342 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002343}
2344
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002345/* Align the ring tail to a cacheline boundary */
John Harrisonbba09b12015-05-29 17:44:06 +01002346int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002347{
John Harrisonbba09b12015-05-29 17:44:06 +01002348 struct intel_engine_cs *ring = req->ring;
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002349 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002350 int ret;
2351
2352 if (num_dwords == 0)
2353 return 0;
2354
Chris Wilson18393f62014-04-09 09:19:40 +01002355 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
John Harrison5fb9de12015-05-29 17:44:07 +01002356 ret = intel_ring_begin(req, num_dwords);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002357 if (ret)
2358 return ret;
2359
2360 while (num_dwords--)
2361 intel_ring_emit(ring, MI_NOOP);
2362
2363 intel_ring_advance(ring);
2364
2365 return 0;
2366}
2367
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002368void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002369{
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002370 struct drm_device *dev = ring->dev;
2371 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002372
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002373 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002374 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2375 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002376 if (HAS_VEBOX(dev))
Ben Widawsky50201502013-08-12 16:53:03 -07002377 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002378 }
Chris Wilson297b0c52010-10-22 17:02:41 +01002379
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002380 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03002381 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01002382}
2383
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002384static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002385 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002386{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002387 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002388
2389 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002390
Chris Wilson12f55812012-07-05 17:14:01 +01002391 /* Disable notification that the ring is IDLE. The GT
2392 * will then assume that it is busy and bring it out of rc6.
2393 */
2394 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2395 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2396
2397 /* Clear the context id. Here be magic! */
2398 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2399
2400 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04002401 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01002402 GEN6_BSD_SLEEP_INDICATOR) == 0,
2403 50))
2404 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002405
Chris Wilson12f55812012-07-05 17:14:01 +01002406 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04002407 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01002408 POSTING_READ(RING_TAIL(ring->mmio_base));
2409
2410 /* Let the ring send IDLE messages to the GT again,
2411 * and so let it sleep to conserve power when idle.
2412 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002413 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01002414 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002415}
2416
John Harrisona84c3ae2015-05-29 17:43:57 +01002417static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002418 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002419{
John Harrisona84c3ae2015-05-29 17:43:57 +01002420 struct intel_engine_cs *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002421 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002422 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002423
John Harrison5fb9de12015-05-29 17:44:07 +01002424 ret = intel_ring_begin(req, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002425 if (ret)
2426 return ret;
2427
Chris Wilson71a77e02011-02-02 12:13:49 +00002428 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002429 if (INTEL_INFO(ring->dev)->gen >= 8)
2430 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002431
2432 /* We always require a command barrier so that subsequent
2433 * commands, such as breadcrumb interrupts, are strictly ordered
2434 * wrt the contents of the write cache being flushed to memory
2435 * (and thus being coherent from the CPU).
2436 */
2437 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2438
Jesse Barnes9a289772012-10-26 09:42:42 -07002439 /*
2440 * Bspec vol 1c.5 - video engine command streamer:
2441 * "If ENABLED, all TLBs will be invalidated once the flush
2442 * operation is complete. This bit is only valid when the
2443 * Post-Sync Operation field is a value of 1h or 3h."
2444 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002445 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002446 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2447
Chris Wilson71a77e02011-02-02 12:13:49 +00002448 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002449 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002450 if (INTEL_INFO(ring->dev)->gen >= 8) {
2451 intel_ring_emit(ring, 0); /* upper addr */
2452 intel_ring_emit(ring, 0); /* value */
2453 } else {
2454 intel_ring_emit(ring, 0);
2455 intel_ring_emit(ring, MI_NOOP);
2456 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002457 intel_ring_advance(ring);
2458 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002459}
2460
2461static int
John Harrison53fddaf2015-05-29 17:44:02 +01002462gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002463 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002464 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002465{
John Harrison53fddaf2015-05-29 17:44:02 +01002466 struct intel_engine_cs *ring = req->ring;
John Harrison8e004ef2015-02-13 11:48:10 +00002467 bool ppgtt = USES_PPGTT(ring->dev) &&
2468 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002469 int ret;
2470
John Harrison5fb9de12015-05-29 17:44:07 +01002471 ret = intel_ring_begin(req, 4);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002472 if (ret)
2473 return ret;
2474
2475 /* FIXME(BDW): Address space and security selectors. */
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002476 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
2477 (dispatch_flags & I915_DISPATCH_RS ?
2478 MI_BATCH_RESOURCE_STREAMER : 0));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002479 intel_ring_emit(ring, lower_32_bits(offset));
2480 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002481 intel_ring_emit(ring, MI_NOOP);
2482 intel_ring_advance(ring);
2483
2484 return 0;
2485}
2486
2487static int
John Harrison53fddaf2015-05-29 17:44:02 +01002488hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002489 u64 offset, u32 len,
2490 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002491{
John Harrison53fddaf2015-05-29 17:44:02 +01002492 struct intel_engine_cs *ring = req->ring;
Akshay Joshi0206e352011-08-16 15:34:10 -04002493 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002494
John Harrison5fb9de12015-05-29 17:44:07 +01002495 ret = intel_ring_begin(req, 2);
Akshay Joshi0206e352011-08-16 15:34:10 -04002496 if (ret)
2497 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002498
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002499 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002500 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002501 (dispatch_flags & I915_DISPATCH_SECURE ?
Abdiel Janulgue919032e2015-06-16 13:39:40 +03002502 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
2503 (dispatch_flags & I915_DISPATCH_RS ?
2504 MI_BATCH_RESOURCE_STREAMER : 0));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002505 /* bit0-7 is the length on GEN6+ */
2506 intel_ring_emit(ring, offset);
2507 intel_ring_advance(ring);
2508
2509 return 0;
2510}
2511
2512static int
John Harrison53fddaf2015-05-29 17:44:02 +01002513gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002514 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002515 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002516{
John Harrison53fddaf2015-05-29 17:44:02 +01002517 struct intel_engine_cs *ring = req->ring;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002518 int ret;
2519
John Harrison5fb9de12015-05-29 17:44:07 +01002520 ret = intel_ring_begin(req, 2);
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002521 if (ret)
2522 return ret;
2523
2524 intel_ring_emit(ring,
2525 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002526 (dispatch_flags & I915_DISPATCH_SECURE ?
2527 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002528 /* bit0-7 is the length on GEN6+ */
2529 intel_ring_emit(ring, offset);
2530 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002531
Akshay Joshi0206e352011-08-16 15:34:10 -04002532 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002533}
2534
Chris Wilson549f7362010-10-19 11:19:32 +01002535/* Blitter support (SandyBridge+) */
2536
John Harrisona84c3ae2015-05-29 17:43:57 +01002537static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002538 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002539{
John Harrisona84c3ae2015-05-29 17:43:57 +01002540 struct intel_engine_cs *ring = req->ring;
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002541 struct drm_device *dev = ring->dev;
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;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002550 if (INTEL_INFO(dev)->gen >= 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.3 - blitter 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_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002567 cmd |= MI_INVALIDATE_TLB;
Chris Wilson71a77e02011-02-02 12:13:49 +00002568 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002569 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002570 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002571 intel_ring_emit(ring, 0); /* upper addr */
2572 intel_ring_emit(ring, 0); /* value */
2573 } else {
2574 intel_ring_emit(ring, 0);
2575 intel_ring_emit(ring, MI_NOOP);
2576 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002577 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002578
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002579 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002580}
2581
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002582int intel_init_render_ring_buffer(struct drm_device *dev)
2583{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002584 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002585 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002586 struct drm_i915_gem_object *obj;
2587 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002588
Daniel Vetter59465b52012-04-11 22:12:48 +02002589 ring->name = "render ring";
2590 ring->id = RCS;
2591 ring->mmio_base = RENDER_RING_BASE;
2592
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002593 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002594 if (i915_semaphore_is_enabled(dev)) {
2595 obj = i915_gem_alloc_object(dev, 4096);
2596 if (obj == NULL) {
2597 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2598 i915.semaphores = 0;
2599 } else {
2600 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2601 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2602 if (ret != 0) {
2603 drm_gem_object_unreference(&obj->base);
2604 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2605 i915.semaphores = 0;
2606 } else
2607 dev_priv->semaphore_obj = obj;
2608 }
2609 }
Mika Kuoppala72253422014-10-07 17:21:26 +03002610
Daniel Vetter8f0e2b92014-12-02 16:19:07 +01002611 ring->init_context = intel_rcs_ctx_init;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002612 ring->add_request = gen6_add_request;
2613 ring->flush = gen8_render_ring_flush;
2614 ring->irq_get = gen8_ring_get_irq;
2615 ring->irq_put = gen8_ring_put_irq;
2616 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2617 ring->get_seqno = gen6_ring_get_seqno;
2618 ring->set_seqno = ring_set_seqno;
2619 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002620 WARN_ON(!dev_priv->semaphore_obj);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002621 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002622 ring->semaphore.signal = gen8_rcs_signal;
2623 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002624 }
2625 } else if (INTEL_INFO(dev)->gen >= 6) {
Francisco Jerez4f91fc62015-10-07 14:44:02 +03002626 ring->init_context = intel_rcs_ctx_init;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002627 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03002628 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01002629 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03002630 ring->flush = gen6_render_ring_flush;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002631 ring->irq_get = gen6_ring_get_irq;
2632 ring->irq_put = gen6_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002633 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01002634 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002635 ring->set_seqno = ring_set_seqno;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002636 if (i915_semaphore_is_enabled(dev)) {
2637 ring->semaphore.sync_to = gen6_ring_sync;
2638 ring->semaphore.signal = gen6_signal;
2639 /*
2640 * The current semaphore is only applied on pre-gen8
2641 * platform. And there is no VCS2 ring on the pre-gen8
2642 * platform. So the semaphore between RCS and VCS2 is
2643 * initialized as INVALID. Gen8 will initialize the
2644 * sema between VCS2 and RCS later.
2645 */
2646 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2647 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2648 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2649 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2650 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2651 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2652 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2653 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2654 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2655 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2656 }
Chris Wilsonc6df5412010-12-15 09:56:50 +00002657 } else if (IS_GEN5(dev)) {
2658 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002659 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00002660 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002661 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002662 ring->irq_get = gen5_ring_get_irq;
2663 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002664 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2665 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002666 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002667 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002668 if (INTEL_INFO(dev)->gen < 4)
2669 ring->flush = gen2_render_ring_flush;
2670 else
2671 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002672 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002673 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002674 if (IS_GEN2(dev)) {
2675 ring->irq_get = i8xx_ring_get_irq;
2676 ring->irq_put = i8xx_ring_put_irq;
2677 } else {
2678 ring->irq_get = i9xx_ring_get_irq;
2679 ring->irq_put = i9xx_ring_put_irq;
2680 }
Daniel Vettere3670312012-04-11 22:12:53 +02002681 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002682 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002683 ring->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002684
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002685 if (IS_HASWELL(dev))
2686 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002687 else if (IS_GEN8(dev))
2688 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002689 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002690 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2691 else if (INTEL_INFO(dev)->gen >= 4)
2692 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2693 else if (IS_I830(dev) || IS_845G(dev))
2694 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2695 else
2696 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002697 ring->init_hw = init_render_ring;
Daniel Vetter59465b52012-04-11 22:12:48 +02002698 ring->cleanup = render_ring_cleanup;
2699
Daniel Vetterb45305f2012-12-17 16:21:27 +01002700 /* Workaround batchbuffer to combat CS tlb bug. */
2701 if (HAS_BROKEN_CS_TLB(dev)) {
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002702 obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002703 if (obj == NULL) {
2704 DRM_ERROR("Failed to allocate batch bo\n");
2705 return -ENOMEM;
2706 }
2707
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002708 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002709 if (ret != 0) {
2710 drm_gem_object_unreference(&obj->base);
2711 DRM_ERROR("Failed to ping batch bo\n");
2712 return ret;
2713 }
2714
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002715 ring->scratch.obj = obj;
2716 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002717 }
2718
Daniel Vetter99be1df2014-11-20 00:33:06 +01002719 ret = intel_init_ring_buffer(dev, ring);
2720 if (ret)
2721 return ret;
2722
2723 if (INTEL_INFO(dev)->gen >= 5) {
2724 ret = intel_init_pipe_control(ring);
2725 if (ret)
2726 return ret;
2727 }
2728
2729 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002730}
2731
2732int intel_init_bsd_ring_buffer(struct drm_device *dev)
2733{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002734 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002735 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002736
Daniel Vetter58fa3832012-04-11 22:12:49 +02002737 ring->name = "bsd ring";
2738 ring->id = VCS;
2739
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002740 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002741 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002742 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002743 /* gen6 bsd needs a special wa for tail updates */
2744 if (IS_GEN6(dev))
2745 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002746 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002747 ring->add_request = gen6_add_request;
2748 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002749 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002750 if (INTEL_INFO(dev)->gen >= 8) {
2751 ring->irq_enable_mask =
2752 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2753 ring->irq_get = gen8_ring_get_irq;
2754 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002755 ring->dispatch_execbuffer =
2756 gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002757 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002758 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002759 ring->semaphore.signal = gen8_xcs_signal;
2760 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002761 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002762 } else {
2763 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2764 ring->irq_get = gen6_ring_get_irq;
2765 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002766 ring->dispatch_execbuffer =
2767 gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002768 if (i915_semaphore_is_enabled(dev)) {
2769 ring->semaphore.sync_to = gen6_ring_sync;
2770 ring->semaphore.signal = gen6_signal;
2771 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2772 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2773 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2774 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2775 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2776 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2777 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2778 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2779 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2780 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2781 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002782 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002783 } else {
2784 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002785 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002786 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002787 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002788 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002789 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002790 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002791 ring->irq_get = gen5_ring_get_irq;
2792 ring->irq_put = gen5_ring_put_irq;
2793 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002794 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002795 ring->irq_get = i9xx_ring_get_irq;
2796 ring->irq_put = i9xx_ring_put_irq;
2797 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002798 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002799 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002800 ring->init_hw = init_ring_common;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002801
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002802 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002803}
Chris Wilson549f7362010-10-19 11:19:32 +01002804
Zhao Yakui845f74a2014-04-17 10:37:37 +08002805/**
Damien Lespiau62659922015-01-29 14:13:40 +00002806 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002807 */
2808int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2809{
2810 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002811 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002812
Rodrigo Vivif7b64232014-07-01 02:41:36 -07002813 ring->name = "bsd2 ring";
Zhao Yakui845f74a2014-04-17 10:37:37 +08002814 ring->id = VCS2;
2815
2816 ring->write_tail = ring_write_tail;
2817 ring->mmio_base = GEN8_BSD2_RING_BASE;
2818 ring->flush = gen6_bsd_ring_flush;
2819 ring->add_request = gen6_add_request;
2820 ring->get_seqno = gen6_ring_get_seqno;
2821 ring->set_seqno = ring_set_seqno;
2822 ring->irq_enable_mask =
2823 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2824 ring->irq_get = gen8_ring_get_irq;
2825 ring->irq_put = gen8_ring_put_irq;
2826 ring->dispatch_execbuffer =
2827 gen8_ring_dispatch_execbuffer;
Ben Widawsky3e789982014-06-30 09:53:37 -07002828 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002829 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002830 ring->semaphore.signal = gen8_xcs_signal;
2831 GEN8_RING_SEMAPHORE_INIT;
2832 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002833 ring->init_hw = init_ring_common;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002834
2835 return intel_init_ring_buffer(dev, ring);
2836}
2837
Chris Wilson549f7362010-10-19 11:19:32 +01002838int intel_init_blt_ring_buffer(struct drm_device *dev)
2839{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002840 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002841 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002842
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002843 ring->name = "blitter ring";
2844 ring->id = BCS;
2845
2846 ring->mmio_base = BLT_RING_BASE;
2847 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002848 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002849 ring->add_request = gen6_add_request;
2850 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002851 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002852 if (INTEL_INFO(dev)->gen >= 8) {
2853 ring->irq_enable_mask =
2854 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2855 ring->irq_get = gen8_ring_get_irq;
2856 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002857 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002858 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002859 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002860 ring->semaphore.signal = gen8_xcs_signal;
2861 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002862 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002863 } else {
2864 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2865 ring->irq_get = gen6_ring_get_irq;
2866 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002867 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002868 if (i915_semaphore_is_enabled(dev)) {
2869 ring->semaphore.signal = gen6_signal;
2870 ring->semaphore.sync_to = gen6_ring_sync;
2871 /*
2872 * The current semaphore is only applied on pre-gen8
2873 * platform. And there is no VCS2 ring on the pre-gen8
2874 * platform. So the semaphore between BCS and VCS2 is
2875 * initialized as INVALID. Gen8 will initialize the
2876 * sema between BCS and VCS2 later.
2877 */
2878 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2879 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2880 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2881 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2882 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2883 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2884 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2885 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2886 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2887 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2888 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002889 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002890 ring->init_hw = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002891
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002892 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002893}
Chris Wilsona7b97612012-07-20 12:41:08 +01002894
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002895int intel_init_vebox_ring_buffer(struct drm_device *dev)
2896{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002897 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002898 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002899
2900 ring->name = "video enhancement ring";
2901 ring->id = VECS;
2902
2903 ring->mmio_base = VEBOX_RING_BASE;
2904 ring->write_tail = ring_write_tail;
2905 ring->flush = gen6_ring_flush;
2906 ring->add_request = gen6_add_request;
2907 ring->get_seqno = gen6_ring_get_seqno;
2908 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002909
2910 if (INTEL_INFO(dev)->gen >= 8) {
2911 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002912 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002913 ring->irq_get = gen8_ring_get_irq;
2914 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002915 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002916 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002917 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002918 ring->semaphore.signal = gen8_xcs_signal;
2919 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002920 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002921 } else {
2922 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2923 ring->irq_get = hsw_vebox_get_irq;
2924 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002925 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002926 if (i915_semaphore_is_enabled(dev)) {
2927 ring->semaphore.sync_to = gen6_ring_sync;
2928 ring->semaphore.signal = gen6_signal;
2929 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2930 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2931 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2932 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2933 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2934 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2935 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2936 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2937 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2938 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2939 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002940 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002941 ring->init_hw = init_ring_common;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002942
2943 return intel_init_ring_buffer(dev, ring);
2944}
2945
Chris Wilsona7b97612012-07-20 12:41:08 +01002946int
John Harrison4866d722015-05-29 17:43:55 +01002947intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002948{
John Harrison4866d722015-05-29 17:43:55 +01002949 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002950 int ret;
2951
2952 if (!ring->gpu_caches_dirty)
2953 return 0;
2954
John Harrisona84c3ae2015-05-29 17:43:57 +01002955 ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002956 if (ret)
2957 return ret;
2958
John Harrisona84c3ae2015-05-29 17:43:57 +01002959 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002960
2961 ring->gpu_caches_dirty = false;
2962 return 0;
2963}
2964
2965int
John Harrison2f200552015-05-29 17:43:53 +01002966intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002967{
John Harrison2f200552015-05-29 17:43:53 +01002968 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002969 uint32_t flush_domains;
2970 int ret;
2971
2972 flush_domains = 0;
2973 if (ring->gpu_caches_dirty)
2974 flush_domains = I915_GEM_GPU_DOMAINS;
2975
John Harrisona84c3ae2015-05-29 17:43:57 +01002976 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002977 if (ret)
2978 return ret;
2979
John Harrisona84c3ae2015-05-29 17:43:57 +01002980 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002981
2982 ring->gpu_caches_dirty = false;
2983 return 0;
2984}
Chris Wilsone3efda42014-04-09 09:19:41 +01002985
2986void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002987intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002988{
2989 int ret;
2990
2991 if (!intel_ring_initialized(ring))
2992 return;
2993
2994 ret = intel_ring_idle(ring);
2995 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2996 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2997 ring->name, ret);
2998
2999 stop_ring(ring);
3000}