blob: 44fdfd07d912a4b29c1cf5b44f5805a565a30a8b [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
Ben Widawsky3e789982014-06-30 09:53:37 -07001174static int gen8_rcs_signal(struct intel_engine_cs *signaller,
1175 unsigned int num_dwords)
1176{
1177#define MBOX_UPDATE_DWORDS 8
1178 struct drm_device *dev = signaller->dev;
1179 struct drm_i915_private *dev_priv = dev->dev_private;
1180 struct intel_engine_cs *waiter;
1181 int i, ret, num_rings;
1182
1183 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1184 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1185#undef MBOX_UPDATE_DWORDS
1186
1187 ret = intel_ring_begin(signaller, num_dwords);
1188 if (ret)
1189 return ret;
1190
1191 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001192 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001193 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1194 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1195 continue;
1196
John Harrison6259cea2014-11-24 18:49:29 +00001197 seqno = i915_gem_request_get_seqno(
1198 signaller->outstanding_lazy_request);
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
1215static int gen8_xcs_signal(struct intel_engine_cs *signaller,
1216 unsigned int num_dwords)
1217{
1218#define MBOX_UPDATE_DWORDS 6
1219 struct drm_device *dev = signaller->dev;
1220 struct drm_i915_private *dev_priv = dev->dev_private;
1221 struct intel_engine_cs *waiter;
1222 int i, ret, num_rings;
1223
1224 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1225 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1226#undef MBOX_UPDATE_DWORDS
1227
1228 ret = intel_ring_begin(signaller, num_dwords);
1229 if (ret)
1230 return ret;
1231
1232 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001233 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001234 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1235 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1236 continue;
1237
John Harrison6259cea2014-11-24 18:49:29 +00001238 seqno = i915_gem_request_get_seqno(
1239 signaller->outstanding_lazy_request);
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
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001254static int gen6_signal(struct intel_engine_cs *signaller,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001255 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001256{
Ben Widawsky024a43e2014-04-29 14:52:30 -07001257 struct drm_device *dev = signaller->dev;
1258 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001259 struct intel_engine_cs *useless;
Ben Widawskya1444b72014-06-30 09:53:35 -07001260 int i, ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001261
Ben Widawskya1444b72014-06-30 09:53:35 -07001262#define MBOX_UPDATE_DWORDS 3
1263 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1264 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1265#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001266
1267 ret = intel_ring_begin(signaller, num_dwords);
1268 if (ret)
1269 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001270
Ben Widawsky78325f22014-04-29 14:52:29 -07001271 for_each_ring(useless, dev_priv, i) {
1272 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1273 if (mbox_reg != GEN6_NOSYNC) {
John Harrison6259cea2014-11-24 18:49:29 +00001274 u32 seqno = i915_gem_request_get_seqno(
1275 signaller->outstanding_lazy_request);
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)
1304 ret = ring->semaphore.signal(ring, 4);
1305 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
1336gen8_ring_sync(struct intel_engine_cs *waiter,
1337 struct intel_engine_cs *signaller,
1338 u32 seqno)
1339{
1340 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
1341 int ret;
1342
1343 ret = intel_ring_begin(waiter, 4);
1344 if (ret)
1345 return ret;
1346
1347 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1348 MI_SEMAPHORE_GLOBAL_GTT |
Ben Widawskybae4fcd2014-06-30 09:53:43 -07001349 MI_SEMAPHORE_POLL |
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001350 MI_SEMAPHORE_SAD_GTE_SDD);
1351 intel_ring_emit(waiter, seqno);
1352 intel_ring_emit(waiter,
1353 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1354 intel_ring_emit(waiter,
1355 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1356 intel_ring_advance(waiter);
1357 return 0;
1358}
1359
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001360static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001361gen6_ring_sync(struct intel_engine_cs *waiter,
1362 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001363 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001364{
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001365 u32 dw1 = MI_SEMAPHORE_MBOX |
1366 MI_SEMAPHORE_COMPARE |
1367 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001368 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1369 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001370
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001371 /* Throughout all of the GEM code, seqno passed implies our current
1372 * seqno is >= the last seqno executed. However for hardware the
1373 * comparison is strictly greater than.
1374 */
1375 seqno -= 1;
1376
Ben Widawskyebc348b2014-04-29 14:52:28 -07001377 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001378
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001379 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001380 if (ret)
1381 return ret;
1382
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001383 /* If seqno wrap happened, omit the wait with no-ops */
1384 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001385 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001386 intel_ring_emit(waiter, seqno);
1387 intel_ring_emit(waiter, 0);
1388 intel_ring_emit(waiter, MI_NOOP);
1389 } else {
1390 intel_ring_emit(waiter, MI_NOOP);
1391 intel_ring_emit(waiter, MI_NOOP);
1392 intel_ring_emit(waiter, MI_NOOP);
1393 intel_ring_emit(waiter, MI_NOOP);
1394 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001395 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001396
1397 return 0;
1398}
1399
Chris Wilsonc6df5412010-12-15 09:56:50 +00001400#define PIPE_CONTROL_FLUSH(ring__, addr__) \
1401do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001402 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1403 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +00001404 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1405 intel_ring_emit(ring__, 0); \
1406 intel_ring_emit(ring__, 0); \
1407} while (0)
1408
1409static int
John Harrisonee044a82015-05-29 17:44:00 +01001410pc_render_add_request(struct drm_i915_gem_request *req)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001411{
John Harrisonee044a82015-05-29 17:44:00 +01001412 struct intel_engine_cs *ring = req->ring;
Chris Wilson18393f62014-04-09 09:19:40 +01001413 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001414 int ret;
1415
1416 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1417 * incoherent with writes to memory, i.e. completely fubar,
1418 * so we need to use PIPE_NOTIFY instead.
1419 *
1420 * However, we also need to workaround the qword write
1421 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1422 * memory before requesting an interrupt.
1423 */
1424 ret = intel_ring_begin(ring, 32);
1425 if (ret)
1426 return ret;
1427
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001428 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001429 PIPE_CONTROL_WRITE_FLUSH |
1430 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001431 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrisonee044a82015-05-29 17:44:00 +01001432 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001433 intel_ring_emit(ring, 0);
1434 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001435 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +00001436 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001437 scratch_addr += 2 * CACHELINE_BYTES;
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 Wilsona71d8d92012-02-15 11:25:36 +00001445
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001446 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001447 PIPE_CONTROL_WRITE_FLUSH |
1448 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001449 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001450 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrisonee044a82015-05-29 17:44:00 +01001451 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001452 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +01001453 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001454
Chris Wilsonc6df5412010-12-15 09:56:50 +00001455 return 0;
1456}
1457
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001458static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001459gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001460{
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001461 /* Workaround to force correct ordering between irq and seqno writes on
1462 * ivb (and maybe also on snb) by reading from a CS register (like
1463 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +00001464 if (!lazy_coherency) {
1465 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1466 POSTING_READ(RING_ACTHD(ring->mmio_base));
1467 }
1468
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001469 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1470}
1471
1472static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001473ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001474{
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001475 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1476}
1477
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001478static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001479ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001480{
1481 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1482}
1483
Chris Wilsonc6df5412010-12-15 09:56:50 +00001484static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001485pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001486{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001487 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001488}
1489
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001490static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001491pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001492{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001493 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001494}
1495
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001496static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001497gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001498{
1499 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001500 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001501 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001502
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001503 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Daniel Vettere48d8632012-04-11 22:12:54 +02001504 return false;
1505
Chris Wilson7338aef2012-04-24 21:48:47 +01001506 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001507 if (ring->irq_refcount++ == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001508 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001509 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001510
1511 return true;
1512}
1513
1514static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001515gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001516{
1517 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001518 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001519 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001520
Chris Wilson7338aef2012-04-24 21:48:47 +01001521 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001522 if (--ring->irq_refcount == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001523 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001524 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001525}
1526
1527static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001528i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001529{
Chris Wilson78501ea2010-10-27 12:18:21 +01001530 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001531 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001532 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001533
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001534 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001535 return false;
1536
Chris Wilson7338aef2012-04-24 21:48:47 +01001537 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001538 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001539 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1540 I915_WRITE(IMR, dev_priv->irq_mask);
1541 POSTING_READ(IMR);
1542 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001543 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001544
1545 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001546}
1547
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001548static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001549i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001550{
Chris Wilson78501ea2010-10-27 12:18:21 +01001551 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001552 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001553 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001554
Chris Wilson7338aef2012-04-24 21:48:47 +01001555 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001556 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001557 dev_priv->irq_mask |= ring->irq_enable_mask;
1558 I915_WRITE(IMR, dev_priv->irq_mask);
1559 POSTING_READ(IMR);
1560 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001561 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001562}
1563
Chris Wilsonc2798b12012-04-22 21:13:57 +01001564static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001565i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001566{
1567 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001568 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001569 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001570
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001571 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonc2798b12012-04-22 21:13:57 +01001572 return false;
1573
Chris Wilson7338aef2012-04-24 21:48:47 +01001574 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001575 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001576 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1577 I915_WRITE16(IMR, dev_priv->irq_mask);
1578 POSTING_READ16(IMR);
1579 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001580 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001581
1582 return true;
1583}
1584
1585static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001586i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001587{
1588 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001589 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001590 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001591
Chris Wilson7338aef2012-04-24 21:48:47 +01001592 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001593 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001594 dev_priv->irq_mask |= ring->irq_enable_mask;
1595 I915_WRITE16(IMR, dev_priv->irq_mask);
1596 POSTING_READ16(IMR);
1597 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001598 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001599}
1600
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001601static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001602bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001603 u32 invalidate_domains,
1604 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001605{
John Harrisona84c3ae2015-05-29 17:43:57 +01001606 struct intel_engine_cs *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001607 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001608
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001609 ret = intel_ring_begin(ring, 2);
1610 if (ret)
1611 return ret;
1612
1613 intel_ring_emit(ring, MI_FLUSH);
1614 intel_ring_emit(ring, MI_NOOP);
1615 intel_ring_advance(ring);
1616 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001617}
1618
Chris Wilson3cce4692010-10-27 16:11:02 +01001619static int
John Harrisonee044a82015-05-29 17:44:00 +01001620i9xx_add_request(struct drm_i915_gem_request *req)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001621{
John Harrisonee044a82015-05-29 17:44:00 +01001622 struct intel_engine_cs *ring = req->ring;
Chris Wilson3cce4692010-10-27 16:11:02 +01001623 int ret;
1624
1625 ret = intel_ring_begin(ring, 4);
1626 if (ret)
1627 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001628
Chris Wilson3cce4692010-10-27 16:11:02 +01001629 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1630 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrisonee044a82015-05-29 17:44:00 +01001631 intel_ring_emit(ring, i915_gem_request_get_seqno(req));
Chris Wilson3cce4692010-10-27 16:11:02 +01001632 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001633 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001634
Chris Wilson3cce4692010-10-27 16:11:02 +01001635 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001636}
1637
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001638static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001639gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001640{
1641 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001642 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001643 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001644
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001645 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1646 return false;
Chris Wilson0f468322011-01-04 17:35:21 +00001647
Chris Wilson7338aef2012-04-24 21:48:47 +01001648 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001649 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001650 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001651 I915_WRITE_IMR(ring,
1652 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001653 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001654 else
1655 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001656 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001657 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001658 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001659
1660 return true;
1661}
1662
1663static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001664gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001665{
1666 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001667 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001668 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001669
Chris Wilson7338aef2012-04-24 21:48:47 +01001670 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001671 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001672 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001673 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001674 else
1675 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001676 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001677 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001678 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001679}
1680
Ben Widawskya19d2932013-05-28 19:22:30 -07001681static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001682hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001683{
1684 struct drm_device *dev = ring->dev;
1685 struct drm_i915_private *dev_priv = dev->dev_private;
1686 unsigned long flags;
1687
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001688 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskya19d2932013-05-28 19:22:30 -07001689 return false;
1690
Daniel Vetter59cdb632013-07-04 23:35:28 +02001691 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001692 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001693 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001694 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001695 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001696 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001697
1698 return true;
1699}
1700
1701static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001702hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001703{
1704 struct drm_device *dev = ring->dev;
1705 struct drm_i915_private *dev_priv = dev->dev_private;
1706 unsigned long flags;
1707
Daniel Vetter59cdb632013-07-04 23:35:28 +02001708 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001709 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001710 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001711 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001712 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001713 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001714}
1715
Ben Widawskyabd58f02013-11-02 21:07:09 -07001716static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001717gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001718{
1719 struct drm_device *dev = ring->dev;
1720 struct drm_i915_private *dev_priv = dev->dev_private;
1721 unsigned long flags;
1722
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001723 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskyabd58f02013-11-02 21:07:09 -07001724 return false;
1725
1726 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1727 if (ring->irq_refcount++ == 0) {
1728 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1729 I915_WRITE_IMR(ring,
1730 ~(ring->irq_enable_mask |
1731 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1732 } else {
1733 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1734 }
1735 POSTING_READ(RING_IMR(ring->mmio_base));
1736 }
1737 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1738
1739 return true;
1740}
1741
1742static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001743gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001744{
1745 struct drm_device *dev = ring->dev;
1746 struct drm_i915_private *dev_priv = dev->dev_private;
1747 unsigned long flags;
1748
1749 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1750 if (--ring->irq_refcount == 0) {
1751 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1752 I915_WRITE_IMR(ring,
1753 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1754 } else {
1755 I915_WRITE_IMR(ring, ~0);
1756 }
1757 POSTING_READ(RING_IMR(ring->mmio_base));
1758 }
1759 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1760}
1761
Zou Nan haid1b851f2010-05-21 09:08:57 +08001762static int
John Harrison53fddaf2015-05-29 17:44:02 +01001763i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001764 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001765 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001766{
John Harrison53fddaf2015-05-29 17:44:02 +01001767 struct intel_engine_cs *ring = req->ring;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001768 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001769
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001770 ret = intel_ring_begin(ring, 2);
1771 if (ret)
1772 return ret;
1773
Chris Wilson78501ea2010-10-27 12:18:21 +01001774 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001775 MI_BATCH_BUFFER_START |
1776 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001777 (dispatch_flags & I915_DISPATCH_SECURE ?
1778 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001779 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001780 intel_ring_advance(ring);
1781
Zou Nan haid1b851f2010-05-21 09:08:57 +08001782 return 0;
1783}
1784
Daniel Vetterb45305f2012-12-17 16:21:27 +01001785/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1786#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001787#define I830_TLB_ENTRIES (2)
1788#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001789static int
John Harrison53fddaf2015-05-29 17:44:02 +01001790i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001791 u64 offset, u32 len,
1792 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001793{
John Harrison53fddaf2015-05-29 17:44:02 +01001794 struct intel_engine_cs *ring = req->ring;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001795 u32 cs_offset = ring->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001796 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001797
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001798 ret = intel_ring_begin(ring, 6);
1799 if (ret)
1800 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001801
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001802 /* Evict the invalid PTE TLBs */
1803 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1804 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1805 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1806 intel_ring_emit(ring, cs_offset);
1807 intel_ring_emit(ring, 0xdeadbeef);
1808 intel_ring_emit(ring, MI_NOOP);
1809 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001810
John Harrison8e004ef2015-02-13 11:48:10 +00001811 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001812 if (len > I830_BATCH_LIMIT)
1813 return -ENOSPC;
1814
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001815 ret = intel_ring_begin(ring, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001816 if (ret)
1817 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001818
1819 /* Blit the batch (which has now all relocs applied) to the
1820 * stable batch scratch bo area (so that the CS never
1821 * stumbles over its tlb invalidation bug) ...
1822 */
1823 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1824 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilson611a7a42014-09-12 07:37:42 +01001825 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001826 intel_ring_emit(ring, cs_offset);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001827 intel_ring_emit(ring, 4096);
1828 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001829
Daniel Vetterb45305f2012-12-17 16:21:27 +01001830 intel_ring_emit(ring, MI_FLUSH);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001831 intel_ring_emit(ring, MI_NOOP);
1832 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001833
1834 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001835 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001836 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001837
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001838 ret = intel_ring_begin(ring, 4);
1839 if (ret)
1840 return ret;
1841
1842 intel_ring_emit(ring, MI_BATCH_BUFFER);
John Harrison8e004ef2015-02-13 11:48:10 +00001843 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1844 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001845 intel_ring_emit(ring, offset + len - 8);
1846 intel_ring_emit(ring, MI_NOOP);
1847 intel_ring_advance(ring);
1848
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001849 return 0;
1850}
1851
1852static int
John Harrison53fddaf2015-05-29 17:44:02 +01001853i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001854 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001855 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001856{
John Harrison53fddaf2015-05-29 17:44:02 +01001857 struct intel_engine_cs *ring = req->ring;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001858 int ret;
1859
1860 ret = intel_ring_begin(ring, 2);
1861 if (ret)
1862 return ret;
1863
Chris Wilson65f56872012-04-17 16:38:12 +01001864 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
John Harrison8e004ef2015-02-13 11:48:10 +00001865 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1866 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001867 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001868
Eric Anholt62fdfea2010-05-21 13:26:39 -07001869 return 0;
1870}
1871
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001872static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001873{
Chris Wilson05394f32010-11-08 19:18:58 +00001874 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001875
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001876 obj = ring->status_page.obj;
1877 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001878 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001879
Chris Wilson9da3da62012-06-01 15:20:22 +01001880 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001881 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001882 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001883 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001884}
1885
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001886static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001887{
Chris Wilson05394f32010-11-08 19:18:58 +00001888 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001889
Chris Wilsone3efda42014-04-09 09:19:41 +01001890 if ((obj = ring->status_page.obj) == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001891 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001892 int ret;
1893
1894 obj = i915_gem_alloc_object(ring->dev, 4096);
1895 if (obj == NULL) {
1896 DRM_ERROR("Failed to allocate status page\n");
1897 return -ENOMEM;
1898 }
1899
1900 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1901 if (ret)
1902 goto err_unref;
1903
Chris Wilson1f767e02014-07-03 17:33:03 -04001904 flags = 0;
1905 if (!HAS_LLC(ring->dev))
1906 /* On g33, we cannot place HWS above 256MiB, so
1907 * restrict its pinning to the low mappable arena.
1908 * Though this restriction is not documented for
1909 * gen4, gen5, or byt, they also behave similarly
1910 * and hang if the HWS is placed at the top of the
1911 * GTT. To generalise, it appears that all !llc
1912 * platforms have issues with us placing the HWS
1913 * above the mappable region (even though we never
1914 * actualy map it).
1915 */
1916 flags |= PIN_MAPPABLE;
1917 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001918 if (ret) {
1919err_unref:
1920 drm_gem_object_unreference(&obj->base);
1921 return ret;
1922 }
1923
1924 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001925 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001926
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001927 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001928 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001929 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001930
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001931 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1932 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001933
1934 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001935}
1936
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001937static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001938{
1939 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001940
1941 if (!dev_priv->status_page_dmah) {
1942 dev_priv->status_page_dmah =
1943 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1944 if (!dev_priv->status_page_dmah)
1945 return -ENOMEM;
1946 }
1947
Chris Wilson6b8294a2012-11-16 11:43:20 +00001948 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1949 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1950
1951 return 0;
1952}
1953
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001954void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1955{
1956 iounmap(ringbuf->virtual_start);
1957 ringbuf->virtual_start = NULL;
1958 i915_gem_object_ggtt_unpin(ringbuf->obj);
1959}
1960
1961int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
1962 struct intel_ringbuffer *ringbuf)
1963{
1964 struct drm_i915_private *dev_priv = to_i915(dev);
1965 struct drm_i915_gem_object *obj = ringbuf->obj;
1966 int ret;
1967
1968 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1969 if (ret)
1970 return ret;
1971
1972 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1973 if (ret) {
1974 i915_gem_object_ggtt_unpin(obj);
1975 return ret;
1976 }
1977
1978 ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
1979 i915_gem_obj_ggtt_offset(obj), ringbuf->size);
1980 if (ringbuf->virtual_start == NULL) {
1981 i915_gem_object_ggtt_unpin(obj);
1982 return -EINVAL;
1983 }
1984
1985 return 0;
1986}
1987
Oscar Mateo84c23772014-07-24 17:04:15 +01001988void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01001989{
Oscar Mateo2919d292014-07-03 16:28:02 +01001990 drm_gem_object_unreference(&ringbuf->obj->base);
1991 ringbuf->obj = NULL;
1992}
1993
Oscar Mateo84c23772014-07-24 17:04:15 +01001994int intel_alloc_ringbuffer_obj(struct drm_device *dev,
1995 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01001996{
Chris Wilsone3efda42014-04-09 09:19:41 +01001997 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01001998
1999 obj = NULL;
2000 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002001 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002002 if (obj == NULL)
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002003 obj = i915_gem_alloc_object(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002004 if (obj == NULL)
2005 return -ENOMEM;
2006
Akash Goel24f3a8c2014-06-17 10:59:42 +05302007 /* mark ring buffers as read-only from GPU side by default */
2008 obj->gt_ro = 1;
2009
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002010 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002011
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002012 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002013}
2014
Ben Widawskyc43b5632012-04-16 14:07:40 -07002015static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002016 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002017{
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002018 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002019 int ret;
2020
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002021 WARN_ON(ring->buffer);
2022
2023 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2024 if (!ringbuf)
2025 return -ENOMEM;
2026 ring->buffer = ringbuf;
Oscar Mateo8ee14972014-05-22 14:13:34 +01002027
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002028 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01002029 INIT_LIST_HEAD(&ring->active_list);
2030 INIT_LIST_HEAD(&ring->request_list);
Oscar Mateocc9130b2014-07-24 17:04:42 +01002031 INIT_LIST_HEAD(&ring->execlist_queue);
Chris Wilson06fbca72015-04-07 16:20:36 +01002032 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002033 ringbuf->size = 32 * PAGE_SIZE;
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002034 ringbuf->ring = ring;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002035 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002036
Chris Wilsonb259f672011-03-29 13:19:09 +01002037 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002038
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002039 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002040 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002041 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002042 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002043 } else {
2044 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002045 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002046 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002047 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002048 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002049
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002050 WARN_ON(ringbuf->obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002051
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002052 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2053 if (ret) {
2054 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
2055 ring->name, ret);
2056 goto error;
2057 }
2058
2059 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2060 if (ret) {
2061 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2062 ring->name, ret);
2063 intel_destroy_ringbuffer_obj(ringbuf);
2064 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002065 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002066
Chris Wilson55249ba2010-12-22 14:04:47 +00002067 /* Workaround an erratum on the i830 which causes a hang if
2068 * the TAIL pointer points to within the last 2 cachelines
2069 * of the buffer.
2070 */
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002071 ringbuf->effective_size = ringbuf->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01002072 if (IS_I830(dev) || IS_845G(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002073 ringbuf->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00002074
Brad Volkin44e895a2014-05-10 14:10:43 -07002075 ret = i915_cmd_parser_init_ring(ring);
2076 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002077 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002078
Oscar Mateo8ee14972014-05-22 14:13:34 +01002079 return 0;
2080
2081error:
2082 kfree(ringbuf);
2083 ring->buffer = NULL;
2084 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002085}
2086
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002087void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002088{
John Harrison6402c332014-10-31 12:00:26 +00002089 struct drm_i915_private *dev_priv;
2090 struct intel_ringbuffer *ringbuf;
Chris Wilson33626e62010-10-29 16:18:36 +01002091
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002092 if (!intel_ring_initialized(ring))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002093 return;
2094
John Harrison6402c332014-10-31 12:00:26 +00002095 dev_priv = to_i915(ring->dev);
2096 ringbuf = ring->buffer;
2097
Chris Wilsone3efda42014-04-09 09:19:41 +01002098 intel_stop_ring_buffer(ring);
Ville Syrjäläde8f0a52014-05-28 19:12:13 +03002099 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002100
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002101 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateo2919d292014-07-03 16:28:02 +01002102 intel_destroy_ringbuffer_obj(ringbuf);
John Harrison6259cea2014-11-24 18:49:29 +00002103 i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
Chris Wilson78501ea2010-10-27 12:18:21 +01002104
Zou Nan hai8d192152010-11-02 16:31:01 +08002105 if (ring->cleanup)
2106 ring->cleanup(ring);
2107
Chris Wilson78501ea2010-10-27 12:18:21 +01002108 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07002109
2110 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01002111 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002112
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002113 kfree(ringbuf);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002114 ring->buffer = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002115}
2116
Chris Wilson595e1ee2015-04-07 16:20:51 +01002117static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002118{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002119 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002120 struct drm_i915_gem_request *request;
Chris Wilsonb4716182015-04-27 13:41:17 +01002121 unsigned space;
2122 int ret;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002123
John Harrison29b1b412015-06-18 13:10:09 +01002124 /* The whole point of reserving space is to not wait! */
2125 WARN_ON(ringbuf->reserved_in_use);
2126
Dave Gordonebd0fd42014-11-27 11:22:49 +00002127 if (intel_ring_space(ringbuf) >= n)
2128 return 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002129
2130 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002131 space = __intel_ring_space(request->postfix, ringbuf->tail,
2132 ringbuf->size);
2133 if (space >= n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002134 break;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002135 }
2136
Chris Wilson595e1ee2015-04-07 16:20:51 +01002137 if (WARN_ON(&request->list == &ring->request_list))
Chris Wilsona71d8d92012-02-15 11:25:36 +00002138 return -ENOSPC;
2139
Daniel Vettera4b3a572014-11-26 14:17:05 +01002140 ret = i915_wait_request(request);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002141 if (ret)
2142 return ret;
2143
Chris Wilsonb4716182015-04-27 13:41:17 +01002144 ringbuf->space = space;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002145 return 0;
2146}
2147
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002148static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002149{
2150 uint32_t __iomem *virt;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002151 struct intel_ringbuffer *ringbuf = ring->buffer;
2152 int rem = ringbuf->size - ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002153
John Harrison29b1b412015-06-18 13:10:09 +01002154 /* Can't wrap if space has already been reserved! */
2155 WARN_ON(ringbuf->reserved_in_use);
2156
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002157 if (ringbuf->space < rem) {
Chris Wilson3e960502012-11-27 16:22:54 +00002158 int ret = ring_wait_for_space(ring, rem);
2159 if (ret)
2160 return ret;
2161 }
2162
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002163 virt = ringbuf->virtual_start + ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002164 rem /= 4;
2165 while (rem--)
2166 iowrite32(MI_NOOP, virt++);
2167
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002168 ringbuf->tail = 0;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002169 intel_ring_update_space(ringbuf);
Chris Wilson3e960502012-11-27 16:22:54 +00002170
2171 return 0;
2172}
2173
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002174int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002175{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002176 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002177
2178 /* We need to add any requests required to flush the objects and ring */
John Harrison75289872015-05-29 17:43:49 +01002179 WARN_ON(ring->outstanding_lazy_request);
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002180 if (ring->outstanding_lazy_request)
John Harrison75289872015-05-29 17:43:49 +01002181 i915_add_request(ring->outstanding_lazy_request);
Chris Wilson3e960502012-11-27 16:22:54 +00002182
2183 /* Wait upon the last request to be completed */
2184 if (list_empty(&ring->request_list))
2185 return 0;
2186
Daniel Vettera4b3a572014-11-26 14:17:05 +01002187 req = list_entry(ring->request_list.prev,
Chris Wilsonb4716182015-04-27 13:41:17 +01002188 struct drm_i915_gem_request,
2189 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002190
Chris Wilsonb4716182015-04-27 13:41:17 +01002191 /* Make sure we do not trigger any retires */
2192 return __i915_wait_request(req,
2193 atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
2194 to_i915(ring->dev)->mm.interruptible,
2195 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002196}
2197
John Harrison6689cb22015-03-19 12:30:08 +00002198int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002199{
John Harrison6689cb22015-03-19 12:30:08 +00002200 request->ringbuf = request->ring->buffer;
John Harrison9eba5d42014-11-24 18:49:23 +00002201 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002202}
2203
John Harrison29b1b412015-06-18 13:10:09 +01002204void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2205{
2206 /* NB: Until request management is fully tidied up and the OLR is
2207 * removed, there are too many ways for get false hits on this
2208 * anti-recursion check! */
2209 /*WARN_ON(ringbuf->reserved_size);*/
2210 WARN_ON(ringbuf->reserved_in_use);
2211
2212 ringbuf->reserved_size = size;
2213
2214 /*
2215 * Really need to call _begin() here but that currently leads to
2216 * recursion problems! This will be fixed later but for now just
2217 * return and hope for the best. Note that there is only a real
2218 * problem if the create of the request never actually calls _begin()
2219 * but if they are not submitting any work then why did they create
2220 * the request in the first place?
2221 */
2222}
2223
2224void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2225{
2226 WARN_ON(ringbuf->reserved_in_use);
2227
2228 ringbuf->reserved_size = 0;
2229 ringbuf->reserved_in_use = false;
2230}
2231
2232void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2233{
2234 WARN_ON(ringbuf->reserved_in_use);
2235
2236 ringbuf->reserved_in_use = true;
2237 ringbuf->reserved_tail = ringbuf->tail;
2238}
2239
2240void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2241{
2242 WARN_ON(!ringbuf->reserved_in_use);
2243 WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2244 "request reserved size too small: %d vs %d!\n",
2245 ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2246
2247 ringbuf->reserved_size = 0;
2248 ringbuf->reserved_in_use = false;
2249}
2250
2251static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002252{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002253 struct intel_ringbuffer *ringbuf = ring->buffer;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002254 int ret;
2255
John Harrison29b1b412015-06-18 13:10:09 +01002256 /*
2257 * Add on the reserved size to the request to make sure that after
2258 * the intended commands have been emitted, there is guaranteed to
2259 * still be enough free space to send them to the hardware.
2260 */
2261 if (!ringbuf->reserved_in_use)
2262 bytes += ringbuf->reserved_size;
2263
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002264 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002265 ret = intel_wrap_ring_buffer(ring);
2266 if (unlikely(ret))
2267 return ret;
John Harrison29b1b412015-06-18 13:10:09 +01002268
2269 if(ringbuf->reserved_size) {
2270 uint32_t size = ringbuf->reserved_size;
2271
2272 intel_ring_reserved_space_cancel(ringbuf);
2273 intel_ring_reserved_space_reserve(ringbuf, size);
2274 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002275 }
2276
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002277 if (unlikely(ringbuf->space < bytes)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002278 ret = ring_wait_for_space(ring, bytes);
2279 if (unlikely(ret))
2280 return ret;
2281 }
2282
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002283 return 0;
2284}
2285
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002286int intel_ring_begin(struct intel_engine_cs *ring,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002287 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002288{
John Harrison217e46b2015-05-29 17:43:29 +01002289 struct drm_i915_gem_request *req;
Jani Nikula4640c4f2014-03-31 14:27:19 +03002290 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002291 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01002292
Daniel Vetter33196de2012-11-14 17:14:05 +01002293 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2294 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02002295 if (ret)
2296 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00002297
Chris Wilson304d6952014-01-02 14:32:35 +00002298 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
2299 if (ret)
2300 return ret;
2301
Chris Wilson9d7730912012-11-27 16:22:52 +00002302 /* Preallocate the olr before touching the ring */
John Harrison217e46b2015-05-29 17:43:29 +01002303 ret = i915_gem_request_alloc(ring, ring->default_context, &req);
Chris Wilson9d7730912012-11-27 16:22:52 +00002304 if (ret)
2305 return ret;
2306
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002307 ring->buffer->space -= num_dwords * sizeof(uint32_t);
Chris Wilson304d6952014-01-02 14:32:35 +00002308 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002309}
2310
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002311/* Align the ring tail to a cacheline boundary */
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002312int intel_ring_cacheline_align(struct intel_engine_cs *ring)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002313{
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002314 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002315 int ret;
2316
2317 if (num_dwords == 0)
2318 return 0;
2319
Chris Wilson18393f62014-04-09 09:19:40 +01002320 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002321 ret = intel_ring_begin(ring, num_dwords);
2322 if (ret)
2323 return ret;
2324
2325 while (num_dwords--)
2326 intel_ring_emit(ring, MI_NOOP);
2327
2328 intel_ring_advance(ring);
2329
2330 return 0;
2331}
2332
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002333void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002334{
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002335 struct drm_device *dev = ring->dev;
2336 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002337
John Harrison6259cea2014-11-24 18:49:29 +00002338 BUG_ON(ring->outstanding_lazy_request);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002339
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002340 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002341 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2342 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002343 if (HAS_VEBOX(dev))
Ben Widawsky50201502013-08-12 16:53:03 -07002344 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002345 }
Chris Wilson297b0c52010-10-22 17:02:41 +01002346
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002347 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03002348 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01002349}
2350
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002351static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002352 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002353{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002354 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002355
2356 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002357
Chris Wilson12f55812012-07-05 17:14:01 +01002358 /* Disable notification that the ring is IDLE. The GT
2359 * will then assume that it is busy and bring it out of rc6.
2360 */
2361 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2362 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2363
2364 /* Clear the context id. Here be magic! */
2365 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2366
2367 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04002368 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01002369 GEN6_BSD_SLEEP_INDICATOR) == 0,
2370 50))
2371 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002372
Chris Wilson12f55812012-07-05 17:14:01 +01002373 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04002374 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01002375 POSTING_READ(RING_TAIL(ring->mmio_base));
2376
2377 /* Let the ring send IDLE messages to the GT again,
2378 * and so let it sleep to conserve power when idle.
2379 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002380 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01002381 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002382}
2383
John Harrisona84c3ae2015-05-29 17:43:57 +01002384static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002385 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002386{
John Harrisona84c3ae2015-05-29 17:43:57 +01002387 struct intel_engine_cs *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002388 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002389 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002390
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002391 ret = intel_ring_begin(ring, 4);
2392 if (ret)
2393 return ret;
2394
Chris Wilson71a77e02011-02-02 12:13:49 +00002395 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002396 if (INTEL_INFO(ring->dev)->gen >= 8)
2397 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002398
2399 /* We always require a command barrier so that subsequent
2400 * commands, such as breadcrumb interrupts, are strictly ordered
2401 * wrt the contents of the write cache being flushed to memory
2402 * (and thus being coherent from the CPU).
2403 */
2404 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2405
Jesse Barnes9a289772012-10-26 09:42:42 -07002406 /*
2407 * Bspec vol 1c.5 - video engine command streamer:
2408 * "If ENABLED, all TLBs will be invalidated once the flush
2409 * operation is complete. This bit is only valid when the
2410 * Post-Sync Operation field is a value of 1h or 3h."
2411 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002412 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002413 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2414
Chris Wilson71a77e02011-02-02 12:13:49 +00002415 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002416 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002417 if (INTEL_INFO(ring->dev)->gen >= 8) {
2418 intel_ring_emit(ring, 0); /* upper addr */
2419 intel_ring_emit(ring, 0); /* value */
2420 } else {
2421 intel_ring_emit(ring, 0);
2422 intel_ring_emit(ring, MI_NOOP);
2423 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002424 intel_ring_advance(ring);
2425 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002426}
2427
2428static int
John Harrison53fddaf2015-05-29 17:44:02 +01002429gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002430 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002431 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002432{
John Harrison53fddaf2015-05-29 17:44:02 +01002433 struct intel_engine_cs *ring = req->ring;
John Harrison8e004ef2015-02-13 11:48:10 +00002434 bool ppgtt = USES_PPGTT(ring->dev) &&
2435 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002436 int ret;
2437
2438 ret = intel_ring_begin(ring, 4);
2439 if (ret)
2440 return ret;
2441
2442 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07002443 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002444 intel_ring_emit(ring, lower_32_bits(offset));
2445 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002446 intel_ring_emit(ring, MI_NOOP);
2447 intel_ring_advance(ring);
2448
2449 return 0;
2450}
2451
2452static int
John Harrison53fddaf2015-05-29 17:44:02 +01002453hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00002454 u64 offset, u32 len,
2455 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002456{
John Harrison53fddaf2015-05-29 17:44:02 +01002457 struct intel_engine_cs *ring = req->ring;
Akshay Joshi0206e352011-08-16 15:34:10 -04002458 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002459
Akshay Joshi0206e352011-08-16 15:34:10 -04002460 ret = intel_ring_begin(ring, 2);
2461 if (ret)
2462 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002463
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002464 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002465 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002466 (dispatch_flags & I915_DISPATCH_SECURE ?
Chris Wilson77072252014-09-10 12:18:27 +01002467 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002468 /* bit0-7 is the length on GEN6+ */
2469 intel_ring_emit(ring, offset);
2470 intel_ring_advance(ring);
2471
2472 return 0;
2473}
2474
2475static int
John Harrison53fddaf2015-05-29 17:44:02 +01002476gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002477 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002478 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002479{
John Harrison53fddaf2015-05-29 17:44:02 +01002480 struct intel_engine_cs *ring = req->ring;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002481 int ret;
2482
2483 ret = intel_ring_begin(ring, 2);
2484 if (ret)
2485 return ret;
2486
2487 intel_ring_emit(ring,
2488 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002489 (dispatch_flags & I915_DISPATCH_SECURE ?
2490 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002491 /* bit0-7 is the length on GEN6+ */
2492 intel_ring_emit(ring, offset);
2493 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002494
Akshay Joshi0206e352011-08-16 15:34:10 -04002495 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002496}
2497
Chris Wilson549f7362010-10-19 11:19:32 +01002498/* Blitter support (SandyBridge+) */
2499
John Harrisona84c3ae2015-05-29 17:43:57 +01002500static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002501 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002502{
John Harrisona84c3ae2015-05-29 17:43:57 +01002503 struct intel_engine_cs *ring = req->ring;
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002504 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00002505 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002506 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002507
Daniel Vetter6a233c72011-12-14 13:57:07 +01002508 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002509 if (ret)
2510 return ret;
2511
Chris Wilson71a77e02011-02-02 12:13:49 +00002512 cmd = MI_FLUSH_DW;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002513 if (INTEL_INFO(dev)->gen >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002514 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002515
2516 /* We always require a command barrier so that subsequent
2517 * commands, such as breadcrumb interrupts, are strictly ordered
2518 * wrt the contents of the write cache being flushed to memory
2519 * (and thus being coherent from the CPU).
2520 */
2521 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2522
Jesse Barnes9a289772012-10-26 09:42:42 -07002523 /*
2524 * Bspec vol 1c.3 - blitter engine command streamer:
2525 * "If ENABLED, all TLBs will be invalidated once the flush
2526 * operation is complete. This bit is only valid when the
2527 * Post-Sync Operation field is a value of 1h or 3h."
2528 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002529 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002530 cmd |= MI_INVALIDATE_TLB;
Chris Wilson71a77e02011-02-02 12:13:49 +00002531 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002532 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002533 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002534 intel_ring_emit(ring, 0); /* upper addr */
2535 intel_ring_emit(ring, 0); /* value */
2536 } else {
2537 intel_ring_emit(ring, 0);
2538 intel_ring_emit(ring, MI_NOOP);
2539 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002540 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002541
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002542 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002543}
2544
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002545int intel_init_render_ring_buffer(struct drm_device *dev)
2546{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002547 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002548 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002549 struct drm_i915_gem_object *obj;
2550 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002551
Daniel Vetter59465b52012-04-11 22:12:48 +02002552 ring->name = "render ring";
2553 ring->id = RCS;
2554 ring->mmio_base = RENDER_RING_BASE;
2555
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002556 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002557 if (i915_semaphore_is_enabled(dev)) {
2558 obj = i915_gem_alloc_object(dev, 4096);
2559 if (obj == NULL) {
2560 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2561 i915.semaphores = 0;
2562 } else {
2563 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2564 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2565 if (ret != 0) {
2566 drm_gem_object_unreference(&obj->base);
2567 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2568 i915.semaphores = 0;
2569 } else
2570 dev_priv->semaphore_obj = obj;
2571 }
2572 }
Mika Kuoppala72253422014-10-07 17:21:26 +03002573
Daniel Vetter8f0e2b92014-12-02 16:19:07 +01002574 ring->init_context = intel_rcs_ctx_init;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002575 ring->add_request = gen6_add_request;
2576 ring->flush = gen8_render_ring_flush;
2577 ring->irq_get = gen8_ring_get_irq;
2578 ring->irq_put = gen8_ring_put_irq;
2579 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2580 ring->get_seqno = gen6_ring_get_seqno;
2581 ring->set_seqno = ring_set_seqno;
2582 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002583 WARN_ON(!dev_priv->semaphore_obj);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002584 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002585 ring->semaphore.signal = gen8_rcs_signal;
2586 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002587 }
2588 } else if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002589 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03002590 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01002591 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03002592 ring->flush = gen6_render_ring_flush;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002593 ring->irq_get = gen6_ring_get_irq;
2594 ring->irq_put = gen6_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002595 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01002596 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002597 ring->set_seqno = ring_set_seqno;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002598 if (i915_semaphore_is_enabled(dev)) {
2599 ring->semaphore.sync_to = gen6_ring_sync;
2600 ring->semaphore.signal = gen6_signal;
2601 /*
2602 * The current semaphore is only applied on pre-gen8
2603 * platform. And there is no VCS2 ring on the pre-gen8
2604 * platform. So the semaphore between RCS and VCS2 is
2605 * initialized as INVALID. Gen8 will initialize the
2606 * sema between VCS2 and RCS later.
2607 */
2608 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2609 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2610 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2611 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2612 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2613 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2614 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2615 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2616 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2617 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2618 }
Chris Wilsonc6df5412010-12-15 09:56:50 +00002619 } else if (IS_GEN5(dev)) {
2620 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002621 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00002622 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002623 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002624 ring->irq_get = gen5_ring_get_irq;
2625 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002626 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2627 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002628 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002629 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002630 if (INTEL_INFO(dev)->gen < 4)
2631 ring->flush = gen2_render_ring_flush;
2632 else
2633 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002634 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002635 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002636 if (IS_GEN2(dev)) {
2637 ring->irq_get = i8xx_ring_get_irq;
2638 ring->irq_put = i8xx_ring_put_irq;
2639 } else {
2640 ring->irq_get = i9xx_ring_get_irq;
2641 ring->irq_put = i9xx_ring_put_irq;
2642 }
Daniel Vettere3670312012-04-11 22:12:53 +02002643 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002644 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002645 ring->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002646
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002647 if (IS_HASWELL(dev))
2648 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002649 else if (IS_GEN8(dev))
2650 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002651 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002652 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2653 else if (INTEL_INFO(dev)->gen >= 4)
2654 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2655 else if (IS_I830(dev) || IS_845G(dev))
2656 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2657 else
2658 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002659 ring->init_hw = init_render_ring;
Daniel Vetter59465b52012-04-11 22:12:48 +02002660 ring->cleanup = render_ring_cleanup;
2661
Daniel Vetterb45305f2012-12-17 16:21:27 +01002662 /* Workaround batchbuffer to combat CS tlb bug. */
2663 if (HAS_BROKEN_CS_TLB(dev)) {
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002664 obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002665 if (obj == NULL) {
2666 DRM_ERROR("Failed to allocate batch bo\n");
2667 return -ENOMEM;
2668 }
2669
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002670 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002671 if (ret != 0) {
2672 drm_gem_object_unreference(&obj->base);
2673 DRM_ERROR("Failed to ping batch bo\n");
2674 return ret;
2675 }
2676
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002677 ring->scratch.obj = obj;
2678 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002679 }
2680
Daniel Vetter99be1df2014-11-20 00:33:06 +01002681 ret = intel_init_ring_buffer(dev, ring);
2682 if (ret)
2683 return ret;
2684
2685 if (INTEL_INFO(dev)->gen >= 5) {
2686 ret = intel_init_pipe_control(ring);
2687 if (ret)
2688 return ret;
2689 }
2690
2691 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002692}
2693
2694int intel_init_bsd_ring_buffer(struct drm_device *dev)
2695{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002696 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002697 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002698
Daniel Vetter58fa3832012-04-11 22:12:49 +02002699 ring->name = "bsd ring";
2700 ring->id = VCS;
2701
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002702 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002703 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002704 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002705 /* gen6 bsd needs a special wa for tail updates */
2706 if (IS_GEN6(dev))
2707 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002708 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002709 ring->add_request = gen6_add_request;
2710 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002711 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002712 if (INTEL_INFO(dev)->gen >= 8) {
2713 ring->irq_enable_mask =
2714 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2715 ring->irq_get = gen8_ring_get_irq;
2716 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002717 ring->dispatch_execbuffer =
2718 gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002719 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002720 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002721 ring->semaphore.signal = gen8_xcs_signal;
2722 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002723 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002724 } else {
2725 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2726 ring->irq_get = gen6_ring_get_irq;
2727 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002728 ring->dispatch_execbuffer =
2729 gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002730 if (i915_semaphore_is_enabled(dev)) {
2731 ring->semaphore.sync_to = gen6_ring_sync;
2732 ring->semaphore.signal = gen6_signal;
2733 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2734 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2735 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2736 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2737 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2738 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2739 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2740 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2741 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2742 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2743 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002744 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002745 } else {
2746 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002747 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002748 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002749 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002750 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002751 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002752 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002753 ring->irq_get = gen5_ring_get_irq;
2754 ring->irq_put = gen5_ring_put_irq;
2755 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002756 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002757 ring->irq_get = i9xx_ring_get_irq;
2758 ring->irq_put = i9xx_ring_put_irq;
2759 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002760 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002761 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002762 ring->init_hw = init_ring_common;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002763
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002764 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002765}
Chris Wilson549f7362010-10-19 11:19:32 +01002766
Zhao Yakui845f74a2014-04-17 10:37:37 +08002767/**
Damien Lespiau62659922015-01-29 14:13:40 +00002768 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002769 */
2770int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2771{
2772 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002773 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002774
Rodrigo Vivif7b64232014-07-01 02:41:36 -07002775 ring->name = "bsd2 ring";
Zhao Yakui845f74a2014-04-17 10:37:37 +08002776 ring->id = VCS2;
2777
2778 ring->write_tail = ring_write_tail;
2779 ring->mmio_base = GEN8_BSD2_RING_BASE;
2780 ring->flush = gen6_bsd_ring_flush;
2781 ring->add_request = gen6_add_request;
2782 ring->get_seqno = gen6_ring_get_seqno;
2783 ring->set_seqno = ring_set_seqno;
2784 ring->irq_enable_mask =
2785 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2786 ring->irq_get = gen8_ring_get_irq;
2787 ring->irq_put = gen8_ring_put_irq;
2788 ring->dispatch_execbuffer =
2789 gen8_ring_dispatch_execbuffer;
Ben Widawsky3e789982014-06-30 09:53:37 -07002790 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002791 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002792 ring->semaphore.signal = gen8_xcs_signal;
2793 GEN8_RING_SEMAPHORE_INIT;
2794 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002795 ring->init_hw = init_ring_common;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002796
2797 return intel_init_ring_buffer(dev, ring);
2798}
2799
Chris Wilson549f7362010-10-19 11:19:32 +01002800int intel_init_blt_ring_buffer(struct drm_device *dev)
2801{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002802 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002803 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002804
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002805 ring->name = "blitter ring";
2806 ring->id = BCS;
2807
2808 ring->mmio_base = BLT_RING_BASE;
2809 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002810 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002811 ring->add_request = gen6_add_request;
2812 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002813 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002814 if (INTEL_INFO(dev)->gen >= 8) {
2815 ring->irq_enable_mask =
2816 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2817 ring->irq_get = gen8_ring_get_irq;
2818 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002819 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002820 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002821 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002822 ring->semaphore.signal = gen8_xcs_signal;
2823 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002824 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002825 } else {
2826 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2827 ring->irq_get = gen6_ring_get_irq;
2828 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002829 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002830 if (i915_semaphore_is_enabled(dev)) {
2831 ring->semaphore.signal = gen6_signal;
2832 ring->semaphore.sync_to = gen6_ring_sync;
2833 /*
2834 * The current semaphore is only applied on pre-gen8
2835 * platform. And there is no VCS2 ring on the pre-gen8
2836 * platform. So the semaphore between BCS and VCS2 is
2837 * initialized as INVALID. Gen8 will initialize the
2838 * sema between BCS and VCS2 later.
2839 */
2840 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2841 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2842 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2843 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2844 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2845 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2846 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2847 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2848 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2849 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2850 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002851 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002852 ring->init_hw = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002853
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002854 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002855}
Chris Wilsona7b97612012-07-20 12:41:08 +01002856
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002857int intel_init_vebox_ring_buffer(struct drm_device *dev)
2858{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002859 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002860 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002861
2862 ring->name = "video enhancement ring";
2863 ring->id = VECS;
2864
2865 ring->mmio_base = VEBOX_RING_BASE;
2866 ring->write_tail = ring_write_tail;
2867 ring->flush = gen6_ring_flush;
2868 ring->add_request = gen6_add_request;
2869 ring->get_seqno = gen6_ring_get_seqno;
2870 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002871
2872 if (INTEL_INFO(dev)->gen >= 8) {
2873 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002874 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002875 ring->irq_get = gen8_ring_get_irq;
2876 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002877 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002878 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002879 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002880 ring->semaphore.signal = gen8_xcs_signal;
2881 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002882 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002883 } else {
2884 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2885 ring->irq_get = hsw_vebox_get_irq;
2886 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002887 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002888 if (i915_semaphore_is_enabled(dev)) {
2889 ring->semaphore.sync_to = gen6_ring_sync;
2890 ring->semaphore.signal = gen6_signal;
2891 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2892 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2893 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2894 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2895 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2896 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2897 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2898 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2899 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2900 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2901 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002902 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002903 ring->init_hw = init_ring_common;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002904
2905 return intel_init_ring_buffer(dev, ring);
2906}
2907
Chris Wilsona7b97612012-07-20 12:41:08 +01002908int
John Harrison4866d722015-05-29 17:43:55 +01002909intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002910{
John Harrison4866d722015-05-29 17:43:55 +01002911 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002912 int ret;
2913
2914 if (!ring->gpu_caches_dirty)
2915 return 0;
2916
John Harrisona84c3ae2015-05-29 17:43:57 +01002917 ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002918 if (ret)
2919 return ret;
2920
John Harrisona84c3ae2015-05-29 17:43:57 +01002921 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002922
2923 ring->gpu_caches_dirty = false;
2924 return 0;
2925}
2926
2927int
John Harrison2f200552015-05-29 17:43:53 +01002928intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002929{
John Harrison2f200552015-05-29 17:43:53 +01002930 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002931 uint32_t flush_domains;
2932 int ret;
2933
2934 flush_domains = 0;
2935 if (ring->gpu_caches_dirty)
2936 flush_domains = I915_GEM_GPU_DOMAINS;
2937
John Harrisona84c3ae2015-05-29 17:43:57 +01002938 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002939 if (ret)
2940 return ret;
2941
John Harrisona84c3ae2015-05-29 17:43:57 +01002942 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002943
2944 ring->gpu_caches_dirty = false;
2945 return 0;
2946}
Chris Wilsone3efda42014-04-09 09:19:41 +01002947
2948void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002949intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002950{
2951 int ret;
2952
2953 if (!intel_ring_initialized(ring))
2954 return;
2955
2956 ret = intel_ring_idle(ring);
2957 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2958 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2959 ring->name, ret);
2960
2961 stop_ring(ring);
2962}