blob: e6ef8d79631115c8c5e86ea3f7bc1a83127146eb [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
109 ret = intel_ring_begin(ring, 2);
110 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 Wilson6f392d5482010-08-07 11:01:22 +0100127 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000128 int ret;
Chris Wilson6f392d5482010-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
168 ret = intel_ring_begin(ring, 2);
169 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
223
224 ret = intel_ring_begin(ring, 6);
225 if (ret)
226 return ret;
227
228 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
229 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
230 PIPE_CONTROL_STALL_AT_SCOREBOARD);
231 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
232 intel_ring_emit(ring, 0); /* low dword */
233 intel_ring_emit(ring, 0); /* high dword */
234 intel_ring_emit(ring, MI_NOOP);
235 intel_ring_advance(ring);
236
237 ret = intel_ring_begin(ring, 6);
238 if (ret)
239 return ret;
240
241 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
242 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
243 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
244 intel_ring_emit(ring, 0);
245 intel_ring_emit(ring, 0);
246 intel_ring_emit(ring, MI_NOOP);
247 intel_ring_advance(ring);
248
249 return 0;
250}
251
252static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100253gen6_render_ring_flush(struct drm_i915_gem_request *req,
254 u32 invalidate_domains, u32 flush_domains)
Jesse Barnes8d315282011-10-16 10:23:31 +0200255{
John Harrisona84c3ae2015-05-29 17:43:57 +0100256 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8d315282011-10-16 10:23:31 +0200257 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100258 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200259 int ret;
260
Paulo Zanonib3111502012-08-17 18:35:42 -0300261 /* Force SNB workarounds for PIPE_CONTROL flushes */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100262 ret = intel_emit_post_sync_nonzero_flush(req);
Paulo Zanonib3111502012-08-17 18:35:42 -0300263 if (ret)
264 return ret;
265
Jesse Barnes8d315282011-10-16 10:23:31 +0200266 /* Just flush everything. Experiments have shown that reducing the
267 * number of bits based on the write domains has little performance
268 * impact.
269 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100270 if (flush_domains) {
271 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
272 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
273 /*
274 * Ensure that any following seqno writes only happen
275 * when the render cache is indeed flushed.
276 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200277 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100278 }
279 if (invalidate_domains) {
280 flags |= PIPE_CONTROL_TLB_INVALIDATE;
281 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
282 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
283 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
284 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
285 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
286 /*
287 * TLB invalidate requires a post-sync write.
288 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700289 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100290 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200291
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100292 ret = intel_ring_begin(ring, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200293 if (ret)
294 return ret;
295
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100296 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
Jesse Barnes8d315282011-10-16 10:23:31 +0200297 intel_ring_emit(ring, flags);
298 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100299 intel_ring_emit(ring, 0);
Jesse Barnes8d315282011-10-16 10:23:31 +0200300 intel_ring_advance(ring);
301
302 return 0;
303}
304
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100305static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100306gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
Paulo Zanonif3987632012-08-17 18:35:43 -0300307{
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100308 struct intel_engine_cs *ring = req->ring;
Paulo Zanonif3987632012-08-17 18:35:43 -0300309 int ret;
310
311 ret = intel_ring_begin(ring, 4);
312 if (ret)
313 return ret;
314
315 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
316 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
317 PIPE_CONTROL_STALL_AT_SCOREBOARD);
318 intel_ring_emit(ring, 0);
319 intel_ring_emit(ring, 0);
320 intel_ring_advance(ring);
321
322 return 0;
323}
324
325static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100326gen7_render_ring_flush(struct drm_i915_gem_request *req,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300327 u32 invalidate_domains, u32 flush_domains)
328{
John Harrisona84c3ae2015-05-29 17:43:57 +0100329 struct intel_engine_cs *ring = req->ring;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300330 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100331 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300332 int ret;
333
Paulo Zanonif3987632012-08-17 18:35:43 -0300334 /*
335 * Ensure that any following seqno writes only happen when the render
336 * cache is indeed flushed.
337 *
338 * Workaround: 4th PIPE_CONTROL command (except the ones with only
339 * read-cache invalidate bits set) must have the CS_STALL bit set. We
340 * don't try to be clever and just set it unconditionally.
341 */
342 flags |= PIPE_CONTROL_CS_STALL;
343
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300344 /* Just flush everything. Experiments have shown that reducing the
345 * number of bits based on the write domains has little performance
346 * impact.
347 */
348 if (flush_domains) {
349 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
350 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300351 }
352 if (invalidate_domains) {
353 flags |= PIPE_CONTROL_TLB_INVALIDATE;
354 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
355 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
356 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
357 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
358 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000359 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300360 /*
361 * TLB invalidate requires a post-sync write.
362 */
363 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200364 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300365
Chris Wilsonadd284a2014-12-16 08:44:32 +0000366 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
367
Paulo Zanonif3987632012-08-17 18:35:43 -0300368 /* Workaround: we must issue a pipe_control with CS-stall bit
369 * set before a pipe_control command that has the state cache
370 * invalidate bit set. */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100371 gen7_render_ring_cs_stall_wa(req);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300372 }
373
374 ret = intel_ring_begin(ring, 4);
375 if (ret)
376 return ret;
377
378 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
379 intel_ring_emit(ring, flags);
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200380 intel_ring_emit(ring, scratch_addr);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300381 intel_ring_emit(ring, 0);
382 intel_ring_advance(ring);
383
384 return 0;
385}
386
Ben Widawskya5f3d682013-11-02 21:07:27 -0700387static int
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100388gen8_emit_pipe_control(struct drm_i915_gem_request *req,
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300389 u32 flags, u32 scratch_addr)
390{
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100391 struct intel_engine_cs *ring = req->ring;
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300392 int ret;
393
394 ret = intel_ring_begin(ring, 6);
395 if (ret)
396 return ret;
397
398 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
399 intel_ring_emit(ring, flags);
400 intel_ring_emit(ring, scratch_addr);
401 intel_ring_emit(ring, 0);
402 intel_ring_emit(ring, 0);
403 intel_ring_emit(ring, 0);
404 intel_ring_advance(ring);
405
406 return 0;
407}
408
409static int
John Harrisona84c3ae2015-05-29 17:43:57 +0100410gen8_render_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700411 u32 invalidate_domains, u32 flush_domains)
412{
413 u32 flags = 0;
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100414 u32 scratch_addr = req->ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800415 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700416
417 flags |= PIPE_CONTROL_CS_STALL;
418
419 if (flush_domains) {
420 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
421 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
422 }
423 if (invalidate_domains) {
424 flags |= PIPE_CONTROL_TLB_INVALIDATE;
425 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
426 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
427 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
428 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
429 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
430 flags |= PIPE_CONTROL_QW_WRITE;
431 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800432
433 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100434 ret = gen8_emit_pipe_control(req,
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800435 PIPE_CONTROL_CS_STALL |
436 PIPE_CONTROL_STALL_AT_SCOREBOARD,
437 0);
438 if (ret)
439 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700440 }
441
John Harrisonf2cf1fc2015-05-29 17:43:58 +0100442 return gen8_emit_pipe_control(req, flags, scratch_addr);
Ben Widawskya5f3d682013-11-02 21:07:27 -0700443}
444
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100445static void ring_write_tail(struct intel_engine_cs *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100446 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800447{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300448 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100449 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800450}
451
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100452u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800453{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300454 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson50877442014-03-21 12:41:53 +0000455 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800456
Chris Wilson50877442014-03-21 12:41:53 +0000457 if (INTEL_INFO(ring->dev)->gen >= 8)
458 acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
459 RING_ACTHD_UDW(ring->mmio_base));
460 else if (INTEL_INFO(ring->dev)->gen >= 4)
461 acthd = I915_READ(RING_ACTHD(ring->mmio_base));
462 else
463 acthd = I915_READ(ACTHD);
464
465 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800466}
467
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100468static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200469{
470 struct drm_i915_private *dev_priv = ring->dev->dev_private;
471 u32 addr;
472
473 addr = dev_priv->status_page_dmah->busaddr;
474 if (INTEL_INFO(ring->dev)->gen >= 4)
475 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
476 I915_WRITE(HWS_PGA, addr);
477}
478
Damien Lespiauaf75f262015-02-10 19:32:17 +0000479static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
480{
481 struct drm_device *dev = ring->dev;
482 struct drm_i915_private *dev_priv = ring->dev->dev_private;
483 u32 mmio = 0;
484
485 /* The ring status page addresses are no longer next to the rest of
486 * the ring registers as of gen7.
487 */
488 if (IS_GEN7(dev)) {
489 switch (ring->id) {
490 case RCS:
491 mmio = RENDER_HWS_PGA_GEN7;
492 break;
493 case BCS:
494 mmio = BLT_HWS_PGA_GEN7;
495 break;
496 /*
497 * VCS2 actually doesn't exist on Gen7. Only shut up
498 * gcc switch check warning
499 */
500 case VCS2:
501 case VCS:
502 mmio = BSD_HWS_PGA_GEN7;
503 break;
504 case VECS:
505 mmio = VEBOX_HWS_PGA_GEN7;
506 break;
507 }
508 } else if (IS_GEN6(ring->dev)) {
509 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
510 } else {
511 /* XXX: gen8 returns to sanity */
512 mmio = RING_HWS_PGA(ring->mmio_base);
513 }
514
515 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
516 POSTING_READ(mmio);
517
518 /*
519 * Flush the TLB for this page
520 *
521 * FIXME: These two bits have disappeared on gen8, so a question
522 * arises: do we still need this and if so how should we go about
523 * invalidating the TLB?
524 */
525 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
526 u32 reg = RING_INSTPM(ring->mmio_base);
527
528 /* ring should be idle before issuing a sync flush*/
529 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
530
531 I915_WRITE(reg,
532 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
533 INSTPM_SYNC_FLUSH));
534 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
535 1000))
536 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
537 ring->name);
538 }
539}
540
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100541static bool stop_ring(struct intel_engine_cs *ring)
Chris Wilson9991ae72014-04-02 16:36:07 +0100542{
543 struct drm_i915_private *dev_priv = to_i915(ring->dev);
544
545 if (!IS_GEN2(ring->dev)) {
546 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
Daniel Vetter403bdd12014-08-07 16:05:39 +0200547 if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
548 DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100549 /* Sometimes we observe that the idle flag is not
550 * set even though the ring is empty. So double
551 * check before giving up.
552 */
553 if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
554 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100555 }
556 }
557
558 I915_WRITE_CTL(ring, 0);
559 I915_WRITE_HEAD(ring, 0);
560 ring->write_tail(ring, 0);
561
562 if (!IS_GEN2(ring->dev)) {
563 (void)I915_READ_CTL(ring);
564 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
565 }
566
567 return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
568}
569
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100570static int init_ring_common(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800571{
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200572 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300573 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100574 struct intel_ringbuffer *ringbuf = ring->buffer;
575 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200576 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800577
Mika Kuoppala59bad942015-01-16 11:34:40 +0200578 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200579
Chris Wilson9991ae72014-04-02 16:36:07 +0100580 if (!stop_ring(ring)) {
581 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000582 DRM_DEBUG_KMS("%s head not reset to zero "
583 "ctl %08x head %08x tail %08x start %08x\n",
584 ring->name,
585 I915_READ_CTL(ring),
586 I915_READ_HEAD(ring),
587 I915_READ_TAIL(ring),
588 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800589
Chris Wilson9991ae72014-04-02 16:36:07 +0100590 if (!stop_ring(ring)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000591 DRM_ERROR("failed to set %s head to zero "
592 "ctl %08x head %08x tail %08x start %08x\n",
593 ring->name,
594 I915_READ_CTL(ring),
595 I915_READ_HEAD(ring),
596 I915_READ_TAIL(ring),
597 I915_READ_START(ring));
Chris Wilson9991ae72014-04-02 16:36:07 +0100598 ret = -EIO;
599 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000600 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700601 }
602
Chris Wilson9991ae72014-04-02 16:36:07 +0100603 if (I915_NEED_GFX_HWS(dev))
604 intel_ring_setup_status_page(ring);
605 else
606 ring_setup_phys_status_page(ring);
607
Jiri Kosinaece4a172014-08-07 16:29:53 +0200608 /* Enforce ordering by reading HEAD register back */
609 I915_READ_HEAD(ring);
610
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200611 /* Initialize the ring. This must happen _after_ we've cleared the ring
612 * registers with the above sequence (the readback of the HEAD registers
613 * also enforces ordering), otherwise the hw might lose the new ring
614 * register values. */
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700615 I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100616
617 /* WaClearRingBufHeadRegAtInit:ctg,elk */
618 if (I915_READ_HEAD(ring))
619 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
620 ring->name, I915_READ_HEAD(ring));
621 I915_WRITE_HEAD(ring, 0);
622 (void)I915_READ_HEAD(ring);
623
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200624 I915_WRITE_CTL(ring,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100625 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000626 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800627
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800628 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400629 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700630 I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
Sean Paulf01db982012-03-16 12:43:22 -0400631 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000632 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100633 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
634 ring->name,
635 I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
636 I915_READ_HEAD(ring), I915_READ_TAIL(ring),
637 I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200638 ret = -EIO;
639 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800640 }
641
Dave Gordonebd0fd42014-11-27 11:22:49 +0000642 ringbuf->last_retired_head = -1;
Chris Wilson5c6c6002014-09-06 10:28:27 +0100643 ringbuf->head = I915_READ_HEAD(ring);
644 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Dave Gordonebd0fd42014-11-27 11:22:49 +0000645 intel_ring_update_space(ringbuf);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000646
Chris Wilson50f018d2013-06-10 11:20:19 +0100647 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
648
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200649out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200650 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200651
652 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700653}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800654
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100655void
656intel_fini_pipe_control(struct intel_engine_cs *ring)
657{
658 struct drm_device *dev = ring->dev;
659
660 if (ring->scratch.obj == NULL)
661 return;
662
663 if (INTEL_INFO(dev)->gen >= 5) {
664 kunmap(sg_page(ring->scratch.obj->pages->sgl));
665 i915_gem_object_ggtt_unpin(ring->scratch.obj);
666 }
667
668 drm_gem_object_unreference(&ring->scratch.obj->base);
669 ring->scratch.obj = NULL;
670}
671
672int
673intel_init_pipe_control(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000674{
Chris Wilsonc6df5412010-12-15 09:56:50 +0000675 int ret;
676
Daniel Vetterbfc882b2014-11-20 00:33:08 +0100677 WARN_ON(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000678
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100679 ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
680 if (ring->scratch.obj == NULL) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000681 DRM_ERROR("Failed to allocate seqno page\n");
682 ret = -ENOMEM;
683 goto err;
684 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100685
Daniel Vettera9cc7262014-02-14 14:01:13 +0100686 ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
687 if (ret)
688 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000689
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100690 ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000691 if (ret)
692 goto err_unref;
693
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100694 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
695 ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
696 if (ring->scratch.cpu_page == NULL) {
Wei Yongjun56b085a2013-05-28 17:51:44 +0800697 ret = -ENOMEM;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000698 goto err_unpin;
Wei Yongjun56b085a2013-05-28 17:51:44 +0800699 }
Chris Wilsonc6df5412010-12-15 09:56:50 +0000700
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200701 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100702 ring->name, ring->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000703 return 0;
704
705err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800706 i915_gem_object_ggtt_unpin(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000707err_unref:
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100708 drm_gem_object_unreference(&ring->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000709err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000710 return ret;
711}
712
John Harrisone2be4fa2015-05-29 17:43:54 +0100713static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100714{
Mika Kuoppala72253422014-10-07 17:21:26 +0300715 int ret, i;
John Harrisone2be4fa2015-05-29 17:43:54 +0100716 struct intel_engine_cs *ring = req->ring;
Arun Siluvery888b5992014-08-26 14:44:51 +0100717 struct drm_device *dev = ring->dev;
718 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala72253422014-10-07 17:21:26 +0300719 struct i915_workarounds *w = &dev_priv->workarounds;
Arun Siluvery888b5992014-08-26 14:44:51 +0100720
Michel Thierrye6c1abb2014-11-26 14:21:02 +0000721 if (WARN_ON_ONCE(w->count == 0))
Mika Kuoppala72253422014-10-07 17:21:26 +0300722 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100723
Mika Kuoppala72253422014-10-07 17:21:26 +0300724 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100725 ret = intel_ring_flush_all_caches(req);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100726 if (ret)
727 return ret;
728
Arun Siluvery22a916a2014-10-22 18:59:52 +0100729 ret = intel_ring_begin(ring, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300730 if (ret)
731 return ret;
732
Arun Siluvery22a916a2014-10-22 18:59:52 +0100733 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300734 for (i = 0; i < w->count; i++) {
Mika Kuoppala72253422014-10-07 17:21:26 +0300735 intel_ring_emit(ring, w->reg[i].addr);
736 intel_ring_emit(ring, w->reg[i].value);
737 }
Arun Siluvery22a916a2014-10-22 18:59:52 +0100738 intel_ring_emit(ring, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300739
740 intel_ring_advance(ring);
741
742 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +0100743 ret = intel_ring_flush_all_caches(req);
Mika Kuoppala72253422014-10-07 17:21:26 +0300744 if (ret)
745 return ret;
746
747 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
748
749 return 0;
750}
751
John Harrison87531812015-05-29 17:43:44 +0100752static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100753{
754 int ret;
755
John Harrisone2be4fa2015-05-29 17:43:54 +0100756 ret = intel_ring_workarounds_emit(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100757 if (ret != 0)
758 return ret;
759
John Harrisonbe013632015-05-29 17:43:45 +0100760 ret = i915_gem_render_state_init(req);
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100761 if (ret)
762 DRM_ERROR("init render state: %d\n", ret);
763
764 return ret;
765}
766
Mika Kuoppala72253422014-10-07 17:21:26 +0300767static int wa_add(struct drm_i915_private *dev_priv,
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000768 const u32 addr, const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300769{
770 const u32 idx = dev_priv->workarounds.count;
771
772 if (WARN_ON(idx >= I915_MAX_WA_REGS))
773 return -ENOSPC;
774
775 dev_priv->workarounds.reg[idx].addr = addr;
776 dev_priv->workarounds.reg[idx].value = val;
777 dev_priv->workarounds.reg[idx].mask = mask;
778
779 dev_priv->workarounds.count++;
780
781 return 0;
782}
783
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000784#define WA_REG(addr, mask, val) { \
785 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300786 if (r) \
787 return r; \
788 }
789
790#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000791 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300792
793#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000794 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300795
Damien Lespiau98533252014-12-08 17:33:51 +0000796#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000797 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300798
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000799#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
800#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300801
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000802#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300803
804static int bdw_init_workarounds(struct intel_engine_cs *ring)
805{
806 struct drm_device *dev = ring->dev;
807 struct drm_i915_private *dev_priv = dev->dev_private;
808
Ville Syrjälä9cc83022015-06-02 15:37:36 +0300809 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
810
Ville Syrjälä2441f872015-06-02 15:37:37 +0300811 /* WaDisableAsyncFlipPerfMode:bdw */
812 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
813
Arun Siluvery86d7f232014-08-26 14:44:50 +0100814 /* WaDisablePartialInstShootdown:bdw */
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700815 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Mika Kuoppala72253422014-10-07 17:21:26 +0300816 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
817 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
818 STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100819
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700820 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300821 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
822 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100823
Mika Kuoppala72253422014-10-07 17:21:26 +0300824 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
825 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100826
827 /* Use Force Non-Coherent whenever executing a 3D context. This is a
828 * workaround for for a possible hang in the unlikely event a TLB
829 * invalidation occurs during a PSD flush.
830 */
Mika Kuoppala72253422014-10-07 17:21:26 +0300831 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000832 /* WaForceEnableNonCoherent:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300833 HDC_FORCE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000834 /* WaForceContextSaveRestoreNonCoherent:bdw */
835 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
836 /* WaHdcDisableFetchWhenMasked:bdw */
Michel Thierryf3f32362014-12-04 15:07:52 +0000837 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000838 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Mika Kuoppala72253422014-10-07 17:21:26 +0300839 (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100840
Kenneth Graunke2701fc42015-01-13 12:46:52 -0800841 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
842 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
843 * polygons in the same 8x4 pixel/sample area to be processed without
844 * stalling waiting for the earlier ones to write to Hierarchical Z
845 * buffer."
846 *
847 * This optimization is off by default for Broadwell; turn it on.
848 */
849 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
850
Arun Siluvery86d7f232014-08-26 14:44:50 +0100851 /* Wa4x4STCOptimizationDisable:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300852 WA_SET_BIT_MASKED(CACHE_MODE_1,
853 GEN8_4x4_STC_OPTIMIZATION_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100854
855 /*
856 * BSpec recommends 8x4 when MSAA is used,
857 * however in practice 16x4 seems fastest.
858 *
859 * Note that PS/WM thread counts depend on the WIZ hashing
860 * disable bit, which we don't touch here, but it's good
861 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
862 */
Damien Lespiau98533252014-12-08 17:33:51 +0000863 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
864 GEN6_WIZ_HASHING_MASK,
865 GEN6_WIZ_HASHING_16x4);
Arun Siluvery888b5992014-08-26 14:44:51 +0100866
Arun Siluvery86d7f232014-08-26 14:44:50 +0100867 return 0;
868}
869
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300870static int chv_init_workarounds(struct intel_engine_cs *ring)
871{
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300872 struct drm_device *dev = ring->dev;
873 struct drm_i915_private *dev_priv = dev->dev_private;
874
Ville Syrjälä9cc83022015-06-02 15:37:36 +0300875 WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
876
Ville Syrjälä2441f872015-06-02 15:37:37 +0300877 /* WaDisableAsyncFlipPerfMode:chv */
878 WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
879
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300880 /* WaDisablePartialInstShootdown:chv */
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300881 /* WaDisableThreadStallDopClockGating:chv */
Mika Kuoppala72253422014-10-07 17:21:26 +0300882 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Arun Siluvery605f1432014-10-28 18:33:13 +0000883 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
884 STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300885
Arun Siluvery952890092014-10-28 18:33:14 +0000886 /* Use Force Non-Coherent whenever executing a 3D context. This is a
887 * workaround for a possible hang in the unlikely event a TLB
888 * invalidation occurs during a PSD flush.
889 */
890 /* WaForceEnableNonCoherent:chv */
891 /* WaHdcDisableFetchWhenMasked:chv */
892 WA_SET_BIT_MASKED(HDC_CHICKEN0,
893 HDC_FORCE_NON_COHERENT |
894 HDC_DONOT_FETCH_MEM_WHEN_MASKED);
895
Kenneth Graunke973a5b02015-01-13 12:46:53 -0800896 /* According to the CACHE_MODE_0 default value documentation, some
897 * CHV platforms disable this optimization by default. Turn it on.
898 */
899 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
900
Ville Syrjälä14bc16e2015-01-21 19:37:58 +0200901 /* Wa4x4STCOptimizationDisable:chv */
902 WA_SET_BIT_MASKED(CACHE_MODE_1,
903 GEN8_4x4_STC_OPTIMIZATION_DISABLE);
904
Kenneth Graunked60de812015-01-10 18:02:22 -0800905 /* Improve HiZ throughput on CHV. */
906 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
907
Ville Syrjäläe7fc2432015-01-21 19:38:00 +0200908 /*
909 * BSpec recommends 8x4 when MSAA is used,
910 * however in practice 16x4 seems fastest.
911 *
912 * Note that PS/WM thread counts depend on the WIZ hashing
913 * disable bit, which we don't touch here, but it's good
914 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
915 */
916 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
917 GEN6_WIZ_HASHING_MASK,
918 GEN6_WIZ_HASHING_16x4);
919
Mika Kuoppala72253422014-10-07 17:21:26 +0300920 return 0;
921}
922
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000923static int gen9_init_workarounds(struct intel_engine_cs *ring)
924{
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000925 struct drm_device *dev = ring->dev;
926 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak8ea6f892015-05-19 17:05:42 +0300927 uint32_t tmp;
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000928
Nick Hoathb0e6f6d2015-05-07 14:15:29 +0100929 /* WaDisablePartialInstShootdown:skl,bxt */
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000930 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
931 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
932
Nick Hoatha119a6e2015-05-07 14:15:30 +0100933 /* Syncing dependencies between camera and graphics:skl,bxt */
Nick Hoath84241712015-02-05 10:47:20 +0000934 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
935 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
936
Nick Hoathd2a31db2015-05-07 14:15:31 +0100937 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) == SKL_REVID_A0 ||
938 INTEL_REVID(dev) == SKL_REVID_B0)) ||
939 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
940 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
Damien Lespiaua86eb582015-02-11 18:21:44 +0000941 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
942 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000943 }
944
Nick Hoatha13d2152015-05-07 14:15:32 +0100945 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0) ||
946 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)) {
947 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
Damien Lespiau183c6da2015-02-09 19:33:11 +0000948 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
949 GEN9_RHWO_OPTIMIZATION_DISABLE);
950 WA_SET_BIT_MASKED(GEN9_SLICE_COMMON_ECO_CHICKEN0,
951 DISABLE_PIXEL_MASK_CAMMING);
952 }
953
Nick Hoath27a1b682015-05-07 14:15:33 +0100954 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) >= SKL_REVID_C0) ||
955 IS_BROXTON(dev)) {
956 /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt */
Nick Hoathcac23df2015-02-05 10:47:22 +0000957 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
958 GEN9_ENABLE_YV12_BUGFIX);
959 }
960
Nick Hoath50683682015-05-07 14:15:35 +0100961 /* Wa4x4STCOptimizationDisable:skl,bxt */
Hoath, Nicholas18404812015-02-05 10:47:23 +0000962 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
963
Nick Hoath27160c92015-05-07 14:15:36 +0100964 /* WaDisablePartialResolveInVc:skl,bxt */
Damien Lespiau9370cd92015-02-09 19:33:17 +0000965 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE);
966
Nick Hoath16be17a2015-05-07 14:15:37 +0100967 /* WaCcsTlbPrefetchDisable:skl,bxt */
Damien Lespiaue2db7072015-02-09 19:33:21 +0000968 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
969 GEN9_CCS_TLB_PREFETCH_ENABLE);
970
Imre Deak5a2ae952015-05-19 15:04:59 +0300971 /* WaDisableMaskBasedCammingInRCC:skl,bxt */
972 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_C0) ||
973 (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0))
Ben Widawsky38a39a72015-03-11 10:54:53 +0200974 WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
975 PIXEL_MASK_CAMMING_DISABLE);
976
Imre Deak8ea6f892015-05-19 17:05:42 +0300977 /* WaForceContextSaveRestoreNonCoherent:skl,bxt */
978 tmp = HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT;
979 if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) == SKL_REVID_F0) ||
980 (IS_BROXTON(dev) && INTEL_REVID(dev) >= BXT_REVID_B0))
981 tmp |= HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE;
982 WA_SET_BIT_MASKED(HDC_CHICKEN0, tmp);
983
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000984 return 0;
985}
986
Damien Lespiaub7668792015-02-14 18:30:29 +0000987static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
Damien Lespiau8d205492015-02-09 19:33:15 +0000988{
Damien Lespiaub7668792015-02-14 18:30:29 +0000989 struct drm_device *dev = ring->dev;
990 struct drm_i915_private *dev_priv = dev->dev_private;
991 u8 vals[3] = { 0, 0, 0 };
992 unsigned int i;
993
994 for (i = 0; i < 3; i++) {
995 u8 ss;
996
997 /*
998 * Only consider slices where one, and only one, subslice has 7
999 * EUs
1000 */
1001 if (hweight8(dev_priv->info.subslice_7eu[i]) != 1)
1002 continue;
1003
1004 /*
1005 * subslice_7eu[i] != 0 (because of the check above) and
1006 * ss_max == 4 (maximum number of subslices possible per slice)
1007 *
1008 * -> 0 <= ss <= 3;
1009 */
1010 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1011 vals[i] = 3 - ss;
1012 }
1013
1014 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1015 return 0;
1016
1017 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1018 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1019 GEN9_IZ_HASHING_MASK(2) |
1020 GEN9_IZ_HASHING_MASK(1) |
1021 GEN9_IZ_HASHING_MASK(0),
1022 GEN9_IZ_HASHING(2, vals[2]) |
1023 GEN9_IZ_HASHING(1, vals[1]) |
1024 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001025
Mika Kuoppala72253422014-10-07 17:21:26 +03001026 return 0;
1027}
1028
Damien Lespiaub7668792015-02-14 18:30:29 +00001029
Damien Lespiau8d205492015-02-09 19:33:15 +00001030static int skl_init_workarounds(struct intel_engine_cs *ring)
1031{
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001032 struct drm_device *dev = ring->dev;
1033 struct drm_i915_private *dev_priv = dev->dev_private;
1034
Damien Lespiau8d205492015-02-09 19:33:15 +00001035 gen9_init_workarounds(ring);
1036
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001037 /* WaDisablePowerCompilerClockGating:skl */
1038 if (INTEL_REVID(dev) == SKL_REVID_B0)
1039 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1040 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1041
Nick Hoathb62adbd2015-05-07 14:15:34 +01001042 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
1043 /*
1044 *Use Force Non-Coherent whenever executing a 3D context. This
1045 * is a workaround for a possible hang in the unlikely event
1046 * a TLB invalidation occurs during a PSD flush.
1047 */
1048 /* WaForceEnableNonCoherent:skl */
1049 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1050 HDC_FORCE_NON_COHERENT);
1051 }
1052
Ville Syrjälä5b6fd122015-06-02 15:37:35 +03001053 if (INTEL_REVID(dev) == SKL_REVID_C0 ||
1054 INTEL_REVID(dev) == SKL_REVID_D0)
1055 /* WaBarrierPerformanceFixDisable:skl */
1056 WA_SET_BIT_MASKED(HDC_CHICKEN0,
1057 HDC_FENCE_DEST_SLM_DISABLE |
1058 HDC_BARRIER_PERFORMANCE_DISABLE);
1059
Damien Lespiaub7668792015-02-14 18:30:29 +00001060 return skl_tune_iz_hashing(ring);
Damien Lespiau8d205492015-02-09 19:33:15 +00001061}
1062
Nick Hoathcae04372015-03-17 11:39:38 +02001063static int bxt_init_workarounds(struct intel_engine_cs *ring)
1064{
Nick Hoathdfb601e2015-04-10 13:12:24 +01001065 struct drm_device *dev = ring->dev;
1066 struct drm_i915_private *dev_priv = dev->dev_private;
1067
Nick Hoathcae04372015-03-17 11:39:38 +02001068 gen9_init_workarounds(ring);
1069
Nick Hoathdfb601e2015-04-10 13:12:24 +01001070 /* WaDisableThreadStallDopClockGating:bxt */
1071 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
1072 STALL_DOP_GATING_DISABLE);
1073
Nick Hoath983b4b92015-04-10 13:12:25 +01001074 /* WaDisableSbeCacheDispatchPortSharing:bxt */
1075 if (INTEL_REVID(dev) <= BXT_REVID_B0) {
1076 WA_SET_BIT_MASKED(
1077 GEN7_HALF_SLICE_CHICKEN1,
1078 GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
1079 }
1080
Nick Hoathcae04372015-03-17 11:39:38 +02001081 return 0;
1082}
1083
Michel Thierry771b9a52014-11-11 16:47:33 +00001084int init_workarounds_ring(struct intel_engine_cs *ring)
Mika Kuoppala72253422014-10-07 17:21:26 +03001085{
1086 struct drm_device *dev = ring->dev;
1087 struct drm_i915_private *dev_priv = dev->dev_private;
1088
1089 WARN_ON(ring->id != RCS);
1090
1091 dev_priv->workarounds.count = 0;
1092
1093 if (IS_BROADWELL(dev))
1094 return bdw_init_workarounds(ring);
1095
1096 if (IS_CHERRYVIEW(dev))
1097 return chv_init_workarounds(ring);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001098
Damien Lespiau8d205492015-02-09 19:33:15 +00001099 if (IS_SKYLAKE(dev))
1100 return skl_init_workarounds(ring);
Nick Hoathcae04372015-03-17 11:39:38 +02001101
1102 if (IS_BROXTON(dev))
1103 return bxt_init_workarounds(ring);
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001104
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001105 return 0;
1106}
1107
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001108static int init_render_ring(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001109{
Chris Wilson78501ea2010-10-27 12:18:21 +01001110 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001111 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001112 int ret = init_ring_common(ring);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001113 if (ret)
1114 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001115
Akash Goel61a563a2014-03-25 18:01:50 +05301116 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1117 if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001118 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001119
1120 /* We need to disable the AsyncFlip performance optimisations in order
1121 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1122 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001123 *
Ville Syrjälä2441f872015-06-02 15:37:37 +03001124 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001125 */
Ville Syrjälä2441f872015-06-02 15:37:37 +03001126 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001127 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1128
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001129 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301130 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001131 if (INTEL_INFO(dev)->gen == 6)
1132 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001133 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001134
Akash Goel01fa0302014-03-24 23:00:04 +05301135 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001136 if (IS_GEN7(dev))
1137 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301138 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001139 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001140
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001141 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001142 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1143 * "If this bit is set, STCunit will have LRA as replacement
1144 * policy. [...] This bit must be reset. LRA replacement
1145 * policy is not supported."
1146 */
1147 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001148 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001149 }
1150
Ville Syrjälä9cc83022015-06-02 15:37:36 +03001151 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001152 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001153
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001154 if (HAS_L3_DPF(dev))
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001155 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001156
Mika Kuoppala72253422014-10-07 17:21:26 +03001157 return init_workarounds_ring(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001158}
1159
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001160static void render_ring_cleanup(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001161{
Daniel Vetterb45305f2012-12-17 16:21:27 +01001162 struct drm_device *dev = ring->dev;
Ben Widawsky3e789982014-06-30 09:53:37 -07001163 struct drm_i915_private *dev_priv = dev->dev_private;
1164
1165 if (dev_priv->semaphore_obj) {
1166 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1167 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1168 dev_priv->semaphore_obj = NULL;
1169 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001170
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001171 intel_fini_pipe_control(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001172}
1173
John Harrisonf7169682015-05-29 17:44:05 +01001174static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001175 unsigned int num_dwords)
1176{
1177#define MBOX_UPDATE_DWORDS 8
John Harrisonf7169682015-05-29 17:44:05 +01001178 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky3e789982014-06-30 09:53:37 -07001179 struct drm_device *dev = signaller->dev;
1180 struct drm_i915_private *dev_priv = dev->dev_private;
1181 struct intel_engine_cs *waiter;
1182 int i, ret, num_rings;
1183
1184 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1185 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1186#undef MBOX_UPDATE_DWORDS
1187
1188 ret = intel_ring_begin(signaller, num_dwords);
1189 if (ret)
1190 return ret;
1191
1192 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001193 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001194 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1195 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1196 continue;
1197
John Harrisonf7169682015-05-29 17:44:05 +01001198 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky3e789982014-06-30 09:53:37 -07001199 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1200 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1201 PIPE_CONTROL_QW_WRITE |
1202 PIPE_CONTROL_FLUSH_ENABLE);
1203 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1204 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001205 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001206 intel_ring_emit(signaller, 0);
1207 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1208 MI_SEMAPHORE_TARGET(waiter->id));
1209 intel_ring_emit(signaller, 0);
1210 }
1211
1212 return 0;
1213}
1214
John Harrisonf7169682015-05-29 17:44:05 +01001215static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky3e789982014-06-30 09:53:37 -07001216 unsigned int num_dwords)
1217{
1218#define MBOX_UPDATE_DWORDS 6
John Harrisonf7169682015-05-29 17:44:05 +01001219 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky3e789982014-06-30 09:53:37 -07001220 struct drm_device *dev = signaller->dev;
1221 struct drm_i915_private *dev_priv = dev->dev_private;
1222 struct intel_engine_cs *waiter;
1223 int i, ret, num_rings;
1224
1225 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1226 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1227#undef MBOX_UPDATE_DWORDS
1228
1229 ret = intel_ring_begin(signaller, num_dwords);
1230 if (ret)
1231 return ret;
1232
1233 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001234 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001235 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1236 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1237 continue;
1238
John Harrisonf7169682015-05-29 17:44:05 +01001239 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky3e789982014-06-30 09:53:37 -07001240 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1241 MI_FLUSH_DW_OP_STOREDW);
1242 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1243 MI_FLUSH_DW_USE_GTT);
1244 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001245 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001246 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1247 MI_SEMAPHORE_TARGET(waiter->id));
1248 intel_ring_emit(signaller, 0);
1249 }
1250
1251 return 0;
1252}
1253
John Harrisonf7169682015-05-29 17:44:05 +01001254static int gen6_signal(struct drm_i915_gem_request *signaller_req,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001255 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001256{
John Harrisonf7169682015-05-29 17:44:05 +01001257 struct intel_engine_cs *signaller = signaller_req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001258 struct drm_device *dev = signaller->dev;
1259 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001260 struct intel_engine_cs *useless;
Ben Widawskya1444b72014-06-30 09:53:35 -07001261 int i, ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001262
Ben Widawskya1444b72014-06-30 09:53:35 -07001263#define MBOX_UPDATE_DWORDS 3
1264 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1265 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1266#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001267
1268 ret = intel_ring_begin(signaller, num_dwords);
1269 if (ret)
1270 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001271
Ben Widawsky78325f22014-04-29 14:52:29 -07001272 for_each_ring(useless, dev_priv, i) {
1273 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1274 if (mbox_reg != GEN6_NOSYNC) {
John Harrisonf7169682015-05-29 17:44:05 +01001275 u32 seqno = i915_gem_request_get_seqno(signaller_req);
Ben Widawsky78325f22014-04-29 14:52:29 -07001276 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1277 intel_ring_emit(signaller, mbox_reg);
John Harrison6259cea2014-11-24 18:49:29 +00001278 intel_ring_emit(signaller, seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001279 }
1280 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001281
Ben Widawskya1444b72014-06-30 09:53:35 -07001282 /* If num_dwords was rounded, make sure the tail pointer is correct */
1283 if (num_rings % 2 == 0)
1284 intel_ring_emit(signaller, MI_NOOP);
1285
Ben Widawsky024a43e2014-04-29 14:52:30 -07001286 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001287}
1288
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001289/**
1290 * gen6_add_request - Update the semaphore mailbox registers
John Harrisonee044a82015-05-29 17:44:00 +01001291 *
1292 * @request - request to write to the ring
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001293 *
1294 * Update the mailbox registers in the *other* rings with the current seqno.
1295 * This acts like a signal in the canonical semaphore.
1296 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001297static int
John Harrisonee044a82015-05-29 17:44:00 +01001298gen6_add_request(struct drm_i915_gem_request *req)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001299{
John Harrisonee044a82015-05-29 17:44:00 +01001300 struct intel_engine_cs *ring = req->ring;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001301 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001302
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001303 if (ring->semaphore.signal)
John Harrisonf7169682015-05-29 17:44:05 +01001304 ret = ring->semaphore.signal(req, 4);
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001305 else
1306 ret = intel_ring_begin(ring, 4);
1307
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001308 if (ret)
1309 return ret;
1310
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001311 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1312 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrisonee044a82015-05-29 17:44:00 +01001313 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001314 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001315 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001316
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001317 return 0;
1318}
1319
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001320static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1321 u32 seqno)
1322{
1323 struct drm_i915_private *dev_priv = dev->dev_private;
1324 return dev_priv->last_seqno < seqno;
1325}
1326
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001327/**
1328 * intel_ring_sync - sync the waiter to the signaller on seqno
1329 *
1330 * @waiter - ring that is waiting
1331 * @signaller - ring which has, or will signal
1332 * @seqno - seqno which the waiter will block on
1333 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001334
1335static int
John Harrison599d9242015-05-29 17:44:04 +01001336gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001337 struct intel_engine_cs *signaller,
1338 u32 seqno)
1339{
John Harrison599d9242015-05-29 17:44:04 +01001340 struct intel_engine_cs *waiter = waiter_req->ring;
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001341 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
1342 int ret;
1343
1344 ret = intel_ring_begin(waiter, 4);
1345 if (ret)
1346 return ret;
1347
1348 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1349 MI_SEMAPHORE_GLOBAL_GTT |
Ben Widawskybae4fcd2014-06-30 09:53:43 -07001350 MI_SEMAPHORE_POLL |
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001351 MI_SEMAPHORE_SAD_GTE_SDD);
1352 intel_ring_emit(waiter, seqno);
1353 intel_ring_emit(waiter,
1354 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1355 intel_ring_emit(waiter,
1356 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1357 intel_ring_advance(waiter);
1358 return 0;
1359}
1360
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001361static int
John Harrison599d9242015-05-29 17:44:04 +01001362gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001363 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001364 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001365{
John Harrison599d9242015-05-29 17:44:04 +01001366 struct intel_engine_cs *waiter = waiter_req->ring;
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001367 u32 dw1 = MI_SEMAPHORE_MBOX |
1368 MI_SEMAPHORE_COMPARE |
1369 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001370 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1371 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001372
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001373 /* Throughout all of the GEM code, seqno passed implies our current
1374 * seqno is >= the last seqno executed. However for hardware the
1375 * comparison is strictly greater than.
1376 */
1377 seqno -= 1;
1378
Ben Widawskyebc348b2014-04-29 14:52:28 -07001379 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001380
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001381 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001382 if (ret)
1383 return ret;
1384
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001385 /* If seqno wrap happened, omit the wait with no-ops */
1386 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001387 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001388 intel_ring_emit(waiter, seqno);
1389 intel_ring_emit(waiter, 0);
1390 intel_ring_emit(waiter, MI_NOOP);
1391 } else {
1392 intel_ring_emit(waiter, MI_NOOP);
1393 intel_ring_emit(waiter, MI_NOOP);
1394 intel_ring_emit(waiter, MI_NOOP);
1395 intel_ring_emit(waiter, MI_NOOP);
1396 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001397 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001398
1399 return 0;
1400}
1401
Chris Wilsonc6df5412010-12-15 09:56:50 +00001402#define PIPE_CONTROL_FLUSH(ring__, addr__) \
1403do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001404 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1405 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +00001406 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1407 intel_ring_emit(ring__, 0); \
1408 intel_ring_emit(ring__, 0); \
1409} while (0)
1410
1411static int
John Harrisonee044a82015-05-29 17:44:00 +01001412pc_render_add_request(struct drm_i915_gem_request *req)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001413{
John Harrisonee044a82015-05-29 17:44:00 +01001414 struct intel_engine_cs *ring = req->ring;
Chris Wilson18393f62014-04-09 09:19:40 +01001415 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001416 int ret;
1417
1418 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1419 * incoherent with writes to memory, i.e. completely fubar,
1420 * so we need to use PIPE_NOTIFY instead.
1421 *
1422 * However, we also need to workaround the qword write
1423 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1424 * memory before requesting an interrupt.
1425 */
1426 ret = intel_ring_begin(ring, 32);
1427 if (ret)
1428 return ret;
1429
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001430 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001431 PIPE_CONTROL_WRITE_FLUSH |
1432 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001433 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrisonee044a82015-05-29 17:44:00 +01001434 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001435 intel_ring_emit(ring, 0);
1436 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001437 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +00001438 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001439 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001440 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001441 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001442 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001443 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001444 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001445 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001446 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001447
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001448 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001449 PIPE_CONTROL_WRITE_FLUSH |
1450 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001451 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001452 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrisonee044a82015-05-29 17:44:00 +01001453 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001454 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +01001455 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001456
Chris Wilsonc6df5412010-12-15 09:56:50 +00001457 return 0;
1458}
1459
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001460static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001461gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001462{
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001463 /* Workaround to force correct ordering between irq and seqno writes on
1464 * ivb (and maybe also on snb) by reading from a CS register (like
1465 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +00001466 if (!lazy_coherency) {
1467 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1468 POSTING_READ(RING_ACTHD(ring->mmio_base));
1469 }
1470
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001471 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1472}
1473
1474static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001475ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001476{
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001477 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1478}
1479
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001480static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001481ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001482{
1483 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1484}
1485
Chris Wilsonc6df5412010-12-15 09:56:50 +00001486static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001487pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001488{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001489 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001490}
1491
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001492static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001493pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001494{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001495 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001496}
1497
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001498static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001499gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001500{
1501 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001502 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001503 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001504
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001505 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Daniel Vettere48d8632012-04-11 22:12:54 +02001506 return false;
1507
Chris Wilson7338aef2012-04-24 21:48:47 +01001508 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001509 if (ring->irq_refcount++ == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001510 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001511 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001512
1513 return true;
1514}
1515
1516static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001517gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001518{
1519 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001520 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001521 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001522
Chris Wilson7338aef2012-04-24 21:48:47 +01001523 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001524 if (--ring->irq_refcount == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001525 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001526 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001527}
1528
1529static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001530i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001531{
Chris Wilson78501ea2010-10-27 12:18:21 +01001532 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001533 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001534 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001535
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001536 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001537 return false;
1538
Chris Wilson7338aef2012-04-24 21:48:47 +01001539 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001540 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001541 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1542 I915_WRITE(IMR, dev_priv->irq_mask);
1543 POSTING_READ(IMR);
1544 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001545 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001546
1547 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001548}
1549
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001550static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001551i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001552{
Chris Wilson78501ea2010-10-27 12:18:21 +01001553 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001554 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001555 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001556
Chris Wilson7338aef2012-04-24 21:48:47 +01001557 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001558 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001559 dev_priv->irq_mask |= ring->irq_enable_mask;
1560 I915_WRITE(IMR, dev_priv->irq_mask);
1561 POSTING_READ(IMR);
1562 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001563 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001564}
1565
Chris Wilsonc2798b12012-04-22 21:13:57 +01001566static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001567i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001568{
1569 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001570 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001571 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001572
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001573 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonc2798b12012-04-22 21:13:57 +01001574 return false;
1575
Chris Wilson7338aef2012-04-24 21:48:47 +01001576 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001577 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001578 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1579 I915_WRITE16(IMR, dev_priv->irq_mask);
1580 POSTING_READ16(IMR);
1581 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001582 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001583
1584 return true;
1585}
1586
1587static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001588i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001589{
1590 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001591 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001592 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001593
Chris Wilson7338aef2012-04-24 21:48:47 +01001594 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001595 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001596 dev_priv->irq_mask |= ring->irq_enable_mask;
1597 I915_WRITE16(IMR, dev_priv->irq_mask);
1598 POSTING_READ16(IMR);
1599 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001600 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001601}
1602
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001603static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001604bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001605 u32 invalidate_domains,
1606 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001607{
John Harrisona84c3ae2015-05-29 17:43:57 +01001608 struct intel_engine_cs *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001609 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001610
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001611 ret = intel_ring_begin(ring, 2);
1612 if (ret)
1613 return ret;
1614
1615 intel_ring_emit(ring, MI_FLUSH);
1616 intel_ring_emit(ring, MI_NOOP);
1617 intel_ring_advance(ring);
1618 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001619}
1620
Chris Wilson3cce4692010-10-27 16:11:02 +01001621static int
John Harrisonee044a82015-05-29 17:44:00 +01001622i9xx_add_request(struct drm_i915_gem_request *req)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001623{
John Harrisonee044a82015-05-29 17:44:00 +01001624 struct intel_engine_cs *ring = req->ring;
Chris Wilson3cce4692010-10-27 16:11:02 +01001625 int ret;
1626
1627 ret = intel_ring_begin(ring, 4);
1628 if (ret)
1629 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001630
Chris Wilson3cce4692010-10-27 16:11:02 +01001631 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1632 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrisonee044a82015-05-29 17:44:00 +01001633 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilson3cce4692010-10-27 16:11:02 +01001634 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001635 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001636
Chris Wilson3cce4692010-10-27 16:11:02 +01001637 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001638}
1639
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001640static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001641gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001642{
1643 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001644 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001645 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001646
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001647 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1648 return false;
Chris Wilson0f468322011-01-04 17:35:21 +00001649
Chris Wilson7338aef2012-04-24 21:48:47 +01001650 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001651 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001652 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001653 I915_WRITE_IMR(ring,
1654 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001655 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001656 else
1657 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001658 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001659 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001660 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001661
1662 return true;
1663}
1664
1665static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001666gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001667{
1668 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001669 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001670 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001671
Chris Wilson7338aef2012-04-24 21:48:47 +01001672 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001673 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001674 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001675 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001676 else
1677 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001678 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001679 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001680 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001681}
1682
Ben Widawskya19d2932013-05-28 19:22:30 -07001683static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001684hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001685{
1686 struct drm_device *dev = ring->dev;
1687 struct drm_i915_private *dev_priv = dev->dev_private;
1688 unsigned long flags;
1689
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001690 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskya19d2932013-05-28 19:22:30 -07001691 return false;
1692
Daniel Vetter59cdb632013-07-04 23:35:28 +02001693 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001694 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001695 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001696 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001697 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001698 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001699
1700 return true;
1701}
1702
1703static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001704hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001705{
1706 struct drm_device *dev = ring->dev;
1707 struct drm_i915_private *dev_priv = dev->dev_private;
1708 unsigned long flags;
1709
Daniel Vetter59cdb632013-07-04 23:35:28 +02001710 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001711 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001712 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001713 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001714 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001715 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001716}
1717
Ben Widawskyabd58f02013-11-02 21:07:09 -07001718static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001719gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001720{
1721 struct drm_device *dev = ring->dev;
1722 struct drm_i915_private *dev_priv = dev->dev_private;
1723 unsigned long flags;
1724
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001725 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskyabd58f02013-11-02 21:07:09 -07001726 return false;
1727
1728 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1729 if (ring->irq_refcount++ == 0) {
1730 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1731 I915_WRITE_IMR(ring,
1732 ~(ring->irq_enable_mask |
1733 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1734 } else {
1735 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1736 }
1737 POSTING_READ(RING_IMR(ring->mmio_base));
1738 }
1739 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1740
1741 return true;
1742}
1743
1744static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001745gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001746{
1747 struct drm_device *dev = ring->dev;
1748 struct drm_i915_private *dev_priv = dev->dev_private;
1749 unsigned long flags;
1750
1751 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1752 if (--ring->irq_refcount == 0) {
1753 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1754 I915_WRITE_IMR(ring,
1755 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1756 } else {
1757 I915_WRITE_IMR(ring, ~0);
1758 }
1759 POSTING_READ(RING_IMR(ring->mmio_base));
1760 }
1761 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1762}
1763
Zou Nan haid1b851f2010-05-21 09:08:57 +08001764static int
John Harrison53fddaf2015-05-29 17:44:02 +01001765i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001766 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001767 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001768{
John Harrison53fddaf2015-05-29 17:44:02 +01001769 struct intel_engine_cs *ring = req->ring;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001770 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001771
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001772 ret = intel_ring_begin(ring, 2);
1773 if (ret)
1774 return ret;
1775
Chris Wilson78501ea2010-10-27 12:18:21 +01001776 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001777 MI_BATCH_BUFFER_START |
1778 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001779 (dispatch_flags & I915_DISPATCH_SECURE ?
1780 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001781 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001782 intel_ring_advance(ring);
1783
Zou Nan haid1b851f2010-05-21 09:08:57 +08001784 return 0;
1785}
1786
Daniel Vetterb45305f2012-12-17 16:21:27 +01001787/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1788#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001789#define I830_TLB_ENTRIES (2)
1790#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001791static int
John Harrison53fddaf2015-05-29 17:44:02 +01001792i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001793 u64 offset, u32 len,
1794 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001795{
John Harrison53fddaf2015-05-29 17:44:02 +01001796 struct intel_engine_cs *ring = req->ring;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001797 u32 cs_offset = ring->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001798 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001799
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001800 ret = intel_ring_begin(ring, 6);
1801 if (ret)
1802 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001803
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001804 /* Evict the invalid PTE TLBs */
1805 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1806 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1807 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1808 intel_ring_emit(ring, cs_offset);
1809 intel_ring_emit(ring, 0xdeadbeef);
1810 intel_ring_emit(ring, MI_NOOP);
1811 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001812
John Harrison8e004ef2015-02-13 11:48:10 +00001813 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001814 if (len > I830_BATCH_LIMIT)
1815 return -ENOSPC;
1816
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001817 ret = intel_ring_begin(ring, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001818 if (ret)
1819 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001820
1821 /* Blit the batch (which has now all relocs applied) to the
1822 * stable batch scratch bo area (so that the CS never
1823 * stumbles over its tlb invalidation bug) ...
1824 */
1825 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1826 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilson611a7a42014-09-12 07:37:42 +01001827 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001828 intel_ring_emit(ring, cs_offset);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001829 intel_ring_emit(ring, 4096);
1830 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001831
Daniel Vetterb45305f2012-12-17 16:21:27 +01001832 intel_ring_emit(ring, MI_FLUSH);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001833 intel_ring_emit(ring, MI_NOOP);
1834 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001835
1836 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001837 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001838 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001839
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001840 ret = intel_ring_begin(ring, 4);
1841 if (ret)
1842 return ret;
1843
1844 intel_ring_emit(ring, MI_BATCH_BUFFER);
John Harrison8e004ef2015-02-13 11:48:10 +00001845 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1846 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001847 intel_ring_emit(ring, offset + len - 8);
1848 intel_ring_emit(ring, MI_NOOP);
1849 intel_ring_advance(ring);
1850
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001851 return 0;
1852}
1853
1854static int
John Harrison53fddaf2015-05-29 17:44:02 +01001855i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001856 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001857 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001858{
John Harrison53fddaf2015-05-29 17:44:02 +01001859 struct intel_engine_cs *ring = req->ring;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001860 int ret;
1861
1862 ret = intel_ring_begin(ring, 2);
1863 if (ret)
1864 return ret;
1865
Chris Wilson65f56872012-04-17 16:38:12 +01001866 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
John Harrison8e004ef2015-02-13 11:48:10 +00001867 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1868 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001869 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001870
Eric Anholt62fdfea2010-05-21 13:26:39 -07001871 return 0;
1872}
1873
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001874static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001875{
Chris Wilson05394f32010-11-08 19:18:58 +00001876 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001877
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001878 obj = ring->status_page.obj;
1879 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001880 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001881
Chris Wilson9da3da62012-06-01 15:20:22 +01001882 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001883 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001884 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001885 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001886}
1887
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001888static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001889{
Chris Wilson05394f32010-11-08 19:18:58 +00001890 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001891
Chris Wilsone3efda42014-04-09 09:19:41 +01001892 if ((obj = ring->status_page.obj) == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001893 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001894 int ret;
1895
1896 obj = i915_gem_alloc_object(ring->dev, 4096);
1897 if (obj == NULL) {
1898 DRM_ERROR("Failed to allocate status page\n");
1899 return -ENOMEM;
1900 }
1901
1902 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1903 if (ret)
1904 goto err_unref;
1905
Chris Wilson1f767e02014-07-03 17:33:03 -04001906 flags = 0;
1907 if (!HAS_LLC(ring->dev))
1908 /* On g33, we cannot place HWS above 256MiB, so
1909 * restrict its pinning to the low mappable arena.
1910 * Though this restriction is not documented for
1911 * gen4, gen5, or byt, they also behave similarly
1912 * and hang if the HWS is placed at the top of the
1913 * GTT. To generalise, it appears that all !llc
1914 * platforms have issues with us placing the HWS
1915 * above the mappable region (even though we never
1916 * actualy map it).
1917 */
1918 flags |= PIN_MAPPABLE;
1919 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001920 if (ret) {
1921err_unref:
1922 drm_gem_object_unreference(&obj->base);
1923 return ret;
1924 }
1925
1926 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001927 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001928
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001929 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001930 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001931 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001932
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001933 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1934 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001935
1936 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001937}
1938
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001939static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001940{
1941 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001942
1943 if (!dev_priv->status_page_dmah) {
1944 dev_priv->status_page_dmah =
1945 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1946 if (!dev_priv->status_page_dmah)
1947 return -ENOMEM;
1948 }
1949
Chris Wilson6b8294a2012-11-16 11:43:20 +00001950 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1951 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1952
1953 return 0;
1954}
1955
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001956void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1957{
1958 iounmap(ringbuf->virtual_start);
1959 ringbuf->virtual_start = NULL;
1960 i915_gem_object_ggtt_unpin(ringbuf->obj);
1961}
1962
1963int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
1964 struct intel_ringbuffer *ringbuf)
1965{
1966 struct drm_i915_private *dev_priv = to_i915(dev);
1967 struct drm_i915_gem_object *obj = ringbuf->obj;
1968 int ret;
1969
1970 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1971 if (ret)
1972 return ret;
1973
1974 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1975 if (ret) {
1976 i915_gem_object_ggtt_unpin(obj);
1977 return ret;
1978 }
1979
1980 ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
1981 i915_gem_obj_ggtt_offset(obj), ringbuf->size);
1982 if (ringbuf->virtual_start == NULL) {
1983 i915_gem_object_ggtt_unpin(obj);
1984 return -EINVAL;
1985 }
1986
1987 return 0;
1988}
1989
Oscar Mateo84c23772014-07-24 17:04:15 +01001990void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01001991{
Oscar Mateo2919d292014-07-03 16:28:02 +01001992 drm_gem_object_unreference(&ringbuf->obj->base);
1993 ringbuf->obj = NULL;
1994}
1995
Oscar Mateo84c23772014-07-24 17:04:15 +01001996int intel_alloc_ringbuffer_obj(struct drm_device *dev,
1997 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01001998{
Chris Wilsone3efda42014-04-09 09:19:41 +01001999 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002000
2001 obj = NULL;
2002 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002003 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002004 if (obj == NULL)
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002005 obj = i915_gem_alloc_object(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002006 if (obj == NULL)
2007 return -ENOMEM;
2008
Akash Goel24f3a8c2014-06-17 10:59:42 +05302009 /* mark ring buffers as read-only from GPU side by default */
2010 obj->gt_ro = 1;
2011
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002012 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002013
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002014 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002015}
2016
Ben Widawskyc43b5632012-04-16 14:07:40 -07002017static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002018 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002019{
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002020 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002021 int ret;
2022
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002023 WARN_ON(ring->buffer);
2024
2025 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2026 if (!ringbuf)
2027 return -ENOMEM;
2028 ring->buffer = ringbuf;
Oscar Mateo8ee14972014-05-22 14:13:34 +01002029
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002030 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01002031 INIT_LIST_HEAD(&ring->active_list);
2032 INIT_LIST_HEAD(&ring->request_list);
Oscar Mateocc9130b2014-07-24 17:04:42 +01002033 INIT_LIST_HEAD(&ring->execlist_queue);
Chris Wilson06fbca72015-04-07 16:20:36 +01002034 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002035 ringbuf->size = 32 * PAGE_SIZE;
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002036 ringbuf->ring = ring;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002037 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002038
Chris Wilsonb259f672011-03-29 13:19:09 +01002039 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002040
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002041 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002042 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002043 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002044 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002045 } else {
2046 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002047 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002048 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002049 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002050 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002051
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002052 WARN_ON(ringbuf->obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002053
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002054 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2055 if (ret) {
2056 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
2057 ring->name, ret);
2058 goto error;
2059 }
2060
2061 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2062 if (ret) {
2063 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2064 ring->name, ret);
2065 intel_destroy_ringbuffer_obj(ringbuf);
2066 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002067 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002068
Chris Wilson55249ba2010-12-22 14:04:47 +00002069 /* Workaround an erratum on the i830 which causes a hang if
2070 * the TAIL pointer points to within the last 2 cachelines
2071 * of the buffer.
2072 */
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002073 ringbuf->effective_size = ringbuf->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01002074 if (IS_I830(dev) || IS_845G(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002075 ringbuf->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00002076
Brad Volkin44e895a2014-05-10 14:10:43 -07002077 ret = i915_cmd_parser_init_ring(ring);
2078 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002079 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002080
Oscar Mateo8ee14972014-05-22 14:13:34 +01002081 return 0;
2082
2083error:
2084 kfree(ringbuf);
2085 ring->buffer = NULL;
2086 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002087}
2088
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002089void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002090{
John Harrison6402c332014-10-31 12:00:26 +00002091 struct drm_i915_private *dev_priv;
2092 struct intel_ringbuffer *ringbuf;
Chris Wilson33626e62010-10-29 16:18:36 +01002093
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002094 if (!intel_ring_initialized(ring))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002095 return;
2096
John Harrison6402c332014-10-31 12:00:26 +00002097 dev_priv = to_i915(ring->dev);
2098 ringbuf = ring->buffer;
2099
Chris Wilsone3efda42014-04-09 09:19:41 +01002100 intel_stop_ring_buffer(ring);
Ville Syrjäläde8f0a52014-05-28 19:12:13 +03002101 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002102
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002103 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateo2919d292014-07-03 16:28:02 +01002104 intel_destroy_ringbuffer_obj(ringbuf);
John Harrison6259cea2014-11-24 18:49:29 +00002105 i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
Chris Wilson78501ea2010-10-27 12:18:21 +01002106
Zou Nan hai8d192152010-11-02 16:31:01 +08002107 if (ring->cleanup)
2108 ring->cleanup(ring);
2109
Chris Wilson78501ea2010-10-27 12:18:21 +01002110 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07002111
2112 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01002113 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002114
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002115 kfree(ringbuf);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002116 ring->buffer = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002117}
2118
Chris Wilson595e1ee2015-04-07 16:20:51 +01002119static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002120{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002121 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002122 struct drm_i915_gem_request *request;
Chris Wilsonb4716182015-04-27 13:41:17 +01002123 unsigned space;
2124 int ret;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002125
John Harrison29b1b412015-06-18 13:10:09 +01002126 /* The whole point of reserving space is to not wait! */
2127 WARN_ON(ringbuf->reserved_in_use);
2128
Dave Gordonebd0fd42014-11-27 11:22:49 +00002129 if (intel_ring_space(ringbuf) >= n)
2130 return 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002131
2132 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002133 space = __intel_ring_space(request->postfix, ringbuf->tail,
2134 ringbuf->size);
2135 if (space >= n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002136 break;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002137 }
2138
Chris Wilson595e1ee2015-04-07 16:20:51 +01002139 if (WARN_ON(&request->list == &ring->request_list))
Chris Wilsona71d8d92012-02-15 11:25:36 +00002140 return -ENOSPC;
2141
Daniel Vettera4b3a572014-11-26 14:17:05 +01002142 ret = i915_wait_request(request);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002143 if (ret)
2144 return ret;
2145
Chris Wilsonb4716182015-04-27 13:41:17 +01002146 ringbuf->space = space;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002147 return 0;
2148}
2149
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002150static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002151{
2152 uint32_t __iomem *virt;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002153 struct intel_ringbuffer *ringbuf = ring->buffer;
2154 int rem = ringbuf->size - ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002155
John Harrison29b1b412015-06-18 13:10:09 +01002156 /* Can't wrap if space has already been reserved! */
2157 WARN_ON(ringbuf->reserved_in_use);
2158
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002159 if (ringbuf->space < rem) {
Chris Wilson3e960502012-11-27 16:22:54 +00002160 int ret = ring_wait_for_space(ring, rem);
2161 if (ret)
2162 return ret;
2163 }
2164
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002165 virt = ringbuf->virtual_start + ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002166 rem /= 4;
2167 while (rem--)
2168 iowrite32(MI_NOOP, virt++);
2169
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002170 ringbuf->tail = 0;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002171 intel_ring_update_space(ringbuf);
Chris Wilson3e960502012-11-27 16:22:54 +00002172
2173 return 0;
2174}
2175
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002176int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002177{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002178 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002179
2180 /* We need to add any requests required to flush the objects and ring */
John Harrison75289872015-05-29 17:43:49 +01002181 WARN_ON(ring->outstanding_lazy_request);
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002182 if (ring->outstanding_lazy_request)
John Harrison75289872015-05-29 17:43:49 +01002183 i915_add_request(ring->outstanding_lazy_request);
Chris Wilson3e960502012-11-27 16:22:54 +00002184
2185 /* Wait upon the last request to be completed */
2186 if (list_empty(&ring->request_list))
2187 return 0;
2188
Daniel Vettera4b3a572014-11-26 14:17:05 +01002189 req = list_entry(ring->request_list.prev,
Chris Wilsonb4716182015-04-27 13:41:17 +01002190 struct drm_i915_gem_request,
2191 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002192
Chris Wilsonb4716182015-04-27 13:41:17 +01002193 /* Make sure we do not trigger any retires */
2194 return __i915_wait_request(req,
2195 atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
2196 to_i915(ring->dev)->mm.interruptible,
2197 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002198}
2199
John Harrison6689cb22015-03-19 12:30:08 +00002200int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002201{
John Harrison6689cb22015-03-19 12:30:08 +00002202 request->ringbuf = request->ring->buffer;
John Harrison9eba5d42014-11-24 18:49:23 +00002203 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002204}
2205
John Harrison29b1b412015-06-18 13:10:09 +01002206void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2207{
2208 /* NB: Until request management is fully tidied up and the OLR is
2209 * removed, there are too many ways for get false hits on this
2210 * anti-recursion check! */
2211 /*WARN_ON(ringbuf->reserved_size);*/
2212 WARN_ON(ringbuf->reserved_in_use);
2213
2214 ringbuf->reserved_size = size;
2215
2216 /*
2217 * Really need to call _begin() here but that currently leads to
2218 * recursion problems! This will be fixed later but for now just
2219 * return and hope for the best. Note that there is only a real
2220 * problem if the create of the request never actually calls _begin()
2221 * but if they are not submitting any work then why did they create
2222 * the request in the first place?
2223 */
2224}
2225
2226void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2227{
2228 WARN_ON(ringbuf->reserved_in_use);
2229
2230 ringbuf->reserved_size = 0;
2231 ringbuf->reserved_in_use = false;
2232}
2233
2234void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2235{
2236 WARN_ON(ringbuf->reserved_in_use);
2237
2238 ringbuf->reserved_in_use = true;
2239 ringbuf->reserved_tail = ringbuf->tail;
2240}
2241
2242void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2243{
2244 WARN_ON(!ringbuf->reserved_in_use);
2245 WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2246 "request reserved size too small: %d vs %d!\n",
2247 ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2248
2249 ringbuf->reserved_size = 0;
2250 ringbuf->reserved_in_use = false;
2251}
2252
2253static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002254{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002255 struct intel_ringbuffer *ringbuf = ring->buffer;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002256 int ret;
2257
John Harrison29b1b412015-06-18 13:10:09 +01002258 /*
2259 * Add on the reserved size to the request to make sure that after
2260 * the intended commands have been emitted, there is guaranteed to
2261 * still be enough free space to send them to the hardware.
2262 */
2263 if (!ringbuf->reserved_in_use)
2264 bytes += ringbuf->reserved_size;
2265
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002266 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002267 ret = intel_wrap_ring_buffer(ring);
2268 if (unlikely(ret))
2269 return ret;
John Harrison29b1b412015-06-18 13:10:09 +01002270
2271 if(ringbuf->reserved_size) {
2272 uint32_t size = ringbuf->reserved_size;
2273
2274 intel_ring_reserved_space_cancel(ringbuf);
2275 intel_ring_reserved_space_reserve(ringbuf, size);
2276 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002277 }
2278
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002279 if (unlikely(ringbuf->space < bytes)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002280 ret = ring_wait_for_space(ring, bytes);
2281 if (unlikely(ret))
2282 return ret;
2283 }
2284
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002285 return 0;
2286}
2287
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002288int intel_ring_begin(struct intel_engine_cs *ring,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002289 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002290{
John Harrison217e46b2015-05-29 17:43:29 +01002291 struct drm_i915_gem_request *req;
Jani Nikula4640c4f2014-03-31 14:27:19 +03002292 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002293 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01002294
Daniel Vetter33196de2012-11-14 17:14:05 +01002295 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2296 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02002297 if (ret)
2298 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00002299
Chris Wilson304d6952014-01-02 14:32:35 +00002300 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
2301 if (ret)
2302 return ret;
2303
Chris Wilson9d7730912012-11-27 16:22:52 +00002304 /* Preallocate the olr before touching the ring */
John Harrison217e46b2015-05-29 17:43:29 +01002305 ret = i915_gem_request_alloc(ring, ring->default_context, &req);
Chris Wilson9d7730912012-11-27 16:22:52 +00002306 if (ret)
2307 return ret;
2308
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002309 ring->buffer->space -= num_dwords * sizeof(uint32_t);
Chris Wilson304d6952014-01-02 14:32:35 +00002310 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002311}
2312
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002313/* Align the ring tail to a cacheline boundary */
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002314int intel_ring_cacheline_align(struct intel_engine_cs *ring)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002315{
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002316 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002317 int ret;
2318
2319 if (num_dwords == 0)
2320 return 0;
2321
Chris Wilson18393f62014-04-09 09:19:40 +01002322 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002323 ret = intel_ring_begin(ring, num_dwords);
2324 if (ret)
2325 return ret;
2326
2327 while (num_dwords--)
2328 intel_ring_emit(ring, MI_NOOP);
2329
2330 intel_ring_advance(ring);
2331
2332 return 0;
2333}
2334
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002335void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002336{
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002337 struct drm_device *dev = ring->dev;
2338 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002339
John Harrison6259cea2014-11-24 18:49:29 +00002340 BUG_ON(ring->outstanding_lazy_request);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002341
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002342 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002343 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2344 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002345 if (HAS_VEBOX(dev))
Ben Widawsky50201502013-08-12 16:53:03 -07002346 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002347 }
Chris Wilson297b0c52010-10-22 17:02:41 +01002348
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002349 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03002350 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01002351}
2352
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002353static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002354 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002355{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002356 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002357
2358 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002359
Chris Wilson12f55812012-07-05 17:14:01 +01002360 /* Disable notification that the ring is IDLE. The GT
2361 * will then assume that it is busy and bring it out of rc6.
2362 */
2363 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2364 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2365
2366 /* Clear the context id. Here be magic! */
2367 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2368
2369 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04002370 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01002371 GEN6_BSD_SLEEP_INDICATOR) == 0,
2372 50))
2373 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002374
Chris Wilson12f55812012-07-05 17:14:01 +01002375 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04002376 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01002377 POSTING_READ(RING_TAIL(ring->mmio_base));
2378
2379 /* Let the ring send IDLE messages to the GT again,
2380 * and so let it sleep to conserve power when idle.
2381 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002382 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01002383 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002384}
2385
John Harrisona84c3ae2015-05-29 17:43:57 +01002386static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002387 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002388{
John Harrisona84c3ae2015-05-29 17:43:57 +01002389 struct intel_engine_cs *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002390 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002391 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002392
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002393 ret = intel_ring_begin(ring, 4);
2394 if (ret)
2395 return ret;
2396
Chris Wilson71a77e02011-02-02 12:13:49 +00002397 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002398 if (INTEL_INFO(ring->dev)->gen >= 8)
2399 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002400
2401 /* We always require a command barrier so that subsequent
2402 * commands, such as breadcrumb interrupts, are strictly ordered
2403 * wrt the contents of the write cache being flushed to memory
2404 * (and thus being coherent from the CPU).
2405 */
2406 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2407
Jesse Barnes9a289772012-10-26 09:42:42 -07002408 /*
2409 * Bspec vol 1c.5 - video engine command streamer:
2410 * "If ENABLED, all TLBs will be invalidated once the flush
2411 * operation is complete. This bit is only valid when the
2412 * Post-Sync Operation field is a value of 1h or 3h."
2413 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002414 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002415 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2416
Chris Wilson71a77e02011-02-02 12:13:49 +00002417 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002418 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002419 if (INTEL_INFO(ring->dev)->gen >= 8) {
2420 intel_ring_emit(ring, 0); /* upper addr */
2421 intel_ring_emit(ring, 0); /* value */
2422 } else {
2423 intel_ring_emit(ring, 0);
2424 intel_ring_emit(ring, MI_NOOP);
2425 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002426 intel_ring_advance(ring);
2427 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002428}
2429
2430static int
John Harrison53fddaf2015-05-29 17:44:02 +01002431gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002432 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002433 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002434{
John Harrison53fddaf2015-05-29 17:44:02 +01002435 struct intel_engine_cs *ring = req->ring;
John Harrison8e004ef2015-02-13 11:48:10 +00002436 bool ppgtt = USES_PPGTT(ring->dev) &&
2437 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002438 int ret;
2439
2440 ret = intel_ring_begin(ring, 4);
2441 if (ret)
2442 return ret;
2443
2444 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07002445 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002446 intel_ring_emit(ring, lower_32_bits(offset));
2447 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002448 intel_ring_emit(ring, MI_NOOP);
2449 intel_ring_advance(ring);
2450
2451 return 0;
2452}
2453
2454static int
John Harrison53fddaf2015-05-29 17:44:02 +01002455hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002456 u64 offset, u32 len,
2457 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002458{
John Harrison53fddaf2015-05-29 17:44:02 +01002459 struct intel_engine_cs *ring = req->ring;
Akshay Joshi0206e352011-08-16 15:34:10 -04002460 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002461
Akshay Joshi0206e352011-08-16 15:34:10 -04002462 ret = intel_ring_begin(ring, 2);
2463 if (ret)
2464 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002465
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002466 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002467 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002468 (dispatch_flags & I915_DISPATCH_SECURE ?
Chris Wilson77072252014-09-10 12:18:27 +01002469 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002470 /* bit0-7 is the length on GEN6+ */
2471 intel_ring_emit(ring, offset);
2472 intel_ring_advance(ring);
2473
2474 return 0;
2475}
2476
2477static int
John Harrison53fddaf2015-05-29 17:44:02 +01002478gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002479 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002480 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002481{
John Harrison53fddaf2015-05-29 17:44:02 +01002482 struct intel_engine_cs *ring = req->ring;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002483 int ret;
2484
2485 ret = intel_ring_begin(ring, 2);
2486 if (ret)
2487 return ret;
2488
2489 intel_ring_emit(ring,
2490 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002491 (dispatch_flags & I915_DISPATCH_SECURE ?
2492 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002493 /* bit0-7 is the length on GEN6+ */
2494 intel_ring_emit(ring, offset);
2495 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002496
Akshay Joshi0206e352011-08-16 15:34:10 -04002497 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002498}
2499
Chris Wilson549f7362010-10-19 11:19:32 +01002500/* Blitter support (SandyBridge+) */
2501
John Harrisona84c3ae2015-05-29 17:43:57 +01002502static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002503 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002504{
John Harrisona84c3ae2015-05-29 17:43:57 +01002505 struct intel_engine_cs *ring = req->ring;
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002506 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00002507 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002508 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002509
Daniel Vetter6a233c72011-12-14 13:57:07 +01002510 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002511 if (ret)
2512 return ret;
2513
Chris Wilson71a77e02011-02-02 12:13:49 +00002514 cmd = MI_FLUSH_DW;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002515 if (INTEL_INFO(dev)->gen >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002516 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002517
2518 /* We always require a command barrier so that subsequent
2519 * commands, such as breadcrumb interrupts, are strictly ordered
2520 * wrt the contents of the write cache being flushed to memory
2521 * (and thus being coherent from the CPU).
2522 */
2523 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2524
Jesse Barnes9a289772012-10-26 09:42:42 -07002525 /*
2526 * Bspec vol 1c.3 - blitter engine command streamer:
2527 * "If ENABLED, all TLBs will be invalidated once the flush
2528 * operation is complete. This bit is only valid when the
2529 * Post-Sync Operation field is a value of 1h or 3h."
2530 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002531 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002532 cmd |= MI_INVALIDATE_TLB;
Chris Wilson71a77e02011-02-02 12:13:49 +00002533 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002534 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002535 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002536 intel_ring_emit(ring, 0); /* upper addr */
2537 intel_ring_emit(ring, 0); /* value */
2538 } else {
2539 intel_ring_emit(ring, 0);
2540 intel_ring_emit(ring, MI_NOOP);
2541 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002542 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002543
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002544 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002545}
2546
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002547int intel_init_render_ring_buffer(struct drm_device *dev)
2548{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002549 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002550 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002551 struct drm_i915_gem_object *obj;
2552 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002553
Daniel Vetter59465b52012-04-11 22:12:48 +02002554 ring->name = "render ring";
2555 ring->id = RCS;
2556 ring->mmio_base = RENDER_RING_BASE;
2557
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002558 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002559 if (i915_semaphore_is_enabled(dev)) {
2560 obj = i915_gem_alloc_object(dev, 4096);
2561 if (obj == NULL) {
2562 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2563 i915.semaphores = 0;
2564 } else {
2565 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2566 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2567 if (ret != 0) {
2568 drm_gem_object_unreference(&obj->base);
2569 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2570 i915.semaphores = 0;
2571 } else
2572 dev_priv->semaphore_obj = obj;
2573 }
2574 }
Mika Kuoppala72253422014-10-07 17:21:26 +03002575
Daniel Vetter8f0e2b92014-12-02 16:19:07 +01002576 ring->init_context = intel_rcs_ctx_init;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002577 ring->add_request = gen6_add_request;
2578 ring->flush = gen8_render_ring_flush;
2579 ring->irq_get = gen8_ring_get_irq;
2580 ring->irq_put = gen8_ring_put_irq;
2581 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2582 ring->get_seqno = gen6_ring_get_seqno;
2583 ring->set_seqno = ring_set_seqno;
2584 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002585 WARN_ON(!dev_priv->semaphore_obj);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002586 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002587 ring->semaphore.signal = gen8_rcs_signal;
2588 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002589 }
2590 } else if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002591 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03002592 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01002593 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03002594 ring->flush = gen6_render_ring_flush;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002595 ring->irq_get = gen6_ring_get_irq;
2596 ring->irq_put = gen6_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002597 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01002598 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002599 ring->set_seqno = ring_set_seqno;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002600 if (i915_semaphore_is_enabled(dev)) {
2601 ring->semaphore.sync_to = gen6_ring_sync;
2602 ring->semaphore.signal = gen6_signal;
2603 /*
2604 * The current semaphore is only applied on pre-gen8
2605 * platform. And there is no VCS2 ring on the pre-gen8
2606 * platform. So the semaphore between RCS and VCS2 is
2607 * initialized as INVALID. Gen8 will initialize the
2608 * sema between VCS2 and RCS later.
2609 */
2610 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2611 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2612 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2613 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2614 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2615 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2616 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2617 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2618 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2619 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2620 }
Chris Wilsonc6df5412010-12-15 09:56:50 +00002621 } else if (IS_GEN5(dev)) {
2622 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002623 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00002624 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002625 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002626 ring->irq_get = gen5_ring_get_irq;
2627 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002628 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2629 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002630 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002631 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002632 if (INTEL_INFO(dev)->gen < 4)
2633 ring->flush = gen2_render_ring_flush;
2634 else
2635 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002636 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002637 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002638 if (IS_GEN2(dev)) {
2639 ring->irq_get = i8xx_ring_get_irq;
2640 ring->irq_put = i8xx_ring_put_irq;
2641 } else {
2642 ring->irq_get = i9xx_ring_get_irq;
2643 ring->irq_put = i9xx_ring_put_irq;
2644 }
Daniel Vettere3670312012-04-11 22:12:53 +02002645 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002646 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002647 ring->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002648
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002649 if (IS_HASWELL(dev))
2650 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002651 else if (IS_GEN8(dev))
2652 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002653 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002654 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2655 else if (INTEL_INFO(dev)->gen >= 4)
2656 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2657 else if (IS_I830(dev) || IS_845G(dev))
2658 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2659 else
2660 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002661 ring->init_hw = init_render_ring;
Daniel Vetter59465b52012-04-11 22:12:48 +02002662 ring->cleanup = render_ring_cleanup;
2663
Daniel Vetterb45305f2012-12-17 16:21:27 +01002664 /* Workaround batchbuffer to combat CS tlb bug. */
2665 if (HAS_BROKEN_CS_TLB(dev)) {
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002666 obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002667 if (obj == NULL) {
2668 DRM_ERROR("Failed to allocate batch bo\n");
2669 return -ENOMEM;
2670 }
2671
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002672 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002673 if (ret != 0) {
2674 drm_gem_object_unreference(&obj->base);
2675 DRM_ERROR("Failed to ping batch bo\n");
2676 return ret;
2677 }
2678
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002679 ring->scratch.obj = obj;
2680 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002681 }
2682
Daniel Vetter99be1df2014-11-20 00:33:06 +01002683 ret = intel_init_ring_buffer(dev, ring);
2684 if (ret)
2685 return ret;
2686
2687 if (INTEL_INFO(dev)->gen >= 5) {
2688 ret = intel_init_pipe_control(ring);
2689 if (ret)
2690 return ret;
2691 }
2692
2693 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002694}
2695
2696int intel_init_bsd_ring_buffer(struct drm_device *dev)
2697{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002698 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002699 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002700
Daniel Vetter58fa3832012-04-11 22:12:49 +02002701 ring->name = "bsd ring";
2702 ring->id = VCS;
2703
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002704 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002705 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002706 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002707 /* gen6 bsd needs a special wa for tail updates */
2708 if (IS_GEN6(dev))
2709 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002710 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002711 ring->add_request = gen6_add_request;
2712 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002713 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002714 if (INTEL_INFO(dev)->gen >= 8) {
2715 ring->irq_enable_mask =
2716 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2717 ring->irq_get = gen8_ring_get_irq;
2718 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002719 ring->dispatch_execbuffer =
2720 gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002721 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002722 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002723 ring->semaphore.signal = gen8_xcs_signal;
2724 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002725 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002726 } else {
2727 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2728 ring->irq_get = gen6_ring_get_irq;
2729 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002730 ring->dispatch_execbuffer =
2731 gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002732 if (i915_semaphore_is_enabled(dev)) {
2733 ring->semaphore.sync_to = gen6_ring_sync;
2734 ring->semaphore.signal = gen6_signal;
2735 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2736 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2737 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2738 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2739 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2740 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2741 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2742 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2743 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2744 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2745 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002746 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002747 } else {
2748 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002749 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002750 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002751 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002752 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002753 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002754 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002755 ring->irq_get = gen5_ring_get_irq;
2756 ring->irq_put = gen5_ring_put_irq;
2757 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002758 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002759 ring->irq_get = i9xx_ring_get_irq;
2760 ring->irq_put = i9xx_ring_put_irq;
2761 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002762 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002763 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002764 ring->init_hw = init_ring_common;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002765
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002766 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002767}
Chris Wilson549f7362010-10-19 11:19:32 +01002768
Zhao Yakui845f74a2014-04-17 10:37:37 +08002769/**
Damien Lespiau62659922015-01-29 14:13:40 +00002770 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002771 */
2772int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2773{
2774 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002775 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002776
Rodrigo Vivif7b64232014-07-01 02:41:36 -07002777 ring->name = "bsd2 ring";
Zhao Yakui845f74a2014-04-17 10:37:37 +08002778 ring->id = VCS2;
2779
2780 ring->write_tail = ring_write_tail;
2781 ring->mmio_base = GEN8_BSD2_RING_BASE;
2782 ring->flush = gen6_bsd_ring_flush;
2783 ring->add_request = gen6_add_request;
2784 ring->get_seqno = gen6_ring_get_seqno;
2785 ring->set_seqno = ring_set_seqno;
2786 ring->irq_enable_mask =
2787 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2788 ring->irq_get = gen8_ring_get_irq;
2789 ring->irq_put = gen8_ring_put_irq;
2790 ring->dispatch_execbuffer =
2791 gen8_ring_dispatch_execbuffer;
Ben Widawsky3e789982014-06-30 09:53:37 -07002792 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002793 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002794 ring->semaphore.signal = gen8_xcs_signal;
2795 GEN8_RING_SEMAPHORE_INIT;
2796 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002797 ring->init_hw = init_ring_common;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002798
2799 return intel_init_ring_buffer(dev, ring);
2800}
2801
Chris Wilson549f7362010-10-19 11:19:32 +01002802int intel_init_blt_ring_buffer(struct drm_device *dev)
2803{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002804 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002805 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002806
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002807 ring->name = "blitter ring";
2808 ring->id = BCS;
2809
2810 ring->mmio_base = BLT_RING_BASE;
2811 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002812 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002813 ring->add_request = gen6_add_request;
2814 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002815 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002816 if (INTEL_INFO(dev)->gen >= 8) {
2817 ring->irq_enable_mask =
2818 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2819 ring->irq_get = gen8_ring_get_irq;
2820 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002821 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002822 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002823 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002824 ring->semaphore.signal = gen8_xcs_signal;
2825 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002826 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002827 } else {
2828 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2829 ring->irq_get = gen6_ring_get_irq;
2830 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002831 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002832 if (i915_semaphore_is_enabled(dev)) {
2833 ring->semaphore.signal = gen6_signal;
2834 ring->semaphore.sync_to = gen6_ring_sync;
2835 /*
2836 * The current semaphore is only applied on pre-gen8
2837 * platform. And there is no VCS2 ring on the pre-gen8
2838 * platform. So the semaphore between BCS and VCS2 is
2839 * initialized as INVALID. Gen8 will initialize the
2840 * sema between BCS and VCS2 later.
2841 */
2842 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2843 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2844 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2845 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2846 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2847 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2848 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2849 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2850 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2851 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2852 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002853 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002854 ring->init_hw = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002855
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002856 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002857}
Chris Wilsona7b97612012-07-20 12:41:08 +01002858
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002859int intel_init_vebox_ring_buffer(struct drm_device *dev)
2860{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002861 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002862 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002863
2864 ring->name = "video enhancement ring";
2865 ring->id = VECS;
2866
2867 ring->mmio_base = VEBOX_RING_BASE;
2868 ring->write_tail = ring_write_tail;
2869 ring->flush = gen6_ring_flush;
2870 ring->add_request = gen6_add_request;
2871 ring->get_seqno = gen6_ring_get_seqno;
2872 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002873
2874 if (INTEL_INFO(dev)->gen >= 8) {
2875 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002876 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002877 ring->irq_get = gen8_ring_get_irq;
2878 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002879 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002880 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002881 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002882 ring->semaphore.signal = gen8_xcs_signal;
2883 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002884 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002885 } else {
2886 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2887 ring->irq_get = hsw_vebox_get_irq;
2888 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002889 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002890 if (i915_semaphore_is_enabled(dev)) {
2891 ring->semaphore.sync_to = gen6_ring_sync;
2892 ring->semaphore.signal = gen6_signal;
2893 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2894 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2895 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2896 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2897 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2898 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2899 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2900 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2901 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2902 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2903 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002904 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002905 ring->init_hw = init_ring_common;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002906
2907 return intel_init_ring_buffer(dev, ring);
2908}
2909
Chris Wilsona7b97612012-07-20 12:41:08 +01002910int
John Harrison4866d722015-05-29 17:43:55 +01002911intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002912{
John Harrison4866d722015-05-29 17:43:55 +01002913 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002914 int ret;
2915
2916 if (!ring->gpu_caches_dirty)
2917 return 0;
2918
John Harrisona84c3ae2015-05-29 17:43:57 +01002919 ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002920 if (ret)
2921 return ret;
2922
John Harrisona84c3ae2015-05-29 17:43:57 +01002923 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002924
2925 ring->gpu_caches_dirty = false;
2926 return 0;
2927}
2928
2929int
John Harrison2f200552015-05-29 17:43:53 +01002930intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002931{
John Harrison2f200552015-05-29 17:43:53 +01002932 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002933 uint32_t flush_domains;
2934 int ret;
2935
2936 flush_domains = 0;
2937 if (ring->gpu_caches_dirty)
2938 flush_domains = I915_GEM_GPU_DOMAINS;
2939
John Harrisona84c3ae2015-05-29 17:43:57 +01002940 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002941 if (ret)
2942 return ret;
2943
John Harrisona84c3ae2015-05-29 17:43:57 +01002944 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002945
2946 ring->gpu_caches_dirty = false;
2947 return 0;
2948}
Chris Wilsone3efda42014-04-09 09:19:41 +01002949
2950void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002951intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002952{
2953 int ret;
2954
2955 if (!intel_ring_initialized(ring))
2956 return;
2957
2958 ret = intel_ring_idle(ring);
2959 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2960 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2961 ring->name, ret);
2962
2963 stop_ring(ring);
2964}