blob: e784ebb8cc27285c4f9315e16f9acde4eedbfbfb [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 Wilson6aa56062010-10-29 21:44:37 +0100290 | RING_REPORT_64K | 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 */
Chris Wilson176f28e2010-10-28 11:18:07 +0100293 if ((I915_READ_CTL(ring) & RING_VALID) == 0 ||
Chris Wilson05394f32010-11-08 19:18:58 +0000294 I915_READ_START(ring) != obj->gtt_offset ||
Chris Wilson176f28e2010-10-28 11:18:07 +0100295 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
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
475intel_ring_sync(struct intel_ring_buffer *waiter,
476 struct intel_ring_buffer *signaller,
477 int ring,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000478 u32 seqno)
479{
480 int ret;
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700481 u32 dw1 = MI_SEMAPHORE_MBOX |
482 MI_SEMAPHORE_COMPARE |
483 MI_SEMAPHORE_REGISTER;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000484
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700485 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000486 if (ret)
487 return ret;
488
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700489 intel_ring_emit(waiter, dw1 | signaller->semaphore_register[ring]);
490 intel_ring_emit(waiter, seqno);
491 intel_ring_emit(waiter, 0);
492 intel_ring_emit(waiter, MI_NOOP);
493 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000494
495 return 0;
496}
497
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700498/* VCS->RCS (RVSYNC) or BCS->RCS (RBSYNC) */
499int
500render_ring_sync_to(struct intel_ring_buffer *waiter,
501 struct intel_ring_buffer *signaller,
502 u32 seqno)
503{
504 WARN_ON(signaller->semaphore_register[RCS] == MI_SEMAPHORE_SYNC_INVALID);
505 return intel_ring_sync(waiter,
506 signaller,
507 RCS,
508 seqno);
509}
510
511/* RCS->VCS (VRSYNC) or BCS->VCS (VBSYNC) */
512int
513gen6_bsd_ring_sync_to(struct intel_ring_buffer *waiter,
514 struct intel_ring_buffer *signaller,
515 u32 seqno)
516{
517 WARN_ON(signaller->semaphore_register[VCS] == MI_SEMAPHORE_SYNC_INVALID);
518 return intel_ring_sync(waiter,
519 signaller,
520 VCS,
521 seqno);
522}
523
524/* RCS->BCS (BRSYNC) or VCS->BCS (BVSYNC) */
525int
526gen6_blt_ring_sync_to(struct intel_ring_buffer *waiter,
527 struct intel_ring_buffer *signaller,
528 u32 seqno)
529{
530 WARN_ON(signaller->semaphore_register[BCS] == MI_SEMAPHORE_SYNC_INVALID);
531 return intel_ring_sync(waiter,
532 signaller,
533 BCS,
534 seqno);
535}
536
537
538
Chris Wilsonc6df5412010-12-15 09:56:50 +0000539#define PIPE_CONTROL_FLUSH(ring__, addr__) \
540do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200541 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
542 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000543 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
544 intel_ring_emit(ring__, 0); \
545 intel_ring_emit(ring__, 0); \
546} while (0)
547
548static int
549pc_render_add_request(struct intel_ring_buffer *ring,
550 u32 *result)
551{
Daniel Vetter53d227f2012-01-25 16:32:49 +0100552 u32 seqno = i915_gem_next_request_seqno(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000553 struct pipe_control *pc = ring->private;
554 u32 scratch_addr = pc->gtt_offset + 128;
555 int ret;
556
557 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
558 * incoherent with writes to memory, i.e. completely fubar,
559 * so we need to use PIPE_NOTIFY instead.
560 *
561 * However, we also need to workaround the qword write
562 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
563 * memory before requesting an interrupt.
564 */
565 ret = intel_ring_begin(ring, 32);
566 if (ret)
567 return ret;
568
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200569 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200570 PIPE_CONTROL_WRITE_FLUSH |
571 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000572 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
573 intel_ring_emit(ring, seqno);
574 intel_ring_emit(ring, 0);
575 PIPE_CONTROL_FLUSH(ring, scratch_addr);
576 scratch_addr += 128; /* write to separate cachelines */
577 PIPE_CONTROL_FLUSH(ring, scratch_addr);
578 scratch_addr += 128;
579 PIPE_CONTROL_FLUSH(ring, scratch_addr);
580 scratch_addr += 128;
581 PIPE_CONTROL_FLUSH(ring, scratch_addr);
582 scratch_addr += 128;
583 PIPE_CONTROL_FLUSH(ring, scratch_addr);
584 scratch_addr += 128;
585 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200586 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200587 PIPE_CONTROL_WRITE_FLUSH |
588 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000589 PIPE_CONTROL_NOTIFY);
590 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
591 intel_ring_emit(ring, seqno);
592 intel_ring_emit(ring, 0);
593 intel_ring_advance(ring);
594
595 *result = seqno;
596 return 0;
597}
598
Chris Wilson3cce4692010-10-27 16:11:02 +0100599static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100600render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100601 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700602{
Daniel Vetter53d227f2012-01-25 16:32:49 +0100603 u32 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson3cce4692010-10-27 16:11:02 +0100604 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800605
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000606 ret = intel_ring_begin(ring, 4);
607 if (ret)
608 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100609
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000610 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
611 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
612 intel_ring_emit(ring, seqno);
613 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100614 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000615
Chris Wilson3cce4692010-10-27 16:11:02 +0100616 *result = seqno;
617 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700618}
619
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800620static u32
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100621gen6_ring_get_seqno(struct intel_ring_buffer *ring)
622{
623 struct drm_device *dev = ring->dev;
624
625 /* Workaround to force correct ordering between irq and seqno writes on
626 * ivb (and maybe also on snb) by reading from a CS register (like
627 * ACTHD) before reading the status page. */
628 if (IS_GEN7(dev))
629 intel_ring_get_active_head(ring);
630 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
631}
632
633static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000634ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800635{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000636 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
637}
638
Chris Wilsonc6df5412010-12-15 09:56:50 +0000639static u32
640pc_render_get_seqno(struct intel_ring_buffer *ring)
641{
642 struct pipe_control *pc = ring->private;
643 return pc->cpu_page[0];
644}
645
Chris Wilson0f468322011-01-04 17:35:21 +0000646static void
647ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
648{
649 dev_priv->gt_irq_mask &= ~mask;
650 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
651 POSTING_READ(GTIMR);
652}
653
654static void
655ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
656{
657 dev_priv->gt_irq_mask |= mask;
658 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
659 POSTING_READ(GTIMR);
660}
661
662static void
663i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
664{
665 dev_priv->irq_mask &= ~mask;
666 I915_WRITE(IMR, dev_priv->irq_mask);
667 POSTING_READ(IMR);
668}
669
670static void
671i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
672{
673 dev_priv->irq_mask |= mask;
674 I915_WRITE(IMR, dev_priv->irq_mask);
675 POSTING_READ(IMR);
676}
677
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000678static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000679render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700680{
Chris Wilson78501ea2010-10-27 12:18:21 +0100681 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000682 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700683
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000684 if (!dev->irq_enabled)
685 return false;
686
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000687 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000688 if (ring->irq_refcount++ == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700689 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000690 ironlake_enable_irq(dev_priv,
691 GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700692 else
693 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
694 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000695 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000696
697 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700698}
699
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800700static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000701render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700702{
Chris Wilson78501ea2010-10-27 12:18:21 +0100703 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000704 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700705
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000706 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000707 if (--ring->irq_refcount == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700708 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000709 ironlake_disable_irq(dev_priv,
710 GT_USER_INTERRUPT |
711 GT_PIPE_NOTIFY);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700712 else
713 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
714 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000715 spin_unlock(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700716}
717
Chris Wilson78501ea2010-10-27 12:18:21 +0100718void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800719{
Eric Anholt45930102011-05-06 17:12:35 -0700720 struct drm_device *dev = ring->dev;
Chris Wilson78501ea2010-10-27 12:18:21 +0100721 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700722 u32 mmio = 0;
723
724 /* The ring status page addresses are no longer next to the rest of
725 * the ring registers as of gen7.
726 */
727 if (IS_GEN7(dev)) {
728 switch (ring->id) {
Daniel Vetter96154f22011-12-14 13:57:00 +0100729 case RCS:
Eric Anholt45930102011-05-06 17:12:35 -0700730 mmio = RENDER_HWS_PGA_GEN7;
731 break;
Daniel Vetter96154f22011-12-14 13:57:00 +0100732 case BCS:
Eric Anholt45930102011-05-06 17:12:35 -0700733 mmio = BLT_HWS_PGA_GEN7;
734 break;
Daniel Vetter96154f22011-12-14 13:57:00 +0100735 case VCS:
Eric Anholt45930102011-05-06 17:12:35 -0700736 mmio = BSD_HWS_PGA_GEN7;
737 break;
738 }
739 } else if (IS_GEN6(ring->dev)) {
740 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
741 } else {
742 mmio = RING_HWS_PGA(ring->mmio_base);
743 }
744
Chris Wilson78501ea2010-10-27 12:18:21 +0100745 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
746 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800747}
748
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000749static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100750bsd_ring_flush(struct intel_ring_buffer *ring,
751 u32 invalidate_domains,
752 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800753{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000754 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000755
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000756 ret = intel_ring_begin(ring, 2);
757 if (ret)
758 return ret;
759
760 intel_ring_emit(ring, MI_FLUSH);
761 intel_ring_emit(ring, MI_NOOP);
762 intel_ring_advance(ring);
763 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800764}
765
Chris Wilson3cce4692010-10-27 16:11:02 +0100766static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100767ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100768 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800769{
770 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100771 int ret;
772
773 ret = intel_ring_begin(ring, 4);
774 if (ret)
775 return ret;
Chris Wilson6f392d5482010-08-07 11:01:22 +0100776
Daniel Vetter53d227f2012-01-25 16:32:49 +0100777 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson6f392d5482010-08-07 11:01:22 +0100778
Chris Wilson3cce4692010-10-27 16:11:02 +0100779 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
780 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
781 intel_ring_emit(ring, seqno);
782 intel_ring_emit(ring, MI_USER_INTERRUPT);
783 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800784
Chris Wilson3cce4692010-10-27 16:11:02 +0100785 *result = seqno;
786 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800787}
788
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000789static bool
Chris Wilson0f468322011-01-04 17:35:21 +0000790gen6_ring_get_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
791{
792 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000793 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000794
795 if (!dev->irq_enabled)
796 return false;
797
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100798 /* It looks like we need to prevent the gt from suspending while waiting
799 * for an notifiy irq, otherwise irqs seem to get lost on at least the
800 * blt/bsd rings on ivb. */
Daniel Vetter99ffa162012-01-25 14:04:00 +0100801 gen6_gt_force_wake_get(dev_priv);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100802
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000803 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000804 if (ring->irq_refcount++ == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000805 ring->irq_mask &= ~rflag;
806 I915_WRITE_IMR(ring, ring->irq_mask);
807 ironlake_enable_irq(dev_priv, gflag);
Chris Wilson0f468322011-01-04 17:35:21 +0000808 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000809 spin_unlock(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000810
811 return true;
812}
813
814static void
815gen6_ring_put_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
816{
817 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000818 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000819
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000820 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000821 if (--ring->irq_refcount == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000822 ring->irq_mask |= rflag;
823 I915_WRITE_IMR(ring, ring->irq_mask);
824 ironlake_disable_irq(dev_priv, gflag);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000825 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000826 spin_unlock(&ring->irq_lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100827
Daniel Vetter99ffa162012-01-25 14:04:00 +0100828 gen6_gt_force_wake_put(dev_priv);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000829}
830
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000831static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000832bsd_ring_get_irq(struct intel_ring_buffer *ring)
833{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800834 struct drm_device *dev = ring->dev;
835 drm_i915_private_t *dev_priv = dev->dev_private;
836
837 if (!dev->irq_enabled)
838 return false;
839
840 spin_lock(&ring->irq_lock);
841 if (ring->irq_refcount++ == 0) {
842 if (IS_G4X(dev))
843 i915_enable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
844 else
845 ironlake_enable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
846 }
847 spin_unlock(&ring->irq_lock);
848
849 return true;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000850}
851static void
852bsd_ring_put_irq(struct intel_ring_buffer *ring)
853{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800854 struct drm_device *dev = ring->dev;
855 drm_i915_private_t *dev_priv = dev->dev_private;
856
857 spin_lock(&ring->irq_lock);
858 if (--ring->irq_refcount == 0) {
859 if (IS_G4X(dev))
860 i915_disable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
861 else
862 ironlake_disable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
863 }
864 spin_unlock(&ring->irq_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800865}
866
867static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000868ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800869{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100870 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100871
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100872 ret = intel_ring_begin(ring, 2);
873 if (ret)
874 return ret;
875
Chris Wilson78501ea2010-10-27 12:18:21 +0100876 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000877 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100878 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000879 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100880 intel_ring_advance(ring);
881
Zou Nan haid1b851f2010-05-21 09:08:57 +0800882 return 0;
883}
884
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800885static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100886render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000887 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700888{
Chris Wilson78501ea2010-10-27 12:18:21 +0100889 struct drm_device *dev = ring->dev;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000890 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700891
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000892 if (IS_I830(dev) || IS_845G(dev)) {
893 ret = intel_ring_begin(ring, 4);
894 if (ret)
895 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700896
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000897 intel_ring_emit(ring, MI_BATCH_BUFFER);
898 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
899 intel_ring_emit(ring, offset + len - 8);
900 intel_ring_emit(ring, 0);
901 } else {
902 ret = intel_ring_begin(ring, 2);
903 if (ret)
904 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100905
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000906 if (INTEL_INFO(dev)->gen >= 4) {
907 intel_ring_emit(ring,
908 MI_BATCH_BUFFER_START | (2 << 6) |
909 MI_BATCH_NON_SECURE_I965);
910 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700911 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000912 intel_ring_emit(ring,
913 MI_BATCH_BUFFER_START | (2 << 6));
914 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700915 }
916 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000917 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700918
Eric Anholt62fdfea2010-05-21 13:26:39 -0700919 return 0;
920}
921
Chris Wilson78501ea2010-10-27 12:18:21 +0100922static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700923{
Chris Wilson78501ea2010-10-27 12:18:21 +0100924 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000925 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700926
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800927 obj = ring->status_page.obj;
928 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700929 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700930
Chris Wilson05394f32010-11-08 19:18:58 +0000931 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700932 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000933 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800934 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700935
936 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700937}
938
Chris Wilson78501ea2010-10-27 12:18:21 +0100939static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700940{
Chris Wilson78501ea2010-10-27 12:18:21 +0100941 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700942 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000943 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700944 int ret;
945
Eric Anholt62fdfea2010-05-21 13:26:39 -0700946 obj = i915_gem_alloc_object(dev, 4096);
947 if (obj == NULL) {
948 DRM_ERROR("Failed to allocate status page\n");
949 ret = -ENOMEM;
950 goto err;
951 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100952
953 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700954
Daniel Vetter75e9e912010-11-04 17:11:09 +0100955 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700956 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700957 goto err_unref;
958 }
959
Chris Wilson05394f32010-11-08 19:18:58 +0000960 ring->status_page.gfx_addr = obj->gtt_offset;
961 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800962 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700963 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700964 goto err_unpin;
965 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800966 ring->status_page.obj = obj;
967 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700968
Chris Wilson78501ea2010-10-27 12:18:21 +0100969 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800970 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
971 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700972
973 return 0;
974
975err_unpin:
976 i915_gem_object_unpin(obj);
977err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000978 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700979err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800980 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700981}
982
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800983int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100984 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700985{
Chris Wilson05394f32010-11-08 19:18:58 +0000986 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100987 int ret;
988
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800989 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +0100990 INIT_LIST_HEAD(&ring->active_list);
991 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +0100992 INIT_LIST_HEAD(&ring->gpu_write_list);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000993
Chris Wilsonb259f672011-03-29 13:19:09 +0100994 init_waitqueue_head(&ring->irq_queue);
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000995 spin_lock_init(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000996 ring->irq_mask = ~0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700997
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800998 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100999 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001000 if (ret)
1001 return ret;
1002 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001003
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001004 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001005 if (obj == NULL) {
1006 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001007 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +01001008 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001009 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001010
Chris Wilson05394f32010-11-08 19:18:58 +00001011 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001012
Daniel Vetter75e9e912010-11-04 17:11:09 +01001013 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +01001014 if (ret)
1015 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001016
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001017 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +00001018 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001019 ring->map.type = 0;
1020 ring->map.flags = 0;
1021 ring->map.mtrr = 0;
1022
1023 drm_core_ioremap_wc(&ring->map, dev);
1024 if (ring->map.handle == NULL) {
1025 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001026 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +01001027 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001028 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001029
Eric Anholt62fdfea2010-05-21 13:26:39 -07001030 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +01001031 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +01001032 if (ret)
1033 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001034
Chris Wilson55249ba2010-12-22 14:04:47 +00001035 /* Workaround an erratum on the i830 which causes a hang if
1036 * the TAIL pointer points to within the last 2 cachelines
1037 * of the buffer.
1038 */
1039 ring->effective_size = ring->size;
1040 if (IS_I830(ring->dev))
1041 ring->effective_size -= 128;
1042
Chris Wilsonc584fe42010-10-29 18:15:52 +01001043 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +01001044
1045err_unmap:
1046 drm_core_ioremapfree(&ring->map, dev);
1047err_unpin:
1048 i915_gem_object_unpin(obj);
1049err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001050 drm_gem_object_unreference(&obj->base);
1051 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +01001052err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +01001053 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001054 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001055}
1056
Chris Wilson78501ea2010-10-27 12:18:21 +01001057void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001058{
Chris Wilson33626e62010-10-29 16:18:36 +01001059 struct drm_i915_private *dev_priv;
1060 int ret;
1061
Chris Wilson05394f32010-11-08 19:18:58 +00001062 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001063 return;
1064
Chris Wilson33626e62010-10-29 16:18:36 +01001065 /* Disable the ring buffer. The ring must be idle at this point */
1066 dev_priv = ring->dev->dev_private;
Ben Widawsky96f298a2011-03-19 18:14:27 -07001067 ret = intel_wait_ring_idle(ring);
Chris Wilson29ee3992011-01-24 16:35:42 +00001068 if (ret)
1069 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
1070 ring->name, ret);
1071
Chris Wilson33626e62010-10-29 16:18:36 +01001072 I915_WRITE_CTL(ring, 0);
1073
Chris Wilson78501ea2010-10-27 12:18:21 +01001074 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001075
Chris Wilson05394f32010-11-08 19:18:58 +00001076 i915_gem_object_unpin(ring->obj);
1077 drm_gem_object_unreference(&ring->obj->base);
1078 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +01001079
Zou Nan hai8d192152010-11-02 16:31:01 +08001080 if (ring->cleanup)
1081 ring->cleanup(ring);
1082
Chris Wilson78501ea2010-10-27 12:18:21 +01001083 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001084}
1085
Chris Wilson78501ea2010-10-27 12:18:21 +01001086static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001087{
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001088 unsigned int *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +00001089 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001090
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001091 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001092 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001093 if (ret)
1094 return ret;
1095 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001096
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001097 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +01001098 rem /= 8;
1099 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001100 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +01001101 *virt++ = MI_NOOP;
1102 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001103
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001104 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001105 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001106
1107 return 0;
1108}
1109
Chris Wilson78501ea2010-10-27 12:18:21 +01001110int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001111{
Chris Wilson78501ea2010-10-27 12:18:21 +01001112 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001113 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001114 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +01001115 u32 head;
1116
Chris Wilsonc7dca472011-01-20 17:00:10 +00001117 /* If the reported head position has wrapped or hasn't advanced,
1118 * fallback to the slow and accurate path.
1119 */
1120 head = intel_read_status_page(ring, 4);
1121 if (head > ring->head) {
1122 ring->head = head;
1123 ring->space = ring_space(ring);
1124 if (ring->space >= n)
1125 return 0;
1126 }
1127
Chris Wilsondb53a302011-02-03 11:57:46 +00001128 trace_i915_ring_wait_begin(ring);
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001129 if (drm_core_check_feature(dev, DRIVER_GEM))
1130 /* With GEM the hangcheck timer should kick us out of the loop,
1131 * leaving it early runs the risk of corrupting GEM state (due
1132 * to running on almost untested codepaths). But on resume
1133 * timers don't work yet, so prevent a complete hang in that
1134 * case by choosing an insanely large timeout. */
1135 end = jiffies + 60 * HZ;
1136 else
1137 end = jiffies + 3 * HZ;
1138
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001139 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001140 ring->head = I915_READ_HEAD(ring);
1141 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001142 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001143 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001144 return 0;
1145 }
1146
1147 if (dev->primary->master) {
1148 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1149 if (master_priv->sarea_priv)
1150 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1151 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001152
Chris Wilsone60a0b12010-10-13 10:09:14 +01001153 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +01001154 if (atomic_read(&dev_priv->mm.wedged))
1155 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001156 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +00001157 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001158 return -EBUSY;
1159}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001160
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001161int intel_ring_begin(struct intel_ring_buffer *ring,
1162 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001163{
Chris Wilson21dd3732011-01-26 15:55:56 +00001164 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +08001165 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001166 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001167
Chris Wilson21dd3732011-01-26 15:55:56 +00001168 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
1169 return -EIO;
1170
Chris Wilson55249ba2010-12-22 14:04:47 +00001171 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001172 ret = intel_wrap_ring_buffer(ring);
1173 if (unlikely(ret))
1174 return ret;
1175 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001176
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001177 if (unlikely(ring->space < n)) {
1178 ret = intel_wait_ring_buffer(ring, n);
1179 if (unlikely(ret))
1180 return ret;
1181 }
Chris Wilsond97ed332010-08-04 15:18:13 +01001182
1183 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001184 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001185}
1186
Chris Wilson78501ea2010-10-27 12:18:21 +01001187void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001188{
Chris Wilsond97ed332010-08-04 15:18:13 +01001189 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +01001190 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001191}
1192
Chris Wilsone0708682010-09-19 14:46:27 +01001193static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001194 .name = "render ring",
Daniel Vetter96154f22011-12-14 13:57:00 +01001195 .id = RCS,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001196 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001197 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001198 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001199 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001200 .flush = render_ring_flush,
1201 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001202 .get_seqno = ring_get_seqno,
1203 .irq_get = render_ring_get_irq,
1204 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001205 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Akshay Joshi0206e352011-08-16 15:34:10 -04001206 .cleanup = render_ring_cleanup,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001207 .sync_to = render_ring_sync_to,
1208 .semaphore_register = {MI_SEMAPHORE_SYNC_INVALID,
1209 MI_SEMAPHORE_SYNC_RV,
1210 MI_SEMAPHORE_SYNC_RB},
1211 .signal_mbox = {GEN6_VRSYNC, GEN6_BRSYNC},
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001212};
Zou Nan haid1b851f2010-05-21 09:08:57 +08001213
1214/* ring buffer for bit-stream decoder */
1215
Chris Wilsone0708682010-09-19 14:46:27 +01001216static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +08001217 .name = "bsd ring",
Daniel Vetter96154f22011-12-14 13:57:00 +01001218 .id = VCS,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001219 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001220 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +01001221 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +01001222 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001223 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +01001224 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001225 .get_seqno = ring_get_seqno,
1226 .irq_get = bsd_ring_get_irq,
1227 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001228 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001229};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001230
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001231
Chris Wilson78501ea2010-10-27 12:18:21 +01001232static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001233 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001234{
Akshay Joshi0206e352011-08-16 15:34:10 -04001235 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001236
1237 /* Every tail move must follow the sequence below */
Akshay Joshi0206e352011-08-16 15:34:10 -04001238 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1239 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1240 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1241 I915_WRITE(GEN6_BSD_RNCID, 0x0);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001242
Akshay Joshi0206e352011-08-16 15:34:10 -04001243 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1244 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1245 50))
1246 DRM_ERROR("timed out waiting for IDLE Indicator\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001247
Akshay Joshi0206e352011-08-16 15:34:10 -04001248 I915_WRITE_TAIL(ring, value);
1249 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1250 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1251 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001252}
1253
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001254static int gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001255 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001256{
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
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001260 ret = intel_ring_begin(ring, 4);
1261 if (ret)
1262 return ret;
1263
Chris Wilson71a77e02011-02-02 12:13:49 +00001264 cmd = MI_FLUSH_DW;
1265 if (invalidate & I915_GEM_GPU_DOMAINS)
1266 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
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;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001273}
1274
1275static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001276gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001277 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001278{
Akshay Joshi0206e352011-08-16 15:34:10 -04001279 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001280
Akshay Joshi0206e352011-08-16 15:34:10 -04001281 ret = intel_ring_begin(ring, 2);
1282 if (ret)
1283 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001284
Akshay Joshi0206e352011-08-16 15:34:10 -04001285 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1286 /* bit0-7 is the length on GEN6+ */
1287 intel_ring_emit(ring, offset);
1288 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001289
Akshay Joshi0206e352011-08-16 15:34:10 -04001290 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001291}
1292
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001293static bool
Chris Wilson0f468322011-01-04 17:35:21 +00001294gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
1295{
1296 return gen6_ring_get_irq(ring,
1297 GT_USER_INTERRUPT,
1298 GEN6_RENDER_USER_INTERRUPT);
1299}
1300
1301static void
1302gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
1303{
1304 return gen6_ring_put_irq(ring,
1305 GT_USER_INTERRUPT,
1306 GEN6_RENDER_USER_INTERRUPT);
1307}
1308
1309static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001310gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
1311{
Chris Wilson0f468322011-01-04 17:35:21 +00001312 return gen6_ring_get_irq(ring,
1313 GT_GEN6_BSD_USER_INTERRUPT,
1314 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001315}
1316
1317static void
1318gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
1319{
Chris Wilson0f468322011-01-04 17:35:21 +00001320 return gen6_ring_put_irq(ring,
1321 GT_GEN6_BSD_USER_INTERRUPT,
1322 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001323}
1324
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001325/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +01001326static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001327 .name = "gen6 bsd ring",
Daniel Vetter96154f22011-12-14 13:57:00 +01001328 .id = VCS,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001329 .mmio_base = GEN6_BSD_RING_BASE,
1330 .size = 32 * PAGE_SIZE,
1331 .init = init_ring_common,
1332 .write_tail = gen6_bsd_ring_write_tail,
1333 .flush = gen6_ring_flush,
1334 .add_request = gen6_add_request,
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001335 .get_seqno = gen6_ring_get_seqno,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001336 .irq_get = gen6_bsd_ring_get_irq,
1337 .irq_put = gen6_bsd_ring_put_irq,
1338 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001339 .sync_to = gen6_bsd_ring_sync_to,
1340 .semaphore_register = {MI_SEMAPHORE_SYNC_VR,
1341 MI_SEMAPHORE_SYNC_INVALID,
1342 MI_SEMAPHORE_SYNC_VB},
1343 .signal_mbox = {GEN6_RVSYNC, GEN6_BVSYNC},
Chris Wilson549f7362010-10-19 11:19:32 +01001344};
1345
1346/* Blitter support (SandyBridge+) */
1347
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001348static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001349blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001350{
Chris Wilson0f468322011-01-04 17:35:21 +00001351 return gen6_ring_get_irq(ring,
1352 GT_BLT_USER_INTERRUPT,
1353 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001354}
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001355
Chris Wilson549f7362010-10-19 11:19:32 +01001356static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001357blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001358{
Chris Wilson0f468322011-01-04 17:35:21 +00001359 gen6_ring_put_irq(ring,
1360 GT_BLT_USER_INTERRUPT,
1361 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001362}
1363
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001364static int blt_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001365 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001366{
Chris Wilson71a77e02011-02-02 12:13:49 +00001367 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001368 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001369
Daniel Vetter6a233c72011-12-14 13:57:07 +01001370 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001371 if (ret)
1372 return ret;
1373
Chris Wilson71a77e02011-02-02 12:13:49 +00001374 cmd = MI_FLUSH_DW;
1375 if (invalidate & I915_GEM_DOMAIN_RENDER)
1376 cmd |= MI_INVALIDATE_TLB;
1377 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001378 intel_ring_emit(ring, 0);
1379 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001380 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001381 intel_ring_advance(ring);
1382 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001383}
1384
Chris Wilson549f7362010-10-19 11:19:32 +01001385static const struct intel_ring_buffer gen6_blt_ring = {
Akshay Joshi0206e352011-08-16 15:34:10 -04001386 .name = "blt ring",
Daniel Vetter96154f22011-12-14 13:57:00 +01001387 .id = BCS,
Akshay Joshi0206e352011-08-16 15:34:10 -04001388 .mmio_base = BLT_RING_BASE,
1389 .size = 32 * PAGE_SIZE,
Daniel Vetter6a233c72011-12-14 13:57:07 +01001390 .init = init_ring_common,
Akshay Joshi0206e352011-08-16 15:34:10 -04001391 .write_tail = ring_write_tail,
1392 .flush = blt_ring_flush,
1393 .add_request = gen6_add_request,
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001394 .get_seqno = gen6_ring_get_seqno,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001395 .irq_get = blt_ring_get_irq,
1396 .irq_put = blt_ring_put_irq,
Akshay Joshi0206e352011-08-16 15:34:10 -04001397 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001398 .sync_to = gen6_blt_ring_sync_to,
1399 .semaphore_register = {MI_SEMAPHORE_SYNC_BR,
1400 MI_SEMAPHORE_SYNC_BV,
1401 MI_SEMAPHORE_SYNC_INVALID},
1402 .signal_mbox = {GEN6_RBSYNC, GEN6_VBSYNC},
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001403};
1404
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001405int intel_init_render_ring_buffer(struct drm_device *dev)
1406{
1407 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001408 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001409
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001410 *ring = render_ring;
1411 if (INTEL_INFO(dev)->gen >= 6) {
1412 ring->add_request = gen6_add_request;
Jesse Barnes8d315282011-10-16 10:23:31 +02001413 ring->flush = gen6_render_ring_flush;
Chris Wilson0f468322011-01-04 17:35:21 +00001414 ring->irq_get = gen6_render_ring_get_irq;
1415 ring->irq_put = gen6_render_ring_put_irq;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001416 ring->get_seqno = gen6_ring_get_seqno;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001417 } else if (IS_GEN5(dev)) {
1418 ring->add_request = pc_render_add_request;
1419 ring->get_seqno = pc_render_get_seqno;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001420 }
1421
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001422 if (!I915_NEED_GFX_HWS(dev)) {
1423 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1424 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1425 }
1426
1427 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001428}
1429
Chris Wilsone8616b62011-01-20 09:57:11 +00001430int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1431{
1432 drm_i915_private_t *dev_priv = dev->dev_private;
1433 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1434
1435 *ring = render_ring;
1436 if (INTEL_INFO(dev)->gen >= 6) {
1437 ring->add_request = gen6_add_request;
1438 ring->irq_get = gen6_render_ring_get_irq;
1439 ring->irq_put = gen6_render_ring_put_irq;
1440 } else if (IS_GEN5(dev)) {
1441 ring->add_request = pc_render_add_request;
1442 ring->get_seqno = pc_render_get_seqno;
1443 }
1444
Keith Packardf3234702011-07-22 10:44:39 -07001445 if (!I915_NEED_GFX_HWS(dev))
1446 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1447
Chris Wilsone8616b62011-01-20 09:57:11 +00001448 ring->dev = dev;
1449 INIT_LIST_HEAD(&ring->active_list);
1450 INIT_LIST_HEAD(&ring->request_list);
1451 INIT_LIST_HEAD(&ring->gpu_write_list);
1452
1453 ring->size = size;
1454 ring->effective_size = ring->size;
1455 if (IS_I830(ring->dev))
1456 ring->effective_size -= 128;
1457
1458 ring->map.offset = start;
1459 ring->map.size = size;
1460 ring->map.type = 0;
1461 ring->map.flags = 0;
1462 ring->map.mtrr = 0;
1463
1464 drm_core_ioremap_wc(&ring->map, dev);
1465 if (ring->map.handle == NULL) {
1466 DRM_ERROR("can not ioremap virtual address for"
1467 " ring buffer\n");
1468 return -ENOMEM;
1469 }
1470
1471 ring->virtual_start = (void __force __iomem *)ring->map.handle;
1472 return 0;
1473}
1474
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001475int intel_init_bsd_ring_buffer(struct drm_device *dev)
1476{
1477 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001478 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001479
Jesse Barnes65d3eb12011-04-06 14:54:44 -07001480 if (IS_GEN6(dev) || IS_GEN7(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001481 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001482 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001483 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001484
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001485 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001486}
Chris Wilson549f7362010-10-19 11:19:32 +01001487
1488int intel_init_blt_ring_buffer(struct drm_device *dev)
1489{
1490 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001491 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001492
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001493 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001494
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001495 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001496}