blob: 1ab842c6032e949a37855a3995aa161d9f276977 [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 Wilson6f392d52010-08-07 11:01:22 +010055static u32 i915_gem_get_seqno(struct drm_device *dev)
56{
57 drm_i915_private_t *dev_priv = dev->dev_private;
58 u32 seqno;
59
60 seqno = dev_priv->next_seqno;
61
62 /* reserve 0 for non-seqno */
63 if (++dev_priv->next_seqno == 0)
64 dev_priv->next_seqno = 1;
65
66 return seqno;
67}
68
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000069static int
Chris Wilson78501ea2010-10-27 12:18:21 +010070render_ring_flush(struct intel_ring_buffer *ring,
Chris Wilsonab6f8e32010-09-19 17:53:44 +010071 u32 invalidate_domains,
72 u32 flush_domains)
Eric Anholt62fdfea2010-05-21 13:26:39 -070073{
Chris Wilson78501ea2010-10-27 12:18:21 +010074 struct drm_device *dev = ring->dev;
Chris Wilson6f392d52010-08-07 11:01:22 +010075 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000076 int ret;
Chris Wilson6f392d52010-08-07 11:01:22 +010077
Chris Wilson36d527d2011-03-19 22:26:49 +000078 /*
79 * read/write caches:
80 *
81 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
82 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
83 * also flushed at 2d versus 3d pipeline switches.
84 *
85 * read-only caches:
86 *
87 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
88 * MI_READ_FLUSH is set, and is always flushed on 965.
89 *
90 * I915_GEM_DOMAIN_COMMAND may not exist?
91 *
92 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
93 * invalidated when MI_EXE_FLUSH is set.
94 *
95 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
96 * invalidated with every MI_FLUSH.
97 *
98 * TLBs:
99 *
100 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
101 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
102 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
103 * are flushed at any MI_FLUSH.
104 */
105
106 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
107 if ((invalidate_domains|flush_domains) &
108 I915_GEM_DOMAIN_RENDER)
109 cmd &= ~MI_NO_WRITE_FLUSH;
110 if (INTEL_INFO(dev)->gen < 4) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700111 /*
Chris Wilson36d527d2011-03-19 22:26:49 +0000112 * On the 965, the sampler cache always gets flushed
113 * and this bit is reserved.
Eric Anholt62fdfea2010-05-21 13:26:39 -0700114 */
Chris Wilson36d527d2011-03-19 22:26:49 +0000115 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
116 cmd |= MI_READ_FLUSH;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800117 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000118 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
119 cmd |= MI_EXE_FLUSH;
120
121 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
122 (IS_G4X(dev) || IS_GEN5(dev)))
123 cmd |= MI_INVALIDATE_ISP;
124
125 ret = intel_ring_begin(ring, 2);
126 if (ret)
127 return ret;
128
129 intel_ring_emit(ring, cmd);
130 intel_ring_emit(ring, MI_NOOP);
131 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000132
133 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800134}
135
Jesse Barnes8d315282011-10-16 10:23:31 +0200136/**
137 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
138 * implementing two workarounds on gen6. From section 1.4.7.1
139 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
140 *
141 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
142 * produced by non-pipelined state commands), software needs to first
143 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
144 * 0.
145 *
146 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
147 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
148 *
149 * And the workaround for these two requires this workaround first:
150 *
151 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
152 * BEFORE the pipe-control with a post-sync op and no write-cache
153 * flushes.
154 *
155 * And this last workaround is tricky because of the requirements on
156 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
157 * volume 2 part 1:
158 *
159 * "1 of the following must also be set:
160 * - Render Target Cache Flush Enable ([12] of DW1)
161 * - Depth Cache Flush Enable ([0] of DW1)
162 * - Stall at Pixel Scoreboard ([1] of DW1)
163 * - Depth Stall ([13] of DW1)
164 * - Post-Sync Operation ([13] of DW1)
165 * - Notify Enable ([8] of DW1)"
166 *
167 * The cache flushes require the workaround flush that triggered this
168 * one, so we can't use it. Depth stall would trigger the same.
169 * Post-sync nonzero is what triggered this second workaround, so we
170 * can't use that one either. Notify enable is IRQs, which aren't
171 * really our business. That leaves only stall at scoreboard.
172 */
173static int
174intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
175{
176 struct pipe_control *pc = ring->private;
177 u32 scratch_addr = pc->gtt_offset + 128;
178 int ret;
179
180
181 ret = intel_ring_begin(ring, 6);
182 if (ret)
183 return ret;
184
185 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
186 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
187 PIPE_CONTROL_STALL_AT_SCOREBOARD);
188 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
189 intel_ring_emit(ring, 0); /* low dword */
190 intel_ring_emit(ring, 0); /* high dword */
191 intel_ring_emit(ring, MI_NOOP);
192 intel_ring_advance(ring);
193
194 ret = intel_ring_begin(ring, 6);
195 if (ret)
196 return ret;
197
198 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
199 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
200 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
201 intel_ring_emit(ring, 0);
202 intel_ring_emit(ring, 0);
203 intel_ring_emit(ring, MI_NOOP);
204 intel_ring_advance(ring);
205
206 return 0;
207}
208
209static int
210gen6_render_ring_flush(struct intel_ring_buffer *ring,
211 u32 invalidate_domains, u32 flush_domains)
212{
213 u32 flags = 0;
214 struct pipe_control *pc = ring->private;
215 u32 scratch_addr = pc->gtt_offset + 128;
216 int ret;
217
218 /* Force SNB workarounds for PIPE_CONTROL flushes */
219 intel_emit_post_sync_nonzero_flush(ring);
220
221 /* Just flush everything. Experiments have shown that reducing the
222 * number of bits based on the write domains has little performance
223 * impact.
224 */
225 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
226 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
227 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
228 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
229 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
230 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
231 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
232
233 ret = intel_ring_begin(ring, 6);
234 if (ret)
235 return ret;
236
237 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
238 intel_ring_emit(ring, flags);
239 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
240 intel_ring_emit(ring, 0); /* lower dword */
241 intel_ring_emit(ring, 0); /* uppwer dword */
242 intel_ring_emit(ring, MI_NOOP);
243 intel_ring_advance(ring);
244
245 return 0;
246}
247
Chris Wilson78501ea2010-10-27 12:18:21 +0100248static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100249 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800250{
Chris Wilson78501ea2010-10-27 12:18:21 +0100251 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100252 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800253}
254
Chris Wilson78501ea2010-10-27 12:18:21 +0100255u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800256{
Chris Wilson78501ea2010-10-27 12:18:21 +0100257 drm_i915_private_t *dev_priv = ring->dev->dev_private;
258 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200259 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800260
261 return I915_READ(acthd_reg);
262}
263
Chris Wilson78501ea2010-10-27 12:18:21 +0100264static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800265{
Chris Wilson78501ea2010-10-27 12:18:21 +0100266 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000267 struct drm_i915_gem_object *obj = ring->obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800268 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800269
270 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200271 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200272 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100273 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800274
275 /* Initialize the ring. */
Chris Wilson05394f32010-11-08 19:18:58 +0000276 I915_WRITE_START(ring, obj->gtt_offset);
Daniel Vetter570ef602010-08-02 17:06:23 +0200277 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800278
279 /* G45 ring initialization fails to reset head to zero */
280 if (head != 0) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000281 DRM_DEBUG_KMS("%s head not reset to zero "
282 "ctl %08x head %08x tail %08x start %08x\n",
283 ring->name,
284 I915_READ_CTL(ring),
285 I915_READ_HEAD(ring),
286 I915_READ_TAIL(ring),
287 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800288
Daniel Vetter570ef602010-08-02 17:06:23 +0200289 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800290
Chris Wilson6fd0d562010-12-05 20:42:33 +0000291 if (I915_READ_HEAD(ring) & HEAD_ADDR) {
292 DRM_ERROR("failed to set %s head to zero "
293 "ctl %08x head %08x tail %08x start %08x\n",
294 ring->name,
295 I915_READ_CTL(ring),
296 I915_READ_HEAD(ring),
297 I915_READ_TAIL(ring),
298 I915_READ_START(ring));
299 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700300 }
301
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200302 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000303 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson6aa56062010-10-29 21:44:37 +0100304 | RING_REPORT_64K | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800305
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800306 /* If the head is still not zero, the ring is dead */
Chris Wilson176f28e2010-10-28 11:18:07 +0100307 if ((I915_READ_CTL(ring) & RING_VALID) == 0 ||
Chris Wilson05394f32010-11-08 19:18:58 +0000308 I915_READ_START(ring) != obj->gtt_offset ||
Chris Wilson176f28e2010-10-28 11:18:07 +0100309 (I915_READ_HEAD(ring) & HEAD_ADDR) != 0) {
Chris Wilsone74cfed2010-11-09 10:16:56 +0000310 DRM_ERROR("%s initialization failed "
311 "ctl %08x head %08x tail %08x start %08x\n",
312 ring->name,
313 I915_READ_CTL(ring),
314 I915_READ_HEAD(ring),
315 I915_READ_TAIL(ring),
316 I915_READ_START(ring));
317 return -EIO;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800318 }
319
Chris Wilson78501ea2010-10-27 12:18:21 +0100320 if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
321 i915_kernel_lost_context(ring->dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800322 else {
Chris Wilsonc7dca472011-01-20 17:00:10 +0000323 ring->head = I915_READ_HEAD(ring);
Daniel Vetter870e86d2010-08-02 16:29:44 +0200324 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Chris Wilsonc7dca472011-01-20 17:00:10 +0000325 ring->space = ring_space(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800326 }
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000327
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800328 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700329}
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800330
Chris Wilsonc6df5412010-12-15 09:56:50 +0000331static int
332init_pipe_control(struct intel_ring_buffer *ring)
333{
334 struct pipe_control *pc;
335 struct drm_i915_gem_object *obj;
336 int ret;
337
338 if (ring->private)
339 return 0;
340
341 pc = kmalloc(sizeof(*pc), GFP_KERNEL);
342 if (!pc)
343 return -ENOMEM;
344
345 obj = i915_gem_alloc_object(ring->dev, 4096);
346 if (obj == NULL) {
347 DRM_ERROR("Failed to allocate seqno page\n");
348 ret = -ENOMEM;
349 goto err;
350 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100351
352 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000353
354 ret = i915_gem_object_pin(obj, 4096, true);
355 if (ret)
356 goto err_unref;
357
358 pc->gtt_offset = obj->gtt_offset;
359 pc->cpu_page = kmap(obj->pages[0]);
360 if (pc->cpu_page == NULL)
361 goto err_unpin;
362
363 pc->obj = obj;
364 ring->private = pc;
365 return 0;
366
367err_unpin:
368 i915_gem_object_unpin(obj);
369err_unref:
370 drm_gem_object_unreference(&obj->base);
371err:
372 kfree(pc);
373 return ret;
374}
375
376static void
377cleanup_pipe_control(struct intel_ring_buffer *ring)
378{
379 struct pipe_control *pc = ring->private;
380 struct drm_i915_gem_object *obj;
381
382 if (!ring->private)
383 return;
384
385 obj = pc->obj;
386 kunmap(obj->pages[0]);
387 i915_gem_object_unpin(obj);
388 drm_gem_object_unreference(&obj->base);
389
390 kfree(pc);
391 ring->private = NULL;
392}
393
Chris Wilson78501ea2010-10-27 12:18:21 +0100394static int init_render_ring(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800395{
Chris Wilson78501ea2010-10-27 12:18:21 +0100396 struct drm_device *dev = ring->dev;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000397 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +0100398 int ret = init_ring_common(ring);
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800399
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100400 if (INTEL_INFO(dev)->gen > 3) {
Chris Wilson78501ea2010-10-27 12:18:21 +0100401 int mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH;
Jesse Barnes65d3eb12011-04-06 14:54:44 -0700402 if (IS_GEN6(dev) || IS_GEN7(dev))
Zhenyu Wanga69ffdb2010-08-30 16:12:42 +0800403 mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE;
404 I915_WRITE(MI_MODE, mode);
Jesse Barnesb095cd02011-08-12 15:28:32 -0700405 if (IS_GEN7(dev))
406 I915_WRITE(GFX_MODE_GEN7,
407 GFX_MODE_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
408 GFX_MODE_ENABLE(GFX_REPLAY_MODE));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800409 }
Chris Wilson78501ea2010-10-27 12:18:21 +0100410
Jesse Barnes8d315282011-10-16 10:23:31 +0200411 if (INTEL_INFO(dev)->gen >= 5) {
Chris Wilsonc6df5412010-12-15 09:56:50 +0000412 ret = init_pipe_control(ring);
413 if (ret)
414 return ret;
415 }
416
Ben Widawsky84f9f932011-12-12 19:21:58 -0800417 if (INTEL_INFO(dev)->gen >= 6) {
418 I915_WRITE(INSTPM,
419 INSTPM_FORCE_ORDERING << 16 | INSTPM_FORCE_ORDERING);
420 }
421
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800422 return ret;
423}
424
Chris Wilsonc6df5412010-12-15 09:56:50 +0000425static void render_ring_cleanup(struct intel_ring_buffer *ring)
426{
427 if (!ring->private)
428 return;
429
430 cleanup_pipe_control(ring);
431}
432
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000433static void
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700434update_mboxes(struct intel_ring_buffer *ring,
435 u32 seqno,
436 u32 mmio_offset)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000437{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700438 intel_ring_emit(ring, MI_SEMAPHORE_MBOX |
439 MI_SEMAPHORE_GLOBAL_GTT |
440 MI_SEMAPHORE_REGISTER |
441 MI_SEMAPHORE_UPDATE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000442 intel_ring_emit(ring, seqno);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700443 intel_ring_emit(ring, mmio_offset);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000444}
445
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700446/**
447 * gen6_add_request - Update the semaphore mailbox registers
448 *
449 * @ring - ring that is adding a request
450 * @seqno - return seqno stuck into the ring
451 *
452 * Update the mailbox registers in the *other* rings with the current seqno.
453 * This acts like a signal in the canonical semaphore.
454 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000455static int
456gen6_add_request(struct intel_ring_buffer *ring,
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700457 u32 *seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000458{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700459 u32 mbox1_reg;
460 u32 mbox2_reg;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000461 int ret;
462
463 ret = intel_ring_begin(ring, 10);
464 if (ret)
465 return ret;
466
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700467 mbox1_reg = ring->signal_mbox[0];
468 mbox2_reg = ring->signal_mbox[1];
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000469
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700470 *seqno = i915_gem_get_seqno(ring->dev);
471
472 update_mboxes(ring, *seqno, mbox1_reg);
473 update_mboxes(ring, *seqno, mbox2_reg);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000474 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
475 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700476 intel_ring_emit(ring, *seqno);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000477 intel_ring_emit(ring, MI_USER_INTERRUPT);
478 intel_ring_advance(ring);
479
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000480 return 0;
481}
482
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700483/**
484 * intel_ring_sync - sync the waiter to the signaller on seqno
485 *
486 * @waiter - ring that is waiting
487 * @signaller - ring which has, or will signal
488 * @seqno - seqno which the waiter will block on
489 */
490static int
491intel_ring_sync(struct intel_ring_buffer *waiter,
492 struct intel_ring_buffer *signaller,
493 int ring,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000494 u32 seqno)
495{
496 int ret;
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700497 u32 dw1 = MI_SEMAPHORE_MBOX |
498 MI_SEMAPHORE_COMPARE |
499 MI_SEMAPHORE_REGISTER;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000500
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700501 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000502 if (ret)
503 return ret;
504
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700505 intel_ring_emit(waiter, dw1 | signaller->semaphore_register[ring]);
506 intel_ring_emit(waiter, seqno);
507 intel_ring_emit(waiter, 0);
508 intel_ring_emit(waiter, MI_NOOP);
509 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000510
511 return 0;
512}
513
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700514/* VCS->RCS (RVSYNC) or BCS->RCS (RBSYNC) */
515int
516render_ring_sync_to(struct intel_ring_buffer *waiter,
517 struct intel_ring_buffer *signaller,
518 u32 seqno)
519{
520 WARN_ON(signaller->semaphore_register[RCS] == MI_SEMAPHORE_SYNC_INVALID);
521 return intel_ring_sync(waiter,
522 signaller,
523 RCS,
524 seqno);
525}
526
527/* RCS->VCS (VRSYNC) or BCS->VCS (VBSYNC) */
528int
529gen6_bsd_ring_sync_to(struct intel_ring_buffer *waiter,
530 struct intel_ring_buffer *signaller,
531 u32 seqno)
532{
533 WARN_ON(signaller->semaphore_register[VCS] == MI_SEMAPHORE_SYNC_INVALID);
534 return intel_ring_sync(waiter,
535 signaller,
536 VCS,
537 seqno);
538}
539
540/* RCS->BCS (BRSYNC) or VCS->BCS (BVSYNC) */
541int
542gen6_blt_ring_sync_to(struct intel_ring_buffer *waiter,
543 struct intel_ring_buffer *signaller,
544 u32 seqno)
545{
546 WARN_ON(signaller->semaphore_register[BCS] == MI_SEMAPHORE_SYNC_INVALID);
547 return intel_ring_sync(waiter,
548 signaller,
549 BCS,
550 seqno);
551}
552
553
554
Chris Wilsonc6df5412010-12-15 09:56:50 +0000555#define PIPE_CONTROL_FLUSH(ring__, addr__) \
556do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200557 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
558 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000559 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
560 intel_ring_emit(ring__, 0); \
561 intel_ring_emit(ring__, 0); \
562} while (0)
563
564static int
565pc_render_add_request(struct intel_ring_buffer *ring,
566 u32 *result)
567{
568 struct drm_device *dev = ring->dev;
569 u32 seqno = i915_gem_get_seqno(dev);
570 struct pipe_control *pc = ring->private;
571 u32 scratch_addr = pc->gtt_offset + 128;
572 int ret;
573
574 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
575 * incoherent with writes to memory, i.e. completely fubar,
576 * so we need to use PIPE_NOTIFY instead.
577 *
578 * However, we also need to workaround the qword write
579 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
580 * memory before requesting an interrupt.
581 */
582 ret = intel_ring_begin(ring, 32);
583 if (ret)
584 return ret;
585
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 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
590 intel_ring_emit(ring, seqno);
591 intel_ring_emit(ring, 0);
592 PIPE_CONTROL_FLUSH(ring, scratch_addr);
593 scratch_addr += 128; /* write to separate cachelines */
594 PIPE_CONTROL_FLUSH(ring, scratch_addr);
595 scratch_addr += 128;
596 PIPE_CONTROL_FLUSH(ring, scratch_addr);
597 scratch_addr += 128;
598 PIPE_CONTROL_FLUSH(ring, scratch_addr);
599 scratch_addr += 128;
600 PIPE_CONTROL_FLUSH(ring, scratch_addr);
601 scratch_addr += 128;
602 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200603 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200604 PIPE_CONTROL_WRITE_FLUSH |
605 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000606 PIPE_CONTROL_NOTIFY);
607 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
608 intel_ring_emit(ring, seqno);
609 intel_ring_emit(ring, 0);
610 intel_ring_advance(ring);
611
612 *result = seqno;
613 return 0;
614}
615
Chris Wilson3cce4692010-10-27 16:11:02 +0100616static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100617render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100618 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700619{
Chris Wilson78501ea2010-10-27 12:18:21 +0100620 struct drm_device *dev = ring->dev;
Chris Wilson3cce4692010-10-27 16:11:02 +0100621 u32 seqno = i915_gem_get_seqno(dev);
622 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800623
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000624 ret = intel_ring_begin(ring, 4);
625 if (ret)
626 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100627
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000628 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
629 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
630 intel_ring_emit(ring, seqno);
631 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100632 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000633
Chris Wilson3cce4692010-10-27 16:11:02 +0100634 *result = seqno;
635 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700636}
637
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800638static u32
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100639gen6_ring_get_seqno(struct intel_ring_buffer *ring)
640{
641 struct drm_device *dev = ring->dev;
642
643 /* Workaround to force correct ordering between irq and seqno writes on
644 * ivb (and maybe also on snb) by reading from a CS register (like
645 * ACTHD) before reading the status page. */
646 if (IS_GEN7(dev))
647 intel_ring_get_active_head(ring);
648 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
649}
650
651static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000652ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800653{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000654 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
655}
656
Chris Wilsonc6df5412010-12-15 09:56:50 +0000657static u32
658pc_render_get_seqno(struct intel_ring_buffer *ring)
659{
660 struct pipe_control *pc = ring->private;
661 return pc->cpu_page[0];
662}
663
Chris Wilson0f46832f2011-01-04 17:35:21 +0000664static void
665ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
666{
667 dev_priv->gt_irq_mask &= ~mask;
668 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
669 POSTING_READ(GTIMR);
670}
671
672static void
673ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
674{
675 dev_priv->gt_irq_mask |= mask;
676 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
677 POSTING_READ(GTIMR);
678}
679
680static void
681i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
682{
683 dev_priv->irq_mask &= ~mask;
684 I915_WRITE(IMR, dev_priv->irq_mask);
685 POSTING_READ(IMR);
686}
687
688static void
689i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
690{
691 dev_priv->irq_mask |= mask;
692 I915_WRITE(IMR, dev_priv->irq_mask);
693 POSTING_READ(IMR);
694}
695
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000696static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000697render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700698{
Chris Wilson78501ea2010-10-27 12:18:21 +0100699 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000700 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700701
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000702 if (!dev->irq_enabled)
703 return false;
704
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000705 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000706 if (ring->irq_refcount++ == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700707 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f46832f2011-01-04 17:35:21 +0000708 ironlake_enable_irq(dev_priv,
709 GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700710 else
711 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
712 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000713 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000714
715 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700716}
717
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800718static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000719render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700720{
Chris Wilson78501ea2010-10-27 12:18:21 +0100721 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000722 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700723
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000724 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000725 if (--ring->irq_refcount == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700726 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f46832f2011-01-04 17:35:21 +0000727 ironlake_disable_irq(dev_priv,
728 GT_USER_INTERRUPT |
729 GT_PIPE_NOTIFY);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700730 else
731 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
732 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000733 spin_unlock(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700734}
735
Chris Wilson78501ea2010-10-27 12:18:21 +0100736void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800737{
Eric Anholt45930102011-05-06 17:12:35 -0700738 struct drm_device *dev = ring->dev;
Chris Wilson78501ea2010-10-27 12:18:21 +0100739 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700740 u32 mmio = 0;
741
742 /* The ring status page addresses are no longer next to the rest of
743 * the ring registers as of gen7.
744 */
745 if (IS_GEN7(dev)) {
746 switch (ring->id) {
747 case RING_RENDER:
748 mmio = RENDER_HWS_PGA_GEN7;
749 break;
750 case RING_BLT:
751 mmio = BLT_HWS_PGA_GEN7;
752 break;
753 case RING_BSD:
754 mmio = BSD_HWS_PGA_GEN7;
755 break;
756 }
757 } else if (IS_GEN6(ring->dev)) {
758 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
759 } else {
760 mmio = RING_HWS_PGA(ring->mmio_base);
761 }
762
Chris Wilson78501ea2010-10-27 12:18:21 +0100763 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
764 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800765}
766
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000767static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100768bsd_ring_flush(struct intel_ring_buffer *ring,
769 u32 invalidate_domains,
770 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800771{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000772 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000773
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000774 ret = intel_ring_begin(ring, 2);
775 if (ret)
776 return ret;
777
778 intel_ring_emit(ring, MI_FLUSH);
779 intel_ring_emit(ring, MI_NOOP);
780 intel_ring_advance(ring);
781 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800782}
783
Chris Wilson3cce4692010-10-27 16:11:02 +0100784static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100785ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100786 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800787{
788 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100789 int ret;
790
791 ret = intel_ring_begin(ring, 4);
792 if (ret)
793 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100794
Chris Wilson78501ea2010-10-27 12:18:21 +0100795 seqno = i915_gem_get_seqno(ring->dev);
Chris Wilson6f392d52010-08-07 11:01:22 +0100796
Chris Wilson3cce4692010-10-27 16:11:02 +0100797 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
798 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
799 intel_ring_emit(ring, seqno);
800 intel_ring_emit(ring, MI_USER_INTERRUPT);
801 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800802
Chris Wilson3cce4692010-10-27 16:11:02 +0100803 *result = seqno;
804 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800805}
806
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000807static bool
Chris Wilson0f46832f2011-01-04 17:35:21 +0000808gen6_ring_get_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
809{
810 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000811 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f46832f2011-01-04 17:35:21 +0000812
813 if (!dev->irq_enabled)
814 return false;
815
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100816 /* It looks like we need to prevent the gt from suspending while waiting
817 * for an notifiy irq, otherwise irqs seem to get lost on at least the
818 * blt/bsd rings on ivb. */
819 if (IS_GEN7(dev))
820 gen6_gt_force_wake_get(dev_priv);
821
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000822 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000823 if (ring->irq_refcount++ == 0) {
Chris Wilson0f46832f2011-01-04 17:35:21 +0000824 ring->irq_mask &= ~rflag;
825 I915_WRITE_IMR(ring, ring->irq_mask);
826 ironlake_enable_irq(dev_priv, gflag);
Chris Wilson0f46832f2011-01-04 17:35:21 +0000827 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000828 spin_unlock(&ring->irq_lock);
Chris Wilson0f46832f2011-01-04 17:35:21 +0000829
830 return true;
831}
832
833static void
834gen6_ring_put_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
835{
836 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000837 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f46832f2011-01-04 17:35:21 +0000838
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000839 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000840 if (--ring->irq_refcount == 0) {
Chris Wilson0f46832f2011-01-04 17:35:21 +0000841 ring->irq_mask |= rflag;
842 I915_WRITE_IMR(ring, ring->irq_mask);
843 ironlake_disable_irq(dev_priv, gflag);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000844 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000845 spin_unlock(&ring->irq_lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100846
847 if (IS_GEN7(dev))
848 gen6_gt_force_wake_put(dev_priv);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000849}
850
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000851static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000852bsd_ring_get_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 if (!dev->irq_enabled)
858 return false;
859
860 spin_lock(&ring->irq_lock);
861 if (ring->irq_refcount++ == 0) {
862 if (IS_G4X(dev))
863 i915_enable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
864 else
865 ironlake_enable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
866 }
867 spin_unlock(&ring->irq_lock);
868
869 return true;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000870}
871static void
872bsd_ring_put_irq(struct intel_ring_buffer *ring)
873{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800874 struct drm_device *dev = ring->dev;
875 drm_i915_private_t *dev_priv = dev->dev_private;
876
877 spin_lock(&ring->irq_lock);
878 if (--ring->irq_refcount == 0) {
879 if (IS_G4X(dev))
880 i915_disable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
881 else
882 ironlake_disable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
883 }
884 spin_unlock(&ring->irq_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800885}
886
887static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000888ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800889{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100890 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100891
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100892 ret = intel_ring_begin(ring, 2);
893 if (ret)
894 return ret;
895
Chris Wilson78501ea2010-10-27 12:18:21 +0100896 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000897 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100898 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000899 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100900 intel_ring_advance(ring);
901
Zou Nan haid1b851f2010-05-21 09:08:57 +0800902 return 0;
903}
904
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800905static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100906render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000907 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700908{
Chris Wilson78501ea2010-10-27 12:18:21 +0100909 struct drm_device *dev = ring->dev;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000910 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700911
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000912 if (IS_I830(dev) || IS_845G(dev)) {
913 ret = intel_ring_begin(ring, 4);
914 if (ret)
915 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700916
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000917 intel_ring_emit(ring, MI_BATCH_BUFFER);
918 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
919 intel_ring_emit(ring, offset + len - 8);
920 intel_ring_emit(ring, 0);
921 } else {
922 ret = intel_ring_begin(ring, 2);
923 if (ret)
924 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100925
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000926 if (INTEL_INFO(dev)->gen >= 4) {
927 intel_ring_emit(ring,
928 MI_BATCH_BUFFER_START | (2 << 6) |
929 MI_BATCH_NON_SECURE_I965);
930 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700931 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000932 intel_ring_emit(ring,
933 MI_BATCH_BUFFER_START | (2 << 6));
934 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700935 }
936 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000937 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700938
Eric Anholt62fdfea2010-05-21 13:26:39 -0700939 return 0;
940}
941
Chris Wilson78501ea2010-10-27 12:18:21 +0100942static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700943{
Chris Wilson78501ea2010-10-27 12:18:21 +0100944 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000945 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700946
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800947 obj = ring->status_page.obj;
948 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700949 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700950
Chris Wilson05394f32010-11-08 19:18:58 +0000951 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700952 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000953 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800954 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700955
956 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700957}
958
Chris Wilson78501ea2010-10-27 12:18:21 +0100959static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700960{
Chris Wilson78501ea2010-10-27 12:18:21 +0100961 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700962 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000963 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700964 int ret;
965
Eric Anholt62fdfea2010-05-21 13:26:39 -0700966 obj = i915_gem_alloc_object(dev, 4096);
967 if (obj == NULL) {
968 DRM_ERROR("Failed to allocate status page\n");
969 ret = -ENOMEM;
970 goto err;
971 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100972
973 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700974
Daniel Vetter75e9e912010-11-04 17:11:09 +0100975 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700976 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700977 goto err_unref;
978 }
979
Chris Wilson05394f32010-11-08 19:18:58 +0000980 ring->status_page.gfx_addr = obj->gtt_offset;
981 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800982 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700983 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700984 goto err_unpin;
985 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800986 ring->status_page.obj = obj;
987 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700988
Chris Wilson78501ea2010-10-27 12:18:21 +0100989 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800990 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
991 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700992
993 return 0;
994
995err_unpin:
996 i915_gem_object_unpin(obj);
997err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000998 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700999err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001000 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001001}
1002
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001003int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001004 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001005{
Chris Wilson05394f32010-11-08 19:18:58 +00001006 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +01001007 int ret;
1008
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001009 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01001010 INIT_LIST_HEAD(&ring->active_list);
1011 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01001012 INIT_LIST_HEAD(&ring->gpu_write_list);
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001013
Chris Wilsonb259f672011-03-29 13:19:09 +01001014 init_waitqueue_head(&ring->irq_queue);
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001015 spin_lock_init(&ring->irq_lock);
Chris Wilson0f46832f2011-01-04 17:35:21 +00001016 ring->irq_mask = ~0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001017
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001018 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001019 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001020 if (ret)
1021 return ret;
1022 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001023
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001024 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001025 if (obj == NULL) {
1026 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001027 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +01001028 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001029 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001030
Chris Wilson05394f32010-11-08 19:18:58 +00001031 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001032
Daniel Vetter75e9e912010-11-04 17:11:09 +01001033 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +01001034 if (ret)
1035 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001036
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001037 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +00001038 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001039 ring->map.type = 0;
1040 ring->map.flags = 0;
1041 ring->map.mtrr = 0;
1042
1043 drm_core_ioremap_wc(&ring->map, dev);
1044 if (ring->map.handle == NULL) {
1045 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001046 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +01001047 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001048 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001049
Eric Anholt62fdfea2010-05-21 13:26:39 -07001050 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +01001051 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +01001052 if (ret)
1053 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001054
Chris Wilson55249ba2010-12-22 14:04:47 +00001055 /* Workaround an erratum on the i830 which causes a hang if
1056 * the TAIL pointer points to within the last 2 cachelines
1057 * of the buffer.
1058 */
1059 ring->effective_size = ring->size;
1060 if (IS_I830(ring->dev))
1061 ring->effective_size -= 128;
1062
Chris Wilsonc584fe42010-10-29 18:15:52 +01001063 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +01001064
1065err_unmap:
1066 drm_core_ioremapfree(&ring->map, dev);
1067err_unpin:
1068 i915_gem_object_unpin(obj);
1069err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001070 drm_gem_object_unreference(&obj->base);
1071 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +01001072err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +01001073 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001074 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001075}
1076
Chris Wilson78501ea2010-10-27 12:18:21 +01001077void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001078{
Chris Wilson33626e62010-10-29 16:18:36 +01001079 struct drm_i915_private *dev_priv;
1080 int ret;
1081
Chris Wilson05394f32010-11-08 19:18:58 +00001082 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001083 return;
1084
Chris Wilson33626e62010-10-29 16:18:36 +01001085 /* Disable the ring buffer. The ring must be idle at this point */
1086 dev_priv = ring->dev->dev_private;
Ben Widawsky96f298a2011-03-19 18:14:27 -07001087 ret = intel_wait_ring_idle(ring);
Chris Wilson29ee3992011-01-24 16:35:42 +00001088 if (ret)
1089 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
1090 ring->name, ret);
1091
Chris Wilson33626e62010-10-29 16:18:36 +01001092 I915_WRITE_CTL(ring, 0);
1093
Chris Wilson78501ea2010-10-27 12:18:21 +01001094 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001095
Chris Wilson05394f32010-11-08 19:18:58 +00001096 i915_gem_object_unpin(ring->obj);
1097 drm_gem_object_unreference(&ring->obj->base);
1098 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +01001099
Zou Nan hai8d192152010-11-02 16:31:01 +08001100 if (ring->cleanup)
1101 ring->cleanup(ring);
1102
Chris Wilson78501ea2010-10-27 12:18:21 +01001103 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001104}
1105
Chris Wilson78501ea2010-10-27 12:18:21 +01001106static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001107{
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001108 unsigned int *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +00001109 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001110
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001111 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001112 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001113 if (ret)
1114 return ret;
1115 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001116
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001117 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +01001118 rem /= 8;
1119 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001120 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +01001121 *virt++ = MI_NOOP;
1122 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001123
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001124 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001125 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001126
1127 return 0;
1128}
1129
Chris Wilson78501ea2010-10-27 12:18:21 +01001130int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001131{
Chris Wilson78501ea2010-10-27 12:18:21 +01001132 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001133 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001134 unsigned long end;
Chris Wilson6aa56062010-10-29 21:44:37 +01001135 u32 head;
1136
Chris Wilsonc7dca472011-01-20 17:00:10 +00001137 /* If the reported head position has wrapped or hasn't advanced,
1138 * fallback to the slow and accurate path.
1139 */
1140 head = intel_read_status_page(ring, 4);
1141 if (head > ring->head) {
1142 ring->head = head;
1143 ring->space = ring_space(ring);
1144 if (ring->space >= n)
1145 return 0;
1146 }
1147
Chris Wilsondb53a302011-02-03 11:57:46 +00001148 trace_i915_ring_wait_begin(ring);
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001149 if (drm_core_check_feature(dev, DRIVER_GEM))
1150 /* With GEM the hangcheck timer should kick us out of the loop,
1151 * leaving it early runs the risk of corrupting GEM state (due
1152 * to running on almost untested codepaths). But on resume
1153 * timers don't work yet, so prevent a complete hang in that
1154 * case by choosing an insanely large timeout. */
1155 end = jiffies + 60 * HZ;
1156 else
1157 end = jiffies + 3 * HZ;
1158
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001159 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001160 ring->head = I915_READ_HEAD(ring);
1161 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001162 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001163 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001164 return 0;
1165 }
1166
1167 if (dev->primary->master) {
1168 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1169 if (master_priv->sarea_priv)
1170 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1171 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001172
Chris Wilsone60a0b12010-10-13 10:09:14 +01001173 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +01001174 if (atomic_read(&dev_priv->mm.wedged))
1175 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001176 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +00001177 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001178 return -EBUSY;
1179}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001180
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001181int intel_ring_begin(struct intel_ring_buffer *ring,
1182 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001183{
Chris Wilson21dd3732011-01-26 15:55:56 +00001184 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +08001185 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001186 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001187
Chris Wilson21dd3732011-01-26 15:55:56 +00001188 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
1189 return -EIO;
1190
Chris Wilson55249ba2010-12-22 14:04:47 +00001191 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001192 ret = intel_wrap_ring_buffer(ring);
1193 if (unlikely(ret))
1194 return ret;
1195 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001196
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001197 if (unlikely(ring->space < n)) {
1198 ret = intel_wait_ring_buffer(ring, n);
1199 if (unlikely(ret))
1200 return ret;
1201 }
Chris Wilsond97ed332010-08-04 15:18:13 +01001202
1203 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001204 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001205}
1206
Chris Wilson78501ea2010-10-27 12:18:21 +01001207void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001208{
Chris Wilsond97ed332010-08-04 15:18:13 +01001209 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +01001210 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001211}
1212
Chris Wilsone0708682010-09-19 14:46:27 +01001213static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001214 .name = "render ring",
Chris Wilson92204342010-09-18 11:02:01 +01001215 .id = RING_RENDER,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001216 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001217 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001218 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001219 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001220 .flush = render_ring_flush,
1221 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001222 .get_seqno = ring_get_seqno,
1223 .irq_get = render_ring_get_irq,
1224 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001225 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Akshay Joshi0206e352011-08-16 15:34:10 -04001226 .cleanup = render_ring_cleanup,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001227 .sync_to = render_ring_sync_to,
1228 .semaphore_register = {MI_SEMAPHORE_SYNC_INVALID,
1229 MI_SEMAPHORE_SYNC_RV,
1230 MI_SEMAPHORE_SYNC_RB},
1231 .signal_mbox = {GEN6_VRSYNC, GEN6_BRSYNC},
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001232};
Zou Nan haid1b851f2010-05-21 09:08:57 +08001233
1234/* ring buffer for bit-stream decoder */
1235
Chris Wilsone0708682010-09-19 14:46:27 +01001236static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +08001237 .name = "bsd ring",
Chris Wilson92204342010-09-18 11:02:01 +01001238 .id = RING_BSD,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001239 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001240 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +01001241 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +01001242 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001243 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +01001244 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001245 .get_seqno = ring_get_seqno,
1246 .irq_get = bsd_ring_get_irq,
1247 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001248 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001249};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001250
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001251
Chris Wilson78501ea2010-10-27 12:18:21 +01001252static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001253 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001254{
Akshay Joshi0206e352011-08-16 15:34:10 -04001255 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001256
1257 /* Every tail move must follow the sequence below */
Akshay Joshi0206e352011-08-16 15:34:10 -04001258 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1259 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1260 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1261 I915_WRITE(GEN6_BSD_RNCID, 0x0);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001262
Akshay Joshi0206e352011-08-16 15:34:10 -04001263 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1264 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1265 50))
1266 DRM_ERROR("timed out waiting for IDLE Indicator\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001267
Akshay Joshi0206e352011-08-16 15:34:10 -04001268 I915_WRITE_TAIL(ring, value);
1269 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1270 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1271 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001272}
1273
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001274static int gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001275 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001276{
Chris Wilson71a77e02011-02-02 12:13:49 +00001277 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001278 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001279
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001280 ret = intel_ring_begin(ring, 4);
1281 if (ret)
1282 return ret;
1283
Chris Wilson71a77e02011-02-02 12:13:49 +00001284 cmd = MI_FLUSH_DW;
1285 if (invalidate & I915_GEM_GPU_DOMAINS)
1286 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1287 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001288 intel_ring_emit(ring, 0);
1289 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001290 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001291 intel_ring_advance(ring);
1292 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001293}
1294
1295static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001296gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001297 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001298{
Akshay Joshi0206e352011-08-16 15:34:10 -04001299 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001300
Akshay Joshi0206e352011-08-16 15:34:10 -04001301 ret = intel_ring_begin(ring, 2);
1302 if (ret)
1303 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001304
Akshay Joshi0206e352011-08-16 15:34:10 -04001305 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1306 /* bit0-7 is the length on GEN6+ */
1307 intel_ring_emit(ring, offset);
1308 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001309
Akshay Joshi0206e352011-08-16 15:34:10 -04001310 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001311}
1312
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001313static bool
Chris Wilson0f46832f2011-01-04 17:35:21 +00001314gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
1315{
1316 return gen6_ring_get_irq(ring,
1317 GT_USER_INTERRUPT,
1318 GEN6_RENDER_USER_INTERRUPT);
1319}
1320
1321static void
1322gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
1323{
1324 return gen6_ring_put_irq(ring,
1325 GT_USER_INTERRUPT,
1326 GEN6_RENDER_USER_INTERRUPT);
1327}
1328
1329static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001330gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
1331{
Chris Wilson0f46832f2011-01-04 17:35:21 +00001332 return gen6_ring_get_irq(ring,
1333 GT_GEN6_BSD_USER_INTERRUPT,
1334 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001335}
1336
1337static void
1338gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
1339{
Chris Wilson0f46832f2011-01-04 17:35:21 +00001340 return gen6_ring_put_irq(ring,
1341 GT_GEN6_BSD_USER_INTERRUPT,
1342 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001343}
1344
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001345/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +01001346static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001347 .name = "gen6 bsd ring",
1348 .id = RING_BSD,
1349 .mmio_base = GEN6_BSD_RING_BASE,
1350 .size = 32 * PAGE_SIZE,
1351 .init = init_ring_common,
1352 .write_tail = gen6_bsd_ring_write_tail,
1353 .flush = gen6_ring_flush,
1354 .add_request = gen6_add_request,
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001355 .get_seqno = gen6_ring_get_seqno,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001356 .irq_get = gen6_bsd_ring_get_irq,
1357 .irq_put = gen6_bsd_ring_put_irq,
1358 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001359 .sync_to = gen6_bsd_ring_sync_to,
1360 .semaphore_register = {MI_SEMAPHORE_SYNC_VR,
1361 MI_SEMAPHORE_SYNC_INVALID,
1362 MI_SEMAPHORE_SYNC_VB},
1363 .signal_mbox = {GEN6_RVSYNC, GEN6_BVSYNC},
Chris Wilson549f7362010-10-19 11:19:32 +01001364};
1365
1366/* Blitter support (SandyBridge+) */
1367
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001368static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001369blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001370{
Chris Wilson0f46832f2011-01-04 17:35:21 +00001371 return gen6_ring_get_irq(ring,
1372 GT_BLT_USER_INTERRUPT,
1373 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001374}
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001375
Chris Wilson549f7362010-10-19 11:19:32 +01001376static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001377blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001378{
Chris Wilson0f46832f2011-01-04 17:35:21 +00001379 gen6_ring_put_irq(ring,
1380 GT_BLT_USER_INTERRUPT,
1381 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001382}
1383
Zou Nan hai8d192152010-11-02 16:31:01 +08001384
1385/* Workaround for some stepping of SNB,
1386 * each time when BLT engine ring tail moved,
1387 * the first command in the ring to be parsed
1388 * should be MI_BATCH_BUFFER_START
1389 */
1390#define NEED_BLT_WORKAROUND(dev) \
1391 (IS_GEN6(dev) && (dev->pdev->revision < 8))
1392
1393static inline struct drm_i915_gem_object *
1394to_blt_workaround(struct intel_ring_buffer *ring)
1395{
1396 return ring->private;
1397}
1398
1399static int blt_ring_init(struct intel_ring_buffer *ring)
1400{
1401 if (NEED_BLT_WORKAROUND(ring->dev)) {
1402 struct drm_i915_gem_object *obj;
Chris Wilson27153f72010-11-02 11:17:23 +00001403 u32 *ptr;
Zou Nan hai8d192152010-11-02 16:31:01 +08001404 int ret;
1405
Chris Wilson05394f32010-11-08 19:18:58 +00001406 obj = i915_gem_alloc_object(ring->dev, 4096);
Zou Nan hai8d192152010-11-02 16:31:01 +08001407 if (obj == NULL)
1408 return -ENOMEM;
1409
Chris Wilson05394f32010-11-08 19:18:58 +00001410 ret = i915_gem_object_pin(obj, 4096, true);
Zou Nan hai8d192152010-11-02 16:31:01 +08001411 if (ret) {
1412 drm_gem_object_unreference(&obj->base);
1413 return ret;
1414 }
1415
1416 ptr = kmap(obj->pages[0]);
Chris Wilson27153f72010-11-02 11:17:23 +00001417 *ptr++ = MI_BATCH_BUFFER_END;
1418 *ptr++ = MI_NOOP;
Zou Nan hai8d192152010-11-02 16:31:01 +08001419 kunmap(obj->pages[0]);
1420
Chris Wilson05394f32010-11-08 19:18:58 +00001421 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Zou Nan hai8d192152010-11-02 16:31:01 +08001422 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00001423 i915_gem_object_unpin(obj);
Zou Nan hai8d192152010-11-02 16:31:01 +08001424 drm_gem_object_unreference(&obj->base);
1425 return ret;
1426 }
1427
1428 ring->private = obj;
1429 }
1430
1431 return init_ring_common(ring);
1432}
1433
1434static int blt_ring_begin(struct intel_ring_buffer *ring,
1435 int num_dwords)
1436{
1437 if (ring->private) {
1438 int ret = intel_ring_begin(ring, num_dwords+2);
1439 if (ret)
1440 return ret;
1441
1442 intel_ring_emit(ring, MI_BATCH_BUFFER_START);
1443 intel_ring_emit(ring, to_blt_workaround(ring)->gtt_offset);
1444
1445 return 0;
1446 } else
1447 return intel_ring_begin(ring, 4);
1448}
1449
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001450static int blt_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001451 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001452{
Chris Wilson71a77e02011-02-02 12:13:49 +00001453 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001454 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001455
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001456 ret = blt_ring_begin(ring, 4);
1457 if (ret)
1458 return ret;
1459
Chris Wilson71a77e02011-02-02 12:13:49 +00001460 cmd = MI_FLUSH_DW;
1461 if (invalidate & I915_GEM_DOMAIN_RENDER)
1462 cmd |= MI_INVALIDATE_TLB;
1463 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001464 intel_ring_emit(ring, 0);
1465 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001466 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001467 intel_ring_advance(ring);
1468 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001469}
1470
Zou Nan hai8d192152010-11-02 16:31:01 +08001471static void blt_ring_cleanup(struct intel_ring_buffer *ring)
1472{
1473 if (!ring->private)
1474 return;
1475
1476 i915_gem_object_unpin(ring->private);
1477 drm_gem_object_unreference(ring->private);
1478 ring->private = NULL;
1479}
1480
Chris Wilson549f7362010-10-19 11:19:32 +01001481static const struct intel_ring_buffer gen6_blt_ring = {
Akshay Joshi0206e352011-08-16 15:34:10 -04001482 .name = "blt ring",
1483 .id = RING_BLT,
1484 .mmio_base = BLT_RING_BASE,
1485 .size = 32 * PAGE_SIZE,
1486 .init = blt_ring_init,
1487 .write_tail = ring_write_tail,
1488 .flush = blt_ring_flush,
1489 .add_request = gen6_add_request,
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001490 .get_seqno = gen6_ring_get_seqno,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001491 .irq_get = blt_ring_get_irq,
1492 .irq_put = blt_ring_put_irq,
Akshay Joshi0206e352011-08-16 15:34:10 -04001493 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001494 .cleanup = blt_ring_cleanup,
1495 .sync_to = gen6_blt_ring_sync_to,
1496 .semaphore_register = {MI_SEMAPHORE_SYNC_BR,
1497 MI_SEMAPHORE_SYNC_BV,
1498 MI_SEMAPHORE_SYNC_INVALID},
1499 .signal_mbox = {GEN6_RBSYNC, GEN6_VBSYNC},
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001500};
1501
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001502int intel_init_render_ring_buffer(struct drm_device *dev)
1503{
1504 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001505 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001506
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001507 *ring = render_ring;
1508 if (INTEL_INFO(dev)->gen >= 6) {
1509 ring->add_request = gen6_add_request;
Jesse Barnes8d315282011-10-16 10:23:31 +02001510 ring->flush = gen6_render_ring_flush;
Chris Wilson0f46832f2011-01-04 17:35:21 +00001511 ring->irq_get = gen6_render_ring_get_irq;
1512 ring->irq_put = gen6_render_ring_put_irq;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001513 ring->get_seqno = gen6_ring_get_seqno;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001514 } else if (IS_GEN5(dev)) {
1515 ring->add_request = pc_render_add_request;
1516 ring->get_seqno = pc_render_get_seqno;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001517 }
1518
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001519 if (!I915_NEED_GFX_HWS(dev)) {
1520 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1521 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1522 }
1523
1524 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001525}
1526
Chris Wilsone8616b62011-01-20 09:57:11 +00001527int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1528{
1529 drm_i915_private_t *dev_priv = dev->dev_private;
1530 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1531
1532 *ring = render_ring;
1533 if (INTEL_INFO(dev)->gen >= 6) {
1534 ring->add_request = gen6_add_request;
1535 ring->irq_get = gen6_render_ring_get_irq;
1536 ring->irq_put = gen6_render_ring_put_irq;
1537 } else if (IS_GEN5(dev)) {
1538 ring->add_request = pc_render_add_request;
1539 ring->get_seqno = pc_render_get_seqno;
1540 }
1541
Keith Packardf3234702011-07-22 10:44:39 -07001542 if (!I915_NEED_GFX_HWS(dev))
1543 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1544
Chris Wilsone8616b62011-01-20 09:57:11 +00001545 ring->dev = dev;
1546 INIT_LIST_HEAD(&ring->active_list);
1547 INIT_LIST_HEAD(&ring->request_list);
1548 INIT_LIST_HEAD(&ring->gpu_write_list);
1549
1550 ring->size = size;
1551 ring->effective_size = ring->size;
1552 if (IS_I830(ring->dev))
1553 ring->effective_size -= 128;
1554
1555 ring->map.offset = start;
1556 ring->map.size = size;
1557 ring->map.type = 0;
1558 ring->map.flags = 0;
1559 ring->map.mtrr = 0;
1560
1561 drm_core_ioremap_wc(&ring->map, dev);
1562 if (ring->map.handle == NULL) {
1563 DRM_ERROR("can not ioremap virtual address for"
1564 " ring buffer\n");
1565 return -ENOMEM;
1566 }
1567
1568 ring->virtual_start = (void __force __iomem *)ring->map.handle;
1569 return 0;
1570}
1571
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001572int intel_init_bsd_ring_buffer(struct drm_device *dev)
1573{
1574 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001575 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001576
Jesse Barnes65d3eb12011-04-06 14:54:44 -07001577 if (IS_GEN6(dev) || IS_GEN7(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001578 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001579 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001580 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001581
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001582 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001583}
Chris Wilson549f7362010-10-19 11:19:32 +01001584
1585int intel_init_blt_ring_buffer(struct drm_device *dev)
1586{
1587 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001588 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001589
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001590 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001591
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001592 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001593}