1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*- 2c0e09200SDave Airlie */ 3c0e09200SDave Airlie /* 4c0e09200SDave Airlie * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. 5c0e09200SDave Airlie * All Rights Reserved. 6c0e09200SDave Airlie * 7c0e09200SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a 8c0e09200SDave Airlie * copy of this software and associated documentation files (the 9c0e09200SDave Airlie * "Software"), to deal in the Software without restriction, including 10c0e09200SDave Airlie * without limitation the rights to use, copy, modify, merge, publish, 11c0e09200SDave Airlie * distribute, sub license, and/or sell copies of the Software, and to 12c0e09200SDave Airlie * permit persons to whom the Software is furnished to do so, subject to 13c0e09200SDave Airlie * the following conditions: 14c0e09200SDave Airlie * 15c0e09200SDave Airlie * The above copyright notice and this permission notice (including the 16c0e09200SDave Airlie * next paragraph) shall be included in all copies or substantial portions 17c0e09200SDave Airlie * of the Software. 18c0e09200SDave Airlie * 19c0e09200SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20c0e09200SDave Airlie * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21c0e09200SDave Airlie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22c0e09200SDave Airlie * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 23c0e09200SDave Airlie * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24c0e09200SDave Airlie * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25c0e09200SDave Airlie * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26c0e09200SDave Airlie * 27c0e09200SDave Airlie */ 28c0e09200SDave Airlie 29c0e09200SDave Airlie #include "drmP.h" 30c0e09200SDave Airlie #include "drm.h" 31c0e09200SDave Airlie #include "i915_drm.h" 32c0e09200SDave Airlie #include "i915_drv.h" 33c0e09200SDave Airlie 34c0e09200SDave Airlie #define MAX_NOPID ((u32)~0) 35c0e09200SDave Airlie 36ed4cb414SEric Anholt /** These are the interrupts used by the driver */ 37ed4cb414SEric Anholt #define I915_INTERRUPT_ENABLE_MASK (I915_USER_INTERRUPT | \ 38ed4cb414SEric Anholt I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT | \ 39*8ee1c3dbSMatthew Garrett I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT | \ 40*8ee1c3dbSMatthew Garrett I915_ASLE_INTERRUPT | \ 41*8ee1c3dbSMatthew Garrett I915_DISPLAY_PIPE_B_EVENT_INTERRUPT) 42ed4cb414SEric Anholt 43*8ee1c3dbSMatthew Garrett void 44ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask) 45ed4cb414SEric Anholt { 46ed4cb414SEric Anholt if ((dev_priv->irq_mask_reg & mask) != 0) { 47ed4cb414SEric Anholt dev_priv->irq_mask_reg &= ~mask; 48ed4cb414SEric Anholt I915_WRITE(IMR, dev_priv->irq_mask_reg); 49ed4cb414SEric Anholt (void) I915_READ(IMR); 50ed4cb414SEric Anholt } 51ed4cb414SEric Anholt } 52ed4cb414SEric Anholt 53ed4cb414SEric Anholt static inline void 54ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask) 55ed4cb414SEric Anholt { 56ed4cb414SEric Anholt if ((dev_priv->irq_mask_reg & mask) != mask) { 57ed4cb414SEric Anholt dev_priv->irq_mask_reg |= mask; 58ed4cb414SEric Anholt I915_WRITE(IMR, dev_priv->irq_mask_reg); 59ed4cb414SEric Anholt (void) I915_READ(IMR); 60ed4cb414SEric Anholt } 61ed4cb414SEric Anholt } 62ed4cb414SEric Anholt 63c0e09200SDave Airlie /** 64c0e09200SDave Airlie * Emit blits for scheduled buffer swaps. 65c0e09200SDave Airlie * 66c0e09200SDave Airlie * This function will be called with the HW lock held. 67c0e09200SDave Airlie */ 68c0e09200SDave Airlie static void i915_vblank_tasklet(struct drm_device *dev) 69c0e09200SDave Airlie { 70c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 71c0e09200SDave Airlie unsigned long irqflags; 72c0e09200SDave Airlie struct list_head *list, *tmp, hits, *hit; 73c0e09200SDave Airlie int nhits, nrects, slice[2], upper[2], lower[2], i; 74c0e09200SDave Airlie unsigned counter[2] = { atomic_read(&dev->vbl_received), 75c0e09200SDave Airlie atomic_read(&dev->vbl_received2) }; 76c0e09200SDave Airlie struct drm_drawable_info *drw; 77c0e09200SDave Airlie drm_i915_sarea_t *sarea_priv = dev_priv->sarea_priv; 78c0e09200SDave Airlie u32 cpp = dev_priv->cpp; 79c0e09200SDave Airlie u32 cmd = (cpp == 4) ? (XY_SRC_COPY_BLT_CMD | 80c0e09200SDave Airlie XY_SRC_COPY_BLT_WRITE_ALPHA | 81c0e09200SDave Airlie XY_SRC_COPY_BLT_WRITE_RGB) 82c0e09200SDave Airlie : XY_SRC_COPY_BLT_CMD; 83c0e09200SDave Airlie u32 src_pitch = sarea_priv->pitch * cpp; 84c0e09200SDave Airlie u32 dst_pitch = sarea_priv->pitch * cpp; 85c0e09200SDave Airlie u32 ropcpp = (0xcc << 16) | ((cpp - 1) << 24); 86c0e09200SDave Airlie RING_LOCALS; 87c0e09200SDave Airlie 88c0e09200SDave Airlie if (IS_I965G(dev) && sarea_priv->front_tiled) { 89c0e09200SDave Airlie cmd |= XY_SRC_COPY_BLT_DST_TILED; 90c0e09200SDave Airlie dst_pitch >>= 2; 91c0e09200SDave Airlie } 92c0e09200SDave Airlie if (IS_I965G(dev) && sarea_priv->back_tiled) { 93c0e09200SDave Airlie cmd |= XY_SRC_COPY_BLT_SRC_TILED; 94c0e09200SDave Airlie src_pitch >>= 2; 95c0e09200SDave Airlie } 96c0e09200SDave Airlie 97c0e09200SDave Airlie DRM_DEBUG("\n"); 98c0e09200SDave Airlie 99c0e09200SDave Airlie INIT_LIST_HEAD(&hits); 100c0e09200SDave Airlie 101c0e09200SDave Airlie nhits = nrects = 0; 102c0e09200SDave Airlie 103c0e09200SDave Airlie spin_lock_irqsave(&dev_priv->swaps_lock, irqflags); 104c0e09200SDave Airlie 105c0e09200SDave Airlie /* Find buffer swaps scheduled for this vertical blank */ 106c0e09200SDave Airlie list_for_each_safe(list, tmp, &dev_priv->vbl_swaps.head) { 107c0e09200SDave Airlie drm_i915_vbl_swap_t *vbl_swap = 108c0e09200SDave Airlie list_entry(list, drm_i915_vbl_swap_t, head); 109c0e09200SDave Airlie 110c0e09200SDave Airlie if ((counter[vbl_swap->pipe] - vbl_swap->sequence) > (1<<23)) 111c0e09200SDave Airlie continue; 112c0e09200SDave Airlie 113c0e09200SDave Airlie list_del(list); 114c0e09200SDave Airlie dev_priv->swaps_pending--; 115c0e09200SDave Airlie 116c0e09200SDave Airlie spin_unlock(&dev_priv->swaps_lock); 117c0e09200SDave Airlie spin_lock(&dev->drw_lock); 118c0e09200SDave Airlie 119c0e09200SDave Airlie drw = drm_get_drawable_info(dev, vbl_swap->drw_id); 120c0e09200SDave Airlie 121c0e09200SDave Airlie if (!drw) { 122c0e09200SDave Airlie spin_unlock(&dev->drw_lock); 123c0e09200SDave Airlie drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER); 124c0e09200SDave Airlie spin_lock(&dev_priv->swaps_lock); 125c0e09200SDave Airlie continue; 126c0e09200SDave Airlie } 127c0e09200SDave Airlie 128c0e09200SDave Airlie list_for_each(hit, &hits) { 129c0e09200SDave Airlie drm_i915_vbl_swap_t *swap_cmp = 130c0e09200SDave Airlie list_entry(hit, drm_i915_vbl_swap_t, head); 131c0e09200SDave Airlie struct drm_drawable_info *drw_cmp = 132c0e09200SDave Airlie drm_get_drawable_info(dev, swap_cmp->drw_id); 133c0e09200SDave Airlie 134c0e09200SDave Airlie if (drw_cmp && 135c0e09200SDave Airlie drw_cmp->rects[0].y1 > drw->rects[0].y1) { 136c0e09200SDave Airlie list_add_tail(list, hit); 137c0e09200SDave Airlie break; 138c0e09200SDave Airlie } 139c0e09200SDave Airlie } 140c0e09200SDave Airlie 141c0e09200SDave Airlie spin_unlock(&dev->drw_lock); 142c0e09200SDave Airlie 143c0e09200SDave Airlie /* List of hits was empty, or we reached the end of it */ 144c0e09200SDave Airlie if (hit == &hits) 145c0e09200SDave Airlie list_add_tail(list, hits.prev); 146c0e09200SDave Airlie 147c0e09200SDave Airlie nhits++; 148c0e09200SDave Airlie 149c0e09200SDave Airlie spin_lock(&dev_priv->swaps_lock); 150c0e09200SDave Airlie } 151c0e09200SDave Airlie 152c0e09200SDave Airlie if (nhits == 0) { 153c0e09200SDave Airlie spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags); 154c0e09200SDave Airlie return; 155c0e09200SDave Airlie } 156c0e09200SDave Airlie 157c0e09200SDave Airlie spin_unlock(&dev_priv->swaps_lock); 158c0e09200SDave Airlie 159c0e09200SDave Airlie i915_kernel_lost_context(dev); 160c0e09200SDave Airlie 161c0e09200SDave Airlie if (IS_I965G(dev)) { 162c0e09200SDave Airlie BEGIN_LP_RING(4); 163c0e09200SDave Airlie 164c0e09200SDave Airlie OUT_RING(GFX_OP_DRAWRECT_INFO_I965); 165c0e09200SDave Airlie OUT_RING(0); 166c0e09200SDave Airlie OUT_RING(((sarea_priv->width - 1) & 0xffff) | ((sarea_priv->height - 1) << 16)); 167c0e09200SDave Airlie OUT_RING(0); 168c0e09200SDave Airlie ADVANCE_LP_RING(); 169c0e09200SDave Airlie } else { 170c0e09200SDave Airlie BEGIN_LP_RING(6); 171c0e09200SDave Airlie 172c0e09200SDave Airlie OUT_RING(GFX_OP_DRAWRECT_INFO); 173c0e09200SDave Airlie OUT_RING(0); 174c0e09200SDave Airlie OUT_RING(0); 175c0e09200SDave Airlie OUT_RING(sarea_priv->width | sarea_priv->height << 16); 176c0e09200SDave Airlie OUT_RING(sarea_priv->width | sarea_priv->height << 16); 177c0e09200SDave Airlie OUT_RING(0); 178c0e09200SDave Airlie 179c0e09200SDave Airlie ADVANCE_LP_RING(); 180c0e09200SDave Airlie } 181c0e09200SDave Airlie 182c0e09200SDave Airlie sarea_priv->ctxOwner = DRM_KERNEL_CONTEXT; 183c0e09200SDave Airlie 184c0e09200SDave Airlie upper[0] = upper[1] = 0; 185c0e09200SDave Airlie slice[0] = max(sarea_priv->pipeA_h / nhits, 1); 186c0e09200SDave Airlie slice[1] = max(sarea_priv->pipeB_h / nhits, 1); 187c0e09200SDave Airlie lower[0] = sarea_priv->pipeA_y + slice[0]; 188c0e09200SDave Airlie lower[1] = sarea_priv->pipeB_y + slice[0]; 189c0e09200SDave Airlie 190c0e09200SDave Airlie spin_lock(&dev->drw_lock); 191c0e09200SDave Airlie 192c0e09200SDave Airlie /* Emit blits for buffer swaps, partitioning both outputs into as many 193c0e09200SDave Airlie * slices as there are buffer swaps scheduled in order to avoid tearing 194c0e09200SDave Airlie * (based on the assumption that a single buffer swap would always 195c0e09200SDave Airlie * complete before scanout starts). 196c0e09200SDave Airlie */ 197c0e09200SDave Airlie for (i = 0; i++ < nhits; 198c0e09200SDave Airlie upper[0] = lower[0], lower[0] += slice[0], 199c0e09200SDave Airlie upper[1] = lower[1], lower[1] += slice[1]) { 200c0e09200SDave Airlie if (i == nhits) 201c0e09200SDave Airlie lower[0] = lower[1] = sarea_priv->height; 202c0e09200SDave Airlie 203c0e09200SDave Airlie list_for_each(hit, &hits) { 204c0e09200SDave Airlie drm_i915_vbl_swap_t *swap_hit = 205c0e09200SDave Airlie list_entry(hit, drm_i915_vbl_swap_t, head); 206c0e09200SDave Airlie struct drm_clip_rect *rect; 207c0e09200SDave Airlie int num_rects, pipe; 208c0e09200SDave Airlie unsigned short top, bottom; 209c0e09200SDave Airlie 210c0e09200SDave Airlie drw = drm_get_drawable_info(dev, swap_hit->drw_id); 211c0e09200SDave Airlie 212c0e09200SDave Airlie if (!drw) 213c0e09200SDave Airlie continue; 214c0e09200SDave Airlie 215c0e09200SDave Airlie rect = drw->rects; 216c0e09200SDave Airlie pipe = swap_hit->pipe; 217c0e09200SDave Airlie top = upper[pipe]; 218c0e09200SDave Airlie bottom = lower[pipe]; 219c0e09200SDave Airlie 220c0e09200SDave Airlie for (num_rects = drw->num_rects; num_rects--; rect++) { 221c0e09200SDave Airlie int y1 = max(rect->y1, top); 222c0e09200SDave Airlie int y2 = min(rect->y2, bottom); 223c0e09200SDave Airlie 224c0e09200SDave Airlie if (y1 >= y2) 225c0e09200SDave Airlie continue; 226c0e09200SDave Airlie 227c0e09200SDave Airlie BEGIN_LP_RING(8); 228c0e09200SDave Airlie 229c0e09200SDave Airlie OUT_RING(cmd); 230c0e09200SDave Airlie OUT_RING(ropcpp | dst_pitch); 231c0e09200SDave Airlie OUT_RING((y1 << 16) | rect->x1); 232c0e09200SDave Airlie OUT_RING((y2 << 16) | rect->x2); 233c0e09200SDave Airlie OUT_RING(sarea_priv->front_offset); 234c0e09200SDave Airlie OUT_RING((y1 << 16) | rect->x1); 235c0e09200SDave Airlie OUT_RING(src_pitch); 236c0e09200SDave Airlie OUT_RING(sarea_priv->back_offset); 237c0e09200SDave Airlie 238c0e09200SDave Airlie ADVANCE_LP_RING(); 239c0e09200SDave Airlie } 240c0e09200SDave Airlie } 241c0e09200SDave Airlie } 242c0e09200SDave Airlie 243c0e09200SDave Airlie spin_unlock_irqrestore(&dev->drw_lock, irqflags); 244c0e09200SDave Airlie 245c0e09200SDave Airlie list_for_each_safe(hit, tmp, &hits) { 246c0e09200SDave Airlie drm_i915_vbl_swap_t *swap_hit = 247c0e09200SDave Airlie list_entry(hit, drm_i915_vbl_swap_t, head); 248c0e09200SDave Airlie 249c0e09200SDave Airlie list_del(hit); 250c0e09200SDave Airlie 251c0e09200SDave Airlie drm_free(swap_hit, sizeof(*swap_hit), DRM_MEM_DRIVER); 252c0e09200SDave Airlie } 253c0e09200SDave Airlie } 254c0e09200SDave Airlie 255c0e09200SDave Airlie irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS) 256c0e09200SDave Airlie { 257c0e09200SDave Airlie struct drm_device *dev = (struct drm_device *) arg; 258c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 259c0e09200SDave Airlie u32 pipea_stats, pipeb_stats; 260ed4cb414SEric Anholt u32 iir; 261c0e09200SDave Airlie 262585fb111SJesse Barnes pipea_stats = I915_READ(PIPEASTAT); 263585fb111SJesse Barnes pipeb_stats = I915_READ(PIPEBSTAT); 264c0e09200SDave Airlie 265ed4cb414SEric Anholt if (dev->pdev->msi_enabled) 266ed4cb414SEric Anholt I915_WRITE(IMR, ~0); 267ed4cb414SEric Anholt iir = I915_READ(IIR); 268c0e09200SDave Airlie 269ed4cb414SEric Anholt DRM_DEBUG("iir=%08x\n", iir); 270c0e09200SDave Airlie 271ed4cb414SEric Anholt if (iir == 0) { 272ed4cb414SEric Anholt if (dev->pdev->msi_enabled) { 273ed4cb414SEric Anholt I915_WRITE(IMR, dev_priv->irq_mask_reg); 274ed4cb414SEric Anholt (void) I915_READ(IMR); 275ed4cb414SEric Anholt } 276c0e09200SDave Airlie return IRQ_NONE; 277ed4cb414SEric Anholt } 278c0e09200SDave Airlie 279*8ee1c3dbSMatthew Garrett I915_WRITE(PIPEASTAT, pipea_stats); 280*8ee1c3dbSMatthew Garrett I915_WRITE(PIPEBSTAT, pipeb_stats); 281*8ee1c3dbSMatthew Garrett 282ed4cb414SEric Anholt I915_WRITE(IIR, iir); 283ed4cb414SEric Anholt if (dev->pdev->msi_enabled) 284ed4cb414SEric Anholt I915_WRITE(IMR, dev_priv->irq_mask_reg); 285ed4cb414SEric Anholt (void) I915_READ(IIR); /* Flush posted writes */ 286c0e09200SDave Airlie 287c0e09200SDave Airlie dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv); 288c0e09200SDave Airlie 289ed4cb414SEric Anholt if (iir & I915_USER_INTERRUPT) 290c0e09200SDave Airlie DRM_WAKEUP(&dev_priv->irq_queue); 291c0e09200SDave Airlie 292ed4cb414SEric Anholt if (iir & (I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT | 293585fb111SJesse Barnes I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT)) { 294c0e09200SDave Airlie int vblank_pipe = dev_priv->vblank_pipe; 295c0e09200SDave Airlie 296c0e09200SDave Airlie if ((vblank_pipe & 297c0e09200SDave Airlie (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B)) 298c0e09200SDave Airlie == (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B)) { 299ed4cb414SEric Anholt if (iir & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) 300c0e09200SDave Airlie atomic_inc(&dev->vbl_received); 301ed4cb414SEric Anholt if (iir & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) 302c0e09200SDave Airlie atomic_inc(&dev->vbl_received2); 303ed4cb414SEric Anholt } else if (((iir & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) && 304c0e09200SDave Airlie (vblank_pipe & DRM_I915_VBLANK_PIPE_A)) || 305ed4cb414SEric Anholt ((iir & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) && 306c0e09200SDave Airlie (vblank_pipe & DRM_I915_VBLANK_PIPE_B))) 307c0e09200SDave Airlie atomic_inc(&dev->vbl_received); 308c0e09200SDave Airlie 309c0e09200SDave Airlie DRM_WAKEUP(&dev->vbl_queue); 310c0e09200SDave Airlie drm_vbl_send_signals(dev); 311c0e09200SDave Airlie 312c0e09200SDave Airlie if (dev_priv->swaps_pending > 0) 313c0e09200SDave Airlie drm_locked_tasklet(dev, i915_vblank_tasklet); 314c0e09200SDave Airlie } 315c0e09200SDave Airlie 316*8ee1c3dbSMatthew Garrett if (iir & I915_ASLE_INTERRUPT) 317*8ee1c3dbSMatthew Garrett opregion_asle_intr(dev); 318*8ee1c3dbSMatthew Garrett 319*8ee1c3dbSMatthew Garrett if (iir & I915_DISPLAY_PIPE_B_EVENT_INTERRUPT) 320*8ee1c3dbSMatthew Garrett opregion_asle_intr(dev); 321*8ee1c3dbSMatthew Garrett 322c0e09200SDave Airlie return IRQ_HANDLED; 323c0e09200SDave Airlie } 324c0e09200SDave Airlie 325c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev) 326c0e09200SDave Airlie { 327c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 328c0e09200SDave Airlie RING_LOCALS; 329c0e09200SDave Airlie 330c0e09200SDave Airlie i915_kernel_lost_context(dev); 331c0e09200SDave Airlie 332c0e09200SDave Airlie DRM_DEBUG("\n"); 333c0e09200SDave Airlie 334c0e09200SDave Airlie dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter; 335c0e09200SDave Airlie 336c0e09200SDave Airlie if (dev_priv->counter > 0x7FFFFFFFUL) 337c0e09200SDave Airlie dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1; 338c0e09200SDave Airlie 339c0e09200SDave Airlie BEGIN_LP_RING(6); 340585fb111SJesse Barnes OUT_RING(MI_STORE_DWORD_INDEX); 341585fb111SJesse Barnes OUT_RING(5 << MI_STORE_DWORD_INDEX_SHIFT); 342c0e09200SDave Airlie OUT_RING(dev_priv->counter); 343c0e09200SDave Airlie OUT_RING(0); 344c0e09200SDave Airlie OUT_RING(0); 345585fb111SJesse Barnes OUT_RING(MI_USER_INTERRUPT); 346c0e09200SDave Airlie ADVANCE_LP_RING(); 347c0e09200SDave Airlie 348c0e09200SDave Airlie return dev_priv->counter; 349c0e09200SDave Airlie } 350c0e09200SDave Airlie 351ed4cb414SEric Anholt static void i915_user_irq_get(struct drm_device *dev) 352ed4cb414SEric Anholt { 353ed4cb414SEric Anholt drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 354ed4cb414SEric Anholt 355ed4cb414SEric Anholt spin_lock(&dev_priv->user_irq_lock); 356ed4cb414SEric Anholt if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) 357ed4cb414SEric Anholt i915_enable_irq(dev_priv, I915_USER_INTERRUPT); 358ed4cb414SEric Anholt spin_unlock(&dev_priv->user_irq_lock); 359ed4cb414SEric Anholt } 360ed4cb414SEric Anholt 361ed4cb414SEric Anholt static void i915_user_irq_put(struct drm_device *dev) 362ed4cb414SEric Anholt { 363ed4cb414SEric Anholt drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 364ed4cb414SEric Anholt 365ed4cb414SEric Anholt spin_lock(&dev_priv->user_irq_lock); 366ed4cb414SEric Anholt BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0); 367ed4cb414SEric Anholt if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) 368ed4cb414SEric Anholt i915_disable_irq(dev_priv, I915_USER_INTERRUPT); 369ed4cb414SEric Anholt spin_unlock(&dev_priv->user_irq_lock); 370ed4cb414SEric Anholt } 371ed4cb414SEric Anholt 372c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr) 373c0e09200SDave Airlie { 374c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 375c0e09200SDave Airlie int ret = 0; 376c0e09200SDave Airlie 377c0e09200SDave Airlie DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr, 378c0e09200SDave Airlie READ_BREADCRUMB(dev_priv)); 379c0e09200SDave Airlie 380ed4cb414SEric Anholt if (READ_BREADCRUMB(dev_priv) >= irq_nr) { 381ed4cb414SEric Anholt dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv); 382c0e09200SDave Airlie return 0; 383ed4cb414SEric Anholt } 384c0e09200SDave Airlie 385c0e09200SDave Airlie dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT; 386c0e09200SDave Airlie 387ed4cb414SEric Anholt i915_user_irq_get(dev); 388c0e09200SDave Airlie DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ, 389c0e09200SDave Airlie READ_BREADCRUMB(dev_priv) >= irq_nr); 390ed4cb414SEric Anholt i915_user_irq_put(dev); 391c0e09200SDave Airlie 392c0e09200SDave Airlie if (ret == -EBUSY) { 393c0e09200SDave Airlie DRM_ERROR("EBUSY -- rec: %d emitted: %d\n", 394c0e09200SDave Airlie READ_BREADCRUMB(dev_priv), (int)dev_priv->counter); 395c0e09200SDave Airlie } 396c0e09200SDave Airlie 397c0e09200SDave Airlie dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv); 398c0e09200SDave Airlie return ret; 399c0e09200SDave Airlie } 400c0e09200SDave Airlie 401c0e09200SDave Airlie static int i915_driver_vblank_do_wait(struct drm_device *dev, unsigned int *sequence, 402c0e09200SDave Airlie atomic_t *counter) 403c0e09200SDave Airlie { 404c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 405c0e09200SDave Airlie unsigned int cur_vblank; 406c0e09200SDave Airlie int ret = 0; 407c0e09200SDave Airlie 408c0e09200SDave Airlie if (!dev_priv) { 409c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 410c0e09200SDave Airlie return -EINVAL; 411c0e09200SDave Airlie } 412c0e09200SDave Airlie 413c0e09200SDave Airlie DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ, 414c0e09200SDave Airlie (((cur_vblank = atomic_read(counter)) 415c0e09200SDave Airlie - *sequence) <= (1<<23))); 416c0e09200SDave Airlie 417c0e09200SDave Airlie *sequence = cur_vblank; 418c0e09200SDave Airlie 419c0e09200SDave Airlie return ret; 420c0e09200SDave Airlie } 421c0e09200SDave Airlie 422c0e09200SDave Airlie 423c0e09200SDave Airlie int i915_driver_vblank_wait(struct drm_device *dev, unsigned int *sequence) 424c0e09200SDave Airlie { 425c0e09200SDave Airlie return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received); 426c0e09200SDave Airlie } 427c0e09200SDave Airlie 428c0e09200SDave Airlie int i915_driver_vblank_wait2(struct drm_device *dev, unsigned int *sequence) 429c0e09200SDave Airlie { 430c0e09200SDave Airlie return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received2); 431c0e09200SDave Airlie } 432c0e09200SDave Airlie 433c0e09200SDave Airlie /* Needs the lock as it touches the ring. 434c0e09200SDave Airlie */ 435c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data, 436c0e09200SDave Airlie struct drm_file *file_priv) 437c0e09200SDave Airlie { 438c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 439c0e09200SDave Airlie drm_i915_irq_emit_t *emit = data; 440c0e09200SDave Airlie int result; 441c0e09200SDave Airlie 442c0e09200SDave Airlie LOCK_TEST_WITH_RETURN(dev, file_priv); 443c0e09200SDave Airlie 444c0e09200SDave Airlie if (!dev_priv) { 445c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 446c0e09200SDave Airlie return -EINVAL; 447c0e09200SDave Airlie } 448c0e09200SDave Airlie 449c0e09200SDave Airlie result = i915_emit_irq(dev); 450c0e09200SDave Airlie 451c0e09200SDave Airlie if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) { 452c0e09200SDave Airlie DRM_ERROR("copy_to_user\n"); 453c0e09200SDave Airlie return -EFAULT; 454c0e09200SDave Airlie } 455c0e09200SDave Airlie 456c0e09200SDave Airlie return 0; 457c0e09200SDave Airlie } 458c0e09200SDave Airlie 459c0e09200SDave Airlie /* Doesn't need the hardware lock. 460c0e09200SDave Airlie */ 461c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data, 462c0e09200SDave Airlie struct drm_file *file_priv) 463c0e09200SDave Airlie { 464c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 465c0e09200SDave Airlie drm_i915_irq_wait_t *irqwait = data; 466c0e09200SDave Airlie 467c0e09200SDave Airlie if (!dev_priv) { 468c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 469c0e09200SDave Airlie return -EINVAL; 470c0e09200SDave Airlie } 471c0e09200SDave Airlie 472c0e09200SDave Airlie return i915_wait_irq(dev, irqwait->irq_seq); 473c0e09200SDave Airlie } 474c0e09200SDave Airlie 475c0e09200SDave Airlie /* Set the vblank monitor pipe 476c0e09200SDave Airlie */ 477c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data, 478c0e09200SDave Airlie struct drm_file *file_priv) 479c0e09200SDave Airlie { 480c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 481c0e09200SDave Airlie drm_i915_vblank_pipe_t *pipe = data; 482ed4cb414SEric Anholt u32 enable_mask = 0, disable_mask = 0; 483c0e09200SDave Airlie 484c0e09200SDave Airlie if (!dev_priv) { 485c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 486c0e09200SDave Airlie return -EINVAL; 487c0e09200SDave Airlie } 488c0e09200SDave Airlie 489c0e09200SDave Airlie if (pipe->pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) { 490c0e09200SDave Airlie DRM_ERROR("called with invalid pipe 0x%x\n", pipe->pipe); 491c0e09200SDave Airlie return -EINVAL; 492c0e09200SDave Airlie } 493c0e09200SDave Airlie 494ed4cb414SEric Anholt if (pipe->pipe & DRM_I915_VBLANK_PIPE_A) 495ed4cb414SEric Anholt enable_mask |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT; 496ed4cb414SEric Anholt else 497ed4cb414SEric Anholt disable_mask |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT; 498c0e09200SDave Airlie 499ed4cb414SEric Anholt if (pipe->pipe & DRM_I915_VBLANK_PIPE_B) 500ed4cb414SEric Anholt enable_mask |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT; 501ed4cb414SEric Anholt else 502ed4cb414SEric Anholt disable_mask |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT; 503ed4cb414SEric Anholt 504ed4cb414SEric Anholt i915_enable_irq(dev_priv, enable_mask); 505ed4cb414SEric Anholt i915_disable_irq(dev_priv, disable_mask); 506ed4cb414SEric Anholt 507ed4cb414SEric Anholt dev_priv->vblank_pipe = pipe->pipe; 508c0e09200SDave Airlie 509c0e09200SDave Airlie return 0; 510c0e09200SDave Airlie } 511c0e09200SDave Airlie 512c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data, 513c0e09200SDave Airlie struct drm_file *file_priv) 514c0e09200SDave Airlie { 515c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 516c0e09200SDave Airlie drm_i915_vblank_pipe_t *pipe = data; 517c0e09200SDave Airlie u16 flag; 518c0e09200SDave Airlie 519c0e09200SDave Airlie if (!dev_priv) { 520c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 521c0e09200SDave Airlie return -EINVAL; 522c0e09200SDave Airlie } 523c0e09200SDave Airlie 524ed4cb414SEric Anholt flag = I915_READ(IMR); 525c0e09200SDave Airlie pipe->pipe = 0; 526585fb111SJesse Barnes if (flag & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) 527c0e09200SDave Airlie pipe->pipe |= DRM_I915_VBLANK_PIPE_A; 528585fb111SJesse Barnes if (flag & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) 529c0e09200SDave Airlie pipe->pipe |= DRM_I915_VBLANK_PIPE_B; 530c0e09200SDave Airlie 531c0e09200SDave Airlie return 0; 532c0e09200SDave Airlie } 533c0e09200SDave Airlie 534c0e09200SDave Airlie /** 535c0e09200SDave Airlie * Schedule buffer swap at given vertical blank. 536c0e09200SDave Airlie */ 537c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data, 538c0e09200SDave Airlie struct drm_file *file_priv) 539c0e09200SDave Airlie { 540c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 541c0e09200SDave Airlie drm_i915_vblank_swap_t *swap = data; 542c0e09200SDave Airlie drm_i915_vbl_swap_t *vbl_swap; 543c0e09200SDave Airlie unsigned int pipe, seqtype, curseq; 544c0e09200SDave Airlie unsigned long irqflags; 545c0e09200SDave Airlie struct list_head *list; 546c0e09200SDave Airlie 547c0e09200SDave Airlie if (!dev_priv) { 548c0e09200SDave Airlie DRM_ERROR("%s called with no initialization\n", __func__); 549c0e09200SDave Airlie return -EINVAL; 550c0e09200SDave Airlie } 551c0e09200SDave Airlie 552c0e09200SDave Airlie if (dev_priv->sarea_priv->rotation) { 553c0e09200SDave Airlie DRM_DEBUG("Rotation not supported\n"); 554c0e09200SDave Airlie return -EINVAL; 555c0e09200SDave Airlie } 556c0e09200SDave Airlie 557c0e09200SDave Airlie if (swap->seqtype & ~(_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE | 558c0e09200SDave Airlie _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS)) { 559c0e09200SDave Airlie DRM_ERROR("Invalid sequence type 0x%x\n", swap->seqtype); 560c0e09200SDave Airlie return -EINVAL; 561c0e09200SDave Airlie } 562c0e09200SDave Airlie 563c0e09200SDave Airlie pipe = (swap->seqtype & _DRM_VBLANK_SECONDARY) ? 1 : 0; 564c0e09200SDave Airlie 565c0e09200SDave Airlie seqtype = swap->seqtype & (_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE); 566c0e09200SDave Airlie 567c0e09200SDave Airlie if (!(dev_priv->vblank_pipe & (1 << pipe))) { 568c0e09200SDave Airlie DRM_ERROR("Invalid pipe %d\n", pipe); 569c0e09200SDave Airlie return -EINVAL; 570c0e09200SDave Airlie } 571c0e09200SDave Airlie 572c0e09200SDave Airlie spin_lock_irqsave(&dev->drw_lock, irqflags); 573c0e09200SDave Airlie 574c0e09200SDave Airlie if (!drm_get_drawable_info(dev, swap->drawable)) { 575c0e09200SDave Airlie spin_unlock_irqrestore(&dev->drw_lock, irqflags); 576c0e09200SDave Airlie DRM_DEBUG("Invalid drawable ID %d\n", swap->drawable); 577c0e09200SDave Airlie return -EINVAL; 578c0e09200SDave Airlie } 579c0e09200SDave Airlie 580c0e09200SDave Airlie spin_unlock_irqrestore(&dev->drw_lock, irqflags); 581c0e09200SDave Airlie 582c0e09200SDave Airlie curseq = atomic_read(pipe ? &dev->vbl_received2 : &dev->vbl_received); 583c0e09200SDave Airlie 584c0e09200SDave Airlie if (seqtype == _DRM_VBLANK_RELATIVE) 585c0e09200SDave Airlie swap->sequence += curseq; 586c0e09200SDave Airlie 587c0e09200SDave Airlie if ((curseq - swap->sequence) <= (1<<23)) { 588c0e09200SDave Airlie if (swap->seqtype & _DRM_VBLANK_NEXTONMISS) { 589c0e09200SDave Airlie swap->sequence = curseq + 1; 590c0e09200SDave Airlie } else { 591c0e09200SDave Airlie DRM_DEBUG("Missed target sequence\n"); 592c0e09200SDave Airlie return -EINVAL; 593c0e09200SDave Airlie } 594c0e09200SDave Airlie } 595c0e09200SDave Airlie 596c0e09200SDave Airlie spin_lock_irqsave(&dev_priv->swaps_lock, irqflags); 597c0e09200SDave Airlie 598c0e09200SDave Airlie list_for_each(list, &dev_priv->vbl_swaps.head) { 599c0e09200SDave Airlie vbl_swap = list_entry(list, drm_i915_vbl_swap_t, head); 600c0e09200SDave Airlie 601c0e09200SDave Airlie if (vbl_swap->drw_id == swap->drawable && 602c0e09200SDave Airlie vbl_swap->pipe == pipe && 603c0e09200SDave Airlie vbl_swap->sequence == swap->sequence) { 604c0e09200SDave Airlie spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags); 605c0e09200SDave Airlie DRM_DEBUG("Already scheduled\n"); 606c0e09200SDave Airlie return 0; 607c0e09200SDave Airlie } 608c0e09200SDave Airlie } 609c0e09200SDave Airlie 610c0e09200SDave Airlie spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags); 611c0e09200SDave Airlie 612c0e09200SDave Airlie if (dev_priv->swaps_pending >= 100) { 613c0e09200SDave Airlie DRM_DEBUG("Too many swaps queued\n"); 614c0e09200SDave Airlie return -EBUSY; 615c0e09200SDave Airlie } 616c0e09200SDave Airlie 617c0e09200SDave Airlie vbl_swap = drm_calloc(1, sizeof(*vbl_swap), DRM_MEM_DRIVER); 618c0e09200SDave Airlie 619c0e09200SDave Airlie if (!vbl_swap) { 620c0e09200SDave Airlie DRM_ERROR("Failed to allocate memory to queue swap\n"); 621c0e09200SDave Airlie return -ENOMEM; 622c0e09200SDave Airlie } 623c0e09200SDave Airlie 624c0e09200SDave Airlie DRM_DEBUG("\n"); 625c0e09200SDave Airlie 626c0e09200SDave Airlie vbl_swap->drw_id = swap->drawable; 627c0e09200SDave Airlie vbl_swap->pipe = pipe; 628c0e09200SDave Airlie vbl_swap->sequence = swap->sequence; 629c0e09200SDave Airlie 630c0e09200SDave Airlie spin_lock_irqsave(&dev_priv->swaps_lock, irqflags); 631c0e09200SDave Airlie 632c0e09200SDave Airlie list_add_tail(&vbl_swap->head, &dev_priv->vbl_swaps.head); 633c0e09200SDave Airlie dev_priv->swaps_pending++; 634c0e09200SDave Airlie 635c0e09200SDave Airlie spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags); 636c0e09200SDave Airlie 637c0e09200SDave Airlie return 0; 638c0e09200SDave Airlie } 639c0e09200SDave Airlie 640c0e09200SDave Airlie /* drm_dma.h hooks 641c0e09200SDave Airlie */ 642c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev) 643c0e09200SDave Airlie { 644c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 645c0e09200SDave Airlie 646ed4cb414SEric Anholt I915_WRITE(HWSTAM, 0xfffe); 647ed4cb414SEric Anholt I915_WRITE(IMR, 0x0); 648ed4cb414SEric Anholt I915_WRITE(IER, 0x0); 649c0e09200SDave Airlie } 650c0e09200SDave Airlie 651c0e09200SDave Airlie void i915_driver_irq_postinstall(struct drm_device * dev) 652c0e09200SDave Airlie { 653c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 654c0e09200SDave Airlie 655c0e09200SDave Airlie spin_lock_init(&dev_priv->swaps_lock); 656c0e09200SDave Airlie INIT_LIST_HEAD(&dev_priv->vbl_swaps.head); 657c0e09200SDave Airlie dev_priv->swaps_pending = 0; 658c0e09200SDave Airlie 659c0e09200SDave Airlie if (!dev_priv->vblank_pipe) 660c0e09200SDave Airlie dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A; 661ed4cb414SEric Anholt 662ed4cb414SEric Anholt /* Set initial unmasked IRQs to just the selected vblank pipes. */ 663ed4cb414SEric Anholt dev_priv->irq_mask_reg = ~0; 664ed4cb414SEric Anholt if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A) 665ed4cb414SEric Anholt dev_priv->irq_mask_reg &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT; 666ed4cb414SEric Anholt if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B) 667ed4cb414SEric Anholt dev_priv->irq_mask_reg &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT; 668ed4cb414SEric Anholt 669*8ee1c3dbSMatthew Garrett dev_priv->irq_mask_reg &= I915_INTERRUPT_ENABLE_MASK; 670*8ee1c3dbSMatthew Garrett 671ed4cb414SEric Anholt I915_WRITE(IMR, dev_priv->irq_mask_reg); 672ed4cb414SEric Anholt I915_WRITE(IER, I915_INTERRUPT_ENABLE_MASK); 673ed4cb414SEric Anholt (void) I915_READ(IER); 674ed4cb414SEric Anholt 675*8ee1c3dbSMatthew Garrett opregion_enable_asle(dev); 676*8ee1c3dbSMatthew Garrett 677c0e09200SDave Airlie DRM_INIT_WAITQUEUE(&dev_priv->irq_queue); 678c0e09200SDave Airlie } 679c0e09200SDave Airlie 680c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev) 681c0e09200SDave Airlie { 682c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 683c0e09200SDave Airlie u16 temp; 684c0e09200SDave Airlie 685c0e09200SDave Airlie if (!dev_priv) 686c0e09200SDave Airlie return; 687c0e09200SDave Airlie 688ed4cb414SEric Anholt I915_WRITE(HWSTAM, 0xffff); 689ed4cb414SEric Anholt I915_WRITE(IMR, 0xffff); 690ed4cb414SEric Anholt I915_WRITE(IER, 0x0); 691c0e09200SDave Airlie 692ed4cb414SEric Anholt temp = I915_READ(IIR); 693ed4cb414SEric Anholt I915_WRITE(IIR, temp); 694c0e09200SDave Airlie } 695