blob: 074b7d67c1c440ae038d601d467f45a69729afa1 [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
30#include "drmP.h"
31#include "drm.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070032#include "i915_drv.h"
Zou Nan hai8187a2b2010-05-21 09:08:55 +080033#include "i915_drm.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070034#include "i915_trace.h"
Xiang, Haihao881f47b2010-09-19 14:40:43 +010035#include "intel_drv.h"
Eric Anholt62fdfea2010-05-21 13:26:39 -070036
Jesse Barnes8d315282011-10-16 10:23:31 +020037/*
38 * 965+ support PIPE_CONTROL commands, which provide finer grained control
39 * over cache flushing.
40 */
41struct pipe_control {
42 struct drm_i915_gem_object *obj;
43 volatile u32 *cpu_page;
44 u32 gtt_offset;
45};
46
Chris Wilsonc7dca472011-01-20 17:00:10 +000047static inline int ring_space(struct intel_ring_buffer *ring)
48{
49 int space = (ring->head & HEAD_ADDR) - (ring->tail + 8);
50 if (space < 0)
51 space += ring->size;
52 return space;
53}
54
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000055static int
Chris Wilson46f0f8d2012-04-18 11:12:11 +010056gen2_render_ring_flush(struct intel_ring_buffer *ring,
57 u32 invalidate_domains,
58 u32 flush_domains)
59{
60 u32 cmd;
61 int ret;
62
63 cmd = MI_FLUSH;
Daniel Vetter31b14c92012-04-19 16:45:22 +020064 if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
Chris Wilson46f0f8d2012-04-18 11:12:11 +010065 cmd |= MI_NO_WRITE_FLUSH;
66
67 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
68 cmd |= MI_READ_FLUSH;
69
70 ret = intel_ring_begin(ring, 2);
71 if (ret)
72 return ret;
73
74 intel_ring_emit(ring, cmd);
75 intel_ring_emit(ring, MI_NOOP);
76 intel_ring_advance(ring);
77
78 return 0;
79}
80
81static int
82gen4_render_ring_flush(struct intel_ring_buffer *ring,
83 u32 invalidate_domains,
84 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070085{
Chris Wilson78501ea2010-10-27 12:18:21 +010086 struct drm_device *dev = ring->dev;
Chris Wilson6f392d52010-08-07 11:01:22 +010087 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000088 int ret;
Chris Wilson6f392d52010-08-07 11:01:22 +010089
Chris Wilson36d527d2011-03-19 22:26:49 +000090 /*
91 * read/write caches:
92 *
93 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
94 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
95 * also flushed at 2d versus 3d pipeline switches.
96 *
97 * read-only caches:
98 *
99 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
100 * MI_READ_FLUSH is set, and is always flushed on 965.
101 *
102 * I915_GEM_DOMAIN_COMMAND may not exist?
103 *
104 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
105 * invalidated when MI_EXE_FLUSH is set.
106 *
107 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
108 * invalidated with every MI_FLUSH.
109 *
110 * TLBs:
111 *
112 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
113 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
114 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
115 * are flushed at any MI_FLUSH.
116 */
117
118 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
Chris Wilson46f0f8d2012-04-18 11:12:11 +0100119 if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
Chris Wilson36d527d2011-03-19 22:26:49 +0000120 cmd &= ~MI_NO_WRITE_FLUSH;
Chris Wilson36d527d2011-03-19 22:26:49 +0000121 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
122 cmd |= MI_EXE_FLUSH;
123
124 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
125 (IS_G4X(dev) || IS_GEN5(dev)))
126 cmd |= MI_INVALIDATE_ISP;
127
128 ret = intel_ring_begin(ring, 2);
129 if (ret)
130 return ret;
131
132 intel_ring_emit(ring, cmd);
133 intel_ring_emit(ring, MI_NOOP);
134 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000135
136 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800137}
138
Jesse Barnes8d315282011-10-16 10:23:31 +0200139/**
140 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
141 * implementing two workarounds on gen6. From section 1.4.7.1
142 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
143 *
144 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
145 * produced by non-pipelined state commands), software needs to first
146 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
147 * 0.
148 *
149 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
150 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
151 *
152 * And the workaround for these two requires this workaround first:
153 *
154 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
155 * BEFORE the pipe-control with a post-sync op and no write-cache
156 * flushes.
157 *
158 * And this last workaround is tricky because of the requirements on
159 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
160 * volume 2 part 1:
161 *
162 * "1 of the following must also be set:
163 * - Render Target Cache Flush Enable ([12] of DW1)
164 * - Depth Cache Flush Enable ([0] of DW1)
165 * - Stall at Pixel Scoreboard ([1] of DW1)
166 * - Depth Stall ([13] of DW1)
167 * - Post-Sync Operation ([13] of DW1)
168 * - Notify Enable ([8] of DW1)"
169 *
170 * The cache flushes require the workaround flush that triggered this
171 * one, so we can't use it. Depth stall would trigger the same.
172 * Post-sync nonzero is what triggered this second workaround, so we
173 * can't use that one either. Notify enable is IRQs, which aren't
174 * really our business. That leaves only stall at scoreboard.
175 */
176static int
177intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
178{
179 struct pipe_control *pc = ring->private;
180 u32 scratch_addr = pc->gtt_offset + 128;
181 int ret;
182
183
184 ret = intel_ring_begin(ring, 6);
185 if (ret)
186 return ret;
187
188 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
189 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
190 PIPE_CONTROL_STALL_AT_SCOREBOARD);
191 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
192 intel_ring_emit(ring, 0); /* low dword */
193 intel_ring_emit(ring, 0); /* high dword */
194 intel_ring_emit(ring, MI_NOOP);
195 intel_ring_advance(ring);
196
197 ret = intel_ring_begin(ring, 6);
198 if (ret)
199 return ret;
200
201 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
202 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
203 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
204 intel_ring_emit(ring, 0);
205 intel_ring_emit(ring, 0);
206 intel_ring_emit(ring, MI_NOOP);
207 intel_ring_advance(ring);
208
209 return 0;
210}
211
212static int
213gen6_render_ring_flush(struct intel_ring_buffer *ring,
214 u32 invalidate_domains, u32 flush_domains)
215{
216 u32 flags = 0;
217 struct pipe_control *pc = ring->private;
218 u32 scratch_addr = pc->gtt_offset + 128;
219 int ret;
220
Jesse Barnes8d315282011-10-16 10:23:31 +0200221 /* Just flush everything. Experiments have shown that reducing the
222 * number of bits based on the write domains has little performance
223 * impact.
224 */
Chris Wilson7d54a902012-08-10 10:18:10 +0100225 if (flush_domains) {
226 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
227 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
228 /*
229 * Ensure that any following seqno writes only happen
230 * when the render cache is indeed flushed.
231 */
Daniel Vetter97f209b2012-06-28 09:48:42 +0200232 flags |= PIPE_CONTROL_CS_STALL;
Chris Wilson7d54a902012-08-10 10:18:10 +0100233 }
234 if (invalidate_domains) {
235 flags |= PIPE_CONTROL_TLB_INVALIDATE;
236 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
237 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
238 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
239 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
240 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
241 /*
242 * TLB invalidate requires a post-sync write.
243 */
244 flags |= PIPE_CONTROL_QW_WRITE;
245 }
Jesse Barnes8d315282011-10-16 10:23:31 +0200246
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100247 ret = intel_ring_begin(ring, 4);
Jesse Barnes8d315282011-10-16 10:23:31 +0200248 if (ret)
249 return ret;
250
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100251 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
Jesse Barnes8d315282011-10-16 10:23:31 +0200252 intel_ring_emit(ring, flags);
253 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100254 intel_ring_emit(ring, 0);
Jesse Barnes8d315282011-10-16 10:23:31 +0200255 intel_ring_advance(ring);
256
257 return 0;
258}
259
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100260static int
Paulo Zanoni4772eae2012-08-17 18:35:41 -0300261gen7_render_ring_flush(struct intel_ring_buffer *ring,
262 u32 invalidate_domains, u32 flush_domains)
263{
264 u32 flags = 0;
265 struct pipe_control *pc = ring->private;
266 u32 scratch_addr = pc->gtt_offset + 128;
267 int ret;
268
269 /* Just flush everything. Experiments have shown that reducing the
270 * number of bits based on the write domains has little performance
271 * impact.
272 */
273 if (flush_domains) {
274 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
275 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
276 /*
277 * Ensure that any following seqno writes only happen
278 * when the render cache is indeed flushed.
279 */
280 flags |= PIPE_CONTROL_CS_STALL;
281 }
282 if (invalidate_domains) {
283 flags |= PIPE_CONTROL_TLB_INVALIDATE;
284 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
285 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
286 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
287 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
288 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
289 /*
290 * TLB invalidate requires a post-sync write.
291 */
292 flags |= PIPE_CONTROL_QW_WRITE;
293 }
294
295 ret = intel_ring_begin(ring, 4);
296 if (ret)
297 return ret;
298
299 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
300 intel_ring_emit(ring, flags);
301 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
302 intel_ring_emit(ring, 0);
303 intel_ring_advance(ring);
304
305 return 0;
306}
307
308static int
Chris Wilson6c6cf5a2012-07-20 18:02:28 +0100309gen6_render_ring_flush__wa(struct intel_ring_buffer *ring,
310 u32 invalidate_domains, u32 flush_domains)
311{
312 int ret;
313
314 /* Force SNB workarounds for PIPE_CONTROL flushes */
315 ret = intel_emit_post_sync_nonzero_flush(ring);
316 if (ret)
317 return ret;
318
319 return gen6_render_ring_flush(ring, invalidate_domains, flush_domains);
320}
321
Chris Wilson78501ea2010-10-27 12:18:21 +0100322static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100323 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800324{
Chris Wilson78501ea2010-10-27 12:18:21 +0100325 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100326 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800327}
328
Chris Wilson78501ea2010-10-27 12:18:21 +0100329u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800330{
Chris Wilson78501ea2010-10-27 12:18:21 +0100331 drm_i915_private_t *dev_priv = ring->dev->dev_private;
332 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200333 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800334
335 return I915_READ(acthd_reg);
336}
337
Chris Wilson78501ea2010-10-27 12:18:21 +0100338static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800339{
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200340 struct drm_device *dev = ring->dev;
341 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000342 struct drm_i915_gem_object *obj = ring->obj;
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200343 int ret = 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800344 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800345
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200346 if (HAS_FORCE_WAKE(dev))
347 gen6_gt_force_wake_get(dev_priv);
348
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800349 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200350 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200351 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100352 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800353
Daniel Vetter570ef602010-08-02 17:06:23 +0200354 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800355
356 /* G45 ring initialization fails to reset head to zero */
357 if (head != 0) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000358 DRM_DEBUG_KMS("%s head not reset to zero "
359 "ctl %08x head %08x tail %08x start %08x\n",
360 ring->name,
361 I915_READ_CTL(ring),
362 I915_READ_HEAD(ring),
363 I915_READ_TAIL(ring),
364 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800365
Daniel Vetter570ef602010-08-02 17:06:23 +0200366 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800367
Chris Wilson6fd0d562010-12-05 20:42:33 +0000368 if (I915_READ_HEAD(ring) & HEAD_ADDR) {
369 DRM_ERROR("failed to set %s head to zero "
370 "ctl %08x head %08x tail %08x start %08x\n",
371 ring->name,
372 I915_READ_CTL(ring),
373 I915_READ_HEAD(ring),
374 I915_READ_TAIL(ring),
375 I915_READ_START(ring));
376 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700377 }
378
Daniel Vetter0d8957c2012-08-07 09:54:14 +0200379 /* Initialize the ring. This must happen _after_ we've cleared the ring
380 * registers with the above sequence (the readback of the HEAD registers
381 * also enforces ordering), otherwise the hw might lose the new ring
382 * register values. */
383 I915_WRITE_START(ring, obj->gtt_offset);
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200384 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000385 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000386 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800387
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800388 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400389 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
390 I915_READ_START(ring) == obj->gtt_offset &&
391 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000392 DRM_ERROR("%s initialization failed "
393 "ctl %08x head %08x tail %08x start %08x\n",
394 ring->name,
395 I915_READ_CTL(ring),
396 I915_READ_HEAD(ring),
397 I915_READ_TAIL(ring),
398 I915_READ_START(ring));
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200399 ret = -EIO;
400 goto out;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800401 }
402
Chris Wilson78501ea2010-10-27 12:18:21 +0100403 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
404 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800405 else {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000406 ring->head = I915_READ_HEAD(ring);
Daniel Vetter870e86d2010-08-02 16:29:44 +0200407 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000408 ring->space = ring_space(ring);
Chris Wilsonc3b20032012-05-28 22:33:02 +0100409 ring->last_retired_head = -1;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800410 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000411
Daniel Vetterb7884eb2012-06-04 11:18:15 +0200412out:
413 if (HAS_FORCE_WAKE(dev))
414 gen6_gt_force_wake_put(dev_priv);
415
416 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700417}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800418
Chris Wilsonc6df5412010-12-15 09:56:50 +0000419static int
420init_pipe_control(struct intel_ring_buffer *ring)
421{
422 struct pipe_control *pc;
423 struct drm_i915_gem_object *obj;
424 int ret;
425
426 if (ring->private)
427 return 0;
428
429 pc = kmalloc(sizeof(*pc), GFP_KERNEL);
430 if (!pc)
431 return -ENOMEM;
432
433 obj = i915_gem_alloc_object(ring->dev, 4096);
434 if (obj == NULL) {
435 DRM_ERROR("Failed to allocate seqno page\n");
436 ret = -ENOMEM;
437 goto err;
438 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100439
440 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000441
Chris Wilson86a1ee22012-08-11 15:41:04 +0100442 ret = i915_gem_object_pin(obj, 4096, true, false);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000443 if (ret)
444 goto err_unref;
445
446 pc->gtt_offset = obj->gtt_offset;
447 pc->cpu_page = kmap(obj->pages[0]);
448 if (pc->cpu_page == NULL)
449 goto err_unpin;
450
451 pc->obj = obj;
452 ring->private = pc;
453 return 0;
454
455err_unpin:
456 i915_gem_object_unpin(obj);
457err_unref:
458 drm_gem_object_unreference(&obj->base);
459err:
460 kfree(pc);
461 return ret;
462}
463
464static void
465cleanup_pipe_control(struct intel_ring_buffer *ring)
466{
467 struct pipe_control *pc = ring->private;
468 struct drm_i915_gem_object *obj;
469
470 if (!ring->private)
471 return;
472
473 obj = pc->obj;
474 kunmap(obj->pages[0]);
475 i915_gem_object_unpin(obj);
476 drm_gem_object_unreference(&obj->base);
477
478 kfree(pc);
479 ring->private = NULL;
480}
481
Chris Wilson78501ea2010-10-27 12:18:21 +0100482static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800483{
Chris Wilson78501ea2010-10-27 12:18:21 +0100484 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000485 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100486 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800487
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100488 if (INTEL_INFO(dev)->gen > 3) {
Daniel Vetter6b26c862012-04-24 14:04:12 +0200489 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
Jesse Barnesb095cd02011-08-12 15:28:32 -0700490 if (IS_GEN7(dev))
491 I915_WRITE(GFX_MODE_GEN7,
Daniel Vetter6b26c862012-04-24 14:04:12 +0200492 _MASKED_BIT_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
493 _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800494 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100495
Jesse Barnes8d315282011-10-16 10:23:31 +0200496 if (INTEL_INFO(dev)->gen >= 5) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000497 ret = init_pipe_control(ring);
498 if (ret)
499 return ret;
500 }
501
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200502 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700503 /* From the Sandybridge PRM, volume 1 part 3, page 24:
504 * "If this bit is set, STCunit will have LRA as replacement
505 * policy. [...] This bit must be reset. LRA replacement
506 * policy is not supported."
507 */
508 I915_WRITE(CACHE_MODE_0,
Daniel Vetter5e13a0c2012-05-08 13:39:59 +0200509 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Ben Widawsky12b02862012-06-04 14:42:50 -0700510
511 /* This is not explicitly set for GEN6, so read the register.
512 * see intel_ring_mi_set_context() for why we care.
513 * TODO: consider explicitly setting the bit for GEN5
514 */
515 ring->itlb_before_ctx_switch =
516 !!(I915_READ(GFX_MODE) & GFX_TLB_INVALIDATE_ALWAYS);
Ben Widawsky84f9f932011-12-12 19:21:58 -0800517 }
518
Daniel Vetter6b26c862012-04-24 14:04:12 +0200519 if (INTEL_INFO(dev)->gen >= 6)
520 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
Chris Wilsonc6df5412010-12-15 09:56:50 +0000521
Ben Widawskye1ef7cc2012-07-24 20:47:31 -0700522 if (HAS_L3_GPU_CACHE(dev))
Ben Widawsky15b9f802012-05-25 16:56:23 -0700523 I915_WRITE_IMR(ring, ~GEN6_RENDER_L3_PARITY_ERROR);
524
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800525 return ret;
526}
527
Chris Wilsonc6df5412010-12-15 09:56:50 +0000528static void render_ring_cleanup(struct intel_ring_buffer *ring)
529{
530 if (!ring->private)
531 return;
532
533 cleanup_pipe_control(ring);
534}
535
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000536static void
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700537update_mboxes(struct intel_ring_buffer *ring,
538 u32 seqno,
539 u32 mmio_offset)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000540{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700541 intel_ring_emit(ring, MI_SEMAPHORE_MBOX |
542 MI_SEMAPHORE_GLOBAL_GTT |
543 MI_SEMAPHORE_REGISTER |
544 MI_SEMAPHORE_UPDATE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000545 intel_ring_emit(ring, seqno);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700546 intel_ring_emit(ring, mmio_offset);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000547}
548
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700549/**
550 * gen6_add_request - Update the semaphore mailbox registers
551 *
552 * @ring - ring that is adding a request
553 * @seqno - return seqno stuck into the ring
554 *
555 * Update the mailbox registers in the *other* rings with the current seqno.
556 * This acts like a signal in the canonical semaphore.
557 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000558static int
559gen6_add_request(struct intel_ring_buffer *ring,
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700560 u32 *seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000561{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700562 u32 mbox1_reg;
563 u32 mbox2_reg;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000564 int ret;
565
566 ret = intel_ring_begin(ring, 10);
567 if (ret)
568 return ret;
569
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700570 mbox1_reg = ring->signal_mbox[0];
571 mbox2_reg = ring->signal_mbox[1];
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000572
Daniel Vetter53d227f2012-01-25 16:32:49 +0100573 *seqno = i915_gem_next_request_seqno(ring);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700574
575 update_mboxes(ring, *seqno, mbox1_reg);
576 update_mboxes(ring, *seqno, mbox2_reg);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000577 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
578 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700579 intel_ring_emit(ring, *seqno);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000580 intel_ring_emit(ring, MI_USER_INTERRUPT);
581 intel_ring_advance(ring);
582
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000583 return 0;
584}
585
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700586/**
587 * intel_ring_sync - sync the waiter to the signaller on seqno
588 *
589 * @waiter - ring that is waiting
590 * @signaller - ring which has, or will signal
591 * @seqno - seqno which the waiter will block on
592 */
593static int
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200594gen6_ring_sync(struct intel_ring_buffer *waiter,
595 struct intel_ring_buffer *signaller,
596 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000597{
598 int ret;
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700599 u32 dw1 = MI_SEMAPHORE_MBOX |
600 MI_SEMAPHORE_COMPARE |
601 MI_SEMAPHORE_REGISTER;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000602
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700603 /* Throughout all of the GEM code, seqno passed implies our current
604 * seqno is >= the last seqno executed. However for hardware the
605 * comparison is strictly greater than.
606 */
607 seqno -= 1;
608
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200609 WARN_ON(signaller->semaphore_register[waiter->id] ==
610 MI_SEMAPHORE_SYNC_INVALID);
611
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700612 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000613 if (ret)
614 return ret;
615
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200616 intel_ring_emit(waiter,
617 dw1 | signaller->semaphore_register[waiter->id]);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700618 intel_ring_emit(waiter, seqno);
619 intel_ring_emit(waiter, 0);
620 intel_ring_emit(waiter, MI_NOOP);
621 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000622
623 return 0;
624}
625
Chris Wilsonc6df5412010-12-15 09:56:50 +0000626#define PIPE_CONTROL_FLUSH(ring__, addr__) \
627do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200628 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
629 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000630 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
631 intel_ring_emit(ring__, 0); \
632 intel_ring_emit(ring__, 0); \
633} while (0)
634
635static int
636pc_render_add_request(struct intel_ring_buffer *ring,
637 u32 *result)
638{
Daniel Vetter53d227f2012-01-25 16:32:49 +0100639 u32 seqno = i915_gem_next_request_seqno(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000640 struct pipe_control *pc = ring->private;
641 u32 scratch_addr = pc->gtt_offset + 128;
642 int ret;
643
644 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
645 * incoherent with writes to memory, i.e. completely fubar,
646 * so we need to use PIPE_NOTIFY instead.
647 *
648 * However, we also need to workaround the qword write
649 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
650 * memory before requesting an interrupt.
651 */
652 ret = intel_ring_begin(ring, 32);
653 if (ret)
654 return ret;
655
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200656 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200657 PIPE_CONTROL_WRITE_FLUSH |
658 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000659 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
660 intel_ring_emit(ring, seqno);
661 intel_ring_emit(ring, 0);
662 PIPE_CONTROL_FLUSH(ring, scratch_addr);
663 scratch_addr += 128; /* write to separate cachelines */
664 PIPE_CONTROL_FLUSH(ring, scratch_addr);
665 scratch_addr += 128;
666 PIPE_CONTROL_FLUSH(ring, scratch_addr);
667 scratch_addr += 128;
668 PIPE_CONTROL_FLUSH(ring, scratch_addr);
669 scratch_addr += 128;
670 PIPE_CONTROL_FLUSH(ring, scratch_addr);
671 scratch_addr += 128;
672 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +0000673
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200674 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200675 PIPE_CONTROL_WRITE_FLUSH |
676 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000677 PIPE_CONTROL_NOTIFY);
678 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
679 intel_ring_emit(ring, seqno);
680 intel_ring_emit(ring, 0);
681 intel_ring_advance(ring);
682
683 *result = seqno;
684 return 0;
685}
686
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800687static u32
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100688gen6_ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100689{
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100690 /* Workaround to force correct ordering between irq and seqno writes on
691 * ivb (and maybe also on snb) by reading from a CS register (like
692 * ACTHD) before reading the status page. */
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100693 if (!lazy_coherency)
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100694 intel_ring_get_active_head(ring);
695 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
696}
697
698static u32
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100699ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800700{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000701 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
702}
703
Chris Wilsonc6df5412010-12-15 09:56:50 +0000704static u32
Chris Wilsonb2eadbc2012-08-09 10:58:30 +0100705pc_render_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
Chris Wilsonc6df5412010-12-15 09:56:50 +0000706{
707 struct pipe_control *pc = ring->private;
708 return pc->cpu_page[0];
709}
710
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000711static bool
Daniel Vettere48d8632012-04-11 22:12:54 +0200712gen5_ring_get_irq(struct intel_ring_buffer *ring)
713{
714 struct drm_device *dev = ring->dev;
715 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100716 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +0200717
718 if (!dev->irq_enabled)
719 return false;
720
Chris Wilson7338aef2012-04-24 21:48:47 +0100721 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterf637fde2012-04-11 22:12:59 +0200722 if (ring->irq_refcount++ == 0) {
723 dev_priv->gt_irq_mask &= ~ring->irq_enable_mask;
724 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
725 POSTING_READ(GTIMR);
726 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100727 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +0200728
729 return true;
730}
731
732static void
733gen5_ring_put_irq(struct intel_ring_buffer *ring)
734{
735 struct drm_device *dev = ring->dev;
736 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100737 unsigned long flags;
Daniel Vettere48d8632012-04-11 22:12:54 +0200738
Chris Wilson7338aef2012-04-24 21:48:47 +0100739 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterf637fde2012-04-11 22:12:59 +0200740 if (--ring->irq_refcount == 0) {
741 dev_priv->gt_irq_mask |= ring->irq_enable_mask;
742 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
743 POSTING_READ(GTIMR);
744 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100745 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vettere48d8632012-04-11 22:12:54 +0200746}
747
748static bool
Daniel Vettere3670312012-04-11 22:12:53 +0200749i9xx_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700750{
Chris Wilson78501ea2010-10-27 12:18:21 +0100751 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000752 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100753 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700754
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000755 if (!dev->irq_enabled)
756 return false;
757
Chris Wilson7338aef2012-04-24 21:48:47 +0100758 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterf637fde2012-04-11 22:12:59 +0200759 if (ring->irq_refcount++ == 0) {
760 dev_priv->irq_mask &= ~ring->irq_enable_mask;
761 I915_WRITE(IMR, dev_priv->irq_mask);
762 POSTING_READ(IMR);
763 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100764 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000765
766 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700767}
768
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800769static void
Daniel Vettere3670312012-04-11 22:12:53 +0200770i9xx_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700771{
Chris Wilson78501ea2010-10-27 12:18:21 +0100772 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000773 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100774 unsigned long flags;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700775
Chris Wilson7338aef2012-04-24 21:48:47 +0100776 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Daniel Vetterf637fde2012-04-11 22:12:59 +0200777 if (--ring->irq_refcount == 0) {
778 dev_priv->irq_mask |= ring->irq_enable_mask;
779 I915_WRITE(IMR, dev_priv->irq_mask);
780 POSTING_READ(IMR);
781 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100782 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700783}
784
Chris Wilsonc2798b12012-04-22 21:13:57 +0100785static bool
786i8xx_ring_get_irq(struct intel_ring_buffer *ring)
787{
788 struct drm_device *dev = ring->dev;
789 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100790 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100791
792 if (!dev->irq_enabled)
793 return false;
794
Chris Wilson7338aef2012-04-24 21:48:47 +0100795 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100796 if (ring->irq_refcount++ == 0) {
797 dev_priv->irq_mask &= ~ring->irq_enable_mask;
798 I915_WRITE16(IMR, dev_priv->irq_mask);
799 POSTING_READ16(IMR);
800 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100801 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100802
803 return true;
804}
805
806static void
807i8xx_ring_put_irq(struct intel_ring_buffer *ring)
808{
809 struct drm_device *dev = ring->dev;
810 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100811 unsigned long flags;
Chris Wilsonc2798b12012-04-22 21:13:57 +0100812
Chris Wilson7338aef2012-04-24 21:48:47 +0100813 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100814 if (--ring->irq_refcount == 0) {
815 dev_priv->irq_mask |= ring->irq_enable_mask;
816 I915_WRITE16(IMR, dev_priv->irq_mask);
817 POSTING_READ16(IMR);
818 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100819 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilsonc2798b12012-04-22 21:13:57 +0100820}
821
Chris Wilson78501ea2010-10-27 12:18:21 +0100822void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800823{
Eric Anholt45930102011-05-06 17:12:35 -0700824 struct drm_device *dev = ring->dev;
Chris Wilson78501ea2010-10-27 12:18:21 +0100825 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700826 u32 mmio = 0;
827
828 /* The ring status page addresses are no longer next to the rest of
829 * the ring registers as of gen7.
830 */
831 if (IS_GEN7(dev)) {
832 switch (ring->id) {
Daniel Vetter96154f22011-12-14 13:57:00 +0100833 case RCS:
Eric Anholt45930102011-05-06 17:12:35 -0700834 mmio = RENDER_HWS_PGA_GEN7;
835 break;
Daniel Vetter96154f22011-12-14 13:57:00 +0100836 case BCS:
Eric Anholt45930102011-05-06 17:12:35 -0700837 mmio = BLT_HWS_PGA_GEN7;
838 break;
Daniel Vetter96154f22011-12-14 13:57:00 +0100839 case VCS:
Eric Anholt45930102011-05-06 17:12:35 -0700840 mmio = BSD_HWS_PGA_GEN7;
841 break;
842 }
843 } else if (IS_GEN6(ring->dev)) {
844 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
845 } else {
846 mmio = RING_HWS_PGA(ring->mmio_base);
847 }
848
Chris Wilson78501ea2010-10-27 12:18:21 +0100849 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
850 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800851}
852
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000853static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100854bsd_ring_flush(struct intel_ring_buffer *ring,
855 u32 invalidate_domains,
856 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800857{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000858 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000859
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000860 ret = intel_ring_begin(ring, 2);
861 if (ret)
862 return ret;
863
864 intel_ring_emit(ring, MI_FLUSH);
865 intel_ring_emit(ring, MI_NOOP);
866 intel_ring_advance(ring);
867 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800868}
869
Chris Wilson3cce4692010-10-27 16:11:02 +0100870static int
Daniel Vetter8620a3a2012-04-11 22:12:57 +0200871i9xx_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100872 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800873{
874 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100875 int ret;
876
877 ret = intel_ring_begin(ring, 4);
878 if (ret)
879 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100880
Daniel Vetter53d227f2012-01-25 16:32:49 +0100881 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson6f392d52010-08-07 11:01:22 +0100882
Chris Wilson3cce4692010-10-27 16:11:02 +0100883 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
884 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
885 intel_ring_emit(ring, seqno);
886 intel_ring_emit(ring, MI_USER_INTERRUPT);
887 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800888
Chris Wilson3cce4692010-10-27 16:11:02 +0100889 *result = seqno;
890 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800891}
892
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000893static bool
Ben Widawsky25c06302012-03-29 19:11:27 -0700894gen6_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson0f468322011-01-04 17:35:21 +0000895{
896 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000897 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100898 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +0000899
900 if (!dev->irq_enabled)
901 return false;
902
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100903 /* It looks like we need to prevent the gt from suspending while waiting
904 * for an notifiy irq, otherwise irqs seem to get lost on at least the
905 * blt/bsd rings on ivb. */
Daniel Vetter99ffa162012-01-25 14:04:00 +0100906 gen6_gt_force_wake_get(dev_priv);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100907
Chris Wilson7338aef2012-04-24 21:48:47 +0100908 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Chris Wilson01a03332011-01-04 22:22:56 +0000909 if (ring->irq_refcount++ == 0) {
Ben Widawskye1ef7cc2012-07-24 20:47:31 -0700910 if (HAS_L3_GPU_CACHE(dev) && ring->id == RCS)
Ben Widawsky15b9f802012-05-25 16:56:23 -0700911 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask |
912 GEN6_RENDER_L3_PARITY_ERROR));
913 else
914 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
Daniel Vetterf637fde2012-04-11 22:12:59 +0200915 dev_priv->gt_irq_mask &= ~ring->irq_enable_mask;
916 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
917 POSTING_READ(GTIMR);
Chris Wilson0f468322011-01-04 17:35:21 +0000918 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100919 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Chris Wilson0f468322011-01-04 17:35:21 +0000920
921 return true;
922}
923
924static void
Ben Widawsky25c06302012-03-29 19:11:27 -0700925gen6_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson0f468322011-01-04 17:35:21 +0000926{
927 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000928 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson7338aef2012-04-24 21:48:47 +0100929 unsigned long flags;
Chris Wilson0f468322011-01-04 17:35:21 +0000930
Chris Wilson7338aef2012-04-24 21:48:47 +0100931 spin_lock_irqsave(&dev_priv->irq_lock, flags);
Chris Wilson01a03332011-01-04 22:22:56 +0000932 if (--ring->irq_refcount == 0) {
Ben Widawskye1ef7cc2012-07-24 20:47:31 -0700933 if (HAS_L3_GPU_CACHE(dev) && ring->id == RCS)
Ben Widawsky15b9f802012-05-25 16:56:23 -0700934 I915_WRITE_IMR(ring, ~GEN6_RENDER_L3_PARITY_ERROR);
935 else
936 I915_WRITE_IMR(ring, ~0);
Daniel Vetterf637fde2012-04-11 22:12:59 +0200937 dev_priv->gt_irq_mask |= ring->irq_enable_mask;
938 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
939 POSTING_READ(GTIMR);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000940 }
Chris Wilson7338aef2012-04-24 21:48:47 +0100941 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100942
Daniel Vetter99ffa162012-01-25 14:04:00 +0100943 gen6_gt_force_wake_put(dev_priv);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000944}
945
Zou Nan haid1b851f2010-05-21 09:08:57 +0800946static int
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200947i965_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800948{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100949 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100950
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100951 ret = intel_ring_begin(ring, 2);
952 if (ret)
953 return ret;
954
Chris Wilson78501ea2010-10-27 12:18:21 +0100955 intel_ring_emit(ring,
Chris Wilson65f56872012-04-17 16:38:12 +0100956 MI_BATCH_BUFFER_START |
957 MI_BATCH_GTT |
Chris Wilson78501ea2010-10-27 12:18:21 +0100958 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000959 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100960 intel_ring_advance(ring);
961
Zou Nan haid1b851f2010-05-21 09:08:57 +0800962 return 0;
963}
964
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800965static int
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200966i830_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000967 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700968{
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000969 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700970
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200971 ret = intel_ring_begin(ring, 4);
972 if (ret)
973 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700974
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200975 intel_ring_emit(ring, MI_BATCH_BUFFER);
976 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
977 intel_ring_emit(ring, offset + len - 8);
978 intel_ring_emit(ring, 0);
979 intel_ring_advance(ring);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100980
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200981 return 0;
982}
983
984static int
985i915_dispatch_execbuffer(struct intel_ring_buffer *ring,
986 u32 offset, u32 len)
987{
988 int ret;
989
990 ret = intel_ring_begin(ring, 2);
991 if (ret)
992 return ret;
993
Chris Wilson65f56872012-04-17 16:38:12 +0100994 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200995 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000996 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700997
Eric Anholt62fdfea2010-05-21 13:26:39 -0700998 return 0;
999}
1000
Chris Wilson78501ea2010-10-27 12:18:21 +01001001static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001002{
Chris Wilson05394f32010-11-08 19:18:58 +00001003 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001004
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001005 obj = ring->status_page.obj;
1006 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001007 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001008
Chris Wilson05394f32010-11-08 19:18:58 +00001009 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001010 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001011 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001012 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001013}
1014
Chris Wilson78501ea2010-10-27 12:18:21 +01001015static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001016{
Chris Wilson78501ea2010-10-27 12:18:21 +01001017 struct drm_device *dev = ring->dev;
Chris Wilson05394f32010-11-08 19:18:58 +00001018 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001019 int ret;
1020
Eric Anholt62fdfea2010-05-21 13:26:39 -07001021 obj = i915_gem_alloc_object(dev, 4096);
1022 if (obj == NULL) {
1023 DRM_ERROR("Failed to allocate status page\n");
1024 ret = -ENOMEM;
1025 goto err;
1026 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01001027
1028 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001029
Chris Wilson86a1ee22012-08-11 15:41:04 +01001030 ret = i915_gem_object_pin(obj, 4096, true, false);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001031 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001032 goto err_unref;
1033 }
1034
Chris Wilson05394f32010-11-08 19:18:58 +00001035 ring->status_page.gfx_addr = obj->gtt_offset;
1036 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001037 if (ring->status_page.page_addr == NULL) {
Ben Widawsky2e6c21e2012-07-12 23:16:12 -07001038 ret = -ENOMEM;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001039 goto err_unpin;
1040 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001041 ring->status_page.obj = obj;
1042 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001043
Chris Wilson78501ea2010-10-27 12:18:21 +01001044 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001045 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
1046 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001047
1048 return 0;
1049
1050err_unpin:
1051 i915_gem_object_unpin(obj);
1052err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001053 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001054err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001055 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001056}
1057
Ben Widawskyc43b5632012-04-16 14:07:40 -07001058static int intel_init_ring_buffer(struct drm_device *dev,
1059 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001060{
Chris Wilson05394f32010-11-08 19:18:58 +00001061 struct drm_i915_gem_object *obj;
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001062 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsondd785e32010-08-07 11:01:34 +01001063 int ret;
1064
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001065 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01001066 INIT_LIST_HEAD(&ring->active_list);
1067 INIT_LIST_HEAD(&ring->request_list);
Daniel Vetterdfc9ef22012-04-11 22:12:47 +02001068 ring->size = 32 * PAGE_SIZE;
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001069
Chris Wilsonb259f672011-03-29 13:19:09 +01001070 init_waitqueue_head(&ring->irq_queue);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001071
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001072 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001073 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001074 if (ret)
1075 return ret;
1076 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001077
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001078 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001079 if (obj == NULL) {
1080 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001081 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +01001082 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001083 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001084
Chris Wilson05394f32010-11-08 19:18:58 +00001085 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001086
Chris Wilson86a1ee22012-08-11 15:41:04 +01001087 ret = i915_gem_object_pin(obj, PAGE_SIZE, true, false);
Chris Wilsondd785e32010-08-07 11:01:34 +01001088 if (ret)
1089 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001090
Chris Wilson3eef8912012-06-04 17:05:40 +01001091 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1092 if (ret)
1093 goto err_unpin;
1094
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001095 ring->virtual_start =
1096 ioremap_wc(dev_priv->mm.gtt->gma_bus_addr + obj->gtt_offset,
1097 ring->size);
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001098 if (ring->virtual_start == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001099 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001100 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +01001101 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001102 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001103
Chris Wilson78501ea2010-10-27 12:18:21 +01001104 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +01001105 if (ret)
1106 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001107
Chris Wilson55249ba2010-12-22 14:04:47 +00001108 /* Workaround an erratum on the i830 which causes a hang if
1109 * the TAIL pointer points to within the last 2 cachelines
1110 * of the buffer.
1111 */
1112 ring->effective_size = ring->size;
Chris Wilson27c1cbd2012-04-09 13:59:46 +01001113 if (IS_I830(ring->dev) || IS_845G(ring->dev))
Chris Wilson55249ba2010-12-22 14:04:47 +00001114 ring->effective_size -= 128;
1115
Chris Wilsonc584fe42010-10-29 18:15:52 +01001116 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +01001117
1118err_unmap:
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001119 iounmap(ring->virtual_start);
Chris Wilsondd785e32010-08-07 11:01:34 +01001120err_unpin:
1121 i915_gem_object_unpin(obj);
1122err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001123 drm_gem_object_unreference(&obj->base);
1124 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +01001125err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +01001126 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001127 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001128}
1129
Chris Wilson78501ea2010-10-27 12:18:21 +01001130void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001131{
Chris Wilson33626e62010-10-29 16:18:36 +01001132 struct drm_i915_private *dev_priv;
1133 int ret;
1134
Chris Wilson05394f32010-11-08 19:18:58 +00001135 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001136 return;
1137
Chris Wilson33626e62010-10-29 16:18:36 +01001138 /* Disable the ring buffer. The ring must be idle at this point */
1139 dev_priv = ring->dev->dev_private;
Ben Widawsky96f298a2011-03-19 18:14:27 -07001140 ret = intel_wait_ring_idle(ring);
Chris Wilson29ee3992011-01-24 16:35:42 +00001141 if (ret)
1142 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
1143 ring->name, ret);
1144
Chris Wilson33626e62010-10-29 16:18:36 +01001145 I915_WRITE_CTL(ring, 0);
1146
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001147 iounmap(ring->virtual_start);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001148
Chris Wilson05394f32010-11-08 19:18:58 +00001149 i915_gem_object_unpin(ring->obj);
1150 drm_gem_object_unreference(&ring->obj->base);
1151 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +01001152
Zou Nan hai8d192152010-11-02 16:31:01 +08001153 if (ring->cleanup)
1154 ring->cleanup(ring);
1155
Chris Wilson78501ea2010-10-27 12:18:21 +01001156 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001157}
1158
Chris Wilson78501ea2010-10-27 12:18:21 +01001159static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001160{
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001161 uint32_t __iomem *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +00001162 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001163
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001164 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001165 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001166 if (ret)
1167 return ret;
1168 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001169
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001170 virt = ring->virtual_start + ring->tail;
1171 rem /= 4;
1172 while (rem--)
1173 iowrite32(MI_NOOP, virt++);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001174
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001175 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001176 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001177
1178 return 0;
1179}
1180
Chris Wilsona71d8d92012-02-15 11:25:36 +00001181static int intel_ring_wait_seqno(struct intel_ring_buffer *ring, u32 seqno)
1182{
Chris Wilsona71d8d92012-02-15 11:25:36 +00001183 int ret;
1184
Ben Widawsky199b2bc2012-05-24 15:03:11 -07001185 ret = i915_wait_seqno(ring, seqno);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001186 if (!ret)
1187 i915_gem_retire_requests_ring(ring);
Chris Wilsona71d8d92012-02-15 11:25:36 +00001188
1189 return ret;
1190}
1191
1192static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
1193{
1194 struct drm_i915_gem_request *request;
1195 u32 seqno = 0;
1196 int ret;
1197
1198 i915_gem_retire_requests_ring(ring);
1199
1200 if (ring->last_retired_head != -1) {
1201 ring->head = ring->last_retired_head;
1202 ring->last_retired_head = -1;
1203 ring->space = ring_space(ring);
1204 if (ring->space >= n)
1205 return 0;
1206 }
1207
1208 list_for_each_entry(request, &ring->request_list, list) {
1209 int space;
1210
1211 if (request->tail == -1)
1212 continue;
1213
1214 space = request->tail - (ring->tail + 8);
1215 if (space < 0)
1216 space += ring->size;
1217 if (space >= n) {
1218 seqno = request->seqno;
1219 break;
1220 }
1221
1222 /* Consume this request in case we need more space than
1223 * is available and so need to prevent a race between
1224 * updating last_retired_head and direct reads of
1225 * I915_RING_HEAD. It also provides a nice sanity check.
1226 */
1227 request->tail = -1;
1228 }
1229
1230 if (seqno == 0)
1231 return -ENOSPC;
1232
1233 ret = intel_ring_wait_seqno(ring, seqno);
1234 if (ret)
1235 return ret;
1236
1237 if (WARN_ON(ring->last_retired_head == -1))
1238 return -ENOSPC;
1239
1240 ring->head = ring->last_retired_head;
1241 ring->last_retired_head = -1;
1242 ring->space = ring_space(ring);
1243 if (WARN_ON(ring->space < n))
1244 return -ENOSPC;
1245
1246 return 0;
1247}
1248
Chris Wilson78501ea2010-10-27 12:18:21 +01001249int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001250{
Chris Wilson78501ea2010-10-27 12:18:21 +01001251 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001252 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001253 unsigned long end;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001254 int ret;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001255
Chris Wilsona71d8d92012-02-15 11:25:36 +00001256 ret = intel_ring_wait_request(ring, n);
1257 if (ret != -ENOSPC)
1258 return ret;
1259
Chris Wilsondb53a302011-02-03 11:57:46 +00001260 trace_i915_ring_wait_begin(ring);
Daniel Vetter63ed2cb2012-04-23 16:50:50 +02001261 /* With GEM the hangcheck timer should kick us out of the loop,
1262 * leaving it early runs the risk of corrupting GEM state (due
1263 * to running on almost untested codepaths). But on resume
1264 * timers don't work yet, so prevent a complete hang in that
1265 * case by choosing an insanely large timeout. */
1266 end = jiffies + 60 * HZ;
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001267
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001268 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001269 ring->head = I915_READ_HEAD(ring);
1270 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001271 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001272 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001273 return 0;
1274 }
1275
1276 if (dev->primary->master) {
1277 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1278 if (master_priv->sarea_priv)
1279 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1280 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001281
Chris Wilsone60a0b12010-10-13 10:09:14 +01001282 msleep(1);
Daniel Vetterd6b2c792012-07-04 22:54:13 +02001283
1284 ret = i915_gem_check_wedge(dev_priv, dev_priv->mm.interruptible);
1285 if (ret)
1286 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001287 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +00001288 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001289 return -EBUSY;
1290}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001291
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001292int intel_ring_begin(struct intel_ring_buffer *ring,
1293 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001294{
Daniel Vetterde2b9982012-07-04 22:52:50 +02001295 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +08001296 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001297 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001298
Daniel Vetterde2b9982012-07-04 22:52:50 +02001299 ret = i915_gem_check_wedge(dev_priv, dev_priv->mm.interruptible);
1300 if (ret)
1301 return ret;
Chris Wilson21dd3732011-01-26 15:55:56 +00001302
Chris Wilson55249ba2010-12-22 14:04:47 +00001303 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001304 ret = intel_wrap_ring_buffer(ring);
1305 if (unlikely(ret))
1306 return ret;
1307 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001308
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001309 if (unlikely(ring->space < n)) {
1310 ret = intel_wait_ring_buffer(ring, n);
1311 if (unlikely(ret))
1312 return ret;
1313 }
Chris Wilsond97ed332010-08-04 15:18:13 +01001314
1315 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001316 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001317}
1318
Chris Wilson78501ea2010-10-27 12:18:21 +01001319void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001320{
Daniel Vettere5eb3d62012-05-03 14:48:16 +02001321 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1322
Chris Wilsond97ed332010-08-04 15:18:13 +01001323 ring->tail &= ring->size - 1;
Daniel Vettere5eb3d62012-05-03 14:48:16 +02001324 if (dev_priv->stop_rings & intel_ring_flag(ring))
1325 return;
Chris Wilson78501ea2010-10-27 12:18:21 +01001326 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001327}
1328
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001329
Chris Wilson78501ea2010-10-27 12:18:21 +01001330static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001331 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001332{
Akshay Joshi0206e352011-08-16 15:34:10 -04001333 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001334
1335 /* Every tail move must follow the sequence below */
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001336
Chris Wilson12f55812012-07-05 17:14:01 +01001337 /* Disable notification that the ring is IDLE. The GT
1338 * will then assume that it is busy and bring it out of rc6.
1339 */
1340 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1341 _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
1342
1343 /* Clear the context id. Here be magic! */
1344 I915_WRITE64(GEN6_BSD_RNCID, 0x0);
1345
1346 /* Wait for the ring not to be idle, i.e. for it to wake up. */
Akshay Joshi0206e352011-08-16 15:34:10 -04001347 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
Chris Wilson12f55812012-07-05 17:14:01 +01001348 GEN6_BSD_SLEEP_INDICATOR) == 0,
1349 50))
1350 DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001351
Chris Wilson12f55812012-07-05 17:14:01 +01001352 /* Now that the ring is fully powered up, update the tail */
Akshay Joshi0206e352011-08-16 15:34:10 -04001353 I915_WRITE_TAIL(ring, value);
Chris Wilson12f55812012-07-05 17:14:01 +01001354 POSTING_READ(RING_TAIL(ring->mmio_base));
1355
1356 /* Let the ring send IDLE messages to the GT again,
1357 * and so let it sleep to conserve power when idle.
1358 */
Akshay Joshi0206e352011-08-16 15:34:10 -04001359 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
Chris Wilson12f55812012-07-05 17:14:01 +01001360 _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001361}
1362
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001363static int gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001364 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001365{
Chris Wilson71a77e02011-02-02 12:13:49 +00001366 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001367 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001368
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001369 ret = intel_ring_begin(ring, 4);
1370 if (ret)
1371 return ret;
1372
Chris Wilson71a77e02011-02-02 12:13:49 +00001373 cmd = MI_FLUSH_DW;
1374 if (invalidate & I915_GEM_GPU_DOMAINS)
1375 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1376 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001377 intel_ring_emit(ring, 0);
1378 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001379 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001380 intel_ring_advance(ring);
1381 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001382}
1383
1384static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001385gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001386 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001387{
Akshay Joshi0206e352011-08-16 15:34:10 -04001388 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001389
Akshay Joshi0206e352011-08-16 15:34:10 -04001390 ret = intel_ring_begin(ring, 2);
1391 if (ret)
1392 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001393
Akshay Joshi0206e352011-08-16 15:34:10 -04001394 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1395 /* bit0-7 is the length on GEN6+ */
1396 intel_ring_emit(ring, offset);
1397 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001398
Akshay Joshi0206e352011-08-16 15:34:10 -04001399 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001400}
1401
Chris Wilson549f7362010-10-19 11:19:32 +01001402/* Blitter support (SandyBridge+) */
1403
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001404static int blt_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001405 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001406{
Chris Wilson71a77e02011-02-02 12:13:49 +00001407 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001408 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001409
Daniel Vetter6a233c72011-12-14 13:57:07 +01001410 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001411 if (ret)
1412 return ret;
1413
Chris Wilson71a77e02011-02-02 12:13:49 +00001414 cmd = MI_FLUSH_DW;
1415 if (invalidate & I915_GEM_DOMAIN_RENDER)
1416 cmd |= MI_INVALIDATE_TLB;
1417 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001418 intel_ring_emit(ring, 0);
1419 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001420 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001421 intel_ring_advance(ring);
1422 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001423}
1424
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001425int intel_init_render_ring_buffer(struct drm_device *dev)
1426{
1427 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001428 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001429
Daniel Vetter59465b52012-04-11 22:12:48 +02001430 ring->name = "render ring";
1431 ring->id = RCS;
1432 ring->mmio_base = RENDER_RING_BASE;
1433
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001434 if (INTEL_INFO(dev)->gen >= 6) {
1435 ring->add_request = gen6_add_request;
Paulo Zanoni4772eae2012-08-17 18:35:41 -03001436 ring->flush = gen7_render_ring_flush;
Chris Wilson6c6cf5a2012-07-20 18:02:28 +01001437 if (INTEL_INFO(dev)->gen == 6)
1438 ring->flush = gen6_render_ring_flush__wa;
Ben Widawsky25c06302012-03-29 19:11:27 -07001439 ring->irq_get = gen6_ring_get_irq;
1440 ring->irq_put = gen6_ring_put_irq;
Daniel Vetter6a848cc2012-04-11 22:12:46 +02001441 ring->irq_enable_mask = GT_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001442 ring->get_seqno = gen6_ring_get_seqno;
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001443 ring->sync_to = gen6_ring_sync;
Daniel Vetter59465b52012-04-11 22:12:48 +02001444 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_INVALID;
1445 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_RV;
1446 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_RB;
1447 ring->signal_mbox[0] = GEN6_VRSYNC;
1448 ring->signal_mbox[1] = GEN6_BRSYNC;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001449 } else if (IS_GEN5(dev)) {
1450 ring->add_request = pc_render_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001451 ring->flush = gen4_render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001452 ring->get_seqno = pc_render_get_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02001453 ring->irq_get = gen5_ring_get_irq;
1454 ring->irq_put = gen5_ring_put_irq;
Daniel Vettere3670312012-04-11 22:12:53 +02001455 ring->irq_enable_mask = GT_USER_INTERRUPT | GT_PIPE_NOTIFY;
Daniel Vetter59465b52012-04-11 22:12:48 +02001456 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02001457 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001458 if (INTEL_INFO(dev)->gen < 4)
1459 ring->flush = gen2_render_ring_flush;
1460 else
1461 ring->flush = gen4_render_ring_flush;
Daniel Vetter59465b52012-04-11 22:12:48 +02001462 ring->get_seqno = ring_get_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001463 if (IS_GEN2(dev)) {
1464 ring->irq_get = i8xx_ring_get_irq;
1465 ring->irq_put = i8xx_ring_put_irq;
1466 } else {
1467 ring->irq_get = i9xx_ring_get_irq;
1468 ring->irq_put = i9xx_ring_put_irq;
1469 }
Daniel Vettere3670312012-04-11 22:12:53 +02001470 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001471 }
Daniel Vetter59465b52012-04-11 22:12:48 +02001472 ring->write_tail = ring_write_tail;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001473 if (INTEL_INFO(dev)->gen >= 6)
1474 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1475 else if (INTEL_INFO(dev)->gen >= 4)
1476 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1477 else if (IS_I830(dev) || IS_845G(dev))
1478 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1479 else
1480 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02001481 ring->init = init_render_ring;
1482 ring->cleanup = render_ring_cleanup;
1483
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001484
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001485 if (!I915_NEED_GFX_HWS(dev)) {
1486 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1487 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1488 }
1489
1490 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001491}
1492
Chris Wilsone8616b62011-01-20 09:57:11 +00001493int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1494{
1495 drm_i915_private_t *dev_priv = dev->dev_private;
1496 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1497
Daniel Vetter59465b52012-04-11 22:12:48 +02001498 ring->name = "render ring";
1499 ring->id = RCS;
1500 ring->mmio_base = RENDER_RING_BASE;
1501
Chris Wilsone8616b62011-01-20 09:57:11 +00001502 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetterb4178f82012-04-11 22:12:51 +02001503 /* non-kms not supported on gen6+ */
1504 return -ENODEV;
Chris Wilsone8616b62011-01-20 09:57:11 +00001505 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02001506
1507 /* Note: gem is not supported on gen5/ilk without kms (the corresponding
1508 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
1509 * the special gen5 functions. */
1510 ring->add_request = i9xx_add_request;
Chris Wilson46f0f8d2012-04-18 11:12:11 +01001511 if (INTEL_INFO(dev)->gen < 4)
1512 ring->flush = gen2_render_ring_flush;
1513 else
1514 ring->flush = gen4_render_ring_flush;
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02001515 ring->get_seqno = ring_get_seqno;
Chris Wilsonc2798b12012-04-22 21:13:57 +01001516 if (IS_GEN2(dev)) {
1517 ring->irq_get = i8xx_ring_get_irq;
1518 ring->irq_put = i8xx_ring_put_irq;
1519 } else {
1520 ring->irq_get = i9xx_ring_get_irq;
1521 ring->irq_put = i9xx_ring_put_irq;
1522 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02001523 ring->irq_enable_mask = I915_USER_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02001524 ring->write_tail = ring_write_tail;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001525 if (INTEL_INFO(dev)->gen >= 4)
1526 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1527 else if (IS_I830(dev) || IS_845G(dev))
1528 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1529 else
1530 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02001531 ring->init = init_render_ring;
1532 ring->cleanup = render_ring_cleanup;
Chris Wilsone8616b62011-01-20 09:57:11 +00001533
Keith Packardf3234702011-07-22 10:44:39 -07001534 if (!I915_NEED_GFX_HWS(dev))
1535 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1536
Chris Wilsone8616b62011-01-20 09:57:11 +00001537 ring->dev = dev;
1538 INIT_LIST_HEAD(&ring->active_list);
1539 INIT_LIST_HEAD(&ring->request_list);
Chris Wilsone8616b62011-01-20 09:57:11 +00001540
1541 ring->size = size;
1542 ring->effective_size = ring->size;
1543 if (IS_I830(ring->dev))
1544 ring->effective_size -= 128;
1545
Daniel Vetter4225d0f2012-04-26 23:28:16 +02001546 ring->virtual_start = ioremap_wc(start, size);
1547 if (ring->virtual_start == NULL) {
Chris Wilsone8616b62011-01-20 09:57:11 +00001548 DRM_ERROR("can not ioremap virtual address for"
1549 " ring buffer\n");
1550 return -ENOMEM;
1551 }
1552
Chris Wilsone8616b62011-01-20 09:57:11 +00001553 return 0;
1554}
1555
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001556int intel_init_bsd_ring_buffer(struct drm_device *dev)
1557{
1558 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001559 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001560
Daniel Vetter58fa3832012-04-11 22:12:49 +02001561 ring->name = "bsd ring";
1562 ring->id = VCS;
1563
Daniel Vetter0fd2c202012-04-11 22:12:55 +02001564 ring->write_tail = ring_write_tail;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001565 if (IS_GEN6(dev) || IS_GEN7(dev)) {
1566 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02001567 /* gen6 bsd needs a special wa for tail updates */
1568 if (IS_GEN6(dev))
1569 ring->write_tail = gen6_bsd_ring_write_tail;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001570 ring->flush = gen6_ring_flush;
1571 ring->add_request = gen6_add_request;
1572 ring->get_seqno = gen6_ring_get_seqno;
1573 ring->irq_enable_mask = GEN6_BSD_USER_INTERRUPT;
1574 ring->irq_get = gen6_ring_get_irq;
1575 ring->irq_put = gen6_ring_put_irq;
1576 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001577 ring->sync_to = gen6_ring_sync;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001578 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_VR;
1579 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_INVALID;
1580 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_VB;
1581 ring->signal_mbox[0] = GEN6_RVSYNC;
1582 ring->signal_mbox[1] = GEN6_BVSYNC;
1583 } else {
1584 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001585 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02001586 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001587 ring->get_seqno = ring_get_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02001588 if (IS_GEN5(dev)) {
Daniel Vettere3670312012-04-11 22:12:53 +02001589 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02001590 ring->irq_get = gen5_ring_get_irq;
1591 ring->irq_put = gen5_ring_put_irq;
1592 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02001593 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02001594 ring->irq_get = i9xx_ring_get_irq;
1595 ring->irq_put = i9xx_ring_put_irq;
1596 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001597 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001598 }
1599 ring->init = init_ring_common;
1600
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001601
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001602 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001603}
Chris Wilson549f7362010-10-19 11:19:32 +01001604
1605int intel_init_blt_ring_buffer(struct drm_device *dev)
1606{
1607 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001608 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001609
Daniel Vetter3535d9d2012-04-11 22:12:50 +02001610 ring->name = "blitter ring";
1611 ring->id = BCS;
1612
1613 ring->mmio_base = BLT_RING_BASE;
1614 ring->write_tail = ring_write_tail;
1615 ring->flush = blt_ring_flush;
1616 ring->add_request = gen6_add_request;
1617 ring->get_seqno = gen6_ring_get_seqno;
1618 ring->irq_enable_mask = GEN6_BLITTER_USER_INTERRUPT;
1619 ring->irq_get = gen6_ring_get_irq;
1620 ring->irq_put = gen6_ring_put_irq;
1621 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001622 ring->sync_to = gen6_ring_sync;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02001623 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_BR;
1624 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_BV;
1625 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_INVALID;
1626 ring->signal_mbox[0] = GEN6_RBSYNC;
1627 ring->signal_mbox[1] = GEN6_VBSYNC;
1628 ring->init = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01001629
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001630 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001631}
Chris Wilsona7b97612012-07-20 12:41:08 +01001632
1633int
1634intel_ring_flush_all_caches(struct intel_ring_buffer *ring)
1635{
1636 int ret;
1637
1638 if (!ring->gpu_caches_dirty)
1639 return 0;
1640
1641 ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
1642 if (ret)
1643 return ret;
1644
1645 trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
1646
1647 ring->gpu_caches_dirty = false;
1648 return 0;
1649}
1650
1651int
1652intel_ring_invalidate_all_caches(struct intel_ring_buffer *ring)
1653{
1654 uint32_t flush_domains;
1655 int ret;
1656
1657 flush_domains = 0;
1658 if (ring->gpu_caches_dirty)
1659 flush_domains = I915_GEM_GPU_DOMAINS;
1660
1661 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
1662 if (ret)
1663 return ret;
1664
1665 trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
1666
1667 ring->gpu_caches_dirty = false;
1668 return 0;
1669}