blob: 6b3ff37e752c4f15bb97a54cc2c8ee7553f0fdca [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 Wilson78501ea2010-10-27 12:18:21 +010056render_ring_flush(struct intel_ring_buffer *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +010057 u32 invalidate_domains,
58 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070059{
Chris Wilson78501ea2010-10-27 12:18:21 +010060 struct drm_device *dev = ring->dev;
Chris Wilson6f392d5482010-08-07 11:01:22 +010061 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000062 int ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +010063
Chris Wilson36d527d2011-03-19 22:26:49 +000064 /*
65 * read/write caches:
66 *
67 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
68 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
69 * also flushed at 2d versus 3d pipeline switches.
70 *
71 * read-only caches:
72 *
73 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
74 * MI_READ_FLUSH is set, and is always flushed on 965.
75 *
76 * I915_GEM_DOMAIN_COMMAND may not exist?
77 *
78 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
79 * invalidated when MI_EXE_FLUSH is set.
80 *
81 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
82 * invalidated with every MI_FLUSH.
83 *
84 * TLBs:
85 *
86 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
87 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
88 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
89 * are flushed at any MI_FLUSH.
90 */
91
92 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
93 if ((invalidate_domains|flush_domains) &
94 I915_GEM_DOMAIN_RENDER)
95 cmd &= ~MI_NO_WRITE_FLUSH;
96 if (INTEL_INFO(dev)->gen < 4) {
Eric Anholt62fdfea2010-05-21 13:26:39 -070097 /*
Chris Wilson36d527d2011-03-19 22:26:49 +000098 * On the 965, the sampler cache always gets flushed
99 * and this bit is reserved.
Eric Anholt62fdfea2010-05-21 13:26:39 -0700100 */
Chris Wilson36d527d2011-03-19 22:26:49 +0000101 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
102 cmd |= MI_READ_FLUSH;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800103 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000104 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
105 cmd |= MI_EXE_FLUSH;
106
107 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
108 (IS_G4X(dev) || IS_GEN5(dev)))
109 cmd |= MI_INVALIDATE_ISP;
110
111 ret = intel_ring_begin(ring, 2);
112 if (ret)
113 return ret;
114
115 intel_ring_emit(ring, cmd);
116 intel_ring_emit(ring, MI_NOOP);
117 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000118
119 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800120}
121
Jesse Barnes8d315282011-10-16 10:23:31 +0200122/**
123 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
124 * implementing two workarounds on gen6. From section 1.4.7.1
125 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
126 *
127 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
128 * produced by non-pipelined state commands), software needs to first
129 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
130 * 0.
131 *
132 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
133 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
134 *
135 * And the workaround for these two requires this workaround first:
136 *
137 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
138 * BEFORE the pipe-control with a post-sync op and no write-cache
139 * flushes.
140 *
141 * And this last workaround is tricky because of the requirements on
142 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
143 * volume 2 part 1:
144 *
145 * "1 of the following must also be set:
146 * - Render Target Cache Flush Enable ([12] of DW1)
147 * - Depth Cache Flush Enable ([0] of DW1)
148 * - Stall at Pixel Scoreboard ([1] of DW1)
149 * - Depth Stall ([13] of DW1)
150 * - Post-Sync Operation ([13] of DW1)
151 * - Notify Enable ([8] of DW1)"
152 *
153 * The cache flushes require the workaround flush that triggered this
154 * one, so we can't use it. Depth stall would trigger the same.
155 * Post-sync nonzero is what triggered this second workaround, so we
156 * can't use that one either. Notify enable is IRQs, which aren't
157 * really our business. That leaves only stall at scoreboard.
158 */
159static int
160intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
161{
162 struct pipe_control *pc = ring->private;
163 u32 scratch_addr = pc->gtt_offset + 128;
164 int ret;
165
166
167 ret = intel_ring_begin(ring, 6);
168 if (ret)
169 return ret;
170
171 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
172 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
173 PIPE_CONTROL_STALL_AT_SCOREBOARD);
174 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
175 intel_ring_emit(ring, 0); /* low dword */
176 intel_ring_emit(ring, 0); /* high dword */
177 intel_ring_emit(ring, MI_NOOP);
178 intel_ring_advance(ring);
179
180 ret = intel_ring_begin(ring, 6);
181 if (ret)
182 return ret;
183
184 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
185 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
186 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
187 intel_ring_emit(ring, 0);
188 intel_ring_emit(ring, 0);
189 intel_ring_emit(ring, MI_NOOP);
190 intel_ring_advance(ring);
191
192 return 0;
193}
194
195static int
196gen6_render_ring_flush(struct intel_ring_buffer *ring,
197 u32 invalidate_domains, u32 flush_domains)
198{
199 u32 flags = 0;
200 struct pipe_control *pc = ring->private;
201 u32 scratch_addr = pc->gtt_offset + 128;
202 int ret;
203
204 /* Force SNB workarounds for PIPE_CONTROL flushes */
205 intel_emit_post_sync_nonzero_flush(ring);
206
207 /* Just flush everything. Experiments have shown that reducing the
208 * number of bits based on the write domains has little performance
209 * impact.
210 */
211 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
212 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
213 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
214 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
215 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
216 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
217 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
218
219 ret = intel_ring_begin(ring, 6);
220 if (ret)
221 return ret;
222
223 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
224 intel_ring_emit(ring, flags);
225 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
226 intel_ring_emit(ring, 0); /* lower dword */
227 intel_ring_emit(ring, 0); /* uppwer dword */
228 intel_ring_emit(ring, MI_NOOP);
229 intel_ring_advance(ring);
230
231 return 0;
232}
233
Chris Wilson78501ea2010-10-27 12:18:21 +0100234static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100235 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800236{
Chris Wilson78501ea2010-10-27 12:18:21 +0100237 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100238 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800239}
240
Chris Wilson78501ea2010-10-27 12:18:21 +0100241u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800242{
Chris Wilson78501ea2010-10-27 12:18:21 +0100243 drm_i915_private_t *dev_priv = ring->dev->dev_private;
244 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200245 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800246
247 return I915_READ(acthd_reg);
248}
249
Chris Wilson78501ea2010-10-27 12:18:21 +0100250static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800251{
Chris Wilson78501ea2010-10-27 12:18:21 +0100252 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000253 struct drm_i915_gem_object *obj = ring->obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800254 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800255
256 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200257 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200258 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100259 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800260
261 /* Initialize the ring. */
Chris Wilson05394f32010-11-08 19:18:58 +0000262 I915_WRITE_START(ring, obj->gtt_offset);
Daniel Vetter570ef602010-08-02 17:06:23 +0200263 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800264
265 /* G45 ring initialization fails to reset head to zero */
266 if (head != 0) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000267 DRM_DEBUG_KMS("%s head not reset to zero "
268 "ctl %08x head %08x tail %08x start %08x\n",
269 ring->name,
270 I915_READ_CTL(ring),
271 I915_READ_HEAD(ring),
272 I915_READ_TAIL(ring),
273 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800274
Daniel Vetter570ef602010-08-02 17:06:23 +0200275 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800276
Chris Wilson6fd0d562010-12-05 20:42:33 +0000277 if (I915_READ_HEAD(ring) & HEAD_ADDR) {
278 DRM_ERROR("failed to set %s head to zero "
279 "ctl %08x head %08x tail %08x start %08x\n",
280 ring->name,
281 I915_READ_CTL(ring),
282 I915_READ_HEAD(ring),
283 I915_READ_TAIL(ring),
284 I915_READ_START(ring));
285 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700286 }
287
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200288 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000289 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000290 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800291
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800292 /* If the head is still not zero, the ring is dead */
Sean Paulf01db982012-03-16 12:43:22 -0400293 if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
294 I915_READ_START(ring) == obj->gtt_offset &&
295 (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000296 DRM_ERROR("%s initialization failed "
297 "ctl %08x head %08x tail %08x start %08x\n",
298 ring->name,
299 I915_READ_CTL(ring),
300 I915_READ_HEAD(ring),
301 I915_READ_TAIL(ring),
302 I915_READ_START(ring));
303 return -EIO;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800304 }
305
Chris Wilson78501ea2010-10-27 12:18:21 +0100306 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
307 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800308 else {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000309 ring->head = I915_READ_HEAD(ring);
Daniel Vetter870e86d2010-08-02 16:29:44 +0200310 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000311 ring->space = ring_space(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800312 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000313
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800314 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700315}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800316
Chris Wilsonc6df5412010-12-15 09:56:50 +0000317static int
318init_pipe_control(struct intel_ring_buffer *ring)
319{
320 struct pipe_control *pc;
321 struct drm_i915_gem_object *obj;
322 int ret;
323
324 if (ring->private)
325 return 0;
326
327 pc = kmalloc(sizeof(*pc), GFP_KERNEL);
328 if (!pc)
329 return -ENOMEM;
330
331 obj = i915_gem_alloc_object(ring->dev, 4096);
332 if (obj == NULL) {
333 DRM_ERROR("Failed to allocate seqno page\n");
334 ret = -ENOMEM;
335 goto err;
336 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100337
338 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000339
340 ret = i915_gem_object_pin(obj, 4096, true);
341 if (ret)
342 goto err_unref;
343
344 pc->gtt_offset = obj->gtt_offset;
345 pc->cpu_page = kmap(obj->pages[0]);
346 if (pc->cpu_page == NULL)
347 goto err_unpin;
348
349 pc->obj = obj;
350 ring->private = pc;
351 return 0;
352
353err_unpin:
354 i915_gem_object_unpin(obj);
355err_unref:
356 drm_gem_object_unreference(&obj->base);
357err:
358 kfree(pc);
359 return ret;
360}
361
362static void
363cleanup_pipe_control(struct intel_ring_buffer *ring)
364{
365 struct pipe_control *pc = ring->private;
366 struct drm_i915_gem_object *obj;
367
368 if (!ring->private)
369 return;
370
371 obj = pc->obj;
372 kunmap(obj->pages[0]);
373 i915_gem_object_unpin(obj);
374 drm_gem_object_unreference(&obj->base);
375
376 kfree(pc);
377 ring->private = NULL;
378}
379
Chris Wilson78501ea2010-10-27 12:18:21 +0100380static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800381{
Chris Wilson78501ea2010-10-27 12:18:21 +0100382 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000383 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100384 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800385
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100386 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100387 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800388 I915_WRITE(MI_MODE, mode);
Jesse Barnesb095cd02011-08-12 15:28:32 -0700389 if (IS_GEN7(dev))
390 I915_WRITE(GFX_MODE_GEN7,
391 GFX_MODE_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
392 GFX_MODE_ENABLE(GFX_REPLAY_MODE));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800393 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100394
Jesse Barnes8d315282011-10-16 10:23:31 +0200395 if (INTEL_INFO(dev)->gen >= 5) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000396 ret = init_pipe_control(ring);
397 if (ret)
398 return ret;
399 }
400
Ben Widawsky84f9f932011-12-12 19:21:58 -0800401 if (INTEL_INFO(dev)->gen >= 6) {
402 I915_WRITE(INSTPM,
403 INSTPM_FORCE_ORDERING << 16 | INSTPM_FORCE_ORDERING);
404 }
405
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800406 return ret;
407}
408
Chris Wilsonc6df5412010-12-15 09:56:50 +0000409static void render_ring_cleanup(struct intel_ring_buffer *ring)
410{
411 if (!ring->private)
412 return;
413
414 cleanup_pipe_control(ring);
415}
416
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000417static void
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700418update_mboxes(struct intel_ring_buffer *ring,
419 u32 seqno,
420 u32 mmio_offset)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000421{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700422 intel_ring_emit(ring, MI_SEMAPHORE_MBOX |
423 MI_SEMAPHORE_GLOBAL_GTT |
424 MI_SEMAPHORE_REGISTER |
425 MI_SEMAPHORE_UPDATE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000426 intel_ring_emit(ring, seqno);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700427 intel_ring_emit(ring, mmio_offset);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000428}
429
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700430/**
431 * gen6_add_request - Update the semaphore mailbox registers
432 *
433 * @ring - ring that is adding a request
434 * @seqno - return seqno stuck into the ring
435 *
436 * Update the mailbox registers in the *other* rings with the current seqno.
437 * This acts like a signal in the canonical semaphore.
438 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000439static int
440gen6_add_request(struct intel_ring_buffer *ring,
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700441 u32 *seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000442{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700443 u32 mbox1_reg;
444 u32 mbox2_reg;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000445 int ret;
446
447 ret = intel_ring_begin(ring, 10);
448 if (ret)
449 return ret;
450
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700451 mbox1_reg = ring->signal_mbox[0];
452 mbox2_reg = ring->signal_mbox[1];
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000453
Daniel Vetter53d227f2012-01-25 16:32:49 +0100454 *seqno = i915_gem_next_request_seqno(ring);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700455
456 update_mboxes(ring, *seqno, mbox1_reg);
457 update_mboxes(ring, *seqno, mbox2_reg);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000458 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
459 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700460 intel_ring_emit(ring, *seqno);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000461 intel_ring_emit(ring, MI_USER_INTERRUPT);
462 intel_ring_advance(ring);
463
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000464 return 0;
465}
466
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700467/**
468 * intel_ring_sync - sync the waiter to the signaller on seqno
469 *
470 * @waiter - ring that is waiting
471 * @signaller - ring which has, or will signal
472 * @seqno - seqno which the waiter will block on
473 */
474static int
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200475gen6_ring_sync(struct intel_ring_buffer *waiter,
476 struct intel_ring_buffer *signaller,
477 u32 seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000478{
479 int ret;
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700480 u32 dw1 = MI_SEMAPHORE_MBOX |
481 MI_SEMAPHORE_COMPARE |
482 MI_SEMAPHORE_REGISTER;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000483
Ben Widawsky1500f7e2012-04-11 11:18:21 -0700484 /* Throughout all of the GEM code, seqno passed implies our current
485 * seqno is >= the last seqno executed. However for hardware the
486 * comparison is strictly greater than.
487 */
488 seqno -= 1;
489
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200490 WARN_ON(signaller->semaphore_register[waiter->id] ==
491 MI_SEMAPHORE_SYNC_INVALID);
492
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700493 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000494 if (ret)
495 return ret;
496
Daniel Vetter686cb5f2012-04-11 22:12:52 +0200497 intel_ring_emit(waiter,
498 dw1 | signaller->semaphore_register[waiter->id]);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700499 intel_ring_emit(waiter, seqno);
500 intel_ring_emit(waiter, 0);
501 intel_ring_emit(waiter, MI_NOOP);
502 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000503
504 return 0;
505}
506
Chris Wilsonc6df5412010-12-15 09:56:50 +0000507#define PIPE_CONTROL_FLUSH(ring__, addr__) \
508do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200509 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
510 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000511 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
512 intel_ring_emit(ring__, 0); \
513 intel_ring_emit(ring__, 0); \
514} while (0)
515
516static int
517pc_render_add_request(struct intel_ring_buffer *ring,
518 u32 *result)
519{
Daniel Vetter53d227f2012-01-25 16:32:49 +0100520 u32 seqno = i915_gem_next_request_seqno(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000521 struct pipe_control *pc = ring->private;
522 u32 scratch_addr = pc->gtt_offset + 128;
523 int ret;
524
525 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
526 * incoherent with writes to memory, i.e. completely fubar,
527 * so we need to use PIPE_NOTIFY instead.
528 *
529 * However, we also need to workaround the qword write
530 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
531 * memory before requesting an interrupt.
532 */
533 ret = intel_ring_begin(ring, 32);
534 if (ret)
535 return ret;
536
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200537 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200538 PIPE_CONTROL_WRITE_FLUSH |
539 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000540 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
541 intel_ring_emit(ring, seqno);
542 intel_ring_emit(ring, 0);
543 PIPE_CONTROL_FLUSH(ring, scratch_addr);
544 scratch_addr += 128; /* write to separate cachelines */
545 PIPE_CONTROL_FLUSH(ring, scratch_addr);
546 scratch_addr += 128;
547 PIPE_CONTROL_FLUSH(ring, scratch_addr);
548 scratch_addr += 128;
549 PIPE_CONTROL_FLUSH(ring, scratch_addr);
550 scratch_addr += 128;
551 PIPE_CONTROL_FLUSH(ring, scratch_addr);
552 scratch_addr += 128;
553 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +0000554
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200555 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200556 PIPE_CONTROL_WRITE_FLUSH |
557 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000558 PIPE_CONTROL_NOTIFY);
559 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
560 intel_ring_emit(ring, seqno);
561 intel_ring_emit(ring, 0);
562 intel_ring_advance(ring);
563
564 *result = seqno;
565 return 0;
566}
567
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800568static u32
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100569gen6_ring_get_seqno(struct intel_ring_buffer *ring)
570{
571 struct drm_device *dev = ring->dev;
572
573 /* Workaround to force correct ordering between irq and seqno writes on
574 * ivb (and maybe also on snb) by reading from a CS register (like
575 * ACTHD) before reading the status page. */
Daniel Vetter1c7eaac2012-03-27 09:31:24 +0200576 if (IS_GEN6(dev) || IS_GEN7(dev))
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100577 intel_ring_get_active_head(ring);
578 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
579}
580
581static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000582ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800583{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000584 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
585}
586
Chris Wilsonc6df5412010-12-15 09:56:50 +0000587static u32
588pc_render_get_seqno(struct intel_ring_buffer *ring)
589{
590 struct pipe_control *pc = ring->private;
591 return pc->cpu_page[0];
592}
593
Chris Wilson0f468322011-01-04 17:35:21 +0000594static void
595ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
596{
597 dev_priv->gt_irq_mask &= ~mask;
598 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
599 POSTING_READ(GTIMR);
600}
601
602static void
603ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
604{
605 dev_priv->gt_irq_mask |= mask;
606 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
607 POSTING_READ(GTIMR);
608}
609
610static void
611i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
612{
613 dev_priv->irq_mask &= ~mask;
614 I915_WRITE(IMR, dev_priv->irq_mask);
615 POSTING_READ(IMR);
616}
617
618static void
619i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
620{
621 dev_priv->irq_mask |= mask;
622 I915_WRITE(IMR, dev_priv->irq_mask);
623 POSTING_READ(IMR);
624}
625
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000626static bool
Daniel Vettere48d8632012-04-11 22:12:54 +0200627gen5_ring_get_irq(struct intel_ring_buffer *ring)
628{
629 struct drm_device *dev = ring->dev;
630 drm_i915_private_t *dev_priv = dev->dev_private;
631
632 if (!dev->irq_enabled)
633 return false;
634
635 spin_lock(&ring->irq_lock);
636 if (ring->irq_refcount++ == 0)
637 ironlake_enable_irq(dev_priv, ring->irq_enable_mask);
638 spin_unlock(&ring->irq_lock);
639
640 return true;
641}
642
643static void
644gen5_ring_put_irq(struct intel_ring_buffer *ring)
645{
646 struct drm_device *dev = ring->dev;
647 drm_i915_private_t *dev_priv = dev->dev_private;
648
649 spin_lock(&ring->irq_lock);
650 if (--ring->irq_refcount == 0)
651 ironlake_disable_irq(dev_priv, ring->irq_enable_mask);
652 spin_unlock(&ring->irq_lock);
653}
654
655static bool
Daniel Vettere3670312012-04-11 22:12:53 +0200656i9xx_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700657{
Chris Wilson78501ea2010-10-27 12:18:21 +0100658 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000659 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700660
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000661 if (!dev->irq_enabled)
662 return false;
663
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000664 spin_lock(&ring->irq_lock);
Daniel Vettere48d8632012-04-11 22:12:54 +0200665 if (ring->irq_refcount++ == 0)
666 i915_enable_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000667 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000668
669 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700670}
671
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800672static void
Daniel Vettere3670312012-04-11 22:12:53 +0200673i9xx_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700674{
Chris Wilson78501ea2010-10-27 12:18:21 +0100675 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000676 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700677
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000678 spin_lock(&ring->irq_lock);
Daniel Vettere48d8632012-04-11 22:12:54 +0200679 if (--ring->irq_refcount == 0)
680 i915_disable_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000681 spin_unlock(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700682}
683
Chris Wilson78501ea2010-10-27 12:18:21 +0100684void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800685{
Eric Anholt45930102011-05-06 17:12:35 -0700686 struct drm_device *dev = ring->dev;
Chris Wilson78501ea2010-10-27 12:18:21 +0100687 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700688 u32 mmio = 0;
689
690 /* The ring status page addresses are no longer next to the rest of
691 * the ring registers as of gen7.
692 */
693 if (IS_GEN7(dev)) {
694 switch (ring->id) {
Daniel Vetter96154f22011-12-14 13:57:00 +0100695 case RCS:
Eric Anholt45930102011-05-06 17:12:35 -0700696 mmio = RENDER_HWS_PGA_GEN7;
697 break;
Daniel Vetter96154f22011-12-14 13:57:00 +0100698 case BCS:
Eric Anholt45930102011-05-06 17:12:35 -0700699 mmio = BLT_HWS_PGA_GEN7;
700 break;
Daniel Vetter96154f22011-12-14 13:57:00 +0100701 case VCS:
Eric Anholt45930102011-05-06 17:12:35 -0700702 mmio = BSD_HWS_PGA_GEN7;
703 break;
704 }
705 } else if (IS_GEN6(ring->dev)) {
706 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
707 } else {
708 mmio = RING_HWS_PGA(ring->mmio_base);
709 }
710
Chris Wilson78501ea2010-10-27 12:18:21 +0100711 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
712 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800713}
714
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000715static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100716bsd_ring_flush(struct intel_ring_buffer *ring,
717 u32 invalidate_domains,
718 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800719{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000720 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000721
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000722 ret = intel_ring_begin(ring, 2);
723 if (ret)
724 return ret;
725
726 intel_ring_emit(ring, MI_FLUSH);
727 intel_ring_emit(ring, MI_NOOP);
728 intel_ring_advance(ring);
729 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800730}
731
Chris Wilson3cce4692010-10-27 16:11:02 +0100732static int
Daniel Vetter8620a3a2012-04-11 22:12:57 +0200733i9xx_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100734 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800735{
736 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100737 int ret;
738
739 ret = intel_ring_begin(ring, 4);
740 if (ret)
741 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100742
Daniel Vetter53d227f2012-01-25 16:32:49 +0100743 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson6f392d5482010-08-07 11:01:22 +0100744
Chris Wilson3cce4692010-10-27 16:11:02 +0100745 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
746 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
747 intel_ring_emit(ring, seqno);
748 intel_ring_emit(ring, MI_USER_INTERRUPT);
749 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800750
Chris Wilson3cce4692010-10-27 16:11:02 +0100751 *result = seqno;
752 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800753}
754
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000755static bool
Ben Widawsky25c06302012-03-29 19:11:27 -0700756gen6_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson0f468322011-01-04 17:35:21 +0000757{
758 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000759 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000760
761 if (!dev->irq_enabled)
762 return false;
763
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100764 /* It looks like we need to prevent the gt from suspending while waiting
765 * for an notifiy irq, otherwise irqs seem to get lost on at least the
766 * blt/bsd rings on ivb. */
Daniel Vetter99ffa162012-01-25 14:04:00 +0100767 gen6_gt_force_wake_get(dev_priv);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100768
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000769 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000770 if (ring->irq_refcount++ == 0) {
Daniel Vetter6a848cc2012-04-11 22:12:46 +0200771 I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
772 ironlake_enable_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson0f468322011-01-04 17:35:21 +0000773 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000774 spin_unlock(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000775
776 return true;
777}
778
779static void
Ben Widawsky25c06302012-03-29 19:11:27 -0700780gen6_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson0f468322011-01-04 17:35:21 +0000781{
782 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000783 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000784
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000785 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000786 if (--ring->irq_refcount == 0) {
Daniel Vetter6a848cc2012-04-11 22:12:46 +0200787 I915_WRITE_IMR(ring, ~0);
788 ironlake_disable_irq(dev_priv, ring->irq_enable_mask);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000789 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000790 spin_unlock(&ring->irq_lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100791
Daniel Vetter99ffa162012-01-25 14:04:00 +0100792 gen6_gt_force_wake_put(dev_priv);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000793}
794
Zou Nan haid1b851f2010-05-21 09:08:57 +0800795static int
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200796i965_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800797{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100798 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100799
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100800 ret = intel_ring_begin(ring, 2);
801 if (ret)
802 return ret;
803
Chris Wilson78501ea2010-10-27 12:18:21 +0100804 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000805 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100806 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000807 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100808 intel_ring_advance(ring);
809
Zou Nan haid1b851f2010-05-21 09:08:57 +0800810 return 0;
811}
812
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800813static int
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200814i830_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000815 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700816{
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000817 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700818
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200819 ret = intel_ring_begin(ring, 4);
820 if (ret)
821 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700822
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200823 intel_ring_emit(ring, MI_BATCH_BUFFER);
824 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
825 intel_ring_emit(ring, offset + len - 8);
826 intel_ring_emit(ring, 0);
827 intel_ring_advance(ring);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100828
Daniel Vetterfb3256d2012-04-11 22:12:56 +0200829 return 0;
830}
831
832static int
833i915_dispatch_execbuffer(struct intel_ring_buffer *ring,
834 u32 offset, u32 len)
835{
836 int ret;
837
838 ret = intel_ring_begin(ring, 2);
839 if (ret)
840 return ret;
841
842 intel_ring_emit(ring, MI_BATCH_BUFFER_START | (2 << 6));
843 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000844 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700845
Eric Anholt62fdfea2010-05-21 13:26:39 -0700846 return 0;
847}
848
Chris Wilson78501ea2010-10-27 12:18:21 +0100849static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700850{
Chris Wilson78501ea2010-10-27 12:18:21 +0100851 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000852 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700853
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800854 obj = ring->status_page.obj;
855 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700856 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700857
Chris Wilson05394f32010-11-08 19:18:58 +0000858 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700859 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000860 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800861 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700862
863 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700864}
865
Chris Wilson78501ea2010-10-27 12:18:21 +0100866static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700867{
Chris Wilson78501ea2010-10-27 12:18:21 +0100868 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700869 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000870 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700871 int ret;
872
Eric Anholt62fdfea2010-05-21 13:26:39 -0700873 obj = i915_gem_alloc_object(dev, 4096);
874 if (obj == NULL) {
875 DRM_ERROR("Failed to allocate status page\n");
876 ret = -ENOMEM;
877 goto err;
878 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100879
880 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700881
Daniel Vetter75e9e912010-11-04 17:11:09 +0100882 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700883 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700884 goto err_unref;
885 }
886
Chris Wilson05394f32010-11-08 19:18:58 +0000887 ring->status_page.gfx_addr = obj->gtt_offset;
888 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800889 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700890 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700891 goto err_unpin;
892 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800893 ring->status_page.obj = obj;
894 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700895
Chris Wilson78501ea2010-10-27 12:18:21 +0100896 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800897 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
898 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700899
900 return 0;
901
902err_unpin:
903 i915_gem_object_unpin(obj);
904err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000905 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700906err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800907 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700908}
909
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800910int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100911 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700912{
Chris Wilson05394f32010-11-08 19:18:58 +0000913 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100914 int ret;
915
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800916 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100917 INIT_LIST_HEAD(&ring->active_list);
918 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100919 INIT_LIST_HEAD(&ring->gpu_write_list);
Daniel Vetterdfc9ef22012-04-11 22:12:47 +0200920 ring->size = 32 * PAGE_SIZE;
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000921
Chris Wilsonb259f672011-03-29 13:19:09 +0100922 init_waitqueue_head(&ring->irq_queue);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000923 spin_lock_init(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700924
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800925 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100926 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800927 if (ret)
928 return ret;
929 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700930
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800931 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700932 if (obj == NULL) {
933 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800934 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +0100935 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700936 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700937
Chris Wilson05394f32010-11-08 19:18:58 +0000938 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800939
Daniel Vetter75e9e912010-11-04 17:11:09 +0100940 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +0100941 if (ret)
942 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700943
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800944 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +0000945 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700946 ring->map.type = 0;
947 ring->map.flags = 0;
948 ring->map.mtrr = 0;
949
950 drm_core_ioremap_wc(&ring->map, dev);
951 if (ring->map.handle == NULL) {
952 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800953 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100954 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700955 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800956
Eric Anholt62fdfea2010-05-21 13:26:39 -0700957 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +0100958 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +0100959 if (ret)
960 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700961
Chris Wilson55249ba2010-12-22 14:04:47 +0000962 /* Workaround an erratum on the i830 which causes a hang if
963 * the TAIL pointer points to within the last 2 cachelines
964 * of the buffer.
965 */
966 ring->effective_size = ring->size;
967 if (IS_I830(ring->dev))
968 ring->effective_size -= 128;
969
Chris Wilsonc584fe42010-10-29 18:15:52 +0100970 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +0100971
972err_unmap:
973 drm_core_ioremapfree(&ring->map, dev);
974err_unpin:
975 i915_gem_object_unpin(obj);
976err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000977 drm_gem_object_unreference(&obj->base);
978 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +0100979err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +0100980 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800981 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700982}
983
Chris Wilson78501ea2010-10-27 12:18:21 +0100984void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700985{
Chris Wilson33626e62010-10-29 16:18:36 +0100986 struct drm_i915_private *dev_priv;
987 int ret;
988
Chris Wilson05394f32010-11-08 19:18:58 +0000989 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700990 return;
991
Chris Wilson33626e62010-10-29 16:18:36 +0100992 /* Disable the ring buffer. The ring must be idle at this point */
993 dev_priv = ring->dev->dev_private;
Ben Widawsky96f298a2011-03-19 18:14:27 -0700994 ret = intel_wait_ring_idle(ring);
Chris Wilson29ee3992011-01-24 16:35:42 +0000995 if (ret)
996 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
997 ring->name, ret);
998
Chris Wilson33626e62010-10-29 16:18:36 +0100999 I915_WRITE_CTL(ring, 0);
1000
Chris Wilson78501ea2010-10-27 12:18:21 +01001001 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001002
Chris Wilson05394f32010-11-08 19:18:58 +00001003 i915_gem_object_unpin(ring->obj);
1004 drm_gem_object_unreference(&ring->obj->base);
1005 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +01001006
Zou Nan hai8d192152010-11-02 16:31:01 +08001007 if (ring->cleanup)
1008 ring->cleanup(ring);
1009
Chris Wilson78501ea2010-10-27 12:18:21 +01001010 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001011}
1012
Chris Wilson78501ea2010-10-27 12:18:21 +01001013static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001014{
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001015 unsigned int *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +00001016 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001017
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001018 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001019 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001020 if (ret)
1021 return ret;
1022 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001023
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001024 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +01001025 rem /= 8;
1026 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001027 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +01001028 *virt++ = MI_NOOP;
1029 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001030
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001031 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001032 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001033
1034 return 0;
1035}
1036
Chris Wilsona71d8d92012-02-15 11:25:36 +00001037static int intel_ring_wait_seqno(struct intel_ring_buffer *ring, u32 seqno)
1038{
1039 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1040 bool was_interruptible;
1041 int ret;
1042
1043 /* XXX As we have not yet audited all the paths to check that
1044 * they are ready for ERESTARTSYS from intel_ring_begin, do not
1045 * allow us to be interruptible by a signal.
1046 */
1047 was_interruptible = dev_priv->mm.interruptible;
1048 dev_priv->mm.interruptible = false;
1049
1050 ret = i915_wait_request(ring, seqno, true);
1051
1052 dev_priv->mm.interruptible = was_interruptible;
1053
1054 return ret;
1055}
1056
1057static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
1058{
1059 struct drm_i915_gem_request *request;
1060 u32 seqno = 0;
1061 int ret;
1062
1063 i915_gem_retire_requests_ring(ring);
1064
1065 if (ring->last_retired_head != -1) {
1066 ring->head = ring->last_retired_head;
1067 ring->last_retired_head = -1;
1068 ring->space = ring_space(ring);
1069 if (ring->space >= n)
1070 return 0;
1071 }
1072
1073 list_for_each_entry(request, &ring->request_list, list) {
1074 int space;
1075
1076 if (request->tail == -1)
1077 continue;
1078
1079 space = request->tail - (ring->tail + 8);
1080 if (space < 0)
1081 space += ring->size;
1082 if (space >= n) {
1083 seqno = request->seqno;
1084 break;
1085 }
1086
1087 /* Consume this request in case we need more space than
1088 * is available and so need to prevent a race between
1089 * updating last_retired_head and direct reads of
1090 * I915_RING_HEAD. It also provides a nice sanity check.
1091 */
1092 request->tail = -1;
1093 }
1094
1095 if (seqno == 0)
1096 return -ENOSPC;
1097
1098 ret = intel_ring_wait_seqno(ring, seqno);
1099 if (ret)
1100 return ret;
1101
1102 if (WARN_ON(ring->last_retired_head == -1))
1103 return -ENOSPC;
1104
1105 ring->head = ring->last_retired_head;
1106 ring->last_retired_head = -1;
1107 ring->space = ring_space(ring);
1108 if (WARN_ON(ring->space < n))
1109 return -ENOSPC;
1110
1111 return 0;
1112}
1113
Chris Wilson78501ea2010-10-27 12:18:21 +01001114int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001115{
Chris Wilson78501ea2010-10-27 12:18:21 +01001116 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001117 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001118 unsigned long end;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001119 int ret;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001120
Chris Wilsona71d8d92012-02-15 11:25:36 +00001121 ret = intel_ring_wait_request(ring, n);
1122 if (ret != -ENOSPC)
1123 return ret;
1124
Chris Wilsondb53a302011-02-03 11:57:46 +00001125 trace_i915_ring_wait_begin(ring);
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001126 if (drm_core_check_feature(dev, DRIVER_GEM))
1127 /* With GEM the hangcheck timer should kick us out of the loop,
1128 * leaving it early runs the risk of corrupting GEM state (due
1129 * to running on almost untested codepaths). But on resume
1130 * timers don't work yet, so prevent a complete hang in that
1131 * case by choosing an insanely large timeout. */
1132 end = jiffies + 60 * HZ;
1133 else
1134 end = jiffies + 3 * HZ;
1135
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001136 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001137 ring->head = I915_READ_HEAD(ring);
1138 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001139 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001140 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001141 return 0;
1142 }
1143
1144 if (dev->primary->master) {
1145 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1146 if (master_priv->sarea_priv)
1147 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1148 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001149
Chris Wilsone60a0b12010-10-13 10:09:14 +01001150 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +01001151 if (atomic_read(&dev_priv->mm.wedged))
1152 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001153 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +00001154 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001155 return -EBUSY;
1156}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001157
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001158int intel_ring_begin(struct intel_ring_buffer *ring,
1159 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001160{
Chris Wilson21dd3732011-01-26 15:55:56 +00001161 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +08001162 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001163 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001164
Chris Wilson21dd3732011-01-26 15:55:56 +00001165 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
1166 return -EIO;
1167
Chris Wilson55249ba2010-12-22 14:04:47 +00001168 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001169 ret = intel_wrap_ring_buffer(ring);
1170 if (unlikely(ret))
1171 return ret;
1172 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001173
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001174 if (unlikely(ring->space < n)) {
1175 ret = intel_wait_ring_buffer(ring, n);
1176 if (unlikely(ret))
1177 return ret;
1178 }
Chris Wilsond97ed332010-08-04 15:18:13 +01001179
1180 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001181 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001182}
1183
Chris Wilson78501ea2010-10-27 12:18:21 +01001184void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001185{
Chris Wilsond97ed332010-08-04 15:18:13 +01001186 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +01001187 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001188}
1189
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001190
Chris Wilson78501ea2010-10-27 12:18:21 +01001191static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001192 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001193{
Akshay Joshi0206e352011-08-16 15:34:10 -04001194 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001195
1196 /* Every tail move must follow the sequence below */
Akshay Joshi0206e352011-08-16 15:34:10 -04001197 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1198 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1199 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1200 I915_WRITE(GEN6_BSD_RNCID, 0x0);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001201
Akshay Joshi0206e352011-08-16 15:34:10 -04001202 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1203 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1204 50))
1205 DRM_ERROR("timed out waiting for IDLE Indicator\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001206
Akshay Joshi0206e352011-08-16 15:34:10 -04001207 I915_WRITE_TAIL(ring, value);
1208 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1209 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1210 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001211}
1212
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001213static int gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001214 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001215{
Chris Wilson71a77e02011-02-02 12:13:49 +00001216 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001217 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001218
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001219 ret = intel_ring_begin(ring, 4);
1220 if (ret)
1221 return ret;
1222
Chris Wilson71a77e02011-02-02 12:13:49 +00001223 cmd = MI_FLUSH_DW;
1224 if (invalidate & I915_GEM_GPU_DOMAINS)
1225 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1226 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001227 intel_ring_emit(ring, 0);
1228 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001229 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001230 intel_ring_advance(ring);
1231 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001232}
1233
1234static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001235gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001236 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001237{
Akshay Joshi0206e352011-08-16 15:34:10 -04001238 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001239
Akshay Joshi0206e352011-08-16 15:34:10 -04001240 ret = intel_ring_begin(ring, 2);
1241 if (ret)
1242 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001243
Akshay Joshi0206e352011-08-16 15:34:10 -04001244 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1245 /* bit0-7 is the length on GEN6+ */
1246 intel_ring_emit(ring, offset);
1247 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001248
Akshay Joshi0206e352011-08-16 15:34:10 -04001249 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001250}
1251
Chris Wilson549f7362010-10-19 11:19:32 +01001252/* Blitter support (SandyBridge+) */
1253
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001254static int blt_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001255 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001256{
Chris Wilson71a77e02011-02-02 12:13:49 +00001257 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001258 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001259
Daniel Vetter6a233c72011-12-14 13:57:07 +01001260 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001261 if (ret)
1262 return ret;
1263
Chris Wilson71a77e02011-02-02 12:13:49 +00001264 cmd = MI_FLUSH_DW;
1265 if (invalidate & I915_GEM_DOMAIN_RENDER)
1266 cmd |= MI_INVALIDATE_TLB;
1267 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001268 intel_ring_emit(ring, 0);
1269 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001270 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001271 intel_ring_advance(ring);
1272 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001273}
1274
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001275int intel_init_render_ring_buffer(struct drm_device *dev)
1276{
1277 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001278 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001279
Daniel Vetter59465b52012-04-11 22:12:48 +02001280 ring->name = "render ring";
1281 ring->id = RCS;
1282 ring->mmio_base = RENDER_RING_BASE;
1283
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001284 if (INTEL_INFO(dev)->gen >= 6) {
1285 ring->add_request = gen6_add_request;
Jesse Barnes8d315282011-10-16 10:23:31 +02001286 ring->flush = gen6_render_ring_flush;
Ben Widawsky25c06302012-03-29 19:11:27 -07001287 ring->irq_get = gen6_ring_get_irq;
1288 ring->irq_put = gen6_ring_put_irq;
Daniel Vetter6a848cc2012-04-11 22:12:46 +02001289 ring->irq_enable_mask = GT_USER_INTERRUPT;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001290 ring->get_seqno = gen6_ring_get_seqno;
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001291 ring->sync_to = gen6_ring_sync;
Daniel Vetter59465b52012-04-11 22:12:48 +02001292 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_INVALID;
1293 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_RV;
1294 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_RB;
1295 ring->signal_mbox[0] = GEN6_VRSYNC;
1296 ring->signal_mbox[1] = GEN6_BRSYNC;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001297 } else if (IS_GEN5(dev)) {
1298 ring->add_request = pc_render_add_request;
Daniel Vetter59465b52012-04-11 22:12:48 +02001299 ring->flush = render_ring_flush;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001300 ring->get_seqno = pc_render_get_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02001301 ring->irq_get = gen5_ring_get_irq;
1302 ring->irq_put = gen5_ring_put_irq;
Daniel Vettere3670312012-04-11 22:12:53 +02001303 ring->irq_enable_mask = GT_USER_INTERRUPT | GT_PIPE_NOTIFY;
Daniel Vetter59465b52012-04-11 22:12:48 +02001304 } else {
Daniel Vetter8620a3a2012-04-11 22:12:57 +02001305 ring->add_request = i9xx_add_request;
Daniel Vetter59465b52012-04-11 22:12:48 +02001306 ring->flush = render_ring_flush;
1307 ring->get_seqno = ring_get_seqno;
Daniel Vettere3670312012-04-11 22:12:53 +02001308 ring->irq_get = i9xx_ring_get_irq;
1309 ring->irq_put = i9xx_ring_put_irq;
1310 ring->irq_enable_mask = I915_USER_INTERRUPT;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001311 }
Daniel Vetter59465b52012-04-11 22:12:48 +02001312 ring->write_tail = ring_write_tail;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001313 if (INTEL_INFO(dev)->gen >= 6)
1314 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
1315 else if (INTEL_INFO(dev)->gen >= 4)
1316 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1317 else if (IS_I830(dev) || IS_845G(dev))
1318 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1319 else
1320 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02001321 ring->init = init_render_ring;
1322 ring->cleanup = render_ring_cleanup;
1323
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001324
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001325 if (!I915_NEED_GFX_HWS(dev)) {
1326 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1327 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1328 }
1329
1330 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001331}
1332
Chris Wilsone8616b62011-01-20 09:57:11 +00001333int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1334{
1335 drm_i915_private_t *dev_priv = dev->dev_private;
1336 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1337
Daniel Vetter59465b52012-04-11 22:12:48 +02001338 ring->name = "render ring";
1339 ring->id = RCS;
1340 ring->mmio_base = RENDER_RING_BASE;
1341
Chris Wilsone8616b62011-01-20 09:57:11 +00001342 if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetterb4178f82012-04-11 22:12:51 +02001343 /* non-kms not supported on gen6+ */
1344 return -ENODEV;
Chris Wilsone8616b62011-01-20 09:57:11 +00001345 }
Daniel Vetter28f0cbf2012-04-11 22:12:58 +02001346
1347 /* Note: gem is not supported on gen5/ilk without kms (the corresponding
1348 * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
1349 * the special gen5 functions. */
1350 ring->add_request = i9xx_add_request;
1351 ring->flush = render_ring_flush;
1352 ring->get_seqno = ring_get_seqno;
1353 ring->irq_get = i9xx_ring_get_irq;
1354 ring->irq_put = i9xx_ring_put_irq;
1355 ring->irq_enable_mask = I915_USER_INTERRUPT;
Daniel Vetter59465b52012-04-11 22:12:48 +02001356 ring->write_tail = ring_write_tail;
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001357 if (INTEL_INFO(dev)->gen >= 4)
1358 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
1359 else if (IS_I830(dev) || IS_845G(dev))
1360 ring->dispatch_execbuffer = i830_dispatch_execbuffer;
1361 else
1362 ring->dispatch_execbuffer = i915_dispatch_execbuffer;
Daniel Vetter59465b52012-04-11 22:12:48 +02001363 ring->init = init_render_ring;
1364 ring->cleanup = render_ring_cleanup;
Chris Wilsone8616b62011-01-20 09:57:11 +00001365
Keith Packardf3234702011-07-22 10:44:39 -07001366 if (!I915_NEED_GFX_HWS(dev))
1367 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1368
Chris Wilsone8616b62011-01-20 09:57:11 +00001369 ring->dev = dev;
1370 INIT_LIST_HEAD(&ring->active_list);
1371 INIT_LIST_HEAD(&ring->request_list);
1372 INIT_LIST_HEAD(&ring->gpu_write_list);
1373
1374 ring->size = size;
1375 ring->effective_size = ring->size;
1376 if (IS_I830(ring->dev))
1377 ring->effective_size -= 128;
1378
1379 ring->map.offset = start;
1380 ring->map.size = size;
1381 ring->map.type = 0;
1382 ring->map.flags = 0;
1383 ring->map.mtrr = 0;
1384
1385 drm_core_ioremap_wc(&ring->map, dev);
1386 if (ring->map.handle == NULL) {
1387 DRM_ERROR("can not ioremap virtual address for"
1388 " ring buffer\n");
1389 return -ENOMEM;
1390 }
1391
1392 ring->virtual_start = (void __force __iomem *)ring->map.handle;
1393 return 0;
1394}
1395
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001396int intel_init_bsd_ring_buffer(struct drm_device *dev)
1397{
1398 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001399 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001400
Daniel Vetter58fa3832012-04-11 22:12:49 +02001401 ring->name = "bsd ring";
1402 ring->id = VCS;
1403
Daniel Vetter0fd2c202012-04-11 22:12:55 +02001404 ring->write_tail = ring_write_tail;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001405 if (IS_GEN6(dev) || IS_GEN7(dev)) {
1406 ring->mmio_base = GEN6_BSD_RING_BASE;
Daniel Vetter0fd2c202012-04-11 22:12:55 +02001407 /* gen6 bsd needs a special wa for tail updates */
1408 if (IS_GEN6(dev))
1409 ring->write_tail = gen6_bsd_ring_write_tail;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001410 ring->flush = gen6_ring_flush;
1411 ring->add_request = gen6_add_request;
1412 ring->get_seqno = gen6_ring_get_seqno;
1413 ring->irq_enable_mask = GEN6_BSD_USER_INTERRUPT;
1414 ring->irq_get = gen6_ring_get_irq;
1415 ring->irq_put = gen6_ring_put_irq;
1416 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001417 ring->sync_to = gen6_ring_sync;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001418 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_VR;
1419 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_INVALID;
1420 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_VB;
1421 ring->signal_mbox[0] = GEN6_RVSYNC;
1422 ring->signal_mbox[1] = GEN6_BVSYNC;
1423 } else {
1424 ring->mmio_base = BSD_RING_BASE;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001425 ring->flush = bsd_ring_flush;
Daniel Vetter8620a3a2012-04-11 22:12:57 +02001426 ring->add_request = i9xx_add_request;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001427 ring->get_seqno = ring_get_seqno;
Daniel Vettere48d8632012-04-11 22:12:54 +02001428 if (IS_GEN5(dev)) {
Daniel Vettere3670312012-04-11 22:12:53 +02001429 ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02001430 ring->irq_get = gen5_ring_get_irq;
1431 ring->irq_put = gen5_ring_put_irq;
1432 } else {
Daniel Vettere3670312012-04-11 22:12:53 +02001433 ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
Daniel Vettere48d8632012-04-11 22:12:54 +02001434 ring->irq_get = i9xx_ring_get_irq;
1435 ring->irq_put = i9xx_ring_put_irq;
1436 }
Daniel Vetterfb3256d2012-04-11 22:12:56 +02001437 ring->dispatch_execbuffer = i965_dispatch_execbuffer;
Daniel Vetter58fa3832012-04-11 22:12:49 +02001438 }
1439 ring->init = init_ring_common;
1440
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001441
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001442 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001443}
Chris Wilson549f7362010-10-19 11:19:32 +01001444
1445int intel_init_blt_ring_buffer(struct drm_device *dev)
1446{
1447 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001448 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001449
Daniel Vetter3535d9d2012-04-11 22:12:50 +02001450 ring->name = "blitter ring";
1451 ring->id = BCS;
1452
1453 ring->mmio_base = BLT_RING_BASE;
1454 ring->write_tail = ring_write_tail;
1455 ring->flush = blt_ring_flush;
1456 ring->add_request = gen6_add_request;
1457 ring->get_seqno = gen6_ring_get_seqno;
1458 ring->irq_enable_mask = GEN6_BLITTER_USER_INTERRUPT;
1459 ring->irq_get = gen6_ring_get_irq;
1460 ring->irq_put = gen6_ring_put_irq;
1461 ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
Daniel Vetter686cb5f2012-04-11 22:12:52 +02001462 ring->sync_to = gen6_ring_sync;
Daniel Vetter3535d9d2012-04-11 22:12:50 +02001463 ring->semaphore_register[0] = MI_SEMAPHORE_SYNC_BR;
1464 ring->semaphore_register[1] = MI_SEMAPHORE_SYNC_BV;
1465 ring->semaphore_register[2] = MI_SEMAPHORE_SYNC_INVALID;
1466 ring->signal_mbox[0] = GEN6_RBSYNC;
1467 ring->signal_mbox[1] = GEN6_VBSYNC;
1468 ring->init = init_ring_common;
Chris Wilson549f7362010-10-19 11:19:32 +01001469
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001470 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001471}