xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 9880b7a527ffbb52f65c2de0a8d4eea86e24775e)
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 
517c463586SKeith Packard /** These are all of the interrupts used by the driver */
527c463586SKeith Packard #define I915_INTERRUPT_ENABLE_MASK (I915_INTERRUPT_ENABLE_FIX | \
537c463586SKeith Packard 				    I915_INTERRUPT_ENABLE_VAR)
547c463586SKeith Packard 
5579e53945SJesse Barnes #define I915_PIPE_VBLANK_STATUS	(PIPE_START_VBLANK_INTERRUPT_STATUS |\
5679e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_STATUS)
5779e53945SJesse Barnes 
5879e53945SJesse Barnes #define I915_PIPE_VBLANK_ENABLE	(PIPE_START_VBLANK_INTERRUPT_ENABLE |\
5979e53945SJesse Barnes 				 PIPE_VBLANK_INTERRUPT_ENABLE)
6079e53945SJesse Barnes 
6179e53945SJesse Barnes #define DRM_I915_VBLANK_PIPE_ALL	(DRM_I915_VBLANK_PIPE_A | \
6279e53945SJesse Barnes 					 DRM_I915_VBLANK_PIPE_B)
6379e53945SJesse Barnes 
648ee1c3dbSMatthew Garrett void
65ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
66ed4cb414SEric Anholt {
67ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != 0) {
68ed4cb414SEric Anholt 		dev_priv->irq_mask_reg &= ~mask;
69ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
70ed4cb414SEric Anholt 		(void) I915_READ(IMR);
71ed4cb414SEric Anholt 	}
72ed4cb414SEric Anholt }
73ed4cb414SEric Anholt 
74ed4cb414SEric Anholt static inline void
75ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
76ed4cb414SEric Anholt {
77ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != mask) {
78ed4cb414SEric Anholt 		dev_priv->irq_mask_reg |= mask;
79ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
80ed4cb414SEric Anholt 		(void) I915_READ(IMR);
81ed4cb414SEric Anholt 	}
82ed4cb414SEric Anholt }
83ed4cb414SEric Anholt 
847c463586SKeith Packard static inline u32
857c463586SKeith Packard i915_pipestat(int pipe)
867c463586SKeith Packard {
877c463586SKeith Packard 	if (pipe == 0)
887c463586SKeith Packard 		return PIPEASTAT;
897c463586SKeith Packard 	if (pipe == 1)
907c463586SKeith Packard 		return PIPEBSTAT;
919c84ba4eSAndrew Morton 	BUG();
927c463586SKeith Packard }
937c463586SKeith Packard 
947c463586SKeith Packard void
957c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
967c463586SKeith Packard {
977c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
987c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
997c463586SKeith Packard 
1007c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
1017c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
1027c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
1037c463586SKeith Packard 		(void) I915_READ(reg);
1047c463586SKeith Packard 	}
1057c463586SKeith Packard }
1067c463586SKeith Packard 
1077c463586SKeith Packard void
1087c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
1097c463586SKeith Packard {
1107c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1117c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1127c463586SKeith Packard 
1137c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1147c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1157c463586SKeith Packard 		(void) I915_READ(reg);
1167c463586SKeith Packard 	}
1177c463586SKeith Packard }
1187c463586SKeith Packard 
119c0e09200SDave Airlie /**
1200a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1210a3e67a4SJesse Barnes  * @dev: DRM device
1220a3e67a4SJesse Barnes  * @pipe: pipe to check
1230a3e67a4SJesse Barnes  *
1240a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1250a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1260a3e67a4SJesse Barnes  * before reading such registers if unsure.
1270a3e67a4SJesse Barnes  */
1280a3e67a4SJesse Barnes static int
1290a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1300a3e67a4SJesse Barnes {
1310a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1320a3e67a4SJesse Barnes 	unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
1330a3e67a4SJesse Barnes 
1340a3e67a4SJesse Barnes 	if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
1350a3e67a4SJesse Barnes 		return 1;
1360a3e67a4SJesse Barnes 
1370a3e67a4SJesse Barnes 	return 0;
1380a3e67a4SJesse Barnes }
1390a3e67a4SJesse Barnes 
14042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
14142f52ef8SKeith Packard  * we use as a pipe index
14242f52ef8SKeith Packard  */
14342f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1440a3e67a4SJesse Barnes {
1450a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1460a3e67a4SJesse Barnes 	unsigned long high_frame;
1470a3e67a4SJesse Barnes 	unsigned long low_frame;
1480a3e67a4SJesse Barnes 	u32 high1, high2, low, count;
1490a3e67a4SJesse Barnes 
1500a3e67a4SJesse Barnes 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
1510a3e67a4SJesse Barnes 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
1520a3e67a4SJesse Barnes 
1530a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
1540a3e67a4SJesse Barnes 		DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
1550a3e67a4SJesse Barnes 		return 0;
1560a3e67a4SJesse Barnes 	}
1570a3e67a4SJesse Barnes 
1580a3e67a4SJesse Barnes 	/*
1590a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1600a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1610a3e67a4SJesse Barnes 	 * register.
1620a3e67a4SJesse Barnes 	 */
1630a3e67a4SJesse Barnes 	do {
1640a3e67a4SJesse Barnes 		high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
1650a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
1660a3e67a4SJesse Barnes 		low =  ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
1670a3e67a4SJesse Barnes 			PIPE_FRAME_LOW_SHIFT);
1680a3e67a4SJesse Barnes 		high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
1690a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
1700a3e67a4SJesse Barnes 	} while (high1 != high2);
1710a3e67a4SJesse Barnes 
1720a3e67a4SJesse Barnes 	count = (high1 << 8) | low;
1730a3e67a4SJesse Barnes 
1740a3e67a4SJesse Barnes 	return count;
1750a3e67a4SJesse Barnes }
1760a3e67a4SJesse Barnes 
177*9880b7a5SJesse Barnes u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
178*9880b7a5SJesse Barnes {
179*9880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
180*9880b7a5SJesse Barnes 	int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
181*9880b7a5SJesse Barnes 
182*9880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
183*9880b7a5SJesse Barnes 		DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
184*9880b7a5SJesse Barnes 		return 0;
185*9880b7a5SJesse Barnes 	}
186*9880b7a5SJesse Barnes 
187*9880b7a5SJesse Barnes 	return I915_READ(reg);
188*9880b7a5SJesse Barnes }
189*9880b7a5SJesse Barnes 
190c0e09200SDave Airlie irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
191c0e09200SDave Airlie {
192c0e09200SDave Airlie 	struct drm_device *dev = (struct drm_device *) arg;
193c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1947c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv;
195cdfbc41fSEric Anholt 	u32 iir, new_iir;
196cdfbc41fSEric Anholt 	u32 pipea_stats, pipeb_stats;
19705eff845SKeith Packard 	u32 vblank_status;
19805eff845SKeith Packard 	u32 vblank_enable;
1990a3e67a4SJesse Barnes 	int vblank = 0;
2007c463586SKeith Packard 	unsigned long irqflags;
20105eff845SKeith Packard 	int irq_received;
20205eff845SKeith Packard 	int ret = IRQ_NONE;
203c0e09200SDave Airlie 
204630681d9SEric Anholt 	atomic_inc(&dev_priv->irq_received);
205630681d9SEric Anholt 
206ed4cb414SEric Anholt 	iir = I915_READ(IIR);
207c0e09200SDave Airlie 
20805eff845SKeith Packard 	if (IS_I965G(dev)) {
20905eff845SKeith Packard 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
21005eff845SKeith Packard 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
21105eff845SKeith Packard 	} else {
21205eff845SKeith Packard 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
21305eff845SKeith Packard 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
21405eff845SKeith Packard 	}
215c0e09200SDave Airlie 
21605eff845SKeith Packard 	for (;;) {
21705eff845SKeith Packard 		irq_received = iir != 0;
21805eff845SKeith Packard 
21905eff845SKeith Packard 		/* Can't rely on pipestat interrupt bit in iir as it might
22005eff845SKeith Packard 		 * have been cleared after the pipestat interrupt was received.
22105eff845SKeith Packard 		 * It doesn't set the bit in iir again, but it still produces
22205eff845SKeith Packard 		 * interrupts (for non-MSI).
22305eff845SKeith Packard 		 */
22405eff845SKeith Packard 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
22505eff845SKeith Packard 		pipea_stats = I915_READ(PIPEASTAT);
22605eff845SKeith Packard 		pipeb_stats = I915_READ(PIPEBSTAT);
22779e53945SJesse Barnes 
2280a3e67a4SJesse Barnes 		/*
2297c463586SKeith Packard 		 * Clear the PIPE(A|B)STAT regs before the IIR
2300a3e67a4SJesse Barnes 		 */
23105eff845SKeith Packard 		if (pipea_stats & 0x8000ffff) {
2328ee1c3dbSMatthew Garrett 			I915_WRITE(PIPEASTAT, pipea_stats);
23305eff845SKeith Packard 			irq_received = 1;
2340a3e67a4SJesse Barnes 		}
2357c463586SKeith Packard 
23605eff845SKeith Packard 		if (pipeb_stats & 0x8000ffff) {
2370a3e67a4SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
23805eff845SKeith Packard 			irq_received = 1;
239c0e09200SDave Airlie 		}
24005eff845SKeith Packard 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
24105eff845SKeith Packard 
24205eff845SKeith Packard 		if (!irq_received)
24305eff845SKeith Packard 			break;
24405eff845SKeith Packard 
24505eff845SKeith Packard 		ret = IRQ_HANDLED;
246c0e09200SDave Airlie 
247673a394bSEric Anholt 		I915_WRITE(IIR, iir);
248cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
2497c463586SKeith Packard 
2507c1c2871SDave Airlie 		if (dev->primary->master) {
2517c1c2871SDave Airlie 			master_priv = dev->primary->master->driver_priv;
2527c1c2871SDave Airlie 			if (master_priv->sarea_priv)
2537c1c2871SDave Airlie 				master_priv->sarea_priv->last_dispatch =
254c99b058fSKristian Høgsberg 					READ_BREADCRUMB(dev_priv);
2557c1c2871SDave Airlie 		}
2560a3e67a4SJesse Barnes 
257673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
258673a394bSEric Anholt 			dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
259673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
260673a394bSEric Anholt 		}
261673a394bSEric Anholt 
26205eff845SKeith Packard 		if (pipea_stats & vblank_status) {
2637c463586SKeith Packard 			vblank++;
2647c463586SKeith Packard 			drm_handle_vblank(dev, 0);
2657c463586SKeith Packard 		}
2667c463586SKeith Packard 
26705eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
2687c463586SKeith Packard 			vblank++;
2697c463586SKeith Packard 			drm_handle_vblank(dev, 1);
2707c463586SKeith Packard 		}
2717c463586SKeith Packard 
2727c463586SKeith Packard 		if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
2737c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
274673a394bSEric Anholt 			opregion_asle_intr(dev);
2750a3e67a4SJesse Barnes 
276cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
277cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
278cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
279cdfbc41fSEric Anholt 		 * we would never get another interrupt.
280cdfbc41fSEric Anholt 		 *
281cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
282cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
283cdfbc41fSEric Anholt 		 * another one.
284cdfbc41fSEric Anholt 		 *
285cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
286cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
287cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
288cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
289cdfbc41fSEric Anholt 		 * stray interrupts.
290cdfbc41fSEric Anholt 		 */
291cdfbc41fSEric Anholt 		iir = new_iir;
29205eff845SKeith Packard 	}
293cdfbc41fSEric Anholt 
29405eff845SKeith Packard 	return ret;
295c0e09200SDave Airlie }
296c0e09200SDave Airlie 
297c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
298c0e09200SDave Airlie {
299c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
3007c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
301c0e09200SDave Airlie 	RING_LOCALS;
302c0e09200SDave Airlie 
303c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
304c0e09200SDave Airlie 
305c0e09200SDave Airlie 	DRM_DEBUG("\n");
306c0e09200SDave Airlie 
307c99b058fSKristian Høgsberg 	dev_priv->counter++;
308c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
309c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
3107c1c2871SDave Airlie 	if (master_priv->sarea_priv)
3117c1c2871SDave Airlie 		master_priv->sarea_priv->last_enqueue = dev_priv->counter;
312c0e09200SDave Airlie 
3130baf823aSKeith Packard 	BEGIN_LP_RING(4);
314585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
3150baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
316c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
317585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
318c0e09200SDave Airlie 	ADVANCE_LP_RING();
319c0e09200SDave Airlie 
320c0e09200SDave Airlie 	return dev_priv->counter;
321c0e09200SDave Airlie }
322c0e09200SDave Airlie 
323673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
324ed4cb414SEric Anholt {
325ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
326e9d21d7fSKeith Packard 	unsigned long irqflags;
327ed4cb414SEric Anholt 
328e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
329ed4cb414SEric Anholt 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
330ed4cb414SEric Anholt 		i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
331e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
332ed4cb414SEric Anholt }
333ed4cb414SEric Anholt 
3340a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
335ed4cb414SEric Anholt {
336ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
337e9d21d7fSKeith Packard 	unsigned long irqflags;
338ed4cb414SEric Anholt 
339e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
340ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
341ed4cb414SEric Anholt 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
342ed4cb414SEric Anholt 		i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
343e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
344ed4cb414SEric Anholt }
345ed4cb414SEric Anholt 
346c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
347c0e09200SDave Airlie {
348c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3497c1c2871SDave Airlie 	struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
350c0e09200SDave Airlie 	int ret = 0;
351c0e09200SDave Airlie 
352c0e09200SDave Airlie 	DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
353c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
354c0e09200SDave Airlie 
355ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
3567c1c2871SDave Airlie 		if (master_priv->sarea_priv)
3577c1c2871SDave Airlie 			master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
358c0e09200SDave Airlie 		return 0;
359ed4cb414SEric Anholt 	}
360c0e09200SDave Airlie 
3617c1c2871SDave Airlie 	if (master_priv->sarea_priv)
3627c1c2871SDave Airlie 		master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
363c0e09200SDave Airlie 
364ed4cb414SEric Anholt 	i915_user_irq_get(dev);
365c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
366c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
367ed4cb414SEric Anholt 	i915_user_irq_put(dev);
368c0e09200SDave Airlie 
369c0e09200SDave Airlie 	if (ret == -EBUSY) {
370c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
371c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
372c0e09200SDave Airlie 	}
373c0e09200SDave Airlie 
374c0e09200SDave Airlie 	return ret;
375c0e09200SDave Airlie }
376c0e09200SDave Airlie 
377c0e09200SDave Airlie /* Needs the lock as it touches the ring.
378c0e09200SDave Airlie  */
379c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
380c0e09200SDave Airlie 			 struct drm_file *file_priv)
381c0e09200SDave Airlie {
382c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
383c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
384c0e09200SDave Airlie 	int result;
385c0e09200SDave Airlie 
386546b0974SEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
387c0e09200SDave Airlie 
388c0e09200SDave Airlie 	if (!dev_priv) {
389c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
390c0e09200SDave Airlie 		return -EINVAL;
391c0e09200SDave Airlie 	}
392546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
393c0e09200SDave Airlie 	result = i915_emit_irq(dev);
394546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
395c0e09200SDave Airlie 
396c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
397c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
398c0e09200SDave Airlie 		return -EFAULT;
399c0e09200SDave Airlie 	}
400c0e09200SDave Airlie 
401c0e09200SDave Airlie 	return 0;
402c0e09200SDave Airlie }
403c0e09200SDave Airlie 
404c0e09200SDave Airlie /* Doesn't need the hardware lock.
405c0e09200SDave Airlie  */
406c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
407c0e09200SDave Airlie 			 struct drm_file *file_priv)
408c0e09200SDave Airlie {
409c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
410c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
411c0e09200SDave Airlie 
412c0e09200SDave Airlie 	if (!dev_priv) {
413c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
414c0e09200SDave Airlie 		return -EINVAL;
415c0e09200SDave Airlie 	}
416c0e09200SDave Airlie 
417c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
418c0e09200SDave Airlie }
419c0e09200SDave Airlie 
42042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
42142f52ef8SKeith Packard  * we use as a pipe index
42242f52ef8SKeith Packard  */
42342f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
4240a3e67a4SJesse Barnes {
4250a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
426e9d21d7fSKeith Packard 	unsigned long irqflags;
42771e0ffa5SJesse Barnes 	int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
42871e0ffa5SJesse Barnes 	u32 pipeconf;
42971e0ffa5SJesse Barnes 
43071e0ffa5SJesse Barnes 	pipeconf = I915_READ(pipeconf_reg);
43171e0ffa5SJesse Barnes 	if (!(pipeconf & PIPEACONF_ENABLE))
43271e0ffa5SJesse Barnes 		return -EINVAL;
4330a3e67a4SJesse Barnes 
434e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
4350a3e67a4SJesse Barnes 	if (IS_I965G(dev))
4367c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
4377c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
4380a3e67a4SJesse Barnes 	else
4397c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
4407c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
441e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
4420a3e67a4SJesse Barnes 	return 0;
4430a3e67a4SJesse Barnes }
4440a3e67a4SJesse Barnes 
44542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
44642f52ef8SKeith Packard  * we use as a pipe index
44742f52ef8SKeith Packard  */
44842f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
4490a3e67a4SJesse Barnes {
4500a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
451e9d21d7fSKeith Packard 	unsigned long irqflags;
4520a3e67a4SJesse Barnes 
453e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
4547c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
4557c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
4567c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
457e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
4580a3e67a4SJesse Barnes }
4590a3e67a4SJesse Barnes 
46079e53945SJesse Barnes void i915_enable_interrupt (struct drm_device *dev)
46179e53945SJesse Barnes {
46279e53945SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
46379e53945SJesse Barnes 	opregion_enable_asle(dev);
46479e53945SJesse Barnes 	dev_priv->irq_enabled = 1;
46579e53945SJesse Barnes }
46679e53945SJesse Barnes 
46779e53945SJesse Barnes 
468c0e09200SDave Airlie /* Set the vblank monitor pipe
469c0e09200SDave Airlie  */
470c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
471c0e09200SDave Airlie 			 struct drm_file *file_priv)
472c0e09200SDave Airlie {
473c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
474c0e09200SDave Airlie 
475c0e09200SDave Airlie 	if (!dev_priv) {
476c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
477c0e09200SDave Airlie 		return -EINVAL;
478c0e09200SDave Airlie 	}
479c0e09200SDave Airlie 
480c0e09200SDave Airlie 	return 0;
481c0e09200SDave Airlie }
482c0e09200SDave Airlie 
483c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
484c0e09200SDave Airlie 			 struct drm_file *file_priv)
485c0e09200SDave Airlie {
486c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
487c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
488c0e09200SDave Airlie 
489c0e09200SDave Airlie 	if (!dev_priv) {
490c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
491c0e09200SDave Airlie 		return -EINVAL;
492c0e09200SDave Airlie 	}
493c0e09200SDave Airlie 
4940a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
495c0e09200SDave Airlie 
496c0e09200SDave Airlie 	return 0;
497c0e09200SDave Airlie }
498c0e09200SDave Airlie 
499c0e09200SDave Airlie /**
500c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
501c0e09200SDave Airlie  */
502c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
503c0e09200SDave Airlie 		     struct drm_file *file_priv)
504c0e09200SDave Airlie {
505bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
506bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
507bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
508bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
509bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
510bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
511bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
512bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
513bd95e0a4SEric Anholt 	 *
514bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
515bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
516bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
517bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
5180a3e67a4SJesse Barnes 	 */
519c0e09200SDave Airlie 	return -EINVAL;
520c0e09200SDave Airlie }
521c0e09200SDave Airlie 
522c0e09200SDave Airlie /* drm_dma.h hooks
523c0e09200SDave Airlie */
524c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
525c0e09200SDave Airlie {
526c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
527c0e09200SDave Airlie 
52879e53945SJesse Barnes 	atomic_set(&dev_priv->irq_received, 0);
52979e53945SJesse Barnes 
5300a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
5317c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
5327c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
5330a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
534ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
5357c463586SKeith Packard 	(void) I915_READ(IER);
536c0e09200SDave Airlie }
537c0e09200SDave Airlie 
5380a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
539c0e09200SDave Airlie {
540c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5410a3e67a4SJesse Barnes 
5420a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
543ed4cb414SEric Anholt 
5440a3e67a4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
5450a3e67a4SJesse Barnes 
5467c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
5477c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
5488ee1c3dbSMatthew Garrett 
5497c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
5507c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
5517c463586SKeith Packard 
5527c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
5537c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
5547c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
5557c463586SKeith Packard 	/* Clear pending interrupt status */
5567c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
5577c463586SKeith Packard 
558ed4cb414SEric Anholt 	I915_WRITE(IER, I915_INTERRUPT_ENABLE_MASK);
5597c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
560ed4cb414SEric Anholt 	(void) I915_READ(IER);
561ed4cb414SEric Anholt 
5628ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
563c0e09200SDave Airlie 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
5640a3e67a4SJesse Barnes 
5650a3e67a4SJesse Barnes 	return 0;
566c0e09200SDave Airlie }
567c0e09200SDave Airlie 
568c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
569c0e09200SDave Airlie {
570c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
571c0e09200SDave Airlie 
572c0e09200SDave Airlie 	if (!dev_priv)
573c0e09200SDave Airlie 		return;
574c0e09200SDave Airlie 
5750a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
5760a3e67a4SJesse Barnes 
5770a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
5787c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
5797c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
5800a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
581ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
582c0e09200SDave Airlie 
5837c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
5847c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
5857c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
586c0e09200SDave Airlie }
587