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" 3379e53945SJesse Barnes #include "intel_drv.h" 34c0e09200SDave Airlie 35c0e09200SDave Airlie #define MAX_NOPID ((u32)~0) 36c0e09200SDave Airlie 377c463586SKeith Packard /** 387c463586SKeith Packard * Interrupts that are always left unmasked. 397c463586SKeith Packard * 407c463586SKeith Packard * Since pipe events are edge-triggered from the PIPESTAT register to IIR, 417c463586SKeith Packard * we leave them always unmasked in IMR and then control enabling them through 427c463586SKeith Packard * PIPESTAT alone. 437c463586SKeith Packard */ 447c463586SKeith Packard #define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT | \ 450a3e67a4SJesse Barnes I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \ 468ee1c3dbSMatthew Garrett I915_DISPLAY_PIPE_B_EVENT_INTERRUPT) 47ed4cb414SEric Anholt 487c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */ 497c463586SKeith Packard #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT) 507c463586SKeith Packard 5179e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\ 5279e53945SJesse Barnes PIPE_VBLANK_INTERRUPT_STATUS) 5379e53945SJesse Barnes 5479e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\ 5579e53945SJesse Barnes PIPE_VBLANK_INTERRUPT_ENABLE) 5679e53945SJesse Barnes 5779e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \ 5879e53945SJesse Barnes DRM_I915_VBLANK_PIPE_B) 5979e53945SJesse Barnes 608ee1c3dbSMatthew Garrett void 61ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask) 62ed4cb414SEric Anholt { 63ed4cb414SEric Anholt if ((dev_priv->irq_mask_reg & mask) != 0) { 64ed4cb414SEric Anholt dev_priv->irq_mask_reg &= ~mask; 65ed4cb414SEric Anholt I915_WRITE(IMR, dev_priv->irq_mask_reg); 66ed4cb414SEric Anholt (void) I915_READ(IMR); 67ed4cb414SEric Anholt } 68ed4cb414SEric Anholt } 69ed4cb414SEric Anholt 70ed4cb414SEric Anholt static inline void 71ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask) 72ed4cb414SEric Anholt { 73ed4cb414SEric Anholt if ((dev_priv->irq_mask_reg & mask) != mask) { 74ed4cb414SEric Anholt dev_priv->irq_mask_reg |= mask; 75ed4cb414SEric Anholt I915_WRITE(IMR, dev_priv->irq_mask_reg); 76ed4cb414SEric Anholt (void) I915_READ(IMR); 77ed4cb414SEric Anholt } 78ed4cb414SEric Anholt } 79ed4cb414SEric Anholt 807c463586SKeith Packard static inline u32 817c463586SKeith Packard i915_pipestat(int pipe) 827c463586SKeith Packard { 837c463586SKeith Packard if (pipe == 0) 847c463586SKeith Packard return PIPEASTAT; 857c463586SKeith Packard if (pipe == 1) 867c463586SKeith Packard return PIPEBSTAT; 879c84ba4eSAndrew Morton BUG(); 887c463586SKeith Packard } 897c463586SKeith Packard 907c463586SKeith Packard void 917c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask) 927c463586SKeith Packard { 937c463586SKeith Packard if ((dev_priv->pipestat[pipe] & mask) != mask) { 947c463586SKeith Packard u32 reg = i915_pipestat(pipe); 957c463586SKeith Packard 967c463586SKeith Packard dev_priv->pipestat[pipe] |= mask; 977c463586SKeith Packard /* Enable the interrupt, clear any pending status */ 987c463586SKeith Packard I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16)); 997c463586SKeith Packard (void) I915_READ(reg); 1007c463586SKeith Packard } 1017c463586SKeith Packard } 1027c463586SKeith Packard 1037c463586SKeith Packard void 1047c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask) 1057c463586SKeith Packard { 1067c463586SKeith Packard if ((dev_priv->pipestat[pipe] & mask) != 0) { 1077c463586SKeith Packard u32 reg = i915_pipestat(pipe); 1087c463586SKeith Packard 1097c463586SKeith Packard dev_priv->pipestat[pipe] &= ~mask; 1107c463586SKeith Packard I915_WRITE(reg, dev_priv->pipestat[pipe]); 1117c463586SKeith Packard (void) I915_READ(reg); 1127c463586SKeith Packard } 1137c463586SKeith Packard } 1147c463586SKeith Packard 115c0e09200SDave Airlie /** 1160a3e67a4SJesse Barnes * i915_pipe_enabled - check if a pipe is enabled 1170a3e67a4SJesse Barnes * @dev: DRM device 1180a3e67a4SJesse Barnes * @pipe: pipe to check 1190a3e67a4SJesse Barnes * 1200a3e67a4SJesse Barnes * Reading certain registers when the pipe is disabled can hang the chip. 1210a3e67a4SJesse Barnes * Use this routine to make sure the PLL is running and the pipe is active 1220a3e67a4SJesse Barnes * before reading such registers if unsure. 1230a3e67a4SJesse Barnes */ 1240a3e67a4SJesse Barnes static int 1250a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe) 1260a3e67a4SJesse Barnes { 1270a3e67a4SJesse Barnes drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 1280a3e67a4SJesse Barnes unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF; 1290a3e67a4SJesse Barnes 1300a3e67a4SJesse Barnes if (I915_READ(pipeconf) & PIPEACONF_ENABLE) 1310a3e67a4SJesse Barnes return 1; 1320a3e67a4SJesse Barnes 1330a3e67a4SJesse Barnes return 0; 1340a3e67a4SJesse Barnes } 1350a3e67a4SJesse Barnes 13642f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which 13742f52ef8SKeith Packard * we use as a pipe index 13842f52ef8SKeith Packard */ 13942f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe) 1400a3e67a4SJesse Barnes { 1410a3e67a4SJesse Barnes drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 1420a3e67a4SJesse Barnes unsigned long high_frame; 1430a3e67a4SJesse Barnes unsigned long low_frame; 1440a3e67a4SJesse Barnes u32 high1, high2, low, count; 1450a3e67a4SJesse Barnes 1460a3e67a4SJesse Barnes high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH; 1470a3e67a4SJesse Barnes low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL; 1480a3e67a4SJesse Barnes 1490a3e67a4SJesse Barnes if (!i915_pipe_enabled(dev, pipe)) { 1500a3e67a4SJesse Barnes DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe); 1510a3e67a4SJesse Barnes return 0; 1520a3e67a4SJesse Barnes } 1530a3e67a4SJesse Barnes 1540a3e67a4SJesse Barnes /* 1550a3e67a4SJesse Barnes * High & low register fields aren't synchronized, so make sure 1560a3e67a4SJesse Barnes * we get a low value that's stable across two reads of the high 1570a3e67a4SJesse Barnes * register. 1580a3e67a4SJesse Barnes */ 1590a3e67a4SJesse Barnes do { 1600a3e67a4SJesse Barnes high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >> 1610a3e67a4SJesse Barnes PIPE_FRAME_HIGH_SHIFT); 1620a3e67a4SJesse Barnes low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >> 1630a3e67a4SJesse Barnes PIPE_FRAME_LOW_SHIFT); 1640a3e67a4SJesse Barnes high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >> 1650a3e67a4SJesse Barnes PIPE_FRAME_HIGH_SHIFT); 1660a3e67a4SJesse Barnes } while (high1 != high2); 1670a3e67a4SJesse Barnes 1680a3e67a4SJesse Barnes count = (high1 << 8) | low; 1690a3e67a4SJesse Barnes 1700a3e67a4SJesse Barnes return count; 1710a3e67a4SJesse Barnes } 1720a3e67a4SJesse Barnes 1739880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe) 1749880b7a5SJesse Barnes { 1759880b7a5SJesse Barnes drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 1769880b7a5SJesse Barnes int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45; 1779880b7a5SJesse Barnes 1789880b7a5SJesse Barnes if (!i915_pipe_enabled(dev, pipe)) { 1799880b7a5SJesse Barnes DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe); 1809880b7a5SJesse Barnes return 0; 1819880b7a5SJesse Barnes } 1829880b7a5SJesse Barnes 1839880b7a5SJesse Barnes return I915_READ(reg); 1849880b7a5SJesse Barnes } 1859880b7a5SJesse Barnes 1865ca58282SJesse Barnes /* 1875ca58282SJesse Barnes * Handle hotplug events outside the interrupt handler proper. 1885ca58282SJesse Barnes */ 1895ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work) 1905ca58282SJesse Barnes { 1915ca58282SJesse Barnes drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t, 1925ca58282SJesse Barnes hotplug_work); 1935ca58282SJesse Barnes struct drm_device *dev = dev_priv->dev; 1945ca58282SJesse Barnes 1955ca58282SJesse Barnes /* Just fire off a uevent and let userspace tell us what to do */ 1965ca58282SJesse Barnes drm_sysfs_hotplug_event(dev); 1975ca58282SJesse Barnes } 1985ca58282SJesse Barnes 199c0e09200SDave Airlie irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS) 200c0e09200SDave Airlie { 201c0e09200SDave Airlie struct drm_device *dev = (struct drm_device *) arg; 202c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 2037c1c2871SDave Airlie struct drm_i915_master_private *master_priv; 204cdfbc41fSEric Anholt u32 iir, new_iir; 205cdfbc41fSEric Anholt u32 pipea_stats, pipeb_stats; 20605eff845SKeith Packard u32 vblank_status; 20705eff845SKeith Packard u32 vblank_enable; 2080a3e67a4SJesse Barnes int vblank = 0; 2097c463586SKeith Packard unsigned long irqflags; 21005eff845SKeith Packard int irq_received; 21105eff845SKeith Packard int ret = IRQ_NONE; 212c0e09200SDave Airlie 213630681d9SEric Anholt atomic_inc(&dev_priv->irq_received); 214630681d9SEric Anholt 215ed4cb414SEric Anholt iir = I915_READ(IIR); 216c0e09200SDave Airlie 21705eff845SKeith Packard if (IS_I965G(dev)) { 21805eff845SKeith Packard vblank_status = I915_START_VBLANK_INTERRUPT_STATUS; 21905eff845SKeith Packard vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE; 22005eff845SKeith Packard } else { 22105eff845SKeith Packard vblank_status = I915_VBLANK_INTERRUPT_STATUS; 22205eff845SKeith Packard vblank_enable = I915_VBLANK_INTERRUPT_ENABLE; 22305eff845SKeith Packard } 224c0e09200SDave Airlie 22505eff845SKeith Packard for (;;) { 22605eff845SKeith Packard irq_received = iir != 0; 22705eff845SKeith Packard 22805eff845SKeith Packard /* Can't rely on pipestat interrupt bit in iir as it might 22905eff845SKeith Packard * have been cleared after the pipestat interrupt was received. 23005eff845SKeith Packard * It doesn't set the bit in iir again, but it still produces 23105eff845SKeith Packard * interrupts (for non-MSI). 23205eff845SKeith Packard */ 23305eff845SKeith Packard spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags); 23405eff845SKeith Packard pipea_stats = I915_READ(PIPEASTAT); 23505eff845SKeith Packard pipeb_stats = I915_READ(PIPEBSTAT); 23679e53945SJesse Barnes 2370a3e67a4SJesse Barnes /* 2387c463586SKeith Packard * Clear the PIPE(A|B)STAT regs before the IIR 2390a3e67a4SJesse Barnes */ 24005eff845SKeith Packard if (pipea_stats & 0x8000ffff) { 2418ee1c3dbSMatthew Garrett I915_WRITE(PIPEASTAT, pipea_stats); 24205eff845SKeith Packard irq_received = 1; 2430a3e67a4SJesse Barnes } 2447c463586SKeith Packard 24505eff845SKeith Packard if (pipeb_stats & 0x8000ffff) { 2460a3e67a4SJesse Barnes I915_WRITE(PIPEBSTAT, pipeb_stats); 24705eff845SKeith Packard irq_received = 1; 248c0e09200SDave Airlie } 24905eff845SKeith Packard spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags); 25005eff845SKeith Packard 25105eff845SKeith Packard if (!irq_received) 25205eff845SKeith Packard break; 25305eff845SKeith Packard 25405eff845SKeith Packard ret = IRQ_HANDLED; 255c0e09200SDave Airlie 2565ca58282SJesse Barnes /* Consume port. Then clear IIR or we'll miss events */ 2575ca58282SJesse Barnes if ((I915_HAS_HOTPLUG(dev)) && 2585ca58282SJesse Barnes (iir & I915_DISPLAY_PORT_INTERRUPT)) { 2595ca58282SJesse Barnes u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT); 2605ca58282SJesse Barnes 2615ca58282SJesse Barnes DRM_DEBUG("hotplug event received, stat 0x%08x\n", 2625ca58282SJesse Barnes hotplug_status); 2635ca58282SJesse Barnes if (hotplug_status & dev_priv->hotplug_supported_mask) 2645ca58282SJesse Barnes schedule_work(&dev_priv->hotplug_work); 2655ca58282SJesse Barnes 2665ca58282SJesse Barnes I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status); 2675ca58282SJesse Barnes I915_READ(PORT_HOTPLUG_STAT); 2685ca58282SJesse Barnes } 2695ca58282SJesse Barnes 270673a394bSEric Anholt I915_WRITE(IIR, iir); 271cdfbc41fSEric Anholt new_iir = I915_READ(IIR); /* Flush posted writes */ 2727c463586SKeith Packard 2737c1c2871SDave Airlie if (dev->primary->master) { 2747c1c2871SDave Airlie master_priv = dev->primary->master->driver_priv; 2757c1c2871SDave Airlie if (master_priv->sarea_priv) 2767c1c2871SDave Airlie master_priv->sarea_priv->last_dispatch = 277c99b058fSKristian Høgsberg READ_BREADCRUMB(dev_priv); 2787c1c2871SDave Airlie } 2790a3e67a4SJesse Barnes 280673a394bSEric Anholt if (iir & I915_USER_INTERRUPT) { 281673a394bSEric Anholt dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev); 282673a394bSEric Anholt DRM_WAKEUP(&dev_priv->irq_queue); 283673a394bSEric Anholt } 284673a394bSEric Anholt 28505eff845SKeith Packard if (pipea_stats & vblank_status) { 2867c463586SKeith Packard vblank++; 2877c463586SKeith Packard drm_handle_vblank(dev, 0); 2887c463586SKeith Packard } 2897c463586SKeith Packard 29005eff845SKeith Packard if (pipeb_stats & vblank_status) { 2917c463586SKeith Packard vblank++; 2927c463586SKeith Packard drm_handle_vblank(dev, 1); 2937c463586SKeith Packard } 2947c463586SKeith Packard 2957c463586SKeith Packard if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) || 2967c463586SKeith Packard (iir & I915_ASLE_INTERRUPT)) 297673a394bSEric Anholt opregion_asle_intr(dev); 2980a3e67a4SJesse Barnes 299cdfbc41fSEric Anholt /* With MSI, interrupts are only generated when iir 300cdfbc41fSEric Anholt * transitions from zero to nonzero. If another bit got 301cdfbc41fSEric Anholt * set while we were handling the existing iir bits, then 302cdfbc41fSEric Anholt * we would never get another interrupt. 303cdfbc41fSEric Anholt * 304cdfbc41fSEric Anholt * This is fine on non-MSI as well, as if we hit this path 305cdfbc41fSEric Anholt * we avoid exiting the interrupt handler only to generate 306cdfbc41fSEric Anholt * another one. 307cdfbc41fSEric Anholt * 308cdfbc41fSEric Anholt * Note that for MSI this could cause a stray interrupt report 309cdfbc41fSEric Anholt * if an interrupt landed in the time between writing IIR and 310cdfbc41fSEric Anholt * the posting read. This should be rare enough to never 311cdfbc41fSEric Anholt * trigger the 99% of 100,000 interrupts test for disabling 312cdfbc41fSEric Anholt * stray interrupts. 313cdfbc41fSEric Anholt */ 314cdfbc41fSEric Anholt iir = new_iir; 31505eff845SKeith Packard } 316cdfbc41fSEric Anholt 31705eff845SKeith Packard return ret; 318c0e09200SDave Airlie } 319c0e09200SDave Airlie 320c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev) 321c0e09200SDave Airlie { 322c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 3237c1c2871SDave Airlie struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv; 324c0e09200SDave Airlie RING_LOCALS; 325c0e09200SDave Airlie 326c0e09200SDave Airlie i915_kernel_lost_context(dev); 327c0e09200SDave Airlie 328c0e09200SDave Airlie DRM_DEBUG("\n"); 329c0e09200SDave Airlie 330c99b058fSKristian Høgsberg dev_priv->counter++; 331c0e09200SDave Airlie if (dev_priv->counter > 0x7FFFFFFFUL) 332c99b058fSKristian Høgsberg dev_priv->counter = 1; 3337c1c2871SDave Airlie if (master_priv->sarea_priv) 3347c1c2871SDave Airlie master_priv->sarea_priv->last_enqueue = dev_priv->counter; 335c0e09200SDave Airlie 3360baf823aSKeith Packard BEGIN_LP_RING(4); 337585fb111SJesse Barnes OUT_RING(MI_STORE_DWORD_INDEX); 3380baf823aSKeith Packard OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT); 339c0e09200SDave Airlie OUT_RING(dev_priv->counter); 340585fb111SJesse Barnes OUT_RING(MI_USER_INTERRUPT); 341c0e09200SDave Airlie ADVANCE_LP_RING(); 342c0e09200SDave Airlie 343c0e09200SDave Airlie return dev_priv->counter; 344c0e09200SDave Airlie } 345c0e09200SDave Airlie 346673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev) 347ed4cb414SEric Anholt { 348ed4cb414SEric Anholt drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 349e9d21d7fSKeith Packard unsigned long irqflags; 350ed4cb414SEric Anholt 351e9d21d7fSKeith Packard spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags); 352ed4cb414SEric Anholt if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1)) 353ed4cb414SEric Anholt i915_enable_irq(dev_priv, I915_USER_INTERRUPT); 354e9d21d7fSKeith Packard spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags); 355ed4cb414SEric Anholt } 356ed4cb414SEric Anholt 3570a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev) 358ed4cb414SEric Anholt { 359ed4cb414SEric Anholt drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 360e9d21d7fSKeith Packard unsigned long irqflags; 361ed4cb414SEric Anholt 362e9d21d7fSKeith Packard spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags); 363ed4cb414SEric Anholt BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0); 364ed4cb414SEric Anholt if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0)) 365ed4cb414SEric Anholt i915_disable_irq(dev_priv, I915_USER_INTERRUPT); 366e9d21d7fSKeith Packard spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags); 367ed4cb414SEric Anholt } 368ed4cb414SEric Anholt 369c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr) 370c0e09200SDave Airlie { 371c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 3727c1c2871SDave Airlie struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv; 373c0e09200SDave Airlie int ret = 0; 374c0e09200SDave Airlie 375c0e09200SDave Airlie DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr, 376c0e09200SDave Airlie READ_BREADCRUMB(dev_priv)); 377c0e09200SDave Airlie 378ed4cb414SEric Anholt if (READ_BREADCRUMB(dev_priv) >= irq_nr) { 3797c1c2871SDave Airlie if (master_priv->sarea_priv) 3807c1c2871SDave Airlie master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv); 381c0e09200SDave Airlie return 0; 382ed4cb414SEric Anholt } 383c0e09200SDave Airlie 3847c1c2871SDave Airlie if (master_priv->sarea_priv) 3857c1c2871SDave Airlie master_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 return ret; 398c0e09200SDave Airlie } 399c0e09200SDave Airlie 400c0e09200SDave Airlie /* Needs the lock as it touches the ring. 401c0e09200SDave Airlie */ 402c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data, 403c0e09200SDave Airlie struct drm_file *file_priv) 404c0e09200SDave Airlie { 405c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 406c0e09200SDave Airlie drm_i915_irq_emit_t *emit = data; 407c0e09200SDave Airlie int result; 408c0e09200SDave Airlie 40907f4f8bfSEric Anholt if (!dev_priv || !dev_priv->ring.virtual_start) { 410c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 411c0e09200SDave Airlie return -EINVAL; 412c0e09200SDave Airlie } 413299eb93cSEric Anholt 414299eb93cSEric Anholt RING_LOCK_TEST_WITH_RETURN(dev, file_priv); 415299eb93cSEric Anholt 416546b0974SEric Anholt mutex_lock(&dev->struct_mutex); 417c0e09200SDave Airlie result = i915_emit_irq(dev); 418546b0974SEric Anholt mutex_unlock(&dev->struct_mutex); 419c0e09200SDave Airlie 420c0e09200SDave Airlie if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) { 421c0e09200SDave Airlie DRM_ERROR("copy_to_user\n"); 422c0e09200SDave Airlie return -EFAULT; 423c0e09200SDave Airlie } 424c0e09200SDave Airlie 425c0e09200SDave Airlie return 0; 426c0e09200SDave Airlie } 427c0e09200SDave Airlie 428c0e09200SDave Airlie /* Doesn't need the hardware lock. 429c0e09200SDave Airlie */ 430c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data, 431c0e09200SDave Airlie struct drm_file *file_priv) 432c0e09200SDave Airlie { 433c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 434c0e09200SDave Airlie drm_i915_irq_wait_t *irqwait = data; 435c0e09200SDave Airlie 436c0e09200SDave Airlie if (!dev_priv) { 437c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 438c0e09200SDave Airlie return -EINVAL; 439c0e09200SDave Airlie } 440c0e09200SDave Airlie 441c0e09200SDave Airlie return i915_wait_irq(dev, irqwait->irq_seq); 442c0e09200SDave Airlie } 443c0e09200SDave Airlie 44442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which 44542f52ef8SKeith Packard * we use as a pipe index 44642f52ef8SKeith Packard */ 44742f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe) 4480a3e67a4SJesse Barnes { 4490a3e67a4SJesse Barnes drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 450e9d21d7fSKeith Packard unsigned long irqflags; 45171e0ffa5SJesse Barnes int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF; 45271e0ffa5SJesse Barnes u32 pipeconf; 45371e0ffa5SJesse Barnes 45471e0ffa5SJesse Barnes pipeconf = I915_READ(pipeconf_reg); 45571e0ffa5SJesse Barnes if (!(pipeconf & PIPEACONF_ENABLE)) 45671e0ffa5SJesse Barnes return -EINVAL; 4570a3e67a4SJesse Barnes 458e9d21d7fSKeith Packard spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags); 4590a3e67a4SJesse Barnes if (IS_I965G(dev)) 4607c463586SKeith Packard i915_enable_pipestat(dev_priv, pipe, 4617c463586SKeith Packard PIPE_START_VBLANK_INTERRUPT_ENABLE); 4620a3e67a4SJesse Barnes else 4637c463586SKeith Packard i915_enable_pipestat(dev_priv, pipe, 4647c463586SKeith Packard PIPE_VBLANK_INTERRUPT_ENABLE); 465e9d21d7fSKeith Packard spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags); 4660a3e67a4SJesse Barnes return 0; 4670a3e67a4SJesse Barnes } 4680a3e67a4SJesse Barnes 46942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which 47042f52ef8SKeith Packard * we use as a pipe index 47142f52ef8SKeith Packard */ 47242f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe) 4730a3e67a4SJesse Barnes { 4740a3e67a4SJesse Barnes drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 475e9d21d7fSKeith Packard unsigned long irqflags; 4760a3e67a4SJesse Barnes 477e9d21d7fSKeith Packard spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags); 4787c463586SKeith Packard i915_disable_pipestat(dev_priv, pipe, 4797c463586SKeith Packard PIPE_VBLANK_INTERRUPT_ENABLE | 4807c463586SKeith Packard PIPE_START_VBLANK_INTERRUPT_ENABLE); 481e9d21d7fSKeith Packard spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags); 4820a3e67a4SJesse Barnes } 4830a3e67a4SJesse Barnes 48479e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev) 48579e53945SJesse Barnes { 48679e53945SJesse Barnes struct drm_i915_private *dev_priv = dev->dev_private; 487*e170b030SZhenyu Wang 488*e170b030SZhenyu Wang if (!IS_IGDNG(dev)) 48979e53945SJesse Barnes opregion_enable_asle(dev); 49079e53945SJesse Barnes dev_priv->irq_enabled = 1; 49179e53945SJesse Barnes } 49279e53945SJesse Barnes 49379e53945SJesse Barnes 494c0e09200SDave Airlie /* Set the vblank monitor pipe 495c0e09200SDave Airlie */ 496c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data, 497c0e09200SDave Airlie struct drm_file *file_priv) 498c0e09200SDave Airlie { 499c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 500c0e09200SDave Airlie 501c0e09200SDave Airlie if (!dev_priv) { 502c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 503c0e09200SDave Airlie return -EINVAL; 504c0e09200SDave Airlie } 505c0e09200SDave Airlie 506c0e09200SDave Airlie return 0; 507c0e09200SDave Airlie } 508c0e09200SDave Airlie 509c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data, 510c0e09200SDave Airlie struct drm_file *file_priv) 511c0e09200SDave Airlie { 512c0e09200SDave Airlie drm_i915_private_t *dev_priv = dev->dev_private; 513c0e09200SDave Airlie drm_i915_vblank_pipe_t *pipe = data; 514c0e09200SDave Airlie 515c0e09200SDave Airlie if (!dev_priv) { 516c0e09200SDave Airlie DRM_ERROR("called with no initialization\n"); 517c0e09200SDave Airlie return -EINVAL; 518c0e09200SDave Airlie } 519c0e09200SDave Airlie 5200a3e67a4SJesse Barnes pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B; 521c0e09200SDave Airlie 522c0e09200SDave Airlie return 0; 523c0e09200SDave Airlie } 524c0e09200SDave Airlie 525c0e09200SDave Airlie /** 526c0e09200SDave Airlie * Schedule buffer swap at given vertical blank. 527c0e09200SDave Airlie */ 528c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data, 529c0e09200SDave Airlie struct drm_file *file_priv) 530c0e09200SDave Airlie { 531bd95e0a4SEric Anholt /* The delayed swap mechanism was fundamentally racy, and has been 532bd95e0a4SEric Anholt * removed. The model was that the client requested a delayed flip/swap 533bd95e0a4SEric Anholt * from the kernel, then waited for vblank before continuing to perform 534bd95e0a4SEric Anholt * rendering. The problem was that the kernel might wake the client 535bd95e0a4SEric Anholt * up before it dispatched the vblank swap (since the lock has to be 536bd95e0a4SEric Anholt * held while touching the ringbuffer), in which case the client would 537bd95e0a4SEric Anholt * clear and start the next frame before the swap occurred, and 538bd95e0a4SEric Anholt * flicker would occur in addition to likely missing the vblank. 539bd95e0a4SEric Anholt * 540bd95e0a4SEric Anholt * In the absence of this ioctl, userland falls back to a correct path 541bd95e0a4SEric Anholt * of waiting for a vblank, then dispatching the swap on its own. 542bd95e0a4SEric Anholt * Context switching to userland and back is plenty fast enough for 543bd95e0a4SEric Anholt * meeting the requirements of vblank swapping. 5440a3e67a4SJesse Barnes */ 545c0e09200SDave Airlie return -EINVAL; 546c0e09200SDave Airlie } 547c0e09200SDave Airlie 548c0e09200SDave Airlie /* drm_dma.h hooks 549c0e09200SDave Airlie */ 550c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev) 551c0e09200SDave Airlie { 552c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 553c0e09200SDave Airlie 55479e53945SJesse Barnes atomic_set(&dev_priv->irq_received, 0); 55579e53945SJesse Barnes 5565ca58282SJesse Barnes if (I915_HAS_HOTPLUG(dev)) { 5575ca58282SJesse Barnes I915_WRITE(PORT_HOTPLUG_EN, 0); 5585ca58282SJesse Barnes I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT)); 5595ca58282SJesse Barnes } 5605ca58282SJesse Barnes 5610a3e67a4SJesse Barnes I915_WRITE(HWSTAM, 0xeffe); 5627c463586SKeith Packard I915_WRITE(PIPEASTAT, 0); 5637c463586SKeith Packard I915_WRITE(PIPEBSTAT, 0); 5640a3e67a4SJesse Barnes I915_WRITE(IMR, 0xffffffff); 565ed4cb414SEric Anholt I915_WRITE(IER, 0x0); 5667c463586SKeith Packard (void) I915_READ(IER); 5675ca58282SJesse Barnes INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func); 568c0e09200SDave Airlie } 569c0e09200SDave Airlie 5700a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev) 571c0e09200SDave Airlie { 572c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 5735ca58282SJesse Barnes u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR; 5740a3e67a4SJesse Barnes 5750a3e67a4SJesse Barnes dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B; 576ed4cb414SEric Anholt 5770a3e67a4SJesse Barnes dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */ 5780a3e67a4SJesse Barnes 5797c463586SKeith Packard /* Unmask the interrupts that we always want on. */ 5807c463586SKeith Packard dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX; 5818ee1c3dbSMatthew Garrett 5827c463586SKeith Packard dev_priv->pipestat[0] = 0; 5837c463586SKeith Packard dev_priv->pipestat[1] = 0; 5847c463586SKeith Packard 5855ca58282SJesse Barnes if (I915_HAS_HOTPLUG(dev)) { 5865ca58282SJesse Barnes u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN); 5875ca58282SJesse Barnes 5885ca58282SJesse Barnes /* Leave other bits alone */ 5895ca58282SJesse Barnes hotplug_en |= HOTPLUG_EN_MASK; 5905ca58282SJesse Barnes I915_WRITE(PORT_HOTPLUG_EN, hotplug_en); 5915ca58282SJesse Barnes 5925ca58282SJesse Barnes dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS | 5935ca58282SJesse Barnes TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS | 5945ca58282SJesse Barnes SDVOB_HOTPLUG_INT_STATUS; 5955ca58282SJesse Barnes if (IS_G4X(dev)) { 5965ca58282SJesse Barnes dev_priv->hotplug_supported_mask |= 5975ca58282SJesse Barnes HDMIB_HOTPLUG_INT_STATUS | 5985ca58282SJesse Barnes HDMIC_HOTPLUG_INT_STATUS | 5995ca58282SJesse Barnes HDMID_HOTPLUG_INT_STATUS; 6005ca58282SJesse Barnes } 6015ca58282SJesse Barnes /* Enable in IER... */ 6025ca58282SJesse Barnes enable_mask |= I915_DISPLAY_PORT_INTERRUPT; 6035ca58282SJesse Barnes /* and unmask in IMR */ 6045ca58282SJesse Barnes i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT); 6055ca58282SJesse Barnes } 6065ca58282SJesse Barnes 6077c463586SKeith Packard /* Disable pipe interrupt enables, clear pending pipe status */ 6087c463586SKeith Packard I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff); 6097c463586SKeith Packard I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff); 6107c463586SKeith Packard /* Clear pending interrupt status */ 6117c463586SKeith Packard I915_WRITE(IIR, I915_READ(IIR)); 6127c463586SKeith Packard 6135ca58282SJesse Barnes I915_WRITE(IER, enable_mask); 6147c463586SKeith Packard I915_WRITE(IMR, dev_priv->irq_mask_reg); 615ed4cb414SEric Anholt (void) I915_READ(IER); 616ed4cb414SEric Anholt 6178ee1c3dbSMatthew Garrett opregion_enable_asle(dev); 618c0e09200SDave Airlie DRM_INIT_WAITQUEUE(&dev_priv->irq_queue); 6190a3e67a4SJesse Barnes 6200a3e67a4SJesse Barnes return 0; 621c0e09200SDave Airlie } 622c0e09200SDave Airlie 623c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev) 624c0e09200SDave Airlie { 625c0e09200SDave Airlie drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private; 626c0e09200SDave Airlie 627c0e09200SDave Airlie if (!dev_priv) 628c0e09200SDave Airlie return; 629c0e09200SDave Airlie 6300a3e67a4SJesse Barnes dev_priv->vblank_pipe = 0; 6310a3e67a4SJesse Barnes 6325ca58282SJesse Barnes if (I915_HAS_HOTPLUG(dev)) { 6335ca58282SJesse Barnes I915_WRITE(PORT_HOTPLUG_EN, 0); 6345ca58282SJesse Barnes I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT)); 6355ca58282SJesse Barnes } 6365ca58282SJesse Barnes 6370a3e67a4SJesse Barnes I915_WRITE(HWSTAM, 0xffffffff); 6387c463586SKeith Packard I915_WRITE(PIPEASTAT, 0); 6397c463586SKeith Packard I915_WRITE(PIPEBSTAT, 0); 6400a3e67a4SJesse Barnes I915_WRITE(IMR, 0xffffffff); 641ed4cb414SEric Anholt I915_WRITE(IER, 0x0); 642c0e09200SDave Airlie 6437c463586SKeith Packard I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff); 6447c463586SKeith Packard I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff); 6457c463586SKeith Packard I915_WRITE(IIR, I915_READ(IIR)); 646c0e09200SDave Airlie } 647