xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 05eff845a28499762075d3a72e238a31f4d2407c)
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 
367c463586SKeith Packard /**
377c463586SKeith Packard  * Interrupts that are always left unmasked.
387c463586SKeith Packard  *
397c463586SKeith Packard  * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
407c463586SKeith Packard  * we leave them always unmasked in IMR and then control enabling them through
417c463586SKeith Packard  * PIPESTAT alone.
427c463586SKeith Packard  */
437c463586SKeith Packard #define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT | \
440a3e67a4SJesse Barnes 				   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |  \
458ee1c3dbSMatthew Garrett 				   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
46ed4cb414SEric Anholt 
477c463586SKeith Packard /** Interrupts that we mask and unmask at runtime. */
487c463586SKeith Packard #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
497c463586SKeith Packard 
507c463586SKeith Packard /** These are all of the interrupts used by the driver */
517c463586SKeith Packard #define I915_INTERRUPT_ENABLE_MASK (I915_INTERRUPT_ENABLE_FIX | \
527c463586SKeith Packard 				    I915_INTERRUPT_ENABLE_VAR)
537c463586SKeith Packard 
548ee1c3dbSMatthew Garrett void
55ed4cb414SEric Anholt i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
56ed4cb414SEric Anholt {
57ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != 0) {
58ed4cb414SEric Anholt 		dev_priv->irq_mask_reg &= ~mask;
59ed4cb414SEric Anholt 		I915_WRITE(IMR, dev_priv->irq_mask_reg);
60ed4cb414SEric Anholt 		(void) I915_READ(IMR);
61ed4cb414SEric Anholt 	}
62ed4cb414SEric Anholt }
63ed4cb414SEric Anholt 
64ed4cb414SEric Anholt static inline void
65ed4cb414SEric Anholt i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
66ed4cb414SEric Anholt {
67ed4cb414SEric Anholt 	if ((dev_priv->irq_mask_reg & mask) != mask) {
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 
747c463586SKeith Packard static inline u32
757c463586SKeith Packard i915_pipestat(int pipe)
767c463586SKeith Packard {
777c463586SKeith Packard 	if (pipe == 0)
787c463586SKeith Packard 		return PIPEASTAT;
797c463586SKeith Packard 	if (pipe == 1)
807c463586SKeith Packard 		return PIPEBSTAT;
817c463586SKeith Packard 	BUG_ON(1);
827c463586SKeith Packard }
837c463586SKeith Packard 
847c463586SKeith Packard void
857c463586SKeith Packard i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
867c463586SKeith Packard {
877c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != mask) {
887c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
897c463586SKeith Packard 
907c463586SKeith Packard 		dev_priv->pipestat[pipe] |= mask;
917c463586SKeith Packard 		/* Enable the interrupt, clear any pending status */
927c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
937c463586SKeith Packard 		(void) I915_READ(reg);
947c463586SKeith Packard 	}
957c463586SKeith Packard }
967c463586SKeith Packard 
977c463586SKeith Packard void
987c463586SKeith Packard i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
997c463586SKeith Packard {
1007c463586SKeith Packard 	if ((dev_priv->pipestat[pipe] & mask) != 0) {
1017c463586SKeith Packard 		u32 reg = i915_pipestat(pipe);
1027c463586SKeith Packard 
1037c463586SKeith Packard 		dev_priv->pipestat[pipe] &= ~mask;
1047c463586SKeith Packard 		I915_WRITE(reg, dev_priv->pipestat[pipe]);
1057c463586SKeith Packard 		(void) I915_READ(reg);
1067c463586SKeith Packard 	}
1077c463586SKeith Packard }
1087c463586SKeith Packard 
109c0e09200SDave Airlie /**
1100a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
1110a3e67a4SJesse Barnes  * @dev: DRM device
1120a3e67a4SJesse Barnes  * @pipe: pipe to check
1130a3e67a4SJesse Barnes  *
1140a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
1150a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
1160a3e67a4SJesse Barnes  * before reading such registers if unsure.
1170a3e67a4SJesse Barnes  */
1180a3e67a4SJesse Barnes static int
1190a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
1200a3e67a4SJesse Barnes {
1210a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1220a3e67a4SJesse Barnes 	unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
1230a3e67a4SJesse Barnes 
1240a3e67a4SJesse Barnes 	if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
1250a3e67a4SJesse Barnes 		return 1;
1260a3e67a4SJesse Barnes 
1270a3e67a4SJesse Barnes 	return 0;
1280a3e67a4SJesse Barnes }
1290a3e67a4SJesse Barnes 
13042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
13142f52ef8SKeith Packard  * we use as a pipe index
13242f52ef8SKeith Packard  */
13342f52ef8SKeith Packard u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
1340a3e67a4SJesse Barnes {
1350a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1360a3e67a4SJesse Barnes 	unsigned long high_frame;
1370a3e67a4SJesse Barnes 	unsigned long low_frame;
1380a3e67a4SJesse Barnes 	u32 high1, high2, low, count;
1390a3e67a4SJesse Barnes 
1400a3e67a4SJesse Barnes 	high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
1410a3e67a4SJesse Barnes 	low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
1420a3e67a4SJesse Barnes 
1430a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
1440a3e67a4SJesse Barnes 		DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
1450a3e67a4SJesse Barnes 		return 0;
1460a3e67a4SJesse Barnes 	}
1470a3e67a4SJesse Barnes 
1480a3e67a4SJesse Barnes 	/*
1490a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
1500a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
1510a3e67a4SJesse Barnes 	 * register.
1520a3e67a4SJesse Barnes 	 */
1530a3e67a4SJesse Barnes 	do {
1540a3e67a4SJesse Barnes 		high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
1550a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
1560a3e67a4SJesse Barnes 		low =  ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
1570a3e67a4SJesse Barnes 			PIPE_FRAME_LOW_SHIFT);
1580a3e67a4SJesse Barnes 		high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
1590a3e67a4SJesse Barnes 			 PIPE_FRAME_HIGH_SHIFT);
1600a3e67a4SJesse Barnes 	} while (high1 != high2);
1610a3e67a4SJesse Barnes 
1620a3e67a4SJesse Barnes 	count = (high1 << 8) | low;
1630a3e67a4SJesse Barnes 
1640a3e67a4SJesse Barnes 	return count;
1650a3e67a4SJesse Barnes }
1660a3e67a4SJesse Barnes 
167c0e09200SDave Airlie irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
168c0e09200SDave Airlie {
169c0e09200SDave Airlie 	struct drm_device *dev = (struct drm_device *) arg;
170c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
171cdfbc41fSEric Anholt 	u32 iir, new_iir;
172cdfbc41fSEric Anholt 	u32 pipea_stats, pipeb_stats;
173*05eff845SKeith Packard 	u32 vblank_status;
174*05eff845SKeith Packard 	u32 vblank_enable;
1750a3e67a4SJesse Barnes 	int vblank = 0;
1767c463586SKeith Packard 	unsigned long irqflags;
177*05eff845SKeith Packard 	int irq_received;
178*05eff845SKeith Packard 	int ret = IRQ_NONE;
179c0e09200SDave Airlie 
180630681d9SEric Anholt 	atomic_inc(&dev_priv->irq_received);
181630681d9SEric Anholt 
182ed4cb414SEric Anholt 	iir = I915_READ(IIR);
183c0e09200SDave Airlie 
184*05eff845SKeith Packard 	if (IS_I965G(dev)) {
185*05eff845SKeith Packard 		vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
186*05eff845SKeith Packard 		vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
187*05eff845SKeith Packard 	} else {
188*05eff845SKeith Packard 		vblank_status = I915_VBLANK_INTERRUPT_STATUS;
189*05eff845SKeith Packard 		vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
190*05eff845SKeith Packard 	}
191c0e09200SDave Airlie 
192*05eff845SKeith Packard 	for (;;) {
193*05eff845SKeith Packard 		irq_received = iir != 0;
194*05eff845SKeith Packard 
195*05eff845SKeith Packard 		/* Can't rely on pipestat interrupt bit in iir as it might
196*05eff845SKeith Packard 		 * have been cleared after the pipestat interrupt was received.
197*05eff845SKeith Packard 		 * It doesn't set the bit in iir again, but it still produces
198*05eff845SKeith Packard 		 * interrupts (for non-MSI).
199*05eff845SKeith Packard 		 */
200*05eff845SKeith Packard 		spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
201*05eff845SKeith Packard 		pipea_stats = I915_READ(PIPEASTAT);
202*05eff845SKeith Packard 		pipeb_stats = I915_READ(PIPEBSTAT);
2030a3e67a4SJesse Barnes 		/*
2047c463586SKeith Packard 		 * Clear the PIPE(A|B)STAT regs before the IIR
2050a3e67a4SJesse Barnes 		 */
206*05eff845SKeith Packard 		if (pipea_stats & 0x8000ffff) {
2078ee1c3dbSMatthew Garrett 			I915_WRITE(PIPEASTAT, pipea_stats);
208*05eff845SKeith Packard 			irq_received = 1;
2090a3e67a4SJesse Barnes 		}
2107c463586SKeith Packard 
211*05eff845SKeith Packard 		if (pipeb_stats & 0x8000ffff) {
2120a3e67a4SJesse Barnes 			I915_WRITE(PIPEBSTAT, pipeb_stats);
213*05eff845SKeith Packard 			irq_received = 1;
214c0e09200SDave Airlie 		}
215*05eff845SKeith Packard 		spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
216*05eff845SKeith Packard 
217*05eff845SKeith Packard 		if (!irq_received)
218*05eff845SKeith Packard 			break;
219*05eff845SKeith Packard 
220*05eff845SKeith Packard 		ret = IRQ_HANDLED;
221c0e09200SDave Airlie 
222673a394bSEric Anholt 		I915_WRITE(IIR, iir);
223cdfbc41fSEric Anholt 		new_iir = I915_READ(IIR); /* Flush posted writes */
2247c463586SKeith Packard 
225c99b058fSKristian Høgsberg 		if (dev_priv->sarea_priv)
226c99b058fSKristian Høgsberg 			dev_priv->sarea_priv->last_dispatch =
227c99b058fSKristian Høgsberg 				READ_BREADCRUMB(dev_priv);
2280a3e67a4SJesse Barnes 
229673a394bSEric Anholt 		if (iir & I915_USER_INTERRUPT) {
230673a394bSEric Anholt 			dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
231673a394bSEric Anholt 			DRM_WAKEUP(&dev_priv->irq_queue);
232673a394bSEric Anholt 		}
233673a394bSEric Anholt 
234*05eff845SKeith Packard 		if (pipea_stats & vblank_status) {
2357c463586SKeith Packard 			vblank++;
2367c463586SKeith Packard 			drm_handle_vblank(dev, 0);
2377c463586SKeith Packard 		}
2387c463586SKeith Packard 
239*05eff845SKeith Packard 		if (pipeb_stats & vblank_status) {
2407c463586SKeith Packard 			vblank++;
2417c463586SKeith Packard 			drm_handle_vblank(dev, 1);
2427c463586SKeith Packard 		}
2437c463586SKeith Packard 
2447c463586SKeith Packard 		if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
2457c463586SKeith Packard 		    (iir & I915_ASLE_INTERRUPT))
246673a394bSEric Anholt 			opregion_asle_intr(dev);
2470a3e67a4SJesse Barnes 
248cdfbc41fSEric Anholt 		/* With MSI, interrupts are only generated when iir
249cdfbc41fSEric Anholt 		 * transitions from zero to nonzero.  If another bit got
250cdfbc41fSEric Anholt 		 * set while we were handling the existing iir bits, then
251cdfbc41fSEric Anholt 		 * we would never get another interrupt.
252cdfbc41fSEric Anholt 		 *
253cdfbc41fSEric Anholt 		 * This is fine on non-MSI as well, as if we hit this path
254cdfbc41fSEric Anholt 		 * we avoid exiting the interrupt handler only to generate
255cdfbc41fSEric Anholt 		 * another one.
256cdfbc41fSEric Anholt 		 *
257cdfbc41fSEric Anholt 		 * Note that for MSI this could cause a stray interrupt report
258cdfbc41fSEric Anholt 		 * if an interrupt landed in the time between writing IIR and
259cdfbc41fSEric Anholt 		 * the posting read.  This should be rare enough to never
260cdfbc41fSEric Anholt 		 * trigger the 99% of 100,000 interrupts test for disabling
261cdfbc41fSEric Anholt 		 * stray interrupts.
262cdfbc41fSEric Anholt 		 */
263cdfbc41fSEric Anholt 		iir = new_iir;
264*05eff845SKeith Packard 	}
265cdfbc41fSEric Anholt 
266*05eff845SKeith Packard 	return ret;
267c0e09200SDave Airlie }
268c0e09200SDave Airlie 
269c0e09200SDave Airlie static int i915_emit_irq(struct drm_device * dev)
270c0e09200SDave Airlie {
271c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
272c0e09200SDave Airlie 	RING_LOCALS;
273c0e09200SDave Airlie 
274c0e09200SDave Airlie 	i915_kernel_lost_context(dev);
275c0e09200SDave Airlie 
276c0e09200SDave Airlie 	DRM_DEBUG("\n");
277c0e09200SDave Airlie 
278c99b058fSKristian Høgsberg 	dev_priv->counter++;
279c0e09200SDave Airlie 	if (dev_priv->counter > 0x7FFFFFFFUL)
280c99b058fSKristian Høgsberg 		dev_priv->counter = 1;
281c99b058fSKristian Høgsberg 	if (dev_priv->sarea_priv)
282c99b058fSKristian Høgsberg 		dev_priv->sarea_priv->last_enqueue = dev_priv->counter;
283c0e09200SDave Airlie 
2840baf823aSKeith Packard 	BEGIN_LP_RING(4);
285585fb111SJesse Barnes 	OUT_RING(MI_STORE_DWORD_INDEX);
2860baf823aSKeith Packard 	OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
287c0e09200SDave Airlie 	OUT_RING(dev_priv->counter);
288585fb111SJesse Barnes 	OUT_RING(MI_USER_INTERRUPT);
289c0e09200SDave Airlie 	ADVANCE_LP_RING();
290c0e09200SDave Airlie 
291c0e09200SDave Airlie 	return dev_priv->counter;
292c0e09200SDave Airlie }
293c0e09200SDave Airlie 
294673a394bSEric Anholt void i915_user_irq_get(struct drm_device *dev)
295ed4cb414SEric Anholt {
296ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
297e9d21d7fSKeith Packard 	unsigned long irqflags;
298ed4cb414SEric Anholt 
299e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
300ed4cb414SEric Anholt 	if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
301ed4cb414SEric Anholt 		i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
302e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
303ed4cb414SEric Anholt }
304ed4cb414SEric Anholt 
3050a3e67a4SJesse Barnes void i915_user_irq_put(struct drm_device *dev)
306ed4cb414SEric Anholt {
307ed4cb414SEric Anholt 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
308e9d21d7fSKeith Packard 	unsigned long irqflags;
309ed4cb414SEric Anholt 
310e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
311ed4cb414SEric Anholt 	BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
312ed4cb414SEric Anholt 	if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
313ed4cb414SEric Anholt 		i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
314e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
315ed4cb414SEric Anholt }
316ed4cb414SEric Anholt 
317c0e09200SDave Airlie static int i915_wait_irq(struct drm_device * dev, int irq_nr)
318c0e09200SDave Airlie {
319c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
320c0e09200SDave Airlie 	int ret = 0;
321c0e09200SDave Airlie 
322c0e09200SDave Airlie 	DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
323c0e09200SDave Airlie 		  READ_BREADCRUMB(dev_priv));
324c0e09200SDave Airlie 
325ed4cb414SEric Anholt 	if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
326c99b058fSKristian Høgsberg 		if (dev_priv->sarea_priv) {
327c99b058fSKristian Høgsberg 			dev_priv->sarea_priv->last_dispatch =
328c99b058fSKristian Høgsberg 				READ_BREADCRUMB(dev_priv);
329c99b058fSKristian Høgsberg 		}
330c0e09200SDave Airlie 		return 0;
331ed4cb414SEric Anholt 	}
332c0e09200SDave Airlie 
333c99b058fSKristian Høgsberg 	if (dev_priv->sarea_priv)
334c0e09200SDave Airlie 		dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
335c0e09200SDave Airlie 
336ed4cb414SEric Anholt 	i915_user_irq_get(dev);
337c0e09200SDave Airlie 	DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
338c0e09200SDave Airlie 		    READ_BREADCRUMB(dev_priv) >= irq_nr);
339ed4cb414SEric Anholt 	i915_user_irq_put(dev);
340c0e09200SDave Airlie 
341c0e09200SDave Airlie 	if (ret == -EBUSY) {
342c0e09200SDave Airlie 		DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
343c0e09200SDave Airlie 			  READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
344c0e09200SDave Airlie 	}
345c0e09200SDave Airlie 
346c99b058fSKristian Høgsberg 	if (dev_priv->sarea_priv)
347c99b058fSKristian Høgsberg 		dev_priv->sarea_priv->last_dispatch =
348c99b058fSKristian Høgsberg 			READ_BREADCRUMB(dev_priv);
349c0e09200SDave Airlie 
350c0e09200SDave Airlie 	return ret;
351c0e09200SDave Airlie }
352c0e09200SDave Airlie 
353c0e09200SDave Airlie /* Needs the lock as it touches the ring.
354c0e09200SDave Airlie  */
355c0e09200SDave Airlie int i915_irq_emit(struct drm_device *dev, void *data,
356c0e09200SDave Airlie 			 struct drm_file *file_priv)
357c0e09200SDave Airlie {
358c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
359c0e09200SDave Airlie 	drm_i915_irq_emit_t *emit = data;
360c0e09200SDave Airlie 	int result;
361c0e09200SDave Airlie 
362546b0974SEric Anholt 	RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
363c0e09200SDave Airlie 
364c0e09200SDave Airlie 	if (!dev_priv) {
365c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
366c0e09200SDave Airlie 		return -EINVAL;
367c0e09200SDave Airlie 	}
368546b0974SEric Anholt 	mutex_lock(&dev->struct_mutex);
369c0e09200SDave Airlie 	result = i915_emit_irq(dev);
370546b0974SEric Anholt 	mutex_unlock(&dev->struct_mutex);
371c0e09200SDave Airlie 
372c0e09200SDave Airlie 	if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
373c0e09200SDave Airlie 		DRM_ERROR("copy_to_user\n");
374c0e09200SDave Airlie 		return -EFAULT;
375c0e09200SDave Airlie 	}
376c0e09200SDave Airlie 
377c0e09200SDave Airlie 	return 0;
378c0e09200SDave Airlie }
379c0e09200SDave Airlie 
380c0e09200SDave Airlie /* Doesn't need the hardware lock.
381c0e09200SDave Airlie  */
382c0e09200SDave Airlie int i915_irq_wait(struct drm_device *dev, void *data,
383c0e09200SDave Airlie 			 struct drm_file *file_priv)
384c0e09200SDave Airlie {
385c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
386c0e09200SDave Airlie 	drm_i915_irq_wait_t *irqwait = data;
387c0e09200SDave Airlie 
388c0e09200SDave Airlie 	if (!dev_priv) {
389c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
390c0e09200SDave Airlie 		return -EINVAL;
391c0e09200SDave Airlie 	}
392c0e09200SDave Airlie 
393c0e09200SDave Airlie 	return i915_wait_irq(dev, irqwait->irq_seq);
394c0e09200SDave Airlie }
395c0e09200SDave Airlie 
39642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
39742f52ef8SKeith Packard  * we use as a pipe index
39842f52ef8SKeith Packard  */
39942f52ef8SKeith Packard int i915_enable_vblank(struct drm_device *dev, int pipe)
4000a3e67a4SJesse Barnes {
4010a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
402e9d21d7fSKeith Packard 	unsigned long irqflags;
4030a3e67a4SJesse Barnes 
404e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
4050a3e67a4SJesse Barnes 	if (IS_I965G(dev))
4067c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
4077c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
4080a3e67a4SJesse Barnes 	else
4097c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
4107c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
411e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
4120a3e67a4SJesse Barnes 	return 0;
4130a3e67a4SJesse Barnes }
4140a3e67a4SJesse Barnes 
41542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
41642f52ef8SKeith Packard  * we use as a pipe index
41742f52ef8SKeith Packard  */
41842f52ef8SKeith Packard void i915_disable_vblank(struct drm_device *dev, int pipe)
4190a3e67a4SJesse Barnes {
4200a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
421e9d21d7fSKeith Packard 	unsigned long irqflags;
4220a3e67a4SJesse Barnes 
423e9d21d7fSKeith Packard 	spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
4247c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
4257c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
4267c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
427e9d21d7fSKeith Packard 	spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
4280a3e67a4SJesse Barnes }
4290a3e67a4SJesse Barnes 
430c0e09200SDave Airlie /* Set the vblank monitor pipe
431c0e09200SDave Airlie  */
432c0e09200SDave Airlie int i915_vblank_pipe_set(struct drm_device *dev, void *data,
433c0e09200SDave Airlie 			 struct drm_file *file_priv)
434c0e09200SDave Airlie {
435c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
436c0e09200SDave Airlie 
437c0e09200SDave Airlie 	if (!dev_priv) {
438c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
439c0e09200SDave Airlie 		return -EINVAL;
440c0e09200SDave Airlie 	}
441c0e09200SDave Airlie 
442c0e09200SDave Airlie 	return 0;
443c0e09200SDave Airlie }
444c0e09200SDave Airlie 
445c0e09200SDave Airlie int i915_vblank_pipe_get(struct drm_device *dev, void *data,
446c0e09200SDave Airlie 			 struct drm_file *file_priv)
447c0e09200SDave Airlie {
448c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = dev->dev_private;
449c0e09200SDave Airlie 	drm_i915_vblank_pipe_t *pipe = data;
450c0e09200SDave Airlie 
451c0e09200SDave Airlie 	if (!dev_priv) {
452c0e09200SDave Airlie 		DRM_ERROR("called with no initialization\n");
453c0e09200SDave Airlie 		return -EINVAL;
454c0e09200SDave Airlie 	}
455c0e09200SDave Airlie 
4560a3e67a4SJesse Barnes 	pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
457c0e09200SDave Airlie 
458c0e09200SDave Airlie 	return 0;
459c0e09200SDave Airlie }
460c0e09200SDave Airlie 
461c0e09200SDave Airlie /**
462c0e09200SDave Airlie  * Schedule buffer swap at given vertical blank.
463c0e09200SDave Airlie  */
464c0e09200SDave Airlie int i915_vblank_swap(struct drm_device *dev, void *data,
465c0e09200SDave Airlie 		     struct drm_file *file_priv)
466c0e09200SDave Airlie {
467bd95e0a4SEric Anholt 	/* The delayed swap mechanism was fundamentally racy, and has been
468bd95e0a4SEric Anholt 	 * removed.  The model was that the client requested a delayed flip/swap
469bd95e0a4SEric Anholt 	 * from the kernel, then waited for vblank before continuing to perform
470bd95e0a4SEric Anholt 	 * rendering.  The problem was that the kernel might wake the client
471bd95e0a4SEric Anholt 	 * up before it dispatched the vblank swap (since the lock has to be
472bd95e0a4SEric Anholt 	 * held while touching the ringbuffer), in which case the client would
473bd95e0a4SEric Anholt 	 * clear and start the next frame before the swap occurred, and
474bd95e0a4SEric Anholt 	 * flicker would occur in addition to likely missing the vblank.
475bd95e0a4SEric Anholt 	 *
476bd95e0a4SEric Anholt 	 * In the absence of this ioctl, userland falls back to a correct path
477bd95e0a4SEric Anholt 	 * of waiting for a vblank, then dispatching the swap on its own.
478bd95e0a4SEric Anholt 	 * Context switching to userland and back is plenty fast enough for
479bd95e0a4SEric Anholt 	 * meeting the requirements of vblank swapping.
4800a3e67a4SJesse Barnes 	 */
481c0e09200SDave Airlie 	return -EINVAL;
482c0e09200SDave Airlie }
483c0e09200SDave Airlie 
484c0e09200SDave Airlie /* drm_dma.h hooks
485c0e09200SDave Airlie */
486c0e09200SDave Airlie void i915_driver_irq_preinstall(struct drm_device * dev)
487c0e09200SDave Airlie {
488c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
489c0e09200SDave Airlie 
4900a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xeffe);
4917c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
4927c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
4930a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
494ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
4957c463586SKeith Packard 	(void) I915_READ(IER);
496c0e09200SDave Airlie }
497c0e09200SDave Airlie 
4980a3e67a4SJesse Barnes int i915_driver_irq_postinstall(struct drm_device *dev)
499c0e09200SDave Airlie {
500c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5010a3e67a4SJesse Barnes 	int ret, num_pipes = 2;
502c0e09200SDave Airlie 
5030a3e67a4SJesse Barnes 	ret = drm_vblank_init(dev, num_pipes);
5040a3e67a4SJesse Barnes 	if (ret)
5050a3e67a4SJesse Barnes 		return ret;
5060a3e67a4SJesse Barnes 
5070a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
508ed4cb414SEric Anholt 
5090a3e67a4SJesse Barnes 	dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
5100a3e67a4SJesse Barnes 
5117c463586SKeith Packard 	/* Unmask the interrupts that we always want on. */
5127c463586SKeith Packard 	dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
5138ee1c3dbSMatthew Garrett 
5147c463586SKeith Packard 	dev_priv->pipestat[0] = 0;
5157c463586SKeith Packard 	dev_priv->pipestat[1] = 0;
5167c463586SKeith Packard 
5177c463586SKeith Packard 	/* Disable pipe interrupt enables, clear pending pipe status */
5187c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
5197c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
5207c463586SKeith Packard 	/* Clear pending interrupt status */
5217c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
5227c463586SKeith Packard 
523ed4cb414SEric Anholt 	I915_WRITE(IER, I915_INTERRUPT_ENABLE_MASK);
5247c463586SKeith Packard 	I915_WRITE(IMR, dev_priv->irq_mask_reg);
525ed4cb414SEric Anholt 	(void) I915_READ(IER);
526ed4cb414SEric Anholt 
5278ee1c3dbSMatthew Garrett 	opregion_enable_asle(dev);
528c0e09200SDave Airlie 	DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
5290a3e67a4SJesse Barnes 
5300a3e67a4SJesse Barnes 	return 0;
531c0e09200SDave Airlie }
532c0e09200SDave Airlie 
533c0e09200SDave Airlie void i915_driver_irq_uninstall(struct drm_device * dev)
534c0e09200SDave Airlie {
535c0e09200SDave Airlie 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
536c0e09200SDave Airlie 
537c0e09200SDave Airlie 	if (!dev_priv)
538c0e09200SDave Airlie 		return;
539c0e09200SDave Airlie 
5400a3e67a4SJesse Barnes 	dev_priv->vblank_pipe = 0;
5410a3e67a4SJesse Barnes 
5420a3e67a4SJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
5437c463586SKeith Packard 	I915_WRITE(PIPEASTAT, 0);
5447c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, 0);
5450a3e67a4SJesse Barnes 	I915_WRITE(IMR, 0xffffffff);
546ed4cb414SEric Anholt 	I915_WRITE(IER, 0x0);
547c0e09200SDave Airlie 
5487c463586SKeith Packard 	I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
5497c463586SKeith Packard 	I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
5507c463586SKeith Packard 	I915_WRITE(IIR, I915_READ(IIR));
551c0e09200SDave Airlie }
552