blob: e0aa008f05550903164e0d1f4afb4280287621d3 [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
1291 *
1292 * @ring - ring that is adding a request
1293 * @seqno - return seqno stuck into the ring
1294 *
1295 * Update the mailbox registers in the *other* rings with the current seqno.
1296 * This acts like a signal in the canonical semaphore.
1297 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001298static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001299gen6_add_request(struct intel_engine_cs *ring)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001300{
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 Harrison6259cea2014-11-24 18:49:29 +00001313 intel_ring_emit(ring,
1314 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001315 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001316 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001317
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001318 return 0;
1319}
1320
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001321static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1322 u32 seqno)
1323{
1324 struct drm_i915_private *dev_priv = dev->dev_private;
1325 return dev_priv->last_seqno < seqno;
1326}
1327
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001328/**
1329 * intel_ring_sync - sync the waiter to the signaller on seqno
1330 *
1331 * @waiter - ring that is waiting
1332 * @signaller - ring which has, or will signal
1333 * @seqno - seqno which the waiter will block on
1334 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001335
1336static int
1337gen8_ring_sync(struct intel_engine_cs *waiter,
1338 struct intel_engine_cs *signaller,
1339 u32 seqno)
1340{
1341 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
1342 int ret;
1343
1344 ret = intel_ring_begin(waiter, 4);
1345 if (ret)
1346 return ret;
1347
1348 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1349 MI_SEMAPHORE_GLOBAL_GTT |
Ben Widawskybae4fcd2014-06-30 09:53:43 -07001350 MI_SEMAPHORE_POLL |
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001351 MI_SEMAPHORE_SAD_GTE_SDD);
1352 intel_ring_emit(waiter, seqno);
1353 intel_ring_emit(waiter,
1354 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1355 intel_ring_emit(waiter,
1356 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1357 intel_ring_advance(waiter);
1358 return 0;
1359}
1360
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001361static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001362gen6_ring_sync(struct intel_engine_cs *waiter,
1363 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001364 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001365{
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001366 u32 dw1 = MI_SEMAPHORE_MBOX |
1367 MI_SEMAPHORE_COMPARE |
1368 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001369 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1370 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001371
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001372 /* Throughout all of the GEM code, seqno passed implies our current
1373 * seqno is >= the last seqno executed. However for hardware the
1374 * comparison is strictly greater than.
1375 */
1376 seqno -= 1;
1377
Ben Widawskyebc348b2014-04-29 14:52:28 -07001378 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001379
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001380 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001381 if (ret)
1382 return ret;
1383
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001384 /* If seqno wrap happened, omit the wait with no-ops */
1385 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001386 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001387 intel_ring_emit(waiter, seqno);
1388 intel_ring_emit(waiter, 0);
1389 intel_ring_emit(waiter, MI_NOOP);
1390 } else {
1391 intel_ring_emit(waiter, MI_NOOP);
1392 intel_ring_emit(waiter, MI_NOOP);
1393 intel_ring_emit(waiter, MI_NOOP);
1394 intel_ring_emit(waiter, MI_NOOP);
1395 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001396 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001397
1398 return 0;
1399}
1400
Chris Wilsonc6df5412010-12-15 09:56:50 +00001401#define PIPE_CONTROL_FLUSH(ring__, addr__) \
1402do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001403 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1404 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +00001405 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1406 intel_ring_emit(ring__, 0); \
1407 intel_ring_emit(ring__, 0); \
1408} while (0)
1409
1410static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001411pc_render_add_request(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001412{
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 Harrison6259cea2014-11-24 18:49:29 +00001432 intel_ring_emit(ring,
1433 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001434 intel_ring_emit(ring, 0);
1435 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001436 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +00001437 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001438 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001439 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001440 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001441 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001442 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001443 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001444 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001445 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001446
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001447 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001448 PIPE_CONTROL_WRITE_FLUSH |
1449 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001450 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001451 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrison6259cea2014-11-24 18:49:29 +00001452 intel_ring_emit(ring,
1453 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001454 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +01001455 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001456
Chris Wilsonc6df5412010-12-15 09:56:50 +00001457 return 0;
1458}
1459
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001460static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001461gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001462{
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001463 /* Workaround to force correct ordering between irq and seqno writes on
1464 * ivb (and maybe also on snb) by reading from a CS register (like
1465 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +00001466 if (!lazy_coherency) {
1467 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1468 POSTING_READ(RING_ACTHD(ring->mmio_base));
1469 }
1470
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001471 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1472}
1473
1474static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001475ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001476{
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001477 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1478}
1479
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001480static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001481ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001482{
1483 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1484}
1485
Chris Wilsonc6df5412010-12-15 09:56:50 +00001486static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001487pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001488{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001489 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001490}
1491
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001492static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001493pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001494{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001495 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001496}
1497
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001498static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001499gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001500{
1501 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001502 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001503 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001504
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001505 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Daniel Vettere48d8632012-04-11 22:12:54 +02001506 return false;
1507
Chris Wilson7338aef2012-04-24 21:48:47 +01001508 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001509 if (ring->irq_refcount++ == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001510 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001511 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001512
1513 return true;
1514}
1515
1516static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001517gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001518{
1519 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001520 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001521 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001522
Chris Wilson7338aef2012-04-24 21:48:47 +01001523 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001524 if (--ring->irq_refcount == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001525 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001526 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001527}
1528
1529static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001530i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001531{
Chris Wilson78501ea2010-10-27 12:18:21 +01001532 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001533 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001534 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001535
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001536 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001537 return false;
1538
Chris Wilson7338aef2012-04-24 21:48:47 +01001539 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001540 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001541 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1542 I915_WRITE(IMR, dev_priv->irq_mask);
1543 POSTING_READ(IMR);
1544 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001545 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001546
1547 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001548}
1549
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001550static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001551i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001552{
Chris Wilson78501ea2010-10-27 12:18:21 +01001553 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001554 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001555 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001556
Chris Wilson7338aef2012-04-24 21:48:47 +01001557 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001558 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001559 dev_priv->irq_mask |= ring->irq_enable_mask;
1560 I915_WRITE(IMR, dev_priv->irq_mask);
1561 POSTING_READ(IMR);
1562 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001563 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001564}
1565
Chris Wilsonc2798b12012-04-22 21:13:57 +01001566static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001567i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001568{
1569 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001570 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001571 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001572
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001573 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonc2798b12012-04-22 21:13:57 +01001574 return false;
1575
Chris Wilson7338aef2012-04-24 21:48:47 +01001576 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001577 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001578 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1579 I915_WRITE16(IMR, dev_priv->irq_mask);
1580 POSTING_READ16(IMR);
1581 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001582 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001583
1584 return true;
1585}
1586
1587static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001588i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001589{
1590 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001591 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001592 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001593
Chris Wilson7338aef2012-04-24 21:48:47 +01001594 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001595 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001596 dev_priv->irq_mask |= ring->irq_enable_mask;
1597 I915_WRITE16(IMR, dev_priv->irq_mask);
1598 POSTING_READ16(IMR);
1599 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001600 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001601}
1602
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001603static int
John Harrisona84c3ae2015-05-29 17:43:57 +01001604bsd_ring_flush(struct drm_i915_gem_request *req,
Chris Wilson78501ea2010-10-27 12:18:21 +01001605 u32 invalidate_domains,
1606 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001607{
John Harrisona84c3ae2015-05-29 17:43:57 +01001608 struct intel_engine_cs *ring = req->ring;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001609 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001610
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001611 ret = intel_ring_begin(ring, 2);
1612 if (ret)
1613 return ret;
1614
1615 intel_ring_emit(ring, MI_FLUSH);
1616 intel_ring_emit(ring, MI_NOOP);
1617 intel_ring_advance(ring);
1618 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001619}
1620
Chris Wilson3cce4692010-10-27 16:11:02 +01001621static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001622i9xx_add_request(struct intel_engine_cs *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001623{
Chris Wilson3cce4692010-10-27 16:11:02 +01001624 int ret;
1625
1626 ret = intel_ring_begin(ring, 4);
1627 if (ret)
1628 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001629
Chris Wilson3cce4692010-10-27 16:11:02 +01001630 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1631 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrison6259cea2014-11-24 18:49:29 +00001632 intel_ring_emit(ring,
1633 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilson3cce4692010-10-27 16:11:02 +01001634 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001635 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001636
Chris Wilson3cce4692010-10-27 16:11:02 +01001637 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001638}
1639
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001640static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001641gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001642{
1643 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001644 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001645 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001646
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001647 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1648 return false;
Chris Wilson0f468322011-01-04 17:35:21 +00001649
Chris Wilson7338aef2012-04-24 21:48:47 +01001650 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001651 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001652 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001653 I915_WRITE_IMR(ring,
1654 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001655 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001656 else
1657 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001658 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001659 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001660 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001661
1662 return true;
1663}
1664
1665static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001666gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001667{
1668 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001669 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001670 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001671
Chris Wilson7338aef2012-04-24 21:48:47 +01001672 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001673 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001674 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001675 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001676 else
1677 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001678 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001679 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001680 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001681}
1682
Ben Widawskya19d2932013-05-28 19:22:30 -07001683static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001684hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001685{
1686 struct drm_device *dev = ring->dev;
1687 struct drm_i915_private *dev_priv = dev->dev_private;
1688 unsigned long flags;
1689
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001690 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskya19d2932013-05-28 19:22:30 -07001691 return false;
1692
Daniel Vetter59cdb632013-07-04 23:35:28 +02001693 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001694 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001695 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001696 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001697 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001698 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001699
1700 return true;
1701}
1702
1703static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001704hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001705{
1706 struct drm_device *dev = ring->dev;
1707 struct drm_i915_private *dev_priv = dev->dev_private;
1708 unsigned long flags;
1709
Daniel Vetter59cdb632013-07-04 23:35:28 +02001710 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001711 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001712 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001713 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001714 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001715 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001716}
1717
Ben Widawskyabd58f02013-11-02 21:07:09 -07001718static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001719gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001720{
1721 struct drm_device *dev = ring->dev;
1722 struct drm_i915_private *dev_priv = dev->dev_private;
1723 unsigned long flags;
1724
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001725 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskyabd58f02013-11-02 21:07:09 -07001726 return false;
1727
1728 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1729 if (ring->irq_refcount++ == 0) {
1730 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1731 I915_WRITE_IMR(ring,
1732 ~(ring->irq_enable_mask |
1733 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1734 } else {
1735 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1736 }
1737 POSTING_READ(RING_IMR(ring->mmio_base));
1738 }
1739 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1740
1741 return true;
1742}
1743
1744static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001745gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001746{
1747 struct drm_device *dev = ring->dev;
1748 struct drm_i915_private *dev_priv = dev->dev_private;
1749 unsigned long flags;
1750
1751 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1752 if (--ring->irq_refcount == 0) {
1753 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1754 I915_WRITE_IMR(ring,
1755 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1756 } else {
1757 I915_WRITE_IMR(ring, ~0);
1758 }
1759 POSTING_READ(RING_IMR(ring->mmio_base));
1760 }
1761 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1762}
1763
Zou Nan haid1b851f2010-05-21 09:08:57 +08001764static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001765i965_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001766 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001767 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001768{
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001769 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001770
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001771 ret = intel_ring_begin(ring, 2);
1772 if (ret)
1773 return ret;
1774
Chris Wilson78501ea2010-10-27 12:18:21 +01001775 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001776 MI_BATCH_BUFFER_START |
1777 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001778 (dispatch_flags & I915_DISPATCH_SECURE ?
1779 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001780 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001781 intel_ring_advance(ring);
1782
Zou Nan haid1b851f2010-05-21 09:08:57 +08001783 return 0;
1784}
1785
Daniel Vetterb45305f2012-12-17 16:21:27 +01001786/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1787#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001788#define I830_TLB_ENTRIES (2)
1789#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001790static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001791i830_dispatch_execbuffer(struct intel_engine_cs *ring,
John Harrison8e004ef2015-02-13 11:48:10 +00001792 u64 offset, u32 len,
1793 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001794{
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
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001853i915_dispatch_execbuffer(struct intel_engine_cs *ring,
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{
1857 int ret;
1858
1859 ret = intel_ring_begin(ring, 2);
1860 if (ret)
1861 return ret;
1862
Chris Wilson65f56872012-04-17 16:38:12 +01001863 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
John Harrison8e004ef2015-02-13 11:48:10 +00001864 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1865 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001866 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001867
Eric Anholt62fdfea2010-05-21 13:26:39 -07001868 return 0;
1869}
1870
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001871static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001872{
Chris Wilson05394f32010-11-08 19:18:58 +00001873 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001874
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001875 obj = ring->status_page.obj;
1876 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001877 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001878
Chris Wilson9da3da62012-06-01 15:20:22 +01001879 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001880 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001881 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001882 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001883}
1884
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001885static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001886{
Chris Wilson05394f32010-11-08 19:18:58 +00001887 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001888
Chris Wilsone3efda42014-04-09 09:19:41 +01001889 if ((obj = ring->status_page.obj) == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001890 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001891 int ret;
1892
1893 obj = i915_gem_alloc_object(ring->dev, 4096);
1894 if (obj == NULL) {
1895 DRM_ERROR("Failed to allocate status page\n");
1896 return -ENOMEM;
1897 }
1898
1899 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1900 if (ret)
1901 goto err_unref;
1902
Chris Wilson1f767e02014-07-03 17:33:03 -04001903 flags = 0;
1904 if (!HAS_LLC(ring->dev))
1905 /* On g33, we cannot place HWS above 256MiB, so
1906 * restrict its pinning to the low mappable arena.
1907 * Though this restriction is not documented for
1908 * gen4, gen5, or byt, they also behave similarly
1909 * and hang if the HWS is placed at the top of the
1910 * GTT. To generalise, it appears that all !llc
1911 * platforms have issues with us placing the HWS
1912 * above the mappable region (even though we never
1913 * actualy map it).
1914 */
1915 flags |= PIN_MAPPABLE;
1916 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001917 if (ret) {
1918err_unref:
1919 drm_gem_object_unreference(&obj->base);
1920 return ret;
1921 }
1922
1923 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001924 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001925
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001926 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001927 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001928 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001929
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001930 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1931 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001932
1933 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001934}
1935
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001936static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001937{
1938 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001939
1940 if (!dev_priv->status_page_dmah) {
1941 dev_priv->status_page_dmah =
1942 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1943 if (!dev_priv->status_page_dmah)
1944 return -ENOMEM;
1945 }
1946
Chris Wilson6b8294a2012-11-16 11:43:20 +00001947 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1948 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1949
1950 return 0;
1951}
1952
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001953void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1954{
1955 iounmap(ringbuf->virtual_start);
1956 ringbuf->virtual_start = NULL;
1957 i915_gem_object_ggtt_unpin(ringbuf->obj);
1958}
1959
1960int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
1961 struct intel_ringbuffer *ringbuf)
1962{
1963 struct drm_i915_private *dev_priv = to_i915(dev);
1964 struct drm_i915_gem_object *obj = ringbuf->obj;
1965 int ret;
1966
1967 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1968 if (ret)
1969 return ret;
1970
1971 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1972 if (ret) {
1973 i915_gem_object_ggtt_unpin(obj);
1974 return ret;
1975 }
1976
1977 ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
1978 i915_gem_obj_ggtt_offset(obj), ringbuf->size);
1979 if (ringbuf->virtual_start == NULL) {
1980 i915_gem_object_ggtt_unpin(obj);
1981 return -EINVAL;
1982 }
1983
1984 return 0;
1985}
1986
Oscar Mateo84c23772014-07-24 17:04:15 +01001987void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01001988{
Oscar Mateo2919d292014-07-03 16:28:02 +01001989 drm_gem_object_unreference(&ringbuf->obj->base);
1990 ringbuf->obj = NULL;
1991}
1992
Oscar Mateo84c23772014-07-24 17:04:15 +01001993int intel_alloc_ringbuffer_obj(struct drm_device *dev,
1994 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01001995{
Chris Wilsone3efda42014-04-09 09:19:41 +01001996 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01001997
1998 obj = NULL;
1999 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002000 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002001 if (obj == NULL)
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002002 obj = i915_gem_alloc_object(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01002003 if (obj == NULL)
2004 return -ENOMEM;
2005
Akash Goel24f3a8c2014-06-17 10:59:42 +05302006 /* mark ring buffers as read-only from GPU side by default */
2007 obj->gt_ro = 1;
2008
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002009 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01002010
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002011 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01002012}
2013
Ben Widawskyc43b5632012-04-16 14:07:40 -07002014static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002015 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002016{
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002017 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01002018 int ret;
2019
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002020 WARN_ON(ring->buffer);
2021
2022 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2023 if (!ringbuf)
2024 return -ENOMEM;
2025 ring->buffer = ringbuf;
Oscar Mateo8ee14972014-05-22 14:13:34 +01002026
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002027 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01002028 INIT_LIST_HEAD(&ring->active_list);
2029 INIT_LIST_HEAD(&ring->request_list);
Oscar Mateocc9130b2014-07-24 17:04:42 +01002030 INIT_LIST_HEAD(&ring->execlist_queue);
Chris Wilson06fbca72015-04-07 16:20:36 +01002031 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002032 ringbuf->size = 32 * PAGE_SIZE;
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002033 ringbuf->ring = ring;
Ben Widawskyebc348b2014-04-29 14:52:28 -07002034 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00002035
Chris Wilsonb259f672011-03-29 13:19:09 +01002036 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07002037
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002038 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01002039 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002040 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002041 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00002042 } else {
2043 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02002044 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00002045 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002046 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002047 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002048
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002049 WARN_ON(ringbuf->obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002050
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002051 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2052 if (ret) {
2053 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
2054 ring->name, ret);
2055 goto error;
2056 }
2057
2058 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2059 if (ret) {
2060 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2061 ring->name, ret);
2062 intel_destroy_ringbuffer_obj(ringbuf);
2063 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002064 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002065
Chris Wilson55249ba2010-12-22 14:04:47 +00002066 /* Workaround an erratum on the i830 which causes a hang if
2067 * the TAIL pointer points to within the last 2 cachelines
2068 * of the buffer.
2069 */
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002070 ringbuf->effective_size = ringbuf->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01002071 if (IS_I830(dev) || IS_845G(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002072 ringbuf->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00002073
Brad Volkin44e895a2014-05-10 14:10:43 -07002074 ret = i915_cmd_parser_init_ring(ring);
2075 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002076 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002077
Oscar Mateo8ee14972014-05-22 14:13:34 +01002078 return 0;
2079
2080error:
2081 kfree(ringbuf);
2082 ring->buffer = NULL;
2083 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002084}
2085
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002086void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002087{
John Harrison6402c332014-10-31 12:00:26 +00002088 struct drm_i915_private *dev_priv;
2089 struct intel_ringbuffer *ringbuf;
Chris Wilson33626e62010-10-29 16:18:36 +01002090
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002091 if (!intel_ring_initialized(ring))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002092 return;
2093
John Harrison6402c332014-10-31 12:00:26 +00002094 dev_priv = to_i915(ring->dev);
2095 ringbuf = ring->buffer;
2096
Chris Wilsone3efda42014-04-09 09:19:41 +01002097 intel_stop_ring_buffer(ring);
Ville Syrjäläde8f0a52014-05-28 19:12:13 +03002098 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002099
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002100 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateo2919d292014-07-03 16:28:02 +01002101 intel_destroy_ringbuffer_obj(ringbuf);
John Harrison6259cea2014-11-24 18:49:29 +00002102 i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
Chris Wilson78501ea2010-10-27 12:18:21 +01002103
Zou Nan hai8d192152010-11-02 16:31:01 +08002104 if (ring->cleanup)
2105 ring->cleanup(ring);
2106
Chris Wilson78501ea2010-10-27 12:18:21 +01002107 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07002108
2109 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01002110 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002111
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002112 kfree(ringbuf);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002113 ring->buffer = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002114}
2115
Chris Wilson595e1ee2015-04-07 16:20:51 +01002116static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002117{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002118 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002119 struct drm_i915_gem_request *request;
Chris Wilsonb4716182015-04-27 13:41:17 +01002120 unsigned space;
2121 int ret;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002122
John Harrison29b1b412015-06-18 13:10:09 +01002123 /* The whole point of reserving space is to not wait! */
2124 WARN_ON(ringbuf->reserved_in_use);
2125
Dave Gordonebd0fd42014-11-27 11:22:49 +00002126 if (intel_ring_space(ringbuf) >= n)
2127 return 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002128
2129 list_for_each_entry(request, &ring->request_list, list) {
Chris Wilsonb4716182015-04-27 13:41:17 +01002130 space = __intel_ring_space(request->postfix, ringbuf->tail,
2131 ringbuf->size);
2132 if (space >= n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002133 break;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002134 }
2135
Chris Wilson595e1ee2015-04-07 16:20:51 +01002136 if (WARN_ON(&request->list == &ring->request_list))
Chris Wilsona71d8d92012-02-15 11:25:36 +00002137 return -ENOSPC;
2138
Daniel Vettera4b3a572014-11-26 14:17:05 +01002139 ret = i915_wait_request(request);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002140 if (ret)
2141 return ret;
2142
Chris Wilsonb4716182015-04-27 13:41:17 +01002143 ringbuf->space = space;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002144 return 0;
2145}
2146
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002147static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002148{
2149 uint32_t __iomem *virt;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002150 struct intel_ringbuffer *ringbuf = ring->buffer;
2151 int rem = ringbuf->size - ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002152
John Harrison29b1b412015-06-18 13:10:09 +01002153 /* Can't wrap if space has already been reserved! */
2154 WARN_ON(ringbuf->reserved_in_use);
2155
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002156 if (ringbuf->space < rem) {
Chris Wilson3e960502012-11-27 16:22:54 +00002157 int ret = ring_wait_for_space(ring, rem);
2158 if (ret)
2159 return ret;
2160 }
2161
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002162 virt = ringbuf->virtual_start + ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002163 rem /= 4;
2164 while (rem--)
2165 iowrite32(MI_NOOP, virt++);
2166
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002167 ringbuf->tail = 0;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002168 intel_ring_update_space(ringbuf);
Chris Wilson3e960502012-11-27 16:22:54 +00002169
2170 return 0;
2171}
2172
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002173int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002174{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002175 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002176
2177 /* We need to add any requests required to flush the objects and ring */
John Harrison75289872015-05-29 17:43:49 +01002178 WARN_ON(ring->outstanding_lazy_request);
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002179 if (ring->outstanding_lazy_request)
John Harrison75289872015-05-29 17:43:49 +01002180 i915_add_request(ring->outstanding_lazy_request);
Chris Wilson3e960502012-11-27 16:22:54 +00002181
2182 /* Wait upon the last request to be completed */
2183 if (list_empty(&ring->request_list))
2184 return 0;
2185
Daniel Vettera4b3a572014-11-26 14:17:05 +01002186 req = list_entry(ring->request_list.prev,
Chris Wilsonb4716182015-04-27 13:41:17 +01002187 struct drm_i915_gem_request,
2188 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002189
Chris Wilsonb4716182015-04-27 13:41:17 +01002190 /* Make sure we do not trigger any retires */
2191 return __i915_wait_request(req,
2192 atomic_read(&to_i915(ring->dev)->gpu_error.reset_counter),
2193 to_i915(ring->dev)->mm.interruptible,
2194 NULL, NULL);
Chris Wilson3e960502012-11-27 16:22:54 +00002195}
2196
John Harrison6689cb22015-03-19 12:30:08 +00002197int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002198{
John Harrison6689cb22015-03-19 12:30:08 +00002199 request->ringbuf = request->ring->buffer;
John Harrison9eba5d42014-11-24 18:49:23 +00002200 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002201}
2202
John Harrison29b1b412015-06-18 13:10:09 +01002203void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size)
2204{
2205 /* NB: Until request management is fully tidied up and the OLR is
2206 * removed, there are too many ways for get false hits on this
2207 * anti-recursion check! */
2208 /*WARN_ON(ringbuf->reserved_size);*/
2209 WARN_ON(ringbuf->reserved_in_use);
2210
2211 ringbuf->reserved_size = size;
2212
2213 /*
2214 * Really need to call _begin() here but that currently leads to
2215 * recursion problems! This will be fixed later but for now just
2216 * return and hope for the best. Note that there is only a real
2217 * problem if the create of the request never actually calls _begin()
2218 * but if they are not submitting any work then why did they create
2219 * the request in the first place?
2220 */
2221}
2222
2223void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf)
2224{
2225 WARN_ON(ringbuf->reserved_in_use);
2226
2227 ringbuf->reserved_size = 0;
2228 ringbuf->reserved_in_use = false;
2229}
2230
2231void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf)
2232{
2233 WARN_ON(ringbuf->reserved_in_use);
2234
2235 ringbuf->reserved_in_use = true;
2236 ringbuf->reserved_tail = ringbuf->tail;
2237}
2238
2239void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf)
2240{
2241 WARN_ON(!ringbuf->reserved_in_use);
2242 WARN(ringbuf->tail > ringbuf->reserved_tail + ringbuf->reserved_size,
2243 "request reserved size too small: %d vs %d!\n",
2244 ringbuf->tail - ringbuf->reserved_tail, ringbuf->reserved_size);
2245
2246 ringbuf->reserved_size = 0;
2247 ringbuf->reserved_in_use = false;
2248}
2249
2250static int __intel_ring_prepare(struct intel_engine_cs *ring, int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002251{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002252 struct intel_ringbuffer *ringbuf = ring->buffer;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002253 int ret;
2254
John Harrison29b1b412015-06-18 13:10:09 +01002255 /*
2256 * Add on the reserved size to the request to make sure that after
2257 * the intended commands have been emitted, there is guaranteed to
2258 * still be enough free space to send them to the hardware.
2259 */
2260 if (!ringbuf->reserved_in_use)
2261 bytes += ringbuf->reserved_size;
2262
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002263 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002264 ret = intel_wrap_ring_buffer(ring);
2265 if (unlikely(ret))
2266 return ret;
John Harrison29b1b412015-06-18 13:10:09 +01002267
2268 if(ringbuf->reserved_size) {
2269 uint32_t size = ringbuf->reserved_size;
2270
2271 intel_ring_reserved_space_cancel(ringbuf);
2272 intel_ring_reserved_space_reserve(ringbuf, size);
2273 }
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002274 }
2275
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002276 if (unlikely(ringbuf->space < bytes)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002277 ret = ring_wait_for_space(ring, bytes);
2278 if (unlikely(ret))
2279 return ret;
2280 }
2281
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002282 return 0;
2283}
2284
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002285int intel_ring_begin(struct intel_engine_cs *ring,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002286 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002287{
John Harrison217e46b2015-05-29 17:43:29 +01002288 struct drm_i915_gem_request *req;
Jani Nikula4640c4f2014-03-31 14:27:19 +03002289 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002290 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01002291
Daniel Vetter33196de2012-11-14 17:14:05 +01002292 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2293 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02002294 if (ret)
2295 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00002296
Chris Wilson304d6952014-01-02 14:32:35 +00002297 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
2298 if (ret)
2299 return ret;
2300
Chris Wilson9d7730912012-11-27 16:22:52 +00002301 /* Preallocate the olr before touching the ring */
John Harrison217e46b2015-05-29 17:43:29 +01002302 ret = i915_gem_request_alloc(ring, ring->default_context, &req);
Chris Wilson9d7730912012-11-27 16:22:52 +00002303 if (ret)
2304 return ret;
2305
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002306 ring->buffer->space -= num_dwords * sizeof(uint32_t);
Chris Wilson304d6952014-01-02 14:32:35 +00002307 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002308}
2309
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002310/* Align the ring tail to a cacheline boundary */
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002311int intel_ring_cacheline_align(struct intel_engine_cs *ring)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002312{
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002313 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002314 int ret;
2315
2316 if (num_dwords == 0)
2317 return 0;
2318
Chris Wilson18393f62014-04-09 09:19:40 +01002319 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002320 ret = intel_ring_begin(ring, num_dwords);
2321 if (ret)
2322 return ret;
2323
2324 while (num_dwords--)
2325 intel_ring_emit(ring, MI_NOOP);
2326
2327 intel_ring_advance(ring);
2328
2329 return 0;
2330}
2331
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002332void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002333{
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002334 struct drm_device *dev = ring->dev;
2335 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002336
John Harrison6259cea2014-11-24 18:49:29 +00002337 BUG_ON(ring->outstanding_lazy_request);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002338
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002339 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002340 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2341 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002342 if (HAS_VEBOX(dev))
Ben Widawsky50201502013-08-12 16:53:03 -07002343 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002344 }
Chris Wilson297b0c52010-10-22 17:02:41 +01002345
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002346 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03002347 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01002348}
2349
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002350static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002351 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002352{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002353 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002354
2355 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002356
Chris Wilson12f55812012-07-05 17:14:01 +01002357 /* Disable notification that the ring is IDLE. The GT
2358 * will then assume that it is busy and bring it out of rc6.
2359 */
2360 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2361 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2362
2363 /* Clear the context id. Here be magic! */
2364 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2365
2366 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04002367 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01002368 GEN6_BSD_SLEEP_INDICATOR) == 0,
2369 50))
2370 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002371
Chris Wilson12f55812012-07-05 17:14:01 +01002372 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04002373 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01002374 POSTING_READ(RING_TAIL(ring->mmio_base));
2375
2376 /* Let the ring send IDLE messages to the GT again,
2377 * and so let it sleep to conserve power when idle.
2378 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002379 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01002380 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002381}
2382
John Harrisona84c3ae2015-05-29 17:43:57 +01002383static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002384 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002385{
John Harrisona84c3ae2015-05-29 17:43:57 +01002386 struct intel_engine_cs *ring = req->ring;
Chris Wilson71a77e02011-02-02 12:13:49 +00002387 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002388 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002389
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002390 ret = intel_ring_begin(ring, 4);
2391 if (ret)
2392 return ret;
2393
Chris Wilson71a77e02011-02-02 12:13:49 +00002394 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002395 if (INTEL_INFO(ring->dev)->gen >= 8)
2396 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002397
2398 /* We always require a command barrier so that subsequent
2399 * commands, such as breadcrumb interrupts, are strictly ordered
2400 * wrt the contents of the write cache being flushed to memory
2401 * (and thus being coherent from the CPU).
2402 */
2403 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2404
Jesse Barnes9a289772012-10-26 09:42:42 -07002405 /*
2406 * Bspec vol 1c.5 - video engine command streamer:
2407 * "If ENABLED, all TLBs will be invalidated once the flush
2408 * operation is complete. This bit is only valid when the
2409 * Post-Sync Operation field is a value of 1h or 3h."
2410 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002411 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002412 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2413
Chris Wilson71a77e02011-02-02 12:13:49 +00002414 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002415 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002416 if (INTEL_INFO(ring->dev)->gen >= 8) {
2417 intel_ring_emit(ring, 0); /* upper addr */
2418 intel_ring_emit(ring, 0); /* value */
2419 } else {
2420 intel_ring_emit(ring, 0);
2421 intel_ring_emit(ring, MI_NOOP);
2422 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002423 intel_ring_advance(ring);
2424 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002425}
2426
2427static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002428gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002429 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002430 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002431{
John Harrison8e004ef2015-02-13 11:48:10 +00002432 bool ppgtt = USES_PPGTT(ring->dev) &&
2433 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002434 int ret;
2435
2436 ret = intel_ring_begin(ring, 4);
2437 if (ret)
2438 return ret;
2439
2440 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07002441 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002442 intel_ring_emit(ring, lower_32_bits(offset));
2443 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002444 intel_ring_emit(ring, MI_NOOP);
2445 intel_ring_advance(ring);
2446
2447 return 0;
2448}
2449
2450static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002451hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
John Harrison8e004ef2015-02-13 11:48:10 +00002452 u64 offset, u32 len,
2453 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002454{
Akshay Joshi0206e352011-08-16 15:34:10 -04002455 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002456
Akshay Joshi0206e352011-08-16 15:34:10 -04002457 ret = intel_ring_begin(ring, 2);
2458 if (ret)
2459 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002460
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002461 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002462 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002463 (dispatch_flags & I915_DISPATCH_SECURE ?
Chris Wilson77072252014-09-10 12:18:27 +01002464 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002465 /* bit0-7 is the length on GEN6+ */
2466 intel_ring_emit(ring, offset);
2467 intel_ring_advance(ring);
2468
2469 return 0;
2470}
2471
2472static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002473gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002474 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002475 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002476{
2477 int ret;
2478
2479 ret = intel_ring_begin(ring, 2);
2480 if (ret)
2481 return ret;
2482
2483 intel_ring_emit(ring,
2484 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002485 (dispatch_flags & I915_DISPATCH_SECURE ?
2486 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002487 /* bit0-7 is the length on GEN6+ */
2488 intel_ring_emit(ring, offset);
2489 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002490
Akshay Joshi0206e352011-08-16 15:34:10 -04002491 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002492}
2493
Chris Wilson549f7362010-10-19 11:19:32 +01002494/* Blitter support (SandyBridge+) */
2495
John Harrisona84c3ae2015-05-29 17:43:57 +01002496static int gen6_ring_flush(struct drm_i915_gem_request *req,
Ben Widawskyea251322013-05-28 19:22:21 -07002497 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002498{
John Harrisona84c3ae2015-05-29 17:43:57 +01002499 struct intel_engine_cs *ring = req->ring;
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002500 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00002501 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002502 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002503
Daniel Vetter6a233c72011-12-14 13:57:07 +01002504 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002505 if (ret)
2506 return ret;
2507
Chris Wilson71a77e02011-02-02 12:13:49 +00002508 cmd = MI_FLUSH_DW;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002509 if (INTEL_INFO(dev)->gen >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002510 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002511
2512 /* We always require a command barrier so that subsequent
2513 * commands, such as breadcrumb interrupts, are strictly ordered
2514 * wrt the contents of the write cache being flushed to memory
2515 * (and thus being coherent from the CPU).
2516 */
2517 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2518
Jesse Barnes9a289772012-10-26 09:42:42 -07002519 /*
2520 * Bspec vol 1c.3 - blitter engine command streamer:
2521 * "If ENABLED, all TLBs will be invalidated once the flush
2522 * operation is complete. This bit is only valid when the
2523 * Post-Sync Operation field is a value of 1h or 3h."
2524 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002525 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002526 cmd |= MI_INVALIDATE_TLB;
Chris Wilson71a77e02011-02-02 12:13:49 +00002527 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002528 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002529 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002530 intel_ring_emit(ring, 0); /* upper addr */
2531 intel_ring_emit(ring, 0); /* value */
2532 } else {
2533 intel_ring_emit(ring, 0);
2534 intel_ring_emit(ring, MI_NOOP);
2535 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002536 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002537
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002538 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002539}
2540
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002541int intel_init_render_ring_buffer(struct drm_device *dev)
2542{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002543 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002544 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002545 struct drm_i915_gem_object *obj;
2546 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002547
Daniel Vetter59465b52012-04-11 22:12:48 +02002548 ring->name = "render ring";
2549 ring->id = RCS;
2550 ring->mmio_base = RENDER_RING_BASE;
2551
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002552 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002553 if (i915_semaphore_is_enabled(dev)) {
2554 obj = i915_gem_alloc_object(dev, 4096);
2555 if (obj == NULL) {
2556 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2557 i915.semaphores = 0;
2558 } else {
2559 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2560 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2561 if (ret != 0) {
2562 drm_gem_object_unreference(&obj->base);
2563 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2564 i915.semaphores = 0;
2565 } else
2566 dev_priv->semaphore_obj = obj;
2567 }
2568 }
Mika Kuoppala72253422014-10-07 17:21:26 +03002569
Daniel Vetter8f0e2b92014-12-02 16:19:07 +01002570 ring->init_context = intel_rcs_ctx_init;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002571 ring->add_request = gen6_add_request;
2572 ring->flush = gen8_render_ring_flush;
2573 ring->irq_get = gen8_ring_get_irq;
2574 ring->irq_put = gen8_ring_put_irq;
2575 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2576 ring->get_seqno = gen6_ring_get_seqno;
2577 ring->set_seqno = ring_set_seqno;
2578 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002579 WARN_ON(!dev_priv->semaphore_obj);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002580 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002581 ring->semaphore.signal = gen8_rcs_signal;
2582 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002583 }
2584 } else if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002585 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03002586 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01002587 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03002588 ring->flush = gen6_render_ring_flush;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002589 ring->irq_get = gen6_ring_get_irq;
2590 ring->irq_put = gen6_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002591 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01002592 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002593 ring->set_seqno = ring_set_seqno;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002594 if (i915_semaphore_is_enabled(dev)) {
2595 ring->semaphore.sync_to = gen6_ring_sync;
2596 ring->semaphore.signal = gen6_signal;
2597 /*
2598 * The current semaphore is only applied on pre-gen8
2599 * platform. And there is no VCS2 ring on the pre-gen8
2600 * platform. So the semaphore between RCS and VCS2 is
2601 * initialized as INVALID. Gen8 will initialize the
2602 * sema between VCS2 and RCS later.
2603 */
2604 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2605 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2606 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2607 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2608 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2609 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2610 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2611 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2612 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2613 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2614 }
Chris Wilsonc6df5412010-12-15 09:56:50 +00002615 } else if (IS_GEN5(dev)) {
2616 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002617 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00002618 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002619 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002620 ring->irq_get = gen5_ring_get_irq;
2621 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002622 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2623 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002624 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002625 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002626 if (INTEL_INFO(dev)->gen < 4)
2627 ring->flush = gen2_render_ring_flush;
2628 else
2629 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002630 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002631 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002632 if (IS_GEN2(dev)) {
2633 ring->irq_get = i8xx_ring_get_irq;
2634 ring->irq_put = i8xx_ring_put_irq;
2635 } else {
2636 ring->irq_get = i9xx_ring_get_irq;
2637 ring->irq_put = i9xx_ring_put_irq;
2638 }
Daniel Vettere3670312012-04-11 22:12:53 +02002639 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002640 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002641 ring->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002642
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002643 if (IS_HASWELL(dev))
2644 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002645 else if (IS_GEN8(dev))
2646 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002647 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002648 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2649 else if (INTEL_INFO(dev)->gen >= 4)
2650 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2651 else if (IS_I830(dev) || IS_845G(dev))
2652 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2653 else
2654 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002655 ring->init_hw = init_render_ring;
Daniel Vetter59465b52012-04-11 22:12:48 +02002656 ring->cleanup = render_ring_cleanup;
2657
Daniel Vetterb45305f2012-12-17 16:21:27 +01002658 /* Workaround batchbuffer to combat CS tlb bug. */
2659 if (HAS_BROKEN_CS_TLB(dev)) {
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002660 obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002661 if (obj == NULL) {
2662 DRM_ERROR("Failed to allocate batch bo\n");
2663 return -ENOMEM;
2664 }
2665
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002666 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002667 if (ret != 0) {
2668 drm_gem_object_unreference(&obj->base);
2669 DRM_ERROR("Failed to ping batch bo\n");
2670 return ret;
2671 }
2672
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002673 ring->scratch.obj = obj;
2674 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002675 }
2676
Daniel Vetter99be1df2014-11-20 00:33:06 +01002677 ret = intel_init_ring_buffer(dev, ring);
2678 if (ret)
2679 return ret;
2680
2681 if (INTEL_INFO(dev)->gen >= 5) {
2682 ret = intel_init_pipe_control(ring);
2683 if (ret)
2684 return ret;
2685 }
2686
2687 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002688}
2689
2690int intel_init_bsd_ring_buffer(struct drm_device *dev)
2691{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002692 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002693 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002694
Daniel Vetter58fa3832012-04-11 22:12:49 +02002695 ring->name = "bsd ring";
2696 ring->id = VCS;
2697
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002698 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002699 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002700 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002701 /* gen6 bsd needs a special wa for tail updates */
2702 if (IS_GEN6(dev))
2703 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002704 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002705 ring->add_request = gen6_add_request;
2706 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002707 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002708 if (INTEL_INFO(dev)->gen >= 8) {
2709 ring->irq_enable_mask =
2710 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2711 ring->irq_get = gen8_ring_get_irq;
2712 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002713 ring->dispatch_execbuffer =
2714 gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002715 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002716 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002717 ring->semaphore.signal = gen8_xcs_signal;
2718 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002719 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002720 } else {
2721 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2722 ring->irq_get = gen6_ring_get_irq;
2723 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002724 ring->dispatch_execbuffer =
2725 gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002726 if (i915_semaphore_is_enabled(dev)) {
2727 ring->semaphore.sync_to = gen6_ring_sync;
2728 ring->semaphore.signal = gen6_signal;
2729 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2730 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2731 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2732 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2733 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2734 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2735 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2736 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2737 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2738 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2739 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002740 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002741 } else {
2742 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002743 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002744 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002745 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002746 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002747 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002748 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002749 ring->irq_get = gen5_ring_get_irq;
2750 ring->irq_put = gen5_ring_put_irq;
2751 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002752 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002753 ring->irq_get = i9xx_ring_get_irq;
2754 ring->irq_put = i9xx_ring_put_irq;
2755 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002756 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002757 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002758 ring->init_hw = init_ring_common;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002759
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002760 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002761}
Chris Wilson549f7362010-10-19 11:19:32 +01002762
Zhao Yakui845f74a2014-04-17 10:37:37 +08002763/**
Damien Lespiau62659922015-01-29 14:13:40 +00002764 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002765 */
2766int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2767{
2768 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002769 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002770
Rodrigo Vivif7b64232014-07-01 02:41:36 -07002771 ring->name = "bsd2 ring";
Zhao Yakui845f74a2014-04-17 10:37:37 +08002772 ring->id = VCS2;
2773
2774 ring->write_tail = ring_write_tail;
2775 ring->mmio_base = GEN8_BSD2_RING_BASE;
2776 ring->flush = gen6_bsd_ring_flush;
2777 ring->add_request = gen6_add_request;
2778 ring->get_seqno = gen6_ring_get_seqno;
2779 ring->set_seqno = ring_set_seqno;
2780 ring->irq_enable_mask =
2781 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2782 ring->irq_get = gen8_ring_get_irq;
2783 ring->irq_put = gen8_ring_put_irq;
2784 ring->dispatch_execbuffer =
2785 gen8_ring_dispatch_execbuffer;
Ben Widawsky3e789982014-06-30 09:53:37 -07002786 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002787 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002788 ring->semaphore.signal = gen8_xcs_signal;
2789 GEN8_RING_SEMAPHORE_INIT;
2790 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002791 ring->init_hw = init_ring_common;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002792
2793 return intel_init_ring_buffer(dev, ring);
2794}
2795
Chris Wilson549f7362010-10-19 11:19:32 +01002796int intel_init_blt_ring_buffer(struct drm_device *dev)
2797{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002798 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002799 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002800
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002801 ring->name = "blitter ring";
2802 ring->id = BCS;
2803
2804 ring->mmio_base = BLT_RING_BASE;
2805 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002806 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002807 ring->add_request = gen6_add_request;
2808 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002809 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002810 if (INTEL_INFO(dev)->gen >= 8) {
2811 ring->irq_enable_mask =
2812 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2813 ring->irq_get = gen8_ring_get_irq;
2814 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002815 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002816 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002817 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002818 ring->semaphore.signal = gen8_xcs_signal;
2819 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002820 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002821 } else {
2822 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2823 ring->irq_get = gen6_ring_get_irq;
2824 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002825 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002826 if (i915_semaphore_is_enabled(dev)) {
2827 ring->semaphore.signal = gen6_signal;
2828 ring->semaphore.sync_to = gen6_ring_sync;
2829 /*
2830 * The current semaphore is only applied on pre-gen8
2831 * platform. And there is no VCS2 ring on the pre-gen8
2832 * platform. So the semaphore between BCS and VCS2 is
2833 * initialized as INVALID. Gen8 will initialize the
2834 * sema between BCS and VCS2 later.
2835 */
2836 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2837 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2838 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2839 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2840 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2841 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2842 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2843 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2844 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2845 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2846 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002847 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002848 ring->init_hw = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002849
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002850 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002851}
Chris Wilsona7b97612012-07-20 12:41:08 +01002852
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002853int intel_init_vebox_ring_buffer(struct drm_device *dev)
2854{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002855 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002856 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002857
2858 ring->name = "video enhancement ring";
2859 ring->id = VECS;
2860
2861 ring->mmio_base = VEBOX_RING_BASE;
2862 ring->write_tail = ring_write_tail;
2863 ring->flush = gen6_ring_flush;
2864 ring->add_request = gen6_add_request;
2865 ring->get_seqno = gen6_ring_get_seqno;
2866 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002867
2868 if (INTEL_INFO(dev)->gen >= 8) {
2869 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002870 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002871 ring->irq_get = gen8_ring_get_irq;
2872 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002873 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002874 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002875 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002876 ring->semaphore.signal = gen8_xcs_signal;
2877 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002878 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002879 } else {
2880 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2881 ring->irq_get = hsw_vebox_get_irq;
2882 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002883 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002884 if (i915_semaphore_is_enabled(dev)) {
2885 ring->semaphore.sync_to = gen6_ring_sync;
2886 ring->semaphore.signal = gen6_signal;
2887 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2888 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2889 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2890 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2891 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2892 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2893 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2894 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2895 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2896 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2897 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002898 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002899 ring->init_hw = init_ring_common;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002900
2901 return intel_init_ring_buffer(dev, ring);
2902}
2903
Chris Wilsona7b97612012-07-20 12:41:08 +01002904int
John Harrison4866d722015-05-29 17:43:55 +01002905intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002906{
John Harrison4866d722015-05-29 17:43:55 +01002907 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002908 int ret;
2909
2910 if (!ring->gpu_caches_dirty)
2911 return 0;
2912
John Harrisona84c3ae2015-05-29 17:43:57 +01002913 ret = ring->flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002914 if (ret)
2915 return ret;
2916
John Harrisona84c3ae2015-05-29 17:43:57 +01002917 trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
Chris Wilsona7b97612012-07-20 12:41:08 +01002918
2919 ring->gpu_caches_dirty = false;
2920 return 0;
2921}
2922
2923int
John Harrison2f200552015-05-29 17:43:53 +01002924intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Chris Wilsona7b97612012-07-20 12:41:08 +01002925{
John Harrison2f200552015-05-29 17:43:53 +01002926 struct intel_engine_cs *ring = req->ring;
Chris Wilsona7b97612012-07-20 12:41:08 +01002927 uint32_t flush_domains;
2928 int ret;
2929
2930 flush_domains = 0;
2931 if (ring->gpu_caches_dirty)
2932 flush_domains = I915_GEM_GPU_DOMAINS;
2933
John Harrisona84c3ae2015-05-29 17:43:57 +01002934 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002935 if (ret)
2936 return ret;
2937
John Harrisona84c3ae2015-05-29 17:43:57 +01002938 trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Chris Wilsona7b97612012-07-20 12:41:08 +01002939
2940 ring->gpu_caches_dirty = false;
2941 return 0;
2942}
Chris Wilsone3efda42014-04-09 09:19:41 +01002943
2944void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002945intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002946{
2947 int ret;
2948
2949 if (!intel_ring_initialized(ring))
2950 return;
2951
2952 ret = intel_ring_idle(ring);
2953 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2954 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2955 ring->name, ret);
2956
2957 stop_ring(ring);
2958}