blob: 62892a826edec6df73de3d2b49edfc893d6e01a5 [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 Wilson6f392d52010-08-07 11:01:22 +010061 u32 cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +000062 int ret;
Chris Wilson6f392d52010-08-07 11:01:22 +010063
Chris Wilson36d527d2011-03-19 22:26:49 +000064 /*
65 * read/write caches:
66 *
67 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
68 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
69 * also flushed at 2d versus 3d pipeline switches.
70 *
71 * read-only caches:
72 *
73 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
74 * MI_READ_FLUSH is set, and is always flushed on 965.
75 *
76 * I915_GEM_DOMAIN_COMMAND may not exist?
77 *
78 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
79 * invalidated when MI_EXE_FLUSH is set.
80 *
81 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
82 * invalidated with every MI_FLUSH.
83 *
84 * TLBs:
85 *
86 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
87 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
88 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
89 * are flushed at any MI_FLUSH.
90 */
91
92 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
93 if ((invalidate_domains|flush_domains) &
94 I915_GEM_DOMAIN_RENDER)
95 cmd &= ~MI_NO_WRITE_FLUSH;
96 if (INTEL_INFO(dev)->gen < 4) {
Eric Anholt62fdfea2010-05-21 13:26:39 -070097 /*
Chris Wilson36d527d2011-03-19 22:26:49 +000098 * On the 965, the sampler cache always gets flushed
99 * and this bit is reserved.
Eric Anholt62fdfea2010-05-21 13:26:39 -0700100 */
Chris Wilson36d527d2011-03-19 22:26:49 +0000101 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
102 cmd |= MI_READ_FLUSH;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800103 }
Chris Wilson36d527d2011-03-19 22:26:49 +0000104 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
105 cmd |= MI_EXE_FLUSH;
106
107 if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
108 (IS_G4X(dev) || IS_GEN5(dev)))
109 cmd |= MI_INVALIDATE_ISP;
110
111 ret = intel_ring_begin(ring, 2);
112 if (ret)
113 return ret;
114
115 intel_ring_emit(ring, cmd);
116 intel_ring_emit(ring, MI_NOOP);
117 intel_ring_advance(ring);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000118
119 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800120}
121
Jesse Barnes8d315282011-10-16 10:23:31 +0200122/**
123 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
124 * implementing two workarounds on gen6. From section 1.4.7.1
125 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
126 *
127 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
128 * produced by non-pipelined state commands), software needs to first
129 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
130 * 0.
131 *
132 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
133 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
134 *
135 * And the workaround for these two requires this workaround first:
136 *
137 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
138 * BEFORE the pipe-control with a post-sync op and no write-cache
139 * flushes.
140 *
141 * And this last workaround is tricky because of the requirements on
142 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
143 * volume 2 part 1:
144 *
145 * "1 of the following must also be set:
146 * - Render Target Cache Flush Enable ([12] of DW1)
147 * - Depth Cache Flush Enable ([0] of DW1)
148 * - Stall at Pixel Scoreboard ([1] of DW1)
149 * - Depth Stall ([13] of DW1)
150 * - Post-Sync Operation ([13] of DW1)
151 * - Notify Enable ([8] of DW1)"
152 *
153 * The cache flushes require the workaround flush that triggered this
154 * one, so we can't use it. Depth stall would trigger the same.
155 * Post-sync nonzero is what triggered this second workaround, so we
156 * can't use that one either. Notify enable is IRQs, which aren't
157 * really our business. That leaves only stall at scoreboard.
158 */
159static int
160intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
161{
162 struct pipe_control *pc = ring->private;
163 u32 scratch_addr = pc->gtt_offset + 128;
164 int ret;
165
166
167 ret = intel_ring_begin(ring, 6);
168 if (ret)
169 return ret;
170
171 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
172 intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
173 PIPE_CONTROL_STALL_AT_SCOREBOARD);
174 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
175 intel_ring_emit(ring, 0); /* low dword */
176 intel_ring_emit(ring, 0); /* high dword */
177 intel_ring_emit(ring, MI_NOOP);
178 intel_ring_advance(ring);
179
180 ret = intel_ring_begin(ring, 6);
181 if (ret)
182 return ret;
183
184 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
185 intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
186 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
187 intel_ring_emit(ring, 0);
188 intel_ring_emit(ring, 0);
189 intel_ring_emit(ring, MI_NOOP);
190 intel_ring_advance(ring);
191
192 return 0;
193}
194
195static int
196gen6_render_ring_flush(struct intel_ring_buffer *ring,
197 u32 invalidate_domains, u32 flush_domains)
198{
199 u32 flags = 0;
200 struct pipe_control *pc = ring->private;
201 u32 scratch_addr = pc->gtt_offset + 128;
202 int ret;
203
204 /* Force SNB workarounds for PIPE_CONTROL flushes */
205 intel_emit_post_sync_nonzero_flush(ring);
206
207 /* Just flush everything. Experiments have shown that reducing the
208 * number of bits based on the write domains has little performance
209 * impact.
210 */
211 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
212 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
213 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
214 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
215 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
216 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
217 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
218
219 ret = intel_ring_begin(ring, 6);
220 if (ret)
221 return ret;
222
223 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
224 intel_ring_emit(ring, flags);
225 intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
226 intel_ring_emit(ring, 0); /* lower dword */
227 intel_ring_emit(ring, 0); /* uppwer dword */
228 intel_ring_emit(ring, MI_NOOP);
229 intel_ring_advance(ring);
230
231 return 0;
232}
233
Chris Wilson78501ea2010-10-27 12:18:21 +0100234static void ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +0100235 u32 value)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800236{
Chris Wilson78501ea2010-10-27 12:18:21 +0100237 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson297b0c52010-10-22 17:02:41 +0100238 I915_WRITE_TAIL(ring, value);
Xiang, Haihaod46eefa2010-09-16 10:43:12 +0800239}
240
Chris Wilson78501ea2010-10-27 12:18:21 +0100241u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800242{
Chris Wilson78501ea2010-10-27 12:18:21 +0100243 drm_i915_private_t *dev_priv = ring->dev->dev_private;
244 u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
Daniel Vetter3d281d82010-09-24 21:14:22 +0200245 RING_ACTHD(ring->mmio_base) : ACTHD;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800246
247 return I915_READ(acthd_reg);
248}
249
Chris Wilson78501ea2010-10-27 12:18:21 +0100250static int init_ring_common(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800251{
Chris Wilson78501ea2010-10-27 12:18:21 +0100252 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000253 struct drm_i915_gem_object *obj = ring->obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800254 u32 head;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800255
256 /* Stop the ring if it's running. */
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200257 I915_WRITE_CTL(ring, 0);
Daniel Vetter570ef602010-08-02 17:06:23 +0200258 I915_WRITE_HEAD(ring, 0);
Chris Wilson78501ea2010-10-27 12:18:21 +0100259 ring->write_tail(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800260
261 /* Initialize the ring. */
Chris Wilson05394f32010-11-08 19:18:58 +0000262 I915_WRITE_START(ring, obj->gtt_offset);
Daniel Vetter570ef602010-08-02 17:06:23 +0200263 head = I915_READ_HEAD(ring) & HEAD_ADDR;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800264
265 /* G45 ring initialization fails to reset head to zero */
266 if (head != 0) {
Chris Wilson6fd0d562010-12-05 20:42:33 +0000267 DRM_DEBUG_KMS("%s head not reset to zero "
268 "ctl %08x head %08x tail %08x start %08x\n",
269 ring->name,
270 I915_READ_CTL(ring),
271 I915_READ_HEAD(ring),
272 I915_READ_TAIL(ring),
273 I915_READ_START(ring));
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800274
Daniel Vetter570ef602010-08-02 17:06:23 +0200275 I915_WRITE_HEAD(ring, 0);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800276
Chris Wilson6fd0d562010-12-05 20:42:33 +0000277 if (I915_READ_HEAD(ring) & HEAD_ADDR) {
278 DRM_ERROR("failed to set %s head to zero "
279 "ctl %08x head %08x tail %08x start %08x\n",
280 ring->name,
281 I915_READ_CTL(ring),
282 I915_READ_HEAD(ring),
283 I915_READ_TAIL(ring),
284 I915_READ_START(ring));
285 }
Eric Anholt62fdfea2010-05-21 13:26:39 -0700286 }
287
Daniel Vetter7f2ab692010-08-02 17:06:59 +0200288 I915_WRITE_CTL(ring,
Chris Wilsonae69b422010-11-07 11:45:52 +0000289 ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
Chris Wilson5d031e52012-02-08 13:34:13 +0000290 | RING_VALID);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800291
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800292 /* If the head is still not zero, the ring is dead */
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
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700401
Daniel Vetter2e7a4482012-05-06 16:50:24 +0200402 if (IS_GEN6(dev)) {
Kenneth Graunke3a69ddd2012-04-27 12:44:41 -0700403 /* From the Sandybridge PRM, volume 1 part 3, page 24:
404 * "If this bit is set, STCunit will have LRA as replacement
405 * policy. [...] This bit must be reset. LRA replacement
406 * policy is not supported."
407 */
408 I915_WRITE(CACHE_MODE_0,
409 CM0_STC_EVICT_DISABLE_LRA_SNB << CM0_MASK_SHIFT);
Ben Widawsky84f9f932011-12-12 19:21:58 -0800410 }
411
Daniel Vetter2e7a4482012-05-06 16:50:24 +0200412 if (INTEL_INFO(dev)->gen >= 6) {
413 I915_WRITE(INSTPM,
414 INSTPM_FORCE_ORDERING << 16 | INSTPM_FORCE_ORDERING);
415 }
416
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800417 return ret;
418}
419
Chris Wilsonc6df5412010-12-15 09:56:50 +0000420static void render_ring_cleanup(struct intel_ring_buffer *ring)
421{
422 if (!ring->private)
423 return;
424
425 cleanup_pipe_control(ring);
426}
427
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000428static void
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700429update_mboxes(struct intel_ring_buffer *ring,
430 u32 seqno,
431 u32 mmio_offset)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000432{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700433 intel_ring_emit(ring, MI_SEMAPHORE_MBOX |
434 MI_SEMAPHORE_GLOBAL_GTT |
435 MI_SEMAPHORE_REGISTER |
436 MI_SEMAPHORE_UPDATE);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000437 intel_ring_emit(ring, seqno);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700438 intel_ring_emit(ring, mmio_offset);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000439}
440
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700441/**
442 * gen6_add_request - Update the semaphore mailbox registers
443 *
444 * @ring - ring that is adding a request
445 * @seqno - return seqno stuck into the ring
446 *
447 * Update the mailbox registers in the *other* rings with the current seqno.
448 * This acts like a signal in the canonical semaphore.
449 */
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000450static int
451gen6_add_request(struct intel_ring_buffer *ring,
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700452 u32 *seqno)
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000453{
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700454 u32 mbox1_reg;
455 u32 mbox2_reg;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000456 int ret;
457
458 ret = intel_ring_begin(ring, 10);
459 if (ret)
460 return ret;
461
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700462 mbox1_reg = ring->signal_mbox[0];
463 mbox2_reg = ring->signal_mbox[1];
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000464
Daniel Vetter53d227f2012-01-25 16:32:49 +0100465 *seqno = i915_gem_next_request_seqno(ring);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700466
467 update_mboxes(ring, *seqno, mbox1_reg);
468 update_mboxes(ring, *seqno, mbox2_reg);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000469 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
470 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700471 intel_ring_emit(ring, *seqno);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000472 intel_ring_emit(ring, MI_USER_INTERRUPT);
473 intel_ring_advance(ring);
474
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000475 return 0;
476}
477
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700478/**
479 * intel_ring_sync - sync the waiter to the signaller on seqno
480 *
481 * @waiter - ring that is waiting
482 * @signaller - ring which has, or will signal
483 * @seqno - seqno which the waiter will block on
484 */
485static int
486intel_ring_sync(struct intel_ring_buffer *waiter,
487 struct intel_ring_buffer *signaller,
488 int ring,
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000489 u32 seqno)
490{
491 int ret;
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700492 u32 dw1 = MI_SEMAPHORE_MBOX |
493 MI_SEMAPHORE_COMPARE |
494 MI_SEMAPHORE_REGISTER;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000495
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700496 ret = intel_ring_begin(waiter, 4);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000497 if (ret)
498 return ret;
499
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700500 intel_ring_emit(waiter, dw1 | signaller->semaphore_register[ring]);
501 intel_ring_emit(waiter, seqno);
502 intel_ring_emit(waiter, 0);
503 intel_ring_emit(waiter, MI_NOOP);
504 intel_ring_advance(waiter);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000505
506 return 0;
507}
508
Ben Widawskyc8c99b02011-09-14 20:32:47 -0700509/* VCS->RCS (RVSYNC) or BCS->RCS (RBSYNC) */
510int
511render_ring_sync_to(struct intel_ring_buffer *waiter,
512 struct intel_ring_buffer *signaller,
513 u32 seqno)
514{
515 WARN_ON(signaller->semaphore_register[RCS] == MI_SEMAPHORE_SYNC_INVALID);
516 return intel_ring_sync(waiter,
517 signaller,
518 RCS,
519 seqno);
520}
521
522/* RCS->VCS (VRSYNC) or BCS->VCS (VBSYNC) */
523int
524gen6_bsd_ring_sync_to(struct intel_ring_buffer *waiter,
525 struct intel_ring_buffer *signaller,
526 u32 seqno)
527{
528 WARN_ON(signaller->semaphore_register[VCS] == MI_SEMAPHORE_SYNC_INVALID);
529 return intel_ring_sync(waiter,
530 signaller,
531 VCS,
532 seqno);
533}
534
535/* RCS->BCS (BRSYNC) or VCS->BCS (BVSYNC) */
536int
537gen6_blt_ring_sync_to(struct intel_ring_buffer *waiter,
538 struct intel_ring_buffer *signaller,
539 u32 seqno)
540{
541 WARN_ON(signaller->semaphore_register[BCS] == MI_SEMAPHORE_SYNC_INVALID);
542 return intel_ring_sync(waiter,
543 signaller,
544 BCS,
545 seqno);
546}
547
548
549
Chris Wilsonc6df5412010-12-15 09:56:50 +0000550#define PIPE_CONTROL_FLUSH(ring__, addr__) \
551do { \
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200552 intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
553 PIPE_CONTROL_DEPTH_STALL); \
Chris Wilsonc6df5412010-12-15 09:56:50 +0000554 intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
555 intel_ring_emit(ring__, 0); \
556 intel_ring_emit(ring__, 0); \
557} while (0)
558
559static int
560pc_render_add_request(struct intel_ring_buffer *ring,
561 u32 *result)
562{
Daniel Vetter53d227f2012-01-25 16:32:49 +0100563 u32 seqno = i915_gem_next_request_seqno(ring);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000564 struct pipe_control *pc = ring->private;
565 u32 scratch_addr = pc->gtt_offset + 128;
566 int ret;
567
568 /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
569 * incoherent with writes to memory, i.e. completely fubar,
570 * so we need to use PIPE_NOTIFY instead.
571 *
572 * However, we also need to workaround the qword write
573 * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
574 * memory before requesting an interrupt.
575 */
576 ret = intel_ring_begin(ring, 32);
577 if (ret)
578 return ret;
579
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200580 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200581 PIPE_CONTROL_WRITE_FLUSH |
582 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
Chris Wilsonc6df5412010-12-15 09:56:50 +0000583 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
584 intel_ring_emit(ring, seqno);
585 intel_ring_emit(ring, 0);
586 PIPE_CONTROL_FLUSH(ring, scratch_addr);
587 scratch_addr += 128; /* write to separate cachelines */
588 PIPE_CONTROL_FLUSH(ring, scratch_addr);
589 scratch_addr += 128;
590 PIPE_CONTROL_FLUSH(ring, scratch_addr);
591 scratch_addr += 128;
592 PIPE_CONTROL_FLUSH(ring, scratch_addr);
593 scratch_addr += 128;
594 PIPE_CONTROL_FLUSH(ring, scratch_addr);
595 scratch_addr += 128;
596 PIPE_CONTROL_FLUSH(ring, scratch_addr);
Chris Wilsona71d8d92012-02-15 11:25:36 +0000597
Kenneth Graunkefcbc34e2011-10-11 23:41:08 +0200598 intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
Kenneth Graunke9d971b32011-10-11 23:41:09 +0200599 PIPE_CONTROL_WRITE_FLUSH |
600 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
Chris Wilsonc6df5412010-12-15 09:56:50 +0000601 PIPE_CONTROL_NOTIFY);
602 intel_ring_emit(ring, pc->gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
603 intel_ring_emit(ring, seqno);
604 intel_ring_emit(ring, 0);
605 intel_ring_advance(ring);
606
607 *result = seqno;
608 return 0;
609}
610
Chris Wilson3cce4692010-10-27 16:11:02 +0100611static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100612render_ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100613 u32 *result)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700614{
Daniel Vetter53d227f2012-01-25 16:32:49 +0100615 u32 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson3cce4692010-10-27 16:11:02 +0100616 int ret;
Zhenyu Wangca764822010-05-27 10:26:42 +0800617
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000618 ret = intel_ring_begin(ring, 4);
619 if (ret)
620 return ret;
Chris Wilson3cce4692010-10-27 16:11:02 +0100621
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000622 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
623 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
624 intel_ring_emit(ring, seqno);
625 intel_ring_emit(ring, MI_USER_INTERRUPT);
Chris Wilson3cce4692010-10-27 16:11:02 +0100626 intel_ring_advance(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000627
Chris Wilson3cce4692010-10-27 16:11:02 +0100628 *result = seqno;
629 return 0;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700630}
631
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800632static u32
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100633gen6_ring_get_seqno(struct intel_ring_buffer *ring)
634{
635 struct drm_device *dev = ring->dev;
636
637 /* Workaround to force correct ordering between irq and seqno writes on
638 * ivb (and maybe also on snb) by reading from a CS register (like
639 * ACTHD) before reading the status page. */
Daniel Vetter1c7eaac2012-03-27 09:31:24 +0200640 if (IS_GEN6(dev) || IS_GEN7(dev))
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100641 intel_ring_get_active_head(ring);
642 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
643}
644
645static u32
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000646ring_get_seqno(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800647{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000648 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
649}
650
Chris Wilsonc6df5412010-12-15 09:56:50 +0000651static u32
652pc_render_get_seqno(struct intel_ring_buffer *ring)
653{
654 struct pipe_control *pc = ring->private;
655 return pc->cpu_page[0];
656}
657
Chris Wilson0f468322011-01-04 17:35:21 +0000658static void
659ironlake_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
660{
661 dev_priv->gt_irq_mask &= ~mask;
662 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
663 POSTING_READ(GTIMR);
664}
665
666static void
667ironlake_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
668{
669 dev_priv->gt_irq_mask |= mask;
670 I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
671 POSTING_READ(GTIMR);
672}
673
674static void
675i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
676{
677 dev_priv->irq_mask &= ~mask;
678 I915_WRITE(IMR, dev_priv->irq_mask);
679 POSTING_READ(IMR);
680}
681
682static void
683i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
684{
685 dev_priv->irq_mask |= mask;
686 I915_WRITE(IMR, dev_priv->irq_mask);
687 POSTING_READ(IMR);
688}
689
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000690static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000691render_ring_get_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700692{
Chris Wilson78501ea2010-10-27 12:18:21 +0100693 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000694 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700695
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000696 if (!dev->irq_enabled)
697 return false;
698
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000699 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000700 if (ring->irq_refcount++ == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700701 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000702 ironlake_enable_irq(dev_priv,
703 GT_PIPE_NOTIFY | GT_USER_INTERRUPT);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700704 else
705 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
706 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000707 spin_unlock(&ring->irq_lock);
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000708
709 return true;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700710}
711
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800712static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000713render_ring_put_irq(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700714{
Chris Wilson78501ea2010-10-27 12:18:21 +0100715 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000716 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700717
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000718 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000719 if (--ring->irq_refcount == 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700720 if (HAS_PCH_SPLIT(dev))
Chris Wilson0f468322011-01-04 17:35:21 +0000721 ironlake_disable_irq(dev_priv,
722 GT_USER_INTERRUPT |
723 GT_PIPE_NOTIFY);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700724 else
725 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
726 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000727 spin_unlock(&ring->irq_lock);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700728}
729
Chris Wilson78501ea2010-10-27 12:18:21 +0100730void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800731{
Eric Anholt45930102011-05-06 17:12:35 -0700732 struct drm_device *dev = ring->dev;
Chris Wilson78501ea2010-10-27 12:18:21 +0100733 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Eric Anholt45930102011-05-06 17:12:35 -0700734 u32 mmio = 0;
735
736 /* The ring status page addresses are no longer next to the rest of
737 * the ring registers as of gen7.
738 */
739 if (IS_GEN7(dev)) {
740 switch (ring->id) {
Daniel Vetter96154f22011-12-14 13:57:00 +0100741 case RCS:
Eric Anholt45930102011-05-06 17:12:35 -0700742 mmio = RENDER_HWS_PGA_GEN7;
743 break;
Daniel Vetter96154f22011-12-14 13:57:00 +0100744 case BCS:
Eric Anholt45930102011-05-06 17:12:35 -0700745 mmio = BLT_HWS_PGA_GEN7;
746 break;
Daniel Vetter96154f22011-12-14 13:57:00 +0100747 case VCS:
Eric Anholt45930102011-05-06 17:12:35 -0700748 mmio = BSD_HWS_PGA_GEN7;
749 break;
750 }
751 } else if (IS_GEN6(ring->dev)) {
752 mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
753 } else {
754 mmio = RING_HWS_PGA(ring->mmio_base);
755 }
756
Chris Wilson78501ea2010-10-27 12:18:21 +0100757 I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
758 POSTING_READ(mmio);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800759}
760
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000761static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100762bsd_ring_flush(struct intel_ring_buffer *ring,
763 u32 invalidate_domains,
764 u32 flush_domains)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800765{
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000766 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000767
Chris Wilsonb72f3ac2011-01-04 17:34:02 +0000768 ret = intel_ring_begin(ring, 2);
769 if (ret)
770 return ret;
771
772 intel_ring_emit(ring, MI_FLUSH);
773 intel_ring_emit(ring, MI_NOOP);
774 intel_ring_advance(ring);
775 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800776}
777
Chris Wilson3cce4692010-10-27 16:11:02 +0100778static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100779ring_add_request(struct intel_ring_buffer *ring,
Chris Wilson3cce4692010-10-27 16:11:02 +0100780 u32 *result)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800781{
782 u32 seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +0100783 int ret;
784
785 ret = intel_ring_begin(ring, 4);
786 if (ret)
787 return ret;
Chris Wilson6f392d52010-08-07 11:01:22 +0100788
Daniel Vetter53d227f2012-01-25 16:32:49 +0100789 seqno = i915_gem_next_request_seqno(ring);
Chris Wilson6f392d52010-08-07 11:01:22 +0100790
Chris Wilson3cce4692010-10-27 16:11:02 +0100791 intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
792 intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
793 intel_ring_emit(ring, seqno);
794 intel_ring_emit(ring, MI_USER_INTERRUPT);
795 intel_ring_advance(ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800796
Chris Wilson3cce4692010-10-27 16:11:02 +0100797 *result = seqno;
798 return 0;
Zou Nan haid1b851f2010-05-21 09:08:57 +0800799}
800
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000801static bool
Chris Wilson0f468322011-01-04 17:35:21 +0000802gen6_ring_get_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
803{
804 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000805 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000806
807 if (!dev->irq_enabled)
808 return false;
809
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100810 /* It looks like we need to prevent the gt from suspending while waiting
811 * for an notifiy irq, otherwise irqs seem to get lost on at least the
812 * blt/bsd rings on ivb. */
Daniel Vetter99ffa162012-01-25 14:04:00 +0100813 gen6_gt_force_wake_get(dev_priv);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100814
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000815 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000816 if (ring->irq_refcount++ == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000817 ring->irq_mask &= ~rflag;
818 I915_WRITE_IMR(ring, ring->irq_mask);
819 ironlake_enable_irq(dev_priv, gflag);
Chris Wilson0f468322011-01-04 17:35:21 +0000820 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000821 spin_unlock(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +0000822
823 return true;
824}
825
826static void
827gen6_ring_put_irq(struct intel_ring_buffer *ring, u32 gflag, u32 rflag)
828{
829 struct drm_device *dev = ring->dev;
Chris Wilson01a03332011-01-04 22:22:56 +0000830 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson0f468322011-01-04 17:35:21 +0000831
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000832 spin_lock(&ring->irq_lock);
Chris Wilson01a03332011-01-04 22:22:56 +0000833 if (--ring->irq_refcount == 0) {
Chris Wilson0f468322011-01-04 17:35:21 +0000834 ring->irq_mask |= rflag;
835 I915_WRITE_IMR(ring, ring->irq_mask);
836 ironlake_disable_irq(dev_priv, gflag);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000837 }
Chris Wilson0dc79fb2011-01-05 10:32:24 +0000838 spin_unlock(&ring->irq_lock);
Daniel Vetter4cd53c02012-12-14 16:01:25 +0100839
Daniel Vetter99ffa162012-01-25 14:04:00 +0100840 gen6_gt_force_wake_put(dev_priv);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000841}
842
Chris Wilsonb13c2b92010-12-13 16:54:50 +0000843static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000844bsd_ring_get_irq(struct intel_ring_buffer *ring)
845{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800846 struct drm_device *dev = ring->dev;
847 drm_i915_private_t *dev_priv = dev->dev_private;
848
849 if (!dev->irq_enabled)
850 return false;
851
852 spin_lock(&ring->irq_lock);
853 if (ring->irq_refcount++ == 0) {
854 if (IS_G4X(dev))
855 i915_enable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
856 else
857 ironlake_enable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
858 }
859 spin_unlock(&ring->irq_lock);
860
861 return true;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000862}
863static void
864bsd_ring_put_irq(struct intel_ring_buffer *ring)
865{
Feng, Boqun5bfa1062011-05-16 16:02:39 +0800866 struct drm_device *dev = ring->dev;
867 drm_i915_private_t *dev_priv = dev->dev_private;
868
869 spin_lock(&ring->irq_lock);
870 if (--ring->irq_refcount == 0) {
871 if (IS_G4X(dev))
872 i915_disable_irq(dev_priv, I915_BSD_USER_INTERRUPT);
873 else
874 ironlake_disable_irq(dev_priv, GT_BSD_USER_INTERRUPT);
875 }
876 spin_unlock(&ring->irq_lock);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800877}
878
879static int
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000880ring_dispatch_execbuffer(struct intel_ring_buffer *ring, u32 offset, u32 length)
Zou Nan haid1b851f2010-05-21 09:08:57 +0800881{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100882 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +0100883
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100884 ret = intel_ring_begin(ring, 2);
885 if (ret)
886 return ret;
887
Chris Wilson78501ea2010-10-27 12:18:21 +0100888 intel_ring_emit(ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000889 MI_BATCH_BUFFER_START | (2 << 6) |
Chris Wilson78501ea2010-10-27 12:18:21 +0100890 MI_BATCH_NON_SECURE_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000891 intel_ring_emit(ring, offset);
Chris Wilson78501ea2010-10-27 12:18:21 +0100892 intel_ring_advance(ring);
893
Zou Nan haid1b851f2010-05-21 09:08:57 +0800894 return 0;
895}
896
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800897static int
Chris Wilson78501ea2010-10-27 12:18:21 +0100898render_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000899 u32 offset, u32 len)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700900{
Chris Wilson78501ea2010-10-27 12:18:21 +0100901 struct drm_device *dev = ring->dev;
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000902 int ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700903
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000904 if (IS_I830(dev) || IS_845G(dev)) {
905 ret = intel_ring_begin(ring, 4);
906 if (ret)
907 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700908
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000909 intel_ring_emit(ring, MI_BATCH_BUFFER);
910 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
911 intel_ring_emit(ring, offset + len - 8);
912 intel_ring_emit(ring, 0);
913 } else {
914 ret = intel_ring_begin(ring, 2);
915 if (ret)
916 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100917
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000918 if (INTEL_INFO(dev)->gen >= 4) {
919 intel_ring_emit(ring,
920 MI_BATCH_BUFFER_START | (2 << 6) |
921 MI_BATCH_NON_SECURE_I965);
922 intel_ring_emit(ring, offset);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700923 } else {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000924 intel_ring_emit(ring,
925 MI_BATCH_BUFFER_START | (2 << 6));
926 intel_ring_emit(ring, offset | MI_BATCH_NON_SECURE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700927 }
928 }
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000929 intel_ring_advance(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700930
Eric Anholt62fdfea2010-05-21 13:26:39 -0700931 return 0;
932}
933
Chris Wilson78501ea2010-10-27 12:18:21 +0100934static void cleanup_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700935{
Chris Wilson78501ea2010-10-27 12:18:21 +0100936 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000937 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700938
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800939 obj = ring->status_page.obj;
940 if (obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700941 return;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700942
Chris Wilson05394f32010-11-08 19:18:58 +0000943 kunmap(obj->pages[0]);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700944 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000945 drm_gem_object_unreference(&obj->base);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800946 ring->status_page.obj = NULL;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700947
948 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700949}
950
Chris Wilson78501ea2010-10-27 12:18:21 +0100951static int init_status_page(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700952{
Chris Wilson78501ea2010-10-27 12:18:21 +0100953 struct drm_device *dev = ring->dev;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700954 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000955 struct drm_i915_gem_object *obj;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700956 int ret;
957
Eric Anholt62fdfea2010-05-21 13:26:39 -0700958 obj = i915_gem_alloc_object(dev, 4096);
959 if (obj == NULL) {
960 DRM_ERROR("Failed to allocate status page\n");
961 ret = -ENOMEM;
962 goto err;
963 }
Chris Wilsone4ffd172011-04-04 09:44:39 +0100964
965 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700966
Daniel Vetter75e9e912010-11-04 17:11:09 +0100967 ret = i915_gem_object_pin(obj, 4096, true);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700968 if (ret != 0) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700969 goto err_unref;
970 }
971
Chris Wilson05394f32010-11-08 19:18:58 +0000972 ring->status_page.gfx_addr = obj->gtt_offset;
973 ring->status_page.page_addr = kmap(obj->pages[0]);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800974 if (ring->status_page.page_addr == NULL) {
Eric Anholt62fdfea2010-05-21 13:26:39 -0700975 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
Eric Anholt62fdfea2010-05-21 13:26:39 -0700976 goto err_unpin;
977 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800978 ring->status_page.obj = obj;
979 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700980
Chris Wilson78501ea2010-10-27 12:18:21 +0100981 intel_ring_setup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800982 DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
983 ring->name, ring->status_page.gfx_addr);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700984
985 return 0;
986
987err_unpin:
988 i915_gem_object_unpin(obj);
989err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +0000990 drm_gem_object_unreference(&obj->base);
Eric Anholt62fdfea2010-05-21 13:26:39 -0700991err:
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800992 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -0700993}
994
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800995int intel_init_ring_buffer(struct drm_device *dev,
Chris Wilsonab6f8e32010-09-19 17:53:44 +0100996 struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -0700997{
Chris Wilson05394f32010-11-08 19:18:58 +0000998 struct drm_i915_gem_object *obj;
Chris Wilsondd785e32010-08-07 11:01:34 +0100999 int ret;
1000
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001001 ring->dev = dev;
Chris Wilson23bc5982010-09-29 16:10:57 +01001002 INIT_LIST_HEAD(&ring->active_list);
1003 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01001004 INIT_LIST_HEAD(&ring->gpu_write_list);
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001005
Chris Wilsonb259f672011-03-29 13:19:09 +01001006 init_waitqueue_head(&ring->irq_queue);
Chris Wilson0dc79fb2011-01-05 10:32:24 +00001007 spin_lock_init(&ring->irq_lock);
Chris Wilson0f468322011-01-04 17:35:21 +00001008 ring->irq_mask = ~0;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001009
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001010 if (I915_NEED_GFX_HWS(dev)) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001011 ret = init_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001012 if (ret)
1013 return ret;
1014 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001015
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001016 obj = i915_gem_alloc_object(dev, ring->size);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001017 if (obj == NULL) {
1018 DRM_ERROR("Failed to allocate ringbuffer\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001019 ret = -ENOMEM;
Chris Wilsondd785e32010-08-07 11:01:34 +01001020 goto err_hws;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001021 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001022
Chris Wilson05394f32010-11-08 19:18:58 +00001023 ring->obj = obj;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001024
Daniel Vetter75e9e912010-11-04 17:11:09 +01001025 ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
Chris Wilsondd785e32010-08-07 11:01:34 +01001026 if (ret)
1027 goto err_unref;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001028
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001029 ring->map.size = ring->size;
Chris Wilson05394f32010-11-08 19:18:58 +00001030 ring->map.offset = dev->agp->base + obj->gtt_offset;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001031 ring->map.type = 0;
1032 ring->map.flags = 0;
1033 ring->map.mtrr = 0;
1034
1035 drm_core_ioremap_wc(&ring->map, dev);
1036 if (ring->map.handle == NULL) {
1037 DRM_ERROR("Failed to map ringbuffer.\n");
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001038 ret = -EINVAL;
Chris Wilsondd785e32010-08-07 11:01:34 +01001039 goto err_unpin;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001040 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001041
Eric Anholt62fdfea2010-05-21 13:26:39 -07001042 ring->virtual_start = ring->map.handle;
Chris Wilson78501ea2010-10-27 12:18:21 +01001043 ret = ring->init(ring);
Chris Wilsondd785e32010-08-07 11:01:34 +01001044 if (ret)
1045 goto err_unmap;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001046
Chris Wilson55249ba2010-12-22 14:04:47 +00001047 /* Workaround an erratum on the i830 which causes a hang if
1048 * the TAIL pointer points to within the last 2 cachelines
1049 * of the buffer.
1050 */
1051 ring->effective_size = ring->size;
Chris Wilson27c1cbd2012-04-09 13:59:46 +01001052 if (IS_I830(ring->dev) || IS_845G(ring->dev))
Chris Wilson55249ba2010-12-22 14:04:47 +00001053 ring->effective_size -= 128;
1054
Chris Wilsonc584fe42010-10-29 18:15:52 +01001055 return 0;
Chris Wilsondd785e32010-08-07 11:01:34 +01001056
1057err_unmap:
1058 drm_core_ioremapfree(&ring->map, dev);
1059err_unpin:
1060 i915_gem_object_unpin(obj);
1061err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001062 drm_gem_object_unreference(&obj->base);
1063 ring->obj = NULL;
Chris Wilsondd785e32010-08-07 11:01:34 +01001064err_hws:
Chris Wilson78501ea2010-10-27 12:18:21 +01001065 cleanup_status_page(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001066 return ret;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001067}
1068
Chris Wilson78501ea2010-10-27 12:18:21 +01001069void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001070{
Chris Wilson33626e62010-10-29 16:18:36 +01001071 struct drm_i915_private *dev_priv;
1072 int ret;
1073
Chris Wilson05394f32010-11-08 19:18:58 +00001074 if (ring->obj == NULL)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001075 return;
1076
Chris Wilson33626e62010-10-29 16:18:36 +01001077 /* Disable the ring buffer. The ring must be idle at this point */
1078 dev_priv = ring->dev->dev_private;
Ben Widawsky96f298a2011-03-19 18:14:27 -07001079 ret = intel_wait_ring_idle(ring);
Chris Wilson29ee3992011-01-24 16:35:42 +00001080 if (ret)
1081 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
1082 ring->name, ret);
1083
Chris Wilson33626e62010-10-29 16:18:36 +01001084 I915_WRITE_CTL(ring, 0);
1085
Chris Wilson78501ea2010-10-27 12:18:21 +01001086 drm_core_ioremapfree(&ring->map, ring->dev);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001087
Chris Wilson05394f32010-11-08 19:18:58 +00001088 i915_gem_object_unpin(ring->obj);
1089 drm_gem_object_unreference(&ring->obj->base);
1090 ring->obj = NULL;
Chris Wilson78501ea2010-10-27 12:18:21 +01001091
Zou Nan hai8d192152010-11-02 16:31:01 +08001092 if (ring->cleanup)
1093 ring->cleanup(ring);
1094
Chris Wilson78501ea2010-10-27 12:18:21 +01001095 cleanup_status_page(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001096}
1097
Chris Wilson78501ea2010-10-27 12:18:21 +01001098static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001099{
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001100 unsigned int *virt;
Chris Wilson55249ba2010-12-22 14:04:47 +00001101 int rem = ring->size - ring->tail;
Eric Anholt62fdfea2010-05-21 13:26:39 -07001102
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001103 if (ring->space < rem) {
Chris Wilson78501ea2010-10-27 12:18:21 +01001104 int ret = intel_wait_ring_buffer(ring, rem);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001105 if (ret)
1106 return ret;
1107 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001108
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001109 virt = (unsigned int *)(ring->virtual_start + ring->tail);
Chris Wilson1741dd42010-08-04 15:18:12 +01001110 rem /= 8;
1111 while (rem--) {
Eric Anholt62fdfea2010-05-21 13:26:39 -07001112 *virt++ = MI_NOOP;
Chris Wilson1741dd42010-08-04 15:18:12 +01001113 *virt++ = MI_NOOP;
1114 }
Eric Anholt62fdfea2010-05-21 13:26:39 -07001115
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001116 ring->tail = 0;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001117 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001118
1119 return 0;
1120}
1121
Chris Wilsona71d8d92012-02-15 11:25:36 +00001122static int intel_ring_wait_seqno(struct intel_ring_buffer *ring, u32 seqno)
1123{
1124 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1125 bool was_interruptible;
1126 int ret;
1127
1128 /* XXX As we have not yet audited all the paths to check that
1129 * they are ready for ERESTARTSYS from intel_ring_begin, do not
1130 * allow us to be interruptible by a signal.
1131 */
1132 was_interruptible = dev_priv->mm.interruptible;
1133 dev_priv->mm.interruptible = false;
1134
1135 ret = i915_wait_request(ring, seqno, true);
1136
1137 dev_priv->mm.interruptible = was_interruptible;
1138
1139 return ret;
1140}
1141
1142static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
1143{
1144 struct drm_i915_gem_request *request;
1145 u32 seqno = 0;
1146 int ret;
1147
1148 i915_gem_retire_requests_ring(ring);
1149
1150 if (ring->last_retired_head != -1) {
1151 ring->head = ring->last_retired_head;
1152 ring->last_retired_head = -1;
1153 ring->space = ring_space(ring);
1154 if (ring->space >= n)
1155 return 0;
1156 }
1157
1158 list_for_each_entry(request, &ring->request_list, list) {
1159 int space;
1160
1161 if (request->tail == -1)
1162 continue;
1163
1164 space = request->tail - (ring->tail + 8);
1165 if (space < 0)
1166 space += ring->size;
1167 if (space >= n) {
1168 seqno = request->seqno;
1169 break;
1170 }
1171
1172 /* Consume this request in case we need more space than
1173 * is available and so need to prevent a race between
1174 * updating last_retired_head and direct reads of
1175 * I915_RING_HEAD. It also provides a nice sanity check.
1176 */
1177 request->tail = -1;
1178 }
1179
1180 if (seqno == 0)
1181 return -ENOSPC;
1182
1183 ret = intel_ring_wait_seqno(ring, seqno);
1184 if (ret)
1185 return ret;
1186
1187 if (WARN_ON(ring->last_retired_head == -1))
1188 return -ENOSPC;
1189
1190 ring->head = ring->last_retired_head;
1191 ring->last_retired_head = -1;
1192 ring->space = ring_space(ring);
1193 if (WARN_ON(ring->space < n))
1194 return -ENOSPC;
1195
1196 return 0;
1197}
1198
Chris Wilson78501ea2010-10-27 12:18:21 +01001199int intel_wait_ring_buffer(struct intel_ring_buffer *ring, int n)
Eric Anholt62fdfea2010-05-21 13:26:39 -07001200{
Chris Wilson78501ea2010-10-27 12:18:21 +01001201 struct drm_device *dev = ring->dev;
Zou Nan haicae58522010-11-09 17:17:32 +08001202 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson78501ea2010-10-27 12:18:21 +01001203 unsigned long end;
Chris Wilsona71d8d92012-02-15 11:25:36 +00001204 int ret;
Chris Wilsonc7dca472011-01-20 17:00:10 +00001205
Chris Wilsona71d8d92012-02-15 11:25:36 +00001206 ret = intel_ring_wait_request(ring, n);
1207 if (ret != -ENOSPC)
1208 return ret;
1209
Chris Wilsondb53a302011-02-03 11:57:46 +00001210 trace_i915_ring_wait_begin(ring);
Daniel Vettere6bfaf82011-12-14 13:56:59 +01001211 if (drm_core_check_feature(dev, DRIVER_GEM))
1212 /* With GEM the hangcheck timer should kick us out of the loop,
1213 * leaving it early runs the risk of corrupting GEM state (due
1214 * to running on almost untested codepaths). But on resume
1215 * timers don't work yet, so prevent a complete hang in that
1216 * case by choosing an insanely large timeout. */
1217 end = jiffies + 60 * HZ;
1218 else
1219 end = jiffies + 3 * HZ;
1220
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001221 do {
Chris Wilsonc7dca472011-01-20 17:00:10 +00001222 ring->head = I915_READ_HEAD(ring);
1223 ring->space = ring_space(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001224 if (ring->space >= n) {
Chris Wilsondb53a302011-02-03 11:57:46 +00001225 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001226 return 0;
1227 }
1228
1229 if (dev->primary->master) {
1230 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1231 if (master_priv->sarea_priv)
1232 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
1233 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08001234
Chris Wilsone60a0b12010-10-13 10:09:14 +01001235 msleep(1);
Chris Wilsonf4e0b292010-10-29 21:06:16 +01001236 if (atomic_read(&dev_priv->mm.wedged))
1237 return -EAGAIN;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001238 } while (!time_after(jiffies, end));
Chris Wilsondb53a302011-02-03 11:57:46 +00001239 trace_i915_ring_wait_end(ring);
Eric Anholt62fdfea2010-05-21 13:26:39 -07001240 return -EBUSY;
1241}
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001242
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001243int intel_ring_begin(struct intel_ring_buffer *ring,
1244 int num_dwords)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001245{
Chris Wilson21dd3732011-01-26 15:55:56 +00001246 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Zou Nan haibe26a102010-06-12 17:40:24 +08001247 int n = 4*num_dwords;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001248 int ret;
Chris Wilson78501ea2010-10-27 12:18:21 +01001249
Chris Wilson21dd3732011-01-26 15:55:56 +00001250 if (unlikely(atomic_read(&dev_priv->mm.wedged)))
1251 return -EIO;
1252
Chris Wilson55249ba2010-12-22 14:04:47 +00001253 if (unlikely(ring->tail + n > ring->effective_size)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001254 ret = intel_wrap_ring_buffer(ring);
1255 if (unlikely(ret))
1256 return ret;
1257 }
Chris Wilson78501ea2010-10-27 12:18:21 +01001258
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001259 if (unlikely(ring->space < n)) {
1260 ret = intel_wait_ring_buffer(ring, n);
1261 if (unlikely(ret))
1262 return ret;
1263 }
Chris Wilsond97ed332010-08-04 15:18:13 +01001264
1265 ring->space -= n;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001266 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001267}
1268
Chris Wilson78501ea2010-10-27 12:18:21 +01001269void intel_ring_advance(struct intel_ring_buffer *ring)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001270{
Chris Wilsond97ed332010-08-04 15:18:13 +01001271 ring->tail &= ring->size - 1;
Chris Wilson78501ea2010-10-27 12:18:21 +01001272 ring->write_tail(ring, ring->tail);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001273}
1274
Chris Wilsone0708682010-09-19 14:46:27 +01001275static const struct intel_ring_buffer render_ring = {
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001276 .name = "render ring",
Daniel Vetter96154f22011-12-14 13:57:00 +01001277 .id = RCS,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001278 .mmio_base = RENDER_RING_BASE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001279 .size = 32 * PAGE_SIZE,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001280 .init = init_render_ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001281 .write_tail = ring_write_tail,
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001282 .flush = render_ring_flush,
1283 .add_request = render_ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001284 .get_seqno = ring_get_seqno,
1285 .irq_get = render_ring_get_irq,
1286 .irq_put = render_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001287 .dispatch_execbuffer = render_ring_dispatch_execbuffer,
Akshay Joshi0206e352011-08-16 15:34:10 -04001288 .cleanup = render_ring_cleanup,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001289 .sync_to = render_ring_sync_to,
1290 .semaphore_register = {MI_SEMAPHORE_SYNC_INVALID,
1291 MI_SEMAPHORE_SYNC_RV,
1292 MI_SEMAPHORE_SYNC_RB},
1293 .signal_mbox = {GEN6_VRSYNC, GEN6_BRSYNC},
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001294};
Zou Nan haid1b851f2010-05-21 09:08:57 +08001295
1296/* ring buffer for bit-stream decoder */
1297
Chris Wilsone0708682010-09-19 14:46:27 +01001298static const struct intel_ring_buffer bsd_ring = {
Zou Nan haid1b851f2010-05-21 09:08:57 +08001299 .name = "bsd ring",
Daniel Vetter96154f22011-12-14 13:57:00 +01001300 .id = VCS,
Daniel Vetter333e9fe2010-08-02 16:24:01 +02001301 .mmio_base = BSD_RING_BASE,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001302 .size = 32 * PAGE_SIZE,
Chris Wilson78501ea2010-10-27 12:18:21 +01001303 .init = init_ring_common,
Chris Wilson297b0c52010-10-22 17:02:41 +01001304 .write_tail = ring_write_tail,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001305 .flush = bsd_ring_flush,
Chris Wilson549f7362010-10-19 11:19:32 +01001306 .add_request = ring_add_request,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001307 .get_seqno = ring_get_seqno,
1308 .irq_get = bsd_ring_get_irq,
1309 .irq_put = bsd_ring_put_irq,
Chris Wilson78501ea2010-10-27 12:18:21 +01001310 .dispatch_execbuffer = ring_dispatch_execbuffer,
Zou Nan haid1b851f2010-05-21 09:08:57 +08001311};
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001312
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001313
Chris Wilson78501ea2010-10-27 12:18:21 +01001314static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
Chris Wilson297b0c52010-10-22 17:02:41 +01001315 u32 value)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001316{
Akshay Joshi0206e352011-08-16 15:34:10 -04001317 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001318
1319 /* Every tail move must follow the sequence below */
Akshay Joshi0206e352011-08-16 15:34:10 -04001320 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1321 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1322 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE);
1323 I915_WRITE(GEN6_BSD_RNCID, 0x0);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001324
Akshay Joshi0206e352011-08-16 15:34:10 -04001325 if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
1326 GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0,
1327 50))
1328 DRM_ERROR("timed out waiting for IDLE Indicator\n");
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001329
Akshay Joshi0206e352011-08-16 15:34:10 -04001330 I915_WRITE_TAIL(ring, value);
1331 I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
1332 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK |
1333 GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE);
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001334}
1335
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001336static int gen6_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001337 u32 invalidate, u32 flush)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001338{
Chris Wilson71a77e02011-02-02 12:13:49 +00001339 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001340 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001341
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001342 ret = intel_ring_begin(ring, 4);
1343 if (ret)
1344 return ret;
1345
Chris Wilson71a77e02011-02-02 12:13:49 +00001346 cmd = MI_FLUSH_DW;
1347 if (invalidate & I915_GEM_GPU_DOMAINS)
1348 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
1349 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001350 intel_ring_emit(ring, 0);
1351 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001352 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001353 intel_ring_advance(ring);
1354 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001355}
1356
1357static int
Chris Wilson78501ea2010-10-27 12:18:21 +01001358gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
Chris Wilsonc4e7a412010-11-30 14:10:25 +00001359 u32 offset, u32 len)
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001360{
Akshay Joshi0206e352011-08-16 15:34:10 -04001361 int ret;
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001362
Akshay Joshi0206e352011-08-16 15:34:10 -04001363 ret = intel_ring_begin(ring, 2);
1364 if (ret)
1365 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01001366
Akshay Joshi0206e352011-08-16 15:34:10 -04001367 intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965);
1368 /* bit0-7 is the length on GEN6+ */
1369 intel_ring_emit(ring, offset);
1370 intel_ring_advance(ring);
Chris Wilsonab6f8e32010-09-19 17:53:44 +01001371
Akshay Joshi0206e352011-08-16 15:34:10 -04001372 return 0;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001373}
1374
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001375static bool
Chris Wilson0f468322011-01-04 17:35:21 +00001376gen6_render_ring_get_irq(struct intel_ring_buffer *ring)
1377{
1378 return gen6_ring_get_irq(ring,
1379 GT_USER_INTERRUPT,
1380 GEN6_RENDER_USER_INTERRUPT);
1381}
1382
1383static void
1384gen6_render_ring_put_irq(struct intel_ring_buffer *ring)
1385{
1386 return gen6_ring_put_irq(ring,
1387 GT_USER_INTERRUPT,
1388 GEN6_RENDER_USER_INTERRUPT);
1389}
1390
1391static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001392gen6_bsd_ring_get_irq(struct intel_ring_buffer *ring)
1393{
Chris Wilson0f468322011-01-04 17:35:21 +00001394 return gen6_ring_get_irq(ring,
1395 GT_GEN6_BSD_USER_INTERRUPT,
1396 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001397}
1398
1399static void
1400gen6_bsd_ring_put_irq(struct intel_ring_buffer *ring)
1401{
Chris Wilson0f468322011-01-04 17:35:21 +00001402 return gen6_ring_put_irq(ring,
1403 GT_GEN6_BSD_USER_INTERRUPT,
1404 GEN6_BSD_USER_INTERRUPT);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001405}
1406
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001407/* ring buffer for Video Codec for Gen6+ */
Chris Wilsone0708682010-09-19 14:46:27 +01001408static const struct intel_ring_buffer gen6_bsd_ring = {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001409 .name = "gen6 bsd ring",
Daniel Vetter96154f22011-12-14 13:57:00 +01001410 .id = VCS,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001411 .mmio_base = GEN6_BSD_RING_BASE,
1412 .size = 32 * PAGE_SIZE,
1413 .init = init_ring_common,
1414 .write_tail = gen6_bsd_ring_write_tail,
1415 .flush = gen6_ring_flush,
1416 .add_request = gen6_add_request,
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001417 .get_seqno = gen6_ring_get_seqno,
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001418 .irq_get = gen6_bsd_ring_get_irq,
1419 .irq_put = gen6_bsd_ring_put_irq,
1420 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001421 .sync_to = gen6_bsd_ring_sync_to,
1422 .semaphore_register = {MI_SEMAPHORE_SYNC_VR,
1423 MI_SEMAPHORE_SYNC_INVALID,
1424 MI_SEMAPHORE_SYNC_VB},
1425 .signal_mbox = {GEN6_RVSYNC, GEN6_BVSYNC},
Chris Wilson549f7362010-10-19 11:19:32 +01001426};
1427
1428/* Blitter support (SandyBridge+) */
1429
Chris Wilsonb13c2b92010-12-13 16:54:50 +00001430static bool
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001431blt_ring_get_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001432{
Chris Wilson0f468322011-01-04 17:35:21 +00001433 return gen6_ring_get_irq(ring,
1434 GT_BLT_USER_INTERRUPT,
1435 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001436}
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001437
Chris Wilson549f7362010-10-19 11:19:32 +01001438static void
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001439blt_ring_put_irq(struct intel_ring_buffer *ring)
Chris Wilson549f7362010-10-19 11:19:32 +01001440{
Chris Wilson0f468322011-01-04 17:35:21 +00001441 gen6_ring_put_irq(ring,
1442 GT_BLT_USER_INTERRUPT,
1443 GEN6_BLITTER_USER_INTERRUPT);
Chris Wilson549f7362010-10-19 11:19:32 +01001444}
1445
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001446static int blt_ring_flush(struct intel_ring_buffer *ring,
Chris Wilson71a77e02011-02-02 12:13:49 +00001447 u32 invalidate, u32 flush)
Zou Nan hai8d192152010-11-02 16:31:01 +08001448{
Chris Wilson71a77e02011-02-02 12:13:49 +00001449 uint32_t cmd;
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001450 int ret;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001451
Daniel Vetter6a233c72011-12-14 13:57:07 +01001452 ret = intel_ring_begin(ring, 4);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001453 if (ret)
1454 return ret;
1455
Chris Wilson71a77e02011-02-02 12:13:49 +00001456 cmd = MI_FLUSH_DW;
1457 if (invalidate & I915_GEM_DOMAIN_RENDER)
1458 cmd |= MI_INVALIDATE_TLB;
1459 intel_ring_emit(ring, cmd);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001460 intel_ring_emit(ring, 0);
1461 intel_ring_emit(ring, 0);
Chris Wilson71a77e02011-02-02 12:13:49 +00001462 intel_ring_emit(ring, MI_NOOP);
Chris Wilsonb72f3ac2011-01-04 17:34:02 +00001463 intel_ring_advance(ring);
1464 return 0;
Zou Nan hai8d192152010-11-02 16:31:01 +08001465}
1466
Chris Wilson549f7362010-10-19 11:19:32 +01001467static const struct intel_ring_buffer gen6_blt_ring = {
Akshay Joshi0206e352011-08-16 15:34:10 -04001468 .name = "blt ring",
Daniel Vetter96154f22011-12-14 13:57:00 +01001469 .id = BCS,
Akshay Joshi0206e352011-08-16 15:34:10 -04001470 .mmio_base = BLT_RING_BASE,
1471 .size = 32 * PAGE_SIZE,
Daniel Vetter6a233c72011-12-14 13:57:07 +01001472 .init = init_ring_common,
Akshay Joshi0206e352011-08-16 15:34:10 -04001473 .write_tail = ring_write_tail,
1474 .flush = blt_ring_flush,
1475 .add_request = gen6_add_request,
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001476 .get_seqno = gen6_ring_get_seqno,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001477 .irq_get = blt_ring_get_irq,
1478 .irq_put = blt_ring_put_irq,
Akshay Joshi0206e352011-08-16 15:34:10 -04001479 .dispatch_execbuffer = gen6_ring_dispatch_execbuffer,
Ben Widawskyc8c99b02011-09-14 20:32:47 -07001480 .sync_to = gen6_blt_ring_sync_to,
1481 .semaphore_register = {MI_SEMAPHORE_SYNC_BR,
1482 MI_SEMAPHORE_SYNC_BV,
1483 MI_SEMAPHORE_SYNC_INVALID},
1484 .signal_mbox = {GEN6_RBSYNC, GEN6_VBSYNC},
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001485};
1486
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001487int intel_init_render_ring_buffer(struct drm_device *dev)
1488{
1489 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001490 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001491
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001492 *ring = render_ring;
1493 if (INTEL_INFO(dev)->gen >= 6) {
1494 ring->add_request = gen6_add_request;
Jesse Barnes8d315282011-10-16 10:23:31 +02001495 ring->flush = gen6_render_ring_flush;
Chris Wilson0f468322011-01-04 17:35:21 +00001496 ring->irq_get = gen6_render_ring_get_irq;
1497 ring->irq_put = gen6_render_ring_put_irq;
Daniel Vetter4cd53c02012-12-14 16:01:25 +01001498 ring->get_seqno = gen6_ring_get_seqno;
Chris Wilsonc6df5412010-12-15 09:56:50 +00001499 } else if (IS_GEN5(dev)) {
1500 ring->add_request = pc_render_add_request;
1501 ring->get_seqno = pc_render_get_seqno;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001502 }
1503
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001504 if (!I915_NEED_GFX_HWS(dev)) {
1505 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1506 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
1507 }
1508
1509 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001510}
1511
Chris Wilsone8616b62011-01-20 09:57:11 +00001512int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
1513{
1514 drm_i915_private_t *dev_priv = dev->dev_private;
1515 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
1516
1517 *ring = render_ring;
1518 if (INTEL_INFO(dev)->gen >= 6) {
1519 ring->add_request = gen6_add_request;
1520 ring->irq_get = gen6_render_ring_get_irq;
1521 ring->irq_put = gen6_render_ring_put_irq;
1522 } else if (IS_GEN5(dev)) {
1523 ring->add_request = pc_render_add_request;
1524 ring->get_seqno = pc_render_get_seqno;
1525 }
1526
Keith Packardf3234702011-07-22 10:44:39 -07001527 if (!I915_NEED_GFX_HWS(dev))
1528 ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
1529
Chris Wilsone8616b62011-01-20 09:57:11 +00001530 ring->dev = dev;
1531 INIT_LIST_HEAD(&ring->active_list);
1532 INIT_LIST_HEAD(&ring->request_list);
1533 INIT_LIST_HEAD(&ring->gpu_write_list);
1534
1535 ring->size = size;
1536 ring->effective_size = ring->size;
1537 if (IS_I830(ring->dev))
1538 ring->effective_size -= 128;
1539
1540 ring->map.offset = start;
1541 ring->map.size = size;
1542 ring->map.type = 0;
1543 ring->map.flags = 0;
1544 ring->map.mtrr = 0;
1545
1546 drm_core_ioremap_wc(&ring->map, dev);
1547 if (ring->map.handle == NULL) {
1548 DRM_ERROR("can not ioremap virtual address for"
1549 " ring buffer\n");
1550 return -ENOMEM;
1551 }
1552
1553 ring->virtual_start = (void __force __iomem *)ring->map.handle;
1554 return 0;
1555}
1556
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001557int intel_init_bsd_ring_buffer(struct drm_device *dev)
1558{
1559 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001560 struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001561
Jesse Barnes65d3eb12011-04-06 14:54:44 -07001562 if (IS_GEN6(dev) || IS_GEN7(dev))
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001563 *ring = gen6_bsd_ring;
Xiang, Haihao881f47b2010-09-19 14:40:43 +01001564 else
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001565 *ring = bsd_ring;
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001566
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001567 return intel_init_ring_buffer(dev, ring);
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08001568}
Chris Wilson549f7362010-10-19 11:19:32 +01001569
1570int intel_init_blt_ring_buffer(struct drm_device *dev)
1571{
1572 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001573 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Chris Wilson549f7362010-10-19 11:19:32 +01001574
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001575 *ring = gen6_blt_ring;
Chris Wilson549f7362010-10-19 11:19:32 +01001576
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001577 return intel_init_ring_buffer(dev, ring);
Chris Wilson549f7362010-10-19 11:19:32 +01001578}