Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 32 | #include "i915_drv.h" |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 33 | #include "i915_drm.h" |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 34 | #include "i915_trace.h" |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 35 | #include "intel_drv.h" |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 36 | |
Chris Wilson | 6f392d5 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 37 | static u32 i915_gem_get_seqno(struct drm_device *dev) |
| 38 | { |
| 39 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 40 | u32 seqno; |
| 41 | |
| 42 | seqno = dev_priv->next_seqno; |
| 43 | |
| 44 | /* reserve 0 for non-seqno */ |
| 45 | if (++dev_priv->next_seqno == 0) |
| 46 | dev_priv->next_seqno = 1; |
| 47 | |
| 48 | return seqno; |
| 49 | } |
| 50 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 51 | static void |
| 52 | render_ring_flush(struct drm_device *dev, |
| 53 | struct intel_ring_buffer *ring, |
| 54 | u32 invalidate_domains, |
| 55 | u32 flush_domains) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 56 | { |
Chris Wilson | 6f392d5 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 57 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 58 | u32 cmd; |
| 59 | |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 60 | #if WATCH_EXEC |
| 61 | DRM_INFO("%s: invalidate %08x flush %08x\n", __func__, |
| 62 | invalidate_domains, flush_domains); |
| 63 | #endif |
Chris Wilson | 6f392d5 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 64 | |
| 65 | trace_i915_gem_request_flush(dev, dev_priv->next_seqno, |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 66 | invalidate_domains, flush_domains); |
| 67 | |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 68 | if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) { |
| 69 | /* |
| 70 | * read/write caches: |
| 71 | * |
| 72 | * I915_GEM_DOMAIN_RENDER is always invalidated, but is |
| 73 | * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is |
| 74 | * also flushed at 2d versus 3d pipeline switches. |
| 75 | * |
| 76 | * read-only caches: |
| 77 | * |
| 78 | * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if |
| 79 | * MI_READ_FLUSH is set, and is always flushed on 965. |
| 80 | * |
| 81 | * I915_GEM_DOMAIN_COMMAND may not exist? |
| 82 | * |
| 83 | * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is |
| 84 | * invalidated when MI_EXE_FLUSH is set. |
| 85 | * |
| 86 | * I915_GEM_DOMAIN_VERTEX, which exists on 965, is |
| 87 | * invalidated with every MI_FLUSH. |
| 88 | * |
| 89 | * TLBs: |
| 90 | * |
| 91 | * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND |
| 92 | * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and |
| 93 | * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER |
| 94 | * are flushed at any MI_FLUSH. |
| 95 | */ |
| 96 | |
| 97 | cmd = MI_FLUSH | MI_NO_WRITE_FLUSH; |
| 98 | if ((invalidate_domains|flush_domains) & |
| 99 | I915_GEM_DOMAIN_RENDER) |
| 100 | cmd &= ~MI_NO_WRITE_FLUSH; |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 101 | if (INTEL_INFO(dev)->gen < 4) { |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 102 | /* |
| 103 | * On the 965, the sampler cache always gets flushed |
| 104 | * and this bit is reserved. |
| 105 | */ |
| 106 | if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER) |
| 107 | cmd |= MI_READ_FLUSH; |
| 108 | } |
| 109 | if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION) |
| 110 | cmd |= MI_EXE_FLUSH; |
| 111 | |
| 112 | #if WATCH_EXEC |
| 113 | DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd); |
| 114 | #endif |
Zou Nan hai | be26a10 | 2010-06-12 17:40:24 +0800 | [diff] [blame] | 115 | intel_ring_begin(dev, ring, 2); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 116 | intel_ring_emit(dev, ring, cmd); |
| 117 | intel_ring_emit(dev, ring, MI_NOOP); |
| 118 | intel_ring_advance(dev, ring); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static unsigned int render_ring_get_head(struct drm_device *dev, |
| 123 | struct intel_ring_buffer *ring) |
| 124 | { |
| 125 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 126 | return I915_READ(PRB0_HEAD) & HEAD_ADDR; |
| 127 | } |
| 128 | |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 129 | static void ring_set_tail(struct drm_device *dev, |
| 130 | struct intel_ring_buffer *ring, |
| 131 | u32 value) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 132 | { |
| 133 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 134 | I915_WRITE_TAIL(ring, ring->tail); |
Xiang, Haihao | d46eefa | 2010-09-16 10:43:12 +0800 | [diff] [blame] | 135 | } |
| 136 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 137 | static unsigned int render_ring_get_active_head(struct drm_device *dev, |
| 138 | struct intel_ring_buffer *ring) |
| 139 | { |
| 140 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 141 | u32 acthd_reg = INTEL_INFO(dev)->gen ? ACTHD_I965 : ACTHD; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 142 | |
| 143 | return I915_READ(acthd_reg); |
| 144 | } |
| 145 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 146 | static int init_ring_common(struct drm_device *dev, |
| 147 | struct intel_ring_buffer *ring) |
| 148 | { |
| 149 | u32 head; |
| 150 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 151 | struct drm_i915_gem_object *obj_priv; |
| 152 | obj_priv = to_intel_bo(ring->gem_object); |
| 153 | |
| 154 | /* Stop the ring if it's running. */ |
| 155 | I915_WRITE(ring->regs.ctl, 0); |
| 156 | I915_WRITE(ring->regs.head, 0); |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 157 | ring->set_tail(dev, ring, 0); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 158 | |
| 159 | /* Initialize the ring. */ |
Daniel Vetter | 6c0e1c5 | 2010-08-02 16:33:33 +0200 | [diff] [blame^] | 160 | I915_WRITE_START(ring, obj_priv->gtt_offset); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 161 | head = ring->get_head(dev, ring); |
| 162 | |
| 163 | /* G45 ring initialization fails to reset head to zero */ |
| 164 | if (head != 0) { |
| 165 | DRM_ERROR("%s head not reset to zero " |
| 166 | "ctl %08x head %08x tail %08x start %08x\n", |
| 167 | ring->name, |
| 168 | I915_READ(ring->regs.ctl), |
| 169 | I915_READ(ring->regs.head), |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 170 | I915_READ_TAIL(ring), |
Daniel Vetter | 6c0e1c5 | 2010-08-02 16:33:33 +0200 | [diff] [blame^] | 171 | I915_READ_START(ring)); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 172 | |
| 173 | I915_WRITE(ring->regs.head, 0); |
| 174 | |
| 175 | DRM_ERROR("%s head forced to zero " |
| 176 | "ctl %08x head %08x tail %08x start %08x\n", |
| 177 | ring->name, |
| 178 | I915_READ(ring->regs.ctl), |
| 179 | I915_READ(ring->regs.head), |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 180 | I915_READ_TAIL(ring), |
Daniel Vetter | 6c0e1c5 | 2010-08-02 16:33:33 +0200 | [diff] [blame^] | 181 | I915_READ_START(ring)); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 184 | I915_WRITE(ring->regs.ctl, |
| 185 | ((ring->gem_object->size - PAGE_SIZE) & RING_NR_PAGES) |
| 186 | | RING_NO_REPORT | RING_VALID); |
| 187 | |
| 188 | head = I915_READ(ring->regs.head) & HEAD_ADDR; |
| 189 | /* If the head is still not zero, the ring is dead */ |
| 190 | if (head != 0) { |
| 191 | DRM_ERROR("%s initialization failed " |
| 192 | "ctl %08x head %08x tail %08x start %08x\n", |
| 193 | ring->name, |
| 194 | I915_READ(ring->regs.ctl), |
| 195 | I915_READ(ring->regs.head), |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 196 | I915_READ_TAIL(ring), |
Daniel Vetter | 6c0e1c5 | 2010-08-02 16:33:33 +0200 | [diff] [blame^] | 197 | I915_READ_START(ring)); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 198 | return -EIO; |
| 199 | } |
| 200 | |
| 201 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 202 | i915_kernel_lost_context(dev); |
| 203 | else { |
| 204 | ring->head = ring->get_head(dev, ring); |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 205 | ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 206 | ring->space = ring->head - (ring->tail + 8); |
| 207 | if (ring->space < 0) |
| 208 | ring->space += ring->size; |
| 209 | } |
| 210 | return 0; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 211 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 212 | |
| 213 | static int init_render_ring(struct drm_device *dev, |
| 214 | struct intel_ring_buffer *ring) |
| 215 | { |
| 216 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 217 | int ret = init_ring_common(dev, ring); |
Zhenyu Wang | a69ffdb | 2010-08-30 16:12:42 +0800 | [diff] [blame] | 218 | int mode; |
| 219 | |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 220 | if (INTEL_INFO(dev)->gen > 3) { |
Zhenyu Wang | a69ffdb | 2010-08-30 16:12:42 +0800 | [diff] [blame] | 221 | mode = VS_TIMER_DISPATCH << 16 | VS_TIMER_DISPATCH; |
| 222 | if (IS_GEN6(dev)) |
| 223 | mode |= MI_FLUSH_ENABLE << 16 | MI_FLUSH_ENABLE; |
| 224 | I915_WRITE(MI_MODE, mode); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 225 | } |
| 226 | return ret; |
| 227 | } |
| 228 | |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 229 | #define PIPE_CONTROL_FLUSH(addr) \ |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 230 | do { \ |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 231 | OUT_RING(GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | \ |
Zhenyu Wang | ca76482 | 2010-05-27 10:26:42 +0800 | [diff] [blame] | 232 | PIPE_CONTROL_DEPTH_STALL | 2); \ |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 233 | OUT_RING(addr | PIPE_CONTROL_GLOBAL_GTT); \ |
| 234 | OUT_RING(0); \ |
| 235 | OUT_RING(0); \ |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 236 | } while (0) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * Creates a new sequence number, emitting a write of it to the status page |
| 240 | * plus an interrupt, which will trigger i915_user_interrupt_handler. |
| 241 | * |
| 242 | * Must be called with struct_lock held. |
| 243 | * |
| 244 | * Returned sequence numbers are nonzero on success. |
| 245 | */ |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 246 | static u32 |
| 247 | render_ring_add_request(struct drm_device *dev, |
| 248 | struct intel_ring_buffer *ring, |
| 249 | struct drm_file *file_priv, |
| 250 | u32 flush_domains) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 251 | { |
| 252 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 6f392d5 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 253 | u32 seqno; |
| 254 | |
| 255 | seqno = i915_gem_get_seqno(dev); |
Zhenyu Wang | ca76482 | 2010-05-27 10:26:42 +0800 | [diff] [blame] | 256 | |
| 257 | if (IS_GEN6(dev)) { |
| 258 | BEGIN_LP_RING(6); |
| 259 | OUT_RING(GFX_OP_PIPE_CONTROL | 3); |
| 260 | OUT_RING(PIPE_CONTROL_QW_WRITE | |
| 261 | PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_IS_FLUSH | |
| 262 | PIPE_CONTROL_NOTIFY); |
| 263 | OUT_RING(dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT); |
| 264 | OUT_RING(seqno); |
| 265 | OUT_RING(0); |
| 266 | OUT_RING(0); |
| 267 | ADVANCE_LP_RING(); |
| 268 | } else if (HAS_PIPE_CONTROL(dev)) { |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 269 | u32 scratch_addr = dev_priv->seqno_gfx_addr + 128; |
| 270 | |
| 271 | /* |
| 272 | * Workaround qword write incoherence by flushing the |
| 273 | * PIPE_NOTIFY buffers out to memory before requesting |
| 274 | * an interrupt. |
| 275 | */ |
| 276 | BEGIN_LP_RING(32); |
| 277 | OUT_RING(GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | |
| 278 | PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH); |
| 279 | OUT_RING(dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT); |
| 280 | OUT_RING(seqno); |
| 281 | OUT_RING(0); |
| 282 | PIPE_CONTROL_FLUSH(scratch_addr); |
| 283 | scratch_addr += 128; /* write to separate cachelines */ |
| 284 | PIPE_CONTROL_FLUSH(scratch_addr); |
| 285 | scratch_addr += 128; |
| 286 | PIPE_CONTROL_FLUSH(scratch_addr); |
| 287 | scratch_addr += 128; |
| 288 | PIPE_CONTROL_FLUSH(scratch_addr); |
| 289 | scratch_addr += 128; |
| 290 | PIPE_CONTROL_FLUSH(scratch_addr); |
| 291 | scratch_addr += 128; |
| 292 | PIPE_CONTROL_FLUSH(scratch_addr); |
| 293 | OUT_RING(GFX_OP_PIPE_CONTROL | PIPE_CONTROL_QW_WRITE | |
| 294 | PIPE_CONTROL_WC_FLUSH | PIPE_CONTROL_TC_FLUSH | |
| 295 | PIPE_CONTROL_NOTIFY); |
| 296 | OUT_RING(dev_priv->seqno_gfx_addr | PIPE_CONTROL_GLOBAL_GTT); |
| 297 | OUT_RING(seqno); |
| 298 | OUT_RING(0); |
| 299 | ADVANCE_LP_RING(); |
| 300 | } else { |
| 301 | BEGIN_LP_RING(4); |
| 302 | OUT_RING(MI_STORE_DWORD_INDEX); |
| 303 | OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT); |
| 304 | OUT_RING(seqno); |
| 305 | |
| 306 | OUT_RING(MI_USER_INTERRUPT); |
| 307 | ADVANCE_LP_RING(); |
| 308 | } |
| 309 | return seqno; |
| 310 | } |
| 311 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 312 | static u32 |
| 313 | render_ring_get_gem_seqno(struct drm_device *dev, |
| 314 | struct intel_ring_buffer *ring) |
| 315 | { |
| 316 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
| 317 | if (HAS_PIPE_CONTROL(dev)) |
| 318 | return ((volatile u32 *)(dev_priv->seqno_page))[0]; |
| 319 | else |
| 320 | return intel_read_status_page(ring, I915_GEM_HWS_INDEX); |
| 321 | } |
| 322 | |
| 323 | static void |
| 324 | render_ring_get_user_irq(struct drm_device *dev, |
| 325 | struct intel_ring_buffer *ring) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 326 | { |
| 327 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
| 328 | unsigned long irqflags; |
| 329 | |
| 330 | spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 331 | if (dev->irq_enabled && (++ring->user_irq_refcount == 1)) { |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 332 | if (HAS_PCH_SPLIT(dev)) |
| 333 | ironlake_enable_graphics_irq(dev_priv, GT_PIPE_NOTIFY); |
| 334 | else |
| 335 | i915_enable_irq(dev_priv, I915_USER_INTERRUPT); |
| 336 | } |
| 337 | spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags); |
| 338 | } |
| 339 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 340 | static void |
| 341 | render_ring_put_user_irq(struct drm_device *dev, |
| 342 | struct intel_ring_buffer *ring) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 343 | { |
| 344 | drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; |
| 345 | unsigned long irqflags; |
| 346 | |
| 347 | spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 348 | BUG_ON(dev->irq_enabled && ring->user_irq_refcount <= 0); |
| 349 | if (dev->irq_enabled && (--ring->user_irq_refcount == 0)) { |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 350 | if (HAS_PCH_SPLIT(dev)) |
| 351 | ironlake_disable_graphics_irq(dev_priv, GT_PIPE_NOTIFY); |
| 352 | else |
| 353 | i915_disable_irq(dev_priv, I915_USER_INTERRUPT); |
| 354 | } |
| 355 | spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags); |
| 356 | } |
| 357 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 358 | static void render_setup_status_page(struct drm_device *dev, |
| 359 | struct intel_ring_buffer *ring) |
| 360 | { |
| 361 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 362 | if (IS_GEN6(dev)) { |
| 363 | I915_WRITE(HWS_PGA_GEN6, ring->status_page.gfx_addr); |
| 364 | I915_READ(HWS_PGA_GEN6); /* posting read */ |
| 365 | } else { |
| 366 | I915_WRITE(HWS_PGA, ring->status_page.gfx_addr); |
| 367 | I915_READ(HWS_PGA); /* posting read */ |
| 368 | } |
| 369 | |
| 370 | } |
| 371 | |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 372 | void |
| 373 | bsd_ring_flush(struct drm_device *dev, |
| 374 | struct intel_ring_buffer *ring, |
| 375 | u32 invalidate_domains, |
| 376 | u32 flush_domains) |
| 377 | { |
Zou Nan hai | be26a10 | 2010-06-12 17:40:24 +0800 | [diff] [blame] | 378 | intel_ring_begin(dev, ring, 2); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 379 | intel_ring_emit(dev, ring, MI_FLUSH); |
| 380 | intel_ring_emit(dev, ring, MI_NOOP); |
| 381 | intel_ring_advance(dev, ring); |
| 382 | } |
| 383 | |
| 384 | static inline unsigned int bsd_ring_get_head(struct drm_device *dev, |
| 385 | struct intel_ring_buffer *ring) |
| 386 | { |
| 387 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 388 | return I915_READ(BSD_RING_HEAD) & HEAD_ADDR; |
| 389 | } |
| 390 | |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 391 | static inline unsigned int bsd_ring_get_active_head(struct drm_device *dev, |
| 392 | struct intel_ring_buffer *ring) |
| 393 | { |
| 394 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 395 | return I915_READ(BSD_RING_ACTHD); |
| 396 | } |
| 397 | |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 398 | static int init_bsd_ring(struct drm_device *dev, |
| 399 | struct intel_ring_buffer *ring) |
| 400 | { |
| 401 | return init_ring_common(dev, ring); |
| 402 | } |
| 403 | |
| 404 | static u32 |
| 405 | bsd_ring_add_request(struct drm_device *dev, |
| 406 | struct intel_ring_buffer *ring, |
| 407 | struct drm_file *file_priv, |
| 408 | u32 flush_domains) |
| 409 | { |
| 410 | u32 seqno; |
Chris Wilson | 6f392d5 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 411 | |
| 412 | seqno = i915_gem_get_seqno(dev); |
| 413 | |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 414 | intel_ring_begin(dev, ring, 4); |
| 415 | intel_ring_emit(dev, ring, MI_STORE_DWORD_INDEX); |
| 416 | intel_ring_emit(dev, ring, |
| 417 | I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT); |
| 418 | intel_ring_emit(dev, ring, seqno); |
| 419 | intel_ring_emit(dev, ring, MI_USER_INTERRUPT); |
| 420 | intel_ring_advance(dev, ring); |
| 421 | |
| 422 | DRM_DEBUG_DRIVER("%s %d\n", ring->name, seqno); |
| 423 | |
| 424 | return seqno; |
| 425 | } |
| 426 | |
| 427 | static void bsd_setup_status_page(struct drm_device *dev, |
| 428 | struct intel_ring_buffer *ring) |
| 429 | { |
| 430 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 431 | I915_WRITE(BSD_HWS_PGA, ring->status_page.gfx_addr); |
| 432 | I915_READ(BSD_HWS_PGA); |
| 433 | } |
| 434 | |
| 435 | static void |
| 436 | bsd_ring_get_user_irq(struct drm_device *dev, |
| 437 | struct intel_ring_buffer *ring) |
| 438 | { |
| 439 | /* do nothing */ |
| 440 | } |
| 441 | static void |
| 442 | bsd_ring_put_user_irq(struct drm_device *dev, |
| 443 | struct intel_ring_buffer *ring) |
| 444 | { |
| 445 | /* do nothing */ |
| 446 | } |
| 447 | |
| 448 | static u32 |
| 449 | bsd_ring_get_gem_seqno(struct drm_device *dev, |
| 450 | struct intel_ring_buffer *ring) |
| 451 | { |
| 452 | return intel_read_status_page(ring, I915_GEM_HWS_INDEX); |
| 453 | } |
| 454 | |
| 455 | static int |
| 456 | bsd_ring_dispatch_gem_execbuffer(struct drm_device *dev, |
| 457 | struct intel_ring_buffer *ring, |
| 458 | struct drm_i915_gem_execbuffer2 *exec, |
| 459 | struct drm_clip_rect *cliprects, |
| 460 | uint64_t exec_offset) |
| 461 | { |
| 462 | uint32_t exec_start; |
| 463 | exec_start = (uint32_t) exec_offset + exec->batch_start_offset; |
| 464 | intel_ring_begin(dev, ring, 2); |
| 465 | intel_ring_emit(dev, ring, MI_BATCH_BUFFER_START | |
| 466 | (2 << 6) | MI_BATCH_NON_SECURE_I965); |
| 467 | intel_ring_emit(dev, ring, exec_start); |
| 468 | intel_ring_advance(dev, ring); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 473 | static int |
| 474 | render_ring_dispatch_gem_execbuffer(struct drm_device *dev, |
| 475 | struct intel_ring_buffer *ring, |
| 476 | struct drm_i915_gem_execbuffer2 *exec, |
| 477 | struct drm_clip_rect *cliprects, |
| 478 | uint64_t exec_offset) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 479 | { |
| 480 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 481 | int nbox = exec->num_cliprects; |
| 482 | int i = 0, count; |
| 483 | uint32_t exec_start, exec_len; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 484 | exec_start = (uint32_t) exec_offset + exec->batch_start_offset; |
| 485 | exec_len = (uint32_t) exec->batch_len; |
| 486 | |
Chris Wilson | 6f392d5 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 487 | trace_i915_gem_request_submit(dev, dev_priv->next_seqno + 1); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 488 | |
| 489 | count = nbox ? nbox : 1; |
| 490 | |
| 491 | for (i = 0; i < count; i++) { |
| 492 | if (i < nbox) { |
| 493 | int ret = i915_emit_box(dev, cliprects, i, |
| 494 | exec->DR1, exec->DR4); |
| 495 | if (ret) |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | if (IS_I830(dev) || IS_845G(dev)) { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 500 | intel_ring_begin(dev, ring, 4); |
| 501 | intel_ring_emit(dev, ring, MI_BATCH_BUFFER); |
| 502 | intel_ring_emit(dev, ring, |
| 503 | exec_start | MI_BATCH_NON_SECURE); |
| 504 | intel_ring_emit(dev, ring, exec_start + exec_len - 4); |
| 505 | intel_ring_emit(dev, ring, 0); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 506 | } else { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 507 | intel_ring_begin(dev, ring, 4); |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 508 | if (INTEL_INFO(dev)->gen >= 4) { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 509 | intel_ring_emit(dev, ring, |
| 510 | MI_BATCH_BUFFER_START | (2 << 6) |
| 511 | | MI_BATCH_NON_SECURE_I965); |
| 512 | intel_ring_emit(dev, ring, exec_start); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 513 | } else { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 514 | intel_ring_emit(dev, ring, MI_BATCH_BUFFER_START |
| 515 | | (2 << 6)); |
| 516 | intel_ring_emit(dev, ring, exec_start | |
| 517 | MI_BATCH_NON_SECURE); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 518 | } |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 519 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 520 | intel_ring_advance(dev, ring); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Zou Nan hai | 1cafd34 | 2010-06-25 13:40:24 +0800 | [diff] [blame] | 523 | if (IS_G4X(dev) || IS_IRONLAKE(dev)) { |
| 524 | intel_ring_begin(dev, ring, 2); |
| 525 | intel_ring_emit(dev, ring, MI_FLUSH | |
| 526 | MI_NO_WRITE_FLUSH | |
| 527 | MI_INVALIDATE_ISP ); |
| 528 | intel_ring_emit(dev, ring, MI_NOOP); |
| 529 | intel_ring_advance(dev, ring); |
| 530 | } |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 531 | /* XXX breadcrumb */ |
Zou Nan hai | 1cafd34 | 2010-06-25 13:40:24 +0800 | [diff] [blame] | 532 | |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 533 | return 0; |
| 534 | } |
| 535 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 536 | static void cleanup_status_page(struct drm_device *dev, |
| 537 | struct intel_ring_buffer *ring) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 538 | { |
| 539 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 540 | struct drm_gem_object *obj; |
| 541 | struct drm_i915_gem_object *obj_priv; |
| 542 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 543 | obj = ring->status_page.obj; |
| 544 | if (obj == NULL) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 545 | return; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 546 | obj_priv = to_intel_bo(obj); |
| 547 | |
| 548 | kunmap(obj_priv->pages[0]); |
| 549 | i915_gem_object_unpin(obj); |
| 550 | drm_gem_object_unreference(obj); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 551 | ring->status_page.obj = NULL; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 552 | |
| 553 | memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map)); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 556 | static int init_status_page(struct drm_device *dev, |
| 557 | struct intel_ring_buffer *ring) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 558 | { |
| 559 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 560 | struct drm_gem_object *obj; |
| 561 | struct drm_i915_gem_object *obj_priv; |
| 562 | int ret; |
| 563 | |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 564 | obj = i915_gem_alloc_object(dev, 4096); |
| 565 | if (obj == NULL) { |
| 566 | DRM_ERROR("Failed to allocate status page\n"); |
| 567 | ret = -ENOMEM; |
| 568 | goto err; |
| 569 | } |
| 570 | obj_priv = to_intel_bo(obj); |
| 571 | obj_priv->agp_type = AGP_USER_CACHED_MEMORY; |
| 572 | |
| 573 | ret = i915_gem_object_pin(obj, 4096); |
| 574 | if (ret != 0) { |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 575 | goto err_unref; |
| 576 | } |
| 577 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 578 | ring->status_page.gfx_addr = obj_priv->gtt_offset; |
| 579 | ring->status_page.page_addr = kmap(obj_priv->pages[0]); |
| 580 | if (ring->status_page.page_addr == NULL) { |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 581 | memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map)); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 582 | goto err_unpin; |
| 583 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 584 | ring->status_page.obj = obj; |
| 585 | memset(ring->status_page.page_addr, 0, PAGE_SIZE); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 586 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 587 | ring->setup_status_page(dev, ring); |
| 588 | DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n", |
| 589 | ring->name, ring->status_page.gfx_addr); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 590 | |
| 591 | return 0; |
| 592 | |
| 593 | err_unpin: |
| 594 | i915_gem_object_unpin(obj); |
| 595 | err_unref: |
| 596 | drm_gem_object_unreference(obj); |
| 597 | err: |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 598 | return ret; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 601 | |
| 602 | int intel_init_ring_buffer(struct drm_device *dev, |
| 603 | struct intel_ring_buffer *ring) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 604 | { |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 605 | struct drm_i915_private *dev_priv = dev->dev_private; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 606 | struct drm_i915_gem_object *obj_priv; |
| 607 | struct drm_gem_object *obj; |
Chris Wilson | dd785e3 | 2010-08-07 11:01:34 +0100 | [diff] [blame] | 608 | int ret; |
| 609 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 610 | ring->dev = dev; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 611 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 612 | if (I915_NEED_GFX_HWS(dev)) { |
| 613 | ret = init_status_page(dev, ring); |
| 614 | if (ret) |
| 615 | return ret; |
| 616 | } |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 617 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 618 | obj = i915_gem_alloc_object(dev, ring->size); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 619 | if (obj == NULL) { |
| 620 | DRM_ERROR("Failed to allocate ringbuffer\n"); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 621 | ret = -ENOMEM; |
Chris Wilson | dd785e3 | 2010-08-07 11:01:34 +0100 | [diff] [blame] | 622 | goto err_hws; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 623 | } |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 624 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 625 | ring->gem_object = obj; |
| 626 | |
| 627 | ret = i915_gem_object_pin(obj, ring->alignment); |
Chris Wilson | dd785e3 | 2010-08-07 11:01:34 +0100 | [diff] [blame] | 628 | if (ret) |
| 629 | goto err_unref; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 630 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 631 | obj_priv = to_intel_bo(obj); |
| 632 | ring->map.size = ring->size; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 633 | ring->map.offset = dev->agp->base + obj_priv->gtt_offset; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 634 | ring->map.type = 0; |
| 635 | ring->map.flags = 0; |
| 636 | ring->map.mtrr = 0; |
| 637 | |
| 638 | drm_core_ioremap_wc(&ring->map, dev); |
| 639 | if (ring->map.handle == NULL) { |
| 640 | DRM_ERROR("Failed to map ringbuffer.\n"); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 641 | ret = -EINVAL; |
Chris Wilson | dd785e3 | 2010-08-07 11:01:34 +0100 | [diff] [blame] | 642 | goto err_unpin; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 643 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 644 | |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 645 | ring->virtual_start = ring->map.handle; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 646 | ret = ring->init(dev, ring); |
Chris Wilson | dd785e3 | 2010-08-07 11:01:34 +0100 | [diff] [blame] | 647 | if (ret) |
| 648 | goto err_unmap; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 649 | |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 650 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 651 | i915_kernel_lost_context(dev); |
| 652 | else { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 653 | ring->head = ring->get_head(dev, ring); |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 654 | ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 655 | ring->space = ring->head - (ring->tail + 8); |
| 656 | if (ring->space < 0) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 657 | ring->space += ring->size; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 658 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 659 | INIT_LIST_HEAD(&ring->active_list); |
| 660 | INIT_LIST_HEAD(&ring->request_list); |
| 661 | return ret; |
Chris Wilson | dd785e3 | 2010-08-07 11:01:34 +0100 | [diff] [blame] | 662 | |
| 663 | err_unmap: |
| 664 | drm_core_ioremapfree(&ring->map, dev); |
| 665 | err_unpin: |
| 666 | i915_gem_object_unpin(obj); |
| 667 | err_unref: |
| 668 | drm_gem_object_unreference(obj); |
| 669 | ring->gem_object = NULL; |
| 670 | err_hws: |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 671 | cleanup_status_page(dev, ring); |
| 672 | return ret; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 675 | void intel_cleanup_ring_buffer(struct drm_device *dev, |
| 676 | struct intel_ring_buffer *ring) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 677 | { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 678 | if (ring->gem_object == NULL) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 679 | return; |
| 680 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 681 | drm_core_ioremapfree(&ring->map, dev); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 682 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 683 | i915_gem_object_unpin(ring->gem_object); |
| 684 | drm_gem_object_unreference(ring->gem_object); |
| 685 | ring->gem_object = NULL; |
| 686 | cleanup_status_page(dev, ring); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 689 | int intel_wrap_ring_buffer(struct drm_device *dev, |
| 690 | struct intel_ring_buffer *ring) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 691 | { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 692 | unsigned int *virt; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 693 | int rem; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 694 | rem = ring->size - ring->tail; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 695 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 696 | if (ring->space < rem) { |
| 697 | int ret = intel_wait_ring_buffer(dev, ring, rem); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 698 | if (ret) |
| 699 | return ret; |
| 700 | } |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 701 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 702 | virt = (unsigned int *)(ring->virtual_start + ring->tail); |
Chris Wilson | 1741dd4 | 2010-08-04 15:18:12 +0100 | [diff] [blame] | 703 | rem /= 8; |
| 704 | while (rem--) { |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 705 | *virt++ = MI_NOOP; |
Chris Wilson | 1741dd4 | 2010-08-04 15:18:12 +0100 | [diff] [blame] | 706 | *virt++ = MI_NOOP; |
| 707 | } |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 708 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 709 | ring->tail = 0; |
Chris Wilson | 43ed340 | 2010-07-01 17:53:00 +0100 | [diff] [blame] | 710 | ring->space = ring->head - 8; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 715 | int intel_wait_ring_buffer(struct drm_device *dev, |
| 716 | struct intel_ring_buffer *ring, int n) |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 717 | { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 718 | unsigned long end; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 719 | |
| 720 | trace_i915_ring_wait_begin (dev); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 721 | end = jiffies + 3 * HZ; |
| 722 | do { |
| 723 | ring->head = ring->get_head(dev, ring); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 724 | ring->space = ring->head - (ring->tail + 8); |
| 725 | if (ring->space < 0) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 726 | ring->space += ring->size; |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 727 | if (ring->space >= n) { |
| 728 | trace_i915_ring_wait_end (dev); |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | if (dev->primary->master) { |
| 733 | struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv; |
| 734 | if (master_priv->sarea_priv) |
| 735 | master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT; |
| 736 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 737 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 738 | yield(); |
| 739 | } while (!time_after(jiffies, end)); |
Eric Anholt | 62fdfea | 2010-05-21 13:26:39 -0700 | [diff] [blame] | 740 | trace_i915_ring_wait_end (dev); |
| 741 | return -EBUSY; |
| 742 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 743 | |
| 744 | void intel_ring_begin(struct drm_device *dev, |
Zou Nan hai | be26a10 | 2010-06-12 17:40:24 +0800 | [diff] [blame] | 745 | struct intel_ring_buffer *ring, int num_dwords) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 746 | { |
Zou Nan hai | be26a10 | 2010-06-12 17:40:24 +0800 | [diff] [blame] | 747 | int n = 4*num_dwords; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 748 | if (unlikely(ring->tail + n > ring->size)) |
| 749 | intel_wrap_ring_buffer(dev, ring); |
| 750 | if (unlikely(ring->space < n)) |
| 751 | intel_wait_ring_buffer(dev, ring, n); |
Chris Wilson | d97ed33 | 2010-08-04 15:18:13 +0100 | [diff] [blame] | 752 | |
| 753 | ring->space -= n; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 754 | } |
| 755 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 756 | void intel_ring_advance(struct drm_device *dev, |
| 757 | struct intel_ring_buffer *ring) |
| 758 | { |
Chris Wilson | d97ed33 | 2010-08-04 15:18:13 +0100 | [diff] [blame] | 759 | ring->tail &= ring->size - 1; |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 760 | ring->set_tail(dev, ring, ring->tail); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | void intel_fill_struct(struct drm_device *dev, |
| 764 | struct intel_ring_buffer *ring, |
| 765 | void *data, |
| 766 | unsigned int len) |
| 767 | { |
| 768 | unsigned int *virt = ring->virtual_start + ring->tail; |
| 769 | BUG_ON((len&~(4-1)) != 0); |
Zou Nan hai | be26a10 | 2010-06-12 17:40:24 +0800 | [diff] [blame] | 770 | intel_ring_begin(dev, ring, len/4); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 771 | memcpy(virt, data, len); |
| 772 | ring->tail += len; |
| 773 | ring->tail &= ring->size - 1; |
| 774 | ring->space -= len; |
| 775 | intel_ring_advance(dev, ring); |
| 776 | } |
| 777 | |
Chris Wilson | e070868 | 2010-09-19 14:46:27 +0100 | [diff] [blame] | 778 | static const struct intel_ring_buffer render_ring = { |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 779 | .name = "render ring", |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 780 | .id = RING_RENDER, |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 781 | .regs = { |
| 782 | .ctl = PRB0_CTL, |
| 783 | .head = PRB0_HEAD, |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 784 | }, |
Daniel Vetter | 333e9fe | 2010-08-02 16:24:01 +0200 | [diff] [blame] | 785 | .mmio_base = RENDER_RING_BASE, |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 786 | .size = 32 * PAGE_SIZE, |
| 787 | .alignment = PAGE_SIZE, |
| 788 | .virtual_start = NULL, |
| 789 | .dev = NULL, |
| 790 | .gem_object = NULL, |
| 791 | .head = 0, |
| 792 | .tail = 0, |
| 793 | .space = 0, |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 794 | .user_irq_refcount = 0, |
| 795 | .irq_gem_seqno = 0, |
| 796 | .waiting_gem_seqno = 0, |
| 797 | .setup_status_page = render_setup_status_page, |
| 798 | .init = init_render_ring, |
| 799 | .get_head = render_ring_get_head, |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 800 | .set_tail = ring_set_tail, |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 801 | .get_active_head = render_ring_get_active_head, |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 802 | .flush = render_ring_flush, |
| 803 | .add_request = render_ring_add_request, |
| 804 | .get_gem_seqno = render_ring_get_gem_seqno, |
| 805 | .user_irq_get = render_ring_get_user_irq, |
| 806 | .user_irq_put = render_ring_put_user_irq, |
| 807 | .dispatch_gem_execbuffer = render_ring_dispatch_gem_execbuffer, |
| 808 | .status_page = {NULL, 0, NULL}, |
| 809 | .map = {0,} |
| 810 | }; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 811 | |
| 812 | /* ring buffer for bit-stream decoder */ |
| 813 | |
Chris Wilson | e070868 | 2010-09-19 14:46:27 +0100 | [diff] [blame] | 814 | static const struct intel_ring_buffer bsd_ring = { |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 815 | .name = "bsd ring", |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 816 | .id = RING_BSD, |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 817 | .regs = { |
| 818 | .ctl = BSD_RING_CTL, |
| 819 | .head = BSD_RING_HEAD, |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 820 | }, |
Daniel Vetter | 333e9fe | 2010-08-02 16:24:01 +0200 | [diff] [blame] | 821 | .mmio_base = BSD_RING_BASE, |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 822 | .size = 32 * PAGE_SIZE, |
| 823 | .alignment = PAGE_SIZE, |
| 824 | .virtual_start = NULL, |
| 825 | .dev = NULL, |
| 826 | .gem_object = NULL, |
| 827 | .head = 0, |
| 828 | .tail = 0, |
| 829 | .space = 0, |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 830 | .user_irq_refcount = 0, |
| 831 | .irq_gem_seqno = 0, |
| 832 | .waiting_gem_seqno = 0, |
| 833 | .setup_status_page = bsd_setup_status_page, |
| 834 | .init = init_bsd_ring, |
| 835 | .get_head = bsd_ring_get_head, |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 836 | .set_tail = ring_set_tail, |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 837 | .get_active_head = bsd_ring_get_active_head, |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 838 | .flush = bsd_ring_flush, |
| 839 | .add_request = bsd_ring_add_request, |
| 840 | .get_gem_seqno = bsd_ring_get_gem_seqno, |
| 841 | .user_irq_get = bsd_ring_get_user_irq, |
| 842 | .user_irq_put = bsd_ring_put_user_irq, |
| 843 | .dispatch_gem_execbuffer = bsd_ring_dispatch_gem_execbuffer, |
| 844 | .status_page = {NULL, 0, NULL}, |
| 845 | .map = {0,} |
| 846 | }; |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 847 | |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 848 | |
| 849 | static void gen6_bsd_setup_status_page(struct drm_device *dev, |
| 850 | struct intel_ring_buffer *ring) |
| 851 | { |
| 852 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 853 | I915_WRITE(GEN6_BSD_HWS_PGA, ring->status_page.gfx_addr); |
| 854 | I915_READ(GEN6_BSD_HWS_PGA); |
| 855 | } |
| 856 | |
| 857 | static inline unsigned int gen6_bsd_ring_get_head(struct drm_device *dev, |
| 858 | struct intel_ring_buffer *ring) |
| 859 | { |
| 860 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 861 | return I915_READ(GEN6_BSD_RING_HEAD) & HEAD_ADDR; |
| 862 | } |
| 863 | |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 864 | static inline void gen6_bsd_ring_set_tail(struct drm_device *dev, |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 865 | struct intel_ring_buffer *ring, |
| 866 | u32 value) |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 867 | { |
| 868 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 869 | |
| 870 | /* Every tail move must follow the sequence below */ |
| 871 | I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL, |
| 872 | GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK | |
| 873 | GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_DISABLE); |
| 874 | I915_WRITE(GEN6_BSD_RNCID, 0x0); |
| 875 | |
| 876 | if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) & |
| 877 | GEN6_BSD_SLEEP_PSMI_CONTROL_IDLE_INDICATOR) == 0, |
| 878 | 50)) |
| 879 | DRM_ERROR("timed out waiting for IDLE Indicator\n"); |
| 880 | |
Daniel Vetter | 870e86d | 2010-08-02 16:29:44 +0200 | [diff] [blame] | 881 | I915_WRITE_TAIL(ring, value); |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 882 | I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL, |
| 883 | GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_MODIFY_MASK | |
| 884 | GEN6_BSD_SLEEP_PSMI_CONTROL_RC_ILDL_MESSAGE_ENABLE); |
| 885 | } |
| 886 | |
| 887 | static inline unsigned int gen6_bsd_ring_get_active_head(struct drm_device *dev, |
| 888 | struct intel_ring_buffer *ring) |
| 889 | { |
| 890 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 891 | return I915_READ(GEN6_BSD_RING_ACTHD); |
| 892 | } |
| 893 | |
| 894 | static void gen6_bsd_ring_flush(struct drm_device *dev, |
| 895 | struct intel_ring_buffer *ring, |
| 896 | u32 invalidate_domains, |
| 897 | u32 flush_domains) |
| 898 | { |
| 899 | intel_ring_begin(dev, ring, 4); |
| 900 | intel_ring_emit(dev, ring, MI_FLUSH_DW); |
| 901 | intel_ring_emit(dev, ring, 0); |
| 902 | intel_ring_emit(dev, ring, 0); |
| 903 | intel_ring_emit(dev, ring, 0); |
| 904 | intel_ring_advance(dev, ring); |
| 905 | } |
| 906 | |
| 907 | static int |
| 908 | gen6_bsd_ring_dispatch_gem_execbuffer(struct drm_device *dev, |
| 909 | struct intel_ring_buffer *ring, |
| 910 | struct drm_i915_gem_execbuffer2 *exec, |
| 911 | struct drm_clip_rect *cliprects, |
| 912 | uint64_t exec_offset) |
| 913 | { |
| 914 | uint32_t exec_start; |
| 915 | exec_start = (uint32_t) exec_offset + exec->batch_start_offset; |
| 916 | intel_ring_begin(dev, ring, 2); |
| 917 | intel_ring_emit(dev, ring, MI_BATCH_BUFFER_START | MI_BATCH_NON_SECURE_I965); /* bit0-7 is the length on GEN6+ */ |
| 918 | intel_ring_emit(dev, ring, exec_start); |
| 919 | intel_ring_advance(dev, ring); |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* ring buffer for Video Codec for Gen6+ */ |
Chris Wilson | e070868 | 2010-09-19 14:46:27 +0100 | [diff] [blame] | 924 | static const struct intel_ring_buffer gen6_bsd_ring = { |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 925 | .name = "gen6 bsd ring", |
| 926 | .id = RING_BSD, |
| 927 | .regs = { |
| 928 | .ctl = GEN6_BSD_RING_CTL, |
| 929 | .head = GEN6_BSD_RING_HEAD, |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 930 | }, |
Daniel Vetter | 333e9fe | 2010-08-02 16:24:01 +0200 | [diff] [blame] | 931 | .mmio_base = GEN6_BSD_RING_BASE, |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 932 | .size = 32 * PAGE_SIZE, |
| 933 | .alignment = PAGE_SIZE, |
| 934 | .virtual_start = NULL, |
| 935 | .dev = NULL, |
| 936 | .gem_object = NULL, |
| 937 | .head = 0, |
| 938 | .tail = 0, |
| 939 | .space = 0, |
| 940 | .user_irq_refcount = 0, |
| 941 | .irq_gem_seqno = 0, |
| 942 | .waiting_gem_seqno = 0, |
| 943 | .setup_status_page = gen6_bsd_setup_status_page, |
| 944 | .init = init_bsd_ring, |
| 945 | .get_head = gen6_bsd_ring_get_head, |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 946 | .set_tail = gen6_bsd_ring_set_tail, |
| 947 | .get_active_head = gen6_bsd_ring_get_active_head, |
| 948 | .flush = gen6_bsd_ring_flush, |
| 949 | .add_request = bsd_ring_add_request, |
| 950 | .get_gem_seqno = bsd_ring_get_gem_seqno, |
| 951 | .user_irq_get = bsd_ring_get_user_irq, |
| 952 | .user_irq_put = bsd_ring_put_user_irq, |
| 953 | .dispatch_gem_execbuffer = gen6_bsd_ring_dispatch_gem_execbuffer, |
| 954 | .status_page = {NULL, 0, NULL}, |
| 955 | .map = {0,} |
| 956 | }; |
| 957 | |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 958 | int intel_init_render_ring_buffer(struct drm_device *dev) |
| 959 | { |
| 960 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 961 | |
| 962 | dev_priv->render_ring = render_ring; |
| 963 | |
| 964 | if (!I915_NEED_GFX_HWS(dev)) { |
| 965 | dev_priv->render_ring.status_page.page_addr |
| 966 | = dev_priv->status_page_dmah->vaddr; |
| 967 | memset(dev_priv->render_ring.status_page.page_addr, |
| 968 | 0, PAGE_SIZE); |
| 969 | } |
| 970 | |
| 971 | return intel_init_ring_buffer(dev, &dev_priv->render_ring); |
| 972 | } |
| 973 | |
| 974 | int intel_init_bsd_ring_buffer(struct drm_device *dev) |
| 975 | { |
| 976 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 977 | |
Xiang, Haihao | 881f47b | 2010-09-19 14:40:43 +0100 | [diff] [blame] | 978 | if (IS_GEN6(dev)) |
| 979 | dev_priv->bsd_ring = gen6_bsd_ring; |
| 980 | else |
| 981 | dev_priv->bsd_ring = bsd_ring; |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 982 | |
| 983 | return intel_init_ring_buffer(dev, &dev_priv->bsd_ring); |
| 984 | } |