blob: e9858d2e92d0ef8b030e71fd41e064bf771c526e [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
Oscar Mateoa4872ba2014-05-22 14:13:33 +010084void __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
Oscar Mateoa4872ba2014-05-22 14:13:33 +010094gen2_render_ring_flush(struct intel_engine_cs *ring,
Chris Wilson46f0f8d2012-04-18 11:12:11 +010095 u32 invalidate_domains,
96 u32 flush_domains)
97{
98 u32 cmd;
99 int ret;
100
101 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +0200102 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100103 cmd |= MI_NO_WRITE_FLUSH;
104
105 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
106 cmd |= MI_READ_FLUSH;
107
108 ret = intel_ring_begin(ring, 2);
109 if (ret)
110 return ret;
111
112 intel_ring_emit(ring, cmd);
113 intel_ring_emit(ring, MI_NOOP);
114 intel_ring_advance(ring);
115
116 return 0;
117}
118
119static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100120gen4_render_ring_flush(struct intel_engine_cs *ring,
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100121 u32 invalidate_domains,
122 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700123{
Chris Wilson78501ea2010-10-27 12:18:21 +0100124 struct drm_device *dev = ring->dev;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100125 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000126 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100127
Chris Wilson36d527d2011-03-19 22:26:49 +0000128 /*
129 * read/write caches:
130 *
131 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
132 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
133 * also flushed at 2d versus 3d pipeline switches.
134 *
135 * read-only caches:
136 *
137 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
138 * MI_READ_FLUSH is set, and is always flushed on 965.
139 *
140 * I915_GEM_DOMAIN_COMMAND may not exist?
141 *
142 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
143 * invalidated when MI_EXE_FLUSH is set.
144 *
145 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
146 * invalidated with every MI_FLUSH.
147 *
148 * TLBs:
149 *
150 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
151 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
152 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
153 * are flushed at any MI_FLUSH.
154 */
155
156 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100157 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000158 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000159 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
160 cmd |= MI_EXE_FLUSH;
161
162 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
163 (IS_G4X(dev) || IS_GEN5(dev)))
164 cmd |= MI_INVALIDATE_ISP;
165
166 ret = intel_ring_begin(ring, 2);
167 if (ret)
168 return ret;
169
170 intel_ring_emit(ring, cmd);
171 intel_ring_emit(ring, MI_NOOP);
172 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000173
174 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800175}
176
Jesse Barnes8d315282011-10-16 10:23:31 +0200177/**
178 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
179 * implementing two workarounds on gen6. From section 1.4.7.1
180 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
181 *
182 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
183 * produced by non-pipelined state commands), software needs to first
184 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
185 * 0.
186 *
187 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
188 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
189 *
190 * And the workaround for these two requires this workaround first:
191 *
192 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
193 * BEFORE the pipe-control with a post-sync op and no write-cache
194 * flushes.
195 *
196 * And this last workaround is tricky because of the requirements on
197 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
198 * volume 2 part 1:
199 *
200 * "1 of the following must also be set:
201 * - Render Target Cache Flush Enable ([12] of DW1)
202 * - Depth Cache Flush Enable ([0] of DW1)
203 * - Stall at Pixel Scoreboard ([1] of DW1)
204 * - Depth Stall ([13] of DW1)
205 * - Post-Sync Operation ([13] of DW1)
206 * - Notify Enable ([8] of DW1)"
207 *
208 * The cache flushes require the workaround flush that triggered this
209 * one, so we can't use it. Depth stall would trigger the same.
210 * Post-sync nonzero is what triggered this second workaround, so we
211 * can't use that one either. Notify enable is IRQs, which aren't
212 * really our business. That leaves only stall at scoreboard.
213 */
214static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100215intel_emit_post_sync_nonzero_flush(struct intel_engine_cs *ring)
Jesse Barnes8d315282011-10-16 10:23:31 +0200216{
Chris Wilson18393f62014-04-09 09:19:40 +0100217 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200218 int ret;
219
220
221 ret = intel_ring_begin(ring, 6);
222 if (ret)
223 return ret;
224
225 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
226 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
227 PIPE_CONTROL_STALL_AT_SCOREBOARD);
228 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
229 intel_ring_emit(ring, 0); /* low dword */
230 intel_ring_emit(ring, 0); /* high dword */
231 intel_ring_emit(ring, MI_NOOP);
232 intel_ring_advance(ring);
233
234 ret = intel_ring_begin(ring, 6);
235 if (ret)
236 return ret;
237
238 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
239 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
240 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
241 intel_ring_emit(ring, 0);
242 intel_ring_emit(ring, 0);
243 intel_ring_emit(ring, MI_NOOP);
244 intel_ring_advance(ring);
245
246 return 0;
247}
248
249static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100250gen6_render_ring_flush(struct intel_engine_cs *ring,
Jesse Barnes8d315282011-10-16 10:23:31 +0200251 u32 invalidate_domains, u32 flush_domains)
252{
253 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100254 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Jesse Barnes8d315282011-10-16 10:23:31 +0200255 int ret;
256
Paulo Zanonib3111502012-08-17 18:35:42 -0300257 /* Force SNB workarounds for PIPE_CONTROL flushes */
258 ret = intel_emit_post_sync_nonzero_flush(ring);
259 if (ret)
260 return ret;
261
Jesse Barnes8d315282011-10-16 10:23:31 +0200262 /* Just flush everything. Experiments have shown that reducing the
263 * number of bits based on the write domains has little performance
264 * impact.
265 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100266 if (flush_domains) {
267 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
268 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
269 /*
270 * Ensure that any following seqno writes only happen
271 * when the render cache is indeed flushed.
272 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200273 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100274 }
275 if (invalidate_domains) {
276 flags |= PIPE_CONTROL_TLB_INVALIDATE;
277 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
278 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
279 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
280 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
281 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
282 /*
283 * TLB invalidate requires a post-sync write.
284 */
Jesse Barnes3ac78312012-10-25 12:15:47 -0700285 flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100286 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200287
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100288 ret = intel_ring_begin(ring, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200289 if (ret)
290 return ret;
291
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100292 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
Jesse Barnes8d315282011-10-16 10:23:31 +0200293 intel_ring_emit(ring, flags);
294 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100295 intel_ring_emit(ring, 0);
Jesse Barnes8d315282011-10-16 10:23:31 +0200296 intel_ring_advance(ring);
297
298 return 0;
299}
300
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100301static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100302gen7_render_ring_cs_stall_wa(struct intel_engine_cs *ring)
Paulo Zanonif3987632012-08-17 18:35:43 -0300303{
304 int ret;
305
306 ret = intel_ring_begin(ring, 4);
307 if (ret)
308 return ret;
309
310 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
311 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
312 PIPE_CONTROL_STALL_AT_SCOREBOARD);
313 intel_ring_emit(ring, 0);
314 intel_ring_emit(ring, 0);
315 intel_ring_advance(ring);
316
317 return 0;
318}
319
320static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100321gen7_render_ring_flush(struct intel_engine_cs *ring,
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300322 u32 invalidate_domains, u32 flush_domains)
323{
324 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100325 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300326 int ret;
327
Paulo Zanonif3987632012-08-17 18:35:43 -0300328 /*
329 * Ensure that any following seqno writes only happen when the render
330 * cache is indeed flushed.
331 *
332 * Workaround: 4th PIPE_CONTROL command (except the ones with only
333 * read-cache invalidate bits set) must have the CS_STALL bit set. We
334 * don't try to be clever and just set it unconditionally.
335 */
336 flags |= PIPE_CONTROL_CS_STALL;
337
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300338 /* Just flush everything. Experiments have shown that reducing the
339 * number of bits based on the write domains has little performance
340 * impact.
341 */
342 if (flush_domains) {
343 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
344 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300345 }
346 if (invalidate_domains) {
347 flags |= PIPE_CONTROL_TLB_INVALIDATE;
348 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
349 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
350 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
351 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
352 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
Chris Wilson148b83d2014-12-16 08:44:31 +0000353 flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300354 /*
355 * TLB invalidate requires a post-sync write.
356 */
357 flags |= PIPE_CONTROL_QW_WRITE;
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200358 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Paulo Zanonif3987632012-08-17 18:35:43 -0300359
Chris Wilsonadd284a2014-12-16 08:44:32 +0000360 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
361
Paulo Zanonif3987632012-08-17 18:35:43 -0300362 /* Workaround: we must issue a pipe_control with CS-stall bit
363 * set before a pipe_control command that has the state cache
364 * invalidate bit set. */
365 gen7_render_ring_cs_stall_wa(ring);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300366 }
367
368 ret = intel_ring_begin(ring, 4);
369 if (ret)
370 return ret;
371
372 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
373 intel_ring_emit(ring, flags);
Ville Syrjäläb9e1faa2013-02-14 21:53:51 +0200374 intel_ring_emit(ring, scratch_addr);
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300375 intel_ring_emit(ring, 0);
376 intel_ring_advance(ring);
377
378 return 0;
379}
380
Ben Widawskya5f3d682013-11-02 21:07:27 -0700381static int
Kenneth Graunke884ceac2014-06-28 02:04:20 +0300382gen8_emit_pipe_control(struct intel_engine_cs *ring,
383 u32 flags, u32 scratch_addr)
384{
385 int ret;
386
387 ret = intel_ring_begin(ring, 6);
388 if (ret)
389 return ret;
390
391 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
392 intel_ring_emit(ring, flags);
393 intel_ring_emit(ring, scratch_addr);
394 intel_ring_emit(ring, 0);
395 intel_ring_emit(ring, 0);
396 intel_ring_emit(ring, 0);
397 intel_ring_advance(ring);
398
399 return 0;
400}
401
402static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100403gen8_render_ring_flush(struct intel_engine_cs *ring,
Ben Widawskya5f3d682013-11-02 21:07:27 -0700404 u32 invalidate_domains, u32 flush_domains)
405{
406 u32 flags = 0;
Chris Wilson18393f62014-04-09 09:19:40 +0100407 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800408 int ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700409
410 flags |= PIPE_CONTROL_CS_STALL;
411
412 if (flush_domains) {
413 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
414 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
415 }
416 if (invalidate_domains) {
417 flags |= PIPE_CONTROL_TLB_INVALIDATE;
418 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
419 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
420 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
421 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
422 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
423 flags |= PIPE_CONTROL_QW_WRITE;
424 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Kenneth Graunke02c9f7e2014-01-27 14:20:16 -0800425
426 /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
427 ret = gen8_emit_pipe_control(ring,
428 PIPE_CONTROL_CS_STALL |
429 PIPE_CONTROL_STALL_AT_SCOREBOARD,
430 0);
431 if (ret)
432 return ret;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700433 }
434
Rodrigo Vivic5ad0112014-08-04 03:51:38 -0700435 ret = gen8_emit_pipe_control(ring, flags, scratch_addr);
436 if (ret)
437 return ret;
438
Rodrigo Vivic5ad0112014-08-04 03:51:38 -0700439 return 0;
Ben Widawskya5f3d682013-11-02 21:07:27 -0700440}
441
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100442static void ring_write_tail(struct intel_engine_cs *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100443 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800444{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300445 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100446 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800447}
448
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100449u64 intel_ring_get_active_head(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800450{
Jani Nikula4640c4f2014-03-31 14:27:19 +0300451 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson50877442014-03-21 12:41:53 +0000452 u64 acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800453
Chris Wilson50877442014-03-21 12:41:53 +0000454 if (INTEL_INFO(ring->dev)->gen >= 8)
455 acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
456 RING_ACTHD_UDW(ring->mmio_base));
457 else if (INTEL_INFO(ring->dev)->gen >= 4)
458 acthd = I915_READ(RING_ACTHD(ring->mmio_base));
459 else
460 acthd = I915_READ(ACTHD);
461
462 return acthd;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800463}
464
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100465static void ring_setup_phys_status_page(struct intel_engine_cs *ring)
Daniel Vetter035dc1e2013-07-03 12:56:54 +0200466{
467 struct drm_i915_private *dev_priv = ring->dev->dev_private;
468 u32 addr;
469
470 addr = dev_priv->status_page_dmah->busaddr;
471 if (INTEL_INFO(ring->dev)->gen >= 4)
472 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
473 I915_WRITE(HWS_PGA, addr);
474}
475
Damien Lespiauaf75f262015-02-10 19:32:17 +0000476static void intel_ring_setup_status_page(struct intel_engine_cs *ring)
477{
478 struct drm_device *dev = ring->dev;
479 struct drm_i915_private *dev_priv = ring->dev->dev_private;
480 u32 mmio = 0;
481
482 /* The ring status page addresses are no longer next to the rest of
483 * the ring registers as of gen7.
484 */
485 if (IS_GEN7(dev)) {
486 switch (ring->id) {
487 case RCS:
488 mmio = RENDER_HWS_PGA_GEN7;
489 break;
490 case BCS:
491 mmio = BLT_HWS_PGA_GEN7;
492 break;
493 /*
494 * VCS2 actually doesn't exist on Gen7. Only shut up
495 * gcc switch check warning
496 */
497 case VCS2:
498 case VCS:
499 mmio = BSD_HWS_PGA_GEN7;
500 break;
501 case VECS:
502 mmio = VEBOX_HWS_PGA_GEN7;
503 break;
504 }
505 } else if (IS_GEN6(ring->dev)) {
506 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
507 } else {
508 /* XXX: gen8 returns to sanity */
509 mmio = RING_HWS_PGA(ring->mmio_base);
510 }
511
512 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
513 POSTING_READ(mmio);
514
515 /*
516 * Flush the TLB for this page
517 *
518 * FIXME: These two bits have disappeared on gen8, so a question
519 * arises: do we still need this and if so how should we go about
520 * invalidating the TLB?
521 */
522 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
523 u32 reg = RING_INSTPM(ring->mmio_base);
524
525 /* ring should be idle before issuing a sync flush*/
526 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
527
528 I915_WRITE(reg,
529 _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
530 INSTPM_SYNC_FLUSH));
531 if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
532 1000))
533 DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
534 ring->name);
535 }
536}
537
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100538static bool stop_ring(struct intel_engine_cs *ring)
Chris Wilson9991ae72014-04-02 16:36:07 +0100539{
540 struct drm_i915_private *dev_priv = to_i915(ring->dev);
541
542 if (!IS_GEN2(ring->dev)) {
543 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
Daniel Vetter403bdd12014-08-07 16:05:39 +0200544 if (wait_for((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
545 DRM_ERROR("%s : timed out trying to stop ring\n", ring->name);
Chris Wilson9bec9b12014-08-11 09:21:35 +0100546 /* Sometimes we observe that the idle flag is not
547 * set even though the ring is empty. So double
548 * check before giving up.
549 */
550 if (I915_READ_HEAD(ring) != I915_READ_TAIL(ring))
551 return false;
Chris Wilson9991ae72014-04-02 16:36:07 +0100552 }
553 }
554
555 I915_WRITE_CTL(ring, 0);
556 I915_WRITE_HEAD(ring, 0);
557 ring->write_tail(ring, 0);
558
559 if (!IS_GEN2(ring->dev)) {
560 (void)I915_READ_CTL(ring);
561 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
562 }
563
564 return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
565}
566
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100567static int init_ring_common(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800568{
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200569 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +0300570 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100571 struct intel_ringbuffer *ringbuf = ring->buffer;
572 struct drm_i915_gem_object *obj = ringbuf->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200573 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800574
Mika Kuoppala59bad942015-01-16 11:34:40 +0200575 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200576
Chris Wilson9991ae72014-04-02 16:36:07 +0100577 if (!stop_ring(ring)) {
578 /* G45 ring initialization often fails to reset head to zero */
Chris Wilson6fd0d562010-12-05 20:42:33 +0000579 DRM_DEBUG_KMS("%s head not reset to zero "
580 "ctl %08x head %08x tail %08x start %08x\n",
581 ring->name,
582 I915_READ_CTL(ring),
583 I915_READ_HEAD(ring),
584 I915_READ_TAIL(ring),
585 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800586
Chris Wilson9991ae72014-04-02 16:36:07 +0100587 if (!stop_ring(ring)) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000588 DRM_ERROR("failed to set %s head to zero "
589 "ctl %08x head %08x tail %08x start %08x\n",
590 ring->name,
591 I915_READ_CTL(ring),
592 I915_READ_HEAD(ring),
593 I915_READ_TAIL(ring),
594 I915_READ_START(ring));
Chris Wilson9991ae72014-04-02 16:36:07 +0100595 ret = -EIO;
596 goto out;
Chris Wilson6fd0d562010-12-05 20:42:33 +0000597 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700598 }
599
Chris Wilson9991ae72014-04-02 16:36:07 +0100600 if (I915_NEED_GFX_HWS(dev))
601 intel_ring_setup_status_page(ring);
602 else
603 ring_setup_phys_status_page(ring);
604
Jiri Kosinaece4a172014-08-07 16:29:53 +0200605 /* Enforce ordering by reading HEAD register back */
606 I915_READ_HEAD(ring);
607
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200608 /* Initialize the ring. This must happen _after_ we've cleared the ring
609 * registers with the above sequence (the readback of the HEAD registers
610 * also enforces ordering), otherwise the hw might lose the new ring
611 * register values. */
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700612 I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
Chris Wilson95468892014-08-07 15:39:54 +0100613
614 /* WaClearRingBufHeadRegAtInit:ctg,elk */
615 if (I915_READ_HEAD(ring))
616 DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
617 ring->name, I915_READ_HEAD(ring));
618 I915_WRITE_HEAD(ring, 0);
619 (void)I915_READ_HEAD(ring);
620
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200621 I915_WRITE_CTL(ring,
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100622 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000623 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800624
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800625 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400626 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700627 I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
Sean Paulf01db982012-03-16 12:43:22 -0400628 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000629 DRM_ERROR("%s initialization failed "
Chris Wilson48e48a02014-04-09 09:19:44 +0100630 "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
631 ring->name,
632 I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
633 I915_READ_HEAD(ring), I915_READ_TAIL(ring),
634 I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200635 ret = -EIO;
636 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800637 }
638
Dave Gordonebd0fd42014-11-27 11:22:49 +0000639 ringbuf->last_retired_head = -1;
Chris Wilson5c6c6002014-09-06 10:28:27 +0100640 ringbuf->head = I915_READ_HEAD(ring);
641 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Dave Gordonebd0fd42014-11-27 11:22:49 +0000642 intel_ring_update_space(ringbuf);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000643
Chris Wilson50f018d2013-06-10 11:20:19 +0100644 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
645
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200646out:
Mika Kuoppala59bad942015-01-16 11:34:40 +0200647 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200648
649 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700650}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800651
Oscar Mateo9b1136d2014-07-24 17:04:24 +0100652void
653intel_fini_pipe_control(struct intel_engine_cs *ring)
654{
655 struct drm_device *dev = ring->dev;
656
657 if (ring->scratch.obj == NULL)
658 return;
659
660 if (INTEL_INFO(dev)->gen >= 5) {
661 kunmap(sg_page(ring->scratch.obj->pages->sgl));
662 i915_gem_object_ggtt_unpin(ring->scratch.obj);
663 }
664
665 drm_gem_object_unreference(&ring->scratch.obj->base);
666 ring->scratch.obj = NULL;
667}
668
669int
670intel_init_pipe_control(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000671{
Chris Wilsonc6df5412010-12-15 09:56:50 +0000672 int ret;
673
Daniel Vetterbfc882b2014-11-20 00:33:08 +0100674 WARN_ON(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000675
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100676 ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
677 if (ring->scratch.obj == NULL) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000678 DRM_ERROR("Failed to allocate seqno page\n");
679 ret = -ENOMEM;
680 goto err;
681 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100682
Daniel Vettera9cc7262014-02-14 14:01:13 +0100683 ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
684 if (ret)
685 goto err_unref;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000686
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100687 ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000688 if (ret)
689 goto err_unref;
690
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100691 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
692 ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
693 if (ring->scratch.cpu_page == NULL) {
Wei Yongjun56b085a2013-05-28 17:51:44 +0800694 ret = -ENOMEM;
Chris Wilsonc6df5412010-12-15 09:56:50 +0000695 goto err_unpin;
Wei Yongjun56b085a2013-05-28 17:51:44 +0800696 }
Chris Wilsonc6df5412010-12-15 09:56:50 +0000697
Ville Syrjälä2b1086c2013-02-12 22:01:38 +0200698 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100699 ring->name, ring->scratch.gtt_offset);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000700 return 0;
701
702err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800703 i915_gem_object_ggtt_unpin(ring->scratch.obj);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000704err_unref:
Chris Wilson0d1aaca2013-08-26 20:58:11 +0100705 drm_gem_object_unreference(&ring->scratch.obj->base);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000706err:
Chris Wilsonc6df5412010-12-15 09:56:50 +0000707 return ret;
708}
709
Michel Thierry771b9a52014-11-11 16:47:33 +0000710static int intel_ring_workarounds_emit(struct intel_engine_cs *ring,
711 struct intel_context *ctx)
Arun Siluvery86d7f232014-08-26 14:44:50 +0100712{
Mika Kuoppala72253422014-10-07 17:21:26 +0300713 int ret, i;
Arun Siluvery888b5992014-08-26 14:44:51 +0100714 struct drm_device *dev = ring->dev;
715 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala72253422014-10-07 17:21:26 +0300716 struct i915_workarounds *w = &dev_priv->workarounds;
Arun Siluvery888b5992014-08-26 14:44:51 +0100717
Michel Thierrye6c1abb2014-11-26 14:21:02 +0000718 if (WARN_ON_ONCE(w->count == 0))
Mika Kuoppala72253422014-10-07 17:21:26 +0300719 return 0;
Arun Siluvery888b5992014-08-26 14:44:51 +0100720
Mika Kuoppala72253422014-10-07 17:21:26 +0300721 ring->gpu_caches_dirty = true;
722 ret = intel_ring_flush_all_caches(ring);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100723 if (ret)
724 return ret;
725
Arun Siluvery22a916a2014-10-22 18:59:52 +0100726 ret = intel_ring_begin(ring, (w->count * 2 + 2));
Mika Kuoppala72253422014-10-07 17:21:26 +0300727 if (ret)
728 return ret;
729
Arun Siluvery22a916a2014-10-22 18:59:52 +0100730 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
Mika Kuoppala72253422014-10-07 17:21:26 +0300731 for (i = 0; i < w->count; i++) {
Mika Kuoppala72253422014-10-07 17:21:26 +0300732 intel_ring_emit(ring, w->reg[i].addr);
733 intel_ring_emit(ring, w->reg[i].value);
734 }
Arun Siluvery22a916a2014-10-22 18:59:52 +0100735 intel_ring_emit(ring, MI_NOOP);
Mika Kuoppala72253422014-10-07 17:21:26 +0300736
737 intel_ring_advance(ring);
738
739 ring->gpu_caches_dirty = true;
740 ret = intel_ring_flush_all_caches(ring);
741 if (ret)
742 return ret;
743
744 DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
745
746 return 0;
747}
748
Daniel Vetter8f0e2b92014-12-02 16:19:07 +0100749static int intel_rcs_ctx_init(struct intel_engine_cs *ring,
750 struct intel_context *ctx)
751{
752 int ret;
753
754 ret = intel_ring_workarounds_emit(ring, ctx);
755 if (ret != 0)
756 return ret;
757
758 ret = i915_gem_render_state_init(ring);
759 if (ret)
760 DRM_ERROR("init render state: %d\n", ret);
761
762 return ret;
763}
764
Mika Kuoppala72253422014-10-07 17:21:26 +0300765static int wa_add(struct drm_i915_private *dev_priv,
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000766 const u32 addr, const u32 mask, const u32 val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300767{
768 const u32 idx = dev_priv->workarounds.count;
769
770 if (WARN_ON(idx >= I915_MAX_WA_REGS))
771 return -ENOSPC;
772
773 dev_priv->workarounds.reg[idx].addr = addr;
774 dev_priv->workarounds.reg[idx].value = val;
775 dev_priv->workarounds.reg[idx].mask = mask;
776
777 dev_priv->workarounds.count++;
778
779 return 0;
780}
781
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000782#define WA_REG(addr, mask, val) { \
783 const int r = wa_add(dev_priv, (addr), (mask), (val)); \
Mika Kuoppala72253422014-10-07 17:21:26 +0300784 if (r) \
785 return r; \
786 }
787
788#define WA_SET_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000789 WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300790
791#define WA_CLR_BIT_MASKED(addr, mask) \
Damien Lespiau26459342014-12-08 17:35:38 +0000792 WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300793
Damien Lespiau98533252014-12-08 17:33:51 +0000794#define WA_SET_FIELD_MASKED(addr, mask, value) \
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000795 WA_REG(addr, mask, _MASKED_FIELD(mask, value))
Mika Kuoppala72253422014-10-07 17:21:26 +0300796
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000797#define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
798#define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
Mika Kuoppala72253422014-10-07 17:21:26 +0300799
Damien Lespiaucf4b0de2014-12-08 17:35:37 +0000800#define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
Mika Kuoppala72253422014-10-07 17:21:26 +0300801
802static int bdw_init_workarounds(struct intel_engine_cs *ring)
803{
804 struct drm_device *dev = ring->dev;
805 struct drm_i915_private *dev_priv = dev->dev_private;
806
Arun Siluvery86d7f232014-08-26 14:44:50 +0100807 /* WaDisablePartialInstShootdown:bdw */
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700808 /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
Mika Kuoppala72253422014-10-07 17:21:26 +0300809 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
810 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
811 STALL_DOP_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100812
Rodrigo Vivi101b3762014-10-09 07:11:47 -0700813 /* WaDisableDopClockGating:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300814 WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
815 DOP_CLOCK_GATING_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100816
Mika Kuoppala72253422014-10-07 17:21:26 +0300817 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
818 GEN8_SAMPLER_POWER_BYPASS_DIS);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100819
820 /* Use Force Non-Coherent whenever executing a 3D context. This is a
821 * workaround for for a possible hang in the unlikely event a TLB
822 * invalidation occurs during a PSD flush.
823 */
Mika Kuoppala72253422014-10-07 17:21:26 +0300824 WA_SET_BIT_MASKED(HDC_CHICKEN0,
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000825 /* WaForceEnableNonCoherent:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300826 HDC_FORCE_NON_COHERENT |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000827 /* WaForceContextSaveRestoreNonCoherent:bdw */
828 HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
829 /* WaHdcDisableFetchWhenMasked:bdw */
Michel Thierryf3f32362014-12-04 15:07:52 +0000830 HDC_DONOT_FETCH_MEM_WHEN_MASKED |
Damien Lespiau35cb6f32015-02-10 10:31:00 +0000831 /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
Mika Kuoppala72253422014-10-07 17:21:26 +0300832 (IS_BDW_GT3(dev) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
Arun Siluvery86d7f232014-08-26 14:44:50 +0100833
Kenneth Graunke2701fc42015-01-13 12:46:52 -0800834 /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
835 * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
836 * polygons in the same 8x4 pixel/sample area to be processed without
837 * stalling waiting for the earlier ones to write to Hierarchical Z
838 * buffer."
839 *
840 * This optimization is off by default for Broadwell; turn it on.
841 */
842 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
843
Arun Siluvery86d7f232014-08-26 14:44:50 +0100844 /* Wa4x4STCOptimizationDisable:bdw */
Mika Kuoppala72253422014-10-07 17:21:26 +0300845 WA_SET_BIT_MASKED(CACHE_MODE_1,
846 GEN8_4x4_STC_OPTIMIZATION_DISABLE);
Arun Siluvery86d7f232014-08-26 14:44:50 +0100847
848 /*
849 * BSpec recommends 8x4 when MSAA is used,
850 * however in practice 16x4 seems fastest.
851 *
852 * Note that PS/WM thread counts depend on the WIZ hashing
853 * disable bit, which we don't touch here, but it's good
854 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
855 */
Damien Lespiau98533252014-12-08 17:33:51 +0000856 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
857 GEN6_WIZ_HASHING_MASK,
858 GEN6_WIZ_HASHING_16x4);
Arun Siluvery888b5992014-08-26 14:44:51 +0100859
Arun Siluvery86d7f232014-08-26 14:44:50 +0100860 return 0;
861}
862
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300863static int chv_init_workarounds(struct intel_engine_cs *ring)
864{
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300865 struct drm_device *dev = ring->dev;
866 struct drm_i915_private *dev_priv = dev->dev_private;
867
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300868 /* WaDisablePartialInstShootdown:chv */
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300869 /* WaDisableThreadStallDopClockGating:chv */
Mika Kuoppala72253422014-10-07 17:21:26 +0300870 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
Arun Siluvery605f1432014-10-28 18:33:13 +0000871 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE |
872 STALL_DOP_GATING_DISABLE);
Ville Syrjälä00e1e622014-08-27 17:33:12 +0300873
Arun Siluvery952890092014-10-28 18:33:14 +0000874 /* Use Force Non-Coherent whenever executing a 3D context. This is a
875 * workaround for a possible hang in the unlikely event a TLB
876 * invalidation occurs during a PSD flush.
877 */
878 /* WaForceEnableNonCoherent:chv */
879 /* WaHdcDisableFetchWhenMasked:chv */
880 WA_SET_BIT_MASKED(HDC_CHICKEN0,
881 HDC_FORCE_NON_COHERENT |
882 HDC_DONOT_FETCH_MEM_WHEN_MASKED);
883
Kenneth Graunke973a5b02015-01-13 12:46:53 -0800884 /* According to the CACHE_MODE_0 default value documentation, some
885 * CHV platforms disable this optimization by default. Turn it on.
886 */
887 WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
888
Ville Syrjälä14bc16e2015-01-21 19:37:58 +0200889 /* Wa4x4STCOptimizationDisable:chv */
890 WA_SET_BIT_MASKED(CACHE_MODE_1,
891 GEN8_4x4_STC_OPTIMIZATION_DISABLE);
892
Kenneth Graunked60de812015-01-10 18:02:22 -0800893 /* Improve HiZ throughput on CHV. */
894 WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
895
Ville Syrjäläe7fc2432015-01-21 19:38:00 +0200896 /*
897 * BSpec recommends 8x4 when MSAA is used,
898 * however in practice 16x4 seems fastest.
899 *
900 * Note that PS/WM thread counts depend on the WIZ hashing
901 * disable bit, which we don't touch here, but it's good
902 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
903 */
904 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
905 GEN6_WIZ_HASHING_MASK,
906 GEN6_WIZ_HASHING_16x4);
907
Damien Lespiau65ca7512015-02-09 19:33:22 +0000908 if (INTEL_REVID(dev) == SKL_REVID_C0 ||
909 INTEL_REVID(dev) == SKL_REVID_D0)
910 /* WaBarrierPerformanceFixDisable:skl */
911 WA_SET_BIT_MASKED(HDC_CHICKEN0,
912 HDC_FENCE_DEST_SLM_DISABLE |
913 HDC_BARRIER_PERFORMANCE_DISABLE);
914
Mika Kuoppala72253422014-10-07 17:21:26 +0300915 return 0;
916}
917
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000918static int gen9_init_workarounds(struct intel_engine_cs *ring)
919{
Hoath, Nicholasab0dfaf2015-02-05 10:47:18 +0000920 struct drm_device *dev = ring->dev;
921 struct drm_i915_private *dev_priv = dev->dev_private;
922
923 /* WaDisablePartialInstShootdown:skl */
924 WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
925 PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
926
Nick Hoath84241712015-02-05 10:47:20 +0000927 /* Syncing dependencies between camera and graphics */
928 WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
929 GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
930
Damien Lespiau35c8ce62015-02-11 18:21:43 +0000931 if (INTEL_REVID(dev) == SKL_REVID_A0 ||
932 INTEL_REVID(dev) == SKL_REVID_B0) {
Damien Lespiaua86eb582015-02-11 18:21:44 +0000933 /* WaDisableDgMirrorFixInHalfSliceChicken5:skl */
934 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
935 GEN9_DG_MIRROR_FIX_ENABLE);
Nick Hoath1de45822015-02-05 10:47:19 +0000936 }
937
Damien Lespiau183c6da2015-02-09 19:33:11 +0000938 if (IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0) {
939 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl */
940 WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
941 GEN9_RHWO_OPTIMIZATION_DISABLE);
942 WA_SET_BIT_MASKED(GEN9_SLICE_COMMON_ECO_CHICKEN0,
943 DISABLE_PIXEL_MASK_CAMMING);
944 }
945
Nick Hoathcac23df2015-02-05 10:47:22 +0000946 if (INTEL_REVID(dev) >= SKL_REVID_C0) {
947 /* WaEnableYV12BugFixInHalfSliceChicken7:skl */
948 WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
949 GEN9_ENABLE_YV12_BUGFIX);
950 }
951
Hoath, Nicholas13bea492015-02-05 10:47:24 +0000952 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
953 /*
954 *Use Force Non-Coherent whenever executing a 3D context. This
955 * is a workaround for a possible hang in the unlikely event
956 * a TLB invalidation occurs during a PSD flush.
957 */
958 /* WaForceEnableNonCoherent:skl */
959 WA_SET_BIT_MASKED(HDC_CHICKEN0,
960 HDC_FORCE_NON_COHERENT);
961 }
962
Hoath, Nicholas18404812015-02-05 10:47:23 +0000963 /* Wa4x4STCOptimizationDisable:skl */
964 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
965
Damien Lespiau9370cd92015-02-09 19:33:17 +0000966 /* WaDisablePartialResolveInVc:skl */
967 WA_SET_BIT_MASKED(CACHE_MODE_1, GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE);
968
Damien Lespiaue2db7072015-02-09 19:33:21 +0000969 /* WaCcsTlbPrefetchDisable:skl */
970 WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
971 GEN9_CCS_TLB_PREFETCH_ENABLE);
972
Hoath, Nicholas3b106532015-02-05 10:47:16 +0000973 return 0;
974}
975
Damien Lespiaub7668792015-02-14 18:30:29 +0000976static int skl_tune_iz_hashing(struct intel_engine_cs *ring)
Damien Lespiau8d205492015-02-09 19:33:15 +0000977{
Damien Lespiaub7668792015-02-14 18:30:29 +0000978 struct drm_device *dev = ring->dev;
979 struct drm_i915_private *dev_priv = dev->dev_private;
980 u8 vals[3] = { 0, 0, 0 };
981 unsigned int i;
982
983 for (i = 0; i < 3; i++) {
984 u8 ss;
985
986 /*
987 * Only consider slices where one, and only one, subslice has 7
988 * EUs
989 */
990 if (hweight8(dev_priv->info.subslice_7eu[i]) != 1)
991 continue;
992
993 /*
994 * subslice_7eu[i] != 0 (because of the check above) and
995 * ss_max == 4 (maximum number of subslices possible per slice)
996 *
997 * -> 0 <= ss <= 3;
998 */
999 ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
1000 vals[i] = 3 - ss;
1001 }
1002
1003 if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
1004 return 0;
1005
1006 /* Tune IZ hashing. See intel_device_info_runtime_init() */
1007 WA_SET_FIELD_MASKED(GEN7_GT_MODE,
1008 GEN9_IZ_HASHING_MASK(2) |
1009 GEN9_IZ_HASHING_MASK(1) |
1010 GEN9_IZ_HASHING_MASK(0),
1011 GEN9_IZ_HASHING(2, vals[2]) |
1012 GEN9_IZ_HASHING(1, vals[1]) |
1013 GEN9_IZ_HASHING(0, vals[0]));
Damien Lespiau8d205492015-02-09 19:33:15 +00001014
Mika Kuoppala72253422014-10-07 17:21:26 +03001015 return 0;
1016}
1017
Damien Lespiaub7668792015-02-14 18:30:29 +00001018
Damien Lespiau8d205492015-02-09 19:33:15 +00001019static int skl_init_workarounds(struct intel_engine_cs *ring)
1020{
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001021 struct drm_device *dev = ring->dev;
1022 struct drm_i915_private *dev_priv = dev->dev_private;
1023
Damien Lespiau8d205492015-02-09 19:33:15 +00001024 gen9_init_workarounds(ring);
1025
Damien Lespiaud0bbbc42015-02-09 19:33:16 +00001026 /* WaDisablePowerCompilerClockGating:skl */
1027 if (INTEL_REVID(dev) == SKL_REVID_B0)
1028 WA_SET_BIT_MASKED(HIZ_CHICKEN,
1029 BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
1030
Damien Lespiaub7668792015-02-14 18:30:29 +00001031 return skl_tune_iz_hashing(ring);
Damien Lespiau8d205492015-02-09 19:33:15 +00001032}
1033
Michel Thierry771b9a52014-11-11 16:47:33 +00001034int init_workarounds_ring(struct intel_engine_cs *ring)
Mika Kuoppala72253422014-10-07 17:21:26 +03001035{
1036 struct drm_device *dev = ring->dev;
1037 struct drm_i915_private *dev_priv = dev->dev_private;
1038
1039 WARN_ON(ring->id != RCS);
1040
1041 dev_priv->workarounds.count = 0;
1042
1043 if (IS_BROADWELL(dev))
1044 return bdw_init_workarounds(ring);
1045
1046 if (IS_CHERRYVIEW(dev))
1047 return chv_init_workarounds(ring);
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001048
Damien Lespiau8d205492015-02-09 19:33:15 +00001049 if (IS_SKYLAKE(dev))
1050 return skl_init_workarounds(ring);
1051 else if (IS_GEN9(dev))
Hoath, Nicholas3b106532015-02-05 10:47:16 +00001052 return gen9_init_workarounds(ring);
1053
Ville Syrjälä00e1e622014-08-27 17:33:12 +03001054 return 0;
1055}
1056
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001057static int init_render_ring(struct intel_engine_cs *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001058{
Chris Wilson78501ea2010-10-27 12:18:21 +01001059 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001060 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001061 int ret = init_ring_common(ring);
Konrad Zapalowicz9c33baa2014-06-19 19:07:15 +02001062 if (ret)
1063 return ret;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +08001064
Akash Goel61a563a2014-03-25 18:01:50 +05301065 /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
1066 if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
Daniel Vetter6b26c862012-04-24 14:04:12 +02001067 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001068
1069 /* We need to disable the AsyncFlip performance optimisations in order
1070 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1071 * programmed to '1' on all products.
Damien Lespiau8693a822013-05-03 18:48:11 +01001072 *
Ville Syrjäläb3f797a2014-04-28 14:31:09 +03001073 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001074 */
Imre Deakfbdcb062013-02-13 15:27:34 +00001075 if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 9)
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001076 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1077
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001078 /* Required for the hardware to program scanline values for waiting */
Akash Goel01fa0302014-03-24 23:00:04 +05301079 /* WaEnableFlushTlbInvalidationMode:snb */
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001080 if (INTEL_INFO(dev)->gen == 6)
1081 I915_WRITE(GFX_MODE,
Chris Wilsonaa83e302014-03-21 17:18:54 +00001082 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
Chris Wilsonf05bb0c2013-01-20 16:33:32 +00001083
Akash Goel01fa0302014-03-24 23:00:04 +05301084 /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001085 if (IS_GEN7(dev))
1086 I915_WRITE(GFX_MODE_GEN7,
Akash Goel01fa0302014-03-24 23:00:04 +05301087 _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
Chris Wilson1c8c38c2013-01-20 16:11:20 +00001088 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Chris Wilson78501ea2010-10-27 12:18:21 +01001089
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001090 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -07001091 /* From the Sandybridge PRM, volume 1 part 3, page 24:
1092 * "If this bit is set, STCunit will have LRA as replacement
1093 * policy. [...] This bit must be reset. LRA replacement
1094 * policy is not supported."
1095 */
1096 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +02001097 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky84f9f932011-12-12 19:21:58 -08001098 }
1099
Daniel Vetter6b26c862012-04-24 14:04:12 +02001100 if (INTEL_INFO(dev)->gen >= 6)
1101 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001102
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001103 if (HAS_L3_DPF(dev))
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001104 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001105
Mika Kuoppala72253422014-10-07 17:21:26 +03001106 return init_workarounds_ring(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001107}
1108
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001109static void render_ring_cleanup(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001110{
Daniel Vetterb45305f2012-12-17 16:21:27 +01001111 struct drm_device *dev = ring->dev;
Ben Widawsky3e789982014-06-30 09:53:37 -07001112 struct drm_i915_private *dev_priv = dev->dev_private;
1113
1114 if (dev_priv->semaphore_obj) {
1115 i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
1116 drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
1117 dev_priv->semaphore_obj = NULL;
1118 }
Daniel Vetterb45305f2012-12-17 16:21:27 +01001119
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001120 intel_fini_pipe_control(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001121}
1122
Ben Widawsky3e789982014-06-30 09:53:37 -07001123static int gen8_rcs_signal(struct intel_engine_cs *signaller,
1124 unsigned int num_dwords)
1125{
1126#define MBOX_UPDATE_DWORDS 8
1127 struct drm_device *dev = signaller->dev;
1128 struct drm_i915_private *dev_priv = dev->dev_private;
1129 struct intel_engine_cs *waiter;
1130 int i, ret, num_rings;
1131
1132 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1133 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1134#undef MBOX_UPDATE_DWORDS
1135
1136 ret = intel_ring_begin(signaller, num_dwords);
1137 if (ret)
1138 return ret;
1139
1140 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001141 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001142 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1143 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1144 continue;
1145
John Harrison6259cea2014-11-24 18:49:29 +00001146 seqno = i915_gem_request_get_seqno(
1147 signaller->outstanding_lazy_request);
Ben Widawsky3e789982014-06-30 09:53:37 -07001148 intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
1149 intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
1150 PIPE_CONTROL_QW_WRITE |
1151 PIPE_CONTROL_FLUSH_ENABLE);
1152 intel_ring_emit(signaller, lower_32_bits(gtt_offset));
1153 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001154 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001155 intel_ring_emit(signaller, 0);
1156 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1157 MI_SEMAPHORE_TARGET(waiter->id));
1158 intel_ring_emit(signaller, 0);
1159 }
1160
1161 return 0;
1162}
1163
1164static int gen8_xcs_signal(struct intel_engine_cs *signaller,
1165 unsigned int num_dwords)
1166{
1167#define MBOX_UPDATE_DWORDS 6
1168 struct drm_device *dev = signaller->dev;
1169 struct drm_i915_private *dev_priv = dev->dev_private;
1170 struct intel_engine_cs *waiter;
1171 int i, ret, num_rings;
1172
1173 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1174 num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
1175#undef MBOX_UPDATE_DWORDS
1176
1177 ret = intel_ring_begin(signaller, num_dwords);
1178 if (ret)
1179 return ret;
1180
1181 for_each_ring(waiter, dev_priv, i) {
John Harrison6259cea2014-11-24 18:49:29 +00001182 u32 seqno;
Ben Widawsky3e789982014-06-30 09:53:37 -07001183 u64 gtt_offset = signaller->semaphore.signal_ggtt[i];
1184 if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
1185 continue;
1186
John Harrison6259cea2014-11-24 18:49:29 +00001187 seqno = i915_gem_request_get_seqno(
1188 signaller->outstanding_lazy_request);
Ben Widawsky3e789982014-06-30 09:53:37 -07001189 intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
1190 MI_FLUSH_DW_OP_STOREDW);
1191 intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
1192 MI_FLUSH_DW_USE_GTT);
1193 intel_ring_emit(signaller, upper_32_bits(gtt_offset));
John Harrison6259cea2014-11-24 18:49:29 +00001194 intel_ring_emit(signaller, seqno);
Ben Widawsky3e789982014-06-30 09:53:37 -07001195 intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
1196 MI_SEMAPHORE_TARGET(waiter->id));
1197 intel_ring_emit(signaller, 0);
1198 }
1199
1200 return 0;
1201}
1202
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001203static int gen6_signal(struct intel_engine_cs *signaller,
Ben Widawsky024a43e2014-04-29 14:52:30 -07001204 unsigned int num_dwords)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001205{
Ben Widawsky024a43e2014-04-29 14:52:30 -07001206 struct drm_device *dev = signaller->dev;
1207 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001208 struct intel_engine_cs *useless;
Ben Widawskya1444b72014-06-30 09:53:35 -07001209 int i, ret, num_rings;
Ben Widawsky78325f22014-04-29 14:52:29 -07001210
Ben Widawskya1444b72014-06-30 09:53:35 -07001211#define MBOX_UPDATE_DWORDS 3
1212 num_rings = hweight32(INTEL_INFO(dev)->ring_mask);
1213 num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
1214#undef MBOX_UPDATE_DWORDS
Ben Widawsky024a43e2014-04-29 14:52:30 -07001215
1216 ret = intel_ring_begin(signaller, num_dwords);
1217 if (ret)
1218 return ret;
Ben Widawsky024a43e2014-04-29 14:52:30 -07001219
Ben Widawsky78325f22014-04-29 14:52:29 -07001220 for_each_ring(useless, dev_priv, i) {
1221 u32 mbox_reg = signaller->semaphore.mbox.signal[i];
1222 if (mbox_reg != GEN6_NOSYNC) {
John Harrison6259cea2014-11-24 18:49:29 +00001223 u32 seqno = i915_gem_request_get_seqno(
1224 signaller->outstanding_lazy_request);
Ben Widawsky78325f22014-04-29 14:52:29 -07001225 intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
1226 intel_ring_emit(signaller, mbox_reg);
John Harrison6259cea2014-11-24 18:49:29 +00001227 intel_ring_emit(signaller, seqno);
Ben Widawsky78325f22014-04-29 14:52:29 -07001228 }
1229 }
Ben Widawsky024a43e2014-04-29 14:52:30 -07001230
Ben Widawskya1444b72014-06-30 09:53:35 -07001231 /* If num_dwords was rounded, make sure the tail pointer is correct */
1232 if (num_rings % 2 == 0)
1233 intel_ring_emit(signaller, MI_NOOP);
1234
Ben Widawsky024a43e2014-04-29 14:52:30 -07001235 return 0;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001236}
1237
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001238/**
1239 * gen6_add_request - Update the semaphore mailbox registers
1240 *
1241 * @ring - ring that is adding a request
1242 * @seqno - return seqno stuck into the ring
1243 *
1244 * Update the mailbox registers in the *other* rings with the current seqno.
1245 * This acts like a signal in the canonical semaphore.
1246 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001247static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001248gen6_add_request(struct intel_engine_cs *ring)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001249{
Ben Widawsky024a43e2014-04-29 14:52:30 -07001250 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001251
Ben Widawsky707d9cf2014-06-30 09:53:36 -07001252 if (ring->semaphore.signal)
1253 ret = ring->semaphore.signal(ring, 4);
1254 else
1255 ret = intel_ring_begin(ring, 4);
1256
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001257 if (ret)
1258 return ret;
1259
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001260 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1261 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrison6259cea2014-11-24 18:49:29 +00001262 intel_ring_emit(ring,
1263 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001264 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001265 __intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001266
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001267 return 0;
1268}
1269
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001270static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
1271 u32 seqno)
1272{
1273 struct drm_i915_private *dev_priv = dev->dev_private;
1274 return dev_priv->last_seqno < seqno;
1275}
1276
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001277/**
1278 * intel_ring_sync - sync the waiter to the signaller on seqno
1279 *
1280 * @waiter - ring that is waiting
1281 * @signaller - ring which has, or will signal
1282 * @seqno - seqno which the waiter will block on
1283 */
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001284
1285static int
1286gen8_ring_sync(struct intel_engine_cs *waiter,
1287 struct intel_engine_cs *signaller,
1288 u32 seqno)
1289{
1290 struct drm_i915_private *dev_priv = waiter->dev->dev_private;
1291 int ret;
1292
1293 ret = intel_ring_begin(waiter, 4);
1294 if (ret)
1295 return ret;
1296
1297 intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
1298 MI_SEMAPHORE_GLOBAL_GTT |
Ben Widawskybae4fcd2014-06-30 09:53:43 -07001299 MI_SEMAPHORE_POLL |
Ben Widawsky5ee426c2014-06-30 09:53:38 -07001300 MI_SEMAPHORE_SAD_GTE_SDD);
1301 intel_ring_emit(waiter, seqno);
1302 intel_ring_emit(waiter,
1303 lower_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1304 intel_ring_emit(waiter,
1305 upper_32_bits(GEN8_WAIT_OFFSET(waiter, signaller->id)));
1306 intel_ring_advance(waiter);
1307 return 0;
1308}
1309
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001310static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001311gen6_ring_sync(struct intel_engine_cs *waiter,
1312 struct intel_engine_cs *signaller,
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001313 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001314{
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001315 u32 dw1 = MI_SEMAPHORE_MBOX |
1316 MI_SEMAPHORE_COMPARE |
1317 MI_SEMAPHORE_REGISTER;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001318 u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
1319 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001320
Ben Widawsky1500f7e2012-04-11 11:18:21 -07001321 /* Throughout all of the GEM code, seqno passed implies our current
1322 * seqno is >= the last seqno executed. However for hardware the
1323 * comparison is strictly greater than.
1324 */
1325 seqno -= 1;
1326
Ben Widawskyebc348b2014-04-29 14:52:28 -07001327 WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001328
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001329 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001330 if (ret)
1331 return ret;
1332
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001333 /* If seqno wrap happened, omit the wait with no-ops */
1334 if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
Ben Widawskyebc348b2014-04-29 14:52:28 -07001335 intel_ring_emit(waiter, dw1 | wait_mbox);
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001336 intel_ring_emit(waiter, seqno);
1337 intel_ring_emit(waiter, 0);
1338 intel_ring_emit(waiter, MI_NOOP);
1339 } else {
1340 intel_ring_emit(waiter, MI_NOOP);
1341 intel_ring_emit(waiter, MI_NOOP);
1342 intel_ring_emit(waiter, MI_NOOP);
1343 intel_ring_emit(waiter, MI_NOOP);
1344 }
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001345 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001346
1347 return 0;
1348}
1349
Chris Wilsonc6df5412010-12-15 09:56:50 +00001350#define PIPE_CONTROL_FLUSH(ring__, addr__) \
1351do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001352 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
1353 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +00001354 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
1355 intel_ring_emit(ring__, 0); \
1356 intel_ring_emit(ring__, 0); \
1357} while (0)
1358
1359static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001360pc_render_add_request(struct intel_engine_cs *ring)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001361{
Chris Wilson18393f62014-04-09 09:19:40 +01001362 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001363 int ret;
1364
1365 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
1366 * incoherent with writes to memory, i.e. completely fubar,
1367 * so we need to use PIPE_NOTIFY instead.
1368 *
1369 * However, we also need to workaround the qword write
1370 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
1371 * memory before requesting an interrupt.
1372 */
1373 ret = intel_ring_begin(ring, 32);
1374 if (ret)
1375 return ret;
1376
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001377 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001378 PIPE_CONTROL_WRITE_FLUSH |
1379 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001380 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrison6259cea2014-11-24 18:49:29 +00001381 intel_ring_emit(ring,
1382 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001383 intel_ring_emit(ring, 0);
1384 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001385 scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
Chris Wilsonc6df5412010-12-15 09:56:50 +00001386 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001387 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001388 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001389 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001390 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001391 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001392 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilson18393f62014-04-09 09:19:40 +01001393 scratch_addr += 2 * CACHELINE_BYTES;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001394 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001395
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +02001396 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +02001397 PIPE_CONTROL_WRITE_FLUSH |
1398 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +00001399 PIPE_CONTROL_NOTIFY);
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001400 intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
John Harrison6259cea2014-11-24 18:49:29 +00001401 intel_ring_emit(ring,
1402 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilsonc6df5412010-12-15 09:56:50 +00001403 intel_ring_emit(ring, 0);
Chris Wilson09246732013-08-10 22:16:32 +01001404 __intel_ring_advance(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +00001405
Chris Wilsonc6df5412010-12-15 09:56:50 +00001406 return 0;
1407}
1408
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001409static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001410gen6_ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001411{
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001412 /* Workaround to force correct ordering between irq and seqno writes on
1413 * ivb (and maybe also on snb) by reading from a CS register (like
1414 * ACTHD) before reading the status page. */
Chris Wilson50877442014-03-21 12:41:53 +00001415 if (!lazy_coherency) {
1416 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1417 POSTING_READ(RING_ACTHD(ring->mmio_base));
1418 }
1419
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001420 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1421}
1422
1423static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001424ring_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001425{
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001426 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1427}
1428
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001429static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001430ring_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001431{
1432 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1433}
1434
Chris Wilsonc6df5412010-12-15 09:56:50 +00001435static u32
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001436pc_render_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +00001437{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001438 return ring->scratch.cpu_page[0];
Chris Wilsonc6df5412010-12-15 09:56:50 +00001439}
1440
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001441static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001442pc_render_set_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001443{
Chris Wilson0d1aaca2013-08-26 20:58:11 +01001444 ring->scratch.cpu_page[0] = seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02001445}
1446
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001447static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001448gen5_ring_get_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001449{
1450 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001451 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001452 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001453
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001454 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Daniel Vettere48d8632012-04-11 22:12:54 +02001455 return false;
1456
Chris Wilson7338aef2012-04-24 21:48:47 +01001457 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001458 if (ring->irq_refcount++ == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001459 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001460 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001461
1462 return true;
1463}
1464
1465static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001466gen5_ring_put_irq(struct intel_engine_cs *ring)
Daniel Vettere48d8632012-04-11 22:12:54 +02001467{
1468 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001469 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001470 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +02001471
Chris Wilson7338aef2012-04-24 21:48:47 +01001472 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Paulo Zanoni43eaea12013-08-06 18:57:12 -03001473 if (--ring->irq_refcount == 0)
Daniel Vetter480c8032014-07-16 09:49:40 +02001474 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson7338aef2012-04-24 21:48:47 +01001475 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +02001476}
1477
1478static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001479i9xx_ring_get_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001480{
Chris Wilson78501ea2010-10-27 12:18:21 +01001481 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001482 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001483 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001484
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001485 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001486 return false;
1487
Chris Wilson7338aef2012-04-24 21:48:47 +01001488 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001489 if (ring->irq_refcount++ == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001490 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1491 I915_WRITE(IMR, dev_priv->irq_mask);
1492 POSTING_READ(IMR);
1493 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001494 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001495
1496 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001497}
1498
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001499static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001500i9xx_ring_put_irq(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001501{
Chris Wilson78501ea2010-10-27 12:18:21 +01001502 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001503 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001504 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001505
Chris Wilson7338aef2012-04-24 21:48:47 +01001506 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001507 if (--ring->irq_refcount == 0) {
Daniel Vetterf637fde2012-04-11 22:12:59 +02001508 dev_priv->irq_mask |= ring->irq_enable_mask;
1509 I915_WRITE(IMR, dev_priv->irq_mask);
1510 POSTING_READ(IMR);
1511 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001512 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001513}
1514
Chris Wilsonc2798b12012-04-22 21:13:57 +01001515static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001516i8xx_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001517{
1518 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001519 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001520 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001521
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001522 if (!intel_irqs_enabled(dev_priv))
Chris Wilsonc2798b12012-04-22 21:13:57 +01001523 return false;
1524
Chris Wilson7338aef2012-04-24 21:48:47 +01001525 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001526 if (ring->irq_refcount++ == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001527 dev_priv->irq_mask &= ~ring->irq_enable_mask;
1528 I915_WRITE16(IMR, dev_priv->irq_mask);
1529 POSTING_READ16(IMR);
1530 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001531 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001532
1533 return true;
1534}
1535
1536static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001537i8xx_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilsonc2798b12012-04-22 21:13:57 +01001538{
1539 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001540 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001541 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001542
Chris Wilson7338aef2012-04-24 21:48:47 +01001543 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001544 if (--ring->irq_refcount == 0) {
Chris Wilsonc2798b12012-04-22 21:13:57 +01001545 dev_priv->irq_mask |= ring->irq_enable_mask;
1546 I915_WRITE16(IMR, dev_priv->irq_mask);
1547 POSTING_READ16(IMR);
1548 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001549 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +01001550}
1551
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001552static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001553bsd_ring_flush(struct intel_engine_cs *ring,
Chris Wilson78501ea2010-10-27 12:18:21 +01001554 u32 invalidate_domains,
1555 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001556{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001557 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001558
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001559 ret = intel_ring_begin(ring, 2);
1560 if (ret)
1561 return ret;
1562
1563 intel_ring_emit(ring, MI_FLUSH);
1564 intel_ring_emit(ring, MI_NOOP);
1565 intel_ring_advance(ring);
1566 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001567}
1568
Chris Wilson3cce4692010-10-27 16:11:02 +01001569static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001570i9xx_add_request(struct intel_engine_cs *ring)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001571{
Chris Wilson3cce4692010-10-27 16:11:02 +01001572 int ret;
1573
1574 ret = intel_ring_begin(ring, 4);
1575 if (ret)
1576 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +01001577
Chris Wilson3cce4692010-10-27 16:11:02 +01001578 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
1579 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
John Harrison6259cea2014-11-24 18:49:29 +00001580 intel_ring_emit(ring,
1581 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Chris Wilson3cce4692010-10-27 16:11:02 +01001582 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson09246732013-08-10 22:16:32 +01001583 __intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +08001584
Chris Wilson3cce4692010-10-27 16:11:02 +01001585 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +08001586}
1587
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001588static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001589gen6_ring_get_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001590{
1591 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001592 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001593 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001594
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001595 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
1596 return false;
Chris Wilson0f468322011-01-04 17:35:21 +00001597
Chris Wilson7338aef2012-04-24 21:48:47 +01001598 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001599 if (ring->irq_refcount++ == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001600 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawskycc609d52013-05-28 19:22:29 -07001601 I915_WRITE_IMR(ring,
1602 ~(ring->irq_enable_mask |
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001603 GT_PARITY_ERROR(dev)));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001604 else
1605 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001606 gen5_enable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +00001607 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001608 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +00001609
1610 return true;
1611}
1612
1613static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001614gen6_ring_put_irq(struct intel_engine_cs *ring)
Chris Wilson0f468322011-01-04 17:35:21 +00001615{
1616 struct drm_device *dev = ring->dev;
Jani Nikula4640c4f2014-03-31 14:27:19 +03001617 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +01001618 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +00001619
Chris Wilson7338aef2012-04-24 21:48:47 +01001620 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001621 if (--ring->irq_refcount == 0) {
Ben Widawsky040d2ba2013-09-19 11:01:40 -07001622 if (HAS_L3_DPF(dev) && ring->id == RCS)
Ben Widawsky35a85ac2013-09-19 11:13:41 -07001623 I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
Ben Widawsky15b9f802012-05-25 16:56:23 -07001624 else
1625 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001626 gen5_disable_gt_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001627 }
Chris Wilson7338aef2012-04-24 21:48:47 +01001628 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001629}
1630
Ben Widawskya19d2932013-05-28 19:22:30 -07001631static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001632hsw_vebox_get_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001633{
1634 struct drm_device *dev = ring->dev;
1635 struct drm_i915_private *dev_priv = dev->dev_private;
1636 unsigned long flags;
1637
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001638 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskya19d2932013-05-28 19:22:30 -07001639 return false;
1640
Daniel Vetter59cdb632013-07-04 23:35:28 +02001641 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001642 if (ring->irq_refcount++ == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001643 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetter480c8032014-07-16 09:49:40 +02001644 gen6_enable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001645 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001646 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001647
1648 return true;
1649}
1650
1651static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001652hsw_vebox_put_irq(struct intel_engine_cs *ring)
Ben Widawskya19d2932013-05-28 19:22:30 -07001653{
1654 struct drm_device *dev = ring->dev;
1655 struct drm_i915_private *dev_priv = dev->dev_private;
1656 unsigned long flags;
1657
Daniel Vetter59cdb632013-07-04 23:35:28 +02001658 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterc7113cc2013-07-04 23:35:29 +02001659 if (--ring->irq_refcount == 0) {
Ben Widawskya19d2932013-05-28 19:22:30 -07001660 I915_WRITE_IMR(ring, ~0);
Daniel Vetter480c8032014-07-16 09:49:40 +02001661 gen6_disable_pm_irq(dev_priv, ring->irq_enable_mask);
Ben Widawskya19d2932013-05-28 19:22:30 -07001662 }
Daniel Vetter59cdb632013-07-04 23:35:28 +02001663 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Ben Widawskya19d2932013-05-28 19:22:30 -07001664}
1665
Ben Widawskyabd58f02013-11-02 21:07:09 -07001666static bool
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001667gen8_ring_get_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001668{
1669 struct drm_device *dev = ring->dev;
1670 struct drm_i915_private *dev_priv = dev->dev_private;
1671 unsigned long flags;
1672
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001673 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Ben Widawskyabd58f02013-11-02 21:07:09 -07001674 return false;
1675
1676 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1677 if (ring->irq_refcount++ == 0) {
1678 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1679 I915_WRITE_IMR(ring,
1680 ~(ring->irq_enable_mask |
1681 GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
1682 } else {
1683 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
1684 }
1685 POSTING_READ(RING_IMR(ring->mmio_base));
1686 }
1687 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1688
1689 return true;
1690}
1691
1692static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001693gen8_ring_put_irq(struct intel_engine_cs *ring)
Ben Widawskyabd58f02013-11-02 21:07:09 -07001694{
1695 struct drm_device *dev = ring->dev;
1696 struct drm_i915_private *dev_priv = dev->dev_private;
1697 unsigned long flags;
1698
1699 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1700 if (--ring->irq_refcount == 0) {
1701 if (HAS_L3_DPF(dev) && ring->id == RCS) {
1702 I915_WRITE_IMR(ring,
1703 ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
1704 } else {
1705 I915_WRITE_IMR(ring, ~0);
1706 }
1707 POSTING_READ(RING_IMR(ring->mmio_base));
1708 }
1709 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1710}
1711
Zou Nan haid1b851f2010-05-21 09:08:57 +08001712static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001713i965_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001714 u64 offset, u32 length,
John Harrison8e004ef2015-02-13 11:48:10 +00001715 unsigned dispatch_flags)
Zou Nan haid1b851f2010-05-21 09:08:57 +08001716{
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001717 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001718
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001719 ret = intel_ring_begin(ring, 2);
1720 if (ret)
1721 return ret;
1722
Chris Wilson78501ea2010-10-27 12:18:21 +01001723 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +01001724 MI_BATCH_BUFFER_START |
1725 MI_BATCH_GTT |
John Harrison8e004ef2015-02-13 11:48:10 +00001726 (dispatch_flags & I915_DISPATCH_SECURE ?
1727 0 : MI_BATCH_NON_SECURE_I965));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001728 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +01001729 intel_ring_advance(ring);
1730
Zou Nan haid1b851f2010-05-21 09:08:57 +08001731 return 0;
1732}
1733
Daniel Vetterb45305f2012-12-17 16:21:27 +01001734/* Just userspace ABI convention to limit the wa batch bo to a resonable size */
1735#define I830_BATCH_LIMIT (256*1024)
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001736#define I830_TLB_ENTRIES (2)
1737#define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001738static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001739i830_dispatch_execbuffer(struct intel_engine_cs *ring,
John Harrison8e004ef2015-02-13 11:48:10 +00001740 u64 offset, u32 len,
1741 unsigned dispatch_flags)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001742{
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001743 u32 cs_offset = ring->scratch.gtt_offset;
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001744 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001745
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001746 ret = intel_ring_begin(ring, 6);
1747 if (ret)
1748 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001749
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001750 /* Evict the invalid PTE TLBs */
1751 intel_ring_emit(ring, COLOR_BLT_CMD | BLT_WRITE_RGBA);
1752 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
1753 intel_ring_emit(ring, I830_TLB_ENTRIES << 16 | 4); /* load each page */
1754 intel_ring_emit(ring, cs_offset);
1755 intel_ring_emit(ring, 0xdeadbeef);
1756 intel_ring_emit(ring, MI_NOOP);
1757 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001758
John Harrison8e004ef2015-02-13 11:48:10 +00001759 if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
Daniel Vetterb45305f2012-12-17 16:21:27 +01001760 if (len > I830_BATCH_LIMIT)
1761 return -ENOSPC;
1762
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001763 ret = intel_ring_begin(ring, 6 + 2);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001764 if (ret)
1765 return ret;
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001766
1767 /* Blit the batch (which has now all relocs applied) to the
1768 * stable batch scratch bo area (so that the CS never
1769 * stumbles over its tlb invalidation bug) ...
1770 */
1771 intel_ring_emit(ring, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
1772 intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
Chris Wilson611a7a42014-09-12 07:37:42 +01001773 intel_ring_emit(ring, DIV_ROUND_UP(len, 4096) << 16 | 4096);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001774 intel_ring_emit(ring, cs_offset);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001775 intel_ring_emit(ring, 4096);
1776 intel_ring_emit(ring, offset);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001777
Daniel Vetterb45305f2012-12-17 16:21:27 +01001778 intel_ring_emit(ring, MI_FLUSH);
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001779 intel_ring_emit(ring, MI_NOOP);
1780 intel_ring_advance(ring);
Daniel Vetterb45305f2012-12-17 16:21:27 +01001781
1782 /* ... and execute it. */
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001783 offset = cs_offset;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001784 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001785
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001786 ret = intel_ring_begin(ring, 4);
1787 if (ret)
1788 return ret;
1789
1790 intel_ring_emit(ring, MI_BATCH_BUFFER);
John Harrison8e004ef2015-02-13 11:48:10 +00001791 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1792 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4d69da2014-09-08 14:25:41 +01001793 intel_ring_emit(ring, offset + len - 8);
1794 intel_ring_emit(ring, MI_NOOP);
1795 intel_ring_advance(ring);
1796
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001797 return 0;
1798}
1799
1800static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001801i915_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07001802 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00001803 unsigned dispatch_flags)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001804{
1805 int ret;
1806
1807 ret = intel_ring_begin(ring, 2);
1808 if (ret)
1809 return ret;
1810
Chris Wilson65f56872012-04-17 16:38:12 +01001811 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
John Harrison8e004ef2015-02-13 11:48:10 +00001812 intel_ring_emit(ring, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
1813 0 : MI_BATCH_NON_SECURE));
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001814 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001815
Eric Anholt62fdfea2010-05-21 13:26:39 -07001816 return 0;
1817}
1818
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001819static void cleanup_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001820{
Chris Wilson05394f32010-11-08 19:18:58 +00001821 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001822
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001823 obj = ring->status_page.obj;
1824 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001825 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001826
Chris Wilson9da3da62012-06-01 15:20:22 +01001827 kunmap(sg_page(obj->pages->sgl));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001828 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001829 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001830 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001831}
1832
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001833static int init_status_page(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001834{
Chris Wilson05394f32010-11-08 19:18:58 +00001835 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001836
Chris Wilsone3efda42014-04-09 09:19:41 +01001837 if ((obj = ring->status_page.obj) == NULL) {
Chris Wilson1f767e02014-07-03 17:33:03 -04001838 unsigned flags;
Chris Wilsone3efda42014-04-09 09:19:41 +01001839 int ret;
1840
1841 obj = i915_gem_alloc_object(ring->dev, 4096);
1842 if (obj == NULL) {
1843 DRM_ERROR("Failed to allocate status page\n");
1844 return -ENOMEM;
1845 }
1846
1847 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
1848 if (ret)
1849 goto err_unref;
1850
Chris Wilson1f767e02014-07-03 17:33:03 -04001851 flags = 0;
1852 if (!HAS_LLC(ring->dev))
1853 /* On g33, we cannot place HWS above 256MiB, so
1854 * restrict its pinning to the low mappable arena.
1855 * Though this restriction is not documented for
1856 * gen4, gen5, or byt, they also behave similarly
1857 * and hang if the HWS is placed at the top of the
1858 * GTT. To generalise, it appears that all !llc
1859 * platforms have issues with us placing the HWS
1860 * above the mappable region (even though we never
1861 * actualy map it).
1862 */
1863 flags |= PIN_MAPPABLE;
1864 ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
Chris Wilsone3efda42014-04-09 09:19:41 +01001865 if (ret) {
1866err_unref:
1867 drm_gem_object_unreference(&obj->base);
1868 return ret;
1869 }
1870
1871 ring->status_page.obj = obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001872 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001873
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001874 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01001875 ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001876 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001877
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001878 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1879 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001880
1881 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001882}
1883
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001884static int init_phys_status_page(struct intel_engine_cs *ring)
Chris Wilson6b8294a2012-11-16 11:43:20 +00001885{
1886 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001887
1888 if (!dev_priv->status_page_dmah) {
1889 dev_priv->status_page_dmah =
1890 drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
1891 if (!dev_priv->status_page_dmah)
1892 return -ENOMEM;
1893 }
1894
Chris Wilson6b8294a2012-11-16 11:43:20 +00001895 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1896 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1897
1898 return 0;
1899}
1900
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001901void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
1902{
1903 iounmap(ringbuf->virtual_start);
1904 ringbuf->virtual_start = NULL;
1905 i915_gem_object_ggtt_unpin(ringbuf->obj);
1906}
1907
1908int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
1909 struct intel_ringbuffer *ringbuf)
1910{
1911 struct drm_i915_private *dev_priv = to_i915(dev);
1912 struct drm_i915_gem_object *obj = ringbuf->obj;
1913 int ret;
1914
1915 ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
1916 if (ret)
1917 return ret;
1918
1919 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1920 if (ret) {
1921 i915_gem_object_ggtt_unpin(obj);
1922 return ret;
1923 }
1924
1925 ringbuf->virtual_start = ioremap_wc(dev_priv->gtt.mappable_base +
1926 i915_gem_obj_ggtt_offset(obj), ringbuf->size);
1927 if (ringbuf->virtual_start == NULL) {
1928 i915_gem_object_ggtt_unpin(obj);
1929 return -EINVAL;
1930 }
1931
1932 return 0;
1933}
1934
Oscar Mateo84c23772014-07-24 17:04:15 +01001935void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
Chris Wilsone3efda42014-04-09 09:19:41 +01001936{
Oscar Mateo2919d292014-07-03 16:28:02 +01001937 drm_gem_object_unreference(&ringbuf->obj->base);
1938 ringbuf->obj = NULL;
1939}
1940
Oscar Mateo84c23772014-07-24 17:04:15 +01001941int intel_alloc_ringbuffer_obj(struct drm_device *dev,
1942 struct intel_ringbuffer *ringbuf)
Oscar Mateo2919d292014-07-03 16:28:02 +01001943{
Chris Wilsone3efda42014-04-09 09:19:41 +01001944 struct drm_i915_gem_object *obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01001945
1946 obj = NULL;
1947 if (!HAS_LLC(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001948 obj = i915_gem_object_create_stolen(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01001949 if (obj == NULL)
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001950 obj = i915_gem_alloc_object(dev, ringbuf->size);
Chris Wilsone3efda42014-04-09 09:19:41 +01001951 if (obj == NULL)
1952 return -ENOMEM;
1953
Akash Goel24f3a8c2014-06-17 10:59:42 +05301954 /* mark ring buffers as read-only from GPU side by default */
1955 obj->gt_ro = 1;
1956
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001957 ringbuf->obj = obj;
Chris Wilsone3efda42014-04-09 09:19:41 +01001958
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001959 return 0;
Chris Wilsone3efda42014-04-09 09:19:41 +01001960}
1961
Ben Widawskyc43b5632012-04-16 14:07:40 -07001962static int intel_init_ring_buffer(struct drm_device *dev,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001963 struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001964{
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001965 struct intel_ringbuffer *ringbuf;
Chris Wilsondd785e32010-08-07 11:01:34 +01001966 int ret;
1967
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001968 WARN_ON(ring->buffer);
1969
1970 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1971 if (!ringbuf)
1972 return -ENOMEM;
1973 ring->buffer = ringbuf;
Oscar Mateo8ee14972014-05-22 14:13:34 +01001974
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001975 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01001976 INIT_LIST_HEAD(&ring->active_list);
1977 INIT_LIST_HEAD(&ring->request_list);
Oscar Mateocc9130b2014-07-24 17:04:42 +01001978 INIT_LIST_HEAD(&ring->execlist_queue);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01001979 ringbuf->size = 32 * PAGE_SIZE;
Daniel Vetter0c7dd532014-08-11 16:17:44 +02001980 ringbuf->ring = ring;
Ben Widawskyebc348b2014-04-29 14:52:28 -07001981 memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001982
Chris Wilsonb259f672011-03-29 13:19:09 +01001983 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001984
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001985 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001986 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001987 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001988 goto error;
Chris Wilson6b8294a2012-11-16 11:43:20 +00001989 } else {
1990 BUG_ON(ring->id != RCS);
Daniel Vetter035dc1e2013-07-03 12:56:54 +02001991 ret = init_phys_status_page(ring);
Chris Wilson6b8294a2012-11-16 11:43:20 +00001992 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01001993 goto error;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001994 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001995
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001996 WARN_ON(ringbuf->obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001997
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001998 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
1999 if (ret) {
2000 DRM_ERROR("Failed to allocate ringbuffer %s: %d\n",
2001 ring->name, ret);
2002 goto error;
2003 }
2004
2005 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2006 if (ret) {
2007 DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
2008 ring->name, ret);
2009 intel_destroy_ringbuffer_obj(ringbuf);
2010 goto error;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002011 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07002012
Chris Wilson55249ba2010-12-22 14:04:47 +00002013 /* Workaround an erratum on the i830 which causes a hang if
2014 * the TAIL pointer points to within the last 2 cachelines
2015 * of the buffer.
2016 */
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002017 ringbuf->effective_size = ringbuf->size;
Chris Wilsone3efda42014-04-09 09:19:41 +01002018 if (IS_I830(dev) || IS_845G(dev))
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002019 ringbuf->effective_size -= 2 * CACHELINE_BYTES;
Chris Wilson55249ba2010-12-22 14:04:47 +00002020
Brad Volkin44e895a2014-05-10 14:10:43 -07002021 ret = i915_cmd_parser_init_ring(ring);
2022 if (ret)
Oscar Mateo8ee14972014-05-22 14:13:34 +01002023 goto error;
Brad Volkin351e3db2014-02-18 10:15:46 -08002024
Oscar Mateo8ee14972014-05-22 14:13:34 +01002025 return 0;
2026
2027error:
2028 kfree(ringbuf);
2029 ring->buffer = NULL;
2030 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002031}
2032
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002033void intel_cleanup_ring_buffer(struct intel_engine_cs *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002034{
John Harrison6402c332014-10-31 12:00:26 +00002035 struct drm_i915_private *dev_priv;
2036 struct intel_ringbuffer *ringbuf;
Chris Wilson33626e62010-10-29 16:18:36 +01002037
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002038 if (!intel_ring_initialized(ring))
Eric Anholt62fdfea2010-05-21 13:26:39 -07002039 return;
2040
John Harrison6402c332014-10-31 12:00:26 +00002041 dev_priv = to_i915(ring->dev);
2042 ringbuf = ring->buffer;
2043
Chris Wilsone3efda42014-04-09 09:19:41 +01002044 intel_stop_ring_buffer(ring);
Ville Syrjäläde8f0a52014-05-28 19:12:13 +03002045 WARN_ON(!IS_GEN2(ring->dev) && (I915_READ_MODE(ring) & MODE_IDLE) == 0);
Chris Wilson33626e62010-10-29 16:18:36 +01002046
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002047 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateo2919d292014-07-03 16:28:02 +01002048 intel_destroy_ringbuffer_obj(ringbuf);
John Harrison6259cea2014-11-24 18:49:29 +00002049 i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
Chris Wilson78501ea2010-10-27 12:18:21 +01002050
Zou Nan hai8d192152010-11-02 16:31:01 +08002051 if (ring->cleanup)
2052 ring->cleanup(ring);
2053
Chris Wilson78501ea2010-10-27 12:18:21 +01002054 cleanup_status_page(ring);
Brad Volkin44e895a2014-05-10 14:10:43 -07002055
2056 i915_cmd_parser_fini_ring(ring);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002057
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002058 kfree(ringbuf);
Oscar Mateo8ee14972014-05-22 14:13:34 +01002059 ring->buffer = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002060}
2061
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002062static int intel_ring_wait_request(struct intel_engine_cs *ring, int n)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002063{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002064 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002065 struct drm_i915_gem_request *request;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002066 int ret;
2067
Dave Gordonebd0fd42014-11-27 11:22:49 +00002068 if (intel_ring_space(ringbuf) >= n)
2069 return 0;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002070
2071 list_for_each_entry(request, &ring->request_list, list) {
Nick Hoath72f95af2015-01-15 13:10:37 +00002072 if (__intel_ring_space(request->postfix, ringbuf->tail,
Oscar Mateo82e104c2014-07-24 17:04:26 +01002073 ringbuf->size) >= n) {
Chris Wilsona71d8d92012-02-15 11:25:36 +00002074 break;
2075 }
Chris Wilsona71d8d92012-02-15 11:25:36 +00002076 }
2077
Daniel Vettera4b3a572014-11-26 14:17:05 +01002078 if (&request->list == &ring->request_list)
Chris Wilsona71d8d92012-02-15 11:25:36 +00002079 return -ENOSPC;
2080
Daniel Vettera4b3a572014-11-26 14:17:05 +01002081 ret = i915_wait_request(request);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002082 if (ret)
2083 return ret;
2084
Chris Wilson1cf0ba12014-05-05 09:07:33 +01002085 i915_gem_retire_requests_ring(ring);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002086
2087 return 0;
2088}
2089
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002090static int ring_wait_for_space(struct intel_engine_cs *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07002091{
Chris Wilson78501ea2010-10-27 12:18:21 +01002092 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08002093 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002094 struct intel_ringbuffer *ringbuf = ring->buffer;
Chris Wilson78501ea2010-10-27 12:18:21 +01002095 unsigned long end;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002096 int ret;
Chris Wilsonc7dca472011-01-20 17:00:10 +00002097
Chris Wilsona71d8d92012-02-15 11:25:36 +00002098 ret = intel_ring_wait_request(ring, n);
2099 if (ret != -ENOSPC)
2100 return ret;
2101
Chris Wilson09246732013-08-10 22:16:32 +01002102 /* force the tail write in case we have been skipping them */
2103 __intel_ring_advance(ring);
2104
Daniel Vetter63ed2cb2012-04-23 16:50:50 +02002105 /* With GEM the hangcheck timer should kick us out of the loop,
2106 * leaving it early runs the risk of corrupting GEM state (due
2107 * to running on almost untested codepaths). But on resume
2108 * timers don't work yet, so prevent a complete hang in that
2109 * case by choosing an insanely large timeout. */
2110 end = jiffies + 60 * HZ;
Daniel Vettere6bfaf82011-12-14 13:56:59 +01002111
Dave Gordonebd0fd42014-11-27 11:22:49 +00002112 ret = 0;
Chris Wilsondcfe0502014-05-05 09:07:32 +01002113 trace_i915_ring_wait_begin(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002114 do {
Dave Gordonebd0fd42014-11-27 11:22:49 +00002115 if (intel_ring_space(ringbuf) >= n)
Chris Wilsondcfe0502014-05-05 09:07:32 +01002116 break;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002117 ringbuf->head = I915_READ_HEAD(ring);
2118 if (intel_ring_space(ringbuf) >= n)
2119 break;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002120
Chris Wilsone60a0b12010-10-13 10:09:14 +01002121 msleep(1);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002122
Chris Wilsondcfe0502014-05-05 09:07:32 +01002123 if (dev_priv->mm.interruptible && signal_pending(current)) {
2124 ret = -ERESTARTSYS;
2125 break;
2126 }
2127
Daniel Vetter33196de2012-11-14 17:14:05 +01002128 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2129 dev_priv->mm.interruptible);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02002130 if (ret)
Chris Wilsondcfe0502014-05-05 09:07:32 +01002131 break;
2132
2133 if (time_after(jiffies, end)) {
2134 ret = -EBUSY;
2135 break;
2136 }
2137 } while (1);
Chris Wilsondb53a302011-02-03 11:57:46 +00002138 trace_i915_ring_wait_end(ring);
Chris Wilsondcfe0502014-05-05 09:07:32 +01002139 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07002140}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002141
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002142static int intel_wrap_ring_buffer(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002143{
2144 uint32_t __iomem *virt;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002145 struct intel_ringbuffer *ringbuf = ring->buffer;
2146 int rem = ringbuf->size - ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002147
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002148 if (ringbuf->space < rem) {
Chris Wilson3e960502012-11-27 16:22:54 +00002149 int ret = ring_wait_for_space(ring, rem);
2150 if (ret)
2151 return ret;
2152 }
2153
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002154 virt = ringbuf->virtual_start + ringbuf->tail;
Chris Wilson3e960502012-11-27 16:22:54 +00002155 rem /= 4;
2156 while (rem--)
2157 iowrite32(MI_NOOP, virt++);
2158
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002159 ringbuf->tail = 0;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002160 intel_ring_update_space(ringbuf);
Chris Wilson3e960502012-11-27 16:22:54 +00002161
2162 return 0;
2163}
2164
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002165int intel_ring_idle(struct intel_engine_cs *ring)
Chris Wilson3e960502012-11-27 16:22:54 +00002166{
Daniel Vettera4b3a572014-11-26 14:17:05 +01002167 struct drm_i915_gem_request *req;
Chris Wilson3e960502012-11-27 16:22:54 +00002168 int ret;
2169
2170 /* We need to add any requests required to flush the objects and ring */
John Harrison6259cea2014-11-24 18:49:29 +00002171 if (ring->outstanding_lazy_request) {
John Harrison9400ae52014-11-24 18:49:36 +00002172 ret = i915_add_request(ring);
Chris Wilson3e960502012-11-27 16:22:54 +00002173 if (ret)
2174 return ret;
2175 }
2176
2177 /* Wait upon the last request to be completed */
2178 if (list_empty(&ring->request_list))
2179 return 0;
2180
Daniel Vettera4b3a572014-11-26 14:17:05 +01002181 req = list_entry(ring->request_list.prev,
Chris Wilson3e960502012-11-27 16:22:54 +00002182 struct drm_i915_gem_request,
Daniel Vettera4b3a572014-11-26 14:17:05 +01002183 list);
Chris Wilson3e960502012-11-27 16:22:54 +00002184
Daniel Vettera4b3a572014-11-26 14:17:05 +01002185 return i915_wait_request(req);
Chris Wilson3e960502012-11-27 16:22:54 +00002186}
2187
Chris Wilson9d7730912012-11-27 16:22:52 +00002188static int
John Harrison6259cea2014-11-24 18:49:29 +00002189intel_ring_alloc_request(struct intel_engine_cs *ring)
Chris Wilson9d7730912012-11-27 16:22:52 +00002190{
John Harrison9eba5d42014-11-24 18:49:23 +00002191 int ret;
2192 struct drm_i915_gem_request *request;
John Harrison67e29372014-12-05 13:49:35 +00002193 struct drm_i915_private *dev_private = ring->dev->dev_private;
John Harrison9eba5d42014-11-24 18:49:23 +00002194
John Harrison6259cea2014-11-24 18:49:29 +00002195 if (ring->outstanding_lazy_request)
Chris Wilson9d7730912012-11-27 16:22:52 +00002196 return 0;
John Harrison9eba5d42014-11-24 18:49:23 +00002197
John Harrisonaaeb1ba2014-12-05 13:49:34 +00002198 request = kzalloc(sizeof(*request), GFP_KERNEL);
John Harrison9eba5d42014-11-24 18:49:23 +00002199 if (request == NULL)
2200 return -ENOMEM;
2201
John Harrisonabfe2622014-11-24 18:49:24 +00002202 kref_init(&request->ref);
John Harrisonff79e852014-11-24 18:49:41 +00002203 request->ring = ring;
John Harrison98e1bd42015-02-13 11:48:12 +00002204 request->ringbuf = ring->buffer;
John Harrison67e29372014-12-05 13:49:35 +00002205 request->uniq = dev_private->request_uniq++;
John Harrisonabfe2622014-11-24 18:49:24 +00002206
John Harrison6259cea2014-11-24 18:49:29 +00002207 ret = i915_gem_get_seqno(ring->dev, &request->seqno);
John Harrison9eba5d42014-11-24 18:49:23 +00002208 if (ret) {
2209 kfree(request);
2210 return ret;
2211 }
2212
John Harrison6259cea2014-11-24 18:49:29 +00002213 ring->outstanding_lazy_request = request;
John Harrison9eba5d42014-11-24 18:49:23 +00002214 return 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002215}
2216
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002217static int __intel_ring_prepare(struct intel_engine_cs *ring,
Chris Wilson304d6952014-01-02 14:32:35 +00002218 int bytes)
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002219{
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002220 struct intel_ringbuffer *ringbuf = ring->buffer;
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002221 int ret;
2222
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002223 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002224 ret = intel_wrap_ring_buffer(ring);
2225 if (unlikely(ret))
2226 return ret;
2227 }
2228
Oscar Mateo93b0a4e2014-05-22 14:13:36 +01002229 if (unlikely(ringbuf->space < bytes)) {
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002230 ret = ring_wait_for_space(ring, bytes);
2231 if (unlikely(ret))
2232 return ret;
2233 }
2234
Mika Kuoppalacbcc80d2012-12-04 15:12:03 +02002235 return 0;
2236}
2237
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002238int intel_ring_begin(struct intel_engine_cs *ring,
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002239 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002240{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002241 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002242 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01002243
Daniel Vetter33196de2012-11-14 17:14:05 +01002244 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
2245 dev_priv->mm.interruptible);
Daniel Vetterde2b9982012-07-04 22:52:50 +02002246 if (ret)
2247 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00002248
Chris Wilson304d6952014-01-02 14:32:35 +00002249 ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
2250 if (ret)
2251 return ret;
2252
Chris Wilson9d7730912012-11-27 16:22:52 +00002253 /* Preallocate the olr before touching the ring */
John Harrison6259cea2014-11-24 18:49:29 +00002254 ret = intel_ring_alloc_request(ring);
Chris Wilson9d7730912012-11-27 16:22:52 +00002255 if (ret)
2256 return ret;
2257
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002258 ring->buffer->space -= num_dwords * sizeof(uint32_t);
Chris Wilson304d6952014-01-02 14:32:35 +00002259 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08002260}
2261
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002262/* Align the ring tail to a cacheline boundary */
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002263int intel_ring_cacheline_align(struct intel_engine_cs *ring)
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002264{
Oscar Mateoee1b1e52014-05-22 14:13:35 +01002265 int num_dwords = (ring->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002266 int ret;
2267
2268 if (num_dwords == 0)
2269 return 0;
2270
Chris Wilson18393f62014-04-09 09:19:40 +01002271 num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
Ville Syrjälä753b1ad2014-02-11 19:52:05 +02002272 ret = intel_ring_begin(ring, num_dwords);
2273 if (ret)
2274 return ret;
2275
2276 while (num_dwords--)
2277 intel_ring_emit(ring, MI_NOOP);
2278
2279 intel_ring_advance(ring);
2280
2281 return 0;
2282}
2283
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002284void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno)
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002285{
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002286 struct drm_device *dev = ring->dev;
2287 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002288
John Harrison6259cea2014-11-24 18:49:29 +00002289 BUG_ON(ring->outstanding_lazy_request);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002290
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002291 if (INTEL_INFO(dev)->gen == 6 || INTEL_INFO(dev)->gen == 7) {
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002292 I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
2293 I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
Oscar Mateo3b2cc8a2014-06-11 16:17:16 +01002294 if (HAS_VEBOX(dev))
Ben Widawsky50201502013-08-12 16:53:03 -07002295 I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
Chris Wilson78501ea2010-10-27 12:18:21 +01002296 }
Chris Wilson297b0c52010-10-22 17:02:41 +01002297
Mika Kuoppalaf7e98ad2012-12-19 11:13:06 +02002298 ring->set_seqno(ring, seqno);
Mika Kuoppala92cab732013-05-24 17:16:07 +03002299 ring->hangcheck.seqno = seqno;
Chris Wilson549f7362010-10-19 11:19:32 +01002300}
2301
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002302static void gen6_bsd_ring_write_tail(struct intel_engine_cs *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002303 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002304{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002305 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002306
2307 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002308
Chris Wilson12f55812012-07-05 17:14:01 +01002309 /* Disable notification that the ring is IDLE. The GT
2310 * will then assume that it is busy and bring it out of rc6.
2311 */
2312 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
2313 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
2314
2315 /* Clear the context id. Here be magic! */
2316 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
2317
2318 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04002319 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01002320 GEN6_BSD_SLEEP_INDICATOR) == 0,
2321 50))
2322 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002323
Chris Wilson12f55812012-07-05 17:14:01 +01002324 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04002325 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01002326 POSTING_READ(RING_TAIL(ring->mmio_base));
2327
2328 /* Let the ring send IDLE messages to the GT again,
2329 * and so let it sleep to conserve power when idle.
2330 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002331 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01002332 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002333}
2334
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002335static int gen6_bsd_ring_flush(struct intel_engine_cs *ring,
Ben Widawskyea251322013-05-28 19:22:21 -07002336 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002337{
Chris Wilson71a77e02011-02-02 12:13:49 +00002338 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002339 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002340
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002341 ret = intel_ring_begin(ring, 4);
2342 if (ret)
2343 return ret;
2344
Chris Wilson71a77e02011-02-02 12:13:49 +00002345 cmd = MI_FLUSH_DW;
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002346 if (INTEL_INFO(ring->dev)->gen >= 8)
2347 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002348
2349 /* We always require a command barrier so that subsequent
2350 * commands, such as breadcrumb interrupts, are strictly ordered
2351 * wrt the contents of the write cache being flushed to memory
2352 * (and thus being coherent from the CPU).
2353 */
2354 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2355
Jesse Barnes9a289772012-10-26 09:42:42 -07002356 /*
2357 * Bspec vol 1c.5 - video engine command streamer:
2358 * "If ENABLED, all TLBs will be invalidated once the flush
2359 * operation is complete. This bit is only valid when the
2360 * Post-Sync Operation field is a value of 1h or 3h."
2361 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002362 if (invalidate & I915_GEM_GPU_DOMAINS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002363 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
2364
Chris Wilson71a77e02011-02-02 12:13:49 +00002365 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002366 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002367 if (INTEL_INFO(ring->dev)->gen >= 8) {
2368 intel_ring_emit(ring, 0); /* upper addr */
2369 intel_ring_emit(ring, 0); /* value */
2370 } else {
2371 intel_ring_emit(ring, 0);
2372 intel_ring_emit(ring, MI_NOOP);
2373 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002374 intel_ring_advance(ring);
2375 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002376}
2377
2378static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002379gen8_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002380 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002381 unsigned dispatch_flags)
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002382{
John Harrison8e004ef2015-02-13 11:48:10 +00002383 bool ppgtt = USES_PPGTT(ring->dev) &&
2384 !(dispatch_flags & I915_DISPATCH_SECURE);
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002385 int ret;
2386
2387 ret = intel_ring_begin(ring, 4);
2388 if (ret)
2389 return ret;
2390
2391 /* FIXME(BDW): Address space and security selectors. */
Ben Widawsky28cf5412013-11-02 21:07:26 -07002392 intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002393 intel_ring_emit(ring, lower_32_bits(offset));
2394 intel_ring_emit(ring, upper_32_bits(offset));
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002395 intel_ring_emit(ring, MI_NOOP);
2396 intel_ring_advance(ring);
2397
2398 return 0;
2399}
2400
2401static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002402hsw_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
John Harrison8e004ef2015-02-13 11:48:10 +00002403 u64 offset, u32 len,
2404 unsigned dispatch_flags)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002405{
Akshay Joshi0206e352011-08-16 15:34:10 -04002406 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002407
Akshay Joshi0206e352011-08-16 15:34:10 -04002408 ret = intel_ring_begin(ring, 2);
2409 if (ret)
2410 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01002411
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002412 intel_ring_emit(ring,
Chris Wilson77072252014-09-10 12:18:27 +01002413 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002414 (dispatch_flags & I915_DISPATCH_SECURE ?
Chris Wilson77072252014-09-10 12:18:27 +01002415 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW));
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002416 /* bit0-7 is the length on GEN6+ */
2417 intel_ring_emit(ring, offset);
2418 intel_ring_advance(ring);
2419
2420 return 0;
2421}
2422
2423static int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002424gen6_ring_dispatch_execbuffer(struct intel_engine_cs *ring,
Ben Widawsky9bcb1442014-04-28 19:29:25 -07002425 u64 offset, u32 len,
John Harrison8e004ef2015-02-13 11:48:10 +00002426 unsigned dispatch_flags)
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002427{
2428 int ret;
2429
2430 ret = intel_ring_begin(ring, 2);
2431 if (ret)
2432 return ret;
2433
2434 intel_ring_emit(ring,
2435 MI_BATCH_BUFFER_START |
John Harrison8e004ef2015-02-13 11:48:10 +00002436 (dispatch_flags & I915_DISPATCH_SECURE ?
2437 0 : MI_BATCH_NON_SECURE_I965));
Akshay Joshi0206e352011-08-16 15:34:10 -04002438 /* bit0-7 is the length on GEN6+ */
2439 intel_ring_emit(ring, offset);
2440 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01002441
Akshay Joshi0206e352011-08-16 15:34:10 -04002442 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01002443}
2444
Chris Wilson549f7362010-10-19 11:19:32 +01002445/* Blitter support (SandyBridge+) */
2446
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002447static int gen6_ring_flush(struct intel_engine_cs *ring,
Ben Widawskyea251322013-05-28 19:22:21 -07002448 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08002449{
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002450 struct drm_device *dev = ring->dev;
Chris Wilson71a77e02011-02-02 12:13:49 +00002451 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002452 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002453
Daniel Vetter6a233c72011-12-14 13:57:07 +01002454 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002455 if (ret)
2456 return ret;
2457
Chris Wilson71a77e02011-02-02 12:13:49 +00002458 cmd = MI_FLUSH_DW;
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002459 if (INTEL_INFO(dev)->gen >= 8)
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002460 cmd += 1;
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002461
2462 /* We always require a command barrier so that subsequent
2463 * commands, such as breadcrumb interrupts, are strictly ordered
2464 * wrt the contents of the write cache being flushed to memory
2465 * (and thus being coherent from the CPU).
2466 */
2467 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2468
Jesse Barnes9a289772012-10-26 09:42:42 -07002469 /*
2470 * Bspec vol 1c.3 - blitter engine command streamer:
2471 * "If ENABLED, all TLBs will be invalidated once the flush
2472 * operation is complete. This bit is only valid when the
2473 * Post-Sync Operation field is a value of 1h or 3h."
2474 */
Chris Wilson71a77e02011-02-02 12:13:49 +00002475 if (invalidate & I915_GEM_DOMAIN_RENDER)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00002476 cmd |= MI_INVALIDATE_TLB;
Chris Wilson71a77e02011-02-02 12:13:49 +00002477 intel_ring_emit(ring, cmd);
Jesse Barnes9a289772012-10-26 09:42:42 -07002478 intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
Paulo Zanonidbef0f12015-02-13 17:23:46 -02002479 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky075b3bb2013-11-02 21:07:13 -07002480 intel_ring_emit(ring, 0); /* upper addr */
2481 intel_ring_emit(ring, 0); /* value */
2482 } else {
2483 intel_ring_emit(ring, 0);
2484 intel_ring_emit(ring, MI_NOOP);
2485 }
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002486 intel_ring_advance(ring);
Rodrigo Vivifd3da6c2013-06-06 16:58:16 -03002487
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00002488 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08002489}
2490
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002491int intel_init_render_ring_buffer(struct drm_device *dev)
2492{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002493 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002494 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Ben Widawsky3e789982014-06-30 09:53:37 -07002495 struct drm_i915_gem_object *obj;
2496 int ret;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002497
Daniel Vetter59465b52012-04-11 22:12:48 +02002498 ring->name = "render ring";
2499 ring->id = RCS;
2500 ring->mmio_base = RENDER_RING_BASE;
2501
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002502 if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002503 if (i915_semaphore_is_enabled(dev)) {
2504 obj = i915_gem_alloc_object(dev, 4096);
2505 if (obj == NULL) {
2506 DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
2507 i915.semaphores = 0;
2508 } else {
2509 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
2510 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
2511 if (ret != 0) {
2512 drm_gem_object_unreference(&obj->base);
2513 DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
2514 i915.semaphores = 0;
2515 } else
2516 dev_priv->semaphore_obj = obj;
2517 }
2518 }
Mika Kuoppala72253422014-10-07 17:21:26 +03002519
Daniel Vetter8f0e2b92014-12-02 16:19:07 +01002520 ring->init_context = intel_rcs_ctx_init;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002521 ring->add_request = gen6_add_request;
2522 ring->flush = gen8_render_ring_flush;
2523 ring->irq_get = gen8_ring_get_irq;
2524 ring->irq_put = gen8_ring_put_irq;
2525 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
2526 ring->get_seqno = gen6_ring_get_seqno;
2527 ring->set_seqno = ring_set_seqno;
2528 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky3e789982014-06-30 09:53:37 -07002529 WARN_ON(!dev_priv->semaphore_obj);
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002530 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002531 ring->semaphore.signal = gen8_rcs_signal;
2532 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002533 }
2534 } else if (INTEL_INFO(dev)->gen >= 6) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002535 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03002536 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01002537 if (INTEL_INFO(dev)->gen == 6)
Paulo Zanonib3111502012-08-17 18:35:42 -03002538 ring->flush = gen6_render_ring_flush;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002539 ring->irq_get = gen6_ring_get_irq;
2540 ring->irq_put = gen6_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002541 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01002542 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002543 ring->set_seqno = ring_set_seqno;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002544 if (i915_semaphore_is_enabled(dev)) {
2545 ring->semaphore.sync_to = gen6_ring_sync;
2546 ring->semaphore.signal = gen6_signal;
2547 /*
2548 * The current semaphore is only applied on pre-gen8
2549 * platform. And there is no VCS2 ring on the pre-gen8
2550 * platform. So the semaphore between RCS and VCS2 is
2551 * initialized as INVALID. Gen8 will initialize the
2552 * sema between VCS2 and RCS later.
2553 */
2554 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
2555 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
2556 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
2557 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
2558 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2559 ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
2560 ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
2561 ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
2562 ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
2563 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2564 }
Chris Wilsonc6df5412010-12-15 09:56:50 +00002565 } else if (IS_GEN5(dev)) {
2566 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002567 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00002568 ring->get_seqno = pc_render_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002569 ring->set_seqno = pc_render_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002570 ring->irq_get = gen5_ring_get_irq;
2571 ring->irq_put = gen5_ring_put_irq;
Ben Widawskycc609d52013-05-28 19:22:29 -07002572 ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
2573 GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02002574 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002575 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01002576 if (INTEL_INFO(dev)->gen < 4)
2577 ring->flush = gen2_render_ring_flush;
2578 else
2579 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02002580 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002581 ring->set_seqno = ring_set_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01002582 if (IS_GEN2(dev)) {
2583 ring->irq_get = i8xx_ring_get_irq;
2584 ring->irq_put = i8xx_ring_put_irq;
2585 } else {
2586 ring->irq_get = i9xx_ring_get_irq;
2587 ring->irq_put = i9xx_ring_put_irq;
2588 }
Daniel Vettere3670312012-04-11 22:12:53 +02002589 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002590 }
Daniel Vetter59465b52012-04-11 22:12:48 +02002591 ring->write_tail = ring_write_tail;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002592
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002593 if (IS_HASWELL(dev))
2594 ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002595 else if (IS_GEN8(dev))
2596 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01002597 else if (INTEL_INFO(dev)->gen >= 6)
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002598 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
2599 else if (INTEL_INFO(dev)->gen >= 4)
2600 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
2601 else if (IS_I830(dev) || IS_845G(dev))
2602 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
2603 else
2604 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002605 ring->init_hw = init_render_ring;
Daniel Vetter59465b52012-04-11 22:12:48 +02002606 ring->cleanup = render_ring_cleanup;
2607
Daniel Vetterb45305f2012-12-17 16:21:27 +01002608 /* Workaround batchbuffer to combat CS tlb bug. */
2609 if (HAS_BROKEN_CS_TLB(dev)) {
Chris Wilsonc4d69da2014-09-08 14:25:41 +01002610 obj = i915_gem_alloc_object(dev, I830_WA_SIZE);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002611 if (obj == NULL) {
2612 DRM_ERROR("Failed to allocate batch bo\n");
2613 return -ENOMEM;
2614 }
2615
Daniel Vetterbe1fa122014-02-14 14:01:14 +01002616 ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002617 if (ret != 0) {
2618 drm_gem_object_unreference(&obj->base);
2619 DRM_ERROR("Failed to ping batch bo\n");
2620 return ret;
2621 }
2622
Chris Wilson0d1aaca2013-08-26 20:58:11 +01002623 ring->scratch.obj = obj;
2624 ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
Daniel Vetterb45305f2012-12-17 16:21:27 +01002625 }
2626
Daniel Vetter99be1df2014-11-20 00:33:06 +01002627 ret = intel_init_ring_buffer(dev, ring);
2628 if (ret)
2629 return ret;
2630
2631 if (INTEL_INFO(dev)->gen >= 5) {
2632 ret = intel_init_pipe_control(ring);
2633 if (ret)
2634 return ret;
2635 }
2636
2637 return 0;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002638}
2639
2640int intel_init_bsd_ring_buffer(struct drm_device *dev)
2641{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002642 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002643 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002644
Daniel Vetter58fa3832012-04-11 22:12:49 +02002645 ring->name = "bsd ring";
2646 ring->id = VCS;
2647
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002648 ring->write_tail = ring_write_tail;
Ben Widawsky780f18c2013-11-02 21:07:28 -07002649 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter58fa3832012-04-11 22:12:49 +02002650 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02002651 /* gen6 bsd needs a special wa for tail updates */
2652 if (IS_GEN6(dev))
2653 ring->write_tail = gen6_bsd_ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002654 ring->flush = gen6_bsd_ring_flush;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002655 ring->add_request = gen6_add_request;
2656 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002657 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002658 if (INTEL_INFO(dev)->gen >= 8) {
2659 ring->irq_enable_mask =
2660 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
2661 ring->irq_get = gen8_ring_get_irq;
2662 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002663 ring->dispatch_execbuffer =
2664 gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002665 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002666 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002667 ring->semaphore.signal = gen8_xcs_signal;
2668 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002669 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002670 } else {
2671 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
2672 ring->irq_get = gen6_ring_get_irq;
2673 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002674 ring->dispatch_execbuffer =
2675 gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002676 if (i915_semaphore_is_enabled(dev)) {
2677 ring->semaphore.sync_to = gen6_ring_sync;
2678 ring->semaphore.signal = gen6_signal;
2679 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
2680 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
2681 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
2682 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
2683 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2684 ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
2685 ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
2686 ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
2687 ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
2688 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2689 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002690 }
Daniel Vetter58fa3832012-04-11 22:12:49 +02002691 } else {
2692 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002693 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02002694 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002695 ring->get_seqno = ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002696 ring->set_seqno = ring_set_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02002697 if (IS_GEN5(dev)) {
Ben Widawskycc609d52013-05-28 19:22:29 -07002698 ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002699 ring->irq_get = gen5_ring_get_irq;
2700 ring->irq_put = gen5_ring_put_irq;
2701 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02002702 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02002703 ring->irq_get = i9xx_ring_get_irq;
2704 ring->irq_put = i9xx_ring_put_irq;
2705 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02002706 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002707 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002708 ring->init_hw = init_ring_common;
Daniel Vetter58fa3832012-04-11 22:12:49 +02002709
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002710 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08002711}
Chris Wilson549f7362010-10-19 11:19:32 +01002712
Zhao Yakui845f74a2014-04-17 10:37:37 +08002713/**
Damien Lespiau62659922015-01-29 14:13:40 +00002714 * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
Zhao Yakui845f74a2014-04-17 10:37:37 +08002715 */
2716int intel_init_bsd2_ring_buffer(struct drm_device *dev)
2717{
2718 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002719 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
Zhao Yakui845f74a2014-04-17 10:37:37 +08002720
Rodrigo Vivif7b64232014-07-01 02:41:36 -07002721 ring->name = "bsd2 ring";
Zhao Yakui845f74a2014-04-17 10:37:37 +08002722 ring->id = VCS2;
2723
2724 ring->write_tail = ring_write_tail;
2725 ring->mmio_base = GEN8_BSD2_RING_BASE;
2726 ring->flush = gen6_bsd_ring_flush;
2727 ring->add_request = gen6_add_request;
2728 ring->get_seqno = gen6_ring_get_seqno;
2729 ring->set_seqno = ring_set_seqno;
2730 ring->irq_enable_mask =
2731 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
2732 ring->irq_get = gen8_ring_get_irq;
2733 ring->irq_put = gen8_ring_put_irq;
2734 ring->dispatch_execbuffer =
2735 gen8_ring_dispatch_execbuffer;
Ben Widawsky3e789982014-06-30 09:53:37 -07002736 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002737 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002738 ring->semaphore.signal = gen8_xcs_signal;
2739 GEN8_RING_SEMAPHORE_INIT;
2740 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002741 ring->init_hw = init_ring_common;
Zhao Yakui845f74a2014-04-17 10:37:37 +08002742
2743 return intel_init_ring_buffer(dev, ring);
2744}
2745
Chris Wilson549f7362010-10-19 11:19:32 +01002746int intel_init_blt_ring_buffer(struct drm_device *dev)
2747{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002748 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002749 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01002750
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002751 ring->name = "blitter ring";
2752 ring->id = BCS;
2753
2754 ring->mmio_base = BLT_RING_BASE;
2755 ring->write_tail = ring_write_tail;
Ben Widawskyea251322013-05-28 19:22:21 -07002756 ring->flush = gen6_ring_flush;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02002757 ring->add_request = gen6_add_request;
2758 ring->get_seqno = gen6_ring_get_seqno;
Mika Kuoppalab70ec5b2012-12-19 11:13:05 +02002759 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002760 if (INTEL_INFO(dev)->gen >= 8) {
2761 ring->irq_enable_mask =
2762 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
2763 ring->irq_get = gen8_ring_get_irq;
2764 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002765 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002766 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002767 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002768 ring->semaphore.signal = gen8_xcs_signal;
2769 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002770 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002771 } else {
2772 ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
2773 ring->irq_get = gen6_ring_get_irq;
2774 ring->irq_put = gen6_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002775 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002776 if (i915_semaphore_is_enabled(dev)) {
2777 ring->semaphore.signal = gen6_signal;
2778 ring->semaphore.sync_to = gen6_ring_sync;
2779 /*
2780 * The current semaphore is only applied on pre-gen8
2781 * platform. And there is no VCS2 ring on the pre-gen8
2782 * platform. So the semaphore between BCS and VCS2 is
2783 * initialized as INVALID. Gen8 will initialize the
2784 * sema between BCS and VCS2 later.
2785 */
2786 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
2787 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
2788 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
2789 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
2790 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2791 ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
2792 ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
2793 ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
2794 ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
2795 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2796 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002797 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002798 ring->init_hw = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01002799
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002800 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01002801}
Chris Wilsona7b97612012-07-20 12:41:08 +01002802
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002803int intel_init_vebox_ring_buffer(struct drm_device *dev)
2804{
Jani Nikula4640c4f2014-03-31 14:27:19 +03002805 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002806 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002807
2808 ring->name = "video enhancement ring";
2809 ring->id = VECS;
2810
2811 ring->mmio_base = VEBOX_RING_BASE;
2812 ring->write_tail = ring_write_tail;
2813 ring->flush = gen6_ring_flush;
2814 ring->add_request = gen6_add_request;
2815 ring->get_seqno = gen6_ring_get_seqno;
2816 ring->set_seqno = ring_set_seqno;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002817
2818 if (INTEL_INFO(dev)->gen >= 8) {
2819 ring->irq_enable_mask =
Daniel Vetter40c499f2013-11-07 21:40:39 -08002820 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Ben Widawskyabd58f02013-11-02 21:07:09 -07002821 ring->irq_get = gen8_ring_get_irq;
2822 ring->irq_put = gen8_ring_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002823 ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002824 if (i915_semaphore_is_enabled(dev)) {
Ben Widawsky5ee426c2014-06-30 09:53:38 -07002825 ring->semaphore.sync_to = gen8_ring_sync;
Ben Widawsky3e789982014-06-30 09:53:37 -07002826 ring->semaphore.signal = gen8_xcs_signal;
2827 GEN8_RING_SEMAPHORE_INIT;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002828 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002829 } else {
2830 ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
2831 ring->irq_get = hsw_vebox_get_irq;
2832 ring->irq_put = hsw_vebox_put_irq;
Ben Widawsky1c7a0622013-11-02 21:07:12 -07002833 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Ben Widawsky707d9cf2014-06-30 09:53:36 -07002834 if (i915_semaphore_is_enabled(dev)) {
2835 ring->semaphore.sync_to = gen6_ring_sync;
2836 ring->semaphore.signal = gen6_signal;
2837 ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
2838 ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
2839 ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
2840 ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
2841 ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
2842 ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
2843 ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
2844 ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
2845 ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
2846 ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
2847 }
Ben Widawskyabd58f02013-11-02 21:07:09 -07002848 }
Daniel Vetterecfe00d2014-11-20 00:33:04 +01002849 ring->init_hw = init_ring_common;
Ben Widawsky9a8a2212013-05-28 19:22:23 -07002850
2851 return intel_init_ring_buffer(dev, ring);
2852}
2853
Chris Wilsona7b97612012-07-20 12:41:08 +01002854int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002855intel_ring_flush_all_caches(struct intel_engine_cs *ring)
Chris Wilsona7b97612012-07-20 12:41:08 +01002856{
2857 int ret;
2858
2859 if (!ring->gpu_caches_dirty)
2860 return 0;
2861
2862 ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
2863 if (ret)
2864 return ret;
2865
2866 trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
2867
2868 ring->gpu_caches_dirty = false;
2869 return 0;
2870}
2871
2872int
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002873intel_ring_invalidate_all_caches(struct intel_engine_cs *ring)
Chris Wilsona7b97612012-07-20 12:41:08 +01002874{
2875 uint32_t flush_domains;
2876 int ret;
2877
2878 flush_domains = 0;
2879 if (ring->gpu_caches_dirty)
2880 flush_domains = I915_GEM_GPU_DOMAINS;
2881
2882 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2883 if (ret)
2884 return ret;
2885
2886 trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
2887
2888 ring->gpu_caches_dirty = false;
2889 return 0;
2890}
Chris Wilsone3efda42014-04-09 09:19:41 +01002891
2892void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002893intel_stop_ring_buffer(struct intel_engine_cs *ring)
Chris Wilsone3efda42014-04-09 09:19:41 +01002894{
2895 int ret;
2896
2897 if (!intel_ring_initialized(ring))
2898 return;
2899
2900 ret = intel_ring_idle(ring);
2901 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
2902 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
2903 ring->name, ret);
2904
2905 stop_ring(ring);
2906}